Import 2.3.48
[davej-history.git] / drivers / block / cmd64x.c
blob4e98893ecdd615c8161a069086e0c5b89a041be8
1 /* $Id: cmd64x.c,v 1.21 2000/01/30 23:23:16
3 * cmd64x.c: Enable interrupts at initialization time on Ultra/PCI machines.
4 * Note, this driver is not used at all on other systems because
5 * there the "BIOS" has done all of the following already.
6 * Due to massive hardware bugs, UltraDMA is only supported
7 * on the 646U2 and not on the 646U.
9 * Copyright (C) 1998 Eddie C. Dost (ecd@skynet.be)
10 * Copyright (C) 1998 David S. Miller (davem@redhat.com)
11 * Copyright (C) 1999-2000 Andre Hedrick (andre@suse.com)
14 #include <linux/types.h>
15 #include <linux/pci.h>
16 #include <linux/delay.h>
17 #include <linux/hdreg.h>
18 #include <linux/ide.h>
20 #include <asm/io.h>
21 #include "ide_modes.h"
23 #define CMD_DEBUG 0
24 #undef NO_WRITE
26 #if CMD_DEBUG
27 #define cmdprintk(x...) printk(##x)
28 #else
29 #define cmdprintk(x...)
30 #endif
33 * CMD64x specific registers definition.
36 #define CNTRL 0x51
37 #define CNTRL_DIS_RA0 0x40
38 #define CNTRL_DIS_RA1 0x80
39 #define CNTRL_ENA_2ND 0x08
41 #define CMDTIM 0x52
42 #define ARTTIM0 0x53
43 #define DRWTIM0 0x54
44 #define ARTTIM1 0x55
45 #define DRWTIM1 0x56
46 #define ARTTIM23 0x57
47 #define ARTTIM23_DIS_RA2 0x04
48 #define ARTTIM23_DIS_RA3 0x08
49 #define DRWTIM23 0x58
50 #define DRWTIM2 0x58
51 #define BRST 0x59
52 #define DRWTIM3 0x5b
54 #define MRDMODE 0x71
56 #define DISPLAY_CMD64X_TIMINGS
58 #if defined(DISPLAY_CMD64X_TIMINGS) && defined(CONFIG_PROC_FS)
59 #include <linux/stat.h>
60 #include <linux/proc_fs.h>
62 static int cmd64x_get_info(char *, char **, off_t, int);
63 extern int (*cmd64x_display_info)(char *, char **, off_t, int); /* ide-proc.c */
64 extern char *ide_media_verbose(ide_drive_t *);
65 static struct pci_dev *bmide_dev;
67 static int cmd64x_get_info (char *buffer, char **addr, off_t offset, int count)
69 char *p = buffer;
70 u32 bibma = bmide_dev->resource[4].start;
71 u8 c0 = 0, c1 = 0;
73 switch(bmide_dev->device) {
74 case PCI_DEVICE_ID_CMD_648:
75 p += sprintf(p, "\n CMD648 Chipset.\n");
76 break;
77 case PCI_DEVICE_ID_CMD_646:
78 p += sprintf(p, "\n CMD646 Chipset.\n");
79 break;
80 case PCI_DEVICE_ID_CMD_643:
81 p += sprintf(p, "\n CMD643 Chipset.\n");
82 break;
83 default:
84 p += sprintf(p, "\n CMD64? Chipse.\n");
85 break;
89 * at that point bibma+0x2 et bibma+0xa are byte registers
90 * to investigate:
92 c0 = inb_p((unsigned short)bibma + 0x02);
93 c1 = inb_p((unsigned short)bibma + 0x0a);
94 p += sprintf(p, "--------------- Primary Channel ---------------- Secondary Channel -------------\n");
95 p += sprintf(p, " %sabled %sabled\n",
96 (c0&0x80) ? "dis" : " en",
97 (c1&0x80) ? "dis" : " en");
98 p += sprintf(p, "--------------- drive0 --------- drive1 -------- drive0 ---------- drive1 ------\n");
99 p += sprintf(p, "DMA enabled: %s %s %s %s\n",
100 (c0&0x20) ? "yes" : "no ", (c0&0x40) ? "yes" : "no ",
101 (c1&0x20) ? "yes" : "no ", (c1&0x40) ? "yes" : "no " );
103 p += sprintf(p, "UDMA\n");
104 p += sprintf(p, "DMA\n");
105 p += sprintf(p, "PIO\n");
107 return p-buffer; /* => must be less than 4k! */
109 #endif /* defined(DISPLAY_CMD64X_TIMINGS) && defined(CONFIG_PROC_FS) */
111 byte cmd64x_proc = 0;
114 * Registers and masks for easy access by drive index:
116 #if 0
117 static byte prefetch_regs[4] = {CNTRL, CNTRL, ARTTIM23, ARTTIM23};
118 static byte prefetch_masks[4] = {CNTRL_DIS_RA0, CNTRL_DIS_RA1, ARTTIM23_DIS_RA2, ARTTIM23_DIS_RA3};
119 #endif
122 * This routine writes the prepared setup/active/recovery counts
123 * for a drive into the cmd646 chipset registers to active them.
125 static void program_drive_counts (ide_drive_t *drive, int setup_count, int active_count, int recovery_count)
127 unsigned long flags;
128 ide_drive_t *drives = HWIF(drive)->drives;
129 byte temp_b;
130 static const byte setup_counts[] = {0x40, 0x40, 0x40, 0x80, 0, 0xc0};
131 static const byte recovery_counts[] =
132 {15, 15, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 0};
133 static const byte arttim_regs[2][2] = {
134 { ARTTIM0, ARTTIM1 },
135 { ARTTIM23, ARTTIM23 }
137 static const byte drwtim_regs[2][2] = {
138 { DRWTIM0, DRWTIM1 },
139 { DRWTIM2, DRWTIM3 }
141 int channel = (int) HWIF(drive)->channel;
142 int slave = (drives != drive); /* Is this really the best way to determine this?? */
144 cmdprintk("program_drive_count parameters = s(%d),a(%d),r(%d),p(%d)\n", setup_count,
145 active_count, recovery_count, drive->present);
147 * Set up address setup count registers.
148 * Primary interface has individual count/timing registers for
149 * each drive. Secondary interface has one common set of registers,
150 * for address setup so we merge these timings, using the slowest
151 * value.
153 if (channel) {
154 drive->drive_data = setup_count;
155 setup_count = IDE_MAX(drives[0].drive_data, drives[1].drive_data);
156 cmdprintk("Secondary interface, setup_count = %d\n", setup_count);
160 * Convert values to internal chipset representation
162 setup_count = (setup_count > 5) ? 0xc0 : (int) setup_counts[setup_count];
163 active_count &= 0xf; /* Remember, max value is 16 */
164 recovery_count = (int) recovery_counts[recovery_count];
166 cmdprintk("Final values = %d,%d,%d\n", setup_count, active_count, recovery_count);
169 * Now that everything is ready, program the new timings
171 __save_flags (flags);
172 __cli();
174 * Program the address_setup clocks into ARTTIM reg,
175 * and then the active/recovery counts into the DRWTIM reg
177 (void) pci_read_config_byte(HWIF(drive)->pci_dev, arttim_regs[channel][slave], &temp_b);
178 #ifndef NO_WRITE
179 (void) pci_write_config_byte(HWIF(drive)->pci_dev, arttim_regs[channel][slave],
180 ((byte) setup_count) | (temp_b & 0x3f));
181 (void) pci_write_config_byte(HWIF(drive)->pci_dev, drwtim_regs[channel][slave],
182 (byte) ((active_count << 4) | recovery_count));
183 #endif
184 cmdprintk ("Write %x to %x\n", ((byte) setup_count) | (temp_b & 0x3f), arttim_regs[channel][slave]);
185 cmdprintk ("Write %x to %x\n", (byte) ((active_count << 4) | recovery_count), drwtim_regs[channel][slave]);
186 __restore_flags(flags);
190 * Attempts to set the interface PIO mode.
191 * The preferred method of selecting PIO modes (e.g. mode 4) is
192 * "echo 'piomode:4' > /proc/ide/hdx/settings". Special cases are
193 * 8: prefetch off, 9: prefetch on, 255: auto-select best mode.
194 * Called with 255 at boot time.
196 static void cmd64x_tuneproc (ide_drive_t *drive, byte mode_wanted)
198 int setup_time, active_time, recovery_time, clock_time, pio_mode, cycle_time;
199 byte recovery_count2, cycle_count;
200 int setup_count, active_count, recovery_count;
201 int bus_speed = ide_system_bus_speed();
202 /*byte b;*/
203 ide_pio_data_t d;
205 switch (mode_wanted) {
206 case 8: /* set prefetch off */
207 case 9: /* set prefetch on */
208 mode_wanted &= 1;
209 /*set_prefetch_mode(index, mode_wanted);*/
210 cmdprintk("%s: %sabled cmd640 prefetch\n", drive->name, mode_wanted ? "en" : "dis");
211 return;
214 (void) ide_get_best_pio_mode (drive, mode_wanted, 5, &d);
215 pio_mode = d.pio_mode;
216 cycle_time = d.cycle_time;
219 * I copied all this complicated stuff from cmd640.c and made a few minor changes.
220 * For now I am just going to pray that it is correct.
222 if (pio_mode > 5)
223 pio_mode = 5;
224 setup_time = ide_pio_timings[pio_mode].setup_time;
225 active_time = ide_pio_timings[pio_mode].active_time;
226 recovery_time = cycle_time - (setup_time + active_time);
227 clock_time = 1000 / bus_speed;
228 cycle_count = (cycle_time + clock_time - 1) / clock_time;
230 setup_count = (setup_time + clock_time - 1) / clock_time;
232 active_count = (active_time + clock_time - 1) / clock_time;
234 recovery_count = (recovery_time + clock_time - 1) / clock_time;
235 recovery_count2 = cycle_count - (setup_count + active_count);
236 if (recovery_count2 > recovery_count)
237 recovery_count = recovery_count2;
238 if (recovery_count > 16) {
239 active_count += recovery_count - 16;
240 recovery_count = 16;
242 if (active_count > 16)
243 active_count = 16; /* maximum allowed by cmd646 */
246 * In a perfect world, we might set the drive pio mode here
247 * (using WIN_SETFEATURE) before continuing.
249 * But we do not, because:
250 * 1) this is the wrong place to do it (proper is do_special() in ide.c)
251 * 2) in practice this is rarely, if ever, necessary
253 program_drive_counts (drive, setup_count, active_count, recovery_count);
255 printk ("%s: selected cmd646 PIO mode%d (%dns)%s, clocks=%d/%d/%d\n",
256 drive->name, pio_mode, cycle_time,
257 d.overridden ? " (overriding vendor mode)" : "",
258 setup_count, active_count, recovery_count);
261 static int config_chipset_for_dma (ide_drive_t *drive, unsigned int rev, byte ultra_66)
263 struct hd_driveid *id = drive->id;
264 ide_hwif_t *hwif = HWIF(drive);
265 struct pci_dev *dev = hwif->pci_dev;
266 unsigned long dma_base = hwif->dma_base;
268 byte unit = (drive->select.b.unit & 0x01);
269 byte speed = 0x00;
270 byte udma_timing_bits = 0x00;
271 byte udma_33 = ((rev >= 0x05) || (ultra_66)) ? 1 : 0;
272 byte udma_66 = ((id->hw_config & 0x2000) && (hwif->udma_four)) ? 1 : 0;
273 /* int drive_number = ((hwif->channel ? 2 : 0) + unit); */
274 int rval;
276 switch(dev->device) {
277 case PCI_DEVICE_ID_CMD_643:
278 case PCI_DEVICE_ID_CMD_646:
279 case PCI_DEVICE_ID_CMD_648:
280 default:
281 break;
284 if (drive->media != ide_disk) {
285 cmdprintk("CMD64X: drive->media != ide_disk at double check, inital check failed!!\n");
286 return ((int) ide_dma_off);
289 /* UltraDMA only supported on PCI646U and PCI646U2,
290 * which correspond to revisions 0x03, 0x05 and 0x07 respectively.
291 * Actually, although the CMD tech support people won't
292 * tell me the details, the 0x03 revision cannot support
293 * UDMA correctly without hardware modifications, and even
294 * then it only works with Quantum disks due to some
295 * hold time assumptions in the 646U part which are fixed
296 * in the 646U2.
297 * So we only do UltraDMA on revision 0x05 and 0x07 chipsets.
300 if ((id->dma_ultra & 0x0010) && (udma_66) && (udma_33)) {
301 speed = XFER_UDMA_4;
302 udma_timing_bits = 0x10; /* 2 clock */
303 } else if ((id->dma_ultra & 0x0008) && (udma_66) && (udma_33)) {
304 speed = XFER_UDMA_3;
305 udma_timing_bits = 0x20; /* 3 clock */
306 } else if ((id->dma_ultra & 0x0004) && (udma_33)) {
307 speed = XFER_UDMA_2;
308 udma_timing_bits = 0x10; /* 2 clock */
309 } else if ((id->dma_ultra & 0x0002) && (udma_33)) {
310 speed = XFER_UDMA_1;
311 udma_timing_bits = 0x20; /* 3 clock */
312 } else if ((id->dma_ultra & 0x0001) && (udma_33)) {
313 speed = XFER_UDMA_0;
314 udma_timing_bits = 0x30; /* 4 clock */
315 } else if (id->dma_mword & 0x0004) {
316 speed = XFER_MW_DMA_2;
317 } else if (id->dma_mword & 0x0002) {
318 speed = XFER_MW_DMA_1;
319 } else if (id->dma_mword & 0x0001) {
320 speed = XFER_MW_DMA_0;
321 } else if (id->dma_1word & 0x0004) {
322 speed = XFER_SW_DMA_2;
323 } else if (id->dma_1word & 0x0002) {
324 speed = XFER_SW_DMA_1;
325 } else if (id->dma_1word & 0x0001) {
326 speed = XFER_SW_DMA_0;
327 } else {
328 return ((int) ide_dma_off_quietly);
331 (void) ide_config_drive_speed(drive, speed);
332 outb(inb(dma_base+2)|(1<<(5+unit)), dma_base+2);
334 if (speed >= XFER_UDMA_0) {
335 byte udma_ctrl = inb(dma_base + 3);
336 /* Put this channel into UDMA mode. */
337 udma_ctrl |= (1 << unit);
338 udma_ctrl &= ~(0x04 << unit);
339 if (udma_66)
340 udma_ctrl |= (0x04 << unit);
341 udma_ctrl &= ~(0x30 << (unit * 2));
342 udma_ctrl |= (udma_timing_bits << (unit * 2));
343 outb(udma_ctrl, dma_base+3);
346 rval = (int)( ((id->dma_ultra >> 11) & 3) ? ide_dma_on :
347 ((id->dma_ultra >> 8) & 7) ? ide_dma_on :
348 ((id->dma_mword >> 8) & 7) ? ide_dma_on :
349 ((id->dma_1word >> 8) & 7) ? ide_dma_on :
350 ide_dma_off_quietly);
352 return rval;
355 static void config_chipset_for_pio (ide_drive_t *drive, unsigned int rev)
357 cmd64x_tuneproc(drive, 5);
360 static int cmd64x_config_drive_for_dma (ide_drive_t *drive)
362 struct hd_driveid *id = drive->id;
363 ide_hwif_t *hwif = HWIF(drive);
364 struct pci_dev *dev = hwif->pci_dev;
365 unsigned int class_rev = 0;
366 byte can_ultra_33 = 0;
367 byte can_ultra_66 = 0;
368 ide_dma_action_t dma_func = ide_dma_on;
370 pci_read_config_dword(dev, PCI_CLASS_REVISION, &class_rev);
371 class_rev &= 0xff;
373 switch(dev->device) {
374 case PCI_DEVICE_ID_CMD_643:
375 can_ultra_33 = 1;
376 can_ultra_66 = 0;
377 break;
378 case PCI_DEVICE_ID_CMD_646:
379 can_ultra_33 = (class_rev >= 0x05) ? 1 : 0;
380 can_ultra_66 = 0;
381 break;
382 case PCI_DEVICE_ID_CMD_648:
383 can_ultra_33 = 1;
384 can_ultra_66 = 1;
385 break;
386 default:
387 return hwif->dmaproc(ide_dma_off, drive);
390 if ((id != NULL) && ((id->capability & 1) != 0) &&
391 hwif->autodma && (drive->media == ide_disk)) {
392 /* Consult the list of known "bad" drives */
393 if (ide_dmaproc(ide_dma_bad_drive, drive)) {
394 dma_func = ide_dma_off;
395 goto fast_ata_pio;
397 dma_func = ide_dma_off_quietly;
398 if ((id->field_valid & 4) && (can_ultra_33)) {
399 if (id->dma_ultra & 0x001F) {
400 /* Force if Capable UltraDMA */
401 dma_func = config_chipset_for_dma(drive, class_rev, can_ultra_66);
402 if ((id->field_valid & 2) &&
403 (dma_func != ide_dma_on))
404 goto try_dma_modes;
406 } else if (id->field_valid & 2) {
407 try_dma_modes:
408 if ((id->dma_mword & 0x0007) ||
409 (id->dma_1word & 0x0007)) {
410 /* Force if Capable regular DMA modes */
411 dma_func = config_chipset_for_dma(drive, class_rev, 0);
412 if (dma_func != ide_dma_on)
413 goto no_dma_set;
415 } else if (ide_dmaproc(ide_dma_good_drive, drive)) {
416 if (id->eide_dma_time > 150) {
417 goto no_dma_set;
419 /* Consult the list of known "good" drives */
420 dma_func = config_chipset_for_dma(drive, class_rev, 0);
421 if (dma_func != ide_dma_on)
422 goto no_dma_set;
423 } else {
424 goto fast_ata_pio;
426 } else if ((id->capability & 8) || (id->field_valid & 2)) {
427 fast_ata_pio:
428 dma_func = ide_dma_off_quietly;
429 no_dma_set:
430 config_chipset_for_pio(drive, class_rev);
432 return HWIF(drive)->dmaproc(dma_func, drive);
435 static int cmd64x_dmaproc (ide_dma_action_t func, ide_drive_t *drive)
437 switch (func) {
438 case ide_dma_check:
439 return cmd64x_config_drive_for_dma(drive);
440 default:
441 break;
443 /* Other cases are done by generic IDE-DMA code. */
444 return ide_dmaproc(func, drive);
448 * ASUS P55T2P4D with CMD646 chipset revision 0x01 requires the old
449 * event order for DMA transfers.
451 static int cmd646_1_dmaproc (ide_dma_action_t func, ide_drive_t *drive)
453 ide_hwif_t *hwif = HWIF(drive);
454 unsigned long dma_base = hwif->dma_base;
455 byte dma_stat;
457 if (func == ide_dma_end) {
458 drive->waiting_for_dma = 0;
459 dma_stat = inb(dma_base+2); /* get DMA status */
460 outb(inb(dma_base)&~1, dma_base); /* stop DMA */
461 outb(dma_stat|6, dma_base+2); /* clear the INTR & ERROR bits */
462 ide_destroy_dmatable(drive); /* and free any DMA resources */
463 return (dma_stat & 7) != 4; /* verify good DMA status */
466 /* Other cases are done by generic IDE-DMA code. */
467 return cmd64x_dmaproc(func, drive);
470 unsigned int __init pci_init_cmd64x (struct pci_dev *dev, const char *name)
472 unsigned char mrdmode;
473 unsigned int class_rev;
475 pci_read_config_dword(dev, PCI_CLASS_REVISION, &class_rev);
476 class_rev &= 0xff;
478 switch(dev->device) {
479 case PCI_DEVICE_ID_CMD_643:
480 break;
481 case PCI_DEVICE_ID_CMD_646:
482 printk("%s: chipset revision 0x%02X, ", name, class_rev);
483 switch(class_rev) {
484 case 0x07:
485 case 0x05:
486 printk("UltraDMA Capable");
487 break;
488 case 0x03:
489 printk("MultiWord DMA Force Limited");
490 break;
491 case 0x01:
492 default:
493 printk("MultiWord DMA Limited, IRQ workaround enabled");
494 break;
496 printk("\n");
497 break;
498 case PCI_DEVICE_ID_CMD_648:
499 break;
500 default:
501 break;
504 /* Set a good latency timer and cache line size value. */
505 (void) pci_write_config_byte(dev, PCI_LATENCY_TIMER, 64);
506 #ifdef __sparc_v9__
507 (void) pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE, 0x10);
508 #endif
510 /* Setup interrupts. */
511 (void) pci_read_config_byte(dev, MRDMODE, &mrdmode);
512 mrdmode &= ~(0x30);
513 (void) pci_write_config_byte(dev, MRDMODE, mrdmode);
515 /* Use MEMORY READ LINE for reads.
516 * NOTE: Although not mentioned in the PCI0646U specs,
517 * these bits are write only and won't be read
518 * back as set or not. The PCI0646U2 specs clarify
519 * this point.
521 (void) pci_write_config_byte(dev, MRDMODE, mrdmode | 0x02);
522 #if 0
523 /* Set reasonable active/recovery/address-setup values. */
524 (void) pci_write_config_byte(dev, ARTTIM0, 0x40);
525 (void) pci_write_config_byte(dev, DRWTIM0, 0x3f);
526 (void) pci_write_config_byte(dev, ARTTIM1, 0x40);
527 (void) pci_write_config_byte(dev, DRWTIM1, 0x3f);
528 (void) pci_write_config_byte(dev, ARTTIM23, 0x5c);
529 (void) pci_write_config_byte(dev, DRWTIM23, 0x3f);
530 (void) pci_write_config_byte(dev, DRWTIM3, 0x3f);
531 #else
532 (void) pci_write_config_byte(dev, ARTTIM23, 0x1c);
533 #endif
535 #if defined(DISPLAY_CMD64X_TIMINGS) && defined(CONFIG_PROC_FS)
536 cmd64x_proc = 1;
537 bmide_dev = dev;
538 cmd64x_display_info = &cmd64x_get_info;
539 #endif /* DISPLAY_CMD64X_TIMINGS && CONFIG_PROC_FS */
541 return 0;
544 unsigned int __init ata66_cmd64x (ide_hwif_t *hwif)
546 byte ata66 = 0;
547 byte mask = (hwif->channel) ? 0x02 : 0x01;
549 pci_read_config_byte(hwif->pci_dev, 0x79, &ata66);
550 return (ata66 & mask) ? 1 : 0;
553 void __init ide_init_cmd64x (ide_hwif_t *hwif)
555 struct pci_dev *dev = hwif->pci_dev;
556 unsigned int class_rev;
558 pci_read_config_dword(dev, PCI_CLASS_REVISION, &class_rev);
559 class_rev &= 0xff;
561 hwif->tuneproc = &cmd64x_tuneproc;
563 if (!hwif->dma_base) {
564 hwif->drives[0].autotune = 1;
565 hwif->drives[1].autotune = 1;
566 return;
569 switch(dev->device) {
570 case PCI_DEVICE_ID_CMD_643:
571 hwif->dmaproc = &cmd64x_dmaproc;
572 break;
573 case PCI_DEVICE_ID_CMD_646:
574 hwif->chipset = ide_cmd646;
575 if (class_rev == 0x01) {
576 hwif->dmaproc = &cmd646_1_dmaproc;
577 } else {
578 hwif->dmaproc = &cmd64x_dmaproc;
580 break;
581 case PCI_DEVICE_ID_CMD_648:
582 hwif->dmaproc = &cmd64x_dmaproc;
583 break;
584 default:
585 break;