blob: 2be592f6dbc079402224b2907bdef04217d23900 [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 Morimoto5e634d92013-04-04 21:20:40 -070035#define CKSCR 0xE61500C0
Kuninori Morimoto0c3091a2013-04-04 21:21:39 -070036#define PLLECR 0xE61500D0
37#define PLL1CR 0xE6150028
38#define PLL2CR 0xE615002C
39#define PLL2SCR 0xE61501F4
40#define PLL2HCR 0xE61501E4
41
42
43#define CPG_MAP(o) ((o - CPG_BASE) + cpg_mapping.base)
Kuninori Morimoto5e634d92013-04-04 21:20:40 -070044
Magnus Dammeccf0602013-03-26 10:34:24 +090045static struct clk_mapping cpg_mapping = {
46 .phys = CPG_BASE,
47 .len = CPG_LEN,
48};
49
50static struct clk extalr_clk = {
51 .rate = 32768,
52 .mapping = &cpg_mapping,
53};
54
55static struct clk extal1_clk = {
56 .rate = 26000000,
57 .mapping = &cpg_mapping,
58};
59
60static struct clk extal2_clk = {
61 .rate = 48000000,
62 .mapping = &cpg_mapping,
63};
64
Kuninori Morimoto5e634d92013-04-04 21:20:40 -070065static struct sh_clk_ops followparent_clk_ops = {
66 .recalc = followparent_recalc,
67};
68
69static struct clk main_clk = {
70 /* .parent will be set r8a73a4_clock_init */
71 .ops = &followparent_clk_ops,
72};
73
74SH_CLK_RATIO(div2, 1, 2);
75SH_CLK_RATIO(div4, 1, 4);
76
77SH_FIXED_RATIO_CLK(main_div2_clk, main_clk, div2);
78SH_FIXED_RATIO_CLK(extal1_div2_clk, extal1_clk, div2);
79SH_FIXED_RATIO_CLK(extal2_div2_clk, extal2_clk, div2);
80SH_FIXED_RATIO_CLK(extal2_div4_clk, extal2_clk, div4);
81
Kuninori Morimoto0c3091a2013-04-04 21:21:39 -070082/*
83 * PLL clocks
84 */
85static struct clk *pll_parent_main[] = {
86 [0] = &main_clk,
87 [1] = &main_div2_clk
88};
89
90static struct clk *pll_parent_main_extal[8] = {
91 [0] = &main_div2_clk,
92 [1] = &extal2_div2_clk,
93 [3] = &extal2_div4_clk,
94 [4] = &main_clk,
95 [5] = &extal2_clk,
96};
97
98static unsigned long pll_recalc(struct clk *clk)
99{
100 unsigned long mult = 1;
101
102 if (ioread32(CPG_MAP(PLLECR)) & (1 << clk->enable_bit))
103 mult = (((ioread32(clk->mapped_reg) >> 24) & 0x7f) + 1);
104
105 return clk->parent->rate * mult;
106}
107
108static int pll_set_parent(struct clk *clk, struct clk *parent)
109{
110 u32 val;
111 int i, ret;
112
113 if (!clk->parent_table || !clk->parent_num)
114 return -EINVAL;
115
116 /* Search the parent */
117 for (i = 0; i < clk->parent_num; i++)
118 if (clk->parent_table[i] == parent)
119 break;
120
121 if (i == clk->parent_num)
122 return -ENODEV;
123
124 ret = clk_reparent(clk, parent);
125 if (ret < 0)
126 return ret;
127
128 val = ioread32(clk->mapped_reg) &
129 ~(((1 << clk->src_width) - 1) << clk->src_shift);
130
131 iowrite32(val | i << clk->src_shift, clk->mapped_reg);
132
133 return 0;
134}
135
136static struct sh_clk_ops pll_clk_ops = {
137 .recalc = pll_recalc,
138 .set_parent = pll_set_parent,
139};
140
141#define PLL_CLOCK(name, p, pt, w, s, reg, e) \
142 static struct clk name = { \
143 .ops = &pll_clk_ops, \
144 .flags = CLK_ENABLE_ON_INIT, \
145 .parent = p, \
146 .parent_table = pt, \
147 .parent_num = ARRAY_SIZE(pt), \
148 .src_width = w, \
149 .src_shift = s, \
150 .enable_reg = (void __iomem *)reg, \
151 .enable_bit = e, \
152 .mapping = &cpg_mapping, \
153 }
154
155PLL_CLOCK(pll1_clk, &main_clk, pll_parent_main, 1, 7, PLL1CR, 1);
156PLL_CLOCK(pll2_clk, &main_div2_clk, pll_parent_main_extal, 3, 5, PLL2CR, 2);
157PLL_CLOCK(pll2s_clk, &main_div2_clk, pll_parent_main_extal, 3, 5, PLL2SCR, 4);
158PLL_CLOCK(pll2h_clk, &main_div2_clk, pll_parent_main_extal, 3, 5, PLL2HCR, 5);
159
160SH_FIXED_RATIO_CLK(pll1_div2_clk, pll1_clk, div2);
161
Magnus Dammeccf0602013-03-26 10:34:24 +0900162static struct clk *main_clks[] = {
163 &extalr_clk,
164 &extal1_clk,
Kuninori Morimoto5e634d92013-04-04 21:20:40 -0700165 &extal1_div2_clk,
Magnus Dammeccf0602013-03-26 10:34:24 +0900166 &extal2_clk,
Kuninori Morimoto5e634d92013-04-04 21:20:40 -0700167 &extal2_div2_clk,
168 &extal2_div4_clk,
169 &main_clk,
170 &main_div2_clk,
Kuninori Morimoto0c3091a2013-04-04 21:21:39 -0700171 &pll1_clk,
172 &pll1_div2_clk,
173 &pll2_clk,
174 &pll2s_clk,
175 &pll2h_clk,
Magnus Dammeccf0602013-03-26 10:34:24 +0900176};
177
Kuninori Morimotoc91cf2f2013-03-25 23:18:15 -0700178enum {
179 MSTP217, MSTP216, MSTP207, MSTP206, MSTP204, MSTP203,
180 MSTP522,
181 MSTP_NR
182};
183
Magnus Dammeccf0602013-03-26 10:34:24 +0900184static struct clk mstp_clks[MSTP_NR] = {
Magnus Damme481a522013-03-26 10:34:33 +0900185 [MSTP204] = SH_CLK_MSTP32(&extal2_clk, SMSTPCR2, 4, 0), /* SCIFA0 */
186 [MSTP203] = SH_CLK_MSTP32(&extal2_clk, SMSTPCR2, 3, 0), /* SCIFA1 */
187 [MSTP206] = SH_CLK_MSTP32(&extal2_clk, SMSTPCR2, 6, 0), /* SCIFB0 */
188 [MSTP207] = SH_CLK_MSTP32(&extal2_clk, SMSTPCR2, 7, 0), /* SCIFB1 */
189 [MSTP216] = SH_CLK_MSTP32(&extal2_clk, SMSTPCR2, 16, 0), /* SCIFB2 */
190 [MSTP217] = SH_CLK_MSTP32(&extal2_clk, SMSTPCR2, 17, 0), /* SCIFB3 */
Kuninori Morimotoc91cf2f2013-03-25 23:18:15 -0700191 [MSTP522] = SH_CLK_MSTP32(&extal2_clk, SMSTPCR5, 22, 0), /* Thermal */
Magnus Dammeccf0602013-03-26 10:34:24 +0900192};
193
194static struct clk_lookup lookups[] = {
Kuninori Morimoto5e634d92013-04-04 21:20:40 -0700195 /* main clock */
196 CLKDEV_CON_ID("extal1", &extal1_clk),
197 CLKDEV_CON_ID("extal1_div2", &extal1_div2_clk),
198 CLKDEV_CON_ID("extal2", &extal2_clk),
199 CLKDEV_CON_ID("extal2_div2", &extal2_div2_clk),
200 CLKDEV_CON_ID("extal2_div4", &extal2_div4_clk),
201
Kuninori Morimoto0c3091a2013-04-04 21:21:39 -0700202 /* pll clock */
203 CLKDEV_CON_ID("pll1", &pll1_clk),
204 CLKDEV_CON_ID("pll1_div2", &pll1_div2_clk),
205 CLKDEV_CON_ID("pll2", &pll2_clk),
206 CLKDEV_CON_ID("pll2s", &pll2s_clk),
207 CLKDEV_CON_ID("pll2h", &pll2h_clk),
208
209 /* MSTP */
Magnus Damme481a522013-03-26 10:34:33 +0900210 CLKDEV_DEV_ID("sh-sci.0", &mstp_clks[MSTP204]),
211 CLKDEV_DEV_ID("sh-sci.1", &mstp_clks[MSTP203]),
212 CLKDEV_DEV_ID("sh-sci.2", &mstp_clks[MSTP206]),
213 CLKDEV_DEV_ID("sh-sci.3", &mstp_clks[MSTP207]),
214 CLKDEV_DEV_ID("sh-sci.4", &mstp_clks[MSTP216]),
215 CLKDEV_DEV_ID("sh-sci.5", &mstp_clks[MSTP217]),
Kuninori Morimotoc91cf2f2013-03-25 23:18:15 -0700216 CLKDEV_DEV_ID("rcar_thermal", &mstp_clks[MSTP522]),
217
218 /* for DT */
219 CLKDEV_DEV_ID("e61f0000.thermal", &mstp_clks[MSTP522]),
Magnus Dammeccf0602013-03-26 10:34:24 +0900220};
221
222void __init r8a73a4_clock_init(void)
223{
224 void __iomem *cpg_base, *reg;
225 int k, ret = 0;
Kuninori Morimoto5e634d92013-04-04 21:20:40 -0700226 u32 ckscr;
Magnus Dammeccf0602013-03-26 10:34:24 +0900227
228 /* fix MPCLK to EXTAL2 for now.
229 * this is needed until more detailed clock topology is supported
230 */
231 cpg_base = ioremap_nocache(CPG_BASE, CPG_LEN);
232 BUG_ON(!cpg_base);
233 reg = cpg_base + (MPCKCR - CPG_BASE);
234 iowrite32(ioread32(reg) | 1 << 7 | 0x0c, reg); /* set CKSEL */
235 iounmap(cpg_base);
236
Kuninori Morimoto5e634d92013-04-04 21:20:40 -0700237 reg = ioremap_nocache(CKSCR, PAGE_SIZE);
238 BUG_ON(!reg);
239 ckscr = ioread32(reg);
240 iounmap(reg);
241
242 switch ((ckscr >> 28) & 0x3) {
243 case 0:
244 main_clk.parent = &extal1_clk;
245 break;
246 case 1:
247 main_clk.parent = &extal1_div2_clk;
248 break;
249 case 2:
250 main_clk.parent = &extal2_clk;
251 break;
252 case 3:
253 main_clk.parent = &extal2_div2_clk;
254 break;
255 }
256
Magnus Dammeccf0602013-03-26 10:34:24 +0900257 for (k = 0; !ret && (k < ARRAY_SIZE(main_clks)); k++)
258 ret = clk_register(main_clks[k]);
259
260 if (!ret)
261 ret = sh_clk_mstp_register(mstp_clks, MSTP_NR);
262
263 clkdev_add_table(lookups, ARRAY_SIZE(lookups));
264
265 if (!ret)
266 shmobile_clk_init();
267 else
268 panic("failed to setup r8a73a4 clocks\n");
269}