pata_hpt37x.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059
  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-2007 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.11"
  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_start - DMA engine begin
  392. * @qc: ATA command
  393. *
  394. * The 370 and 370A want us to reset the DMA engine each time we
  395. * use it. The 372 and later are fine.
  396. */
  397. static void hpt370_bmdma_start(struct ata_queued_cmd *qc)
  398. {
  399. struct ata_port *ap = qc->ap;
  400. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  401. pci_write_config_byte(pdev, 0x50 + 4 * ap->port_no, 0x37);
  402. udelay(10);
  403. ata_bmdma_start(qc);
  404. }
  405. /**
  406. * hpt370_bmdma_end - DMA engine stop
  407. * @qc: ATA command
  408. *
  409. * Work around the HPT370 DMA engine.
  410. */
  411. static void hpt370_bmdma_stop(struct ata_queued_cmd *qc)
  412. {
  413. struct ata_port *ap = qc->ap;
  414. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  415. u8 dma_stat = ioread8(ap->ioaddr.bmdma_addr + 2);
  416. u8 dma_cmd;
  417. void __iomem *bmdma = ap->ioaddr.bmdma_addr;
  418. if (dma_stat & 0x01) {
  419. udelay(20);
  420. dma_stat = ioread8(bmdma + 2);
  421. }
  422. if (dma_stat & 0x01) {
  423. /* Clear the engine */
  424. pci_write_config_byte(pdev, 0x50 + 4 * ap->port_no, 0x37);
  425. udelay(10);
  426. /* Stop DMA */
  427. dma_cmd = ioread8(bmdma );
  428. iowrite8(dma_cmd & 0xFE, bmdma);
  429. /* Clear Error */
  430. dma_stat = ioread8(bmdma + 2);
  431. iowrite8(dma_stat | 0x06 , bmdma + 2);
  432. /* Clear the engine */
  433. pci_write_config_byte(pdev, 0x50 + 4 * ap->port_no, 0x37);
  434. udelay(10);
  435. }
  436. ata_bmdma_stop(qc);
  437. }
  438. /**
  439. * hpt372_set_piomode - PIO setup
  440. * @ap: ATA interface
  441. * @adev: device on the interface
  442. *
  443. * Perform PIO mode setup.
  444. */
  445. static void hpt372_set_piomode(struct ata_port *ap, struct ata_device *adev)
  446. {
  447. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  448. u32 addr1, addr2;
  449. u32 reg;
  450. u32 mode;
  451. u8 fast;
  452. addr1 = 0x40 + 4 * (adev->devno + 2 * ap->port_no);
  453. addr2 = 0x51 + 4 * ap->port_no;
  454. /* Fast interrupt prediction disable, hold off interrupt disable */
  455. pci_read_config_byte(pdev, addr2, &fast);
  456. fast &= ~0x07;
  457. pci_write_config_byte(pdev, addr2, fast);
  458. pci_read_config_dword(pdev, addr1, &reg);
  459. mode = hpt37x_find_mode(ap, adev->pio_mode);
  460. printk("Find mode for %d reports %X\n", adev->pio_mode, mode);
  461. mode &= ~0x80000000; /* No FIFO in PIO */
  462. mode &= ~0x30070000; /* Leave config bits alone */
  463. reg &= 0x30070000; /* Strip timing bits */
  464. pci_write_config_dword(pdev, addr1, reg | mode);
  465. }
  466. /**
  467. * hpt372_set_dmamode - DMA timing setup
  468. * @ap: ATA interface
  469. * @adev: Device being configured
  470. *
  471. * Set up the channel for MWDMA or UDMA modes. Much the same as with
  472. * PIO, load the mode number and then set MWDMA or UDMA flag.
  473. */
  474. static void hpt372_set_dmamode(struct ata_port *ap, struct ata_device *adev)
  475. {
  476. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  477. u32 addr1, addr2;
  478. u32 reg;
  479. u32 mode;
  480. u8 fast;
  481. addr1 = 0x40 + 4 * (adev->devno + 2 * ap->port_no);
  482. addr2 = 0x51 + 4 * ap->port_no;
  483. /* Fast interrupt prediction disable, hold off interrupt disable */
  484. pci_read_config_byte(pdev, addr2, &fast);
  485. fast &= ~0x07;
  486. pci_write_config_byte(pdev, addr2, fast);
  487. pci_read_config_dword(pdev, addr1, &reg);
  488. mode = hpt37x_find_mode(ap, adev->dma_mode);
  489. printk("Find mode for DMA %d reports %X\n", adev->dma_mode, mode);
  490. mode &= ~0xC0000000; /* Leave config bits alone */
  491. mode |= 0x80000000; /* FIFO in MWDMA or UDMA */
  492. reg &= 0xC0000000; /* Strip timing bits */
  493. pci_write_config_dword(pdev, addr1, reg | mode);
  494. }
  495. /**
  496. * hpt37x_bmdma_end - DMA engine stop
  497. * @qc: ATA command
  498. *
  499. * Clean up after the HPT372 and later DMA engine
  500. */
  501. static void hpt37x_bmdma_stop(struct ata_queued_cmd *qc)
  502. {
  503. struct ata_port *ap = qc->ap;
  504. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  505. int mscreg = 0x50 + 4 * ap->port_no;
  506. u8 bwsr_stat, msc_stat;
  507. pci_read_config_byte(pdev, 0x6A, &bwsr_stat);
  508. pci_read_config_byte(pdev, mscreg, &msc_stat);
  509. if (bwsr_stat & (1 << ap->port_no))
  510. pci_write_config_byte(pdev, mscreg, msc_stat | 0x30);
  511. ata_bmdma_stop(qc);
  512. }
  513. static struct scsi_host_template hpt37x_sht = {
  514. ATA_BMDMA_SHT(DRV_NAME),
  515. };
  516. /*
  517. * Configuration for HPT370
  518. */
  519. static struct ata_port_operations hpt370_port_ops = {
  520. .inherits = &ata_bmdma_port_ops,
  521. .bmdma_start = hpt370_bmdma_start,
  522. .bmdma_stop = hpt370_bmdma_stop,
  523. .mode_filter = hpt370_filter,
  524. .set_piomode = hpt370_set_piomode,
  525. .set_dmamode = hpt370_set_dmamode,
  526. .prereset = hpt37x_pre_reset,
  527. };
  528. /*
  529. * Configuration for HPT370A. Close to 370 but less filters
  530. */
  531. static struct ata_port_operations hpt370a_port_ops = {
  532. .inherits = &hpt370_port_ops,
  533. .mode_filter = hpt370a_filter,
  534. };
  535. /*
  536. * Configuration for HPT372, HPT371, HPT302. Slightly different PIO
  537. * and DMA mode setting functionality.
  538. */
  539. static struct ata_port_operations hpt372_port_ops = {
  540. .inherits = &ata_bmdma_port_ops,
  541. .bmdma_stop = hpt37x_bmdma_stop,
  542. .set_piomode = hpt372_set_piomode,
  543. .set_dmamode = hpt372_set_dmamode,
  544. .prereset = hpt37x_pre_reset,
  545. };
  546. /*
  547. * Configuration for HPT374. Mode setting works like 372 and friends
  548. * but we have a different cable detection procedure for function 1.
  549. */
  550. static struct ata_port_operations hpt374_fn1_port_ops = {
  551. .inherits = &hpt372_port_ops,
  552. .prereset = hpt374_fn1_pre_reset,
  553. };
  554. /**
  555. * htp37x_clock_slot - Turn timing to PC clock entry
  556. * @freq: Reported frequency timing
  557. * @base: Base timing
  558. *
  559. * Turn the timing data intoa clock slot (0 for 33, 1 for 40, 2 for 50
  560. * and 3 for 66Mhz)
  561. */
  562. static int hpt37x_clock_slot(unsigned int freq, unsigned int base)
  563. {
  564. unsigned int f = (base * freq) / 192; /* Mhz */
  565. if (f < 40)
  566. return 0; /* 33Mhz slot */
  567. if (f < 45)
  568. return 1; /* 40Mhz slot */
  569. if (f < 55)
  570. return 2; /* 50Mhz slot */
  571. return 3; /* 60Mhz slot */
  572. }
  573. /**
  574. * hpt37x_calibrate_dpll - Calibrate the DPLL loop
  575. * @dev: PCI device
  576. *
  577. * Perform a calibration cycle on the HPT37x DPLL. Returns 1 if this
  578. * succeeds
  579. */
  580. static int hpt37x_calibrate_dpll(struct pci_dev *dev)
  581. {
  582. u8 reg5b;
  583. u32 reg5c;
  584. int tries;
  585. for(tries = 0; tries < 0x5000; tries++) {
  586. udelay(50);
  587. pci_read_config_byte(dev, 0x5b, &reg5b);
  588. if (reg5b & 0x80) {
  589. /* See if it stays set */
  590. for(tries = 0; tries < 0x1000; tries ++) {
  591. pci_read_config_byte(dev, 0x5b, &reg5b);
  592. /* Failed ? */
  593. if ((reg5b & 0x80) == 0)
  594. return 0;
  595. }
  596. /* Turn off tuning, we have the DPLL set */
  597. pci_read_config_dword(dev, 0x5c, &reg5c);
  598. pci_write_config_dword(dev, 0x5c, reg5c & ~ 0x100);
  599. return 1;
  600. }
  601. }
  602. /* Never went stable */
  603. return 0;
  604. }
  605. static u32 hpt374_read_freq(struct pci_dev *pdev)
  606. {
  607. u32 freq;
  608. unsigned long io_base = pci_resource_start(pdev, 4);
  609. if (PCI_FUNC(pdev->devfn) & 1) {
  610. struct pci_dev *pdev_0;
  611. pdev_0 = pci_get_slot(pdev->bus, pdev->devfn - 1);
  612. /* Someone hot plugged the controller on us ? */
  613. if (pdev_0 == NULL)
  614. return 0;
  615. io_base = pci_resource_start(pdev_0, 4);
  616. freq = inl(io_base + 0x90);
  617. pci_dev_put(pdev_0);
  618. } else
  619. freq = inl(io_base + 0x90);
  620. return freq;
  621. }
  622. /**
  623. * hpt37x_init_one - Initialise an HPT37X/302
  624. * @dev: PCI device
  625. * @id: Entry in match table
  626. *
  627. * Initialise an HPT37x device. There are some interesting complications
  628. * here. Firstly the chip may report 366 and be one of several variants.
  629. * Secondly all the timings depend on the clock for the chip which we must
  630. * detect and look up
  631. *
  632. * This is the known chip mappings. It may be missing a couple of later
  633. * releases.
  634. *
  635. * Chip version PCI Rev Notes
  636. * HPT366 4 (HPT366) 0 Other driver
  637. * HPT366 4 (HPT366) 1 Other driver
  638. * HPT368 4 (HPT366) 2 Other driver
  639. * HPT370 4 (HPT366) 3 UDMA100
  640. * HPT370A 4 (HPT366) 4 UDMA100
  641. * HPT372 4 (HPT366) 5 UDMA133 (1)
  642. * HPT372N 4 (HPT366) 6 Other driver
  643. * HPT372A 5 (HPT372) 1 UDMA133 (1)
  644. * HPT372N 5 (HPT372) 2 Other driver
  645. * HPT302 6 (HPT302) 1 UDMA133
  646. * HPT302N 6 (HPT302) 2 Other driver
  647. * HPT371 7 (HPT371) * UDMA133
  648. * HPT374 8 (HPT374) * UDMA133 4 channel
  649. * HPT372N 9 (HPT372N) * Other driver
  650. *
  651. * (1) UDMA133 support depends on the bus clock
  652. */
  653. static int hpt37x_init_one(struct pci_dev *dev, const struct pci_device_id *id)
  654. {
  655. /* HPT370 - UDMA100 */
  656. static const struct ata_port_info info_hpt370 = {
  657. .flags = ATA_FLAG_SLAVE_POSS,
  658. .pio_mask = 0x1f,
  659. .mwdma_mask = 0x07,
  660. .udma_mask = ATA_UDMA5,
  661. .port_ops = &hpt370_port_ops
  662. };
  663. /* HPT370A - UDMA100 */
  664. static const struct ata_port_info info_hpt370a = {
  665. .flags = ATA_FLAG_SLAVE_POSS,
  666. .pio_mask = 0x1f,
  667. .mwdma_mask = 0x07,
  668. .udma_mask = ATA_UDMA5,
  669. .port_ops = &hpt370a_port_ops
  670. };
  671. /* HPT370 - UDMA100 */
  672. static const struct ata_port_info info_hpt370_33 = {
  673. .flags = ATA_FLAG_SLAVE_POSS,
  674. .pio_mask = 0x1f,
  675. .mwdma_mask = 0x07,
  676. .udma_mask = ATA_UDMA5,
  677. .port_ops = &hpt370_port_ops
  678. };
  679. /* HPT370A - UDMA100 */
  680. static const struct ata_port_info info_hpt370a_33 = {
  681. .flags = ATA_FLAG_SLAVE_POSS,
  682. .pio_mask = 0x1f,
  683. .mwdma_mask = 0x07,
  684. .udma_mask = ATA_UDMA5,
  685. .port_ops = &hpt370a_port_ops
  686. };
  687. /* HPT371, 372 and friends - UDMA133 */
  688. static const struct ata_port_info info_hpt372 = {
  689. .flags = ATA_FLAG_SLAVE_POSS,
  690. .pio_mask = 0x1f,
  691. .mwdma_mask = 0x07,
  692. .udma_mask = ATA_UDMA6,
  693. .port_ops = &hpt372_port_ops
  694. };
  695. /* HPT374 - UDMA100, function 1 uses different prereset method */
  696. static const struct ata_port_info info_hpt374_fn0 = {
  697. .flags = ATA_FLAG_SLAVE_POSS,
  698. .pio_mask = 0x1f,
  699. .mwdma_mask = 0x07,
  700. .udma_mask = ATA_UDMA5,
  701. .port_ops = &hpt372_port_ops
  702. };
  703. static const struct ata_port_info info_hpt374_fn1 = {
  704. .flags = ATA_FLAG_SLAVE_POSS,
  705. .pio_mask = 0x1f,
  706. .mwdma_mask = 0x07,
  707. .udma_mask = ATA_UDMA5,
  708. .port_ops = &hpt374_fn1_port_ops
  709. };
  710. static const int MHz[4] = { 33, 40, 50, 66 };
  711. void *private_data = NULL;
  712. const struct ata_port_info *ppi[] = { NULL, NULL };
  713. u8 irqmask;
  714. u32 class_rev;
  715. u8 mcr1;
  716. u32 freq;
  717. int prefer_dpll = 1;
  718. unsigned long iobase = pci_resource_start(dev, 4);
  719. const struct hpt_chip *chip_table;
  720. int clock_slot;
  721. int rc;
  722. rc = pcim_enable_device(dev);
  723. if (rc)
  724. return rc;
  725. pci_read_config_dword(dev, PCI_CLASS_REVISION, &class_rev);
  726. class_rev &= 0xFF;
  727. if (dev->device == PCI_DEVICE_ID_TTI_HPT366) {
  728. /* May be a later chip in disguise. Check */
  729. /* Older chips are in the HPT366 driver. Ignore them */
  730. if (class_rev < 3)
  731. return -ENODEV;
  732. /* N series chips have their own driver. Ignore */
  733. if (class_rev == 6)
  734. return -ENODEV;
  735. switch(class_rev) {
  736. case 3:
  737. ppi[0] = &info_hpt370;
  738. chip_table = &hpt370;
  739. prefer_dpll = 0;
  740. break;
  741. case 4:
  742. ppi[0] = &info_hpt370a;
  743. chip_table = &hpt370a;
  744. prefer_dpll = 0;
  745. break;
  746. case 5:
  747. ppi[0] = &info_hpt372;
  748. chip_table = &hpt372;
  749. break;
  750. default:
  751. printk(KERN_ERR "pata_hpt37x: Unknown HPT366 subtype please report (%d).\n", class_rev);
  752. return -ENODEV;
  753. }
  754. } else {
  755. switch(dev->device) {
  756. case PCI_DEVICE_ID_TTI_HPT372:
  757. /* 372N if rev >= 2*/
  758. if (class_rev >= 2)
  759. return -ENODEV;
  760. ppi[0] = &info_hpt372;
  761. chip_table = &hpt372a;
  762. break;
  763. case PCI_DEVICE_ID_TTI_HPT302:
  764. /* 302N if rev > 1 */
  765. if (class_rev > 1)
  766. return -ENODEV;
  767. ppi[0] = &info_hpt372;
  768. /* Check this */
  769. chip_table = &hpt302;
  770. break;
  771. case PCI_DEVICE_ID_TTI_HPT371:
  772. if (class_rev > 1)
  773. return -ENODEV;
  774. ppi[0] = &info_hpt372;
  775. chip_table = &hpt371;
  776. /* Single channel device, master is not present
  777. but the BIOS (or us for non x86) must mark it
  778. absent */
  779. pci_read_config_byte(dev, 0x50, &mcr1);
  780. mcr1 &= ~0x04;
  781. pci_write_config_byte(dev, 0x50, mcr1);
  782. break;
  783. case PCI_DEVICE_ID_TTI_HPT374:
  784. chip_table = &hpt374;
  785. if (!(PCI_FUNC(dev->devfn) & 1))
  786. *ppi = &info_hpt374_fn0;
  787. else
  788. *ppi = &info_hpt374_fn1;
  789. break;
  790. default:
  791. printk(KERN_ERR "pata_hpt37x: PCI table is bogus please report (%d).\n", dev->device);
  792. return -ENODEV;
  793. }
  794. }
  795. /* Ok so this is a chip we support */
  796. pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE, (L1_CACHE_BYTES / 4));
  797. pci_write_config_byte(dev, PCI_LATENCY_TIMER, 0x78);
  798. pci_write_config_byte(dev, PCI_MIN_GNT, 0x08);
  799. pci_write_config_byte(dev, PCI_MAX_LAT, 0x08);
  800. pci_read_config_byte(dev, 0x5A, &irqmask);
  801. irqmask &= ~0x10;
  802. pci_write_config_byte(dev, 0x5a, irqmask);
  803. /*
  804. * default to pci clock. make sure MA15/16 are set to output
  805. * to prevent drives having problems with 40-pin cables. Needed
  806. * for some drives such as IBM-DTLA which will not enter ready
  807. * state on reset when PDIAG is a input.
  808. */
  809. pci_write_config_byte(dev, 0x5b, 0x23);
  810. /*
  811. * HighPoint does this for HPT372A.
  812. * NOTE: This register is only writeable via I/O space.
  813. */
  814. if (chip_table == &hpt372a)
  815. outb(0x0e, iobase + 0x9c);
  816. /* Some devices do not let this value be accessed via PCI space
  817. according to the old driver. In addition we must use the value
  818. from FN 0 on the HPT374 */
  819. if (chip_table == &hpt374) {
  820. freq = hpt374_read_freq(dev);
  821. if (freq == 0)
  822. return -ENODEV;
  823. } else
  824. freq = inl(iobase + 0x90);
  825. if ((freq >> 12) != 0xABCDE) {
  826. int i;
  827. u8 sr;
  828. u32 total = 0;
  829. printk(KERN_WARNING "pata_hpt37x: BIOS has not set timing clocks.\n");
  830. /* This is the process the HPT371 BIOS is reported to use */
  831. for(i = 0; i < 128; i++) {
  832. pci_read_config_byte(dev, 0x78, &sr);
  833. total += sr & 0x1FF;
  834. udelay(15);
  835. }
  836. freq = total / 128;
  837. }
  838. freq &= 0x1FF;
  839. /*
  840. * Turn the frequency check into a band and then find a timing
  841. * table to match it.
  842. */
  843. clock_slot = hpt37x_clock_slot(freq, chip_table->base);
  844. if (chip_table->clocks[clock_slot] == NULL || prefer_dpll) {
  845. /*
  846. * We need to try PLL mode instead
  847. *
  848. * For non UDMA133 capable devices we should
  849. * use a 50MHz DPLL by choice
  850. */
  851. unsigned int f_low, f_high;
  852. int dpll, adjust;
  853. /* Compute DPLL */
  854. dpll = (ppi[0]->udma_mask & 0xC0) ? 3 : 2;
  855. f_low = (MHz[clock_slot] * 48) / MHz[dpll];
  856. f_high = f_low + 2;
  857. if (clock_slot > 1)
  858. f_high += 2;
  859. /* Select the DPLL clock. */
  860. pci_write_config_byte(dev, 0x5b, 0x21);
  861. pci_write_config_dword(dev, 0x5C, (f_high << 16) | f_low | 0x100);
  862. for(adjust = 0; adjust < 8; adjust++) {
  863. if (hpt37x_calibrate_dpll(dev))
  864. break;
  865. /* See if it'll settle at a fractionally different clock */
  866. if (adjust & 1)
  867. f_low -= adjust >> 1;
  868. else
  869. f_high += adjust >> 1;
  870. pci_write_config_dword(dev, 0x5C, (f_high << 16) | f_low | 0x100);
  871. }
  872. if (adjust == 8) {
  873. printk(KERN_ERR "pata_hpt37x: DPLL did not stabilize!\n");
  874. return -ENODEV;
  875. }
  876. if (dpll == 3)
  877. private_data = (void *)hpt37x_timings_66;
  878. else
  879. private_data = (void *)hpt37x_timings_50;
  880. printk(KERN_INFO "pata_hpt37x: bus clock %dMHz, using %dMHz DPLL.\n",
  881. MHz[clock_slot], MHz[dpll]);
  882. } else {
  883. private_data = (void *)chip_table->clocks[clock_slot];
  884. /*
  885. * Perform a final fixup. Note that we will have used the
  886. * DPLL on the HPT372 which means we don't have to worry
  887. * about lack of UDMA133 support on lower clocks
  888. */
  889. if (clock_slot < 2 && ppi[0] == &info_hpt370)
  890. ppi[0] = &info_hpt370_33;
  891. if (clock_slot < 2 && ppi[0] == &info_hpt370a)
  892. ppi[0] = &info_hpt370a_33;
  893. printk(KERN_INFO "pata_hpt37x: %s using %dMHz bus clock.\n",
  894. chip_table->name, MHz[clock_slot]);
  895. }
  896. /* Now kick off ATA set up */
  897. return ata_pci_sff_init_one(dev, ppi, &hpt37x_sht, private_data);
  898. }
  899. static const struct pci_device_id hpt37x[] = {
  900. { PCI_VDEVICE(TTI, PCI_DEVICE_ID_TTI_HPT366), },
  901. { PCI_VDEVICE(TTI, PCI_DEVICE_ID_TTI_HPT371), },
  902. { PCI_VDEVICE(TTI, PCI_DEVICE_ID_TTI_HPT372), },
  903. { PCI_VDEVICE(TTI, PCI_DEVICE_ID_TTI_HPT374), },
  904. { PCI_VDEVICE(TTI, PCI_DEVICE_ID_TTI_HPT302), },
  905. { },
  906. };
  907. static struct pci_driver hpt37x_pci_driver = {
  908. .name = DRV_NAME,
  909. .id_table = hpt37x,
  910. .probe = hpt37x_init_one,
  911. .remove = ata_pci_remove_one
  912. };
  913. static int __init hpt37x_init(void)
  914. {
  915. return pci_register_driver(&hpt37x_pci_driver);
  916. }
  917. static void __exit hpt37x_exit(void)
  918. {
  919. pci_unregister_driver(&hpt37x_pci_driver);
  920. }
  921. MODULE_AUTHOR("Alan Cox");
  922. MODULE_DESCRIPTION("low-level driver for the Highpoint HPT37x/30x");
  923. MODULE_LICENSE("GPL");
  924. MODULE_DEVICE_TABLE(pci, hpt37x);
  925. MODULE_VERSION(DRV_VERSION);
  926. module_init(hpt37x_init);
  927. module_exit(hpt37x_exit);