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