Sync pci_cfgreg.c with FreeBSD 5, rev. 1.101. This makes the PCI interrupt
[dragonfly.git] / sys / bus / pci / i386 / pcibus.c
1 /*
2  * Copyright (c) 1997, Stefan Esser <se@freebsd.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice unmodified, this list of conditions, and the following
10  *    disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  * $FreeBSD: src/sys/i386/isa/pcibus.c,v 1.57.2.11 2002/11/13 21:40:40 peter Exp $
27  * $DragonFly: src/sys/bus/pci/i386/pcibus.c,v 1.4 2004/01/15 08:05:41 joerg Exp $
28  *
29  */
30
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/bus.h>
34 #include <sys/kernel.h>
35
36 #include <bus/pci/pcivar.h>
37 #include <bus/pci/pcireg.h>
38 #include "pcibus.h"
39 #include <bus/pci/i386/pci_cfgreg.h>
40 #include <machine/md_var.h>
41
42 #include "opt_cpu.h"
43
44 #include "pci_if.h"
45
46 static devclass_t       pcib_devclass;
47
48 static const char *
49 nexus_pcib_is_host_bridge(pcicfgregs *cfg,
50                           u_int32_t id, u_int8_t class, u_int8_t subclass,
51                           u_int8_t *busnum)
52 {
53         const char *s = NULL;
54         static u_int8_t pxb[4]; /* hack for 450nx */
55
56         *busnum = 0;
57
58         switch (id) {
59         case 0x12258086:
60                 s = "Intel 824?? host to PCI bridge";
61                 /* XXX This is a guess */
62                 /* *busnum = pci_cfgread(cfg, 0x41, 1); */
63                 *busnum = cfg->bus;
64                 break;
65         case 0x71208086:
66                 s = "Intel 82810 (i810 GMCH) Host To Hub bridge";
67                 break;
68         case 0x71228086:
69                 s = "Intel 82810-DC100 (i810-DC100 GMCH) Host To Hub bridge";
70                 break;
71         case 0x71248086:
72                 s = "Intel 82810E (i810E GMCH) Host To Hub bridge";
73                 break;
74         case 0x71808086:
75                 s = "Intel 82443LX (440 LX) host to PCI bridge";
76                 break;
77         case 0x71908086:
78                 s = "Intel 82443BX (440 BX) host to PCI bridge";
79                 break;
80         case 0x71928086:
81                 s = "Intel 82443BX host to PCI bridge (AGP disabled)";
82                 break;
83         case 0x71948086:
84                 s = "Intel 82443MX host to PCI bridge";
85                 break;
86         case 0x71a08086:
87                 s = "Intel 82443GX host to PCI bridge";
88                 break;
89         case 0x71a18086:
90                 s = "Intel 82443GX host to AGP bridge";
91                 break;
92         case 0x71a28086:
93                 s = "Intel 82443GX host to PCI bridge (AGP disabled)";
94                 break;
95         case 0x84c48086:
96                 s = "Intel 82454KX/GX (Orion) host to PCI bridge";
97                 *busnum = pci_cfgread(cfg, 0x4a, 1);
98                 break;
99         case 0x84ca8086:
100                 /*
101                  * For the 450nx chipset, there is a whole bundle of
102                  * things pretending to be host bridges. The MIOC will 
103                  * be seen first and isn't really a pci bridge (the
104                  * actual busses are attached to the PXB's). We need to 
105                  * read the registers of the MIOC to figure out the
106                  * bus numbers for the PXB channels.
107                  *
108                  * Since the MIOC doesn't have a pci bus attached, we
109                  * pretend it wasn't there.
110                  */
111                 pxb[0] = pci_cfgread(cfg, 0xd0, 1); /* BUSNO[0] */
112                 pxb[1] = pci_cfgread(cfg, 0xd1, 1) + 1; /* SUBA[0]+1 */
113                 pxb[2] = pci_cfgread(cfg, 0xd3, 1); /* BUSNO[1] */
114                 pxb[3] = pci_cfgread(cfg, 0xd4, 1) + 1; /* SUBA[1]+1 */
115                 return NULL;
116         case 0x84cb8086:
117                 switch (cfg->slot) {
118                 case 0x12:
119                         s = "Intel 82454NX PXB#0, Bus#A";
120                         *busnum = pxb[0];
121                         break;
122                 case 0x13:
123                         s = "Intel 82454NX PXB#0, Bus#B";
124                         *busnum = pxb[1];
125                         break;
126                 case 0x14:
127                         s = "Intel 82454NX PXB#1, Bus#A";
128                         *busnum = pxb[2];
129                         break;
130                 case 0x15:
131                         s = "Intel 82454NX PXB#1, Bus#B";
132                         *busnum = pxb[3];
133                         break;
134                 }
135                 break;
136         case 0x1A308086:
137                 s = "Intel 82845 Host to PCI bridge";
138                 break;
139
140                 /* AMD -- vendor 0x1022 */
141         case 0x30001022:
142                 s = "AMD Elan SC520 host to PCI bridge";
143 #ifdef CPU_ELAN
144                 init_AMD_Elan_sc520();
145 #else
146                 printf("*** WARNING: kernel option CPU_ELAN missing");
147                 printf("-- timekeeping may be wrong\n");
148 #endif
149                 break;
150         case 0x70061022:
151                 s = "AMD-751 host to PCI bridge";
152                 break;
153         case 0x700e1022:
154                 s = "AMD-761 host to PCI bridge";
155                 break;
156
157                 /* SiS -- vendor 0x1039 */
158         case 0x04961039:
159                 s = "SiS 85c496";
160                 break;
161         case 0x04061039:
162                 s = "SiS 85c501";
163                 break;
164         case 0x06011039:
165                 s = "SiS 85c601";
166                 break;
167         case 0x55911039:
168                 s = "SiS 5591 host to PCI bridge";
169                 break;
170         case 0x00011039:
171                 s = "SiS 5591 host to AGP bridge";
172                 break;
173
174                 /* VLSI -- vendor 0x1004 */
175         case 0x00051004:
176                 s = "VLSI 82C592 Host to PCI bridge";
177                 break;
178
179                 /* XXX Here is MVP3, I got the datasheet but NO M/B to test it  */
180                 /* totally. Please let me know if anything wrong.            -F */
181                 /* XXX need info on the MVP3 -- any takers? */
182         case 0x05981106:
183                 s = "VIA 82C598MVP (Apollo MVP3) host bridge";
184                 break;
185
186                 /* AcerLabs -- vendor 0x10b9 */
187                 /* Funny : The datasheet told me vendor id is "10b8",sub-vendor */
188                 /* id is '10b9" but the register always shows "10b9". -Foxfair  */
189         case 0x154110b9:
190                 s = "AcerLabs M1541 (Aladdin-V) PCI host bridge";
191                 break;
192
193                 /* OPTi -- vendor 0x1045 */
194         case 0xc8221045:
195                 s = "OPTi 82C822 host to PCI Bridge";
196                 break;
197
198                 /* ServerWorks -- vendor 0x1166 */
199         case 0x00051166:
200                 s = "ServerWorks NB6536 2.0HE host to PCI bridge";
201                 *busnum = pci_cfgread(cfg, 0x44, 1);
202                 break;
203         
204         case 0x00061166:
205                 /* FALLTHROUGH */
206         case 0x00081166:
207                 s = "ServerWorks host to PCI bridge";
208                 *busnum = pci_cfgread(cfg, 0x44, 1);
209                 break;
210
211         case 0x00091166:
212                 s = "ServerWorks NB6635 3.0LE host to PCI bridge";
213                 *busnum = pci_cfgread(cfg, 0x44, 1);
214                 break;
215
216         case 0x00101166:
217                 s = "ServerWorks CIOB30 host to PCI bridge";
218                 *busnum = pci_cfgread(cfg, 0x44, 1);
219                 break;
220
221                 /* XXX unknown chipset, but working */
222         case 0x00171166:
223                 /* FALLTHROUGH */
224         case 0x01011166:
225                 s = "ServerWorks host to PCI bridge(unknown chipset)";
226                 *busnum = pci_cfgread(cfg, 0x44, 1);
227                 break;
228
229                 /* Integrated Micro Solutions -- vendor 0x10e0 */
230         case 0x884910e0:
231                 s = "Integrated Micro Solutions VL Bridge";
232                 break;
233
234         default:
235                 if (class == PCIC_BRIDGE && subclass == PCIS_BRIDGE_HOST)
236                         s = "Host to PCI bridge";
237                 break;
238         }
239
240         return s;
241 }
242
243 /*
244  * Scan the first pci bus for host-pci bridges and add pcib instances
245  * to the nexus for each bridge.
246  */
247 static void
248 nexus_pcib_identify(driver_t *driver, device_t parent)
249 {
250         pcicfgregs probe;
251         u_int8_t  hdrtype;
252         int found = 0;
253         int pcifunchigh;
254         int found824xx = 0;
255         int found_orion = 0;
256
257         if (pci_cfgregopen() == 0)
258                 return;
259         probe.hose = 0;
260         probe.bus = 0;
261  retry:
262         for (probe.slot = 0; probe.slot <= PCI_SLOTMAX; probe.slot++) {
263                 probe.func = 0;
264                 hdrtype = pci_cfgread(&probe, PCIR_HEADERTYPE, 1);
265                 if (hdrtype & PCIM_MFDEV && (!found_orion || hdrtype != 0xff) )
266                         pcifunchigh = 7;
267                 else
268                         pcifunchigh = 0;
269                 for (probe.func = 0;
270                      probe.func <= pcifunchigh;
271                      probe.func++) {
272                         /*
273                          * Read the IDs and class from the device.
274                          */
275                         u_int32_t id;
276                         u_int8_t class, subclass, busnum;
277                         device_t child;
278                         const char *s;
279
280                         id = pci_cfgread(&probe, PCIR_DEVVENDOR, 4);
281                         if (id == -1)
282                                 continue;
283                         class = pci_cfgread(&probe, PCIR_CLASS, 1);
284                         subclass = pci_cfgread(&probe, PCIR_SUBCLASS, 1);
285
286                         s = nexus_pcib_is_host_bridge(&probe, id,
287                                                       class, subclass,
288                                                       &busnum);
289                         if (s) {
290                                 /*
291                                  * Add at priority 100 to make sure we
292                                  * go after any motherboard resources
293                                  */
294                                 child = BUS_ADD_CHILD(parent, 100,
295                                                       "pcib", busnum);
296                                 device_set_desc(child, s);
297                                 found = 1;
298                                 if (id == 0x12258086)
299                                         found824xx = 1;
300                                 if (id == 0x84c48086)
301                                         found_orion = 1;
302                         }
303                 }
304         }
305         if (found824xx && probe.bus == 0) {
306                 probe.bus++;
307                 goto retry;
308         }
309
310         /*
311          * Make sure we add at least one bridge since some old
312          * hardware doesn't actually have a host-pci bridge device.
313          * Note that pci_cfgregopen() thinks we have PCI devices..
314          */
315         if (!found) {
316                 if (bootverbose)
317                         printf(
318         "nexus_pcib_identify: no bridge found, adding pcib0 anyway\n");
319                 BUS_ADD_CHILD(parent, 100, "pcib", 0);
320         }
321 }
322
323 static int
324 nexus_pcib_probe(device_t dev)
325 {
326         if (pci_cfgregopen() != 0) {
327                 device_add_child(dev, "pci", device_get_unit(dev));
328                 return 0;
329         }
330         return ENXIO;
331 }
332
333 /* route interrupt */
334
335 static int
336 nexus_pcib_route_interrupt(device_t pcib, device_t dev, int pin)
337 {
338         return(pci_cfgintr(pci_get_bus(dev), pci_get_slot(dev), pin, 
339                            pci_get_irq(dev)));
340 }
341
342 static device_method_t nexus_pcib_methods[] = {
343         /* Device interface */
344         DEVMETHOD(device_identify,      nexus_pcib_identify),
345         DEVMETHOD(device_probe,         nexus_pcib_probe),
346         DEVMETHOD(device_attach,        bus_generic_attach),
347         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
348         DEVMETHOD(device_suspend,       bus_generic_suspend),
349         DEVMETHOD(device_resume,        bus_generic_resume),
350
351         /* Bus interface */
352         DEVMETHOD(bus_print_child,      bus_generic_print_child),
353         DEVMETHOD(bus_alloc_resource,   bus_generic_alloc_resource),
354         DEVMETHOD(bus_release_resource, bus_generic_release_resource),
355         DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
356         DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
357         DEVMETHOD(bus_setup_intr,       bus_generic_setup_intr),
358         DEVMETHOD(bus_teardown_intr,    bus_generic_teardown_intr),
359
360         /* pci interface */
361         DEVMETHOD(pci_route_interrupt,  nexus_pcib_route_interrupt),
362         { 0, 0 }
363 };
364
365 static driver_t nexus_pcib_driver = {
366         "pcib",
367         nexus_pcib_methods,
368         1,
369 };
370
371 DRIVER_MODULE(pcib, nexus, nexus_pcib_driver, pcib_devclass, 0, 0);