2 * Copyright (C) 2013 Boris BREZILLON <b.brezillon@overkiz.com>
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.
11 #include <linux/clk-provider.h>
12 #include <linux/clkdev.h>
13 #include <linux/clk/at91_pmc.h>
14 #include <linux/delay.h>
16 #include <linux/mfd/syscon.h>
17 #include <linux/regmap.h>
21 #define SLOW_CLOCK_FREQ 32768
23 #define MAINFRDY_TIMEOUT (((MAINF_DIV + 1) * USEC_PER_SEC) / \
25 #define MAINF_LOOP_MIN_WAIT (USEC_PER_SEC / SLOW_CLOCK_FREQ)
26 #define MAINF_LOOP_MAX_WAIT MAINFRDY_TIMEOUT
28 #define MOR_KEY_MASK (0xff << 16)
32 struct regmap
*regmap
;
35 #define to_clk_main_osc(hw) container_of(hw, struct clk_main_osc, hw)
37 struct clk_main_rc_osc
{
39 struct regmap
*regmap
;
40 unsigned long frequency
;
41 unsigned long accuracy
;
44 #define to_clk_main_rc_osc(hw) container_of(hw, struct clk_main_rc_osc, hw)
46 struct clk_rm9200_main
{
48 struct regmap
*regmap
;
51 #define to_clk_rm9200_main(hw) container_of(hw, struct clk_rm9200_main, hw)
53 struct clk_sam9x5_main
{
55 struct regmap
*regmap
;
59 #define to_clk_sam9x5_main(hw) container_of(hw, struct clk_sam9x5_main, hw)
61 static inline bool clk_main_osc_ready(struct regmap
*regmap
)
65 regmap_read(regmap
, AT91_PMC_SR
, &status
);
67 return status
& AT91_PMC_MOSCS
;
70 static int clk_main_osc_prepare(struct clk_hw
*hw
)
72 struct clk_main_osc
*osc
= to_clk_main_osc(hw
);
73 struct regmap
*regmap
= osc
->regmap
;
76 regmap_read(regmap
, AT91_CKGR_MOR
, &tmp
);
79 if (tmp
& AT91_PMC_OSCBYPASS
)
82 if (!(tmp
& AT91_PMC_MOSCEN
)) {
83 tmp
|= AT91_PMC_MOSCEN
| AT91_PMC_KEY
;
84 regmap_write(regmap
, AT91_CKGR_MOR
, tmp
);
87 while (!clk_main_osc_ready(regmap
))
93 static void clk_main_osc_unprepare(struct clk_hw
*hw
)
95 struct clk_main_osc
*osc
= to_clk_main_osc(hw
);
96 struct regmap
*regmap
= osc
->regmap
;
99 regmap_read(regmap
, AT91_CKGR_MOR
, &tmp
);
100 if (tmp
& AT91_PMC_OSCBYPASS
)
103 if (!(tmp
& AT91_PMC_MOSCEN
))
106 tmp
&= ~(AT91_PMC_KEY
| AT91_PMC_MOSCEN
);
107 regmap_write(regmap
, AT91_CKGR_MOR
, tmp
| AT91_PMC_KEY
);
110 static int clk_main_osc_is_prepared(struct clk_hw
*hw
)
112 struct clk_main_osc
*osc
= to_clk_main_osc(hw
);
113 struct regmap
*regmap
= osc
->regmap
;
116 regmap_read(regmap
, AT91_CKGR_MOR
, &tmp
);
117 if (tmp
& AT91_PMC_OSCBYPASS
)
120 regmap_read(regmap
, AT91_PMC_SR
, &status
);
122 return (status
& AT91_PMC_MOSCS
) && (tmp
& AT91_PMC_MOSCEN
);
125 static 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
,
131 static struct clk
* __init
132 at91_clk_register_main_osc(struct regmap
*regmap
,
134 const char *parent_name
,
137 struct clk_main_osc
*osc
;
138 struct clk
*clk
= NULL
;
139 struct clk_init_data init
;
141 if (!name
|| !parent_name
)
142 return ERR_PTR(-EINVAL
);
144 osc
= kzalloc(sizeof(*osc
), GFP_KERNEL
);
146 return ERR_PTR(-ENOMEM
);
149 init
.ops
= &main_osc_ops
;
150 init
.parent_names
= &parent_name
;
151 init
.num_parents
= 1;
152 init
.flags
= CLK_IGNORE_UNUSED
;
154 osc
->hw
.init
= &init
;
155 osc
->regmap
= regmap
;
158 regmap_update_bits(regmap
,
159 AT91_CKGR_MOR
, MOR_KEY_MASK
|
161 AT91_PMC_OSCBYPASS
| AT91_PMC_KEY
);
163 clk
= clk_register(NULL
, &osc
->hw
);
170 static void __init
of_at91rm9200_clk_main_osc_setup(struct device_node
*np
)
173 const char *name
= np
->name
;
174 const char *parent_name
;
175 struct regmap
*regmap
;
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);
182 regmap
= syscon_node_to_regmap(of_get_parent(np
));
186 clk
= at91_clk_register_main_osc(regmap
, name
, parent_name
, bypass
);
190 of_clk_add_provider(np
, of_clk_src_simple_get
, clk
);
192 CLK_OF_DECLARE(at91rm9200_clk_main_osc
, "atmel,at91rm9200-clk-main-osc",
193 of_at91rm9200_clk_main_osc_setup
);
195 static bool clk_main_rc_osc_ready(struct regmap
*regmap
)
199 regmap_read(regmap
, AT91_PMC_SR
, &status
);
201 return status
& AT91_PMC_MOSCRCS
;
204 static int clk_main_rc_osc_prepare(struct clk_hw
*hw
)
206 struct clk_main_rc_osc
*osc
= to_clk_main_rc_osc(hw
);
207 struct regmap
*regmap
= osc
->regmap
;
210 regmap_read(regmap
, AT91_CKGR_MOR
, &mor
);
212 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
);
217 while (!clk_main_rc_osc_ready(regmap
))
223 static void clk_main_rc_osc_unprepare(struct clk_hw
*hw
)
225 struct clk_main_rc_osc
*osc
= to_clk_main_rc_osc(hw
);
226 struct regmap
*regmap
= osc
->regmap
;
229 regmap_read(regmap
, AT91_CKGR_MOR
, &mor
);
231 if (!(mor
& AT91_PMC_MOSCRCEN
))
234 regmap_update_bits(regmap
, AT91_CKGR_MOR
,
235 MOR_KEY_MASK
| AT91_PMC_MOSCRCEN
, AT91_PMC_KEY
);
238 static int clk_main_rc_osc_is_prepared(struct clk_hw
*hw
)
240 struct clk_main_rc_osc
*osc
= to_clk_main_rc_osc(hw
);
241 struct regmap
*regmap
= osc
->regmap
;
242 unsigned int mor
, status
;
244 regmap_read(regmap
, AT91_CKGR_MOR
, &mor
);
245 regmap_read(regmap
, AT91_PMC_SR
, &status
);
247 return (mor
& AT91_PMC_MOSCRCEN
) && (status
& AT91_PMC_MOSCRCS
);
250 static unsigned long clk_main_rc_osc_recalc_rate(struct clk_hw
*hw
,
251 unsigned long parent_rate
)
253 struct clk_main_rc_osc
*osc
= to_clk_main_rc_osc(hw
);
255 return osc
->frequency
;
258 static unsigned long clk_main_rc_osc_recalc_accuracy(struct clk_hw
*hw
,
259 unsigned long parent_acc
)
261 struct clk_main_rc_osc
*osc
= to_clk_main_rc_osc(hw
);
263 return osc
->accuracy
;
266 static 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
,
274 static struct clk
* __init
275 at91_clk_register_main_rc_osc(struct regmap
*regmap
,
277 u32 frequency
, u32 accuracy
)
279 struct clk_main_rc_osc
*osc
;
280 struct clk
*clk
= NULL
;
281 struct clk_init_data init
;
283 if (!name
|| !frequency
)
284 return ERR_PTR(-EINVAL
);
286 osc
= kzalloc(sizeof(*osc
), GFP_KERNEL
);
288 return ERR_PTR(-ENOMEM
);
291 init
.ops
= &main_rc_osc_ops
;
292 init
.parent_names
= NULL
;
293 init
.num_parents
= 0;
294 init
.flags
= CLK_IGNORE_UNUSED
;
296 osc
->hw
.init
= &init
;
297 osc
->regmap
= regmap
;
298 osc
->frequency
= frequency
;
299 osc
->accuracy
= accuracy
;
301 clk
= clk_register(NULL
, &osc
->hw
);
308 static void __init
of_at91sam9x5_clk_main_rc_osc_setup(struct device_node
*np
)
313 const char *name
= np
->name
;
314 struct regmap
*regmap
;
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
);
320 regmap
= syscon_node_to_regmap(of_get_parent(np
));
324 clk
= at91_clk_register_main_rc_osc(regmap
, name
, frequency
, accuracy
);
328 of_clk_add_provider(np
, of_clk_src_simple_get
, clk
);
330 CLK_OF_DECLARE(at91sam9x5_clk_main_rc_osc
, "atmel,at91sam9x5-clk-main-rc-osc",
331 of_at91sam9x5_clk_main_rc_osc_setup
);
334 static int clk_main_probe_frequency(struct regmap
*regmap
)
336 unsigned long prep_time
, timeout
;
339 timeout
= jiffies
+ usecs_to_jiffies(MAINFRDY_TIMEOUT
);
342 regmap_read(regmap
, AT91_CKGR_MCFR
, &mcfr
);
343 if (mcfr
& AT91_PMC_MAINRDY
)
345 usleep_range(MAINF_LOOP_MIN_WAIT
, MAINF_LOOP_MAX_WAIT
);
346 } while (time_before(prep_time
, timeout
));
351 static unsigned long clk_main_recalc_rate(struct regmap
*regmap
,
352 unsigned long parent_rate
)
359 pr_warn("Main crystal frequency not set, using approximate value\n");
360 regmap_read(regmap
, AT91_CKGR_MCFR
, &mcfr
);
361 if (!(mcfr
& AT91_PMC_MAINRDY
))
364 return ((mcfr
& AT91_PMC_MAINF
) * SLOW_CLOCK_FREQ
) / MAINF_DIV
;
367 static int clk_rm9200_main_prepare(struct clk_hw
*hw
)
369 struct clk_rm9200_main
*clkmain
= to_clk_rm9200_main(hw
);
371 return clk_main_probe_frequency(clkmain
->regmap
);
374 static int clk_rm9200_main_is_prepared(struct clk_hw
*hw
)
376 struct clk_rm9200_main
*clkmain
= to_clk_rm9200_main(hw
);
379 regmap_read(clkmain
->regmap
, AT91_CKGR_MCFR
, &status
);
381 return status
& AT91_PMC_MAINRDY
? 1 : 0;
384 static unsigned long clk_rm9200_main_recalc_rate(struct clk_hw
*hw
,
385 unsigned long parent_rate
)
387 struct clk_rm9200_main
*clkmain
= to_clk_rm9200_main(hw
);
389 return clk_main_recalc_rate(clkmain
->regmap
, parent_rate
);
392 static 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
,
398 static struct clk
* __init
399 at91_clk_register_rm9200_main(struct regmap
*regmap
,
401 const char *parent_name
)
403 struct clk_rm9200_main
*clkmain
;
404 struct clk
*clk
= NULL
;
405 struct clk_init_data init
;
408 return ERR_PTR(-EINVAL
);
411 return ERR_PTR(-EINVAL
);
413 clkmain
= kzalloc(sizeof(*clkmain
), GFP_KERNEL
);
415 return ERR_PTR(-ENOMEM
);
418 init
.ops
= &rm9200_main_ops
;
419 init
.parent_names
= &parent_name
;
420 init
.num_parents
= 1;
423 clkmain
->hw
.init
= &init
;
424 clkmain
->regmap
= regmap
;
426 clk
= clk_register(NULL
, &clkmain
->hw
);
433 static void __init
of_at91rm9200_clk_main_setup(struct device_node
*np
)
436 const char *parent_name
;
437 const char *name
= np
->name
;
438 struct regmap
*regmap
;
440 parent_name
= of_clk_get_parent_name(np
, 0);
441 of_property_read_string(np
, "clock-output-names", &name
);
443 regmap
= syscon_node_to_regmap(of_get_parent(np
));
447 clk
= at91_clk_register_rm9200_main(regmap
, name
, parent_name
);
451 of_clk_add_provider(np
, of_clk_src_simple_get
, clk
);
453 CLK_OF_DECLARE(at91rm9200_clk_main
, "atmel,at91rm9200-clk-main",
454 of_at91rm9200_clk_main_setup
);
456 static inline bool clk_sam9x5_main_ready(struct regmap
*regmap
)
460 regmap_read(regmap
, AT91_PMC_SR
, &status
);
462 return status
& AT91_PMC_MOSCSELS
? 1 : 0;
465 static int clk_sam9x5_main_prepare(struct clk_hw
*hw
)
467 struct clk_sam9x5_main
*clkmain
= to_clk_sam9x5_main(hw
);
468 struct regmap
*regmap
= clkmain
->regmap
;
470 while (!clk_sam9x5_main_ready(regmap
))
473 return clk_main_probe_frequency(regmap
);
476 static int clk_sam9x5_main_is_prepared(struct clk_hw
*hw
)
478 struct clk_sam9x5_main
*clkmain
= to_clk_sam9x5_main(hw
);
480 return clk_sam9x5_main_ready(clkmain
->regmap
);
483 static unsigned long clk_sam9x5_main_recalc_rate(struct clk_hw
*hw
,
484 unsigned long parent_rate
)
486 struct clk_sam9x5_main
*clkmain
= to_clk_sam9x5_main(hw
);
488 return clk_main_recalc_rate(clkmain
->regmap
, parent_rate
);
491 static int clk_sam9x5_main_set_parent(struct clk_hw
*hw
, u8 index
)
493 struct clk_sam9x5_main
*clkmain
= to_clk_sam9x5_main(hw
);
494 struct regmap
*regmap
= clkmain
->regmap
;
500 regmap_read(regmap
, AT91_CKGR_MOR
, &tmp
);
501 tmp
&= ~MOR_KEY_MASK
;
503 if (index
&& !(tmp
& AT91_PMC_MOSCSEL
))
504 regmap_write(regmap
, AT91_CKGR_MOR
, tmp
| AT91_PMC_MOSCSEL
);
505 else if (!index
&& (tmp
& AT91_PMC_MOSCSEL
))
506 regmap_write(regmap
, AT91_CKGR_MOR
, tmp
& ~AT91_PMC_MOSCSEL
);
508 while (!clk_sam9x5_main_ready(regmap
))
514 static u8
clk_sam9x5_main_get_parent(struct clk_hw
*hw
)
516 struct clk_sam9x5_main
*clkmain
= to_clk_sam9x5_main(hw
);
519 regmap_read(clkmain
->regmap
, AT91_CKGR_MOR
, &status
);
521 return status
& AT91_PMC_MOSCEN
? 1 : 0;
524 static 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
,
532 static struct clk
* __init
533 at91_clk_register_sam9x5_main(struct regmap
*regmap
,
535 const char **parent_names
,
538 struct clk_sam9x5_main
*clkmain
;
539 struct clk
*clk
= NULL
;
540 struct clk_init_data init
;
544 return ERR_PTR(-EINVAL
);
546 if (!parent_names
|| !num_parents
)
547 return ERR_PTR(-EINVAL
);
549 clkmain
= kzalloc(sizeof(*clkmain
), GFP_KERNEL
);
551 return ERR_PTR(-ENOMEM
);
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
;
559 clkmain
->hw
.init
= &init
;
560 clkmain
->regmap
= regmap
;
561 regmap_read(clkmain
->regmap
, AT91_CKGR_MOR
, &status
);
562 clkmain
->parent
= status
& AT91_PMC_MOSCEN
? 1 : 0;
564 clk
= clk_register(NULL
, &clkmain
->hw
);
571 static void __init
of_at91sam9x5_clk_main_setup(struct device_node
*np
)
574 const char *parent_names
[2];
575 unsigned int num_parents
;
576 const char *name
= np
->name
;
577 struct regmap
*regmap
;
579 num_parents
= of_clk_get_parent_count(np
);
580 if (num_parents
== 0 || num_parents
> 2)
583 of_clk_parent_fill(np
, parent_names
, num_parents
);
584 regmap
= syscon_node_to_regmap(of_get_parent(np
));
588 of_property_read_string(np
, "clock-output-names", &name
);
590 clk
= at91_clk_register_sam9x5_main(regmap
, name
, parent_names
,
595 of_clk_add_provider(np
, of_clk_src_simple_get
, clk
);
597 CLK_OF_DECLARE(at91sam9x5_clk_main
, "atmel,at91sam9x5-clk-main",
598 of_at91sam9x5_clk_main_setup
);