pci.c revision 1.14 1 1.14 cgd /* $NetBSD: pci.c,v 1.14 1996/03/14 02:35:32 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.13 cgd bus_chipset_tag_t sc_bc;
49 1.10 cgd };
50 1.10 cgd
51 1.10 cgd int pcimatch __P((struct device *, void *, void *));
52 1.10 cgd void pciattach __P((struct device *, struct device *, void *));
53 1.10 cgd
54 1.10 cgd struct cfdriver pcicd = {
55 1.10 cgd NULL, "pci", pcimatch, pciattach, DV_DULL, sizeof(struct pci_softc)
56 1.10 cgd };
57 1.10 cgd
58 1.10 cgd int pciprint __P((void *, char *));
59 1.10 cgd int pcisubmatch __P((struct device *, void *, void *));
60 1.10 cgd
61 1.10 cgd int
62 1.10 cgd pcimatch(parent, match, aux)
63 1.10 cgd struct device *parent;
64 1.10 cgd void *match, *aux;
65 1.10 cgd {
66 1.10 cgd struct cfdata *cf = match;
67 1.10 cgd struct pcibus_attach_args *pba = aux;
68 1.10 cgd
69 1.10 cgd if (strcmp(pba->pba_busname, cf->cf_driver->cd_name))
70 1.10 cgd return (0);
71 1.10 cgd
72 1.10 cgd /* Check the locators */
73 1.14 cgd if (cf->pcibuscf_bus != PCIBUS_UNK_BUS &&
74 1.14 cgd cf->pcibuscf_bus != pba->pba_bus)
75 1.10 cgd return (0);
76 1.10 cgd
77 1.10 cgd /* sanity */
78 1.10 cgd if (pba->pba_bus < 0 || pba->pba_bus > 255)
79 1.10 cgd return (0);
80 1.10 cgd
81 1.10 cgd /*
82 1.10 cgd * XXX check other (hardware?) indicators
83 1.10 cgd */
84 1.10 cgd
85 1.10 cgd return 1;
86 1.10 cgd }
87 1.10 cgd
88 1.10 cgd void
89 1.10 cgd pciattach(parent, self, aux)
90 1.10 cgd struct device *parent, *self;
91 1.10 cgd void *aux;
92 1.10 cgd {
93 1.10 cgd struct pci_softc *sc = (struct pci_softc *)self;
94 1.10 cgd struct pcibus_attach_args *pba = aux;
95 1.13 cgd bus_chipset_tag_t bc;
96 1.14 cgd int device, function, nfunctions;
97 1.10 cgd
98 1.10 cgd sc->sc_bus = pba->pba_bus;
99 1.13 cgd sc->sc_bc = bc = pba->pba_bc;
100 1.10 cgd
101 1.14 cgd pci_md_attach_hook(parent, sc, pba);
102 1.14 cgd printf("\n");
103 1.10 cgd
104 1.14 cgd for (device = 0; device < PCI_MAX_DEVICE_NUMBER; device++) {
105 1.10 cgd pcitag_t tag;
106 1.10 cgd pcireg_t id, class;
107 1.10 cgd struct pci_attach_args pa;
108 1.10 cgd struct cfdata *cf;
109 1.10 cgd int supported;
110 1.10 cgd
111 1.11 cgd tag = pci_make_tag(sc->sc_bus, device, 0);
112 1.10 cgd id = pci_conf_read(tag, PCI_ID_REG);
113 1.10 cgd if (id == 0 || id == 0xffffffff)
114 1.10 cgd continue;
115 1.10 cgd
116 1.10 cgd nfunctions = 1; /* XXX */
117 1.10 cgd
118 1.10 cgd for (function = 0; function < nfunctions; function++) {
119 1.10 cgd tag = pci_make_tag(sc->sc_bus, device, function);
120 1.10 cgd id = pci_conf_read(tag, PCI_ID_REG);
121 1.10 cgd if (id == 0 || id == 0xffffffff)
122 1.10 cgd continue;
123 1.10 cgd class = pci_conf_read(tag, PCI_CLASS_REG);
124 1.10 cgd
125 1.13 cgd pa.pa_bc = bc;
126 1.10 cgd pa.pa_device = device;
127 1.10 cgd pa.pa_function = function;
128 1.10 cgd pa.pa_tag = tag;
129 1.10 cgd pa.pa_id = id;
130 1.10 cgd pa.pa_class = class;
131 1.10 cgd
132 1.10 cgd config_found_sm(self, &pa, pciprint, pcisubmatch);
133 1.10 cgd }
134 1.10 cgd }
135 1.10 cgd }
136 1.1 mycroft
137 1.1 mycroft int
138 1.10 cgd pciprint(aux, pnp)
139 1.1 mycroft void *aux;
140 1.10 cgd char *pnp;
141 1.1 mycroft {
142 1.1 mycroft register struct pci_attach_args *pa = aux;
143 1.10 cgd char devinfo[256];
144 1.1 mycroft
145 1.10 cgd if (pnp) {
146 1.10 cgd pci_devinfo(pa->pa_id, pa->pa_class, 1, devinfo);
147 1.10 cgd printf("%s at %s", devinfo, pnp);
148 1.10 cgd }
149 1.10 cgd printf(" dev %d function %d", pa->pa_device, pa->pa_function);
150 1.6 mycroft return (UNCONF);
151 1.6 mycroft }
152 1.6 mycroft
153 1.6 mycroft int
154 1.6 mycroft pcisubmatch(parent, match, aux)
155 1.6 mycroft struct device *parent;
156 1.6 mycroft void *match, *aux;
157 1.6 mycroft {
158 1.6 mycroft struct cfdata *cf = match;
159 1.6 mycroft struct pci_attach_args *pa = aux;
160 1.6 mycroft
161 1.14 cgd if (cf->pcicf_dev != PCI_UNK_DEV &&
162 1.14 cgd cf->pcicf_dev != pa->pa_device)
163 1.6 mycroft return 0;
164 1.14 cgd if (cf->pcicf_function != PCI_UNK_FUNCTION &&
165 1.14 cgd cf->pcicf_function != pa->pa_function)
166 1.6 mycroft return 0;
167 1.6 mycroft return ((*cf->cf_driver->cd_match)(parent, match, aux));
168 1.1 mycroft }
169