2 * pdc_adma.c - Pacific Digital Corporation ADMA
4 * Maintained by: Mark Lord <mlord@pobox.com>
6 * Copyright 2005 Mark Lord
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2, or (at your option)
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; see the file COPYING. If not, write to
20 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23 * libata documentation is available via 'make {ps|pdf}docs',
24 * as Documentation/DocBook/libata.*
27 * Supports ATA disks in single-packet ADMA mode.
28 * Uses PIO for everything else.
30 * TODO: Use ADMA transfers for ATAPI devices, when possible.
31 * This requires careful attention to a number of quirks of the chip.
35 #include <linux/kernel.h>
36 #include <linux/module.h>
37 #include <linux/pci.h>
38 #include <linux/init.h>
39 #include <linux/blkdev.h>
40 #include <linux/delay.h>
41 #include <linux/interrupt.h>
42 #include <linux/sched.h>
43 #include <linux/device.h>
44 #include <scsi/scsi_host.h>
46 #include <linux/libata.h>
48 #define DRV_NAME "pdc_adma"
49 #define DRV_VERSION "0.04"
51 /* macro to calculate base address for ATA regs */
52 #define ADMA_ATA_REGS(base,port_no) ((base) + ((port_no) * 0x40))
54 /* macro to calculate base address for ADMA regs */
55 #define ADMA_REGS(base,port_no) ((base) + 0x80 + ((port_no) * 0x20))
60 ADMA_PRD_BYTES = LIBATA_MAX_PRD * 16,
61 ADMA_PKT_BYTES = ADMA_CPB_BYTES + ADMA_PRD_BYTES,
63 ADMA_DMA_BOUNDARY = 0xffffffff,
65 /* global register offsets */
66 ADMA_MODE_LOCK = 0x00c7,
68 /* per-channel register offsets */
69 ADMA_CONTROL = 0x0000, /* ADMA control */
70 ADMA_STATUS = 0x0002, /* ADMA status */
71 ADMA_CPB_COUNT = 0x0004, /* CPB count */
72 ADMA_CPB_CURRENT = 0x000c, /* current CPB address */
73 ADMA_CPB_NEXT = 0x000c, /* next CPB address */
74 ADMA_CPB_LOOKUP = 0x0010, /* CPB lookup table */
75 ADMA_FIFO_IN = 0x0014, /* input FIFO threshold */
76 ADMA_FIFO_OUT = 0x0016, /* output FIFO threshold */
78 /* ADMA_CONTROL register bits */
79 aNIEN = (1 << 8), /* irq mask: 1==masked */
80 aGO = (1 << 7), /* packet trigger ("Go!") */
81 aRSTADM = (1 << 5), /* ADMA logic reset */
82 aPIOMD4 = 0x0003, /* PIO mode 4 */
84 /* ADMA_STATUS register bits */
100 /* ATA register flags */
104 /* ATA register addresses */
105 ADMA_REGS_CONTROL = 0x0e,
106 ADMA_REGS_SECTOR_COUNT = 0x12,
107 ADMA_REGS_LBA_LOW = 0x13,
108 ADMA_REGS_LBA_MID = 0x14,
109 ADMA_REGS_LBA_HIGH = 0x15,
110 ADMA_REGS_DEVICE = 0x16,
111 ADMA_REGS_COMMAND = 0x17,
114 board_1841_idx = 0, /* ADMA 2-port controller */
117 typedef enum { adma_state_idle, adma_state_pkt, adma_state_mmio } adma_state_t;
119 struct adma_port_priv {
125 static int adma_ata_init_one (struct pci_dev *pdev,
126 const struct pci_device_id *ent);
127 static irqreturn_t adma_intr (int irq, void *dev_instance,
128 struct pt_regs *regs);
129 static int adma_port_start(struct ata_port *ap);
130 static void adma_host_stop(struct ata_host *host);
131 static void adma_port_stop(struct ata_port *ap);
132 static void adma_phy_reset(struct ata_port *ap);
133 static void adma_qc_prep(struct ata_queued_cmd *qc);
134 static unsigned int adma_qc_issue(struct ata_queued_cmd *qc);
135 static int adma_check_atapi_dma(struct ata_queued_cmd *qc);
136 static void adma_bmdma_stop(struct ata_queued_cmd *qc);
137 static u8 adma_bmdma_status(struct ata_port *ap);
138 static void adma_irq_clear(struct ata_port *ap);
139 static void adma_eng_timeout(struct ata_port *ap);
141 static struct scsi_host_template adma_ata_sht = {
142 .module = THIS_MODULE,
144 .ioctl = ata_scsi_ioctl,
145 .queuecommand = ata_scsi_queuecmd,
146 .can_queue = ATA_DEF_QUEUE,
147 .this_id = ATA_SHT_THIS_ID,
148 .sg_tablesize = LIBATA_MAX_PRD,
149 .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
150 .emulated = ATA_SHT_EMULATED,
151 .use_clustering = ENABLE_CLUSTERING,
152 .proc_name = DRV_NAME,
153 .dma_boundary = ADMA_DMA_BOUNDARY,
154 .slave_configure = ata_scsi_slave_config,
155 .slave_destroy = ata_scsi_slave_destroy,
156 .bios_param = ata_std_bios_param,
159 static const struct ata_port_operations adma_ata_ops = {
160 .port_disable = ata_port_disable,
161 .tf_load = ata_tf_load,
162 .tf_read = ata_tf_read,
163 .check_status = ata_check_status,
164 .check_atapi_dma = adma_check_atapi_dma,
165 .exec_command = ata_exec_command,
166 .dev_select = ata_std_dev_select,
167 .phy_reset = adma_phy_reset,
168 .qc_prep = adma_qc_prep,
169 .qc_issue = adma_qc_issue,
170 .eng_timeout = adma_eng_timeout,
171 .data_xfer = ata_mmio_data_xfer,
172 .irq_handler = adma_intr,
173 .irq_clear = adma_irq_clear,
174 .port_start = adma_port_start,
175 .port_stop = adma_port_stop,
176 .host_stop = adma_host_stop,
177 .bmdma_stop = adma_bmdma_stop,
178 .bmdma_status = adma_bmdma_status,
181 static struct ata_port_info adma_port_info[] = {
184 .sht = &adma_ata_sht,
185 .flags = ATA_FLAG_SLAVE_POSS | ATA_FLAG_SRST |
186 ATA_FLAG_NO_LEGACY | ATA_FLAG_MMIO |
187 ATA_FLAG_PIO_POLLING,
188 .pio_mask = 0x10, /* pio4 */
189 .udma_mask = 0x1f, /* udma0-4 */
190 .port_ops = &adma_ata_ops,
194 static const struct pci_device_id adma_ata_pci_tbl[] = {
195 { PCI_VENDOR_ID_PDC, 0x1841, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
198 { } /* terminate list */
201 static struct pci_driver adma_ata_pci_driver = {
203 .id_table = adma_ata_pci_tbl,
204 .probe = adma_ata_init_one,
205 .remove = ata_pci_remove_one,
208 static int adma_check_atapi_dma(struct ata_queued_cmd *qc)
210 return 1; /* ATAPI DMA not yet supported */
213 static void adma_bmdma_stop(struct ata_queued_cmd *qc)
218 static u8 adma_bmdma_status(struct ata_port *ap)
223 static void adma_irq_clear(struct ata_port *ap)
228 static void adma_reset_engine(void __iomem *chan)
230 /* reset ADMA to idle state */
231 writew(aPIOMD4 | aNIEN | aRSTADM, chan + ADMA_CONTROL);
233 writew(aPIOMD4, chan + ADMA_CONTROL);
237 static void adma_reinit_engine(struct ata_port *ap)
239 struct adma_port_priv *pp = ap->private_data;
240 void __iomem *mmio_base = ap->host->mmio_base;
241 void __iomem *chan = ADMA_REGS(mmio_base, ap->port_no);
243 /* mask/clear ATA interrupts */
244 writeb(ATA_NIEN, (void __iomem *)ap->ioaddr.ctl_addr);
245 ata_check_status(ap);
247 /* reset the ADMA engine */
248 adma_reset_engine(chan);
250 /* set in-FIFO threshold to 0x100 */
251 writew(0x100, chan + ADMA_FIFO_IN);
253 /* set CPB pointer */
254 writel((u32)pp->pkt_dma, chan + ADMA_CPB_NEXT);
256 /* set out-FIFO threshold to 0x100 */
257 writew(0x100, chan + ADMA_FIFO_OUT);
260 writew(1, chan + ADMA_CPB_COUNT);
262 /* read/discard ADMA status */
263 readb(chan + ADMA_STATUS);
266 static inline void adma_enter_reg_mode(struct ata_port *ap)
268 void __iomem *chan = ADMA_REGS(ap->host->mmio_base, ap->port_no);
270 writew(aPIOMD4, chan + ADMA_CONTROL);
271 readb(chan + ADMA_STATUS); /* flush */
274 static void adma_phy_reset(struct ata_port *ap)
276 struct adma_port_priv *pp = ap->private_data;
278 pp->state = adma_state_idle;
279 adma_reinit_engine(ap);
284 static void adma_eng_timeout(struct ata_port *ap)
286 struct adma_port_priv *pp = ap->private_data;
288 if (pp->state != adma_state_idle) /* healthy paranoia */
289 pp->state = adma_state_mmio;
290 adma_reinit_engine(ap);
294 static int adma_fill_sg(struct ata_queued_cmd *qc)
296 struct scatterlist *sg;
297 struct ata_port *ap = qc->ap;
298 struct adma_port_priv *pp = ap->private_data;
300 int i = (2 + buf[3]) * 8;
301 u8 pFLAGS = pORD | ((qc->tf.flags & ATA_TFLAG_WRITE) ? pDIRO : 0);
303 ata_for_each_sg(sg, qc) {
307 addr = (u32)sg_dma_address(sg);
308 *(__le32 *)(buf + i) = cpu_to_le32(addr);
311 len = sg_dma_len(sg) >> 3;
312 *(__le32 *)(buf + i) = cpu_to_le32(len);
315 if (ata_sg_is_last(sg, qc))
318 buf[i++] = qc->dev->dma_mode & 0xf;
319 buf[i++] = 0; /* pPKLW */
320 buf[i++] = 0; /* reserved */
323 = (pFLAGS & pEND) ? 0 : cpu_to_le32(pp->pkt_dma + i + 4);
326 VPRINTK("PRD[%u] = (0x%lX, 0x%X)\n", i/4,
327 (unsigned long)addr, len);
332 static void adma_qc_prep(struct ata_queued_cmd *qc)
334 struct adma_port_priv *pp = qc->ap->private_data;
336 u32 pkt_dma = (u32)pp->pkt_dma;
341 adma_enter_reg_mode(qc->ap);
342 if (qc->tf.protocol != ATA_PROT_DMA) {
347 buf[i++] = 0; /* Response flags */
348 buf[i++] = 0; /* reserved */
349 buf[i++] = cVLD | cDAT | cIEN;
350 i++; /* cLEN, gets filled in below */
352 *(__le32 *)(buf+i) = cpu_to_le32(pkt_dma); /* cNCPB */
354 i += 4; /* cPRD, gets filled in below */
356 buf[i++] = 0; /* reserved */
357 buf[i++] = 0; /* reserved */
358 buf[i++] = 0; /* reserved */
359 buf[i++] = 0; /* reserved */
361 /* ATA registers; must be a multiple of 4 */
362 buf[i++] = qc->tf.device;
363 buf[i++] = ADMA_REGS_DEVICE;
364 if ((qc->tf.flags & ATA_TFLAG_LBA48)) {
365 buf[i++] = qc->tf.hob_nsect;
366 buf[i++] = ADMA_REGS_SECTOR_COUNT;
367 buf[i++] = qc->tf.hob_lbal;
368 buf[i++] = ADMA_REGS_LBA_LOW;
369 buf[i++] = qc->tf.hob_lbam;
370 buf[i++] = ADMA_REGS_LBA_MID;
371 buf[i++] = qc->tf.hob_lbah;
372 buf[i++] = ADMA_REGS_LBA_HIGH;
374 buf[i++] = qc->tf.nsect;
375 buf[i++] = ADMA_REGS_SECTOR_COUNT;
376 buf[i++] = qc->tf.lbal;
377 buf[i++] = ADMA_REGS_LBA_LOW;
378 buf[i++] = qc->tf.lbam;
379 buf[i++] = ADMA_REGS_LBA_MID;
380 buf[i++] = qc->tf.lbah;
381 buf[i++] = ADMA_REGS_LBA_HIGH;
383 buf[i++] = ADMA_REGS_CONTROL;
386 buf[i++] = qc->tf.command;
387 buf[i++] = ADMA_REGS_COMMAND | rEND;
389 buf[3] = (i >> 3) - 2; /* cLEN */
390 *(__le32 *)(buf+8) = cpu_to_le32(pkt_dma + i); /* cPRD */
392 i = adma_fill_sg(qc);
393 wmb(); /* flush PRDs and pkt to memory */
395 /* dump out CPB + PRDs for debug */
398 static char obuf[2048];
399 for (j = 0; j < i; ++j) {
400 len += sprintf(obuf+len, "%02x ", buf[j]);
402 printk("%s\n", obuf);
407 printk("%s\n", obuf);
412 static inline void adma_packet_start(struct ata_queued_cmd *qc)
414 struct ata_port *ap = qc->ap;
415 void __iomem *chan = ADMA_REGS(ap->host->mmio_base, ap->port_no);
417 VPRINTK("ENTER, ap %p\n", ap);
419 /* fire up the ADMA engine */
420 writew(aPIOMD4 | aGO, chan + ADMA_CONTROL);
423 static unsigned int adma_qc_issue(struct ata_queued_cmd *qc)
425 struct adma_port_priv *pp = qc->ap->private_data;
427 switch (qc->tf.protocol) {
429 pp->state = adma_state_pkt;
430 adma_packet_start(qc);
433 case ATA_PROT_ATAPI_DMA:
441 pp->state = adma_state_mmio;
442 return ata_qc_issue_prot(qc);
445 static inline unsigned int adma_intr_pkt(struct ata_host *host)
447 unsigned int handled = 0, port_no;
448 u8 __iomem *mmio_base = host->mmio_base;
450 for (port_no = 0; port_no < host->n_ports; ++port_no) {
451 struct ata_port *ap = host->ports[port_no];
452 struct adma_port_priv *pp;
453 struct ata_queued_cmd *qc;
454 void __iomem *chan = ADMA_REGS(mmio_base, port_no);
455 u8 status = readb(chan + ADMA_STATUS);
460 adma_enter_reg_mode(ap);
461 if (ap->flags & ATA_FLAG_DISABLED)
463 pp = ap->private_data;
464 if (!pp || pp->state != adma_state_pkt)
466 qc = ata_qc_from_tag(ap, ap->active_tag);
467 if (qc && (!(qc->tf.flags & ATA_TFLAG_POLLING))) {
468 if ((status & (aPERR | aPSD | aUIRQ)))
469 qc->err_mask |= AC_ERR_OTHER;
470 else if (pp->pkt[0] != cDONE)
471 qc->err_mask |= AC_ERR_OTHER;
479 static inline unsigned int adma_intr_mmio(struct ata_host *host)
481 unsigned int handled = 0, port_no;
483 for (port_no = 0; port_no < host->n_ports; ++port_no) {
485 ap = host->ports[port_no];
486 if (ap && (!(ap->flags & ATA_FLAG_DISABLED))) {
487 struct ata_queued_cmd *qc;
488 struct adma_port_priv *pp = ap->private_data;
489 if (!pp || pp->state != adma_state_mmio)
491 qc = ata_qc_from_tag(ap, ap->active_tag);
492 if (qc && (!(qc->tf.flags & ATA_TFLAG_POLLING))) {
494 /* check main status, clearing INTRQ */
495 u8 status = ata_check_status(ap);
496 if ((status & ATA_BUSY))
498 DPRINTK("ata%u: protocol %d (dev_stat 0x%X)\n",
499 ap->id, qc->tf.protocol, status);
501 /* complete taskfile transaction */
502 pp->state = adma_state_idle;
503 qc->err_mask |= ac_err_mask(status);
512 static irqreturn_t adma_intr(int irq, void *dev_instance, struct pt_regs *regs)
514 struct ata_host *host = dev_instance;
515 unsigned int handled = 0;
519 spin_lock(&host->lock);
520 handled = adma_intr_pkt(host) | adma_intr_mmio(host);
521 spin_unlock(&host->lock);
525 return IRQ_RETVAL(handled);
528 static void adma_ata_setup_port(struct ata_ioports *port, unsigned long base)
531 port->data_addr = base + 0x000;
533 port->feature_addr = base + 0x004;
534 port->nsect_addr = base + 0x008;
535 port->lbal_addr = base + 0x00c;
536 port->lbam_addr = base + 0x010;
537 port->lbah_addr = base + 0x014;
538 port->device_addr = base + 0x018;
540 port->command_addr = base + 0x01c;
541 port->altstatus_addr =
542 port->ctl_addr = base + 0x038;
545 static int adma_port_start(struct ata_port *ap)
547 struct device *dev = ap->host->dev;
548 struct adma_port_priv *pp;
551 rc = ata_port_start(ap);
554 adma_enter_reg_mode(ap);
556 pp = kcalloc(1, sizeof(*pp), GFP_KERNEL);
559 pp->pkt = dma_alloc_coherent(dev, ADMA_PKT_BYTES, &pp->pkt_dma,
564 if ((pp->pkt_dma & 7) != 0) {
565 printk("bad alignment for pp->pkt_dma: %08x\n",
567 dma_free_coherent(dev, ADMA_PKT_BYTES,
568 pp->pkt, pp->pkt_dma);
571 memset(pp->pkt, 0, ADMA_PKT_BYTES);
572 ap->private_data = pp;
573 adma_reinit_engine(ap);
583 static void adma_port_stop(struct ata_port *ap)
585 struct device *dev = ap->host->dev;
586 struct adma_port_priv *pp = ap->private_data;
588 adma_reset_engine(ADMA_REGS(ap->host->mmio_base, ap->port_no));
590 ap->private_data = NULL;
592 dma_free_coherent(dev, ADMA_PKT_BYTES,
593 pp->pkt, pp->pkt_dma);
599 static void adma_host_stop(struct ata_host *host)
601 unsigned int port_no;
603 for (port_no = 0; port_no < ADMA_PORTS; ++port_no)
604 adma_reset_engine(ADMA_REGS(host->mmio_base, port_no));
606 ata_pci_host_stop(host);
609 static void adma_host_init(unsigned int chip_id,
610 struct ata_probe_ent *probe_ent)
612 unsigned int port_no;
613 void __iomem *mmio_base = probe_ent->mmio_base;
615 /* enable/lock aGO operation */
616 writeb(7, mmio_base + ADMA_MODE_LOCK);
618 /* reset the ADMA logic */
619 for (port_no = 0; port_no < ADMA_PORTS; ++port_no)
620 adma_reset_engine(ADMA_REGS(mmio_base, port_no));
623 static int adma_set_dma_masks(struct pci_dev *pdev, void __iomem *mmio_base)
627 rc = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
629 dev_printk(KERN_ERR, &pdev->dev,
630 "32-bit DMA enable failed\n");
633 rc = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
635 dev_printk(KERN_ERR, &pdev->dev,
636 "32-bit consistent DMA enable failed\n");
642 static int adma_ata_init_one(struct pci_dev *pdev,
643 const struct pci_device_id *ent)
645 static int printed_version;
646 struct ata_probe_ent *probe_ent = NULL;
647 void __iomem *mmio_base;
648 unsigned int board_idx = (unsigned int) ent->driver_data;
651 if (!printed_version++)
652 dev_printk(KERN_DEBUG, &pdev->dev, "version " DRV_VERSION "\n");
654 rc = pci_enable_device(pdev);
658 rc = pci_request_regions(pdev, DRV_NAME);
662 if ((pci_resource_flags(pdev, 4) & IORESOURCE_MEM) == 0) {
664 goto err_out_regions;
667 mmio_base = pci_iomap(pdev, 4, 0);
668 if (mmio_base == NULL) {
670 goto err_out_regions;
673 rc = adma_set_dma_masks(pdev, mmio_base);
675 goto err_out_iounmap;
677 probe_ent = kcalloc(1, sizeof(*probe_ent), GFP_KERNEL);
678 if (probe_ent == NULL) {
680 goto err_out_iounmap;
683 probe_ent->dev = pci_dev_to_dev(pdev);
684 INIT_LIST_HEAD(&probe_ent->node);
686 probe_ent->sht = adma_port_info[board_idx].sht;
687 probe_ent->port_flags = adma_port_info[board_idx].flags;
688 probe_ent->pio_mask = adma_port_info[board_idx].pio_mask;
689 probe_ent->mwdma_mask = adma_port_info[board_idx].mwdma_mask;
690 probe_ent->udma_mask = adma_port_info[board_idx].udma_mask;
691 probe_ent->port_ops = adma_port_info[board_idx].port_ops;
693 probe_ent->irq = pdev->irq;
694 probe_ent->irq_flags = IRQF_SHARED;
695 probe_ent->mmio_base = mmio_base;
696 probe_ent->n_ports = ADMA_PORTS;
698 for (port_no = 0; port_no < probe_ent->n_ports; ++port_no) {
699 adma_ata_setup_port(&probe_ent->port[port_no],
700 ADMA_ATA_REGS((unsigned long)mmio_base, port_no));
703 pci_set_master(pdev);
705 /* initialize adapter */
706 adma_host_init(board_idx, probe_ent);
708 rc = ata_device_add(probe_ent);
710 if (rc != ADMA_PORTS)
711 goto err_out_iounmap;
715 pci_iounmap(pdev, mmio_base);
717 pci_release_regions(pdev);
719 pci_disable_device(pdev);
723 static int __init adma_ata_init(void)
725 return pci_register_driver(&adma_ata_pci_driver);
728 static void __exit adma_ata_exit(void)
730 pci_unregister_driver(&adma_ata_pci_driver);
733 MODULE_AUTHOR("Mark Lord");
734 MODULE_DESCRIPTION("Pacific Digital Corporation ADMA low-level driver");
735 MODULE_LICENSE("GPL");
736 MODULE_DEVICE_TABLE(pci, adma_ata_pci_tbl);
737 MODULE_VERSION(DRV_VERSION);
739 module_init(adma_ata_init);
740 module_exit(adma_ata_exit);