amd74xx.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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_drive_t *drive, const u8 speed)
  67. {
  68. ide_hwif_t *hwif = drive->hwif;
  69. struct pci_dev *dev = to_pci_dev(hwif->dev);
  70. ide_drive_t *peer = ide_get_pair_dev(drive);
  71. struct ide_timing t, p;
  72. int T, UT;
  73. u8 udma_mask = hwif->ultra_mask;
  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->current_speed, &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. amd_set_drive(drive, drive->pio_mode);
  91. }
  92. static void amd7409_cable_detect(struct pci_dev *dev)
  93. {
  94. /* no host side cable detection */
  95. amd_80w = 0x03;
  96. }
  97. static void amd7411_cable_detect(struct pci_dev *dev)
  98. {
  99. int i;
  100. u32 u = 0;
  101. u8 t = 0, offset = amd_offset(dev);
  102. pci_read_config_byte(dev, AMD_CABLE_DETECT + offset, &t);
  103. pci_read_config_dword(dev, AMD_UDMA_TIMING + offset, &u);
  104. amd_80w = ((t & 0x3) ? 1 : 0) | ((t & 0xc) ? 2 : 0);
  105. for (i = 24; i >= 0; i -= 8)
  106. if (((u >> i) & 4) && !(amd_80w & (1 << (1 - (i >> 4))))) {
  107. printk(KERN_WARNING DRV_NAME " %s: BIOS didn't set "
  108. "cable bits correctly. Enabling workaround.\n",
  109. pci_name(dev));
  110. amd_80w |= (1 << (1 - (i >> 4)));
  111. }
  112. }
  113. /*
  114. * The initialization callback. Initialize drive independent registers.
  115. */
  116. static int init_chipset_amd74xx(struct pci_dev *dev)
  117. {
  118. u8 t = 0, offset = amd_offset(dev);
  119. /*
  120. * Check 80-wire cable presence.
  121. */
  122. if (dev->vendor == PCI_VENDOR_ID_AMD &&
  123. dev->device == PCI_DEVICE_ID_AMD_COBRA_7401)
  124. ; /* no UDMA > 2 */
  125. else if (dev->vendor == PCI_VENDOR_ID_AMD &&
  126. dev->device == PCI_DEVICE_ID_AMD_VIPER_7409)
  127. amd7409_cable_detect(dev);
  128. else
  129. amd7411_cable_detect(dev);
  130. /*
  131. * Take care of prefetch & postwrite.
  132. */
  133. pci_read_config_byte(dev, AMD_IDE_CONFIG + offset, &t);
  134. /*
  135. * Check for broken FIFO support.
  136. */
  137. if (dev->vendor == PCI_VENDOR_ID_AMD &&
  138. dev->device == PCI_DEVICE_ID_AMD_VIPER_7411)
  139. t &= 0x0f;
  140. else
  141. t |= 0xf0;
  142. pci_write_config_byte(dev, AMD_IDE_CONFIG + offset, t);
  143. return 0;
  144. }
  145. static u8 amd_cable_detect(ide_hwif_t *hwif)
  146. {
  147. if ((amd_80w >> hwif->channel) & 1)
  148. return ATA_CBL_PATA80;
  149. else
  150. return ATA_CBL_PATA40;
  151. }
  152. static const struct ide_port_ops amd_port_ops = {
  153. .set_pio_mode = amd_set_pio_mode,
  154. .set_dma_mode = amd_set_drive,
  155. .cable_detect = amd_cable_detect,
  156. };
  157. #define IDE_HFLAGS_AMD \
  158. (IDE_HFLAG_PIO_NO_BLACKLIST | \
  159. IDE_HFLAG_POST_SET_MODE | \
  160. IDE_HFLAG_IO_32BIT | \
  161. IDE_HFLAG_UNMASK_IRQS)
  162. #define DECLARE_AMD_DEV(swdma, udma) \
  163. { \
  164. .name = DRV_NAME, \
  165. .init_chipset = init_chipset_amd74xx, \
  166. .enablebits = {{0x40,0x02,0x02}, {0x40,0x01,0x01}}, \
  167. .port_ops = &amd_port_ops, \
  168. .host_flags = IDE_HFLAGS_AMD, \
  169. .pio_mask = ATA_PIO5, \
  170. .swdma_mask = swdma, \
  171. .mwdma_mask = ATA_MWDMA2, \
  172. .udma_mask = udma, \
  173. }
  174. #define DECLARE_NV_DEV(udma) \
  175. { \
  176. .name = DRV_NAME, \
  177. .init_chipset = init_chipset_amd74xx, \
  178. .enablebits = {{0x50,0x02,0x02}, {0x50,0x01,0x01}}, \
  179. .port_ops = &amd_port_ops, \
  180. .host_flags = IDE_HFLAGS_AMD, \
  181. .pio_mask = ATA_PIO5, \
  182. .swdma_mask = ATA_SWDMA2, \
  183. .mwdma_mask = ATA_MWDMA2, \
  184. .udma_mask = udma, \
  185. }
  186. static const struct ide_port_info amd74xx_chipsets[] __devinitdata = {
  187. /* 0: AMD7401 */ DECLARE_AMD_DEV(0x00, ATA_UDMA2),
  188. /* 1: AMD7409 */ DECLARE_AMD_DEV(ATA_SWDMA2, ATA_UDMA4),
  189. /* 2: AMD7411/7441 */ DECLARE_AMD_DEV(ATA_SWDMA2, ATA_UDMA5),
  190. /* 3: AMD8111 */ DECLARE_AMD_DEV(ATA_SWDMA2, ATA_UDMA6),
  191. /* 4: NFORCE */ DECLARE_NV_DEV(ATA_UDMA5),
  192. /* 5: >= NFORCE2 */ DECLARE_NV_DEV(ATA_UDMA6),
  193. /* 6: AMD5536 */ DECLARE_AMD_DEV(ATA_SWDMA2, ATA_UDMA5),
  194. };
  195. static int __devinit amd74xx_probe(struct pci_dev *dev, const struct pci_device_id *id)
  196. {
  197. struct ide_port_info d;
  198. u8 idx = id->driver_data;
  199. d = amd74xx_chipsets[idx];
  200. /*
  201. * Check for bad SWDMA and incorrectly wired Serenade mainboards.
  202. */
  203. if (idx == 1) {
  204. if (dev->revision <= 7)
  205. d.swdma_mask = 0;
  206. d.host_flags |= IDE_HFLAG_CLEAR_SIMPLEX;
  207. } else if (idx == 3) {
  208. if (dev->subsystem_vendor == PCI_VENDOR_ID_AMD &&
  209. dev->subsystem_device == PCI_DEVICE_ID_AMD_SERENADE)
  210. d.udma_mask = ATA_UDMA5;
  211. }
  212. /*
  213. * It seems that on some nVidia controllers using AltStatus
  214. * register can be unreliable so default to Status register
  215. * if the device is in Compatibility Mode.
  216. */
  217. if (dev->vendor == PCI_VENDOR_ID_NVIDIA &&
  218. ide_pci_is_in_compatibility_mode(dev))
  219. d.host_flags |= IDE_HFLAG_BROKEN_ALTSTATUS;
  220. printk(KERN_INFO "%s %s: UDMA%s controller\n",
  221. d.name, pci_name(dev), amd_dma[fls(d.udma_mask) - 1]);
  222. /*
  223. * Determine the system bus clock.
  224. */
  225. amd_clock = (ide_pci_clk ? ide_pci_clk : 33) * 1000;
  226. switch (amd_clock) {
  227. case 33000: amd_clock = 33333; break;
  228. case 37000: amd_clock = 37500; break;
  229. case 41000: amd_clock = 41666; break;
  230. }
  231. if (amd_clock < 20000 || amd_clock > 50000) {
  232. printk(KERN_WARNING "%s: User given PCI clock speed impossible"
  233. " (%d), using 33 MHz instead.\n",
  234. d.name, amd_clock);
  235. amd_clock = 33333;
  236. }
  237. return ide_pci_init_one(dev, &d, NULL);
  238. }
  239. static const struct pci_device_id amd74xx_pci_tbl[] = {
  240. { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_COBRA_7401), 0 },
  241. { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_VIPER_7409), 1 },
  242. { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_VIPER_7411), 2 },
  243. { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_OPUS_7441), 2 },
  244. { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_8111_IDE), 3 },
  245. { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_IDE), 4 },
  246. { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE2_IDE), 5 },
  247. { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE2S_IDE), 5 },
  248. #ifdef CONFIG_BLK_DEV_IDE_SATA
  249. { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE2S_SATA), 5 },
  250. #endif
  251. { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE3_IDE), 5 },
  252. { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE3S_IDE), 5 },
  253. #ifdef CONFIG_BLK_DEV_IDE_SATA
  254. { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE3S_SATA), 5 },
  255. { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE3S_SATA2), 5 },
  256. #endif
  257. { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_CK804_IDE), 5 },
  258. { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP04_IDE), 5 },
  259. { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP51_IDE), 5 },
  260. { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP55_IDE), 5 },
  261. { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP61_IDE), 5 },
  262. { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP65_IDE), 5 },
  263. { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP67_IDE), 5 },
  264. { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP73_IDE), 5 },
  265. { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP77_IDE), 5 },
  266. { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_CS5536_IDE), 6 },
  267. { 0, },
  268. };
  269. MODULE_DEVICE_TABLE(pci, amd74xx_pci_tbl);
  270. static struct pci_driver amd74xx_pci_driver = {
  271. .name = "AMD_IDE",
  272. .id_table = amd74xx_pci_tbl,
  273. .probe = amd74xx_probe,
  274. .remove = ide_pci_remove,
  275. .suspend = ide_pci_suspend,
  276. .resume = ide_pci_resume,
  277. };
  278. static int __init amd74xx_ide_init(void)
  279. {
  280. return ide_pci_register_driver(&amd74xx_pci_driver);
  281. }
  282. static void __exit amd74xx_ide_exit(void)
  283. {
  284. pci_unregister_driver(&amd74xx_pci_driver);
  285. }
  286. module_init(amd74xx_ide_init);
  287. module_exit(amd74xx_ide_exit);
  288. MODULE_AUTHOR("Vojtech Pavlik, Bartlomiej Zolnierkiewicz");
  289. MODULE_DESCRIPTION("AMD PCI IDE driver");
  290. MODULE_LICENSE("GPL");