pata_hpt37x.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066
  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-2010 MontaVista Software, Inc.
  12. *
  13. * TODO
  14. * Look into engine reset on timeout errors. Should not be required.
  15. */
  16. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/pci.h>
  20. #include <linux/blkdev.h>
  21. #include <linux/delay.h>
  22. #include <scsi/scsi_host.h>
  23. #include <linux/libata.h>
  24. #define DRV_NAME "pata_hpt37x"
  25. #define DRV_VERSION "0.6.23"
  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 = 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. pr_warn("%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 * const bad_ata33[] = {
  213. "Maxtor 92720U8", "Maxtor 92040U6", "Maxtor 91360U4", "Maxtor 91020U3",
  214. "Maxtor 90845U3", "Maxtor 90650U2",
  215. "Maxtor 91360D8", "Maxtor 91190D7", "Maxtor 91020D6", "Maxtor 90845D5",
  216. "Maxtor 90680D4", "Maxtor 90510D3", "Maxtor 90340D2",
  217. "Maxtor 91152D8", "Maxtor 91008D7", "Maxtor 90845D6", "Maxtor 90840D6",
  218. "Maxtor 90720D5", "Maxtor 90648D5", "Maxtor 90576D4",
  219. "Maxtor 90510D4",
  220. "Maxtor 90432D3", "Maxtor 90288D2", "Maxtor 90256D2",
  221. "Maxtor 91000D8", "Maxtor 90910D8", "Maxtor 90875D7", "Maxtor 90840D7",
  222. "Maxtor 90750D6", "Maxtor 90625D5", "Maxtor 90500D4",
  223. "Maxtor 91728D8", "Maxtor 91512D7", "Maxtor 91303D6", "Maxtor 91080D5",
  224. "Maxtor 90845D4", "Maxtor 90680D4", "Maxtor 90648D3", "Maxtor 90432D2",
  225. NULL
  226. };
  227. static const char * const bad_ata100_5[] = {
  228. "IBM-DTLA-307075",
  229. "IBM-DTLA-307060",
  230. "IBM-DTLA-307045",
  231. "IBM-DTLA-307030",
  232. "IBM-DTLA-307020",
  233. "IBM-DTLA-307015",
  234. "IBM-DTLA-305040",
  235. "IBM-DTLA-305030",
  236. "IBM-DTLA-305020",
  237. "IC35L010AVER07-0",
  238. "IC35L020AVER07-0",
  239. "IC35L030AVER07-0",
  240. "IC35L040AVER07-0",
  241. "IC35L060AVER07-0",
  242. "WDC AC310200R",
  243. NULL
  244. };
  245. /**
  246. * hpt370_filter - mode selection filter
  247. * @adev: ATA device
  248. *
  249. * Block UDMA on devices that cause trouble with this controller.
  250. */
  251. static unsigned long hpt370_filter(struct ata_device *adev, unsigned long mask)
  252. {
  253. if (adev->class == ATA_DEV_ATA) {
  254. if (hpt_dma_blacklisted(adev, "UDMA", bad_ata33))
  255. mask &= ~ATA_MASK_UDMA;
  256. if (hpt_dma_blacklisted(adev, "UDMA100", bad_ata100_5))
  257. mask &= ~(0xE0 << ATA_SHIFT_UDMA);
  258. }
  259. return mask;
  260. }
  261. /**
  262. * hpt370a_filter - mode selection filter
  263. * @adev: ATA device
  264. *
  265. * Block UDMA on devices that cause trouble with this controller.
  266. */
  267. static unsigned long hpt370a_filter(struct ata_device *adev, unsigned long 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 long hpt372_filter(struct ata_device *adev, unsigned long 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. if (!pci_test_config_bits(pdev, &hpt37x_enable_bits[ap->port_no]))
  351. return -ENOENT;
  352. /* Reset the state machine */
  353. pci_write_config_byte(pdev, 0x50 + 4 * ap->port_no, 0x37);
  354. udelay(100);
  355. return ata_sff_prereset(link, deadline);
  356. }
  357. static void hpt370_set_mode(struct ata_port *ap, struct ata_device *adev,
  358. u8 mode)
  359. {
  360. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  361. u32 addr1, addr2;
  362. u32 reg, timing, mask;
  363. u8 fast;
  364. addr1 = 0x40 + 4 * (adev->devno + 2 * ap->port_no);
  365. addr2 = 0x51 + 4 * ap->port_no;
  366. /* Fast interrupt prediction disable, hold off interrupt disable */
  367. pci_read_config_byte(pdev, addr2, &fast);
  368. fast &= ~0x02;
  369. fast |= 0x01;
  370. pci_write_config_byte(pdev, addr2, fast);
  371. /* Determine timing mask and find matching mode entry */
  372. if (mode < XFER_MW_DMA_0)
  373. mask = 0xcfc3ffff;
  374. else if (mode < XFER_UDMA_0)
  375. mask = 0x31c001ff;
  376. else
  377. mask = 0x303c0000;
  378. timing = hpt37x_find_mode(ap, mode);
  379. pci_read_config_dword(pdev, addr1, &reg);
  380. reg = (reg & ~mask) | (timing & mask);
  381. pci_write_config_dword(pdev, addr1, reg);
  382. }
  383. /**
  384. * hpt370_set_piomode - PIO setup
  385. * @ap: ATA interface
  386. * @adev: device on the interface
  387. *
  388. * Perform PIO mode setup.
  389. */
  390. static void hpt370_set_piomode(struct ata_port *ap, struct ata_device *adev)
  391. {
  392. hpt370_set_mode(ap, adev, adev->pio_mode);
  393. }
  394. /**
  395. * hpt370_set_dmamode - DMA timing setup
  396. * @ap: ATA interface
  397. * @adev: Device being configured
  398. *
  399. * Set up the channel for MWDMA or UDMA modes.
  400. */
  401. static void hpt370_set_dmamode(struct ata_port *ap, struct ata_device *adev)
  402. {
  403. hpt370_set_mode(ap, adev, adev->dma_mode);
  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. void __iomem *bmdma = ap->ioaddr.bmdma_addr;
  416. u8 dma_stat = ioread8(bmdma + ATA_DMA_STATUS);
  417. u8 dma_cmd;
  418. if (dma_stat & ATA_DMA_ACTIVE) {
  419. udelay(20);
  420. dma_stat = ioread8(bmdma + ATA_DMA_STATUS);
  421. }
  422. if (dma_stat & ATA_DMA_ACTIVE) {
  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 + ATA_DMA_CMD);
  428. iowrite8(dma_cmd & ~ATA_DMA_START, bmdma + ATA_DMA_CMD);
  429. /* Clear Error */
  430. dma_stat = ioread8(bmdma + ATA_DMA_STATUS);
  431. iowrite8(dma_stat | ATA_DMA_INTR | ATA_DMA_ERR,
  432. bmdma + ATA_DMA_STATUS);
  433. /* Clear the engine */
  434. pci_write_config_byte(pdev, 0x50 + 4 * ap->port_no, 0x37);
  435. udelay(10);
  436. }
  437. ata_bmdma_stop(qc);
  438. }
  439. static void hpt372_set_mode(struct ata_port *ap, struct ata_device *adev,
  440. u8 mode)
  441. {
  442. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  443. u32 addr1, addr2;
  444. u32 reg, timing, mask;
  445. u8 fast;
  446. addr1 = 0x40 + 4 * (adev->devno + 2 * ap->port_no);
  447. addr2 = 0x51 + 4 * ap->port_no;
  448. /* Fast interrupt prediction disable, hold off interrupt disable */
  449. pci_read_config_byte(pdev, addr2, &fast);
  450. fast &= ~0x07;
  451. pci_write_config_byte(pdev, addr2, fast);
  452. /* Determine timing mask and find matching mode entry */
  453. if (mode < XFER_MW_DMA_0)
  454. mask = 0xcfc3ffff;
  455. else if (mode < XFER_UDMA_0)
  456. mask = 0x31c001ff;
  457. else
  458. mask = 0x303c0000;
  459. timing = hpt37x_find_mode(ap, mode);
  460. pci_read_config_dword(pdev, addr1, &reg);
  461. reg = (reg & ~mask) | (timing & mask);
  462. pci_write_config_dword(pdev, addr1, reg);
  463. }
  464. /**
  465. * hpt372_set_piomode - PIO setup
  466. * @ap: ATA interface
  467. * @adev: device on the interface
  468. *
  469. * Perform PIO mode setup.
  470. */
  471. static void hpt372_set_piomode(struct ata_port *ap, struct ata_device *adev)
  472. {
  473. hpt372_set_mode(ap, adev, adev->pio_mode);
  474. }
  475. /**
  476. * hpt372_set_dmamode - DMA timing setup
  477. * @ap: ATA interface
  478. * @adev: Device being configured
  479. *
  480. * Set up the channel for MWDMA or UDMA modes.
  481. */
  482. static void hpt372_set_dmamode(struct ata_port *ap, struct ata_device *adev)
  483. {
  484. hpt372_set_mode(ap, adev, adev->dma_mode);
  485. }
  486. /**
  487. * hpt37x_bmdma_end - DMA engine stop
  488. * @qc: ATA command
  489. *
  490. * Clean up after the HPT372 and later DMA engine
  491. */
  492. static void hpt37x_bmdma_stop(struct ata_queued_cmd *qc)
  493. {
  494. struct ata_port *ap = qc->ap;
  495. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  496. int mscreg = 0x50 + 4 * ap->port_no;
  497. u8 bwsr_stat, msc_stat;
  498. pci_read_config_byte(pdev, 0x6A, &bwsr_stat);
  499. pci_read_config_byte(pdev, mscreg, &msc_stat);
  500. if (bwsr_stat & (1 << ap->port_no))
  501. pci_write_config_byte(pdev, mscreg, msc_stat | 0x30);
  502. ata_bmdma_stop(qc);
  503. }
  504. static struct scsi_host_template hpt37x_sht = {
  505. ATA_BMDMA_SHT(DRV_NAME),
  506. };
  507. /*
  508. * Configuration for HPT370
  509. */
  510. static struct ata_port_operations hpt370_port_ops = {
  511. .inherits = &ata_bmdma_port_ops,
  512. .bmdma_stop = hpt370_bmdma_stop,
  513. .mode_filter = hpt370_filter,
  514. .cable_detect = hpt37x_cable_detect,
  515. .set_piomode = hpt370_set_piomode,
  516. .set_dmamode = hpt370_set_dmamode,
  517. .prereset = hpt37x_pre_reset,
  518. };
  519. /*
  520. * Configuration for HPT370A. Close to 370 but less filters
  521. */
  522. static struct ata_port_operations hpt370a_port_ops = {
  523. .inherits = &hpt370_port_ops,
  524. .mode_filter = hpt370a_filter,
  525. };
  526. /*
  527. * Configuration for HPT371 and HPT302. Slightly different PIO and DMA
  528. * mode setting functionality.
  529. */
  530. static struct ata_port_operations hpt302_port_ops = {
  531. .inherits = &ata_bmdma_port_ops,
  532. .bmdma_stop = hpt37x_bmdma_stop,
  533. .cable_detect = hpt37x_cable_detect,
  534. .set_piomode = hpt372_set_piomode,
  535. .set_dmamode = hpt372_set_dmamode,
  536. .prereset = hpt37x_pre_reset,
  537. };
  538. /*
  539. * Configuration for HPT372. Mode setting works like 371 and 302
  540. * but we have a mode filter.
  541. */
  542. static struct ata_port_operations hpt372_port_ops = {
  543. .inherits = &hpt302_port_ops,
  544. .mode_filter = hpt372_filter,
  545. };
  546. /*
  547. * Configuration for HPT374. Mode setting and filtering works like 372
  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. .cable_detect = hpt374_fn1_cable_detect,
  553. };
  554. /**
  555. * hpt37x_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 = 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 = {
  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. /* HPT370 - UDMA66 */
  672. static const struct ata_port_info info_hpt370_33 = {
  673. .flags = ATA_FLAG_SLAVE_POSS,
  674. .pio_mask = ATA_PIO4,
  675. .mwdma_mask = ATA_MWDMA2,
  676. .udma_mask = ATA_UDMA4,
  677. .port_ops = &hpt370_port_ops
  678. };
  679. /* HPT370A - UDMA66 */
  680. static const struct ata_port_info info_hpt370a_33 = {
  681. .flags = ATA_FLAG_SLAVE_POSS,
  682. .pio_mask = ATA_PIO4,
  683. .mwdma_mask = ATA_MWDMA2,
  684. .udma_mask = ATA_UDMA4,
  685. .port_ops = &hpt370a_port_ops
  686. };
  687. /* HPT372 - UDMA133 */
  688. static const struct ata_port_info info_hpt372 = {
  689. .flags = ATA_FLAG_SLAVE_POSS,
  690. .pio_mask = ATA_PIO4,
  691. .mwdma_mask = ATA_MWDMA2,
  692. .udma_mask = ATA_UDMA6,
  693. .port_ops = &hpt372_port_ops
  694. };
  695. /* HPT371, 302 - UDMA133 */
  696. static const struct ata_port_info info_hpt302 = {
  697. .flags = ATA_FLAG_SLAVE_POSS,
  698. .pio_mask = ATA_PIO4,
  699. .mwdma_mask = ATA_MWDMA2,
  700. .udma_mask = ATA_UDMA6,
  701. .port_ops = &hpt302_port_ops
  702. };
  703. /* HPT374 - UDMA100, function 1 uses different cable_detect method */
  704. static const struct ata_port_info info_hpt374_fn0 = {
  705. .flags = ATA_FLAG_SLAVE_POSS,
  706. .pio_mask = ATA_PIO4,
  707. .mwdma_mask = ATA_MWDMA2,
  708. .udma_mask = ATA_UDMA5,
  709. .port_ops = &hpt372_port_ops
  710. };
  711. static const struct ata_port_info info_hpt374_fn1 = {
  712. .flags = ATA_FLAG_SLAVE_POSS,
  713. .pio_mask = ATA_PIO4,
  714. .mwdma_mask = ATA_MWDMA2,
  715. .udma_mask = ATA_UDMA5,
  716. .port_ops = &hpt374_fn1_port_ops
  717. };
  718. static const int MHz[4] = { 33, 40, 50, 66 };
  719. void *private_data = NULL;
  720. const struct ata_port_info *ppi[] = { NULL, NULL };
  721. u8 rev = dev->revision;
  722. u8 irqmask;
  723. u8 mcr1;
  724. u32 freq;
  725. int prefer_dpll = 1;
  726. unsigned long iobase = pci_resource_start(dev, 4);
  727. const struct hpt_chip *chip_table;
  728. int clock_slot;
  729. int rc;
  730. rc = pcim_enable_device(dev);
  731. if (rc)
  732. return rc;
  733. switch (dev->device) {
  734. case PCI_DEVICE_ID_TTI_HPT366:
  735. /* May be a later chip in disguise. Check */
  736. /* Older chips are in the HPT366 driver. Ignore them */
  737. if (rev < 3)
  738. return -ENODEV;
  739. /* N series chips have their own driver. Ignore */
  740. if (rev == 6)
  741. return -ENODEV;
  742. switch (rev) {
  743. case 3:
  744. ppi[0] = &info_hpt370;
  745. chip_table = &hpt370;
  746. prefer_dpll = 0;
  747. break;
  748. case 4:
  749. ppi[0] = &info_hpt370a;
  750. chip_table = &hpt370a;
  751. prefer_dpll = 0;
  752. break;
  753. case 5:
  754. ppi[0] = &info_hpt372;
  755. chip_table = &hpt372;
  756. break;
  757. default:
  758. pr_err("Unknown HPT366 subtype, please report (%d)\n",
  759. rev);
  760. return -ENODEV;
  761. }
  762. break;
  763. case PCI_DEVICE_ID_TTI_HPT372:
  764. /* 372N if rev >= 2 */
  765. if (rev >= 2)
  766. return -ENODEV;
  767. ppi[0] = &info_hpt372;
  768. chip_table = &hpt372a;
  769. break;
  770. case PCI_DEVICE_ID_TTI_HPT302:
  771. /* 302N if rev > 1 */
  772. if (rev > 1)
  773. return -ENODEV;
  774. ppi[0] = &info_hpt302;
  775. /* Check this */
  776. chip_table = &hpt302;
  777. break;
  778. case PCI_DEVICE_ID_TTI_HPT371:
  779. if (rev > 1)
  780. return -ENODEV;
  781. ppi[0] = &info_hpt302;
  782. chip_table = &hpt371;
  783. /*
  784. * Single channel device, master is not present but the BIOS
  785. * (or us for non x86) must mark it absent
  786. */
  787. pci_read_config_byte(dev, 0x50, &mcr1);
  788. mcr1 &= ~0x04;
  789. pci_write_config_byte(dev, 0x50, mcr1);
  790. break;
  791. case PCI_DEVICE_ID_TTI_HPT374:
  792. chip_table = &hpt374;
  793. if (!(PCI_FUNC(dev->devfn) & 1))
  794. *ppi = &info_hpt374_fn0;
  795. else
  796. *ppi = &info_hpt374_fn1;
  797. break;
  798. default:
  799. pr_err("PCI table is bogus, please report (%d)\n", dev->device);
  800. return -ENODEV;
  801. }
  802. /* Ok so this is a chip we support */
  803. pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE, (L1_CACHE_BYTES / 4));
  804. pci_write_config_byte(dev, PCI_LATENCY_TIMER, 0x78);
  805. pci_write_config_byte(dev, PCI_MIN_GNT, 0x08);
  806. pci_write_config_byte(dev, PCI_MAX_LAT, 0x08);
  807. pci_read_config_byte(dev, 0x5A, &irqmask);
  808. irqmask &= ~0x10;
  809. pci_write_config_byte(dev, 0x5a, irqmask);
  810. /*
  811. * default to pci clock. make sure MA15/16 are set to output
  812. * to prevent drives having problems with 40-pin cables. Needed
  813. * for some drives such as IBM-DTLA which will not enter ready
  814. * state on reset when PDIAG is a input.
  815. */
  816. pci_write_config_byte(dev, 0x5b, 0x23);
  817. /*
  818. * HighPoint does this for HPT372A.
  819. * NOTE: This register is only writeable via I/O space.
  820. */
  821. if (chip_table == &hpt372a)
  822. outb(0x0e, iobase + 0x9c);
  823. /*
  824. * Some devices do not let this value be accessed via PCI space
  825. * according to the old driver. In addition we must use the value
  826. * from FN 0 on the HPT374.
  827. */
  828. if (chip_table == &hpt374) {
  829. freq = hpt374_read_freq(dev);
  830. if (freq == 0)
  831. return -ENODEV;
  832. } else
  833. freq = inl(iobase + 0x90);
  834. if ((freq >> 12) != 0xABCDE) {
  835. int i;
  836. u8 sr;
  837. u32 total = 0;
  838. pr_warn("BIOS has not set timing clocks\n");
  839. /* This is the process the HPT371 BIOS is reported to use */
  840. for (i = 0; i < 128; i++) {
  841. pci_read_config_byte(dev, 0x78, &sr);
  842. total += sr & 0x1FF;
  843. udelay(15);
  844. }
  845. freq = total / 128;
  846. }
  847. freq &= 0x1FF;
  848. /*
  849. * Turn the frequency check into a band and then find a timing
  850. * table to match it.
  851. */
  852. clock_slot = hpt37x_clock_slot(freq, chip_table->base);
  853. if (chip_table->clocks[clock_slot] == NULL || prefer_dpll) {
  854. /*
  855. * We need to try PLL mode instead
  856. *
  857. * For non UDMA133 capable devices we should
  858. * use a 50MHz DPLL by choice
  859. */
  860. unsigned int f_low, f_high;
  861. int dpll, adjust;
  862. /* Compute DPLL */
  863. dpll = (ppi[0]->udma_mask & 0xC0) ? 3 : 2;
  864. f_low = (MHz[clock_slot] * 48) / MHz[dpll];
  865. f_high = f_low + 2;
  866. if (clock_slot > 1)
  867. f_high += 2;
  868. /* Select the DPLL clock. */
  869. pci_write_config_byte(dev, 0x5b, 0x21);
  870. pci_write_config_dword(dev, 0x5C,
  871. (f_high << 16) | f_low | 0x100);
  872. for (adjust = 0; adjust < 8; adjust++) {
  873. if (hpt37x_calibrate_dpll(dev))
  874. break;
  875. /*
  876. * See if it'll settle at a fractionally
  877. * different clock
  878. */
  879. if (adjust & 1)
  880. f_low -= adjust >> 1;
  881. else
  882. f_high += adjust >> 1;
  883. pci_write_config_dword(dev, 0x5C,
  884. (f_high << 16) | f_low | 0x100);
  885. }
  886. if (adjust == 8) {
  887. pr_err("DPLL did not stabilize!\n");
  888. return -ENODEV;
  889. }
  890. if (dpll == 3)
  891. private_data = (void *)hpt37x_timings_66;
  892. else
  893. private_data = (void *)hpt37x_timings_50;
  894. pr_info("bus clock %dMHz, using %dMHz DPLL\n",
  895. MHz[clock_slot], MHz[dpll]);
  896. } else {
  897. private_data = (void *)chip_table->clocks[clock_slot];
  898. /*
  899. * Perform a final fixup. Note that we will have used the
  900. * DPLL on the HPT372 which means we don't have to worry
  901. * about lack of UDMA133 support on lower clocks
  902. */
  903. if (clock_slot < 2 && ppi[0] == &info_hpt370)
  904. ppi[0] = &info_hpt370_33;
  905. if (clock_slot < 2 && ppi[0] == &info_hpt370a)
  906. ppi[0] = &info_hpt370a_33;
  907. pr_info("%s using %dMHz bus clock\n",
  908. chip_table->name, MHz[clock_slot]);
  909. }
  910. /* Now kick off ATA set up */
  911. return ata_pci_bmdma_init_one(dev, ppi, &hpt37x_sht, private_data, 0);
  912. }
  913. static const struct pci_device_id hpt37x[] = {
  914. { PCI_VDEVICE(TTI, PCI_DEVICE_ID_TTI_HPT366), },
  915. { PCI_VDEVICE(TTI, PCI_DEVICE_ID_TTI_HPT371), },
  916. { PCI_VDEVICE(TTI, PCI_DEVICE_ID_TTI_HPT372), },
  917. { PCI_VDEVICE(TTI, PCI_DEVICE_ID_TTI_HPT374), },
  918. { PCI_VDEVICE(TTI, PCI_DEVICE_ID_TTI_HPT302), },
  919. { },
  920. };
  921. static struct pci_driver hpt37x_pci_driver = {
  922. .name = DRV_NAME,
  923. .id_table = hpt37x,
  924. .probe = hpt37x_init_one,
  925. .remove = ata_pci_remove_one
  926. };
  927. module_pci_driver(hpt37x_pci_driver);
  928. MODULE_AUTHOR("Alan Cox");
  929. MODULE_DESCRIPTION("low-level driver for the Highpoint HPT37x/30x");
  930. MODULE_LICENSE("GPL");
  931. MODULE_DEVICE_TABLE(pci, hpt37x);
  932. MODULE_VERSION(DRV_VERSION);