pata_cmd64x.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  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.2"
  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 initial PIO mode data
  85. * @ap: ATA interface
  86. * @adev: ATA device
  87. *
  88. * Called to do the PIO mode setup.
  89. */
  90. static void cmd64x_set_piomode(struct ata_port *ap, struct ata_device *adev)
  91. {
  92. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  93. struct ata_timing t;
  94. const unsigned long T = 1000000 / 33;
  95. const u8 setup_data[] = { 0x40, 0x40, 0x40, 0x80, 0x00 };
  96. u8 reg;
  97. /* Port layout is not logical so use a table */
  98. const u8 arttim_port[2][2] = {
  99. { ARTTIM0, ARTTIM1 },
  100. { ARTTIM23, ARTTIM23 }
  101. };
  102. const u8 drwtim_port[2][2] = {
  103. { DRWTIM0, DRWTIM1 },
  104. { DRWTIM2, DRWTIM3 }
  105. };
  106. int arttim = arttim_port[ap->port_no][adev->devno];
  107. int drwtim = drwtim_port[ap->port_no][adev->devno];
  108. if (ata_timing_compute(adev, adev->pio_mode, &t, T, 0) < 0) {
  109. printk(KERN_ERR DRV_NAME ": mode computation failed.\n");
  110. return;
  111. }
  112. if (ap->port_no) {
  113. /* Slave has shared address setup */
  114. struct ata_device *pair = ata_dev_pair(adev);
  115. if (pair) {
  116. struct ata_timing tp;
  117. ata_timing_compute(pair, pair->pio_mode, &tp, T, 0);
  118. ata_timing_merge(&t, &tp, &t, ATA_TIMING_SETUP);
  119. }
  120. }
  121. printk(KERN_DEBUG DRV_NAME ": active %d recovery %d setup %d.\n",
  122. t.active, t.recover, t.setup);
  123. if (t.recover > 16) {
  124. t.active += t.recover - 16;
  125. t.recover = 16;
  126. }
  127. if (t.active > 16)
  128. t.active = 16;
  129. /* Now convert the clocks into values we can actually stuff into
  130. the chip */
  131. if (t.recover > 1)
  132. t.recover--;
  133. else
  134. t.recover = 15;
  135. if (t.setup > 4)
  136. t.setup = 0xC0;
  137. else
  138. t.setup = setup_data[t.setup];
  139. t.active &= 0x0F; /* 0 = 16 */
  140. /* Load setup timing */
  141. pci_read_config_byte(pdev, arttim, &reg);
  142. reg &= 0x3F;
  143. reg |= t.setup;
  144. pci_write_config_byte(pdev, arttim, reg);
  145. /* Load active/recovery */
  146. pci_write_config_byte(pdev, drwtim, (t.active << 4) | t.recover);
  147. }
  148. /**
  149. * cmd64x_set_dmamode - set initial DMA mode data
  150. * @ap: ATA interface
  151. * @adev: ATA device
  152. *
  153. * Called to do the DMA mode setup.
  154. */
  155. static void cmd64x_set_dmamode(struct ata_port *ap, struct ata_device *adev)
  156. {
  157. static const u8 udma_data[] = {
  158. 0x30, 0x20, 0x10, 0x20, 0x10, 0x00
  159. };
  160. static const u8 mwdma_data[] = {
  161. 0x30, 0x20, 0x10
  162. };
  163. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  164. u8 regU, regD;
  165. int pciU = UDIDETCR0 + 8 * ap->port_no;
  166. int pciD = BMIDESR0 + 8 * ap->port_no;
  167. int shift = 2 * adev->devno;
  168. pci_read_config_byte(pdev, pciD, &regD);
  169. pci_read_config_byte(pdev, pciU, &regU);
  170. /* DMA bits off */
  171. regD &= ~(0x20 << adev->devno);
  172. /* DMA control bits */
  173. regU &= ~(0x30 << shift);
  174. /* DMA timing bits */
  175. regU &= ~(0x05 << adev->devno);
  176. if (adev->dma_mode >= XFER_UDMA_0) {
  177. /* Merge thge timing value */
  178. regU |= udma_data[adev->dma_mode - XFER_UDMA_0] << shift;
  179. /* Merge the control bits */
  180. regU |= 1 << adev->devno; /* UDMA on */
  181. if (adev->dma_mode > 2) /* 15nS timing */
  182. regU |= 4 << adev->devno;
  183. } else
  184. regD |= mwdma_data[adev->dma_mode - XFER_MW_DMA_0] << shift;
  185. regD |= 0x20 << adev->devno;
  186. pci_write_config_byte(pdev, pciU, regU);
  187. pci_write_config_byte(pdev, pciD, regD);
  188. }
  189. /**
  190. * cmd648_dma_stop - DMA stop callback
  191. * @qc: Command in progress
  192. *
  193. * DMA has completed.
  194. */
  195. static void cmd648_bmdma_stop(struct ata_queued_cmd *qc)
  196. {
  197. struct ata_port *ap = qc->ap;
  198. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  199. u8 dma_intr;
  200. int dma_mask = ap->port_no ? ARTTIM23_INTR_CH1 : CFR_INTR_CH0;
  201. int dma_reg = ap->port_no ? ARTTIM2 : CFR;
  202. ata_bmdma_stop(qc);
  203. pci_read_config_byte(pdev, dma_reg, &dma_intr);
  204. pci_write_config_byte(pdev, dma_reg, dma_intr | dma_mask);
  205. }
  206. /**
  207. * cmd646r1_dma_stop - DMA stop callback
  208. * @qc: Command in progress
  209. *
  210. * Stub for now while investigating the r1 quirk in the old driver.
  211. */
  212. static void cmd646r1_bmdma_stop(struct ata_queued_cmd *qc)
  213. {
  214. ata_bmdma_stop(qc);
  215. }
  216. static struct scsi_host_template cmd64x_sht = {
  217. .module = THIS_MODULE,
  218. .name = DRV_NAME,
  219. .ioctl = ata_scsi_ioctl,
  220. .queuecommand = ata_scsi_queuecmd,
  221. .can_queue = ATA_DEF_QUEUE,
  222. .this_id = ATA_SHT_THIS_ID,
  223. .sg_tablesize = LIBATA_MAX_PRD,
  224. .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
  225. .emulated = ATA_SHT_EMULATED,
  226. .use_clustering = ATA_SHT_USE_CLUSTERING,
  227. .proc_name = DRV_NAME,
  228. .dma_boundary = ATA_DMA_BOUNDARY,
  229. .slave_configure = ata_scsi_slave_config,
  230. .slave_destroy = ata_scsi_slave_destroy,
  231. .bios_param = ata_std_bios_param,
  232. };
  233. static struct ata_port_operations cmd64x_port_ops = {
  234. .port_disable = ata_port_disable,
  235. .set_piomode = cmd64x_set_piomode,
  236. .set_dmamode = cmd64x_set_dmamode,
  237. .mode_filter = ata_pci_default_filter,
  238. .tf_load = ata_tf_load,
  239. .tf_read = ata_tf_read,
  240. .check_status = ata_check_status,
  241. .exec_command = ata_exec_command,
  242. .dev_select = ata_std_dev_select,
  243. .freeze = ata_bmdma_freeze,
  244. .thaw = ata_bmdma_thaw,
  245. .error_handler = ata_bmdma_error_handler,
  246. .post_internal_cmd = ata_bmdma_post_internal_cmd,
  247. .cable_detect = ata_cable_40wire,
  248. .bmdma_setup = ata_bmdma_setup,
  249. .bmdma_start = ata_bmdma_start,
  250. .bmdma_stop = ata_bmdma_stop,
  251. .bmdma_status = ata_bmdma_status,
  252. .qc_prep = ata_qc_prep,
  253. .qc_issue = ata_qc_issue_prot,
  254. .data_xfer = ata_data_xfer,
  255. .irq_handler = ata_interrupt,
  256. .irq_clear = ata_bmdma_irq_clear,
  257. .irq_on = ata_irq_on,
  258. .irq_ack = ata_irq_ack,
  259. .port_start = ata_port_start,
  260. };
  261. static struct ata_port_operations cmd646r1_port_ops = {
  262. .port_disable = ata_port_disable,
  263. .set_piomode = cmd64x_set_piomode,
  264. .set_dmamode = cmd64x_set_dmamode,
  265. .mode_filter = ata_pci_default_filter,
  266. .tf_load = ata_tf_load,
  267. .tf_read = ata_tf_read,
  268. .check_status = ata_check_status,
  269. .exec_command = ata_exec_command,
  270. .dev_select = ata_std_dev_select,
  271. .freeze = ata_bmdma_freeze,
  272. .thaw = ata_bmdma_thaw,
  273. .error_handler = ata_bmdma_error_handler,
  274. .post_internal_cmd = ata_bmdma_post_internal_cmd,
  275. .cable_detect = ata_cable_40wire,
  276. .bmdma_setup = ata_bmdma_setup,
  277. .bmdma_start = ata_bmdma_start,
  278. .bmdma_stop = cmd646r1_bmdma_stop,
  279. .bmdma_status = ata_bmdma_status,
  280. .qc_prep = ata_qc_prep,
  281. .qc_issue = ata_qc_issue_prot,
  282. .data_xfer = ata_data_xfer,
  283. .irq_handler = ata_interrupt,
  284. .irq_clear = ata_bmdma_irq_clear,
  285. .irq_on = ata_irq_on,
  286. .irq_ack = ata_irq_ack,
  287. .port_start = ata_port_start,
  288. };
  289. static struct ata_port_operations cmd648_port_ops = {
  290. .port_disable = ata_port_disable,
  291. .set_piomode = cmd64x_set_piomode,
  292. .set_dmamode = cmd64x_set_dmamode,
  293. .mode_filter = ata_pci_default_filter,
  294. .tf_load = ata_tf_load,
  295. .tf_read = ata_tf_read,
  296. .check_status = ata_check_status,
  297. .exec_command = ata_exec_command,
  298. .dev_select = ata_std_dev_select,
  299. .freeze = ata_bmdma_freeze,
  300. .thaw = ata_bmdma_thaw,
  301. .error_handler = ata_bmdma_error_handler,
  302. .post_internal_cmd = ata_bmdma_post_internal_cmd,
  303. .cable_detect = cmd648_cable_detect,
  304. .bmdma_setup = ata_bmdma_setup,
  305. .bmdma_start = ata_bmdma_start,
  306. .bmdma_stop = cmd648_bmdma_stop,
  307. .bmdma_status = ata_bmdma_status,
  308. .qc_prep = ata_qc_prep,
  309. .qc_issue = ata_qc_issue_prot,
  310. .data_xfer = ata_data_xfer,
  311. .irq_handler = ata_interrupt,
  312. .irq_clear = ata_bmdma_irq_clear,
  313. .irq_on = ata_irq_on,
  314. .irq_ack = ata_irq_ack,
  315. .port_start = ata_port_start,
  316. };
  317. static int cmd64x_init_one(struct pci_dev *pdev, const struct pci_device_id *id)
  318. {
  319. u32 class_rev;
  320. static const struct ata_port_info cmd_info[6] = {
  321. { /* CMD 643 - no UDMA */
  322. .sht = &cmd64x_sht,
  323. .flags = ATA_FLAG_SLAVE_POSS | ATA_FLAG_SRST,
  324. .pio_mask = 0x1f,
  325. .mwdma_mask = 0x07,
  326. .port_ops = &cmd64x_port_ops
  327. },
  328. { /* CMD 646 with broken UDMA */
  329. .sht = &cmd64x_sht,
  330. .flags = ATA_FLAG_SLAVE_POSS | ATA_FLAG_SRST,
  331. .pio_mask = 0x1f,
  332. .mwdma_mask = 0x07,
  333. .port_ops = &cmd64x_port_ops
  334. },
  335. { /* CMD 646 with working UDMA */
  336. .sht = &cmd64x_sht,
  337. .flags = ATA_FLAG_SLAVE_POSS | ATA_FLAG_SRST,
  338. .pio_mask = 0x1f,
  339. .mwdma_mask = 0x07,
  340. .udma_mask = ATA_UDMA1,
  341. .port_ops = &cmd64x_port_ops
  342. },
  343. { /* CMD 646 rev 1 */
  344. .sht = &cmd64x_sht,
  345. .flags = ATA_FLAG_SLAVE_POSS | ATA_FLAG_SRST,
  346. .pio_mask = 0x1f,
  347. .mwdma_mask = 0x07,
  348. .port_ops = &cmd646r1_port_ops
  349. },
  350. { /* CMD 648 */
  351. .sht = &cmd64x_sht,
  352. .flags = ATA_FLAG_SLAVE_POSS | ATA_FLAG_SRST,
  353. .pio_mask = 0x1f,
  354. .mwdma_mask = 0x07,
  355. .udma_mask = ATA_UDMA2,
  356. .port_ops = &cmd648_port_ops
  357. },
  358. { /* CMD 649 */
  359. .sht = &cmd64x_sht,
  360. .flags = ATA_FLAG_SLAVE_POSS | ATA_FLAG_SRST,
  361. .pio_mask = 0x1f,
  362. .mwdma_mask = 0x07,
  363. .udma_mask = ATA_UDMA3,
  364. .port_ops = &cmd648_port_ops
  365. }
  366. };
  367. const struct ata_port_info *ppi[] = { &cmd_info[id->driver_data], NULL };
  368. u8 mrdmode;
  369. pci_read_config_dword(pdev, PCI_CLASS_REVISION, &class_rev);
  370. class_rev &= 0xFF;
  371. if (id->driver_data == 0) /* 643 */
  372. ata_pci_clear_simplex(pdev);
  373. if (pdev->device == PCI_DEVICE_ID_CMD_646) {
  374. /* Does UDMA work ? */
  375. if (class_rev > 4)
  376. ppi[0] = &cmd_info[2];
  377. /* Early rev with other problems ? */
  378. else if (class_rev == 1)
  379. ppi[0] = &cmd_info[3];
  380. }
  381. pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 64);
  382. pci_read_config_byte(pdev, MRDMODE, &mrdmode);
  383. mrdmode &= ~ 0x30; /* IRQ set up */
  384. mrdmode |= 0x02; /* Memory read line enable */
  385. pci_write_config_byte(pdev, MRDMODE, mrdmode);
  386. /* Force PIO 0 here.. */
  387. /* PPC specific fixup copied from old driver */
  388. #ifdef CONFIG_PPC
  389. pci_write_config_byte(pdev, UDIDETCR0, 0xF0);
  390. #endif
  391. return ata_pci_init_one(pdev, ppi);
  392. }
  393. #ifdef CONFIG_PM
  394. static int cmd64x_reinit_one(struct pci_dev *pdev)
  395. {
  396. u8 mrdmode;
  397. pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 64);
  398. pci_read_config_byte(pdev, MRDMODE, &mrdmode);
  399. mrdmode &= ~ 0x30; /* IRQ set up */
  400. mrdmode |= 0x02; /* Memory read line enable */
  401. pci_write_config_byte(pdev, MRDMODE, mrdmode);
  402. #ifdef CONFIG_PPC
  403. pci_write_config_byte(pdev, UDIDETCR0, 0xF0);
  404. #endif
  405. return ata_pci_device_resume(pdev);
  406. }
  407. #endif
  408. static const struct pci_device_id cmd64x[] = {
  409. { PCI_VDEVICE(CMD, PCI_DEVICE_ID_CMD_643), 0 },
  410. { PCI_VDEVICE(CMD, PCI_DEVICE_ID_CMD_646), 1 },
  411. { PCI_VDEVICE(CMD, PCI_DEVICE_ID_CMD_648), 4 },
  412. { PCI_VDEVICE(CMD, PCI_DEVICE_ID_CMD_649), 5 },
  413. { },
  414. };
  415. static struct pci_driver cmd64x_pci_driver = {
  416. .name = DRV_NAME,
  417. .id_table = cmd64x,
  418. .probe = cmd64x_init_one,
  419. .remove = ata_pci_remove_one,
  420. #ifdef CONFIG_PM
  421. .suspend = ata_pci_device_suspend,
  422. .resume = cmd64x_reinit_one,
  423. #endif
  424. };
  425. static int __init cmd64x_init(void)
  426. {
  427. return pci_register_driver(&cmd64x_pci_driver);
  428. }
  429. static void __exit cmd64x_exit(void)
  430. {
  431. pci_unregister_driver(&cmd64x_pci_driver);
  432. }
  433. MODULE_AUTHOR("Alan Cox");
  434. MODULE_DESCRIPTION("low-level driver for CMD64x series PATA controllers");
  435. MODULE_LICENSE("GPL");
  436. MODULE_DEVICE_TABLE(pci, cmd64x);
  437. MODULE_VERSION(DRV_VERSION);
  438. module_init(cmd64x_init);
  439. module_exit(cmd64x_exit);