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: stable/10/sys/dev/stg/tmc18c30_subr.c 240325 2012-09-10 18:49:49Z jhb $");
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 
56 #include <dev/stg/tmc18c30reg.h>
57 #include <dev/stg/tmc18c30var.h>
58 #include <dev/stg/tmc18c30.h>
59 
60 #define	STG_HOSTID	7
61 
62 devclass_t stg_devclass;
63 
64 int
stg_alloc_resource(device_t dev)65 stg_alloc_resource(device_t dev)
66 {
67 	struct stg_softc *	sc = device_get_softc(dev);
68 	u_long			maddr, msize;
69 	int			error;
70 
71 	sc->port_res = bus_alloc_resource_any(dev, SYS_RES_IOPORT,
72 					      &sc->port_rid, RF_ACTIVE);
73 	if (sc->port_res == NULL) {
74 		stg_release_resource(dev);
75 		return(ENOMEM);
76 	}
77 
78 	sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid,
79 					     RF_ACTIVE);
80 	if (sc->irq_res == NULL) {
81 		stg_release_resource(dev);
82 		return(ENOMEM);
83 	}
84 	error = bus_get_resource(dev, SYS_RES_MEMORY, 0, &maddr, &msize);
85 	if (error) {
86 		return(0);      /* XXX */
87 	}
88 
89 	/* no need to allocate memory if not configured */
90 	if (maddr == 0 || msize == 0) {
91 		return(0);
92 	}
93 
94 	sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->mem_rid,
95 					     RF_ACTIVE);
96 	if (sc->mem_res == NULL) {
97 		stg_release_resource(dev);
98 		return(ENOMEM);
99 	}
100 
101 	return(0);
102 }
103 
104 void
stg_release_resource(device_t dev)105 stg_release_resource(device_t dev)
106 {
107 	struct stg_softc	*sc = device_get_softc(dev);
108 
109 	if (sc->stg_intrhand)
110 		bus_teardown_intr(dev, sc->irq_res, sc->stg_intrhand);
111 	if (sc->port_res)
112 		bus_release_resource(dev, SYS_RES_IOPORT,
113 				     sc->port_rid, sc->port_res);
114 	if (sc->irq_res)
115 		bus_release_resource(dev, SYS_RES_IRQ,
116 				     sc->irq_rid, sc->irq_res);
117 	if (sc->mem_res)
118 		bus_release_resource(dev, SYS_RES_MEMORY,
119 				     sc->mem_rid, sc->mem_res);
120 	return;
121 }
122 
123 int
stg_probe(device_t dev)124 stg_probe(device_t dev)
125 {
126 	int rv;
127 	struct stg_softc *sc = device_get_softc(dev);
128 
129 	rv = stgprobesubr(rman_get_bustag(sc->port_res),
130 			  rman_get_bushandle(sc->port_res),
131 			  device_get_flags(dev));
132 
133 	return rv;
134 }
135 
136 int
stg_attach(device_t dev)137 stg_attach(device_t dev)
138 {
139 	struct stg_softc *sc;
140 	struct scsi_low_softc *slp;
141 	u_int32_t flags = device_get_flags(dev);
142 	intrmask_t s;
143 	char	dvname[16];
144 
145 	sc = device_get_softc(dev);
146 
147 	strcpy(dvname,"stg");
148 
149 	slp = &sc->sc_sclow;
150 	slp->sl_dev = dev;
151 	sc->sc_iot = rman_get_bustag(sc->port_res);
152 	sc->sc_ioh = rman_get_bushandle(sc->port_res);
153 
154 	slp->sl_hostid = STG_HOSTID;
155 	slp->sl_cfgflags = flags;
156 
157 	s = splcam();
158 	stgattachsubr(sc);
159 	splx(s);
160 
161 	return(STGIOSZ);
162 }
163 
164 int
stg_detach(device_t dev)165 stg_detach (device_t dev)
166 {
167 	struct stg_softc *sc = device_get_softc(dev);
168 	intrmask_t s;
169 
170 	s = splcam();
171 	scsi_low_deactivate((struct scsi_low_softc *)sc);
172 	scsi_low_dettach(&sc->sc_sclow);
173 	splx(s);
174 	stg_release_resource(dev);
175 	return (0);
176 }
177 
178 void
stg_intr(void * arg)179 stg_intr (void *arg)
180 {
181 	stgintr(arg);
182 	return;
183 }
184