pcii.c revision 141747
1/*-
2 * Copyright (c) 2005 Poul-Henning Kamp <phk@FreeBSD.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * Driver for GPIB cards based on NEC �PD7210 and compatibles.
27 *
28 * This driver just hooks up to the hardware and leaves all the interesting
29 * stuff to upd7210.c.
30 *
31 * Supported hardware:
32 *    PCIIA compatible cards.
33 *
34 *    Tested and known working:
35 *	"B&C Microsystems PC488A-0"
36 *
37 */
38
39#include <sys/cdefs.h>
40__FBSDID("$FreeBSD: head/sys/dev/ieee488/pcii.c 141747 2005-02-12 17:39:50Z phk $");
41
42#include <sys/param.h>
43#include <sys/systm.h>
44#include <sys/lock.h>
45#include <sys/mutex.h>
46#include <sys/kernel.h>
47#include <sys/module.h>
48#include <sys/bus.h>
49#include <machine/bus.h>
50#include <machine/resource.h>
51#include <sys/rman.h>
52#include <isa/isavar.h>
53
54#define UPD7210_HW_DRIVER
55#include <dev/ieee488/upd7210.h>
56
57struct pcii_softc {
58	int foo;
59	struct resource	*port[8];
60	struct resource	*irq;
61	struct resource	*dma;
62	void *intr_handler;
63	struct upd7210	upd7210;
64};
65
66static devclass_t pcii_devclass;
67
68static int	pcii_probe(device_t dev);
69static int	pcii_attach(device_t dev);
70
71static device_method_t pcii_methods[] = {
72	DEVMETHOD(device_probe,		pcii_probe),
73	DEVMETHOD(device_attach,	pcii_attach),
74	DEVMETHOD(device_suspend,	bus_generic_suspend),
75	DEVMETHOD(device_resume,	bus_generic_resume),
76
77	{ 0, 0 }
78};
79
80static driver_t pcii_driver = {
81	"pcii",
82	pcii_methods,
83	sizeof(struct pcii_softc),
84};
85
86static int
87pcii_probe(device_t dev)
88{
89	struct resource	*port;
90	int rid;
91	u_long start, count;
92	int i, j, error = 0;
93
94	device_set_desc(dev, "PCII IEEE-4888 controller");
95
96	rid = 0;
97	if (bus_get_resource(dev, SYS_RES_IOPORT, rid, &start, &count) != 0)
98		return ENXIO;
99	if ((start & 0x3ff) != 0x2e1)
100		return (ENXIO);
101	count = 1;
102	if (bus_set_resource(dev, SYS_RES_IOPORT, rid, start, count) != 0)
103		return ENXIO;
104	for (i = 0; i < 8; i++) {
105		j = bus_set_resource(dev, SYS_RES_IOPORT, i,
106		    start + 0x400 * i, 1);
107		if (j) {
108			error = ENXIO;
109			break;
110		}
111		rid = i;
112		port = bus_alloc_resource_any(dev, SYS_RES_IOPORT,
113		    &rid, RF_ACTIVE);
114		if (port == NULL)
115			return (ENXIO);
116		else
117			bus_release_resource(dev, SYS_RES_IOPORT, i, port);
118	}
119
120	rid = 0;
121	port = bus_alloc_resource_any(dev,
122	    SYS_RES_IRQ, &rid, RF_SHAREABLE | RF_ACTIVE);
123	if (port == NULL)
124		return (ENXIO);
125	bus_release_resource(dev, SYS_RES_IRQ, rid, port);
126
127	return (error);
128}
129
130static int
131pcii_attach(device_t dev)
132{
133	struct pcii_softc *sc;
134	int		unit;
135	int		rid;
136	int i, error = 0;
137
138	unit = device_get_unit(dev);
139	sc = device_get_softc(dev);
140	memset(sc, 0, sizeof *sc);
141
142	device_set_desc(dev, "PCII IEEE-4888 controller");
143
144	for (rid = 0; rid < 8; rid++) {
145		sc->port[rid] = bus_alloc_resource_any(dev,
146		    SYS_RES_IOPORT, &rid, RF_ACTIVE);
147		if (sc->port[rid] == NULL) {
148			error = ENXIO;
149			break;
150		}
151		sc->upd7210.reg_tag[rid] = rman_get_bustag(sc->port[rid]);
152		sc->upd7210.reg_handle[rid] = rman_get_bushandle(sc->port[rid]);
153	}
154	if (!error) {
155		rid = 0;
156		sc->irq = bus_alloc_resource_any(dev,
157		    SYS_RES_IRQ, &rid, RF_SHAREABLE | RF_ACTIVE);
158		if (sc->irq == NULL) {
159			error = ENXIO;
160		} else {
161			error = bus_setup_intr(dev, sc->irq,
162			    INTR_TYPE_MISC | INTR_MPSAFE,
163			    upd7210intr, &sc->upd7210, &sc->intr_handler);
164		}
165	}
166	if (!error) {
167		rid = 0;
168		sc->dma = bus_alloc_resource_any(dev,
169		    SYS_RES_DRQ, &rid, RF_ACTIVE | RF_SHAREABLE);
170		if (sc->dma == NULL)
171			sc->upd7210.dmachan = -1;
172		else
173			sc->upd7210.dmachan = rman_get_start(sc->dma);
174	}
175	if (error) {
176		for (i = 0; i < 8; i++) {
177			if (sc->port[i] == NULL)
178				break;
179			bus_release_resource(dev, SYS_RES_IOPORT,
180			    0, sc->port[i]);
181		}
182		if (sc->intr_handler != NULL)
183			bus_teardown_intr(dev, sc->irq, sc->intr_handler);
184		if (sc->irq != NULL)
185			bus_release_resource(dev, SYS_RES_IRQ, i, sc->irq);
186	}
187	upd7210attach(&sc->upd7210);
188	return (error);
189}
190
191DRIVER_MODULE(pcii, isa, pcii_driver, pcii_devclass, 0, 0);
192DRIVER_MODULE(pcii, acpi, pcii_driver, pcii_devclass, 0, 0);
193