pata_cmd640.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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_prot. Reloading the setup time is expensive
  96. so we 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_prot - 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_prot(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_qc_issue_prot(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_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. .module = THIS_MODULE,
  146. .name = DRV_NAME,
  147. .ioctl = ata_scsi_ioctl,
  148. .queuecommand = ata_scsi_queuecmd,
  149. .can_queue = ATA_DEF_QUEUE,
  150. .this_id = ATA_SHT_THIS_ID,
  151. .sg_tablesize = LIBATA_MAX_PRD,
  152. .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
  153. .emulated = ATA_SHT_EMULATED,
  154. .use_clustering = ATA_SHT_USE_CLUSTERING,
  155. .proc_name = DRV_NAME,
  156. .dma_boundary = ATA_DMA_BOUNDARY,
  157. .slave_configure = ata_scsi_slave_config,
  158. .slave_destroy = ata_scsi_slave_destroy,
  159. .bios_param = ata_std_bios_param,
  160. };
  161. static struct ata_port_operations cmd640_port_ops = {
  162. .port_disable = ata_port_disable,
  163. .set_piomode = cmd640_set_piomode,
  164. .mode_filter = ata_pci_default_filter,
  165. .tf_load = ata_tf_load,
  166. .tf_read = ata_tf_read,
  167. .check_status = ata_check_status,
  168. .exec_command = ata_exec_command,
  169. .dev_select = ata_std_dev_select,
  170. .freeze = ata_bmdma_freeze,
  171. .thaw = ata_bmdma_thaw,
  172. .error_handler = ata_bmdma_error_handler,
  173. .post_internal_cmd = ata_bmdma_post_internal_cmd,
  174. .cable_detect = ata_cable_40wire,
  175. .bmdma_setup = ata_bmdma_setup,
  176. .bmdma_start = ata_bmdma_start,
  177. .bmdma_stop = ata_bmdma_stop,
  178. .bmdma_status = ata_bmdma_status,
  179. .qc_prep = ata_qc_prep,
  180. .qc_issue = cmd640_qc_issue_prot,
  181. /* In theory this is not needed once we kill the prefetcher */
  182. .data_xfer = ata_data_xfer_noirq,
  183. .irq_handler = ata_interrupt,
  184. .irq_clear = ata_bmdma_irq_clear,
  185. .irq_on = ata_irq_on,
  186. .irq_ack = ata_irq_ack,
  187. .port_start = cmd640_port_start,
  188. };
  189. static void cmd640_hardware_init(struct pci_dev *pdev)
  190. {
  191. u8 r;
  192. u8 ctrl;
  193. /* CMD640 detected, commiserations */
  194. pci_write_config_byte(pdev, 0x5B, 0x00);
  195. /* Get version info */
  196. pci_read_config_byte(pdev, CFR, &r);
  197. /* PIO0 command cycles */
  198. pci_write_config_byte(pdev, CMDTIM, 0);
  199. /* 512 byte bursts (sector) */
  200. pci_write_config_byte(pdev, BRST, 0x40);
  201. /*
  202. * A reporter a long time ago
  203. * Had problems with the data fifo
  204. * So don't run the risk
  205. * Of putting crap on the disk
  206. * For its better just to go slow
  207. */
  208. /* Do channel 0 */
  209. pci_read_config_byte(pdev, CNTRL, &ctrl);
  210. pci_write_config_byte(pdev, CNTRL, ctrl | 0xC0);
  211. /* Ditto for channel 1 */
  212. pci_read_config_byte(pdev, ARTIM23, &ctrl);
  213. ctrl |= 0x0C;
  214. pci_write_config_byte(pdev, ARTIM23, ctrl);
  215. }
  216. static int cmd640_init_one(struct pci_dev *pdev, const struct pci_device_id *id)
  217. {
  218. static const struct ata_port_info info = {
  219. .sht = &cmd640_sht,
  220. .flags = ATA_FLAG_SLAVE_POSS | ATA_FLAG_SRST,
  221. .pio_mask = 0x1f,
  222. .port_ops = &cmd640_port_ops
  223. };
  224. const struct ata_port_info *ppi[] = { &info, NULL };
  225. cmd640_hardware_init(pdev);
  226. return ata_pci_init_one(pdev, ppi);
  227. }
  228. static int cmd640_reinit_one(struct pci_dev *pdev)
  229. {
  230. cmd640_hardware_init(pdev);
  231. #ifdef CONFIG_PM
  232. return ata_pci_device_resume(pdev);
  233. #else
  234. return 0;
  235. #endif
  236. }
  237. static const struct pci_device_id cmd640[] = {
  238. { PCI_VDEVICE(CMD, 0x640), 0 },
  239. { },
  240. };
  241. static struct pci_driver cmd640_pci_driver = {
  242. .name = DRV_NAME,
  243. .id_table = cmd640,
  244. .probe = cmd640_init_one,
  245. .remove = ata_pci_remove_one,
  246. #ifdef CONFIG_PM
  247. .suspend = ata_pci_device_suspend,
  248. #endif
  249. .resume = cmd640_reinit_one,
  250. };
  251. static int __init cmd640_init(void)
  252. {
  253. return pci_register_driver(&cmd640_pci_driver);
  254. }
  255. static void __exit cmd640_exit(void)
  256. {
  257. pci_unregister_driver(&cmd640_pci_driver);
  258. }
  259. MODULE_AUTHOR("Alan Cox");
  260. MODULE_DESCRIPTION("low-level driver for CMD640 PATA controllers");
  261. MODULE_LICENSE("GPL");
  262. MODULE_DEVICE_TABLE(pci, cmd640);
  263. MODULE_VERSION(DRV_VERSION);
  264. module_init(cmd640_init);
  265. module_exit(cmd640_exit);