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