aeaaa87cca06168ae66094e5f51353780a0d7770
[platform/adaptation/renesas_rcar/renesas_kernel.git] / arch / arm / mach-omap2 / clkt34xx_dpll3m2.c
1 /*
2  * OMAP34xx M2 divider clock code
3  *
4  * Copyright (C) 2007-2008 Texas Instruments, Inc.
5  * Copyright (C) 2007-2010 Nokia Corporation
6  *
7  * Paul Walmsley
8  * Jouni Högander
9  *
10  * Parts of this code are based on code written by
11  * Richard Woodruff, Tony Lindgren, Tuukka Tikkanen, Karthik Dasu
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License version 2 as
15  * published by the Free Software Foundation.
16  */
17 #undef DEBUG
18
19 #include <linux/kernel.h>
20 #include <linux/errno.h>
21 #include <linux/clk.h>
22 #include <linux/io.h>
23
24 #include "../plat-omap/sram.h"
25
26 #include "clock.h"
27 #include "clock3xxx.h"
28 #include "clock34xx.h"
29 #include "sdrc.h"
30
31 #define CYCLES_PER_MHZ                  1000000
32
33 /*
34  * CORE DPLL (DPLL3) M2 divider rate programming functions
35  *
36  * These call into SRAM code to do the actual CM writes, since the SDRAM
37  * is clocked from DPLL3.
38  */
39
40 /**
41  * omap3_core_dpll_m2_set_rate - set CORE DPLL M2 divider
42  * @clk: struct clk * of DPLL to set
43  * @rate: rounded target rate
44  *
45  * Program the DPLL M2 divider with the rounded target rate.  Returns
46  * -EINVAL upon error, or 0 upon success.
47  */
48 #ifdef CONFIG_COMMON_CLK
49 int omap3_core_dpll_m2_set_rate(struct clk_hw *hw, unsigned long rate,
50                                         unsigned long parent_rate)
51 {
52         struct clk_hw_omap *clk = to_clk_hw_omap(hw);
53 #else
54 int omap3_core_dpll_m2_set_rate(struct clk *clk, unsigned long rate)
55 {
56 #endif
57         u32 new_div = 0;
58         u32 unlock_dll = 0;
59         u32 c;
60         unsigned long validrate, sdrcrate, _mpurate;
61         struct omap_sdrc_params *sdrc_cs0;
62         struct omap_sdrc_params *sdrc_cs1;
63         int ret;
64         unsigned long clkrate;
65
66         if (!clk || !rate)
67                 return -EINVAL;
68
69         validrate = omap2_clksel_round_rate_div(clk, rate, &new_div);
70         if (validrate != rate)
71                 return -EINVAL;
72
73         sdrcrate = __clk_get_rate(sdrc_ick_p);
74 #ifdef CONFIG_COMMON_CLK
75         clkrate = __clk_get_rate(hw->clk);
76 #else
77         clkrate = __clk_get_rate(clk);
78 #endif
79         if (rate > clkrate)
80                 sdrcrate <<= ((rate / clkrate) >> 1);
81         else
82                 sdrcrate >>= ((clkrate / rate) >> 1);
83
84         ret = omap2_sdrc_get_params(sdrcrate, &sdrc_cs0, &sdrc_cs1);
85         if (ret)
86                 return -EINVAL;
87
88         if (sdrcrate < MIN_SDRC_DLL_LOCK_FREQ) {
89                 pr_debug("clock: will unlock SDRC DLL\n");
90                 unlock_dll = 1;
91         }
92
93         /*
94          * XXX This only needs to be done when the CPU frequency changes
95          */
96         _mpurate = __clk_get_rate(arm_fck_p) / CYCLES_PER_MHZ;
97         c = (_mpurate << SDRC_MPURATE_SCALE) >> SDRC_MPURATE_BASE_SHIFT;
98         c += 1;  /* for safety */
99         c *= SDRC_MPURATE_LOOPS;
100         c >>= SDRC_MPURATE_SCALE;
101         if (c == 0)
102                 c = 1;
103
104         pr_debug("clock: changing CORE DPLL rate from %lu to %lu\n",
105                  clkrate, validrate);
106         pr_debug("clock: SDRC CS0 timing params used: RFR %08x CTRLA %08x CTRLB %08x MR %08x\n",
107                  sdrc_cs0->rfr_ctrl, sdrc_cs0->actim_ctrla,
108                  sdrc_cs0->actim_ctrlb, sdrc_cs0->mr);
109         if (sdrc_cs1)
110                 pr_debug("clock: SDRC CS1 timing params used: RFR %08x CTRLA %08x CTRLB %08x MR %08x\n",
111                          sdrc_cs1->rfr_ctrl, sdrc_cs1->actim_ctrla,
112                          sdrc_cs1->actim_ctrlb, sdrc_cs1->mr);
113
114         if (sdrc_cs1)
115                 omap3_configure_core_dpll(
116                                   new_div, unlock_dll, c, rate > clkrate,
117                                   sdrc_cs0->rfr_ctrl, sdrc_cs0->actim_ctrla,
118                                   sdrc_cs0->actim_ctrlb, sdrc_cs0->mr,
119                                   sdrc_cs1->rfr_ctrl, sdrc_cs1->actim_ctrla,
120                                   sdrc_cs1->actim_ctrlb, sdrc_cs1->mr);
121         else
122                 omap3_configure_core_dpll(
123                                   new_div, unlock_dll, c, rate > clkrate,
124                                   sdrc_cs0->rfr_ctrl, sdrc_cs0->actim_ctrla,
125                                   sdrc_cs0->actim_ctrlb, sdrc_cs0->mr,
126                                   0, 0, 0, 0);
127 #ifndef CONFIG_COMMON_CLK
128         clk->rate = rate;
129 #endif
130
131         return 0;
132 }
133