Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Xilinx SystemACE device driver |
| 3 | * |
| 4 | * Copyright 2007 Secret Lab Technologies Ltd. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify it |
| 7 | * under the terms of the GNU General Public License version 2 as published |
| 8 | * by the Free Software Foundation. |
| 9 | */ |
| 10 | |
| 11 | /* |
| 12 | * The SystemACE chip is designed to configure FPGAs by loading an FPGA |
| 13 | * bitstream from a file on a CF card and squirting it into FPGAs connected |
| 14 | * to the SystemACE JTAG chain. It also has the advantage of providing an |
| 15 | * MPU interface which can be used to control the FPGA configuration process |
| 16 | * and to use the attached CF card for general purpose storage. |
| 17 | * |
| 18 | * This driver is a block device driver for the SystemACE. |
| 19 | * |
| 20 | * Initialization: |
| 21 | * The driver registers itself as a platform_device driver at module |
| 22 | * load time. The platform bus will take care of calling the |
| 23 | * ace_probe() method for all SystemACE instances in the system. Any |
| 24 | * number of SystemACE instances are supported. ace_probe() calls |
| 25 | * ace_setup() which initialized all data structures, reads the CF |
| 26 | * id structure and registers the device. |
| 27 | * |
| 28 | * Processing: |
| 29 | * Just about all of the heavy lifting in this driver is performed by |
| 30 | * a Finite State Machine (FSM). The driver needs to wait on a number |
| 31 | * of events; some raised by interrupts, some which need to be polled |
| 32 | * for. Describing all of the behaviour in a FSM seems to be the |
| 33 | * easiest way to keep the complexity low and make it easy to |
| 34 | * understand what the driver is doing. If the block ops or the |
| 35 | * request function need to interact with the hardware, then they |
| 36 | * simply need to flag the request and kick of FSM processing. |
| 37 | * |
| 38 | * The FSM itself is atomic-safe code which can be run from any |
| 39 | * context. The general process flow is: |
| 40 | * 1. obtain the ace->lock spinlock. |
| 41 | * 2. loop on ace_fsm_dostate() until the ace->fsm_continue flag is |
| 42 | * cleared. |
| 43 | * 3. release the lock. |
| 44 | * |
| 45 | * Individual states do not sleep in any way. If a condition needs to |
| 46 | * be waited for then the state much clear the fsm_continue flag and |
| 47 | * either schedule the FSM to be run again at a later time, or expect |
| 48 | * an interrupt to call the FSM when the desired condition is met. |
| 49 | * |
| 50 | * In normal operation, the FSM is processed at interrupt context |
| 51 | * either when the driver's tasklet is scheduled, or when an irq is |
| 52 | * raised by the hardware. The tasklet can be scheduled at any time. |
| 53 | * The request method in particular schedules the tasklet when a new |
| 54 | * request has been indicated by the block layer. Once started, the |
| 55 | * FSM proceeds as far as it can processing the request until it |
| 56 | * needs on a hardware event. At this point, it must yield execution. |
| 57 | * |
| 58 | * A state has two options when yielding execution: |
| 59 | * 1. ace_fsm_yield() |
| 60 | * - Call if need to poll for event. |
| 61 | * - clears the fsm_continue flag to exit the processing loop |
| 62 | * - reschedules the tasklet to run again as soon as possible |
| 63 | * 2. ace_fsm_yieldirq() |
| 64 | * - Call if an irq is expected from the HW |
| 65 | * - clears the fsm_continue flag to exit the processing loop |
| 66 | * - does not reschedule the tasklet so the FSM will not be processed |
| 67 | * again until an irq is received. |
| 68 | * After calling a yield function, the state must return control back |
| 69 | * to the FSM main loop. |
| 70 | * |
| 71 | * Additionally, the driver maintains a kernel timer which can process |
| 72 | * the FSM. If the FSM gets stalled, typically due to a missed |
| 73 | * interrupt, then the kernel timer will expire and the driver can |
| 74 | * continue where it left off. |
| 75 | * |
| 76 | * To Do: |
| 77 | * - Add FPGA configuration control interface. |
| 78 | * - Request major number from lanana |
| 79 | */ |
| 80 | |
| 81 | #undef DEBUG |
| 82 | |
| 83 | #include <linux/module.h> |
| 84 | #include <linux/ctype.h> |
| 85 | #include <linux/init.h> |
| 86 | #include <linux/interrupt.h> |
| 87 | #include <linux/errno.h> |
| 88 | #include <linux/kernel.h> |
| 89 | #include <linux/delay.h> |
| 90 | #include <linux/slab.h> |
Jens Axboe | 804186f | 2018-10-15 09:05:59 -0600 | [diff] [blame] | 91 | #include <linux/blk-mq.h> |
Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 92 | #include <linux/mutex.h> |
Bartlomiej Zolnierkiewicz | 4aaf2fe | 2009-04-01 21:42:22 +0200 | [diff] [blame] | 93 | #include <linux/ata.h> |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 94 | #include <linux/hdreg.h> |
| 95 | #include <linux/platform_device.h> |
Grant Likely | 95e896c | 2007-10-01 16:33:55 +0200 | [diff] [blame] | 96 | #if defined(CONFIG_OF) |
Graeme Smecher | 7a50d06 | 2010-08-17 10:13:44 -0700 | [diff] [blame] | 97 | #include <linux/of_address.h> |
Grant Likely | 95e896c | 2007-10-01 16:33:55 +0200 | [diff] [blame] | 98 | #include <linux/of_device.h> |
| 99 | #include <linux/of_platform.h> |
| 100 | #endif |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 101 | |
| 102 | MODULE_AUTHOR("Grant Likely <grant.likely@secretlab.ca>"); |
| 103 | MODULE_DESCRIPTION("Xilinx SystemACE device driver"); |
| 104 | MODULE_LICENSE("GPL"); |
| 105 | |
| 106 | /* SystemACE register definitions */ |
| 107 | #define ACE_BUSMODE (0x00) |
| 108 | |
| 109 | #define ACE_STATUS (0x04) |
| 110 | #define ACE_STATUS_CFGLOCK (0x00000001) |
| 111 | #define ACE_STATUS_MPULOCK (0x00000002) |
| 112 | #define ACE_STATUS_CFGERROR (0x00000004) /* config controller error */ |
| 113 | #define ACE_STATUS_CFCERROR (0x00000008) /* CF controller error */ |
| 114 | #define ACE_STATUS_CFDETECT (0x00000010) |
| 115 | #define ACE_STATUS_DATABUFRDY (0x00000020) |
| 116 | #define ACE_STATUS_DATABUFMODE (0x00000040) |
| 117 | #define ACE_STATUS_CFGDONE (0x00000080) |
| 118 | #define ACE_STATUS_RDYFORCFCMD (0x00000100) |
| 119 | #define ACE_STATUS_CFGMODEPIN (0x00000200) |
| 120 | #define ACE_STATUS_CFGADDR_MASK (0x0000e000) |
| 121 | #define ACE_STATUS_CFBSY (0x00020000) |
| 122 | #define ACE_STATUS_CFRDY (0x00040000) |
| 123 | #define ACE_STATUS_CFDWF (0x00080000) |
| 124 | #define ACE_STATUS_CFDSC (0x00100000) |
| 125 | #define ACE_STATUS_CFDRQ (0x00200000) |
| 126 | #define ACE_STATUS_CFCORR (0x00400000) |
| 127 | #define ACE_STATUS_CFERR (0x00800000) |
| 128 | |
| 129 | #define ACE_ERROR (0x08) |
| 130 | #define ACE_CFGLBA (0x0c) |
| 131 | #define ACE_MPULBA (0x10) |
| 132 | |
| 133 | #define ACE_SECCNTCMD (0x14) |
| 134 | #define ACE_SECCNTCMD_RESET (0x0100) |
| 135 | #define ACE_SECCNTCMD_IDENTIFY (0x0200) |
| 136 | #define ACE_SECCNTCMD_READ_DATA (0x0300) |
| 137 | #define ACE_SECCNTCMD_WRITE_DATA (0x0400) |
| 138 | #define ACE_SECCNTCMD_ABORT (0x0600) |
| 139 | |
| 140 | #define ACE_VERSION (0x16) |
| 141 | #define ACE_VERSION_REVISION_MASK (0x00FF) |
| 142 | #define ACE_VERSION_MINOR_MASK (0x0F00) |
| 143 | #define ACE_VERSION_MAJOR_MASK (0xF000) |
| 144 | |
| 145 | #define ACE_CTRL (0x18) |
| 146 | #define ACE_CTRL_FORCELOCKREQ (0x0001) |
| 147 | #define ACE_CTRL_LOCKREQ (0x0002) |
| 148 | #define ACE_CTRL_FORCECFGADDR (0x0004) |
| 149 | #define ACE_CTRL_FORCECFGMODE (0x0008) |
| 150 | #define ACE_CTRL_CFGMODE (0x0010) |
| 151 | #define ACE_CTRL_CFGSTART (0x0020) |
| 152 | #define ACE_CTRL_CFGSEL (0x0040) |
| 153 | #define ACE_CTRL_CFGRESET (0x0080) |
| 154 | #define ACE_CTRL_DATABUFRDYIRQ (0x0100) |
| 155 | #define ACE_CTRL_ERRORIRQ (0x0200) |
| 156 | #define ACE_CTRL_CFGDONEIRQ (0x0400) |
| 157 | #define ACE_CTRL_RESETIRQ (0x0800) |
| 158 | #define ACE_CTRL_CFGPROG (0x1000) |
| 159 | #define ACE_CTRL_CFGADDR_MASK (0xe000) |
| 160 | |
| 161 | #define ACE_FATSTAT (0x1c) |
| 162 | |
| 163 | #define ACE_NUM_MINORS 16 |
| 164 | #define ACE_SECTOR_SIZE (512) |
| 165 | #define ACE_FIFO_SIZE (32) |
| 166 | #define ACE_BUF_PER_SECTOR (ACE_SECTOR_SIZE / ACE_FIFO_SIZE) |
| 167 | |
Grant Likely | 4a24d86 | 2007-10-01 16:33:54 +0200 | [diff] [blame] | 168 | #define ACE_BUS_WIDTH_8 0 |
| 169 | #define ACE_BUS_WIDTH_16 1 |
| 170 | |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 171 | struct ace_reg_ops; |
| 172 | |
| 173 | struct ace_device { |
| 174 | /* driver state data */ |
| 175 | int id; |
| 176 | int media_change; |
| 177 | int users; |
| 178 | struct list_head list; |
| 179 | |
| 180 | /* finite state machine data */ |
| 181 | struct tasklet_struct fsm_tasklet; |
| 182 | uint fsm_task; /* Current activity (ACE_TASK_*) */ |
| 183 | uint fsm_state; /* Current state (ACE_FSM_STATE_*) */ |
| 184 | uint fsm_continue_flag; /* cleared to exit FSM mainloop */ |
| 185 | uint fsm_iter_num; |
| 186 | struct timer_list stall_timer; |
| 187 | |
| 188 | /* Transfer state/result, use for both id and block request */ |
| 189 | struct request *req; /* request being processed */ |
| 190 | void *data_ptr; /* pointer to I/O buffer */ |
| 191 | int data_count; /* number of buffers remaining */ |
| 192 | int data_result; /* Result of transfer; 0 := success */ |
| 193 | |
| 194 | int id_req_count; /* count of id requests */ |
| 195 | int id_result; |
| 196 | struct completion id_completion; /* used when id req finishes */ |
| 197 | int in_irq; |
| 198 | |
| 199 | /* Details of hardware device */ |
Yuri Tikhonov | c14464b | 2008-11-14 10:21:57 -0700 | [diff] [blame] | 200 | resource_size_t physaddr; |
Grant Likely | b5515d8 | 2007-10-04 08:52:39 +0200 | [diff] [blame] | 201 | void __iomem *baseaddr; |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 202 | int irq; |
| 203 | int bus_width; /* 0 := 8 bit; 1 := 16 bit */ |
| 204 | struct ace_reg_ops *reg_ops; |
| 205 | int lock_count; |
| 206 | |
| 207 | /* Block device data structures */ |
| 208 | spinlock_t lock; |
| 209 | struct device *dev; |
| 210 | struct request_queue *queue; |
| 211 | struct gendisk *gd; |
Jens Axboe | 804186f | 2018-10-15 09:05:59 -0600 | [diff] [blame] | 212 | struct blk_mq_tag_set tag_set; |
| 213 | struct list_head rq_list; |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 214 | |
| 215 | /* Inserted CF card parameters */ |
Bartlomiej Zolnierkiewicz | 4aaf2fe | 2009-04-01 21:42:22 +0200 | [diff] [blame] | 216 | u16 cf_id[ATA_ID_WORDS]; |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 217 | }; |
| 218 | |
Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 219 | static DEFINE_MUTEX(xsysace_mutex); |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 220 | static int ace_major; |
| 221 | |
| 222 | /* --------------------------------------------------------------------- |
| 223 | * Low level register access |
| 224 | */ |
| 225 | |
| 226 | struct ace_reg_ops { |
| 227 | u16(*in) (struct ace_device * ace, int reg); |
| 228 | void (*out) (struct ace_device * ace, int reg, u16 val); |
| 229 | void (*datain) (struct ace_device * ace); |
| 230 | void (*dataout) (struct ace_device * ace); |
| 231 | }; |
| 232 | |
| 233 | /* 8 Bit bus width */ |
| 234 | static u16 ace_in_8(struct ace_device *ace, int reg) |
| 235 | { |
Grant Likely | b5515d8 | 2007-10-04 08:52:39 +0200 | [diff] [blame] | 236 | void __iomem *r = ace->baseaddr + reg; |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 237 | return in_8(r) | (in_8(r + 1) << 8); |
| 238 | } |
| 239 | |
| 240 | static void ace_out_8(struct ace_device *ace, int reg, u16 val) |
| 241 | { |
Grant Likely | b5515d8 | 2007-10-04 08:52:39 +0200 | [diff] [blame] | 242 | void __iomem *r = ace->baseaddr + reg; |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 243 | out_8(r, val); |
| 244 | out_8(r + 1, val >> 8); |
| 245 | } |
| 246 | |
| 247 | static void ace_datain_8(struct ace_device *ace) |
| 248 | { |
Grant Likely | b5515d8 | 2007-10-04 08:52:39 +0200 | [diff] [blame] | 249 | void __iomem *r = ace->baseaddr + 0x40; |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 250 | u8 *dst = ace->data_ptr; |
| 251 | int i = ACE_FIFO_SIZE; |
| 252 | while (i--) |
| 253 | *dst++ = in_8(r++); |
| 254 | ace->data_ptr = dst; |
| 255 | } |
| 256 | |
| 257 | static void ace_dataout_8(struct ace_device *ace) |
| 258 | { |
Grant Likely | b5515d8 | 2007-10-04 08:52:39 +0200 | [diff] [blame] | 259 | void __iomem *r = ace->baseaddr + 0x40; |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 260 | u8 *src = ace->data_ptr; |
| 261 | int i = ACE_FIFO_SIZE; |
| 262 | while (i--) |
| 263 | out_8(r++, *src++); |
| 264 | ace->data_ptr = src; |
| 265 | } |
| 266 | |
| 267 | static struct ace_reg_ops ace_reg_8_ops = { |
| 268 | .in = ace_in_8, |
| 269 | .out = ace_out_8, |
| 270 | .datain = ace_datain_8, |
| 271 | .dataout = ace_dataout_8, |
| 272 | }; |
| 273 | |
| 274 | /* 16 bit big endian bus attachment */ |
| 275 | static u16 ace_in_be16(struct ace_device *ace, int reg) |
| 276 | { |
| 277 | return in_be16(ace->baseaddr + reg); |
| 278 | } |
| 279 | |
| 280 | static void ace_out_be16(struct ace_device *ace, int reg, u16 val) |
| 281 | { |
| 282 | out_be16(ace->baseaddr + reg, val); |
| 283 | } |
| 284 | |
| 285 | static void ace_datain_be16(struct ace_device *ace) |
| 286 | { |
| 287 | int i = ACE_FIFO_SIZE / 2; |
| 288 | u16 *dst = ace->data_ptr; |
| 289 | while (i--) |
| 290 | *dst++ = in_le16(ace->baseaddr + 0x40); |
| 291 | ace->data_ptr = dst; |
| 292 | } |
| 293 | |
| 294 | static void ace_dataout_be16(struct ace_device *ace) |
| 295 | { |
| 296 | int i = ACE_FIFO_SIZE / 2; |
| 297 | u16 *src = ace->data_ptr; |
| 298 | while (i--) |
| 299 | out_le16(ace->baseaddr + 0x40, *src++); |
| 300 | ace->data_ptr = src; |
| 301 | } |
| 302 | |
| 303 | /* 16 bit little endian bus attachment */ |
| 304 | static u16 ace_in_le16(struct ace_device *ace, int reg) |
| 305 | { |
| 306 | return in_le16(ace->baseaddr + reg); |
| 307 | } |
| 308 | |
| 309 | static void ace_out_le16(struct ace_device *ace, int reg, u16 val) |
| 310 | { |
| 311 | out_le16(ace->baseaddr + reg, val); |
| 312 | } |
| 313 | |
| 314 | static void ace_datain_le16(struct ace_device *ace) |
| 315 | { |
| 316 | int i = ACE_FIFO_SIZE / 2; |
| 317 | u16 *dst = ace->data_ptr; |
| 318 | while (i--) |
| 319 | *dst++ = in_be16(ace->baseaddr + 0x40); |
| 320 | ace->data_ptr = dst; |
| 321 | } |
| 322 | |
| 323 | static void ace_dataout_le16(struct ace_device *ace) |
| 324 | { |
| 325 | int i = ACE_FIFO_SIZE / 2; |
| 326 | u16 *src = ace->data_ptr; |
| 327 | while (i--) |
| 328 | out_be16(ace->baseaddr + 0x40, *src++); |
| 329 | ace->data_ptr = src; |
| 330 | } |
| 331 | |
| 332 | static struct ace_reg_ops ace_reg_be16_ops = { |
| 333 | .in = ace_in_be16, |
| 334 | .out = ace_out_be16, |
| 335 | .datain = ace_datain_be16, |
| 336 | .dataout = ace_dataout_be16, |
| 337 | }; |
| 338 | |
| 339 | static struct ace_reg_ops ace_reg_le16_ops = { |
| 340 | .in = ace_in_le16, |
| 341 | .out = ace_out_le16, |
| 342 | .datain = ace_datain_le16, |
| 343 | .dataout = ace_dataout_le16, |
| 344 | }; |
| 345 | |
| 346 | static inline u16 ace_in(struct ace_device *ace, int reg) |
| 347 | { |
| 348 | return ace->reg_ops->in(ace, reg); |
| 349 | } |
| 350 | |
| 351 | static inline u32 ace_in32(struct ace_device *ace, int reg) |
| 352 | { |
| 353 | return ace_in(ace, reg) | (ace_in(ace, reg + 2) << 16); |
| 354 | } |
| 355 | |
| 356 | static inline void ace_out(struct ace_device *ace, int reg, u16 val) |
| 357 | { |
| 358 | ace->reg_ops->out(ace, reg, val); |
| 359 | } |
| 360 | |
| 361 | static inline void ace_out32(struct ace_device *ace, int reg, u32 val) |
| 362 | { |
| 363 | ace_out(ace, reg, val); |
| 364 | ace_out(ace, reg + 2, val >> 16); |
| 365 | } |
| 366 | |
| 367 | /* --------------------------------------------------------------------- |
| 368 | * Debug support functions |
| 369 | */ |
| 370 | |
| 371 | #if defined(DEBUG) |
| 372 | static void ace_dump_mem(void *base, int len) |
| 373 | { |
| 374 | const char *ptr = base; |
| 375 | int i, j; |
| 376 | |
| 377 | for (i = 0; i < len; i += 16) { |
| 378 | printk(KERN_INFO "%.8x:", i); |
| 379 | for (j = 0; j < 16; j++) { |
| 380 | if (!(j % 4)) |
| 381 | printk(" "); |
| 382 | printk("%.2x", ptr[i + j]); |
| 383 | } |
| 384 | printk(" "); |
| 385 | for (j = 0; j < 16; j++) |
| 386 | printk("%c", isprint(ptr[i + j]) ? ptr[i + j] : '.'); |
| 387 | printk("\n"); |
| 388 | } |
| 389 | } |
| 390 | #else |
| 391 | static inline void ace_dump_mem(void *base, int len) |
| 392 | { |
| 393 | } |
| 394 | #endif |
| 395 | |
| 396 | static void ace_dump_regs(struct ace_device *ace) |
| 397 | { |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 398 | dev_info(ace->dev, |
| 399 | " ctrl: %.8x seccnt/cmd: %.4x ver:%.4x\n" |
| 400 | " status:%.8x mpu_lba:%.8x busmode:%4x\n" |
| 401 | " error: %.8x cfg_lba:%.8x fatstat:%.4x\n", |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 402 | ace_in32(ace, ACE_CTRL), |
| 403 | ace_in(ace, ACE_SECCNTCMD), |
| 404 | ace_in(ace, ACE_VERSION), |
| 405 | ace_in32(ace, ACE_STATUS), |
| 406 | ace_in32(ace, ACE_MPULBA), |
| 407 | ace_in(ace, ACE_BUSMODE), |
| 408 | ace_in32(ace, ACE_ERROR), |
| 409 | ace_in32(ace, ACE_CFGLBA), ace_in(ace, ACE_FATSTAT)); |
| 410 | } |
| 411 | |
Michal Simek | 4937a26 | 2013-02-07 17:28:20 +0100 | [diff] [blame] | 412 | static void ace_fix_driveid(u16 *id) |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 413 | { |
| 414 | #if defined(__BIG_ENDIAN) |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 415 | int i; |
| 416 | |
| 417 | /* All half words have wrong byte order; swap the bytes */ |
Bartlomiej Zolnierkiewicz | 4aaf2fe | 2009-04-01 21:42:22 +0200 | [diff] [blame] | 418 | for (i = 0; i < ATA_ID_WORDS; i++, id++) |
| 419 | *id = le16_to_cpu(*id); |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 420 | #endif |
| 421 | } |
| 422 | |
| 423 | /* --------------------------------------------------------------------- |
| 424 | * Finite State Machine (FSM) implementation |
| 425 | */ |
| 426 | |
| 427 | /* FSM tasks; used to direct state transitions */ |
| 428 | #define ACE_TASK_IDLE 0 |
| 429 | #define ACE_TASK_IDENTIFY 1 |
| 430 | #define ACE_TASK_READ 2 |
| 431 | #define ACE_TASK_WRITE 3 |
| 432 | #define ACE_FSM_NUM_TASKS 4 |
| 433 | |
| 434 | /* FSM state definitions */ |
| 435 | #define ACE_FSM_STATE_IDLE 0 |
| 436 | #define ACE_FSM_STATE_REQ_LOCK 1 |
| 437 | #define ACE_FSM_STATE_WAIT_LOCK 2 |
| 438 | #define ACE_FSM_STATE_WAIT_CFREADY 3 |
| 439 | #define ACE_FSM_STATE_IDENTIFY_PREPARE 4 |
| 440 | #define ACE_FSM_STATE_IDENTIFY_TRANSFER 5 |
| 441 | #define ACE_FSM_STATE_IDENTIFY_COMPLETE 6 |
| 442 | #define ACE_FSM_STATE_REQ_PREPARE 7 |
| 443 | #define ACE_FSM_STATE_REQ_TRANSFER 8 |
| 444 | #define ACE_FSM_STATE_REQ_COMPLETE 9 |
| 445 | #define ACE_FSM_STATE_ERROR 10 |
| 446 | #define ACE_FSM_NUM_STATES 11 |
| 447 | |
| 448 | /* Set flag to exit FSM loop and reschedule tasklet */ |
| 449 | static inline void ace_fsm_yield(struct ace_device *ace) |
| 450 | { |
| 451 | dev_dbg(ace->dev, "ace_fsm_yield()\n"); |
| 452 | tasklet_schedule(&ace->fsm_tasklet); |
| 453 | ace->fsm_continue_flag = 0; |
| 454 | } |
| 455 | |
| 456 | /* Set flag to exit FSM loop and wait for IRQ to reschedule tasklet */ |
| 457 | static inline void ace_fsm_yieldirq(struct ace_device *ace) |
| 458 | { |
| 459 | dev_dbg(ace->dev, "ace_fsm_yieldirq()\n"); |
| 460 | |
Michal Simek | ba2d5af | 2011-12-20 11:09:14 +0100 | [diff] [blame] | 461 | if (!ace->irq) |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 462 | /* No IRQ assigned, so need to poll */ |
| 463 | tasklet_schedule(&ace->fsm_tasklet); |
| 464 | ace->fsm_continue_flag = 0; |
| 465 | } |
| 466 | |
Jens Axboe | 804186f | 2018-10-15 09:05:59 -0600 | [diff] [blame] | 467 | static bool ace_has_next_request(struct request_queue *q) |
| 468 | { |
| 469 | struct ace_device *ace = q->queuedata; |
| 470 | |
| 471 | return !list_empty(&ace->rq_list); |
| 472 | } |
| 473 | |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 474 | /* Get the next read/write request; ending requests that we don't handle */ |
Michal Simek | 4937a26 | 2013-02-07 17:28:20 +0100 | [diff] [blame] | 475 | static struct request *ace_get_next_request(struct request_queue *q) |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 476 | { |
Jens Axboe | 804186f | 2018-10-15 09:05:59 -0600 | [diff] [blame] | 477 | struct ace_device *ace = q->queuedata; |
| 478 | struct request *rq; |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 479 | |
Jens Axboe | 804186f | 2018-10-15 09:05:59 -0600 | [diff] [blame] | 480 | rq = list_first_entry_or_null(&ace->rq_list, struct request, queuelist); |
| 481 | if (rq) { |
| 482 | list_del_init(&rq->queuelist); |
| 483 | blk_mq_start_request(rq); |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 484 | } |
Jens Axboe | 804186f | 2018-10-15 09:05:59 -0600 | [diff] [blame] | 485 | |
| 486 | return NULL; |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | static void ace_fsm_dostate(struct ace_device *ace) |
| 490 | { |
| 491 | struct request *req; |
| 492 | u32 status; |
| 493 | u16 val; |
| 494 | int count; |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 495 | |
| 496 | #if defined(DEBUG) |
| 497 | dev_dbg(ace->dev, "fsm_state=%i, id_req_count=%i\n", |
| 498 | ace->fsm_state, ace->id_req_count); |
| 499 | #endif |
| 500 | |
Grant Likely | bfbd442 | 2009-03-09 13:42:24 +0100 | [diff] [blame] | 501 | /* Verify that there is actually a CF in the slot. If not, then |
| 502 | * bail out back to the idle state and wake up all the waiters */ |
| 503 | status = ace_in32(ace, ACE_STATUS); |
| 504 | if ((status & ACE_STATUS_CFDETECT) == 0) { |
| 505 | ace->fsm_state = ACE_FSM_STATE_IDLE; |
| 506 | ace->media_change = 1; |
| 507 | set_capacity(ace->gd, 0); |
| 508 | dev_info(ace->dev, "No CF in slot\n"); |
| 509 | |
Tejun Heo | 2d75ce0 | 2009-05-08 11:54:05 +0900 | [diff] [blame] | 510 | /* Drop all in-flight and pending requests */ |
| 511 | if (ace->req) { |
Jens Axboe | 804186f | 2018-10-15 09:05:59 -0600 | [diff] [blame] | 512 | blk_mq_end_request(ace->req, BLK_STS_IOERR); |
Tejun Heo | 2d75ce0 | 2009-05-08 11:54:05 +0900 | [diff] [blame] | 513 | ace->req = NULL; |
| 514 | } |
Jens Axboe | 804186f | 2018-10-15 09:05:59 -0600 | [diff] [blame] | 515 | while ((req = ace_get_next_request(ace->queue)) != NULL) |
| 516 | blk_mq_end_request(req, BLK_STS_IOERR); |
Grant Likely | bfbd442 | 2009-03-09 13:42:24 +0100 | [diff] [blame] | 517 | |
| 518 | /* Drop back to IDLE state and notify waiters */ |
| 519 | ace->fsm_state = ACE_FSM_STATE_IDLE; |
| 520 | ace->id_result = -EIO; |
| 521 | while (ace->id_req_count) { |
| 522 | complete(&ace->id_completion); |
| 523 | ace->id_req_count--; |
| 524 | } |
| 525 | } |
| 526 | |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 527 | switch (ace->fsm_state) { |
| 528 | case ACE_FSM_STATE_IDLE: |
| 529 | /* See if there is anything to do */ |
Jens Axboe | 804186f | 2018-10-15 09:05:59 -0600 | [diff] [blame] | 530 | if (ace->id_req_count || ace_has_next_request(ace->queue)) { |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 531 | ace->fsm_iter_num++; |
| 532 | ace->fsm_state = ACE_FSM_STATE_REQ_LOCK; |
| 533 | mod_timer(&ace->stall_timer, jiffies + HZ); |
| 534 | if (!timer_pending(&ace->stall_timer)) |
| 535 | add_timer(&ace->stall_timer); |
| 536 | break; |
| 537 | } |
| 538 | del_timer(&ace->stall_timer); |
| 539 | ace->fsm_continue_flag = 0; |
| 540 | break; |
| 541 | |
| 542 | case ACE_FSM_STATE_REQ_LOCK: |
| 543 | if (ace_in(ace, ACE_STATUS) & ACE_STATUS_MPULOCK) { |
| 544 | /* Already have the lock, jump to next state */ |
| 545 | ace->fsm_state = ACE_FSM_STATE_WAIT_CFREADY; |
| 546 | break; |
| 547 | } |
| 548 | |
| 549 | /* Request the lock */ |
| 550 | val = ace_in(ace, ACE_CTRL); |
| 551 | ace_out(ace, ACE_CTRL, val | ACE_CTRL_LOCKREQ); |
| 552 | ace->fsm_state = ACE_FSM_STATE_WAIT_LOCK; |
| 553 | break; |
| 554 | |
| 555 | case ACE_FSM_STATE_WAIT_LOCK: |
| 556 | if (ace_in(ace, ACE_STATUS) & ACE_STATUS_MPULOCK) { |
| 557 | /* got the lock; move to next state */ |
| 558 | ace->fsm_state = ACE_FSM_STATE_WAIT_CFREADY; |
| 559 | break; |
| 560 | } |
| 561 | |
| 562 | /* wait a bit for the lock */ |
| 563 | ace_fsm_yield(ace); |
| 564 | break; |
| 565 | |
| 566 | case ACE_FSM_STATE_WAIT_CFREADY: |
| 567 | status = ace_in32(ace, ACE_STATUS); |
| 568 | if (!(status & ACE_STATUS_RDYFORCFCMD) || |
| 569 | (status & ACE_STATUS_CFBSY)) { |
| 570 | /* CF card isn't ready; it needs to be polled */ |
| 571 | ace_fsm_yield(ace); |
| 572 | break; |
| 573 | } |
| 574 | |
| 575 | /* Device is ready for command; determine what to do next */ |
| 576 | if (ace->id_req_count) |
| 577 | ace->fsm_state = ACE_FSM_STATE_IDENTIFY_PREPARE; |
| 578 | else |
| 579 | ace->fsm_state = ACE_FSM_STATE_REQ_PREPARE; |
| 580 | break; |
| 581 | |
| 582 | case ACE_FSM_STATE_IDENTIFY_PREPARE: |
| 583 | /* Send identify command */ |
| 584 | ace->fsm_task = ACE_TASK_IDENTIFY; |
Grant Likely | f0edef8 | 2009-04-08 14:13:04 +0200 | [diff] [blame] | 585 | ace->data_ptr = ace->cf_id; |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 586 | ace->data_count = ACE_BUF_PER_SECTOR; |
| 587 | ace_out(ace, ACE_SECCNTCMD, ACE_SECCNTCMD_IDENTIFY); |
| 588 | |
| 589 | /* As per datasheet, put config controller in reset */ |
| 590 | val = ace_in(ace, ACE_CTRL); |
| 591 | ace_out(ace, ACE_CTRL, val | ACE_CTRL_CFGRESET); |
| 592 | |
| 593 | /* irq handler takes over from this point; wait for the |
| 594 | * transfer to complete */ |
| 595 | ace->fsm_state = ACE_FSM_STATE_IDENTIFY_TRANSFER; |
| 596 | ace_fsm_yieldirq(ace); |
| 597 | break; |
| 598 | |
| 599 | case ACE_FSM_STATE_IDENTIFY_TRANSFER: |
| 600 | /* Check that the sysace is ready to receive data */ |
| 601 | status = ace_in32(ace, ACE_STATUS); |
| 602 | if (status & ACE_STATUS_CFBSY) { |
| 603 | dev_dbg(ace->dev, "CFBSY set; t=%i iter=%i dc=%i\n", |
| 604 | ace->fsm_task, ace->fsm_iter_num, |
| 605 | ace->data_count); |
| 606 | ace_fsm_yield(ace); |
| 607 | break; |
| 608 | } |
| 609 | if (!(status & ACE_STATUS_DATABUFRDY)) { |
| 610 | ace_fsm_yield(ace); |
| 611 | break; |
| 612 | } |
| 613 | |
| 614 | /* Transfer the next buffer */ |
| 615 | ace->reg_ops->datain(ace); |
| 616 | ace->data_count--; |
| 617 | |
| 618 | /* If there are still buffers to be transfers; jump out here */ |
| 619 | if (ace->data_count != 0) { |
| 620 | ace_fsm_yieldirq(ace); |
| 621 | break; |
| 622 | } |
| 623 | |
| 624 | /* transfer finished; kick state machine */ |
| 625 | dev_dbg(ace->dev, "identify finished\n"); |
| 626 | ace->fsm_state = ACE_FSM_STATE_IDENTIFY_COMPLETE; |
| 627 | break; |
| 628 | |
| 629 | case ACE_FSM_STATE_IDENTIFY_COMPLETE: |
Grant Likely | f0edef8 | 2009-04-08 14:13:04 +0200 | [diff] [blame] | 630 | ace_fix_driveid(ace->cf_id); |
| 631 | ace_dump_mem(ace->cf_id, 512); /* Debug: Dump out disk ID */ |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 632 | |
| 633 | if (ace->data_result) { |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 634 | /* Error occurred, disable the disk */ |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 635 | ace->media_change = 1; |
| 636 | set_capacity(ace->gd, 0); |
| 637 | dev_err(ace->dev, "error fetching CF id (%i)\n", |
| 638 | ace->data_result); |
| 639 | } else { |
| 640 | ace->media_change = 0; |
| 641 | |
| 642 | /* Record disk parameters */ |
Bartlomiej Zolnierkiewicz | 4aaf2fe | 2009-04-01 21:42:22 +0200 | [diff] [blame] | 643 | set_capacity(ace->gd, |
Grant Likely | f0edef8 | 2009-04-08 14:13:04 +0200 | [diff] [blame] | 644 | ata_id_u32(ace->cf_id, ATA_ID_LBA_CAPACITY)); |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 645 | dev_info(ace->dev, "capacity: %i sectors\n", |
Grant Likely | f0edef8 | 2009-04-08 14:13:04 +0200 | [diff] [blame] | 646 | ata_id_u32(ace->cf_id, ATA_ID_LBA_CAPACITY)); |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 647 | } |
| 648 | |
| 649 | /* We're done, drop to IDLE state and notify waiters */ |
| 650 | ace->fsm_state = ACE_FSM_STATE_IDLE; |
| 651 | ace->id_result = ace->data_result; |
| 652 | while (ace->id_req_count) { |
| 653 | complete(&ace->id_completion); |
| 654 | ace->id_req_count--; |
| 655 | } |
| 656 | break; |
| 657 | |
| 658 | case ACE_FSM_STATE_REQ_PREPARE: |
| 659 | req = ace_get_next_request(ace->queue); |
| 660 | if (!req) { |
| 661 | ace->fsm_state = ACE_FSM_STATE_IDLE; |
| 662 | break; |
| 663 | } |
| 664 | |
| 665 | /* Okay, it's a data request, set it up for transfer */ |
| 666 | dev_dbg(ace->dev, |
Tejun Heo | 5b93629 | 2009-05-07 22:24:38 +0900 | [diff] [blame] | 667 | "request: sec=%llx hcnt=%x, ccnt=%x, dir=%i\n", |
Tejun Heo | 83096eb | 2009-05-07 22:24:39 +0900 | [diff] [blame] | 668 | (unsigned long long)blk_rq_pos(req), |
| 669 | blk_rq_sectors(req), blk_rq_cur_sectors(req), |
| 670 | rq_data_dir(req)); |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 671 | |
| 672 | ace->req = req; |
Jens Axboe | b4f42e2 | 2014-04-10 09:46:28 -0600 | [diff] [blame] | 673 | ace->data_ptr = bio_data(req->bio); |
Tejun Heo | 83096eb | 2009-05-07 22:24:39 +0900 | [diff] [blame] | 674 | ace->data_count = blk_rq_cur_sectors(req) * ACE_BUF_PER_SECTOR; |
| 675 | ace_out32(ace, ACE_MPULBA, blk_rq_pos(req) & 0x0FFFFFFF); |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 676 | |
Tejun Heo | 5b93629 | 2009-05-07 22:24:38 +0900 | [diff] [blame] | 677 | count = blk_rq_sectors(req); |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 678 | if (rq_data_dir(req)) { |
| 679 | /* Kick off write request */ |
| 680 | dev_dbg(ace->dev, "write data\n"); |
| 681 | ace->fsm_task = ACE_TASK_WRITE; |
| 682 | ace_out(ace, ACE_SECCNTCMD, |
| 683 | count | ACE_SECCNTCMD_WRITE_DATA); |
| 684 | } else { |
| 685 | /* Kick off read request */ |
| 686 | dev_dbg(ace->dev, "read data\n"); |
| 687 | ace->fsm_task = ACE_TASK_READ; |
| 688 | ace_out(ace, ACE_SECCNTCMD, |
| 689 | count | ACE_SECCNTCMD_READ_DATA); |
| 690 | } |
| 691 | |
| 692 | /* As per datasheet, put config controller in reset */ |
| 693 | val = ace_in(ace, ACE_CTRL); |
| 694 | ace_out(ace, ACE_CTRL, val | ACE_CTRL_CFGRESET); |
| 695 | |
| 696 | /* Move to the transfer state. The systemace will raise |
| 697 | * an interrupt once there is something to do |
| 698 | */ |
| 699 | ace->fsm_state = ACE_FSM_STATE_REQ_TRANSFER; |
| 700 | if (ace->fsm_task == ACE_TASK_READ) |
| 701 | ace_fsm_yieldirq(ace); /* wait for data ready */ |
| 702 | break; |
| 703 | |
| 704 | case ACE_FSM_STATE_REQ_TRANSFER: |
| 705 | /* Check that the sysace is ready to receive data */ |
| 706 | status = ace_in32(ace, ACE_STATUS); |
| 707 | if (status & ACE_STATUS_CFBSY) { |
| 708 | dev_dbg(ace->dev, |
| 709 | "CFBSY set; t=%i iter=%i c=%i dc=%i irq=%i\n", |
| 710 | ace->fsm_task, ace->fsm_iter_num, |
Tejun Heo | 83096eb | 2009-05-07 22:24:39 +0900 | [diff] [blame] | 711 | blk_rq_cur_sectors(ace->req) * 16, |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 712 | ace->data_count, ace->in_irq); |
| 713 | ace_fsm_yield(ace); /* need to poll CFBSY bit */ |
| 714 | break; |
| 715 | } |
| 716 | if (!(status & ACE_STATUS_DATABUFRDY)) { |
| 717 | dev_dbg(ace->dev, |
| 718 | "DATABUF not set; t=%i iter=%i c=%i dc=%i irq=%i\n", |
| 719 | ace->fsm_task, ace->fsm_iter_num, |
Tejun Heo | 83096eb | 2009-05-07 22:24:39 +0900 | [diff] [blame] | 720 | blk_rq_cur_sectors(ace->req) * 16, |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 721 | ace->data_count, ace->in_irq); |
| 722 | ace_fsm_yieldirq(ace); |
| 723 | break; |
| 724 | } |
| 725 | |
| 726 | /* Transfer the next buffer */ |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 727 | if (ace->fsm_task == ACE_TASK_WRITE) |
| 728 | ace->reg_ops->dataout(ace); |
| 729 | else |
| 730 | ace->reg_ops->datain(ace); |
| 731 | ace->data_count--; |
| 732 | |
| 733 | /* If there are still buffers to be transfers; jump out here */ |
| 734 | if (ace->data_count != 0) { |
| 735 | ace_fsm_yieldirq(ace); |
| 736 | break; |
| 737 | } |
| 738 | |
| 739 | /* bio finished; is there another one? */ |
Jens Axboe | 804186f | 2018-10-15 09:05:59 -0600 | [diff] [blame] | 740 | if (blk_update_request(ace->req, BLK_STS_OK, |
| 741 | blk_rq_cur_bytes(ace->req))) { |
Tejun Heo | 5b93629 | 2009-05-07 22:24:38 +0900 | [diff] [blame] | 742 | /* dev_dbg(ace->dev, "next block; h=%u c=%u\n", |
| 743 | * blk_rq_sectors(ace->req), |
Tejun Heo | 83096eb | 2009-05-07 22:24:39 +0900 | [diff] [blame] | 744 | * blk_rq_cur_sectors(ace->req)); |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 745 | */ |
Jens Axboe | b4f42e2 | 2014-04-10 09:46:28 -0600 | [diff] [blame] | 746 | ace->data_ptr = bio_data(ace->req->bio); |
Tejun Heo | 83096eb | 2009-05-07 22:24:39 +0900 | [diff] [blame] | 747 | ace->data_count = blk_rq_cur_sectors(ace->req) * 16; |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 748 | ace_fsm_yieldirq(ace); |
| 749 | break; |
| 750 | } |
| 751 | |
| 752 | ace->fsm_state = ACE_FSM_STATE_REQ_COMPLETE; |
| 753 | break; |
| 754 | |
| 755 | case ACE_FSM_STATE_REQ_COMPLETE: |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 756 | ace->req = NULL; |
| 757 | |
| 758 | /* Finished request; go to idle state */ |
| 759 | ace->fsm_state = ACE_FSM_STATE_IDLE; |
| 760 | break; |
| 761 | |
| 762 | default: |
| 763 | ace->fsm_state = ACE_FSM_STATE_IDLE; |
| 764 | break; |
| 765 | } |
| 766 | } |
| 767 | |
| 768 | static void ace_fsm_tasklet(unsigned long data) |
| 769 | { |
| 770 | struct ace_device *ace = (void *)data; |
| 771 | unsigned long flags; |
| 772 | |
| 773 | spin_lock_irqsave(&ace->lock, flags); |
| 774 | |
| 775 | /* Loop over state machine until told to stop */ |
| 776 | ace->fsm_continue_flag = 1; |
| 777 | while (ace->fsm_continue_flag) |
| 778 | ace_fsm_dostate(ace); |
| 779 | |
| 780 | spin_unlock_irqrestore(&ace->lock, flags); |
| 781 | } |
| 782 | |
Kees Cook | e99e88a | 2017-10-16 14:43:17 -0700 | [diff] [blame] | 783 | static void ace_stall_timer(struct timer_list *t) |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 784 | { |
Kees Cook | e99e88a | 2017-10-16 14:43:17 -0700 | [diff] [blame] | 785 | struct ace_device *ace = from_timer(ace, t, stall_timer); |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 786 | unsigned long flags; |
| 787 | |
| 788 | dev_warn(ace->dev, |
| 789 | "kicking stalled fsm; state=%i task=%i iter=%i dc=%i\n", |
| 790 | ace->fsm_state, ace->fsm_task, ace->fsm_iter_num, |
| 791 | ace->data_count); |
| 792 | spin_lock_irqsave(&ace->lock, flags); |
| 793 | |
| 794 | /* Rearm the stall timer *before* entering FSM (which may then |
| 795 | * delete the timer) */ |
| 796 | mod_timer(&ace->stall_timer, jiffies + HZ); |
| 797 | |
| 798 | /* Loop over state machine until told to stop */ |
| 799 | ace->fsm_continue_flag = 1; |
| 800 | while (ace->fsm_continue_flag) |
| 801 | ace_fsm_dostate(ace); |
| 802 | |
| 803 | spin_unlock_irqrestore(&ace->lock, flags); |
| 804 | } |
| 805 | |
| 806 | /* --------------------------------------------------------------------- |
| 807 | * Interrupt handling routines |
| 808 | */ |
| 809 | static int ace_interrupt_checkstate(struct ace_device *ace) |
| 810 | { |
| 811 | u32 sreg = ace_in32(ace, ACE_STATUS); |
| 812 | u16 creg = ace_in(ace, ACE_CTRL); |
| 813 | |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 814 | /* Check for error occurrence */ |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 815 | if ((sreg & (ACE_STATUS_CFGERROR | ACE_STATUS_CFCERROR)) && |
| 816 | (creg & ACE_CTRL_ERRORIRQ)) { |
| 817 | dev_err(ace->dev, "transfer failure\n"); |
| 818 | ace_dump_regs(ace); |
| 819 | return -EIO; |
| 820 | } |
| 821 | |
| 822 | return 0; |
| 823 | } |
| 824 | |
| 825 | static irqreturn_t ace_interrupt(int irq, void *dev_id) |
| 826 | { |
| 827 | u16 creg; |
| 828 | struct ace_device *ace = dev_id; |
| 829 | |
| 830 | /* be safe and get the lock */ |
| 831 | spin_lock(&ace->lock); |
| 832 | ace->in_irq = 1; |
| 833 | |
| 834 | /* clear the interrupt */ |
| 835 | creg = ace_in(ace, ACE_CTRL); |
| 836 | ace_out(ace, ACE_CTRL, creg | ACE_CTRL_RESETIRQ); |
| 837 | ace_out(ace, ACE_CTRL, creg); |
| 838 | |
| 839 | /* check for IO failures */ |
| 840 | if (ace_interrupt_checkstate(ace)) |
| 841 | ace->data_result = -EIO; |
| 842 | |
| 843 | if (ace->fsm_task == 0) { |
| 844 | dev_err(ace->dev, |
| 845 | "spurious irq; stat=%.8x ctrl=%.8x cmd=%.4x\n", |
| 846 | ace_in32(ace, ACE_STATUS), ace_in32(ace, ACE_CTRL), |
| 847 | ace_in(ace, ACE_SECCNTCMD)); |
| 848 | dev_err(ace->dev, "fsm_task=%i fsm_state=%i data_count=%i\n", |
| 849 | ace->fsm_task, ace->fsm_state, ace->data_count); |
| 850 | } |
| 851 | |
| 852 | /* Loop over state machine until told to stop */ |
| 853 | ace->fsm_continue_flag = 1; |
| 854 | while (ace->fsm_continue_flag) |
| 855 | ace_fsm_dostate(ace); |
| 856 | |
| 857 | /* done with interrupt; drop the lock */ |
| 858 | ace->in_irq = 0; |
| 859 | spin_unlock(&ace->lock); |
| 860 | |
| 861 | return IRQ_HANDLED; |
| 862 | } |
| 863 | |
| 864 | /* --------------------------------------------------------------------- |
| 865 | * Block ops |
| 866 | */ |
Jens Axboe | 804186f | 2018-10-15 09:05:59 -0600 | [diff] [blame] | 867 | static blk_status_t ace_queue_rq(struct blk_mq_hw_ctx *hctx, |
| 868 | const struct blk_mq_queue_data *bd) |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 869 | { |
Jens Axboe | 804186f | 2018-10-15 09:05:59 -0600 | [diff] [blame] | 870 | struct ace_device *ace = hctx->queue->queuedata; |
| 871 | struct request *req = bd->rq; |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 872 | |
Jens Axboe | 804186f | 2018-10-15 09:05:59 -0600 | [diff] [blame] | 873 | if (blk_rq_is_passthrough(req)) { |
| 874 | blk_mq_start_request(req); |
| 875 | return BLK_STS_IOERR; |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 876 | } |
Jens Axboe | 804186f | 2018-10-15 09:05:59 -0600 | [diff] [blame] | 877 | |
| 878 | spin_lock_irq(&ace->lock); |
| 879 | list_add_tail(&req->queuelist, &ace->rq_list); |
| 880 | spin_unlock_irq(&ace->lock); |
| 881 | |
| 882 | tasklet_schedule(&ace->fsm_tasklet); |
| 883 | return BLK_STS_OK; |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 884 | } |
| 885 | |
Tejun Heo | 3a20091 | 2011-03-09 19:54:28 +0100 | [diff] [blame] | 886 | static unsigned int ace_check_events(struct gendisk *gd, unsigned int clearing) |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 887 | { |
| 888 | struct ace_device *ace = gd->private_data; |
Tejun Heo | 3a20091 | 2011-03-09 19:54:28 +0100 | [diff] [blame] | 889 | dev_dbg(ace->dev, "ace_check_events(): %i\n", ace->media_change); |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 890 | |
Tejun Heo | 3a20091 | 2011-03-09 19:54:28 +0100 | [diff] [blame] | 891 | return ace->media_change ? DISK_EVENT_MEDIA_CHANGE : 0; |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 892 | } |
| 893 | |
| 894 | static int ace_revalidate_disk(struct gendisk *gd) |
| 895 | { |
| 896 | struct ace_device *ace = gd->private_data; |
| 897 | unsigned long flags; |
| 898 | |
| 899 | dev_dbg(ace->dev, "ace_revalidate_disk()\n"); |
| 900 | |
| 901 | if (ace->media_change) { |
| 902 | dev_dbg(ace->dev, "requesting cf id and scheduling tasklet\n"); |
| 903 | |
| 904 | spin_lock_irqsave(&ace->lock, flags); |
| 905 | ace->id_req_count++; |
| 906 | spin_unlock_irqrestore(&ace->lock, flags); |
| 907 | |
| 908 | tasklet_schedule(&ace->fsm_tasklet); |
| 909 | wait_for_completion(&ace->id_completion); |
| 910 | } |
| 911 | |
| 912 | dev_dbg(ace->dev, "revalidate complete\n"); |
| 913 | return ace->id_result; |
| 914 | } |
| 915 | |
Al Viro | f3f68b3 | 2008-03-02 10:24:18 -0500 | [diff] [blame] | 916 | static int ace_open(struct block_device *bdev, fmode_t mode) |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 917 | { |
Al Viro | f3f68b3 | 2008-03-02 10:24:18 -0500 | [diff] [blame] | 918 | struct ace_device *ace = bdev->bd_disk->private_data; |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 919 | unsigned long flags; |
| 920 | |
| 921 | dev_dbg(ace->dev, "ace_open() users=%i\n", ace->users + 1); |
| 922 | |
Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 923 | mutex_lock(&xsysace_mutex); |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 924 | spin_lock_irqsave(&ace->lock, flags); |
| 925 | ace->users++; |
| 926 | spin_unlock_irqrestore(&ace->lock, flags); |
| 927 | |
Al Viro | f3f68b3 | 2008-03-02 10:24:18 -0500 | [diff] [blame] | 928 | check_disk_change(bdev); |
Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 929 | mutex_unlock(&xsysace_mutex); |
Arnd Bergmann | 6e9624b | 2010-08-07 18:25:34 +0200 | [diff] [blame] | 930 | |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 931 | return 0; |
| 932 | } |
| 933 | |
Al Viro | db2a144 | 2013-05-05 21:52:57 -0400 | [diff] [blame] | 934 | static void ace_release(struct gendisk *disk, fmode_t mode) |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 935 | { |
Al Viro | f3f68b3 | 2008-03-02 10:24:18 -0500 | [diff] [blame] | 936 | struct ace_device *ace = disk->private_data; |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 937 | unsigned long flags; |
| 938 | u16 val; |
| 939 | |
| 940 | dev_dbg(ace->dev, "ace_release() users=%i\n", ace->users - 1); |
| 941 | |
Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 942 | mutex_lock(&xsysace_mutex); |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 943 | spin_lock_irqsave(&ace->lock, flags); |
| 944 | ace->users--; |
| 945 | if (ace->users == 0) { |
| 946 | val = ace_in(ace, ACE_CTRL); |
| 947 | ace_out(ace, ACE_CTRL, val & ~ACE_CTRL_LOCKREQ); |
| 948 | } |
| 949 | spin_unlock_irqrestore(&ace->lock, flags); |
Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 950 | mutex_unlock(&xsysace_mutex); |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 951 | } |
| 952 | |
Christoph Hellwig | a6b3a93 | 2007-08-11 22:34:31 +0200 | [diff] [blame] | 953 | static int ace_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 954 | { |
Christoph Hellwig | a6b3a93 | 2007-08-11 22:34:31 +0200 | [diff] [blame] | 955 | struct ace_device *ace = bdev->bd_disk->private_data; |
Grant Likely | f0edef8 | 2009-04-08 14:13:04 +0200 | [diff] [blame] | 956 | u16 *cf_id = ace->cf_id; |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 957 | |
Christoph Hellwig | a6b3a93 | 2007-08-11 22:34:31 +0200 | [diff] [blame] | 958 | dev_dbg(ace->dev, "ace_getgeo()\n"); |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 959 | |
Bartlomiej Zolnierkiewicz | 4aaf2fe | 2009-04-01 21:42:22 +0200 | [diff] [blame] | 960 | geo->heads = cf_id[ATA_ID_HEADS]; |
| 961 | geo->sectors = cf_id[ATA_ID_SECTORS]; |
| 962 | geo->cylinders = cf_id[ATA_ID_CYLS]; |
Christoph Hellwig | a6b3a93 | 2007-08-11 22:34:31 +0200 | [diff] [blame] | 963 | |
| 964 | return 0; |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 965 | } |
| 966 | |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 967 | static const struct block_device_operations ace_fops = { |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 968 | .owner = THIS_MODULE, |
Al Viro | f3f68b3 | 2008-03-02 10:24:18 -0500 | [diff] [blame] | 969 | .open = ace_open, |
| 970 | .release = ace_release, |
Tejun Heo | 3a20091 | 2011-03-09 19:54:28 +0100 | [diff] [blame] | 971 | .check_events = ace_check_events, |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 972 | .revalidate_disk = ace_revalidate_disk, |
Christoph Hellwig | a6b3a93 | 2007-08-11 22:34:31 +0200 | [diff] [blame] | 973 | .getgeo = ace_getgeo, |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 974 | }; |
| 975 | |
Jens Axboe | 804186f | 2018-10-15 09:05:59 -0600 | [diff] [blame] | 976 | static const struct blk_mq_ops ace_mq_ops = { |
| 977 | .queue_rq = ace_queue_rq, |
| 978 | }; |
| 979 | |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 980 | /* -------------------------------------------------------------------- |
| 981 | * SystemACE device setup/teardown code |
| 982 | */ |
Greg Kroah-Hartman | 8d85fce | 2012-12-21 15:13:49 -0800 | [diff] [blame] | 983 | static int ace_setup(struct ace_device *ace) |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 984 | { |
| 985 | u16 version; |
| 986 | u16 val; |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 987 | int rc; |
| 988 | |
Grant Likely | 4a24d86 | 2007-10-01 16:33:54 +0200 | [diff] [blame] | 989 | dev_dbg(ace->dev, "ace_setup(ace=0x%p)\n", ace); |
Yuri Tikhonov | c14464b | 2008-11-14 10:21:57 -0700 | [diff] [blame] | 990 | dev_dbg(ace->dev, "physaddr=0x%llx irq=%i\n", |
| 991 | (unsigned long long)ace->physaddr, ace->irq); |
Grant Likely | 4a24d86 | 2007-10-01 16:33:54 +0200 | [diff] [blame] | 992 | |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 993 | spin_lock_init(&ace->lock); |
| 994 | init_completion(&ace->id_completion); |
Jens Axboe | 804186f | 2018-10-15 09:05:59 -0600 | [diff] [blame] | 995 | INIT_LIST_HEAD(&ace->rq_list); |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 996 | |
| 997 | /* |
| 998 | * Map the device |
| 999 | */ |
| 1000 | ace->baseaddr = ioremap(ace->physaddr, 0x80); |
| 1001 | if (!ace->baseaddr) |
| 1002 | goto err_ioremap; |
| 1003 | |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 1004 | /* |
| 1005 | * Initialize the state machine tasklet and stall timer |
| 1006 | */ |
| 1007 | tasklet_init(&ace->fsm_tasklet, ace_fsm_tasklet, (unsigned long)ace); |
Kees Cook | e99e88a | 2017-10-16 14:43:17 -0700 | [diff] [blame] | 1008 | timer_setup(&ace->stall_timer, ace_stall_timer, 0); |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 1009 | |
| 1010 | /* |
| 1011 | * Initialize the request queue |
| 1012 | */ |
Jens Axboe | 804186f | 2018-10-15 09:05:59 -0600 | [diff] [blame] | 1013 | ace->queue = blk_mq_init_sq_queue(&ace->tag_set, &ace_mq_ops, 2, |
| 1014 | BLK_MQ_F_SHOULD_MERGE); |
| 1015 | if (IS_ERR(ace->queue)) { |
| 1016 | rc = PTR_ERR(ace->queue); |
| 1017 | ace->queue = NULL; |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 1018 | goto err_blk_initq; |
Jens Axboe | 804186f | 2018-10-15 09:05:59 -0600 | [diff] [blame] | 1019 | } |
| 1020 | ace->queue->queuedata = ace; |
| 1021 | |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 1022 | blk_queue_logical_block_size(ace->queue, 512); |
Christoph Hellwig | 8fc4504 | 2017-06-19 09:26:26 +0200 | [diff] [blame] | 1023 | blk_queue_bounce_limit(ace->queue, BLK_BOUNCE_HIGH); |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 1024 | |
| 1025 | /* |
| 1026 | * Allocate and initialize GD structure |
| 1027 | */ |
| 1028 | ace->gd = alloc_disk(ACE_NUM_MINORS); |
| 1029 | if (!ace->gd) |
| 1030 | goto err_alloc_disk; |
| 1031 | |
| 1032 | ace->gd->major = ace_major; |
| 1033 | ace->gd->first_minor = ace->id * ACE_NUM_MINORS; |
| 1034 | ace->gd->fops = &ace_fops; |
| 1035 | ace->gd->queue = ace->queue; |
| 1036 | ace->gd->private_data = ace; |
| 1037 | snprintf(ace->gd->disk_name, 32, "xs%c", ace->id + 'a'); |
| 1038 | |
| 1039 | /* set bus width */ |
Grant Likely | 4a24d86 | 2007-10-01 16:33:54 +0200 | [diff] [blame] | 1040 | if (ace->bus_width == ACE_BUS_WIDTH_16) { |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 1041 | /* 0x0101 should work regardless of endianess */ |
| 1042 | ace_out_le16(ace, ACE_BUSMODE, 0x0101); |
| 1043 | |
| 1044 | /* read it back to determine endianess */ |
| 1045 | if (ace_in_le16(ace, ACE_BUSMODE) == 0x0001) |
| 1046 | ace->reg_ops = &ace_reg_le16_ops; |
| 1047 | else |
| 1048 | ace->reg_ops = &ace_reg_be16_ops; |
| 1049 | } else { |
| 1050 | ace_out_8(ace, ACE_BUSMODE, 0x00); |
| 1051 | ace->reg_ops = &ace_reg_8_ops; |
| 1052 | } |
| 1053 | |
| 1054 | /* Make sure version register is sane */ |
| 1055 | version = ace_in(ace, ACE_VERSION); |
| 1056 | if ((version == 0) || (version == 0xFFFF)) |
| 1057 | goto err_read; |
| 1058 | |
| 1059 | /* Put sysace in a sane state by clearing most control reg bits */ |
| 1060 | ace_out(ace, ACE_CTRL, ACE_CTRL_FORCECFGMODE | |
| 1061 | ACE_CTRL_DATABUFRDYIRQ | ACE_CTRL_ERRORIRQ); |
| 1062 | |
Grant Likely | 32f6fff | 2007-10-01 16:33:54 +0200 | [diff] [blame] | 1063 | /* Now we can hook up the irq handler */ |
Michal Simek | ba2d5af | 2011-12-20 11:09:14 +0100 | [diff] [blame] | 1064 | if (ace->irq) { |
Grant Likely | 32f6fff | 2007-10-01 16:33:54 +0200 | [diff] [blame] | 1065 | rc = request_irq(ace->irq, ace_interrupt, 0, "systemace", ace); |
| 1066 | if (rc) { |
| 1067 | /* Failure - fall back to polled mode */ |
| 1068 | dev_err(ace->dev, "request_irq failed\n"); |
Michal Simek | ba2d5af | 2011-12-20 11:09:14 +0100 | [diff] [blame] | 1069 | ace->irq = 0; |
Grant Likely | 32f6fff | 2007-10-01 16:33:54 +0200 | [diff] [blame] | 1070 | } |
| 1071 | } |
| 1072 | |
Grant Likely | d2bbf3d | 2007-10-04 08:52:40 +0200 | [diff] [blame] | 1073 | /* Enable interrupts */ |
| 1074 | val = ace_in(ace, ACE_CTRL); |
| 1075 | val |= ACE_CTRL_DATABUFRDYIRQ | ACE_CTRL_ERRORIRQ; |
| 1076 | ace_out(ace, ACE_CTRL, val); |
| 1077 | |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 1078 | /* Print the identification */ |
| 1079 | dev_info(ace->dev, "Xilinx SystemACE revision %i.%i.%i\n", |
| 1080 | (version >> 12) & 0xf, (version >> 8) & 0x0f, version & 0xff); |
Yuri Tikhonov | c14464b | 2008-11-14 10:21:57 -0700 | [diff] [blame] | 1081 | dev_dbg(ace->dev, "physaddr 0x%llx, mapped to 0x%p, irq=%i\n", |
| 1082 | (unsigned long long) ace->physaddr, ace->baseaddr, ace->irq); |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 1083 | |
| 1084 | ace->media_change = 1; |
| 1085 | ace_revalidate_disk(ace->gd); |
| 1086 | |
| 1087 | /* Make the sysace device 'live' */ |
| 1088 | add_disk(ace->gd); |
| 1089 | |
| 1090 | return 0; |
| 1091 | |
Grant Likely | ed155a9 | 2007-10-01 16:33:56 +0200 | [diff] [blame] | 1092 | err_read: |
Guenter Roeck | 47b1682 | 2019-02-19 08:49:56 -0800 | [diff] [blame] | 1093 | /* prevent double queue cleanup */ |
| 1094 | ace->gd->queue = NULL; |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 1095 | put_disk(ace->gd); |
Grant Likely | ed155a9 | 2007-10-01 16:33:56 +0200 | [diff] [blame] | 1096 | err_alloc_disk: |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 1097 | blk_cleanup_queue(ace->queue); |
Jens Axboe | 804186f | 2018-10-15 09:05:59 -0600 | [diff] [blame] | 1098 | blk_mq_free_tag_set(&ace->tag_set); |
Grant Likely | ed155a9 | 2007-10-01 16:33:56 +0200 | [diff] [blame] | 1099 | err_blk_initq: |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 1100 | iounmap(ace->baseaddr); |
Grant Likely | ed155a9 | 2007-10-01 16:33:56 +0200 | [diff] [blame] | 1101 | err_ioremap: |
Yuri Tikhonov | c14464b | 2008-11-14 10:21:57 -0700 | [diff] [blame] | 1102 | dev_info(ace->dev, "xsysace: error initializing device at 0x%llx\n", |
| 1103 | (unsigned long long) ace->physaddr); |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 1104 | return -ENOMEM; |
| 1105 | } |
| 1106 | |
Greg Kroah-Hartman | 8d85fce | 2012-12-21 15:13:49 -0800 | [diff] [blame] | 1107 | static void ace_teardown(struct ace_device *ace) |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 1108 | { |
| 1109 | if (ace->gd) { |
| 1110 | del_gendisk(ace->gd); |
| 1111 | put_disk(ace->gd); |
| 1112 | } |
| 1113 | |
Jens Axboe | 804186f | 2018-10-15 09:05:59 -0600 | [diff] [blame] | 1114 | if (ace->queue) { |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 1115 | blk_cleanup_queue(ace->queue); |
Jens Axboe | 804186f | 2018-10-15 09:05:59 -0600 | [diff] [blame] | 1116 | blk_mq_free_tag_set(&ace->tag_set); |
| 1117 | } |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 1118 | |
| 1119 | tasklet_kill(&ace->fsm_tasklet); |
| 1120 | |
Michal Simek | ba2d5af | 2011-12-20 11:09:14 +0100 | [diff] [blame] | 1121 | if (ace->irq) |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 1122 | free_irq(ace->irq, ace); |
| 1123 | |
| 1124 | iounmap(ace->baseaddr); |
| 1125 | } |
| 1126 | |
Greg Kroah-Hartman | 8d85fce | 2012-12-21 15:13:49 -0800 | [diff] [blame] | 1127 | static int ace_alloc(struct device *dev, int id, resource_size_t physaddr, |
| 1128 | int irq, int bus_width) |
Grant Likely | 1b45546 | 2007-10-01 16:33:53 +0200 | [diff] [blame] | 1129 | { |
| 1130 | struct ace_device *ace; |
| 1131 | int rc; |
| 1132 | dev_dbg(dev, "ace_alloc(%p)\n", dev); |
| 1133 | |
| 1134 | if (!physaddr) { |
| 1135 | rc = -ENODEV; |
| 1136 | goto err_noreg; |
| 1137 | } |
| 1138 | |
| 1139 | /* Allocate and initialize the ace device structure */ |
| 1140 | ace = kzalloc(sizeof(struct ace_device), GFP_KERNEL); |
| 1141 | if (!ace) { |
| 1142 | rc = -ENOMEM; |
| 1143 | goto err_alloc; |
| 1144 | } |
| 1145 | |
| 1146 | ace->dev = dev; |
| 1147 | ace->id = id; |
| 1148 | ace->physaddr = physaddr; |
| 1149 | ace->irq = irq; |
| 1150 | ace->bus_width = bus_width; |
| 1151 | |
| 1152 | /* Call the setup code */ |
Grant Likely | 34e1b83 | 2007-10-04 08:52:38 +0200 | [diff] [blame] | 1153 | rc = ace_setup(ace); |
| 1154 | if (rc) |
Grant Likely | 1b45546 | 2007-10-01 16:33:53 +0200 | [diff] [blame] | 1155 | goto err_setup; |
| 1156 | |
| 1157 | dev_set_drvdata(dev, ace); |
| 1158 | return 0; |
| 1159 | |
Grant Likely | ed155a9 | 2007-10-01 16:33:56 +0200 | [diff] [blame] | 1160 | err_setup: |
Grant Likely | 1b45546 | 2007-10-01 16:33:53 +0200 | [diff] [blame] | 1161 | dev_set_drvdata(dev, NULL); |
| 1162 | kfree(ace); |
Grant Likely | ed155a9 | 2007-10-01 16:33:56 +0200 | [diff] [blame] | 1163 | err_alloc: |
| 1164 | err_noreg: |
Grant Likely | 1b45546 | 2007-10-01 16:33:53 +0200 | [diff] [blame] | 1165 | dev_err(dev, "could not initialize device, err=%i\n", rc); |
| 1166 | return rc; |
| 1167 | } |
| 1168 | |
Greg Kroah-Hartman | 8d85fce | 2012-12-21 15:13:49 -0800 | [diff] [blame] | 1169 | static void ace_free(struct device *dev) |
Grant Likely | 1b45546 | 2007-10-01 16:33:53 +0200 | [diff] [blame] | 1170 | { |
| 1171 | struct ace_device *ace = dev_get_drvdata(dev); |
| 1172 | dev_dbg(dev, "ace_free(%p)\n", dev); |
| 1173 | |
| 1174 | if (ace) { |
| 1175 | ace_teardown(ace); |
| 1176 | dev_set_drvdata(dev, NULL); |
| 1177 | kfree(ace); |
| 1178 | } |
| 1179 | } |
| 1180 | |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 1181 | /* --------------------------------------------------------------------- |
| 1182 | * Platform Bus Support |
| 1183 | */ |
| 1184 | |
Greg Kroah-Hartman | 8d85fce | 2012-12-21 15:13:49 -0800 | [diff] [blame] | 1185 | static int ace_probe(struct platform_device *dev) |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 1186 | { |
Yuri Tikhonov | c14464b | 2008-11-14 10:21:57 -0700 | [diff] [blame] | 1187 | resource_size_t physaddr = 0; |
Grant Likely | 4a24d86 | 2007-10-01 16:33:54 +0200 | [diff] [blame] | 1188 | int bus_width = ACE_BUS_WIDTH_16; /* FIXME: should not be hard coded */ |
Grant Likely | 5d10302 | 2011-07-14 05:33:52 -0600 | [diff] [blame] | 1189 | u32 id = dev->id; |
Michal Simek | ba2d5af | 2011-12-20 11:09:14 +0100 | [diff] [blame] | 1190 | int irq = 0; |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 1191 | int i; |
| 1192 | |
Grant Likely | edec496 | 2007-10-01 16:33:52 +0200 | [diff] [blame] | 1193 | dev_dbg(&dev->dev, "ace_probe(%p)\n", dev); |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 1194 | |
Grant Likely | 5d10302 | 2011-07-14 05:33:52 -0600 | [diff] [blame] | 1195 | /* device id and bus width */ |
Gernot Vormayr | 585dc0c | 2013-05-24 15:55:03 -0700 | [diff] [blame] | 1196 | if (of_property_read_u32(dev->dev.of_node, "port-number", &id)) |
Grant Likely | 5d10302 | 2011-07-14 05:33:52 -0600 | [diff] [blame] | 1197 | id = 0; |
| 1198 | if (of_find_property(dev->dev.of_node, "8-bit", NULL)) |
| 1199 | bus_width = ACE_BUS_WIDTH_8; |
| 1200 | |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 1201 | for (i = 0; i < dev->num_resources; i++) { |
| 1202 | if (dev->resource[i].flags & IORESOURCE_MEM) |
Grant Likely | 1b45546 | 2007-10-01 16:33:53 +0200 | [diff] [blame] | 1203 | physaddr = dev->resource[i].start; |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 1204 | if (dev->resource[i].flags & IORESOURCE_IRQ) |
Grant Likely | 1b45546 | 2007-10-01 16:33:53 +0200 | [diff] [blame] | 1205 | irq = dev->resource[i].start; |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 1206 | } |
| 1207 | |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 1208 | /* Call the bus-independent setup code */ |
Grant Likely | 1b45546 | 2007-10-01 16:33:53 +0200 | [diff] [blame] | 1209 | return ace_alloc(&dev->dev, id, physaddr, irq, bus_width); |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 1210 | } |
| 1211 | |
| 1212 | /* |
| 1213 | * Platform bus remove() method |
| 1214 | */ |
Greg Kroah-Hartman | 8d85fce | 2012-12-21 15:13:49 -0800 | [diff] [blame] | 1215 | static int ace_remove(struct platform_device *dev) |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 1216 | { |
Grant Likely | 1b45546 | 2007-10-01 16:33:53 +0200 | [diff] [blame] | 1217 | ace_free(&dev->dev); |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 1218 | return 0; |
| 1219 | } |
| 1220 | |
Grant Likely | 95e896c | 2007-10-01 16:33:55 +0200 | [diff] [blame] | 1221 | #if defined(CONFIG_OF) |
Grant Likely | 95e896c | 2007-10-01 16:33:55 +0200 | [diff] [blame] | 1222 | /* Match table for of_platform binding */ |
Greg Kroah-Hartman | 8d85fce | 2012-12-21 15:13:49 -0800 | [diff] [blame] | 1223 | static const struct of_device_id ace_of_match[] = { |
Stephen Neuendorffer | 0e349b0 | 2008-01-09 06:35:05 +1100 | [diff] [blame] | 1224 | { .compatible = "xlnx,opb-sysace-1.00.b", }, |
| 1225 | { .compatible = "xlnx,opb-sysace-1.00.c", }, |
| 1226 | { .compatible = "xlnx,xps-sysace-1.00.a", }, |
Yuri Tikhonov | f502038 | 2009-01-09 15:49:06 -0700 | [diff] [blame] | 1227 | { .compatible = "xlnx,sysace", }, |
Grant Likely | 95e896c | 2007-10-01 16:33:55 +0200 | [diff] [blame] | 1228 | {}, |
| 1229 | }; |
| 1230 | MODULE_DEVICE_TABLE(of, ace_of_match); |
Grant Likely | 5d10302 | 2011-07-14 05:33:52 -0600 | [diff] [blame] | 1231 | #else /* CONFIG_OF */ |
| 1232 | #define ace_of_match NULL |
| 1233 | #endif /* CONFIG_OF */ |
Grant Likely | 95e896c | 2007-10-01 16:33:55 +0200 | [diff] [blame] | 1234 | |
Grant Likely | 5d10302 | 2011-07-14 05:33:52 -0600 | [diff] [blame] | 1235 | static struct platform_driver ace_platform_driver = { |
| 1236 | .probe = ace_probe, |
Greg Kroah-Hartman | 8d85fce | 2012-12-21 15:13:49 -0800 | [diff] [blame] | 1237 | .remove = ace_remove, |
Grant Likely | 95e896c | 2007-10-01 16:33:55 +0200 | [diff] [blame] | 1238 | .driver = { |
Grant Likely | 5d10302 | 2011-07-14 05:33:52 -0600 | [diff] [blame] | 1239 | .name = "xsysace", |
Grant Likely | 4018294 | 2010-04-13 16:13:02 -0700 | [diff] [blame] | 1240 | .of_match_table = ace_of_match, |
Grant Likely | 95e896c | 2007-10-01 16:33:55 +0200 | [diff] [blame] | 1241 | }, |
| 1242 | }; |
| 1243 | |
Grant Likely | 95e896c | 2007-10-01 16:33:55 +0200 | [diff] [blame] | 1244 | /* --------------------------------------------------------------------- |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 1245 | * Module init/exit routines |
| 1246 | */ |
| 1247 | static int __init ace_init(void) |
| 1248 | { |
Grant Likely | edec496 | 2007-10-01 16:33:52 +0200 | [diff] [blame] | 1249 | int rc; |
| 1250 | |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 1251 | ace_major = register_blkdev(ace_major, "xsysace"); |
| 1252 | if (ace_major <= 0) { |
Grant Likely | edec496 | 2007-10-01 16:33:52 +0200 | [diff] [blame] | 1253 | rc = -ENOMEM; |
| 1254 | goto err_blk; |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 1255 | } |
| 1256 | |
Grant Likely | 34e1b83 | 2007-10-04 08:52:38 +0200 | [diff] [blame] | 1257 | rc = platform_driver_register(&ace_platform_driver); |
| 1258 | if (rc) |
Grant Likely | edec496 | 2007-10-01 16:33:52 +0200 | [diff] [blame] | 1259 | goto err_plat; |
| 1260 | |
| 1261 | pr_info("Xilinx SystemACE device driver, major=%i\n", ace_major); |
| 1262 | return 0; |
| 1263 | |
Grant Likely | ed155a9 | 2007-10-01 16:33:56 +0200 | [diff] [blame] | 1264 | err_plat: |
Grant Likely | edec496 | 2007-10-01 16:33:52 +0200 | [diff] [blame] | 1265 | unregister_blkdev(ace_major, "xsysace"); |
Grant Likely | ed155a9 | 2007-10-01 16:33:56 +0200 | [diff] [blame] | 1266 | err_blk: |
Grant Likely | edec496 | 2007-10-01 16:33:52 +0200 | [diff] [blame] | 1267 | printk(KERN_ERR "xsysace: registration failed; err=%i\n", rc); |
| 1268 | return rc; |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 1269 | } |
Grant Likely | 5d10302 | 2011-07-14 05:33:52 -0600 | [diff] [blame] | 1270 | module_init(ace_init); |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 1271 | |
| 1272 | static void __exit ace_exit(void) |
| 1273 | { |
| 1274 | pr_debug("Unregistering Xilinx SystemACE driver\n"); |
Grant Likely | edec496 | 2007-10-01 16:33:52 +0200 | [diff] [blame] | 1275 | platform_driver_unregister(&ace_platform_driver); |
Akinobu Mita | c6d4d63 | 2007-07-17 04:03:46 -0700 | [diff] [blame] | 1276 | unregister_blkdev(ace_major, "xsysace"); |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 1277 | } |
Grant Likely | 74489a9 | 2007-07-17 04:03:39 -0700 | [diff] [blame] | 1278 | module_exit(ace_exit); |