pata_hpt366.c 11 KB

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