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