puc.c revision 1.1 1 1.1 cgd /* $NetBSD: puc.c,v 1.1 1998/06/26 18:52:41 cgd Exp $ */
2 1.1 cgd
3 1.1 cgd /*
4 1.1 cgd * Copyright (c) 1996, 1998 Christopher G. Demetriou. All rights reserved.
5 1.1 cgd *
6 1.1 cgd * Redistribution and use in source and binary forms, with or without
7 1.1 cgd * modification, are permitted provided that the following conditions
8 1.1 cgd * are met:
9 1.1 cgd * 1. Redistributions of source code must retain the above copyright
10 1.1 cgd * notice, this list of conditions and the following disclaimer.
11 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 cgd * notice, this list of conditions and the following disclaimer in the
13 1.1 cgd * documentation and/or other materials provided with the distribution.
14 1.1 cgd * 3. All advertising materials mentioning features or use of this software
15 1.1 cgd * must display the following acknowledgement:
16 1.1 cgd * This product includes software developed by Christopher G. Demetriou
17 1.1 cgd * for the NetBSD Project.
18 1.1 cgd * 4. The name of the author may not be used to endorse or promote products
19 1.1 cgd * derived from this software without specific prior written permission
20 1.1 cgd *
21 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 1.1 cgd * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 1.1 cgd * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 1.1 cgd * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 1.1 cgd * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 1.1 cgd * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 1.1 cgd * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 1.1 cgd * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 1.1 cgd * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 1.1 cgd * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 1.1 cgd */
32 1.1 cgd
33 1.1 cgd /*
34 1.1 cgd * PCI "universal" communication card device driver, glues com, lpt,
35 1.1 cgd * and similar ports to PCI via bridge chip often much larger than
36 1.1 cgd * the devices being glued.
37 1.1 cgd *
38 1.1 cgd * Author: Christopher G. Demetriou, May 14, 1998 (derived from NetBSD
39 1.1 cgd * sys/dev/pci/pciide.c, revision 1.6).
40 1.1 cgd *
41 1.1 cgd * These devices could be (and some times are) described as
42 1.1 cgd * communications/{serial,parallel}, etc. devices with known
43 1.1 cgd * programming interfaces, but those programming interfaces (in
44 1.1 cgd * particular the BAR assignments for devices, etc.) in fact are not
45 1.1 cgd * particularly well defined.
46 1.1 cgd *
47 1.1 cgd * After I/we have seen more of these devices, it may be possible
48 1.1 cgd * to generalize some of these bits. In particular, devices which
49 1.1 cgd * describe themselves as communications/serial/16[45]50, and
50 1.1 cgd * communications/parallel/??? might be attached via direct
51 1.1 cgd * 'com' and 'lpt' attachments to pci.
52 1.1 cgd */
53 1.1 cgd
54 1.1 cgd #include <sys/param.h>
55 1.1 cgd #include <sys/systm.h>
56 1.1 cgd #include <sys/device.h>
57 1.1 cgd
58 1.1 cgd #include <dev/pci/pcireg.h>
59 1.1 cgd #include <dev/pci/pcivar.h>
60 1.1 cgd #include <dev/pci/pucvar.h>
61 1.1 cgd
62 1.1 cgd struct puc_softc {
63 1.1 cgd struct device sc_dev;
64 1.1 cgd
65 1.1 cgd /* static configuration data */
66 1.1 cgd const struct puc_device_description *sc_desc;
67 1.1 cgd
68 1.1 cgd /* card-global dynamic data */
69 1.1 cgd void *sc_ih;
70 1.1 cgd struct {
71 1.1 cgd int mapped;
72 1.1 cgd bus_addr_t a;
73 1.1 cgd bus_size_t s;
74 1.1 cgd bus_space_tag_t t;
75 1.1 cgd bus_space_handle_t h;
76 1.1 cgd } sc_bar_mappings[6]; /* XXX constant */
77 1.1 cgd
78 1.1 cgd /* per-port dynamic data */
79 1.1 cgd struct {
80 1.1 cgd struct device *dev;
81 1.1 cgd
82 1.1 cgd /* filled in by port attachments */
83 1.1 cgd int (*ihand) __P((void *));
84 1.1 cgd void *ihandarg;
85 1.1 cgd } sc_ports[PUC_MAX_PORTS];
86 1.1 cgd };
87 1.1 cgd
88 1.1 cgd int puc_match __P((struct device *, struct cfdata *, void *));
89 1.1 cgd void puc_attach __P((struct device *, struct device *, void *));
90 1.1 cgd int puc_print __P((void *, const char *));
91 1.1 cgd int puc_submatch __P((struct device *, struct cfdata *, void *));
92 1.1 cgd
93 1.1 cgd struct cfattach puc_ca = {
94 1.1 cgd sizeof(struct puc_softc), puc_match, puc_attach
95 1.1 cgd };
96 1.1 cgd
97 1.1 cgd static const struct puc_device_description *
98 1.1 cgd puc_find_description __P((pcireg_t, pcireg_t, pcireg_t, pcireg_t));
99 1.1 cgd static const char *
100 1.1 cgd puc_port_type_name __P((int));
101 1.1 cgd
102 1.1 cgd int
103 1.1 cgd puc_match(parent, match, aux)
104 1.1 cgd struct device *parent;
105 1.1 cgd struct cfdata *match;
106 1.1 cgd void *aux;
107 1.1 cgd {
108 1.1 cgd struct pci_attach_args *pa = aux;
109 1.1 cgd const struct puc_device_description *desc;
110 1.1 cgd pcireg_t bhlc, subsys;
111 1.1 cgd
112 1.1 cgd bhlc = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_BHLC_REG);
113 1.1 cgd if (PCI_HDRTYPE_TYPE(bhlc) != 0)
114 1.1 cgd return (0);
115 1.1 cgd
116 1.1 cgd subsys = pci_conf_read(pa->pa_pc, pa->pa_tag, 0x2c); /* XXX const */
117 1.1 cgd
118 1.1 cgd desc = puc_find_description(PCI_VENDOR(pa->pa_id),
119 1.1 cgd PCI_PRODUCT(pa->pa_id), PCI_VENDOR(subsys), PCI_PRODUCT(subsys));
120 1.1 cgd if (desc != NULL)
121 1.1 cgd return (10);
122 1.1 cgd
123 1.1 cgd /*
124 1.1 cgd * Match class/subclass, so we can tell people to compile kernel
125 1.1 cgd * with options that cause this driver to spew.
126 1.1 cgd */
127 1.1 cgd if (PCI_CLASS(pa->pa_class) == PCI_CLASS_COMMUNICATIONS &&
128 1.1 cgd PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_BRIDGE_PCI)
129 1.1 cgd return (1);
130 1.1 cgd
131 1.1 cgd return (0);
132 1.1 cgd }
133 1.1 cgd
134 1.1 cgd void
135 1.1 cgd puc_attach(parent, self, aux)
136 1.1 cgd struct device *parent, *self;
137 1.1 cgd void *aux;
138 1.1 cgd {
139 1.1 cgd struct puc_softc *sc = (struct puc_softc *)self;
140 1.1 cgd struct pci_attach_args *pa = aux;
141 1.1 cgd struct puc_attach_args paa;
142 1.1 cgd pci_intr_handle_t intrhandle;
143 1.1 cgd pcireg_t subsys;
144 1.1 cgd int i, barindex;
145 1.1 cgd
146 1.1 cgd subsys = pci_conf_read(pa->pa_pc, pa->pa_tag, 0x2c); /* XXX const */
147 1.1 cgd sc->sc_desc = puc_find_description(PCI_VENDOR(pa->pa_id),
148 1.1 cgd PCI_PRODUCT(pa->pa_id), PCI_VENDOR(subsys), PCI_PRODUCT(subsys));
149 1.1 cgd if (sc->sc_desc == NULL) {
150 1.1 cgd /*
151 1.1 cgd * This was a class/subclass match, so tell people to compile
152 1.1 cgd * kernel with options that cause this driver to spew.
153 1.1 cgd */
154 1.1 cgd #ifdef PUC_PRINT_REGS
155 1.1 cgd printf(":\n");
156 1.1 cgd pci_conf_print(pa->pa_pc, pa->pa_tag, NULL);
157 1.1 cgd #else
158 1.1 cgd printf(": unknown PCI communications device\n");
159 1.1 cgd printf("%s: compile kernel with PUC_PRINT_REGS and larger\n",
160 1.1 cgd sc->sc_dev.dv_xname);
161 1.1 cgd printf("%s: mesage buffer (via 'options MSGBUFSIZE=...'),\n",
162 1.1 cgd sc->sc_dev.dv_xname);
163 1.1 cgd printf("%s: and report the result with send-pr\n",
164 1.1 cgd sc->sc_dev.dv_xname);
165 1.1 cgd #endif
166 1.1 cgd return;
167 1.1 cgd }
168 1.1 cgd
169 1.1 cgd printf(": %s (", sc->sc_desc->name);
170 1.1 cgd for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++)
171 1.1 cgd printf("%s%s", i ? ", " : "",
172 1.1 cgd puc_port_type_name(sc->sc_desc->ports[i].type));
173 1.1 cgd printf(")\n");
174 1.1 cgd
175 1.1 cgd /*
176 1.1 cgd * XXX This driver assumes that 'com' ports attached to it
177 1.1 cgd * XXX can not be console. That isn't unreasonable, because PCI
178 1.1 cgd * XXX devices are supposed to be dynamically mapped, and com
179 1.1 cgd * XXX console ports want fixed addresses. When/if baseboard
180 1.1 cgd * XXX 'com' ports are identified as PCI/communications/serial
181 1.1 cgd * XXX devices and are known to be mapped at the standard
182 1.1 cgd * XXX addresses, if they can be the system console then we have
183 1.1 cgd * XXX to cope with doing the mapping right. Then this will get
184 1.1 cgd * XXX really ugly. Of course, by then we might know the real
185 1.1 cgd * XXX definition of PCI/communications/serial, and attach 'com'
186 1.1 cgd * XXX directly on PCI.
187 1.1 cgd */
188 1.1 cgd for (i = 0; i < 6; i++) {
189 1.1 cgd pcireg_t bar, type;
190 1.1 cgd
191 1.1 cgd sc->sc_bar_mappings[i].mapped = 0;
192 1.1 cgd
193 1.1 cgd bar = pci_conf_read(pa->pa_pc, pa->pa_tag,
194 1.1 cgd PCI_MAPREG_START + 4 * i); /* XXX const */
195 1.1 cgd if (bar == 0) /* BAR not implemented(?) */
196 1.1 cgd continue;
197 1.1 cgd
198 1.1 cgd type = (PCI_MAPREG_TYPE(bar) == PCI_MAPREG_TYPE_IO ?
199 1.1 cgd PCI_MAPREG_TYPE_IO : PCI_MAPREG_MEM_TYPE(bar));
200 1.1 cgd
201 1.1 cgd sc->sc_bar_mappings[i].mapped = (pci_mapreg_map(pa,
202 1.1 cgd PCI_MAPREG_START + 4 * i, type, 0,
203 1.1 cgd &sc->sc_bar_mappings[i].t, &sc->sc_bar_mappings[i].h,
204 1.1 cgd &sc->sc_bar_mappings[i].a, &sc->sc_bar_mappings[i].s)
205 1.1 cgd == 0);
206 1.1 cgd if (sc->sc_bar_mappings[i].mapped)
207 1.1 cgd continue;
208 1.1 cgd
209 1.1 cgd printf("%s: couldn't map BAR at offset 0x%lx\n",
210 1.1 cgd sc->sc_dev.dv_xname, (long)(PCI_MAPREG_START + 4 * i));
211 1.1 cgd }
212 1.1 cgd
213 1.1 cgd /* Map interrupt. */
214 1.1 cgd if (pci_intr_map(pa->pa_pc, pa->pa_intrtag, pa->pa_intrpin,
215 1.1 cgd pa->pa_intrline, &intrhandle)) {
216 1.1 cgd printf("%s: couldn't map interrupt\n", sc->sc_dev.dv_xname);
217 1.1 cgd return;
218 1.1 cgd }
219 1.1 cgd /*
220 1.1 cgd * XXX the sub-devices establish the interrupts, for the
221 1.1 cgd * XXX following reasons:
222 1.1 cgd * XXX
223 1.1 cgd * XXX * we can't really know what IPLs they'd want
224 1.1 cgd * XXX
225 1.1 cgd * XXX * the MD dispatching code can ("should") dispatch
226 1.1 cgd * XXX chained interrupts better than we can.
227 1.1 cgd * XXX
228 1.1 cgd * XXX It would be nice if we could indicate to the MD interrupt
229 1.1 cgd * XXX handling code that the interrupt line used by the device
230 1.1 cgd * XXX was a PCI (level triggered) interrupt.
231 1.1 cgd * XXX
232 1.1 cgd * XXX It's not pretty, but hey, what is?
233 1.1 cgd */
234 1.1 cgd
235 1.1 cgd /* Configure each port. */
236 1.1 cgd for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) {
237 1.1 cgd
238 1.1 cgd /* make sure the base address register is mapped */
239 1.1 cgd barindex = PUC_PORT_BAR_INDEX(sc->sc_desc->ports[i].bar);
240 1.1 cgd if (!sc->sc_bar_mappings[barindex].mapped) {
241 1.1 cgd printf("%s: %s port uses unmapped BAR (0x%x)\n",
242 1.1 cgd sc->sc_dev.dv_xname,
243 1.1 cgd puc_port_type_name(sc->sc_desc->ports[i].type),
244 1.1 cgd sc->sc_desc->ports[i].bar);
245 1.1 cgd continue;
246 1.1 cgd }
247 1.1 cgd
248 1.1 cgd /* set up to configure the child device */
249 1.1 cgd paa.port = i;
250 1.1 cgd paa.type = sc->sc_desc->ports[i].type;
251 1.1 cgd paa.pc = pa->pa_pc;
252 1.1 cgd paa.intrhandle = intrhandle;
253 1.1 cgd paa.a = sc->sc_bar_mappings[barindex].a;
254 1.1 cgd paa.s = sc->sc_bar_mappings[barindex].s;
255 1.1 cgd paa.t = sc->sc_bar_mappings[barindex].t;
256 1.1 cgd paa.h = sc->sc_bar_mappings[barindex].h;
257 1.1 cgd
258 1.1 cgd #if 0
259 1.1 cgd printf("%s: port %d: %s @ (index %d) 0x%x/%d (0x%lx, 0x%lx)\n",
260 1.1 cgd sc->sc_dev.dv_xname, paa.port,
261 1.1 cgd puc_port_type_name(paa.type), barindex, (int)paa.a,
262 1.1 cgd (int)paa.s, (long)paa.t, (long)paa.h);
263 1.1 cgd #endif
264 1.1 cgd
265 1.1 cgd /* and configure it */
266 1.1 cgd sc->sc_ports[i].dev = config_found_sm(self, &paa, puc_print,
267 1.1 cgd puc_submatch);
268 1.1 cgd }
269 1.1 cgd }
270 1.1 cgd
271 1.1 cgd int
272 1.1 cgd puc_print(aux, pnp)
273 1.1 cgd void *aux;
274 1.1 cgd const char *pnp;
275 1.1 cgd {
276 1.1 cgd struct puc_attach_args *paa = aux;
277 1.1 cgd
278 1.1 cgd if (pnp)
279 1.1 cgd printf("%s at %s", puc_port_type_name(paa->type), pnp);
280 1.1 cgd printf(" port %d", paa->port);
281 1.1 cgd return (UNCONF);
282 1.1 cgd }
283 1.1 cgd
284 1.1 cgd int
285 1.1 cgd puc_submatch(parent, cf, aux)
286 1.1 cgd struct device *parent;
287 1.1 cgd struct cfdata *cf;
288 1.1 cgd void *aux;
289 1.1 cgd {
290 1.1 cgd struct puc_attach_args *aa = aux;
291 1.1 cgd
292 1.1 cgd if (cf->cf_loc[PUCCF_PORT] != PUCCF_PORT_DEFAULT &&
293 1.1 cgd cf->cf_loc[PUCCF_PORT] != aa->port)
294 1.1 cgd return 0;
295 1.1 cgd return ((*cf->cf_attach->ca_match)(parent, cf, aux));
296 1.1 cgd }
297 1.1 cgd
298 1.1 cgd static const struct puc_device_description *
299 1.1 cgd puc_find_description(vend, prod, svend, sprod)
300 1.1 cgd pcireg_t vend, prod, svend, sprod;
301 1.1 cgd {
302 1.1 cgd int i;
303 1.1 cgd
304 1.1 cgd #define checkreg(val, index) \
305 1.1 cgd (((val) & puc_devices[i].rmask[(index)]) == puc_devices[i].rval[(index)])
306 1.1 cgd
307 1.1 cgd for (i = 0; puc_devices[i].name != NULL; i++) {
308 1.1 cgd if (checkreg(vend, PUC_REG_VEND) &&
309 1.1 cgd checkreg(prod, PUC_REG_PROD) &&
310 1.1 cgd checkreg(svend, PUC_REG_SVEND) &&
311 1.1 cgd checkreg(sprod, PUC_REG_SPROD))
312 1.1 cgd return (&puc_devices[i]);
313 1.1 cgd }
314 1.1 cgd
315 1.1 cgd #undef checkreg
316 1.1 cgd
317 1.1 cgd return (NULL);
318 1.1 cgd }
319 1.1 cgd
320 1.1 cgd static const char *
321 1.1 cgd puc_port_type_name(type)
322 1.1 cgd int type;
323 1.1 cgd {
324 1.1 cgd
325 1.1 cgd switch (type) {
326 1.1 cgd case PUC_PORT_TYPE_COM:
327 1.1 cgd return "com";
328 1.1 cgd case PUC_PORT_TYPE_LPT:
329 1.1 cgd return "lpt";
330 1.1 cgd default:
331 1.1 cgd panic("puc_port_type_name %d", type);
332 1.1 cgd }
333 1.1 cgd }
334