pata_qdi.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /*
  2. * pata_qdi.c - QDI VLB ATA controllers
  3. * (C) 2006 Red Hat
  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@ens-lyon.org>
  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.1"
  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 - clamp_val(t.active, 1, 8);
  54. recovery = 18 - clamp_val(t.recover, 3, 18);
  55. } else {
  56. active = 9 - clamp_val(t.active, 2, 9);
  57. recovery = 15 - clamp_val(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 - clamp_val(t.active, 1, 8);
  73. recovery = 18 - clamp_val(t.recover, 3, 18);
  74. } else {
  75. active = 9 - clamp_val(t.active, 2, 9);
  76. recovery = 15 - clamp_val(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 - 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(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_sff_qc_issue(qc);
  104. }
  105. static unsigned int qdi_data_xfer(struct ata_device *dev, unsigned char *buf,
  106. unsigned int buflen, int rw)
  107. {
  108. if (ata_id_has_dword_io(dev->id)) {
  109. struct ata_port *ap = dev->link->ap;
  110. int slop = buflen & 3;
  111. if (rw == READ)
  112. ioread32_rep(ap->ioaddr.data_addr, buf, buflen >> 2);
  113. else
  114. iowrite32_rep(ap->ioaddr.data_addr, buf, buflen >> 2);
  115. if (unlikely(slop)) {
  116. __le32 pad;
  117. if (rw == READ) {
  118. pad = cpu_to_le32(ioread32(ap->ioaddr.data_addr));
  119. memcpy(buf + buflen - slop, &pad, slop);
  120. } else {
  121. memcpy(&pad, buf + buflen - slop, slop);
  122. iowrite32(le32_to_cpu(pad), ap->ioaddr.data_addr);
  123. }
  124. buflen += 4 - slop;
  125. }
  126. } else
  127. buflen = ata_sff_data_xfer(dev, buf, buflen, rw);
  128. return buflen;
  129. }
  130. static struct scsi_host_template qdi_sht = {
  131. ATA_PIO_SHT(DRV_NAME),
  132. };
  133. static struct ata_port_operations qdi6500_port_ops = {
  134. .inherits = &ata_sff_port_ops,
  135. .qc_issue = qdi_qc_issue,
  136. .sff_data_xfer = qdi_data_xfer,
  137. .cable_detect = ata_cable_40wire,
  138. .set_piomode = qdi6500_set_piomode,
  139. };
  140. static struct ata_port_operations qdi6580_port_ops = {
  141. .inherits = &qdi6500_port_ops,
  142. .set_piomode = qdi6580_set_piomode,
  143. };
  144. /**
  145. * qdi_init_one - attach a qdi interface
  146. * @type: Type to display
  147. * @io: I/O port start
  148. * @irq: interrupt line
  149. * @fast: True if on a > 33Mhz VLB
  150. *
  151. * Register an ISA bus IDE interface. Such interfaces are PIO and we
  152. * assume do not support IRQ sharing.
  153. */
  154. static __init int qdi_init_one(unsigned long port, int type, unsigned long io, int irq, int fast)
  155. {
  156. unsigned long ctl = io + 0x206;
  157. struct platform_device *pdev;
  158. struct ata_host *host;
  159. struct ata_port *ap;
  160. void __iomem *io_addr, *ctl_addr;
  161. int ret;
  162. /*
  163. * Fill in a probe structure first of all
  164. */
  165. pdev = platform_device_register_simple(DRV_NAME, nr_qdi_host, NULL, 0);
  166. if (IS_ERR(pdev))
  167. return PTR_ERR(pdev);
  168. ret = -ENOMEM;
  169. io_addr = devm_ioport_map(&pdev->dev, io, 8);
  170. ctl_addr = devm_ioport_map(&pdev->dev, ctl, 1);
  171. if (!io_addr || !ctl_addr)
  172. goto fail;
  173. ret = -ENOMEM;
  174. host = ata_host_alloc(&pdev->dev, 1);
  175. if (!host)
  176. goto fail;
  177. ap = host->ports[0];
  178. if (type == 6580) {
  179. ap->ops = &qdi6580_port_ops;
  180. ap->pio_mask = ATA_PIO4;
  181. ap->flags |= ATA_FLAG_SLAVE_POSS;
  182. } else {
  183. ap->ops = &qdi6500_port_ops;
  184. ap->pio_mask = ATA_PIO2; /* Actually PIO3 !IORDY is possible */
  185. ap->flags = ATA_FLAG_SLAVE_POSS | ATA_FLAG_NO_IORDY;
  186. }
  187. ap->ioaddr.cmd_addr = io_addr;
  188. ap->ioaddr.altstatus_addr = ctl_addr;
  189. ap->ioaddr.ctl_addr = ctl_addr;
  190. ata_sff_std_ports(&ap->ioaddr);
  191. ata_port_desc(ap, "cmd %lx ctl %lx", io, ctl);
  192. /*
  193. * Hook in a private data structure per channel
  194. */
  195. ap->private_data = &qdi_data[nr_qdi_host];
  196. qdi_data[nr_qdi_host].timing = port;
  197. qdi_data[nr_qdi_host].fast = fast;
  198. qdi_data[nr_qdi_host].platform_dev = pdev;
  199. printk(KERN_INFO DRV_NAME": qd%d at 0x%lx.\n", type, io);
  200. /* activate */
  201. ret = ata_host_activate(host, irq, ata_sff_interrupt, 0, &qdi_sht);
  202. if (ret)
  203. goto fail;
  204. qdi_host[nr_qdi_host++] = dev_get_drvdata(&pdev->dev);
  205. return 0;
  206. fail:
  207. platform_device_unregister(pdev);
  208. return ret;
  209. }
  210. /**
  211. * qdi_init - attach qdi interfaces
  212. *
  213. * Attach qdi IDE interfaces by scanning the ports it may occupy.
  214. */
  215. static __init int qdi_init(void)
  216. {
  217. unsigned long flags;
  218. static const unsigned long qd_port[2] = { 0x30, 0xB0 };
  219. static const unsigned long ide_port[2] = { 0x170, 0x1F0 };
  220. static const int ide_irq[2] = { 14, 15 };
  221. int ct = 0;
  222. int i;
  223. if (probe_qdi == 0)
  224. return -ENODEV;
  225. /*
  226. * Check each possible QD65xx base address
  227. */
  228. for (i = 0; i < 2; i++) {
  229. unsigned long port = qd_port[i];
  230. u8 r, res;
  231. if (request_region(port, 2, "pata_qdi")) {
  232. /* Check for a card */
  233. local_irq_save(flags);
  234. r = inb_p(port);
  235. outb_p(0x19, port);
  236. res = inb_p(port);
  237. outb_p(r, port);
  238. local_irq_restore(flags);
  239. /* Fail */
  240. if (res == 0x19)
  241. {
  242. release_region(port, 2);
  243. continue;
  244. }
  245. /* Passes the presence test */
  246. r = inb_p(port + 1); /* Check port agrees with port set */
  247. if ((r & 2) >> 1 != i) {
  248. release_region(port, 2);
  249. continue;
  250. }
  251. /* Check card type */
  252. if ((r & 0xF0) == 0xC0) {
  253. /* QD6500: single channel */
  254. if (r & 8) {
  255. /* Disabled ? */
  256. release_region(port, 2);
  257. continue;
  258. }
  259. if (qdi_init_one(port, 6500, ide_port[r & 0x01], ide_irq[r & 0x01], r & 0x04) == 0)
  260. ct++;
  261. }
  262. if (((r & 0xF0) == 0xA0) || (r & 0xF0) == 0x50) {
  263. /* QD6580: dual channel */
  264. if (!request_region(port + 2 , 2, "pata_qdi"))
  265. {
  266. release_region(port, 2);
  267. continue;
  268. }
  269. res = inb(port + 3);
  270. if (res & 1) {
  271. /* Single channel mode */
  272. if (qdi_init_one(port, 6580, ide_port[r & 0x01], ide_irq[r & 0x01], r & 0x04) == 0)
  273. ct++;
  274. } else {
  275. /* Dual channel mode */
  276. if (qdi_init_one(port, 6580, 0x1F0, 14, r & 0x04) == 0)
  277. ct++;
  278. if (qdi_init_one(port + 2, 6580, 0x170, 15, r & 0x04) == 0)
  279. ct++;
  280. }
  281. }
  282. }
  283. }
  284. if (ct != 0)
  285. return 0;
  286. return -ENODEV;
  287. }
  288. static __exit void qdi_exit(void)
  289. {
  290. int i;
  291. for (i = 0; i < nr_qdi_host; i++) {
  292. ata_host_detach(qdi_host[i]);
  293. /* Free the control resource. The 6580 dual channel has the resources
  294. * claimed as a pair of 2 byte resources so we need no special cases...
  295. */
  296. release_region(qdi_data[i].timing, 2);
  297. platform_device_unregister(qdi_data[i].platform_dev);
  298. }
  299. }
  300. MODULE_AUTHOR("Alan Cox");
  301. MODULE_DESCRIPTION("low-level driver for qdi ATA");
  302. MODULE_LICENSE("GPL");
  303. MODULE_VERSION(DRV_VERSION);
  304. module_init(qdi_init);
  305. module_exit(qdi_exit);
  306. module_param(probe_qdi, int, 0);