puc.c revision 1.12
1/*	$NetBSD: puc.c,v 1.12 2001/11/13 07:48:48 lukem 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.12 2001/11/13 07:48:48 lukem 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 "opt_puccn.h"
70
71struct puc_softc {
72	struct device		sc_dev;
73
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		struct device	*dev;
90
91                /* filled in by port attachments */
92                int             (*ihand) __P((void *));
93                void            *ihandarg;
94        } sc_ports[PUC_MAX_PORTS];
95};
96
97int	puc_match __P((struct device *, struct cfdata *, void *));
98void	puc_attach __P((struct device *, struct device *, void *));
99int	puc_print __P((void *, const char *));
100int	puc_submatch __P((struct device *, struct cfdata *, void *));
101
102struct cfattach puc_ca = {
103	sizeof(struct puc_softc), puc_match, puc_attach
104};
105
106const struct puc_device_description *
107	puc_find_description __P((pcireg_t, pcireg_t, pcireg_t, pcireg_t));
108static const char *
109	puc_port_type_name __P((int));
110
111int
112puc_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
154void
155puc_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.intrhandle = intrhandle;
283		paa.a = sc->sc_bar_mappings[barindex].a;
284		paa.t = sc->sc_bar_mappings[barindex].t;
285
286		if (
287#ifdef PUCCN
288		    !com_is_console(sc->sc_bar_mappings[barindex].t,
289		    sc->sc_bar_mappings[barindex].a, &subregion_handle)
290		   &&
291#endif
292		    bus_space_subregion(sc->sc_bar_mappings[barindex].t,
293		    sc->sc_bar_mappings[barindex].h,
294		    sc->sc_desc->ports[i].offset,
295		    sc->sc_bar_mappings[barindex].s -
296		      sc->sc_desc->ports[i].offset,
297		    &subregion_handle) != 0) {
298			printf("%s: couldn't get subregion for port %d\n",
299			    sc->sc_dev.dv_xname, i);
300			continue;
301		}
302		paa.h = subregion_handle;
303
304#if 0
305		printf("%s: port %d: %s @ (index %d) 0x%x (0x%lx, 0x%lx)\n",
306		    sc->sc_dev.dv_xname, paa.port,
307		    puc_port_type_name(paa.type), barindex, (int)paa.a,
308		    (long)paa.t, (long)paa.h);
309#endif
310
311		/* and configure it */
312		sc->sc_ports[i].dev = config_found_sm(self, &paa, puc_print,
313		    puc_submatch);
314	}
315}
316
317int
318puc_print(aux, pnp)
319	void *aux;
320	const char *pnp;
321{
322	struct puc_attach_args *paa = aux;
323
324	if (pnp)
325		printf("%s at %s", puc_port_type_name(paa->type), pnp);
326	printf(" port %d", paa->port);
327	return (UNCONF);
328}
329
330int
331puc_submatch(parent, cf, aux)
332	struct device *parent;
333	struct cfdata *cf;
334	void *aux;
335{
336	struct puc_attach_args *aa = aux;
337
338	if (cf->cf_loc[PUCCF_PORT] != PUCCF_PORT_DEFAULT &&
339	    cf->cf_loc[PUCCF_PORT] != aa->port)
340		return 0;
341	return ((*cf->cf_attach->ca_match)(parent, cf, aux));
342}
343
344const struct puc_device_description *
345puc_find_description(vend, prod, svend, sprod)
346	pcireg_t vend, prod, svend, sprod;
347{
348	int i;
349
350#define checkreg(val, index) \
351    (((val) & puc_devices[i].rmask[(index)]) == puc_devices[i].rval[(index)])
352
353	for (i = 0; puc_devices[i].name != NULL; i++) {
354		if (checkreg(vend, PUC_REG_VEND) &&
355		    checkreg(prod, PUC_REG_PROD) &&
356		    checkreg(svend, PUC_REG_SVEND) &&
357		    checkreg(sprod, PUC_REG_SPROD))
358			return (&puc_devices[i]);
359	}
360
361#undef checkreg
362
363	return (NULL);
364}
365
366static const char *
367puc_port_type_name(type)
368	int type;
369{
370
371	switch (type) {
372	case PUC_PORT_TYPE_COM:
373		return "com";
374	case PUC_PORT_TYPE_LPT:
375		return "lpt";
376	default:
377		panic("puc_port_type_name %d", type);
378	}
379}
380