1141121Sphk/*-
2141121Sphk * Copyright (c) 2005 Poul-Henning Kamp <phk@FreeBSD.org>
3202870Sjoerg * Copyright (c) 2010 Joerg Wunsch <joerg@FreeBSD.org>
4141121Sphk * All rights reserved.
5141121Sphk *
6141121Sphk * Redistribution and use in source and binary forms, with or without
7141121Sphk * modification, are permitted provided that the following conditions
8141121Sphk * are met:
9141121Sphk * 1. Redistributions of source code must retain the above copyright
10141398Sphk *    notice, this list of conditions and the following disclaimer.
11141121Sphk * 2. Redistributions in binary form must reproduce the above copyright
12141121Sphk *    notice, this list of conditions and the following disclaimer in the
13141121Sphk *    documentation and/or other materials provided with the distribution.
14141121Sphk *
15141398Sphk * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16141398Sphk * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17141398Sphk * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18141398Sphk * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19141398Sphk * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20141398Sphk * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21141398Sphk * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22141398Sphk * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23141398Sphk * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24141398Sphk * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25141398Sphk * SUCH DAMAGE.
26141121Sphk *
27230134Suqs * Driver for GPIB cards based on NEC ��PD7210 and compatibles.
28141398Sphk *
29141398Sphk * This driver just hooks up to the hardware and leaves all the interesting
30141398Sphk * stuff to upd7210.c.
31141398Sphk *
32141121Sphk * Supported hardware:
33141123Sphk *    PCIIA compatible cards.
34141123Sphk *
35141123Sphk *    Tested and known working:
36141121Sphk *	"B&C Microsystems PC488A-0"
37202870Sjoerg *	"National Instruments GPIB-PCII/PCIIA" (in PCIIa mode)
38202870Sjoerg *	"Axiom AX5488"
39141121Sphk *
40141121Sphk */
41141121Sphk
42141121Sphk#include <sys/cdefs.h>
43141121Sphk__FBSDID("$FreeBSD$");
44141121Sphk
45141121Sphk#include <sys/param.h>
46141121Sphk#include <sys/systm.h>
47141398Sphk#include <sys/lock.h>
48141398Sphk#include <sys/mutex.h>
49141121Sphk#include <sys/kernel.h>
50141121Sphk#include <sys/module.h>
51141121Sphk#include <sys/bus.h>
52141121Sphk#include <machine/bus.h>
53141121Sphk#include <machine/resource.h>
54141121Sphk#include <sys/rman.h>
55141398Sphk#include <isa/isavar.h>
56141121Sphk
57141747Sphk#define UPD7210_HW_DRIVER
58141398Sphk#include <dev/ieee488/upd7210.h>
59141121Sphk
60141121Sphkstruct pcii_softc {
61141121Sphk	int foo;
62202870Sjoerg	struct resource	*res[11];
63141121Sphk	void *intr_handler;
64141121Sphk	struct upd7210	upd7210;
65141121Sphk};
66141121Sphk
67141121Sphkstatic devclass_t pcii_devclass;
68141121Sphk
69141121Sphkstatic int	pcii_probe(device_t dev);
70141121Sphkstatic int	pcii_attach(device_t dev);
71141121Sphk
72141121Sphkstatic device_method_t pcii_methods[] = {
73141121Sphk	DEVMETHOD(device_probe,		pcii_probe),
74141121Sphk	DEVMETHOD(device_attach,	pcii_attach),
75141121Sphk	DEVMETHOD(device_suspend,	bus_generic_suspend),
76141121Sphk	DEVMETHOD(device_resume,	bus_generic_resume),
77141121Sphk
78141121Sphk	{ 0, 0 }
79141121Sphk};
80141121Sphk
81150525Sphkstatic struct resource_spec pcii_res_spec[] = {
82150525Sphk	{ SYS_RES_IRQ,		0, RF_ACTIVE | RF_SHAREABLE},
83150525Sphk	{ SYS_RES_DRQ,		0, RF_ACTIVE | RF_SHAREABLE | RF_OPTIONAL},
84150525Sphk	{ SYS_RES_IOPORT,	0, RF_ACTIVE},
85202870Sjoerg	{ SYS_RES_IOPORT,	1, RF_ACTIVE},
86202870Sjoerg	{ SYS_RES_IOPORT,	2, RF_ACTIVE},
87202870Sjoerg	{ SYS_RES_IOPORT,	3, RF_ACTIVE},
88202870Sjoerg	{ SYS_RES_IOPORT,	4, RF_ACTIVE},
89202870Sjoerg	{ SYS_RES_IOPORT,	5, RF_ACTIVE},
90202870Sjoerg	{ SYS_RES_IOPORT,	6, RF_ACTIVE},
91202870Sjoerg	{ SYS_RES_IOPORT,	7, RF_ACTIVE},
92202870Sjoerg	{ SYS_RES_IOPORT,	8, RF_ACTIVE | RF_SHAREABLE},
93150525Sphk	{ -1, 0, 0 }
94150525Sphk};
95150525Sphk
96141121Sphkstatic driver_t pcii_driver = {
97141121Sphk	"pcii",
98141121Sphk	pcii_methods,
99141423Sphk	sizeof(struct pcii_softc),
100141121Sphk};
101141121Sphk
102141121Sphkstatic int
103141121Sphkpcii_probe(device_t dev)
104141121Sphk{
105150525Sphk	int rid, i, j;
106202870Sjoerg	u_long start, count, addr;
107150525Sphk	int error = 0;
108150525Sphk	struct pcii_softc *sc;
109141121Sphk
110141121Sphk	device_set_desc(dev, "PCII IEEE-4888 controller");
111150525Sphk	sc = device_get_softc(dev);
112141121Sphk
113141121Sphk	rid = 0;
114141121Sphk	if (bus_get_resource(dev, SYS_RES_IOPORT, rid, &start, &count) != 0)
115141121Sphk		return ENXIO;
116202870Sjoerg	/*
117202870Sjoerg	 * The PCIIA decodes a fixed pattern of 0x2e1 for the lower 10
118202870Sjoerg	 * address bits A0 ... A9.  Bits A10 through A12 are used by
119230134Suqs	 * the ��PD7210 register select lines.  This makes the
120202870Sjoerg	 * individual 7210 register being 0x400 bytes apart in the ISA
121202870Sjoerg	 * bus address space.  Address bits A13 and A14 are compared
122202870Sjoerg	 * to a DIP switch setting on the card, allowing for up to 4
123202870Sjoerg	 * different cards being installed (at base addresses 0x2e1,
124202870Sjoerg	 * 0x22e1, 0x42e1, and 0x62e1, respectively).  A15 has been
125202870Sjoerg	 * used to select an optional on-board time-of-day clock chip
126230134Suqs	 * (MM58167A) on the original PCIIA rather than the ��PD7210
127202870Sjoerg	 * (which is not implemented on later boards).  The
128202870Sjoerg	 * documentation states the respective addresses for that chip
129202870Sjoerg	 * should be handled as reserved addresses, which we don't do
130202870Sjoerg	 * (right now).  Finally, the IO addresses 0x2f0 ... 0x2f7 for
131202870Sjoerg	 * a "special interrupt handling feature" (re-enable
132202870Sjoerg	 * interrupts so the IRQ can be shared).
133202870Sjoerg	 *
134202870Sjoerg	 * Usually, the user will only set the base address in the
135202870Sjoerg	 * device hints, so we handle the rest here.
136202870Sjoerg	 *
137202870Sjoerg	 * (Source: GPIB-PCIIA Technical Reference Manual, September
138202870Sjoerg	 * 1989 Edition, National Instruments.)
139202870Sjoerg	 */
140202870Sjoerg	if ((start & 0x3ff) != 0x2e1) {
141202898Sjoerg		if (bootverbose)
142202898Sjoerg			printf("pcii_probe: PCIIA base address 0x%lx not "
143202898Sjoerg			       "0x2e1/0x22e1/0x42e1/0x62e1\n",
144202898Sjoerg			       start);
145141121Sphk		return (ENXIO);
146202870Sjoerg	}
147202870Sjoerg
148202870Sjoerg	for (rid = 0, addr = start; rid < 8; rid++, addr += 0x400) {
149202870Sjoerg		if (bus_set_resource(dev, SYS_RES_IOPORT, rid, addr, 1) != 0) {
150202870Sjoerg			printf("pcii_probe: could not set IO port 0x%lx\n",
151202870Sjoerg			       addr);
152202870Sjoerg			return (ENXIO);
153202870Sjoerg		}
154202870Sjoerg	}
155202870Sjoerg	if (bus_get_resource(dev, SYS_RES_IRQ, 0, &start, &count) != 0) {
156202870Sjoerg		printf("pcii_probe: cannot obtain IRQ level\n");
157141121Sphk		return ENXIO;
158202870Sjoerg	}
159202870Sjoerg	if (start > 7) {
160202870Sjoerg		printf("pcii_probe: IRQ level %lu too high\n", start);
161202870Sjoerg		return ENXIO;
162202870Sjoerg	}
163202870Sjoerg
164202870Sjoerg	if (bus_set_resource(dev, SYS_RES_IOPORT, 8, 0x2f0 + start, 1) != 0) {
165202870Sjoerg		printf("pcii_probe: could not set IO port 0x%3lx\n",
166202870Sjoerg		       0x2f0 + start);
167202870Sjoerg		return (ENXIO);
168202870Sjoerg	}
169202870Sjoerg
170150525Sphk	error = bus_alloc_resources(dev, pcii_res_spec, sc->res);
171202870Sjoerg	if (error) {
172202870Sjoerg		printf("pcii_probe: Could not allocate resources\n");
173150525Sphk		return (error);
174202870Sjoerg	}
175150525Sphk	error = ENXIO;
176202870Sjoerg	/*
177230134Suqs	 * Perform some basic tests on the ��PD7210 registers.  At
178202870Sjoerg	 * least *some* register must read different from 0x00 or
179202870Sjoerg	 * 0xff.
180202870Sjoerg	 */
181141121Sphk	for (i = 0; i < 8; i++) {
182202870Sjoerg		j = bus_read_1(sc->res[2 + i], 0);
183150525Sphk		if (j != 0x00 && j != 0xff)
184150525Sphk			error = 0;
185150525Sphk	}
186202870Sjoerg	/* SPSR/SPMR read/write test */
187150525Sphk	if (!error) {
188202870Sjoerg		bus_write_1(sc->res[2 + 3], 0, 0x55);
189202870Sjoerg		if (bus_read_1(sc->res[2 + 3], 0) != 0x55)
190141121Sphk			error = ENXIO;
191141121Sphk	}
192150525Sphk	if (!error) {
193202870Sjoerg		bus_write_1(sc->res[2 + 3], 0, 0xaa);
194202870Sjoerg		if (bus_read_1(sc->res[2 + 3], 0) != 0xaa)
195150525Sphk			error = ENXIO;
196150525Sphk	}
197202870Sjoerg	if (error)
198202870Sjoerg		printf("pcii_probe: probe failure\n");
199202870Sjoerg
200150525Sphk	bus_release_resources(dev, pcii_res_spec, sc->res);
201141121Sphk	return (error);
202141121Sphk}
203141121Sphk
204141121Sphkstatic int
205141121Sphkpcii_attach(device_t dev)
206141121Sphk{
207141121Sphk	struct pcii_softc *sc;
208202870Sjoerg	u_long		start, count;
209141121Sphk	int		unit;
210141121Sphk	int		rid;
211150525Sphk	int		error = 0;
212141121Sphk
213141121Sphk	unit = device_get_unit(dev);
214141121Sphk	sc = device_get_softc(dev);
215141121Sphk	memset(sc, 0, sizeof *sc);
216141121Sphk
217141121Sphk	device_set_desc(dev, "PCII IEEE-4888 controller");
218141121Sphk
219202870Sjoerg	if (bus_get_resource(dev, SYS_RES_IRQ, 0, &start, &count) != 0) {
220202870Sjoerg		printf("pcii_attach: cannot obtain IRQ number\n");
221202870Sjoerg		return ENXIO;
222202870Sjoerg	}
223202870Sjoerg
224150525Sphk	error = bus_alloc_resources(dev, pcii_res_spec, sc->res);
225150525Sphk	if (error)
226150525Sphk		return (error);
227150525Sphk
228150525Sphk	error = bus_setup_intr(dev, sc->res[0],
229166914Simp	    INTR_TYPE_MISC | INTR_MPSAFE, NULL,
230150525Sphk	    upd7210intr, &sc->upd7210, &sc->intr_handler);
231150525Sphk	if (error) {
232150525Sphk		bus_release_resources(dev, pcii_res_spec, sc->res);
233150525Sphk		return (error);
234150525Sphk	}
235150525Sphk
236141121Sphk	for (rid = 0; rid < 8; rid++) {
237202870Sjoerg		sc->upd7210.reg_res[rid] = sc->res[2 + rid];
238202898Sjoerg		sc->upd7210.reg_offset[rid] = 0;
239141121Sphk	}
240202870Sjoerg	sc->upd7210.irq_clear_res = sc->res[10];
241203360Sjoerg	sc->upd7210.use_fifo = 0;
242150525Sphk
243150525Sphk	if (sc->res[1] == NULL)
244150525Sphk		sc->upd7210.dmachan = -1;
245150525Sphk	else
246150525Sphk		sc->upd7210.dmachan = rman_get_start(sc->res[1]);
247150525Sphk
248141121Sphk	upd7210attach(&sc->upd7210);
249203360Sjoerg	device_printf(dev, "attached gpib%d\n", sc->upd7210.unit);
250203360Sjoerg
251203360Sjoerg	return (0);
252141121Sphk}
253141121Sphk
254141121SphkDRIVER_MODULE(pcii, isa, pcii_driver, pcii_devclass, 0, 0);
255141121SphkDRIVER_MODULE(pcii, acpi, pcii_driver, pcii_devclass, 0, 0);
256