blob: 45879dc6fa41fca76235e081d214bb41dc736436 [file] [log] [blame]
Jeff Garzik669a5db2006-08-29 18:12:40 -04001/*
2 * pata_qdi.c - QDI VLB ATA controllers
Alan Coxab771632008-10-27 15:09:10 +00003 * (C) 2006 Red Hat
Jeff Garzik669a5db2006-08-29 18:12:40 -04004 *
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
Samuel Thibault51935352009-02-03 13:12:58 +010015 * Samuel Thibault <samuel.thibault@ens-lyon.org>
Jeff Garzik669a5db2006-08-29 18:12:40 -040016 */
17
18#include <linux/kernel.h>
19#include <linux/module.h>
20#include <linux/pci.h>
21#include <linux/init.h>
22#include <linux/blkdev.h>
23#include <linux/delay.h>
24#include <scsi/scsi_host.h>
25#include <linux/libata.h>
26#include <linux/platform_device.h>
27
28#define DRV_NAME "pata_qdi"
Jeff Garzik8bc3fc42007-05-21 20:26:38 -040029#define DRV_VERSION "0.3.1"
Jeff Garzik669a5db2006-08-29 18:12:40 -040030
31#define NR_HOST 4 /* Two 6580s */
32
33struct qdi_data {
34 unsigned long timing;
35 u8 clock[2];
36 u8 last;
37 int fast;
38 struct platform_device *platform_dev;
Jeff Garzik85cd7252006-08-31 00:03:49 -040039
Jeff Garzik669a5db2006-08-29 18:12:40 -040040};
41
42static struct ata_host *qdi_host[NR_HOST];
43static struct qdi_data qdi_data[NR_HOST];
44static int nr_qdi_host;
45
46#ifdef MODULE
47static int probe_qdi = 1;
48#else
49static int probe_qdi;
50#endif
51
52static void qdi6500_set_piomode(struct ata_port *ap, struct ata_device *adev)
53{
54 struct ata_timing t;
55 struct qdi_data *qdi = ap->host->private_data;
56 int active, recovery;
57 u8 timing;
58
59 /* Get the timing data in cycles */
60 ata_timing_compute(adev, adev->pio_mode, &t, 30303, 1000);
Jeff Garzik85cd7252006-08-31 00:03:49 -040061
Jeff Garzik669a5db2006-08-29 18:12:40 -040062 if (qdi->fast) {
Harvey Harrison07633b52008-05-14 16:17:00 -070063 active = 8 - clamp_val(t.active, 1, 8);
64 recovery = 18 - clamp_val(t.recover, 3, 18);
Jeff Garzik669a5db2006-08-29 18:12:40 -040065 } else {
Harvey Harrison07633b52008-05-14 16:17:00 -070066 active = 9 - clamp_val(t.active, 2, 9);
67 recovery = 15 - clamp_val(t.recover, 0, 15);
Jeff Garzik669a5db2006-08-29 18:12:40 -040068 }
69 timing = (recovery << 4) | active | 0x08;
Jeff Garzik85cd7252006-08-31 00:03:49 -040070
Jeff Garzik669a5db2006-08-29 18:12:40 -040071 qdi->clock[adev->devno] = timing;
72
Jeff Garzik85cd7252006-08-31 00:03:49 -040073 outb(timing, qdi->timing);
Jeff Garzik669a5db2006-08-29 18:12:40 -040074}
75
76static void qdi6580_set_piomode(struct ata_port *ap, struct ata_device *adev)
77{
78 struct ata_timing t;
79 struct qdi_data *qdi = ap->host->private_data;
80 int active, recovery;
81 u8 timing;
82
83 /* Get the timing data in cycles */
84 ata_timing_compute(adev, adev->pio_mode, &t, 30303, 1000);
Jeff Garzik85cd7252006-08-31 00:03:49 -040085
Jeff Garzik669a5db2006-08-29 18:12:40 -040086 if (qdi->fast) {
Harvey Harrison07633b52008-05-14 16:17:00 -070087 active = 8 - clamp_val(t.active, 1, 8);
88 recovery = 18 - clamp_val(t.recover, 3, 18);
Jeff Garzik669a5db2006-08-29 18:12:40 -040089 } else {
Harvey Harrison07633b52008-05-14 16:17:00 -070090 active = 9 - clamp_val(t.active, 2, 9);
91 recovery = 15 - clamp_val(t.recover, 0, 15);
Jeff Garzik669a5db2006-08-29 18:12:40 -040092 }
93 timing = (recovery << 4) | active | 0x08;
Jeff Garzik85cd7252006-08-31 00:03:49 -040094
Jeff Garzik669a5db2006-08-29 18:12:40 -040095 qdi->clock[adev->devno] = timing;
96
97 outb(timing, qdi->timing);
Jeff Garzik85cd7252006-08-31 00:03:49 -040098
Jeff Garzik669a5db2006-08-29 18:12:40 -040099 /* Clear the FIFO */
100 if (adev->class != ATA_DEV_ATA)
101 outb(0x5F, (qdi->timing & 0xFFF0) + 3);
102}
103
104/**
Tejun Heo9363c382008-04-07 22:47:16 +0900105 * qdi_qc_issue - command issue
Jeff Garzik669a5db2006-08-29 18:12:40 -0400106 * @qc: command pending
107 *
108 * Called when the libata layer is about to issue a command. We wrap
109 * this interface so that we can load the correct ATA timings.
110 */
111
Tejun Heo9363c382008-04-07 22:47:16 +0900112static unsigned int qdi_qc_issue(struct ata_queued_cmd *qc)
Jeff Garzik669a5db2006-08-29 18:12:40 -0400113{
114 struct ata_port *ap = qc->ap;
115 struct ata_device *adev = qc->dev;
116 struct qdi_data *qdi = ap->host->private_data;
117
118 if (qdi->clock[adev->devno] != qdi->last) {
119 if (adev->pio_mode) {
120 qdi->last = qdi->clock[adev->devno];
121 outb(qdi->clock[adev->devno], qdi->timing);
122 }
123 }
Tejun Heo9363c382008-04-07 22:47:16 +0900124 return ata_sff_qc_issue(qc);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400125}
126
Tejun Heo55dba312007-12-05 16:43:07 +0900127static unsigned int qdi_data_xfer(struct ata_device *dev, unsigned char *buf,
128 unsigned int buflen, int rw)
Jeff Garzik669a5db2006-08-29 18:12:40 -0400129{
Tejun Heo55dba312007-12-05 16:43:07 +0900130 if (ata_id_has_dword_io(dev->id)) {
131 struct ata_port *ap = dev->link->ap;
132 int slop = buflen & 3;
Jeff Garzik85cd7252006-08-31 00:03:49 -0400133
Tejun Heo55dba312007-12-05 16:43:07 +0900134 if (rw == READ)
Tejun Heo0d5ff562007-02-01 15:06:36 +0900135 ioread32_rep(ap->ioaddr.data_addr, buf, buflen >> 2);
Tejun Heo55dba312007-12-05 16:43:07 +0900136 else
137 iowrite32_rep(ap->ioaddr.data_addr, buf, buflen >> 2);
Jeff Garzik85cd7252006-08-31 00:03:49 -0400138
Jeff Garzik669a5db2006-08-29 18:12:40 -0400139 if (unlikely(slop)) {
Harvey Harrison6ad67402008-06-18 17:16:43 -0700140 __le32 pad;
Tejun Heo55dba312007-12-05 16:43:07 +0900141 if (rw == READ) {
Al Virob50e56d2008-01-12 14:16:14 +0000142 pad = cpu_to_le32(ioread32(ap->ioaddr.data_addr));
Jeff Garzik669a5db2006-08-29 18:12:40 -0400143 memcpy(buf + buflen - slop, &pad, slop);
Tejun Heo55dba312007-12-05 16:43:07 +0900144 } else {
145 memcpy(&pad, buf + buflen - slop, slop);
146 iowrite32(le32_to_cpu(pad), ap->ioaddr.data_addr);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400147 }
Tejun Heo55dba312007-12-05 16:43:07 +0900148 buflen += 4 - slop;
Jeff Garzik669a5db2006-08-29 18:12:40 -0400149 }
150 } else
Tejun Heo9363c382008-04-07 22:47:16 +0900151 buflen = ata_sff_data_xfer(dev, buf, buflen, rw);
Tejun Heo55dba312007-12-05 16:43:07 +0900152
153 return buflen;
Jeff Garzik669a5db2006-08-29 18:12:40 -0400154}
155
156static struct scsi_host_template qdi_sht = {
Tejun Heo68d1d072008-03-25 12:22:49 +0900157 ATA_PIO_SHT(DRV_NAME),
Jeff Garzik669a5db2006-08-29 18:12:40 -0400158};
159
160static struct ata_port_operations qdi6500_port_ops = {
Tejun Heo029cfd62008-03-25 12:22:49 +0900161 .inherits = &ata_sff_port_ops,
Tejun Heo9363c382008-04-07 22:47:16 +0900162 .qc_issue = qdi_qc_issue,
Tejun Heo5682ed32008-04-07 22:47:16 +0900163 .sff_data_xfer = qdi_data_xfer,
Tejun Heo029cfd62008-03-25 12:22:49 +0900164 .cable_detect = ata_cable_40wire,
165 .set_piomode = qdi6500_set_piomode,
Jeff Garzik85cd7252006-08-31 00:03:49 -0400166};
Jeff Garzik669a5db2006-08-29 18:12:40 -0400167
168static struct ata_port_operations qdi6580_port_ops = {
Tejun Heo029cfd62008-03-25 12:22:49 +0900169 .inherits = &qdi6500_port_ops,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400170 .set_piomode = qdi6580_set_piomode,
Jeff Garzik85cd7252006-08-31 00:03:49 -0400171};
Jeff Garzik669a5db2006-08-29 18:12:40 -0400172
173/**
174 * qdi_init_one - attach a qdi interface
175 * @type: Type to display
176 * @io: I/O port start
177 * @irq: interrupt line
178 * @fast: True if on a > 33Mhz VLB
179 *
180 * Register an ISA bus IDE interface. Such interfaces are PIO and we
181 * assume do not support IRQ sharing.
182 */
Jeff Garzik85cd7252006-08-31 00:03:49 -0400183
Jeff Garzik669a5db2006-08-29 18:12:40 -0400184static __init int qdi_init_one(unsigned long port, int type, unsigned long io, int irq, int fast)
185{
Tejun Heocbcdd872007-08-18 13:14:55 +0900186 unsigned long ctl = io + 0x206;
Jeff Garzik669a5db2006-08-29 18:12:40 -0400187 struct platform_device *pdev;
Tejun Heo5d7288242007-04-17 23:44:08 +0900188 struct ata_host *host;
189 struct ata_port *ap;
Tejun Heo0d5ff562007-02-01 15:06:36 +0900190 void __iomem *io_addr, *ctl_addr;
Jeff Garzik669a5db2006-08-29 18:12:40 -0400191 int ret;
192
Jeff Garzik669a5db2006-08-29 18:12:40 -0400193 /*
194 * Fill in a probe structure first of all
195 */
196
197 pdev = platform_device_register_simple(DRV_NAME, nr_qdi_host, NULL, 0);
Akinobu Mita9825d732006-12-15 13:08:51 -0800198 if (IS_ERR(pdev))
199 return PTR_ERR(pdev);
Jeff Garzik85cd7252006-08-31 00:03:49 -0400200
Tejun Heo0d5ff562007-02-01 15:06:36 +0900201 ret = -ENOMEM;
202 io_addr = devm_ioport_map(&pdev->dev, io, 8);
Tejun Heocbcdd872007-08-18 13:14:55 +0900203 ctl_addr = devm_ioport_map(&pdev->dev, ctl, 1);
Tejun Heo0d5ff562007-02-01 15:06:36 +0900204 if (!io_addr || !ctl_addr)
205 goto fail;
206
Tejun Heo5d7288242007-04-17 23:44:08 +0900207 ret = -ENOMEM;
208 host = ata_host_alloc(&pdev->dev, 1);
209 if (!host)
210 goto fail;
211 ap = host->ports[0];
Jeff Garzik85cd7252006-08-31 00:03:49 -0400212
Jeff Garzik669a5db2006-08-29 18:12:40 -0400213 if (type == 6580) {
Tejun Heo5d7288242007-04-17 23:44:08 +0900214 ap->ops = &qdi6580_port_ops;
Erik Inge Bolsø14bdef92009-03-14 21:38:24 +0100215 ap->pio_mask = ATA_PIO4;
Tejun Heo5d7288242007-04-17 23:44:08 +0900216 ap->flags |= ATA_FLAG_SLAVE_POSS;
Jeff Garzik669a5db2006-08-29 18:12:40 -0400217 } else {
Tejun Heo5d7288242007-04-17 23:44:08 +0900218 ap->ops = &qdi6500_port_ops;
Erik Inge Bolsø14bdef92009-03-14 21:38:24 +0100219 ap->pio_mask = ATA_PIO2; /* Actually PIO3 !IORDY is possible */
Tejun Heo5d7288242007-04-17 23:44:08 +0900220 ap->flags = ATA_FLAG_SLAVE_POSS | ATA_FLAG_NO_IORDY;
Jeff Garzik669a5db2006-08-29 18:12:40 -0400221 }
Jeff Garzik85cd7252006-08-31 00:03:49 -0400222
Tejun Heo5d7288242007-04-17 23:44:08 +0900223 ap->ioaddr.cmd_addr = io_addr;
224 ap->ioaddr.altstatus_addr = ctl_addr;
225 ap->ioaddr.ctl_addr = ctl_addr;
Tejun Heo9363c382008-04-07 22:47:16 +0900226 ata_sff_std_ports(&ap->ioaddr);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400227
Tejun Heocbcdd872007-08-18 13:14:55 +0900228 ata_port_desc(ap, "cmd %lx ctl %lx", io, ctl);
229
Jeff Garzik669a5db2006-08-29 18:12:40 -0400230 /*
231 * Hook in a private data structure per channel
232 */
Tejun Heo5d7288242007-04-17 23:44:08 +0900233 ap->private_data = &qdi_data[nr_qdi_host];
Jeff Garzik669a5db2006-08-29 18:12:40 -0400234
235 qdi_data[nr_qdi_host].timing = port;
236 qdi_data[nr_qdi_host].fast = fast;
237 qdi_data[nr_qdi_host].platform_dev = pdev;
238
239 printk(KERN_INFO DRV_NAME": qd%d at 0x%lx.\n", type, io);
Tejun Heo0d5ff562007-02-01 15:06:36 +0900240
Tejun Heo5d7288242007-04-17 23:44:08 +0900241 /* activate */
Tejun Heo9363c382008-04-07 22:47:16 +0900242 ret = ata_host_activate(host, irq, ata_sff_interrupt, 0, &qdi_sht);
Tejun Heo5d7288242007-04-17 23:44:08 +0900243 if (ret)
Tejun Heo0d5ff562007-02-01 15:06:36 +0900244 goto fail;
Jeff Garzik85cd7252006-08-31 00:03:49 -0400245
Jeff Garzik669a5db2006-08-29 18:12:40 -0400246 qdi_host[nr_qdi_host++] = dev_get_drvdata(&pdev->dev);
Jeff Garzik85cd7252006-08-31 00:03:49 -0400247 return 0;
Tejun Heo0d5ff562007-02-01 15:06:36 +0900248
249 fail:
250 platform_device_unregister(pdev);
251 return ret;
Jeff Garzik669a5db2006-08-29 18:12:40 -0400252}
253
254/**
255 * qdi_init - attach qdi interfaces
256 *
257 * Attach qdi IDE interfaces by scanning the ports it may occupy.
258 */
Jeff Garzik85cd7252006-08-31 00:03:49 -0400259
Jeff Garzik669a5db2006-08-29 18:12:40 -0400260static __init int qdi_init(void)
261{
262 unsigned long flags;
263 static const unsigned long qd_port[2] = { 0x30, 0xB0 };
264 static const unsigned long ide_port[2] = { 0x170, 0x1F0 };
265 static const int ide_irq[2] = { 14, 15 };
Jeff Garzik85cd7252006-08-31 00:03:49 -0400266
Jeff Garzik669a5db2006-08-29 18:12:40 -0400267 int ct = 0;
268 int i;
Jeff Garzik85cd7252006-08-31 00:03:49 -0400269
Jeff Garzik669a5db2006-08-29 18:12:40 -0400270 if (probe_qdi == 0)
271 return -ENODEV;
Jeff Garzik85cd7252006-08-31 00:03:49 -0400272
Jeff Garzik669a5db2006-08-29 18:12:40 -0400273 /*
274 * Check each possible QD65xx base address
275 */
Jeff Garzik85cd7252006-08-31 00:03:49 -0400276
Jeff Garzik669a5db2006-08-29 18:12:40 -0400277 for (i = 0; i < 2; i++) {
278 unsigned long port = qd_port[i];
279 u8 r, res;
Jeff Garzik85cd7252006-08-31 00:03:49 -0400280
281
Jeff Garzik669a5db2006-08-29 18:12:40 -0400282 if (request_region(port, 2, "pata_qdi")) {
283 /* Check for a card */
284 local_irq_save(flags);
285 r = inb_p(port);
286 outb_p(0x19, port);
287 res = inb_p(port);
288 outb_p(r, port);
289 local_irq_restore(flags);
Jeff Garzik85cd7252006-08-31 00:03:49 -0400290
Jeff Garzik669a5db2006-08-29 18:12:40 -0400291 /* Fail */
292 if (res == 0x19)
293 {
294 release_region(port, 2);
295 continue;
296 }
Jeff Garzik85cd7252006-08-31 00:03:49 -0400297
Jeff Garzik669a5db2006-08-29 18:12:40 -0400298 /* Passes the presence test */
299 r = inb_p(port + 1); /* Check port agrees with port set */
300 if ((r & 2) >> 1 != i) {
301 release_region(port, 2);
302 continue;
303 }
304
Jeff Garzik85cd7252006-08-31 00:03:49 -0400305 /* Check card type */
Jeff Garzik669a5db2006-08-29 18:12:40 -0400306 if ((r & 0xF0) == 0xC0) {
307 /* QD6500: single channel */
308 if (r & 8) {
309 /* Disabled ? */
310 release_region(port, 2);
311 continue;
312 }
Alan Coxcc7c15e2007-03-02 14:56:35 +0000313 if (qdi_init_one(port, 6500, ide_port[r & 0x01], ide_irq[r & 0x01], r & 0x04) == 0)
314 ct++;
Jeff Garzik669a5db2006-08-29 18:12:40 -0400315 }
316 if (((r & 0xF0) == 0xA0) || (r & 0xF0) == 0x50) {
317 /* QD6580: dual channel */
318 if (!request_region(port + 2 , 2, "pata_qdi"))
319 {
320 release_region(port, 2);
321 continue;
322 }
323 res = inb(port + 3);
324 if (res & 1) {
325 /* Single channel mode */
Samuel Thibault6878cce2007-05-03 11:30:25 +0200326 if (qdi_init_one(port, 6580, ide_port[r & 0x01], ide_irq[r & 0x01], r & 0x04) == 0)
Alan Coxcc7c15e2007-03-02 14:56:35 +0000327 ct++;
Jeff Garzik669a5db2006-08-29 18:12:40 -0400328 } else {
329 /* Dual channel mode */
Alan Coxcc7c15e2007-03-02 14:56:35 +0000330 if (qdi_init_one(port, 6580, 0x1F0, 14, r & 0x04) == 0)
331 ct++;
332 if (qdi_init_one(port + 2, 6580, 0x170, 15, r & 0x04) == 0)
333 ct++;
Jeff Garzik669a5db2006-08-29 18:12:40 -0400334 }
335 }
336 }
337 }
338 if (ct != 0)
339 return 0;
340 return -ENODEV;
341}
342
343static __exit void qdi_exit(void)
344{
345 int i;
346
347 for (i = 0; i < nr_qdi_host; i++) {
Tejun Heo24dc5f32007-01-20 16:00:28 +0900348 ata_host_detach(qdi_host[i]);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400349 /* Free the control resource. The 6580 dual channel has the resources
350 * claimed as a pair of 2 byte resources so we need no special cases...
351 */
352 release_region(qdi_data[i].timing, 2);
353 platform_device_unregister(qdi_data[i].platform_dev);
Jeff Garzik85cd7252006-08-31 00:03:49 -0400354 }
Jeff Garzik669a5db2006-08-29 18:12:40 -0400355}
356
357MODULE_AUTHOR("Alan Cox");
358MODULE_DESCRIPTION("low-level driver for qdi ATA");
359MODULE_LICENSE("GPL");
360MODULE_VERSION(DRV_VERSION);
361
362module_init(qdi_init);
363module_exit(qdi_exit);
364
365module_param(probe_qdi, int, 0);
366