Merge remote branch 'common/android-2.6.36' into android-tegra-2.6.36
[firefly-linux-kernel-4.4.55.git] / arch / arm / mach-tegra / tegra2_clocks.c
1 /*
2  * arch/arm/mach-tegra/tegra2_clocks.c
3  *
4  * Copyright (C) 2010 Google, Inc.
5  *
6  * Author:
7  *      Colin Cross <ccross@google.com>
8  *
9  * This software is licensed under the terms of the GNU General Public
10  * License version 2, as published by the Free Software Foundation, and
11  * may be copied, distributed, and modified under those terms.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  */
19
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/list.h>
23 #include <linux/spinlock.h>
24 #include <linux/delay.h>
25 #include <linux/io.h>
26 #include <linux/clk.h>
27
28 #include <asm/clkdev.h>
29
30 #include <mach/iomap.h>
31 #include <mach/suspend.h>
32
33 #include "clock.h"
34 #include "fuse.h"
35 #include "tegra2_emc.h"
36
37 #define RST_DEVICES                     0x004
38 #define RST_DEVICES_SET                 0x300
39 #define RST_DEVICES_CLR                 0x304
40 #define RST_DEVICES_NUM                 3
41
42 #define CLK_OUT_ENB                     0x010
43 #define CLK_OUT_ENB_SET                 0x320
44 #define CLK_OUT_ENB_CLR                 0x324
45 #define CLK_OUT_ENB_NUM                 3
46
47 #define CLK_MASK_ARM                    0x44
48 #define MISC_CLK_ENB                    0x48
49
50 #define OSC_CTRL                        0x50
51 #define OSC_CTRL_OSC_FREQ_MASK          (3<<30)
52 #define OSC_CTRL_OSC_FREQ_13MHZ         (0<<30)
53 #define OSC_CTRL_OSC_FREQ_19_2MHZ       (1<<30)
54 #define OSC_CTRL_OSC_FREQ_12MHZ         (2<<30)
55 #define OSC_CTRL_OSC_FREQ_26MHZ         (3<<30)
56 #define OSC_CTRL_MASK                   (0x3f2 | OSC_CTRL_OSC_FREQ_MASK)
57
58 #define OSC_FREQ_DET                    0x58
59 #define OSC_FREQ_DET_TRIG               (1<<31)
60
61 #define OSC_FREQ_DET_STATUS             0x5C
62 #define OSC_FREQ_DET_BUSY               (1<<31)
63 #define OSC_FREQ_DET_CNT_MASK           0xFFFF
64
65 #define PERIPH_CLK_SOURCE_I2S1          0x100
66 #define PERIPH_CLK_SOURCE_EMC           0x19c
67 #define PERIPH_CLK_SOURCE_OSC           0x1fc
68 #define PERIPH_CLK_SOURCE_NUM \
69         ((PERIPH_CLK_SOURCE_OSC - PERIPH_CLK_SOURCE_I2S1) / 4)
70
71 #define PERIPH_CLK_SOURCE_MASK          (3<<30)
72 #define PERIPH_CLK_SOURCE_SHIFT         30
73 #define PERIPH_CLK_SOURCE_ENABLE        (1<<28)
74 #define PERIPH_CLK_SOURCE_DIVU71_MASK   0xFF
75 #define PERIPH_CLK_SOURCE_DIVU16_MASK   0xFFFF
76 #define PERIPH_CLK_SOURCE_DIV_SHIFT     0
77
78 #define SDMMC_CLK_INT_FB_SEL            (1 << 23)
79 #define SDMMC_CLK_INT_FB_DLY_SHIFT      16
80 #define SDMMC_CLK_INT_FB_DLY_MASK       (0xF << SDMMC_CLK_INT_FB_DLY_SHIFT)
81
82 #define PLL_BASE                        0x0
83 #define PLL_BASE_BYPASS                 (1<<31)
84 #define PLL_BASE_ENABLE                 (1<<30)
85 #define PLL_BASE_REF_ENABLE             (1<<29)
86 #define PLL_BASE_OVERRIDE               (1<<28)
87 #define PLL_BASE_DIVP_MASK              (0x7<<20)
88 #define PLL_BASE_DIVP_SHIFT             20
89 #define PLL_BASE_DIVN_MASK              (0x3FF<<8)
90 #define PLL_BASE_DIVN_SHIFT             8
91 #define PLL_BASE_DIVM_MASK              (0x1F)
92 #define PLL_BASE_DIVM_SHIFT             0
93
94 #define PLL_OUT_RATIO_MASK              (0xFF<<8)
95 #define PLL_OUT_RATIO_SHIFT             8
96 #define PLL_OUT_OVERRIDE                (1<<2)
97 #define PLL_OUT_CLKEN                   (1<<1)
98 #define PLL_OUT_RESET_DISABLE           (1<<0)
99
100 #define PLL_MISC(c)                     (((c)->flags & PLL_ALT_MISC_REG) ? 0x4 : 0xc)
101
102 #define PLL_MISC_DCCON_SHIFT            20
103 #define PLL_MISC_CPCON_SHIFT            8
104 #define PLL_MISC_CPCON_MASK             (0xF<<PLL_MISC_CPCON_SHIFT)
105 #define PLL_MISC_LFCON_SHIFT            4
106 #define PLL_MISC_LFCON_MASK             (0xF<<PLL_MISC_LFCON_SHIFT)
107 #define PLL_MISC_VCOCON_SHIFT           0
108 #define PLL_MISC_VCOCON_MASK            (0xF<<PLL_MISC_VCOCON_SHIFT)
109
110 #define PLLU_BASE_POST_DIV              (1<<20)
111
112 #define PLLD_MISC_CLKENABLE             (1<<30)
113 #define PLLD_MISC_DIV_RST               (1<<23)
114 #define PLLD_MISC_DCCON_SHIFT           12
115
116 #define PERIPH_CLK_TO_ENB_REG(c)        ((c->u.periph.clk_num / 32) * 4)
117 #define PERIPH_CLK_TO_ENB_SET_REG(c)    ((c->u.periph.clk_num / 32) * 8)
118 #define PERIPH_CLK_TO_ENB_BIT(c)        (1 << (c->u.periph.clk_num % 32))
119
120 #define SUPER_CLK_MUX                   0x00
121 #define SUPER_STATE_SHIFT               28
122 #define SUPER_STATE_MASK                (0xF << SUPER_STATE_SHIFT)
123 #define SUPER_STATE_STANDBY             (0x0 << SUPER_STATE_SHIFT)
124 #define SUPER_STATE_IDLE                (0x1 << SUPER_STATE_SHIFT)
125 #define SUPER_STATE_RUN                 (0x2 << SUPER_STATE_SHIFT)
126 #define SUPER_STATE_IRQ                 (0x3 << SUPER_STATE_SHIFT)
127 #define SUPER_STATE_FIQ                 (0x4 << SUPER_STATE_SHIFT)
128 #define SUPER_SOURCE_MASK               0xF
129 #define SUPER_FIQ_SOURCE_SHIFT          12
130 #define SUPER_IRQ_SOURCE_SHIFT          8
131 #define SUPER_RUN_SOURCE_SHIFT          4
132 #define SUPER_IDLE_SOURCE_SHIFT         0
133
134 #define SUPER_CLK_DIVIDER               0x04
135
136 #define BUS_CLK_DISABLE                 (1<<3)
137 #define BUS_CLK_DIV_MASK                0x3
138
139 #define PMC_CTRL                        0x0
140  #define PMC_CTRL_BLINK_ENB             (1 << 7)
141
142 #define PMC_DPD_PADS_ORIDE              0x1c
143  #define PMC_DPD_PADS_ORIDE_BLINK_ENB   (1 << 20)
144
145 #define PMC_BLINK_TIMER_DATA_ON_SHIFT   0
146 #define PMC_BLINK_TIMER_DATA_ON_MASK    0x7fff
147 #define PMC_BLINK_TIMER_ENB             (1 << 15)
148 #define PMC_BLINK_TIMER_DATA_OFF_SHIFT  16
149 #define PMC_BLINK_TIMER_DATA_OFF_MASK   0xffff
150
151 static void __iomem *reg_clk_base = IO_ADDRESS(TEGRA_CLK_RESET_BASE);
152 static void __iomem *reg_pmc_base = IO_ADDRESS(TEGRA_PMC_BASE);
153
154 /*
155  * Some peripheral clocks share an enable bit, so refcount the enable bits
156  * in registers CLK_ENABLE_L, CLK_ENABLE_H, and CLK_ENABLE_U
157  */
158 static int tegra_periph_clk_enable_refcount[3 * 32];
159
160 #define clk_writel(value, reg) \
161         __raw_writel(value, (u32)reg_clk_base + (reg))
162 #define clk_readl(reg) \
163         __raw_readl((u32)reg_clk_base + (reg))
164 #define pmc_writel(value, reg) \
165         __raw_writel(value, (u32)reg_pmc_base + (reg))
166 #define pmc_readl(reg) \
167         __raw_readl((u32)reg_pmc_base + (reg))
168
169 unsigned long clk_measure_input_freq(void)
170 {
171         u32 clock_autodetect;
172         clk_writel(OSC_FREQ_DET_TRIG | 1, OSC_FREQ_DET);
173         do {} while (clk_readl(OSC_FREQ_DET_STATUS) & OSC_FREQ_DET_BUSY);
174         clock_autodetect = clk_readl(OSC_FREQ_DET_STATUS);
175         if (clock_autodetect >= 732 - 3 && clock_autodetect <= 732 + 3) {
176                 return 12000000;
177         } else if (clock_autodetect >= 794 - 3 && clock_autodetect <= 794 + 3) {
178                 return 13000000;
179         } else if (clock_autodetect >= 1172 - 3 && clock_autodetect <= 1172 + 3) {
180                 return 19200000;
181         } else if (clock_autodetect >= 1587 - 3 && clock_autodetect <= 1587 + 3) {
182                 return 26000000;
183         } else {
184                 pr_err("%s: Unexpected clock autodetect value %d", __func__, clock_autodetect);
185                 BUG();
186                 return 0;
187         }
188 }
189
190 static int clk_div71_get_divider(unsigned long parent_rate, unsigned long rate)
191 {
192         s64 divider_u71 = parent_rate * 2;
193         divider_u71 += rate - 1;
194         do_div(divider_u71, rate);
195
196         if (divider_u71 - 2 < 0)
197                 return 0;
198
199         if (divider_u71 - 2 > 255)
200                 return -EINVAL;
201
202         return divider_u71 - 2;
203 }
204
205 static int clk_div16_get_divider(unsigned long parent_rate, unsigned long rate)
206 {
207         s64 divider_u16;
208
209         divider_u16 = parent_rate;
210         divider_u16 += rate - 1;
211         do_div(divider_u16, rate);
212
213         if (divider_u16 - 1 < 0)
214                 return 0;
215
216         if (divider_u16 - 1 > 255)
217                 return -EINVAL;
218
219         return divider_u16 - 1;
220 }
221
222 /* clk_m functions */
223 static unsigned long tegra2_clk_m_autodetect_rate(struct clk *c)
224 {
225         u32 auto_clock_control = clk_readl(OSC_CTRL) & ~OSC_CTRL_OSC_FREQ_MASK;
226
227         c->rate = clk_measure_input_freq();
228         switch (c->rate) {
229         case 12000000:
230                 auto_clock_control |= OSC_CTRL_OSC_FREQ_12MHZ;
231                 break;
232         case 13000000:
233                 auto_clock_control |= OSC_CTRL_OSC_FREQ_13MHZ;
234                 break;
235         case 19200000:
236                 auto_clock_control |= OSC_CTRL_OSC_FREQ_19_2MHZ;
237                 break;
238         case 26000000:
239                 auto_clock_control |= OSC_CTRL_OSC_FREQ_26MHZ;
240                 break;
241         default:
242                 pr_err("%s: Unexpected clock rate %ld", __func__, c->rate);
243                 BUG();
244         }
245         clk_writel(auto_clock_control, OSC_CTRL);
246         return c->rate;
247 }
248
249 static void tegra2_clk_m_init(struct clk *c)
250 {
251         pr_debug("%s on clock %s\n", __func__, c->name);
252         tegra2_clk_m_autodetect_rate(c);
253 }
254
255 static int tegra2_clk_m_enable(struct clk *c)
256 {
257         pr_debug("%s on clock %s\n", __func__, c->name);
258         return 0;
259 }
260
261 static void tegra2_clk_m_disable(struct clk *c)
262 {
263         pr_debug("%s on clock %s\n", __func__, c->name);
264         BUG();
265 }
266
267 static struct clk_ops tegra_clk_m_ops = {
268         .init           = tegra2_clk_m_init,
269         .enable         = tegra2_clk_m_enable,
270         .disable        = tegra2_clk_m_disable,
271 };
272
273 void tegra2_periph_reset_assert(struct clk *c)
274 {
275         BUG_ON(!c->ops->reset);
276         c->ops->reset(c, true);
277 }
278
279 void tegra2_periph_reset_deassert(struct clk *c)
280 {
281         BUG_ON(!c->ops->reset);
282         c->ops->reset(c, false);
283 }
284
285 /* super clock functions */
286 /* "super clocks" on tegra have two-stage muxes and a clock skipping
287  * super divider.  We will ignore the clock skipping divider, since we
288  * can't lower the voltage when using the clock skip, but we can if we
289  * lower the PLL frequency.
290  */
291 static void tegra2_super_clk_init(struct clk *c)
292 {
293         u32 val;
294         int source;
295         int shift;
296         const struct clk_mux_sel *sel;
297         val = clk_readl(c->reg + SUPER_CLK_MUX);
298         c->state = ON;
299         BUG_ON(((val & SUPER_STATE_MASK) != SUPER_STATE_RUN) &&
300                 ((val & SUPER_STATE_MASK) != SUPER_STATE_IDLE));
301         shift = ((val & SUPER_STATE_MASK) == SUPER_STATE_IDLE) ?
302                 SUPER_IDLE_SOURCE_SHIFT : SUPER_RUN_SOURCE_SHIFT;
303         source = (val >> shift) & SUPER_SOURCE_MASK;
304         for (sel = c->inputs; sel->input != NULL; sel++) {
305                 if (sel->value == source)
306                         break;
307         }
308         BUG_ON(sel->input == NULL);
309         c->parent = sel->input;
310 }
311
312 static int tegra2_super_clk_enable(struct clk *c)
313 {
314         clk_writel(0, c->reg + SUPER_CLK_DIVIDER);
315         return 0;
316 }
317
318 static void tegra2_super_clk_disable(struct clk *c)
319 {
320         pr_debug("%s on clock %s\n", __func__, c->name);
321
322         /* oops - don't disable the CPU clock! */
323         BUG();
324 }
325
326 static int tegra2_super_clk_set_parent(struct clk *c, struct clk *p)
327 {
328         u32 val;
329         const struct clk_mux_sel *sel;
330         int shift;
331
332         val = clk_readl(c->reg + SUPER_CLK_MUX);;
333         BUG_ON(((val & SUPER_STATE_MASK) != SUPER_STATE_RUN) &&
334                 ((val & SUPER_STATE_MASK) != SUPER_STATE_IDLE));
335         shift = ((val & SUPER_STATE_MASK) == SUPER_STATE_IDLE) ?
336                 SUPER_IDLE_SOURCE_SHIFT : SUPER_RUN_SOURCE_SHIFT;
337         for (sel = c->inputs; sel->input != NULL; sel++) {
338                 if (sel->input == p) {
339                         val &= ~(SUPER_SOURCE_MASK << shift);
340                         val |= sel->value << shift;
341
342                         if (c->refcnt)
343                                 clk_enable(p);
344
345                         clk_writel(val, c->reg);
346
347                         if (c->refcnt && c->parent)
348                                 clk_disable(c->parent);
349
350                         clk_reparent(c, p);
351                         return 0;
352                 }
353         }
354         return -EINVAL;
355 }
356
357 /*
358  * Super clocks have "clock skippers" instead of dividers.  Dividing using
359  * a clock skipper does not allow the voltage to be scaled down, so instead
360  * adjust the rate of the parent clock.  This requires that the parent of a
361  * super clock have no other children, otherwise the rate will change
362  * underneath the other children.
363  */
364 static int tegra2_super_clk_set_rate(struct clk *c, unsigned long rate)
365 {
366         return clk_set_rate(c->parent, rate);
367 }
368
369 static struct clk_ops tegra_super_ops = {
370         .init                   = tegra2_super_clk_init,
371         .enable                 = tegra2_super_clk_enable,
372         .disable                = tegra2_super_clk_disable,
373         .set_parent             = tegra2_super_clk_set_parent,
374         .set_rate               = tegra2_super_clk_set_rate,
375 };
376
377 /* virtual cpu clock functions */
378 /* some clocks can not be stopped (cpu, memory bus) while the SoC is running.
379    To change the frequency of these clocks, the parent pll may need to be
380    reprogrammed, so the clock must be moved off the pll, the pll reprogrammed,
381    and then the clock moved back to the pll.  To hide this sequence, a virtual
382    clock handles it.
383  */
384 static void tegra2_cpu_clk_init(struct clk *c)
385 {
386 }
387
388 static int tegra2_cpu_clk_enable(struct clk *c)
389 {
390         return 0;
391 }
392
393 static void tegra2_cpu_clk_disable(struct clk *c)
394 {
395         pr_debug("%s on clock %s\n", __func__, c->name);
396
397         /* oops - don't disable the CPU clock! */
398         BUG();
399 }
400
401 static int tegra2_cpu_clk_set_rate(struct clk *c, unsigned long rate)
402 {
403         int ret;
404         /*
405          * Take an extra reference to the main pll so it doesn't turn
406          * off when we move the cpu off of it
407          */
408         clk_enable(c->u.cpu.main);
409
410         ret = clk_set_parent(c->parent, c->u.cpu.backup);
411         if (ret) {
412                 pr_err("Failed to switch cpu to clock %s\n", c->u.cpu.backup->name);
413                 goto out;
414         }
415
416         if (rate == clk_get_rate(c->u.cpu.backup))
417                 goto out;
418
419         ret = clk_set_rate(c->u.cpu.main, rate);
420         if (ret) {
421                 pr_err("Failed to change cpu pll to %lu\n", rate);
422                 goto out;
423         }
424
425         ret = clk_set_parent(c->parent, c->u.cpu.main);
426         if (ret) {
427                 pr_err("Failed to switch cpu to clock %s\n", c->u.cpu.main->name);
428                 goto out;
429         }
430
431 out:
432         clk_disable(c->u.cpu.main);
433         return ret;
434 }
435
436 static struct clk_ops tegra_cpu_ops = {
437         .init     = tegra2_cpu_clk_init,
438         .enable   = tegra2_cpu_clk_enable,
439         .disable  = tegra2_cpu_clk_disable,
440         .set_rate = tegra2_cpu_clk_set_rate,
441 };
442
443 /* virtual cop clock functions. Used to acquire the fake 'cop' clock to
444  * reset the COP block (i.e. AVP) */
445 static void tegra2_cop_clk_reset(struct clk *c, bool assert)
446 {
447         unsigned long reg = assert ? RST_DEVICES_SET : RST_DEVICES_CLR;
448
449         pr_debug("%s %s\n", __func__, assert ? "assert" : "deassert");
450         clk_writel(1 << 1, reg);
451 }
452
453 static struct clk_ops tegra_cop_ops = {
454         .reset    = tegra2_cop_clk_reset,
455 };
456
457 /* bus clock functions */
458 static void tegra2_bus_clk_init(struct clk *c)
459 {
460         u32 val = clk_readl(c->reg);
461         c->state = ((val >> c->reg_shift) & BUS_CLK_DISABLE) ? OFF : ON;
462         c->div = ((val >> c->reg_shift) & BUS_CLK_DIV_MASK) + 1;
463         c->mul = 1;
464 }
465
466 static int tegra2_bus_clk_enable(struct clk *c)
467 {
468         u32 val = clk_readl(c->reg);
469         val &= ~(BUS_CLK_DISABLE << c->reg_shift);
470         clk_writel(val, c->reg);
471         return 0;
472 }
473
474 static void tegra2_bus_clk_disable(struct clk *c)
475 {
476         u32 val = clk_readl(c->reg);
477         val |= BUS_CLK_DISABLE << c->reg_shift;
478         clk_writel(val, c->reg);
479 }
480
481 static int tegra2_bus_clk_set_rate(struct clk *c, unsigned long rate)
482 {
483         u32 val = clk_readl(c->reg);
484         unsigned long parent_rate = clk_get_rate(c->parent);
485         int i;
486         for (i = 1; i <= 4; i++) {
487                 if (rate == parent_rate / i) {
488                         val &= ~(BUS_CLK_DIV_MASK << c->reg_shift);
489                         val |= (i - 1) << c->reg_shift;
490                         clk_writel(val, c->reg);
491                         c->div = i;
492                         c->mul = 1;
493                         return 0;
494                 }
495         }
496         return -EINVAL;
497 }
498
499 static struct clk_ops tegra_bus_ops = {
500         .init                   = tegra2_bus_clk_init,
501         .enable                 = tegra2_bus_clk_enable,
502         .disable                = tegra2_bus_clk_disable,
503         .set_rate               = tegra2_bus_clk_set_rate,
504 };
505
506 /* Blink output functions */
507
508 static void tegra2_blink_clk_init(struct clk *c)
509 {
510         u32 val;
511
512         val = pmc_readl(PMC_CTRL);
513         c->state = (val & PMC_CTRL_BLINK_ENB) ? ON : OFF;
514         c->mul = 1;
515         val = pmc_readl(c->reg);
516
517         if (val & PMC_BLINK_TIMER_ENB) {
518                 unsigned int on_off;
519
520                 on_off = (val >> PMC_BLINK_TIMER_DATA_ON_SHIFT) &
521                         PMC_BLINK_TIMER_DATA_ON_MASK;
522                 val >>= PMC_BLINK_TIMER_DATA_OFF_SHIFT;
523                 val &= PMC_BLINK_TIMER_DATA_OFF_MASK;
524                 on_off += val;
525                 /* each tick in the blink timer is 4 32KHz clocks */
526                 c->div = on_off * 4;
527         } else {
528                 c->div = 1;
529         }
530 }
531
532 static int tegra2_blink_clk_enable(struct clk *c)
533 {
534         u32 val;
535
536         val = pmc_readl(PMC_DPD_PADS_ORIDE);
537         pmc_writel(val | PMC_DPD_PADS_ORIDE_BLINK_ENB, PMC_DPD_PADS_ORIDE);
538
539         val = pmc_readl(PMC_CTRL);
540         pmc_writel(val | PMC_CTRL_BLINK_ENB, PMC_CTRL);
541
542         return 0;
543 }
544
545 static void tegra2_blink_clk_disable(struct clk *c)
546 {
547         u32 val;
548
549         val = pmc_readl(PMC_CTRL);
550         pmc_writel(val & ~PMC_CTRL_BLINK_ENB, PMC_CTRL);
551
552         val = pmc_readl(PMC_DPD_PADS_ORIDE);
553         pmc_writel(val & ~PMC_DPD_PADS_ORIDE_BLINK_ENB, PMC_DPD_PADS_ORIDE);
554 }
555
556 static int tegra2_blink_clk_set_rate(struct clk *c, unsigned long rate)
557 {
558         unsigned long parent_rate = clk_get_rate(c->parent);
559         if (rate >= parent_rate) {
560                 c->div = 1;
561                 pmc_writel(0, c->reg);
562         } else {
563                 unsigned int on_off;
564                 u32 val;
565
566                 on_off = DIV_ROUND_UP(parent_rate / 8, rate);
567                 c->div = on_off * 8;
568
569                 val = (on_off & PMC_BLINK_TIMER_DATA_ON_MASK) <<
570                         PMC_BLINK_TIMER_DATA_ON_SHIFT;
571                 on_off &= PMC_BLINK_TIMER_DATA_OFF_MASK;
572                 on_off <<= PMC_BLINK_TIMER_DATA_OFF_SHIFT;
573                 val |= on_off;
574                 val |= PMC_BLINK_TIMER_ENB;
575                 pmc_writel(val, c->reg);
576         }
577
578         return 0;
579 }
580
581 static struct clk_ops tegra_blink_clk_ops = {
582         .init                   = &tegra2_blink_clk_init,
583         .enable                 = &tegra2_blink_clk_enable,
584         .disable                = &tegra2_blink_clk_disable,
585         .set_rate               = &tegra2_blink_clk_set_rate,
586 };
587
588 /* PLL Functions */
589 static int tegra2_pll_clk_wait_for_lock(struct clk *c)
590 {
591         udelay(c->u.pll.lock_delay);
592
593         return 0;
594 }
595
596 static void tegra2_pll_clk_init(struct clk *c)
597 {
598         u32 val = clk_readl(c->reg + PLL_BASE);
599
600         c->state = (val & PLL_BASE_ENABLE) ? ON : OFF;
601
602         if (c->flags & PLL_FIXED && !(val & PLL_BASE_OVERRIDE)) {
603                 pr_warning("Clock %s has unknown fixed frequency\n", c->name);
604                 c->mul = 1;
605                 c->div = 1;
606         } else if (val & PLL_BASE_BYPASS) {
607                 c->mul = 1;
608                 c->div = 1;
609         } else {
610                 c->mul = (val & PLL_BASE_DIVN_MASK) >> PLL_BASE_DIVN_SHIFT;
611                 c->div = (val & PLL_BASE_DIVM_MASK) >> PLL_BASE_DIVM_SHIFT;
612                 if (c->flags & PLLU)
613                         c->div *= (val & PLLU_BASE_POST_DIV) ? 1 : 2;
614                 else
615                         c->div *= (val & PLL_BASE_DIVP_MASK) ? 2 : 1;
616         }
617 }
618
619 static int tegra2_pll_clk_enable(struct clk *c)
620 {
621         u32 val;
622         pr_debug("%s on clock %s\n", __func__, c->name);
623
624         val = clk_readl(c->reg + PLL_BASE);
625         val &= ~PLL_BASE_BYPASS;
626         val |= PLL_BASE_ENABLE;
627         clk_writel(val, c->reg + PLL_BASE);
628
629         tegra2_pll_clk_wait_for_lock(c);
630
631         return 0;
632 }
633
634 static void tegra2_pll_clk_disable(struct clk *c)
635 {
636         u32 val;
637         pr_debug("%s on clock %s\n", __func__, c->name);
638
639         val = clk_readl(c->reg);
640         val &= ~(PLL_BASE_BYPASS | PLL_BASE_ENABLE);
641         clk_writel(val, c->reg);
642 }
643
644 static int tegra2_pll_clk_set_rate(struct clk *c, unsigned long rate)
645 {
646         u32 val;
647         unsigned long input_rate;
648         const struct clk_pll_freq_table *sel;
649
650         pr_debug("%s: %s %lu\n", __func__, c->name, rate);
651
652         input_rate = clk_get_rate(c->parent);
653         for (sel = c->u.pll.freq_table; sel->input_rate != 0; sel++) {
654                 if (sel->input_rate == input_rate && sel->output_rate == rate) {
655                         c->mul = sel->n;
656                         c->div = sel->m * sel->p;
657
658                         val = clk_readl(c->reg + PLL_BASE);
659                         if (c->flags & PLL_FIXED)
660                                 val |= PLL_BASE_OVERRIDE;
661                         val &= ~(PLL_BASE_DIVP_MASK | PLL_BASE_DIVN_MASK |
662                                  PLL_BASE_DIVM_MASK);
663                         val |= (sel->m << PLL_BASE_DIVM_SHIFT) |
664                                 (sel->n << PLL_BASE_DIVN_SHIFT);
665                         BUG_ON(sel->p < 1 || sel->p > 2);
666                         if (c->flags & PLLU) {
667                                 if (sel->p == 1)
668                                         val |= PLLU_BASE_POST_DIV;
669                         } else {
670                                 if (sel->p == 2)
671                                         val |= 1 << PLL_BASE_DIVP_SHIFT;
672                         }
673                         clk_writel(val, c->reg + PLL_BASE);
674
675                         if (c->flags & PLL_HAS_CPCON) {
676                                 val = clk_readl(c->reg + PLL_MISC(c));
677                                 val &= ~PLL_MISC_CPCON_MASK;
678                                 val |= sel->cpcon << PLL_MISC_CPCON_SHIFT;
679                                 clk_writel(val, c->reg + PLL_MISC(c));
680                         }
681
682                         if (c->state == ON)
683                                 tegra2_pll_clk_enable(c);
684
685                         return 0;
686                 }
687         }
688         return -EINVAL;
689 }
690
691 static struct clk_ops tegra_pll_ops = {
692         .init                   = tegra2_pll_clk_init,
693         .enable                 = tegra2_pll_clk_enable,
694         .disable                = tegra2_pll_clk_disable,
695         .set_rate               = tegra2_pll_clk_set_rate,
696 };
697
698 static void tegra2_pllx_clk_init(struct clk *c)
699 {
700         tegra2_pll_clk_init(c);
701
702         if (tegra_sku_id() == 7)
703                 c->max_rate = 750000000;
704 }
705
706 static struct clk_ops tegra_pllx_ops = {
707         .init     = tegra2_pllx_clk_init,
708         .enable   = tegra2_pll_clk_enable,
709         .disable  = tegra2_pll_clk_disable,
710         .set_rate = tegra2_pll_clk_set_rate,
711 };
712
713 /* Clock divider ops */
714 static void tegra2_pll_div_clk_init(struct clk *c)
715 {
716         u32 val = clk_readl(c->reg);
717         u32 divu71;
718         val >>= c->reg_shift;
719         c->state = (val & PLL_OUT_CLKEN) ? ON : OFF;
720         if (!(val & PLL_OUT_RESET_DISABLE))
721                 c->state = OFF;
722
723         if (c->flags & DIV_U71) {
724                 divu71 = (val & PLL_OUT_RATIO_MASK) >> PLL_OUT_RATIO_SHIFT;
725                 c->div = (divu71 + 2);
726                 c->mul = 2;
727         } else if (c->flags & DIV_2) {
728                 c->div = 2;
729                 c->mul = 1;
730         } else {
731                 c->div = 1;
732                 c->mul = 1;
733         }
734 }
735
736 static int tegra2_pll_div_clk_enable(struct clk *c)
737 {
738         u32 val;
739         u32 new_val;
740
741         pr_debug("%s: %s\n", __func__, c->name);
742         if (c->flags & DIV_U71) {
743                 val = clk_readl(c->reg);
744                 new_val = val >> c->reg_shift;
745                 new_val &= 0xFFFF;
746
747                 new_val |= PLL_OUT_CLKEN | PLL_OUT_RESET_DISABLE;
748
749                 val &= ~(0xFFFF << c->reg_shift);
750                 val |= new_val << c->reg_shift;
751                 clk_writel(val, c->reg);
752                 return 0;
753         } else if (c->flags & DIV_2) {
754                 BUG_ON(!(c->flags & PLLD));
755                 val = clk_readl(c->reg);
756                 val &= ~PLLD_MISC_DIV_RST;
757                 clk_writel(val, c->reg);
758                 return 0;
759         }
760         return -EINVAL;
761 }
762
763 static void tegra2_pll_div_clk_disable(struct clk *c)
764 {
765         u32 val;
766         u32 new_val;
767
768         pr_debug("%s: %s\n", __func__, c->name);
769         if (c->flags & DIV_U71) {
770                 val = clk_readl(c->reg);
771                 new_val = val >> c->reg_shift;
772                 new_val &= 0xFFFF;
773
774                 new_val &= ~(PLL_OUT_CLKEN | PLL_OUT_RESET_DISABLE);
775
776                 val &= ~(0xFFFF << c->reg_shift);
777                 val |= new_val << c->reg_shift;
778                 clk_writel(val, c->reg);
779         } else if (c->flags & DIV_2) {
780                 BUG_ON(!(c->flags & PLLD));
781                 val = clk_readl(c->reg);
782                 val |= PLLD_MISC_DIV_RST;
783                 clk_writel(val, c->reg);
784         }
785 }
786
787 static int tegra2_pll_div_clk_set_rate(struct clk *c, unsigned long rate)
788 {
789         u32 val;
790         u32 new_val;
791         int divider_u71;
792         unsigned long parent_rate = clk_get_rate(c->parent);
793
794         pr_debug("%s: %s %lu\n", __func__, c->name, rate);
795         if (c->flags & DIV_U71) {
796                 divider_u71 = clk_div71_get_divider(parent_rate, rate);
797                 if (divider_u71 >= 0) {
798                         val = clk_readl(c->reg);
799                         new_val = val >> c->reg_shift;
800                         new_val &= 0xFFFF;
801                         if (c->flags & DIV_U71_FIXED)
802                                 new_val |= PLL_OUT_OVERRIDE;
803                         new_val &= ~PLL_OUT_RATIO_MASK;
804                         new_val |= divider_u71 << PLL_OUT_RATIO_SHIFT;
805
806                         val &= ~(0xFFFF << c->reg_shift);
807                         val |= new_val << c->reg_shift;
808                         clk_writel(val, c->reg);
809                         c->div = divider_u71 + 2;
810                         c->mul = 2;
811                         return 0;
812                 }
813         } else if (c->flags & DIV_2) {
814                 if (parent_rate == rate * 2)
815                         return 0;
816         }
817         return -EINVAL;
818 }
819
820 static long tegra2_pll_div_clk_round_rate(struct clk *c, unsigned long rate)
821 {
822         int divider;
823         unsigned long parent_rate = clk_get_rate(c->parent);
824         pr_debug("%s: %s %lu\n", __func__, c->name, rate);
825
826         if (c->flags & DIV_U71) {
827                 divider = clk_div71_get_divider(parent_rate, rate);
828                 if (divider < 0)
829                         return divider;
830                 return DIV_ROUND_UP(parent_rate * 2, divider + 2);
831         } else if (c->flags & DIV_2) {
832                 return DIV_ROUND_UP(parent_rate, 2);
833         }
834         return -EINVAL;
835 }
836
837 static struct clk_ops tegra_pll_div_ops = {
838         .init                   = tegra2_pll_div_clk_init,
839         .enable                 = tegra2_pll_div_clk_enable,
840         .disable                = tegra2_pll_div_clk_disable,
841         .set_rate               = tegra2_pll_div_clk_set_rate,
842         .round_rate             = tegra2_pll_div_clk_round_rate,
843 };
844
845 /* Periph clk ops */
846
847 static void tegra2_periph_clk_init(struct clk *c)
848 {
849         u32 val = clk_readl(c->reg);
850         const struct clk_mux_sel *mux = 0;
851         const struct clk_mux_sel *sel;
852         if (c->flags & MUX) {
853                 for (sel = c->inputs; sel->input != NULL; sel++) {
854                         if (val >> PERIPH_CLK_SOURCE_SHIFT == sel->value)
855                                 mux = sel;
856                 }
857                 BUG_ON(!mux);
858
859                 c->parent = mux->input;
860         } else {
861                 c->parent = c->inputs[0].input;
862         }
863
864         if (c->flags & DIV_U71) {
865                 u32 divu71 = val & PERIPH_CLK_SOURCE_DIVU71_MASK;
866                 c->div = divu71 + 2;
867                 c->mul = 2;
868         } else if (c->flags & DIV_U16) {
869                 u32 divu16 = val & PERIPH_CLK_SOURCE_DIVU16_MASK;
870                 c->div = divu16 + 1;
871                 c->mul = 1;
872         } else {
873                 c->div = 1;
874                 c->mul = 1;
875         }
876
877         c->state = ON;
878         if (!(clk_readl(CLK_OUT_ENB + PERIPH_CLK_TO_ENB_REG(c)) &
879                         PERIPH_CLK_TO_ENB_BIT(c)))
880                 c->state = OFF;
881         if (!(c->flags & PERIPH_NO_RESET))
882                 if (clk_readl(RST_DEVICES + PERIPH_CLK_TO_ENB_REG(c)) &
883                                 PERIPH_CLK_TO_ENB_BIT(c))
884                         c->state = OFF;
885 }
886
887 static int tegra2_periph_clk_enable(struct clk *c)
888 {
889         u32 val;
890         pr_debug("%s on clock %s\n", __func__, c->name);
891
892         tegra_periph_clk_enable_refcount[c->u.periph.clk_num]++;
893         if (tegra_periph_clk_enable_refcount[c->u.periph.clk_num] > 1)
894                 return 0;
895
896         clk_writel(PERIPH_CLK_TO_ENB_BIT(c),
897                 CLK_OUT_ENB_SET + PERIPH_CLK_TO_ENB_SET_REG(c));
898         if (!(c->flags & PERIPH_NO_RESET) && !(c->flags & PERIPH_MANUAL_RESET))
899                 clk_writel(PERIPH_CLK_TO_ENB_BIT(c),
900                         RST_DEVICES_CLR + PERIPH_CLK_TO_ENB_SET_REG(c));
901         if (c->flags & PERIPH_EMC_ENB) {
902                 /* The EMC peripheral clock has 2 extra enable bits */
903                 /* FIXME: Do they need to be disabled? */
904                 val = clk_readl(c->reg);
905                 val |= 0x3 << 24;
906                 clk_writel(val, c->reg);
907         }
908         return 0;
909 }
910
911 static void tegra2_periph_clk_disable(struct clk *c)
912 {
913         pr_debug("%s on clock %s\n", __func__, c->name);
914
915         if (c->refcnt)
916                 tegra_periph_clk_enable_refcount[c->u.periph.clk_num]--;
917
918         if (tegra_periph_clk_enable_refcount[c->u.periph.clk_num] == 0)
919                 clk_writel(PERIPH_CLK_TO_ENB_BIT(c),
920                         CLK_OUT_ENB_CLR + PERIPH_CLK_TO_ENB_SET_REG(c));
921 }
922
923 static void tegra2_periph_clk_reset(struct clk *c, bool assert)
924 {
925         unsigned long base = assert ? RST_DEVICES_SET : RST_DEVICES_CLR;
926
927         pr_debug("%s %s on clock %s\n", __func__,
928                  assert ? "assert" : "deassert", c->name);
929         if (!(c->flags & PERIPH_NO_RESET))
930                 clk_writel(PERIPH_CLK_TO_ENB_BIT(c),
931                            base + PERIPH_CLK_TO_ENB_SET_REG(c));
932 }
933
934 static int tegra2_periph_clk_set_parent(struct clk *c, struct clk *p)
935 {
936         u32 val;
937         const struct clk_mux_sel *sel;
938         pr_debug("%s: %s %s\n", __func__, c->name, p->name);
939         for (sel = c->inputs; sel->input != NULL; sel++) {
940                 if (sel->input == p) {
941                         val = clk_readl(c->reg);
942                         val &= ~PERIPH_CLK_SOURCE_MASK;
943                         val |= (sel->value) << PERIPH_CLK_SOURCE_SHIFT;
944
945                         if (c->refcnt)
946                                 clk_enable(p);
947
948                         clk_writel(val, c->reg);
949
950                         if (c->refcnt && c->parent)
951                                 clk_disable(c->parent);
952
953                         clk_reparent(c, p);
954                         return 0;
955                 }
956         }
957
958         return -EINVAL;
959 }
960
961 static int tegra2_periph_clk_set_rate(struct clk *c, unsigned long rate)
962 {
963         u32 val;
964         int divider;
965         unsigned long parent_rate = clk_get_rate(c->parent);
966
967         if (c->flags & DIV_U71) {
968                 divider = clk_div71_get_divider(parent_rate, rate);
969                 if (divider >= 0) {
970                         val = clk_readl(c->reg);
971                         val &= ~PERIPH_CLK_SOURCE_DIVU71_MASK;
972                         val |= divider;
973                         clk_writel(val, c->reg);
974                         c->div = divider + 2;
975                         c->mul = 2;
976                         return 0;
977                 }
978         } else if (c->flags & DIV_U16) {
979                 divider = clk_div16_get_divider(parent_rate, rate);
980                 if (divider >= 0) {
981                         val = clk_readl(c->reg);
982                         val &= ~PERIPH_CLK_SOURCE_DIVU16_MASK;
983                         val |= divider;
984                         clk_writel(val, c->reg);
985                         c->div = divider + 1;
986                         c->mul = 1;
987                         return 0;
988                 }
989         } else if (parent_rate <= rate) {
990                 c->div = 1;
991                 c->mul = 1;
992                 return 0;
993         }
994         return -EINVAL;
995 }
996
997 static long tegra2_periph_clk_round_rate(struct clk *c,
998         unsigned long rate)
999 {
1000         int divider;
1001         unsigned long parent_rate = clk_get_rate(c->parent);
1002         pr_debug("%s: %s %lu\n", __func__, c->name, rate);
1003
1004         if (c->flags & DIV_U71) {
1005                 divider = clk_div71_get_divider(parent_rate, rate);
1006                 if (divider < 0)
1007                         return divider;
1008
1009                 return DIV_ROUND_UP(parent_rate * 2, divider + 2);
1010         } else if (c->flags & DIV_U16) {
1011                 divider = clk_div16_get_divider(parent_rate, rate);
1012                 if (divider < 0)
1013                         return divider;
1014                 return DIV_ROUND_UP(parent_rate, divider + 1);
1015         }
1016         return -EINVAL;
1017 }
1018
1019 static struct clk_ops tegra_periph_clk_ops = {
1020         .init                   = &tegra2_periph_clk_init,
1021         .enable                 = &tegra2_periph_clk_enable,
1022         .disable                = &tegra2_periph_clk_disable,
1023         .set_parent             = &tegra2_periph_clk_set_parent,
1024         .set_rate               = &tegra2_periph_clk_set_rate,
1025         .round_rate             = &tegra2_periph_clk_round_rate,
1026         .reset                  = &tegra2_periph_clk_reset,
1027 };
1028
1029 /* The SDMMC controllers have extra bits in the clock source register that
1030  * adjust the delay between the clock and data to compenstate for delays
1031  * on the PCB. */
1032 void tegra2_sdmmc_tap_delay(struct clk *c, int delay) {
1033         u32 reg;
1034
1035         delay = clamp(delay, 0, 15);
1036         reg = clk_readl(c->reg);
1037         reg &= ~SDMMC_CLK_INT_FB_DLY_MASK;
1038         reg |= SDMMC_CLK_INT_FB_SEL;
1039         reg |= delay << SDMMC_CLK_INT_FB_DLY_SHIFT;
1040         clk_writel(reg, c->reg);
1041 }
1042
1043 /* External memory controller clock ops */
1044 static void tegra2_emc_clk_init(struct clk *c)
1045 {
1046         tegra2_periph_clk_init(c);
1047         c->max_rate = clk_get_rate_locked(c);
1048 }
1049
1050 static long tegra2_emc_clk_round_rate(struct clk *c, unsigned long rate)
1051 {
1052         long new_rate = rate;
1053
1054         new_rate = tegra_emc_round_rate(new_rate);
1055         if (new_rate < 0)
1056                 return c->max_rate;
1057
1058         BUG_ON(new_rate != tegra2_periph_clk_round_rate(c, new_rate));
1059
1060         return new_rate;
1061 }
1062
1063 static int tegra2_emc_clk_set_rate(struct clk *c, unsigned long rate)
1064 {
1065         int ret;
1066         /* The Tegra2 memory controller has an interlock with the clock
1067          * block that allows memory shadowed registers to be updated,
1068          * and then transfer them to the main registers at the same
1069          * time as the clock update without glitches. */
1070         ret = tegra_emc_set_rate(rate);
1071         if (ret < 0)
1072                 return ret;
1073
1074         ret = tegra2_periph_clk_set_rate(c, rate);
1075         udelay(1);
1076
1077         return ret;
1078 }
1079
1080 static struct clk_ops tegra_emc_clk_ops = {
1081         .init                   = &tegra2_emc_clk_init,
1082         .enable                 = &tegra2_periph_clk_enable,
1083         .disable                = &tegra2_periph_clk_disable,
1084         .set_parent             = &tegra2_periph_clk_set_parent,
1085         .set_rate               = &tegra2_emc_clk_set_rate,
1086         .round_rate             = &tegra2_emc_clk_round_rate,
1087         .reset                  = &tegra2_periph_clk_reset,
1088 };
1089
1090 /* Clock doubler ops */
1091 static void tegra2_clk_double_init(struct clk *c)
1092 {
1093         c->mul = 2;
1094         c->div = 1;
1095         c->state = ON;
1096         if (!(clk_readl(CLK_OUT_ENB + PERIPH_CLK_TO_ENB_REG(c)) &
1097                         PERIPH_CLK_TO_ENB_BIT(c)))
1098                 c->state = OFF;
1099 };
1100
1101 static int tegra2_clk_double_set_rate(struct clk *c, unsigned long rate)
1102 {
1103         if (rate != 2 * clk_get_rate(c->parent))
1104                 return -EINVAL;
1105         c->mul = 2;
1106         c->div = 1;
1107         return 0;
1108 }
1109
1110 static struct clk_ops tegra_clk_double_ops = {
1111         .init                   = &tegra2_clk_double_init,
1112         .enable                 = &tegra2_periph_clk_enable,
1113         .disable                = &tegra2_periph_clk_disable,
1114         .set_rate               = &tegra2_clk_double_set_rate,
1115 };
1116
1117 /* Audio sync clock ops */
1118 static void tegra2_audio_sync_clk_init(struct clk *c)
1119 {
1120         int source;
1121         const struct clk_mux_sel *sel;
1122         u32 val = clk_readl(c->reg);
1123         c->state = (val & (1<<4)) ? OFF : ON;
1124         source = val & 0xf;
1125         for (sel = c->inputs; sel->input != NULL; sel++)
1126                 if (sel->value == source)
1127                         break;
1128         BUG_ON(sel->input == NULL);
1129         c->parent = sel->input;
1130 }
1131
1132 static int tegra2_audio_sync_clk_enable(struct clk *c)
1133 {
1134         clk_writel(0, c->reg);
1135         return 0;
1136 }
1137
1138 static void tegra2_audio_sync_clk_disable(struct clk *c)
1139 {
1140         clk_writel(1, c->reg);
1141 }
1142
1143 static int tegra2_audio_sync_clk_set_parent(struct clk *c, struct clk *p)
1144 {
1145         u32 val;
1146         const struct clk_mux_sel *sel;
1147         for (sel = c->inputs; sel->input != NULL; sel++) {
1148                 if (sel->input == p) {
1149                         val = clk_readl(c->reg);
1150                         val &= ~0xf;
1151                         val |= sel->value;
1152
1153                         if (c->refcnt)
1154                                 clk_enable(p);
1155
1156                         clk_writel(val, c->reg);
1157
1158                         if (c->refcnt && c->parent)
1159                                 clk_disable(c->parent);
1160
1161                         clk_reparent(c, p);
1162                         return 0;
1163                 }
1164         }
1165
1166         return -EINVAL;
1167 }
1168
1169 static struct clk_ops tegra_audio_sync_clk_ops = {
1170         .init       = tegra2_audio_sync_clk_init,
1171         .enable     = tegra2_audio_sync_clk_enable,
1172         .disable    = tegra2_audio_sync_clk_disable,
1173         .set_parent = tegra2_audio_sync_clk_set_parent,
1174 };
1175
1176 /* cdev1 and cdev2 (dap_mclk1 and dap_mclk2) ops */
1177
1178 static void tegra2_cdev_clk_init(struct clk *c)
1179 {
1180         /* We could un-tristate the cdev1 or cdev2 pingroup here; this is
1181          * currently done in the pinmux code. */
1182         c->state = ON;
1183         if (!(clk_readl(CLK_OUT_ENB + PERIPH_CLK_TO_ENB_REG(c)) &
1184                         PERIPH_CLK_TO_ENB_BIT(c)))
1185                 c->state = OFF;
1186 }
1187
1188 static int tegra2_cdev_clk_enable(struct clk *c)
1189 {
1190         clk_writel(PERIPH_CLK_TO_ENB_BIT(c),
1191                 CLK_OUT_ENB_SET + PERIPH_CLK_TO_ENB_SET_REG(c));
1192         return 0;
1193 }
1194
1195 static void tegra2_cdev_clk_disable(struct clk *c)
1196 {
1197         clk_writel(PERIPH_CLK_TO_ENB_BIT(c),
1198                 CLK_OUT_ENB_CLR + PERIPH_CLK_TO_ENB_SET_REG(c));
1199 }
1200
1201 static struct clk_ops tegra_cdev_clk_ops = {
1202         .init                   = &tegra2_cdev_clk_init,
1203         .enable                 = &tegra2_cdev_clk_enable,
1204         .disable                = &tegra2_cdev_clk_disable,
1205 };
1206
1207 /* shared bus ops */
1208 /*
1209  * Some clocks may have multiple downstream users that need to request a
1210  * higher clock rate.  Shared bus clocks provide a unique shared_bus_user
1211  * clock to each user.  The frequency of the bus is set to the highest
1212  * enabled shared_bus_user clock, with a minimum value set by the
1213  * shared bus.
1214  */
1215 static void tegra_clk_shared_bus_update(struct clk *bus)
1216 {
1217         struct clk *c;
1218         unsigned long rate = bus->min_rate;
1219
1220         list_for_each_entry(c, &bus->shared_bus_list,
1221                         u.shared_bus_user.node) {
1222                 if (c->u.shared_bus_user.enabled)
1223                         rate = max(c->u.shared_bus_user.rate, rate);
1224         }
1225
1226         if (rate != clk_get_rate(bus))
1227                 clk_set_rate(bus, rate);
1228 };
1229
1230 static void tegra_clk_shared_bus_init(struct clk *c)
1231 {
1232         c->max_rate = c->parent->max_rate;
1233         c->u.shared_bus_user.rate = c->parent->max_rate;
1234         c->state = OFF;
1235         c->set = true;
1236
1237         list_add_tail(&c->u.shared_bus_user.node,
1238                 &c->parent->shared_bus_list);
1239 }
1240
1241 static int tegra_clk_shared_bus_set_rate(struct clk *c, unsigned long rate)
1242 {
1243         c->u.shared_bus_user.rate = rate;
1244         tegra_clk_shared_bus_update(c->parent);
1245         return 0;
1246 }
1247
1248 static long tegra_clk_shared_bus_round_rate(struct clk *c, unsigned long rate)
1249 {
1250         return clk_round_rate(c->parent, rate);
1251 }
1252
1253 static int tegra_clk_shared_bus_enable(struct clk *c)
1254 {
1255         c->u.shared_bus_user.enabled = true;
1256         tegra_clk_shared_bus_update(c->parent);
1257         return 0;
1258 }
1259
1260 static void tegra_clk_shared_bus_disable(struct clk *c)
1261 {
1262         c->u.shared_bus_user.enabled = false;
1263         tegra_clk_shared_bus_update(c->parent);
1264 }
1265
1266 static struct clk_ops tegra_clk_shared_bus_ops = {
1267         .init = tegra_clk_shared_bus_init,
1268         .enable = tegra_clk_shared_bus_enable,
1269         .disable = tegra_clk_shared_bus_disable,
1270         .set_rate = tegra_clk_shared_bus_set_rate,
1271         .round_rate = tegra_clk_shared_bus_round_rate,
1272 };
1273
1274
1275 /* Clock definitions */
1276 static struct clk tegra_clk_32k = {
1277         .name = "clk_32k",
1278         .rate = 32768,
1279         .ops  = NULL,
1280         .max_rate = 32768,
1281 };
1282
1283 static struct clk_pll_freq_table tegra_pll_s_freq_table[] = {
1284         {32768, 12000000, 366, 1, 1, 0},
1285         {32768, 13000000, 397, 1, 1, 0},
1286         {32768, 19200000, 586, 1, 1, 0},
1287         {32768, 26000000, 793, 1, 1, 0},
1288         {0, 0, 0, 0, 0, 0},
1289 };
1290
1291 static struct clk tegra_pll_s = {
1292         .name      = "pll_s",
1293         .flags     = PLL_ALT_MISC_REG,
1294         .ops       = &tegra_pll_ops,
1295         .parent    = &tegra_clk_32k,
1296         .max_rate  = 26000000,
1297         .reg       = 0xf0,
1298         .u.pll = {
1299                 .input_min = 32768,
1300                 .input_max = 32768,
1301                 .cf_min    = 0, /* FIXME */
1302                 .cf_max    = 0, /* FIXME */
1303                 .vco_min   = 12000000,
1304                 .vco_max   = 26000000,
1305                 .freq_table = tegra_pll_s_freq_table,
1306                 .lock_delay = 300,
1307         },
1308 };
1309
1310 static struct clk_mux_sel tegra_clk_m_sel[] = {
1311         { .input = &tegra_clk_32k, .value = 0},
1312         { .input = &tegra_pll_s,  .value = 1},
1313         { 0, 0},
1314 };
1315
1316 static struct clk tegra_clk_m = {
1317         .name      = "clk_m",
1318         .flags     = ENABLE_ON_INIT,
1319         .ops       = &tegra_clk_m_ops,
1320         .inputs    = tegra_clk_m_sel,
1321         .reg       = 0x1fc,
1322         .reg_shift = 28,
1323         .max_rate  = 26000000,
1324 };
1325
1326 static struct clk_pll_freq_table tegra_pll_c_freq_table[] = {
1327         { 0, 0, 0, 0, 0, 0 },
1328 };
1329
1330 static struct clk tegra_pll_c = {
1331         .name      = "pll_c",
1332         .flags     = PLL_HAS_CPCON,
1333         .ops       = &tegra_pll_ops,
1334         .reg       = 0x80,
1335         .parent    = &tegra_clk_m,
1336         .max_rate  = 600000000,
1337         .u.pll = {
1338                 .input_min = 2000000,
1339                 .input_max = 31000000,
1340                 .cf_min    = 1000000,
1341                 .cf_max    = 6000000,
1342                 .vco_min   = 20000000,
1343                 .vco_max   = 1400000000,
1344                 .freq_table = tegra_pll_c_freq_table,
1345                 .lock_delay = 300,
1346         },
1347 };
1348
1349 static struct clk tegra_pll_c_out1 = {
1350         .name      = "pll_c_out1",
1351         .ops       = &tegra_pll_div_ops,
1352         .flags     = DIV_U71,
1353         .parent    = &tegra_pll_c,
1354         .reg       = 0x84,
1355         .reg_shift = 0,
1356         .max_rate  = 600000000,
1357 };
1358
1359 static struct clk_pll_freq_table tegra_pll_m_freq_table[] = {
1360         { 12000000, 666000000, 666, 12, 1, 8},
1361         { 13000000, 666000000, 666, 13, 1, 8},
1362         { 19200000, 666000000, 555, 16, 1, 8},
1363         { 26000000, 666000000, 666, 26, 1, 8},
1364         { 12000000, 600000000, 600, 12, 1, 8},
1365         { 13000000, 600000000, 600, 13, 1, 8},
1366         { 19200000, 600000000, 375, 12, 1, 6},
1367         { 26000000, 600000000, 600, 26, 1, 8},
1368         { 0, 0, 0, 0, 0, 0 },
1369 };
1370
1371 static struct clk tegra_pll_m = {
1372         .name      = "pll_m",
1373         .flags     = PLL_HAS_CPCON,
1374         .ops       = &tegra_pll_ops,
1375         .reg       = 0x90,
1376         .parent    = &tegra_clk_m,
1377         .max_rate  = 800000000,
1378         .u.pll = {
1379                 .input_min = 2000000,
1380                 .input_max = 31000000,
1381                 .cf_min    = 1000000,
1382                 .cf_max    = 6000000,
1383                 .vco_min   = 20000000,
1384                 .vco_max   = 1200000000,
1385                 .freq_table = tegra_pll_m_freq_table,
1386                 .lock_delay = 300,
1387         },
1388 };
1389
1390 static struct clk tegra_pll_m_out1 = {
1391         .name      = "pll_m_out1",
1392         .ops       = &tegra_pll_div_ops,
1393         .flags     = DIV_U71,
1394         .parent    = &tegra_pll_m,
1395         .reg       = 0x94,
1396         .reg_shift = 0,
1397         .max_rate  = 600000000,
1398 };
1399
1400 static struct clk_pll_freq_table tegra_pll_p_freq_table[] = {
1401         { 12000000, 216000000, 432, 12, 2, 8},
1402         { 13000000, 216000000, 432, 13, 2, 8},
1403         { 19200000, 216000000, 90,   4, 2, 1},
1404         { 26000000, 216000000, 432, 26, 2, 8},
1405         { 12000000, 432000000, 432, 12, 1, 8},
1406         { 13000000, 432000000, 432, 13, 1, 8},
1407         { 19200000, 432000000, 90,   4, 1, 1},
1408         { 26000000, 432000000, 432, 26, 1, 8},
1409         { 0, 0, 0, 0, 0, 0 },
1410 };
1411
1412 static struct clk tegra_pll_p = {
1413         .name      = "pll_p",
1414         .flags     = ENABLE_ON_INIT | PLL_FIXED | PLL_HAS_CPCON,
1415         .ops       = &tegra_pll_ops,
1416         .reg       = 0xa0,
1417         .parent    = &tegra_clk_m,
1418         .max_rate  = 432000000,
1419         .u.pll = {
1420                 .input_min = 2000000,
1421                 .input_max = 31000000,
1422                 .cf_min    = 1000000,
1423                 .cf_max    = 6000000,
1424                 .vco_min   = 20000000,
1425                 .vco_max   = 1400000000,
1426                 .freq_table = tegra_pll_p_freq_table,
1427                 .lock_delay = 300,
1428         },
1429 };
1430
1431 static struct clk tegra_pll_p_out1 = {
1432         .name      = "pll_p_out1",
1433         .ops       = &tegra_pll_div_ops,
1434         .flags     = ENABLE_ON_INIT | DIV_U71 | DIV_U71_FIXED,
1435         .parent    = &tegra_pll_p,
1436         .reg       = 0xa4,
1437         .reg_shift = 0,
1438         .max_rate  = 432000000,
1439 };
1440
1441 static struct clk tegra_pll_p_out2 = {
1442         .name      = "pll_p_out2",
1443         .ops       = &tegra_pll_div_ops,
1444         .flags     = ENABLE_ON_INIT | DIV_U71 | DIV_U71_FIXED,
1445         .parent    = &tegra_pll_p,
1446         .reg       = 0xa4,
1447         .reg_shift = 16,
1448         .max_rate  = 432000000,
1449 };
1450
1451 static struct clk tegra_pll_p_out3 = {
1452         .name      = "pll_p_out3",
1453         .ops       = &tegra_pll_div_ops,
1454         .flags     = ENABLE_ON_INIT | DIV_U71 | DIV_U71_FIXED,
1455         .parent    = &tegra_pll_p,
1456         .reg       = 0xa8,
1457         .reg_shift = 0,
1458         .max_rate  = 432000000,
1459 };
1460
1461 static struct clk tegra_pll_p_out4 = {
1462         .name      = "pll_p_out4",
1463         .ops       = &tegra_pll_div_ops,
1464         .flags     = ENABLE_ON_INIT | DIV_U71 | DIV_U71_FIXED,
1465         .parent    = &tegra_pll_p,
1466         .reg       = 0xa8,
1467         .reg_shift = 16,
1468         .max_rate  = 432000000,
1469 };
1470
1471 static struct clk_pll_freq_table tegra_pll_a_freq_table[] = {
1472         { 28800000, 56448000, 49, 25, 1, 1},
1473         { 28800000, 73728000, 64, 25, 1, 1},
1474         { 28800000, 24000000,  5,  6, 1, 1},
1475         { 0, 0, 0, 0, 0, 0 },
1476 };
1477
1478 static struct clk tegra_pll_a = {
1479         .name      = "pll_a",
1480         .flags     = PLL_HAS_CPCON,
1481         .ops       = &tegra_pll_ops,
1482         .reg       = 0xb0,
1483         .parent    = &tegra_pll_p_out1,
1484         .max_rate  = 73728000,
1485         .u.pll = {
1486                 .input_min = 2000000,
1487                 .input_max = 31000000,
1488                 .cf_min    = 1000000,
1489                 .cf_max    = 6000000,
1490                 .vco_min   = 20000000,
1491                 .vco_max   = 1400000000,
1492                 .freq_table = tegra_pll_a_freq_table,
1493                 .lock_delay = 300,
1494         },
1495 };
1496
1497 static struct clk tegra_pll_a_out0 = {
1498         .name      = "pll_a_out0",
1499         .ops       = &tegra_pll_div_ops,
1500         .flags     = DIV_U71,
1501         .parent    = &tegra_pll_a,
1502         .reg       = 0xb4,
1503         .reg_shift = 0,
1504         .max_rate  = 73728000,
1505 };
1506
1507 static struct clk_pll_freq_table tegra_pll_d_freq_table[] = {
1508         { 12000000, 216000000, 216, 12, 1, 4},
1509         { 13000000, 216000000, 216, 13, 1, 4},
1510         { 19200000, 216000000, 135, 12, 1, 3},
1511         { 26000000, 216000000, 216, 26, 1, 4},
1512
1513         { 12000000, 594000000, 594, 12, 1, 8},
1514         { 13000000, 594000000, 594, 13, 1, 8},
1515         { 19200000, 594000000, 495, 16, 1, 8},
1516         { 26000000, 594000000, 594, 26, 1, 8},
1517
1518         { 12000000, 1000000000, 1000, 12, 1, 12},
1519         { 13000000, 1000000000, 1000, 13, 1, 12},
1520         { 19200000, 1000000000, 625,  12, 1, 8},
1521         { 26000000, 1000000000, 1000, 26, 1, 12},
1522
1523         { 0, 0, 0, 0, 0, 0 },
1524 };
1525
1526 static struct clk tegra_pll_d = {
1527         .name      = "pll_d",
1528         .flags     = PLL_HAS_CPCON | PLLD,
1529         .ops       = &tegra_pll_ops,
1530         .reg       = 0xd0,
1531         .parent    = &tegra_clk_m,
1532         .max_rate  = 1000000000,
1533         .u.pll = {
1534                 .input_min = 2000000,
1535                 .input_max = 40000000,
1536                 .cf_min    = 1000000,
1537                 .cf_max    = 6000000,
1538                 .vco_min   = 40000000,
1539                 .vco_max   = 1000000000,
1540                 .freq_table = tegra_pll_d_freq_table,
1541                 .lock_delay = 1000,
1542         },
1543 };
1544
1545 static struct clk tegra_pll_d_out0 = {
1546         .name      = "pll_d_out0",
1547         .ops       = &tegra_pll_div_ops,
1548         .flags     = DIV_2 | PLLD,
1549         .parent    = &tegra_pll_d,
1550         .max_rate  = 500000000,
1551 };
1552
1553 static struct clk_pll_freq_table tegra_pll_u_freq_table[] = {
1554         { 12000000, 480000000, 960, 12, 2, 0},
1555         { 13000000, 480000000, 960, 13, 2, 0},
1556         { 19200000, 480000000, 200, 4,  2, 0},
1557         { 26000000, 480000000, 960, 26, 2, 0},
1558         { 0, 0, 0, 0, 0, 0 },
1559 };
1560
1561 static struct clk tegra_pll_u = {
1562         .name      = "pll_u",
1563         .flags     = PLLU,
1564         .ops       = &tegra_pll_ops,
1565         .reg       = 0xc0,
1566         .parent    = &tegra_clk_m,
1567         .max_rate  = 480000000,
1568         .u.pll = {
1569                 .input_min = 2000000,
1570                 .input_max = 40000000,
1571                 .cf_min    = 1000000,
1572                 .cf_max    = 6000000,
1573                 .vco_min   = 480000000,
1574                 .vco_max   = 960000000,
1575                 .freq_table = tegra_pll_u_freq_table,
1576                 .lock_delay = 1000,
1577         },
1578 };
1579
1580 static struct clk_pll_freq_table tegra_pll_x_freq_table[] = {
1581         /* 1 GHz */
1582         { 12000000, 1000000000, 1000, 12, 1, 12},
1583         { 13000000, 1000000000, 1000, 13, 1, 12},
1584         { 19200000, 1000000000, 625,  12, 1, 8},
1585         { 26000000, 1000000000, 1000, 26, 1, 12},
1586
1587         /* 912 MHz */
1588         { 12000000, 912000000,  912,  12, 1, 12},
1589         { 13000000, 912000000,  912,  13, 1, 12},
1590         { 19200000, 912000000,  760,  16, 1, 8},
1591         { 26000000, 912000000,  912,  26, 1, 12},
1592
1593         /* 816 MHz */
1594         { 12000000, 816000000,  816,  12, 1, 12},
1595         { 13000000, 816000000,  816,  13, 1, 12},
1596         { 19200000, 816000000,  680,  16, 1, 8},
1597         { 26000000, 816000000,  816,  26, 1, 12},
1598
1599         /* 760 MHz */
1600         { 12000000, 760000000,  760,  12, 1, 12},
1601         { 13000000, 760000000,  760,  13, 1, 12},
1602         { 19200000, 760000000,  950,  24, 1, 8},
1603         { 26000000, 760000000,  760,  26, 1, 12},
1604
1605         /* 608 MHz */
1606         { 12000000, 608000000,  608,  12, 1, 12},
1607         { 13000000, 608000000,  608,  13, 1, 12},
1608         { 19200000, 608000000,  380,  12, 1, 8},
1609         { 26000000, 608000000,  608,  26, 1, 12},
1610
1611         /* 456 MHz */
1612         { 12000000, 456000000,  456,  12, 1, 12},
1613         { 13000000, 456000000,  456,  13, 1, 12},
1614         { 19200000, 456000000,  380,  16, 1, 8},
1615         { 26000000, 456000000,  456,  26, 1, 12},
1616
1617         /* 312 MHz */
1618         { 12000000, 312000000,  312,  12, 1, 12},
1619         { 13000000, 312000000,  312,  13, 1, 12},
1620         { 19200000, 312000000,  260,  16, 1, 8},
1621         { 26000000, 312000000,  312,  26, 1, 12},
1622
1623         { 0, 0, 0, 0, 0, 0 },
1624 };
1625
1626 static struct clk tegra_pll_x = {
1627         .name      = "pll_x",
1628         .flags     = PLL_HAS_CPCON | PLL_ALT_MISC_REG,
1629         .ops       = &tegra_pllx_ops,
1630         .reg       = 0xe0,
1631         .parent    = &tegra_clk_m,
1632         .max_rate  = 1000000000,
1633         .u.pll = {
1634                 .input_min = 2000000,
1635                 .input_max = 31000000,
1636                 .cf_min    = 1000000,
1637                 .cf_max    = 6000000,
1638                 .vco_min   = 20000000,
1639                 .vco_max   = 1200000000,
1640                 .freq_table = tegra_pll_x_freq_table,
1641                 .lock_delay = 300,
1642         },
1643 };
1644
1645 static struct clk tegra_clk_d = {
1646         .name      = "clk_d",
1647         .flags     = PERIPH_NO_RESET,
1648         .ops       = &tegra_clk_double_ops,
1649         .reg       = 0x34,
1650         .reg_shift = 12,
1651         .parent    = &tegra_clk_m,
1652         .max_rate  = 52000000,
1653         .u.periph  = {
1654                 .clk_num = 90,
1655         },
1656 };
1657
1658 /* dap_mclk1, belongs to the cdev1 pingroup. */
1659 static struct clk tegra_dev1_clk = {
1660         .name      = "clk_dev1",
1661         .ops       = &tegra_cdev_clk_ops,
1662         .rate      = 26000000,
1663         .max_rate  = 26000000,
1664         .u.periph  = {
1665                 .clk_num = 94,
1666         },
1667 };
1668
1669 /* dap_mclk2, belongs to the cdev2 pingroup. */
1670 static struct clk tegra_dev2_clk = {
1671         .name      = "clk_dev2",
1672         .ops       = &tegra_cdev_clk_ops,
1673         .rate      = 26000000,
1674         .max_rate  = 26000000,
1675         .u.periph  = {
1676                 .clk_num   = 93,
1677         },
1678 };
1679
1680 /* initialized before peripheral clocks */
1681 static struct clk_mux_sel mux_audio_sync_clk[8+1];
1682 static const struct audio_sources {
1683         const char *name;
1684         int value;
1685 } mux_audio_sync_clk_sources[] = {
1686         { .name = "spdif_in", .value = 0 },
1687         { .name = "i2s1", .value = 1 },
1688         { .name = "i2s2", .value = 2 },
1689         { .name = "pll_a_out0", .value = 4 },
1690 #if 0 /* FIXME: not implemented */
1691         { .name = "ac97", .value = 3 },
1692         { .name = "ext_audio_clk2", .value = 5 },
1693         { .name = "ext_audio_clk1", .value = 6 },
1694         { .name = "ext_vimclk", .value = 7 },
1695 #endif
1696         { 0, 0 }
1697 };
1698
1699 static struct clk tegra_clk_audio = {
1700         .name      = "audio",
1701         .inputs    = mux_audio_sync_clk,
1702         .reg       = 0x38,
1703         .max_rate  = 73728000,
1704         .ops       = &tegra_audio_sync_clk_ops
1705 };
1706
1707 static struct clk tegra_clk_audio_2x = {
1708         .name      = "audio_2x",
1709         .flags     = PERIPH_NO_RESET,
1710         .max_rate  = 48000000,
1711         .ops       = &tegra_clk_double_ops,
1712         .reg       = 0x34,
1713         .reg_shift = 8,
1714         .parent    = &tegra_clk_audio,
1715         .u.periph = {
1716                 .clk_num = 89,
1717         },
1718 };
1719
1720 struct clk_lookup tegra_audio_clk_lookups[] = {
1721         { .con_id = "audio", .clk = &tegra_clk_audio },
1722         { .con_id = "audio_2x", .clk = &tegra_clk_audio_2x }
1723 };
1724
1725 /* This is called after peripheral clocks are initialized, as the
1726  * audio_sync clock depends on some of the peripheral clocks.
1727  */
1728
1729 static void init_audio_sync_clock_mux(void)
1730 {
1731         int i;
1732         struct clk_mux_sel *sel = mux_audio_sync_clk;
1733         const struct audio_sources *src = mux_audio_sync_clk_sources;
1734         struct clk_lookup *lookup;
1735
1736         for (i = 0; src->name; i++, sel++, src++) {
1737                 sel->input = tegra_get_clock_by_name(src->name);
1738                 if (!sel->input)
1739                         pr_err("%s: could not find clk %s\n", __func__,
1740                                 src->name);
1741                 sel->value = src->value;
1742         }
1743
1744         lookup = tegra_audio_clk_lookups;
1745         for (i = 0; i < ARRAY_SIZE(tegra_audio_clk_lookups); i++, lookup++) {
1746                 clk_init(lookup->clk);
1747                 clkdev_add(lookup);
1748         }
1749 }
1750
1751 static struct clk_mux_sel mux_cclk[] = {
1752         { .input = &tegra_clk_m,        .value = 0},
1753         { .input = &tegra_pll_c,        .value = 1},
1754         { .input = &tegra_clk_32k,      .value = 2},
1755         { .input = &tegra_pll_m,        .value = 3},
1756         { .input = &tegra_pll_p,        .value = 4},
1757         { .input = &tegra_pll_p_out4,   .value = 5},
1758         { .input = &tegra_pll_p_out3,   .value = 6},
1759         { .input = &tegra_clk_d,        .value = 7},
1760         { .input = &tegra_pll_x,        .value = 8},
1761         { 0, 0},
1762 };
1763
1764 static struct clk_mux_sel mux_sclk[] = {
1765         { .input = &tegra_clk_m,        .value = 0},
1766         { .input = &tegra_pll_c_out1,   .value = 1},
1767         { .input = &tegra_pll_p_out4,   .value = 2},
1768         { .input = &tegra_pll_p_out3,   .value = 3},
1769         { .input = &tegra_pll_p_out2,   .value = 4},
1770         { .input = &tegra_clk_d,        .value = 5},
1771         { .input = &tegra_clk_32k,      .value = 6},
1772         { .input = &tegra_pll_m_out1,   .value = 7},
1773         { 0, 0},
1774 };
1775
1776 static struct clk tegra_clk_cclk = {
1777         .name   = "cclk",
1778         .inputs = mux_cclk,
1779         .reg    = 0x20,
1780         .ops    = &tegra_super_ops,
1781         .max_rate = 1000000000,
1782 };
1783
1784 static struct clk tegra_clk_sclk = {
1785         .name   = "sclk",
1786         .inputs = mux_sclk,
1787         .reg    = 0x28,
1788         .ops    = &tegra_super_ops,
1789         .max_rate = 240000000,
1790         .min_rate = 120000000,
1791 };
1792
1793 static struct clk tegra_clk_virtual_cpu = {
1794         .name      = "cpu",
1795         .parent    = &tegra_clk_cclk,
1796         .ops       = &tegra_cpu_ops,
1797         .max_rate  = 1000000000,
1798         .u.cpu = {
1799                 .main      = &tegra_pll_x,
1800                 .backup    = &tegra_pll_p,
1801         },
1802 };
1803
1804 static struct clk tegra_clk_cop = {
1805         .name      = "cop",
1806         .parent    = &tegra_clk_sclk,
1807         .ops       = &tegra_cop_ops,
1808         .max_rate  = 240000000,
1809 };
1810
1811 static struct clk tegra_clk_hclk = {
1812         .name           = "hclk",
1813         .flags          = DIV_BUS,
1814         .parent         = &tegra_clk_sclk,
1815         .reg            = 0x30,
1816         .reg_shift      = 4,
1817         .ops            = &tegra_bus_ops,
1818         .max_rate       = 240000000,
1819 };
1820
1821 static struct clk tegra_clk_pclk = {
1822         .name           = "pclk",
1823         .flags          = DIV_BUS,
1824         .parent         = &tegra_clk_hclk,
1825         .reg            = 0x30,
1826         .reg_shift      = 0,
1827         .ops            = &tegra_bus_ops,
1828         .max_rate       = 120000000,
1829 };
1830
1831 static struct clk tegra_clk_blink = {
1832         .name           = "blink",
1833         .parent         = &tegra_clk_32k,
1834         .reg            = 0x40,
1835         .ops            = &tegra_blink_clk_ops,
1836         .max_rate       = 32768,
1837 };
1838
1839 static struct clk_mux_sel mux_pllm_pllc_pllp_plla[] = {
1840         { .input = &tegra_pll_m, .value = 0},
1841         { .input = &tegra_pll_c, .value = 1},
1842         { .input = &tegra_pll_p, .value = 2},
1843         { .input = &tegra_pll_a_out0, .value = 3},
1844         { 0, 0},
1845 };
1846
1847 static struct clk_mux_sel mux_pllm_pllc_pllp_clkm[] = {
1848         { .input = &tegra_pll_m, .value = 0},
1849         { .input = &tegra_pll_c, .value = 1},
1850         { .input = &tegra_pll_p, .value = 2},
1851         { .input = &tegra_clk_m, .value = 3},
1852         { 0, 0},
1853 };
1854
1855 static struct clk_mux_sel mux_pllp_pllc_pllm_clkm[] = {
1856         { .input = &tegra_pll_p, .value = 0},
1857         { .input = &tegra_pll_c, .value = 1},
1858         { .input = &tegra_pll_m, .value = 2},
1859         { .input = &tegra_clk_m, .value = 3},
1860         { 0, 0},
1861 };
1862
1863 static struct clk_mux_sel mux_pllaout0_audio2x_pllp_clkm[] = {
1864         {.input = &tegra_pll_a_out0, .value = 0},
1865         {.input = &tegra_clk_audio_2x, .value = 1},
1866         {.input = &tegra_pll_p, .value = 2},
1867         {.input = &tegra_clk_m, .value = 3},
1868         { 0, 0},
1869 };
1870
1871 static struct clk_mux_sel mux_pllp_plld_pllc_clkm[] = {
1872         {.input = &tegra_pll_p, .value = 0},
1873         {.input = &tegra_pll_d_out0, .value = 1},
1874         {.input = &tegra_pll_c, .value = 2},
1875         {.input = &tegra_clk_m, .value = 3},
1876         { 0, 0},
1877 };
1878
1879 static struct clk_mux_sel mux_pllp_pllc_audio_clkm_clk32[] = {
1880         {.input = &tegra_pll_p,     .value = 0},
1881         {.input = &tegra_pll_c,     .value = 1},
1882         {.input = &tegra_clk_audio,     .value = 2},
1883         {.input = &tegra_clk_m,     .value = 3},
1884         {.input = &tegra_clk_32k,   .value = 4},
1885         { 0, 0},
1886 };
1887
1888 static struct clk_mux_sel mux_pllp_pllc_pllm[] = {
1889         {.input = &tegra_pll_p,     .value = 0},
1890         {.input = &tegra_pll_c,     .value = 1},
1891         {.input = &tegra_pll_m,     .value = 2},
1892         { 0, 0},
1893 };
1894
1895 static struct clk_mux_sel mux_clk_m[] = {
1896         { .input = &tegra_clk_m, .value = 0},
1897         { 0, 0},
1898 };
1899
1900 static struct clk_mux_sel mux_pllp_out3[] = {
1901         { .input = &tegra_pll_p_out3, .value = 0},
1902         { 0, 0},
1903 };
1904
1905 static struct clk_mux_sel mux_plld[] = {
1906         { .input = &tegra_pll_d, .value = 0},
1907         { 0, 0},
1908 };
1909
1910 static struct clk_mux_sel mux_clk_32k[] = {
1911         { .input = &tegra_clk_32k, .value = 0},
1912         { 0, 0},
1913 };
1914
1915 static struct clk tegra_clk_emc = {
1916         .name = "emc",
1917         .ops = &tegra_emc_clk_ops,
1918         .reg = 0x19c,
1919         .max_rate = 800000000,
1920         .inputs = mux_pllm_pllc_pllp_clkm,
1921         .flags = MUX | DIV_U71 | PERIPH_EMC_ENB,
1922         .u.periph = {
1923                 .clk_num = 57,
1924         },
1925 };
1926
1927 #define PERIPH_CLK(_name, _dev, _con, _clk_num, _reg, _max, _inputs, _flags) \
1928         {                                               \
1929                 .name      = _name,                     \
1930                 .lookup    = {                          \
1931                         .dev_id    = _dev,              \
1932                         .con_id    = _con,              \
1933                 },                                      \
1934                 .ops       = &tegra_periph_clk_ops,     \
1935                 .reg       = _reg,                      \
1936                 .inputs    = _inputs,                   \
1937                 .flags     = _flags,                    \
1938                 .max_rate  = _max,                      \
1939                 .u.periph = {                           \
1940                         .clk_num   = _clk_num,          \
1941                 },                                      \
1942         }
1943
1944 #define SHARED_CLK(_name, _dev, _con, _parent)          \
1945         {                                               \
1946                 .name      = _name,                     \
1947                 .lookup    = {                          \
1948                         .dev_id    = _dev,              \
1949                         .con_id    = _con,              \
1950                 },                                      \
1951                 .ops       = &tegra_clk_shared_bus_ops, \
1952                 .parent = _parent,                      \
1953         }
1954
1955 struct clk tegra_list_clks[] = {
1956         PERIPH_CLK("rtc",       "rtc-tegra",            NULL,   4,      0,      32768,     mux_clk_32k,                 PERIPH_NO_RESET),
1957         PERIPH_CLK("timer",     "timer",                NULL,   5,      0,      26000000,  mux_clk_m,                   0),
1958         PERIPH_CLK("kfuse",     "kfuse-tegra",          NULL,   40,     0,      26000000,  mux_clk_m,                   0),
1959         PERIPH_CLK("i2s1",      "i2s.0",                NULL,   11,     0x100,  26000000,  mux_pllaout0_audio2x_pllp_clkm,      MUX | DIV_U71),
1960         PERIPH_CLK("i2s2",      "i2s.1",                NULL,   18,     0x104,  26000000,  mux_pllaout0_audio2x_pllp_clkm,      MUX | DIV_U71),
1961         PERIPH_CLK("spdif_out", "spdif_out",            NULL,   10,     0x108,  100000000, mux_pllaout0_audio2x_pllp_clkm,      MUX | DIV_U71),
1962         PERIPH_CLK("spdif_in",  "spdif_in",             NULL,   10,     0x10c,  100000000, mux_pllp_pllc_pllm,          MUX | DIV_U71),
1963         PERIPH_CLK("pwm",       "pwm",                  NULL,   17,     0x110,  432000000, mux_pllp_pllc_audio_clkm_clk32,      MUX | DIV_U71),
1964         PERIPH_CLK("spi",       "spi",                  NULL,   43,     0x114,  40000000,  mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71),
1965         PERIPH_CLK("xio",       "xio",                  NULL,   45,     0x120,  150000000, mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71),
1966         PERIPH_CLK("twc",       "twc",                  NULL,   16,     0x12c,  150000000, mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71),
1967         PERIPH_CLK("sbc1",      "spi_tegra.0",          NULL,   41,     0x134,  160000000, mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71),
1968         PERIPH_CLK("sbc2",      "spi_tegra.1",          NULL,   44,     0x118,  160000000, mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71),
1969         PERIPH_CLK("sbc3",      "spi_tegra.2",          NULL,   46,     0x11c,  160000000, mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71),
1970         PERIPH_CLK("sbc4",      "spi_tegra.3",          NULL,   68,     0x1b4,  160000000, mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71),
1971         PERIPH_CLK("ide",       "ide",                  NULL,   25,     0x144,  100000000, mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71), /* requires min voltage */
1972         PERIPH_CLK("ndflash",   "tegra_nand",           NULL,   13,     0x160,  164000000, mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71), /* scales with voltage */
1973         PERIPH_CLK("vfir",      "vfir",                 NULL,   7,      0x168,  72000000,  mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71),
1974         PERIPH_CLK("sdmmc1",    "sdhci-tegra.0",        NULL,   14,     0x150,  52000000,  mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71), /* scales with voltage */
1975         PERIPH_CLK("sdmmc2",    "sdhci-tegra.1",        NULL,   9,      0x154,  52000000,  mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71), /* scales with voltage */
1976         PERIPH_CLK("sdmmc3",    "sdhci-tegra.2",        NULL,   69,     0x1bc,  52000000,  mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71), /* scales with voltage */
1977         PERIPH_CLK("sdmmc4",    "sdhci-tegra.3",        NULL,   15,     0x164,  52000000,  mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71), /* scales with voltage */
1978         PERIPH_CLK("vcp",       "tegra-avp",            "vcp",  29,     0,      250000000, mux_clk_m,                   0),
1979         PERIPH_CLK("bsea",      "tegra-avp",            "bsea", 62,     0,      250000000, mux_clk_m,                   0),
1980         PERIPH_CLK("bsev",      "tegra-aes",            "bsev", 63,     0,      250000000, mux_clk_m,                   0),
1981         PERIPH_CLK("vde",       "tegra-avp",            "vde",  61,     0x1c8,  250000000, mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71), /* scales with voltage and process_id */
1982         PERIPH_CLK("csite",     "csite",                NULL,   73,     0x1d4,  144000000, mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71), /* max rate ??? */
1983         /* FIXME: what is la? */
1984         PERIPH_CLK("la",        "la",                   NULL,   76,     0x1f8,  26000000,  mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71),
1985         PERIPH_CLK("owr",       "tegra_w1",             NULL,   71,     0x1cc,  26000000,  mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71),
1986         PERIPH_CLK("nor",       "nor",                  NULL,   42,     0x1d0,  92000000,  mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71), /* requires min voltage */
1987         PERIPH_CLK("mipi",      "mipi",                 NULL,   50,     0x174,  60000000,  mux_pllp_pllc_pllm_clkm,     MUX | DIV_U71), /* scales with voltage */
1988         PERIPH_CLK("i2c1",      "tegra-i2c.0",          NULL,   12,     0x124,  26000000,  mux_pllp_pllc_pllm_clkm,     MUX | DIV_U16),
1989         PERIPH_CLK("i2c2",      "tegra-i2c.1",          NULL,   54,     0x198,  26000000,  mux_pllp_pllc_pllm_clkm,     MUX | DIV_U16),
1990         PERIPH_CLK("i2c3",      "tegra-i2c.2",          NULL,   67,     0x1b8,  26000000,  mux_pllp_pllc_pllm_clkm,     MUX | DIV_U16),
1991         PERIPH_CLK("dvc",       "tegra-i2c.3",          NULL,   47,     0x128,  26000000,  mux_pllp_pllc_pllm_clkm,     MUX | DIV_U16),
1992         PERIPH_CLK("i2c1_i2c",  "tegra-i2c.0",          "i2c",  0,      0,      72000000,  mux_pllp_out3,                       0),
1993         PERIPH_CLK("i2c2_i2c",  "tegra-i2c.1",          "i2c",  0,      0,      72000000,  mux_pllp_out3,                       0),
1994         PERIPH_CLK("i2c3_i2c",  "tegra-i2c.2",          "i2c",  0,      0,      72000000,  mux_pllp_out3,                       0),
1995         PERIPH_CLK("dvc_i2c",   "tegra-i2c.3",          "i2c",  0,      0,      72000000,  mux_pllp_out3,                       0),
1996         PERIPH_CLK("uarta",     "uart.0",               NULL,   6,      0x178,  600000000, mux_pllp_pllc_pllm_clkm,     MUX),
1997         PERIPH_CLK("uartb",     "uart.1",               NULL,   7,      0x17c,  600000000, mux_pllp_pllc_pllm_clkm,     MUX),
1998         PERIPH_CLK("uartc",     "uart.2",               NULL,   55,     0x1a0,  600000000, mux_pllp_pllc_pllm_clkm,     MUX),
1999         PERIPH_CLK("uartd",     "uart.3",               NULL,   65,     0x1c0,  600000000, mux_pllp_pllc_pllm_clkm,     MUX),
2000         PERIPH_CLK("uarte",     "uart.4",               NULL,   66,     0x1c4,  600000000, mux_pllp_pllc_pllm_clkm,     MUX),
2001         PERIPH_CLK("3d",        "3d",                   NULL,   24,     0x158,  300000000, mux_pllm_pllc_pllp_plla,     MUX | DIV_U71 | PERIPH_MANUAL_RESET), /* scales with voltage and process_id */
2002         PERIPH_CLK("2d",        "2d",                   NULL,   21,     0x15c,  300000000, mux_pllm_pllc_pllp_plla,     MUX | DIV_U71), /* scales with voltage and process_id */
2003         PERIPH_CLK("vi",        "tegra_camera",         "vi",   20,     0x148,  150000000, mux_pllm_pllc_pllp_plla,     MUX | DIV_U71), /* scales with voltage and process_id */
2004         PERIPH_CLK("vi_sensor", "tegra_camera",         "vi_sensor",    20,     0x1a8,  150000000, mux_pllm_pllc_pllp_plla,     MUX | DIV_U71 | PERIPH_NO_RESET), /* scales with voltage and process_id */
2005         PERIPH_CLK("epp",       "epp",                  NULL,   19,     0x16c,  300000000, mux_pllm_pllc_pllp_plla,     MUX | DIV_U71), /* scales with voltage and process_id */
2006         PERIPH_CLK("mpe",       "mpe",                  NULL,   60,     0x170,  250000000, mux_pllm_pllc_pllp_plla,     MUX | DIV_U71), /* scales with voltage and process_id */
2007         PERIPH_CLK("host1x",    "host1x",               NULL,   28,     0x180,  166000000, mux_pllm_pllc_pllp_plla,     MUX | DIV_U71), /* scales with voltage and process_id */
2008         PERIPH_CLK("cve",       "cve",                  NULL,   49,     0x140,  250000000, mux_pllp_plld_pllc_clkm,     MUX | DIV_U71), /* requires min voltage */
2009         PERIPH_CLK("tvo",       "tvo",                  NULL,   49,     0x188,  250000000, mux_pllp_plld_pllc_clkm,     MUX | DIV_U71), /* requires min voltage */
2010         PERIPH_CLK("hdmi",      "hdmi",                 NULL,   51,     0x18c,  600000000, mux_pllp_plld_pllc_clkm,     MUX | DIV_U71), /* requires min voltage */
2011         PERIPH_CLK("tvdac",     "tvdac",                NULL,   53,     0x194,  250000000, mux_pllp_plld_pllc_clkm,     MUX | DIV_U71), /* requires min voltage */
2012         PERIPH_CLK("disp1",     "tegradc.0",            NULL,   27,     0x138,  600000000, mux_pllp_plld_pllc_clkm,     MUX | DIV_U71), /* scales with voltage and process_id */
2013         PERIPH_CLK("disp2",     "tegradc.1",            NULL,   26,     0x13c,  600000000, mux_pllp_plld_pllc_clkm,     MUX | DIV_U71), /* scales with voltage and process_id */
2014         PERIPH_CLK("usbd",      "fsl-tegra-udc",        NULL,   22,     0,      480000000, mux_clk_m,                   0), /* requires min voltage */
2015         PERIPH_CLK("usb2",      "tegra-ehci.1",         NULL,   58,     0,      480000000, mux_clk_m,                   0), /* requires min voltage */
2016         PERIPH_CLK("usb3",      "tegra-ehci.2",         NULL,   59,     0,      480000000, mux_clk_m,                   0), /* requires min voltage */
2017         PERIPH_CLK("dsi",       "dsi",                  NULL,   48,     0,      500000000, mux_plld,                    0), /* scales with voltage */
2018         PERIPH_CLK("csi",       "tegra_camera",         "csi",  52,     0,      72000000,  mux_pllp_out3,               0),
2019         PERIPH_CLK("isp",       "tegra_camera",         "isp",  23,     0,      150000000, mux_clk_m,                   0), /* same frequency as VI */
2020         PERIPH_CLK("csus",      "tegra_camera",         "csus", 92,     0,      150000000, mux_clk_m,                   PERIPH_NO_RESET),
2021
2022         SHARED_CLK("avp.sclk",  "tegra-avp",            "sclk", &tegra_clk_sclk),
2023         SHARED_CLK("avp.emc",   "tegra-avp",            "emc",  &tegra_clk_emc),
2024         SHARED_CLK("cpu.emc",   "cpu",                  "emc",  &tegra_clk_emc),
2025         SHARED_CLK("disp1.emc", "tegradc.0",            "emc",  &tegra_clk_emc),
2026         SHARED_CLK("disp2.emc", "tegradc.1",            "emc",  &tegra_clk_emc),
2027         SHARED_CLK("hdmi.emc",  "hdmi",                 "emc",  &tegra_clk_emc),
2028         SHARED_CLK("host.emc",  "tegra_grhost",         "emc",  &tegra_clk_emc),
2029         SHARED_CLK("usbd.emc",  "fsl-tegra-udc",        "emc",  &tegra_clk_emc),
2030         SHARED_CLK("usb1.emc",  "tegra-ehci.0",         "emc",  &tegra_clk_emc),
2031         SHARED_CLK("usb2.emc",  "tegra-ehci.1",         "emc",  &tegra_clk_emc),
2032         SHARED_CLK("usb3.emc",  "tegra-ehci.2",         "emc",  &tegra_clk_emc),
2033 };
2034
2035 #define CLK_DUPLICATE(_name, _dev, _con)                \
2036         {                                               \
2037                 .name   = _name,                        \
2038                 .lookup = {                             \
2039                         .dev_id = _dev,                 \
2040                         .con_id         = _con,         \
2041                 },                                      \
2042         }
2043
2044 /* Some clocks may be used by different drivers depending on the board
2045  * configuration.  List those here to register them twice in the clock lookup
2046  * table under two names.
2047  */
2048 struct clk_duplicate tegra_clk_duplicates[] = {
2049         CLK_DUPLICATE("uarta",  "tegra_uart.0", NULL),
2050         CLK_DUPLICATE("uartb",  "tegra_uart.1", NULL),
2051         CLK_DUPLICATE("uartc",  "tegra_uart.2", NULL),
2052         CLK_DUPLICATE("uartd",  "tegra_uart.3", NULL),
2053         CLK_DUPLICATE("uarte",  "tegra_uart.4", NULL),
2054         CLK_DUPLICATE("usbd", "utmip-pad", NULL),
2055         CLK_DUPLICATE("usbd", "tegra-ehci.0", NULL),
2056         CLK_DUPLICATE("usbd", "tegra-otg", NULL),
2057         CLK_DUPLICATE("hdmi", "tegradc.0", "hdmi"),
2058         CLK_DUPLICATE("hdmi", "tegradc.1", "hdmi"),
2059         CLK_DUPLICATE("pwm", "tegra_pwm.0", NULL),
2060         CLK_DUPLICATE("pwm", "tegra_pwm.1", NULL),
2061         CLK_DUPLICATE("pwm", "tegra_pwm.2", NULL),
2062         CLK_DUPLICATE("pwm", "tegra_pwm.3", NULL),
2063         CLK_DUPLICATE("host1x", "tegra_grhost", "host1x"),
2064         CLK_DUPLICATE("2d", "tegra_grhost", "gr2d"),
2065         CLK_DUPLICATE("3d", "tegra_grhost", "gr3d"),
2066         CLK_DUPLICATE("epp", "tegra_grhost", "epp"),
2067         CLK_DUPLICATE("mpe", "tegra_grhost", "mpe"),
2068         CLK_DUPLICATE("cop", "tegra-avp", "cop"),
2069         CLK_DUPLICATE("vde", "tegra-aes", "vde"),
2070 };
2071
2072 #define CLK(dev, con, ck)       \
2073         {                       \
2074                 .dev_id = dev,  \
2075                 .con_id = con,  \
2076                 .clk = ck,      \
2077         }
2078
2079 struct clk *tegra_ptr_clks[] = {
2080         &tegra_clk_32k,
2081         &tegra_pll_s,
2082         &tegra_clk_m,
2083         &tegra_pll_m,
2084         &tegra_pll_m_out1,
2085         &tegra_pll_c,
2086         &tegra_pll_c_out1,
2087         &tegra_pll_p,
2088         &tegra_pll_p_out1,
2089         &tegra_pll_p_out2,
2090         &tegra_pll_p_out3,
2091         &tegra_pll_p_out4,
2092         &tegra_pll_a,
2093         &tegra_pll_a_out0,
2094         &tegra_pll_d,
2095         &tegra_pll_d_out0,
2096         &tegra_pll_u,
2097         &tegra_pll_x,
2098         &tegra_clk_cclk,
2099         &tegra_clk_sclk,
2100         &tegra_clk_hclk,
2101         &tegra_clk_pclk,
2102         &tegra_clk_d,
2103         &tegra_dev1_clk,
2104         &tegra_dev2_clk,
2105         &tegra_clk_virtual_cpu,
2106         &tegra_clk_blink,
2107         &tegra_clk_cop,
2108         &tegra_clk_emc,
2109 };
2110
2111 static void tegra2_init_one_clock(struct clk *c)
2112 {
2113         clk_init(c);
2114         INIT_LIST_HEAD(&c->shared_bus_list);
2115         if (!c->lookup.dev_id && !c->lookup.con_id)
2116                 c->lookup.con_id = c->name;
2117         c->lookup.clk = c;
2118         clkdev_add(&c->lookup);
2119 }
2120
2121 void __init tegra2_init_clocks(void)
2122 {
2123         int i;
2124         struct clk *c;
2125
2126         for (i = 0; i < ARRAY_SIZE(tegra_ptr_clks); i++)
2127                 tegra2_init_one_clock(tegra_ptr_clks[i]);
2128
2129         for (i = 0; i < ARRAY_SIZE(tegra_list_clks); i++)
2130                 tegra2_init_one_clock(&tegra_list_clks[i]);
2131
2132         for (i = 0; i < ARRAY_SIZE(tegra_clk_duplicates); i++) {
2133                 c = tegra_get_clock_by_name(tegra_clk_duplicates[i].name);
2134                 if (!c) {
2135                         pr_err("%s: Unknown duplicate clock %s\n", __func__,
2136                                 tegra_clk_duplicates[i].name);
2137                         continue;
2138                 }
2139
2140                 tegra_clk_duplicates[i].lookup.clk = c;
2141                 clkdev_add(&tegra_clk_duplicates[i].lookup);
2142         }
2143
2144         init_audio_sync_clock_mux();
2145 }
2146
2147 #ifdef CONFIG_PM
2148 static u32 clk_rst_suspend[RST_DEVICES_NUM + CLK_OUT_ENB_NUM +
2149                            PERIPH_CLK_SOURCE_NUM + 22];
2150
2151 void tegra_clk_suspend(void)
2152 {
2153         unsigned long off, i;
2154         u32 *ctx = clk_rst_suspend;
2155
2156         *ctx++ = clk_readl(OSC_CTRL) & OSC_CTRL_MASK;
2157         *ctx++ = clk_readl(tegra_pll_c.reg + PLL_BASE);
2158         *ctx++ = clk_readl(tegra_pll_c.reg + PLL_MISC(&tegra_pll_c));
2159         *ctx++ = clk_readl(tegra_pll_a.reg + PLL_BASE);
2160         *ctx++ = clk_readl(tegra_pll_a.reg + PLL_MISC(&tegra_pll_a));
2161         *ctx++ = clk_readl(tegra_pll_s.reg + PLL_BASE);
2162         *ctx++ = clk_readl(tegra_pll_s.reg + PLL_MISC(&tegra_pll_s));
2163         *ctx++ = clk_readl(tegra_pll_d.reg + PLL_BASE);
2164         *ctx++ = clk_readl(tegra_pll_d.reg + PLL_MISC(&tegra_pll_d));
2165         *ctx++ = clk_readl(tegra_pll_u.reg + PLL_BASE);
2166         *ctx++ = clk_readl(tegra_pll_u.reg + PLL_MISC(&tegra_pll_u));
2167
2168         *ctx++ = clk_readl(tegra_pll_m_out1.reg);
2169         *ctx++ = clk_readl(tegra_pll_a_out0.reg);
2170         *ctx++ = clk_readl(tegra_pll_c_out1.reg);
2171
2172         *ctx++ = clk_readl(tegra_clk_cclk.reg);
2173         *ctx++ = clk_readl(tegra_clk_cclk.reg + SUPER_CLK_DIVIDER);
2174
2175         *ctx++ = clk_readl(tegra_clk_sclk.reg);
2176         *ctx++ = clk_readl(tegra_clk_sclk.reg + SUPER_CLK_DIVIDER);
2177         *ctx++ = clk_readl(tegra_clk_pclk.reg);
2178
2179         *ctx++ = clk_readl(tegra_clk_audio.reg);
2180
2181         for (off = PERIPH_CLK_SOURCE_I2S1; off <= PERIPH_CLK_SOURCE_OSC;
2182                         off += 4) {
2183                 if (off == PERIPH_CLK_SOURCE_EMC)
2184                         continue;
2185                 *ctx++ = clk_readl(off);
2186         }
2187
2188         off = RST_DEVICES;
2189         for (i = 0; i < RST_DEVICES_NUM; i++, off += 4)
2190                 *ctx++ = clk_readl(off);
2191
2192         off = CLK_OUT_ENB;
2193         for (i = 0; i < CLK_OUT_ENB_NUM; i++, off += 4)
2194                 *ctx++ = clk_readl(off);
2195
2196         *ctx++ = clk_readl(MISC_CLK_ENB);
2197         *ctx++ = clk_readl(CLK_MASK_ARM);
2198
2199         BUG_ON(ctx - clk_rst_suspend != ARRAY_SIZE(clk_rst_suspend));
2200 }
2201
2202 void tegra_clk_resume(void)
2203 {
2204         unsigned long off, i;
2205         const u32 *ctx = clk_rst_suspend;
2206         u32 val;
2207
2208         val = clk_readl(OSC_CTRL) & ~OSC_CTRL_MASK;
2209         val |= *ctx++;
2210         clk_writel(val, OSC_CTRL);
2211
2212         clk_writel(*ctx++, tegra_pll_c.reg + PLL_BASE);
2213         clk_writel(*ctx++, tegra_pll_c.reg + PLL_MISC(&tegra_pll_c));
2214         clk_writel(*ctx++, tegra_pll_a.reg + PLL_BASE);
2215         clk_writel(*ctx++, tegra_pll_a.reg + PLL_MISC(&tegra_pll_a));
2216         clk_writel(*ctx++, tegra_pll_s.reg + PLL_BASE);
2217         clk_writel(*ctx++, tegra_pll_s.reg + PLL_MISC(&tegra_pll_s));
2218         clk_writel(*ctx++, tegra_pll_d.reg + PLL_BASE);
2219         clk_writel(*ctx++, tegra_pll_d.reg + PLL_MISC(&tegra_pll_d));
2220         clk_writel(*ctx++, tegra_pll_u.reg + PLL_BASE);
2221         clk_writel(*ctx++, tegra_pll_u.reg + PLL_MISC(&tegra_pll_u));
2222         udelay(1000);
2223
2224         clk_writel(*ctx++, tegra_pll_m_out1.reg);
2225         clk_writel(*ctx++, tegra_pll_a_out0.reg);
2226         clk_writel(*ctx++, tegra_pll_c_out1.reg);
2227
2228         clk_writel(*ctx++, tegra_clk_cclk.reg);
2229         clk_writel(*ctx++, tegra_clk_cclk.reg + SUPER_CLK_DIVIDER);
2230
2231         clk_writel(*ctx++, tegra_clk_sclk.reg);
2232         clk_writel(*ctx++, tegra_clk_sclk.reg + SUPER_CLK_DIVIDER);
2233         clk_writel(*ctx++, tegra_clk_pclk.reg);
2234
2235         clk_writel(*ctx++, tegra_clk_audio.reg);
2236
2237         /* enable all clocks before configuring clock sources */
2238         clk_writel(0xbffffff9ul, CLK_OUT_ENB);
2239         clk_writel(0xfefffff7ul, CLK_OUT_ENB + 4);
2240         clk_writel(0x77f01bfful, CLK_OUT_ENB + 8);
2241         wmb();
2242
2243         for (off = PERIPH_CLK_SOURCE_I2S1; off <= PERIPH_CLK_SOURCE_OSC;
2244                         off += 4) {
2245                 if (off == PERIPH_CLK_SOURCE_EMC)
2246                         continue;
2247                 clk_writel(*ctx++, off);
2248         }
2249         wmb();
2250
2251         off = RST_DEVICES;
2252         for (i = 0; i < RST_DEVICES_NUM; i++, off += 4)
2253                 clk_writel(*ctx++, off);
2254         wmb();
2255
2256         off = CLK_OUT_ENB;
2257         for (i = 0; i < CLK_OUT_ENB_NUM; i++, off += 4)
2258                 clk_writel(*ctx++, off);
2259         wmb();
2260
2261         clk_writel(*ctx++, MISC_CLK_ENB);
2262         clk_writel(*ctx++, CLK_MASK_ARM);
2263 }
2264 #endif