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