puc.c revision 1.33 1 /* $NetBSD: puc.c,v 1.33 2013/07/22 13:40:36 soren 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.33 2013/07/22 13:40:36 soren 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 <dev/pci/pcidevs.h>
66 #include <sys/termios.h>
67 #include <dev/ic/comreg.h>
68 #include <dev/ic/comvar.h>
69
70 #include "locators.h"
71 #include "com.h"
72
73 struct puc_softc {
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 device_t dev;
90
91 /* filled in by port attachments */
92 int (*ihand)(void *);
93 void *ihandarg;
94 } sc_ports[PUC_MAX_PORTS];
95 };
96
97 static int puc_print(void *, const char *);
98
99 static const char *puc_port_type_name(int);
100
101 static int
102 puc_match(device_t parent, cfdata_t match, void *aux)
103 {
104 struct pci_attach_args *pa = aux;
105 const struct puc_device_description *desc;
106 pcireg_t bhlc, subsys;
107
108 bhlc = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_BHLC_REG);
109 if (PCI_HDRTYPE_TYPE(bhlc) != 0)
110 return (0);
111
112 subsys = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG);
113
114 desc = puc_find_description(PCI_VENDOR(pa->pa_id),
115 PCI_PRODUCT(pa->pa_id), PCI_VENDOR(subsys), PCI_PRODUCT(subsys));
116 if (desc != NULL)
117 return (10);
118
119 #if 0
120 /*
121 * XXX this is obviously bogus. eventually, we might want
122 * XXX to match communications/modem, etc., but that needs some
123 * XXX special work in the match fn.
124 */
125 /*
126 * Match class/subclass, so we can tell people to compile kernel
127 * with options that cause this driver to spew.
128 */
129 if (PCI_CLASS(pa->pa_class) == PCI_CLASS_COMMUNICATIONS) {
130 switch (PCI_SUBCLASS(pa->pa_class)) {
131 case PCI_SUBCLASS_COMMUNICATIONS_SERIAL:
132 case PCI_SUBCLASS_COMMUNICATIONS_MODEM:
133 return (1);
134 }
135 }
136 #endif
137
138 return (0);
139 }
140
141 static void
142 puc_attach(device_t parent, device_t self, void *aux)
143 {
144 struct puc_softc *sc = device_private(self);
145 struct pci_attach_args *pa = aux;
146 struct puc_attach_args paa;
147 pci_intr_handle_t intrhandle;
148 pcireg_t subsys;
149 int i, barindex;
150 int locs[PUCCF_NLOCS];
151
152 sc->sc_pc = pa->pa_pc;
153
154 subsys = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG);
155 sc->sc_desc = puc_find_description(PCI_VENDOR(pa->pa_id),
156 PCI_PRODUCT(pa->pa_id), PCI_VENDOR(subsys), PCI_PRODUCT(subsys));
157 if (sc->sc_desc == NULL) {
158 /*
159 * This was a class/subclass match, so tell people to compile
160 * kernel with options that cause this driver to spew.
161 */
162 #ifdef PUC_PRINT_REGS
163 printf(":\n");
164 pci_conf_print(pa->pa_pc, pa->pa_tag, NULL);
165 #else
166 printf(": unknown PCI communications device\n");
167 printf("%s: compile kernel with PUC_PRINT_REGS and larger\n",
168 device_xname(self));
169 printf("%s: mesage buffer (via 'options MSGBUFSIZE=...'),\n",
170 device_xname(self));
171 printf("%s: and report the result with send-pr\n",
172 device_xname(self));
173 #endif
174 return;
175 }
176
177 aprint_naive("\n");
178 aprint_normal(": %s (", sc->sc_desc->name);
179 for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++)
180 aprint_normal("%s%s", i ? ", " : "",
181 puc_port_type_name(sc->sc_desc->ports[i].type));
182 aprint_normal(")\n");
183
184 for (i = 0; i < 6; i++) {
185 pcireg_t bar, type;
186
187 sc->sc_bar_mappings[i].mapped = 0;
188
189 bar = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_BAR(i));
190 if (bar == 0) /* BAR not implemented(?) */
191 continue;
192
193 type = (PCI_MAPREG_TYPE(bar) == PCI_MAPREG_TYPE_IO ?
194 PCI_MAPREG_TYPE_IO : PCI_MAPREG_MEM_TYPE(bar));
195
196 if (type == PCI_MAPREG_TYPE_IO) {
197 sc->sc_bar_mappings[i].t = pa->pa_iot;
198 sc->sc_bar_mappings[i].a = PCI_MAPREG_IO_ADDR(bar);
199 sc->sc_bar_mappings[i].s = PCI_MAPREG_IO_SIZE(bar);
200 } else {
201 sc->sc_bar_mappings[i].t = pa->pa_memt;
202 sc->sc_bar_mappings[i].a = PCI_MAPREG_MEM_ADDR(bar);
203 sc->sc_bar_mappings[i].s = PCI_MAPREG_MEM_SIZE(bar);
204 }
205
206 sc->sc_bar_mappings[i].mapped = (pci_mapreg_map(pa,
207 PCI_BAR(i), type, 0,
208 &sc->sc_bar_mappings[i].t, &sc->sc_bar_mappings[i].h,
209 &sc->sc_bar_mappings[i].a, &sc->sc_bar_mappings[i].s)
210 == 0);
211 if (sc->sc_bar_mappings[i].mapped)
212 continue;
213
214 aprint_debug_dev(self, "couldn't map BAR at offset 0x%lx\n",
215 (long)(PCI_MAPREG_START + 4 * i));
216 }
217
218 /* Map interrupt. */
219 if (pci_intr_map(pa, &intrhandle)) {
220 aprint_error_dev(self, "couldn't map interrupt\n");
221 return;
222 }
223 /*
224 * XXX the sub-devices establish the interrupts, for the
225 * XXX following reasons:
226 * XXX
227 * XXX * we can't really know what IPLs they'd want
228 * XXX
229 * XXX * the MD dispatching code can ("should") dispatch
230 * XXX chained interrupts better than we can.
231 * XXX
232 * XXX It would be nice if we could indicate to the MD interrupt
233 * XXX handling code that the interrupt line used by the device
234 * XXX was a PCI (level triggered) interrupt.
235 * XXX
236 * XXX It's not pretty, but hey, what is?
237 */
238
239 /* SB16C10xx board specific initialization */
240 if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_SYSTEMBASE &&
241 (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_SYSTEMBASE_SB16C1054 ||
242 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_SYSTEMBASE_SB16C1058)) {
243 if (!sc->sc_bar_mappings[1].mapped) {
244 aprint_error_dev(self,
245 "optional register is not mapped\n");
246 return;
247 }
248 #define SB16C105X_OPT_IMRREG0 0x0000000c
249 /* enable port 0-7 interrupt */
250 bus_space_write_1(sc->sc_bar_mappings[1].t,
251 sc->sc_bar_mappings[1].h, SB16C105X_OPT_IMRREG0, 0xff);
252 } else {
253 if (!pmf_device_register(self, NULL, NULL);
254 aprint_error_dev(self,
255 "couldn't establish power handler\n");
256 }
257
258 /* Configure each port. */
259 for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) {
260 barindex = PUC_PORT_BAR_INDEX(sc->sc_desc->ports[i].bar);
261 bus_space_handle_t subregion_handle;
262 int is_console = 0;
263
264 /* make sure the base address register is mapped */
265 #if NCOM > 0
266 is_console = com_is_console(sc->sc_bar_mappings[barindex].t,
267 sc->sc_bar_mappings[barindex].a +
268 sc->sc_desc->ports[i].offset, &subregion_handle);
269 if (is_console) {
270 sc->sc_bar_mappings[barindex].mapped = 1;
271 sc->sc_bar_mappings[barindex].h = subregion_handle -
272 sc->sc_desc->ports[i].offset; /* XXX hack */
273 }
274 #endif
275 if (!sc->sc_bar_mappings[barindex].mapped) {
276 aprint_error_dev(self,
277 "%s port uses unmapped BAR (0x%x)\n",
278 puc_port_type_name(sc->sc_desc->ports[i].type),
279 sc->sc_desc->ports[i].bar);
280 continue;
281 }
282
283 /* set up to configure the child device */
284 paa.port = i;
285 paa.type = sc->sc_desc->ports[i].type;
286 paa.flags = sc->sc_desc->ports[i].flags;
287 paa.pc = pa->pa_pc;
288 paa.tag = pa->pa_tag;
289 paa.intrhandle = intrhandle;
290 paa.a = sc->sc_bar_mappings[barindex].a +
291 sc->sc_desc->ports[i].offset;
292 paa.t = sc->sc_bar_mappings[barindex].t;
293 paa.dmat = pa->pa_dmat;
294 paa.dmat64 = pa->pa_dmat64;
295
296 if (!is_console &&
297 bus_space_subregion(sc->sc_bar_mappings[barindex].t,
298 sc->sc_bar_mappings[barindex].h,
299 sc->sc_desc->ports[i].offset,
300 sc->sc_bar_mappings[barindex].s -
301 sc->sc_desc->ports[i].offset,
302 &subregion_handle) != 0) {
303 aprint_error_dev(self, "couldn't get subregion for port %d\n", i);
304 continue;
305 }
306 paa.h = subregion_handle;
307
308 #if 0
309 printf("%s: port %d: %s @ (index %d) 0x%x (0x%lx, 0x%lx)\n",
310 device_xname(self), paa.port,
311 puc_port_type_name(paa.type), barindex, (int)paa.a,
312 (long)paa.t, (long)paa.h);
313 #endif
314
315 locs[PUCCF_PORT] = i;
316
317 /* and configure it */
318 sc->sc_ports[i].dev = config_found_sm_loc(self, "puc", locs,
319 &paa, puc_print, config_stdsubmatch);
320 }
321 }
322
323 CFATTACH_DECL_NEW(puc, sizeof(struct puc_softc),
324 puc_match, puc_attach, NULL, NULL);
325
326 static int
327 puc_print(void *aux, const char *pnp)
328 {
329 struct puc_attach_args *paa = aux;
330
331 if (pnp)
332 aprint_normal("%s at %s", puc_port_type_name(paa->type), pnp);
333 aprint_normal(" port %d", paa->port);
334 return (UNCONF);
335 }
336
337 const struct puc_device_description *
338 puc_find_description(pcireg_t vend, pcireg_t prod, pcireg_t svend,
339 pcireg_t 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(int type)
361 {
362
363 switch (type) {
364 case PUC_PORT_TYPE_COM:
365 return "com";
366 case PUC_PORT_TYPE_LPT:
367 return "lpt";
368 default:
369 panic("puc_port_type_name %d", type);
370 }
371 }
372