puc.c revision 1.15 1 /* $NetBSD: puc.c,v 1.15 2002/09/30 20:38:02 thorpej 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/cdefs.h>
56 __KERNEL_RCSID(0, "$NetBSD: puc.c,v 1.15 2002/09/30 20:38:02 thorpej Exp $");
57
58 #include <sys/param.h>
59 #include <sys/systm.h>
60 #include <sys/device.h>
61
62 #include <dev/pci/pcireg.h>
63 #include <dev/pci/pcivar.h>
64 #include <dev/pci/pucvar.h>
65 #include <sys/termios.h>
66 #include <dev/ic/comreg.h>
67 #include <dev/ic/comvar.h>
68
69 #include "opt_puccn.h"
70
71 struct puc_softc {
72 struct device sc_dev;
73
74 /* static configuration data */
75 const struct puc_device_description *sc_desc;
76
77 /* card-global dynamic data */
78 void *sc_ih;
79 struct {
80 int mapped;
81 bus_addr_t a;
82 bus_size_t s;
83 bus_space_tag_t t;
84 bus_space_handle_t h;
85 } sc_bar_mappings[6]; /* XXX constant */
86
87 /* per-port dynamic data */
88 struct {
89 struct device *dev;
90
91 /* filled in by port attachments */
92 int (*ihand) __P((void *));
93 void *ihandarg;
94 } sc_ports[PUC_MAX_PORTS];
95 };
96
97 int puc_match __P((struct device *, struct cfdata *, void *));
98 void puc_attach __P((struct device *, struct device *, void *));
99 int puc_print __P((void *, const char *));
100 int puc_submatch __P((struct device *, struct cfdata *, void *));
101
102 CFATTACH_DECL(puc, sizeof(struct puc_softc),
103 puc_match, puc_attach, NULL, NULL)
104
105 const struct puc_device_description *
106 puc_find_description __P((pcireg_t, pcireg_t, pcireg_t, pcireg_t));
107 static const char *
108 puc_port_type_name __P((int));
109
110 int
111 puc_match(parent, match, aux)
112 struct device *parent;
113 struct cfdata *match;
114 void *aux;
115 {
116 struct pci_attach_args *pa = aux;
117 const struct puc_device_description *desc;
118 pcireg_t bhlc, subsys;
119
120 bhlc = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_BHLC_REG);
121 if (PCI_HDRTYPE_TYPE(bhlc) != 0)
122 return (0);
123
124 subsys = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG);
125
126 desc = puc_find_description(PCI_VENDOR(pa->pa_id),
127 PCI_PRODUCT(pa->pa_id), PCI_VENDOR(subsys), PCI_PRODUCT(subsys));
128 if (desc != NULL)
129 return (10);
130
131 #if 0
132 /*
133 * XXX this is obviously bogus. eventually, we might want
134 * XXX to match communications/modem, etc., but that needs some
135 * XXX special work in the match fn.
136 */
137 /*
138 * Match class/subclass, so we can tell people to compile kernel
139 * with options that cause this driver to spew.
140 */
141 if (PCI_CLASS(pa->pa_class) == PCI_CLASS_COMMUNICATIONS) {
142 switch (PCI_SUBCLASS(pa->pa_class)) {
143 case PCI_SUBCLASS_COMMUNICATIONS_SERIAL:
144 case PCI_SUBCLASS_COMMUNICATIONS_MODEM:
145 return (1);
146 }
147 }
148 #endif
149
150 return (0);
151 }
152
153 void
154 puc_attach(parent, self, aux)
155 struct device *parent, *self;
156 void *aux;
157 {
158 struct puc_softc *sc = (struct puc_softc *)self;
159 struct pci_attach_args *pa = aux;
160 struct puc_attach_args paa;
161 pci_intr_handle_t intrhandle;
162 pcireg_t subsys;
163 int i, barindex;
164 bus_addr_t base;
165 bus_space_tag_t tag;
166 #ifdef PUCCN
167 bus_space_handle_t ioh;
168 #endif
169
170 subsys = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG);
171 sc->sc_desc = puc_find_description(PCI_VENDOR(pa->pa_id),
172 PCI_PRODUCT(pa->pa_id), PCI_VENDOR(subsys), PCI_PRODUCT(subsys));
173 if (sc->sc_desc == NULL) {
174 /*
175 * This was a class/subclass match, so tell people to compile
176 * kernel with options that cause this driver to spew.
177 */
178 #ifdef PUC_PRINT_REGS
179 printf(":\n");
180 pci_conf_print(pa->pa_pc, pa->pa_tag, NULL);
181 #else
182 printf(": unknown PCI communications device\n");
183 printf("%s: compile kernel with PUC_PRINT_REGS and larger\n",
184 sc->sc_dev.dv_xname);
185 printf("%s: mesage buffer (via 'options MSGBUFSIZE=...'),\n",
186 sc->sc_dev.dv_xname);
187 printf("%s: and report the result with send-pr\n",
188 sc->sc_dev.dv_xname);
189 #endif
190 return;
191 }
192
193 printf(": %s (", sc->sc_desc->name);
194 for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++)
195 printf("%s%s", i ? ", " : "",
196 puc_port_type_name(sc->sc_desc->ports[i].type));
197 printf(")\n");
198
199 for (i = 0; i < 6; i++) {
200 pcireg_t bar, type;
201
202 sc->sc_bar_mappings[i].mapped = 0;
203
204 bar = pci_conf_read(pa->pa_pc, pa->pa_tag,
205 PCI_MAPREG_START + 4 * i); /* XXX const */
206 if (bar == 0) /* BAR not implemented(?) */
207 continue;
208
209 type = (PCI_MAPREG_TYPE(bar) == PCI_MAPREG_TYPE_IO ?
210 PCI_MAPREG_TYPE_IO : PCI_MAPREG_MEM_TYPE(bar));
211
212 if (type == PCI_MAPREG_TYPE_IO) {
213 tag = pa->pa_iot;
214 base = PCI_MAPREG_IO_ADDR(bar);
215 } else {
216 tag = pa->pa_memt;
217 base = PCI_MAPREG_MEM_ADDR(bar);
218 }
219 #ifdef PUCCN
220 if (com_is_console(tag, base, &ioh)) {
221 sc->sc_bar_mappings[i].mapped = 1;
222 sc->sc_bar_mappings[i].a = base;
223 sc->sc_bar_mappings[i].s = COM_NPORTS;
224 sc->sc_bar_mappings[i].t = tag;
225 sc->sc_bar_mappings[i].h = ioh;
226 continue;
227 }
228 #endif
229 sc->sc_bar_mappings[i].mapped = (pci_mapreg_map(pa,
230 PCI_MAPREG_START + 4 * i, type, 0,
231 &sc->sc_bar_mappings[i].t, &sc->sc_bar_mappings[i].h,
232 &sc->sc_bar_mappings[i].a, &sc->sc_bar_mappings[i].s)
233 == 0);
234 if (sc->sc_bar_mappings[i].mapped)
235 continue;
236
237 printf("%s: couldn't map BAR at offset 0x%lx\n",
238 sc->sc_dev.dv_xname, (long)(PCI_MAPREG_START + 4 * i));
239 }
240
241 /* Map interrupt. */
242 if (pci_intr_map(pa, &intrhandle)) {
243 printf("%s: couldn't map interrupt\n", sc->sc_dev.dv_xname);
244 return;
245 }
246 /*
247 * XXX the sub-devices establish the interrupts, for the
248 * XXX following reasons:
249 * XXX
250 * XXX * we can't really know what IPLs they'd want
251 * XXX
252 * XXX * the MD dispatching code can ("should") dispatch
253 * XXX chained interrupts better than we can.
254 * XXX
255 * XXX It would be nice if we could indicate to the MD interrupt
256 * XXX handling code that the interrupt line used by the device
257 * XXX was a PCI (level triggered) interrupt.
258 * XXX
259 * XXX It's not pretty, but hey, what is?
260 */
261
262 /* Configure each port. */
263 for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) {
264 bus_space_handle_t subregion_handle;
265
266 /* make sure the base address register is mapped */
267 barindex = PUC_PORT_BAR_INDEX(sc->sc_desc->ports[i].bar);
268 if (!sc->sc_bar_mappings[barindex].mapped) {
269 printf("%s: %s port uses unmapped BAR (0x%x)\n",
270 sc->sc_dev.dv_xname,
271 puc_port_type_name(sc->sc_desc->ports[i].type),
272 sc->sc_desc->ports[i].bar);
273 continue;
274 }
275
276 /* set up to configure the child device */
277 paa.port = i;
278 paa.type = sc->sc_desc->ports[i].type;
279 paa.flags = sc->sc_desc->ports[i].flags;
280 paa.pc = pa->pa_pc;
281 paa.intrhandle = intrhandle;
282 paa.a = sc->sc_bar_mappings[barindex].a;
283 paa.t = sc->sc_bar_mappings[barindex].t;
284
285 if (
286 #ifdef PUCCN
287 !com_is_console(sc->sc_bar_mappings[barindex].t,
288 sc->sc_bar_mappings[barindex].a, &subregion_handle)
289 &&
290 #endif
291 bus_space_subregion(sc->sc_bar_mappings[barindex].t,
292 sc->sc_bar_mappings[barindex].h,
293 sc->sc_desc->ports[i].offset,
294 sc->sc_bar_mappings[barindex].s -
295 sc->sc_desc->ports[i].offset,
296 &subregion_handle) != 0) {
297 printf("%s: couldn't get subregion for port %d\n",
298 sc->sc_dev.dv_xname, i);
299 continue;
300 }
301 paa.h = subregion_handle;
302
303 #if 0
304 printf("%s: port %d: %s @ (index %d) 0x%x (0x%lx, 0x%lx)\n",
305 sc->sc_dev.dv_xname, paa.port,
306 puc_port_type_name(paa.type), barindex, (int)paa.a,
307 (long)paa.t, (long)paa.h);
308 #endif
309
310 /* and configure it */
311 sc->sc_ports[i].dev = config_found_sm(self, &paa, puc_print,
312 puc_submatch);
313 }
314 }
315
316 int
317 puc_print(aux, pnp)
318 void *aux;
319 const char *pnp;
320 {
321 struct puc_attach_args *paa = aux;
322
323 if (pnp)
324 printf("%s at %s", puc_port_type_name(paa->type), pnp);
325 printf(" port %d", paa->port);
326 return (UNCONF);
327 }
328
329 int
330 puc_submatch(parent, cf, aux)
331 struct device *parent;
332 struct cfdata *cf;
333 void *aux;
334 {
335 struct puc_attach_args *aa = aux;
336
337 if (cf->cf_loc[PUCCF_PORT] != PUCCF_PORT_DEFAULT &&
338 cf->cf_loc[PUCCF_PORT] != aa->port)
339 return 0;
340 return (config_match(parent, cf, aux));
341 }
342
343 const struct puc_device_description *
344 puc_find_description(vend, prod, svend, sprod)
345 pcireg_t vend, prod, svend, sprod;
346 {
347 int i;
348
349 #define checkreg(val, index) \
350 (((val) & puc_devices[i].rmask[(index)]) == puc_devices[i].rval[(index)])
351
352 for (i = 0; puc_devices[i].name != NULL; i++) {
353 if (checkreg(vend, PUC_REG_VEND) &&
354 checkreg(prod, PUC_REG_PROD) &&
355 checkreg(svend, PUC_REG_SVEND) &&
356 checkreg(sprod, PUC_REG_SPROD))
357 return (&puc_devices[i]);
358 }
359
360 #undef checkreg
361
362 return (NULL);
363 }
364
365 static const char *
366 puc_port_type_name(type)
367 int type;
368 {
369
370 switch (type) {
371 case PUC_PORT_TYPE_COM:
372 return "com";
373 case PUC_PORT_TYPE_LPT:
374 return "lpt";
375 default:
376 panic("puc_port_type_name %d", type);
377 }
378 }
379