blob: c813c27f2e58c6f8e62c523a1a9a8b1d212a96d8 [file] [log] [blame]
Boris BREZILLON38d34c32013-10-11 10:44:49 +02001/*
2 * Copyright (C) 2013 Boris BREZILLON <b.brezillon@overkiz.com>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 */
10
11#include <linux/clk-provider.h>
12#include <linux/clkdev.h>
13#include <linux/clk/at91_pmc.h>
14#include <linux/delay.h>
15#include <linux/of.h>
Boris Brezillon1bdf0232014-09-07 08:14:29 +020016#include <linux/mfd/syscon.h>
17#include <linux/regmap.h>
Boris BREZILLON38d34c32013-10-11 10:44:49 +020018
19#include "pmc.h"
20
21#define SLOW_CLOCK_FREQ 32768
22#define MAINF_DIV 16
23#define MAINFRDY_TIMEOUT (((MAINF_DIV + 1) * USEC_PER_SEC) / \
24 SLOW_CLOCK_FREQ)
25#define MAINF_LOOP_MIN_WAIT (USEC_PER_SEC / SLOW_CLOCK_FREQ)
26#define MAINF_LOOP_MAX_WAIT MAINFRDY_TIMEOUT
27
Boris BREZILLON27cb1c22014-05-07 18:00:08 +020028#define MOR_KEY_MASK (0xff << 16)
29
30struct clk_main_osc {
Boris BREZILLON38d34c32013-10-11 10:44:49 +020031 struct clk_hw hw;
Boris Brezillon1bdf0232014-09-07 08:14:29 +020032 struct regmap *regmap;
Boris BREZILLON38d34c32013-10-11 10:44:49 +020033};
34
Boris BREZILLON27cb1c22014-05-07 18:00:08 +020035#define to_clk_main_osc(hw) container_of(hw, struct clk_main_osc, hw)
Boris BREZILLON38d34c32013-10-11 10:44:49 +020036
Boris BREZILLON27cb1c22014-05-07 18:00:08 +020037struct clk_main_rc_osc {
38 struct clk_hw hw;
Boris Brezillon1bdf0232014-09-07 08:14:29 +020039 struct regmap *regmap;
Boris BREZILLON27cb1c22014-05-07 18:00:08 +020040 unsigned long frequency;
41 unsigned long accuracy;
42};
43
44#define to_clk_main_rc_osc(hw) container_of(hw, struct clk_main_rc_osc, hw)
45
46struct clk_rm9200_main {
47 struct clk_hw hw;
Boris Brezillon1bdf0232014-09-07 08:14:29 +020048 struct regmap *regmap;
Boris BREZILLON27cb1c22014-05-07 18:00:08 +020049};
50
51#define to_clk_rm9200_main(hw) container_of(hw, struct clk_rm9200_main, hw)
52
53struct clk_sam9x5_main {
54 struct clk_hw hw;
Boris Brezillon1bdf0232014-09-07 08:14:29 +020055 struct regmap *regmap;
Boris BREZILLON27cb1c22014-05-07 18:00:08 +020056 u8 parent;
57};
58
59#define to_clk_sam9x5_main(hw) container_of(hw, struct clk_sam9x5_main, hw)
60
Boris Brezillon1bdf0232014-09-07 08:14:29 +020061static inline bool clk_main_osc_ready(struct regmap *regmap)
62{
63 unsigned int status;
64
65 regmap_read(regmap, AT91_PMC_SR, &status);
66
67 return status & AT91_PMC_MOSCS;
68}
69
Boris BREZILLON27cb1c22014-05-07 18:00:08 +020070static int clk_main_osc_prepare(struct clk_hw *hw)
Boris BREZILLON38d34c32013-10-11 10:44:49 +020071{
Boris BREZILLON27cb1c22014-05-07 18:00:08 +020072 struct clk_main_osc *osc = to_clk_main_osc(hw);
Boris Brezillon1bdf0232014-09-07 08:14:29 +020073 struct regmap *regmap = osc->regmap;
Boris BREZILLON38d34c32013-10-11 10:44:49 +020074 u32 tmp;
75
Boris Brezillon1bdf0232014-09-07 08:14:29 +020076 regmap_read(regmap, AT91_CKGR_MOR, &tmp);
77 tmp &= ~MOR_KEY_MASK;
78
Boris BREZILLON27cb1c22014-05-07 18:00:08 +020079 if (tmp & AT91_PMC_OSCBYPASS)
Boris BREZILLON38d34c32013-10-11 10:44:49 +020080 return 0;
81
Boris BREZILLON27cb1c22014-05-07 18:00:08 +020082 if (!(tmp & AT91_PMC_MOSCEN)) {
83 tmp |= AT91_PMC_MOSCEN | AT91_PMC_KEY;
Boris Brezillon1bdf0232014-09-07 08:14:29 +020084 regmap_write(regmap, AT91_CKGR_MOR, tmp);
Boris BREZILLON27cb1c22014-05-07 18:00:08 +020085 }
86
Alexandre Belloni99a81702015-09-16 23:47:39 +020087 while (!clk_main_osc_ready(regmap))
88 cpu_relax();
Boris BREZILLON38d34c32013-10-11 10:44:49 +020089
90 return 0;
91}
92
Boris BREZILLON27cb1c22014-05-07 18:00:08 +020093static void clk_main_osc_unprepare(struct clk_hw *hw)
Boris BREZILLON38d34c32013-10-11 10:44:49 +020094{
Boris BREZILLON27cb1c22014-05-07 18:00:08 +020095 struct clk_main_osc *osc = to_clk_main_osc(hw);
Boris Brezillon1bdf0232014-09-07 08:14:29 +020096 struct regmap *regmap = osc->regmap;
97 u32 tmp;
Boris BREZILLON38d34c32013-10-11 10:44:49 +020098
Boris Brezillon1bdf0232014-09-07 08:14:29 +020099 regmap_read(regmap, AT91_CKGR_MOR, &tmp);
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200100 if (tmp & AT91_PMC_OSCBYPASS)
101 return;
102
103 if (!(tmp & AT91_PMC_MOSCEN))
104 return;
105
106 tmp &= ~(AT91_PMC_KEY | AT91_PMC_MOSCEN);
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200107 regmap_write(regmap, AT91_CKGR_MOR, tmp | AT91_PMC_KEY);
Boris BREZILLON38d34c32013-10-11 10:44:49 +0200108}
109
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200110static int clk_main_osc_is_prepared(struct clk_hw *hw)
Boris BREZILLON38d34c32013-10-11 10:44:49 +0200111{
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200112 struct clk_main_osc *osc = to_clk_main_osc(hw);
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200113 struct regmap *regmap = osc->regmap;
114 u32 tmp, status;
Boris BREZILLON38d34c32013-10-11 10:44:49 +0200115
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200116 regmap_read(regmap, AT91_CKGR_MOR, &tmp);
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200117 if (tmp & AT91_PMC_OSCBYPASS)
118 return 1;
Boris BREZILLON38d34c32013-10-11 10:44:49 +0200119
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200120 regmap_read(regmap, AT91_PMC_SR, &status);
121
122 return (status & AT91_PMC_MOSCS) && (tmp & AT91_PMC_MOSCEN);
Boris BREZILLON38d34c32013-10-11 10:44:49 +0200123}
124
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200125static const struct clk_ops main_osc_ops = {
126 .prepare = clk_main_osc_prepare,
127 .unprepare = clk_main_osc_unprepare,
128 .is_prepared = clk_main_osc_is_prepared,
Boris BREZILLON38d34c32013-10-11 10:44:49 +0200129};
130
Stephen Boydf5644f12016-06-01 14:31:22 -0700131static struct clk_hw * __init
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200132at91_clk_register_main_osc(struct regmap *regmap,
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200133 const char *name,
134 const char *parent_name,
135 bool bypass)
Boris BREZILLON38d34c32013-10-11 10:44:49 +0200136{
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200137 struct clk_main_osc *osc;
Boris BREZILLON38d34c32013-10-11 10:44:49 +0200138 struct clk_init_data init;
Stephen Boydf5644f12016-06-01 14:31:22 -0700139 struct clk_hw *hw;
140 int ret;
Boris BREZILLON38d34c32013-10-11 10:44:49 +0200141
Alexandre Belloni99a81702015-09-16 23:47:39 +0200142 if (!name || !parent_name)
Boris BREZILLON38d34c32013-10-11 10:44:49 +0200143 return ERR_PTR(-EINVAL);
144
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200145 osc = kzalloc(sizeof(*osc), GFP_KERNEL);
146 if (!osc)
147 return ERR_PTR(-ENOMEM);
148
149 init.name = name;
150 init.ops = &main_osc_ops;
151 init.parent_names = &parent_name;
152 init.num_parents = 1;
153 init.flags = CLK_IGNORE_UNUSED;
154
155 osc->hw.init = &init;
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200156 osc->regmap = regmap;
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200157
158 if (bypass)
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200159 regmap_update_bits(regmap,
160 AT91_CKGR_MOR, MOR_KEY_MASK |
161 AT91_PMC_MOSCEN,
162 AT91_PMC_OSCBYPASS | AT91_PMC_KEY);
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200163
Stephen Boydf5644f12016-06-01 14:31:22 -0700164 hw = &osc->hw;
165 ret = clk_hw_register(NULL, &osc->hw);
166 if (ret) {
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200167 kfree(osc);
Stephen Boydf5644f12016-06-01 14:31:22 -0700168 hw = ERR_PTR(ret);
169 }
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200170
Stephen Boydf5644f12016-06-01 14:31:22 -0700171 return hw;
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200172}
173
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200174static void __init of_at91rm9200_clk_main_osc_setup(struct device_node *np)
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200175{
Stephen Boydf5644f12016-06-01 14:31:22 -0700176 struct clk_hw *hw;
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200177 const char *name = np->name;
178 const char *parent_name;
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200179 struct regmap *regmap;
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200180 bool bypass;
181
182 of_property_read_string(np, "clock-output-names", &name);
183 bypass = of_property_read_bool(np, "atmel,osc-bypass");
184 parent_name = of_clk_get_parent_name(np, 0);
185
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200186 regmap = syscon_node_to_regmap(of_get_parent(np));
187 if (IS_ERR(regmap))
188 return;
189
Stephen Boydf5644f12016-06-01 14:31:22 -0700190 hw = at91_clk_register_main_osc(regmap, name, parent_name, bypass);
191 if (IS_ERR(hw))
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200192 return;
193
Stephen Boydf5644f12016-06-01 14:31:22 -0700194 of_clk_add_hw_provider(np, of_clk_hw_simple_get, hw);
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200195}
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200196CLK_OF_DECLARE(at91rm9200_clk_main_osc, "atmel,at91rm9200-clk-main-osc",
197 of_at91rm9200_clk_main_osc_setup);
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200198
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200199static bool clk_main_rc_osc_ready(struct regmap *regmap)
200{
201 unsigned int status;
202
203 regmap_read(regmap, AT91_PMC_SR, &status);
204
205 return status & AT91_PMC_MOSCRCS;
206}
207
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200208static int clk_main_rc_osc_prepare(struct clk_hw *hw)
209{
210 struct clk_main_rc_osc *osc = to_clk_main_rc_osc(hw);
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200211 struct regmap *regmap = osc->regmap;
212 unsigned int mor;
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200213
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200214 regmap_read(regmap, AT91_CKGR_MOR, &mor);
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200215
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200216 if (!(mor & AT91_PMC_MOSCRCEN))
217 regmap_update_bits(regmap, AT91_CKGR_MOR,
218 MOR_KEY_MASK | AT91_PMC_MOSCRCEN,
219 AT91_PMC_MOSCRCEN | AT91_PMC_KEY);
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200220
Alexandre Belloni99a81702015-09-16 23:47:39 +0200221 while (!clk_main_rc_osc_ready(regmap))
222 cpu_relax();
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200223
224 return 0;
225}
226
227static void clk_main_rc_osc_unprepare(struct clk_hw *hw)
228{
229 struct clk_main_rc_osc *osc = to_clk_main_rc_osc(hw);
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200230 struct regmap *regmap = osc->regmap;
231 unsigned int mor;
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200232
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200233 regmap_read(regmap, AT91_CKGR_MOR, &mor);
234
235 if (!(mor & AT91_PMC_MOSCRCEN))
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200236 return;
237
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200238 regmap_update_bits(regmap, AT91_CKGR_MOR,
239 MOR_KEY_MASK | AT91_PMC_MOSCRCEN, AT91_PMC_KEY);
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200240}
241
242static int clk_main_rc_osc_is_prepared(struct clk_hw *hw)
243{
244 struct clk_main_rc_osc *osc = to_clk_main_rc_osc(hw);
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200245 struct regmap *regmap = osc->regmap;
246 unsigned int mor, status;
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200247
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200248 regmap_read(regmap, AT91_CKGR_MOR, &mor);
249 regmap_read(regmap, AT91_PMC_SR, &status);
250
251 return (mor & AT91_PMC_MOSCRCEN) && (status & AT91_PMC_MOSCRCS);
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200252}
253
254static unsigned long clk_main_rc_osc_recalc_rate(struct clk_hw *hw,
255 unsigned long parent_rate)
256{
257 struct clk_main_rc_osc *osc = to_clk_main_rc_osc(hw);
258
259 return osc->frequency;
260}
261
262static unsigned long clk_main_rc_osc_recalc_accuracy(struct clk_hw *hw,
263 unsigned long parent_acc)
264{
265 struct clk_main_rc_osc *osc = to_clk_main_rc_osc(hw);
266
267 return osc->accuracy;
268}
269
270static const struct clk_ops main_rc_osc_ops = {
271 .prepare = clk_main_rc_osc_prepare,
272 .unprepare = clk_main_rc_osc_unprepare,
273 .is_prepared = clk_main_rc_osc_is_prepared,
274 .recalc_rate = clk_main_rc_osc_recalc_rate,
275 .recalc_accuracy = clk_main_rc_osc_recalc_accuracy,
276};
277
Stephen Boydf5644f12016-06-01 14:31:22 -0700278static struct clk_hw * __init
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200279at91_clk_register_main_rc_osc(struct regmap *regmap,
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200280 const char *name,
281 u32 frequency, u32 accuracy)
282{
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200283 struct clk_main_rc_osc *osc;
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200284 struct clk_init_data init;
Stephen Boydf5644f12016-06-01 14:31:22 -0700285 struct clk_hw *hw;
286 int ret;
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200287
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200288 if (!name || !frequency)
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200289 return ERR_PTR(-EINVAL);
290
291 osc = kzalloc(sizeof(*osc), GFP_KERNEL);
292 if (!osc)
293 return ERR_PTR(-ENOMEM);
294
295 init.name = name;
296 init.ops = &main_rc_osc_ops;
297 init.parent_names = NULL;
298 init.num_parents = 0;
Stephen Boyda9bb2ef2016-03-01 10:59:46 -0800299 init.flags = CLK_IGNORE_UNUSED;
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200300
301 osc->hw.init = &init;
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200302 osc->regmap = regmap;
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200303 osc->frequency = frequency;
304 osc->accuracy = accuracy;
305
Stephen Boydf5644f12016-06-01 14:31:22 -0700306 hw = &osc->hw;
307 ret = clk_hw_register(NULL, hw);
308 if (ret) {
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200309 kfree(osc);
Stephen Boydf5644f12016-06-01 14:31:22 -0700310 hw = ERR_PTR(ret);
311 }
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200312
Stephen Boydf5644f12016-06-01 14:31:22 -0700313 return hw;
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200314}
315
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200316static void __init of_at91sam9x5_clk_main_rc_osc_setup(struct device_node *np)
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200317{
Stephen Boydf5644f12016-06-01 14:31:22 -0700318 struct clk_hw *hw;
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200319 u32 frequency = 0;
320 u32 accuracy = 0;
321 const char *name = np->name;
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200322 struct regmap *regmap;
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200323
324 of_property_read_string(np, "clock-output-names", &name);
325 of_property_read_u32(np, "clock-frequency", &frequency);
326 of_property_read_u32(np, "clock-accuracy", &accuracy);
327
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200328 regmap = syscon_node_to_regmap(of_get_parent(np));
329 if (IS_ERR(regmap))
330 return;
331
Stephen Boydf5644f12016-06-01 14:31:22 -0700332 hw = at91_clk_register_main_rc_osc(regmap, name, frequency, accuracy);
333 if (IS_ERR(hw))
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200334 return;
335
Stephen Boydf5644f12016-06-01 14:31:22 -0700336 of_clk_add_hw_provider(np, of_clk_hw_simple_get, hw);
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200337}
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200338CLK_OF_DECLARE(at91sam9x5_clk_main_rc_osc, "atmel,at91sam9x5-clk-main-rc-osc",
339 of_at91sam9x5_clk_main_rc_osc_setup);
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200340
341
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200342static int clk_main_probe_frequency(struct regmap *regmap)
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200343{
344 unsigned long prep_time, timeout;
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200345 unsigned int mcfr;
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200346
347 timeout = jiffies + usecs_to_jiffies(MAINFRDY_TIMEOUT);
348 do {
349 prep_time = jiffies;
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200350 regmap_read(regmap, AT91_CKGR_MCFR, &mcfr);
351 if (mcfr & AT91_PMC_MAINRDY)
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200352 return 0;
353 usleep_range(MAINF_LOOP_MIN_WAIT, MAINF_LOOP_MAX_WAIT);
354 } while (time_before(prep_time, timeout));
355
356 return -ETIMEDOUT;
357}
358
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200359static unsigned long clk_main_recalc_rate(struct regmap *regmap,
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200360 unsigned long parent_rate)
361{
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200362 unsigned int mcfr;
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200363
364 if (parent_rate)
365 return parent_rate;
366
Alexandre Belloni4da66b62014-07-01 16:12:12 +0200367 pr_warn("Main crystal frequency not set, using approximate value\n");
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200368 regmap_read(regmap, AT91_CKGR_MCFR, &mcfr);
369 if (!(mcfr & AT91_PMC_MAINRDY))
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200370 return 0;
371
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200372 return ((mcfr & AT91_PMC_MAINF) * SLOW_CLOCK_FREQ) / MAINF_DIV;
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200373}
374
375static int clk_rm9200_main_prepare(struct clk_hw *hw)
376{
377 struct clk_rm9200_main *clkmain = to_clk_rm9200_main(hw);
378
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200379 return clk_main_probe_frequency(clkmain->regmap);
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200380}
381
382static int clk_rm9200_main_is_prepared(struct clk_hw *hw)
383{
384 struct clk_rm9200_main *clkmain = to_clk_rm9200_main(hw);
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200385 unsigned int status;
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200386
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200387 regmap_read(clkmain->regmap, AT91_CKGR_MCFR, &status);
388
389 return status & AT91_PMC_MAINRDY ? 1 : 0;
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200390}
391
392static unsigned long clk_rm9200_main_recalc_rate(struct clk_hw *hw,
393 unsigned long parent_rate)
394{
395 struct clk_rm9200_main *clkmain = to_clk_rm9200_main(hw);
396
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200397 return clk_main_recalc_rate(clkmain->regmap, parent_rate);
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200398}
399
400static const struct clk_ops rm9200_main_ops = {
401 .prepare = clk_rm9200_main_prepare,
402 .is_prepared = clk_rm9200_main_is_prepared,
403 .recalc_rate = clk_rm9200_main_recalc_rate,
404};
405
Stephen Boydf5644f12016-06-01 14:31:22 -0700406static struct clk_hw * __init
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200407at91_clk_register_rm9200_main(struct regmap *regmap,
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200408 const char *name,
409 const char *parent_name)
410{
411 struct clk_rm9200_main *clkmain;
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200412 struct clk_init_data init;
Stephen Boydf5644f12016-06-01 14:31:22 -0700413 struct clk_hw *hw;
414 int ret;
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200415
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200416 if (!name)
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200417 return ERR_PTR(-EINVAL);
418
419 if (!parent_name)
Boris BREZILLON38d34c32013-10-11 10:44:49 +0200420 return ERR_PTR(-EINVAL);
421
422 clkmain = kzalloc(sizeof(*clkmain), GFP_KERNEL);
423 if (!clkmain)
424 return ERR_PTR(-ENOMEM);
425
426 init.name = name;
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200427 init.ops = &rm9200_main_ops;
428 init.parent_names = &parent_name;
429 init.num_parents = 1;
430 init.flags = 0;
Boris BREZILLON38d34c32013-10-11 10:44:49 +0200431
432 clkmain->hw.init = &init;
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200433 clkmain->regmap = regmap;
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200434
Stephen Boydf5644f12016-06-01 14:31:22 -0700435 hw = &clkmain->hw;
436 ret = clk_hw_register(NULL, &clkmain->hw);
437 if (ret) {
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200438 kfree(clkmain);
Stephen Boydf5644f12016-06-01 14:31:22 -0700439 hw = ERR_PTR(ret);
440 }
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200441
Stephen Boydf5644f12016-06-01 14:31:22 -0700442 return hw;
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200443}
444
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200445static void __init of_at91rm9200_clk_main_setup(struct device_node *np)
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200446{
Stephen Boydf5644f12016-06-01 14:31:22 -0700447 struct clk_hw *hw;
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200448 const char *parent_name;
449 const char *name = np->name;
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200450 struct regmap *regmap;
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200451
452 parent_name = of_clk_get_parent_name(np, 0);
453 of_property_read_string(np, "clock-output-names", &name);
454
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200455 regmap = syscon_node_to_regmap(of_get_parent(np));
456 if (IS_ERR(regmap))
457 return;
458
Stephen Boydf5644f12016-06-01 14:31:22 -0700459 hw = at91_clk_register_rm9200_main(regmap, name, parent_name);
460 if (IS_ERR(hw))
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200461 return;
462
Stephen Boydf5644f12016-06-01 14:31:22 -0700463 of_clk_add_hw_provider(np, of_clk_hw_simple_get, hw);
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200464}
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200465CLK_OF_DECLARE(at91rm9200_clk_main, "atmel,at91rm9200-clk-main",
466 of_at91rm9200_clk_main_setup);
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200467
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200468static inline bool clk_sam9x5_main_ready(struct regmap *regmap)
469{
470 unsigned int status;
471
472 regmap_read(regmap, AT91_PMC_SR, &status);
473
474 return status & AT91_PMC_MOSCSELS ? 1 : 0;
475}
476
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200477static int clk_sam9x5_main_prepare(struct clk_hw *hw)
478{
479 struct clk_sam9x5_main *clkmain = to_clk_sam9x5_main(hw);
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200480 struct regmap *regmap = clkmain->regmap;
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200481
Alexandre Belloni99a81702015-09-16 23:47:39 +0200482 while (!clk_sam9x5_main_ready(regmap))
483 cpu_relax();
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200484
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200485 return clk_main_probe_frequency(regmap);
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200486}
487
488static int clk_sam9x5_main_is_prepared(struct clk_hw *hw)
489{
490 struct clk_sam9x5_main *clkmain = to_clk_sam9x5_main(hw);
491
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200492 return clk_sam9x5_main_ready(clkmain->regmap);
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200493}
494
495static unsigned long clk_sam9x5_main_recalc_rate(struct clk_hw *hw,
496 unsigned long parent_rate)
497{
498 struct clk_sam9x5_main *clkmain = to_clk_sam9x5_main(hw);
499
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200500 return clk_main_recalc_rate(clkmain->regmap, parent_rate);
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200501}
502
503static int clk_sam9x5_main_set_parent(struct clk_hw *hw, u8 index)
504{
505 struct clk_sam9x5_main *clkmain = to_clk_sam9x5_main(hw);
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200506 struct regmap *regmap = clkmain->regmap;
507 unsigned int tmp;
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200508
509 if (index > 1)
510 return -EINVAL;
511
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200512 regmap_read(regmap, AT91_CKGR_MOR, &tmp);
513 tmp &= ~MOR_KEY_MASK;
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200514
515 if (index && !(tmp & AT91_PMC_MOSCSEL))
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200516 regmap_write(regmap, AT91_CKGR_MOR, tmp | AT91_PMC_MOSCSEL);
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200517 else if (!index && (tmp & AT91_PMC_MOSCSEL))
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200518 regmap_write(regmap, AT91_CKGR_MOR, tmp & ~AT91_PMC_MOSCSEL);
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200519
Alexandre Belloni99a81702015-09-16 23:47:39 +0200520 while (!clk_sam9x5_main_ready(regmap))
521 cpu_relax();
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200522
523 return 0;
524}
525
526static u8 clk_sam9x5_main_get_parent(struct clk_hw *hw)
527{
528 struct clk_sam9x5_main *clkmain = to_clk_sam9x5_main(hw);
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200529 unsigned int status;
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200530
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200531 regmap_read(clkmain->regmap, AT91_CKGR_MOR, &status);
532
533 return status & AT91_PMC_MOSCEN ? 1 : 0;
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200534}
535
536static const struct clk_ops sam9x5_main_ops = {
537 .prepare = clk_sam9x5_main_prepare,
538 .is_prepared = clk_sam9x5_main_is_prepared,
539 .recalc_rate = clk_sam9x5_main_recalc_rate,
540 .set_parent = clk_sam9x5_main_set_parent,
541 .get_parent = clk_sam9x5_main_get_parent,
542};
543
Stephen Boydf5644f12016-06-01 14:31:22 -0700544static struct clk_hw * __init
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200545at91_clk_register_sam9x5_main(struct regmap *regmap,
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200546 const char *name,
547 const char **parent_names,
548 int num_parents)
549{
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200550 struct clk_sam9x5_main *clkmain;
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200551 struct clk_init_data init;
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200552 unsigned int status;
Stephen Boydf5644f12016-06-01 14:31:22 -0700553 struct clk_hw *hw;
554 int ret;
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200555
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200556 if (!name)
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200557 return ERR_PTR(-EINVAL);
558
559 if (!parent_names || !num_parents)
560 return ERR_PTR(-EINVAL);
561
562 clkmain = kzalloc(sizeof(*clkmain), GFP_KERNEL);
563 if (!clkmain)
564 return ERR_PTR(-ENOMEM);
565
566 init.name = name;
567 init.ops = &sam9x5_main_ops;
568 init.parent_names = parent_names;
569 init.num_parents = num_parents;
570 init.flags = CLK_SET_PARENT_GATE;
571
572 clkmain->hw.init = &init;
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200573 clkmain->regmap = regmap;
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200574 regmap_read(clkmain->regmap, AT91_CKGR_MOR, &status);
575 clkmain->parent = status & AT91_PMC_MOSCEN ? 1 : 0;
Boris BREZILLON38d34c32013-10-11 10:44:49 +0200576
Stephen Boydf5644f12016-06-01 14:31:22 -0700577 hw = &clkmain->hw;
578 ret = clk_hw_register(NULL, &clkmain->hw);
579 if (ret) {
Boris BREZILLON38d34c32013-10-11 10:44:49 +0200580 kfree(clkmain);
Stephen Boydf5644f12016-06-01 14:31:22 -0700581 hw = ERR_PTR(ret);
582 }
Boris BREZILLON38d34c32013-10-11 10:44:49 +0200583
Stephen Boydf5644f12016-06-01 14:31:22 -0700584 return hw;
Boris BREZILLON38d34c32013-10-11 10:44:49 +0200585}
586
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200587static void __init of_at91sam9x5_clk_main_setup(struct device_node *np)
Boris BREZILLON38d34c32013-10-11 10:44:49 +0200588{
Stephen Boydf5644f12016-06-01 14:31:22 -0700589 struct clk_hw *hw;
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200590 const char *parent_names[2];
Stephen Boyd8c1b1e52016-02-19 17:29:17 -0800591 unsigned int num_parents;
Boris BREZILLON38d34c32013-10-11 10:44:49 +0200592 const char *name = np->name;
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200593 struct regmap *regmap;
Boris BREZILLON38d34c32013-10-11 10:44:49 +0200594
Geert Uytterhoeven51a43be2015-05-29 11:25:45 +0200595 num_parents = of_clk_get_parent_count(np);
Stephen Boyd8c1b1e52016-02-19 17:29:17 -0800596 if (num_parents == 0 || num_parents > 2)
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200597 return;
598
Dinh Nguyenf0557fb2015-07-06 22:59:01 -0500599 of_clk_parent_fill(np, parent_names, num_parents);
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200600 regmap = syscon_node_to_regmap(of_get_parent(np));
601 if (IS_ERR(regmap))
602 return;
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200603
Boris BREZILLON38d34c32013-10-11 10:44:49 +0200604 of_property_read_string(np, "clock-output-names", &name);
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200605
Stephen Boydf5644f12016-06-01 14:31:22 -0700606 hw = at91_clk_register_sam9x5_main(regmap, name, parent_names,
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200607 num_parents);
Stephen Boydf5644f12016-06-01 14:31:22 -0700608 if (IS_ERR(hw))
Boris BREZILLON38d34c32013-10-11 10:44:49 +0200609 return;
610
Stephen Boydf5644f12016-06-01 14:31:22 -0700611 of_clk_add_hw_provider(np, of_clk_hw_simple_get, hw);
Boris BREZILLON38d34c32013-10-11 10:44:49 +0200612}
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200613CLK_OF_DECLARE(at91sam9x5_clk_main, "atmel,at91sam9x5-clk-main",
614 of_at91sam9x5_clk_main_setup);