1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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>
92#include <linux/ata.h>
93#include <linux/hdreg.h>
94#include <linux/platform_device.h>
95#if defined(CONFIG_OF)
96#include <linux/of_device.h>
97#include <linux/of_platform.h>
98#endif
99
100MODULE_AUTHOR("Grant Likely <grant.likely@secretlab.ca>");
101MODULE_DESCRIPTION("Xilinx SystemACE device driver");
102MODULE_LICENSE("GPL");
103
104
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)
111#define ACE_STATUS_CFCERROR (0x00000008)
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
166#define ACE_BUS_WIDTH_8 0
167#define ACE_BUS_WIDTH_16 1
168
169struct ace_reg_ops;
170
171struct ace_device {
172
173 int id;
174 int media_change;
175 int users;
176 struct list_head list;
177
178
179 struct tasklet_struct fsm_tasklet;
180 uint fsm_task;
181 uint fsm_state;
182 uint fsm_continue_flag;
183 uint fsm_iter_num;
184 struct timer_list stall_timer;
185
186
187 struct request *req;
188 void *data_ptr;
189 int data_count;
190 int data_result;
191
192 int id_req_count;
193 int id_result;
194 struct completion id_completion;
195 int in_irq;
196
197
198 resource_size_t physaddr;
199 void __iomem *baseaddr;
200 int irq;
201 int bus_width;
202 struct ace_reg_ops *reg_ops;
203 int lock_count;
204
205
206 spinlock_t lock;
207 struct device *dev;
208 struct request_queue *queue;
209 struct gendisk *gd;
210
211
212 u16 cf_id[ATA_ID_WORDS];
213};
214
215static int ace_major;
216
217
218
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
229static u16 ace_in_8(struct ace_device *ace, int reg)
230{
231 void __iomem *r = ace->baseaddr + reg;
232 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{
237 void __iomem *r = ace->baseaddr + reg;
238 out_8(r, val);
239 out_8(r + 1, val >> 8);
240}
241
242static void ace_datain_8(struct ace_device *ace)
243{
244 void __iomem *r = ace->baseaddr + 0x40;
245 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{
254 void __iomem *r = ace->baseaddr + 0x40;
255 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
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
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
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"
394 KERN_INFO " status:%.8x mpu_lba:%.8x busmode:%4x\n"
395 KERN_INFO " error: %.8x cfg_lba:%.8x fatstat:%.4x\n",
396 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
406void ace_fix_driveid(u16 *id)
407{
408#if defined(__BIG_ENDIAN)
409 int i;
410
411
412 for (i = 0; i < ATA_ID_WORDS; i++, id++)
413 *id = le16_to_cpu(*id);
414#endif
415}
416
417
418
419
420
421
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
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
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
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
457 tasklet_schedule(&ace->fsm_tasklet);
458 ace->fsm_continue_flag = 0;
459}
460
461
462struct request *ace_get_next_request(struct request_queue * q)
463{
464 struct request *req;
465
466 while ((req = elv_next_request(q)) != NULL) {
467 if (blk_fs_request(req))
468 break;
469 end_request(req, 0);
470 }
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;
480
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
486
487
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
496 while ((req = elv_next_request(ace->queue)) != NULL)
497 end_request(req, 0);
498
499
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
508 switch (ace->fsm_state) {
509 case ACE_FSM_STATE_IDLE:
510
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
526 ace->fsm_state = ACE_FSM_STATE_WAIT_CFREADY;
527 break;
528 }
529
530
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
539 ace->fsm_state = ACE_FSM_STATE_WAIT_CFREADY;
540 break;
541 }
542
543
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
552 ace_fsm_yield(ace);
553 break;
554 }
555
556
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
565 ace->fsm_task = ACE_TASK_IDENTIFY;
566 ace->data_ptr = ace->cf_id;
567 ace->data_count = ACE_BUF_PER_SECTOR;
568 ace_out(ace, ACE_SECCNTCMD, ACE_SECCNTCMD_IDENTIFY);
569
570
571 val = ace_in(ace, ACE_CTRL);
572 ace_out(ace, ACE_CTRL, val | ACE_CTRL_CFGRESET);
573
574
575
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
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
596 ace->reg_ops->datain(ace);
597 ace->data_count--;
598
599
600 if (ace->data_count != 0) {
601 ace_fsm_yieldirq(ace);
602 break;
603 }
604
605
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:
611 ace_fix_driveid(ace->cf_id);
612 ace_dump_mem(ace->cf_id, 512);
613
614 if (ace->data_result) {
615
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
624 set_capacity(ace->gd,
625 ata_id_u32(ace->cf_id, ATA_ID_LBA_CAPACITY));
626 dev_info(ace->dev, "capacity: %i sectors\n",
627 ata_id_u32(ace->cf_id, ATA_ID_LBA_CAPACITY));
628 }
629
630
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
647 dev_dbg(ace->dev,
648 "request: sec=%llx hcnt=%lx, ccnt=%x, dir=%i\n",
649 (unsigned long long) req->sector, req->hard_nr_sectors,
650 req->current_nr_sectors, rq_data_dir(req));
651
652 ace->req = req;
653 ace->data_ptr = req->buffer;
654 ace->data_count = req->current_nr_sectors * ACE_BUF_PER_SECTOR;
655 ace_out32(ace, ACE_MPULBA, req->sector & 0x0FFFFFFF);
656
657 count = req->hard_nr_sectors;
658 if (rq_data_dir(req)) {
659
660 dev_dbg(ace->dev, "write data\n");
661 ace->fsm_task = ACE_TASK_WRITE;
662 ace_out(ace, ACE_SECCNTCMD,
663 count | ACE_SECCNTCMD_WRITE_DATA);
664 } else {
665
666 dev_dbg(ace->dev, "read data\n");
667 ace->fsm_task = ACE_TASK_READ;
668 ace_out(ace, ACE_SECCNTCMD,
669 count | ACE_SECCNTCMD_READ_DATA);
670 }
671
672
673 val = ace_in(ace, ACE_CTRL);
674 ace_out(ace, ACE_CTRL, val | ACE_CTRL_CFGRESET);
675
676
677
678
679 ace->fsm_state = ACE_FSM_STATE_REQ_TRANSFER;
680 if (ace->fsm_task == ACE_TASK_READ)
681 ace_fsm_yieldirq(ace);
682 break;
683
684 case ACE_FSM_STATE_REQ_TRANSFER:
685
686 status = ace_in32(ace, ACE_STATUS);
687 if (status & ACE_STATUS_CFBSY) {
688 dev_dbg(ace->dev,
689 "CFBSY set; t=%i iter=%i c=%i dc=%i irq=%i\n",
690 ace->fsm_task, ace->fsm_iter_num,
691 ace->req->current_nr_sectors * 16,
692 ace->data_count, ace->in_irq);
693 ace_fsm_yield(ace);
694 break;
695 }
696 if (!(status & ACE_STATUS_DATABUFRDY)) {
697 dev_dbg(ace->dev,
698 "DATABUF not set; t=%i iter=%i c=%i dc=%i irq=%i\n",
699 ace->fsm_task, ace->fsm_iter_num,
700 ace->req->current_nr_sectors * 16,
701 ace->data_count, ace->in_irq);
702 ace_fsm_yieldirq(ace);
703 break;
704 }
705
706
707 if (ace->fsm_task == ACE_TASK_WRITE)
708 ace->reg_ops->dataout(ace);
709 else
710 ace->reg_ops->datain(ace);
711 ace->data_count--;
712
713
714 if (ace->data_count != 0) {
715 ace_fsm_yieldirq(ace);
716 break;
717 }
718
719
720 if (__blk_end_request(ace->req, 0,
721 blk_rq_cur_bytes(ace->req))) {
722
723
724
725
726 ace->data_ptr = ace->req->buffer;
727 ace->data_count = ace->req->current_nr_sectors * 16;
728 ace_fsm_yieldirq(ace);
729 break;
730 }
731
732 ace->fsm_state = ACE_FSM_STATE_REQ_COMPLETE;
733 break;
734
735 case ACE_FSM_STATE_REQ_COMPLETE:
736 ace->req = NULL;
737
738
739 ace->fsm_state = ACE_FSM_STATE_IDLE;
740 break;
741
742 default:
743 ace->fsm_state = ACE_FSM_STATE_IDLE;
744 break;
745 }
746}
747
748static void ace_fsm_tasklet(unsigned long data)
749{
750 struct ace_device *ace = (void *)data;
751 unsigned long flags;
752
753 spin_lock_irqsave(&ace->lock, flags);
754
755
756 ace->fsm_continue_flag = 1;
757 while (ace->fsm_continue_flag)
758 ace_fsm_dostate(ace);
759
760 spin_unlock_irqrestore(&ace->lock, flags);
761}
762
763static void ace_stall_timer(unsigned long data)
764{
765 struct ace_device *ace = (void *)data;
766 unsigned long flags;
767
768 dev_warn(ace->dev,
769 "kicking stalled fsm; state=%i task=%i iter=%i dc=%i\n",
770 ace->fsm_state, ace->fsm_task, ace->fsm_iter_num,
771 ace->data_count);
772 spin_lock_irqsave(&ace->lock, flags);
773
774
775
776 mod_timer(&ace->stall_timer, jiffies + HZ);
777
778
779 ace->fsm_continue_flag = 1;
780 while (ace->fsm_continue_flag)
781 ace_fsm_dostate(ace);
782
783 spin_unlock_irqrestore(&ace->lock, flags);
784}
785
786
787
788
789static int ace_interrupt_checkstate(struct ace_device *ace)
790{
791 u32 sreg = ace_in32(ace, ACE_STATUS);
792 u16 creg = ace_in(ace, ACE_CTRL);
793
794
795 if ((sreg & (ACE_STATUS_CFGERROR | ACE_STATUS_CFCERROR)) &&
796 (creg & ACE_CTRL_ERRORIRQ)) {
797 dev_err(ace->dev, "transfer failure\n");
798 ace_dump_regs(ace);
799 return -EIO;
800 }
801
802 return 0;
803}
804
805static irqreturn_t ace_interrupt(int irq, void *dev_id)
806{
807 u16 creg;
808 struct ace_device *ace = dev_id;
809
810
811 spin_lock(&ace->lock);
812 ace->in_irq = 1;
813
814
815 creg = ace_in(ace, ACE_CTRL);
816 ace_out(ace, ACE_CTRL, creg | ACE_CTRL_RESETIRQ);
817 ace_out(ace, ACE_CTRL, creg);
818
819
820 if (ace_interrupt_checkstate(ace))
821 ace->data_result = -EIO;
822
823 if (ace->fsm_task == 0) {
824 dev_err(ace->dev,
825 "spurious irq; stat=%.8x ctrl=%.8x cmd=%.4x\n",
826 ace_in32(ace, ACE_STATUS), ace_in32(ace, ACE_CTRL),
827 ace_in(ace, ACE_SECCNTCMD));
828 dev_err(ace->dev, "fsm_task=%i fsm_state=%i data_count=%i\n",
829 ace->fsm_task, ace->fsm_state, ace->data_count);
830 }
831
832
833 ace->fsm_continue_flag = 1;
834 while (ace->fsm_continue_flag)
835 ace_fsm_dostate(ace);
836
837
838 ace->in_irq = 0;
839 spin_unlock(&ace->lock);
840
841 return IRQ_HANDLED;
842}
843
844
845
846
847static void ace_request(struct request_queue * q)
848{
849 struct request *req;
850 struct ace_device *ace;
851
852 req = ace_get_next_request(q);
853
854 if (req) {
855 ace = req->rq_disk->private_data;
856 tasklet_schedule(&ace->fsm_tasklet);
857 }
858}
859
860static int ace_media_changed(struct gendisk *gd)
861{
862 struct ace_device *ace = gd->private_data;
863 dev_dbg(ace->dev, "ace_media_changed(): %i\n", ace->media_change);
864
865 return ace->media_change;
866}
867
868static int ace_revalidate_disk(struct gendisk *gd)
869{
870 struct ace_device *ace = gd->private_data;
871 unsigned long flags;
872
873 dev_dbg(ace->dev, "ace_revalidate_disk()\n");
874
875 if (ace->media_change) {
876 dev_dbg(ace->dev, "requesting cf id and scheduling tasklet\n");
877
878 spin_lock_irqsave(&ace->lock, flags);
879 ace->id_req_count++;
880 spin_unlock_irqrestore(&ace->lock, flags);
881
882 tasklet_schedule(&ace->fsm_tasklet);
883 wait_for_completion(&ace->id_completion);
884 }
885
886 dev_dbg(ace->dev, "revalidate complete\n");
887 return ace->id_result;
888}
889
890static int ace_open(struct block_device *bdev, fmode_t mode)
891{
892 struct ace_device *ace = bdev->bd_disk->private_data;
893 unsigned long flags;
894
895 dev_dbg(ace->dev, "ace_open() users=%i\n", ace->users + 1);
896
897 spin_lock_irqsave(&ace->lock, flags);
898 ace->users++;
899 spin_unlock_irqrestore(&ace->lock, flags);
900
901 check_disk_change(bdev);
902 return 0;
903}
904
905static int ace_release(struct gendisk *disk, fmode_t mode)
906{
907 struct ace_device *ace = disk->private_data;
908 unsigned long flags;
909 u16 val;
910
911 dev_dbg(ace->dev, "ace_release() users=%i\n", ace->users - 1);
912
913 spin_lock_irqsave(&ace->lock, flags);
914 ace->users--;
915 if (ace->users == 0) {
916 val = ace_in(ace, ACE_CTRL);
917 ace_out(ace, ACE_CTRL, val & ~ACE_CTRL_LOCKREQ);
918 }
919 spin_unlock_irqrestore(&ace->lock, flags);
920 return 0;
921}
922
923static int ace_getgeo(struct block_device *bdev, struct hd_geometry *geo)
924{
925 struct ace_device *ace = bdev->bd_disk->private_data;
926 u16 *cf_id = ace->cf_id;
927
928 dev_dbg(ace->dev, "ace_getgeo()\n");
929
930 geo->heads = cf_id[ATA_ID_HEADS];
931 geo->sectors = cf_id[ATA_ID_SECTORS];
932 geo->cylinders = cf_id[ATA_ID_CYLS];
933
934 return 0;
935}
936
937static struct block_device_operations ace_fops = {
938 .owner = THIS_MODULE,
939 .open = ace_open,
940 .release = ace_release,
941 .media_changed = ace_media_changed,
942 .revalidate_disk = ace_revalidate_disk,
943 .getgeo = ace_getgeo,
944};
945
946
947
948
949static int __devinit ace_setup(struct ace_device *ace)
950{
951 u16 version;
952 u16 val;
953 int rc;
954
955 dev_dbg(ace->dev, "ace_setup(ace=0x%p)\n", ace);
956 dev_dbg(ace->dev, "physaddr=0x%llx irq=%i\n",
957 (unsigned long long)ace->physaddr, ace->irq);
958
959 spin_lock_init(&ace->lock);
960 init_completion(&ace->id_completion);
961
962
963
964
965 ace->baseaddr = ioremap(ace->physaddr, 0x80);
966 if (!ace->baseaddr)
967 goto err_ioremap;
968
969
970
971
972 tasklet_init(&ace->fsm_tasklet, ace_fsm_tasklet, (unsigned long)ace);
973 setup_timer(&ace->stall_timer, ace_stall_timer, (unsigned long)ace);
974
975
976
977
978 ace->queue = blk_init_queue(ace_request, &ace->lock);
979 if (ace->queue == NULL)
980 goto err_blk_initq;
981 blk_queue_hardsect_size(ace->queue, 512);
982
983
984
985
986 ace->gd = alloc_disk(ACE_NUM_MINORS);
987 if (!ace->gd)
988 goto err_alloc_disk;
989
990 ace->gd->major = ace_major;
991 ace->gd->first_minor = ace->id * ACE_NUM_MINORS;
992 ace->gd->fops = &ace_fops;
993 ace->gd->queue = ace->queue;
994 ace->gd->private_data = ace;
995 snprintf(ace->gd->disk_name, 32, "xs%c", ace->id + 'a');
996
997
998 if (ace->bus_width == ACE_BUS_WIDTH_16) {
999
1000 ace_out_le16(ace, ACE_BUSMODE, 0x0101);
1001
1002
1003 if (ace_in_le16(ace, ACE_BUSMODE) == 0x0001)
1004 ace->reg_ops = &ace_reg_le16_ops;
1005 else
1006 ace->reg_ops = &ace_reg_be16_ops;
1007 } else {
1008 ace_out_8(ace, ACE_BUSMODE, 0x00);
1009 ace->reg_ops = &ace_reg_8_ops;
1010 }
1011
1012
1013 version = ace_in(ace, ACE_VERSION);
1014 if ((version == 0) || (version == 0xFFFF))
1015 goto err_read;
1016
1017
1018 ace_out(ace, ACE_CTRL, ACE_CTRL_FORCECFGMODE |
1019 ACE_CTRL_DATABUFRDYIRQ | ACE_CTRL_ERRORIRQ);
1020
1021
1022 if (ace->irq != NO_IRQ) {
1023 rc = request_irq(ace->irq, ace_interrupt, 0, "systemace", ace);
1024 if (rc) {
1025
1026 dev_err(ace->dev, "request_irq failed\n");
1027 ace->irq = NO_IRQ;
1028 }
1029 }
1030
1031
1032 val = ace_in(ace, ACE_CTRL);
1033 val |= ACE_CTRL_DATABUFRDYIRQ | ACE_CTRL_ERRORIRQ;
1034 ace_out(ace, ACE_CTRL, val);
1035
1036
1037 dev_info(ace->dev, "Xilinx SystemACE revision %i.%i.%i\n",
1038 (version >> 12) & 0xf, (version >> 8) & 0x0f, version & 0xff);
1039 dev_dbg(ace->dev, "physaddr 0x%llx, mapped to 0x%p, irq=%i\n",
1040 (unsigned long long) ace->physaddr, ace->baseaddr, ace->irq);
1041
1042 ace->media_change = 1;
1043 ace_revalidate_disk(ace->gd);
1044
1045
1046 add_disk(ace->gd);
1047
1048 return 0;
1049
1050err_read:
1051 put_disk(ace->gd);
1052err_alloc_disk:
1053 blk_cleanup_queue(ace->queue);
1054err_blk_initq:
1055 iounmap(ace->baseaddr);
1056err_ioremap:
1057 dev_info(ace->dev, "xsysace: error initializing device at 0x%llx\n",
1058 (unsigned long long) ace->physaddr);
1059 return -ENOMEM;
1060}
1061
1062static void __devexit ace_teardown(struct ace_device *ace)
1063{
1064 if (ace->gd) {
1065 del_gendisk(ace->gd);
1066 put_disk(ace->gd);
1067 }
1068
1069 if (ace->queue)
1070 blk_cleanup_queue(ace->queue);
1071
1072 tasklet_kill(&ace->fsm_tasklet);
1073
1074 if (ace->irq != NO_IRQ)
1075 free_irq(ace->irq, ace);
1076
1077 iounmap(ace->baseaddr);
1078}
1079
1080static int __devinit
1081ace_alloc(struct device *dev, int id, resource_size_t physaddr,
1082 int irq, int bus_width)
1083{
1084 struct ace_device *ace;
1085 int rc;
1086 dev_dbg(dev, "ace_alloc(%p)\n", dev);
1087
1088 if (!physaddr) {
1089 rc = -ENODEV;
1090 goto err_noreg;
1091 }
1092
1093
1094 ace = kzalloc(sizeof(struct ace_device), GFP_KERNEL);
1095 if (!ace) {
1096 rc = -ENOMEM;
1097 goto err_alloc;
1098 }
1099
1100 ace->dev = dev;
1101 ace->id = id;
1102 ace->physaddr = physaddr;
1103 ace->irq = irq;
1104 ace->bus_width = bus_width;
1105
1106
1107 rc = ace_setup(ace);
1108 if (rc)
1109 goto err_setup;
1110
1111 dev_set_drvdata(dev, ace);
1112 return 0;
1113
1114err_setup:
1115 dev_set_drvdata(dev, NULL);
1116 kfree(ace);
1117err_alloc:
1118err_noreg:
1119 dev_err(dev, "could not initialize device, err=%i\n", rc);
1120 return rc;
1121}
1122
1123static void __devexit ace_free(struct device *dev)
1124{
1125 struct ace_device *ace = dev_get_drvdata(dev);
1126 dev_dbg(dev, "ace_free(%p)\n", dev);
1127
1128 if (ace) {
1129 ace_teardown(ace);
1130 dev_set_drvdata(dev, NULL);
1131 kfree(ace);
1132 }
1133}
1134
1135
1136
1137
1138
1139static int __devinit ace_probe(struct platform_device *dev)
1140{
1141 resource_size_t physaddr = 0;
1142 int bus_width = ACE_BUS_WIDTH_16;
1143 int id = dev->id;
1144 int irq = NO_IRQ;
1145 int i;
1146
1147 dev_dbg(&dev->dev, "ace_probe(%p)\n", dev);
1148
1149 for (i = 0; i < dev->num_resources; i++) {
1150 if (dev->resource[i].flags & IORESOURCE_MEM)
1151 physaddr = dev->resource[i].start;
1152 if (dev->resource[i].flags & IORESOURCE_IRQ)
1153 irq = dev->resource[i].start;
1154 }
1155
1156
1157 return ace_alloc(&dev->dev, id, physaddr, irq, bus_width);
1158}
1159
1160
1161
1162
1163static int __devexit ace_remove(struct platform_device *dev)
1164{
1165 ace_free(&dev->dev);
1166 return 0;
1167}
1168
1169static struct platform_driver ace_platform_driver = {
1170 .probe = ace_probe,
1171 .remove = __devexit_p(ace_remove),
1172 .driver = {
1173 .owner = THIS_MODULE,
1174 .name = "xsysace",
1175 },
1176};
1177
1178
1179
1180
1181
1182#if defined(CONFIG_OF)
1183static int __devinit
1184ace_of_probe(struct of_device *op, const struct of_device_id *match)
1185{
1186 struct resource res;
1187 resource_size_t physaddr;
1188 const u32 *id;
1189 int irq, bus_width, rc;
1190
1191 dev_dbg(&op->dev, "ace_of_probe(%p, %p)\n", op, match);
1192
1193
1194 id = of_get_property(op->node, "port-number", NULL);
1195
1196
1197 rc = of_address_to_resource(op->node, 0, &res);
1198 if (rc) {
1199 dev_err(&op->dev, "invalid address\n");
1200 return rc;
1201 }
1202 physaddr = res.start;
1203
1204
1205 irq = irq_of_parse_and_map(op->node, 0);
1206
1207
1208 bus_width = ACE_BUS_WIDTH_16;
1209 if (of_find_property(op->node, "8-bit", NULL))
1210 bus_width = ACE_BUS_WIDTH_8;
1211
1212
1213 return ace_alloc(&op->dev, id ? *id : 0, physaddr, irq, bus_width);
1214}
1215
1216static int __devexit ace_of_remove(struct of_device *op)
1217{
1218 ace_free(&op->dev);
1219 return 0;
1220}
1221
1222
1223static struct of_device_id ace_of_match[] __devinitdata = {
1224 { .compatible = "xlnx,opb-sysace-1.00.b", },
1225 { .compatible = "xlnx,opb-sysace-1.00.c", },
1226 { .compatible = "xlnx,xps-sysace-1.00.a", },
1227 { .compatible = "xlnx,sysace", },
1228 {},
1229};
1230MODULE_DEVICE_TABLE(of, ace_of_match);
1231
1232static struct of_platform_driver ace_of_driver = {
1233 .owner = THIS_MODULE,
1234 .name = "xsysace",
1235 .match_table = ace_of_match,
1236 .probe = ace_of_probe,
1237 .remove = __devexit_p(ace_of_remove),
1238 .driver = {
1239 .name = "xsysace",
1240 },
1241};
1242
1243
1244static inline int __init ace_of_register(void)
1245{
1246 pr_debug("xsysace: registering OF binding\n");
1247 return of_register_platform_driver(&ace_of_driver);
1248}
1249
1250static inline void __exit ace_of_unregister(void)
1251{
1252 of_unregister_platform_driver(&ace_of_driver);
1253}
1254#else
1255
1256static inline int __init ace_of_register(void) { return 0; }
1257static inline void __exit ace_of_unregister(void) { }
1258#endif
1259
1260
1261
1262
1263static int __init ace_init(void)
1264{
1265 int rc;
1266
1267 ace_major = register_blkdev(ace_major, "xsysace");
1268 if (ace_major <= 0) {
1269 rc = -ENOMEM;
1270 goto err_blk;
1271 }
1272
1273 rc = ace_of_register();
1274 if (rc)
1275 goto err_of;
1276
1277 pr_debug("xsysace: registering platform binding\n");
1278 rc = platform_driver_register(&ace_platform_driver);
1279 if (rc)
1280 goto err_plat;
1281
1282 pr_info("Xilinx SystemACE device driver, major=%i\n", ace_major);
1283 return 0;
1284
1285err_plat:
1286 ace_of_unregister();
1287err_of:
1288 unregister_blkdev(ace_major, "xsysace");
1289err_blk:
1290 printk(KERN_ERR "xsysace: registration failed; err=%i\n", rc);
1291 return rc;
1292}
1293
1294static void __exit ace_exit(void)
1295{
1296 pr_debug("Unregistering Xilinx SystemACE driver\n");
1297 platform_driver_unregister(&ace_platform_driver);
1298 ace_of_unregister();
1299 unregister_blkdev(ace_major, "xsysace");
1300}
1301
1302module_init(ace_init);
1303module_exit(ace_exit);
1304