blob: 147314ac6a14b13684a37e7c996a2ab55b8b4735 [file] [log] [blame]
Magnus Dammeccf0602013-03-26 10:34:24 +09001/*
2 * r8a73a4 clock framework support
3 *
4 * Copyright (C) 2013 Renesas Solutions Corp.
5 * Copyright (C) 2013 Magnus Damm
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 as published by
9 * the Free Software Foundation; version 2 of the License.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20#include <linux/init.h>
21#include <linux/io.h>
22#include <linux/kernel.h>
23#include <linux/sh_clk.h>
24#include <linux/clkdev.h>
Kuninori Morimoto5e634d92013-04-04 21:20:40 -070025#include <mach/clock.h>
Magnus Dammeccf0602013-03-26 10:34:24 +090026#include <mach/common.h>
27
28#define CPG_BASE 0xe6150000
29#define CPG_LEN 0x270
30
31#define MPCKCR 0xe6150080
Magnus Damme481a522013-03-26 10:34:33 +090032#define SMSTPCR2 0xe6150138
Kuninori Morimotoc91cf2f2013-03-25 23:18:15 -070033#define SMSTPCR5 0xe6150144
Magnus Dammeccf0602013-03-26 10:34:24 +090034
Kuninori Morimotob89edf32013-04-04 21:22:16 -070035#define FRQCRA 0xE6150000
36#define FRQCRB 0xE6150004
Kuninori Morimoto5e634d92013-04-04 21:20:40 -070037#define CKSCR 0xE61500C0
Kuninori Morimoto0c3091a2013-04-04 21:21:39 -070038#define PLLECR 0xE61500D0
39#define PLL1CR 0xE6150028
40#define PLL2CR 0xE615002C
41#define PLL2SCR 0xE61501F4
42#define PLL2HCR 0xE61501E4
43
44
45#define CPG_MAP(o) ((o - CPG_BASE) + cpg_mapping.base)
Kuninori Morimoto5e634d92013-04-04 21:20:40 -070046
Magnus Dammeccf0602013-03-26 10:34:24 +090047static struct clk_mapping cpg_mapping = {
48 .phys = CPG_BASE,
49 .len = CPG_LEN,
50};
51
52static struct clk extalr_clk = {
53 .rate = 32768,
54 .mapping = &cpg_mapping,
55};
56
57static struct clk extal1_clk = {
58 .rate = 26000000,
59 .mapping = &cpg_mapping,
60};
61
62static struct clk extal2_clk = {
63 .rate = 48000000,
64 .mapping = &cpg_mapping,
65};
66
Kuninori Morimoto5e634d92013-04-04 21:20:40 -070067static struct sh_clk_ops followparent_clk_ops = {
68 .recalc = followparent_recalc,
69};
70
71static struct clk main_clk = {
72 /* .parent will be set r8a73a4_clock_init */
73 .ops = &followparent_clk_ops,
74};
75
76SH_CLK_RATIO(div2, 1, 2);
77SH_CLK_RATIO(div4, 1, 4);
78
79SH_FIXED_RATIO_CLK(main_div2_clk, main_clk, div2);
80SH_FIXED_RATIO_CLK(extal1_div2_clk, extal1_clk, div2);
81SH_FIXED_RATIO_CLK(extal2_div2_clk, extal2_clk, div2);
82SH_FIXED_RATIO_CLK(extal2_div4_clk, extal2_clk, div4);
83
Kuninori Morimoto0c3091a2013-04-04 21:21:39 -070084/*
85 * PLL clocks
86 */
87static struct clk *pll_parent_main[] = {
88 [0] = &main_clk,
89 [1] = &main_div2_clk
90};
91
92static struct clk *pll_parent_main_extal[8] = {
93 [0] = &main_div2_clk,
94 [1] = &extal2_div2_clk,
95 [3] = &extal2_div4_clk,
96 [4] = &main_clk,
97 [5] = &extal2_clk,
98};
99
100static unsigned long pll_recalc(struct clk *clk)
101{
102 unsigned long mult = 1;
103
104 if (ioread32(CPG_MAP(PLLECR)) & (1 << clk->enable_bit))
105 mult = (((ioread32(clk->mapped_reg) >> 24) & 0x7f) + 1);
106
107 return clk->parent->rate * mult;
108}
109
110static int pll_set_parent(struct clk *clk, struct clk *parent)
111{
112 u32 val;
113 int i, ret;
114
115 if (!clk->parent_table || !clk->parent_num)
116 return -EINVAL;
117
118 /* Search the parent */
119 for (i = 0; i < clk->parent_num; i++)
120 if (clk->parent_table[i] == parent)
121 break;
122
123 if (i == clk->parent_num)
124 return -ENODEV;
125
126 ret = clk_reparent(clk, parent);
127 if (ret < 0)
128 return ret;
129
130 val = ioread32(clk->mapped_reg) &
131 ~(((1 << clk->src_width) - 1) << clk->src_shift);
132
133 iowrite32(val | i << clk->src_shift, clk->mapped_reg);
134
135 return 0;
136}
137
138static struct sh_clk_ops pll_clk_ops = {
139 .recalc = pll_recalc,
140 .set_parent = pll_set_parent,
141};
142
143#define PLL_CLOCK(name, p, pt, w, s, reg, e) \
144 static struct clk name = { \
145 .ops = &pll_clk_ops, \
146 .flags = CLK_ENABLE_ON_INIT, \
147 .parent = p, \
148 .parent_table = pt, \
149 .parent_num = ARRAY_SIZE(pt), \
150 .src_width = w, \
151 .src_shift = s, \
152 .enable_reg = (void __iomem *)reg, \
153 .enable_bit = e, \
154 .mapping = &cpg_mapping, \
155 }
156
157PLL_CLOCK(pll1_clk, &main_clk, pll_parent_main, 1, 7, PLL1CR, 1);
158PLL_CLOCK(pll2_clk, &main_div2_clk, pll_parent_main_extal, 3, 5, PLL2CR, 2);
159PLL_CLOCK(pll2s_clk, &main_div2_clk, pll_parent_main_extal, 3, 5, PLL2SCR, 4);
160PLL_CLOCK(pll2h_clk, &main_div2_clk, pll_parent_main_extal, 3, 5, PLL2HCR, 5);
161
162SH_FIXED_RATIO_CLK(pll1_div2_clk, pll1_clk, div2);
163
Magnus Dammeccf0602013-03-26 10:34:24 +0900164static struct clk *main_clks[] = {
165 &extalr_clk,
166 &extal1_clk,
Kuninori Morimoto5e634d92013-04-04 21:20:40 -0700167 &extal1_div2_clk,
Magnus Dammeccf0602013-03-26 10:34:24 +0900168 &extal2_clk,
Kuninori Morimoto5e634d92013-04-04 21:20:40 -0700169 &extal2_div2_clk,
170 &extal2_div4_clk,
171 &main_clk,
172 &main_div2_clk,
Kuninori Morimoto0c3091a2013-04-04 21:21:39 -0700173 &pll1_clk,
174 &pll1_div2_clk,
175 &pll2_clk,
176 &pll2s_clk,
177 &pll2h_clk,
Magnus Dammeccf0602013-03-26 10:34:24 +0900178};
179
Kuninori Morimotob89edf32013-04-04 21:22:16 -0700180/* DIV4 */
181static void div4_kick(struct clk *clk)
182{
183 unsigned long value;
184
185 /* set KICK bit in FRQCRB to update hardware setting */
186 value = ioread32(CPG_MAP(FRQCRB));
187 value |= (1 << 31);
188 iowrite32(value, CPG_MAP(FRQCRB));
189}
190
191static int divisors[] = { 2, 3, 4, 6, 8, 12, 16, 18, 24, 0, 36, 48, 10};
192
193static struct clk_div_mult_table div4_div_mult_table = {
194 .divisors = divisors,
195 .nr_divisors = ARRAY_SIZE(divisors),
196};
197
198static struct clk_div4_table div4_table = {
199 .div_mult_table = &div4_div_mult_table,
200 .kick = div4_kick,
201};
202
203enum {
204 DIV4_I, DIV4_M3, DIV4_B, DIV4_M1, DIV4_M2,
205 DIV4_ZX, DIV4_ZS, DIV4_HP,
206 DIV4_NR };
207
208static struct clk div4_clks[DIV4_NR] = {
209 [DIV4_I] = SH_CLK_DIV4(&pll1_clk, FRQCRA, 20, 0x0dff, CLK_ENABLE_ON_INIT),
210 [DIV4_M3] = SH_CLK_DIV4(&pll1_clk, FRQCRA, 12, 0x1dff, CLK_ENABLE_ON_INIT),
211 [DIV4_B] = SH_CLK_DIV4(&pll1_clk, FRQCRA, 8, 0x0dff, CLK_ENABLE_ON_INIT),
212 [DIV4_M1] = SH_CLK_DIV4(&pll1_clk, FRQCRA, 4, 0x1dff, 0),
213 [DIV4_M2] = SH_CLK_DIV4(&pll1_clk, FRQCRA, 0, 0x1dff, 0),
214 [DIV4_ZX] = SH_CLK_DIV4(&pll1_clk, FRQCRB, 12, 0x0dff, 0),
215 [DIV4_ZS] = SH_CLK_DIV4(&pll1_clk, FRQCRB, 8, 0x0dff, 0),
216 [DIV4_HP] = SH_CLK_DIV4(&pll1_clk, FRQCRB, 4, 0x0dff, 0),
217};
218
219/* MSTP */
Kuninori Morimotoc91cf2f2013-03-25 23:18:15 -0700220enum {
221 MSTP217, MSTP216, MSTP207, MSTP206, MSTP204, MSTP203,
222 MSTP522,
223 MSTP_NR
224};
225
Magnus Dammeccf0602013-03-26 10:34:24 +0900226static struct clk mstp_clks[MSTP_NR] = {
Magnus Damme481a522013-03-26 10:34:33 +0900227 [MSTP204] = SH_CLK_MSTP32(&extal2_clk, SMSTPCR2, 4, 0), /* SCIFA0 */
228 [MSTP203] = SH_CLK_MSTP32(&extal2_clk, SMSTPCR2, 3, 0), /* SCIFA1 */
229 [MSTP206] = SH_CLK_MSTP32(&extal2_clk, SMSTPCR2, 6, 0), /* SCIFB0 */
230 [MSTP207] = SH_CLK_MSTP32(&extal2_clk, SMSTPCR2, 7, 0), /* SCIFB1 */
231 [MSTP216] = SH_CLK_MSTP32(&extal2_clk, SMSTPCR2, 16, 0), /* SCIFB2 */
232 [MSTP217] = SH_CLK_MSTP32(&extal2_clk, SMSTPCR2, 17, 0), /* SCIFB3 */
Kuninori Morimotoc91cf2f2013-03-25 23:18:15 -0700233 [MSTP522] = SH_CLK_MSTP32(&extal2_clk, SMSTPCR5, 22, 0), /* Thermal */
Magnus Dammeccf0602013-03-26 10:34:24 +0900234};
235
236static struct clk_lookup lookups[] = {
Kuninori Morimoto5e634d92013-04-04 21:20:40 -0700237 /* main clock */
238 CLKDEV_CON_ID("extal1", &extal1_clk),
239 CLKDEV_CON_ID("extal1_div2", &extal1_div2_clk),
240 CLKDEV_CON_ID("extal2", &extal2_clk),
241 CLKDEV_CON_ID("extal2_div2", &extal2_div2_clk),
242 CLKDEV_CON_ID("extal2_div4", &extal2_div4_clk),
243
Kuninori Morimoto0c3091a2013-04-04 21:21:39 -0700244 /* pll clock */
245 CLKDEV_CON_ID("pll1", &pll1_clk),
246 CLKDEV_CON_ID("pll1_div2", &pll1_div2_clk),
247 CLKDEV_CON_ID("pll2", &pll2_clk),
248 CLKDEV_CON_ID("pll2s", &pll2s_clk),
249 CLKDEV_CON_ID("pll2h", &pll2h_clk),
250
251 /* MSTP */
Magnus Damme481a522013-03-26 10:34:33 +0900252 CLKDEV_DEV_ID("sh-sci.0", &mstp_clks[MSTP204]),
253 CLKDEV_DEV_ID("sh-sci.1", &mstp_clks[MSTP203]),
254 CLKDEV_DEV_ID("sh-sci.2", &mstp_clks[MSTP206]),
255 CLKDEV_DEV_ID("sh-sci.3", &mstp_clks[MSTP207]),
256 CLKDEV_DEV_ID("sh-sci.4", &mstp_clks[MSTP216]),
257 CLKDEV_DEV_ID("sh-sci.5", &mstp_clks[MSTP217]),
Kuninori Morimotoc91cf2f2013-03-25 23:18:15 -0700258 CLKDEV_DEV_ID("rcar_thermal", &mstp_clks[MSTP522]),
259
260 /* for DT */
261 CLKDEV_DEV_ID("e61f0000.thermal", &mstp_clks[MSTP522]),
Magnus Dammeccf0602013-03-26 10:34:24 +0900262};
263
264void __init r8a73a4_clock_init(void)
265{
266 void __iomem *cpg_base, *reg;
267 int k, ret = 0;
Kuninori Morimoto5e634d92013-04-04 21:20:40 -0700268 u32 ckscr;
Magnus Dammeccf0602013-03-26 10:34:24 +0900269
270 /* fix MPCLK to EXTAL2 for now.
271 * this is needed until more detailed clock topology is supported
272 */
273 cpg_base = ioremap_nocache(CPG_BASE, CPG_LEN);
274 BUG_ON(!cpg_base);
275 reg = cpg_base + (MPCKCR - CPG_BASE);
276 iowrite32(ioread32(reg) | 1 << 7 | 0x0c, reg); /* set CKSEL */
277 iounmap(cpg_base);
278
Kuninori Morimoto5e634d92013-04-04 21:20:40 -0700279 reg = ioremap_nocache(CKSCR, PAGE_SIZE);
280 BUG_ON(!reg);
281 ckscr = ioread32(reg);
282 iounmap(reg);
283
284 switch ((ckscr >> 28) & 0x3) {
285 case 0:
286 main_clk.parent = &extal1_clk;
287 break;
288 case 1:
289 main_clk.parent = &extal1_div2_clk;
290 break;
291 case 2:
292 main_clk.parent = &extal2_clk;
293 break;
294 case 3:
295 main_clk.parent = &extal2_div2_clk;
296 break;
297 }
298
Magnus Dammeccf0602013-03-26 10:34:24 +0900299 for (k = 0; !ret && (k < ARRAY_SIZE(main_clks)); k++)
300 ret = clk_register(main_clks[k]);
301
302 if (!ret)
Kuninori Morimotob89edf32013-04-04 21:22:16 -0700303 ret = sh_clk_div4_register(div4_clks, DIV4_NR, &div4_table);
304
305 if (!ret)
Magnus Dammeccf0602013-03-26 10:34:24 +0900306 ret = sh_clk_mstp_register(mstp_clks, MSTP_NR);
307
308 clkdev_add_table(lookups, ARRAY_SIZE(lookups));
309
310 if (!ret)
311 shmobile_clk_init();
312 else
313 panic("failed to setup r8a73a4 clocks\n");
314}