tmc18c30_isa.c revision 67845
1/*	$FreeBSD: head/sys/dev/stg/tmc18c30_isa.c 67845 2000-10-29 06:03:47Z non $	*/
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/disklabel.h>
48#if defined(__FreeBSD__) && __FreeBSD_version >= 500001
49#include <sys/bio.h>
50#endif
51#include <sys/buf.h>
52#include <sys/queue.h>
53#include <sys/malloc.h>
54#include <sys/errno.h>
55
56#include <vm/vm.h>
57
58#include <machine/bus_pio.h>
59#include <machine/bus.h>
60#include <machine/resource.h>
61#include <sys/rman.h>
62
63#include <isa/isavar.h>
64
65#include <machine/dvcfg.h>
66
67#include <sys/device_port.h>
68
69#include <cam/scsi/scsi_low.h>
70#include <isa/isa_common.h>
71#include <cam/scsi/scsi_low_pisa.h>
72
73#include <dev/stg/tmc18c30reg.h>
74#include <dev/stg/tmc18c30var.h>
75
76#define	STG_HOSTID	7
77
78#include	<sys/kernel.h>
79#include	<sys/module.h>
80#include	<sys/select.h>
81
82static	int	stgprobe(device_t devi);
83static	int	stgattach(device_t devi);
84
85static	void	stg_isa_unload	__P((device_t));
86
87static void
88stg_isa_intr(void * arg)
89{
90	stgintr(arg);
91}
92
93static void
94stg_release_resource(device_t dev)
95{
96	struct stg_softc	*sc = device_get_softc(dev);
97
98	if (sc->stg_intrhand) {
99		bus_teardown_intr(dev, sc->irq_res, sc->stg_intrhand);
100	}
101
102	if (sc->port_res) {
103		bus_release_resource(dev, SYS_RES_IOPORT,
104				     sc->port_rid, sc->port_res);
105	}
106
107	if (sc->irq_res) {
108		bus_release_resource(dev, SYS_RES_IRQ,
109				     sc->irq_rid, sc->irq_res);
110	}
111
112	if (sc->mem_res) {
113		bus_release_resource(dev, SYS_RES_MEMORY,
114				     sc->mem_rid, sc->mem_res);
115	}
116}
117
118static int
119stg_alloc_resource(device_t dev)
120{
121	struct stg_softc	*sc = device_get_softc(dev);
122	u_long			maddr, msize;
123	int			error;
124
125	sc->port_rid = 0;
126	sc->port_res = bus_alloc_resource(dev, SYS_RES_IOPORT, &sc->port_rid,
127					  0, ~0, STGIOSZ, RF_ACTIVE);
128	if (sc->port_res == NULL) {
129		stg_release_resource(dev);
130		return(ENOMEM);
131	}
132
133	sc->irq_rid = 0;
134	sc->irq_res = bus_alloc_resource(dev, SYS_RES_IRQ, &sc->irq_rid,
135					 0, ~0, 1, RF_ACTIVE);
136	if (sc->irq_res == NULL) {
137		stg_release_resource(dev);
138		return(ENOMEM);
139	}
140
141	error = bus_get_resource(dev, SYS_RES_MEMORY, 0, &maddr, &msize);
142	if (error) {
143		return(0);      /* XXX */
144	}
145
146	/* no need to allocate memory if not configured */
147	if (maddr == 0 || msize == 0) {
148		return(0);
149	}
150
151	sc->mem_rid = 0;
152	sc->mem_res = bus_alloc_resource(dev, SYS_RES_MEMORY, &sc->mem_rid,
153					 0, ~0, msize, RF_ACTIVE);
154	if (sc->mem_res == NULL) {
155		stg_release_resource(dev);
156		return(ENOMEM);
157	}
158
159	return(0);
160}
161
162static int
163stg_isa_probe(device_t dev)
164{
165	struct stg_softc	*sc = device_get_softc(dev);
166	int			error;
167
168	bzero(sc, sizeof(struct stg_softc));
169
170	error = stg_alloc_resource(dev);
171	if (error) {
172		return(error);
173	}
174
175	if (stgprobe(dev) == 0) {
176		stg_release_resource(dev);
177		return(ENXIO);
178	}
179
180	stg_release_resource(dev);
181
182	return(0);
183}
184
185static int
186stg_isa_attach(device_t dev)
187{
188	struct stg_softc	*sc = device_get_softc(dev);
189	int			error;
190
191	error = stg_alloc_resource(dev);
192	if (error) {
193		return(error);
194	}
195
196	error = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_CAM,
197			       stg_isa_intr, (void *)sc, &sc->stg_intrhand);
198	if (error) {
199		stg_release_resource(dev);
200		return(error);
201	}
202
203	if (stgattach(dev) == 0) {
204		stg_release_resource(dev);
205		return(ENXIO);
206	}
207
208	return(0);
209}
210
211static	void
212stg_isa_detach(device_t dev)
213{
214	stg_isa_unload(dev);
215	stg_release_resource(dev);
216}
217
218static device_method_t stg_isa_methods[] = {
219	/* Device interface */
220	DEVMETHOD(device_probe,		stg_isa_probe),
221	DEVMETHOD(device_attach,	stg_isa_attach),
222	DEVMETHOD(device_detach,	stg_isa_detach),
223
224	{ 0, 0 }
225};
226
227static driver_t stg_isa_driver = {
228	"stg",
229	stg_isa_methods,
230	sizeof(struct stg_softc),
231};
232
233static devclass_t stg_devclass;
234
235DRIVER_MODULE(stg, isa, stg_isa_driver, stg_devclass, 0, 0);
236
237static	void
238stg_isa_unload(device_t devi)
239{
240	struct stg_softc *sc = device_get_softc(devi);
241
242	printf("%s: unload\n",sc->sc_sclow.sl_xname);
243	scsi_low_deactivate((struct scsi_low_softc *)sc);
244	scsi_low_dettach(&sc->sc_sclow);
245}
246
247static	int
248stgprobe(device_t devi)
249{
250	int rv;
251	struct stg_softc *sc = device_get_softc(devi);
252
253	rv = stgprobesubr(rman_get_bustag(sc->port_res),
254			  rman_get_bushandle(sc->port_res),
255			  device_get_flags(devi));
256
257	return rv;
258}
259
260static	int
261stgattach(device_t devi)
262{
263	struct stg_softc *sc;
264	struct scsi_low_softc *slp;
265	u_int32_t flags = device_get_flags(devi);
266	u_int iobase = bus_get_resource_start(devi, SYS_RES_IOPORT, 0);
267
268	char	dvname[16];
269
270	strcpy(dvname,"stg");
271
272
273	if (iobase == 0)
274	{
275		printf("%s: no ioaddr is given\n", dvname);
276		return (0);
277	}
278
279	sc = device_get_softc(devi);
280	if (sc == NULL) {
281		return(0);
282	}
283
284	slp = &sc->sc_sclow;
285	slp->sl_dev = devi;
286	sc->sc_iot = rman_get_bustag(sc->port_res);
287	sc->sc_ioh = rman_get_bushandle(sc->port_res);
288
289	slp->sl_hostid = STG_HOSTID;
290	slp->sl_cfgflags = flags;
291
292	stgattachsubr(sc);
293
294	sc->sc_ih = stgintr;
295
296	return(STGIOSZ);
297}
298