2 * pata_amd.c - AMD PATA for new ATA layer
3 * (C) 2005-2006 Red Hat Inc
4 * Alan Cox <alan@redhat.com>
6 * Based on pata-sil680. Errata information is taken from data sheets
7 * and the amd74xx.c driver by Vojtech Pavlik. Nvidia SATA devices are
8 * claimed by sata-nv.c.
11 * Variable system clock when/if it makes sense
12 * Power management on ports
15 * Documentation publically available.
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/pci.h>
21 #include <linux/init.h>
22 #include <linux/blkdev.h>
23 #include <linux/delay.h>
24 #include <scsi/scsi_host.h>
25 #include <linux/libata.h>
27 #define DRV_NAME "pata_amd"
28 #define DRV_VERSION "0.2.4"
31 * timing_setup - shared timing computation and load
32 * @ap: ATA port being set up
33 * @adev: drive being configured
34 * @offset: port offset
35 * @speed: target speed
36 * @clock: clock multiplier (number of times 33MHz for this part)
38 * Perform the actual timing set up for Nvidia or AMD PATA devices.
39 * The actual devices vary so they all call into this helper function
40 * providing the clock multipler and offset (because AMD and Nvidia put
41 * the ports at different locations).
44 static void timing_setup(struct ata_port *ap, struct ata_device *adev, int offset, int speed, int clock)
46 static const unsigned char amd_cyc2udma[] = {
47 6, 6, 5, 4, 0, 1, 1, 2, 2, 3, 3, 3, 3, 3, 3, 7
50 struct pci_dev *pdev = to_pci_dev(ap->host->dev);
51 struct ata_device *peer = ata_dev_pair(adev);
52 int dn = ap->port_no * 2 + adev->devno;
53 struct ata_timing at, apeer;
55 const int amd_clock = 33333; /* KHz. */
58 T = 1000000000 / amd_clock;
59 UT = T / min_t(int, max_t(int, clock, 1), 2);
61 if (ata_timing_compute(adev, speed, &at, T, UT) < 0) {
62 dev_printk(KERN_ERR, &pdev->dev, "unknown mode %d.\n", speed);
67 /* This may be over conservative */
69 ata_timing_compute(peer, peer->dma_mode, &apeer, T, UT);
70 ata_timing_merge(&apeer, &at, &at, ATA_TIMING_8BIT);
72 ata_timing_compute(peer, peer->pio_mode, &apeer, T, UT);
73 ata_timing_merge(&apeer, &at, &at, ATA_TIMING_8BIT);
76 if (speed == XFER_UDMA_5 && amd_clock <= 33333) at.udma = 1;
77 if (speed == XFER_UDMA_6 && amd_clock <= 33333) at.udma = 15;
80 * Now do the setup work
83 /* Configure the address set up timing */
84 pci_read_config_byte(pdev, offset + 0x0C, &t);
85 t = (t & ~(3 << ((3 - dn) << 1))) | ((FIT(at.setup, 1, 4) - 1) << ((3 - dn) << 1));
86 pci_write_config_byte(pdev, offset + 0x0C , t);
88 /* Configure the 8bit I/O timing */
89 pci_write_config_byte(pdev, offset + 0x0E + (1 - (dn >> 1)),
90 ((FIT(at.act8b, 1, 16) - 1) << 4) | (FIT(at.rec8b, 1, 16) - 1));
93 pci_write_config_byte(pdev, offset + 0x08 + (3 - dn),
94 ((FIT(at.active, 1, 16) - 1) << 4) | (FIT(at.recover, 1, 16) - 1));
98 t = at.udma ? (0xc0 | (FIT(at.udma, 2, 5) - 2)) : 0x03;
102 t = at.udma ? (0xc0 | amd_cyc2udma[FIT(at.udma, 2, 10)]) : 0x03;
106 t = at.udma ? (0xc0 | amd_cyc2udma[FIT(at.udma, 1, 10)]) : 0x03;
110 t = at.udma ? (0xc0 | amd_cyc2udma[FIT(at.udma, 1, 15)]) : 0x03;
118 pci_write_config_byte(pdev, offset + 0x10 + (3 - dn), t);
122 * amd_probe_init - cable detection
125 * Perform cable detection. The BIOS stores this in PCI config
129 static int amd_pre_reset(struct ata_port *ap)
131 static const u32 bitmask[2] = {0x03, 0xC0};
132 static const struct pci_bits amd_enable_bits[] = {
133 { 0x40, 1, 0x02, 0x02 },
134 { 0x40, 1, 0x01, 0x01 }
137 struct pci_dev *pdev = to_pci_dev(ap->host->dev);
140 if (!pci_test_config_bits(pdev, &amd_enable_bits[ap->port_no]))
143 pci_read_config_byte(pdev, 0x42, &ata66);
144 if (ata66 & bitmask[ap->port_no])
145 ap->cbl = ATA_CBL_PATA80;
147 ap->cbl = ATA_CBL_PATA40;
148 return ata_std_prereset(ap);
152 static void amd_error_handler(struct ata_port *ap)
154 return ata_bmdma_drive_eh(ap, amd_pre_reset,
155 ata_std_softreset, NULL,
159 static int amd_early_pre_reset(struct ata_port *ap)
161 struct pci_dev *pdev = to_pci_dev(ap->host->dev);
162 static struct pci_bits amd_enable_bits[] = {
163 { 0x40, 1, 0x02, 0x02 },
164 { 0x40, 1, 0x01, 0x01 }
167 if (!pci_test_config_bits(pdev, &amd_enable_bits[ap->port_no]))
170 /* No host side cable detection */
171 ap->cbl = ATA_CBL_PATA80;
172 return ata_std_prereset(ap);
176 static void amd_early_error_handler(struct ata_port *ap)
178 ata_bmdma_drive_eh(ap, amd_early_pre_reset,
179 ata_std_softreset, NULL,
184 * amd33_set_piomode - set initial PIO mode data
188 * Program the AMD registers for PIO mode.
191 static void amd33_set_piomode(struct ata_port *ap, struct ata_device *adev)
193 timing_setup(ap, adev, 0x40, adev->pio_mode, 1);
196 static void amd66_set_piomode(struct ata_port *ap, struct ata_device *adev)
198 timing_setup(ap, adev, 0x40, adev->pio_mode, 2);
201 static void amd100_set_piomode(struct ata_port *ap, struct ata_device *adev)
203 timing_setup(ap, adev, 0x40, adev->pio_mode, 3);
206 static void amd133_set_piomode(struct ata_port *ap, struct ata_device *adev)
208 timing_setup(ap, adev, 0x40, adev->pio_mode, 4);
212 * amd33_set_dmamode - set initial DMA mode data
216 * Program the MWDMA/UDMA modes for the AMD and Nvidia
220 static void amd33_set_dmamode(struct ata_port *ap, struct ata_device *adev)
222 timing_setup(ap, adev, 0x40, adev->dma_mode, 1);
225 static void amd66_set_dmamode(struct ata_port *ap, struct ata_device *adev)
227 timing_setup(ap, adev, 0x40, adev->dma_mode, 2);
230 static void amd100_set_dmamode(struct ata_port *ap, struct ata_device *adev)
232 timing_setup(ap, adev, 0x40, adev->dma_mode, 3);
235 static void amd133_set_dmamode(struct ata_port *ap, struct ata_device *adev)
237 timing_setup(ap, adev, 0x40, adev->dma_mode, 4);
242 * nv_probe_init - cable detection
245 * Perform cable detection. The BIOS stores this in PCI config
249 static int nv_pre_reset(struct ata_port *ap) {
250 static const u8 bitmask[2] = {0x03, 0xC0};
251 static const struct pci_bits nv_enable_bits[] = {
252 { 0x50, 1, 0x02, 0x02 },
253 { 0x50, 1, 0x01, 0x01 }
256 struct pci_dev *pdev = to_pci_dev(ap->host->dev);
260 if (!pci_test_config_bits(pdev, &nv_enable_bits[ap->port_no]))
263 pci_read_config_byte(pdev, 0x52, &ata66);
264 if (ata66 & bitmask[ap->port_no])
265 ap->cbl = ATA_CBL_PATA80;
267 ap->cbl = ATA_CBL_PATA40;
269 /* We now have to double check because the Nvidia boxes BIOS
270 doesn't always set the cable bits but does set mode bits */
272 pci_read_config_word(pdev, 0x62 - 2 * ap->port_no, &udma);
273 if ((udma & 0xC4) == 0xC4 || (udma & 0xC400) == 0xC400)
274 ap->cbl = ATA_CBL_PATA80;
275 return ata_std_prereset(ap);
278 static void nv_error_handler(struct ata_port *ap)
280 ata_bmdma_drive_eh(ap, nv_pre_reset,
281 ata_std_softreset, NULL,
285 * nv100_set_piomode - set initial PIO mode data
289 * Program the AMD registers for PIO mode.
292 static void nv100_set_piomode(struct ata_port *ap, struct ata_device *adev)
294 timing_setup(ap, adev, 0x50, adev->pio_mode, 3);
297 static void nv133_set_piomode(struct ata_port *ap, struct ata_device *adev)
299 timing_setup(ap, adev, 0x50, adev->pio_mode, 4);
303 * nv100_set_dmamode - set initial DMA mode data
307 * Program the MWDMA/UDMA modes for the AMD and Nvidia
311 static void nv100_set_dmamode(struct ata_port *ap, struct ata_device *adev)
313 timing_setup(ap, adev, 0x50, adev->dma_mode, 3);
316 static void nv133_set_dmamode(struct ata_port *ap, struct ata_device *adev)
318 timing_setup(ap, adev, 0x50, adev->dma_mode, 4);
321 static struct scsi_host_template amd_sht = {
322 .module = THIS_MODULE,
324 .ioctl = ata_scsi_ioctl,
325 .queuecommand = ata_scsi_queuecmd,
326 .can_queue = ATA_DEF_QUEUE,
327 .this_id = ATA_SHT_THIS_ID,
328 .sg_tablesize = LIBATA_MAX_PRD,
329 .max_sectors = ATA_MAX_SECTORS,
330 .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
331 .emulated = ATA_SHT_EMULATED,
332 .use_clustering = ATA_SHT_USE_CLUSTERING,
333 .proc_name = DRV_NAME,
334 .dma_boundary = ATA_DMA_BOUNDARY,
335 .slave_configure = ata_scsi_slave_config,
336 .slave_destroy = ata_scsi_slave_destroy,
337 .bios_param = ata_std_bios_param,
340 static struct ata_port_operations amd33_port_ops = {
341 .port_disable = ata_port_disable,
342 .set_piomode = amd33_set_piomode,
343 .set_dmamode = amd33_set_dmamode,
344 .mode_filter = ata_pci_default_filter,
345 .tf_load = ata_tf_load,
346 .tf_read = ata_tf_read,
347 .check_status = ata_check_status,
348 .exec_command = ata_exec_command,
349 .dev_select = ata_std_dev_select,
351 .freeze = ata_bmdma_freeze,
352 .thaw = ata_bmdma_thaw,
353 .error_handler = amd_early_error_handler,
354 .post_internal_cmd = ata_bmdma_post_internal_cmd,
356 .bmdma_setup = ata_bmdma_setup,
357 .bmdma_start = ata_bmdma_start,
358 .bmdma_stop = ata_bmdma_stop,
359 .bmdma_status = ata_bmdma_status,
361 .qc_prep = ata_qc_prep,
362 .qc_issue = ata_qc_issue_prot,
364 .data_xfer = ata_pio_data_xfer,
366 .irq_handler = ata_interrupt,
367 .irq_clear = ata_bmdma_irq_clear,
369 .port_start = ata_port_start,
370 .port_stop = ata_port_stop,
371 .host_stop = ata_host_stop
374 static struct ata_port_operations amd66_port_ops = {
375 .port_disable = ata_port_disable,
376 .set_piomode = amd66_set_piomode,
377 .set_dmamode = amd66_set_dmamode,
378 .mode_filter = ata_pci_default_filter,
379 .tf_load = ata_tf_load,
380 .tf_read = ata_tf_read,
381 .check_status = ata_check_status,
382 .exec_command = ata_exec_command,
383 .dev_select = ata_std_dev_select,
385 .freeze = ata_bmdma_freeze,
386 .thaw = ata_bmdma_thaw,
387 .error_handler = amd_early_error_handler,
388 .post_internal_cmd = ata_bmdma_post_internal_cmd,
390 .bmdma_setup = ata_bmdma_setup,
391 .bmdma_start = ata_bmdma_start,
392 .bmdma_stop = ata_bmdma_stop,
393 .bmdma_status = ata_bmdma_status,
395 .qc_prep = ata_qc_prep,
396 .qc_issue = ata_qc_issue_prot,
398 .data_xfer = ata_pio_data_xfer,
400 .irq_handler = ata_interrupt,
401 .irq_clear = ata_bmdma_irq_clear,
403 .port_start = ata_port_start,
404 .port_stop = ata_port_stop,
405 .host_stop = ata_host_stop
408 static struct ata_port_operations amd100_port_ops = {
409 .port_disable = ata_port_disable,
410 .set_piomode = amd100_set_piomode,
411 .set_dmamode = amd100_set_dmamode,
412 .mode_filter = ata_pci_default_filter,
413 .tf_load = ata_tf_load,
414 .tf_read = ata_tf_read,
415 .check_status = ata_check_status,
416 .exec_command = ata_exec_command,
417 .dev_select = ata_std_dev_select,
419 .freeze = ata_bmdma_freeze,
420 .thaw = ata_bmdma_thaw,
421 .error_handler = amd_error_handler,
422 .post_internal_cmd = ata_bmdma_post_internal_cmd,
424 .bmdma_setup = ata_bmdma_setup,
425 .bmdma_start = ata_bmdma_start,
426 .bmdma_stop = ata_bmdma_stop,
427 .bmdma_status = ata_bmdma_status,
429 .qc_prep = ata_qc_prep,
430 .qc_issue = ata_qc_issue_prot,
432 .data_xfer = ata_pio_data_xfer,
434 .irq_handler = ata_interrupt,
435 .irq_clear = ata_bmdma_irq_clear,
437 .port_start = ata_port_start,
438 .port_stop = ata_port_stop,
439 .host_stop = ata_host_stop
442 static struct ata_port_operations amd133_port_ops = {
443 .port_disable = ata_port_disable,
444 .set_piomode = amd133_set_piomode,
445 .set_dmamode = amd133_set_dmamode,
446 .mode_filter = ata_pci_default_filter,
447 .tf_load = ata_tf_load,
448 .tf_read = ata_tf_read,
449 .check_status = ata_check_status,
450 .exec_command = ata_exec_command,
451 .dev_select = ata_std_dev_select,
453 .freeze = ata_bmdma_freeze,
454 .thaw = ata_bmdma_thaw,
455 .error_handler = amd_error_handler,
456 .post_internal_cmd = ata_bmdma_post_internal_cmd,
458 .bmdma_setup = ata_bmdma_setup,
459 .bmdma_start = ata_bmdma_start,
460 .bmdma_stop = ata_bmdma_stop,
461 .bmdma_status = ata_bmdma_status,
463 .qc_prep = ata_qc_prep,
464 .qc_issue = ata_qc_issue_prot,
466 .data_xfer = ata_pio_data_xfer,
468 .irq_handler = ata_interrupt,
469 .irq_clear = ata_bmdma_irq_clear,
471 .port_start = ata_port_start,
472 .port_stop = ata_port_stop,
473 .host_stop = ata_host_stop
476 static struct ata_port_operations nv100_port_ops = {
477 .port_disable = ata_port_disable,
478 .set_piomode = nv100_set_piomode,
479 .set_dmamode = nv100_set_dmamode,
480 .mode_filter = ata_pci_default_filter,
481 .tf_load = ata_tf_load,
482 .tf_read = ata_tf_read,
483 .check_status = ata_check_status,
484 .exec_command = ata_exec_command,
485 .dev_select = ata_std_dev_select,
487 .freeze = ata_bmdma_freeze,
488 .thaw = ata_bmdma_thaw,
489 .error_handler = nv_error_handler,
490 .post_internal_cmd = ata_bmdma_post_internal_cmd,
492 .bmdma_setup = ata_bmdma_setup,
493 .bmdma_start = ata_bmdma_start,
494 .bmdma_stop = ata_bmdma_stop,
495 .bmdma_status = ata_bmdma_status,
497 .qc_prep = ata_qc_prep,
498 .qc_issue = ata_qc_issue_prot,
500 .data_xfer = ata_pio_data_xfer,
502 .irq_handler = ata_interrupt,
503 .irq_clear = ata_bmdma_irq_clear,
505 .port_start = ata_port_start,
506 .port_stop = ata_port_stop,
507 .host_stop = ata_host_stop
510 static struct ata_port_operations nv133_port_ops = {
511 .port_disable = ata_port_disable,
512 .set_piomode = nv133_set_piomode,
513 .set_dmamode = nv133_set_dmamode,
514 .mode_filter = ata_pci_default_filter,
515 .tf_load = ata_tf_load,
516 .tf_read = ata_tf_read,
517 .check_status = ata_check_status,
518 .exec_command = ata_exec_command,
519 .dev_select = ata_std_dev_select,
521 .freeze = ata_bmdma_freeze,
522 .thaw = ata_bmdma_thaw,
523 .error_handler = nv_error_handler,
524 .post_internal_cmd = ata_bmdma_post_internal_cmd,
526 .bmdma_setup = ata_bmdma_setup,
527 .bmdma_start = ata_bmdma_start,
528 .bmdma_stop = ata_bmdma_stop,
529 .bmdma_status = ata_bmdma_status,
531 .qc_prep = ata_qc_prep,
532 .qc_issue = ata_qc_issue_prot,
534 .data_xfer = ata_pio_data_xfer,
536 .irq_handler = ata_interrupt,
537 .irq_clear = ata_bmdma_irq_clear,
539 .port_start = ata_port_start,
540 .port_stop = ata_port_stop,
541 .host_stop = ata_host_stop
544 static int amd_init_one(struct pci_dev *pdev, const struct pci_device_id *id)
546 static struct ata_port_info info[10] = {
549 .flags = ATA_FLAG_SLAVE_POSS | ATA_FLAG_SRST,
551 .mwdma_mask = 0x07, /* No SWDMA */
552 .udma_mask = 0x07, /* UDMA 33 */
553 .port_ops = &amd33_port_ops
555 { /* 1: Early AMD7409 - no swdma */
557 .flags = ATA_FLAG_SLAVE_POSS | ATA_FLAG_SRST,
560 .udma_mask = 0x1f, /* UDMA 66 */
561 .port_ops = &amd66_port_ops
563 { /* 2: AMD 7409, no swdma errata */
565 .flags = ATA_FLAG_SLAVE_POSS | ATA_FLAG_SRST,
568 .udma_mask = 0x1f, /* UDMA 66 */
569 .port_ops = &amd66_port_ops
573 .flags = ATA_FLAG_SLAVE_POSS | ATA_FLAG_SRST,
576 .udma_mask = 0x3f, /* UDMA 100 */
577 .port_ops = &amd100_port_ops
581 .flags = ATA_FLAG_SLAVE_POSS | ATA_FLAG_SRST,
584 .udma_mask = 0x3f, /* UDMA 100 */
585 .port_ops = &amd100_port_ops
589 .flags = ATA_FLAG_SLAVE_POSS | ATA_FLAG_SRST,
592 .udma_mask = 0x7f, /* UDMA 133, no swdma */
593 .port_ops = &amd133_port_ops
595 { /* 6: AMD 8111 UDMA 100 (Serenade) */
597 .flags = ATA_FLAG_SLAVE_POSS | ATA_FLAG_SRST,
600 .udma_mask = 0x3f, /* UDMA 100, no swdma */
601 .port_ops = &amd133_port_ops
603 { /* 7: Nvidia Nforce */
605 .flags = ATA_FLAG_SLAVE_POSS | ATA_FLAG_SRST,
608 .udma_mask = 0x3f, /* UDMA 100 */
609 .port_ops = &nv100_port_ops
611 { /* 8: Nvidia Nforce2 and later */
613 .flags = ATA_FLAG_SLAVE_POSS | ATA_FLAG_SRST,
616 .udma_mask = 0x7f, /* UDMA 133, no swdma */
617 .port_ops = &nv133_port_ops
619 { /* 9: AMD CS5536 (Geode companion) */
621 .flags = ATA_FLAG_SLAVE_POSS | ATA_FLAG_SRST,
624 .udma_mask = 0x3f, /* UDMA 100 */
625 .port_ops = &amd100_port_ops
628 static struct ata_port_info *port_info[2];
629 static int printed_version;
630 int type = id->driver_data;
634 if (!printed_version++)
635 dev_printk(KERN_DEBUG, &pdev->dev, "version " DRV_VERSION "\n");
637 pci_read_config_byte(pdev, PCI_REVISION_ID, &rev);
638 pci_read_config_byte(pdev, 0x41, &fifo);
640 /* Check for AMD7409 without swdma errata and if found adjust type */
641 if (type == 1 && rev > 0x7)
644 /* Check for AMD7411 */
647 pci_write_config_byte(pdev, 0x41, fifo & 0x0F);
649 pci_write_config_byte(pdev, 0x41, fifo | 0xF0);
652 if (type == 5 && pdev->subsystem_vendor == PCI_VENDOR_ID_AMD &&
653 pdev->subsystem_device == PCI_DEVICE_ID_AMD_SERENADE)
654 type = 6; /* UDMA 100 only */
657 ata_pci_clear_simplex(pdev);
661 port_info[0] = port_info[1] = &info[type];
662 return ata_pci_init_one(pdev, port_info, 2);
665 static const struct pci_device_id amd[] = {
666 { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_COBRA_7401), 0 },
667 { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_VIPER_7409), 1 },
668 { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_VIPER_7411), 3 },
669 { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_OPUS_7441), 4 },
670 { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_8111_IDE), 5 },
671 { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_IDE), 7 },
672 { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE2_IDE), 8 },
673 { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE2S_IDE), 8 },
674 { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE3_IDE), 8 },
675 { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE3S_IDE), 8 },
676 { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_CK804_IDE), 8 },
677 { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP04_IDE), 8 },
678 { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP51_IDE), 8 },
679 { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP55_IDE), 8 },
680 { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP61_IDE), 8 },
681 { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP65_IDE), 8 },
682 { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP67_IDE), 8 },
683 { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_CS5536_IDE), 9 },
688 static struct pci_driver amd_pci_driver = {
691 .probe = amd_init_one,
692 .remove = ata_pci_remove_one
695 static int __init amd_init(void)
697 return pci_register_driver(&amd_pci_driver);
700 static void __exit amd_exit(void)
702 pci_unregister_driver(&amd_pci_driver);
705 MODULE_AUTHOR("Alan Cox");
706 MODULE_DESCRIPTION("low-level driver for AMD PATA IDE");
707 MODULE_LICENSE("GPL");
708 MODULE_DEVICE_TABLE(pci, amd);
709 MODULE_VERSION(DRV_VERSION);
711 module_init(amd_init);
712 module_exit(amd_exit);