puc.c revision 108470
11558Srgrimes/*	$NetBSD: puc.c,v 1.7 2000/07/29 17:43:38 jlam Exp $	*/
21558Srgrimes
31558Srgrimes/*-
41558Srgrimes * Copyright (c) 2002 JF Hay.  All rights reserved.
51558Srgrimes * Copyright (c) 2000 M. Warner Losh.  All rights reserved.
61558Srgrimes *
71558Srgrimes * Redistribution and use in source and binary forms, with or without
81558Srgrimes * modification, are permitted provided that the following conditions
91558Srgrimes * are met:
101558Srgrimes * 1. Redistributions of source code must retain the above copyright
111558Srgrimes *    notice unmodified, this list of conditions, and the following
121558Srgrimes *    disclaimer.
131558Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
141558Srgrimes *    notice, this list of conditions and the following disclaimer in the
151558Srgrimes *    documentation and/or other materials provided with the distribution.
161558Srgrimes *
171558Srgrimes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
181558Srgrimes * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
191558Srgrimes * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
201558Srgrimes * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
211558Srgrimes * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
221558Srgrimes * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
231558Srgrimes * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
241558Srgrimes * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
251558Srgrimes * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
261558Srgrimes * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
271558Srgrimes */
281558Srgrimes
291558Srgrimes/*
301558Srgrimes * Copyright (c) 1996, 1998, 1999
311558Srgrimes *	Christopher G. Demetriou.  All rights reserved.
321558Srgrimes *
331558Srgrimes * Redistribution and use in source and binary forms, with or without
3437428Scharnier * modification, are permitted provided that the following conditions
351558Srgrimes * are met:
361558Srgrimes * 1. Redistributions of source code must retain the above copyright
371558Srgrimes *    notice, this list of conditions and the following disclaimer.
381558Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
391558Srgrimes *    notice, this list of conditions and the following disclaimer in the
4037428Scharnier *    documentation and/or other materials provided with the distribution.
4123680Speter * 3. All advertising materials mentioning features or use of this software
4237428Scharnier *    must display the following acknowledgement:
4315770Swollman *      This product includes software developed by Christopher G. Demetriou
4450476Speter *	for the NetBSD Project.
451558Srgrimes * 4. The name of the author may not be used to endorse or promote products
461558Srgrimes *    derived from this software without specific prior written permission
471558Srgrimes *
481558Srgrimes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
49101829Smux * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
501558Srgrimes * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
511558Srgrimes * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
521558Srgrimes * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
531558Srgrimes * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
541558Srgrimes * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
5515770Swollman * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
5615770Swollman * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
571558Srgrimes * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
581558Srgrimes */
591558Srgrimes
6092882Simp#include <sys/cdefs.h>
6192882Simp__FBSDID("$FreeBSD: head/sys/dev/puc/puc.c 108470 2002-12-30 21:18:15Z schweikh $");
621558Srgrimes
631558Srgrimes/*
64152670Srodrigc * PCI "universal" communication card device driver, glues com, lpt,
651558Srgrimes * and similar ports to PCI via bridge chip often much larger than
66245005Skib * the devices being glued.
67245005Skib *
6826072Sdfr * Author: Christopher G. Demetriou, May 14, 1998 (derived from NetBSD
691558Srgrimes * sys/dev/pci/pciide.c, revision 1.6).
70245005Skib *
71247856Sjkim * These devices could be (and some times are) described as
72245005Skib * communications/{serial,parallel}, etc. devices with known
731558Srgrimes * programming interfaces, but those programming interfaces (in
74245005Skib * particular the BAR assignments for devices, etc.) in fact are not
75245005Skib * particularly well defined.
76245005Skib *
7724359Simp * After I/we have seen more of these devices, it may be possible
781558Srgrimes * to generalize some of these bits.  In particular, devices which
791558Srgrimes * describe themselves as communications/serial/16[45]50, and
80245005Skib * communications/parallel/??? might be attached via direct
81245005Skib * 'com' and 'lpt' attachments to pci.
82245005Skib */
83245005Skib
84245005Skib#include "opt_puc.h"
85245005Skib
86245005Skib#include <sys/param.h>
87245005Skib#include <sys/systm.h>
881558Srgrimes#include <sys/kernel.h>
891558Srgrimes#include <sys/bus.h>
901558Srgrimes#include <sys/conf.h>
911558Srgrimes#include <sys/malloc.h>
921558Srgrimes
931558Srgrimes#include <machine/bus.h>
941558Srgrimes#include <machine/resource.h>
951558Srgrimes#include <sys/rman.h>
961558Srgrimes
971558Srgrimes#include <dev/pci/pcireg.h>
981558Srgrimes#include <dev/pci/pcivar.h>
9952055Sphk
100230226Sjh#define PUC_ENTRAILS	1
101230226Sjh#include <dev/puc/pucvar.h>
102230226Sjh
103230226Sjhstruct puc_device {
1041558Srgrimes	struct resource_list resources;
10526072Sdfr	u_int serialfreq;
106267808Srodrigc};
1071558Srgrimes
1081558Srgrimesstatic void puc_intr(void *arg);
109245005Skib
110245005Skibstatic int puc_find_free_unit(char *);
111245005Skib#ifdef PUC_DEBUG
112245005Skibstatic void puc_print_resource_list(struct resource_list *);
113247856Sjkim#endif
114267808Srodrigc
115245005Skibdevclass_t puc_devclass;
116267808Srodrigc
117245005Skibstatic int
118245005Skibpuc_port_bar_index(struct puc_softc *sc, int bar)
1191558Srgrimes{
1201558Srgrimes	int i;
1211558Srgrimes
1221558Srgrimes	for (i = 0; i < PUC_MAX_BAR; i += 1) {
123201227Sed		if (!sc->sc_bar_mappings[i].used)
1241558Srgrimes			break;
1251558Srgrimes		if (sc->sc_bar_mappings[i].bar == bar)
1261558Srgrimes			return (i);
1271558Srgrimes	}
1281558Srgrimes	sc->sc_bar_mappings[i].bar = bar;
1291558Srgrimes	sc->sc_bar_mappings[i].used = 1;
1301558Srgrimes	return (i);
1311558Srgrimes}
1321558Srgrimes
1331558Srgrimesint
1341558Srgrimespuc_attach(device_t dev, const struct puc_device_description *desc)
1351558Srgrimes{
1361558Srgrimes	char *typestr;
13715770Swollman	int bidx, childunit, i, irq_setup, rid;
138201227Sed	struct puc_softc *sc;
1391558Srgrimes	struct puc_device *pdev;
1401558Srgrimes	struct resource *res;
141141611Sru	struct resource_list_entry *rle;
1421558Srgrimes
1431558Srgrimes	sc = (struct puc_softc *)device_get_softc(dev);
144	bzero(sc, sizeof(*sc));
145	sc->sc_desc = desc;
146	if (sc->sc_desc == NULL)
147		return (ENXIO);
148
149#ifdef PUC_DEBUG
150	bootverbose = 1;
151
152	printf("puc: name: %s\n", sc->sc_desc->name);
153#endif
154	rid = 0;
155	res = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
156	    RF_ACTIVE | RF_SHAREABLE);
157	if (!res)
158		return (ENXIO);
159
160	sc->irqres = res;
161	sc->irqrid = rid;
162#ifdef PUC_FASTINTR
163	irq_setup = BUS_SETUP_INTR(device_get_parent(dev), dev, res,
164	    INTR_TYPE_TTY | INTR_FAST, puc_intr, sc, &sc->intr_cookie);
165	if (irq_setup == 0)
166		sc->fastintr = INTR_FAST;
167	else
168		irq_setup = BUS_SETUP_INTR(device_get_parent(dev), dev, res,
169		    INTR_TYPE_TTY, puc_intr, sc, &sc->intr_cookie);
170#else
171	irq_setup = BUS_SETUP_INTR(device_get_parent(dev), dev, res,
172	    INTR_TYPE_TTY, puc_intr, sc, &sc->intr_cookie);
173#endif
174	if (irq_setup != 0)
175		return (ENXIO);
176
177	rid = 0;
178	for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) {
179		if (i > 0 && rid == sc->sc_desc->ports[i].bar)
180			sc->barmuxed = 1;
181		rid = sc->sc_desc->ports[i].bar;
182		bidx = puc_port_bar_index(sc, rid);
183
184		if (sc->sc_bar_mappings[bidx].res != NULL)
185			continue;
186		res = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
187		    0ul, ~0ul, 1, RF_ACTIVE);
188		if (res == NULL) {
189			printf("could not get resource\n");
190			continue;
191		}
192		sc->sc_bar_mappings[bidx].res = res;
193#ifdef PUC_DEBUG
194		printf("port rid %d bst %x, start %x, end %x\n", rid,
195		    (u_int)rman_get_bustag(res), (u_int)rman_get_start(res),
196		    (u_int)rman_get_end(res));
197#endif
198	}
199
200	if (desc->init != NULL) {
201		i = desc->init(sc);
202		if (i != 0)
203			return (i);
204	}
205
206	for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) {
207		rid = sc->sc_desc->ports[i].bar;
208		bidx = puc_port_bar_index(sc, rid);
209		if (sc->sc_bar_mappings[bidx].res == NULL)
210			continue;
211
212		switch (sc->sc_desc->ports[i].type) {
213		case PUC_PORT_TYPE_COM:
214			typestr = "sio";
215			break;
216		default:
217			continue;
218		}
219		pdev = malloc(sizeof(struct puc_device), M_DEVBUF,
220		    M_NOWAIT | M_ZERO);
221		if (!pdev)
222			continue;
223		resource_list_init(&pdev->resources);
224
225		/* First fake up an IRQ resource. */
226		resource_list_add(&pdev->resources, SYS_RES_IRQ, 0,
227		    rman_get_start(sc->irqres), rman_get_end(sc->irqres),
228		    rman_get_end(sc->irqres) - rman_get_start(sc->irqres) + 1);
229		rle = resource_list_find(&pdev->resources, SYS_RES_IRQ, 0);
230		rle->res = sc->irqres;
231
232		/* Now fake an IOPORT resource */
233		res = sc->sc_bar_mappings[bidx].res;
234		resource_list_add(&pdev->resources, SYS_RES_IOPORT, 0,
235		    rman_get_start(res) + sc->sc_desc->ports[i].offset,
236		    rman_get_start(res) + sc->sc_desc->ports[i].offset + 8 - 1,
237		    8);
238		rle = resource_list_find(&pdev->resources, SYS_RES_IOPORT, 0);
239
240		if (sc->barmuxed == 0) {
241			rle->res = sc->sc_bar_mappings[bidx].res;
242		} else {
243			rle->res = malloc(sizeof(struct resource), M_DEVBUF,
244			    M_WAITOK | M_ZERO);
245			if (rle->res == NULL) {
246				free(pdev, M_DEVBUF);
247				return (ENOMEM);
248			}
249
250			rle->res->r_start = rman_get_start(res) +
251			    sc->sc_desc->ports[i].offset;
252			rle->res->r_end = rle->res->r_start + 8 - 1;
253			rle->res->r_bustag = rman_get_bustag(res);
254			bus_space_subregion(rle->res->r_bustag,
255			    rman_get_bushandle(res),
256			    sc->sc_desc->ports[i].offset, 8,
257			    &rle->res->r_bushandle);
258		}
259
260		pdev->serialfreq = sc->sc_desc->ports[i].serialfreq;
261
262		childunit = puc_find_free_unit(typestr);
263		sc->sc_ports[i].dev = device_add_child(dev, typestr, childunit);
264		if (sc->sc_ports[i].dev == NULL) {
265			if (sc->barmuxed) {
266				bus_space_unmap(rman_get_bustag(rle->res),
267						rman_get_bushandle(rle->res),
268						8);
269				free(rle->res, M_DEVBUF);
270				free(pdev, M_DEVBUF);
271			}
272			continue;
273		}
274		device_set_ivars(sc->sc_ports[i].dev, pdev);
275		device_set_desc(sc->sc_ports[i].dev, sc->sc_desc->name);
276		if (!bootverbose)
277			device_quiet(sc->sc_ports[i].dev);
278#ifdef PUC_DEBUG
279		printf("puc: type %d, bar %x, offset %x\n",
280		    sc->sc_desc->ports[i].type,
281		    sc->sc_desc->ports[i].bar,
282		    sc->sc_desc->ports[i].offset);
283		puc_print_resource_list(&pdev->resources);
284#endif
285		device_set_flags(sc->sc_ports[i].dev,
286		    sc->sc_desc->ports[i].flags);
287		if (device_probe_and_attach(sc->sc_ports[i].dev) != 0) {
288			if (sc->barmuxed) {
289				bus_space_unmap(rman_get_bustag(rle->res),
290						rman_get_bushandle(rle->res),
291						8);
292				free(rle->res, M_DEVBUF);
293				free(pdev, M_DEVBUF);
294			}
295		}
296	}
297
298#ifdef PUC_DEBUG
299	bootverbose = 0;
300#endif
301	return (0);
302}
303
304/*
305 * This is just a brute force interrupt handler. It just calls all the
306 * registered handlers sequencially.
307 *
308 * Later on we should maybe have a different handler for boards that can
309 * tell us which device generated the interrupt.
310 */
311static void
312puc_intr(void *arg)
313{
314	int i;
315	struct puc_softc *sc;
316
317	sc = (struct puc_softc *)arg;
318	for (i = 0; i < PUC_MAX_PORTS; i++)
319		if (sc->sc_ports[i].ihand != NULL)
320			(sc->sc_ports[i].ihand)(sc->sc_ports[i].ihandarg);
321}
322
323const struct puc_device_description *
324puc_find_description(uint32_t vend, uint32_t prod, uint32_t svend,
325    uint32_t sprod)
326{
327	int i;
328
329#define checkreg(val, index) \
330    (((val) & puc_devices[i].rmask[(index)]) == puc_devices[i].rval[(index)])
331
332	for (i = 0; puc_devices[i].name != NULL; i++) {
333		if (checkreg(vend, PUC_REG_VEND) &&
334		    checkreg(prod, PUC_REG_PROD) &&
335		    checkreg(svend, PUC_REG_SVEND) &&
336		    checkreg(sprod, PUC_REG_SPROD))
337			return (&puc_devices[i]);
338	}
339
340#undef checkreg
341
342	return (NULL);
343}
344
345static int
346puc_find_free_unit(char *name)
347{
348	devclass_t dc;
349	int start;
350	int unit;
351
352	unit = 0;
353	start = 0;
354	while (resource_int_value(name, unit, "port", &start) == 0 &&
355	    start > 0)
356		unit++;
357	dc = devclass_find(name);
358	if (dc == NULL)
359		return (-1);
360	while (devclass_get_device(dc, unit))
361		unit++;
362#ifdef PUC_DEBUG
363	printf("puc: Using %s%d\n", name, unit);
364#endif
365	return (unit);
366}
367
368#ifdef PUC_DEBUG
369static void
370puc_print_resource_list(struct resource_list *rl)
371{
372#if 0
373	struct resource_list_entry *rle;
374
375	printf("print_resource_list: rl %p\n", rl);
376	SLIST_FOREACH(rle, rl, link)
377		printf("  type %x, rid %x start %x end %x count %x\n",
378		    rle->type, rle->rid, rle->start, rle->end, rle->count);
379	printf("print_resource_list: end.\n");
380#endif
381}
382#endif
383
384struct resource *
385puc_alloc_resource(device_t dev, device_t child, int type, int *rid,
386    u_long start, u_long end, u_long count, u_int flags)
387{
388	struct puc_device *pdev;
389	struct resource *retval;
390	struct resource_list *rl;
391	struct resource_list_entry *rle;
392
393	pdev = device_get_ivars(child);
394	rl = &pdev->resources;
395
396#ifdef PUC_DEBUG
397	printf("puc_alloc_resource: pdev %p, looking for t %x, r %x\n",
398	    pdev, type, *rid);
399	puc_print_resource_list(rl);
400#endif
401	retval = NULL;
402	rle = resource_list_find(rl, type, *rid);
403	if (rle) {
404		start = rle->start;
405		end = rle->end;
406		count = rle->count;
407#ifdef PUC_DEBUG
408		printf("found rle, %lx, %lx, %lx\n", start, end, count);
409#endif
410		retval = rle->res;
411	} else
412		printf("oops rle is gone\n");
413
414	return (retval);
415}
416
417int
418puc_release_resource(device_t dev, device_t child, int type, int rid,
419    struct resource *res)
420{
421	return (0);
422}
423
424int
425puc_get_resource(device_t dev, device_t child, int type, int rid,
426    u_long *startp, u_long *countp)
427{
428	struct puc_device *pdev;
429	struct resource_list *rl;
430	struct resource_list_entry *rle;
431
432	pdev = device_get_ivars(child);
433	rl = &pdev->resources;
434
435#ifdef PUC_DEBUG
436	printf("puc_get_resource: pdev %p, looking for t %x, r %x\n", pdev,
437	    type, rid);
438	puc_print_resource_list(rl);
439#endif
440	rle = resource_list_find(rl, type, rid);
441	if (rle) {
442#ifdef PUC_DEBUG
443		printf("found rle %p,", rle);
444#endif
445		if (startp != NULL)
446			*startp = rle->start;
447		if (countp != NULL)
448			*countp = rle->count;
449#ifdef PUC_DEBUG
450		printf(" %lx, %lx\n", rle->start, rle->count);
451#endif
452		return (0);
453	} else
454		printf("oops rle is gone\n");
455	return (ENXIO);
456}
457
458int
459puc_setup_intr(device_t dev, device_t child, struct resource *r, int flags,
460	       void (*ihand)(void *), void *arg, void **cookiep)
461{
462	int i;
463	struct puc_softc *sc;
464
465	sc = (struct puc_softc *)device_get_softc(dev);
466	if ((flags & INTR_FAST) != sc->fastintr)
467		return (ENXIO);
468	for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) {
469		if (sc->sc_ports[i].dev == child) {
470			if (sc->sc_ports[i].ihand != 0)
471				return (ENXIO);
472			sc->sc_ports[i].ihand = ihand;
473			sc->sc_ports[i].ihandarg = arg;
474			*cookiep = arg;
475			return (0);
476		}
477	}
478	return (ENXIO);
479}
480
481int
482puc_teardown_intr(device_t dev, device_t child, struct resource *r,
483		  void *cookie)
484{
485	int i;
486	struct puc_softc *sc;
487
488	sc = (struct puc_softc *)device_get_softc(dev);
489	for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) {
490		if (sc->sc_ports[i].dev == child) {
491			sc->sc_ports[i].ihand = NULL;
492			sc->sc_ports[i].ihandarg = NULL;
493			return (0);
494		}
495	}
496	return (ENXIO);
497}
498
499int
500puc_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
501{
502	struct puc_device *pdev;
503
504	pdev = device_get_ivars(child);
505	if (pdev == NULL)
506		return (ENOENT);
507
508	switch(index) {
509	case PUC_IVAR_FREQ:
510		*result = pdev->serialfreq;
511		break;
512	default:
513		return (ENOENT);
514	}
515	return (0);
516}
517