blob: 314938dea1fcbd9b98f67ed61531608befb580ed [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"
29#define DRV_VERSION "0.2.4"
30
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
127static void qdi_data_xfer(struct ata_device *adev, unsigned char *buf, unsigned int buflen, int write_data)
128{
129 struct ata_port *ap = adev->ap;
130 int slop = buflen & 3;
Jeff Garzik85cd7252006-08-31 00:03:49 -0400131
Jeff Garzik669a5db2006-08-29 18:12:40 -0400132 if (ata_id_has_dword_io(adev->id)) {
133 if (write_data)
134 outsl(ap->ioaddr.data_addr, buf, buflen >> 2);
135 else
136 insl(ap->ioaddr.data_addr, buf, buflen >> 2);
Jeff Garzik85cd7252006-08-31 00:03:49 -0400137
Jeff Garzik669a5db2006-08-29 18:12:40 -0400138 if (unlikely(slop)) {
139 u32 pad;
140 if (write_data) {
141 memcpy(&pad, buf + buflen - slop, slop);
142 outl(le32_to_cpu(pad), ap->ioaddr.data_addr);
143 } else {
Peter Korsgaard53e36ad2006-10-09 09:53:09 +0200144 pad = cpu_to_le32(inl(ap->ioaddr.data_addr));
Jeff Garzik669a5db2006-08-29 18:12:40 -0400145 memcpy(buf + buflen - slop, &pad, slop);
146 }
147 }
148 } else
149 ata_pio_data_xfer(adev, buf, buflen, write_data);
150}
151
152static struct scsi_host_template qdi_sht = {
153 .module = THIS_MODULE,
154 .name = DRV_NAME,
155 .ioctl = ata_scsi_ioctl,
156 .queuecommand = ata_scsi_queuecmd,
157 .can_queue = ATA_DEF_QUEUE,
158 .this_id = ATA_SHT_THIS_ID,
159 .sg_tablesize = LIBATA_MAX_PRD,
160 .max_sectors = ATA_MAX_SECTORS,
161 .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
162 .emulated = ATA_SHT_EMULATED,
163 .use_clustering = ATA_SHT_USE_CLUSTERING,
164 .proc_name = DRV_NAME,
165 .dma_boundary = ATA_DMA_BOUNDARY,
166 .slave_configure = ata_scsi_slave_config,
Tejun Heoafdfe892006-11-29 11:26:47 +0900167 .slave_destroy = ata_scsi_slave_destroy,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400168 .bios_param = ata_std_bios_param,
169};
170
171static struct ata_port_operations qdi6500_port_ops = {
172 .port_disable = ata_port_disable,
173 .set_piomode = qdi6500_set_piomode,
174
175 .tf_load = ata_tf_load,
176 .tf_read = ata_tf_read,
177 .check_status = ata_check_status,
178 .exec_command = ata_exec_command,
179 .dev_select = ata_std_dev_select,
180
181 .freeze = ata_bmdma_freeze,
182 .thaw = ata_bmdma_thaw,
183 .error_handler = ata_bmdma_error_handler,
184 .post_internal_cmd = ata_bmdma_post_internal_cmd,
Jeff Garzik85cd7252006-08-31 00:03:49 -0400185
Jeff Garzik669a5db2006-08-29 18:12:40 -0400186 .qc_prep = ata_qc_prep,
187 .qc_issue = qdi_qc_issue_prot,
Jeff Garzikbda30282006-09-27 05:41:13 -0400188
Jeff Garzik669a5db2006-08-29 18:12:40 -0400189 .data_xfer = qdi_data_xfer,
190
191 .irq_handler = ata_interrupt,
192 .irq_clear = ata_bmdma_irq_clear,
Jeff Garzik85cd7252006-08-31 00:03:49 -0400193
Jeff Garzik669a5db2006-08-29 18:12:40 -0400194 .port_start = ata_port_start,
195 .port_stop = ata_port_stop,
196 .host_stop = ata_host_stop
Jeff Garzik85cd7252006-08-31 00:03:49 -0400197};
Jeff Garzik669a5db2006-08-29 18:12:40 -0400198
199static struct ata_port_operations qdi6580_port_ops = {
200 .port_disable = ata_port_disable,
201 .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,
Jeff Garzik85cd7252006-08-31 00:03:49 -0400213
Jeff Garzik669a5db2006-08-29 18:12:40 -0400214 .qc_prep = ata_qc_prep,
215 .qc_issue = qdi_qc_issue_prot,
Jeff Garzikbda30282006-09-27 05:41:13 -0400216
Jeff Garzik669a5db2006-08-29 18:12:40 -0400217 .data_xfer = qdi_data_xfer,
218
219 .irq_handler = ata_interrupt,
220 .irq_clear = ata_bmdma_irq_clear,
Jeff Garzik85cd7252006-08-31 00:03:49 -0400221
Jeff Garzik669a5db2006-08-29 18:12:40 -0400222 .port_start = ata_port_start,
223 .port_stop = ata_port_stop,
224 .host_stop = ata_host_stop
Jeff Garzik85cd7252006-08-31 00:03:49 -0400225};
Jeff Garzik669a5db2006-08-29 18:12:40 -0400226
227/**
228 * qdi_init_one - attach a qdi interface
229 * @type: Type to display
230 * @io: I/O port start
231 * @irq: interrupt line
232 * @fast: True if on a > 33Mhz VLB
233 *
234 * Register an ISA bus IDE interface. Such interfaces are PIO and we
235 * assume do not support IRQ sharing.
236 */
Jeff Garzik85cd7252006-08-31 00:03:49 -0400237
Jeff Garzik669a5db2006-08-29 18:12:40 -0400238static __init int qdi_init_one(unsigned long port, int type, unsigned long io, int irq, int fast)
239{
240 struct ata_probe_ent ae;
241 struct platform_device *pdev;
242 int ret;
243
244 unsigned long ctrl = io + 0x206;
245
246 /*
247 * Fill in a probe structure first of all
248 */
249
250 pdev = platform_device_register_simple(DRV_NAME, nr_qdi_host, NULL, 0);
251 if (pdev == NULL)
252 return -ENOMEM;
Jeff Garzik85cd7252006-08-31 00:03:49 -0400253
Jeff Garzik669a5db2006-08-29 18:12:40 -0400254 memset(&ae, 0, sizeof(struct ata_probe_ent));
255 INIT_LIST_HEAD(&ae.node);
256 ae.dev = &pdev->dev;
Jeff Garzik85cd7252006-08-31 00:03:49 -0400257
Jeff Garzik669a5db2006-08-29 18:12:40 -0400258 if (type == 6580) {
259 ae.port_ops = &qdi6580_port_ops;
260 ae.pio_mask = 0x1F;
261 } else {
262 ae.port_ops = &qdi6500_port_ops;
263 ae.pio_mask = 0x07; /* Actually PIO3 !IORDY is possible */
264 }
Jeff Garzik85cd7252006-08-31 00:03:49 -0400265
Jeff Garzik669a5db2006-08-29 18:12:40 -0400266 ae.sht = &qdi_sht;
267 ae.n_ports = 1;
268 ae.irq = irq;
269 ae.irq_flags = 0;
270 ae.port_flags = ATA_FLAG_SLAVE_POSS | ATA_FLAG_SRST;
271 ae.port[0].cmd_addr = io;
272 ae.port[0].altstatus_addr = ctrl;
273 ae.port[0].ctl_addr = ctrl;
274 ata_std_ports(&ae.port[0]);
275
276 /*
277 * Hook in a private data structure per channel
278 */
279 ae.private_data = &qdi_data[nr_qdi_host];
280
281 qdi_data[nr_qdi_host].timing = port;
282 qdi_data[nr_qdi_host].fast = fast;
283 qdi_data[nr_qdi_host].platform_dev = pdev;
284
285 printk(KERN_INFO DRV_NAME": qd%d at 0x%lx.\n", type, io);
286 ret = ata_device_add(&ae);
287 if (ret == 0) {
288 platform_device_unregister(pdev);
289 return -ENODEV;
290 }
Jeff Garzik85cd7252006-08-31 00:03:49 -0400291
Jeff Garzik669a5db2006-08-29 18:12:40 -0400292 qdi_host[nr_qdi_host++] = dev_get_drvdata(&pdev->dev);
Jeff Garzik85cd7252006-08-31 00:03:49 -0400293 return 0;
Jeff Garzik669a5db2006-08-29 18:12:40 -0400294}
295
296/**
297 * qdi_init - attach qdi interfaces
298 *
299 * Attach qdi IDE interfaces by scanning the ports it may occupy.
300 */
Jeff Garzik85cd7252006-08-31 00:03:49 -0400301
Jeff Garzik669a5db2006-08-29 18:12:40 -0400302static __init int qdi_init(void)
303{
304 unsigned long flags;
305 static const unsigned long qd_port[2] = { 0x30, 0xB0 };
306 static const unsigned long ide_port[2] = { 0x170, 0x1F0 };
307 static const int ide_irq[2] = { 14, 15 };
Jeff Garzik85cd7252006-08-31 00:03:49 -0400308
Jeff Garzik669a5db2006-08-29 18:12:40 -0400309 int ct = 0;
310 int i;
Jeff Garzik85cd7252006-08-31 00:03:49 -0400311
Jeff Garzik669a5db2006-08-29 18:12:40 -0400312 if (probe_qdi == 0)
313 return -ENODEV;
Jeff Garzik85cd7252006-08-31 00:03:49 -0400314
Jeff Garzik669a5db2006-08-29 18:12:40 -0400315 /*
316 * Check each possible QD65xx base address
317 */
Jeff Garzik85cd7252006-08-31 00:03:49 -0400318
Jeff Garzik669a5db2006-08-29 18:12:40 -0400319 for (i = 0; i < 2; i++) {
320 unsigned long port = qd_port[i];
321 u8 r, res;
Jeff Garzik85cd7252006-08-31 00:03:49 -0400322
323
Jeff Garzik669a5db2006-08-29 18:12:40 -0400324 if (request_region(port, 2, "pata_qdi")) {
325 /* Check for a card */
326 local_irq_save(flags);
327 r = inb_p(port);
328 outb_p(0x19, port);
329 res = inb_p(port);
330 outb_p(r, port);
331 local_irq_restore(flags);
Jeff Garzik85cd7252006-08-31 00:03:49 -0400332
Jeff Garzik669a5db2006-08-29 18:12:40 -0400333 /* Fail */
334 if (res == 0x19)
335 {
336 release_region(port, 2);
337 continue;
338 }
Jeff Garzik85cd7252006-08-31 00:03:49 -0400339
Jeff Garzik669a5db2006-08-29 18:12:40 -0400340 /* Passes the presence test */
341 r = inb_p(port + 1); /* Check port agrees with port set */
342 if ((r & 2) >> 1 != i) {
343 release_region(port, 2);
344 continue;
345 }
346
Jeff Garzik85cd7252006-08-31 00:03:49 -0400347 /* Check card type */
Jeff Garzik669a5db2006-08-29 18:12:40 -0400348 if ((r & 0xF0) == 0xC0) {
349 /* QD6500: single channel */
350 if (r & 8) {
351 /* Disabled ? */
352 release_region(port, 2);
353 continue;
354 }
355 ct += qdi_init_one(port, 6500, ide_port[r & 0x01], ide_irq[r & 0x01], r & 0x04);
356 }
357 if (((r & 0xF0) == 0xA0) || (r & 0xF0) == 0x50) {
358 /* QD6580: dual channel */
359 if (!request_region(port + 2 , 2, "pata_qdi"))
360 {
361 release_region(port, 2);
362 continue;
363 }
364 res = inb(port + 3);
365 if (res & 1) {
366 /* Single channel mode */
367 ct += qdi_init_one(port, 6580, ide_port[r & 0x01], ide_irq[r & 0x01], r & 0x04);
368 } else {
369 /* Dual channel mode */
370 ct += qdi_init_one(port, 6580, 0x1F0, 14, r & 0x04);
371 ct += qdi_init_one(port + 2, 6580, 0x170, 15, r & 0x04);
372 }
373 }
374 }
375 }
376 if (ct != 0)
377 return 0;
378 return -ENODEV;
379}
380
381static __exit void qdi_exit(void)
382{
383 int i;
384
385 for (i = 0; i < nr_qdi_host; i++) {
386 ata_host_remove(qdi_host[i]);
387 /* Free the control resource. The 6580 dual channel has the resources
388 * claimed as a pair of 2 byte resources so we need no special cases...
389 */
390 release_region(qdi_data[i].timing, 2);
391 platform_device_unregister(qdi_data[i].platform_dev);
Jeff Garzik85cd7252006-08-31 00:03:49 -0400392 }
Jeff Garzik669a5db2006-08-29 18:12:40 -0400393}
394
395MODULE_AUTHOR("Alan Cox");
396MODULE_DESCRIPTION("low-level driver for qdi ATA");
397MODULE_LICENSE("GPL");
398MODULE_VERSION(DRV_VERSION);
399
400module_init(qdi_init);
401module_exit(qdi_exit);
402
403module_param(probe_qdi, int, 0);
404