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