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