pata_hpt37x.c 26 KB

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