xref: /netbsd-src/sys/dev/pci/puc.c (revision b7b7574d3bf8eeb51a1fa3977b59142ec6434a55)
1 /*	$NetBSD: puc.c,v 1.37 2014/02/07 11:51:00 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.37 2014/02/07 11:51:00 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 
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 	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_SB16C1054 ||
240 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_SYSTEMBASE_SB16C1058)) {
241 		if (!sc->sc_bar_mappings[1].mapped) {
242 			aprint_error_dev(self,
243 			    "optional register is not mapped\n");
244 			return;
245 		}
246 #define SB16C105X_OPT_IMRREG0 0x0000000c
247 		/* enable port 0-7 interrupt */
248 		bus_space_write_1(sc->sc_bar_mappings[1].t,
249 		    sc->sc_bar_mappings[1].h, SB16C105X_OPT_IMRREG0, 0xff);
250 	} else {
251 		if (!pmf_device_register(self, NULL, NULL))
252 	                aprint_error_dev(self,
253 			    "couldn't establish power handler\n");
254 	}
255 
256 	/* Configure each port. */
257 	for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) {
258 		barindex = PUC_PORT_BAR_INDEX(sc->sc_desc->ports[i].bar);
259 		bus_space_handle_t subregion_handle;
260 		int is_console = 0;
261 
262 		/* make sure the base address register is mapped */
263 #if NCOM > 0
264 		is_console = com_is_console(sc->sc_bar_mappings[barindex].t,
265 		    sc->sc_bar_mappings[barindex].a +
266 		    sc->sc_desc->ports[i].offset, &subregion_handle);
267 		if (is_console) {
268 			sc->sc_bar_mappings[barindex].mapped = 1;
269 #if defined(amd64) || defined(i386)
270 			sc->sc_bar_mappings[barindex].h = subregion_handle -
271 			    sc->sc_desc->ports[i].offset;	/* XXX hack */
272 #endif
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