tmc18c30_pccard.c revision 103693
1/*	$FreeBSD: head/sys/dev/stg/tmc18c30_pccard.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/malloc.h>
45#include <sys/errno.h>
46
47#include <machine/bus.h>
48#include <machine/bus_pio.h>
49#include <machine/dvcfg.h>
50
51#include <sys/device_port.h>
52
53#include <cam/scsi/scsi_low.h>
54#include <cam/scsi/scsi_low_pisa.h>
55
56#include <dev/stg/tmc18c30reg.h>
57#include <dev/stg/tmc18c30var.h>
58
59#define	STG_HOSTID	7
60
61#include	<sys/kernel.h>
62#include	<sys/module.h>
63#if !defined(__FreeBSD__) || __FreeBSD_version < 500014
64#include	<sys/select.h>
65#endif
66#include	<pccard/cardinfo.h>
67#include	<pccard/slot.h>
68
69static	int	stgprobe(DEVPORT_PDEVICE devi);
70static	int	stgattach(DEVPORT_PDEVICE devi);
71
72static	void	stg_card_unload	(DEVPORT_PDEVICE);
73
74/*
75 * Additional code for FreeBSD new-bus PCCard frontend
76 */
77
78static void
79stg_pccard_intr(void * arg)
80{
81	stgintr(arg);
82}
83
84static void
85stg_release_resource(DEVPORT_PDEVICE 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(DEVPORT_PDEVICE dev)
111{
112	struct stg_softc	*sc = device_get_softc(dev);
113	u_long			ioaddr, iosize, maddr, msize;
114	int			error;
115
116	error = bus_get_resource(dev, SYS_RES_IOPORT, 0, &ioaddr, &iosize);
117	if (error || iosize < STGIOSZ) {
118		return(ENOMEM);
119	}
120
121	sc->port_rid = 0;
122	sc->port_res = bus_alloc_resource(dev, SYS_RES_IOPORT, &sc->port_rid,
123					  0, ~0, STGIOSZ, RF_ACTIVE);
124	if (sc->port_res == NULL) {
125		stg_release_resource(dev);
126		return(ENOMEM);
127	}
128
129	sc->irq_rid = 0;
130	sc->irq_res = bus_alloc_resource(dev, SYS_RES_IRQ, &sc->irq_rid,
131					 0, ~0, 1, RF_ACTIVE);
132	if (sc->irq_res == NULL) {
133		stg_release_resource(dev);
134		return(ENOMEM);
135	}
136
137	error = bus_get_resource(dev, SYS_RES_MEMORY, 0, &maddr, &msize);
138	if (error) {
139		return(0);      /* XXX */
140	}
141
142	/* no need to allocate memory if not configured */
143	if (maddr == 0 || msize == 0) {
144		return(0);
145	}
146
147	sc->mem_rid = 0;
148	sc->mem_res = bus_alloc_resource(dev, SYS_RES_MEMORY, &sc->mem_rid,
149					 0, ~0, 1, RF_ACTIVE);
150	if (sc->mem_res == NULL) {
151		stg_release_resource(dev);
152		return(ENOMEM);
153	}
154
155	return(0);
156}
157
158static int
159stg_pccard_probe(DEVPORT_PDEVICE dev)
160{
161	struct stg_softc	*sc = device_get_softc(dev);
162	int			error;
163
164	bzero(sc, sizeof(struct stg_softc));
165
166	error = stg_alloc_resource(dev);
167	if (error) {
168		return(error);
169	}
170
171	if (stgprobe(dev) == 0) {
172		stg_release_resource(dev);
173		return(ENXIO);
174	}
175
176	stg_release_resource(dev);
177
178	return(0);
179}
180
181static int
182stg_pccard_attach(DEVPORT_PDEVICE dev)
183{
184	struct stg_softc	*sc = device_get_softc(dev);
185	int			error;
186
187	error = stg_alloc_resource(dev);
188	if (error) {
189		return(error);
190	}
191
192	error = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_CAM | INTR_ENTROPY,
193			       stg_pccard_intr, (void *)sc, &sc->stg_intrhand);
194	if (error) {
195		stg_release_resource(dev);
196		return(error);
197	}
198
199	if (stgattach(dev) == 0) {
200		stg_release_resource(dev);
201		return(ENXIO);
202	}
203
204	return(0);
205}
206
207static	void
208stg_pccard_detach(DEVPORT_PDEVICE dev)
209{
210	stg_card_unload(dev);
211	stg_release_resource(dev);
212}
213
214static device_method_t stg_pccard_methods[] = {
215	/* Device interface */
216	DEVMETHOD(device_probe,		stg_pccard_probe),
217	DEVMETHOD(device_attach,	stg_pccard_attach),
218	DEVMETHOD(device_detach,	stg_pccard_detach),
219
220	{ 0, 0 }
221};
222
223static driver_t stg_pccard_driver = {
224	"stg",
225	stg_pccard_methods,
226	sizeof(struct stg_softc),
227};
228
229static devclass_t stg_devclass;
230
231MODULE_DEPEND(stg, scsi_low, 1, 1, 1);
232DRIVER_MODULE(stg, pccard, stg_pccard_driver, stg_devclass, 0, 0);
233
234static	void
235stg_card_unload(DEVPORT_PDEVICE devi)
236{
237	struct stg_softc *sc = DEVPORT_PDEVGET_SOFTC(devi);
238	intrmask_t s;
239
240	printf("%s: unload\n",sc->sc_sclow.sl_xname);
241	s = splcam();
242	scsi_low_deactivate((struct scsi_low_softc *)sc);
243        scsi_low_dettach(&sc->sc_sclow);
244	splx(s);
245}
246
247static	int
248stgprobe(DEVPORT_PDEVICE 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			  DEVPORT_PDEVFLAGS(devi));
256
257	return rv;
258}
259
260static	int
261stgattach(DEVPORT_PDEVICE devi)
262{
263	struct stg_softc *sc;
264	struct scsi_low_softc *slp;
265	u_int32_t flags = DEVPORT_PDEVFLAGS(devi);
266	u_int iobase = DEVPORT_PDEVIOBASE(devi);
267	intrmask_t s;
268	char	dvname[16];
269
270	strcpy(dvname,"stg");
271
272	if (iobase == 0)
273	{
274		printf("%s: no ioaddr is given\n", dvname);
275		return (0);
276	}
277
278	sc = DEVPORT_PDEVALLOC_SOFTC(devi);
279	if (sc == NULL) {
280		return(0);
281	}
282
283	slp = &sc->sc_sclow;
284	slp->sl_dev = devi;
285	sc->sc_iot = rman_get_bustag(sc->port_res);
286	sc->sc_ioh = rman_get_bushandle(sc->port_res);
287
288	slp->sl_hostid = STG_HOSTID;
289	slp->sl_cfgflags = flags;
290
291	s = splcam();
292	stgattachsubr(sc);
293	splx(s);
294
295	return(STGIOSZ);
296}
297