pci.c revision 1.10 1 1.10 cgd /* $NetBSD: pci.c,v 1.10 1996/02/28 01:44:41 cgd Exp $ */
2 1.3 cgd
3 1.1 mycroft /*
4 1.10 cgd * Copyright (c) 1995, 1996 Christopher G. Demetriou. All rights reserved.
5 1.1 mycroft * Copyright (c) 1994 Charles Hannum. All rights reserved.
6 1.1 mycroft *
7 1.1 mycroft * Redistribution and use in source and binary forms, with or without
8 1.1 mycroft * modification, are permitted provided that the following conditions
9 1.1 mycroft * are met:
10 1.1 mycroft * 1. Redistributions of source code must retain the above copyright
11 1.1 mycroft * notice, this list of conditions and the following disclaimer.
12 1.1 mycroft * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 mycroft * notice, this list of conditions and the following disclaimer in the
14 1.1 mycroft * documentation and/or other materials provided with the distribution.
15 1.1 mycroft * 3. All advertising materials mentioning features or use of this software
16 1.1 mycroft * must display the following acknowledgement:
17 1.1 mycroft * This product includes software developed by Charles Hannum.
18 1.1 mycroft * 4. The name of the author may not be used to endorse or promote products
19 1.1 mycroft * derived from this software without specific prior written permission.
20 1.1 mycroft *
21 1.1 mycroft * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 1.1 mycroft * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 1.1 mycroft * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 1.1 mycroft * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 1.1 mycroft * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 1.1 mycroft * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 1.1 mycroft * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 1.1 mycroft * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 1.1 mycroft * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 1.1 mycroft * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 1.1 mycroft */
32 1.1 mycroft
33 1.1 mycroft /*
34 1.10 cgd * PCI bus autoconfiguration.
35 1.1 mycroft */
36 1.1 mycroft
37 1.1 mycroft #include <sys/param.h>
38 1.10 cgd #include <sys/systm.h>
39 1.1 mycroft #include <sys/device.h>
40 1.1 mycroft
41 1.10 cgd #include <dev/pci/pcireg.h>
42 1.7 cgd #include <dev/pci/pcivar.h>
43 1.10 cgd
44 1.10 cgd struct pci_softc {
45 1.10 cgd struct device sc_dev;
46 1.10 cgd
47 1.10 cgd int sc_bus;
48 1.10 cgd };
49 1.10 cgd
50 1.10 cgd int pcimatch __P((struct device *, void *, void *));
51 1.10 cgd void pciattach __P((struct device *, struct device *, void *));
52 1.10 cgd
53 1.10 cgd struct cfdriver pcicd = {
54 1.10 cgd NULL, "pci", pcimatch, pciattach, DV_DULL, sizeof(struct pci_softc)
55 1.10 cgd };
56 1.10 cgd
57 1.10 cgd int pciprint __P((void *, char *));
58 1.10 cgd int pcisubmatch __P((struct device *, void *, void *));
59 1.10 cgd
60 1.10 cgd int
61 1.10 cgd pcimatch(parent, match, aux)
62 1.10 cgd struct device *parent;
63 1.10 cgd void *match, *aux;
64 1.10 cgd {
65 1.10 cgd struct cfdata *cf = match;
66 1.10 cgd struct pcibus_attach_args *pba = aux;
67 1.10 cgd
68 1.10 cgd if (strcmp(pba->pba_busname, cf->cf_driver->cd_name))
69 1.10 cgd return (0);
70 1.10 cgd
71 1.10 cgd /* Check the locators */
72 1.10 cgd #ifdef i386 /* XXX should not be attached at root, but is on i386 */
73 1.10 cgd if (parent != NULL)
74 1.10 cgd #endif /* i386 XXX */
75 1.10 cgd if (cf->cf_loc[0] != -1 && cf->cf_loc[0] != pba->pba_bus)
76 1.10 cgd return (0);
77 1.10 cgd
78 1.10 cgd /* sanity */
79 1.10 cgd if (pba->pba_bus < 0 || pba->pba_bus > 255)
80 1.10 cgd return (0);
81 1.10 cgd if (pba->pba_maxndevs < 0 || pba->pba_maxndevs > 32)
82 1.10 cgd return (0);
83 1.10 cgd
84 1.10 cgd /*
85 1.10 cgd * XXX check other (hardware?) indicators
86 1.10 cgd */
87 1.10 cgd
88 1.10 cgd return 1;
89 1.10 cgd }
90 1.10 cgd
91 1.10 cgd void
92 1.10 cgd pciattach(parent, self, aux)
93 1.10 cgd struct device *parent, *self;
94 1.10 cgd void *aux;
95 1.10 cgd {
96 1.10 cgd struct pci_softc *sc = (struct pci_softc *)self;
97 1.10 cgd struct pcibus_attach_args *pba = aux;
98 1.10 cgd int maxndevs, device, function, nfunctions;
99 1.10 cgd
100 1.10 cgd #ifdef i386 /* XXX should not be attached at root, but is on i386 */
101 1.10 cgd if (parent == NULL)
102 1.10 cgd printf(": bus %d, configuration mode %d", pba->pba_bus,
103 1.10 cgd pci_mode);
104 1.10 cgd #endif /* XXX */
105 1.10 cgd printf("\n");
106 1.10 cgd
107 1.10 cgd sc->sc_bus = pba->pba_bus;
108 1.10 cgd
109 1.10 cgd maxndevs = pba->pba_maxndevs;
110 1.10 cgd
111 1.10 cgd for (device = 0; device < maxndevs; device++) {
112 1.10 cgd pcitag_t tag;
113 1.10 cgd pcireg_t id, class;
114 1.10 cgd struct pci_attach_args pa;
115 1.10 cgd struct cfdata *cf;
116 1.10 cgd int supported;
117 1.10 cgd
118 1.10 cgd tag = pci_make_tag(sc->sc_bus, device, function);
119 1.10 cgd id = pci_conf_read(tag, PCI_ID_REG);
120 1.10 cgd if (id == 0 || id == 0xffffffff)
121 1.10 cgd continue;
122 1.10 cgd
123 1.10 cgd nfunctions = 1; /* XXX */
124 1.10 cgd
125 1.10 cgd for (function = 0; function < nfunctions; function++) {
126 1.10 cgd tag = pci_make_tag(sc->sc_bus, device, function);
127 1.10 cgd id = pci_conf_read(tag, PCI_ID_REG);
128 1.10 cgd if (id == 0 || id == 0xffffffff)
129 1.10 cgd continue;
130 1.10 cgd class = pci_conf_read(tag, PCI_CLASS_REG);
131 1.10 cgd
132 1.10 cgd pa.pa_device = device;
133 1.10 cgd pa.pa_function = function;
134 1.10 cgd pa.pa_tag = tag;
135 1.10 cgd pa.pa_id = id;
136 1.10 cgd pa.pa_class = class;
137 1.10 cgd
138 1.10 cgd config_found_sm(self, &pa, pciprint, pcisubmatch);
139 1.10 cgd }
140 1.10 cgd }
141 1.10 cgd }
142 1.1 mycroft
143 1.1 mycroft int
144 1.10 cgd pciprint(aux, pnp)
145 1.1 mycroft void *aux;
146 1.10 cgd char *pnp;
147 1.1 mycroft {
148 1.1 mycroft register struct pci_attach_args *pa = aux;
149 1.10 cgd char devinfo[256];
150 1.1 mycroft
151 1.10 cgd if (pnp) {
152 1.10 cgd pci_devinfo(pa->pa_id, pa->pa_class, 1, devinfo);
153 1.10 cgd printf("%s at %s", devinfo, pnp);
154 1.10 cgd }
155 1.10 cgd printf(" dev %d function %d", pa->pa_device, pa->pa_function);
156 1.6 mycroft return (UNCONF);
157 1.6 mycroft }
158 1.6 mycroft
159 1.6 mycroft int
160 1.6 mycroft pcisubmatch(parent, match, aux)
161 1.6 mycroft struct device *parent;
162 1.6 mycroft void *match, *aux;
163 1.6 mycroft {
164 1.6 mycroft struct cfdata *cf = match;
165 1.6 mycroft struct pci_attach_args *pa = aux;
166 1.6 mycroft
167 1.10 cgd if (cf->cf_loc[0] != -1 && cf->cf_loc[0] != pa->pa_device)
168 1.6 mycroft return 0;
169 1.10 cgd if (cf->cf_loc[1] != -1 && cf->cf_loc[1] != pa->pa_function)
170 1.6 mycroft return 0;
171 1.6 mycroft return ((*cf->cf_driver->cd_match)(parent, match, aux));
172 1.1 mycroft }
173