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