pata_hpt366.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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;
  159. ata_id_c_string(dev->id, model_num, ATA_ID_PROD, sizeof(model_num));
  160. i = match_string(list, -1, model_num);
  161. if (i >= 0) {
  162. pr_warn("%s is not supported for %s\n", modestr, list[i]);
  163. return 1;
  164. }
  165. return 0;
  166. }
  167. /**
  168. * hpt366_filter - mode selection filter
  169. * @adev: ATA device
  170. *
  171. * Block UDMA on devices that cause trouble with this controller.
  172. */
  173. static unsigned long hpt366_filter(struct ata_device *adev, unsigned long mask)
  174. {
  175. if (adev->class == ATA_DEV_ATA) {
  176. if (hpt_dma_blacklisted(adev, "UDMA", bad_ata33))
  177. mask &= ~ATA_MASK_UDMA;
  178. if (hpt_dma_blacklisted(adev, "UDMA3", bad_ata66_3))
  179. mask &= ~(0xF8 << ATA_SHIFT_UDMA);
  180. if (hpt_dma_blacklisted(adev, "UDMA4", bad_ata66_4))
  181. mask &= ~(0xF0 << ATA_SHIFT_UDMA);
  182. } else if (adev->class == ATA_DEV_ATAPI)
  183. mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
  184. return mask;
  185. }
  186. static int hpt36x_cable_detect(struct ata_port *ap)
  187. {
  188. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  189. u8 ata66;
  190. /*
  191. * Each channel of pata_hpt366 occupies separate PCI function
  192. * as the primary channel and bit1 indicates the cable type.
  193. */
  194. pci_read_config_byte(pdev, 0x5A, &ata66);
  195. if (ata66 & 2)
  196. return ATA_CBL_PATA40;
  197. return ATA_CBL_PATA80;
  198. }
  199. static void hpt366_set_mode(struct ata_port *ap, struct ata_device *adev,
  200. u8 mode)
  201. {
  202. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  203. u32 addr = 0x40 + 4 * adev->devno;
  204. u32 mask, reg, t;
  205. /* determine timing mask and find matching clock entry */
  206. if (mode < XFER_MW_DMA_0)
  207. mask = 0xc1f8ffff;
  208. else if (mode < XFER_UDMA_0)
  209. mask = 0x303800ff;
  210. else
  211. mask = 0x30070000;
  212. t = hpt36x_find_mode(ap, mode);
  213. /*
  214. * Combine new mode bits with old config bits and disable
  215. * on-chip PIO FIFO/buffer (and PIO MST mode as well) to avoid
  216. * problems handling I/O errors later.
  217. */
  218. pci_read_config_dword(pdev, addr, &reg);
  219. reg = ((reg & ~mask) | (t & mask)) & ~0xc0000000;
  220. pci_write_config_dword(pdev, addr, reg);
  221. }
  222. /**
  223. * hpt366_set_piomode - PIO setup
  224. * @ap: ATA interface
  225. * @adev: device on the interface
  226. *
  227. * Perform PIO mode setup.
  228. */
  229. static void hpt366_set_piomode(struct ata_port *ap, struct ata_device *adev)
  230. {
  231. hpt366_set_mode(ap, adev, adev->pio_mode);
  232. }
  233. /**
  234. * hpt366_set_dmamode - DMA timing setup
  235. * @ap: ATA interface
  236. * @adev: Device being configured
  237. *
  238. * Set up the channel for MWDMA or UDMA modes. Much the same as with
  239. * PIO, load the mode number and then set MWDMA or UDMA flag.
  240. */
  241. static void hpt366_set_dmamode(struct ata_port *ap, struct ata_device *adev)
  242. {
  243. hpt366_set_mode(ap, adev, adev->dma_mode);
  244. }
  245. static struct scsi_host_template hpt36x_sht = {
  246. ATA_BMDMA_SHT(DRV_NAME),
  247. };
  248. /*
  249. * Configuration for HPT366/68
  250. */
  251. static struct ata_port_operations hpt366_port_ops = {
  252. .inherits = &ata_bmdma_port_ops,
  253. .cable_detect = hpt36x_cable_detect,
  254. .mode_filter = hpt366_filter,
  255. .set_piomode = hpt366_set_piomode,
  256. .set_dmamode = hpt366_set_dmamode,
  257. };
  258. /**
  259. * hpt36x_init_chipset - common chip setup
  260. * @dev: PCI device
  261. *
  262. * Perform the chip setup work that must be done at both init and
  263. * resume time
  264. */
  265. static void hpt36x_init_chipset(struct pci_dev *dev)
  266. {
  267. u8 drive_fast;
  268. pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE, (L1_CACHE_BYTES / 4));
  269. pci_write_config_byte(dev, PCI_LATENCY_TIMER, 0x78);
  270. pci_write_config_byte(dev, PCI_MIN_GNT, 0x08);
  271. pci_write_config_byte(dev, PCI_MAX_LAT, 0x08);
  272. pci_read_config_byte(dev, 0x51, &drive_fast);
  273. if (drive_fast & 0x80)
  274. pci_write_config_byte(dev, 0x51, drive_fast & ~0x80);
  275. }
  276. /**
  277. * hpt36x_init_one - Initialise an HPT366/368
  278. * @dev: PCI device
  279. * @id: Entry in match table
  280. *
  281. * Initialise an HPT36x device. There are some interesting complications
  282. * here. Firstly the chip may report 366 and be one of several variants.
  283. * Secondly all the timings depend on the clock for the chip which we must
  284. * detect and look up
  285. *
  286. * This is the known chip mappings. It may be missing a couple of later
  287. * releases.
  288. *
  289. * Chip version PCI Rev Notes
  290. * HPT366 4 (HPT366) 0 UDMA66
  291. * HPT366 4 (HPT366) 1 UDMA66
  292. * HPT368 4 (HPT366) 2 UDMA66
  293. * HPT37x/30x 4 (HPT366) 3+ Other driver
  294. *
  295. */
  296. static int hpt36x_init_one(struct pci_dev *dev, const struct pci_device_id *id)
  297. {
  298. static const struct ata_port_info info_hpt366 = {
  299. .flags = ATA_FLAG_SLAVE_POSS,
  300. .pio_mask = ATA_PIO4,
  301. .mwdma_mask = ATA_MWDMA2,
  302. .udma_mask = ATA_UDMA4,
  303. .port_ops = &hpt366_port_ops
  304. };
  305. const struct ata_port_info *ppi[] = { &info_hpt366, NULL };
  306. const void *hpriv = NULL;
  307. u32 reg1;
  308. int rc;
  309. rc = pcim_enable_device(dev);
  310. if (rc)
  311. return rc;
  312. /* May be a later chip in disguise. Check */
  313. /* Newer chips are not in the HPT36x driver. Ignore them */
  314. if (dev->revision > 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 & 0xf00) >> 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_bmdma_init_one(dev, ppi, &hpt36x_sht, (void *)hpriv, 0);
  333. }
  334. #ifdef CONFIG_PM_SLEEP
  335. static int hpt36x_reinit_one(struct pci_dev *dev)
  336. {
  337. struct ata_host *host = pci_get_drvdata(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_SLEEP
  357. .suspend = ata_pci_device_suspend,
  358. .resume = hpt36x_reinit_one,
  359. #endif
  360. };
  361. module_pci_driver(hpt36x_pci_driver);
  362. MODULE_AUTHOR("Alan Cox");
  363. MODULE_DESCRIPTION("low-level driver for the Highpoint HPT366/368");
  364. MODULE_LICENSE("GPL");
  365. MODULE_DEVICE_TABLE(pci, hpt36x);
  366. MODULE_VERSION(DRV_VERSION);