puc.c revision 95392
133965Sjdp/*	$NetBSD: puc.c,v 1.7 2000/07/29 17:43:38 jlam Exp $	*/
2218822Sdim
378828Sobrien/*-
433965Sjdp * Copyright (c) 2002 JF Hay.  All rights reserved.
533965Sjdp * Copyright (c) 2000 M. Warner Losh.  All rights reserved.
633965Sjdp *
733965Sjdp * Redistribution and use in source and binary forms, with or without
833965Sjdp * modification, are permitted provided that the following conditions
933965Sjdp * are met:
1033965Sjdp * 1. Redistributions of source code must retain the above copyright
1133965Sjdp *    notice unmodified, this list of conditions, and the following
1233965Sjdp *    disclaimer.
1333965Sjdp * 2. Redistributions in binary form must reproduce the above copyright
1433965Sjdp *    notice, this list of conditions and the following disclaimer in the
1533965Sjdp *    documentation and/or other materials provided with the distribution.
1633965Sjdp *
1733965Sjdp * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1833965Sjdp * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1933965Sjdp * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2033965Sjdp * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2133965Sjdp * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2233965Sjdp * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23218822Sdim * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2433965Sjdp * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2533965Sjdp * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2633965Sjdp * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2733965Sjdp */
2833965Sjdp
2978828Sobrien/*
3033965Sjdp * Copyright (c) 1996, 1998, 1999
3160484Sobrien *	Christopher G. Demetriou.  All rights reserved.
3233965Sjdp *
3333965Sjdp * Redistribution and use in source and binary forms, with or without
3433965Sjdp * modification, are permitted provided that the following conditions
3533965Sjdp * are met:
3633965Sjdp * 1. Redistributions of source code must retain the above copyright
3733965Sjdp *    notice, this list of conditions and the following disclaimer.
3833965Sjdp * 2. Redistributions in binary form must reproduce the above copyright
3933965Sjdp *    notice, this list of conditions and the following disclaimer in the
4033965Sjdp *    documentation and/or other materials provided with the distribution.
4133965Sjdp * 3. All advertising materials mentioning features or use of this software
4233965Sjdp *    must display the following acknowledgement:
4333965Sjdp *      This product includes software developed by Christopher G. Demetriou
4433965Sjdp *	for the NetBSD Project.
4533965Sjdp * 4. The name of the author may not be used to endorse or promote products
4633965Sjdp *    derived from this software without specific prior written permission
4733965Sjdp *
4833965Sjdp * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
4933965Sjdp * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
5033965Sjdp * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
5133965Sjdp * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
5233965Sjdp * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
5333965Sjdp * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
5433965Sjdp * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
5533965Sjdp * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
5633965Sjdp * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
5733965Sjdp * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
5833965Sjdp */
5933965Sjdp
6033965Sjdp#include <sys/cdefs.h>
6133965Sjdp__FBSDID("$FreeBSD: head/sys/dev/puc/puc.c 95392 2002-04-24 18:53:43Z jhay $");
6233965Sjdp
6333965Sjdp/*
6433965Sjdp * PCI "universal" communication card device driver, glues com, lpt,
6533965Sjdp * and similar ports to PCI via bridge chip often much larger than
6633965Sjdp * the devices being glued.
6733965Sjdp *
6833965Sjdp * Author: Christopher G. Demetriou, May 14, 1998 (derived from NetBSD
6933965Sjdp * sys/dev/pci/pciide.c, revision 1.6).
7033965Sjdp *
7133965Sjdp * These devices could be (and some times are) described as
7233965Sjdp * communications/{serial,parallel}, etc. devices with known
7333965Sjdp * programming interfaces, but those programming interfaces (in
7433965Sjdp * particular the BAR assignments for devices, etc.) in fact are not
7533965Sjdp * particularly well defined.
7633965Sjdp *
7733965Sjdp * After I/we have seen more of these devices, it may be possible
7833965Sjdp * to generalize some of these bits.  In particular, devices which
7933965Sjdp * describe themselves as communications/serial/16[45]50, and
8033965Sjdp * communications/parallel/??? might be attached via direct
8133965Sjdp * 'com' and 'lpt' attachments to pci.
8233965Sjdp */
8333965Sjdp
8433965Sjdp#include <sys/param.h>
8533965Sjdp#include <sys/systm.h>
8633965Sjdp#include <sys/kernel.h>
8733965Sjdp#include <sys/bus.h>
8833965Sjdp#include <sys/conf.h>
8933965Sjdp#include <sys/malloc.h>
9033965Sjdp
9133965Sjdp#include <machine/bus.h>
9233965Sjdp#include <machine/resource.h>
9333965Sjdp#include <sys/rman.h>
9433965Sjdp
9533965Sjdp#include <dev/pci/pcireg.h>
9633965Sjdp#include <dev/pci/pcivar.h>
9733965Sjdp#include <dev/puc/pucvar.h>
9833965Sjdp
9933965Sjdp#include <opt_puc.h>
10033965Sjdp
10133965Sjdpstruct puc_softc {
10233965Sjdp	const struct puc_device_description *sc_desc;
10333965Sjdp
10433965Sjdp	/* card-global dynamic data */
10533965Sjdp	int			barmuxed;
10633965Sjdp	int			irqrid;
10733965Sjdp	struct resource		*irqres;
10833965Sjdp	void			*intr_cookie;
10933965Sjdp
11033965Sjdp	struct {
11133965Sjdp		struct resource	*res;
11233965Sjdp	} sc_bar_mappings[PUC_MAX_BAR];
11333965Sjdp
11433965Sjdp	/* per-port dynamic data */
11533965Sjdp        struct {
11633965Sjdp		struct device	*dev;
11733965Sjdp		/* filled in by bus_setup_intr() */
11833965Sjdp		void		(*ihand)(void *);
11933965Sjdp		void		*ihandarg;
12033965Sjdp        } sc_ports[PUC_MAX_PORTS];
12133965Sjdp};
12233965Sjdp
12333965Sjdpstruct puc_device {
12433965Sjdp	struct resource_list resources;
12533965Sjdp	u_int serialfreq;
12633965Sjdp};
12733965Sjdp
12833965Sjdpstatic int puc_pci_probe(device_t dev);
12933965Sjdpstatic int puc_pci_attach(device_t dev);
13033965Sjdpstatic void puc_intr(void *arg);
13133965Sjdp
13233965Sjdpstatic struct resource *puc_alloc_resource(device_t, device_t, int, int *,
13333965Sjdp    u_long, u_long, u_long, u_int);
13433965Sjdpstatic int puc_release_resource(device_t, device_t, int, int,
13533965Sjdp    struct resource *);
13633965Sjdpstatic int puc_get_resource(device_t, device_t, int, int, u_long *, u_long *);
13733965Sjdpstatic int puc_setup_intr(device_t, device_t, struct resource *, int,
13833965Sjdp    void (*)(void *), void *, void **);
13933965Sjdpstatic int puc_teardown_intr(device_t, device_t, struct resource *,
14033965Sjdp    void *);
14133965Sjdpstatic int puc_read_ivar(device_t, device_t, int, uintptr_t *);
14233965Sjdp
14333965Sjdpstatic const struct puc_device_description *puc_find_description(uint32_t,
14433965Sjdp    uint32_t, uint32_t, uint32_t);
14533965Sjdpstatic void puc_config_superio(device_t);
14689857Sobrienstatic void puc_config_win877(struct resource *);
14789857Sobrienstatic int puc_find_free_unit(char *);
14889857Sobrien#ifdef PUC_DEBUG
14989857Sobrienstatic void puc_print_win877(bus_space_tag_t, bus_space_handle_t, u_int,
15033965Sjdp    u_int);
15133965Sjdpstatic void puc_print_resource_list(struct resource_list *);
15233965Sjdp#endif
15333965Sjdp
15433965Sjdpstatic int
15533965Sjdppuc_pci_probe(device_t dev)
15633965Sjdp{
15733965Sjdp	uint32_t v1, v2, d1, d2;
15833965Sjdp	const struct puc_device_description *desc;
15933965Sjdp
16033965Sjdp	if ((pci_read_config(dev, PCIR_HEADERTYPE, 1) & 0x7f) != 0)
16133965Sjdp		return (ENXIO);
16233965Sjdp
16333965Sjdp	v1 = pci_read_config(dev, PCIR_VENDOR, 2);
16433965Sjdp	d1 = pci_read_config(dev, PCIR_DEVICE, 2);
16533965Sjdp	v2 = pci_read_config(dev, PCIR_SUBVEND_0, 2);
16633965Sjdp	d2 = pci_read_config(dev, PCIR_SUBDEV_0, 2);
16733965Sjdp
16833965Sjdp	desc = puc_find_description(v1, d1, v2, d2);
16933965Sjdp	if (desc == NULL)
17033965Sjdp		return (ENXIO);
17133965Sjdp	device_set_desc(dev, desc->name);
17233965Sjdp	return (0);
17333965Sjdp}
17433965Sjdp
17533965Sjdpstatic int
17633965Sjdppuc_pci_attach(device_t dev)
17733965Sjdp{
17833965Sjdp	char *typestr;
17933965Sjdp	int bidx, childunit, i, irq_setup, rid;
18033965Sjdp	uint32_t v1, v2, d1, d2;
18133965Sjdp	struct puc_softc *sc;
18233965Sjdp	struct puc_device *pdev;
18333965Sjdp	struct resource *res;
18433965Sjdp	struct resource_list_entry *rle;
18533965Sjdp
18633965Sjdp	sc = (struct puc_softc *)device_get_softc(dev);
18733965Sjdp	bzero(sc, sizeof(*sc));
18833965Sjdp	v1 = pci_read_config(dev, PCIR_VENDOR, 2);
18933965Sjdp	d1 = pci_read_config(dev, PCIR_DEVICE, 2);
19033965Sjdp	v2 = pci_read_config(dev, PCIR_SUBVEND_0, 2);
19133965Sjdp	d2 = pci_read_config(dev, PCIR_SUBDEV_0, 2);
19233965Sjdp	sc->sc_desc = puc_find_description(v1, d1, v2, d2);
19333965Sjdp	if (sc->sc_desc == NULL)
19433965Sjdp		return (ENXIO);
19533965Sjdp
19633965Sjdp#ifdef PUC_DEBUG
19733965Sjdp	bootverbose = 1;
19833965Sjdp
19933965Sjdp	printf("puc: name: %s\n", sc->sc_desc->name);
20033965Sjdp#endif
20133965Sjdp	rid = 0;
20233965Sjdp	res = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
20333965Sjdp	    RF_ACTIVE | RF_SHAREABLE);
20433965Sjdp	if (!res)
20533965Sjdp		return (ENXIO);
20633965Sjdp
20733965Sjdp	sc->irqres = res;
20833965Sjdp	sc->irqrid = rid;
20933965Sjdp#ifdef PUC_FASTINTR
21033965Sjdp	irq_setup = BUS_SETUP_INTR(device_get_parent(dev), dev, res,
21133965Sjdp	    INTR_TYPE_TTY | INTR_FAST, puc_intr, sc, &sc->intr_cookie);
21233965Sjdp#else
21333965Sjdp	irq_setup = ENXIO;
21433965Sjdp#endif
21533965Sjdp	if (irq_setup != 0)
21633965Sjdp		irq_setup = BUS_SETUP_INTR(device_get_parent(dev), dev, res,
21733965Sjdp		    INTR_TYPE_TTY, puc_intr, sc, &sc->intr_cookie);
21833965Sjdp	if (irq_setup != 0)
21933965Sjdp		return (ENXIO);
22033965Sjdp
22133965Sjdp	rid = 0;
22233965Sjdp	for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) {
22333965Sjdp		if (rid == sc->sc_desc->ports[i].bar)
22433965Sjdp			sc->barmuxed = 1;
22533965Sjdp		rid = sc->sc_desc->ports[i].bar;
22633965Sjdp		bidx = PUC_PORT_BAR_INDEX(rid);
22733965Sjdp
22833965Sjdp		if (sc->sc_bar_mappings[bidx].res != NULL)
22933965Sjdp			continue;
23033965Sjdp		res = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
23133965Sjdp		    0ul, ~0ul, 1, RF_ACTIVE);
23233965Sjdp		if (res == NULL) {
23333965Sjdp			printf("could not get resource\n");
23433965Sjdp			continue;
23533965Sjdp		}
23633965Sjdp		sc->sc_bar_mappings[bidx].res = res;
23733965Sjdp#ifdef PUC_DEBUG
23833965Sjdp		printf("port bst %x, start %x, end %x\n",
23933965Sjdp		    (u_int)rman_get_bustag(res), (u_int)rman_get_start(res),
24033965Sjdp		    (u_int)rman_get_end(res));
24133965Sjdp#endif
24233965Sjdp	}
24333965Sjdp
24433965Sjdp	puc_config_superio(dev);
24533965Sjdp
24633965Sjdp	for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) {
24733965Sjdp		rid = sc->sc_desc->ports[i].bar;
248218822Sdim		bidx = PUC_PORT_BAR_INDEX(rid);
24933965Sjdp		if (sc->sc_bar_mappings[bidx].res == NULL)
25060484Sobrien			continue;
25160484Sobrien
25260484Sobrien		switch (sc->sc_desc->ports[i].type) {
25360484Sobrien		case PUC_PORT_TYPE_COM:
25460484Sobrien			typestr = "sio";
25560484Sobrien			break;
25660484Sobrien		default:
257130561Sobrien			continue;
258130561Sobrien		}
259130561Sobrien		pdev = malloc(sizeof(struct puc_device), M_DEVBUF,
260130561Sobrien		    M_NOWAIT | M_ZERO);
261130561Sobrien		if (!pdev)
262130561Sobrien			continue;
263130561Sobrien		resource_list_init(&pdev->resources);
264130561Sobrien
265130561Sobrien		/* First fake up an IRQ resource. */
266130561Sobrien		resource_list_add(&pdev->resources, SYS_RES_IRQ, 0,
267130561Sobrien		    rman_get_start(sc->irqres), rman_get_end(sc->irqres),
268130561Sobrien		    rman_get_end(sc->irqres) - rman_get_start(sc->irqres) + 1);
269130561Sobrien		rle = resource_list_find(&pdev->resources, SYS_RES_IRQ, 0);
270130561Sobrien		rle->res = sc->irqres;
271218822Sdim
272130561Sobrien		/* Now fake an IOPORT resource */
273218822Sdim		res = sc->sc_bar_mappings[bidx].res;
274218822Sdim		resource_list_add(&pdev->resources, SYS_RES_IOPORT, 0,
27533965Sjdp		    rman_get_start(res) + sc->sc_desc->ports[i].offset,
276		    rman_get_end(res) + sc->sc_desc->ports[i].offset + 8 - 1,
277		    8);
278		rle = resource_list_find(&pdev->resources, SYS_RES_IOPORT, 0);
279
280		if (sc->barmuxed == 0) {
281			rle->res = sc->sc_bar_mappings[bidx].res;
282		} else {
283			rle->res = malloc(sizeof(struct resource), M_DEVBUF,
284			    M_WAITOK | M_ZERO);
285			if (rle->res == NULL) {
286				free(pdev, M_DEVBUF);
287				return (ENOMEM);
288			}
289
290			rle->res->r_start = rman_get_start(res) +
291			    sc->sc_desc->ports[i].offset;
292			rle->res->r_end = rle->res->r_start + 8 - 1;
293			rle->res->r_bustag = rman_get_bustag(res);
294			bus_space_subregion(rle->res->r_bustag,
295			    rman_get_bushandle(res),
296			    sc->sc_desc->ports[i].offset, 8,
297			    &rle->res->r_bushandle);
298		}
299
300		pdev->serialfreq = sc->sc_desc->ports[i].serialfreq;
301
302		childunit = puc_find_free_unit(typestr);
303		sc->sc_ports[i].dev = device_add_child(dev, typestr, childunit);
304		if (sc->sc_ports[i].dev == NULL) {
305			if (sc->barmuxed) {
306				bus_space_unmap(rman_get_bustag(rle->res),
307						rman_get_bushandle(rle->res),
308						8);
309				free(rle->res, M_DEVBUF);
310				free(pdev, M_DEVBUF);
311			}
312			continue;
313		}
314		device_set_ivars(sc->sc_ports[i].dev, pdev);
315		device_set_desc(sc->sc_ports[i].dev, sc->sc_desc->name);
316		if (!bootverbose)
317			device_quiet(sc->sc_ports[i].dev);
318#ifdef PUC_DEBUG
319		printf("puc: type %d, bar %x, offset %x\n",
320		    sc->sc_desc->ports[i].type,
321		    sc->sc_desc->ports[i].bar,
322		    sc->sc_desc->ports[i].offset);
323		print_resource_list(&pdev->resources);
324#endif
325		if (device_probe_and_attach(sc->sc_ports[i].dev) != 0) {
326			if (sc->barmuxed) {
327				bus_space_unmap(rman_get_bustag(rle->res),
328						rman_get_bushandle(rle->res),
329						8);
330				free(rle->res, M_DEVBUF);
331				free(pdev, M_DEVBUF);
332			}
333		}
334	}
335
336#ifdef PUC_DEBUG
337	bootverbose = 0;
338#endif
339	return (0);
340}
341
342/*
343 * This is just an brute force interrupt handler. It just calls all the
344 * registered handlers sequencially.
345 *
346 * Later on we should maybe have a different handler for boards that can
347 * tell us which device generated the interrupt.
348 */
349static void
350puc_intr(void *arg)
351{
352	int i;
353	struct puc_softc *sc;
354
355	sc = (struct puc_softc *)arg;
356	for (i = 0; i < PUC_MAX_PORTS; i++)
357		if (sc->sc_ports[i].ihand != NULL)
358			(sc->sc_ports[i].ihand)(sc->sc_ports[i].ihandarg);
359}
360
361static const struct puc_device_description *
362puc_find_description(uint32_t vend, uint32_t prod, uint32_t svend,
363    uint32_t sprod)
364{
365	int i;
366
367#define checkreg(val, index) \
368    (((val) & puc_devices[i].rmask[(index)]) == puc_devices[i].rval[(index)])
369
370	for (i = 0; puc_devices[i].name != NULL; i++) {
371		if (checkreg(vend, PUC_REG_VEND) &&
372		    checkreg(prod, PUC_REG_PROD) &&
373		    checkreg(svend, PUC_REG_SVEND) &&
374		    checkreg(sprod, PUC_REG_SPROD))
375			return (&puc_devices[i]);
376	}
377
378#undef checkreg
379
380	return (NULL);
381}
382
383/*
384 * It might be possible to make these more generic if we can detect patterns.
385 * For instance maybe if the size of a bar is 0x400 (the old isa space) it
386 * might contain one or more superio chips.
387 */
388static void
389puc_config_superio(device_t dev)
390{
391	struct puc_softc *sc = (struct puc_softc *)device_get_softc(dev);
392
393	if (sc->sc_desc->rval[PUC_REG_VEND] == 0x1592 &&
394	    sc->sc_desc->rval[PUC_REG_PROD] == 0x0781)
395		puc_config_win877(sc->sc_bar_mappings[0].res);
396}
397
398#define rdspio(indx)		(bus_space_write_1(bst, bsh, efir, indx), \
399				bus_space_read_1(bst, bsh, efdr))
400#define wrspio(indx,data)	(bus_space_write_1(bst, bsh, efir, indx), \
401				bus_space_write_1(bst, bsh, efdr, data))
402
403#ifdef PUC_DEBUG
404static void
405puc_print_win877(bus_space_tag_t bst, bus_space_handle_t bsh, u_int efir,
406	u_int efdr)
407{
408	u_char cr00, cr01, cr04, cr09, cr0d, cr14, cr15, cr16, cr17;
409	u_char cr18, cr19, cr24, cr25, cr28, cr2c, cr31, cr32;
410
411	cr00 = rdspio(0x00);
412	cr01 = rdspio(0x01);
413	cr04 = rdspio(0x04);
414	cr09 = rdspio(0x09);
415	cr0d = rdspio(0x0d);
416	cr14 = rdspio(0x14);
417	cr15 = rdspio(0x15);
418	cr16 = rdspio(0x16);
419	cr17 = rdspio(0x17);
420	cr18 = rdspio(0x18);
421	cr19 = rdspio(0x19);
422	cr24 = rdspio(0x24);
423	cr25 = rdspio(0x25);
424	cr28 = rdspio(0x28);
425	cr2c = rdspio(0x2c);
426	cr31 = rdspio(0x31);
427	cr32 = rdspio(0x32);
428	printf("877T: cr00 %x, cr01 %x, cr04 %x, cr09 %x, cr0d %x, cr14 %x, "
429	    "cr15 %x, cr16 %x, cr17 %x, cr18 %x, cr19 %x, cr24 %x, cr25 %x, "
430	    "cr28 %x, cr2c %x, cr31 %x, cr32 %x\n", cr00, cr01, cr04, cr09,
431	    cr0d, cr14, cr15, cr16, cr17,
432	    cr18, cr19, cr24, cr25, cr28, cr2c, cr31, cr32);
433}
434#endif
435
436static void
437puc_config_win877(struct resource *res)
438{
439	u_char val;
440	u_int efir, efdr;
441	bus_space_tag_t bst;
442	bus_space_handle_t bsh;
443
444	bst = rman_get_bustag(res);
445	bsh = rman_get_bushandle(res);
446
447	/* configure the first W83877TF */
448	bus_space_write_1(bst, bsh, 0x250, 0x89);
449	efir = 0x251;
450	efdr = 0x252;
451	val = rdspio(0x09) & 0x0f;
452	if (val != 0x0c) {
453		printf("conf_win877: Oops not a W83877TF\n");
454		return;
455	}
456
457#ifdef PUC_DEBUG
458	printf("before: ");
459	puc_print_win877(bst, bsh, efir, efdr);
460#endif
461
462	val = rdspio(0x16);
463	val |= 0x04;
464	wrspio(0x16, val);
465	val &= ~0x04;
466	wrspio(0x16, val);
467
468	wrspio(0x24, 0x2e8 >> 2);
469	wrspio(0x25, 0x2f8 >> 2);
470	wrspio(0x17, 0x03);
471	wrspio(0x28, 0x43);
472
473#ifdef PUC_DEBUG
474	printf("after: ");
475	puc_print_win877(bst, bsh, efir, efdr);
476#endif
477
478	bus_space_write_1(bst, bsh, 0x250, 0xaa);
479
480	/* configure the second W83877TF */
481	bus_space_write_1(bst, bsh, 0x3f0, 0x87);
482	bus_space_write_1(bst, bsh, 0x3f0, 0x87);
483	efir = 0x3f0;
484	efdr = 0x3f1;
485	val = rdspio(0x09) & 0x0f;
486	if (val != 0x0c) {
487		printf("conf_win877: Oops not a W83877TF\n");
488		return;
489	}
490
491#ifdef PUC_DEBUG
492	printf("before: ");
493	puc_print_win877(bst, bsh, efir, efdr);
494#endif
495
496	val = rdspio(0x16);
497	val |= 0x04;
498	wrspio(0x16, val);
499	val &= ~0x04;
500	wrspio(0x16, val);
501
502	wrspio(0x24, 0x3e8 >> 2);
503	wrspio(0x25, 0x3f8 >> 2);
504	wrspio(0x17, 0x03);
505	wrspio(0x28, 0x43);
506
507#ifdef PUC_DEBUG
508	printf("after: ");
509	puc_print_win877(bst, bsh, efir, efdr);
510#endif
511
512	bus_space_write_1(bst, bsh, 0x3f0, 0xaa);
513}
514
515#undef rdspio
516#undef wrspio
517
518static int puc_find_free_unit(char *name)
519{
520	devclass_t dc;
521	int start;
522	int unit;
523
524	unit = 0;
525	start = 0;
526	while (resource_int_value(name, unit, "port", &start) == 0 &&
527	    start > 0)
528		unit++;
529	dc = devclass_find(name);
530	if (dc == NULL)
531		return (-1);
532	while (devclass_get_device(dc, unit))
533		unit++;
534#ifdef PUC_DEBUG
535	printf("puc: Using %s%d\n", name, unit);
536#endif
537	return (unit);
538}
539
540#ifdef PUC_DEBUG
541static void
542puc_print_resource_list(struct resource_list *rl)
543{
544	struct resource_list_entry *rle;
545
546	printf("print_resource_list: rl %p\n", rl);
547	SLIST_FOREACH(rle, rl, link)
548		printf("type %x, rid %x\n", rle->type, rle->rid);
549	printf("print_resource_list: end.\n");
550}
551#endif
552
553static struct resource *
554puc_alloc_resource(device_t dev, device_t child, int type, int *rid,
555    u_long start, u_long end, u_long count, u_int flags)
556{
557	struct puc_device *pdev;
558	struct resource *retval;
559	struct resource_list *rl;
560	struct resource_list_entry *rle;
561
562	pdev = device_get_ivars(child);
563	rl = &pdev->resources;
564
565#ifdef PUC_DEBUG
566	printf("puc_alloc_resource: pdev %p, looking for t %x, r %x\n",
567	    pdev, type, *rid);
568	puc_print_resource_list(rl);
569#endif
570	retval = NULL;
571	rle = resource_list_find(rl, type, *rid);
572	if (rle) {
573		start = rle->start;
574		end = rle->end;
575		count = rle->count;
576#ifdef PUC_DEBUG
577		printf("found rle, %lx, %lx, %lx\n", start, end, count);
578#endif
579		retval = rle->res;
580	} else
581		printf("oops rle is gone\n");
582
583	return (retval);
584}
585
586static int
587puc_release_resource(device_t dev, device_t child, int type, int rid,
588    struct resource *res)
589{
590	return (0);
591}
592
593static int
594puc_get_resource(device_t dev, device_t child, int type, int rid,
595    u_long *startp, u_long *countp)
596{
597	struct puc_device *pdev;
598	struct resource_list *rl;
599	struct resource_list_entry *rle;
600
601	pdev = device_get_ivars(child);
602	rl = &pdev->resources;
603
604#ifdef PUC_DEBUG
605	printf("puc_get_resource: pdev %p, looking for t %x, r %x\n", pdev,
606	    type, rid);
607	puc_print_resource_list(rl);
608#endif
609	rle = resource_list_find(rl, type, rid);
610	if (rle) {
611#ifdef PUC_DEBUG
612		printf("found rle %p,", rle);
613#endif
614		if (startp != NULL)
615			*startp = rle->start;
616		if (countp != NULL)
617			*countp = rle->count;
618#ifdef PUC_DEBUG
619		printf(" %lx, %lx\n", rle->start, rle->count);
620#endif
621		return (0);
622	} else
623		printf("oops rle is gone\n");
624	return (ENXIO);
625}
626
627static int
628puc_setup_intr(device_t dev, device_t child, struct resource *r, int flags,
629	       void (*ihand)(void *), void *arg, void **cookiep)
630{
631	int i;
632	struct puc_softc *sc;
633
634	sc = (struct puc_softc *)device_get_softc(dev);
635	for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) {
636		if (sc->sc_ports[i].dev == child) {
637			if (sc->sc_ports[i].ihand != 0)
638				return (ENXIO);
639			sc->sc_ports[i].ihand = ihand;
640			sc->sc_ports[i].ihandarg = arg;
641			*cookiep = arg;
642			return (0);
643		}
644	}
645	return (ENXIO);
646}
647
648static int
649puc_teardown_intr(device_t dev, device_t child, struct resource *r,
650		  void *cookie)
651{
652	int i;
653	struct puc_softc *sc;
654
655	sc = (struct puc_softc *)device_get_softc(dev);
656	for (i = 0; PUC_PORT_VALID(sc->sc_desc, i); i++) {
657		if (sc->sc_ports[i].dev == child) {
658			sc->sc_ports[i].ihand = NULL;
659			sc->sc_ports[i].ihandarg = NULL;
660			return (0);
661		}
662	}
663	return (ENXIO);
664}
665
666static int
667puc_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
668{
669	struct puc_device *pdev;
670
671	pdev = device_get_ivars(child);
672	if (pdev == NULL)
673		return (ENOENT);
674
675	switch(index) {
676	case PUC_IVAR_FREQ:
677		*result = pdev->serialfreq;
678		break;
679	default:
680		return (ENOENT);
681	}
682	return (0);
683}
684
685static device_method_t puc_pci_methods[] = {
686    /* Device interface */
687    DEVMETHOD(device_probe,		puc_pci_probe),
688    DEVMETHOD(device_attach,		puc_pci_attach),
689
690    DEVMETHOD(bus_alloc_resource,	puc_alloc_resource),
691    DEVMETHOD(bus_release_resource,	puc_release_resource),
692    DEVMETHOD(bus_get_resource,		puc_get_resource),
693    DEVMETHOD(bus_read_ivar,		puc_read_ivar),
694    DEVMETHOD(bus_setup_intr,		puc_setup_intr),
695    DEVMETHOD(bus_teardown_intr,	puc_teardown_intr),
696    DEVMETHOD(bus_print_child,		bus_generic_print_child),
697    DEVMETHOD(bus_driver_added,		bus_generic_driver_added),
698    { 0, 0 }
699};
700
701static driver_t puc_pci_driver = {
702	"puc",
703	puc_pci_methods,
704	sizeof(struct puc_softc),
705};
706
707static devclass_t puc_devclass;
708
709DRIVER_MODULE(puc, pci, puc_pci_driver, puc_devclass, 0, 0);
710