pata_qdi.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. /*
  2. * pata_qdi.c - QDI VLB ATA controllers
  3. * (C) 2006 Red Hat <alan@redhat.com>
  4. *
  5. * This driver mostly exists as a proof of concept for non PCI devices under
  6. * libata. While the QDI6580 was 'neat' in 1993 it is no longer terribly
  7. * useful.
  8. *
  9. * Tuning code written from the documentation at
  10. * http://www.ryston.cz/petr/vlb/qd6500.html
  11. * http://www.ryston.cz/petr/vlb/qd6580.html
  12. *
  13. * Probe code based on drivers/ide/legacy/qd65xx.c
  14. * Rewritten from the work of Colten Edwards <pje120@cs.usask.ca> by
  15. * Samuel Thibault <samuel.thibault@fnac.net>
  16. */
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/pci.h>
  20. #include <linux/init.h>
  21. #include <linux/blkdev.h>
  22. #include <linux/delay.h>
  23. #include <scsi/scsi_host.h>
  24. #include <linux/libata.h>
  25. #include <linux/platform_device.h>
  26. #define DRV_NAME "pata_qdi"
  27. #define DRV_VERSION "0.3.0"
  28. #define NR_HOST 4 /* Two 6580s */
  29. struct qdi_data {
  30. unsigned long timing;
  31. u8 clock[2];
  32. u8 last;
  33. int fast;
  34. struct platform_device *platform_dev;
  35. };
  36. static struct ata_host *qdi_host[NR_HOST];
  37. static struct qdi_data qdi_data[NR_HOST];
  38. static int nr_qdi_host;
  39. #ifdef MODULE
  40. static int probe_qdi = 1;
  41. #else
  42. static int probe_qdi;
  43. #endif
  44. static void qdi6500_set_piomode(struct ata_port *ap, struct ata_device *adev)
  45. {
  46. struct ata_timing t;
  47. struct qdi_data *qdi = ap->host->private_data;
  48. int active, recovery;
  49. u8 timing;
  50. /* Get the timing data in cycles */
  51. ata_timing_compute(adev, adev->pio_mode, &t, 30303, 1000);
  52. if (qdi->fast) {
  53. active = 8 - FIT(t.active, 1, 8);
  54. recovery = 18 - FIT(t.recover, 3, 18);
  55. } else {
  56. active = 9 - FIT(t.active, 2, 9);
  57. recovery = 15 - FIT(t.recover, 0, 15);
  58. }
  59. timing = (recovery << 4) | active | 0x08;
  60. qdi->clock[adev->devno] = timing;
  61. outb(timing, qdi->timing);
  62. }
  63. static void qdi6580_set_piomode(struct ata_port *ap, struct ata_device *adev)
  64. {
  65. struct ata_timing t;
  66. struct qdi_data *qdi = ap->host->private_data;
  67. int active, recovery;
  68. u8 timing;
  69. /* Get the timing data in cycles */
  70. ata_timing_compute(adev, adev->pio_mode, &t, 30303, 1000);
  71. if (qdi->fast) {
  72. active = 8 - FIT(t.active, 1, 8);
  73. recovery = 18 - FIT(t.recover, 3, 18);
  74. } else {
  75. active = 9 - FIT(t.active, 2, 9);
  76. recovery = 15 - FIT(t.recover, 0, 15);
  77. }
  78. timing = (recovery << 4) | active | 0x08;
  79. qdi->clock[adev->devno] = timing;
  80. outb(timing, qdi->timing);
  81. /* Clear the FIFO */
  82. if (adev->class != ATA_DEV_ATA)
  83. outb(0x5F, (qdi->timing & 0xFFF0) + 3);
  84. }
  85. /**
  86. * qdi_qc_issue_prot - command issue
  87. * @qc: command pending
  88. *
  89. * Called when the libata layer is about to issue a command. We wrap
  90. * this interface so that we can load the correct ATA timings.
  91. */
  92. static unsigned int qdi_qc_issue_prot(struct ata_queued_cmd *qc)
  93. {
  94. struct ata_port *ap = qc->ap;
  95. struct ata_device *adev = qc->dev;
  96. struct qdi_data *qdi = ap->host->private_data;
  97. if (qdi->clock[adev->devno] != qdi->last) {
  98. if (adev->pio_mode) {
  99. qdi->last = qdi->clock[adev->devno];
  100. outb(qdi->clock[adev->devno], qdi->timing);
  101. }
  102. }
  103. return ata_qc_issue_prot(qc);
  104. }
  105. static void qdi_data_xfer(struct ata_device *adev, unsigned char *buf, unsigned int buflen, int write_data)
  106. {
  107. struct ata_port *ap = adev->ap;
  108. int slop = buflen & 3;
  109. if (ata_id_has_dword_io(adev->id)) {
  110. if (write_data)
  111. iowrite32_rep(ap->ioaddr.data_addr, buf, buflen >> 2);
  112. else
  113. ioread32_rep(ap->ioaddr.data_addr, buf, buflen >> 2);
  114. if (unlikely(slop)) {
  115. u32 pad;
  116. if (write_data) {
  117. memcpy(&pad, buf + buflen - slop, slop);
  118. pad = le32_to_cpu(pad);
  119. iowrite32(pad, ap->ioaddr.data_addr);
  120. } else {
  121. pad = ioread32(ap->ioaddr.data_addr);
  122. pad = cpu_to_le32(pad);
  123. memcpy(buf + buflen - slop, &pad, slop);
  124. }
  125. }
  126. } else
  127. ata_data_xfer(adev, buf, buflen, write_data);
  128. }
  129. static struct scsi_host_template qdi_sht = {
  130. .module = THIS_MODULE,
  131. .name = DRV_NAME,
  132. .ioctl = ata_scsi_ioctl,
  133. .queuecommand = ata_scsi_queuecmd,
  134. .can_queue = ATA_DEF_QUEUE,
  135. .this_id = ATA_SHT_THIS_ID,
  136. .sg_tablesize = LIBATA_MAX_PRD,
  137. .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
  138. .emulated = ATA_SHT_EMULATED,
  139. .use_clustering = ATA_SHT_USE_CLUSTERING,
  140. .proc_name = DRV_NAME,
  141. .dma_boundary = ATA_DMA_BOUNDARY,
  142. .slave_configure = ata_scsi_slave_config,
  143. .slave_destroy = ata_scsi_slave_destroy,
  144. .bios_param = ata_std_bios_param,
  145. };
  146. static struct ata_port_operations qdi6500_port_ops = {
  147. .port_disable = ata_port_disable,
  148. .set_piomode = qdi6500_set_piomode,
  149. .tf_load = ata_tf_load,
  150. .tf_read = ata_tf_read,
  151. .check_status = ata_check_status,
  152. .exec_command = ata_exec_command,
  153. .dev_select = ata_std_dev_select,
  154. .freeze = ata_bmdma_freeze,
  155. .thaw = ata_bmdma_thaw,
  156. .error_handler = ata_bmdma_error_handler,
  157. .post_internal_cmd = ata_bmdma_post_internal_cmd,
  158. .cable_detect = ata_cable_40wire,
  159. .qc_prep = ata_qc_prep,
  160. .qc_issue = qdi_qc_issue_prot,
  161. .data_xfer = qdi_data_xfer,
  162. .irq_clear = ata_bmdma_irq_clear,
  163. .irq_on = ata_irq_on,
  164. .irq_ack = ata_irq_ack,
  165. .port_start = ata_port_start,
  166. };
  167. static struct ata_port_operations qdi6580_port_ops = {
  168. .port_disable = ata_port_disable,
  169. .set_piomode = qdi6580_set_piomode,
  170. .tf_load = ata_tf_load,
  171. .tf_read = ata_tf_read,
  172. .check_status = ata_check_status,
  173. .exec_command = ata_exec_command,
  174. .dev_select = ata_std_dev_select,
  175. .freeze = ata_bmdma_freeze,
  176. .thaw = ata_bmdma_thaw,
  177. .error_handler = ata_bmdma_error_handler,
  178. .post_internal_cmd = ata_bmdma_post_internal_cmd,
  179. .cable_detect = ata_cable_40wire,
  180. .qc_prep = ata_qc_prep,
  181. .qc_issue = qdi_qc_issue_prot,
  182. .data_xfer = qdi_data_xfer,
  183. .irq_clear = ata_bmdma_irq_clear,
  184. .irq_on = ata_irq_on,
  185. .irq_ack = ata_irq_ack,
  186. .port_start = ata_port_start,
  187. };
  188. /**
  189. * qdi_init_one - attach a qdi interface
  190. * @type: Type to display
  191. * @io: I/O port start
  192. * @irq: interrupt line
  193. * @fast: True if on a > 33Mhz VLB
  194. *
  195. * Register an ISA bus IDE interface. Such interfaces are PIO and we
  196. * assume do not support IRQ sharing.
  197. */
  198. static __init int qdi_init_one(unsigned long port, int type, unsigned long io, int irq, int fast)
  199. {
  200. struct platform_device *pdev;
  201. struct ata_host *host;
  202. struct ata_port *ap;
  203. void __iomem *io_addr, *ctl_addr;
  204. int ret;
  205. /*
  206. * Fill in a probe structure first of all
  207. */
  208. pdev = platform_device_register_simple(DRV_NAME, nr_qdi_host, NULL, 0);
  209. if (IS_ERR(pdev))
  210. return PTR_ERR(pdev);
  211. ret = -ENOMEM;
  212. io_addr = devm_ioport_map(&pdev->dev, io, 8);
  213. ctl_addr = devm_ioport_map(&pdev->dev, io + 0x206, 1);
  214. if (!io_addr || !ctl_addr)
  215. goto fail;
  216. ret = -ENOMEM;
  217. host = ata_host_alloc(&pdev->dev, 1);
  218. if (!host)
  219. goto fail;
  220. ap = host->ports[0];
  221. if (type == 6580) {
  222. ap->ops = &qdi6580_port_ops;
  223. ap->pio_mask = 0x1F;
  224. ap->flags |= ATA_FLAG_SLAVE_POSS;
  225. } else {
  226. ap->ops = &qdi6500_port_ops;
  227. ap->pio_mask = 0x07; /* Actually PIO3 !IORDY is possible */
  228. ap->flags = ATA_FLAG_SLAVE_POSS | ATA_FLAG_NO_IORDY;
  229. }
  230. ap->ioaddr.cmd_addr = io_addr;
  231. ap->ioaddr.altstatus_addr = ctl_addr;
  232. ap->ioaddr.ctl_addr = ctl_addr;
  233. ata_std_ports(&ap->ioaddr);
  234. /*
  235. * Hook in a private data structure per channel
  236. */
  237. ap->private_data = &qdi_data[nr_qdi_host];
  238. qdi_data[nr_qdi_host].timing = port;
  239. qdi_data[nr_qdi_host].fast = fast;
  240. qdi_data[nr_qdi_host].platform_dev = pdev;
  241. printk(KERN_INFO DRV_NAME": qd%d at 0x%lx.\n", type, io);
  242. /* activate */
  243. ret = ata_host_activate(host, irq, ata_interrupt, 0, &qdi_sht);
  244. if (ret)
  245. goto fail;
  246. qdi_host[nr_qdi_host++] = dev_get_drvdata(&pdev->dev);
  247. return 0;
  248. fail:
  249. platform_device_unregister(pdev);
  250. return ret;
  251. }
  252. /**
  253. * qdi_init - attach qdi interfaces
  254. *
  255. * Attach qdi IDE interfaces by scanning the ports it may occupy.
  256. */
  257. static __init int qdi_init(void)
  258. {
  259. unsigned long flags;
  260. static const unsigned long qd_port[2] = { 0x30, 0xB0 };
  261. static const unsigned long ide_port[2] = { 0x170, 0x1F0 };
  262. static const int ide_irq[2] = { 14, 15 };
  263. int ct = 0;
  264. int i;
  265. if (probe_qdi == 0)
  266. return -ENODEV;
  267. /*
  268. * Check each possible QD65xx base address
  269. */
  270. for (i = 0; i < 2; i++) {
  271. unsigned long port = qd_port[i];
  272. u8 r, res;
  273. if (request_region(port, 2, "pata_qdi")) {
  274. /* Check for a card */
  275. local_irq_save(flags);
  276. r = inb_p(port);
  277. outb_p(0x19, port);
  278. res = inb_p(port);
  279. outb_p(r, port);
  280. local_irq_restore(flags);
  281. /* Fail */
  282. if (res == 0x19)
  283. {
  284. release_region(port, 2);
  285. continue;
  286. }
  287. /* Passes the presence test */
  288. r = inb_p(port + 1); /* Check port agrees with port set */
  289. if ((r & 2) >> 1 != i) {
  290. release_region(port, 2);
  291. continue;
  292. }
  293. /* Check card type */
  294. if ((r & 0xF0) == 0xC0) {
  295. /* QD6500: single channel */
  296. if (r & 8) {
  297. /* Disabled ? */
  298. release_region(port, 2);
  299. continue;
  300. }
  301. if (qdi_init_one(port, 6500, ide_port[r & 0x01], ide_irq[r & 0x01], r & 0x04) == 0)
  302. ct++;
  303. }
  304. if (((r & 0xF0) == 0xA0) || (r & 0xF0) == 0x50) {
  305. /* QD6580: dual channel */
  306. if (!request_region(port + 2 , 2, "pata_qdi"))
  307. {
  308. release_region(port, 2);
  309. continue;
  310. }
  311. res = inb(port + 3);
  312. if (res & 1) {
  313. /* Single channel mode */
  314. if (qdi_init_one(port, 6580, ide_port[r & 0x01], ide_irq[r & 0x01], r & 0x04) == 0)
  315. ct++;
  316. } else {
  317. /* Dual channel mode */
  318. if (qdi_init_one(port, 6580, 0x1F0, 14, r & 0x04) == 0)
  319. ct++;
  320. if (qdi_init_one(port + 2, 6580, 0x170, 15, r & 0x04) == 0)
  321. ct++;
  322. }
  323. }
  324. }
  325. }
  326. if (ct != 0)
  327. return 0;
  328. return -ENODEV;
  329. }
  330. static __exit void qdi_exit(void)
  331. {
  332. int i;
  333. for (i = 0; i < nr_qdi_host; i++) {
  334. ata_host_detach(qdi_host[i]);
  335. /* Free the control resource. The 6580 dual channel has the resources
  336. * claimed as a pair of 2 byte resources so we need no special cases...
  337. */
  338. release_region(qdi_data[i].timing, 2);
  339. platform_device_unregister(qdi_data[i].platform_dev);
  340. }
  341. }
  342. MODULE_AUTHOR("Alan Cox");
  343. MODULE_DESCRIPTION("low-level driver for qdi ATA");
  344. MODULE_LICENSE("GPL");
  345. MODULE_VERSION(DRV_VERSION);
  346. module_init(qdi_init);
  347. module_exit(qdi_exit);
  348. module_param(probe_qdi, int, 0);