2 * Copyright (c) 2009 Marius Strobl <marius@FreeBSD.org>
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/systm.h>
33 #include <sys/kernel.h>
35 #include <sys/module.h>
36 #include <sys/mutex.h>
37 #include <sys/resource.h>
40 #include <dev/led/led.h>
41 #include <dev/ofw/ofw_bus.h>
43 #include <machine/bus.h>
44 #include <machine/resource.h>
46 #define EPIC_DELAY 10000
51 #define EPIC_FW_LED_DATA 0x40
52 #define EPIC_FW_LED_ADDR 0x41
53 #define EPIC_FW_LED_WRITE_MASK 0x80
55 #define EPIC_FW_VERSION 0x05
56 #define EPIC_LED_STATE0 0x06
58 #define EPIC_LED_ALERT_MASK 0x0c
59 #define EPIC_LED_ALERT_OFF 0x00
60 #define EPIC_LED_ALERT_ON 0x04
62 #define EPIC_LED_POWER_MASK 0x30
63 #define EPIC_LED_POWER_OFF 0x00
64 #define EPIC_LED_POWER_ON 0x10
65 #define EPIC_LED_POWER_SB_BLINK 0x20
66 #define EPIC_LED_POWER_FAST_BLINK 0x30
68 static struct resource_spec epic_res_spec[] = {
69 { SYS_RES_MEMORY, EPIC_FW_LED, RF_ACTIVE },
75 struct resource *sc_res[EPIC_NREG];
76 struct cdev *sc_led_dev_alert;
77 struct cdev *sc_led_dev_power;
80 #define EPIC_FW_LED_READ(sc, off) ({ \
82 bus_write_1((sc)->sc_res[EPIC_FW_LED], EPIC_FW_LED_ADDR, (off));\
83 bus_barrier((sc)->sc_res[EPIC_FW_LED], EPIC_FW_LED_ADDR, 1, \
84 BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE); \
86 __val = bus_read_1((sc)->sc_res[EPIC_FW_LED], EPIC_FW_LED_DATA);\
87 bus_barrier((sc)->sc_res[EPIC_FW_LED], EPIC_FW_LED_DATA, 1, \
88 BUS_SPACE_BARRIER_READ); \
93 #define EPIC_FW_LED_WRITE(sc, off, mask, val) do { \
94 bus_write_1((sc)->sc_res[EPIC_FW_LED], EPIC_FW_LED_ADDR, (off));\
95 bus_barrier((sc)->sc_res[EPIC_FW_LED], EPIC_FW_LED_ADDR, 1, \
96 BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE); \
98 bus_write_1((sc)->sc_res[EPIC_FW_LED], EPIC_FW_LED_WRITE_MASK, \
100 bus_barrier((sc)->sc_res[EPIC_FW_LED], EPIC_FW_LED_WRITE_MASK, \
101 1, BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE); \
103 bus_write_1((sc)->sc_res[EPIC_FW_LED], EPIC_FW_LED_DATA, (val));\
104 bus_barrier((sc)->sc_res[EPIC_FW_LED], EPIC_FW_LED_DATA, 1, \
105 BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE); \
109 #define EPIC_LOCK_INIT(sc) \
110 mtx_init(&(sc)->sc_mtx, "epic mtx", NULL, MTX_DEF)
111 #define EPIC_LOCK_DESTROY(sc) mtx_destroy(&(sc)->sc_mtx)
112 #define EPIC_LOCK(sc) mtx_lock(&(sc)->sc_mtx)
113 #define EPIC_UNLOCK(sc) mtx_unlock(&(sc)->sc_mtx)
115 static device_probe_t epic_probe;
116 static device_attach_t epic_attach;
117 static device_detach_t epic_detach;
119 static void epic_led_alert(void *arg, int onoff);
120 static void epic_led_power(void *arg, int onoff);
122 static device_method_t epic_methods[] = {
123 /* Device interface */
124 DEVMETHOD(device_probe, epic_probe),
125 DEVMETHOD(device_attach, epic_attach),
126 DEVMETHOD(device_detach, epic_detach),
131 static devclass_t epic_devclass;
133 DEFINE_CLASS_0(epic, epic_driver, epic_methods,
134 sizeof(struct epic_softc));
135 DRIVER_MODULE(epic, ebus, epic_driver, epic_devclass, 0, 0);
138 epic_probe(device_t dev)
142 compat = ofw_bus_get_compat(dev);
143 if (compat != NULL && strcmp(ofw_bus_get_name(dev),
144 "env-monitor") == 0 && strcmp(compat, "epic") == 0) {
145 device_set_desc(dev, "Sun Fire V215/V245 LEDs");
146 return (BUS_PROBE_DEFAULT);
152 epic_attach(device_t dev)
154 struct epic_softc *sc;
156 sc = device_get_softc(dev);
157 if (bus_alloc_resources(dev, epic_res_spec, sc->sc_res)) {
158 device_printf(dev, "failed to allocate resources\n");
159 bus_release_resources(dev, epic_res_spec, sc->sc_res);
166 device_printf(dev, "version 0x%x\n",
167 EPIC_FW_LED_READ(sc, EPIC_FW_VERSION));
169 sc->sc_led_dev_alert = led_create(epic_led_alert, sc, "alert");
170 sc->sc_led_dev_power = led_create(epic_led_power, sc, "power");
176 epic_detach(device_t dev)
178 struct epic_softc *sc;
180 sc = device_get_softc(dev);
182 led_destroy(sc->sc_led_dev_alert);
183 led_destroy(sc->sc_led_dev_power);
185 bus_release_resources(dev, epic_res_spec, sc->sc_res);
187 EPIC_LOCK_DESTROY(sc);
193 epic_led_alert(void *arg, int onoff)
195 struct epic_softc *sc;
197 sc = (struct epic_softc *)arg;
200 EPIC_FW_LED_WRITE(sc, EPIC_LED_STATE0, EPIC_LED_ALERT_MASK,
201 onoff ? EPIC_LED_ALERT_ON : EPIC_LED_ALERT_OFF);
206 epic_led_power(void *arg, int onoff)
208 struct epic_softc *sc;
210 sc = (struct epic_softc *)arg;
213 EPIC_FW_LED_WRITE(sc, EPIC_LED_STATE0, EPIC_LED_POWER_MASK,
214 onoff ? EPIC_LED_POWER_ON : EPIC_LED_POWER_OFF);