pata_hpt37x.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079
  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/init.h>
  21. #include <linux/blkdev.h>
  22. #include <linux/delay.h>
  23. #include <scsi/scsi_host.h>
  24. #include <linux/libata.h>
  25. #define DRV_NAME "pata_hpt37x"
  26. #define DRV_VERSION "0.6.23"
  27. struct hpt_clock {
  28. u8 xfer_speed;
  29. u32 timing;
  30. };
  31. struct hpt_chip {
  32. const char *name;
  33. unsigned int base;
  34. struct hpt_clock const *clocks[4];
  35. };
  36. /* key for bus clock timings
  37. * bit
  38. * 0:3 data_high_time. Inactive time of DIOW_/DIOR_ for PIO and MW DMA.
  39. * cycles = value + 1
  40. * 4:8 data_low_time. Active time of DIOW_/DIOR_ for PIO and MW DMA.
  41. * cycles = value + 1
  42. * 9:12 cmd_high_time. Inactive time of DIOW_/DIOR_ during task file
  43. * register access.
  44. * 13:17 cmd_low_time. Active time of DIOW_/DIOR_ during task file
  45. * register access.
  46. * 18:20 udma_cycle_time. Clock cycles for UDMA xfer.
  47. * 21 CLK frequency for UDMA: 0=ATA clock, 1=dual ATA clock.
  48. * 22:24 pre_high_time. Time to initialize 1st cycle for PIO and MW DMA xfer.
  49. * 25:27 cmd_pre_high_time. Time to initialize 1st PIO cycle for task file
  50. * register access.
  51. * 28 UDMA enable.
  52. * 29 DMA enable.
  53. * 30 PIO_MST enable. If set, the chip is in bus master mode during
  54. * PIO xfer.
  55. * 31 FIFO enable. Only for PIO.
  56. */
  57. static struct hpt_clock hpt37x_timings_33[] = {
  58. { XFER_UDMA_6, 0x12446231 }, /* 0x12646231 ?? */
  59. { XFER_UDMA_5, 0x12446231 },
  60. { XFER_UDMA_4, 0x12446231 },
  61. { XFER_UDMA_3, 0x126c6231 },
  62. { XFER_UDMA_2, 0x12486231 },
  63. { XFER_UDMA_1, 0x124c6233 },
  64. { XFER_UDMA_0, 0x12506297 },
  65. { XFER_MW_DMA_2, 0x22406c31 },
  66. { XFER_MW_DMA_1, 0x22406c33 },
  67. { XFER_MW_DMA_0, 0x22406c97 },
  68. { XFER_PIO_4, 0x06414e31 },
  69. { XFER_PIO_3, 0x06414e42 },
  70. { XFER_PIO_2, 0x06414e53 },
  71. { XFER_PIO_1, 0x06814e93 },
  72. { XFER_PIO_0, 0x06814ea7 }
  73. };
  74. static struct hpt_clock hpt37x_timings_50[] = {
  75. { XFER_UDMA_6, 0x12848242 },
  76. { XFER_UDMA_5, 0x12848242 },
  77. { XFER_UDMA_4, 0x12ac8242 },
  78. { XFER_UDMA_3, 0x128c8242 },
  79. { XFER_UDMA_2, 0x120c8242 },
  80. { XFER_UDMA_1, 0x12148254 },
  81. { XFER_UDMA_0, 0x121882ea },
  82. { XFER_MW_DMA_2, 0x22808242 },
  83. { XFER_MW_DMA_1, 0x22808254 },
  84. { XFER_MW_DMA_0, 0x228082ea },
  85. { XFER_PIO_4, 0x0a81f442 },
  86. { XFER_PIO_3, 0x0a81f443 },
  87. { XFER_PIO_2, 0x0a81f454 },
  88. { XFER_PIO_1, 0x0ac1f465 },
  89. { XFER_PIO_0, 0x0ac1f48a }
  90. };
  91. static struct hpt_clock hpt37x_timings_66[] = {
  92. { XFER_UDMA_6, 0x1c869c62 },
  93. { XFER_UDMA_5, 0x1cae9c62 }, /* 0x1c8a9c62 */
  94. { XFER_UDMA_4, 0x1c8a9c62 },
  95. { XFER_UDMA_3, 0x1c8e9c62 },
  96. { XFER_UDMA_2, 0x1c929c62 },
  97. { XFER_UDMA_1, 0x1c9a9c62 },
  98. { XFER_UDMA_0, 0x1c829c62 },
  99. { XFER_MW_DMA_2, 0x2c829c62 },
  100. { XFER_MW_DMA_1, 0x2c829c66 },
  101. { XFER_MW_DMA_0, 0x2c829d2e },
  102. { XFER_PIO_4, 0x0c829c62 },
  103. { XFER_PIO_3, 0x0c829c84 },
  104. { XFER_PIO_2, 0x0c829ca6 },
  105. { XFER_PIO_1, 0x0d029d26 },
  106. { XFER_PIO_0, 0x0d029d5e }
  107. };
  108. static const struct hpt_chip hpt370 = {
  109. "HPT370",
  110. 48,
  111. {
  112. hpt37x_timings_33,
  113. NULL,
  114. NULL,
  115. NULL
  116. }
  117. };
  118. static const struct hpt_chip hpt370a = {
  119. "HPT370A",
  120. 48,
  121. {
  122. hpt37x_timings_33,
  123. NULL,
  124. hpt37x_timings_50,
  125. NULL
  126. }
  127. };
  128. static const struct hpt_chip hpt372 = {
  129. "HPT372",
  130. 55,
  131. {
  132. hpt37x_timings_33,
  133. NULL,
  134. hpt37x_timings_50,
  135. hpt37x_timings_66
  136. }
  137. };
  138. static const struct hpt_chip hpt302 = {
  139. "HPT302",
  140. 66,
  141. {
  142. hpt37x_timings_33,
  143. NULL,
  144. hpt37x_timings_50,
  145. hpt37x_timings_66
  146. }
  147. };
  148. static const struct hpt_chip hpt371 = {
  149. "HPT371",
  150. 66,
  151. {
  152. hpt37x_timings_33,
  153. NULL,
  154. hpt37x_timings_50,
  155. hpt37x_timings_66
  156. }
  157. };
  158. static const struct hpt_chip hpt372a = {
  159. "HPT372A",
  160. 66,
  161. {
  162. hpt37x_timings_33,
  163. NULL,
  164. hpt37x_timings_50,
  165. hpt37x_timings_66
  166. }
  167. };
  168. static const struct hpt_chip hpt374 = {
  169. "HPT374",
  170. 48,
  171. {
  172. hpt37x_timings_33,
  173. NULL,
  174. NULL,
  175. NULL
  176. }
  177. };
  178. /**
  179. * hpt37x_find_mode - reset the hpt37x bus
  180. * @ap: ATA port
  181. * @speed: transfer mode
  182. *
  183. * Return the 32bit register programming information for this channel
  184. * that matches the speed provided.
  185. */
  186. static u32 hpt37x_find_mode(struct ata_port *ap, int speed)
  187. {
  188. struct hpt_clock *clocks = ap->host->private_data;
  189. while (clocks->xfer_speed) {
  190. if (clocks->xfer_speed == speed)
  191. return clocks->timing;
  192. clocks++;
  193. }
  194. BUG();
  195. return 0xffffffffU; /* silence compiler warning */
  196. }
  197. static int hpt_dma_blacklisted(const struct ata_device *dev, char *modestr,
  198. const char * const list[])
  199. {
  200. unsigned char model_num[ATA_ID_PROD_LEN + 1];
  201. int i = 0;
  202. ata_id_c_string(dev->id, model_num, ATA_ID_PROD, sizeof(model_num));
  203. while (list[i] != NULL) {
  204. if (!strcmp(list[i], model_num)) {
  205. pr_warn("%s is not supported for %s\n",
  206. modestr, list[i]);
  207. return 1;
  208. }
  209. i++;
  210. }
  211. return 0;
  212. }
  213. static const char * const bad_ata33[] = {
  214. "Maxtor 92720U8", "Maxtor 92040U6", "Maxtor 91360U4", "Maxtor 91020U3",
  215. "Maxtor 90845U3", "Maxtor 90650U2",
  216. "Maxtor 91360D8", "Maxtor 91190D7", "Maxtor 91020D6", "Maxtor 90845D5",
  217. "Maxtor 90680D4", "Maxtor 90510D3", "Maxtor 90340D2",
  218. "Maxtor 91152D8", "Maxtor 91008D7", "Maxtor 90845D6", "Maxtor 90840D6",
  219. "Maxtor 90720D5", "Maxtor 90648D5", "Maxtor 90576D4",
  220. "Maxtor 90510D4",
  221. "Maxtor 90432D3", "Maxtor 90288D2", "Maxtor 90256D2",
  222. "Maxtor 91000D8", "Maxtor 90910D8", "Maxtor 90875D7", "Maxtor 90840D7",
  223. "Maxtor 90750D6", "Maxtor 90625D5", "Maxtor 90500D4",
  224. "Maxtor 91728D8", "Maxtor 91512D7", "Maxtor 91303D6", "Maxtor 91080D5",
  225. "Maxtor 90845D4", "Maxtor 90680D4", "Maxtor 90648D3", "Maxtor 90432D2",
  226. NULL
  227. };
  228. static const char * const bad_ata100_5[] = {
  229. "IBM-DTLA-307075",
  230. "IBM-DTLA-307060",
  231. "IBM-DTLA-307045",
  232. "IBM-DTLA-307030",
  233. "IBM-DTLA-307020",
  234. "IBM-DTLA-307015",
  235. "IBM-DTLA-305040",
  236. "IBM-DTLA-305030",
  237. "IBM-DTLA-305020",
  238. "IC35L010AVER07-0",
  239. "IC35L020AVER07-0",
  240. "IC35L030AVER07-0",
  241. "IC35L040AVER07-0",
  242. "IC35L060AVER07-0",
  243. "WDC AC310200R",
  244. NULL
  245. };
  246. /**
  247. * hpt370_filter - mode selection filter
  248. * @adev: ATA device
  249. *
  250. * Block UDMA on devices that cause trouble with this controller.
  251. */
  252. static unsigned long hpt370_filter(struct ata_device *adev, unsigned long mask)
  253. {
  254. if (adev->class == ATA_DEV_ATA) {
  255. if (hpt_dma_blacklisted(adev, "UDMA", bad_ata33))
  256. mask &= ~ATA_MASK_UDMA;
  257. if (hpt_dma_blacklisted(adev, "UDMA100", bad_ata100_5))
  258. mask &= ~(0xE0 << ATA_SHIFT_UDMA);
  259. }
  260. return mask;
  261. }
  262. /**
  263. * hpt370a_filter - mode selection filter
  264. * @adev: ATA device
  265. *
  266. * Block UDMA on devices that cause trouble with this controller.
  267. */
  268. static unsigned long hpt370a_filter(struct ata_device *adev, unsigned long mask)
  269. {
  270. if (adev->class == ATA_DEV_ATA) {
  271. if (hpt_dma_blacklisted(adev, "UDMA100", bad_ata100_5))
  272. mask &= ~(0xE0 << ATA_SHIFT_UDMA);
  273. }
  274. return mask;
  275. }
  276. /**
  277. * hpt372_filter - mode selection filter
  278. * @adev: ATA device
  279. * @mask: mode mask
  280. *
  281. * The Marvell bridge chips used on the HighPoint SATA cards do not seem
  282. * to support the UltraDMA modes 1, 2, and 3 as well as any MWDMA modes...
  283. */
  284. static unsigned long hpt372_filter(struct ata_device *adev, unsigned long mask)
  285. {
  286. if (ata_id_is_sata(adev->id))
  287. mask &= ~((0xE << ATA_SHIFT_UDMA) | ATA_MASK_MWDMA);
  288. return mask;
  289. }
  290. /**
  291. * hpt37x_cable_detect - Detect the cable type
  292. * @ap: ATA port to detect on
  293. *
  294. * Return the cable type attached to this port
  295. */
  296. static int hpt37x_cable_detect(struct ata_port *ap)
  297. {
  298. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  299. u8 scr2, ata66;
  300. pci_read_config_byte(pdev, 0x5B, &scr2);
  301. pci_write_config_byte(pdev, 0x5B, scr2 & ~0x01);
  302. udelay(10); /* debounce */
  303. /* Cable register now active */
  304. pci_read_config_byte(pdev, 0x5A, &ata66);
  305. /* Restore state */
  306. pci_write_config_byte(pdev, 0x5B, scr2);
  307. if (ata66 & (2 >> ap->port_no))
  308. return ATA_CBL_PATA40;
  309. else
  310. return ATA_CBL_PATA80;
  311. }
  312. /**
  313. * hpt374_fn1_cable_detect - Detect the cable type
  314. * @ap: ATA port to detect on
  315. *
  316. * Return the cable type attached to this port
  317. */
  318. static int hpt374_fn1_cable_detect(struct ata_port *ap)
  319. {
  320. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  321. unsigned int mcrbase = 0x50 + 4 * ap->port_no;
  322. u16 mcr3;
  323. u8 ata66;
  324. /* Do the extra channel work */
  325. pci_read_config_word(pdev, mcrbase + 2, &mcr3);
  326. /* Set bit 15 of 0x52 to enable TCBLID as input */
  327. pci_write_config_word(pdev, mcrbase + 2, mcr3 | 0x8000);
  328. pci_read_config_byte(pdev, 0x5A, &ata66);
  329. /* Reset TCBLID/FCBLID to output */
  330. pci_write_config_word(pdev, mcrbase + 2, mcr3);
  331. if (ata66 & (2 >> ap->port_no))
  332. return ATA_CBL_PATA40;
  333. else
  334. return ATA_CBL_PATA80;
  335. }
  336. /**
  337. * hpt37x_pre_reset - reset the hpt37x bus
  338. * @link: ATA link to reset
  339. * @deadline: deadline jiffies for the operation
  340. *
  341. * Perform the initial reset handling for the HPT37x.
  342. */
  343. static int hpt37x_pre_reset(struct ata_link *link, unsigned long deadline)
  344. {
  345. struct ata_port *ap = link->ap;
  346. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  347. static const struct pci_bits hpt37x_enable_bits[] = {
  348. { 0x50, 1, 0x04, 0x04 },
  349. { 0x54, 1, 0x04, 0x04 }
  350. };
  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. return ata_sff_prereset(link, deadline);
  357. }
  358. static void hpt370_set_mode(struct ata_port *ap, struct ata_device *adev,
  359. u8 mode)
  360. {
  361. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  362. u32 addr1, addr2;
  363. u32 reg, timing, mask;
  364. u8 fast;
  365. addr1 = 0x40 + 4 * (adev->devno + 2 * ap->port_no);
  366. addr2 = 0x51 + 4 * ap->port_no;
  367. /* Fast interrupt prediction disable, hold off interrupt disable */
  368. pci_read_config_byte(pdev, addr2, &fast);
  369. fast &= ~0x02;
  370. fast |= 0x01;
  371. pci_write_config_byte(pdev, addr2, fast);
  372. /* Determine timing mask and find matching mode entry */
  373. if (mode < XFER_MW_DMA_0)
  374. mask = 0xcfc3ffff;
  375. else if (mode < XFER_UDMA_0)
  376. mask = 0x31c001ff;
  377. else
  378. mask = 0x303c0000;
  379. timing = hpt37x_find_mode(ap, mode);
  380. pci_read_config_dword(pdev, addr1, &reg);
  381. reg = (reg & ~mask) | (timing & mask);
  382. pci_write_config_dword(pdev, addr1, reg);
  383. }
  384. /**
  385. * hpt370_set_piomode - PIO setup
  386. * @ap: ATA interface
  387. * @adev: device on the interface
  388. *
  389. * Perform PIO mode setup.
  390. */
  391. static void hpt370_set_piomode(struct ata_port *ap, struct ata_device *adev)
  392. {
  393. hpt370_set_mode(ap, adev, adev->pio_mode);
  394. }
  395. /**
  396. * hpt370_set_dmamode - DMA timing setup
  397. * @ap: ATA interface
  398. * @adev: Device being configured
  399. *
  400. * Set up the channel for MWDMA or UDMA modes.
  401. */
  402. static void hpt370_set_dmamode(struct ata_port *ap, struct ata_device *adev)
  403. {
  404. hpt370_set_mode(ap, adev, adev->dma_mode);
  405. }
  406. /**
  407. * hpt370_bmdma_end - DMA engine stop
  408. * @qc: ATA command
  409. *
  410. * Work around the HPT370 DMA engine.
  411. */
  412. static void hpt370_bmdma_stop(struct ata_queued_cmd *qc)
  413. {
  414. struct ata_port *ap = qc->ap;
  415. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  416. void __iomem *bmdma = ap->ioaddr.bmdma_addr;
  417. u8 dma_stat = ioread8(bmdma + ATA_DMA_STATUS);
  418. u8 dma_cmd;
  419. if (dma_stat & ATA_DMA_ACTIVE) {
  420. udelay(20);
  421. dma_stat = ioread8(bmdma + ATA_DMA_STATUS);
  422. }
  423. if (dma_stat & ATA_DMA_ACTIVE) {
  424. /* Clear the engine */
  425. pci_write_config_byte(pdev, 0x50 + 4 * ap->port_no, 0x37);
  426. udelay(10);
  427. /* Stop DMA */
  428. dma_cmd = ioread8(bmdma + ATA_DMA_CMD);
  429. iowrite8(dma_cmd & ~ATA_DMA_START, bmdma + ATA_DMA_CMD);
  430. /* Clear Error */
  431. dma_stat = ioread8(bmdma + ATA_DMA_STATUS);
  432. iowrite8(dma_stat | ATA_DMA_INTR | ATA_DMA_ERR,
  433. bmdma + ATA_DMA_STATUS);
  434. /* Clear the engine */
  435. pci_write_config_byte(pdev, 0x50 + 4 * ap->port_no, 0x37);
  436. udelay(10);
  437. }
  438. ata_bmdma_stop(qc);
  439. }
  440. static void hpt372_set_mode(struct ata_port *ap, struct ata_device *adev,
  441. u8 mode)
  442. {
  443. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  444. u32 addr1, addr2;
  445. u32 reg, timing, mask;
  446. u8 fast;
  447. addr1 = 0x40 + 4 * (adev->devno + 2 * ap->port_no);
  448. addr2 = 0x51 + 4 * ap->port_no;
  449. /* Fast interrupt prediction disable, hold off interrupt disable */
  450. pci_read_config_byte(pdev, addr2, &fast);
  451. fast &= ~0x07;
  452. pci_write_config_byte(pdev, addr2, fast);
  453. /* Determine timing mask and find matching mode entry */
  454. if (mode < XFER_MW_DMA_0)
  455. mask = 0xcfc3ffff;
  456. else if (mode < XFER_UDMA_0)
  457. mask = 0x31c001ff;
  458. else
  459. mask = 0x303c0000;
  460. timing = hpt37x_find_mode(ap, mode);
  461. pci_read_config_dword(pdev, addr1, &reg);
  462. reg = (reg & ~mask) | (timing & mask);
  463. pci_write_config_dword(pdev, addr1, reg);
  464. }
  465. /**
  466. * hpt372_set_piomode - PIO setup
  467. * @ap: ATA interface
  468. * @adev: device on the interface
  469. *
  470. * Perform PIO mode setup.
  471. */
  472. static void hpt372_set_piomode(struct ata_port *ap, struct ata_device *adev)
  473. {
  474. hpt372_set_mode(ap, adev, adev->pio_mode);
  475. }
  476. /**
  477. * hpt372_set_dmamode - DMA timing setup
  478. * @ap: ATA interface
  479. * @adev: Device being configured
  480. *
  481. * Set up the channel for MWDMA or UDMA modes.
  482. */
  483. static void hpt372_set_dmamode(struct ata_port *ap, struct ata_device *adev)
  484. {
  485. hpt372_set_mode(ap, adev, adev->dma_mode);
  486. }
  487. /**
  488. * hpt37x_bmdma_end - DMA engine stop
  489. * @qc: ATA command
  490. *
  491. * Clean up after the HPT372 and later DMA engine
  492. */
  493. static void hpt37x_bmdma_stop(struct ata_queued_cmd *qc)
  494. {
  495. struct ata_port *ap = qc->ap;
  496. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  497. int mscreg = 0x50 + 4 * ap->port_no;
  498. u8 bwsr_stat, msc_stat;
  499. pci_read_config_byte(pdev, 0x6A, &bwsr_stat);
  500. pci_read_config_byte(pdev, mscreg, &msc_stat);
  501. if (bwsr_stat & (1 << ap->port_no))
  502. pci_write_config_byte(pdev, mscreg, msc_stat | 0x30);
  503. ata_bmdma_stop(qc);
  504. }
  505. static struct scsi_host_template hpt37x_sht = {
  506. ATA_BMDMA_SHT(DRV_NAME),
  507. };
  508. /*
  509. * Configuration for HPT370
  510. */
  511. static struct ata_port_operations hpt370_port_ops = {
  512. .inherits = &ata_bmdma_port_ops,
  513. .bmdma_stop = hpt370_bmdma_stop,
  514. .mode_filter = hpt370_filter,
  515. .cable_detect = hpt37x_cable_detect,
  516. .set_piomode = hpt370_set_piomode,
  517. .set_dmamode = hpt370_set_dmamode,
  518. .prereset = hpt37x_pre_reset,
  519. };
  520. /*
  521. * Configuration for HPT370A. Close to 370 but less filters
  522. */
  523. static struct ata_port_operations hpt370a_port_ops = {
  524. .inherits = &hpt370_port_ops,
  525. .mode_filter = hpt370a_filter,
  526. };
  527. /*
  528. * Configuration for HPT371 and HPT302. Slightly different PIO and DMA
  529. * mode setting functionality.
  530. */
  531. static struct ata_port_operations hpt302_port_ops = {
  532. .inherits = &ata_bmdma_port_ops,
  533. .bmdma_stop = hpt37x_bmdma_stop,
  534. .cable_detect = hpt37x_cable_detect,
  535. .set_piomode = hpt372_set_piomode,
  536. .set_dmamode = hpt372_set_dmamode,
  537. .prereset = hpt37x_pre_reset,
  538. };
  539. /*
  540. * Configuration for HPT372. Mode setting works like 371 and 302
  541. * but we have a mode filter.
  542. */
  543. static struct ata_port_operations hpt372_port_ops = {
  544. .inherits = &hpt302_port_ops,
  545. .mode_filter = hpt372_filter,
  546. };
  547. /*
  548. * Configuration for HPT374. Mode setting and filtering works like 372
  549. * but we have a different cable detection procedure for function 1.
  550. */
  551. static struct ata_port_operations hpt374_fn1_port_ops = {
  552. .inherits = &hpt372_port_ops,
  553. .cable_detect = hpt374_fn1_cable_detect,
  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 cable_detect 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. switch (dev->device) {
  735. case PCI_DEVICE_ID_TTI_HPT366:
  736. /* May be a later chip in disguise. Check */
  737. /* Older chips are in the HPT366 driver. Ignore them */
  738. if (rev < 3)
  739. return -ENODEV;
  740. /* N series chips have their own driver. Ignore */
  741. if (rev == 6)
  742. return -ENODEV;
  743. switch (rev) {
  744. case 3:
  745. ppi[0] = &info_hpt370;
  746. chip_table = &hpt370;
  747. prefer_dpll = 0;
  748. break;
  749. case 4:
  750. ppi[0] = &info_hpt370a;
  751. chip_table = &hpt370a;
  752. prefer_dpll = 0;
  753. break;
  754. case 5:
  755. ppi[0] = &info_hpt372;
  756. chip_table = &hpt372;
  757. break;
  758. default:
  759. pr_err("Unknown HPT366 subtype, please report (%d)\n",
  760. rev);
  761. return -ENODEV;
  762. }
  763. break;
  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 but the BIOS
  786. * (or us for non x86) must mark it absent
  787. */
  788. pci_read_config_byte(dev, 0x50, &mcr1);
  789. mcr1 &= ~0x04;
  790. pci_write_config_byte(dev, 0x50, mcr1);
  791. break;
  792. case PCI_DEVICE_ID_TTI_HPT374:
  793. chip_table = &hpt374;
  794. if (!(PCI_FUNC(dev->devfn) & 1))
  795. *ppi = &info_hpt374_fn0;
  796. else
  797. *ppi = &info_hpt374_fn1;
  798. break;
  799. default:
  800. pr_err("PCI table is bogus, please report (%d)\n", dev->device);
  801. return -ENODEV;
  802. }
  803. /* Ok so this is a chip we support */
  804. pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE, (L1_CACHE_BYTES / 4));
  805. pci_write_config_byte(dev, PCI_LATENCY_TIMER, 0x78);
  806. pci_write_config_byte(dev, PCI_MIN_GNT, 0x08);
  807. pci_write_config_byte(dev, PCI_MAX_LAT, 0x08);
  808. pci_read_config_byte(dev, 0x5A, &irqmask);
  809. irqmask &= ~0x10;
  810. pci_write_config_byte(dev, 0x5a, irqmask);
  811. /*
  812. * default to pci clock. make sure MA15/16 are set to output
  813. * to prevent drives having problems with 40-pin cables. Needed
  814. * for some drives such as IBM-DTLA which will not enter ready
  815. * state on reset when PDIAG is a input.
  816. */
  817. pci_write_config_byte(dev, 0x5b, 0x23);
  818. /*
  819. * HighPoint does this for HPT372A.
  820. * NOTE: This register is only writeable via I/O space.
  821. */
  822. if (chip_table == &hpt372a)
  823. outb(0x0e, iobase + 0x9c);
  824. /*
  825. * Some devices do not let this value be accessed via PCI space
  826. * according to the old driver. In addition we must use the value
  827. * from FN 0 on the HPT374.
  828. */
  829. if (chip_table == &hpt374) {
  830. freq = hpt374_read_freq(dev);
  831. if (freq == 0)
  832. return -ENODEV;
  833. } else
  834. freq = inl(iobase + 0x90);
  835. if ((freq >> 12) != 0xABCDE) {
  836. int i;
  837. u8 sr;
  838. u32 total = 0;
  839. pr_warn("BIOS has not set timing clocks\n");
  840. /* This is the process the HPT371 BIOS is reported to use */
  841. for (i = 0; i < 128; i++) {
  842. pci_read_config_byte(dev, 0x78, &sr);
  843. total += sr & 0x1FF;
  844. udelay(15);
  845. }
  846. freq = total / 128;
  847. }
  848. freq &= 0x1FF;
  849. /*
  850. * Turn the frequency check into a band and then find a timing
  851. * table to match it.
  852. */
  853. clock_slot = hpt37x_clock_slot(freq, chip_table->base);
  854. if (chip_table->clocks[clock_slot] == NULL || prefer_dpll) {
  855. /*
  856. * We need to try PLL mode instead
  857. *
  858. * For non UDMA133 capable devices we should
  859. * use a 50MHz DPLL by choice
  860. */
  861. unsigned int f_low, f_high;
  862. int dpll, adjust;
  863. /* Compute DPLL */
  864. dpll = (ppi[0]->udma_mask & 0xC0) ? 3 : 2;
  865. f_low = (MHz[clock_slot] * 48) / MHz[dpll];
  866. f_high = f_low + 2;
  867. if (clock_slot > 1)
  868. f_high += 2;
  869. /* Select the DPLL clock. */
  870. pci_write_config_byte(dev, 0x5b, 0x21);
  871. pci_write_config_dword(dev, 0x5C,
  872. (f_high << 16) | f_low | 0x100);
  873. for (adjust = 0; adjust < 8; adjust++) {
  874. if (hpt37x_calibrate_dpll(dev))
  875. break;
  876. /*
  877. * See if it'll settle at a fractionally
  878. * different clock
  879. */
  880. if (adjust & 1)
  881. f_low -= adjust >> 1;
  882. else
  883. f_high += adjust >> 1;
  884. pci_write_config_dword(dev, 0x5C,
  885. (f_high << 16) | f_low | 0x100);
  886. }
  887. if (adjust == 8) {
  888. pr_err("DPLL did not stabilize!\n");
  889. return -ENODEV;
  890. }
  891. if (dpll == 3)
  892. private_data = (void *)hpt37x_timings_66;
  893. else
  894. private_data = (void *)hpt37x_timings_50;
  895. pr_info("bus clock %dMHz, using %dMHz DPLL\n",
  896. MHz[clock_slot], MHz[dpll]);
  897. } else {
  898. private_data = (void *)chip_table->clocks[clock_slot];
  899. /*
  900. * Perform a final fixup. Note that we will have used the
  901. * DPLL on the HPT372 which means we don't have to worry
  902. * about lack of UDMA133 support on lower clocks
  903. */
  904. if (clock_slot < 2 && ppi[0] == &info_hpt370)
  905. ppi[0] = &info_hpt370_33;
  906. if (clock_slot < 2 && ppi[0] == &info_hpt370a)
  907. ppi[0] = &info_hpt370a_33;
  908. pr_info("%s using %dMHz bus clock\n",
  909. chip_table->name, MHz[clock_slot]);
  910. }
  911. /* Now kick off ATA set up */
  912. return ata_pci_bmdma_init_one(dev, ppi, &hpt37x_sht, private_data, 0);
  913. }
  914. static const struct pci_device_id hpt37x[] = {
  915. { PCI_VDEVICE(TTI, PCI_DEVICE_ID_TTI_HPT366), },
  916. { PCI_VDEVICE(TTI, PCI_DEVICE_ID_TTI_HPT371), },
  917. { PCI_VDEVICE(TTI, PCI_DEVICE_ID_TTI_HPT372), },
  918. { PCI_VDEVICE(TTI, PCI_DEVICE_ID_TTI_HPT374), },
  919. { PCI_VDEVICE(TTI, PCI_DEVICE_ID_TTI_HPT302), },
  920. { },
  921. };
  922. static struct pci_driver hpt37x_pci_driver = {
  923. .name = DRV_NAME,
  924. .id_table = hpt37x,
  925. .probe = hpt37x_init_one,
  926. .remove = ata_pci_remove_one
  927. };
  928. static int __init hpt37x_init(void)
  929. {
  930. return pci_register_driver(&hpt37x_pci_driver);
  931. }
  932. static void __exit hpt37x_exit(void)
  933. {
  934. pci_unregister_driver(&hpt37x_pci_driver);
  935. }
  936. MODULE_AUTHOR("Alan Cox");
  937. MODULE_DESCRIPTION("low-level driver for the Highpoint HPT37x/30x");
  938. MODULE_LICENSE("GPL");
  939. MODULE_DEVICE_TABLE(pci, hpt37x);
  940. MODULE_VERSION(DRV_VERSION);
  941. module_init(hpt37x_init);
  942. module_exit(hpt37x_exit);