1/*-
2 * Copyright (c) 2009 Marius Strobl <marius@FreeBSD.org>
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#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: head/sys/sparc64/ebus/epic.c 202006 2010-01-10 15:44:48Z marius $");
29
30#include <sys/param.h>
31#include <sys/systm.h>
32#include <sys/bus.h>
33#include <sys/kernel.h>
34#include <sys/lock.h>
35#include <sys/module.h>
36#include <sys/mutex.h>
37#include <sys/resource.h>
38#include <sys/rman.h>
39
40#include <dev/led/led.h>
41#include <dev/ofw/ofw_bus.h>
42
43#include <machine/bus.h>
44#include <machine/resource.h>
45
46#define EPIC_DELAY 10000
47
48#define EPIC_NREG 1
49#define EPIC_FW_LED 0
50
51#define EPIC_FW_LED_DATA 0x40
52#define EPIC_FW_LED_ADDR 0x41
53#define EPIC_FW_LED_WRITE_MASK 0x80
54
55#define EPIC_FW_VERSION 0x05
56#define EPIC_LED_STATE0 0x06
57
58#define EPIC_LED_ALERT_MASK 0x0c
59#define EPIC_LED_ALERT_OFF 0x00
60#define EPIC_LED_ALERT_ON 0x04
61
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
67
68static struct resource_spec epic_res_spec[] = {
69 { SYS_RES_MEMORY, EPIC_FW_LED, RF_ACTIVE },
70 { -1, 0 }
71};
72
73struct epic_softc {
74 struct mtx sc_mtx;
75 struct resource *sc_res[EPIC_NREG];
76 struct cdev *sc_led_dev_alert;
77 struct cdev *sc_led_dev_power;
78};
79
80#define EPIC_FW_LED_READ(sc, off) ({ \
81 uint8_t __val; \
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); \
85 DELAY(EPIC_DELAY); \
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); \
89 DELAY(EPIC_DELAY); \
90 __val; \
91})
92
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); \
97 DELAY(EPIC_DELAY); \
98 bus_write_1((sc)->sc_res[EPIC_FW_LED], EPIC_FW_LED_WRITE_MASK, \
99 (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); \
102 DELAY(EPIC_DELAY); \
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); \
106 DELAY(EPIC_DELAY); \
107} while (0)
108
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)
114
115static device_probe_t epic_probe;
116static device_attach_t epic_attach;
117static device_detach_t epic_detach;
118
119static void epic_led_alert(void *arg, int onoff);
120static void epic_led_power(void *arg, int onoff);
121
122static 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),
127
128 KOBJMETHOD_END
129};
130
131static devclass_t epic_devclass;
132
133DEFINE_CLASS_0(epic, epic_driver, epic_methods,
134 sizeof(struct epic_softc));
135DRIVER_MODULE(epic, ebus, epic_driver, epic_devclass, 0, 0);
136
137static int
138epic_probe(device_t dev)
139{
140 const char* compat;
141
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);
147 }
148 return (ENXIO);
149}
150
151static int
152epic_attach(device_t dev)
153{
154 struct epic_softc *sc;
155
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);
160 return (ENXIO);
161 }
162
163 EPIC_LOCK_INIT(sc);
164
165 if (bootverbose)
166 device_printf(dev, "version 0x%x\n",
167 EPIC_FW_LED_READ(sc, EPIC_FW_VERSION));
168
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");
171
172 return (0);
173}
174
175static int
176epic_detach(device_t dev)
177{
178 struct epic_softc *sc;
179
180 sc = device_get_softc(dev);
181
182 led_destroy(sc->sc_led_dev_alert);
183 led_destroy(sc->sc_led_dev_power);
184
185 bus_release_resources(dev, epic_res_spec, sc->sc_res);
186
187 EPIC_LOCK_DESTROY(sc);
188
189 return (0);
190}
191
192static void
193epic_led_alert(void *arg, int onoff)
194{
195 struct epic_softc *sc;
196
197 sc = (struct epic_softc *)arg;
198
199 EPIC_LOCK(sc);
200 EPIC_FW_LED_WRITE(sc, EPIC_LED_STATE0, EPIC_LED_ALERT_MASK,
201 onoff ? EPIC_LED_ALERT_ON : EPIC_LED_ALERT_OFF);
202 EPIC_UNLOCK(sc);
203}
204
205static void
206epic_led_power(void *arg, int onoff)
207{
208 struct epic_softc *sc;
209
210 sc = (struct epic_softc *)arg;
211
212 EPIC_LOCK(sc);
213 EPIC_FW_LED_WRITE(sc, EPIC_LED_STATE0, EPIC_LED_POWER_MASK,
214 onoff ? EPIC_LED_POWER_ON : EPIC_LED_POWER_OFF);
215 EPIC_UNLOCK(sc);
216}
2 * Copyright (c) 2009 Marius Strobl <marius@FreeBSD.org>
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#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: head/sys/sparc64/ebus/epic.c 202006 2010-01-10 15:44:48Z marius $");
29
30#include <sys/param.h>
31#include <sys/systm.h>
32#include <sys/bus.h>
33#include <sys/kernel.h>
34#include <sys/lock.h>
35#include <sys/module.h>
36#include <sys/mutex.h>
37#include <sys/resource.h>
38#include <sys/rman.h>
39
40#include <dev/led/led.h>
41#include <dev/ofw/ofw_bus.h>
42
43#include <machine/bus.h>
44#include <machine/resource.h>
45
46#define EPIC_DELAY 10000
47
48#define EPIC_NREG 1
49#define EPIC_FW_LED 0
50
51#define EPIC_FW_LED_DATA 0x40
52#define EPIC_FW_LED_ADDR 0x41
53#define EPIC_FW_LED_WRITE_MASK 0x80
54
55#define EPIC_FW_VERSION 0x05
56#define EPIC_LED_STATE0 0x06
57
58#define EPIC_LED_ALERT_MASK 0x0c
59#define EPIC_LED_ALERT_OFF 0x00
60#define EPIC_LED_ALERT_ON 0x04
61
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
67
68static struct resource_spec epic_res_spec[] = {
69 { SYS_RES_MEMORY, EPIC_FW_LED, RF_ACTIVE },
70 { -1, 0 }
71};
72
73struct epic_softc {
74 struct mtx sc_mtx;
75 struct resource *sc_res[EPIC_NREG];
76 struct cdev *sc_led_dev_alert;
77 struct cdev *sc_led_dev_power;
78};
79
80#define EPIC_FW_LED_READ(sc, off) ({ \
81 uint8_t __val; \
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); \
85 DELAY(EPIC_DELAY); \
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); \
89 DELAY(EPIC_DELAY); \
90 __val; \
91})
92
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); \
97 DELAY(EPIC_DELAY); \
98 bus_write_1((sc)->sc_res[EPIC_FW_LED], EPIC_FW_LED_WRITE_MASK, \
99 (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); \
102 DELAY(EPIC_DELAY); \
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); \
106 DELAY(EPIC_DELAY); \
107} while (0)
108
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)
114
115static device_probe_t epic_probe;
116static device_attach_t epic_attach;
117static device_detach_t epic_detach;
118
119static void epic_led_alert(void *arg, int onoff);
120static void epic_led_power(void *arg, int onoff);
121
122static 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),
127
128 KOBJMETHOD_END
129};
130
131static devclass_t epic_devclass;
132
133DEFINE_CLASS_0(epic, epic_driver, epic_methods,
134 sizeof(struct epic_softc));
135DRIVER_MODULE(epic, ebus, epic_driver, epic_devclass, 0, 0);
136
137static int
138epic_probe(device_t dev)
139{
140 const char* compat;
141
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);
147 }
148 return (ENXIO);
149}
150
151static int
152epic_attach(device_t dev)
153{
154 struct epic_softc *sc;
155
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);
160 return (ENXIO);
161 }
162
163 EPIC_LOCK_INIT(sc);
164
165 if (bootverbose)
166 device_printf(dev, "version 0x%x\n",
167 EPIC_FW_LED_READ(sc, EPIC_FW_VERSION));
168
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");
171
172 return (0);
173}
174
175static int
176epic_detach(device_t dev)
177{
178 struct epic_softc *sc;
179
180 sc = device_get_softc(dev);
181
182 led_destroy(sc->sc_led_dev_alert);
183 led_destroy(sc->sc_led_dev_power);
184
185 bus_release_resources(dev, epic_res_spec, sc->sc_res);
186
187 EPIC_LOCK_DESTROY(sc);
188
189 return (0);
190}
191
192static void
193epic_led_alert(void *arg, int onoff)
194{
195 struct epic_softc *sc;
196
197 sc = (struct epic_softc *)arg;
198
199 EPIC_LOCK(sc);
200 EPIC_FW_LED_WRITE(sc, EPIC_LED_STATE0, EPIC_LED_ALERT_MASK,
201 onoff ? EPIC_LED_ALERT_ON : EPIC_LED_ALERT_OFF);
202 EPIC_UNLOCK(sc);
203}
204
205static void
206epic_led_power(void *arg, int onoff)
207{
208 struct epic_softc *sc;
209
210 sc = (struct epic_softc *)arg;
211
212 EPIC_LOCK(sc);
213 EPIC_FW_LED_WRITE(sc, EPIC_LED_STATE0, EPIC_LED_POWER_MASK,
214 onoff ? EPIC_LED_POWER_ON : EPIC_LED_POWER_OFF);
215 EPIC_UNLOCK(sc);
216}