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