2 * pata_qdi.c - QDI VLB ATA controllers
3 * (C) 2006 Red Hat <alan@redhat.com>
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
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
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>
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>
28 #define DRV_NAME "pata_qdi"
29 #define DRV_VERSION "0.2.4"
31 #define NR_HOST 4 /* Two 6580s */
38 struct platform_device *platform_dev;
42 static struct ata_host *qdi_host[NR_HOST];
43 static struct qdi_data qdi_data[NR_HOST];
44 static int nr_qdi_host;
47 static int probe_qdi = 1;
52 static void qdi6500_set_piomode(struct ata_port *ap, struct ata_device *adev)
55 struct qdi_data *qdi = ap->host->private_data;
59 /* Get the timing data in cycles */
60 ata_timing_compute(adev, adev->pio_mode, &t, 30303, 1000);
63 active = 8 - FIT(t.active, 1, 8);
64 recovery = 18 - FIT(t.recover, 3, 18);
66 active = 9 - FIT(t.active, 2, 9);
67 recovery = 15 - FIT(t.recover, 0, 15);
69 timing = (recovery << 4) | active | 0x08;
71 qdi->clock[adev->devno] = timing;
73 outb(timing, qdi->timing);
76 static void qdi6580_set_piomode(struct ata_port *ap, struct ata_device *adev)
79 struct qdi_data *qdi = ap->host->private_data;
83 /* Get the timing data in cycles */
84 ata_timing_compute(adev, adev->pio_mode, &t, 30303, 1000);
87 active = 8 - FIT(t.active, 1, 8);
88 recovery = 18 - FIT(t.recover, 3, 18);
90 active = 9 - FIT(t.active, 2, 9);
91 recovery = 15 - FIT(t.recover, 0, 15);
93 timing = (recovery << 4) | active | 0x08;
95 qdi->clock[adev->devno] = timing;
97 outb(timing, qdi->timing);
100 if (adev->class != ATA_DEV_ATA)
101 outb(0x5F, (qdi->timing & 0xFFF0) + 3);
105 * qdi_qc_issue_prot - command issue
106 * @qc: command pending
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.
112 static unsigned int qdi_qc_issue_prot(struct ata_queued_cmd *qc)
114 struct ata_port *ap = qc->ap;
115 struct ata_device *adev = qc->dev;
116 struct qdi_data *qdi = ap->host->private_data;
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);
124 return ata_qc_issue_prot(qc);
127 static void qdi_data_xfer(struct ata_device *adev, unsigned char *buf, unsigned int buflen, int write_data)
129 struct ata_port *ap = adev->ap;
130 int slop = buflen & 3;
132 if (ata_id_has_dword_io(adev->id)) {
134 outsl(ap->ioaddr.data_addr, buf, buflen >> 2);
136 insl(ap->ioaddr.data_addr, buf, buflen >> 2);
138 if (unlikely(slop)) {
141 memcpy(&pad, buf + buflen - slop, slop);
142 outl(le32_to_cpu(pad), ap->ioaddr.data_addr);
144 pad = cpu_to_le16(inl(ap->ioaddr.data_addr));
145 memcpy(buf + buflen - slop, &pad, slop);
149 ata_pio_data_xfer(adev, buf, buflen, write_data);
152 static struct scsi_host_template qdi_sht = {
153 .module = THIS_MODULE,
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,
167 .bios_param = ata_std_bios_param,
170 static struct ata_port_operations qdi6500_port_ops = {
171 .port_disable = ata_port_disable,
172 .set_piomode = qdi6500_set_piomode,
174 .tf_load = ata_tf_load,
175 .tf_read = ata_tf_read,
176 .check_status = ata_check_status,
177 .exec_command = ata_exec_command,
178 .dev_select = ata_std_dev_select,
180 .freeze = ata_bmdma_freeze,
181 .thaw = ata_bmdma_thaw,
182 .error_handler = ata_bmdma_error_handler,
183 .post_internal_cmd = ata_bmdma_post_internal_cmd,
185 .qc_prep = ata_qc_prep,
186 .qc_issue = qdi_qc_issue_prot,
188 .data_xfer = qdi_data_xfer,
190 .irq_handler = ata_interrupt,
191 .irq_clear = ata_bmdma_irq_clear,
193 .port_start = ata_port_start,
194 .port_stop = ata_port_stop,
195 .host_stop = ata_host_stop
198 static struct ata_port_operations qdi6580_port_ops = {
199 .port_disable = ata_port_disable,
200 .set_piomode = qdi6580_set_piomode,
202 .tf_load = ata_tf_load,
203 .tf_read = ata_tf_read,
204 .check_status = ata_check_status,
205 .exec_command = ata_exec_command,
206 .dev_select = ata_std_dev_select,
208 .freeze = ata_bmdma_freeze,
209 .thaw = ata_bmdma_thaw,
210 .error_handler = ata_bmdma_error_handler,
211 .post_internal_cmd = ata_bmdma_post_internal_cmd,
213 .qc_prep = ata_qc_prep,
214 .qc_issue = qdi_qc_issue_prot,
216 .data_xfer = qdi_data_xfer,
218 .irq_handler = ata_interrupt,
219 .irq_clear = ata_bmdma_irq_clear,
221 .port_start = ata_port_start,
222 .port_stop = ata_port_stop,
223 .host_stop = ata_host_stop
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
233 * Register an ISA bus IDE interface. Such interfaces are PIO and we
234 * assume do not support IRQ sharing.
237 static __init int qdi_init_one(unsigned long port, int type, unsigned long io, int irq, int fast)
239 struct ata_probe_ent ae;
240 struct platform_device *pdev;
243 unsigned long ctrl = io + 0x206;
246 * Fill in a probe structure first of all
249 pdev = platform_device_register_simple(DRV_NAME, nr_qdi_host, NULL, 0);
253 memset(&ae, 0, sizeof(struct ata_probe_ent));
254 INIT_LIST_HEAD(&ae.node);
258 ae.port_ops = &qdi6580_port_ops;
261 ae.port_ops = &qdi6500_port_ops;
262 ae.pio_mask = 0x07; /* Actually PIO3 !IORDY is possible */
269 ae.port_flags = ATA_FLAG_SLAVE_POSS | ATA_FLAG_SRST;
270 ae.port[0].cmd_addr = io;
271 ae.port[0].altstatus_addr = ctrl;
272 ae.port[0].ctl_addr = ctrl;
273 ata_std_ports(&ae.port[0]);
276 * Hook in a private data structure per channel
278 ae.private_data = &qdi_data[nr_qdi_host];
280 qdi_data[nr_qdi_host].timing = port;
281 qdi_data[nr_qdi_host].fast = fast;
282 qdi_data[nr_qdi_host].platform_dev = pdev;
284 printk(KERN_INFO DRV_NAME": qd%d at 0x%lx.\n", type, io);
285 ret = ata_device_add(&ae);
287 platform_device_unregister(pdev);
291 qdi_host[nr_qdi_host++] = dev_get_drvdata(&pdev->dev);
296 * qdi_init - attach qdi interfaces
298 * Attach qdi IDE interfaces by scanning the ports it may occupy.
301 static __init int qdi_init(void)
304 static const unsigned long qd_port[2] = { 0x30, 0xB0 };
305 static const unsigned long ide_port[2] = { 0x170, 0x1F0 };
306 static const int ide_irq[2] = { 14, 15 };
315 * Check each possible QD65xx base address
318 for (i = 0; i < 2; i++) {
319 unsigned long port = qd_port[i];
323 if (request_region(port, 2, "pata_qdi")) {
324 /* Check for a card */
325 local_irq_save(flags);
330 local_irq_restore(flags);
335 release_region(port, 2);
339 /* Passes the presence test */
340 r = inb_p(port + 1); /* Check port agrees with port set */
341 if ((r & 2) >> 1 != i) {
342 release_region(port, 2);
346 /* Check card type */
347 if ((r & 0xF0) == 0xC0) {
348 /* QD6500: single channel */
351 release_region(port, 2);
354 ct += qdi_init_one(port, 6500, ide_port[r & 0x01], ide_irq[r & 0x01], r & 0x04);
356 if (((r & 0xF0) == 0xA0) || (r & 0xF0) == 0x50) {
357 /* QD6580: dual channel */
358 if (!request_region(port + 2 , 2, "pata_qdi"))
360 release_region(port, 2);
365 /* Single channel mode */
366 ct += qdi_init_one(port, 6580, ide_port[r & 0x01], ide_irq[r & 0x01], r & 0x04);
368 /* Dual channel mode */
369 ct += qdi_init_one(port, 6580, 0x1F0, 14, r & 0x04);
370 ct += qdi_init_one(port + 2, 6580, 0x170, 15, r & 0x04);
380 static __exit void qdi_exit(void)
384 for (i = 0; i < nr_qdi_host; i++) {
385 ata_host_remove(qdi_host[i]);
386 /* Free the control resource. The 6580 dual channel has the resources
387 * claimed as a pair of 2 byte resources so we need no special cases...
389 release_region(qdi_data[i].timing, 2);
390 platform_device_unregister(qdi_data[i].platform_dev);
394 MODULE_AUTHOR("Alan Cox");
395 MODULE_DESCRIPTION("low-level driver for qdi ATA");
396 MODULE_LICENSE("GPL");
397 MODULE_VERSION(DRV_VERSION);
399 module_init(qdi_init);
400 module_exit(qdi_exit);
402 module_param(probe_qdi, int, 0);