Merge branch 'for-linus' of master.kernel.org:/pub/scm/linux/kernel/git/drzeus/mmc
[linux-drm-fsl-dcu.git] / drivers / ata / pata_legacy.c
1 /*
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"
67 #define DRV_VERSION "0.5.3"
68
69 #define NR_HOST 6
70
71 static int legacy_port[NR_HOST] = { 0x1f0, 0x170, 0x1e8, 0x168, 0x1e0, 0x160 };
72 static int legacy_irq[NR_HOST] = { 15, 14, 11, 10, 8, 12 };
73
74 struct 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
83 static struct legacy_data legacy_data[NR_HOST];
84 static struct ata_host *legacy_host[NR_HOST];
85 static int nr_legacy_host;
86
87
88 static int probe_all;                   /* Set to check all ISA port ranges */
89 static int ht6560a;                     /* HT 6560A on primary 1, secondary 2, both 3 */
90 static int ht6560b;                     /* HT 6560A on primary 1, secondary 2, both 3 */
91 static int opti82c611a;                 /* Opti82c611A on primary 1, secondary 2, both 3 */
92 static int opti82c46x;          /* Opti 82c465MV present (pri/sec autodetect) */
93 static int autospeed;                   /* Chip present which snoops speed changes */
94 static int pio_mask = 0x1F;             /* PIO range for autospeed devices */
95
96 /**
97  *      legacy_set_mode         -       mode setting
98  *      @ap: IDE interface
99  *
100  *      Use a non standard set_mode function. We don't want to be tuned.
101  *
102  *      The BIOS configured everything. Our job is not to fiddle. Just use
103  *      whatever PIO the hardware is using and leave it at that. When we
104  *      get some kind of nice user driven API for control then we can
105  *      expand on this as per hdparm in the base kernel.
106  */
107
108 static void legacy_set_mode(struct ata_port *ap)
109 {
110         int i;
111
112         for (i = 0; i < ATA_MAX_DEVICES; i++) {
113                 struct ata_device *dev = &ap->device[i];
114                 if (ata_dev_enabled(dev)) {
115                         dev->pio_mode = XFER_PIO_0;
116                         dev->xfer_mode = XFER_PIO_0;
117                         dev->xfer_shift = ATA_SHIFT_PIO;
118                         dev->flags |= ATA_DFLAG_PIO;
119                 }
120         }
121 }
122
123 static struct scsi_host_template legacy_sht = {
124         .module                 = THIS_MODULE,
125         .name                   = DRV_NAME,
126         .ioctl                  = ata_scsi_ioctl,
127         .queuecommand           = ata_scsi_queuecmd,
128         .can_queue              = ATA_DEF_QUEUE,
129         .this_id                = ATA_SHT_THIS_ID,
130         .sg_tablesize           = LIBATA_MAX_PRD,
131         .max_sectors            = ATA_MAX_SECTORS,
132         .cmd_per_lun            = ATA_SHT_CMD_PER_LUN,
133         .emulated               = ATA_SHT_EMULATED,
134         .use_clustering         = ATA_SHT_USE_CLUSTERING,
135         .proc_name              = DRV_NAME,
136         .dma_boundary           = ATA_DMA_BOUNDARY,
137         .slave_configure        = ata_scsi_slave_config,
138         .slave_destroy          = ata_scsi_slave_destroy,
139         .bios_param             = ata_std_bios_param,
140 };
141
142 /*
143  *      These ops are used if the user indicates the hardware
144  *      snoops the commands to decide on the mode and handles the
145  *      mode selection "magically" itself. Several legacy controllers
146  *      do this. The mode range can be set if it is not 0x1F by setting
147  *      pio_mask as well.
148  */
149
150 static struct ata_port_operations simple_port_ops = {
151         .port_disable   = ata_port_disable,
152         .tf_load        = ata_tf_load,
153         .tf_read        = ata_tf_read,
154         .check_status   = ata_check_status,
155         .exec_command   = ata_exec_command,
156         .dev_select     = ata_std_dev_select,
157
158         .freeze         = ata_bmdma_freeze,
159         .thaw           = ata_bmdma_thaw,
160         .error_handler  = ata_bmdma_error_handler,
161         .post_internal_cmd = ata_bmdma_post_internal_cmd,
162
163         .qc_prep        = ata_qc_prep,
164         .qc_issue       = ata_qc_issue_prot,
165
166         .data_xfer      = ata_pio_data_xfer_noirq,
167
168         .irq_handler    = ata_interrupt,
169         .irq_clear      = ata_bmdma_irq_clear,
170
171         .port_start     = ata_port_start,
172         .port_stop      = ata_port_stop,
173         .host_stop      = ata_host_stop
174 };
175
176 static struct ata_port_operations legacy_port_ops = {
177         .set_mode       = legacy_set_mode,
178
179         .port_disable   = ata_port_disable,
180         .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,
185
186         .error_handler  = ata_bmdma_error_handler,
187
188         .qc_prep        = ata_qc_prep,
189         .qc_issue       = ata_qc_issue_prot,
190
191         .data_xfer      = ata_pio_data_xfer_noirq,
192
193         .irq_handler    = ata_interrupt,
194         .irq_clear      = ata_bmdma_irq_clear,
195
196         .port_start     = ata_port_start,
197         .port_stop      = ata_port_stop,
198         .host_stop      = ata_host_stop
199 };
200
201 /*
202  *      Promise 20230C and 20620 support
203  *
204  *      This controller supports PIO0 to PIO2. We set PIO timings conservatively to
205  *      allow for 50MHz Vesa Local Bus. The 20620 DMA support is weird being DMA to
206  *      controller and PIO'd to the host and not supported.
207  */
208
209 static void pdc20230_set_piomode(struct ata_port *ap, struct ata_device *adev)
210 {
211         int tries = 5;
212         int pio = adev->pio_mode - XFER_PIO_0;
213         u8 rt;
214         unsigned long flags;
215
216         /* Safe as UP only. Force I/Os to occur together */
217
218         local_irq_save(flags);
219
220         /* Unlock the control interface */
221         do
222         {
223                 inb(0x1F5);
224                 outb(inb(0x1F2) | 0x80, 0x1F2);
225                 inb(0x1F2);
226                 inb(0x3F6);
227                 inb(0x3F6);
228                 inb(0x1F2);
229                 inb(0x1F2);
230         }
231         while((inb(0x1F2) & 0x80) && --tries);
232
233         local_irq_restore(flags);
234
235         outb(inb(0x1F4) & 0x07, 0x1F4);
236
237         rt = inb(0x1F3);
238         rt &= 0x07 << (3 * adev->devno);
239         if (pio)
240                 rt |= (1 + 3 * pio) << (3 * adev->devno);
241
242         udelay(100);
243         outb(inb(0x1F2) | 0x01, 0x1F2);
244         udelay(100);
245         inb(0x1F5);
246
247 }
248
249 static void pdc_data_xfer_vlb(struct ata_device *adev, unsigned char *buf, unsigned int buflen, int write_data)
250 {
251         struct ata_port *ap = adev->ap;
252         int slop = buflen & 3;
253         unsigned long flags;
254
255         if (ata_id_has_dword_io(adev->id)) {
256                 local_irq_save(flags);
257
258                 /* Perform the 32bit I/O synchronization sequence */
259                 inb(ap->ioaddr.nsect_addr);
260                 inb(ap->ioaddr.nsect_addr);
261                 inb(ap->ioaddr.nsect_addr);
262
263                 /* Now the data */
264
265                 if (write_data)
266                         outsl(ap->ioaddr.data_addr, buf, buflen >> 2);
267                 else
268                         insl(ap->ioaddr.data_addr, buf, buflen >> 2);
269
270                 if (unlikely(slop)) {
271                         u32 pad;
272                         if (write_data) {
273                                 memcpy(&pad, buf + buflen - slop, slop);
274                                 outl(le32_to_cpu(pad), ap->ioaddr.data_addr);
275                         } else {
276                                 pad = cpu_to_le16(inl(ap->ioaddr.data_addr));
277                                 memcpy(buf + buflen - slop, &pad, slop);
278                         }
279                 }
280                 local_irq_restore(flags);
281         }
282         else
283                 ata_pio_data_xfer_noirq(adev, buf, buflen, write_data);
284 }
285
286 static struct ata_port_operations pdc20230_port_ops = {
287         .set_piomode    = pdc20230_set_piomode,
288
289         .port_disable   = ata_port_disable,
290         .tf_load        = ata_tf_load,
291         .tf_read        = ata_tf_read,
292         .check_status   = ata_check_status,
293         .exec_command   = ata_exec_command,
294         .dev_select     = ata_std_dev_select,
295
296         .error_handler  = ata_bmdma_error_handler,
297
298         .qc_prep        = ata_qc_prep,
299         .qc_issue       = ata_qc_issue_prot,
300
301         .data_xfer      = pdc_data_xfer_vlb,
302
303         .irq_handler    = ata_interrupt,
304         .irq_clear      = ata_bmdma_irq_clear,
305
306         .port_start     = ata_port_start,
307         .port_stop      = ata_port_stop,
308         .host_stop      = ata_host_stop
309 };
310
311 /*
312  *      Holtek 6560A support
313  *
314  *      This controller supports PIO0 to PIO2 (no IORDY even though higher timings
315  *      can be loaded).
316  */
317
318 static void ht6560a_set_piomode(struct ata_port *ap, struct ata_device *adev)
319 {
320         u8 active, recover;
321         struct ata_timing t;
322
323         /* Get the timing data in cycles. For now play safe at 50Mhz */
324         ata_timing_compute(adev, adev->pio_mode, &t, 20000, 1000);
325
326         active = FIT(t.active, 2, 15);
327         recover = FIT(t.recover, 4, 15);
328
329         inb(0x3E6);
330         inb(0x3E6);
331         inb(0x3E6);
332         inb(0x3E6);
333
334         outb(recover << 4 | active, ap->ioaddr.device_addr);
335         inb(ap->ioaddr.status_addr);
336 }
337
338 static struct ata_port_operations ht6560a_port_ops = {
339         .set_piomode    = ht6560a_set_piomode,
340
341         .port_disable   = ata_port_disable,
342         .tf_load        = ata_tf_load,
343         .tf_read        = ata_tf_read,
344         .check_status   = ata_check_status,
345         .exec_command   = ata_exec_command,
346         .dev_select     = ata_std_dev_select,
347
348         .error_handler  = ata_bmdma_error_handler,
349
350         .qc_prep        = ata_qc_prep,
351         .qc_issue       = ata_qc_issue_prot,
352
353         .data_xfer      = ata_pio_data_xfer,    /* Check vlb/noirq */
354
355         .irq_handler    = ata_interrupt,
356         .irq_clear      = ata_bmdma_irq_clear,
357
358         .port_start     = ata_port_start,
359         .port_stop      = ata_port_stop,
360         .host_stop      = ata_host_stop
361 };
362
363 /*
364  *      Holtek 6560B support
365  *
366  *      This controller supports PIO0 to PIO4. We honour the BIOS/jumper FIFO setting
367  *      unless we see an ATAPI device in which case we force it off.
368  *
369  *      FIXME: need to implement 2nd channel support.
370  */
371
372 static void ht6560b_set_piomode(struct ata_port *ap, struct ata_device *adev)
373 {
374         u8 active, recover;
375         struct ata_timing t;
376
377         /* Get the timing data in cycles. For now play safe at 50Mhz */
378         ata_timing_compute(adev, adev->pio_mode, &t, 20000, 1000);
379
380         active = FIT(t.active, 2, 15);
381         recover = FIT(t.recover, 2, 16);
382         recover &= 0x15;
383
384         inb(0x3E6);
385         inb(0x3E6);
386         inb(0x3E6);
387         inb(0x3E6);
388
389         outb(recover << 4 | active, ap->ioaddr.device_addr);
390
391         if (adev->class != ATA_DEV_ATA) {
392                 u8 rconf = inb(0x3E6);
393                 if (rconf & 0x24) {
394                         rconf &= ~ 0x24;
395                         outb(rconf, 0x3E6);
396                 }
397         }
398         inb(ap->ioaddr.status_addr);
399 }
400
401 static struct ata_port_operations ht6560b_port_ops = {
402         .set_piomode    = ht6560b_set_piomode,
403
404         .port_disable   = ata_port_disable,
405         .tf_load        = ata_tf_load,
406         .tf_read        = ata_tf_read,
407         .check_status   = ata_check_status,
408         .exec_command   = ata_exec_command,
409         .dev_select     = ata_std_dev_select,
410
411         .error_handler  = ata_bmdma_error_handler,
412
413         .qc_prep        = ata_qc_prep,
414         .qc_issue       = ata_qc_issue_prot,
415
416         .data_xfer      = ata_pio_data_xfer,    /* FIXME: Check 32bit and noirq */
417
418         .irq_handler    = ata_interrupt,
419         .irq_clear      = ata_bmdma_irq_clear,
420
421         .port_start     = ata_port_start,
422         .port_stop      = ata_port_stop,
423         .host_stop      = ata_host_stop
424 };
425
426 /*
427  *      Opti core chipset helpers
428  */
429
430 /**
431  *      opti_syscfg     -       read OPTI chipset configuration
432  *      @reg: Configuration register to read
433  *
434  *      Returns the value of an OPTI system board configuration register.
435  */
436
437 static u8 opti_syscfg(u8 reg)
438 {
439         unsigned long flags;
440         u8 r;
441
442         /* Uniprocessor chipset and must force cycles adjancent */
443         local_irq_save(flags);
444         outb(reg, 0x22);
445         r = inb(0x24);
446         local_irq_restore(flags);
447         return r;
448 }
449
450 /*
451  *      Opti 82C611A
452  *
453  *      This controller supports PIO0 to PIO3.
454  */
455
456 static void opti82c611a_set_piomode(struct ata_port *ap, struct ata_device *adev)
457 {
458         u8 active, recover, setup;
459         struct ata_timing t;
460         struct ata_device *pair = ata_dev_pair(adev);
461         int clock;
462         int khz[4] = { 50000, 40000, 33000, 25000 };
463         u8 rc;
464
465         /* Enter configuration mode */
466         inw(ap->ioaddr.error_addr);
467         inw(ap->ioaddr.error_addr);
468         outb(3, ap->ioaddr.nsect_addr);
469
470         /* Read VLB clock strapping */
471         clock = 1000000000 / khz[inb(ap->ioaddr.lbah_addr) & 0x03];
472
473         /* Get the timing data in cycles */
474         ata_timing_compute(adev, adev->pio_mode, &t, clock, 1000);
475
476         /* Setup timing is shared */
477         if (pair) {
478                 struct ata_timing tp;
479                 ata_timing_compute(pair, pair->pio_mode, &tp, clock, 1000);
480
481                 ata_timing_merge(&t, &tp, &t, ATA_TIMING_SETUP);
482         }
483
484         active = FIT(t.active, 2, 17) - 2;
485         recover = FIT(t.recover, 1, 16) - 1;
486         setup = FIT(t.setup, 1, 4) - 1;
487
488         /* Select the right timing bank for write timing */
489         rc = inb(ap->ioaddr.lbal_addr);
490         rc &= 0x7F;
491         rc |= (adev->devno << 7);
492         outb(rc, ap->ioaddr.lbal_addr);
493
494         /* Write the timings */
495         outb(active << 4 | recover, ap->ioaddr.error_addr);
496
497         /* Select the right bank for read timings, also
498            load the shared timings for address */
499         rc = inb(ap->ioaddr.device_addr);
500         rc &= 0xC0;
501         rc |= adev->devno;      /* Index select */
502         rc |= (setup << 4) | 0x04;
503         outb(rc, ap->ioaddr.device_addr);
504
505         /* Load the read timings */
506         outb(active << 4 | recover, ap->ioaddr.data_addr);
507
508         /* Ensure the timing register mode is right */
509         rc = inb (ap->ioaddr.lbal_addr);
510         rc &= 0x73;
511         rc |= 0x84;
512         outb(rc, ap->ioaddr.lbal_addr);
513
514         /* Exit command mode */
515         outb(0x83,  ap->ioaddr.nsect_addr);
516 }
517
518
519 static struct ata_port_operations opti82c611a_port_ops = {
520         .set_piomode    = opti82c611a_set_piomode,
521
522         .port_disable   = ata_port_disable,
523         .tf_load        = ata_tf_load,
524         .tf_read        = ata_tf_read,
525         .check_status   = ata_check_status,
526         .exec_command   = ata_exec_command,
527         .dev_select     = ata_std_dev_select,
528
529         .error_handler  = ata_bmdma_error_handler,
530
531         .qc_prep        = ata_qc_prep,
532         .qc_issue       = ata_qc_issue_prot,
533
534         .data_xfer      = ata_pio_data_xfer,
535
536         .irq_handler    = ata_interrupt,
537         .irq_clear      = ata_bmdma_irq_clear,
538
539         .port_start     = ata_port_start,
540         .port_stop      = ata_port_stop,
541         .host_stop      = ata_host_stop
542 };
543
544 /*
545  *      Opti 82C465MV
546  *
547  *      This controller supports PIO0 to PIO3. Unlike the 611A the MVB
548  *      version is dual channel but doesn't have a lot of unique registers.
549  */
550
551 static void opti82c46x_set_piomode(struct ata_port *ap, struct ata_device *adev)
552 {
553         u8 active, recover, setup;
554         struct ata_timing t;
555         struct ata_device *pair = ata_dev_pair(adev);
556         int clock;
557         int khz[4] = { 50000, 40000, 33000, 25000 };
558         u8 rc;
559         u8 sysclk;
560
561         /* Get the clock */
562         sysclk = opti_syscfg(0xAC) & 0xC0;      /* BIOS set */
563
564         /* Enter configuration mode */
565         inw(ap->ioaddr.error_addr);
566         inw(ap->ioaddr.error_addr);
567         outb(3, ap->ioaddr.nsect_addr);
568
569         /* Read VLB clock strapping */
570         clock = 1000000000 / khz[sysclk];
571
572         /* Get the timing data in cycles */
573         ata_timing_compute(adev, adev->pio_mode, &t, clock, 1000);
574
575         /* Setup timing is shared */
576         if (pair) {
577                 struct ata_timing tp;
578                 ata_timing_compute(pair, pair->pio_mode, &tp, clock, 1000);
579
580                 ata_timing_merge(&t, &tp, &t, ATA_TIMING_SETUP);
581         }
582
583         active = FIT(t.active, 2, 17) - 2;
584         recover = FIT(t.recover, 1, 16) - 1;
585         setup = FIT(t.setup, 1, 4) - 1;
586
587         /* Select the right timing bank for write timing */
588         rc = inb(ap->ioaddr.lbal_addr);
589         rc &= 0x7F;
590         rc |= (adev->devno << 7);
591         outb(rc, ap->ioaddr.lbal_addr);
592
593         /* Write the timings */
594         outb(active << 4 | recover, ap->ioaddr.error_addr);
595
596         /* Select the right bank for read timings, also
597            load the shared timings for address */
598         rc = inb(ap->ioaddr.device_addr);
599         rc &= 0xC0;
600         rc |= adev->devno;      /* Index select */
601         rc |= (setup << 4) | 0x04;
602         outb(rc, ap->ioaddr.device_addr);
603
604         /* Load the read timings */
605         outb(active << 4 | recover, ap->ioaddr.data_addr);
606
607         /* Ensure the timing register mode is right */
608         rc = inb (ap->ioaddr.lbal_addr);
609         rc &= 0x73;
610         rc |= 0x84;
611         outb(rc, ap->ioaddr.lbal_addr);
612
613         /* Exit command mode */
614         outb(0x83,  ap->ioaddr.nsect_addr);
615
616         /* We need to know this for quad device on the MVB */
617         ap->host->private_data = ap;
618 }
619
620 /**
621  *      opt82c465mv_qc_issue_prot       -       command issue
622  *      @qc: command pending
623  *
624  *      Called when the libata layer is about to issue a command. We wrap
625  *      this interface so that we can load the correct ATA timings. The
626  *      MVB has a single set of timing registers and these are shared
627  *      across channels. As there are two registers we really ought to
628  *      track the last two used values as a sort of register window. For
629  *      now we just reload on a channel switch. On the single channel
630  *      setup this condition never fires so we do nothing extra.
631  *
632  *      FIXME: dual channel needs ->serialize support
633  */
634
635 static unsigned int opti82c46x_qc_issue_prot(struct ata_queued_cmd *qc)
636 {
637         struct ata_port *ap = qc->ap;
638         struct ata_device *adev = qc->dev;
639
640         /* If timings are set and for the wrong channel (2nd test is
641            due to a libata shortcoming and will eventually go I hope) */
642         if (ap->host->private_data != ap->host
643             && ap->host->private_data != NULL)
644                 opti82c46x_set_piomode(ap, adev);
645
646         return ata_qc_issue_prot(qc);
647 }
648
649 static struct ata_port_operations opti82c46x_port_ops = {
650         .set_piomode    = opti82c46x_set_piomode,
651
652         .port_disable   = ata_port_disable,
653         .tf_load        = ata_tf_load,
654         .tf_read        = ata_tf_read,
655         .check_status   = ata_check_status,
656         .exec_command   = ata_exec_command,
657         .dev_select     = ata_std_dev_select,
658
659         .error_handler  = ata_bmdma_error_handler,
660
661         .qc_prep        = ata_qc_prep,
662         .qc_issue       = opti82c46x_qc_issue_prot,
663
664         .data_xfer      = ata_pio_data_xfer,
665
666         .irq_handler    = ata_interrupt,
667         .irq_clear      = ata_bmdma_irq_clear,
668
669         .port_start     = ata_port_start,
670         .port_stop      = ata_port_stop,
671         .host_stop      = ata_host_stop
672 };
673
674
675 /**
676  *      legacy_init_one         -       attach a legacy interface
677  *      @port: port number
678  *      @io: I/O port start
679  *      @ctrl: control port
680  *      @irq: interrupt line
681  *
682  *      Register an ISA bus IDE interface. Such interfaces are PIO and we
683  *      assume do not support IRQ sharing.
684  */
685
686 static __init int legacy_init_one(int port, unsigned long io, unsigned long ctrl, int irq)
687 {
688         struct legacy_data *ld = &legacy_data[nr_legacy_host];
689         struct ata_probe_ent ae;
690         struct platform_device *pdev;
691         int ret = -EBUSY;
692         struct ata_port_operations *ops = &legacy_port_ops;
693         int pio_modes = pio_mask;
694         u32 mask = (1 << port);
695
696         if (request_region(io, 8, "pata_legacy") == NULL)
697                 return -EBUSY;
698         if (request_region(ctrl, 1, "pata_legacy") == NULL)
699                 goto fail_io;
700
701         pdev = platform_device_register_simple(DRV_NAME, nr_legacy_host, NULL, 0);
702         if (pdev == NULL)
703                 goto fail_dev;
704
705         if (ht6560a & mask) {
706                 ops = &ht6560a_port_ops;
707                 pio_modes = 0x07;
708         }
709         if (ht6560b & mask) {
710                 ops = &ht6560b_port_ops;
711                 pio_modes = 0x1F;
712         }
713         if (opti82c611a & mask) {
714                 ops = &opti82c611a_port_ops;
715                 pio_modes = 0x0F;
716         }
717         if (opti82c46x & mask) {
718                 ops = &opti82c46x_port_ops;
719                 pio_modes = 0x0F;
720         }
721
722         /* Probe for automatically detectable controllers */
723
724         if (io == 0x1F0 && ops == &legacy_port_ops) {
725                 unsigned long flags;
726
727                 local_irq_save(flags);
728
729                 /* Probes */
730                 inb(0x1F5);
731                 outb(inb(0x1F2) | 0x80, 0x1F2);
732                 inb(0x1F2);
733                 inb(0x3F6);
734                 inb(0x3F6);
735                 inb(0x1F2);
736                 inb(0x1F2);
737
738                 if ((inb(0x1F2) & 0x80) == 0) {
739                         /* PDC20230c or 20630 ? */
740                         printk(KERN_INFO "PDC20230-C/20630 VLB ATA controller detected.\n");
741                                 pio_modes = 0x07;
742                         ops = &pdc20230_port_ops;
743                         udelay(100);
744                         inb(0x1F5);
745                 } else {
746                         outb(0x55, 0x1F2);
747                         inb(0x1F2);
748                         inb(0x1F2);
749                         if (inb(0x1F2) == 0x00) {
750                                 printk(KERN_INFO "PDC20230-B VLB ATA controller detected.\n");
751                         }
752                 }
753                 local_irq_restore(flags);
754         }
755
756
757         /* Chip does mode setting by command snooping */
758         if (ops == &legacy_port_ops && (autospeed & mask))
759                 ops = &simple_port_ops;
760         memset(&ae, 0, sizeof(struct ata_probe_ent));
761         INIT_LIST_HEAD(&ae.node);
762         ae.dev = &pdev->dev;
763         ae.port_ops = ops;
764         ae.sht = &legacy_sht;
765         ae.n_ports = 1;
766         ae.pio_mask = pio_modes;
767         ae.irq = irq;
768         ae.irq_flags = 0;
769         ae.port_flags = ATA_FLAG_SLAVE_POSS|ATA_FLAG_SRST;
770         ae.port[0].cmd_addr = io;
771         ae.port[0].altstatus_addr = ctrl;
772         ae.port[0].ctl_addr =   ctrl;
773         ata_std_ports(&ae.port[0]);
774         ae.private_data = ld;
775
776         ret = ata_device_add(&ae);
777         if (ret == 0) {
778                 ret = -ENODEV;
779                 goto fail;
780         }
781         legacy_host[nr_legacy_host++] = dev_get_drvdata(&pdev->dev);
782         ld->platform_dev = pdev;
783         return 0;
784
785 fail:
786         platform_device_unregister(pdev);
787 fail_dev:
788         release_region(ctrl, 1);
789 fail_io:
790         release_region(io, 8);
791         return ret;
792 }
793
794 /**
795  *      legacy_check_special_cases      -       ATA special cases
796  *      @p: PCI device to check
797  *      @master: set this if we find an ATA master
798  *      @master: set this if we find an ATA secondary
799  *
800  *      A small number of vendors implemented early PCI ATA interfaces on bridge logic
801  *      without the ATA interface being PCI visible. Where we have a matching PCI driver
802  *      we must skip the relevant device here. If we don't know about it then the legacy
803  *      driver is the right driver anyway.
804  */
805
806 static void legacy_check_special_cases(struct pci_dev *p, int *primary, int *secondary)
807 {
808         /* Cyrix CS5510 pre SFF MWDMA ATA on the bridge */
809         if (p->vendor == 0x1078 && p->device == 0x0000) {
810                 *primary = *secondary = 1;
811                 return;
812         }
813         /* Cyrix CS5520 pre SFF MWDMA ATA on the bridge */
814         if (p->vendor == 0x1078 && p->device == 0x0002) {
815                 *primary = *secondary = 1;
816                 return;
817         }
818         /* Intel MPIIX - PIO ATA on non PCI side of bridge */
819         if (p->vendor == 0x8086 && p->device == 0x1234) {
820                 u16 r;
821                 pci_read_config_word(p, 0x6C, &r);
822                 if (r & 0x8000) {       /* ATA port enabled */
823                         if (r & 0x4000)
824                                 *secondary = 1;
825                         else
826                                 *primary = 1;
827                 }
828                 return;
829         }
830 }
831
832
833 /**
834  *      legacy_init             -       attach legacy interfaces
835  *
836  *      Attach legacy IDE interfaces by scanning the usual IRQ/port suspects.
837  *      Right now we do not scan the ide0 and ide1 address but should do so
838  *      for non PCI systems or systems with no PCI IDE legacy mode devices.
839  *      If you fix that note there are special cases to consider like VLB
840  *      drivers and CS5510/20.
841  */
842
843 static __init int legacy_init(void)
844 {
845         int i;
846         int ct = 0;
847         int primary = 0;
848         int secondary = 0;
849         int last_port = NR_HOST;
850
851         struct pci_dev *p = NULL;
852
853         for_each_pci_dev(p) {
854                 int r;
855                 /* Check for any overlap of the system ATA mappings. Native mode controllers
856                    stuck on these addresses or some devices in 'raid' mode won't be found by
857                    the storage class test */
858                 for (r = 0; r < 6; r++) {
859                         if (pci_resource_start(p, r) == 0x1f0)
860                                 primary = 1;
861                         if (pci_resource_start(p, r) == 0x170)
862                                 secondary = 1;
863                 }
864                 /* Check for special cases */
865                 legacy_check_special_cases(p, &primary, &secondary);
866
867                 /* If PCI bus is present then don't probe for tertiary legacy ports */
868                 if (probe_all == 0)
869                         last_port = 2;
870         }
871
872         /* If an OPTI 82C46X is present find out where the channels are */
873         if (opti82c46x) {
874                 static const char *optis[4] = {
875                         "3/463MV", "5MV",
876                         "5MVA", "5MVB"
877                 };
878                 u8 chans = 1;
879                 u8 ctrl = (opti_syscfg(0x30) & 0xC0) >> 6;
880
881                 opti82c46x = 3; /* Assume master and slave first */
882                 printk(KERN_INFO DRV_NAME ": Opti 82C46%s chipset support.\n", optis[ctrl]);
883                 if (ctrl == 3)
884                         chans = (opti_syscfg(0x3F) & 0x20) ? 2 : 1;
885                 ctrl = opti_syscfg(0xAC);
886                 /* Check enabled and this port is the 465MV port. On the
887                    MVB we may have two channels */
888                 if (ctrl & 8) {
889                         if (ctrl & 4)
890                                 opti82c46x = 2; /* Slave */
891                         else
892                                 opti82c46x = 1; /* Master */
893                         if (chans == 2)
894                                 opti82c46x = 3; /* Master and Slave */
895                 }       /* Slave only */
896                 else if (chans == 1)
897                         opti82c46x = 1;
898         }
899
900         for (i = 0; i < last_port; i++) {
901                 /* Skip primary if we have seen a PCI one */
902                 if (i == 0 && primary == 1)
903                         continue;
904                 /* Skip secondary if we have seen a PCI one */
905                 if (i == 1 && secondary == 1)
906                         continue;
907                 if (legacy_init_one(i, legacy_port[i],
908                                    legacy_port[i] + 0x0206,
909                                    legacy_irq[i]) == 0)
910                         ct++;
911         }
912         if (ct != 0)
913                 return 0;
914         return -ENODEV;
915 }
916
917 static __exit void legacy_exit(void)
918 {
919         int i;
920
921         for (i = 0; i < nr_legacy_host; i++) {
922                 struct legacy_data *ld = &legacy_data[i];
923                 struct ata_port *ap =legacy_host[i]->ports[0];
924                 unsigned long io = ap->ioaddr.cmd_addr;
925                 unsigned long ctrl = ap->ioaddr.ctl_addr;
926                 ata_host_remove(legacy_host[i]);
927                 platform_device_unregister(ld->platform_dev);
928                 if (ld->timing)
929                         release_region(ld->timing, 2);
930                 release_region(io, 8);
931                 release_region(ctrl, 1);
932         }
933 }
934
935 MODULE_AUTHOR("Alan Cox");
936 MODULE_DESCRIPTION("low-level driver for legacy ATA");
937 MODULE_LICENSE("GPL");
938 MODULE_VERSION(DRV_VERSION);
939
940 module_param(probe_all, int, 0);
941 module_param(autospeed, int, 0);
942 module_param(ht6560a, int, 0);
943 module_param(ht6560b, int, 0);
944 module_param(opti82c611a, int, 0);
945 module_param(opti82c46x, int, 0);
946 module_param(pio_mask, int, 0);
947
948 module_init(legacy_init);
949 module_exit(legacy_exit);
950