Merge to Fedora kernel-2.6.18-1.2224_FC5 patched with stable patch-2.6.18.1-vs2.0...
[linux-2.6.git] / drivers / ide / pci / cmd640.c
1 /*
2  *  linux/drivers/ide/pci/cmd640.c              Version 1.02  Sep 01, 1996
3  *
4  *  Copyright (C) 1995-1996  Linus Torvalds & authors (see below)
5  */
6
7 /*
8  *  Original authors:   abramov@cecmow.enet.dec.com (Igor Abramov)
9  *                      mlord@pobox.com (Mark Lord)
10  *
11  *  See linux/MAINTAINERS for address of current maintainer.
12  *
13  *  This file provides support for the advanced features and bugs
14  *  of IDE interfaces using the CMD Technologies 0640 IDE interface chip.
15  *
16  *  These chips are basically fucked by design, and getting this driver
17  *  to work on every motherboard design that uses this screwed chip seems
18  *  bloody well impossible.  However, we're still trying.
19  *
20  *  Version 0.97 worked for everybody.
21  *
22  *  User feedback is essential.  Many thanks to the beta test team:
23  *
24  *  A.Hartgers@stud.tue.nl, JZDQC@CUNYVM.CUNY.edu, abramov@cecmow.enet.dec.com,
25  *  bardj@utopia.ppp.sn.no, bart@gaga.tue.nl, bbol001@cs.auckland.ac.nz,
26  *  chrisc@dbass.demon.co.uk, dalecki@namu26.Num.Math.Uni-Goettingen.de,
27  *  derekn@vw.ece.cmu.edu, florian@btp2x3.phy.uni-bayreuth.de,
28  *  flynn@dei.unipd.it, gadio@netvision.net.il, godzilla@futuris.net,
29  *  j@pobox.com, jkemp1@mises.uni-paderborn.de, jtoppe@hiwaay.net,
30  *  kerouac@ssnet.com, meskes@informatik.rwth-aachen.de, hzoli@cs.elte.hu,
31  *  peter@udgaard.isgtec.com, phil@tazenda.demon.co.uk, roadcapw@cfw.com,
32  *  s0033las@sun10.vsz.bme.hu, schaffer@tam.cornell.edu, sjd@slip.net,
33  *  steve@ei.org, ulrpeg@bigcomm.gun.de, ism@tardis.ed.ac.uk, mack@cray.com
34  *  liug@mama.indstate.edu, and others.
35  *
36  *  Version 0.01        Initial version, hacked out of ide.c,
37  *                      and #include'd rather than compiled separately.
38  *                      This will get cleaned up in a subsequent release.
39  *
40  *  Version 0.02        Fixes for vlb initialization code, enable prefetch
41  *                      for versions 'B' and 'C' of chip by default,
42  *                      some code cleanup.
43  *
44  *  Version 0.03        Added reset of secondary interface,
45  *                      and black list for devices which are not compatible
46  *                      with prefetch mode. Separate function for setting
47  *                      prefetch is added, possibly it will be called some
48  *                      day from ioctl processing code.
49  *
50  *  Version 0.04        Now configs/compiles separate from ide.c
51  *
52  *  Version 0.05        Major rewrite of interface timing code.
53  *                      Added new function cmd640_set_mode to set PIO mode
54  *                      from ioctl call. New drives added to black list.
55  *
56  *  Version 0.06        More code cleanup. Prefetch is enabled only for
57  *                      detected hard drives, not included in prefetch
58  *                      black list.
59  *
60  *  Version 0.07        Changed to more conservative drive tuning policy.
61  *                      Unknown drives, which report PIO < 4 are set to
62  *                      (reported_PIO - 1) if it is supported, or to PIO0.
63  *                      List of known drives extended by info provided by
64  *                      CMD at their ftp site.
65  *
66  *  Version 0.08        Added autotune/noautotune support.
67  *
68  *  Version 0.09        Try to be smarter about 2nd port enabling.
69  *  Version 0.10        Be nice and don't reset 2nd port.
70  *  Version 0.11        Try to handle more weird situations.
71  *
72  *  Version 0.12        Lots of bug fixes from Laszlo Peter
73  *                      irq unmasking disabled for reliability.
74  *                      try to be even smarter about the second port.
75  *                      tidy up source code formatting.
76  *  Version 0.13        permit irq unmasking again.
77  *  Version 0.90        massive code cleanup, some bugs fixed.
78  *                      defaults all drives to PIO mode0, prefetch off.
79  *                      autotune is OFF by default, with compile time flag.
80  *                      prefetch can be turned OFF/ON using "hdparm -p8/-p9"
81  *                       (requires hdparm-3.1 or newer)
82  *  Version 0.91        first release to linux-kernel list.
83  *  Version 0.92        move initial reg dump to separate callable function
84  *                      change "readahead" to "prefetch" to avoid confusion
85  *  Version 0.95        respect original BIOS timings unless autotuning.
86  *                      tons of code cleanup and rearrangement.
87  *                      added CONFIG_BLK_DEV_CMD640_ENHANCED option
88  *                      prevent use of unmask when prefetch is on
89  *  Version 0.96        prevent use of io_32bit when prefetch is off
90  *  Version 0.97        fix VLB secondary interface for sjd@slip.net
91  *                      other minor tune-ups:  0.96 was very good.
92  *  Version 0.98        ignore PCI version when disabled by BIOS
93  *  Version 0.99        display setup/active/recovery clocks with PIO mode
94  *  Version 1.00        Mmm.. cannot depend on PCMD_ENA in all systems
95  *  Version 1.01        slow/fast devsel can be selected with "hdparm -p6/-p7"
96  *                       ("fast" is necessary for 32bit I/O in some systems)
97  *  Version 1.02        fix bug that resulted in slow "setup times"
98  *                       (patch courtesy of Zoltan Hidvegi)
99  */
100
101 #undef REALLY_SLOW_IO           /* most systems can safely undef this */
102 #define CMD640_PREFETCH_MASKS 1
103
104 //#define CMD640_DUMP_REGS
105
106 #include <linux/types.h>
107 #include <linux/kernel.h>
108 #include <linux/delay.h>
109 #include <linux/timer.h>
110 #include <linux/mm.h>
111 #include <linux/ioport.h>
112 #include <linux/blkdev.h>
113 #include <linux/hdreg.h>
114 #include <linux/ide.h>
115 #include <linux/init.h>
116
117 #include <asm/io.h>
118
119 /*
120  * This flag is set in ide.c by the parameter:  ide0=cmd640_vlb
121  */
122 int cmd640_vlb = 0;
123
124 /*
125  * CMD640 specific registers definition.
126  */
127
128 #define VID             0x00
129 #define DID             0x02
130 #define PCMD            0x04
131 #define   PCMD_ENA      0x01
132 #define PSTTS           0x06
133 #define REVID           0x08
134 #define PROGIF          0x09
135 #define SUBCL           0x0a
136 #define BASCL           0x0b
137 #define BaseA0          0x10
138 #define BaseA1          0x14
139 #define BaseA2          0x18
140 #define BaseA3          0x1c
141 #define INTLINE         0x3c
142 #define INPINE          0x3d
143
144 #define CFR             0x50
145 #define   CFR_DEVREV            0x03
146 #define   CFR_IDE01INTR         0x04
147 #define   CFR_DEVID             0x18
148 #define   CFR_AT_VESA_078h      0x20
149 #define   CFR_DSA1              0x40
150 #define   CFR_DSA0              0x80
151
152 #define CNTRL           0x51
153 #define   CNTRL_DIS_RA0         0x40
154 #define   CNTRL_DIS_RA1         0x80
155 #define   CNTRL_ENA_2ND         0x08
156
157 #define CMDTIM          0x52
158 #define ARTTIM0         0x53
159 #define DRWTIM0         0x54
160 #define ARTTIM1         0x55
161 #define DRWTIM1         0x56
162 #define ARTTIM23        0x57
163 #define   ARTTIM23_DIS_RA2      0x04
164 #define   ARTTIM23_DIS_RA3      0x08
165 #define DRWTIM23        0x58
166 #define BRST            0x59
167
168 /*
169  * Registers and masks for easy access by drive index:
170  */
171 static u8 prefetch_regs[4]  = {CNTRL, CNTRL, ARTTIM23, ARTTIM23};
172 static u8 prefetch_masks[4] = {CNTRL_DIS_RA0, CNTRL_DIS_RA1, ARTTIM23_DIS_RA2, ARTTIM23_DIS_RA3};
173
174 #ifdef CONFIG_BLK_DEV_CMD640_ENHANCED
175
176 static u8 arttim_regs[4] = {ARTTIM0, ARTTIM1, ARTTIM23, ARTTIM23};
177 static u8 drwtim_regs[4] = {DRWTIM0, DRWTIM1, DRWTIM23, DRWTIM23};
178
179 /*
180  * Current cmd640 timing values for each drive.
181  * The defaults for each are the slowest possible timings.
182  */
183 static u8 setup_counts[4]    = {4, 4, 4, 4};     /* Address setup count (in clocks) */
184 static u8 active_counts[4]   = {16, 16, 16, 16}; /* Active count   (encoded) */
185 static u8 recovery_counts[4] = {16, 16, 16, 16}; /* Recovery count (encoded) */
186
187 #endif /* CONFIG_BLK_DEV_CMD640_ENHANCED */
188
189 /*
190  * These are initialized to point at the devices we control
191  */
192 static ide_hwif_t  *cmd_hwif0, *cmd_hwif1;
193 static ide_drive_t *cmd_drives[4];
194
195 /*
196  * Interface to access cmd640x registers
197  */
198 static unsigned int cmd640_key;
199 static void (*__put_cmd640_reg)(u16 reg, u8 val);
200 static u8 (*__get_cmd640_reg)(u16 reg);
201
202 /*
203  * This is read from the CFR reg, and is used in several places.
204  */
205 static unsigned int cmd640_chip_version;
206
207 /*
208  * The CMD640x chip does not support DWORD config write cycles, but some
209  * of the BIOSes use them to implement the config services.
210  * Therefore, we must use direct IO instead.
211  */
212
213 /* PCI method 1 access */
214
215 static void put_cmd640_reg_pci1 (u16 reg, u8 val)
216 {
217         outl_p((reg & 0xfc) | cmd640_key, 0xcf8);
218         outb_p(val, (reg & 3) | 0xcfc);
219 }
220
221 static u8 get_cmd640_reg_pci1 (u16 reg)
222 {
223         outl_p((reg & 0xfc) | cmd640_key, 0xcf8);
224         return inb_p((reg & 3) | 0xcfc);
225 }
226
227 /* PCI method 2 access (from CMD datasheet) */
228
229 static void put_cmd640_reg_pci2 (u16 reg, u8 val)
230 {
231         outb_p(0x10, 0xcf8);
232         outb_p(val, cmd640_key + reg);
233         outb_p(0, 0xcf8);
234 }
235
236 static u8 get_cmd640_reg_pci2 (u16 reg)
237 {
238         u8 b;
239
240         outb_p(0x10, 0xcf8);
241         b = inb_p(cmd640_key + reg);
242         outb_p(0, 0xcf8);
243         return b;
244 }
245
246 /* VLB access */
247
248 static void put_cmd640_reg_vlb (u16 reg, u8 val)
249 {
250         outb_p(reg, cmd640_key);
251         outb_p(val, cmd640_key + 4);
252 }
253
254 static u8 get_cmd640_reg_vlb (u16 reg)
255 {
256         outb_p(reg, cmd640_key);
257         return inb_p(cmd640_key + 4);
258 }
259
260 static u8 get_cmd640_reg(u16 reg)
261 {
262         u8 b;
263         unsigned long flags;
264
265         spin_lock_irqsave(&ide_lock, flags);
266         b = __get_cmd640_reg(reg);
267         spin_unlock_irqrestore(&ide_lock, flags);
268         return b;
269 }
270
271 static void put_cmd640_reg(u16 reg, u8 val)
272 {
273         unsigned long flags;
274
275         spin_lock_irqsave(&ide_lock, flags);
276         __put_cmd640_reg(reg,val);
277         spin_unlock_irqrestore(&ide_lock, flags);
278 }
279
280 static int __init match_pci_cmd640_device (void)
281 {
282         const u8 ven_dev[4] = {0x95, 0x10, 0x40, 0x06};
283         unsigned int i;
284         for (i = 0; i < 4; i++) {
285                 if (get_cmd640_reg(i) != ven_dev[i])
286                         return 0;
287         }
288 #ifdef STUPIDLY_TRUST_BROKEN_PCMD_ENA_BIT
289         if ((get_cmd640_reg(PCMD) & PCMD_ENA) == 0) {
290                 printk("ide: cmd640 on PCI disabled by BIOS\n");
291                 return 0;
292         }
293 #endif /* STUPIDLY_TRUST_BROKEN_PCMD_ENA_BIT */
294         return 1; /* success */
295 }
296
297 /*
298  * Probe for CMD640x -- pci method 1
299  */
300 static int __init probe_for_cmd640_pci1 (void)
301 {
302         __get_cmd640_reg = get_cmd640_reg_pci1;
303         __put_cmd640_reg = put_cmd640_reg_pci1;
304         for (cmd640_key = 0x80000000;
305              cmd640_key <= 0x8000f800;
306              cmd640_key += 0x800) {
307                 if (match_pci_cmd640_device())
308                         return 1; /* success */
309         }
310         return 0;
311 }
312
313 /*
314  * Probe for CMD640x -- pci method 2
315  */
316 static int __init probe_for_cmd640_pci2 (void)
317 {
318         __get_cmd640_reg = get_cmd640_reg_pci2;
319         __put_cmd640_reg = put_cmd640_reg_pci2;
320         for (cmd640_key = 0xc000; cmd640_key <= 0xcf00; cmd640_key += 0x100) {
321                 if (match_pci_cmd640_device())
322                         return 1; /* success */
323         }
324         return 0;
325 }
326
327 /*
328  * Probe for CMD640x -- vlb
329  */
330 static int __init probe_for_cmd640_vlb (void)
331 {
332         u8 b;
333
334         __get_cmd640_reg = get_cmd640_reg_vlb;
335         __put_cmd640_reg = put_cmd640_reg_vlb;
336         cmd640_key = 0x178;
337         b = get_cmd640_reg(CFR);
338         if (b == 0xff || b == 0x00 || (b & CFR_AT_VESA_078h)) {
339                 cmd640_key = 0x78;
340                 b = get_cmd640_reg(CFR);
341                 if (b == 0xff || b == 0x00 || !(b & CFR_AT_VESA_078h))
342                         return 0;
343         }
344         return 1; /* success */
345 }
346
347 /*
348  *  Returns 1 if an IDE interface/drive exists at 0x170,
349  *  Returns 0 otherwise.
350  */
351 static int __init secondary_port_responding (void)
352 {
353         unsigned long flags;
354
355         spin_lock_irqsave(&ide_lock, flags);
356
357         outb_p(0x0a, 0x170 + IDE_SELECT_OFFSET);        /* select drive0 */
358         udelay(100);
359         if ((inb_p(0x170 + IDE_SELECT_OFFSET) & 0x1f) != 0x0a) {
360                 outb_p(0x1a, 0x170 + IDE_SELECT_OFFSET); /* select drive1 */
361                 udelay(100);
362                 if ((inb_p(0x170 + IDE_SELECT_OFFSET) & 0x1f) != 0x1a) {
363                         spin_unlock_irqrestore(&ide_lock, flags);
364                         return 0; /* nothing responded */
365                 }
366         }
367         spin_unlock_irqrestore(&ide_lock, flags);
368         return 1; /* success */
369 }
370
371 #ifdef CMD640_DUMP_REGS
372 /*
373  * Dump out all cmd640 registers.  May be called from ide.c
374  */
375 static void cmd640_dump_regs (void)
376 {
377         unsigned int reg = cmd640_vlb ? 0x50 : 0x00;
378
379         /* Dump current state of chip registers */
380         printk("ide: cmd640 internal register dump:");
381         for (; reg <= 0x59; reg++) {
382                 if (!(reg & 0x0f))
383                         printk("\n%04x:", reg);
384                 printk(" %02x", get_cmd640_reg(reg));
385         }
386         printk("\n");
387 }
388 #endif
389
390 /*
391  * Check whether prefetch is on for a drive,
392  * and initialize the unmask flags for safe operation.
393  */
394 static void __init check_prefetch (unsigned int index)
395 {
396         ide_drive_t *drive = cmd_drives[index];
397         u8 b = get_cmd640_reg(prefetch_regs[index]);
398
399         if (b & prefetch_masks[index]) {        /* is prefetch off? */
400                 drive->no_unmask = 0;
401                 drive->no_io_32bit = 1;
402                 drive->io_32bit = 0;
403         } else {
404 #if CMD640_PREFETCH_MASKS
405                 drive->no_unmask = 1;
406                 drive->unmask = 0;
407 #endif
408                 drive->no_io_32bit = 0;
409         }
410 }
411
412 /*
413  * Figure out which devices we control
414  */
415 static void __init setup_device_ptrs (void)
416 {
417         unsigned int i;
418
419         cmd_hwif0 = &ide_hwifs[0]; /* default, if not found below */
420         cmd_hwif1 = &ide_hwifs[1]; /* default, if not found below */
421         for (i = 0; i < MAX_HWIFS; i++) {
422                 ide_hwif_t *hwif = &ide_hwifs[i];
423                 if (hwif->chipset == ide_unknown || hwif->chipset == ide_forced) {
424                         if (hwif->io_ports[IDE_DATA_OFFSET] == 0x1f0)
425                                 cmd_hwif0 = hwif;
426                         else if (hwif->io_ports[IDE_DATA_OFFSET] == 0x170)
427                                 cmd_hwif1 = hwif;
428                 }
429         }
430         cmd_drives[0] = &cmd_hwif0->drives[0];
431         cmd_drives[1] = &cmd_hwif0->drives[1];
432         cmd_drives[2] = &cmd_hwif1->drives[0];
433         cmd_drives[3] = &cmd_hwif1->drives[1];
434 }
435
436 #ifdef CONFIG_BLK_DEV_CMD640_ENHANCED
437
438 /*
439  * Sets prefetch mode for a drive.
440  */
441 static void set_prefetch_mode (unsigned int index, int mode)
442 {
443         ide_drive_t *drive = cmd_drives[index];
444         int reg = prefetch_regs[index];
445         u8 b;
446         unsigned long flags;
447
448         spin_lock_irqsave(&ide_lock, flags);
449         b = __get_cmd640_reg(reg);
450         if (mode) {     /* want prefetch on? */
451 #if CMD640_PREFETCH_MASKS
452                 drive->no_unmask = 1;
453                 drive->unmask = 0;
454 #endif
455                 drive->no_io_32bit = 0;
456                 b &= ~prefetch_masks[index];    /* enable prefetch */
457         } else {
458                 drive->no_unmask = 0;
459                 drive->no_io_32bit = 1;
460                 drive->io_32bit = 0;
461                 b |= prefetch_masks[index];     /* disable prefetch */
462         }
463         __put_cmd640_reg(reg, b);
464         spin_unlock_irqrestore(&ide_lock, flags);
465 }
466
467 /*
468  * Dump out current drive clocks settings
469  */
470 static void display_clocks (unsigned int index)
471 {
472         u8 active_count, recovery_count;
473
474         active_count = active_counts[index];
475         if (active_count == 1)
476                 ++active_count;
477         recovery_count = recovery_counts[index];
478         if (active_count > 3 && recovery_count == 1)
479                 ++recovery_count;
480         if (cmd640_chip_version > 1)
481                 recovery_count += 1;  /* cmd640b uses (count + 1)*/
482         printk(", clocks=%d/%d/%d\n", setup_counts[index], active_count, recovery_count);
483 }
484
485 /*
486  * Pack active and recovery counts into single byte representation
487  * used by controller
488  */
489 static inline u8 pack_nibbles (u8 upper, u8 lower)
490 {
491         return ((upper & 0x0f) << 4) | (lower & 0x0f);
492 }
493
494 /*
495  * This routine retrieves the initial drive timings from the chipset.
496  */
497 static void __init retrieve_drive_counts (unsigned int index)
498 {
499         u8 b;
500
501         /*
502          * Get the internal setup timing, and convert to clock count
503          */
504         b = get_cmd640_reg(arttim_regs[index]) & ~0x3f;
505         switch (b) {
506                 case 0x00: b = 4; break;
507                 case 0x80: b = 3; break;
508                 case 0x40: b = 2; break;
509                 default:   b = 5; break;
510         }
511         setup_counts[index] = b;
512
513         /*
514          * Get the active/recovery counts
515          */
516         b = get_cmd640_reg(drwtim_regs[index]);
517         active_counts[index]   = (b >> 4)   ? (b >> 4)   : 0x10;
518         recovery_counts[index] = (b & 0x0f) ? (b & 0x0f) : 0x10;
519 }
520
521
522 /*
523  * This routine writes the prepared setup/active/recovery counts
524  * for a drive into the cmd640 chipset registers to active them.
525  */
526 static void program_drive_counts (unsigned int index)
527 {
528         unsigned long flags;
529         u8 setup_count    = setup_counts[index];
530         u8 active_count   = active_counts[index];
531         u8 recovery_count = recovery_counts[index];
532
533         /*
534          * Set up address setup count and drive read/write timing registers.
535          * Primary interface has individual count/timing registers for
536          * each drive.  Secondary interface has one common set of registers,
537          * so we merge the timings, using the slowest value for each timing.
538          */
539         if (index > 1) {
540                 unsigned int mate;
541                 if (cmd_drives[mate = index ^ 1]->present) {
542                         if (setup_count < setup_counts[mate])
543                                 setup_count = setup_counts[mate];
544                         if (active_count < active_counts[mate])
545                                 active_count = active_counts[mate];
546                         if (recovery_count < recovery_counts[mate])
547                                 recovery_count = recovery_counts[mate];
548                 }
549         }
550
551         /*
552          * Convert setup_count to internal chipset representation
553          */
554         switch (setup_count) {
555                 case 4:  setup_count = 0x00; break;
556                 case 3:  setup_count = 0x80; break;
557                 case 1:
558                 case 2:  setup_count = 0x40; break;
559                 default: setup_count = 0xc0; /* case 5 */
560         }
561
562         /*
563          * Now that everything is ready, program the new timings
564          */
565         spin_lock_irqsave(&ide_lock, flags);
566         /*
567          * Program the address_setup clocks into ARTTIM reg,
568          * and then the active/recovery counts into the DRWTIM reg
569          * (this converts counts of 16 into counts of zero -- okay).
570          */
571         setup_count |= __get_cmd640_reg(arttim_regs[index]) & 0x3f;
572         __put_cmd640_reg(arttim_regs[index], setup_count);
573         __put_cmd640_reg(drwtim_regs[index], pack_nibbles(active_count, recovery_count));
574         spin_unlock_irqrestore(&ide_lock, flags);
575 }
576
577 /*
578  * Set a specific pio_mode for a drive
579  */
580 static void cmd640_set_mode (unsigned int index, u8 pio_mode, unsigned int cycle_time)
581 {
582         int setup_time, active_time, recovery_time, clock_time;
583         u8 setup_count, active_count, recovery_count, recovery_count2, cycle_count;
584         int bus_speed = system_bus_clock();
585
586         if (pio_mode > 5)
587                 pio_mode = 5;
588         setup_time  = ide_pio_timings[pio_mode].setup_time;
589         active_time = ide_pio_timings[pio_mode].active_time;
590         recovery_time = cycle_time - (setup_time + active_time);
591         clock_time = 1000 / bus_speed;
592         cycle_count = (cycle_time + clock_time - 1) / clock_time;
593
594         setup_count = (setup_time + clock_time - 1) / clock_time;
595
596         active_count = (active_time + clock_time - 1) / clock_time;
597         if (active_count < 2)
598                 active_count = 2; /* minimum allowed by cmd640 */
599
600         recovery_count = (recovery_time + clock_time - 1) / clock_time;
601         recovery_count2 = cycle_count - (setup_count + active_count);
602         if (recovery_count2 > recovery_count)
603                 recovery_count = recovery_count2;
604         if (recovery_count < 2)
605                 recovery_count = 2; /* minimum allowed by cmd640 */
606         if (recovery_count > 17) {
607                 active_count += recovery_count - 17;
608                 recovery_count = 17;
609         }
610         if (active_count > 16)
611                 active_count = 16; /* maximum allowed by cmd640 */
612         if (cmd640_chip_version > 1)
613                 recovery_count -= 1;  /* cmd640b uses (count + 1)*/
614         if (recovery_count > 16)
615                 recovery_count = 16; /* maximum allowed by cmd640 */
616
617         setup_counts[index]    = setup_count;
618         active_counts[index]   = active_count;
619         recovery_counts[index] = recovery_count;
620
621         /*
622          * In a perfect world, we might set the drive pio mode here
623          * (using WIN_SETFEATURE) before continuing.
624          *
625          * But we do not, because:
626          *      1) this is the wrong place to do it (proper is do_special() in ide.c)
627          *      2) in practice this is rarely, if ever, necessary
628          */
629         program_drive_counts (index);
630 }
631
632 /*
633  * Drive PIO mode selection:
634  */
635 static void cmd640_tune_drive (ide_drive_t *drive, u8 mode_wanted)
636 {
637         u8 b;
638         ide_pio_data_t  d;
639         unsigned int index = 0;
640
641         while (drive != cmd_drives[index]) {
642                 if (++index > 3) {
643                         printk("%s: bad news in cmd640_tune_drive\n", drive->name);
644                         return;
645                 }
646         }
647         switch (mode_wanted) {
648                 case 6: /* set fast-devsel off */
649                 case 7: /* set fast-devsel on */
650                         mode_wanted &= 1;
651                         b = get_cmd640_reg(CNTRL) & ~0x27;
652                         if (mode_wanted)
653                                 b |= 0x27;
654                         put_cmd640_reg(CNTRL, b);
655                         printk("%s: %sabled cmd640 fast host timing (devsel)\n", drive->name, mode_wanted ? "en" : "dis");
656                         return;
657
658                 case 8: /* set prefetch off */
659                 case 9: /* set prefetch on */
660                         mode_wanted &= 1;
661                         set_prefetch_mode(index, mode_wanted);
662                         printk("%s: %sabled cmd640 prefetch\n", drive->name, mode_wanted ? "en" : "dis");
663                         return;
664         }
665
666         (void) ide_get_best_pio_mode (drive, mode_wanted, 5, &d);
667         cmd640_set_mode (index, d.pio_mode, d.cycle_time);
668
669         printk ("%s: selected cmd640 PIO mode%d (%dns)%s",
670                 drive->name,
671                 d.pio_mode,
672                 d.cycle_time,
673                 d.overridden ? " (overriding vendor mode)" : "");
674         display_clocks(index);
675         return;
676 }
677
678 #endif /* CONFIG_BLK_DEV_CMD640_ENHANCED */
679
680 static int pci_conf1(void)
681 {
682         u32 tmp;
683         unsigned long flags;
684
685         spin_lock_irqsave(&ide_lock, flags);
686         outb(0x01, 0xCFB);
687         tmp = inl(0xCF8);
688         outl(0x80000000, 0xCF8);
689         if (inl(0xCF8) == 0x80000000) {
690                 outl(tmp, 0xCF8);
691                 spin_unlock_irqrestore(&ide_lock, flags);
692                 return 1;
693         }
694         outl(tmp, 0xCF8);
695         spin_unlock_irqrestore(&ide_lock, flags);
696         return 0;
697 }
698
699 static int pci_conf2(void)
700 {
701         unsigned long flags;
702
703         spin_lock_irqsave(&ide_lock, flags);
704         outb(0x00, 0xCFB);
705         outb(0x00, 0xCF8);
706         outb(0x00, 0xCFA);
707         if (inb(0xCF8) == 0x00 && inb(0xCF8) == 0x00) {
708                 spin_unlock_irqrestore(&ide_lock, flags);
709                 return 1;
710         }
711         spin_unlock_irqrestore(&ide_lock, flags);
712         return 0;
713 }
714
715 /*
716  * Probe for a cmd640 chipset, and initialize it if found.  Called from ide.c
717  */
718 int __init ide_probe_for_cmd640x (void)
719 {
720 #ifdef CONFIG_BLK_DEV_CMD640_ENHANCED
721         int second_port_toggled = 0;
722 #endif /* CONFIG_BLK_DEV_CMD640_ENHANCED */
723         int second_port_cmd640 = 0;
724         const char *bus_type, *port2;
725         unsigned int index;
726         u8 b, cfr;
727
728         if (cmd640_vlb && probe_for_cmd640_vlb()) {
729                 bus_type = "VLB";
730         } else {
731                 cmd640_vlb = 0;
732                 /* Find out what kind of PCI probing is supported otherwise
733                    Justin Gibbs will sulk.. */
734                 if (pci_conf1() && probe_for_cmd640_pci1())
735                         bus_type = "PCI (type1)";
736                 else if (pci_conf2() && probe_for_cmd640_pci2())
737                         bus_type = "PCI (type2)";
738                 else
739                         return 0;
740         }
741         /*
742          * Undocumented magic (there is no 0x5b reg in specs)
743          */
744         put_cmd640_reg(0x5b, 0xbd);
745         if (get_cmd640_reg(0x5b) != 0xbd) {
746                 printk(KERN_ERR "ide: cmd640 init failed: wrong value in reg 0x5b\n");
747                 return 0;
748         }
749         put_cmd640_reg(0x5b, 0);
750
751 #ifdef CMD640_DUMP_REGS
752         cmd640_dump_regs();
753 #endif
754
755         /*
756          * Documented magic begins here
757          */
758         cfr = get_cmd640_reg(CFR);
759         cmd640_chip_version = cfr & CFR_DEVREV;
760         if (cmd640_chip_version == 0) {
761                 printk ("ide: bad cmd640 revision: %d\n", cmd640_chip_version);
762                 return 0;
763         }
764
765         /*
766          * Initialize data for primary port
767          */
768         setup_device_ptrs ();
769         printk("%s: buggy cmd640%c interface on %s, config=0x%02x\n",
770                cmd_hwif0->name, 'a' + cmd640_chip_version - 1, bus_type, cfr);
771         cmd_hwif0->chipset = ide_cmd640;
772 #ifdef CONFIG_BLK_DEV_CMD640_ENHANCED
773         cmd_hwif0->tuneproc = &cmd640_tune_drive;
774 #endif /* CONFIG_BLK_DEV_CMD640_ENHANCED */
775
776         /*
777          * Ensure compatibility by always using the slowest timings
778          * for access to the drive's command register block,
779          * and reset the prefetch burstsize to default (512 bytes).
780          *
781          * Maybe we need a way to NOT do these on *some* systems?
782          */
783         put_cmd640_reg(CMDTIM, 0);
784         put_cmd640_reg(BRST, 0x40);
785
786         /*
787          * Try to enable the secondary interface, if not already enabled
788          */
789         if (cmd_hwif1->noprobe) {
790                 port2 = "not probed";
791         } else {
792                 b = get_cmd640_reg(CNTRL);
793                 if (secondary_port_responding()) {
794                         if ((b & CNTRL_ENA_2ND)) {
795                                 second_port_cmd640 = 1;
796                                 port2 = "okay";
797                         } else if (cmd640_vlb) {
798                                 second_port_cmd640 = 1;
799                                 port2 = "alive";
800                         } else
801                                 port2 = "not cmd640";
802                 } else {
803                         put_cmd640_reg(CNTRL, b ^ CNTRL_ENA_2ND); /* toggle the bit */
804                         if (secondary_port_responding()) {
805                                 second_port_cmd640 = 1;
806 #ifdef CONFIG_BLK_DEV_CMD640_ENHANCED
807                                 second_port_toggled = 1;
808 #endif /* CONFIG_BLK_DEV_CMD640_ENHANCED */
809                                 port2 = "enabled";
810                         } else {
811                                 put_cmd640_reg(CNTRL, b); /* restore original setting */
812                                 port2 = "not responding";
813                         }
814                 }
815         }
816
817         /*
818          * Initialize data for secondary cmd640 port, if enabled
819          */
820         if (second_port_cmd640) {
821                 cmd_hwif0->serialized = 1;
822                 cmd_hwif1->serialized = 1;
823                 cmd_hwif1->chipset = ide_cmd640;
824                 cmd_hwif0->mate = cmd_hwif1;
825                 cmd_hwif1->mate = cmd_hwif0;
826                 cmd_hwif1->channel = 1;
827 #ifdef CONFIG_BLK_DEV_CMD640_ENHANCED
828                 cmd_hwif1->tuneproc = &cmd640_tune_drive;
829 #endif /* CONFIG_BLK_DEV_CMD640_ENHANCED */
830         }
831         printk(KERN_INFO "%s: %sserialized, secondary interface %s\n", cmd_hwif1->name,
832                 cmd_hwif0->serialized ? "" : "not ", port2);
833
834         /*
835          * Establish initial timings/prefetch for all drives.
836          * Do not unnecessarily disturb any prior BIOS setup of these.
837          */
838         for (index = 0; index < (2 + (second_port_cmd640 << 1)); index++) {
839                 ide_drive_t *drive = cmd_drives[index];
840 #ifdef CONFIG_BLK_DEV_CMD640_ENHANCED
841                 if (drive->autotune || ((index > 1) && second_port_toggled)) {
842                         /*
843                          * Reset timing to the slowest speed and turn off prefetch.
844                          * This way, the drive identify code has a better chance.
845                          */
846                         setup_counts    [index] = 4;    /* max possible */
847                         active_counts   [index] = 16;   /* max possible */
848                         recovery_counts [index] = 16;   /* max possible */
849                         program_drive_counts (index);
850                         set_prefetch_mode (index, 0);
851                         printk("cmd640: drive%d timings/prefetch cleared\n", index);
852                 } else {
853                         /*
854                          * Record timings/prefetch without changing them.
855                          * This preserves any prior BIOS setup.
856                          */
857                         retrieve_drive_counts (index);
858                         check_prefetch (index);
859                         printk("cmd640: drive%d timings/prefetch(%s) preserved",
860                                 index, drive->no_io_32bit ? "off" : "on");
861                         display_clocks(index);
862                 }
863 #else
864                 /*
865                  * Set the drive unmask flags to match the prefetch setting
866                  */
867                 check_prefetch (index);
868                 printk("cmd640: drive%d timings/prefetch(%s) preserved\n",
869                         index, drive->no_io_32bit ? "off" : "on");
870 #endif /* CONFIG_BLK_DEV_CMD640_ENHANCED */
871         }
872
873 #ifdef CMD640_DUMP_REGS
874         cmd640_dump_regs();
875 #endif
876         return 1;
877 }
878