xref: /rk3399_rockchip-uboot/arch/arm/include/asm/arch-tegra/clock.h (revision 6dbcc962e48e4f2395806469ba59f19fb18aa64a)
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,
193e8650c0STom Warren 	CLOCK_OSC_FREQ_38_4,
203e8650c0STom Warren 	CLOCK_OSC_FREQ_48_0,
21150c2493STom Warren 
22150c2493STom Warren 	CLOCK_OSC_FREQ_COUNT,
23150c2493STom Warren };
24150c2493STom Warren 
255916a36eSStephen Warren /*
265916a36eSStephen Warren  * Note that no Tegra clock register actually uses all of bits 31:28 as
275916a36eSStephen Warren  * the mux field. Rather, bits 30:28, 29:28, or 28 are used. However, in
285916a36eSStephen Warren  * those cases, nothing is stored in the bits about the mux field, so it's
295916a36eSStephen Warren  * safe to pretend that the mux field extends all the way to the end of the
305916a36eSStephen Warren  * register. As such, the U-Boot clock driver is currently a bit lazy, and
315916a36eSStephen Warren  * doesn't distinguish between 31:28, 30:28, 29:28 and 28; it just lumps
325916a36eSStephen Warren  * them all together and pretends they're all 31:28.
335916a36eSStephen Warren  */
340b01b53aSTom Warren enum {
3504b8e8e7SStephen Warren 	MASK_BITS_31_30,
360b01b53aSTom Warren 	MASK_BITS_31_29,
375916a36eSStephen Warren 	MASK_BITS_31_28,
380b01b53aSTom Warren };
390b01b53aSTom Warren 
40150c2493STom Warren #include <asm/arch/clock-tables.h>
41150c2493STom Warren /* PLL stabilization delay in usec */
42150c2493STom Warren #define CLOCK_PLL_STABLE_DELAY_US 300
43150c2493STom Warren 
44150c2493STom Warren /* return the current oscillator clock frequency */
45150c2493STom Warren enum clock_osc_freq clock_get_osc_freq(void);
46150c2493STom Warren 
47c043c025SThierry Reding /* return the clk_m frequency */
48c043c025SThierry Reding unsigned int clk_m_get_rate(unsigned int parent_rate);
49c043c025SThierry Reding 
50150c2493STom Warren /**
51150c2493STom Warren  * Start PLL using the provided configuration parameters.
52150c2493STom Warren  *
53150c2493STom Warren  * @param id	clock id
54150c2493STom Warren  * @param divm	input divider
55150c2493STom Warren  * @param divn	feedback divider
56150c2493STom Warren  * @param divp	post divider 2^n
57150c2493STom Warren  * @param cpcon	charge pump setup control
58150c2493STom Warren  * @param lfcon	loop filter setup control
59150c2493STom Warren  *
60150c2493STom Warren  * @returns monotonic time in us that the PLL will be stable
61150c2493STom Warren  */
62150c2493STom Warren unsigned long clock_start_pll(enum clock_id id, u32 divm, u32 divn,
63150c2493STom Warren 		u32 divp, u32 cpcon, u32 lfcon);
64150c2493STom Warren 
65150c2493STom Warren /**
6665530a84SLucas Stach  * Set PLL output frequency
6765530a84SLucas Stach  *
6865530a84SLucas Stach  * @param clkid	clock id
6965530a84SLucas Stach  * @param pllout	pll output id
7065530a84SLucas Stach  * @param rate		desired output rate
7165530a84SLucas Stach  *
7265530a84SLucas Stach  * @return 0 if ok, -1 on error (invalid clock id or no suitable divider)
7365530a84SLucas Stach  */
7465530a84SLucas Stach int clock_set_pllout(enum clock_id clkid, enum pll_out_id pllout,
7565530a84SLucas Stach 		unsigned rate);
7665530a84SLucas Stach 
7765530a84SLucas Stach /**
78150c2493STom Warren  * Read low-level parameters of a PLL.
79150c2493STom Warren  *
80150c2493STom Warren  * @param id	clock id to read (note: USB is not supported)
81150c2493STom Warren  * @param divm	returns input divider
82150c2493STom Warren  * @param divn	returns feedback divider
83150c2493STom Warren  * @param divp	returns post divider 2^n
84150c2493STom Warren  * @param cpcon	returns charge pump setup control
85150c2493STom Warren  * @param lfcon	returns loop filter setup control
86150c2493STom Warren  *
87150c2493STom Warren  * @returns 0 if ok, -1 on error (invalid clock id)
88150c2493STom Warren  */
89150c2493STom Warren int clock_ll_read_pll(enum clock_id clkid, u32 *divm, u32 *divn,
90150c2493STom Warren 		u32 *divp, u32 *cpcon, u32 *lfcon);
91150c2493STom Warren 
92150c2493STom Warren /*
93150c2493STom Warren  * Enable a clock
94150c2493STom Warren  *
95150c2493STom Warren  * @param id	clock id
96150c2493STom Warren  */
97150c2493STom Warren void clock_enable(enum periph_id clkid);
98150c2493STom Warren 
99150c2493STom Warren /*
100150c2493STom Warren  * Disable a clock
101150c2493STom Warren  *
102150c2493STom Warren  * @param id	clock id
103150c2493STom Warren  */
104150c2493STom Warren void clock_disable(enum periph_id clkid);
105150c2493STom Warren 
106150c2493STom Warren /*
107150c2493STom Warren  * Set whether a clock is enabled or disabled.
108150c2493STom Warren  *
109150c2493STom Warren  * @param id		clock id
110150c2493STom Warren  * @param enable	1 to enable, 0 to disable
111150c2493STom Warren  */
112150c2493STom Warren void clock_set_enable(enum periph_id clkid, int enable);
113150c2493STom Warren 
114150c2493STom Warren /**
115150c2493STom Warren  * Reset a peripheral. This puts it in reset, waits for a delay, then takes
116150c2493STom Warren  * it out of reset and waits for th delay again.
117150c2493STom Warren  *
118150c2493STom Warren  * @param periph_id	peripheral to reset
119150c2493STom Warren  * @param us_delay	time to delay in microseconds
120150c2493STom Warren  */
121150c2493STom Warren void reset_periph(enum periph_id periph_id, int us_delay);
122150c2493STom Warren 
123150c2493STom Warren /**
124150c2493STom Warren  * Put a peripheral into or out of reset.
125150c2493STom Warren  *
126150c2493STom Warren  * @param periph_id	peripheral to reset
127150c2493STom Warren  * @param enable	1 to put into reset, 0 to take out of reset
128150c2493STom Warren  */
129150c2493STom Warren void reset_set_enable(enum periph_id periph_id, int enable);
130150c2493STom Warren 
131150c2493STom Warren 
132150c2493STom Warren /* CLK_RST_CONTROLLER_RST_CPU_CMPLX_SET/CLR_0 */
133150c2493STom Warren enum crc_reset_id {
134150c2493STom Warren 	/* Things we can hold in reset for each CPU */
135150c2493STom Warren 	crc_rst_cpu = 1,
136766afc3dSAlban Bedel 	crc_rst_de = 1 << 4,	/* What is de? */
137766afc3dSAlban Bedel 	crc_rst_watchdog = 1 << 8,
138766afc3dSAlban Bedel 	crc_rst_debug = 1 << 12,
139150c2493STom Warren };
140150c2493STom Warren 
141150c2493STom Warren /**
142150c2493STom Warren  * Put parts of the CPU complex into or out of reset.\
143150c2493STom Warren  *
144dc89ad14STom Warren  * @param cpu		cpu number (0 or 1 on Tegra2, 0-3 on Tegra3)
145150c2493STom Warren  * @param which		which parts of the complex to affect (OR of crc_reset_id)
146150c2493STom Warren  * @param reset		1 to assert reset, 0 to de-assert
147150c2493STom Warren  */
148150c2493STom Warren void reset_cmplx_set_enable(int cpu, int which, int reset);
149150c2493STom Warren 
150150c2493STom Warren /**
151150c2493STom Warren  * Set the source for a peripheral clock. This plus the divisor sets the
152150c2493STom Warren  * clock rate. You need to look up the datasheet to see the meaning of the
153150c2493STom Warren  * source parameter as it changes for each peripheral.
154150c2493STom Warren  *
155150c2493STom Warren  * Warning: This function is only for use pre-relocation. Please use
156150c2493STom Warren  * clock_start_periph_pll() instead.
157150c2493STom Warren  *
158150c2493STom Warren  * @param periph_id	peripheral to adjust
159150c2493STom Warren  * @param source	source clock (0, 1, 2 or 3)
160150c2493STom Warren  */
161150c2493STom Warren void clock_ll_set_source(enum periph_id periph_id, unsigned source);
162150c2493STom Warren 
163150c2493STom Warren /**
1647bb6199bSSimon Glass  * This function is similar to clock_ll_set_source() except that it can be
1657bb6199bSSimon Glass  * used for clocks with more than 2 mux bits.
1667bb6199bSSimon Glass  *
1677bb6199bSSimon Glass  * @param periph_id	peripheral to adjust
1687bb6199bSSimon Glass  * @param mux_bits	number of mux bits for the clock
1697bb6199bSSimon Glass  * @param source	source clock (0-15 depending on mux_bits)
1707bb6199bSSimon Glass  */
1717bb6199bSSimon Glass int clock_ll_set_source_bits(enum periph_id periph_id, int mux_bits,
1727bb6199bSSimon Glass 			     unsigned source);
1737bb6199bSSimon Glass 
1747bb6199bSSimon Glass /**
175150c2493STom Warren  * Set the source and divisor for a peripheral clock. This sets the
176150c2493STom Warren  * clock rate. You need to look up the datasheet to see the meaning of the
177150c2493STom Warren  * source parameter as it changes for each peripheral.
178150c2493STom Warren  *
179150c2493STom Warren  * Warning: This function is only for use pre-relocation. Please use
180150c2493STom Warren  * clock_start_periph_pll() instead.
181150c2493STom Warren  *
182150c2493STom Warren  * @param periph_id	peripheral to adjust
183150c2493STom Warren  * @param source	source clock (0, 1, 2 or 3)
184150c2493STom Warren  * @param divisor	divisor value to use
185150c2493STom Warren  */
186150c2493STom Warren void clock_ll_set_source_divisor(enum periph_id periph_id, unsigned source,
187150c2493STom Warren 		unsigned divisor);
188150c2493STom Warren 
189150c2493STom Warren /**
190150c2493STom Warren  * Start a peripheral PLL clock at the given rate. This also resets the
191150c2493STom Warren  * peripheral.
192150c2493STom Warren  *
193150c2493STom Warren  * @param periph_id	peripheral to start
194150c2493STom Warren  * @param parent	PLL id of required parent clock
195150c2493STom Warren  * @param rate		Required clock rate in Hz
196150c2493STom Warren  * @return rate selected in Hz, or -1U if something went wrong
197150c2493STom Warren  */
198150c2493STom Warren unsigned clock_start_periph_pll(enum periph_id periph_id,
199150c2493STom Warren 		enum clock_id parent, unsigned rate);
200150c2493STom Warren 
201150c2493STom Warren /**
202150c2493STom Warren  * Returns the rate of a peripheral clock in Hz. Since the caller almost
203150c2493STom Warren  * certainly knows the parent clock (having just set it) we require that
204150c2493STom Warren  * this be passed in so we don't need to work it out.
205150c2493STom Warren  *
206150c2493STom Warren  * @param periph_id	peripheral to start
207150c2493STom Warren  * @param parent	PLL id of parent clock (used to calculate rate, you
208150c2493STom Warren  *			must know this!)
209150c2493STom Warren  * @return clock rate of peripheral in Hz
210150c2493STom Warren  */
211150c2493STom Warren unsigned long clock_get_periph_rate(enum periph_id periph_id,
212150c2493STom Warren 		enum clock_id parent);
213150c2493STom Warren 
214150c2493STom Warren /**
215150c2493STom Warren  * Adjust peripheral PLL clock to the given rate. This does not reset the
216150c2493STom Warren  * peripheral. If a second stage divisor is not available, pass NULL for
217150c2493STom Warren  * extra_div. If it is available, then this parameter will return the
218150c2493STom Warren  * divisor selected (which will be a power of 2 from 1 to 256).
219150c2493STom Warren  *
220150c2493STom Warren  * @param periph_id	peripheral to start
221150c2493STom Warren  * @param parent	PLL id of required parent clock
222150c2493STom Warren  * @param rate		Required clock rate in Hz
223150c2493STom Warren  * @param extra_div	value for the second-stage divisor (NULL if one is
224150c2493STom Warren 			not available)
225150c2493STom Warren  * @return rate selected in Hz, or -1U if something went wrong
226150c2493STom Warren  */
227150c2493STom Warren unsigned clock_adjust_periph_pll_div(enum periph_id periph_id,
228150c2493STom Warren 		enum clock_id parent, unsigned rate, int *extra_div);
229150c2493STom Warren 
230150c2493STom Warren /**
231150c2493STom Warren  * Returns the clock rate of a specified clock, in Hz.
232150c2493STom Warren  *
233150c2493STom Warren  * @param parent	PLL id of clock to check
234150c2493STom Warren  * @return rate of clock in Hz
235150c2493STom Warren  */
236150c2493STom Warren unsigned clock_get_rate(enum clock_id clkid);
237150c2493STom Warren 
238150c2493STom Warren /**
239150c2493STom Warren  * Start up a UART using low-level calls
240150c2493STom Warren  *
241150c2493STom Warren  * Prior to relocation clock_start_periph_pll() cannot be called. This
242150c2493STom Warren  * function provides a way to set up a UART using low-level calls which
243150c2493STom Warren  * do not require BSS.
244150c2493STom Warren  *
245150c2493STom Warren  * @param periph_id	Peripheral ID of UART to enable (e,g, PERIPH_ID_UART1)
246150c2493STom Warren  */
247150c2493STom Warren void clock_ll_start_uart(enum periph_id periph_id);
248150c2493STom Warren 
249150c2493STom Warren /**
250150c2493STom Warren  * Decode a peripheral ID from a device tree node.
251150c2493STom Warren  *
252150c2493STom Warren  * This works by looking up the peripheral's 'clocks' node and reading out
253150c2493STom Warren  * the second cell, which is the clock number / peripheral ID.
254150c2493STom Warren  *
255150c2493STom Warren  * @param blob		FDT blob to use
256150c2493STom Warren  * @param node		Node to look at
257150c2493STom Warren  * @return peripheral ID, or PERIPH_ID_NONE if none
258150c2493STom Warren  */
259150c2493STom Warren enum periph_id clock_decode_periph_id(const void *blob, int node);
260150c2493STom Warren 
261150c2493STom Warren /**
262150c2493STom Warren  * Checks if the oscillator bypass is enabled (XOBP bit)
263150c2493STom Warren  *
264150c2493STom Warren  * @return 1 if bypass is enabled, 0 if not
265150c2493STom Warren  */
266150c2493STom Warren int clock_get_osc_bypass(void);
267150c2493STom Warren 
268150c2493STom Warren /*
269150c2493STom Warren  * Checks that clocks are valid and prints a warning if not
270150c2493STom Warren  *
271150c2493STom Warren  * @return 0 if ok, -1 on error
272150c2493STom Warren  */
273150c2493STom Warren int clock_verify(void);
274150c2493STom Warren 
275150c2493STom Warren /* Initialize the clocks */
276150c2493STom Warren void clock_init(void);
277150c2493STom Warren 
278150c2493STom Warren /* Initialize the PLLs */
279150c2493STom Warren void clock_early_init(void);
280150c2493STom Warren 
281f29f086aSTom Warren /* Returns a pointer to the clock source register for a peripheral */
282f29f086aSTom Warren u32 *get_periph_source_reg(enum periph_id periph_id);
283f29f086aSTom Warren 
284801b05cdSSimon Glass /* Returns a pointer to the given 'simple' PLL */
285801b05cdSSimon Glass struct clk_pll_simple *clock_get_simple_pll(enum clock_id clkid);
286801b05cdSSimon Glass 
287f29f086aSTom Warren /**
288f29f086aSTom Warren  * Given a peripheral ID and the required source clock, this returns which
289f29f086aSTom Warren  * value should be programmed into the source mux for that peripheral.
290f29f086aSTom Warren  *
291f29f086aSTom Warren  * There is special code here to handle the one source type with 5 sources.
292f29f086aSTom Warren  *
293f29f086aSTom Warren  * @param periph_id     peripheral to start
294f29f086aSTom Warren  * @param source        PLL id of required parent clock
295f29f086aSTom Warren  * @param mux_bits      Set to number of bits in mux register: 2 or 4
296f29f086aSTom Warren  * @param divider_bits  Set to number of divider bits (8 or 16)
297f29f086aSTom Warren  * @return mux value (0-4, or -1 if not found)
298f29f086aSTom Warren  */
299f29f086aSTom Warren int get_periph_clock_source(enum periph_id periph_id,
300f29f086aSTom Warren 		enum clock_id parent, int *mux_bits, int *divider_bits);
301f29f086aSTom Warren 
302f29f086aSTom Warren /*
303f29f086aSTom Warren  * Convert a device tree clock ID to our peripheral ID. They are mostly
304f29f086aSTom Warren  * the same but we are very cautious so we check that a valid clock ID is
305f29f086aSTom Warren  * provided.
306f29f086aSTom Warren  *
307f29f086aSTom Warren  * @param clk_id        Clock ID according to tegra30 device tree binding
308f29f086aSTom Warren  * @return peripheral ID, or PERIPH_ID_NONE if the clock ID is invalid
309f29f086aSTom Warren  */
310f29f086aSTom Warren enum periph_id clk_id_to_periph_id(int clk_id);
311f29f086aSTom Warren 
312f29f086aSTom Warren /**
313f29f086aSTom Warren  * Set the output frequency you want for each PLL clock.
314f29f086aSTom Warren  * PLL output frequencies are programmed by setting their N, M and P values.
315f29f086aSTom Warren  * The governing equations are:
316f29f086aSTom Warren  *     VCO = (Fi / m) * n, Fo = VCO / (2^p)
317f29f086aSTom Warren  *     where Fo is the output frequency from the PLL.
318f29f086aSTom Warren  * Example: Set the output frequency to 216Mhz(Fo) with 12Mhz OSC(Fi)
319f29f086aSTom Warren  *     216Mhz = ((12Mhz / m) * n) / (2^p) so n=432,m=12,p=1
320f29f086aSTom Warren  * Please see Tegra TRM section 5.3 to get the detail for PLL Programming
321f29f086aSTom Warren  *
322f29f086aSTom Warren  * @param n PLL feedback divider(DIVN)
323f29f086aSTom Warren  * @param m PLL input divider(DIVN)
324f29f086aSTom Warren  * @param p post divider(DIVP)
325f29f086aSTom Warren  * @param cpcon base PLL charge pump(CPCON)
326f29f086aSTom Warren  * @return 0 if ok, -1 on error (the requested PLL is incorrect and cannot
32762a3b7ddSRobert P. J. Day  *              be overridden), 1 if PLL is already correct
328f29f086aSTom Warren  */
329f29f086aSTom Warren int clock_set_rate(enum clock_id clkid, u32 n, u32 m, u32 p, u32 cpcon);
330f29f086aSTom Warren 
331f29f086aSTom Warren /* return 1 if a peripheral ID is in range */
332f29f086aSTom Warren #define clock_type_id_isvalid(id) ((id) >= 0 && \
333f29f086aSTom Warren 		(id) < CLOCK_TYPE_COUNT)
334f29f086aSTom Warren 
335f29f086aSTom Warren /* return 1 if a periphc_internal_id is in range */
336f29f086aSTom Warren #define periphc_internal_id_isvalid(id) ((id) >= 0 && \
337f29f086aSTom Warren 		(id) < PERIPHC_COUNT)
338f29f086aSTom Warren 
339b40f734aSTom Warren /* SoC-specific TSC init */
340b40f734aSTom Warren void arch_timer_init(void);
341b40f734aSTom Warren 
342b9dd6215SJimmy Zhang void tegra30_set_up_pllp(void);
343b9dd6215SJimmy Zhang 
344c043c025SThierry Reding /* Number of PLL-based clocks (i.e. not OSC, MCLK or 32KHz) */
345c043c025SThierry Reding #define CLOCK_ID_PLL_COUNT	(CLOCK_ID_COUNT - 3)
346722e000cSTom Warren 
347722e000cSTom Warren struct clk_pll_info {
348722e000cSTom Warren 	u32	m_shift:5;	/* DIVM_SHIFT */
349722e000cSTom Warren 	u32	n_shift:5;	/* DIVN_SHIFT */
350722e000cSTom Warren 	u32	p_shift:5;	/* DIVP_SHIFT */
351722e000cSTom Warren 	u32	kcp_shift:5;	/* KCP/cpcon SHIFT */
352722e000cSTom Warren 	u32	kvco_shift:5;	/* KVCO/lfcon SHIFT */
353722e000cSTom Warren 	u32	lock_ena:6;	/* LOCK_ENABLE/EN_LOCKDET shift */
354722e000cSTom Warren 	u32	rsvd:1;
355722e000cSTom Warren 	u32	m_mask:10;	/* DIVM_MASK */
356722e000cSTom Warren 	u32	n_mask:12;	/* DIVN_MASK */
357722e000cSTom Warren 	u32	p_mask:10;	/* DIVP_MASK or VCO_MASK */
358722e000cSTom Warren 	u32	kcp_mask:10;	/* KCP/CPCON MASK */
359722e000cSTom Warren 	u32	kvco_mask:10;	/* KVCO/LFCON MASK */
360722e000cSTom Warren 	u32	lock_det:6;	/* LOCK_DETECT/LOCKED shift */
361722e000cSTom Warren 	u32	rsvd2:6;
362722e000cSTom Warren };
363722e000cSTom Warren extern struct clk_pll_info tegra_pll_info_table[CLOCK_ID_PLL_COUNT];
364722e000cSTom Warren 
365*6dbcc962SStephen Warren struct periph_clk_init {
366*6dbcc962SStephen Warren 	enum periph_id periph_id;
367*6dbcc962SStephen Warren 	enum clock_id parent_clock_id;
368*6dbcc962SStephen Warren };
369*6dbcc962SStephen Warren extern struct periph_clk_init periph_clk_init_table[];
370*6dbcc962SStephen Warren 
371746dc76bSSimon Glass /**
372746dc76bSSimon Glass  * Enable output clock for external peripherals
373746dc76bSSimon Glass  *
374746dc76bSSimon Glass  * @param clk_id	Clock ID to output (1, 2 or 3)
375746dc76bSSimon Glass  * @return 0 if OK. -ve on error
376746dc76bSSimon Glass  */
377746dc76bSSimon Glass int clock_external_output(int clk_id);
378746dc76bSSimon Glass 
379dc89ad14STom Warren #endif  /* _TEGRA_CLOCK_H_ */
380