25 #include <linux/kernel.h>
26 #include <linux/module.h>
27 #include <linux/pci.h>
31 #include <linux/device.h>
32 #include <scsi/scsi.h>
37 #define DRV_NAME "pata_pdc2027x"
38 #define DRV_VERSION "1.0"
42 #define PDPRINTK(fmt, args...) printk(KERN_ERR "%s: " fmt, __func__, ## args)
44 #define PDPRINTK(fmt, args...)
66 static int pdc2027x_reinit_one(
struct pci_dev *pdev);
67 static int pdc2027x_prereset(
struct ata_link *
link,
unsigned long deadline);
71 static unsigned long pdc2027x_mode_filter(
struct ata_device *adev,
unsigned long mask);
72 static int pdc2027x_cable_detect(
struct ata_port *ap);
83 static struct pdc2027x_pio_timing {
84 u8 value0, value1, value2;
85 } pdc2027x_pio_timing_tbl [] = {
93 static struct pdc2027x_mdma_timing {
95 } pdc2027x_mdma_timing_tbl [] = {
101 static struct pdc2027x_udma_timing {
102 u8 value0, value1, value2;
103 } pdc2027x_udma_timing_tbl [] = {
104 { 0x4a, 0x0f, 0xd5 },
105 { 0x3a, 0x0a, 0xd0 },
106 { 0x2a, 0x07, 0xcd },
107 { 0x1a, 0x05, 0xcd },
108 { 0x1a, 0x03, 0xcd },
109 { 0x1a, 0x02, 0xcb },
110 { 0x1a, 0x01, 0xcb },
125 static struct pci_driver pdc2027x_pci_driver = {
127 .id_table = pdc2027x_pci_tbl,
128 .probe = pdc2027x_init_one,
129 .remove = ata_pci_remove_one,
131 .suspend = ata_pci_device_suspend,
132 .resume = pdc2027x_reinit_one,
141 .inherits = &ata_bmdma_port_ops,
142 .check_atapi_dma = pdc2027x_check_atapi_dma,
143 .cable_detect = pdc2027x_cable_detect,
144 .prereset = pdc2027x_prereset,
148 .inherits = &pdc2027x_pata100_ops,
150 .set_piomode = pdc2027x_set_piomode,
151 .set_dmamode = pdc2027x_set_dmamode,
152 .set_mode = pdc2027x_set_mode,
162 .port_ops = &pdc2027x_pata100_ops,
170 .port_ops = &pdc2027x_pata133_ops,
198 u8 adj = (adev->
devno) ? 0x08 : 0x00;
199 return port_mmio(ap, offset) + adj;
212 static int pdc2027x_cable_detect(
struct ata_port *ap)
218 if (cgcr & (1 << 26))
233 static inline int pdc2027x_port_enabled(
struct ata_port *ap)
249 static int pdc2027x_prereset(
struct ata_link *
link,
unsigned long deadline)
252 if (!pdc2027x_port_enabled(link->
ap))
265 static unsigned long pdc2027x_mode_filter(
struct ata_device *adev,
unsigned long mask)
313 ctcr0 |= pdc2027x_pio_timing_tbl[
pio].value0 |
314 (pdc2027x_pio_timing_tbl[
pio].value1 << 8);
319 ctcr1 |= (pdc2027x_pio_timing_tbl[
pio].value2 << 24);
324 PDPRINTK(
"Set to pio mode[%u] \n", pio);
345 unsigned int udma_mode = dma_mode & 0x07;
361 ctcr1 |= pdc2027x_udma_timing_tbl[udma_mode].value0 |
362 (pdc2027x_udma_timing_tbl[udma_mode].value1 << 8) |
363 (pdc2027x_udma_timing_tbl[udma_mode].value2 << 16);
368 PDPRINTK(
"Set to udma mode[%u] \n", udma_mode);
373 unsigned int mdma_mode = dma_mode & 0x07;
379 ctcr0 |= (pdc2027x_mdma_timing_tbl[mdma_mode].value0 << 16) |
380 (pdc2027x_mdma_timing_tbl[mdma_mode].value1 << 24);
385 PDPRINTK(
"Set to mdma mode[%u] \n", mdma_mode);
411 pdc2027x_set_piomode(ap, dev);
423 pdc2027x_set_dmamode(ap, dev);
451 switch (scsicmd[0]) {
480 u32 bccrl, bccrh, bccrlv, bccrhv;
490 counter = (bccrh << 15) | bccrl;
492 PDPRINTK(
"bccrh [%X] bccrl [%X]\n", bccrh, bccrl);
493 PDPRINTK(
"bccrhv[%X] bccrlv[%X]\n", bccrhv, bccrlv);
500 if (retry && !(bccrh == bccrhv && bccrl >= bccrlv)) {
516 static void pdc_adjust_pll(
struct ata_host *host,
long pll_clock,
unsigned int board_idx)
520 long pll_clock_khz = pll_clock / 1000;
522 long ratio = pout_required / pll_clock_khz;
526 if (
unlikely(pll_clock_khz < 5000L || pll_clock_khz > 70000L)) {
532 PDPRINTK(
"pout_required is %ld\n", pout_required);
549 }
else if (ratio < 12900L) {
552 }
else if (ratio < 16100L) {
555 }
else if (ratio < 64000L) {
563 F = (ratio * (R+2)) / 1000 - 2;
571 PDPRINTK(
"F[%d] R[%d] ratio*1000[%ld]\n", F, R, ratio);
573 pll_ctl = (R << 8) | F;
575 PDPRINTK(
"Writing pll_ctl[%X]\n", pll_ctl);
602 static long pdc_detect_pll_input_clock(
struct ata_host *host)
606 long start_count, end_count;
608 long pll_clock, usec_elapsed;
617 start_count = pdc_read_counter(host);
624 end_count = pdc_read_counter(host);
634 usec_elapsed = (end_time.tv_sec -
start_time.tv_sec) * 1000000 +
637 pll_clock = ((start_count - end_count) & 0x3fffffff) / 100 *
638 (100000000 / usec_elapsed);
640 PDPRINTK(
"start[%ld] end[%ld] \n", start_count, end_count);
641 PDPRINTK(
"PLL input clock[%ld]Hz\n", pll_clock);
651 static int pdc_hardware_init(
struct ata_host *host,
unsigned int board_idx)
661 pll_clock = pdc_detect_pll_input_clock(host);
663 dev_info(host->
dev,
"PLL input clock %ld kHz\n", pll_clock/1000);
666 pdc_adjust_pll(host, pll_clock, board_idx);
676 static void pdc_ata_setup_port(
struct ata_ioports *
port,
void __iomem *base)
679 port->data_addr = base;
681 port->error_addr = base + 0x05;
682 port->nsect_addr = base + 0x0a;
683 port->lbal_addr = base + 0x0f;
684 port->lbam_addr = base + 0x10;
685 port->lbah_addr = base + 0x15;
686 port->device_addr = base + 0x1a;
688 port->status_addr = base + 0x1f;
689 port->altstatus_addr =
690 port->ctl_addr = base + 0x81a;
705 static const unsigned long cmd_offset[] = { 0x17c0, 0x15c0 };
706 static const unsigned long bmdma_offset[] = { 0x1000, 0x1008 };
709 { &pdc2027x_port_info[board_idx],
NULL };
741 for (i = 0; i < 2; i++) {
744 pdc_ata_setup_port(&ap->ioaddr, mmio_base +
cmd_offset[i]);
745 ap->ioaddr.bmdma_addr = mmio_base + bmdma_offset[
i];
754 if (pdc_hardware_init(host, board_idx) != 0)
763 static int pdc2027x_reinit_one(
struct pci_dev *pdev)
766 unsigned int board_idx;
769 rc = ata_pci_device_do_resume(pdev);
779 if (pdc_hardware_init(host, board_idx))
782 ata_host_resume(host);