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