blob: dbf0fa733b765caf9583bb3250bf8926521244e7 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106#include <linux/hdreg.h>
107#include <linux/ide.h>
108#include <linux/init.h>
109
110#include <asm/io.h>
111
112/*
113 * This flag is set in ide.c by the parameter: ide0=cmd640_vlb
114 */
115int cmd640_vlb = 0;
116
117/*
118 * CMD640 specific registers definition.
119 */
120
121#define VID 0x00
122#define DID 0x02
123#define PCMD 0x04
124#define PCMD_ENA 0x01
125#define PSTTS 0x06
126#define REVID 0x08
127#define PROGIF 0x09
128#define SUBCL 0x0a
129#define BASCL 0x0b
130#define BaseA0 0x10
131#define BaseA1 0x14
132#define BaseA2 0x18
133#define BaseA3 0x1c
134#define INTLINE 0x3c
135#define INPINE 0x3d
136
137#define CFR 0x50
138#define CFR_DEVREV 0x03
139#define CFR_IDE01INTR 0x04
140#define CFR_DEVID 0x18
141#define CFR_AT_VESA_078h 0x20
142#define CFR_DSA1 0x40
143#define CFR_DSA0 0x80
144
145#define CNTRL 0x51
146#define CNTRL_DIS_RA0 0x40
147#define CNTRL_DIS_RA1 0x80
148#define CNTRL_ENA_2ND 0x08
149
150#define CMDTIM 0x52
151#define ARTTIM0 0x53
152#define DRWTIM0 0x54
153#define ARTTIM1 0x55
154#define DRWTIM1 0x56
155#define ARTTIM23 0x57
156#define ARTTIM23_DIS_RA2 0x04
157#define ARTTIM23_DIS_RA3 0x08
158#define DRWTIM23 0x58
159#define BRST 0x59
160
161/*
162 * Registers and masks for easy access by drive index:
163 */
164static u8 prefetch_regs[4] = {CNTRL, CNTRL, ARTTIM23, ARTTIM23};
165static u8 prefetch_masks[4] = {CNTRL_DIS_RA0, CNTRL_DIS_RA1, ARTTIM23_DIS_RA2, ARTTIM23_DIS_RA3};
166
167#ifdef CONFIG_BLK_DEV_CMD640_ENHANCED
168
169static u8 arttim_regs[4] = {ARTTIM0, ARTTIM1, ARTTIM23, ARTTIM23};
170static u8 drwtim_regs[4] = {DRWTIM0, DRWTIM1, DRWTIM23, DRWTIM23};
171
172/*
173 * Current cmd640 timing values for each drive.
174 * The defaults for each are the slowest possible timings.
175 */
176static u8 setup_counts[4] = {4, 4, 4, 4}; /* Address setup count (in clocks) */
177static u8 active_counts[4] = {16, 16, 16, 16}; /* Active count (encoded) */
178static u8 recovery_counts[4] = {16, 16, 16, 16}; /* Recovery count (encoded) */
179
180#endif /* CONFIG_BLK_DEV_CMD640_ENHANCED */
181
Bartlomiej Zolnierkiewicz5bbcf922007-10-20 00:32:35 +0200182static DEFINE_SPINLOCK(cmd640_lock);
183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184/*
185 * These are initialized to point at the devices we control
186 */
187static ide_hwif_t *cmd_hwif0, *cmd_hwif1;
188static ide_drive_t *cmd_drives[4];
189
190/*
191 * Interface to access cmd640x registers
192 */
193static unsigned int cmd640_key;
194static void (*__put_cmd640_reg)(u16 reg, u8 val);
195static u8 (*__get_cmd640_reg)(u16 reg);
196
197/*
198 * This is read from the CFR reg, and is used in several places.
199 */
200static unsigned int cmd640_chip_version;
201
202/*
203 * The CMD640x chip does not support DWORD config write cycles, but some
204 * of the BIOSes use them to implement the config services.
205 * Therefore, we must use direct IO instead.
206 */
207
208/* PCI method 1 access */
209
210static void put_cmd640_reg_pci1 (u16 reg, u8 val)
211{
212 outl_p((reg & 0xfc) | cmd640_key, 0xcf8);
213 outb_p(val, (reg & 3) | 0xcfc);
214}
215
216static u8 get_cmd640_reg_pci1 (u16 reg)
217{
218 outl_p((reg & 0xfc) | cmd640_key, 0xcf8);
219 return inb_p((reg & 3) | 0xcfc);
220}
221
222/* PCI method 2 access (from CMD datasheet) */
223
224static void put_cmd640_reg_pci2 (u16 reg, u8 val)
225{
226 outb_p(0x10, 0xcf8);
227 outb_p(val, cmd640_key + reg);
228 outb_p(0, 0xcf8);
229}
230
231static u8 get_cmd640_reg_pci2 (u16 reg)
232{
233 u8 b;
234
235 outb_p(0x10, 0xcf8);
236 b = inb_p(cmd640_key + reg);
237 outb_p(0, 0xcf8);
238 return b;
239}
240
241/* VLB access */
242
243static void put_cmd640_reg_vlb (u16 reg, u8 val)
244{
245 outb_p(reg, cmd640_key);
246 outb_p(val, cmd640_key + 4);
247}
248
249static u8 get_cmd640_reg_vlb (u16 reg)
250{
251 outb_p(reg, cmd640_key);
252 return inb_p(cmd640_key + 4);
253}
254
255static u8 get_cmd640_reg(u16 reg)
256{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 unsigned long flags;
Bartlomiej Zolnierkiewicz5bbcf922007-10-20 00:32:35 +0200258 u8 b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
Bartlomiej Zolnierkiewicz5bbcf922007-10-20 00:32:35 +0200260 spin_lock_irqsave(&cmd640_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 b = __get_cmd640_reg(reg);
Bartlomiej Zolnierkiewicz5bbcf922007-10-20 00:32:35 +0200262 spin_unlock_irqrestore(&cmd640_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 return b;
264}
265
266static void put_cmd640_reg(u16 reg, u8 val)
267{
268 unsigned long flags;
269
Bartlomiej Zolnierkiewicz5bbcf922007-10-20 00:32:35 +0200270 spin_lock_irqsave(&cmd640_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 __put_cmd640_reg(reg,val);
Bartlomiej Zolnierkiewicz5bbcf922007-10-20 00:32:35 +0200272 spin_unlock_irqrestore(&cmd640_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273}
274
275static int __init match_pci_cmd640_device (void)
276{
277 const u8 ven_dev[4] = {0x95, 0x10, 0x40, 0x06};
278 unsigned int i;
279 for (i = 0; i < 4; i++) {
280 if (get_cmd640_reg(i) != ven_dev[i])
281 return 0;
282 }
283#ifdef STUPIDLY_TRUST_BROKEN_PCMD_ENA_BIT
284 if ((get_cmd640_reg(PCMD) & PCMD_ENA) == 0) {
285 printk("ide: cmd640 on PCI disabled by BIOS\n");
286 return 0;
287 }
288#endif /* STUPIDLY_TRUST_BROKEN_PCMD_ENA_BIT */
289 return 1; /* success */
290}
291
292/*
293 * Probe for CMD640x -- pci method 1
294 */
295static int __init probe_for_cmd640_pci1 (void)
296{
297 __get_cmd640_reg = get_cmd640_reg_pci1;
298 __put_cmd640_reg = put_cmd640_reg_pci1;
299 for (cmd640_key = 0x80000000;
300 cmd640_key <= 0x8000f800;
301 cmd640_key += 0x800) {
302 if (match_pci_cmd640_device())
303 return 1; /* success */
304 }
305 return 0;
306}
307
308/*
309 * Probe for CMD640x -- pci method 2
310 */
311static int __init probe_for_cmd640_pci2 (void)
312{
313 __get_cmd640_reg = get_cmd640_reg_pci2;
314 __put_cmd640_reg = put_cmd640_reg_pci2;
315 for (cmd640_key = 0xc000; cmd640_key <= 0xcf00; cmd640_key += 0x100) {
316 if (match_pci_cmd640_device())
317 return 1; /* success */
318 }
319 return 0;
320}
321
322/*
323 * Probe for CMD640x -- vlb
324 */
325static int __init probe_for_cmd640_vlb (void)
326{
327 u8 b;
328
329 __get_cmd640_reg = get_cmd640_reg_vlb;
330 __put_cmd640_reg = put_cmd640_reg_vlb;
331 cmd640_key = 0x178;
332 b = get_cmd640_reg(CFR);
333 if (b == 0xff || b == 0x00 || (b & CFR_AT_VESA_078h)) {
334 cmd640_key = 0x78;
335 b = get_cmd640_reg(CFR);
336 if (b == 0xff || b == 0x00 || !(b & CFR_AT_VESA_078h))
337 return 0;
338 }
339 return 1; /* success */
340}
341
342/*
343 * Returns 1 if an IDE interface/drive exists at 0x170,
344 * Returns 0 otherwise.
345 */
346static int __init secondary_port_responding (void)
347{
348 unsigned long flags;
349
Bartlomiej Zolnierkiewicz5bbcf922007-10-20 00:32:35 +0200350 spin_lock_irqsave(&cmd640_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
352 outb_p(0x0a, 0x170 + IDE_SELECT_OFFSET); /* select drive0 */
353 udelay(100);
354 if ((inb_p(0x170 + IDE_SELECT_OFFSET) & 0x1f) != 0x0a) {
355 outb_p(0x1a, 0x170 + IDE_SELECT_OFFSET); /* select drive1 */
356 udelay(100);
357 if ((inb_p(0x170 + IDE_SELECT_OFFSET) & 0x1f) != 0x1a) {
Bartlomiej Zolnierkiewicz5bbcf922007-10-20 00:32:35 +0200358 spin_unlock_irqrestore(&cmd640_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 return 0; /* nothing responded */
360 }
361 }
Bartlomiej Zolnierkiewicz5bbcf922007-10-20 00:32:35 +0200362 spin_unlock_irqrestore(&cmd640_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 return 1; /* success */
364}
365
366#ifdef CMD640_DUMP_REGS
367/*
368 * Dump out all cmd640 registers. May be called from ide.c
369 */
370static void cmd640_dump_regs (void)
371{
372 unsigned int reg = cmd640_vlb ? 0x50 : 0x00;
373
374 /* Dump current state of chip registers */
375 printk("ide: cmd640 internal register dump:");
376 for (; reg <= 0x59; reg++) {
377 if (!(reg & 0x0f))
378 printk("\n%04x:", reg);
379 printk(" %02x", get_cmd640_reg(reg));
380 }
381 printk("\n");
382}
383#endif
384
385/*
386 * Check whether prefetch is on for a drive,
387 * and initialize the unmask flags for safe operation.
388 */
389static void __init check_prefetch (unsigned int index)
390{
391 ide_drive_t *drive = cmd_drives[index];
392 u8 b = get_cmd640_reg(prefetch_regs[index]);
393
394 if (b & prefetch_masks[index]) { /* is prefetch off? */
395 drive->no_unmask = 0;
396 drive->no_io_32bit = 1;
397 drive->io_32bit = 0;
398 } else {
399#if CMD640_PREFETCH_MASKS
400 drive->no_unmask = 1;
401 drive->unmask = 0;
402#endif
403 drive->no_io_32bit = 0;
404 }
405}
406
407/*
408 * Figure out which devices we control
409 */
410static void __init setup_device_ptrs (void)
411{
Bartlomiej Zolnierkiewiczdaaad242008-04-18 00:46:34 +0200412 cmd_hwif0 = &ide_hwifs[0];
413 cmd_hwif1 = &ide_hwifs[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 cmd_drives[0] = &cmd_hwif0->drives[0];
416 cmd_drives[1] = &cmd_hwif0->drives[1];
417 cmd_drives[2] = &cmd_hwif1->drives[0];
418 cmd_drives[3] = &cmd_hwif1->drives[1];
419}
420
421#ifdef CONFIG_BLK_DEV_CMD640_ENHANCED
422
423/*
424 * Sets prefetch mode for a drive.
425 */
426static void set_prefetch_mode (unsigned int index, int mode)
427{
428 ide_drive_t *drive = cmd_drives[index];
Bartlomiej Zolnierkiewicz5bbcf922007-10-20 00:32:35 +0200429 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 int reg = prefetch_regs[index];
431 u8 b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
Bartlomiej Zolnierkiewicz5bbcf922007-10-20 00:32:35 +0200433 spin_lock_irqsave(&cmd640_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 b = __get_cmd640_reg(reg);
435 if (mode) { /* want prefetch on? */
436#if CMD640_PREFETCH_MASKS
437 drive->no_unmask = 1;
438 drive->unmask = 0;
439#endif
440 drive->no_io_32bit = 0;
441 b &= ~prefetch_masks[index]; /* enable prefetch */
442 } else {
443 drive->no_unmask = 0;
444 drive->no_io_32bit = 1;
445 drive->io_32bit = 0;
446 b |= prefetch_masks[index]; /* disable prefetch */
447 }
448 __put_cmd640_reg(reg, b);
Bartlomiej Zolnierkiewicz5bbcf922007-10-20 00:32:35 +0200449 spin_unlock_irqrestore(&cmd640_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450}
451
452/*
453 * Dump out current drive clocks settings
454 */
455static void display_clocks (unsigned int index)
456{
457 u8 active_count, recovery_count;
458
459 active_count = active_counts[index];
460 if (active_count == 1)
461 ++active_count;
462 recovery_count = recovery_counts[index];
463 if (active_count > 3 && recovery_count == 1)
464 ++recovery_count;
465 if (cmd640_chip_version > 1)
466 recovery_count += 1; /* cmd640b uses (count + 1)*/
467 printk(", clocks=%d/%d/%d\n", setup_counts[index], active_count, recovery_count);
468}
469
470/*
471 * Pack active and recovery counts into single byte representation
472 * used by controller
473 */
Jesper Juhl77933d72005-07-27 11:46:09 -0700474static inline u8 pack_nibbles (u8 upper, u8 lower)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{
476 return ((upper & 0x0f) << 4) | (lower & 0x0f);
477}
478
479/*
480 * This routine retrieves the initial drive timings from the chipset.
481 */
482static void __init retrieve_drive_counts (unsigned int index)
483{
484 u8 b;
485
486 /*
487 * Get the internal setup timing, and convert to clock count
488 */
489 b = get_cmd640_reg(arttim_regs[index]) & ~0x3f;
490 switch (b) {
491 case 0x00: b = 4; break;
492 case 0x80: b = 3; break;
493 case 0x40: b = 2; break;
494 default: b = 5; break;
495 }
496 setup_counts[index] = b;
497
498 /*
499 * Get the active/recovery counts
500 */
501 b = get_cmd640_reg(drwtim_regs[index]);
502 active_counts[index] = (b >> 4) ? (b >> 4) : 0x10;
503 recovery_counts[index] = (b & 0x0f) ? (b & 0x0f) : 0x10;
504}
505
506
507/*
508 * This routine writes the prepared setup/active/recovery counts
509 * for a drive into the cmd640 chipset registers to active them.
510 */
511static void program_drive_counts (unsigned int index)
512{
513 unsigned long flags;
514 u8 setup_count = setup_counts[index];
515 u8 active_count = active_counts[index];
516 u8 recovery_count = recovery_counts[index];
517
518 /*
519 * Set up address setup count and drive read/write timing registers.
520 * Primary interface has individual count/timing registers for
521 * each drive. Secondary interface has one common set of registers,
522 * so we merge the timings, using the slowest value for each timing.
523 */
524 if (index > 1) {
525 unsigned int mate;
526 if (cmd_drives[mate = index ^ 1]->present) {
527 if (setup_count < setup_counts[mate])
528 setup_count = setup_counts[mate];
529 if (active_count < active_counts[mate])
530 active_count = active_counts[mate];
531 if (recovery_count < recovery_counts[mate])
532 recovery_count = recovery_counts[mate];
533 }
534 }
535
536 /*
537 * Convert setup_count to internal chipset representation
538 */
539 switch (setup_count) {
540 case 4: setup_count = 0x00; break;
541 case 3: setup_count = 0x80; break;
542 case 1:
543 case 2: setup_count = 0x40; break;
544 default: setup_count = 0xc0; /* case 5 */
545 }
546
547 /*
548 * Now that everything is ready, program the new timings
549 */
Bartlomiej Zolnierkiewicz5bbcf922007-10-20 00:32:35 +0200550 spin_lock_irqsave(&cmd640_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 /*
552 * Program the address_setup clocks into ARTTIM reg,
553 * and then the active/recovery counts into the DRWTIM reg
554 * (this converts counts of 16 into counts of zero -- okay).
555 */
556 setup_count |= __get_cmd640_reg(arttim_regs[index]) & 0x3f;
557 __put_cmd640_reg(arttim_regs[index], setup_count);
558 __put_cmd640_reg(drwtim_regs[index], pack_nibbles(active_count, recovery_count));
Bartlomiej Zolnierkiewicz5bbcf922007-10-20 00:32:35 +0200559 spin_unlock_irqrestore(&cmd640_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560}
561
562/*
563 * Set a specific pio_mode for a drive
564 */
565static void cmd640_set_mode (unsigned int index, u8 pio_mode, unsigned int cycle_time)
566{
567 int setup_time, active_time, recovery_time, clock_time;
568 u8 setup_count, active_count, recovery_count, recovery_count2, cycle_count;
569 int bus_speed = system_bus_clock();
570
571 if (pio_mode > 5)
572 pio_mode = 5;
573 setup_time = ide_pio_timings[pio_mode].setup_time;
574 active_time = ide_pio_timings[pio_mode].active_time;
575 recovery_time = cycle_time - (setup_time + active_time);
576 clock_time = 1000 / bus_speed;
577 cycle_count = (cycle_time + clock_time - 1) / clock_time;
578
579 setup_count = (setup_time + clock_time - 1) / clock_time;
580
581 active_count = (active_time + clock_time - 1) / clock_time;
582 if (active_count < 2)
583 active_count = 2; /* minimum allowed by cmd640 */
584
585 recovery_count = (recovery_time + clock_time - 1) / clock_time;
586 recovery_count2 = cycle_count - (setup_count + active_count);
587 if (recovery_count2 > recovery_count)
588 recovery_count = recovery_count2;
589 if (recovery_count < 2)
590 recovery_count = 2; /* minimum allowed by cmd640 */
591 if (recovery_count > 17) {
592 active_count += recovery_count - 17;
593 recovery_count = 17;
594 }
595 if (active_count > 16)
596 active_count = 16; /* maximum allowed by cmd640 */
597 if (cmd640_chip_version > 1)
598 recovery_count -= 1; /* cmd640b uses (count + 1)*/
599 if (recovery_count > 16)
600 recovery_count = 16; /* maximum allowed by cmd640 */
601
602 setup_counts[index] = setup_count;
603 active_counts[index] = active_count;
604 recovery_counts[index] = recovery_count;
605
606 /*
607 * In a perfect world, we might set the drive pio mode here
608 * (using WIN_SETFEATURE) before continuing.
609 *
610 * But we do not, because:
611 * 1) this is the wrong place to do it (proper is do_special() in ide.c)
612 * 2) in practice this is rarely, if ever, necessary
613 */
614 program_drive_counts (index);
615}
616
Bartlomiej Zolnierkiewicz26bcb872007-10-11 23:54:00 +0200617static void cmd640_set_pio_mode(ide_drive_t *drive, const u8 pio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618{
Bartlomiej Zolnierkiewicz7dd00082007-07-20 01:11:56 +0200619 unsigned int index = 0, cycle_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 u8 b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
622 while (drive != cmd_drives[index]) {
623 if (++index > 3) {
Bartlomiej Zolnierkiewicz26bcb872007-10-11 23:54:00 +0200624 printk(KERN_ERR "%s: bad news in %s\n",
625 drive->name, __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 return;
627 }
628 }
Bartlomiej Zolnierkiewicz26bcb872007-10-11 23:54:00 +0200629 switch (pio) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 case 6: /* set fast-devsel off */
631 case 7: /* set fast-devsel on */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 b = get_cmd640_reg(CNTRL) & ~0x27;
Bartlomiej Zolnierkiewicz26bcb872007-10-11 23:54:00 +0200633 if (pio & 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 b |= 0x27;
635 put_cmd640_reg(CNTRL, b);
Bartlomiej Zolnierkiewicz26bcb872007-10-11 23:54:00 +0200636 printk("%s: %sabled cmd640 fast host timing (devsel)\n", drive->name, (pio & 1) ? "en" : "dis");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 return;
638
639 case 8: /* set prefetch off */
640 case 9: /* set prefetch on */
Bartlomiej Zolnierkiewicz26bcb872007-10-11 23:54:00 +0200641 set_prefetch_mode(index, pio & 1);
642 printk("%s: %sabled cmd640 prefetch\n", drive->name, (pio & 1) ? "en" : "dis");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 return;
644 }
645
Bartlomiej Zolnierkiewicz26bcb872007-10-11 23:54:00 +0200646 cycle_time = ide_pio_cycle_time(drive, pio);
647 cmd640_set_mode(index, pio, cycle_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Bartlomiej Zolnierkiewicz342cdb62007-07-20 01:11:55 +0200649 printk("%s: selected cmd640 PIO mode%d (%dns)",
Bartlomiej Zolnierkiewicz26bcb872007-10-11 23:54:00 +0200650 drive->name, pio, cycle_time);
Bartlomiej Zolnierkiewicz342cdb62007-07-20 01:11:55 +0200651
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 display_clocks(index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653}
654
655#endif /* CONFIG_BLK_DEV_CMD640_ENHANCED */
656
657static int pci_conf1(void)
658{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 unsigned long flags;
Bartlomiej Zolnierkiewicz5bbcf922007-10-20 00:32:35 +0200660 u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
Bartlomiej Zolnierkiewicz5bbcf922007-10-20 00:32:35 +0200662 spin_lock_irqsave(&cmd640_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 outb(0x01, 0xCFB);
664 tmp = inl(0xCF8);
665 outl(0x80000000, 0xCF8);
666 if (inl(0xCF8) == 0x80000000) {
667 outl(tmp, 0xCF8);
Bartlomiej Zolnierkiewicz5bbcf922007-10-20 00:32:35 +0200668 spin_unlock_irqrestore(&cmd640_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 return 1;
670 }
671 outl(tmp, 0xCF8);
Bartlomiej Zolnierkiewicz5bbcf922007-10-20 00:32:35 +0200672 spin_unlock_irqrestore(&cmd640_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 return 0;
674}
675
676static int pci_conf2(void)
677{
678 unsigned long flags;
679
Bartlomiej Zolnierkiewicz5bbcf922007-10-20 00:32:35 +0200680 spin_lock_irqsave(&cmd640_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 outb(0x00, 0xCFB);
682 outb(0x00, 0xCF8);
683 outb(0x00, 0xCFA);
684 if (inb(0xCF8) == 0x00 && inb(0xCF8) == 0x00) {
Bartlomiej Zolnierkiewicz5bbcf922007-10-20 00:32:35 +0200685 spin_unlock_irqrestore(&cmd640_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 return 1;
687 }
Bartlomiej Zolnierkiewicz5bbcf922007-10-20 00:32:35 +0200688 spin_unlock_irqrestore(&cmd640_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 return 0;
690}
691
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +0100692static const struct ide_port_info cmd640_port_info __initdata = {
693 .chipset = ide_cmd640,
694 .host_flags = IDE_HFLAG_SERIALIZE |
695 IDE_HFLAG_NO_DMA |
696 IDE_HFLAG_NO_AUTOTUNE |
697 IDE_HFLAG_ABUSE_PREFETCH |
698 IDE_HFLAG_ABUSE_FAST_DEVSEL,
699#ifdef CONFIG_BLK_DEV_CMD640_ENHANCED
700 .pio_mask = ATA_PIO5,
701#endif
702};
703
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704/*
Bartlomiej Zolnierkiewiczade2daf2008-01-26 20:13:07 +0100705 * Probe for a cmd640 chipset, and initialize it if found.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 */
Bartlomiej Zolnierkiewiczade2daf2008-01-26 20:13:07 +0100707static int __init cmd640x_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708{
709#ifdef CONFIG_BLK_DEV_CMD640_ENHANCED
710 int second_port_toggled = 0;
711#endif /* CONFIG_BLK_DEV_CMD640_ENHANCED */
712 int second_port_cmd640 = 0;
713 const char *bus_type, *port2;
714 unsigned int index;
715 u8 b, cfr;
Bartlomiej Zolnierkiewicz8ac4ce72008-01-26 20:13:06 +0100716 u8 idx[4] = { 0xff, 0xff, 0xff, 0xff };
Bartlomiej Zolnierkiewicz6d3803b2008-04-18 00:46:34 +0200717 hw_regs_t hw[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
719 if (cmd640_vlb && probe_for_cmd640_vlb()) {
720 bus_type = "VLB";
721 } else {
722 cmd640_vlb = 0;
723 /* Find out what kind of PCI probing is supported otherwise
724 Justin Gibbs will sulk.. */
725 if (pci_conf1() && probe_for_cmd640_pci1())
726 bus_type = "PCI (type1)";
727 else if (pci_conf2() && probe_for_cmd640_pci2())
728 bus_type = "PCI (type2)";
729 else
730 return 0;
731 }
732 /*
733 * Undocumented magic (there is no 0x5b reg in specs)
734 */
735 put_cmd640_reg(0x5b, 0xbd);
736 if (get_cmd640_reg(0x5b) != 0xbd) {
737 printk(KERN_ERR "ide: cmd640 init failed: wrong value in reg 0x5b\n");
738 return 0;
739 }
740 put_cmd640_reg(0x5b, 0);
741
742#ifdef CMD640_DUMP_REGS
743 cmd640_dump_regs();
744#endif
745
746 /*
747 * Documented magic begins here
748 */
749 cfr = get_cmd640_reg(CFR);
750 cmd640_chip_version = cfr & CFR_DEVREV;
751 if (cmd640_chip_version == 0) {
752 printk ("ide: bad cmd640 revision: %d\n", cmd640_chip_version);
753 return 0;
754 }
755
Bartlomiej Zolnierkiewicz6d3803b2008-04-18 00:46:34 +0200756 memset(&hw, 0, sizeof(hw));
757
758 ide_std_init_ports(&hw[0], 0x1f0, 0x3f6);
759 hw[0].irq = 14;
760
761 ide_std_init_ports(&hw[1], 0x170, 0x376);
762 hw[1].irq = 15;
763
764 printk(KERN_INFO "cmd640: buggy cmd640%c interface on %s, config=0x%02x"
765 "\n", 'a' + cmd640_chip_version - 1, bus_type, cfr);
766
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 /*
768 * Initialize data for primary port
769 */
770 setup_device_ptrs ();
Bartlomiej Zolnierkiewicz6d3803b2008-04-18 00:46:34 +0200771
772 ide_init_port_hw(cmd_hwif0, &hw[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773#ifdef CONFIG_BLK_DEV_CMD640_ENHANCED
Bartlomiej Zolnierkiewicz26bcb872007-10-11 23:54:00 +0200774 cmd_hwif0->set_pio_mode = &cmd640_set_pio_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775#endif /* CONFIG_BLK_DEV_CMD640_ENHANCED */
776
Bartlomiej Zolnierkiewicz8ac4ce72008-01-26 20:13:06 +0100777 idx[0] = cmd_hwif0->index;
778
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 /*
780 * Ensure compatibility by always using the slowest timings
781 * for access to the drive's command register block,
782 * and reset the prefetch burstsize to default (512 bytes).
783 *
784 * Maybe we need a way to NOT do these on *some* systems?
785 */
786 put_cmd640_reg(CMDTIM, 0);
787 put_cmd640_reg(BRST, 0x40);
788
789 /*
790 * Try to enable the secondary interface, if not already enabled
791 */
Bartlomiej Zolnierkiewiczc53ea182008-02-26 21:50:34 +0100792 if (cmd_hwif1->noprobe ||
793 (cmd_hwif1->drives[0].noprobe && cmd_hwif1->drives[1].noprobe)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 port2 = "not probed";
795 } else {
796 b = get_cmd640_reg(CNTRL);
797 if (secondary_port_responding()) {
798 if ((b & CNTRL_ENA_2ND)) {
799 second_port_cmd640 = 1;
800 port2 = "okay";
801 } else if (cmd640_vlb) {
802 second_port_cmd640 = 1;
803 port2 = "alive";
804 } else
805 port2 = "not cmd640";
806 } else {
807 put_cmd640_reg(CNTRL, b ^ CNTRL_ENA_2ND); /* toggle the bit */
808 if (secondary_port_responding()) {
809 second_port_cmd640 = 1;
810#ifdef CONFIG_BLK_DEV_CMD640_ENHANCED
811 second_port_toggled = 1;
812#endif /* CONFIG_BLK_DEV_CMD640_ENHANCED */
813 port2 = "enabled";
814 } else {
815 put_cmd640_reg(CNTRL, b); /* restore original setting */
816 port2 = "not responding";
817 }
818 }
819 }
820
821 /*
822 * Initialize data for secondary cmd640 port, if enabled
823 */
824 if (second_port_cmd640) {
Bartlomiej Zolnierkiewicz6d3803b2008-04-18 00:46:34 +0200825 ide_init_port_hw(cmd_hwif1, &hw[1]);
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);
Adrian Bunk776c0bc2008-04-02 21:22:02 +0200887
888MODULE_LICENSE("GPL");