blob: 97c99b43f88151bb3fa70b37f56aee8a04118e2e [file] [log] [blame]
Grant Likely74489a92007-07-17 04:03:39 -07001/*
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>
91#include <linux/blkdev.h>
Bartlomiej Zolnierkiewicz4aaf2fe2009-04-01 21:42:22 +020092#include <linux/ata.h>
Grant Likely74489a92007-07-17 04:03:39 -070093#include <linux/hdreg.h>
94#include <linux/platform_device.h>
Grant Likely95e896c2007-10-01 16:33:55 +020095#if defined(CONFIG_OF)
96#include <linux/of_device.h>
97#include <linux/of_platform.h>
98#endif
Grant Likely74489a92007-07-17 04:03:39 -070099
100MODULE_AUTHOR("Grant Likely <grant.likely@secretlab.ca>");
101MODULE_DESCRIPTION("Xilinx SystemACE device driver");
102MODULE_LICENSE("GPL");
103
104/* SystemACE register definitions */
105#define ACE_BUSMODE (0x00)
106
107#define ACE_STATUS (0x04)
108#define ACE_STATUS_CFGLOCK (0x00000001)
109#define ACE_STATUS_MPULOCK (0x00000002)
110#define ACE_STATUS_CFGERROR (0x00000004) /* config controller error */
111#define ACE_STATUS_CFCERROR (0x00000008) /* CF controller error */
112#define ACE_STATUS_CFDETECT (0x00000010)
113#define ACE_STATUS_DATABUFRDY (0x00000020)
114#define ACE_STATUS_DATABUFMODE (0x00000040)
115#define ACE_STATUS_CFGDONE (0x00000080)
116#define ACE_STATUS_RDYFORCFCMD (0x00000100)
117#define ACE_STATUS_CFGMODEPIN (0x00000200)
118#define ACE_STATUS_CFGADDR_MASK (0x0000e000)
119#define ACE_STATUS_CFBSY (0x00020000)
120#define ACE_STATUS_CFRDY (0x00040000)
121#define ACE_STATUS_CFDWF (0x00080000)
122#define ACE_STATUS_CFDSC (0x00100000)
123#define ACE_STATUS_CFDRQ (0x00200000)
124#define ACE_STATUS_CFCORR (0x00400000)
125#define ACE_STATUS_CFERR (0x00800000)
126
127#define ACE_ERROR (0x08)
128#define ACE_CFGLBA (0x0c)
129#define ACE_MPULBA (0x10)
130
131#define ACE_SECCNTCMD (0x14)
132#define ACE_SECCNTCMD_RESET (0x0100)
133#define ACE_SECCNTCMD_IDENTIFY (0x0200)
134#define ACE_SECCNTCMD_READ_DATA (0x0300)
135#define ACE_SECCNTCMD_WRITE_DATA (0x0400)
136#define ACE_SECCNTCMD_ABORT (0x0600)
137
138#define ACE_VERSION (0x16)
139#define ACE_VERSION_REVISION_MASK (0x00FF)
140#define ACE_VERSION_MINOR_MASK (0x0F00)
141#define ACE_VERSION_MAJOR_MASK (0xF000)
142
143#define ACE_CTRL (0x18)
144#define ACE_CTRL_FORCELOCKREQ (0x0001)
145#define ACE_CTRL_LOCKREQ (0x0002)
146#define ACE_CTRL_FORCECFGADDR (0x0004)
147#define ACE_CTRL_FORCECFGMODE (0x0008)
148#define ACE_CTRL_CFGMODE (0x0010)
149#define ACE_CTRL_CFGSTART (0x0020)
150#define ACE_CTRL_CFGSEL (0x0040)
151#define ACE_CTRL_CFGRESET (0x0080)
152#define ACE_CTRL_DATABUFRDYIRQ (0x0100)
153#define ACE_CTRL_ERRORIRQ (0x0200)
154#define ACE_CTRL_CFGDONEIRQ (0x0400)
155#define ACE_CTRL_RESETIRQ (0x0800)
156#define ACE_CTRL_CFGPROG (0x1000)
157#define ACE_CTRL_CFGADDR_MASK (0xe000)
158
159#define ACE_FATSTAT (0x1c)
160
161#define ACE_NUM_MINORS 16
162#define ACE_SECTOR_SIZE (512)
163#define ACE_FIFO_SIZE (32)
164#define ACE_BUF_PER_SECTOR (ACE_SECTOR_SIZE / ACE_FIFO_SIZE)
165
Grant Likely4a24d862007-10-01 16:33:54 +0200166#define ACE_BUS_WIDTH_8 0
167#define ACE_BUS_WIDTH_16 1
168
Grant Likely74489a92007-07-17 04:03:39 -0700169struct ace_reg_ops;
170
171struct ace_device {
172 /* driver state data */
173 int id;
174 int media_change;
175 int users;
176 struct list_head list;
177
178 /* finite state machine data */
179 struct tasklet_struct fsm_tasklet;
180 uint fsm_task; /* Current activity (ACE_TASK_*) */
181 uint fsm_state; /* Current state (ACE_FSM_STATE_*) */
182 uint fsm_continue_flag; /* cleared to exit FSM mainloop */
183 uint fsm_iter_num;
184 struct timer_list stall_timer;
185
186 /* Transfer state/result, use for both id and block request */
187 struct request *req; /* request being processed */
188 void *data_ptr; /* pointer to I/O buffer */
189 int data_count; /* number of buffers remaining */
190 int data_result; /* Result of transfer; 0 := success */
191
192 int id_req_count; /* count of id requests */
193 int id_result;
194 struct completion id_completion; /* used when id req finishes */
195 int in_irq;
196
197 /* Details of hardware device */
Yuri Tikhonovc14464b2008-11-14 10:21:57 -0700198 resource_size_t physaddr;
Grant Likelyb5515d82007-10-04 08:52:39 +0200199 void __iomem *baseaddr;
Grant Likely74489a92007-07-17 04:03:39 -0700200 int irq;
201 int bus_width; /* 0 := 8 bit; 1 := 16 bit */
202 struct ace_reg_ops *reg_ops;
203 int lock_count;
204
205 /* Block device data structures */
206 spinlock_t lock;
207 struct device *dev;
208 struct request_queue *queue;
209 struct gendisk *gd;
210
211 /* Inserted CF card parameters */
Bartlomiej Zolnierkiewicz4aaf2fe2009-04-01 21:42:22 +0200212 u16 cf_id[ATA_ID_WORDS];
Grant Likely74489a92007-07-17 04:03:39 -0700213};
214
215static int ace_major;
216
217/* ---------------------------------------------------------------------
218 * Low level register access
219 */
220
221struct ace_reg_ops {
222 u16(*in) (struct ace_device * ace, int reg);
223 void (*out) (struct ace_device * ace, int reg, u16 val);
224 void (*datain) (struct ace_device * ace);
225 void (*dataout) (struct ace_device * ace);
226};
227
228/* 8 Bit bus width */
229static u16 ace_in_8(struct ace_device *ace, int reg)
230{
Grant Likelyb5515d82007-10-04 08:52:39 +0200231 void __iomem *r = ace->baseaddr + reg;
Grant Likely74489a92007-07-17 04:03:39 -0700232 return in_8(r) | (in_8(r + 1) << 8);
233}
234
235static void ace_out_8(struct ace_device *ace, int reg, u16 val)
236{
Grant Likelyb5515d82007-10-04 08:52:39 +0200237 void __iomem *r = ace->baseaddr + reg;
Grant Likely74489a92007-07-17 04:03:39 -0700238 out_8(r, val);
239 out_8(r + 1, val >> 8);
240}
241
242static void ace_datain_8(struct ace_device *ace)
243{
Grant Likelyb5515d82007-10-04 08:52:39 +0200244 void __iomem *r = ace->baseaddr + 0x40;
Grant Likely74489a92007-07-17 04:03:39 -0700245 u8 *dst = ace->data_ptr;
246 int i = ACE_FIFO_SIZE;
247 while (i--)
248 *dst++ = in_8(r++);
249 ace->data_ptr = dst;
250}
251
252static void ace_dataout_8(struct ace_device *ace)
253{
Grant Likelyb5515d82007-10-04 08:52:39 +0200254 void __iomem *r = ace->baseaddr + 0x40;
Grant Likely74489a92007-07-17 04:03:39 -0700255 u8 *src = ace->data_ptr;
256 int i = ACE_FIFO_SIZE;
257 while (i--)
258 out_8(r++, *src++);
259 ace->data_ptr = src;
260}
261
262static struct ace_reg_ops ace_reg_8_ops = {
263 .in = ace_in_8,
264 .out = ace_out_8,
265 .datain = ace_datain_8,
266 .dataout = ace_dataout_8,
267};
268
269/* 16 bit big endian bus attachment */
270static u16 ace_in_be16(struct ace_device *ace, int reg)
271{
272 return in_be16(ace->baseaddr + reg);
273}
274
275static void ace_out_be16(struct ace_device *ace, int reg, u16 val)
276{
277 out_be16(ace->baseaddr + reg, val);
278}
279
280static void ace_datain_be16(struct ace_device *ace)
281{
282 int i = ACE_FIFO_SIZE / 2;
283 u16 *dst = ace->data_ptr;
284 while (i--)
285 *dst++ = in_le16(ace->baseaddr + 0x40);
286 ace->data_ptr = dst;
287}
288
289static void ace_dataout_be16(struct ace_device *ace)
290{
291 int i = ACE_FIFO_SIZE / 2;
292 u16 *src = ace->data_ptr;
293 while (i--)
294 out_le16(ace->baseaddr + 0x40, *src++);
295 ace->data_ptr = src;
296}
297
298/* 16 bit little endian bus attachment */
299static u16 ace_in_le16(struct ace_device *ace, int reg)
300{
301 return in_le16(ace->baseaddr + reg);
302}
303
304static void ace_out_le16(struct ace_device *ace, int reg, u16 val)
305{
306 out_le16(ace->baseaddr + reg, val);
307}
308
309static void ace_datain_le16(struct ace_device *ace)
310{
311 int i = ACE_FIFO_SIZE / 2;
312 u16 *dst = ace->data_ptr;
313 while (i--)
314 *dst++ = in_be16(ace->baseaddr + 0x40);
315 ace->data_ptr = dst;
316}
317
318static void ace_dataout_le16(struct ace_device *ace)
319{
320 int i = ACE_FIFO_SIZE / 2;
321 u16 *src = ace->data_ptr;
322 while (i--)
323 out_be16(ace->baseaddr + 0x40, *src++);
324 ace->data_ptr = src;
325}
326
327static struct ace_reg_ops ace_reg_be16_ops = {
328 .in = ace_in_be16,
329 .out = ace_out_be16,
330 .datain = ace_datain_be16,
331 .dataout = ace_dataout_be16,
332};
333
334static struct ace_reg_ops ace_reg_le16_ops = {
335 .in = ace_in_le16,
336 .out = ace_out_le16,
337 .datain = ace_datain_le16,
338 .dataout = ace_dataout_le16,
339};
340
341static inline u16 ace_in(struct ace_device *ace, int reg)
342{
343 return ace->reg_ops->in(ace, reg);
344}
345
346static inline u32 ace_in32(struct ace_device *ace, int reg)
347{
348 return ace_in(ace, reg) | (ace_in(ace, reg + 2) << 16);
349}
350
351static inline void ace_out(struct ace_device *ace, int reg, u16 val)
352{
353 ace->reg_ops->out(ace, reg, val);
354}
355
356static inline void ace_out32(struct ace_device *ace, int reg, u32 val)
357{
358 ace_out(ace, reg, val);
359 ace_out(ace, reg + 2, val >> 16);
360}
361
362/* ---------------------------------------------------------------------
363 * Debug support functions
364 */
365
366#if defined(DEBUG)
367static void ace_dump_mem(void *base, int len)
368{
369 const char *ptr = base;
370 int i, j;
371
372 for (i = 0; i < len; i += 16) {
373 printk(KERN_INFO "%.8x:", i);
374 for (j = 0; j < 16; j++) {
375 if (!(j % 4))
376 printk(" ");
377 printk("%.2x", ptr[i + j]);
378 }
379 printk(" ");
380 for (j = 0; j < 16; j++)
381 printk("%c", isprint(ptr[i + j]) ? ptr[i + j] : '.');
382 printk("\n");
383 }
384}
385#else
386static inline void ace_dump_mem(void *base, int len)
387{
388}
389#endif
390
391static void ace_dump_regs(struct ace_device *ace)
392{
393 dev_info(ace->dev, " ctrl: %.8x seccnt/cmd: %.4x ver:%.4x\n"
Joe Perches898eb712007-10-18 03:06:30 -0700394 KERN_INFO " status:%.8x mpu_lba:%.8x busmode:%4x\n"
395 KERN_INFO " error: %.8x cfg_lba:%.8x fatstat:%.4x\n",
Grant Likely74489a92007-07-17 04:03:39 -0700396 ace_in32(ace, ACE_CTRL),
397 ace_in(ace, ACE_SECCNTCMD),
398 ace_in(ace, ACE_VERSION),
399 ace_in32(ace, ACE_STATUS),
400 ace_in32(ace, ACE_MPULBA),
401 ace_in(ace, ACE_BUSMODE),
402 ace_in32(ace, ACE_ERROR),
403 ace_in32(ace, ACE_CFGLBA), ace_in(ace, ACE_FATSTAT));
404}
405
Bartlomiej Zolnierkiewicz4aaf2fe2009-04-01 21:42:22 +0200406void ace_fix_driveid(u16 *id)
Grant Likely74489a92007-07-17 04:03:39 -0700407{
408#if defined(__BIG_ENDIAN)
Grant Likely74489a92007-07-17 04:03:39 -0700409 int i;
410
411 /* All half words have wrong byte order; swap the bytes */
Bartlomiej Zolnierkiewicz4aaf2fe2009-04-01 21:42:22 +0200412 for (i = 0; i < ATA_ID_WORDS; i++, id++)
413 *id = le16_to_cpu(*id);
Grant Likely74489a92007-07-17 04:03:39 -0700414#endif
415}
416
417/* ---------------------------------------------------------------------
418 * Finite State Machine (FSM) implementation
419 */
420
421/* FSM tasks; used to direct state transitions */
422#define ACE_TASK_IDLE 0
423#define ACE_TASK_IDENTIFY 1
424#define ACE_TASK_READ 2
425#define ACE_TASK_WRITE 3
426#define ACE_FSM_NUM_TASKS 4
427
428/* FSM state definitions */
429#define ACE_FSM_STATE_IDLE 0
430#define ACE_FSM_STATE_REQ_LOCK 1
431#define ACE_FSM_STATE_WAIT_LOCK 2
432#define ACE_FSM_STATE_WAIT_CFREADY 3
433#define ACE_FSM_STATE_IDENTIFY_PREPARE 4
434#define ACE_FSM_STATE_IDENTIFY_TRANSFER 5
435#define ACE_FSM_STATE_IDENTIFY_COMPLETE 6
436#define ACE_FSM_STATE_REQ_PREPARE 7
437#define ACE_FSM_STATE_REQ_TRANSFER 8
438#define ACE_FSM_STATE_REQ_COMPLETE 9
439#define ACE_FSM_STATE_ERROR 10
440#define ACE_FSM_NUM_STATES 11
441
442/* Set flag to exit FSM loop and reschedule tasklet */
443static inline void ace_fsm_yield(struct ace_device *ace)
444{
445 dev_dbg(ace->dev, "ace_fsm_yield()\n");
446 tasklet_schedule(&ace->fsm_tasklet);
447 ace->fsm_continue_flag = 0;
448}
449
450/* Set flag to exit FSM loop and wait for IRQ to reschedule tasklet */
451static inline void ace_fsm_yieldirq(struct ace_device *ace)
452{
453 dev_dbg(ace->dev, "ace_fsm_yieldirq()\n");
454
455 if (ace->irq == NO_IRQ)
456 /* No IRQ assigned, so need to poll */
457 tasklet_schedule(&ace->fsm_tasklet);
458 ace->fsm_continue_flag = 0;
459}
460
461/* Get the next read/write request; ending requests that we don't handle */
Jens Axboe165125e2007-07-24 09:28:11 +0200462struct request *ace_get_next_request(struct request_queue * q)
Grant Likely74489a92007-07-17 04:03:39 -0700463{
464 struct request *req;
465
466 while ((req = elv_next_request(q)) != NULL) {
467 if (blk_fs_request(req))
468 break;
Tejun Heof06d9a22009-04-23 11:05:19 +0900469 __blk_end_request_cur(req, -EIO);
Grant Likely74489a92007-07-17 04:03:39 -0700470 }
471 return req;
472}
473
474static void ace_fsm_dostate(struct ace_device *ace)
475{
476 struct request *req;
477 u32 status;
478 u16 val;
479 int count;
Grant Likely74489a92007-07-17 04:03:39 -0700480
481#if defined(DEBUG)
482 dev_dbg(ace->dev, "fsm_state=%i, id_req_count=%i\n",
483 ace->fsm_state, ace->id_req_count);
484#endif
485
Grant Likelybfbd4422009-03-09 13:42:24 +0100486 /* Verify that there is actually a CF in the slot. If not, then
487 * bail out back to the idle state and wake up all the waiters */
488 status = ace_in32(ace, ACE_STATUS);
489 if ((status & ACE_STATUS_CFDETECT) == 0) {
490 ace->fsm_state = ACE_FSM_STATE_IDLE;
491 ace->media_change = 1;
492 set_capacity(ace->gd, 0);
493 dev_info(ace->dev, "No CF in slot\n");
494
495 /* Drop all pending requests */
496 while ((req = elv_next_request(ace->queue)) != NULL)
Tejun Heof06d9a22009-04-23 11:05:19 +0900497 __blk_end_request_cur(req, -EIO);
Grant Likelybfbd4422009-03-09 13:42:24 +0100498
499 /* Drop back to IDLE state and notify waiters */
500 ace->fsm_state = ACE_FSM_STATE_IDLE;
501 ace->id_result = -EIO;
502 while (ace->id_req_count) {
503 complete(&ace->id_completion);
504 ace->id_req_count--;
505 }
506 }
507
Grant Likely74489a92007-07-17 04:03:39 -0700508 switch (ace->fsm_state) {
509 case ACE_FSM_STATE_IDLE:
510 /* See if there is anything to do */
511 if (ace->id_req_count || ace_get_next_request(ace->queue)) {
512 ace->fsm_iter_num++;
513 ace->fsm_state = ACE_FSM_STATE_REQ_LOCK;
514 mod_timer(&ace->stall_timer, jiffies + HZ);
515 if (!timer_pending(&ace->stall_timer))
516 add_timer(&ace->stall_timer);
517 break;
518 }
519 del_timer(&ace->stall_timer);
520 ace->fsm_continue_flag = 0;
521 break;
522
523 case ACE_FSM_STATE_REQ_LOCK:
524 if (ace_in(ace, ACE_STATUS) & ACE_STATUS_MPULOCK) {
525 /* Already have the lock, jump to next state */
526 ace->fsm_state = ACE_FSM_STATE_WAIT_CFREADY;
527 break;
528 }
529
530 /* Request the lock */
531 val = ace_in(ace, ACE_CTRL);
532 ace_out(ace, ACE_CTRL, val | ACE_CTRL_LOCKREQ);
533 ace->fsm_state = ACE_FSM_STATE_WAIT_LOCK;
534 break;
535
536 case ACE_FSM_STATE_WAIT_LOCK:
537 if (ace_in(ace, ACE_STATUS) & ACE_STATUS_MPULOCK) {
538 /* got the lock; move to next state */
539 ace->fsm_state = ACE_FSM_STATE_WAIT_CFREADY;
540 break;
541 }
542
543 /* wait a bit for the lock */
544 ace_fsm_yield(ace);
545 break;
546
547 case ACE_FSM_STATE_WAIT_CFREADY:
548 status = ace_in32(ace, ACE_STATUS);
549 if (!(status & ACE_STATUS_RDYFORCFCMD) ||
550 (status & ACE_STATUS_CFBSY)) {
551 /* CF card isn't ready; it needs to be polled */
552 ace_fsm_yield(ace);
553 break;
554 }
555
556 /* Device is ready for command; determine what to do next */
557 if (ace->id_req_count)
558 ace->fsm_state = ACE_FSM_STATE_IDENTIFY_PREPARE;
559 else
560 ace->fsm_state = ACE_FSM_STATE_REQ_PREPARE;
561 break;
562
563 case ACE_FSM_STATE_IDENTIFY_PREPARE:
564 /* Send identify command */
565 ace->fsm_task = ACE_TASK_IDENTIFY;
Grant Likelyf0edef82009-04-08 14:13:04 +0200566 ace->data_ptr = ace->cf_id;
Grant Likely74489a92007-07-17 04:03:39 -0700567 ace->data_count = ACE_BUF_PER_SECTOR;
568 ace_out(ace, ACE_SECCNTCMD, ACE_SECCNTCMD_IDENTIFY);
569
570 /* As per datasheet, put config controller in reset */
571 val = ace_in(ace, ACE_CTRL);
572 ace_out(ace, ACE_CTRL, val | ACE_CTRL_CFGRESET);
573
574 /* irq handler takes over from this point; wait for the
575 * transfer to complete */
576 ace->fsm_state = ACE_FSM_STATE_IDENTIFY_TRANSFER;
577 ace_fsm_yieldirq(ace);
578 break;
579
580 case ACE_FSM_STATE_IDENTIFY_TRANSFER:
581 /* Check that the sysace is ready to receive data */
582 status = ace_in32(ace, ACE_STATUS);
583 if (status & ACE_STATUS_CFBSY) {
584 dev_dbg(ace->dev, "CFBSY set; t=%i iter=%i dc=%i\n",
585 ace->fsm_task, ace->fsm_iter_num,
586 ace->data_count);
587 ace_fsm_yield(ace);
588 break;
589 }
590 if (!(status & ACE_STATUS_DATABUFRDY)) {
591 ace_fsm_yield(ace);
592 break;
593 }
594
595 /* Transfer the next buffer */
596 ace->reg_ops->datain(ace);
597 ace->data_count--;
598
599 /* If there are still buffers to be transfers; jump out here */
600 if (ace->data_count != 0) {
601 ace_fsm_yieldirq(ace);
602 break;
603 }
604
605 /* transfer finished; kick state machine */
606 dev_dbg(ace->dev, "identify finished\n");
607 ace->fsm_state = ACE_FSM_STATE_IDENTIFY_COMPLETE;
608 break;
609
610 case ACE_FSM_STATE_IDENTIFY_COMPLETE:
Grant Likelyf0edef82009-04-08 14:13:04 +0200611 ace_fix_driveid(ace->cf_id);
612 ace_dump_mem(ace->cf_id, 512); /* Debug: Dump out disk ID */
Grant Likely74489a92007-07-17 04:03:39 -0700613
614 if (ace->data_result) {
615 /* Error occured, disable the disk */
616 ace->media_change = 1;
617 set_capacity(ace->gd, 0);
618 dev_err(ace->dev, "error fetching CF id (%i)\n",
619 ace->data_result);
620 } else {
621 ace->media_change = 0;
622
623 /* Record disk parameters */
Bartlomiej Zolnierkiewicz4aaf2fe2009-04-01 21:42:22 +0200624 set_capacity(ace->gd,
Grant Likelyf0edef82009-04-08 14:13:04 +0200625 ata_id_u32(ace->cf_id, ATA_ID_LBA_CAPACITY));
Grant Likely74489a92007-07-17 04:03:39 -0700626 dev_info(ace->dev, "capacity: %i sectors\n",
Grant Likelyf0edef82009-04-08 14:13:04 +0200627 ata_id_u32(ace->cf_id, ATA_ID_LBA_CAPACITY));
Grant Likely74489a92007-07-17 04:03:39 -0700628 }
629
630 /* We're done, drop to IDLE state and notify waiters */
631 ace->fsm_state = ACE_FSM_STATE_IDLE;
632 ace->id_result = ace->data_result;
633 while (ace->id_req_count) {
634 complete(&ace->id_completion);
635 ace->id_req_count--;
636 }
637 break;
638
639 case ACE_FSM_STATE_REQ_PREPARE:
640 req = ace_get_next_request(ace->queue);
641 if (!req) {
642 ace->fsm_state = ACE_FSM_STATE_IDLE;
643 break;
644 }
645
646 /* Okay, it's a data request, set it up for transfer */
647 dev_dbg(ace->dev,
Tejun Heo5b936292009-05-07 22:24:38 +0900648 "request: sec=%llx hcnt=%x, ccnt=%x, dir=%i\n",
Tejun Heo83096eb2009-05-07 22:24:39 +0900649 (unsigned long long)blk_rq_pos(req),
650 blk_rq_sectors(req), blk_rq_cur_sectors(req),
651 rq_data_dir(req));
Grant Likely74489a92007-07-17 04:03:39 -0700652
653 ace->req = req;
654 ace->data_ptr = req->buffer;
Tejun Heo83096eb2009-05-07 22:24:39 +0900655 ace->data_count = blk_rq_cur_sectors(req) * ACE_BUF_PER_SECTOR;
656 ace_out32(ace, ACE_MPULBA, blk_rq_pos(req) & 0x0FFFFFFF);
Grant Likely74489a92007-07-17 04:03:39 -0700657
Tejun Heo5b936292009-05-07 22:24:38 +0900658 count = blk_rq_sectors(req);
Grant Likely74489a92007-07-17 04:03:39 -0700659 if (rq_data_dir(req)) {
660 /* Kick off write request */
661 dev_dbg(ace->dev, "write data\n");
662 ace->fsm_task = ACE_TASK_WRITE;
663 ace_out(ace, ACE_SECCNTCMD,
664 count | ACE_SECCNTCMD_WRITE_DATA);
665 } else {
666 /* Kick off read request */
667 dev_dbg(ace->dev, "read data\n");
668 ace->fsm_task = ACE_TASK_READ;
669 ace_out(ace, ACE_SECCNTCMD,
670 count | ACE_SECCNTCMD_READ_DATA);
671 }
672
673 /* As per datasheet, put config controller in reset */
674 val = ace_in(ace, ACE_CTRL);
675 ace_out(ace, ACE_CTRL, val | ACE_CTRL_CFGRESET);
676
677 /* Move to the transfer state. The systemace will raise
678 * an interrupt once there is something to do
679 */
680 ace->fsm_state = ACE_FSM_STATE_REQ_TRANSFER;
681 if (ace->fsm_task == ACE_TASK_READ)
682 ace_fsm_yieldirq(ace); /* wait for data ready */
683 break;
684
685 case ACE_FSM_STATE_REQ_TRANSFER:
686 /* Check that the sysace is ready to receive data */
687 status = ace_in32(ace, ACE_STATUS);
688 if (status & ACE_STATUS_CFBSY) {
689 dev_dbg(ace->dev,
690 "CFBSY set; t=%i iter=%i c=%i dc=%i irq=%i\n",
691 ace->fsm_task, ace->fsm_iter_num,
Tejun Heo83096eb2009-05-07 22:24:39 +0900692 blk_rq_cur_sectors(ace->req) * 16,
Grant Likely74489a92007-07-17 04:03:39 -0700693 ace->data_count, ace->in_irq);
694 ace_fsm_yield(ace); /* need to poll CFBSY bit */
695 break;
696 }
697 if (!(status & ACE_STATUS_DATABUFRDY)) {
698 dev_dbg(ace->dev,
699 "DATABUF not set; t=%i iter=%i c=%i dc=%i irq=%i\n",
700 ace->fsm_task, ace->fsm_iter_num,
Tejun Heo83096eb2009-05-07 22:24:39 +0900701 blk_rq_cur_sectors(ace->req) * 16,
Grant Likely74489a92007-07-17 04:03:39 -0700702 ace->data_count, ace->in_irq);
703 ace_fsm_yieldirq(ace);
704 break;
705 }
706
707 /* Transfer the next buffer */
Grant Likely74489a92007-07-17 04:03:39 -0700708 if (ace->fsm_task == ACE_TASK_WRITE)
709 ace->reg_ops->dataout(ace);
710 else
711 ace->reg_ops->datain(ace);
712 ace->data_count--;
713
714 /* If there are still buffers to be transfers; jump out here */
715 if (ace->data_count != 0) {
716 ace_fsm_yieldirq(ace);
717 break;
718 }
719
720 /* bio finished; is there another one? */
Jens Axboe9bf72252008-01-29 21:54:53 +0100721 if (__blk_end_request(ace->req, 0,
722 blk_rq_cur_bytes(ace->req))) {
Tejun Heo5b936292009-05-07 22:24:38 +0900723 /* dev_dbg(ace->dev, "next block; h=%u c=%u\n",
724 * blk_rq_sectors(ace->req),
Tejun Heo83096eb2009-05-07 22:24:39 +0900725 * blk_rq_cur_sectors(ace->req));
Grant Likely74489a92007-07-17 04:03:39 -0700726 */
727 ace->data_ptr = ace->req->buffer;
Tejun Heo83096eb2009-05-07 22:24:39 +0900728 ace->data_count = blk_rq_cur_sectors(ace->req) * 16;
Grant Likely74489a92007-07-17 04:03:39 -0700729 ace_fsm_yieldirq(ace);
730 break;
731 }
732
733 ace->fsm_state = ACE_FSM_STATE_REQ_COMPLETE;
734 break;
735
736 case ACE_FSM_STATE_REQ_COMPLETE:
Grant Likely74489a92007-07-17 04:03:39 -0700737 ace->req = NULL;
738
739 /* Finished request; go to idle state */
740 ace->fsm_state = ACE_FSM_STATE_IDLE;
741 break;
742
743 default:
744 ace->fsm_state = ACE_FSM_STATE_IDLE;
745 break;
746 }
747}
748
749static void ace_fsm_tasklet(unsigned long data)
750{
751 struct ace_device *ace = (void *)data;
752 unsigned long flags;
753
754 spin_lock_irqsave(&ace->lock, flags);
755
756 /* Loop over state machine until told to stop */
757 ace->fsm_continue_flag = 1;
758 while (ace->fsm_continue_flag)
759 ace_fsm_dostate(ace);
760
761 spin_unlock_irqrestore(&ace->lock, flags);
762}
763
764static void ace_stall_timer(unsigned long data)
765{
766 struct ace_device *ace = (void *)data;
767 unsigned long flags;
768
769 dev_warn(ace->dev,
770 "kicking stalled fsm; state=%i task=%i iter=%i dc=%i\n",
771 ace->fsm_state, ace->fsm_task, ace->fsm_iter_num,
772 ace->data_count);
773 spin_lock_irqsave(&ace->lock, flags);
774
775 /* Rearm the stall timer *before* entering FSM (which may then
776 * delete the timer) */
777 mod_timer(&ace->stall_timer, jiffies + HZ);
778
779 /* Loop over state machine until told to stop */
780 ace->fsm_continue_flag = 1;
781 while (ace->fsm_continue_flag)
782 ace_fsm_dostate(ace);
783
784 spin_unlock_irqrestore(&ace->lock, flags);
785}
786
787/* ---------------------------------------------------------------------
788 * Interrupt handling routines
789 */
790static int ace_interrupt_checkstate(struct ace_device *ace)
791{
792 u32 sreg = ace_in32(ace, ACE_STATUS);
793 u16 creg = ace_in(ace, ACE_CTRL);
794
795 /* Check for error occurance */
796 if ((sreg & (ACE_STATUS_CFGERROR | ACE_STATUS_CFCERROR)) &&
797 (creg & ACE_CTRL_ERRORIRQ)) {
798 dev_err(ace->dev, "transfer failure\n");
799 ace_dump_regs(ace);
800 return -EIO;
801 }
802
803 return 0;
804}
805
806static irqreturn_t ace_interrupt(int irq, void *dev_id)
807{
808 u16 creg;
809 struct ace_device *ace = dev_id;
810
811 /* be safe and get the lock */
812 spin_lock(&ace->lock);
813 ace->in_irq = 1;
814
815 /* clear the interrupt */
816 creg = ace_in(ace, ACE_CTRL);
817 ace_out(ace, ACE_CTRL, creg | ACE_CTRL_RESETIRQ);
818 ace_out(ace, ACE_CTRL, creg);
819
820 /* check for IO failures */
821 if (ace_interrupt_checkstate(ace))
822 ace->data_result = -EIO;
823
824 if (ace->fsm_task == 0) {
825 dev_err(ace->dev,
826 "spurious irq; stat=%.8x ctrl=%.8x cmd=%.4x\n",
827 ace_in32(ace, ACE_STATUS), ace_in32(ace, ACE_CTRL),
828 ace_in(ace, ACE_SECCNTCMD));
829 dev_err(ace->dev, "fsm_task=%i fsm_state=%i data_count=%i\n",
830 ace->fsm_task, ace->fsm_state, ace->data_count);
831 }
832
833 /* Loop over state machine until told to stop */
834 ace->fsm_continue_flag = 1;
835 while (ace->fsm_continue_flag)
836 ace_fsm_dostate(ace);
837
838 /* done with interrupt; drop the lock */
839 ace->in_irq = 0;
840 spin_unlock(&ace->lock);
841
842 return IRQ_HANDLED;
843}
844
845/* ---------------------------------------------------------------------
846 * Block ops
847 */
Jens Axboe165125e2007-07-24 09:28:11 +0200848static void ace_request(struct request_queue * q)
Grant Likely74489a92007-07-17 04:03:39 -0700849{
850 struct request *req;
851 struct ace_device *ace;
852
853 req = ace_get_next_request(q);
854
855 if (req) {
856 ace = req->rq_disk->private_data;
857 tasklet_schedule(&ace->fsm_tasklet);
858 }
859}
860
861static int ace_media_changed(struct gendisk *gd)
862{
863 struct ace_device *ace = gd->private_data;
864 dev_dbg(ace->dev, "ace_media_changed(): %i\n", ace->media_change);
865
866 return ace->media_change;
867}
868
869static int ace_revalidate_disk(struct gendisk *gd)
870{
871 struct ace_device *ace = gd->private_data;
872 unsigned long flags;
873
874 dev_dbg(ace->dev, "ace_revalidate_disk()\n");
875
876 if (ace->media_change) {
877 dev_dbg(ace->dev, "requesting cf id and scheduling tasklet\n");
878
879 spin_lock_irqsave(&ace->lock, flags);
880 ace->id_req_count++;
881 spin_unlock_irqrestore(&ace->lock, flags);
882
883 tasklet_schedule(&ace->fsm_tasklet);
884 wait_for_completion(&ace->id_completion);
885 }
886
887 dev_dbg(ace->dev, "revalidate complete\n");
888 return ace->id_result;
889}
890
Al Virof3f68b32008-03-02 10:24:18 -0500891static int ace_open(struct block_device *bdev, fmode_t mode)
Grant Likely74489a92007-07-17 04:03:39 -0700892{
Al Virof3f68b32008-03-02 10:24:18 -0500893 struct ace_device *ace = bdev->bd_disk->private_data;
Grant Likely74489a92007-07-17 04:03:39 -0700894 unsigned long flags;
895
896 dev_dbg(ace->dev, "ace_open() users=%i\n", ace->users + 1);
897
Grant Likely74489a92007-07-17 04:03:39 -0700898 spin_lock_irqsave(&ace->lock, flags);
899 ace->users++;
900 spin_unlock_irqrestore(&ace->lock, flags);
901
Al Virof3f68b32008-03-02 10:24:18 -0500902 check_disk_change(bdev);
Grant Likely74489a92007-07-17 04:03:39 -0700903 return 0;
904}
905
Al Virof3f68b32008-03-02 10:24:18 -0500906static int ace_release(struct gendisk *disk, fmode_t mode)
Grant Likely74489a92007-07-17 04:03:39 -0700907{
Al Virof3f68b32008-03-02 10:24:18 -0500908 struct ace_device *ace = disk->private_data;
Grant Likely74489a92007-07-17 04:03:39 -0700909 unsigned long flags;
910 u16 val;
911
912 dev_dbg(ace->dev, "ace_release() users=%i\n", ace->users - 1);
913
914 spin_lock_irqsave(&ace->lock, flags);
915 ace->users--;
916 if (ace->users == 0) {
917 val = ace_in(ace, ACE_CTRL);
918 ace_out(ace, ACE_CTRL, val & ~ACE_CTRL_LOCKREQ);
919 }
920 spin_unlock_irqrestore(&ace->lock, flags);
921 return 0;
922}
923
Christoph Hellwiga6b3a932007-08-11 22:34:31 +0200924static int ace_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Grant Likely74489a92007-07-17 04:03:39 -0700925{
Christoph Hellwiga6b3a932007-08-11 22:34:31 +0200926 struct ace_device *ace = bdev->bd_disk->private_data;
Grant Likelyf0edef82009-04-08 14:13:04 +0200927 u16 *cf_id = ace->cf_id;
Grant Likely74489a92007-07-17 04:03:39 -0700928
Christoph Hellwiga6b3a932007-08-11 22:34:31 +0200929 dev_dbg(ace->dev, "ace_getgeo()\n");
Grant Likely74489a92007-07-17 04:03:39 -0700930
Bartlomiej Zolnierkiewicz4aaf2fe2009-04-01 21:42:22 +0200931 geo->heads = cf_id[ATA_ID_HEADS];
932 geo->sectors = cf_id[ATA_ID_SECTORS];
933 geo->cylinders = cf_id[ATA_ID_CYLS];
Christoph Hellwiga6b3a932007-08-11 22:34:31 +0200934
935 return 0;
Grant Likely74489a92007-07-17 04:03:39 -0700936}
937
938static struct block_device_operations ace_fops = {
939 .owner = THIS_MODULE,
Al Virof3f68b32008-03-02 10:24:18 -0500940 .open = ace_open,
941 .release = ace_release,
Grant Likely74489a92007-07-17 04:03:39 -0700942 .media_changed = ace_media_changed,
943 .revalidate_disk = ace_revalidate_disk,
Christoph Hellwiga6b3a932007-08-11 22:34:31 +0200944 .getgeo = ace_getgeo,
Grant Likely74489a92007-07-17 04:03:39 -0700945};
946
947/* --------------------------------------------------------------------
948 * SystemACE device setup/teardown code
949 */
950static int __devinit ace_setup(struct ace_device *ace)
951{
952 u16 version;
953 u16 val;
Grant Likely74489a92007-07-17 04:03:39 -0700954 int rc;
955
Grant Likely4a24d862007-10-01 16:33:54 +0200956 dev_dbg(ace->dev, "ace_setup(ace=0x%p)\n", ace);
Yuri Tikhonovc14464b2008-11-14 10:21:57 -0700957 dev_dbg(ace->dev, "physaddr=0x%llx irq=%i\n",
958 (unsigned long long)ace->physaddr, ace->irq);
Grant Likely4a24d862007-10-01 16:33:54 +0200959
Grant Likely74489a92007-07-17 04:03:39 -0700960 spin_lock_init(&ace->lock);
961 init_completion(&ace->id_completion);
962
963 /*
964 * Map the device
965 */
966 ace->baseaddr = ioremap(ace->physaddr, 0x80);
967 if (!ace->baseaddr)
968 goto err_ioremap;
969
Grant Likely74489a92007-07-17 04:03:39 -0700970 /*
971 * Initialize the state machine tasklet and stall timer
972 */
973 tasklet_init(&ace->fsm_tasklet, ace_fsm_tasklet, (unsigned long)ace);
974 setup_timer(&ace->stall_timer, ace_stall_timer, (unsigned long)ace);
975
976 /*
977 * Initialize the request queue
978 */
979 ace->queue = blk_init_queue(ace_request, &ace->lock);
980 if (ace->queue == NULL)
981 goto err_blk_initq;
982 blk_queue_hardsect_size(ace->queue, 512);
983
984 /*
985 * Allocate and initialize GD structure
986 */
987 ace->gd = alloc_disk(ACE_NUM_MINORS);
988 if (!ace->gd)
989 goto err_alloc_disk;
990
991 ace->gd->major = ace_major;
992 ace->gd->first_minor = ace->id * ACE_NUM_MINORS;
993 ace->gd->fops = &ace_fops;
994 ace->gd->queue = ace->queue;
995 ace->gd->private_data = ace;
996 snprintf(ace->gd->disk_name, 32, "xs%c", ace->id + 'a');
997
998 /* set bus width */
Grant Likely4a24d862007-10-01 16:33:54 +0200999 if (ace->bus_width == ACE_BUS_WIDTH_16) {
Grant Likely74489a92007-07-17 04:03:39 -07001000 /* 0x0101 should work regardless of endianess */
1001 ace_out_le16(ace, ACE_BUSMODE, 0x0101);
1002
1003 /* read it back to determine endianess */
1004 if (ace_in_le16(ace, ACE_BUSMODE) == 0x0001)
1005 ace->reg_ops = &ace_reg_le16_ops;
1006 else
1007 ace->reg_ops = &ace_reg_be16_ops;
1008 } else {
1009 ace_out_8(ace, ACE_BUSMODE, 0x00);
1010 ace->reg_ops = &ace_reg_8_ops;
1011 }
1012
1013 /* Make sure version register is sane */
1014 version = ace_in(ace, ACE_VERSION);
1015 if ((version == 0) || (version == 0xFFFF))
1016 goto err_read;
1017
1018 /* Put sysace in a sane state by clearing most control reg bits */
1019 ace_out(ace, ACE_CTRL, ACE_CTRL_FORCECFGMODE |
1020 ACE_CTRL_DATABUFRDYIRQ | ACE_CTRL_ERRORIRQ);
1021
Grant Likely32f6fff2007-10-01 16:33:54 +02001022 /* Now we can hook up the irq handler */
1023 if (ace->irq != NO_IRQ) {
1024 rc = request_irq(ace->irq, ace_interrupt, 0, "systemace", ace);
1025 if (rc) {
1026 /* Failure - fall back to polled mode */
1027 dev_err(ace->dev, "request_irq failed\n");
1028 ace->irq = NO_IRQ;
1029 }
1030 }
1031
Grant Likelyd2bbf3d2007-10-04 08:52:40 +02001032 /* Enable interrupts */
1033 val = ace_in(ace, ACE_CTRL);
1034 val |= ACE_CTRL_DATABUFRDYIRQ | ACE_CTRL_ERRORIRQ;
1035 ace_out(ace, ACE_CTRL, val);
1036
Grant Likely74489a92007-07-17 04:03:39 -07001037 /* Print the identification */
1038 dev_info(ace->dev, "Xilinx SystemACE revision %i.%i.%i\n",
1039 (version >> 12) & 0xf, (version >> 8) & 0x0f, version & 0xff);
Yuri Tikhonovc14464b2008-11-14 10:21:57 -07001040 dev_dbg(ace->dev, "physaddr 0x%llx, mapped to 0x%p, irq=%i\n",
1041 (unsigned long long) ace->physaddr, ace->baseaddr, ace->irq);
Grant Likely74489a92007-07-17 04:03:39 -07001042
1043 ace->media_change = 1;
1044 ace_revalidate_disk(ace->gd);
1045
1046 /* Make the sysace device 'live' */
1047 add_disk(ace->gd);
1048
1049 return 0;
1050
Grant Likelyed155a92007-10-01 16:33:56 +02001051err_read:
Grant Likely74489a92007-07-17 04:03:39 -07001052 put_disk(ace->gd);
Grant Likelyed155a92007-10-01 16:33:56 +02001053err_alloc_disk:
Grant Likely74489a92007-07-17 04:03:39 -07001054 blk_cleanup_queue(ace->queue);
Grant Likelyed155a92007-10-01 16:33:56 +02001055err_blk_initq:
Grant Likely74489a92007-07-17 04:03:39 -07001056 iounmap(ace->baseaddr);
Grant Likelyed155a92007-10-01 16:33:56 +02001057err_ioremap:
Yuri Tikhonovc14464b2008-11-14 10:21:57 -07001058 dev_info(ace->dev, "xsysace: error initializing device at 0x%llx\n",
1059 (unsigned long long) ace->physaddr);
Grant Likely74489a92007-07-17 04:03:39 -07001060 return -ENOMEM;
1061}
1062
1063static void __devexit ace_teardown(struct ace_device *ace)
1064{
1065 if (ace->gd) {
1066 del_gendisk(ace->gd);
1067 put_disk(ace->gd);
1068 }
1069
1070 if (ace->queue)
1071 blk_cleanup_queue(ace->queue);
1072
1073 tasklet_kill(&ace->fsm_tasklet);
1074
1075 if (ace->irq != NO_IRQ)
1076 free_irq(ace->irq, ace);
1077
1078 iounmap(ace->baseaddr);
1079}
1080
Grant Likely1b455462007-10-01 16:33:53 +02001081static int __devinit
Yuri Tikhonovc14464b2008-11-14 10:21:57 -07001082ace_alloc(struct device *dev, int id, resource_size_t physaddr,
Grant Likely1b455462007-10-01 16:33:53 +02001083 int irq, int bus_width)
1084{
1085 struct ace_device *ace;
1086 int rc;
1087 dev_dbg(dev, "ace_alloc(%p)\n", dev);
1088
1089 if (!physaddr) {
1090 rc = -ENODEV;
1091 goto err_noreg;
1092 }
1093
1094 /* Allocate and initialize the ace device structure */
1095 ace = kzalloc(sizeof(struct ace_device), GFP_KERNEL);
1096 if (!ace) {
1097 rc = -ENOMEM;
1098 goto err_alloc;
1099 }
1100
1101 ace->dev = dev;
1102 ace->id = id;
1103 ace->physaddr = physaddr;
1104 ace->irq = irq;
1105 ace->bus_width = bus_width;
1106
1107 /* Call the setup code */
Grant Likely34e1b832007-10-04 08:52:38 +02001108 rc = ace_setup(ace);
1109 if (rc)
Grant Likely1b455462007-10-01 16:33:53 +02001110 goto err_setup;
1111
1112 dev_set_drvdata(dev, ace);
1113 return 0;
1114
Grant Likelyed155a92007-10-01 16:33:56 +02001115err_setup:
Grant Likely1b455462007-10-01 16:33:53 +02001116 dev_set_drvdata(dev, NULL);
1117 kfree(ace);
Grant Likelyed155a92007-10-01 16:33:56 +02001118err_alloc:
1119err_noreg:
Grant Likely1b455462007-10-01 16:33:53 +02001120 dev_err(dev, "could not initialize device, err=%i\n", rc);
1121 return rc;
1122}
1123
1124static void __devexit ace_free(struct device *dev)
1125{
1126 struct ace_device *ace = dev_get_drvdata(dev);
1127 dev_dbg(dev, "ace_free(%p)\n", dev);
1128
1129 if (ace) {
1130 ace_teardown(ace);
1131 dev_set_drvdata(dev, NULL);
1132 kfree(ace);
1133 }
1134}
1135
Grant Likely74489a92007-07-17 04:03:39 -07001136/* ---------------------------------------------------------------------
1137 * Platform Bus Support
1138 */
1139
Grant Likelyedec4962007-10-01 16:33:52 +02001140static int __devinit ace_probe(struct platform_device *dev)
Grant Likely74489a92007-07-17 04:03:39 -07001141{
Yuri Tikhonovc14464b2008-11-14 10:21:57 -07001142 resource_size_t physaddr = 0;
Grant Likely4a24d862007-10-01 16:33:54 +02001143 int bus_width = ACE_BUS_WIDTH_16; /* FIXME: should not be hard coded */
Grant Likely1b455462007-10-01 16:33:53 +02001144 int id = dev->id;
1145 int irq = NO_IRQ;
Grant Likely74489a92007-07-17 04:03:39 -07001146 int i;
1147
Grant Likelyedec4962007-10-01 16:33:52 +02001148 dev_dbg(&dev->dev, "ace_probe(%p)\n", dev);
Grant Likely74489a92007-07-17 04:03:39 -07001149
Grant Likely74489a92007-07-17 04:03:39 -07001150 for (i = 0; i < dev->num_resources; i++) {
1151 if (dev->resource[i].flags & IORESOURCE_MEM)
Grant Likely1b455462007-10-01 16:33:53 +02001152 physaddr = dev->resource[i].start;
Grant Likely74489a92007-07-17 04:03:39 -07001153 if (dev->resource[i].flags & IORESOURCE_IRQ)
Grant Likely1b455462007-10-01 16:33:53 +02001154 irq = dev->resource[i].start;
Grant Likely74489a92007-07-17 04:03:39 -07001155 }
1156
Grant Likely74489a92007-07-17 04:03:39 -07001157 /* Call the bus-independant setup code */
Grant Likely1b455462007-10-01 16:33:53 +02001158 return ace_alloc(&dev->dev, id, physaddr, irq, bus_width);
Grant Likely74489a92007-07-17 04:03:39 -07001159}
1160
1161/*
1162 * Platform bus remove() method
1163 */
Grant Likelyedec4962007-10-01 16:33:52 +02001164static int __devexit ace_remove(struct platform_device *dev)
Grant Likely74489a92007-07-17 04:03:39 -07001165{
Grant Likely1b455462007-10-01 16:33:53 +02001166 ace_free(&dev->dev);
Grant Likely74489a92007-07-17 04:03:39 -07001167 return 0;
1168}
1169
Grant Likelyedec4962007-10-01 16:33:52 +02001170static struct platform_driver ace_platform_driver = {
Grant Likely74489a92007-07-17 04:03:39 -07001171 .probe = ace_probe,
1172 .remove = __devexit_p(ace_remove),
Grant Likelyedec4962007-10-01 16:33:52 +02001173 .driver = {
1174 .owner = THIS_MODULE,
1175 .name = "xsysace",
1176 },
Grant Likely74489a92007-07-17 04:03:39 -07001177};
1178
1179/* ---------------------------------------------------------------------
Grant Likely95e896c2007-10-01 16:33:55 +02001180 * OF_Platform Bus Support
1181 */
1182
1183#if defined(CONFIG_OF)
1184static int __devinit
1185ace_of_probe(struct of_device *op, const struct of_device_id *match)
1186{
1187 struct resource res;
Yuri Tikhonovc14464b2008-11-14 10:21:57 -07001188 resource_size_t physaddr;
Grant Likely95e896c2007-10-01 16:33:55 +02001189 const u32 *id;
1190 int irq, bus_width, rc;
1191
1192 dev_dbg(&op->dev, "ace_of_probe(%p, %p)\n", op, match);
1193
1194 /* device id */
1195 id = of_get_property(op->node, "port-number", NULL);
1196
1197 /* physaddr */
1198 rc = of_address_to_resource(op->node, 0, &res);
1199 if (rc) {
1200 dev_err(&op->dev, "invalid address\n");
1201 return rc;
1202 }
1203 physaddr = res.start;
1204
1205 /* irq */
1206 irq = irq_of_parse_and_map(op->node, 0);
1207
1208 /* bus width */
1209 bus_width = ACE_BUS_WIDTH_16;
1210 if (of_find_property(op->node, "8-bit", NULL))
1211 bus_width = ACE_BUS_WIDTH_8;
1212
1213 /* Call the bus-independant setup code */
1214 return ace_alloc(&op->dev, id ? *id : 0, physaddr, irq, bus_width);
1215}
1216
1217static int __devexit ace_of_remove(struct of_device *op)
1218{
1219 ace_free(&op->dev);
1220 return 0;
1221}
1222
1223/* Match table for of_platform binding */
Grant Likely911a3172008-02-06 10:23:12 -07001224static struct of_device_id ace_of_match[] __devinitdata = {
Stephen Neuendorffer0e349b02008-01-09 06:35:05 +11001225 { .compatible = "xlnx,opb-sysace-1.00.b", },
1226 { .compatible = "xlnx,opb-sysace-1.00.c", },
1227 { .compatible = "xlnx,xps-sysace-1.00.a", },
Yuri Tikhonovf5020382009-01-09 15:49:06 -07001228 { .compatible = "xlnx,sysace", },
Grant Likely95e896c2007-10-01 16:33:55 +02001229 {},
1230};
1231MODULE_DEVICE_TABLE(of, ace_of_match);
1232
1233static struct of_platform_driver ace_of_driver = {
1234 .owner = THIS_MODULE,
1235 .name = "xsysace",
1236 .match_table = ace_of_match,
1237 .probe = ace_of_probe,
1238 .remove = __devexit_p(ace_of_remove),
1239 .driver = {
1240 .name = "xsysace",
1241 },
1242};
1243
1244/* Registration helpers to keep the number of #ifdefs to a minimum */
1245static inline int __init ace_of_register(void)
1246{
1247 pr_debug("xsysace: registering OF binding\n");
1248 return of_register_platform_driver(&ace_of_driver);
1249}
1250
1251static inline void __exit ace_of_unregister(void)
1252{
1253 of_unregister_platform_driver(&ace_of_driver);
1254}
1255#else /* CONFIG_OF */
1256/* CONFIG_OF not enabled; do nothing helpers */
1257static inline int __init ace_of_register(void) { return 0; }
1258static inline void __exit ace_of_unregister(void) { }
1259#endif /* CONFIG_OF */
1260
1261/* ---------------------------------------------------------------------
Grant Likely74489a92007-07-17 04:03:39 -07001262 * Module init/exit routines
1263 */
1264static int __init ace_init(void)
1265{
Grant Likelyedec4962007-10-01 16:33:52 +02001266 int rc;
1267
Grant Likely74489a92007-07-17 04:03:39 -07001268 ace_major = register_blkdev(ace_major, "xsysace");
1269 if (ace_major <= 0) {
Grant Likelyedec4962007-10-01 16:33:52 +02001270 rc = -ENOMEM;
1271 goto err_blk;
Grant Likely74489a92007-07-17 04:03:39 -07001272 }
1273
Grant Likely34e1b832007-10-04 08:52:38 +02001274 rc = ace_of_register();
1275 if (rc)
Grant Likely95e896c2007-10-01 16:33:55 +02001276 goto err_of;
1277
Grant Likely4a24d862007-10-01 16:33:54 +02001278 pr_debug("xsysace: registering platform binding\n");
Grant Likely34e1b832007-10-04 08:52:38 +02001279 rc = platform_driver_register(&ace_platform_driver);
1280 if (rc)
Grant Likelyedec4962007-10-01 16:33:52 +02001281 goto err_plat;
1282
1283 pr_info("Xilinx SystemACE device driver, major=%i\n", ace_major);
1284 return 0;
1285
Grant Likelyed155a92007-10-01 16:33:56 +02001286err_plat:
Grant Likely95e896c2007-10-01 16:33:55 +02001287 ace_of_unregister();
Grant Likelyed155a92007-10-01 16:33:56 +02001288err_of:
Grant Likelyedec4962007-10-01 16:33:52 +02001289 unregister_blkdev(ace_major, "xsysace");
Grant Likelyed155a92007-10-01 16:33:56 +02001290err_blk:
Grant Likelyedec4962007-10-01 16:33:52 +02001291 printk(KERN_ERR "xsysace: registration failed; err=%i\n", rc);
1292 return rc;
Grant Likely74489a92007-07-17 04:03:39 -07001293}
1294
1295static void __exit ace_exit(void)
1296{
1297 pr_debug("Unregistering Xilinx SystemACE driver\n");
Grant Likelyedec4962007-10-01 16:33:52 +02001298 platform_driver_unregister(&ace_platform_driver);
Grant Likely95e896c2007-10-01 16:33:55 +02001299 ace_of_unregister();
Akinobu Mitac6d4d632007-07-17 04:03:46 -07001300 unregister_blkdev(ace_major, "xsysace");
Grant Likely74489a92007-07-17 04:03:39 -07001301}
1302
1303module_init(ace_init);
1304module_exit(ace_exit);