Use nexus_pcib_read_config instead of pci_cfgread. Do some cleanup.
[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.6 2004/01/15 20:41:57 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 #include <sys/malloc.h>
36
37 #include <bus/pci/pcivar.h>
38 #include <bus/pci/pcireg.h>
39 #include <bus/pci/i386/pcibus.h>
40 #include <bus/isa/isavar.h>
41 #include <bus/pci/i386/pci_cfgreg.h>
42 #include <machine/md_var.h>
43 #include <machine/nexusvar.h>
44
45 #include "pcib_if.h"
46
47 static int
48 nexus_pcib_maxslots(device_t dev)
49 {
50         return 31;
51 }
52
53 /*
54  * Read configuration space register.
55  */
56 static u_int32_t
57 nexus_pcib_read_config(device_t dev, int bus, int slot, int func,
58                        int reg, int bytes)
59 {
60         return (pci_cfgregread(bus, slot, func, reg, bytes));
61 }
62
63 static void
64 nexus_pcib_write_config(device_t dev, int bus, int slot, int func,
65                         int reg, u_int32_t data, int bytes)
66 {
67         pci_cfgregwrite(bus, slot, func, reg, data, bytes);
68 }
69
70 static devclass_t       pcib_devclass;
71
72 static const char *
73 nexus_pcib_is_host_bridge(int bus, int slot, int func,
74                           u_int32_t id, u_int8_t class, u_int8_t subclass,
75                           u_int8_t *busnum)
76 {
77         const char *s = NULL;
78         static u_int8_t pxb[4]; /* hack for 450nx */
79
80         *busnum = 0;
81
82         switch (id) {
83         case 0x12258086:
84                 s = "Intel 824?? host to PCI bridge";
85                 /* XXX This is a guess */
86                 /* *busnum = nexus_pcib_read_config(0, bus, slot, func, 0x41, 1); */
87                 *busnum = bus;
88                 break;
89         case 0x71208086:
90                 s = "Intel 82810 (i810 GMCH) Host To Hub bridge";
91                 break;
92         case 0x71228086:
93                 s = "Intel 82810-DC100 (i810-DC100 GMCH) Host To Hub bridge";
94                 break;
95         case 0x71248086:
96                 s = "Intel 82810E (i810E GMCH) Host To Hub bridge";
97                 break;
98         case 0x71808086:
99                 s = "Intel 82443LX (440 LX) host to PCI bridge";
100                 break;
101         case 0x71908086:
102                 s = "Intel 82443BX (440 BX) host to PCI bridge";
103                 break;
104         case 0x71928086:
105                 s = "Intel 82443BX host to PCI bridge (AGP disabled)";
106                 break;
107         case 0x71948086:
108                 s = "Intel 82443MX host to PCI bridge";
109                 break;
110         case 0x71a08086:
111                 s = "Intel 82443GX host to PCI bridge";
112                 break;
113         case 0x71a18086:
114                 s = "Intel 82443GX host to AGP bridge";
115                 break;
116         case 0x71a28086:
117                 s = "Intel 82443GX host to PCI bridge (AGP disabled)";
118                 break;
119         case 0x84c48086:
120                 s = "Intel 82454KX/GX (Orion) host to PCI bridge";
121                 *busnum = nexus_pcib_read_config(0, bus, slot, func, 0x4a, 1);
122                 break;
123         case 0x84ca8086:
124                 /*
125                  * For the 450nx chipset, there is a whole bundle of
126                  * things pretending to be host bridges. The MIOC will 
127                  * be seen first and isn't really a pci bridge (the
128                  * actual busses are attached to the PXB's). We need to 
129                  * read the registers of the MIOC to figure out the
130                  * bus numbers for the PXB channels.
131                  *
132                  * Since the MIOC doesn't have a pci bus attached, we
133                  * pretend it wasn't there.
134                  */
135                 pxb[0] = nexus_pcib_read_config(0, bus, slot, func,
136                                                 0xd0, 1); /* BUSNO[0] */
137                 pxb[1] = nexus_pcib_read_config(0, bus, slot, func,
138                                                 0xd1, 1) + 1;   /* SUBA[0]+1 */
139                 pxb[2] = nexus_pcib_read_config(0, bus, slot, func,
140                                                 0xd3, 1); /* BUSNO[1] */
141                 pxb[3] = nexus_pcib_read_config(0, bus, slot, func,
142                                                 0xd4, 1) + 1;   /* SUBA[1]+1 */
143                 return NULL;
144         case 0x84cb8086:
145                 switch (slot) {
146                 case 0x12:
147                         s = "Intel 82454NX PXB#0, Bus#A";
148                         *busnum = pxb[0];
149                         break;
150                 case 0x13:
151                         s = "Intel 82454NX PXB#0, Bus#B";
152                         *busnum = pxb[1];
153                         break;
154                 case 0x14:
155                         s = "Intel 82454NX PXB#1, Bus#A";
156                         *busnum = pxb[2];
157                         break;
158                 case 0x15:
159                         s = "Intel 82454NX PXB#1, Bus#B";
160                         *busnum = pxb[3];
161                         break;
162                 }
163                 break;
164         case 0x1A308086:
165                 s = "Intel 82845 Host to PCI bridge";
166                 break;
167
168                 /* AMD -- vendor 0x1022 */
169         case 0x30001022:
170                 s = "AMD Elan SC520 host to PCI bridge";
171 #ifdef CPU_ELAN
172                 init_AMD_Elan_sc520();
173 #else
174                 printf("*** WARNING: kernel option CPU_ELAN missing");
175                 printf("-- timekeeping may be wrong\n");
176 #endif
177                 break;
178         case 0x70061022:
179                 s = "AMD-751 host to PCI bridge";
180                 break;
181         case 0x700e1022:
182                 s = "AMD-761 host to PCI bridge";
183                 break;
184
185                 /* SiS -- vendor 0x1039 */
186         case 0x04961039:
187                 s = "SiS 85c496";
188                 break;
189         case 0x04061039:
190                 s = "SiS 85c501";
191                 break;
192         case 0x06011039:
193                 s = "SiS 85c601";
194                 break;
195         case 0x55911039:
196                 s = "SiS 5591 host to PCI bridge";
197                 break;
198         case 0x00011039:
199                 s = "SiS 5591 host to AGP bridge";
200                 break;
201
202                 /* VLSI -- vendor 0x1004 */
203         case 0x00051004:
204                 s = "VLSI 82C592 Host to PCI bridge";
205                 break;
206
207                 /* XXX Here is MVP3, I got the datasheet but NO M/B to test it  */
208                 /* totally. Please let me know if anything wrong.            -F */
209                 /* XXX need info on the MVP3 -- any takers? */
210         case 0x05981106:
211                 s = "VIA 82C598MVP (Apollo MVP3) host bridge";
212                 break;
213
214                 /* AcerLabs -- vendor 0x10b9 */
215                 /* Funny : The datasheet told me vendor id is "10b8",sub-vendor */
216                 /* id is '10b9" but the register always shows "10b9". -Foxfair  */
217         case 0x154110b9:
218                 s = "AcerLabs M1541 (Aladdin-V) PCI host bridge";
219                 break;
220
221                 /* OPTi -- vendor 0x1045 */
222         case 0xc7011045:
223                 s = "OPTi 82C700 host to PCI bridge";
224                 break;
225         case 0xc8221045:
226                 s = "OPTi 82C822 host to PCI Bridge";
227                 break;
228
229                 /* ServerWorks -- vendor 0x1166 */
230         case 0x00051166:
231                 s = "ServerWorks NB6536 2.0HE host to PCI bridge";
232                 *busnum = nexus_pcib_read_config(0, bus, slot, func, 0x44, 1);
233                 break;
234         
235         case 0x00061166:
236                 /* FALLTHROUGH */
237         case 0x00081166:
238                 s = "ServerWorks host to PCI bridge";
239                 *busnum = nexus_pcib_read_config(0, bus, slot, func, 0x44, 1);
240                 break;
241
242         case 0x00091166:
243                 s = "ServerWorks NB6635 3.0LE host to PCI bridge";
244                 *busnum = nexus_pcib_read_config(0, bus, slot, func, 0x44, 1);
245                 break;
246
247         case 0x00101166:
248                 s = "ServerWorks CIOB30 host to PCI bridge";
249                 *busnum = nexus_pcib_read_config(0, bus, slot, func, 0x44, 1);
250                 break;
251
252                 /* XXX unknown chipset, but working */
253         case 0x00171166:
254                 /* FALLTHROUGH */
255         case 0x01011166:
256                 s = "ServerWorks host to PCI bridge(unknown chipset)";
257                 *busnum = nexus_pcib_read_config(0, bus, slot, func, 0x44, 1);
258                 break;
259
260                 /* Integrated Micro Solutions -- vendor 0x10e0 */
261         case 0x884910e0:
262                 s = "Integrated Micro Solutions VL Bridge";
263                 break;
264
265         default:
266                 if (class == PCIC_BRIDGE && subclass == PCIS_BRIDGE_HOST)
267                         s = "Host to PCI bridge";
268                 break;
269         }
270
271         return s;
272 }
273
274 /*
275  * Scan the first pci bus for host-pci bridges and add pcib instances
276  * to the nexus for each bridge.
277  */
278 static void
279 nexus_pcib_identify(driver_t *driver, device_t parent)
280 {
281         int bus, slot, func;
282         u_int8_t  hdrtype;
283         int found = 0;
284         int pcifunchigh;
285         int found824xx = 0;
286         device_t child;
287         devclass_t pci_devclass;
288
289         if (pci_cfgregopen() == 0)
290                 return;
291         /*
292          * Check to see if we haven't already had a PCI bus added
293          * via some other means. If we have, bail since otherwise
294          * we're going to end up duplicating it.
295          */
296         if ((pci_devclass = devclass_find("pci")) &&
297             devclass_get_device(pci_devclass,0))
298                 return;
299         
300         bus = 0;
301  retry:
302         for (slot = 0; slot <= PCI_SLOTMAX; slot++) {
303                 func = 0;
304                 hdrtype = nexus_pcib_read_config(0, bus, slot, func,
305                                                  PCIR_HEADERTYPE, 1);
306                 if (hdrtype & PCIM_MFDEV)
307                         pcifunchigh = 7;
308                 else
309                         pcifunchigh = 0;
310                 for (func = 0; func <= pcifunchigh; func++) {
311                         /*
312                          * Read the IDs and class from the device.
313                          */
314                         u_int32_t id;
315                         u_int8_t class, subclass, busnum;
316                         const char *s;
317                         device_t *devs;
318                         int ndevs, i;
319
320                         id = nexus_pcib_read_config(0, bus, slot, func,
321                                                     PCIR_DEVVENDOR, 4);
322                         if (id == -1)
323                                 continue;
324                         class = nexus_pcib_read_config(0, bus, slot, func,
325                                                        PCIR_CLASS, 1);
326                         subclass = nexus_pcib_read_config(0, bus, slot, func,
327                                                           PCIR_SUBCLASS, 1);
328
329                         s = nexus_pcib_is_host_bridge(bus, slot, func,
330                                                       id, class, subclass,
331                                                       &busnum);
332                         if (s == NULL)
333                                 continue;
334
335                         /*
336                          * Check to see if the physical bus has already
337                          * been seen. Eg: hybrid 32 and 64 bit host
338                          * bridges to the same logical bus.
339                          */
340                         if (device_get_children(parent, &devs, &ndevs) == 0) {
341                                 for (i = 0; s != NULL && i < ndevs; i++) {
342                                         if (strcmp(device_get_name(devs[i]),
343                                             "pcib") != 0)
344                                                 continue;
345                                         if (nexus_get_pcibus(devs[i]) == busnum)
346                                                 s = NULL;
347                                 }
348                                 free(devs, M_TEMP);
349                         }
350
351                         if (s == NULL)
352                                 continue;
353                         /*
354                          * Add at priority 100 to make sure we
355                          * go after any motherboard resources
356                          */
357                         child = BUS_ADD_CHILD(parent, 100,
358                                               "pcib", busnum);
359                         device_set_desc(child, s);
360                         nexus_set_pcibus(child, busnum);
361
362                         found = 1;
363                         if (id == 0x12258086)
364                                 found824xx = 1;
365                 }
366         }
367         if (found824xx && bus == 0) {
368                 bus++;
369                 goto retry;
370         }
371
372         /*
373          * Make sure we add at least one bridge since some old
374          * hardware doesn't actually have a host-pci bridge device.
375          * Note that pci_cfgregopen() thinks we have PCI devices..
376          */
377         if (!found) {
378                 if (bootverbose)
379                         printf(
380         "nexus_pcib_identify: no bridge found, adding pcib0 anyway\n");
381                 child = BUS_ADD_CHILD(parent, 100, "pcib", 0);
382                 nexus_set_pcibus(child, 0);
383         }
384 }
385
386 static int
387 nexus_pcib_probe(device_t dev)
388 {
389         devclass_t pci_devclass;
390
391         if (pci_cfgregopen() == 0)
392                 return (ENXIO);
393         /*
394          * Check to see if we haven't already had a PCI bus added
395          * via some other means.  If we have, bail since otherwise
396          * we're going to end up duplicating it.
397          */
398         if ((pci_devclass = devclass_find("pci")) && 
399                 devclass_get_device(pci_devclass, device_get_unit(dev)))
400                 return (ENXIO);
401
402         return (0);
403 }
404
405 static int
406 nexus_pcib_attach(device_t dev)
407 {
408         device_t child;
409
410         child = device_add_child(dev, "pci", device_get_unit(dev));
411
412         return (bus_generic_attach(dev));
413 }
414
415 static int
416 nexus_pcib_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
417 {
418         switch (which) {
419         case  PCIB_IVAR_BUS:
420                 *result = nexus_get_pcibus(dev);
421                 return (0);
422         }
423         return (ENOENT);
424 }
425
426 static int
427 nexus_pcib_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
428 {
429         switch (which) {
430         case  PCIB_IVAR_BUS:
431                 nexus_set_pcibus(dev, value);
432                 return (0);
433         }
434         return (ENOENT);
435 }
436
437 /* route interrupt */
438
439 static int
440 nexus_pcib_route_interrupt(device_t pcib, device_t dev, int pin)
441 {
442         return(pci_cfgintr(pci_get_bus(dev), pci_get_slot(dev), pin, 
443                            pci_get_irq(dev)));
444 }
445
446 static device_method_t nexus_pcib_methods[] = {
447         /* Device interface */
448         DEVMETHOD(device_identify,      nexus_pcib_identify),
449         DEVMETHOD(device_probe,         nexus_pcib_probe),
450         DEVMETHOD(device_attach,        nexus_pcib_attach),
451         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
452         DEVMETHOD(device_suspend,       bus_generic_suspend),
453         DEVMETHOD(device_resume,        bus_generic_resume),
454
455         /* Bus interface */
456         DEVMETHOD(bus_print_child,      bus_generic_print_child),
457         DEVMETHOD(bus_read_ivar,        nexus_pcib_read_ivar),
458         DEVMETHOD(bus_write_ivar,       nexus_pcib_write_ivar),
459         DEVMETHOD(bus_alloc_resource,   bus_generic_alloc_resource),
460         DEVMETHOD(bus_release_resource, bus_generic_release_resource),
461         DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
462         DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
463         DEVMETHOD(bus_setup_intr,       bus_generic_setup_intr),
464         DEVMETHOD(bus_teardown_intr,    bus_generic_teardown_intr),
465
466         /* pcib interface */
467         DEVMETHOD(pcib_maxslots,        nexus_pcib_maxslots),
468         DEVMETHOD(pcib_read_config,     nexus_pcib_read_config),
469         DEVMETHOD(pcib_write_config,    nexus_pcib_write_config),
470         DEVMETHOD(pcib_route_interrupt, nexus_pcib_route_interrupt),
471
472         { 0, 0 }
473 };
474
475 static driver_t nexus_pcib_driver = {
476         "pcib",
477         nexus_pcib_methods,
478         1,
479 };
480
481 DRIVER_MODULE(pcib, nexus, nexus_pcib_driver, pcib_devclass, 0, 0);
482
483
484 /*
485  * Provide a device to "eat" the host->pci bridges that we dug up above
486  * and stop them showing up twice on the probes.  This also stops them
487  * showing up as 'none' in pciconf -l.
488  */
489 static int
490 pci_hostb_probe(device_t dev)
491 {
492         if (pci_get_class(dev) == PCIC_BRIDGE &&
493             pci_get_subclass(dev) == PCIS_BRIDGE_HOST) {
494                 device_set_desc(dev, "Host to PCI bridge");
495                 device_quiet(dev);
496                 return 0;
497         }
498         return (ENXIO);
499 }
500
501 static int
502 pci_hostb_attach(device_t dev)
503 {
504         return (0);
505 }
506
507 static device_method_t pci_hostb_methods[] = {
508         /* Device interface */
509         DEVMETHOD(device_probe,         pci_hostb_probe),
510         DEVMETHOD(device_attach,        pci_hostb_attach),
511         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
512         DEVMETHOD(device_suspend,       bus_generic_suspend),
513         DEVMETHOD(device_resume,        bus_generic_resume),
514
515         { 0, 0 }
516 };
517 static driver_t pci_hostb_driver = {
518         "hostb",
519         pci_hostb_methods,
520         1,
521 };
522 static devclass_t pci_hostb_devclass;
523
524 DRIVER_MODULE(hostb, pci, pci_hostb_driver, pci_hostb_devclass, 0, 0);
525
526
527 /*
528  * Install placeholder to claim the resources owned by the
529  * PCI bus interface.  This could be used to extract the 
530  * config space registers in the extreme case where the PnP
531  * ID is available and the PCI BIOS isn't, but for now we just
532  * eat the PnP ID and do nothing else.
533  *
534  * XXX we should silence this probe, as it will generally confuse 
535  * people.
536  */
537 static struct isa_pnp_id pcibus_pnp_ids[] = {
538         { 0x030ad041 /* PNP030A */, "PCI Bus" },
539         { 0 }
540 };
541
542 static int
543 pcibus_pnp_probe(device_t dev)
544 {
545         int result;
546         
547         if ((result = ISA_PNP_PROBE(device_get_parent(dev), dev, pcibus_pnp_ids)) <= 0)
548                 device_quiet(dev);
549         return (result);
550 }
551
552 static int
553 pcibus_pnp_attach(device_t dev)
554 {
555         return(0);
556 }
557
558 static device_method_t pcibus_pnp_methods[] = {
559         /* Device interface */
560         DEVMETHOD(device_probe,         pcibus_pnp_probe),
561         DEVMETHOD(device_attach,        pcibus_pnp_attach),
562         DEVMETHOD(device_detach,        bus_generic_detach),
563         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
564         DEVMETHOD(device_suspend,       bus_generic_suspend),
565         DEVMETHOD(device_resume,        bus_generic_resume),
566         { 0, 0 }
567 };
568
569 static driver_t pcibus_pnp_driver = {
570         "pcibus_pnp",
571         pcibus_pnp_methods,
572         1,              /* no softc */
573 };
574
575 static devclass_t pcibus_pnp_devclass;
576
577 DRIVER_MODULE(pcibus_pnp, isa, pcibus_pnp_driver, pcibus_pnp_devclass, 0, 0);