pata_qdi.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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.2.4"
  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. outsl(ap->ioaddr.data_addr, buf, buflen >> 2);
  112. else
  113. insl(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. outl(le32_to_cpu(pad), ap->ioaddr.data_addr);
  119. } else {
  120. pad = cpu_to_le32(inl(ap->ioaddr.data_addr));
  121. memcpy(buf + buflen - slop, &pad, slop);
  122. }
  123. }
  124. } else
  125. ata_pio_data_xfer(adev, buf, buflen, write_data);
  126. }
  127. static struct scsi_host_template qdi_sht = {
  128. .module = THIS_MODULE,
  129. .name = DRV_NAME,
  130. .ioctl = ata_scsi_ioctl,
  131. .queuecommand = ata_scsi_queuecmd,
  132. .can_queue = ATA_DEF_QUEUE,
  133. .this_id = ATA_SHT_THIS_ID,
  134. .sg_tablesize = LIBATA_MAX_PRD,
  135. .max_sectors = ATA_MAX_SECTORS,
  136. .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
  137. .emulated = ATA_SHT_EMULATED,
  138. .use_clustering = ATA_SHT_USE_CLUSTERING,
  139. .proc_name = DRV_NAME,
  140. .dma_boundary = ATA_DMA_BOUNDARY,
  141. .slave_configure = ata_scsi_slave_config,
  142. .bios_param = ata_std_bios_param,
  143. };
  144. static struct ata_port_operations qdi6500_port_ops = {
  145. .port_disable = ata_port_disable,
  146. .set_piomode = qdi6500_set_piomode,
  147. .tf_load = ata_tf_load,
  148. .tf_read = ata_tf_read,
  149. .check_status = ata_check_status,
  150. .exec_command = ata_exec_command,
  151. .dev_select = ata_std_dev_select,
  152. .freeze = ata_bmdma_freeze,
  153. .thaw = ata_bmdma_thaw,
  154. .error_handler = ata_bmdma_error_handler,
  155. .post_internal_cmd = ata_bmdma_post_internal_cmd,
  156. .qc_prep = ata_qc_prep,
  157. .qc_issue = qdi_qc_issue_prot,
  158. .data_xfer = qdi_data_xfer,
  159. .irq_handler = ata_interrupt,
  160. .irq_clear = ata_bmdma_irq_clear,
  161. .port_start = ata_port_start,
  162. .port_stop = ata_port_stop,
  163. .host_stop = ata_host_stop
  164. };
  165. static struct ata_port_operations qdi6580_port_ops = {
  166. .port_disable = ata_port_disable,
  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. .qc_prep = ata_qc_prep,
  178. .qc_issue = qdi_qc_issue_prot,
  179. .data_xfer = qdi_data_xfer,
  180. .irq_handler = ata_interrupt,
  181. .irq_clear = ata_bmdma_irq_clear,
  182. .port_start = ata_port_start,
  183. .port_stop = ata_port_stop,
  184. .host_stop = ata_host_stop
  185. };
  186. /**
  187. * qdi_init_one - attach a qdi interface
  188. * @type: Type to display
  189. * @io: I/O port start
  190. * @irq: interrupt line
  191. * @fast: True if on a > 33Mhz VLB
  192. *
  193. * Register an ISA bus IDE interface. Such interfaces are PIO and we
  194. * assume do not support IRQ sharing.
  195. */
  196. static __init int qdi_init_one(unsigned long port, int type, unsigned long io, int irq, int fast)
  197. {
  198. struct ata_probe_ent ae;
  199. struct platform_device *pdev;
  200. int ret;
  201. unsigned long ctrl = io + 0x206;
  202. /*
  203. * Fill in a probe structure first of all
  204. */
  205. pdev = platform_device_register_simple(DRV_NAME, nr_qdi_host, NULL, 0);
  206. if (pdev == NULL)
  207. return -ENOMEM;
  208. memset(&ae, 0, sizeof(struct ata_probe_ent));
  209. INIT_LIST_HEAD(&ae.node);
  210. ae.dev = &pdev->dev;
  211. if (type == 6580) {
  212. ae.port_ops = &qdi6580_port_ops;
  213. ae.pio_mask = 0x1F;
  214. } else {
  215. ae.port_ops = &qdi6500_port_ops;
  216. ae.pio_mask = 0x07; /* Actually PIO3 !IORDY is possible */
  217. }
  218. ae.sht = &qdi_sht;
  219. ae.n_ports = 1;
  220. ae.irq = irq;
  221. ae.irq_flags = 0;
  222. ae.port_flags = ATA_FLAG_SLAVE_POSS | ATA_FLAG_SRST;
  223. ae.port[0].cmd_addr = io;
  224. ae.port[0].altstatus_addr = ctrl;
  225. ae.port[0].ctl_addr = ctrl;
  226. ata_std_ports(&ae.port[0]);
  227. /*
  228. * Hook in a private data structure per channel
  229. */
  230. ae.private_data = &qdi_data[nr_qdi_host];
  231. qdi_data[nr_qdi_host].timing = port;
  232. qdi_data[nr_qdi_host].fast = fast;
  233. qdi_data[nr_qdi_host].platform_dev = pdev;
  234. printk(KERN_INFO DRV_NAME": qd%d at 0x%lx.\n", type, io);
  235. ret = ata_device_add(&ae);
  236. if (ret == 0) {
  237. platform_device_unregister(pdev);
  238. return -ENODEV;
  239. }
  240. qdi_host[nr_qdi_host++] = dev_get_drvdata(&pdev->dev);
  241. return 0;
  242. }
  243. /**
  244. * qdi_init - attach qdi interfaces
  245. *
  246. * Attach qdi IDE interfaces by scanning the ports it may occupy.
  247. */
  248. static __init int qdi_init(void)
  249. {
  250. unsigned long flags;
  251. static const unsigned long qd_port[2] = { 0x30, 0xB0 };
  252. static const unsigned long ide_port[2] = { 0x170, 0x1F0 };
  253. static const int ide_irq[2] = { 14, 15 };
  254. int ct = 0;
  255. int i;
  256. if (probe_qdi == 0)
  257. return -ENODEV;
  258. /*
  259. * Check each possible QD65xx base address
  260. */
  261. for (i = 0; i < 2; i++) {
  262. unsigned long port = qd_port[i];
  263. u8 r, res;
  264. if (request_region(port, 2, "pata_qdi")) {
  265. /* Check for a card */
  266. local_irq_save(flags);
  267. r = inb_p(port);
  268. outb_p(0x19, port);
  269. res = inb_p(port);
  270. outb_p(r, port);
  271. local_irq_restore(flags);
  272. /* Fail */
  273. if (res == 0x19)
  274. {
  275. release_region(port, 2);
  276. continue;
  277. }
  278. /* Passes the presence test */
  279. r = inb_p(port + 1); /* Check port agrees with port set */
  280. if ((r & 2) >> 1 != i) {
  281. release_region(port, 2);
  282. continue;
  283. }
  284. /* Check card type */
  285. if ((r & 0xF0) == 0xC0) {
  286. /* QD6500: single channel */
  287. if (r & 8) {
  288. /* Disabled ? */
  289. release_region(port, 2);
  290. continue;
  291. }
  292. ct += qdi_init_one(port, 6500, ide_port[r & 0x01], ide_irq[r & 0x01], r & 0x04);
  293. }
  294. if (((r & 0xF0) == 0xA0) || (r & 0xF0) == 0x50) {
  295. /* QD6580: dual channel */
  296. if (!request_region(port + 2 , 2, "pata_qdi"))
  297. {
  298. release_region(port, 2);
  299. continue;
  300. }
  301. res = inb(port + 3);
  302. if (res & 1) {
  303. /* Single channel mode */
  304. ct += qdi_init_one(port, 6580, ide_port[r & 0x01], ide_irq[r & 0x01], r & 0x04);
  305. } else {
  306. /* Dual channel mode */
  307. ct += qdi_init_one(port, 6580, 0x1F0, 14, r & 0x04);
  308. ct += qdi_init_one(port + 2, 6580, 0x170, 15, r & 0x04);
  309. }
  310. }
  311. }
  312. }
  313. if (ct != 0)
  314. return 0;
  315. return -ENODEV;
  316. }
  317. static __exit void qdi_exit(void)
  318. {
  319. int i;
  320. for (i = 0; i < nr_qdi_host; i++) {
  321. ata_host_remove(qdi_host[i]);
  322. /* Free the control resource. The 6580 dual channel has the resources
  323. * claimed as a pair of 2 byte resources so we need no special cases...
  324. */
  325. release_region(qdi_data[i].timing, 2);
  326. platform_device_unregister(qdi_data[i].platform_dev);
  327. }
  328. }
  329. MODULE_AUTHOR("Alan Cox");
  330. MODULE_DESCRIPTION("low-level driver for qdi ATA");
  331. MODULE_LICENSE("GPL");
  332. MODULE_VERSION(DRV_VERSION);
  333. module_init(qdi_init);
  334. module_exit(qdi_exit);
  335. module_param(probe_qdi, int, 0);