Home | History | Annotate | Line # | Download | only in dev
smu.c revision 1.14
      1 /*-
      2  * Copyright (c) 2013 Phileas Fogg
      3  * All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  * 1. Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  * 2. Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in the
     12  *    documentation and/or other materials provided with the distribution.
     13  *
     14  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     15  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     16  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     17  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     18  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     19  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     20  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     21  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     22  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     23  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     24  * POSSIBILITY OF SUCH DAMAGE.
     25  */
     26 
     27 #include <sys/param.h>
     28 #include <sys/systm.h>
     29 #include <sys/kernel.h>
     30 #include <sys/malloc.h>
     31 #include <sys/device.h>
     32 #include <sys/proc.h>
     33 #include <sys/mutex.h>
     34 #include <sys/time.h>
     35 #include <sys/reboot.h>
     36 #include <sys/sysctl.h>
     37 #include <sys/kthread.h>
     38 
     39 #include <machine/autoconf.h>
     40 
     41 #include <dev/ofw/openfirm.h>
     42 #include <dev/i2c/i2cvar.h>
     43 #include <dev/clock_subr.h>
     44 #include <dev/sysmon/sysmonvar.h>
     45 #include <dev/sysmon/sysmon_taskq.h>
     46 
     47 #include <macppc/dev/obiovar.h>
     48 #include <macppc/dev/smuvar.h>
     49 
     50 #include "opt_smu.h"
     51 
     52 struct smu_softc;
     53 
     54 struct smu_cmd {
     55 	u_char cmd;
     56 	u_char len;
     57 	u_char data[254];
     58 };
     59 
     60 struct smu_fan {
     61 	struct smu_softc* sc;
     62 
     63 	char location[32];
     64 	int reg;
     65 	int zone;
     66 	int rpm_ctl;
     67 	int min_rpm;
     68 	int max_rpm;
     69 	int default_rpm;
     70 	int current_rpm;
     71 	time_t last_update;
     72 };
     73 
     74 struct smu_iicbus {
     75 	struct smu_softc* sc;
     76 
     77 	int reg;
     78 	struct i2c_controller i2c;
     79 };
     80 
     81 #define SMU_MAX_FANS		8
     82 #define SMU_MAX_IICBUS		3
     83 #define SMU_MAX_SME_SENSORS	(SMU_MAX_FANS + 8)
     84 
     85 struct smu_zone {
     86 	bool (*filter)(const envsys_data_t *);
     87 	int nfans;
     88 	int fans[SMU_MAX_FANS];
     89 	int threshold, step;
     90 	int duty;
     91 };
     92 
     93 
     94 #define SMU_ZONE_CPUS	0
     95 #define SMU_ZONE_DRIVES	1
     96 #define SMU_ZONE_SLOTS	2
     97 #define SMU_ZONES	3
     98 
     99 #define C_TO_uK(n) (n * 1000000 + 273150000)
    100 
    101 struct smu_softc {
    102 	device_t sc_dev;
    103 	int sc_node;
    104 	struct sysctlnode *sc_sysctl_me;
    105 
    106 	kmutex_t sc_cmd_lock;
    107 	kmutex_t sc_msg_lock;
    108 	struct smu_cmd *sc_cmd;
    109 	paddr_t sc_cmd_paddr;
    110 	int sc_dbell_mbox;
    111 	int sc_dbell_gpio;
    112 
    113 	int sc_num_fans;
    114 	struct smu_fan sc_fans[SMU_MAX_FANS];
    115 
    116 	int sc_num_iicbus;
    117 	struct smu_iicbus sc_iicbus[SMU_MAX_IICBUS];
    118 
    119 	struct todr_chip_handle sc_todr;
    120 
    121 	struct sysmon_envsys *sc_sme;
    122 	envsys_data_t sc_sme_sensors[SMU_MAX_SME_SENSORS];
    123 	uint32_t cpu_m;
    124 	int32_t  cpu_b;
    125 
    126 	struct smu_zone sc_zones[SMU_ZONES];
    127 	lwp_t *sc_thread;
    128 	bool sc_dying;
    129 };
    130 
    131 #define SMU_CMD_FAN	0x4a
    132 #define SMU_CMD_RTC	0x8e
    133 #define SMU_CMD_I2C	0x9a
    134 #define SMU_CMD_POWER	0xaa
    135 #define SMU_CMD_ADC	0xd8
    136 #define SMU_MISC	0xee
    137 #define  SMU_MISC_GET_DATA	0x02
    138 #define  SMU_MISC_LED_CTRL	0x04
    139 
    140 #define SMU_CPUTEMP_CAL 0x18
    141 #define SMU_CPUVOLT_CAL	0x21
    142 #define SMU_SLOTPW_CAL	0x78
    143 
    144 #define SMU_PARTITION		0x3e
    145 #define SMU_PARTITION_LATEST	0x01
    146 #define SMU_PARTITION_BASE	0x02
    147 #define SMU_PARTITION_UPDATE	0x03
    148 
    149 #ifdef SMU_DEBUG
    150 #define DPRINTF printf
    151 #else
    152 #define DPRINTF while (0) printf
    153 #endif
    154 
    155 static int smu_match(device_t, struct cfdata *, void *);
    156 static void smu_attach(device_t, device_t, void *);
    157 static int smu_setup_doorbell(struct smu_softc *);
    158 static void smu_setup_fans(struct smu_softc *);
    159 static void smu_setup_iicbus(struct smu_softc *);
    160 static void smu_setup_sme(struct smu_softc *);
    161 static int smu_iicbus_print(void *, const char *);
    162 static void smu_sme_refresh(struct sysmon_envsys *, envsys_data_t *);
    163 static int smu_do_cmd(struct smu_softc *, struct smu_cmd *, int);
    164 static int smu_dbell_gpio_intr(void *);
    165 static int smu_todr_gettime_ymdhms(todr_chip_handle_t, struct clock_ymdhms *);
    166 static int smu_todr_settime_ymdhms(todr_chip_handle_t, struct clock_ymdhms *);
    167 static int smu_fan_update_rpm(struct smu_fan *);
    168 static int smu_fan_get_rpm(struct smu_fan *, int *);
    169 static int smu_fan_set_rpm(struct smu_fan *, int);
    170 static int smu_read_adc(struct smu_softc *, int);
    171 
    172 static int smu_iicbus_exec(void *, i2c_op_t, i2c_addr_t, const void *,
    173     size_t, void *, size_t, int);
    174 static int smu_sysctl_fan_rpm(SYSCTLFN_ARGS);
    175 
    176 static void smu_setup_zones(struct smu_softc *);
    177 static void smu_adjust_zone(struct smu_softc *, int);
    178 static void smu_adjust(void *);
    179 static bool is_cpu_sensor(const envsys_data_t *);
    180 static bool is_drive_sensor(const envsys_data_t *);
    181 static bool is_slots_sensor(const envsys_data_t *);
    182 
    183 int smu_get_datablock(int, uint8_t *, size_t);
    184 
    185 CFATTACH_DECL_NEW(smu, sizeof(struct smu_softc),
    186     smu_match, smu_attach, NULL, NULL);
    187 
    188 static struct smu_softc *smu0 = NULL;
    189 
    190 static int
    191 smu_match(device_t parent, struct cfdata *cf, void *aux)
    192 {
    193 	struct confargs *ca = aux;
    194 
    195 	if (strcmp(ca->ca_name, "smu") == 0)
    196 		return 5;
    197 
    198 	return 0;
    199 }
    200 
    201 static void
    202 smu_attach(device_t parent, device_t self, void *aux)
    203 {
    204 	struct confargs *ca = aux;
    205 	struct smu_softc *sc = device_private(self);
    206 	uint16_t data[4];
    207 
    208 	sc->sc_dev = self;
    209 	sc->sc_node = ca->ca_node;
    210 
    211 	if (smu0 == NULL)
    212 		smu0 = sc;
    213 
    214 	sysctl_createv(NULL, 0, NULL, (void *) &sc->sc_sysctl_me,
    215 	    CTLFLAG_READWRITE,
    216 	    CTLTYPE_NODE, device_xname(sc->sc_dev), NULL,
    217 	    NULL, 0, NULL, 0,
    218 	    CTL_MACHDEP, CTL_CREATE, CTL_EOL);
    219 
    220 	if (smu_setup_doorbell(sc) != 0) {
    221 		aprint_normal(": unable to set up doorbell\n");
    222 		return;
    223 	}
    224 
    225 	aprint_normal("\n");
    226 
    227 	smu_setup_fans(sc);
    228 	smu_setup_iicbus(sc);
    229 
    230 	sc->sc_todr.todr_gettime_ymdhms = smu_todr_gettime_ymdhms;
    231 	sc->sc_todr.todr_settime_ymdhms = smu_todr_settime_ymdhms;
    232 	sc->sc_todr.cookie = sc;
    233 	todr_attach(&sc->sc_todr);
    234 
    235 	/* calibration data */
    236 	memset(data, 0, 8);
    237 	smu_get_datablock(SMU_CPUTEMP_CAL, (void *)data, 8);
    238 	DPRINTF("data %04x %04x %04x %04x\n", data[0], data[1], data[2], data[3]);
    239 	sc->cpu_m = data[2];
    240 	sc->cpu_b = (int16_t)data[3];
    241 
    242 	smu_setup_sme(sc);
    243 
    244 	smu_setup_zones(sc);
    245 }
    246 
    247 static int
    248 smu_setup_doorbell(struct smu_softc *sc)
    249 {
    250 	int node, parent, reg[4], gpio_base, irq;
    251 
    252 	mutex_init(&sc->sc_cmd_lock, MUTEX_DEFAULT, IPL_NONE);
    253 	sc->sc_cmd = malloc(4096, M_DEVBUF, M_WAITOK);
    254 	sc->sc_cmd_paddr = vtophys((vaddr_t) sc->sc_cmd);
    255 
    256 	DPRINTF("%s: cmd vaddr 0x%x paddr 0x%x\n",
    257 	    __func__, (unsigned int) sc->sc_cmd,
    258 	    (unsigned int) sc->sc_cmd_paddr);
    259 
    260 	if (OF_getprop(sc->sc_node, "platform-doorbell-buff",
    261 	        &node, sizeof(node)) <= 0)
    262 		return -1;
    263 
    264 	if (OF_getprop(node, "platform-do-doorbell-buff",
    265 	        reg, sizeof(reg)) < sizeof(reg))
    266 		return -1;
    267 
    268 	sc->sc_dbell_mbox = reg[3];
    269 
    270 	if (OF_getprop(sc->sc_node, "platform-doorbell-ack",
    271 	        &node, sizeof(node)) <= 0)
    272 		return -1;
    273 
    274 	parent = OF_parent(node);
    275 	if (parent == 0)
    276 		return -1;
    277 
    278 	if (OF_getprop(parent, "reg", &gpio_base, sizeof(gpio_base)) <= 0)
    279 		return -1;
    280 
    281 	if (OF_getprop(node, "reg", reg, sizeof(reg)) <= 0)
    282 		return -1;
    283 
    284 	if (OF_getprop(node, "interrupts", &irq, sizeof(irq)) <= 0)
    285 		return -1;
    286 
    287 	sc->sc_dbell_gpio = gpio_base + reg[0];
    288 
    289 	aprint_normal(" mbox 0x%x gpio 0x%x irq %d",
    290 	    sc->sc_dbell_mbox, sc->sc_dbell_gpio, irq);
    291 
    292 	intr_establish_xname(irq, IST_EDGE_FALLING, IPL_TTY,
    293 	    smu_dbell_gpio_intr, sc, device_xname(sc->sc_dev));
    294 
    295 	return 0;
    296 }
    297 
    298 static void
    299 smu_setup_fans(struct smu_softc *sc)
    300 {
    301 	struct smu_fan *fan;
    302 	struct sysctlnode *sysctl_fans, *sysctl_fan, *sysctl_node;
    303 	char type[32], sysctl_fan_name[32];
    304 	int node, i, j;
    305 	const char *fans[] = { "fans", "rpm-fans", 0 };
    306 	int n = 0;
    307 
    308 	while (fans[n][0] != 0) {
    309 		node = of_getnode_byname(sc->sc_node, fans[n]);
    310 		for (node = OF_child(node);
    311 		    (node != 0) && (sc->sc_num_fans < SMU_MAX_FANS);
    312 		    node = OF_peer(node)) {
    313 			fan = &sc->sc_fans[sc->sc_num_fans];
    314 			fan->sc = sc;
    315 
    316 			memset(fan->location, 0, sizeof(fan->location));
    317 			OF_getprop(node, "location", fan->location,
    318 			    sizeof(fan->location));
    319 
    320 			if (OF_getprop(node, "reg", &fan->reg,
    321 			        sizeof(fan->reg)) <= 0)
    322 				continue;
    323 
    324 			if (OF_getprop(node, "zone", &fan->zone	,
    325 			        sizeof(fan->zone)) <= 0)
    326 				continue;
    327 
    328 			memset(type, 0, sizeof(type));
    329 			OF_getprop(node, "device_type", type, sizeof(type));
    330 			if (strcmp(type, "fan-rpm-control") == 0)
    331 				fan->rpm_ctl = 1;
    332 			else
    333 				fan->rpm_ctl = 0;
    334 
    335 			if (OF_getprop(node, "min-value", &fan->min_rpm,
    336 			    sizeof(fan->min_rpm)) <= 0)
    337 				fan->min_rpm = 0;
    338 
    339 			if (OF_getprop(node, "max-value", &fan->max_rpm,
    340 			    sizeof(fan->max_rpm)) <= 0)
    341 				fan->max_rpm = 0xffff;
    342 
    343 			if (OF_getprop(node, "unmanage-value", &fan->default_rpm,
    344 			    sizeof(fan->default_rpm)) <= 0)
    345 				fan->default_rpm = fan->max_rpm;
    346 
    347 			DPRINTF("fan: location %s reg %x zone %d rpm_ctl %d "
    348 			    "min_rpm %d max_rpm %d default_rpm %d\n",
    349 			    fan->location, fan->reg, fan->zone, fan->rpm_ctl,
    350 			    fan->min_rpm, fan->max_rpm, fan->default_rpm);
    351 
    352 			sc->sc_num_fans++;
    353 		}
    354 		n++;
    355 	}
    356 
    357 	for (i = 0; i < sc->sc_num_fans; i++) {
    358 		fan = &sc->sc_fans[i];
    359 		smu_fan_set_rpm(fan, fan->default_rpm);
    360 		smu_fan_get_rpm(fan, &fan->current_rpm);
    361 	}
    362 
    363 	/* Create sysctl nodes for each fan */
    364 
    365 	sysctl_createv(NULL, 0, NULL, (void *) &sysctl_fans,
    366 	    CTLFLAG_READWRITE | CTLFLAG_OWNDESC,
    367 	    CTLTYPE_NODE, "fans", NULL,
    368 	    NULL, 0, NULL, 0,
    369 	    CTL_MACHDEP,
    370 	    sc->sc_sysctl_me->sysctl_num,
    371 	    CTL_CREATE, CTL_EOL);
    372 
    373 	for (i = 0; i < sc->sc_num_fans; i++) {
    374 		fan = &sc->sc_fans[i];
    375 
    376 		for (j = 0; j < strlen(fan->location); j++) {
    377 			sysctl_fan_name[j] = tolower(fan->location[j]);
    378 			if (sysctl_fan_name[j] == ' ')
    379 				sysctl_fan_name[j] = '_';
    380 		}
    381 		sysctl_fan_name[j] = '\0';
    382 
    383 		sysctl_createv(NULL, 0, NULL, (void *) &sysctl_fan,
    384 		    CTLFLAG_READWRITE | CTLFLAG_OWNDESC,
    385 		    CTLTYPE_NODE, sysctl_fan_name, "fan information",
    386 		    NULL, 0, NULL, 0,
    387 		    CTL_MACHDEP,
    388 		    sc->sc_sysctl_me->sysctl_num,
    389 		    sysctl_fans->sysctl_num,
    390 		    CTL_CREATE, CTL_EOL);
    391 
    392 		sysctl_createv(NULL, 0, NULL, (void *) &sysctl_node,
    393 		    CTLFLAG_READONLY | CTLFLAG_OWNDESC,
    394 		    CTLTYPE_INT, "zone", "fan zone",
    395 		    NULL, 0, &fan->zone, 0,
    396 		    CTL_MACHDEP,
    397 		    sc->sc_sysctl_me->sysctl_num,
    398 		    sysctl_fans->sysctl_num,
    399 		    sysctl_fan->sysctl_num,
    400 		    CTL_CREATE, CTL_EOL);
    401 
    402 		sysctl_createv(NULL, 0, NULL, (void *) &sysctl_node,
    403 		    CTLFLAG_READONLY | CTLFLAG_OWNDESC,
    404 		    CTLTYPE_INT, "min_rpm", "fan minimum rpm",
    405 		    NULL, 0, &fan->min_rpm, 0,
    406 		    CTL_MACHDEP,
    407 		    sc->sc_sysctl_me->sysctl_num,
    408 		    sysctl_fans->sysctl_num,
    409 		    sysctl_fan->sysctl_num,
    410 		    CTL_CREATE, CTL_EOL);
    411 
    412 		sysctl_createv(NULL, 0, NULL, (void *) &sysctl_node,
    413 		    CTLFLAG_READONLY | CTLFLAG_OWNDESC,
    414 		    CTLTYPE_INT, "max_rpm", "fan maximum rpm",
    415 		    NULL, 0, &fan->max_rpm, 0,
    416 		    CTL_MACHDEP,
    417 		    sc->sc_sysctl_me->sysctl_num,
    418 		    sysctl_fans->sysctl_num,
    419 		    sysctl_fan->sysctl_num,
    420 		    CTL_CREATE, CTL_EOL);
    421 
    422 		sysctl_createv(NULL, 0, NULL, (void *) &sysctl_node,
    423 		    CTLFLAG_READONLY | CTLFLAG_OWNDESC,
    424 		    CTLTYPE_INT, "default_rpm", "fan default rpm",
    425 		    NULL, 0, &fan->default_rpm, 0,
    426 		    CTL_MACHDEP,
    427 		    sc->sc_sysctl_me->sysctl_num,
    428 		    sysctl_fans->sysctl_num,
    429 		    sysctl_fan->sysctl_num,
    430 		    CTL_CREATE, CTL_EOL);
    431 
    432 		sysctl_createv(NULL, 0, NULL, (void *) &sysctl_node,
    433 		    CTLFLAG_READWRITE | CTLFLAG_OWNDESC,
    434 		    CTLTYPE_INT, "rpm", "fan current rpm",
    435 		    smu_sysctl_fan_rpm, 0, (void *) fan, 0,
    436 		    CTL_MACHDEP,
    437 		    sc->sc_sysctl_me->sysctl_num,
    438 		    sysctl_fans->sysctl_num,
    439 		    sysctl_fan->sysctl_num,
    440 		    CTL_CREATE, CTL_EOL);
    441 	}
    442 }
    443 
    444 static void
    445 smu_setup_iicbus(struct smu_softc *sc)
    446 {
    447 	struct smu_iicbus *iicbus;
    448 	struct i2c_controller *i2c;
    449 	struct smu_iicbus_confargs ca;
    450 	int node;
    451 	char name[32];
    452 
    453 	node = of_getnode_byname(sc->sc_node, "smu-i2c-control");
    454 	if (node == 0) node = sc->sc_node;
    455 	for (node = OF_child(node);
    456 	    (node != 0) && (sc->sc_num_iicbus < SMU_MAX_IICBUS);
    457 	    node = OF_peer(node)) {
    458 		memset(name, 0, sizeof(name));
    459 		OF_getprop(node, "name", name, sizeof(name));
    460 		if ((strcmp(name, "i2c-bus") != 0) &&
    461 		    (strcmp(name, "i2c") != 0))
    462 			continue;
    463 
    464 		iicbus = &sc->sc_iicbus[sc->sc_num_iicbus];
    465 		iicbus->sc = sc;
    466 		i2c = &iicbus->i2c;
    467 
    468 		if (OF_getprop(node, "reg", &iicbus->reg, sizeof(iicbus->reg)) <= 0)
    469 			continue;
    470 
    471 		DPRINTF("iicbus: reg %x\n", iicbus->reg);
    472 
    473 		iic_tag_init(i2c);
    474 		i2c->ic_cookie = iicbus;
    475 		i2c->ic_exec = smu_iicbus_exec;
    476 
    477 		ca.ca_name = name;
    478 		ca.ca_node = node;
    479 		ca.ca_tag = i2c;
    480 		config_found(sc->sc_dev, &ca, smu_iicbus_print,
    481 		    CFARGS(.devhandle = devhandle_from_of(node)));
    482 
    483 		sc->sc_num_iicbus++;
    484 	}
    485 }
    486 
    487 static void
    488 smu_setup_sme(struct smu_softc *sc)
    489 {
    490 	struct smu_fan *fan;
    491 	envsys_data_t *sme_sensor;
    492 	int i, sensors, child, reg;
    493 	char loc[32], type[32];
    494 
    495 	sc->sc_sme = sysmon_envsys_create();
    496 
    497 	for (i = 0; i < sc->sc_num_fans; i++) {
    498 		sme_sensor = &sc->sc_sme_sensors[i];
    499 		fan = &sc->sc_fans[i];
    500 
    501 		sme_sensor->units = ENVSYS_SFANRPM;
    502 		sme_sensor->state = ENVSYS_SINVALID;
    503 		snprintf(sme_sensor->desc, sizeof(sme_sensor->desc),
    504 		    "%s", fan->location);
    505 
    506 		if (sysmon_envsys_sensor_attach(sc->sc_sme, sme_sensor)) {
    507 			sysmon_envsys_destroy(sc->sc_sme);
    508 			return;
    509 		}
    510 	}
    511 	sensors = OF_finddevice("/smu/sensors");
    512 	child = OF_child(sensors);
    513 	while (child != 0) {
    514 		sme_sensor = &sc->sc_sme_sensors[i];
    515 		if (OF_getprop(child, "location", loc, 32) == 0) goto next;
    516 		if (OF_getprop(child, "device_type", type, 32) == 0) goto next;
    517 		if (OF_getprop(child, "reg", &reg, 4) == 0) goto next;
    518 		if (strcmp(type, "temp-sensor") == 0) {
    519 			sme_sensor->units = ENVSYS_STEMP;
    520 			sme_sensor->state = ENVSYS_SINVALID;
    521 			strncpy(sme_sensor->desc, loc, sizeof(sme_sensor->desc));
    522 			sme_sensor->private = reg;
    523 			sysmon_envsys_sensor_attach(sc->sc_sme, sme_sensor);
    524 			i++;
    525 			printf("%s: %s@%x\n", loc, type, reg);
    526 		}
    527 next:
    528 		child = OF_peer(child);
    529 	}
    530 
    531 	sc->sc_sme->sme_name = device_xname(sc->sc_dev);
    532 	sc->sc_sme->sme_cookie = sc;
    533 	sc->sc_sme->sme_refresh = smu_sme_refresh;
    534 
    535 	if (sysmon_envsys_register(sc->sc_sme)) {
    536 		aprint_error_dev(sc->sc_dev,
    537 		    "unable to register with sysmon\n");
    538 		sysmon_envsys_destroy(sc->sc_sme);
    539 	}
    540 }
    541 
    542 static int
    543 smu_iicbus_print(void *aux, const char *smu)
    544 {
    545 	struct smu_iicbus_confargs *ca = aux;
    546 
    547 	if (smu)
    548 		aprint_normal("%s at %s", ca->ca_name, smu);
    549 
    550 	return UNCONF;
    551 }
    552 
    553 static void
    554 smu_sme_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
    555 {
    556 	struct smu_softc *sc = sme->sme_cookie;
    557 	struct smu_fan *fan;
    558 	int which = edata->sensor;
    559 	int ret;
    560 
    561 	edata->state = ENVSYS_SINVALID;
    562 
    563 	if (which < sc->sc_num_fans) {
    564 		fan = &sc->sc_fans[which];
    565 
    566 		ret = smu_fan_get_rpm(fan, &fan->current_rpm);
    567 		if (ret == 0) {
    568 			edata->value_cur = fan->current_rpm;
    569 			edata->state = ENVSYS_SVALID;
    570 		}
    571 	} else if (edata->private > 0) {
    572 		/* this works only for the CPU diode */
    573 		int64_t r = smu_read_adc(sc, edata->private);
    574 		if (r != -1) {
    575 			r = r * sc->cpu_m;
    576 			r >>= 3;
    577 			r += (int64_t)sc->cpu_b << 9;
    578 			r <<= 1;
    579 			r *= 15625;
    580 			r /= 1024;
    581 			edata->value_cur = r + 273150000;
    582 			edata->state = ENVSYS_SVALID;
    583 		}
    584 	}
    585 }
    586 
    587 static int
    588 smu_do_cmd(struct smu_softc *sc, struct smu_cmd *cmd, int timo)
    589 {
    590 	int gpio, ret, bail;
    591 	u_char ack;
    592 
    593 	mutex_enter(&sc->sc_cmd_lock);
    594 
    595 	DPRINTF("%s: cmd %02x len %02x\n", __func__, cmd->cmd, cmd->len);
    596 	DPRINTF("%s: data %02x %02x %02x %02x %02x %02x %02x %02x\n", __func__,
    597 	    cmd->data[0], cmd->data[1], cmd->data[2], cmd->data[3],
    598 	    cmd->data[4], cmd->data[5], cmd->data[6], cmd->data[7]);
    599 
    600 	sc->sc_cmd->cmd = cmd->cmd;
    601 	sc->sc_cmd->len = cmd->len;
    602 	memcpy(sc->sc_cmd->data, cmd->data, cmd->len);
    603 
    604 	__asm volatile ("dcbf 0,%0; sync" :: "r"(sc->sc_cmd) : "memory");
    605 
    606 	obio_write_4(sc->sc_dbell_mbox, sc->sc_cmd_paddr);
    607 	obio_write_1(sc->sc_dbell_gpio, 0x04);
    608 
    609 	bail = 0;
    610 
    611 	gpio = obio_read_1(sc->sc_dbell_gpio);
    612 
    613 	while (((gpio & 0x07) != 0x07) && (bail < timo)) {
    614 		ret = tsleep(sc->sc_cmd, PWAIT, "smu_cmd", mstohz(10));
    615 		if (ret != 0) {
    616 			bail++;
    617 		}
    618 		gpio = obio_read_1(sc->sc_dbell_gpio);
    619 	}
    620 
    621 	if ((gpio & 0x07) != 0x07) {
    622 		mutex_exit(&sc->sc_cmd_lock);
    623 		return EWOULDBLOCK;
    624 	}
    625 
    626 	__asm volatile ("dcbf 0,%0; sync" :: "r"(sc->sc_cmd) : "memory");
    627 
    628 	ack = (~cmd->cmd) & 0xff;
    629 	if (sc->sc_cmd->cmd != ack) {
    630 		DPRINTF("%s: invalid ack, got %x expected %x\n",
    631 		    __func__, sc->sc_cmd->cmd, ack);
    632 		mutex_exit(&sc->sc_cmd_lock);
    633 		return EIO;
    634 	}
    635 
    636 	cmd->cmd = sc->sc_cmd->cmd;
    637 	cmd->len = sc->sc_cmd->len;
    638 	memcpy(cmd->data, sc->sc_cmd->data, sc->sc_cmd->len);
    639 
    640 	mutex_exit(&sc->sc_cmd_lock);
    641 
    642 	return 0;
    643 }
    644 
    645 
    646 static int
    647 smu_dbell_gpio_intr(void *arg)
    648 {
    649 	struct smu_softc *sc = arg;
    650 
    651 	DPRINTF("%s\n", __func__);
    652 
    653 	wakeup(sc->sc_cmd);
    654 
    655 	return 1;
    656 }
    657 
    658 void
    659 smu_poweroff(void)
    660 {
    661 	struct smu_cmd cmd;
    662 
    663 	if (smu0 == NULL)
    664 		return;
    665 
    666 	cmd.cmd = SMU_CMD_POWER;
    667 	strcpy(cmd.data, "SHUTDOWN");
    668 	cmd.len = strlen(cmd.data) + 1;
    669 	smu_do_cmd(smu0, &cmd, 800);
    670 
    671 	for (;;);
    672 }
    673 
    674 void
    675 smu_restart(void)
    676 {
    677 	struct smu_cmd cmd;
    678 
    679 	if (smu0 == NULL)
    680 		return;
    681 
    682 	cmd.cmd = SMU_CMD_POWER;
    683 	strcpy(cmd.data, "RESTART");
    684 	cmd.len = strlen(cmd.data) + 1;
    685 	smu_do_cmd(smu0, &cmd, 800);
    686 
    687 	for (;;);
    688 }
    689 
    690 static int
    691 smu_todr_gettime_ymdhms(todr_chip_handle_t tch, struct clock_ymdhms *dt)
    692 {
    693 	struct smu_softc *sc = tch->cookie;
    694 	struct smu_cmd cmd;
    695 	int ret;
    696 
    697 	cmd.cmd = SMU_CMD_RTC;
    698 	cmd.len = 1;
    699 	cmd.data[0] = 0x81;
    700 
    701 	ret = smu_do_cmd(sc, &cmd, 800);
    702 	if (ret != 0)
    703 		return ret;
    704 
    705 	dt->dt_sec = bcdtobin(cmd.data[0]);
    706 	dt->dt_min = bcdtobin(cmd.data[1]);
    707 	dt->dt_hour = bcdtobin(cmd.data[2]);
    708 	dt->dt_wday = bcdtobin(cmd.data[3]);
    709 	dt->dt_day = bcdtobin(cmd.data[4]);
    710 	dt->dt_mon = bcdtobin(cmd.data[5]);
    711 	dt->dt_year = bcdtobin(cmd.data[6]) + 2000;
    712 
    713 	return 0;
    714 }
    715 
    716 static int
    717 smu_todr_settime_ymdhms(todr_chip_handle_t tch, struct clock_ymdhms *dt)
    718 {
    719 	struct smu_softc *sc = tch->cookie;
    720 	struct smu_cmd cmd;
    721 
    722 	cmd.cmd = SMU_CMD_RTC;
    723 	cmd.len = 8;
    724 	cmd.data[0] = 0x80;
    725 	cmd.data[1] = bintobcd(dt->dt_sec);
    726 	cmd.data[2] = bintobcd(dt->dt_min);
    727 	cmd.data[3] = bintobcd(dt->dt_hour);
    728 	cmd.data[4] = bintobcd(dt->dt_wday);
    729 	cmd.data[5] = bintobcd(dt->dt_day);
    730 	cmd.data[6] = bintobcd(dt->dt_mon);
    731 	cmd.data[7] = bintobcd(dt->dt_year - 2000);
    732 
    733 	return smu_do_cmd(sc, &cmd, 800);
    734 }
    735 
    736 static int
    737 smu_fan_update_rpm(struct smu_fan *fan)
    738 {
    739 	struct smu_softc *sc = fan->sc;
    740 	struct smu_cmd cmd;
    741 	int ret;
    742 
    743 	cmd.cmd = SMU_CMD_FAN;
    744 	cmd.len = 2;
    745 	cmd.data[0] = 0x31;
    746 	cmd.data[1] = fan->reg;
    747 
    748 	ret = smu_do_cmd(sc, &cmd, 800);
    749 	if (ret == 0) {
    750 		fan->last_update = time_uptime;
    751 		fan->current_rpm = (cmd.data[0] << 8) | cmd.data[1];
    752 	} else {
    753 		cmd.cmd = SMU_CMD_FAN;
    754 		cmd.len = 1;
    755 		cmd.data[0] = 0x01;
    756 
    757 		ret = smu_do_cmd(sc, &cmd, 800);
    758 		if (ret == 0) {
    759 			fan->last_update = time_uptime;
    760 			fan->current_rpm = (cmd.data[1 + fan->reg * 2] << 8) |
    761 			    cmd.data[2 + fan->reg * 2];
    762 		}
    763 	}
    764 
    765 	return ret;
    766 }
    767 
    768 static int
    769 smu_fan_get_rpm(struct smu_fan *fan, int *rpm)
    770 {
    771 	int ret;
    772 	ret = 0;
    773 
    774 	if (time_uptime - fan->last_update > 1) {
    775 		ret = smu_fan_update_rpm(fan);
    776 		if (ret != 0)
    777 			return ret;
    778 	}
    779 
    780 	*rpm = fan->current_rpm;
    781 
    782 	return ret;
    783 }
    784 
    785 static int
    786 smu_fan_set_rpm(struct smu_fan *fan, int rpm)
    787 {
    788 	struct smu_softc *sc = fan->sc;
    789 	struct smu_cmd cmd;
    790 	int ret;
    791 
    792 	DPRINTF("%s: fan %s rpm %d\n", __func__, fan->location, rpm);
    793 
    794 	rpm = uimax(fan->min_rpm, rpm);
    795 	rpm = uimin(fan->max_rpm, rpm);
    796 
    797 	cmd.cmd = SMU_CMD_FAN;
    798 	cmd.len = 4;
    799 	cmd.data[0] = 0x30;
    800 	cmd.data[1] = fan->reg;
    801 	cmd.data[2] = (rpm >> 8) & 0xff;
    802 	cmd.data[3] = rpm & 0xff;
    803 
    804 	ret = smu_do_cmd(sc, &cmd, 800);
    805 	if (ret != 0) {
    806 		cmd.cmd = SMU_CMD_FAN;
    807 		cmd.len = 14;
    808 		cmd.data[0] = fan->rpm_ctl ? 0x00 : 0x10;
    809 		cmd.data[1] = 1 << fan->reg;
    810 		cmd.data[2] = cmd.data[2 + fan->reg * 2] = (rpm >> 8) & 0xff;
    811 		cmd.data[3] = cmd.data[3 + fan->reg * 2] = rpm & 0xff;
    812 
    813 		ret = smu_do_cmd(sc, &cmd, 800);
    814 	}
    815 
    816 	return ret;
    817 }
    818 
    819 static int
    820 smu_read_adc(struct smu_softc *sc, int id)
    821 {
    822 	struct smu_cmd cmd;
    823 	int ret;
    824 
    825 	cmd.cmd = SMU_CMD_ADC;
    826 	cmd.len = 1;
    827 	cmd.data[0] = id;
    828 
    829 	ret = smu_do_cmd(sc, &cmd, 800);
    830 	if (ret == 0) {
    831 		return cmd.data[0] << 8 | cmd.data[1];
    832 	}
    833 	return -1;
    834 }
    835 
    836 static int
    837 smu_iicbus_exec(void *cookie, i2c_op_t op, i2c_addr_t addr, const void *send,
    838     size_t send_len, void *recv, size_t recv_len, int flags)
    839 {
    840 	struct smu_iicbus *iicbus = cookie;
    841 	struct smu_softc *sc = iicbus->sc;
    842 	struct smu_cmd cmd;
    843 	int retries, ret;
    844 
    845 	DPRINTF("%s: op %x addr %x send_len %d recv_len %d\n",
    846 	    __func__, op, addr, send_len, recv_len);
    847 
    848 	cmd.cmd = SMU_CMD_I2C;
    849 	cmd.len = 9 + recv_len;
    850 	cmd.data[0] = iicbus->reg;
    851 	cmd.data[1] = I2C_OP_READ_P(op) ? 0x02 : 0x00;
    852 	cmd.data[2] = addr << 1;
    853 	cmd.data[3] = send_len;
    854 	memcpy(&cmd.data[4], send, send_len);
    855 	cmd.data[7] = addr << 1;
    856 	if (I2C_OP_READ_P(op))
    857 		cmd.data[7] |= 0x01;
    858 	cmd.data[8] = recv_len;
    859 	memcpy(&cmd.data[9], recv, recv_len);
    860 
    861 	ret = smu_do_cmd(sc, &cmd, 800);
    862 	if (ret != 0)
    863 		return (ret);
    864 
    865 	for (retries = 0; retries < 10; retries++) {
    866 		cmd.cmd = SMU_CMD_I2C;
    867 		cmd.len = 1;
    868 		cmd.data[0] = 0x00;
    869 		memset(&cmd.data[1], 0xff, recv_len);
    870 
    871 		ret = smu_do_cmd(sc, &cmd, 800);
    872 
    873 		DPRINTF("%s: cmd data[0] %x\n", __func__, cmd.data[0]);
    874 
    875 		if (ret == 0 && (cmd.data[0] & 0x80) == 0)
    876 			break;
    877 
    878 		DELAY(10000);
    879 	}
    880 
    881 	if (cmd.data[0] & 0x80)
    882 		return EIO;
    883 
    884 	if (I2C_OP_READ_P(op))
    885 		memcpy(recv, &cmd.data[1], recv_len);
    886 
    887 	return 0;
    888 }
    889 
    890 static int
    891 smu_sysctl_fan_rpm(SYSCTLFN_ARGS)
    892 {
    893 	struct sysctlnode node = *rnode;
    894 	struct smu_fan *fan = node.sysctl_data;
    895 	int rpm = 0;
    896 	int ret;
    897 
    898 	node.sysctl_data = &rpm;
    899 
    900 	if (newp) {
    901 		if (sysctl_lookup(SYSCTLFN_CALL(&node)) == 0) {
    902 			rpm = *(int *) node.sysctl_data;
    903 			return smu_fan_set_rpm(fan, rpm);
    904 		}
    905 		return EINVAL;
    906 	} else {
    907 		ret = smu_fan_get_rpm(fan, &rpm);
    908 		if (ret != 0)
    909 			return (ret);
    910 
    911 		return sysctl_lookup(SYSCTLFN_CALL(&node));
    912 	}
    913 
    914 	return 0;
    915 }
    916 
    917 SYSCTL_SETUP(smu_sysctl_setup, "SMU sysctl subtree setup")
    918 {
    919 	sysctl_createv(NULL, 0, NULL, NULL,
    920 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "machdep", NULL,
    921 	    NULL, 0, NULL, 0, CTL_MACHDEP, CTL_EOL);
    922 }
    923 
    924 static void
    925 smu_setup_zones(struct smu_softc *sc)
    926 {
    927 	struct smu_zone *z;
    928 	struct smu_fan *f;
    929 	int i;
    930 
    931 	/* find CPU fans */
    932 	z = &sc->sc_zones[SMU_ZONE_CPUS];
    933 	z->nfans = 0;
    934 	for (i = 0; i < SMU_MAX_FANS; i++) {
    935 		f = &sc->sc_fans[i];
    936 		if ((strstr(f->location, "CPU") != NULL) ||
    937 		    (strstr(f->location, "System") != NULL)) {
    938 			z->fans[z->nfans] = i;
    939 			z->nfans++;
    940 		}
    941 	}
    942 	aprint_normal_dev(sc->sc_dev,
    943 	    "using %d fans for CPU zone\n", z->nfans);
    944 	z->threshold = C_TO_uK(45);
    945 	z->duty = 150;
    946 	z->step = 3;
    947 	z->filter = is_cpu_sensor;
    948 
    949 	z = &sc->sc_zones[SMU_ZONE_DRIVES];
    950 	z->nfans = 0;
    951 	for (i = 0; i < SMU_MAX_FANS; i++) {
    952 		f = &sc->sc_fans[i];
    953 		if ((strstr(f->location, "DRIVE") != NULL) ||
    954 		    (strstr(f->location, "Drive") != NULL)) {
    955 			z->fans[z->nfans] = i;
    956 			z->nfans++;
    957 		}
    958 	}
    959 	aprint_normal_dev(sc->sc_dev,
    960 	    "using %d fans for drive bay zone\n", z->nfans);
    961 	z->threshold = C_TO_uK(40);
    962 	z->duty = 150;
    963 	z->step = 2;
    964 	z->filter = is_drive_sensor;
    965 
    966 	z = &sc->sc_zones[SMU_ZONE_SLOTS];
    967 	z->nfans = 0;
    968 	for (i = 0; i < SMU_MAX_FANS; i++) {
    969 		f = &sc->sc_fans[i];
    970 		if ((strstr(f->location, "BACKSIDE") != NULL) ||
    971 		    (strstr(f->location, "SLOTS") != NULL)) {
    972 			z->fans[z->nfans] = i;
    973 			z->nfans++;
    974 		}
    975 	}
    976 	aprint_normal_dev(sc->sc_dev,
    977 	    "using %d fans for expansion slots zone\n", z->nfans);
    978 	z->threshold = C_TO_uK(40);
    979 	z->duty = 150;
    980 	z->step = 2;
    981 	z->filter = is_slots_sensor;
    982 
    983 	sc->sc_dying = false;
    984 	kthread_create(PRI_NONE, 0, curcpu(), smu_adjust, sc, &sc->sc_thread,
    985 	    "fan control");
    986 }
    987 
    988 static void
    989 smu_adjust_zone(struct smu_softc *sc, int which)
    990 {
    991 	struct smu_zone *z = &sc->sc_zones[which];
    992 	struct smu_fan *f;
    993 	long temp, newduty, i, speed, diff;
    994 
    995 	DPRINTF("%s %d\n", __func__, which);
    996 
    997 	temp = sysmon_envsys_get_max_value(z->filter, true);
    998 	if (temp == 0) {
    999 		/* no sensor data - leave fan alone */
   1000 		DPRINTF("nodata\n");
   1001 		return;
   1002 	}
   1003 	DPRINTF("temp %ld ", (temp - 273150000) / 1000000);
   1004 	diff = ((temp - z->threshold) / 1000000) * z->step;
   1005 
   1006 	if (diff < 0) newduty = 0;
   1007 	else if (diff > 100) newduty = 100;
   1008 	else newduty = diff;
   1009 
   1010 	DPRINTF("newduty %ld diff %ld \n", newduty, diff);
   1011 	if (newduty == z->duty) {
   1012 		DPRINTF("no change\n");
   1013 		return;
   1014 	}
   1015 	z->duty = newduty;
   1016 	/* now adjust each fan to the new duty cycle */
   1017 	for (i = 0; i < z->nfans; i++) {
   1018 		f = &sc->sc_fans[z->fans[i]];
   1019 		speed = f->min_rpm + ((f->max_rpm - f->min_rpm) * newduty) / 100;
   1020 		DPRINTF("fan %d speed %ld ", z->fans[i], speed);
   1021 		smu_fan_set_rpm(f, speed);
   1022 	}
   1023 	DPRINTF("\n");
   1024 }
   1025 
   1026 static void
   1027 smu_adjust(void *cookie)
   1028 {
   1029 	struct smu_softc *sc = cookie;
   1030 	int i;
   1031 
   1032 	while (!sc->sc_dying) {
   1033 		for (i = 0; i < SMU_ZONES; i++)
   1034 			smu_adjust_zone(sc, i);
   1035 		kpause("fanctrl", true, mstohz(3000), NULL);
   1036 	}
   1037 	kthread_exit(0);
   1038 }
   1039 
   1040 static bool is_cpu_sensor(const envsys_data_t *edata)
   1041 {
   1042 	if (edata->units != ENVSYS_STEMP)
   1043 		return false;
   1044 	if (strstr(edata->desc, "CPU") != NULL)
   1045 		return TRUE;
   1046 	return false;
   1047 }
   1048 
   1049 static bool is_drive_sensor(const envsys_data_t *edata)
   1050 {
   1051 	if (edata->units != ENVSYS_STEMP)
   1052 		return false;
   1053 	if (strstr(edata->desc, "DRIVE") != NULL)
   1054 		return TRUE;
   1055 	if (strstr(edata->desc, "drive") != NULL)
   1056 		return TRUE;
   1057 	return false;
   1058 }
   1059 
   1060 static bool is_slots_sensor(const envsys_data_t *edata)
   1061 {
   1062 	if (edata->units != ENVSYS_STEMP)
   1063 		return false;
   1064 	if (strstr(edata->desc, "BACKSIDE") != NULL)
   1065 		return TRUE;
   1066 	if (strstr(edata->desc, "INLET") != NULL)
   1067 		return TRUE;
   1068 	if (strstr(edata->desc, "DIODE") != NULL)
   1069 		return TRUE;
   1070 	if (strstr(edata->desc, "TUNNEL") != NULL)
   1071 		return TRUE;
   1072 	return false;
   1073 }
   1074 
   1075 int
   1076 smu_get_datablock(int id, uint8_t *buf, size_t len)
   1077 {
   1078 	struct smu_cmd cmd;
   1079 
   1080 	cmd.cmd = SMU_PARTITION;
   1081 	cmd.len = 2;
   1082 	cmd.data[0] = SMU_PARTITION_LATEST;
   1083 	cmd.data[1] = id;
   1084 	smu_do_cmd(smu0, &cmd, 100);
   1085 
   1086 	cmd.data[4] = cmd.data[0];
   1087 	cmd.data[5] = cmd.data[1];
   1088 
   1089 	cmd.cmd = SMU_MISC;
   1090 	cmd.len = 7;
   1091 	cmd.data[0] = SMU_MISC_GET_DATA;
   1092 	cmd.data[1] = 4;
   1093 	cmd.data[2] = 0;
   1094 	cmd.data[3] = 0;
   1095 	cmd.data[6] = len;
   1096 	smu_do_cmd(smu0, &cmd, 100);
   1097 
   1098 	memcpy(buf, cmd.data, len);
   1099 	return 0;
   1100 }
   1101