pata_cmd640.c 6.5 KB

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