xref: /freebsd/sys/arm/freescale/imx/imx6_anatop.c (revision 3514f98940a0d43264ef9e13990c8c70a4252e04)
19808ebfaSIan Lepore /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3af3dc4a7SPedro F. Giffuni  *
49808ebfaSIan Lepore  * Copyright (c) 2013 Ian Lepore <ian@freebsd.org>
586a55754SIan Lepore  * Copyright (c) 2014 Steven Lawrance <stl@koffein.net>
69808ebfaSIan Lepore  * All rights reserved.
79808ebfaSIan Lepore  *
89808ebfaSIan Lepore  * Redistribution and use in source and binary forms, with or without
99808ebfaSIan Lepore  * modification, are permitted provided that the following conditions
109808ebfaSIan Lepore  * are met:
119808ebfaSIan Lepore  * 1. Redistributions of source code must retain the above copyright
129808ebfaSIan Lepore  *    notice, this list of conditions and the following disclaimer.
139808ebfaSIan Lepore  * 2. Redistributions in binary form must reproduce the above copyright
149808ebfaSIan Lepore  *    notice, this list of conditions and the following disclaimer in the
159808ebfaSIan Lepore  *    documentation and/or other materials provided with the distribution.
169808ebfaSIan Lepore  *
179808ebfaSIan Lepore  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
189808ebfaSIan Lepore  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
199808ebfaSIan Lepore  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
209808ebfaSIan Lepore  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
219808ebfaSIan Lepore  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
229808ebfaSIan Lepore  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
239808ebfaSIan Lepore  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
249808ebfaSIan Lepore  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
259808ebfaSIan Lepore  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
269808ebfaSIan Lepore  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
279808ebfaSIan Lepore  * SUCH DAMAGE.
289808ebfaSIan Lepore  */
299808ebfaSIan Lepore 
309808ebfaSIan Lepore #include <sys/cdefs.h>
319808ebfaSIan Lepore /*
329808ebfaSIan Lepore  * Analog PLL and power regulator driver for Freescale i.MX6 family of SoCs.
3386a55754SIan Lepore  * Also, temperature montoring and cpu frequency control.  It was Freescale who
3486a55754SIan Lepore  * kitchen-sinked this device, not us. :)
359808ebfaSIan Lepore  *
369808ebfaSIan Lepore  * We don't really do anything with analog PLLs, but the registers for
379808ebfaSIan Lepore  * controlling them belong to the same block as the power regulator registers.
389808ebfaSIan Lepore  * Since the newbus hierarchy makes it hard for anyone other than us to get at
399808ebfaSIan Lepore  * them, we just export a couple public functions to allow the imx6 CCM clock
409808ebfaSIan Lepore  * driver to read and write those registers.
419808ebfaSIan Lepore  *
429808ebfaSIan Lepore  * We also don't do anything about power regulation yet, but when the need
439808ebfaSIan Lepore  * arises, this would be the place for that code to live.
449808ebfaSIan Lepore  *
459808ebfaSIan Lepore  * I have no idea where the "anatop" name comes from.  It's in the standard DTS
469808ebfaSIan Lepore  * source describing i.MX6 SoCs, and in the linux and u-boot code which comes
479808ebfaSIan Lepore  * from Freescale, but it's not in the SoC manual.
4886a55754SIan Lepore  *
4986a55754SIan Lepore  * Note that temperature values throughout this code are handled in two types of
5086a55754SIan Lepore  * units.  Items with '_cnt' in the name use the hardware temperature count
5186a55754SIan Lepore  * units (higher counts are lower temperatures).  Items with '_val' in the name
529097ac9aSElyes HAOUAS  * are deci-Celsius, which are converted to/from deci-Kelvins in the sysctl
5386a55754SIan Lepore  * handlers (dK is the standard unit for temperature in sysctl).
549808ebfaSIan Lepore  */
559808ebfaSIan Lepore 
569808ebfaSIan Lepore #include <sys/param.h>
579808ebfaSIan Lepore #include <sys/systm.h>
5886a55754SIan Lepore #include <sys/callout.h>
599808ebfaSIan Lepore #include <sys/kernel.h>
6001407480SIan Lepore #include <sys/limits.h>
6186a55754SIan Lepore #include <sys/sysctl.h>
629808ebfaSIan Lepore #include <sys/module.h>
639808ebfaSIan Lepore #include <sys/bus.h>
649808ebfaSIan Lepore #include <sys/rman.h>
659808ebfaSIan Lepore 
669808ebfaSIan Lepore #include <dev/ofw/ofw_bus.h>
679808ebfaSIan Lepore #include <dev/ofw/ofw_bus_subr.h>
689808ebfaSIan Lepore 
699808ebfaSIan Lepore #include <machine/bus.h>
709808ebfaSIan Lepore 
71a3ff7ef6SIan Lepore #include <arm/arm/mpcore_timervar.h>
7286a55754SIan Lepore #include <arm/freescale/fsl_ocotpreg.h>
7386a55754SIan Lepore #include <arm/freescale/fsl_ocotpvar.h>
74955691feSIan Lepore #include <arm/freescale/imx/imx_ccmvar.h>
75bd6b2f9bSIan Lepore #include <arm/freescale/imx/imx_machdep.h>
769808ebfaSIan Lepore #include <arm/freescale/imx/imx6_anatopreg.h>
779808ebfaSIan Lepore #include <arm/freescale/imx/imx6_anatopvar.h>
789808ebfaSIan Lepore 
7986a55754SIan Lepore static struct resource_spec imx6_anatop_spec[] = {
8086a55754SIan Lepore 	{ SYS_RES_MEMORY,	0,	RF_ACTIVE },
8186a55754SIan Lepore 	{ -1, 0 }
8286a55754SIan Lepore };
8386a55754SIan Lepore #define	MEMRES	0
8486a55754SIan Lepore #define	IRQRES	1
8586a55754SIan Lepore 
869808ebfaSIan Lepore struct imx6_anatop_softc {
879808ebfaSIan Lepore 	device_t	dev;
8886a55754SIan Lepore 	struct resource	*res[2];
8960f47da9SIan Lepore 	struct intr_config_hook
9060f47da9SIan Lepore 			intr_setup_hook;
9186a55754SIan Lepore 	uint32_t	cpu_curmhz;
92f85153feSIan Lepore 	uint32_t	cpu_curmv;
9301407480SIan Lepore 	uint32_t	cpu_minmhz;
94f85153feSIan Lepore 	uint32_t	cpu_minmv;
9501407480SIan Lepore 	uint32_t	cpu_maxmhz;
96f85153feSIan Lepore 	uint32_t	cpu_maxmv;
9701407480SIan Lepore 	uint32_t	cpu_maxmhz_hw;
9801407480SIan Lepore 	boolean_t	cpu_overclock_enable;
99a2d27ff8SIan Lepore 	boolean_t	cpu_init_done;
10001407480SIan Lepore 	uint32_t	refosc_mhz;
10186a55754SIan Lepore 	void		*temp_intrhand;
10286a55754SIan Lepore 	uint32_t	temp_high_val;
10386a55754SIan Lepore 	uint32_t	temp_high_cnt;
10486a55754SIan Lepore 	uint32_t	temp_last_cnt;
10586a55754SIan Lepore 	uint32_t	temp_room_cnt;
10686a55754SIan Lepore 	struct callout	temp_throttle_callout;
10786a55754SIan Lepore 	sbintime_t	temp_throttle_delay;
10886a55754SIan Lepore 	uint32_t	temp_throttle_reset_cnt;
10986a55754SIan Lepore 	uint32_t	temp_throttle_trigger_cnt;
11086a55754SIan Lepore 	uint32_t	temp_throttle_val;
1119808ebfaSIan Lepore };
1129808ebfaSIan Lepore 
1139808ebfaSIan Lepore static struct imx6_anatop_softc *imx6_anatop_sc;
1149808ebfaSIan Lepore 
11586a55754SIan Lepore /*
11601407480SIan Lepore  * Table of "operating points".
11701407480SIan Lepore  * These are combinations of frequency and voltage blessed by Freescale.
118955691feSIan Lepore  * While the datasheet says the ARM voltage can be as low as 925mV at
119955691feSIan Lepore  * 396MHz, it also says that the ARM and SOC voltages can't differ by
120955691feSIan Lepore  * more than 200mV, and the minimum SOC voltage is 1150mV, so that
121955691feSIan Lepore  * dictates the 950mV entry in this table.
12286a55754SIan Lepore  */
12301407480SIan Lepore static struct oppt {
12401407480SIan Lepore 	uint32_t	mhz;
12501407480SIan Lepore 	uint32_t	mv;
12601407480SIan Lepore } imx6_oppt_table[] = {
127955691feSIan Lepore 	{ 396,	 950},
12801407480SIan Lepore 	{ 792,	1150},
12901407480SIan Lepore 	{ 852,	1225},
13001407480SIan Lepore 	{ 996,	1225},
13101407480SIan Lepore 	{1200,	1275},
13286a55754SIan Lepore };
13301407480SIan Lepore 
13401407480SIan Lepore /*
13501407480SIan Lepore  * Table of CPU max frequencies.  This is used to translate the max frequency
13601407480SIan Lepore  * value (0-3) from the ocotp CFG3 register into a mhz value that can be looked
13701407480SIan Lepore  * up in the operating points table.
13801407480SIan Lepore  */
13901407480SIan Lepore static uint32_t imx6_ocotp_mhz_tab[] = {792, 852, 996, 1200};
14086a55754SIan Lepore 
1419097ac9aSElyes HAOUAS #define	TZ_ZEROC	2731	/* deci-Kelvin <-> deci-Celsius offset. */
14286a55754SIan Lepore 
1439808ebfaSIan Lepore uint32_t
imx6_anatop_read_4(bus_size_t offset)1449808ebfaSIan Lepore imx6_anatop_read_4(bus_size_t offset)
1459808ebfaSIan Lepore {
1469808ebfaSIan Lepore 
14786a55754SIan Lepore 	KASSERT(imx6_anatop_sc != NULL, ("imx6_anatop_read_4 sc NULL"));
14886a55754SIan Lepore 
14986a55754SIan Lepore 	return (bus_read_4(imx6_anatop_sc->res[MEMRES], offset));
1509808ebfaSIan Lepore }
1519808ebfaSIan Lepore 
1529808ebfaSIan Lepore void
imx6_anatop_write_4(bus_size_t offset,uint32_t value)1539808ebfaSIan Lepore imx6_anatop_write_4(bus_size_t offset, uint32_t value)
1549808ebfaSIan Lepore {
1559808ebfaSIan Lepore 
15686a55754SIan Lepore 	KASSERT(imx6_anatop_sc != NULL, ("imx6_anatop_write_4 sc NULL"));
15786a55754SIan Lepore 
15886a55754SIan Lepore 	bus_write_4(imx6_anatop_sc->res[MEMRES], offset, value);
15986a55754SIan Lepore }
16086a55754SIan Lepore 
161f85153feSIan Lepore static void
vdd_set(struct imx6_anatop_softc * sc,int mv)162f85153feSIan Lepore vdd_set(struct imx6_anatop_softc *sc, int mv)
163f85153feSIan Lepore {
164955691feSIan Lepore 	int newtarg, newtargSoc, oldtarg;
165f85153feSIan Lepore 	uint32_t delay, pmureg;
166f85153feSIan Lepore 	static boolean_t init_done = false;
167f85153feSIan Lepore 
168f85153feSIan Lepore 	/*
169f85153feSIan Lepore 	 * The datasheet says VDD_PU and VDD_SOC must be equal, and VDD_ARM
170955691feSIan Lepore 	 * can't be more than 50mV above or 200mV below them.  We keep them the
171955691feSIan Lepore 	 * same except in the case of the lowest operating point, which is
172955691feSIan Lepore 	 * handled as a special case below.
173f85153feSIan Lepore 	 */
174f85153feSIan Lepore 
175f85153feSIan Lepore 	pmureg = imx6_anatop_read_4(IMX6_ANALOG_PMU_REG_CORE);
176f85153feSIan Lepore 	oldtarg = pmureg & IMX6_ANALOG_PMU_REG0_TARG_MASK;
177f85153feSIan Lepore 
178f85153feSIan Lepore 	/* Convert mV to target value.  Clamp target to valid range. */
179f85153feSIan Lepore 	if (mv < 725)
180f85153feSIan Lepore 		newtarg = 0x00;
181f85153feSIan Lepore 	else if (mv > 1450)
182f85153feSIan Lepore 		newtarg = 0x1F;
183f85153feSIan Lepore 	else
184f85153feSIan Lepore 		newtarg = (mv - 700) / 25;
185f85153feSIan Lepore 
186f85153feSIan Lepore 	/*
187955691feSIan Lepore 	 * The SOC voltage can't go below 1150mV, and thus because of the 200mV
188955691feSIan Lepore 	 * rule, the ARM voltage can't go below 950mV.  The 950 is encoded in
189955691feSIan Lepore 	 * our oppt table, here we handle the SOC 1150 rule as a special case.
190955691feSIan Lepore 	 * (1150-700/25=18).
191955691feSIan Lepore 	 */
192955691feSIan Lepore 	newtargSoc = (newtarg < 18) ? 18 : newtarg;
193955691feSIan Lepore 
194955691feSIan Lepore 	/*
195f85153feSIan Lepore 	 * The first time through the 3 voltages might not be equal so use a
196f85153feSIan Lepore 	 * long conservative delay.  After that we need to delay 3uS for every
197955691feSIan Lepore 	 * 25mV step upward; we actually delay 6uS because empirically, it works
198955691feSIan Lepore 	 * and the 3uS per step recommended by the docs doesn't (3uS fails when
199955691feSIan Lepore 	 * going from 400->1200, but works for smaller changes).
200f85153feSIan Lepore 	 */
201f85153feSIan Lepore 	if (init_done) {
202f85153feSIan Lepore 		if (newtarg == oldtarg)
203f85153feSIan Lepore 			return;
204f85153feSIan Lepore 		else if (newtarg > oldtarg)
205955691feSIan Lepore 			delay = (newtarg - oldtarg) * 6;
206f85153feSIan Lepore 		else
207f85153feSIan Lepore 			delay = 0;
208f85153feSIan Lepore 	} else {
209955691feSIan Lepore 		delay = (700 / 25) * 6;
210f85153feSIan Lepore 		init_done = true;
211f85153feSIan Lepore 	}
212f85153feSIan Lepore 
213f85153feSIan Lepore 	/*
214f85153feSIan Lepore 	 * Make the change and wait for it to take effect.
215f85153feSIan Lepore 	 */
216f85153feSIan Lepore 	pmureg &= ~(IMX6_ANALOG_PMU_REG0_TARG_MASK |
217f85153feSIan Lepore 	    IMX6_ANALOG_PMU_REG1_TARG_MASK |
218f85153feSIan Lepore 	    IMX6_ANALOG_PMU_REG2_TARG_MASK);
219f85153feSIan Lepore 
220f85153feSIan Lepore 	pmureg |= newtarg << IMX6_ANALOG_PMU_REG0_TARG_SHIFT;
221f85153feSIan Lepore 	pmureg |= newtarg << IMX6_ANALOG_PMU_REG1_TARG_SHIFT;
222955691feSIan Lepore 	pmureg |= newtargSoc << IMX6_ANALOG_PMU_REG2_TARG_SHIFT;
223f85153feSIan Lepore 
224f85153feSIan Lepore 	imx6_anatop_write_4(IMX6_ANALOG_PMU_REG_CORE, pmureg);
225f85153feSIan Lepore 	DELAY(delay);
226f85153feSIan Lepore 	sc->cpu_curmv = newtarg * 25 + 700;
227f85153feSIan Lepore }
228f85153feSIan Lepore 
22986a55754SIan Lepore static inline uint32_t
cpufreq_mhz_from_div(struct imx6_anatop_softc * sc,uint32_t corediv,uint32_t plldiv)230955691feSIan Lepore cpufreq_mhz_from_div(struct imx6_anatop_softc *sc, uint32_t corediv,
231955691feSIan Lepore     uint32_t plldiv)
23286a55754SIan Lepore {
23386a55754SIan Lepore 
234955691feSIan Lepore 	return ((sc->refosc_mhz * (plldiv / 2)) / (corediv + 1));
23586a55754SIan Lepore }
23686a55754SIan Lepore 
237955691feSIan Lepore static inline void
cpufreq_mhz_to_div(struct imx6_anatop_softc * sc,uint32_t cpu_mhz,uint32_t * corediv,uint32_t * plldiv)238955691feSIan Lepore cpufreq_mhz_to_div(struct imx6_anatop_softc *sc, uint32_t cpu_mhz,
239955691feSIan Lepore     uint32_t *corediv, uint32_t *plldiv)
24086a55754SIan Lepore {
24186a55754SIan Lepore 
242955691feSIan Lepore 	*corediv = (cpu_mhz < 650) ? 1 : 0;
243955691feSIan Lepore 	*plldiv = ((*corediv + 1) * cpu_mhz) / (sc->refosc_mhz / 2);
24486a55754SIan Lepore }
24586a55754SIan Lepore 
24686a55754SIan Lepore static inline uint32_t
cpufreq_actual_mhz(struct imx6_anatop_softc * sc,uint32_t cpu_mhz)24701407480SIan Lepore cpufreq_actual_mhz(struct imx6_anatop_softc *sc, uint32_t cpu_mhz)
24886a55754SIan Lepore {
249955691feSIan Lepore 	uint32_t corediv, plldiv;
25086a55754SIan Lepore 
251955691feSIan Lepore 	cpufreq_mhz_to_div(sc, cpu_mhz, &corediv, &plldiv);
252955691feSIan Lepore 	return (cpufreq_mhz_from_div(sc, corediv, plldiv));
25301407480SIan Lepore }
25401407480SIan Lepore 
25501407480SIan Lepore static struct oppt *
cpufreq_nearest_oppt(struct imx6_anatop_softc * sc,uint32_t cpu_newmhz)25601407480SIan Lepore cpufreq_nearest_oppt(struct imx6_anatop_softc *sc, uint32_t cpu_newmhz)
25701407480SIan Lepore {
25801407480SIan Lepore 	int d, diff, i, nearest;
25901407480SIan Lepore 
26001407480SIan Lepore 	if (cpu_newmhz > sc->cpu_maxmhz_hw && !sc->cpu_overclock_enable)
26101407480SIan Lepore 		cpu_newmhz = sc->cpu_maxmhz_hw;
26201407480SIan Lepore 
26301407480SIan Lepore 	diff = INT_MAX;
26401407480SIan Lepore 	nearest = 0;
26501407480SIan Lepore 	for (i = 0; i < nitems(imx6_oppt_table); ++i) {
26601407480SIan Lepore 		d = abs((int)cpu_newmhz - (int)imx6_oppt_table[i].mhz);
26701407480SIan Lepore 		if (diff > d) {
26801407480SIan Lepore 			diff = d;
26901407480SIan Lepore 			nearest = i;
27001407480SIan Lepore 		}
27101407480SIan Lepore 	}
27201407480SIan Lepore 	return (&imx6_oppt_table[nearest]);
27386a55754SIan Lepore }
27486a55754SIan Lepore 
27586a55754SIan Lepore static void
cpufreq_set_clock(struct imx6_anatop_softc * sc,struct oppt * op)27601407480SIan Lepore cpufreq_set_clock(struct imx6_anatop_softc * sc, struct oppt *op)
27786a55754SIan Lepore {
278955691feSIan Lepore 	uint32_t corediv, plldiv, timeout, wrk32;
27986a55754SIan Lepore 
28001407480SIan Lepore 	/* If increasing the frequency, we must first increase the voltage. */
28101407480SIan Lepore 	if (op->mhz > sc->cpu_curmhz) {
28201407480SIan Lepore 		vdd_set(sc, op->mv);
28301407480SIan Lepore 	}
28486a55754SIan Lepore 
28586a55754SIan Lepore 	/*
28686a55754SIan Lepore 	 * I can't find a documented procedure for changing the ARM PLL divisor,
28786a55754SIan Lepore 	 * but some trial and error came up with this:
28886a55754SIan Lepore 	 *  - Set the bypass clock source to REF_CLK_24M (source #0).
28986a55754SIan Lepore 	 *  - Set the PLL into bypass mode; cpu should now be running at 24mhz.
29086a55754SIan Lepore 	 *  - Change the divisor.
29186a55754SIan Lepore 	 *  - Wait for the LOCK bit to come on; it takes ~50 loop iterations.
29201407480SIan Lepore 	 *  - Turn off bypass mode; cpu should now be running at the new speed.
29386a55754SIan Lepore 	 */
294955691feSIan Lepore 	cpufreq_mhz_to_div(sc, op->mhz, &corediv, &plldiv);
29586a55754SIan Lepore 	imx6_anatop_write_4(IMX6_ANALOG_CCM_PLL_ARM_CLR,
29686a55754SIan Lepore 	    IMX6_ANALOG_CCM_PLL_ARM_CLK_SRC_MASK);
29786a55754SIan Lepore 	imx6_anatop_write_4(IMX6_ANALOG_CCM_PLL_ARM_SET,
29886a55754SIan Lepore 	    IMX6_ANALOG_CCM_PLL_ARM_BYPASS);
29986a55754SIan Lepore 
30086a55754SIan Lepore 	wrk32 = imx6_anatop_read_4(IMX6_ANALOG_CCM_PLL_ARM);
30186a55754SIan Lepore 	wrk32 &= ~IMX6_ANALOG_CCM_PLL_ARM_DIV_MASK;
302955691feSIan Lepore 	wrk32 |= plldiv;
30386a55754SIan Lepore 	imx6_anatop_write_4(IMX6_ANALOG_CCM_PLL_ARM, wrk32);
30486a55754SIan Lepore 
30586a55754SIan Lepore 	timeout = 10000;
30686a55754SIan Lepore 	while ((imx6_anatop_read_4(IMX6_ANALOG_CCM_PLL_ARM) &
30786a55754SIan Lepore 	    IMX6_ANALOG_CCM_PLL_ARM_LOCK) == 0)
30886a55754SIan Lepore 		if (--timeout == 0)
30986a55754SIan Lepore 			panic("imx6_set_cpu_clock(): PLL never locked");
31086a55754SIan Lepore 
31186a55754SIan Lepore 	imx6_anatop_write_4(IMX6_ANALOG_CCM_PLL_ARM_CLR,
31286a55754SIan Lepore 	    IMX6_ANALOG_CCM_PLL_ARM_BYPASS);
313955691feSIan Lepore 	imx_ccm_set_cacrr(corediv);
314a3ff7ef6SIan Lepore 
31501407480SIan Lepore 	/* If lowering the frequency, it is now safe to lower the voltage. */
31601407480SIan Lepore 	if (op->mhz < sc->cpu_curmhz)
31701407480SIan Lepore 		vdd_set(sc, op->mv);
31801407480SIan Lepore 	sc->cpu_curmhz = op->mhz;
31901407480SIan Lepore 
32001407480SIan Lepore 	/* Tell the mpcore timer that its frequency has changed. */
32101407480SIan Lepore 	arm_tmr_change_frequency(
32201407480SIan Lepore 	    cpufreq_actual_mhz(sc, sc->cpu_curmhz) * 1000000 / 2);
32301407480SIan Lepore }
32401407480SIan Lepore 
32501407480SIan Lepore static int
cpufreq_sysctl_minmhz(SYSCTL_HANDLER_ARGS)32601407480SIan Lepore cpufreq_sysctl_minmhz(SYSCTL_HANDLER_ARGS)
32701407480SIan Lepore {
32801407480SIan Lepore 	struct imx6_anatop_softc *sc;
32901407480SIan Lepore 	struct oppt * op;
33001407480SIan Lepore 	uint32_t temp;
33101407480SIan Lepore 	int err;
33201407480SIan Lepore 
33301407480SIan Lepore 	sc = arg1;
33401407480SIan Lepore 
33501407480SIan Lepore 	temp = sc->cpu_minmhz;
33601407480SIan Lepore 	err = sysctl_handle_int(oidp, &temp, 0, req);
33701407480SIan Lepore 	if (err != 0 || req->newptr == NULL)
33801407480SIan Lepore 		return (err);
33901407480SIan Lepore 
34001407480SIan Lepore 	op = cpufreq_nearest_oppt(sc, temp);
34101407480SIan Lepore 	if (op->mhz > sc->cpu_maxmhz)
34201407480SIan Lepore 		return (ERANGE);
34301407480SIan Lepore 	else if (op->mhz == sc->cpu_minmhz)
34401407480SIan Lepore 		return (0);
34501407480SIan Lepore 
34601407480SIan Lepore 	/*
34701407480SIan Lepore 	 * Value changed, update softc.  If the new min is higher than the
34801407480SIan Lepore 	 * current speed, raise the current speed to match.
34901407480SIan Lepore 	 */
35001407480SIan Lepore 	sc->cpu_minmhz = op->mhz;
35101407480SIan Lepore 	if (sc->cpu_minmhz > sc->cpu_curmhz) {
35201407480SIan Lepore 		cpufreq_set_clock(sc, op);
35301407480SIan Lepore 	}
35401407480SIan Lepore 	return (err);
35501407480SIan Lepore }
35601407480SIan Lepore 
35701407480SIan Lepore static int
cpufreq_sysctl_maxmhz(SYSCTL_HANDLER_ARGS)35801407480SIan Lepore cpufreq_sysctl_maxmhz(SYSCTL_HANDLER_ARGS)
35901407480SIan Lepore {
36001407480SIan Lepore 	struct imx6_anatop_softc *sc;
36101407480SIan Lepore 	struct oppt * op;
36201407480SIan Lepore 	uint32_t temp;
36301407480SIan Lepore 	int err;
36401407480SIan Lepore 
36501407480SIan Lepore 	sc = arg1;
36601407480SIan Lepore 
36701407480SIan Lepore 	temp = sc->cpu_maxmhz;
36801407480SIan Lepore 	err = sysctl_handle_int(oidp, &temp, 0, req);
36901407480SIan Lepore 	if (err != 0 || req->newptr == NULL)
37001407480SIan Lepore 		return (err);
37101407480SIan Lepore 
37201407480SIan Lepore 	op = cpufreq_nearest_oppt(sc, temp);
37301407480SIan Lepore 	if (op->mhz < sc->cpu_minmhz)
37401407480SIan Lepore 		return (ERANGE);
37501407480SIan Lepore 	else if (op->mhz == sc->cpu_maxmhz)
37601407480SIan Lepore 		return (0);
37701407480SIan Lepore 
37801407480SIan Lepore 	/*
37901407480SIan Lepore 	 *  Value changed, update softc and hardware.  The hardware update is
38001407480SIan Lepore 	 *  unconditional.  We always try to run at max speed, so any change of
38101407480SIan Lepore 	 *  the max means we need to change the current speed too, regardless of
38201407480SIan Lepore 	 *  whether it is higher or lower than the old max.
38301407480SIan Lepore 	 */
38401407480SIan Lepore 	sc->cpu_maxmhz = op->mhz;
38501407480SIan Lepore 	cpufreq_set_clock(sc, op);
38601407480SIan Lepore 
38701407480SIan Lepore 	return (err);
38886a55754SIan Lepore }
38986a55754SIan Lepore 
39086a55754SIan Lepore static void
cpufreq_initialize(struct imx6_anatop_softc * sc)39186a55754SIan Lepore cpufreq_initialize(struct imx6_anatop_softc *sc)
39286a55754SIan Lepore {
39386a55754SIan Lepore 	uint32_t cfg3speed;
39401407480SIan Lepore 	struct oppt * op;
39586a55754SIan Lepore 
396bd6b2f9bSIan Lepore 	SYSCTL_ADD_INT(NULL, SYSCTL_STATIC_CHILDREN(_hw_imx),
39786a55754SIan Lepore 	    OID_AUTO, "cpu_mhz", CTLFLAG_RD, &sc->cpu_curmhz, 0,
39801407480SIan Lepore 	    "CPU frequency");
39901407480SIan Lepore 
400bd6b2f9bSIan Lepore 	SYSCTL_ADD_PROC(NULL, SYSCTL_STATIC_CHILDREN(_hw_imx),
4017029da5cSPawel Biernacki 	    OID_AUTO, "cpu_minmhz",
4027029da5cSPawel Biernacki 	    CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_NOFETCH | CTLFLAG_NEEDGIANT,
403af3b2549SHans Petter Selasky 	    sc, 0, cpufreq_sysctl_minmhz, "IU", "Minimum CPU frequency");
40401407480SIan Lepore 
405bd6b2f9bSIan Lepore 	SYSCTL_ADD_PROC(NULL, SYSCTL_STATIC_CHILDREN(_hw_imx),
4067029da5cSPawel Biernacki 	    OID_AUTO, "cpu_maxmhz",
4077029da5cSPawel Biernacki 	    CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_NOFETCH | CTLFLAG_NEEDGIANT,
408af3b2549SHans Petter Selasky 	    sc, 0, cpufreq_sysctl_maxmhz, "IU", "Maximum CPU frequency");
40901407480SIan Lepore 
410bd6b2f9bSIan Lepore 	SYSCTL_ADD_INT(NULL, SYSCTL_STATIC_CHILDREN(_hw_imx),
41101407480SIan Lepore 	    OID_AUTO, "cpu_maxmhz_hw", CTLFLAG_RD, &sc->cpu_maxmhz_hw, 0,
41201407480SIan Lepore 	    "Maximum CPU frequency allowed by hardware");
41301407480SIan Lepore 
414bd6b2f9bSIan Lepore 	SYSCTL_ADD_INT(NULL, SYSCTL_STATIC_CHILDREN(_hw_imx),
415453efe58SIan Lepore 	    OID_AUTO, "cpu_overclock_enable", CTLFLAG_RWTUN,
416453efe58SIan Lepore 	    &sc->cpu_overclock_enable, 0,
41701407480SIan Lepore 	    "Allow setting CPU frequency higher than cpu_maxmhz_hw");
41886a55754SIan Lepore 
41986a55754SIan Lepore 	/*
42086a55754SIan Lepore 	 * XXX 24mhz shouldn't be hard-coded, should get this from imx6_ccm
42186a55754SIan Lepore 	 * (even though in the real world it will always be 24mhz).  Oh wait a
42286a55754SIan Lepore 	 * sec, I never wrote imx6_ccm.
42386a55754SIan Lepore 	 */
42401407480SIan Lepore 	sc->refosc_mhz = 24;
42586a55754SIan Lepore 
42686a55754SIan Lepore 	/*
42786a55754SIan Lepore 	 * Get the maximum speed this cpu can be set to.  The values in the
42886a55754SIan Lepore 	 * OCOTP CFG3 register are not documented in the reference manual.
42986a55754SIan Lepore 	 * The following info was in an archived email found via web search:
43086a55754SIan Lepore 	 *   - 2b'11: 1200000000Hz;
43186a55754SIan Lepore 	 *   - 2b'10: 996000000Hz;
43286a55754SIan Lepore 	 *   - 2b'01: 852000000Hz; -- i.MX6Q Only, exclusive with 996MHz.
43386a55754SIan Lepore 	 *   - 2b'00: 792000000Hz;
43401407480SIan Lepore 	 * The default hardware max speed can be overridden by a tunable.
43586a55754SIan Lepore 	 */
43686a55754SIan Lepore 	cfg3speed = (fsl_ocotp_read_4(FSL_OCOTP_CFG3) &
43786a55754SIan Lepore 	    FSL_OCOTP_CFG3_SPEED_MASK) >> FSL_OCOTP_CFG3_SPEED_SHIFT;
43801407480SIan Lepore 	sc->cpu_maxmhz_hw = imx6_ocotp_mhz_tab[cfg3speed];
43901407480SIan Lepore 	sc->cpu_maxmhz = sc->cpu_maxmhz_hw;
44086a55754SIan Lepore 
44101407480SIan Lepore 	TUNABLE_INT_FETCH("hw.imx6.cpu_minmhz", &sc->cpu_minmhz);
44201407480SIan Lepore 	op = cpufreq_nearest_oppt(sc, sc->cpu_minmhz);
44301407480SIan Lepore 	sc->cpu_minmhz = op->mhz;
44401407480SIan Lepore 	sc->cpu_minmv = op->mv;
44501407480SIan Lepore 
44601407480SIan Lepore 	TUNABLE_INT_FETCH("hw.imx6.cpu_maxmhz", &sc->cpu_maxmhz);
44701407480SIan Lepore 	op = cpufreq_nearest_oppt(sc, sc->cpu_maxmhz);
44801407480SIan Lepore 	sc->cpu_maxmhz = op->mhz;
44901407480SIan Lepore 	sc->cpu_maxmv = op->mv;
45086a55754SIan Lepore 
45186a55754SIan Lepore 	/*
45286a55754SIan Lepore 	 * Set the CPU to maximum speed.
45386a55754SIan Lepore 	 *
45486a55754SIan Lepore 	 * We won't have thermal throttling until interrupts are enabled, but we
45586a55754SIan Lepore 	 * want to run at full speed through all the device init stuff.  This
45686a55754SIan Lepore 	 * basically assumes that a single core can't overheat before interrupts
45786a55754SIan Lepore 	 * are enabled; empirical testing shows that to be a safe assumption.
45886a55754SIan Lepore 	 */
45901407480SIan Lepore 	cpufreq_set_clock(sc, op);
46086a55754SIan Lepore }
46186a55754SIan Lepore 
46286a55754SIan Lepore static inline uint32_t
temp_from_count(struct imx6_anatop_softc * sc,uint32_t count)46386a55754SIan Lepore temp_from_count(struct imx6_anatop_softc *sc, uint32_t count)
46486a55754SIan Lepore {
46586a55754SIan Lepore 
46686a55754SIan Lepore 	return (((sc->temp_high_val - (count - sc->temp_high_cnt) *
46786a55754SIan Lepore 	    (sc->temp_high_val - 250) /
46886a55754SIan Lepore 	    (sc->temp_room_cnt - sc->temp_high_cnt))));
46986a55754SIan Lepore }
47086a55754SIan Lepore 
47186a55754SIan Lepore static inline uint32_t
temp_to_count(struct imx6_anatop_softc * sc,uint32_t temp)47286a55754SIan Lepore temp_to_count(struct imx6_anatop_softc *sc, uint32_t temp)
47386a55754SIan Lepore {
47486a55754SIan Lepore 
47586a55754SIan Lepore 	return ((sc->temp_room_cnt - sc->temp_high_cnt) *
47686a55754SIan Lepore 	    (sc->temp_high_val - temp) / (sc->temp_high_val - 250) +
47786a55754SIan Lepore 	    sc->temp_high_cnt);
47886a55754SIan Lepore }
47986a55754SIan Lepore 
48086a55754SIan Lepore static void
temp_update_count(struct imx6_anatop_softc * sc)48186a55754SIan Lepore temp_update_count(struct imx6_anatop_softc *sc)
48286a55754SIan Lepore {
48386a55754SIan Lepore 	uint32_t val;
48486a55754SIan Lepore 
48586a55754SIan Lepore 	val = imx6_anatop_read_4(IMX6_ANALOG_TEMPMON_TEMPSENSE0);
48686a55754SIan Lepore 	if (!(val & IMX6_ANALOG_TEMPMON_TEMPSENSE0_VALID))
48786a55754SIan Lepore 		return;
48886a55754SIan Lepore 	sc->temp_last_cnt =
48986a55754SIan Lepore 	    (val & IMX6_ANALOG_TEMPMON_TEMPSENSE0_TEMP_CNT_MASK) >>
49086a55754SIan Lepore 	    IMX6_ANALOG_TEMPMON_TEMPSENSE0_TEMP_CNT_SHIFT;
49186a55754SIan Lepore }
49286a55754SIan Lepore 
49386a55754SIan Lepore static int
temp_sysctl_handler(SYSCTL_HANDLER_ARGS)49486a55754SIan Lepore temp_sysctl_handler(SYSCTL_HANDLER_ARGS)
49586a55754SIan Lepore {
49686a55754SIan Lepore 	struct imx6_anatop_softc *sc = arg1;
49786a55754SIan Lepore 	uint32_t t;
49886a55754SIan Lepore 
49986a55754SIan Lepore 	temp_update_count(sc);
50086a55754SIan Lepore 
50186a55754SIan Lepore 	t = temp_from_count(sc, sc->temp_last_cnt) + TZ_ZEROC;
50286a55754SIan Lepore 
50386a55754SIan Lepore 	return (sysctl_handle_int(oidp, &t, 0, req));
50486a55754SIan Lepore }
50586a55754SIan Lepore 
50686a55754SIan Lepore static int
temp_throttle_sysctl_handler(SYSCTL_HANDLER_ARGS)50786a55754SIan Lepore temp_throttle_sysctl_handler(SYSCTL_HANDLER_ARGS)
50886a55754SIan Lepore {
50986a55754SIan Lepore 	struct imx6_anatop_softc *sc = arg1;
51086a55754SIan Lepore 	int err;
51186a55754SIan Lepore 	uint32_t temp;
51286a55754SIan Lepore 
51386a55754SIan Lepore 	temp = sc->temp_throttle_val + TZ_ZEROC;
51486a55754SIan Lepore 	err = sysctl_handle_int(oidp, &temp, 0, req);
51586a55754SIan Lepore 	if (temp < TZ_ZEROC)
51686a55754SIan Lepore 		return (ERANGE);
51786a55754SIan Lepore 	temp -= TZ_ZEROC;
51886a55754SIan Lepore 	if (err != 0 || req->newptr == NULL || temp == sc->temp_throttle_val)
51986a55754SIan Lepore 		return (err);
52086a55754SIan Lepore 
52186a55754SIan Lepore 	/* Value changed, update counts in softc and hardware. */
52286a55754SIan Lepore 	sc->temp_throttle_val = temp;
52386a55754SIan Lepore 	sc->temp_throttle_trigger_cnt = temp_to_count(sc, sc->temp_throttle_val);
52486a55754SIan Lepore 	sc->temp_throttle_reset_cnt = temp_to_count(sc, sc->temp_throttle_val - 100);
52586a55754SIan Lepore 	imx6_anatop_write_4(IMX6_ANALOG_TEMPMON_TEMPSENSE0_CLR,
52686a55754SIan Lepore 	    IMX6_ANALOG_TEMPMON_TEMPSENSE0_ALARM_MASK);
52786a55754SIan Lepore 	imx6_anatop_write_4(IMX6_ANALOG_TEMPMON_TEMPSENSE0_SET,
52886a55754SIan Lepore 	    (sc->temp_throttle_trigger_cnt <<
52986a55754SIan Lepore 	     IMX6_ANALOG_TEMPMON_TEMPSENSE0_ALARM_SHIFT));
53086a55754SIan Lepore 	return (err);
53186a55754SIan Lepore }
53286a55754SIan Lepore 
53386a55754SIan Lepore static void
tempmon_gofast(struct imx6_anatop_softc * sc)53486a55754SIan Lepore tempmon_gofast(struct imx6_anatop_softc *sc)
53586a55754SIan Lepore {
53686a55754SIan Lepore 
53701407480SIan Lepore 	if (sc->cpu_curmhz < sc->cpu_maxmhz) {
53801407480SIan Lepore 		cpufreq_set_clock(sc, cpufreq_nearest_oppt(sc, sc->cpu_maxmhz));
53986a55754SIan Lepore 	}
54086a55754SIan Lepore }
54186a55754SIan Lepore 
54286a55754SIan Lepore static void
tempmon_goslow(struct imx6_anatop_softc * sc)54386a55754SIan Lepore tempmon_goslow(struct imx6_anatop_softc *sc)
54486a55754SIan Lepore {
54586a55754SIan Lepore 
54601407480SIan Lepore 	if (sc->cpu_curmhz > sc->cpu_minmhz) {
54701407480SIan Lepore 		cpufreq_set_clock(sc, cpufreq_nearest_oppt(sc, sc->cpu_minmhz));
54886a55754SIan Lepore 	}
54986a55754SIan Lepore }
55086a55754SIan Lepore 
55186a55754SIan Lepore static int
tempmon_intr(void * arg)55286a55754SIan Lepore tempmon_intr(void *arg)
55386a55754SIan Lepore {
55486a55754SIan Lepore 	struct imx6_anatop_softc *sc = arg;
55586a55754SIan Lepore 
55686a55754SIan Lepore 	/*
55786a55754SIan Lepore 	 * XXX Note that this code doesn't currently run (for some mysterious
55886a55754SIan Lepore 	 * reason we just never get an interrupt), so the real monitoring is
55986a55754SIan Lepore 	 * done by tempmon_throttle_check().
56086a55754SIan Lepore 	 */
56186a55754SIan Lepore 	tempmon_goslow(sc);
56286a55754SIan Lepore 	/* XXX Schedule callout to speed back up eventually. */
56386a55754SIan Lepore 	return (FILTER_HANDLED);
56486a55754SIan Lepore }
56586a55754SIan Lepore 
56686a55754SIan Lepore static void
tempmon_throttle_check(void * arg)56786a55754SIan Lepore tempmon_throttle_check(void *arg)
56886a55754SIan Lepore {
56986a55754SIan Lepore 	struct imx6_anatop_softc *sc = arg;
57086a55754SIan Lepore 
57186a55754SIan Lepore 	/* Lower counts are higher temperatures. */
57286a55754SIan Lepore 	if (sc->temp_last_cnt < sc->temp_throttle_trigger_cnt)
57386a55754SIan Lepore 		tempmon_goslow(sc);
57486a55754SIan Lepore 	else if (sc->temp_last_cnt > (sc->temp_throttle_reset_cnt))
57586a55754SIan Lepore 		tempmon_gofast(sc);
57686a55754SIan Lepore 
57786a55754SIan Lepore 	callout_reset_sbt(&sc->temp_throttle_callout, sc->temp_throttle_delay,
57886a55754SIan Lepore 		0, tempmon_throttle_check, sc, 0);
57986a55754SIan Lepore 
58086a55754SIan Lepore }
58186a55754SIan Lepore 
58286a55754SIan Lepore static void
initialize_tempmon(struct imx6_anatop_softc * sc)58386a55754SIan Lepore initialize_tempmon(struct imx6_anatop_softc *sc)
58486a55754SIan Lepore {
58586a55754SIan Lepore 	uint32_t cal;
58686a55754SIan Lepore 
58786a55754SIan Lepore 	/*
58886a55754SIan Lepore 	 * Fetch calibration data: a sensor count at room temperature (25C),
58986a55754SIan Lepore 	 * a sensor count at a high temperature, and that temperature
59086a55754SIan Lepore 	 */
59186a55754SIan Lepore 	cal = fsl_ocotp_read_4(FSL_OCOTP_ANA1);
59286a55754SIan Lepore 	sc->temp_room_cnt = (cal & 0xFFF00000) >> 20;
59386a55754SIan Lepore 	sc->temp_high_cnt = (cal & 0x000FFF00) >> 8;
59486a55754SIan Lepore 	sc->temp_high_val = (cal & 0x000000FF) * 10;
59586a55754SIan Lepore 
59686a55754SIan Lepore 	/*
59786a55754SIan Lepore 	 * Throttle to a lower cpu freq at 10C below the "hot" temperature, and
59886a55754SIan Lepore 	 * reset back to max cpu freq at 5C below the trigger.
59986a55754SIan Lepore 	 */
60086a55754SIan Lepore 	sc->temp_throttle_val = sc->temp_high_val - 100;
60186a55754SIan Lepore 	sc->temp_throttle_trigger_cnt =
60286a55754SIan Lepore 	    temp_to_count(sc, sc->temp_throttle_val);
60386a55754SIan Lepore 	sc->temp_throttle_reset_cnt =
60486a55754SIan Lepore 	    temp_to_count(sc, sc->temp_throttle_val - 50);
60586a55754SIan Lepore 
60686a55754SIan Lepore 	/*
60786a55754SIan Lepore 	 * Set the sensor to sample automatically at 16Hz (32.768KHz/0x800), set
60886a55754SIan Lepore 	 * the throttle count, and begin making measurements.
60986a55754SIan Lepore 	 */
61086a55754SIan Lepore 	imx6_anatop_write_4(IMX6_ANALOG_TEMPMON_TEMPSENSE1, 0x0800);
61186a55754SIan Lepore 	imx6_anatop_write_4(IMX6_ANALOG_TEMPMON_TEMPSENSE0,
61286a55754SIan Lepore 	    (sc->temp_throttle_trigger_cnt <<
61386a55754SIan Lepore 	    IMX6_ANALOG_TEMPMON_TEMPSENSE0_ALARM_SHIFT) |
61486a55754SIan Lepore 	    IMX6_ANALOG_TEMPMON_TEMPSENSE0_MEASURE);
61586a55754SIan Lepore 
61686a55754SIan Lepore 	/*
61786a55754SIan Lepore 	 * XXX Note that the alarm-interrupt feature isn't working yet, so
61886a55754SIan Lepore 	 * we'll use a callout handler to check at 10Hz.  Make sure we have an
61986a55754SIan Lepore 	 * initial temperature reading before starting up the callouts so we
62086a55754SIan Lepore 	 * don't get a bogus reading of zero.
62186a55754SIan Lepore 	 */
62286a55754SIan Lepore 	while (sc->temp_last_cnt == 0)
62386a55754SIan Lepore 		temp_update_count(sc);
62486a55754SIan Lepore 	sc->temp_throttle_delay = 100 * SBT_1MS;
62586a55754SIan Lepore 	callout_init(&sc->temp_throttle_callout, 0);
62686a55754SIan Lepore 	callout_reset_sbt(&sc->temp_throttle_callout, sc->temp_throttle_delay,
62786a55754SIan Lepore 	    0, tempmon_throttle_check, sc, 0);
62886a55754SIan Lepore 
629bd6b2f9bSIan Lepore 	SYSCTL_ADD_PROC(NULL, SYSCTL_STATIC_CHILDREN(_hw_imx),
6307029da5cSPawel Biernacki 	    OID_AUTO, "temperature",
6317029da5cSPawel Biernacki 	    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_NEEDGIANT, sc, 0,
63286a55754SIan Lepore 	    temp_sysctl_handler, "IK", "Current die temperature");
633bd6b2f9bSIan Lepore 	SYSCTL_ADD_PROC(NULL, SYSCTL_STATIC_CHILDREN(_hw_imx),
6347029da5cSPawel Biernacki 	    OID_AUTO, "throttle_temperature",
6357029da5cSPawel Biernacki 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, sc,
63686a55754SIan Lepore 	    0, temp_throttle_sysctl_handler, "IK",
63786a55754SIan Lepore 	    "Throttle CPU when exceeding this temperature");
6389808ebfaSIan Lepore }
6399808ebfaSIan Lepore 
64060f47da9SIan Lepore static void
intr_setup(void * arg)64160f47da9SIan Lepore intr_setup(void *arg)
64260f47da9SIan Lepore {
6434d7f3703SSvatopluk Kraus 	int rid;
64460f47da9SIan Lepore 	struct imx6_anatop_softc *sc;
64560f47da9SIan Lepore 
64660f47da9SIan Lepore 	sc = arg;
6474d7f3703SSvatopluk Kraus 	rid = 0;
6484d7f3703SSvatopluk Kraus 	sc->res[IRQRES] = bus_alloc_resource_any(sc->dev, SYS_RES_IRQ, &rid,
6494d7f3703SSvatopluk Kraus 	    RF_ACTIVE);
6504d7f3703SSvatopluk Kraus 	if (sc->res[IRQRES] != NULL) {
6514d7f3703SSvatopluk Kraus 		bus_setup_intr(sc->dev, sc->res[IRQRES],
6524d7f3703SSvatopluk Kraus 		    INTR_TYPE_MISC | INTR_MPSAFE, tempmon_intr, NULL, sc,
6534d7f3703SSvatopluk Kraus 		    &sc->temp_intrhand);
6544d7f3703SSvatopluk Kraus 	} else {
6554d7f3703SSvatopluk Kraus 		device_printf(sc->dev, "Cannot allocate IRQ resource\n");
6564d7f3703SSvatopluk Kraus 	}
65760f47da9SIan Lepore 	config_intrhook_disestablish(&sc->intr_setup_hook);
65860f47da9SIan Lepore }
65960f47da9SIan Lepore 
660a2d27ff8SIan Lepore static void
imx6_anatop_new_pass(device_t dev)661a2d27ff8SIan Lepore imx6_anatop_new_pass(device_t dev)
662a2d27ff8SIan Lepore {
663a2d27ff8SIan Lepore 	struct imx6_anatop_softc *sc;
664a2d27ff8SIan Lepore 	const int cpu_init_pass = BUS_PASS_CPU + BUS_PASS_ORDER_MIDDLE;
665a2d27ff8SIan Lepore 
666a2d27ff8SIan Lepore 	/*
667a2d27ff8SIan Lepore 	 * We attach during BUS_PASS_BUS (because some day we will be a
668a2d27ff8SIan Lepore 	 * simplebus that has regulator devices as children), but some of our
669a2d27ff8SIan Lepore 	 * init work cannot be done until BUS_PASS_CPU (we rely on other devices
670a2d27ff8SIan Lepore 	 * that attach on the CPU pass).
671a2d27ff8SIan Lepore 	 */
672a2d27ff8SIan Lepore 	sc = device_get_softc(dev);
673*3514f989SElliott Mitchell 	if (!sc->cpu_init_done && bus_get_pass() >= cpu_init_pass) {
674a2d27ff8SIan Lepore 		sc->cpu_init_done = true;
675a2d27ff8SIan Lepore 		cpufreq_initialize(sc);
676a2d27ff8SIan Lepore 		initialize_tempmon(sc);
677a2d27ff8SIan Lepore 		if (bootverbose) {
678a2d27ff8SIan Lepore 			device_printf(sc->dev, "CPU %uMHz @ %umV\n",
679a2d27ff8SIan Lepore 			    sc->cpu_curmhz, sc->cpu_curmv);
680a2d27ff8SIan Lepore 		}
681a2d27ff8SIan Lepore 	}
682a2d27ff8SIan Lepore 	bus_generic_new_pass(dev);
683a2d27ff8SIan Lepore }
684a2d27ff8SIan Lepore 
6859808ebfaSIan Lepore static int
imx6_anatop_detach(device_t dev)6869808ebfaSIan Lepore imx6_anatop_detach(device_t dev)
6879808ebfaSIan Lepore {
6889808ebfaSIan Lepore 
68960f47da9SIan Lepore 	/* This device can never detach. */
69086a55754SIan Lepore 	return (EBUSY);
6919808ebfaSIan Lepore }
6929808ebfaSIan Lepore 
6939808ebfaSIan Lepore static int
imx6_anatop_attach(device_t dev)6949808ebfaSIan Lepore imx6_anatop_attach(device_t dev)
6959808ebfaSIan Lepore {
6969808ebfaSIan Lepore 	struct imx6_anatop_softc *sc;
69786a55754SIan Lepore 	int err;
6989808ebfaSIan Lepore 
6999808ebfaSIan Lepore 	sc = device_get_softc(dev);
70086a55754SIan Lepore 	sc->dev = dev;
7019808ebfaSIan Lepore 
7029808ebfaSIan Lepore 	/* Allocate bus_space resources. */
70386a55754SIan Lepore 	if (bus_alloc_resources(dev, imx6_anatop_spec, sc->res)) {
70486a55754SIan Lepore 		device_printf(dev, "Cannot allocate resources\n");
7059808ebfaSIan Lepore 		err = ENXIO;
7069808ebfaSIan Lepore 		goto out;
7079808ebfaSIan Lepore 	}
7089808ebfaSIan Lepore 
70960f47da9SIan Lepore 	sc->intr_setup_hook.ich_func = intr_setup;
71060f47da9SIan Lepore 	sc->intr_setup_hook.ich_arg = sc;
71160f47da9SIan Lepore 	config_intrhook_establish(&sc->intr_setup_hook);
71286a55754SIan Lepore 
713f85153feSIan Lepore 	SYSCTL_ADD_UINT(device_get_sysctl_ctx(sc->dev),
714f85153feSIan Lepore 	    SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev)),
715f85153feSIan Lepore 	    OID_AUTO, "cpu_voltage", CTLFLAG_RD,
716f85153feSIan Lepore 	    &sc->cpu_curmv, 0, "Current CPU voltage in millivolts");
717f85153feSIan Lepore 
7189808ebfaSIan Lepore 	imx6_anatop_sc = sc;
71986a55754SIan Lepore 
72086a55754SIan Lepore 	/*
72186a55754SIan Lepore 	 * Other code seen on the net sets this SELFBIASOFF flag around the same
72286a55754SIan Lepore 	 * time the temperature sensor is set up, although it's unclear how the
72386a55754SIan Lepore 	 * two are related (if at all).
72486a55754SIan Lepore 	 */
72586a55754SIan Lepore 	imx6_anatop_write_4(IMX6_ANALOG_PMU_MISC0_SET,
72686a55754SIan Lepore 	    IMX6_ANALOG_PMU_MISC0_SELFBIASOFF);
72786a55754SIan Lepore 
728a2d27ff8SIan Lepore 	/*
729a2d27ff8SIan Lepore 	 * Some day, when we're ready to deal with the actual anatop regulators
730a2d27ff8SIan Lepore 	 * that are described in fdt data as children of this "bus", this would
731a2d27ff8SIan Lepore 	 * be the place to invoke a simplebus helper routine to instantiate the
732a2d27ff8SIan Lepore 	 * children from the fdt data.
733a2d27ff8SIan Lepore 	 */
73486a55754SIan Lepore 
7359808ebfaSIan Lepore 	err = 0;
7369808ebfaSIan Lepore 
7379808ebfaSIan Lepore out:
7389808ebfaSIan Lepore 
73986a55754SIan Lepore 	if (err != 0) {
74086a55754SIan Lepore 		bus_release_resources(dev, imx6_anatop_spec, sc->res);
74186a55754SIan Lepore 	}
7429808ebfaSIan Lepore 
7439808ebfaSIan Lepore 	return (err);
7449808ebfaSIan Lepore }
7459808ebfaSIan Lepore 
746f0583578SRuslan Bukin uint32_t
pll4_configure_output(uint32_t mfi,uint32_t mfn,uint32_t mfd)747f0583578SRuslan Bukin pll4_configure_output(uint32_t mfi, uint32_t mfn, uint32_t mfd)
748f0583578SRuslan Bukin {
749f0583578SRuslan Bukin 	int reg;
750f0583578SRuslan Bukin 
751f0583578SRuslan Bukin 	/*
752f0583578SRuslan Bukin 	 * Audio PLL (PLL4).
753f0583578SRuslan Bukin 	 * PLL output frequency = Fref * (DIV_SELECT + NUM/DENOM)
754f0583578SRuslan Bukin 	 */
755f0583578SRuslan Bukin 
756f0583578SRuslan Bukin 	reg = (IMX6_ANALOG_CCM_PLL_AUDIO_ENABLE);
757f0583578SRuslan Bukin 	reg &= ~(IMX6_ANALOG_CCM_PLL_AUDIO_DIV_SELECT_MASK << \
758f0583578SRuslan Bukin 		IMX6_ANALOG_CCM_PLL_AUDIO_DIV_SELECT_SHIFT);
759f0583578SRuslan Bukin 	reg |= (mfi << IMX6_ANALOG_CCM_PLL_AUDIO_DIV_SELECT_SHIFT);
760f0583578SRuslan Bukin 	imx6_anatop_write_4(IMX6_ANALOG_CCM_PLL_AUDIO, reg);
761f0583578SRuslan Bukin 	imx6_anatop_write_4(IMX6_ANALOG_CCM_PLL_AUDIO_NUM, mfn);
762f0583578SRuslan Bukin 	imx6_anatop_write_4(IMX6_ANALOG_CCM_PLL_AUDIO_DENOM, mfd);
763f0583578SRuslan Bukin 
764f0583578SRuslan Bukin 	return (0);
765f0583578SRuslan Bukin }
766f0583578SRuslan Bukin 
7679808ebfaSIan Lepore static int
imx6_anatop_probe(device_t dev)7689808ebfaSIan Lepore imx6_anatop_probe(device_t dev)
7699808ebfaSIan Lepore {
7709808ebfaSIan Lepore 
771add35ed5SIan Lepore 	if (!ofw_bus_status_okay(dev))
772add35ed5SIan Lepore 		return (ENXIO);
773add35ed5SIan Lepore 
7749808ebfaSIan Lepore 	if (ofw_bus_is_compatible(dev, "fsl,imx6q-anatop") == 0)
7759808ebfaSIan Lepore 		return (ENXIO);
7769808ebfaSIan Lepore 
7779808ebfaSIan Lepore 	device_set_desc(dev, "Freescale i.MX6 Analog PLLs and Power");
7789808ebfaSIan Lepore 
7799808ebfaSIan Lepore 	return (BUS_PROBE_DEFAULT);
7809808ebfaSIan Lepore }
7819808ebfaSIan Lepore 
78286a55754SIan Lepore uint32_t
imx6_get_cpu_clock(void)78359249a51SAndrew Turner imx6_get_cpu_clock(void)
78486a55754SIan Lepore {
785955691feSIan Lepore 	uint32_t corediv, plldiv;
78686a55754SIan Lepore 
787955691feSIan Lepore 	corediv = imx_ccm_get_cacrr();
788955691feSIan Lepore 	plldiv = imx6_anatop_read_4(IMX6_ANALOG_CCM_PLL_ARM) &
78986a55754SIan Lepore 	    IMX6_ANALOG_CCM_PLL_ARM_DIV_MASK;
790955691feSIan Lepore 	return (cpufreq_mhz_from_div(imx6_anatop_sc, corediv, plldiv));
79186a55754SIan Lepore }
79286a55754SIan Lepore 
7939808ebfaSIan Lepore static device_method_t imx6_anatop_methods[] = {
7949808ebfaSIan Lepore 	/* Device interface */
7959808ebfaSIan Lepore 	DEVMETHOD(device_probe,  imx6_anatop_probe),
7969808ebfaSIan Lepore 	DEVMETHOD(device_attach, imx6_anatop_attach),
7979808ebfaSIan Lepore 	DEVMETHOD(device_detach, imx6_anatop_detach),
7989808ebfaSIan Lepore 
799a2d27ff8SIan Lepore 	/* Bus interface */
800a2d27ff8SIan Lepore 	DEVMETHOD(bus_new_pass,  imx6_anatop_new_pass),
801a2d27ff8SIan Lepore 
8029808ebfaSIan Lepore 	DEVMETHOD_END
8039808ebfaSIan Lepore };
8049808ebfaSIan Lepore 
8059808ebfaSIan Lepore static driver_t imx6_anatop_driver = {
8069808ebfaSIan Lepore 	"imx6_anatop",
8079808ebfaSIan Lepore 	imx6_anatop_methods,
8089808ebfaSIan Lepore 	sizeof(struct imx6_anatop_softc)
8099808ebfaSIan Lepore };
8109808ebfaSIan Lepore 
811ea538dabSJohn Baldwin EARLY_DRIVER_MODULE(imx6_anatop, simplebus, imx6_anatop_driver, 0, 0,
812ea538dabSJohn Baldwin     BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE);
813ea538dabSJohn Baldwin EARLY_DRIVER_MODULE(imx6_anatop, ofwbus, imx6_anatop_driver, 0, 0,
814ea538dabSJohn Baldwin     BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE);
815