amd74xx.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /*
  2. * Version 2.24
  3. *
  4. * AMD 755/756/766/8111 and nVidia nForce/2/2s/3/3s/CK804/MCP04
  5. * IDE driver for Linux.
  6. *
  7. * Copyright (c) 2000-2002 Vojtech Pavlik
  8. * Copyright (c) 2007 Bartlomiej Zolnierkiewicz
  9. *
  10. * Based on the work of:
  11. * Andre Hedrick
  12. */
  13. /*
  14. * This program is free software; you can redistribute it and/or modify it
  15. * under the terms of the GNU General Public License version 2 as published by
  16. * the Free Software Foundation.
  17. */
  18. #include <linux/module.h>
  19. #include <linux/kernel.h>
  20. #include <linux/ioport.h>
  21. #include <linux/blkdev.h>
  22. #include <linux/pci.h>
  23. #include <linux/init.h>
  24. #include <linux/ide.h>
  25. #include <asm/io.h>
  26. #include "ide-timing.h"
  27. #define AMD_IDE_CONFIG (0x01 + amd_config->base)
  28. #define AMD_CABLE_DETECT (0x02 + amd_config->base)
  29. #define AMD_DRIVE_TIMING (0x08 + amd_config->base)
  30. #define AMD_8BIT_TIMING (0x0e + amd_config->base)
  31. #define AMD_ADDRESS_SETUP (0x0c + amd_config->base)
  32. #define AMD_UDMA_TIMING (0x10 + amd_config->base)
  33. #define AMD_CHECK_SWDMA 0x08
  34. #define AMD_BAD_SWDMA 0x10
  35. #define AMD_BAD_FIFO 0x20
  36. #define AMD_CHECK_SERENADE 0x40
  37. /*
  38. * AMD SouthBridge chips.
  39. */
  40. static struct amd_ide_chip {
  41. unsigned short id;
  42. u8 base;
  43. u8 udma_mask;
  44. u8 flags;
  45. } amd_ide_chips[] = {
  46. { PCI_DEVICE_ID_AMD_COBRA_7401, 0x40, ATA_UDMA2, AMD_BAD_SWDMA },
  47. { PCI_DEVICE_ID_AMD_VIPER_7409, 0x40, ATA_UDMA4, AMD_CHECK_SWDMA },
  48. { PCI_DEVICE_ID_AMD_VIPER_7411, 0x40, ATA_UDMA5, AMD_BAD_FIFO },
  49. { PCI_DEVICE_ID_AMD_OPUS_7441, 0x40, ATA_UDMA5, },
  50. { PCI_DEVICE_ID_AMD_8111_IDE, 0x40, ATA_UDMA6, AMD_CHECK_SERENADE },
  51. { PCI_DEVICE_ID_NVIDIA_NFORCE_IDE, 0x50, ATA_UDMA5, },
  52. { PCI_DEVICE_ID_NVIDIA_NFORCE2_IDE, 0x50, ATA_UDMA6, },
  53. { PCI_DEVICE_ID_NVIDIA_NFORCE2S_IDE, 0x50, ATA_UDMA6, },
  54. { PCI_DEVICE_ID_NVIDIA_NFORCE2S_SATA, 0x50, ATA_UDMA6, },
  55. { PCI_DEVICE_ID_NVIDIA_NFORCE3_IDE, 0x50, ATA_UDMA6, },
  56. { PCI_DEVICE_ID_NVIDIA_NFORCE3S_IDE, 0x50, ATA_UDMA6, },
  57. { PCI_DEVICE_ID_NVIDIA_NFORCE3S_SATA, 0x50, ATA_UDMA6, },
  58. { PCI_DEVICE_ID_NVIDIA_NFORCE3S_SATA2, 0x50, ATA_UDMA6, },
  59. { PCI_DEVICE_ID_NVIDIA_NFORCE_CK804_IDE, 0x50, ATA_UDMA6, },
  60. { PCI_DEVICE_ID_NVIDIA_NFORCE_MCP04_IDE, 0x50, ATA_UDMA6, },
  61. { PCI_DEVICE_ID_NVIDIA_NFORCE_MCP51_IDE, 0x50, ATA_UDMA6, },
  62. { PCI_DEVICE_ID_NVIDIA_NFORCE_MCP55_IDE, 0x50, ATA_UDMA6, },
  63. { PCI_DEVICE_ID_NVIDIA_NFORCE_MCP61_IDE, 0x50, ATA_UDMA6, },
  64. { PCI_DEVICE_ID_NVIDIA_NFORCE_MCP65_IDE, 0x50, ATA_UDMA6, },
  65. { PCI_DEVICE_ID_NVIDIA_NFORCE_MCP67_IDE, 0x50, ATA_UDMA6, },
  66. { PCI_DEVICE_ID_NVIDIA_NFORCE_MCP73_IDE, 0x50, ATA_UDMA6, },
  67. { PCI_DEVICE_ID_NVIDIA_NFORCE_MCP77_IDE, 0x50, ATA_UDMA6, },
  68. { PCI_DEVICE_ID_AMD_CS5536_IDE, 0x40, ATA_UDMA5, },
  69. { 0 }
  70. };
  71. static struct amd_ide_chip *amd_config;
  72. static const struct ide_port_info *amd_chipset;
  73. static unsigned int amd_80w;
  74. static unsigned int amd_clock;
  75. static char *amd_dma[] = { "16", "25", "33", "44", "66", "100", "133" };
  76. static unsigned char amd_cyc2udma[] = { 6, 6, 5, 4, 0, 1, 1, 2, 2, 3, 3, 3, 3, 3, 3, 7 };
  77. /*
  78. * amd_set_speed() writes timing values to the chipset registers
  79. */
  80. static void amd_set_speed(struct pci_dev *dev, unsigned char dn, struct ide_timing *timing)
  81. {
  82. unsigned char t;
  83. pci_read_config_byte(dev, AMD_ADDRESS_SETUP, &t);
  84. t = (t & ~(3 << ((3 - dn) << 1))) | ((FIT(timing->setup, 1, 4) - 1) << ((3 - dn) << 1));
  85. pci_write_config_byte(dev, AMD_ADDRESS_SETUP, t);
  86. pci_write_config_byte(dev, AMD_8BIT_TIMING + (1 - (dn >> 1)),
  87. ((FIT(timing->act8b, 1, 16) - 1) << 4) | (FIT(timing->rec8b, 1, 16) - 1));
  88. pci_write_config_byte(dev, AMD_DRIVE_TIMING + (3 - dn),
  89. ((FIT(timing->active, 1, 16) - 1) << 4) | (FIT(timing->recover, 1, 16) - 1));
  90. switch (amd_config->udma_mask) {
  91. case ATA_UDMA2: t = timing->udma ? (0xc0 | (FIT(timing->udma, 2, 5) - 2)) : 0x03; break;
  92. case ATA_UDMA4: t = timing->udma ? (0xc0 | amd_cyc2udma[FIT(timing->udma, 2, 10)]) : 0x03; break;
  93. case ATA_UDMA5: t = timing->udma ? (0xc0 | amd_cyc2udma[FIT(timing->udma, 1, 10)]) : 0x03; break;
  94. case ATA_UDMA6: t = timing->udma ? (0xc0 | amd_cyc2udma[FIT(timing->udma, 1, 15)]) : 0x03; break;
  95. default: return;
  96. }
  97. pci_write_config_byte(dev, AMD_UDMA_TIMING + (3 - dn), t);
  98. }
  99. /*
  100. * amd_set_drive() computes timing values and configures the chipset
  101. * to a desired transfer mode. It also can be called by upper layers.
  102. */
  103. static void amd_set_drive(ide_drive_t *drive, const u8 speed)
  104. {
  105. ide_drive_t *peer = HWIF(drive)->drives + (~drive->dn & 1);
  106. struct ide_timing t, p;
  107. int T, UT;
  108. T = 1000000000 / amd_clock;
  109. UT = (amd_config->udma_mask == ATA_UDMA2) ? T : (T / 2);
  110. ide_timing_compute(drive, speed, &t, T, UT);
  111. if (peer->present) {
  112. ide_timing_compute(peer, peer->current_speed, &p, T, UT);
  113. ide_timing_merge(&p, &t, &t, IDE_TIMING_8BIT);
  114. }
  115. if (speed == XFER_UDMA_5 && amd_clock <= 33333) t.udma = 1;
  116. if (speed == XFER_UDMA_6 && amd_clock <= 33333) t.udma = 15;
  117. amd_set_speed(HWIF(drive)->pci_dev, drive->dn, &t);
  118. }
  119. /*
  120. * amd_set_pio_mode() is a callback from upper layers for PIO-only tuning.
  121. */
  122. static void amd_set_pio_mode(ide_drive_t *drive, const u8 pio)
  123. {
  124. amd_set_drive(drive, XFER_PIO_0 + pio);
  125. }
  126. /*
  127. * The initialization callback. Here we determine the IDE chip type
  128. * and initialize its drive independent registers.
  129. */
  130. static unsigned int __devinit init_chipset_amd74xx(struct pci_dev *dev, const char *name)
  131. {
  132. unsigned char t;
  133. unsigned int u;
  134. int i;
  135. /*
  136. * Check for bad SWDMA.
  137. */
  138. if (amd_config->flags & AMD_CHECK_SWDMA) {
  139. if (dev->revision <= 7)
  140. amd_config->flags |= AMD_BAD_SWDMA;
  141. }
  142. /*
  143. * Check 80-wire cable presence.
  144. */
  145. switch (amd_config->udma_mask) {
  146. case ATA_UDMA6:
  147. case ATA_UDMA5:
  148. pci_read_config_byte(dev, AMD_CABLE_DETECT, &t);
  149. pci_read_config_dword(dev, AMD_UDMA_TIMING, &u);
  150. amd_80w = ((t & 0x3) ? 1 : 0) | ((t & 0xc) ? 2 : 0);
  151. for (i = 24; i >= 0; i -= 8)
  152. if (((u >> i) & 4) && !(amd_80w & (1 << (1 - (i >> 4))))) {
  153. printk(KERN_WARNING "%s: BIOS didn't set cable bits correctly. Enabling workaround.\n",
  154. amd_chipset->name);
  155. amd_80w |= (1 << (1 - (i >> 4)));
  156. }
  157. break;
  158. case ATA_UDMA4:
  159. /* no host side cable detection */
  160. amd_80w = 0x03;
  161. break;
  162. }
  163. /*
  164. * Take care of prefetch & postwrite.
  165. */
  166. pci_read_config_byte(dev, AMD_IDE_CONFIG, &t);
  167. pci_write_config_byte(dev, AMD_IDE_CONFIG,
  168. (amd_config->flags & AMD_BAD_FIFO) ? (t & 0x0f) : (t | 0xf0));
  169. /*
  170. * Take care of incorrectly wired Serenade mainboards.
  171. */
  172. if ((amd_config->flags & AMD_CHECK_SERENADE) &&
  173. dev->subsystem_vendor == PCI_VENDOR_ID_AMD &&
  174. dev->subsystem_device == PCI_DEVICE_ID_AMD_SERENADE)
  175. amd_config->udma_mask = ATA_UDMA5;
  176. /*
  177. * Determine the system bus clock.
  178. */
  179. amd_clock = system_bus_clock() * 1000;
  180. switch (amd_clock) {
  181. case 33000: amd_clock = 33333; break;
  182. case 37000: amd_clock = 37500; break;
  183. case 41000: amd_clock = 41666; break;
  184. }
  185. if (amd_clock < 20000 || amd_clock > 50000) {
  186. printk(KERN_WARNING "%s: User given PCI clock speed impossible (%d), using 33 MHz instead.\n",
  187. amd_chipset->name, amd_clock);
  188. amd_clock = 33333;
  189. }
  190. /*
  191. * Print the boot message.
  192. */
  193. printk(KERN_INFO "%s: %s (rev %02x) UDMA%s controller\n",
  194. amd_chipset->name, pci_name(dev), dev->revision,
  195. amd_dma[fls(amd_config->udma_mask) - 1]);
  196. return dev->irq;
  197. }
  198. static void __devinit init_hwif_amd74xx(ide_hwif_t *hwif)
  199. {
  200. if (hwif->irq == 0) /* 0 is bogus but will do for now */
  201. hwif->irq = pci_get_legacy_ide_irq(hwif->pci_dev, hwif->channel);
  202. hwif->set_pio_mode = &amd_set_pio_mode;
  203. hwif->set_dma_mode = &amd_set_drive;
  204. if (!hwif->dma_base)
  205. return;
  206. hwif->ultra_mask = amd_config->udma_mask;
  207. if (amd_config->flags & AMD_BAD_SWDMA)
  208. hwif->swdma_mask = 0x00;
  209. if (hwif->cbl != ATA_CBL_PATA40_SHORT) {
  210. if ((amd_80w >> hwif->channel) & 1)
  211. hwif->cbl = ATA_CBL_PATA80;
  212. else
  213. hwif->cbl = ATA_CBL_PATA40;
  214. }
  215. }
  216. #define IDE_HFLAGS_AMD \
  217. (IDE_HFLAG_PIO_NO_BLACKLIST | \
  218. IDE_HFLAG_PIO_NO_DOWNGRADE | \
  219. IDE_HFLAG_POST_SET_MODE | \
  220. IDE_HFLAG_IO_32BIT | \
  221. IDE_HFLAG_UNMASK_IRQS | \
  222. IDE_HFLAG_BOOTABLE)
  223. #define DECLARE_AMD_DEV(name_str) \
  224. { \
  225. .name = name_str, \
  226. .init_chipset = init_chipset_amd74xx, \
  227. .init_hwif = init_hwif_amd74xx, \
  228. .enablebits = {{0x40,0x02,0x02}, {0x40,0x01,0x01}}, \
  229. .host_flags = IDE_HFLAGS_AMD, \
  230. .pio_mask = ATA_PIO5, \
  231. .swdma_mask = ATA_SWDMA2, \
  232. .mwdma_mask = ATA_MWDMA2, \
  233. }
  234. #define DECLARE_NV_DEV(name_str) \
  235. { \
  236. .name = name_str, \
  237. .init_chipset = init_chipset_amd74xx, \
  238. .init_hwif = init_hwif_amd74xx, \
  239. .enablebits = {{0x50,0x02,0x02}, {0x50,0x01,0x01}}, \
  240. .host_flags = IDE_HFLAGS_AMD, \
  241. .pio_mask = ATA_PIO5, \
  242. .swdma_mask = ATA_SWDMA2, \
  243. .mwdma_mask = ATA_MWDMA2, \
  244. }
  245. static const struct ide_port_info amd74xx_chipsets[] __devinitdata = {
  246. /* 0 */ DECLARE_AMD_DEV("AMD7401"),
  247. /* 1 */ DECLARE_AMD_DEV("AMD7409"),
  248. /* 2 */ DECLARE_AMD_DEV("AMD7411"),
  249. /* 3 */ DECLARE_AMD_DEV("AMD7441"),
  250. /* 4 */ DECLARE_AMD_DEV("AMD8111"),
  251. /* 5 */ DECLARE_NV_DEV("NFORCE"),
  252. /* 6 */ DECLARE_NV_DEV("NFORCE2"),
  253. /* 7 */ DECLARE_NV_DEV("NFORCE2-U400R"),
  254. /* 8 */ DECLARE_NV_DEV("NFORCE2-U400R-SATA"),
  255. /* 9 */ DECLARE_NV_DEV("NFORCE3-150"),
  256. /* 10 */ DECLARE_NV_DEV("NFORCE3-250"),
  257. /* 11 */ DECLARE_NV_DEV("NFORCE3-250-SATA"),
  258. /* 12 */ DECLARE_NV_DEV("NFORCE3-250-SATA2"),
  259. /* 13 */ DECLARE_NV_DEV("NFORCE-CK804"),
  260. /* 14 */ DECLARE_NV_DEV("NFORCE-MCP04"),
  261. /* 15 */ DECLARE_NV_DEV("NFORCE-MCP51"),
  262. /* 16 */ DECLARE_NV_DEV("NFORCE-MCP55"),
  263. /* 17 */ DECLARE_NV_DEV("NFORCE-MCP61"),
  264. /* 18 */ DECLARE_NV_DEV("NFORCE-MCP65"),
  265. /* 19 */ DECLARE_NV_DEV("NFORCE-MCP67"),
  266. /* 20 */ DECLARE_NV_DEV("NFORCE-MCP73"),
  267. /* 21 */ DECLARE_NV_DEV("NFORCE-MCP77"),
  268. /* 22 */ DECLARE_AMD_DEV("AMD5536"),
  269. };
  270. static int __devinit amd74xx_probe(struct pci_dev *dev, const struct pci_device_id *id)
  271. {
  272. amd_chipset = amd74xx_chipsets + id->driver_data;
  273. amd_config = amd_ide_chips + id->driver_data;
  274. if (dev->device != amd_config->id) {
  275. printk(KERN_ERR "%s: assertion 0x%02x == 0x%02x failed !\n",
  276. pci_name(dev), dev->device, amd_config->id);
  277. return -ENODEV;
  278. }
  279. return ide_setup_pci_device(dev, amd_chipset);
  280. }
  281. static const struct pci_device_id amd74xx_pci_tbl[] = {
  282. { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_COBRA_7401), 0 },
  283. { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_VIPER_7409), 1 },
  284. { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_VIPER_7411), 2 },
  285. { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_OPUS_7441), 3 },
  286. { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_8111_IDE), 4 },
  287. { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_IDE), 5 },
  288. { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE2_IDE), 6 },
  289. { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE2S_IDE), 7 },
  290. #ifdef CONFIG_BLK_DEV_IDE_SATA
  291. { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE2S_SATA), 8 },
  292. #endif
  293. { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE3_IDE), 9 },
  294. { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE3S_IDE), 10 },
  295. #ifdef CONFIG_BLK_DEV_IDE_SATA
  296. { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE3S_SATA), 11 },
  297. { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE3S_SATA2), 12 },
  298. #endif
  299. { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_CK804_IDE), 13 },
  300. { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP04_IDE), 14 },
  301. { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP51_IDE), 15 },
  302. { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP55_IDE), 16 },
  303. { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP61_IDE), 17 },
  304. { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP65_IDE), 18 },
  305. { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP67_IDE), 19 },
  306. { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP73_IDE), 20 },
  307. { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP77_IDE), 21 },
  308. { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_CS5536_IDE), 22 },
  309. { 0, },
  310. };
  311. MODULE_DEVICE_TABLE(pci, amd74xx_pci_tbl);
  312. static struct pci_driver driver = {
  313. .name = "AMD_IDE",
  314. .id_table = amd74xx_pci_tbl,
  315. .probe = amd74xx_probe,
  316. };
  317. static int __init amd74xx_ide_init(void)
  318. {
  319. return ide_pci_register_driver(&driver);
  320. }
  321. module_init(amd74xx_ide_init);
  322. MODULE_AUTHOR("Vojtech Pavlik");
  323. MODULE_DESCRIPTION("AMD PCI IDE driver");
  324. MODULE_LICENSE("GPL");