1 /* $NetBSD: lightbar.c,v 1.5 2026/04/08 06:51:24 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.5 2026/04/08 06:51:24 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 <sys/sysctl.h> 39 #include <uvm/uvm_extern.h> 40 41 #include <dev/ofw/openfirm.h> 42 #include <machine/autoconf.h> 43 #include <machine/pio.h> 44 #include <macppc/dev/dbdma.h> 45 #include <macppc/dev/obiovar.h> 46 #include <macppc/dev/i2sreg.h> 47 48 #ifdef LIGHTBAR_DEBUG 49 # define DPRINTF printf 50 #else 51 # define DPRINTF while (0) printf 52 #endif 53 54 struct lightbar_softc { 55 device_t sc_dev; 56 int sc_node; 57 bus_space_tag_t sc_tag; 58 bus_space_handle_t sc_bsh; 59 bus_space_handle_t sc_odmah; 60 dbdma_regmap_t *sc_odma; 61 struct dbdma_command *sc_odmacmd; 62 uint32_t sc_baseaddr; 63 uint32_t *sc_dmabuf; 64 lwp_t *sc_thread; 65 int sc_sys, sc_user; 66 struct cpu_info *sc_cpu[2]; 67 struct sysctlnode *sc_sysctl_me; 68 }; 69 70 static int lightbar_match(device_t, struct cfdata *, void *); 71 static void lightbar_attach(device_t, device_t, void *); 72 static void lightbar_thread(void *); 73 74 CFATTACH_DECL_NEW(lightbar, sizeof(struct lightbar_softc), lightbar_match, 75 lightbar_attach, NULL, NULL); 76 77 /* 78 * upper 16 bit are LEDs from the top right to the bottom left 79 * however, the hardware has them rotated so the upper left bit is in 1 80 */ 81 #define LEDMASK(x) ((x << 1) | (((x) & 0x80000000) >> 31)) 82 83 static int 84 lightbar_match(device_t parent, struct cfdata *match, void *aux) 85 { 86 struct confargs *ca; 87 int soundbus, soundchip; 88 char buf[32]; 89 90 ca = aux; 91 if (strcmp(ca->ca_name, "i2s") != 0) 92 return 0; 93 94 if ((soundbus = OF_child(ca->ca_node)) == 0 || 95 (soundchip = OF_child(soundbus)) == 0) 96 return 0; 97 98 if (OF_getprop(soundchip, "virtual", buf, 32) == 0) 99 return 200; /* beat out snapper */ 100 101 return 0; 102 } 103 104 static void 105 lightbar_attach(device_t parent, device_t self, void *aux) 106 { 107 struct lightbar_softc *sc; 108 struct confargs *ca; 109 struct dbdma_command *cmd; 110 struct sysctlnode *node; 111 uint32_t reg[6], x; 112 int i, timo; 113 114 sc = device_private(self); 115 sc->sc_dev = self; 116 117 ca = aux; 118 sc->sc_node = ca->ca_node; 119 sc->sc_tag = ca->ca_tag; 120 121 sc->sc_odmacmd = dbdma_alloc(4 * sizeof(struct dbdma_command), NULL); 122 123 sc->sc_baseaddr = ca->ca_baseaddr; 124 125 /* 126 * default brightness for system and user time bar 127 * can be changed via sysctl later on 128 */ 129 sc->sc_sys = 2; 130 sc->sc_user = 8; 131 132 OF_getprop(sc->sc_node, "reg", reg, sizeof(reg)); 133 reg[0] += ca->ca_baseaddr; 134 reg[2] += ca->ca_baseaddr; 135 136 bus_space_map(sc->sc_tag, reg[0], reg[1], 0, &sc->sc_bsh); 137 obio_space_map(reg[2], reg[3], &sc->sc_odmah); 138 sc->sc_odma = bus_space_vaddr(sc->sc_tag, sc->sc_odmah); 139 DPRINTF("reg %08x odma %08x\n", (uint32_t)sc->sc_bsh, (uint32_t)sc->sc_odmah); 140 141 aprint_normal("\n"); 142 143 /* PMF event handler */ 144 pmf_device_register(sc->sc_dev, NULL, NULL); 145 146 /* enable i2s goop */ 147 x = obio_read_4(KEYLARGO_FCR1); 148 x |= I2S0CLKEN | I2S0EN; 149 obio_write_4(KEYLARGO_FCR1, x); 150 151 /* Clear CLKSTOPPEND. */ 152 bus_space_write_4(sc->sc_tag, sc->sc_bsh, I2S_INT, I2S_INT_CLKSTOPPEND); 153 154 x = obio_read_4(KEYLARGO_FCR1); /* FCR */ 155 x &= ~I2S0CLKEN; /* XXX I2S0 */ 156 obio_write_4(KEYLARGO_FCR1, x); 157 158 /* Wait until clock is stopped. */ 159 for (timo = 1000; timo > 0; timo--) { 160 if (bus_space_read_4(sc->sc_tag, sc->sc_bsh, I2S_INT) & 161 I2S_INT_CLKSTOPPEND) 162 goto done; 163 delay(1); 164 } 165 DPRINTF("timeout\n"); 166 done: 167 bus_space_write_4(sc->sc_tag, sc->sc_bsh, I2S_FORMAT, 168 CLKSRC_18MHz | MCLK_DIV4 | SCLK_DIV32 | SCLK_MASTER | SERIAL_32x); 169 170 x = obio_read_4(KEYLARGO_FCR1); 171 x |= I2S0CLKEN; 172 obio_write_4(KEYLARGO_FCR1, x); 173 174 sc->sc_dmabuf = kmem_alloc(4096, KM_SLEEP); 175 176 /* initial pattern, just to say hi */ 177 for (i = 0; i < 32; i++) { 178 sc->sc_dmabuf[i] = LEDMASK(0xaa550000); 179 } 180 181 /* 182 * We use a single DMA buffer, with just 8 32bit words which the DBDMA 183 * engine loops over. That way we can: 184 * - get away without using interrupts, just scribble a new pattern into 185 * the buffer 186 * - play PWM tricks with the LEDs, giving us 8 levels of brightness 187 */ 188 cmd = sc->sc_odmacmd; 189 DBDMA_BUILD(cmd, DBDMA_CMD_OUT_MORE, 0, 32, vtophys((vaddr_t)sc->sc_dmabuf), 190 0, DBDMA_WAIT_NEVER, DBDMA_BRANCH_NEVER); 191 cmd++; 192 DBDMA_BUILD(cmd, DBDMA_CMD_NOP, 0, 0, 193 0/*vtophys((vaddr_t)sc->sc_odmacmd)*/, 0, DBDMA_WAIT_NEVER, 194 DBDMA_BRANCH_ALWAYS); 195 196 out32rb(&cmd->d_cmddep, vtophys((vaddr_t)sc->sc_odmacmd)); 197 198 dbdma_start(sc->sc_odma, sc->sc_odmacmd); 199 200 sc->sc_cpu[0] = cpu_lookup(0); 201 sc->sc_cpu[1] = cpu_lookup(1); 202 aprint_normal_dev(sc->sc_dev, "monitoring %s\n", 203 sc->sc_cpu[1] == NULL ? "one CPU" : "two CPUs"); 204 205 if (kthread_create(PRI_NONE, 0, NULL, lightbar_thread, sc, 206 &sc->sc_thread, "%s", "lightbar") != 0) { 207 aprint_error_dev(self, "unable to create kthread\n"); 208 } 209 210 /* setup sysctl nodes */ 211 sysctl_createv(NULL, 0, NULL, (void *) &sc->sc_sysctl_me, 212 CTLFLAG_READWRITE, 213 CTLTYPE_NODE, device_xname(sc->sc_dev), NULL, 214 NULL, 0, NULL, 0, 215 CTL_HW, CTL_CREATE, CTL_EOL); 216 sysctl_createv(NULL, 0, NULL, (void *) &node, 217 CTLFLAG_READWRITE | CTLFLAG_OWNDESC, 218 CTLTYPE_INT, "sys", "system time", 219 NULL, 0, (void *)&sc->sc_sys, 0, 220 CTL_HW, 221 sc->sc_sysctl_me->sysctl_num, 222 CTL_CREATE, CTL_EOL); 223 sysctl_createv(NULL, 0, NULL, (void *) &node, 224 CTLFLAG_READWRITE | CTLFLAG_OWNDESC, 225 CTLTYPE_INT, "user", "user time", 226 NULL, 0, (void *)&sc->sc_user, 0, 227 CTL_HW, 228 sc->sc_sysctl_me->sysctl_num, 229 CTL_CREATE, CTL_EOL); 230 } 231 232 /* 233 * this draws a bar into out[0..7], system time as dim, other CPU time as 234 * bright, idle as off 235 * prev[CPUSTATES] stores old counter values, old[2] old bar lengths 236 */ 237 static int 238 lightbar_update(struct lightbar_softc *sc, uint64_t *cp_time, uint64_t *prev, 239 int *old, uint32_t *out) 240 { 241 uint64_t total = 0; 242 int all, sys, idle, syst, i; 243 244 for (i = 0; i < CPUSTATES; i++) 245 total += cp_time[i] - prev[i]; 246 idle = (int)(cp_time[CP_IDLE] - prev[CP_IDLE]); 247 syst = (int)(cp_time[CP_SYS] - prev[CP_SYS]); 248 all = (total - idle) * 8 / total; 249 sys = syst * 8 / total; 250 for (i = 0; i < CPUSTATES; i++) 251 prev[i] = cp_time[i]; 252 if ((all != old[0]) || (sys != old[1])) { 253 for (i = 0; i < sys; i++) out[i] = sc->sc_sys; 254 for (; i < all; i++) out[i] = sc->sc_user; 255 for (; i < 8; i++) out[i] = 0; 256 old[0] = all; 257 old[1] = sys; 258 return 1; 259 } 260 return 0; 261 } 262 263 static void 264 lightbar_thread(void *cookie) 265 { 266 struct lightbar_softc *sc = cookie; 267 uint32_t latch; 268 uint64_t prev[2 * CPUSTATES]; 269 int i, j, old[4] = {0, 0, 0, 0}, intensity[16]; 270 271 for (i = 0; i < 2 * CPUSTATES; i++) 272 prev[i] = 0; 273 274 tsleep(cookie, PRI_NONE, "lights", hz); 275 276 while (1) { 277 int update; 278 /* draw CPU0's usage into the upper bar */ 279 update = lightbar_update(sc, 280 sc->sc_cpu[0]->ci_schedstate.spc_cp_time, 281 prev, old, &intensity[8]); 282 if (sc->sc_cpu[1] != NULL) { 283 /* 284 * if we have a 2nd CPU draw its usage into the lower 285 * bar 286 */ 287 update |= lightbar_update(sc, 288 sc->sc_cpu[1]->ci_schedstate.spc_cp_time, 289 &prev[CPUSTATES], &old[2], intensity); 290 } else { 291 /* 292 * if we don't have a 2nd CPU just duplicate the bar 293 * from the first 294 */ 295 for (i = 0; i < 8; i++) 296 intensity[i] = intensity[i + 8]; 297 } 298 if (update) { 299 /* 300 * this turns our intensity map into a bit pattern for 301 * the hardware - we have 8 samples in our buffer, for 302 * each LED we set the corresponding bit in intensity[j] 303 * samples 304 */ 305 for (i = 0; i < 8; i++) { 306 latch = 0; 307 for (j = 0; j < 16; j++) { 308 if (intensity[j] > i) 309 latch |= 1 << (j + 16); 310 } 311 sc->sc_dmabuf[i] = LEDMASK(latch); 312 } 313 } 314 315 tsleep(cookie, PRI_NONE, "lights", hz / 5); 316 } 317 kthread_exit(0); 318 } 319