pata_cmd64x.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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. *
  7. * Based upon
  8. * linux/drivers/ide/pci/cmd64x.c Version 1.30 Sept 10, 2002
  9. *
  10. * cmd64x.c: Enable interrupts at initialization time on Ultra/PCI machines.
  11. * Note, this driver is not used at all on other systems because
  12. * there the "BIOS" has done all of the following already.
  13. * Due to massive hardware bugs, UltraDMA is only supported
  14. * on the 646U2 and not on the 646U.
  15. *
  16. * Copyright (C) 1998 Eddie C. Dost (ecd@skynet.be)
  17. * Copyright (C) 1998 David S. Miller (davem@redhat.com)
  18. *
  19. * Copyright (C) 1999-2002 Andre Hedrick <andre@linux-ide.org>
  20. *
  21. * TODO
  22. * Testing work
  23. */
  24. #include <linux/kernel.h>
  25. #include <linux/module.h>
  26. #include <linux/pci.h>
  27. #include <linux/init.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.5"
  34. /*
  35. * CMD64x specific registers definition.
  36. */
  37. enum {
  38. CFR = 0x50,
  39. CFR_INTR_CH0 = 0x04,
  40. CMDTIM = 0x52,
  41. ARTTIM0 = 0x53,
  42. DRWTIM0 = 0x54,
  43. ARTTIM1 = 0x55,
  44. DRWTIM1 = 0x56,
  45. ARTTIM23 = 0x57,
  46. ARTTIM23_DIS_RA2 = 0x04,
  47. ARTTIM23_DIS_RA3 = 0x08,
  48. ARTTIM23_INTR_CH1 = 0x10,
  49. DRWTIM2 = 0x58,
  50. BRST = 0x59,
  51. DRWTIM3 = 0x5b,
  52. BMIDECR0 = 0x70,
  53. MRDMODE = 0x71,
  54. MRDMODE_INTR_CH0 = 0x04,
  55. MRDMODE_INTR_CH1 = 0x08,
  56. BMIDESR0 = 0x72,
  57. UDIDETCR0 = 0x73,
  58. DTPR0 = 0x74,
  59. BMIDECR1 = 0x78,
  60. BMIDECSR = 0x79,
  61. UDIDETCR1 = 0x7B,
  62. DTPR1 = 0x7C
  63. };
  64. static int cmd648_cable_detect(struct ata_port *ap)
  65. {
  66. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  67. u8 r;
  68. /* Check cable detect bits */
  69. pci_read_config_byte(pdev, BMIDECSR, &r);
  70. if (r & (1 << ap->port_no))
  71. return ATA_CBL_PATA80;
  72. return ATA_CBL_PATA40;
  73. }
  74. /**
  75. * cmd64x_set_piomode - set PIO and MWDMA timing
  76. * @ap: ATA interface
  77. * @adev: ATA device
  78. * @mode: mode
  79. *
  80. * Called to do the PIO and MWDMA mode setup.
  81. */
  82. static void cmd64x_set_timing(struct ata_port *ap, struct ata_device *adev, u8 mode)
  83. {
  84. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  85. struct ata_timing t;
  86. const unsigned long T = 1000000 / 33;
  87. const u8 setup_data[] = { 0x40, 0x40, 0x40, 0x80, 0x00 };
  88. u8 reg;
  89. /* Port layout is not logical so use a table */
  90. const u8 arttim_port[2][2] = {
  91. { ARTTIM0, ARTTIM1 },
  92. { ARTTIM23, ARTTIM23 }
  93. };
  94. const u8 drwtim_port[2][2] = {
  95. { DRWTIM0, DRWTIM1 },
  96. { DRWTIM2, DRWTIM3 }
  97. };
  98. int arttim = arttim_port[ap->port_no][adev->devno];
  99. int drwtim = drwtim_port[ap->port_no][adev->devno];
  100. /* ata_timing_compute is smart and will produce timings for MWDMA
  101. that don't violate the drives PIO capabilities. */
  102. if (ata_timing_compute(adev, mode, &t, T, 0) < 0) {
  103. printk(KERN_ERR DRV_NAME ": mode computation failed.\n");
  104. return;
  105. }
  106. if (ap->port_no) {
  107. /* Slave has shared address setup */
  108. struct ata_device *pair = ata_dev_pair(adev);
  109. if (pair) {
  110. struct ata_timing tp;
  111. ata_timing_compute(pair, pair->pio_mode, &tp, T, 0);
  112. ata_timing_merge(&t, &tp, &t, ATA_TIMING_SETUP);
  113. }
  114. }
  115. printk(KERN_DEBUG DRV_NAME ": active %d recovery %d setup %d.\n",
  116. t.active, t.recover, t.setup);
  117. if (t.recover > 16) {
  118. t.active += t.recover - 16;
  119. t.recover = 16;
  120. }
  121. if (t.active > 16)
  122. t.active = 16;
  123. /* Now convert the clocks into values we can actually stuff into
  124. the chip */
  125. if (t.recover == 16)
  126. t.recover = 0;
  127. else if (t.recover > 1)
  128. t.recover--;
  129. else
  130. t.recover = 15;
  131. if (t.setup > 4)
  132. t.setup = 0xC0;
  133. else
  134. t.setup = setup_data[t.setup];
  135. t.active &= 0x0F; /* 0 = 16 */
  136. /* Load setup timing */
  137. pci_read_config_byte(pdev, arttim, &reg);
  138. reg &= 0x3F;
  139. reg |= t.setup;
  140. pci_write_config_byte(pdev, arttim, reg);
  141. /* Load active/recovery */
  142. pci_write_config_byte(pdev, drwtim, (t.active << 4) | t.recover);
  143. }
  144. /**
  145. * cmd64x_set_piomode - set initial PIO mode data
  146. * @ap: ATA interface
  147. * @adev: ATA device
  148. *
  149. * Used when configuring the devices ot set the PIO timings. All the
  150. * actual work is done by the PIO/MWDMA setting helper
  151. */
  152. static void cmd64x_set_piomode(struct ata_port *ap, struct ata_device *adev)
  153. {
  154. cmd64x_set_timing(ap, adev, adev->pio_mode);
  155. }
  156. /**
  157. * cmd64x_set_dmamode - set initial DMA mode data
  158. * @ap: ATA interface
  159. * @adev: ATA device
  160. *
  161. * Called to do the DMA mode setup.
  162. */
  163. static void cmd64x_set_dmamode(struct ata_port *ap, struct ata_device *adev)
  164. {
  165. static const u8 udma_data[] = {
  166. 0x30, 0x20, 0x10, 0x20, 0x10, 0x00
  167. };
  168. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  169. u8 regU, regD;
  170. int pciU = UDIDETCR0 + 8 * ap->port_no;
  171. int pciD = BMIDESR0 + 8 * ap->port_no;
  172. int shift = 2 * adev->devno;
  173. pci_read_config_byte(pdev, pciD, &regD);
  174. pci_read_config_byte(pdev, pciU, &regU);
  175. /* DMA bits off */
  176. regD &= ~(0x20 << adev->devno);
  177. /* DMA control bits */
  178. regU &= ~(0x30 << shift);
  179. /* DMA timing bits */
  180. regU &= ~(0x05 << adev->devno);
  181. if (adev->dma_mode >= XFER_UDMA_0) {
  182. /* Merge the timing value */
  183. regU |= udma_data[adev->dma_mode - XFER_UDMA_0] << shift;
  184. /* Merge the control bits */
  185. regU |= 1 << adev->devno; /* UDMA on */
  186. if (adev->dma_mode > XFER_UDMA_2) /* 15nS timing */
  187. regU |= 4 << adev->devno;
  188. } else {
  189. regU &= ~ (1 << adev->devno); /* UDMA off */
  190. cmd64x_set_timing(ap, adev, adev->dma_mode);
  191. }
  192. regD |= 0x20 << adev->devno;
  193. pci_write_config_byte(pdev, pciU, regU);
  194. pci_write_config_byte(pdev, pciD, regD);
  195. }
  196. /**
  197. * cmd648_dma_stop - DMA stop callback
  198. * @qc: Command in progress
  199. *
  200. * DMA has completed.
  201. */
  202. static void cmd648_bmdma_stop(struct ata_queued_cmd *qc)
  203. {
  204. struct ata_port *ap = qc->ap;
  205. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  206. u8 dma_intr;
  207. int dma_mask = ap->port_no ? ARTTIM23_INTR_CH1 : CFR_INTR_CH0;
  208. int dma_reg = ap->port_no ? ARTTIM23 : CFR;
  209. ata_bmdma_stop(qc);
  210. pci_read_config_byte(pdev, dma_reg, &dma_intr);
  211. pci_write_config_byte(pdev, dma_reg, dma_intr | dma_mask);
  212. }
  213. /**
  214. * cmd646r1_dma_stop - DMA stop callback
  215. * @qc: Command in progress
  216. *
  217. * Stub for now while investigating the r1 quirk in the old driver.
  218. */
  219. static void cmd646r1_bmdma_stop(struct ata_queued_cmd *qc)
  220. {
  221. ata_bmdma_stop(qc);
  222. }
  223. static struct scsi_host_template cmd64x_sht = {
  224. ATA_BMDMA_SHT(DRV_NAME),
  225. };
  226. static const struct ata_port_operations cmd64x_base_ops = {
  227. .inherits = &ata_bmdma_port_ops,
  228. .set_piomode = cmd64x_set_piomode,
  229. .set_dmamode = cmd64x_set_dmamode,
  230. };
  231. static struct ata_port_operations cmd64x_port_ops = {
  232. .inherits = &cmd64x_base_ops,
  233. .cable_detect = ata_cable_40wire,
  234. };
  235. static struct ata_port_operations cmd646r1_port_ops = {
  236. .inherits = &cmd64x_base_ops,
  237. .bmdma_stop = cmd646r1_bmdma_stop,
  238. .cable_detect = ata_cable_40wire,
  239. };
  240. static struct ata_port_operations cmd648_port_ops = {
  241. .inherits = &cmd64x_base_ops,
  242. .bmdma_stop = cmd648_bmdma_stop,
  243. .cable_detect = cmd648_cable_detect,
  244. };
  245. static int cmd64x_init_one(struct pci_dev *pdev, const struct pci_device_id *id)
  246. {
  247. static const struct ata_port_info cmd_info[6] = {
  248. { /* CMD 643 - no UDMA */
  249. .flags = ATA_FLAG_SLAVE_POSS,
  250. .pio_mask = ATA_PIO4,
  251. .mwdma_mask = ATA_MWDMA2,
  252. .port_ops = &cmd64x_port_ops
  253. },
  254. { /* CMD 646 with broken UDMA */
  255. .flags = ATA_FLAG_SLAVE_POSS,
  256. .pio_mask = ATA_PIO4,
  257. .mwdma_mask = ATA_MWDMA2,
  258. .port_ops = &cmd64x_port_ops
  259. },
  260. { /* CMD 646 with working UDMA */
  261. .flags = ATA_FLAG_SLAVE_POSS,
  262. .pio_mask = ATA_PIO4,
  263. .mwdma_mask = ATA_MWDMA2,
  264. .udma_mask = ATA_UDMA2,
  265. .port_ops = &cmd64x_port_ops
  266. },
  267. { /* CMD 646 rev 1 */
  268. .flags = ATA_FLAG_SLAVE_POSS,
  269. .pio_mask = ATA_PIO4,
  270. .mwdma_mask = ATA_MWDMA2,
  271. .port_ops = &cmd646r1_port_ops
  272. },
  273. { /* CMD 648 */
  274. .flags = ATA_FLAG_SLAVE_POSS,
  275. .pio_mask = ATA_PIO4,
  276. .mwdma_mask = ATA_MWDMA2,
  277. .udma_mask = ATA_UDMA4,
  278. .port_ops = &cmd648_port_ops
  279. },
  280. { /* CMD 649 */
  281. .flags = ATA_FLAG_SLAVE_POSS,
  282. .pio_mask = ATA_PIO4,
  283. .mwdma_mask = ATA_MWDMA2,
  284. .udma_mask = ATA_UDMA5,
  285. .port_ops = &cmd648_port_ops
  286. }
  287. };
  288. const struct ata_port_info *ppi[] = { &cmd_info[id->driver_data], NULL };
  289. u8 mrdmode;
  290. int rc;
  291. rc = pcim_enable_device(pdev);
  292. if (rc)
  293. return rc;
  294. if (id->driver_data == 0) /* 643 */
  295. ata_pci_bmdma_clear_simplex(pdev);
  296. if (pdev->device == PCI_DEVICE_ID_CMD_646) {
  297. /* Does UDMA work ? */
  298. if (pdev->revision > 4)
  299. ppi[0] = &cmd_info[2];
  300. /* Early rev with other problems ? */
  301. else if (pdev->revision == 1)
  302. ppi[0] = &cmd_info[3];
  303. }
  304. pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 64);
  305. pci_read_config_byte(pdev, MRDMODE, &mrdmode);
  306. mrdmode &= ~ 0x30; /* IRQ set up */
  307. mrdmode |= 0x02; /* Memory read line enable */
  308. pci_write_config_byte(pdev, MRDMODE, mrdmode);
  309. /* Force PIO 0 here.. */
  310. /* PPC specific fixup copied from old driver */
  311. #ifdef CONFIG_PPC
  312. pci_write_config_byte(pdev, UDIDETCR0, 0xF0);
  313. #endif
  314. return ata_pci_bmdma_init_one(pdev, ppi, &cmd64x_sht, NULL, 0);
  315. }
  316. #ifdef CONFIG_PM
  317. static int cmd64x_reinit_one(struct pci_dev *pdev)
  318. {
  319. struct ata_host *host = dev_get_drvdata(&pdev->dev);
  320. u8 mrdmode;
  321. int rc;
  322. rc = ata_pci_device_do_resume(pdev);
  323. if (rc)
  324. return rc;
  325. pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 64);
  326. pci_read_config_byte(pdev, MRDMODE, &mrdmode);
  327. mrdmode &= ~ 0x30; /* IRQ set up */
  328. mrdmode |= 0x02; /* Memory read line enable */
  329. pci_write_config_byte(pdev, MRDMODE, mrdmode);
  330. #ifdef CONFIG_PPC
  331. pci_write_config_byte(pdev, UDIDETCR0, 0xF0);
  332. #endif
  333. ata_host_resume(host);
  334. return 0;
  335. }
  336. #endif
  337. static const struct pci_device_id cmd64x[] = {
  338. { PCI_VDEVICE(CMD, PCI_DEVICE_ID_CMD_643), 0 },
  339. { PCI_VDEVICE(CMD, PCI_DEVICE_ID_CMD_646), 1 },
  340. { PCI_VDEVICE(CMD, PCI_DEVICE_ID_CMD_648), 4 },
  341. { PCI_VDEVICE(CMD, PCI_DEVICE_ID_CMD_649), 5 },
  342. { },
  343. };
  344. static struct pci_driver cmd64x_pci_driver = {
  345. .name = DRV_NAME,
  346. .id_table = cmd64x,
  347. .probe = cmd64x_init_one,
  348. .remove = ata_pci_remove_one,
  349. #ifdef CONFIG_PM
  350. .suspend = ata_pci_device_suspend,
  351. .resume = cmd64x_reinit_one,
  352. #endif
  353. };
  354. static int __init cmd64x_init(void)
  355. {
  356. return pci_register_driver(&cmd64x_pci_driver);
  357. }
  358. static void __exit cmd64x_exit(void)
  359. {
  360. pci_unregister_driver(&cmd64x_pci_driver);
  361. }
  362. MODULE_AUTHOR("Alan Cox");
  363. MODULE_DESCRIPTION("low-level driver for CMD64x series PATA controllers");
  364. MODULE_LICENSE("GPL");
  365. MODULE_DEVICE_TABLE(pci, cmd64x);
  366. MODULE_VERSION(DRV_VERSION);
  367. module_init(cmd64x_init);
  368. module_exit(cmd64x_exit);