2 * Copyright (C) 2013 Broadcom Corporation
3 * Copyright 2013 Linaro Limited
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation version 2.
9 * This program is distributed "as is" WITHOUT ANY WARRANTY of any
10 * kind, whether express or implied; without even the implied warranty
11 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
16 #include <linux/of_address.h>
20 /* These are used when a selector or trigger is found to be unneeded */
21 #define selector_clear_exists(sel) ((sel)->width = 0)
22 #define trigger_clear_exists(trig) FLAG_CLEAR(trig, TRIG, EXISTS)
24 LIST_HEAD(ccu_list
); /* The list of set up CCUs */
26 /* Validity checking */
28 static bool ccu_data_offsets_valid(struct ccu_data
*ccu
)
30 struct ccu_policy
*ccu_policy
= &ccu
->policy
;
33 limit
= ccu
->range
- sizeof(u32
);
34 limit
= round_down(limit
, sizeof(u32
));
35 if (ccu_policy_exists(ccu_policy
)) {
36 if (ccu_policy
->enable
.offset
> limit
) {
37 pr_err("%s: bad policy enable offset for %s "
38 "(%u > %u)\n", __func__
,
39 ccu
->name
, ccu_policy
->enable
.offset
, limit
);
42 if (ccu_policy
->control
.offset
> limit
) {
43 pr_err("%s: bad policy control offset for %s "
44 "(%u > %u)\n", __func__
,
45 ccu
->name
, ccu_policy
->control
.offset
, limit
);
53 static bool clk_requires_trigger(struct kona_clk
*bcm_clk
)
55 struct peri_clk_data
*peri
= bcm_clk
->u
.peri
;
56 struct bcm_clk_sel
*sel
;
57 struct bcm_clk_div
*div
;
59 if (bcm_clk
->type
!= bcm_clk_peri
)
63 if (sel
->parent_count
&& selector_exists(sel
))
67 if (!divider_exists(div
))
70 /* Fixed dividers don't need triggers */
71 if (!divider_is_fixed(div
))
76 return divider_exists(div
) && !divider_is_fixed(div
);
79 static bool peri_clk_data_offsets_valid(struct kona_clk
*bcm_clk
)
81 struct peri_clk_data
*peri
;
82 struct bcm_clk_policy
*policy
;
83 struct bcm_clk_gate
*gate
;
84 struct bcm_clk_hyst
*hyst
;
85 struct bcm_clk_div
*div
;
86 struct bcm_clk_sel
*sel
;
87 struct bcm_clk_trig
*trig
;
92 BUG_ON(bcm_clk
->type
!= bcm_clk_peri
);
93 peri
= bcm_clk
->u
.peri
;
94 name
= bcm_clk
->init_data
.name
;
95 range
= bcm_clk
->ccu
->range
;
97 limit
= range
- sizeof(u32
);
98 limit
= round_down(limit
, sizeof(u32
));
100 policy
= &peri
->policy
;
101 if (policy_exists(policy
)) {
102 if (policy
->offset
> limit
) {
103 pr_err("%s: bad policy offset for %s (%u > %u)\n",
104 __func__
, name
, policy
->offset
, limit
);
111 if (gate_exists(gate
)) {
112 if (gate
->offset
> limit
) {
113 pr_err("%s: bad gate offset for %s (%u > %u)\n",
114 __func__
, name
, gate
->offset
, limit
);
118 if (hyst_exists(hyst
)) {
119 if (hyst
->offset
> limit
) {
120 pr_err("%s: bad hysteresis offset for %s "
121 "(%u > %u)\n", __func__
,
122 name
, hyst
->offset
, limit
);
126 } else if (hyst_exists(hyst
)) {
127 pr_err("%s: hysteresis but no gate for %s\n", __func__
, name
);
132 if (divider_exists(div
)) {
133 if (div
->u
.s
.offset
> limit
) {
134 pr_err("%s: bad divider offset for %s (%u > %u)\n",
135 __func__
, name
, div
->u
.s
.offset
, limit
);
140 div
= &peri
->pre_div
;
141 if (divider_exists(div
)) {
142 if (div
->u
.s
.offset
> limit
) {
143 pr_err("%s: bad pre-divider offset for %s "
145 __func__
, name
, div
->u
.s
.offset
, limit
);
151 if (selector_exists(sel
)) {
152 if (sel
->offset
> limit
) {
153 pr_err("%s: bad selector offset for %s (%u > %u)\n",
154 __func__
, name
, sel
->offset
, limit
);
160 if (trigger_exists(trig
)) {
161 if (trig
->offset
> limit
) {
162 pr_err("%s: bad trigger offset for %s (%u > %u)\n",
163 __func__
, name
, trig
->offset
, limit
);
168 trig
= &peri
->pre_trig
;
169 if (trigger_exists(trig
)) {
170 if (trig
->offset
> limit
) {
171 pr_err("%s: bad pre-trigger offset for %s (%u > %u)\n",
172 __func__
, name
, trig
->offset
, limit
);
180 /* A bit position must be less than the number of bits in a 32-bit register. */
181 static bool bit_posn_valid(u32 bit_posn
, const char *field_name
,
182 const char *clock_name
)
184 u32 limit
= BITS_PER_BYTE
* sizeof(u32
) - 1;
186 if (bit_posn
> limit
) {
187 pr_err("%s: bad %s bit for %s (%u > %u)\n", __func__
,
188 field_name
, clock_name
, bit_posn
, limit
);
195 * A bitfield must be at least 1 bit wide. Both the low-order and
196 * high-order bits must lie within a 32-bit register. We require
197 * fields to be less than 32 bits wide, mainly because we use
198 * shifting to produce field masks, and shifting a full word width
199 * is not well-defined by the C standard.
201 static bool bitfield_valid(u32 shift
, u32 width
, const char *field_name
,
202 const char *clock_name
)
204 u32 limit
= BITS_PER_BYTE
* sizeof(u32
);
207 pr_err("%s: bad %s field width 0 for %s\n", __func__
,
208 field_name
, clock_name
);
211 if (shift
+ width
> limit
) {
212 pr_err("%s: bad %s for %s (%u + %u > %u)\n", __func__
,
213 field_name
, clock_name
, shift
, width
, limit
);
220 ccu_policy_valid(struct ccu_policy
*ccu_policy
, const char *ccu_name
)
222 struct bcm_lvm_en
*enable
= &ccu_policy
->enable
;
223 struct bcm_policy_ctl
*control
;
225 if (!bit_posn_valid(enable
->bit
, "policy enable", ccu_name
))
228 control
= &ccu_policy
->control
;
229 if (!bit_posn_valid(control
->go_bit
, "policy control GO", ccu_name
))
232 if (!bit_posn_valid(control
->atl_bit
, "policy control ATL", ccu_name
))
235 if (!bit_posn_valid(control
->ac_bit
, "policy control AC", ccu_name
))
241 static bool policy_valid(struct bcm_clk_policy
*policy
, const char *clock_name
)
243 if (!bit_posn_valid(policy
->bit
, "policy", clock_name
))
250 * All gates, if defined, have a status bit, and for hardware-only
251 * gates, that's it. Gates that can be software controlled also
252 * have an enable bit. And a gate that can be hardware or software
253 * controlled will have a hardware/software select bit.
255 static bool gate_valid(struct bcm_clk_gate
*gate
, const char *field_name
,
256 const char *clock_name
)
258 if (!bit_posn_valid(gate
->status_bit
, "gate status", clock_name
))
261 if (gate_is_sw_controllable(gate
)) {
262 if (!bit_posn_valid(gate
->en_bit
, "gate enable", clock_name
))
265 if (gate_is_hw_controllable(gate
)) {
266 if (!bit_posn_valid(gate
->hw_sw_sel_bit
,
272 BUG_ON(!gate_is_hw_controllable(gate
));
278 static bool hyst_valid(struct bcm_clk_hyst
*hyst
, const char *clock_name
)
280 if (!bit_posn_valid(hyst
->en_bit
, "hysteresis enable", clock_name
))
283 if (!bit_posn_valid(hyst
->val_bit
, "hysteresis value", clock_name
))
290 * A selector bitfield must be valid. Its parent_sel array must
291 * also be reasonable for the field.
293 static bool sel_valid(struct bcm_clk_sel
*sel
, const char *field_name
,
294 const char *clock_name
)
296 if (!bitfield_valid(sel
->shift
, sel
->width
, field_name
, clock_name
))
299 if (sel
->parent_count
) {
304 * Make sure the selector field can hold all the
305 * selector values we expect to be able to use. A
306 * clock only needs to have a selector defined if it
307 * has more than one parent. And in that case the
308 * highest selector value will be in the last entry
311 max_sel
= sel
->parent_sel
[sel
->parent_count
- 1];
312 limit
= (1 << sel
->width
) - 1;
313 if (max_sel
> limit
) {
314 pr_err("%s: bad selector for %s "
315 "(%u needs > %u bits)\n",
316 __func__
, clock_name
, max_sel
,
321 pr_warn("%s: ignoring selector for %s (no parents)\n",
322 __func__
, clock_name
);
323 selector_clear_exists(sel
);
324 kfree(sel
->parent_sel
);
325 sel
->parent_sel
= NULL
;
332 * A fixed divider just needs to be non-zero. A variable divider
333 * has to have a valid divider bitfield, and if it has a fraction,
334 * the width of the fraction must not be no more than the width of
335 * the divider as a whole.
337 static bool div_valid(struct bcm_clk_div
*div
, const char *field_name
,
338 const char *clock_name
)
340 if (divider_is_fixed(div
)) {
341 /* Any fixed divider value but 0 is OK */
342 if (div
->u
.fixed
== 0) {
343 pr_err("%s: bad %s fixed value 0 for %s\n", __func__
,
344 field_name
, clock_name
);
349 if (!bitfield_valid(div
->u
.s
.shift
, div
->u
.s
.width
,
350 field_name
, clock_name
))
353 if (divider_has_fraction(div
))
354 if (div
->u
.s
.frac_width
> div
->u
.s
.width
) {
355 pr_warn("%s: bad %s fraction width for %s (%u > %u)\n",
356 __func__
, field_name
, clock_name
,
357 div
->u
.s
.frac_width
, div
->u
.s
.width
);
365 * If a clock has two dividers, the combined number of fractional
366 * bits must be representable in a 32-bit unsigned value. This
367 * is because we scale up a dividend using both dividers before
368 * dividing to improve accuracy, and we need to avoid overflow.
370 static bool kona_dividers_valid(struct kona_clk
*bcm_clk
)
372 struct peri_clk_data
*peri
= bcm_clk
->u
.peri
;
373 struct bcm_clk_div
*div
;
374 struct bcm_clk_div
*pre_div
;
377 BUG_ON(bcm_clk
->type
!= bcm_clk_peri
);
379 if (!divider_exists(&peri
->div
) || !divider_exists(&peri
->pre_div
))
383 pre_div
= &peri
->pre_div
;
384 if (divider_is_fixed(div
) || divider_is_fixed(pre_div
))
387 limit
= BITS_PER_BYTE
* sizeof(u32
);
389 return div
->u
.s
.frac_width
+ pre_div
->u
.s
.frac_width
<= limit
;
393 /* A trigger just needs to represent a valid bit position */
394 static bool trig_valid(struct bcm_clk_trig
*trig
, const char *field_name
,
395 const char *clock_name
)
397 return bit_posn_valid(trig
->bit
, field_name
, clock_name
);
400 /* Determine whether the set of peripheral clock registers are valid. */
402 peri_clk_data_valid(struct kona_clk
*bcm_clk
)
404 struct peri_clk_data
*peri
;
405 struct bcm_clk_policy
*policy
;
406 struct bcm_clk_gate
*gate
;
407 struct bcm_clk_hyst
*hyst
;
408 struct bcm_clk_sel
*sel
;
409 struct bcm_clk_div
*div
;
410 struct bcm_clk_div
*pre_div
;
411 struct bcm_clk_trig
*trig
;
414 BUG_ON(bcm_clk
->type
!= bcm_clk_peri
);
417 * First validate register offsets. This is the only place
418 * where we need something from the ccu, so we do these
421 if (!peri_clk_data_offsets_valid(bcm_clk
))
424 peri
= bcm_clk
->u
.peri
;
425 name
= bcm_clk
->init_data
.name
;
427 policy
= &peri
->policy
;
428 if (policy_exists(policy
) && !policy_valid(policy
, name
))
432 if (gate_exists(gate
) && !gate_valid(gate
, "gate", name
))
436 if (hyst_exists(hyst
) && !hyst_valid(hyst
, name
))
440 if (selector_exists(sel
)) {
441 if (!sel_valid(sel
, "selector", name
))
444 } else if (sel
->parent_count
> 1) {
445 pr_err("%s: multiple parents but no selector for %s\n",
452 pre_div
= &peri
->pre_div
;
453 if (divider_exists(div
)) {
454 if (!div_valid(div
, "divider", name
))
457 if (divider_exists(pre_div
))
458 if (!div_valid(pre_div
, "pre-divider", name
))
460 } else if (divider_exists(pre_div
)) {
461 pr_err("%s: pre-divider but no divider for %s\n", __func__
,
467 if (trigger_exists(trig
)) {
468 if (!trig_valid(trig
, "trigger", name
))
471 if (trigger_exists(&peri
->pre_trig
)) {
472 if (!trig_valid(trig
, "pre-trigger", name
)) {
476 if (!clk_requires_trigger(bcm_clk
)) {
477 pr_warn("%s: ignoring trigger for %s (not needed)\n",
479 trigger_clear_exists(trig
);
481 } else if (trigger_exists(&peri
->pre_trig
)) {
482 pr_err("%s: pre-trigger but no trigger for %s\n", __func__
,
485 } else if (clk_requires_trigger(bcm_clk
)) {
486 pr_err("%s: required trigger missing for %s\n", __func__
,
491 return kona_dividers_valid(bcm_clk
);
494 static bool kona_clk_valid(struct kona_clk
*bcm_clk
)
496 switch (bcm_clk
->type
) {
498 if (!peri_clk_data_valid(bcm_clk
))
502 pr_err("%s: unrecognized clock type (%d)\n", __func__
,
510 * Scan an array of parent clock names to determine whether there
511 * are any entries containing BAD_CLK_NAME. Such entries are
512 * placeholders for non-supported clocks. Keep track of the
513 * position of each clock name in the original array.
515 * Allocates an array of pointers to to hold the names of all
516 * non-null entries in the original array, and returns a pointer to
517 * that array in *names. This will be used for registering the
518 * clock with the common clock code. On successful return,
519 * *count indicates how many entries are in that names array.
521 * If there is more than one entry in the resulting names array,
522 * another array is allocated to record the parent selector value
523 * for each (defined) parent clock. This is the value that
524 * represents this parent clock in the clock's source selector
525 * register. The position of the clock in the original parent array
526 * defines that selector value. The number of entries in this array
527 * is the same as the number of entries in the parent names array.
529 * The array of selector values is returned. If the clock has no
530 * parents, no selector is required and a null pointer is returned.
532 * Returns a null pointer if the clock names array supplied was
533 * null. (This is not an error.)
535 * Returns a pointer-coded error if an error occurs.
537 static u32
*parent_process(const char *clocks
[],
538 u32
*count
, const char ***names
)
540 static const char **parent_names
;
541 static u32
*parent_sel
;
549 *count
= 0; /* In case of early return */
555 * Count the number of names in the null-terminated array,
556 * and find out how many of those are actually clock names.
558 for (clock
= clocks
; *clock
; clock
++)
559 if (*clock
== BAD_CLK_NAME
)
561 orig_count
= (u32
)(clock
- clocks
);
562 parent_count
= orig_count
- bad_count
;
564 /* If all clocks are unsupported, we treat it as no clock */
568 /* Avoid exceeding our parent clock limit */
569 if (parent_count
> PARENT_COUNT_MAX
) {
570 pr_err("%s: too many parents (%u > %u)\n", __func__
,
571 parent_count
, PARENT_COUNT_MAX
);
572 return ERR_PTR(-EINVAL
);
576 * There is one parent name for each defined parent clock.
577 * We also maintain an array containing the selector value
578 * for each defined clock. If there's only one clock, the
579 * selector is not required, but we allocate space for the
580 * array anyway to keep things simple.
582 parent_names
= kmalloc(parent_count
* sizeof(parent_names
), GFP_KERNEL
);
584 pr_err("%s: error allocating %u parent names\n", __func__
,
586 return ERR_PTR(-ENOMEM
);
589 /* There is at least one parent, so allocate a selector array */
591 parent_sel
= kmalloc(parent_count
* sizeof(*parent_sel
), GFP_KERNEL
);
593 pr_err("%s: error allocating %u parent selectors\n", __func__
,
597 return ERR_PTR(-ENOMEM
);
600 /* Now fill in the parent names and selector arrays */
601 for (i
= 0, j
= 0; i
< orig_count
; i
++) {
602 if (clocks
[i
] != BAD_CLK_NAME
) {
603 parent_names
[j
] = clocks
[i
];
608 *names
= parent_names
;
609 *count
= parent_count
;
615 clk_sel_setup(const char **clocks
, struct bcm_clk_sel
*sel
,
616 struct clk_init_data
*init_data
)
618 const char **parent_names
= NULL
;
619 u32 parent_count
= 0;
623 * If a peripheral clock has multiple parents, the value
624 * used by the hardware to select that parent is represented
625 * by the parent clock's position in the "clocks" list. Some
626 * values don't have defined or supported clocks; these will
627 * have BAD_CLK_NAME entries in the parents[] array. The
628 * list is terminated by a NULL entry.
630 * We need to supply (only) the names of defined parent
631 * clocks when registering a clock though, so we use an
632 * array of parent selector values to map between the
633 * indexes the common clock code uses and the selector
636 parent_sel
= parent_process(clocks
, &parent_count
, &parent_names
);
637 if (IS_ERR(parent_sel
)) {
638 int ret
= PTR_ERR(parent_sel
);
640 pr_err("%s: error processing parent clocks for %s (%d)\n",
641 __func__
, init_data
->name
, ret
);
646 init_data
->parent_names
= parent_names
;
647 init_data
->num_parents
= parent_count
;
649 sel
->parent_count
= parent_count
;
650 sel
->parent_sel
= parent_sel
;
655 static void clk_sel_teardown(struct bcm_clk_sel
*sel
,
656 struct clk_init_data
*init_data
)
658 kfree(sel
->parent_sel
);
659 sel
->parent_sel
= NULL
;
660 sel
->parent_count
= 0;
662 init_data
->num_parents
= 0;
663 kfree(init_data
->parent_names
);
664 init_data
->parent_names
= NULL
;
667 static void peri_clk_teardown(struct peri_clk_data
*data
,
668 struct clk_init_data
*init_data
)
670 clk_sel_teardown(&data
->sel
, init_data
);
674 * Caller is responsible for freeing the parent_names[] and
675 * parent_sel[] arrays in the peripheral clock's "data" structure
676 * that can be assigned if the clock has one or more parent clocks
677 * associated with it.
680 peri_clk_setup(struct peri_clk_data
*data
, struct clk_init_data
*init_data
)
682 init_data
->flags
= CLK_IGNORE_UNUSED
;
684 return clk_sel_setup(data
->clocks
, &data
->sel
, init_data
);
687 static void bcm_clk_teardown(struct kona_clk
*bcm_clk
)
689 switch (bcm_clk
->type
) {
691 peri_clk_teardown(bcm_clk
->u
.data
, &bcm_clk
->init_data
);
696 bcm_clk
->u
.data
= NULL
;
697 bcm_clk
->type
= bcm_clk_none
;
700 static void kona_clk_teardown(struct clk
*clk
)
703 struct kona_clk
*bcm_clk
;
708 hw
= __clk_get_hw(clk
);
710 pr_err("%s: clk %p has null hw pointer\n", __func__
, clk
);
715 bcm_clk
= to_kona_clk(hw
);
716 bcm_clk_teardown(bcm_clk
);
719 struct clk
*kona_clk_setup(struct kona_clk
*bcm_clk
)
721 struct clk_init_data
*init_data
= &bcm_clk
->init_data
;
722 struct clk
*clk
= NULL
;
724 switch (bcm_clk
->type
) {
726 if (peri_clk_setup(bcm_clk
->u
.data
, init_data
))
730 pr_err("%s: clock type %d invalid for %s\n", __func__
,
731 (int)bcm_clk
->type
, init_data
->name
);
735 /* Make sure everything makes sense before we set it up */
736 if (!kona_clk_valid(bcm_clk
)) {
737 pr_err("%s: clock data invalid for %s\n", __func__
,
742 bcm_clk
->hw
.init
= init_data
;
743 clk
= clk_register(NULL
, &bcm_clk
->hw
);
745 pr_err("%s: error registering clock %s (%ld)\n", __func__
,
746 init_data
->name
, PTR_ERR(clk
));
753 bcm_clk_teardown(bcm_clk
);
758 static void ccu_clks_teardown(struct ccu_data
*ccu
)
762 for (i
= 0; i
< ccu
->clk_data
.clk_num
; i
++)
763 kona_clk_teardown(ccu
->clk_data
.clks
[i
]);
764 kfree(ccu
->clk_data
.clks
);
767 static void kona_ccu_teardown(struct ccu_data
*ccu
)
769 kfree(ccu
->clk_data
.clks
);
770 ccu
->clk_data
.clks
= NULL
;
774 of_clk_del_provider(ccu
->node
); /* safe if never added */
775 ccu_clks_teardown(ccu
);
776 list_del(&ccu
->links
);
777 of_node_put(ccu
->node
);
783 static bool ccu_data_valid(struct ccu_data
*ccu
)
785 struct ccu_policy
*ccu_policy
;
787 if (!ccu_data_offsets_valid(ccu
))
790 ccu_policy
= &ccu
->policy
;
791 if (ccu_policy_exists(ccu_policy
))
792 if (!ccu_policy_valid(ccu_policy
, ccu
->name
))
799 * Set up a CCU. Call the provided ccu_clks_setup callback to
800 * initialize the array of clocks provided by the CCU.
802 void __init
kona_dt_ccu_setup(struct ccu_data
*ccu
,
803 struct device_node
*node
)
805 struct resource res
= { 0 };
806 resource_size_t range
;
810 if (ccu
->clk_data
.clk_num
) {
813 size
= ccu
->clk_data
.clk_num
* sizeof(*ccu
->clk_data
.clks
);
814 ccu
->clk_data
.clks
= kzalloc(size
, GFP_KERNEL
);
815 if (!ccu
->clk_data
.clks
) {
816 pr_err("%s: unable to allocate %u clocks for %s\n",
817 __func__
, ccu
->clk_data
.clk_num
, node
->name
);
822 ret
= of_address_to_resource(node
, 0, &res
);
824 pr_err("%s: no valid CCU registers found for %s\n", __func__
,
829 range
= resource_size(&res
);
830 if (range
> (resource_size_t
)U32_MAX
) {
831 pr_err("%s: address range too large for %s\n", __func__
,
836 ccu
->range
= (u32
)range
;
838 if (!ccu_data_valid(ccu
)) {
839 pr_err("%s: ccu data not valid for %s\n", __func__
, node
->name
);
843 ccu
->base
= ioremap(res
.start
, ccu
->range
);
845 pr_err("%s: unable to map CCU registers for %s\n", __func__
,
849 ccu
->node
= of_node_get(node
);
850 list_add_tail(&ccu
->links
, &ccu_list
);
853 * Set up each defined kona clock and save the result in
854 * the clock framework clock array (in ccu->data). Then
855 * register as a provider for these clocks.
857 for (i
= 0; i
< ccu
->clk_data
.clk_num
; i
++) {
858 if (!ccu
->kona_clks
[i
].ccu
)
860 ccu
->clk_data
.clks
[i
] = kona_clk_setup(&ccu
->kona_clks
[i
]);
863 ret
= of_clk_add_provider(node
, of_clk_src_onecell_get
, &ccu
->clk_data
);
865 pr_err("%s: error adding ccu %s as provider (%d)\n", __func__
,
870 if (!kona_ccu_init(ccu
))
871 pr_err("Broadcom %s initialization had errors\n", node
->name
);
875 kona_ccu_teardown(ccu
);
876 pr_err("Broadcom %s setup aborted\n", node
->name
);