pata_hpt366.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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_mode;
  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. } else if (adev->class == ATA_DEV_ATAPI)
  165. mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
  166. return ata_bmdma_mode_filter(adev, mask);
  167. }
  168. static int hpt36x_cable_detect(struct ata_port *ap)
  169. {
  170. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  171. u8 ata66;
  172. /*
  173. * Each channel of pata_hpt366 occupies separate PCI function
  174. * as the primary channel and bit1 indicates the cable type.
  175. */
  176. pci_read_config_byte(pdev, 0x5A, &ata66);
  177. if (ata66 & 2)
  178. return ATA_CBL_PATA40;
  179. return ATA_CBL_PATA80;
  180. }
  181. static void hpt366_set_mode(struct ata_port *ap, struct ata_device *adev,
  182. u8 mode)
  183. {
  184. struct hpt_clock *clocks = ap->host->private_data;
  185. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  186. u32 addr1 = 0x40 + 4 * (adev->devno + 2 * ap->port_no);
  187. u32 addr2 = 0x51 + 4 * ap->port_no;
  188. u32 mask, reg;
  189. u8 fast;
  190. /* Fast interrupt prediction disable, hold off interrupt disable */
  191. pci_read_config_byte(pdev, addr2, &fast);
  192. if (fast & 0x80) {
  193. fast &= ~0x80;
  194. pci_write_config_byte(pdev, addr2, fast);
  195. }
  196. /* determine timing mask and find matching clock entry */
  197. if (mode < XFER_MW_DMA_0)
  198. mask = 0xc1f8ffff;
  199. else if (mode < XFER_UDMA_0)
  200. mask = 0x303800ff;
  201. else
  202. mask = 0x30070000;
  203. while (clocks->xfer_mode) {
  204. if (clocks->xfer_mode == mode)
  205. break;
  206. clocks++;
  207. }
  208. if (!clocks->xfer_mode)
  209. BUG();
  210. /*
  211. * Combine new mode bits with old config bits and disable
  212. * on-chip PIO FIFO/buffer (and PIO MST mode as well) to avoid
  213. * problems handling I/O errors later.
  214. */
  215. pci_read_config_dword(pdev, addr1, &reg);
  216. reg = ((reg & ~mask) | (clocks->timing & mask)) & ~0xc0000000;
  217. pci_write_config_dword(pdev, addr1, reg);
  218. }
  219. /**
  220. * hpt366_set_piomode - PIO setup
  221. * @ap: ATA interface
  222. * @adev: device on the interface
  223. *
  224. * Perform PIO mode setup.
  225. */
  226. static void hpt366_set_piomode(struct ata_port *ap, struct ata_device *adev)
  227. {
  228. hpt366_set_mode(ap, adev, adev->pio_mode);
  229. }
  230. /**
  231. * hpt366_set_dmamode - DMA timing setup
  232. * @ap: ATA interface
  233. * @adev: Device being configured
  234. *
  235. * Set up the channel for MWDMA or UDMA modes. Much the same as with
  236. * PIO, load the mode number and then set MWDMA or UDMA flag.
  237. */
  238. static void hpt366_set_dmamode(struct ata_port *ap, struct ata_device *adev)
  239. {
  240. hpt366_set_mode(ap, adev, adev->dma_mode);
  241. }
  242. static struct scsi_host_template hpt36x_sht = {
  243. ATA_BMDMA_SHT(DRV_NAME),
  244. };
  245. /*
  246. * Configuration for HPT366/68
  247. */
  248. static struct ata_port_operations hpt366_port_ops = {
  249. .inherits = &ata_bmdma_port_ops,
  250. .cable_detect = hpt36x_cable_detect,
  251. .mode_filter = hpt366_filter,
  252. .set_piomode = hpt366_set_piomode,
  253. .set_dmamode = hpt366_set_dmamode,
  254. };
  255. /**
  256. * hpt36x_init_chipset - common chip setup
  257. * @dev: PCI device
  258. *
  259. * Perform the chip setup work that must be done at both init and
  260. * resume time
  261. */
  262. static void hpt36x_init_chipset(struct pci_dev *dev)
  263. {
  264. u8 drive_fast;
  265. pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE, (L1_CACHE_BYTES / 4));
  266. pci_write_config_byte(dev, PCI_LATENCY_TIMER, 0x78);
  267. pci_write_config_byte(dev, PCI_MIN_GNT, 0x08);
  268. pci_write_config_byte(dev, PCI_MAX_LAT, 0x08);
  269. pci_read_config_byte(dev, 0x51, &drive_fast);
  270. if (drive_fast & 0x80)
  271. pci_write_config_byte(dev, 0x51, drive_fast & ~0x80);
  272. }
  273. /**
  274. * hpt36x_init_one - Initialise an HPT366/368
  275. * @dev: PCI device
  276. * @id: Entry in match table
  277. *
  278. * Initialise an HPT36x device. There are some interesting complications
  279. * here. Firstly the chip may report 366 and be one of several variants.
  280. * Secondly all the timings depend on the clock for the chip which we must
  281. * detect and look up
  282. *
  283. * This is the known chip mappings. It may be missing a couple of later
  284. * releases.
  285. *
  286. * Chip version PCI Rev Notes
  287. * HPT366 4 (HPT366) 0 UDMA66
  288. * HPT366 4 (HPT366) 1 UDMA66
  289. * HPT368 4 (HPT366) 2 UDMA66
  290. * HPT37x/30x 4 (HPT366) 3+ Other driver
  291. *
  292. */
  293. static int hpt36x_init_one(struct pci_dev *dev, const struct pci_device_id *id)
  294. {
  295. static const struct ata_port_info info_hpt366 = {
  296. .flags = ATA_FLAG_SLAVE_POSS,
  297. .pio_mask = ATA_PIO4,
  298. .mwdma_mask = ATA_MWDMA2,
  299. .udma_mask = ATA_UDMA4,
  300. .port_ops = &hpt366_port_ops
  301. };
  302. const struct ata_port_info *ppi[] = { &info_hpt366, NULL };
  303. void *hpriv = NULL;
  304. u32 class_rev;
  305. u32 reg1;
  306. int rc;
  307. rc = pcim_enable_device(dev);
  308. if (rc)
  309. return rc;
  310. pci_read_config_dword(dev, PCI_CLASS_REVISION, &class_rev);
  311. class_rev &= 0xFF;
  312. /* May be a later chip in disguise. Check */
  313. /* Newer chips are not in the HPT36x driver. Ignore them */
  314. if (class_rev > 2)
  315. return -ENODEV;
  316. hpt36x_init_chipset(dev);
  317. pci_read_config_dword(dev, 0x40, &reg1);
  318. /* PCI clocking determines the ATA timing values to use */
  319. /* info_hpt366 is safe against re-entry so we can scribble on it */
  320. switch((reg1 & 0x700) >> 8) {
  321. case 9:
  322. hpriv = &hpt366_40;
  323. break;
  324. case 5:
  325. hpriv = &hpt366_25;
  326. break;
  327. default:
  328. hpriv = &hpt366_33;
  329. break;
  330. }
  331. /* Now kick off ATA set up */
  332. return ata_pci_sff_init_one(dev, ppi, &hpt36x_sht, hpriv);
  333. }
  334. #ifdef CONFIG_PM
  335. static int hpt36x_reinit_one(struct pci_dev *dev)
  336. {
  337. struct ata_host *host = dev_get_drvdata(&dev->dev);
  338. int rc;
  339. rc = ata_pci_device_do_resume(dev);
  340. if (rc)
  341. return rc;
  342. hpt36x_init_chipset(dev);
  343. ata_host_resume(host);
  344. return 0;
  345. }
  346. #endif
  347. static const struct pci_device_id hpt36x[] = {
  348. { PCI_VDEVICE(TTI, PCI_DEVICE_ID_TTI_HPT366), },
  349. { },
  350. };
  351. static struct pci_driver hpt36x_pci_driver = {
  352. .name = DRV_NAME,
  353. .id_table = hpt36x,
  354. .probe = hpt36x_init_one,
  355. .remove = ata_pci_remove_one,
  356. #ifdef CONFIG_PM
  357. .suspend = ata_pci_device_suspend,
  358. .resume = hpt36x_reinit_one,
  359. #endif
  360. };
  361. static int __init hpt36x_init(void)
  362. {
  363. return pci_register_driver(&hpt36x_pci_driver);
  364. }
  365. static void __exit hpt36x_exit(void)
  366. {
  367. pci_unregister_driver(&hpt36x_pci_driver);
  368. }
  369. MODULE_AUTHOR("Alan Cox");
  370. MODULE_DESCRIPTION("low-level driver for the Highpoint HPT366/368");
  371. MODULE_LICENSE("GPL");
  372. MODULE_DEVICE_TABLE(pci, hpt36x);
  373. MODULE_VERSION(DRV_VERSION);
  374. module_init(hpt36x_init);
  375. module_exit(hpt36x_exit);