2 * linux/drivers/ide/pci/atiixp.c Version 0.01-bart2 Feb. 26, 2004
4 * Copyright (C) 2003 ATI Inc. <hyu@ati.com>
5 * Copyright (C) 2004 Bartlomiej Zolnierkiewicz
9 #include <linux/config.h>
10 #include <linux/types.h>
11 #include <linux/module.h>
12 #include <linux/kernel.h>
13 #include <linux/ioport.h>
14 #include <linux/pci.h>
15 #include <linux/hdreg.h>
16 #include <linux/ide.h>
17 #include <linux/delay.h>
18 #include <linux/init.h>
22 #include "ide_modes.h"
24 #define ATIIXP_IDE_PIO_TIMING 0x40
25 #define ATIIXP_IDE_MDMA_TIMING 0x44
26 #define ATIIXP_IDE_PIO_CONTROL 0x48
27 #define ATIIXP_IDE_PIO_MODE 0x4a
28 #define ATIIXP_IDE_UDMA_CONTROL 0x54
29 #define ATIIXP_IDE_UDMA_MODE 0x56
36 static atiixp_ide_timing pio_timing[] = {
44 static atiixp_ide_timing mdma_timing[] = {
50 static int save_mdma_mode[4];
52 #define DISPLAY_ATIIXP_TIMINGS
54 #if defined(DISPLAY_ATIIXP_TIMINGS) && defined(CONFIG_PROC_FS)
56 #include <linux/stat.h>
57 #include <linux/proc_fs.h>
59 static u8 atiixp_proc;
60 static struct pci_dev *bmide_dev;
63 * atiixp_get_info - fill in /proc for ATIIXP IDE
64 * @buffer: buffer to fill
65 * @addr: address of user start in buffer
66 * @offset: offset into 'file'
67 * @count: buffer count
69 * Output summary data on the tuning.
72 static int atiixp_get_info(char *buffer, char **addr, off_t offset, int count)
75 struct pci_dev *dev = bmide_dev;
76 unsigned long bibma = pci_resource_start(dev, 4);
78 u16 udma_mode = 0, pio_mode = 0;
79 u8 c0, c1, udma_control = 0;
81 p += sprintf(p, "\n ATI ");
82 p += sprintf(p, "ATIIXP Ultra100 IDE Chipset.\n");
84 pci_read_config_byte(dev, ATIIXP_IDE_UDMA_CONTROL, &udma_control);
85 pci_read_config_word(dev, ATIIXP_IDE_UDMA_MODE, &udma_mode);
86 pci_read_config_word(dev, ATIIXP_IDE_PIO_MODE, &pio_mode);
87 pci_read_config_dword(dev, ATIIXP_IDE_MDMA_TIMING, &mdma_timing);
90 * at that point bibma+0x2 et bibma+0xa are byte registers
93 c0 = inb(bibma + 0x02);
94 c1 = inb(bibma + 0x0a);
96 p += sprintf(p, "--------------- Primary Channel "
97 "---------------- Secondary Channel "
99 p += sprintf(p, " %sabled "
101 (c0 & 0x80) ? "dis" : " en",
102 (c1 & 0x80) ? "dis" : " en");
103 p += sprintf(p, "--------------- drive0 --------- drive1 "
104 "-------- drive0 ---------- drive1 ------\n");
105 p += sprintf(p, "DMA enabled: %s %s "
107 (c0 & 0x20) ? "yes" : "no ",
108 (c0 & 0x40) ? "yes" : "no ",
109 (c1 & 0x20) ? "yes" : "no ",
110 (c1 & 0x40) ? "yes" : "no " );
111 p += sprintf(p, "UDMA enabled: %s %s "
113 (udma_control & 0x01) ? "yes" : "no ",
114 (udma_control & 0x02) ? "yes" : "no ",
115 (udma_control & 0x04) ? "yes" : "no ",
116 (udma_control & 0x08) ? "yes" : "no " );
117 p += sprintf(p, "UDMA mode: %c %c "
119 (udma_control & 0x01) ?
120 ((udma_mode & 0x07) + 48) : 'X',
121 (udma_control & 0x02) ?
122 (((udma_mode >> 4) & 0x07) + 48) : 'X',
123 (udma_control & 0x04) ?
124 (((udma_mode >> 8) & 0x07) + 48) : 'X',
125 (udma_control & 0x08) ?
126 (((udma_mode >> 12) & 0x07) + 48) : 'X');
127 p += sprintf(p, "MDMA mode: %c %c "
129 (save_mdma_mode[0] && (c0 & 0x20)) ?
130 ((save_mdma_mode[0] & 0xf) + 48) : 'X',
131 (save_mdma_mode[1] && (c0 & 0x40)) ?
132 ((save_mdma_mode[1] & 0xf) + 48) : 'X',
133 (save_mdma_mode[2] && (c1 & 0x20)) ?
134 ((save_mdma_mode[2] & 0xf) + 48) : 'X',
135 (save_mdma_mode[3] && (c1 & 0x40)) ?
136 ((save_mdma_mode[3] & 0xf) + 48) : 'X');
137 p += sprintf(p, "PIO mode: %c %c "
139 (c0 & 0x20) ? 'X' : ((pio_mode & 0x07) + 48),
140 (c0 & 0x40) ? 'X' : (((pio_mode >> 4) & 0x07) + 48),
141 (c1 & 0x20) ? 'X' : (((pio_mode >> 8) & 0x07) + 48),
142 (c1 & 0x40) ? 'X' : (((pio_mode >> 12) & 0x07) + 48));
144 return p - buffer; /* => must be less than 4k! */
147 static ide_pci_host_proc_t atiixp_procs = {
150 .get_info = atiixp_get_info,
152 #endif /* defined(DISPLAY_ATIIXP_TIMINGS) && defined(CONFIG_PROC_FS) */
155 * atiixp_ratemask - compute rate mask for ATIIXP IDE
156 * @drive: IDE drive to compute for
158 * Returns the available modes for the ATIIXP IDE controller.
161 static u8 atiixp_ratemask(ide_drive_t *drive)
165 if (!eighty_ninty_three(drive))
166 mode = min(mode, (u8)1);
171 * atiixp_dma_2_pio - return the PIO mode matching DMA
172 * @xfer_rate: transfer speed
174 * Returns the nearest equivalent PIO timing for the PIO or DMA
175 * mode requested by the controller.
178 static u8 atiixp_dma_2_pio(u8 xfer_rate) {
207 static int atiixp_ide_dma_host_on(ide_drive_t *drive)
209 struct pci_dev *dev = drive->hwif->pci_dev;
213 spin_lock_irqsave(&ide_lock, flags);
215 pci_read_config_word(dev, ATIIXP_IDE_UDMA_CONTROL, &tmp16);
216 if (save_mdma_mode[drive->dn])
217 tmp16 &= ~(1 << drive->dn);
219 tmp16 |= (1 << drive->dn);
220 pci_write_config_word(dev, ATIIXP_IDE_UDMA_CONTROL, tmp16);
222 spin_unlock_irqrestore(&ide_lock, flags);
224 return __ide_dma_host_on(drive);
227 static int atiixp_ide_dma_host_off(ide_drive_t *drive)
229 struct pci_dev *dev = drive->hwif->pci_dev;
233 spin_lock_irqsave(&ide_lock, flags);
235 pci_read_config_word(dev, ATIIXP_IDE_UDMA_CONTROL, &tmp16);
236 tmp16 &= ~(1 << drive->dn);
237 pci_write_config_word(dev, ATIIXP_IDE_UDMA_CONTROL, tmp16);
239 spin_unlock_irqrestore(&ide_lock, flags);
241 return __ide_dma_host_off(drive);
245 * atiixp_tune_drive - tune a drive attached to a ATIIXP
246 * @drive: drive to tune
247 * @pio: desired PIO mode
249 * Set the interface PIO mode.
252 static void atiixp_tuneproc(ide_drive_t *drive, u8 pio)
254 struct pci_dev *dev = drive->hwif->pci_dev;
256 int timing_shift = (drive->dn & 2) ? 16 : 0 + (drive->dn & 1) ? 0 : 8;
260 spin_lock_irqsave(&ide_lock, flags);
262 pci_read_config_word(dev, ATIIXP_IDE_PIO_MODE, &pio_mode_data);
263 pio_mode_data &= ~(0x07 << (drive->dn * 4));
264 pio_mode_data |= (pio << (drive->dn * 4));
265 pci_write_config_word(dev, ATIIXP_IDE_PIO_MODE, pio_mode_data);
267 pci_read_config_dword(dev, ATIIXP_IDE_PIO_TIMING, &pio_timing_data);
268 pio_timing_data &= ~(0xff << timing_shift);
269 pio_timing_data |= (pio_timing[pio].recover_width << timing_shift) |
270 (pio_timing[pio].command_width << (timing_shift + 4));
271 pci_write_config_dword(dev, ATIIXP_IDE_PIO_TIMING, pio_timing_data);
273 spin_unlock_irqrestore(&ide_lock, flags);
277 * atiixp_tune_chipset - tune a ATIIXP interface
278 * @drive: IDE drive to tune
279 * @xferspeed: speed to configure
281 * Set a ATIIXP interface channel to the desired speeds. This involves
282 * requires the right timing data into the ATIIXP configuration space
283 * then setting the drive parameters appropriately
286 static int atiixp_speedproc(ide_drive_t *drive, u8 xferspeed)
288 struct pci_dev *dev = drive->hwif->pci_dev;
290 int timing_shift = (drive->dn & 2) ? 16 : 0 + (drive->dn & 1) ? 0 : 8;
295 speed = ide_rate_filter(atiixp_ratemask(drive), xferspeed);
297 spin_lock_irqsave(&ide_lock, flags);
299 save_mdma_mode[drive->dn] = 0;
300 if (speed >= XFER_UDMA_0) {
301 pci_read_config_word(dev, ATIIXP_IDE_UDMA_MODE, &tmp16);
302 tmp16 &= ~(0x07 << (drive->dn * 4));
303 tmp16 |= ((speed & 0x07) << (drive->dn * 4));
304 pci_write_config_word(dev, ATIIXP_IDE_UDMA_MODE, tmp16);
306 if ((speed >= XFER_MW_DMA_0) && (speed <= XFER_MW_DMA_2)) {
307 save_mdma_mode[drive->dn] = speed;
308 pci_read_config_dword(dev, ATIIXP_IDE_MDMA_TIMING, &tmp32);
309 tmp32 &= ~(0xff << timing_shift);
310 tmp32 |= (mdma_timing[speed & 0x03].recover_width << timing_shift) |
311 (mdma_timing[speed & 0x03].command_width << (timing_shift + 4));
312 pci_write_config_dword(dev, ATIIXP_IDE_MDMA_TIMING, tmp32);
316 spin_unlock_irqrestore(&ide_lock, flags);
318 if (speed >= XFER_SW_DMA_0)
319 pio = atiixp_dma_2_pio(speed);
321 pio = speed - XFER_PIO_0;
323 atiixp_tuneproc(drive, pio);
325 return ide_config_drive_speed(drive, speed);
329 * atiixp_config_drive_for_dma - configure drive for DMA
330 * @drive: IDE drive to configure
332 * Set up a ATIIXP interface channel for the best available speed.
333 * We prefer UDMA if it is available and then MWDMA. If DMA is
334 * not available we switch to PIO and return 0.
337 static int atiixp_config_drive_for_dma(ide_drive_t *drive)
339 u8 speed = ide_dma_speed(drive, atiixp_ratemask(drive));
341 /* If no DMA speed was available then disable DMA and use PIO. */
343 u8 tspeed = ide_get_best_pio_mode(drive, 255, 5, NULL);
344 speed = atiixp_dma_2_pio(XFER_PIO_0 + tspeed) + XFER_PIO_0;
347 (void) atiixp_speedproc(drive, speed);
348 return ide_dma_enable(drive);
352 * atiixp_dma_check - set up an IDE device
353 * @drive: IDE drive to configure
355 * Set up the ATIIXP interface for the best available speed on this
356 * interface, preferring DMA to PIO.
359 static int atiixp_dma_check(ide_drive_t *drive)
361 ide_hwif_t *hwif = HWIF(drive);
362 struct hd_driveid *id = drive->id;
365 drive->init_speed = 0;
367 if ((id->capability & 1) && drive->autodma) {
368 /* Consult the list of known "bad" drives */
369 if (__ide_dma_bad_drive(drive))
371 if (id->field_valid & 4) {
372 if (id->dma_ultra & hwif->ultra_mask) {
373 /* Force if Capable UltraDMA */
374 if ((id->field_valid & 2) &&
375 (!atiixp_config_drive_for_dma(drive)))
378 } else if (id->field_valid & 2) {
380 if ((id->dma_mword & hwif->mwdma_mask) ||
381 (id->dma_1word & hwif->swdma_mask)) {
382 /* Force if Capable regular DMA modes */
383 if (!atiixp_config_drive_for_dma(drive))
386 } else if (__ide_dma_good_drive(drive) &&
387 (id->eide_dma_time < 150)) {
388 /* Consult the list of known "good" drives */
389 if (!atiixp_config_drive_for_dma(drive))
394 return hwif->ide_dma_on(drive);
395 } else if ((id->capability & 8) || (id->field_valid & 2)) {
398 tspeed = ide_get_best_pio_mode(drive, 255, 5, NULL);
399 speed = atiixp_dma_2_pio(XFER_PIO_0 + tspeed) + XFER_PIO_0;
400 hwif->speedproc(drive, speed);
401 return hwif->ide_dma_off_quietly(drive);
403 /* IORDY not supported */
408 * init_chipset_atiixp - set up the ATIIXP chipset
409 * @dev: PCI device to set up
410 * @name: Name of the device
412 * Initialize the PCI device as required. For the ATIIXP this turns
413 * out to be nice and simple
416 static unsigned int __devinit init_chipset_atiixp(struct pci_dev *dev, const char *name)
418 #if defined(DISPLAY_ATIIXP_TIMINGS) && defined(CONFIG_PROC_FS)
422 ide_pci_register_host_proc(&atiixp_procs);
424 #endif /* DISPLAY_ATIIXP_TIMINGS && CONFIG_PROC_FS */
429 * init_hwif_atiixp - fill in the hwif for the ATIIXP
430 * @hwif: IDE interface
432 * Set up the ide_hwif_t for the ATIIXP interface according to the
433 * capabilities of the hardware.
436 static void __devinit init_hwif_atiixp(ide_hwif_t *hwif)
439 hwif->irq = hwif->channel ? 15 : 14;
442 hwif->tuneproc = &atiixp_tuneproc;
443 hwif->speedproc = &atiixp_speedproc;
444 hwif->drives[0].autotune = 1;
445 hwif->drives[1].autotune = 1;
451 hwif->ultra_mask = 0x3f;
452 hwif->mwdma_mask = 0x06;
453 hwif->swdma_mask = 0x04;
455 /* FIXME: proper cable detection needed */
457 hwif->ide_dma_host_on = &atiixp_ide_dma_host_on;
458 hwif->ide_dma_host_off = &atiixp_ide_dma_host_off;
459 hwif->ide_dma_check = &atiixp_dma_check;
463 hwif->drives[1].autodma = hwif->autodma;
464 hwif->drives[0].autodma = hwif->autodma;
467 static ide_pci_device_t atiixp_pci_info[] __devinitdata = {
469 .vendor = PCI_VENDOR_ID_ATI,
470 .device = PCI_DEVICE_ID_ATI_IXP200_IDE,
471 .name = "ATI-IXP200",
472 .init_chipset = init_chipset_atiixp,
473 .init_hwif = init_hwif_atiixp,
476 .enablebits = {{0x48,0x01,0x00}, {0x48,0x08,0x00}},
477 .bootable = ON_BOARD,
479 .vendor = PCI_VENDOR_ID_ATI,
480 .device = PCI_DEVICE_ID_ATI_IXP300_IDE,
481 .name = "ATI-IXP300",
482 .init_chipset = init_chipset_atiixp,
483 .init_hwif = init_hwif_atiixp,
486 .enablebits = {{0x48,0x01,0x00}, {0x48,0x08,0x00}},
487 .bootable = ON_BOARD,
489 .vendor = PCI_VENDOR_ID_ATI,
490 .device = PCI_DEVICE_ID_ATI_IXP400_IDE,
491 .name = "ATI-IXP400",
492 .init_chipset = init_chipset_atiixp,
493 .init_hwif = init_hwif_atiixp,
496 .enablebits = {{0x48,0x01,0x00}, {0x48,0x08,0x00}},
497 .bootable = ON_BOARD,
502 * atiixp_init_one - called when a ATIIXP is found
503 * @dev: the atiixp device
504 * @id: the matching pci id
506 * Called when the PCI registration layer (or the IDE initialization)
507 * finds a device matching our IDE device tables.
510 static int __devinit atiixp_init_one(struct pci_dev *dev, const struct pci_device_id *id)
512 ide_pci_device_t *d = &atiixp_pci_info[id->driver_data];
514 if (dev->device != d->device)
516 ide_setup_pci_device(dev, d);
520 static struct pci_device_id atiixp_pci_tbl[] = {
521 { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_IXP200_IDE, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
522 { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_IXP300_IDE, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 1},
523 { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_IXP400_IDE, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 2},
527 static struct pci_driver driver = {
528 .name = "ATIIXP IDE",
529 .id_table = atiixp_pci_tbl,
530 .probe = atiixp_init_one,
533 static int atiixp_ide_init(void)
535 return ide_pci_register_driver(&driver);
538 module_init(atiixp_ide_init);
540 MODULE_AUTHOR("HUI YU");
541 MODULE_DESCRIPTION("PCI driver module for ATI IXP IDE");
542 MODULE_LICENSE("GPL");