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