blob: deaa7f962b84ac814eb2445d698d343d45018aec [file] [log] [blame]
Alex Elder1f27f152014-02-14 12:29:18 -06001/*
2 * Copyright (C) 2013 Broadcom Corporation
3 * Copyright 2013 Linaro Limited
4 *
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.
8 *
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.
13 */
14
15#include <linux/io.h>
16#include <linux/of_address.h>
17
18#include "clk-kona.h"
19
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)
23
Alex Elder1f27f152014-02-14 12:29:18 -060024/* Validity checking */
25
Alex Eldera597fac2014-04-21 16:11:42 -050026static bool ccu_data_offsets_valid(struct ccu_data *ccu)
27{
28 struct ccu_policy *ccu_policy = &ccu->policy;
29 u32 limit;
30
31 limit = ccu->range - sizeof(u32);
32 limit = round_down(limit, sizeof(u32));
33 if (ccu_policy_exists(ccu_policy)) {
34 if (ccu_policy->enable.offset > limit) {
35 pr_err("%s: bad policy enable offset for %s "
36 "(%u > %u)\n", __func__,
37 ccu->name, ccu_policy->enable.offset, limit);
38 return false;
39 }
40 if (ccu_policy->control.offset > limit) {
41 pr_err("%s: bad policy control offset for %s "
42 "(%u > %u)\n", __func__,
43 ccu->name, ccu_policy->control.offset, limit);
44 return false;
45 }
46 }
47
48 return true;
49}
50
Alex Elder1f27f152014-02-14 12:29:18 -060051static bool clk_requires_trigger(struct kona_clk *bcm_clk)
52{
Alex Eldere813d492014-04-07 08:22:12 -050053 struct peri_clk_data *peri = bcm_clk->u.peri;
Alex Elder1f27f152014-02-14 12:29:18 -060054 struct bcm_clk_sel *sel;
55 struct bcm_clk_div *div;
56
57 if (bcm_clk->type != bcm_clk_peri)
58 return false;
59
60 sel = &peri->sel;
61 if (sel->parent_count && selector_exists(sel))
62 return true;
63
64 div = &peri->div;
65 if (!divider_exists(div))
66 return false;
67
68 /* Fixed dividers don't need triggers */
69 if (!divider_is_fixed(div))
70 return true;
71
72 div = &peri->pre_div;
73
74 return divider_exists(div) && !divider_is_fixed(div);
75}
76
77static bool peri_clk_data_offsets_valid(struct kona_clk *bcm_clk)
78{
79 struct peri_clk_data *peri;
Alex Eldera597fac2014-04-21 16:11:42 -050080 struct bcm_clk_policy *policy;
Alex Elder1f27f152014-02-14 12:29:18 -060081 struct bcm_clk_gate *gate;
Alex Elderdc613842014-04-21 16:11:43 -050082 struct bcm_clk_hyst *hyst;
Alex Elder1f27f152014-02-14 12:29:18 -060083 struct bcm_clk_div *div;
84 struct bcm_clk_sel *sel;
85 struct bcm_clk_trig *trig;
86 const char *name;
87 u32 range;
88 u32 limit;
89
90 BUG_ON(bcm_clk->type != bcm_clk_peri);
Alex Eldere813d492014-04-07 08:22:12 -050091 peri = bcm_clk->u.peri;
Alex Eldere7563252014-04-21 16:11:38 -050092 name = bcm_clk->init_data.name;
Alex Elder1f27f152014-02-14 12:29:18 -060093 range = bcm_clk->ccu->range;
94
95 limit = range - sizeof(u32);
96 limit = round_down(limit, sizeof(u32));
97
Alex Eldera597fac2014-04-21 16:11:42 -050098 policy = &peri->policy;
99 if (policy_exists(policy)) {
100 if (policy->offset > limit) {
101 pr_err("%s: bad policy offset for %s (%u > %u)\n",
102 __func__, name, policy->offset, limit);
103 return false;
104 }
105 }
106
Alex Elder1f27f152014-02-14 12:29:18 -0600107 gate = &peri->gate;
Alex Elderdc613842014-04-21 16:11:43 -0500108 hyst = &peri->hyst;
Alex Elder1f27f152014-02-14 12:29:18 -0600109 if (gate_exists(gate)) {
110 if (gate->offset > limit) {
111 pr_err("%s: bad gate offset for %s (%u > %u)\n",
112 __func__, name, gate->offset, limit);
113 return false;
114 }
Alex Elderdc613842014-04-21 16:11:43 -0500115
116 if (hyst_exists(hyst)) {
117 if (hyst->offset > limit) {
118 pr_err("%s: bad hysteresis offset for %s "
119 "(%u > %u)\n", __func__,
120 name, hyst->offset, limit);
121 return false;
122 }
123 }
124 } else if (hyst_exists(hyst)) {
125 pr_err("%s: hysteresis but no gate for %s\n", __func__, name);
126 return false;
Alex Elder1f27f152014-02-14 12:29:18 -0600127 }
128
129 div = &peri->div;
130 if (divider_exists(div)) {
Alex Eldere813d492014-04-07 08:22:12 -0500131 if (div->u.s.offset > limit) {
Alex Elder1f27f152014-02-14 12:29:18 -0600132 pr_err("%s: bad divider offset for %s (%u > %u)\n",
Alex Eldere813d492014-04-07 08:22:12 -0500133 __func__, name, div->u.s.offset, limit);
Alex Elder1f27f152014-02-14 12:29:18 -0600134 return false;
135 }
136 }
137
138 div = &peri->pre_div;
139 if (divider_exists(div)) {
Alex Eldere813d492014-04-07 08:22:12 -0500140 if (div->u.s.offset > limit) {
Alex Elder1f27f152014-02-14 12:29:18 -0600141 pr_err("%s: bad pre-divider offset for %s "
142 "(%u > %u)\n",
Alex Eldere813d492014-04-07 08:22:12 -0500143 __func__, name, div->u.s.offset, limit);
Alex Elder1f27f152014-02-14 12:29:18 -0600144 return false;
145 }
146 }
147
148 sel = &peri->sel;
149 if (selector_exists(sel)) {
150 if (sel->offset > limit) {
151 pr_err("%s: bad selector offset for %s (%u > %u)\n",
152 __func__, name, sel->offset, limit);
153 return false;
154 }
155 }
156
157 trig = &peri->trig;
158 if (trigger_exists(trig)) {
159 if (trig->offset > limit) {
160 pr_err("%s: bad trigger offset for %s (%u > %u)\n",
161 __func__, name, trig->offset, limit);
162 return false;
163 }
164 }
165
166 trig = &peri->pre_trig;
167 if (trigger_exists(trig)) {
168 if (trig->offset > limit) {
169 pr_err("%s: bad pre-trigger offset for %s (%u > %u)\n",
170 __func__, name, trig->offset, limit);
171 return false;
172 }
173 }
174
175 return true;
176}
177
178/* A bit position must be less than the number of bits in a 32-bit register. */
179static bool bit_posn_valid(u32 bit_posn, const char *field_name,
180 const char *clock_name)
181{
182 u32 limit = BITS_PER_BYTE * sizeof(u32) - 1;
183
184 if (bit_posn > limit) {
185 pr_err("%s: bad %s bit for %s (%u > %u)\n", __func__,
186 field_name, clock_name, bit_posn, limit);
187 return false;
188 }
189 return true;
190}
191
192/*
193 * A bitfield must be at least 1 bit wide. Both the low-order and
194 * high-order bits must lie within a 32-bit register. We require
195 * fields to be less than 32 bits wide, mainly because we use
196 * shifting to produce field masks, and shifting a full word width
197 * is not well-defined by the C standard.
198 */
199static bool bitfield_valid(u32 shift, u32 width, const char *field_name,
200 const char *clock_name)
201{
202 u32 limit = BITS_PER_BYTE * sizeof(u32);
203
204 if (!width) {
205 pr_err("%s: bad %s field width 0 for %s\n", __func__,
206 field_name, clock_name);
207 return false;
208 }
209 if (shift + width > limit) {
210 pr_err("%s: bad %s for %s (%u + %u > %u)\n", __func__,
211 field_name, clock_name, shift, width, limit);
212 return false;
213 }
214 return true;
215}
216
Alex Eldera597fac2014-04-21 16:11:42 -0500217static bool
218ccu_policy_valid(struct ccu_policy *ccu_policy, const char *ccu_name)
219{
220 struct bcm_lvm_en *enable = &ccu_policy->enable;
221 struct bcm_policy_ctl *control;
222
223 if (!bit_posn_valid(enable->bit, "policy enable", ccu_name))
224 return false;
225
226 control = &ccu_policy->control;
227 if (!bit_posn_valid(control->go_bit, "policy control GO", ccu_name))
228 return false;
229
230 if (!bit_posn_valid(control->atl_bit, "policy control ATL", ccu_name))
231 return false;
232
233 if (!bit_posn_valid(control->ac_bit, "policy control AC", ccu_name))
234 return false;
235
236 return true;
237}
238
239static bool policy_valid(struct bcm_clk_policy *policy, const char *clock_name)
240{
241 if (!bit_posn_valid(policy->bit, "policy", clock_name))
242 return false;
243
244 return true;
245}
246
Alex Elder1f27f152014-02-14 12:29:18 -0600247/*
248 * All gates, if defined, have a status bit, and for hardware-only
249 * gates, that's it. Gates that can be software controlled also
250 * have an enable bit. And a gate that can be hardware or software
251 * controlled will have a hardware/software select bit.
252 */
253static bool gate_valid(struct bcm_clk_gate *gate, const char *field_name,
254 const char *clock_name)
255{
256 if (!bit_posn_valid(gate->status_bit, "gate status", clock_name))
257 return false;
258
259 if (gate_is_sw_controllable(gate)) {
260 if (!bit_posn_valid(gate->en_bit, "gate enable", clock_name))
261 return false;
262
263 if (gate_is_hw_controllable(gate)) {
264 if (!bit_posn_valid(gate->hw_sw_sel_bit,
265 "gate hw/sw select",
266 clock_name))
267 return false;
268 }
269 } else {
270 BUG_ON(!gate_is_hw_controllable(gate));
271 }
272
273 return true;
274}
275
Alex Elderdc613842014-04-21 16:11:43 -0500276static bool hyst_valid(struct bcm_clk_hyst *hyst, const char *clock_name)
277{
278 if (!bit_posn_valid(hyst->en_bit, "hysteresis enable", clock_name))
279 return false;
280
281 if (!bit_posn_valid(hyst->val_bit, "hysteresis value", clock_name))
282 return false;
283
284 return true;
285}
286
Alex Elder1f27f152014-02-14 12:29:18 -0600287/*
288 * A selector bitfield must be valid. Its parent_sel array must
289 * also be reasonable for the field.
290 */
291static bool sel_valid(struct bcm_clk_sel *sel, const char *field_name,
292 const char *clock_name)
293{
294 if (!bitfield_valid(sel->shift, sel->width, field_name, clock_name))
295 return false;
296
297 if (sel->parent_count) {
298 u32 max_sel;
299 u32 limit;
300
301 /*
302 * Make sure the selector field can hold all the
303 * selector values we expect to be able to use. A
304 * clock only needs to have a selector defined if it
305 * has more than one parent. And in that case the
306 * highest selector value will be in the last entry
307 * in the array.
308 */
309 max_sel = sel->parent_sel[sel->parent_count - 1];
310 limit = (1 << sel->width) - 1;
311 if (max_sel > limit) {
312 pr_err("%s: bad selector for %s "
313 "(%u needs > %u bits)\n",
314 __func__, clock_name, max_sel,
315 sel->width);
316 return false;
317 }
318 } else {
319 pr_warn("%s: ignoring selector for %s (no parents)\n",
320 __func__, clock_name);
321 selector_clear_exists(sel);
322 kfree(sel->parent_sel);
323 sel->parent_sel = NULL;
324 }
325
326 return true;
327}
328
329/*
330 * A fixed divider just needs to be non-zero. A variable divider
331 * has to have a valid divider bitfield, and if it has a fraction,
332 * the width of the fraction must not be no more than the width of
333 * the divider as a whole.
334 */
335static bool div_valid(struct bcm_clk_div *div, const char *field_name,
336 const char *clock_name)
337{
338 if (divider_is_fixed(div)) {
339 /* Any fixed divider value but 0 is OK */
Alex Eldere813d492014-04-07 08:22:12 -0500340 if (div->u.fixed == 0) {
Alex Elder1f27f152014-02-14 12:29:18 -0600341 pr_err("%s: bad %s fixed value 0 for %s\n", __func__,
342 field_name, clock_name);
343 return false;
344 }
345 return true;
346 }
Alex Eldere813d492014-04-07 08:22:12 -0500347 if (!bitfield_valid(div->u.s.shift, div->u.s.width,
348 field_name, clock_name))
Alex Elder1f27f152014-02-14 12:29:18 -0600349 return false;
350
351 if (divider_has_fraction(div))
Alex Eldere813d492014-04-07 08:22:12 -0500352 if (div->u.s.frac_width > div->u.s.width) {
Alex Elder1f27f152014-02-14 12:29:18 -0600353 pr_warn("%s: bad %s fraction width for %s (%u > %u)\n",
354 __func__, field_name, clock_name,
Alex Eldere813d492014-04-07 08:22:12 -0500355 div->u.s.frac_width, div->u.s.width);
Alex Elder1f27f152014-02-14 12:29:18 -0600356 return false;
357 }
358
359 return true;
360}
361
362/*
363 * If a clock has two dividers, the combined number of fractional
364 * bits must be representable in a 32-bit unsigned value. This
365 * is because we scale up a dividend using both dividers before
366 * dividing to improve accuracy, and we need to avoid overflow.
367 */
368static bool kona_dividers_valid(struct kona_clk *bcm_clk)
369{
Alex Eldere813d492014-04-07 08:22:12 -0500370 struct peri_clk_data *peri = bcm_clk->u.peri;
Alex Elder1f27f152014-02-14 12:29:18 -0600371 struct bcm_clk_div *div;
372 struct bcm_clk_div *pre_div;
373 u32 limit;
374
375 BUG_ON(bcm_clk->type != bcm_clk_peri);
376
377 if (!divider_exists(&peri->div) || !divider_exists(&peri->pre_div))
378 return true;
379
380 div = &peri->div;
381 pre_div = &peri->pre_div;
382 if (divider_is_fixed(div) || divider_is_fixed(pre_div))
383 return true;
384
385 limit = BITS_PER_BYTE * sizeof(u32);
386
Alex Eldere813d492014-04-07 08:22:12 -0500387 return div->u.s.frac_width + pre_div->u.s.frac_width <= limit;
Alex Elder1f27f152014-02-14 12:29:18 -0600388}
389
390
391/* A trigger just needs to represent a valid bit position */
392static bool trig_valid(struct bcm_clk_trig *trig, const char *field_name,
393 const char *clock_name)
394{
395 return bit_posn_valid(trig->bit, field_name, clock_name);
396}
397
398/* Determine whether the set of peripheral clock registers are valid. */
399static bool
400peri_clk_data_valid(struct kona_clk *bcm_clk)
401{
402 struct peri_clk_data *peri;
Alex Eldera597fac2014-04-21 16:11:42 -0500403 struct bcm_clk_policy *policy;
Alex Elder1f27f152014-02-14 12:29:18 -0600404 struct bcm_clk_gate *gate;
Alex Elderdc613842014-04-21 16:11:43 -0500405 struct bcm_clk_hyst *hyst;
Alex Elder1f27f152014-02-14 12:29:18 -0600406 struct bcm_clk_sel *sel;
407 struct bcm_clk_div *div;
408 struct bcm_clk_div *pre_div;
409 struct bcm_clk_trig *trig;
410 const char *name;
411
412 BUG_ON(bcm_clk->type != bcm_clk_peri);
413
414 /*
415 * First validate register offsets. This is the only place
416 * where we need something from the ccu, so we do these
417 * together.
418 */
419 if (!peri_clk_data_offsets_valid(bcm_clk))
420 return false;
421
Alex Eldere813d492014-04-07 08:22:12 -0500422 peri = bcm_clk->u.peri;
Alex Eldere7563252014-04-21 16:11:38 -0500423 name = bcm_clk->init_data.name;
Alex Eldera597fac2014-04-21 16:11:42 -0500424
425 policy = &peri->policy;
426 if (policy_exists(policy) && !policy_valid(policy, name))
427 return false;
428
Alex Elder1f27f152014-02-14 12:29:18 -0600429 gate = &peri->gate;
430 if (gate_exists(gate) && !gate_valid(gate, "gate", name))
431 return false;
432
Alex Elderdc613842014-04-21 16:11:43 -0500433 hyst = &peri->hyst;
434 if (hyst_exists(hyst) && !hyst_valid(hyst, name))
435 return false;
436
Alex Elder1f27f152014-02-14 12:29:18 -0600437 sel = &peri->sel;
438 if (selector_exists(sel)) {
439 if (!sel_valid(sel, "selector", name))
440 return false;
441
442 } else if (sel->parent_count > 1) {
443 pr_err("%s: multiple parents but no selector for %s\n",
444 __func__, name);
445
446 return false;
447 }
448
449 div = &peri->div;
450 pre_div = &peri->pre_div;
451 if (divider_exists(div)) {
452 if (!div_valid(div, "divider", name))
453 return false;
454
455 if (divider_exists(pre_div))
456 if (!div_valid(pre_div, "pre-divider", name))
457 return false;
458 } else if (divider_exists(pre_div)) {
459 pr_err("%s: pre-divider but no divider for %s\n", __func__,
460 name);
461 return false;
462 }
463
464 trig = &peri->trig;
465 if (trigger_exists(trig)) {
466 if (!trig_valid(trig, "trigger", name))
467 return false;
468
469 if (trigger_exists(&peri->pre_trig)) {
470 if (!trig_valid(trig, "pre-trigger", name)) {
471 return false;
472 }
473 }
474 if (!clk_requires_trigger(bcm_clk)) {
475 pr_warn("%s: ignoring trigger for %s (not needed)\n",
476 __func__, name);
477 trigger_clear_exists(trig);
478 }
479 } else if (trigger_exists(&peri->pre_trig)) {
480 pr_err("%s: pre-trigger but no trigger for %s\n", __func__,
481 name);
482 return false;
483 } else if (clk_requires_trigger(bcm_clk)) {
484 pr_err("%s: required trigger missing for %s\n", __func__,
485 name);
486 return false;
487 }
488
489 return kona_dividers_valid(bcm_clk);
490}
491
492static bool kona_clk_valid(struct kona_clk *bcm_clk)
493{
494 switch (bcm_clk->type) {
495 case bcm_clk_peri:
496 if (!peri_clk_data_valid(bcm_clk))
497 return false;
498 break;
499 default:
500 pr_err("%s: unrecognized clock type (%d)\n", __func__,
501 (int)bcm_clk->type);
502 return false;
503 }
504 return true;
505}
506
507/*
508 * Scan an array of parent clock names to determine whether there
509 * are any entries containing BAD_CLK_NAME. Such entries are
510 * placeholders for non-supported clocks. Keep track of the
511 * position of each clock name in the original array.
512 *
513 * Allocates an array of pointers to to hold the names of all
514 * non-null entries in the original array, and returns a pointer to
515 * that array in *names. This will be used for registering the
516 * clock with the common clock code. On successful return,
517 * *count indicates how many entries are in that names array.
518 *
519 * If there is more than one entry in the resulting names array,
520 * another array is allocated to record the parent selector value
521 * for each (defined) parent clock. This is the value that
522 * represents this parent clock in the clock's source selector
523 * register. The position of the clock in the original parent array
524 * defines that selector value. The number of entries in this array
525 * is the same as the number of entries in the parent names array.
526 *
527 * The array of selector values is returned. If the clock has no
528 * parents, no selector is required and a null pointer is returned.
529 *
530 * Returns a null pointer if the clock names array supplied was
531 * null. (This is not an error.)
532 *
533 * Returns a pointer-coded error if an error occurs.
534 */
535static u32 *parent_process(const char *clocks[],
536 u32 *count, const char ***names)
537{
538 static const char **parent_names;
539 static u32 *parent_sel;
540 const char **clock;
541 u32 parent_count;
542 u32 bad_count = 0;
543 u32 orig_count;
544 u32 i;
545 u32 j;
546
547 *count = 0; /* In case of early return */
548 *names = NULL;
549 if (!clocks)
550 return NULL;
551
552 /*
553 * Count the number of names in the null-terminated array,
554 * and find out how many of those are actually clock names.
555 */
556 for (clock = clocks; *clock; clock++)
557 if (*clock == BAD_CLK_NAME)
558 bad_count++;
559 orig_count = (u32)(clock - clocks);
560 parent_count = orig_count - bad_count;
561
562 /* If all clocks are unsupported, we treat it as no clock */
563 if (!parent_count)
564 return NULL;
565
566 /* Avoid exceeding our parent clock limit */
567 if (parent_count > PARENT_COUNT_MAX) {
568 pr_err("%s: too many parents (%u > %u)\n", __func__,
569 parent_count, PARENT_COUNT_MAX);
570 return ERR_PTR(-EINVAL);
571 }
572
573 /*
574 * There is one parent name for each defined parent clock.
575 * We also maintain an array containing the selector value
576 * for each defined clock. If there's only one clock, the
577 * selector is not required, but we allocate space for the
578 * array anyway to keep things simple.
579 */
580 parent_names = kmalloc(parent_count * sizeof(parent_names), GFP_KERNEL);
581 if (!parent_names) {
582 pr_err("%s: error allocating %u parent names\n", __func__,
583 parent_count);
584 return ERR_PTR(-ENOMEM);
585 }
586
587 /* There is at least one parent, so allocate a selector array */
588
589 parent_sel = kmalloc(parent_count * sizeof(*parent_sel), GFP_KERNEL);
590 if (!parent_sel) {
591 pr_err("%s: error allocating %u parent selectors\n", __func__,
592 parent_count);
593 kfree(parent_names);
594
595 return ERR_PTR(-ENOMEM);
596 }
597
598 /* Now fill in the parent names and selector arrays */
599 for (i = 0, j = 0; i < orig_count; i++) {
600 if (clocks[i] != BAD_CLK_NAME) {
601 parent_names[j] = clocks[i];
602 parent_sel[j] = i;
603 j++;
604 }
605 }
606 *names = parent_names;
607 *count = parent_count;
608
609 return parent_sel;
610}
611
612static int
613clk_sel_setup(const char **clocks, struct bcm_clk_sel *sel,
614 struct clk_init_data *init_data)
615{
616 const char **parent_names = NULL;
617 u32 parent_count = 0;
618 u32 *parent_sel;
619
620 /*
621 * If a peripheral clock has multiple parents, the value
622 * used by the hardware to select that parent is represented
623 * by the parent clock's position in the "clocks" list. Some
624 * values don't have defined or supported clocks; these will
625 * have BAD_CLK_NAME entries in the parents[] array. The
626 * list is terminated by a NULL entry.
627 *
628 * We need to supply (only) the names of defined parent
629 * clocks when registering a clock though, so we use an
630 * array of parent selector values to map between the
631 * indexes the common clock code uses and the selector
632 * values we need.
633 */
634 parent_sel = parent_process(clocks, &parent_count, &parent_names);
635 if (IS_ERR(parent_sel)) {
636 int ret = PTR_ERR(parent_sel);
637
638 pr_err("%s: error processing parent clocks for %s (%d)\n",
639 __func__, init_data->name, ret);
640
641 return ret;
642 }
643
644 init_data->parent_names = parent_names;
645 init_data->num_parents = parent_count;
646
647 sel->parent_count = parent_count;
648 sel->parent_sel = parent_sel;
649
650 return 0;
651}
652
653static void clk_sel_teardown(struct bcm_clk_sel *sel,
654 struct clk_init_data *init_data)
655{
656 kfree(sel->parent_sel);
657 sel->parent_sel = NULL;
658 sel->parent_count = 0;
659
660 init_data->num_parents = 0;
661 kfree(init_data->parent_names);
662 init_data->parent_names = NULL;
663}
664
665static void peri_clk_teardown(struct peri_clk_data *data,
666 struct clk_init_data *init_data)
667{
668 clk_sel_teardown(&data->sel, init_data);
Alex Elder1f27f152014-02-14 12:29:18 -0600669}
670
671/*
672 * Caller is responsible for freeing the parent_names[] and
673 * parent_sel[] arrays in the peripheral clock's "data" structure
674 * that can be assigned if the clock has one or more parent clocks
675 * associated with it.
676 */
Alex Elder03548ec2014-04-21 16:11:41 -0500677static int
678peri_clk_setup(struct peri_clk_data *data, struct clk_init_data *init_data)
Alex Elder1f27f152014-02-14 12:29:18 -0600679{
Alex Elder1a5823c2014-02-14 12:29:19 -0600680 init_data->flags = CLK_IGNORE_UNUSED;
Alex Elder1f27f152014-02-14 12:29:18 -0600681
682 return clk_sel_setup(data->clocks, &data->sel, init_data);
683}
684
685static void bcm_clk_teardown(struct kona_clk *bcm_clk)
686{
687 switch (bcm_clk->type) {
688 case bcm_clk_peri:
Alex Eldere813d492014-04-07 08:22:12 -0500689 peri_clk_teardown(bcm_clk->u.data, &bcm_clk->init_data);
Alex Elder1f27f152014-02-14 12:29:18 -0600690 break;
691 default:
692 break;
693 }
Alex Eldere813d492014-04-07 08:22:12 -0500694 bcm_clk->u.data = NULL;
Alex Elder1f27f152014-02-14 12:29:18 -0600695 bcm_clk->type = bcm_clk_none;
696}
697
698static void kona_clk_teardown(struct clk *clk)
699{
700 struct clk_hw *hw;
701 struct kona_clk *bcm_clk;
702
703 if (!clk)
704 return;
705
706 hw = __clk_get_hw(clk);
707 if (!hw) {
708 pr_err("%s: clk %p has null hw pointer\n", __func__, clk);
709 return;
710 }
711 clk_unregister(clk);
712
713 bcm_clk = to_kona_clk(hw);
714 bcm_clk_teardown(bcm_clk);
715}
716
Alex Elder03548ec2014-04-21 16:11:41 -0500717struct clk *kona_clk_setup(struct kona_clk *bcm_clk)
Alex Elder1f27f152014-02-14 12:29:18 -0600718{
Alex Elder03548ec2014-04-21 16:11:41 -0500719 struct clk_init_data *init_data = &bcm_clk->init_data;
Alex Elder1f27f152014-02-14 12:29:18 -0600720 struct clk *clk = NULL;
721
Alex Elder03548ec2014-04-21 16:11:41 -0500722 switch (bcm_clk->type) {
Alex Elder1f27f152014-02-14 12:29:18 -0600723 case bcm_clk_peri:
Alex Elder03548ec2014-04-21 16:11:41 -0500724 if (peri_clk_setup(bcm_clk->u.data, init_data))
725 return NULL;
Alex Elder1f27f152014-02-14 12:29:18 -0600726 break;
727 default:
Alex Elder03548ec2014-04-21 16:11:41 -0500728 pr_err("%s: clock type %d invalid for %s\n", __func__,
729 (int)bcm_clk->type, init_data->name);
730 return NULL;
Alex Elder1f27f152014-02-14 12:29:18 -0600731 }
Alex Elder1f27f152014-02-14 12:29:18 -0600732
733 /* Make sure everything makes sense before we set it up */
734 if (!kona_clk_valid(bcm_clk)) {
Alex Elder03548ec2014-04-21 16:11:41 -0500735 pr_err("%s: clock data invalid for %s\n", __func__,
736 init_data->name);
Alex Elder1f27f152014-02-14 12:29:18 -0600737 goto out_teardown;
738 }
739
740 bcm_clk->hw.init = init_data;
741 clk = clk_register(NULL, &bcm_clk->hw);
742 if (IS_ERR(clk)) {
743 pr_err("%s: error registering clock %s (%ld)\n", __func__,
Alex Elder03548ec2014-04-21 16:11:41 -0500744 init_data->name, PTR_ERR(clk));
Alex Elder1f27f152014-02-14 12:29:18 -0600745 goto out_teardown;
746 }
747 BUG_ON(!clk);
748
749 return clk;
750out_teardown:
751 bcm_clk_teardown(bcm_clk);
Alex Elder1f27f152014-02-14 12:29:18 -0600752
753 return NULL;
754}
755
756static void ccu_clks_teardown(struct ccu_data *ccu)
757{
758 u32 i;
759
Alex Elderb12151c2014-04-21 16:11:40 -0500760 for (i = 0; i < ccu->clk_data.clk_num; i++)
761 kona_clk_teardown(ccu->clk_data.clks[i]);
762 kfree(ccu->clk_data.clks);
Alex Elder1f27f152014-02-14 12:29:18 -0600763}
764
765static void kona_ccu_teardown(struct ccu_data *ccu)
766{
Alex Elderb12151c2014-04-21 16:11:40 -0500767 kfree(ccu->clk_data.clks);
768 ccu->clk_data.clks = NULL;
Alex Elder1f27f152014-02-14 12:29:18 -0600769 if (!ccu->base)
Alex Elderb12151c2014-04-21 16:11:40 -0500770 return;
Alex Elder1f27f152014-02-14 12:29:18 -0600771
772 of_clk_del_provider(ccu->node); /* safe if never added */
773 ccu_clks_teardown(ccu);
Alex Elder1f27f152014-02-14 12:29:18 -0600774 of_node_put(ccu->node);
Alex Elderb12151c2014-04-21 16:11:40 -0500775 ccu->node = NULL;
Alex Elder1f27f152014-02-14 12:29:18 -0600776 iounmap(ccu->base);
Alex Elderb12151c2014-04-21 16:11:40 -0500777 ccu->base = NULL;
Alex Elder1f27f152014-02-14 12:29:18 -0600778}
779
Alex Eldera597fac2014-04-21 16:11:42 -0500780static bool ccu_data_valid(struct ccu_data *ccu)
781{
782 struct ccu_policy *ccu_policy;
783
784 if (!ccu_data_offsets_valid(ccu))
785 return false;
786
787 ccu_policy = &ccu->policy;
788 if (ccu_policy_exists(ccu_policy))
789 if (!ccu_policy_valid(ccu_policy, ccu->name))
790 return false;
791
792 return true;
793}
794
Alex Elder1f27f152014-02-14 12:29:18 -0600795/*
796 * Set up a CCU. Call the provided ccu_clks_setup callback to
797 * initialize the array of clocks provided by the CCU.
798 */
Alex Elderb12151c2014-04-21 16:11:40 -0500799void __init kona_dt_ccu_setup(struct ccu_data *ccu,
Alex Elder03548ec2014-04-21 16:11:41 -0500800 struct device_node *node)
Alex Elder1f27f152014-02-14 12:29:18 -0600801{
Alex Elder1f27f152014-02-14 12:29:18 -0600802 struct resource res = { 0 };
803 resource_size_t range;
Alex Elder03548ec2014-04-21 16:11:41 -0500804 unsigned int i;
Alex Elder1f27f152014-02-14 12:29:18 -0600805 int ret;
806
Alex Elderb12151c2014-04-21 16:11:40 -0500807 if (ccu->clk_data.clk_num) {
808 size_t size;
Alex Elder1f27f152014-02-14 12:29:18 -0600809
Alex Elderb12151c2014-04-21 16:11:40 -0500810 size = ccu->clk_data.clk_num * sizeof(*ccu->clk_data.clks);
811 ccu->clk_data.clks = kzalloc(size, GFP_KERNEL);
812 if (!ccu->clk_data.clks) {
813 pr_err("%s: unable to allocate %u clocks for %s\n",
814 __func__, ccu->clk_data.clk_num, node->name);
815 return;
816 }
Alex Elder1f27f152014-02-14 12:29:18 -0600817 }
818
819 ret = of_address_to_resource(node, 0, &res);
820 if (ret) {
821 pr_err("%s: no valid CCU registers found for %s\n", __func__,
822 node->name);
823 goto out_err;
824 }
825
826 range = resource_size(&res);
827 if (range > (resource_size_t)U32_MAX) {
828 pr_err("%s: address range too large for %s\n", __func__,
829 node->name);
830 goto out_err;
831 }
832
833 ccu->range = (u32)range;
Alex Eldera597fac2014-04-21 16:11:42 -0500834
835 if (!ccu_data_valid(ccu)) {
836 pr_err("%s: ccu data not valid for %s\n", __func__, node->name);
837 goto out_err;
838 }
839
Alex Elder1f27f152014-02-14 12:29:18 -0600840 ccu->base = ioremap(res.start, ccu->range);
841 if (!ccu->base) {
842 pr_err("%s: unable to map CCU registers for %s\n", __func__,
843 node->name);
844 goto out_err;
845 }
Alex Elder1f27f152014-02-14 12:29:18 -0600846 ccu->node = of_node_get(node);
Alex Elder1f27f152014-02-14 12:29:18 -0600847
Alex Elder03548ec2014-04-21 16:11:41 -0500848 /*
849 * Set up each defined kona clock and save the result in
850 * the clock framework clock array (in ccu->data). Then
851 * register as a provider for these clocks.
852 */
853 for (i = 0; i < ccu->clk_data.clk_num; i++) {
854 if (!ccu->kona_clks[i].ccu)
855 continue;
856 ccu->clk_data.clks[i] = kona_clk_setup(&ccu->kona_clks[i]);
857 }
Alex Elder1f27f152014-02-14 12:29:18 -0600858
Alex Elderb12151c2014-04-21 16:11:40 -0500859 ret = of_clk_add_provider(node, of_clk_src_onecell_get, &ccu->clk_data);
Alex Elder1f27f152014-02-14 12:29:18 -0600860 if (ret) {
861 pr_err("%s: error adding ccu %s as provider (%d)\n", __func__,
862 node->name, ret);
863 goto out_err;
864 }
865
866 if (!kona_ccu_init(ccu))
867 pr_err("Broadcom %s initialization had errors\n", node->name);
868
869 return;
870out_err:
871 kona_ccu_teardown(ccu);
872 pr_err("Broadcom %s setup aborted\n", node->name);
873}