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