blob: 4bfc94d6c26ea3643aa87caeb359f7fc363aedf6 [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
131static struct clk * __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 *clk = NULL;
139 struct clk_init_data init;
140
Alexandre Belloni99a81702015-09-16 23:47:39 +0200141 if (!name || !parent_name)
Boris BREZILLON38d34c32013-10-11 10:44:49 +0200142 return ERR_PTR(-EINVAL);
143
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200144 osc = kzalloc(sizeof(*osc), GFP_KERNEL);
145 if (!osc)
146 return ERR_PTR(-ENOMEM);
147
148 init.name = name;
149 init.ops = &main_osc_ops;
150 init.parent_names = &parent_name;
151 init.num_parents = 1;
152 init.flags = CLK_IGNORE_UNUSED;
153
154 osc->hw.init = &init;
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200155 osc->regmap = regmap;
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200156
157 if (bypass)
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200158 regmap_update_bits(regmap,
159 AT91_CKGR_MOR, MOR_KEY_MASK |
160 AT91_PMC_MOSCEN,
161 AT91_PMC_OSCBYPASS | AT91_PMC_KEY);
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200162
163 clk = clk_register(NULL, &osc->hw);
Alexandre Belloni99a81702015-09-16 23:47:39 +0200164 if (IS_ERR(clk))
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200165 kfree(osc);
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200166
167 return clk;
168}
169
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200170static void __init of_at91rm9200_clk_main_osc_setup(struct device_node *np)
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200171{
172 struct clk *clk;
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200173 const char *name = np->name;
174 const char *parent_name;
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200175 struct regmap *regmap;
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200176 bool bypass;
177
178 of_property_read_string(np, "clock-output-names", &name);
179 bypass = of_property_read_bool(np, "atmel,osc-bypass");
180 parent_name = of_clk_get_parent_name(np, 0);
181
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200182 regmap = syscon_node_to_regmap(of_get_parent(np));
183 if (IS_ERR(regmap))
184 return;
185
Alexandre Belloni99a81702015-09-16 23:47:39 +0200186 clk = at91_clk_register_main_osc(regmap, name, parent_name, bypass);
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200187 if (IS_ERR(clk))
188 return;
189
190 of_clk_add_provider(np, of_clk_src_simple_get, clk);
191}
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200192CLK_OF_DECLARE(at91rm9200_clk_main_osc, "atmel,at91rm9200-clk-main-osc",
193 of_at91rm9200_clk_main_osc_setup);
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200194
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200195static bool clk_main_rc_osc_ready(struct regmap *regmap)
196{
197 unsigned int status;
198
199 regmap_read(regmap, AT91_PMC_SR, &status);
200
201 return status & AT91_PMC_MOSCRCS;
202}
203
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200204static int clk_main_rc_osc_prepare(struct clk_hw *hw)
205{
206 struct clk_main_rc_osc *osc = to_clk_main_rc_osc(hw);
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200207 struct regmap *regmap = osc->regmap;
208 unsigned int mor;
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200209
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200210 regmap_read(regmap, AT91_CKGR_MOR, &mor);
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200211
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200212 if (!(mor & AT91_PMC_MOSCRCEN))
213 regmap_update_bits(regmap, AT91_CKGR_MOR,
214 MOR_KEY_MASK | AT91_PMC_MOSCRCEN,
215 AT91_PMC_MOSCRCEN | AT91_PMC_KEY);
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200216
Alexandre Belloni99a81702015-09-16 23:47:39 +0200217 while (!clk_main_rc_osc_ready(regmap))
218 cpu_relax();
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200219
220 return 0;
221}
222
223static void clk_main_rc_osc_unprepare(struct clk_hw *hw)
224{
225 struct clk_main_rc_osc *osc = to_clk_main_rc_osc(hw);
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200226 struct regmap *regmap = osc->regmap;
227 unsigned int mor;
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200228
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200229 regmap_read(regmap, AT91_CKGR_MOR, &mor);
230
231 if (!(mor & AT91_PMC_MOSCRCEN))
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200232 return;
233
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200234 regmap_update_bits(regmap, AT91_CKGR_MOR,
235 MOR_KEY_MASK | AT91_PMC_MOSCRCEN, AT91_PMC_KEY);
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200236}
237
238static int clk_main_rc_osc_is_prepared(struct clk_hw *hw)
239{
240 struct clk_main_rc_osc *osc = to_clk_main_rc_osc(hw);
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200241 struct regmap *regmap = osc->regmap;
242 unsigned int mor, status;
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200243
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200244 regmap_read(regmap, AT91_CKGR_MOR, &mor);
245 regmap_read(regmap, AT91_PMC_SR, &status);
246
247 return (mor & AT91_PMC_MOSCRCEN) && (status & AT91_PMC_MOSCRCS);
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200248}
249
250static unsigned long clk_main_rc_osc_recalc_rate(struct clk_hw *hw,
251 unsigned long parent_rate)
252{
253 struct clk_main_rc_osc *osc = to_clk_main_rc_osc(hw);
254
255 return osc->frequency;
256}
257
258static unsigned long clk_main_rc_osc_recalc_accuracy(struct clk_hw *hw,
259 unsigned long parent_acc)
260{
261 struct clk_main_rc_osc *osc = to_clk_main_rc_osc(hw);
262
263 return osc->accuracy;
264}
265
266static const struct clk_ops main_rc_osc_ops = {
267 .prepare = clk_main_rc_osc_prepare,
268 .unprepare = clk_main_rc_osc_unprepare,
269 .is_prepared = clk_main_rc_osc_is_prepared,
270 .recalc_rate = clk_main_rc_osc_recalc_rate,
271 .recalc_accuracy = clk_main_rc_osc_recalc_accuracy,
272};
273
274static struct clk * __init
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200275at91_clk_register_main_rc_osc(struct regmap *regmap,
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200276 const char *name,
277 u32 frequency, u32 accuracy)
278{
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200279 struct clk_main_rc_osc *osc;
280 struct clk *clk = NULL;
281 struct clk_init_data init;
282
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200283 if (!name || !frequency)
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200284 return ERR_PTR(-EINVAL);
285
286 osc = kzalloc(sizeof(*osc), GFP_KERNEL);
287 if (!osc)
288 return ERR_PTR(-ENOMEM);
289
290 init.name = name;
291 init.ops = &main_rc_osc_ops;
292 init.parent_names = NULL;
293 init.num_parents = 0;
294 init.flags = CLK_IS_ROOT | CLK_IGNORE_UNUSED;
295
296 osc->hw.init = &init;
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200297 osc->regmap = regmap;
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200298 osc->frequency = frequency;
299 osc->accuracy = accuracy;
300
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200301 clk = clk_register(NULL, &osc->hw);
Alexandre Belloni99a81702015-09-16 23:47:39 +0200302 if (IS_ERR(clk))
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200303 kfree(osc);
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200304
305 return clk;
306}
307
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200308static void __init of_at91sam9x5_clk_main_rc_osc_setup(struct device_node *np)
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200309{
310 struct clk *clk;
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200311 u32 frequency = 0;
312 u32 accuracy = 0;
313 const char *name = np->name;
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200314 struct regmap *regmap;
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200315
316 of_property_read_string(np, "clock-output-names", &name);
317 of_property_read_u32(np, "clock-frequency", &frequency);
318 of_property_read_u32(np, "clock-accuracy", &accuracy);
319
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200320 regmap = syscon_node_to_regmap(of_get_parent(np));
321 if (IS_ERR(regmap))
322 return;
323
Alexandre Belloni99a81702015-09-16 23:47:39 +0200324 clk = at91_clk_register_main_rc_osc(regmap, name, frequency, accuracy);
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200325 if (IS_ERR(clk))
326 return;
327
328 of_clk_add_provider(np, of_clk_src_simple_get, clk);
329}
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200330CLK_OF_DECLARE(at91sam9x5_clk_main_rc_osc, "atmel,at91sam9x5-clk-main-rc-osc",
331 of_at91sam9x5_clk_main_rc_osc_setup);
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200332
333
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200334static int clk_main_probe_frequency(struct regmap *regmap)
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200335{
336 unsigned long prep_time, timeout;
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200337 unsigned int mcfr;
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200338
339 timeout = jiffies + usecs_to_jiffies(MAINFRDY_TIMEOUT);
340 do {
341 prep_time = jiffies;
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200342 regmap_read(regmap, AT91_CKGR_MCFR, &mcfr);
343 if (mcfr & AT91_PMC_MAINRDY)
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200344 return 0;
345 usleep_range(MAINF_LOOP_MIN_WAIT, MAINF_LOOP_MAX_WAIT);
346 } while (time_before(prep_time, timeout));
347
348 return -ETIMEDOUT;
349}
350
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200351static unsigned long clk_main_recalc_rate(struct regmap *regmap,
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200352 unsigned long parent_rate)
353{
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200354 unsigned int mcfr;
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200355
356 if (parent_rate)
357 return parent_rate;
358
Alexandre Belloni4da66b62014-07-01 16:12:12 +0200359 pr_warn("Main crystal frequency not set, using approximate value\n");
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200360 regmap_read(regmap, AT91_CKGR_MCFR, &mcfr);
361 if (!(mcfr & AT91_PMC_MAINRDY))
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200362 return 0;
363
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200364 return ((mcfr & AT91_PMC_MAINF) * SLOW_CLOCK_FREQ) / MAINF_DIV;
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200365}
366
367static int clk_rm9200_main_prepare(struct clk_hw *hw)
368{
369 struct clk_rm9200_main *clkmain = to_clk_rm9200_main(hw);
370
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200371 return clk_main_probe_frequency(clkmain->regmap);
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200372}
373
374static int clk_rm9200_main_is_prepared(struct clk_hw *hw)
375{
376 struct clk_rm9200_main *clkmain = to_clk_rm9200_main(hw);
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200377 unsigned int status;
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200378
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200379 regmap_read(clkmain->regmap, AT91_CKGR_MCFR, &status);
380
381 return status & AT91_PMC_MAINRDY ? 1 : 0;
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200382}
383
384static unsigned long clk_rm9200_main_recalc_rate(struct clk_hw *hw,
385 unsigned long parent_rate)
386{
387 struct clk_rm9200_main *clkmain = to_clk_rm9200_main(hw);
388
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200389 return clk_main_recalc_rate(clkmain->regmap, parent_rate);
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200390}
391
392static const struct clk_ops rm9200_main_ops = {
393 .prepare = clk_rm9200_main_prepare,
394 .is_prepared = clk_rm9200_main_is_prepared,
395 .recalc_rate = clk_rm9200_main_recalc_rate,
396};
397
398static struct clk * __init
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200399at91_clk_register_rm9200_main(struct regmap *regmap,
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200400 const char *name,
401 const char *parent_name)
402{
403 struct clk_rm9200_main *clkmain;
404 struct clk *clk = NULL;
405 struct clk_init_data init;
406
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200407 if (!name)
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200408 return ERR_PTR(-EINVAL);
409
410 if (!parent_name)
Boris BREZILLON38d34c32013-10-11 10:44:49 +0200411 return ERR_PTR(-EINVAL);
412
413 clkmain = kzalloc(sizeof(*clkmain), GFP_KERNEL);
414 if (!clkmain)
415 return ERR_PTR(-ENOMEM);
416
417 init.name = name;
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200418 init.ops = &rm9200_main_ops;
419 init.parent_names = &parent_name;
420 init.num_parents = 1;
421 init.flags = 0;
Boris BREZILLON38d34c32013-10-11 10:44:49 +0200422
423 clkmain->hw.init = &init;
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200424 clkmain->regmap = regmap;
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200425
426 clk = clk_register(NULL, &clkmain->hw);
427 if (IS_ERR(clk))
428 kfree(clkmain);
429
430 return clk;
431}
432
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200433static void __init of_at91rm9200_clk_main_setup(struct device_node *np)
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200434{
435 struct clk *clk;
436 const char *parent_name;
437 const char *name = np->name;
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200438 struct regmap *regmap;
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200439
440 parent_name = of_clk_get_parent_name(np, 0);
441 of_property_read_string(np, "clock-output-names", &name);
442
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200443 regmap = syscon_node_to_regmap(of_get_parent(np));
444 if (IS_ERR(regmap))
445 return;
446
447 clk = at91_clk_register_rm9200_main(regmap, name, parent_name);
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200448 if (IS_ERR(clk))
449 return;
450
451 of_clk_add_provider(np, of_clk_src_simple_get, clk);
452}
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200453CLK_OF_DECLARE(at91rm9200_clk_main, "atmel,at91rm9200-clk-main",
454 of_at91rm9200_clk_main_setup);
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200455
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200456static inline bool clk_sam9x5_main_ready(struct regmap *regmap)
457{
458 unsigned int status;
459
460 regmap_read(regmap, AT91_PMC_SR, &status);
461
462 return status & AT91_PMC_MOSCSELS ? 1 : 0;
463}
464
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200465static int clk_sam9x5_main_prepare(struct clk_hw *hw)
466{
467 struct clk_sam9x5_main *clkmain = to_clk_sam9x5_main(hw);
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200468 struct regmap *regmap = clkmain->regmap;
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200469
Alexandre Belloni99a81702015-09-16 23:47:39 +0200470 while (!clk_sam9x5_main_ready(regmap))
471 cpu_relax();
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200472
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200473 return clk_main_probe_frequency(regmap);
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200474}
475
476static int clk_sam9x5_main_is_prepared(struct clk_hw *hw)
477{
478 struct clk_sam9x5_main *clkmain = to_clk_sam9x5_main(hw);
479
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200480 return clk_sam9x5_main_ready(clkmain->regmap);
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200481}
482
483static unsigned long clk_sam9x5_main_recalc_rate(struct clk_hw *hw,
484 unsigned long parent_rate)
485{
486 struct clk_sam9x5_main *clkmain = to_clk_sam9x5_main(hw);
487
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200488 return clk_main_recalc_rate(clkmain->regmap, parent_rate);
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200489}
490
491static int clk_sam9x5_main_set_parent(struct clk_hw *hw, u8 index)
492{
493 struct clk_sam9x5_main *clkmain = to_clk_sam9x5_main(hw);
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200494 struct regmap *regmap = clkmain->regmap;
495 unsigned int tmp;
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200496
497 if (index > 1)
498 return -EINVAL;
499
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200500 regmap_read(regmap, AT91_CKGR_MOR, &tmp);
501 tmp &= ~MOR_KEY_MASK;
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200502
503 if (index && !(tmp & AT91_PMC_MOSCSEL))
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200504 regmap_write(regmap, AT91_CKGR_MOR, tmp | AT91_PMC_MOSCSEL);
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200505 else if (!index && (tmp & AT91_PMC_MOSCSEL))
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200506 regmap_write(regmap, AT91_CKGR_MOR, tmp & ~AT91_PMC_MOSCSEL);
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200507
Alexandre Belloni99a81702015-09-16 23:47:39 +0200508 while (!clk_sam9x5_main_ready(regmap))
509 cpu_relax();
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200510
511 return 0;
512}
513
514static u8 clk_sam9x5_main_get_parent(struct clk_hw *hw)
515{
516 struct clk_sam9x5_main *clkmain = to_clk_sam9x5_main(hw);
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200517 unsigned int status;
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200518
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200519 regmap_read(clkmain->regmap, AT91_CKGR_MOR, &status);
520
521 return status & AT91_PMC_MOSCEN ? 1 : 0;
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200522}
523
524static const struct clk_ops sam9x5_main_ops = {
525 .prepare = clk_sam9x5_main_prepare,
526 .is_prepared = clk_sam9x5_main_is_prepared,
527 .recalc_rate = clk_sam9x5_main_recalc_rate,
528 .set_parent = clk_sam9x5_main_set_parent,
529 .get_parent = clk_sam9x5_main_get_parent,
530};
531
532static struct clk * __init
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200533at91_clk_register_sam9x5_main(struct regmap *regmap,
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200534 const char *name,
535 const char **parent_names,
536 int num_parents)
537{
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200538 struct clk_sam9x5_main *clkmain;
539 struct clk *clk = NULL;
540 struct clk_init_data init;
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200541 unsigned int status;
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200542
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200543 if (!name)
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200544 return ERR_PTR(-EINVAL);
545
546 if (!parent_names || !num_parents)
547 return ERR_PTR(-EINVAL);
548
549 clkmain = kzalloc(sizeof(*clkmain), GFP_KERNEL);
550 if (!clkmain)
551 return ERR_PTR(-ENOMEM);
552
553 init.name = name;
554 init.ops = &sam9x5_main_ops;
555 init.parent_names = parent_names;
556 init.num_parents = num_parents;
557 init.flags = CLK_SET_PARENT_GATE;
558
559 clkmain->hw.init = &init;
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200560 clkmain->regmap = regmap;
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200561 regmap_read(clkmain->regmap, AT91_CKGR_MOR, &status);
562 clkmain->parent = status & AT91_PMC_MOSCEN ? 1 : 0;
Boris BREZILLON38d34c32013-10-11 10:44:49 +0200563
564 clk = clk_register(NULL, &clkmain->hw);
Alexandre Belloni99a81702015-09-16 23:47:39 +0200565 if (IS_ERR(clk))
Boris BREZILLON38d34c32013-10-11 10:44:49 +0200566 kfree(clkmain);
Boris BREZILLON38d34c32013-10-11 10:44:49 +0200567
568 return clk;
569}
570
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200571static void __init of_at91sam9x5_clk_main_setup(struct device_node *np)
Boris BREZILLON38d34c32013-10-11 10:44:49 +0200572{
573 struct clk *clk;
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200574 const char *parent_names[2];
575 int num_parents;
Boris BREZILLON38d34c32013-10-11 10:44:49 +0200576 const char *name = np->name;
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200577 struct regmap *regmap;
Boris BREZILLON38d34c32013-10-11 10:44:49 +0200578
Geert Uytterhoeven51a43be2015-05-29 11:25:45 +0200579 num_parents = of_clk_get_parent_count(np);
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200580 if (num_parents <= 0 || num_parents > 2)
581 return;
582
Dinh Nguyenf0557fb2015-07-06 22:59:01 -0500583 of_clk_parent_fill(np, parent_names, num_parents);
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200584 regmap = syscon_node_to_regmap(of_get_parent(np));
585 if (IS_ERR(regmap))
586 return;
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200587
Boris BREZILLON38d34c32013-10-11 10:44:49 +0200588 of_property_read_string(np, "clock-output-names", &name);
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200589
Alexandre Belloni99a81702015-09-16 23:47:39 +0200590 clk = at91_clk_register_sam9x5_main(regmap, name, parent_names,
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200591 num_parents);
Boris BREZILLON38d34c32013-10-11 10:44:49 +0200592 if (IS_ERR(clk))
593 return;
594
595 of_clk_add_provider(np, of_clk_src_simple_get, clk);
596}
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200597CLK_OF_DECLARE(at91sam9x5_clk_main, "atmel,at91sam9x5-clk-main",
598 of_at91sam9x5_clk_main_setup);