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