pata_cmd640.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. * pata_cmd640.c - CMD640 PCI PATA for new ATA layer
  3. * (C) 2007 Red Hat Inc
  4. *
  5. * Based upon
  6. * linux/drivers/ide/pci/cmd640.c Version 1.02 Sep 01, 1996
  7. *
  8. * Copyright (C) 1995-1996 Linus Torvalds & authors (see driver)
  9. *
  10. * This drives only the PCI version of the controller. If you have a
  11. * VLB one then we have enough docs to support it but you can write
  12. * your own code.
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/pci.h>
  17. #include <linux/init.h>
  18. #include <linux/blkdev.h>
  19. #include <linux/delay.h>
  20. #include <scsi/scsi_host.h>
  21. #include <linux/libata.h>
  22. #define DRV_NAME "pata_cmd640"
  23. #define DRV_VERSION "0.0.5"
  24. struct cmd640_reg {
  25. int last;
  26. u8 reg58[ATA_MAX_DEVICES];
  27. };
  28. enum {
  29. CFR = 0x50,
  30. CNTRL = 0x51,
  31. CMDTIM = 0x52,
  32. ARTIM0 = 0x53,
  33. DRWTIM0 = 0x54,
  34. ARTIM23 = 0x57,
  35. DRWTIM23 = 0x58,
  36. BRST = 0x59
  37. };
  38. /**
  39. * cmd640_set_piomode - set initial PIO mode data
  40. * @ap: ATA port
  41. * @adev: ATA device
  42. *
  43. * Called to do the PIO mode setup.
  44. */
  45. static void cmd640_set_piomode(struct ata_port *ap, struct ata_device *adev)
  46. {
  47. struct cmd640_reg *timing = ap->private_data;
  48. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  49. struct ata_timing t;
  50. const unsigned long T = 1000000 / 33;
  51. const u8 setup_data[] = { 0x40, 0x40, 0x40, 0x80, 0x00 };
  52. u8 reg;
  53. int arttim = ARTIM0 + 2 * adev->devno;
  54. struct ata_device *pair = ata_dev_pair(adev);
  55. if (ata_timing_compute(adev, adev->pio_mode, &t, T, 0) < 0) {
  56. printk(KERN_ERR DRV_NAME ": mode computation failed.\n");
  57. return;
  58. }
  59. /* The second channel has shared timings and the setup timing is
  60. messy to switch to merge it for worst case */
  61. if (ap->port_no && pair) {
  62. struct ata_timing p;
  63. ata_timing_compute(pair, pair->pio_mode, &p, T, 1);
  64. ata_timing_merge(&p, &t, &t, ATA_TIMING_SETUP);
  65. }
  66. /* Make the timings fit */
  67. if (t.recover > 16) {
  68. t.active += t.recover - 16;
  69. t.recover = 16;
  70. }
  71. if (t.active > 16)
  72. t.active = 16;
  73. /* Now convert the clocks into values we can actually stuff into
  74. the chip */
  75. if (t.recover > 1)
  76. t.recover--; /* 640B only */
  77. else
  78. t.recover = 15;
  79. if (t.setup > 4)
  80. t.setup = 0xC0;
  81. else
  82. t.setup = setup_data[t.setup];
  83. if (ap->port_no == 0) {
  84. t.active &= 0x0F; /* 0 = 16 */
  85. /* Load setup timing */
  86. pci_read_config_byte(pdev, arttim, &reg);
  87. reg &= 0x3F;
  88. reg |= t.setup;
  89. pci_write_config_byte(pdev, arttim, reg);
  90. /* Load active/recovery */
  91. pci_write_config_byte(pdev, arttim + 1, (t.active << 4) | t.recover);
  92. } else {
  93. /* Save the shared timings for channel, they will be loaded
  94. by qc_issue. Reloading the setup time is expensive so we
  95. keep a merged one loaded */
  96. pci_read_config_byte(pdev, ARTIM23, &reg);
  97. reg &= 0x3F;
  98. reg |= t.setup;
  99. pci_write_config_byte(pdev, ARTIM23, reg);
  100. timing->reg58[adev->devno] = (t.active << 4) | t.recover;
  101. }
  102. }
  103. /**
  104. * cmd640_qc_issue - command preparation hook
  105. * @qc: Command to be issued
  106. *
  107. * Channel 1 has shared timings. We must reprogram the
  108. * clock each drive 2/3 switch we do.
  109. */
  110. static unsigned int cmd640_qc_issue(struct ata_queued_cmd *qc)
  111. {
  112. struct ata_port *ap = qc->ap;
  113. struct ata_device *adev = qc->dev;
  114. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  115. struct cmd640_reg *timing = ap->private_data;
  116. if (ap->port_no != 0 && adev->devno != timing->last) {
  117. pci_write_config_byte(pdev, DRWTIM23, timing->reg58[adev->devno]);
  118. timing->last = adev->devno;
  119. }
  120. return ata_sff_qc_issue(qc);
  121. }
  122. /**
  123. * cmd640_port_start - port setup
  124. * @ap: ATA port being set up
  125. *
  126. * The CMD640 needs to maintain private data structures so we
  127. * allocate space here.
  128. */
  129. static int cmd640_port_start(struct ata_port *ap)
  130. {
  131. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  132. struct cmd640_reg *timing;
  133. int ret = ata_sff_port_start(ap);
  134. if (ret < 0)
  135. return ret;
  136. timing = devm_kzalloc(&pdev->dev, sizeof(struct cmd640_reg), GFP_KERNEL);
  137. if (timing == NULL)
  138. return -ENOMEM;
  139. timing->last = -1; /* Force a load */
  140. ap->private_data = timing;
  141. return ret;
  142. }
  143. static struct scsi_host_template cmd640_sht = {
  144. ATA_BMDMA_SHT(DRV_NAME),
  145. };
  146. static struct ata_port_operations cmd640_port_ops = {
  147. .inherits = &ata_bmdma_port_ops,
  148. /* In theory xfer_noirq is not needed once we kill the prefetcher */
  149. .sff_data_xfer = ata_sff_data_xfer_noirq,
  150. .qc_issue = cmd640_qc_issue,
  151. .cable_detect = ata_cable_40wire,
  152. .set_piomode = cmd640_set_piomode,
  153. .port_start = cmd640_port_start,
  154. };
  155. static void cmd640_hardware_init(struct pci_dev *pdev)
  156. {
  157. u8 r;
  158. u8 ctrl;
  159. /* CMD640 detected, commiserations */
  160. pci_write_config_byte(pdev, 0x5B, 0x00);
  161. /* Get version info */
  162. pci_read_config_byte(pdev, CFR, &r);
  163. /* PIO0 command cycles */
  164. pci_write_config_byte(pdev, CMDTIM, 0);
  165. /* 512 byte bursts (sector) */
  166. pci_write_config_byte(pdev, BRST, 0x40);
  167. /*
  168. * A reporter a long time ago
  169. * Had problems with the data fifo
  170. * So don't run the risk
  171. * Of putting crap on the disk
  172. * For its better just to go slow
  173. */
  174. /* Do channel 0 */
  175. pci_read_config_byte(pdev, CNTRL, &ctrl);
  176. pci_write_config_byte(pdev, CNTRL, ctrl | 0xC0);
  177. /* Ditto for channel 1 */
  178. pci_read_config_byte(pdev, ARTIM23, &ctrl);
  179. ctrl |= 0x0C;
  180. pci_write_config_byte(pdev, ARTIM23, ctrl);
  181. }
  182. static int cmd640_init_one(struct pci_dev *pdev, const struct pci_device_id *id)
  183. {
  184. static const struct ata_port_info info = {
  185. .flags = ATA_FLAG_SLAVE_POSS,
  186. .pio_mask = 0x1f,
  187. .port_ops = &cmd640_port_ops
  188. };
  189. const struct ata_port_info *ppi[] = { &info, NULL };
  190. int rc;
  191. rc = pcim_enable_device(pdev);
  192. if (rc)
  193. return rc;
  194. cmd640_hardware_init(pdev);
  195. return ata_pci_sff_init_one(pdev, ppi, &cmd640_sht, NULL);
  196. }
  197. #ifdef CONFIG_PM
  198. static int cmd640_reinit_one(struct pci_dev *pdev)
  199. {
  200. struct ata_host *host = dev_get_drvdata(&pdev->dev);
  201. int rc;
  202. rc = ata_pci_device_do_resume(pdev);
  203. if (rc)
  204. return rc;
  205. cmd640_hardware_init(pdev);
  206. ata_host_resume(host);
  207. return 0;
  208. }
  209. #endif
  210. static const struct pci_device_id cmd640[] = {
  211. { PCI_VDEVICE(CMD, 0x640), 0 },
  212. { },
  213. };
  214. static struct pci_driver cmd640_pci_driver = {
  215. .name = DRV_NAME,
  216. .id_table = cmd640,
  217. .probe = cmd640_init_one,
  218. .remove = ata_pci_remove_one,
  219. #ifdef CONFIG_PM
  220. .suspend = ata_pci_device_suspend,
  221. .resume = cmd640_reinit_one,
  222. #endif
  223. };
  224. static int __init cmd640_init(void)
  225. {
  226. return pci_register_driver(&cmd640_pci_driver);
  227. }
  228. static void __exit cmd640_exit(void)
  229. {
  230. pci_unregister_driver(&cmd640_pci_driver);
  231. }
  232. MODULE_AUTHOR("Alan Cox");
  233. MODULE_DESCRIPTION("low-level driver for CMD640 PATA controllers");
  234. MODULE_LICENSE("GPL");
  235. MODULE_DEVICE_TABLE(pci, cmd640);
  236. MODULE_VERSION(DRV_VERSION);
  237. module_init(cmd640_init);
  238. module_exit(cmd640_exit);