mbox series

[v4,0/9,v3,0/9] mips: ralink: add complete clock and reset driver for mtmips SoCs

Message ID 20230617052435.359177-1-sergio.paracuellos@gmail.com
Headers show
Series mips: ralink: add complete clock and reset driver for mtmips SoCs | expand

Message

Sergio Paracuellos June 17, 2023, 5:24 a.m. UTC
Hi all!

This patchset is a big effort to properly implement a clock and reset
driver for old ralink SoCs. This allow to properly define clocks in 
device tree and avoid to use fixed-clocks directly from 'arch/mips/ralink'
architecture directory code.

Device tree 'sysc' node will be both clock and reset provider using 
'clock-cells' and 'reset-cells' properties.

The ralink SoCs we are taking about are RT2880, RT3050, RT3052, RT3350,
RT3352, RT3883, RT5350, MT7620, MT7628 and MT7688. Mostly the code in
this new driver has been extracted from 'arch/mips/ralink' and cleanly
put using kernel clock and reset driver APIs. The clock plans for this
SoCs only talks about relation between CPU frequency and BUS frequency.
This relation is different depending on the particular SoC. CPU clock is
derived from XTAL frequencies.

 Depending on the SoC we have the following frequencies:
 * RT2880 SoC:
     - XTAL: 40 MHz.
     - CPU: 250, 266, 280 or 300 MHz.
     - BUS: CPU / 2 MHz.
  * RT3050, RT3052, RT3350:
     - XTAL: 40 MHz.
     - CPU: 320 or 384 MHz.
     - BUS: CPU / 3 MHz.
  * RT3352:
     - XTAL: 40 MHz.
     - CPU: 384 or 400 MHz.
     - BUS: CPU / 3 MHz.
     - PERIPH: 40 MHz.
  * RT3383:
     - XTAL: 40 MHz.
     - CPU: 250, 384, 480 or 500 MHz.
     - BUS: Depends on RAM Type and CPU:
       + RAM DDR2: 125. ELSE 83 MHz.
       + RAM DDR2: 128. ELSE 96 MHz.
       + RAM DDR2: 160. ELSE 120 MHz.
       + RAM DDR2: 166. ELSE 125 MHz.
  * RT5350:
      - XTAL: 40 MHz.
      - CPU: 300, 320 or 360 MHz.
      - BUS: CPU / 3, CPU / 4, CPU / 3 MHz.
      - PERIPH: 40 MHz.
  * MT7628 and MT7688:
     - XTAL: 20 MHz or 40 MHz.
     - CPU: 575 or 580 MHz.
     - BUS: CPU / 3.
     - PCMI2S: 480 MHz.
     - PERIPH: 40 MHz.
  * MT7620:
     - XTAL: 20 MHz or 40 MHz.
     - PLL: XTAL, 480, 600 MHz.
     - CPU: depends on PLL and some mult and dividers.
     - BUS: depends on PLL and some mult and dividers.
     - PERIPH: 40 or XTAL MHz.

MT7620 is a bit more complex deriving CPU clock from a PLL and an bunch of
register reads and predividers. To derive CPU and BUS frequencies in the
MT7620 SoC 'mt7620_calc_rate()' helper is used.
In the case XTAL can have different frequencies and we need a different
clock frequency for peripherals 'periph' clock in introduced.
The rest of the peripherals present in the SoC just follow their parent
frequencies.

I am using 'mtmips' inside for ralink clock driver. This is aligned with
pinctrl series recently merged through pinctrl git tree [0].

I am maintaining ralink as prefix for compatible strings after discussions
between Rob and Arinc in v2 of this series [1].

Changes have been compile tested for:
- RT2880
- RT3883
- MT7620

Changes have been properly tested in RT5350 SoC based board (ALL5003 board)
resulting in a working platform.

Dts files for these SoCs in-tree except MT7621 are incomplete. We are
planning to align with openWRT files at some point and add extra needed
changes. Hence I am not touching them at all in these series. If this is
a problem, please let me know and I will update them.

Talking about merging this series I'd like all of the patches going through
the MIPS tree if possible.

Thanks in advance for your time.

Best regards,
    Sergio Paracuellos

Changes in v4:
- Collect Reviewed-by and Acked-by tags.
- Pass proper data for 'ralink,rt3352-sysc' and 'ralink,rt3883-sysc' compatible string.

Changes in v3:
- Address Stephen comments in v2:
    + Drop unsused include '<linux/clk.h>'.
    + Add fixed and factor clocks when it makes sense.
    + Make 'mtmips_periph_clk_ops' named variable.
    + WARN_ON -> WARN_ON_ONCE.
    + Avoid CONFIG_USB dependent code. Introduce new 'mtmips_clk_regs_init'.
    + Don't validate the bindings in the driver.
    + Make const 'struct clk_init_data' used inside macros.
    + do_div -> div_u64.
    + Make use of dev_err_probe.

Changes in v2:
- Address bindings documentation changes pointed out by Krzysztof:
    + Rename the file into 'mediatek,mtmips-sysc.yaml'.
    + Redo commit subject and log message.
    + Order compatibles alphabetically.
    + Redo bindings description taking into account this is a system
      controller node which provides both clocks and resets to the world.
    + Drop label from example.
    + Use 'syscon' as node name in example.
    + Drop no sense 'ralink,rt2880-reset' compatible string 
- Squash patches 6 and 7 together as pointed out by Stephen Boyd.

Previoous series:
v3: https://lore.kernel.org/linux-mips/20230616064735.GA6380@alpha.franken.de/T/#mafbc1704938b94a367c4b6240d9880a67be1f92f
v2: https://lore.kernel.org/linux-clk/CAMhs-H-BfZb3mD8E=LeJ4vT22uibQ1DnaZsfTrtRxSiv=8L5RA@mail.gmail.com/T/#t
v1: https://lore.kernel.org/linux-clk/20230320161823.1424278-1-sergio.paracuellos@gmail.com/T/#t

[0]: https://lore.kernel.org/linux-gpio/e9e6ad87-2db5-9767-ff39-64a302b06185@arinc9.com/T/#t
[1]: https://lore.kernel.org/linux-clk/CAMhs-H-BfZb3mD8E=LeJ4vT22uibQ1DnaZsfTrtRxSiv=8L5RA@mail.gmail.com/T/#mfe725b6e3382c6fb09736472a846cbbc84f264dc

Sergio Paracuellos (9):
  dt-bindings: clock: add mtmips SoCs system controller
  clk: ralink: add clock and reset driver for MTMIPS SoCs
  mips: ralink: rt288x: remove clock related code
  mips: ralink: rt305x: remove clock related code
  mips: ralink: rt3883: remove clock related code
  mips: ralink: mt7620: remove clock related code
  mips: ralink: remove reset related code
  mips: ralink: get cpu rate from new driver code
  MAINTAINERS: add Mediatek MTMIPS Clock maintainer

 .../bindings/clock/mediatek,mtmips-sysc.yaml  |   65 +
 MAINTAINERS                                   |    6 +
 arch/mips/include/asm/mach-ralink/mt7620.h    |   35 -
 arch/mips/include/asm/mach-ralink/rt288x.h    |   10 -
 arch/mips/include/asm/mach-ralink/rt305x.h    |   21 -
 arch/mips/include/asm/mach-ralink/rt3883.h    |    8 -
 arch/mips/ralink/clk.c                        |   26 +-
 arch/mips/ralink/common.h                     |    5 -
 arch/mips/ralink/mt7620.c                     |  226 ----
 arch/mips/ralink/of.c                         |    4 -
 arch/mips/ralink/reset.c                      |   61 -
 arch/mips/ralink/rt288x.c                     |   31 -
 arch/mips/ralink/rt305x.c                     |   78 --
 arch/mips/ralink/rt3883.c                     |   44 -
 drivers/clk/ralink/Kconfig                    |    7 +
 drivers/clk/ralink/Makefile                   |    1 +
 drivers/clk/ralink/clk-mtmips.c               | 1134 +++++++++++++++++
 17 files changed, 1232 insertions(+), 530 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/clock/mediatek,mtmips-sysc.yaml
 create mode 100644 drivers/clk/ralink/clk-mtmips.c

Comments

Krzysztof Kozlowski June 17, 2023, 2:48 p.m. UTC | #1
On 17/06/2023 07:24, Sergio Paracuellos wrote:
> At very early stage on boot, there is a need to set 'mips_hpt_frequency'.
> This timer frequency is a half of the CPU frequency. To get clocks properly
> set we need to call to 'of_clk_init()' and properly get cpu clock frequency
> afterwards. Depending on the SoC, CPU clock index in the clock provider is
> different being two for MT7620 SoC and one for the rest. Hence, adapt code
> to be aligned with new clock driver.


>  void __init plat_time_init(void)
>  {
> +	struct of_phandle_args clkspec;
>  	struct clk *clk;
> +	int cpu_clk_idx;
>  
>  	ralink_of_remap();
>  
> -	ralink_clk_init();
> -	clk = clk_get_sys("cpu", NULL);
> +	cpu_clk_idx = clk_cpu_index();
> +	if (cpu_clk_idx == -1)
> +		panic("unable to get CPU clock index");
> +
> +	of_clk_init(NULL);
> +	clkspec.np = of_find_node_by_name(NULL, "sysc");
> +	clkspec.args_count = 1;
> +	clkspec.args[0] = cpu_clk_idx;
> +	clk = of_clk_get_from_provider(&clkspec);

This is very obfuscated way of getting clock. Why can't you get it from
"clocks" property of "cpu", like every other recent platform?

Anyway, NAK for of_find_node_by_name(), because you now create ABI for
node name. It's broken approach.

Best regards,
Krzysztof
Sergio Paracuellos June 17, 2023, 3:35 p.m. UTC | #2
On Sat, Jun 17, 2023 at 4:48 PM Krzysztof Kozlowski
<krzysztof.kozlowski@linaro.org> wrote:
>
> On 17/06/2023 07:24, Sergio Paracuellos wrote:
> > At very early stage on boot, there is a need to set 'mips_hpt_frequency'.
> > This timer frequency is a half of the CPU frequency. To get clocks properly
> > set we need to call to 'of_clk_init()' and properly get cpu clock frequency
> > afterwards. Depending on the SoC, CPU clock index in the clock provider is
> > different being two for MT7620 SoC and one for the rest. Hence, adapt code
> > to be aligned with new clock driver.
>
>
> >  void __init plat_time_init(void)
> >  {
> > +     struct of_phandle_args clkspec;
> >       struct clk *clk;
> > +     int cpu_clk_idx;
> >
> >       ralink_of_remap();
> >
> > -     ralink_clk_init();
> > -     clk = clk_get_sys("cpu", NULL);
> > +     cpu_clk_idx = clk_cpu_index();
> > +     if (cpu_clk_idx == -1)
> > +             panic("unable to get CPU clock index");
> > +
> > +     of_clk_init(NULL);
> > +     clkspec.np = of_find_node_by_name(NULL, "sysc");
> > +     clkspec.args_count = 1;
> > +     clkspec.args[0] = cpu_clk_idx;
> > +     clk = of_clk_get_from_provider(&clkspec);
>
> This is very obfuscated way of getting clock. Why can't you get it from
> "clocks" property of "cpu", like every other recent platform?

I did not find any other approach that works for me. So I ended up in this one.
Can you point me out in a sample of code doing the same so I can check
if it works for me then?

>
> Anyway, NAK for of_find_node_by_name(), because you now create ABI for
> node name. It's broken approach.

I will change whatever is needed to provide a valid approach. Please,
point me out in the right direction.

>
> Best regards,
> Krzysztof
>

Thanks in advance for your time.

Best regards,
    Sergio Paracuellos
Krzysztof Kozlowski June 17, 2023, 5:27 p.m. UTC | #3
On 17/06/2023 17:35, Sergio Paracuellos wrote:
> On Sat, Jun 17, 2023 at 4:48 PM Krzysztof Kozlowski
> <krzysztof.kozlowski@linaro.org> wrote:
>>
>> On 17/06/2023 07:24, Sergio Paracuellos wrote:
>>> At very early stage on boot, there is a need to set 'mips_hpt_frequency'.
>>> This timer frequency is a half of the CPU frequency. To get clocks properly
>>> set we need to call to 'of_clk_init()' and properly get cpu clock frequency
>>> afterwards. Depending on the SoC, CPU clock index in the clock provider is
>>> different being two for MT7620 SoC and one for the rest. Hence, adapt code
>>> to be aligned with new clock driver.
>>
>>
>>>  void __init plat_time_init(void)
>>>  {
>>> +     struct of_phandle_args clkspec;
>>>       struct clk *clk;
>>> +     int cpu_clk_idx;
>>>
>>>       ralink_of_remap();
>>>
>>> -     ralink_clk_init();
>>> -     clk = clk_get_sys("cpu", NULL);
>>> +     cpu_clk_idx = clk_cpu_index();
>>> +     if (cpu_clk_idx == -1)
>>> +             panic("unable to get CPU clock index");
>>> +
>>> +     of_clk_init(NULL);
>>> +     clkspec.np = of_find_node_by_name(NULL, "sysc");
>>> +     clkspec.args_count = 1;
>>> +     clkspec.args[0] = cpu_clk_idx;
>>> +     clk = of_clk_get_from_provider(&clkspec);
>>
>> This is very obfuscated way of getting clock. Why can't you get it from
>> "clocks" property of "cpu", like every other recent platform?
> 
> I did not find any other approach that works for me. So I ended up in this one.
> Can you point me out in a sample of code doing the same so I can check
> if it works for me then?

You mean bindings?
git grep cpus.yaml

Driver?
git grep clk_get_rate
clk_get
eventually of_clk_get

It all depends on the context.

Anyway, it is very easy to find existing solutions not using
of_find_node_by_name for your platform:

git grep mips_hpt_frequency

First result.


Best regards,
Krzysztof
Sergio Paracuellos June 17, 2023, 7:01 p.m. UTC | #4
On Sat, Jun 17, 2023 at 7:27 PM Krzysztof Kozlowski
<krzysztof.kozlowski@linaro.org> wrote:
>
> On 17/06/2023 17:35, Sergio Paracuellos wrote:
> > On Sat, Jun 17, 2023 at 4:48 PM Krzysztof Kozlowski
> > <krzysztof.kozlowski@linaro.org> wrote:
> >>
> >> On 17/06/2023 07:24, Sergio Paracuellos wrote:
> >>> At very early stage on boot, there is a need to set 'mips_hpt_frequency'.
> >>> This timer frequency is a half of the CPU frequency. To get clocks properly
> >>> set we need to call to 'of_clk_init()' and properly get cpu clock frequency
> >>> afterwards. Depending on the SoC, CPU clock index in the clock provider is
> >>> different being two for MT7620 SoC and one for the rest. Hence, adapt code
> >>> to be aligned with new clock driver.
> >>
> >>
> >>>  void __init plat_time_init(void)
> >>>  {
> >>> +     struct of_phandle_args clkspec;
> >>>       struct clk *clk;
> >>> +     int cpu_clk_idx;
> >>>
> >>>       ralink_of_remap();
> >>>
> >>> -     ralink_clk_init();
> >>> -     clk = clk_get_sys("cpu", NULL);
> >>> +     cpu_clk_idx = clk_cpu_index();
> >>> +     if (cpu_clk_idx == -1)
> >>> +             panic("unable to get CPU clock index");
> >>> +
> >>> +     of_clk_init(NULL);
> >>> +     clkspec.np = of_find_node_by_name(NULL, "sysc");
> >>> +     clkspec.args_count = 1;
> >>> +     clkspec.args[0] = cpu_clk_idx;
> >>> +     clk = of_clk_get_from_provider(&clkspec);
> >>
> >> This is very obfuscated way of getting clock. Why can't you get it from
> >> "clocks" property of "cpu", like every other recent platform?
> >
> > I did not find any other approach that works for me. So I ended up in this one.
> > Can you point me out in a sample of code doing the same so I can check
> > if it works for me then?
>
> You mean bindings?
> git grep cpus.yaml
>
> Driver?
> git grep clk_get_rate
> clk_get
> eventually of_clk_get
>
> It all depends on the context.
>
> Anyway, it is very easy to find existing solutions not using
> of_find_node_by_name for your platform:
>
> git grep mips_hpt_frequency
>
> First result.

I have tested before all of these and got into panic because clock
cannot get retrieved:

For example first result is to make use of clk_get so change the code into:

void __init plat_time_init(void)
{
    struct clk *clk;

    of_clk_init(NULL);
    clk = clk_get(NULL, "cpu");
    if (IS_ERR(clk))
       panic("unable to get CPU clock, err=%ld", PTR_ERR(clk));

    pr_info("CPU Clock: %ldMHz\n", clk_get_rate(clk) / 1000000);
    mips_hpt_frequency = clk_get_rate(clk) / 2;
    clk_put(clk);
    timer_probe();
}

And I panic because I cannot get the cpu clock...

>
>
> Best regards,
> Krzysztof
>

Thanks,
    Sergio Paracuellos