tmc18c30_pccard.c revision 107363
1/*	$FreeBSD: head/sys/dev/stg/tmc18c30_pccard.c 107363 2002-11-28 01:15:01Z 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/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 <dev/pccard/pccarddevs.h>
54#include <dev/pccard/pccardvar.h>
55
56#include <cam/scsi/scsi_low.h>
57#include <cam/scsi/scsi_low_pisa.h>
58
59#include <dev/stg/tmc18c30reg.h>
60#include <dev/stg/tmc18c30var.h>
61
62#define	STG_HOSTID	7
63
64#include	<sys/kernel.h>
65#include	<sys/module.h>
66#if !defined(__FreeBSD__) || __FreeBSD_version < 500014
67#include	<sys/select.h>
68#endif
69#include	<pccard/cardinfo.h>
70#include	<pccard/slot.h>
71
72static	int	stgprobe(DEVPORT_PDEVICE devi);
73static	int	stgattach(DEVPORT_PDEVICE devi);
74
75static	void	stg_card_unload	(DEVPORT_PDEVICE);
76
77static const struct pccard_product stg_products[] = {
78	PCMCIA_CARD(FUTUREDOMAIN, SCSI2GO, 0),
79	PCMCIA_CARD(IBM, SCSICARD, 0),
80	PCMCIA_CARD(RATOC, REX5536, 0),
81	PCMCIA_CARD(RATOC, REX5536AM, 0),
82	PCMCIA_CARD(RATOC, REX5536M, 0),
83	{ NULL }
84};
85
86/*
87 * Additional code for FreeBSD new-bus PCCard frontend
88 */
89
90static void
91stg_pccard_intr(void * arg)
92{
93	stgintr(arg);
94}
95
96static void
97stg_release_resource(DEVPORT_PDEVICE dev)
98{
99	struct stg_softc	*sc = device_get_softc(dev);
100
101	if (sc->stg_intrhand) {
102		bus_teardown_intr(dev, sc->irq_res, sc->stg_intrhand);
103	}
104
105	if (sc->port_res) {
106		bus_release_resource(dev, SYS_RES_IOPORT,
107				     sc->port_rid, sc->port_res);
108	}
109
110	if (sc->irq_res) {
111		bus_release_resource(dev, SYS_RES_IRQ,
112				     sc->irq_rid, sc->irq_res);
113	}
114
115	if (sc->mem_res) {
116		bus_release_resource(dev, SYS_RES_MEMORY,
117				     sc->mem_rid, sc->mem_res);
118	}
119}
120
121static int
122stg_alloc_resource(DEVPORT_PDEVICE dev)
123{
124	struct stg_softc	*sc = device_get_softc(dev);
125	u_long			ioaddr, iosize, maddr, msize;
126	int			error;
127
128	error = bus_get_resource(dev, SYS_RES_IOPORT, 0, &ioaddr, &iosize);
129	if (error || iosize < STGIOSZ) {
130		return(ENOMEM);
131	}
132
133	sc->port_rid = 0;
134	sc->port_res = bus_alloc_resource(dev, SYS_RES_IOPORT, &sc->port_rid,
135					  0, ~0, STGIOSZ, RF_ACTIVE);
136	if (sc->port_res == NULL) {
137		stg_release_resource(dev);
138		return(ENOMEM);
139	}
140
141	sc->irq_rid = 0;
142	sc->irq_res = bus_alloc_resource(dev, SYS_RES_IRQ, &sc->irq_rid,
143					 0, ~0, 1, RF_ACTIVE);
144	if (sc->irq_res == NULL) {
145		stg_release_resource(dev);
146		return(ENOMEM);
147	}
148
149	error = bus_get_resource(dev, SYS_RES_MEMORY, 0, &maddr, &msize);
150	if (error) {
151		return(0);      /* XXX */
152	}
153
154	/* no need to allocate memory if not configured */
155	if (maddr == 0 || msize == 0) {
156		return(0);
157	}
158
159	sc->mem_rid = 0;
160	sc->mem_res = bus_alloc_resource(dev, SYS_RES_MEMORY, &sc->mem_rid,
161					 0, ~0, 1, RF_ACTIVE);
162	if (sc->mem_res == NULL) {
163		stg_release_resource(dev);
164		return(ENOMEM);
165	}
166
167	return(0);
168}
169
170static int stg_pccard_match(device_t dev)
171{
172  	const struct pccard_product *pp;
173
174	if ((pp = pccard_product_lookup(dev, stg_products,
175	    sizeof(stg_products[0]), NULL)) != NULL) {
176		device_set_desc(dev, pp->pp_name);
177		return(0);
178	}
179	return(EIO);
180}
181
182static int
183stg_pccard_probe(DEVPORT_PDEVICE dev)
184{
185	struct stg_softc	*sc = device_get_softc(dev);
186	int			error;
187
188	bzero(sc, sizeof(struct stg_softc));
189
190	error = stg_alloc_resource(dev);
191	if (error) {
192		return(error);
193	}
194
195	if (stgprobe(dev) == 0) {
196		stg_release_resource(dev);
197		return(ENXIO);
198	}
199
200	stg_release_resource(dev);
201
202	return(0);
203}
204
205static int
206stg_pccard_attach(DEVPORT_PDEVICE dev)
207{
208	struct stg_softc	*sc = device_get_softc(dev);
209	int			error;
210
211	error = stg_alloc_resource(dev);
212	if (error) {
213		return(error);
214	}
215
216	error = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_CAM | INTR_ENTROPY,
217			       stg_pccard_intr, (void *)sc, &sc->stg_intrhand);
218	if (error) {
219		stg_release_resource(dev);
220		return(error);
221	}
222
223	if (stgattach(dev) == 0) {
224		stg_release_resource(dev);
225		return(ENXIO);
226	}
227
228	return(0);
229}
230
231static	void
232stg_pccard_detach(DEVPORT_PDEVICE dev)
233{
234	stg_card_unload(dev);
235	stg_release_resource(dev);
236}
237
238static device_method_t stg_pccard_methods[] = {
239	/* Device interface */
240	DEVMETHOD(device_probe,		pccard_compat_probe),
241	DEVMETHOD(device_attach,	pccard_compat_attach),
242	DEVMETHOD(device_detach,	stg_pccard_detach),
243
244	/* Card interface */
245	DEVMETHOD(card_compat_match,	stg_pccard_match),
246	DEVMETHOD(card_compat_probe,	stg_pccard_probe),
247	DEVMETHOD(card_compat_attach,	stg_pccard_attach),
248
249	{ 0, 0 }
250};
251
252static driver_t stg_pccard_driver = {
253	"stg",
254	stg_pccard_methods,
255	sizeof(struct stg_softc),
256};
257
258static devclass_t stg_devclass;
259
260MODULE_DEPEND(stg, scsi_low, 1, 1, 1);
261DRIVER_MODULE(stg, pccard, stg_pccard_driver, stg_devclass, 0, 0);
262
263static	void
264stg_card_unload(DEVPORT_PDEVICE devi)
265{
266	struct stg_softc *sc = DEVPORT_PDEVGET_SOFTC(devi);
267	intrmask_t s;
268
269	printf("%s: unload\n",sc->sc_sclow.sl_xname);
270	s = splcam();
271	scsi_low_deactivate((struct scsi_low_softc *)sc);
272        scsi_low_dettach(&sc->sc_sclow);
273	splx(s);
274}
275
276static	int
277stgprobe(DEVPORT_PDEVICE devi)
278{
279	int rv;
280	struct stg_softc *sc = device_get_softc(devi);
281
282	rv = stgprobesubr(rman_get_bustag(sc->port_res),
283			  rman_get_bushandle(sc->port_res),
284			  DEVPORT_PDEVFLAGS(devi));
285
286	return rv;
287}
288
289static	int
290stgattach(DEVPORT_PDEVICE devi)
291{
292	struct stg_softc *sc;
293	struct scsi_low_softc *slp;
294	u_int32_t flags = DEVPORT_PDEVFLAGS(devi);
295	u_int iobase = DEVPORT_PDEVIOBASE(devi);
296	intrmask_t s;
297	char	dvname[16];
298
299	strcpy(dvname,"stg");
300
301	if (iobase == 0)
302	{
303		printf("%s: no ioaddr is given\n", dvname);
304		return (0);
305	}
306
307	sc = DEVPORT_PDEVALLOC_SOFTC(devi);
308	if (sc == NULL) {
309		return(0);
310	}
311
312	slp = &sc->sc_sclow;
313	slp->sl_dev = devi;
314	sc->sc_iot = rman_get_bustag(sc->port_res);
315	sc->sc_ioh = rman_get_bushandle(sc->port_res);
316
317	slp->sl_hostid = STG_HOSTID;
318	slp->sl_cfgflags = flags;
319
320	s = splcam();
321	stgattachsubr(sc);
322	splx(s);
323
324	return(STGIOSZ);
325}
326