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