pata_cmd64x.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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.1"
  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. 0x31, 0x21, 0x11, 0x25, 0x15, 0x05
  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. regD &= ~(0x20 << shift);
  186. regU &= ~(0x35 << shift);
  187. if (adev->dma_mode >= XFER_UDMA_0)
  188. regU |= udma_data[adev->dma_mode - XFER_UDMA_0] << shift;
  189. else
  190. regD |= mwdma_data[adev->dma_mode - XFER_MW_DMA_0] << shift;
  191. regD |= 0x20 << adev->devno;
  192. pci_write_config_byte(pdev, pciU, regU);
  193. pci_write_config_byte(pdev, pciD, regD);
  194. }
  195. /**
  196. * cmd648_dma_stop - DMA stop callback
  197. * @qc: Command in progress
  198. *
  199. * DMA has completed.
  200. */
  201. static void cmd648_bmdma_stop(struct ata_queued_cmd *qc)
  202. {
  203. struct ata_port *ap = qc->ap;
  204. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  205. u8 dma_intr;
  206. int dma_reg = ap->port_no ? ARTTIM23_INTR_CH1 : CFR_INTR_CH0;
  207. int dma_mask = ap->port_no ? ARTTIM2 : CFR;
  208. ata_bmdma_stop(qc);
  209. pci_read_config_byte(pdev, dma_reg, &dma_intr);
  210. pci_write_config_byte(pdev, dma_reg, dma_intr | dma_mask);
  211. }
  212. /**
  213. * cmd646r1_dma_stop - DMA stop callback
  214. * @qc: Command in progress
  215. *
  216. * Stub for now while investigating the r1 quirk in the old driver.
  217. */
  218. static void cmd646r1_bmdma_stop(struct ata_queued_cmd *qc)
  219. {
  220. ata_bmdma_stop(qc);
  221. }
  222. static struct scsi_host_template cmd64x_sht = {
  223. .module = THIS_MODULE,
  224. .name = DRV_NAME,
  225. .ioctl = ata_scsi_ioctl,
  226. .queuecommand = ata_scsi_queuecmd,
  227. .can_queue = ATA_DEF_QUEUE,
  228. .this_id = ATA_SHT_THIS_ID,
  229. .sg_tablesize = LIBATA_MAX_PRD,
  230. .max_sectors = ATA_MAX_SECTORS,
  231. .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
  232. .emulated = ATA_SHT_EMULATED,
  233. .use_clustering = ATA_SHT_USE_CLUSTERING,
  234. .proc_name = DRV_NAME,
  235. .dma_boundary = ATA_DMA_BOUNDARY,
  236. .slave_configure = ata_scsi_slave_config,
  237. .bios_param = ata_std_bios_param,
  238. };
  239. static struct ata_port_operations cmd64x_port_ops = {
  240. .port_disable = ata_port_disable,
  241. .set_piomode = cmd64x_set_piomode,
  242. .set_dmamode = cmd64x_set_dmamode,
  243. .mode_filter = ata_pci_default_filter,
  244. .tf_load = ata_tf_load,
  245. .tf_read = ata_tf_read,
  246. .check_status = ata_check_status,
  247. .exec_command = ata_exec_command,
  248. .dev_select = ata_std_dev_select,
  249. .freeze = ata_bmdma_freeze,
  250. .thaw = ata_bmdma_thaw,
  251. .error_handler = cmd64x_error_handler,
  252. .post_internal_cmd = ata_bmdma_post_internal_cmd,
  253. .bmdma_setup = ata_bmdma_setup,
  254. .bmdma_start = ata_bmdma_start,
  255. .bmdma_stop = ata_bmdma_stop,
  256. .bmdma_status = ata_bmdma_status,
  257. .qc_prep = ata_qc_prep,
  258. .qc_issue = ata_qc_issue_prot,
  259. .data_xfer = ata_pio_data_xfer,
  260. .irq_handler = ata_interrupt,
  261. .irq_clear = ata_bmdma_irq_clear,
  262. .port_start = ata_port_start,
  263. .port_stop = ata_port_stop,
  264. .host_stop = ata_host_stop
  265. };
  266. static struct ata_port_operations cmd646r1_port_ops = {
  267. .port_disable = ata_port_disable,
  268. .set_piomode = cmd64x_set_piomode,
  269. .set_dmamode = cmd64x_set_dmamode,
  270. .mode_filter = ata_pci_default_filter,
  271. .tf_load = ata_tf_load,
  272. .tf_read = ata_tf_read,
  273. .check_status = ata_check_status,
  274. .exec_command = ata_exec_command,
  275. .dev_select = ata_std_dev_select,
  276. .freeze = ata_bmdma_freeze,
  277. .thaw = ata_bmdma_thaw,
  278. .error_handler = cmd64x_error_handler,
  279. .post_internal_cmd = ata_bmdma_post_internal_cmd,
  280. .bmdma_setup = ata_bmdma_setup,
  281. .bmdma_start = ata_bmdma_start,
  282. .bmdma_stop = cmd646r1_bmdma_stop,
  283. .bmdma_status = ata_bmdma_status,
  284. .qc_prep = ata_qc_prep,
  285. .qc_issue = ata_qc_issue_prot,
  286. .data_xfer = ata_pio_data_xfer,
  287. .irq_handler = ata_interrupt,
  288. .irq_clear = ata_bmdma_irq_clear,
  289. .port_start = ata_port_start,
  290. .port_stop = ata_port_stop,
  291. .host_stop = ata_host_stop
  292. };
  293. static struct ata_port_operations cmd648_port_ops = {
  294. .port_disable = ata_port_disable,
  295. .set_piomode = cmd64x_set_piomode,
  296. .set_dmamode = cmd64x_set_dmamode,
  297. .mode_filter = ata_pci_default_filter,
  298. .tf_load = ata_tf_load,
  299. .tf_read = ata_tf_read,
  300. .check_status = ata_check_status,
  301. .exec_command = ata_exec_command,
  302. .dev_select = ata_std_dev_select,
  303. .freeze = ata_bmdma_freeze,
  304. .thaw = ata_bmdma_thaw,
  305. .error_handler = cmd648_error_handler,
  306. .post_internal_cmd = ata_bmdma_post_internal_cmd,
  307. .bmdma_setup = ata_bmdma_setup,
  308. .bmdma_start = ata_bmdma_start,
  309. .bmdma_stop = cmd648_bmdma_stop,
  310. .bmdma_status = ata_bmdma_status,
  311. .qc_prep = ata_qc_prep,
  312. .qc_issue = ata_qc_issue_prot,
  313. .data_xfer = ata_pio_data_xfer,
  314. .irq_handler = ata_interrupt,
  315. .irq_clear = ata_bmdma_irq_clear,
  316. .port_start = ata_port_start,
  317. .port_stop = ata_port_stop,
  318. .host_stop = ata_host_stop
  319. };
  320. static int cmd64x_init_one(struct pci_dev *pdev, const struct pci_device_id *id)
  321. {
  322. u32 class_rev;
  323. static struct ata_port_info cmd_info[6] = {
  324. { /* CMD 643 - no UDMA */
  325. .sht = &cmd64x_sht,
  326. .flags = ATA_FLAG_SLAVE_POSS | ATA_FLAG_SRST,
  327. .pio_mask = 0x1f,
  328. .mwdma_mask = 0x07,
  329. .port_ops = &cmd64x_port_ops
  330. },
  331. { /* CMD 646 with broken UDMA */
  332. .sht = &cmd64x_sht,
  333. .flags = ATA_FLAG_SLAVE_POSS | ATA_FLAG_SRST,
  334. .pio_mask = 0x1f,
  335. .mwdma_mask = 0x07,
  336. .port_ops = &cmd64x_port_ops
  337. },
  338. { /* CMD 646 with working UDMA */
  339. .sht = &cmd64x_sht,
  340. .flags = ATA_FLAG_SLAVE_POSS | ATA_FLAG_SRST,
  341. .pio_mask = 0x1f,
  342. .mwdma_mask = 0x07,
  343. .udma_mask = ATA_UDMA1,
  344. .port_ops = &cmd64x_port_ops
  345. },
  346. { /* CMD 646 rev 1 */
  347. .sht = &cmd64x_sht,
  348. .flags = ATA_FLAG_SLAVE_POSS | ATA_FLAG_SRST,
  349. .pio_mask = 0x1f,
  350. .mwdma_mask = 0x07,
  351. .port_ops = &cmd646r1_port_ops
  352. },
  353. { /* CMD 648 */
  354. .sht = &cmd64x_sht,
  355. .flags = ATA_FLAG_SLAVE_POSS | ATA_FLAG_SRST,
  356. .pio_mask = 0x1f,
  357. .mwdma_mask = 0x07,
  358. .udma_mask = ATA_UDMA2,
  359. .port_ops = &cmd648_port_ops
  360. },
  361. { /* CMD 649 */
  362. .sht = &cmd64x_sht,
  363. .flags = ATA_FLAG_SLAVE_POSS | ATA_FLAG_SRST,
  364. .pio_mask = 0x1f,
  365. .mwdma_mask = 0x07,
  366. .udma_mask = ATA_UDMA3,
  367. .port_ops = &cmd648_port_ops
  368. }
  369. };
  370. static struct ata_port_info *port_info[2], *info;
  371. u8 mrdmode;
  372. info = &cmd_info[id->driver_data];
  373. pci_read_config_dword(pdev, PCI_CLASS_REVISION, &class_rev);
  374. class_rev &= 0xFF;
  375. if (id->driver_data == 0) /* 643 */
  376. ata_pci_clear_simplex(pdev);
  377. if (pdev->device == PCI_DEVICE_ID_CMD_646) {
  378. /* Does UDMA work ? */
  379. if (class_rev > 4)
  380. info = &cmd_info[2];
  381. /* Early rev with other problems ? */
  382. else if (class_rev == 1)
  383. info = &cmd_info[3];
  384. }
  385. pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 64);
  386. pci_read_config_byte(pdev, MRDMODE, &mrdmode);
  387. mrdmode &= ~ 0x30; /* IRQ set up */
  388. mrdmode |= 0x02; /* Memory read line enable */
  389. pci_write_config_byte(pdev, MRDMODE, mrdmode);
  390. /* Force PIO 0 here.. */
  391. /* PPC specific fixup copied from old driver */
  392. #ifdef CONFIG_PPC
  393. pci_write_config_byte(pdev, UDIDETCR0, 0xF0);
  394. #endif
  395. port_info[0] = port_info[1] = info;
  396. return ata_pci_init_one(pdev, port_info, 2);
  397. }
  398. static struct pci_device_id cmd64x[] = {
  399. { PCI_VENDOR_ID_CMD, PCI_DEVICE_ID_CMD_643, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  400. { PCI_VENDOR_ID_CMD, PCI_DEVICE_ID_CMD_646, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 1},
  401. { PCI_VENDOR_ID_CMD, PCI_DEVICE_ID_CMD_648, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 4},
  402. { PCI_VENDOR_ID_CMD, PCI_DEVICE_ID_CMD_649, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 5},
  403. { 0, },
  404. };
  405. static struct pci_driver cmd64x_pci_driver = {
  406. .name = DRV_NAME,
  407. .id_table = cmd64x,
  408. .probe = cmd64x_init_one,
  409. .remove = ata_pci_remove_one
  410. };
  411. static int __init cmd64x_init(void)
  412. {
  413. return pci_register_driver(&cmd64x_pci_driver);
  414. }
  415. static void __exit cmd64x_exit(void)
  416. {
  417. pci_unregister_driver(&cmd64x_pci_driver);
  418. }
  419. MODULE_AUTHOR("Alan Cox");
  420. MODULE_DESCRIPTION("low-level driver for CMD64x series PATA controllers");
  421. MODULE_LICENSE("GPL");
  422. MODULE_DEVICE_TABLE(pci, cmd64x);
  423. MODULE_VERSION(DRV_VERSION);
  424. module_init(cmd64x_init);
  425. module_exit(cmd64x_exit);