pata_cmd64x.c 10 KB

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