blob: 2957b97e6a5a65f06fd553cfca3c2c6df82c2188 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
Tom Warrenab371962012-09-19 15:50:56 -07002/*
3 * Copyright (c) 2011 The Chromium OS Authors.
Tom Warrenab371962012-09-19 15:50:56 -07004 */
5
6/* Tegra clock control functions */
7
Tom Warren13ac5442012-12-11 13:34:12 +00008#ifndef _TEGRA_CLOCK_H_
9#define _TEGRA_CLOCK_H_
Tom Warrenab371962012-09-19 15:50:56 -070010
Simon Glass3ba929a2020-10-30 21:38:53 -060011struct udevice;
12
Tom Warrenab371962012-09-19 15:50:56 -070013/* Set of oscillator frequencies supported in the internal API. */
14enum clock_osc_freq {
15 /* All in MHz, so 13_0 is 13.0MHz */
Svyatoslav Ryhel7f4ab332023-02-01 10:53:01 +020016 CLOCK_OSC_FREQ_13_0 = 0,
17 CLOCK_OSC_FREQ_16_8,
18 CLOCK_OSC_FREQ_19_2 = 4,
Tom Warren27bce712015-06-22 13:03:44 -070019 CLOCK_OSC_FREQ_38_4,
Svyatoslav Ryhel7f4ab332023-02-01 10:53:01 +020020 CLOCK_OSC_FREQ_12_0 = 8,
Tom Warren27bce712015-06-22 13:03:44 -070021 CLOCK_OSC_FREQ_48_0,
Svyatoslav Ryhel7f4ab332023-02-01 10:53:01 +020022 CLOCK_OSC_FREQ_26_0 = 12,
Tom Warrenab371962012-09-19 15:50:56 -070023
24 CLOCK_OSC_FREQ_COUNT,
25};
26
Stephen Warren510c0ae2014-01-24 10:16:18 -070027/*
28 * Note that no Tegra clock register actually uses all of bits 31:28 as
29 * the mux field. Rather, bits 30:28, 29:28, or 28 are used. However, in
30 * those cases, nothing is stored in the bits about the mux field, so it's
31 * safe to pretend that the mux field extends all the way to the end of the
32 * register. As such, the U-Boot clock driver is currently a bit lazy, and
33 * doesn't distinguish between 31:28, 30:28, 29:28 and 28; it just lumps
34 * them all together and pretends they're all 31:28.
35 */
Tom Warrenea21e762014-01-24 10:16:17 -070036enum {
Stephen Warren33e5da92014-01-24 10:16:21 -070037 MASK_BITS_31_30,
Tom Warrenea21e762014-01-24 10:16:17 -070038 MASK_BITS_31_29,
Stephen Warren510c0ae2014-01-24 10:16:18 -070039 MASK_BITS_31_28,
Tom Warrenea21e762014-01-24 10:16:17 -070040};
41
Tom Warrenab371962012-09-19 15:50:56 -070042#include <asm/arch/clock-tables.h>
43/* PLL stabilization delay in usec */
44#define CLOCK_PLL_STABLE_DELAY_US 300
45
46/* return the current oscillator clock frequency */
47enum clock_osc_freq clock_get_osc_freq(void);
48
Thierry Redingfa6e24d2015-08-20 11:42:19 +020049/* return the clk_m frequency */
50unsigned int clk_m_get_rate(unsigned int parent_rate);
51
Tom Warrenab371962012-09-19 15:50:56 -070052/**
53 * Start PLL using the provided configuration parameters.
54 *
55 * @param id clock id
56 * @param divm input divider
57 * @param divn feedback divider
58 * @param divp post divider 2^n
59 * @param cpcon charge pump setup control
60 * @param lfcon loop filter setup control
61 *
62 * @returns monotonic time in us that the PLL will be stable
63 */
64unsigned long clock_start_pll(enum clock_id id, u32 divm, u32 divn,
65 u32 divp, u32 cpcon, u32 lfcon);
66
67/**
Lucas Stachf7ee2a42012-09-25 20:21:13 +000068 * Set PLL output frequency
69 *
70 * @param clkid clock id
71 * @param pllout pll output id
72 * @param rate desired output rate
73 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +010074 * Return: 0 if ok, -1 on error (invalid clock id or no suitable divider)
Lucas Stachf7ee2a42012-09-25 20:21:13 +000075 */
76int clock_set_pllout(enum clock_id clkid, enum pll_out_id pllout,
77 unsigned rate);
78
79/**
Tom Warrenab371962012-09-19 15:50:56 -070080 * Read low-level parameters of a PLL.
81 *
82 * @param id clock id to read (note: USB is not supported)
83 * @param divm returns input divider
84 * @param divn returns feedback divider
85 * @param divp returns post divider 2^n
86 * @param cpcon returns charge pump setup control
87 * @param lfcon returns loop filter setup control
88 *
89 * @returns 0 if ok, -1 on error (invalid clock id)
90 */
91int clock_ll_read_pll(enum clock_id clkid, u32 *divm, u32 *divn,
Tom Warren795f9d72013-01-23 14:01:01 -070092 u32 *divp, u32 *cpcon, u32 *lfcon);
Tom Warrenab371962012-09-19 15:50:56 -070093
94/*
95 * Enable a clock
96 *
97 * @param id clock id
98 */
99void clock_enable(enum periph_id clkid);
100
101/*
102 * Disable a clock
103 *
104 * @param id clock id
105 */
106void clock_disable(enum periph_id clkid);
107
108/*
109 * Set whether a clock is enabled or disabled.
110 *
111 * @param id clock id
112 * @param enable 1 to enable, 0 to disable
113 */
114void clock_set_enable(enum periph_id clkid, int enable);
115
116/**
117 * Reset a peripheral. This puts it in reset, waits for a delay, then takes
118 * it out of reset and waits for th delay again.
119 *
120 * @param periph_id peripheral to reset
121 * @param us_delay time to delay in microseconds
122 */
123void reset_periph(enum periph_id periph_id, int us_delay);
124
125/**
126 * Put a peripheral into or out of reset.
127 *
128 * @param periph_id peripheral to reset
129 * @param enable 1 to put into reset, 0 to take out of reset
130 */
131void reset_set_enable(enum periph_id periph_id, int enable);
132
Tom Warrenab371962012-09-19 15:50:56 -0700133/* CLK_RST_CONTROLLER_RST_CPU_CMPLX_SET/CLR_0 */
134enum crc_reset_id {
135 /* Things we can hold in reset for each CPU */
136 crc_rst_cpu = 1,
Alban Bedel2a639fc2013-11-20 17:42:46 +0100137 crc_rst_de = 1 << 4, /* What is de? */
138 crc_rst_watchdog = 1 << 8,
139 crc_rst_debug = 1 << 12,
Tom Warrenab371962012-09-19 15:50:56 -0700140};
141
142/**
143 * Put parts of the CPU complex into or out of reset.\
144 *
Tom Warren13ac5442012-12-11 13:34:12 +0000145 * @param cpu cpu number (0 or 1 on Tegra2, 0-3 on Tegra3)
Tom Warrenab371962012-09-19 15:50:56 -0700146 * @param which which parts of the complex to affect (OR of crc_reset_id)
147 * @param reset 1 to assert reset, 0 to de-assert
148 */
149void reset_cmplx_set_enable(int cpu, int which, int reset);
150
151/**
152 * Set the source for a peripheral clock. This plus the divisor sets the
153 * clock rate. You need to look up the datasheet to see the meaning of the
154 * source parameter as it changes for each peripheral.
155 *
156 * Warning: This function is only for use pre-relocation. Please use
157 * clock_start_periph_pll() instead.
158 *
159 * @param periph_id peripheral to adjust
160 * @param source source clock (0, 1, 2 or 3)
161 */
162void clock_ll_set_source(enum periph_id periph_id, unsigned source);
163
164/**
Simon Glassd2d1c3f2015-04-14 21:03:33 -0600165 * This function is similar to clock_ll_set_source() except that it can be
166 * used for clocks with more than 2 mux bits.
167 *
168 * @param periph_id peripheral to adjust
169 * @param mux_bits number of mux bits for the clock
170 * @param source source clock (0-15 depending on mux_bits)
171 */
172int clock_ll_set_source_bits(enum periph_id periph_id, int mux_bits,
173 unsigned source);
174
175/**
Tom Warrenab371962012-09-19 15:50:56 -0700176 * Set the source and divisor for a peripheral clock. This sets the
177 * clock rate. You need to look up the datasheet to see the meaning of the
178 * source parameter as it changes for each peripheral.
179 *
180 * Warning: This function is only for use pre-relocation. Please use
181 * clock_start_periph_pll() instead.
182 *
183 * @param periph_id peripheral to adjust
184 * @param source source clock (0, 1, 2 or 3)
185 * @param divisor divisor value to use
186 */
187void clock_ll_set_source_divisor(enum periph_id periph_id, unsigned source,
188 unsigned divisor);
189
190/**
Stephen Warren532543c2016-09-13 10:45:56 -0600191 * Returns the current parent clock ID of a given peripheral. This can be
192 * useful in order to call clock_*_periph_*() from generic code that has no
193 * specific knowledge of system-level clock tree structure.
194 *
195 * @param periph_id peripheral to query
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100196 * Return: clock ID of the peripheral's current parent clock
Stephen Warren532543c2016-09-13 10:45:56 -0600197 */
198enum clock_id clock_get_periph_parent(enum periph_id periph_id);
199
200/**
Tom Warrenab371962012-09-19 15:50:56 -0700201 * Start a peripheral PLL clock at the given rate. This also resets the
202 * peripheral.
203 *
204 * @param periph_id peripheral to start
205 * @param parent PLL id of required parent clock
206 * @param rate Required clock rate in Hz
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100207 * Return: rate selected in Hz, or -1U if something went wrong
Tom Warrenab371962012-09-19 15:50:56 -0700208 */
209unsigned clock_start_periph_pll(enum periph_id periph_id,
210 enum clock_id parent, unsigned rate);
211
212/**
213 * Returns the rate of a peripheral clock in Hz. Since the caller almost
214 * certainly knows the parent clock (having just set it) we require that
215 * this be passed in so we don't need to work it out.
216 *
217 * @param periph_id peripheral to start
218 * @param parent PLL id of parent clock (used to calculate rate, you
219 * must know this!)
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100220 * Return: clock rate of peripheral in Hz
Tom Warrenab371962012-09-19 15:50:56 -0700221 */
222unsigned long clock_get_periph_rate(enum periph_id periph_id,
223 enum clock_id parent);
224
225/**
226 * Adjust peripheral PLL clock to the given rate. This does not reset the
227 * peripheral. If a second stage divisor is not available, pass NULL for
228 * extra_div. If it is available, then this parameter will return the
229 * divisor selected (which will be a power of 2 from 1 to 256).
230 *
231 * @param periph_id peripheral to start
232 * @param parent PLL id of required parent clock
233 * @param rate Required clock rate in Hz
234 * @param extra_div value for the second-stage divisor (NULL if one is
235 not available)
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100236 * Return: rate selected in Hz, or -1U if something went wrong
Tom Warrenab371962012-09-19 15:50:56 -0700237 */
238unsigned clock_adjust_periph_pll_div(enum periph_id periph_id,
239 enum clock_id parent, unsigned rate, int *extra_div);
240
241/**
242 * Returns the clock rate of a specified clock, in Hz.
243 *
244 * @param parent PLL id of clock to check
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100245 * Return: rate of clock in Hz
Tom Warrenab371962012-09-19 15:50:56 -0700246 */
247unsigned clock_get_rate(enum clock_id clkid);
248
249/**
250 * Start up a UART using low-level calls
251 *
252 * Prior to relocation clock_start_periph_pll() cannot be called. This
253 * function provides a way to set up a UART using low-level calls which
254 * do not require BSS.
255 *
256 * @param periph_id Peripheral ID of UART to enable (e,g, PERIPH_ID_UART1)
257 */
258void clock_ll_start_uart(enum periph_id periph_id);
259
260/**
261 * Decode a peripheral ID from a device tree node.
262 *
263 * This works by looking up the peripheral's 'clocks' node and reading out
264 * the second cell, which is the clock number / peripheral ID.
265 *
266 * @param blob FDT blob to use
267 * @param node Node to look at
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100268 * Return: peripheral ID, or PERIPH_ID_NONE if none
Tom Warrenab371962012-09-19 15:50:56 -0700269 */
Simon Glassc3f26502017-07-25 08:30:00 -0600270int clock_decode_periph_id(struct udevice *dev);
Tom Warrenab371962012-09-19 15:50:56 -0700271
272/**
Svyatoslav Ryhelb6e50b82023-02-14 19:35:26 +0200273 * Get periph clock id and its parent from device tree.
274 *
275 * This works by looking up the peripheral's 'clocks' node and reading out
276 * the second and fourth cells, which are the peripheral and PLL clock numbers.
277 *
278 * @param dev udevice associated with FDT node
279 * @param clk_id pointer to int array of 2 values
280 * first is periph clock, second is
281 * its PLL parent according to FDT.
282 */
283int clock_decode_pair(struct udevice *dev, int *clk_id);
284
285/**
Tom Warrenab371962012-09-19 15:50:56 -0700286 * Checks if the oscillator bypass is enabled (XOBP bit)
287 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100288 * Return: 1 if bypass is enabled, 0 if not
Tom Warrenab371962012-09-19 15:50:56 -0700289 */
290int clock_get_osc_bypass(void);
291
292/*
293 * Checks that clocks are valid and prints a warning if not
294 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100295 * Return: 0 if ok, -1 on error
Tom Warrenab371962012-09-19 15:50:56 -0700296 */
297int clock_verify(void);
298
299/* Initialize the clocks */
300void clock_init(void);
301
302/* Initialize the PLLs */
303void clock_early_init(void);
304
Simon Glass2b4029a2017-05-31 17:57:16 -0600305/* @return true if hardware indicates that clock_early_init() was called */
306bool clock_early_init_done(void);
307
Tom Warren795f9d72013-01-23 14:01:01 -0700308/* Returns a pointer to the clock source register for a peripheral */
309u32 *get_periph_source_reg(enum periph_id periph_id);
310
Simon Glass6017b9a2015-04-14 21:03:32 -0600311/* Returns a pointer to the given 'simple' PLL */
312struct clk_pll_simple *clock_get_simple_pll(enum clock_id clkid);
313
Stephen Warren532543c2016-09-13 10:45:56 -0600314/*
315 * Given a peripheral ID, determine where the mux bits are in the peripheral
316 * clock's register, the number of divider bits the clock has, and the SoC-
317 * specific clock type.
318 *
319 * This is an internal API between the core Tegra clock code and the SoC-
320 * specific clock code.
321 *
322 * @param periph_id peripheral to query
323 * @param mux_bits Set to number of bits in mux register
324 * @param divider_bits Set to the relevant MASK_BITS_* value
325 * @param type Set to the SoC-specific clock type
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100326 * Return: 0 on success, -1 on error
Stephen Warren532543c2016-09-13 10:45:56 -0600327 */
328int get_periph_clock_info(enum periph_id periph_id, int *mux_bits,
329 int *divider_bits, int *type);
330
331/*
332 * Given a peripheral ID and clock source mux value, determine the clock_id
333 * of that peripheral's parent.
334 *
335 * This is an internal API between the core Tegra clock code and the SoC-
336 * specific clock code.
337 *
338 * @param periph_id peripheral to query
339 * @param source raw clock source mux value
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100340 * Return: the CLOCK_ID_* value @source represents
Stephen Warren532543c2016-09-13 10:45:56 -0600341 */
342enum clock_id get_periph_clock_id(enum periph_id periph_id, int source);
343
Tom Warren795f9d72013-01-23 14:01:01 -0700344/**
345 * Given a peripheral ID and the required source clock, this returns which
346 * value should be programmed into the source mux for that peripheral.
347 *
348 * There is special code here to handle the one source type with 5 sources.
349 *
350 * @param periph_id peripheral to start
351 * @param source PLL id of required parent clock
352 * @param mux_bits Set to number of bits in mux register: 2 or 4
353 * @param divider_bits Set to number of divider bits (8 or 16)
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100354 * Return: mux value (0-4, or -1 if not found)
Tom Warren795f9d72013-01-23 14:01:01 -0700355 */
356int get_periph_clock_source(enum periph_id periph_id,
357 enum clock_id parent, int *mux_bits, int *divider_bits);
358
359/*
360 * Convert a device tree clock ID to our peripheral ID. They are mostly
361 * the same but we are very cautious so we check that a valid clock ID is
362 * provided.
363 *
364 * @param clk_id Clock ID according to tegra30 device tree binding
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100365 * Return: peripheral ID, or PERIPH_ID_NONE if the clock ID is invalid
Tom Warren795f9d72013-01-23 14:01:01 -0700366 */
367enum periph_id clk_id_to_periph_id(int clk_id);
368
Svyatoslav Ryhel19a5b032023-02-14 19:35:25 +0200369/*
370 * Convert a device tree clock ID to our PLL ID.
371 *
372 * @param clk_id Clock ID according to tegra device tree binding
373 * Return: clock ID, or CLOCK_ID_NONE if the clock ID is invalid
374 */
375enum clock_id clk_id_to_pll_id(int clk_id);
376
Tom Warren795f9d72013-01-23 14:01:01 -0700377/**
378 * Set the output frequency you want for each PLL clock.
379 * PLL output frequencies are programmed by setting their N, M and P values.
380 * The governing equations are:
381 * VCO = (Fi / m) * n, Fo = VCO / (2^p)
382 * where Fo is the output frequency from the PLL.
383 * Example: Set the output frequency to 216Mhz(Fo) with 12Mhz OSC(Fi)
384 * 216Mhz = ((12Mhz / m) * n) / (2^p) so n=432,m=12,p=1
385 * Please see Tegra TRM section 5.3 to get the detail for PLL Programming
386 *
387 * @param n PLL feedback divider(DIVN)
388 * @param m PLL input divider(DIVN)
389 * @param p post divider(DIVP)
390 * @param cpcon base PLL charge pump(CPCON)
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100391 * Return: 0 if ok, -1 on error (the requested PLL is incorrect and cannot
Robert P. J. Day8d56db92016-07-15 13:44:45 -0400392 * be overridden), 1 if PLL is already correct
Tom Warren795f9d72013-01-23 14:01:01 -0700393 */
394int clock_set_rate(enum clock_id clkid, u32 n, u32 m, u32 p, u32 cpcon);
395
396/* return 1 if a peripheral ID is in range */
397#define clock_type_id_isvalid(id) ((id) >= 0 && \
398 (id) < CLOCK_TYPE_COUNT)
399
400/* return 1 if a periphc_internal_id is in range */
401#define periphc_internal_id_isvalid(id) ((id) >= 0 && \
402 (id) < PERIPHC_COUNT)
403
Tom Warrenfbef3552013-04-01 15:48:54 -0700404/* SoC-specific TSC init */
405void arch_timer_init(void);
406
Jimmy Zhang2a544db2014-01-24 10:37:36 -0700407void tegra30_set_up_pllp(void);
408
Thierry Redingfa6e24d2015-08-20 11:42:19 +0200409/* Number of PLL-based clocks (i.e. not OSC, MCLK or 32KHz) */
410#define CLOCK_ID_PLL_COUNT (CLOCK_ID_COUNT - 3)
Tom Warrena8480ef2015-06-25 09:50:44 -0700411
412struct clk_pll_info {
413 u32 m_shift:5; /* DIVM_SHIFT */
414 u32 n_shift:5; /* DIVN_SHIFT */
415 u32 p_shift:5; /* DIVP_SHIFT */
416 u32 kcp_shift:5; /* KCP/cpcon SHIFT */
417 u32 kvco_shift:5; /* KVCO/lfcon SHIFT */
418 u32 lock_ena:6; /* LOCK_ENABLE/EN_LOCKDET shift */
419 u32 rsvd:1;
420 u32 m_mask:10; /* DIVM_MASK */
421 u32 n_mask:12; /* DIVN_MASK */
422 u32 p_mask:10; /* DIVP_MASK or VCO_MASK */
423 u32 kcp_mask:10; /* KCP/CPCON MASK */
424 u32 kvco_mask:10; /* KVCO/LFCON MASK */
425 u32 lock_det:6; /* LOCK_DETECT/LOCKED shift */
426 u32 rsvd2:6;
427};
428extern struct clk_pll_info tegra_pll_info_table[CLOCK_ID_PLL_COUNT];
429
Stephen Warren1453d102016-09-13 10:45:55 -0600430struct periph_clk_init {
431 enum periph_id periph_id;
432 enum clock_id parent_clock_id;
433};
434extern struct periph_clk_init periph_clk_init_table[];
435
Simon Glasscd4b59b2015-06-05 14:39:36 -0600436/**
437 * Enable output clock for external peripherals
438 *
439 * @param clk_id Clock ID to output (1, 2 or 3)
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100440 * Return: 0 if OK. -ve on error
Simon Glasscd4b59b2015-06-05 14:39:36 -0600441 */
442int clock_external_output(int clk_id);
443
Tom Warren795f9d72013-01-23 14:01:01 -0700444#endif /* _TEGRA_CLOCK_H_ */