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