pata_hpt366.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. /*
  2. * Libata driver for the highpoint 366 and 368 UDMA66 ATA controllers.
  3. *
  4. * This driver is heavily based upon:
  5. *
  6. * linux/drivers/ide/pci/hpt366.c Version 0.36 April 25, 2003
  7. *
  8. * Copyright (C) 1999-2003 Andre Hedrick <andre@linux-ide.org>
  9. * Portions Copyright (C) 2001 Sun Microsystems, Inc.
  10. * Portions Copyright (C) 2003 Red Hat Inc
  11. *
  12. *
  13. * TODO
  14. * Maybe PLL mode
  15. * Look into engine reset on timeout errors. Should not be
  16. * required.
  17. */
  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>
  26. #define DRV_NAME "pata_hpt366"
  27. #define DRV_VERSION "0.6.2"
  28. struct hpt_clock {
  29. u8 xfer_speed;
  30. u32 timing;
  31. };
  32. /* key for bus clock timings
  33. * bit
  34. * 0:3 data_high_time. inactive time of DIOW_/DIOR_ for PIO and MW
  35. * DMA. cycles = value + 1
  36. * 4:8 data_low_time. active time of DIOW_/DIOR_ for PIO and MW
  37. * DMA. cycles = value + 1
  38. * 9:12 cmd_high_time. inactive time of DIOW_/DIOR_ during task file
  39. * register access.
  40. * 13:17 cmd_low_time. active time of DIOW_/DIOR_ during task file
  41. * register access.
  42. * 18:21 udma_cycle_time. clock freq and clock cycles for UDMA xfer.
  43. * during task file register access.
  44. * 22:24 pre_high_time. time to initialize 1st cycle for PIO and MW DMA
  45. * xfer.
  46. * 25:27 cmd_pre_high_time. time to initialize 1st PIO cycle for task
  47. * register access.
  48. * 28 UDMA enable
  49. * 29 DMA enable
  50. * 30 PIO_MST enable. if set, the chip is in bus master mode during
  51. * PIO.
  52. * 31 FIFO enable.
  53. */
  54. static const struct hpt_clock hpt366_40[] = {
  55. { XFER_UDMA_4, 0x900fd943 },
  56. { XFER_UDMA_3, 0x900ad943 },
  57. { XFER_UDMA_2, 0x900bd943 },
  58. { XFER_UDMA_1, 0x9008d943 },
  59. { XFER_UDMA_0, 0x9008d943 },
  60. { XFER_MW_DMA_2, 0xa008d943 },
  61. { XFER_MW_DMA_1, 0xa010d955 },
  62. { XFER_MW_DMA_0, 0xa010d9fc },
  63. { XFER_PIO_4, 0xc008d963 },
  64. { XFER_PIO_3, 0xc010d974 },
  65. { XFER_PIO_2, 0xc010d997 },
  66. { XFER_PIO_1, 0xc010d9c7 },
  67. { XFER_PIO_0, 0xc018d9d9 },
  68. { 0, 0x0120d9d9 }
  69. };
  70. static const struct hpt_clock hpt366_33[] = {
  71. { XFER_UDMA_4, 0x90c9a731 },
  72. { XFER_UDMA_3, 0x90cfa731 },
  73. { XFER_UDMA_2, 0x90caa731 },
  74. { XFER_UDMA_1, 0x90cba731 },
  75. { XFER_UDMA_0, 0x90c8a731 },
  76. { XFER_MW_DMA_2, 0xa0c8a731 },
  77. { XFER_MW_DMA_1, 0xa0c8a732 }, /* 0xa0c8a733 */
  78. { XFER_MW_DMA_0, 0xa0c8a797 },
  79. { XFER_PIO_4, 0xc0c8a731 },
  80. { XFER_PIO_3, 0xc0c8a742 },
  81. { XFER_PIO_2, 0xc0d0a753 },
  82. { XFER_PIO_1, 0xc0d0a7a3 }, /* 0xc0d0a793 */
  83. { XFER_PIO_0, 0xc0d0a7aa }, /* 0xc0d0a7a7 */
  84. { 0, 0x0120a7a7 }
  85. };
  86. static const struct hpt_clock hpt366_25[] = {
  87. { XFER_UDMA_4, 0x90c98521 },
  88. { XFER_UDMA_3, 0x90cf8521 },
  89. { XFER_UDMA_2, 0x90cf8521 },
  90. { XFER_UDMA_1, 0x90cb8521 },
  91. { XFER_UDMA_0, 0x90cb8521 },
  92. { XFER_MW_DMA_2, 0xa0ca8521 },
  93. { XFER_MW_DMA_1, 0xa0ca8532 },
  94. { XFER_MW_DMA_0, 0xa0ca8575 },
  95. { XFER_PIO_4, 0xc0ca8521 },
  96. { XFER_PIO_3, 0xc0ca8532 },
  97. { XFER_PIO_2, 0xc0ca8542 },
  98. { XFER_PIO_1, 0xc0d08572 },
  99. { XFER_PIO_0, 0xc0d08585 },
  100. { 0, 0x01208585 }
  101. };
  102. static const char *bad_ata33[] = {
  103. "Maxtor 92720U8", "Maxtor 92040U6", "Maxtor 91360U4", "Maxtor 91020U3", "Maxtor 90845U3", "Maxtor 90650U2",
  104. "Maxtor 91360D8", "Maxtor 91190D7", "Maxtor 91020D6", "Maxtor 90845D5", "Maxtor 90680D4", "Maxtor 90510D3", "Maxtor 90340D2",
  105. "Maxtor 91152D8", "Maxtor 91008D7", "Maxtor 90845D6", "Maxtor 90840D6", "Maxtor 90720D5", "Maxtor 90648D5", "Maxtor 90576D4",
  106. "Maxtor 90510D4",
  107. "Maxtor 90432D3", "Maxtor 90288D2", "Maxtor 90256D2",
  108. "Maxtor 91000D8", "Maxtor 90910D8", "Maxtor 90875D7", "Maxtor 90840D7", "Maxtor 90750D6", "Maxtor 90625D5", "Maxtor 90500D4",
  109. "Maxtor 91728D8", "Maxtor 91512D7", "Maxtor 91303D6", "Maxtor 91080D5", "Maxtor 90845D4", "Maxtor 90680D4", "Maxtor 90648D3", "Maxtor 90432D2",
  110. NULL
  111. };
  112. static const char *bad_ata66_4[] = {
  113. "IBM-DTLA-307075",
  114. "IBM-DTLA-307060",
  115. "IBM-DTLA-307045",
  116. "IBM-DTLA-307030",
  117. "IBM-DTLA-307020",
  118. "IBM-DTLA-307015",
  119. "IBM-DTLA-305040",
  120. "IBM-DTLA-305030",
  121. "IBM-DTLA-305020",
  122. "IC35L010AVER07-0",
  123. "IC35L020AVER07-0",
  124. "IC35L030AVER07-0",
  125. "IC35L040AVER07-0",
  126. "IC35L060AVER07-0",
  127. "WDC AC310200R",
  128. NULL
  129. };
  130. static const char *bad_ata66_3[] = {
  131. "WDC AC310200R",
  132. NULL
  133. };
  134. static int hpt_dma_blacklisted(const struct ata_device *dev, char *modestr, const char *list[])
  135. {
  136. unsigned char model_num[ATA_ID_PROD_LEN + 1];
  137. int i = 0;
  138. ata_id_c_string(dev->id, model_num, ATA_ID_PROD, sizeof(model_num));
  139. while (list[i] != NULL) {
  140. if (!strcmp(list[i], model_num)) {
  141. printk(KERN_WARNING DRV_NAME ": %s is not supported for %s.\n",
  142. modestr, list[i]);
  143. return 1;
  144. }
  145. i++;
  146. }
  147. return 0;
  148. }
  149. /**
  150. * hpt366_filter - mode selection filter
  151. * @adev: ATA device
  152. *
  153. * Block UDMA on devices that cause trouble with this controller.
  154. */
  155. static unsigned long hpt366_filter(struct ata_device *adev, unsigned long mask)
  156. {
  157. if (adev->class == ATA_DEV_ATA) {
  158. if (hpt_dma_blacklisted(adev, "UDMA", bad_ata33))
  159. mask &= ~ATA_MASK_UDMA;
  160. if (hpt_dma_blacklisted(adev, "UDMA3", bad_ata66_3))
  161. mask &= ~(0xF8 << ATA_SHIFT_UDMA);
  162. if (hpt_dma_blacklisted(adev, "UDMA4", bad_ata66_4))
  163. mask &= ~(0xF0 << ATA_SHIFT_UDMA);
  164. }
  165. return ata_bmdma_mode_filter(adev, mask);
  166. }
  167. /**
  168. * hpt36x_find_mode - reset the hpt36x bus
  169. * @ap: ATA port
  170. * @speed: transfer mode
  171. *
  172. * Return the 32bit register programming information for this channel
  173. * that matches the speed provided.
  174. */
  175. static u32 hpt36x_find_mode(struct ata_port *ap, int speed)
  176. {
  177. struct hpt_clock *clocks = ap->host->private_data;
  178. while(clocks->xfer_speed) {
  179. if (clocks->xfer_speed == speed)
  180. return clocks->timing;
  181. clocks++;
  182. }
  183. BUG();
  184. return 0xffffffffU; /* silence compiler warning */
  185. }
  186. static int hpt36x_cable_detect(struct ata_port *ap)
  187. {
  188. u8 ata66;
  189. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  190. pci_read_config_byte(pdev, 0x5A, &ata66);
  191. if (ata66 & (1 << ap->port_no))
  192. return ATA_CBL_PATA40;
  193. return ATA_CBL_PATA80;
  194. }
  195. /**
  196. * hpt366_set_piomode - PIO setup
  197. * @ap: ATA interface
  198. * @adev: device on the interface
  199. *
  200. * Perform PIO mode setup.
  201. */
  202. static void hpt366_set_piomode(struct ata_port *ap, struct ata_device *adev)
  203. {
  204. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  205. u32 addr1, addr2;
  206. u32 reg;
  207. u32 mode;
  208. u8 fast;
  209. addr1 = 0x40 + 4 * (adev->devno + 2 * ap->port_no);
  210. addr2 = 0x51 + 4 * ap->port_no;
  211. /* Fast interrupt prediction disable, hold off interrupt disable */
  212. pci_read_config_byte(pdev, addr2, &fast);
  213. if (fast & 0x80) {
  214. fast &= ~0x80;
  215. pci_write_config_byte(pdev, addr2, fast);
  216. }
  217. pci_read_config_dword(pdev, addr1, &reg);
  218. mode = hpt36x_find_mode(ap, adev->pio_mode);
  219. mode &= ~0x8000000; /* No FIFO in PIO */
  220. mode &= ~0x30070000; /* Leave config bits alone */
  221. reg &= 0x30070000; /* Strip timing bits */
  222. pci_write_config_dword(pdev, addr1, reg | mode);
  223. }
  224. /**
  225. * hpt366_set_dmamode - DMA timing setup
  226. * @ap: ATA interface
  227. * @adev: Device being configured
  228. *
  229. * Set up the channel for MWDMA or UDMA modes. Much the same as with
  230. * PIO, load the mode number and then set MWDMA or UDMA flag.
  231. */
  232. static void hpt366_set_dmamode(struct ata_port *ap, struct ata_device *adev)
  233. {
  234. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  235. u32 addr1, addr2;
  236. u32 reg;
  237. u32 mode;
  238. u8 fast;
  239. addr1 = 0x40 + 4 * (adev->devno + 2 * ap->port_no);
  240. addr2 = 0x51 + 4 * ap->port_no;
  241. /* Fast interrupt prediction disable, hold off interrupt disable */
  242. pci_read_config_byte(pdev, addr2, &fast);
  243. if (fast & 0x80) {
  244. fast &= ~0x80;
  245. pci_write_config_byte(pdev, addr2, fast);
  246. }
  247. pci_read_config_dword(pdev, addr1, &reg);
  248. mode = hpt36x_find_mode(ap, adev->dma_mode);
  249. mode |= 0x8000000; /* FIFO in MWDMA or UDMA */
  250. mode &= ~0xC0000000; /* Leave config bits alone */
  251. reg &= 0xC0000000; /* Strip timing bits */
  252. pci_write_config_dword(pdev, addr1, reg | mode);
  253. }
  254. static struct scsi_host_template hpt36x_sht = {
  255. ATA_BMDMA_SHT(DRV_NAME),
  256. };
  257. /*
  258. * Configuration for HPT366/68
  259. */
  260. static struct ata_port_operations hpt366_port_ops = {
  261. .inherits = &ata_bmdma_port_ops,
  262. .cable_detect = hpt36x_cable_detect,
  263. .mode_filter = hpt366_filter,
  264. .set_piomode = hpt366_set_piomode,
  265. .set_dmamode = hpt366_set_dmamode,
  266. };
  267. /**
  268. * hpt36x_init_chipset - common chip setup
  269. * @dev: PCI device
  270. *
  271. * Perform the chip setup work that must be done at both init and
  272. * resume time
  273. */
  274. static void hpt36x_init_chipset(struct pci_dev *dev)
  275. {
  276. u8 drive_fast;
  277. pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE, (L1_CACHE_BYTES / 4));
  278. pci_write_config_byte(dev, PCI_LATENCY_TIMER, 0x78);
  279. pci_write_config_byte(dev, PCI_MIN_GNT, 0x08);
  280. pci_write_config_byte(dev, PCI_MAX_LAT, 0x08);
  281. pci_read_config_byte(dev, 0x51, &drive_fast);
  282. if (drive_fast & 0x80)
  283. pci_write_config_byte(dev, 0x51, drive_fast & ~0x80);
  284. }
  285. /**
  286. * hpt36x_init_one - Initialise an HPT366/368
  287. * @dev: PCI device
  288. * @id: Entry in match table
  289. *
  290. * Initialise an HPT36x device. There are some interesting complications
  291. * here. Firstly the chip may report 366 and be one of several variants.
  292. * Secondly all the timings depend on the clock for the chip which we must
  293. * detect and look up
  294. *
  295. * This is the known chip mappings. It may be missing a couple of later
  296. * releases.
  297. *
  298. * Chip version PCI Rev Notes
  299. * HPT366 4 (HPT366) 0 UDMA66
  300. * HPT366 4 (HPT366) 1 UDMA66
  301. * HPT368 4 (HPT366) 2 UDMA66
  302. * HPT37x/30x 4 (HPT366) 3+ Other driver
  303. *
  304. */
  305. static int hpt36x_init_one(struct pci_dev *dev, const struct pci_device_id *id)
  306. {
  307. static const struct ata_port_info info_hpt366 = {
  308. .flags = ATA_FLAG_SLAVE_POSS,
  309. .pio_mask = 0x1f,
  310. .mwdma_mask = 0x07,
  311. .udma_mask = ATA_UDMA4,
  312. .port_ops = &hpt366_port_ops
  313. };
  314. const struct ata_port_info *ppi[] = { &info_hpt366, NULL };
  315. void *hpriv = NULL;
  316. u32 class_rev;
  317. u32 reg1;
  318. int rc;
  319. rc = pcim_enable_device(dev);
  320. if (rc)
  321. return rc;
  322. pci_read_config_dword(dev, PCI_CLASS_REVISION, &class_rev);
  323. class_rev &= 0xFF;
  324. /* May be a later chip in disguise. Check */
  325. /* Newer chips are not in the HPT36x driver. Ignore them */
  326. if (class_rev > 2)
  327. return -ENODEV;
  328. hpt36x_init_chipset(dev);
  329. pci_read_config_dword(dev, 0x40, &reg1);
  330. /* PCI clocking determines the ATA timing values to use */
  331. /* info_hpt366 is safe against re-entry so we can scribble on it */
  332. switch((reg1 & 0x700) >> 8) {
  333. case 5:
  334. hpriv = &hpt366_40;
  335. break;
  336. case 9:
  337. hpriv = &hpt366_25;
  338. break;
  339. default:
  340. hpriv = &hpt366_33;
  341. break;
  342. }
  343. /* Now kick off ATA set up */
  344. return ata_pci_sff_init_one(dev, ppi, &hpt36x_sht, hpriv);
  345. }
  346. #ifdef CONFIG_PM
  347. static int hpt36x_reinit_one(struct pci_dev *dev)
  348. {
  349. struct ata_host *host = dev_get_drvdata(&dev->dev);
  350. int rc;
  351. rc = ata_pci_device_do_resume(dev);
  352. if (rc)
  353. return rc;
  354. hpt36x_init_chipset(dev);
  355. ata_host_resume(host);
  356. return 0;
  357. }
  358. #endif
  359. static const struct pci_device_id hpt36x[] = {
  360. { PCI_VDEVICE(TTI, PCI_DEVICE_ID_TTI_HPT366), },
  361. { },
  362. };
  363. static struct pci_driver hpt36x_pci_driver = {
  364. .name = DRV_NAME,
  365. .id_table = hpt36x,
  366. .probe = hpt36x_init_one,
  367. .remove = ata_pci_remove_one,
  368. #ifdef CONFIG_PM
  369. .suspend = ata_pci_device_suspend,
  370. .resume = hpt36x_reinit_one,
  371. #endif
  372. };
  373. static int __init hpt36x_init(void)
  374. {
  375. return pci_register_driver(&hpt36x_pci_driver);
  376. }
  377. static void __exit hpt36x_exit(void)
  378. {
  379. pci_unregister_driver(&hpt36x_pci_driver);
  380. }
  381. MODULE_AUTHOR("Alan Cox");
  382. MODULE_DESCRIPTION("low-level driver for the Highpoint HPT366/368");
  383. MODULE_LICENSE("GPL");
  384. MODULE_DEVICE_TABLE(pci, hpt36x);
  385. MODULE_VERSION(DRV_VERSION);
  386. module_init(hpt36x_init);
  387. module_exit(hpt36x_exit);