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