2 * linux/drivers/ide/arm/icside.c
4 * Copyright (c) 1996-2004 Russell King.
6 * Please note that this platform does not support 32-bit IDE IO.
9 #include <linux/string.h>
10 #include <linux/module.h>
11 #include <linux/ioport.h>
12 #include <linux/slab.h>
13 #include <linux/blkdev.h>
14 #include <linux/errno.h>
15 #include <linux/hdreg.h>
16 #include <linux/ide.h>
17 #include <linux/dma-mapping.h>
18 #include <linux/device.h>
19 #include <linux/init.h>
20 #include <linux/scatterlist.h>
23 #include <asm/ecard.h>
26 #define ICS_IDENT_OFFSET 0x2280
28 #define ICS_ARCIN_V5_INTRSTAT 0x0000
29 #define ICS_ARCIN_V5_INTROFFSET 0x0004
30 #define ICS_ARCIN_V5_IDEOFFSET 0x2800
31 #define ICS_ARCIN_V5_IDEALTOFFSET 0x2b80
32 #define ICS_ARCIN_V5_IDESTEPPING 6
34 #define ICS_ARCIN_V6_IDEOFFSET_1 0x2000
35 #define ICS_ARCIN_V6_INTROFFSET_1 0x2200
36 #define ICS_ARCIN_V6_INTRSTAT_1 0x2290
37 #define ICS_ARCIN_V6_IDEALTOFFSET_1 0x2380
38 #define ICS_ARCIN_V6_IDEOFFSET_2 0x3000
39 #define ICS_ARCIN_V6_INTROFFSET_2 0x3200
40 #define ICS_ARCIN_V6_INTRSTAT_2 0x3290
41 #define ICS_ARCIN_V6_IDEALTOFFSET_2 0x3380
42 #define ICS_ARCIN_V6_IDESTEPPING 6
45 unsigned int dataoffset;
46 unsigned int ctrloffset;
47 unsigned int stepping;
50 static struct cardinfo icside_cardinfo_v5 = {
51 .dataoffset = ICS_ARCIN_V5_IDEOFFSET,
52 .ctrloffset = ICS_ARCIN_V5_IDEALTOFFSET,
53 .stepping = ICS_ARCIN_V5_IDESTEPPING,
56 static struct cardinfo icside_cardinfo_v6_1 = {
57 .dataoffset = ICS_ARCIN_V6_IDEOFFSET_1,
58 .ctrloffset = ICS_ARCIN_V6_IDEALTOFFSET_1,
59 .stepping = ICS_ARCIN_V6_IDESTEPPING,
62 static struct cardinfo icside_cardinfo_v6_2 = {
63 .dataoffset = ICS_ARCIN_V6_IDEOFFSET_2,
64 .ctrloffset = ICS_ARCIN_V6_IDEALTOFFSET_2,
65 .stepping = ICS_ARCIN_V6_IDESTEPPING,
71 void __iomem *irq_port;
72 void __iomem *ioc_base;
74 /* parent device... until the IDE core gets one of its own */
79 #define ICS_TYPE_A3IN 0
80 #define ICS_TYPE_A3USER 1
82 #define ICS_TYPE_V5 15
83 #define ICS_TYPE_NOTYPE ((unsigned int)-1)
85 /* ---------------- Version 5 PCB Support Functions --------------------- */
86 /* Prototype: icside_irqenable_arcin_v5 (struct expansion_card *ec, int irqnr)
87 * Purpose : enable interrupts from card
89 static void icside_irqenable_arcin_v5 (struct expansion_card *ec, int irqnr)
91 struct icside_state *state = ec->irq_data;
93 writeb(0, state->irq_port + ICS_ARCIN_V5_INTROFFSET);
96 /* Prototype: icside_irqdisable_arcin_v5 (struct expansion_card *ec, int irqnr)
97 * Purpose : disable interrupts from card
99 static void icside_irqdisable_arcin_v5 (struct expansion_card *ec, int irqnr)
101 struct icside_state *state = ec->irq_data;
103 readb(state->irq_port + ICS_ARCIN_V5_INTROFFSET);
106 static const expansioncard_ops_t icside_ops_arcin_v5 = {
107 .irqenable = icside_irqenable_arcin_v5,
108 .irqdisable = icside_irqdisable_arcin_v5,
112 /* ---------------- Version 6 PCB Support Functions --------------------- */
113 /* Prototype: icside_irqenable_arcin_v6 (struct expansion_card *ec, int irqnr)
114 * Purpose : enable interrupts from card
116 static void icside_irqenable_arcin_v6 (struct expansion_card *ec, int irqnr)
118 struct icside_state *state = ec->irq_data;
119 void __iomem *base = state->irq_port;
123 switch (state->channel) {
125 writeb(0, base + ICS_ARCIN_V6_INTROFFSET_1);
126 readb(base + ICS_ARCIN_V6_INTROFFSET_2);
129 writeb(0, base + ICS_ARCIN_V6_INTROFFSET_2);
130 readb(base + ICS_ARCIN_V6_INTROFFSET_1);
135 /* Prototype: icside_irqdisable_arcin_v6 (struct expansion_card *ec, int irqnr)
136 * Purpose : disable interrupts from card
138 static void icside_irqdisable_arcin_v6 (struct expansion_card *ec, int irqnr)
140 struct icside_state *state = ec->irq_data;
144 readb(state->irq_port + ICS_ARCIN_V6_INTROFFSET_1);
145 readb(state->irq_port + ICS_ARCIN_V6_INTROFFSET_2);
148 /* Prototype: icside_irqprobe(struct expansion_card *ec)
149 * Purpose : detect an active interrupt from card
151 static int icside_irqpending_arcin_v6(struct expansion_card *ec)
153 struct icside_state *state = ec->irq_data;
155 return readb(state->irq_port + ICS_ARCIN_V6_INTRSTAT_1) & 1 ||
156 readb(state->irq_port + ICS_ARCIN_V6_INTRSTAT_2) & 1;
159 static const expansioncard_ops_t icside_ops_arcin_v6 = {
160 .irqenable = icside_irqenable_arcin_v6,
161 .irqdisable = icside_irqdisable_arcin_v6,
162 .irqpending = icside_irqpending_arcin_v6,
166 * Handle routing of interrupts. This is called before
167 * we write the command to the drive.
169 static void icside_maskproc(ide_drive_t *drive, int mask)
171 ide_hwif_t *hwif = HWIF(drive);
172 struct icside_state *state = hwif->hwif_data;
175 local_irq_save(flags);
177 state->channel = hwif->channel;
179 if (state->enabled && !mask) {
180 switch (hwif->channel) {
182 writeb(0, state->irq_port + ICS_ARCIN_V6_INTROFFSET_1);
183 readb(state->irq_port + ICS_ARCIN_V6_INTROFFSET_2);
186 writeb(0, state->irq_port + ICS_ARCIN_V6_INTROFFSET_2);
187 readb(state->irq_port + ICS_ARCIN_V6_INTROFFSET_1);
191 readb(state->irq_port + ICS_ARCIN_V6_INTROFFSET_2);
192 readb(state->irq_port + ICS_ARCIN_V6_INTROFFSET_1);
195 local_irq_restore(flags);
198 #ifdef CONFIG_BLK_DEV_IDEDMA_ICS
200 #ifndef CONFIG_IDEDMA_ICS_AUTO
201 #warning CONFIG_IDEDMA_ICS_AUTO=n support is obsolete, and will be removed soon.
207 * Similar to the BM-DMA, but we use the RiscPCs IOMD DMA controllers.
208 * There is only one DMA controller per card, which means that only
209 * one drive can be accessed at one time. NOTE! We do not enforce that
210 * here, but we rely on the main IDE driver spotting that both
211 * interfaces use the same IRQ, which should guarantee this.
214 static void icside_build_sglist(ide_drive_t *drive, struct request *rq)
216 ide_hwif_t *hwif = drive->hwif;
217 struct icside_state *state = hwif->hwif_data;
218 struct scatterlist *sg = hwif->sg_table;
220 ide_map_sg(drive, rq);
222 if (rq_data_dir(rq) == READ)
223 hwif->sg_dma_direction = DMA_FROM_DEVICE;
225 hwif->sg_dma_direction = DMA_TO_DEVICE;
227 hwif->sg_nents = dma_map_sg(state->dev, sg, hwif->sg_nents,
228 hwif->sg_dma_direction);
232 * Configure the IOMD to give the appropriate timings for the transfer
233 * mode being requested. We take the advice of the ATA standards, and
234 * calculate the cycle time based on the transfer mode, and the EIDE
235 * MW DMA specs that the drive provides in the IDENTIFY command.
237 * We have the following IOMD DMA modes to choose from:
239 * Type Active Recovery Cycle
240 * A 250 (250) 312 (550) 562 (800)
242 * C 125 (125) 125 (375) 250 (500)
245 * (figures in brackets are actual measured timings)
247 * However, we also need to take care of the read/write active and
251 * Mode Active -- Recovery -- Cycle IOMD type
252 * MW0 215 50 215 480 A
256 static int icside_set_speed(ide_drive_t *drive, u8 xfer_mode)
258 int on = 0, cycle_time = 0, use_dma_info = 0;
261 * Limit the transfer speed to MW_DMA_2.
263 if (xfer_mode > XFER_MW_DMA_2)
264 xfer_mode = XFER_MW_DMA_2;
289 * If we're going to be doing MW_DMA_1 or MW_DMA_2, we should
290 * take care to note the values in the ID...
292 if (use_dma_info && drive->id->eide_dma_time > cycle_time)
293 cycle_time = drive->id->eide_dma_time;
295 drive->drive_data = cycle_time;
297 if (cycle_time && ide_config_drive_speed(drive, xfer_mode) == 0)
300 drive->drive_data = 480;
302 printk("%s: %s selected (peak %dMB/s)\n", drive->name,
303 ide_xfer_verbose(xfer_mode), 2000 / drive->drive_data);
305 drive->current_speed = xfer_mode;
310 static int icside_dma_host_off(ide_drive_t *drive)
315 static int icside_dma_off_quietly(ide_drive_t *drive)
317 drive->using_dma = 0;
318 return icside_dma_host_off(drive);
321 static int icside_dma_host_on(ide_drive_t *drive)
326 static int icside_dma_on(ide_drive_t *drive)
328 drive->using_dma = 1;
329 return icside_dma_host_on(drive);
332 static int icside_dma_check(ide_drive_t *drive)
334 struct hd_driveid *id = drive->id;
335 ide_hwif_t *hwif = HWIF(drive);
336 int xfer_mode = XFER_PIO_2;
339 if (!(id->capability & 1) || !hwif->autodma)
343 * Consult the list of known "bad" drives
345 if (__ide_dma_bad_drive(drive))
349 * Enable DMA on any drive that has multiword DMA
351 if (id->field_valid & 2) {
352 xfer_mode = ide_dma_speed(drive, 0);
357 * Consult the list of known "good" drives
359 if (__ide_dma_good_drive(drive)) {
360 if (id->eide_dma_time > 150)
362 xfer_mode = XFER_MW_DMA_1;
366 on = icside_set_speed(drive, xfer_mode);
369 return icside_dma_on(drive);
371 return icside_dma_off_quietly(drive);
374 static int icside_dma_end(ide_drive_t *drive)
376 ide_hwif_t *hwif = HWIF(drive);
377 struct icside_state *state = hwif->hwif_data;
379 drive->waiting_for_dma = 0;
381 disable_dma(hwif->hw.dma);
383 /* Teardown mappings after DMA has completed. */
384 dma_unmap_sg(state->dev, hwif->sg_table, hwif->sg_nents,
385 hwif->sg_dma_direction);
387 return get_dma_residue(hwif->hw.dma) != 0;
390 static void icside_dma_start(ide_drive_t *drive)
392 ide_hwif_t *hwif = HWIF(drive);
394 /* We can not enable DMA on both channels simultaneously. */
395 BUG_ON(dma_channel_active(hwif->hw.dma));
396 enable_dma(hwif->hw.dma);
399 static int icside_dma_setup(ide_drive_t *drive)
401 ide_hwif_t *hwif = HWIF(drive);
402 struct request *rq = hwif->hwgroup->rq;
403 unsigned int dma_mode;
406 dma_mode = DMA_MODE_WRITE;
408 dma_mode = DMA_MODE_READ;
411 * We can not enable DMA on both channels.
413 BUG_ON(dma_channel_active(hwif->hw.dma));
415 icside_build_sglist(drive, rq);
418 * Ensure that we have the right interrupt routed.
420 icside_maskproc(drive, 0);
423 * Route the DMA signals to the correct interface.
425 writeb(hwif->select_data, hwif->config_data);
428 * Select the correct timing for this drive.
430 set_dma_speed(hwif->hw.dma, drive->drive_data);
433 * Tell the DMA engine about the SG table and
436 set_dma_sg(hwif->hw.dma, hwif->sg_table, hwif->sg_nents);
437 set_dma_mode(hwif->hw.dma, dma_mode);
439 drive->waiting_for_dma = 1;
444 static void icside_dma_exec_cmd(ide_drive_t *drive, u8 cmd)
446 /* issue cmd to drive */
447 ide_execute_command(drive, cmd, ide_dma_intr, 2 * WAIT_CMD, NULL);
450 static int icside_dma_test_irq(ide_drive_t *drive)
452 ide_hwif_t *hwif = HWIF(drive);
453 struct icside_state *state = hwif->hwif_data;
455 return readb(state->irq_port +
457 ICS_ARCIN_V6_INTRSTAT_2 :
458 ICS_ARCIN_V6_INTRSTAT_1)) & 1;
461 static int icside_dma_timeout(ide_drive_t *drive)
463 printk(KERN_ERR "%s: DMA timeout occurred: ", drive->name);
465 if (icside_dma_test_irq(drive))
468 ide_dump_status(drive, "DMA timeout",
469 HWIF(drive)->INB(IDE_STATUS_REG));
471 return icside_dma_end(drive);
474 static int icside_dma_lostirq(ide_drive_t *drive)
476 printk(KERN_ERR "%s: IRQ lost\n", drive->name);
480 static void icside_dma_init(ide_hwif_t *hwif)
484 #ifdef CONFIG_IDEDMA_ICS_AUTO
488 printk(" %s: SG-DMA", hwif->name);
491 hwif->mwdma_mask = 7; /* MW0..2 */
492 hwif->swdma_mask = 7; /* SW0..2 */
494 hwif->dmatable_cpu = NULL;
495 hwif->dmatable_dma = 0;
496 hwif->speedproc = icside_set_speed;
497 hwif->autodma = autodma;
499 hwif->ide_dma_check = icside_dma_check;
500 hwif->ide_dma_host_off = icside_dma_host_off;
501 hwif->ide_dma_off_quietly = icside_dma_off_quietly;
502 hwif->ide_dma_host_on = icside_dma_host_on;
503 hwif->ide_dma_on = icside_dma_on;
504 hwif->dma_setup = icside_dma_setup;
505 hwif->dma_exec_cmd = icside_dma_exec_cmd;
506 hwif->dma_start = icside_dma_start;
507 hwif->ide_dma_end = icside_dma_end;
508 hwif->ide_dma_test_irq = icside_dma_test_irq;
509 hwif->ide_dma_timeout = icside_dma_timeout;
510 hwif->ide_dma_lostirq = icside_dma_lostirq;
512 hwif->drives[0].autodma = hwif->autodma;
513 hwif->drives[1].autodma = hwif->autodma;
515 printk(" capable%s\n", hwif->autodma ? ", auto-enable" : "");
518 #define icside_dma_init(hwif) (0)
521 static ide_hwif_t *icside_find_hwif(unsigned long dataport)
526 for (index = 0; index < MAX_HWIFS; ++index) {
527 hwif = &ide_hwifs[index];
528 if (hwif->io_ports[IDE_DATA_OFFSET] == dataport)
532 for (index = 0; index < MAX_HWIFS; ++index) {
533 hwif = &ide_hwifs[index];
534 if (!hwif->io_ports[IDE_DATA_OFFSET])
544 icside_setup(void __iomem *base, struct cardinfo *info, struct expansion_card *ec)
546 unsigned long port = (unsigned long)base + info->dataoffset;
549 hwif = icside_find_hwif(port);
553 memset(&hwif->hw, 0, sizeof(hw_regs_t));
556 * Ensure we're using MMIO
558 default_hwif_mmiops(hwif);
561 for (i = IDE_DATA_OFFSET; i <= IDE_STATUS_OFFSET; i++) {
562 hwif->hw.io_ports[i] = port;
563 hwif->io_ports[i] = port;
564 port += 1 << info->stepping;
566 hwif->hw.io_ports[IDE_CONTROL_OFFSET] = (unsigned long)base + info->ctrloffset;
567 hwif->io_ports[IDE_CONTROL_OFFSET] = (unsigned long)base + info->ctrloffset;
568 hwif->hw.irq = ec->irq;
571 hwif->chipset = ide_acorn;
572 hwif->gendev.parent = &ec->dev;
579 icside_register_v5(struct icside_state *state, struct expansion_card *ec)
584 base = ioremap(ecard_resource_start(ec, ECARD_RES_MEMC),
585 ecard_resource_len(ec, ECARD_RES_MEMC));
589 state->irq_port = base;
591 ec->irqaddr = base + ICS_ARCIN_V5_INTRSTAT;
593 ec->irq_data = state;
594 ec->ops = &icside_ops_arcin_v5;
597 * Be on the safe side - disable interrupts
599 icside_irqdisable_arcin_v5(ec, 0);
601 hwif = icside_setup(base, &icside_cardinfo_v5, ec);
607 state->hwif[0] = hwif;
609 probe_hwif_init(hwif);
610 create_proc_ide_interfaces();
616 icside_register_v6(struct icside_state *state, struct expansion_card *ec)
618 ide_hwif_t *hwif, *mate;
619 void __iomem *ioc_base, *easi_base;
620 unsigned int sel = 0;
623 ioc_base = ioremap(ecard_resource_start(ec, ECARD_RES_IOCFAST),
624 ecard_resource_len(ec, ECARD_RES_IOCFAST));
630 easi_base = ioc_base;
632 if (ecard_resource_flags(ec, ECARD_RES_EASI)) {
633 easi_base = ioremap(ecard_resource_start(ec, ECARD_RES_EASI),
634 ecard_resource_len(ec, ECARD_RES_EASI));
641 * Enable access to the EASI region.
646 writeb(sel, ioc_base);
648 ec->irq_data = state;
649 ec->ops = &icside_ops_arcin_v6;
651 state->irq_port = easi_base;
652 state->ioc_base = ioc_base;
655 * Be on the safe side - disable interrupts
657 icside_irqdisable_arcin_v6(ec, 0);
660 * Find and register the interfaces.
662 hwif = icside_setup(easi_base, &icside_cardinfo_v6_1, ec);
663 mate = icside_setup(easi_base, &icside_cardinfo_v6_2, ec);
665 if (!hwif || !mate) {
670 state->hwif[0] = hwif;
671 state->hwif[1] = mate;
673 hwif->maskproc = icside_maskproc;
675 hwif->hwif_data = state;
677 hwif->serialized = 1;
678 hwif->config_data = (unsigned long)ioc_base;
679 hwif->select_data = sel;
680 hwif->hw.dma = ec->dma;
682 mate->maskproc = icside_maskproc;
684 mate->hwif_data = state;
686 mate->serialized = 1;
687 mate->config_data = (unsigned long)ioc_base;
688 mate->select_data = sel | 1;
689 mate->hw.dma = ec->dma;
691 if (ec->dma != NO_DMA && !request_dma(ec->dma, hwif->name)) {
692 icside_dma_init(hwif);
693 icside_dma_init(mate);
696 probe_hwif_init(hwif);
697 probe_hwif_init(mate);
698 create_proc_ide_interfaces();
703 if (easi_base != ioc_base)
712 icside_probe(struct expansion_card *ec, const struct ecard_id *id)
714 struct icside_state *state;
718 ret = ecard_request_resources(ec);
722 state = kmalloc(sizeof(struct icside_state), GFP_KERNEL);
728 memset(state, 0, sizeof(state));
729 state->type = ICS_TYPE_NOTYPE;
730 state->dev = &ec->dev;
732 idmem = ioremap(ecard_resource_start(ec, ECARD_RES_IOCFAST),
733 ecard_resource_len(ec, ECARD_RES_IOCFAST));
737 type = readb(idmem + ICS_IDENT_OFFSET) & 1;
738 type |= (readb(idmem + ICS_IDENT_OFFSET + 4) & 1) << 1;
739 type |= (readb(idmem + ICS_IDENT_OFFSET + 8) & 1) << 2;
740 type |= (readb(idmem + ICS_IDENT_OFFSET + 12) & 1) << 3;
746 switch (state->type) {
748 dev_warn(&ec->dev, "A3IN unsupported\n");
752 case ICS_TYPE_A3USER:
753 dev_warn(&ec->dev, "A3USER unsupported\n");
758 ret = icside_register_v5(state, ec);
762 ret = icside_register_v6(state, ec);
766 dev_warn(&ec->dev, "unknown interface type\n");
772 ecard_set_drvdata(ec, state);
778 ecard_release_resources(ec);
783 static void __devexit icside_remove(struct expansion_card *ec)
785 struct icside_state *state = ecard_get_drvdata(ec);
787 switch (state->type) {
789 /* FIXME: tell IDE to stop using the interface */
791 /* Disable interrupts */
792 icside_irqdisable_arcin_v5(ec, 0);
796 /* FIXME: tell IDE to stop using the interface */
797 if (ec->dma != NO_DMA)
800 /* Disable interrupts */
801 icside_irqdisable_arcin_v6(ec, 0);
803 /* Reset the ROM pointer/EASI selection */
804 writeb(0, state->ioc_base);
808 ecard_set_drvdata(ec, NULL);
813 iounmap(state->ioc_base);
814 if (state->ioc_base != state->irq_port)
815 iounmap(state->irq_port);
818 ecard_release_resources(ec);
821 static void icside_shutdown(struct expansion_card *ec)
823 struct icside_state *state = ecard_get_drvdata(ec);
827 * Disable interrupts from this card. We need to do
828 * this before disabling EASI since we may be accessing
829 * this register via that region.
831 local_irq_save(flags);
832 ec->ops->irqdisable(ec, 0);
833 local_irq_restore(flags);
836 * Reset the ROM pointer so that we can read the ROM
837 * after a soft reboot. This also disables access to
838 * the IDE taskfile via the EASI region.
841 writeb(0, state->ioc_base);
844 static const struct ecard_id icside_ids[] = {
845 { MANU_ICS, PROD_ICS_IDE },
846 { MANU_ICS2, PROD_ICS2_IDE },
850 static struct ecard_driver icside_driver = {
851 .probe = icside_probe,
852 .remove = __devexit_p(icside_remove),
853 .shutdown = icside_shutdown,
854 .id_table = icside_ids,
860 static int __init icside_init(void)
862 return ecard_register_driver(&icside_driver);
865 MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>");
866 MODULE_LICENSE("GPL");
867 MODULE_DESCRIPTION("ICS IDE driver");
869 module_init(icside_init);