]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/sparc64/ebus/epic.c
Import CK as of commit b19ed4c6a56ec93215ab567ba18ba61bf1cfbac8
[FreeBSD/FreeBSD.git] / sys / sparc64 / ebus / epic.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2009 Marius Strobl <marius@FreeBSD.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
15  *
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
26  * SUCH DAMAGE.
27  */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/bus.h>
35 #include <sys/kernel.h>
36 #include <sys/lock.h>
37 #include <sys/module.h>
38 #include <sys/mutex.h>
39 #include <sys/resource.h>
40 #include <sys/rman.h>
41
42 #include <dev/led/led.h>
43 #include <dev/ofw/ofw_bus.h>
44
45 #include <machine/bus.h>
46 #include <machine/resource.h>
47
48 #define EPIC_DELAY                      10000
49
50 #define EPIC_NREG                       1
51 #define EPIC_FW_LED                     0
52
53 #define EPIC_FW_LED_DATA                0x40
54 #define EPIC_FW_LED_ADDR                0x41
55 #define EPIC_FW_LED_WRITE_MASK          0x80
56
57 #define EPIC_FW_VERSION                 0x05
58 #define EPIC_LED_STATE0                 0x06
59
60 #define EPIC_LED_ALERT_MASK             0x0c
61 #define EPIC_LED_ALERT_OFF              0x00
62 #define EPIC_LED_ALERT_ON               0x04
63
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
69
70 static struct resource_spec epic_res_spec[] = {
71         { SYS_RES_MEMORY, EPIC_FW_LED, RF_ACTIVE },
72         { -1, 0 }
73 };
74
75 struct epic_softc {
76         struct mtx              sc_mtx;
77         struct resource         *sc_res[EPIC_NREG];
78         struct cdev             *sc_led_dev_alert;
79         struct cdev             *sc_led_dev_power;
80 };
81
82 #define EPIC_FW_LED_READ(sc, off) ({                                    \
83         uint8_t __val;                                                  \
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);          \
87         DELAY(EPIC_DELAY);                                              \
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);                                    \
91         DELAY(EPIC_DELAY);                                              \
92         __val;                                                          \
93 })
94
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);          \
99         DELAY(EPIC_DELAY);                                              \
100         bus_write_1((sc)->sc_res[EPIC_FW_LED], EPIC_FW_LED_WRITE_MASK,  \
101             (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);       \
104         DELAY(EPIC_DELAY);                                              \
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);          \
108         DELAY(EPIC_DELAY);                                              \
109 } while (0)
110
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)
116
117 static device_probe_t epic_probe;
118 static device_attach_t epic_attach;
119 static device_detach_t epic_detach;
120
121 static void epic_led_alert(void *arg, int onoff);
122 static void epic_led_power(void *arg, int onoff);
123
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),
129
130         DEVMETHOD_END
131 };
132
133 static devclass_t epic_devclass;
134
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);
138
139 static int
140 epic_probe(device_t dev)
141 {
142         const char* compat;
143
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);
149         }
150         return (ENXIO);
151 }
152
153 static int
154 epic_attach(device_t dev)
155 {
156         struct epic_softc *sc;
157
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);
162                 return (ENXIO);
163         }
164
165         EPIC_LOCK_INIT(sc);
166
167         if (bootverbose)
168                 device_printf(dev, "version 0x%x\n",
169                     EPIC_FW_LED_READ(sc, EPIC_FW_VERSION));
170
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");
173
174         return (0);
175 }
176
177 static int
178 epic_detach(device_t dev)
179 {
180         struct epic_softc *sc;
181
182         sc = device_get_softc(dev);
183
184         led_destroy(sc->sc_led_dev_alert);
185         led_destroy(sc->sc_led_dev_power);
186
187         bus_release_resources(dev, epic_res_spec, sc->sc_res);
188
189         EPIC_LOCK_DESTROY(sc);
190
191         return (0);
192 }
193
194 static void
195 epic_led_alert(void *arg, int onoff)
196 {
197         struct epic_softc *sc;
198
199         sc = (struct epic_softc *)arg;
200
201         EPIC_LOCK(sc);
202         EPIC_FW_LED_WRITE(sc, EPIC_LED_STATE0, EPIC_LED_ALERT_MASK,
203             onoff ? EPIC_LED_ALERT_ON : EPIC_LED_ALERT_OFF);
204         EPIC_UNLOCK(sc);
205 }
206
207 static void
208 epic_led_power(void *arg, int onoff)
209 {
210         struct epic_softc *sc;
211
212         sc = (struct epic_softc *)arg;
213
214         EPIC_LOCK(sc);
215         EPIC_FW_LED_WRITE(sc, EPIC_LED_STATE0, EPIC_LED_POWER_MASK,
216             onoff ? EPIC_LED_POWER_ON : EPIC_LED_POWER_OFF);
217         EPIC_UNLOCK(sc);
218 }