tmc18c30_isa.c revision 103693
1/*	$FreeBSD: head/sys/dev/stg/tmc18c30_isa.c 103693 2002-09-20 16:53:19Z phk $	*/
2/*	$NecBSD: tmc18c30_pisa.c,v 1.22 1998/11/26 01:59:21 honda Exp $	*/
3/*	$NetBSD$	*/
4
5/*
6 * [Ported for FreeBSD]
7 *  Copyright (c) 2000
8 *      Noriaki Mitsunaga, Mitsuru Iwasaki and Takanori Watanabe.
9 *      All rights reserved.
10 * [NetBSD for NEC PC-98 series]
11 *  Copyright (c) 1996, 1997, 1998
12 *	NetBSD/pc98 porting staff. All rights reserved.
13 *  Copyright (c) 1996, 1997, 1998
14 *	Naofumi HONDA. All rights reserved.
15 *  Copyright (c) 1996, 1997, 1998
16 *	Kouichi Matsuda. All rights reserved.
17 *
18 *  Redistribution and use in source and binary forms, with or without
19 *  modification, are permitted provided that the following conditions
20 *  are met:
21 *  1. Redistributions of source code must retain the above copyright
22 *     notice, this list of conditions and the following disclaimer.
23 *  2. Redistributions in binary form must reproduce the above copyright
24 *     notice, this list of conditions and the following disclaimer in the
25 *     documentation and/or other materials provided with the distribution.
26 *  3. The name of the author may not be used to endorse or promote products
27 *     derived from this software without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
30 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
31 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
32 * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
33 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
38 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39 * POSSIBILITY OF SUCH DAMAGE.
40 */
41
42#include <sys/param.h>
43#include <sys/systm.h>
44#include <sys/kernel.h>
45#include <sys/module.h>
46#include <sys/bus.h>
47#include <sys/malloc.h>
48#include <sys/errno.h>
49
50#include <machine/bus_pio.h>
51#include <machine/bus.h>
52#include <machine/resource.h>
53#include <sys/rman.h>
54
55#include <isa/isavar.h>
56
57#include <machine/dvcfg.h>
58
59#include <sys/device_port.h>
60
61#include <cam/scsi/scsi_low.h>
62#include <isa/isa_common.h>
63#include <cam/scsi/scsi_low_pisa.h>
64
65#include <dev/stg/tmc18c30reg.h>
66#include <dev/stg/tmc18c30var.h>
67
68#define	STG_HOSTID	7
69
70#include	<sys/kernel.h>
71#include	<sys/module.h>
72
73static	int	stgprobe(device_t devi);
74static	int	stgattach(device_t devi);
75
76static	void	stg_isa_unload	(device_t);
77
78static void
79stg_isa_intr(void * arg)
80{
81	stgintr(arg);
82}
83
84static void
85stg_release_resource(device_t dev)
86{
87	struct stg_softc	*sc = device_get_softc(dev);
88
89	if (sc->stg_intrhand) {
90		bus_teardown_intr(dev, sc->irq_res, sc->stg_intrhand);
91	}
92
93	if (sc->port_res) {
94		bus_release_resource(dev, SYS_RES_IOPORT,
95				     sc->port_rid, sc->port_res);
96	}
97
98	if (sc->irq_res) {
99		bus_release_resource(dev, SYS_RES_IRQ,
100				     sc->irq_rid, sc->irq_res);
101	}
102
103	if (sc->mem_res) {
104		bus_release_resource(dev, SYS_RES_MEMORY,
105				     sc->mem_rid, sc->mem_res);
106	}
107}
108
109static int
110stg_alloc_resource(device_t dev)
111{
112	struct stg_softc	*sc = device_get_softc(dev);
113	u_long			maddr, msize;
114	int			error;
115
116	sc->port_rid = 0;
117	sc->port_res = bus_alloc_resource(dev, SYS_RES_IOPORT, &sc->port_rid,
118					  0, ~0, STGIOSZ, RF_ACTIVE);
119	if (sc->port_res == NULL) {
120		stg_release_resource(dev);
121		return(ENOMEM);
122	}
123
124	sc->irq_rid = 0;
125	sc->irq_res = bus_alloc_resource(dev, SYS_RES_IRQ, &sc->irq_rid,
126					 0, ~0, 1, RF_ACTIVE);
127	if (sc->irq_res == NULL) {
128		stg_release_resource(dev);
129		return(ENOMEM);
130	}
131
132	error = bus_get_resource(dev, SYS_RES_MEMORY, 0, &maddr, &msize);
133	if (error) {
134		return(0);      /* XXX */
135	}
136
137	/* no need to allocate memory if not configured */
138	if (maddr == 0 || msize == 0) {
139		return(0);
140	}
141
142	sc->mem_rid = 0;
143	sc->mem_res = bus_alloc_resource(dev, SYS_RES_MEMORY, &sc->mem_rid,
144					 0, ~0, 1, RF_ACTIVE);
145	if (sc->mem_res == NULL) {
146		stg_release_resource(dev);
147		return(ENOMEM);
148	}
149
150	return(0);
151}
152
153static int
154stg_isa_probe(device_t dev)
155{
156	struct stg_softc	*sc = device_get_softc(dev);
157	int			error;
158
159	bzero(sc, sizeof(struct stg_softc));
160
161	error = stg_alloc_resource(dev);
162	if (error) {
163		return(error);
164	}
165
166	if (stgprobe(dev) == 0) {
167		stg_release_resource(dev);
168		return(ENXIO);
169	}
170
171	stg_release_resource(dev);
172
173	return(0);
174}
175
176static int
177stg_isa_attach(device_t dev)
178{
179	struct stg_softc	*sc = device_get_softc(dev);
180	int			error;
181
182	error = stg_alloc_resource(dev);
183	if (error) {
184		return(error);
185	}
186
187	error = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_CAM | INTR_ENTROPY,
188			       stg_isa_intr, (void *)sc, &sc->stg_intrhand);
189	if (error) {
190		stg_release_resource(dev);
191		return(error);
192	}
193
194	if (stgattach(dev) == 0) {
195		stg_release_resource(dev);
196		return(ENXIO);
197	}
198
199	return(0);
200}
201
202static	void
203stg_isa_detach(device_t dev)
204{
205	stg_isa_unload(dev);
206	stg_release_resource(dev);
207}
208
209static device_method_t stg_isa_methods[] = {
210	/* Device interface */
211	DEVMETHOD(device_probe,		stg_isa_probe),
212	DEVMETHOD(device_attach,	stg_isa_attach),
213	DEVMETHOD(device_detach,	stg_isa_detach),
214
215	{ 0, 0 }
216};
217
218static driver_t stg_isa_driver = {
219	"stg",
220	stg_isa_methods,
221	sizeof(struct stg_softc),
222};
223
224static devclass_t stg_devclass;
225
226DRIVER_MODULE(stg, isa, stg_isa_driver, stg_devclass, 0, 0);
227
228static	void
229stg_isa_unload(device_t devi)
230{
231	struct stg_softc *sc = device_get_softc(devi);
232	intrmask_t s;
233
234	printf("%s: unload\n",sc->sc_sclow.sl_xname);
235	s = splcam();
236	scsi_low_deactivate((struct scsi_low_softc *)sc);
237	scsi_low_dettach(&sc->sc_sclow);
238	splx(s);
239}
240
241static	int
242stgprobe(device_t devi)
243{
244	int rv;
245	struct stg_softc *sc = device_get_softc(devi);
246
247	rv = stgprobesubr(rman_get_bustag(sc->port_res),
248			  rman_get_bushandle(sc->port_res),
249			  device_get_flags(devi));
250
251	return rv;
252}
253
254static	int
255stgattach(device_t devi)
256{
257	struct stg_softc *sc;
258	struct scsi_low_softc *slp;
259	u_int32_t flags = device_get_flags(devi);
260	u_int iobase = bus_get_resource_start(devi, SYS_RES_IOPORT, 0);
261	intrmask_t s;
262	char	dvname[16];
263
264	strcpy(dvname,"stg");
265
266
267	if (iobase == 0)
268	{
269		printf("%s: no ioaddr is given\n", dvname);
270		return (0);
271	}
272
273	sc = device_get_softc(devi);
274	if (sc == NULL) {
275		return(0);
276	}
277
278	slp = &sc->sc_sclow;
279	slp->sl_dev = devi;
280	sc->sc_iot = rman_get_bustag(sc->port_res);
281	sc->sc_ioh = rman_get_bushandle(sc->port_res);
282
283	slp->sl_hostid = STG_HOSTID;
284	slp->sl_cfgflags = flags;
285
286	s = splcam();
287	stgattachsubr(sc);
288	splx(s);
289
290	return(STGIOSZ);
291}
292