Import 2.3.9pre5
[davej-history.git] / drivers / block / cmd640.c
blobee6d7c9cc96bd74b092ce2e2908580709db4b9fd
1 /*
2 * linux/drivers/block/cmd640.c Version 1.02 Sep 01, 1996
4 * Copyright (C) 1995-1996 Linus Torvalds & authors (see below)
5 */
7 /*
8 * Original authors: abramov@cecmow.enet.dec.com (Igor Abramov)
9 * mlord@pobox.com (Mark Lord)
11 * See linux/MAINTAINERS for address of current maintainer.
13 * This file provides support for the advanced features and bugs
14 * of IDE interfaces using the CMD Technologies 0640 IDE interface chip.
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.
20 * Version 0.97 worked for everybody.
22 * User feedback is essential. Many thanks to the beta test team:
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.
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.
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.
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.
50 * Version 0.04 Now configs/compiles separate from ide.c
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.
56 * Version 0.06 More code cleanup. Prefetch is enabled only for
57 * detected hard drives, not included in prefetch
58 * black list.
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.
66 * Version 0.08 Added autotune/noautotune support.
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 wierd situations.
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)
101 #undef REALLY_SLOW_IO /* most systems can safely undef this */
102 #define CMD640_PREFETCH_MASKS 1
104 #include <linux/config.h>
105 #include <linux/types.h>
106 #include <linux/kernel.h>
107 #include <linux/delay.h>
108 #include <linux/timer.h>
109 #include <linux/mm.h>
110 #include <linux/ioport.h>
111 #include <linux/blkdev.h>
112 #include <linux/hdreg.h>
113 #include <linux/ide.h>
115 #include <asm/io.h>
117 #include "ide_modes.h"
120 * This flag is set in ide.c by the parameter: ide0=cmd640_vlb
122 int cmd640_vlb = 0;
125 * CMD640 specific registers definition.
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
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
152 #define CNTRL 0x51
153 #define CNTRL_DIS_RA0 0x40
154 #define CNTRL_DIS_RA1 0x80
155 #define CNTRL_ENA_2ND 0x08
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
169 * Registers and masks for easy access by drive index:
171 static byte prefetch_regs[4] = {CNTRL, CNTRL, ARTTIM23, ARTTIM23};
172 static byte prefetch_masks[4] = {CNTRL_DIS_RA0, CNTRL_DIS_RA1, ARTTIM23_DIS_RA2, ARTTIM23_DIS_RA3};
174 #ifdef CONFIG_BLK_DEV_CMD640_ENHANCED
176 static byte arttim_regs[4] = {ARTTIM0, ARTTIM1, ARTTIM23, ARTTIM23};
177 static byte drwtim_regs[4] = {DRWTIM0, DRWTIM1, DRWTIM23, DRWTIM23};
180 * Current cmd640 timing values for each drive.
181 * The defaults for each are the slowest possible timings.
183 static byte setup_counts[4] = {4, 4, 4, 4}; /* Address setup count (in clocks) */
184 static byte active_counts[4] = {16, 16, 16, 16}; /* Active count (encoded) */
185 static byte recovery_counts[4] = {16, 16, 16, 16}; /* Recovery count (encoded) */
187 #endif /* CONFIG_BLK_DEV_CMD640_ENHANCED */
190 * These are initialized to point at the devices we control
192 static ide_hwif_t *cmd_hwif0, *cmd_hwif1;
193 static ide_drive_t *cmd_drives[4];
196 * Interface to access cmd640x registers
198 static unsigned int cmd640_key;
199 static void (*put_cmd640_reg)(unsigned short reg, byte val);
200 static byte (*get_cmd640_reg)(unsigned short reg);
203 * This is read from the CFR reg, and is used in several places.
205 static unsigned int cmd640_chip_version;
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.
213 /* PCI method 1 access */
215 static void put_cmd640_reg_pci1 (unsigned short reg, byte val)
217 unsigned long flags;
219 save_flags(flags);
220 cli();
221 outl_p((reg & 0xfc) | cmd640_key, 0xcf8);
222 outb_p(val, (reg & 3) | 0xcfc);
223 restore_flags(flags);
226 static byte get_cmd640_reg_pci1 (unsigned short reg)
228 byte b;
229 unsigned long flags;
231 save_flags(flags);
232 cli();
233 outl_p((reg & 0xfc) | cmd640_key, 0xcf8);
234 b = inb_p((reg & 3) | 0xcfc);
235 restore_flags(flags);
236 return b;
239 /* PCI method 2 access (from CMD datasheet) */
241 static void put_cmd640_reg_pci2 (unsigned short reg, byte val)
243 unsigned long flags;
245 save_flags(flags);
246 cli();
247 outb_p(0x10, 0xcf8);
248 outb_p(val, cmd640_key + reg);
249 outb_p(0, 0xcf8);
250 restore_flags(flags);
253 static byte get_cmd640_reg_pci2 (unsigned short reg)
255 byte b;
256 unsigned long flags;
258 save_flags(flags);
259 cli();
260 outb_p(0x10, 0xcf8);
261 b = inb_p(cmd640_key + reg);
262 outb_p(0, 0xcf8);
263 restore_flags(flags);
264 return b;
267 /* VLB access */
269 static void put_cmd640_reg_vlb (unsigned short reg, byte val)
271 unsigned long flags;
273 save_flags(flags);
274 cli();
275 outb_p(reg, cmd640_key);
276 outb_p(val, cmd640_key + 4);
277 restore_flags(flags);
280 static byte get_cmd640_reg_vlb (unsigned short reg)
282 byte b;
283 unsigned long flags;
285 save_flags(flags);
286 cli();
287 outb_p(reg, cmd640_key);
288 b = inb_p(cmd640_key + 4);
289 restore_flags(flags);
290 return b;
293 static int match_pci_cmd640_device (void)
295 const byte ven_dev[4] = {0x95, 0x10, 0x40, 0x06};
296 unsigned int i;
297 for (i = 0; i < 4; i++) {
298 if (get_cmd640_reg(i) != ven_dev[i])
299 return 0;
301 #ifdef STUPIDLY_TRUST_BROKEN_PCMD_ENA_BIT
302 if ((get_cmd640_reg(PCMD) & PCMD_ENA) == 0) {
303 printk("ide: cmd640 on PCI disabled by BIOS\n");
304 return 0;
306 #endif /* STUPIDLY_TRUST_BROKEN_PCMD_ENA_BIT */
307 return 1; /* success */
311 * Probe for CMD640x -- pci method 1
313 static int probe_for_cmd640_pci1 (void)
315 get_cmd640_reg = get_cmd640_reg_pci1;
316 put_cmd640_reg = put_cmd640_reg_pci1;
317 for (cmd640_key = 0x80000000; cmd640_key <= 0x8000f800; cmd640_key += 0x800) {
318 if (match_pci_cmd640_device())
319 return 1; /* success */
321 return 0;
325 * Probe for CMD640x -- pci method 2
327 static int probe_for_cmd640_pci2 (void)
329 get_cmd640_reg = get_cmd640_reg_pci2;
330 put_cmd640_reg = put_cmd640_reg_pci2;
331 for (cmd640_key = 0xc000; cmd640_key <= 0xcf00; cmd640_key += 0x100) {
332 if (match_pci_cmd640_device())
333 return 1; /* success */
335 return 0;
339 * Probe for CMD640x -- vlb
341 static int probe_for_cmd640_vlb (void)
343 byte b;
345 get_cmd640_reg = get_cmd640_reg_vlb;
346 put_cmd640_reg = put_cmd640_reg_vlb;
347 cmd640_key = 0x178;
348 b = get_cmd640_reg(CFR);
349 if (b == 0xff || b == 0x00 || (b & CFR_AT_VESA_078h)) {
350 cmd640_key = 0x78;
351 b = get_cmd640_reg(CFR);
352 if (b == 0xff || b == 0x00 || !(b & CFR_AT_VESA_078h))
353 return 0;
355 return 1; /* success */
359 * Returns 1 if an IDE interface/drive exists at 0x170,
360 * Returns 0 otherwise.
362 static int secondary_port_responding (void)
364 unsigned long flags;
366 save_flags(flags);
367 cli();
369 outb_p(0x0a, 0x170 + IDE_SELECT_OFFSET); /* select drive0 */
370 udelay(100);
371 if ((inb_p(0x170 + IDE_SELECT_OFFSET) & 0x1f) != 0x0a) {
372 outb_p(0x1a, 0x170 + IDE_SELECT_OFFSET); /* select drive1 */
373 udelay(100);
374 if ((inb_p(0x170 + IDE_SELECT_OFFSET) & 0x1f) != 0x1a) {
375 restore_flags(flags);
376 return 0; /* nothing responded */
379 restore_flags(flags);
380 return 1; /* success */
383 #ifdef CMD640_DUMP_REGS
385 * Dump out all cmd640 registers. May be called from ide.c
387 void cmd640_dump_regs (void)
389 unsigned int reg = cmd640_vlb ? 0x50 : 0x00;
391 /* Dump current state of chip registers */
392 printk("ide: cmd640 internal register dump:");
393 for (; reg <= 0x59; reg++) {
394 if (!(reg & 0x0f))
395 printk("\n%04x:", reg);
396 printk(" %02x", get_cmd640_reg(reg));
398 printk("\n");
400 #endif
403 * Check whether prefetch is on for a drive,
404 * and initialize the unmask flags for safe operation.
406 static void check_prefetch (unsigned int index)
408 ide_drive_t *drive = cmd_drives[index];
409 byte b = get_cmd640_reg(prefetch_regs[index]);
411 if (b & prefetch_masks[index]) { /* is prefetch off? */
412 drive->no_unmask = 0;
413 drive->no_io_32bit = 1;
414 drive->io_32bit = 0;
415 } else {
416 #if CMD640_PREFETCH_MASKS
417 drive->no_unmask = 1;
418 drive->unmask = 0;
419 #endif
420 drive->no_io_32bit = 0;
425 * Figure out which devices we control
427 static void setup_device_ptrs (void)
429 unsigned int i;
431 cmd_hwif0 = &ide_hwifs[0]; /* default, if not found below */
432 cmd_hwif1 = &ide_hwifs[1]; /* default, if not found below */
433 for (i = 0; i < MAX_HWIFS; i++) {
434 ide_hwif_t *hwif = &ide_hwifs[i];
435 if (hwif->chipset == ide_unknown || hwif->chipset == ide_generic) {
436 if (hwif->io_ports[IDE_DATA_OFFSET] == 0x1f0)
437 cmd_hwif0 = hwif;
438 else if (hwif->io_ports[IDE_DATA_OFFSET] == 0x170)
439 cmd_hwif1 = hwif;
442 cmd_drives[0] = &cmd_hwif0->drives[0];
443 cmd_drives[1] = &cmd_hwif0->drives[1];
444 cmd_drives[2] = &cmd_hwif1->drives[0];
445 cmd_drives[3] = &cmd_hwif1->drives[1];
448 #ifdef CONFIG_BLK_DEV_CMD640_ENHANCED
451 * Sets prefetch mode for a drive.
453 static void set_prefetch_mode (unsigned int index, int mode)
455 ide_drive_t *drive = cmd_drives[index];
456 int reg = prefetch_regs[index];
457 byte b;
458 unsigned long flags;
460 save_flags(flags);
461 cli();
462 b = get_cmd640_reg(reg);
463 if (mode) { /* want prefetch on? */
464 #if CMD640_PREFETCH_MASKS
465 drive->no_unmask = 1;
466 drive->unmask = 0;
467 #endif
468 drive->no_io_32bit = 0;
469 b &= ~prefetch_masks[index]; /* enable prefetch */
470 } else {
471 drive->no_unmask = 0;
472 drive->no_io_32bit = 1;
473 drive->io_32bit = 0;
474 b |= prefetch_masks[index]; /* disable prefetch */
476 put_cmd640_reg(reg, b);
477 restore_flags(flags);
481 * Dump out current drive clocks settings
483 static void display_clocks (unsigned int index)
485 byte active_count, recovery_count;
487 active_count = active_counts[index];
488 if (active_count == 1)
489 ++active_count;
490 recovery_count = recovery_counts[index];
491 if (active_count > 3 && recovery_count == 1)
492 ++recovery_count;
493 if (cmd640_chip_version > 1)
494 recovery_count += 1; /* cmd640b uses (count + 1)*/
495 printk(", clocks=%d/%d/%d\n", setup_counts[index], active_count, recovery_count);
499 * Pack active and recovery counts into single byte representation
500 * used by controller
502 inline static byte pack_nibbles (byte upper, byte lower)
504 return ((upper & 0x0f) << 4) | (lower & 0x0f);
508 * This routine retrieves the initial drive timings from the chipset.
510 static void retrieve_drive_counts (unsigned int index)
512 byte b;
515 * Get the internal setup timing, and convert to clock count
517 b = get_cmd640_reg(arttim_regs[index]) & ~0x3f;
518 switch (b) {
519 case 0x00: b = 4; break;
520 case 0x80: b = 3; break;
521 case 0x40: b = 2; break;
522 default: b = 5; break;
524 setup_counts[index] = b;
527 * Get the active/recovery counts
529 b = get_cmd640_reg(drwtim_regs[index]);
530 active_counts[index] = (b >> 4) ? (b >> 4) : 0x10;
531 recovery_counts[index] = (b & 0x0f) ? (b & 0x0f) : 0x10;
536 * This routine writes the prepared setup/active/recovery counts
537 * for a drive into the cmd640 chipset registers to active them.
539 static void program_drive_counts (unsigned int index)
541 unsigned long flags;
542 byte setup_count = setup_counts[index];
543 byte active_count = active_counts[index];
544 byte recovery_count = recovery_counts[index];
547 * Set up address setup count and drive read/write timing registers.
548 * Primary interface has individual count/timing registers for
549 * each drive. Secondary interface has one common set of registers,
550 * so we merge the timings, using the slowest value for each timing.
552 if (index > 1) {
553 unsigned int mate;
554 if (cmd_drives[mate = index ^ 1]->present) {
555 if (setup_count < setup_counts[mate])
556 setup_count = setup_counts[mate];
557 if (active_count < active_counts[mate])
558 active_count = active_counts[mate];
559 if (recovery_count < recovery_counts[mate])
560 recovery_count = recovery_counts[mate];
565 * Convert setup_count to internal chipset representation
567 switch (setup_count) {
568 case 4: setup_count = 0x00; break;
569 case 3: setup_count = 0x80; break;
570 case 1:
571 case 2: setup_count = 0x40; break;
572 default: setup_count = 0xc0; /* case 5 */
576 * Now that everything is ready, program the new timings
578 save_flags (flags);
579 cli();
581 * Program the address_setup clocks into ARTTIM reg,
582 * and then the active/recovery counts into the DRWTIM reg
583 * (this converts counts of 16 into counts of zero -- okay).
585 setup_count |= get_cmd640_reg(arttim_regs[index]) & 0x3f;
586 put_cmd640_reg(arttim_regs[index], setup_count);
587 put_cmd640_reg(drwtim_regs[index], pack_nibbles(active_count, recovery_count));
588 restore_flags(flags);
592 * Set a specific pio_mode for a drive
594 static void cmd640_set_mode (unsigned int index, byte pio_mode, unsigned int cycle_time)
596 int setup_time, active_time, recovery_time, clock_time;
597 byte setup_count, active_count, recovery_count, recovery_count2, cycle_count;
598 int bus_speed = ide_system_bus_speed();
600 if (pio_mode > 5)
601 pio_mode = 5;
602 setup_time = ide_pio_timings[pio_mode].setup_time;
603 active_time = ide_pio_timings[pio_mode].active_time;
604 recovery_time = cycle_time - (setup_time + active_time);
605 clock_time = 1000 / bus_speed;
606 cycle_count = (cycle_time + clock_time - 1) / clock_time;
608 setup_count = (setup_time + clock_time - 1) / clock_time;
610 active_count = (active_time + clock_time - 1) / clock_time;
611 if (active_count < 2)
612 active_count = 2; /* minimum allowed by cmd640 */
614 recovery_count = (recovery_time + clock_time - 1) / clock_time;
615 recovery_count2 = cycle_count - (setup_count + active_count);
616 if (recovery_count2 > recovery_count)
617 recovery_count = recovery_count2;
618 if (recovery_count < 2)
619 recovery_count = 2; /* minimum allowed by cmd640 */
620 if (recovery_count > 17) {
621 active_count += recovery_count - 17;
622 recovery_count = 17;
624 if (active_count > 16)
625 active_count = 16; /* maximum allowed by cmd640 */
626 if (cmd640_chip_version > 1)
627 recovery_count -= 1; /* cmd640b uses (count + 1)*/
628 if (recovery_count > 16)
629 recovery_count = 16; /* maximum allowed by cmd640 */
631 setup_counts[index] = setup_count;
632 active_counts[index] = active_count;
633 recovery_counts[index] = recovery_count;
636 * In a perfect world, we might set the drive pio mode here
637 * (using WIN_SETFEATURE) before continuing.
639 * But we do not, because:
640 * 1) this is the wrong place to do it (proper is do_special() in ide.c)
641 * 2) in practice this is rarely, if ever, necessary
643 program_drive_counts (index);
647 * Drive PIO mode selection:
649 static void cmd640_tune_drive (ide_drive_t *drive, byte mode_wanted)
651 byte b;
652 ide_pio_data_t d;
653 unsigned int index = 0;
655 while (drive != cmd_drives[index]) {
656 if (++index > 3) {
657 printk("%s: bad news in cmd640_tune_drive\n", drive->name);
658 return;
661 switch (mode_wanted) {
662 case 6: /* set fast-devsel off */
663 case 7: /* set fast-devsel on */
664 mode_wanted &= 1;
665 b = get_cmd640_reg(CNTRL) & ~0x27;
666 if (mode_wanted)
667 b |= 0x27;
668 put_cmd640_reg(CNTRL, b);
669 printk("%s: %sabled cmd640 fast host timing (devsel)\n", drive->name, mode_wanted ? "en" : "dis");
670 return;
672 case 8: /* set prefetch off */
673 case 9: /* set prefetch on */
674 mode_wanted &= 1;
675 set_prefetch_mode(index, mode_wanted);
676 printk("%s: %sabled cmd640 prefetch\n", drive->name, mode_wanted ? "en" : "dis");
677 return;
680 (void) ide_get_best_pio_mode (drive, mode_wanted, 5, &d);
681 cmd640_set_mode (index, d.pio_mode, d.cycle_time);
683 printk ("%s: selected cmd640 PIO mode%d (%dns)%s",
684 drive->name,
685 d.pio_mode,
686 d.cycle_time,
687 d.overridden ? " (overriding vendor mode)" : "");
688 display_clocks(index);
691 #endif /* CONFIG_BLK_DEV_CMD640_ENHANCED */
694 * Probe for a cmd640 chipset, and initialize it if found. Called from ide.c
696 int ide_probe_for_cmd640x (void)
698 #ifdef CONFIG_BLK_DEV_CMD640_ENHANCED
699 int second_port_toggled = 0;
700 #endif /* CONFIG_BLK_DEV_CMD640_ENHANCED */
701 int second_port_cmd640 = 0;
702 const char *bus_type, *port2;
703 unsigned int index;
704 byte b, cfr;
706 if (cmd640_vlb && probe_for_cmd640_vlb()) {
707 bus_type = "VLB";
708 } else {
709 cmd640_vlb = 0;
710 if (probe_for_cmd640_pci1())
711 bus_type = "PCI (type1)";
712 else if (probe_for_cmd640_pci2())
713 bus_type = "PCI (type2)";
714 else
715 return 0;
718 * Undocumented magic (there is no 0x5b reg in specs)
720 put_cmd640_reg(0x5b, 0xbd);
721 if (get_cmd640_reg(0x5b) != 0xbd) {
722 printk("ide: cmd640 init failed: wrong value in reg 0x5b\n");
723 return 0;
725 put_cmd640_reg(0x5b, 0);
727 #ifdef CMD640_DUMP_REGS
728 CMD640_DUMP_REGS;
729 #endif
732 * Documented magic begins here
734 cfr = get_cmd640_reg(CFR);
735 cmd640_chip_version = cfr & CFR_DEVREV;
736 if (cmd640_chip_version == 0) {
737 printk ("ide: bad cmd640 revision: %d\n", cmd640_chip_version);
738 return 0;
742 * Initialize data for primary port
744 setup_device_ptrs ();
745 printk("%s: buggy cmd640%c interface on %s, config=0x%02x\n",
746 cmd_hwif0->name, 'a' + cmd640_chip_version - 1, bus_type, cfr);
747 cmd_hwif0->chipset = ide_cmd640;
748 #ifdef CONFIG_BLK_DEV_CMD640_ENHANCED
749 cmd_hwif0->tuneproc = &cmd640_tune_drive;
750 #endif /* CONFIG_BLK_DEV_CMD640_ENHANCED */
753 * Ensure compatibility by always using the slowest timings
754 * for access to the drive's command register block,
755 * and reset the prefetch burstsize to default (512 bytes).
757 * Maybe we need a way to NOT do these on *some* systems?
759 put_cmd640_reg(CMDTIM, 0);
760 put_cmd640_reg(BRST, 0x40);
763 * Try to enable the secondary interface, if not already enabled
765 if (cmd_hwif1->noprobe) {
766 port2 = "not probed";
767 } else {
768 b = get_cmd640_reg(CNTRL);
769 if (secondary_port_responding()) {
770 if ((b & CNTRL_ENA_2ND)) {
771 second_port_cmd640 = 1;
772 port2 = "okay";
773 } else if (cmd640_vlb) {
774 second_port_cmd640 = 1;
775 port2 = "alive";
776 } else
777 port2 = "not cmd640";
778 } else {
779 put_cmd640_reg(CNTRL, b ^ CNTRL_ENA_2ND); /* toggle the bit */
780 if (secondary_port_responding()) {
781 second_port_cmd640 = 1;
782 #ifdef CONFIG_BLK_DEV_CMD640_ENHANCED
783 second_port_toggled = 1;
784 #endif /* CONFIG_BLK_DEV_CMD640_ENHANCED */
785 port2 = "enabled";
786 } else {
787 put_cmd640_reg(CNTRL, b); /* restore original setting */
788 port2 = "not responding";
794 * Initialize data for secondary cmd640 port, if enabled
796 if (second_port_cmd640) {
797 cmd_hwif0->serialized = 1;
798 cmd_hwif1->serialized = 1;
799 cmd_hwif1->chipset = ide_cmd640;
800 cmd_hwif0->mate = cmd_hwif1;
801 cmd_hwif1->mate = cmd_hwif0;
802 cmd_hwif1->channel = 1;
803 #ifdef CONFIG_BLK_DEV_CMD640_ENHANCED
804 cmd_hwif1->tuneproc = &cmd640_tune_drive;
805 #endif /* CONFIG_BLK_DEV_CMD640_ENHANCED */
807 printk("%s: %sserialized, secondary interface %s\n", cmd_hwif1->name,
808 cmd_hwif0->serialized ? "" : "not ", port2);
811 * Establish initial timings/prefetch for all drives.
812 * Do not unnecessarily disturb any prior BIOS setup of these.
814 for (index = 0; index < (2 + (second_port_cmd640 << 1)); index++) {
815 ide_drive_t *drive = cmd_drives[index];
816 #ifdef CONFIG_BLK_DEV_CMD640_ENHANCED
817 if (drive->autotune || ((index > 1) && second_port_toggled)) {
819 * Reset timing to the slowest speed and turn off prefetch.
820 * This way, the drive identify code has a better chance.
822 setup_counts [index] = 4; /* max possible */
823 active_counts [index] = 16; /* max possible */
824 recovery_counts [index] = 16; /* max possible */
825 program_drive_counts (index);
826 set_prefetch_mode (index, 0);
827 printk("cmd640: drive%d timings/prefetch cleared\n", index);
828 } else {
830 * Record timings/prefetch without changing them.
831 * This preserves any prior BIOS setup.
833 retrieve_drive_counts (index);
834 check_prefetch (index);
835 printk("cmd640: drive%d timings/prefetch(%s) preserved",
836 index, drive->no_io_32bit ? "off" : "on");
837 display_clocks(index);
839 #else
841 * Set the drive unmask flags to match the prefetch setting
843 check_prefetch (index);
844 printk("cmd640: drive%d timings/prefetch(%s) preserved\n",
845 index, drive->no_io_32bit ? "off" : "on");
846 #endif /* CONFIG_BLK_DEV_CMD640_ENHANCED */
849 #ifdef CMD640_DUMP_REGS
850 CMD640_DUMP_REGS;
851 #endif
852 return 1;