Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[linux-btrfs-devel.git] / arch / sh / kernel / cpu / sh4 / clock-sh4-202.c
blobf4e262adb39ed9ed8a450a9ec370bb8b533cf53a
1 /*
2 * arch/sh/kernel/cpu/sh4/clock-sh4-202.c
4 * Additional SH4-202 support for the clock framework
6 * Copyright (C) 2005 Paul Mundt
8 * This file is subject to the terms and conditions of the GNU General Public
9 * License. See the file "COPYING" in the main directory of this archive
10 * for more details.
12 #include <linux/init.h>
13 #include <linux/kernel.h>
14 #include <linux/err.h>
15 #include <linux/io.h>
16 #include <linux/clkdev.h>
17 #include <asm/clock.h>
18 #include <asm/freq.h>
20 #define CPG2_FRQCR3 0xfe0a0018
22 static int frqcr3_divisors[] = { 1, 2, 3, 4, 6, 8, 16 };
23 static int frqcr3_values[] = { 0, 1, 2, 3, 4, 5, 6 };
25 static unsigned long emi_clk_recalc(struct clk *clk)
27 int idx = __raw_readl(CPG2_FRQCR3) & 0x0007;
28 return clk->parent->rate / frqcr3_divisors[idx];
31 static inline int frqcr3_lookup(struct clk *clk, unsigned long rate)
33 int divisor = clk->parent->rate / rate;
34 int i;
36 for (i = 0; i < ARRAY_SIZE(frqcr3_divisors); i++)
37 if (frqcr3_divisors[i] == divisor)
38 return frqcr3_values[i];
40 /* Safe fallback */
41 return 5;
44 static struct clk_ops sh4202_emi_clk_ops = {
45 .recalc = emi_clk_recalc,
48 static struct clk sh4202_emi_clk = {
49 .flags = CLK_ENABLE_ON_INIT,
50 .ops = &sh4202_emi_clk_ops,
53 static unsigned long femi_clk_recalc(struct clk *clk)
55 int idx = (__raw_readl(CPG2_FRQCR3) >> 3) & 0x0007;
56 return clk->parent->rate / frqcr3_divisors[idx];
59 static struct clk_ops sh4202_femi_clk_ops = {
60 .recalc = femi_clk_recalc,
63 static struct clk sh4202_femi_clk = {
64 .flags = CLK_ENABLE_ON_INIT,
65 .ops = &sh4202_femi_clk_ops,
68 static void shoc_clk_init(struct clk *clk)
70 int i;
73 * For some reason, the shoc_clk seems to be set to some really
74 * insane value at boot (values outside of the allowable frequency
75 * range for instance). We deal with this by scaling it back down
76 * to something sensible just in case.
78 * Start scaling from the high end down until we find something
79 * that passes rate verification..
81 for (i = 0; i < ARRAY_SIZE(frqcr3_divisors); i++) {
82 int divisor = frqcr3_divisors[i];
84 if (clk->ops->set_rate(clk, clk->parent->rate / divisor) == 0)
85 break;
88 WARN_ON(i == ARRAY_SIZE(frqcr3_divisors)); /* Undefined clock */
91 static unsigned long shoc_clk_recalc(struct clk *clk)
93 int idx = (__raw_readl(CPG2_FRQCR3) >> 6) & 0x0007;
94 return clk->parent->rate / frqcr3_divisors[idx];
97 static int shoc_clk_verify_rate(struct clk *clk, unsigned long rate)
99 struct clk *bclk = clk_get(NULL, "bus_clk");
100 unsigned long bclk_rate = clk_get_rate(bclk);
102 clk_put(bclk);
104 if (rate > bclk_rate)
105 return 1;
106 if (rate > 66000000)
107 return 1;
109 return 0;
112 static int shoc_clk_set_rate(struct clk *clk, unsigned long rate)
114 unsigned long frqcr3;
115 unsigned int tmp;
117 /* Make sure we have something sensible to switch to */
118 if (shoc_clk_verify_rate(clk, rate) != 0)
119 return -EINVAL;
121 tmp = frqcr3_lookup(clk, rate);
123 frqcr3 = __raw_readl(CPG2_FRQCR3);
124 frqcr3 &= ~(0x0007 << 6);
125 frqcr3 |= tmp << 6;
126 __raw_writel(frqcr3, CPG2_FRQCR3);
128 clk->rate = clk->parent->rate / frqcr3_divisors[tmp];
130 return 0;
133 static struct clk_ops sh4202_shoc_clk_ops = {
134 .init = shoc_clk_init,
135 .recalc = shoc_clk_recalc,
136 .set_rate = shoc_clk_set_rate,
139 static struct clk sh4202_shoc_clk = {
140 .flags = CLK_ENABLE_ON_INIT,
141 .ops = &sh4202_shoc_clk_ops,
144 static struct clk *sh4202_onchip_clocks[] = {
145 &sh4202_emi_clk,
146 &sh4202_femi_clk,
147 &sh4202_shoc_clk,
150 static struct clk_lookup lookups[] = {
151 /* main clocks */
152 CLKDEV_CON_ID("emi_clk", &sh4202_emi_clk),
153 CLKDEV_CON_ID("femi_clk", &sh4202_femi_clk),
154 CLKDEV_CON_ID("shoc_clk", &sh4202_shoc_clk),
157 int __init arch_clk_init(void)
159 struct clk *clk;
160 int i, ret = 0;
162 cpg_clk_init();
164 clk = clk_get(NULL, "master_clk");
165 for (i = 0; i < ARRAY_SIZE(sh4202_onchip_clocks); i++) {
166 struct clk *clkp = sh4202_onchip_clocks[i];
168 clkp->parent = clk;
169 ret |= clk_register(clkp);
172 clk_put(clk);
174 clkdev_add_table(lookups, ARRAY_SIZE(lookups));
176 return ret;