blob: 9d92843b033d1a5adabdc796f5d7f7bc8c8ed635 [file] [log] [blame]
Jeff Garzik669a5db2006-08-29 18:12:40 -04001/*
2 * pata-legacy.c - Legacy port PATA/SATA controller driver.
3 * Copyright 2005/2006 Red Hat <alan@redhat.com>, all rights reserved.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
8 * any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; see the file COPYING. If not, write to
17 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
18 *
19 * An ATA driver for the legacy ATA ports.
20 *
21 * Data Sources:
22 * Opti 82C465/82C611 support: Data sheets at opti-inc.com
23 * HT6560 series:
24 * Promise 20230/20620:
25 * http://www.ryston.cz/petr/vlb/pdc20230b.html
26 * http://www.ryston.cz/petr/vlb/pdc20230c.html
27 * http://www.ryston.cz/petr/vlb/pdc20630.html
28 *
29 * Unsupported but docs exist:
30 * Appian/Adaptec AIC25VL01/Cirrus Logic PD7220
31 * Winbond W83759A
32 *
33 * This driver handles legacy (that is "ISA/VLB side") IDE ports found
34 * on PC class systems. There are three hybrid devices that are exceptions
35 * The Cyrix 5510/5520 where a pre SFF ATA device is on the bridge and
36 * the MPIIX where the tuning is PCI side but the IDE is "ISA side".
37 *
38 * Specific support is included for the ht6560a/ht6560b/opti82c611a/
39 * opti82c465mv/promise 20230c/20630
40 *
41 * Use the autospeed and pio_mask options with:
42 * Appian ADI/2 aka CLPD7220 or AIC25VL01.
43 * Use the jumpers, autospeed and set pio_mask to the mode on the jumpers with
44 * Goldstar GM82C711, PIC-1288A-125, UMC 82C871F, Winbond W83759,
45 * Winbond W83759A, Promise PDC20230-B
46 *
47 * For now use autospeed and pio_mask as above with the W83759A. This may
48 * change.
49 *
50 * TODO
51 * Merge existing pata_qdi driver
52 *
53 */
54
55#include <linux/kernel.h>
56#include <linux/module.h>
57#include <linux/pci.h>
58#include <linux/init.h>
59#include <linux/blkdev.h>
60#include <linux/delay.h>
61#include <scsi/scsi_host.h>
62#include <linux/ata.h>
63#include <linux/libata.h>
64#include <linux/platform_device.h>
65
66#define DRV_NAME "pata_legacy"
Jeff Garzik8bc3fc42007-05-21 20:26:38 -040067#define DRV_VERSION "0.5.5"
Jeff Garzik669a5db2006-08-29 18:12:40 -040068
69#define NR_HOST 6
70
71static int legacy_port[NR_HOST] = { 0x1f0, 0x170, 0x1e8, 0x168, 0x1e0, 0x160 };
Mikael Pettersson8b966dd2007-03-03 20:57:44 +010072static int legacy_irq[NR_HOST] = { 14, 15, 11, 10, 8, 12 };
Jeff Garzik669a5db2006-08-29 18:12:40 -040073
74struct legacy_data {
75 unsigned long timing;
76 u8 clock[2];
77 u8 last;
78 int fast;
79 struct platform_device *platform_dev;
80
81};
82
83static struct legacy_data legacy_data[NR_HOST];
84static struct ata_host *legacy_host[NR_HOST];
85static int nr_legacy_host;
86
87
88static int probe_all; /* Set to check all ISA port ranges */
89static int ht6560a; /* HT 6560A on primary 1, secondary 2, both 3 */
90static int ht6560b; /* HT 6560A on primary 1, secondary 2, both 3 */
91static int opti82c611a; /* Opti82c611A on primary 1, secondary 2, both 3 */
Alan Coxf834e492007-02-07 13:46:00 -080092static int opti82c46x; /* Opti 82c465MV present (pri/sec autodetect) */
Jeff Garzik669a5db2006-08-29 18:12:40 -040093static int autospeed; /* Chip present which snoops speed changes */
94static int pio_mask = 0x1F; /* PIO range for autospeed devices */
Alan Coxf834e492007-02-07 13:46:00 -080095static int iordy_mask = 0xFFFFFFFF; /* Use iordy if available */
Jeff Garzik669a5db2006-08-29 18:12:40 -040096
97/**
98 * legacy_set_mode - mode setting
99 * @ap: IDE interface
Alanb229a7b2007-01-24 11:47:07 +0000100 * @unused: Device that failed when error is returned
Jeff Garzik669a5db2006-08-29 18:12:40 -0400101 *
102 * Use a non standard set_mode function. We don't want to be tuned.
103 *
104 * The BIOS configured everything. Our job is not to fiddle. Just use
105 * whatever PIO the hardware is using and leave it at that. When we
106 * get some kind of nice user driven API for control then we can
107 * expand on this as per hdparm in the base kernel.
108 */
109
Alanb229a7b2007-01-24 11:47:07 +0000110static int legacy_set_mode(struct ata_port *ap, struct ata_device **unused)
Jeff Garzik669a5db2006-08-29 18:12:40 -0400111{
Tejun Heof58229f2007-08-06 18:36:23 +0900112 struct ata_device *dev;
Jeff Garzik669a5db2006-08-29 18:12:40 -0400113
Tejun Heof58229f2007-08-06 18:36:23 +0900114 ata_link_for_each_dev(dev, &ap->link) {
Jeff Garzik669a5db2006-08-29 18:12:40 -0400115 if (ata_dev_enabled(dev)) {
Alan Coxf834e492007-02-07 13:46:00 -0800116 ata_dev_printk(dev, KERN_INFO, "configured for PIO\n");
Jeff Garzik669a5db2006-08-29 18:12:40 -0400117 dev->pio_mode = XFER_PIO_0;
118 dev->xfer_mode = XFER_PIO_0;
119 dev->xfer_shift = ATA_SHIFT_PIO;
120 dev->flags |= ATA_DFLAG_PIO;
121 }
122 }
Alanb229a7b2007-01-24 11:47:07 +0000123 return 0;
Jeff Garzik669a5db2006-08-29 18:12:40 -0400124}
125
126static struct scsi_host_template legacy_sht = {
127 .module = THIS_MODULE,
128 .name = DRV_NAME,
129 .ioctl = ata_scsi_ioctl,
130 .queuecommand = ata_scsi_queuecmd,
131 .can_queue = ATA_DEF_QUEUE,
132 .this_id = ATA_SHT_THIS_ID,
133 .sg_tablesize = LIBATA_MAX_PRD,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400134 .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
135 .emulated = ATA_SHT_EMULATED,
136 .use_clustering = ATA_SHT_USE_CLUSTERING,
137 .proc_name = DRV_NAME,
138 .dma_boundary = ATA_DMA_BOUNDARY,
139 .slave_configure = ata_scsi_slave_config,
Tejun Heoafdfe892006-11-29 11:26:47 +0900140 .slave_destroy = ata_scsi_slave_destroy,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400141 .bios_param = ata_std_bios_param,
142};
143
144/*
145 * These ops are used if the user indicates the hardware
146 * snoops the commands to decide on the mode and handles the
147 * mode selection "magically" itself. Several legacy controllers
148 * do this. The mode range can be set if it is not 0x1F by setting
149 * pio_mask as well.
150 */
151
152static struct ata_port_operations simple_port_ops = {
153 .port_disable = ata_port_disable,
154 .tf_load = ata_tf_load,
155 .tf_read = ata_tf_read,
156 .check_status = ata_check_status,
157 .exec_command = ata_exec_command,
158 .dev_select = ata_std_dev_select,
159
160 .freeze = ata_bmdma_freeze,
161 .thaw = ata_bmdma_thaw,
162 .error_handler = ata_bmdma_error_handler,
163 .post_internal_cmd = ata_bmdma_post_internal_cmd,
Jeff Garzika73984a2007-03-09 08:37:46 -0500164 .cable_detect = ata_cable_40wire,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400165
166 .qc_prep = ata_qc_prep,
167 .qc_issue = ata_qc_issue_prot,
Jeff Garzikbda30282006-09-27 05:41:13 -0400168
Tejun Heo0d5ff562007-02-01 15:06:36 +0900169 .data_xfer = ata_data_xfer_noirq,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400170
171 .irq_handler = ata_interrupt,
172 .irq_clear = ata_bmdma_irq_clear,
Akira Iguchi246ce3b2007-01-26 16:27:58 +0900173 .irq_on = ata_irq_on,
174 .irq_ack = ata_irq_ack,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400175
176 .port_start = ata_port_start,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400177};
178
179static struct ata_port_operations legacy_port_ops = {
180 .set_mode = legacy_set_mode,
181
182 .port_disable = ata_port_disable,
183 .tf_load = ata_tf_load,
184 .tf_read = ata_tf_read,
185 .check_status = ata_check_status,
186 .exec_command = ata_exec_command,
187 .dev_select = ata_std_dev_select,
Jeff Garzika73984a2007-03-09 08:37:46 -0500188 .cable_detect = ata_cable_40wire,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400189
Jeff Garzikbf7551c2007-03-02 18:09:05 -0500190 .freeze = ata_bmdma_freeze,
191 .thaw = ata_bmdma_thaw,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400192 .error_handler = ata_bmdma_error_handler,
Jeff Garzikbf7551c2007-03-02 18:09:05 -0500193 .post_internal_cmd = ata_bmdma_post_internal_cmd,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400194
195 .qc_prep = ata_qc_prep,
196 .qc_issue = ata_qc_issue_prot,
Jeff Garzikbda30282006-09-27 05:41:13 -0400197
Tejun Heo0d5ff562007-02-01 15:06:36 +0900198 .data_xfer = ata_data_xfer_noirq,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400199
200 .irq_handler = ata_interrupt,
201 .irq_clear = ata_bmdma_irq_clear,
Akira Iguchi246ce3b2007-01-26 16:27:58 +0900202 .irq_on = ata_irq_on,
203 .irq_ack = ata_irq_ack,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400204
205 .port_start = ata_port_start,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400206};
207
208/*
209 * Promise 20230C and 20620 support
210 *
211 * This controller supports PIO0 to PIO2. We set PIO timings conservatively to
212 * allow for 50MHz Vesa Local Bus. The 20620 DMA support is weird being DMA to
213 * controller and PIO'd to the host and not supported.
214 */
215
216static void pdc20230_set_piomode(struct ata_port *ap, struct ata_device *adev)
217{
218 int tries = 5;
219 int pio = adev->pio_mode - XFER_PIO_0;
220 u8 rt;
221 unsigned long flags;
Jeff Garzik85cd7252006-08-31 00:03:49 -0400222
Jeff Garzik669a5db2006-08-29 18:12:40 -0400223 /* Safe as UP only. Force I/Os to occur together */
Jeff Garzik85cd7252006-08-31 00:03:49 -0400224
Jeff Garzik669a5db2006-08-29 18:12:40 -0400225 local_irq_save(flags);
Jeff Garzik85cd7252006-08-31 00:03:49 -0400226
Jeff Garzik669a5db2006-08-29 18:12:40 -0400227 /* Unlock the control interface */
228 do
229 {
230 inb(0x1F5);
231 outb(inb(0x1F2) | 0x80, 0x1F2);
232 inb(0x1F2);
233 inb(0x3F6);
234 inb(0x3F6);
235 inb(0x1F2);
236 inb(0x1F2);
237 }
238 while((inb(0x1F2) & 0x80) && --tries);
239
240 local_irq_restore(flags);
Jeff Garzik85cd7252006-08-31 00:03:49 -0400241
Jeff Garzik669a5db2006-08-29 18:12:40 -0400242 outb(inb(0x1F4) & 0x07, 0x1F4);
243
244 rt = inb(0x1F3);
245 rt &= 0x07 << (3 * adev->devno);
246 if (pio)
247 rt |= (1 + 3 * pio) << (3 * adev->devno);
248
249 udelay(100);
250 outb(inb(0x1F2) | 0x01, 0x1F2);
251 udelay(100);
252 inb(0x1F5);
253
254}
255
256static void pdc_data_xfer_vlb(struct ata_device *adev, unsigned char *buf, unsigned int buflen, int write_data)
257{
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900258 struct ata_port *ap = adev->link->ap;
Jeff Garzik669a5db2006-08-29 18:12:40 -0400259 int slop = buflen & 3;
260 unsigned long flags;
261
262 if (ata_id_has_dword_io(adev->id)) {
263 local_irq_save(flags);
264
265 /* Perform the 32bit I/O synchronization sequence */
Tejun Heo0d5ff562007-02-01 15:06:36 +0900266 ioread8(ap->ioaddr.nsect_addr);
267 ioread8(ap->ioaddr.nsect_addr);
268 ioread8(ap->ioaddr.nsect_addr);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400269
270 /* Now the data */
271
272 if (write_data)
Tejun Heo0d5ff562007-02-01 15:06:36 +0900273 iowrite32_rep(ap->ioaddr.data_addr, buf, buflen >> 2);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400274 else
Tejun Heo0d5ff562007-02-01 15:06:36 +0900275 ioread32_rep(ap->ioaddr.data_addr, buf, buflen >> 2);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400276
277 if (unlikely(slop)) {
278 u32 pad;
279 if (write_data) {
280 memcpy(&pad, buf + buflen - slop, slop);
Tejun Heo0d5ff562007-02-01 15:06:36 +0900281 pad = le32_to_cpu(pad);
282 iowrite32(pad, ap->ioaddr.data_addr);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400283 } else {
Tejun Heo0d5ff562007-02-01 15:06:36 +0900284 pad = ioread32(ap->ioaddr.data_addr);
285 pad = cpu_to_le16(pad);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400286 memcpy(buf + buflen - slop, &pad, slop);
287 }
288 }
289 local_irq_restore(flags);
290 }
291 else
Tejun Heo0d5ff562007-02-01 15:06:36 +0900292 ata_data_xfer_noirq(adev, buf, buflen, write_data);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400293}
294
295static struct ata_port_operations pdc20230_port_ops = {
296 .set_piomode = pdc20230_set_piomode,
297
298 .port_disable = ata_port_disable,
299 .tf_load = ata_tf_load,
300 .tf_read = ata_tf_read,
301 .check_status = ata_check_status,
302 .exec_command = ata_exec_command,
303 .dev_select = ata_std_dev_select,
304
Jeff Garzikbf7551c2007-03-02 18:09:05 -0500305 .freeze = ata_bmdma_freeze,
306 .thaw = ata_bmdma_thaw,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400307 .error_handler = ata_bmdma_error_handler,
Jeff Garzikbf7551c2007-03-02 18:09:05 -0500308 .post_internal_cmd = ata_bmdma_post_internal_cmd,
Jeff Garzika73984a2007-03-09 08:37:46 -0500309 .cable_detect = ata_cable_40wire,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400310
311 .qc_prep = ata_qc_prep,
312 .qc_issue = ata_qc_issue_prot,
Jeff Garzikbda30282006-09-27 05:41:13 -0400313
Jeff Garzik669a5db2006-08-29 18:12:40 -0400314 .data_xfer = pdc_data_xfer_vlb,
315
316 .irq_handler = ata_interrupt,
317 .irq_clear = ata_bmdma_irq_clear,
Akira Iguchi246ce3b2007-01-26 16:27:58 +0900318 .irq_on = ata_irq_on,
319 .irq_ack = ata_irq_ack,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400320
321 .port_start = ata_port_start,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400322};
323
324/*
325 * Holtek 6560A support
326 *
327 * This controller supports PIO0 to PIO2 (no IORDY even though higher timings
328 * can be loaded).
329 */
330
331static void ht6560a_set_piomode(struct ata_port *ap, struct ata_device *adev)
332{
333 u8 active, recover;
334 struct ata_timing t;
335
336 /* Get the timing data in cycles. For now play safe at 50Mhz */
337 ata_timing_compute(adev, adev->pio_mode, &t, 20000, 1000);
338
339 active = FIT(t.active, 2, 15);
340 recover = FIT(t.recover, 4, 15);
341
342 inb(0x3E6);
343 inb(0x3E6);
344 inb(0x3E6);
345 inb(0x3E6);
346
Tejun Heo0d5ff562007-02-01 15:06:36 +0900347 iowrite8(recover << 4 | active, ap->ioaddr.device_addr);
348 ioread8(ap->ioaddr.status_addr);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400349}
350
351static struct ata_port_operations ht6560a_port_ops = {
352 .set_piomode = ht6560a_set_piomode,
353
354 .port_disable = ata_port_disable,
355 .tf_load = ata_tf_load,
356 .tf_read = ata_tf_read,
357 .check_status = ata_check_status,
358 .exec_command = ata_exec_command,
359 .dev_select = ata_std_dev_select,
360
Jeff Garzikbf7551c2007-03-02 18:09:05 -0500361 .freeze = ata_bmdma_freeze,
362 .thaw = ata_bmdma_thaw,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400363 .error_handler = ata_bmdma_error_handler,
Jeff Garzikbf7551c2007-03-02 18:09:05 -0500364 .post_internal_cmd = ata_bmdma_post_internal_cmd,
Jeff Garzika73984a2007-03-09 08:37:46 -0500365 .cable_detect = ata_cable_40wire,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400366
367 .qc_prep = ata_qc_prep,
368 .qc_issue = ata_qc_issue_prot,
Jeff Garzikbda30282006-09-27 05:41:13 -0400369
Tejun Heo0d5ff562007-02-01 15:06:36 +0900370 .data_xfer = ata_data_xfer, /* Check vlb/noirq */
Jeff Garzik669a5db2006-08-29 18:12:40 -0400371
372 .irq_handler = ata_interrupt,
373 .irq_clear = ata_bmdma_irq_clear,
Akira Iguchi246ce3b2007-01-26 16:27:58 +0900374 .irq_on = ata_irq_on,
375 .irq_ack = ata_irq_ack,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400376
377 .port_start = ata_port_start,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400378};
379
380/*
381 * Holtek 6560B support
382 *
383 * This controller supports PIO0 to PIO4. We honour the BIOS/jumper FIFO setting
384 * unless we see an ATAPI device in which case we force it off.
385 *
386 * FIXME: need to implement 2nd channel support.
387 */
388
389static void ht6560b_set_piomode(struct ata_port *ap, struct ata_device *adev)
390{
391 u8 active, recover;
392 struct ata_timing t;
393
394 /* Get the timing data in cycles. For now play safe at 50Mhz */
395 ata_timing_compute(adev, adev->pio_mode, &t, 20000, 1000);
396
397 active = FIT(t.active, 2, 15);
398 recover = FIT(t.recover, 2, 16);
399 recover &= 0x15;
400
401 inb(0x3E6);
402 inb(0x3E6);
403 inb(0x3E6);
404 inb(0x3E6);
405
Tejun Heo0d5ff562007-02-01 15:06:36 +0900406 iowrite8(recover << 4 | active, ap->ioaddr.device_addr);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400407
408 if (adev->class != ATA_DEV_ATA) {
409 u8 rconf = inb(0x3E6);
410 if (rconf & 0x24) {
411 rconf &= ~ 0x24;
412 outb(rconf, 0x3E6);
413 }
414 }
Tejun Heo0d5ff562007-02-01 15:06:36 +0900415 ioread8(ap->ioaddr.status_addr);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400416}
417
418static struct ata_port_operations ht6560b_port_ops = {
419 .set_piomode = ht6560b_set_piomode,
420
421 .port_disable = ata_port_disable,
422 .tf_load = ata_tf_load,
423 .tf_read = ata_tf_read,
424 .check_status = ata_check_status,
425 .exec_command = ata_exec_command,
426 .dev_select = ata_std_dev_select,
427
Jeff Garzikbf7551c2007-03-02 18:09:05 -0500428 .freeze = ata_bmdma_freeze,
429 .thaw = ata_bmdma_thaw,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400430 .error_handler = ata_bmdma_error_handler,
Jeff Garzikbf7551c2007-03-02 18:09:05 -0500431 .post_internal_cmd = ata_bmdma_post_internal_cmd,
Jeff Garzika73984a2007-03-09 08:37:46 -0500432 .cable_detect = ata_cable_40wire,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400433
434 .qc_prep = ata_qc_prep,
435 .qc_issue = ata_qc_issue_prot,
Jeff Garzikbda30282006-09-27 05:41:13 -0400436
Tejun Heo0d5ff562007-02-01 15:06:36 +0900437 .data_xfer = ata_data_xfer, /* FIXME: Check 32bit and noirq */
Jeff Garzik669a5db2006-08-29 18:12:40 -0400438
439 .irq_handler = ata_interrupt,
440 .irq_clear = ata_bmdma_irq_clear,
Akira Iguchi246ce3b2007-01-26 16:27:58 +0900441 .irq_on = ata_irq_on,
442 .irq_ack = ata_irq_ack,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400443
444 .port_start = ata_port_start,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400445};
446
447/*
448 * Opti core chipset helpers
449 */
Jeff Garzik85cd7252006-08-31 00:03:49 -0400450
Jeff Garzik669a5db2006-08-29 18:12:40 -0400451/**
452 * opti_syscfg - read OPTI chipset configuration
453 * @reg: Configuration register to read
454 *
455 * Returns the value of an OPTI system board configuration register.
456 */
457
458static u8 opti_syscfg(u8 reg)
459{
460 unsigned long flags;
461 u8 r;
Jeff Garzik85cd7252006-08-31 00:03:49 -0400462
Jeff Garzik669a5db2006-08-29 18:12:40 -0400463 /* Uniprocessor chipset and must force cycles adjancent */
464 local_irq_save(flags);
465 outb(reg, 0x22);
466 r = inb(0x24);
467 local_irq_restore(flags);
468 return r;
469}
470
471/*
472 * Opti 82C611A
473 *
474 * This controller supports PIO0 to PIO3.
475 */
476
477static void opti82c611a_set_piomode(struct ata_port *ap, struct ata_device *adev)
478{
479 u8 active, recover, setup;
480 struct ata_timing t;
481 struct ata_device *pair = ata_dev_pair(adev);
482 int clock;
483 int khz[4] = { 50000, 40000, 33000, 25000 };
484 u8 rc;
485
486 /* Enter configuration mode */
Tejun Heo0d5ff562007-02-01 15:06:36 +0900487 ioread16(ap->ioaddr.error_addr);
488 ioread16(ap->ioaddr.error_addr);
489 iowrite8(3, ap->ioaddr.nsect_addr);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400490
491 /* Read VLB clock strapping */
Tejun Heo0d5ff562007-02-01 15:06:36 +0900492 clock = 1000000000 / khz[ioread8(ap->ioaddr.lbah_addr) & 0x03];
Jeff Garzik669a5db2006-08-29 18:12:40 -0400493
494 /* Get the timing data in cycles */
495 ata_timing_compute(adev, adev->pio_mode, &t, clock, 1000);
496
497 /* Setup timing is shared */
498 if (pair) {
499 struct ata_timing tp;
500 ata_timing_compute(pair, pair->pio_mode, &tp, clock, 1000);
501
502 ata_timing_merge(&t, &tp, &t, ATA_TIMING_SETUP);
503 }
504
505 active = FIT(t.active, 2, 17) - 2;
506 recover = FIT(t.recover, 1, 16) - 1;
507 setup = FIT(t.setup, 1, 4) - 1;
508
509 /* Select the right timing bank for write timing */
Tejun Heo0d5ff562007-02-01 15:06:36 +0900510 rc = ioread8(ap->ioaddr.lbal_addr);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400511 rc &= 0x7F;
512 rc |= (adev->devno << 7);
Tejun Heo0d5ff562007-02-01 15:06:36 +0900513 iowrite8(rc, ap->ioaddr.lbal_addr);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400514
515 /* Write the timings */
Tejun Heo0d5ff562007-02-01 15:06:36 +0900516 iowrite8(active << 4 | recover, ap->ioaddr.error_addr);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400517
518 /* Select the right bank for read timings, also
519 load the shared timings for address */
Tejun Heo0d5ff562007-02-01 15:06:36 +0900520 rc = ioread8(ap->ioaddr.device_addr);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400521 rc &= 0xC0;
522 rc |= adev->devno; /* Index select */
523 rc |= (setup << 4) | 0x04;
Tejun Heo0d5ff562007-02-01 15:06:36 +0900524 iowrite8(rc, ap->ioaddr.device_addr);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400525
526 /* Load the read timings */
Tejun Heo0d5ff562007-02-01 15:06:36 +0900527 iowrite8(active << 4 | recover, ap->ioaddr.data_addr);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400528
529 /* Ensure the timing register mode is right */
Tejun Heo0d5ff562007-02-01 15:06:36 +0900530 rc = ioread8(ap->ioaddr.lbal_addr);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400531 rc &= 0x73;
532 rc |= 0x84;
Tejun Heo0d5ff562007-02-01 15:06:36 +0900533 iowrite8(rc, ap->ioaddr.lbal_addr);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400534
535 /* Exit command mode */
Tejun Heo0d5ff562007-02-01 15:06:36 +0900536 iowrite8(0x83, ap->ioaddr.nsect_addr);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400537}
538
539
540static struct ata_port_operations opti82c611a_port_ops = {
541 .set_piomode = opti82c611a_set_piomode,
542
543 .port_disable = ata_port_disable,
544 .tf_load = ata_tf_load,
545 .tf_read = ata_tf_read,
546 .check_status = ata_check_status,
547 .exec_command = ata_exec_command,
548 .dev_select = ata_std_dev_select,
549
Jeff Garzikbf7551c2007-03-02 18:09:05 -0500550 .freeze = ata_bmdma_freeze,
551 .thaw = ata_bmdma_thaw,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400552 .error_handler = ata_bmdma_error_handler,
Jeff Garzikbf7551c2007-03-02 18:09:05 -0500553 .post_internal_cmd = ata_bmdma_post_internal_cmd,
Jeff Garzika73984a2007-03-09 08:37:46 -0500554 .cable_detect = ata_cable_40wire,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400555
556 .qc_prep = ata_qc_prep,
557 .qc_issue = ata_qc_issue_prot,
Jeff Garzikbda30282006-09-27 05:41:13 -0400558
Tejun Heo0d5ff562007-02-01 15:06:36 +0900559 .data_xfer = ata_data_xfer,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400560
561 .irq_handler = ata_interrupt,
562 .irq_clear = ata_bmdma_irq_clear,
Akira Iguchi246ce3b2007-01-26 16:27:58 +0900563 .irq_on = ata_irq_on,
564 .irq_ack = ata_irq_ack,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400565
566 .port_start = ata_port_start,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400567};
568
569/*
570 * Opti 82C465MV
571 *
572 * This controller supports PIO0 to PIO3. Unlike the 611A the MVB
573 * version is dual channel but doesn't have a lot of unique registers.
574 */
575
576static void opti82c46x_set_piomode(struct ata_port *ap, struct ata_device *adev)
577{
578 u8 active, recover, setup;
579 struct ata_timing t;
580 struct ata_device *pair = ata_dev_pair(adev);
581 int clock;
582 int khz[4] = { 50000, 40000, 33000, 25000 };
583 u8 rc;
584 u8 sysclk;
585
586 /* Get the clock */
587 sysclk = opti_syscfg(0xAC) & 0xC0; /* BIOS set */
588
589 /* Enter configuration mode */
Tejun Heo0d5ff562007-02-01 15:06:36 +0900590 ioread16(ap->ioaddr.error_addr);
591 ioread16(ap->ioaddr.error_addr);
592 iowrite8(3, ap->ioaddr.nsect_addr);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400593
594 /* Read VLB clock strapping */
595 clock = 1000000000 / khz[sysclk];
596
597 /* Get the timing data in cycles */
598 ata_timing_compute(adev, adev->pio_mode, &t, clock, 1000);
599
600 /* Setup timing is shared */
601 if (pair) {
602 struct ata_timing tp;
603 ata_timing_compute(pair, pair->pio_mode, &tp, clock, 1000);
604
605 ata_timing_merge(&t, &tp, &t, ATA_TIMING_SETUP);
606 }
607
608 active = FIT(t.active, 2, 17) - 2;
609 recover = FIT(t.recover, 1, 16) - 1;
610 setup = FIT(t.setup, 1, 4) - 1;
611
612 /* Select the right timing bank for write timing */
Tejun Heo0d5ff562007-02-01 15:06:36 +0900613 rc = ioread8(ap->ioaddr.lbal_addr);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400614 rc &= 0x7F;
615 rc |= (adev->devno << 7);
Tejun Heo0d5ff562007-02-01 15:06:36 +0900616 iowrite8(rc, ap->ioaddr.lbal_addr);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400617
618 /* Write the timings */
Tejun Heo0d5ff562007-02-01 15:06:36 +0900619 iowrite8(active << 4 | recover, ap->ioaddr.error_addr);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400620
621 /* Select the right bank for read timings, also
622 load the shared timings for address */
Tejun Heo0d5ff562007-02-01 15:06:36 +0900623 rc = ioread8(ap->ioaddr.device_addr);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400624 rc &= 0xC0;
625 rc |= adev->devno; /* Index select */
626 rc |= (setup << 4) | 0x04;
Tejun Heo0d5ff562007-02-01 15:06:36 +0900627 iowrite8(rc, ap->ioaddr.device_addr);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400628
629 /* Load the read timings */
Tejun Heo0d5ff562007-02-01 15:06:36 +0900630 iowrite8(active << 4 | recover, ap->ioaddr.data_addr);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400631
632 /* Ensure the timing register mode is right */
Tejun Heo0d5ff562007-02-01 15:06:36 +0900633 rc = ioread8(ap->ioaddr.lbal_addr);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400634 rc &= 0x73;
635 rc |= 0x84;
Tejun Heo0d5ff562007-02-01 15:06:36 +0900636 iowrite8(rc, ap->ioaddr.lbal_addr);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400637
638 /* Exit command mode */
Tejun Heo0d5ff562007-02-01 15:06:36 +0900639 iowrite8(0x83, ap->ioaddr.nsect_addr);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400640
641 /* We need to know this for quad device on the MVB */
642 ap->host->private_data = ap;
643}
644
645/**
646 * opt82c465mv_qc_issue_prot - command issue
647 * @qc: command pending
648 *
649 * Called when the libata layer is about to issue a command. We wrap
650 * this interface so that we can load the correct ATA timings. The
651 * MVB has a single set of timing registers and these are shared
652 * across channels. As there are two registers we really ought to
653 * track the last two used values as a sort of register window. For
654 * now we just reload on a channel switch. On the single channel
655 * setup this condition never fires so we do nothing extra.
656 *
657 * FIXME: dual channel needs ->serialize support
658 */
659
660static unsigned int opti82c46x_qc_issue_prot(struct ata_queued_cmd *qc)
661{
662 struct ata_port *ap = qc->ap;
663 struct ata_device *adev = qc->dev;
664
665 /* If timings are set and for the wrong channel (2nd test is
666 due to a libata shortcoming and will eventually go I hope) */
667 if (ap->host->private_data != ap->host
668 && ap->host->private_data != NULL)
669 opti82c46x_set_piomode(ap, adev);
670
671 return ata_qc_issue_prot(qc);
672}
673
674static struct ata_port_operations opti82c46x_port_ops = {
675 .set_piomode = opti82c46x_set_piomode,
676
677 .port_disable = ata_port_disable,
678 .tf_load = ata_tf_load,
679 .tf_read = ata_tf_read,
680 .check_status = ata_check_status,
681 .exec_command = ata_exec_command,
682 .dev_select = ata_std_dev_select,
683
Jeff Garzikbf7551c2007-03-02 18:09:05 -0500684 .freeze = ata_bmdma_freeze,
685 .thaw = ata_bmdma_thaw,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400686 .error_handler = ata_bmdma_error_handler,
Jeff Garzikbf7551c2007-03-02 18:09:05 -0500687 .post_internal_cmd = ata_bmdma_post_internal_cmd,
Jeff Garzika73984a2007-03-09 08:37:46 -0500688 .cable_detect = ata_cable_40wire,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400689
690 .qc_prep = ata_qc_prep,
691 .qc_issue = opti82c46x_qc_issue_prot,
Jeff Garzikbda30282006-09-27 05:41:13 -0400692
Tejun Heo0d5ff562007-02-01 15:06:36 +0900693 .data_xfer = ata_data_xfer,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400694
695 .irq_handler = ata_interrupt,
696 .irq_clear = ata_bmdma_irq_clear,
Akira Iguchi246ce3b2007-01-26 16:27:58 +0900697 .irq_on = ata_irq_on,
698 .irq_ack = ata_irq_ack,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400699
700 .port_start = ata_port_start,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400701};
702
703
704/**
705 * legacy_init_one - attach a legacy interface
706 * @port: port number
707 * @io: I/O port start
708 * @ctrl: control port
709 * @irq: interrupt line
710 *
711 * Register an ISA bus IDE interface. Such interfaces are PIO and we
712 * assume do not support IRQ sharing.
713 */
714
715static __init int legacy_init_one(int port, unsigned long io, unsigned long ctrl, int irq)
716{
717 struct legacy_data *ld = &legacy_data[nr_legacy_host];
Tejun Heo5d728822007-04-17 23:44:08 +0900718 struct ata_host *host;
719 struct ata_port *ap;
Jeff Garzik669a5db2006-08-29 18:12:40 -0400720 struct platform_device *pdev;
Jeff Garzik669a5db2006-08-29 18:12:40 -0400721 struct ata_port_operations *ops = &legacy_port_ops;
Tejun Heo0d5ff562007-02-01 15:06:36 +0900722 void __iomem *io_addr, *ctrl_addr;
Jeff Garzik669a5db2006-08-29 18:12:40 -0400723 int pio_modes = pio_mask;
724 u32 mask = (1 << port);
Alan Coxf834e492007-02-07 13:46:00 -0800725 u32 iordy = (iordy_mask & mask) ? 0: ATA_FLAG_NO_IORDY;
Tejun Heo24dc5f32007-01-20 16:00:28 +0900726 int ret;
Jeff Garzik669a5db2006-08-29 18:12:40 -0400727
728 pdev = platform_device_register_simple(DRV_NAME, nr_legacy_host, NULL, 0);
Tejun Heo24dc5f32007-01-20 16:00:28 +0900729 if (IS_ERR(pdev))
730 return PTR_ERR(pdev);
731
732 ret = -EBUSY;
733 if (devm_request_region(&pdev->dev, io, 8, "pata_legacy") == NULL ||
734 devm_request_region(&pdev->dev, ctrl, 1, "pata_legacy") == NULL)
735 goto fail;
Jeff Garzik669a5db2006-08-29 18:12:40 -0400736
Tejun Heo0d5ff562007-02-01 15:06:36 +0900737 ret = -ENOMEM;
738 io_addr = devm_ioport_map(&pdev->dev, io, 8);
739 ctrl_addr = devm_ioport_map(&pdev->dev, ctrl, 1);
740 if (!io_addr || !ctrl_addr)
741 goto fail;
742
Jeff Garzik669a5db2006-08-29 18:12:40 -0400743 if (ht6560a & mask) {
744 ops = &ht6560a_port_ops;
745 pio_modes = 0x07;
Alan Coxf834e492007-02-07 13:46:00 -0800746 iordy = ATA_FLAG_NO_IORDY;
Jeff Garzik669a5db2006-08-29 18:12:40 -0400747 }
748 if (ht6560b & mask) {
749 ops = &ht6560b_port_ops;
750 pio_modes = 0x1F;
751 }
752 if (opti82c611a & mask) {
753 ops = &opti82c611a_port_ops;
754 pio_modes = 0x0F;
755 }
756 if (opti82c46x & mask) {
757 ops = &opti82c46x_port_ops;
758 pio_modes = 0x0F;
759 }
760
761 /* Probe for automatically detectable controllers */
Jeff Garzik85cd7252006-08-31 00:03:49 -0400762
Jeff Garzik669a5db2006-08-29 18:12:40 -0400763 if (io == 0x1F0 && ops == &legacy_port_ops) {
764 unsigned long flags;
765
766 local_irq_save(flags);
767
768 /* Probes */
769 inb(0x1F5);
770 outb(inb(0x1F2) | 0x80, 0x1F2);
771 inb(0x1F2);
772 inb(0x3F6);
773 inb(0x3F6);
774 inb(0x1F2);
775 inb(0x1F2);
776
777 if ((inb(0x1F2) & 0x80) == 0) {
778 /* PDC20230c or 20630 ? */
779 printk(KERN_INFO "PDC20230-C/20630 VLB ATA controller detected.\n");
780 pio_modes = 0x07;
781 ops = &pdc20230_port_ops;
Alan Coxf834e492007-02-07 13:46:00 -0800782 iordy = ATA_FLAG_NO_IORDY;
Jeff Garzik669a5db2006-08-29 18:12:40 -0400783 udelay(100);
784 inb(0x1F5);
785 } else {
786 outb(0x55, 0x1F2);
787 inb(0x1F2);
788 inb(0x1F2);
789 if (inb(0x1F2) == 0x00) {
790 printk(KERN_INFO "PDC20230-B VLB ATA controller detected.\n");
791 }
792 }
793 local_irq_restore(flags);
794 }
795
796
797 /* Chip does mode setting by command snooping */
798 if (ops == &legacy_port_ops && (autospeed & mask))
799 ops = &simple_port_ops;
Alan Coxf834e492007-02-07 13:46:00 -0800800
Tejun Heo5d728822007-04-17 23:44:08 +0900801 ret = -ENOMEM;
802 host = ata_host_alloc(&pdev->dev, 1);
803 if (!host)
804 goto fail;
805 ap = host->ports[0];
Jeff Garzik669a5db2006-08-29 18:12:40 -0400806
Tejun Heo5d728822007-04-17 23:44:08 +0900807 ap->ops = ops;
808 ap->pio_mask = pio_modes;
809 ap->flags |= ATA_FLAG_SLAVE_POSS | iordy;
810 ap->ioaddr.cmd_addr = io_addr;
811 ap->ioaddr.altstatus_addr = ctrl_addr;
812 ap->ioaddr.ctl_addr = ctrl_addr;
813 ata_std_ports(&ap->ioaddr);
814 ap->private_data = ld;
815
816 ret = ata_host_activate(host, irq, ata_interrupt, 0, &legacy_sht);
817 if (ret)
Jeff Garzik669a5db2006-08-29 18:12:40 -0400818 goto fail;
Tejun Heo24dc5f32007-01-20 16:00:28 +0900819
Jeff Garzik669a5db2006-08-29 18:12:40 -0400820 legacy_host[nr_legacy_host++] = dev_get_drvdata(&pdev->dev);
821 ld->platform_dev = pdev;
822 return 0;
823
824fail:
825 platform_device_unregister(pdev);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400826 return ret;
827}
828
829/**
830 * legacy_check_special_cases - ATA special cases
831 * @p: PCI device to check
832 * @master: set this if we find an ATA master
833 * @master: set this if we find an ATA secondary
834 *
835 * A small number of vendors implemented early PCI ATA interfaces on bridge logic
836 * without the ATA interface being PCI visible. Where we have a matching PCI driver
837 * we must skip the relevant device here. If we don't know about it then the legacy
838 * driver is the right driver anyway.
839 */
840
841static void legacy_check_special_cases(struct pci_dev *p, int *primary, int *secondary)
842{
843 /* Cyrix CS5510 pre SFF MWDMA ATA on the bridge */
844 if (p->vendor == 0x1078 && p->device == 0x0000) {
845 *primary = *secondary = 1;
846 return;
847 }
848 /* Cyrix CS5520 pre SFF MWDMA ATA on the bridge */
849 if (p->vendor == 0x1078 && p->device == 0x0002) {
850 *primary = *secondary = 1;
851 return;
852 }
853 /* Intel MPIIX - PIO ATA on non PCI side of bridge */
854 if (p->vendor == 0x8086 && p->device == 0x1234) {
855 u16 r;
856 pci_read_config_word(p, 0x6C, &r);
857 if (r & 0x8000) { /* ATA port enabled */
858 if (r & 0x4000)
859 *secondary = 1;
860 else
861 *primary = 1;
862 }
863 return;
864 }
865}
866
867
868/**
869 * legacy_init - attach legacy interfaces
870 *
871 * Attach legacy IDE interfaces by scanning the usual IRQ/port suspects.
872 * Right now we do not scan the ide0 and ide1 address but should do so
873 * for non PCI systems or systems with no PCI IDE legacy mode devices.
874 * If you fix that note there are special cases to consider like VLB
875 * drivers and CS5510/20.
876 */
877
878static __init int legacy_init(void)
879{
880 int i;
881 int ct = 0;
882 int primary = 0;
883 int secondary = 0;
884 int last_port = NR_HOST;
885
886 struct pci_dev *p = NULL;
887
888 for_each_pci_dev(p) {
889 int r;
890 /* Check for any overlap of the system ATA mappings. Native mode controllers
891 stuck on these addresses or some devices in 'raid' mode won't be found by
892 the storage class test */
893 for (r = 0; r < 6; r++) {
894 if (pci_resource_start(p, r) == 0x1f0)
895 primary = 1;
896 if (pci_resource_start(p, r) == 0x170)
897 secondary = 1;
898 }
899 /* Check for special cases */
900 legacy_check_special_cases(p, &primary, &secondary);
901
902 /* If PCI bus is present then don't probe for tertiary legacy ports */
903 if (probe_all == 0)
904 last_port = 2;
905 }
906
Jeff Garzik85cd7252006-08-31 00:03:49 -0400907 /* If an OPTI 82C46X is present find out where the channels are */
Jeff Garzik669a5db2006-08-29 18:12:40 -0400908 if (opti82c46x) {
909 static const char *optis[4] = {
910 "3/463MV", "5MV",
911 "5MVA", "5MVB"
912 };
913 u8 chans = 1;
914 u8 ctrl = (opti_syscfg(0x30) & 0xC0) >> 6;
Jeff Garzik85cd7252006-08-31 00:03:49 -0400915
Jeff Garzik669a5db2006-08-29 18:12:40 -0400916 opti82c46x = 3; /* Assume master and slave first */
917 printk(KERN_INFO DRV_NAME ": Opti 82C46%s chipset support.\n", optis[ctrl]);
918 if (ctrl == 3)
919 chans = (opti_syscfg(0x3F) & 0x20) ? 2 : 1;
920 ctrl = opti_syscfg(0xAC);
921 /* Check enabled and this port is the 465MV port. On the
922 MVB we may have two channels */
923 if (ctrl & 8) {
924 if (ctrl & 4)
925 opti82c46x = 2; /* Slave */
926 else
927 opti82c46x = 1; /* Master */
928 if (chans == 2)
929 opti82c46x = 3; /* Master and Slave */
930 } /* Slave only */
931 else if (chans == 1)
932 opti82c46x = 1;
933 }
934
935 for (i = 0; i < last_port; i++) {
936 /* Skip primary if we have seen a PCI one */
937 if (i == 0 && primary == 1)
938 continue;
939 /* Skip secondary if we have seen a PCI one */
940 if (i == 1 && secondary == 1)
941 continue;
942 if (legacy_init_one(i, legacy_port[i],
943 legacy_port[i] + 0x0206,
944 legacy_irq[i]) == 0)
945 ct++;
946 }
947 if (ct != 0)
948 return 0;
949 return -ENODEV;
950}
951
952static __exit void legacy_exit(void)
953{
954 int i;
955
956 for (i = 0; i < nr_legacy_host; i++) {
957 struct legacy_data *ld = &legacy_data[i];
Tejun Heo24dc5f32007-01-20 16:00:28 +0900958
959 ata_host_detach(legacy_host[i]);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400960 platform_device_unregister(ld->platform_dev);
961 if (ld->timing)
962 release_region(ld->timing, 2);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400963 }
964}
965
966MODULE_AUTHOR("Alan Cox");
967MODULE_DESCRIPTION("low-level driver for legacy ATA");
968MODULE_LICENSE("GPL");
969MODULE_VERSION(DRV_VERSION);
970
971module_param(probe_all, int, 0);
972module_param(autospeed, int, 0);
973module_param(ht6560a, int, 0);
974module_param(ht6560b, int, 0);
975module_param(opti82c611a, int, 0);
976module_param(opti82c46x, int, 0);
977module_param(pio_mask, int, 0);
Alan Coxf834e492007-02-07 13:46:00 -0800978module_param(iordy_mask, int, 0);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400979
980module_init(legacy_init);
981module_exit(legacy_exit);
982