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/hdreg.h>
93#include <linux/platform_device.h>
94#if defined(CONFIG_OF)
95#include <linux/of_device.h>
96#include <linux/of_platform.h>
97#endif
98
99MODULE_AUTHOR("Grant Likely <grant.likely@secretlab.ca>");
100MODULE_DESCRIPTION("Xilinx SystemACE device driver");
101MODULE_LICENSE("GPL");
102
103
104#define ACE_BUSMODE (0x00)
105
106#define ACE_STATUS (0x04)
107#define ACE_STATUS_CFGLOCK (0x00000001)
108#define ACE_STATUS_MPULOCK (0x00000002)
109#define ACE_STATUS_CFGERROR (0x00000004)
110#define ACE_STATUS_CFCERROR (0x00000008)
111#define ACE_STATUS_CFDETECT (0x00000010)
112#define ACE_STATUS_DATABUFRDY (0x00000020)
113#define ACE_STATUS_DATABUFMODE (0x00000040)
114#define ACE_STATUS_CFGDONE (0x00000080)
115#define ACE_STATUS_RDYFORCFCMD (0x00000100)
116#define ACE_STATUS_CFGMODEPIN (0x00000200)
117#define ACE_STATUS_CFGADDR_MASK (0x0000e000)
118#define ACE_STATUS_CFBSY (0x00020000)
119#define ACE_STATUS_CFRDY (0x00040000)
120#define ACE_STATUS_CFDWF (0x00080000)
121#define ACE_STATUS_CFDSC (0x00100000)
122#define ACE_STATUS_CFDRQ (0x00200000)
123#define ACE_STATUS_CFCORR (0x00400000)
124#define ACE_STATUS_CFERR (0x00800000)
125
126#define ACE_ERROR (0x08)
127#define ACE_CFGLBA (0x0c)
128#define ACE_MPULBA (0x10)
129
130#define ACE_SECCNTCMD (0x14)
131#define ACE_SECCNTCMD_RESET (0x0100)
132#define ACE_SECCNTCMD_IDENTIFY (0x0200)
133#define ACE_SECCNTCMD_READ_DATA (0x0300)
134#define ACE_SECCNTCMD_WRITE_DATA (0x0400)
135#define ACE_SECCNTCMD_ABORT (0x0600)
136
137#define ACE_VERSION (0x16)
138#define ACE_VERSION_REVISION_MASK (0x00FF)
139#define ACE_VERSION_MINOR_MASK (0x0F00)
140#define ACE_VERSION_MAJOR_MASK (0xF000)
141
142#define ACE_CTRL (0x18)
143#define ACE_CTRL_FORCELOCKREQ (0x0001)
144#define ACE_CTRL_LOCKREQ (0x0002)
145#define ACE_CTRL_FORCECFGADDR (0x0004)
146#define ACE_CTRL_FORCECFGMODE (0x0008)
147#define ACE_CTRL_CFGMODE (0x0010)
148#define ACE_CTRL_CFGSTART (0x0020)
149#define ACE_CTRL_CFGSEL (0x0040)
150#define ACE_CTRL_CFGRESET (0x0080)
151#define ACE_CTRL_DATABUFRDYIRQ (0x0100)
152#define ACE_CTRL_ERRORIRQ (0x0200)
153#define ACE_CTRL_CFGDONEIRQ (0x0400)
154#define ACE_CTRL_RESETIRQ (0x0800)
155#define ACE_CTRL_CFGPROG (0x1000)
156#define ACE_CTRL_CFGADDR_MASK (0xe000)
157
158#define ACE_FATSTAT (0x1c)
159
160#define ACE_NUM_MINORS 16
161#define ACE_SECTOR_SIZE (512)
162#define ACE_FIFO_SIZE (32)
163#define ACE_BUF_PER_SECTOR (ACE_SECTOR_SIZE / ACE_FIFO_SIZE)
164
165#define ACE_BUS_WIDTH_8 0
166#define ACE_BUS_WIDTH_16 1
167
168struct ace_reg_ops;
169
170struct ace_device {
171
172 int id;
173 int media_change;
174 int users;
175 struct list_head list;
176
177
178 struct tasklet_struct fsm_tasklet;
179 uint fsm_task;
180 uint fsm_state;
181 uint fsm_continue_flag;
182 uint fsm_iter_num;
183 struct timer_list stall_timer;
184
185
186 struct request *req;
187 void *data_ptr;
188 int data_count;
189 int data_result;
190
191 int id_req_count;
192 int id_result;
193 struct completion id_completion;
194 int in_irq;
195
196
197 unsigned long physaddr;
198 void __iomem *baseaddr;
199 int irq;
200 int bus_width;
201 struct ace_reg_ops *reg_ops;
202 int lock_count;
203
204
205 spinlock_t lock;
206 struct device *dev;
207 struct request_queue *queue;
208 struct gendisk *gd;
209
210
211 struct hd_driveid cf_id;
212};
213
214static int ace_major;
215
216
217
218
219
220struct ace_reg_ops {
221 u16(*in) (struct ace_device * ace, int reg);
222 void (*out) (struct ace_device * ace, int reg, u16 val);
223 void (*datain) (struct ace_device * ace);
224 void (*dataout) (struct ace_device * ace);
225};
226
227
228static u16 ace_in_8(struct ace_device *ace, int reg)
229{
230 void __iomem *r = ace->baseaddr + reg;
231 return in_8(r) | (in_8(r + 1) << 8);
232}
233
234static void ace_out_8(struct ace_device *ace, int reg, u16 val)
235{
236 void __iomem *r = ace->baseaddr + reg;
237 out_8(r, val);
238 out_8(r + 1, val >> 8);
239}
240
241static void ace_datain_8(struct ace_device *ace)
242{
243 void __iomem *r = ace->baseaddr + 0x40;
244 u8 *dst = ace->data_ptr;
245 int i = ACE_FIFO_SIZE;
246 while (i--)
247 *dst++ = in_8(r++);
248 ace->data_ptr = dst;
249}
250
251static void ace_dataout_8(struct ace_device *ace)
252{
253 void __iomem *r = ace->baseaddr + 0x40;
254 u8 *src = ace->data_ptr;
255 int i = ACE_FIFO_SIZE;
256 while (i--)
257 out_8(r++, *src++);
258 ace->data_ptr = src;
259}
260
261static struct ace_reg_ops ace_reg_8_ops = {
262 .in = ace_in_8,
263 .out = ace_out_8,
264 .datain = ace_datain_8,
265 .dataout = ace_dataout_8,
266};
267
268
269static u16 ace_in_be16(struct ace_device *ace, int reg)
270{
271 return in_be16(ace->baseaddr + reg);
272}
273
274static void ace_out_be16(struct ace_device *ace, int reg, u16 val)
275{
276 out_be16(ace->baseaddr + reg, val);
277}
278
279static void ace_datain_be16(struct ace_device *ace)
280{
281 int i = ACE_FIFO_SIZE / 2;
282 u16 *dst = ace->data_ptr;
283 while (i--)
284 *dst++ = in_le16(ace->baseaddr + 0x40);
285 ace->data_ptr = dst;
286}
287
288static void ace_dataout_be16(struct ace_device *ace)
289{
290 int i = ACE_FIFO_SIZE / 2;
291 u16 *src = ace->data_ptr;
292 while (i--)
293 out_le16(ace->baseaddr + 0x40, *src++);
294 ace->data_ptr = src;
295}
296
297
298static u16 ace_in_le16(struct ace_device *ace, int reg)
299{
300 return in_le16(ace->baseaddr + reg);
301}
302
303static void ace_out_le16(struct ace_device *ace, int reg, u16 val)
304{
305 out_le16(ace->baseaddr + reg, val);
306}
307
308static void ace_datain_le16(struct ace_device *ace)
309{
310 int i = ACE_FIFO_SIZE / 2;
311 u16 *dst = ace->data_ptr;
312 while (i--)
313 *dst++ = in_be16(ace->baseaddr + 0x40);
314 ace->data_ptr = dst;
315}
316
317static void ace_dataout_le16(struct ace_device *ace)
318{
319 int i = ACE_FIFO_SIZE / 2;
320 u16 *src = ace->data_ptr;
321 while (i--)
322 out_be16(ace->baseaddr + 0x40, *src++);
323 ace->data_ptr = src;
324}
325
326static struct ace_reg_ops ace_reg_be16_ops = {
327 .in = ace_in_be16,
328 .out = ace_out_be16,
329 .datain = ace_datain_be16,
330 .dataout = ace_dataout_be16,
331};
332
333static struct ace_reg_ops ace_reg_le16_ops = {
334 .in = ace_in_le16,
335 .out = ace_out_le16,
336 .datain = ace_datain_le16,
337 .dataout = ace_dataout_le16,
338};
339
340static inline u16 ace_in(struct ace_device *ace, int reg)
341{
342 return ace->reg_ops->in(ace, reg);
343}
344
345static inline u32 ace_in32(struct ace_device *ace, int reg)
346{
347 return ace_in(ace, reg) | (ace_in(ace, reg + 2) << 16);
348}
349
350static inline void ace_out(struct ace_device *ace, int reg, u16 val)
351{
352 ace->reg_ops->out(ace, reg, val);
353}
354
355static inline void ace_out32(struct ace_device *ace, int reg, u32 val)
356{
357 ace_out(ace, reg, val);
358 ace_out(ace, reg + 2, val >> 16);
359}
360
361
362
363
364
365#if defined(DEBUG)
366static void ace_dump_mem(void *base, int len)
367{
368 const char *ptr = base;
369 int i, j;
370
371 for (i = 0; i < len; i += 16) {
372 printk(KERN_INFO "%.8x:", i);
373 for (j = 0; j < 16; j++) {
374 if (!(j % 4))
375 printk(" ");
376 printk("%.2x", ptr[i + j]);
377 }
378 printk(" ");
379 for (j = 0; j < 16; j++)
380 printk("%c", isprint(ptr[i + j]) ? ptr[i + j] : '.');
381 printk("\n");
382 }
383}
384#else
385static inline void ace_dump_mem(void *base, int len)
386{
387}
388#endif
389
390static void ace_dump_regs(struct ace_device *ace)
391{
392 dev_info(ace->dev, " ctrl: %.8x seccnt/cmd: %.4x ver:%.4x\n"
393 KERN_INFO " status:%.8x mpu_lba:%.8x busmode:%4x\n"
394 KERN_INFO " error: %.8x cfg_lba:%.8x fatstat:%.4x\n",
395 ace_in32(ace, ACE_CTRL),
396 ace_in(ace, ACE_SECCNTCMD),
397 ace_in(ace, ACE_VERSION),
398 ace_in32(ace, ACE_STATUS),
399 ace_in32(ace, ACE_MPULBA),
400 ace_in(ace, ACE_BUSMODE),
401 ace_in32(ace, ACE_ERROR),
402 ace_in32(ace, ACE_CFGLBA), ace_in(ace, ACE_FATSTAT));
403}
404
405void ace_fix_driveid(struct hd_driveid *id)
406{
407#if defined(__BIG_ENDIAN)
408 u16 *buf = (void *)id;
409 int i;
410
411
412 for (i = 0; i < sizeof(struct hd_driveid); i += 2, buf++)
413 *buf = le16_to_cpu(*buf);
414
415
416 id->lba_capacity = ((id->lba_capacity >> 16) & 0x0000FFFF) |
417 ((id->lba_capacity << 16) & 0xFFFF0000);
418 id->spg = ((id->spg >> 16) & 0x0000FFFF) |
419 ((id->spg << 16) & 0xFFFF0000);
420#endif
421}
422
423
424
425
426
427
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
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
449static 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
457static inline void ace_fsm_yieldirq(struct ace_device *ace)
458{
459 dev_dbg(ace->dev, "ace_fsm_yieldirq()\n");
460
461 if (ace->irq == NO_IRQ)
462
463 tasklet_schedule(&ace->fsm_tasklet);
464 ace->fsm_continue_flag = 0;
465}
466
467
468struct request *ace_get_next_request(struct request_queue * q)
469{
470 struct request *req;
471
472 while ((req = elv_next_request(q)) != NULL) {
473 if (blk_fs_request(req))
474 break;
475 end_request(req, 0);
476 }
477 return req;
478}
479
480static void ace_fsm_dostate(struct ace_device *ace)
481{
482 struct request *req;
483 u32 status;
484 u16 val;
485 int count;
486
487#if defined(DEBUG)
488 dev_dbg(ace->dev, "fsm_state=%i, id_req_count=%i\n",
489 ace->fsm_state, ace->id_req_count);
490#endif
491
492 switch (ace->fsm_state) {
493 case ACE_FSM_STATE_IDLE:
494
495 if (ace->id_req_count || ace_get_next_request(ace->queue)) {
496 ace->fsm_iter_num++;
497 ace->fsm_state = ACE_FSM_STATE_REQ_LOCK;
498 mod_timer(&ace->stall_timer, jiffies + HZ);
499 if (!timer_pending(&ace->stall_timer))
500 add_timer(&ace->stall_timer);
501 break;
502 }
503 del_timer(&ace->stall_timer);
504 ace->fsm_continue_flag = 0;
505 break;
506
507 case ACE_FSM_STATE_REQ_LOCK:
508 if (ace_in(ace, ACE_STATUS) & ACE_STATUS_MPULOCK) {
509
510 ace->fsm_state = ACE_FSM_STATE_WAIT_CFREADY;
511 break;
512 }
513
514
515 val = ace_in(ace, ACE_CTRL);
516 ace_out(ace, ACE_CTRL, val | ACE_CTRL_LOCKREQ);
517 ace->fsm_state = ACE_FSM_STATE_WAIT_LOCK;
518 break;
519
520 case ACE_FSM_STATE_WAIT_LOCK:
521 if (ace_in(ace, ACE_STATUS) & ACE_STATUS_MPULOCK) {
522
523 ace->fsm_state = ACE_FSM_STATE_WAIT_CFREADY;
524 break;
525 }
526
527
528 ace_fsm_yield(ace);
529 break;
530
531 case ACE_FSM_STATE_WAIT_CFREADY:
532 status = ace_in32(ace, ACE_STATUS);
533 if (!(status & ACE_STATUS_RDYFORCFCMD) ||
534 (status & ACE_STATUS_CFBSY)) {
535
536 ace_fsm_yield(ace);
537 break;
538 }
539
540
541 if (ace->id_req_count)
542 ace->fsm_state = ACE_FSM_STATE_IDENTIFY_PREPARE;
543 else
544 ace->fsm_state = ACE_FSM_STATE_REQ_PREPARE;
545 break;
546
547 case ACE_FSM_STATE_IDENTIFY_PREPARE:
548
549 ace->fsm_task = ACE_TASK_IDENTIFY;
550 ace->data_ptr = &ace->cf_id;
551 ace->data_count = ACE_BUF_PER_SECTOR;
552 ace_out(ace, ACE_SECCNTCMD, ACE_SECCNTCMD_IDENTIFY);
553
554
555 val = ace_in(ace, ACE_CTRL);
556 ace_out(ace, ACE_CTRL, val | ACE_CTRL_CFGRESET);
557
558
559
560 ace->fsm_state = ACE_FSM_STATE_IDENTIFY_TRANSFER;
561 ace_fsm_yieldirq(ace);
562 break;
563
564 case ACE_FSM_STATE_IDENTIFY_TRANSFER:
565
566 status = ace_in32(ace, ACE_STATUS);
567 if (status & ACE_STATUS_CFBSY) {
568 dev_dbg(ace->dev, "CFBSY set; t=%i iter=%i dc=%i\n",
569 ace->fsm_task, ace->fsm_iter_num,
570 ace->data_count);
571 ace_fsm_yield(ace);
572 break;
573 }
574 if (!(status & ACE_STATUS_DATABUFRDY)) {
575 ace_fsm_yield(ace);
576 break;
577 }
578
579
580 ace->reg_ops->datain(ace);
581 ace->data_count--;
582
583
584 if (ace->data_count != 0) {
585 ace_fsm_yieldirq(ace);
586 break;
587 }
588
589
590 dev_dbg(ace->dev, "identify finished\n");
591 ace->fsm_state = ACE_FSM_STATE_IDENTIFY_COMPLETE;
592 break;
593
594 case ACE_FSM_STATE_IDENTIFY_COMPLETE:
595 ace_fix_driveid(&ace->cf_id);
596 ace_dump_mem(&ace->cf_id, 512);
597
598 if (ace->data_result) {
599
600 ace->media_change = 1;
601 set_capacity(ace->gd, 0);
602 dev_err(ace->dev, "error fetching CF id (%i)\n",
603 ace->data_result);
604 } else {
605 ace->media_change = 0;
606
607
608 set_capacity(ace->gd, ace->cf_id.lba_capacity);
609 dev_info(ace->dev, "capacity: %i sectors\n",
610 ace->cf_id.lba_capacity);
611 }
612
613
614 ace->fsm_state = ACE_FSM_STATE_IDLE;
615 ace->id_result = ace->data_result;
616 while (ace->id_req_count) {
617 complete(&ace->id_completion);
618 ace->id_req_count--;
619 }
620 break;
621
622 case ACE_FSM_STATE_REQ_PREPARE:
623 req = ace_get_next_request(ace->queue);
624 if (!req) {
625 ace->fsm_state = ACE_FSM_STATE_IDLE;
626 break;
627 }
628
629
630 dev_dbg(ace->dev,
631 "request: sec=%lx hcnt=%lx, ccnt=%x, dir=%i\n",
632 req->sector, req->hard_nr_sectors,
633 req->current_nr_sectors, rq_data_dir(req));
634
635 ace->req = req;
636 ace->data_ptr = req->buffer;
637 ace->data_count = req->current_nr_sectors * ACE_BUF_PER_SECTOR;
638 ace_out32(ace, ACE_MPULBA, req->sector & 0x0FFFFFFF);
639
640 count = req->hard_nr_sectors;
641 if (rq_data_dir(req)) {
642
643 dev_dbg(ace->dev, "write data\n");
644 ace->fsm_task = ACE_TASK_WRITE;
645 ace_out(ace, ACE_SECCNTCMD,
646 count | ACE_SECCNTCMD_WRITE_DATA);
647 } else {
648
649 dev_dbg(ace->dev, "read data\n");
650 ace->fsm_task = ACE_TASK_READ;
651 ace_out(ace, ACE_SECCNTCMD,
652 count | ACE_SECCNTCMD_READ_DATA);
653 }
654
655
656 val = ace_in(ace, ACE_CTRL);
657 ace_out(ace, ACE_CTRL, val | ACE_CTRL_CFGRESET);
658
659
660
661
662 ace->fsm_state = ACE_FSM_STATE_REQ_TRANSFER;
663 if (ace->fsm_task == ACE_TASK_READ)
664 ace_fsm_yieldirq(ace);
665 break;
666
667 case ACE_FSM_STATE_REQ_TRANSFER:
668
669 status = ace_in32(ace, ACE_STATUS);
670 if (status & ACE_STATUS_CFBSY) {
671 dev_dbg(ace->dev,
672 "CFBSY set; t=%i iter=%i c=%i dc=%i irq=%i\n",
673 ace->fsm_task, ace->fsm_iter_num,
674 ace->req->current_nr_sectors * 16,
675 ace->data_count, ace->in_irq);
676 ace_fsm_yield(ace);
677 break;
678 }
679 if (!(status & ACE_STATUS_DATABUFRDY)) {
680 dev_dbg(ace->dev,
681 "DATABUF not set; t=%i iter=%i c=%i dc=%i irq=%i\n",
682 ace->fsm_task, ace->fsm_iter_num,
683 ace->req->current_nr_sectors * 16,
684 ace->data_count, ace->in_irq);
685 ace_fsm_yieldirq(ace);
686 break;
687 }
688
689
690 if (ace->fsm_task == ACE_TASK_WRITE)
691 ace->reg_ops->dataout(ace);
692 else
693 ace->reg_ops->datain(ace);
694 ace->data_count--;
695
696
697 if (ace->data_count != 0) {
698 ace_fsm_yieldirq(ace);
699 break;
700 }
701
702
703 if (__blk_end_request(ace->req, 0,
704 blk_rq_cur_bytes(ace->req))) {
705
706
707
708
709 ace->data_ptr = ace->req->buffer;
710 ace->data_count = ace->req->current_nr_sectors * 16;
711 ace_fsm_yieldirq(ace);
712 break;
713 }
714
715 ace->fsm_state = ACE_FSM_STATE_REQ_COMPLETE;
716 break;
717
718 case ACE_FSM_STATE_REQ_COMPLETE:
719 ace->req = NULL;
720
721
722 ace->fsm_state = ACE_FSM_STATE_IDLE;
723 break;
724
725 default:
726 ace->fsm_state = ACE_FSM_STATE_IDLE;
727 break;
728 }
729}
730
731static void ace_fsm_tasklet(unsigned long data)
732{
733 struct ace_device *ace = (void *)data;
734 unsigned long flags;
735
736 spin_lock_irqsave(&ace->lock, flags);
737
738
739 ace->fsm_continue_flag = 1;
740 while (ace->fsm_continue_flag)
741 ace_fsm_dostate(ace);
742
743 spin_unlock_irqrestore(&ace->lock, flags);
744}
745
746static void ace_stall_timer(unsigned long data)
747{
748 struct ace_device *ace = (void *)data;
749 unsigned long flags;
750
751 dev_warn(ace->dev,
752 "kicking stalled fsm; state=%i task=%i iter=%i dc=%i\n",
753 ace->fsm_state, ace->fsm_task, ace->fsm_iter_num,
754 ace->data_count);
755 spin_lock_irqsave(&ace->lock, flags);
756
757
758
759 mod_timer(&ace->stall_timer, jiffies + HZ);
760
761
762 ace->fsm_continue_flag = 1;
763 while (ace->fsm_continue_flag)
764 ace_fsm_dostate(ace);
765
766 spin_unlock_irqrestore(&ace->lock, flags);
767}
768
769
770
771
772static int ace_interrupt_checkstate(struct ace_device *ace)
773{
774 u32 sreg = ace_in32(ace, ACE_STATUS);
775 u16 creg = ace_in(ace, ACE_CTRL);
776
777
778 if ((sreg & (ACE_STATUS_CFGERROR | ACE_STATUS_CFCERROR)) &&
779 (creg & ACE_CTRL_ERRORIRQ)) {
780 dev_err(ace->dev, "transfer failure\n");
781 ace_dump_regs(ace);
782 return -EIO;
783 }
784
785 return 0;
786}
787
788static irqreturn_t ace_interrupt(int irq, void *dev_id)
789{
790 u16 creg;
791 struct ace_device *ace = dev_id;
792
793
794 spin_lock(&ace->lock);
795 ace->in_irq = 1;
796
797
798 creg = ace_in(ace, ACE_CTRL);
799 ace_out(ace, ACE_CTRL, creg | ACE_CTRL_RESETIRQ);
800 ace_out(ace, ACE_CTRL, creg);
801
802
803 if (ace_interrupt_checkstate(ace))
804 ace->data_result = -EIO;
805
806 if (ace->fsm_task == 0) {
807 dev_err(ace->dev,
808 "spurious irq; stat=%.8x ctrl=%.8x cmd=%.4x\n",
809 ace_in32(ace, ACE_STATUS), ace_in32(ace, ACE_CTRL),
810 ace_in(ace, ACE_SECCNTCMD));
811 dev_err(ace->dev, "fsm_task=%i fsm_state=%i data_count=%i\n",
812 ace->fsm_task, ace->fsm_state, ace->data_count);
813 }
814
815
816 ace->fsm_continue_flag = 1;
817 while (ace->fsm_continue_flag)
818 ace_fsm_dostate(ace);
819
820
821 ace->in_irq = 0;
822 spin_unlock(&ace->lock);
823
824 return IRQ_HANDLED;
825}
826
827
828
829
830static void ace_request(struct request_queue * q)
831{
832 struct request *req;
833 struct ace_device *ace;
834
835 req = ace_get_next_request(q);
836
837 if (req) {
838 ace = req->rq_disk->private_data;
839 tasklet_schedule(&ace->fsm_tasklet);
840 }
841}
842
843static int ace_media_changed(struct gendisk *gd)
844{
845 struct ace_device *ace = gd->private_data;
846 dev_dbg(ace->dev, "ace_media_changed(): %i\n", ace->media_change);
847
848 return ace->media_change;
849}
850
851static int ace_revalidate_disk(struct gendisk *gd)
852{
853 struct ace_device *ace = gd->private_data;
854 unsigned long flags;
855
856 dev_dbg(ace->dev, "ace_revalidate_disk()\n");
857
858 if (ace->media_change) {
859 dev_dbg(ace->dev, "requesting cf id and scheduling tasklet\n");
860
861 spin_lock_irqsave(&ace->lock, flags);
862 ace->id_req_count++;
863 spin_unlock_irqrestore(&ace->lock, flags);
864
865 tasklet_schedule(&ace->fsm_tasklet);
866 wait_for_completion(&ace->id_completion);
867 }
868
869 dev_dbg(ace->dev, "revalidate complete\n");
870 return ace->id_result;
871}
872
873static int ace_open(struct inode *inode, struct file *filp)
874{
875 struct ace_device *ace = inode->i_bdev->bd_disk->private_data;
876 unsigned long flags;
877
878 dev_dbg(ace->dev, "ace_open() users=%i\n", ace->users + 1);
879
880 filp->private_data = ace;
881 spin_lock_irqsave(&ace->lock, flags);
882 ace->users++;
883 spin_unlock_irqrestore(&ace->lock, flags);
884
885 check_disk_change(inode->i_bdev);
886 return 0;
887}
888
889static int ace_release(struct inode *inode, struct file *filp)
890{
891 struct ace_device *ace = inode->i_bdev->bd_disk->private_data;
892 unsigned long flags;
893 u16 val;
894
895 dev_dbg(ace->dev, "ace_release() users=%i\n", ace->users - 1);
896
897 spin_lock_irqsave(&ace->lock, flags);
898 ace->users--;
899 if (ace->users == 0) {
900 val = ace_in(ace, ACE_CTRL);
901 ace_out(ace, ACE_CTRL, val & ~ACE_CTRL_LOCKREQ);
902 }
903 spin_unlock_irqrestore(&ace->lock, flags);
904 return 0;
905}
906
907static int ace_getgeo(struct block_device *bdev, struct hd_geometry *geo)
908{
909 struct ace_device *ace = bdev->bd_disk->private_data;
910
911 dev_dbg(ace->dev, "ace_getgeo()\n");
912
913 geo->heads = ace->cf_id.heads;
914 geo->sectors = ace->cf_id.sectors;
915 geo->cylinders = ace->cf_id.cyls;
916
917 return 0;
918}
919
920static struct block_device_operations ace_fops = {
921 .owner = THIS_MODULE,
922 .open = ace_open,
923 .release = ace_release,
924 .media_changed = ace_media_changed,
925 .revalidate_disk = ace_revalidate_disk,
926 .getgeo = ace_getgeo,
927};
928
929
930
931
932static int __devinit ace_setup(struct ace_device *ace)
933{
934 u16 version;
935 u16 val;
936 int rc;
937
938 dev_dbg(ace->dev, "ace_setup(ace=0x%p)\n", ace);
939 dev_dbg(ace->dev, "physaddr=0x%lx irq=%i\n", ace->physaddr, ace->irq);
940
941 spin_lock_init(&ace->lock);
942 init_completion(&ace->id_completion);
943
944
945
946
947 ace->baseaddr = ioremap(ace->physaddr, 0x80);
948 if (!ace->baseaddr)
949 goto err_ioremap;
950
951
952
953
954 tasklet_init(&ace->fsm_tasklet, ace_fsm_tasklet, (unsigned long)ace);
955 setup_timer(&ace->stall_timer, ace_stall_timer, (unsigned long)ace);
956
957
958
959
960 ace->queue = blk_init_queue(ace_request, &ace->lock);
961 if (ace->queue == NULL)
962 goto err_blk_initq;
963 blk_queue_hardsect_size(ace->queue, 512);
964
965
966
967
968 ace->gd = alloc_disk(ACE_NUM_MINORS);
969 if (!ace->gd)
970 goto err_alloc_disk;
971
972 ace->gd->major = ace_major;
973 ace->gd->first_minor = ace->id * ACE_NUM_MINORS;
974 ace->gd->fops = &ace_fops;
975 ace->gd->queue = ace->queue;
976 ace->gd->private_data = ace;
977 snprintf(ace->gd->disk_name, 32, "xs%c", ace->id + 'a');
978
979
980 if (ace->bus_width == ACE_BUS_WIDTH_16) {
981
982 ace_out_le16(ace, ACE_BUSMODE, 0x0101);
983
984
985 if (ace_in_le16(ace, ACE_BUSMODE) == 0x0001)
986 ace->reg_ops = &ace_reg_le16_ops;
987 else
988 ace->reg_ops = &ace_reg_be16_ops;
989 } else {
990 ace_out_8(ace, ACE_BUSMODE, 0x00);
991 ace->reg_ops = &ace_reg_8_ops;
992 }
993
994
995 version = ace_in(ace, ACE_VERSION);
996 if ((version == 0) || (version == 0xFFFF))
997 goto err_read;
998
999
1000 ace_out(ace, ACE_CTRL, ACE_CTRL_FORCECFGMODE |
1001 ACE_CTRL_DATABUFRDYIRQ | ACE_CTRL_ERRORIRQ);
1002
1003
1004 if (ace->irq != NO_IRQ) {
1005 rc = request_irq(ace->irq, ace_interrupt, 0, "systemace", ace);
1006 if (rc) {
1007
1008 dev_err(ace->dev, "request_irq failed\n");
1009 ace->irq = NO_IRQ;
1010 }
1011 }
1012
1013
1014 val = ace_in(ace, ACE_CTRL);
1015 val |= ACE_CTRL_DATABUFRDYIRQ | ACE_CTRL_ERRORIRQ;
1016 ace_out(ace, ACE_CTRL, val);
1017
1018
1019 dev_info(ace->dev, "Xilinx SystemACE revision %i.%i.%i\n",
1020 (version >> 12) & 0xf, (version >> 8) & 0x0f, version & 0xff);
1021 dev_dbg(ace->dev, "physaddr 0x%lx, mapped to 0x%p, irq=%i\n",
1022 ace->physaddr, ace->baseaddr, ace->irq);
1023
1024 ace->media_change = 1;
1025 ace_revalidate_disk(ace->gd);
1026
1027
1028 add_disk(ace->gd);
1029
1030 return 0;
1031
1032err_read:
1033 put_disk(ace->gd);
1034err_alloc_disk:
1035 blk_cleanup_queue(ace->queue);
1036err_blk_initq:
1037 iounmap(ace->baseaddr);
1038err_ioremap:
1039 dev_info(ace->dev, "xsysace: error initializing device at 0x%lx\n",
1040 ace->physaddr);
1041 return -ENOMEM;
1042}
1043
1044static void __devexit ace_teardown(struct ace_device *ace)
1045{
1046 if (ace->gd) {
1047 del_gendisk(ace->gd);
1048 put_disk(ace->gd);
1049 }
1050
1051 if (ace->queue)
1052 blk_cleanup_queue(ace->queue);
1053
1054 tasklet_kill(&ace->fsm_tasklet);
1055
1056 if (ace->irq != NO_IRQ)
1057 free_irq(ace->irq, ace);
1058
1059 iounmap(ace->baseaddr);
1060}
1061
1062static int __devinit
1063ace_alloc(struct device *dev, int id, unsigned long physaddr,
1064 int irq, int bus_width)
1065{
1066 struct ace_device *ace;
1067 int rc;
1068 dev_dbg(dev, "ace_alloc(%p)\n", dev);
1069
1070 if (!physaddr) {
1071 rc = -ENODEV;
1072 goto err_noreg;
1073 }
1074
1075
1076 ace = kzalloc(sizeof(struct ace_device), GFP_KERNEL);
1077 if (!ace) {
1078 rc = -ENOMEM;
1079 goto err_alloc;
1080 }
1081
1082 ace->dev = dev;
1083 ace->id = id;
1084 ace->physaddr = physaddr;
1085 ace->irq = irq;
1086 ace->bus_width = bus_width;
1087
1088
1089 rc = ace_setup(ace);
1090 if (rc)
1091 goto err_setup;
1092
1093 dev_set_drvdata(dev, ace);
1094 return 0;
1095
1096err_setup:
1097 dev_set_drvdata(dev, NULL);
1098 kfree(ace);
1099err_alloc:
1100err_noreg:
1101 dev_err(dev, "could not initialize device, err=%i\n", rc);
1102 return rc;
1103}
1104
1105static void __devexit ace_free(struct device *dev)
1106{
1107 struct ace_device *ace = dev_get_drvdata(dev);
1108 dev_dbg(dev, "ace_free(%p)\n", dev);
1109
1110 if (ace) {
1111 ace_teardown(ace);
1112 dev_set_drvdata(dev, NULL);
1113 kfree(ace);
1114 }
1115}
1116
1117
1118
1119
1120
1121static int __devinit ace_probe(struct platform_device *dev)
1122{
1123 unsigned long physaddr = 0;
1124 int bus_width = ACE_BUS_WIDTH_16;
1125 int id = dev->id;
1126 int irq = NO_IRQ;
1127 int i;
1128
1129 dev_dbg(&dev->dev, "ace_probe(%p)\n", dev);
1130
1131 for (i = 0; i < dev->num_resources; i++) {
1132 if (dev->resource[i].flags & IORESOURCE_MEM)
1133 physaddr = dev->resource[i].start;
1134 if (dev->resource[i].flags & IORESOURCE_IRQ)
1135 irq = dev->resource[i].start;
1136 }
1137
1138
1139 return ace_alloc(&dev->dev, id, physaddr, irq, bus_width);
1140}
1141
1142
1143
1144
1145static int __devexit ace_remove(struct platform_device *dev)
1146{
1147 ace_free(&dev->dev);
1148 return 0;
1149}
1150
1151static struct platform_driver ace_platform_driver = {
1152 .probe = ace_probe,
1153 .remove = __devexit_p(ace_remove),
1154 .driver = {
1155 .owner = THIS_MODULE,
1156 .name = "xsysace",
1157 },
1158};
1159
1160
1161
1162
1163
1164#if defined(CONFIG_OF)
1165static int __devinit
1166ace_of_probe(struct of_device *op, const struct of_device_id *match)
1167{
1168 struct resource res;
1169 unsigned long physaddr;
1170 const u32 *id;
1171 int irq, bus_width, rc;
1172
1173 dev_dbg(&op->dev, "ace_of_probe(%p, %p)\n", op, match);
1174
1175
1176 id = of_get_property(op->node, "port-number", NULL);
1177
1178
1179 rc = of_address_to_resource(op->node, 0, &res);
1180 if (rc) {
1181 dev_err(&op->dev, "invalid address\n");
1182 return rc;
1183 }
1184 physaddr = res.start;
1185
1186
1187 irq = irq_of_parse_and_map(op->node, 0);
1188
1189
1190 bus_width = ACE_BUS_WIDTH_16;
1191 if (of_find_property(op->node, "8-bit", NULL))
1192 bus_width = ACE_BUS_WIDTH_8;
1193
1194
1195 return ace_alloc(&op->dev, id ? *id : 0, physaddr, irq, bus_width);
1196}
1197
1198static int __devexit ace_of_remove(struct of_device *op)
1199{
1200 ace_free(&op->dev);
1201 return 0;
1202}
1203
1204
1205static struct of_device_id ace_of_match[] __devinitdata = {
1206 { .compatible = "xlnx,opb-sysace-1.00.b", },
1207 { .compatible = "xlnx,opb-sysace-1.00.c", },
1208 { .compatible = "xlnx,xps-sysace-1.00.a", },
1209 {},
1210};
1211MODULE_DEVICE_TABLE(of, ace_of_match);
1212
1213static struct of_platform_driver ace_of_driver = {
1214 .owner = THIS_MODULE,
1215 .name = "xsysace",
1216 .match_table = ace_of_match,
1217 .probe = ace_of_probe,
1218 .remove = __devexit_p(ace_of_remove),
1219 .driver = {
1220 .name = "xsysace",
1221 },
1222};
1223
1224
1225static inline int __init ace_of_register(void)
1226{
1227 pr_debug("xsysace: registering OF binding\n");
1228 return of_register_platform_driver(&ace_of_driver);
1229}
1230
1231static inline void __exit ace_of_unregister(void)
1232{
1233 of_unregister_platform_driver(&ace_of_driver);
1234}
1235#else
1236
1237static inline int __init ace_of_register(void) { return 0; }
1238static inline void __exit ace_of_unregister(void) { }
1239#endif
1240
1241
1242
1243
1244static int __init ace_init(void)
1245{
1246 int rc;
1247
1248 ace_major = register_blkdev(ace_major, "xsysace");
1249 if (ace_major <= 0) {
1250 rc = -ENOMEM;
1251 goto err_blk;
1252 }
1253
1254 rc = ace_of_register();
1255 if (rc)
1256 goto err_of;
1257
1258 pr_debug("xsysace: registering platform binding\n");
1259 rc = platform_driver_register(&ace_platform_driver);
1260 if (rc)
1261 goto err_plat;
1262
1263 pr_info("Xilinx SystemACE device driver, major=%i\n", ace_major);
1264 return 0;
1265
1266err_plat:
1267 ace_of_unregister();
1268err_of:
1269 unregister_blkdev(ace_major, "xsysace");
1270err_blk:
1271 printk(KERN_ERR "xsysace: registration failed; err=%i\n", rc);
1272 return rc;
1273}
1274
1275static void __exit ace_exit(void)
1276{
1277 pr_debug("Unregistering Xilinx SystemACE driver\n");
1278 platform_driver_unregister(&ace_platform_driver);
1279 ace_of_unregister();
1280 unregister_blkdev(ace_major, "xsysace");
1281}
1282
1283module_init(ace_init);
1284module_exit(ace_exit);
1285