xref: /rk3399_rockchip-uboot/arch/arm/include/asm/arch-tegra/clock.h (revision 04b8e8e7452a4cd00003e33a5a736bd077ff3471)
1150c2493STom Warren /*
2150c2493STom Warren  * Copyright (c) 2011 The Chromium OS Authors.
3150c2493STom Warren  *
41a459660SWolfgang Denk  * SPDX-License-Identifier:	GPL-2.0+
5150c2493STom Warren  */
6150c2493STom Warren 
7150c2493STom Warren /* Tegra clock control functions */
8150c2493STom Warren 
9dc89ad14STom Warren #ifndef _TEGRA_CLOCK_H_
10dc89ad14STom Warren #define _TEGRA_CLOCK_H_
11150c2493STom Warren 
12150c2493STom Warren /* Set of oscillator frequencies supported in the internal API. */
13150c2493STom Warren enum clock_osc_freq {
14150c2493STom Warren 	/* All in MHz, so 13_0 is 13.0MHz */
15150c2493STom Warren 	CLOCK_OSC_FREQ_13_0,
16150c2493STom Warren 	CLOCK_OSC_FREQ_19_2,
17150c2493STom Warren 	CLOCK_OSC_FREQ_12_0,
18150c2493STom Warren 	CLOCK_OSC_FREQ_26_0,
19150c2493STom Warren 
20150c2493STom Warren 	CLOCK_OSC_FREQ_COUNT,
21150c2493STom Warren };
22150c2493STom Warren 
235916a36eSStephen Warren /*
245916a36eSStephen Warren  * Note that no Tegra clock register actually uses all of bits 31:28 as
255916a36eSStephen Warren  * the mux field. Rather, bits 30:28, 29:28, or 28 are used. However, in
265916a36eSStephen Warren  * those cases, nothing is stored in the bits about the mux field, so it's
275916a36eSStephen Warren  * safe to pretend that the mux field extends all the way to the end of the
285916a36eSStephen Warren  * register. As such, the U-Boot clock driver is currently a bit lazy, and
295916a36eSStephen Warren  * doesn't distinguish between 31:28, 30:28, 29:28 and 28; it just lumps
305916a36eSStephen Warren  * them all together and pretends they're all 31:28.
315916a36eSStephen Warren  */
320b01b53aSTom Warren enum {
33*04b8e8e7SStephen Warren 	MASK_BITS_31_30,
340b01b53aSTom Warren 	MASK_BITS_31_29,
355916a36eSStephen Warren 	MASK_BITS_31_28,
360b01b53aSTom Warren };
370b01b53aSTom Warren 
38150c2493STom Warren #include <asm/arch/clock-tables.h>
39150c2493STom Warren /* PLL stabilization delay in usec */
40150c2493STom Warren #define CLOCK_PLL_STABLE_DELAY_US 300
41150c2493STom Warren 
42150c2493STom Warren /* return the current oscillator clock frequency */
43150c2493STom Warren enum clock_osc_freq clock_get_osc_freq(void);
44150c2493STom Warren 
45150c2493STom Warren /**
46150c2493STom Warren  * Start PLL using the provided configuration parameters.
47150c2493STom Warren  *
48150c2493STom Warren  * @param id	clock id
49150c2493STom Warren  * @param divm	input divider
50150c2493STom Warren  * @param divn	feedback divider
51150c2493STom Warren  * @param divp	post divider 2^n
52150c2493STom Warren  * @param cpcon	charge pump setup control
53150c2493STom Warren  * @param lfcon	loop filter setup control
54150c2493STom Warren  *
55150c2493STom Warren  * @returns monotonic time in us that the PLL will be stable
56150c2493STom Warren  */
57150c2493STom Warren unsigned long clock_start_pll(enum clock_id id, u32 divm, u32 divn,
58150c2493STom Warren 		u32 divp, u32 cpcon, u32 lfcon);
59150c2493STom Warren 
60150c2493STom Warren /**
6165530a84SLucas Stach  * Set PLL output frequency
6265530a84SLucas Stach  *
6365530a84SLucas Stach  * @param clkid	clock id
6465530a84SLucas Stach  * @param pllout	pll output id
6565530a84SLucas Stach  * @param rate		desired output rate
6665530a84SLucas Stach  *
6765530a84SLucas Stach  * @return 0 if ok, -1 on error (invalid clock id or no suitable divider)
6865530a84SLucas Stach  */
6965530a84SLucas Stach int clock_set_pllout(enum clock_id clkid, enum pll_out_id pllout,
7065530a84SLucas Stach 		unsigned rate);
7165530a84SLucas Stach 
7265530a84SLucas Stach /**
73150c2493STom Warren  * Read low-level parameters of a PLL.
74150c2493STom Warren  *
75150c2493STom Warren  * @param id	clock id to read (note: USB is not supported)
76150c2493STom Warren  * @param divm	returns input divider
77150c2493STom Warren  * @param divn	returns feedback divider
78150c2493STom Warren  * @param divp	returns post divider 2^n
79150c2493STom Warren  * @param cpcon	returns charge pump setup control
80150c2493STom Warren  * @param lfcon	returns loop filter setup control
81150c2493STom Warren  *
82150c2493STom Warren  * @returns 0 if ok, -1 on error (invalid clock id)
83150c2493STom Warren  */
84150c2493STom Warren int clock_ll_read_pll(enum clock_id clkid, u32 *divm, u32 *divn,
85150c2493STom Warren 		u32 *divp, u32 *cpcon, u32 *lfcon);
86150c2493STom Warren 
87150c2493STom Warren /*
88150c2493STom Warren  * Enable a clock
89150c2493STom Warren  *
90150c2493STom Warren  * @param id	clock id
91150c2493STom Warren  */
92150c2493STom Warren void clock_enable(enum periph_id clkid);
93150c2493STom Warren 
94150c2493STom Warren /*
95150c2493STom Warren  * Disable a clock
96150c2493STom Warren  *
97150c2493STom Warren  * @param id	clock id
98150c2493STom Warren  */
99150c2493STom Warren void clock_disable(enum periph_id clkid);
100150c2493STom Warren 
101150c2493STom Warren /*
102150c2493STom Warren  * Set whether a clock is enabled or disabled.
103150c2493STom Warren  *
104150c2493STom Warren  * @param id		clock id
105150c2493STom Warren  * @param enable	1 to enable, 0 to disable
106150c2493STom Warren  */
107150c2493STom Warren void clock_set_enable(enum periph_id clkid, int enable);
108150c2493STom Warren 
109150c2493STom Warren /**
110150c2493STom Warren  * Reset a peripheral. This puts it in reset, waits for a delay, then takes
111150c2493STom Warren  * it out of reset and waits for th delay again.
112150c2493STom Warren  *
113150c2493STom Warren  * @param periph_id	peripheral to reset
114150c2493STom Warren  * @param us_delay	time to delay in microseconds
115150c2493STom Warren  */
116150c2493STom Warren void reset_periph(enum periph_id periph_id, int us_delay);
117150c2493STom Warren 
118150c2493STom Warren /**
119150c2493STom Warren  * Put a peripheral into or out of reset.
120150c2493STom Warren  *
121150c2493STom Warren  * @param periph_id	peripheral to reset
122150c2493STom Warren  * @param enable	1 to put into reset, 0 to take out of reset
123150c2493STom Warren  */
124150c2493STom Warren void reset_set_enable(enum periph_id periph_id, int enable);
125150c2493STom Warren 
126150c2493STom Warren 
127150c2493STom Warren /* CLK_RST_CONTROLLER_RST_CPU_CMPLX_SET/CLR_0 */
128150c2493STom Warren enum crc_reset_id {
129150c2493STom Warren 	/* Things we can hold in reset for each CPU */
130150c2493STom Warren 	crc_rst_cpu = 1,
131766afc3dSAlban Bedel 	crc_rst_de = 1 << 4,	/* What is de? */
132766afc3dSAlban Bedel 	crc_rst_watchdog = 1 << 8,
133766afc3dSAlban Bedel 	crc_rst_debug = 1 << 12,
134150c2493STom Warren };
135150c2493STom Warren 
136150c2493STom Warren /**
137150c2493STom Warren  * Put parts of the CPU complex into or out of reset.\
138150c2493STom Warren  *
139dc89ad14STom Warren  * @param cpu		cpu number (0 or 1 on Tegra2, 0-3 on Tegra3)
140150c2493STom Warren  * @param which		which parts of the complex to affect (OR of crc_reset_id)
141150c2493STom Warren  * @param reset		1 to assert reset, 0 to de-assert
142150c2493STom Warren  */
143150c2493STom Warren void reset_cmplx_set_enable(int cpu, int which, int reset);
144150c2493STom Warren 
145150c2493STom Warren /**
146150c2493STom Warren  * Set the source for a peripheral clock. This plus the divisor sets the
147150c2493STom Warren  * clock rate. You need to look up the datasheet to see the meaning of the
148150c2493STom Warren  * source parameter as it changes for each peripheral.
149150c2493STom Warren  *
150150c2493STom Warren  * Warning: This function is only for use pre-relocation. Please use
151150c2493STom Warren  * clock_start_periph_pll() instead.
152150c2493STom Warren  *
153150c2493STom Warren  * @param periph_id	peripheral to adjust
154150c2493STom Warren  * @param source	source clock (0, 1, 2 or 3)
155150c2493STom Warren  */
156150c2493STom Warren void clock_ll_set_source(enum periph_id periph_id, unsigned source);
157150c2493STom Warren 
158150c2493STom Warren /**
159150c2493STom Warren  * Set the source and divisor for a peripheral clock. This sets the
160150c2493STom Warren  * clock rate. You need to look up the datasheet to see the meaning of the
161150c2493STom Warren  * source parameter as it changes for each peripheral.
162150c2493STom Warren  *
163150c2493STom Warren  * Warning: This function is only for use pre-relocation. Please use
164150c2493STom Warren  * clock_start_periph_pll() instead.
165150c2493STom Warren  *
166150c2493STom Warren  * @param periph_id	peripheral to adjust
167150c2493STom Warren  * @param source	source clock (0, 1, 2 or 3)
168150c2493STom Warren  * @param divisor	divisor value to use
169150c2493STom Warren  */
170150c2493STom Warren void clock_ll_set_source_divisor(enum periph_id periph_id, unsigned source,
171150c2493STom Warren 		unsigned divisor);
172150c2493STom Warren 
173150c2493STom Warren /**
174150c2493STom Warren  * Start a peripheral PLL clock at the given rate. This also resets the
175150c2493STom Warren  * peripheral.
176150c2493STom Warren  *
177150c2493STom Warren  * @param periph_id	peripheral to start
178150c2493STom Warren  * @param parent	PLL id of required parent clock
179150c2493STom Warren  * @param rate		Required clock rate in Hz
180150c2493STom Warren  * @return rate selected in Hz, or -1U if something went wrong
181150c2493STom Warren  */
182150c2493STom Warren unsigned clock_start_periph_pll(enum periph_id periph_id,
183150c2493STom Warren 		enum clock_id parent, unsigned rate);
184150c2493STom Warren 
185150c2493STom Warren /**
186150c2493STom Warren  * Returns the rate of a peripheral clock in Hz. Since the caller almost
187150c2493STom Warren  * certainly knows the parent clock (having just set it) we require that
188150c2493STom Warren  * this be passed in so we don't need to work it out.
189150c2493STom Warren  *
190150c2493STom Warren  * @param periph_id	peripheral to start
191150c2493STom Warren  * @param parent	PLL id of parent clock (used to calculate rate, you
192150c2493STom Warren  *			must know this!)
193150c2493STom Warren  * @return clock rate of peripheral in Hz
194150c2493STom Warren  */
195150c2493STom Warren unsigned long clock_get_periph_rate(enum periph_id periph_id,
196150c2493STom Warren 		enum clock_id parent);
197150c2493STom Warren 
198150c2493STom Warren /**
199150c2493STom Warren  * Adjust peripheral PLL clock to the given rate. This does not reset the
200150c2493STom Warren  * peripheral. If a second stage divisor is not available, pass NULL for
201150c2493STom Warren  * extra_div. If it is available, then this parameter will return the
202150c2493STom Warren  * divisor selected (which will be a power of 2 from 1 to 256).
203150c2493STom Warren  *
204150c2493STom Warren  * @param periph_id	peripheral to start
205150c2493STom Warren  * @param parent	PLL id of required parent clock
206150c2493STom Warren  * @param rate		Required clock rate in Hz
207150c2493STom Warren  * @param extra_div	value for the second-stage divisor (NULL if one is
208150c2493STom Warren 			not available)
209150c2493STom Warren  * @return rate selected in Hz, or -1U if something went wrong
210150c2493STom Warren  */
211150c2493STom Warren unsigned clock_adjust_periph_pll_div(enum periph_id periph_id,
212150c2493STom Warren 		enum clock_id parent, unsigned rate, int *extra_div);
213150c2493STom Warren 
214150c2493STom Warren /**
215150c2493STom Warren  * Returns the clock rate of a specified clock, in Hz.
216150c2493STom Warren  *
217150c2493STom Warren  * @param parent	PLL id of clock to check
218150c2493STom Warren  * @return rate of clock in Hz
219150c2493STom Warren  */
220150c2493STom Warren unsigned clock_get_rate(enum clock_id clkid);
221150c2493STom Warren 
222150c2493STom Warren /**
223150c2493STom Warren  * Start up a UART using low-level calls
224150c2493STom Warren  *
225150c2493STom Warren  * Prior to relocation clock_start_periph_pll() cannot be called. This
226150c2493STom Warren  * function provides a way to set up a UART using low-level calls which
227150c2493STom Warren  * do not require BSS.
228150c2493STom Warren  *
229150c2493STom Warren  * @param periph_id	Peripheral ID of UART to enable (e,g, PERIPH_ID_UART1)
230150c2493STom Warren  */
231150c2493STom Warren void clock_ll_start_uart(enum periph_id periph_id);
232150c2493STom Warren 
233150c2493STom Warren /**
234150c2493STom Warren  * Decode a peripheral ID from a device tree node.
235150c2493STom Warren  *
236150c2493STom Warren  * This works by looking up the peripheral's 'clocks' node and reading out
237150c2493STom Warren  * the second cell, which is the clock number / peripheral ID.
238150c2493STom Warren  *
239150c2493STom Warren  * @param blob		FDT blob to use
240150c2493STom Warren  * @param node		Node to look at
241150c2493STom Warren  * @return peripheral ID, or PERIPH_ID_NONE if none
242150c2493STom Warren  */
243150c2493STom Warren enum periph_id clock_decode_periph_id(const void *blob, int node);
244150c2493STom Warren 
245150c2493STom Warren /**
246150c2493STom Warren  * Checks if the oscillator bypass is enabled (XOBP bit)
247150c2493STom Warren  *
248150c2493STom Warren  * @return 1 if bypass is enabled, 0 if not
249150c2493STom Warren  */
250150c2493STom Warren int clock_get_osc_bypass(void);
251150c2493STom Warren 
252150c2493STom Warren /*
253150c2493STom Warren  * Checks that clocks are valid and prints a warning if not
254150c2493STom Warren  *
255150c2493STom Warren  * @return 0 if ok, -1 on error
256150c2493STom Warren  */
257150c2493STom Warren int clock_verify(void);
258150c2493STom Warren 
259150c2493STom Warren /* Initialize the clocks */
260150c2493STom Warren void clock_init(void);
261150c2493STom Warren 
262150c2493STom Warren /* Initialize the PLLs */
263150c2493STom Warren void clock_early_init(void);
264150c2493STom Warren 
265f29f086aSTom Warren /* Returns a pointer to the clock source register for a peripheral */
266f29f086aSTom Warren u32 *get_periph_source_reg(enum periph_id periph_id);
267f29f086aSTom Warren 
268f29f086aSTom Warren /**
269f29f086aSTom Warren  * Given a peripheral ID and the required source clock, this returns which
270f29f086aSTom Warren  * value should be programmed into the source mux for that peripheral.
271f29f086aSTom Warren  *
272f29f086aSTom Warren  * There is special code here to handle the one source type with 5 sources.
273f29f086aSTom Warren  *
274f29f086aSTom Warren  * @param periph_id     peripheral to start
275f29f086aSTom Warren  * @param source        PLL id of required parent clock
276f29f086aSTom Warren  * @param mux_bits      Set to number of bits in mux register: 2 or 4
277f29f086aSTom Warren  * @param divider_bits  Set to number of divider bits (8 or 16)
278f29f086aSTom Warren  * @return mux value (0-4, or -1 if not found)
279f29f086aSTom Warren  */
280f29f086aSTom Warren int get_periph_clock_source(enum periph_id periph_id,
281f29f086aSTom Warren 		enum clock_id parent, int *mux_bits, int *divider_bits);
282f29f086aSTom Warren 
283f29f086aSTom Warren /*
284f29f086aSTom Warren  * Convert a device tree clock ID to our peripheral ID. They are mostly
285f29f086aSTom Warren  * the same but we are very cautious so we check that a valid clock ID is
286f29f086aSTom Warren  * provided.
287f29f086aSTom Warren  *
288f29f086aSTom Warren  * @param clk_id        Clock ID according to tegra30 device tree binding
289f29f086aSTom Warren  * @return peripheral ID, or PERIPH_ID_NONE if the clock ID is invalid
290f29f086aSTom Warren  */
291f29f086aSTom Warren enum periph_id clk_id_to_periph_id(int clk_id);
292f29f086aSTom Warren 
293f29f086aSTom Warren /**
294f29f086aSTom Warren  * Set the output frequency you want for each PLL clock.
295f29f086aSTom Warren  * PLL output frequencies are programmed by setting their N, M and P values.
296f29f086aSTom Warren  * The governing equations are:
297f29f086aSTom Warren  *     VCO = (Fi / m) * n, Fo = VCO / (2^p)
298f29f086aSTom Warren  *     where Fo is the output frequency from the PLL.
299f29f086aSTom Warren  * Example: Set the output frequency to 216Mhz(Fo) with 12Mhz OSC(Fi)
300f29f086aSTom Warren  *     216Mhz = ((12Mhz / m) * n) / (2^p) so n=432,m=12,p=1
301f29f086aSTom Warren  * Please see Tegra TRM section 5.3 to get the detail for PLL Programming
302f29f086aSTom Warren  *
303f29f086aSTom Warren  * @param n PLL feedback divider(DIVN)
304f29f086aSTom Warren  * @param m PLL input divider(DIVN)
305f29f086aSTom Warren  * @param p post divider(DIVP)
306f29f086aSTom Warren  * @param cpcon base PLL charge pump(CPCON)
307f29f086aSTom Warren  * @return 0 if ok, -1 on error (the requested PLL is incorrect and cannot
308f29f086aSTom Warren  *              be overriden), 1 if PLL is already correct
309f29f086aSTom Warren  */
310f29f086aSTom Warren int clock_set_rate(enum clock_id clkid, u32 n, u32 m, u32 p, u32 cpcon);
311f29f086aSTom Warren 
312f29f086aSTom Warren /* return 1 if a peripheral ID is in range */
313f29f086aSTom Warren #define clock_type_id_isvalid(id) ((id) >= 0 && \
314f29f086aSTom Warren 		(id) < CLOCK_TYPE_COUNT)
315f29f086aSTom Warren 
316f29f086aSTom Warren /* return 1 if a periphc_internal_id is in range */
317f29f086aSTom Warren #define periphc_internal_id_isvalid(id) ((id) >= 0 && \
318f29f086aSTom Warren 		(id) < PERIPHC_COUNT)
319f29f086aSTom Warren 
320b40f734aSTom Warren /* SoC-specific TSC init */
321b40f734aSTom Warren void arch_timer_init(void);
322b40f734aSTom Warren 
323dc89ad14STom Warren #endif  /* _TEGRA_CLOCK_H_ */
324