pata_hpt37x.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081
  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. #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.18"
  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. 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 * 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. .prereset = hpt37x_pre_reset,
  554. };
  555. /**
  556. * hpt37x_clock_slot - Turn timing to PC clock entry
  557. * @freq: Reported frequency timing
  558. * @base: Base timing
  559. *
  560. * Turn the timing data intoa clock slot (0 for 33, 1 for 40, 2 for 50
  561. * and 3 for 66Mhz)
  562. */
  563. static int hpt37x_clock_slot(unsigned int freq, unsigned int base)
  564. {
  565. unsigned int f = (base * freq) / 192; /* Mhz */
  566. if (f < 40)
  567. return 0; /* 33Mhz slot */
  568. if (f < 45)
  569. return 1; /* 40Mhz slot */
  570. if (f < 55)
  571. return 2; /* 50Mhz slot */
  572. return 3; /* 60Mhz slot */
  573. }
  574. /**
  575. * hpt37x_calibrate_dpll - Calibrate the DPLL loop
  576. * @dev: PCI device
  577. *
  578. * Perform a calibration cycle on the HPT37x DPLL. Returns 1 if this
  579. * succeeds
  580. */
  581. static int hpt37x_calibrate_dpll(struct pci_dev *dev)
  582. {
  583. u8 reg5b;
  584. u32 reg5c;
  585. int tries;
  586. for (tries = 0; tries < 0x5000; tries++) {
  587. udelay(50);
  588. pci_read_config_byte(dev, 0x5b, &reg5b);
  589. if (reg5b & 0x80) {
  590. /* See if it stays set */
  591. for (tries = 0; tries < 0x1000; tries++) {
  592. pci_read_config_byte(dev, 0x5b, &reg5b);
  593. /* Failed ? */
  594. if ((reg5b & 0x80) == 0)
  595. return 0;
  596. }
  597. /* Turn off tuning, we have the DPLL set */
  598. pci_read_config_dword(dev, 0x5c, &reg5c);
  599. pci_write_config_dword(dev, 0x5c, reg5c & ~0x100);
  600. return 1;
  601. }
  602. }
  603. /* Never went stable */
  604. return 0;
  605. }
  606. static u32 hpt374_read_freq(struct pci_dev *pdev)
  607. {
  608. u32 freq;
  609. unsigned long io_base = pci_resource_start(pdev, 4);
  610. if (PCI_FUNC(pdev->devfn) & 1) {
  611. struct pci_dev *pdev_0;
  612. pdev_0 = pci_get_slot(pdev->bus, pdev->devfn - 1);
  613. /* Someone hot plugged the controller on us ? */
  614. if (pdev_0 == NULL)
  615. return 0;
  616. io_base = pci_resource_start(pdev_0, 4);
  617. freq = inl(io_base + 0x90);
  618. pci_dev_put(pdev_0);
  619. } else
  620. freq = inl(io_base + 0x90);
  621. return freq;
  622. }
  623. /**
  624. * hpt37x_init_one - Initialise an HPT37X/302
  625. * @dev: PCI device
  626. * @id: Entry in match table
  627. *
  628. * Initialise an HPT37x device. There are some interesting complications
  629. * here. Firstly the chip may report 366 and be one of several variants.
  630. * Secondly all the timings depend on the clock for the chip which we must
  631. * detect and look up
  632. *
  633. * This is the known chip mappings. It may be missing a couple of later
  634. * releases.
  635. *
  636. * Chip version PCI Rev Notes
  637. * HPT366 4 (HPT366) 0 Other driver
  638. * HPT366 4 (HPT366) 1 Other driver
  639. * HPT368 4 (HPT366) 2 Other driver
  640. * HPT370 4 (HPT366) 3 UDMA100
  641. * HPT370A 4 (HPT366) 4 UDMA100
  642. * HPT372 4 (HPT366) 5 UDMA133 (1)
  643. * HPT372N 4 (HPT366) 6 Other driver
  644. * HPT372A 5 (HPT372) 1 UDMA133 (1)
  645. * HPT372N 5 (HPT372) 2 Other driver
  646. * HPT302 6 (HPT302) 1 UDMA133
  647. * HPT302N 6 (HPT302) 2 Other driver
  648. * HPT371 7 (HPT371) * UDMA133
  649. * HPT374 8 (HPT374) * UDMA133 4 channel
  650. * HPT372N 9 (HPT372N) * Other driver
  651. *
  652. * (1) UDMA133 support depends on the bus clock
  653. */
  654. static int hpt37x_init_one(struct pci_dev *dev, const struct pci_device_id *id)
  655. {
  656. /* HPT370 - UDMA100 */
  657. static const struct ata_port_info info_hpt370 = {
  658. .flags = ATA_FLAG_SLAVE_POSS,
  659. .pio_mask = ATA_PIO4,
  660. .mwdma_mask = ATA_MWDMA2,
  661. .udma_mask = ATA_UDMA5,
  662. .port_ops = &hpt370_port_ops
  663. };
  664. /* HPT370A - UDMA100 */
  665. static const struct ata_port_info info_hpt370a = {
  666. .flags = ATA_FLAG_SLAVE_POSS,
  667. .pio_mask = ATA_PIO4,
  668. .mwdma_mask = ATA_MWDMA2,
  669. .udma_mask = ATA_UDMA5,
  670. .port_ops = &hpt370a_port_ops
  671. };
  672. /* HPT370 - UDMA66 */
  673. static const struct ata_port_info info_hpt370_33 = {
  674. .flags = ATA_FLAG_SLAVE_POSS,
  675. .pio_mask = ATA_PIO4,
  676. .mwdma_mask = ATA_MWDMA2,
  677. .udma_mask = ATA_UDMA4,
  678. .port_ops = &hpt370_port_ops
  679. };
  680. /* HPT370A - UDMA66 */
  681. static const struct ata_port_info info_hpt370a_33 = {
  682. .flags = ATA_FLAG_SLAVE_POSS,
  683. .pio_mask = ATA_PIO4,
  684. .mwdma_mask = ATA_MWDMA2,
  685. .udma_mask = ATA_UDMA4,
  686. .port_ops = &hpt370a_port_ops
  687. };
  688. /* HPT372 - UDMA133 */
  689. static const struct ata_port_info info_hpt372 = {
  690. .flags = ATA_FLAG_SLAVE_POSS,
  691. .pio_mask = ATA_PIO4,
  692. .mwdma_mask = ATA_MWDMA2,
  693. .udma_mask = ATA_UDMA6,
  694. .port_ops = &hpt372_port_ops
  695. };
  696. /* HPT371, 302 - UDMA133 */
  697. static const struct ata_port_info info_hpt302 = {
  698. .flags = ATA_FLAG_SLAVE_POSS,
  699. .pio_mask = ATA_PIO4,
  700. .mwdma_mask = ATA_MWDMA2,
  701. .udma_mask = ATA_UDMA6,
  702. .port_ops = &hpt302_port_ops
  703. };
  704. /* HPT374 - UDMA100, function 1 uses different prereset method */
  705. static const struct ata_port_info info_hpt374_fn0 = {
  706. .flags = ATA_FLAG_SLAVE_POSS,
  707. .pio_mask = ATA_PIO4,
  708. .mwdma_mask = ATA_MWDMA2,
  709. .udma_mask = ATA_UDMA5,
  710. .port_ops = &hpt372_port_ops
  711. };
  712. static const struct ata_port_info info_hpt374_fn1 = {
  713. .flags = ATA_FLAG_SLAVE_POSS,
  714. .pio_mask = ATA_PIO4,
  715. .mwdma_mask = ATA_MWDMA2,
  716. .udma_mask = ATA_UDMA5,
  717. .port_ops = &hpt374_fn1_port_ops
  718. };
  719. static const int MHz[4] = { 33, 40, 50, 66 };
  720. void *private_data = NULL;
  721. const struct ata_port_info *ppi[] = { NULL, NULL };
  722. u8 rev = dev->revision;
  723. u8 irqmask;
  724. u8 mcr1;
  725. u32 freq;
  726. int prefer_dpll = 1;
  727. unsigned long iobase = pci_resource_start(dev, 4);
  728. const struct hpt_chip *chip_table;
  729. int clock_slot;
  730. int rc;
  731. rc = pcim_enable_device(dev);
  732. if (rc)
  733. return rc;
  734. if (dev->device == 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. printk(KERN_ERR "pata_hpt37x: Unknown HPT366 subtype, "
  759. "please report (%d).\n", rev);
  760. return -ENODEV;
  761. }
  762. } else {
  763. switch (dev->device) {
  764. case PCI_DEVICE_ID_TTI_HPT372:
  765. /* 372N if rev >= 2 */
  766. if (rev >= 2)
  767. return -ENODEV;
  768. ppi[0] = &info_hpt372;
  769. chip_table = &hpt372a;
  770. break;
  771. case PCI_DEVICE_ID_TTI_HPT302:
  772. /* 302N if rev > 1 */
  773. if (rev > 1)
  774. return -ENODEV;
  775. ppi[0] = &info_hpt302;
  776. /* Check this */
  777. chip_table = &hpt302;
  778. break;
  779. case PCI_DEVICE_ID_TTI_HPT371:
  780. if (rev > 1)
  781. return -ENODEV;
  782. ppi[0] = &info_hpt302;
  783. chip_table = &hpt371;
  784. /*
  785. * Single channel device, master is not present
  786. * but the BIOS (or us for non x86) must mark it
  787. * absent
  788. */
  789. pci_read_config_byte(dev, 0x50, &mcr1);
  790. mcr1 &= ~0x04;
  791. pci_write_config_byte(dev, 0x50, mcr1);
  792. break;
  793. case PCI_DEVICE_ID_TTI_HPT374:
  794. chip_table = &hpt374;
  795. if (!(PCI_FUNC(dev->devfn) & 1))
  796. *ppi = &info_hpt374_fn0;
  797. else
  798. *ppi = &info_hpt374_fn1;
  799. break;
  800. default:
  801. printk(KERN_ERR
  802. "pata_hpt37x: PCI table is bogus, please report (%d).\n",
  803. dev->device);
  804. return -ENODEV;
  805. }
  806. }
  807. /* Ok so this is a chip we support */
  808. pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE, (L1_CACHE_BYTES / 4));
  809. pci_write_config_byte(dev, PCI_LATENCY_TIMER, 0x78);
  810. pci_write_config_byte(dev, PCI_MIN_GNT, 0x08);
  811. pci_write_config_byte(dev, PCI_MAX_LAT, 0x08);
  812. pci_read_config_byte(dev, 0x5A, &irqmask);
  813. irqmask &= ~0x10;
  814. pci_write_config_byte(dev, 0x5a, irqmask);
  815. /*
  816. * default to pci clock. make sure MA15/16 are set to output
  817. * to prevent drives having problems with 40-pin cables. Needed
  818. * for some drives such as IBM-DTLA which will not enter ready
  819. * state on reset when PDIAG is a input.
  820. */
  821. pci_write_config_byte(dev, 0x5b, 0x23);
  822. /*
  823. * HighPoint does this for HPT372A.
  824. * NOTE: This register is only writeable via I/O space.
  825. */
  826. if (chip_table == &hpt372a)
  827. outb(0x0e, iobase + 0x9c);
  828. /*
  829. * Some devices do not let this value be accessed via PCI space
  830. * according to the old driver. In addition we must use the value
  831. * from FN 0 on the HPT374.
  832. */
  833. if (chip_table == &hpt374) {
  834. freq = hpt374_read_freq(dev);
  835. if (freq == 0)
  836. return -ENODEV;
  837. } else
  838. freq = inl(iobase + 0x90);
  839. if ((freq >> 12) != 0xABCDE) {
  840. int i;
  841. u8 sr;
  842. u32 total = 0;
  843. printk(KERN_WARNING
  844. "pata_hpt37x: BIOS has not set timing clocks.\n");
  845. /* This is the process the HPT371 BIOS is reported to use */
  846. for (i = 0; i < 128; i++) {
  847. pci_read_config_byte(dev, 0x78, &sr);
  848. total += sr & 0x1FF;
  849. udelay(15);
  850. }
  851. freq = total / 128;
  852. }
  853. freq &= 0x1FF;
  854. /*
  855. * Turn the frequency check into a band and then find a timing
  856. * table to match it.
  857. */
  858. clock_slot = hpt37x_clock_slot(freq, chip_table->base);
  859. if (chip_table->clocks[clock_slot] == NULL || prefer_dpll) {
  860. /*
  861. * We need to try PLL mode instead
  862. *
  863. * For non UDMA133 capable devices we should
  864. * use a 50MHz DPLL by choice
  865. */
  866. unsigned int f_low, f_high;
  867. int dpll, adjust;
  868. /* Compute DPLL */
  869. dpll = (ppi[0]->udma_mask & 0xC0) ? 3 : 2;
  870. f_low = (MHz[clock_slot] * 48) / MHz[dpll];
  871. f_high = f_low + 2;
  872. if (clock_slot > 1)
  873. f_high += 2;
  874. /* Select the DPLL clock. */
  875. pci_write_config_byte(dev, 0x5b, 0x21);
  876. pci_write_config_dword(dev, 0x5C,
  877. (f_high << 16) | f_low | 0x100);
  878. for (adjust = 0; adjust < 8; adjust++) {
  879. if (hpt37x_calibrate_dpll(dev))
  880. break;
  881. /*
  882. * See if it'll settle at a fractionally
  883. * different clock
  884. */
  885. if (adjust & 1)
  886. f_low -= adjust >> 1;
  887. else
  888. f_high += adjust >> 1;
  889. pci_write_config_dword(dev, 0x5C,
  890. (f_high << 16) | f_low | 0x100);
  891. }
  892. if (adjust == 8) {
  893. printk(KERN_ERR "pata_hpt37x: DPLL did not stabilize!\n");
  894. return -ENODEV;
  895. }
  896. if (dpll == 3)
  897. private_data = (void *)hpt37x_timings_66;
  898. else
  899. private_data = (void *)hpt37x_timings_50;
  900. printk(KERN_INFO "pata_hpt37x: bus clock %dMHz, using %dMHz DPLL.\n",
  901. MHz[clock_slot], MHz[dpll]);
  902. } else {
  903. private_data = (void *)chip_table->clocks[clock_slot];
  904. /*
  905. * Perform a final fixup. Note that we will have used the
  906. * DPLL on the HPT372 which means we don't have to worry
  907. * about lack of UDMA133 support on lower clocks
  908. */
  909. if (clock_slot < 2 && ppi[0] == &info_hpt370)
  910. ppi[0] = &info_hpt370_33;
  911. if (clock_slot < 2 && ppi[0] == &info_hpt370a)
  912. ppi[0] = &info_hpt370a_33;
  913. printk(KERN_INFO "pata_hpt37x: %s using %dMHz bus clock.\n",
  914. chip_table->name, MHz[clock_slot]);
  915. }
  916. /* Now kick off ATA set up */
  917. return ata_pci_bmdma_init_one(dev, ppi, &hpt37x_sht, private_data, 0);
  918. }
  919. static const struct pci_device_id hpt37x[] = {
  920. { PCI_VDEVICE(TTI, PCI_DEVICE_ID_TTI_HPT366), },
  921. { PCI_VDEVICE(TTI, PCI_DEVICE_ID_TTI_HPT371), },
  922. { PCI_VDEVICE(TTI, PCI_DEVICE_ID_TTI_HPT372), },
  923. { PCI_VDEVICE(TTI, PCI_DEVICE_ID_TTI_HPT374), },
  924. { PCI_VDEVICE(TTI, PCI_DEVICE_ID_TTI_HPT302), },
  925. { },
  926. };
  927. static struct pci_driver hpt37x_pci_driver = {
  928. .name = DRV_NAME,
  929. .id_table = hpt37x,
  930. .probe = hpt37x_init_one,
  931. .remove = ata_pci_remove_one
  932. };
  933. static int __init hpt37x_init(void)
  934. {
  935. return pci_register_driver(&hpt37x_pci_driver);
  936. }
  937. static void __exit hpt37x_exit(void)
  938. {
  939. pci_unregister_driver(&hpt37x_pci_driver);
  940. }
  941. MODULE_AUTHOR("Alan Cox");
  942. MODULE_DESCRIPTION("low-level driver for the Highpoint HPT37x/30x");
  943. MODULE_LICENSE("GPL");
  944. MODULE_DEVICE_TABLE(pci, hpt37x);
  945. MODULE_VERSION(DRV_VERSION);
  946. module_init(hpt37x_init);
  947. module_exit(hpt37x_exit);