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