puc.c revision 1.10 1 /* $NetBSD: puc.c,v 1.10 2001/01/03 15:13:15 bouyer 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 PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_BRIDGE_PCI)
141 return (1);
142 #endif
143
144 return (0);
145 }
146
147 void
148 puc_attach(parent, self, aux)
149 struct device *parent, *self;
150 void *aux;
151 {
152 struct puc_softc *sc = (struct puc_softc *)self;
153 struct pci_attach_args *pa = aux;
154 struct puc_attach_args paa;
155 pci_intr_handle_t intrhandle;
156 pcireg_t subsys;
157 int i, barindex;
158 bus_addr_t base;
159 bus_space_tag_t tag;
160 #ifdef PUCCN
161 bus_space_handle_t ioh;
162 #endif
163
164 subsys = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG);
165 sc->sc_desc = puc_find_description(PCI_VENDOR(pa->pa_id),
166 PCI_PRODUCT(pa->pa_id), PCI_VENDOR(subsys), PCI_PRODUCT(subsys));
167 if (sc->sc_desc == NULL) {
168 /*
169 * This was a class/subclass match, so tell people to compile
170 * kernel with options that cause this driver to spew.
171 */
172 #ifdef PUC_PRINT_REGS
173 printf(":\n");
174 pci_conf_print(pa->pa_pc, pa->pa_tag, NULL);
175 #else
176 printf(": unknown PCI communications device\n");
177 printf("%s: compile kernel with PUC_PRINT_REGS and larger\n",
178 sc->sc_dev.dv_xname);
179 printf("%s: mesage buffer (via 'options MSGBUFSIZE=...'),\n",
180 sc->sc_dev.dv_xname);
181 printf("%s: and report the result with send-pr\n",
182 sc->sc_dev.dv_xname);
183 #endif
184 return;
185 }
186
187 printf(": %s (", sc->sc_desc->name);
188 for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++)
189 printf("%s%s", i ? ", " : "",
190 puc_port_type_name(sc->sc_desc->ports[i].type));
191 printf(")\n");
192
193 for (i = 0; i < 6; i++) {
194 pcireg_t bar, type;
195
196 sc->sc_bar_mappings[i].mapped = 0;
197
198 bar = pci_conf_read(pa->pa_pc, pa->pa_tag,
199 PCI_MAPREG_START + 4 * i); /* XXX const */
200 if (bar == 0) /* BAR not implemented(?) */
201 continue;
202
203 type = (PCI_MAPREG_TYPE(bar) == PCI_MAPREG_TYPE_IO ?
204 PCI_MAPREG_TYPE_IO : PCI_MAPREG_MEM_TYPE(bar));
205
206 if (type == PCI_MAPREG_TYPE_IO) {
207 tag = pa->pa_iot;
208 base = PCI_MAPREG_IO_ADDR(bar);
209 } else {
210 tag = pa->pa_memt;
211 base = PCI_MAPREG_MEM_ADDR(bar);
212 }
213 #ifdef PUCCN
214 if (com_is_console(tag, base, &ioh)) {
215 sc->sc_bar_mappings[i].mapped = 1;
216 sc->sc_bar_mappings[i].a = base;
217 sc->sc_bar_mappings[i].s = COM_NPORTS;
218 sc->sc_bar_mappings[i].t = tag;
219 sc->sc_bar_mappings[i].h = ioh;
220 continue;
221 }
222 #endif
223 sc->sc_bar_mappings[i].mapped = (pci_mapreg_map(pa,
224 PCI_MAPREG_START + 4 * i, type, 0,
225 &sc->sc_bar_mappings[i].t, &sc->sc_bar_mappings[i].h,
226 &sc->sc_bar_mappings[i].a, &sc->sc_bar_mappings[i].s)
227 == 0);
228 if (sc->sc_bar_mappings[i].mapped)
229 continue;
230
231 printf("%s: couldn't map BAR at offset 0x%lx\n",
232 sc->sc_dev.dv_xname, (long)(PCI_MAPREG_START + 4 * i));
233 }
234
235 /* Map interrupt. */
236 if (pci_intr_map(pa, &intrhandle)) {
237 printf("%s: couldn't map interrupt\n", sc->sc_dev.dv_xname);
238 return;
239 }
240 /*
241 * XXX the sub-devices establish the interrupts, for the
242 * XXX following reasons:
243 * XXX
244 * XXX * we can't really know what IPLs they'd want
245 * XXX
246 * XXX * the MD dispatching code can ("should") dispatch
247 * XXX chained interrupts better than we can.
248 * XXX
249 * XXX It would be nice if we could indicate to the MD interrupt
250 * XXX handling code that the interrupt line used by the device
251 * XXX was a PCI (level triggered) interrupt.
252 * XXX
253 * XXX It's not pretty, but hey, what is?
254 */
255
256 /* Configure each port. */
257 for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) {
258 bus_space_handle_t subregion_handle;
259
260 /* make sure the base address register is mapped */
261 barindex = PUC_PORT_BAR_INDEX(sc->sc_desc->ports[i].bar);
262 if (!sc->sc_bar_mappings[barindex].mapped) {
263 printf("%s: %s port uses unmapped BAR (0x%x)\n",
264 sc->sc_dev.dv_xname,
265 puc_port_type_name(sc->sc_desc->ports[i].type),
266 sc->sc_desc->ports[i].bar);
267 continue;
268 }
269
270 /* set up to configure the child device */
271 paa.port = i;
272 paa.type = sc->sc_desc->ports[i].type;
273 paa.flags = sc->sc_desc->ports[i].flags;
274 paa.pc = pa->pa_pc;
275 paa.intrhandle = intrhandle;
276 paa.a = sc->sc_bar_mappings[barindex].a;
277 paa.t = sc->sc_bar_mappings[barindex].t;
278
279 if (
280 #ifdef PUCCN
281 !com_is_console(sc->sc_bar_mappings[barindex].t,
282 sc->sc_bar_mappings[barindex].a, &subregion_handle)
283 &&
284 #endif
285 bus_space_subregion(sc->sc_bar_mappings[barindex].t,
286 sc->sc_bar_mappings[barindex].h,
287 sc->sc_desc->ports[i].offset,
288 sc->sc_bar_mappings[barindex].s -
289 sc->sc_desc->ports[i].offset,
290 &subregion_handle) != 0) {
291 printf("%s: couldn't get subregion for port %d\n",
292 sc->sc_dev.dv_xname, i);
293 continue;
294 }
295 paa.h = subregion_handle;
296
297 #if 0
298 printf("%s: port %d: %s @ (index %d) 0x%x (0x%lx, 0x%lx)\n",
299 sc->sc_dev.dv_xname, paa.port,
300 puc_port_type_name(paa.type), barindex, (int)paa.a,
301 (long)paa.t, (long)paa.h);
302 #endif
303
304 /* and configure it */
305 sc->sc_ports[i].dev = config_found_sm(self, &paa, puc_print,
306 puc_submatch);
307 }
308 }
309
310 int
311 puc_print(aux, pnp)
312 void *aux;
313 const char *pnp;
314 {
315 struct puc_attach_args *paa = aux;
316
317 if (pnp)
318 printf("%s at %s", puc_port_type_name(paa->type), pnp);
319 printf(" port %d", paa->port);
320 return (UNCONF);
321 }
322
323 int
324 puc_submatch(parent, cf, aux)
325 struct device *parent;
326 struct cfdata *cf;
327 void *aux;
328 {
329 struct puc_attach_args *aa = aux;
330
331 if (cf->cf_loc[PUCCF_PORT] != PUCCF_PORT_DEFAULT &&
332 cf->cf_loc[PUCCF_PORT] != aa->port)
333 return 0;
334 return ((*cf->cf_attach->ca_match)(parent, cf, aux));
335 }
336
337 const struct puc_device_description *
338 puc_find_description(vend, prod, svend, sprod)
339 pcireg_t vend, prod, svend, sprod;
340 {
341 int i;
342
343 #define checkreg(val, index) \
344 (((val) & puc_devices[i].rmask[(index)]) == puc_devices[i].rval[(index)])
345
346 for (i = 0; puc_devices[i].name != NULL; i++) {
347 if (checkreg(vend, PUC_REG_VEND) &&
348 checkreg(prod, PUC_REG_PROD) &&
349 checkreg(svend, PUC_REG_SVEND) &&
350 checkreg(sprod, PUC_REG_SPROD))
351 return (&puc_devices[i]);
352 }
353
354 #undef checkreg
355
356 return (NULL);
357 }
358
359 static const char *
360 puc_port_type_name(type)
361 int type;
362 {
363
364 switch (type) {
365 case PUC_PORT_TYPE_COM:
366 return "com";
367 case PUC_PORT_TYPE_LPT:
368 return "lpt";
369 default:
370 panic("puc_port_type_name %d", type);
371 }
372 }
373