blob: 7d90c34d13369bc034cc2fee9112908f32f092fe [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 "clk-kona.h"
16
17#include <linux/delay.h>
18
Alex Eldera597fac2014-04-21 16:11:42 -050019/*
20 * "Policies" affect the frequencies of bus clocks provided by a
21 * CCU. (I believe these polices are named "Deep Sleep", "Economy",
22 * "Normal", and "Turbo".) A lower policy number has lower power
23 * consumption, and policy 2 is the default.
24 */
25#define CCU_POLICY_COUNT 4
26
Alex Elder1f27f152014-02-14 12:29:18 -060027#define CCU_ACCESS_PASSWORD 0xA5A500
28#define CLK_GATE_DELAY_LOOP 2000
29
30/* Bitfield operations */
31
32/* Produces a mask of set bits covering a range of a 32-bit value */
33static inline u32 bitfield_mask(u32 shift, u32 width)
34{
35 return ((1 << width) - 1) << shift;
36}
37
38/* Extract the value of a bitfield found within a given register value */
39static inline u32 bitfield_extract(u32 reg_val, u32 shift, u32 width)
40{
41 return (reg_val & bitfield_mask(shift, width)) >> shift;
42}
43
44/* Replace the value of a bitfield found within a given register value */
45static inline u32 bitfield_replace(u32 reg_val, u32 shift, u32 width, u32 val)
46{
47 u32 mask = bitfield_mask(shift, width);
48
49 return (reg_val & ~mask) | (val << shift);
50}
51
52/* Divider and scaling helpers */
53
54/*
55 * Implement DIV_ROUND_CLOSEST() for 64-bit dividend and both values
56 * unsigned. Note that unlike do_div(), the remainder is discarded
57 * and the return value is the quotient (not the remainder).
58 */
59u64 do_div_round_closest(u64 dividend, unsigned long divisor)
60{
61 u64 result;
62
63 result = dividend + ((u64)divisor >> 1);
64 (void)do_div(result, divisor);
65
66 return result;
67}
68
69/* Convert a divider into the scaled divisor value it represents. */
70static inline u64 scaled_div_value(struct bcm_clk_div *div, u32 reg_div)
71{
Alex Eldere813d492014-04-07 08:22:12 -050072 return (u64)reg_div + ((u64)1 << div->u.s.frac_width);
Alex Elder1f27f152014-02-14 12:29:18 -060073}
74
75/*
76 * Build a scaled divider value as close as possible to the
77 * given whole part (div_value) and fractional part (expressed
78 * in billionths).
79 */
80u64 scaled_div_build(struct bcm_clk_div *div, u32 div_value, u32 billionths)
81{
82 u64 combined;
83
84 BUG_ON(!div_value);
85 BUG_ON(billionths >= BILLION);
86
87 combined = (u64)div_value * BILLION + billionths;
Alex Eldere813d492014-04-07 08:22:12 -050088 combined <<= div->u.s.frac_width;
Alex Elder1f27f152014-02-14 12:29:18 -060089
90 return do_div_round_closest(combined, BILLION);
91}
92
93/* The scaled minimum divisor representable by a divider */
94static inline u64
95scaled_div_min(struct bcm_clk_div *div)
96{
97 if (divider_is_fixed(div))
Alex Eldere813d492014-04-07 08:22:12 -050098 return (u64)div->u.fixed;
Alex Elder1f27f152014-02-14 12:29:18 -060099
100 return scaled_div_value(div, 0);
101}
102
103/* The scaled maximum divisor representable by a divider */
104u64 scaled_div_max(struct bcm_clk_div *div)
105{
106 u32 reg_div;
107
108 if (divider_is_fixed(div))
Alex Eldere813d492014-04-07 08:22:12 -0500109 return (u64)div->u.fixed;
Alex Elder1f27f152014-02-14 12:29:18 -0600110
Alex Eldere813d492014-04-07 08:22:12 -0500111 reg_div = ((u32)1 << div->u.s.width) - 1;
Alex Elder1f27f152014-02-14 12:29:18 -0600112
113 return scaled_div_value(div, reg_div);
114}
115
116/*
117 * Convert a scaled divisor into its divider representation as
118 * stored in a divider register field.
119 */
120static inline u32
121divider(struct bcm_clk_div *div, u64 scaled_div)
122{
123 BUG_ON(scaled_div < scaled_div_min(div));
124 BUG_ON(scaled_div > scaled_div_max(div));
125
Alex Eldere813d492014-04-07 08:22:12 -0500126 return (u32)(scaled_div - ((u64)1 << div->u.s.frac_width));
Alex Elder1f27f152014-02-14 12:29:18 -0600127}
128
129/* Return a rate scaled for use when dividing by a scaled divisor. */
130static inline u64
131scale_rate(struct bcm_clk_div *div, u32 rate)
132{
133 if (divider_is_fixed(div))
134 return (u64)rate;
135
Alex Eldere813d492014-04-07 08:22:12 -0500136 return (u64)rate << div->u.s.frac_width;
Alex Elder1f27f152014-02-14 12:29:18 -0600137}
138
139/* CCU access */
140
141/* Read a 32-bit register value from a CCU's address space. */
142static inline u32 __ccu_read(struct ccu_data *ccu, u32 reg_offset)
143{
144 return readl(ccu->base + reg_offset);
145}
146
147/* Write a 32-bit register value into a CCU's address space. */
148static inline void
149__ccu_write(struct ccu_data *ccu, u32 reg_offset, u32 reg_val)
150{
151 writel(reg_val, ccu->base + reg_offset);
152}
153
154static inline unsigned long ccu_lock(struct ccu_data *ccu)
155{
156 unsigned long flags;
157
158 spin_lock_irqsave(&ccu->lock, flags);
159
160 return flags;
161}
162static inline void ccu_unlock(struct ccu_data *ccu, unsigned long flags)
163{
164 spin_unlock_irqrestore(&ccu->lock, flags);
165}
166
167/*
168 * Enable/disable write access to CCU protected registers. The
169 * WR_ACCESS register for all CCUs is at offset 0.
170 */
171static inline void __ccu_write_enable(struct ccu_data *ccu)
172{
173 if (ccu->write_enabled) {
174 pr_err("%s: access already enabled for %s\n", __func__,
175 ccu->name);
176 return;
177 }
178 ccu->write_enabled = true;
179 __ccu_write(ccu, 0, CCU_ACCESS_PASSWORD | 1);
180}
181
182static inline void __ccu_write_disable(struct ccu_data *ccu)
183{
184 if (!ccu->write_enabled) {
185 pr_err("%s: access wasn't enabled for %s\n", __func__,
186 ccu->name);
187 return;
188 }
189
190 __ccu_write(ccu, 0, CCU_ACCESS_PASSWORD);
191 ccu->write_enabled = false;
192}
193
194/*
195 * Poll a register in a CCU's address space, returning when the
196 * specified bit in that register's value is set (or clear). Delay
197 * a microsecond after each read of the register. Returns true if
198 * successful, or false if we gave up trying.
199 *
200 * Caller must ensure the CCU lock is held.
201 */
202static inline bool
203__ccu_wait_bit(struct ccu_data *ccu, u32 reg_offset, u32 bit, bool want)
204{
205 unsigned int tries;
206 u32 bit_mask = 1 << bit;
207
208 for (tries = 0; tries < CLK_GATE_DELAY_LOOP; tries++) {
209 u32 val;
210 bool bit_val;
211
212 val = __ccu_read(ccu, reg_offset);
213 bit_val = (val & bit_mask) != 0;
214 if (bit_val == want)
215 return true;
216 udelay(1);
217 }
Alex Elder4bac65c2014-04-21 16:11:37 -0500218 pr_warn("%s: %s/0x%04x bit %u was never %s\n", __func__,
219 ccu->name, reg_offset, bit, want ? "set" : "clear");
220
Alex Elder1f27f152014-02-14 12:29:18 -0600221 return false;
222}
223
Alex Eldera597fac2014-04-21 16:11:42 -0500224/* Policy operations */
225
226static bool __ccu_policy_engine_start(struct ccu_data *ccu, bool sync)
227{
228 struct bcm_policy_ctl *control = &ccu->policy.control;
229 u32 offset;
230 u32 go_bit;
231 u32 mask;
232 bool ret;
233
234 /* If we don't need to control policy for this CCU, we're done. */
235 if (!policy_ctl_exists(control))
236 return true;
237
238 offset = control->offset;
239 go_bit = control->go_bit;
240
241 /* Ensure we're not busy before we start */
242 ret = __ccu_wait_bit(ccu, offset, go_bit, false);
243 if (!ret) {
244 pr_err("%s: ccu %s policy engine wouldn't go idle\n",
245 __func__, ccu->name);
246 return false;
247 }
248
249 /*
250 * If it's a synchronous request, we'll wait for the voltage
251 * and frequency of the active load to stabilize before
252 * returning. To do this we select the active load by
253 * setting the ATL bit.
254 *
255 * An asynchronous request instead ramps the voltage in the
256 * background, and when that process stabilizes, the target
257 * load is copied to the active load and the CCU frequency
258 * is switched. We do this by selecting the target load
259 * (ATL bit clear) and setting the request auto-copy (AC bit
260 * set).
261 *
262 * Note, we do NOT read-modify-write this register.
263 */
264 mask = (u32)1 << go_bit;
265 if (sync)
266 mask |= 1 << control->atl_bit;
267 else
268 mask |= 1 << control->ac_bit;
269 __ccu_write(ccu, offset, mask);
270
271 /* Wait for indication that operation is complete. */
272 ret = __ccu_wait_bit(ccu, offset, go_bit, false);
273 if (!ret)
274 pr_err("%s: ccu %s policy engine never started\n",
275 __func__, ccu->name);
276
277 return ret;
278}
279
280static bool __ccu_policy_engine_stop(struct ccu_data *ccu)
281{
282 struct bcm_lvm_en *enable = &ccu->policy.enable;
283 u32 offset;
284 u32 enable_bit;
285 bool ret;
286
287 /* If we don't need to control policy for this CCU, we're done. */
288 if (!policy_lvm_en_exists(enable))
289 return true;
290
291 /* Ensure we're not busy before we start */
292 offset = enable->offset;
293 enable_bit = enable->bit;
294 ret = __ccu_wait_bit(ccu, offset, enable_bit, false);
295 if (!ret) {
296 pr_err("%s: ccu %s policy engine already stopped\n",
297 __func__, ccu->name);
298 return false;
299 }
300
301 /* Now set the bit to stop the engine (NO read-modify-write) */
302 __ccu_write(ccu, offset, (u32)1 << enable_bit);
303
304 /* Wait for indication that it has stopped. */
305 ret = __ccu_wait_bit(ccu, offset, enable_bit, false);
306 if (!ret)
307 pr_err("%s: ccu %s policy engine never stopped\n",
308 __func__, ccu->name);
309
310 return ret;
311}
312
313/*
314 * A CCU has four operating conditions ("policies"), and some clocks
315 * can be disabled or enabled based on which policy is currently in
316 * effect. Such clocks have a bit in a "policy mask" register for
317 * each policy indicating whether the clock is enabled for that
318 * policy or not. The bit position for a clock is the same for all
319 * four registers, and the 32-bit registers are at consecutive
320 * addresses.
321 */
322static bool policy_init(struct ccu_data *ccu, struct bcm_clk_policy *policy)
323{
324 u32 offset;
325 u32 mask;
326 int i;
327 bool ret;
328
329 if (!policy_exists(policy))
330 return true;
331
332 /*
333 * We need to stop the CCU policy engine to allow update
334 * of our policy bits.
335 */
336 if (!__ccu_policy_engine_stop(ccu)) {
337 pr_err("%s: unable to stop CCU %s policy engine\n",
338 __func__, ccu->name);
339 return false;
340 }
341
342 /*
343 * For now, if a clock defines its policy bit we just mark
344 * it "enabled" for all four policies.
345 */
346 offset = policy->offset;
347 mask = (u32)1 << policy->bit;
348 for (i = 0; i < CCU_POLICY_COUNT; i++) {
349 u32 reg_val;
350
351 reg_val = __ccu_read(ccu, offset);
352 reg_val |= mask;
353 __ccu_write(ccu, offset, reg_val);
354 offset += sizeof(u32);
355 }
356
357 /* We're done updating; fire up the policy engine again. */
358 ret = __ccu_policy_engine_start(ccu, true);
359 if (!ret)
360 pr_err("%s: unable to restart CCU %s policy engine\n",
361 __func__, ccu->name);
362
363 return ret;
364}
365
Alex Elder1f27f152014-02-14 12:29:18 -0600366/* Gate operations */
367
368/* Determine whether a clock is gated. CCU lock must be held. */
369static bool
370__is_clk_gate_enabled(struct ccu_data *ccu, struct bcm_clk_gate *gate)
371{
372 u32 bit_mask;
373 u32 reg_val;
374
375 /* If there is no gate we can assume it's enabled. */
376 if (!gate_exists(gate))
377 return true;
378
379 bit_mask = 1 << gate->status_bit;
380 reg_val = __ccu_read(ccu, gate->offset);
381
382 return (reg_val & bit_mask) != 0;
383}
384
385/* Determine whether a clock is gated. */
386static bool
387is_clk_gate_enabled(struct ccu_data *ccu, struct bcm_clk_gate *gate)
388{
389 long flags;
390 bool ret;
391
392 /* Avoid taking the lock if we can */
393 if (!gate_exists(gate))
394 return true;
395
396 flags = ccu_lock(ccu);
397 ret = __is_clk_gate_enabled(ccu, gate);
398 ccu_unlock(ccu, flags);
399
400 return ret;
401}
402
403/*
404 * Commit our desired gate state to the hardware.
405 * Returns true if successful, false otherwise.
406 */
407static bool
408__gate_commit(struct ccu_data *ccu, struct bcm_clk_gate *gate)
409{
410 u32 reg_val;
411 u32 mask;
412 bool enabled = false;
413
414 BUG_ON(!gate_exists(gate));
415 if (!gate_is_sw_controllable(gate))
416 return true; /* Nothing we can change */
417
418 reg_val = __ccu_read(ccu, gate->offset);
419
420 /* For a hardware/software gate, set which is in control */
421 if (gate_is_hw_controllable(gate)) {
422 mask = (u32)1 << gate->hw_sw_sel_bit;
423 if (gate_is_sw_managed(gate))
424 reg_val |= mask;
425 else
426 reg_val &= ~mask;
427 }
428
429 /*
430 * If software is in control, enable or disable the gate.
431 * If hardware is, clear the enabled bit for good measure.
432 * If a software controlled gate can't be disabled, we're
433 * required to write a 0 into the enable bit (but the gate
434 * will be enabled).
435 */
436 mask = (u32)1 << gate->en_bit;
437 if (gate_is_sw_managed(gate) && (enabled = gate_is_enabled(gate)) &&
438 !gate_is_no_disable(gate))
439 reg_val |= mask;
440 else
441 reg_val &= ~mask;
442
443 __ccu_write(ccu, gate->offset, reg_val);
444
445 /* For a hardware controlled gate, we're done */
446 if (!gate_is_sw_managed(gate))
447 return true;
448
449 /* Otherwise wait for the gate to be in desired state */
450 return __ccu_wait_bit(ccu, gate->offset, gate->status_bit, enabled);
451}
452
453/*
454 * Initialize a gate. Our desired state (hardware/software select,
455 * and if software, its enable state) is committed to hardware
456 * without the usual checks to see if it's already set up that way.
457 * Returns true if successful, false otherwise.
458 */
459static bool gate_init(struct ccu_data *ccu, struct bcm_clk_gate *gate)
460{
461 if (!gate_exists(gate))
462 return true;
463 return __gate_commit(ccu, gate);
464}
465
466/*
467 * Set a gate to enabled or disabled state. Does nothing if the
468 * gate is not currently under software control, or if it is already
469 * in the requested state. Returns true if successful, false
470 * otherwise. CCU lock must be held.
471 */
472static bool
473__clk_gate(struct ccu_data *ccu, struct bcm_clk_gate *gate, bool enable)
474{
475 bool ret;
476
477 if (!gate_exists(gate) || !gate_is_sw_managed(gate))
478 return true; /* Nothing to do */
479
480 if (!enable && gate_is_no_disable(gate)) {
481 pr_warn("%s: invalid gate disable request (ignoring)\n",
482 __func__);
483 return true;
484 }
485
486 if (enable == gate_is_enabled(gate))
487 return true; /* No change */
488
489 gate_flip_enabled(gate);
490 ret = __gate_commit(ccu, gate);
491 if (!ret)
492 gate_flip_enabled(gate); /* Revert the change */
493
494 return ret;
495}
496
497/* Enable or disable a gate. Returns 0 if successful, -EIO otherwise */
498static int clk_gate(struct ccu_data *ccu, const char *name,
499 struct bcm_clk_gate *gate, bool enable)
500{
501 unsigned long flags;
502 bool success;
503
504 /*
505 * Avoid taking the lock if we can. We quietly ignore
506 * requests to change state that don't make sense.
507 */
508 if (!gate_exists(gate) || !gate_is_sw_managed(gate))
509 return 0;
510 if (!enable && gate_is_no_disable(gate))
511 return 0;
512
513 flags = ccu_lock(ccu);
514 __ccu_write_enable(ccu);
515
516 success = __clk_gate(ccu, gate, enable);
517
518 __ccu_write_disable(ccu);
519 ccu_unlock(ccu, flags);
520
521 if (success)
522 return 0;
523
524 pr_err("%s: failed to %s gate for %s\n", __func__,
525 enable ? "enable" : "disable", name);
526
527 return -EIO;
528}
529
530/* Trigger operations */
531
532/*
533 * Caller must ensure CCU lock is held and access is enabled.
534 * Returns true if successful, false otherwise.
535 */
536static bool __clk_trigger(struct ccu_data *ccu, struct bcm_clk_trig *trig)
537{
538 /* Trigger the clock and wait for it to finish */
539 __ccu_write(ccu, trig->offset, 1 << trig->bit);
540
541 return __ccu_wait_bit(ccu, trig->offset, trig->bit, false);
542}
543
544/* Divider operations */
545
546/* Read a divider value and return the scaled divisor it represents. */
547static u64 divider_read_scaled(struct ccu_data *ccu, struct bcm_clk_div *div)
548{
549 unsigned long flags;
550 u32 reg_val;
551 u32 reg_div;
552
553 if (divider_is_fixed(div))
Alex Eldere813d492014-04-07 08:22:12 -0500554 return (u64)div->u.fixed;
Alex Elder1f27f152014-02-14 12:29:18 -0600555
556 flags = ccu_lock(ccu);
Alex Eldere813d492014-04-07 08:22:12 -0500557 reg_val = __ccu_read(ccu, div->u.s.offset);
Alex Elder1f27f152014-02-14 12:29:18 -0600558 ccu_unlock(ccu, flags);
559
560 /* Extract the full divider field from the register value */
Alex Eldere813d492014-04-07 08:22:12 -0500561 reg_div = bitfield_extract(reg_val, div->u.s.shift, div->u.s.width);
Alex Elder1f27f152014-02-14 12:29:18 -0600562
563 /* Return the scaled divisor value it represents */
564 return scaled_div_value(div, reg_div);
565}
566
567/*
568 * Convert a divider's scaled divisor value into its recorded form
569 * and commit it into the hardware divider register.
570 *
571 * Returns 0 on success. Returns -EINVAL for invalid arguments.
572 * Returns -ENXIO if gating failed, and -EIO if a trigger failed.
573 */
574static int __div_commit(struct ccu_data *ccu, struct bcm_clk_gate *gate,
575 struct bcm_clk_div *div, struct bcm_clk_trig *trig)
576{
577 bool enabled;
578 u32 reg_div;
579 u32 reg_val;
580 int ret = 0;
581
582 BUG_ON(divider_is_fixed(div));
583
584 /*
585 * If we're just initializing the divider, and no initial
586 * state was defined in the device tree, we just find out
587 * what its current value is rather than updating it.
588 */
Alex Eldere813d492014-04-07 08:22:12 -0500589 if (div->u.s.scaled_div == BAD_SCALED_DIV_VALUE) {
590 reg_val = __ccu_read(ccu, div->u.s.offset);
591 reg_div = bitfield_extract(reg_val, div->u.s.shift,
592 div->u.s.width);
593 div->u.s.scaled_div = scaled_div_value(div, reg_div);
Alex Elder1f27f152014-02-14 12:29:18 -0600594
595 return 0;
596 }
597
598 /* Convert the scaled divisor to the value we need to record */
Alex Eldere813d492014-04-07 08:22:12 -0500599 reg_div = divider(div, div->u.s.scaled_div);
Alex Elder1f27f152014-02-14 12:29:18 -0600600
601 /* Clock needs to be enabled before changing the rate */
602 enabled = __is_clk_gate_enabled(ccu, gate);
603 if (!enabled && !__clk_gate(ccu, gate, true)) {
604 ret = -ENXIO;
605 goto out;
606 }
607
608 /* Replace the divider value and record the result */
Alex Eldere813d492014-04-07 08:22:12 -0500609 reg_val = __ccu_read(ccu, div->u.s.offset);
610 reg_val = bitfield_replace(reg_val, div->u.s.shift, div->u.s.width,
611 reg_div);
612 __ccu_write(ccu, div->u.s.offset, reg_val);
Alex Elder1f27f152014-02-14 12:29:18 -0600613
614 /* If the trigger fails we still want to disable the gate */
615 if (!__clk_trigger(ccu, trig))
616 ret = -EIO;
617
618 /* Disable the clock again if it was disabled to begin with */
619 if (!enabled && !__clk_gate(ccu, gate, false))
620 ret = ret ? ret : -ENXIO; /* return first error */
621out:
622 return ret;
623}
624
625/*
626 * Initialize a divider by committing our desired state to hardware
627 * without the usual checks to see if it's already set up that way.
628 * Returns true if successful, false otherwise.
629 */
630static bool div_init(struct ccu_data *ccu, struct bcm_clk_gate *gate,
631 struct bcm_clk_div *div, struct bcm_clk_trig *trig)
632{
633 if (!divider_exists(div) || divider_is_fixed(div))
634 return true;
635 return !__div_commit(ccu, gate, div, trig);
636}
637
638static int divider_write(struct ccu_data *ccu, struct bcm_clk_gate *gate,
639 struct bcm_clk_div *div, struct bcm_clk_trig *trig,
640 u64 scaled_div)
641{
642 unsigned long flags;
643 u64 previous;
644 int ret;
645
646 BUG_ON(divider_is_fixed(div));
647
Alex Eldere813d492014-04-07 08:22:12 -0500648 previous = div->u.s.scaled_div;
Alex Elder1f27f152014-02-14 12:29:18 -0600649 if (previous == scaled_div)
650 return 0; /* No change */
651
Alex Eldere813d492014-04-07 08:22:12 -0500652 div->u.s.scaled_div = scaled_div;
Alex Elder1f27f152014-02-14 12:29:18 -0600653
654 flags = ccu_lock(ccu);
655 __ccu_write_enable(ccu);
656
657 ret = __div_commit(ccu, gate, div, trig);
658
659 __ccu_write_disable(ccu);
660 ccu_unlock(ccu, flags);
661
662 if (ret)
Alex Eldere813d492014-04-07 08:22:12 -0500663 div->u.s.scaled_div = previous; /* Revert the change */
Alex Elder1f27f152014-02-14 12:29:18 -0600664
665 return ret;
666
667}
668
669/* Common clock rate helpers */
670
671/*
672 * Implement the common clock framework recalc_rate method, taking
673 * into account a divider and an optional pre-divider. The
674 * pre-divider register pointer may be NULL.
675 */
676static unsigned long clk_recalc_rate(struct ccu_data *ccu,
677 struct bcm_clk_div *div, struct bcm_clk_div *pre_div,
678 unsigned long parent_rate)
679{
680 u64 scaled_parent_rate;
681 u64 scaled_div;
682 u64 result;
683
684 if (!divider_exists(div))
685 return parent_rate;
686
687 if (parent_rate > (unsigned long)LONG_MAX)
688 return 0; /* actually this would be a caller bug */
689
690 /*
691 * If there is a pre-divider, divide the scaled parent rate
692 * by the pre-divider value first. In this case--to improve
693 * accuracy--scale the parent rate by *both* the pre-divider
694 * value and the divider before actually computing the
695 * result of the pre-divider.
696 *
697 * If there's only one divider, just scale the parent rate.
698 */
699 if (pre_div && divider_exists(pre_div)) {
700 u64 scaled_rate;
701
702 scaled_rate = scale_rate(pre_div, parent_rate);
703 scaled_rate = scale_rate(div, scaled_rate);
704 scaled_div = divider_read_scaled(ccu, pre_div);
705 scaled_parent_rate = do_div_round_closest(scaled_rate,
706 scaled_div);
707 } else {
708 scaled_parent_rate = scale_rate(div, parent_rate);
709 }
710
711 /*
712 * Get the scaled divisor value, and divide the scaled
713 * parent rate by that to determine this clock's resulting
714 * rate.
715 */
716 scaled_div = divider_read_scaled(ccu, div);
717 result = do_div_round_closest(scaled_parent_rate, scaled_div);
718
719 return (unsigned long)result;
720}
721
722/*
723 * Compute the output rate produced when a given parent rate is fed
724 * into two dividers. The pre-divider can be NULL, and even if it's
725 * non-null it may be nonexistent. It's also OK for the divider to
726 * be nonexistent, and in that case the pre-divider is also ignored.
727 *
728 * If scaled_div is non-null, it is used to return the scaled divisor
729 * value used by the (downstream) divider to produce that rate.
730 */
731static long round_rate(struct ccu_data *ccu, struct bcm_clk_div *div,
732 struct bcm_clk_div *pre_div,
733 unsigned long rate, unsigned long parent_rate,
734 u64 *scaled_div)
735{
736 u64 scaled_parent_rate;
737 u64 min_scaled_div;
738 u64 max_scaled_div;
739 u64 best_scaled_div;
740 u64 result;
741
742 BUG_ON(!divider_exists(div));
743 BUG_ON(!rate);
744 BUG_ON(parent_rate > (u64)LONG_MAX);
745
746 /*
747 * If there is a pre-divider, divide the scaled parent rate
748 * by the pre-divider value first. In this case--to improve
749 * accuracy--scale the parent rate by *both* the pre-divider
750 * value and the divider before actually computing the
751 * result of the pre-divider.
752 *
753 * If there's only one divider, just scale the parent rate.
754 *
755 * For simplicity we treat the pre-divider as fixed (for now).
756 */
757 if (divider_exists(pre_div)) {
758 u64 scaled_rate;
759 u64 scaled_pre_div;
760
761 scaled_rate = scale_rate(pre_div, parent_rate);
762 scaled_rate = scale_rate(div, scaled_rate);
763 scaled_pre_div = divider_read_scaled(ccu, pre_div);
764 scaled_parent_rate = do_div_round_closest(scaled_rate,
765 scaled_pre_div);
766 } else {
767 scaled_parent_rate = scale_rate(div, parent_rate);
768 }
769
770 /*
771 * Compute the best possible divider and ensure it is in
772 * range. A fixed divider can't be changed, so just report
773 * the best we can do.
774 */
775 if (!divider_is_fixed(div)) {
776 best_scaled_div = do_div_round_closest(scaled_parent_rate,
777 rate);
778 min_scaled_div = scaled_div_min(div);
779 max_scaled_div = scaled_div_max(div);
780 if (best_scaled_div > max_scaled_div)
781 best_scaled_div = max_scaled_div;
782 else if (best_scaled_div < min_scaled_div)
783 best_scaled_div = min_scaled_div;
784 } else {
785 best_scaled_div = divider_read_scaled(ccu, div);
786 }
787
788 /* OK, figure out the resulting rate */
789 result = do_div_round_closest(scaled_parent_rate, best_scaled_div);
790
791 if (scaled_div)
792 *scaled_div = best_scaled_div;
793
794 return (long)result;
795}
796
797/* Common clock parent helpers */
798
799/*
800 * For a given parent selector (register field) value, find the
801 * index into a selector's parent_sel array that contains it.
802 * Returns the index, or BAD_CLK_INDEX if it's not found.
803 */
804static u8 parent_index(struct bcm_clk_sel *sel, u8 parent_sel)
805{
806 u8 i;
807
808 BUG_ON(sel->parent_count > (u32)U8_MAX);
809 for (i = 0; i < sel->parent_count; i++)
810 if (sel->parent_sel[i] == parent_sel)
811 return i;
812 return BAD_CLK_INDEX;
813}
814
815/*
816 * Fetch the current value of the selector, and translate that into
817 * its corresponding index in the parent array we registered with
818 * the clock framework.
819 *
820 * Returns parent array index that corresponds with the value found,
821 * or BAD_CLK_INDEX if the found value is out of range.
822 */
823static u8 selector_read_index(struct ccu_data *ccu, struct bcm_clk_sel *sel)
824{
825 unsigned long flags;
826 u32 reg_val;
827 u32 parent_sel;
828 u8 index;
829
830 /* If there's no selector, there's only one parent */
831 if (!selector_exists(sel))
832 return 0;
833
834 /* Get the value in the selector register */
835 flags = ccu_lock(ccu);
836 reg_val = __ccu_read(ccu, sel->offset);
837 ccu_unlock(ccu, flags);
838
839 parent_sel = bitfield_extract(reg_val, sel->shift, sel->width);
840
841 /* Look up that selector's parent array index and return it */
842 index = parent_index(sel, parent_sel);
843 if (index == BAD_CLK_INDEX)
844 pr_err("%s: out-of-range parent selector %u (%s 0x%04x)\n",
845 __func__, parent_sel, ccu->name, sel->offset);
846
847 return index;
848}
849
850/*
851 * Commit our desired selector value to the hardware.
852 *
853 * Returns 0 on success. Returns -EINVAL for invalid arguments.
854 * Returns -ENXIO if gating failed, and -EIO if a trigger failed.
855 */
856static int
857__sel_commit(struct ccu_data *ccu, struct bcm_clk_gate *gate,
858 struct bcm_clk_sel *sel, struct bcm_clk_trig *trig)
859{
860 u32 parent_sel;
861 u32 reg_val;
862 bool enabled;
863 int ret = 0;
864
865 BUG_ON(!selector_exists(sel));
866
867 /*
868 * If we're just initializing the selector, and no initial
869 * state was defined in the device tree, we just find out
870 * what its current value is rather than updating it.
871 */
872 if (sel->clk_index == BAD_CLK_INDEX) {
873 u8 index;
874
875 reg_val = __ccu_read(ccu, sel->offset);
876 parent_sel = bitfield_extract(reg_val, sel->shift, sel->width);
877 index = parent_index(sel, parent_sel);
878 if (index == BAD_CLK_INDEX)
879 return -EINVAL;
880 sel->clk_index = index;
881
882 return 0;
883 }
884
885 BUG_ON((u32)sel->clk_index >= sel->parent_count);
886 parent_sel = sel->parent_sel[sel->clk_index];
887
888 /* Clock needs to be enabled before changing the parent */
889 enabled = __is_clk_gate_enabled(ccu, gate);
890 if (!enabled && !__clk_gate(ccu, gate, true))
891 return -ENXIO;
892
893 /* Replace the selector value and record the result */
894 reg_val = __ccu_read(ccu, sel->offset);
895 reg_val = bitfield_replace(reg_val, sel->shift, sel->width, parent_sel);
896 __ccu_write(ccu, sel->offset, reg_val);
897
898 /* If the trigger fails we still want to disable the gate */
899 if (!__clk_trigger(ccu, trig))
900 ret = -EIO;
901
902 /* Disable the clock again if it was disabled to begin with */
903 if (!enabled && !__clk_gate(ccu, gate, false))
904 ret = ret ? ret : -ENXIO; /* return first error */
905
906 return ret;
907}
908
909/*
910 * Initialize a selector by committing our desired state to hardware
911 * without the usual checks to see if it's already set up that way.
912 * Returns true if successful, false otherwise.
913 */
914static bool sel_init(struct ccu_data *ccu, struct bcm_clk_gate *gate,
915 struct bcm_clk_sel *sel, struct bcm_clk_trig *trig)
916{
917 if (!selector_exists(sel))
918 return true;
919 return !__sel_commit(ccu, gate, sel, trig);
920}
921
922/*
923 * Write a new value into a selector register to switch to a
924 * different parent clock. Returns 0 on success, or an error code
925 * (from __sel_commit()) otherwise.
926 */
927static int selector_write(struct ccu_data *ccu, struct bcm_clk_gate *gate,
928 struct bcm_clk_sel *sel, struct bcm_clk_trig *trig,
929 u8 index)
930{
931 unsigned long flags;
932 u8 previous;
933 int ret;
934
935 previous = sel->clk_index;
936 if (previous == index)
937 return 0; /* No change */
938
939 sel->clk_index = index;
940
941 flags = ccu_lock(ccu);
942 __ccu_write_enable(ccu);
943
944 ret = __sel_commit(ccu, gate, sel, trig);
945
946 __ccu_write_disable(ccu);
947 ccu_unlock(ccu, flags);
948
949 if (ret)
950 sel->clk_index = previous; /* Revert the change */
951
952 return ret;
953}
954
955/* Clock operations */
956
957static int kona_peri_clk_enable(struct clk_hw *hw)
958{
959 struct kona_clk *bcm_clk = to_kona_clk(hw);
Alex Eldere813d492014-04-07 08:22:12 -0500960 struct bcm_clk_gate *gate = &bcm_clk->u.peri->gate;
Alex Elder1f27f152014-02-14 12:29:18 -0600961
Alex Eldere7563252014-04-21 16:11:38 -0500962 return clk_gate(bcm_clk->ccu, bcm_clk->init_data.name, gate, true);
Alex Elder1f27f152014-02-14 12:29:18 -0600963}
964
965static void kona_peri_clk_disable(struct clk_hw *hw)
966{
967 struct kona_clk *bcm_clk = to_kona_clk(hw);
Alex Eldere813d492014-04-07 08:22:12 -0500968 struct bcm_clk_gate *gate = &bcm_clk->u.peri->gate;
Alex Elder1f27f152014-02-14 12:29:18 -0600969
Alex Eldere7563252014-04-21 16:11:38 -0500970 (void)clk_gate(bcm_clk->ccu, bcm_clk->init_data.name, gate, false);
Alex Elder1f27f152014-02-14 12:29:18 -0600971}
972
973static int kona_peri_clk_is_enabled(struct clk_hw *hw)
974{
975 struct kona_clk *bcm_clk = to_kona_clk(hw);
Alex Eldere813d492014-04-07 08:22:12 -0500976 struct bcm_clk_gate *gate = &bcm_clk->u.peri->gate;
Alex Elder1f27f152014-02-14 12:29:18 -0600977
978 return is_clk_gate_enabled(bcm_clk->ccu, gate) ? 1 : 0;
979}
980
981static unsigned long kona_peri_clk_recalc_rate(struct clk_hw *hw,
982 unsigned long parent_rate)
983{
984 struct kona_clk *bcm_clk = to_kona_clk(hw);
Alex Eldere813d492014-04-07 08:22:12 -0500985 struct peri_clk_data *data = bcm_clk->u.peri;
Alex Elder1f27f152014-02-14 12:29:18 -0600986
987 return clk_recalc_rate(bcm_clk->ccu, &data->div, &data->pre_div,
988 parent_rate);
989}
990
991static long kona_peri_clk_round_rate(struct clk_hw *hw, unsigned long rate,
992 unsigned long *parent_rate)
993{
994 struct kona_clk *bcm_clk = to_kona_clk(hw);
Alex Eldere813d492014-04-07 08:22:12 -0500995 struct bcm_clk_div *div = &bcm_clk->u.peri->div;
Alex Elder1f27f152014-02-14 12:29:18 -0600996
997 if (!divider_exists(div))
998 return __clk_get_rate(hw->clk);
999
1000 /* Quietly avoid a zero rate */
Alex Eldere813d492014-04-07 08:22:12 -05001001 return round_rate(bcm_clk->ccu, div, &bcm_clk->u.peri->pre_div,
Alex Elder1f27f152014-02-14 12:29:18 -06001002 rate ? rate : 1, *parent_rate, NULL);
1003}
1004
1005static int kona_peri_clk_set_parent(struct clk_hw *hw, u8 index)
1006{
1007 struct kona_clk *bcm_clk = to_kona_clk(hw);
Alex Eldere813d492014-04-07 08:22:12 -05001008 struct peri_clk_data *data = bcm_clk->u.peri;
Alex Elder1f27f152014-02-14 12:29:18 -06001009 struct bcm_clk_sel *sel = &data->sel;
1010 struct bcm_clk_trig *trig;
1011 int ret;
1012
1013 BUG_ON(index >= sel->parent_count);
1014
1015 /* If there's only one parent we don't require a selector */
1016 if (!selector_exists(sel))
1017 return 0;
1018
1019 /*
1020 * The regular trigger is used by default, but if there's a
1021 * pre-trigger we want to use that instead.
1022 */
1023 trig = trigger_exists(&data->pre_trig) ? &data->pre_trig
1024 : &data->trig;
1025
1026 ret = selector_write(bcm_clk->ccu, &data->gate, sel, trig, index);
1027 if (ret == -ENXIO) {
Alex Eldere7563252014-04-21 16:11:38 -05001028 pr_err("%s: gating failure for %s\n", __func__,
1029 bcm_clk->init_data.name);
Alex Elder1f27f152014-02-14 12:29:18 -06001030 ret = -EIO; /* Don't proliferate weird errors */
1031 } else if (ret == -EIO) {
1032 pr_err("%s: %strigger failed for %s\n", __func__,
1033 trig == &data->pre_trig ? "pre-" : "",
Alex Eldere7563252014-04-21 16:11:38 -05001034 bcm_clk->init_data.name);
Alex Elder1f27f152014-02-14 12:29:18 -06001035 }
1036
1037 return ret;
1038}
1039
1040static u8 kona_peri_clk_get_parent(struct clk_hw *hw)
1041{
1042 struct kona_clk *bcm_clk = to_kona_clk(hw);
Alex Eldere813d492014-04-07 08:22:12 -05001043 struct peri_clk_data *data = bcm_clk->u.peri;
Alex Elder1f27f152014-02-14 12:29:18 -06001044 u8 index;
1045
1046 index = selector_read_index(bcm_clk->ccu, &data->sel);
1047
1048 /* Not all callers would handle an out-of-range value gracefully */
1049 return index == BAD_CLK_INDEX ? 0 : index;
1050}
1051
1052static int kona_peri_clk_set_rate(struct clk_hw *hw, unsigned long rate,
1053 unsigned long parent_rate)
1054{
1055 struct kona_clk *bcm_clk = to_kona_clk(hw);
Alex Eldere813d492014-04-07 08:22:12 -05001056 struct peri_clk_data *data = bcm_clk->u.peri;
Alex Elder1f27f152014-02-14 12:29:18 -06001057 struct bcm_clk_div *div = &data->div;
1058 u64 scaled_div = 0;
1059 int ret;
1060
1061 if (parent_rate > (unsigned long)LONG_MAX)
1062 return -EINVAL;
1063
1064 if (rate == __clk_get_rate(hw->clk))
1065 return 0;
1066
1067 if (!divider_exists(div))
1068 return rate == parent_rate ? 0 : -EINVAL;
1069
1070 /*
1071 * A fixed divider can't be changed. (Nor can a fixed
1072 * pre-divider be, but for now we never actually try to
1073 * change that.) Tolerate a request for a no-op change.
1074 */
1075 if (divider_is_fixed(&data->div))
1076 return rate == parent_rate ? 0 : -EINVAL;
1077
1078 /*
1079 * Get the scaled divisor value needed to achieve a clock
1080 * rate as close as possible to what was requested, given
1081 * the parent clock rate supplied.
1082 */
1083 (void)round_rate(bcm_clk->ccu, div, &data->pre_div,
1084 rate ? rate : 1, parent_rate, &scaled_div);
1085
1086 /*
1087 * We aren't updating any pre-divider at this point, so
1088 * we'll use the regular trigger.
1089 */
1090 ret = divider_write(bcm_clk->ccu, &data->gate, &data->div,
1091 &data->trig, scaled_div);
1092 if (ret == -ENXIO) {
Alex Eldere7563252014-04-21 16:11:38 -05001093 pr_err("%s: gating failure for %s\n", __func__,
1094 bcm_clk->init_data.name);
Alex Elder1f27f152014-02-14 12:29:18 -06001095 ret = -EIO; /* Don't proliferate weird errors */
1096 } else if (ret == -EIO) {
Alex Eldere7563252014-04-21 16:11:38 -05001097 pr_err("%s: trigger failed for %s\n", __func__,
1098 bcm_clk->init_data.name);
Alex Elder1f27f152014-02-14 12:29:18 -06001099 }
1100
1101 return ret;
1102}
1103
1104struct clk_ops kona_peri_clk_ops = {
1105 .enable = kona_peri_clk_enable,
1106 .disable = kona_peri_clk_disable,
1107 .is_enabled = kona_peri_clk_is_enabled,
1108 .recalc_rate = kona_peri_clk_recalc_rate,
1109 .round_rate = kona_peri_clk_round_rate,
1110 .set_parent = kona_peri_clk_set_parent,
1111 .get_parent = kona_peri_clk_get_parent,
1112 .set_rate = kona_peri_clk_set_rate,
1113};
1114
1115/* Put a peripheral clock into its initial state */
1116static bool __peri_clk_init(struct kona_clk *bcm_clk)
1117{
1118 struct ccu_data *ccu = bcm_clk->ccu;
Alex Eldere813d492014-04-07 08:22:12 -05001119 struct peri_clk_data *peri = bcm_clk->u.peri;
Alex Eldere7563252014-04-21 16:11:38 -05001120 const char *name = bcm_clk->init_data.name;
Alex Elder1f27f152014-02-14 12:29:18 -06001121 struct bcm_clk_trig *trig;
1122
1123 BUG_ON(bcm_clk->type != bcm_clk_peri);
1124
Alex Eldera597fac2014-04-21 16:11:42 -05001125 if (!policy_init(ccu, &peri->policy)) {
1126 pr_err("%s: error initializing policy for %s\n",
1127 __func__, name);
1128 return false;
1129 }
Alex Elder1f27f152014-02-14 12:29:18 -06001130 if (!gate_init(ccu, &peri->gate)) {
1131 pr_err("%s: error initializing gate for %s\n", __func__, name);
1132 return false;
1133 }
1134 if (!div_init(ccu, &peri->gate, &peri->div, &peri->trig)) {
1135 pr_err("%s: error initializing divider for %s\n", __func__,
1136 name);
1137 return false;
1138 }
1139
1140 /*
1141 * For the pre-divider and selector, the pre-trigger is used
1142 * if it's present, otherwise we just use the regular trigger.
1143 */
1144 trig = trigger_exists(&peri->pre_trig) ? &peri->pre_trig
1145 : &peri->trig;
1146
1147 if (!div_init(ccu, &peri->gate, &peri->pre_div, trig)) {
1148 pr_err("%s: error initializing pre-divider for %s\n", __func__,
1149 name);
1150 return false;
1151 }
1152
1153 if (!sel_init(ccu, &peri->gate, &peri->sel, trig)) {
1154 pr_err("%s: error initializing selector for %s\n", __func__,
1155 name);
1156 return false;
1157 }
1158
1159 return true;
1160}
1161
1162static bool __kona_clk_init(struct kona_clk *bcm_clk)
1163{
1164 switch (bcm_clk->type) {
1165 case bcm_clk_peri:
1166 return __peri_clk_init(bcm_clk);
1167 default:
1168 BUG();
1169 }
1170 return -EINVAL;
1171}
1172
1173/* Set a CCU and all its clocks into their desired initial state */
1174bool __init kona_ccu_init(struct ccu_data *ccu)
1175{
1176 unsigned long flags;
1177 unsigned int which;
Alex Elderb12151c2014-04-21 16:11:40 -05001178 struct clk **clks = ccu->clk_data.clks;
Alex Elder1f27f152014-02-14 12:29:18 -06001179 bool success = true;
1180
1181 flags = ccu_lock(ccu);
1182 __ccu_write_enable(ccu);
1183
Alex Elderb12151c2014-04-21 16:11:40 -05001184 for (which = 0; which < ccu->clk_data.clk_num; which++) {
Alex Elder1f27f152014-02-14 12:29:18 -06001185 struct kona_clk *bcm_clk;
1186
1187 if (!clks[which])
1188 continue;
1189 bcm_clk = to_kona_clk(__clk_get_hw(clks[which]));
1190 success &= __kona_clk_init(bcm_clk);
1191 }
1192
1193 __ccu_write_disable(ccu);
1194 ccu_unlock(ccu, flags);
1195 return success;
1196}