tmc18c30_subr.c revision 113205
1113205Smdodd/*
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 * $FreeBSD: head/sys/dev/stg/tmc18c30_subr.c 113205 2003-04-07 10:13:25Z mdodd $
38113205Smdodd */
39113205Smdodd
40113205Smdodd#include <sys/param.h>
41113205Smdodd#include <sys/systm.h>
42113205Smdodd#include <sys/kernel.h>
43113205Smdodd#include <sys/socket.h>
44113205Smdodd
45113205Smdodd#include <sys/module.h>
46113205Smdodd#include <sys/bus.h>
47113205Smdodd
48113205Smdodd#include <machine/bus_memio.h>
49113205Smdodd#include <machine/bus_pio.h>
50113205Smdodd#include <machine/bus.h>
51113205Smdodd#include <machine/resource.h>
52113205Smdodd#include <sys/rman.h>
53113205Smdodd
54113205Smdodd#include <cam/scsi/scsi_low.h>
55113205Smdodd#include <cam/scsi/scsi_low_pisa.h>
56113205Smdodd
57113205Smdodd#include <dev/stg/tmc18c30reg.h>
58113205Smdodd#include <dev/stg/tmc18c30var.h>
59113205Smdodd#include <dev/stg/tmc18c30.h>
60113205Smdodd
61113205Smdodd#define	STG_HOSTID	7
62113205Smdodd
63113205Smdodddevclass_t stg_devclass;
64113205Smdodd
65113205Smdoddint
66113205Smdoddstg_alloc_resource(device_t dev)
67113205Smdodd{
68113205Smdodd	struct stg_softc *	sc = device_get_softc(dev);
69113205Smdodd	u_long			maddr, msize;
70113205Smdodd	int			error;
71113205Smdodd
72113205Smdodd	sc->port_res = bus_alloc_resource(dev, SYS_RES_IOPORT, &sc->port_rid,
73113205Smdodd					  0, ~0, 1, RF_ACTIVE);
74113205Smdodd	if (sc->port_res == NULL) {
75113205Smdodd		stg_release_resource(dev);
76113205Smdodd		return(ENOMEM);
77113205Smdodd	}
78113205Smdodd
79113205Smdodd	sc->irq_res = bus_alloc_resource(dev, SYS_RES_IRQ, &sc->irq_rid,
80113205Smdodd					 0, ~0, 1, RF_ACTIVE);
81113205Smdodd	if (sc->irq_res == NULL) {
82113205Smdodd		stg_release_resource(dev);
83113205Smdodd		return(ENOMEM);
84113205Smdodd	}
85113205Smdodd	error = bus_get_resource(dev, SYS_RES_MEMORY, 0, &maddr, &msize);
86113205Smdodd	if (error) {
87113205Smdodd		return(0);      /* XXX */
88113205Smdodd	}
89113205Smdodd
90113205Smdodd	/* no need to allocate memory if not configured */
91113205Smdodd	if (maddr == 0 || msize == 0) {
92113205Smdodd		return(0);
93113205Smdodd	}
94113205Smdodd
95113205Smdodd	sc->mem_res = bus_alloc_resource(dev, SYS_RES_MEMORY, &sc->mem_rid,
96113205Smdodd					 0, ~0, 1, RF_ACTIVE);
97113205Smdodd	if (sc->mem_res == NULL) {
98113205Smdodd		stg_release_resource(dev);
99113205Smdodd		return(ENOMEM);
100113205Smdodd	}
101113205Smdodd
102113205Smdodd	return(0);
103113205Smdodd}
104113205Smdodd
105113205Smdoddvoid
106113205Smdoddstg_release_resource(device_t dev)
107113205Smdodd{
108113205Smdodd	struct stg_softc	*sc = device_get_softc(dev);
109113205Smdodd
110113205Smdodd	if (sc->stg_intrhand)
111113205Smdodd		bus_teardown_intr(dev, sc->irq_res, sc->stg_intrhand);
112113205Smdodd	if (sc->port_res)
113113205Smdodd		bus_release_resource(dev, SYS_RES_IOPORT,
114113205Smdodd				     sc->port_rid, sc->port_res);
115113205Smdodd	if (sc->irq_res)
116113205Smdodd		bus_release_resource(dev, SYS_RES_IRQ,
117113205Smdodd				     sc->irq_rid, sc->irq_res);
118113205Smdodd	if (sc->mem_res)
119113205Smdodd		bus_release_resource(dev, SYS_RES_MEMORY,
120113205Smdodd				     sc->mem_rid, sc->mem_res);
121113205Smdodd	return;
122113205Smdodd}
123113205Smdodd
124113205Smdoddint
125113205Smdoddstg_probe(device_t dev)
126113205Smdodd{
127113205Smdodd	int rv;
128113205Smdodd	struct stg_softc *sc = device_get_softc(dev);
129113205Smdodd
130113205Smdodd	rv = stgprobesubr(rman_get_bustag(sc->port_res),
131113205Smdodd			  rman_get_bushandle(sc->port_res),
132113205Smdodd			  device_get_flags(dev));
133113205Smdodd
134113205Smdodd	return rv;
135113205Smdodd}
136113205Smdodd
137113205Smdoddint
138113205Smdoddstg_attach(device_t dev)
139113205Smdodd{
140113205Smdodd	struct stg_softc *sc;
141113205Smdodd	struct scsi_low_softc *slp;
142113205Smdodd	u_int32_t flags = device_get_flags(dev);
143113205Smdodd	intrmask_t s;
144113205Smdodd	char	dvname[16];
145113205Smdodd
146113205Smdodd	sc = device_get_softc(dev);
147113205Smdodd
148113205Smdodd	strcpy(dvname,"stg");
149113205Smdodd
150113205Smdodd	slp = &sc->sc_sclow;
151113205Smdodd	slp->sl_dev = dev;
152113205Smdodd	sc->sc_iot = rman_get_bustag(sc->port_res);
153113205Smdodd	sc->sc_ioh = rman_get_bushandle(sc->port_res);
154113205Smdodd
155113205Smdodd	slp->sl_hostid = STG_HOSTID;
156113205Smdodd	slp->sl_cfgflags = flags;
157113205Smdodd
158113205Smdodd	s = splcam();
159113205Smdodd	stgattachsubr(sc);
160113205Smdodd	splx(s);
161113205Smdodd
162113205Smdodd	return(STGIOSZ);
163113205Smdodd}
164113205Smdodd
165113205Smdoddvoid
166113205Smdoddstg_detach (device_t dev)
167113205Smdodd{
168113205Smdodd	struct stg_softc *sc = device_get_softc(dev);
169113205Smdodd	intrmask_t s;
170113205Smdodd
171113205Smdodd	printf("%s: unload\n",sc->sc_sclow.sl_xname);
172113205Smdodd	s = splcam();
173113205Smdodd	scsi_low_deactivate((struct scsi_low_softc *)sc);
174113205Smdodd	scsi_low_dettach(&sc->sc_sclow);
175113205Smdodd	splx(s);
176113205Smdodd	stg_release_resource(dev);
177113205Smdodd	return;
178113205Smdodd}
179113205Smdodd
180113205Smdoddvoid
181113205Smdoddstg_intr (void *arg)
182113205Smdodd{
183113205Smdodd	stgintr(arg);
184113205Smdodd	return;
185113205Smdodd}
186