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