Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

clk: multiplier: Prevent the multiplier from under / over flowing

In the current multiplier base clock implementation, if the
CLK_SET_RATE_PARENT flag isn't set, the code will not make sure that the
multiplier computed remains within the boundaries of our clock.

This means that if the clock we want to reach is below the parent rate,
or if the multiplier is above the maximum that we can reach, we will end up
with a completely bogus one that the clock cannot achieve.

Fixes: f2e0a53271a4 ("clk: Add a basic multiplier clock")
Signed-off-by: Maxime Ripard <maxime.ripard@free-electrons.com>
Signed-off-by: Michael Turquette <mturquette@baylibre.com>
Link: lkml.kernel.org/r/1463402840-17062-3-git-send-email-maxime.ripard@free-electrons.com

authored by

Maxime Ripard and committed by
Michael Turquette
25f77a3a 071a0cb6

+17 -3
+17 -3
drivers/clk/clk-multiplier.c
··· 52 52 unsigned long *best_parent_rate, 53 53 u8 width, unsigned long flags) 54 54 { 55 + struct clk_multiplier *mult = to_clk_multiplier(hw); 55 56 unsigned long orig_parent_rate = *best_parent_rate; 56 57 unsigned long parent_rate, current_rate, best_rate = ~0; 57 58 unsigned int i, bestmult = 0; 59 + unsigned int maxmult = (1 << width) - 1; 58 60 59 - if (!(clk_hw_get_flags(hw) & CLK_SET_RATE_PARENT)) 60 - return rate / *best_parent_rate; 61 + if (!(clk_hw_get_flags(hw) & CLK_SET_RATE_PARENT)) { 62 + bestmult = rate / orig_parent_rate; 61 63 62 - for (i = 1; i < ((1 << width) - 1); i++) { 64 + /* Make sure we don't end up with a 0 multiplier */ 65 + if ((bestmult == 0) && 66 + !(mult->flags & CLK_MULTIPLIER_ZERO_BYPASS)) 67 + bestmult = 1; 68 + 69 + /* Make sure we don't overflow the multiplier */ 70 + if (bestmult > maxmult) 71 + bestmult = maxmult; 72 + 73 + return bestmult; 74 + } 75 + 76 + for (i = 1; i < maxmult; i++) { 63 77 if (rate == orig_parent_rate * i) { 64 78 /* 65 79 * This is the best case for us if we have a