aboutsummaryrefslogtreecommitdiffstats
path: root/arch/arm/mach-tegra/tegra20_clocks.c
diff options
context:
space:
mode:
Diffstat (limited to 'arch/arm/mach-tegra/tegra20_clocks.c')
-rw-r--r--arch/arm/mach-tegra/tegra20_clocks.c1625
1 files changed, 1625 insertions, 0 deletions
diff --git a/arch/arm/mach-tegra/tegra20_clocks.c b/arch/arm/mach-tegra/tegra20_clocks.c
new file mode 100644
index 000000000000..9273b0dffc66
--- /dev/null
+++ b/arch/arm/mach-tegra/tegra20_clocks.c
@@ -0,0 +1,1625 @@
1/*
2 * arch/arm/mach-tegra/tegra20_clocks.c
3 *
4 * Copyright (C) 2010 Google, Inc.
5 * Copyright (c) 2010-2012 NVIDIA CORPORATION. All rights reserved.
6 *
7 * Author:
8 * Colin Cross <ccross@google.com>
9 *
10 * This software is licensed under the terms of the GNU General Public
11 * License version 2, as published by the Free Software Foundation, and
12 * may be copied, distributed, and modified under those terms.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 */
20
21#include <linux/kernel.h>
22#include <linux/module.h>
23#include <linux/list.h>
24#include <linux/spinlock.h>
25#include <linux/delay.h>
26#include <linux/io.h>
27#include <linux/clkdev.h>
28#include <linux/clk.h>
29
30#include <mach/iomap.h>
31#include <mach/suspend.h>
32
33#include "clock.h"
34#include "fuse.h"
35#include "tegra2_emc.h"
36#include "tegra_cpu_car.h"
37
38#define RST_DEVICES 0x004
39#define RST_DEVICES_SET 0x300
40#define RST_DEVICES_CLR 0x304
41#define RST_DEVICES_NUM 3
42
43#define CLK_OUT_ENB 0x010
44#define CLK_OUT_ENB_SET 0x320
45#define CLK_OUT_ENB_CLR 0x324
46#define CLK_OUT_ENB_NUM 3
47
48#define CLK_MASK_ARM 0x44
49#define MISC_CLK_ENB 0x48
50
51#define OSC_CTRL 0x50
52#define OSC_CTRL_OSC_FREQ_MASK (3<<30)
53#define OSC_CTRL_OSC_FREQ_13MHZ (0<<30)
54#define OSC_CTRL_OSC_FREQ_19_2MHZ (1<<30)
55#define OSC_CTRL_OSC_FREQ_12MHZ (2<<30)
56#define OSC_CTRL_OSC_FREQ_26MHZ (3<<30)
57#define OSC_CTRL_MASK (0x3f2 | OSC_CTRL_OSC_FREQ_MASK)
58
59#define OSC_FREQ_DET 0x58
60#define OSC_FREQ_DET_TRIG (1<<31)
61
62#define OSC_FREQ_DET_STATUS 0x5C
63#define OSC_FREQ_DET_BUSY (1<<31)
64#define OSC_FREQ_DET_CNT_MASK 0xFFFF
65
66#define PERIPH_CLK_SOURCE_I2S1 0x100
67#define PERIPH_CLK_SOURCE_EMC 0x19c
68#define PERIPH_CLK_SOURCE_OSC 0x1fc
69#define PERIPH_CLK_SOURCE_NUM \
70 ((PERIPH_CLK_SOURCE_OSC - PERIPH_CLK_SOURCE_I2S1) / 4)
71
72#define PERIPH_CLK_SOURCE_MASK (3<<30)
73#define PERIPH_CLK_SOURCE_SHIFT 30
74#define PERIPH_CLK_SOURCE_PWM_MASK (7<<28)
75#define PERIPH_CLK_SOURCE_PWM_SHIFT 28
76#define PERIPH_CLK_SOURCE_ENABLE (1<<28)
77#define PERIPH_CLK_SOURCE_DIVU71_MASK 0xFF
78#define PERIPH_CLK_SOURCE_DIVU16_MASK 0xFFFF
79#define PERIPH_CLK_SOURCE_DIV_SHIFT 0
80
81#define SDMMC_CLK_INT_FB_SEL (1 << 23)
82#define SDMMC_CLK_INT_FB_DLY_SHIFT 16
83#define SDMMC_CLK_INT_FB_DLY_MASK (0xF << SDMMC_CLK_INT_FB_DLY_SHIFT)
84
85#define PLL_BASE 0x0
86#define PLL_BASE_BYPASS (1<<31)
87#define PLL_BASE_ENABLE (1<<30)
88#define PLL_BASE_REF_ENABLE (1<<29)
89#define PLL_BASE_OVERRIDE (1<<28)
90#define PLL_BASE_DIVP_MASK (0x7<<20)
91#define PLL_BASE_DIVP_SHIFT 20
92#define PLL_BASE_DIVN_MASK (0x3FF<<8)
93#define PLL_BASE_DIVN_SHIFT 8
94#define PLL_BASE_DIVM_MASK (0x1F)
95#define PLL_BASE_DIVM_SHIFT 0
96
97#define PLL_OUT_RATIO_MASK (0xFF<<8)
98#define PLL_OUT_RATIO_SHIFT 8
99#define PLL_OUT_OVERRIDE (1<<2)
100#define PLL_OUT_CLKEN (1<<1)
101#define PLL_OUT_RESET_DISABLE (1<<0)
102
103#define PLL_MISC(c) (((c)->flags & PLL_ALT_MISC_REG) ? 0x4 : 0xc)
104
105#define PLL_MISC_DCCON_SHIFT 20
106#define PLL_MISC_CPCON_SHIFT 8
107#define PLL_MISC_CPCON_MASK (0xF<<PLL_MISC_CPCON_SHIFT)
108#define PLL_MISC_LFCON_SHIFT 4
109#define PLL_MISC_LFCON_MASK (0xF<<PLL_MISC_LFCON_SHIFT)
110#define PLL_MISC_VCOCON_SHIFT 0
111#define PLL_MISC_VCOCON_MASK (0xF<<PLL_MISC_VCOCON_SHIFT)
112
113#define PLLU_BASE_POST_DIV (1<<20)
114
115#define PLLD_MISC_CLKENABLE (1<<30)
116#define PLLD_MISC_DIV_RST (1<<23)
117#define PLLD_MISC_DCCON_SHIFT 12
118
119#define PLLE_MISC_READY (1 << 15)
120
121#define PERIPH_CLK_TO_ENB_REG(c) ((c->u.periph.clk_num / 32) * 4)
122#define PERIPH_CLK_TO_ENB_SET_REG(c) ((c->u.periph.clk_num / 32) * 8)
123#define PERIPH_CLK_TO_ENB_BIT(c) (1 << (c->u.periph.clk_num % 32))
124
125#define SUPER_CLK_MUX 0x00
126#define SUPER_STATE_SHIFT 28
127#define SUPER_STATE_MASK (0xF << SUPER_STATE_SHIFT)
128#define SUPER_STATE_STANDBY (0x0 << SUPER_STATE_SHIFT)
129#define SUPER_STATE_IDLE (0x1 << SUPER_STATE_SHIFT)
130#define SUPER_STATE_RUN (0x2 << SUPER_STATE_SHIFT)
131#define SUPER_STATE_IRQ (0x3 << SUPER_STATE_SHIFT)
132#define SUPER_STATE_FIQ (0x4 << SUPER_STATE_SHIFT)
133#define SUPER_SOURCE_MASK 0xF
134#define SUPER_FIQ_SOURCE_SHIFT 12
135#define SUPER_IRQ_SOURCE_SHIFT 8
136#define SUPER_RUN_SOURCE_SHIFT 4
137#define SUPER_IDLE_SOURCE_SHIFT 0
138
139#define SUPER_CLK_DIVIDER 0x04
140
141#define BUS_CLK_DISABLE (1<<3)
142#define BUS_CLK_DIV_MASK 0x3
143
144#define PMC_CTRL 0x0
145 #define PMC_CTRL_BLINK_ENB (1 << 7)
146
147#define PMC_DPD_PADS_ORIDE 0x1c
148 #define PMC_DPD_PADS_ORIDE_BLINK_ENB (1 << 20)
149
150#define PMC_BLINK_TIMER_DATA_ON_SHIFT 0
151#define PMC_BLINK_TIMER_DATA_ON_MASK 0x7fff
152#define PMC_BLINK_TIMER_ENB (1 << 15)
153#define PMC_BLINK_TIMER_DATA_OFF_SHIFT 16
154#define PMC_BLINK_TIMER_DATA_OFF_MASK 0xffff
155
156/* Tegra CPU clock and reset control regs */
157#define TEGRA_CLK_RST_CONTROLLER_CLK_CPU_CMPLX 0x4c
158#define TEGRA_CLK_RST_CONTROLLER_RST_CPU_CMPLX_SET 0x340
159#define TEGRA_CLK_RST_CONTROLLER_RST_CPU_CMPLX_CLR 0x344
160
161#define CPU_CLOCK(cpu) (0x1 << (8 + cpu))
162#define CPU_RESET(cpu) (0x1111ul << (cpu))
163
164static void __iomem *reg_clk_base = IO_ADDRESS(TEGRA_CLK_RESET_BASE);
165static void __iomem *reg_pmc_base = IO_ADDRESS(TEGRA_PMC_BASE);
166
167/*
168 * Some clocks share a register with other clocks. Any clock op that
169 * non-atomically modifies a register used by another clock must lock
170 * clock_register_lock first.
171 */
172static DEFINE_SPINLOCK(clock_register_lock);
173
174/*
175 * Some peripheral clocks share an enable bit, so refcount the enable bits
176 * in registers CLK_ENABLE_L, CLK_ENABLE_H, and CLK_ENABLE_U
177 */
178static int tegra_periph_clk_enable_refcount[3 * 32];
179
180#define clk_writel(value, reg) \
181 __raw_writel(value, reg_clk_base + (reg))
182#define clk_readl(reg) \
183 __raw_readl(reg_clk_base + (reg))
184#define pmc_writel(value, reg) \
185 __raw_writel(value, reg_pmc_base + (reg))
186#define pmc_readl(reg) \
187 __raw_readl(reg_pmc_base + (reg))
188
189static unsigned long clk_measure_input_freq(void)
190{
191 u32 clock_autodetect;
192 clk_writel(OSC_FREQ_DET_TRIG | 1, OSC_FREQ_DET);
193 do {} while (clk_readl(OSC_FREQ_DET_STATUS) & OSC_FREQ_DET_BUSY);
194 clock_autodetect = clk_readl(OSC_FREQ_DET_STATUS);
195 if (clock_autodetect >= 732 - 3 && clock_autodetect <= 732 + 3) {
196 return 12000000;
197 } else if (clock_autodetect >= 794 - 3 && clock_autodetect <= 794 + 3) {
198 return 13000000;
199 } else if (clock_autodetect >= 1172 - 3 && clock_autodetect <= 1172 + 3) {
200 return 19200000;
201 } else if (clock_autodetect >= 1587 - 3 && clock_autodetect <= 1587 + 3) {
202 return 26000000;
203 } else {
204 pr_err("%s: Unexpected clock autodetect value %d",
205 __func__, clock_autodetect);
206 BUG();
207 return 0;
208 }
209}
210
211static int clk_div71_get_divider(unsigned long parent_rate, unsigned long rate)
212{
213 s64 divider_u71 = parent_rate * 2;
214 divider_u71 += rate - 1;
215 do_div(divider_u71, rate);
216
217 if (divider_u71 - 2 < 0)
218 return 0;
219
220 if (divider_u71 - 2 > 255)
221 return -EINVAL;
222
223 return divider_u71 - 2;
224}
225
226static int clk_div16_get_divider(unsigned long parent_rate, unsigned long rate)
227{
228 s64 divider_u16;
229
230 divider_u16 = parent_rate;
231 divider_u16 += rate - 1;
232 do_div(divider_u16, rate);
233
234 if (divider_u16 - 1 < 0)
235 return 0;
236
237 if (divider_u16 - 1 > 0xFFFF)
238 return -EINVAL;
239
240 return divider_u16 - 1;
241}
242
243static unsigned long tegra_clk_fixed_recalc_rate(struct clk_hw *hw,
244 unsigned long parent_rate)
245{
246 return to_clk_tegra(hw)->fixed_rate;
247}
248
249struct clk_ops tegra_clk_32k_ops = {
250 .recalc_rate = tegra_clk_fixed_recalc_rate,
251};
252
253/* clk_m functions */
254static unsigned long tegra20_clk_m_recalc_rate(struct clk_hw *hw,
255 unsigned long prate)
256{
257 if (!to_clk_tegra(hw)->fixed_rate)
258 to_clk_tegra(hw)->fixed_rate = clk_measure_input_freq();
259 return to_clk_tegra(hw)->fixed_rate;
260}
261
262static void tegra20_clk_m_init(struct clk_hw *hw)
263{
264 struct clk_tegra *c = to_clk_tegra(hw);
265 u32 osc_ctrl = clk_readl(OSC_CTRL);
266 u32 auto_clock_control = osc_ctrl & ~OSC_CTRL_OSC_FREQ_MASK;
267
268 switch (c->fixed_rate) {
269 case 12000000:
270 auto_clock_control |= OSC_CTRL_OSC_FREQ_12MHZ;
271 break;
272 case 13000000:
273 auto_clock_control |= OSC_CTRL_OSC_FREQ_13MHZ;
274 break;
275 case 19200000:
276 auto_clock_control |= OSC_CTRL_OSC_FREQ_19_2MHZ;
277 break;
278 case 26000000:
279 auto_clock_control |= OSC_CTRL_OSC_FREQ_26MHZ;
280 break;
281 default:
282 BUG();
283 }
284 clk_writel(auto_clock_control, OSC_CTRL);
285}
286
287struct clk_ops tegra_clk_m_ops = {
288 .init = tegra20_clk_m_init,
289 .recalc_rate = tegra20_clk_m_recalc_rate,
290};
291
292/* super clock functions */
293/* "super clocks" on tegra have two-stage muxes and a clock skipping
294 * super divider. We will ignore the clock skipping divider, since we
295 * can't lower the voltage when using the clock skip, but we can if we
296 * lower the PLL frequency.
297 */
298static int tegra20_super_clk_is_enabled(struct clk_hw *hw)
299{
300 struct clk_tegra *c = to_clk_tegra(hw);
301 u32 val;
302
303 val = clk_readl(c->reg + SUPER_CLK_MUX);
304 BUG_ON(((val & SUPER_STATE_MASK) != SUPER_STATE_RUN) &&
305 ((val & SUPER_STATE_MASK) != SUPER_STATE_IDLE));
306 c->state = ON;
307 return c->state;
308}
309
310static int tegra20_super_clk_enable(struct clk_hw *hw)
311{
312 struct clk_tegra *c = to_clk_tegra(hw);
313 clk_writel(0, c->reg + SUPER_CLK_DIVIDER);
314 return 0;
315}
316
317static void tegra20_super_clk_disable(struct clk_hw *hw)
318{
319 pr_debug("%s on clock %s\n", __func__, __clk_get_name(hw->clk));
320
321 /* oops - don't disable the CPU clock! */
322 BUG();
323}
324
325static u8 tegra20_super_clk_get_parent(struct clk_hw *hw)
326{
327 struct clk_tegra *c = to_clk_tegra(hw);
328 int val = clk_readl(c->reg + SUPER_CLK_MUX);
329 int source;
330 int shift;
331
332 BUG_ON(((val & SUPER_STATE_MASK) != SUPER_STATE_RUN) &&
333 ((val & SUPER_STATE_MASK) != SUPER_STATE_IDLE));
334 shift = ((val & SUPER_STATE_MASK) == SUPER_STATE_IDLE) ?
335 SUPER_IDLE_SOURCE_SHIFT : SUPER_RUN_SOURCE_SHIFT;
336 source = (val >> shift) & SUPER_SOURCE_MASK;
337 return source;
338}
339
340static int tegra20_super_clk_set_parent(struct clk_hw *hw, u8 index)
341{
342 struct clk_tegra *c = to_clk_tegra(hw);
343 u32 val = clk_readl(c->reg + SUPER_CLK_MUX);
344 int shift;
345
346 BUG_ON(((val & SUPER_STATE_MASK) != SUPER_STATE_RUN) &&
347 ((val & SUPER_STATE_MASK) != SUPER_STATE_IDLE));
348 shift = ((val & SUPER_STATE_MASK) == SUPER_STATE_IDLE) ?
349 SUPER_IDLE_SOURCE_SHIFT : SUPER_RUN_SOURCE_SHIFT;
350 val &= ~(SUPER_SOURCE_MASK << shift);
351 val |= index << shift;
352
353 clk_writel(val, c->reg);
354
355 return 0;
356}
357
358/* FIX ME: Need to switch parents to change the source PLL rate */
359static unsigned long tegra20_super_clk_recalc_rate(struct clk_hw *hw,
360 unsigned long prate)
361{
362 return prate;
363}
364
365static long tegra20_super_clk_round_rate(struct clk_hw *hw, unsigned long rate,
366 unsigned long *prate)
367{
368 return *prate;
369}
370
371static int tegra20_super_clk_set_rate(struct clk_hw *hw, unsigned long rate,
372 unsigned long parent_rate)
373{
374 return 0;
375}
376
377struct clk_ops tegra_super_ops = {
378 .is_enabled = tegra20_super_clk_is_enabled,
379 .enable = tegra20_super_clk_enable,
380 .disable = tegra20_super_clk_disable,
381 .set_parent = tegra20_super_clk_set_parent,
382 .get_parent = tegra20_super_clk_get_parent,
383 .set_rate = tegra20_super_clk_set_rate,
384 .round_rate = tegra20_super_clk_round_rate,
385 .recalc_rate = tegra20_super_clk_recalc_rate,
386};
387
388static unsigned long tegra20_twd_clk_recalc_rate(struct clk_hw *hw,
389 unsigned long parent_rate)
390{
391 struct clk_tegra *c = to_clk_tegra(hw);
392 u64 rate = parent_rate;
393
394 if (c->mul != 0 && c->div != 0) {
395 rate *= c->mul;
396 rate += c->div - 1; /* round up */
397 do_div(rate, c->div);
398 }
399
400 return rate;
401}
402
403struct clk_ops tegra_twd_ops = {
404 .recalc_rate = tegra20_twd_clk_recalc_rate,
405};
406
407static u8 tegra20_cop_clk_get_parent(struct clk_hw *hw)
408{
409 return 0;
410}
411
412struct clk_ops tegra_cop_ops = {
413 .get_parent = tegra20_cop_clk_get_parent,
414};
415
416/* virtual cop clock functions. Used to acquire the fake 'cop' clock to
417 * reset the COP block (i.e. AVP) */
418void tegra2_cop_clk_reset(struct clk_hw *hw, bool assert)
419{
420 unsigned long reg = assert ? RST_DEVICES_SET : RST_DEVICES_CLR;
421
422 pr_debug("%s %s\n", __func__, assert ? "assert" : "deassert");
423 clk_writel(1 << 1, reg);
424}
425
426/* bus clock functions */
427static int tegra20_bus_clk_is_enabled(struct clk_hw *hw)
428{
429 struct clk_tegra *c = to_clk_tegra(hw);
430 u32 val = clk_readl(c->reg);
431
432 c->state = ((val >> c->reg_shift) & BUS_CLK_DISABLE) ? OFF : ON;
433 return c->state;
434}
435
436static int tegra20_bus_clk_enable(struct clk_hw *hw)
437{
438 struct clk_tegra *c = to_clk_tegra(hw);
439 unsigned long flags;
440 u32 val;
441
442 spin_lock_irqsave(&clock_register_lock, flags);
443
444 val = clk_readl(c->reg);
445 val &= ~(BUS_CLK_DISABLE << c->reg_shift);
446 clk_writel(val, c->reg);
447
448 spin_unlock_irqrestore(&clock_register_lock, flags);
449
450 return 0;
451}
452
453static void tegra20_bus_clk_disable(struct clk_hw *hw)
454{
455 struct clk_tegra *c = to_clk_tegra(hw);
456 unsigned long flags;
457 u32 val;
458
459 spin_lock_irqsave(&clock_register_lock, flags);
460
461 val = clk_readl(c->reg);
462 val |= BUS_CLK_DISABLE << c->reg_shift;
463 clk_writel(val, c->reg);
464
465 spin_unlock_irqrestore(&clock_register_lock, flags);
466}
467
468static unsigned long tegra20_bus_clk_recalc_rate(struct clk_hw *hw,
469 unsigned long prate)
470{
471 struct clk_tegra *c = to_clk_tegra(hw);
472 u32 val = clk_readl(c->reg);
473 u64 rate = prate;
474
475 c->div = ((val >> c->reg_shift) & BUS_CLK_DIV_MASK) + 1;
476 c->mul = 1;
477
478 if (c->mul != 0 && c->div != 0) {
479 rate *= c->mul;
480 rate += c->div - 1; /* round up */
481 do_div(rate, c->div);
482 }
483 return rate;
484}
485
486static int tegra20_bus_clk_set_rate(struct clk_hw *hw, unsigned long rate,
487 unsigned long parent_rate)
488{
489 struct clk_tegra *c = to_clk_tegra(hw);
490 int ret = -EINVAL;
491 unsigned long flags;
492 u32 val;
493 int i;
494
495 spin_lock_irqsave(&clock_register_lock, flags);
496
497 val = clk_readl(c->reg);
498 for (i = 1; i <= 4; i++) {
499 if (rate == parent_rate / i) {
500 val &= ~(BUS_CLK_DIV_MASK << c->reg_shift);
501 val |= (i - 1) << c->reg_shift;
502 clk_writel(val, c->reg);
503 c->div = i;
504 c->mul = 1;
505 ret = 0;
506 break;
507 }
508 }
509
510 spin_unlock_irqrestore(&clock_register_lock, flags);
511
512 return ret;
513}
514
515static long tegra20_bus_clk_round_rate(struct clk_hw *hw, unsigned long rate,
516 unsigned long *prate)
517{
518 unsigned long parent_rate = *prate;
519 s64 divider;
520
521 if (rate >= parent_rate)
522 return rate;
523
524 divider = parent_rate;
525 divider += rate - 1;
526 do_div(divider, rate);
527
528 if (divider < 0)
529 return divider;
530
531 if (divider > 4)
532 divider = 4;
533 do_div(parent_rate, divider);
534
535 return parent_rate;
536}
537
538struct clk_ops tegra_bus_ops = {
539 .is_enabled = tegra20_bus_clk_is_enabled,
540 .enable = tegra20_bus_clk_enable,
541 .disable = tegra20_bus_clk_disable,
542 .set_rate = tegra20_bus_clk_set_rate,
543 .round_rate = tegra20_bus_clk_round_rate,
544 .recalc_rate = tegra20_bus_clk_recalc_rate,
545};
546
547/* Blink output functions */
548static int tegra20_blink_clk_is_enabled(struct clk_hw *hw)
549{
550 struct clk_tegra *c = to_clk_tegra(hw);
551 u32 val;
552
553 val = pmc_readl(PMC_CTRL);
554 c->state = (val & PMC_CTRL_BLINK_ENB) ? ON : OFF;
555 return c->state;
556}
557
558static unsigned long tegra20_blink_clk_recalc_rate(struct clk_hw *hw,
559 unsigned long prate)
560{
561 struct clk_tegra *c = to_clk_tegra(hw);
562 u64 rate = prate;
563 u32 val;
564
565 c->mul = 1;
566 val = pmc_readl(c->reg);
567
568 if (val & PMC_BLINK_TIMER_ENB) {
569 unsigned int on_off;
570
571 on_off = (val >> PMC_BLINK_TIMER_DATA_ON_SHIFT) &
572 PMC_BLINK_TIMER_DATA_ON_MASK;
573 val >>= PMC_BLINK_TIMER_DATA_OFF_SHIFT;
574 val &= PMC_BLINK_TIMER_DATA_OFF_MASK;
575 on_off += val;
576 /* each tick in the blink timer is 4 32KHz clocks */
577 c->div = on_off * 4;
578 } else {
579 c->div = 1;
580 }
581
582 if (c->mul != 0 && c->div != 0) {
583 rate *= c->mul;
584 rate += c->div - 1; /* round up */
585 do_div(rate, c->div);
586 }
587 return rate;
588}
589
590static int tegra20_blink_clk_enable(struct clk_hw *hw)
591{
592 u32 val;
593
594 val = pmc_readl(PMC_DPD_PADS_ORIDE);
595 pmc_writel(val | PMC_DPD_PADS_ORIDE_BLINK_ENB, PMC_DPD_PADS_ORIDE);
596
597 val = pmc_readl(PMC_CTRL);
598 pmc_writel(val | PMC_CTRL_BLINK_ENB, PMC_CTRL);
599
600 return 0;
601}
602
603static void tegra20_blink_clk_disable(struct clk_hw *hw)
604{
605 u32 val;
606
607 val = pmc_readl(PMC_CTRL);
608 pmc_writel(val & ~PMC_CTRL_BLINK_ENB, PMC_CTRL);
609
610 val = pmc_readl(PMC_DPD_PADS_ORIDE);
611 pmc_writel(val & ~PMC_DPD_PADS_ORIDE_BLINK_ENB, PMC_DPD_PADS_ORIDE);
612}
613
614static int tegra20_blink_clk_set_rate(struct clk_hw *hw, unsigned long rate,
615 unsigned long parent_rate)
616{
617 struct clk_tegra *c = to_clk_tegra(hw);
618
619 if (rate >= parent_rate) {
620 c->div = 1;
621 pmc_writel(0, c->reg);
622 } else {
623 unsigned int on_off;
624 u32 val;
625
626 on_off = DIV_ROUND_UP(parent_rate / 8, rate);
627 c->div = on_off * 8;
628
629 val = (on_off & PMC_BLINK_TIMER_DATA_ON_MASK) <<
630 PMC_BLINK_TIMER_DATA_ON_SHIFT;
631 on_off &= PMC_BLINK_TIMER_DATA_OFF_MASK;
632 on_off <<= PMC_BLINK_TIMER_DATA_OFF_SHIFT;
633 val |= on_off;
634 val |= PMC_BLINK_TIMER_ENB;
635 pmc_writel(val, c->reg);
636 }
637
638 return 0;
639}
640
641static long tegra20_blink_clk_round_rate(struct clk_hw *hw, unsigned long rate,
642 unsigned long *prate)
643{
644 int div;
645 int mul;
646 long round_rate = *prate;
647
648 mul = 1;
649
650 if (rate >= *prate) {
651 div = 1;
652 } else {
653 div = DIV_ROUND_UP(*prate / 8, rate);
654 div *= 8;
655 }
656
657 round_rate *= mul;
658 round_rate += div - 1;
659 do_div(round_rate, div);
660
661 return round_rate;
662}
663
664struct clk_ops tegra_blink_clk_ops = {
665 .is_enabled = tegra20_blink_clk_is_enabled,
666 .enable = tegra20_blink_clk_enable,
667 .disable = tegra20_blink_clk_disable,
668 .set_rate = tegra20_blink_clk_set_rate,
669 .round_rate = tegra20_blink_clk_round_rate,
670 .recalc_rate = tegra20_blink_clk_recalc_rate,
671};
672
673/* PLL Functions */
674static int tegra20_pll_clk_wait_for_lock(struct clk_tegra *c)
675{
676 udelay(c->u.pll.lock_delay);
677 return 0;
678}
679
680static int tegra20_pll_clk_is_enabled(struct clk_hw *hw)
681{
682 struct clk_tegra *c = to_clk_tegra(hw);
683 u32 val = clk_readl(c->reg + PLL_BASE);
684
685 c->state = (val & PLL_BASE_ENABLE) ? ON : OFF;
686 return c->state;
687}
688
689static unsigned long tegra20_pll_clk_recalc_rate(struct clk_hw *hw,
690 unsigned long prate)
691{
692 struct clk_tegra *c = to_clk_tegra(hw);
693 u32 val = clk_readl(c->reg + PLL_BASE);
694 u64 rate = prate;
695
696 if (c->flags & PLL_FIXED && !(val & PLL_BASE_OVERRIDE)) {
697 const struct clk_pll_freq_table *sel;
698 for (sel = c->u.pll.freq_table; sel->input_rate != 0; sel++) {
699 if (sel->input_rate == prate &&
700 sel->output_rate == c->u.pll.fixed_rate) {
701 c->mul = sel->n;
702 c->div = sel->m * sel->p;
703 break;
704 }
705 }
706 pr_err("Clock %s has unknown fixed frequency\n",
707 __clk_get_name(hw->clk));
708 BUG();
709 } else if (val & PLL_BASE_BYPASS) {
710 c->mul = 1;
711 c->div = 1;
712 } else {
713 c->mul = (val & PLL_BASE_DIVN_MASK) >> PLL_BASE_DIVN_SHIFT;
714 c->div = (val & PLL_BASE_DIVM_MASK) >> PLL_BASE_DIVM_SHIFT;
715 if (c->flags & PLLU)
716 c->div *= (val & PLLU_BASE_POST_DIV) ? 1 : 2;
717 else
718 c->div *= (val & PLL_BASE_DIVP_MASK) ? 2 : 1;
719 }
720
721 if (c->mul != 0 && c->div != 0) {
722 rate *= c->mul;
723 rate += c->div - 1; /* round up */
724 do_div(rate, c->div);
725 }
726 return rate;
727}
728
729static int tegra20_pll_clk_enable(struct clk_hw *hw)
730{
731 struct clk_tegra *c = to_clk_tegra(hw);
732 u32 val;
733 pr_debug("%s on clock %s\n", __func__, __clk_get_name(hw->clk));
734
735 val = clk_readl(c->reg + PLL_BASE);
736 val &= ~PLL_BASE_BYPASS;
737 val |= PLL_BASE_ENABLE;
738 clk_writel(val, c->reg + PLL_BASE);
739
740 tegra20_pll_clk_wait_for_lock(c);
741
742 return 0;
743}
744
745static void tegra20_pll_clk_disable(struct clk_hw *hw)
746{
747 struct clk_tegra *c = to_clk_tegra(hw);
748 u32 val;
749 pr_debug("%s on clock %s\n", __func__, __clk_get_name(hw->clk));
750
751 val = clk_readl(c->reg);
752 val &= ~(PLL_BASE_BYPASS | PLL_BASE_ENABLE);
753 clk_writel(val, c->reg);
754}
755
756static int tegra20_pll_clk_set_rate(struct clk_hw *hw, unsigned long rate,
757 unsigned long parent_rate)
758{
759 struct clk_tegra *c = to_clk_tegra(hw);
760 unsigned long input_rate = parent_rate;
761 const struct clk_pll_freq_table *sel;
762 u32 val;
763
764 pr_debug("%s: %s %lu\n", __func__, __clk_get_name(hw->clk), rate);
765
766 if (c->flags & PLL_FIXED) {
767 int ret = 0;
768 if (rate != c->u.pll.fixed_rate) {
769 pr_err("%s: Can not change %s fixed rate %lu to %lu\n",
770 __func__, __clk_get_name(hw->clk),
771 c->u.pll.fixed_rate, rate);
772 ret = -EINVAL;
773 }
774 return ret;
775 }
776
777 for (sel = c->u.pll.freq_table; sel->input_rate != 0; sel++) {
778 if (sel->input_rate == input_rate && sel->output_rate == rate) {
779 c->mul = sel->n;
780 c->div = sel->m * sel->p;
781
782 val = clk_readl(c->reg + PLL_BASE);
783 if (c->flags & PLL_FIXED)
784 val |= PLL_BASE_OVERRIDE;
785 val &= ~(PLL_BASE_DIVP_MASK | PLL_BASE_DIVN_MASK |
786 PLL_BASE_DIVM_MASK);
787 val |= (sel->m << PLL_BASE_DIVM_SHIFT) |
788 (sel->n << PLL_BASE_DIVN_SHIFT);
789 BUG_ON(sel->p < 1 || sel->p > 2);
790 if (c->flags & PLLU) {
791 if (sel->p == 1)
792 val |= PLLU_BASE_POST_DIV;
793 } else {
794 if (sel->p == 2)
795 val |= 1 << PLL_BASE_DIVP_SHIFT;
796 }
797 clk_writel(val, c->reg + PLL_BASE);
798
799 if (c->flags & PLL_HAS_CPCON) {
800 val = clk_readl(c->reg + PLL_MISC(c));
801 val &= ~PLL_MISC_CPCON_MASK;
802 val |= sel->cpcon << PLL_MISC_CPCON_SHIFT;
803 clk_writel(val, c->reg + PLL_MISC(c));
804 }
805
806 if (c->state == ON)
807 tegra20_pll_clk_enable(hw);
808 return 0;
809 }
810 }
811 return -EINVAL;
812}
813
814static long tegra20_pll_clk_round_rate(struct clk_hw *hw, unsigned long rate,
815 unsigned long *prate)
816{
817 struct clk_tegra *c = to_clk_tegra(hw);
818 const struct clk_pll_freq_table *sel;
819 unsigned long input_rate = *prate;
820 u64 output_rate = *prate;
821 int mul;
822 int div;
823
824 if (c->flags & PLL_FIXED)
825 return c->u.pll.fixed_rate;
826
827 for (sel = c->u.pll.freq_table; sel->input_rate != 0; sel++)
828 if (sel->input_rate == input_rate && sel->output_rate == rate) {
829 mul = sel->n;
830 div = sel->m * sel->p;
831 break;
832 }
833
834 if (sel->input_rate == 0)
835 return -EINVAL;
836
837 output_rate *= mul;
838 output_rate += div - 1; /* round up */
839 do_div(output_rate, div);
840
841 return output_rate;
842}
843
844struct clk_ops tegra_pll_ops = {
845 .is_enabled = tegra20_pll_clk_is_enabled,
846 .enable = tegra20_pll_clk_enable,
847 .disable = tegra20_pll_clk_disable,
848 .set_rate = tegra20_pll_clk_set_rate,
849 .recalc_rate = tegra20_pll_clk_recalc_rate,
850 .round_rate = tegra20_pll_clk_round_rate,
851};
852
853static void tegra20_pllx_clk_init(struct clk_hw *hw)
854{
855 struct clk_tegra *c = to_clk_tegra(hw);
856
857 if (tegra_sku_id == 7)
858 c->max_rate = 750000000;
859}
860
861struct clk_ops tegra_pllx_ops = {
862 .init = tegra20_pllx_clk_init,
863 .is_enabled = tegra20_pll_clk_is_enabled,
864 .enable = tegra20_pll_clk_enable,
865 .disable = tegra20_pll_clk_disable,
866 .set_rate = tegra20_pll_clk_set_rate,
867 .recalc_rate = tegra20_pll_clk_recalc_rate,
868 .round_rate = tegra20_pll_clk_round_rate,
869};
870
871static int tegra20_plle_clk_enable(struct clk_hw *hw)
872{
873 struct clk_tegra *c = to_clk_tegra(hw);
874 u32 val;
875
876 pr_debug("%s on clock %s\n", __func__, __clk_get_name(hw->clk));
877
878 mdelay(1);
879
880 val = clk_readl(c->reg + PLL_BASE);
881 if (!(val & PLLE_MISC_READY))
882 return -EBUSY;
883
884 val = clk_readl(c->reg + PLL_BASE);
885 val |= PLL_BASE_ENABLE | PLL_BASE_BYPASS;
886 clk_writel(val, c->reg + PLL_BASE);
887
888 return 0;
889}
890
891struct clk_ops tegra_plle_ops = {
892 .is_enabled = tegra20_pll_clk_is_enabled,
893 .enable = tegra20_plle_clk_enable,
894 .set_rate = tegra20_pll_clk_set_rate,
895 .recalc_rate = tegra20_pll_clk_recalc_rate,
896 .round_rate = tegra20_pll_clk_round_rate,
897};
898
899/* Clock divider ops */
900static int tegra20_pll_div_clk_is_enabled(struct clk_hw *hw)
901{
902 struct clk_tegra *c = to_clk_tegra(hw);
903 u32 val = clk_readl(c->reg);
904
905 val >>= c->reg_shift;
906 c->state = (val & PLL_OUT_CLKEN) ? ON : OFF;
907 if (!(val & PLL_OUT_RESET_DISABLE))
908 c->state = OFF;
909 return c->state;
910}
911
912static unsigned long tegra20_pll_div_clk_recalc_rate(struct clk_hw *hw,
913 unsigned long prate)
914{
915 struct clk_tegra *c = to_clk_tegra(hw);
916 u64 rate = prate;
917 u32 val = clk_readl(c->reg);
918 u32 divu71;
919
920 val >>= c->reg_shift;
921
922 if (c->flags & DIV_U71) {
923 divu71 = (val & PLL_OUT_RATIO_MASK) >> PLL_OUT_RATIO_SHIFT;
924 c->div = (divu71 + 2);
925 c->mul = 2;
926 } else if (c->flags & DIV_2) {
927 c->div = 2;
928 c->mul = 1;
929 } else {
930 c->div = 1;
931 c->mul = 1;
932 }
933
934 rate *= c->mul;
935 rate += c->div - 1; /* round up */
936 do_div(rate, c->div);
937
938 return rate;
939}
940
941static int tegra20_pll_div_clk_enable(struct clk_hw *hw)
942{
943 struct clk_tegra *c = to_clk_tegra(hw);
944 unsigned long flags;
945 u32 new_val;
946 u32 val;
947
948 pr_debug("%s: %s\n", __func__, __clk_get_name(hw->clk));
949
950 if (c->flags & DIV_U71) {
951 spin_lock_irqsave(&clock_register_lock, flags);
952 val = clk_readl(c->reg);
953 new_val = val >> c->reg_shift;
954 new_val &= 0xFFFF;
955
956 new_val |= PLL_OUT_CLKEN | PLL_OUT_RESET_DISABLE;
957
958 val &= ~(0xFFFF << c->reg_shift);
959 val |= new_val << c->reg_shift;
960 clk_writel(val, c->reg);
961 spin_unlock_irqrestore(&clock_register_lock, flags);
962 return 0;
963 } else if (c->flags & DIV_2) {
964 BUG_ON(!(c->flags & PLLD));
965 spin_lock_irqsave(&clock_register_lock, flags);
966 val = clk_readl(c->reg);
967 val &= ~PLLD_MISC_DIV_RST;
968 clk_writel(val, c->reg);
969 spin_unlock_irqrestore(&clock_register_lock, flags);
970 return 0;
971 }
972 return -EINVAL;
973}
974
975static void tegra20_pll_div_clk_disable(struct clk_hw *hw)
976{
977 struct clk_tegra *c = to_clk_tegra(hw);
978 unsigned long flags;
979 u32 new_val;
980 u32 val;
981
982 pr_debug("%s: %s\n", __func__, __clk_get_name(hw->clk));
983
984 if (c->flags & DIV_U71) {
985 spin_lock_irqsave(&clock_register_lock, flags);
986 val = clk_readl(c->reg);
987 new_val = val >> c->reg_shift;
988 new_val &= 0xFFFF;
989
990 new_val &= ~(PLL_OUT_CLKEN | PLL_OUT_RESET_DISABLE);
991
992 val &= ~(0xFFFF << c->reg_shift);
993 val |= new_val << c->reg_shift;
994 clk_writel(val, c->reg);
995 spin_unlock_irqrestore(&clock_register_lock, flags);
996 } else if (c->flags & DIV_2) {
997 BUG_ON(!(c->flags & PLLD));
998 spin_lock_irqsave(&clock_register_lock, flags);
999 val = clk_readl(c->reg);
1000 val |= PLLD_MISC_DIV_RST;
1001 clk_writel(val, c->reg);
1002 spin_unlock_irqrestore(&clock_register_lock, flags);
1003 }
1004}
1005
1006static int tegra20_pll_div_clk_set_rate(struct clk_hw *hw, unsigned long rate,
1007 unsigned long parent_rate)
1008{
1009 struct clk_tegra *c = to_clk_tegra(hw);
1010 unsigned long flags;
1011 int divider_u71;
1012 u32 new_val;
1013 u32 val;
1014
1015 pr_debug("%s: %s %lu\n", __func__, __clk_get_name(hw->clk), rate);
1016
1017 if (c->flags & DIV_U71) {
1018 divider_u71 = clk_div71_get_divider(parent_rate, rate);
1019 if (divider_u71 >= 0) {
1020 spin_lock_irqsave(&clock_register_lock, flags);
1021 val = clk_readl(c->reg);
1022 new_val = val >> c->reg_shift;
1023 new_val &= 0xFFFF;
1024 if (c->flags & DIV_U71_FIXED)
1025 new_val |= PLL_OUT_OVERRIDE;
1026 new_val &= ~PLL_OUT_RATIO_MASK;
1027 new_val |= divider_u71 << PLL_OUT_RATIO_SHIFT;
1028
1029 val &= ~(0xFFFF << c->reg_shift);
1030 val |= new_val << c->reg_shift;
1031 clk_writel(val, c->reg);
1032 c->div = divider_u71 + 2;
1033 c->mul = 2;
1034 spin_unlock_irqrestore(&clock_register_lock, flags);
1035 return 0;
1036 }
1037 } else if (c->flags & DIV_2) {
1038 if (parent_rate == rate * 2)
1039 return 0;
1040 }
1041 return -EINVAL;
1042}
1043
1044static long tegra20_pll_div_clk_round_rate(struct clk_hw *hw, unsigned long rate,
1045 unsigned long *prate)
1046{
1047 struct clk_tegra *c = to_clk_tegra(hw);
1048 unsigned long parent_rate = *prate;
1049 int divider;
1050
1051 pr_debug("%s: %s %lu\n", __func__, __clk_get_name(hw->clk), rate);
1052
1053 if (c->flags & DIV_U71) {
1054 divider = clk_div71_get_divider(parent_rate, rate);
1055 if (divider < 0)
1056 return divider;
1057 return DIV_ROUND_UP(parent_rate * 2, divider + 2);
1058 } else if (c->flags & DIV_2) {
1059 return DIV_ROUND_UP(parent_rate, 2);
1060 }
1061 return -EINVAL;
1062}
1063
1064struct clk_ops tegra_pll_div_ops = {
1065 .is_enabled = tegra20_pll_div_clk_is_enabled,
1066 .enable = tegra20_pll_div_clk_enable,
1067 .disable = tegra20_pll_div_clk_disable,
1068 .set_rate = tegra20_pll_div_clk_set_rate,
1069 .round_rate = tegra20_pll_div_clk_round_rate,
1070 .recalc_rate = tegra20_pll_div_clk_recalc_rate,
1071};
1072
1073/* Periph clk ops */
1074
1075static int tegra20_periph_clk_is_enabled(struct clk_hw *hw)
1076{
1077 struct clk_tegra *c = to_clk_tegra(hw);
1078
1079 c->state = ON;
1080
1081 if (!c->u.periph.clk_num)
1082 goto out;
1083
1084 if (!(clk_readl(CLK_OUT_ENB + PERIPH_CLK_TO_ENB_REG(c)) &
1085 PERIPH_CLK_TO_ENB_BIT(c)))
1086 c->state = OFF;
1087
1088 if (!(c->flags & PERIPH_NO_RESET))
1089 if (clk_readl(RST_DEVICES + PERIPH_CLK_TO_ENB_REG(c)) &
1090 PERIPH_CLK_TO_ENB_BIT(c))
1091 c->state = OFF;
1092
1093out:
1094 return c->state;
1095}
1096
1097static int tegra20_periph_clk_enable(struct clk_hw *hw)
1098{
1099 struct clk_tegra *c = to_clk_tegra(hw);
1100 unsigned long flags;
1101 u32 val;
1102
1103 pr_debug("%s on clock %s\n", __func__, __clk_get_name(hw->clk));
1104
1105 if (!c->u.periph.clk_num)
1106 return 0;
1107
1108 tegra_periph_clk_enable_refcount[c->u.periph.clk_num]++;
1109 if (tegra_periph_clk_enable_refcount[c->u.periph.clk_num] > 1)
1110 return 0;
1111
1112 spin_lock_irqsave(&clock_register_lock, flags);
1113
1114 clk_writel(PERIPH_CLK_TO_ENB_BIT(c),
1115 CLK_OUT_ENB_SET + PERIPH_CLK_TO_ENB_SET_REG(c));
1116 if (!(c->flags & PERIPH_NO_RESET) && !(c->flags & PERIPH_MANUAL_RESET))
1117 clk_writel(PERIPH_CLK_TO_ENB_BIT(c),
1118 RST_DEVICES_CLR + PERIPH_CLK_TO_ENB_SET_REG(c));
1119 if (c->flags & PERIPH_EMC_ENB) {
1120 /* The EMC peripheral clock has 2 extra enable bits */
1121 /* FIXME: Do they need to be disabled? */
1122 val = clk_readl(c->reg);
1123 val |= 0x3 << 24;
1124 clk_writel(val, c->reg);
1125 }
1126
1127 spin_unlock_irqrestore(&clock_register_lock, flags);
1128
1129 return 0;
1130}
1131
1132static void tegra20_periph_clk_disable(struct clk_hw *hw)
1133{
1134 struct clk_tegra *c = to_clk_tegra(hw);
1135 unsigned long flags;
1136
1137 pr_debug("%s on clock %s\n", __func__, __clk_get_name(hw->clk));
1138
1139 if (!c->u.periph.clk_num)
1140 return;
1141
1142 tegra_periph_clk_enable_refcount[c->u.periph.clk_num]--;
1143
1144 if (tegra_periph_clk_enable_refcount[c->u.periph.clk_num] > 0)
1145 return;
1146
1147 spin_lock_irqsave(&clock_register_lock, flags);
1148
1149 clk_writel(PERIPH_CLK_TO_ENB_BIT(c),
1150 CLK_OUT_ENB_CLR + PERIPH_CLK_TO_ENB_SET_REG(c));
1151
1152 spin_unlock_irqrestore(&clock_register_lock, flags);
1153}
1154
1155void tegra2_periph_clk_reset(struct clk_hw *hw, bool assert)
1156{
1157 struct clk_tegra *c = to_clk_tegra(hw);
1158 unsigned long base = assert ? RST_DEVICES_SET : RST_DEVICES_CLR;
1159
1160 pr_debug("%s %s on clock %s\n", __func__,
1161 assert ? "assert" : "deassert", __clk_get_name(hw->clk));
1162
1163 BUG_ON(!c->u.periph.clk_num);
1164
1165 if (!(c->flags & PERIPH_NO_RESET))
1166 clk_writel(PERIPH_CLK_TO_ENB_BIT(c),
1167 base + PERIPH_CLK_TO_ENB_SET_REG(c));
1168}
1169
1170static int tegra20_periph_clk_set_parent(struct clk_hw *hw, u8 index)
1171{
1172 struct clk_tegra *c = to_clk_tegra(hw);
1173 u32 val;
1174 u32 mask;
1175 u32 shift;
1176
1177 pr_debug("%s: %s %d\n", __func__, __clk_get_name(hw->clk), index);
1178
1179 if (c->flags & MUX_PWM) {
1180 shift = PERIPH_CLK_SOURCE_PWM_SHIFT;
1181 mask = PERIPH_CLK_SOURCE_PWM_MASK;
1182 } else {
1183 shift = PERIPH_CLK_SOURCE_SHIFT;
1184 mask = PERIPH_CLK_SOURCE_MASK;
1185 }
1186
1187 val = clk_readl(c->reg);
1188 val &= ~mask;
1189 val |= (index) << shift;
1190
1191 clk_writel(val, c->reg);
1192
1193 return 0;
1194}
1195
1196static u8 tegra20_periph_clk_get_parent(struct clk_hw *hw)
1197{
1198 struct clk_tegra *c = to_clk_tegra(hw);
1199 u32 val = clk_readl(c->reg);
1200 u32 mask;
1201 u32 shift;
1202
1203 if (c->flags & MUX_PWM) {
1204 shift = PERIPH_CLK_SOURCE_PWM_SHIFT;
1205 mask = PERIPH_CLK_SOURCE_PWM_MASK;
1206 } else {
1207 shift = PERIPH_CLK_SOURCE_SHIFT;
1208 mask = PERIPH_CLK_SOURCE_MASK;
1209 }
1210
1211 if (c->flags & MUX)
1212 return (val & mask) >> shift;
1213 else
1214 return 0;
1215}
1216
1217static unsigned long tegra20_periph_clk_recalc_rate(struct clk_hw *hw,
1218 unsigned long prate)
1219{
1220 struct clk_tegra *c = to_clk_tegra(hw);
1221 unsigned long rate = prate;
1222 u32 val = clk_readl(c->reg);
1223
1224 if (c->flags & DIV_U71) {
1225 u32 divu71 = val & PERIPH_CLK_SOURCE_DIVU71_MASK;
1226 c->div = divu71 + 2;
1227 c->mul = 2;
1228 } else if (c->flags & DIV_U16) {
1229 u32 divu16 = val & PERIPH_CLK_SOURCE_DIVU16_MASK;
1230 c->div = divu16 + 1;
1231 c->mul = 1;
1232 } else {
1233 c->div = 1;
1234 c->mul = 1;
1235 return rate;
1236 }
1237
1238 if (c->mul != 0 && c->div != 0) {
1239 rate *= c->mul;
1240 rate += c->div - 1; /* round up */
1241 do_div(rate, c->div);
1242 }
1243
1244 return rate;
1245}
1246
1247static int tegra20_periph_clk_set_rate(struct clk_hw *hw, unsigned long rate,
1248 unsigned long parent_rate)
1249{
1250 struct clk_tegra *c = to_clk_tegra(hw);
1251 u32 val;
1252 int divider;
1253
1254 val = clk_readl(c->reg);
1255
1256 if (c->flags & DIV_U71) {
1257 divider = clk_div71_get_divider(parent_rate, rate);
1258
1259 if (divider >= 0) {
1260 val = clk_readl(c->reg);
1261 val &= ~PERIPH_CLK_SOURCE_DIVU71_MASK;
1262 val |= divider;
1263 clk_writel(val, c->reg);
1264 c->div = divider + 2;
1265 c->mul = 2;
1266 return 0;
1267 }
1268 } else if (c->flags & DIV_U16) {
1269 divider = clk_div16_get_divider(parent_rate, rate);
1270 if (divider >= 0) {
1271 val = clk_readl(c->reg);
1272 val &= ~PERIPH_CLK_SOURCE_DIVU16_MASK;
1273 val |= divider;
1274 clk_writel(val, c->reg);
1275 c->div = divider + 1;
1276 c->mul = 1;
1277 return 0;
1278 }
1279 } else if (parent_rate <= rate) {
1280 c->div = 1;
1281 c->mul = 1;
1282 return 0;
1283 }
1284
1285 return -EINVAL;
1286}
1287
1288static long tegra20_periph_clk_round_rate(struct clk_hw *hw,
1289 unsigned long rate, unsigned long *prate)
1290{
1291 struct clk_tegra *c = to_clk_tegra(hw);
1292 unsigned long parent_rate = __clk_get_rate(__clk_get_parent(hw->clk));
1293 int divider;
1294
1295 pr_debug("%s: %s %lu\n", __func__, __clk_get_name(hw->clk), rate);
1296
1297 if (prate)
1298 parent_rate = *prate;
1299
1300 if (c->flags & DIV_U71) {
1301 divider = clk_div71_get_divider(parent_rate, rate);
1302 if (divider < 0)
1303 return divider;
1304
1305 return DIV_ROUND_UP(parent_rate * 2, divider + 2);
1306 } else if (c->flags & DIV_U16) {
1307 divider = clk_div16_get_divider(parent_rate, rate);
1308 if (divider < 0)
1309 return divider;
1310 return DIV_ROUND_UP(parent_rate, divider + 1);
1311 }
1312 return -EINVAL;
1313}
1314
1315struct clk_ops tegra_periph_clk_ops = {
1316 .is_enabled = tegra20_periph_clk_is_enabled,
1317 .enable = tegra20_periph_clk_enable,
1318 .disable = tegra20_periph_clk_disable,
1319 .set_parent = tegra20_periph_clk_set_parent,
1320 .get_parent = tegra20_periph_clk_get_parent,
1321 .set_rate = tegra20_periph_clk_set_rate,
1322 .round_rate = tegra20_periph_clk_round_rate,
1323 .recalc_rate = tegra20_periph_clk_recalc_rate,
1324};
1325
1326/* External memory controller clock ops */
1327static void tegra20_emc_clk_init(struct clk_hw *hw)
1328{
1329 struct clk_tegra *c = to_clk_tegra(hw);
1330 c->max_rate = __clk_get_rate(hw->clk);
1331}
1332
1333static long tegra20_emc_clk_round_rate(struct clk_hw *hw, unsigned long rate,
1334 unsigned long *prate)
1335{
1336 struct clk_tegra *c = to_clk_tegra(hw);
1337 long emc_rate;
1338 long clk_rate;
1339
1340 /*
1341 * The slowest entry in the EMC clock table that is at least as
1342 * fast as rate.
1343 */
1344 emc_rate = tegra_emc_round_rate(rate);
1345 if (emc_rate < 0)
1346 return c->max_rate;
1347
1348 /*
1349 * The fastest rate the PLL will generate that is at most the
1350 * requested rate.
1351 */
1352 clk_rate = tegra20_periph_clk_round_rate(hw, emc_rate, NULL);
1353
1354 /*
1355 * If this fails, and emc_rate > clk_rate, it's because the maximum
1356 * rate in the EMC tables is larger than the maximum rate of the EMC
1357 * clock. The EMC clock's max rate is the rate it was running when the
1358 * kernel booted. Such a mismatch is probably due to using the wrong
1359 * BCT, i.e. using a Tegra20 BCT with an EMC table written for Tegra25.
1360 */
1361 WARN_ONCE(emc_rate != clk_rate,
1362 "emc_rate %ld != clk_rate %ld",
1363 emc_rate, clk_rate);
1364
1365 return emc_rate;
1366}
1367
1368static int tegra20_emc_clk_set_rate(struct clk_hw *hw, unsigned long rate,
1369 unsigned long parent_rate)
1370{
1371 int ret;
1372
1373 /*
1374 * The Tegra2 memory controller has an interlock with the clock
1375 * block that allows memory shadowed registers to be updated,
1376 * and then transfer them to the main registers at the same
1377 * time as the clock update without glitches.
1378 */
1379 ret = tegra_emc_set_rate(rate);
1380 if (ret < 0)
1381 return ret;
1382
1383 ret = tegra20_periph_clk_set_rate(hw, rate, parent_rate);
1384 udelay(1);
1385
1386 return ret;
1387}
1388
1389struct clk_ops tegra_emc_clk_ops = {
1390 .init = tegra20_emc_clk_init,
1391 .is_enabled = tegra20_periph_clk_is_enabled,
1392 .enable = tegra20_periph_clk_enable,
1393 .disable = tegra20_periph_clk_disable,
1394 .set_parent = tegra20_periph_clk_set_parent,
1395 .get_parent = tegra20_periph_clk_get_parent,
1396 .set_rate = tegra20_emc_clk_set_rate,
1397 .round_rate = tegra20_emc_clk_round_rate,
1398 .recalc_rate = tegra20_periph_clk_recalc_rate,
1399};
1400
1401/* Clock doubler ops */
1402static int tegra20_clk_double_is_enabled(struct clk_hw *hw)
1403{
1404 struct clk_tegra *c = to_clk_tegra(hw);
1405
1406 c->state = ON;
1407
1408 if (!c->u.periph.clk_num)
1409 goto out;
1410
1411 if (!(clk_readl(CLK_OUT_ENB + PERIPH_CLK_TO_ENB_REG(c)) &
1412 PERIPH_CLK_TO_ENB_BIT(c)))
1413 c->state = OFF;
1414
1415out:
1416 return c->state;
1417};
1418
1419static unsigned long tegra20_clk_double_recalc_rate(struct clk_hw *hw,
1420 unsigned long prate)
1421{
1422 struct clk_tegra *c = to_clk_tegra(hw);
1423 u64 rate = prate;
1424
1425 c->mul = 2;
1426 c->div = 1;
1427
1428 rate *= c->mul;
1429 rate += c->div - 1; /* round up */
1430 do_div(rate, c->div);
1431
1432 return rate;
1433}
1434
1435static long tegra20_clk_double_round_rate(struct clk_hw *hw, unsigned long rate,
1436 unsigned long *prate)
1437{
1438 unsigned long output_rate = *prate;
1439
1440 do_div(output_rate, 2);
1441 return output_rate;
1442}
1443
1444static int tegra20_clk_double_set_rate(struct clk_hw *hw, unsigned long rate,
1445 unsigned long parent_rate)
1446{
1447 if (rate != 2 * parent_rate)
1448 return -EINVAL;
1449 return 0;
1450}
1451
1452struct clk_ops tegra_clk_double_ops = {
1453 .is_enabled = tegra20_clk_double_is_enabled,
1454 .enable = tegra20_periph_clk_enable,
1455 .disable = tegra20_periph_clk_disable,
1456 .set_rate = tegra20_clk_double_set_rate,
1457 .recalc_rate = tegra20_clk_double_recalc_rate,
1458 .round_rate = tegra20_clk_double_round_rate,
1459};
1460
1461/* Audio sync clock ops */
1462static int tegra20_audio_sync_clk_is_enabled(struct clk_hw *hw)
1463{
1464 struct clk_tegra *c = to_clk_tegra(hw);
1465 u32 val = clk_readl(c->reg);
1466
1467 c->state = (val & (1<<4)) ? OFF : ON;
1468 return c->state;
1469}
1470
1471static int tegra20_audio_sync_clk_enable(struct clk_hw *hw)
1472{
1473 struct clk_tegra *c = to_clk_tegra(hw);
1474
1475 clk_writel(0, c->reg);
1476 return 0;
1477}
1478
1479static void tegra20_audio_sync_clk_disable(struct clk_hw *hw)
1480{
1481 struct clk_tegra *c = to_clk_tegra(hw);
1482 clk_writel(1, c->reg);
1483}
1484
1485static u8 tegra20_audio_sync_clk_get_parent(struct clk_hw *hw)
1486{
1487 struct clk_tegra *c = to_clk_tegra(hw);
1488 u32 val = clk_readl(c->reg);
1489 int source;
1490
1491 source = val & 0xf;
1492 return source;
1493}
1494
1495static int tegra20_audio_sync_clk_set_parent(struct clk_hw *hw, u8 index)
1496{
1497 struct clk_tegra *c = to_clk_tegra(hw);
1498 u32 val;
1499
1500 val = clk_readl(c->reg);
1501 val &= ~0xf;
1502 val |= index;
1503
1504 clk_writel(val, c->reg);
1505
1506 return 0;
1507}
1508
1509struct clk_ops tegra_audio_sync_clk_ops = {
1510 .is_enabled = tegra20_audio_sync_clk_is_enabled,
1511 .enable = tegra20_audio_sync_clk_enable,
1512 .disable = tegra20_audio_sync_clk_disable,
1513 .set_parent = tegra20_audio_sync_clk_set_parent,
1514 .get_parent = tegra20_audio_sync_clk_get_parent,
1515};
1516
1517/* cdev1 and cdev2 (dap_mclk1 and dap_mclk2) ops */
1518
1519static int tegra20_cdev_clk_is_enabled(struct clk_hw *hw)
1520{
1521 struct clk_tegra *c = to_clk_tegra(hw);
1522 /* We could un-tristate the cdev1 or cdev2 pingroup here; this is
1523 * currently done in the pinmux code. */
1524 c->state = ON;
1525
1526 BUG_ON(!c->u.periph.clk_num);
1527
1528 if (!(clk_readl(CLK_OUT_ENB + PERIPH_CLK_TO_ENB_REG(c)) &
1529 PERIPH_CLK_TO_ENB_BIT(c)))
1530 c->state = OFF;
1531 return c->state;
1532}
1533
1534static int tegra20_cdev_clk_enable(struct clk_hw *hw)
1535{
1536 struct clk_tegra *c = to_clk_tegra(hw);
1537 BUG_ON(!c->u.periph.clk_num);
1538
1539 clk_writel(PERIPH_CLK_TO_ENB_BIT(c),
1540 CLK_OUT_ENB_SET + PERIPH_CLK_TO_ENB_SET_REG(c));
1541 return 0;
1542}
1543
1544static void tegra20_cdev_clk_disable(struct clk_hw *hw)
1545{
1546 struct clk_tegra *c = to_clk_tegra(hw);
1547 BUG_ON(!c->u.periph.clk_num);
1548
1549 clk_writel(PERIPH_CLK_TO_ENB_BIT(c),
1550 CLK_OUT_ENB_CLR + PERIPH_CLK_TO_ENB_SET_REG(c));
1551}
1552
1553static unsigned long tegra20_cdev_recalc_rate(struct clk_hw *hw,
1554 unsigned long prate)
1555{
1556 return to_clk_tegra(hw)->fixed_rate;
1557}
1558
1559struct clk_ops tegra_cdev_clk_ops = {
1560 .is_enabled = tegra20_cdev_clk_is_enabled,
1561 .enable = tegra20_cdev_clk_enable,
1562 .disable = tegra20_cdev_clk_disable,
1563 .recalc_rate = tegra20_cdev_recalc_rate,
1564};
1565
1566/* Tegra20 CPU clock and reset control functions */
1567static void tegra20_wait_cpu_in_reset(u32 cpu)
1568{
1569 unsigned int reg;
1570
1571 do {
1572 reg = readl(reg_clk_base +
1573 TEGRA_CLK_RST_CONTROLLER_RST_CPU_CMPLX_SET);
1574 cpu_relax();
1575 } while (!(reg & (1 << cpu))); /* check CPU been reset or not */
1576
1577 return;
1578}
1579
1580static void tegra20_put_cpu_in_reset(u32 cpu)
1581{
1582 writel(CPU_RESET(cpu),
1583 reg_clk_base + TEGRA_CLK_RST_CONTROLLER_RST_CPU_CMPLX_SET);
1584 dmb();
1585}
1586
1587static void tegra20_cpu_out_of_reset(u32 cpu)
1588{
1589 writel(CPU_RESET(cpu),
1590 reg_clk_base + TEGRA_CLK_RST_CONTROLLER_RST_CPU_CMPLX_CLR);
1591 wmb();
1592}
1593
1594static void tegra20_enable_cpu_clock(u32 cpu)
1595{
1596 unsigned int reg;
1597
1598 reg = readl(reg_clk_base + TEGRA_CLK_RST_CONTROLLER_CLK_CPU_CMPLX);
1599 writel(reg & ~CPU_CLOCK(cpu),
1600 reg_clk_base + TEGRA_CLK_RST_CONTROLLER_CLK_CPU_CMPLX);
1601 barrier();
1602 reg = readl(reg_clk_base + TEGRA_CLK_RST_CONTROLLER_CLK_CPU_CMPLX);
1603}
1604
1605static void tegra20_disable_cpu_clock(u32 cpu)
1606{
1607 unsigned int reg;
1608
1609 reg = readl(reg_clk_base + TEGRA_CLK_RST_CONTROLLER_CLK_CPU_CMPLX);
1610 writel(reg | CPU_CLOCK(cpu),
1611 reg_clk_base + TEGRA_CLK_RST_CONTROLLER_CLK_CPU_CMPLX);
1612}
1613
1614static struct tegra_cpu_car_ops tegra20_cpu_car_ops = {
1615 .wait_for_reset = tegra20_wait_cpu_in_reset,
1616 .put_in_reset = tegra20_put_cpu_in_reset,
1617 .out_of_reset = tegra20_cpu_out_of_reset,
1618 .enable_clock = tegra20_enable_cpu_clock,
1619 .disable_clock = tegra20_disable_cpu_clock,
1620};
1621
1622void __init tegra20_cpu_car_ops_init(void)
1623{
1624 tegra_cpu_car_ops = &tegra20_cpu_car_ops;
1625}