1 /* $NetBSD: autoconf.c,v 1.65 2026/05/11 10:31:50 macallan Exp $ */ 2 3 /* 4 * Copyright (c) 2025 The NetBSD Foundation, Inc. 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 NETBSD FOUNDATION, INC. AND CONTRIBUTORS 17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __KERNEL_RCSID(0, "$NetBSD: autoconf.c,v 1.65 2026/05/11 10:31:50 macallan Exp $"); 31 32 #include <sys/param.h> 33 #include <sys/systm.h> 34 #include <sys/device.h> 35 36 #include <dev/ofw/openfirm.h> 37 38 #include <powerpc/ofw_machdep.h> 39 40 static const char * 41 sensor_prop_name(char *buf, size_t buflen, int num) 42 { 43 snprintf(buf, buflen, "s%02x", num); 44 return buf; 45 } 46 47 static void 48 macppc_assign_sensor_names_old(device_t dev, device_t gparent, int phandle) 49 { 50 uint32_t ids[4]; 51 char buf[256]; 52 char num[8]; 53 54 /* Only if the grandparent is ki2c. */ 55 if (! device_is_a(gparent, "ki2c")) { 56 return; 57 } 58 59 int len = OF_getprop(phandle, "hwsensor-id", ids, sizeof(ids)); 60 61 if (len <= 0) { 62 /* 63 * No info, fill in what we know based on the model. 64 */ 65 int root = OF_finddevice("/"); 66 if (OF_getprop(phandle, "name", buf, sizeof(buf)) <= 0) { 67 return; 68 } 69 if (strcmp(buf, "temp-monitor") == 0) { 70 if (OF_getprop(root, "model", buf, sizeof(buf)) <= 0) { 71 return; 72 } 73 if (strcmp(buf, "RackMac1,2") == 0) { 74 device_setprop_string(dev, "s00", "CPU"); 75 return; 76 } 77 } 78 return; 79 } 80 int sllen = OF_getprop(phandle, "hwsensor-location", buf, 81 sizeof(buf)); 82 if (sllen <= 0) { 83 /* Well that sucks! */ 84 return; 85 } 86 len >>= 2; 87 for (int i = 0; i < len; i++) { 88 const char *descr = strlist_string(buf, sllen, i); 89 if (descr == NULL) { 90 /* Ran out of descriptions. */ 91 return; 92 } 93 /* 94 * Yes, array index instead if ids[i]. The old 95 * OpenFirmware encoding was apparently ... odd. 96 * But macallan@ assures me this is correct. 97 */ 98 device_setprop_string(dev, 99 sensor_prop_name(num, sizeof(num), i), 100 descr); 101 } 102 } 103 104 static void 105 macppc_assign_sensor_names(device_t dev, device_t gparent) 106 { 107 int phandle = devhandle_to_of(device_handle(dev)); 108 int snode; 109 110 snode = OF_child(phandle); 111 if (snode == 0) { 112 macppc_assign_sensor_names_old(dev, gparent, phandle); 113 return; 114 } 115 116 for (; snode != 0; snode = OF_peer(snode)) { 117 char descr[64], num[8]; 118 int reg; 119 120 if (OF_getprop(snode, "reg", ®, sizeof(reg)) < sizeof(reg)) { 121 continue; 122 } 123 if (OF_getprop(snode, "location", descr, sizeof(descr)) <= 0) { 124 continue; 125 } 126 device_setprop_string(dev, 127 sensor_prop_name(num, sizeof(num), reg), descr); 128 } 129 } 130 131 void 132 device_register(device_t dev, void *aux) 133 { 134 ofw_device_register(dev, aux); 135 136 /* 137 * Look for sensors hung off an I2C controller and pass 138 * along the sensor name properties. 139 */ 140 device_t parent = device_parent(dev); 141 if (device_is_a(parent, "iic") && 142 devhandle_type(device_handle(dev)) == DEVHANDLE_TYPE_OF) { 143 macppc_assign_sensor_names(dev, device_parent(parent)); 144 } 145 } 146