blob: 11d2346d83b0fda80052b79f68ac3583779ba990 [file] [log] [blame]
Simon Glassb4ba2be2011-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#include <asm/io.h>
25#include <asm/arch/clk_rst.h>
26#include <asm/arch/clock.h>
27#include <asm/arch/timer.h>
28#include <asm/arch/tegra2.h>
29#include <common.h>
Simon Glass4ed59e72011-09-21 12:40:04 +000030#include <div64.h>
31
32/*
33 * This is our record of the current clock rate of each clock. We don't
34 * fill all of these in since we are only really interested in clocks which
35 * we use as parents.
36 */
37static unsigned pll_rate[CLOCK_ID_COUNT];
38
39/*
40 * The oscillator frequency is fixed to one of four set values. Based on this
41 * the other clocks are set up appropriately.
42 */
43static unsigned osc_freq[CLOCK_OSC_FREQ_COUNT] = {
44 13000000,
45 19200000,
46 12000000,
47 26000000,
48};
49
50/*
51 * Clock types that we can use as a source. The Tegra2 has muxes for the
52 * peripheral clocks, and in most cases there are four options for the clock
53 * source. This gives us a clock 'type' and exploits what commonality exists
54 * in the device.
55 *
56 * Letters are obvious, except for T which means CLK_M, and S which means the
57 * clock derived from 32KHz. Beware that CLK_M (also called OSC in the
58 * datasheet) and PLL_M are different things. The former is the basic
59 * clock supplied to the SOC from an external oscillator. The latter is the
60 * memory clock PLL.
61 *
62 * See definitions in clock_id in the header file.
63 */
64enum clock_type_id {
65 CLOCK_TYPE_AXPT, /* PLL_A, PLL_X, PLL_P, CLK_M */
66 CLOCK_TYPE_MCPA, /* and so on */
67 CLOCK_TYPE_MCPT,
68 CLOCK_TYPE_PCM,
69 CLOCK_TYPE_PCMT,
70 CLOCK_TYPE_PCXTS,
71 CLOCK_TYPE_PDCT,
72
73 CLOCK_TYPE_COUNT,
74 CLOCK_TYPE_NONE = -1, /* invalid clock type */
75};
76
77/* return 1 if a peripheral ID is in range */
78#define clock_type_id_isvalid(id) ((id) >= 0 && \
79 (id) < CLOCK_TYPE_COUNT)
80
81char pllp_valid = 1; /* PLLP is set up correctly */
82
83enum {
84 CLOCK_MAX_MUX = 4 /* number of source options for each clock */
85};
86
87/*
88 * Clock source mux for each clock type. This just converts our enum into
89 * a list of mux sources for use by the code. Note that CLOCK_TYPE_PCXTS
90 * is special as it has 5 sources. Since it also has a different number of
91 * bits in its register for the source, we just handle it with a special
92 * case in the code.
93 */
94#define CLK(x) CLOCK_ID_ ## x
95static enum clock_id clock_source[CLOCK_TYPE_COUNT][CLOCK_MAX_MUX] = {
96 { CLK(AUDIO), CLK(XCPU), CLK(PERIPH), CLK(OSC) },
97 { CLK(MEMORY), CLK(CGENERAL), CLK(PERIPH), CLK(AUDIO) },
98 { CLK(MEMORY), CLK(CGENERAL), CLK(PERIPH), CLK(OSC) },
99 { CLK(PERIPH), CLK(CGENERAL), CLK(MEMORY), CLK(NONE) },
100 { CLK(PERIPH), CLK(CGENERAL), CLK(MEMORY), CLK(OSC) },
101 { CLK(PERIPH), CLK(CGENERAL), CLK(XCPU), CLK(OSC) },
102 { CLK(PERIPH), CLK(DISPLAY), CLK(CGENERAL), CLK(OSC) },
103};
104
105/*
106 * Clock peripheral IDs which sadly don't match up with PERIPH_ID. This is
107 * not in the header file since it is for purely internal use - we want
108 * callers to use the PERIPH_ID for all access to peripheral clocks to avoid
109 * confusion bewteen PERIPH_ID_... and PERIPHC_...
110 *
111 * We don't call this CLOCK_PERIPH_ID or PERIPH_CLOCK_ID as it would just be
112 * confusing.
113 *
114 * Note to SOC vendors: perhaps define a unified numbering for peripherals and
115 * use it for reset, clock enable, clock source/divider and even pinmuxing
116 * if you can.
117 */
118enum periphc_internal_id {
119 /* 0x00 */
120 PERIPHC_I2S1,
121 PERIPHC_I2S2,
122 PERIPHC_SPDIF_OUT,
123 PERIPHC_SPDIF_IN,
124 PERIPHC_PWM,
125 PERIPHC_SPI1,
126 PERIPHC_SPI2,
127 PERIPHC_SPI3,
128
129 /* 0x08 */
130 PERIPHC_XIO,
131 PERIPHC_I2C1,
132 PERIPHC_DVC_I2C,
133 PERIPHC_TWC,
134 PERIPHC_0c,
135 PERIPHC_10, /* PERIPHC_SPI1, what is this really? */
136 PERIPHC_DISP1,
137 PERIPHC_DISP2,
138
139 /* 0x10 */
140 PERIPHC_CVE,
141 PERIPHC_IDE0,
142 PERIPHC_VI,
143 PERIPHC_1c,
144 PERIPHC_SDMMC1,
145 PERIPHC_SDMMC2,
146 PERIPHC_G3D,
147 PERIPHC_G2D,
148
149 /* 0x18 */
150 PERIPHC_NDFLASH,
151 PERIPHC_SDMMC4,
152 PERIPHC_VFIR,
153 PERIPHC_EPP,
154 PERIPHC_MPE,
155 PERIPHC_MIPI,
156 PERIPHC_UART1,
157 PERIPHC_UART2,
158
159 /* 0x20 */
160 PERIPHC_HOST1X,
161 PERIPHC_21,
162 PERIPHC_TVO,
163 PERIPHC_HDMI,
164 PERIPHC_24,
165 PERIPHC_TVDAC,
166 PERIPHC_I2C2,
167 PERIPHC_EMC,
168
169 /* 0x28 */
170 PERIPHC_UART3,
171 PERIPHC_29,
172 PERIPHC_VI_SENSOR,
173 PERIPHC_2b,
174 PERIPHC_2c,
175 PERIPHC_SPI4,
176 PERIPHC_I2C3,
177 PERIPHC_SDMMC3,
178
179 /* 0x30 */
180 PERIPHC_UART4,
181 PERIPHC_UART5,
182 PERIPHC_VDE,
183 PERIPHC_OWR,
184 PERIPHC_NOR,
185 PERIPHC_CSITE,
186
187 PERIPHC_COUNT,
188
189 PERIPHC_NONE = -1,
190};
191
192/* return 1 if a periphc_internal_id is in range */
193#define periphc_internal_id_isvalid(id) ((id) >= 0 && \
194 (id) < PERIPHC_COUNT)
195
196/*
197 * Clock type for each peripheral clock source. We put the name in each
198 * record just so it is easy to match things up
199 */
200#define TYPE(name, type) type
201static enum clock_type_id clock_periph_type[PERIPHC_COUNT] = {
202 /* 0x00 */
203 TYPE(PERIPHC_I2S1, CLOCK_TYPE_AXPT),
204 TYPE(PERIPHC_I2S2, CLOCK_TYPE_AXPT),
205 TYPE(PERIPHC_SPDIF_OUT, CLOCK_TYPE_AXPT),
206 TYPE(PERIPHC_SPDIF_IN, CLOCK_TYPE_PCM),
207 TYPE(PERIPHC_PWM, CLOCK_TYPE_PCXTS),
208 TYPE(PERIPHC_SPI1, CLOCK_TYPE_PCMT),
209 TYPE(PERIPHC_SPI22, CLOCK_TYPE_PCMT),
210 TYPE(PERIPHC_SPI3, CLOCK_TYPE_PCMT),
211
212 /* 0x08 */
213 TYPE(PERIPHC_XIO, CLOCK_TYPE_PCMT),
214 TYPE(PERIPHC_I2C1, CLOCK_TYPE_PCMT),
215 TYPE(PERIPHC_DVC_I2C, CLOCK_TYPE_PCMT),
216 TYPE(PERIPHC_TWC, CLOCK_TYPE_PCMT),
217 TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
218 TYPE(PERIPHC_SPI1, CLOCK_TYPE_PCMT),
219 TYPE(PERIPHC_DISP1, CLOCK_TYPE_PDCT),
220 TYPE(PERIPHC_DISP2, CLOCK_TYPE_PDCT),
221
222 /* 0x10 */
223 TYPE(PERIPHC_CVE, CLOCK_TYPE_PDCT),
224 TYPE(PERIPHC_IDE0, CLOCK_TYPE_PCMT),
225 TYPE(PERIPHC_VI, CLOCK_TYPE_MCPA),
226 TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
227 TYPE(PERIPHC_SDMMC1, CLOCK_TYPE_PCMT),
228 TYPE(PERIPHC_SDMMC2, CLOCK_TYPE_PCMT),
229 TYPE(PERIPHC_G3D, CLOCK_TYPE_MCPA),
230 TYPE(PERIPHC_G2D, CLOCK_TYPE_MCPA),
231
232 /* 0x18 */
233 TYPE(PERIPHC_NDFLASH, CLOCK_TYPE_PCMT),
234 TYPE(PERIPHC_SDMMC4, CLOCK_TYPE_PCMT),
235 TYPE(PERIPHC_VFIR, CLOCK_TYPE_PCMT),
236 TYPE(PERIPHC_EPP, CLOCK_TYPE_MCPA),
237 TYPE(PERIPHC_MPE, CLOCK_TYPE_MCPA),
238 TYPE(PERIPHC_MIPI, CLOCK_TYPE_PCMT),
239 TYPE(PERIPHC_UART1, CLOCK_TYPE_PCMT),
240 TYPE(PERIPHC_UART2, CLOCK_TYPE_PCMT),
241
242 /* 0x20 */
243 TYPE(PERIPHC_HOST1X, CLOCK_TYPE_MCPA),
244 TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
245 TYPE(PERIPHC_TVO, CLOCK_TYPE_PDCT),
246 TYPE(PERIPHC_HDMI, CLOCK_TYPE_PDCT),
247 TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
248 TYPE(PERIPHC_TVDAC, CLOCK_TYPE_PDCT),
249 TYPE(PERIPHC_I2C2, CLOCK_TYPE_PCMT),
250 TYPE(PERIPHC_EMC, CLOCK_TYPE_MCPT),
251
252 /* 0x28 */
253 TYPE(PERIPHC_UART3, CLOCK_TYPE_PCMT),
254 TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
255 TYPE(PERIPHC_VI, CLOCK_TYPE_MCPA),
256 TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
257 TYPE(PERIPHC_NONE, CLOCK_TYPE_NONE),
258 TYPE(PERIPHC_SPI4, CLOCK_TYPE_PCMT),
259 TYPE(PERIPHC_I2C3, CLOCK_TYPE_PCMT),
260 TYPE(PERIPHC_SDMMC3, CLOCK_TYPE_PCMT),
261
262 /* 0x30 */
263 TYPE(PERIPHC_UART4, CLOCK_TYPE_PCMT),
264 TYPE(PERIPHC_UART5, CLOCK_TYPE_PCMT),
265 TYPE(PERIPHC_VDE, CLOCK_TYPE_PCMT),
266 TYPE(PERIPHC_OWR, CLOCK_TYPE_PCMT),
267 TYPE(PERIPHC_NOR, CLOCK_TYPE_PCMT),
268 TYPE(PERIPHC_CSITE, CLOCK_TYPE_PCMT),
269};
270
271/*
272 * This array translates a periph_id to a periphc_internal_id
273 *
274 * Not present/matched up:
275 * uint vi_sensor; _VI_SENSOR_0, 0x1A8
276 * SPDIF - which is both 0x08 and 0x0c
277 *
278 */
279#define NONE(name) (-1)
280#define OFFSET(name, value) PERIPHC_ ## name
281static s8 periph_id_to_internal_id[PERIPH_ID_COUNT] = {
282 /* Low word: 31:0 */
283 NONE(CPU),
284 NONE(RESERVED1),
285 NONE(RESERVED2),
286 NONE(AC97),
287 NONE(RTC),
288 NONE(TMR),
289 PERIPHC_UART1,
290 PERIPHC_UART2, /* and vfir 0x68 */
291
292 /* 0x08 */
293 NONE(GPIO),
294 PERIPHC_SDMMC2,
295 NONE(SPDIF), /* 0x08 and 0x0c, unclear which to use */
296 PERIPHC_I2S1,
297 PERIPHC_I2C1,
298 PERIPHC_NDFLASH,
299 PERIPHC_SDMMC1,
300 PERIPHC_SDMMC4,
301
302 /* 0x10 */
303 PERIPHC_TWC,
304 PERIPHC_PWM,
305 PERIPHC_I2S2,
306 PERIPHC_EPP,
307 PERIPHC_VI,
308 PERIPHC_G2D,
309 NONE(USBD),
310 NONE(ISP),
311
312 /* 0x18 */
313 PERIPHC_G3D,
314 PERIPHC_IDE0,
315 PERIPHC_DISP2,
316 PERIPHC_DISP1,
317 PERIPHC_HOST1X,
318 NONE(VCP),
319 NONE(RESERVED30),
320 NONE(CACHE2),
321
322 /* Middle word: 63:32 */
323 NONE(MEM),
324 NONE(AHBDMA),
325 NONE(APBDMA),
326 NONE(RESERVED35),
327 NONE(KBC),
328 NONE(STAT_MON),
329 NONE(PMC),
330 NONE(FUSE),
331
332 /* 0x28 */
333 NONE(KFUSE),
334 NONE(SBC1), /* SBC1, 0x34, is this SPI1? */
335 PERIPHC_NOR,
336 PERIPHC_SPI1,
337 PERIPHC_SPI2,
338 PERIPHC_XIO,
339 PERIPHC_SPI3,
340 PERIPHC_DVC_I2C,
341
342 /* 0x30 */
343 NONE(DSI),
344 PERIPHC_TVO, /* also CVE 0x40 */
345 PERIPHC_MIPI,
346 PERIPHC_HDMI,
347 PERIPHC_CSITE,
348 PERIPHC_TVDAC,
349 PERIPHC_I2C2,
350 PERIPHC_UART3,
351
352 /* 0x38 */
353 NONE(RESERVED56),
354 PERIPHC_EMC,
355 NONE(USB2),
356 NONE(USB3),
357 PERIPHC_MPE,
358 PERIPHC_VDE,
359 NONE(BSEA),
360 NONE(BSEV),
361
362 /* Upper word 95:64 */
363 NONE(SPEEDO),
364 PERIPHC_UART4,
365 PERIPHC_UART5,
366 PERIPHC_I2C3,
367 PERIPHC_SPI4,
368 PERIPHC_SDMMC3,
369 NONE(PCIE),
370 PERIPHC_OWR,
371
372 /* 0x48 */
373 NONE(AFI),
374 NONE(CORESIGHT),
375 NONE(RESERVED74),
376 NONE(AVPUCQ),
377 NONE(RESERVED76),
378 NONE(RESERVED77),
379 NONE(RESERVED78),
380 NONE(RESERVED79),
381
382 /* 0x50 */
383 NONE(RESERVED80),
384 NONE(RESERVED81),
385 NONE(RESERVED82),
386 NONE(RESERVED83),
387 NONE(IRAMA),
388 NONE(IRAMB),
389 NONE(IRAMC),
390 NONE(IRAMD),
391
392 /* 0x58 */
393 NONE(CRAM2),
394};
Simon Glassb4ba2be2011-08-30 06:23:13 +0000395
Simon Glassb4ba2be2011-08-30 06:23:13 +0000396/*
397 * Get the oscillator frequency, from the corresponding hardware configuration
398 * field.
399 */
400enum clock_osc_freq clock_get_osc_freq(void)
401{
402 struct clk_rst_ctlr *clkrst =
403 (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
404 u32 reg;
405
406 reg = readl(&clkrst->crc_osc_ctrl);
407 return (reg & OSC_FREQ_MASK) >> OSC_FREQ_SHIFT;
408}
409
Simon Glass4ed59e72011-09-21 12:40:04 +0000410/* Returns a pointer to the registers of the given pll */
411static struct clk_pll *get_pll(enum clock_id clkid)
Simon Glassb4ba2be2011-08-30 06:23:13 +0000412{
413 struct clk_rst_ctlr *clkrst =
414 (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
Simon Glassb4ba2be2011-08-30 06:23:13 +0000415
Simon Glass03c609f2011-09-21 12:40:02 +0000416 assert(clock_id_isvalid(clkid));
Simon Glass4ed59e72011-09-21 12:40:04 +0000417 return &clkrst->crc_pll[clkid];
418}
419
420unsigned long clock_start_pll(enum clock_id clkid, u32 divm, u32 divn,
421 u32 divp, u32 cpcon, u32 lfcon)
422{
423 struct clk_pll *pll = get_pll(clkid);
424 u32 data;
Simon Glassb4ba2be2011-08-30 06:23:13 +0000425
426 /*
427 * We cheat by treating all PLL (except PLLU) in the same fashion.
428 * This works only because:
429 * - same fields are always mapped at same offsets, except DCCON
430 * - DCCON is always 0, doesn't conflict
431 * - M,N, P of PLLP values are ignored for PLLP
432 */
433 data = (cpcon << PLL_CPCON_SHIFT) | (lfcon << PLL_LFCON_SHIFT);
434 writel(data, &pll->pll_misc);
435
436 data = (divm << PLL_DIVM_SHIFT) | (divn << PLL_DIVN_SHIFT) |
437 (0 << PLL_BYPASS_SHIFT) | (1 << PLL_ENABLE_SHIFT);
438
Simon Glass03c609f2011-09-21 12:40:02 +0000439 if (clkid == CLOCK_ID_USB)
Simon Glassb4ba2be2011-08-30 06:23:13 +0000440 data |= divp << PLLU_VCO_FREQ_SHIFT;
441 else
442 data |= divp << PLL_DIVP_SHIFT;
443 writel(data, &pll->pll_base);
444
445 /* calculate the stable time */
446 return timer_get_us() + CLOCK_PLL_STABLE_DELAY_US;
447}
448
Simon Glass4ed59e72011-09-21 12:40:04 +0000449/* return 1 if a peripheral ID is in range and valid */
450static int clock_periph_id_isvalid(enum periph_id id)
451{
452 if (id < PERIPH_ID_FIRST || id >= PERIPH_ID_COUNT)
453 printf("Peripheral id %d out of range\n", id);
454 else {
455 switch (id) {
456 case PERIPH_ID_RESERVED1:
457 case PERIPH_ID_RESERVED2:
458 case PERIPH_ID_RESERVED30:
459 case PERIPH_ID_RESERVED35:
460 case PERIPH_ID_RESERVED56:
461 case PERIPH_ID_RESERVED74:
462 case PERIPH_ID_RESERVED76:
463 case PERIPH_ID_RESERVED77:
464 case PERIPH_ID_RESERVED78:
465 case PERIPH_ID_RESERVED79:
466 case PERIPH_ID_RESERVED80:
467 case PERIPH_ID_RESERVED81:
468 case PERIPH_ID_RESERVED82:
469 case PERIPH_ID_RESERVED83:
470 printf("Peripheral id %d is reserved\n", id);
471 break;
472 default:
473 return 1;
474 }
475 }
476 return 0;
477}
478
479/* Returns a pointer to the clock source register for a peripheral */
480static u32 *get_periph_source_reg(enum periph_id periph_id)
481{
482 struct clk_rst_ctlr *clkrst =
483 (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
484 enum periphc_internal_id internal_id;
485
486 assert(clock_periph_id_isvalid(periph_id));
487 internal_id = periph_id_to_internal_id[periph_id];
488 assert(internal_id != -1);
489 return &clkrst->crc_clk_src[internal_id];
490}
491
492void clock_ll_set_source_divisor(enum periph_id periph_id, unsigned source,
493 unsigned divisor)
494{
495 u32 *reg = get_periph_source_reg(periph_id);
496 u32 value;
497
498 value = readl(reg);
499
500 value &= ~OUT_CLK_SOURCE_MASK;
501 value |= source << OUT_CLK_SOURCE_SHIFT;
502
503 value &= ~OUT_CLK_DIVISOR_MASK;
504 value |= divisor << OUT_CLK_DIVISOR_SHIFT;
505
506 writel(value, reg);
507}
508
509void clock_ll_set_source(enum periph_id periph_id, unsigned source)
510{
511 u32 *reg = get_periph_source_reg(periph_id);
512
513 clrsetbits_le32(reg, OUT_CLK_SOURCE_MASK,
514 source << OUT_CLK_SOURCE_SHIFT);
515}
516
517/**
518 * Given the parent's rate and the required rate for the children, this works
519 * out the peripheral clock divider to use, in 7.1 binary format.
520 *
521 * @param parent_rate clock rate of parent clock in Hz
522 * @param rate required clock rate for this clock
523 * @return divider which should be used
524 */
525static int clk_div7_1_get_divider(unsigned long parent_rate,
526 unsigned long rate)
527{
528 u64 divider = parent_rate * 2;
529
530 divider += rate - 1;
531 do_div(divider, rate);
532
533 if ((s64)divider - 2 < 0)
534 return 0;
535
536 if ((s64)divider - 2 > 255)
537 return -1;
538
539 return divider - 2;
540}
541
542/**
543 * Given the parent's rate and the divider in 7.1 format, this works out the
544 * resulting peripheral clock rate.
545 *
546 * @param parent_rate clock rate of parent clock in Hz
547 * @param divider which should be used in 7.1 format
548 * @return effective clock rate of peripheral
549 */
550static unsigned long get_rate_from_divider(unsigned long parent_rate,
551 int divider)
552{
553 u64 rate;
554
555 rate = (u64)parent_rate * 2;
556 do_div(rate, divider + 2);
557 return rate;
558}
559
560unsigned long clock_get_periph_rate(enum periph_id periph_id,
561 enum clock_id parent)
562{
563 u32 *reg = get_periph_source_reg(periph_id);
564
565 return get_rate_from_divider(pll_rate[parent],
566 (readl(reg) & OUT_CLK_DIVISOR_MASK) >> OUT_CLK_DIVISOR_SHIFT);
567}
568
569/**
570 * Find the best available 7.1 format divisor given a parent clock rate and
571 * required child clock rate. This function assumes that a second-stage
572 * divisor is available which can divide by powers of 2 from 1 to 256.
573 *
574 * @param parent_rate clock rate of parent clock in Hz
575 * @param rate required clock rate for this clock
576 * @param extra_div value for the second-stage divisor (not set if this
577 * function returns -1.
578 * @return divider which should be used, or -1 if nothing is valid
579 *
580 */
581static int find_best_divider(unsigned long parent_rate, unsigned long rate,
582 int *extra_div)
583{
584 int shift;
585 int best_divider = -1;
586 int best_error = rate;
587
588 /* try dividers from 1 to 256 and find closest match */
589 for (shift = 0; shift <= 8 && best_error > 0; shift++) {
590 unsigned divided_parent = parent_rate >> shift;
591 int divider = clk_div7_1_get_divider(divided_parent, rate);
592 unsigned effective_rate = get_rate_from_divider(divided_parent,
593 divider);
594 int error = rate - effective_rate;
595
596 /* Given a valid divider, look for the lowest error */
597 if (divider != -1 && error < best_error) {
598 best_error = error;
599 *extra_div = 1 << shift;
600 best_divider = divider;
601 }
602 }
603
604 /* return what we found - *extra_div will already be set */
605 return best_divider;
606}
607
608/**
609 * Given a peripheral ID and the required source clock, this returns which
610 * value should be programmed into the source mux for that peripheral.
611 *
612 * There is special code here to handle the one source type with 5 sources.
613 *
614 * @param periph_id peripheral to start
615 * @param source PLL id of required parent clock
616 * @param mux_bits Set to number of bits in mux register: 2 or 4
617 * @return mux value (0-4, or -1 if not found)
618 */
619static int get_periph_clock_source(enum periph_id periph_id,
620 enum clock_id parent, int *mux_bits)
621{
622 enum clock_type_id type;
623 enum periphc_internal_id internal_id;
624 int mux;
625
626 assert(clock_periph_id_isvalid(periph_id));
627
628 internal_id = periph_id_to_internal_id[periph_id];
629 assert(periphc_internal_id_isvalid(internal_id));
630
631 type = clock_periph_type[internal_id];
632 assert(clock_type_id_isvalid(type));
633
634 /* Special case here for the clock with a 4-bit source mux */
635 if (type == CLOCK_TYPE_PCXTS)
636 *mux_bits = 4;
637 else
638 *mux_bits = 2;
639
640 for (mux = 0; mux < CLOCK_MAX_MUX; mux++)
641 if (clock_source[type][mux] == parent)
642 return mux;
643
644 /*
645 * Not found: it might be looking for the 'S' in CLOCK_TYPE_PCXTS
646 * which is not in our table. If not, then they are asking for a
647 * source which this peripheral can't access through its mux.
648 */
649 assert(type == CLOCK_TYPE_PCXTS);
650 assert(parent == CLOCK_ID_SFROM32KHZ);
651 if (type == CLOCK_TYPE_PCXTS && parent == CLOCK_ID_SFROM32KHZ)
652 return 4; /* mux value for this clock */
653
654 /* if we get here, either us or the caller has made a mistake */
655 printf("Caller requested bad clock: periph=%d, parent=%d\n", periph_id,
656 parent);
657 return -1;
658}
659
660/**
661 * Adjust peripheral PLL to use the given divider and source.
662 *
663 * @param periph_id peripheral to adjust
664 * @param parent Required parent clock (for source mux)
665 * @param divider Required divider in 7.1 format
666 * @return 0 if ok, -1 on error (requesting a parent clock which is not valid
667 * for this peripheral)
668 */
669static int adjust_periph_pll(enum periph_id periph_id,
670 enum clock_id parent, unsigned divider)
671{
672 u32 *reg = get_periph_source_reg(periph_id);
673 unsigned source;
674 int mux_bits;
675
676 clrsetbits_le32(reg, OUT_CLK_DIVISOR_MASK,
677 divider << OUT_CLK_DIVISOR_SHIFT);
678 udelay(1);
679
680 /* work out the source clock and set it */
681 source = get_periph_clock_source(periph_id, parent, &mux_bits);
682 if (source < 0)
683 return -1;
684 if (mux_bits == 4) {
685 clrsetbits_le32(reg, OUT_CLK_SOURCE4_MASK,
686 source << OUT_CLK_SOURCE4_SHIFT);
687 } else {
688 clrsetbits_le32(reg, OUT_CLK_SOURCE_MASK,
689 source << OUT_CLK_SOURCE_SHIFT);
690 }
691 udelay(2);
692 return 0;
693}
694
695unsigned clock_adjust_periph_pll_div(enum periph_id periph_id,
696 enum clock_id parent, unsigned rate, int *extra_div)
697{
698 unsigned effective_rate;
699 int divider;
700
701 if (extra_div)
702 divider = find_best_divider(pll_rate[parent], rate, extra_div);
703 else
704 divider = clk_div7_1_get_divider(pll_rate[parent], rate);
705 assert(divider >= 0);
706 if (adjust_periph_pll(periph_id, parent, divider))
707 return -1U;
708 debug("periph %d, rate=%d, reg=%p = %x\n", periph_id, rate,
709 get_periph_source_reg(periph_id),
710 readl(get_periph_source_reg(periph_id)));
711
712 /* Check what we ended up with. This shouldn't matter though */
713 effective_rate = clock_get_periph_rate(periph_id, parent);
714 if (extra_div)
715 effective_rate /= *extra_div;
716 if (rate != effective_rate)
717 debug("Requested clock rate %u not honored (got %u)\n",
718 rate, effective_rate);
719 return effective_rate;
720}
721
722unsigned clock_start_periph_pll(enum periph_id periph_id,
723 enum clock_id parent, unsigned rate)
724{
725 unsigned effective_rate;
726
727 reset_set_enable(periph_id, 1);
728 clock_enable(periph_id);
729
730 effective_rate = clock_adjust_periph_pll_div(periph_id, parent, rate,
731 NULL);
732
733 reset_set_enable(periph_id, 0);
734 return effective_rate;
735}
736
Simon Glassb4ba2be2011-08-30 06:23:13 +0000737void clock_set_enable(enum periph_id periph_id, int enable)
738{
739 struct clk_rst_ctlr *clkrst =
740 (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
741 u32 *clk = &clkrst->crc_clk_out_enb[PERIPH_REG(periph_id)];
742 u32 reg;
743
744 /* Enable/disable the clock to this peripheral */
745 assert(clock_periph_id_isvalid(periph_id));
746 reg = readl(clk);
747 if (enable)
748 reg |= PERIPH_MASK(periph_id);
749 else
750 reg &= ~PERIPH_MASK(periph_id);
751 writel(reg, clk);
752}
753
754void clock_enable(enum periph_id clkid)
755{
756 clock_set_enable(clkid, 1);
757}
758
759void clock_disable(enum periph_id clkid)
760{
761 clock_set_enable(clkid, 0);
762}
763
764void reset_set_enable(enum periph_id periph_id, int enable)
765{
766 struct clk_rst_ctlr *clkrst =
767 (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
768 u32 *reset = &clkrst->crc_rst_dev[PERIPH_REG(periph_id)];
769 u32 reg;
770
771 /* Enable/disable reset to the peripheral */
772 assert(clock_periph_id_isvalid(periph_id));
773 reg = readl(reset);
774 if (enable)
775 reg |= PERIPH_MASK(periph_id);
776 else
777 reg &= ~PERIPH_MASK(periph_id);
778 writel(reg, reset);
779}
780
781void reset_periph(enum periph_id periph_id, int us_delay)
782{
783 /* Put peripheral into reset */
784 reset_set_enable(periph_id, 1);
785 udelay(us_delay);
786
787 /* Remove reset */
788 reset_set_enable(periph_id, 0);
789
790 udelay(us_delay);
791}
792
793void reset_cmplx_set_enable(int cpu, int which, int reset)
794{
795 struct clk_rst_ctlr *clkrst =
796 (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
797 u32 mask;
798
799 /* Form the mask, which depends on the cpu chosen. Tegra2 has 2 */
800 assert(cpu >= 0 && cpu < 2);
801 mask = which << cpu;
802
803 /* either enable or disable those reset for that CPU */
804 if (reset)
805 writel(mask, &clkrst->crc_cpu_cmplx_set);
806 else
807 writel(mask, &clkrst->crc_cpu_cmplx_clr);
808}
Simon Glass4ed59e72011-09-21 12:40:04 +0000809
810unsigned clock_get_rate(enum clock_id clkid)
811{
812 struct clk_pll *pll;
813 u32 base;
814 u32 divm;
815 u64 parent_rate;
816 u64 rate;
817
818 parent_rate = osc_freq[clock_get_osc_freq()];
819 if (clkid == CLOCK_ID_OSC)
820 return parent_rate;
821
822 pll = get_pll(clkid);
823 base = readl(&pll->pll_base);
824
825 /* Oh for bf_unpack()... */
826 rate = parent_rate * ((base & PLL_DIVN_MASK) >> PLL_DIVN_SHIFT);
827 divm = (base & PLL_DIVM_MASK) >> PLL_DIVM_SHIFT;
828 if (clkid == CLOCK_ID_USB)
829 divm <<= (base & PLLU_VCO_FREQ_MASK) >> PLLU_VCO_FREQ_SHIFT;
830 else
831 divm <<= (base & PLL_DIVP_MASK) >> PLL_DIVP_SHIFT;
832 do_div(rate, divm);
833 return rate;
834}
835
836/**
837 * Set the output frequency you want for each PLL clock.
838 * PLL output frequencies are programmed by setting their N, M and P values.
839 * The governing equations are:
840 * VCO = (Fi / m) * n, Fo = VCO / (2^p)
841 * where Fo is the output frequency from the PLL.
842 * Example: Set the output frequency to 216Mhz(Fo) with 12Mhz OSC(Fi)
843 * 216Mhz = ((12Mhz / m) * n) / (2^p) so n=432,m=12,p=1
844 * Please see Tegra TRM section 5.3 to get the detail for PLL Programming
845 *
846 * @param n PLL feedback divider(DIVN)
847 * @param m PLL input divider(DIVN)
848 * @param p post divider(DIVP)
849 * @param cpcon base PLL charge pump(CPCON)
850 * @return 0 if ok, -1 on error (the requested PLL is incorrect and cannot
851 * be overriden), 1 if PLL is already correct
852 */
853static int clock_set_rate(enum clock_id clkid, u32 n, u32 m, u32 p, u32 cpcon)
854{
855 u32 base_reg;
856 u32 misc_reg;
857 struct clk_pll *pll;
858
859 pll = get_pll(clkid);
860
861 base_reg = readl(&pll->pll_base);
862
863 /* Set BYPASS, m, n and p to PLL_BASE */
864 base_reg &= ~PLL_DIVM_MASK;
865 base_reg |= m << PLL_DIVM_SHIFT;
866
867 base_reg &= ~PLL_DIVN_MASK;
868 base_reg |= n << PLL_DIVN_SHIFT;
869
870 base_reg &= ~PLL_DIVP_MASK;
871 base_reg |= p << PLL_DIVP_SHIFT;
872
873 if (clkid == CLOCK_ID_PERIPH) {
874 /*
875 * If the PLL is already set up, check that it is correct
876 * and record this info for clock_verify() to check.
877 */
878 if (base_reg & PLL_BASE_OVRRIDE_MASK) {
879 base_reg |= PLL_ENABLE_MASK;
880 if (base_reg != readl(&pll->pll_base))
881 pllp_valid = 0;
882 return pllp_valid ? 1 : -1;
883 }
884 base_reg |= PLL_BASE_OVRRIDE_MASK;
885 }
886
887 base_reg |= PLL_BYPASS_MASK;
888 writel(base_reg, &pll->pll_base);
889
890 /* Set cpcon to PLL_MISC */
891 misc_reg = readl(&pll->pll_misc);
892 misc_reg &= ~PLL_CPCON_MASK;
893 misc_reg |= cpcon << PLL_CPCON_SHIFT;
894 writel(misc_reg, &pll->pll_misc);
895
896 /* Enable PLL */
897 base_reg |= PLL_ENABLE_MASK;
898 writel(base_reg, &pll->pll_base);
899
900 /* Disable BYPASS */
901 base_reg &= ~PLL_BYPASS_MASK;
902 writel(base_reg, &pll->pll_base);
903
904 return 0;
905}
906
Simon Glass8442fd32011-11-28 15:04:37 +0000907void clock_ll_start_uart(enum periph_id periph_id)
908{
909 /* Assert UART reset and enable clock */
910 reset_set_enable(periph_id, 1);
911 clock_enable(periph_id);
912 clock_ll_set_source(periph_id, 0); /* UARTx_CLK_SRC = 00, PLLP_OUT0 */
913
914 /* wait for 2us */
915 udelay(2);
916
917 /* De-assert reset to UART */
918 reset_set_enable(periph_id, 0);
919}
920
Simon Glass4ed59e72011-09-21 12:40:04 +0000921int clock_verify(void)
922{
923 struct clk_pll *pll = get_pll(CLOCK_ID_PERIPH);
924 u32 reg = readl(&pll->pll_base);
925
926 if (!pllp_valid) {
927 printf("Warning: PLLP %x is not correct\n", reg);
928 return -1;
929 }
930 debug("PLLX %x is correct\n", reg);
931 return 0;
932}
933
934void clock_early_init(void)
935{
936 /*
937 * PLLP output frequency set to 216MHz
938 * PLLC output frequency set to 600Mhz
939 *
940 * TODO: Can we calculate these values instead of hard-coding?
941 */
942 switch (clock_get_osc_freq()) {
943 case CLOCK_OSC_FREQ_12_0: /* OSC is 12Mhz */
944 clock_set_rate(CLOCK_ID_PERIPH, 432, 12, 1, 8);
945 clock_set_rate(CLOCK_ID_CGENERAL, 600, 12, 0, 8);
946 break;
947
948 case CLOCK_OSC_FREQ_26_0: /* OSC is 26Mhz */
949 clock_set_rate(CLOCK_ID_PERIPH, 432, 26, 1, 8);
950 clock_set_rate(CLOCK_ID_CGENERAL, 600, 26, 0, 8);
951 break;
952
953 case CLOCK_OSC_FREQ_13_0:
954 case CLOCK_OSC_FREQ_19_2:
955 default:
956 /*
957 * These are not supported. It is too early to print a
958 * message and the UART likely won't work anyway due to the
959 * oscillator being wrong.
960 */
961 break;
962 }
963}
964
965void clock_init(void)
966{
967 pll_rate[CLOCK_ID_MEMORY] = clock_get_rate(CLOCK_ID_MEMORY);
968 pll_rate[CLOCK_ID_PERIPH] = clock_get_rate(CLOCK_ID_PERIPH);
969 pll_rate[CLOCK_ID_CGENERAL] = clock_get_rate(CLOCK_ID_CGENERAL);
970 pll_rate[CLOCK_ID_OSC] = clock_get_rate(CLOCK_ID_OSC);
971 pll_rate[CLOCK_ID_SFROM32KHZ] = 32768;
972 debug("Osc = %d\n", pll_rate[CLOCK_ID_OSC]);
973 debug("PLLM = %d\n", pll_rate[CLOCK_ID_MEMORY]);
974 debug("PLLP = %d\n", pll_rate[CLOCK_ID_PERIPH]);
975}