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