aboutsummaryrefslogtreecommitdiffstats
path: root/arch/arm/plat-samsung/clock-clksrc.c
diff options
context:
space:
mode:
authorHarald Welte <laforge@gnumonks.org>2009-11-30 20:24:34 -0500
committerBen Dooks <ben-linux@fluff.org>2010-01-15 03:10:09 -0500
commitaa9ad6ad9c16e1daff41792c485f46e601a5af33 (patch)
tree3958ea197f6a08a0969e5a30afb35b3868030095 /arch/arm/plat-samsung/clock-clksrc.c
parent8360493c4ae4c116339cd1cf4da100c3547f23af (diff)
ARM: SAMSUNG: Add core clock implementation for clksrc based clocks
Add a core for the clksrc clock implementation, which is found in many of the newer Samsung SoCs into plat-samsung. Signed-off-by: Harald Welte <laforge@gnumonks.org> [ben-linux@fluff.org: split from original patch to make change smaller] [ben-linux@fluff.org: split clk and clksrc changes] [ben-linux@fluff.org: moved to plat-samsung from plat-s3c] [ben-linux@fluff.org: re-wrote headers after splits] [ben-linux@fluff.org: added better documentation to headers] Signed-off-by: Ben Dooks <ben-linux@fluff.org>
Diffstat (limited to 'arch/arm/plat-samsung/clock-clksrc.c')
-rw-r--r--arch/arm/plat-samsung/clock-clksrc.c177
1 files changed, 177 insertions, 0 deletions
diff --git a/arch/arm/plat-samsung/clock-clksrc.c b/arch/arm/plat-samsung/clock-clksrc.c
new file mode 100644
index 000000000000..5872f0b62b7c
--- /dev/null
+++ b/arch/arm/plat-samsung/clock-clksrc.c
@@ -0,0 +1,177 @@
1/* linux/arch/arm/plat-samsung/clock-clksrc.c
2 *
3 * Copyright 2008 Simtec Electronics
4 * Ben Dooks <ben@simtec.co.uk>
5 * http://armlinux.simtec.co.uk/
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10*/
11
12#include <linux/init.h>
13#include <linux/module.h>
14#include <linux/kernel.h>
15#include <linux/list.h>
16#include <linux/errno.h>
17#include <linux/err.h>
18#include <linux/clk.h>
19#include <linux/sysdev.h>
20#include <linux/io.h>
21
22#include <plat/clock.h>
23#include <plat/clock-clksrc.h>
24#include <plat/cpu-freq.h>
25
26static inline struct clksrc_clk *to_clksrc(struct clk *clk)
27{
28 return container_of(clk, struct clksrc_clk, clk);
29}
30
31static inline u32 bit_mask(u32 shift, u32 nr_bits)
32{
33 u32 mask = 0xffffffff >> (32 - nr_bits);
34
35 return mask << shift;
36}
37
38static unsigned long s3c_getrate_clksrc(struct clk *clk)
39{
40 struct clksrc_clk *sclk = to_clksrc(clk);
41 unsigned long rate = clk_get_rate(clk->parent);
42 u32 clkdiv = __raw_readl(sclk->reg_div.reg);
43 u32 mask = bit_mask(sclk->reg_div.shift, sclk->reg_div.size);
44
45 clkdiv &= mask;
46 clkdiv >>= sclk->reg_div.shift;
47 clkdiv++;
48
49 rate /= clkdiv;
50 return rate;
51}
52
53static int s3c_setrate_clksrc(struct clk *clk, unsigned long rate)
54{
55 struct clksrc_clk *sclk = to_clksrc(clk);
56 void __iomem *reg = sclk->reg_div.reg;
57 unsigned int div;
58 u32 mask = bit_mask(sclk->reg_div.shift, sclk->reg_div.size);
59 u32 val;
60
61 rate = clk_round_rate(clk, rate);
62 div = clk_get_rate(clk->parent) / rate;
63 if (div > 16)
64 return -EINVAL;
65
66 val = __raw_readl(reg);
67 val &= ~mask;
68 val |= (div - 1) << sclk->reg_div.shift;
69 __raw_writel(val, reg);
70
71 return 0;
72}
73
74static int s3c_setparent_clksrc(struct clk *clk, struct clk *parent)
75{
76 struct clksrc_clk *sclk = to_clksrc(clk);
77 struct clksrc_sources *srcs = sclk->sources;
78 u32 clksrc = __raw_readl(sclk->reg_src.reg);
79 u32 mask = bit_mask(sclk->reg_src.shift, sclk->reg_src.size);
80 int src_nr = -1;
81 int ptr;
82
83 for (ptr = 0; ptr < srcs->nr_sources; ptr++)
84 if (srcs->sources[ptr] == parent) {
85 src_nr = ptr;
86 break;
87 }
88
89 if (src_nr >= 0 && sclk->reg_src.reg) {
90 clk->parent = parent;
91
92 clksrc &= ~mask;
93 clksrc |= src_nr << sclk->reg_src.shift;
94
95 __raw_writel(clksrc, sclk->reg_src.reg);
96 return 0;
97 }
98
99 return -EINVAL;
100}
101
102static unsigned long s3c_roundrate_clksrc(struct clk *clk,
103 unsigned long rate)
104{
105 unsigned long parent_rate = clk_get_rate(clk->parent);
106 int div;
107
108 if (rate >= parent_rate)
109 rate = parent_rate;
110 else {
111 div = parent_rate / rate;
112 if (parent_rate % rate)
113 div++;
114
115 if (div == 0)
116 div = 1;
117 if (div > 16)
118 div = 16;
119
120 rate = parent_rate / div;
121 }
122
123 return rate;
124}
125
126/* Clock initialisation code */
127
128void __init_or_cpufreq s3c_set_clksrc(struct clksrc_clk *clk)
129{
130 struct clksrc_sources *srcs = clk->sources;
131 u32 mask = bit_mask(clk->reg_src.shift, clk->reg_src.size);
132 u32 clksrc = 0;
133
134 if (clk->reg_src.reg)
135 clksrc = __raw_readl(clk->reg_src.reg);
136
137 clksrc &= mask;
138 clksrc >>= clk->reg_src.shift;
139
140 if (clksrc > srcs->nr_sources || !srcs->sources[clksrc]) {
141 printk(KERN_ERR "%s: bad source %d\n",
142 clk->clk.name, clksrc);
143 return;
144 }
145
146 clk->clk.parent = srcs->sources[clksrc];
147
148 printk(KERN_INFO "%s: source is %s (%d), rate is %ld\n",
149 clk->clk.name, clk->clk.parent->name, clksrc,
150 clk_get_rate(&clk->clk));
151}
152
153void __init s3c_register_clksrc(struct clksrc_clk *clksrc, int size)
154{
155 int ret;
156
157 for (; size > 0; size--, clksrc++) {
158 /* fill in the default functions */
159 if (!clksrc->clk.set_parent)
160 clksrc->clk.set_parent = s3c_setparent_clksrc;
161 if (!clksrc->clk.get_rate)
162 clksrc->clk.get_rate = s3c_getrate_clksrc;
163 if (!clksrc->clk.set_rate)
164 clksrc->clk.set_rate = s3c_setrate_clksrc;
165 if (!clksrc->clk.round_rate)
166 clksrc->clk.round_rate = s3c_roundrate_clksrc;
167
168 s3c_set_clksrc(clksrc);
169
170 ret = s3c24xx_register_clock(&clksrc->clk);
171
172 if (ret < 0) {
173 printk(KERN_ERR "%s: failed to register %s (%d)\n",
174 __func__, clksrc->clk.name, ret);
175 }
176 }
177}