pata_hpt366.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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.5"
  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[40];
  137. char *s;
  138. unsigned int len;
  139. int i = 0;
  140. ata_id_string(dev->id, model_num, ATA_ID_PROD_OFS, sizeof(model_num));
  141. s = &model_num[0];
  142. len = strnlen(s, sizeof(model_num));
  143. /* ATAPI specifies that empty space is blank-filled; remove blanks */
  144. while ((len > 0) && (s[len - 1] == ' ')) {
  145. len--;
  146. s[len] = 0;
  147. }
  148. while(list[i] != NULL) {
  149. if (!strncmp(list[i], s, len)) {
  150. printk(KERN_WARNING DRV_NAME ": %s is not supported for %s.\n",
  151. modestr, list[i]);
  152. return 1;
  153. }
  154. i++;
  155. }
  156. return 0;
  157. }
  158. /**
  159. * hpt366_filter - mode selection filter
  160. * @ap: ATA interface
  161. * @adev: ATA device
  162. *
  163. * Block UDMA on devices that cause trouble with this controller.
  164. */
  165. static unsigned long hpt366_filter(const struct ata_port *ap, struct ata_device *adev, unsigned long mask)
  166. {
  167. if (adev->class == ATA_DEV_ATA) {
  168. if (hpt_dma_blacklisted(adev, "UDMA", bad_ata33))
  169. mask &= ~ATA_MASK_UDMA;
  170. if (hpt_dma_blacklisted(adev, "UDMA3", bad_ata66_3))
  171. mask &= ~(0x07 << ATA_SHIFT_UDMA);
  172. if (hpt_dma_blacklisted(adev, "UDMA4", bad_ata66_4))
  173. mask &= ~(0x0F << ATA_SHIFT_UDMA);
  174. }
  175. return ata_pci_default_filter(ap, adev, mask);
  176. }
  177. /**
  178. * hpt36x_find_mode - reset the hpt36x bus
  179. * @ap: ATA port
  180. * @speed: transfer mode
  181. *
  182. * Return the 32bit register programming information for this channel
  183. * that matches the speed provided.
  184. */
  185. static u32 hpt36x_find_mode(struct ata_port *ap, int speed)
  186. {
  187. struct hpt_clock *clocks = ap->host->private_data;
  188. while(clocks->xfer_speed) {
  189. if (clocks->xfer_speed == speed)
  190. return clocks->timing;
  191. clocks++;
  192. }
  193. BUG();
  194. return 0xffffffffU; /* silence compiler warning */
  195. }
  196. static int hpt36x_pre_reset(struct ata_port *ap)
  197. {
  198. u8 ata66;
  199. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  200. pci_read_config_byte(pdev, 0x5A, &ata66);
  201. if (ata66 & (1 << ap->port_no))
  202. ap->cbl = ATA_CBL_PATA40;
  203. else
  204. ap->cbl = ATA_CBL_PATA80;
  205. return ata_std_prereset(ap);
  206. }
  207. /**
  208. * hpt36x_error_handler - reset the hpt36x bus
  209. * @ap: ATA port to reset
  210. *
  211. * Perform the reset handling for the 366/368
  212. */
  213. static void hpt36x_error_handler(struct ata_port *ap)
  214. {
  215. ata_bmdma_drive_eh(ap, hpt36x_pre_reset, ata_std_softreset, NULL, ata_std_postreset);
  216. }
  217. /**
  218. * hpt366_set_piomode - PIO setup
  219. * @ap: ATA interface
  220. * @adev: device on the interface
  221. *
  222. * Perform PIO mode setup.
  223. */
  224. static void hpt366_set_piomode(struct ata_port *ap, struct ata_device *adev)
  225. {
  226. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  227. u32 addr1, addr2;
  228. u32 reg;
  229. u32 mode;
  230. u8 fast;
  231. addr1 = 0x40 + 4 * (adev->devno + 2 * ap->port_no);
  232. addr2 = 0x51 + 4 * ap->port_no;
  233. /* Fast interrupt prediction disable, hold off interrupt disable */
  234. pci_read_config_byte(pdev, addr2, &fast);
  235. if (fast & 0x80) {
  236. fast &= ~0x80;
  237. pci_write_config_byte(pdev, addr2, fast);
  238. }
  239. pci_read_config_dword(pdev, addr1, &reg);
  240. mode = hpt36x_find_mode(ap, adev->pio_mode);
  241. mode &= ~0x8000000; /* No FIFO in PIO */
  242. mode &= ~0x30070000; /* Leave config bits alone */
  243. reg &= 0x30070000; /* Strip timing bits */
  244. pci_write_config_dword(pdev, addr1, reg | mode);
  245. }
  246. /**
  247. * hpt366_set_dmamode - DMA timing setup
  248. * @ap: ATA interface
  249. * @adev: Device being configured
  250. *
  251. * Set up the channel for MWDMA or UDMA modes. Much the same as with
  252. * PIO, load the mode number and then set MWDMA or UDMA flag.
  253. */
  254. static void hpt366_set_dmamode(struct ata_port *ap, struct ata_device *adev)
  255. {
  256. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  257. u32 addr1, addr2;
  258. u32 reg;
  259. u32 mode;
  260. u8 fast;
  261. addr1 = 0x40 + 4 * (adev->devno + 2 * ap->port_no);
  262. addr2 = 0x51 + 4 * ap->port_no;
  263. /* Fast interrupt prediction disable, hold off interrupt disable */
  264. pci_read_config_byte(pdev, addr2, &fast);
  265. if (fast & 0x80) {
  266. fast &= ~0x80;
  267. pci_write_config_byte(pdev, addr2, fast);
  268. }
  269. pci_read_config_dword(pdev, addr1, &reg);
  270. mode = hpt36x_find_mode(ap, adev->dma_mode);
  271. mode |= 0x8000000; /* FIFO in MWDMA or UDMA */
  272. mode &= ~0xC0000000; /* Leave config bits alone */
  273. reg &= 0xC0000000; /* Strip timing bits */
  274. pci_write_config_dword(pdev, addr1, reg | mode);
  275. }
  276. static struct scsi_host_template hpt36x_sht = {
  277. .module = THIS_MODULE,
  278. .name = DRV_NAME,
  279. .ioctl = ata_scsi_ioctl,
  280. .queuecommand = ata_scsi_queuecmd,
  281. .can_queue = ATA_DEF_QUEUE,
  282. .this_id = ATA_SHT_THIS_ID,
  283. .sg_tablesize = LIBATA_MAX_PRD,
  284. .max_sectors = ATA_MAX_SECTORS,
  285. .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
  286. .emulated = ATA_SHT_EMULATED,
  287. .use_clustering = ATA_SHT_USE_CLUSTERING,
  288. .proc_name = DRV_NAME,
  289. .dma_boundary = ATA_DMA_BOUNDARY,
  290. .slave_configure = ata_scsi_slave_config,
  291. .bios_param = ata_std_bios_param,
  292. };
  293. /*
  294. * Configuration for HPT366/68
  295. */
  296. static struct ata_port_operations hpt366_port_ops = {
  297. .port_disable = ata_port_disable,
  298. .set_piomode = hpt366_set_piomode,
  299. .set_dmamode = hpt366_set_dmamode,
  300. .mode_filter = hpt366_filter,
  301. .tf_load = ata_tf_load,
  302. .tf_read = ata_tf_read,
  303. .check_status = ata_check_status,
  304. .exec_command = ata_exec_command,
  305. .dev_select = ata_std_dev_select,
  306. .freeze = ata_bmdma_freeze,
  307. .thaw = ata_bmdma_thaw,
  308. .error_handler = hpt36x_error_handler,
  309. .post_internal_cmd = ata_bmdma_post_internal_cmd,
  310. .bmdma_setup = ata_bmdma_setup,
  311. .bmdma_start = ata_bmdma_start,
  312. .bmdma_stop = ata_bmdma_stop,
  313. .bmdma_status = ata_bmdma_status,
  314. .qc_prep = ata_qc_prep,
  315. .qc_issue = ata_qc_issue_prot,
  316. .data_xfer = ata_pio_data_xfer,
  317. .irq_handler = ata_interrupt,
  318. .irq_clear = ata_bmdma_irq_clear,
  319. .port_start = ata_port_start,
  320. .port_stop = ata_port_stop,
  321. .host_stop = ata_host_stop
  322. };
  323. /**
  324. * hpt36x_init_one - Initialise an HPT366/368
  325. * @dev: PCI device
  326. * @id: Entry in match table
  327. *
  328. * Initialise an HPT36x device. There are some interesting complications
  329. * here. Firstly the chip may report 366 and be one of several variants.
  330. * Secondly all the timings depend on the clock for the chip which we must
  331. * detect and look up
  332. *
  333. * This is the known chip mappings. It may be missing a couple of later
  334. * releases.
  335. *
  336. * Chip version PCI Rev Notes
  337. * HPT366 4 (HPT366) 0 UDMA66
  338. * HPT366 4 (HPT366) 1 UDMA66
  339. * HPT368 4 (HPT366) 2 UDMA66
  340. * HPT37x/30x 4 (HPT366) 3+ Other driver
  341. *
  342. */
  343. static int hpt36x_init_one(struct pci_dev *dev, const struct pci_device_id *id)
  344. {
  345. static struct ata_port_info info_hpt366 = {
  346. .sht = &hpt36x_sht,
  347. .flags = ATA_FLAG_SLAVE_POSS | ATA_FLAG_SRST,
  348. .pio_mask = 0x1f,
  349. .mwdma_mask = 0x07,
  350. .udma_mask = 0x1f,
  351. .port_ops = &hpt366_port_ops
  352. };
  353. struct ata_port_info *port_info[2] = {&info_hpt366, &info_hpt366};
  354. u32 class_rev;
  355. u32 reg1;
  356. u8 drive_fast;
  357. pci_read_config_dword(dev, PCI_CLASS_REVISION, &class_rev);
  358. class_rev &= 0xFF;
  359. /* May be a later chip in disguise. Check */
  360. /* Newer chips are not in the HPT36x driver. Ignore them */
  361. if (class_rev > 2)
  362. return -ENODEV;
  363. pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE, (L1_CACHE_BYTES / 4));
  364. pci_write_config_byte(dev, PCI_LATENCY_TIMER, 0x78);
  365. pci_write_config_byte(dev, PCI_MIN_GNT, 0x08);
  366. pci_write_config_byte(dev, PCI_MAX_LAT, 0x08);
  367. pci_read_config_byte(dev, 0x51, &drive_fast);
  368. if (drive_fast & 0x80)
  369. pci_write_config_byte(dev, 0x51, drive_fast & ~0x80);
  370. pci_read_config_dword(dev, 0x40, &reg1);
  371. /* PCI clocking determines the ATA timing values to use */
  372. /* info_hpt366 is safe against re-entry so we can scribble on it */
  373. switch((reg1 & 0x700) >> 8) {
  374. case 5:
  375. info_hpt366.private_data = &hpt366_40;
  376. break;
  377. case 9:
  378. info_hpt366.private_data = &hpt366_25;
  379. break;
  380. default:
  381. info_hpt366.private_data = &hpt366_33;
  382. break;
  383. }
  384. /* Now kick off ATA set up */
  385. return ata_pci_init_one(dev, port_info, 2);
  386. }
  387. static const struct pci_device_id hpt36x[] = {
  388. { PCI_VDEVICE(TTI, PCI_DEVICE_ID_TTI_HPT366), },
  389. { },
  390. };
  391. static struct pci_driver hpt36x_pci_driver = {
  392. .name = DRV_NAME,
  393. .id_table = hpt36x,
  394. .probe = hpt36x_init_one,
  395. .remove = ata_pci_remove_one
  396. };
  397. static int __init hpt36x_init(void)
  398. {
  399. return pci_register_driver(&hpt36x_pci_driver);
  400. }
  401. static void __exit hpt36x_exit(void)
  402. {
  403. pci_unregister_driver(&hpt36x_pci_driver);
  404. }
  405. MODULE_AUTHOR("Alan Cox");
  406. MODULE_DESCRIPTION("low-level driver for the Highpoint HPT366/368");
  407. MODULE_LICENSE("GPL");
  408. MODULE_DEVICE_TABLE(pci, hpt36x);
  409. MODULE_VERSION(DRV_VERSION);
  410. module_init(hpt36x_init);
  411. module_exit(hpt36x_exit);