xref: /netbsd-src/sys/arch/macppc/dev/smu.c (revision 8ecbf5f02b752fcb7debe1a8fab1dc82602bc760)
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
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 
124 	struct smu_zone sc_zones[SMU_ZONES];
125 	lwp_t *sc_thread;
126 	bool sc_dying;
127 };
128 
129 #define SMU_CMD_FAN	0x4a
130 #define SMU_CMD_RTC	0x8e
131 #define SMU_CMD_I2C	0x9a
132 #define SMU_CMD_POWER	0xaa
133 #define SMU_ADC		0xd8
134 #define SMU_MISC	0xee
135 #define  SMU_MISC_GET_DATA	0x02
136 #define  SMU_MISC_LED_CTRL	0x04
137 
138 #define SMU_CPUTEMP_CAL 0x18
139 #define SMU_CPUVOLT_CAL	0x21
140 #define SMU_SLOTPW_CAL	0x78
141 
142 #define SMU_PARTITION		0x3e
143 #define SMU_PARTITION_LATEST	0x01
144 #define SMU_PARTITION_BASE	0x02
145 #define SMU_PARTITION_UPDATE	0x03
146 
147 #ifdef SMU_DEBUG
148 #define DPRINTF printf
149 #else
150 #define DPRINTF while (0) printf
151 #endif
152 
153 static int smu_match(device_t, struct cfdata *, void *);
154 static void smu_attach(device_t, device_t, void *);
155 static int smu_setup_doorbell(struct smu_softc *);
156 static void smu_setup_fans(struct smu_softc *);
157 static void smu_setup_iicbus(struct smu_softc *);
158 static void smu_setup_sme(struct smu_softc *);
159 static int smu_iicbus_print(void *, const char *);
160 static void smu_sme_refresh(struct sysmon_envsys *, envsys_data_t *);
161 static int smu_do_cmd(struct smu_softc *, struct smu_cmd *, int);
162 static int smu_dbell_gpio_intr(void *);
163 static int smu_todr_gettime_ymdhms(todr_chip_handle_t, struct clock_ymdhms *);
164 static int smu_todr_settime_ymdhms(todr_chip_handle_t, struct clock_ymdhms *);
165 static int smu_fan_update_rpm(struct smu_fan *);
166 static int smu_fan_get_rpm(struct smu_fan *, int *);
167 static int smu_fan_set_rpm(struct smu_fan *, int);
168 static int smu_iicbus_exec(void *, i2c_op_t, i2c_addr_t, const void *,
169     size_t, void *, size_t, int);
170 static int smu_sysctl_fan_rpm(SYSCTLFN_ARGS);
171 
172 static void smu_setup_zones(struct smu_softc *);
173 static void smu_adjust_zone(struct smu_softc *, int);
174 static void smu_adjust(void *);
175 static bool is_cpu_sensor(const envsys_data_t *);
176 static bool is_drive_sensor(const envsys_data_t *);
177 static bool is_slots_sensor(const envsys_data_t *);
178 
179 int smu_get_datablock(int, uint8_t *, size_t);
180 
181 CFATTACH_DECL_NEW(smu, sizeof(struct smu_softc),
182     smu_match, smu_attach, NULL, NULL);
183 
184 static struct smu_softc *smu0 = NULL;
185 
186 static int
187 smu_match(device_t parent, struct cfdata *cf, void *aux)
188 {
189 	struct confargs *ca = aux;
190 
191 	if (strcmp(ca->ca_name, "smu") == 0)
192 		return 5;
193 
194 	return 0;
195 }
196 
197 static void
198 smu_attach(device_t parent, device_t self, void *aux)
199 {
200 	struct confargs *ca = aux;
201 	struct smu_softc *sc = device_private(self);
202 
203 	sc->sc_dev = self;
204 	sc->sc_node = ca->ca_node;
205 
206 	if (smu0 == NULL)
207 		smu0 = sc;
208 
209 	sysctl_createv(NULL, 0, NULL, (void *) &sc->sc_sysctl_me,
210 	    CTLFLAG_READWRITE,
211 	    CTLTYPE_NODE, device_xname(sc->sc_dev), NULL,
212 	    NULL, 0, NULL, 0,
213 	    CTL_MACHDEP, CTL_CREATE, CTL_EOL);
214 
215 	if (smu_setup_doorbell(sc) != 0) {
216 		aprint_normal(": unable to set up doorbell\n");
217 		return;
218 	}
219 
220 	aprint_normal("\n");
221 
222 	smu_setup_fans(sc);
223 	smu_setup_iicbus(sc);
224 
225 	sc->sc_todr.todr_gettime_ymdhms = smu_todr_gettime_ymdhms;
226 	sc->sc_todr.todr_settime_ymdhms = smu_todr_settime_ymdhms;
227 	sc->sc_todr.cookie = sc;
228 	todr_attach(&sc->sc_todr);
229 
230 	smu_setup_sme(sc);
231 
232 	smu_setup_zones(sc);
233 }
234 
235 static int
236 smu_setup_doorbell(struct smu_softc *sc)
237 {
238 	int node, parent, reg[4], gpio_base, irq;
239 
240 	mutex_init(&sc->sc_cmd_lock, MUTEX_DEFAULT, IPL_NONE);
241 	sc->sc_cmd = malloc(4096, M_DEVBUF, M_WAITOK);
242 	sc->sc_cmd_paddr = vtophys((vaddr_t) sc->sc_cmd);
243 
244 	DPRINTF("%s: cmd vaddr 0x%x paddr 0x%x\n",
245 	    __func__, (unsigned int) sc->sc_cmd,
246 	    (unsigned int) sc->sc_cmd_paddr);
247 
248 	if (OF_getprop(sc->sc_node, "platform-doorbell-buff",
249 	        &node, sizeof(node)) <= 0)
250 		return -1;
251 
252 	if (OF_getprop(node, "platform-do-doorbell-buff",
253 	        reg, sizeof(reg)) < sizeof(reg))
254 		return -1;
255 
256 	sc->sc_dbell_mbox = reg[3];
257 
258 	if (OF_getprop(sc->sc_node, "platform-doorbell-ack",
259 	        &node, sizeof(node)) <= 0)
260 		return -1;
261 
262 	parent = OF_parent(node);
263 	if (parent == 0)
264 		return -1;
265 
266 	if (OF_getprop(parent, "reg", &gpio_base, sizeof(gpio_base)) <= 0)
267 		return -1;
268 
269 	if (OF_getprop(node, "reg", reg, sizeof(reg)) <= 0)
270 		return -1;
271 
272 	if (OF_getprop(node, "interrupts", &irq, sizeof(irq)) <= 0)
273 		return -1;
274 
275 	sc->sc_dbell_gpio = gpio_base + reg[0];
276 
277 	aprint_normal(" mbox 0x%x gpio 0x%x irq %d",
278 	    sc->sc_dbell_mbox, sc->sc_dbell_gpio, irq);
279 
280 	intr_establish(irq, IST_EDGE_FALLING, IPL_TTY, smu_dbell_gpio_intr, sc);
281 
282 	return 0;
283 }
284 
285 static void
286 smu_setup_fans(struct smu_softc *sc)
287 {
288 	struct smu_fan *fan;
289 	struct sysctlnode *sysctl_fans, *sysctl_fan, *sysctl_node;
290 	char type[32], sysctl_fan_name[32];
291 	int node, i, j;
292 
293 	node = of_getnode_byname(sc->sc_node, "fans");
294 	for (node = OF_child(node);
295 	    (node != 0) && (sc->sc_num_fans < SMU_MAX_FANS);
296 	    node = OF_peer(node)) {
297 		fan = &sc->sc_fans[sc->sc_num_fans];
298 		fan->sc = sc;
299 
300 		memset(fan->location, 0, sizeof(fan->location));
301 		OF_getprop(node, "location", fan->location,
302 		    sizeof(fan->location));
303 
304 		if (OF_getprop(node, "reg", &fan->reg,
305 		        sizeof(fan->reg)) <= 0)
306 			continue;
307 
308 		if (OF_getprop(node, "zone", &fan->zone,
309 		        sizeof(fan->zone)) <= 0)
310 			continue;
311 
312 		memset(type, 0, sizeof(type));
313 		OF_getprop(node, "device_type", type, sizeof(type));
314 		if (strcmp(type, "fan-rpm-control") == 0)
315 			fan->rpm_ctl = 1;
316 		else
317 			fan->rpm_ctl = 0;
318 
319 		if (OF_getprop(node, "min-value", &fan->min_rpm,
320 		    sizeof(fan->min_rpm)) <= 0)
321 			fan->min_rpm = 0;
322 
323 		if (OF_getprop(node, "max-value", &fan->max_rpm,
324 		    sizeof(fan->max_rpm)) <= 0)
325 			fan->max_rpm = 0xffff;
326 
327 		if (OF_getprop(node, "unmanage-value", &fan->default_rpm,
328 		    sizeof(fan->default_rpm)) <= 0)
329 			fan->default_rpm = fan->max_rpm;
330 
331 		DPRINTF("fan: location %s reg %x zone %d rpm_ctl %d "
332 		    "min_rpm %d max_rpm %d default_rpm %d\n",
333 		    fan->location, fan->reg, fan->zone, fan->rpm_ctl,
334 		    fan->min_rpm, fan->max_rpm, fan->default_rpm);
335 
336 		sc->sc_num_fans++;
337 	}
338 
339 	for (i = 0; i < sc->sc_num_fans; i++) {
340 		fan = &sc->sc_fans[i];
341 		smu_fan_set_rpm(fan, fan->default_rpm);
342 		smu_fan_get_rpm(fan, &fan->current_rpm);
343 	}
344 
345 	/* Create sysctl nodes for each fan */
346 
347 	sysctl_createv(NULL, 0, NULL, (void *) &sysctl_fans,
348 	    CTLFLAG_READWRITE | CTLFLAG_OWNDESC,
349 	    CTLTYPE_NODE, "fans", NULL,
350 	    NULL, 0, NULL, 0,
351 	    CTL_MACHDEP,
352 	    sc->sc_sysctl_me->sysctl_num,
353 	    CTL_CREATE, CTL_EOL);
354 
355 	for (i = 0; i < sc->sc_num_fans; i++) {
356 		fan = &sc->sc_fans[i];
357 
358 		for (j = 0; j < strlen(fan->location); j++) {
359 			sysctl_fan_name[j] = tolower(fan->location[j]);
360 			if (sysctl_fan_name[j] == ' ')
361 				sysctl_fan_name[j] = '_';
362 		}
363 		sysctl_fan_name[j] = '\0';
364 
365 		sysctl_createv(NULL, 0, NULL, (void *) &sysctl_fan,
366 		    CTLFLAG_READWRITE | CTLFLAG_OWNDESC,
367 		    CTLTYPE_NODE, sysctl_fan_name, "fan information",
368 		    NULL, 0, NULL, 0,
369 		    CTL_MACHDEP,
370 		    sc->sc_sysctl_me->sysctl_num,
371 		    sysctl_fans->sysctl_num,
372 		    CTL_CREATE, CTL_EOL);
373 
374 		sysctl_createv(NULL, 0, NULL, (void *) &sysctl_node,
375 		    CTLFLAG_READONLY | CTLFLAG_OWNDESC,
376 		    CTLTYPE_INT, "zone", "fan zone",
377 		    NULL, 0, &fan->zone, 0,
378 		    CTL_MACHDEP,
379 		    sc->sc_sysctl_me->sysctl_num,
380 		    sysctl_fans->sysctl_num,
381 		    sysctl_fan->sysctl_num,
382 		    CTL_CREATE, CTL_EOL);
383 
384 		sysctl_createv(NULL, 0, NULL, (void *) &sysctl_node,
385 		    CTLFLAG_READONLY | CTLFLAG_OWNDESC,
386 		    CTLTYPE_INT, "min_rpm", "fan minimum rpm",
387 		    NULL, 0, &fan->min_rpm, 0,
388 		    CTL_MACHDEP,
389 		    sc->sc_sysctl_me->sysctl_num,
390 		    sysctl_fans->sysctl_num,
391 		    sysctl_fan->sysctl_num,
392 		    CTL_CREATE, CTL_EOL);
393 
394 		sysctl_createv(NULL, 0, NULL, (void *) &sysctl_node,
395 		    CTLFLAG_READONLY | CTLFLAG_OWNDESC,
396 		    CTLTYPE_INT, "max_rpm", "fan maximum rpm",
397 		    NULL, 0, &fan->max_rpm, 0,
398 		    CTL_MACHDEP,
399 		    sc->sc_sysctl_me->sysctl_num,
400 		    sysctl_fans->sysctl_num,
401 		    sysctl_fan->sysctl_num,
402 		    CTL_CREATE, CTL_EOL);
403 
404 		sysctl_createv(NULL, 0, NULL, (void *) &sysctl_node,
405 		    CTLFLAG_READONLY | CTLFLAG_OWNDESC,
406 		    CTLTYPE_INT, "default_rpm", "fan default rpm",
407 		    NULL, 0, &fan->default_rpm, 0,
408 		    CTL_MACHDEP,
409 		    sc->sc_sysctl_me->sysctl_num,
410 		    sysctl_fans->sysctl_num,
411 		    sysctl_fan->sysctl_num,
412 		    CTL_CREATE, CTL_EOL);
413 
414 		sysctl_createv(NULL, 0, NULL, (void *) &sysctl_node,
415 		    CTLFLAG_READWRITE | CTLFLAG_OWNDESC,
416 		    CTLTYPE_INT, "rpm", "fan current rpm",
417 		    smu_sysctl_fan_rpm, 0, (void *) fan, 0,
418 		    CTL_MACHDEP,
419 		    sc->sc_sysctl_me->sysctl_num,
420 		    sysctl_fans->sysctl_num,
421 		    sysctl_fan->sysctl_num,
422 		    CTL_CREATE, CTL_EOL);
423 	}
424 }
425 
426 static void
427 smu_setup_iicbus(struct smu_softc *sc)
428 {
429 	struct smu_iicbus *iicbus;
430 	struct i2c_controller *i2c;
431 	struct smu_iicbus_confargs ca;
432 	int node;
433 	char name[32];
434 
435 	node = of_getnode_byname(sc->sc_node, "smu-i2c-control");
436 	for (node = OF_child(node);
437 	    (node != 0) && (sc->sc_num_iicbus < SMU_MAX_IICBUS);
438 	    node = OF_peer(node)) {
439 		memset(name, 0, sizeof(name));
440 		OF_getprop(node, "name", name, sizeof(name));
441 		if (strcmp(name, "i2c-bus") != 0)
442 			continue;
443 
444 		iicbus = &sc->sc_iicbus[sc->sc_num_iicbus];
445 		iicbus->sc = sc;
446 		i2c = &iicbus->i2c;
447 
448 		if (OF_getprop(node, "reg", &iicbus->reg, sizeof(iicbus->reg)) <= 0)
449 			continue;
450 
451 		DPRINTF("iicbus: reg %x\n", iicbus->reg);
452 
453 		iic_tag_init(i2c);
454 		i2c->ic_cookie = iicbus;
455 		i2c->ic_exec = smu_iicbus_exec;
456 
457 		ca.ca_name = name;
458 		ca.ca_node = node;
459 		ca.ca_tag = i2c;
460 		config_found_ia(sc->sc_dev, "smu", &ca, smu_iicbus_print);
461 
462 		sc->sc_num_iicbus++;
463 	}
464 }
465 
466 static void
467 smu_setup_sme(struct smu_softc *sc)
468 {
469 	struct smu_fan *fan;
470 	envsys_data_t *sme_sensor;
471 	int i;
472 
473 	sc->sc_sme = sysmon_envsys_create();
474 
475 	for (i = 0; i < sc->sc_num_fans; i++) {
476 		sme_sensor = &sc->sc_sme_sensors[i];
477 		fan = &sc->sc_fans[i];
478 
479 		sme_sensor->units = ENVSYS_SFANRPM;
480 		sme_sensor->state = ENVSYS_SINVALID;
481 		snprintf(sme_sensor->desc, sizeof(sme_sensor->desc),
482 		    "%s", fan->location);
483 
484 		if (sysmon_envsys_sensor_attach(sc->sc_sme, sme_sensor)) {
485 			sysmon_envsys_destroy(sc->sc_sme);
486 			return;
487 		}
488 	}
489 
490 	sc->sc_sme->sme_name = device_xname(sc->sc_dev);
491 	sc->sc_sme->sme_cookie = sc;
492 	sc->sc_sme->sme_refresh = smu_sme_refresh;
493 
494 	if (sysmon_envsys_register(sc->sc_sme)) {
495 		aprint_error_dev(sc->sc_dev,
496 		    "unable to register with sysmon\n");
497 		sysmon_envsys_destroy(sc->sc_sme);
498 	}
499 }
500 
501 static int
502 smu_iicbus_print(void *aux, const char *smu)
503 {
504 	struct smu_iicbus_confargs *ca = aux;
505 
506 	if (smu)
507 		aprint_normal("%s at %s", ca->ca_name, smu);
508 
509 	return UNCONF;
510 }
511 
512 static void
513 smu_sme_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
514 {
515 	struct smu_softc *sc = sme->sme_cookie;
516 	struct smu_fan *fan;
517 	int which = edata->sensor;
518 	int ret;
519 
520 	edata->state = ENVSYS_SINVALID;
521 
522 	if (which < sc->sc_num_fans) {
523 		fan = &sc->sc_fans[which];
524 
525 		ret = smu_fan_get_rpm(fan, &fan->current_rpm);
526 		if (ret == 0) {
527 			edata->value_cur = fan->current_rpm;
528 			edata->state = ENVSYS_SVALID;
529 		}
530 	}
531 }
532 
533 static int
534 smu_do_cmd(struct smu_softc *sc, struct smu_cmd *cmd, int timo)
535 {
536 	int gpio, ret, bail;
537 	u_char ack;
538 
539 	mutex_enter(&sc->sc_cmd_lock);
540 
541 	DPRINTF("%s: cmd %02x len %02x\n", __func__, cmd->cmd, cmd->len);
542 	DPRINTF("%s: data %02x %02x %02x %02x %02x %02x %02x %02x\n", __func__,
543 	    cmd->data[0], cmd->data[1], cmd->data[2], cmd->data[3],
544 	    cmd->data[4], cmd->data[5], cmd->data[6], cmd->data[7]);
545 
546 	sc->sc_cmd->cmd = cmd->cmd;
547 	sc->sc_cmd->len = cmd->len;
548 	memcpy(sc->sc_cmd->data, cmd->data, cmd->len);
549 
550 	__asm volatile ("dcbf 0,%0; sync" :: "r"(sc->sc_cmd) : "memory");
551 
552 	obio_write_4(sc->sc_dbell_mbox, sc->sc_cmd_paddr);
553 	obio_write_1(sc->sc_dbell_gpio, 0x04);
554 
555 	bail = 0;
556 
557 	gpio = obio_read_1(sc->sc_dbell_gpio);
558 
559 	while (((gpio & 0x07) != 0x07) && (bail < timo)) {
560 		ret = tsleep(sc->sc_cmd, PWAIT, "smu_cmd", mstohz(10));
561 		if (ret != 0) {
562 			bail++;
563 		}
564 		gpio = obio_read_1(sc->sc_dbell_gpio);
565 	}
566 
567 	if ((gpio & 0x07) != 0x07) {
568 		mutex_exit(&sc->sc_cmd_lock);
569 		return EWOULDBLOCK;
570 	}
571 
572 	__asm volatile ("dcbf 0,%0; sync" :: "r"(sc->sc_cmd) : "memory");
573 
574 	ack = (~cmd->cmd) & 0xff;
575 	if (sc->sc_cmd->cmd != ack) {
576 		DPRINTF("%s: invalid ack, got %x expected %x\n",
577 		    __func__, sc->sc_cmd->cmd, ack);
578 		mutex_exit(&sc->sc_cmd_lock);
579 		return EIO;
580 	}
581 
582 	cmd->cmd = sc->sc_cmd->cmd;
583 	cmd->len = sc->sc_cmd->len;
584 	memcpy(cmd->data, sc->sc_cmd->data, sc->sc_cmd->len);
585 
586 	mutex_exit(&sc->sc_cmd_lock);
587 
588 	return 0;
589 }
590 
591 
592 static int
593 smu_dbell_gpio_intr(void *arg)
594 {
595 	struct smu_softc *sc = arg;
596 
597 	DPRINTF("%s\n", __func__);
598 
599 	wakeup(sc->sc_cmd);
600 
601 	return 1;
602 }
603 
604 void
605 smu_poweroff(void)
606 {
607 	struct smu_cmd cmd;
608 
609 	if (smu0 == NULL)
610 		return;
611 
612 	cmd.cmd = SMU_CMD_POWER;
613 	strcpy(cmd.data, "SHUTDOWN");
614 	cmd.len = strlen(cmd.data) + 1;
615 	smu_do_cmd(smu0, &cmd, 800);
616 
617 	for (;;);
618 }
619 
620 void
621 smu_restart(void)
622 {
623 	struct smu_cmd cmd;
624 
625 	if (smu0 == NULL)
626 		return;
627 
628 	cmd.cmd = SMU_CMD_POWER;
629 	strcpy(cmd.data, "RESTART");
630 	cmd.len = strlen(cmd.data) + 1;
631 	smu_do_cmd(smu0, &cmd, 800);
632 
633 	for (;;);
634 }
635 
636 static int
637 smu_todr_gettime_ymdhms(todr_chip_handle_t tch, struct clock_ymdhms *dt)
638 {
639 	struct smu_softc *sc = tch->cookie;
640 	struct smu_cmd cmd;
641 	int ret;
642 
643 	cmd.cmd = SMU_CMD_RTC;
644 	cmd.len = 1;
645 	cmd.data[0] = 0x81;
646 
647 	ret = smu_do_cmd(sc, &cmd, 800);
648 	if (ret != 0)
649 		return ret;
650 
651 	dt->dt_sec = bcdtobin(cmd.data[0]);
652 	dt->dt_min = bcdtobin(cmd.data[1]);
653 	dt->dt_hour = bcdtobin(cmd.data[2]);
654 	dt->dt_wday = bcdtobin(cmd.data[3]);
655 	dt->dt_day = bcdtobin(cmd.data[4]);
656 	dt->dt_mon = bcdtobin(cmd.data[5]);
657 	dt->dt_year = bcdtobin(cmd.data[6]) + 2000;
658 
659 	return 0;
660 }
661 
662 static int
663 smu_todr_settime_ymdhms(todr_chip_handle_t tch, struct clock_ymdhms *dt)
664 {
665 	struct smu_softc *sc = tch->cookie;
666 	struct smu_cmd cmd;
667 
668 	cmd.cmd = SMU_CMD_RTC;
669 	cmd.len = 8;
670 	cmd.data[0] = 0x80;
671 	cmd.data[1] = bintobcd(dt->dt_sec);
672 	cmd.data[2] = bintobcd(dt->dt_min);
673 	cmd.data[3] = bintobcd(dt->dt_hour);
674 	cmd.data[4] = bintobcd(dt->dt_wday);
675 	cmd.data[5] = bintobcd(dt->dt_day);
676 	cmd.data[6] = bintobcd(dt->dt_mon);
677 	cmd.data[7] = bintobcd(dt->dt_year - 2000);
678 
679 	return smu_do_cmd(sc, &cmd, 800);
680 }
681 
682 static int
683 smu_fan_update_rpm(struct smu_fan *fan)
684 {
685 	struct smu_softc *sc = fan->sc;
686 	struct smu_cmd cmd;
687 	int ret;
688 
689 	cmd.cmd = SMU_CMD_FAN;
690 	cmd.len = 2;
691 	cmd.data[0] = 0x31;
692 	cmd.data[1] = fan->reg;
693 
694 	ret = smu_do_cmd(sc, &cmd, 800);
695 	if (ret == 0) {
696 		fan->last_update = time_uptime;
697 		fan->current_rpm = (cmd.data[0] << 8) | cmd.data[1];
698 	} else {
699 		cmd.cmd = SMU_CMD_FAN;
700 		cmd.len = 1;
701 		cmd.data[0] = 0x01;
702 
703 		ret = smu_do_cmd(sc, &cmd, 800);
704 		if (ret == 0) {
705 			fan->last_update = time_uptime;
706 			fan->current_rpm = (cmd.data[1 + fan->reg * 2] << 8) |
707 			    cmd.data[2 + fan->reg * 2];
708 		}
709 	}
710 
711 	return ret;
712 }
713 
714 static int
715 smu_fan_get_rpm(struct smu_fan *fan, int *rpm)
716 {
717 	int ret;
718 	ret = 0;
719 
720 	if (time_uptime - fan->last_update > 1) {
721 		ret = smu_fan_update_rpm(fan);
722 		if (ret != 0)
723 			return ret;
724 	}
725 
726 	*rpm = fan->current_rpm;
727 
728 	return ret;
729 }
730 
731 static int
732 smu_fan_set_rpm(struct smu_fan *fan, int rpm)
733 {
734 	struct smu_softc *sc = fan->sc;
735 	struct smu_cmd cmd;
736 	int ret;
737 
738 	DPRINTF("%s: fan %s rpm %d\n", __func__, fan->location, rpm);
739 
740 	rpm = uimax(fan->min_rpm, rpm);
741 	rpm = uimin(fan->max_rpm, rpm);
742 
743 	cmd.cmd = SMU_CMD_FAN;
744 	cmd.len = 4;
745 	cmd.data[0] = 0x30;
746 	cmd.data[1] = fan->reg;
747 	cmd.data[2] = (rpm >> 8) & 0xff;
748 	cmd.data[3] = rpm & 0xff;
749 
750 	ret = smu_do_cmd(sc, &cmd, 800);
751 	if (ret != 0) {
752 		cmd.cmd = SMU_CMD_FAN;
753 		cmd.len = 14;
754 		cmd.data[0] = fan->rpm_ctl ? 0x00 : 0x10;
755 		cmd.data[1] = 1 << fan->reg;
756 		cmd.data[2] = cmd.data[2 + fan->reg * 2] = (rpm >> 8) & 0xff;
757 		cmd.data[3] = cmd.data[3 + fan->reg * 2] = rpm & 0xff;
758 
759 		ret = smu_do_cmd(sc, &cmd, 800);
760 	}
761 
762 	return ret;
763 }
764 
765 static int
766 smu_iicbus_exec(void *cookie, i2c_op_t op, i2c_addr_t addr, const void *send,
767     size_t send_len, void *recv, size_t recv_len, int flags)
768 {
769 	struct smu_iicbus *iicbus = cookie;
770 	struct smu_softc *sc = iicbus->sc;
771 	struct smu_cmd cmd;
772 	int retries, ret;
773 
774 	DPRINTF("%s: op %x addr %x send_len %d recv_len %d\n",
775 	    __func__, op, addr, send_len, recv_len);
776 
777 	cmd.cmd = SMU_CMD_I2C;
778 	cmd.len = 9 + recv_len;
779 	cmd.data[0] = iicbus->reg;
780 	cmd.data[1] = I2C_OP_READ_P(op) ? 0x02 : 0x00;
781 	cmd.data[2] = addr << 1;
782 	cmd.data[3] = send_len;
783 	memcpy(&cmd.data[4], send, send_len);
784 	cmd.data[7] = addr << 1;
785 	if (I2C_OP_READ_P(op))
786 		cmd.data[7] |= 0x01;
787 	cmd.data[8] = recv_len;
788 	memcpy(&cmd.data[9], recv, recv_len);
789 
790 	ret = smu_do_cmd(sc, &cmd, 800);
791 	if (ret != 0)
792 		return (ret);
793 
794 	for (retries = 0; retries < 10; retries++) {
795 		cmd.cmd = SMU_CMD_I2C;
796 		cmd.len = 1;
797 		cmd.data[0] = 0x00;
798 		memset(&cmd.data[1], 0xff, recv_len);
799 
800 		ret = smu_do_cmd(sc, &cmd, 800);
801 
802 		DPRINTF("%s: cmd data[0] %x\n", __func__, cmd.data[0]);
803 
804 		if (ret == 0 && (cmd.data[0] & 0x80) == 0)
805 			break;
806 
807 		DELAY(10000);
808 	}
809 
810 	if (cmd.data[0] & 0x80)
811 		return EIO;
812 
813 	if (I2C_OP_READ_P(op))
814 		memcpy(recv, &cmd.data[1], recv_len);
815 
816 	return 0;
817 }
818 
819 static int
820 smu_sysctl_fan_rpm(SYSCTLFN_ARGS)
821 {
822 	struct sysctlnode node = *rnode;
823 	struct smu_fan *fan = node.sysctl_data;
824 	int rpm = 0;
825 	int ret;
826 
827 	node.sysctl_data = &rpm;
828 
829 	if (newp) {
830 		if (sysctl_lookup(SYSCTLFN_CALL(&node)) == 0) {
831 			rpm = *(int *) node.sysctl_data;
832 			return smu_fan_set_rpm(fan, rpm);
833 		}
834 		return EINVAL;
835 	} else {
836 		ret = smu_fan_get_rpm(fan, &rpm);
837 		if (ret != 0)
838 			return (ret);
839 
840 		return sysctl_lookup(SYSCTLFN_CALL(&node));
841 	}
842 
843 	return 0;
844 }
845 
846 SYSCTL_SETUP(smu_sysctl_setup, "SMU sysctl subtree setup")
847 {
848 	sysctl_createv(NULL, 0, NULL, NULL,
849 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "machdep", NULL,
850 	    NULL, 0, NULL, 0, CTL_MACHDEP, CTL_EOL);
851 }
852 
853 static void
854 smu_setup_zones(struct smu_softc *sc)
855 {
856 	struct smu_zone *z;
857 	struct smu_fan *f;
858 	int i;
859 
860 	/* find CPU fans */
861 	z = &sc->sc_zones[SMU_ZONE_CPUS];
862 	z->nfans = 0;
863 	for (i = 0; i < SMU_MAX_FANS; i++) {
864 		f = &sc->sc_fans[i];
865 		if (strstr(f->location, "CPU") != NULL) {
866 			z->fans[z->nfans] = i;
867 			z->nfans++;
868 		}
869 	}
870 	aprint_normal_dev(sc->sc_dev,
871 	    "using %d fans for CPU zone\n", z->nfans);
872 	z->threshold = C_TO_uK(45);
873 	z->duty = 150;
874 	z->step = 3;
875 	z->filter = is_cpu_sensor;
876 
877 	z = &sc->sc_zones[SMU_ZONE_DRIVES];
878 	z->nfans = 0;
879 	for (i = 0; i < SMU_MAX_FANS; i++) {
880 		f = &sc->sc_fans[i];
881 		if (strstr(f->location, "DRIVE") != NULL) {
882 			z->fans[z->nfans] = i;
883 			z->nfans++;
884 		}
885 	}
886 	aprint_normal_dev(sc->sc_dev,
887 	    "using %d fans for drive bay zone\n", z->nfans);
888 	z->threshold = C_TO_uK(40);
889 	z->duty = 150;
890 	z->step = 2;
891 	z->filter = is_drive_sensor;
892 
893 	z = &sc->sc_zones[SMU_ZONE_SLOTS];
894 	z->nfans = 0;
895 	for (i = 0; i < SMU_MAX_FANS; i++) {
896 		f = &sc->sc_fans[i];
897 		if ((strstr(f->location, "BACKSIDE") != NULL) ||
898 		    (strstr(f->location, "SLOTS") != NULL)) {
899 			z->fans[z->nfans] = i;
900 			z->nfans++;
901 		}
902 	}
903 	aprint_normal_dev(sc->sc_dev,
904 	    "using %d fans for expansion slots zone\n", z->nfans);
905 	z->threshold = C_TO_uK(40);
906 	z->duty = 150;
907 	z->step = 2;
908 	z->filter = is_slots_sensor;
909 
910 	sc->sc_dying = false;
911 	kthread_create(PRI_NONE, 0, curcpu(), smu_adjust, sc, &sc->sc_thread,
912 	    "fan control");
913 }
914 
915 static void
916 smu_adjust_zone(struct smu_softc *sc, int which)
917 {
918 	struct smu_zone *z = &sc->sc_zones[which];
919 	struct smu_fan *f;
920 	long temp, newduty, i, speed, diff;
921 
922 	DPRINTF("%s %d\n", __func__, which);
923 
924 	temp = sysmon_envsys_get_max_value(z->filter, true);
925 	if (temp == 0) {
926 		/* no sensor data - leave fan alone */
927 		DPRINTF("nodata\n");
928 		return;
929 	}
930 	DPRINTF("temp %ld ", (temp - 273150000) / 1000000);
931 	diff = ((temp - z->threshold) / 1000000) * z->step;
932 
933 	if (diff < 0) newduty = 0;
934 	else if (diff > 100) newduty = 100;
935 	else newduty = diff;
936 
937 	DPRINTF("newduty %ld diff %ld \n", newduty, diff);
938 	if (newduty == z->duty) {
939 		DPRINTF("no change\n");
940 		return;
941 	}
942 	z->duty = newduty;
943 	/* now adjust each fan to the new duty cycle */
944 	for (i = 0; i < z->nfans; i++) {
945 		f = &sc->sc_fans[z->fans[i]];
946 		speed = f->min_rpm + ((f->max_rpm - f->min_rpm) * newduty) / 100;
947 		DPRINTF("fan %d speed %ld ", z->fans[i], speed);
948 		smu_fan_set_rpm(f, speed);
949 	}
950 	DPRINTF("\n");
951 }
952 
953 static void
954 smu_adjust(void *cookie)
955 {
956 	struct smu_softc *sc = cookie;
957 	int i;
958 
959 	while (!sc->sc_dying) {
960 		for (i = 0; i < SMU_ZONES; i++)
961 			smu_adjust_zone(sc, i);
962 		kpause("fanctrl", true, mstohz(30000), NULL);
963 	}
964 	kthread_exit(0);
965 }
966 
967 static bool is_cpu_sensor(const envsys_data_t *edata)
968 {
969 	if (edata->units != ENVSYS_STEMP)
970 		return false;
971 	if ((strstr(edata->desc, "CPU") != NULL) &&
972 	    (strstr(edata->desc, "DIODE") != NULL))
973 		return TRUE;
974 	if (strstr(edata->desc, "TUNNEL") != NULL)
975 		return TRUE;
976 	return false;
977 }
978 
979 static bool is_drive_sensor(const envsys_data_t *edata)
980 {
981 	if (edata->units != ENVSYS_STEMP)
982 		return false;
983 	if (strstr(edata->desc, "DRIVE BAY") != NULL)
984 		return TRUE;
985 	return false;
986 }
987 
988 static bool is_slots_sensor(const envsys_data_t *edata)
989 {
990 	if (edata->units != ENVSYS_STEMP)
991 		return false;
992 	if (strstr(edata->desc, "BACKSIDE") != NULL)
993 		return TRUE;
994 	if (strstr(edata->desc, "INLET") != NULL)
995 		return TRUE;
996 	return false;
997 }
998 
999 int
1000 smu_get_datablock(int id, uint8_t *buf, size_t len)
1001 {
1002 	struct smu_cmd cmd;
1003 
1004 	cmd.cmd = SMU_PARTITION;
1005 	cmd.len = 2;
1006 	cmd.data[0] = SMU_PARTITION_LATEST;
1007 	cmd.data[1] = id;
1008 	smu_do_cmd(smu0, &cmd, 100);
1009 
1010 	cmd.data[4] = cmd.data[0];
1011 	cmd.data[5] = cmd.data[1];
1012 
1013 	cmd.cmd = SMU_MISC;
1014 	cmd.len = 7;
1015 	cmd.data[0] = SMU_MISC_GET_DATA;
1016 	cmd.data[1] = 4;
1017 	cmd.data[2] = 0;
1018 	cmd.data[3] = 0;
1019 	cmd.data[6] = len;
1020 	smu_do_cmd(smu0, &cmd, 100);
1021 
1022 	memcpy(buf, cmd.data, len);
1023 	return 0;
1024 }
1025