2 * Copyright (c) 2004 Pyun YongHyeon
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
28 /* $NetBSD: auxio.c,v 1.11 2003/07/15 03:36:04 lukem Exp $ */
31 * Copyright (c) 2000, 2001 Matthew R. Green
32 * All rights reserved.
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions
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.
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
59 * AUXIO registers support on the SBus & EBus2, used for the floppy driver
60 * and to control the system LED, for the BLINK option.
63 #include <sys/cdefs.h>
64 __FBSDID("$FreeBSD$");
66 #include <sys/param.h>
67 #include <sys/systm.h>
69 #include <sys/kernel.h>
71 #include <sys/module.h>
72 #include <sys/mutex.h>
73 #include <sys/resource.h>
76 #include <dev/led/led.h>
77 #include <dev/ofw/ofw_bus.h>
78 #include <dev/ofw/openfirm.h>
80 #include <machine/bus.h>
81 #include <machine/ofw_machdep.h>
82 #include <machine/resource.h>
84 #include <sparc64/sbus/sbusvar.h>
85 #include <dev/auxio/auxioreg.h>
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.
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_NREG 5
101 struct device *sc_dev;
104 struct resource *sc_res[AUXIO_PCIO_NREG];
105 int sc_rid[AUXIO_PCIO_NREG];
106 bus_space_tag_t sc_regt[AUXIO_PCIO_NREG];
107 bus_space_handle_t sc_regh[AUXIO_PCIO_NREG];
108 struct cdev *sc_led_dev;
109 u_int32_t sc_led_stat;
112 #define AUXIO_LEDONLY 0x1
113 #define AUXIO_EBUS 0x2
114 #define AUXIO_SBUS 0x4
119 static void auxio_led_func(void *arg, int onoff);
120 static int 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);
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),
137 static driver_t auxio_sbus_driver = {
140 sizeof(struct auxio_softc)
143 static devclass_t auxio_devclass;
144 /* The probe order is handled by sbus(4). */
145 EARLY_DRIVER_MODULE(auxio, sbus, auxio_sbus_driver, auxio_devclass, 0, 0,
147 MODULE_DEPEND(auxio, sbus, 1, 1, 1);
150 static device_method_t auxio_ebus_methods[] = {
151 DEVMETHOD(device_probe, auxio_bus_probe),
152 DEVMETHOD(device_attach, auxio_ebus_attach),
153 DEVMETHOD(device_detach, auxio_bus_detach),
157 static driver_t auxio_ebus_driver = {
160 sizeof(struct auxio_softc)
163 EARLY_DRIVER_MODULE(auxio, ebus, auxio_ebus_driver, auxio_devclass, 0, 0,
165 MODULE_DEPEND(auxio, ebus, 1, 1, 1);
166 MODULE_VERSION(auxio, 1);
168 #define AUXIO_LOCK_INIT(sc) \
169 mtx_init(&sc->sc_lock, "auxio mtx", NULL, MTX_DEF)
170 #define AUXIO_LOCK(sc) mtx_lock(&sc->sc_lock)
171 #define AUXIO_UNLOCK(sc) mtx_unlock(&sc->sc_lock)
172 #define AUXIO_LOCK_DESTROY(sc) mtx_destroy(&sc->sc_lock)
175 auxio_led_write(struct auxio_softc *sc, u_int32_t v)
177 if (sc->sc_flags & AUXIO_EBUS)
178 bus_space_write_4(sc->sc_regt[AUXIO_PCIO_LED],
179 sc->sc_regh[AUXIO_PCIO_LED], 0, v);
181 bus_space_write_1(sc->sc_regt[AUXIO_PCIO_LED],
182 sc->sc_regh[AUXIO_PCIO_LED], 0, v);
185 static __inline u_int32_t
186 auxio_led_read(struct auxio_softc *sc)
190 if (sc->sc_flags & AUXIO_EBUS)
191 led = bus_space_read_4(sc->sc_regt[AUXIO_PCIO_LED],
192 sc->sc_regh[AUXIO_PCIO_LED], 0);
194 led = bus_space_read_1(sc->sc_regt[AUXIO_PCIO_LED],
195 sc->sc_regh[AUXIO_PCIO_LED], 0);
201 auxio_led_func(void *arg, int onoff)
203 struct auxio_softc *sc;
206 sc = (struct auxio_softc *)arg;
210 * NB: We must not touch the other bits of the SBus AUXIO reg.
212 led = auxio_led_read(sc);
214 led |= AUXIO_LED_LED;
216 led &= ~AUXIO_LED_LED;
217 auxio_led_write(sc, led);
222 auxio_bus_probe(device_t dev)
226 name = ofw_bus_get_name(dev);
227 if (strcmp("auxio", name) == 0) {
228 device_set_desc(dev, "Sun Auxiliary I/O");
236 auxio_ebus_attach(device_t dev)
238 struct auxio_softc *sc;
240 sc = device_get_softc(dev);
244 sc->sc_nauxio = AUXIO_PCIO_NREG;
245 sc->sc_flags = AUXIO_LEDONLY | AUXIO_EBUS;
247 return(auxio_attach_common(sc));
251 auxio_attach_common(struct auxio_softc *sc)
253 struct resource *res;
256 for (i = 0; i < sc->sc_nauxio; i++) {
258 res = bus_alloc_resource_any(sc->sc_dev, SYS_RES_MEMORY,
259 &sc->sc_rid[i], RF_ACTIVE);
261 device_printf(sc->sc_dev,
262 "could not allocate resources\n");
266 sc->sc_regt[i] = rman_get_bustag(res);
267 sc->sc_regh[i] = rman_get_bushandle(res);
270 sc->sc_led_stat = auxio_led_read(sc) & AUXIO_LED_LED;
271 sc->sc_led_dev = led_create(auxio_led_func, sc, "auxioled");
272 /* turn on the LED */
273 auxio_led_func(sc, 1);
278 auxio_free_resource(sc);
284 auxio_bus_detach(device_t dev)
286 struct auxio_softc *sc;
288 sc = device_get_softc(dev);
289 led_destroy(sc->sc_led_dev);
290 auxio_led_func(sc, sc->sc_led_stat);
291 auxio_free_resource(sc);
297 auxio_free_resource(struct auxio_softc *sc)
301 for (i = 0; i < sc->sc_nauxio; i++)
303 bus_release_resource(sc->sc_dev, SYS_RES_MEMORY,
304 sc->sc_rid[i], sc->sc_res[i]);
305 AUXIO_LOCK_DESTROY(sc);
309 auxio_sbus_attach(device_t dev)
311 struct auxio_softc *sc;
313 sc = device_get_softc(dev);
318 sc->sc_flags = AUXIO_LEDONLY | AUXIO_SBUS;
320 return (auxio_attach_common(sc));