blob: 1d3ae3864407c42d06f8f0018e2e1aeae2d4cb36 [file] [log] [blame]
Simon Glass16134fd2011-08-30 06:23:13 +00001/*
2 * Copyright (c) 2011 The Chromium OS Authors.
3 * See file CREDITS for list of people who contributed to this
4 * project.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of
9 * the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
19 * MA 02111-1307 USA
20 */
21
22/* Tegra2 clock control functions */
23
24#ifndef _CLOCK_H
Simon Glass069784e2011-09-21 12:40:02 +000025#define _CLOCK_H
Simon Glass16134fd2011-08-30 06:23:13 +000026
27/* Set of oscillator frequencies supported in the internal API. */
28enum clock_osc_freq {
29 /* All in MHz, so 13_0 is 13.0MHz */
30 CLOCK_OSC_FREQ_13_0,
31 CLOCK_OSC_FREQ_19_2,
32 CLOCK_OSC_FREQ_12_0,
33 CLOCK_OSC_FREQ_26_0,
34
35 CLOCK_OSC_FREQ_COUNT,
36};
37
38/* The PLLs supported by the hardware */
Simon Glass069784e2011-09-21 12:40:02 +000039enum clock_id {
40 CLOCK_ID_FIRST,
41 CLOCK_ID_CGENERAL = CLOCK_ID_FIRST,
42 CLOCK_ID_MEMORY,
43 CLOCK_ID_PERIPH,
44 CLOCK_ID_AUDIO,
45 CLOCK_ID_USB,
46 CLOCK_ID_DISPLAY,
Simon Glass16134fd2011-08-30 06:23:13 +000047
48 /* now the simple ones */
Simon Glass069784e2011-09-21 12:40:02 +000049 CLOCK_ID_FIRST_SIMPLE,
50 CLOCK_ID_XCPU = CLOCK_ID_FIRST_SIMPLE,
51 CLOCK_ID_EPCI,
52 CLOCK_ID_SFROM32KHZ,
Simon Glass16134fd2011-08-30 06:23:13 +000053
Simon Glassc2ea5e42011-09-21 12:40:04 +000054 /* These are the base clocks (inputs to the Tegra SOC) */
55 CLOCK_ID_32KHZ,
56 CLOCK_ID_OSC,
57
58 CLOCK_ID_COUNT, /* number of clocks */
59 CLOCK_ID_NONE = -1,
Simon Glass16134fd2011-08-30 06:23:13 +000060};
61
62/* The clocks supported by the hardware */
63enum periph_id {
64 PERIPH_ID_FIRST,
65
66 /* Low word: 31:0 */
67 PERIPH_ID_CPU = PERIPH_ID_FIRST,
68 PERIPH_ID_RESERVED1,
69 PERIPH_ID_RESERVED2,
70 PERIPH_ID_AC97,
71 PERIPH_ID_RTC,
72 PERIPH_ID_TMR,
73 PERIPH_ID_UART1,
74 PERIPH_ID_UART2,
75
76 /* 8 */
77 PERIPH_ID_GPIO,
78 PERIPH_ID_SDMMC2,
79 PERIPH_ID_SPDIF,
80 PERIPH_ID_I2S1,
81 PERIPH_ID_I2C1,
82 PERIPH_ID_NDFLASH,
83 PERIPH_ID_SDMMC1,
84 PERIPH_ID_SDMMC4,
85
86 /* 16 */
87 PERIPH_ID_TWC,
Simon Glass069784e2011-09-21 12:40:02 +000088 PERIPH_ID_PWM,
Simon Glass16134fd2011-08-30 06:23:13 +000089 PERIPH_ID_I2S2,
90 PERIPH_ID_EPP,
91 PERIPH_ID_VI,
92 PERIPH_ID_2D,
93 PERIPH_ID_USBD,
94 PERIPH_ID_ISP,
95
96 /* 24 */
97 PERIPH_ID_3D,
98 PERIPH_ID_IDE,
99 PERIPH_ID_DISP2,
100 PERIPH_ID_DISP1,
101 PERIPH_ID_HOST1X,
102 PERIPH_ID_VCP,
103 PERIPH_ID_RESERVED30,
104 PERIPH_ID_CACHE2,
105
106 /* Middle word: 63:32 */
107 PERIPH_ID_MEM,
108 PERIPH_ID_AHBDMA,
109 PERIPH_ID_APBDMA,
110 PERIPH_ID_RESERVED35,
111 PERIPH_ID_KBC,
112 PERIPH_ID_STAT_MON,
113 PERIPH_ID_PMC,
114 PERIPH_ID_FUSE,
115
116 /* 40 */
117 PERIPH_ID_KFUSE,
118 PERIPH_ID_SBC1,
119 PERIPH_ID_SNOR,
120 PERIPH_ID_SPI1,
121 PERIPH_ID_SBC2,
122 PERIPH_ID_XIO,
123 PERIPH_ID_SBC3,
124 PERIPH_ID_DVC_I2C,
125
126 /* 48 */
127 PERIPH_ID_DSI,
128 PERIPH_ID_TVO,
129 PERIPH_ID_MIPI,
130 PERIPH_ID_HDMI,
131 PERIPH_ID_CSI,
132 PERIPH_ID_TVDAC,
133 PERIPH_ID_I2C2,
134 PERIPH_ID_UART3,
135
136 /* 56 */
137 PERIPH_ID_RESERVED56,
138 PERIPH_ID_EMC,
139 PERIPH_ID_USB2,
140 PERIPH_ID_USB3,
141 PERIPH_ID_MPE,
142 PERIPH_ID_VDE,
143 PERIPH_ID_BSEA,
144 PERIPH_ID_BSEV,
145
146 /* Upper word 95:64 */
147 PERIPH_ID_SPEEDO,
148 PERIPH_ID_UART4,
149 PERIPH_ID_UART5,
150 PERIPH_ID_I2C3,
151 PERIPH_ID_SBC4,
152 PERIPH_ID_SDMMC3,
153 PERIPH_ID_PCIE,
154 PERIPH_ID_OWR,
155
156 /* 72 */
157 PERIPH_ID_AFI,
158 PERIPH_ID_CORESIGHT,
159 PERIPH_ID_RESERVED74,
160 PERIPH_ID_AVPUCQ,
161 PERIPH_ID_RESERVED76,
162 PERIPH_ID_RESERVED77,
163 PERIPH_ID_RESERVED78,
164 PERIPH_ID_RESERVED79,
165
166 /* 80 */
167 PERIPH_ID_RESERVED80,
168 PERIPH_ID_RESERVED81,
169 PERIPH_ID_RESERVED82,
170 PERIPH_ID_RESERVED83,
171 PERIPH_ID_IRAMA,
172 PERIPH_ID_IRAMB,
173 PERIPH_ID_IRAMC,
174 PERIPH_ID_IRAMD,
175
176 /* 88 */
177 PERIPH_ID_CRAM2,
178
179 PERIPH_ID_COUNT,
Simon Glass2966cd22012-03-06 17:10:27 +0000180 PERIPH_ID_NONE = -1,
Simon Glass16134fd2011-08-30 06:23:13 +0000181};
182
183/* Converts a clock number to a clock register: 0=L, 1=H, 2=U */
184#define PERIPH_REG(id) ((id) >> 5)
185
186/* Mask value for a clock (within PERIPH_REG(id)) */
187#define PERIPH_MASK(id) (1 << ((id) & 0x1f))
188
189/* return 1 if a PLL ID is in range */
Simon Glass069784e2011-09-21 12:40:02 +0000190#define clock_id_isvalid(id) ((id) >= CLOCK_ID_FIRST && (id) < CLOCK_ID_COUNT)
Simon Glass16134fd2011-08-30 06:23:13 +0000191
Simon Glass16134fd2011-08-30 06:23:13 +0000192/* PLL stabilization delay in usec */
193#define CLOCK_PLL_STABLE_DELAY_US 300
194
195/* return the current oscillator clock frequency */
196enum clock_osc_freq clock_get_osc_freq(void);
197
Simon Glass069784e2011-09-21 12:40:02 +0000198/**
Simon Glass16134fd2011-08-30 06:23:13 +0000199 * Start PLL using the provided configuration parameters.
200 *
201 * @param id clock id
202 * @param divm input divider
203 * @param divn feedback divider
204 * @param divp post divider 2^n
205 * @param cpcon charge pump setup control
206 * @param lfcon loop filter setup control
207 *
208 * @returns monotonic time in us that the PLL will be stable
209 */
Simon Glass069784e2011-09-21 12:40:02 +0000210unsigned long clock_start_pll(enum clock_id id, u32 divm, u32 divn,
Simon Glass16134fd2011-08-30 06:23:13 +0000211 u32 divp, u32 cpcon, u32 lfcon);
212
Simon Glass01df75f2012-04-02 13:18:47 +0000213/**
214 * Read low-level parameters of a PLL.
215 *
216 * @param id clock id to read (note: USB is not supported)
217 * @param divm returns input divider
218 * @param divn returns feedback divider
219 * @param divp returns post divider 2^n
220 * @param cpcon returns charge pump setup control
221 * @param lfcon returns loop filter setup control
222 *
223 * @returns 0 if ok, -1 on error (invalid clock id)
224 */
225int clock_ll_read_pll(enum clock_id clkid, u32 *divm, u32 *divn,
226 u32 *divp, u32 *cpcon, u32 *lfcon);
227
Simon Glass16134fd2011-08-30 06:23:13 +0000228/*
229 * Enable a clock
230 *
231 * @param id clock id
232 */
233void clock_enable(enum periph_id clkid);
234
235/*
Simon Glassc2ea5e42011-09-21 12:40:04 +0000236 * Disable a clock
237 *
238 * @param id clock id
239 */
240void clock_disable(enum periph_id clkid);
241
242/*
Simon Glass16134fd2011-08-30 06:23:13 +0000243 * Set whether a clock is enabled or disabled.
244 *
245 * @param id clock id
246 * @param enable 1 to enable, 0 to disable
247 */
248void clock_set_enable(enum periph_id clkid, int enable);
249
Simon Glass069784e2011-09-21 12:40:02 +0000250/**
Simon Glass16134fd2011-08-30 06:23:13 +0000251 * Reset a peripheral. This puts it in reset, waits for a delay, then takes
252 * it out of reset and waits for th delay again.
253 *
254 * @param periph_id peripheral to reset
255 * @param us_delay time to delay in microseconds
256 */
257void reset_periph(enum periph_id periph_id, int us_delay);
258
Simon Glass069784e2011-09-21 12:40:02 +0000259/**
Simon Glass16134fd2011-08-30 06:23:13 +0000260 * Put a peripheral into or out of reset.
261 *
262 * @param periph_id peripheral to reset
263 * @param enable 1 to put into reset, 0 to take out of reset
264 */
265void reset_set_enable(enum periph_id periph_id, int enable);
266
267
268/* CLK_RST_CONTROLLER_RST_CPU_CMPLX_SET/CLR_0 */
269enum crc_reset_id {
270 /* Things we can hold in reset for each CPU */
271 crc_rst_cpu = 1,
272 crc_rst_de = 1 << 2, /* What is de? */
273 crc_rst_watchdog = 1 << 3,
274 crc_rst_debug = 1 << 4,
275};
276
Simon Glass069784e2011-09-21 12:40:02 +0000277/**
Simon Glass16134fd2011-08-30 06:23:13 +0000278 * Put parts of the CPU complex into or out of reset.\
279 *
280 * @param cpu cpu number (0 or 1 on Tegra2)
281 * @param which which parts of the complex to affect (OR of crc_reset_id)
282 * @param reset 1 to assert reset, 0 to de-assert
283 */
284void reset_cmplx_set_enable(int cpu, int which, int reset);
285
Simon Glassc2ea5e42011-09-21 12:40:04 +0000286/**
287 * Set the source for a peripheral clock. This plus the divisor sets the
288 * clock rate. You need to look up the datasheet to see the meaning of the
289 * source parameter as it changes for each peripheral.
290 *
291 * Warning: This function is only for use pre-relocation. Please use
292 * clock_start_periph_pll() instead.
293 *
294 * @param periph_id peripheral to adjust
295 * @param source source clock (0, 1, 2 or 3)
296 */
297void clock_ll_set_source(enum periph_id periph_id, unsigned source);
298
299/**
300 * Set the source and divisor for a peripheral clock. This sets the
301 * clock rate. You need to look up the datasheet to see the meaning of the
302 * source parameter as it changes for each peripheral.
303 *
304 * Warning: This function is only for use pre-relocation. Please use
305 * clock_start_periph_pll() instead.
306 *
307 * @param periph_id peripheral to adjust
308 * @param source source clock (0, 1, 2 or 3)
309 * @param divisor divisor value to use
310 */
311void clock_ll_set_source_divisor(enum periph_id periph_id, unsigned source,
312 unsigned divisor);
313
314/**
315 * Start a peripheral PLL clock at the given rate. This also resets the
316 * peripheral.
317 *
318 * @param periph_id peripheral to start
319 * @param parent PLL id of required parent clock
320 * @param rate Required clock rate in Hz
321 * @return rate selected in Hz, or -1U if something went wrong
322 */
323unsigned clock_start_periph_pll(enum periph_id periph_id,
324 enum clock_id parent, unsigned rate);
325
326/**
327 * Returns the rate of a peripheral clock in Hz. Since the caller almost
328 * certainly knows the parent clock (having just set it) we require that
329 * this be passed in so we don't need to work it out.
330 *
331 * @param periph_id peripheral to start
332 * @param parent PLL id of parent clock (used to calculate rate, you
333 * must know this!)
334 * @return clock rate of peripheral in Hz
335 */
336unsigned long clock_get_periph_rate(enum periph_id periph_id,
337 enum clock_id parent);
338
339/**
340 * Adjust peripheral PLL clock to the given rate. This does not reset the
341 * peripheral. If a second stage divisor is not available, pass NULL for
342 * extra_div. If it is available, then this parameter will return the
343 * divisor selected (which will be a power of 2 from 1 to 256).
344 *
345 * @param periph_id peripheral to start
346 * @param parent PLL id of required parent clock
347 * @param rate Required clock rate in Hz
348 * @param extra_div value for the second-stage divisor (NULL if one is
349 not available)
350 * @return rate selected in Hz, or -1U if something went wrong
351 */
352unsigned clock_adjust_periph_pll_div(enum periph_id periph_id,
353 enum clock_id parent, unsigned rate, int *extra_div);
354
355/**
356 * Returns the clock rate of a specified clock, in Hz.
357 *
358 * @param parent PLL id of clock to check
359 * @return rate of clock in Hz
360 */
361unsigned clock_get_rate(enum clock_id clkid);
362
Simon Glass2ffbb252011-11-28 15:04:37 +0000363/**
364 * Start up a UART using low-level calls
365 *
366 * Prior to relocation clock_start_periph_pll() cannot be called. This
367 * function provides a way to set up a UART using low-level calls which
368 * do not require BSS.
369 *
370 * @param periph_id Peripheral ID of UART to enable (e,g, PERIPH_ID_UART1)
371 */
372void clock_ll_start_uart(enum periph_id periph_id);
373
Simon Glass2966cd22012-03-06 17:10:27 +0000374/**
375 * Decode a peripheral ID from a device tree node.
376 *
377 * This works by looking up the peripheral's 'clocks' node and reading out
378 * the second cell, which is the clock number / peripheral ID.
379 *
380 * @param blob FDT blob to use
381 * @param node Node to look at
382 * @return peripheral ID, or PERIPH_ID_NONE if none
383 */
384enum periph_id clock_decode_periph_id(const void *blob, int node);
385
Simon Glass01df75f2012-04-02 13:18:47 +0000386/**
387 * Checks if the oscillator bypass is enabled (XOBP bit)
388 *
389 * @return 1 if bypass is enabled, 0 if not
390 */
391int clock_get_osc_bypass(void);
392
Simon Glassc2ea5e42011-09-21 12:40:04 +0000393/*
394 * Checks that clocks are valid and prints a warning if not
395 *
396 * @return 0 if ok, -1 on error
397 */
398int clock_verify(void);
399
400/* Initialize the clocks */
401void clock_init(void);
402
403/* Initialize the PLLs */
404void clock_early_init(void);
405
Simon Glass16134fd2011-08-30 06:23:13 +0000406#endif