Home | History | Annotate | Line # | Download | only in dev
      1 /*	$NetBSD: lightbar.c,v 1.2 2025/09/15 06:42:10 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.2 2025/09/15 06:42:10 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 uppper 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 brightnesss 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, 0x01fa0000);
    168 
    169 	x = obio_read_4(KEYLARGO_FCR1);
    170 	x |= I2S0CLKEN;
    171 	obio_write_4(KEYLARGO_FCR1, x);
    172 
    173 	sc->sc_dmabuf = kmem_alloc(4096, KM_SLEEP);
    174 
    175 	/* initial pattern, just to say hi */
    176 	for (i = 0; i < 32; i++) {
    177 		sc->sc_dmabuf[i] = LEDMASK(0xaa550000);
    178 	}
    179 
    180 	/*
    181 	 * We use a single DMA buffer, with just 8 32bit words which the DBDMA
    182 	 * engine loops over. That way we can:
    183 	 * - get away without using interrupts, just scribble a new pattern into
    184 	 *   the buffer
    185 	 * - play PWM tricks with the LEDs, giving us 8 levels of brightness
    186 	 */
    187 	cmd = sc->sc_odmacmd;
    188 	DBDMA_BUILD(cmd, DBDMA_CMD_OUT_MORE, 0, 32, vtophys((vaddr_t)sc->sc_dmabuf),
    189 		    0, DBDMA_WAIT_NEVER, DBDMA_BRANCH_NEVER);
    190 	cmd++;
    191 	DBDMA_BUILD(cmd, DBDMA_CMD_NOP, 0, 0,
    192 	    0/*vtophys((vaddr_t)sc->sc_odmacmd)*/, 0, DBDMA_WAIT_NEVER,
    193 	    DBDMA_BRANCH_ALWAYS);
    194 
    195 	out32rb(&cmd->d_cmddep, vtophys((vaddr_t)sc->sc_odmacmd));
    196 
    197 	dbdma_start(sc->sc_odma, sc->sc_odmacmd);
    198 
    199 	sc->sc_cpu[0] = cpu_lookup(0);
    200 	sc->sc_cpu[1] = cpu_lookup(1);
    201 	aprint_normal_dev(sc->sc_dev, "monitoring %s\n",
    202 	    sc->sc_cpu[1] == NULL ? "one CPU" : "two CPUs");
    203 
    204 	if (kthread_create(PRI_NONE, 0, NULL, lightbar_thread, sc,
    205 	    &sc->sc_thread, "%s", "lightbar") != 0) {
    206 		aprint_error_dev(self, "unable to create kthread\n");
    207 	}
    208 
    209 	/* setup sysctl nodes */
    210 	sysctl_createv(NULL, 0, NULL, (void *) &sc->sc_sysctl_me,
    211 	    CTLFLAG_READWRITE,
    212 	    CTLTYPE_NODE, device_xname(sc->sc_dev), NULL,
    213 	    NULL, 0, NULL, 0,
    214 	    CTL_HW, CTL_CREATE, CTL_EOL);
    215 	sysctl_createv(NULL, 0, NULL, (void *) &node,
    216 	    CTLFLAG_READWRITE | CTLFLAG_OWNDESC,
    217 	    CTLTYPE_INT, "sys", "system time",
    218 	    NULL, 0, (void *)&sc->sc_sys, 0,
    219 	    CTL_HW,
    220 	    sc->sc_sysctl_me->sysctl_num,
    221 	    CTL_CREATE, CTL_EOL);
    222 	sysctl_createv(NULL, 0, NULL, (void *) &node,
    223 	    CTLFLAG_READWRITE | CTLFLAG_OWNDESC,
    224 	    CTLTYPE_INT, "user", "user time",
    225 	    NULL, 0, (void *)&sc->sc_user, 0,
    226 	    CTL_HW,
    227 	    sc->sc_sysctl_me->sysctl_num,
    228 	    CTL_CREATE, CTL_EOL);
    229 }
    230 
    231 /*
    232  * this draws a bar into out[0..7], system time as dim, other CPU time as
    233  * bright, idle as off
    234  * prev[CPUSTATES] stores old counter values, old[2] old bar lengths
    235  */
    236 static int
    237 lightbar_update(struct lightbar_softc *sc, uint64_t *cp_time, uint64_t *prev,
    238 		 int *old, uint32_t *out)
    239 {
    240 	uint64_t total = 0;
    241 	int all, sys, idle, syst, i;
    242 
    243 	for (i = 0; i < CPUSTATES; i++)
    244 		total += cp_time[i] - prev[i];
    245 	idle = (int)(cp_time[CP_IDLE] - prev[CP_IDLE]);
    246 	syst = (int)(cp_time[CP_SYS] - prev[CP_SYS]);
    247 	all = (total - idle) * 8 / total;
    248 	sys = syst * 8 / total;
    249 	for (i = 0; i < CPUSTATES; i++)
    250 		prev[i] = cp_time[i];
    251 	if ((all != old[0]) || (sys != old[1])) {
    252 		for (i = 0; i < sys; i++) out[i] = sc->sc_sys;
    253 		for (; i < all; i++) out[i] = sc->sc_user;
    254 		for (; i < 8; i++) out[i] = 0;
    255 		old[0] = all;
    256 		old[1] = sys;
    257 		return 1;
    258 	}
    259 	return 0;
    260 }
    261 
    262 static void
    263 lightbar_thread(void *cookie)
    264 {
    265 	struct lightbar_softc *sc = cookie;
    266 	uint32_t latch;
    267 	uint64_t prev[2 * CPUSTATES];
    268 	int i, j, old[4] = {0, 0, 0, 0}, intensity[16];
    269 
    270 	for (i = 0; i <  2 * CPUSTATES; i++)
    271 		prev[i] = 0;
    272 
    273 	tsleep(cookie, PRI_NONE, "lights", hz);
    274 
    275 	while (1) {
    276 		int update;
    277 		/* draw CPU0's usage into the upper bar */
    278 		update = lightbar_update(sc,
    279 				sc->sc_cpu[0]->ci_schedstate.spc_cp_time,
    280 				prev, old, &intensity[8]);
    281 		if (sc->sc_cpu[1] != NULL) {
    282 			/*
    283 			 * if we have a 2nd CPU draw its usage into the lower
    284 			 * bar
    285 			 */
    286 			update |= lightbar_update(sc,
    287 				sc->sc_cpu[1]->ci_schedstate.spc_cp_time,
    288 				&prev[CPUSTATES], &old[2], intensity);
    289 		} else {
    290 			/*
    291 			 * if we don't have a 2nd CPU just duplicate the bar
    292 			 * from the first
    293 			 */
    294 			for (i = 0; i < 8; i++)
    295 				intensity[i] = intensity[i + 8];
    296 		}
    297 		if (update) {
    298 			/*
    299 			 * this turns our intensity map into a bit pattern for
    300 			 * the hardware - we have 8 samples in our buffer, for
    301 			 * each LED we set the corresponding bit in intensity[j]
    302 			 * samples
    303 			 */
    304 			for (i = 0; i < 8; i++) {
    305 				latch = 0;
    306 				for (j = 0; j < 16; j++) {
    307 					if (intensity[j] > i)
    308 						latch |= 1 << (j + 16);
    309 				}
    310 				sc->sc_dmabuf[i] = LEDMASK(latch);
    311 			}
    312 		}
    313 
    314 		tsleep(cookie, PRI_NONE, "lights", hz / 5);
    315 	}
    316 	kthread_exit(0);
    317 }
    318