blob: 0f6f11e698073e302c9708ee332de5ab1ca9e4b1 [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)
Paolo Ciarrocchi4752b5e2008-04-26 17:36:41 +02007 * mlord@pobox.com (Mark Lord)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
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
Paolo Ciarrocchi4752b5e2008-04-26 17:36:41 +0200101/*#define CMD640_DUMP_REGS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
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
Bartlomiej Zolnierkiewicz7ebe5932008-04-26 22:25:19 +0200112#define DRV_NAME "cmd640"
113
Bartlomiej Zolnierkiewiczef87f8d2008-04-27 15:38:24 +0200114static int cmd640_vlb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116/*
117 * CMD640 specific registers definition.
118 */
119
120#define VID 0x00
121#define DID 0x02
122#define PCMD 0x04
123#define PCMD_ENA 0x01
124#define PSTTS 0x06
125#define REVID 0x08
126#define PROGIF 0x09
127#define SUBCL 0x0a
128#define BASCL 0x0b
129#define BaseA0 0x10
130#define BaseA1 0x14
131#define BaseA2 0x18
132#define BaseA3 0x1c
133#define INTLINE 0x3c
134#define INPINE 0x3d
135
136#define CFR 0x50
137#define CFR_DEVREV 0x03
138#define CFR_IDE01INTR 0x04
139#define CFR_DEVID 0x18
140#define CFR_AT_VESA_078h 0x20
141#define CFR_DSA1 0x40
142#define CFR_DSA0 0x80
143
144#define CNTRL 0x51
145#define CNTRL_DIS_RA0 0x40
146#define CNTRL_DIS_RA1 0x80
147#define CNTRL_ENA_2ND 0x08
148
149#define CMDTIM 0x52
150#define ARTTIM0 0x53
151#define DRWTIM0 0x54
152#define ARTTIM1 0x55
153#define DRWTIM1 0x56
154#define ARTTIM23 0x57
155#define ARTTIM23_DIS_RA2 0x04
156#define ARTTIM23_DIS_RA3 0x08
157#define DRWTIM23 0x58
158#define BRST 0x59
159
160/*
161 * Registers and masks for easy access by drive index:
162 */
163static u8 prefetch_regs[4] = {CNTRL, CNTRL, ARTTIM23, ARTTIM23};
164static u8 prefetch_masks[4] = {CNTRL_DIS_RA0, CNTRL_DIS_RA1, ARTTIM23_DIS_RA2, ARTTIM23_DIS_RA3};
165
166#ifdef CONFIG_BLK_DEV_CMD640_ENHANCED
167
168static u8 arttim_regs[4] = {ARTTIM0, ARTTIM1, ARTTIM23, ARTTIM23};
169static u8 drwtim_regs[4] = {DRWTIM0, DRWTIM1, DRWTIM23, DRWTIM23};
170
171/*
172 * Current cmd640 timing values for each drive.
173 * The defaults for each are the slowest possible timings.
174 */
175static u8 setup_counts[4] = {4, 4, 4, 4}; /* Address setup count (in clocks) */
176static u8 active_counts[4] = {16, 16, 16, 16}; /* Active count (encoded) */
177static u8 recovery_counts[4] = {16, 16, 16, 16}; /* Recovery count (encoded) */
178
179#endif /* CONFIG_BLK_DEV_CMD640_ENHANCED */
180
Bartlomiej Zolnierkiewicz5bbcf922007-10-20 00:32:35 +0200181static DEFINE_SPINLOCK(cmd640_lock);
182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183/*
184 * These are initialized to point at the devices we control
185 */
186static ide_hwif_t *cmd_hwif0, *cmd_hwif1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
188/*
189 * Interface to access cmd640x registers
190 */
191static unsigned int cmd640_key;
192static void (*__put_cmd640_reg)(u16 reg, u8 val);
193static u8 (*__get_cmd640_reg)(u16 reg);
194
195/*
196 * This is read from the CFR reg, and is used in several places.
197 */
198static unsigned int cmd640_chip_version;
199
200/*
201 * The CMD640x chip does not support DWORD config write cycles, but some
202 * of the BIOSes use them to implement the config services.
203 * Therefore, we must use direct IO instead.
204 */
205
206/* PCI method 1 access */
207
Paolo Ciarrocchi4752b5e2008-04-26 17:36:41 +0200208static void put_cmd640_reg_pci1(u16 reg, u8 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209{
210 outl_p((reg & 0xfc) | cmd640_key, 0xcf8);
211 outb_p(val, (reg & 3) | 0xcfc);
212}
213
Paolo Ciarrocchi4752b5e2008-04-26 17:36:41 +0200214static u8 get_cmd640_reg_pci1(u16 reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215{
216 outl_p((reg & 0xfc) | cmd640_key, 0xcf8);
217 return inb_p((reg & 3) | 0xcfc);
218}
219
220/* PCI method 2 access (from CMD datasheet) */
221
Paolo Ciarrocchi4752b5e2008-04-26 17:36:41 +0200222static void put_cmd640_reg_pci2(u16 reg, u8 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223{
224 outb_p(0x10, 0xcf8);
225 outb_p(val, cmd640_key + reg);
226 outb_p(0, 0xcf8);
227}
228
Paolo Ciarrocchi4752b5e2008-04-26 17:36:41 +0200229static u8 get_cmd640_reg_pci2(u16 reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230{
231 u8 b;
232
233 outb_p(0x10, 0xcf8);
234 b = inb_p(cmd640_key + reg);
235 outb_p(0, 0xcf8);
236 return b;
237}
238
239/* VLB access */
240
Paolo Ciarrocchi4752b5e2008-04-26 17:36:41 +0200241static void put_cmd640_reg_vlb(u16 reg, u8 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242{
243 outb_p(reg, cmd640_key);
244 outb_p(val, cmd640_key + 4);
245}
246
Paolo Ciarrocchi4752b5e2008-04-26 17:36:41 +0200247static u8 get_cmd640_reg_vlb(u16 reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248{
249 outb_p(reg, cmd640_key);
250 return inb_p(cmd640_key + 4);
251}
252
253static u8 get_cmd640_reg(u16 reg)
254{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 unsigned long flags;
Bartlomiej Zolnierkiewicz5bbcf922007-10-20 00:32:35 +0200256 u8 b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Bartlomiej Zolnierkiewicz5bbcf922007-10-20 00:32:35 +0200258 spin_lock_irqsave(&cmd640_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 b = __get_cmd640_reg(reg);
Bartlomiej Zolnierkiewicz5bbcf922007-10-20 00:32:35 +0200260 spin_unlock_irqrestore(&cmd640_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 return b;
262}
263
264static void put_cmd640_reg(u16 reg, u8 val)
265{
266 unsigned long flags;
267
Bartlomiej Zolnierkiewicz5bbcf922007-10-20 00:32:35 +0200268 spin_lock_irqsave(&cmd640_lock, flags);
Paolo Ciarrocchi4752b5e2008-04-26 17:36:41 +0200269 __put_cmd640_reg(reg, val);
Bartlomiej Zolnierkiewicz5bbcf922007-10-20 00:32:35 +0200270 spin_unlock_irqrestore(&cmd640_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271}
272
Paolo Ciarrocchi4752b5e2008-04-26 17:36:41 +0200273static int __init match_pci_cmd640_device(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274{
275 const u8 ven_dev[4] = {0x95, 0x10, 0x40, 0x06};
276 unsigned int i;
277 for (i = 0; i < 4; i++) {
278 if (get_cmd640_reg(i) != ven_dev[i])
279 return 0;
280 }
281#ifdef STUPIDLY_TRUST_BROKEN_PCMD_ENA_BIT
282 if ((get_cmd640_reg(PCMD) & PCMD_ENA) == 0) {
283 printk("ide: cmd640 on PCI disabled by BIOS\n");
284 return 0;
285 }
286#endif /* STUPIDLY_TRUST_BROKEN_PCMD_ENA_BIT */
287 return 1; /* success */
288}
289
290/*
291 * Probe for CMD640x -- pci method 1
292 */
Paolo Ciarrocchi4752b5e2008-04-26 17:36:41 +0200293static int __init probe_for_cmd640_pci1(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294{
295 __get_cmd640_reg = get_cmd640_reg_pci1;
296 __put_cmd640_reg = put_cmd640_reg_pci1;
297 for (cmd640_key = 0x80000000;
298 cmd640_key <= 0x8000f800;
299 cmd640_key += 0x800) {
300 if (match_pci_cmd640_device())
301 return 1; /* success */
302 }
303 return 0;
304}
305
306/*
307 * Probe for CMD640x -- pci method 2
308 */
Paolo Ciarrocchi4752b5e2008-04-26 17:36:41 +0200309static int __init probe_for_cmd640_pci2(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
311 __get_cmd640_reg = get_cmd640_reg_pci2;
312 __put_cmd640_reg = put_cmd640_reg_pci2;
313 for (cmd640_key = 0xc000; cmd640_key <= 0xcf00; cmd640_key += 0x100) {
314 if (match_pci_cmd640_device())
315 return 1; /* success */
316 }
317 return 0;
318}
319
320/*
321 * Probe for CMD640x -- vlb
322 */
Paolo Ciarrocchi4752b5e2008-04-26 17:36:41 +0200323static int __init probe_for_cmd640_vlb(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324{
325 u8 b;
326
327 __get_cmd640_reg = get_cmd640_reg_vlb;
328 __put_cmd640_reg = put_cmd640_reg_vlb;
329 cmd640_key = 0x178;
330 b = get_cmd640_reg(CFR);
331 if (b == 0xff || b == 0x00 || (b & CFR_AT_VESA_078h)) {
332 cmd640_key = 0x78;
333 b = get_cmd640_reg(CFR);
334 if (b == 0xff || b == 0x00 || !(b & CFR_AT_VESA_078h))
335 return 0;
336 }
337 return 1; /* success */
338}
339
340/*
341 * Returns 1 if an IDE interface/drive exists at 0x170,
342 * Returns 0 otherwise.
343 */
Paolo Ciarrocchi4752b5e2008-04-26 17:36:41 +0200344static int __init secondary_port_responding(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
346 unsigned long flags;
347
Bartlomiej Zolnierkiewicz5bbcf922007-10-20 00:32:35 +0200348 spin_lock_irqsave(&cmd640_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
350 outb_p(0x0a, 0x170 + IDE_SELECT_OFFSET); /* select drive0 */
351 udelay(100);
352 if ((inb_p(0x170 + IDE_SELECT_OFFSET) & 0x1f) != 0x0a) {
353 outb_p(0x1a, 0x170 + IDE_SELECT_OFFSET); /* select drive1 */
354 udelay(100);
355 if ((inb_p(0x170 + IDE_SELECT_OFFSET) & 0x1f) != 0x1a) {
Bartlomiej Zolnierkiewicz5bbcf922007-10-20 00:32:35 +0200356 spin_unlock_irqrestore(&cmd640_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 return 0; /* nothing responded */
358 }
359 }
Bartlomiej Zolnierkiewicz5bbcf922007-10-20 00:32:35 +0200360 spin_unlock_irqrestore(&cmd640_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 return 1; /* success */
362}
363
364#ifdef CMD640_DUMP_REGS
365/*
366 * Dump out all cmd640 registers. May be called from ide.c
367 */
Paolo Ciarrocchi4752b5e2008-04-26 17:36:41 +0200368static void cmd640_dump_regs(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369{
370 unsigned int reg = cmd640_vlb ? 0x50 : 0x00;
371
372 /* Dump current state of chip registers */
373 printk("ide: cmd640 internal register dump:");
374 for (; reg <= 0x59; reg++) {
375 if (!(reg & 0x0f))
376 printk("\n%04x:", reg);
377 printk(" %02x", get_cmd640_reg(reg));
378 }
379 printk("\n");
380}
381#endif
382
383/*
384 * Check whether prefetch is on for a drive,
385 * and initialize the unmask flags for safe operation.
386 */
Bartlomiej Zolnierkiewicz95230762008-04-26 17:36:33 +0200387static void __init check_prefetch(ide_drive_t *drive, unsigned int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 u8 b = get_cmd640_reg(prefetch_regs[index]);
390
391 if (b & prefetch_masks[index]) { /* is prefetch off? */
392 drive->no_unmask = 0;
393 drive->no_io_32bit = 1;
394 drive->io_32bit = 0;
395 } else {
396#if CMD640_PREFETCH_MASKS
397 drive->no_unmask = 1;
398 drive->unmask = 0;
399#endif
400 drive->no_io_32bit = 0;
401 }
402}
403
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404#ifdef CONFIG_BLK_DEV_CMD640_ENHANCED
405
406/*
407 * Sets prefetch mode for a drive.
408 */
Bartlomiej Zolnierkiewicz95230762008-04-26 17:36:33 +0200409static void set_prefetch_mode(ide_drive_t *drive, unsigned int index, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410{
Bartlomiej Zolnierkiewicz5bbcf922007-10-20 00:32:35 +0200411 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 int reg = prefetch_regs[index];
413 u8 b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
Bartlomiej Zolnierkiewicz5bbcf922007-10-20 00:32:35 +0200415 spin_lock_irqsave(&cmd640_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 b = __get_cmd640_reg(reg);
417 if (mode) { /* want prefetch on? */
418#if CMD640_PREFETCH_MASKS
419 drive->no_unmask = 1;
420 drive->unmask = 0;
421#endif
422 drive->no_io_32bit = 0;
423 b &= ~prefetch_masks[index]; /* enable prefetch */
424 } else {
425 drive->no_unmask = 0;
426 drive->no_io_32bit = 1;
427 drive->io_32bit = 0;
428 b |= prefetch_masks[index]; /* disable prefetch */
429 }
430 __put_cmd640_reg(reg, b);
Bartlomiej Zolnierkiewicz5bbcf922007-10-20 00:32:35 +0200431 spin_unlock_irqrestore(&cmd640_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432}
433
434/*
435 * Dump out current drive clocks settings
436 */
Paolo Ciarrocchi4752b5e2008-04-26 17:36:41 +0200437static void display_clocks(unsigned int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
439 u8 active_count, recovery_count;
440
441 active_count = active_counts[index];
442 if (active_count == 1)
443 ++active_count;
444 recovery_count = recovery_counts[index];
445 if (active_count > 3 && recovery_count == 1)
446 ++recovery_count;
447 if (cmd640_chip_version > 1)
448 recovery_count += 1; /* cmd640b uses (count + 1)*/
449 printk(", clocks=%d/%d/%d\n", setup_counts[index], active_count, recovery_count);
450}
451
452/*
453 * Pack active and recovery counts into single byte representation
454 * used by controller
455 */
Paolo Ciarrocchi4752b5e2008-04-26 17:36:41 +0200456static inline u8 pack_nibbles(u8 upper, u8 lower)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457{
458 return ((upper & 0x0f) << 4) | (lower & 0x0f);
459}
460
461/*
462 * This routine retrieves the initial drive timings from the chipset.
463 */
Paolo Ciarrocchi4752b5e2008-04-26 17:36:41 +0200464static void __init retrieve_drive_counts(unsigned int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465{
466 u8 b;
467
468 /*
469 * Get the internal setup timing, and convert to clock count
470 */
471 b = get_cmd640_reg(arttim_regs[index]) & ~0x3f;
472 switch (b) {
Paolo Ciarrocchi4752b5e2008-04-26 17:36:41 +0200473 case 0x00: b = 4; break;
474 case 0x80: b = 3; break;
475 case 0x40: b = 2; break;
476 default: b = 5; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 }
478 setup_counts[index] = b;
479
480 /*
481 * Get the active/recovery counts
482 */
483 b = get_cmd640_reg(drwtim_regs[index]);
484 active_counts[index] = (b >> 4) ? (b >> 4) : 0x10;
485 recovery_counts[index] = (b & 0x0f) ? (b & 0x0f) : 0x10;
486}
487
488
489/*
490 * This routine writes the prepared setup/active/recovery counts
491 * for a drive into the cmd640 chipset registers to active them.
492 */
Bartlomiej Zolnierkiewicz95230762008-04-26 17:36:33 +0200493static void program_drive_counts(ide_drive_t *drive, unsigned int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
495 unsigned long flags;
496 u8 setup_count = setup_counts[index];
497 u8 active_count = active_counts[index];
498 u8 recovery_count = recovery_counts[index];
499
500 /*
501 * Set up address setup count and drive read/write timing registers.
502 * Primary interface has individual count/timing registers for
503 * each drive. Secondary interface has one common set of registers,
504 * so we merge the timings, using the slowest value for each timing.
505 */
506 if (index > 1) {
Bartlomiej Zolnierkiewicz95230762008-04-26 17:36:33 +0200507 ide_hwif_t *hwif = drive->hwif;
508 ide_drive_t *peer = &hwif->drives[!drive->select.b.unit];
509 unsigned int mate = index ^ 1;
510
511 if (peer->present) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 if (setup_count < setup_counts[mate])
513 setup_count = setup_counts[mate];
514 if (active_count < active_counts[mate])
515 active_count = active_counts[mate];
516 if (recovery_count < recovery_counts[mate])
517 recovery_count = recovery_counts[mate];
518 }
519 }
520
521 /*
522 * Convert setup_count to internal chipset representation
523 */
524 switch (setup_count) {
Paolo Ciarrocchi4752b5e2008-04-26 17:36:41 +0200525 case 4: setup_count = 0x00; break;
526 case 3: setup_count = 0x80; break;
527 case 1:
528 case 2: setup_count = 0x40; break;
529 default: setup_count = 0xc0; /* case 5 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 }
531
532 /*
533 * Now that everything is ready, program the new timings
534 */
Bartlomiej Zolnierkiewicz5bbcf922007-10-20 00:32:35 +0200535 spin_lock_irqsave(&cmd640_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 /*
537 * Program the address_setup clocks into ARTTIM reg,
538 * and then the active/recovery counts into the DRWTIM reg
539 * (this converts counts of 16 into counts of zero -- okay).
540 */
541 setup_count |= __get_cmd640_reg(arttim_regs[index]) & 0x3f;
542 __put_cmd640_reg(arttim_regs[index], setup_count);
543 __put_cmd640_reg(drwtim_regs[index], pack_nibbles(active_count, recovery_count));
Bartlomiej Zolnierkiewicz5bbcf922007-10-20 00:32:35 +0200544 spin_unlock_irqrestore(&cmd640_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545}
546
547/*
548 * Set a specific pio_mode for a drive
549 */
Bartlomiej Zolnierkiewicz95230762008-04-26 17:36:33 +0200550static void cmd640_set_mode(ide_drive_t *drive, unsigned int index,
551 u8 pio_mode, unsigned int cycle_time)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552{
553 int setup_time, active_time, recovery_time, clock_time;
554 u8 setup_count, active_count, recovery_count, recovery_count2, cycle_count;
555 int bus_speed = system_bus_clock();
556
557 if (pio_mode > 5)
558 pio_mode = 5;
559 setup_time = ide_pio_timings[pio_mode].setup_time;
560 active_time = ide_pio_timings[pio_mode].active_time;
561 recovery_time = cycle_time - (setup_time + active_time);
562 clock_time = 1000 / bus_speed;
Julia Lawall00fe8b72008-04-26 17:36:35 +0200563 cycle_count = DIV_ROUND_UP(cycle_time, clock_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
Julia Lawall00fe8b72008-04-26 17:36:35 +0200565 setup_count = DIV_ROUND_UP(setup_time, clock_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
Julia Lawall00fe8b72008-04-26 17:36:35 +0200567 active_count = DIV_ROUND_UP(active_time, clock_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 if (active_count < 2)
569 active_count = 2; /* minimum allowed by cmd640 */
570
Julia Lawall00fe8b72008-04-26 17:36:35 +0200571 recovery_count = DIV_ROUND_UP(recovery_time, clock_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 recovery_count2 = cycle_count - (setup_count + active_count);
573 if (recovery_count2 > recovery_count)
574 recovery_count = recovery_count2;
575 if (recovery_count < 2)
576 recovery_count = 2; /* minimum allowed by cmd640 */
577 if (recovery_count > 17) {
578 active_count += recovery_count - 17;
579 recovery_count = 17;
580 }
581 if (active_count > 16)
582 active_count = 16; /* maximum allowed by cmd640 */
583 if (cmd640_chip_version > 1)
584 recovery_count -= 1; /* cmd640b uses (count + 1)*/
585 if (recovery_count > 16)
586 recovery_count = 16; /* maximum allowed by cmd640 */
587
588 setup_counts[index] = setup_count;
589 active_counts[index] = active_count;
590 recovery_counts[index] = recovery_count;
591
592 /*
593 * In a perfect world, we might set the drive pio mode here
594 * (using WIN_SETFEATURE) before continuing.
595 *
596 * But we do not, because:
597 * 1) this is the wrong place to do it (proper is do_special() in ide.c)
598 * 2) in practice this is rarely, if ever, necessary
599 */
Bartlomiej Zolnierkiewicz95230762008-04-26 17:36:33 +0200600 program_drive_counts(drive, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601}
602
Bartlomiej Zolnierkiewicz26bcb872007-10-11 23:54:00 +0200603static void cmd640_set_pio_mode(ide_drive_t *drive, const u8 pio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604{
Bartlomiej Zolnierkiewicz7dd00082007-07-20 01:11:56 +0200605 unsigned int index = 0, cycle_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 u8 b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Bartlomiej Zolnierkiewicz26bcb872007-10-11 23:54:00 +0200608 switch (pio) {
Paolo Ciarrocchi4752b5e2008-04-26 17:36:41 +0200609 case 6: /* set fast-devsel off */
610 case 7: /* set fast-devsel on */
611 b = get_cmd640_reg(CNTRL) & ~0x27;
612 if (pio & 1)
613 b |= 0x27;
614 put_cmd640_reg(CNTRL, b);
615 printk("%s: %sabled cmd640 fast host timing (devsel)\n",
616 drive->name, (pio & 1) ? "en" : "dis");
617 return;
618 case 8: /* set prefetch off */
619 case 9: /* set prefetch on */
620 set_prefetch_mode(drive, index, pio & 1);
621 printk("%s: %sabled cmd640 prefetch\n",
622 drive->name, (pio & 1) ? "en" : "dis");
623 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 }
625
Bartlomiej Zolnierkiewicz26bcb872007-10-11 23:54:00 +0200626 cycle_time = ide_pio_cycle_time(drive, pio);
Bartlomiej Zolnierkiewicz95230762008-04-26 17:36:33 +0200627 cmd640_set_mode(drive, index, pio, cycle_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
Bartlomiej Zolnierkiewicz342cdb62007-07-20 01:11:55 +0200629 printk("%s: selected cmd640 PIO mode%d (%dns)",
Bartlomiej Zolnierkiewicz26bcb872007-10-11 23:54:00 +0200630 drive->name, pio, cycle_time);
Bartlomiej Zolnierkiewicz342cdb62007-07-20 01:11:55 +0200631
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 display_clocks(index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633}
634
Bartlomiej Zolnierkiewiczac95bee2008-04-26 22:25:14 +0200635static const struct ide_port_ops cmd640_port_ops = {
636 .set_pio_mode = cmd640_set_pio_mode,
637};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638#endif /* CONFIG_BLK_DEV_CMD640_ENHANCED */
639
640static int pci_conf1(void)
641{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 unsigned long flags;
Bartlomiej Zolnierkiewicz5bbcf922007-10-20 00:32:35 +0200643 u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Bartlomiej Zolnierkiewicz5bbcf922007-10-20 00:32:35 +0200645 spin_lock_irqsave(&cmd640_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 outb(0x01, 0xCFB);
647 tmp = inl(0xCF8);
648 outl(0x80000000, 0xCF8);
649 if (inl(0xCF8) == 0x80000000) {
650 outl(tmp, 0xCF8);
Bartlomiej Zolnierkiewicz5bbcf922007-10-20 00:32:35 +0200651 spin_unlock_irqrestore(&cmd640_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 return 1;
653 }
654 outl(tmp, 0xCF8);
Bartlomiej Zolnierkiewicz5bbcf922007-10-20 00:32:35 +0200655 spin_unlock_irqrestore(&cmd640_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 return 0;
657}
658
659static int pci_conf2(void)
660{
661 unsigned long flags;
662
Bartlomiej Zolnierkiewicz5bbcf922007-10-20 00:32:35 +0200663 spin_lock_irqsave(&cmd640_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 outb(0x00, 0xCFB);
665 outb(0x00, 0xCF8);
666 outb(0x00, 0xCFA);
667 if (inb(0xCF8) == 0x00 && inb(0xCF8) == 0x00) {
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 }
Bartlomiej Zolnierkiewicz5bbcf922007-10-20 00:32:35 +0200671 spin_unlock_irqrestore(&cmd640_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 return 0;
673}
674
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +0100675static const struct ide_port_info cmd640_port_info __initdata = {
676 .chipset = ide_cmd640,
677 .host_flags = IDE_HFLAG_SERIALIZE |
678 IDE_HFLAG_NO_DMA |
679 IDE_HFLAG_NO_AUTOTUNE |
680 IDE_HFLAG_ABUSE_PREFETCH |
681 IDE_HFLAG_ABUSE_FAST_DEVSEL,
682#ifdef CONFIG_BLK_DEV_CMD640_ENHANCED
Bartlomiej Zolnierkiewiczac95bee2008-04-26 22:25:14 +0200683 .port_ops = &cmd640_port_ops,
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +0100684 .pio_mask = ATA_PIO5,
685#endif
686};
687
Bartlomiej Zolnierkiewicz7ebe5932008-04-26 22:25:19 +0200688static int cmd640x_init_one(unsigned long base, unsigned long ctl)
689{
690 if (!request_region(base, 8, DRV_NAME)) {
691 printk(KERN_ERR "%s: I/O resource 0x%lX-0x%lX not free.\n",
692 DRV_NAME, base, base + 7);
693 return -EBUSY;
694 }
695
696 if (!request_region(ctl, 1, DRV_NAME)) {
697 printk(KERN_ERR "%s: I/O resource 0x%lX not free.\n",
698 DRV_NAME, ctl);
699 release_region(base, 8);
700 return -EBUSY;
701 }
702
703 return 0;
704}
705
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706/*
Bartlomiej Zolnierkiewiczade2daf2008-01-26 20:13:07 +0100707 * Probe for a cmd640 chipset, and initialize it if found.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 */
Bartlomiej Zolnierkiewiczade2daf2008-01-26 20:13:07 +0100709static int __init cmd640x_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710{
711#ifdef CONFIG_BLK_DEV_CMD640_ENHANCED
712 int second_port_toggled = 0;
713#endif /* CONFIG_BLK_DEV_CMD640_ENHANCED */
Bartlomiej Zolnierkiewicz7ebe5932008-04-26 22:25:19 +0200714 int second_port_cmd640 = 0, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 const char *bus_type, *port2;
716 unsigned int index;
717 u8 b, cfr;
Bartlomiej Zolnierkiewicz8ac4ce72008-01-26 20:13:06 +0100718 u8 idx[4] = { 0xff, 0xff, 0xff, 0xff };
Bartlomiej Zolnierkiewicz6d3803b2008-04-18 00:46:34 +0200719 hw_regs_t hw[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
721 if (cmd640_vlb && probe_for_cmd640_vlb()) {
722 bus_type = "VLB";
723 } else {
724 cmd640_vlb = 0;
725 /* Find out what kind of PCI probing is supported otherwise
726 Justin Gibbs will sulk.. */
727 if (pci_conf1() && probe_for_cmd640_pci1())
728 bus_type = "PCI (type1)";
729 else if (pci_conf2() && probe_for_cmd640_pci2())
730 bus_type = "PCI (type2)";
731 else
732 return 0;
733 }
734 /*
735 * Undocumented magic (there is no 0x5b reg in specs)
736 */
737 put_cmd640_reg(0x5b, 0xbd);
738 if (get_cmd640_reg(0x5b) != 0xbd) {
739 printk(KERN_ERR "ide: cmd640 init failed: wrong value in reg 0x5b\n");
740 return 0;
741 }
742 put_cmd640_reg(0x5b, 0);
743
744#ifdef CMD640_DUMP_REGS
745 cmd640_dump_regs();
746#endif
747
748 /*
749 * Documented magic begins here
750 */
751 cfr = get_cmd640_reg(CFR);
752 cmd640_chip_version = cfr & CFR_DEVREV;
753 if (cmd640_chip_version == 0) {
Paolo Ciarrocchi4752b5e2008-04-26 17:36:41 +0200754 printk("ide: bad cmd640 revision: %d\n", cmd640_chip_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 return 0;
756 }
757
Bartlomiej Zolnierkiewicz7ebe5932008-04-26 22:25:19 +0200758 rc = cmd640x_init_one(0x1f0, 0x3f6);
759 if (rc)
760 return rc;
761
762 rc = cmd640x_init_one(0x170, 0x376);
763 if (rc) {
764 release_region(0x3f6, 1);
765 release_region(0x1f0, 8);
766 return rc;
767 }
768
Bartlomiej Zolnierkiewicz6d3803b2008-04-18 00:46:34 +0200769 memset(&hw, 0, sizeof(hw));
770
771 ide_std_init_ports(&hw[0], 0x1f0, 0x3f6);
772 hw[0].irq = 14;
773
774 ide_std_init_ports(&hw[1], 0x170, 0x376);
775 hw[1].irq = 15;
776
777 printk(KERN_INFO "cmd640: buggy cmd640%c interface on %s, config=0x%02x"
778 "\n", 'a' + cmd640_chip_version - 1, bus_type, cfr);
779
Bartlomiej Zolnierkiewicz84f05df2008-04-26 17:36:33 +0200780 cmd_hwif0 = ide_find_port();
Bartlomiej Zolnierkiewicz95230762008-04-26 17:36:33 +0200781
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 /*
783 * Initialize data for primary port
784 */
Bartlomiej Zolnierkiewicz84f05df2008-04-26 17:36:33 +0200785 if (cmd_hwif0) {
786 ide_init_port_hw(cmd_hwif0, &hw[0]);
Bartlomiej Zolnierkiewicz84f05df2008-04-26 17:36:33 +0200787 idx[0] = cmd_hwif0->index;
788 }
Bartlomiej Zolnierkiewicz8ac4ce72008-01-26 20:13:06 +0100789
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 /*
791 * Ensure compatibility by always using the slowest timings
792 * for access to the drive's command register block,
793 * and reset the prefetch burstsize to default (512 bytes).
794 *
795 * Maybe we need a way to NOT do these on *some* systems?
796 */
797 put_cmd640_reg(CMDTIM, 0);
798 put_cmd640_reg(BRST, 0x40);
799
Bartlomiej Zolnierkiewicz84f05df2008-04-26 17:36:33 +0200800 cmd_hwif1 = ide_find_port();
801
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 /*
803 * Try to enable the secondary interface, if not already enabled
804 */
Bartlomiej Zolnierkiewicz84f05df2008-04-26 17:36:33 +0200805 if (cmd_hwif1 &&
806 cmd_hwif1->drives[0].noprobe && cmd_hwif1->drives[1].noprobe) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 port2 = "not probed";
808 } else {
809 b = get_cmd640_reg(CNTRL);
810 if (secondary_port_responding()) {
811 if ((b & CNTRL_ENA_2ND)) {
812 second_port_cmd640 = 1;
813 port2 = "okay";
814 } else if (cmd640_vlb) {
815 second_port_cmd640 = 1;
816 port2 = "alive";
817 } else
818 port2 = "not cmd640";
819 } else {
820 put_cmd640_reg(CNTRL, b ^ CNTRL_ENA_2ND); /* toggle the bit */
821 if (secondary_port_responding()) {
822 second_port_cmd640 = 1;
823#ifdef CONFIG_BLK_DEV_CMD640_ENHANCED
824 second_port_toggled = 1;
825#endif /* CONFIG_BLK_DEV_CMD640_ENHANCED */
826 port2 = "enabled";
827 } else {
828 put_cmd640_reg(CNTRL, b); /* restore original setting */
829 port2 = "not responding";
830 }
831 }
832 }
833
834 /*
835 * Initialize data for secondary cmd640 port, if enabled
836 */
Bartlomiej Zolnierkiewicz84f05df2008-04-26 17:36:33 +0200837 if (second_port_cmd640 && cmd_hwif1) {
Bartlomiej Zolnierkiewicz6d3803b2008-04-18 00:46:34 +0200838 ide_init_port_hw(cmd_hwif1, &hw[1]);
Bartlomiej Zolnierkiewicz8ac4ce72008-01-26 20:13:06 +0100839 idx[1] = cmd_hwif1->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 }
Bartlomiej Zolnierkiewicz84f05df2008-04-26 17:36:33 +0200841 printk(KERN_INFO "cmd640: %sserialized, secondary interface %s\n",
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +0100842 second_port_cmd640 ? "" : "not ", port2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
844 /*
845 * Establish initial timings/prefetch for all drives.
846 * Do not unnecessarily disturb any prior BIOS setup of these.
847 */
848 for (index = 0; index < (2 + (second_port_cmd640 << 1)); index++) {
Bartlomiej Zolnierkiewicz95230762008-04-26 17:36:33 +0200849 ide_drive_t *drive;
850
Bartlomiej Zolnierkiewicz84f05df2008-04-26 17:36:33 +0200851 if (index > 1) {
852 if (cmd_hwif1 == NULL)
853 continue;
Bartlomiej Zolnierkiewicz95230762008-04-26 17:36:33 +0200854 drive = &cmd_hwif1->drives[index & 1];
Bartlomiej Zolnierkiewicz84f05df2008-04-26 17:36:33 +0200855 } else {
856 if (cmd_hwif0 == NULL)
857 continue;
Bartlomiej Zolnierkiewicz95230762008-04-26 17:36:33 +0200858 drive = &cmd_hwif0->drives[index & 1];
Bartlomiej Zolnierkiewicz84f05df2008-04-26 17:36:33 +0200859 }
Bartlomiej Zolnierkiewicz95230762008-04-26 17:36:33 +0200860
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861#ifdef CONFIG_BLK_DEV_CMD640_ENHANCED
862 if (drive->autotune || ((index > 1) && second_port_toggled)) {
Paolo Ciarrocchi4752b5e2008-04-26 17:36:41 +0200863 /*
864 * Reset timing to the slowest speed and turn off
865 * prefetch. This way, the drive identify code has
866 * a better chance.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 */
868 setup_counts [index] = 4; /* max possible */
869 active_counts [index] = 16; /* max possible */
870 recovery_counts [index] = 16; /* max possible */
Bartlomiej Zolnierkiewicz95230762008-04-26 17:36:33 +0200871 program_drive_counts(drive, index);
872 set_prefetch_mode(drive, index, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 printk("cmd640: drive%d timings/prefetch cleared\n", index);
874 } else {
875 /*
876 * Record timings/prefetch without changing them.
877 * This preserves any prior BIOS setup.
878 */
879 retrieve_drive_counts (index);
Bartlomiej Zolnierkiewicz95230762008-04-26 17:36:33 +0200880 check_prefetch(drive, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 printk("cmd640: drive%d timings/prefetch(%s) preserved",
882 index, drive->no_io_32bit ? "off" : "on");
883 display_clocks(index);
884 }
885#else
886 /*
887 * Set the drive unmask flags to match the prefetch setting
888 */
Bartlomiej Zolnierkiewicz95230762008-04-26 17:36:33 +0200889 check_prefetch(drive, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 printk("cmd640: drive%d timings/prefetch(%s) preserved\n",
891 index, drive->no_io_32bit ? "off" : "on");
892#endif /* CONFIG_BLK_DEV_CMD640_ENHANCED */
893 }
894
895#ifdef CMD640_DUMP_REGS
896 cmd640_dump_regs();
897#endif
Bartlomiej Zolnierkiewicz8ac4ce72008-01-26 20:13:06 +0100898
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +0100899 ide_device_add(idx, &cmd640_port_info);
Bartlomiej Zolnierkiewicz8ac4ce72008-01-26 20:13:06 +0100900
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 return 1;
902}
903
Bartlomiej Zolnierkiewiczade2daf2008-01-26 20:13:07 +0100904module_param_named(probe_vlb, cmd640_vlb, bool, 0);
905MODULE_PARM_DESC(probe_vlb, "probe for VLB version of CMD640 chipset");
906
907module_init(cmd640x_init);
Adrian Bunk776c0bc2008-04-02 21:22:02 +0200908
909MODULE_LICENSE("GPL");