blob: a76451a074c6b9a13102660e8a367462ad36f29b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* $Id: cmd64x.c,v 1.21 2000/01/30 23:23:16
2 *
3 * linux/drivers/ide/pci/cmd64x.c Version 1.30 Sept 10, 2002
4 *
5 * cmd64x.c: Enable interrupts at initialization time on Ultra/PCI machines.
6 * Note, this driver is not used at all on other systems because
7 * there the "BIOS" has done all of the following already.
8 * Due to massive hardware bugs, UltraDMA is only supported
9 * on the 646U2 and not on the 646U.
10 *
11 * Copyright (C) 1998 Eddie C. Dost (ecd@skynet.be)
12 * Copyright (C) 1998 David S. Miller (davem@redhat.com)
13 *
14 * Copyright (C) 1999-2002 Andre Hedrick <andre@linux-ide.org>
15 */
16
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/module.h>
18#include <linux/types.h>
19#include <linux/pci.h>
20#include <linux/delay.h>
21#include <linux/hdreg.h>
22#include <linux/ide.h>
23#include <linux/init.h>
24
25#include <asm/io.h>
26
27#define DISPLAY_CMD64X_TIMINGS
28
29#define CMD_DEBUG 0
30
31#if CMD_DEBUG
32#define cmdprintk(x...) printk(x)
33#else
34#define cmdprintk(x...)
35#endif
36
37/*
38 * CMD64x specific registers definition.
39 */
40#define CFR 0x50
41#define CFR_INTR_CH0 0x02
42#define CNTRL 0x51
43#define CNTRL_DIS_RA0 0x40
44#define CNTRL_DIS_RA1 0x80
45#define CNTRL_ENA_2ND 0x08
46
47#define CMDTIM 0x52
48#define ARTTIM0 0x53
49#define DRWTIM0 0x54
50#define ARTTIM1 0x55
51#define DRWTIM1 0x56
52#define ARTTIM23 0x57
53#define ARTTIM23_DIS_RA2 0x04
54#define ARTTIM23_DIS_RA3 0x08
55#define ARTTIM23_INTR_CH1 0x10
56#define ARTTIM2 0x57
57#define ARTTIM3 0x57
58#define DRWTIM23 0x58
59#define DRWTIM2 0x58
60#define BRST 0x59
61#define DRWTIM3 0x5b
62
63#define BMIDECR0 0x70
64#define MRDMODE 0x71
65#define MRDMODE_INTR_CH0 0x04
66#define MRDMODE_INTR_CH1 0x08
67#define MRDMODE_BLK_CH0 0x10
68#define MRDMODE_BLK_CH1 0x20
69#define BMIDESR0 0x72
70#define UDIDETCR0 0x73
71#define DTPR0 0x74
72#define BMIDECR1 0x78
73#define BMIDECSR 0x79
74#define BMIDESR1 0x7A
75#define UDIDETCR1 0x7B
76#define DTPR1 0x7C
77
78#if defined(DISPLAY_CMD64X_TIMINGS) && defined(CONFIG_PROC_FS)
79#include <linux/stat.h>
80#include <linux/proc_fs.h>
81
82static u8 cmd64x_proc = 0;
83
84#define CMD_MAX_DEVS 5
85
86static struct pci_dev *cmd_devs[CMD_MAX_DEVS];
87static int n_cmd_devs;
88
89static char * print_cmd64x_get_info (char *buf, struct pci_dev *dev, int index)
90{
91 char *p = buf;
92
93 u8 reg53 = 0, reg54 = 0, reg55 = 0, reg56 = 0; /* primary */
94 u8 reg57 = 0, reg58 = 0, reg5b; /* secondary */
95 u8 reg72 = 0, reg73 = 0; /* primary */
96 u8 reg7a = 0, reg7b = 0; /* secondary */
97 u8 reg50 = 0, reg71 = 0; /* extra */
98
99 p += sprintf(p, "\nController: %d\n", index);
100 p += sprintf(p, "CMD%x Chipset.\n", dev->device);
101 (void) pci_read_config_byte(dev, CFR, &reg50);
102 (void) pci_read_config_byte(dev, ARTTIM0, &reg53);
103 (void) pci_read_config_byte(dev, DRWTIM0, &reg54);
104 (void) pci_read_config_byte(dev, ARTTIM1, &reg55);
105 (void) pci_read_config_byte(dev, DRWTIM1, &reg56);
106 (void) pci_read_config_byte(dev, ARTTIM2, &reg57);
107 (void) pci_read_config_byte(dev, DRWTIM2, &reg58);
108 (void) pci_read_config_byte(dev, DRWTIM3, &reg5b);
109 (void) pci_read_config_byte(dev, MRDMODE, &reg71);
110 (void) pci_read_config_byte(dev, BMIDESR0, &reg72);
111 (void) pci_read_config_byte(dev, UDIDETCR0, &reg73);
112 (void) pci_read_config_byte(dev, BMIDESR1, &reg7a);
113 (void) pci_read_config_byte(dev, UDIDETCR1, &reg7b);
114
115 p += sprintf(p, "--------------- Primary Channel "
116 "---------------- Secondary Channel "
117 "-------------\n");
118 p += sprintf(p, " %sabled "
119 " %sabled\n",
120 (reg72&0x80)?"dis":" en",
121 (reg7a&0x80)?"dis":" en");
122 p += sprintf(p, "--------------- drive0 "
123 "--------- drive1 -------- drive0 "
124 "---------- drive1 ------\n");
125 p += sprintf(p, "DMA enabled: %s %s"
126 " %s %s\n",
127 (reg72&0x20)?"yes":"no ", (reg72&0x40)?"yes":"no ",
128 (reg7a&0x20)?"yes":"no ", (reg7a&0x40)?"yes":"no ");
129
130 p += sprintf(p, "DMA Mode: %s(%s) %s(%s)",
131 (reg72&0x20)?((reg73&0x01)?"UDMA":" DMA"):" PIO",
132 (reg72&0x20)?(
133 ((reg73&0x30)==0x30)?(((reg73&0x35)==0x35)?"3":"0"):
134 ((reg73&0x20)==0x20)?(((reg73&0x25)==0x25)?"3":"1"):
135 ((reg73&0x10)==0x10)?(((reg73&0x15)==0x15)?"4":"2"):
136 ((reg73&0x00)==0x00)?(((reg73&0x05)==0x05)?"5":"2"):
137 "X"):"?",
138 (reg72&0x40)?((reg73&0x02)?"UDMA":" DMA"):" PIO",
139 (reg72&0x40)?(
140 ((reg73&0xC0)==0xC0)?(((reg73&0xC5)==0xC5)?"3":"0"):
141 ((reg73&0x80)==0x80)?(((reg73&0x85)==0x85)?"3":"1"):
142 ((reg73&0x40)==0x40)?(((reg73&0x4A)==0x4A)?"4":"2"):
143 ((reg73&0x00)==0x00)?(((reg73&0x0A)==0x0A)?"5":"2"):
144 "X"):"?");
145 p += sprintf(p, " %s(%s) %s(%s)\n",
146 (reg7a&0x20)?((reg7b&0x01)?"UDMA":" DMA"):" PIO",
147 (reg7a&0x20)?(
148 ((reg7b&0x30)==0x30)?(((reg7b&0x35)==0x35)?"3":"0"):
149 ((reg7b&0x20)==0x20)?(((reg7b&0x25)==0x25)?"3":"1"):
150 ((reg7b&0x10)==0x10)?(((reg7b&0x15)==0x15)?"4":"2"):
151 ((reg7b&0x00)==0x00)?(((reg7b&0x05)==0x05)?"5":"2"):
152 "X"):"?",
153 (reg7a&0x40)?((reg7b&0x02)?"UDMA":" DMA"):" PIO",
154 (reg7a&0x40)?(
155 ((reg7b&0xC0)==0xC0)?(((reg7b&0xC5)==0xC5)?"3":"0"):
156 ((reg7b&0x80)==0x80)?(((reg7b&0x85)==0x85)?"3":"1"):
157 ((reg7b&0x40)==0x40)?(((reg7b&0x4A)==0x4A)?"4":"2"):
158 ((reg7b&0x00)==0x00)?(((reg7b&0x0A)==0x0A)?"5":"2"):
159 "X"):"?" );
160 p += sprintf(p, "PIO Mode: %s %s"
161 " %s %s\n",
162 "?", "?", "?", "?");
163 p += sprintf(p, " %s %s\n",
164 (reg50 & CFR_INTR_CH0) ? "interrupting" : "polling ",
165 (reg57 & ARTTIM23_INTR_CH1) ? "interrupting" : "polling");
166 p += sprintf(p, " %s %s\n",
167 (reg71 & MRDMODE_INTR_CH0) ? "pending" : "clear ",
168 (reg71 & MRDMODE_INTR_CH1) ? "pending" : "clear");
169 p += sprintf(p, " %s %s\n",
170 (reg71 & MRDMODE_BLK_CH0) ? "blocked" : "enabled",
171 (reg71 & MRDMODE_BLK_CH1) ? "blocked" : "enabled");
172
173 return (char *)p;
174}
175
176static int cmd64x_get_info (char *buffer, char **addr, off_t offset, int count)
177{
178 char *p = buffer;
179 int i;
180
181 p += sprintf(p, "\n");
182 for (i = 0; i < n_cmd_devs; i++) {
183 struct pci_dev *dev = cmd_devs[i];
184 p = print_cmd64x_get_info(p, dev, i);
185 }
186 return p-buffer; /* => must be less than 4k! */
187}
188
189#endif /* defined(DISPLAY_CMD64X_TIMINGS) && defined(CONFIG_PROC_FS) */
190
191/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 * This routine writes the prepared setup/active/recovery counts
193 * for a drive into the cmd646 chipset registers to active them.
194 */
195static void program_drive_counts (ide_drive_t *drive, int setup_count, int active_count, int recovery_count)
196{
197 unsigned long flags;
198 struct pci_dev *dev = HWIF(drive)->pci_dev;
199 ide_drive_t *drives = HWIF(drive)->drives;
200 u8 temp_b;
201 static const u8 setup_counts[] = {0x40, 0x40, 0x40, 0x80, 0, 0xc0};
202 static const u8 recovery_counts[] =
203 {15, 15, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 0};
204 static const u8 arttim_regs[2][2] = {
205 { ARTTIM0, ARTTIM1 },
206 { ARTTIM23, ARTTIM23 }
207 };
208 static const u8 drwtim_regs[2][2] = {
209 { DRWTIM0, DRWTIM1 },
210 { DRWTIM2, DRWTIM3 }
211 };
212 int channel = (int) HWIF(drive)->channel;
213 int slave = (drives != drive); /* Is this really the best way to determine this?? */
214
215 cmdprintk("program_drive_count parameters = s(%d),a(%d),r(%d),p(%d)\n",
216 setup_count, active_count, recovery_count, drive->present);
217 /*
218 * Set up address setup count registers.
219 * Primary interface has individual count/timing registers for
220 * each drive. Secondary interface has one common set of registers,
221 * for address setup so we merge these timings, using the slowest
222 * value.
223 */
224 if (channel) {
225 drive->drive_data = setup_count;
226 setup_count = max(drives[0].drive_data,
227 drives[1].drive_data);
228 cmdprintk("Secondary interface, setup_count = %d\n",
229 setup_count);
230 }
231
232 /*
233 * Convert values to internal chipset representation
234 */
235 setup_count = (setup_count > 5) ? 0xc0 : (int) setup_counts[setup_count];
236 active_count &= 0xf; /* Remember, max value is 16 */
237 recovery_count = (int) recovery_counts[recovery_count];
238
239 cmdprintk("Final values = %d,%d,%d\n",
240 setup_count, active_count, recovery_count);
241
242 /*
243 * Now that everything is ready, program the new timings
244 */
245 local_irq_save(flags);
246 /*
247 * Program the address_setup clocks into ARTTIM reg,
248 * and then the active/recovery counts into the DRWTIM reg
249 */
250 (void) pci_read_config_byte(dev, arttim_regs[channel][slave], &temp_b);
251 (void) pci_write_config_byte(dev, arttim_regs[channel][slave],
252 ((u8) setup_count) | (temp_b & 0x3f));
253 (void) pci_write_config_byte(dev, drwtim_regs[channel][slave],
254 (u8) ((active_count << 4) | recovery_count));
255 cmdprintk ("Write %x to %x\n",
256 ((u8) setup_count) | (temp_b & 0x3f),
257 arttim_regs[channel][slave]);
258 cmdprintk ("Write %x to %x\n",
259 (u8) ((active_count << 4) | recovery_count),
260 drwtim_regs[channel][slave]);
261 local_irq_restore(flags);
262}
263
264/*
265 * Attempts to set the interface PIO mode.
266 * The preferred method of selecting PIO modes (e.g. mode 4) is
267 * "echo 'piomode:4' > /proc/ide/hdx/settings". Special cases are
268 * 8: prefetch off, 9: prefetch on, 255: auto-select best mode.
269 * Called with 255 at boot time.
270 */
271
272static void cmd64x_tuneproc (ide_drive_t *drive, u8 mode_wanted)
273{
274 int setup_time, active_time, recovery_time;
275 int clock_time, pio_mode, cycle_time;
276 u8 recovery_count2, cycle_count;
277 int setup_count, active_count, recovery_count;
278 int bus_speed = system_bus_clock();
279 /*byte b;*/
280 ide_pio_data_t d;
281
282 switch (mode_wanted) {
283 case 8: /* set prefetch off */
284 case 9: /* set prefetch on */
285 mode_wanted &= 1;
286 /*set_prefetch_mode(index, mode_wanted);*/
287 cmdprintk("%s: %sabled cmd640 prefetch\n",
288 drive->name, mode_wanted ? "en" : "dis");
289 return;
290 }
291
292 mode_wanted = ide_get_best_pio_mode (drive, mode_wanted, 5, &d);
293 pio_mode = d.pio_mode;
294 cycle_time = d.cycle_time;
295
296 /*
297 * I copied all this complicated stuff from cmd640.c and made a few
298 * minor changes. For now I am just going to pray that it is correct.
299 */
300 if (pio_mode > 5)
301 pio_mode = 5;
302 setup_time = ide_pio_timings[pio_mode].setup_time;
303 active_time = ide_pio_timings[pio_mode].active_time;
304 recovery_time = cycle_time - (setup_time + active_time);
305 clock_time = 1000 / bus_speed;
306 cycle_count = (cycle_time + clock_time - 1) / clock_time;
307
308 setup_count = (setup_time + clock_time - 1) / clock_time;
309
310 active_count = (active_time + clock_time - 1) / clock_time;
311
312 recovery_count = (recovery_time + clock_time - 1) / clock_time;
313 recovery_count2 = cycle_count - (setup_count + active_count);
314 if (recovery_count2 > recovery_count)
315 recovery_count = recovery_count2;
316 if (recovery_count > 16) {
317 active_count += recovery_count - 16;
318 recovery_count = 16;
319 }
320 if (active_count > 16)
321 active_count = 16; /* maximum allowed by cmd646 */
322
323 /*
324 * In a perfect world, we might set the drive pio mode here
325 * (using WIN_SETFEATURE) before continuing.
326 *
327 * But we do not, because:
328 * 1) this is the wrong place to do it
329 * (proper is do_special() in ide.c)
330 * 2) in practice this is rarely, if ever, necessary
331 */
332 program_drive_counts (drive, setup_count, active_count, recovery_count);
333
334 cmdprintk("%s: selected cmd646 PIO mode%d : %d (%dns)%s, "
335 "clocks=%d/%d/%d\n",
336 drive->name, pio_mode, mode_wanted, cycle_time,
337 d.overridden ? " (overriding vendor mode)" : "",
338 setup_count, active_count, recovery_count);
339}
340
341static u8 cmd64x_ratemask (ide_drive_t *drive)
342{
343 struct pci_dev *dev = HWIF(drive)->pci_dev;
344 u8 mode = 0;
345
346 switch(dev->device) {
347 case PCI_DEVICE_ID_CMD_649:
348 mode = 3;
349 break;
350 case PCI_DEVICE_ID_CMD_648:
351 mode = 2;
352 break;
353 case PCI_DEVICE_ID_CMD_643:
354 return 0;
355
356 case PCI_DEVICE_ID_CMD_646:
357 {
358 unsigned int class_rev = 0;
359 pci_read_config_dword(dev,
360 PCI_CLASS_REVISION, &class_rev);
361 class_rev &= 0xff;
362 /*
363 * UltraDMA only supported on PCI646U and PCI646U2, which
364 * correspond to revisions 0x03, 0x05 and 0x07 respectively.
365 * Actually, although the CMD tech support people won't
366 * tell me the details, the 0x03 revision cannot support
367 * UDMA correctly without hardware modifications, and even
368 * then it only works with Quantum disks due to some
369 * hold time assumptions in the 646U part which are fixed
370 * in the 646U2.
371 *
372 * So we only do UltraDMA on revision 0x05 and 0x07 chipsets.
373 */
374 switch(class_rev) {
375 case 0x07:
376 case 0x05:
377 return 1;
378 case 0x03:
379 case 0x01:
380 default:
381 return 0;
382 }
383 }
384 }
385 if (!eighty_ninty_three(drive))
386 mode = min(mode, (u8)1);
387 return mode;
388}
389
390static void config_cmd64x_chipset_for_pio (ide_drive_t *drive, u8 set_speed)
391{
392 u8 speed = 0x00;
393 u8 set_pio = ide_get_best_pio_mode(drive, 4, 5, NULL);
394
395 cmd64x_tuneproc(drive, set_pio);
396 speed = XFER_PIO_0 + set_pio;
397 if (set_speed)
398 (void) ide_config_drive_speed(drive, speed);
399}
400
401static void config_chipset_for_pio (ide_drive_t *drive, u8 set_speed)
402{
403 config_cmd64x_chipset_for_pio(drive, set_speed);
404}
405
406static int cmd64x_tune_chipset (ide_drive_t *drive, u8 xferspeed)
407{
408 ide_hwif_t *hwif = HWIF(drive);
409 struct pci_dev *dev = hwif->pci_dev;
410
411 u8 unit = (drive->select.b.unit & 0x01);
412 u8 regU = 0, pciU = (hwif->channel) ? UDIDETCR1 : UDIDETCR0;
413 u8 regD = 0, pciD = (hwif->channel) ? BMIDESR1 : BMIDESR0;
414
415 u8 speed = ide_rate_filter(cmd64x_ratemask(drive), xferspeed);
416
417 if (speed > XFER_PIO_4) {
418 (void) pci_read_config_byte(dev, pciD, &regD);
419 (void) pci_read_config_byte(dev, pciU, &regU);
420 regD &= ~(unit ? 0x40 : 0x20);
421 regU &= ~(unit ? 0xCA : 0x35);
422 (void) pci_write_config_byte(dev, pciD, regD);
423 (void) pci_write_config_byte(dev, pciU, regU);
424 (void) pci_read_config_byte(dev, pciD, &regD);
425 (void) pci_read_config_byte(dev, pciU, &regU);
426 }
427
428 switch(speed) {
429 case XFER_UDMA_5: regU |= (unit ? 0x0A : 0x05); break;
430 case XFER_UDMA_4: regU |= (unit ? 0x4A : 0x15); break;
431 case XFER_UDMA_3: regU |= (unit ? 0x8A : 0x25); break;
432 case XFER_UDMA_2: regU |= (unit ? 0x42 : 0x11); break;
433 case XFER_UDMA_1: regU |= (unit ? 0x82 : 0x21); break;
434 case XFER_UDMA_0: regU |= (unit ? 0xC2 : 0x31); break;
435 case XFER_MW_DMA_2: regD |= (unit ? 0x40 : 0x10); break;
436 case XFER_MW_DMA_1: regD |= (unit ? 0x80 : 0x20); break;
437 case XFER_MW_DMA_0: regD |= (unit ? 0xC0 : 0x30); break;
438 case XFER_SW_DMA_2: regD |= (unit ? 0x40 : 0x10); break;
439 case XFER_SW_DMA_1: regD |= (unit ? 0x80 : 0x20); break;
440 case XFER_SW_DMA_0: regD |= (unit ? 0xC0 : 0x30); break;
441 case XFER_PIO_4: cmd64x_tuneproc(drive, 4); break;
442 case XFER_PIO_3: cmd64x_tuneproc(drive, 3); break;
443 case XFER_PIO_2: cmd64x_tuneproc(drive, 2); break;
444 case XFER_PIO_1: cmd64x_tuneproc(drive, 1); break;
445 case XFER_PIO_0: cmd64x_tuneproc(drive, 0); break;
446
447 default:
448 return 1;
449 }
450
451 if (speed > XFER_PIO_4) {
452 (void) pci_write_config_byte(dev, pciU, regU);
453 regD |= (unit ? 0x40 : 0x20);
454 (void) pci_write_config_byte(dev, pciD, regD);
455 }
456
457 return (ide_config_drive_speed(drive, speed));
458}
459
460static int config_chipset_for_dma (ide_drive_t *drive)
461{
462 u8 speed = ide_dma_speed(drive, cmd64x_ratemask(drive));
463
464 config_chipset_for_pio(drive, !speed);
465
466 if (!speed)
467 return 0;
468
Bartlomiej Zolnierkiewicz056a6972007-02-17 02:40:24 +0100469 if (cmd64x_tune_chipset(drive, speed))
470 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
472 return ide_dma_enable(drive);
473}
474
475static int cmd64x_config_drive_for_dma (ide_drive_t *drive)
476{
477 ide_hwif_t *hwif = HWIF(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
Bartlomiej Zolnierkiewicz7569e8d2007-02-17 02:40:25 +0100479 if (ide_use_dma(drive) && config_chipset_for_dma(drive))
480 return hwif->ide_dma_on(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
Bartlomiej Zolnierkiewicz7569e8d2007-02-17 02:40:25 +0100482 if (ide_use_fast_pio(drive)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 config_chipset_for_pio(drive, 1);
484 return hwif->ide_dma_off_quietly(drive);
485 }
486 /* IORDY not supported */
487 return 0;
488}
489
490static int cmd64x_alt_dma_status (struct pci_dev *dev)
491{
492 switch(dev->device) {
493 case PCI_DEVICE_ID_CMD_648:
494 case PCI_DEVICE_ID_CMD_649:
495 return 1;
496 default:
497 break;
498 }
499 return 0;
500}
501
502static int cmd64x_ide_dma_end (ide_drive_t *drive)
503{
504 u8 dma_stat = 0, dma_cmd = 0;
505 ide_hwif_t *hwif = HWIF(drive);
506 struct pci_dev *dev = hwif->pci_dev;
507
508 drive->waiting_for_dma = 0;
509 /* read DMA command state */
Bartlomiej Zolnierkiewicz0ecdca22007-02-17 02:40:25 +0100510 dma_cmd = inb(hwif->dma_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 /* stop DMA */
Bartlomiej Zolnierkiewicz0ecdca22007-02-17 02:40:25 +0100512 outb(dma_cmd & ~1, hwif->dma_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 /* get DMA status */
Bartlomiej Zolnierkiewicz0ecdca22007-02-17 02:40:25 +0100514 dma_stat = inb(hwif->dma_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 /* clear the INTR & ERROR bits */
Bartlomiej Zolnierkiewicz0ecdca22007-02-17 02:40:25 +0100516 outb(dma_stat | 6, hwif->dma_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 if (cmd64x_alt_dma_status(dev)) {
518 u8 dma_intr = 0;
519 u8 dma_mask = (hwif->channel) ? ARTTIM23_INTR_CH1 :
520 CFR_INTR_CH0;
521 u8 dma_reg = (hwif->channel) ? ARTTIM2 : CFR;
522 (void) pci_read_config_byte(dev, dma_reg, &dma_intr);
523 /* clear the INTR bit */
524 (void) pci_write_config_byte(dev, dma_reg, dma_intr|dma_mask);
525 }
526 /* purge DMA mappings */
527 ide_destroy_dmatable(drive);
528 /* verify good DMA status */
529 return (dma_stat & 7) != 4;
530}
531
532static int cmd64x_ide_dma_test_irq (ide_drive_t *drive)
533{
534 ide_hwif_t *hwif = HWIF(drive);
535 struct pci_dev *dev = hwif->pci_dev;
536 u8 dma_alt_stat = 0, mask = (hwif->channel) ? MRDMODE_INTR_CH1 :
537 MRDMODE_INTR_CH0;
Bartlomiej Zolnierkiewicz0ecdca22007-02-17 02:40:25 +0100538 u8 dma_stat = inb(hwif->dma_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
540 (void) pci_read_config_byte(dev, MRDMODE, &dma_alt_stat);
541#ifdef DEBUG
542 printk("%s: dma_stat: 0x%02x dma_alt_stat: "
543 "0x%02x mask: 0x%02x\n", drive->name,
544 dma_stat, dma_alt_stat, mask);
545#endif
546 if (!(dma_alt_stat & mask))
547 return 0;
548
549 /* return 1 if INTR asserted */
550 if ((dma_stat & 4) == 4)
551 return 1;
552
553 return 0;
554}
555
556/*
557 * ASUS P55T2P4D with CMD646 chipset revision 0x01 requires the old
558 * event order for DMA transfers.
559 */
560
561static int cmd646_1_ide_dma_end (ide_drive_t *drive)
562{
563 ide_hwif_t *hwif = HWIF(drive);
564 u8 dma_stat = 0, dma_cmd = 0;
565
566 drive->waiting_for_dma = 0;
567 /* get DMA status */
Bartlomiej Zolnierkiewicz0ecdca22007-02-17 02:40:25 +0100568 dma_stat = inb(hwif->dma_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 /* read DMA command state */
Bartlomiej Zolnierkiewicz0ecdca22007-02-17 02:40:25 +0100570 dma_cmd = inb(hwif->dma_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 /* stop DMA */
Bartlomiej Zolnierkiewicz0ecdca22007-02-17 02:40:25 +0100572 outb(dma_cmd & ~1, hwif->dma_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 /* clear the INTR & ERROR bits */
Bartlomiej Zolnierkiewicz0ecdca22007-02-17 02:40:25 +0100574 outb(dma_stat | 6, hwif->dma_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 /* and free any DMA resources */
576 ide_destroy_dmatable(drive);
577 /* verify good DMA status */
578 return (dma_stat & 7) != 4;
579}
580
581static unsigned int __devinit init_chipset_cmd64x(struct pci_dev *dev, const char *name)
582{
583 u32 class_rev = 0;
584 u8 mrdmode = 0;
585
586 pci_read_config_dword(dev, PCI_CLASS_REVISION, &class_rev);
587 class_rev &= 0xff;
588
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 switch(dev->device) {
590 case PCI_DEVICE_ID_CMD_643:
591 break;
592 case PCI_DEVICE_ID_CMD_646:
593 printk(KERN_INFO "%s: chipset revision 0x%02X, ", name, class_rev);
594 switch(class_rev) {
595 case 0x07:
596 case 0x05:
597 printk("UltraDMA Capable");
598 break;
599 case 0x03:
600 printk("MultiWord DMA Force Limited");
601 break;
602 case 0x01:
603 default:
604 printk("MultiWord DMA Limited, IRQ workaround enabled");
605 break;
606 }
607 printk("\n");
608 break;
609 case PCI_DEVICE_ID_CMD_648:
610 case PCI_DEVICE_ID_CMD_649:
611 break;
612 default:
613 break;
614 }
615
616 /* Set a good latency timer and cache line size value. */
617 (void) pci_write_config_byte(dev, PCI_LATENCY_TIMER, 64);
618 /* FIXME: pci_set_master() to ensure a good latency timer value */
619
620 /* Setup interrupts. */
621 (void) pci_read_config_byte(dev, MRDMODE, &mrdmode);
622 mrdmode &= ~(0x30);
623 (void) pci_write_config_byte(dev, MRDMODE, mrdmode);
624
625 /* Use MEMORY READ LINE for reads.
626 * NOTE: Although not mentioned in the PCI0646U specs,
627 * these bits are write only and won't be read
628 * back as set or not. The PCI0646U2 specs clarify
629 * this point.
630 */
631 (void) pci_write_config_byte(dev, MRDMODE, mrdmode | 0x02);
632
633 /* Set reasonable active/recovery/address-setup values. */
634 (void) pci_write_config_byte(dev, ARTTIM0, 0x40);
635 (void) pci_write_config_byte(dev, DRWTIM0, 0x3f);
636 (void) pci_write_config_byte(dev, ARTTIM1, 0x40);
637 (void) pci_write_config_byte(dev, DRWTIM1, 0x3f);
638#ifdef __i386__
639 (void) pci_write_config_byte(dev, ARTTIM23, 0x1c);
640#else
641 (void) pci_write_config_byte(dev, ARTTIM23, 0x5c);
642#endif
643 (void) pci_write_config_byte(dev, DRWTIM23, 0x3f);
644 (void) pci_write_config_byte(dev, DRWTIM3, 0x3f);
645#ifdef CONFIG_PPC
646 (void) pci_write_config_byte(dev, UDIDETCR0, 0xf0);
647#endif /* CONFIG_PPC */
648
649#if defined(DISPLAY_CMD64X_TIMINGS) && defined(CONFIG_PROC_FS)
650
651 cmd_devs[n_cmd_devs++] = dev;
652
653 if (!cmd64x_proc) {
654 cmd64x_proc = 1;
655 ide_pci_create_host_proc("cmd64x", cmd64x_get_info);
656 }
657#endif /* DISPLAY_CMD64X_TIMINGS && CONFIG_PROC_FS */
658
659 return 0;
660}
661
662static unsigned int __devinit ata66_cmd64x(ide_hwif_t *hwif)
663{
664 u8 ata66 = 0, mask = (hwif->channel) ? 0x02 : 0x01;
665
666 switch(hwif->pci_dev->device) {
667 case PCI_DEVICE_ID_CMD_643:
668 case PCI_DEVICE_ID_CMD_646:
669 return ata66;
670 default:
671 break;
672 }
673 pci_read_config_byte(hwif->pci_dev, BMIDECSR, &ata66);
674 return (ata66 & mask) ? 1 : 0;
675}
676
677static void __devinit init_hwif_cmd64x(ide_hwif_t *hwif)
678{
679 struct pci_dev *dev = hwif->pci_dev;
680 unsigned int class_rev;
681
682 hwif->autodma = 0;
683 pci_read_config_dword(dev, PCI_CLASS_REVISION, &class_rev);
684 class_rev &= 0xff;
685
686 hwif->tuneproc = &cmd64x_tuneproc;
687 hwif->speedproc = &cmd64x_tune_chipset;
688
689 if (!hwif->dma_base) {
690 hwif->drives[0].autotune = 1;
691 hwif->drives[1].autotune = 1;
692 return;
693 }
694
695 hwif->atapi_dma = 1;
696
697 hwif->ultra_mask = 0x3f;
698 hwif->mwdma_mask = 0x07;
699 hwif->swdma_mask = 0x07;
700
701 if (dev->device == PCI_DEVICE_ID_CMD_643)
702 hwif->ultra_mask = 0x80;
703 if (dev->device == PCI_DEVICE_ID_CMD_646)
704 hwif->ultra_mask = (class_rev > 0x04) ? 0x07 : 0x80;
705 if (dev->device == PCI_DEVICE_ID_CMD_648)
706 hwif->ultra_mask = 0x1f;
707
708 hwif->ide_dma_check = &cmd64x_config_drive_for_dma;
709 if (!(hwif->udma_four))
710 hwif->udma_four = ata66_cmd64x(hwif);
711
712 if (dev->device == PCI_DEVICE_ID_CMD_646) {
713 hwif->chipset = ide_cmd646;
714 if (class_rev == 0x01) {
715 hwif->ide_dma_end = &cmd646_1_ide_dma_end;
716 } else {
717 hwif->ide_dma_end = &cmd64x_ide_dma_end;
718 hwif->ide_dma_test_irq = &cmd64x_ide_dma_test_irq;
719 }
720 } else {
721 hwif->ide_dma_end = &cmd64x_ide_dma_end;
722 hwif->ide_dma_test_irq = &cmd64x_ide_dma_test_irq;
723 }
724
725
726 if (!noautodma)
727 hwif->autodma = 1;
728 hwif->drives[0].autodma = hwif->autodma;
729 hwif->drives[1].autodma = hwif->autodma;
730}
731
732static ide_pci_device_t cmd64x_chipsets[] __devinitdata = {
733 { /* 0 */
734 .name = "CMD643",
735 .init_chipset = init_chipset_cmd64x,
736 .init_hwif = init_hwif_cmd64x,
737 .channels = 2,
738 .autodma = AUTODMA,
739 .bootable = ON_BOARD,
740 },{ /* 1 */
741 .name = "CMD646",
742 .init_chipset = init_chipset_cmd64x,
743 .init_hwif = init_hwif_cmd64x,
744 .channels = 2,
745 .autodma = AUTODMA,
746 .enablebits = {{0x00,0x00,0x00}, {0x51,0x80,0x80}},
747 .bootable = ON_BOARD,
748 },{ /* 2 */
749 .name = "CMD648",
750 .init_chipset = init_chipset_cmd64x,
751 .init_hwif = init_hwif_cmd64x,
752 .channels = 2,
753 .autodma = AUTODMA,
754 .bootable = ON_BOARD,
755 },{ /* 3 */
756 .name = "CMD649",
757 .init_chipset = init_chipset_cmd64x,
758 .init_hwif = init_hwif_cmd64x,
759 .channels = 2,
760 .autodma = AUTODMA,
761 .bootable = ON_BOARD,
762 }
763};
764
765static int __devinit cmd64x_init_one(struct pci_dev *dev, const struct pci_device_id *id)
766{
767 return ide_setup_pci_device(dev, &cmd64x_chipsets[id->driver_data]);
768}
769
770static struct pci_device_id cmd64x_pci_tbl[] = {
771 { PCI_VENDOR_ID_CMD, PCI_DEVICE_ID_CMD_643, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
772 { PCI_VENDOR_ID_CMD, PCI_DEVICE_ID_CMD_646, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 1},
773 { PCI_VENDOR_ID_CMD, PCI_DEVICE_ID_CMD_648, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 2},
774 { PCI_VENDOR_ID_CMD, PCI_DEVICE_ID_CMD_649, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 3},
775 { 0, },
776};
777MODULE_DEVICE_TABLE(pci, cmd64x_pci_tbl);
778
779static struct pci_driver driver = {
780 .name = "CMD64x_IDE",
781 .id_table = cmd64x_pci_tbl,
782 .probe = cmd64x_init_one,
783};
784
Bartlomiej Zolnierkiewicz82ab1ee2007-01-27 13:46:56 +0100785static int __init cmd64x_ide_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786{
787 return ide_pci_register_driver(&driver);
788}
789
790module_init(cmd64x_ide_init);
791
792MODULE_AUTHOR("Eddie Dost, David Miller, Andre Hedrick");
793MODULE_DESCRIPTION("PCI driver module for CMD64x IDE");
794MODULE_LICENSE("GPL");