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/of_address.h>
17 #include <linux/of_irq.h>
19 #include <linux/interrupt.h>
20 #include <linux/irq.h>
21 #include <linux/sched.h>
22 #include <linux/wait.h>
26 #define SLOW_CLOCK_FREQ 32768
28 #define MAINFRDY_TIMEOUT (((MAINF_DIV + 1) * USEC_PER_SEC) / \
30 #define MAINF_LOOP_MIN_WAIT (USEC_PER_SEC / SLOW_CLOCK_FREQ)
31 #define MAINF_LOOP_MAX_WAIT MAINFRDY_TIMEOUT
33 #define MOR_KEY_MASK (0xff << 16)
39 wait_queue_head_t wait
;
42 #define to_clk_main_osc(hw) container_of(hw, struct clk_main_osc, hw)
44 struct clk_main_rc_osc
{
48 wait_queue_head_t wait
;
49 unsigned long frequency
;
50 unsigned long accuracy
;
53 #define to_clk_main_rc_osc(hw) container_of(hw, struct clk_main_rc_osc, hw)
55 struct clk_rm9200_main
{
60 #define to_clk_rm9200_main(hw) container_of(hw, struct clk_rm9200_main, hw)
62 struct clk_sam9x5_main
{
66 wait_queue_head_t wait
;
70 #define to_clk_sam9x5_main(hw) container_of(hw, struct clk_sam9x5_main, hw)
72 static irqreturn_t
clk_main_osc_irq_handler(int irq
, void *dev_id
)
74 struct clk_main_osc
*osc
= dev_id
;
77 disable_irq_nosync(osc
->irq
);
82 static int clk_main_osc_prepare(struct clk_hw
*hw
)
84 struct clk_main_osc
*osc
= to_clk_main_osc(hw
);
85 struct at91_pmc
*pmc
= osc
->pmc
;
88 tmp
= pmc_read(pmc
, AT91_CKGR_MOR
) & ~MOR_KEY_MASK
;
89 if (tmp
& AT91_PMC_OSCBYPASS
)
92 if (!(tmp
& AT91_PMC_MOSCEN
)) {
93 tmp
|= AT91_PMC_MOSCEN
| AT91_PMC_KEY
;
94 pmc_write(pmc
, AT91_CKGR_MOR
, tmp
);
97 while (!(pmc_read(pmc
, AT91_PMC_SR
) & AT91_PMC_MOSCS
)) {
100 pmc_read(pmc
, AT91_PMC_SR
) & AT91_PMC_MOSCS
);
106 static void clk_main_osc_unprepare(struct clk_hw
*hw
)
108 struct clk_main_osc
*osc
= to_clk_main_osc(hw
);
109 struct at91_pmc
*pmc
= osc
->pmc
;
110 u32 tmp
= pmc_read(pmc
, AT91_CKGR_MOR
);
112 if (tmp
& AT91_PMC_OSCBYPASS
)
115 if (!(tmp
& AT91_PMC_MOSCEN
))
118 tmp
&= ~(AT91_PMC_KEY
| AT91_PMC_MOSCEN
);
119 pmc_write(pmc
, AT91_CKGR_MOR
, tmp
| AT91_PMC_KEY
);
122 static int clk_main_osc_is_prepared(struct clk_hw
*hw
)
124 struct clk_main_osc
*osc
= to_clk_main_osc(hw
);
125 struct at91_pmc
*pmc
= osc
->pmc
;
126 u32 tmp
= pmc_read(pmc
, AT91_CKGR_MOR
);
128 if (tmp
& AT91_PMC_OSCBYPASS
)
131 return !!((pmc_read(pmc
, AT91_PMC_SR
) & AT91_PMC_MOSCS
) &&
132 (pmc_read(pmc
, AT91_CKGR_MOR
) & AT91_PMC_MOSCEN
));
135 static const struct clk_ops main_osc_ops
= {
136 .prepare
= clk_main_osc_prepare
,
137 .unprepare
= clk_main_osc_unprepare
,
138 .is_prepared
= clk_main_osc_is_prepared
,
141 static struct clk
* __init
142 at91_clk_register_main_osc(struct at91_pmc
*pmc
,
145 const char *parent_name
,
149 struct clk_main_osc
*osc
;
150 struct clk
*clk
= NULL
;
151 struct clk_init_data init
;
153 if (!pmc
|| !irq
|| !name
|| !parent_name
)
154 return ERR_PTR(-EINVAL
);
156 osc
= kzalloc(sizeof(*osc
), GFP_KERNEL
);
158 return ERR_PTR(-ENOMEM
);
161 init
.ops
= &main_osc_ops
;
162 init
.parent_names
= &parent_name
;
163 init
.num_parents
= 1;
164 init
.flags
= CLK_IGNORE_UNUSED
;
166 osc
->hw
.init
= &init
;
170 init_waitqueue_head(&osc
->wait
);
171 irq_set_status_flags(osc
->irq
, IRQ_NOAUTOEN
);
172 ret
= request_irq(osc
->irq
, clk_main_osc_irq_handler
,
173 IRQF_TRIGGER_HIGH
, name
, osc
);
180 pmc_write(pmc
, AT91_CKGR_MOR
,
181 (pmc_read(pmc
, AT91_CKGR_MOR
) &
182 ~(MOR_KEY_MASK
| AT91_PMC_MOSCEN
)) |
183 AT91_PMC_OSCBYPASS
| AT91_PMC_KEY
);
185 clk
= clk_register(NULL
, &osc
->hw
);
194 void __init
of_at91rm9200_clk_main_osc_setup(struct device_node
*np
,
195 struct at91_pmc
*pmc
)
199 const char *name
= np
->name
;
200 const char *parent_name
;
203 of_property_read_string(np
, "clock-output-names", &name
);
204 bypass
= of_property_read_bool(np
, "atmel,osc-bypass");
205 parent_name
= of_clk_get_parent_name(np
, 0);
207 irq
= irq_of_parse_and_map(np
, 0);
211 clk
= at91_clk_register_main_osc(pmc
, irq
, name
, parent_name
, bypass
);
215 of_clk_add_provider(np
, of_clk_src_simple_get
, clk
);
218 static irqreturn_t
clk_main_rc_osc_irq_handler(int irq
, void *dev_id
)
220 struct clk_main_rc_osc
*osc
= dev_id
;
223 disable_irq_nosync(osc
->irq
);
228 static int clk_main_rc_osc_prepare(struct clk_hw
*hw
)
230 struct clk_main_rc_osc
*osc
= to_clk_main_rc_osc(hw
);
231 struct at91_pmc
*pmc
= osc
->pmc
;
234 tmp
= pmc_read(pmc
, AT91_CKGR_MOR
) & ~MOR_KEY_MASK
;
236 if (!(tmp
& AT91_PMC_MOSCRCEN
)) {
237 tmp
|= AT91_PMC_MOSCRCEN
| AT91_PMC_KEY
;
238 pmc_write(pmc
, AT91_CKGR_MOR
, tmp
);
241 while (!(pmc_read(pmc
, AT91_PMC_SR
) & AT91_PMC_MOSCRCS
)) {
242 enable_irq(osc
->irq
);
243 wait_event(osc
->wait
,
244 pmc_read(pmc
, AT91_PMC_SR
) & AT91_PMC_MOSCRCS
);
250 static void clk_main_rc_osc_unprepare(struct clk_hw
*hw
)
252 struct clk_main_rc_osc
*osc
= to_clk_main_rc_osc(hw
);
253 struct at91_pmc
*pmc
= osc
->pmc
;
254 u32 tmp
= pmc_read(pmc
, AT91_CKGR_MOR
);
256 if (!(tmp
& AT91_PMC_MOSCRCEN
))
259 tmp
&= ~(MOR_KEY_MASK
| AT91_PMC_MOSCRCEN
);
260 pmc_write(pmc
, AT91_CKGR_MOR
, tmp
| AT91_PMC_KEY
);
263 static int clk_main_rc_osc_is_prepared(struct clk_hw
*hw
)
265 struct clk_main_rc_osc
*osc
= to_clk_main_rc_osc(hw
);
266 struct at91_pmc
*pmc
= osc
->pmc
;
268 return !!((pmc_read(pmc
, AT91_PMC_SR
) & AT91_PMC_MOSCRCS
) &&
269 (pmc_read(pmc
, AT91_CKGR_MOR
) & AT91_PMC_MOSCRCEN
));
272 static unsigned long clk_main_rc_osc_recalc_rate(struct clk_hw
*hw
,
273 unsigned long parent_rate
)
275 struct clk_main_rc_osc
*osc
= to_clk_main_rc_osc(hw
);
277 return osc
->frequency
;
280 static unsigned long clk_main_rc_osc_recalc_accuracy(struct clk_hw
*hw
,
281 unsigned long parent_acc
)
283 struct clk_main_rc_osc
*osc
= to_clk_main_rc_osc(hw
);
285 return osc
->accuracy
;
288 static const struct clk_ops main_rc_osc_ops
= {
289 .prepare
= clk_main_rc_osc_prepare
,
290 .unprepare
= clk_main_rc_osc_unprepare
,
291 .is_prepared
= clk_main_rc_osc_is_prepared
,
292 .recalc_rate
= clk_main_rc_osc_recalc_rate
,
293 .recalc_accuracy
= clk_main_rc_osc_recalc_accuracy
,
296 static struct clk
* __init
297 at91_clk_register_main_rc_osc(struct at91_pmc
*pmc
,
300 u32 frequency
, u32 accuracy
)
303 struct clk_main_rc_osc
*osc
;
304 struct clk
*clk
= NULL
;
305 struct clk_init_data init
;
307 if (!pmc
|| !irq
|| !name
|| !frequency
)
308 return ERR_PTR(-EINVAL
);
310 osc
= kzalloc(sizeof(*osc
), GFP_KERNEL
);
312 return ERR_PTR(-ENOMEM
);
315 init
.ops
= &main_rc_osc_ops
;
316 init
.parent_names
= NULL
;
317 init
.num_parents
= 0;
318 init
.flags
= CLK_IS_ROOT
| CLK_IGNORE_UNUSED
;
320 osc
->hw
.init
= &init
;
323 osc
->frequency
= frequency
;
324 osc
->accuracy
= accuracy
;
326 init_waitqueue_head(&osc
->wait
);
327 irq_set_status_flags(osc
->irq
, IRQ_NOAUTOEN
);
328 ret
= request_irq(osc
->irq
, clk_main_rc_osc_irq_handler
,
329 IRQF_TRIGGER_HIGH
, name
, osc
);
333 clk
= clk_register(NULL
, &osc
->hw
);
342 void __init
of_at91sam9x5_clk_main_rc_osc_setup(struct device_node
*np
,
343 struct at91_pmc
*pmc
)
349 const char *name
= np
->name
;
351 of_property_read_string(np
, "clock-output-names", &name
);
352 of_property_read_u32(np
, "clock-frequency", &frequency
);
353 of_property_read_u32(np
, "clock-accuracy", &accuracy
);
355 irq
= irq_of_parse_and_map(np
, 0);
359 clk
= at91_clk_register_main_rc_osc(pmc
, irq
, name
, frequency
,
364 of_clk_add_provider(np
, of_clk_src_simple_get
, clk
);
368 static int clk_main_probe_frequency(struct at91_pmc
*pmc
)
370 unsigned long prep_time
, timeout
;
373 timeout
= jiffies
+ usecs_to_jiffies(MAINFRDY_TIMEOUT
);
376 tmp
= pmc_read(pmc
, AT91_CKGR_MCFR
);
377 if (tmp
& AT91_PMC_MAINRDY
)
379 usleep_range(MAINF_LOOP_MIN_WAIT
, MAINF_LOOP_MAX_WAIT
);
380 } while (time_before(prep_time
, timeout
));
385 static unsigned long clk_main_recalc_rate(struct at91_pmc
*pmc
,
386 unsigned long parent_rate
)
393 pr_warn("Main crystal frequency not set, using approximate value\n");
394 tmp
= pmc_read(pmc
, AT91_CKGR_MCFR
);
395 if (!(tmp
& AT91_PMC_MAINRDY
))
398 return ((tmp
& AT91_PMC_MAINF
) * SLOW_CLOCK_FREQ
) / MAINF_DIV
;
401 static int clk_rm9200_main_prepare(struct clk_hw
*hw
)
403 struct clk_rm9200_main
*clkmain
= to_clk_rm9200_main(hw
);
405 return clk_main_probe_frequency(clkmain
->pmc
);
408 static int clk_rm9200_main_is_prepared(struct clk_hw
*hw
)
410 struct clk_rm9200_main
*clkmain
= to_clk_rm9200_main(hw
);
412 return !!(pmc_read(clkmain
->pmc
, AT91_CKGR_MCFR
) & AT91_PMC_MAINRDY
);
415 static unsigned long clk_rm9200_main_recalc_rate(struct clk_hw
*hw
,
416 unsigned long parent_rate
)
418 struct clk_rm9200_main
*clkmain
= to_clk_rm9200_main(hw
);
420 return clk_main_recalc_rate(clkmain
->pmc
, parent_rate
);
423 static const struct clk_ops rm9200_main_ops
= {
424 .prepare
= clk_rm9200_main_prepare
,
425 .is_prepared
= clk_rm9200_main_is_prepared
,
426 .recalc_rate
= clk_rm9200_main_recalc_rate
,
429 static struct clk
* __init
430 at91_clk_register_rm9200_main(struct at91_pmc
*pmc
,
432 const char *parent_name
)
434 struct clk_rm9200_main
*clkmain
;
435 struct clk
*clk
= NULL
;
436 struct clk_init_data init
;
439 return ERR_PTR(-EINVAL
);
442 return ERR_PTR(-EINVAL
);
444 clkmain
= kzalloc(sizeof(*clkmain
), GFP_KERNEL
);
446 return ERR_PTR(-ENOMEM
);
449 init
.ops
= &rm9200_main_ops
;
450 init
.parent_names
= &parent_name
;
451 init
.num_parents
= 1;
454 clkmain
->hw
.init
= &init
;
457 clk
= clk_register(NULL
, &clkmain
->hw
);
464 void __init
of_at91rm9200_clk_main_setup(struct device_node
*np
,
465 struct at91_pmc
*pmc
)
468 const char *parent_name
;
469 const char *name
= np
->name
;
471 parent_name
= of_clk_get_parent_name(np
, 0);
472 of_property_read_string(np
, "clock-output-names", &name
);
474 clk
= at91_clk_register_rm9200_main(pmc
, name
, parent_name
);
478 of_clk_add_provider(np
, of_clk_src_simple_get
, clk
);
481 static irqreturn_t
clk_sam9x5_main_irq_handler(int irq
, void *dev_id
)
483 struct clk_sam9x5_main
*clkmain
= dev_id
;
485 wake_up(&clkmain
->wait
);
486 disable_irq_nosync(clkmain
->irq
);
491 static int clk_sam9x5_main_prepare(struct clk_hw
*hw
)
493 struct clk_sam9x5_main
*clkmain
= to_clk_sam9x5_main(hw
);
494 struct at91_pmc
*pmc
= clkmain
->pmc
;
496 while (!(pmc_read(pmc
, AT91_PMC_SR
) & AT91_PMC_MOSCSELS
)) {
497 enable_irq(clkmain
->irq
);
498 wait_event(clkmain
->wait
,
499 pmc_read(pmc
, AT91_PMC_SR
) & AT91_PMC_MOSCSELS
);
502 return clk_main_probe_frequency(pmc
);
505 static int clk_sam9x5_main_is_prepared(struct clk_hw
*hw
)
507 struct clk_sam9x5_main
*clkmain
= to_clk_sam9x5_main(hw
);
509 return !!(pmc_read(clkmain
->pmc
, AT91_PMC_SR
) & AT91_PMC_MOSCSELS
);
512 static unsigned long clk_sam9x5_main_recalc_rate(struct clk_hw
*hw
,
513 unsigned long parent_rate
)
515 struct clk_sam9x5_main
*clkmain
= to_clk_sam9x5_main(hw
);
517 return clk_main_recalc_rate(clkmain
->pmc
, parent_rate
);
520 static int clk_sam9x5_main_set_parent(struct clk_hw
*hw
, u8 index
)
522 struct clk_sam9x5_main
*clkmain
= to_clk_sam9x5_main(hw
);
523 struct at91_pmc
*pmc
= clkmain
->pmc
;
529 tmp
= pmc_read(pmc
, AT91_CKGR_MOR
) & ~MOR_KEY_MASK
;
531 if (index
&& !(tmp
& AT91_PMC_MOSCSEL
))
532 pmc_write(pmc
, AT91_CKGR_MOR
, tmp
| AT91_PMC_MOSCSEL
);
533 else if (!index
&& (tmp
& AT91_PMC_MOSCSEL
))
534 pmc_write(pmc
, AT91_CKGR_MOR
, tmp
& ~AT91_PMC_MOSCSEL
);
536 while (!(pmc_read(pmc
, AT91_PMC_SR
) & AT91_PMC_MOSCSELS
)) {
537 enable_irq(clkmain
->irq
);
538 wait_event(clkmain
->wait
,
539 pmc_read(pmc
, AT91_PMC_SR
) & AT91_PMC_MOSCSELS
);
545 static u8
clk_sam9x5_main_get_parent(struct clk_hw
*hw
)
547 struct clk_sam9x5_main
*clkmain
= to_clk_sam9x5_main(hw
);
549 return !!(pmc_read(clkmain
->pmc
, AT91_CKGR_MOR
) & AT91_PMC_MOSCEN
);
552 static const struct clk_ops sam9x5_main_ops
= {
553 .prepare
= clk_sam9x5_main_prepare
,
554 .is_prepared
= clk_sam9x5_main_is_prepared
,
555 .recalc_rate
= clk_sam9x5_main_recalc_rate
,
556 .set_parent
= clk_sam9x5_main_set_parent
,
557 .get_parent
= clk_sam9x5_main_get_parent
,
560 static struct clk
* __init
561 at91_clk_register_sam9x5_main(struct at91_pmc
*pmc
,
564 const char **parent_names
,
568 struct clk_sam9x5_main
*clkmain
;
569 struct clk
*clk
= NULL
;
570 struct clk_init_data init
;
572 if (!pmc
|| !irq
|| !name
)
573 return ERR_PTR(-EINVAL
);
575 if (!parent_names
|| !num_parents
)
576 return ERR_PTR(-EINVAL
);
578 clkmain
= kzalloc(sizeof(*clkmain
), GFP_KERNEL
);
580 return ERR_PTR(-ENOMEM
);
583 init
.ops
= &sam9x5_main_ops
;
584 init
.parent_names
= parent_names
;
585 init
.num_parents
= num_parents
;
586 init
.flags
= CLK_SET_PARENT_GATE
;
588 clkmain
->hw
.init
= &init
;
591 clkmain
->parent
= !!(pmc_read(clkmain
->pmc
, AT91_CKGR_MOR
) &
593 init_waitqueue_head(&clkmain
->wait
);
594 irq_set_status_flags(clkmain
->irq
, IRQ_NOAUTOEN
);
595 ret
= request_irq(clkmain
->irq
, clk_sam9x5_main_irq_handler
,
596 IRQF_TRIGGER_HIGH
, name
, clkmain
);
600 clk
= clk_register(NULL
, &clkmain
->hw
);
602 free_irq(clkmain
->irq
, clkmain
);
609 void __init
of_at91sam9x5_clk_main_setup(struct device_node
*np
,
610 struct at91_pmc
*pmc
)
613 const char *parent_names
[2];
616 const char *name
= np
->name
;
618 num_parents
= of_clk_get_parent_count(np
);
619 if (num_parents
<= 0 || num_parents
> 2)
622 of_clk_parent_fill(np
, parent_names
, num_parents
);
624 of_property_read_string(np
, "clock-output-names", &name
);
626 irq
= irq_of_parse_and_map(np
, 0);
630 clk
= at91_clk_register_sam9x5_main(pmc
, irq
, name
, parent_names
,
635 of_clk_add_provider(np
, of_clk_src_simple_get
, clk
);