1/*-
2 * [Ported for FreeBSD]
3 *  Copyright (c) 2000
4 *      Noriaki Mitsunaga, Mitsuru Iwasaki and Takanori Watanabe.
5 *      All rights reserved.
6 * [NetBSD for NEC PC-98 series]
7 *  Copyright (c) 1996, 1997, 1998
8 *	NetBSD/pc98 porting staff. All rights reserved.
9 *  Copyright (c) 1996, 1997, 1998
10 *	Naofumi HONDA. All rights reserved.
11 *  Copyright (c) 1996, 1997, 1998
12 *	Kouichi Matsuda. All rights reserved.
13 *
14 *  Redistribution and use in source and binary forms, with or without
15 *  modification, are permitted provided that the following conditions
16 *  are met:
17 *  1. Redistributions of source code must retain the above copyright
18 *     notice, this list of conditions and the following disclaimer.
19 *  2. Redistributions in binary form must reproduce the above copyright
20 *     notice, this list of conditions and the following disclaimer in the
21 *     documentation and/or other materials provided with the distribution.
22 *  3. The name of the author may not be used to endorse or promote products
23 *     derived from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
26 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
27 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
29 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
33 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
36 *
37 */
38
39#include <sys/cdefs.h>
40__FBSDID("$FreeBSD$");
41
42#include <sys/param.h>
43#include <sys/systm.h>
44#include <sys/kernel.h>
45#include <sys/socket.h>
46
47#include <sys/module.h>
48#include <sys/bus.h>
49
50#include <machine/bus.h>
51#include <machine/resource.h>
52#include <sys/rman.h>
53
54#include <cam/scsi/scsi_low.h>
55#include <cam/scsi/scsi_low_pisa.h>
56
57#include <dev/stg/tmc18c30reg.h>
58#include <dev/stg/tmc18c30var.h>
59#include <dev/stg/tmc18c30.h>
60
61#define	STG_HOSTID	7
62
63devclass_t stg_devclass;
64
65int
66stg_alloc_resource(device_t dev)
67{
68	struct stg_softc *	sc = device_get_softc(dev);
69	u_long			maddr, msize;
70	int			error;
71
72	sc->port_res = bus_alloc_resource_any(dev, SYS_RES_IOPORT,
73					      &sc->port_rid, RF_ACTIVE);
74	if (sc->port_res == NULL) {
75		stg_release_resource(dev);
76		return(ENOMEM);
77	}
78
79	sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid,
80					     RF_ACTIVE);
81	if (sc->irq_res == NULL) {
82		stg_release_resource(dev);
83		return(ENOMEM);
84	}
85	error = bus_get_resource(dev, SYS_RES_MEMORY, 0, &maddr, &msize);
86	if (error) {
87		return(0);      /* XXX */
88	}
89
90	/* no need to allocate memory if not configured */
91	if (maddr == 0 || msize == 0) {
92		return(0);
93	}
94
95	sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->mem_rid,
96					     RF_ACTIVE);
97	if (sc->mem_res == NULL) {
98		stg_release_resource(dev);
99		return(ENOMEM);
100	}
101
102	return(0);
103}
104
105void
106stg_release_resource(device_t dev)
107{
108	struct stg_softc	*sc = device_get_softc(dev);
109
110	if (sc->stg_intrhand)
111		bus_teardown_intr(dev, sc->irq_res, sc->stg_intrhand);
112	if (sc->port_res)
113		bus_release_resource(dev, SYS_RES_IOPORT,
114				     sc->port_rid, sc->port_res);
115	if (sc->irq_res)
116		bus_release_resource(dev, SYS_RES_IRQ,
117				     sc->irq_rid, sc->irq_res);
118	if (sc->mem_res)
119		bus_release_resource(dev, SYS_RES_MEMORY,
120				     sc->mem_rid, sc->mem_res);
121	return;
122}
123
124int
125stg_probe(device_t dev)
126{
127	int rv;
128	struct stg_softc *sc = device_get_softc(dev);
129
130	rv = stgprobesubr(rman_get_bustag(sc->port_res),
131			  rman_get_bushandle(sc->port_res),
132			  device_get_flags(dev));
133
134	return rv;
135}
136
137int
138stg_attach(device_t dev)
139{
140	struct stg_softc *sc;
141	struct scsi_low_softc *slp;
142	u_int32_t flags = device_get_flags(dev);
143	intrmask_t s;
144	char	dvname[16];
145
146	sc = device_get_softc(dev);
147
148	strcpy(dvname,"stg");
149
150	slp = &sc->sc_sclow;
151	slp->sl_dev = dev;
152	sc->sc_iot = rman_get_bustag(sc->port_res);
153	sc->sc_ioh = rman_get_bushandle(sc->port_res);
154
155	slp->sl_hostid = STG_HOSTID;
156	slp->sl_cfgflags = flags;
157
158	s = splcam();
159	stgattachsubr(sc);
160	splx(s);
161
162	return(STGIOSZ);
163}
164
165int
166stg_detach (device_t dev)
167{
168	struct stg_softc *sc = device_get_softc(dev);
169	intrmask_t s;
170
171	s = splcam();
172	scsi_low_deactivate((struct scsi_low_softc *)sc);
173	scsi_low_dettach(&sc->sc_sclow);
174	splx(s);
175	stg_release_resource(dev);
176	return (0);
177}
178
179void
180stg_intr (void *arg)
181{
182	stgintr(arg);
183	return;
184}
185