blob: 311956abf4aa05fc6bd9f1f4d799cb33b5363394 [file] [log] [blame]
Boris BREZILLON80eded62014-05-07 18:02:15 +02001/*
2 * drivers/clk/at91/sckc.c
3 *
4 * Copyright (C) 2013 Boris BREZILLON <b.brezillon@overkiz.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 */
12
13#include <linux/clk-provider.h>
14#include <linux/clkdev.h>
Alexandre Belloniec187ef2016-09-20 22:58:29 +020015#include <linux/delay.h>
Boris BREZILLON80eded62014-05-07 18:02:15 +020016#include <linux/of.h>
17#include <linux/of_address.h>
18#include <linux/io.h>
19
Alexandre Belloniec187ef2016-09-20 22:58:29 +020020#define SLOW_CLOCK_FREQ 32768
21#define SLOWCK_SW_CYCLES 5
22#define SLOWCK_SW_TIME_USEC ((SLOWCK_SW_CYCLES * USEC_PER_SEC) / \
23 SLOW_CLOCK_FREQ)
24
25#define AT91_SCKC_CR 0x00
26#define AT91_SCKC_RCEN (1 << 0)
27#define AT91_SCKC_OSC32EN (1 << 1)
28#define AT91_SCKC_OSC32BYP (1 << 2)
29#define AT91_SCKC_OSCSEL (1 << 3)
30
31struct clk_slow_osc {
32 struct clk_hw hw;
33 void __iomem *sckcr;
34 unsigned long startup_usec;
35};
36
37#define to_clk_slow_osc(hw) container_of(hw, struct clk_slow_osc, hw)
38
39struct clk_slow_rc_osc {
40 struct clk_hw hw;
41 void __iomem *sckcr;
42 unsigned long frequency;
43 unsigned long accuracy;
44 unsigned long startup_usec;
45};
46
47#define to_clk_slow_rc_osc(hw) container_of(hw, struct clk_slow_rc_osc, hw)
48
49struct clk_sam9x5_slow {
50 struct clk_hw hw;
51 void __iomem *sckcr;
52 u8 parent;
53};
54
55#define to_clk_sam9x5_slow(hw) container_of(hw, struct clk_sam9x5_slow, hw)
56
57static int clk_slow_osc_prepare(struct clk_hw *hw)
58{
59 struct clk_slow_osc *osc = to_clk_slow_osc(hw);
60 void __iomem *sckcr = osc->sckcr;
61 u32 tmp = readl(sckcr);
62
63 if (tmp & AT91_SCKC_OSC32BYP)
64 return 0;
65
66 writel(tmp | AT91_SCKC_OSC32EN, sckcr);
67
68 usleep_range(osc->startup_usec, osc->startup_usec + 1);
69
70 return 0;
71}
72
73static void clk_slow_osc_unprepare(struct clk_hw *hw)
74{
75 struct clk_slow_osc *osc = to_clk_slow_osc(hw);
76 void __iomem *sckcr = osc->sckcr;
77 u32 tmp = readl(sckcr);
78
79 if (tmp & AT91_SCKC_OSC32BYP)
80 return;
81
82 writel(tmp & ~AT91_SCKC_OSC32EN, sckcr);
83}
84
85static int clk_slow_osc_is_prepared(struct clk_hw *hw)
86{
87 struct clk_slow_osc *osc = to_clk_slow_osc(hw);
88 void __iomem *sckcr = osc->sckcr;
89 u32 tmp = readl(sckcr);
90
91 if (tmp & AT91_SCKC_OSC32BYP)
92 return 1;
93
94 return !!(tmp & AT91_SCKC_OSC32EN);
95}
96
97static const struct clk_ops slow_osc_ops = {
98 .prepare = clk_slow_osc_prepare,
99 .unprepare = clk_slow_osc_unprepare,
100 .is_prepared = clk_slow_osc_is_prepared,
101};
102
103static struct clk_hw * __init
104at91_clk_register_slow_osc(void __iomem *sckcr,
105 const char *name,
106 const char *parent_name,
107 unsigned long startup,
108 bool bypass)
109{
110 struct clk_slow_osc *osc;
111 struct clk_hw *hw;
112 struct clk_init_data init;
113 int ret;
114
115 if (!sckcr || !name || !parent_name)
116 return ERR_PTR(-EINVAL);
117
118 osc = kzalloc(sizeof(*osc), GFP_KERNEL);
119 if (!osc)
120 return ERR_PTR(-ENOMEM);
121
122 init.name = name;
123 init.ops = &slow_osc_ops;
124 init.parent_names = &parent_name;
125 init.num_parents = 1;
126 init.flags = CLK_IGNORE_UNUSED;
127
128 osc->hw.init = &init;
129 osc->sckcr = sckcr;
130 osc->startup_usec = startup;
131
132 if (bypass)
133 writel((readl(sckcr) & ~AT91_SCKC_OSC32EN) | AT91_SCKC_OSC32BYP,
134 sckcr);
135
136 hw = &osc->hw;
137 ret = clk_hw_register(NULL, &osc->hw);
138 if (ret) {
139 kfree(osc);
140 hw = ERR_PTR(ret);
141 }
142
143 return hw;
144}
145
146static void __init
147of_at91sam9x5_clk_slow_osc_setup(struct device_node *np, void __iomem *sckcr)
148{
149 struct clk_hw *hw;
150 const char *parent_name;
151 const char *name = np->name;
152 u32 startup;
153 bool bypass;
154
155 parent_name = of_clk_get_parent_name(np, 0);
156 of_property_read_string(np, "clock-output-names", &name);
157 of_property_read_u32(np, "atmel,startup-time-usec", &startup);
158 bypass = of_property_read_bool(np, "atmel,osc-bypass");
159
160 hw = at91_clk_register_slow_osc(sckcr, name, parent_name, startup,
161 bypass);
162 if (IS_ERR(hw))
163 return;
164
165 of_clk_add_hw_provider(np, of_clk_hw_simple_get, hw);
166}
167
168static unsigned long clk_slow_rc_osc_recalc_rate(struct clk_hw *hw,
169 unsigned long parent_rate)
170{
171 struct clk_slow_rc_osc *osc = to_clk_slow_rc_osc(hw);
172
173 return osc->frequency;
174}
175
176static unsigned long clk_slow_rc_osc_recalc_accuracy(struct clk_hw *hw,
177 unsigned long parent_acc)
178{
179 struct clk_slow_rc_osc *osc = to_clk_slow_rc_osc(hw);
180
181 return osc->accuracy;
182}
183
184static int clk_slow_rc_osc_prepare(struct clk_hw *hw)
185{
186 struct clk_slow_rc_osc *osc = to_clk_slow_rc_osc(hw);
187 void __iomem *sckcr = osc->sckcr;
188
189 writel(readl(sckcr) | AT91_SCKC_RCEN, sckcr);
190
191 usleep_range(osc->startup_usec, osc->startup_usec + 1);
192
193 return 0;
194}
195
196static void clk_slow_rc_osc_unprepare(struct clk_hw *hw)
197{
198 struct clk_slow_rc_osc *osc = to_clk_slow_rc_osc(hw);
199 void __iomem *sckcr = osc->sckcr;
200
201 writel(readl(sckcr) & ~AT91_SCKC_RCEN, sckcr);
202}
203
204static int clk_slow_rc_osc_is_prepared(struct clk_hw *hw)
205{
206 struct clk_slow_rc_osc *osc = to_clk_slow_rc_osc(hw);
207
208 return !!(readl(osc->sckcr) & AT91_SCKC_RCEN);
209}
210
211static const struct clk_ops slow_rc_osc_ops = {
212 .prepare = clk_slow_rc_osc_prepare,
213 .unprepare = clk_slow_rc_osc_unprepare,
214 .is_prepared = clk_slow_rc_osc_is_prepared,
215 .recalc_rate = clk_slow_rc_osc_recalc_rate,
216 .recalc_accuracy = clk_slow_rc_osc_recalc_accuracy,
217};
218
219static struct clk_hw * __init
220at91_clk_register_slow_rc_osc(void __iomem *sckcr,
221 const char *name,
222 unsigned long frequency,
223 unsigned long accuracy,
224 unsigned long startup)
225{
226 struct clk_slow_rc_osc *osc;
227 struct clk_hw *hw;
228 struct clk_init_data init;
229 int ret;
230
231 if (!sckcr || !name)
232 return ERR_PTR(-EINVAL);
233
234 osc = kzalloc(sizeof(*osc), GFP_KERNEL);
235 if (!osc)
236 return ERR_PTR(-ENOMEM);
237
238 init.name = name;
239 init.ops = &slow_rc_osc_ops;
240 init.parent_names = NULL;
241 init.num_parents = 0;
242 init.flags = CLK_IGNORE_UNUSED;
243
244 osc->hw.init = &init;
245 osc->sckcr = sckcr;
246 osc->frequency = frequency;
247 osc->accuracy = accuracy;
248 osc->startup_usec = startup;
249
250 hw = &osc->hw;
251 ret = clk_hw_register(NULL, &osc->hw);
252 if (ret) {
253 kfree(osc);
254 hw = ERR_PTR(ret);
255 }
256
257 return hw;
258}
259
260static void __init
261of_at91sam9x5_clk_slow_rc_osc_setup(struct device_node *np, void __iomem *sckcr)
262{
263 struct clk_hw *hw;
264 u32 frequency = 0;
265 u32 accuracy = 0;
266 u32 startup = 0;
267 const char *name = np->name;
268
269 of_property_read_string(np, "clock-output-names", &name);
270 of_property_read_u32(np, "clock-frequency", &frequency);
271 of_property_read_u32(np, "clock-accuracy", &accuracy);
272 of_property_read_u32(np, "atmel,startup-time-usec", &startup);
273
274 hw = at91_clk_register_slow_rc_osc(sckcr, name, frequency, accuracy,
275 startup);
276 if (IS_ERR(hw))
277 return;
278
279 of_clk_add_hw_provider(np, of_clk_hw_simple_get, hw);
280}
281
282static int clk_sam9x5_slow_set_parent(struct clk_hw *hw, u8 index)
283{
284 struct clk_sam9x5_slow *slowck = to_clk_sam9x5_slow(hw);
285 void __iomem *sckcr = slowck->sckcr;
286 u32 tmp;
287
288 if (index > 1)
289 return -EINVAL;
290
291 tmp = readl(sckcr);
292
293 if ((!index && !(tmp & AT91_SCKC_OSCSEL)) ||
294 (index && (tmp & AT91_SCKC_OSCSEL)))
295 return 0;
296
297 if (index)
298 tmp |= AT91_SCKC_OSCSEL;
299 else
300 tmp &= ~AT91_SCKC_OSCSEL;
301
302 writel(tmp, sckcr);
303
304 usleep_range(SLOWCK_SW_TIME_USEC, SLOWCK_SW_TIME_USEC + 1);
305
306 return 0;
307}
308
309static u8 clk_sam9x5_slow_get_parent(struct clk_hw *hw)
310{
311 struct clk_sam9x5_slow *slowck = to_clk_sam9x5_slow(hw);
312
313 return !!(readl(slowck->sckcr) & AT91_SCKC_OSCSEL);
314}
315
316static const struct clk_ops sam9x5_slow_ops = {
317 .set_parent = clk_sam9x5_slow_set_parent,
318 .get_parent = clk_sam9x5_slow_get_parent,
319};
320
321static struct clk_hw * __init
322at91_clk_register_sam9x5_slow(void __iomem *sckcr,
323 const char *name,
324 const char **parent_names,
325 int num_parents)
326{
327 struct clk_sam9x5_slow *slowck;
328 struct clk_hw *hw;
329 struct clk_init_data init;
330 int ret;
331
332 if (!sckcr || !name || !parent_names || !num_parents)
333 return ERR_PTR(-EINVAL);
334
335 slowck = kzalloc(sizeof(*slowck), GFP_KERNEL);
336 if (!slowck)
337 return ERR_PTR(-ENOMEM);
338
339 init.name = name;
340 init.ops = &sam9x5_slow_ops;
341 init.parent_names = parent_names;
342 init.num_parents = num_parents;
343 init.flags = 0;
344
345 slowck->hw.init = &init;
346 slowck->sckcr = sckcr;
347 slowck->parent = !!(readl(sckcr) & AT91_SCKC_OSCSEL);
348
349 hw = &slowck->hw;
350 ret = clk_hw_register(NULL, &slowck->hw);
351 if (ret) {
352 kfree(slowck);
353 hw = ERR_PTR(ret);
354 }
355
356 return hw;
357}
358
359static void __init
360of_at91sam9x5_clk_slow_setup(struct device_node *np, void __iomem *sckcr)
361{
362 struct clk_hw *hw;
363 const char *parent_names[2];
364 unsigned int num_parents;
365 const char *name = np->name;
366
367 num_parents = of_clk_get_parent_count(np);
368 if (num_parents == 0 || num_parents > 2)
369 return;
370
371 of_clk_parent_fill(np, parent_names, num_parents);
372
373 of_property_read_string(np, "clock-output-names", &name);
374
375 hw = at91_clk_register_sam9x5_slow(sckcr, name, parent_names,
376 num_parents);
377 if (IS_ERR(hw))
378 return;
379
380 of_clk_add_hw_provider(np, of_clk_hw_simple_get, hw);
381}
Boris BREZILLON80eded62014-05-07 18:02:15 +0200382
383static const struct of_device_id sckc_clk_ids[] __initconst = {
384 /* Slow clock */
385 {
386 .compatible = "atmel,at91sam9x5-clk-slow-osc",
387 .data = of_at91sam9x5_clk_slow_osc_setup,
388 },
389 {
390 .compatible = "atmel,at91sam9x5-clk-slow-rc-osc",
391 .data = of_at91sam9x5_clk_slow_rc_osc_setup,
392 },
393 {
394 .compatible = "atmel,at91sam9x5-clk-slow",
395 .data = of_at91sam9x5_clk_slow_setup,
396 },
397 { /*sentinel*/ }
398};
399
400static void __init of_at91sam9x5_sckc_setup(struct device_node *np)
401{
402 struct device_node *childnp;
403 void (*clk_setup)(struct device_node *, void __iomem *);
404 const struct of_device_id *clk_id;
405 void __iomem *regbase = of_iomap(np, 0);
406
407 if (!regbase)
408 return;
409
410 for_each_child_of_node(np, childnp) {
411 clk_id = of_match_node(sckc_clk_ids, childnp);
412 if (!clk_id)
413 continue;
414 clk_setup = clk_id->data;
415 clk_setup(childnp, regbase);
416 }
417}
418CLK_OF_DECLARE(at91sam9x5_clk_sckc, "atmel,at91sam9x5-sckc",
419 of_at91sam9x5_sckc_setup);