pata_cmd64x.c 13 KB

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