1 #include <linux/kernel.h>
2 #include <linux/module.h>
3 #include <linux/init.h>
4 #include <linux/blkdev.h>
5 #include <scsi/scsi_host.h>
7 #include <linux/libata.h>
10 #include <asm/ecard.h>
12 #define DRV_NAME "pata_icside"
14 #define ICS_IDENT_OFFSET 0x2280
16 #define ICS_ARCIN_V5_INTRSTAT 0x0000
17 #define ICS_ARCIN_V5_INTROFFSET 0x0004
19 #define ICS_ARCIN_V6_INTROFFSET_1 0x2200
20 #define ICS_ARCIN_V6_INTRSTAT_1 0x2290
21 #define ICS_ARCIN_V6_INTROFFSET_2 0x3200
22 #define ICS_ARCIN_V6_INTRSTAT_2 0x3290
25 unsigned int dataoffset;
26 unsigned int ctrloffset;
27 unsigned int stepping;
30 static const struct portinfo pata_icside_portinfo_v5 = {
36 static const struct portinfo pata_icside_portinfo_v6_1 = {
42 static const struct portinfo pata_icside_portinfo_v6_2 = {
48 #define PATA_ICSIDE_MAX_SG 128
50 struct pata_icside_state {
51 void __iomem *irq_port;
52 void __iomem *ioc_base;
58 unsigned int speed[ATA_MAX_DEVICES];
60 struct scatterlist sg[PATA_ICSIDE_MAX_SG];
63 struct pata_icside_info {
64 struct pata_icside_state *state;
65 struct expansion_card *ec;
67 void __iomem *irqaddr;
69 const expansioncard_ops_t *irqops;
70 unsigned int mwdma_mask;
71 unsigned int nr_ports;
72 const struct portinfo *port[2];
73 unsigned long raw_base;
74 unsigned long raw_ioc_base;
77 #define ICS_TYPE_A3IN 0
78 #define ICS_TYPE_A3USER 1
80 #define ICS_TYPE_V5 15
81 #define ICS_TYPE_NOTYPE ((unsigned int)-1)
83 /* ---------------- Version 5 PCB Support Functions --------------------- */
84 /* Prototype: pata_icside_irqenable_arcin_v5 (struct expansion_card *ec, int irqnr)
85 * Purpose : enable interrupts from card
87 static void pata_icside_irqenable_arcin_v5 (struct expansion_card *ec, int irqnr)
89 struct pata_icside_state *state = ec->irq_data;
91 writeb(0, state->irq_port + ICS_ARCIN_V5_INTROFFSET);
94 /* Prototype: pata_icside_irqdisable_arcin_v5 (struct expansion_card *ec, int irqnr)
95 * Purpose : disable interrupts from card
97 static void pata_icside_irqdisable_arcin_v5 (struct expansion_card *ec, int irqnr)
99 struct pata_icside_state *state = ec->irq_data;
101 readb(state->irq_port + ICS_ARCIN_V5_INTROFFSET);
104 static const expansioncard_ops_t pata_icside_ops_arcin_v5 = {
105 .irqenable = pata_icside_irqenable_arcin_v5,
106 .irqdisable = pata_icside_irqdisable_arcin_v5,
110 /* ---------------- Version 6 PCB Support Functions --------------------- */
111 /* Prototype: pata_icside_irqenable_arcin_v6 (struct expansion_card *ec, int irqnr)
112 * Purpose : enable interrupts from card
114 static void pata_icside_irqenable_arcin_v6 (struct expansion_card *ec, int irqnr)
116 struct pata_icside_state *state = ec->irq_data;
117 void __iomem *base = state->irq_port;
119 if (!state->port[0].disabled)
120 writeb(0, base + ICS_ARCIN_V6_INTROFFSET_1);
121 if (!state->port[1].disabled)
122 writeb(0, base + ICS_ARCIN_V6_INTROFFSET_2);
125 /* Prototype: pata_icside_irqdisable_arcin_v6 (struct expansion_card *ec, int irqnr)
126 * Purpose : disable interrupts from card
128 static void pata_icside_irqdisable_arcin_v6 (struct expansion_card *ec, int irqnr)
130 struct pata_icside_state *state = ec->irq_data;
132 readb(state->irq_port + ICS_ARCIN_V6_INTROFFSET_1);
133 readb(state->irq_port + ICS_ARCIN_V6_INTROFFSET_2);
136 /* Prototype: pata_icside_irqprobe(struct expansion_card *ec)
137 * Purpose : detect an active interrupt from card
139 static int pata_icside_irqpending_arcin_v6(struct expansion_card *ec)
141 struct pata_icside_state *state = ec->irq_data;
143 return readb(state->irq_port + ICS_ARCIN_V6_INTRSTAT_1) & 1 ||
144 readb(state->irq_port + ICS_ARCIN_V6_INTRSTAT_2) & 1;
147 static const expansioncard_ops_t pata_icside_ops_arcin_v6 = {
148 .irqenable = pata_icside_irqenable_arcin_v6,
149 .irqdisable = pata_icside_irqdisable_arcin_v6,
150 .irqpending = pata_icside_irqpending_arcin_v6,
157 * Similar to the BM-DMA, but we use the RiscPCs IOMD DMA controllers.
158 * There is only one DMA controller per card, which means that only
159 * one drive can be accessed at one time. NOTE! We do not enforce that
160 * here, but we rely on the main IDE driver spotting that both
161 * interfaces use the same IRQ, which should guarantee this.
165 * Configure the IOMD to give the appropriate timings for the transfer
166 * mode being requested. We take the advice of the ATA standards, and
167 * calculate the cycle time based on the transfer mode, and the EIDE
168 * MW DMA specs that the drive provides in the IDENTIFY command.
170 * We have the following IOMD DMA modes to choose from:
172 * Type Active Recovery Cycle
173 * A 250 (250) 312 (550) 562 (800)
174 * B 187 (200) 250 (550) 437 (750)
175 * C 125 (125) 125 (375) 250 (500)
176 * D 62 (50) 125 (375) 187 (425)
178 * (figures in brackets are actual measured timings on DIOR/DIOW)
180 * However, we also need to take care of the read/write active and
184 * Mode Active -- Recovery -- Cycle IOMD type
185 * MW0 215 50 215 480 A
189 static void pata_icside_set_dmamode(struct ata_port *ap, struct ata_device *adev)
191 struct pata_icside_state *state = ap->host->private_data;
197 * DMA is based on a 16MHz clock
199 if (ata_timing_compute(adev, adev->dma_mode, &t, 1000, 1))
203 * Choose the IOMD cycle timing which ensure that the interface
204 * satisfies the measured active, recovery and cycle times.
206 if (t.active <= 50 && t.recover <= 375 && t.cycle <= 425)
207 iomd_type = 'D', cycle = 187;
208 else if (t.active <= 125 && t.recover <= 375 && t.cycle <= 500)
209 iomd_type = 'C', cycle = 250;
210 else if (t.active <= 200 && t.recover <= 550 && t.cycle <= 750)
211 iomd_type = 'B', cycle = 437;
213 iomd_type = 'A', cycle = 562;
215 ata_dev_printk(adev, KERN_INFO, "timings: act %dns rec %dns cyc %dns (%c)\n",
216 t.active, t.recover, t.cycle, iomd_type);
218 state->port[ap->port_no].speed[adev->devno] = cycle;
221 static void pata_icside_bmdma_setup(struct ata_queued_cmd *qc)
223 struct ata_port *ap = qc->ap;
224 struct pata_icside_state *state = ap->host->private_data;
225 struct scatterlist *sg, *rsg = state->sg;
226 unsigned int write = qc->tf.flags & ATA_TFLAG_WRITE;
229 * We are simplex; BUG if we try to fiddle with DMA
232 BUG_ON(dma_channel_active(state->dma));
235 * Copy ATAs scattered sg list into a contiguous array of sg
237 ata_for_each_sg(sg, qc) {
238 memcpy(rsg, sg, sizeof(*sg));
243 * Route the DMA signals to the correct interface
245 writeb(state->port[ap->port_no].port_sel, state->ioc_base);
247 set_dma_speed(state->dma, state->port[ap->port_no].speed[qc->dev->devno]);
248 set_dma_sg(state->dma, state->sg, rsg - state->sg);
249 set_dma_mode(state->dma, write ? DMA_MODE_WRITE : DMA_MODE_READ);
251 /* issue r/w command */
252 ap->ops->exec_command(ap, &qc->tf);
255 static void pata_icside_bmdma_start(struct ata_queued_cmd *qc)
257 struct ata_port *ap = qc->ap;
258 struct pata_icside_state *state = ap->host->private_data;
260 BUG_ON(dma_channel_active(state->dma));
261 enable_dma(state->dma);
264 static void pata_icside_bmdma_stop(struct ata_queued_cmd *qc)
266 struct ata_port *ap = qc->ap;
267 struct pata_icside_state *state = ap->host->private_data;
269 disable_dma(state->dma);
271 /* see ata_bmdma_stop */
275 static u8 pata_icside_bmdma_status(struct ata_port *ap)
277 struct pata_icside_state *state = ap->host->private_data;
278 void __iomem *irq_port;
280 irq_port = state->irq_port + (ap->port_no ? ICS_ARCIN_V6_INTRSTAT_2 :
281 ICS_ARCIN_V6_INTRSTAT_1);
283 return readb(irq_port) & 1 ? ATA_DMA_INTR : 0;
286 static int icside_dma_init(struct pata_icside_info *info)
288 struct pata_icside_state *state = info->state;
289 struct expansion_card *ec = info->ec;
292 for (i = 0; i < ATA_MAX_DEVICES; i++) {
293 state->port[0].speed[i] = 480;
294 state->port[1].speed[i] = 480;
297 if (ec->dma != NO_DMA && !request_dma(ec->dma, DRV_NAME)) {
298 state->dma = ec->dma;
299 info->mwdma_mask = 0x07; /* MW0..2 */
306 static int pata_icside_port_start(struct ata_port *ap)
308 /* No PRD to alloc */
309 return ata_pad_alloc(ap, ap->dev);
312 static struct scsi_host_template pata_icside_sht = {
313 .module = THIS_MODULE,
315 .ioctl = ata_scsi_ioctl,
316 .queuecommand = ata_scsi_queuecmd,
317 .can_queue = ATA_DEF_QUEUE,
318 .this_id = ATA_SHT_THIS_ID,
319 .sg_tablesize = PATA_ICSIDE_MAX_SG,
320 .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
321 .emulated = ATA_SHT_EMULATED,
322 .use_clustering = ATA_SHT_USE_CLUSTERING,
323 .proc_name = DRV_NAME,
324 .dma_boundary = ~0, /* no dma boundaries */
325 .slave_configure = ata_scsi_slave_config,
326 .slave_destroy = ata_scsi_slave_destroy,
327 .bios_param = ata_std_bios_param,
330 /* wish this was exported from libata-core */
331 static void ata_dummy_noret(struct ata_port *port)
335 static void pata_icside_postreset(struct ata_link *link, unsigned int *classes)
337 struct ata_port *ap = link->ap;
338 struct pata_icside_state *state = ap->host->private_data;
340 if (classes[0] != ATA_DEV_NONE || classes[1] != ATA_DEV_NONE)
341 return ata_std_postreset(link, classes);
343 state->port[ap->port_no].disabled = 1;
345 if (state->type == ICS_TYPE_V6) {
347 * Disable interrupts from this port, otherwise we
348 * receive spurious interrupts from the floating
351 void __iomem *irq_port = state->irq_port +
352 (ap->port_no ? ICS_ARCIN_V6_INTROFFSET_2 : ICS_ARCIN_V6_INTROFFSET_1);
357 static void pata_icside_error_handler(struct ata_port *ap)
359 ata_bmdma_drive_eh(ap, ata_std_prereset, ata_std_softreset, NULL,
360 pata_icside_postreset);
363 static struct ata_port_operations pata_icside_port_ops = {
364 .set_dmamode = pata_icside_set_dmamode,
366 .tf_load = ata_tf_load,
367 .tf_read = ata_tf_read,
368 .exec_command = ata_exec_command,
369 .check_status = ata_check_status,
370 .dev_select = ata_std_dev_select,
372 .cable_detect = ata_cable_40wire,
374 .bmdma_setup = pata_icside_bmdma_setup,
375 .bmdma_start = pata_icside_bmdma_start,
377 .data_xfer = ata_data_xfer_noirq,
379 /* no need to build any PRD tables for DMA */
380 .qc_prep = ata_noop_qc_prep,
381 .qc_issue = ata_qc_issue_prot,
383 .freeze = ata_bmdma_freeze,
384 .thaw = ata_bmdma_thaw,
385 .error_handler = pata_icside_error_handler,
386 .post_internal_cmd = pata_icside_bmdma_stop,
388 .irq_clear = ata_dummy_noret,
389 .irq_on = ata_irq_on,
391 .port_start = pata_icside_port_start,
393 .bmdma_stop = pata_icside_bmdma_stop,
394 .bmdma_status = pata_icside_bmdma_status,
397 static void __devinit
398 pata_icside_setup_ioaddr(struct ata_port *ap, void __iomem *base,
399 struct pata_icside_info *info,
400 const struct portinfo *port)
402 struct ata_ioports *ioaddr = &ap->ioaddr;
403 void __iomem *cmd = base + port->dataoffset;
405 ioaddr->cmd_addr = cmd;
406 ioaddr->data_addr = cmd + (ATA_REG_DATA << port->stepping);
407 ioaddr->error_addr = cmd + (ATA_REG_ERR << port->stepping);
408 ioaddr->feature_addr = cmd + (ATA_REG_FEATURE << port->stepping);
409 ioaddr->nsect_addr = cmd + (ATA_REG_NSECT << port->stepping);
410 ioaddr->lbal_addr = cmd + (ATA_REG_LBAL << port->stepping);
411 ioaddr->lbam_addr = cmd + (ATA_REG_LBAM << port->stepping);
412 ioaddr->lbah_addr = cmd + (ATA_REG_LBAH << port->stepping);
413 ioaddr->device_addr = cmd + (ATA_REG_DEVICE << port->stepping);
414 ioaddr->status_addr = cmd + (ATA_REG_STATUS << port->stepping);
415 ioaddr->command_addr = cmd + (ATA_REG_CMD << port->stepping);
417 ioaddr->ctl_addr = base + port->ctrloffset;
418 ioaddr->altstatus_addr = ioaddr->ctl_addr;
420 ata_port_desc(ap, "cmd 0x%lx ctl 0x%lx",
421 info->raw_base + port->dataoffset,
422 info->raw_base + port->ctrloffset);
424 if (info->raw_ioc_base)
425 ata_port_desc(ap, "iocbase 0x%lx", info->raw_ioc_base);
428 static int __devinit pata_icside_register_v5(struct pata_icside_info *info)
430 struct pata_icside_state *state = info->state;
433 base = ecardm_iomap(info->ec, ECARD_RES_MEMC, 0, 0);
437 state->irq_port = base;
440 info->irqaddr = base + ICS_ARCIN_V5_INTRSTAT;
442 info->irqops = &pata_icside_ops_arcin_v5;
444 info->port[0] = &pata_icside_portinfo_v5;
446 info->raw_base = ecard_resource_start(info->ec, ECARD_RES_MEMC);
451 static int __devinit pata_icside_register_v6(struct pata_icside_info *info)
453 struct pata_icside_state *state = info->state;
454 struct expansion_card *ec = info->ec;
455 void __iomem *ioc_base, *easi_base;
456 unsigned int sel = 0;
458 ioc_base = ecardm_iomap(ec, ECARD_RES_IOCFAST, 0, 0);
462 easi_base = ioc_base;
464 if (ecard_resource_flags(ec, ECARD_RES_EASI)) {
465 easi_base = ecardm_iomap(ec, ECARD_RES_EASI, 0, 0);
470 * Enable access to the EASI region.
475 writeb(sel, ioc_base);
477 state->irq_port = easi_base;
478 state->ioc_base = ioc_base;
479 state->port[0].port_sel = sel;
480 state->port[1].port_sel = sel | 1;
482 info->base = easi_base;
483 info->irqops = &pata_icside_ops_arcin_v6;
485 info->port[0] = &pata_icside_portinfo_v6_1;
486 info->port[1] = &pata_icside_portinfo_v6_2;
488 info->raw_base = ecard_resource_start(ec, ECARD_RES_EASI);
489 info->raw_ioc_base = ecard_resource_start(ec, ECARD_RES_IOCFAST);
491 return icside_dma_init(info);
494 static int __devinit pata_icside_add_ports(struct pata_icside_info *info)
496 struct expansion_card *ec = info->ec;
497 struct ata_host *host;
501 ec->irqaddr = info->irqaddr;
502 ec->irqmask = info->irqmask;
505 ecard_setirq(ec, info->irqops, info->state);
508 * Be on the safe side - disable interrupts
510 ec->ops->irqdisable(ec, ec->irq);
512 host = ata_host_alloc(&ec->dev, info->nr_ports);
516 host->private_data = info->state;
517 host->flags = ATA_HOST_SIMPLEX;
519 for (i = 0; i < info->nr_ports; i++) {
520 struct ata_port *ap = host->ports[i];
523 ap->mwdma_mask = info->mwdma_mask;
524 ap->flags |= ATA_FLAG_SLAVE_POSS;
525 ap->ops = &pata_icside_port_ops;
527 pata_icside_setup_ioaddr(ap, info->base, info, info->port[i]);
530 return ata_host_activate(host, ec->irq, ata_interrupt, 0,
535 pata_icside_probe(struct expansion_card *ec, const struct ecard_id *id)
537 struct pata_icside_state *state;
538 struct pata_icside_info info;
542 ret = ecard_request_resources(ec);
546 state = devm_kzalloc(&ec->dev, sizeof(*state), GFP_KERNEL);
552 state->type = ICS_TYPE_NOTYPE;
555 idmem = ecardm_iomap(ec, ECARD_RES_IOCFAST, 0, 0);
559 type = readb(idmem + ICS_IDENT_OFFSET) & 1;
560 type |= (readb(idmem + ICS_IDENT_OFFSET + 4) & 1) << 1;
561 type |= (readb(idmem + ICS_IDENT_OFFSET + 8) & 1) << 2;
562 type |= (readb(idmem + ICS_IDENT_OFFSET + 12) & 1) << 3;
563 ecardm_iounmap(ec, idmem);
568 memset(&info, 0, sizeof(info));
572 switch (state->type) {
574 dev_warn(&ec->dev, "A3IN unsupported\n");
578 case ICS_TYPE_A3USER:
579 dev_warn(&ec->dev, "A3USER unsupported\n");
584 ret = pata_icside_register_v5(&info);
588 ret = pata_icside_register_v6(&info);
592 dev_warn(&ec->dev, "unknown interface type\n");
598 ret = pata_icside_add_ports(&info);
604 ecard_release_resources(ec);
609 static void pata_icside_shutdown(struct expansion_card *ec)
611 struct ata_host *host = ecard_get_drvdata(ec);
615 * Disable interrupts from this card. We need to do
616 * this before disabling EASI since we may be accessing
617 * this register via that region.
619 local_irq_save(flags);
620 ec->ops->irqdisable(ec, ec->irq);
621 local_irq_restore(flags);
624 * Reset the ROM pointer so that we can read the ROM
625 * after a soft reboot. This also disables access to
626 * the IDE taskfile via the EASI region.
629 struct pata_icside_state *state = host->private_data;
631 writeb(0, state->ioc_base);
635 static void __devexit pata_icside_remove(struct expansion_card *ec)
637 struct ata_host *host = ecard_get_drvdata(ec);
638 struct pata_icside_state *state = host->private_data;
640 ata_host_detach(host);
642 pata_icside_shutdown(ec);
645 * don't NULL out the drvdata - devres/libata wants it
646 * to free the ata_host structure.
648 if (state->dma != NO_DMA)
649 free_dma(state->dma);
651 ecard_release_resources(ec);
654 static const struct ecard_id pata_icside_ids[] = {
655 { MANU_ICS, PROD_ICS_IDE },
656 { MANU_ICS2, PROD_ICS2_IDE },
660 static struct ecard_driver pata_icside_driver = {
661 .probe = pata_icside_probe,
662 .remove = __devexit_p(pata_icside_remove),
663 .shutdown = pata_icside_shutdown,
664 .id_table = pata_icside_ids,
670 static int __init pata_icside_init(void)
672 return ecard_register_driver(&pata_icside_driver);
675 static void __exit pata_icside_exit(void)
677 ecard_remove_driver(&pata_icside_driver);
680 MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>");
681 MODULE_LICENSE("GPL");
682 MODULE_DESCRIPTION("ICS PATA driver");
684 module_init(pata_icside_init);
685 module_exit(pata_icside_exit);