2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2009 Marius Strobl <marius@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
35 #include <sys/kernel.h>
37 #include <sys/module.h>
38 #include <sys/mutex.h>
39 #include <sys/resource.h>
42 #include <dev/led/led.h>
43 #include <dev/ofw/ofw_bus.h>
45 #include <machine/bus.h>
46 #include <machine/resource.h>
48 #define EPIC_DELAY 10000
53 #define EPIC_FW_LED_DATA 0x40
54 #define EPIC_FW_LED_ADDR 0x41
55 #define EPIC_FW_LED_WRITE_MASK 0x80
57 #define EPIC_FW_VERSION 0x05
58 #define EPIC_LED_STATE0 0x06
60 #define EPIC_LED_ALERT_MASK 0x0c
61 #define EPIC_LED_ALERT_OFF 0x00
62 #define EPIC_LED_ALERT_ON 0x04
64 #define EPIC_LED_POWER_MASK 0x30
65 #define EPIC_LED_POWER_OFF 0x00
66 #define EPIC_LED_POWER_ON 0x10
67 #define EPIC_LED_POWER_SB_BLINK 0x20
68 #define EPIC_LED_POWER_FAST_BLINK 0x30
70 static struct resource_spec epic_res_spec[] = {
71 { SYS_RES_MEMORY, EPIC_FW_LED, RF_ACTIVE },
77 struct resource *sc_res[EPIC_NREG];
78 struct cdev *sc_led_dev_alert;
79 struct cdev *sc_led_dev_power;
82 #define EPIC_FW_LED_READ(sc, off) ({ \
84 bus_write_1((sc)->sc_res[EPIC_FW_LED], EPIC_FW_LED_ADDR, (off));\
85 bus_barrier((sc)->sc_res[EPIC_FW_LED], EPIC_FW_LED_ADDR, 1, \
86 BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE); \
88 __val = bus_read_1((sc)->sc_res[EPIC_FW_LED], EPIC_FW_LED_DATA);\
89 bus_barrier((sc)->sc_res[EPIC_FW_LED], EPIC_FW_LED_DATA, 1, \
90 BUS_SPACE_BARRIER_READ); \
95 #define EPIC_FW_LED_WRITE(sc, off, mask, val) do { \
96 bus_write_1((sc)->sc_res[EPIC_FW_LED], EPIC_FW_LED_ADDR, (off));\
97 bus_barrier((sc)->sc_res[EPIC_FW_LED], EPIC_FW_LED_ADDR, 1, \
98 BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE); \
100 bus_write_1((sc)->sc_res[EPIC_FW_LED], EPIC_FW_LED_WRITE_MASK, \
102 bus_barrier((sc)->sc_res[EPIC_FW_LED], EPIC_FW_LED_WRITE_MASK, \
103 1, BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE); \
105 bus_write_1((sc)->sc_res[EPIC_FW_LED], EPIC_FW_LED_DATA, (val));\
106 bus_barrier((sc)->sc_res[EPIC_FW_LED], EPIC_FW_LED_DATA, 1, \
107 BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE); \
111 #define EPIC_LOCK_INIT(sc) \
112 mtx_init(&(sc)->sc_mtx, "epic mtx", NULL, MTX_DEF)
113 #define EPIC_LOCK_DESTROY(sc) mtx_destroy(&(sc)->sc_mtx)
114 #define EPIC_LOCK(sc) mtx_lock(&(sc)->sc_mtx)
115 #define EPIC_UNLOCK(sc) mtx_unlock(&(sc)->sc_mtx)
117 static device_probe_t epic_probe;
118 static device_attach_t epic_attach;
119 static device_detach_t epic_detach;
121 static void epic_led_alert(void *arg, int onoff);
122 static void epic_led_power(void *arg, int onoff);
124 static device_method_t epic_methods[] = {
125 /* Device interface */
126 DEVMETHOD(device_probe, epic_probe),
127 DEVMETHOD(device_attach, epic_attach),
128 DEVMETHOD(device_detach, epic_detach),
133 static devclass_t epic_devclass;
135 DEFINE_CLASS_0(epic, epic_driver, epic_methods,
136 sizeof(struct epic_softc));
137 DRIVER_MODULE(epic, ebus, epic_driver, epic_devclass, 0, 0);
140 epic_probe(device_t dev)
144 compat = ofw_bus_get_compat(dev);
145 if (compat != NULL && strcmp(ofw_bus_get_name(dev),
146 "env-monitor") == 0 && strcmp(compat, "epic") == 0) {
147 device_set_desc(dev, "Sun Fire V215/V245 LEDs");
148 return (BUS_PROBE_DEFAULT);
154 epic_attach(device_t dev)
156 struct epic_softc *sc;
158 sc = device_get_softc(dev);
159 if (bus_alloc_resources(dev, epic_res_spec, sc->sc_res)) {
160 device_printf(dev, "failed to allocate resources\n");
161 bus_release_resources(dev, epic_res_spec, sc->sc_res);
168 device_printf(dev, "version 0x%x\n",
169 EPIC_FW_LED_READ(sc, EPIC_FW_VERSION));
171 sc->sc_led_dev_alert = led_create(epic_led_alert, sc, "alert");
172 sc->sc_led_dev_power = led_create(epic_led_power, sc, "power");
178 epic_detach(device_t dev)
180 struct epic_softc *sc;
182 sc = device_get_softc(dev);
184 led_destroy(sc->sc_led_dev_alert);
185 led_destroy(sc->sc_led_dev_power);
187 bus_release_resources(dev, epic_res_spec, sc->sc_res);
189 EPIC_LOCK_DESTROY(sc);
195 epic_led_alert(void *arg, int onoff)
197 struct epic_softc *sc;
199 sc = (struct epic_softc *)arg;
202 EPIC_FW_LED_WRITE(sc, EPIC_LED_STATE0, EPIC_LED_ALERT_MASK,
203 onoff ? EPIC_LED_ALERT_ON : EPIC_LED_ALERT_OFF);
208 epic_led_power(void *arg, int onoff)
210 struct epic_softc *sc;
212 sc = (struct epic_softc *)arg;
215 EPIC_FW_LED_WRITE(sc, EPIC_LED_STATE0, EPIC_LED_POWER_MASK,
216 onoff ? EPIC_LED_POWER_ON : EPIC_LED_POWER_OFF);