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