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