]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/i386/isa/pcibus.c
unfinished sblive driver, playback/mixer only for now - not enabled in
[FreeBSD/FreeBSD.git] / sys / i386 / isa / 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$
27  *
28  */
29
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/bus.h>
33 #include <sys/kernel.h>
34
35 #include <pci/pcivar.h>
36 #include <pci/pcireg.h>
37 #include <i386/isa/pcibus.h>
38
39 static int cfgmech;
40 static int devmax;
41
42 /* enable configuration space accesses and return data port address */
43
44 static int
45 pci_cfgenable(unsigned bus, unsigned slot, unsigned func, int reg, int bytes)
46 {
47         int dataport = 0;
48
49         if (bus <= PCI_BUSMAX
50             && slot < devmax
51             && func <= PCI_FUNCMAX
52             && reg <= PCI_REGMAX
53             && bytes != 3
54             && (unsigned) bytes <= 4
55             && (reg & (bytes -1)) == 0) {
56                 switch (cfgmech) {
57                 case 1:
58                         outl(CONF1_ADDR_PORT, (1 << 31)
59                              | (bus << 16) | (slot << 11) 
60                              | (func << 8) | (reg & ~0x03));
61                         dataport = CONF1_DATA_PORT + (reg & 0x03);
62                         break;
63                 case 2:
64                         outb(CONF2_ENABLE_PORT, 0xf0 | (func << 1));
65                         outb(CONF2_FORWARD_PORT, bus);
66                         dataport = 0xc000 | (slot << 8) | reg;
67                         break;
68                 }
69         }
70         return (dataport);
71 }
72
73 /* disable configuration space accesses */
74
75 static void
76 pci_cfgdisable(void)
77 {
78         switch (cfgmech) {
79         case 1:
80                 outl(CONF1_ADDR_PORT, 0);
81                 break;
82         case 2:
83                 outb(CONF2_ENABLE_PORT, 0);
84                 outb(CONF2_FORWARD_PORT, 0);
85                 break;
86         }
87 }
88
89 /* read configuration space register */
90
91 int
92 pci_cfgread(pcicfgregs *cfg, int reg, int bytes)
93 {
94         int data = -1;
95         int port;
96
97         port = pci_cfgenable(cfg->bus, cfg->slot, cfg->func, reg, bytes);
98
99         if (port != 0) {
100                 switch (bytes) {
101                 case 1:
102                         data = inb(port);
103                         break;
104                 case 2:
105                         data = inw(port);
106                         break;
107                 case 4:
108                         data = inl(port);
109                         break;
110                 }
111                 pci_cfgdisable();
112         }
113         return (data);
114 }
115
116 /* write configuration space register */
117
118 void
119 pci_cfgwrite(pcicfgregs *cfg, int reg, int data, int bytes)
120 {
121         int port;
122
123         port = pci_cfgenable(cfg->bus, cfg->slot, cfg->func, reg, bytes);
124         if (port != 0) {
125                 switch (bytes) {
126                 case 1:
127                         outb(port, data);
128                         break;
129                 case 2:
130                         outw(port, data);
131                         break;
132                 case 4:
133                         outl(port, data);
134                         break;
135                 }
136                 pci_cfgdisable();
137         }
138 }
139
140 /* check whether the configuration mechanism has been correct identified */
141
142 static int
143 pci_cfgcheck(int maxdev)
144 {
145         u_char device;
146
147         if (bootverbose) 
148                 printf("pci_cfgcheck:\tdevice ");
149
150         for (device = 0; device < maxdev; device++) {
151                 unsigned id, class, header;
152                 if (bootverbose) 
153                         printf("%d ", device);
154
155                 id = inl(pci_cfgenable(0, device, 0, 0, 4));
156                 if (id == 0 || id == -1)
157                         continue;
158
159                 class = inl(pci_cfgenable(0, device, 0, 8, 4)) >> 8;
160                 if (bootverbose)
161                         printf("[class=%06x] ", class);
162                 if (class == 0 || (class & 0xf870ff) != 0)
163                         continue;
164
165                 header = inb(pci_cfgenable(0, device, 0, 14, 1));
166                 if (bootverbose) 
167                         printf("[hdr=%02x] ", header);
168                 if ((header & 0x7e) != 0)
169                         continue;
170
171                 if (bootverbose)
172                         printf("is there (id=%08x)\n", id);
173
174                 pci_cfgdisable();
175                 return (1);
176         }
177         if (bootverbose) 
178                 printf("-- nothing found\n");
179
180         pci_cfgdisable();
181         return (0);
182 }
183
184 static int
185 pci_cfgopen(void)
186 {
187         unsigned long mode1res,oldval1;
188         unsigned char mode2res,oldval2;
189
190         oldval1 = inl(CONF1_ADDR_PORT);
191
192         if (bootverbose) {
193                 printf("pci_open(1):\tmode 1 addr port (0x0cf8) is 0x%08lx\n",
194                        oldval1);
195         }
196
197         if ((oldval1 & CONF1_ENABLE_MSK) == 0) {
198
199                 cfgmech = 1;
200                 devmax = 32;
201
202                 outl(CONF1_ADDR_PORT, CONF1_ENABLE_CHK);
203                 outb(CONF1_ADDR_PORT +3, 0);
204                 mode1res = inl(CONF1_ADDR_PORT);
205                 outl(CONF1_ADDR_PORT, oldval1);
206
207                 if (bootverbose)
208                         printf("pci_open(1a):\tmode1res=0x%08lx (0x%08lx)\n", 
209                                mode1res, CONF1_ENABLE_CHK);
210
211                 if (mode1res) {
212                         if (pci_cfgcheck(32)) 
213                                 return (cfgmech);
214                 }
215
216                 outl(CONF1_ADDR_PORT, CONF1_ENABLE_CHK1);
217                 mode1res = inl(CONF1_ADDR_PORT);
218                 outl(CONF1_ADDR_PORT, oldval1);
219
220                 if (bootverbose)
221                         printf("pci_open(1b):\tmode1res=0x%08lx (0x%08lx)\n", 
222                                mode1res, CONF1_ENABLE_CHK1);
223
224                 if ((mode1res & CONF1_ENABLE_MSK1) == CONF1_ENABLE_RES1) {
225                         if (pci_cfgcheck(32)) 
226                                 return (cfgmech);
227                 }
228         }
229
230         oldval2 = inb(CONF2_ENABLE_PORT);
231
232         if (bootverbose) {
233                 printf("pci_open(2):\tmode 2 enable port (0x0cf8) is 0x%02x\n",
234                        oldval2);
235         }
236
237         if ((oldval2 & 0xf0) == 0) {
238
239                 cfgmech = 2;
240                 devmax = 16;
241
242                 outb(CONF2_ENABLE_PORT, CONF2_ENABLE_CHK);
243                 mode2res = inb(CONF2_ENABLE_PORT);
244                 outb(CONF2_ENABLE_PORT, oldval2);
245
246                 if (bootverbose)
247                         printf("pci_open(2a):\tmode2res=0x%02x (0x%02x)\n", 
248                                mode2res, CONF2_ENABLE_CHK);
249
250                 if (mode2res == CONF2_ENABLE_RES) {
251                         if (bootverbose)
252                                 printf("pci_open(2a):\tnow trying mechanism 2\n");
253
254                         if (pci_cfgcheck(16)) 
255                                 return (cfgmech);
256                 }
257         }
258
259         cfgmech = 0;
260         devmax = 0;
261         return (cfgmech);
262 }
263
264 static devclass_t       pcib_devclass;
265
266 static const char *
267 nexus_pcib_is_host_bridge(pcicfgregs *cfg,
268                           u_int32_t id, u_int8_t class, u_int8_t subclass,
269                           u_int8_t *busnum)
270 {
271         const char *s = NULL;
272         static u_int8_t pxb[4]; /* hack for 450nx */
273
274         *busnum = 0;
275
276         switch (id) {
277         case 0x12258086:
278                 s = "Intel 824?? host to PCI bridge";
279                 /* XXX This is a guess */
280                 /* *busnum = pci_cfgread(cfg, 0x41, 1); */
281                 *busnum = cfg->bus;
282                 break;
283         case 0x71208086:
284                 s = "Intel 82810 (i810 GMCH) Host To Hub bridge";
285                 break;
286         case 0x71228086:
287                 s = "Intel 82810-DC100 (i810-DC100 GMCH) Host To Hub bridge";
288                 break;
289         case 0x71248086:
290                 s = "Intel 82810E (i810E GMCH) Host To Hub bridge";
291                 break;
292         case 0x71808086:
293                 s = "Intel 82443LX (440 LX) host to PCI bridge";
294                 break;
295         case 0x71908086:
296                 s = "Intel 82443BX (440 BX) host to PCI bridge";
297                 break;
298         case 0x71928086:
299                 s = "Intel 82443BX host to PCI bridge (AGP disabled)";
300                 break;
301         case 0x71a08086:
302                 s = "Intel 82443GX host to PCI bridge";
303                 break;
304         case 0x71a18086:
305                 s = "Intel 82443GX host to AGP bridge";
306                 break;
307         case 0x71a28086:
308                 s = "Intel 82443GX host to PCI bridge (AGP disabled)";
309                 break;
310         case 0x84c48086:
311                 s = "Intel 82454KX/GX (Orion) host to PCI bridge";
312                 *busnum = pci_cfgread(cfg, 0x4a, 1);
313                 break;
314         case 0x84ca8086:
315                 /*
316                  * For the 450nx chipset, there is a whole bundle of
317                  * things pretending to be host bridges. The MIOC will 
318                  * be seen first and isn't really a pci bridge (the
319                  * actual busses are attached to the PXB's). We need to 
320                  * read the registers of the MIOC to figure out the
321                  * bus numbers for the PXB channels.
322                  *
323                  * Since the MIOC doesn't have a pci bus attached, we
324                  * pretend it wasn't there.
325                  */
326                 pxb[0] = pci_cfgread(cfg, 0xd0, 1); /* BUSNO[0] */
327                 pxb[1] = pci_cfgread(cfg, 0xd1, 1) + 1; /* SUBA[0]+1 */
328                 pxb[2] = pci_cfgread(cfg, 0xd3, 1); /* BUSNO[1] */
329                 pxb[3] = pci_cfgread(cfg, 0xd4, 1) + 1; /* SUBA[1]+1 */
330                 return NULL;
331         case 0x84cb8086:
332                 switch (cfg->slot) {
333                 case 0x12:
334                         s = "Intel 82454NX PXB#0, Bus#A";
335                         *busnum = pxb[0];
336                         break;
337                 case 0x13:
338                         s = "Intel 82454NX PXB#0, Bus#B";
339                         *busnum = pxb[1];
340                         break;
341                 case 0x14:
342                         s = "Intel 82454NX PXB#1, Bus#A";
343                         *busnum = pxb[2];
344                         break;
345                 case 0x15:
346                         s = "Intel 82454NX PXB#1, Bus#B";
347                         *busnum = pxb[3];
348                         break;
349                 }
350                 break;
351
352                 /* AMD -- vendor 0x1022 */
353         case 0x70061022:
354                 s = "AMD-751 host to PCI bridge";
355                 break;
356
357                 /* SiS -- vendor 0x1039 */
358         case 0x04961039:
359                 s = "SiS 85c496";
360                 break;
361         case 0x04061039:
362                 s = "SiS 85c501";
363                 break;
364         case 0x06011039:
365                 s = "SiS 85c601";
366                 break;
367         case 0x55911039:
368                 s = "SiS 5591 host to PCI bridge";
369                 break;
370         case 0x00011039:
371                 s = "SiS 5591 host to AGP bridge";
372                 break;
373
374                 /* VLSI -- vendor 0x1004 */
375         case 0x00051004:
376                 s = "VLSI 82C592 Host to PCI bridge";
377                 break;
378
379                 /* XXX Here is MVP3, I got the datasheet but NO M/B to test it  */
380                 /* totally. Please let me know if anything wrong.            -F */
381                 /* XXX need info on the MVP3 -- any takers? */
382         case 0x05981106:
383                 s = "VIA 82C598MVP (Apollo MVP3) host bridge";
384                 break;
385
386                 /* AcerLabs -- vendor 0x10b9 */
387                 /* Funny : The datasheet told me vendor id is "10b8",sub-vendor */
388                 /* id is '10b9" but the register always shows "10b9". -Foxfair  */
389         case 0x154110b9:
390                 s = "AcerLabs M1541 (Aladdin-V) PCI host bridge";
391                 break;
392
393                 /* OPTi -- vendor 0x1045 */
394         case 0xc8221045:
395                 s = "OPTi 82C822 host to PCI Bridge";
396                 break;
397
398                 /* RCC -- vendor 0x1166 */
399         case 0x00051166:
400                 s = "RCC HE host to PCI bridge";
401                 *busnum = pci_cfgread(cfg, 0x44, 1);
402                 break;
403         
404         case 0x00061166:
405                 /* FALLTHROUGH */
406         case 0x00081166:
407                 s = "RCC host to PCI bridge";
408                 *busnum = pci_cfgread(cfg, 0x44, 1);
409                 break;
410
411         case 0x00091166:
412                 s = "RCC LE host to PCI bridge";
413                 *busnum = pci_cfgread(cfg, 0x44, 1);
414                 break;
415
416                 /* Integrated Micro Solutions -- vendor 0x10e0 */
417         case 0x884910e0:
418                 s = "Integrated Micro Solutions VL Bridge";
419                 break;
420
421         default:
422                 if (class == PCIC_BRIDGE && subclass == PCIS_BRIDGE_HOST)
423                         s = "Host to PCI bridge";
424                 break;
425         }
426
427         return s;
428 }
429
430 /*
431  * Scan the first pci bus for host-pci bridges and add pcib instances
432  * to the nexus for each bridge.
433  */
434 static void
435 nexus_pcib_identify(driver_t *driver, device_t parent)
436 {
437         pcicfgregs probe;
438         u_int8_t  hdrtype;
439         int found = 0;
440         int pcifunchigh;
441         int found824xx = 0;
442
443         if (pci_cfgopen() == 0)
444                 return;
445         probe.hose = 0;
446         probe.bus = 0;
447  retry:
448         for (probe.slot = 0; probe.slot <= PCI_SLOTMAX; probe.slot++) {
449                 probe.func = 0;
450                 hdrtype = pci_cfgread(&probe, PCIR_HEADERTYPE, 1);
451                 if (hdrtype & PCIM_MFDEV)
452                         pcifunchigh = 7;
453                 else
454                         pcifunchigh = 0;
455                 for (probe.func = 0;
456                      probe.func <= pcifunchigh;
457                      probe.func++) {
458                         /*
459                          * Read the IDs and class from the device.
460                          */
461                         u_int32_t id;
462                         u_int8_t class, subclass, busnum;
463                         device_t child;
464                         const char *s;
465
466                         id = pci_cfgread(&probe, PCIR_DEVVENDOR, 4);
467                         if (id == -1)
468                                 continue;
469                         class = pci_cfgread(&probe, PCIR_CLASS, 1);
470                         subclass = pci_cfgread(&probe, PCIR_SUBCLASS, 1);
471
472                         s = nexus_pcib_is_host_bridge(&probe, id,
473                                                       class, subclass,
474                                                       &busnum);
475                         if (s) {
476                                 /*
477                                  * Add at priority 100 to make sure we
478                                  * go after any motherboard resources
479                                  */
480                                 child = BUS_ADD_CHILD(parent, 100,
481                                                       "pcib", busnum);
482                                 device_set_desc(child, s);
483                                 found = 1;
484                                 if (id == 0x12258086)
485                                         found824xx = 1;
486                         }
487                 }
488         }
489         if (found824xx && probe.bus == 0) {
490                 probe.bus++;
491                 goto retry;
492         }
493
494         /*
495          * Make sure we add at least one bridge since some old
496          * hardware doesn't actually have a host-pci bridge device.
497          * Note that pci_cfgopen() thinks we have PCI devices..
498          */
499         if (!found) {
500                 if (bootverbose)
501                         printf(
502         "nexus_pcib_identify: no bridge found, adding pcib0 anyway\n");
503                 BUS_ADD_CHILD(parent, 100, "pcib", 0);
504         }
505 }
506
507 static int
508 nexus_pcib_probe(device_t dev)
509 {
510         if (pci_cfgopen() != 0) {
511                 device_add_child(dev, "pci", device_get_unit(dev));
512                 return 0;
513         }
514         return ENXIO;
515 }
516
517 static device_method_t nexus_pcib_methods[] = {
518         /* Device interface */
519         DEVMETHOD(device_identify,      nexus_pcib_identify),
520         DEVMETHOD(device_probe,         nexus_pcib_probe),
521         DEVMETHOD(device_attach,        bus_generic_attach),
522         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
523         DEVMETHOD(device_suspend,       bus_generic_suspend),
524         DEVMETHOD(device_resume,        bus_generic_resume),
525
526         /* Bus interface */
527         DEVMETHOD(bus_print_child,      bus_generic_print_child),
528         DEVMETHOD(bus_alloc_resource,   bus_generic_alloc_resource),
529         DEVMETHOD(bus_release_resource, bus_generic_release_resource),
530         DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
531         DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
532         DEVMETHOD(bus_setup_intr,       bus_generic_setup_intr),
533         DEVMETHOD(bus_teardown_intr,    bus_generic_teardown_intr),
534
535         { 0, 0 }
536 };
537
538 static driver_t nexus_pcib_driver = {
539         "pcib",
540         nexus_pcib_methods,
541         1,
542 };
543
544 DRIVER_MODULE(pcib, nexus, nexus_pcib_driver, pcib_devclass, 0, 0);