tmc18c30_pccard.c revision 73280
1/*	$FreeBSD: head/sys/dev/stg/tmc18c30_pccard.c 73280 2001-03-01 17:09:09Z markm $	*/
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/disklabel.h>
45#if defined(__FreeBSD__) && __FreeBSD_version >= 500001
46#include <sys/bio.h>
47#endif
48#include <sys/buf.h>
49#include <sys/queue.h>
50#include <sys/malloc.h>
51#include <sys/errno.h>
52
53#include <vm/vm.h>
54
55#include <machine/bus.h>
56
57#include <machine/bus_pio.h>
58#include <i386/isa/isa_device.h>
59
60#include <machine/dvcfg.h>
61
62#if defined(__FreeBSD__) && __FreeBSD_version < 400001
63static struct stg_softc *stg_get_softc(int);
64extern struct stg_softc *stgdata[];
65#define DEVPORT_ALLOCSOFTCFUNC stg_get_softc
66#define DEVPORT_SOFTCARRAY     stgdata
67#endif
68#include <sys/device_port.h>
69
70#include <cam/scsi/scsi_low.h>
71#include <cam/scsi/scsi_low_pisa.h>
72
73#include <dev/stg/tmc18c30reg.h>
74#include <dev/stg/tmc18c30var.h>
75#if defined(__FreeBSD__) && __FreeBSD_version < 400001
76#include "stg.h"
77#endif
78
79#define	STG_HOSTID	7
80
81/* pccard support */
82#include	"card.h"
83#if NCARD > 0
84#include	<sys/kernel.h>
85#include	<sys/module.h>
86#if !defined(__FreeBSD__) || __FreeBSD_version < 500014
87#include	<sys/select.h>
88#endif
89#include	<pccard/cardinfo.h>
90#include	<pccard/slot.h>
91
92static	int	stgprobe(DEVPORT_PDEVICE devi);
93static	int	stgattach(DEVPORT_PDEVICE devi);
94
95static	void	stg_card_unload	__P((DEVPORT_PDEVICE));
96#if defined(__FreeBSD__) && __FreeBSD_version < 400001
97static	int	stg_card_init	__P((DEVPORT_PDEVICE));
98static	int	stg_card_intr	__P((DEVPORT_PDEVICE));
99#endif
100
101#if defined(__FreeBSD__) && __FreeBSD_version >= 400001
102/*
103 * Additional code for FreeBSD new-bus PCCard frontend
104 */
105
106static void
107stg_pccard_intr(void * arg)
108{
109	stgintr(arg);
110}
111
112static void
113stg_release_resource(DEVPORT_PDEVICE dev)
114{
115	struct stg_softc	*sc = device_get_softc(dev);
116
117	if (sc->stg_intrhand) {
118		bus_teardown_intr(dev, sc->irq_res, sc->stg_intrhand);
119	}
120
121	if (sc->port_res) {
122		bus_release_resource(dev, SYS_RES_IOPORT,
123				     sc->port_rid, sc->port_res);
124	}
125
126	if (sc->irq_res) {
127		bus_release_resource(dev, SYS_RES_IRQ,
128				     sc->irq_rid, sc->irq_res);
129	}
130
131	if (sc->mem_res) {
132		bus_release_resource(dev, SYS_RES_MEMORY,
133				     sc->mem_rid, sc->mem_res);
134	}
135}
136
137static int
138stg_alloc_resource(DEVPORT_PDEVICE dev)
139{
140	struct stg_softc	*sc = device_get_softc(dev);
141	u_long			ioaddr, iosize, maddr, msize;
142	int			error;
143
144	error = bus_get_resource(dev, SYS_RES_IOPORT, 0, &ioaddr, &iosize);
145	if (error || iosize < STGIOSZ) {
146		return(ENOMEM);
147	}
148
149	sc->port_rid = 0;
150	sc->port_res = bus_alloc_resource(dev, SYS_RES_IOPORT, &sc->port_rid,
151					  0, ~0, 0, RF_ACTIVE);
152	if (sc->port_res == NULL) {
153		stg_release_resource(dev);
154		return(ENOMEM);
155	}
156
157	sc->irq_rid = 0;
158	sc->irq_res = bus_alloc_resource(dev, SYS_RES_IRQ, &sc->irq_rid,
159					 0, ~0, 1, RF_ACTIVE);
160	if (sc->irq_res == NULL) {
161		stg_release_resource(dev);
162		return(ENOMEM);
163	}
164
165	error = bus_get_resource(dev, SYS_RES_MEMORY, 0, &maddr, &msize);
166	if (error) {
167		return(0);      /* XXX */
168	}
169
170	/* no need to allocate memory if not configured */
171	if (maddr == 0 || msize == 0) {
172		return(0);
173	}
174
175	sc->mem_rid = 0;
176	sc->mem_res = bus_alloc_resource(dev, SYS_RES_MEMORY, &sc->mem_rid,
177					 0, ~0, msize, RF_ACTIVE);
178	if (sc->mem_res == NULL) {
179		stg_release_resource(dev);
180		return(ENOMEM);
181	}
182
183	return(0);
184}
185
186static int
187stg_pccard_probe(DEVPORT_PDEVICE dev)
188{
189	struct stg_softc	*sc = device_get_softc(dev);
190	int			error;
191
192	bzero(sc, sizeof(struct stg_softc));
193
194	error = stg_alloc_resource(dev);
195	if (error) {
196		return(error);
197	}
198
199	if (stgprobe(dev) == 0) {
200		stg_release_resource(dev);
201		return(ENXIO);
202	}
203
204	stg_release_resource(dev);
205
206	return(0);
207}
208
209static int
210stg_pccard_attach(DEVPORT_PDEVICE dev)
211{
212	struct stg_softc	*sc = device_get_softc(dev);
213	int			error;
214
215	error = stg_alloc_resource(dev);
216	if (error) {
217		return(error);
218	}
219
220	error = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_CAM | INTR_ENTROPY,
221			       stg_pccard_intr, (void *)sc, &sc->stg_intrhand);
222	if (error) {
223		stg_release_resource(dev);
224		return(error);
225	}
226
227	if (stgattach(dev) == 0) {
228		stg_release_resource(dev);
229		return(ENXIO);
230	}
231
232	return(0);
233}
234
235static	void
236stg_pccard_detach(DEVPORT_PDEVICE dev)
237{
238	stg_card_unload(dev);
239	stg_release_resource(dev);
240}
241
242static device_method_t stg_pccard_methods[] = {
243	/* Device interface */
244	DEVMETHOD(device_probe,		stg_pccard_probe),
245	DEVMETHOD(device_attach,	stg_pccard_attach),
246	DEVMETHOD(device_detach,	stg_pccard_detach),
247
248	{ 0, 0 }
249};
250
251static driver_t stg_pccard_driver = {
252	"stg",
253	stg_pccard_methods,
254	sizeof(struct stg_softc),
255};
256
257static devclass_t stg_devclass;
258
259DRIVER_MODULE(stg, pccard, stg_pccard_driver, stg_devclass, 0, 0);
260
261#else
262
263PCCARD_MODULE(stg, stg_card_init,stg_card_unload, stg_card_intr, 0, cam_imask);
264
265#endif
266
267#if defined(__FreeBSD__) && __FreeBSD_version < 400001
268static struct stg_softc *
269stg_get_softc(int unit)
270{
271	struct stg_softc *sc;
272
273	if (unit >= NSTG) {
274		return(NULL);
275	}
276
277	if (stgdata[unit] == NULL) {
278		sc = malloc(sizeof(struct stg_softc), M_TEMP,M_NOWAIT);
279		if (sc == NULL) {
280			printf("stg_get_softc: cannot malloc!\n");
281			return(NULL);
282		}
283		stgdata[unit] = sc;
284	} else {
285		sc = stgdata[unit];
286	}
287
288	return(sc);
289}
290
291static	int
292stg_card_init(DEVPORT_PDEVICE devi)
293{
294	int unit = DEVPORT_PDEVUNIT(devi);
295
296	if (NSTG <= unit)
297		return (ENODEV);
298
299	printf("probe stg\n");
300	if (stgprobe(devi) == 0)
301		return (ENXIO);
302
303	printf("attach stg\n");
304	if (stgattach(devi) == 0)
305		return (ENXIO);
306
307	return (0);
308}
309
310static	int
311stg_card_intr(DEVPORT_PDEVICE devi)
312{
313	stgintr(DEVPORT_PDEVGET_SOFTC(devi));
314	return 1;
315}
316#endif
317
318static	void
319stg_card_unload(DEVPORT_PDEVICE devi)
320{
321	struct stg_softc *sc = DEVPORT_PDEVGET_SOFTC(devi);
322
323	printf("%s: unload\n",sc->sc_sclow.sl_xname);
324	scsi_low_deactivate((struct scsi_low_softc *)sc);
325        scsi_low_dettach(&sc->sc_sclow);
326}
327
328static	int
329stgprobe(DEVPORT_PDEVICE devi)
330{
331	int rv;
332#if defined(__FreeBSD__) && __FreeBSD_version >= 400001
333	struct stg_softc *sc = device_get_softc(devi);
334
335	rv = stgprobesubr(rman_get_bustag(sc->port_res),
336			  rman_get_bushandle(sc->port_res),
337			  DEVPORT_PDEVFLAGS(devi));
338#else
339	rv = stgprobesubr(I386_BUS_SPACE_IO,
340			  DEVPORT_PDEVIOBASE(devi), DEVPORT_PDEVFLAGS(devi));
341#endif
342
343	return rv;
344}
345
346static	int
347stgattach(DEVPORT_PDEVICE devi)
348{
349#if defined(__FreeBSD__) && __FreeBSD_version < 400001
350	int unit = DEVPORT_PDEVUNIT(devi);
351#endif
352	struct stg_softc *sc;
353	struct scsi_low_softc *slp;
354	u_int32_t flags = DEVPORT_PDEVFLAGS(devi);
355	u_int iobase = DEVPORT_PDEVIOBASE(devi);
356
357	char	dvname[16];
358
359	strcpy(dvname,"stg");
360
361#if defined(__FreeBSD__) && __FreeBSD_version < 400001
362	if (unit >= NSTG)
363	{
364		printf("%s: unit number too high\n",dvname);
365		return (0);
366	}
367#endif
368
369	if (iobase == 0)
370	{
371		printf("%s: no ioaddr is given\n", dvname);
372		return (0);
373	}
374
375	sc = DEVPORT_PDEVALLOC_SOFTC(devi);
376	if (sc == NULL) {
377		return(0);
378	}
379
380	slp = &sc->sc_sclow;
381#if defined(__FreeBSD__) && __FreeBSD_version >= 400001
382	slp->sl_dev = devi;
383	sc->sc_iot = rman_get_bustag(sc->port_res);
384	sc->sc_ioh = rman_get_bushandle(sc->port_res);
385#else
386	bzero(sc, sizeof(struct stg_softc));
387	strcpy(slp->sl_dev.dv_xname, dvname);
388	slp->sl_dev.dv_unit = unit;
389	sc->sc_iot = I386_BUS_SPACE_IO;
390	sc->sc_ioh = iobase;
391#endif
392
393	slp->sl_hostid = STG_HOSTID;
394	slp->sl_cfgflags = flags;
395
396	stgattachsubr(sc);
397
398	sc->sc_ih = stgintr;
399
400	return(STGIOSZ);
401}
402#endif /* NCARD>0 */
403