lightbar.c revision 1.1 1 /* $NetBSD: lightbar.c,v 1.1 2025/09/08 08:08:15 macallan Exp $ */
2
3 /*
4 * Copyright (c) 2025 Michael Lorenz
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 ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: lightbar.c,v 1.1 2025/09/08 08:08:15 macallan Exp $");
31
32 #include <sys/param.h>
33 #include <sys/device.h>
34 #include <sys/systm.h>
35 #include <sys/kmem.h>
36 #include <sys/kthread.h>
37 #include <sys/cpu.h>
38 #include <uvm/uvm_extern.h>
39
40 #include <dev/ofw/openfirm.h>
41 #include <machine/autoconf.h>
42 #include <machine/pio.h>
43 #include <macppc/dev/dbdma.h>
44 #include <macppc/dev/obiovar.h>
45 #include <macppc/dev/i2sreg.h>
46
47 #ifdef LIGHTBAR_DEBUG
48 # define DPRINTF printf
49 #else
50 # define DPRINTF while (0) printf
51 #endif
52
53 struct lightbar_softc {
54 device_t sc_dev;
55 int sc_node;
56 bus_space_tag_t sc_tag;
57 bus_space_handle_t sc_bsh;
58 bus_space_handle_t sc_odmah;
59 dbdma_regmap_t *sc_odma;
60 struct dbdma_command *sc_odmacmd;
61 uint32_t sc_baseaddr;
62 uint32_t *sc_dmabuf;
63 lwp_t *sc_thread;
64 struct cpu_info *sc_cpu[2];
65 };
66
67 static int lightbar_match(device_t, struct cfdata *, void *);
68 static void lightbar_attach(device_t, device_t, void *);
69 static void lightbar_thread(void *);
70
71 CFATTACH_DECL_NEW(lightbar, sizeof(struct lightbar_softc), lightbar_match,
72 lightbar_attach, NULL, NULL);
73
74 /*
75 * upper 16 bit are LEDs from the top right to the bottom left
76 * however, the hardware has them rotated so the uppper left bit is in 1
77 */
78 #define LEDMASK(x) ((x << 1) | (((x) & 0x80000000) >> 31))
79
80 static int
81 lightbar_match(device_t parent, struct cfdata *match, void *aux)
82 {
83 struct confargs *ca;
84 int soundbus, soundchip;
85 char buf[32];
86
87 ca = aux;
88 if (strcmp(ca->ca_name, "i2s") != 0)
89 return 0;
90
91 if ((soundbus = OF_child(ca->ca_node)) == 0 ||
92 (soundchip = OF_child(soundbus)) == 0)
93 return 0;
94
95 if (OF_getprop(soundchip, "virtual", buf, 32) == 0)
96 return 200; /* beat out snapper */
97
98 return 0;
99 }
100
101 static void
102 lightbar_attach(device_t parent, device_t self, void *aux)
103 {
104 struct lightbar_softc *sc;
105 struct confargs *ca;
106 struct dbdma_command *cmd;
107 uint32_t reg[6], x;
108 int i, timo;
109
110 sc = device_private(self);
111 sc->sc_dev = self;
112
113 ca = aux;
114 sc->sc_node = ca->ca_node;
115 sc->sc_tag = ca->ca_tag;
116
117 sc->sc_odmacmd = dbdma_alloc(4 * sizeof(struct dbdma_command), NULL);
118
119 sc->sc_baseaddr = ca->ca_baseaddr;
120
121 OF_getprop(sc->sc_node, "reg", reg, sizeof(reg));
122 reg[0] += ca->ca_baseaddr;
123 reg[2] += ca->ca_baseaddr;
124
125 bus_space_map(sc->sc_tag, reg[0], reg[1], 0, &sc->sc_bsh);
126 obio_space_map(reg[2], reg[3], &sc->sc_odmah);
127 sc->sc_odma = bus_space_vaddr(sc->sc_tag, sc->sc_odmah);
128 DPRINTF("reg %08x odma %08x\n", (uint32_t)sc->sc_bsh, (uint32_t)sc->sc_odmah);
129
130 aprint_normal("\n");
131
132 /* PMF event handler */
133 pmf_device_register(sc->sc_dev, NULL, NULL);
134
135 /* enable i2s goop */
136 x = obio_read_4(KEYLARGO_FCR1);
137 x |= I2S0CLKEN | I2S0EN;
138 obio_write_4(KEYLARGO_FCR1, x);
139
140 /* Clear CLKSTOPPEND. */
141 bus_space_write_4(sc->sc_tag, sc->sc_bsh, I2S_INT, I2S_INT_CLKSTOPPEND);
142
143 x = obio_read_4(KEYLARGO_FCR1); /* FCR */
144 x &= ~I2S0CLKEN; /* XXX I2S0 */
145 obio_write_4(KEYLARGO_FCR1, x);
146
147 /* Wait until clock is stopped. */
148 for (timo = 1000; timo > 0; timo--) {
149 if (bus_space_read_4(sc->sc_tag, sc->sc_bsh, I2S_INT) &
150 I2S_INT_CLKSTOPPEND)
151 goto done;
152 delay(1);
153 }
154 DPRINTF("timeout\n");
155 done:
156 bus_space_write_4(sc->sc_tag, sc->sc_bsh, I2S_FORMAT, 0x01fa0000);
157
158 x = obio_read_4(KEYLARGO_FCR1);
159 x |= I2S0CLKEN;
160 obio_write_4(KEYLARGO_FCR1, x);
161
162 sc->sc_dmabuf = kmem_alloc(4096, KM_SLEEP);
163
164 /* initial pattern, just to say hi */
165 for (i = 0; i < 32; i++) {
166 sc->sc_dmabuf[i] = LEDMASK(0xaa550000);
167 }
168
169 /*
170 * We use a single DMA buffer, with just 8 32bit words which the DBDMA
171 * engine loops over. That way we can:
172 * - get away without using interrupts, just scribble a new pattern into
173 * the buffer
174 * - play PWM tricks with the LEDs, giving us 8 levels of brightness
175 */
176 cmd = sc->sc_odmacmd;
177 DBDMA_BUILD(cmd, DBDMA_CMD_OUT_MORE, 0, 32, vtophys((vaddr_t)sc->sc_dmabuf),
178 0, DBDMA_WAIT_NEVER, DBDMA_BRANCH_NEVER);
179 cmd++;
180 DBDMA_BUILD(cmd, DBDMA_CMD_NOP, 0, 0,
181 0/*vtophys((vaddr_t)sc->sc_odmacmd)*/, 0, DBDMA_WAIT_NEVER,
182 DBDMA_BRANCH_ALWAYS);
183
184 out32rb(&cmd->d_cmddep, vtophys((vaddr_t)sc->sc_odmacmd));
185
186 dbdma_start(sc->sc_odma, sc->sc_odmacmd);
187
188 sc->sc_cpu[0] = cpu_lookup(0);
189 sc->sc_cpu[1] = cpu_lookup(1);
190 aprint_normal_dev(sc->sc_dev, "monitoring %s\n",
191 sc->sc_cpu[1] == NULL ? "one CPU" : "two CPUs");
192
193 if (kthread_create(PRI_NONE, 0, NULL, lightbar_thread, sc,
194 &sc->sc_thread, "%s", "lightbar") != 0) {
195 aprint_error_dev(self, "unable to create kthread\n");
196 }
197
198 }
199
200 /*
201 * this draws a bar into out[0..7], system time as dim, other CPU time as
202 * bright, idle as off
203 * prev[CPUSTATES] stores old counter values, old[2] old bar lengths
204 */
205 static int
206 lightbar_update(uint64_t *cp_time, uint64_t *prev, int *old, uint32_t *out)
207 {
208 uint64_t total = 0;
209 int all, sys, idle, syst, i;
210
211 for (i = 0; i < CPUSTATES; i++)
212 total += cp_time[i] - prev[i];
213 idle = (int)(cp_time[CP_IDLE] - prev[CP_IDLE]);
214 syst = (int)(cp_time[CP_SYS] - prev[CP_SYS]);
215 all = (total - idle) * 8 / total;
216 sys = syst * 8 / total;
217 for (i = 0; i < CPUSTATES; i++)
218 prev[i] = cp_time[i];
219 if ((all != old[0]) || (sys != old[1])) {
220 for (i = 0; i < sys; i++) out[i] = 2;
221 for (; i < all; i++) out[i] = 8;
222 for (; i < 8; i++) out[i] = 0;
223 old[0] = all;
224 old[1] = sys;
225 return 1;
226 }
227 return 0;
228 }
229
230 static void
231 lightbar_thread(void *cookie)
232 {
233 struct lightbar_softc *sc = cookie;
234 uint32_t latch;
235 uint64_t prev[2 * CPUSTATES];
236 int i, j, old[4] = {0, 0, 0, 0}, intensity[16];
237
238 for (i = 0; i < 2 * CPUSTATES; i++)
239 prev[i] = 0;
240
241 tsleep(cookie, PRI_NONE, "lights", hz);
242
243 while (1) {
244 int update;
245 /* draw CPU0's usage into the upper bar */
246 update = lightbar_update(sc->sc_cpu[0]->ci_schedstate.spc_cp_time,
247 prev, old, &intensity[8]);
248 if (sc->sc_cpu[1] != NULL) {
249 /*
250 * if we have a 2nd CPU draw its usage into the lower
251 * bar
252 */
253 update |= lightbar_update(
254 sc->sc_cpu[1]->ci_schedstate.spc_cp_time,
255 &prev[CPUSTATES], &old[2], intensity);
256 } else {
257 /*
258 * if we don't have a 2nd CPU just duplicate the bar
259 * from the first
260 */
261 for (i = 0; i < 8; i++)
262 intensity[i] = intensity[i + 8];
263 }
264 if (update) {
265 /*
266 * this turns our intensity map into a bit pattern for
267 * the hardware - we have 8 samples in our buffer, for
268 * each LED we set the corresponding bit in intensity[j]
269 * samples
270 */
271 for (i = 0; i < 8; i++) {
272 latch = 0;
273 for (j = 0; j < 16; j++) {
274 if (intensity[j] > i)
275 latch |= 1 << (j + 16);
276 }
277 sc->sc_dmabuf[i] = LEDMASK(latch);
278 }
279 }
280
281 tsleep(cookie, PRI_NONE, "lights", hz / 5);
282 }
283 kthread_exit(0);
284 }
285