#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: autoconf.c,v 1.65 2026/05/11 10:31:50 macallan Exp $");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <dev/ofw/openfirm.h>
#include <powerpc/ofw_machdep.h>
static const char *
sensor_prop_name(char *buf, size_t buflen, int num)
{
snprintf(buf, buflen, "s%02x", num);
return buf;
}
static void
macppc_assign_sensor_names_old(device_t dev, device_t gparent, int phandle)
{
uint32_t ids[4];
char buf[256];
char num[8];
if (! device_is_a(gparent, "ki2c")) {
return;
}
int len = OF_getprop(phandle, "hwsensor-id", ids, sizeof(ids));
if (len <= 0) {
int root = OF_finddevice("/");
if (OF_getprop(phandle, "name", buf, sizeof(buf)) <= 0) {
return;
}
if (strcmp(buf, "temp-monitor") == 0) {
if (OF_getprop(root, "model", buf, sizeof(buf)) <= 0) {
return;
}
if (strcmp(buf, "RackMac1,2") == 0) {
device_setprop_string(dev, "s00", "CPU");
return;
}
}
return;
}
int sllen = OF_getprop(phandle, "hwsensor-location", buf,
sizeof(buf));
if (sllen <= 0) {
return;
}
len >>= 2;
for (int i = 0; i < len; i++) {
const char *descr = strlist_string(buf, sllen, i);
if (descr == NULL) {
return;
}
device_setprop_string(dev,
sensor_prop_name(num, sizeof(num), i),
descr);
}
}
static void
macppc_assign_sensor_names(device_t dev, device_t gparent)
{
int phandle = devhandle_to_of(device_handle(dev));
int snode;
snode = OF_child(phandle);
if (snode == 0) {
macppc_assign_sensor_names_old(dev, gparent, phandle);
return;
}
for (; snode != 0; snode = OF_peer(snode)) {
char descr[64], num[8];
int reg;
if (OF_getprop(snode, "reg", ®, sizeof(reg)) < sizeof(reg)) {
continue;
}
if (OF_getprop(snode, "location", descr, sizeof(descr)) <= 0) {
continue;
}
device_setprop_string(dev,
sensor_prop_name(num, sizeof(num), reg), descr);
}
}
void
device_register(device_t dev, void *aux)
{
ofw_device_register(dev, aux);
device_t parent = device_parent(dev);
if (device_is_a(parent, "iic") &&
devhandle_type(device_handle(dev)) == DEVHANDLE_TYPE_OF) {
macppc_assign_sensor_names(dev, device_parent(parent));
}
}