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_hw
* __init
132 at91_clk_register_main_osc(struct regmap
*regmap
,
134 const char *parent_name
,
137 struct clk_main_osc
*osc
;
138 struct clk_init_data init
;
142 if (!name
|| !parent_name
)
143 return ERR_PTR(-EINVAL
);
145 osc
= kzalloc(sizeof(*osc
), GFP_KERNEL
);
147 return ERR_PTR(-ENOMEM
);
150 init
.ops
= &main_osc_ops
;
151 init
.parent_names
= &parent_name
;
152 init
.num_parents
= 1;
153 init
.flags
= CLK_IGNORE_UNUSED
;
155 osc
->hw
.init
= &init
;
156 osc
->regmap
= regmap
;
159 regmap_update_bits(regmap
,
160 AT91_CKGR_MOR
, MOR_KEY_MASK
|
162 AT91_PMC_OSCBYPASS
| AT91_PMC_KEY
);
165 ret
= clk_hw_register(NULL
, &osc
->hw
);
174 static void __init
of_at91rm9200_clk_main_osc_setup(struct device_node
*np
)
177 const char *name
= np
->name
;
178 const char *parent_name
;
179 struct regmap
*regmap
;
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);
186 regmap
= syscon_node_to_regmap(of_get_parent(np
));
190 hw
= at91_clk_register_main_osc(regmap
, name
, parent_name
, bypass
);
194 of_clk_add_hw_provider(np
, of_clk_hw_simple_get
, hw
);
196 CLK_OF_DECLARE(at91rm9200_clk_main_osc
, "atmel,at91rm9200-clk-main-osc",
197 of_at91rm9200_clk_main_osc_setup
);
199 static bool clk_main_rc_osc_ready(struct regmap
*regmap
)
203 regmap_read(regmap
, AT91_PMC_SR
, &status
);
205 return status
& AT91_PMC_MOSCRCS
;
208 static int clk_main_rc_osc_prepare(struct clk_hw
*hw
)
210 struct clk_main_rc_osc
*osc
= to_clk_main_rc_osc(hw
);
211 struct regmap
*regmap
= osc
->regmap
;
214 regmap_read(regmap
, AT91_CKGR_MOR
, &mor
);
216 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
);
221 while (!clk_main_rc_osc_ready(regmap
))
227 static void clk_main_rc_osc_unprepare(struct clk_hw
*hw
)
229 struct clk_main_rc_osc
*osc
= to_clk_main_rc_osc(hw
);
230 struct regmap
*regmap
= osc
->regmap
;
233 regmap_read(regmap
, AT91_CKGR_MOR
, &mor
);
235 if (!(mor
& AT91_PMC_MOSCRCEN
))
238 regmap_update_bits(regmap
, AT91_CKGR_MOR
,
239 MOR_KEY_MASK
| AT91_PMC_MOSCRCEN
, AT91_PMC_KEY
);
242 static int clk_main_rc_osc_is_prepared(struct clk_hw
*hw
)
244 struct clk_main_rc_osc
*osc
= to_clk_main_rc_osc(hw
);
245 struct regmap
*regmap
= osc
->regmap
;
246 unsigned int mor
, status
;
248 regmap_read(regmap
, AT91_CKGR_MOR
, &mor
);
249 regmap_read(regmap
, AT91_PMC_SR
, &status
);
251 return (mor
& AT91_PMC_MOSCRCEN
) && (status
& AT91_PMC_MOSCRCS
);
254 static unsigned long clk_main_rc_osc_recalc_rate(struct clk_hw
*hw
,
255 unsigned long parent_rate
)
257 struct clk_main_rc_osc
*osc
= to_clk_main_rc_osc(hw
);
259 return osc
->frequency
;
262 static unsigned long clk_main_rc_osc_recalc_accuracy(struct clk_hw
*hw
,
263 unsigned long parent_acc
)
265 struct clk_main_rc_osc
*osc
= to_clk_main_rc_osc(hw
);
267 return osc
->accuracy
;
270 static 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
,
278 static struct clk_hw
* __init
279 at91_clk_register_main_rc_osc(struct regmap
*regmap
,
281 u32 frequency
, u32 accuracy
)
283 struct clk_main_rc_osc
*osc
;
284 struct clk_init_data init
;
288 if (!name
|| !frequency
)
289 return ERR_PTR(-EINVAL
);
291 osc
= kzalloc(sizeof(*osc
), GFP_KERNEL
);
293 return ERR_PTR(-ENOMEM
);
296 init
.ops
= &main_rc_osc_ops
;
297 init
.parent_names
= NULL
;
298 init
.num_parents
= 0;
299 init
.flags
= CLK_IGNORE_UNUSED
;
301 osc
->hw
.init
= &init
;
302 osc
->regmap
= regmap
;
303 osc
->frequency
= frequency
;
304 osc
->accuracy
= accuracy
;
307 ret
= clk_hw_register(NULL
, hw
);
316 static void __init
of_at91sam9x5_clk_main_rc_osc_setup(struct device_node
*np
)
321 const char *name
= np
->name
;
322 struct regmap
*regmap
;
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
);
328 regmap
= syscon_node_to_regmap(of_get_parent(np
));
332 hw
= at91_clk_register_main_rc_osc(regmap
, name
, frequency
, accuracy
);
336 of_clk_add_hw_provider(np
, of_clk_hw_simple_get
, hw
);
338 CLK_OF_DECLARE(at91sam9x5_clk_main_rc_osc
, "atmel,at91sam9x5-clk-main-rc-osc",
339 of_at91sam9x5_clk_main_rc_osc_setup
);
342 static int clk_main_probe_frequency(struct regmap
*regmap
)
344 unsigned long prep_time
, timeout
;
347 timeout
= jiffies
+ usecs_to_jiffies(MAINFRDY_TIMEOUT
);
350 regmap_read(regmap
, AT91_CKGR_MCFR
, &mcfr
);
351 if (mcfr
& AT91_PMC_MAINRDY
)
353 usleep_range(MAINF_LOOP_MIN_WAIT
, MAINF_LOOP_MAX_WAIT
);
354 } while (time_before(prep_time
, timeout
));
359 static unsigned long clk_main_recalc_rate(struct regmap
*regmap
,
360 unsigned long parent_rate
)
367 pr_warn("Main crystal frequency not set, using approximate value\n");
368 regmap_read(regmap
, AT91_CKGR_MCFR
, &mcfr
);
369 if (!(mcfr
& AT91_PMC_MAINRDY
))
372 return ((mcfr
& AT91_PMC_MAINF
) * SLOW_CLOCK_FREQ
) / MAINF_DIV
;
375 static int clk_rm9200_main_prepare(struct clk_hw
*hw
)
377 struct clk_rm9200_main
*clkmain
= to_clk_rm9200_main(hw
);
379 return clk_main_probe_frequency(clkmain
->regmap
);
382 static int clk_rm9200_main_is_prepared(struct clk_hw
*hw
)
384 struct clk_rm9200_main
*clkmain
= to_clk_rm9200_main(hw
);
387 regmap_read(clkmain
->regmap
, AT91_CKGR_MCFR
, &status
);
389 return status
& AT91_PMC_MAINRDY
? 1 : 0;
392 static unsigned long clk_rm9200_main_recalc_rate(struct clk_hw
*hw
,
393 unsigned long parent_rate
)
395 struct clk_rm9200_main
*clkmain
= to_clk_rm9200_main(hw
);
397 return clk_main_recalc_rate(clkmain
->regmap
, parent_rate
);
400 static 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
,
406 static struct clk_hw
* __init
407 at91_clk_register_rm9200_main(struct regmap
*regmap
,
409 const char *parent_name
)
411 struct clk_rm9200_main
*clkmain
;
412 struct clk_init_data init
;
417 return ERR_PTR(-EINVAL
);
420 return ERR_PTR(-EINVAL
);
422 clkmain
= kzalloc(sizeof(*clkmain
), GFP_KERNEL
);
424 return ERR_PTR(-ENOMEM
);
427 init
.ops
= &rm9200_main_ops
;
428 init
.parent_names
= &parent_name
;
429 init
.num_parents
= 1;
432 clkmain
->hw
.init
= &init
;
433 clkmain
->regmap
= regmap
;
436 ret
= clk_hw_register(NULL
, &clkmain
->hw
);
445 static void __init
of_at91rm9200_clk_main_setup(struct device_node
*np
)
448 const char *parent_name
;
449 const char *name
= np
->name
;
450 struct regmap
*regmap
;
452 parent_name
= of_clk_get_parent_name(np
, 0);
453 of_property_read_string(np
, "clock-output-names", &name
);
455 regmap
= syscon_node_to_regmap(of_get_parent(np
));
459 hw
= at91_clk_register_rm9200_main(regmap
, name
, parent_name
);
463 of_clk_add_hw_provider(np
, of_clk_hw_simple_get
, hw
);
465 CLK_OF_DECLARE(at91rm9200_clk_main
, "atmel,at91rm9200-clk-main",
466 of_at91rm9200_clk_main_setup
);
468 static inline bool clk_sam9x5_main_ready(struct regmap
*regmap
)
472 regmap_read(regmap
, AT91_PMC_SR
, &status
);
474 return status
& AT91_PMC_MOSCSELS
? 1 : 0;
477 static int clk_sam9x5_main_prepare(struct clk_hw
*hw
)
479 struct clk_sam9x5_main
*clkmain
= to_clk_sam9x5_main(hw
);
480 struct regmap
*regmap
= clkmain
->regmap
;
482 while (!clk_sam9x5_main_ready(regmap
))
485 return clk_main_probe_frequency(regmap
);
488 static int clk_sam9x5_main_is_prepared(struct clk_hw
*hw
)
490 struct clk_sam9x5_main
*clkmain
= to_clk_sam9x5_main(hw
);
492 return clk_sam9x5_main_ready(clkmain
->regmap
);
495 static unsigned long clk_sam9x5_main_recalc_rate(struct clk_hw
*hw
,
496 unsigned long parent_rate
)
498 struct clk_sam9x5_main
*clkmain
= to_clk_sam9x5_main(hw
);
500 return clk_main_recalc_rate(clkmain
->regmap
, parent_rate
);
503 static int clk_sam9x5_main_set_parent(struct clk_hw
*hw
, u8 index
)
505 struct clk_sam9x5_main
*clkmain
= to_clk_sam9x5_main(hw
);
506 struct regmap
*regmap
= clkmain
->regmap
;
512 regmap_read(regmap
, AT91_CKGR_MOR
, &tmp
);
513 tmp
&= ~MOR_KEY_MASK
;
515 if (index
&& !(tmp
& AT91_PMC_MOSCSEL
))
516 regmap_write(regmap
, AT91_CKGR_MOR
, tmp
| AT91_PMC_MOSCSEL
);
517 else if (!index
&& (tmp
& AT91_PMC_MOSCSEL
))
518 regmap_write(regmap
, AT91_CKGR_MOR
, tmp
& ~AT91_PMC_MOSCSEL
);
520 while (!clk_sam9x5_main_ready(regmap
))
526 static u8
clk_sam9x5_main_get_parent(struct clk_hw
*hw
)
528 struct clk_sam9x5_main
*clkmain
= to_clk_sam9x5_main(hw
);
531 regmap_read(clkmain
->regmap
, AT91_CKGR_MOR
, &status
);
533 return status
& AT91_PMC_MOSCEN
? 1 : 0;
536 static 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
,
544 static struct clk_hw
* __init
545 at91_clk_register_sam9x5_main(struct regmap
*regmap
,
547 const char **parent_names
,
550 struct clk_sam9x5_main
*clkmain
;
551 struct clk_init_data init
;
557 return ERR_PTR(-EINVAL
);
559 if (!parent_names
|| !num_parents
)
560 return ERR_PTR(-EINVAL
);
562 clkmain
= kzalloc(sizeof(*clkmain
), GFP_KERNEL
);
564 return ERR_PTR(-ENOMEM
);
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
;
572 clkmain
->hw
.init
= &init
;
573 clkmain
->regmap
= regmap
;
574 regmap_read(clkmain
->regmap
, AT91_CKGR_MOR
, &status
);
575 clkmain
->parent
= status
& AT91_PMC_MOSCEN
? 1 : 0;
578 ret
= clk_hw_register(NULL
, &clkmain
->hw
);
587 static void __init
of_at91sam9x5_clk_main_setup(struct device_node
*np
)
590 const char *parent_names
[2];
591 unsigned int num_parents
;
592 const char *name
= np
->name
;
593 struct regmap
*regmap
;
595 num_parents
= of_clk_get_parent_count(np
);
596 if (num_parents
== 0 || num_parents
> 2)
599 of_clk_parent_fill(np
, parent_names
, num_parents
);
600 regmap
= syscon_node_to_regmap(of_get_parent(np
));
604 of_property_read_string(np
, "clock-output-names", &name
);
606 hw
= at91_clk_register_sam9x5_main(regmap
, name
, parent_names
,
611 of_clk_add_hw_provider(np
, of_clk_hw_simple_get
, hw
);
613 CLK_OF_DECLARE(at91sam9x5_clk_main
, "atmel,at91sam9x5-clk-main",
614 of_at91sam9x5_clk_main_setup
);