Re: [PATCH v7 05/11] clk: rp1: Add support for clocks provided by RP1

Andrea della Porta
2025-02-20T17:19:52Z

Hi Stefan,

On 15:58 Sat 08 Feb , Stefan Wahren wrote:
> Hi Andrea,
>
> Am 07.02.25 um 22:31 schrieb Andrea della Porta:
> > RaspberryPi RP1 is an MFD providing, among other peripherals, several
> > clock generators and PLLs that drives the sub-peripherals.
> > Add the driver to support the clock providers.
> >
> > Signed-off-by: Andrea della Porta <andrea.porta@suse.com>

...

> > +
> > +#define MAX_CLK_PARENTS 16
> > +
> > +/*
> > + * Secondary PLL channel output divider table.
> > + * Divider values range from 8 to 19.
> > + * Invalid values default to 19
> Maybe it's worth to add a short define for this invalid value?

Ack.

> > + */
> > +static const struct clk_div_table pll_sec_div_table[] = {
> > + { 0x00, 19 },
> > + { 0x01, 19 },
> > + { 0x02, 19 },
> > +

...

> > + regmap_read(clockman->regmap, reg, &val);
> > +
> > + return val;
> > +}
> > +
> > +static int rp1_pll_core_is_on(struct clk_hw *hw)
> > +{
> > + struct rp1_clk_desc *pll_core = container_of(hw, struct rp1_clk_desc, hw);
> > + struct rp1_clockman *clockman = pll_core->clockman;
> > + const struct rp1_pll_core_data *data = pll_core->data;
> > +
> Please drop this empty line

Ack.

> > + u32 pwr = clockman_read(clockman, data->pwr_reg);
> > +
> > + return (pwr & PLL_PWR_PD) || (pwr & PLL_PWR_POSTDIVPD);
> > +}
> > +
> > +static int rp1_pll_core_on(struct clk_hw *hw)
> > +{
> > + struct rp1_clk_desc *pll_core = container_of(hw, struct rp1_clk_desc, hw);
> > + struct rp1_clockman *clockman = pll_core->clockman;
> > + const struct rp1_pll_core_data *data = pll_core->data;
> > +
> ditto

Ack.

> > + u32 fbdiv_frac, val;
> > + int ret;
> > +
> > + spin_lock(&clockman->regs_lock);

...

> > +static int rp1_pll_ph_on(struct clk_hw *hw)
> > +{
> > + struct rp1_clk_desc *pll_ph = container_of(hw, struct rp1_clk_desc, hw);
> > + struct rp1_clockman *clockman = pll_ph->clockman;
> > + const struct rp1_pll_ph_data *data = pll_ph->data;
> > + u32 ph_reg;
> > +
> > + /* TODO: ensure pri/sec is enabled! */
> Please extend this TODO. Primary/secondary of what

I think the orginal comment is misleading. It seems to be related
to the fact that phase shifted clocks should have their parent enabled
before setting them up. Pri here shuold be the only phased clock, while
Sec is not and depends directly on a core clock, so I'll change that
comment entirely.

> > + spin_lock(&clockman->regs_lock);
> > + ph_reg = clockman_read(clockman, data->ph_reg);
> > + ph_reg |= data->phase << PLL_PH_PHASE_SHIFT;
> > + ph_reg |= PLL_PH_EN;
> > + clockman_write(clockman, data->ph_reg, ph_reg);
> > + spin_unlock(&clockman->regs_lock);
> > +
> > + return 0;
> > +}

...

> > +static unsigned long rp1_clock_recalc_rate(struct clk_hw *hw,
> > + unsigned long parent_rate)
> > +{
> > + struct rp1_clk_desc *clock = container_of(hw, struct rp1_clk_desc, hw);
> > + struct rp1_clockman *clockman = clock->clockman;
> > + const struct rp1_clock_data *data = clock->data;
> > + u64 calc_rate;
> > + u64 div;
> > +
> Please drop empty line

Ack.

> > + u32 frac;
> > +
> > + div = clockman_read(clockman, data->div_int_reg);
> > + frac = (data->div_frac_reg != 0) ?
> > + clockman_read(clockman, data->div_frac_reg) : 0;
> > +
> > + /* If the integer portion of the divider is 0, treat it as 2^16 */
> > + if (!div)
> > + div = 1 << 16;
> > +
> > + div = (div << CLK_DIV_FRAC_BITS) | (frac >> (32 - CLK_DIV_FRAC_BITS));
> > +
> > + calc_rate = (u64)parent_rate << CLK_DIV_FRAC_BITS;
> > + calc_rate = div64_u64(calc_rate, div);
> > +
> > + return calc_rate;
> > +}

...

> > + ctrl |= (AUX_SEL << CLK_CTRL_SRC_SHIFT) & data->clk_src_mask;
> > + } else {
> > + ctrl &= ~data->clk_src_mask;
> > + ctrl |= (index << CLK_CTRL_SRC_SHIFT) & data->clk_src_mask;
> > + }
> > +
> > + clockman_write(clockman, data->ctrl_reg, ctrl);
> > + spin_unlock(&clockman->regs_lock);
> > +
> > + sel = rp1_clock_get_parent(hw);
> > + WARN(sel != index, "(%s): Parent index req %u returned back %u\n",
> > + clk_hw_get_name(hw), index, sel);
> I don't think such an important clock callback should emit WARN(),
> because this might cause a message flood.
>
> So i think either a WARN_ONCE() or dev_warn_once() might be better.

Ack.

> > +
> > + return 0;
> > +}
> > +
> > +static int rp1_clock_set_rate_and_parent(struct clk_hw *hw,
> > + unsigned long rate,
> > + unsigned long parent_rate,
> > + u8 parent)
> > +{
> > + struct rp1_clk_desc *clock = container_of(hw, struct rp1_clk_desc, hw);
> > + struct rp1_clockman *clockman = clock->clockman;
> > + const struct rp1_clock_data *data = clock->data;
> > + u32 div = rp1_clock_choose_div(rate, parent_rate, data);
> > +
> > + WARN(rate > 4000000000ll, "rate is -ve (%d)\n", (int)rate);
> This looks suspicious. Is this is a limit? Except of this, casting to
> int is wrong.

I think that's not an hard limit, the original intent was probably
to filter some clock rates resulting from functions that can return
a (negative) error code.
Since clock->data->max_freq contains the maximum frequency achievable,
I'll turn that WARN into a proper one that check for that limit.

>
> In case this is not possible please make it a WARN_ONCE() or dev_warn_once()
> > +
> > + if (WARN(!div,
> > + "clk divider calculated as 0! (%s, rate %ld, parent rate %ld)\n",
> > + clk_hw_get_name(hw), rate, parent_rate))
> > + div = 1 << CLK_DIV_FRAC_BITS;
> Same here

Ack.

Many thanks,
Andrea

> > +
> > + spin_lock(&clockman->regs_lock);
> > +
> > + clockman_write(clockman, data->div_int_reg, div >> CLK_DIV_FRAC_BITS);
> > + if (data->div_frac_reg)
> > + clockman_write(clockman, data->div_frac_reg, div << (32 - CLK_DIV_FRAC_BITS));
> > +
> > + spin_unlock(&clockman->regs_lock);
> > +
> > + if (parent != 0xff)
> > + rp1_clock_set_parent(hw, parent);
> > +
> > + return 0;
> > +}
> > +
> >