FreeBSD/Linux Kernel Cross Reference
sys/dev/auxio/auxio.c
1 /*-
2 * Copyright (c) 2004 Pyun YongHyeon
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 */
27
28 /* $NetBSD: auxio.c,v 1.11 2003/07/15 03:36:04 lukem Exp $ */
29
30 /*-
31 * Copyright (c) 2000, 2001 Matthew R. Green
32 * All rights reserved.
33 *
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions
36 * are met:
37 * 1. Redistributions of source code must retain the above copyright
38 * notice, this list of conditions and the following disclaimer.
39 * 2. Redistributions in binary form must reproduce the above copyright
40 * notice, this list of conditions and the following disclaimer in the
41 * documentation and/or other materials provided with the distribution.
42 * 3. The name of the author may not be used to endorse or promote products
43 * derived from this software without specific prior written permission.
44 *
45 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
46 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
47 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
48 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
49 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
50 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
51 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
52 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
53 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55 * SUCH DAMAGE.
56 */
57
58 /*
59 * AUXIO registers support on the sbus & ebus2, used for the floppy driver
60 * and to control the system LED, for the BLINK option.
61 */
62
63 #include <sys/cdefs.h>
64 __FBSDID("$FreeBSD: releng/5.4/sys/dev/auxio/auxio.c 141016 2005-01-30 01:00:13Z imp $");
65
66 #include <sys/param.h>
67 #include <sys/systm.h>
68 #include <sys/bus.h>
69 #include <sys/kernel.h>
70 #include <sys/lock.h>
71 #include <sys/module.h>
72 #include <sys/mutex.h>
73 #include <sys/resource.h>
74 #include <sys/rman.h>
75
76 #include <dev/led/led.h>
77 #include <dev/ofw/ofw_bus.h>
78 #include <dev/ofw/openfirm.h>
79
80 #include <machine/bus.h>
81 #include <machine/ofw_machdep.h>
82 #include <machine/resource.h>
83
84 #include <sparc64/sbus/sbusvar.h>
85 #include <dev/auxio/auxioreg.h>
86
87 /*
88 * on sun4u, auxio exists with one register (LED) on the sbus, and 5
89 * registers on the ebus2 (pci) (LED, PCIMODE, FREQUENCY, SCSI
90 * OSCILLATOR, and TEMP SENSE.
91 */
92
93 #define AUXIO_PCIO_LED 0
94 #define AUXIO_PCIO_PCI 1
95 #define AUXIO_PCIO_FREQ 2
96 #define AUXIO_PCIO_OSC 3
97 #define AUXIO_PCIO_TEMP 4
98 #define AUXIO_PCIO_MAX 8
99
100 struct auxio_softc {
101 struct device *sc_dev;
102
103 int sc_nauxio;
104 struct resource *sc_res[AUXIO_PCIO_MAX];
105 int sc_rid[AUXIO_PCIO_MAX];
106 bus_space_tag_t sc_regt[AUXIO_PCIO_MAX];
107 bus_space_handle_t sc_regh[AUXIO_PCIO_MAX];
108 struct cdev *sc_led_dev;
109 u_int32_t sc_led_stat;
110
111 int sc_flags;
112 #define AUXIO_LEDONLY 0x1
113 #define AUXIO_EBUS 0x2
114 #define AUXIO_SBUS 0x4
115
116 struct mtx sc_lock;
117 };
118
119 static void auxio_led_func(void *arg, int onoff);
120 static void auxio_attach_common(struct auxio_softc *);
121 static int auxio_bus_probe(device_t);
122 static int auxio_sbus_attach(device_t);
123 static int auxio_ebus_attach(device_t);
124 static int auxio_bus_detach(device_t);
125 static void auxio_free_resource(struct auxio_softc *);
126 static __inline u_int32_t auxio_led_read(struct auxio_softc *);
127 static __inline void auxio_led_write(struct auxio_softc *, u_int32_t);
128
129 /* SBus */
130 static device_method_t auxio_sbus_methods[] = {
131 DEVMETHOD(device_probe, auxio_bus_probe),
132 DEVMETHOD(device_attach, auxio_sbus_attach),
133 DEVMETHOD(device_detach, auxio_bus_detach),
134 {0, 0}
135 };
136
137 static driver_t auxio_sbus_driver = {
138 "auxio",
139 auxio_sbus_methods,
140 sizeof(struct auxio_softc)
141 };
142
143 static devclass_t auxio_devclass;
144 DRIVER_MODULE(auxio, sbus, auxio_sbus_driver, auxio_devclass, 0, 0);
145
146 /* EBus */
147 static device_method_t auxio_ebus_methods[] = {
148 DEVMETHOD(device_probe, auxio_bus_probe),
149 DEVMETHOD(device_attach, auxio_ebus_attach),
150 DEVMETHOD(device_detach, auxio_bus_detach),
151 {0, 0}
152 };
153
154 static driver_t auxio_ebus_driver = {
155 "auxio",
156 auxio_ebus_methods,
157 sizeof(struct auxio_softc)
158 };
159
160 DRIVER_MODULE(auxio, ebus, auxio_ebus_driver, auxio_devclass, 0, 0);
161 MODULE_VERSION(auxio, 1);
162
163 #define AUXIO_LOCK_INIT(sc) \
164 mtx_init(&sc->sc_lock, "auxio mtx", NULL, MTX_DEF)
165 #define AUXIO_LOCK(sc) mtx_lock(&sc->sc_lock)
166 #define AUXIO_UNLOCK(sc) mtx_unlock(&sc->sc_lock)
167 #define AUXIO_LOCK_DESTROY(sc) mtx_destroy(&sc->sc_lock)
168
169 static __inline void
170 auxio_led_write(struct auxio_softc *sc, u_int32_t v)
171 {
172 if (sc->sc_flags & AUXIO_EBUS)
173 bus_space_write_4(sc->sc_regt[AUXIO_PCIO_LED],
174 sc->sc_regh[AUXIO_PCIO_LED], 0, v);
175 else
176 bus_space_write_1(sc->sc_regt[AUXIO_PCIO_LED],
177 sc->sc_regh[AUXIO_PCIO_LED], 0, v);
178 }
179
180 static __inline u_int32_t
181 auxio_led_read(struct auxio_softc *sc)
182 {
183 u_int32_t led;
184
185 if (sc->sc_flags & AUXIO_EBUS)
186 led = bus_space_read_4(sc->sc_regt[AUXIO_PCIO_LED],
187 sc->sc_regh[AUXIO_PCIO_LED], 0);
188 else
189 led = bus_space_read_1(sc->sc_regt[AUXIO_PCIO_LED],
190 sc->sc_regh[AUXIO_PCIO_LED], 0);
191
192 return (led);
193 }
194
195 static void
196 auxio_led_func(void *arg, int onoff)
197 {
198 struct auxio_softc *sc;
199 u_int32_t led;
200
201 sc = (struct auxio_softc *)arg;
202
203 led = onoff ? AUXIO_LED_LED : 0;
204 AUXIO_LOCK(sc);
205 auxio_led_write(sc, led);
206 AUXIO_UNLOCK(sc);
207 }
208
209 static int
210 auxio_bus_probe(device_t dev)
211 {
212 const char *name;
213
214 name = ofw_bus_get_name(dev);
215 if (strcmp("auxio", name) == 0) {
216 device_set_desc(dev, "Sun Auxiliary I/O");
217 return (0);
218 }
219
220 return (ENXIO);
221 }
222
223 static int
224 auxio_ebus_attach(device_t dev)
225 {
226 struct auxio_softc *sc;
227 struct resource *res;
228 u_long start, count;
229 int i;
230
231 sc = device_get_softc(dev);
232 bzero(sc, sizeof(*sc));
233 sc->sc_dev = dev;
234
235 AUXIO_LOCK_INIT(sc);
236 sc->sc_nauxio = AUXIO_PCIO_MAX;
237 sc->sc_flags = AUXIO_LEDONLY | AUXIO_EBUS;
238 for (i = 0;
239 i < AUXIO_PCIO_MAX &&
240 bus_get_resource(dev, SYS_RES_IOPORT, i, &start, &count) == 0;
241 i++) {
242 sc->sc_rid[i] = i;
243 if (bootverbose)
244 device_printf(sc->sc_dev,
245 "Got rid %d, start %#lx, count %#lx\n",
246 i, start, count);
247 res = bus_alloc_resource_any(dev, SYS_RES_IOPORT,
248 &sc->sc_rid[i], RF_ACTIVE);
249 if (res == NULL) {
250 device_printf(sc->sc_dev,
251 "could not allocate resources\n");
252 goto attach_fail;
253 }
254 sc->sc_res[i] = res;
255 sc->sc_regt[i] = rman_get_bustag(res);
256 sc->sc_regh[i] = rman_get_bushandle(res);
257 }
258 sc->sc_nauxio = i;
259 auxio_attach_common(sc);
260
261 return (0);
262
263 attach_fail:
264 auxio_free_resource(sc);
265
266 return (ENXIO);
267 }
268
269 static void
270 auxio_attach_common(struct auxio_softc *sc)
271 {
272 sc->sc_led_stat = auxio_led_read(sc);
273 sc->sc_led_dev = led_create(auxio_led_func, sc, "auxioled");
274 /* turn on the LED */
275 auxio_led_func(sc, 1);
276 }
277
278 static int
279 auxio_bus_detach(device_t dev)
280 {
281 struct auxio_softc *sc;
282
283 sc = device_get_softc(dev);
284 led_destroy(sc->sc_led_dev);
285 auxio_led_func(sc, sc->sc_led_stat);
286 auxio_free_resource(sc);
287
288 return (0);
289 }
290
291 static void
292 auxio_free_resource(struct auxio_softc *sc)
293 {
294 int i, n;
295
296 n = sc->sc_nauxio;
297 for (i = 0; i < n; i++)
298 if (sc->sc_res[i])
299 bus_release_resource(sc->sc_dev,
300 (sc->sc_flags & AUXIO_SBUS) ? SYS_RES_MEMORY :
301 SYS_RES_IOPORT, sc->sc_rid[i], sc->sc_res[i]);
302 AUXIO_LOCK_DESTROY(sc);
303 }
304
305 static int
306 auxio_sbus_attach(device_t dev)
307 {
308 struct auxio_softc *sc;
309 struct resource *res;
310
311 sc = device_get_softc(dev);
312 bzero(sc, sizeof(*sc));
313 sc->sc_dev = dev;
314
315 AUXIO_LOCK_INIT(sc);
316 sc->sc_nauxio = 1;
317 sc->sc_flags = AUXIO_LEDONLY | AUXIO_SBUS;
318 sc->sc_rid[AUXIO_PCIO_LED] = 0;
319 res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
320 &sc->sc_rid[AUXIO_PCIO_LED], RF_ACTIVE);
321 if (res == NULL) {
322 device_printf(sc->sc_dev, "could not allocate resources\n");
323 goto attach_fail;
324 }
325 sc->sc_res[AUXIO_PCIO_LED] = res;
326 sc->sc_regt[AUXIO_PCIO_LED] = rman_get_bustag(res);
327 sc->sc_regh[AUXIO_PCIO_LED] = rman_get_bushandle(res);
328 auxio_attach_common(sc);
329 return (0);
330
331 attach_fail:
332 auxio_free_resource(sc);
333
334 return (ENXIO);
335 }
Cache object: d4552976da6d2425f34d34f5e7d164d0
|