pata_cmd64x.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. /*
  2. * pata_cmd64x.c - CMD64x PATA for new ATA layer
  3. * (C) 2005 Red Hat Inc
  4. * Alan Cox <alan@lxorguk.ukuu.org.uk>
  5. * (C) 2009-2010 Bartlomiej Zolnierkiewicz
  6. * (C) 2012 MontaVista Software, LLC <source@mvista.com>
  7. *
  8. * Based upon
  9. * linux/drivers/ide/pci/cmd64x.c Version 1.30 Sept 10, 2002
  10. *
  11. * cmd64x.c: Enable interrupts at initialization time on Ultra/PCI machines.
  12. * Note, this driver is not used at all on other systems because
  13. * there the "BIOS" has done all of the following already.
  14. * Due to massive hardware bugs, UltraDMA is only supported
  15. * on the 646U2 and not on the 646U.
  16. *
  17. * Copyright (C) 1998 Eddie C. Dost (ecd@skynet.be)
  18. * Copyright (C) 1998 David S. Miller (davem@redhat.com)
  19. *
  20. * Copyright (C) 1999-2002 Andre Hedrick <andre@linux-ide.org>
  21. *
  22. * TODO
  23. * Testing work
  24. */
  25. #include <linux/kernel.h>
  26. #include <linux/module.h>
  27. #include <linux/pci.h>
  28. #include <linux/blkdev.h>
  29. #include <linux/delay.h>
  30. #include <scsi/scsi_host.h>
  31. #include <linux/libata.h>
  32. #define DRV_NAME "pata_cmd64x"
  33. #define DRV_VERSION "0.2.18"
  34. /*
  35. * CMD64x specific registers definition.
  36. */
  37. enum {
  38. CFR = 0x50,
  39. CFR_INTR_CH0 = 0x04,
  40. CNTRL = 0x51,
  41. CNTRL_CH0 = 0x04,
  42. CNTRL_CH1 = 0x08,
  43. CMDTIM = 0x52,
  44. ARTTIM0 = 0x53,
  45. DRWTIM0 = 0x54,
  46. ARTTIM1 = 0x55,
  47. DRWTIM1 = 0x56,
  48. ARTTIM23 = 0x57,
  49. ARTTIM23_DIS_RA2 = 0x04,
  50. ARTTIM23_DIS_RA3 = 0x08,
  51. ARTTIM23_INTR_CH1 = 0x10,
  52. DRWTIM2 = 0x58,
  53. BRST = 0x59,
  54. DRWTIM3 = 0x5b,
  55. BMIDECR0 = 0x70,
  56. MRDMODE = 0x71,
  57. MRDMODE_INTR_CH0 = 0x04,
  58. MRDMODE_INTR_CH1 = 0x08,
  59. BMIDESR0 = 0x72,
  60. UDIDETCR0 = 0x73,
  61. DTPR0 = 0x74,
  62. BMIDECR1 = 0x78,
  63. BMIDECSR = 0x79,
  64. UDIDETCR1 = 0x7B,
  65. DTPR1 = 0x7C
  66. };
  67. static int cmd648_cable_detect(struct ata_port *ap)
  68. {
  69. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  70. u8 r;
  71. /* Check cable detect bits */
  72. pci_read_config_byte(pdev, BMIDECSR, &r);
  73. if (r & (1 << ap->port_no))
  74. return ATA_CBL_PATA80;
  75. return ATA_CBL_PATA40;
  76. }
  77. /**
  78. * cmd64x_set_timing - set PIO and MWDMA timing
  79. * @ap: ATA interface
  80. * @adev: ATA device
  81. * @mode: mode
  82. *
  83. * Called to do the PIO and MWDMA mode setup.
  84. */
  85. static void cmd64x_set_timing(struct ata_port *ap, struct ata_device *adev, u8 mode)
  86. {
  87. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  88. struct ata_timing t;
  89. const unsigned long T = 1000000 / 33;
  90. const u8 setup_data[] = { 0x40, 0x40, 0x40, 0x80, 0x00 };
  91. u8 reg;
  92. /* Port layout is not logical so use a table */
  93. const u8 arttim_port[2][2] = {
  94. { ARTTIM0, ARTTIM1 },
  95. { ARTTIM23, ARTTIM23 }
  96. };
  97. const u8 drwtim_port[2][2] = {
  98. { DRWTIM0, DRWTIM1 },
  99. { DRWTIM2, DRWTIM3 }
  100. };
  101. int arttim = arttim_port[ap->port_no][adev->devno];
  102. int drwtim = drwtim_port[ap->port_no][adev->devno];
  103. /* ata_timing_compute is smart and will produce timings for MWDMA
  104. that don't violate the drives PIO capabilities. */
  105. if (ata_timing_compute(adev, mode, &t, T, 0) < 0) {
  106. printk(KERN_ERR DRV_NAME ": mode computation failed.\n");
  107. return;
  108. }
  109. if (ap->port_no) {
  110. /* Slave has shared address setup */
  111. struct ata_device *pair = ata_dev_pair(adev);
  112. if (pair) {
  113. struct ata_timing tp;
  114. ata_timing_compute(pair, pair->pio_mode, &tp, T, 0);
  115. ata_timing_merge(&t, &tp, &t, ATA_TIMING_SETUP);
  116. }
  117. }
  118. printk(KERN_DEBUG DRV_NAME ": active %d recovery %d setup %d.\n",
  119. t.active, t.recover, t.setup);
  120. if (t.recover > 16) {
  121. t.active += t.recover - 16;
  122. t.recover = 16;
  123. }
  124. if (t.active > 16)
  125. t.active = 16;
  126. /* Now convert the clocks into values we can actually stuff into
  127. the chip */
  128. if (t.recover == 16)
  129. t.recover = 0;
  130. else if (t.recover > 1)
  131. t.recover--;
  132. else
  133. t.recover = 15;
  134. if (t.setup > 4)
  135. t.setup = 0xC0;
  136. else
  137. t.setup = setup_data[t.setup];
  138. t.active &= 0x0F; /* 0 = 16 */
  139. /* Load setup timing */
  140. pci_read_config_byte(pdev, arttim, &reg);
  141. reg &= 0x3F;
  142. reg |= t.setup;
  143. pci_write_config_byte(pdev, arttim, reg);
  144. /* Load active/recovery */
  145. pci_write_config_byte(pdev, drwtim, (t.active << 4) | t.recover);
  146. }
  147. /**
  148. * cmd64x_set_piomode - set initial PIO mode data
  149. * @ap: ATA interface
  150. * @adev: ATA device
  151. *
  152. * Used when configuring the devices ot set the PIO timings. All the
  153. * actual work is done by the PIO/MWDMA setting helper
  154. */
  155. static void cmd64x_set_piomode(struct ata_port *ap, struct ata_device *adev)
  156. {
  157. cmd64x_set_timing(ap, adev, adev->pio_mode);
  158. }
  159. /**
  160. * cmd64x_set_dmamode - set initial DMA mode data
  161. * @ap: ATA interface
  162. * @adev: ATA device
  163. *
  164. * Called to do the DMA mode setup.
  165. */
  166. static void cmd64x_set_dmamode(struct ata_port *ap, struct ata_device *adev)
  167. {
  168. static const u8 udma_data[] = {
  169. 0x30, 0x20, 0x10, 0x20, 0x10, 0x00
  170. };
  171. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  172. u8 regU, regD;
  173. int pciU = UDIDETCR0 + 8 * ap->port_no;
  174. int pciD = BMIDESR0 + 8 * ap->port_no;
  175. int shift = 2 * adev->devno;
  176. pci_read_config_byte(pdev, pciD, &regD);
  177. pci_read_config_byte(pdev, pciU, &regU);
  178. /* DMA bits off */
  179. regD &= ~(0x20 << adev->devno);
  180. /* DMA control bits */
  181. regU &= ~(0x30 << shift);
  182. /* DMA timing bits */
  183. regU &= ~(0x05 << adev->devno);
  184. if (adev->dma_mode >= XFER_UDMA_0) {
  185. /* Merge the timing value */
  186. regU |= udma_data[adev->dma_mode - XFER_UDMA_0] << shift;
  187. /* Merge the control bits */
  188. regU |= 1 << adev->devno; /* UDMA on */
  189. if (adev->dma_mode > XFER_UDMA_2) /* 15nS timing */
  190. regU |= 4 << adev->devno;
  191. } else {
  192. regU &= ~ (1 << adev->devno); /* UDMA off */
  193. cmd64x_set_timing(ap, adev, adev->dma_mode);
  194. }
  195. regD |= 0x20 << adev->devno;
  196. pci_write_config_byte(pdev, pciU, regU);
  197. pci_write_config_byte(pdev, pciD, regD);
  198. }
  199. /**
  200. * cmd64x_sff_irq_check - check IDE interrupt
  201. * @ap: ATA interface
  202. *
  203. * Check IDE interrupt in CFR/ARTTIM23 registers.
  204. */
  205. static bool cmd64x_sff_irq_check(struct ata_port *ap)
  206. {
  207. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  208. int irq_mask = ap->port_no ? ARTTIM23_INTR_CH1 : CFR_INTR_CH0;
  209. int irq_reg = ap->port_no ? ARTTIM23 : CFR;
  210. u8 irq_stat;
  211. /* NOTE: reading the register should clear the interrupt */
  212. pci_read_config_byte(pdev, irq_reg, &irq_stat);
  213. return irq_stat & irq_mask;
  214. }
  215. /**
  216. * cmd64x_sff_irq_clear - clear IDE interrupt
  217. * @ap: ATA interface
  218. *
  219. * Clear IDE interrupt in CFR/ARTTIM23 and DMA status registers.
  220. */
  221. static void cmd64x_sff_irq_clear(struct ata_port *ap)
  222. {
  223. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  224. int irq_reg = ap->port_no ? ARTTIM23 : CFR;
  225. u8 irq_stat;
  226. ata_bmdma_irq_clear(ap);
  227. /* Reading the register should be enough to clear the interrupt */
  228. pci_read_config_byte(pdev, irq_reg, &irq_stat);
  229. }
  230. /**
  231. * cmd648_sff_irq_check - check IDE interrupt
  232. * @ap: ATA interface
  233. *
  234. * Check IDE interrupt in MRDMODE register.
  235. */
  236. static bool cmd648_sff_irq_check(struct ata_port *ap)
  237. {
  238. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  239. unsigned long base = pci_resource_start(pdev, 4);
  240. int irq_mask = ap->port_no ? MRDMODE_INTR_CH1 : MRDMODE_INTR_CH0;
  241. u8 mrdmode = inb(base + 1);
  242. return mrdmode & irq_mask;
  243. }
  244. /**
  245. * cmd648_sff_irq_clear - clear IDE interrupt
  246. * @ap: ATA interface
  247. *
  248. * Clear IDE interrupt in MRDMODE and DMA status registers.
  249. */
  250. static void cmd648_sff_irq_clear(struct ata_port *ap)
  251. {
  252. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  253. unsigned long base = pci_resource_start(pdev, 4);
  254. int irq_mask = ap->port_no ? MRDMODE_INTR_CH1 : MRDMODE_INTR_CH0;
  255. u8 mrdmode;
  256. ata_bmdma_irq_clear(ap);
  257. /* Clear this port's interrupt bit (leaving the other port alone) */
  258. mrdmode = inb(base + 1);
  259. mrdmode &= ~(MRDMODE_INTR_CH0 | MRDMODE_INTR_CH1);
  260. outb(mrdmode | irq_mask, base + 1);
  261. }
  262. /**
  263. * cmd646r1_bmdma_stop - DMA stop callback
  264. * @qc: Command in progress
  265. *
  266. * Stub for now while investigating the r1 quirk in the old driver.
  267. */
  268. static void cmd646r1_bmdma_stop(struct ata_queued_cmd *qc)
  269. {
  270. ata_bmdma_stop(qc);
  271. }
  272. static struct scsi_host_template cmd64x_sht = {
  273. ATA_BMDMA_SHT(DRV_NAME),
  274. };
  275. static const struct ata_port_operations cmd64x_base_ops = {
  276. .inherits = &ata_bmdma_port_ops,
  277. .set_piomode = cmd64x_set_piomode,
  278. .set_dmamode = cmd64x_set_dmamode,
  279. };
  280. static struct ata_port_operations cmd64x_port_ops = {
  281. .inherits = &cmd64x_base_ops,
  282. .sff_irq_check = cmd64x_sff_irq_check,
  283. .sff_irq_clear = cmd64x_sff_irq_clear,
  284. .cable_detect = ata_cable_40wire,
  285. };
  286. static struct ata_port_operations cmd646r1_port_ops = {
  287. .inherits = &cmd64x_base_ops,
  288. .sff_irq_check = cmd64x_sff_irq_check,
  289. .sff_irq_clear = cmd64x_sff_irq_clear,
  290. .bmdma_stop = cmd646r1_bmdma_stop,
  291. .cable_detect = ata_cable_40wire,
  292. };
  293. static struct ata_port_operations cmd646r3_port_ops = {
  294. .inherits = &cmd64x_base_ops,
  295. .sff_irq_check = cmd648_sff_irq_check,
  296. .sff_irq_clear = cmd648_sff_irq_clear,
  297. .cable_detect = ata_cable_40wire,
  298. };
  299. static struct ata_port_operations cmd648_port_ops = {
  300. .inherits = &cmd64x_base_ops,
  301. .sff_irq_check = cmd648_sff_irq_check,
  302. .sff_irq_clear = cmd648_sff_irq_clear,
  303. .cable_detect = cmd648_cable_detect,
  304. };
  305. static void cmd64x_fixup(struct pci_dev *pdev)
  306. {
  307. u8 mrdmode;
  308. pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 64);
  309. pci_read_config_byte(pdev, MRDMODE, &mrdmode);
  310. mrdmode &= ~0x30; /* IRQ set up */
  311. mrdmode |= 0x02; /* Memory read line enable */
  312. pci_write_config_byte(pdev, MRDMODE, mrdmode);
  313. /* PPC specific fixup copied from old driver */
  314. #ifdef CONFIG_PPC
  315. pci_write_config_byte(pdev, UDIDETCR0, 0xF0);
  316. #endif
  317. }
  318. static int cmd64x_init_one(struct pci_dev *pdev, const struct pci_device_id *id)
  319. {
  320. static const struct ata_port_info cmd_info[7] = {
  321. { /* CMD 643 - no UDMA */
  322. .flags = ATA_FLAG_SLAVE_POSS,
  323. .pio_mask = ATA_PIO4,
  324. .mwdma_mask = ATA_MWDMA2,
  325. .port_ops = &cmd64x_port_ops
  326. },
  327. { /* CMD 646 with broken UDMA */
  328. .flags = ATA_FLAG_SLAVE_POSS,
  329. .pio_mask = ATA_PIO4,
  330. .mwdma_mask = ATA_MWDMA2,
  331. .port_ops = &cmd64x_port_ops
  332. },
  333. { /* CMD 646U with broken UDMA */
  334. .flags = ATA_FLAG_SLAVE_POSS,
  335. .pio_mask = ATA_PIO4,
  336. .mwdma_mask = ATA_MWDMA2,
  337. .port_ops = &cmd646r3_port_ops
  338. },
  339. { /* CMD 646U2 with working UDMA */
  340. .flags = ATA_FLAG_SLAVE_POSS,
  341. .pio_mask = ATA_PIO4,
  342. .mwdma_mask = ATA_MWDMA2,
  343. .udma_mask = ATA_UDMA2,
  344. .port_ops = &cmd646r3_port_ops
  345. },
  346. { /* CMD 646 rev 1 */
  347. .flags = ATA_FLAG_SLAVE_POSS,
  348. .pio_mask = ATA_PIO4,
  349. .mwdma_mask = ATA_MWDMA2,
  350. .port_ops = &cmd646r1_port_ops
  351. },
  352. { /* CMD 648 */
  353. .flags = ATA_FLAG_SLAVE_POSS,
  354. .pio_mask = ATA_PIO4,
  355. .mwdma_mask = ATA_MWDMA2,
  356. .udma_mask = ATA_UDMA4,
  357. .port_ops = &cmd648_port_ops
  358. },
  359. { /* CMD 649 */
  360. .flags = ATA_FLAG_SLAVE_POSS,
  361. .pio_mask = ATA_PIO4,
  362. .mwdma_mask = ATA_MWDMA2,
  363. .udma_mask = ATA_UDMA5,
  364. .port_ops = &cmd648_port_ops
  365. }
  366. };
  367. const struct ata_port_info *ppi[] = {
  368. &cmd_info[id->driver_data],
  369. &cmd_info[id->driver_data],
  370. NULL
  371. };
  372. u8 reg;
  373. int rc;
  374. struct pci_dev *bridge = pdev->bus->self;
  375. /* mobility split bridges don't report enabled ports correctly */
  376. int port_ok = !(bridge && bridge->vendor ==
  377. PCI_VENDOR_ID_MOBILITY_ELECTRONICS);
  378. /* all (with exceptions below) apart from 643 have CNTRL_CH0 bit */
  379. int cntrl_ch0_ok = (id->driver_data != 0);
  380. rc = pcim_enable_device(pdev);
  381. if (rc)
  382. return rc;
  383. if (id->driver_data == 0) /* 643 */
  384. ata_pci_bmdma_clear_simplex(pdev);
  385. if (pdev->device == PCI_DEVICE_ID_CMD_646)
  386. switch (pdev->revision) {
  387. /* UDMA works since rev 5 */
  388. default:
  389. ppi[0] = &cmd_info[3];
  390. ppi[1] = &cmd_info[3];
  391. break;
  392. /* Interrupts in MRDMODE since rev 3 */
  393. case 3:
  394. case 4:
  395. ppi[0] = &cmd_info[2];
  396. ppi[1] = &cmd_info[2];
  397. break;
  398. /* Rev 1 with other problems? */
  399. case 1:
  400. ppi[0] = &cmd_info[4];
  401. ppi[1] = &cmd_info[4];
  402. /* FALL THRU */
  403. /* Early revs have no CNTRL_CH0 */
  404. case 2:
  405. case 0:
  406. cntrl_ch0_ok = 0;
  407. break;
  408. }
  409. cmd64x_fixup(pdev);
  410. /* check for enabled ports */
  411. pci_read_config_byte(pdev, CNTRL, &reg);
  412. if (!port_ok)
  413. dev_notice(&pdev->dev, "Mobility Bridge detected, ignoring CNTRL port enable/disable\n");
  414. if (port_ok && cntrl_ch0_ok && !(reg & CNTRL_CH0)) {
  415. dev_notice(&pdev->dev, "Primary port is disabled\n");
  416. ppi[0] = &ata_dummy_port_info;
  417. }
  418. if (port_ok && !(reg & CNTRL_CH1)) {
  419. dev_notice(&pdev->dev, "Secondary port is disabled\n");
  420. ppi[1] = &ata_dummy_port_info;
  421. }
  422. return ata_pci_bmdma_init_one(pdev, ppi, &cmd64x_sht, NULL, 0);
  423. }
  424. #ifdef CONFIG_PM_SLEEP
  425. static int cmd64x_reinit_one(struct pci_dev *pdev)
  426. {
  427. struct ata_host *host = pci_get_drvdata(pdev);
  428. int rc;
  429. rc = ata_pci_device_do_resume(pdev);
  430. if (rc)
  431. return rc;
  432. cmd64x_fixup(pdev);
  433. ata_host_resume(host);
  434. return 0;
  435. }
  436. #endif
  437. static const struct pci_device_id cmd64x[] = {
  438. { PCI_VDEVICE(CMD, PCI_DEVICE_ID_CMD_643), 0 },
  439. { PCI_VDEVICE(CMD, PCI_DEVICE_ID_CMD_646), 1 },
  440. { PCI_VDEVICE(CMD, PCI_DEVICE_ID_CMD_648), 5 },
  441. { PCI_VDEVICE(CMD, PCI_DEVICE_ID_CMD_649), 6 },
  442. { },
  443. };
  444. static struct pci_driver cmd64x_pci_driver = {
  445. .name = DRV_NAME,
  446. .id_table = cmd64x,
  447. .probe = cmd64x_init_one,
  448. .remove = ata_pci_remove_one,
  449. #ifdef CONFIG_PM_SLEEP
  450. .suspend = ata_pci_device_suspend,
  451. .resume = cmd64x_reinit_one,
  452. #endif
  453. };
  454. module_pci_driver(cmd64x_pci_driver);
  455. MODULE_AUTHOR("Alan Cox");
  456. MODULE_DESCRIPTION("low-level driver for CMD64x series PATA controllers");
  457. MODULE_LICENSE("GPL");
  458. MODULE_DEVICE_TABLE(pci, cmd64x);
  459. MODULE_VERSION(DRV_VERSION);