amd74xx.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /*
  2. * AMD 755/756/766/8111 and nVidia nForce/2/2s/3/3s/CK804/MCP04
  3. * IDE driver for Linux.
  4. *
  5. * Copyright (c) 2000-2002 Vojtech Pavlik
  6. * Copyright (c) 2007-2010 Bartlomiej Zolnierkiewicz
  7. *
  8. * Based on the work of:
  9. * Andre Hedrick
  10. */
  11. /*
  12. * This program is free software; you can redistribute it and/or modify it
  13. * under the terms of the GNU General Public License version 2 as published by
  14. * the Free Software Foundation.
  15. */
  16. #include <linux/module.h>
  17. #include <linux/kernel.h>
  18. #include <linux/pci.h>
  19. #include <linux/init.h>
  20. #include <linux/ide.h>
  21. #define DRV_NAME "amd74xx"
  22. enum {
  23. AMD_IDE_CONFIG = 0x41,
  24. AMD_CABLE_DETECT = 0x42,
  25. AMD_DRIVE_TIMING = 0x48,
  26. AMD_8BIT_TIMING = 0x4e,
  27. AMD_ADDRESS_SETUP = 0x4c,
  28. AMD_UDMA_TIMING = 0x50,
  29. };
  30. static unsigned int amd_80w;
  31. static unsigned int amd_clock;
  32. static char *amd_dma[] = { "16", "25", "33", "44", "66", "100", "133" };
  33. static unsigned char amd_cyc2udma[] = { 6, 6, 5, 4, 0, 1, 1, 2, 2, 3, 3, 3, 3, 3, 3, 7 };
  34. static inline u8 amd_offset(struct pci_dev *dev)
  35. {
  36. return (dev->vendor == PCI_VENDOR_ID_NVIDIA) ? 0x10 : 0;
  37. }
  38. /*
  39. * amd_set_speed() writes timing values to the chipset registers
  40. */
  41. static void amd_set_speed(struct pci_dev *dev, u8 dn, u8 udma_mask,
  42. struct ide_timing *timing)
  43. {
  44. u8 t = 0, offset = amd_offset(dev);
  45. pci_read_config_byte(dev, AMD_ADDRESS_SETUP + offset, &t);
  46. t = (t & ~(3 << ((3 - dn) << 1))) | ((clamp_val(timing->setup, 1, 4) - 1) << ((3 - dn) << 1));
  47. pci_write_config_byte(dev, AMD_ADDRESS_SETUP + offset, t);
  48. pci_write_config_byte(dev, AMD_8BIT_TIMING + offset + (1 - (dn >> 1)),
  49. ((clamp_val(timing->act8b, 1, 16) - 1) << 4) | (clamp_val(timing->rec8b, 1, 16) - 1));
  50. pci_write_config_byte(dev, AMD_DRIVE_TIMING + offset + (3 - dn),
  51. ((clamp_val(timing->active, 1, 16) - 1) << 4) | (clamp_val(timing->recover, 1, 16) - 1));
  52. switch (udma_mask) {
  53. case ATA_UDMA2: t = timing->udma ? (0xc0 | (clamp_val(timing->udma, 2, 5) - 2)) : 0x03; break;
  54. case ATA_UDMA4: t = timing->udma ? (0xc0 | amd_cyc2udma[clamp_val(timing->udma, 2, 10)]) : 0x03; break;
  55. case ATA_UDMA5: t = timing->udma ? (0xc0 | amd_cyc2udma[clamp_val(timing->udma, 1, 10)]) : 0x03; break;
  56. case ATA_UDMA6: t = timing->udma ? (0xc0 | amd_cyc2udma[clamp_val(timing->udma, 1, 15)]) : 0x03; break;
  57. default: return;
  58. }
  59. if (timing->udma)
  60. pci_write_config_byte(dev, AMD_UDMA_TIMING + offset + 3 - dn, t);
  61. }
  62. /*
  63. * amd_set_drive() computes timing values and configures the chipset
  64. * to a desired transfer mode. It also can be called by upper layers.
  65. */
  66. static void amd_set_drive(ide_hwif_t *hwif, ide_drive_t *drive)
  67. {
  68. struct pci_dev *dev = to_pci_dev(hwif->dev);
  69. ide_drive_t *peer = ide_get_pair_dev(drive);
  70. struct ide_timing t, p;
  71. int T, UT;
  72. u8 udma_mask = hwif->ultra_mask;
  73. const u8 speed = drive->dma_mode;
  74. T = 1000000000 / amd_clock;
  75. UT = (udma_mask == ATA_UDMA2) ? T : (T / 2);
  76. ide_timing_compute(drive, speed, &t, T, UT);
  77. if (peer) {
  78. ide_timing_compute(peer, peer->pio_mode, &p, T, UT);
  79. ide_timing_merge(&p, &t, &t, IDE_TIMING_8BIT);
  80. }
  81. if (speed == XFER_UDMA_5 && amd_clock <= 33333) t.udma = 1;
  82. if (speed == XFER_UDMA_6 && amd_clock <= 33333) t.udma = 15;
  83. amd_set_speed(dev, drive->dn, udma_mask, &t);
  84. }
  85. /*
  86. * amd_set_pio_mode() is a callback from upper layers for PIO-only tuning.
  87. */
  88. static void amd_set_pio_mode(ide_hwif_t *hwif, ide_drive_t *drive)
  89. {
  90. drive->dma_mode = drive->pio_mode;
  91. amd_set_drive(hwif, drive);
  92. }
  93. static void amd7409_cable_detect(struct pci_dev *dev)
  94. {
  95. /* no host side cable detection */
  96. amd_80w = 0x03;
  97. }
  98. static void amd7411_cable_detect(struct pci_dev *dev)
  99. {
  100. int i;
  101. u32 u = 0;
  102. u8 t = 0, offset = amd_offset(dev);
  103. pci_read_config_byte(dev, AMD_CABLE_DETECT + offset, &t);
  104. pci_read_config_dword(dev, AMD_UDMA_TIMING + offset, &u);
  105. amd_80w = ((t & 0x3) ? 1 : 0) | ((t & 0xc) ? 2 : 0);
  106. for (i = 24; i >= 0; i -= 8)
  107. if (((u >> i) & 4) && !(amd_80w & (1 << (1 - (i >> 4))))) {
  108. printk(KERN_WARNING DRV_NAME " %s: BIOS didn't set "
  109. "cable bits correctly. Enabling workaround.\n",
  110. pci_name(dev));
  111. amd_80w |= (1 << (1 - (i >> 4)));
  112. }
  113. }
  114. /*
  115. * The initialization callback. Initialize drive independent registers.
  116. */
  117. static int init_chipset_amd74xx(struct pci_dev *dev)
  118. {
  119. u8 t = 0, offset = amd_offset(dev);
  120. /*
  121. * Check 80-wire cable presence.
  122. */
  123. if (dev->vendor == PCI_VENDOR_ID_AMD &&
  124. dev->device == PCI_DEVICE_ID_AMD_COBRA_7401)
  125. ; /* no UDMA > 2 */
  126. else if (dev->vendor == PCI_VENDOR_ID_AMD &&
  127. dev->device == PCI_DEVICE_ID_AMD_VIPER_7409)
  128. amd7409_cable_detect(dev);
  129. else
  130. amd7411_cable_detect(dev);
  131. /*
  132. * Take care of prefetch & postwrite.
  133. */
  134. pci_read_config_byte(dev, AMD_IDE_CONFIG + offset, &t);
  135. /*
  136. * Check for broken FIFO support.
  137. */
  138. if (dev->vendor == PCI_VENDOR_ID_AMD &&
  139. dev->device == PCI_DEVICE_ID_AMD_VIPER_7411)
  140. t &= 0x0f;
  141. else
  142. t |= 0xf0;
  143. pci_write_config_byte(dev, AMD_IDE_CONFIG + offset, t);
  144. return 0;
  145. }
  146. static u8 amd_cable_detect(ide_hwif_t *hwif)
  147. {
  148. if ((amd_80w >> hwif->channel) & 1)
  149. return ATA_CBL_PATA80;
  150. else
  151. return ATA_CBL_PATA40;
  152. }
  153. static const struct ide_port_ops amd_port_ops = {
  154. .set_pio_mode = amd_set_pio_mode,
  155. .set_dma_mode = amd_set_drive,
  156. .cable_detect = amd_cable_detect,
  157. };
  158. #define IDE_HFLAGS_AMD \
  159. (IDE_HFLAG_PIO_NO_BLACKLIST | \
  160. IDE_HFLAG_POST_SET_MODE | \
  161. IDE_HFLAG_IO_32BIT | \
  162. IDE_HFLAG_UNMASK_IRQS)
  163. #define DECLARE_AMD_DEV(swdma, udma) \
  164. { \
  165. .name = DRV_NAME, \
  166. .init_chipset = init_chipset_amd74xx, \
  167. .enablebits = {{0x40,0x02,0x02}, {0x40,0x01,0x01}}, \
  168. .port_ops = &amd_port_ops, \
  169. .host_flags = IDE_HFLAGS_AMD, \
  170. .pio_mask = ATA_PIO5, \
  171. .swdma_mask = swdma, \
  172. .mwdma_mask = ATA_MWDMA2, \
  173. .udma_mask = udma, \
  174. }
  175. #define DECLARE_NV_DEV(udma) \
  176. { \
  177. .name = DRV_NAME, \
  178. .init_chipset = init_chipset_amd74xx, \
  179. .enablebits = {{0x50,0x02,0x02}, {0x50,0x01,0x01}}, \
  180. .port_ops = &amd_port_ops, \
  181. .host_flags = IDE_HFLAGS_AMD, \
  182. .pio_mask = ATA_PIO5, \
  183. .swdma_mask = ATA_SWDMA2, \
  184. .mwdma_mask = ATA_MWDMA2, \
  185. .udma_mask = udma, \
  186. }
  187. static const struct ide_port_info amd74xx_chipsets[] = {
  188. /* 0: AMD7401 */ DECLARE_AMD_DEV(0x00, ATA_UDMA2),
  189. /* 1: AMD7409 */ DECLARE_AMD_DEV(ATA_SWDMA2, ATA_UDMA4),
  190. /* 2: AMD7411/7441 */ DECLARE_AMD_DEV(ATA_SWDMA2, ATA_UDMA5),
  191. /* 3: AMD8111 */ DECLARE_AMD_DEV(ATA_SWDMA2, ATA_UDMA6),
  192. /* 4: NFORCE */ DECLARE_NV_DEV(ATA_UDMA5),
  193. /* 5: >= NFORCE2 */ DECLARE_NV_DEV(ATA_UDMA6),
  194. /* 6: AMD5536 */ DECLARE_AMD_DEV(ATA_SWDMA2, ATA_UDMA5),
  195. };
  196. static int amd74xx_probe(struct pci_dev *dev, const struct pci_device_id *id)
  197. {
  198. struct ide_port_info d;
  199. u8 idx = id->driver_data;
  200. d = amd74xx_chipsets[idx];
  201. /*
  202. * Check for bad SWDMA and incorrectly wired Serenade mainboards.
  203. */
  204. if (idx == 1) {
  205. if (dev->revision <= 7)
  206. d.swdma_mask = 0;
  207. d.host_flags |= IDE_HFLAG_CLEAR_SIMPLEX;
  208. } else if (idx == 3) {
  209. if (dev->subsystem_vendor == PCI_VENDOR_ID_AMD &&
  210. dev->subsystem_device == PCI_DEVICE_ID_AMD_SERENADE)
  211. d.udma_mask = ATA_UDMA5;
  212. }
  213. /*
  214. * It seems that on some nVidia controllers using AltStatus
  215. * register can be unreliable so default to Status register
  216. * if the device is in Compatibility Mode.
  217. */
  218. if (dev->vendor == PCI_VENDOR_ID_NVIDIA &&
  219. ide_pci_is_in_compatibility_mode(dev))
  220. d.host_flags |= IDE_HFLAG_BROKEN_ALTSTATUS;
  221. printk(KERN_INFO "%s %s: UDMA%s controller\n",
  222. d.name, pci_name(dev), amd_dma[fls(d.udma_mask) - 1]);
  223. /*
  224. * Determine the system bus clock.
  225. */
  226. amd_clock = (ide_pci_clk ? ide_pci_clk : 33) * 1000;
  227. switch (amd_clock) {
  228. case 33000: amd_clock = 33333; break;
  229. case 37000: amd_clock = 37500; break;
  230. case 41000: amd_clock = 41666; break;
  231. }
  232. if (amd_clock < 20000 || amd_clock > 50000) {
  233. printk(KERN_WARNING "%s: User given PCI clock speed impossible"
  234. " (%d), using 33 MHz instead.\n",
  235. d.name, amd_clock);
  236. amd_clock = 33333;
  237. }
  238. return ide_pci_init_one(dev, &d, NULL);
  239. }
  240. static const struct pci_device_id amd74xx_pci_tbl[] = {
  241. { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_COBRA_7401), 0 },
  242. { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_VIPER_7409), 1 },
  243. { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_VIPER_7411), 2 },
  244. { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_OPUS_7441), 2 },
  245. { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_8111_IDE), 3 },
  246. { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_IDE), 4 },
  247. { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE2_IDE), 5 },
  248. { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE2S_IDE), 5 },
  249. #ifdef CONFIG_BLK_DEV_IDE_SATA
  250. { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE2S_SATA), 5 },
  251. #endif
  252. { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE3_IDE), 5 },
  253. { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE3S_IDE), 5 },
  254. #ifdef CONFIG_BLK_DEV_IDE_SATA
  255. { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE3S_SATA), 5 },
  256. { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE3S_SATA2), 5 },
  257. #endif
  258. { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_CK804_IDE), 5 },
  259. { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP04_IDE), 5 },
  260. { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP51_IDE), 5 },
  261. { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP55_IDE), 5 },
  262. { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP61_IDE), 5 },
  263. { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP65_IDE), 5 },
  264. { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP67_IDE), 5 },
  265. { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP73_IDE), 5 },
  266. { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP77_IDE), 5 },
  267. { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_CS5536_IDE), 6 },
  268. { 0, },
  269. };
  270. MODULE_DEVICE_TABLE(pci, amd74xx_pci_tbl);
  271. static struct pci_driver amd74xx_pci_driver = {
  272. .name = "AMD_IDE",
  273. .id_table = amd74xx_pci_tbl,
  274. .probe = amd74xx_probe,
  275. .remove = ide_pci_remove,
  276. .suspend = ide_pci_suspend,
  277. .resume = ide_pci_resume,
  278. };
  279. static int __init amd74xx_ide_init(void)
  280. {
  281. return ide_pci_register_driver(&amd74xx_pci_driver);
  282. }
  283. static void __exit amd74xx_ide_exit(void)
  284. {
  285. pci_unregister_driver(&amd74xx_pci_driver);
  286. }
  287. module_init(amd74xx_ide_init);
  288. module_exit(amd74xx_ide_exit);
  289. MODULE_AUTHOR("Vojtech Pavlik, Bartlomiej Zolnierkiewicz");
  290. MODULE_DESCRIPTION("AMD PCI IDE driver");
  291. MODULE_LICENSE("GPL");