2 * linux/drivers/block/ide-pci.c Version 1.02 December 29, 1997
4 * Copyright (c) 1995-1998 Mark Lord
5 * May be copied or modified under the terms of the GNU General Public License
9 * This module provides support for automatic detection and
10 * configuration of all PCI IDE interfaces present in a system.
13 #include <linux/config.h>
14 #include <linux/types.h>
15 #include <linux/kernel.h>
16 #include <linux/timer.h>
18 #include <linux/interrupt.h>
19 #include <linux/pci.h>
20 #include <linux/bios32.h>
27 #define DEVID_PIIX ((ide_pci_devid_t){PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371FB_1})
28 #define DEVID_PIIX3 ((ide_pci_devid_t){PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371SB_1})
29 #define DEVID_PIIX4 ((ide_pci_devid_t){PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371AB})
30 #define DEVID_VP_IDE ((ide_pci_devid_t){PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_82C586_1})
31 #define DEVID_PDC20246 ((ide_pci_devid_t){PCI_VENDOR_ID_PROMISE, PCI_DEVICE_ID_PROMISE_20246})
32 #define DEVID_RZ1000 ((ide_pci_devid_t){PCI_VENDOR_ID_PCTECH, PCI_DEVICE_ID_PCTECH_RZ1000})
33 #define DEVID_RZ1001 ((ide_pci_devid_t){PCI_VENDOR_ID_PCTECH, PCI_DEVICE_ID_PCTECH_RZ1001})
34 #define DEVID_CMD640 ((ide_pci_devid_t){PCI_VENDOR_ID_CMD, PCI_DEVICE_ID_CMD_640})
35 #define DEVID_CMD646 ((ide_pci_devid_t){PCI_VENDOR_ID_CMD, PCI_DEVICE_ID_CMD_646})
36 #define DEVID_SIS5513 ((ide_pci_devid_t){PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_5513})
37 #define DEVID_OPTI621 ((ide_pci_devid_t){PCI_VENDOR_ID_OPTI, PCI_DEVICE_ID_OPTI_82C621})
38 #define DEVID_OPTI621V ((ide_pci_devid_t){PCI_VENDOR_ID_OPTI, PCI_DEVICE_ID_OPTI_82C558})
39 #define DEVID_OPTI621X ((ide_pci_devid_t){PCI_VENDOR_ID_OPTI, 0xd568}) /* from datasheets */
40 #define DEVID_TRM290 ((ide_pci_devid_t){PCI_VENDOR_ID_TEKRAM, PCI_DEVICE_ID_TEKRAM_DC290})
41 #define DEVID_NS87410 ((ide_pci_devid_t){PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_87410})
42 #define DEVID_NS87415 ((ide_pci_devid_t){PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_87415})
43 #define DEVID_HT6565 ((ide_pci_devid_t){PCI_VENDOR_ID_HOLTEK, PCI_DEVICE_ID_HOLTEK_6565})
44 #define DEVID_AEC6210 ((ide_pci_devid_t){0x1191, 0x0005})
46 #define IDE_IGNORE ((void *)-1)
48 #ifdef CONFIG_BLK_DEV_TRM290
49 extern void ide_init_trm290(ide_hwif_t
*);
50 #define INIT_TRM290 &ide_init_trm290
52 #define INIT_TRM290 IDE_IGNORE
55 #ifdef CONFIG_BLK_DEV_OPTI621
56 extern void ide_init_opti621(ide_hwif_t
*);
57 #define INIT_OPTI621 &ide_init_opti621
59 #define INIT_OPTI621 NULL
62 #ifdef CONFIG_BLK_DEV_NS87415
63 extern void ide_init_ns87415(ide_hwif_t
*);
64 #define INIT_NS87415 &ide_init_ns87415
66 #define INIT_NS87415 IDE_IGNORE
69 #ifdef CONFIG_BLK_DEV_RZ1000
70 extern void ide_init_rz1000(ide_hwif_t
*);
71 #define INIT_RZ1000 &ide_init_rz1000
73 #define INIT_RZ1000 IDE_IGNORE
76 typedef struct ide_pci_enablebit_s
{
77 byte reg
; /* byte pci reg holding the enable-bit */
78 byte mask
; /* mask to isolate the enable-bit */
79 byte val
; /* value of masked reg when "enabled" */
80 } ide_pci_enablebit_t
;
82 typedef struct ide_pci_device_s
{
83 ide_pci_devid_t devid
;
85 void (*init_hwif
)(ide_hwif_t
*hwif
);
86 ide_pci_enablebit_t enablebits
[2];
89 static ide_pci_device_t ide_pci_chipsets
[] __initdata
= {
90 {DEVID_PIIX
, "PIIX", NULL
, {{0x41,0x80,0x80}, {0x43,0x80,0x80}} },
91 {DEVID_PIIX3
, "PIIX3", NULL
, {{0x41,0x80,0x80}, {0x43,0x80,0x80}} },
92 {DEVID_PIIX4
, "PIIX4", NULL
, {{0x41,0x80,0x80}, {0x43,0x80,0x80}} },
93 {DEVID_VP_IDE
, "VP_IDE", NULL
, {{0x40,0x02,0x02}, {0x40,0x01,0x01}} },
94 {DEVID_PDC20246
,"PDC20246", NULL
, {{0x50,0x02,0x02}, {0x50,0x04,0x04}} },
95 {DEVID_RZ1000
, "RZ1000", INIT_RZ1000
, {{0x00,0x00,0x00}, {0x00,0x00,0x00}} },
96 {DEVID_RZ1001
, "RZ1001", INIT_RZ1000
, {{0x00,0x00,0x00}, {0x00,0x00,0x00}} },
97 {DEVID_CMD640
, "CMD640", IDE_IGNORE
, {{0x00,0x00,0x00}, {0x00,0x00,0x00}} },
98 {DEVID_NS87410
, "NS87410", NULL
, {{0x43,0x08,0x08}, {0x47,0x08,0x08}} },
99 {DEVID_SIS5513
, "SIS5513", NULL
, {{0x4a,0x02,0x02}, {0x4a,0x04,0x04}} },
100 {DEVID_CMD646
, "CMD646", NULL
, {{0x00,0x00,0x00}, {0x51,0x80,0x80}} },
101 {DEVID_HT6565
, "HT6565", NULL
, {{0x00,0x00,0x00}, {0x00,0x00,0x00}} },
102 {DEVID_OPTI621
, "OPTI621", INIT_OPTI621
, {{0x45,0x80,0x00}, {0x40,0x08,0x00}} },
103 {DEVID_OPTI621X
,"OPTI621X", INIT_OPTI621
, {{0x45,0x80,0x00}, {0x40,0x08,0x00}} },
104 {DEVID_TRM290
, "TRM290", INIT_TRM290
, {{0x00,0x00,0x00}, {0x00,0x00,0x00}} },
105 {DEVID_NS87415
, "NS87415", INIT_NS87415
, {{0x00,0x00,0x00}, {0x00,0x00,0x00}} },
106 {DEVID_AEC6210
, "AEC6210", NULL
, {{0x00,0x00,0x00}, {0x00,0x00,0x00}} },
107 {IDE_PCI_DEVID_NULL
, "PCI_IDE", NULL
, {{0x00,0x00,0x00}, {0x00,0x00,0x00}} }};
110 * Search for an (apparently) unused block of I/O space
111 * of "size" bytes in length. Ideally we ought to do a pass
112 * through pcicfg space to eliminate ports already allocated
113 * by the BIOS, to avoid conflicts later in the init cycle,
114 * but we don't. FIXME
116 unsigned long ide_find_free_region (unsigned short size
) /* __init */
118 static unsigned short base
= 0x5800; /* it works for me */
121 for (; base
> 0; base
-= 0x200) {
122 if (!check_region(base
,size
)) {
123 for (i
= 0; i
< size
; i
++) {
124 if (inb(base
+i
) != 0xff)
127 return base
; /* success */
131 return 0; /* failure */
135 * Match a PCI IDE port against an entry in ide_hwifs[],
136 * based on io_base port if possible.
138 __initfunc(static ide_hwif_t
*ide_match_hwif (unsigned int io_base
, const char *name
))
144 * Look for a hwif with matching io_base specified using
145 * parameters to ide_setup().
147 for (h
= 0; h
< MAX_HWIFS
; ++h
) {
148 hwif
= &ide_hwifs
[h
];
149 if (hwif
->io_ports
[IDE_DATA_OFFSET
] == io_base
) {
150 if (hwif
->chipset
== ide_generic
)
151 return hwif
; /* a perfect match */
155 * Look for a hwif with matching io_base default value.
156 * If chipset is "ide_unknown", then claim that hwif slot.
157 * Otherwise, some other chipset has already claimed it.. :(
159 for (h
= 0; h
< MAX_HWIFS
; ++h
) {
160 hwif
= &ide_hwifs
[h
];
161 if (hwif
->io_ports
[IDE_DATA_OFFSET
] == io_base
) {
162 if (hwif
->chipset
== ide_unknown
)
163 return hwif
; /* match */
164 printk("%s: port 0x%04x already claimed by %s\n", name
, io_base
, hwif
->name
);
165 return NULL
; /* already claimed */
169 * Okay, there is no hwif matching our io_base,
170 * so we'll just claim an unassigned slot.
171 * Give preference to claiming other slots before claiming ide0/ide1,
172 * just in case there's another interface yet-to-be-scanned
173 * which uses ports 1f0/170 (the ide0/ide1 defaults).
175 for (h
= 0; h
< MAX_HWIFS
; ++h
) {
176 int hwifs
[] = {2,3,1,0}; /* assign 3rd/4th before 1st/2nd */
177 hwif
= &ide_hwifs
[hwifs
[h
]];
178 if (hwif
->chipset
== ide_unknown
)
179 return hwif
; /* pick an unused entry */
181 printk("%s: too many IDE interfaces, no room in table\n", name
);
185 __initfunc(static int ide_setup_pci_baseregs (byte bus
, byte fn
, const char *name
))
187 unsigned int base
, readback
;
188 byte reg
, progif
= 0;
191 * Place both IDE interfaces into PCI "native" mode:
193 if (pcibios_read_config_byte(bus
, fn
, 0x09, &progif
) || (progif
& 5) != 5) {
194 if ((progif
& 0xa) != 0xa) {
195 printk("%s: device not capable of full native PCI mode\n", name
);
198 printk("%s: placing both ports into native PCI mode\n", name
);
199 (void) pcibios_write_config_byte(bus
, fn
, 0x09, progif
|5);
200 if (pcibios_read_config_byte(bus
, fn
, 0x09, &progif
) || (progif
& 5) != 5) {
201 printk("%s: rewrite of PROGIF failed, wanted 0x%04x, got 0x%04x\n", name
, progif
|5, progif
);
206 * Setup base registers for IDE command/control spaces for each interface:
208 if (!(base
= ide_find_free_region(32)))
210 for (reg
= 0x10; reg
<= 0x1c; reg
+= 4, base
+= 8) {
211 (void) pcibios_write_config_dword(bus
, fn
, reg
, base
|1);
212 if (pcibios_read_config_dword(bus
, fn
, reg
, &readback
) || (readback
&= ~1) != base
) {
213 printk("%s: readback failed for basereg 0x%02x: wrote 0x%04x, read 0x%x04\n", name
, reg
, base
, readback
);
221 * ide_setup_pci_device() looks at the primary/secondary interfaces
222 * on a PCI IDE device and, if they are enabled, prepares the IDE driver
223 * for use with them. This generic code works for most PCI chipsets.
225 * One thing that is not standardized is the location of the
226 * primary/secondary interface "enable/disable" bits. For chipsets that
227 * we "know" about, this information is in the ide_pci_device_t struct;
228 * for all other chipsets, we just assume both interfaces are enabled.
230 __initfunc(static void ide_setup_pci_device (byte bus
, byte fn
, unsigned int ccode
, ide_pci_device_t
*d
))
232 unsigned int port
, at_least_one_hwif_enabled
= 0, no_autodma
= 0;
233 unsigned short pcicmd
= 0, tried_config
= 0;
234 byte tmp
= 0, progif
= 0, pciirq
= 0;
235 ide_hwif_t
*hwif
, *mate
= NULL
;
238 if (pcibios_read_config_word(bus
, fn
, 0x04, &pcicmd
)
239 || pcibios_read_config_byte(bus
, fn
, 0x09, &progif
)
240 || pcibios_read_config_byte(bus
, fn
, 0x3c, &pciirq
))
242 printk("%s: error accessing PCI regs\n", d
->name
);
245 if (!(pcicmd
& 1)) { /* is device disabled? */
247 * PnP BIOS was *supposed* to have set this device up for us,
248 * but we can do it ourselves, so long as the BIOS has assigned an IRQ
249 * (or possibly the device is using a "legacy header" for IRQs).
250 * Maybe the user deliberately *disabled* the device,
251 * but we'll eventually ignore it again if no drives respond.
254 || ide_setup_pci_baseregs(bus
, fn
, d
->name
)
255 || pcibios_write_config_word(bus
, fn
, 0x04, pcicmd
|1))
257 printk("%s: device disabled (BIOS)\n", d
->name
);
260 no_autodma
= 1; /* default DMA off if we had to configure it here */
261 goto check_if_enabled
;
264 printk("%s: device enabled (Linux)\n", d
->name
);
266 * Can we trust the reported IRQ?
268 if ((ccode
>> 16) != PCI_CLASS_STORAGE_IDE
|| (progif
& 5) != 5) {
269 printk("%s: not 100%% native mode: will probe irqs later\n", d
->name
);
271 } else if (tried_config
) {
272 printk("%s: will probe irqs later\n", d
->name
);
274 } else if (!pciirq
|| pciirq
>= NR_IRQS
) {
275 printk("%s: bad irq from BIOS (%d): will probe later\n", d
->name
, pciirq
);
278 printk("%s: 100%% native mode on irq %d\n", d
->name
, pciirq
);
281 * Set up the IDE ports
283 for (port
= 0; port
<= 1; ++port
) {
284 unsigned int base
= 0, ctl
= 0;
285 ide_pci_enablebit_t
*e
= &(d
->enablebits
[port
]);
286 if (e
->reg
&& (pcibios_read_config_byte(bus
, fn
, e
->reg
, &tmp
) || (tmp
& e
->mask
) != e
->val
))
287 continue; /* port not enabled */
288 if (pcibios_read_config_dword(bus
, fn
, 0x14+(port
*8), &ctl
) || (ctl
&= ~3) == 0)
289 ctl
= port
? 0x374 : 0x3f4; /* use default value */
290 if (pcibios_read_config_dword(bus
, fn
, 0x10+(port
*8), &base
) || (base
&= ~7) == 0)
291 base
= port
? 0x170 : 0x1f0; /* use default value */
292 if ((hwif
= ide_match_hwif(base
, d
->name
)) == NULL
)
293 continue; /* no room in ide_hwifs[] */
294 if (hwif
->io_ports
[IDE_DATA_OFFSET
] != base
) {
295 ide_init_hwif_ports(hwif
->io_ports
, base
, NULL
);
296 hwif
->io_ports
[IDE_CONTROL_OFFSET
] = ctl
+ 2;
298 hwif
->chipset
= ide_pci
;
301 hwif
->pci_devid
= d
->devid
;
302 hwif
->channel
= port
;
310 hwif
->no_autodma
= 1;
311 #ifdef CONFIG_BLK_DEV_IDEDMA
312 if (IDE_PCI_DEVID_EQ(d
->devid
, DEVID_PDC20246
) || ((ccode
>> 16) == PCI_CLASS_STORAGE_IDE
&& (ccode
& 0x8000))) {
313 unsigned int extra
= (!mate
&& IDE_PCI_DEVID_EQ(d
->devid
, DEVID_PDC20246
)) ? 16 : 0;
314 unsigned long dma_base
= ide_get_or_set_dma_base(hwif
, extra
, d
->name
);
315 if (dma_base
&& !(pcicmd
& 4)) {
317 * Set up BM-DMA capability (PnP BIOS should have done this)
319 hwif
->no_autodma
= 1; /* default DMA off if we had to configure it here */
320 (void) pcibios_write_config_word(bus
, fn
, 0x04, (pcicmd
|4));
321 if (pcibios_read_config_word(bus
, fn
, 0x04, &pcicmd
) || !(pcicmd
& 4)) {
322 printk("%s: %s error updating PCICMD\n", hwif
->name
, d
->name
);
327 ide_setup_dma(hwif
, dma_base
, 8);
329 printk("%s: %s Bus-Master DMA disabled (BIOS), pcicmd=0x%04x, ccode=0x%04x, dma_base=0x%04lx\n",
330 hwif
->name
, d
->name
, pcicmd
, ccode
, dma_base
);
332 #endif /* CONFIG_BLK_DEV_IDEDMA */
333 if (d
->init_hwif
) /* Call chipset-specific routine for each enabled hwif */
336 at_least_one_hwif_enabled
= 1;
338 if (!at_least_one_hwif_enabled
)
339 printk("%s: neither IDE port enabled (BIOS)\n", d
->name
);
343 * ide_scan_pci_device() examines all functions of a PCI device,
344 * looking for IDE interfaces and/or devices in ide_pci_chipsets[].
345 * We cannot use pcibios_find_class() cuz it doesn't work in all systems.
347 static inline void ide_scan_pci_device (unsigned int bus
, unsigned int fn
)
350 ide_pci_devid_t devid
;
354 if (pcibios_read_config_byte(bus
, fn
, 0x0e, &hedt
))
357 if (pcibios_read_config_word(bus
, fn
, 0x00, &devid
.vid
)
358 || devid
.vid
== 0xffff
359 || pcibios_read_config_word(bus
, fn
, 0x02, &devid
.did
)
360 || IDE_PCI_DEVID_EQ(devid
, IDE_PCI_DEVID_NULL
)
361 || pcibios_read_config_dword(bus
, fn
, 0x08, &ccode
))
363 for (d
= ide_pci_chipsets
; d
->devid
.vid
&& !IDE_PCI_DEVID_EQ(d
->devid
, devid
); ++d
);
364 if (d
->init_hwif
== IDE_IGNORE
)
365 printk("%s: ignored by ide_scan_pci_device() (uses own driver)\n", d
->name
);
366 else if (IDE_PCI_DEVID_EQ(d
->devid
, DEVID_OPTI621V
) && !(fn
& 1))
367 continue; /* OPTI Viper-M uses same devid for functions 0 and 1 */
368 else if (!IDE_PCI_DEVID_EQ(d
->devid
, IDE_PCI_DEVID_NULL
) || (ccode
>> 16) == PCI_CLASS_STORAGE_IDE
) {
369 if (IDE_PCI_DEVID_EQ(d
->devid
, IDE_PCI_DEVID_NULL
))
370 printk("%s: unknown IDE controller on PCI bus %d function %d, VID=%04x, DID=%04x\n",
371 d
->name
, bus
, fn
, devid
.vid
, devid
.did
);
373 printk("%s: IDE controller on PCI bus %d function %d\n", d
->name
, bus
, fn
);
374 ide_setup_pci_device(bus
, fn
, ccode
, d
);
376 } while (hedt
== 0x80 && (++fn
& 7));
380 * ide_scan_pcibus() gets invoked at boot time from ide.c
382 * Loops over all PCI devices on all PCI buses, invoking ide_scan_pci_device().
383 * We cannot use pcibios_find_class() cuz it doesn't work in all systems.
385 void ide_scan_pcibus (void) /* __init */
387 unsigned int bus
, dev
;
389 if (!pcibios_present())
391 for (bus
= 0; bus
<= 255; ++bus
) {
392 for (dev
= 0; dev
< 256; dev
+= 8) {
393 ide_scan_pci_device(bus
, dev
);