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