atiixp.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2003 ATI Inc. <hyu@ati.com>
  4. * Copyright (C) 2004,2007 Bartlomiej Zolnierkiewicz
  5. */
  6. #include <linux/types.h>
  7. #include <linux/module.h>
  8. #include <linux/kernel.h>
  9. #include <linux/pci.h>
  10. #include <linux/ide.h>
  11. #include <linux/init.h>
  12. #define DRV_NAME "atiixp"
  13. #define ATIIXP_IDE_PIO_TIMING 0x40
  14. #define ATIIXP_IDE_MDMA_TIMING 0x44
  15. #define ATIIXP_IDE_PIO_CONTROL 0x48
  16. #define ATIIXP_IDE_PIO_MODE 0x4a
  17. #define ATIIXP_IDE_UDMA_CONTROL 0x54
  18. #define ATIIXP_IDE_UDMA_MODE 0x56
  19. struct atiixp_ide_timing {
  20. u8 command_width;
  21. u8 recover_width;
  22. };
  23. static struct atiixp_ide_timing pio_timing[] = {
  24. { 0x05, 0x0d },
  25. { 0x04, 0x07 },
  26. { 0x03, 0x04 },
  27. { 0x02, 0x02 },
  28. { 0x02, 0x00 },
  29. };
  30. static struct atiixp_ide_timing mdma_timing[] = {
  31. { 0x07, 0x07 },
  32. { 0x02, 0x01 },
  33. { 0x02, 0x00 },
  34. };
  35. static DEFINE_SPINLOCK(atiixp_lock);
  36. /**
  37. * atiixp_set_pio_mode - set host controller for PIO mode
  38. * @hwif: port
  39. * @drive: drive
  40. *
  41. * Set the interface PIO mode.
  42. */
  43. static void atiixp_set_pio_mode(ide_hwif_t *hwif, ide_drive_t *drive)
  44. {
  45. struct pci_dev *dev = to_pci_dev(hwif->dev);
  46. unsigned long flags;
  47. int timing_shift = (drive->dn ^ 1) * 8;
  48. u32 pio_timing_data;
  49. u16 pio_mode_data;
  50. const u8 pio = drive->pio_mode - XFER_PIO_0;
  51. spin_lock_irqsave(&atiixp_lock, flags);
  52. pci_read_config_word(dev, ATIIXP_IDE_PIO_MODE, &pio_mode_data);
  53. pio_mode_data &= ~(0x07 << (drive->dn * 4));
  54. pio_mode_data |= (pio << (drive->dn * 4));
  55. pci_write_config_word(dev, ATIIXP_IDE_PIO_MODE, pio_mode_data);
  56. pci_read_config_dword(dev, ATIIXP_IDE_PIO_TIMING, &pio_timing_data);
  57. pio_timing_data &= ~(0xff << timing_shift);
  58. pio_timing_data |= (pio_timing[pio].recover_width << timing_shift) |
  59. (pio_timing[pio].command_width << (timing_shift + 4));
  60. pci_write_config_dword(dev, ATIIXP_IDE_PIO_TIMING, pio_timing_data);
  61. spin_unlock_irqrestore(&atiixp_lock, flags);
  62. }
  63. /**
  64. * atiixp_set_dma_mode - set host controller for DMA mode
  65. * @hwif: port
  66. * @drive: drive
  67. *
  68. * Set a ATIIXP host controller to the desired DMA mode. This involves
  69. * programming the right timing data into the PCI configuration space.
  70. */
  71. static void atiixp_set_dma_mode(ide_hwif_t *hwif, ide_drive_t *drive)
  72. {
  73. struct pci_dev *dev = to_pci_dev(hwif->dev);
  74. unsigned long flags;
  75. int timing_shift = (drive->dn ^ 1) * 8;
  76. u32 tmp32;
  77. u16 tmp16;
  78. u16 udma_ctl = 0;
  79. const u8 speed = drive->dma_mode;
  80. spin_lock_irqsave(&atiixp_lock, flags);
  81. pci_read_config_word(dev, ATIIXP_IDE_UDMA_CONTROL, &udma_ctl);
  82. if (speed >= XFER_UDMA_0) {
  83. pci_read_config_word(dev, ATIIXP_IDE_UDMA_MODE, &tmp16);
  84. tmp16 &= ~(0x07 << (drive->dn * 4));
  85. tmp16 |= ((speed & 0x07) << (drive->dn * 4));
  86. pci_write_config_word(dev, ATIIXP_IDE_UDMA_MODE, tmp16);
  87. udma_ctl |= (1 << drive->dn);
  88. } else if (speed >= XFER_MW_DMA_0) {
  89. u8 i = speed & 0x03;
  90. pci_read_config_dword(dev, ATIIXP_IDE_MDMA_TIMING, &tmp32);
  91. tmp32 &= ~(0xff << timing_shift);
  92. tmp32 |= (mdma_timing[i].recover_width << timing_shift) |
  93. (mdma_timing[i].command_width << (timing_shift + 4));
  94. pci_write_config_dword(dev, ATIIXP_IDE_MDMA_TIMING, tmp32);
  95. udma_ctl &= ~(1 << drive->dn);
  96. }
  97. pci_write_config_word(dev, ATIIXP_IDE_UDMA_CONTROL, udma_ctl);
  98. spin_unlock_irqrestore(&atiixp_lock, flags);
  99. }
  100. static u8 atiixp_cable_detect(ide_hwif_t *hwif)
  101. {
  102. struct pci_dev *pdev = to_pci_dev(hwif->dev);
  103. u8 udma_mode = 0, ch = hwif->channel;
  104. pci_read_config_byte(pdev, ATIIXP_IDE_UDMA_MODE + ch, &udma_mode);
  105. if ((udma_mode & 0x07) >= 0x04 || (udma_mode & 0x70) >= 0x40)
  106. return ATA_CBL_PATA80;
  107. else
  108. return ATA_CBL_PATA40;
  109. }
  110. static const struct ide_port_ops atiixp_port_ops = {
  111. .set_pio_mode = atiixp_set_pio_mode,
  112. .set_dma_mode = atiixp_set_dma_mode,
  113. .cable_detect = atiixp_cable_detect,
  114. };
  115. static const struct ide_port_info atiixp_pci_info[] = {
  116. { /* 0: IXP200/300/400/700 */
  117. .name = DRV_NAME,
  118. .enablebits = {{0x48,0x01,0x00}, {0x48,0x08,0x00}},
  119. .port_ops = &atiixp_port_ops,
  120. .pio_mask = ATA_PIO4,
  121. .mwdma_mask = ATA_MWDMA2,
  122. .udma_mask = ATA_UDMA5,
  123. },
  124. { /* 1: IXP600 */
  125. .name = DRV_NAME,
  126. .enablebits = {{0x48,0x01,0x00}, {0x00,0x00,0x00}},
  127. .port_ops = &atiixp_port_ops,
  128. .host_flags = IDE_HFLAG_SINGLE,
  129. .pio_mask = ATA_PIO4,
  130. .mwdma_mask = ATA_MWDMA2,
  131. .udma_mask = ATA_UDMA5,
  132. },
  133. };
  134. /**
  135. * atiixp_init_one - called when a ATIIXP is found
  136. * @dev: the atiixp device
  137. * @id: the matching pci id
  138. *
  139. * Called when the PCI registration layer (or the IDE initialization)
  140. * finds a device matching our IDE device tables.
  141. */
  142. static int atiixp_init_one(struct pci_dev *dev, const struct pci_device_id *id)
  143. {
  144. return ide_pci_init_one(dev, &atiixp_pci_info[id->driver_data], NULL);
  145. }
  146. static const struct pci_device_id atiixp_pci_tbl[] = {
  147. { PCI_VDEVICE(ATI, PCI_DEVICE_ID_ATI_IXP200_IDE), 0 },
  148. { PCI_VDEVICE(ATI, PCI_DEVICE_ID_ATI_IXP300_IDE), 0 },
  149. { PCI_VDEVICE(ATI, PCI_DEVICE_ID_ATI_IXP400_IDE), 0 },
  150. { PCI_VDEVICE(ATI, PCI_DEVICE_ID_ATI_IXP600_IDE), 1 },
  151. { PCI_VDEVICE(ATI, PCI_DEVICE_ID_ATI_IXP700_IDE), 0 },
  152. { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_HUDSON2_IDE), 0 },
  153. { 0, },
  154. };
  155. MODULE_DEVICE_TABLE(pci, atiixp_pci_tbl);
  156. static struct pci_driver atiixp_pci_driver = {
  157. .name = "ATIIXP_IDE",
  158. .id_table = atiixp_pci_tbl,
  159. .probe = atiixp_init_one,
  160. .remove = ide_pci_remove,
  161. .suspend = ide_pci_suspend,
  162. .resume = ide_pci_resume,
  163. };
  164. static int __init atiixp_ide_init(void)
  165. {
  166. return ide_pci_register_driver(&atiixp_pci_driver);
  167. }
  168. static void __exit atiixp_ide_exit(void)
  169. {
  170. pci_unregister_driver(&atiixp_pci_driver);
  171. }
  172. module_init(atiixp_ide_init);
  173. module_exit(atiixp_ide_exit);
  174. MODULE_AUTHOR("HUI YU");
  175. MODULE_DESCRIPTION("PCI driver module for ATI IXP IDE");
  176. MODULE_LICENSE("GPL");