blob: 17159b5e1e43d4d48b17aa76568bb77247499b58 [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
Tejun Heo02607312007-08-06 18:36:23 +090099 * @link: IDE link
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
Tejun Heo02607312007-08-06 18:36:23 +0900110static int legacy_set_mode(struct ata_link *link, 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 Heo02607312007-08-06 18:36:23 +0900114 ata_link_for_each_dev(dev, 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 = {
Jeff Garzik669a5db2006-08-29 18:12:40 -0400153 .tf_load = ata_tf_load,
154 .tf_read = ata_tf_read,
155 .check_status = ata_check_status,
156 .exec_command = ata_exec_command,
157 .dev_select = ata_std_dev_select,
158
159 .freeze = ata_bmdma_freeze,
160 .thaw = ata_bmdma_thaw,
161 .error_handler = ata_bmdma_error_handler,
162 .post_internal_cmd = ata_bmdma_post_internal_cmd,
Jeff Garzika73984a2007-03-09 08:37:46 -0500163 .cable_detect = ata_cable_40wire,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400164
165 .qc_prep = ata_qc_prep,
166 .qc_issue = ata_qc_issue_prot,
Jeff Garzikbda30282006-09-27 05:41:13 -0400167
Tejun Heo0d5ff562007-02-01 15:06:36 +0900168 .data_xfer = ata_data_xfer_noirq,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400169
170 .irq_handler = ata_interrupt,
171 .irq_clear = ata_bmdma_irq_clear,
Akira Iguchi246ce3b2007-01-26 16:27:58 +0900172 .irq_on = ata_irq_on,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400173
174 .port_start = ata_port_start,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400175};
176
177static struct ata_port_operations legacy_port_ops = {
178 .set_mode = legacy_set_mode,
179
Jeff Garzik669a5db2006-08-29 18:12:40 -0400180 .tf_load = ata_tf_load,
181 .tf_read = ata_tf_read,
182 .check_status = ata_check_status,
183 .exec_command = ata_exec_command,
184 .dev_select = ata_std_dev_select,
Jeff Garzika73984a2007-03-09 08:37:46 -0500185 .cable_detect = ata_cable_40wire,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400186
Jeff Garzikbf7551c2007-03-02 18:09:05 -0500187 .freeze = ata_bmdma_freeze,
188 .thaw = ata_bmdma_thaw,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400189 .error_handler = ata_bmdma_error_handler,
Jeff Garzikbf7551c2007-03-02 18:09:05 -0500190 .post_internal_cmd = ata_bmdma_post_internal_cmd,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400191
192 .qc_prep = ata_qc_prep,
193 .qc_issue = ata_qc_issue_prot,
Jeff Garzikbda30282006-09-27 05:41:13 -0400194
Tejun Heo0d5ff562007-02-01 15:06:36 +0900195 .data_xfer = ata_data_xfer_noirq,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400196
197 .irq_handler = ata_interrupt,
198 .irq_clear = ata_bmdma_irq_clear,
Akira Iguchi246ce3b2007-01-26 16:27:58 +0900199 .irq_on = ata_irq_on,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400200
201 .port_start = ata_port_start,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400202};
203
204/*
205 * Promise 20230C and 20620 support
206 *
207 * This controller supports PIO0 to PIO2. We set PIO timings conservatively to
208 * allow for 50MHz Vesa Local Bus. The 20620 DMA support is weird being DMA to
209 * controller and PIO'd to the host and not supported.
210 */
211
212static void pdc20230_set_piomode(struct ata_port *ap, struct ata_device *adev)
213{
214 int tries = 5;
215 int pio = adev->pio_mode - XFER_PIO_0;
216 u8 rt;
217 unsigned long flags;
Jeff Garzik85cd7252006-08-31 00:03:49 -0400218
Jeff Garzik669a5db2006-08-29 18:12:40 -0400219 /* Safe as UP only. Force I/Os to occur together */
Jeff Garzik85cd7252006-08-31 00:03:49 -0400220
Jeff Garzik669a5db2006-08-29 18:12:40 -0400221 local_irq_save(flags);
Jeff Garzik85cd7252006-08-31 00:03:49 -0400222
Jeff Garzik669a5db2006-08-29 18:12:40 -0400223 /* Unlock the control interface */
224 do
225 {
226 inb(0x1F5);
227 outb(inb(0x1F2) | 0x80, 0x1F2);
228 inb(0x1F2);
229 inb(0x3F6);
230 inb(0x3F6);
231 inb(0x1F2);
232 inb(0x1F2);
233 }
234 while((inb(0x1F2) & 0x80) && --tries);
235
236 local_irq_restore(flags);
Jeff Garzik85cd7252006-08-31 00:03:49 -0400237
Jeff Garzik669a5db2006-08-29 18:12:40 -0400238 outb(inb(0x1F4) & 0x07, 0x1F4);
239
240 rt = inb(0x1F3);
241 rt &= 0x07 << (3 * adev->devno);
242 if (pio)
243 rt |= (1 + 3 * pio) << (3 * adev->devno);
244
245 udelay(100);
246 outb(inb(0x1F2) | 0x01, 0x1F2);
247 udelay(100);
248 inb(0x1F5);
249
250}
251
252static void pdc_data_xfer_vlb(struct ata_device *adev, unsigned char *buf, unsigned int buflen, int write_data)
253{
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900254 struct ata_port *ap = adev->link->ap;
Jeff Garzik669a5db2006-08-29 18:12:40 -0400255 int slop = buflen & 3;
256 unsigned long flags;
257
258 if (ata_id_has_dword_io(adev->id)) {
259 local_irq_save(flags);
260
261 /* Perform the 32bit I/O synchronization sequence */
Tejun Heo0d5ff562007-02-01 15:06:36 +0900262 ioread8(ap->ioaddr.nsect_addr);
263 ioread8(ap->ioaddr.nsect_addr);
264 ioread8(ap->ioaddr.nsect_addr);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400265
266 /* Now the data */
267
268 if (write_data)
Tejun Heo0d5ff562007-02-01 15:06:36 +0900269 iowrite32_rep(ap->ioaddr.data_addr, buf, buflen >> 2);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400270 else
Tejun Heo0d5ff562007-02-01 15:06:36 +0900271 ioread32_rep(ap->ioaddr.data_addr, buf, buflen >> 2);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400272
273 if (unlikely(slop)) {
Al Virob50e56d2008-01-12 14:16:14 +0000274 __le32 pad = 0;
Jeff Garzik669a5db2006-08-29 18:12:40 -0400275 if (write_data) {
276 memcpy(&pad, buf + buflen - slop, slop);
Al Virob50e56d2008-01-12 14:16:14 +0000277 iowrite32(le32_to_cpu(pad), ap->ioaddr.data_addr);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400278 } else {
Al Virob50e56d2008-01-12 14:16:14 +0000279 pad = cpu_to_le32(ioread32(ap->ioaddr.data_addr));
Jeff Garzik669a5db2006-08-29 18:12:40 -0400280 memcpy(buf + buflen - slop, &pad, slop);
281 }
282 }
283 local_irq_restore(flags);
284 }
285 else
Tejun Heo0d5ff562007-02-01 15:06:36 +0900286 ata_data_xfer_noirq(adev, buf, buflen, write_data);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400287}
288
289static struct ata_port_operations pdc20230_port_ops = {
290 .set_piomode = pdc20230_set_piomode,
291
Jeff Garzik669a5db2006-08-29 18:12:40 -0400292 .tf_load = ata_tf_load,
293 .tf_read = ata_tf_read,
294 .check_status = ata_check_status,
295 .exec_command = ata_exec_command,
296 .dev_select = ata_std_dev_select,
297
Jeff Garzikbf7551c2007-03-02 18:09:05 -0500298 .freeze = ata_bmdma_freeze,
299 .thaw = ata_bmdma_thaw,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400300 .error_handler = ata_bmdma_error_handler,
Jeff Garzikbf7551c2007-03-02 18:09:05 -0500301 .post_internal_cmd = ata_bmdma_post_internal_cmd,
Jeff Garzika73984a2007-03-09 08:37:46 -0500302 .cable_detect = ata_cable_40wire,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400303
304 .qc_prep = ata_qc_prep,
305 .qc_issue = ata_qc_issue_prot,
Jeff Garzikbda30282006-09-27 05:41:13 -0400306
Jeff Garzik669a5db2006-08-29 18:12:40 -0400307 .data_xfer = pdc_data_xfer_vlb,
308
309 .irq_handler = ata_interrupt,
310 .irq_clear = ata_bmdma_irq_clear,
Akira Iguchi246ce3b2007-01-26 16:27:58 +0900311 .irq_on = ata_irq_on,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400312
313 .port_start = ata_port_start,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400314};
315
316/*
317 * Holtek 6560A support
318 *
319 * This controller supports PIO0 to PIO2 (no IORDY even though higher timings
320 * can be loaded).
321 */
322
323static void ht6560a_set_piomode(struct ata_port *ap, struct ata_device *adev)
324{
325 u8 active, recover;
326 struct ata_timing t;
327
328 /* Get the timing data in cycles. For now play safe at 50Mhz */
329 ata_timing_compute(adev, adev->pio_mode, &t, 20000, 1000);
330
331 active = FIT(t.active, 2, 15);
332 recover = FIT(t.recover, 4, 15);
333
334 inb(0x3E6);
335 inb(0x3E6);
336 inb(0x3E6);
337 inb(0x3E6);
338
Tejun Heo0d5ff562007-02-01 15:06:36 +0900339 iowrite8(recover << 4 | active, ap->ioaddr.device_addr);
340 ioread8(ap->ioaddr.status_addr);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400341}
342
343static struct ata_port_operations ht6560a_port_ops = {
344 .set_piomode = ht6560a_set_piomode,
345
Jeff Garzik669a5db2006-08-29 18:12:40 -0400346 .tf_load = ata_tf_load,
347 .tf_read = ata_tf_read,
348 .check_status = ata_check_status,
349 .exec_command = ata_exec_command,
350 .dev_select = ata_std_dev_select,
351
Jeff Garzikbf7551c2007-03-02 18:09:05 -0500352 .freeze = ata_bmdma_freeze,
353 .thaw = ata_bmdma_thaw,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400354 .error_handler = ata_bmdma_error_handler,
Jeff Garzikbf7551c2007-03-02 18:09:05 -0500355 .post_internal_cmd = ata_bmdma_post_internal_cmd,
Jeff Garzika73984a2007-03-09 08:37:46 -0500356 .cable_detect = ata_cable_40wire,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400357
358 .qc_prep = ata_qc_prep,
359 .qc_issue = ata_qc_issue_prot,
Jeff Garzikbda30282006-09-27 05:41:13 -0400360
Tejun Heo0d5ff562007-02-01 15:06:36 +0900361 .data_xfer = ata_data_xfer, /* Check vlb/noirq */
Jeff Garzik669a5db2006-08-29 18:12:40 -0400362
363 .irq_handler = ata_interrupt,
364 .irq_clear = ata_bmdma_irq_clear,
Akira Iguchi246ce3b2007-01-26 16:27:58 +0900365 .irq_on = ata_irq_on,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400366
367 .port_start = ata_port_start,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400368};
369
370/*
371 * Holtek 6560B support
372 *
373 * This controller supports PIO0 to PIO4. We honour the BIOS/jumper FIFO setting
374 * unless we see an ATAPI device in which case we force it off.
375 *
376 * FIXME: need to implement 2nd channel support.
377 */
378
379static void ht6560b_set_piomode(struct ata_port *ap, struct ata_device *adev)
380{
381 u8 active, recover;
382 struct ata_timing t;
383
384 /* Get the timing data in cycles. For now play safe at 50Mhz */
385 ata_timing_compute(adev, adev->pio_mode, &t, 20000, 1000);
386
387 active = FIT(t.active, 2, 15);
388 recover = FIT(t.recover, 2, 16);
389 recover &= 0x15;
390
391 inb(0x3E6);
392 inb(0x3E6);
393 inb(0x3E6);
394 inb(0x3E6);
395
Tejun Heo0d5ff562007-02-01 15:06:36 +0900396 iowrite8(recover << 4 | active, ap->ioaddr.device_addr);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400397
398 if (adev->class != ATA_DEV_ATA) {
399 u8 rconf = inb(0x3E6);
400 if (rconf & 0x24) {
401 rconf &= ~ 0x24;
402 outb(rconf, 0x3E6);
403 }
404 }
Tejun Heo0d5ff562007-02-01 15:06:36 +0900405 ioread8(ap->ioaddr.status_addr);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400406}
407
408static struct ata_port_operations ht6560b_port_ops = {
409 .set_piomode = ht6560b_set_piomode,
410
Jeff Garzik669a5db2006-08-29 18:12:40 -0400411 .tf_load = ata_tf_load,
412 .tf_read = ata_tf_read,
413 .check_status = ata_check_status,
414 .exec_command = ata_exec_command,
415 .dev_select = ata_std_dev_select,
416
Jeff Garzikbf7551c2007-03-02 18:09:05 -0500417 .freeze = ata_bmdma_freeze,
418 .thaw = ata_bmdma_thaw,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400419 .error_handler = ata_bmdma_error_handler,
Jeff Garzikbf7551c2007-03-02 18:09:05 -0500420 .post_internal_cmd = ata_bmdma_post_internal_cmd,
Jeff Garzika73984a2007-03-09 08:37:46 -0500421 .cable_detect = ata_cable_40wire,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400422
423 .qc_prep = ata_qc_prep,
424 .qc_issue = ata_qc_issue_prot,
Jeff Garzikbda30282006-09-27 05:41:13 -0400425
Tejun Heo0d5ff562007-02-01 15:06:36 +0900426 .data_xfer = ata_data_xfer, /* FIXME: Check 32bit and noirq */
Jeff Garzik669a5db2006-08-29 18:12:40 -0400427
428 .irq_handler = ata_interrupt,
429 .irq_clear = ata_bmdma_irq_clear,
Akira Iguchi246ce3b2007-01-26 16:27:58 +0900430 .irq_on = ata_irq_on,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400431
432 .port_start = ata_port_start,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400433};
434
435/*
436 * Opti core chipset helpers
437 */
Jeff Garzik85cd7252006-08-31 00:03:49 -0400438
Jeff Garzik669a5db2006-08-29 18:12:40 -0400439/**
440 * opti_syscfg - read OPTI chipset configuration
441 * @reg: Configuration register to read
442 *
443 * Returns the value of an OPTI system board configuration register.
444 */
445
446static u8 opti_syscfg(u8 reg)
447{
448 unsigned long flags;
449 u8 r;
Jeff Garzik85cd7252006-08-31 00:03:49 -0400450
Jeff Garzik669a5db2006-08-29 18:12:40 -0400451 /* Uniprocessor chipset and must force cycles adjancent */
452 local_irq_save(flags);
453 outb(reg, 0x22);
454 r = inb(0x24);
455 local_irq_restore(flags);
456 return r;
457}
458
459/*
460 * Opti 82C611A
461 *
462 * This controller supports PIO0 to PIO3.
463 */
464
465static void opti82c611a_set_piomode(struct ata_port *ap, struct ata_device *adev)
466{
467 u8 active, recover, setup;
468 struct ata_timing t;
469 struct ata_device *pair = ata_dev_pair(adev);
470 int clock;
471 int khz[4] = { 50000, 40000, 33000, 25000 };
472 u8 rc;
473
474 /* Enter configuration mode */
Tejun Heo0d5ff562007-02-01 15:06:36 +0900475 ioread16(ap->ioaddr.error_addr);
476 ioread16(ap->ioaddr.error_addr);
477 iowrite8(3, ap->ioaddr.nsect_addr);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400478
479 /* Read VLB clock strapping */
Tejun Heo0d5ff562007-02-01 15:06:36 +0900480 clock = 1000000000 / khz[ioread8(ap->ioaddr.lbah_addr) & 0x03];
Jeff Garzik669a5db2006-08-29 18:12:40 -0400481
482 /* Get the timing data in cycles */
483 ata_timing_compute(adev, adev->pio_mode, &t, clock, 1000);
484
485 /* Setup timing is shared */
486 if (pair) {
487 struct ata_timing tp;
488 ata_timing_compute(pair, pair->pio_mode, &tp, clock, 1000);
489
490 ata_timing_merge(&t, &tp, &t, ATA_TIMING_SETUP);
491 }
492
493 active = FIT(t.active, 2, 17) - 2;
494 recover = FIT(t.recover, 1, 16) - 1;
495 setup = FIT(t.setup, 1, 4) - 1;
496
497 /* Select the right timing bank for write timing */
Tejun Heo0d5ff562007-02-01 15:06:36 +0900498 rc = ioread8(ap->ioaddr.lbal_addr);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400499 rc &= 0x7F;
500 rc |= (adev->devno << 7);
Tejun Heo0d5ff562007-02-01 15:06:36 +0900501 iowrite8(rc, ap->ioaddr.lbal_addr);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400502
503 /* Write the timings */
Tejun Heo0d5ff562007-02-01 15:06:36 +0900504 iowrite8(active << 4 | recover, ap->ioaddr.error_addr);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400505
506 /* Select the right bank for read timings, also
507 load the shared timings for address */
Tejun Heo0d5ff562007-02-01 15:06:36 +0900508 rc = ioread8(ap->ioaddr.device_addr);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400509 rc &= 0xC0;
510 rc |= adev->devno; /* Index select */
511 rc |= (setup << 4) | 0x04;
Tejun Heo0d5ff562007-02-01 15:06:36 +0900512 iowrite8(rc, ap->ioaddr.device_addr);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400513
514 /* Load the read timings */
Tejun Heo0d5ff562007-02-01 15:06:36 +0900515 iowrite8(active << 4 | recover, ap->ioaddr.data_addr);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400516
517 /* Ensure the timing register mode is right */
Tejun Heo0d5ff562007-02-01 15:06:36 +0900518 rc = ioread8(ap->ioaddr.lbal_addr);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400519 rc &= 0x73;
520 rc |= 0x84;
Tejun Heo0d5ff562007-02-01 15:06:36 +0900521 iowrite8(rc, ap->ioaddr.lbal_addr);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400522
523 /* Exit command mode */
Tejun Heo0d5ff562007-02-01 15:06:36 +0900524 iowrite8(0x83, ap->ioaddr.nsect_addr);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400525}
526
527
528static struct ata_port_operations opti82c611a_port_ops = {
529 .set_piomode = opti82c611a_set_piomode,
530
Jeff Garzik669a5db2006-08-29 18:12:40 -0400531 .tf_load = ata_tf_load,
532 .tf_read = ata_tf_read,
533 .check_status = ata_check_status,
534 .exec_command = ata_exec_command,
535 .dev_select = ata_std_dev_select,
536
Jeff Garzikbf7551c2007-03-02 18:09:05 -0500537 .freeze = ata_bmdma_freeze,
538 .thaw = ata_bmdma_thaw,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400539 .error_handler = ata_bmdma_error_handler,
Jeff Garzikbf7551c2007-03-02 18:09:05 -0500540 .post_internal_cmd = ata_bmdma_post_internal_cmd,
Jeff Garzika73984a2007-03-09 08:37:46 -0500541 .cable_detect = ata_cable_40wire,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400542
543 .qc_prep = ata_qc_prep,
544 .qc_issue = ata_qc_issue_prot,
Jeff Garzikbda30282006-09-27 05:41:13 -0400545
Tejun Heo0d5ff562007-02-01 15:06:36 +0900546 .data_xfer = ata_data_xfer,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400547
548 .irq_handler = ata_interrupt,
549 .irq_clear = ata_bmdma_irq_clear,
Akira Iguchi246ce3b2007-01-26 16:27:58 +0900550 .irq_on = ata_irq_on,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400551
552 .port_start = ata_port_start,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400553};
554
555/*
556 * Opti 82C465MV
557 *
558 * This controller supports PIO0 to PIO3. Unlike the 611A the MVB
559 * version is dual channel but doesn't have a lot of unique registers.
560 */
561
562static void opti82c46x_set_piomode(struct ata_port *ap, struct ata_device *adev)
563{
564 u8 active, recover, setup;
565 struct ata_timing t;
566 struct ata_device *pair = ata_dev_pair(adev);
567 int clock;
568 int khz[4] = { 50000, 40000, 33000, 25000 };
569 u8 rc;
570 u8 sysclk;
571
572 /* Get the clock */
573 sysclk = opti_syscfg(0xAC) & 0xC0; /* BIOS set */
574
575 /* Enter configuration mode */
Tejun Heo0d5ff562007-02-01 15:06:36 +0900576 ioread16(ap->ioaddr.error_addr);
577 ioread16(ap->ioaddr.error_addr);
578 iowrite8(3, ap->ioaddr.nsect_addr);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400579
580 /* Read VLB clock strapping */
581 clock = 1000000000 / khz[sysclk];
582
583 /* Get the timing data in cycles */
584 ata_timing_compute(adev, adev->pio_mode, &t, clock, 1000);
585
586 /* Setup timing is shared */
587 if (pair) {
588 struct ata_timing tp;
589 ata_timing_compute(pair, pair->pio_mode, &tp, clock, 1000);
590
591 ata_timing_merge(&t, &tp, &t, ATA_TIMING_SETUP);
592 }
593
594 active = FIT(t.active, 2, 17) - 2;
595 recover = FIT(t.recover, 1, 16) - 1;
596 setup = FIT(t.setup, 1, 4) - 1;
597
598 /* Select the right timing bank for write timing */
Tejun Heo0d5ff562007-02-01 15:06:36 +0900599 rc = ioread8(ap->ioaddr.lbal_addr);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400600 rc &= 0x7F;
601 rc |= (adev->devno << 7);
Tejun Heo0d5ff562007-02-01 15:06:36 +0900602 iowrite8(rc, ap->ioaddr.lbal_addr);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400603
604 /* Write the timings */
Tejun Heo0d5ff562007-02-01 15:06:36 +0900605 iowrite8(active << 4 | recover, ap->ioaddr.error_addr);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400606
607 /* Select the right bank for read timings, also
608 load the shared timings for address */
Tejun Heo0d5ff562007-02-01 15:06:36 +0900609 rc = ioread8(ap->ioaddr.device_addr);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400610 rc &= 0xC0;
611 rc |= adev->devno; /* Index select */
612 rc |= (setup << 4) | 0x04;
Tejun Heo0d5ff562007-02-01 15:06:36 +0900613 iowrite8(rc, ap->ioaddr.device_addr);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400614
615 /* Load the read timings */
Tejun Heo0d5ff562007-02-01 15:06:36 +0900616 iowrite8(active << 4 | recover, ap->ioaddr.data_addr);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400617
618 /* Ensure the timing register mode is right */
Tejun Heo0d5ff562007-02-01 15:06:36 +0900619 rc = ioread8(ap->ioaddr.lbal_addr);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400620 rc &= 0x73;
621 rc |= 0x84;
Tejun Heo0d5ff562007-02-01 15:06:36 +0900622 iowrite8(rc, ap->ioaddr.lbal_addr);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400623
624 /* Exit command mode */
Tejun Heo0d5ff562007-02-01 15:06:36 +0900625 iowrite8(0x83, ap->ioaddr.nsect_addr);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400626
627 /* We need to know this for quad device on the MVB */
628 ap->host->private_data = ap;
629}
630
631/**
632 * opt82c465mv_qc_issue_prot - command issue
633 * @qc: command pending
634 *
635 * Called when the libata layer is about to issue a command. We wrap
636 * this interface so that we can load the correct ATA timings. The
637 * MVB has a single set of timing registers and these are shared
638 * across channels. As there are two registers we really ought to
639 * track the last two used values as a sort of register window. For
640 * now we just reload on a channel switch. On the single channel
641 * setup this condition never fires so we do nothing extra.
642 *
643 * FIXME: dual channel needs ->serialize support
644 */
645
646static unsigned int opti82c46x_qc_issue_prot(struct ata_queued_cmd *qc)
647{
648 struct ata_port *ap = qc->ap;
649 struct ata_device *adev = qc->dev;
650
651 /* If timings are set and for the wrong channel (2nd test is
652 due to a libata shortcoming and will eventually go I hope) */
653 if (ap->host->private_data != ap->host
654 && ap->host->private_data != NULL)
655 opti82c46x_set_piomode(ap, adev);
656
657 return ata_qc_issue_prot(qc);
658}
659
660static struct ata_port_operations opti82c46x_port_ops = {
661 .set_piomode = opti82c46x_set_piomode,
662
Jeff Garzik669a5db2006-08-29 18:12:40 -0400663 .tf_load = ata_tf_load,
664 .tf_read = ata_tf_read,
665 .check_status = ata_check_status,
666 .exec_command = ata_exec_command,
667 .dev_select = ata_std_dev_select,
668
Jeff Garzikbf7551c2007-03-02 18:09:05 -0500669 .freeze = ata_bmdma_freeze,
670 .thaw = ata_bmdma_thaw,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400671 .error_handler = ata_bmdma_error_handler,
Jeff Garzikbf7551c2007-03-02 18:09:05 -0500672 .post_internal_cmd = ata_bmdma_post_internal_cmd,
Jeff Garzika73984a2007-03-09 08:37:46 -0500673 .cable_detect = ata_cable_40wire,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400674
675 .qc_prep = ata_qc_prep,
676 .qc_issue = opti82c46x_qc_issue_prot,
Jeff Garzikbda30282006-09-27 05:41:13 -0400677
Tejun Heo0d5ff562007-02-01 15:06:36 +0900678 .data_xfer = ata_data_xfer,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400679
680 .irq_handler = ata_interrupt,
681 .irq_clear = ata_bmdma_irq_clear,
Akira Iguchi246ce3b2007-01-26 16:27:58 +0900682 .irq_on = ata_irq_on,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400683
684 .port_start = ata_port_start,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400685};
686
687
688/**
689 * legacy_init_one - attach a legacy interface
690 * @port: port number
691 * @io: I/O port start
692 * @ctrl: control port
693 * @irq: interrupt line
694 *
695 * Register an ISA bus IDE interface. Such interfaces are PIO and we
696 * assume do not support IRQ sharing.
697 */
698
699static __init int legacy_init_one(int port, unsigned long io, unsigned long ctrl, int irq)
700{
701 struct legacy_data *ld = &legacy_data[nr_legacy_host];
Tejun Heo5d728822007-04-17 23:44:08 +0900702 struct ata_host *host;
703 struct ata_port *ap;
Jeff Garzik669a5db2006-08-29 18:12:40 -0400704 struct platform_device *pdev;
Jeff Garzik669a5db2006-08-29 18:12:40 -0400705 struct ata_port_operations *ops = &legacy_port_ops;
Tejun Heo0d5ff562007-02-01 15:06:36 +0900706 void __iomem *io_addr, *ctrl_addr;
Jeff Garzik669a5db2006-08-29 18:12:40 -0400707 int pio_modes = pio_mask;
708 u32 mask = (1 << port);
Alan Coxf834e492007-02-07 13:46:00 -0800709 u32 iordy = (iordy_mask & mask) ? 0: ATA_FLAG_NO_IORDY;
Tejun Heo24dc5f32007-01-20 16:00:28 +0900710 int ret;
Jeff Garzik669a5db2006-08-29 18:12:40 -0400711
712 pdev = platform_device_register_simple(DRV_NAME, nr_legacy_host, NULL, 0);
Tejun Heo24dc5f32007-01-20 16:00:28 +0900713 if (IS_ERR(pdev))
714 return PTR_ERR(pdev);
715
716 ret = -EBUSY;
717 if (devm_request_region(&pdev->dev, io, 8, "pata_legacy") == NULL ||
718 devm_request_region(&pdev->dev, ctrl, 1, "pata_legacy") == NULL)
719 goto fail;
Jeff Garzik669a5db2006-08-29 18:12:40 -0400720
Tejun Heo0d5ff562007-02-01 15:06:36 +0900721 ret = -ENOMEM;
722 io_addr = devm_ioport_map(&pdev->dev, io, 8);
723 ctrl_addr = devm_ioport_map(&pdev->dev, ctrl, 1);
724 if (!io_addr || !ctrl_addr)
725 goto fail;
726
Jeff Garzik669a5db2006-08-29 18:12:40 -0400727 if (ht6560a & mask) {
728 ops = &ht6560a_port_ops;
729 pio_modes = 0x07;
Alan Coxf834e492007-02-07 13:46:00 -0800730 iordy = ATA_FLAG_NO_IORDY;
Jeff Garzik669a5db2006-08-29 18:12:40 -0400731 }
732 if (ht6560b & mask) {
733 ops = &ht6560b_port_ops;
734 pio_modes = 0x1F;
735 }
736 if (opti82c611a & mask) {
737 ops = &opti82c611a_port_ops;
738 pio_modes = 0x0F;
739 }
740 if (opti82c46x & mask) {
741 ops = &opti82c46x_port_ops;
742 pio_modes = 0x0F;
743 }
744
745 /* Probe for automatically detectable controllers */
Jeff Garzik85cd7252006-08-31 00:03:49 -0400746
Jeff Garzik669a5db2006-08-29 18:12:40 -0400747 if (io == 0x1F0 && ops == &legacy_port_ops) {
748 unsigned long flags;
749
750 local_irq_save(flags);
751
752 /* Probes */
753 inb(0x1F5);
754 outb(inb(0x1F2) | 0x80, 0x1F2);
755 inb(0x1F2);
756 inb(0x3F6);
757 inb(0x3F6);
758 inb(0x1F2);
759 inb(0x1F2);
760
761 if ((inb(0x1F2) & 0x80) == 0) {
762 /* PDC20230c or 20630 ? */
763 printk(KERN_INFO "PDC20230-C/20630 VLB ATA controller detected.\n");
764 pio_modes = 0x07;
765 ops = &pdc20230_port_ops;
Alan Coxf834e492007-02-07 13:46:00 -0800766 iordy = ATA_FLAG_NO_IORDY;
Jeff Garzik669a5db2006-08-29 18:12:40 -0400767 udelay(100);
768 inb(0x1F5);
769 } else {
770 outb(0x55, 0x1F2);
771 inb(0x1F2);
772 inb(0x1F2);
773 if (inb(0x1F2) == 0x00) {
774 printk(KERN_INFO "PDC20230-B VLB ATA controller detected.\n");
775 }
776 }
777 local_irq_restore(flags);
778 }
779
780
781 /* Chip does mode setting by command snooping */
782 if (ops == &legacy_port_ops && (autospeed & mask))
783 ops = &simple_port_ops;
Alan Coxf834e492007-02-07 13:46:00 -0800784
Tejun Heo5d728822007-04-17 23:44:08 +0900785 ret = -ENOMEM;
786 host = ata_host_alloc(&pdev->dev, 1);
787 if (!host)
788 goto fail;
789 ap = host->ports[0];
Jeff Garzik669a5db2006-08-29 18:12:40 -0400790
Tejun Heo5d728822007-04-17 23:44:08 +0900791 ap->ops = ops;
792 ap->pio_mask = pio_modes;
793 ap->flags |= ATA_FLAG_SLAVE_POSS | iordy;
794 ap->ioaddr.cmd_addr = io_addr;
795 ap->ioaddr.altstatus_addr = ctrl_addr;
796 ap->ioaddr.ctl_addr = ctrl_addr;
797 ata_std_ports(&ap->ioaddr);
798 ap->private_data = ld;
799
Tejun Heocbcdd872007-08-18 13:14:55 +0900800 ata_port_desc(ap, "cmd 0x%lx ctl 0x%lx", io, ctrl);
801
Tejun Heo5d728822007-04-17 23:44:08 +0900802 ret = ata_host_activate(host, irq, ata_interrupt, 0, &legacy_sht);
803 if (ret)
Jeff Garzik669a5db2006-08-29 18:12:40 -0400804 goto fail;
Tejun Heo24dc5f32007-01-20 16:00:28 +0900805
Jeff Garzik669a5db2006-08-29 18:12:40 -0400806 legacy_host[nr_legacy_host++] = dev_get_drvdata(&pdev->dev);
807 ld->platform_dev = pdev;
808 return 0;
809
810fail:
811 platform_device_unregister(pdev);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400812 return ret;
813}
814
815/**
816 * legacy_check_special_cases - ATA special cases
817 * @p: PCI device to check
818 * @master: set this if we find an ATA master
819 * @master: set this if we find an ATA secondary
820 *
821 * A small number of vendors implemented early PCI ATA interfaces on bridge logic
822 * without the ATA interface being PCI visible. Where we have a matching PCI driver
823 * we must skip the relevant device here. If we don't know about it then the legacy
824 * driver is the right driver anyway.
825 */
826
827static void legacy_check_special_cases(struct pci_dev *p, int *primary, int *secondary)
828{
829 /* Cyrix CS5510 pre SFF MWDMA ATA on the bridge */
830 if (p->vendor == 0x1078 && p->device == 0x0000) {
831 *primary = *secondary = 1;
832 return;
833 }
834 /* Cyrix CS5520 pre SFF MWDMA ATA on the bridge */
835 if (p->vendor == 0x1078 && p->device == 0x0002) {
836 *primary = *secondary = 1;
837 return;
838 }
839 /* Intel MPIIX - PIO ATA on non PCI side of bridge */
840 if (p->vendor == 0x8086 && p->device == 0x1234) {
841 u16 r;
842 pci_read_config_word(p, 0x6C, &r);
843 if (r & 0x8000) { /* ATA port enabled */
844 if (r & 0x4000)
845 *secondary = 1;
846 else
847 *primary = 1;
848 }
849 return;
850 }
851}
852
853
854/**
855 * legacy_init - attach legacy interfaces
856 *
857 * Attach legacy IDE interfaces by scanning the usual IRQ/port suspects.
858 * Right now we do not scan the ide0 and ide1 address but should do so
859 * for non PCI systems or systems with no PCI IDE legacy mode devices.
860 * If you fix that note there are special cases to consider like VLB
861 * drivers and CS5510/20.
862 */
863
864static __init int legacy_init(void)
865{
866 int i;
867 int ct = 0;
868 int primary = 0;
869 int secondary = 0;
870 int last_port = NR_HOST;
871
872 struct pci_dev *p = NULL;
873
874 for_each_pci_dev(p) {
875 int r;
876 /* Check for any overlap of the system ATA mappings. Native mode controllers
877 stuck on these addresses or some devices in 'raid' mode won't be found by
878 the storage class test */
879 for (r = 0; r < 6; r++) {
880 if (pci_resource_start(p, r) == 0x1f0)
881 primary = 1;
882 if (pci_resource_start(p, r) == 0x170)
883 secondary = 1;
884 }
885 /* Check for special cases */
886 legacy_check_special_cases(p, &primary, &secondary);
887
888 /* If PCI bus is present then don't probe for tertiary legacy ports */
889 if (probe_all == 0)
890 last_port = 2;
891 }
892
Jeff Garzik85cd7252006-08-31 00:03:49 -0400893 /* If an OPTI 82C46X is present find out where the channels are */
Jeff Garzik669a5db2006-08-29 18:12:40 -0400894 if (opti82c46x) {
895 static const char *optis[4] = {
896 "3/463MV", "5MV",
897 "5MVA", "5MVB"
898 };
899 u8 chans = 1;
900 u8 ctrl = (opti_syscfg(0x30) & 0xC0) >> 6;
Jeff Garzik85cd7252006-08-31 00:03:49 -0400901
Jeff Garzik669a5db2006-08-29 18:12:40 -0400902 opti82c46x = 3; /* Assume master and slave first */
903 printk(KERN_INFO DRV_NAME ": Opti 82C46%s chipset support.\n", optis[ctrl]);
904 if (ctrl == 3)
905 chans = (opti_syscfg(0x3F) & 0x20) ? 2 : 1;
906 ctrl = opti_syscfg(0xAC);
907 /* Check enabled and this port is the 465MV port. On the
908 MVB we may have two channels */
909 if (ctrl & 8) {
910 if (ctrl & 4)
911 opti82c46x = 2; /* Slave */
912 else
913 opti82c46x = 1; /* Master */
914 if (chans == 2)
915 opti82c46x = 3; /* Master and Slave */
916 } /* Slave only */
917 else if (chans == 1)
918 opti82c46x = 1;
919 }
920
921 for (i = 0; i < last_port; i++) {
922 /* Skip primary if we have seen a PCI one */
923 if (i == 0 && primary == 1)
924 continue;
925 /* Skip secondary if we have seen a PCI one */
926 if (i == 1 && secondary == 1)
927 continue;
928 if (legacy_init_one(i, legacy_port[i],
929 legacy_port[i] + 0x0206,
930 legacy_irq[i]) == 0)
931 ct++;
932 }
933 if (ct != 0)
934 return 0;
935 return -ENODEV;
936}
937
938static __exit void legacy_exit(void)
939{
940 int i;
941
942 for (i = 0; i < nr_legacy_host; i++) {
943 struct legacy_data *ld = &legacy_data[i];
Tejun Heo24dc5f32007-01-20 16:00:28 +0900944
945 ata_host_detach(legacy_host[i]);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400946 platform_device_unregister(ld->platform_dev);
947 if (ld->timing)
948 release_region(ld->timing, 2);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400949 }
950}
951
952MODULE_AUTHOR("Alan Cox");
953MODULE_DESCRIPTION("low-level driver for legacy ATA");
954MODULE_LICENSE("GPL");
955MODULE_VERSION(DRV_VERSION);
956
957module_param(probe_all, int, 0);
958module_param(autospeed, int, 0);
959module_param(ht6560a, int, 0);
960module_param(ht6560b, int, 0);
961module_param(opti82c611a, int, 0);
962module_param(opti82c46x, int, 0);
963module_param(pio_mask, int, 0);
Alan Coxf834e492007-02-07 13:46:00 -0800964module_param(iordy_mask, int, 0);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400965
966module_init(legacy_init);
967module_exit(legacy_exit);
968