]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/ide/pci/cmd640.c
ide: remove SECTOR_WORDS define
[mirror_ubuntu-bionic-kernel.git] / drivers / ide / pci / cmd640.c
CommitLineData
1da177e4 1/*
1da177e4
LT
2 * Copyright (C) 1995-1996 Linus Torvalds & authors (see below)
3 */
4
5/*
6 * Original authors: abramov@cecmow.enet.dec.com (Igor Abramov)
4752b5e7 7 * mlord@pobox.com (Mark Lord)
1da177e4
LT
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
1da177e4
LT
99#define CMD640_PREFETCH_MASKS 1
100
4752b5e7 101/*#define CMD640_DUMP_REGS */
1da177e4 102
1da177e4
LT
103#include <linux/types.h>
104#include <linux/kernel.h>
105#include <linux/delay.h>
1da177e4
LT
106#include <linux/ide.h>
107#include <linux/init.h>
108
109#include <asm/io.h>
110
7ebe5936
BZ
111#define DRV_NAME "cmd640"
112
ef87f8d0 113static int cmd640_vlb;
1da177e4
LT
114
115/*
116 * CMD640 specific registers definition.
117 */
118
119#define VID 0x00
120#define DID 0x02
121#define PCMD 0x04
122#define PCMD_ENA 0x01
123#define PSTTS 0x06
124#define REVID 0x08
125#define PROGIF 0x09
126#define SUBCL 0x0a
127#define BASCL 0x0b
128#define BaseA0 0x10
129#define BaseA1 0x14
130#define BaseA2 0x18
131#define BaseA3 0x1c
132#define INTLINE 0x3c
133#define INPINE 0x3d
134
135#define CFR 0x50
136#define CFR_DEVREV 0x03
137#define CFR_IDE01INTR 0x04
138#define CFR_DEVID 0x18
139#define CFR_AT_VESA_078h 0x20
140#define CFR_DSA1 0x40
141#define CFR_DSA0 0x80
142
143#define CNTRL 0x51
144#define CNTRL_DIS_RA0 0x40
145#define CNTRL_DIS_RA1 0x80
146#define CNTRL_ENA_2ND 0x08
147
148#define CMDTIM 0x52
149#define ARTTIM0 0x53
150#define DRWTIM0 0x54
151#define ARTTIM1 0x55
152#define DRWTIM1 0x56
153#define ARTTIM23 0x57
154#define ARTTIM23_DIS_RA2 0x04
155#define ARTTIM23_DIS_RA3 0x08
156#define DRWTIM23 0x58
157#define BRST 0x59
158
159/*
160 * Registers and masks for easy access by drive index:
161 */
162static u8 prefetch_regs[4] = {CNTRL, CNTRL, ARTTIM23, ARTTIM23};
163static u8 prefetch_masks[4] = {CNTRL_DIS_RA0, CNTRL_DIS_RA1, ARTTIM23_DIS_RA2, ARTTIM23_DIS_RA3};
164
165#ifdef CONFIG_BLK_DEV_CMD640_ENHANCED
166
167static u8 arttim_regs[4] = {ARTTIM0, ARTTIM1, ARTTIM23, ARTTIM23};
168static u8 drwtim_regs[4] = {DRWTIM0, DRWTIM1, DRWTIM23, DRWTIM23};
169
170/*
171 * Current cmd640 timing values for each drive.
172 * The defaults for each are the slowest possible timings.
173 */
174static u8 setup_counts[4] = {4, 4, 4, 4}; /* Address setup count (in clocks) */
175static u8 active_counts[4] = {16, 16, 16, 16}; /* Active count (encoded) */
176static u8 recovery_counts[4] = {16, 16, 16, 16}; /* Recovery count (encoded) */
177
178#endif /* CONFIG_BLK_DEV_CMD640_ENHANCED */
179
5bbcf924
BZ
180static DEFINE_SPINLOCK(cmd640_lock);
181
1da177e4
LT
182/*
183 * Interface to access cmd640x registers
184 */
185static unsigned int cmd640_key;
186static void (*__put_cmd640_reg)(u16 reg, u8 val);
187static u8 (*__get_cmd640_reg)(u16 reg);
188
189/*
190 * This is read from the CFR reg, and is used in several places.
191 */
192static unsigned int cmd640_chip_version;
193
194/*
195 * The CMD640x chip does not support DWORD config write cycles, but some
196 * of the BIOSes use them to implement the config services.
197 * Therefore, we must use direct IO instead.
198 */
199
200/* PCI method 1 access */
201
4752b5e7 202static void put_cmd640_reg_pci1(u16 reg, u8 val)
1da177e4
LT
203{
204 outl_p((reg & 0xfc) | cmd640_key, 0xcf8);
205 outb_p(val, (reg & 3) | 0xcfc);
206}
207
4752b5e7 208static u8 get_cmd640_reg_pci1(u16 reg)
1da177e4
LT
209{
210 outl_p((reg & 0xfc) | cmd640_key, 0xcf8);
211 return inb_p((reg & 3) | 0xcfc);
212}
213
214/* PCI method 2 access (from CMD datasheet) */
215
4752b5e7 216static void put_cmd640_reg_pci2(u16 reg, u8 val)
1da177e4
LT
217{
218 outb_p(0x10, 0xcf8);
219 outb_p(val, cmd640_key + reg);
220 outb_p(0, 0xcf8);
221}
222
4752b5e7 223static u8 get_cmd640_reg_pci2(u16 reg)
1da177e4
LT
224{
225 u8 b;
226
227 outb_p(0x10, 0xcf8);
228 b = inb_p(cmd640_key + reg);
229 outb_p(0, 0xcf8);
230 return b;
231}
232
233/* VLB access */
234
4752b5e7 235static void put_cmd640_reg_vlb(u16 reg, u8 val)
1da177e4
LT
236{
237 outb_p(reg, cmd640_key);
238 outb_p(val, cmd640_key + 4);
239}
240
4752b5e7 241static u8 get_cmd640_reg_vlb(u16 reg)
1da177e4
LT
242{
243 outb_p(reg, cmd640_key);
244 return inb_p(cmd640_key + 4);
245}
246
247static u8 get_cmd640_reg(u16 reg)
248{
1da177e4 249 unsigned long flags;
5bbcf924 250 u8 b;
1da177e4 251
5bbcf924 252 spin_lock_irqsave(&cmd640_lock, flags);
1da177e4 253 b = __get_cmd640_reg(reg);
5bbcf924 254 spin_unlock_irqrestore(&cmd640_lock, flags);
1da177e4
LT
255 return b;
256}
257
258static void put_cmd640_reg(u16 reg, u8 val)
259{
260 unsigned long flags;
261
5bbcf924 262 spin_lock_irqsave(&cmd640_lock, flags);
4752b5e7 263 __put_cmd640_reg(reg, val);
5bbcf924 264 spin_unlock_irqrestore(&cmd640_lock, flags);
1da177e4
LT
265}
266
4752b5e7 267static int __init match_pci_cmd640_device(void)
1da177e4
LT
268{
269 const u8 ven_dev[4] = {0x95, 0x10, 0x40, 0x06};
270 unsigned int i;
271 for (i = 0; i < 4; i++) {
272 if (get_cmd640_reg(i) != ven_dev[i])
273 return 0;
274 }
275#ifdef STUPIDLY_TRUST_BROKEN_PCMD_ENA_BIT
276 if ((get_cmd640_reg(PCMD) & PCMD_ENA) == 0) {
277 printk("ide: cmd640 on PCI disabled by BIOS\n");
278 return 0;
279 }
280#endif /* STUPIDLY_TRUST_BROKEN_PCMD_ENA_BIT */
281 return 1; /* success */
282}
283
284/*
285 * Probe for CMD640x -- pci method 1
286 */
4752b5e7 287static int __init probe_for_cmd640_pci1(void)
1da177e4
LT
288{
289 __get_cmd640_reg = get_cmd640_reg_pci1;
290 __put_cmd640_reg = put_cmd640_reg_pci1;
291 for (cmd640_key = 0x80000000;
292 cmd640_key <= 0x8000f800;
293 cmd640_key += 0x800) {
294 if (match_pci_cmd640_device())
295 return 1; /* success */
296 }
297 return 0;
298}
299
300/*
301 * Probe for CMD640x -- pci method 2
302 */
4752b5e7 303static int __init probe_for_cmd640_pci2(void)
1da177e4
LT
304{
305 __get_cmd640_reg = get_cmd640_reg_pci2;
306 __put_cmd640_reg = put_cmd640_reg_pci2;
307 for (cmd640_key = 0xc000; cmd640_key <= 0xcf00; cmd640_key += 0x100) {
308 if (match_pci_cmd640_device())
309 return 1; /* success */
310 }
311 return 0;
312}
313
314/*
315 * Probe for CMD640x -- vlb
316 */
4752b5e7 317static int __init probe_for_cmd640_vlb(void)
1da177e4
LT
318{
319 u8 b;
320
321 __get_cmd640_reg = get_cmd640_reg_vlb;
322 __put_cmd640_reg = put_cmd640_reg_vlb;
323 cmd640_key = 0x178;
324 b = get_cmd640_reg(CFR);
325 if (b == 0xff || b == 0x00 || (b & CFR_AT_VESA_078h)) {
326 cmd640_key = 0x78;
327 b = get_cmd640_reg(CFR);
328 if (b == 0xff || b == 0x00 || !(b & CFR_AT_VESA_078h))
329 return 0;
330 }
331 return 1; /* success */
332}
333
334/*
335 * Returns 1 if an IDE interface/drive exists at 0x170,
336 * Returns 0 otherwise.
337 */
4752b5e7 338static int __init secondary_port_responding(void)
1da177e4
LT
339{
340 unsigned long flags;
341
5bbcf924 342 spin_lock_irqsave(&cmd640_lock, flags);
1da177e4 343
4c3032d8 344 outb_p(0x0a, 0x176); /* select drive0 */
1da177e4 345 udelay(100);
4c3032d8
BZ
346 if ((inb_p(0x176) & 0x1f) != 0x0a) {
347 outb_p(0x1a, 0x176); /* select drive1 */
1da177e4 348 udelay(100);
4c3032d8 349 if ((inb_p(0x176) & 0x1f) != 0x1a) {
5bbcf924 350 spin_unlock_irqrestore(&cmd640_lock, flags);
1da177e4
LT
351 return 0; /* nothing responded */
352 }
353 }
5bbcf924 354 spin_unlock_irqrestore(&cmd640_lock, flags);
1da177e4
LT
355 return 1; /* success */
356}
357
358#ifdef CMD640_DUMP_REGS
359/*
360 * Dump out all cmd640 registers. May be called from ide.c
361 */
4752b5e7 362static void cmd640_dump_regs(void)
1da177e4
LT
363{
364 unsigned int reg = cmd640_vlb ? 0x50 : 0x00;
365
366 /* Dump current state of chip registers */
367 printk("ide: cmd640 internal register dump:");
368 for (; reg <= 0x59; reg++) {
369 if (!(reg & 0x0f))
370 printk("\n%04x:", reg);
371 printk(" %02x", get_cmd640_reg(reg));
372 }
373 printk("\n");
374}
375#endif
376
bdffe5d2 377#ifndef CONFIG_BLK_DEV_CMD640_ENHANCED
1da177e4
LT
378/*
379 * Check whether prefetch is on for a drive,
380 * and initialize the unmask flags for safe operation.
381 */
9523076a 382static void __init check_prefetch(ide_drive_t *drive, unsigned int index)
1da177e4 383{
1da177e4
LT
384 u8 b = get_cmd640_reg(prefetch_regs[index]);
385
386 if (b & prefetch_masks[index]) { /* is prefetch off? */
387 drive->no_unmask = 0;
388 drive->no_io_32bit = 1;
389 drive->io_32bit = 0;
390 } else {
391#if CMD640_PREFETCH_MASKS
392 drive->no_unmask = 1;
393 drive->unmask = 0;
394#endif
395 drive->no_io_32bit = 0;
396 }
397}
bdffe5d2 398#else
1da177e4
LT
399/*
400 * Sets prefetch mode for a drive.
401 */
9523076a 402static void set_prefetch_mode(ide_drive_t *drive, unsigned int index, int mode)
1da177e4 403{
5bbcf924 404 unsigned long flags;
1da177e4
LT
405 int reg = prefetch_regs[index];
406 u8 b;
1da177e4 407
5bbcf924 408 spin_lock_irqsave(&cmd640_lock, flags);
1da177e4
LT
409 b = __get_cmd640_reg(reg);
410 if (mode) { /* want prefetch on? */
411#if CMD640_PREFETCH_MASKS
412 drive->no_unmask = 1;
413 drive->unmask = 0;
414#endif
415 drive->no_io_32bit = 0;
416 b &= ~prefetch_masks[index]; /* enable prefetch */
417 } else {
418 drive->no_unmask = 0;
419 drive->no_io_32bit = 1;
420 drive->io_32bit = 0;
421 b |= prefetch_masks[index]; /* disable prefetch */
422 }
423 __put_cmd640_reg(reg, b);
5bbcf924 424 spin_unlock_irqrestore(&cmd640_lock, flags);
1da177e4
LT
425}
426
427/*
428 * Dump out current drive clocks settings
429 */
4752b5e7 430static void display_clocks(unsigned int index)
1da177e4
LT
431{
432 u8 active_count, recovery_count;
433
434 active_count = active_counts[index];
435 if (active_count == 1)
436 ++active_count;
437 recovery_count = recovery_counts[index];
438 if (active_count > 3 && recovery_count == 1)
439 ++recovery_count;
440 if (cmd640_chip_version > 1)
441 recovery_count += 1; /* cmd640b uses (count + 1)*/
442 printk(", clocks=%d/%d/%d\n", setup_counts[index], active_count, recovery_count);
443}
444
445/*
446 * Pack active and recovery counts into single byte representation
447 * used by controller
448 */
4752b5e7 449static inline u8 pack_nibbles(u8 upper, u8 lower)
1da177e4
LT
450{
451 return ((upper & 0x0f) << 4) | (lower & 0x0f);
452}
453
1da177e4
LT
454/*
455 * This routine writes the prepared setup/active/recovery counts
456 * for a drive into the cmd640 chipset registers to active them.
457 */
9523076a 458static void program_drive_counts(ide_drive_t *drive, unsigned int index)
1da177e4
LT
459{
460 unsigned long flags;
461 u8 setup_count = setup_counts[index];
462 u8 active_count = active_counts[index];
463 u8 recovery_count = recovery_counts[index];
464
465 /*
466 * Set up address setup count and drive read/write timing registers.
467 * Primary interface has individual count/timing registers for
468 * each drive. Secondary interface has one common set of registers,
469 * so we merge the timings, using the slowest value for each timing.
470 */
471 if (index > 1) {
9523076a
BZ
472 ide_hwif_t *hwif = drive->hwif;
473 ide_drive_t *peer = &hwif->drives[!drive->select.b.unit];
474 unsigned int mate = index ^ 1;
475
476 if (peer->present) {
1da177e4
LT
477 if (setup_count < setup_counts[mate])
478 setup_count = setup_counts[mate];
479 if (active_count < active_counts[mate])
480 active_count = active_counts[mate];
481 if (recovery_count < recovery_counts[mate])
482 recovery_count = recovery_counts[mate];
483 }
484 }
485
486 /*
487 * Convert setup_count to internal chipset representation
488 */
489 switch (setup_count) {
4752b5e7
PC
490 case 4: setup_count = 0x00; break;
491 case 3: setup_count = 0x80; break;
492 case 1:
493 case 2: setup_count = 0x40; break;
494 default: setup_count = 0xc0; /* case 5 */
1da177e4
LT
495 }
496
497 /*
498 * Now that everything is ready, program the new timings
499 */
5bbcf924 500 spin_lock_irqsave(&cmd640_lock, flags);
1da177e4
LT
501 /*
502 * Program the address_setup clocks into ARTTIM reg,
503 * and then the active/recovery counts into the DRWTIM reg
504 * (this converts counts of 16 into counts of zero -- okay).
505 */
506 setup_count |= __get_cmd640_reg(arttim_regs[index]) & 0x3f;
507 __put_cmd640_reg(arttim_regs[index], setup_count);
508 __put_cmd640_reg(drwtim_regs[index], pack_nibbles(active_count, recovery_count));
5bbcf924 509 spin_unlock_irqrestore(&cmd640_lock, flags);
1da177e4
LT
510}
511
512/*
513 * Set a specific pio_mode for a drive
514 */
9523076a
BZ
515static void cmd640_set_mode(ide_drive_t *drive, unsigned int index,
516 u8 pio_mode, unsigned int cycle_time)
1da177e4 517{
17b500de 518 struct ide_timing *t;
1da177e4
LT
519 int setup_time, active_time, recovery_time, clock_time;
520 u8 setup_count, active_count, recovery_count, recovery_count2, cycle_count;
ebae41a5
BZ
521 int bus_speed;
522
30e5ee4d
BZ
523 if (cmd640_vlb)
524 bus_speed = ide_vlb_clk ? ide_vlb_clk : 50;
ebae41a5 525 else
30e5ee4d 526 bus_speed = ide_pci_clk ? ide_pci_clk : 33;
1da177e4
LT
527
528 if (pio_mode > 5)
529 pio_mode = 5;
17b500de
BZ
530
531 t = ide_timing_find_mode(XFER_PIO_0 + pio_mode);
532 setup_time = t->setup;
533 active_time = t->active;
534
1da177e4
LT
535 recovery_time = cycle_time - (setup_time + active_time);
536 clock_time = 1000 / bus_speed;
00fe8b7a 537 cycle_count = DIV_ROUND_UP(cycle_time, clock_time);
1da177e4 538
00fe8b7a 539 setup_count = DIV_ROUND_UP(setup_time, clock_time);
1da177e4 540
00fe8b7a 541 active_count = DIV_ROUND_UP(active_time, clock_time);
1da177e4
LT
542 if (active_count < 2)
543 active_count = 2; /* minimum allowed by cmd640 */
544
00fe8b7a 545 recovery_count = DIV_ROUND_UP(recovery_time, clock_time);
1da177e4
LT
546 recovery_count2 = cycle_count - (setup_count + active_count);
547 if (recovery_count2 > recovery_count)
548 recovery_count = recovery_count2;
549 if (recovery_count < 2)
550 recovery_count = 2; /* minimum allowed by cmd640 */
551 if (recovery_count > 17) {
552 active_count += recovery_count - 17;
553 recovery_count = 17;
554 }
555 if (active_count > 16)
556 active_count = 16; /* maximum allowed by cmd640 */
557 if (cmd640_chip_version > 1)
558 recovery_count -= 1; /* cmd640b uses (count + 1)*/
559 if (recovery_count > 16)
560 recovery_count = 16; /* maximum allowed by cmd640 */
561
562 setup_counts[index] = setup_count;
563 active_counts[index] = active_count;
564 recovery_counts[index] = recovery_count;
565
566 /*
567 * In a perfect world, we might set the drive pio mode here
568 * (using WIN_SETFEATURE) before continuing.
569 *
570 * But we do not, because:
571 * 1) this is the wrong place to do it (proper is do_special() in ide.c)
572 * 2) in practice this is rarely, if ever, necessary
573 */
9523076a 574 program_drive_counts(drive, index);
1da177e4
LT
575}
576
26bcb879 577static void cmd640_set_pio_mode(ide_drive_t *drive, const u8 pio)
1da177e4 578{
7dd00083 579 unsigned int index = 0, cycle_time;
1da177e4 580 u8 b;
1da177e4 581
26bcb879 582 switch (pio) {
4752b5e7
PC
583 case 6: /* set fast-devsel off */
584 case 7: /* set fast-devsel on */
585 b = get_cmd640_reg(CNTRL) & ~0x27;
586 if (pio & 1)
587 b |= 0x27;
588 put_cmd640_reg(CNTRL, b);
589 printk("%s: %sabled cmd640 fast host timing (devsel)\n",
590 drive->name, (pio & 1) ? "en" : "dis");
591 return;
592 case 8: /* set prefetch off */
593 case 9: /* set prefetch on */
594 set_prefetch_mode(drive, index, pio & 1);
595 printk("%s: %sabled cmd640 prefetch\n",
596 drive->name, (pio & 1) ? "en" : "dis");
597 return;
1da177e4
LT
598 }
599
26bcb879 600 cycle_time = ide_pio_cycle_time(drive, pio);
9523076a 601 cmd640_set_mode(drive, index, pio, cycle_time);
1da177e4 602
342cdb6d 603 printk("%s: selected cmd640 PIO mode%d (%dns)",
26bcb879 604 drive->name, pio, cycle_time);
342cdb6d 605
1da177e4 606 display_clocks(index);
1da177e4 607}
b48c89a9
BZ
608#endif /* CONFIG_BLK_DEV_CMD640_ENHANCED */
609
610static void cmd640_init_dev(ide_drive_t *drive)
611{
612 unsigned int i = drive->hwif->channel * 2 + drive->select.b.unit;
613
614#ifdef CONFIG_BLK_DEV_CMD640_ENHANCED
615 /*
616 * Reset timing to the slowest speed and turn off prefetch.
617 * This way, the drive identify code has a better chance.
618 */
619 setup_counts[i] = 4; /* max possible */
620 active_counts[i] = 16; /* max possible */
621 recovery_counts[i] = 16; /* max possible */
622 program_drive_counts(drive, i);
623 set_prefetch_mode(drive, i, 0);
624 printk(KERN_INFO DRV_NAME ": drive%d timings/prefetch cleared\n", i);
625#else
626 /*
627 * Set the drive unmask flags to match the prefetch setting.
628 */
629 check_prefetch(drive, i);
630 printk(KERN_INFO DRV_NAME ": drive%d timings/prefetch(%s) preserved\n",
631 i, drive->no_io_32bit ? "off" : "on");
632#endif /* CONFIG_BLK_DEV_CMD640_ENHANCED */
633}
634
1da177e4 635
ac95beed 636static const struct ide_port_ops cmd640_port_ops = {
b48c89a9
BZ
637 .init_dev = cmd640_init_dev,
638#ifdef CONFIG_BLK_DEV_CMD640_ENHANCED
ac95beed 639 .set_pio_mode = cmd640_set_pio_mode,
b48c89a9 640#endif
ac95beed 641};
1da177e4
LT
642
643static int pci_conf1(void)
644{
1da177e4 645 unsigned long flags;
5bbcf924 646 u32 tmp;
1da177e4 647
5bbcf924 648 spin_lock_irqsave(&cmd640_lock, flags);
1da177e4
LT
649 outb(0x01, 0xCFB);
650 tmp = inl(0xCF8);
651 outl(0x80000000, 0xCF8);
652 if (inl(0xCF8) == 0x80000000) {
653 outl(tmp, 0xCF8);
5bbcf924 654 spin_unlock_irqrestore(&cmd640_lock, flags);
1da177e4
LT
655 return 1;
656 }
657 outl(tmp, 0xCF8);
5bbcf924 658 spin_unlock_irqrestore(&cmd640_lock, flags);
1da177e4
LT
659 return 0;
660}
661
662static int pci_conf2(void)
663{
664 unsigned long flags;
665
5bbcf924 666 spin_lock_irqsave(&cmd640_lock, flags);
1da177e4
LT
667 outb(0x00, 0xCFB);
668 outb(0x00, 0xCF8);
669 outb(0x00, 0xCFA);
670 if (inb(0xCF8) == 0x00 && inb(0xCF8) == 0x00) {
5bbcf924 671 spin_unlock_irqrestore(&cmd640_lock, flags);
1da177e4
LT
672 return 1;
673 }
5bbcf924 674 spin_unlock_irqrestore(&cmd640_lock, flags);
1da177e4
LT
675 return 0;
676}
677
c413b9b9
BZ
678static const struct ide_port_info cmd640_port_info __initdata = {
679 .chipset = ide_cmd640,
680 .host_flags = IDE_HFLAG_SERIALIZE |
681 IDE_HFLAG_NO_DMA |
c413b9b9
BZ
682 IDE_HFLAG_ABUSE_PREFETCH |
683 IDE_HFLAG_ABUSE_FAST_DEVSEL,
ac95beed 684 .port_ops = &cmd640_port_ops,
c413b9b9 685 .pio_mask = ATA_PIO5,
c413b9b9
BZ
686};
687
7ebe5936
BZ
688static 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
1da177e4 706/*
ade2daf9 707 * Probe for a cmd640 chipset, and initialize it if found.
1da177e4 708 */
ade2daf9 709static int __init cmd640x_init(void)
1da177e4 710{
7ebe5936 711 int second_port_cmd640 = 0, rc;
1da177e4 712 const char *bus_type, *port2;
1da177e4 713 u8 b, cfr;
c97c6aca 714 hw_regs_t hw[2], *hws[] = { NULL, NULL, NULL, NULL };
1da177e4
LT
715
716 if (cmd640_vlb && probe_for_cmd640_vlb()) {
717 bus_type = "VLB";
718 } else {
719 cmd640_vlb = 0;
720 /* Find out what kind of PCI probing is supported otherwise
721 Justin Gibbs will sulk.. */
722 if (pci_conf1() && probe_for_cmd640_pci1())
723 bus_type = "PCI (type1)";
724 else if (pci_conf2() && probe_for_cmd640_pci2())
725 bus_type = "PCI (type2)";
726 else
727 return 0;
728 }
729 /*
730 * Undocumented magic (there is no 0x5b reg in specs)
731 */
732 put_cmd640_reg(0x5b, 0xbd);
733 if (get_cmd640_reg(0x5b) != 0xbd) {
734 printk(KERN_ERR "ide: cmd640 init failed: wrong value in reg 0x5b\n");
735 return 0;
736 }
737 put_cmd640_reg(0x5b, 0);
738
739#ifdef CMD640_DUMP_REGS
740 cmd640_dump_regs();
741#endif
742
743 /*
744 * Documented magic begins here
745 */
746 cfr = get_cmd640_reg(CFR);
747 cmd640_chip_version = cfr & CFR_DEVREV;
748 if (cmd640_chip_version == 0) {
4752b5e7 749 printk("ide: bad cmd640 revision: %d\n", cmd640_chip_version);
1da177e4
LT
750 return 0;
751 }
752
7ebe5936
BZ
753 rc = cmd640x_init_one(0x1f0, 0x3f6);
754 if (rc)
755 return rc;
756
757 rc = cmd640x_init_one(0x170, 0x376);
758 if (rc) {
759 release_region(0x3f6, 1);
760 release_region(0x1f0, 8);
761 return rc;
762 }
763
6d3803b6
BZ
764 memset(&hw, 0, sizeof(hw));
765
766 ide_std_init_ports(&hw[0], 0x1f0, 0x3f6);
767 hw[0].irq = 14;
d427e836 768 hw[0].chipset = ide_cmd640;
6d3803b6
BZ
769
770 ide_std_init_ports(&hw[1], 0x170, 0x376);
771 hw[1].irq = 15;
d427e836 772 hw[1].chipset = ide_cmd640;
6d3803b6
BZ
773
774 printk(KERN_INFO "cmd640: buggy cmd640%c interface on %s, config=0x%02x"
775 "\n", 'a' + cmd640_chip_version - 1, bus_type, cfr);
776
1da177e4
LT
777 /*
778 * Initialize data for primary port
779 */
48c3c107 780 hws[0] = &hw[0];
8ac4ce74 781
1da177e4
LT
782 /*
783 * Ensure compatibility by always using the slowest timings
784 * for access to the drive's command register block,
785 * and reset the prefetch burstsize to default (512 bytes).
786 *
787 * Maybe we need a way to NOT do these on *some* systems?
788 */
789 put_cmd640_reg(CMDTIM, 0);
790 put_cmd640_reg(BRST, 0x40);
791
a698400a 792 b = get_cmd640_reg(CNTRL);
84f05df4 793
1da177e4
LT
794 /*
795 * Try to enable the secondary interface, if not already enabled
796 */
a698400a
BZ
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";
1da177e4 806 } else {
a698400a 807 put_cmd640_reg(CNTRL, b ^ CNTRL_ENA_2ND); /* toggle the bit */
1da177e4 808 if (secondary_port_responding()) {
a698400a
BZ
809 second_port_cmd640 = 1;
810 port2 = "enabled";
1da177e4 811 } else {
a698400a
BZ
812 put_cmd640_reg(CNTRL, b); /* restore original setting */
813 port2 = "not responding";
1da177e4
LT
814 }
815 }
816
817 /*
818 * Initialize data for secondary cmd640 port, if enabled
819 */
48c3c107
BZ
820 if (second_port_cmd640)
821 hws[1] = &hw[1];
822
84f05df4 823 printk(KERN_INFO "cmd640: %sserialized, secondary interface %s\n",
c413b9b9 824 second_port_cmd640 ? "" : "not ", port2);
1da177e4 825
1da177e4
LT
826#ifdef CMD640_DUMP_REGS
827 cmd640_dump_regs();
828#endif
8ac4ce74 829
6f904d01 830 return ide_host_add(&cmd640_port_info, hws, NULL);
1da177e4
LT
831}
832
ade2daf9
BZ
833module_param_named(probe_vlb, cmd640_vlb, bool, 0);
834MODULE_PARM_DESC(probe_vlb, "probe for VLB version of CMD640 chipset");
835
836module_init(cmd640x_init);
776c0bce
AB
837
838MODULE_LICENSE("GPL");