pata_qdi.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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.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 - 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 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. u32 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_data_xfer(dev, buf, buflen, rw);
  128. return buflen;
  129. }
  130. static struct scsi_host_template qdi_sht = {
  131. .module = THIS_MODULE,
  132. .name = DRV_NAME,
  133. .ioctl = ata_scsi_ioctl,
  134. .queuecommand = ata_scsi_queuecmd,
  135. .can_queue = ATA_DEF_QUEUE,
  136. .this_id = ATA_SHT_THIS_ID,
  137. .sg_tablesize = LIBATA_MAX_PRD,
  138. .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
  139. .emulated = ATA_SHT_EMULATED,
  140. .use_clustering = ATA_SHT_USE_CLUSTERING,
  141. .proc_name = DRV_NAME,
  142. .dma_boundary = ATA_DMA_BOUNDARY,
  143. .slave_configure = ata_scsi_slave_config,
  144. .slave_destroy = ata_scsi_slave_destroy,
  145. .bios_param = ata_std_bios_param,
  146. };
  147. static struct ata_port_operations qdi6500_port_ops = {
  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. .port_start = ata_sff_port_start,
  165. };
  166. static struct ata_port_operations qdi6580_port_ops = {
  167. .set_piomode = qdi6580_set_piomode,
  168. .tf_load = ata_tf_load,
  169. .tf_read = ata_tf_read,
  170. .check_status = ata_check_status,
  171. .exec_command = ata_exec_command,
  172. .dev_select = ata_std_dev_select,
  173. .freeze = ata_bmdma_freeze,
  174. .thaw = ata_bmdma_thaw,
  175. .error_handler = ata_bmdma_error_handler,
  176. .post_internal_cmd = ata_bmdma_post_internal_cmd,
  177. .cable_detect = ata_cable_40wire,
  178. .qc_prep = ata_qc_prep,
  179. .qc_issue = qdi_qc_issue_prot,
  180. .data_xfer = qdi_data_xfer,
  181. .irq_clear = ata_bmdma_irq_clear,
  182. .irq_on = ata_irq_on,
  183. .port_start = ata_sff_port_start,
  184. };
  185. /**
  186. * qdi_init_one - attach a qdi interface
  187. * @type: Type to display
  188. * @io: I/O port start
  189. * @irq: interrupt line
  190. * @fast: True if on a > 33Mhz VLB
  191. *
  192. * Register an ISA bus IDE interface. Such interfaces are PIO and we
  193. * assume do not support IRQ sharing.
  194. */
  195. static __init int qdi_init_one(unsigned long port, int type, unsigned long io, int irq, int fast)
  196. {
  197. unsigned long ctl = io + 0x206;
  198. struct platform_device *pdev;
  199. struct ata_host *host;
  200. struct ata_port *ap;
  201. void __iomem *io_addr, *ctl_addr;
  202. int ret;
  203. /*
  204. * Fill in a probe structure first of all
  205. */
  206. pdev = platform_device_register_simple(DRV_NAME, nr_qdi_host, NULL, 0);
  207. if (IS_ERR(pdev))
  208. return PTR_ERR(pdev);
  209. ret = -ENOMEM;
  210. io_addr = devm_ioport_map(&pdev->dev, io, 8);
  211. ctl_addr = devm_ioport_map(&pdev->dev, ctl, 1);
  212. if (!io_addr || !ctl_addr)
  213. goto fail;
  214. ret = -ENOMEM;
  215. host = ata_host_alloc(&pdev->dev, 1);
  216. if (!host)
  217. goto fail;
  218. ap = host->ports[0];
  219. if (type == 6580) {
  220. ap->ops = &qdi6580_port_ops;
  221. ap->pio_mask = 0x1F;
  222. ap->flags |= ATA_FLAG_SLAVE_POSS;
  223. } else {
  224. ap->ops = &qdi6500_port_ops;
  225. ap->pio_mask = 0x07; /* Actually PIO3 !IORDY is possible */
  226. ap->flags = ATA_FLAG_SLAVE_POSS | ATA_FLAG_NO_IORDY;
  227. }
  228. ap->ioaddr.cmd_addr = io_addr;
  229. ap->ioaddr.altstatus_addr = ctl_addr;
  230. ap->ioaddr.ctl_addr = ctl_addr;
  231. ata_std_ports(&ap->ioaddr);
  232. ata_port_desc(ap, "cmd %lx ctl %lx", io, ctl);
  233. /*
  234. * Hook in a private data structure per channel
  235. */
  236. ap->private_data = &qdi_data[nr_qdi_host];
  237. qdi_data[nr_qdi_host].timing = port;
  238. qdi_data[nr_qdi_host].fast = fast;
  239. qdi_data[nr_qdi_host].platform_dev = pdev;
  240. printk(KERN_INFO DRV_NAME": qd%d at 0x%lx.\n", type, io);
  241. /* activate */
  242. ret = ata_host_activate(host, irq, ata_interrupt, 0, &qdi_sht);
  243. if (ret)
  244. goto fail;
  245. qdi_host[nr_qdi_host++] = dev_get_drvdata(&pdev->dev);
  246. return 0;
  247. fail:
  248. platform_device_unregister(pdev);
  249. return ret;
  250. }
  251. /**
  252. * qdi_init - attach qdi interfaces
  253. *
  254. * Attach qdi IDE interfaces by scanning the ports it may occupy.
  255. */
  256. static __init int qdi_init(void)
  257. {
  258. unsigned long flags;
  259. static const unsigned long qd_port[2] = { 0x30, 0xB0 };
  260. static const unsigned long ide_port[2] = { 0x170, 0x1F0 };
  261. static const int ide_irq[2] = { 14, 15 };
  262. int ct = 0;
  263. int i;
  264. if (probe_qdi == 0)
  265. return -ENODEV;
  266. /*
  267. * Check each possible QD65xx base address
  268. */
  269. for (i = 0; i < 2; i++) {
  270. unsigned long port = qd_port[i];
  271. u8 r, res;
  272. if (request_region(port, 2, "pata_qdi")) {
  273. /* Check for a card */
  274. local_irq_save(flags);
  275. r = inb_p(port);
  276. outb_p(0x19, port);
  277. res = inb_p(port);
  278. outb_p(r, port);
  279. local_irq_restore(flags);
  280. /* Fail */
  281. if (res == 0x19)
  282. {
  283. release_region(port, 2);
  284. continue;
  285. }
  286. /* Passes the presence test */
  287. r = inb_p(port + 1); /* Check port agrees with port set */
  288. if ((r & 2) >> 1 != i) {
  289. release_region(port, 2);
  290. continue;
  291. }
  292. /* Check card type */
  293. if ((r & 0xF0) == 0xC0) {
  294. /* QD6500: single channel */
  295. if (r & 8) {
  296. /* Disabled ? */
  297. release_region(port, 2);
  298. continue;
  299. }
  300. if (qdi_init_one(port, 6500, ide_port[r & 0x01], ide_irq[r & 0x01], r & 0x04) == 0)
  301. ct++;
  302. }
  303. if (((r & 0xF0) == 0xA0) || (r & 0xF0) == 0x50) {
  304. /* QD6580: dual channel */
  305. if (!request_region(port + 2 , 2, "pata_qdi"))
  306. {
  307. release_region(port, 2);
  308. continue;
  309. }
  310. res = inb(port + 3);
  311. if (res & 1) {
  312. /* Single channel mode */
  313. if (qdi_init_one(port, 6580, ide_port[r & 0x01], ide_irq[r & 0x01], r & 0x04) == 0)
  314. ct++;
  315. } else {
  316. /* Dual channel mode */
  317. if (qdi_init_one(port, 6580, 0x1F0, 14, r & 0x04) == 0)
  318. ct++;
  319. if (qdi_init_one(port + 2, 6580, 0x170, 15, r & 0x04) == 0)
  320. ct++;
  321. }
  322. }
  323. }
  324. }
  325. if (ct != 0)
  326. return 0;
  327. return -ENODEV;
  328. }
  329. static __exit void qdi_exit(void)
  330. {
  331. int i;
  332. for (i = 0; i < nr_qdi_host; i++) {
  333. ata_host_detach(qdi_host[i]);
  334. /* Free the control resource. The 6580 dual channel has the resources
  335. * claimed as a pair of 2 byte resources so we need no special cases...
  336. */
  337. release_region(qdi_data[i].timing, 2);
  338. platform_device_unregister(qdi_data[i].platform_dev);
  339. }
  340. }
  341. MODULE_AUTHOR("Alan Cox");
  342. MODULE_DESCRIPTION("low-level driver for qdi ATA");
  343. MODULE_LICENSE("GPL");
  344. MODULE_VERSION(DRV_VERSION);
  345. module_init(qdi_init);
  346. module_exit(qdi_exit);
  347. module_param(probe_qdi, int, 0);