blob: 60238d5748a769138e658069c2dc8362e6f63e4e [file] [log] [blame]
Jeff Garzik669a5db2006-08-29 18:12:40 -04001/*
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
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) {
63 active = 8 - FIT(t.active, 1, 8);
64 recovery = 18 - FIT(t.recover, 3, 18);
65 } else {
66 active = 9 - FIT(t.active, 2, 9);
67 recovery = 15 - FIT(t.recover, 0, 15);
68 }
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) {
87 active = 8 - FIT(t.active, 1, 8);
88 recovery = 18 - FIT(t.recover, 3, 18);
89 } else {
90 active = 9 - FIT(t.active, 2, 9);
91 recovery = 15 - FIT(t.recover, 0, 15);
92 }
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/**
105 * qdi_qc_issue_prot - command issue
106 * @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
112static unsigned int qdi_qc_issue_prot(struct ata_queued_cmd *qc)
113{
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 }
124 return ata_qc_issue_prot(qc);
125}
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)) {
Tejun Heo55dba312007-12-05 16:43:07 +0900140 u32 pad;
141 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 Heo55dba312007-12-05 16:43:07 +0900151 buflen = ata_data_xfer(dev, buf, buflen, rw);
152
153 return buflen;
Jeff Garzik669a5db2006-08-29 18:12:40 -0400154}
155
156static struct scsi_host_template qdi_sht = {
157 .module = THIS_MODULE,
158 .name = DRV_NAME,
159 .ioctl = ata_scsi_ioctl,
160 .queuecommand = ata_scsi_queuecmd,
161 .can_queue = ATA_DEF_QUEUE,
162 .this_id = ATA_SHT_THIS_ID,
163 .sg_tablesize = LIBATA_MAX_PRD,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400164 .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
165 .emulated = ATA_SHT_EMULATED,
166 .use_clustering = ATA_SHT_USE_CLUSTERING,
167 .proc_name = DRV_NAME,
168 .dma_boundary = ATA_DMA_BOUNDARY,
169 .slave_configure = ata_scsi_slave_config,
Tejun Heoafdfe892006-11-29 11:26:47 +0900170 .slave_destroy = ata_scsi_slave_destroy,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400171 .bios_param = ata_std_bios_param,
172};
173
174static struct ata_port_operations qdi6500_port_ops = {
Jeff Garzik669a5db2006-08-29 18:12:40 -0400175 .set_piomode = qdi6500_set_piomode,
176
177 .tf_load = ata_tf_load,
178 .tf_read = ata_tf_read,
179 .check_status = ata_check_status,
180 .exec_command = ata_exec_command,
181 .dev_select = ata_std_dev_select,
182
183 .freeze = ata_bmdma_freeze,
184 .thaw = ata_bmdma_thaw,
185 .error_handler = ata_bmdma_error_handler,
186 .post_internal_cmd = ata_bmdma_post_internal_cmd,
Alan Cox3be40d72007-03-26 21:43:40 -0800187 .cable_detect = ata_cable_40wire,
Jeff Garzik85cd7252006-08-31 00:03:49 -0400188
Jeff Garzik669a5db2006-08-29 18:12:40 -0400189 .qc_prep = ata_qc_prep,
190 .qc_issue = qdi_qc_issue_prot,
Jeff Garzikbda30282006-09-27 05:41:13 -0400191
Jeff Garzik669a5db2006-08-29 18:12:40 -0400192 .data_xfer = qdi_data_xfer,
193
Tejun Heo358f9a72008-03-25 12:22:47 +0900194 .irq_clear = ata_noop_irq_clear,
Akira Iguchi246ce3b2007-01-26 16:27:58 +0900195 .irq_on = ata_irq_on,
Jeff Garzik85cd7252006-08-31 00:03:49 -0400196
Alan Cox81ad1832007-08-22 22:55:41 +0100197 .port_start = ata_sff_port_start,
Jeff Garzik85cd7252006-08-31 00:03:49 -0400198};
Jeff Garzik669a5db2006-08-29 18:12:40 -0400199
200static struct ata_port_operations qdi6580_port_ops = {
Jeff Garzik669a5db2006-08-29 18:12:40 -0400201 .set_piomode = qdi6580_set_piomode,
202
203 .tf_load = ata_tf_load,
204 .tf_read = ata_tf_read,
205 .check_status = ata_check_status,
206 .exec_command = ata_exec_command,
207 .dev_select = ata_std_dev_select,
208
209 .freeze = ata_bmdma_freeze,
210 .thaw = ata_bmdma_thaw,
211 .error_handler = ata_bmdma_error_handler,
212 .post_internal_cmd = ata_bmdma_post_internal_cmd,
Alan Cox3be40d72007-03-26 21:43:40 -0800213 .cable_detect = ata_cable_40wire,
Jeff Garzik85cd7252006-08-31 00:03:49 -0400214
Jeff Garzik669a5db2006-08-29 18:12:40 -0400215 .qc_prep = ata_qc_prep,
216 .qc_issue = qdi_qc_issue_prot,
Jeff Garzikbda30282006-09-27 05:41:13 -0400217
Jeff Garzik669a5db2006-08-29 18:12:40 -0400218 .data_xfer = qdi_data_xfer,
219
Tejun Heo358f9a72008-03-25 12:22:47 +0900220 .irq_clear = ata_noop_irq_clear,
Akira Iguchi246ce3b2007-01-26 16:27:58 +0900221 .irq_on = ata_irq_on,
Jeff Garzik85cd7252006-08-31 00:03:49 -0400222
Alan Cox81ad1832007-08-22 22:55:41 +0100223 .port_start = ata_sff_port_start,
Jeff Garzik85cd7252006-08-31 00:03:49 -0400224};
Jeff Garzik669a5db2006-08-29 18:12:40 -0400225
226/**
227 * qdi_init_one - attach a qdi interface
228 * @type: Type to display
229 * @io: I/O port start
230 * @irq: interrupt line
231 * @fast: True if on a > 33Mhz VLB
232 *
233 * Register an ISA bus IDE interface. Such interfaces are PIO and we
234 * assume do not support IRQ sharing.
235 */
Jeff Garzik85cd7252006-08-31 00:03:49 -0400236
Jeff Garzik669a5db2006-08-29 18:12:40 -0400237static __init int qdi_init_one(unsigned long port, int type, unsigned long io, int irq, int fast)
238{
Tejun Heocbcdd872007-08-18 13:14:55 +0900239 unsigned long ctl = io + 0x206;
Jeff Garzik669a5db2006-08-29 18:12:40 -0400240 struct platform_device *pdev;
Tejun Heo5d728822007-04-17 23:44:08 +0900241 struct ata_host *host;
242 struct ata_port *ap;
Tejun Heo0d5ff562007-02-01 15:06:36 +0900243 void __iomem *io_addr, *ctl_addr;
Jeff Garzik669a5db2006-08-29 18:12:40 -0400244 int ret;
245
Jeff Garzik669a5db2006-08-29 18:12:40 -0400246 /*
247 * Fill in a probe structure first of all
248 */
249
250 pdev = platform_device_register_simple(DRV_NAME, nr_qdi_host, NULL, 0);
Akinobu Mita9825d732006-12-15 13:08:51 -0800251 if (IS_ERR(pdev))
252 return PTR_ERR(pdev);
Jeff Garzik85cd7252006-08-31 00:03:49 -0400253
Tejun Heo0d5ff562007-02-01 15:06:36 +0900254 ret = -ENOMEM;
255 io_addr = devm_ioport_map(&pdev->dev, io, 8);
Tejun Heocbcdd872007-08-18 13:14:55 +0900256 ctl_addr = devm_ioport_map(&pdev->dev, ctl, 1);
Tejun Heo0d5ff562007-02-01 15:06:36 +0900257 if (!io_addr || !ctl_addr)
258 goto fail;
259
Tejun Heo5d728822007-04-17 23:44:08 +0900260 ret = -ENOMEM;
261 host = ata_host_alloc(&pdev->dev, 1);
262 if (!host)
263 goto fail;
264 ap = host->ports[0];
Jeff Garzik85cd7252006-08-31 00:03:49 -0400265
Jeff Garzik669a5db2006-08-29 18:12:40 -0400266 if (type == 6580) {
Tejun Heo5d728822007-04-17 23:44:08 +0900267 ap->ops = &qdi6580_port_ops;
268 ap->pio_mask = 0x1F;
269 ap->flags |= ATA_FLAG_SLAVE_POSS;
Jeff Garzik669a5db2006-08-29 18:12:40 -0400270 } else {
Tejun Heo5d728822007-04-17 23:44:08 +0900271 ap->ops = &qdi6500_port_ops;
272 ap->pio_mask = 0x07; /* Actually PIO3 !IORDY is possible */
273 ap->flags = ATA_FLAG_SLAVE_POSS | ATA_FLAG_NO_IORDY;
Jeff Garzik669a5db2006-08-29 18:12:40 -0400274 }
Jeff Garzik85cd7252006-08-31 00:03:49 -0400275
Tejun Heo5d728822007-04-17 23:44:08 +0900276 ap->ioaddr.cmd_addr = io_addr;
277 ap->ioaddr.altstatus_addr = ctl_addr;
278 ap->ioaddr.ctl_addr = ctl_addr;
279 ata_std_ports(&ap->ioaddr);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400280
Tejun Heocbcdd872007-08-18 13:14:55 +0900281 ata_port_desc(ap, "cmd %lx ctl %lx", io, ctl);
282
Jeff Garzik669a5db2006-08-29 18:12:40 -0400283 /*
284 * Hook in a private data structure per channel
285 */
Tejun Heo5d728822007-04-17 23:44:08 +0900286 ap->private_data = &qdi_data[nr_qdi_host];
Jeff Garzik669a5db2006-08-29 18:12:40 -0400287
288 qdi_data[nr_qdi_host].timing = port;
289 qdi_data[nr_qdi_host].fast = fast;
290 qdi_data[nr_qdi_host].platform_dev = pdev;
291
292 printk(KERN_INFO DRV_NAME": qd%d at 0x%lx.\n", type, io);
Tejun Heo0d5ff562007-02-01 15:06:36 +0900293
Tejun Heo5d728822007-04-17 23:44:08 +0900294 /* activate */
295 ret = ata_host_activate(host, irq, ata_interrupt, 0, &qdi_sht);
296 if (ret)
Tejun Heo0d5ff562007-02-01 15:06:36 +0900297 goto fail;
Jeff Garzik85cd7252006-08-31 00:03:49 -0400298
Jeff Garzik669a5db2006-08-29 18:12:40 -0400299 qdi_host[nr_qdi_host++] = dev_get_drvdata(&pdev->dev);
Jeff Garzik85cd7252006-08-31 00:03:49 -0400300 return 0;
Tejun Heo0d5ff562007-02-01 15:06:36 +0900301
302 fail:
303 platform_device_unregister(pdev);
304 return ret;
Jeff Garzik669a5db2006-08-29 18:12:40 -0400305}
306
307/**
308 * qdi_init - attach qdi interfaces
309 *
310 * Attach qdi IDE interfaces by scanning the ports it may occupy.
311 */
Jeff Garzik85cd7252006-08-31 00:03:49 -0400312
Jeff Garzik669a5db2006-08-29 18:12:40 -0400313static __init int qdi_init(void)
314{
315 unsigned long flags;
316 static const unsigned long qd_port[2] = { 0x30, 0xB0 };
317 static const unsigned long ide_port[2] = { 0x170, 0x1F0 };
318 static const int ide_irq[2] = { 14, 15 };
Jeff Garzik85cd7252006-08-31 00:03:49 -0400319
Jeff Garzik669a5db2006-08-29 18:12:40 -0400320 int ct = 0;
321 int i;
Jeff Garzik85cd7252006-08-31 00:03:49 -0400322
Jeff Garzik669a5db2006-08-29 18:12:40 -0400323 if (probe_qdi == 0)
324 return -ENODEV;
Jeff Garzik85cd7252006-08-31 00:03:49 -0400325
Jeff Garzik669a5db2006-08-29 18:12:40 -0400326 /*
327 * Check each possible QD65xx base address
328 */
Jeff Garzik85cd7252006-08-31 00:03:49 -0400329
Jeff Garzik669a5db2006-08-29 18:12:40 -0400330 for (i = 0; i < 2; i++) {
331 unsigned long port = qd_port[i];
332 u8 r, res;
Jeff Garzik85cd7252006-08-31 00:03:49 -0400333
334
Jeff Garzik669a5db2006-08-29 18:12:40 -0400335 if (request_region(port, 2, "pata_qdi")) {
336 /* Check for a card */
337 local_irq_save(flags);
338 r = inb_p(port);
339 outb_p(0x19, port);
340 res = inb_p(port);
341 outb_p(r, port);
342 local_irq_restore(flags);
Jeff Garzik85cd7252006-08-31 00:03:49 -0400343
Jeff Garzik669a5db2006-08-29 18:12:40 -0400344 /* Fail */
345 if (res == 0x19)
346 {
347 release_region(port, 2);
348 continue;
349 }
Jeff Garzik85cd7252006-08-31 00:03:49 -0400350
Jeff Garzik669a5db2006-08-29 18:12:40 -0400351 /* Passes the presence test */
352 r = inb_p(port + 1); /* Check port agrees with port set */
353 if ((r & 2) >> 1 != i) {
354 release_region(port, 2);
355 continue;
356 }
357
Jeff Garzik85cd7252006-08-31 00:03:49 -0400358 /* Check card type */
Jeff Garzik669a5db2006-08-29 18:12:40 -0400359 if ((r & 0xF0) == 0xC0) {
360 /* QD6500: single channel */
361 if (r & 8) {
362 /* Disabled ? */
363 release_region(port, 2);
364 continue;
365 }
Alan Coxcc7c15e2007-03-02 14:56:35 +0000366 if (qdi_init_one(port, 6500, ide_port[r & 0x01], ide_irq[r & 0x01], r & 0x04) == 0)
367 ct++;
Jeff Garzik669a5db2006-08-29 18:12:40 -0400368 }
369 if (((r & 0xF0) == 0xA0) || (r & 0xF0) == 0x50) {
370 /* QD6580: dual channel */
371 if (!request_region(port + 2 , 2, "pata_qdi"))
372 {
373 release_region(port, 2);
374 continue;
375 }
376 res = inb(port + 3);
377 if (res & 1) {
378 /* Single channel mode */
Samuel Thibault6878cce2007-05-03 11:30:25 +0200379 if (qdi_init_one(port, 6580, ide_port[r & 0x01], ide_irq[r & 0x01], r & 0x04) == 0)
Alan Coxcc7c15e2007-03-02 14:56:35 +0000380 ct++;
Jeff Garzik669a5db2006-08-29 18:12:40 -0400381 } else {
382 /* Dual channel mode */
Alan Coxcc7c15e2007-03-02 14:56:35 +0000383 if (qdi_init_one(port, 6580, 0x1F0, 14, r & 0x04) == 0)
384 ct++;
385 if (qdi_init_one(port + 2, 6580, 0x170, 15, r & 0x04) == 0)
386 ct++;
Jeff Garzik669a5db2006-08-29 18:12:40 -0400387 }
388 }
389 }
390 }
391 if (ct != 0)
392 return 0;
393 return -ENODEV;
394}
395
396static __exit void qdi_exit(void)
397{
398 int i;
399
400 for (i = 0; i < nr_qdi_host; i++) {
Tejun Heo24dc5f32007-01-20 16:00:28 +0900401 ata_host_detach(qdi_host[i]);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400402 /* Free the control resource. The 6580 dual channel has the resources
403 * claimed as a pair of 2 byte resources so we need no special cases...
404 */
405 release_region(qdi_data[i].timing, 2);
406 platform_device_unregister(qdi_data[i].platform_dev);
Jeff Garzik85cd7252006-08-31 00:03:49 -0400407 }
Jeff Garzik669a5db2006-08-29 18:12:40 -0400408}
409
410MODULE_AUTHOR("Alan Cox");
411MODULE_DESCRIPTION("low-level driver for qdi ATA");
412MODULE_LICENSE("GPL");
413MODULE_VERSION(DRV_VERSION);
414
415module_init(qdi_init);
416module_exit(qdi_exit);
417
418module_param(probe_qdi, int, 0);
419