1139749Simp/*-
2113205Smdodd * [Ported for FreeBSD]
3113205Smdodd *  Copyright (c) 2000
4113205Smdodd *      Noriaki Mitsunaga, Mitsuru Iwasaki and Takanori Watanabe.
5113205Smdodd *      All rights reserved.
6113205Smdodd * [NetBSD for NEC PC-98 series]
7113205Smdodd *  Copyright (c) 1996, 1997, 1998
8113205Smdodd *	NetBSD/pc98 porting staff. All rights reserved.
9113205Smdodd *  Copyright (c) 1996, 1997, 1998
10113205Smdodd *	Naofumi HONDA. All rights reserved.
11113205Smdodd *  Copyright (c) 1996, 1997, 1998
12113205Smdodd *	Kouichi Matsuda. All rights reserved.
13113205Smdodd *
14113205Smdodd *  Redistribution and use in source and binary forms, with or without
15113205Smdodd *  modification, are permitted provided that the following conditions
16113205Smdodd *  are met:
17113205Smdodd *  1. Redistributions of source code must retain the above copyright
18113205Smdodd *     notice, this list of conditions and the following disclaimer.
19113205Smdodd *  2. Redistributions in binary form must reproduce the above copyright
20113205Smdodd *     notice, this list of conditions and the following disclaimer in the
21113205Smdodd *     documentation and/or other materials provided with the distribution.
22113205Smdodd *  3. The name of the author may not be used to endorse or promote products
23113205Smdodd *     derived from this software without specific prior written permission.
24113205Smdodd *
25113205Smdodd * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
26113205Smdodd * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
27113205Smdodd * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28113205Smdodd * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
29113205Smdodd * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30113205Smdodd * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31113205Smdodd * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32113205Smdodd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
33113205Smdodd * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34113205Smdodd * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35113205Smdodd * POSSIBILITY OF SUCH DAMAGE.
36113205Smdodd *
37113205Smdodd */
38113205Smdodd
39119420Sobrien#include <sys/cdefs.h>
40119420Sobrien__FBSDID("$FreeBSD$");
41119420Sobrien
42113205Smdodd#include <sys/param.h>
43113205Smdodd#include <sys/systm.h>
44113205Smdodd#include <sys/kernel.h>
45113205Smdodd#include <sys/socket.h>
46113205Smdodd
47113205Smdodd#include <sys/module.h>
48113205Smdodd#include <sys/bus.h>
49113205Smdodd
50113205Smdodd#include <machine/bus.h>
51113205Smdodd#include <machine/resource.h>
52113205Smdodd#include <sys/rman.h>
53113205Smdodd
54113205Smdodd#include <cam/scsi/scsi_low.h>
55113205Smdodd
56113205Smdodd#include <dev/stg/tmc18c30reg.h>
57113205Smdodd#include <dev/stg/tmc18c30var.h>
58113205Smdodd#include <dev/stg/tmc18c30.h>
59113205Smdodd
60113205Smdodd#define	STG_HOSTID	7
61113205Smdodd
62113205Smdodddevclass_t stg_devclass;
63113205Smdodd
64113205Smdoddint
65113205Smdoddstg_alloc_resource(device_t dev)
66113205Smdodd{
67113205Smdodd	struct stg_softc *	sc = device_get_softc(dev);
68113205Smdodd	u_long			maddr, msize;
69113205Smdodd	int			error;
70113205Smdodd
71127135Snjl	sc->port_res = bus_alloc_resource_any(dev, SYS_RES_IOPORT,
72127135Snjl					      &sc->port_rid, RF_ACTIVE);
73113205Smdodd	if (sc->port_res == NULL) {
74113205Smdodd		stg_release_resource(dev);
75113205Smdodd		return(ENOMEM);
76113205Smdodd	}
77113205Smdodd
78127135Snjl	sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid,
79127135Snjl					     RF_ACTIVE);
80113205Smdodd	if (sc->irq_res == NULL) {
81113205Smdodd		stg_release_resource(dev);
82113205Smdodd		return(ENOMEM);
83113205Smdodd	}
84113205Smdodd	error = bus_get_resource(dev, SYS_RES_MEMORY, 0, &maddr, &msize);
85113205Smdodd	if (error) {
86113205Smdodd		return(0);      /* XXX */
87113205Smdodd	}
88113205Smdodd
89113205Smdodd	/* no need to allocate memory if not configured */
90113205Smdodd	if (maddr == 0 || msize == 0) {
91113205Smdodd		return(0);
92113205Smdodd	}
93113205Smdodd
94127135Snjl	sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->mem_rid,
95127135Snjl					     RF_ACTIVE);
96113205Smdodd	if (sc->mem_res == NULL) {
97113205Smdodd		stg_release_resource(dev);
98113205Smdodd		return(ENOMEM);
99113205Smdodd	}
100113205Smdodd
101113205Smdodd	return(0);
102113205Smdodd}
103113205Smdodd
104113205Smdoddvoid
105113205Smdoddstg_release_resource(device_t dev)
106113205Smdodd{
107113205Smdodd	struct stg_softc	*sc = device_get_softc(dev);
108113205Smdodd
109113205Smdodd	if (sc->stg_intrhand)
110113205Smdodd		bus_teardown_intr(dev, sc->irq_res, sc->stg_intrhand);
111113205Smdodd	if (sc->port_res)
112113205Smdodd		bus_release_resource(dev, SYS_RES_IOPORT,
113113205Smdodd				     sc->port_rid, sc->port_res);
114113205Smdodd	if (sc->irq_res)
115113205Smdodd		bus_release_resource(dev, SYS_RES_IRQ,
116113205Smdodd				     sc->irq_rid, sc->irq_res);
117113205Smdodd	if (sc->mem_res)
118113205Smdodd		bus_release_resource(dev, SYS_RES_MEMORY,
119113205Smdodd				     sc->mem_rid, sc->mem_res);
120113205Smdodd	return;
121113205Smdodd}
122113205Smdodd
123113205Smdoddint
124113205Smdoddstg_probe(device_t dev)
125113205Smdodd{
126113205Smdodd	int rv;
127113205Smdodd	struct stg_softc *sc = device_get_softc(dev);
128113205Smdodd
129113205Smdodd	rv = stgprobesubr(rman_get_bustag(sc->port_res),
130113205Smdodd			  rman_get_bushandle(sc->port_res),
131113205Smdodd			  device_get_flags(dev));
132113205Smdodd
133113205Smdodd	return rv;
134113205Smdodd}
135113205Smdodd
136113205Smdoddint
137113205Smdoddstg_attach(device_t dev)
138113205Smdodd{
139113205Smdodd	struct stg_softc *sc;
140113205Smdodd	struct scsi_low_softc *slp;
141113205Smdodd	u_int32_t flags = device_get_flags(dev);
142113205Smdodd	intrmask_t s;
143113205Smdodd	char	dvname[16];
144113205Smdodd
145113205Smdodd	sc = device_get_softc(dev);
146113205Smdodd
147113205Smdodd	strcpy(dvname,"stg");
148113205Smdodd
149113205Smdodd	slp = &sc->sc_sclow;
150113205Smdodd	slp->sl_dev = dev;
151113205Smdodd	sc->sc_iot = rman_get_bustag(sc->port_res);
152113205Smdodd	sc->sc_ioh = rman_get_bushandle(sc->port_res);
153113205Smdodd
154113205Smdodd	slp->sl_hostid = STG_HOSTID;
155113205Smdodd	slp->sl_cfgflags = flags;
156113205Smdodd
157113205Smdodd	s = splcam();
158113205Smdodd	stgattachsubr(sc);
159113205Smdodd	splx(s);
160113205Smdodd
161113205Smdodd	return(STGIOSZ);
162113205Smdodd}
163113205Smdodd
164194023Savgint
165113205Smdoddstg_detach (device_t dev)
166113205Smdodd{
167113205Smdodd	struct stg_softc *sc = device_get_softc(dev);
168113205Smdodd	intrmask_t s;
169113205Smdodd
170113205Smdodd	s = splcam();
171113205Smdodd	scsi_low_deactivate((struct scsi_low_softc *)sc);
172113205Smdodd	scsi_low_dettach(&sc->sc_sclow);
173113205Smdodd	splx(s);
174113205Smdodd	stg_release_resource(dev);
175194023Savg	return (0);
176113205Smdodd}
177113205Smdodd
178113205Smdoddvoid
179113205Smdoddstg_intr (void *arg)
180113205Smdodd{
181113205Smdodd	stgintr(arg);
182113205Smdodd	return;
183113205Smdodd}
184