pata_hpt37x.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041
  1. /*
  2. * Libata driver for the highpoint 37x and 30x 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. * Portions Copyright (C) 2005-2009 MontaVista Software, Inc.
  12. *
  13. * TODO
  14. * Look into engine reset on timeout errors. Should not be required.
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/pci.h>
  19. #include <linux/init.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_hpt37x"
  25. #define DRV_VERSION "0.6.12"
  26. struct hpt_clock {
  27. u8 xfer_speed;
  28. u32 timing;
  29. };
  30. struct hpt_chip {
  31. const char *name;
  32. unsigned int base;
  33. struct hpt_clock const *clocks[4];
  34. };
  35. /* key for bus clock timings
  36. * bit
  37. * 0:3 data_high_time. inactive time of DIOW_/DIOR_ for PIO and MW
  38. * DMA. cycles = value + 1
  39. * 4:8 data_low_time. active time of DIOW_/DIOR_ for PIO and MW
  40. * DMA. cycles = value + 1
  41. * 9:12 cmd_high_time. inactive time of DIOW_/DIOR_ during task file
  42. * register access.
  43. * 13:17 cmd_low_time. active time of DIOW_/DIOR_ during task file
  44. * register access.
  45. * 18:21 udma_cycle_time. clock freq and clock cycles for UDMA xfer.
  46. * during task file register access.
  47. * 22:24 pre_high_time. time to initialize 1st cycle for PIO and MW DMA
  48. * xfer.
  49. * 25:27 cmd_pre_high_time. time to initialize 1st PIO cycle for task
  50. * register access.
  51. * 28 UDMA enable
  52. * 29 DMA enable
  53. * 30 PIO_MST enable. if set, the chip is in bus master mode during
  54. * PIO.
  55. * 31 FIFO enable.
  56. */
  57. static struct hpt_clock hpt37x_timings_33[] = {
  58. { XFER_UDMA_6, 0x12446231 }, /* 0x12646231 ?? */
  59. { XFER_UDMA_5, 0x12446231 },
  60. { XFER_UDMA_4, 0x12446231 },
  61. { XFER_UDMA_3, 0x126c6231 },
  62. { XFER_UDMA_2, 0x12486231 },
  63. { XFER_UDMA_1, 0x124c6233 },
  64. { XFER_UDMA_0, 0x12506297 },
  65. { XFER_MW_DMA_2, 0x22406c31 },
  66. { XFER_MW_DMA_1, 0x22406c33 },
  67. { XFER_MW_DMA_0, 0x22406c97 },
  68. { XFER_PIO_4, 0x06414e31 },
  69. { XFER_PIO_3, 0x06414e42 },
  70. { XFER_PIO_2, 0x06414e53 },
  71. { XFER_PIO_1, 0x06814e93 },
  72. { XFER_PIO_0, 0x06814ea7 }
  73. };
  74. static struct hpt_clock hpt37x_timings_50[] = {
  75. { XFER_UDMA_6, 0x12848242 },
  76. { XFER_UDMA_5, 0x12848242 },
  77. { XFER_UDMA_4, 0x12ac8242 },
  78. { XFER_UDMA_3, 0x128c8242 },
  79. { XFER_UDMA_2, 0x120c8242 },
  80. { XFER_UDMA_1, 0x12148254 },
  81. { XFER_UDMA_0, 0x121882ea },
  82. { XFER_MW_DMA_2, 0x22808242 },
  83. { XFER_MW_DMA_1, 0x22808254 },
  84. { XFER_MW_DMA_0, 0x228082ea },
  85. { XFER_PIO_4, 0x0a81f442 },
  86. { XFER_PIO_3, 0x0a81f443 },
  87. { XFER_PIO_2, 0x0a81f454 },
  88. { XFER_PIO_1, 0x0ac1f465 },
  89. { XFER_PIO_0, 0x0ac1f48a }
  90. };
  91. static struct hpt_clock hpt37x_timings_66[] = {
  92. { XFER_UDMA_6, 0x1c869c62 },
  93. { XFER_UDMA_5, 0x1cae9c62 }, /* 0x1c8a9c62 */
  94. { XFER_UDMA_4, 0x1c8a9c62 },
  95. { XFER_UDMA_3, 0x1c8e9c62 },
  96. { XFER_UDMA_2, 0x1c929c62 },
  97. { XFER_UDMA_1, 0x1c9a9c62 },
  98. { XFER_UDMA_0, 0x1c829c62 },
  99. { XFER_MW_DMA_2, 0x2c829c62 },
  100. { XFER_MW_DMA_1, 0x2c829c66 },
  101. { XFER_MW_DMA_0, 0x2c829d2e },
  102. { XFER_PIO_4, 0x0c829c62 },
  103. { XFER_PIO_3, 0x0c829c84 },
  104. { XFER_PIO_2, 0x0c829ca6 },
  105. { XFER_PIO_1, 0x0d029d26 },
  106. { XFER_PIO_0, 0x0d029d5e }
  107. };
  108. static const struct hpt_chip hpt370 = {
  109. "HPT370",
  110. 48,
  111. {
  112. hpt37x_timings_33,
  113. NULL,
  114. NULL,
  115. NULL
  116. }
  117. };
  118. static const struct hpt_chip hpt370a = {
  119. "HPT370A",
  120. 48,
  121. {
  122. hpt37x_timings_33,
  123. NULL,
  124. hpt37x_timings_50,
  125. NULL
  126. }
  127. };
  128. static const struct hpt_chip hpt372 = {
  129. "HPT372",
  130. 55,
  131. {
  132. hpt37x_timings_33,
  133. NULL,
  134. hpt37x_timings_50,
  135. hpt37x_timings_66
  136. }
  137. };
  138. static const struct hpt_chip hpt302 = {
  139. "HPT302",
  140. 66,
  141. {
  142. hpt37x_timings_33,
  143. NULL,
  144. hpt37x_timings_50,
  145. hpt37x_timings_66
  146. }
  147. };
  148. static const struct hpt_chip hpt371 = {
  149. "HPT371",
  150. 66,
  151. {
  152. hpt37x_timings_33,
  153. NULL,
  154. hpt37x_timings_50,
  155. hpt37x_timings_66
  156. }
  157. };
  158. static const struct hpt_chip hpt372a = {
  159. "HPT372A",
  160. 66,
  161. {
  162. hpt37x_timings_33,
  163. NULL,
  164. hpt37x_timings_50,
  165. hpt37x_timings_66
  166. }
  167. };
  168. static const struct hpt_chip hpt374 = {
  169. "HPT374",
  170. 48,
  171. {
  172. hpt37x_timings_33,
  173. NULL,
  174. NULL,
  175. NULL
  176. }
  177. };
  178. /**
  179. * hpt37x_find_mode - reset the hpt37x bus
  180. * @ap: ATA port
  181. * @speed: transfer mode
  182. *
  183. * Return the 32bit register programming information for this channel
  184. * that matches the speed provided.
  185. */
  186. static u32 hpt37x_find_mode(struct ata_port *ap, int speed)
  187. {
  188. struct hpt_clock *clocks = ap->host->private_data;
  189. while(clocks->xfer_speed) {
  190. if (clocks->xfer_speed == speed)
  191. return clocks->timing;
  192. clocks++;
  193. }
  194. BUG();
  195. return 0xffffffffU; /* silence compiler warning */
  196. }
  197. static int hpt_dma_blacklisted(const struct ata_device *dev, char *modestr, const char *list[])
  198. {
  199. unsigned char model_num[ATA_ID_PROD_LEN + 1];
  200. int i = 0;
  201. ata_id_c_string(dev->id, model_num, ATA_ID_PROD, sizeof(model_num));
  202. while (list[i] != NULL) {
  203. if (!strcmp(list[i], model_num)) {
  204. printk(KERN_WARNING DRV_NAME ": %s is not supported for %s.\n",
  205. modestr, list[i]);
  206. return 1;
  207. }
  208. i++;
  209. }
  210. return 0;
  211. }
  212. static const char *bad_ata33[] = {
  213. "Maxtor 92720U8", "Maxtor 92040U6", "Maxtor 91360U4", "Maxtor 91020U3", "Maxtor 90845U3", "Maxtor 90650U2",
  214. "Maxtor 91360D8", "Maxtor 91190D7", "Maxtor 91020D6", "Maxtor 90845D5", "Maxtor 90680D4", "Maxtor 90510D3", "Maxtor 90340D2",
  215. "Maxtor 91152D8", "Maxtor 91008D7", "Maxtor 90845D6", "Maxtor 90840D6", "Maxtor 90720D5", "Maxtor 90648D5", "Maxtor 90576D4",
  216. "Maxtor 90510D4",
  217. "Maxtor 90432D3", "Maxtor 90288D2", "Maxtor 90256D2",
  218. "Maxtor 91000D8", "Maxtor 90910D8", "Maxtor 90875D7", "Maxtor 90840D7", "Maxtor 90750D6", "Maxtor 90625D5", "Maxtor 90500D4",
  219. "Maxtor 91728D8", "Maxtor 91512D7", "Maxtor 91303D6", "Maxtor 91080D5", "Maxtor 90845D4", "Maxtor 90680D4", "Maxtor 90648D3", "Maxtor 90432D2",
  220. NULL
  221. };
  222. static const char *bad_ata100_5[] = {
  223. "IBM-DTLA-307075",
  224. "IBM-DTLA-307060",
  225. "IBM-DTLA-307045",
  226. "IBM-DTLA-307030",
  227. "IBM-DTLA-307020",
  228. "IBM-DTLA-307015",
  229. "IBM-DTLA-305040",
  230. "IBM-DTLA-305030",
  231. "IBM-DTLA-305020",
  232. "IC35L010AVER07-0",
  233. "IC35L020AVER07-0",
  234. "IC35L030AVER07-0",
  235. "IC35L040AVER07-0",
  236. "IC35L060AVER07-0",
  237. "WDC AC310200R",
  238. NULL
  239. };
  240. /**
  241. * hpt370_filter - mode selection filter
  242. * @adev: ATA device
  243. *
  244. * Block UDMA on devices that cause trouble with this controller.
  245. */
  246. static unsigned long hpt370_filter(struct ata_device *adev, unsigned long mask)
  247. {
  248. if (adev->class == ATA_DEV_ATA) {
  249. if (hpt_dma_blacklisted(adev, "UDMA", bad_ata33))
  250. mask &= ~ATA_MASK_UDMA;
  251. if (hpt_dma_blacklisted(adev, "UDMA100", bad_ata100_5))
  252. mask &= ~(0xE0 << ATA_SHIFT_UDMA);
  253. }
  254. return ata_bmdma_mode_filter(adev, mask);
  255. }
  256. /**
  257. * hpt370a_filter - mode selection filter
  258. * @adev: ATA device
  259. *
  260. * Block UDMA on devices that cause trouble with this controller.
  261. */
  262. static unsigned long hpt370a_filter(struct ata_device *adev, unsigned long mask)
  263. {
  264. if (adev->class == ATA_DEV_ATA) {
  265. if (hpt_dma_blacklisted(adev, "UDMA100", bad_ata100_5))
  266. mask &= ~(0xE0 << ATA_SHIFT_UDMA);
  267. }
  268. return ata_bmdma_mode_filter(adev, mask);
  269. }
  270. /**
  271. * hpt37x_pre_reset - reset the hpt37x bus
  272. * @link: ATA link to reset
  273. * @deadline: deadline jiffies for the operation
  274. *
  275. * Perform the initial reset handling for the 370/372 and 374 func 0
  276. */
  277. static int hpt37x_pre_reset(struct ata_link *link, unsigned long deadline)
  278. {
  279. u8 scr2, ata66;
  280. struct ata_port *ap = link->ap;
  281. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  282. static const struct pci_bits hpt37x_enable_bits[] = {
  283. { 0x50, 1, 0x04, 0x04 },
  284. { 0x54, 1, 0x04, 0x04 }
  285. };
  286. if (!pci_test_config_bits(pdev, &hpt37x_enable_bits[ap->port_no]))
  287. return -ENOENT;
  288. pci_read_config_byte(pdev, 0x5B, &scr2);
  289. pci_write_config_byte(pdev, 0x5B, scr2 & ~0x01);
  290. /* Cable register now active */
  291. pci_read_config_byte(pdev, 0x5A, &ata66);
  292. /* Restore state */
  293. pci_write_config_byte(pdev, 0x5B, scr2);
  294. if (ata66 & (2 >> ap->port_no))
  295. ap->cbl = ATA_CBL_PATA40;
  296. else
  297. ap->cbl = ATA_CBL_PATA80;
  298. /* Reset the state machine */
  299. pci_write_config_byte(pdev, 0x50 + 4 * ap->port_no, 0x37);
  300. udelay(100);
  301. return ata_sff_prereset(link, deadline);
  302. }
  303. static int hpt374_fn1_pre_reset(struct ata_link *link, unsigned long deadline)
  304. {
  305. static const struct pci_bits hpt37x_enable_bits[] = {
  306. { 0x50, 1, 0x04, 0x04 },
  307. { 0x54, 1, 0x04, 0x04 }
  308. };
  309. u16 mcr3;
  310. u8 ata66;
  311. struct ata_port *ap = link->ap;
  312. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  313. unsigned int mcrbase = 0x50 + 4 * ap->port_no;
  314. if (!pci_test_config_bits(pdev, &hpt37x_enable_bits[ap->port_no]))
  315. return -ENOENT;
  316. /* Do the extra channel work */
  317. pci_read_config_word(pdev, mcrbase + 2, &mcr3);
  318. /* Set bit 15 of 0x52 to enable TCBLID as input
  319. */
  320. pci_write_config_word(pdev, mcrbase + 2, mcr3 | 0x8000);
  321. pci_read_config_byte(pdev, 0x5A, &ata66);
  322. /* Reset TCBLID/FCBLID to output */
  323. pci_write_config_word(pdev, mcrbase + 2, mcr3);
  324. if (ata66 & (2 >> ap->port_no))
  325. ap->cbl = ATA_CBL_PATA40;
  326. else
  327. ap->cbl = ATA_CBL_PATA80;
  328. /* Reset the state machine */
  329. pci_write_config_byte(pdev, 0x50 + 4 * ap->port_no, 0x37);
  330. udelay(100);
  331. return ata_sff_prereset(link, deadline);
  332. }
  333. /**
  334. * hpt370_set_piomode - PIO setup
  335. * @ap: ATA interface
  336. * @adev: device on the interface
  337. *
  338. * Perform PIO mode setup.
  339. */
  340. static void hpt370_set_piomode(struct ata_port *ap, struct ata_device *adev)
  341. {
  342. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  343. u32 addr1, addr2;
  344. u32 reg;
  345. u32 mode;
  346. u8 fast;
  347. addr1 = 0x40 + 4 * (adev->devno + 2 * ap->port_no);
  348. addr2 = 0x51 + 4 * ap->port_no;
  349. /* Fast interrupt prediction disable, hold off interrupt disable */
  350. pci_read_config_byte(pdev, addr2, &fast);
  351. fast &= ~0x02;
  352. fast |= 0x01;
  353. pci_write_config_byte(pdev, addr2, fast);
  354. pci_read_config_dword(pdev, addr1, &reg);
  355. mode = hpt37x_find_mode(ap, adev->pio_mode);
  356. mode &= ~0x8000000; /* No FIFO in PIO */
  357. mode &= ~0x30070000; /* Leave config bits alone */
  358. reg &= 0x30070000; /* Strip timing bits */
  359. pci_write_config_dword(pdev, addr1, reg | mode);
  360. }
  361. /**
  362. * hpt370_set_dmamode - DMA timing setup
  363. * @ap: ATA interface
  364. * @adev: Device being configured
  365. *
  366. * Set up the channel for MWDMA or UDMA modes. Much the same as with
  367. * PIO, load the mode number and then set MWDMA or UDMA flag.
  368. */
  369. static void hpt370_set_dmamode(struct ata_port *ap, struct ata_device *adev)
  370. {
  371. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  372. u32 addr1, addr2;
  373. u32 reg;
  374. u32 mode;
  375. u8 fast;
  376. addr1 = 0x40 + 4 * (adev->devno + 2 * ap->port_no);
  377. addr2 = 0x51 + 4 * ap->port_no;
  378. /* Fast interrupt prediction disable, hold off interrupt disable */
  379. pci_read_config_byte(pdev, addr2, &fast);
  380. fast &= ~0x02;
  381. fast |= 0x01;
  382. pci_write_config_byte(pdev, addr2, fast);
  383. pci_read_config_dword(pdev, addr1, &reg);
  384. mode = hpt37x_find_mode(ap, adev->dma_mode);
  385. mode |= 0x8000000; /* FIFO in MWDMA or UDMA */
  386. mode &= ~0xC0000000; /* Leave config bits alone */
  387. reg &= 0xC0000000; /* Strip timing bits */
  388. pci_write_config_dword(pdev, addr1, reg | mode);
  389. }
  390. /**
  391. * hpt370_bmdma_end - DMA engine stop
  392. * @qc: ATA command
  393. *
  394. * Work around the HPT370 DMA engine.
  395. */
  396. static void hpt370_bmdma_stop(struct ata_queued_cmd *qc)
  397. {
  398. struct ata_port *ap = qc->ap;
  399. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  400. u8 dma_stat = ioread8(ap->ioaddr.bmdma_addr + 2);
  401. u8 dma_cmd;
  402. void __iomem *bmdma = ap->ioaddr.bmdma_addr;
  403. if (dma_stat & 0x01) {
  404. udelay(20);
  405. dma_stat = ioread8(bmdma + 2);
  406. }
  407. if (dma_stat & 0x01) {
  408. /* Clear the engine */
  409. pci_write_config_byte(pdev, 0x50 + 4 * ap->port_no, 0x37);
  410. udelay(10);
  411. /* Stop DMA */
  412. dma_cmd = ioread8(bmdma );
  413. iowrite8(dma_cmd & 0xFE, bmdma);
  414. /* Clear Error */
  415. dma_stat = ioread8(bmdma + 2);
  416. iowrite8(dma_stat | 0x06 , bmdma + 2);
  417. /* Clear the engine */
  418. pci_write_config_byte(pdev, 0x50 + 4 * ap->port_no, 0x37);
  419. udelay(10);
  420. }
  421. ata_bmdma_stop(qc);
  422. }
  423. /**
  424. * hpt372_set_piomode - PIO setup
  425. * @ap: ATA interface
  426. * @adev: device on the interface
  427. *
  428. * Perform PIO mode setup.
  429. */
  430. static void hpt372_set_piomode(struct ata_port *ap, struct ata_device *adev)
  431. {
  432. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  433. u32 addr1, addr2;
  434. u32 reg;
  435. u32 mode;
  436. u8 fast;
  437. addr1 = 0x40 + 4 * (adev->devno + 2 * ap->port_no);
  438. addr2 = 0x51 + 4 * ap->port_no;
  439. /* Fast interrupt prediction disable, hold off interrupt disable */
  440. pci_read_config_byte(pdev, addr2, &fast);
  441. fast &= ~0x07;
  442. pci_write_config_byte(pdev, addr2, fast);
  443. pci_read_config_dword(pdev, addr1, &reg);
  444. mode = hpt37x_find_mode(ap, adev->pio_mode);
  445. printk("Find mode for %d reports %X\n", adev->pio_mode, mode);
  446. mode &= ~0x80000000; /* No FIFO in PIO */
  447. mode &= ~0x30070000; /* Leave config bits alone */
  448. reg &= 0x30070000; /* Strip timing bits */
  449. pci_write_config_dword(pdev, addr1, reg | mode);
  450. }
  451. /**
  452. * hpt372_set_dmamode - DMA timing setup
  453. * @ap: ATA interface
  454. * @adev: Device being configured
  455. *
  456. * Set up the channel for MWDMA or UDMA modes. Much the same as with
  457. * PIO, load the mode number and then set MWDMA or UDMA flag.
  458. */
  459. static void hpt372_set_dmamode(struct ata_port *ap, struct ata_device *adev)
  460. {
  461. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  462. u32 addr1, addr2;
  463. u32 reg;
  464. u32 mode;
  465. u8 fast;
  466. addr1 = 0x40 + 4 * (adev->devno + 2 * ap->port_no);
  467. addr2 = 0x51 + 4 * ap->port_no;
  468. /* Fast interrupt prediction disable, hold off interrupt disable */
  469. pci_read_config_byte(pdev, addr2, &fast);
  470. fast &= ~0x07;
  471. pci_write_config_byte(pdev, addr2, fast);
  472. pci_read_config_dword(pdev, addr1, &reg);
  473. mode = hpt37x_find_mode(ap, adev->dma_mode);
  474. printk("Find mode for DMA %d reports %X\n", adev->dma_mode, mode);
  475. mode &= ~0xC0000000; /* Leave config bits alone */
  476. mode |= 0x80000000; /* FIFO in MWDMA or UDMA */
  477. reg &= 0xC0000000; /* Strip timing bits */
  478. pci_write_config_dword(pdev, addr1, reg | mode);
  479. }
  480. /**
  481. * hpt37x_bmdma_end - DMA engine stop
  482. * @qc: ATA command
  483. *
  484. * Clean up after the HPT372 and later DMA engine
  485. */
  486. static void hpt37x_bmdma_stop(struct ata_queued_cmd *qc)
  487. {
  488. struct ata_port *ap = qc->ap;
  489. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  490. int mscreg = 0x50 + 4 * ap->port_no;
  491. u8 bwsr_stat, msc_stat;
  492. pci_read_config_byte(pdev, 0x6A, &bwsr_stat);
  493. pci_read_config_byte(pdev, mscreg, &msc_stat);
  494. if (bwsr_stat & (1 << ap->port_no))
  495. pci_write_config_byte(pdev, mscreg, msc_stat | 0x30);
  496. ata_bmdma_stop(qc);
  497. }
  498. static struct scsi_host_template hpt37x_sht = {
  499. ATA_BMDMA_SHT(DRV_NAME),
  500. };
  501. /*
  502. * Configuration for HPT370
  503. */
  504. static struct ata_port_operations hpt370_port_ops = {
  505. .inherits = &ata_bmdma_port_ops,
  506. .bmdma_stop = hpt370_bmdma_stop,
  507. .mode_filter = hpt370_filter,
  508. .set_piomode = hpt370_set_piomode,
  509. .set_dmamode = hpt370_set_dmamode,
  510. .prereset = hpt37x_pre_reset,
  511. };
  512. /*
  513. * Configuration for HPT370A. Close to 370 but less filters
  514. */
  515. static struct ata_port_operations hpt370a_port_ops = {
  516. .inherits = &hpt370_port_ops,
  517. .mode_filter = hpt370a_filter,
  518. };
  519. /*
  520. * Configuration for HPT372, HPT371, HPT302. Slightly different PIO
  521. * and DMA mode setting functionality.
  522. */
  523. static struct ata_port_operations hpt372_port_ops = {
  524. .inherits = &ata_bmdma_port_ops,
  525. .bmdma_stop = hpt37x_bmdma_stop,
  526. .set_piomode = hpt372_set_piomode,
  527. .set_dmamode = hpt372_set_dmamode,
  528. .prereset = hpt37x_pre_reset,
  529. };
  530. /*
  531. * Configuration for HPT374. Mode setting works like 372 and friends
  532. * but we have a different cable detection procedure for function 1.
  533. */
  534. static struct ata_port_operations hpt374_fn1_port_ops = {
  535. .inherits = &hpt372_port_ops,
  536. .prereset = hpt374_fn1_pre_reset,
  537. };
  538. /**
  539. * hpt37x_clock_slot - Turn timing to PC clock entry
  540. * @freq: Reported frequency timing
  541. * @base: Base timing
  542. *
  543. * Turn the timing data intoa clock slot (0 for 33, 1 for 40, 2 for 50
  544. * and 3 for 66Mhz)
  545. */
  546. static int hpt37x_clock_slot(unsigned int freq, unsigned int base)
  547. {
  548. unsigned int f = (base * freq) / 192; /* Mhz */
  549. if (f < 40)
  550. return 0; /* 33Mhz slot */
  551. if (f < 45)
  552. return 1; /* 40Mhz slot */
  553. if (f < 55)
  554. return 2; /* 50Mhz slot */
  555. return 3; /* 60Mhz slot */
  556. }
  557. /**
  558. * hpt37x_calibrate_dpll - Calibrate the DPLL loop
  559. * @dev: PCI device
  560. *
  561. * Perform a calibration cycle on the HPT37x DPLL. Returns 1 if this
  562. * succeeds
  563. */
  564. static int hpt37x_calibrate_dpll(struct pci_dev *dev)
  565. {
  566. u8 reg5b;
  567. u32 reg5c;
  568. int tries;
  569. for(tries = 0; tries < 0x5000; tries++) {
  570. udelay(50);
  571. pci_read_config_byte(dev, 0x5b, &reg5b);
  572. if (reg5b & 0x80) {
  573. /* See if it stays set */
  574. for(tries = 0; tries < 0x1000; tries ++) {
  575. pci_read_config_byte(dev, 0x5b, &reg5b);
  576. /* Failed ? */
  577. if ((reg5b & 0x80) == 0)
  578. return 0;
  579. }
  580. /* Turn off tuning, we have the DPLL set */
  581. pci_read_config_dword(dev, 0x5c, &reg5c);
  582. pci_write_config_dword(dev, 0x5c, reg5c & ~ 0x100);
  583. return 1;
  584. }
  585. }
  586. /* Never went stable */
  587. return 0;
  588. }
  589. static u32 hpt374_read_freq(struct pci_dev *pdev)
  590. {
  591. u32 freq;
  592. unsigned long io_base = pci_resource_start(pdev, 4);
  593. if (PCI_FUNC(pdev->devfn) & 1) {
  594. struct pci_dev *pdev_0;
  595. pdev_0 = pci_get_slot(pdev->bus, pdev->devfn - 1);
  596. /* Someone hot plugged the controller on us ? */
  597. if (pdev_0 == NULL)
  598. return 0;
  599. io_base = pci_resource_start(pdev_0, 4);
  600. freq = inl(io_base + 0x90);
  601. pci_dev_put(pdev_0);
  602. } else
  603. freq = inl(io_base + 0x90);
  604. return freq;
  605. }
  606. /**
  607. * hpt37x_init_one - Initialise an HPT37X/302
  608. * @dev: PCI device
  609. * @id: Entry in match table
  610. *
  611. * Initialise an HPT37x device. There are some interesting complications
  612. * here. Firstly the chip may report 366 and be one of several variants.
  613. * Secondly all the timings depend on the clock for the chip which we must
  614. * detect and look up
  615. *
  616. * This is the known chip mappings. It may be missing a couple of later
  617. * releases.
  618. *
  619. * Chip version PCI Rev Notes
  620. * HPT366 4 (HPT366) 0 Other driver
  621. * HPT366 4 (HPT366) 1 Other driver
  622. * HPT368 4 (HPT366) 2 Other driver
  623. * HPT370 4 (HPT366) 3 UDMA100
  624. * HPT370A 4 (HPT366) 4 UDMA100
  625. * HPT372 4 (HPT366) 5 UDMA133 (1)
  626. * HPT372N 4 (HPT366) 6 Other driver
  627. * HPT372A 5 (HPT372) 1 UDMA133 (1)
  628. * HPT372N 5 (HPT372) 2 Other driver
  629. * HPT302 6 (HPT302) 1 UDMA133
  630. * HPT302N 6 (HPT302) 2 Other driver
  631. * HPT371 7 (HPT371) * UDMA133
  632. * HPT374 8 (HPT374) * UDMA133 4 channel
  633. * HPT372N 9 (HPT372N) * Other driver
  634. *
  635. * (1) UDMA133 support depends on the bus clock
  636. */
  637. static int hpt37x_init_one(struct pci_dev *dev, const struct pci_device_id *id)
  638. {
  639. /* HPT370 - UDMA100 */
  640. static const struct ata_port_info info_hpt370 = {
  641. .flags = ATA_FLAG_SLAVE_POSS,
  642. .pio_mask = ATA_PIO4,
  643. .mwdma_mask = ATA_MWDMA2,
  644. .udma_mask = ATA_UDMA5,
  645. .port_ops = &hpt370_port_ops
  646. };
  647. /* HPT370A - UDMA100 */
  648. static const struct ata_port_info info_hpt370a = {
  649. .flags = ATA_FLAG_SLAVE_POSS,
  650. .pio_mask = ATA_PIO4,
  651. .mwdma_mask = ATA_MWDMA2,
  652. .udma_mask = ATA_UDMA5,
  653. .port_ops = &hpt370a_port_ops
  654. };
  655. /* HPT370 - UDMA100 */
  656. static const struct ata_port_info info_hpt370_33 = {
  657. .flags = ATA_FLAG_SLAVE_POSS,
  658. .pio_mask = ATA_PIO4,
  659. .mwdma_mask = ATA_MWDMA2,
  660. .udma_mask = ATA_UDMA5,
  661. .port_ops = &hpt370_port_ops
  662. };
  663. /* HPT370A - UDMA100 */
  664. static const struct ata_port_info info_hpt370a_33 = {
  665. .flags = ATA_FLAG_SLAVE_POSS,
  666. .pio_mask = ATA_PIO4,
  667. .mwdma_mask = ATA_MWDMA2,
  668. .udma_mask = ATA_UDMA5,
  669. .port_ops = &hpt370a_port_ops
  670. };
  671. /* HPT371, 372 and friends - UDMA133 */
  672. static const struct ata_port_info info_hpt372 = {
  673. .flags = ATA_FLAG_SLAVE_POSS,
  674. .pio_mask = ATA_PIO4,
  675. .mwdma_mask = ATA_MWDMA2,
  676. .udma_mask = ATA_UDMA6,
  677. .port_ops = &hpt372_port_ops
  678. };
  679. /* HPT374 - UDMA100, function 1 uses different prereset method */
  680. static const struct ata_port_info info_hpt374_fn0 = {
  681. .flags = ATA_FLAG_SLAVE_POSS,
  682. .pio_mask = ATA_PIO4,
  683. .mwdma_mask = ATA_MWDMA2,
  684. .udma_mask = ATA_UDMA5,
  685. .port_ops = &hpt372_port_ops
  686. };
  687. static const struct ata_port_info info_hpt374_fn1 = {
  688. .flags = ATA_FLAG_SLAVE_POSS,
  689. .pio_mask = ATA_PIO4,
  690. .mwdma_mask = ATA_MWDMA2,
  691. .udma_mask = ATA_UDMA5,
  692. .port_ops = &hpt374_fn1_port_ops
  693. };
  694. static const int MHz[4] = { 33, 40, 50, 66 };
  695. void *private_data = NULL;
  696. const struct ata_port_info *ppi[] = { NULL, NULL };
  697. u8 irqmask;
  698. u32 class_rev;
  699. u8 mcr1;
  700. u32 freq;
  701. int prefer_dpll = 1;
  702. unsigned long iobase = pci_resource_start(dev, 4);
  703. const struct hpt_chip *chip_table;
  704. int clock_slot;
  705. int rc;
  706. rc = pcim_enable_device(dev);
  707. if (rc)
  708. return rc;
  709. pci_read_config_dword(dev, PCI_CLASS_REVISION, &class_rev);
  710. class_rev &= 0xFF;
  711. if (dev->device == PCI_DEVICE_ID_TTI_HPT366) {
  712. /* May be a later chip in disguise. Check */
  713. /* Older chips are in the HPT366 driver. Ignore them */
  714. if (class_rev < 3)
  715. return -ENODEV;
  716. /* N series chips have their own driver. Ignore */
  717. if (class_rev == 6)
  718. return -ENODEV;
  719. switch(class_rev) {
  720. case 3:
  721. ppi[0] = &info_hpt370;
  722. chip_table = &hpt370;
  723. prefer_dpll = 0;
  724. break;
  725. case 4:
  726. ppi[0] = &info_hpt370a;
  727. chip_table = &hpt370a;
  728. prefer_dpll = 0;
  729. break;
  730. case 5:
  731. ppi[0] = &info_hpt372;
  732. chip_table = &hpt372;
  733. break;
  734. default:
  735. printk(KERN_ERR "pata_hpt37x: Unknown HPT366 subtype please report (%d).\n", class_rev);
  736. return -ENODEV;
  737. }
  738. } else {
  739. switch(dev->device) {
  740. case PCI_DEVICE_ID_TTI_HPT372:
  741. /* 372N if rev >= 2*/
  742. if (class_rev >= 2)
  743. return -ENODEV;
  744. ppi[0] = &info_hpt372;
  745. chip_table = &hpt372a;
  746. break;
  747. case PCI_DEVICE_ID_TTI_HPT302:
  748. /* 302N if rev > 1 */
  749. if (class_rev > 1)
  750. return -ENODEV;
  751. ppi[0] = &info_hpt372;
  752. /* Check this */
  753. chip_table = &hpt302;
  754. break;
  755. case PCI_DEVICE_ID_TTI_HPT371:
  756. if (class_rev > 1)
  757. return -ENODEV;
  758. ppi[0] = &info_hpt372;
  759. chip_table = &hpt371;
  760. /* Single channel device, master is not present
  761. but the BIOS (or us for non x86) must mark it
  762. absent */
  763. pci_read_config_byte(dev, 0x50, &mcr1);
  764. mcr1 &= ~0x04;
  765. pci_write_config_byte(dev, 0x50, mcr1);
  766. break;
  767. case PCI_DEVICE_ID_TTI_HPT374:
  768. chip_table = &hpt374;
  769. if (!(PCI_FUNC(dev->devfn) & 1))
  770. *ppi = &info_hpt374_fn0;
  771. else
  772. *ppi = &info_hpt374_fn1;
  773. break;
  774. default:
  775. printk(KERN_ERR "pata_hpt37x: PCI table is bogus please report (%d).\n", dev->device);
  776. return -ENODEV;
  777. }
  778. }
  779. /* Ok so this is a chip we support */
  780. pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE, (L1_CACHE_BYTES / 4));
  781. pci_write_config_byte(dev, PCI_LATENCY_TIMER, 0x78);
  782. pci_write_config_byte(dev, PCI_MIN_GNT, 0x08);
  783. pci_write_config_byte(dev, PCI_MAX_LAT, 0x08);
  784. pci_read_config_byte(dev, 0x5A, &irqmask);
  785. irqmask &= ~0x10;
  786. pci_write_config_byte(dev, 0x5a, irqmask);
  787. /*
  788. * default to pci clock. make sure MA15/16 are set to output
  789. * to prevent drives having problems with 40-pin cables. Needed
  790. * for some drives such as IBM-DTLA which will not enter ready
  791. * state on reset when PDIAG is a input.
  792. */
  793. pci_write_config_byte(dev, 0x5b, 0x23);
  794. /*
  795. * HighPoint does this for HPT372A.
  796. * NOTE: This register is only writeable via I/O space.
  797. */
  798. if (chip_table == &hpt372a)
  799. outb(0x0e, iobase + 0x9c);
  800. /* Some devices do not let this value be accessed via PCI space
  801. according to the old driver. In addition we must use the value
  802. from FN 0 on the HPT374 */
  803. if (chip_table == &hpt374) {
  804. freq = hpt374_read_freq(dev);
  805. if (freq == 0)
  806. return -ENODEV;
  807. } else
  808. freq = inl(iobase + 0x90);
  809. if ((freq >> 12) != 0xABCDE) {
  810. int i;
  811. u8 sr;
  812. u32 total = 0;
  813. printk(KERN_WARNING "pata_hpt37x: BIOS has not set timing clocks.\n");
  814. /* This is the process the HPT371 BIOS is reported to use */
  815. for(i = 0; i < 128; i++) {
  816. pci_read_config_byte(dev, 0x78, &sr);
  817. total += sr & 0x1FF;
  818. udelay(15);
  819. }
  820. freq = total / 128;
  821. }
  822. freq &= 0x1FF;
  823. /*
  824. * Turn the frequency check into a band and then find a timing
  825. * table to match it.
  826. */
  827. clock_slot = hpt37x_clock_slot(freq, chip_table->base);
  828. if (chip_table->clocks[clock_slot] == NULL || prefer_dpll) {
  829. /*
  830. * We need to try PLL mode instead
  831. *
  832. * For non UDMA133 capable devices we should
  833. * use a 50MHz DPLL by choice
  834. */
  835. unsigned int f_low, f_high;
  836. int dpll, adjust;
  837. /* Compute DPLL */
  838. dpll = (ppi[0]->udma_mask & 0xC0) ? 3 : 2;
  839. f_low = (MHz[clock_slot] * 48) / MHz[dpll];
  840. f_high = f_low + 2;
  841. if (clock_slot > 1)
  842. f_high += 2;
  843. /* Select the DPLL clock. */
  844. pci_write_config_byte(dev, 0x5b, 0x21);
  845. pci_write_config_dword(dev, 0x5C, (f_high << 16) | f_low | 0x100);
  846. for(adjust = 0; adjust < 8; adjust++) {
  847. if (hpt37x_calibrate_dpll(dev))
  848. break;
  849. /* See if it'll settle at a fractionally different clock */
  850. if (adjust & 1)
  851. f_low -= adjust >> 1;
  852. else
  853. f_high += adjust >> 1;
  854. pci_write_config_dword(dev, 0x5C, (f_high << 16) | f_low | 0x100);
  855. }
  856. if (adjust == 8) {
  857. printk(KERN_ERR "pata_hpt37x: DPLL did not stabilize!\n");
  858. return -ENODEV;
  859. }
  860. if (dpll == 3)
  861. private_data = (void *)hpt37x_timings_66;
  862. else
  863. private_data = (void *)hpt37x_timings_50;
  864. printk(KERN_INFO "pata_hpt37x: bus clock %dMHz, using %dMHz DPLL.\n",
  865. MHz[clock_slot], MHz[dpll]);
  866. } else {
  867. private_data = (void *)chip_table->clocks[clock_slot];
  868. /*
  869. * Perform a final fixup. Note that we will have used the
  870. * DPLL on the HPT372 which means we don't have to worry
  871. * about lack of UDMA133 support on lower clocks
  872. */
  873. if (clock_slot < 2 && ppi[0] == &info_hpt370)
  874. ppi[0] = &info_hpt370_33;
  875. if (clock_slot < 2 && ppi[0] == &info_hpt370a)
  876. ppi[0] = &info_hpt370a_33;
  877. printk(KERN_INFO "pata_hpt37x: %s using %dMHz bus clock.\n",
  878. chip_table->name, MHz[clock_slot]);
  879. }
  880. /* Now kick off ATA set up */
  881. return ata_pci_sff_init_one(dev, ppi, &hpt37x_sht, private_data);
  882. }
  883. static const struct pci_device_id hpt37x[] = {
  884. { PCI_VDEVICE(TTI, PCI_DEVICE_ID_TTI_HPT366), },
  885. { PCI_VDEVICE(TTI, PCI_DEVICE_ID_TTI_HPT371), },
  886. { PCI_VDEVICE(TTI, PCI_DEVICE_ID_TTI_HPT372), },
  887. { PCI_VDEVICE(TTI, PCI_DEVICE_ID_TTI_HPT374), },
  888. { PCI_VDEVICE(TTI, PCI_DEVICE_ID_TTI_HPT302), },
  889. { },
  890. };
  891. static struct pci_driver hpt37x_pci_driver = {
  892. .name = DRV_NAME,
  893. .id_table = hpt37x,
  894. .probe = hpt37x_init_one,
  895. .remove = ata_pci_remove_one
  896. };
  897. static int __init hpt37x_init(void)
  898. {
  899. return pci_register_driver(&hpt37x_pci_driver);
  900. }
  901. static void __exit hpt37x_exit(void)
  902. {
  903. pci_unregister_driver(&hpt37x_pci_driver);
  904. }
  905. MODULE_AUTHOR("Alan Cox");
  906. MODULE_DESCRIPTION("low-level driver for the Highpoint HPT37x/30x");
  907. MODULE_LICENSE("GPL");
  908. MODULE_DEVICE_TABLE(pci, hpt37x);
  909. MODULE_VERSION(DRV_VERSION);
  910. module_init(hpt37x_init);
  911. module_exit(hpt37x_exit);