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