pata_hpt37x.c 25 KB

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