pata_cmd640.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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/blkdev.h>
  18. #include <linux/delay.h>
  19. #include <linux/gfp.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. timing = devm_kzalloc(&pdev->dev, sizeof(struct cmd640_reg), GFP_KERNEL);
  134. if (timing == NULL)
  135. return -ENOMEM;
  136. timing->last = -1; /* Force a load */
  137. ap->private_data = timing;
  138. return 0;
  139. }
  140. static bool cmd640_sff_irq_check(struct ata_port *ap)
  141. {
  142. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  143. int irq_reg = ap->port_no ? ARTIM23 : CFR;
  144. u8 irq_stat, irq_mask = ap->port_no ? 0x10 : 0x04;
  145. pci_read_config_byte(pdev, irq_reg, &irq_stat);
  146. return irq_stat & irq_mask;
  147. }
  148. static struct scsi_host_template cmd640_sht = {
  149. ATA_PIO_SHT(DRV_NAME),
  150. };
  151. static struct ata_port_operations cmd640_port_ops = {
  152. .inherits = &ata_sff_port_ops,
  153. /* In theory xfer_noirq is not needed once we kill the prefetcher */
  154. .sff_data_xfer = ata_sff_data_xfer_noirq,
  155. .sff_irq_check = cmd640_sff_irq_check,
  156. .qc_issue = cmd640_qc_issue,
  157. .cable_detect = ata_cable_40wire,
  158. .set_piomode = cmd640_set_piomode,
  159. .port_start = cmd640_port_start,
  160. };
  161. static void cmd640_hardware_init(struct pci_dev *pdev)
  162. {
  163. u8 ctrl;
  164. /* CMD640 detected, commiserations */
  165. pci_write_config_byte(pdev, 0x5B, 0x00);
  166. /* PIO0 command cycles */
  167. pci_write_config_byte(pdev, CMDTIM, 0);
  168. /* 512 byte bursts (sector) */
  169. pci_write_config_byte(pdev, BRST, 0x40);
  170. /*
  171. * A reporter a long time ago
  172. * Had problems with the data fifo
  173. * So don't run the risk
  174. * Of putting crap on the disk
  175. * For its better just to go slow
  176. */
  177. /* Do channel 0 */
  178. pci_read_config_byte(pdev, CNTRL, &ctrl);
  179. pci_write_config_byte(pdev, CNTRL, ctrl | 0xC0);
  180. /* Ditto for channel 1 */
  181. pci_read_config_byte(pdev, ARTIM23, &ctrl);
  182. ctrl |= 0x0C;
  183. pci_write_config_byte(pdev, ARTIM23, ctrl);
  184. }
  185. static int cmd640_init_one(struct pci_dev *pdev, const struct pci_device_id *id)
  186. {
  187. static const struct ata_port_info info = {
  188. .flags = ATA_FLAG_SLAVE_POSS,
  189. .pio_mask = ATA_PIO4,
  190. .port_ops = &cmd640_port_ops
  191. };
  192. const struct ata_port_info *ppi[] = { &info, NULL };
  193. int rc;
  194. rc = pcim_enable_device(pdev);
  195. if (rc)
  196. return rc;
  197. cmd640_hardware_init(pdev);
  198. return ata_pci_sff_init_one(pdev, ppi, &cmd640_sht, NULL, 0);
  199. }
  200. #ifdef CONFIG_PM_SLEEP
  201. static int cmd640_reinit_one(struct pci_dev *pdev)
  202. {
  203. struct ata_host *host = pci_get_drvdata(pdev);
  204. int rc;
  205. rc = ata_pci_device_do_resume(pdev);
  206. if (rc)
  207. return rc;
  208. cmd640_hardware_init(pdev);
  209. ata_host_resume(host);
  210. return 0;
  211. }
  212. #endif
  213. static const struct pci_device_id cmd640[] = {
  214. { PCI_VDEVICE(CMD, 0x640), 0 },
  215. { },
  216. };
  217. static struct pci_driver cmd640_pci_driver = {
  218. .name = DRV_NAME,
  219. .id_table = cmd640,
  220. .probe = cmd640_init_one,
  221. .remove = ata_pci_remove_one,
  222. #ifdef CONFIG_PM_SLEEP
  223. .suspend = ata_pci_device_suspend,
  224. .resume = cmd640_reinit_one,
  225. #endif
  226. };
  227. module_pci_driver(cmd640_pci_driver);
  228. MODULE_AUTHOR("Alan Cox");
  229. MODULE_DESCRIPTION("low-level driver for CMD640 PATA controllers");
  230. MODULE_LICENSE("GPL");
  231. MODULE_DEVICE_TABLE(pci, cmd640);
  232. MODULE_VERSION(DRV_VERSION);