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 clk_requires_trigger(struct kona_clk *bcm_clk)
30 struct peri_clk_data *peri = bcm_clk->peri;
31 struct bcm_clk_sel *sel;
32 struct bcm_clk_div *div;
34 if (bcm_clk->type != bcm_clk_peri)
38 if (sel->parent_count && selector_exists(sel))
42 if (!divider_exists(div))
45 /* Fixed dividers don't need triggers */
46 if (!divider_is_fixed(div))
51 return divider_exists(div) && !divider_is_fixed(div);
54 static bool peri_clk_data_offsets_valid(struct kona_clk *bcm_clk)
56 struct peri_clk_data *peri;
57 struct bcm_clk_gate *gate;
58 struct bcm_clk_div *div;
59 struct bcm_clk_sel *sel;
60 struct bcm_clk_trig *trig;
65 BUG_ON(bcm_clk->type != bcm_clk_peri);
68 range = bcm_clk->ccu->range;
70 limit = range - sizeof(u32);
71 limit = round_down(limit, sizeof(u32));
74 if (gate_exists(gate)) {
75 if (gate->offset > limit) {
76 pr_err("%s: bad gate offset for %s (%u > %u)\n",
77 __func__, name, gate->offset, limit);
83 if (divider_exists(div)) {
84 if (div->offset > limit) {
85 pr_err("%s: bad divider offset for %s (%u > %u)\n",
86 __func__, name, div->offset, limit);
92 if (divider_exists(div)) {
93 if (div->offset > limit) {
94 pr_err("%s: bad pre-divider offset for %s "
96 __func__, name, div->offset, limit);
102 if (selector_exists(sel)) {
103 if (sel->offset > limit) {
104 pr_err("%s: bad selector offset for %s (%u > %u)\n",
105 __func__, name, sel->offset, limit);
111 if (trigger_exists(trig)) {
112 if (trig->offset > limit) {
113 pr_err("%s: bad trigger offset for %s (%u > %u)\n",
114 __func__, name, trig->offset, limit);
119 trig = &peri->pre_trig;
120 if (trigger_exists(trig)) {
121 if (trig->offset > limit) {
122 pr_err("%s: bad pre-trigger offset for %s (%u > %u)\n",
123 __func__, name, trig->offset, limit);
131 /* A bit position must be less than the number of bits in a 32-bit register. */
132 static bool bit_posn_valid(u32 bit_posn, const char *field_name,
133 const char *clock_name)
135 u32 limit = BITS_PER_BYTE * sizeof(u32) - 1;
137 if (bit_posn > limit) {
138 pr_err("%s: bad %s bit for %s (%u > %u)\n", __func__,
139 field_name, clock_name, bit_posn, limit);
146 * A bitfield must be at least 1 bit wide. Both the low-order and
147 * high-order bits must lie within a 32-bit register. We require
148 * fields to be less than 32 bits wide, mainly because we use
149 * shifting to produce field masks, and shifting a full word width
150 * is not well-defined by the C standard.
152 static bool bitfield_valid(u32 shift, u32 width, const char *field_name,
153 const char *clock_name)
155 u32 limit = BITS_PER_BYTE * sizeof(u32);
158 pr_err("%s: bad %s field width 0 for %s\n", __func__,
159 field_name, clock_name);
162 if (shift + width > limit) {
163 pr_err("%s: bad %s for %s (%u + %u > %u)\n", __func__,
164 field_name, clock_name, shift, width, limit);
171 * All gates, if defined, have a status bit, and for hardware-only
172 * gates, that's it. Gates that can be software controlled also
173 * have an enable bit. And a gate that can be hardware or software
174 * controlled will have a hardware/software select bit.
176 static bool gate_valid(struct bcm_clk_gate *gate, const char *field_name,
177 const char *clock_name)
179 if (!bit_posn_valid(gate->status_bit, "gate status", clock_name))
182 if (gate_is_sw_controllable(gate)) {
183 if (!bit_posn_valid(gate->en_bit, "gate enable", clock_name))
186 if (gate_is_hw_controllable(gate)) {
187 if (!bit_posn_valid(gate->hw_sw_sel_bit,
193 BUG_ON(!gate_is_hw_controllable(gate));
200 * A selector bitfield must be valid. Its parent_sel array must
201 * also be reasonable for the field.
203 static bool sel_valid(struct bcm_clk_sel *sel, const char *field_name,
204 const char *clock_name)
206 if (!bitfield_valid(sel->shift, sel->width, field_name, clock_name))
209 if (sel->parent_count) {
214 * Make sure the selector field can hold all the
215 * selector values we expect to be able to use. A
216 * clock only needs to have a selector defined if it
217 * has more than one parent. And in that case the
218 * highest selector value will be in the last entry
221 max_sel = sel->parent_sel[sel->parent_count - 1];
222 limit = (1 << sel->width) - 1;
223 if (max_sel > limit) {
224 pr_err("%s: bad selector for %s "
225 "(%u needs > %u bits)\n",
226 __func__, clock_name, max_sel,
231 pr_warn("%s: ignoring selector for %s (no parents)\n",
232 __func__, clock_name);
233 selector_clear_exists(sel);
234 kfree(sel->parent_sel);
235 sel->parent_sel = NULL;
242 * A fixed divider just needs to be non-zero. A variable divider
243 * has to have a valid divider bitfield, and if it has a fraction,
244 * the width of the fraction must not be no more than the width of
245 * the divider as a whole.
247 static bool div_valid(struct bcm_clk_div *div, const char *field_name,
248 const char *clock_name)
250 if (divider_is_fixed(div)) {
251 /* Any fixed divider value but 0 is OK */
252 if (div->fixed == 0) {
253 pr_err("%s: bad %s fixed value 0 for %s\n", __func__,
254 field_name, clock_name);
259 if (!bitfield_valid(div->shift, div->width, field_name, clock_name))
262 if (divider_has_fraction(div))
263 if (div->frac_width > div->width) {
264 pr_warn("%s: bad %s fraction width for %s (%u > %u)\n",
265 __func__, field_name, clock_name,
266 div->frac_width, div->width);
274 * If a clock has two dividers, the combined number of fractional
275 * bits must be representable in a 32-bit unsigned value. This
276 * is because we scale up a dividend using both dividers before
277 * dividing to improve accuracy, and we need to avoid overflow.
279 static bool kona_dividers_valid(struct kona_clk *bcm_clk)
281 struct peri_clk_data *peri = bcm_clk->peri;
282 struct bcm_clk_div *div;
283 struct bcm_clk_div *pre_div;
286 BUG_ON(bcm_clk->type != bcm_clk_peri);
288 if (!divider_exists(&peri->div) || !divider_exists(&peri->pre_div))
292 pre_div = &peri->pre_div;
293 if (divider_is_fixed(div) || divider_is_fixed(pre_div))
296 limit = BITS_PER_BYTE * sizeof(u32);
298 return div->frac_width + pre_div->frac_width <= limit;
302 /* A trigger just needs to represent a valid bit position */
303 static bool trig_valid(struct bcm_clk_trig *trig, const char *field_name,
304 const char *clock_name)
306 return bit_posn_valid(trig->bit, field_name, clock_name);
309 /* Determine whether the set of peripheral clock registers are valid. */
311 peri_clk_data_valid(struct kona_clk *bcm_clk)
313 struct peri_clk_data *peri;
314 struct bcm_clk_gate *gate;
315 struct bcm_clk_sel *sel;
316 struct bcm_clk_div *div;
317 struct bcm_clk_div *pre_div;
318 struct bcm_clk_trig *trig;
321 BUG_ON(bcm_clk->type != bcm_clk_peri);
324 * First validate register offsets. This is the only place
325 * where we need something from the ccu, so we do these
328 if (!peri_clk_data_offsets_valid(bcm_clk))
331 peri = bcm_clk->peri;
332 name = bcm_clk->name;
334 if (gate_exists(gate) && !gate_valid(gate, "gate", name))
338 if (selector_exists(sel)) {
339 if (!sel_valid(sel, "selector", name))
342 } else if (sel->parent_count > 1) {
343 pr_err("%s: multiple parents but no selector for %s\n",
350 pre_div = &peri->pre_div;
351 if (divider_exists(div)) {
352 if (!div_valid(div, "divider", name))
355 if (divider_exists(pre_div))
356 if (!div_valid(pre_div, "pre-divider", name))
358 } else if (divider_exists(pre_div)) {
359 pr_err("%s: pre-divider but no divider for %s\n", __func__,
365 if (trigger_exists(trig)) {
366 if (!trig_valid(trig, "trigger", name))
369 if (trigger_exists(&peri->pre_trig)) {
370 if (!trig_valid(trig, "pre-trigger", name)) {
374 if (!clk_requires_trigger(bcm_clk)) {
375 pr_warn("%s: ignoring trigger for %s (not needed)\n",
377 trigger_clear_exists(trig);
379 } else if (trigger_exists(&peri->pre_trig)) {
380 pr_err("%s: pre-trigger but no trigger for %s\n", __func__,
383 } else if (clk_requires_trigger(bcm_clk)) {
384 pr_err("%s: required trigger missing for %s\n", __func__,
389 return kona_dividers_valid(bcm_clk);
392 static bool kona_clk_valid(struct kona_clk *bcm_clk)
394 switch (bcm_clk->type) {
396 if (!peri_clk_data_valid(bcm_clk))
400 pr_err("%s: unrecognized clock type (%d)\n", __func__,
408 * Scan an array of parent clock names to determine whether there
409 * are any entries containing BAD_CLK_NAME. Such entries are
410 * placeholders for non-supported clocks. Keep track of the
411 * position of each clock name in the original array.
413 * Allocates an array of pointers to to hold the names of all
414 * non-null entries in the original array, and returns a pointer to
415 * that array in *names. This will be used for registering the
416 * clock with the common clock code. On successful return,
417 * *count indicates how many entries are in that names array.
419 * If there is more than one entry in the resulting names array,
420 * another array is allocated to record the parent selector value
421 * for each (defined) parent clock. This is the value that
422 * represents this parent clock in the clock's source selector
423 * register. The position of the clock in the original parent array
424 * defines that selector value. The number of entries in this array
425 * is the same as the number of entries in the parent names array.
427 * The array of selector values is returned. If the clock has no
428 * parents, no selector is required and a null pointer is returned.
430 * Returns a null pointer if the clock names array supplied was
431 * null. (This is not an error.)
433 * Returns a pointer-coded error if an error occurs.
435 static u32 *parent_process(const char *clocks[],
436 u32 *count, const char ***names)
438 static const char **parent_names;
439 static u32 *parent_sel;
447 *count = 0; /* In case of early return */
453 * Count the number of names in the null-terminated array,
454 * and find out how many of those are actually clock names.
456 for (clock = clocks; *clock; clock++)
457 if (*clock == BAD_CLK_NAME)
459 orig_count = (u32)(clock - clocks);
460 parent_count = orig_count - bad_count;
462 /* If all clocks are unsupported, we treat it as no clock */
466 /* Avoid exceeding our parent clock limit */
467 if (parent_count > PARENT_COUNT_MAX) {
468 pr_err("%s: too many parents (%u > %u)\n", __func__,
469 parent_count, PARENT_COUNT_MAX);
470 return ERR_PTR(-EINVAL);
474 * There is one parent name for each defined parent clock.
475 * We also maintain an array containing the selector value
476 * for each defined clock. If there's only one clock, the
477 * selector is not required, but we allocate space for the
478 * array anyway to keep things simple.
480 parent_names = kmalloc(parent_count * sizeof(parent_names), GFP_KERNEL);
482 pr_err("%s: error allocating %u parent names\n", __func__,
484 return ERR_PTR(-ENOMEM);
487 /* There is at least one parent, so allocate a selector array */
489 parent_sel = kmalloc(parent_count * sizeof(*parent_sel), GFP_KERNEL);
491 pr_err("%s: error allocating %u parent selectors\n", __func__,
495 return ERR_PTR(-ENOMEM);
498 /* Now fill in the parent names and selector arrays */
499 for (i = 0, j = 0; i < orig_count; i++) {
500 if (clocks[i] != BAD_CLK_NAME) {
501 parent_names[j] = clocks[i];
506 *names = parent_names;
507 *count = parent_count;
513 clk_sel_setup(const char **clocks, struct bcm_clk_sel *sel,
514 struct clk_init_data *init_data)
516 const char **parent_names = NULL;
517 u32 parent_count = 0;
521 * If a peripheral clock has multiple parents, the value
522 * used by the hardware to select that parent is represented
523 * by the parent clock's position in the "clocks" list. Some
524 * values don't have defined or supported clocks; these will
525 * have BAD_CLK_NAME entries in the parents[] array. The
526 * list is terminated by a NULL entry.
528 * We need to supply (only) the names of defined parent
529 * clocks when registering a clock though, so we use an
530 * array of parent selector values to map between the
531 * indexes the common clock code uses and the selector
534 parent_sel = parent_process(clocks, &parent_count, &parent_names);
535 if (IS_ERR(parent_sel)) {
536 int ret = PTR_ERR(parent_sel);
538 pr_err("%s: error processing parent clocks for %s (%d)\n",
539 __func__, init_data->name, ret);
544 init_data->parent_names = parent_names;
545 init_data->num_parents = parent_count;
547 sel->parent_count = parent_count;
548 sel->parent_sel = parent_sel;
553 static void clk_sel_teardown(struct bcm_clk_sel *sel,
554 struct clk_init_data *init_data)
556 kfree(sel->parent_sel);
557 sel->parent_sel = NULL;
558 sel->parent_count = 0;
560 init_data->num_parents = 0;
561 kfree(init_data->parent_names);
562 init_data->parent_names = NULL;
565 static void peri_clk_teardown(struct peri_clk_data *data,
566 struct clk_init_data *init_data)
568 clk_sel_teardown(&data->sel, init_data);
569 init_data->ops = NULL;
573 * Caller is responsible for freeing the parent_names[] and
574 * parent_sel[] arrays in the peripheral clock's "data" structure
575 * that can be assigned if the clock has one or more parent clocks
576 * associated with it.
578 static int peri_clk_setup(struct ccu_data *ccu, struct peri_clk_data *data,
579 struct clk_init_data *init_data)
581 init_data->ops = &kona_peri_clk_ops;
582 init_data->flags = CLK_IGNORE_UNUSED;
584 return clk_sel_setup(data->clocks, &data->sel, init_data);
587 static void bcm_clk_teardown(struct kona_clk *bcm_clk)
589 switch (bcm_clk->type) {
591 peri_clk_teardown(bcm_clk->data, &bcm_clk->init_data);
596 bcm_clk->data = NULL;
597 bcm_clk->type = bcm_clk_none;
600 static void kona_clk_teardown(struct clk *clk)
603 struct kona_clk *bcm_clk;
608 hw = __clk_get_hw(clk);
610 pr_err("%s: clk %p has null hw pointer\n", __func__, clk);
615 bcm_clk = to_kona_clk(hw);
616 bcm_clk_teardown(bcm_clk);
619 struct clk *kona_clk_setup(struct ccu_data *ccu, const char *name,
620 enum bcm_clk_type type, void *data)
622 struct kona_clk *bcm_clk;
623 struct clk_init_data *init_data;
624 struct clk *clk = NULL;
626 bcm_clk = kzalloc(sizeof(*bcm_clk), GFP_KERNEL);
628 pr_err("%s: failed to allocate bcm_clk for %s\n", __func__,
633 bcm_clk->name = name;
635 init_data = &bcm_clk->init_data;
636 init_data->name = name;
639 if (peri_clk_setup(ccu, data, init_data))
646 bcm_clk->type = type;
647 bcm_clk->data = data;
649 /* Make sure everything makes sense before we set it up */
650 if (!kona_clk_valid(bcm_clk)) {
651 pr_err("%s: clock data invalid for %s\n", __func__, name);
655 bcm_clk->hw.init = init_data;
656 clk = clk_register(NULL, &bcm_clk->hw);
658 pr_err("%s: error registering clock %s (%ld)\n", __func__,
666 bcm_clk_teardown(bcm_clk);
673 static void ccu_clks_teardown(struct ccu_data *ccu)
677 for (i = 0; i < ccu->data.clk_num; i++)
678 kona_clk_teardown(ccu->data.clks[i]);
679 kfree(ccu->data.clks);
682 static void kona_ccu_teardown(struct ccu_data *ccu)
690 of_clk_del_provider(ccu->node); /* safe if never added */
691 ccu_clks_teardown(ccu);
692 list_del(&ccu->links);
693 of_node_put(ccu->node);
701 * Set up a CCU. Call the provided ccu_clks_setup callback to
702 * initialize the array of clocks provided by the CCU.
704 void __init kona_dt_ccu_setup(struct device_node *node,
705 int (*ccu_clks_setup)(struct ccu_data *))
707 struct ccu_data *ccu;
708 struct resource res = { 0 };
709 resource_size_t range;
712 ccu = kzalloc(sizeof(*ccu), GFP_KERNEL);
714 ccu->name = kstrdup(node->name, GFP_KERNEL);
715 if (!ccu || !ccu->name) {
716 pr_err("%s: unable to allocate CCU struct for %s\n",
717 __func__, node->name);
723 ret = of_address_to_resource(node, 0, &res);
725 pr_err("%s: no valid CCU registers found for %s\n", __func__,
730 range = resource_size(&res);
731 if (range > (resource_size_t)U32_MAX) {
732 pr_err("%s: address range too large for %s\n", __func__,
737 ccu->range = (u32)range;
738 ccu->base = ioremap(res.start, ccu->range);
740 pr_err("%s: unable to map CCU registers for %s\n", __func__,
745 spin_lock_init(&ccu->lock);
746 INIT_LIST_HEAD(&ccu->links);
747 ccu->node = of_node_get(node);
749 list_add_tail(&ccu->links, &ccu_list);
751 /* Set up clocks array (in ccu->data) */
752 if (ccu_clks_setup(ccu))
755 ret = of_clk_add_provider(node, of_clk_src_onecell_get, &ccu->data);
757 pr_err("%s: error adding ccu %s as provider (%d)\n", __func__,
762 if (!kona_ccu_init(ccu))
763 pr_err("Broadcom %s initialization had errors\n", node->name);
767 kona_ccu_teardown(ccu);
768 pr_err("Broadcom %s setup aborted\n", node->name);