puc.c revision 102734
1/*	$NetBSD: puc.c,v 1.7 2000/07/29 17:43:38 jlam Exp $	*/
2
3/*-
4 * Copyright (c) 2002 JF Hay.  All rights reserved.
5 * Copyright (c) 2000 M. Warner Losh.  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 unmodified, this list of conditions, and the following
12 *    disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/*
30 * Copyright (c) 1996, 1998, 1999
31 *	Christopher G. Demetriou.  All rights reserved.
32 *
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
35 * are met:
36 * 1. Redistributions of source code must retain the above copyright
37 *    notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 *    notice, this list of conditions and the following disclaimer in the
40 *    documentation and/or other materials provided with the distribution.
41 * 3. All advertising materials mentioning features or use of this software
42 *    must display the following acknowledgement:
43 *      This product includes software developed by Christopher G. Demetriou
44 *	for the NetBSD Project.
45 * 4. The name of the author may not be used to endorse or promote products
46 *    derived from this software without specific prior written permission
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
49 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
50 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
51 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
52 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
53 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
54 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
55 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
56 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
57 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
58 */
59
60#include <sys/cdefs.h>
61__FBSDID("$FreeBSD: head/sys/dev/puc/puc.c 102734 2002-08-31 20:29:46Z phk $");
62
63/*
64 * PCI "universal" communication card device driver, glues com, lpt,
65 * and similar ports to PCI via bridge chip often much larger than
66 * the devices being glued.
67 *
68 * Author: Christopher G. Demetriou, May 14, 1998 (derived from NetBSD
69 * sys/dev/pci/pciide.c, revision 1.6).
70 *
71 * These devices could be (and some times are) described as
72 * communications/{serial,parallel}, etc. devices with known
73 * programming interfaces, but those programming interfaces (in
74 * particular the BAR assignments for devices, etc.) in fact are not
75 * particularly well defined.
76 *
77 * After I/we have seen more of these devices, it may be possible
78 * to generalize some of these bits.  In particular, devices which
79 * describe themselves as communications/serial/16[45]50, and
80 * communications/parallel/??? might be attached via direct
81 * 'com' and 'lpt' attachments to pci.
82 */
83
84#include <sys/param.h>
85#include <sys/systm.h>
86#include <sys/kernel.h>
87#include <sys/bus.h>
88#include <sys/conf.h>
89#include <sys/malloc.h>
90
91#include <machine/bus.h>
92#include <machine/resource.h>
93#include <sys/rman.h>
94
95#include <dev/pci/pcireg.h>
96#include <dev/pci/pcivar.h>
97
98#define PUC_ENTRAILS	1
99#include <dev/puc/pucvar.h>
100
101#include <opt_puc.h>
102
103
104struct puc_device {
105	struct resource_list resources;
106	u_int serialfreq;
107};
108
109static void puc_intr(void *arg);
110
111static int puc_find_free_unit(char *);
112#ifdef PUC_DEBUG
113static void puc_print_resource_list(struct resource_list *);
114#endif
115
116static int
117puc_port_bar_index(struct puc_softc *sc, int bar)
118{
119	int i;
120
121	for (i = 0; i < PUC_MAX_BAR; i += 1) {
122		if (!sc->sc_bar_mappings[i].used)
123			break;
124		if (sc->sc_bar_mappings[i].bar == bar)
125			return (i);
126	}
127	sc->sc_bar_mappings[i].bar = bar;
128	sc->sc_bar_mappings[i].used = 1;
129	return (i);
130}
131
132int
133puc_attach(device_t dev, const struct puc_device_description *desc)
134{
135	char *typestr;
136	int bidx, childunit, i, irq_setup, rid;
137	struct puc_softc *sc;
138	struct puc_device *pdev;
139	struct resource *res;
140	struct resource_list_entry *rle;
141
142	sc = (struct puc_softc *)device_get_softc(dev);
143	bzero(sc, sizeof(*sc));
144	sc->sc_desc = desc;
145	if (sc->sc_desc == NULL)
146		return (ENXIO);
147
148#ifdef PUC_DEBUG
149	bootverbose = 1;
150
151	printf("puc: name: %s\n", sc->sc_desc->name);
152#endif
153	rid = 0;
154	res = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
155	    RF_ACTIVE | RF_SHAREABLE);
156	if (!res)
157		return (ENXIO);
158
159	sc->irqres = res;
160	sc->irqrid = rid;
161#ifdef PUC_FASTINTR
162	irq_setup = BUS_SETUP_INTR(device_get_parent(dev), dev, res,
163	    INTR_TYPE_TTY | INTR_FAST, puc_intr, sc, &sc->intr_cookie);
164#else
165	irq_setup = ENXIO;
166#endif
167	if (irq_setup != 0)
168		irq_setup = BUS_SETUP_INTR(device_get_parent(dev), dev, res,
169		    INTR_TYPE_TTY, puc_intr, sc, &sc->intr_cookie);
170	if (irq_setup != 0)
171		return (ENXIO);
172
173	rid = 0;
174	for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) {
175		if (i > 0 && rid == sc->sc_desc->ports[i].bar)
176			sc->barmuxed = 1;
177		rid = sc->sc_desc->ports[i].bar;
178		bidx = puc_port_bar_index(sc, rid);
179
180		if (sc->sc_bar_mappings[bidx].res != NULL)
181			continue;
182		res = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
183		    0ul, ~0ul, 1, RF_ACTIVE);
184		if (res == NULL) {
185			printf("could not get resource\n");
186			continue;
187		}
188		sc->sc_bar_mappings[bidx].res = res;
189#ifdef PUC_DEBUG
190		printf("port rid %d bst %x, start %x, end %x\n", rid,
191		    (u_int)rman_get_bustag(res), (u_int)rman_get_start(res),
192		    (u_int)rman_get_end(res));
193#endif
194	}
195
196	if (desc->init != NULL) {
197		i = desc->init(sc);
198		if (i != 0)
199			return (i);
200	}
201
202	for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) {
203		rid = sc->sc_desc->ports[i].bar;
204		bidx = puc_port_bar_index(sc, rid);
205		if (sc->sc_bar_mappings[bidx].res == NULL)
206			continue;
207
208		switch (sc->sc_desc->ports[i].type) {
209		case PUC_PORT_TYPE_COM:
210			typestr = "sio";
211			break;
212		default:
213			continue;
214		}
215		pdev = malloc(sizeof(struct puc_device), M_DEVBUF,
216		    M_NOWAIT | M_ZERO);
217		if (!pdev)
218			continue;
219		resource_list_init(&pdev->resources);
220
221		/* First fake up an IRQ resource. */
222		resource_list_add(&pdev->resources, SYS_RES_IRQ, 0,
223		    rman_get_start(sc->irqres), rman_get_end(sc->irqres),
224		    rman_get_end(sc->irqres) - rman_get_start(sc->irqres) + 1);
225		rle = resource_list_find(&pdev->resources, SYS_RES_IRQ, 0);
226		rle->res = sc->irqres;
227
228		/* Now fake an IOPORT resource */
229		res = sc->sc_bar_mappings[bidx].res;
230		resource_list_add(&pdev->resources, SYS_RES_IOPORT, 0,
231		    rman_get_start(res) + sc->sc_desc->ports[i].offset,
232		    rman_get_start(res) + sc->sc_desc->ports[i].offset + 8 - 1,
233		    8);
234		rle = resource_list_find(&pdev->resources, SYS_RES_IOPORT, 0);
235
236		if (sc->barmuxed == 0) {
237			rle->res = sc->sc_bar_mappings[bidx].res;
238		} else {
239			rle->res = malloc(sizeof(struct resource), M_DEVBUF,
240			    M_WAITOK | M_ZERO);
241			if (rle->res == NULL) {
242				free(pdev, M_DEVBUF);
243				return (ENOMEM);
244			}
245
246			rle->res->r_start = rman_get_start(res) +
247			    sc->sc_desc->ports[i].offset;
248			rle->res->r_end = rle->res->r_start + 8 - 1;
249			rle->res->r_bustag = rman_get_bustag(res);
250			bus_space_subregion(rle->res->r_bustag,
251			    rman_get_bushandle(res),
252			    sc->sc_desc->ports[i].offset, 8,
253			    &rle->res->r_bushandle);
254		}
255
256		pdev->serialfreq = sc->sc_desc->ports[i].serialfreq;
257
258		childunit = puc_find_free_unit(typestr);
259		sc->sc_ports[i].dev = device_add_child(dev, typestr, childunit);
260		if (sc->sc_ports[i].dev == NULL) {
261			if (sc->barmuxed) {
262				bus_space_unmap(rman_get_bustag(rle->res),
263						rman_get_bushandle(rle->res),
264						8);
265				free(rle->res, M_DEVBUF);
266				free(pdev, M_DEVBUF);
267			}
268			continue;
269		}
270		device_set_ivars(sc->sc_ports[i].dev, pdev);
271		device_set_desc(sc->sc_ports[i].dev, sc->sc_desc->name);
272		if (!bootverbose)
273			device_quiet(sc->sc_ports[i].dev);
274#ifdef PUC_DEBUG
275		printf("puc: type %d, bar %x, offset %x\n",
276		    sc->sc_desc->ports[i].type,
277		    sc->sc_desc->ports[i].bar,
278		    sc->sc_desc->ports[i].offset);
279		puc_print_resource_list(&pdev->resources);
280#endif
281		if (device_probe_and_attach(sc->sc_ports[i].dev) != 0) {
282			if (sc->barmuxed) {
283				bus_space_unmap(rman_get_bustag(rle->res),
284						rman_get_bushandle(rle->res),
285						8);
286				free(rle->res, M_DEVBUF);
287				free(pdev, M_DEVBUF);
288			}
289		}
290	}
291
292#ifdef PUC_DEBUG
293	bootverbose = 0;
294#endif
295	return (0);
296}
297
298/*
299 * This is just an brute force interrupt handler. It just calls all the
300 * registered handlers sequencially.
301 *
302 * Later on we should maybe have a different handler for boards that can
303 * tell us which device generated the interrupt.
304 */
305static void
306puc_intr(void *arg)
307{
308	int i;
309	struct puc_softc *sc;
310
311printf("puc_intr\n");
312	sc = (struct puc_softc *)arg;
313	for (i = 0; i < PUC_MAX_PORTS; i++)
314		if (sc->sc_ports[i].ihand != NULL)
315			(sc->sc_ports[i].ihand)(sc->sc_ports[i].ihandarg);
316}
317
318const struct puc_device_description *
319puc_find_description(uint32_t vend, uint32_t prod, uint32_t svend,
320    uint32_t sprod)
321{
322	int i;
323
324#define checkreg(val, index) \
325    (((val) & puc_devices[i].rmask[(index)]) == puc_devices[i].rval[(index)])
326
327	for (i = 0; puc_devices[i].name != NULL; i++) {
328		if (checkreg(vend, PUC_REG_VEND) &&
329		    checkreg(prod, PUC_REG_PROD) &&
330		    checkreg(svend, PUC_REG_SVEND) &&
331		    checkreg(sprod, PUC_REG_SPROD))
332			return (&puc_devices[i]);
333	}
334
335#undef checkreg
336
337	return (NULL);
338}
339static int puc_find_free_unit(char *name)
340{
341	devclass_t dc;
342	int start;
343	int unit;
344
345	unit = 0;
346	start = 0;
347	while (resource_int_value(name, unit, "port", &start) == 0 &&
348	    start > 0)
349		unit++;
350	dc = devclass_find(name);
351	if (dc == NULL)
352		return (-1);
353	while (devclass_get_device(dc, unit))
354		unit++;
355#ifdef PUC_DEBUG
356	printf("puc: Using %s%d\n", name, unit);
357#endif
358	return (unit);
359}
360
361#ifdef PUC_DEBUG
362static void
363puc_print_resource_list(struct resource_list *rl)
364{
365#if 0
366	struct resource_list_entry *rle;
367
368	printf("print_resource_list: rl %p\n", rl);
369	SLIST_FOREACH(rle, rl, link)
370		printf("  type %x, rid %x start %x end %x count %x\n",
371		    rle->type, rle->rid, rle->start, rle->end, rle->count);
372	printf("print_resource_list: end.\n");
373#endif
374}
375#endif
376
377struct resource *
378puc_alloc_resource(device_t dev, device_t child, int type, int *rid,
379    u_long start, u_long end, u_long count, u_int flags)
380{
381	struct puc_device *pdev;
382	struct resource *retval;
383	struct resource_list *rl;
384	struct resource_list_entry *rle;
385
386	pdev = device_get_ivars(child);
387	rl = &pdev->resources;
388
389#ifdef PUC_DEBUG
390	printf("puc_alloc_resource: pdev %p, looking for t %x, r %x\n",
391	    pdev, type, *rid);
392	puc_print_resource_list(rl);
393#endif
394	retval = NULL;
395	rle = resource_list_find(rl, type, *rid);
396	if (rle) {
397		start = rle->start;
398		end = rle->end;
399		count = rle->count;
400#ifdef PUC_DEBUG
401		printf("found rle, %lx, %lx, %lx\n", start, end, count);
402#endif
403		retval = rle->res;
404	} else
405		printf("oops rle is gone\n");
406
407	return (retval);
408}
409
410int
411puc_release_resource(device_t dev, device_t child, int type, int rid,
412    struct resource *res)
413{
414	return (0);
415}
416
417int
418puc_get_resource(device_t dev, device_t child, int type, int rid,
419    u_long *startp, u_long *countp)
420{
421	struct puc_device *pdev;
422	struct resource_list *rl;
423	struct resource_list_entry *rle;
424
425	pdev = device_get_ivars(child);
426	rl = &pdev->resources;
427
428#ifdef PUC_DEBUG
429	printf("puc_get_resource: pdev %p, looking for t %x, r %x\n", pdev,
430	    type, rid);
431	puc_print_resource_list(rl);
432#endif
433	rle = resource_list_find(rl, type, rid);
434	if (rle) {
435#ifdef PUC_DEBUG
436		printf("found rle %p,", rle);
437#endif
438		if (startp != NULL)
439			*startp = rle->start;
440		if (countp != NULL)
441			*countp = rle->count;
442#ifdef PUC_DEBUG
443		printf(" %lx, %lx\n", rle->start, rle->count);
444#endif
445		return (0);
446	} else
447		printf("oops rle is gone\n");
448	return (ENXIO);
449}
450
451int
452puc_setup_intr(device_t dev, device_t child, struct resource *r, int flags,
453	       void (*ihand)(void *), void *arg, void **cookiep)
454{
455	int i;
456	struct puc_softc *sc;
457
458printf("puc_setup_intr()\n");
459	sc = (struct puc_softc *)device_get_softc(dev);
460	for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) {
461		if (sc->sc_ports[i].dev == child) {
462			if (sc->sc_ports[i].ihand != 0)
463				return (ENXIO);
464			sc->sc_ports[i].ihand = ihand;
465			sc->sc_ports[i].ihandarg = arg;
466			*cookiep = arg;
467			return (0);
468		}
469	}
470	return (ENXIO);
471}
472
473int
474puc_teardown_intr(device_t dev, device_t child, struct resource *r,
475		  void *cookie)
476{
477	int i;
478	struct puc_softc *sc;
479
480printf("puc_teardown_intr()\n");
481	sc = (struct puc_softc *)device_get_softc(dev);
482	for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) {
483		if (sc->sc_ports[i].dev == child) {
484			sc->sc_ports[i].ihand = NULL;
485			sc->sc_ports[i].ihandarg = NULL;
486			return (0);
487		}
488	}
489	return (ENXIO);
490}
491
492int
493puc_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
494{
495	struct puc_device *pdev;
496
497	pdev = device_get_ivars(child);
498	if (pdev == NULL)
499		return (ENOENT);
500
501	switch(index) {
502	case PUC_IVAR_FREQ:
503		*result = pdev->serialfreq;
504		break;
505	default:
506		return (ENOENT);
507	}
508	return (0);
509}
510
511devclass_t puc_devclass;
512
513