]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - sys/arm/freescale/imx/imx6_anatop.c
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.git] / sys / arm / freescale / imx / imx6_anatop.c
1 /*-
2  * Copyright (c) 2013 Ian Lepore <ian@freebsd.org>
3  * Copyright (c) 2014 Steven Lawrance <stl@koffein.net>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30
31 /*
32  * Analog PLL and power regulator driver for Freescale i.MX6 family of SoCs.
33  * Also, temperature montoring and cpu frequency control.  It was Freescale who
34  * kitchen-sinked this device, not us. :)
35  *
36  * We don't really do anything with analog PLLs, but the registers for
37  * controlling them belong to the same block as the power regulator registers.
38  * Since the newbus hierarchy makes it hard for anyone other than us to get at
39  * them, we just export a couple public functions to allow the imx6 CCM clock
40  * driver to read and write those registers.
41  *
42  * We also don't do anything about power regulation yet, but when the need
43  * arises, this would be the place for that code to live.
44  *
45  * I have no idea where the "anatop" name comes from.  It's in the standard DTS
46  * source describing i.MX6 SoCs, and in the linux and u-boot code which comes
47  * from Freescale, but it's not in the SoC manual.
48  *
49  * Note that temperature values throughout this code are handled in two types of
50  * units.  Items with '_cnt' in the name use the hardware temperature count
51  * units (higher counts are lower temperatures).  Items with '_val' in the name
52  * are deci-Celcius, which are converted to/from deci-Kelvins in the sysctl
53  * handlers (dK is the standard unit for temperature in sysctl).
54  */
55
56 #include <sys/param.h>
57 #include <sys/systm.h>
58 #include <sys/callout.h>
59 #include <sys/kernel.h>
60 #include <sys/limits.h>
61 #include <sys/sysctl.h>
62 #include <sys/module.h>
63 #include <sys/bus.h>
64 #include <sys/rman.h>
65
66 #include <dev/ofw/ofw_bus.h>
67 #include <dev/ofw/ofw_bus_subr.h>
68
69 #include <machine/bus.h>
70 #include <machine/fdt.h>
71
72 #include <arm/arm/mpcore_timervar.h>
73 #include <arm/freescale/fsl_ocotpreg.h>
74 #include <arm/freescale/fsl_ocotpvar.h>
75 #include <arm/freescale/imx/imx_ccmvar.h>
76 #include <arm/freescale/imx/imx6_anatopreg.h>
77 #include <arm/freescale/imx/imx6_anatopvar.h>
78
79 static SYSCTL_NODE(_hw, OID_AUTO, imx6, CTLFLAG_RW, NULL, "i.MX6 container");
80
81 static struct resource_spec imx6_anatop_spec[] = {
82         { SYS_RES_MEMORY,       0,      RF_ACTIVE },
83         { SYS_RES_IRQ,          0,      RF_ACTIVE },
84         { -1, 0 }
85 };
86 #define MEMRES  0
87 #define IRQRES  1
88
89 struct imx6_anatop_softc {
90         device_t        dev;
91         struct resource *res[2];
92         struct intr_config_hook
93                         intr_setup_hook;
94         uint32_t        cpu_curmhz;
95         uint32_t        cpu_curmv;
96         uint32_t        cpu_minmhz;
97         uint32_t        cpu_minmv;
98         uint32_t        cpu_maxmhz;
99         uint32_t        cpu_maxmv;
100         uint32_t        cpu_maxmhz_hw;
101         boolean_t       cpu_overclock_enable;
102         boolean_t       cpu_init_done;
103         uint32_t        refosc_mhz;
104         void            *temp_intrhand;
105         uint32_t        temp_high_val;
106         uint32_t        temp_high_cnt;
107         uint32_t        temp_last_cnt;
108         uint32_t        temp_room_cnt;
109         struct callout  temp_throttle_callout;
110         sbintime_t      temp_throttle_delay;
111         uint32_t        temp_throttle_reset_cnt;
112         uint32_t        temp_throttle_trigger_cnt;
113         uint32_t        temp_throttle_val;
114 };
115
116 static struct imx6_anatop_softc *imx6_anatop_sc;
117
118 /*
119  * Table of "operating points".
120  * These are combinations of frequency and voltage blessed by Freescale.
121  * While the datasheet says the ARM voltage can be as low as 925mV at
122  * 396MHz, it also says that the ARM and SOC voltages can't differ by
123  * more than 200mV, and the minimum SOC voltage is 1150mV, so that
124  * dictates the 950mV entry in this table.
125  */
126 static struct oppt {
127         uint32_t        mhz;
128         uint32_t        mv;
129 } imx6_oppt_table[] = {
130         { 396,   950},
131         { 792,  1150},
132         { 852,  1225},
133         { 996,  1225},
134         {1200,  1275},
135 };
136
137 /*
138  * Table of CPU max frequencies.  This is used to translate the max frequency
139  * value (0-3) from the ocotp CFG3 register into a mhz value that can be looked
140  * up in the operating points table.
141  */
142 static uint32_t imx6_ocotp_mhz_tab[] = {792, 852, 996, 1200};
143
144 #define TZ_ZEROC        2732    /* deci-Kelvin <-> deci-Celcius offset. */
145
146 uint32_t
147 imx6_anatop_read_4(bus_size_t offset)
148 {
149
150         KASSERT(imx6_anatop_sc != NULL, ("imx6_anatop_read_4 sc NULL"));
151
152         return (bus_read_4(imx6_anatop_sc->res[MEMRES], offset));
153 }
154
155 void
156 imx6_anatop_write_4(bus_size_t offset, uint32_t value)
157 {
158
159         KASSERT(imx6_anatop_sc != NULL, ("imx6_anatop_write_4 sc NULL"));
160
161         bus_write_4(imx6_anatop_sc->res[MEMRES], offset, value);
162 }
163
164 static void
165 vdd_set(struct imx6_anatop_softc *sc, int mv)
166 {
167         int newtarg, newtargSoc, oldtarg;
168         uint32_t delay, pmureg;
169         static boolean_t init_done = false;
170
171         /*
172          * The datasheet says VDD_PU and VDD_SOC must be equal, and VDD_ARM
173          * can't be more than 50mV above or 200mV below them.  We keep them the
174          * same except in the case of the lowest operating point, which is
175          * handled as a special case below.
176          */
177
178         pmureg = imx6_anatop_read_4(IMX6_ANALOG_PMU_REG_CORE);
179         oldtarg = pmureg & IMX6_ANALOG_PMU_REG0_TARG_MASK;
180
181         /* Convert mV to target value.  Clamp target to valid range. */
182         if (mv < 725)
183                 newtarg = 0x00;
184         else if (mv > 1450)
185                 newtarg = 0x1F;
186         else
187                 newtarg = (mv - 700) / 25;
188
189         /*
190          * The SOC voltage can't go below 1150mV, and thus because of the 200mV
191          * rule, the ARM voltage can't go below 950mV.  The 950 is encoded in
192          * our oppt table, here we handle the SOC 1150 rule as a special case.
193          * (1150-700/25=18).
194          */
195         newtargSoc = (newtarg < 18) ? 18 : newtarg;
196
197         /*
198          * The first time through the 3 voltages might not be equal so use a
199          * long conservative delay.  After that we need to delay 3uS for every
200          * 25mV step upward; we actually delay 6uS because empirically, it works
201          * and the 3uS per step recommended by the docs doesn't (3uS fails when
202          * going from 400->1200, but works for smaller changes).
203          */
204         if (init_done) {
205                 if (newtarg == oldtarg)
206                         return;
207                 else if (newtarg > oldtarg)
208                         delay = (newtarg - oldtarg) * 6;
209                 else
210                         delay = 0;
211         } else {
212                 delay = (700 / 25) * 6;
213                 init_done = true;
214         }
215
216         /*
217          * Make the change and wait for it to take effect.
218          */
219         pmureg &= ~(IMX6_ANALOG_PMU_REG0_TARG_MASK |
220             IMX6_ANALOG_PMU_REG1_TARG_MASK |
221             IMX6_ANALOG_PMU_REG2_TARG_MASK);
222
223         pmureg |= newtarg << IMX6_ANALOG_PMU_REG0_TARG_SHIFT;
224         pmureg |= newtarg << IMX6_ANALOG_PMU_REG1_TARG_SHIFT;
225         pmureg |= newtargSoc << IMX6_ANALOG_PMU_REG2_TARG_SHIFT;
226
227         imx6_anatop_write_4(IMX6_ANALOG_PMU_REG_CORE, pmureg);
228         DELAY(delay);
229         sc->cpu_curmv = newtarg * 25 + 700;
230 }
231
232 static inline uint32_t
233 cpufreq_mhz_from_div(struct imx6_anatop_softc *sc, uint32_t corediv, 
234     uint32_t plldiv)
235 {
236
237         return ((sc->refosc_mhz * (plldiv / 2)) / (corediv + 1));
238 }
239
240 static inline void
241 cpufreq_mhz_to_div(struct imx6_anatop_softc *sc, uint32_t cpu_mhz,
242     uint32_t *corediv, uint32_t *plldiv)
243 {
244
245         *corediv = (cpu_mhz < 650) ? 1 : 0;
246         *plldiv = ((*corediv + 1) * cpu_mhz) / (sc->refosc_mhz / 2);
247 }
248
249 static inline uint32_t
250 cpufreq_actual_mhz(struct imx6_anatop_softc *sc, uint32_t cpu_mhz)
251 {
252         uint32_t corediv, plldiv;
253
254         cpufreq_mhz_to_div(sc, cpu_mhz, &corediv, &plldiv);
255         return (cpufreq_mhz_from_div(sc, corediv, plldiv));
256 }
257
258 static struct oppt *
259 cpufreq_nearest_oppt(struct imx6_anatop_softc *sc, uint32_t cpu_newmhz)
260 {
261         int d, diff, i, nearest;
262
263         if (cpu_newmhz > sc->cpu_maxmhz_hw && !sc->cpu_overclock_enable)
264                 cpu_newmhz = sc->cpu_maxmhz_hw;
265
266         diff = INT_MAX;
267         nearest = 0;
268         for (i = 0; i < nitems(imx6_oppt_table); ++i) {
269                 d = abs((int)cpu_newmhz - (int)imx6_oppt_table[i].mhz);
270                 if (diff > d) {
271                         diff = d;
272                         nearest = i;
273                 }
274         }
275         return (&imx6_oppt_table[nearest]);
276 }
277
278 static void 
279 cpufreq_set_clock(struct imx6_anatop_softc * sc, struct oppt *op)
280 {
281         uint32_t corediv, plldiv, timeout, wrk32;
282
283         /* If increasing the frequency, we must first increase the voltage. */
284         if (op->mhz > sc->cpu_curmhz) {
285                 vdd_set(sc, op->mv);
286         }
287
288         /*
289          * I can't find a documented procedure for changing the ARM PLL divisor,
290          * but some trial and error came up with this:
291          *  - Set the bypass clock source to REF_CLK_24M (source #0).
292          *  - Set the PLL into bypass mode; cpu should now be running at 24mhz.
293          *  - Change the divisor.
294          *  - Wait for the LOCK bit to come on; it takes ~50 loop iterations.
295          *  - Turn off bypass mode; cpu should now be running at the new speed.
296          */
297         cpufreq_mhz_to_div(sc, op->mhz, &corediv, &plldiv);
298         imx6_anatop_write_4(IMX6_ANALOG_CCM_PLL_ARM_CLR, 
299             IMX6_ANALOG_CCM_PLL_ARM_CLK_SRC_MASK);
300         imx6_anatop_write_4(IMX6_ANALOG_CCM_PLL_ARM_SET, 
301             IMX6_ANALOG_CCM_PLL_ARM_BYPASS);
302
303         wrk32 = imx6_anatop_read_4(IMX6_ANALOG_CCM_PLL_ARM);
304         wrk32 &= ~IMX6_ANALOG_CCM_PLL_ARM_DIV_MASK;
305         wrk32 |= plldiv;
306         imx6_anatop_write_4(IMX6_ANALOG_CCM_PLL_ARM, wrk32);
307
308         timeout = 10000;
309         while ((imx6_anatop_read_4(IMX6_ANALOG_CCM_PLL_ARM) &
310             IMX6_ANALOG_CCM_PLL_ARM_LOCK) == 0)
311                 if (--timeout == 0)
312                         panic("imx6_set_cpu_clock(): PLL never locked");
313
314         imx6_anatop_write_4(IMX6_ANALOG_CCM_PLL_ARM_CLR, 
315             IMX6_ANALOG_CCM_PLL_ARM_BYPASS);
316         imx_ccm_set_cacrr(corediv);
317
318         /* If lowering the frequency, it is now safe to lower the voltage. */
319         if (op->mhz < sc->cpu_curmhz)
320                 vdd_set(sc, op->mv);
321         sc->cpu_curmhz = op->mhz;
322
323         /* Tell the mpcore timer that its frequency has changed. */
324         arm_tmr_change_frequency(
325             cpufreq_actual_mhz(sc, sc->cpu_curmhz) * 1000000 / 2);
326 }
327
328 static int
329 cpufreq_sysctl_minmhz(SYSCTL_HANDLER_ARGS)
330 {
331         struct imx6_anatop_softc *sc;
332         struct oppt * op;
333         uint32_t temp;
334         int err;
335
336         sc = arg1;
337
338         temp = sc->cpu_minmhz;
339         err = sysctl_handle_int(oidp, &temp, 0, req);
340         if (err != 0 || req->newptr == NULL)
341                 return (err);
342
343         op = cpufreq_nearest_oppt(sc, temp);
344         if (op->mhz > sc->cpu_maxmhz)
345                 return (ERANGE);
346         else if (op->mhz == sc->cpu_minmhz)
347                 return (0);
348
349         /*
350          * Value changed, update softc.  If the new min is higher than the
351          * current speed, raise the current speed to match.
352          */
353         sc->cpu_minmhz = op->mhz;
354         if (sc->cpu_minmhz > sc->cpu_curmhz) {
355                 cpufreq_set_clock(sc, op);
356         }
357         return (err);
358 }
359
360 static int
361 cpufreq_sysctl_maxmhz(SYSCTL_HANDLER_ARGS)
362 {
363         struct imx6_anatop_softc *sc;
364         struct oppt * op;
365         uint32_t temp;
366         int err;
367
368         sc = arg1;
369
370         temp = sc->cpu_maxmhz;
371         err = sysctl_handle_int(oidp, &temp, 0, req);
372         if (err != 0 || req->newptr == NULL)
373                 return (err);
374
375         op = cpufreq_nearest_oppt(sc, temp);
376         if (op->mhz < sc->cpu_minmhz)
377                 return (ERANGE);
378         else if (op->mhz == sc->cpu_maxmhz)
379                 return (0);
380
381         /*
382          *  Value changed, update softc and hardware.  The hardware update is
383          *  unconditional.  We always try to run at max speed, so any change of
384          *  the max means we need to change the current speed too, regardless of
385          *  whether it is higher or lower than the old max.
386          */
387         sc->cpu_maxmhz = op->mhz;
388         cpufreq_set_clock(sc, op);
389
390         return (err);
391 }
392
393 static void
394 cpufreq_initialize(struct imx6_anatop_softc *sc)
395 {
396         uint32_t cfg3speed;
397         struct oppt * op;
398
399         SYSCTL_ADD_INT(NULL, SYSCTL_STATIC_CHILDREN(_hw_imx6),
400             OID_AUTO, "cpu_mhz", CTLFLAG_RD, &sc->cpu_curmhz, 0, 
401             "CPU frequency");
402
403         SYSCTL_ADD_PROC(NULL, SYSCTL_STATIC_CHILDREN(_hw_imx6), 
404             OID_AUTO, "cpu_minmhz", CTLTYPE_INT | CTLFLAG_RWTUN, sc, 0,
405             cpufreq_sysctl_minmhz, "IU", "Minimum CPU frequency");
406
407         SYSCTL_ADD_PROC(NULL, SYSCTL_STATIC_CHILDREN(_hw_imx6),
408             OID_AUTO, "cpu_maxmhz", CTLTYPE_INT | CTLFLAG_RWTUN, sc, 0,
409             cpufreq_sysctl_maxmhz, "IU", "Maximum CPU frequency");
410
411         SYSCTL_ADD_INT(NULL, SYSCTL_STATIC_CHILDREN(_hw_imx6),
412             OID_AUTO, "cpu_maxmhz_hw", CTLFLAG_RD, &sc->cpu_maxmhz_hw, 0, 
413             "Maximum CPU frequency allowed by hardware");
414
415         SYSCTL_ADD_INT(NULL, SYSCTL_STATIC_CHILDREN(_hw_imx6),
416             OID_AUTO, "cpu_overclock_enable", CTLFLAG_RWTUN, 
417             &sc->cpu_overclock_enable, 0, 
418             "Allow setting CPU frequency higher than cpu_maxmhz_hw");
419
420         /*
421          * XXX 24mhz shouldn't be hard-coded, should get this from imx6_ccm
422          * (even though in the real world it will always be 24mhz).  Oh wait a
423          * sec, I never wrote imx6_ccm.
424          */
425         sc->refosc_mhz = 24;
426
427         /*
428          * Get the maximum speed this cpu can be set to.  The values in the
429          * OCOTP CFG3 register are not documented in the reference manual.
430          * The following info was in an archived email found via web search:
431          *   - 2b'11: 1200000000Hz;
432          *   - 2b'10: 996000000Hz;
433          *   - 2b'01: 852000000Hz; -- i.MX6Q Only, exclusive with 996MHz.
434          *   - 2b'00: 792000000Hz;
435          * The default hardware max speed can be overridden by a tunable.
436          */
437         cfg3speed = (fsl_ocotp_read_4(FSL_OCOTP_CFG3) & 
438             FSL_OCOTP_CFG3_SPEED_MASK) >> FSL_OCOTP_CFG3_SPEED_SHIFT;
439         sc->cpu_maxmhz_hw = imx6_ocotp_mhz_tab[cfg3speed];
440         sc->cpu_maxmhz = sc->cpu_maxmhz_hw;
441
442         TUNABLE_INT_FETCH("hw.imx6.cpu_overclock_enable",
443             &sc->cpu_overclock_enable);
444
445         TUNABLE_INT_FETCH("hw.imx6.cpu_minmhz", &sc->cpu_minmhz);
446         op = cpufreq_nearest_oppt(sc, sc->cpu_minmhz);
447         sc->cpu_minmhz = op->mhz;
448         sc->cpu_minmv = op->mv;
449
450         TUNABLE_INT_FETCH("hw.imx6.cpu_maxmhz", &sc->cpu_maxmhz);
451         op = cpufreq_nearest_oppt(sc, sc->cpu_maxmhz);
452         sc->cpu_maxmhz = op->mhz;
453         sc->cpu_maxmv = op->mv;
454
455         /*
456          * Set the CPU to maximum speed.
457          *
458          * We won't have thermal throttling until interrupts are enabled, but we
459          * want to run at full speed through all the device init stuff.  This
460          * basically assumes that a single core can't overheat before interrupts
461          * are enabled; empirical testing shows that to be a safe assumption.
462          */
463         cpufreq_set_clock(sc, op);
464 }
465
466 static inline uint32_t
467 temp_from_count(struct imx6_anatop_softc *sc, uint32_t count)
468 {
469
470         return (((sc->temp_high_val - (count - sc->temp_high_cnt) *
471             (sc->temp_high_val - 250) / 
472             (sc->temp_room_cnt - sc->temp_high_cnt))));
473 }
474
475 static inline uint32_t
476 temp_to_count(struct imx6_anatop_softc *sc, uint32_t temp)
477 {
478
479         return ((sc->temp_room_cnt - sc->temp_high_cnt) * 
480             (sc->temp_high_val - temp) / (sc->temp_high_val - 250) + 
481             sc->temp_high_cnt);
482 }
483
484 static void
485 temp_update_count(struct imx6_anatop_softc *sc)
486 {
487         uint32_t val;
488
489         val = imx6_anatop_read_4(IMX6_ANALOG_TEMPMON_TEMPSENSE0);
490         if (!(val & IMX6_ANALOG_TEMPMON_TEMPSENSE0_VALID))
491                 return;
492         sc->temp_last_cnt =
493             (val & IMX6_ANALOG_TEMPMON_TEMPSENSE0_TEMP_CNT_MASK) >>
494             IMX6_ANALOG_TEMPMON_TEMPSENSE0_TEMP_CNT_SHIFT;
495 }
496
497 static int
498 temp_sysctl_handler(SYSCTL_HANDLER_ARGS)
499 {
500         struct imx6_anatop_softc *sc = arg1;
501         uint32_t t;
502
503         temp_update_count(sc);
504
505         t = temp_from_count(sc, sc->temp_last_cnt) + TZ_ZEROC;
506
507         return (sysctl_handle_int(oidp, &t, 0, req));
508 }
509
510 static int
511 temp_throttle_sysctl_handler(SYSCTL_HANDLER_ARGS)
512 {
513         struct imx6_anatop_softc *sc = arg1;
514         int err;
515         uint32_t temp;
516
517         temp = sc->temp_throttle_val + TZ_ZEROC;
518         err = sysctl_handle_int(oidp, &temp, 0, req);
519         if (temp < TZ_ZEROC)
520                 return (ERANGE);
521         temp -= TZ_ZEROC;
522         if (err != 0 || req->newptr == NULL || temp == sc->temp_throttle_val)
523                 return (err);
524
525         /* Value changed, update counts in softc and hardware. */
526         sc->temp_throttle_val = temp;
527         sc->temp_throttle_trigger_cnt = temp_to_count(sc, sc->temp_throttle_val);
528         sc->temp_throttle_reset_cnt = temp_to_count(sc, sc->temp_throttle_val - 100);
529         imx6_anatop_write_4(IMX6_ANALOG_TEMPMON_TEMPSENSE0_CLR,
530             IMX6_ANALOG_TEMPMON_TEMPSENSE0_ALARM_MASK);
531         imx6_anatop_write_4(IMX6_ANALOG_TEMPMON_TEMPSENSE0_SET,
532             (sc->temp_throttle_trigger_cnt <<
533              IMX6_ANALOG_TEMPMON_TEMPSENSE0_ALARM_SHIFT));
534         return (err);
535 }
536
537 static void
538 tempmon_gofast(struct imx6_anatop_softc *sc)
539 {
540
541         if (sc->cpu_curmhz < sc->cpu_maxmhz) {
542                 cpufreq_set_clock(sc, cpufreq_nearest_oppt(sc, sc->cpu_maxmhz));
543         }
544 }
545
546 static void
547 tempmon_goslow(struct imx6_anatop_softc *sc)
548 {
549
550         if (sc->cpu_curmhz > sc->cpu_minmhz) {
551                 cpufreq_set_clock(sc, cpufreq_nearest_oppt(sc, sc->cpu_minmhz));
552         }
553 }
554
555 static int
556 tempmon_intr(void *arg)
557 {
558         struct imx6_anatop_softc *sc = arg;
559
560         /*
561          * XXX Note that this code doesn't currently run (for some mysterious
562          * reason we just never get an interrupt), so the real monitoring is
563          * done by tempmon_throttle_check().
564          */
565         tempmon_goslow(sc);
566         /* XXX Schedule callout to speed back up eventually. */
567         return (FILTER_HANDLED);
568 }
569
570 static void
571 tempmon_throttle_check(void *arg)
572 {
573         struct imx6_anatop_softc *sc = arg;
574
575         /* Lower counts are higher temperatures. */
576         if (sc->temp_last_cnt < sc->temp_throttle_trigger_cnt)
577                 tempmon_goslow(sc);
578         else if (sc->temp_last_cnt > (sc->temp_throttle_reset_cnt))
579                 tempmon_gofast(sc);
580
581         callout_reset_sbt(&sc->temp_throttle_callout, sc->temp_throttle_delay,
582                 0, tempmon_throttle_check, sc, 0);
583
584 }
585
586 static void
587 initialize_tempmon(struct imx6_anatop_softc *sc)
588 {
589         uint32_t cal;
590
591         /*
592          * Fetch calibration data: a sensor count at room temperature (25C),
593          * a sensor count at a high temperature, and that temperature
594          */
595         cal = fsl_ocotp_read_4(FSL_OCOTP_ANA1);
596         sc->temp_room_cnt = (cal & 0xFFF00000) >> 20;
597         sc->temp_high_cnt = (cal & 0x000FFF00) >> 8;
598         sc->temp_high_val = (cal & 0x000000FF) * 10;
599
600         /*
601          * Throttle to a lower cpu freq at 10C below the "hot" temperature, and
602          * reset back to max cpu freq at 5C below the trigger.
603          */
604         sc->temp_throttle_val = sc->temp_high_val - 100;
605         sc->temp_throttle_trigger_cnt =
606             temp_to_count(sc, sc->temp_throttle_val);
607         sc->temp_throttle_reset_cnt = 
608             temp_to_count(sc, sc->temp_throttle_val - 50);
609
610         /*
611          * Set the sensor to sample automatically at 16Hz (32.768KHz/0x800), set
612          * the throttle count, and begin making measurements.
613          */
614         imx6_anatop_write_4(IMX6_ANALOG_TEMPMON_TEMPSENSE1, 0x0800);
615         imx6_anatop_write_4(IMX6_ANALOG_TEMPMON_TEMPSENSE0,
616             (sc->temp_throttle_trigger_cnt << 
617             IMX6_ANALOG_TEMPMON_TEMPSENSE0_ALARM_SHIFT) |
618             IMX6_ANALOG_TEMPMON_TEMPSENSE0_MEASURE);
619
620         /*
621          * XXX Note that the alarm-interrupt feature isn't working yet, so
622          * we'll use a callout handler to check at 10Hz.  Make sure we have an
623          * initial temperature reading before starting up the callouts so we
624          * don't get a bogus reading of zero.
625          */
626         while (sc->temp_last_cnt == 0)
627                 temp_update_count(sc);
628         sc->temp_throttle_delay = 100 * SBT_1MS;
629         callout_init(&sc->temp_throttle_callout, 0);
630         callout_reset_sbt(&sc->temp_throttle_callout, sc->temp_throttle_delay, 
631             0, tempmon_throttle_check, sc, 0);
632
633         SYSCTL_ADD_PROC(NULL, SYSCTL_STATIC_CHILDREN(_hw_imx6), 
634             OID_AUTO, "temperature", CTLTYPE_INT | CTLFLAG_RD, sc, 0,
635             temp_sysctl_handler, "IK", "Current die temperature");
636         SYSCTL_ADD_PROC(NULL, SYSCTL_STATIC_CHILDREN(_hw_imx6), 
637             OID_AUTO, "throttle_temperature", CTLTYPE_INT | CTLFLAG_RW, sc,
638             0, temp_throttle_sysctl_handler, "IK", 
639             "Throttle CPU when exceeding this temperature");
640 }
641
642 static void
643 intr_setup(void *arg)
644 {
645         struct imx6_anatop_softc *sc;
646
647         sc = arg;
648         bus_setup_intr(sc->dev, sc->res[IRQRES], INTR_TYPE_MISC | INTR_MPSAFE,
649             tempmon_intr, NULL, sc, &sc->temp_intrhand);
650         config_intrhook_disestablish(&sc->intr_setup_hook);
651 }
652
653 static void
654 imx6_anatop_new_pass(device_t dev)
655 {
656         struct imx6_anatop_softc *sc;
657         const int cpu_init_pass = BUS_PASS_CPU + BUS_PASS_ORDER_MIDDLE;
658
659         /*
660          * We attach during BUS_PASS_BUS (because some day we will be a
661          * simplebus that has regulator devices as children), but some of our
662          * init work cannot be done until BUS_PASS_CPU (we rely on other devices
663          * that attach on the CPU pass).
664          */
665         sc = device_get_softc(dev);
666         if (!sc->cpu_init_done && bus_current_pass >= cpu_init_pass) {
667                 sc->cpu_init_done = true;
668                 cpufreq_initialize(sc);
669                 initialize_tempmon(sc);
670                 if (bootverbose) {
671                         device_printf(sc->dev, "CPU %uMHz @ %umV\n", 
672                             sc->cpu_curmhz, sc->cpu_curmv);
673                 }
674         }
675         bus_generic_new_pass(dev);
676 }
677
678 static int
679 imx6_anatop_detach(device_t dev)
680 {
681
682         /* This device can never detach. */
683         return (EBUSY);
684 }
685
686 static int
687 imx6_anatop_attach(device_t dev)
688 {
689         struct imx6_anatop_softc *sc;
690         int err;
691
692         sc = device_get_softc(dev);
693         sc->dev = dev;
694
695         /* Allocate bus_space resources. */
696         if (bus_alloc_resources(dev, imx6_anatop_spec, sc->res)) {
697                 device_printf(dev, "Cannot allocate resources\n");
698                 err = ENXIO;
699                 goto out;
700         }
701
702         sc->intr_setup_hook.ich_func = intr_setup;
703         sc->intr_setup_hook.ich_arg = sc;
704         config_intrhook_establish(&sc->intr_setup_hook);
705
706         SYSCTL_ADD_UINT(device_get_sysctl_ctx(sc->dev),
707             SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev)),
708             OID_AUTO, "cpu_voltage", CTLFLAG_RD,
709             &sc->cpu_curmv, 0, "Current CPU voltage in millivolts");
710
711         imx6_anatop_sc = sc;
712
713         /*
714          * Other code seen on the net sets this SELFBIASOFF flag around the same
715          * time the temperature sensor is set up, although it's unclear how the
716          * two are related (if at all).
717          */
718         imx6_anatop_write_4(IMX6_ANALOG_PMU_MISC0_SET, 
719             IMX6_ANALOG_PMU_MISC0_SELFBIASOFF);
720
721         /*
722          * Some day, when we're ready to deal with the actual anatop regulators
723          * that are described in fdt data as children of this "bus", this would
724          * be the place to invoke a simplebus helper routine to instantiate the
725          * children from the fdt data.
726          */
727
728         err = 0;
729
730 out:
731
732         if (err != 0) {
733                 bus_release_resources(dev, imx6_anatop_spec, sc->res);
734         }
735
736         return (err);
737 }
738
739 uint32_t
740 pll4_configure_output(uint32_t mfi, uint32_t mfn, uint32_t mfd)
741 {
742         int reg;
743
744         /*
745          * Audio PLL (PLL4).
746          * PLL output frequency = Fref * (DIV_SELECT + NUM/DENOM)
747          */
748
749         reg = (IMX6_ANALOG_CCM_PLL_AUDIO_ENABLE);
750         reg &= ~(IMX6_ANALOG_CCM_PLL_AUDIO_DIV_SELECT_MASK << \
751                 IMX6_ANALOG_CCM_PLL_AUDIO_DIV_SELECT_SHIFT);
752         reg |= (mfi << IMX6_ANALOG_CCM_PLL_AUDIO_DIV_SELECT_SHIFT);
753         imx6_anatop_write_4(IMX6_ANALOG_CCM_PLL_AUDIO, reg);
754         imx6_anatop_write_4(IMX6_ANALOG_CCM_PLL_AUDIO_NUM, mfn);
755         imx6_anatop_write_4(IMX6_ANALOG_CCM_PLL_AUDIO_DENOM, mfd);
756
757         return (0);
758 }
759
760 static int
761 imx6_anatop_probe(device_t dev)
762 {
763
764         if (!ofw_bus_status_okay(dev))
765                 return (ENXIO);
766
767         if (ofw_bus_is_compatible(dev, "fsl,imx6q-anatop") == 0)
768                 return (ENXIO);
769
770         device_set_desc(dev, "Freescale i.MX6 Analog PLLs and Power");
771
772         return (BUS_PROBE_DEFAULT);
773 }
774
775 uint32_t 
776 imx6_get_cpu_clock()
777 {
778         uint32_t corediv, plldiv;
779
780         corediv = imx_ccm_get_cacrr();
781         plldiv = imx6_anatop_read_4(IMX6_ANALOG_CCM_PLL_ARM) &
782             IMX6_ANALOG_CCM_PLL_ARM_DIV_MASK;
783         return (cpufreq_mhz_from_div(imx6_anatop_sc, corediv, plldiv));
784 }
785
786 static device_method_t imx6_anatop_methods[] = {
787         /* Device interface */
788         DEVMETHOD(device_probe,  imx6_anatop_probe),
789         DEVMETHOD(device_attach, imx6_anatop_attach),
790         DEVMETHOD(device_detach, imx6_anatop_detach),
791
792         /* Bus interface */
793         DEVMETHOD(bus_new_pass,  imx6_anatop_new_pass),
794
795         DEVMETHOD_END
796 };
797
798 static driver_t imx6_anatop_driver = {
799         "imx6_anatop",
800         imx6_anatop_methods,
801         sizeof(struct imx6_anatop_softc)
802 };
803
804 static devclass_t imx6_anatop_devclass;
805
806 EARLY_DRIVER_MODULE(imx6_anatop, simplebus, imx6_anatop_driver,
807     imx6_anatop_devclass, 0, 0, BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE);
808 EARLY_DRIVER_MODULE(imx6_anatop, ofwbus, imx6_anatop_driver,
809     imx6_anatop_devclass, 0, 0, BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE);
810