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