blob: 37c22667e8319011aa5528613c26a9e0544e3e6e [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Boris BREZILLON38d34c32013-10-11 10:44:49 +02002/*
3 * Copyright (C) 2013 Boris BREZILLON <b.brezillon@overkiz.com>
Boris BREZILLON38d34c32013-10-11 10:44:49 +02004 */
5
6#include <linux/clk-provider.h>
7#include <linux/clkdev.h>
8#include <linux/clk/at91_pmc.h>
9#include <linux/delay.h>
Boris Brezillon1bdf0232014-09-07 08:14:29 +020010#include <linux/mfd/syscon.h>
11#include <linux/regmap.h>
Boris BREZILLON38d34c32013-10-11 10:44:49 +020012
13#include "pmc.h"
14
15#define SLOW_CLOCK_FREQ 32768
16#define MAINF_DIV 16
17#define MAINFRDY_TIMEOUT (((MAINF_DIV + 1) * USEC_PER_SEC) / \
18 SLOW_CLOCK_FREQ)
19#define MAINF_LOOP_MIN_WAIT (USEC_PER_SEC / SLOW_CLOCK_FREQ)
20#define MAINF_LOOP_MAX_WAIT MAINFRDY_TIMEOUT
21
Boris BREZILLON27cb1c22014-05-07 18:00:08 +020022#define MOR_KEY_MASK (0xff << 16)
23
Eugen Hristev69a6bcd2019-09-09 15:30:34 +000024#define clk_main_parent_select(s) (((s) & \
25 (AT91_PMC_MOSCEN | \
26 AT91_PMC_OSCBYPASS)) ? 1 : 0)
27
Boris BREZILLON27cb1c22014-05-07 18:00:08 +020028struct clk_main_osc {
Boris BREZILLON38d34c32013-10-11 10:44:49 +020029 struct clk_hw hw;
Boris Brezillon1bdf0232014-09-07 08:14:29 +020030 struct regmap *regmap;
Boris BREZILLON38d34c32013-10-11 10:44:49 +020031};
32
Boris BREZILLON27cb1c22014-05-07 18:00:08 +020033#define to_clk_main_osc(hw) container_of(hw, struct clk_main_osc, hw)
Boris BREZILLON38d34c32013-10-11 10:44:49 +020034
Boris BREZILLON27cb1c22014-05-07 18:00:08 +020035struct clk_main_rc_osc {
36 struct clk_hw hw;
Boris Brezillon1bdf0232014-09-07 08:14:29 +020037 struct regmap *regmap;
Boris BREZILLON27cb1c22014-05-07 18:00:08 +020038 unsigned long frequency;
39 unsigned long accuracy;
40};
41
42#define to_clk_main_rc_osc(hw) container_of(hw, struct clk_main_rc_osc, hw)
43
44struct clk_rm9200_main {
45 struct clk_hw hw;
Boris Brezillon1bdf0232014-09-07 08:14:29 +020046 struct regmap *regmap;
Boris BREZILLON27cb1c22014-05-07 18:00:08 +020047};
48
49#define to_clk_rm9200_main(hw) container_of(hw, struct clk_rm9200_main, hw)
50
51struct clk_sam9x5_main {
52 struct clk_hw hw;
Boris Brezillon1bdf0232014-09-07 08:14:29 +020053 struct regmap *regmap;
Boris BREZILLON27cb1c22014-05-07 18:00:08 +020054 u8 parent;
55};
56
57#define to_clk_sam9x5_main(hw) container_of(hw, struct clk_sam9x5_main, hw)
58
Boris Brezillon1bdf0232014-09-07 08:14:29 +020059static inline bool clk_main_osc_ready(struct regmap *regmap)
60{
61 unsigned int status;
62
63 regmap_read(regmap, AT91_PMC_SR, &status);
64
65 return status & AT91_PMC_MOSCS;
66}
67
Boris BREZILLON27cb1c22014-05-07 18:00:08 +020068static int clk_main_osc_prepare(struct clk_hw *hw)
Boris BREZILLON38d34c32013-10-11 10:44:49 +020069{
Boris BREZILLON27cb1c22014-05-07 18:00:08 +020070 struct clk_main_osc *osc = to_clk_main_osc(hw);
Boris Brezillon1bdf0232014-09-07 08:14:29 +020071 struct regmap *regmap = osc->regmap;
Boris BREZILLON38d34c32013-10-11 10:44:49 +020072 u32 tmp;
73
Boris Brezillon1bdf0232014-09-07 08:14:29 +020074 regmap_read(regmap, AT91_CKGR_MOR, &tmp);
75 tmp &= ~MOR_KEY_MASK;
76
Boris BREZILLON27cb1c22014-05-07 18:00:08 +020077 if (tmp & AT91_PMC_OSCBYPASS)
Boris BREZILLON38d34c32013-10-11 10:44:49 +020078 return 0;
79
Boris BREZILLON27cb1c22014-05-07 18:00:08 +020080 if (!(tmp & AT91_PMC_MOSCEN)) {
81 tmp |= AT91_PMC_MOSCEN | AT91_PMC_KEY;
Boris Brezillon1bdf0232014-09-07 08:14:29 +020082 regmap_write(regmap, AT91_CKGR_MOR, tmp);
Boris BREZILLON27cb1c22014-05-07 18:00:08 +020083 }
84
Alexandre Belloni99a81702015-09-16 23:47:39 +020085 while (!clk_main_osc_ready(regmap))
86 cpu_relax();
Boris BREZILLON38d34c32013-10-11 10:44:49 +020087
88 return 0;
89}
90
Boris BREZILLON27cb1c22014-05-07 18:00:08 +020091static void clk_main_osc_unprepare(struct clk_hw *hw)
Boris BREZILLON38d34c32013-10-11 10:44:49 +020092{
Boris BREZILLON27cb1c22014-05-07 18:00:08 +020093 struct clk_main_osc *osc = to_clk_main_osc(hw);
Boris Brezillon1bdf0232014-09-07 08:14:29 +020094 struct regmap *regmap = osc->regmap;
95 u32 tmp;
Boris BREZILLON38d34c32013-10-11 10:44:49 +020096
Boris Brezillon1bdf0232014-09-07 08:14:29 +020097 regmap_read(regmap, AT91_CKGR_MOR, &tmp);
Boris BREZILLON27cb1c22014-05-07 18:00:08 +020098 if (tmp & AT91_PMC_OSCBYPASS)
99 return;
100
101 if (!(tmp & AT91_PMC_MOSCEN))
102 return;
103
104 tmp &= ~(AT91_PMC_KEY | AT91_PMC_MOSCEN);
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200105 regmap_write(regmap, AT91_CKGR_MOR, tmp | AT91_PMC_KEY);
Boris BREZILLON38d34c32013-10-11 10:44:49 +0200106}
107
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200108static int clk_main_osc_is_prepared(struct clk_hw *hw)
Boris BREZILLON38d34c32013-10-11 10:44:49 +0200109{
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200110 struct clk_main_osc *osc = to_clk_main_osc(hw);
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200111 struct regmap *regmap = osc->regmap;
112 u32 tmp, status;
Boris BREZILLON38d34c32013-10-11 10:44:49 +0200113
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200114 regmap_read(regmap, AT91_CKGR_MOR, &tmp);
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200115 if (tmp & AT91_PMC_OSCBYPASS)
116 return 1;
Boris BREZILLON38d34c32013-10-11 10:44:49 +0200117
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200118 regmap_read(regmap, AT91_PMC_SR, &status);
119
Eugen Hristev69a6bcd2019-09-09 15:30:34 +0000120 return (status & AT91_PMC_MOSCS) && clk_main_parent_select(tmp);
Boris BREZILLON38d34c32013-10-11 10:44:49 +0200121}
122
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200123static const struct clk_ops main_osc_ops = {
124 .prepare = clk_main_osc_prepare,
125 .unprepare = clk_main_osc_unprepare,
126 .is_prepared = clk_main_osc_is_prepared,
Boris BREZILLON38d34c32013-10-11 10:44:49 +0200127};
128
Alexandre Bellonib2e39dc2018-10-16 16:21:44 +0200129struct clk_hw * __init
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200130at91_clk_register_main_osc(struct regmap *regmap,
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200131 const char *name,
132 const char *parent_name,
133 bool bypass)
Boris BREZILLON38d34c32013-10-11 10:44:49 +0200134{
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200135 struct clk_main_osc *osc;
Boris BREZILLON38d34c32013-10-11 10:44:49 +0200136 struct clk_init_data init;
Stephen Boydf5644f12016-06-01 14:31:22 -0700137 struct clk_hw *hw;
138 int ret;
Boris BREZILLON38d34c32013-10-11 10:44:49 +0200139
Alexandre Belloni99a81702015-09-16 23:47:39 +0200140 if (!name || !parent_name)
Boris BREZILLON38d34c32013-10-11 10:44:49 +0200141 return ERR_PTR(-EINVAL);
142
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200143 osc = kzalloc(sizeof(*osc), GFP_KERNEL);
144 if (!osc)
145 return ERR_PTR(-ENOMEM);
146
147 init.name = name;
148 init.ops = &main_osc_ops;
149 init.parent_names = &parent_name;
150 init.num_parents = 1;
151 init.flags = CLK_IGNORE_UNUSED;
152
153 osc->hw.init = &init;
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200154 osc->regmap = regmap;
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200155
156 if (bypass)
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200157 regmap_update_bits(regmap,
158 AT91_CKGR_MOR, MOR_KEY_MASK |
Eugen Hristev263eaf82019-09-09 15:30:31 +0000159 AT91_PMC_OSCBYPASS,
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200160 AT91_PMC_OSCBYPASS | AT91_PMC_KEY);
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200161
Stephen Boydf5644f12016-06-01 14:31:22 -0700162 hw = &osc->hw;
163 ret = clk_hw_register(NULL, &osc->hw);
164 if (ret) {
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200165 kfree(osc);
Stephen Boydf5644f12016-06-01 14:31:22 -0700166 hw = ERR_PTR(ret);
167 }
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200168
Stephen Boydf5644f12016-06-01 14:31:22 -0700169 return hw;
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200170}
171
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200172static bool clk_main_rc_osc_ready(struct regmap *regmap)
173{
174 unsigned int status;
175
176 regmap_read(regmap, AT91_PMC_SR, &status);
177
178 return status & AT91_PMC_MOSCRCS;
179}
180
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200181static int clk_main_rc_osc_prepare(struct clk_hw *hw)
182{
183 struct clk_main_rc_osc *osc = to_clk_main_rc_osc(hw);
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200184 struct regmap *regmap = osc->regmap;
185 unsigned int mor;
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200186
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200187 regmap_read(regmap, AT91_CKGR_MOR, &mor);
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200188
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200189 if (!(mor & AT91_PMC_MOSCRCEN))
190 regmap_update_bits(regmap, AT91_CKGR_MOR,
191 MOR_KEY_MASK | AT91_PMC_MOSCRCEN,
192 AT91_PMC_MOSCRCEN | AT91_PMC_KEY);
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200193
Alexandre Belloni99a81702015-09-16 23:47:39 +0200194 while (!clk_main_rc_osc_ready(regmap))
195 cpu_relax();
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200196
197 return 0;
198}
199
200static void clk_main_rc_osc_unprepare(struct clk_hw *hw)
201{
202 struct clk_main_rc_osc *osc = to_clk_main_rc_osc(hw);
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200203 struct regmap *regmap = osc->regmap;
204 unsigned int mor;
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200205
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200206 regmap_read(regmap, AT91_CKGR_MOR, &mor);
207
208 if (!(mor & AT91_PMC_MOSCRCEN))
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200209 return;
210
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200211 regmap_update_bits(regmap, AT91_CKGR_MOR,
212 MOR_KEY_MASK | AT91_PMC_MOSCRCEN, AT91_PMC_KEY);
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200213}
214
215static int clk_main_rc_osc_is_prepared(struct clk_hw *hw)
216{
217 struct clk_main_rc_osc *osc = to_clk_main_rc_osc(hw);
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200218 struct regmap *regmap = osc->regmap;
219 unsigned int mor, status;
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200220
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200221 regmap_read(regmap, AT91_CKGR_MOR, &mor);
222 regmap_read(regmap, AT91_PMC_SR, &status);
223
224 return (mor & AT91_PMC_MOSCRCEN) && (status & AT91_PMC_MOSCRCS);
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200225}
226
227static unsigned long clk_main_rc_osc_recalc_rate(struct clk_hw *hw,
228 unsigned long parent_rate)
229{
230 struct clk_main_rc_osc *osc = to_clk_main_rc_osc(hw);
231
232 return osc->frequency;
233}
234
235static unsigned long clk_main_rc_osc_recalc_accuracy(struct clk_hw *hw,
236 unsigned long parent_acc)
237{
238 struct clk_main_rc_osc *osc = to_clk_main_rc_osc(hw);
239
240 return osc->accuracy;
241}
242
243static const struct clk_ops main_rc_osc_ops = {
244 .prepare = clk_main_rc_osc_prepare,
245 .unprepare = clk_main_rc_osc_unprepare,
246 .is_prepared = clk_main_rc_osc_is_prepared,
247 .recalc_rate = clk_main_rc_osc_recalc_rate,
248 .recalc_accuracy = clk_main_rc_osc_recalc_accuracy,
249};
250
Alexandre Bellonib2e39dc2018-10-16 16:21:44 +0200251struct clk_hw * __init
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200252at91_clk_register_main_rc_osc(struct regmap *regmap,
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200253 const char *name,
254 u32 frequency, u32 accuracy)
255{
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200256 struct clk_main_rc_osc *osc;
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200257 struct clk_init_data init;
Stephen Boydf5644f12016-06-01 14:31:22 -0700258 struct clk_hw *hw;
259 int ret;
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200260
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200261 if (!name || !frequency)
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200262 return ERR_PTR(-EINVAL);
263
264 osc = kzalloc(sizeof(*osc), GFP_KERNEL);
265 if (!osc)
266 return ERR_PTR(-ENOMEM);
267
268 init.name = name;
269 init.ops = &main_rc_osc_ops;
270 init.parent_names = NULL;
271 init.num_parents = 0;
Stephen Boyda9bb2ef2016-03-01 10:59:46 -0800272 init.flags = CLK_IGNORE_UNUSED;
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200273
274 osc->hw.init = &init;
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200275 osc->regmap = regmap;
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200276 osc->frequency = frequency;
277 osc->accuracy = accuracy;
278
Stephen Boydf5644f12016-06-01 14:31:22 -0700279 hw = &osc->hw;
280 ret = clk_hw_register(NULL, hw);
281 if (ret) {
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200282 kfree(osc);
Stephen Boydf5644f12016-06-01 14:31:22 -0700283 hw = ERR_PTR(ret);
284 }
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200285
Stephen Boydf5644f12016-06-01 14:31:22 -0700286 return hw;
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200287}
288
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200289static int clk_main_probe_frequency(struct regmap *regmap)
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200290{
291 unsigned long prep_time, timeout;
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200292 unsigned int mcfr;
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200293
294 timeout = jiffies + usecs_to_jiffies(MAINFRDY_TIMEOUT);
295 do {
296 prep_time = jiffies;
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200297 regmap_read(regmap, AT91_CKGR_MCFR, &mcfr);
298 if (mcfr & AT91_PMC_MAINRDY)
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200299 return 0;
Alexandre Belloni658fd652019-09-20 17:39:06 +0200300 if (system_state < SYSTEM_RUNNING)
301 udelay(MAINF_LOOP_MIN_WAIT);
302 else
303 usleep_range(MAINF_LOOP_MIN_WAIT, MAINF_LOOP_MAX_WAIT);
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200304 } while (time_before(prep_time, timeout));
305
306 return -ETIMEDOUT;
307}
308
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200309static unsigned long clk_main_recalc_rate(struct regmap *regmap,
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200310 unsigned long parent_rate)
311{
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200312 unsigned int mcfr;
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200313
314 if (parent_rate)
315 return parent_rate;
316
Alexandre Belloni4da66b62014-07-01 16:12:12 +0200317 pr_warn("Main crystal frequency not set, using approximate value\n");
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200318 regmap_read(regmap, AT91_CKGR_MCFR, &mcfr);
319 if (!(mcfr & AT91_PMC_MAINRDY))
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200320 return 0;
321
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200322 return ((mcfr & AT91_PMC_MAINF) * SLOW_CLOCK_FREQ) / MAINF_DIV;
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200323}
324
325static int clk_rm9200_main_prepare(struct clk_hw *hw)
326{
327 struct clk_rm9200_main *clkmain = to_clk_rm9200_main(hw);
328
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200329 return clk_main_probe_frequency(clkmain->regmap);
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200330}
331
332static int clk_rm9200_main_is_prepared(struct clk_hw *hw)
333{
334 struct clk_rm9200_main *clkmain = to_clk_rm9200_main(hw);
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200335 unsigned int status;
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200336
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200337 regmap_read(clkmain->regmap, AT91_CKGR_MCFR, &status);
338
339 return status & AT91_PMC_MAINRDY ? 1 : 0;
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200340}
341
342static unsigned long clk_rm9200_main_recalc_rate(struct clk_hw *hw,
343 unsigned long parent_rate)
344{
345 struct clk_rm9200_main *clkmain = to_clk_rm9200_main(hw);
346
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200347 return clk_main_recalc_rate(clkmain->regmap, parent_rate);
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200348}
349
350static const struct clk_ops rm9200_main_ops = {
351 .prepare = clk_rm9200_main_prepare,
352 .is_prepared = clk_rm9200_main_is_prepared,
353 .recalc_rate = clk_rm9200_main_recalc_rate,
354};
355
Alexandre Bellonib2e39dc2018-10-16 16:21:44 +0200356struct clk_hw * __init
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200357at91_clk_register_rm9200_main(struct regmap *regmap,
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200358 const char *name,
359 const char *parent_name)
360{
361 struct clk_rm9200_main *clkmain;
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200362 struct clk_init_data init;
Stephen Boydf5644f12016-06-01 14:31:22 -0700363 struct clk_hw *hw;
364 int ret;
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200365
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200366 if (!name)
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200367 return ERR_PTR(-EINVAL);
368
369 if (!parent_name)
Boris BREZILLON38d34c32013-10-11 10:44:49 +0200370 return ERR_PTR(-EINVAL);
371
372 clkmain = kzalloc(sizeof(*clkmain), GFP_KERNEL);
373 if (!clkmain)
374 return ERR_PTR(-ENOMEM);
375
376 init.name = name;
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200377 init.ops = &rm9200_main_ops;
378 init.parent_names = &parent_name;
379 init.num_parents = 1;
380 init.flags = 0;
Boris BREZILLON38d34c32013-10-11 10:44:49 +0200381
382 clkmain->hw.init = &init;
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200383 clkmain->regmap = regmap;
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200384
Stephen Boydf5644f12016-06-01 14:31:22 -0700385 hw = &clkmain->hw;
386 ret = clk_hw_register(NULL, &clkmain->hw);
387 if (ret) {
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200388 kfree(clkmain);
Stephen Boydf5644f12016-06-01 14:31:22 -0700389 hw = ERR_PTR(ret);
390 }
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200391
Stephen Boydf5644f12016-06-01 14:31:22 -0700392 return hw;
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200393}
394
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200395static inline bool clk_sam9x5_main_ready(struct regmap *regmap)
396{
397 unsigned int status;
398
399 regmap_read(regmap, AT91_PMC_SR, &status);
400
401 return status & AT91_PMC_MOSCSELS ? 1 : 0;
402}
403
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200404static int clk_sam9x5_main_prepare(struct clk_hw *hw)
405{
406 struct clk_sam9x5_main *clkmain = to_clk_sam9x5_main(hw);
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200407 struct regmap *regmap = clkmain->regmap;
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200408
Alexandre Belloni99a81702015-09-16 23:47:39 +0200409 while (!clk_sam9x5_main_ready(regmap))
410 cpu_relax();
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200411
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200412 return clk_main_probe_frequency(regmap);
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200413}
414
415static int clk_sam9x5_main_is_prepared(struct clk_hw *hw)
416{
417 struct clk_sam9x5_main *clkmain = to_clk_sam9x5_main(hw);
418
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200419 return clk_sam9x5_main_ready(clkmain->regmap);
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200420}
421
422static unsigned long clk_sam9x5_main_recalc_rate(struct clk_hw *hw,
423 unsigned long parent_rate)
424{
425 struct clk_sam9x5_main *clkmain = to_clk_sam9x5_main(hw);
426
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200427 return clk_main_recalc_rate(clkmain->regmap, parent_rate);
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200428}
429
430static int clk_sam9x5_main_set_parent(struct clk_hw *hw, u8 index)
431{
432 struct clk_sam9x5_main *clkmain = to_clk_sam9x5_main(hw);
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200433 struct regmap *regmap = clkmain->regmap;
434 unsigned int tmp;
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200435
436 if (index > 1)
437 return -EINVAL;
438
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200439 regmap_read(regmap, AT91_CKGR_MOR, &tmp);
440 tmp &= ~MOR_KEY_MASK;
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200441
442 if (index && !(tmp & AT91_PMC_MOSCSEL))
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200443 regmap_write(regmap, AT91_CKGR_MOR, tmp | AT91_PMC_MOSCSEL);
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200444 else if (!index && (tmp & AT91_PMC_MOSCSEL))
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200445 regmap_write(regmap, AT91_CKGR_MOR, tmp & ~AT91_PMC_MOSCSEL);
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200446
Alexandre Belloni99a81702015-09-16 23:47:39 +0200447 while (!clk_sam9x5_main_ready(regmap))
448 cpu_relax();
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200449
450 return 0;
451}
452
453static u8 clk_sam9x5_main_get_parent(struct clk_hw *hw)
454{
455 struct clk_sam9x5_main *clkmain = to_clk_sam9x5_main(hw);
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200456 unsigned int status;
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200457
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200458 regmap_read(clkmain->regmap, AT91_CKGR_MOR, &status);
459
Eugen Hristev69a6bcd2019-09-09 15:30:34 +0000460 return clk_main_parent_select(status);
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200461}
462
463static const struct clk_ops sam9x5_main_ops = {
464 .prepare = clk_sam9x5_main_prepare,
465 .is_prepared = clk_sam9x5_main_is_prepared,
466 .recalc_rate = clk_sam9x5_main_recalc_rate,
467 .set_parent = clk_sam9x5_main_set_parent,
468 .get_parent = clk_sam9x5_main_get_parent,
469};
470
Alexandre Bellonib2e39dc2018-10-16 16:21:44 +0200471struct clk_hw * __init
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200472at91_clk_register_sam9x5_main(struct regmap *regmap,
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200473 const char *name,
474 const char **parent_names,
475 int num_parents)
476{
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200477 struct clk_sam9x5_main *clkmain;
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200478 struct clk_init_data init;
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200479 unsigned int status;
Stephen Boydf5644f12016-06-01 14:31:22 -0700480 struct clk_hw *hw;
481 int ret;
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200482
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200483 if (!name)
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200484 return ERR_PTR(-EINVAL);
485
486 if (!parent_names || !num_parents)
487 return ERR_PTR(-EINVAL);
488
489 clkmain = kzalloc(sizeof(*clkmain), GFP_KERNEL);
490 if (!clkmain)
491 return ERR_PTR(-ENOMEM);
492
493 init.name = name;
494 init.ops = &sam9x5_main_ops;
495 init.parent_names = parent_names;
496 init.num_parents = num_parents;
497 init.flags = CLK_SET_PARENT_GATE;
498
499 clkmain->hw.init = &init;
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200500 clkmain->regmap = regmap;
Boris Brezillon1bdf0232014-09-07 08:14:29 +0200501 regmap_read(clkmain->regmap, AT91_CKGR_MOR, &status);
Eugen Hristev69a6bcd2019-09-09 15:30:34 +0000502 clkmain->parent = clk_main_parent_select(status);
Boris BREZILLON38d34c32013-10-11 10:44:49 +0200503
Stephen Boydf5644f12016-06-01 14:31:22 -0700504 hw = &clkmain->hw;
505 ret = clk_hw_register(NULL, &clkmain->hw);
506 if (ret) {
Boris BREZILLON38d34c32013-10-11 10:44:49 +0200507 kfree(clkmain);
Stephen Boydf5644f12016-06-01 14:31:22 -0700508 hw = ERR_PTR(ret);
509 }
Boris BREZILLON38d34c32013-10-11 10:44:49 +0200510
Stephen Boydf5644f12016-06-01 14:31:22 -0700511 return hw;
Boris BREZILLON38d34c32013-10-11 10:44:49 +0200512}