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
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.3.1"
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;
39
40};
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);
61
62	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;
70
71	qdi->clock[adev->devno] = timing;
72
73	outb(timing, qdi->timing);
74}
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);
85
86	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;
94
95	qdi->clock[adev->devno] = timing;
96
97	outb(timing, qdi->timing);
98
99	/* 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;
131
132	if (ata_id_has_dword_io(adev->id)) {
133		if (write_data)
134			iowrite32_rep(ap->ioaddr.data_addr, buf, buflen >> 2);
135		else
136			ioread32_rep(ap->ioaddr.data_addr, buf, buflen >> 2);
137
138		if (unlikely(slop)) {
139			u32 pad;
140			if (write_data) {
141				memcpy(&pad, buf + buflen - slop, slop);
142				pad = le32_to_cpu(pad);
143				iowrite32(pad, ap->ioaddr.data_addr);
144			} else {
145				pad = ioread32(ap->ioaddr.data_addr);
146				pad = cpu_to_le32(pad);
147				memcpy(buf + buflen - slop, &pad, slop);
148			}
149		}
150	} else
151		ata_data_xfer(adev, buf, buflen, write_data);
152}
153
154static struct scsi_host_template qdi_sht = {
155	.module			= THIS_MODULE,
156	.name			= DRV_NAME,
157	.ioctl			= ata_scsi_ioctl,
158	.queuecommand		= ata_scsi_queuecmd,
159	.can_queue		= ATA_DEF_QUEUE,
160	.this_id		= ATA_SHT_THIS_ID,
161	.sg_tablesize		= LIBATA_MAX_PRD,
162	.cmd_per_lun		= ATA_SHT_CMD_PER_LUN,
163	.emulated		= ATA_SHT_EMULATED,
164	.use_clustering		= ATA_SHT_USE_CLUSTERING,
165	.proc_name		= DRV_NAME,
166	.dma_boundary		= ATA_DMA_BOUNDARY,
167	.slave_configure	= ata_scsi_slave_config,
168	.slave_destroy		= ata_scsi_slave_destroy,
169	.bios_param		= ata_std_bios_param,
170};
171
172static struct ata_port_operations qdi6500_port_ops = {
173	.port_disable	= ata_port_disable,
174	.set_piomode	= qdi6500_set_piomode,
175
176	.tf_load	= ata_tf_load,
177	.tf_read	= ata_tf_read,
178	.check_status 	= ata_check_status,
179	.exec_command	= ata_exec_command,
180	.dev_select 	= ata_std_dev_select,
181
182	.freeze		= ata_bmdma_freeze,
183	.thaw		= ata_bmdma_thaw,
184	.error_handler	= ata_bmdma_error_handler,
185	.post_internal_cmd = ata_bmdma_post_internal_cmd,
186	.cable_detect	= ata_cable_40wire,
187
188	.qc_prep 	= ata_qc_prep,
189	.qc_issue	= qdi_qc_issue_prot,
190
191	.data_xfer	= qdi_data_xfer,
192
193	.irq_clear	= ata_bmdma_irq_clear,
194	.irq_on		= ata_irq_on,
195	.irq_ack	= ata_irq_ack,
196
197	.port_start	= ata_port_start,
198};
199
200static struct ata_port_operations qdi6580_port_ops = {
201	.port_disable	= ata_port_disable,
202	.set_piomode	= qdi6580_set_piomode,
203
204	.tf_load	= ata_tf_load,
205	.tf_read	= ata_tf_read,
206	.check_status 	= ata_check_status,
207	.exec_command	= ata_exec_command,
208	.dev_select 	= ata_std_dev_select,
209
210	.freeze		= ata_bmdma_freeze,
211	.thaw		= ata_bmdma_thaw,
212	.error_handler	= ata_bmdma_error_handler,
213	.post_internal_cmd = ata_bmdma_post_internal_cmd,
214	.cable_detect	= ata_cable_40wire,
215
216	.qc_prep 	= ata_qc_prep,
217	.qc_issue	= qdi_qc_issue_prot,
218
219	.data_xfer	= qdi_data_xfer,
220
221	.irq_clear	= ata_bmdma_irq_clear,
222	.irq_on		= ata_irq_on,
223	.irq_ack	= ata_irq_ack,
224
225	.port_start	= ata_port_start,
226};
227
228/**
229 *	qdi_init_one		-	attach a qdi interface
230 *	@type: Type to display
231 *	@io: I/O port start
232 *	@irq: interrupt line
233 *	@fast: True if on a > 33Mhz VLB
234 *
235 *	Register an ISA bus IDE interface. Such interfaces are PIO and we
236 *	assume do not support IRQ sharing.
237 */
238
239static __init int qdi_init_one(unsigned long port, int type, unsigned long io, int irq, int fast)
240{
241	struct platform_device *pdev;
242	struct ata_host *host;
243	struct ata_port *ap;
244	void __iomem *io_addr, *ctl_addr;
245	int ret;
246
247	/*
248	 *	Fill in a probe structure first of all
249	 */
250
251	pdev = platform_device_register_simple(DRV_NAME, nr_qdi_host, NULL, 0);
252	if (IS_ERR(pdev))
253		return PTR_ERR(pdev);
254
255	ret = -ENOMEM;
256	io_addr = devm_ioport_map(&pdev->dev, io, 8);
257	ctl_addr = devm_ioport_map(&pdev->dev, io + 0x206, 1);
258	if (!io_addr || !ctl_addr)
259		goto fail;
260
261	ret = -ENOMEM;
262	host = ata_host_alloc(&pdev->dev, 1);
263	if (!host)
264		goto fail;
265	ap = host->ports[0];
266
267	if (type == 6580) {
268		ap->ops = &qdi6580_port_ops;
269		ap->pio_mask = 0x1F;
270		ap->flags |= ATA_FLAG_SLAVE_POSS;
271	} else {
272		ap->ops = &qdi6500_port_ops;
273		ap->pio_mask = 0x07;	/* Actually PIO3 !IORDY is possible */
274		ap->flags = ATA_FLAG_SLAVE_POSS | ATA_FLAG_NO_IORDY;
275	}
276
277	ap->ioaddr.cmd_addr = io_addr;
278	ap->ioaddr.altstatus_addr = ctl_addr;
279	ap->ioaddr.ctl_addr = ctl_addr;
280	ata_std_ports(&ap->ioaddr);
281
282	/*
283	 *	Hook in a private data structure per channel
284	 */
285	ap->private_data = &qdi_data[nr_qdi_host];
286
287	qdi_data[nr_qdi_host].timing = port;
288	qdi_data[nr_qdi_host].fast = fast;
289	qdi_data[nr_qdi_host].platform_dev = pdev;
290
291	printk(KERN_INFO DRV_NAME": qd%d at 0x%lx.\n", type, io);
292
293	/* activate */
294	ret = ata_host_activate(host, irq, ata_interrupt, 0, &qdi_sht);
295	if (ret)
296		goto fail;
297
298	qdi_host[nr_qdi_host++] = dev_get_drvdata(&pdev->dev);
299	return 0;
300
301 fail:
302	platform_device_unregister(pdev);
303	return ret;
304}
305
306/**
307 *	qdi_init		-	attach qdi interfaces
308 *
309 *	Attach qdi IDE interfaces by scanning the ports it may occupy.
310 */
311
312static __init int qdi_init(void)
313{
314	unsigned long flags;
315	static const unsigned long qd_port[2] = { 0x30, 0xB0 };
316	static const unsigned long ide_port[2] = { 0x170, 0x1F0 };
317	static const int ide_irq[2] = { 14, 15 };
318
319	int ct = 0;
320	int i;
321
322	if (probe_qdi == 0)
323		return -ENODEV;
324
325	/*
326 	 *	Check each possible QD65xx base address
327	 */
328
329	for (i = 0; i < 2; i++) {
330		unsigned long port = qd_port[i];
331		u8 r, res;
332
333
334		if (request_region(port, 2, "pata_qdi")) {
335			/* Check for a card */
336			local_irq_save(flags);
337			r = inb_p(port);
338			outb_p(0x19, port);
339			res = inb_p(port);
340			outb_p(r, port);
341			local_irq_restore(flags);
342
343			/* Fail */
344			if (res == 0x19)
345			{
346				release_region(port, 2);
347				continue;
348			}
349
350			/* Passes the presence test */
351			r = inb_p(port + 1);	/* Check port agrees with port set */
352			if ((r & 2) >> 1 != i) {
353				release_region(port, 2);
354				continue;
355			}
356
357			/* Check card type */
358			if ((r & 0xF0) == 0xC0) {
359				/* QD6500: single channel */
360				if (r & 8) {
361					/* Disabled ? */
362					release_region(port, 2);
363					continue;
364				}
365				if (qdi_init_one(port, 6500, ide_port[r & 0x01], ide_irq[r & 0x01], r & 0x04) == 0)
366					ct++;
367			}
368			if (((r & 0xF0) == 0xA0) || (r & 0xF0) == 0x50) {
369				/* QD6580: dual channel */
370				if (!request_region(port + 2 , 2, "pata_qdi"))
371				{
372					release_region(port, 2);
373					continue;
374				}
375				res = inb(port + 3);
376				if (res & 1) {
377					/* Single channel mode */
378					if (qdi_init_one(port, 6580, ide_port[r & 0x01], ide_irq[r & 0x01], r & 0x04) == 0)
379						ct++;
380				} else {
381					/* Dual channel mode */
382					if (qdi_init_one(port, 6580, 0x1F0, 14, r & 0x04) == 0)
383						ct++;
384					if (qdi_init_one(port + 2, 6580, 0x170, 15, r & 0x04) == 0)
385						ct++;
386				}
387			}
388		}
389	}
390	if (ct != 0)
391		return 0;
392	return -ENODEV;
393}
394
395static __exit void qdi_exit(void)
396{
397	int i;
398
399	for (i = 0; i < nr_qdi_host; i++) {
400		ata_host_detach(qdi_host[i]);
401		/* Free the control resource. The 6580 dual channel has the resources
402		 * claimed as a pair of 2 byte resources so we need no special cases...
403		 */
404		release_region(qdi_data[i].timing, 2);
405		platform_device_unregister(qdi_data[i].platform_dev);
406	}
407}
408
409MODULE_AUTHOR("Alan Cox");
410MODULE_DESCRIPTION("low-level driver for qdi ATA");
411MODULE_LICENSE("GPL");
412MODULE_VERSION(DRV_VERSION);
413
414module_init(qdi_init);
415module_exit(qdi_exit);
416
417module_param(probe_qdi, int, 0);
418