Lines Matching +full:imx +full:- +full:sc +full:- +full:thermal

1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
34 * kitchen-sinked this device, not us. :)
46 * source describing i.MX6 SoCs, and in the linux and u-boot code which comes
52 * are deci-Celsius, which are converted to/from deci-Kelvins in the sysctl
74 #include <arm/freescale/imx/imx_ccmvar.h>
75 #include <arm/freescale/imx/imx_machdep.h>
76 #include <arm/freescale/imx/imx6_anatopreg.h>
77 #include <arm/freescale/imx/imx6_anatopvar.h>
81 { -1, 0 }
136 * value (0-3) from the ocotp CFG3 register into a mhz value that can be looked
141 #define TZ_ZEROC 2731 /* deci-Kelvin <-> deci-Celsius offset. */
147 KASSERT(imx6_anatop_sc != NULL, ("imx6_anatop_read_4 sc NULL"));
149 return (bus_read_4(imx6_anatop_sc->res[MEMRES], offset));
156 KASSERT(imx6_anatop_sc != NULL, ("imx6_anatop_write_4 sc NULL"));
158 bus_write_4(imx6_anatop_sc->res[MEMRES], offset, value);
162 vdd_set(struct imx6_anatop_softc *sc, int mv)
184 newtarg = (mv - 700) / 25;
190 * (1150-700/25=18).
199 * going from 400->1200, but works for smaller changes).
205 delay = (newtarg - oldtarg) * 6;
226 sc->cpu_curmv = newtarg * 25 + 700;
230 cpufreq_mhz_from_div(struct imx6_anatop_softc *sc, uint32_t corediv,
234 return ((sc->refosc_mhz * (plldiv / 2)) / (corediv + 1));
238 cpufreq_mhz_to_div(struct imx6_anatop_softc *sc, uint32_t cpu_mhz,
243 *plldiv = ((*corediv + 1) * cpu_mhz) / (sc->refosc_mhz / 2);
247 cpufreq_actual_mhz(struct imx6_anatop_softc *sc, uint32_t cpu_mhz)
251 cpufreq_mhz_to_div(sc, cpu_mhz, &corediv, &plldiv);
252 return (cpufreq_mhz_from_div(sc, corediv, plldiv));
256 cpufreq_nearest_oppt(struct imx6_anatop_softc *sc, uint32_t cpu_newmhz)
260 if (cpu_newmhz > sc->cpu_maxmhz_hw && !sc->cpu_overclock_enable)
261 cpu_newmhz = sc->cpu_maxmhz_hw;
266 d = abs((int)cpu_newmhz - (int)imx6_oppt_table[i].mhz);
276 cpufreq_set_clock(struct imx6_anatop_softc * sc, struct oppt *op)
281 if (op->mhz > sc->cpu_curmhz) {
282 vdd_set(sc, op->mv);
288 * - Set the bypass clock source to REF_CLK_24M (source #0).
289 * - Set the PLL into bypass mode; cpu should now be running at 24mhz.
290 * - Change the divisor.
291 * - Wait for the LOCK bit to come on; it takes ~50 loop iterations.
292 * - Turn off bypass mode; cpu should now be running at the new speed.
294 cpufreq_mhz_to_div(sc, op->mhz, &corediv, &plldiv);
308 if (--timeout == 0)
316 if (op->mhz < sc->cpu_curmhz)
317 vdd_set(sc, op->mv);
318 sc->cpu_curmhz = op->mhz;
322 cpufreq_actual_mhz(sc, sc->cpu_curmhz) * 1000000 / 2);
328 struct imx6_anatop_softc *sc;
333 sc = arg1;
335 temp = sc->cpu_minmhz;
337 if (err != 0 || req->newptr == NULL)
340 op = cpufreq_nearest_oppt(sc, temp);
341 if (op->mhz > sc->cpu_maxmhz)
343 else if (op->mhz == sc->cpu_minmhz)
350 sc->cpu_minmhz = op->mhz;
351 if (sc->cpu_minmhz > sc->cpu_curmhz) {
352 cpufreq_set_clock(sc, op);
360 struct imx6_anatop_softc *sc;
365 sc = arg1;
367 temp = sc->cpu_maxmhz;
369 if (err != 0 || req->newptr == NULL)
372 op = cpufreq_nearest_oppt(sc, temp);
373 if (op->mhz < sc->cpu_minmhz)
375 else if (op->mhz == sc->cpu_maxmhz)
384 sc->cpu_maxmhz = op->mhz;
385 cpufreq_set_clock(sc, op);
391 cpufreq_initialize(struct imx6_anatop_softc *sc)
397 OID_AUTO, "cpu_mhz", CTLFLAG_RD, &sc->cpu_curmhz, 0,
403 sc, 0, cpufreq_sysctl_minmhz, "IU", "Minimum CPU frequency");
408 sc, 0, cpufreq_sysctl_maxmhz, "IU", "Maximum CPU frequency");
411 OID_AUTO, "cpu_maxmhz_hw", CTLFLAG_RD, &sc->cpu_maxmhz_hw, 0,
416 &sc->cpu_overclock_enable, 0,
420 * XXX 24mhz shouldn't be hard-coded, should get this from imx6_ccm
424 sc->refosc_mhz = 24;
430 * - 2b'11: 1200000000Hz;
431 * - 2b'10: 996000000Hz;
432 * - 2b'01: 852000000Hz; -- i.MX6Q Only, exclusive with 996MHz.
433 * - 2b'00: 792000000Hz;
438 sc->cpu_maxmhz_hw = imx6_ocotp_mhz_tab[cfg3speed];
439 sc->cpu_maxmhz = sc->cpu_maxmhz_hw;
441 TUNABLE_INT_FETCH("hw.imx6.cpu_minmhz", &sc->cpu_minmhz);
442 op = cpufreq_nearest_oppt(sc, sc->cpu_minmhz);
443 sc->cpu_minmhz = op->mhz;
444 sc->cpu_minmv = op->mv;
446 TUNABLE_INT_FETCH("hw.imx6.cpu_maxmhz", &sc->cpu_maxmhz);
447 op = cpufreq_nearest_oppt(sc, sc->cpu_maxmhz);
448 sc->cpu_maxmhz = op->mhz;
449 sc->cpu_maxmv = op->mv;
454 * We won't have thermal throttling until interrupts are enabled, but we
459 cpufreq_set_clock(sc, op);
463 temp_from_count(struct imx6_anatop_softc *sc, uint32_t count)
466 return (((sc->temp_high_val - (count - sc->temp_high_cnt) *
467 (sc->temp_high_val - 250) /
468 (sc->temp_room_cnt - sc->temp_high_cnt))));
472 temp_to_count(struct imx6_anatop_softc *sc, uint32_t temp)
475 return ((sc->temp_room_cnt - sc->temp_high_cnt) *
476 (sc->temp_high_val - temp) / (sc->temp_high_val - 250) +
477 sc->temp_high_cnt);
481 temp_update_count(struct imx6_anatop_softc *sc)
488 sc->temp_last_cnt =
496 struct imx6_anatop_softc *sc = arg1;
499 temp_update_count(sc);
501 t = temp_from_count(sc, sc->temp_last_cnt) + TZ_ZEROC;
509 struct imx6_anatop_softc *sc = arg1;
513 temp = sc->temp_throttle_val + TZ_ZEROC;
517 temp -= TZ_ZEROC;
518 if (err != 0 || req->newptr == NULL || temp == sc->temp_throttle_val)
522 sc->temp_throttle_val = temp;
523 sc->temp_throttle_trigger_cnt = temp_to_count(sc, sc->temp_throttle_val);
524 sc->temp_throttle_reset_cnt = temp_to_count(sc, sc->temp_throttle_val - 100);
528 (sc->temp_throttle_trigger_cnt <<
534 tempmon_gofast(struct imx6_anatop_softc *sc)
537 if (sc->cpu_curmhz < sc->cpu_maxmhz) {
538 cpufreq_set_clock(sc, cpufreq_nearest_oppt(sc, sc->cpu_maxmhz));
543 tempmon_goslow(struct imx6_anatop_softc *sc)
546 if (sc->cpu_curmhz > sc->cpu_minmhz) {
547 cpufreq_set_clock(sc, cpufreq_nearest_oppt(sc, sc->cpu_minmhz));
554 struct imx6_anatop_softc *sc = arg;
561 tempmon_goslow(sc);
569 struct imx6_anatop_softc *sc = arg;
572 if (sc->temp_last_cnt < sc->temp_throttle_trigger_cnt)
573 tempmon_goslow(sc);
574 else if (sc->temp_last_cnt > (sc->temp_throttle_reset_cnt))
575 tempmon_gofast(sc);
577 callout_reset_sbt(&sc->temp_throttle_callout, sc->temp_throttle_delay,
578 0, tempmon_throttle_check, sc, 0);
583 initialize_tempmon(struct imx6_anatop_softc *sc)
592 sc->temp_room_cnt = (cal & 0xFFF00000) >> 20;
593 sc->temp_high_cnt = (cal & 0x000FFF00) >> 8;
594 sc->temp_high_val = (cal & 0x000000FF) * 10;
600 sc->temp_throttle_val = sc->temp_high_val - 100;
601 sc->temp_throttle_trigger_cnt =
602 temp_to_count(sc, sc->temp_throttle_val);
603 sc->temp_throttle_reset_cnt =
604 temp_to_count(sc, sc->temp_throttle_val - 50);
612 (sc->temp_throttle_trigger_cnt <<
617 * XXX Note that the alarm-interrupt feature isn't working yet, so
622 while (sc->temp_last_cnt == 0)
623 temp_update_count(sc);
624 sc->temp_throttle_delay = 100 * SBT_1MS;
625 callout_init(&sc->temp_throttle_callout, 0);
626 callout_reset_sbt(&sc->temp_throttle_callout, sc->temp_throttle_delay,
627 0, tempmon_throttle_check, sc, 0);
631 CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_NEEDGIANT, sc, 0,
635 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, sc,
644 struct imx6_anatop_softc *sc;
646 sc = arg;
648 sc->res[IRQRES] = bus_alloc_resource_any(sc->dev, SYS_RES_IRQ, &rid,
650 if (sc->res[IRQRES] != NULL) {
651 bus_setup_intr(sc->dev, sc->res[IRQRES],
652 INTR_TYPE_MISC | INTR_MPSAFE, tempmon_intr, NULL, sc,
653 &sc->temp_intrhand);
655 device_printf(sc->dev, "Cannot allocate IRQ resource\n");
657 config_intrhook_disestablish(&sc->intr_setup_hook);
663 struct imx6_anatop_softc *sc;
672 sc = device_get_softc(dev);
673 if (!sc->cpu_init_done && bus_get_pass() >= cpu_init_pass) {
674 sc->cpu_init_done = true;
675 cpufreq_initialize(sc);
676 initialize_tempmon(sc);
678 device_printf(sc->dev, "CPU %uMHz @ %umV\n",
679 sc->cpu_curmhz, sc->cpu_curmv);
696 struct imx6_anatop_softc *sc;
699 sc = device_get_softc(dev);
700 sc->dev = dev;
703 if (bus_alloc_resources(dev, imx6_anatop_spec, sc->res)) {
709 sc->intr_setup_hook.ich_func = intr_setup;
710 sc->intr_setup_hook.ich_arg = sc;
711 config_intrhook_establish(&sc->intr_setup_hook);
713 SYSCTL_ADD_UINT(device_get_sysctl_ctx(sc->dev),
714 SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev)),
716 &sc->cpu_curmv, 0, "Current CPU voltage in millivolts");
718 imx6_anatop_sc = sc;
740 bus_release_resources(dev, imx6_anatop_spec, sc->res);
774 if (ofw_bus_is_compatible(dev, "fsl,imx6q-anatop") == 0)