blob: 74bd22be1aeb19de76bc518f8e7ddc8553a231c6 [file] [log] [blame]
Tom Warrenb2871032012-12-11 13:34:15 +00001/*
Tom Warrenf29f0862013-01-23 14:01:01 -07002 * Copyright (c) 2010-2013, NVIDIA CORPORATION. All rights reserved.
Tom Warrenb2871032012-12-11 13:34:15 +00003 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17/* Tegra30 Clock control functions */
18
19#include <common.h>
20#include <asm/io.h>
21#include <asm/arch/clock.h>
22#include <asm/arch/tegra.h>
23#include <asm/arch-tegra/clk_rst.h>
24#include <asm/arch-tegra/timer.h>
25#include <div64.h>
26#include <fdtdec.h>
27
28/*
Tom Warrenf29f0862013-01-23 14:01:01 -070029 * Clock types that we can use as a source. The Tegra30 has muxes for the
Tom Warrenb2871032012-12-11 13:34:15 +000030 * peripheral clocks, and in most cases there are four options for the clock
31 * source. This gives us a clock 'type' and exploits what commonality exists
32 * in the device.
33 *
34 * Letters are obvious, except for T which means CLK_M, and S which means the
35 * clock derived from 32KHz. Beware that CLK_M (also called OSC in the
36 * datasheet) and PLL_M are different things. The former is the basic
37 * clock supplied to the SOC from an external oscillator. The latter is the
38 * memory clock PLL.
39 *
40 * See definitions in clock_id in the header file.
41 */
42enum clock_type_id {
43 CLOCK_TYPE_AXPT, /* PLL_A, PLL_X, PLL_P, CLK_M */
44 CLOCK_TYPE_MCPA, /* and so on */
45 CLOCK_TYPE_MCPT,
46 CLOCK_TYPE_PCM,
47 CLOCK_TYPE_PCMT,
Tom Warren619bd992012-12-21 15:02:45 -070048 CLOCK_TYPE_PCMT16,
Tom Warrenb2871032012-12-11 13:34:15 +000049 CLOCK_TYPE_PDCT,
50 CLOCK_TYPE_ACPT,
51 CLOCK_TYPE_ASPTE,
52 CLOCK_TYPE_PMDACD2T,
53 CLOCK_TYPE_PCST,
54
55 CLOCK_TYPE_COUNT,
Tom Warrenf29f0862013-01-23 14:01:01 -070056 CLOCK_TYPE_NONE = -1, /* invalid clock type */
Tom Warrenb2871032012-12-11 13:34:15 +000057};
58
59enum {
Tom Warrenf29f0862013-01-23 14:01:01 -070060 CLOCK_MAX_MUX = 8 /* number of source options for each clock */
61};
62
63enum {
64 MASK_BITS_31_30 = 2, /* num of bits used to specify clock source */
Tom Warrenb2871032012-12-11 13:34:15 +000065 MASK_BITS_31_29,
66 MASK_BITS_29_28,
67};
68
69/*
70 * Clock source mux for each clock type. This just converts our enum into
71 * a list of mux sources for use by the code.
72 *
73 * Note:
74 * The extra column in each clock source array is used to store the mask
75 * bits in its register for the source.
76 */
77#define CLK(x) CLOCK_ID_ ## x
78static enum clock_id clock_source[CLOCK_TYPE_COUNT][CLOCK_MAX_MUX+1] = {
Tom Warrenf29f0862013-01-23 14:01:01 -070079 { CLK(AUDIO), CLK(XCPU), CLK(PERIPH), CLK(OSC),
80 CLK(NONE), CLK(NONE), CLK(NONE), CLK(NONE),
Tom Warrenb2871032012-12-11 13:34:15 +000081 MASK_BITS_31_30},
Tom Warrenf29f0862013-01-23 14:01:01 -070082 { CLK(MEMORY), CLK(CGENERAL), CLK(PERIPH), CLK(AUDIO),
83 CLK(NONE), CLK(NONE), CLK(NONE), CLK(NONE),
Tom Warrenb2871032012-12-11 13:34:15 +000084 MASK_BITS_31_30},
Tom Warrenf29f0862013-01-23 14:01:01 -070085 { CLK(MEMORY), CLK(CGENERAL), CLK(PERIPH), CLK(OSC),
86 CLK(NONE), CLK(NONE), CLK(NONE), CLK(NONE),
Tom Warrenb2871032012-12-11 13:34:15 +000087 MASK_BITS_31_30},
Tom Warrenf29f0862013-01-23 14:01:01 -070088 { CLK(PERIPH), CLK(CGENERAL), CLK(MEMORY), CLK(NONE),
89 CLK(NONE), CLK(NONE), CLK(NONE), CLK(NONE),
Tom Warrenb2871032012-12-11 13:34:15 +000090 MASK_BITS_31_30},
Tom Warrenf29f0862013-01-23 14:01:01 -070091 { CLK(PERIPH), CLK(CGENERAL), CLK(MEMORY), CLK(OSC),
92 CLK(NONE), CLK(NONE), CLK(NONE), CLK(NONE),
Tom Warrenb2871032012-12-11 13:34:15 +000093 MASK_BITS_31_30},
Tom Warrenf29f0862013-01-23 14:01:01 -070094 { CLK(PERIPH), CLK(CGENERAL), CLK(MEMORY), CLK(OSC),
95 CLK(NONE), CLK(NONE), CLK(NONE), CLK(NONE),
Tom Warren619bd992012-12-21 15:02:45 -070096 MASK_BITS_31_30},
Tom Warrenf29f0862013-01-23 14:01:01 -070097 { CLK(PERIPH), CLK(DISPLAY), CLK(CGENERAL), CLK(OSC),
98 CLK(NONE), CLK(NONE), CLK(NONE), CLK(NONE),
Tom Warrenb2871032012-12-11 13:34:15 +000099 MASK_BITS_31_30},
Tom Warrenf29f0862013-01-23 14:01:01 -0700100 { CLK(AUDIO), CLK(CGENERAL), CLK(PERIPH), CLK(OSC),
101 CLK(NONE), CLK(NONE), CLK(NONE), CLK(NONE),
Tom Warrenb2871032012-12-11 13:34:15 +0000102 MASK_BITS_31_30},
Tom Warrenf29f0862013-01-23 14:01:01 -0700103 { CLK(AUDIO), CLK(SFROM32KHZ), CLK(PERIPH), CLK(OSC),
104 CLK(EPCI), CLK(NONE), CLK(NONE), CLK(NONE),
Tom Warrenb2871032012-12-11 13:34:15 +0000105 MASK_BITS_31_29},
Tom Warrenf29f0862013-01-23 14:01:01 -0700106 { CLK(PERIPH), CLK(MEMORY), CLK(DISPLAY), CLK(AUDIO),
107 CLK(CGENERAL), CLK(DISPLAY2), CLK(OSC), CLK(NONE),
Tom Warrenb2871032012-12-11 13:34:15 +0000108 MASK_BITS_31_29},
Tom Warrenf29f0862013-01-23 14:01:01 -0700109 { CLK(PERIPH), CLK(CGENERAL), CLK(SFROM32KHZ), CLK(OSC),
110 CLK(NONE), CLK(NONE), CLK(NONE), CLK(NONE),
Tom Warrenb2871032012-12-11 13:34:15 +0000111 MASK_BITS_29_28}
112};
113
Tom Warrenb2871032012-12-11 13:34:15 +0000114/*
115 * Clock type for each peripheral clock source. We put the name in each
116 * record just so it is easy to match things up
117 */
118#define TYPE(name, type) type
119static enum clock_type_id clock_periph_type[PERIPHC_COUNT] = {
120 /* 0x00 */
121 TYPE(PERIPHC_I2S1, CLOCK_TYPE_AXPT),
Tom Warrenf29f0862013-01-23 14:01:01 -0700122 TYPE(PERIPHC_I2S2, CLOCK_TYPE_AXPT),
123 TYPE(PERIPHC_SPDIF_OUT, CLOCK_TYPE_AXPT),
124 TYPE(PERIPHC_SPDIF_IN, CLOCK_TYPE_PCM),
125 TYPE(PERIPHC_PWM, CLOCK_TYPE_PCST), /* only PWM uses b29:28 */
126 TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
127 TYPE(PERIPHC_SBC2, CLOCK_TYPE_PCMT),
128 TYPE(PERIPHC_SBC3, CLOCK_TYPE_PCMT),
Tom Warrenb2871032012-12-11 13:34:15 +0000129
130 /* 0x08 */
Tom Warrenf29f0862013-01-23 14:01:01 -0700131 TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
132 TYPE(PERIPHC_I2C1, CLOCK_TYPE_PCMT16),
133 TYPE(PERIPHC_DVC_I2C, CLOCK_TYPE_PCMT16),
134 TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
135 TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
136 TYPE(PERIPHC_SBC1, CLOCK_TYPE_PCMT),
137 TYPE(PERIPHC_DISP1, CLOCK_TYPE_PMDACD2T),
138 TYPE(PERIPHC_DISP2, CLOCK_TYPE_PMDACD2T),
Tom Warrenb2871032012-12-11 13:34:15 +0000139
140 /* 0x10 */
Tom Warrenf29f0862013-01-23 14:01:01 -0700141 TYPE(PERIPHC_CVE, CLOCK_TYPE_PDCT),
142 TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
Tom Warrenb2871032012-12-11 13:34:15 +0000143 TYPE(PERIPHC_VI, CLOCK_TYPE_MCPA),
Tom Warrenf29f0862013-01-23 14:01:01 -0700144 TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
145 TYPE(PERIPHC_SDMMC1, CLOCK_TYPE_PCMT),
Tom Warrenb2871032012-12-11 13:34:15 +0000146 TYPE(PERIPHC_SDMMC2, CLOCK_TYPE_PCMT),
147 TYPE(PERIPHC_G3D, CLOCK_TYPE_MCPA),
148 TYPE(PERIPHC_G2D, CLOCK_TYPE_MCPA),
149
150 /* 0x18 */
151 TYPE(PERIPHC_NDFLASH, CLOCK_TYPE_PCMT),
152 TYPE(PERIPHC_SDMMC4, CLOCK_TYPE_PCMT),
Tom Warrenf29f0862013-01-23 14:01:01 -0700153 TYPE(PERIPHC_VFIR, CLOCK_TYPE_PCMT),
154 TYPE(PERIPHC_EPP, CLOCK_TYPE_MCPA),
155 TYPE(PERIPHC_MPE, CLOCK_TYPE_MCPA),
156 TYPE(PERIPHC_MIPI, CLOCK_TYPE_PCMT), /* MIPI base-band HSI */
157 TYPE(PERIPHC_UART1, CLOCK_TYPE_PCMT),
158 TYPE(PERIPHC_UART2, CLOCK_TYPE_PCMT),
Tom Warrenb2871032012-12-11 13:34:15 +0000159
160 /* 0x20 */
Tom Warrenf29f0862013-01-23 14:01:01 -0700161 TYPE(PERIPHC_HOST1X, CLOCK_TYPE_MCPA),
162 TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
163 TYPE(PERIPHC_TVO, CLOCK_TYPE_PDCT),
164 TYPE(PERIPHC_HDMI, CLOCK_TYPE_PMDACD2T),
165 TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
166 TYPE(PERIPHC_TVDAC, CLOCK_TYPE_PDCT),
167 TYPE(PERIPHC_I2C2, CLOCK_TYPE_PCMT16),
Tom Warrenb2871032012-12-11 13:34:15 +0000168 TYPE(PERIPHC_EMC, CLOCK_TYPE_MCPT),
169
170 /* 0x28 */
171 TYPE(PERIPHC_UART3, CLOCK_TYPE_PCMT),
172 TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
173 TYPE(PERIPHC_VI, CLOCK_TYPE_MCPA),
Tom Warrenf29f0862013-01-23 14:01:01 -0700174 TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
175 TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
176 TYPE(PERIPHC_SBC4, CLOCK_TYPE_PCMT),
177 TYPE(PERIPHC_I2C3, CLOCK_TYPE_PCMT16),
178 TYPE(PERIPHC_SDMMC3, CLOCK_TYPE_PCMT),
Tom Warrenb2871032012-12-11 13:34:15 +0000179
180 /* 0x30 */
181 TYPE(PERIPHC_UART4, CLOCK_TYPE_PCMT),
182 TYPE(PERIPHC_UART5, CLOCK_TYPE_PCMT),
183 TYPE(PERIPHC_VDE, CLOCK_TYPE_PCMT),
Tom Warrenf29f0862013-01-23 14:01:01 -0700184 TYPE(PERIPHC_OWR, CLOCK_TYPE_PCMT),
185 TYPE(PERIPHC_NOR, CLOCK_TYPE_PCMT),
186 TYPE(PERIPHC_CSITE, CLOCK_TYPE_PCMT),
187 TYPE(PERIPHC_I2S0, CLOCK_TYPE_AXPT),
188 TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
Tom Warrenb2871032012-12-11 13:34:15 +0000189
Tom Warrenf29f0862013-01-23 14:01:01 -0700190 /* 0x38h */ /* Jumps to reg offset 0x3B0h - new for T30 */
191 TYPE(PERIPHC_G3D2, CLOCK_TYPE_MCPA),
192 TYPE(PERIPHC_MSELECT, CLOCK_TYPE_PCMT),
193 TYPE(PERIPHC_TSENSOR, CLOCK_TYPE_PCST), /* s/b PCTS */
194 TYPE(PERIPHC_I2S3, CLOCK_TYPE_AXPT),
195 TYPE(PERIPHC_I2S4, CLOCK_TYPE_AXPT),
196 TYPE(PERIPHC_I2C4, CLOCK_TYPE_PCMT16),
197 TYPE(PERIPHC_SBC5, CLOCK_TYPE_PCMT),
198 TYPE(PERIPHC_SBC6, CLOCK_TYPE_PCMT),
Tom Warrenb2871032012-12-11 13:34:15 +0000199
200 /* 0x40 */
Tom Warrenf29f0862013-01-23 14:01:01 -0700201 TYPE(PERIPHC_AUDIO, CLOCK_TYPE_ACPT),
202 TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
203 TYPE(PERIPHC_DAM0, CLOCK_TYPE_ACPT),
204 TYPE(PERIPHC_DAM1, CLOCK_TYPE_ACPT),
205 TYPE(PERIPHC_DAM2, CLOCK_TYPE_ACPT),
Tom Warrenb2871032012-12-11 13:34:15 +0000206 TYPE(PERIPHC_HDA2CODEC2X, CLOCK_TYPE_PCMT),
Tom Warrenf29f0862013-01-23 14:01:01 -0700207 TYPE(PERIPHC_ACTMON, CLOCK_TYPE_PCST), /* MASK 31:30 */
Tom Warrenb2871032012-12-11 13:34:15 +0000208 TYPE(PERIPHC_EXTPERIPH1, CLOCK_TYPE_ASPTE),
209
210 /* 0x48 */
211 TYPE(PERIPHC_EXTPERIPH2, CLOCK_TYPE_ASPTE),
212 TYPE(PERIPHC_EXTPERIPH3, CLOCK_TYPE_ASPTE),
Tom Warrenf29f0862013-01-23 14:01:01 -0700213 TYPE(PERIPHC_NANDSPEED, CLOCK_TYPE_PCMT),
214 TYPE(PERIPHC_I2CSLOW, CLOCK_TYPE_PCST), /* MASK 31:30 */
215 TYPE(PERIPHC_SYS, CLOCK_TYPE_NONE),
216 TYPE(PERIPHC_SPEEDO, CLOCK_TYPE_PCMT),
217 TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
218 TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
Tom Warrenb2871032012-12-11 13:34:15 +0000219
220 /* 0x50 */
Tom Warrenf29f0862013-01-23 14:01:01 -0700221 TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
222 TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
223 TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
224 TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
225 TYPE(PERIPHC_SATAOOB, CLOCK_TYPE_PCMT), /* offset 0x420h */
226 TYPE(PERIPHC_SATA, CLOCK_TYPE_PCMT),
227 TYPE(PERIPHC_HDA, CLOCK_TYPE_PCMT),
Tom Warrenb2871032012-12-11 13:34:15 +0000228};
229
230/*
231 * This array translates a periph_id to a periphc_internal_id
232 *
233 * Not present/matched up:
234 * uint vi_sensor; _VI_SENSOR_0, 0x1A8
235 * SPDIF - which is both 0x08 and 0x0c
236 *
237 */
238#define NONE(name) (-1)
239#define OFFSET(name, value) PERIPHC_ ## name
240static s8 periph_id_to_internal_id[PERIPH_ID_COUNT] = {
241 /* Low word: 31:0 */
242 NONE(CPU),
243 NONE(COP),
244 NONE(TRIGSYS),
245 NONE(RESERVED3),
246 NONE(RESERVED4),
247 NONE(TMR),
248 PERIPHC_UART1,
Tom Warrenf29f0862013-01-23 14:01:01 -0700249 PERIPHC_UART2, /* and vfir 0x68 */
Tom Warrenb2871032012-12-11 13:34:15 +0000250
251 /* 8 */
252 NONE(GPIO),
253 PERIPHC_SDMMC2,
Tom Warrenf29f0862013-01-23 14:01:01 -0700254 NONE(SPDIF), /* 0x08 and 0x0c, unclear which to use */
Tom Warrenb2871032012-12-11 13:34:15 +0000255 PERIPHC_I2S1,
256 PERIPHC_I2C1,
257 PERIPHC_NDFLASH,
258 PERIPHC_SDMMC1,
259 PERIPHC_SDMMC4,
260
261 /* 16 */
262 NONE(RESERVED16),
263 PERIPHC_PWM,
264 PERIPHC_I2S2,
265 PERIPHC_EPP,
266 PERIPHC_VI,
267 PERIPHC_G2D,
268 NONE(USBD),
269 NONE(ISP),
270
271 /* 24 */
272 PERIPHC_G3D,
273 NONE(RESERVED25),
274 PERIPHC_DISP2,
275 PERIPHC_DISP1,
276 PERIPHC_HOST1X,
277 NONE(VCP),
278 PERIPHC_I2S0,
279 NONE(CACHE2),
280
281 /* Middle word: 63:32 */
282 NONE(MEM),
283 NONE(AHBDMA),
284 NONE(APBDMA),
285 NONE(RESERVED35),
286 NONE(RESERVED36),
287 NONE(STAT_MON),
288 NONE(RESERVED38),
289 NONE(RESERVED39),
290
291 /* 40 */
292 NONE(KFUSE),
Allen Martin7d54f022013-01-29 13:51:25 +0000293 PERIPHC_SBC1,
Tom Warrenb2871032012-12-11 13:34:15 +0000294 PERIPHC_NOR,
295 NONE(RESERVED43),
296 PERIPHC_SBC2,
297 NONE(RESERVED45),
298 PERIPHC_SBC3,
299 PERIPHC_DVC_I2C,
300
301 /* 48 */
302 NONE(DSI),
Tom Warrenf29f0862013-01-23 14:01:01 -0700303 PERIPHC_TVO, /* also CVE 0x40 */
Tom Warrenb2871032012-12-11 13:34:15 +0000304 PERIPHC_MIPI,
305 PERIPHC_HDMI,
306 NONE(CSI),
307 PERIPHC_TVDAC,
308 PERIPHC_I2C2,
309 PERIPHC_UART3,
310
311 /* 56 */
312 NONE(RESERVED56),
313 PERIPHC_EMC,
314 NONE(USB2),
315 NONE(USB3),
316 PERIPHC_MPE,
317 PERIPHC_VDE,
318 NONE(BSEA),
319 NONE(BSEV),
320
321 /* Upper word 95:64 */
322 PERIPHC_SPEEDO,
323 PERIPHC_UART4,
324 PERIPHC_UART5,
325 PERIPHC_I2C3,
326 PERIPHC_SBC4,
327 PERIPHC_SDMMC3,
328 NONE(PCIE),
329 PERIPHC_OWR,
330
331 /* 72 */
332 NONE(AFI),
333 PERIPHC_CSITE,
334 NONE(PCIEXCLK),
335 NONE(AVPUCQ),
336 NONE(RESERVED76),
337 NONE(RESERVED77),
338 NONE(RESERVED78),
339 NONE(DTV),
340
341 /* 80 */
342 PERIPHC_NANDSPEED,
343 PERIPHC_I2CSLOW,
344 NONE(DSIB),
345 NONE(RESERVED83),
346 NONE(IRAMA),
347 NONE(IRAMB),
348 NONE(IRAMC),
349 NONE(IRAMD),
350
351 /* 88 */
352 NONE(CRAM2),
353 NONE(RESERVED89),
354 NONE(MDOUBLER),
355 NONE(RESERVED91),
356 NONE(SUSOUT),
357 NONE(RESERVED93),
358 NONE(RESERVED94),
359 NONE(RESERVED95),
360
361 /* V word: 31:0 */
362 NONE(CPUG),
363 NONE(CPULP),
364 PERIPHC_G3D2,
365 PERIPHC_MSELECT,
366 PERIPHC_TSENSOR,
367 PERIPHC_I2S3,
368 PERIPHC_I2S4,
369 PERIPHC_I2C4,
370
371 /* 08 */
372 PERIPHC_SBC5,
373 PERIPHC_SBC6,
374 PERIPHC_AUDIO,
375 NONE(APBIF),
376 PERIPHC_DAM0,
377 PERIPHC_DAM1,
378 PERIPHC_DAM2,
379 PERIPHC_HDA2CODEC2X,
380
381 /* 16 */
382 NONE(ATOMICS),
383 NONE(RESERVED17),
384 NONE(RESERVED18),
385 NONE(RESERVED19),
386 NONE(RESERVED20),
387 NONE(RESERVED21),
388 NONE(RESERVED22),
389 PERIPHC_ACTMON,
390
391 /* 24 */
392 NONE(RESERVED24),
393 NONE(RESERVED25),
394 NONE(RESERVED26),
395 NONE(RESERVED27),
396 PERIPHC_SATA,
397 PERIPHC_HDA,
398 NONE(RESERVED30),
399 NONE(RESERVED31),
400
401 /* W word: 31:0 */
402 NONE(HDA2HDMICODEC),
403 NONE(SATACOLD),
404 NONE(RESERVED0_PCIERX0),
405 NONE(RESERVED1_PCIERX1),
406 NONE(RESERVED2_PCIERX2),
407 NONE(RESERVED3_PCIERX3),
408 NONE(RESERVED4_PCIERX4),
409 NONE(RESERVED5_PCIERX5),
410
411 /* 40 */
412 NONE(CEC),
413 NONE(RESERVED6_PCIE2),
414 NONE(RESERVED7_EMC),
415 NONE(RESERVED8_HDMI),
416 NONE(RESERVED9_SATA),
417 NONE(RESERVED10_MIPI),
418 NONE(EX_RESERVED46),
419 NONE(EX_RESERVED47),
420};
421
422/*
423 * Get the oscillator frequency, from the corresponding hardware configuration
Tom Warrenf29f0862013-01-23 14:01:01 -0700424 * field. Note that T30 supports 3 new higher freqs, but we map back
425 * to the old T20 freqs. Support for the higher oscillators is TBD.
Tom Warrenb2871032012-12-11 13:34:15 +0000426 */
427enum clock_osc_freq clock_get_osc_freq(void)
428{
429 struct clk_rst_ctlr *clkrst =
430 (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
431 u32 reg;
432
433 reg = readl(&clkrst->crc_osc_ctrl);
Tom Warrenf29f0862013-01-23 14:01:01 -0700434 reg = (reg & OSC_FREQ_MASK) >> OSC_FREQ_SHIFT;
Tom Warrenb2871032012-12-11 13:34:15 +0000435
Tom Warrenf29f0862013-01-23 14:01:01 -0700436 if (reg & 1) /* one of the newer freqs */
437 printf("Warning: OSC_FREQ is unsupported! (%d)\n", reg);
Tom Warrenb2871032012-12-11 13:34:15 +0000438
Tom Warrenf29f0862013-01-23 14:01:01 -0700439 return reg >> 2; /* Map to most common (T20) freqs */
Tom Warrenb2871032012-12-11 13:34:15 +0000440}
441
442/* Returns a pointer to the clock source register for a peripheral */
Tom Warrenf29f0862013-01-23 14:01:01 -0700443u32 *get_periph_source_reg(enum periph_id periph_id)
Tom Warrenb2871032012-12-11 13:34:15 +0000444{
445 struct clk_rst_ctlr *clkrst =
Tom Warrenf29f0862013-01-23 14:01:01 -0700446 (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
Tom Warrenb2871032012-12-11 13:34:15 +0000447 enum periphc_internal_id internal_id;
448
449 /* Coresight is a special case */
450 if (periph_id == PERIPH_ID_CSI)
451 return &clkrst->crc_clk_src[PERIPH_ID_CSI+1];
452
453 assert(periph_id >= PERIPH_ID_FIRST && periph_id < PERIPH_ID_COUNT);
454 internal_id = periph_id_to_internal_id[periph_id];
455 assert(internal_id != -1);
456 if (internal_id >= PERIPHC_VW_FIRST) {
457 internal_id -= PERIPHC_VW_FIRST;
458 return &clkrst->crc_clk_src_vw[internal_id];
459 } else
460 return &clkrst->crc_clk_src[internal_id];
461}
462
Tom Warrenb2871032012-12-11 13:34:15 +0000463/**
464 * Given a peripheral ID and the required source clock, this returns which
465 * value should be programmed into the source mux for that peripheral.
466 *
467 * There is special code here to handle the one source type with 5 sources.
468 *
469 * @param periph_id peripheral to start
470 * @param source PLL id of required parent clock
471 * @param mux_bits Set to number of bits in mux register: 2 or 4
Tom Warrenf29f0862013-01-23 14:01:01 -0700472 * @param divider_bits Set to number of divider bits (8 or 16)
Tom Warrenb2871032012-12-11 13:34:15 +0000473 * @return mux value (0-4, or -1 if not found)
474 */
Tom Warrenf29f0862013-01-23 14:01:01 -0700475int get_periph_clock_source(enum periph_id periph_id,
476 enum clock_id parent, int *mux_bits, int *divider_bits)
Tom Warrenb2871032012-12-11 13:34:15 +0000477{
478 enum clock_type_id type;
479 enum periphc_internal_id internal_id;
480 int mux;
481
482 assert(clock_periph_id_isvalid(periph_id));
483
484 internal_id = periph_id_to_internal_id[periph_id];
485 assert(periphc_internal_id_isvalid(internal_id));
486
487 type = clock_periph_type[internal_id];
488 assert(clock_type_id_isvalid(type));
489
490 *mux_bits = clock_source[type][CLOCK_MAX_MUX];
491
Tom Warren619bd992012-12-21 15:02:45 -0700492 if (type == CLOCK_TYPE_PCMT16)
493 *divider_bits = 16;
494 else
495 *divider_bits = 8;
496
Tom Warrenb2871032012-12-11 13:34:15 +0000497 for (mux = 0; mux < CLOCK_MAX_MUX; mux++)
498 if (clock_source[type][mux] == parent)
499 return mux;
500
501 /* if we get here, either us or the caller has made a mistake */
502 printf("Caller requested bad clock: periph=%d, parent=%d\n", periph_id,
503 parent);
504 return -1;
505}
506
Tom Warrenb2871032012-12-11 13:34:15 +0000507void clock_set_enable(enum periph_id periph_id, int enable)
508{
509 struct clk_rst_ctlr *clkrst =
510 (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
511 u32 *clk;
512 u32 reg;
513
514 /* Enable/disable the clock to this peripheral */
515 assert(clock_periph_id_isvalid(periph_id));
516 if ((int)periph_id < (int)PERIPH_ID_VW_FIRST)
517 clk = &clkrst->crc_clk_out_enb[PERIPH_REG(periph_id)];
518 else
519 clk = &clkrst->crc_clk_out_enb_vw[PERIPH_REG(periph_id)];
520 reg = readl(clk);
521 if (enable)
522 reg |= PERIPH_MASK(periph_id);
523 else
524 reg &= ~PERIPH_MASK(periph_id);
525 writel(reg, clk);
526}
527
Tom Warrenb2871032012-12-11 13:34:15 +0000528void reset_set_enable(enum periph_id periph_id, int enable)
529{
530 struct clk_rst_ctlr *clkrst =
531 (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
532 u32 *reset;
533 u32 reg;
534
535 /* Enable/disable reset to the peripheral */
536 assert(clock_periph_id_isvalid(periph_id));
537 if (periph_id < PERIPH_ID_VW_FIRST)
538 reset = &clkrst->crc_rst_dev[PERIPH_REG(periph_id)];
539 else
540 reset = &clkrst->crc_rst_dev_vw[PERIPH_REG(periph_id)];
541 reg = readl(reset);
542 if (enable)
543 reg |= PERIPH_MASK(periph_id);
544 else
545 reg &= ~PERIPH_MASK(periph_id);
546 writel(reg, reset);
547}
548
Tom Warrenb2871032012-12-11 13:34:15 +0000549#ifdef CONFIG_OF_CONTROL
550/*
551 * Convert a device tree clock ID to our peripheral ID. They are mostly
552 * the same but we are very cautious so we check that a valid clock ID is
553 * provided.
554 *
Tom Warren619bd992012-12-21 15:02:45 -0700555 * @param clk_id Clock ID according to tegra30 device tree binding
Tom Warrenb2871032012-12-11 13:34:15 +0000556 * @return peripheral ID, or PERIPH_ID_NONE if the clock ID is invalid
557 */
Tom Warrenf29f0862013-01-23 14:01:01 -0700558enum periph_id clk_id_to_periph_id(int clk_id)
Tom Warrenb2871032012-12-11 13:34:15 +0000559{
Tom Warren619bd992012-12-21 15:02:45 -0700560 if (clk_id > PERIPH_ID_COUNT)
Tom Warrenb2871032012-12-11 13:34:15 +0000561 return PERIPH_ID_NONE;
562
563 switch (clk_id) {
Tom Warren619bd992012-12-21 15:02:45 -0700564 case PERIPH_ID_RESERVED3:
565 case PERIPH_ID_RESERVED4:
566 case PERIPH_ID_RESERVED16:
567 case PERIPH_ID_RESERVED24:
568 case PERIPH_ID_RESERVED35:
569 case PERIPH_ID_RESERVED43:
570 case PERIPH_ID_RESERVED45:
571 case PERIPH_ID_RESERVED56:
572 case PERIPH_ID_RESERVED76:
573 case PERIPH_ID_RESERVED77:
574 case PERIPH_ID_RESERVED78:
575 case PERIPH_ID_RESERVED83:
576 case PERIPH_ID_RESERVED89:
577 case PERIPH_ID_RESERVED91:
578 case PERIPH_ID_RESERVED93:
579 case PERIPH_ID_RESERVED94:
580 case PERIPH_ID_RESERVED95:
Tom Warrenb2871032012-12-11 13:34:15 +0000581 return PERIPH_ID_NONE;
582 default:
583 return clk_id;
584 }
585}
Tom Warrenb2871032012-12-11 13:34:15 +0000586#endif /* CONFIG_OF_CONTROL */
587
Tom Warrenb2871032012-12-11 13:34:15 +0000588void clock_early_init(void)
589{
590 /*
591 * PLLP output frequency set to 408Mhz
592 * PLLC output frequency set to 228Mhz
593 */
594 switch (clock_get_osc_freq()) {
595 case CLOCK_OSC_FREQ_12_0: /* OSC is 12Mhz */
596 clock_set_rate(CLOCK_ID_PERIPH, 408, 12, 0, 8);
597 clock_set_rate(CLOCK_ID_CGENERAL, 456, 12, 1, 8);
598 break;
599
600 case CLOCK_OSC_FREQ_26_0: /* OSC is 26Mhz */
601 clock_set_rate(CLOCK_ID_PERIPH, 408, 26, 0, 8);
602 clock_set_rate(CLOCK_ID_CGENERAL, 600, 26, 0, 8);
603 break;
604
605 case CLOCK_OSC_FREQ_13_0: /* OSC is 13Mhz */
606 clock_set_rate(CLOCK_ID_PERIPH, 408, 13, 0, 8);
607 clock_set_rate(CLOCK_ID_CGENERAL, 600, 13, 0, 8);
608 break;
609 case CLOCK_OSC_FREQ_19_2:
610 default:
611 /*
612 * These are not supported. It is too early to print a
613 * message and the UART likely won't work anyway due to the
614 * oscillator being wrong.
615 */
616 break;
617 }
618}
Tom Warrenb40f7342013-04-01 15:48:54 -0700619
620void arch_timer_init(void)
621{
622}