Re: [PATCH 2/3] ARM: at91: mmc-xload: allow overriding card capacity

2021-06-02 Thread Alexander Dahl
Hello,

thanks for the surprisingly detailed explanation.

Am Thu, Jun 03, 2021 at 08:28:48AM +0200 schrieb Ahmad Fatoum:
> Hei hei Alex,
> 
> On 03.06.21 07:34, Alexander Dahl wrote:
> > Am Wed, Jun 02, 2021 at 12:25:23PM +0200 schrieb Ahmad Fatoum:
> >> The PBL MMC driver works with the assumption that the BootROM has left
> >> the SD-Card in transfer mode. There seems to be no definitive way
> >> to find out whether a running card is high capacity (> 2G) or not,
> >> but we need this info when reading, because default capacities accept
> >> their read offset in bytes while high capacity deal in 512 byte blocks.
> > 
> > I'm a little surprised there's not.  Once like ten years ago I had to
> > write a SD card driver for a small microcontroller.  In the firmware I
> > switched to high capacity mode just based on the Card Capacity Status
> > (CCS) bit in the Operation Conditions Register (OCR) of the SD card.
> > Got that after sending advanced command 41 (send op cond) to the card.
> 
> barebox proper does that in sd_send_op_cond() as well.
> 
> > Not sure if it's that easy, or if that command was only sent under
> > certain conditions, but I can not remember just guessing high capacity
> > based on some heuristics nor hard code it. 
> 
> When you build at91_multi_defconfig, multiple images are generated, currently:
> 
> barebox-at91sam9x5ek.img
> barebox-at91sam9263ek.img
> barebox-microchip-ksz9477-evb.img
> barebox-microchip-ksz9477-evb-xload-mmc.img
> barebox-sama5d27-som1-ek.img
> barebox-sama5d27-som1-ek-xload-mmc.img
> barebox-groboards-sama5d27-giantboard.img
> barebox-groboards-sama5d27-giantboard-xload-mmc.img
> barebox-skov-arm9cpu.img
> 
> This patch here is for the xload-mmc.img's, which result from the barebox
> prebootloader. The PBL sets up SDRAM and chainloads barebox proper from the
> same boot medium that itself came from. Because of this limited scope, it can
> reuse here the SD-card setup done by the BootROM. The BootROM leaves the
> SD-Card in transfer mode, allowing the PBL to directly read blocks off the
> SD-Card without a full MMC/SD-Card driver.

I see.

> > We certainly used low
> > capacity (like 32 MB for example) and high capacity cards (4G or more)
> > with that system.
> > 
> > You probably already looked for a reliable way to detect this.  I was
> > just curious why this needs to be hardcoded.
> 
> It's been a while since I wrote the sama5d27 PBL support (which the
> sama5d3 support I am patching here is based on), but I recall that
> the send op cond did not work in transfer mode, the card must be sent
> into idle first, i.e. reset. I'd be happy if there happens to be indeed
> a way to deduce high capacity mode without resetting and having to repeat
> the SD-Card setup in the first boot stage.

Makes sense.  And yes, IIRC correctly, that register read happens
while setting up the card.  If you can not or don't want to reset the
card again, it's probably tricky.

> There are of course alternatives to this:
>   - build barebox twice with different configs for first and second stage:
> There is already code for this (chainload code that uses barebox proper
> driver that fully re-initializes card). The first stage config needs to
> be small to fit into SRAM, so we can no longer generate both stages from
> the same multi-image config as we already do.
> 
>   - provide full MMC support in PBL:
> The objective so far was to keep PBL code to the bare minimum. Doing
> full MMC setup there violates that.
> 
> 
> AFAIK, all barebox platforms, except for i.MX, went for the first alternative
> of having multiple configs. barebox on i.MX doesn't have this issue, because
> it reads barebox from offset 0 of the SD/MMC, which works equally well whether
> booting off default or high capacity cards.
> 
> I like how the i.MX defconfig generates more than a hundred images in one go 
> and
> wanted AT91 to have something similar, but the fact that FAT needs to seek 
> around
> (offsets != 0) complicates this.
> The trade off I took then was assuming high capacity cards and postpone the 
> decision
> on how to deal with default capacity cards in PBL into the future.

I just had the idea of just trying to read from different small
offsets and compare it with the block you get from offset 0.  For
example reading 500 bytes from offset 10 and reading 510 bytes from
offset 0 in byte mode should match the last 500 bytes, but probably in
block mode that wouldn't match.  But that's obviously highly dependent
on the card content and more an ugly hack, right?

Greets
Alex


___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


Re: [PATCH 2/3] ARM: at91: mmc-xload: allow overriding card capacity

2021-06-02 Thread Ahmad Fatoum
Hei hei Alex,

On 03.06.21 07:34, Alexander Dahl wrote:
> Am Wed, Jun 02, 2021 at 12:25:23PM +0200 schrieb Ahmad Fatoum:
>> The PBL MMC driver works with the assumption that the BootROM has left
>> the SD-Card in transfer mode. There seems to be no definitive way
>> to find out whether a running card is high capacity (> 2G) or not,
>> but we need this info when reading, because default capacities accept
>> their read offset in bytes while high capacity deal in 512 byte blocks.
> 
> I'm a little surprised there's not.  Once like ten years ago I had to
> write a SD card driver for a small microcontroller.  In the firmware I
> switched to high capacity mode just based on the Card Capacity Status
> (CCS) bit in the Operation Conditions Register (OCR) of the SD card.
> Got that after sending advanced command 41 (send op cond) to the card.

barebox proper does that in sd_send_op_cond() as well.

> Not sure if it's that easy, or if that command was only sent under
> certain conditions, but I can not remember just guessing high capacity
> based on some heuristics nor hard code it. 

When you build at91_multi_defconfig, multiple images are generated, currently:

barebox-at91sam9x5ek.img
barebox-at91sam9263ek.img
barebox-microchip-ksz9477-evb.img
barebox-microchip-ksz9477-evb-xload-mmc.img
barebox-sama5d27-som1-ek.img
barebox-sama5d27-som1-ek-xload-mmc.img
barebox-groboards-sama5d27-giantboard.img
barebox-groboards-sama5d27-giantboard-xload-mmc.img
barebox-skov-arm9cpu.img

This patch here is for the xload-mmc.img's, which result from the barebox
prebootloader. The PBL sets up SDRAM and chainloads barebox proper from the
same boot medium that itself came from. Because of this limited scope, it can
reuse here the SD-card setup done by the BootROM. The BootROM leaves the
SD-Card in transfer mode, allowing the PBL to directly read blocks off the
SD-Card without a full MMC/SD-Card driver.

> We certainly used low
> capacity (like 32 MB for example) and high capacity cards (4G or more)
> with that system.
> 
> You probably already looked for a reliable way to detect this.  I was
> just curious why this needs to be hardcoded.

It's been a while since I wrote the sama5d27 PBL support (which the
sama5d3 support I am patching here is based on), but I recall that
the send op cond did not work in transfer mode, the card must be sent
into idle first, i.e. reset. I'd be happy if there happens to be indeed
a way to deduce high capacity mode without resetting and having to repeat
the SD-Card setup in the first boot stage.

There are of course alternatives to this:
  - build barebox twice with different configs for first and second stage:
There is already code for this (chainload code that uses barebox proper
driver that fully re-initializes card). The first stage config needs to
be small to fit into SRAM, so we can no longer generate both stages from
the same multi-image config as we already do.

  - provide full MMC support in PBL:
The objective so far was to keep PBL code to the bare minimum. Doing
full MMC setup there violates that.


AFAIK, all barebox platforms, except for i.MX, went for the first alternative
of having multiple configs. barebox on i.MX doesn't have this issue, because
it reads barebox from offset 0 of the SD/MMC, which works equally well whether
booting off default or high capacity cards.

I like how the i.MX defconfig generates more than a hundred images in one go and
wanted AT91 to have something similar, but the fact that FAT needs to seek 
around
(offsets != 0) complicates this.
The trade off I took then was assuming high capacity cards and postpone the 
decision
on how to deal with default capacity cards in PBL into the future.

Cheers,
Ahmad

> 
> Have a nice day
> Alex
> 
>> For i.MX, this is elegantly solved by just reading from sector 0.
>> For AT91, we chainload barebox from FAT, so we need to seek around.
>> So far, we just had an assumption buried in the driver that we are
>> always highcapacity.
>>
>> Export a knob to change this, so we can move the hardcoding from
>> driver to board code, where it's more prominent. This is done
>> in a follow-up commit.
>>
>> Signed-off-by: Ahmad Fatoum 
>> ---
>>  arch/arm/mach-at91/include/mach/xload.h | 3 +++
>>  drivers/mci/atmel-sdhci-pbl.c   | 8 +++-
>>  drivers/mci/atmel_mci_pbl.c | 8 +++-
>>  3 files changed, 17 insertions(+), 2 deletions(-)
>>
>> diff --git a/arch/arm/mach-at91/include/mach/xload.h 
>> b/arch/arm/mach-at91/include/mach/xload.h
>> index bbc70af2108a..038f32554568 100644
>> --- a/arch/arm/mach-at91/include/mach/xload.h
>> +++ b/arch/arm/mach-at91/include/mach/xload.h
>> @@ -12,4 +12,7 @@ int at91_sdhci_bio_init(struct pbl_bio *bio, void __iomem 
>> *base);
>>  int at91_mci_bio_init(struct pbl_bio *bio, void __iomem *base,
>>unsigned int clock, unsigned int slot);
>>  
>> +void at91_mci_set_highcapacity(struct pbl_bio *bio, bool highcap);
>> +void at91_sdhci_set_highca

Re: [PATCH 2/3] ARM: at91: mmc-xload: allow overriding card capacity

2021-06-02 Thread Alexander Dahl
Hello Ahmad,

just glanced over this series by accident, something catched my eye …

Am Wed, Jun 02, 2021 at 12:25:23PM +0200 schrieb Ahmad Fatoum:
> The PBL MMC driver works with the assumption that the BootROM has left
> the SD-Card in transfer mode. There seems to be no definitive way
> to find out whether a running card is high capacity (> 2G) or not,
> but we need this info when reading, because default capacities accept
> their read offset in bytes while high capacity deal in 512 byte blocks.

I'm a little surprised there's not.  Once like ten years ago I had to
write a SD card driver for a small microcontroller.  In the firmware I
switched to high capacity mode just based on the Card Capacity Status
(CCS) bit in the Operation Conditions Register (OCR) of the SD card.
Got that after sending advanced command 41 (send op cond) to the card.
Not sure if it's that easy, or if that command was only sent under
certain conditions, but I can not remember just guessing high capacity
based on some heuristics nor hard code it.  We certainly used low
capacity (like 32 MB for example) and high capacity cards (4G or more)
with that system.

You probably already looked for a reliable way to detect this.  I was
just curious why this needs to be hardcoded.

Have a nice day
Alex

> For i.MX, this is elegantly solved by just reading from sector 0.
> For AT91, we chainload barebox from FAT, so we need to seek around.
> So far, we just had an assumption buried in the driver that we are
> always highcapacity.
> 
> Export a knob to change this, so we can move the hardcoding from
> driver to board code, where it's more prominent. This is done
> in a follow-up commit.
> 
> Signed-off-by: Ahmad Fatoum 
> ---
>  arch/arm/mach-at91/include/mach/xload.h | 3 +++
>  drivers/mci/atmel-sdhci-pbl.c   | 8 +++-
>  drivers/mci/atmel_mci_pbl.c | 8 +++-
>  3 files changed, 17 insertions(+), 2 deletions(-)
> 
> diff --git a/arch/arm/mach-at91/include/mach/xload.h 
> b/arch/arm/mach-at91/include/mach/xload.h
> index bbc70af2108a..038f32554568 100644
> --- a/arch/arm/mach-at91/include/mach/xload.h
> +++ b/arch/arm/mach-at91/include/mach/xload.h
> @@ -12,4 +12,7 @@ int at91_sdhci_bio_init(struct pbl_bio *bio, void __iomem 
> *base);
>  int at91_mci_bio_init(struct pbl_bio *bio, void __iomem *base,
> unsigned int clock, unsigned int slot);
>  
> +void at91_mci_set_highcapacity(struct pbl_bio *bio, bool highcap);
> +void at91_sdhci_set_highcapacity(struct pbl_bio *bio, bool highcap);
> +
>  #endif /* __MACH_XLOAD_H */
> diff --git a/drivers/mci/atmel-sdhci-pbl.c b/drivers/mci/atmel-sdhci-pbl.c
> index 626e4008fe85..317f26f8af0d 100644
> --- a/drivers/mci/atmel-sdhci-pbl.c
> +++ b/drivers/mci/atmel-sdhci-pbl.c
> @@ -99,6 +99,12 @@ static int at91_sdhci_bio_read(struct pbl_bio *bio, off_t 
> start,
>   return blocks_done;
>  }
>  
> +void at91_sdhci_set_highcapacity(struct pbl_bio *bio, bool highcap)
> +{
> + struct at91_sdhci_priv *priv = bio->priv;
> + priv->highcapacity_card = highcap;
> +}
> +
>  static struct at91_sdhci_priv atmel_sdcard;
>  
>  int at91_sdhci_bio_init(struct pbl_bio *bio, void __iomem *base)
> @@ -122,7 +128,7 @@ int at91_sdhci_bio_init(struct pbl_bio *bio, void __iomem 
> *base)
>   ret = at91_sdhci_set_ios(host, &ios);
>  
>// FIXME can we determine this without leaving SD transfer mode?
> - priv->highcapacity_card = 1;
> + at91_sdhci_set_highcapacity(bio, true);
>  
>   return 0;
>  }
> diff --git a/drivers/mci/atmel_mci_pbl.c b/drivers/mci/atmel_mci_pbl.c
> index 767d6f3ce2d7..82e45fd4e89e 100644
> --- a/drivers/mci/atmel_mci_pbl.c
> +++ b/drivers/mci/atmel_mci_pbl.c
> @@ -82,6 +82,12 @@ static int at91_mci_bio_read(struct pbl_bio *bio, off_t 
> start,
>   return blocks_done;
>  }
>  
> +void at91_mci_set_highcapacity(struct pbl_bio *bio, bool highcap)
> +{
> + struct atmel_mci_priv *priv = bio->priv;
> + priv->highcapacity_card = highcap;
> +}
> +
>  int at91_mci_bio_init(struct pbl_bio *bio, void __iomem *base,
> unsigned int clock, unsigned int slot)
>  {
> @@ -110,7 +116,7 @@ int at91_mci_bio_init(struct pbl_bio *bio, void __iomem 
> *base,
>  
>   atmci_common_set_ios(host, &ios);
>  
> - priv->highcapacity_card = 1;
> + at91_mci_set_highcapacity(bio, true);
>  
>   return 0;
>  }
> -- 
> 2.29.2
> 
> 
> ___
> barebox mailing list
> barebox@lists.infradead.org
> http://lists.infradead.org/mailman/listinfo/barebox

___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


Re: [PATCH 11/24] Add rational_best_approximation()

2021-06-02 Thread Trent Piepho
On Wed, Jun 2, 2021 at 3:05 AM Sascha Hauer  wrote:
>
> Import rational_best_approximation() from Linux. This is used by an
> upcoming update of the clk_fractional_divider code.

> +   if ((n2 > max_numerator) || (d2 > max_denominator)) {
> +   unsigned long t = min((max_numerator - n0) / n1,
> + (max_denominator - d0) / d1);
> +

There is a divide by zero bug in this version if the input is greater
than the largest value allowed or less than the smallest non-zero
value allowed.  I sent a patch to lkml for this a week ago,
https://lore.kernel.org/patchwork/patch/1436255/

___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


Re: [PATCH 01/24] clk: clk-mux: Fix handling of CLK_MUX_HIWORD_MASK

2021-06-02 Thread Ahmad Fatoum



On 02.06.21 11:54, Sascha Hauer wrote:
> CLK_MUX_HIWORD_MASK is a flag of the mux clock, not a generic clock
> flag.
> 
> Signed-off-by: Sascha Hauer 

Reviewed-by: Ahmad Fatoum 

> ---
>  drivers/clk/clk-mux.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/clk/clk-mux.c b/drivers/clk/clk-mux.c
> index a4743c51b0..7977a76db9 100644
> --- a/drivers/clk/clk-mux.c
> +++ b/drivers/clk/clk-mux.c
> @@ -34,7 +34,7 @@ static int clk_mux_set_parent(struct clk *clk, u8 idx)
>   val &= ~(((1 << m->width) - 1) << m->shift);
>   val |= idx << m->shift;
>  
> - if (clk->flags & CLK_MUX_HIWORD_MASK)
> + if (m->flags & CLK_MUX_HIWORD_MASK)
>   val |= ((1 << m->width) - 1) << (m->shift + 16);
>   writel(val, m->reg);
>  
> 

-- 
Pengutronix e.K.   | |
Steuerwalder Str. 21   | http://www.pengutronix.de/  |
31137 Hildesheim, Germany  | Phone: +49-5121-206917-0|
Amtsgericht Hildesheim, HRA 2686   | Fax:   +49-5121-206917- |

___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


[PATCH v3 09/13] test: add labgrid configs for some emulated targets

2021-06-02 Thread Ahmad Fatoum
There are various ways barebox can run under emulation, some of them are
described in the documentation and some more in the git history.

Make running the emulators more convenient to use by collecting some
known-good emulator invocations in a machine readable format.

These YAML files can be parsed by labgrid for running tests or executed
by a new test/emulate.pl runner script that will be added in a follow-up
commit.

Using labgrid for this will allow using the same test suite for physical
targets as well in future.

Acked-by: Rouven Czerwinski 
Signed-off-by: Ahmad Fatoum 
---
 test/arm/a15@vexpress_defconfig.yaml | 20 +++
 test/arm/a9@vexpress_defconfig.yaml  | 20 +++
 test/arm/qemu_virt64_defconfig.yaml  | 24 ++
 test/arm/vexpress_defconfig.yaml |  1 +
 test/arm/virt@vexpress_defconfig.yaml| 22 
 test/kconfig/virtio-pci.cfg  |  6 +
 test/mips/be@qemu-malta_defconfig.yaml   | 22 
 test/mips/le@qemu-malta_defconfig.yaml   | 25 ++
 test/mips/qemu-malta_defconfig.yaml  |  1 +
 test/openrisc/generic_defconfig.yaml | 20 +++
 test/riscv/qemu@virt32_defconfig.yaml| 27 
 test/riscv/qemu@virt64_defconfig.yaml| 27 
 test/riscv/sifive_defconfig.yaml | 25 ++
 test/riscv/tinyemu@virt32_defconfig.yaml | 22 
 test/riscv/tinyemu@virt64_defconfig.yaml | 22 
 test/riscv/virt32_defconfig.yaml |  1 +
 test/riscv/virt64_defconfig.yaml |  1 +
 test/sandbox/sandbox_defconfig.yaml  | 12 +
 test/x86/efi_defconfig.yaml  |  1 +
 test/x86/pc@efi_defconfig.yaml   | 31 +++
 test/x86/q35@efi_defconfig.yaml  | 31 +++
 test/x86/virtio@efi_defconfig.yaml   | 32 
 22 files changed, 393 insertions(+)
 create mode 100644 test/arm/a15@vexpress_defconfig.yaml
 create mode 100644 test/arm/a9@vexpress_defconfig.yaml
 create mode 100644 test/arm/qemu_virt64_defconfig.yaml
 create mode 12 test/arm/vexpress_defconfig.yaml
 create mode 100644 test/arm/virt@vexpress_defconfig.yaml
 create mode 100644 test/kconfig/virtio-pci.cfg
 create mode 100644 test/mips/be@qemu-malta_defconfig.yaml
 create mode 100644 test/mips/le@qemu-malta_defconfig.yaml
 create mode 12 test/mips/qemu-malta_defconfig.yaml
 create mode 100644 test/openrisc/generic_defconfig.yaml
 create mode 100644 test/riscv/qemu@virt32_defconfig.yaml
 create mode 100644 test/riscv/qemu@virt64_defconfig.yaml
 create mode 100644 test/riscv/sifive_defconfig.yaml
 create mode 100644 test/riscv/tinyemu@virt32_defconfig.yaml
 create mode 100644 test/riscv/tinyemu@virt64_defconfig.yaml
 create mode 12 test/riscv/virt32_defconfig.yaml
 create mode 12 test/riscv/virt64_defconfig.yaml
 create mode 100644 test/sandbox/sandbox_defconfig.yaml
 create mode 12 test/x86/efi_defconfig.yaml
 create mode 100644 test/x86/pc@efi_defconfig.yaml
 create mode 100644 test/x86/q35@efi_defconfig.yaml
 create mode 100644 test/x86/virtio@efi_defconfig.yaml

diff --git a/test/arm/a15@vexpress_defconfig.yaml 
b/test/arm/a15@vexpress_defconfig.yaml
new file mode 100644
index ..70141df800dc
--- /dev/null
+++ b/test/arm/a15@vexpress_defconfig.yaml
@@ -0,0 +1,20 @@
+targets:
+  main:
+drivers:
+  QEMUDriver:
+qemu_bin: qemu
+machine: vexpress-a15
+cpu: cortex-a15
+memory: 1024M
+bios: barebox-vexpress-ca15.img
+extra_args: ''
+  BareboxDriver:
+prompt: 'barebox@[^:]+:[^ ]+ '
+bootstring: 'commandline:'
+  BareboxTestStrategy: {}
+images:
+  barebox-vexpress-ca15.img: $LG_BUILDDIR/images/barebox-vexpress-ca15.img
+tools:
+  qemu: /usr/bin/qemu-system-arm
+imports:
+  -  ../strategy.py
diff --git a/test/arm/a9@vexpress_defconfig.yaml 
b/test/arm/a9@vexpress_defconfig.yaml
new file mode 100644
index ..0d4faf86904e
--- /dev/null
+++ b/test/arm/a9@vexpress_defconfig.yaml
@@ -0,0 +1,20 @@
+targets:
+  main:
+drivers:
+  QEMUDriver:
+qemu_bin: qemu
+machine: vexpress-a9
+cpu: cortex-a9
+memory: 1024M
+bios: barebox-vexpress-ca9.img
+extra_args: ''
+  BareboxDriver:
+prompt: 'barebox@[^:]+:[^ ]+ '
+bootstring: 'commandline:'
+  BareboxTestStrategy: {}
+images:
+  barebox-vexpress-ca9.img: $LG_BUILDDIR/images/barebox-vexpress-ca9.img
+tools:
+  qemu: /usr/bin/qemu-system-arm
+imports:
+  -  ../strategy.py
diff --git a/test/arm/qemu_virt64_defconfig.yaml 
b/test/arm/qemu_virt64_defconfig.yaml
new file mode 100644
index ..0098ebc93aa9
--- /dev/null
+++ b/test/arm/qemu_virt64_defconfig.yaml
@@ -0,0 +1,24 @@
+targets:
+  main:
+drivers:
+  QEMUDriver:
+qemu_bin: qemu
+machine: virt
+cpu: cortex-a57
+memory: 1024M
+  

[PATCH v3 06/13] Documentation: boards: RISC-V: update TinyEMU support

2021-06-02 Thread Ahmad Fatoum
Adding the tinyemu files as separate files and including them has the
nice benefit that temu users can just use these files directly instead
of copy pasting. While at it, update the section as input, as support
was added meanwhile.

Signed-off-by: Ahmad Fatoum 
---
 Documentation/boards/riscv.rst| 20 +--
 Documentation/boards/riscv/barebox-virt32.cfg |  7 +++
 Documentation/boards/riscv/barebox-virt64.cfg |  7 +++
 3 files changed, 24 insertions(+), 10 deletions(-)
 create mode 100644 Documentation/boards/riscv/barebox-virt32.cfg
 create mode 100644 Documentation/boards/riscv/barebox-virt64.cfg

diff --git a/Documentation/boards/riscv.rst b/Documentation/boards/riscv.rst
index 59cdc00a99e6..955a6fbb6bd1 100644
--- a/Documentation/boards/riscv.rst
+++ b/Documentation/boards/riscv.rst
@@ -41,25 +41,25 @@ TinyEMU
 ---
 
 TinyEMU can emulate a qemu-virt like machine with a RISC-V 32-, 64-
-and 128-bit CPU. It can run barebox with this sample configuration::
+and 128-bit CPU. It can run 32-bit barebox with this sample configuration:
 
-  /* temu barebox-virt64.cfg */
-  {
-  version: 1,
-  machine: "riscv64",
-  memory_size: 256,
-  bios: "bbl64.bin",
-  kernel: "./images/barebox-dt-2nd.img",
-  }
+.. literalinclude:: riscv/barebox-virt32.cfg
+
+as well as 64-bit barebox with this configuration:
+
+.. literalinclude:: riscv/barebox-virt64.cfg
 
 ``barebox-dt-2nd.img`` can be generated like with Qemu. Graphical
-output is also supported, but virtio input support is still missing.
+output and input are also supported.
 To activate add::
 
 display0: { device: "simplefb", width: 800, height: 600 },
+input_device: "virtio",
 
 into the config file.
 
+See https://barebox.org/jsbarebox/?graphic=1 for a live example.
+
 Erizo
 -
 
diff --git a/Documentation/boards/riscv/barebox-virt32.cfg 
b/Documentation/boards/riscv/barebox-virt32.cfg
new file mode 100644
index ..5f0eb34eee9c
--- /dev/null
+++ b/Documentation/boards/riscv/barebox-virt32.cfg
@@ -0,0 +1,7 @@
+{
+version: 1,
+machine: "riscv32",
+memory_size: 256,
+bios: "bbl32.bin",
+kernel: "images/barebox-dt-2nd.img",
+}
diff --git a/Documentation/boards/riscv/barebox-virt64.cfg 
b/Documentation/boards/riscv/barebox-virt64.cfg
new file mode 100644
index ..45e1cd830802
--- /dev/null
+++ b/Documentation/boards/riscv/barebox-virt64.cfg
@@ -0,0 +1,7 @@
+{
+version: 1,
+machine: "riscv64",
+memory_size: 256,
+bios: "bbl64.bin",
+kernel: "images/barebox-dt-2nd.img",
+}
-- 
2.29.2


___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


[PATCH v3 11/13] test: add emulate.pl, a runner for barebox on emulated targets

2021-06-02 Thread Ahmad Fatoum
This script is a wrapper around tuxmake, qemu and labgrid-pytest.

It parses the same YAML files, labgrid-pytest uses and instructs
tuxmake to build and collect the needed images.

By default, it will start an interactive emulator session, but with
--test, it can also run labgrid-pytest instead.

The script has some knowledge of QEMU options to make common tasks like
passing images straight-forward.

Script is written with both manual use and CI in mind.

Signed-off-by: Ahmad Fatoum 
---
 Documentation/boards/emulated.rst |  70 
 test/emulate.pl   | 509 ++
 test/kconfig/base.cfg |   0
 test/kconfig/full.cfg |   0
 4 files changed, 579 insertions(+)
 create mode 100644 Documentation/boards/emulated.rst
 create mode 100755 test/emulate.pl
 create mode 100644 test/kconfig/base.cfg
 create mode 100644 test/kconfig/full.cfg

diff --git a/Documentation/boards/emulated.rst 
b/Documentation/boards/emulated.rst
new file mode 100644
index ..37f5038560a6
--- /dev/null
+++ b/Documentation/boards/emulated.rst
@@ -0,0 +1,70 @@
+Emulated targets
+
+
+Some targets barebox runs on are virtualized by emulators like QEMU, which
+allows basic testing of barebox functionality without the actual hardware.
+
+Generic DT image
+
+
+Supported for ARM and RISC-V. It generates a barebox image that can
+be booted with the Linux kernel booting convention, which makes
+it suitable to be passed as argument to the QEMU ``-kernel`` option.
+The device tree is either the QEMU internal device tree or a device
+tree supplied by QEMU's ``-dtb`` option. The former can be very useful
+with :ref:`virtio_sect`, because QEMU will fix up the virtio mmio
+regions into the device tree and barebox will discover the devices
+automatically, analogously to what it does with VirtIO over PCI.
+
+test/emulate.pl
+---
+
+The ``emulate.pl`` script shipped with barebox can be used to easily
+start VMs. It reads a number of YAML files in ``test/``, which describe
+some virtualized targets that barebox is known to run on.
+Controlled by command line options, these targets are built with
+tuxmake if available and loaded into the emulator for either interactive
+use or for automated testing with Labgrid ``QEMUDriver``.
+
+.. _tuxmake: https://pypi.org/project/tuxmake/
+.. _Labgrid: https://labgrid.org
+
+Install dependencies for interactive use::
+
+  cpan YAML::Tiny # or use e.g. libyaml-tiny-perl on debian
+  pip3 install tuxmake # optional
+
+Example usage::
+
+  # Switch to barebox source directory
+  cd barebox
+
+  # emulate ARM virt with an image built by vexpress_defconfig
+  ARCH=arm ./test/emulate.pl virt@vexpress_defconfig
+
+  # build all MIPS targets known to emulate.pl and exit
+  ARCH=mips ./test/emulate.pl --no-emulate
+
+The script can also be used with a precompiled barebox tree::
+
+  # Switch to build directory
+  export KBUILD_OUTPUT=build
+
+  # run a barebox image built outside tuxmake with an emulated vexpress-ca15
+  ARCH=x86 ./test/emulate.pl efi_defconfig --no-tuxmake
+
+  # run tests instead of starting emulator interactively
+  ARCH=x86 ./test/emulate.pl efi_defconfig --no-tuxmake --test
+
+``emulate.pl`` also has some knowledge on paravirtualized devices::
+
+  # Run target and pass a block device (here /dev/virtioblk0)
+  ARCH=riscv ./test/emulate.pl --blk=rootfs.ext4 virt64_defconfig
+
+``emulate.pl`` if needed command line options can be passed directly
+to the emulator/``pytest`` as well by placing them behind ``--``::
+
+  # appends -device ? to the command line. Add -n to see the final result
+  ARCH=riscv ./test/emulate.pl virt64_defconfig -- -device ?
+
+For a complete listing of options run ``./test/emulate.pl -h``.
diff --git a/test/emulate.pl b/test/emulate.pl
new file mode 100755
index ..6eb166ee
--- /dev/null
+++ b/test/emulate.pl
@@ -0,0 +1,509 @@
+#!/usr/bin/env perl
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (c) 2021 Ahmad Fatoum
+
+use strict;
+use warnings;
+
+use Cwd;
+use File::Basename;
+use File::Spec;
+use File::Temp 'tempdir';
+use Getopt::Long;
+use List::Util 'first';
+use Pod::Usage;
+use YAML::Tiny;
+
+my @QEMU_INTERACTIVE_OPTS = qw(-serial mon:stdio -trace file=/dev/null);
+
+my %targets;
+
+for my $arch (glob dirname(__FILE__) . "/*/") {
+for my $cfg (glob "$arch/*.yaml") {
+   my $linkdest = readlink $cfg // '';
+
+   my $yaml = YAML::Tiny->read($cfg);
+
+   defined $yaml && exists $yaml->[0]{targets} && exists 
$yaml->[0]{targets}{main}
+   or die "Invalid structure for $cfg\n";
+
+   my $path = File::Spec->abs2rel($cfg, getcwd);
+   $cfg = basename($cfg);
+   $cfg =~ s/\.yaml$//;
+   $linkdest =~ s{^.*?([^/]+)\.yaml$}{$1};
+
+   $targets{basename $arch}{$cfg} = $yaml->[0]{targets}{main};
+   $targets{basename $arch}{$cfg}{path} = $path;
+   $targets{basename $arch}{$cfg}{tools} = $yaml->[0]{tools};
+

[PATCH v3 07/13] test: add basic barebox self-test infrastructure

2021-06-02 Thread Ahmad Fatoum
Self tests is code written to run within barebox to exercise
functionality. They offer flexibility to test specific units of barebox
instead of the program as a whole. Add a very simple infrastructure
for registering and executing self-tests. This is based on the Linux
kselftest modules. We don't utilize modules for this, however, because
we only have module support on ARM, but we need a generic solution.

Selftests can be enabled individually and even tested without shell
support to allow tests to happen for size-restricted barebox images
as well.

Acked-by: Rouven Czerwinski 
Signed-off-by: Ahmad Fatoum 
---
 Kconfig |  1 +
 Makefile|  2 +-
 commands/Makefile   |  1 +
 commands/selftest.c | 88 +
 common/startup.c|  4 +++
 include/bselftest.h | 74 ++
 test/Kconfig|  8 +
 test/Makefile   |  1 +
 test/self/Kconfig   | 33 +
 test/self/Makefile  |  3 ++
 test/self/core.c| 22 
 11 files changed, 236 insertions(+), 1 deletion(-)
 create mode 100644 commands/selftest.c
 create mode 100644 include/bselftest.h
 create mode 100644 test/Kconfig
 create mode 100644 test/Makefile
 create mode 100644 test/self/Kconfig
 create mode 100644 test/self/Makefile
 create mode 100644 test/self/core.c

diff --git a/Kconfig b/Kconfig
index 29c32463fb43..7c4cf36881b4 100644
--- a/Kconfig
+++ b/Kconfig
@@ -15,3 +15,4 @@ source "lib/Kconfig"
 source "crypto/Kconfig"
 source "firmware/Kconfig"
 source "scripts/Kconfig"
+source "test/Kconfig"
diff --git a/Makefile b/Makefile
index a1411386f1b3..e856d8672b71 100644
--- a/Makefile
+++ b/Makefile
@@ -581,7 +581,7 @@ endif
 include $(srctree)/scripts/Makefile.lib
 
 # Objects we will link into barebox / subdirs we need to visit
-common-y   := common/ drivers/ commands/ lib/ crypto/ net/ fs/ 
firmware/
+common-y   := common/ drivers/ commands/ lib/ crypto/ net/ fs/ 
firmware/ test/
 
 include $(srctree)/arch/$(SRCARCH)/Makefile
 
diff --git a/commands/Makefile b/commands/Makefile
index 447349fd157d..4b45d266fd56 100644
--- a/commands/Makefile
+++ b/commands/Makefile
@@ -130,5 +130,6 @@ obj-$(CONFIG_CMD_SEED)  += seed.o
 obj-$(CONFIG_CMD_IP_ROUTE_GET)  += ip-route-get.o
 obj-$(CONFIG_CMD_BTHREAD)  += bthread.o
 obj-$(CONFIG_CMD_UBSAN)+= ubsan.o
+obj-$(CONFIG_CMD_SELFTEST) += selftest.o
 
 UBSAN_SANITIZE_ubsan.o := y
diff --git a/commands/selftest.c b/commands/selftest.c
new file mode 100644
index ..a10f1467fece
--- /dev/null
+++ b/commands/selftest.c
@@ -0,0 +1,88 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+#define pr_fmt(fmt) "selftest: " fmt
+
+#include 
+#include 
+#include 
+#include 
+#include 
+
+static int run_selftest(const char *match, bool list)
+{
+   struct selftest *test;
+   int matches = 0;
+   int err = 0;
+
+   list_for_each_entry(test, &selftests, list) {
+   if (list) {
+   printf("%s\n", test->name);
+   matches++;
+   continue;
+   }
+
+   if (match && strcmp(test->name, match))
+   continue;
+
+   err |= test->func();
+   matches++;
+   }
+
+   if (!matches) {
+   if (match) {
+   printf("No tests matching '%s' found.\n", match);
+   return -EINVAL;
+   }
+
+   printf("No tests found.\n");
+   }
+
+   return err;
+}
+
+static int do_selftest(int argc, char *argv[])
+{
+   bool list = false;
+   int i, err = 0;
+   int opt;
+
+   while((opt = getopt(argc, argv, "l")) > 0) {
+   switch(opt) {
+   case 'l':
+   list = true;
+   break;
+   default:
+   return COMMAND_ERROR_USAGE;
+   }
+   }
+
+   if (optind == argc) {
+   err = run_selftest(NULL, list);
+   } else {
+   for (i = optind; i < argc; i++) {
+   err = run_selftest(argv[i], list);
+   if (err)
+   goto out;
+   }
+   }
+
+out:
+   return err ? COMMAND_ERROR : COMMAND_SUCCESS;
+}
+
+BAREBOX_CMD_HELP_START(selftest)
+BAREBOX_CMD_HELP_TEXT("Run enabled barebox self-tests")
+BAREBOX_CMD_HELP_TEXT("If run without arguments, all tests are run")
+BAREBOX_CMD_HELP_TEXT("")
+BAREBOX_CMD_HELP_TEXT("Options:")
+BAREBOX_CMD_HELP_OPT ("-l", "list available tests")
+BAREBOX_CMD_HELP_END
+
+BAREBOX_CMD_START(selftest)
+   .cmd= do_selftest,
+   BAREBOX_CMD_DESC("Run selftests")
+   BAREBOX_CMD_OPTS("[-l] [tests..]")
+   BAREBOX_CMD_GROUP(CMD_GRP_MISC)
+   BAREBOX_CMD_COMPLETE(empty_complete)
+   BAREBOX_CMD_HELP(cmd_selftest_help)
+BAREBOX_CMD_END
diff --git a/common/startu

[PATCH v3 05/13] openrisc: set default KBUILD_IMAGE

2021-06-02 Thread Ahmad Fatoum
For non-multi-image configuration, KBUILD_IMAGE is taken as the image to
install. It was so far not set for openRISC. Define it, so it can be
used by the make install stage, e.g. to collect artifacts after a CI
run.

Signed-off-by: Ahmad Fatoum 
---
 arch/openrisc/Makefile | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/arch/openrisc/Makefile b/arch/openrisc/Makefile
index 72d7fa3d53fe..1776f56df97b 100644
--- a/arch/openrisc/Makefile
+++ b/arch/openrisc/Makefile
@@ -28,3 +28,5 @@ dts := arch/openrisc/dts
 
 %.dtb: scripts
$(Q)$(MAKE) $(build)=$(dts) $(dts)/$@
+
+KBUILD_IMAGE := barebox
-- 
2.29.2


___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


[PATCH v3 10/13] test: add first sample tests

2021-06-02 Thread Ahmad Fatoum
The test can be run manually with e.g.

  labgrid-pytest --lg-env test/arm/qemu_virt64_defconfig.yaml test/py

Acked-by: Rouven Czerwinski 
Signed-off-by: Ahmad Fatoum 
---
 test/.gitignore   |  1 +
 test/__init__.py  |  0
 test/conftest.py  | 27 ++
 test/py/__init__.py   |  0
 test/py/helper.py | 38 +++
 test/py/test_shell.py | 36 +
 test/strategy.py  | 53 +++
 7 files changed, 155 insertions(+)
 create mode 100644 test/.gitignore
 create mode 100644 test/__init__.py
 create mode 100644 test/conftest.py
 create mode 100644 test/py/__init__.py
 create mode 100644 test/py/helper.py
 create mode 100644 test/py/test_shell.py
 create mode 100644 test/strategy.py

diff --git a/test/.gitignore b/test/.gitignore
new file mode 100644
index ..bee8a64b79a9
--- /dev/null
+++ b/test/.gitignore
@@ -0,0 +1 @@
+__pycache__
diff --git a/test/__init__.py b/test/__init__.py
new file mode 100644
index ..e69de29bb2d1
diff --git a/test/conftest.py b/test/conftest.py
new file mode 100644
index ..1a043a91fa78
--- /dev/null
+++ b/test/conftest.py
@@ -0,0 +1,27 @@
+import pytest
+import os
+from .py import helper
+
+
+@pytest.fixture(scope='function')
+def barebox(strategy, target):
+strategy.transition('barebox')
+return target.get_driver('BareboxDriver')
+
+@pytest.fixture(scope="session")
+def barebox_config(strategy, target):
+strategy.transition('barebox')
+command = target.get_driver("BareboxDriver")
+return helper.get_config(command)
+
+def pytest_configure(config):
+if 'LG_BUILDDIR' not in os.environ:
+if 'KBUILD_OUTPUT' in os.environ:
+os.environ['LG_BUILDDIR'] = os.environ['KBUILD_OUTPUT']
+elif os.path.isdir('build'):
+os.environ['LG_BUILDDIR'] = os.path.realpath('build')
+else:
+os.environ['LG_BUILDDIR'] = os.getcwd()
+
+if os.environ['LG_BUILDDIR'] is not None:
+os.environ['LG_BUILDDIR'] = os.path.realpath(os.environ['LG_BUILDDIR'])
diff --git a/test/py/__init__.py b/test/py/__init__.py
new file mode 100644
index ..e69de29bb2d1
diff --git a/test/py/helper.py b/test/py/helper.py
new file mode 100644
index ..4a68e83669ba
--- /dev/null
+++ b/test/py/helper.py
@@ -0,0 +1,38 @@
+from labgrid.driver import BareboxDriver
+import pytest
+import os
+from itertools import filterfalse
+
+
+def get_config(command):
+"""Returns the enabled config options of barebox, either from
+a running instance if supported or by looking into .config
+in the build directory.
+Args:
+command (BareboxDriver): An instance of the BareboxDriver
+Returns:
+list: list of the enabled config options
+"""
+assert isinstance(command, BareboxDriver)
+
+out, err, returncode = command.run("cat /env/data/config")
+if returncode != 0:
+try:
+with open(os.environ['LG_BUILDDIR'] + "/.config") as f:
+out = f.read().splitlines()
+except OSError:
+return set()
+
+options = set()
+for line in out:
+if line and line.startswith("CONFIG_"):
+options.add(line.split('=')[0])
+return options
+
+
+def skip_disabled(config, *options):
+if bool(config):
+undefined = list(filterfalse(config.__contains__, options))
+
+if bool(undefined):
+pytest.skip("skipping test due to disabled " + 
(",".join(undefined)) + " dependency")
diff --git a/test/py/test_shell.py b/test/py/test_shell.py
new file mode 100644
index ..1af7d597a175
--- /dev/null
+++ b/test/py/test_shell.py
@@ -0,0 +1,36 @@
+import pytest
+from .helper import *
+
+
+def test_barebox_true(barebox, barebox_config):
+skip_disabled(barebox_config, "CONFIG_CMD_TRUE")
+
+_, _, returncode = barebox.run('true')
+assert returncode == 0
+
+def test_barebox_false(barebox, barebox_config):
+skip_disabled(barebox_config, "CONFIG_CMD_FALSE")
+
+_, _, returncode = barebox.run('false')
+assert returncode == 1
+
+def test_barebox_md5sum(barebox, barebox_config):
+skip_disabled(barebox_config, "CONFIG_CMD_MD5SUM", "CONFIG_CMD_ECHO")
+
+barebox.run_check("echo -o md5 test")
+out = barebox.run_check("md5sum md5")
+assert out == ["d8e8fca2dc0f896fd7cb4cb0031ba249  md5"]
+
+def test_barebox_version(barebox, barebox_config):
+skip_disabled(barebox_config, "CONFIG_CMD_VERSION")
+
+stdout, _, returncode = barebox.run('version')
+assert 'barebox' in stdout[1]
+assert returncode == 0
+
+def test_barebox_no_err(barebox, barebox_config):
+skip_disabled(barebox_config, "CONFIG_CMD_DMESG")
+
+# TODO extend by err once all qemu platforms conform
+stdout, _, _ = barebox.run('dmesg -l crit,alert,emerg')
+assert stdout == []
diff --git a/test/strategy.py b/test/strategy.py
new file mode 100644
index ..1fe

[PATCH v3 04/13] MIPS: qemu-malta: generate swapped image as part of multi-image build

2021-06-02 Thread Ahmad Fatoum
Having to manually swap the words in the MIPS Malta image for QEMU
little endian emulation is annoying.

Have the multi-image build for Malta generate a second .swapped
image that can be readily used if needed.

Signed-off-by: Ahmad Fatoum 
---
 Documentation/boards/mips/qemu-malta.rst | 16 
 images/.gitignore|  1 +
 images/Makefile  |  2 +-
 images/Makefile.malta| 10 +-
 4 files changed, 15 insertions(+), 14 deletions(-)

diff --git a/Documentation/boards/mips/qemu-malta.rst 
b/Documentation/boards/mips/qemu-malta.rst
index e188ae8c642a..fd37d5edb229 100644
--- a/Documentation/boards/mips/qemu-malta.rst
+++ b/Documentation/boards/mips/qemu-malta.rst
@@ -10,31 +10,23 @@ QEMU run string:
 
   qemu-system-mips -nodefaults -M malta -m 256 \
   -device VGA -serial stdio -monitor null \
-  -bios barebox-flash-image
+  -bios ./images/barebox-qemu-malta.img
 
 
 Little-endian mode
 --
 
-Running little-endian Malta is a bit tricky.
 In little-endian mode the 32bit words in the boot flash image are swapped,
 a neat trick which allows bi-endian firmware.
 
-You have to swap words of ``zbarebox.bin`` image, e.g.:
-
-.. code-block:: sh
-
-  echo arch/mips/pbl/zbarebox.bin \
-  | cpio --create \
-  | cpio --extract --swap --unconditional
-
-QEMU run string:
+The barebox build generates a second 
``./images/barebox-qemu-malta.img.swapped``
+image that can be used in this case, e.g.:
 
 .. code-block:: sh
 
   qemu-system-mipsel -nodefaults -M malta -m 256 \
   -device VGA -serial stdio -monitor null \
-  -bios barebox-flash-image
+  -bios ./images/barebox-qemu-malta.img.swapped
 
 
 Using GXemul
diff --git a/images/.gitignore b/images/.gitignore
index eafdb44b5bdd..3a9a77dad16e 100644
--- a/images/.gitignore
+++ b/images/.gitignore
@@ -32,3 +32,4 @@ barebox.sum
 *.mvebu1img
 *.stm32
 *.nmon
+*.swapped
diff --git a/images/Makefile b/images/Makefile
index c185982c17ed..cc330d957597 100644
--- a/images/Makefile
+++ b/images/Makefile
@@ -218,5 +218,5 @@ $(flash-list): $(image-y-path)
 clean-files := *.pbl *.pblb *.map start_*.imximg *.img barebox.z 
start_*.kwbimg \
start_*.kwbuartimg *.socfpgaimg *.mlo *.t20img *.t20img.cfg *.t30img \
*.t30img.cfg *.t124img *.t124img.cfg *.mlospi *.mlo *.mxsbs *.mxssd \
-   start_*.simximg start_*.usimximg *.zynqimg *.image
+   start_*.simximg start_*.usimximg *.zynqimg *.image *.swapped
 clean-files += pbl.lds
diff --git a/images/Makefile.malta b/images/Makefile.malta
index 5739ec464092..96d7b86b1187 100644
--- a/images/Makefile.malta
+++ b/images/Makefile.malta
@@ -1,3 +1,11 @@
+quiet_cmd_bswap32_image = BSWAP4  $@
+  cmd_bswap32_image = cp $< $@ && \
+ truncate -s %4 $@ && \
+ objcopy -I binary --reverse-byte=4 $@
+
+$(obj)/%.img.swapped: $(obj)/%.img FORCE
+   $(call if_changed,bswap32_image)
+
 pblb-$(CONFIG_BOARD_QEMU_MALTA) += start_qemu_malta
 FILE_barebox-qemu-malta.img = start_qemu_malta.pblb
-image-$(CONFIG_BOARD_QEMU_MALTA) += barebox-qemu-malta.img
+image-$(CONFIG_BOARD_QEMU_MALTA) += barebox-qemu-malta.img 
barebox-qemu-malta.img.swapped
-- 
2.29.2


___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


[PATCH v3 00/13] add barebox in-tree testing infrastructure

2021-06-02 Thread Ahmad Fatoum
v2 -> v3:
  - Added own BareboxTestStrategy instead of reusing labgrid's.
This affords us flexibility in future and lets us get rid
of unused ShellDriver boilerplate in YAML/conftest (Rouven)
  - Make selftest timeout of 30 seconds explicit (Rouven)
This can be increased as needed in future.
  - Print out barebox output when selftest assertion fails (Rouven)
  - Collected Rouven's off-list Acked-by's

v1 -> v2:
  - drop already merged patches
  - use objcopy to swap image byte order instead of writing something new 
(Jules)
  - always run tests from source directory, not build directory when
building out-of-tree (honour KBUILD_OUTPUT, build symlink in that case)
  - add some more sample tests
  - add test/riscv/sifive_defconfig.yaml

Ahmad Fatoum (13):
  kbuild: add install target
  kbuild: add ARCH={i386, x86_64} as aliases for x86
  kbuild: add ARCH=um alias for sandbox
  MIPS: qemu-malta: generate swapped image as part of multi-image build
  openrisc: set default KBUILD_IMAGE
  Documentation: boards: RISC-V: update TinyEMU support
  test: add basic barebox self-test infrastructure
  test: self: port Linux printf kselftest
  test: add labgrid configs for some emulated targets
  test: add first sample tests
  test: add emulate.pl, a runner for barebox on emulated targets
  test: self: run selftests as part of the pytest suite
  test: add bthread test

 Documentation/boards/emulated.rst |  70 +++
 Documentation/boards/mips/qemu-malta.rst  |  16 +-
 Documentation/boards/riscv.rst|  20 +-
 Documentation/boards/riscv/barebox-virt32.cfg |   7 +
 Documentation/boards/riscv/barebox-virt64.cfg |   7 +
 Kconfig   |   1 +
 Makefile  |  28 +-
 arch/openrisc/Makefile|   2 +
 commands/Makefile |   1 +
 commands/selftest.c   |  88 +++
 common/startup.c  |   4 +
 images/.gitignore |   1 +
 images/Makefile   |   7 +-
 images/Makefile.malta |  10 +-
 include/bselftest.h   |  74 +++
 include/stdlib.h  |   5 +
 test/.gitignore   |   1 +
 test/Kconfig  |   8 +
 test/Makefile |   1 +
 test/__init__.py  |   0
 test/arm/a15@vexpress_defconfig.yaml  |  20 +
 test/arm/a9@vexpress_defconfig.yaml   |  20 +
 test/arm/qemu_virt64_defconfig.yaml   |  24 +
 test/arm/vexpress_defconfig.yaml  |   1 +
 test/arm/virt@vexpress_defconfig.yaml |  22 +
 test/conftest.py  |  27 +
 test/emulate.pl   | 509 ++
 test/kconfig/base.cfg |   4 +
 test/kconfig/full.cfg |   2 +
 test/kconfig/virtio-pci.cfg   |   6 +
 test/mips/be@qemu-malta_defconfig.yaml|  22 +
 test/mips/le@qemu-malta_defconfig.yaml|  25 +
 test/mips/qemu-malta_defconfig.yaml   |   1 +
 test/openrisc/generic_defconfig.yaml  |  20 +
 test/py/__init__.py   |   0
 test/py/helper.py |  38 ++
 test/py/test_bselftests.py|   8 +
 test/py/test_bthread.py   |  23 +
 test/py/test_shell.py |  36 ++
 test/riscv/qemu@virt32_defconfig.yaml |  27 +
 test/riscv/qemu@virt64_defconfig.yaml |  27 +
 test/riscv/sifive_defconfig.yaml  |  25 +
 test/riscv/tinyemu@virt32_defconfig.yaml  |  22 +
 test/riscv/tinyemu@virt64_defconfig.yaml  |  22 +
 test/riscv/virt32_defconfig.yaml  |   1 +
 test/riscv/virt64_defconfig.yaml  |   1 +
 test/sandbox/sandbox_defconfig.yaml   |  12 +
 test/self/Kconfig |  39 ++
 test/self/Makefile|   4 +
 test/self/core.c  |  22 +
 test/self/printf.c| 302 +++
 test/strategy.py  |  53 ++
 test/x86/efi_defconfig.yaml   |   1 +
 test/x86/pc@efi_defconfig.yaml|  31 ++
 test/x86/q35@efi_defconfig.yaml   |  31 ++
 test/x86/virtio@efi_defconfig.yaml|  32 ++
 56 files changed, 1786 insertions(+), 25 deletions(-)
 create mode 100644 Documentation/boards/emulated.rst
 create mode 100644 Documentation/boards/riscv/barebox-virt32.cfg
 create mode 100644 Documentation/boards/riscv/barebox-virt64.cfg
 create mode 100644 commands/selftest.c
 create mode 100644 include/bselftest.h
 create mode 100644 test/.gitignore
 create mode 100644 test/Kconfig
 create mode 100644 test/Makefile
 create mode 100644 test/__init__.py
 create mode 100644 test/a

[PATCH v3 13/13] test: add bthread test

2021-06-02 Thread Ahmad Fatoum
The bthread -v command already returns an appropriate exit code if
threads can't be scheduled. Have it spawn 16 threads and switch between
them for a second and verify that all of these threads were indeed
terminated.

Acked-by: Rouven Czerwinski 
Signed-off-by: Ahmad Fatoum 
---
 test/kconfig/full.cfg   |  2 ++
 test/py/test_bthread.py | 23 +++
 2 files changed, 25 insertions(+)
 create mode 100644 test/py/test_bthread.py

diff --git a/test/kconfig/full.cfg b/test/kconfig/full.cfg
index e69de29bb2d1..39275768ea1f 100644
--- a/test/kconfig/full.cfg
+++ b/test/kconfig/full.cfg
@@ -0,0 +1,2 @@
+CONFIG_BTHREAD=y
+CONFIG_CMD_BTHREAD=y
diff --git a/test/py/test_bthread.py b/test/py/test_bthread.py
new file mode 100644
index ..6e7b4ba5007e
--- /dev/null
+++ b/test/py/test_bthread.py
@@ -0,0 +1,23 @@
+import pytest
+from .helper import *
+
+def stale_spawners(barebox):
+threads = barebox.run_check("bthread -i")
+if len(threads) == 0:
+return False
+return len([t for t in threads if t.startswith('spawner')]) > 0
+
+def test_bthread(barebox, barebox_config):
+skip_disabled(barebox_config, "CONFIG_CMD_BTHREAD")
+
+assert not stale_spawners(barebox)
+
+_, _, returncode = barebox.run('bthread -')
+assert returncode == 0
+
+assert not stale_spawners(barebox)
+
+switches = int(barebox.run_check("bthread -c")[0].split()[0])
+yields   = int(barebox.run_check("bthread -t")[0].split()[0])
+
+assert yields < switches
-- 
2.29.2


___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


[PATCH v3 02/13] kbuild: add ARCH={i386, x86_64} as aliases for x86

2021-06-02 Thread Ahmad Fatoum
Linux already does this. Follow suit so we can interoperate with tools
that assume Linux-like Kbuild structure.

Signed-off-by: Ahmad Fatoum 
---
 Makefile | 8 
 1 file changed, 8 insertions(+)

diff --git a/Makefile b/Makefile
index 65e3f6f80c72..3b7dbd4c772d 100644
--- a/Makefile
+++ b/Makefile
@@ -343,6 +343,14 @@ ifeq ($(ARCH),arm64)
SRCARCH := arm
 endif
 
+ifeq ($(ARCH),i386)
+   SRCARCH := x86
+endif
+
+ifeq ($(ARCH),x86_64)
+   SRCARCH := x86
+endif
+
 # Support ARCH=ppc for backward compatibility
 ifeq ($(ARCH),ppc)
SRCARCH := powerpc
-- 
2.29.2


___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


[PATCH v3 03/13] kbuild: add ARCH=um alias for sandbox

2021-06-02 Thread Ahmad Fatoum
ARCH=um (User Mode) is the Linux kernel counterpart to our sandbox
architecture. Add um as an alias, so we can interoperate with tools that
handle ARCH=um specially.

Signed-off-by: Ahmad Fatoum 
---
 Makefile | 4 
 1 file changed, 4 insertions(+)

diff --git a/Makefile b/Makefile
index 3b7dbd4c772d..a1411386f1b3 100644
--- a/Makefile
+++ b/Makefile
@@ -356,6 +356,10 @@ ifeq ($(ARCH),ppc)
SRCARCH := powerpc
 endif
 
+ifeq ($(ARCH),um)
+   SRCARCH := sandbox
+endif
+
 KCONFIG_CONFIG ?= .config
 
 export KCONFIG_CONFIG
-- 
2.29.2


___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


[PATCH v3 12/13] test: self: run selftests as part of the pytest suite

2021-06-02 Thread Ahmad Fatoum
We don't want to enable self tests in the normal configs as they may,
in future, bloat size needlessly. Enable it instead in the base.cfg
fragment and add a test that verifies the selftest command
runs without errors.

Selftests can be run on startup for CONFIG_SHELL_NONE systems. This is
not implemented here. For such systems the test will be skipped
as CONFIG_CMD_SELFTEST won't be defined. To manually skip with
emulate.pl, add --no-kconfig-base.

Acked-by: Rouven Czerwinski 
Signed-off-by: Ahmad Fatoum 
---
 test/kconfig/base.cfg  | 4 
 test/py/test_bselftests.py | 8 
 2 files changed, 12 insertions(+)
 create mode 100644 test/py/test_bselftests.py

diff --git a/test/kconfig/base.cfg b/test/kconfig/base.cfg
index e69de29bb2d1..6a9f68349816 100644
--- a/test/kconfig/base.cfg
+++ b/test/kconfig/base.cfg
@@ -0,0 +1,4 @@
+CONFIG_TEST=y
+CONFIG_SELFTEST=y
+CONFIG_CMD_SELFTEST=y
+CONFIG_SELFTEST_ENABLE_ALL=y
diff --git a/test/py/test_bselftests.py b/test/py/test_bselftests.py
new file mode 100644
index ..7417e7434916
--- /dev/null
+++ b/test/py/test_bselftests.py
@@ -0,0 +1,8 @@
+import pytest
+from .helper import *
+
+def test_bselftest(barebox, barebox_config):
+skip_disabled(barebox_config, "CONFIG_CMD_SELFTEST")
+
+stdout, _, returncode = barebox.run('selftest', timeout=30)
+assert returncode == 0, "selftest failed:\n{}\n".format("\n".join(stdout))
-- 
2.29.2


___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


[PATCH v3 01/13] kbuild: add install target

2021-06-02 Thread Ahmad Fatoum
For CI, it would be useful to have the barebox build install the
artifacts into a directory. Add an install target that does this.

Example usage: make install INSTALL_PATH=install/

Unlike Linux, we don't set INSTALL_PATH to a default value, because
most barebox-enabled boards don't have barebox in a file system.

Signed-off-by: Ahmad Fatoum 
---
 Makefile| 14 ++
 images/Makefile |  5 +
 2 files changed, 19 insertions(+)

diff --git a/Makefile b/Makefile
index ec1770042549..65e3f6f80c72 100644
--- a/Makefile
+++ b/Makefile
@@ -880,6 +880,20 @@ ifndef CONFIG_PBL_IMAGE
$(call cmd,check_file_size,$@,$(CONFIG_BAREBOX_MAX_IMAGE_SIZE))
 endif
 
+install:
+ifeq ($(INSTALL_PATH),)
+   @echo 'error: INSTALL_PATH undefined' >&2
+   @exit 1
+endif
+ifdef CONFIG_PBL_IMAGE
+   $(Q)$(MAKE) $(build)=images __images_install
+   @install -t "$(INSTALL_PATH)" barebox.bin
+else
+   @install -t "$(INSTALL_PATH)" $(KBUILD_IMAGE)
+endif
+
+PHONY += install
+
 # By default the uImage load address is 2MB below CONFIG_TEXT_BASE,
 # leaving space for the compressed PBL image at 1MB below CONFIG_TEXT_BASE.
 UIMAGE_BASE ?= $(shell printf "0x%08x" $$(($(CONFIG_TEXT_BASE) - 0x20)))
diff --git a/images/Makefile b/images/Makefile
index ee1347f6b6bd..c185982c17ed 100644
--- a/images/Makefile
+++ b/images/Makefile
@@ -204,6 +204,11 @@ images: $(image-y-path) $(flash-link) $(flash-list) FORCE
@echo "images built:"
@for i in $(image-y); do echo $$i; done
 
+__images_install: images
+   @for i in $(image-y-path); do install -t "$(INSTALL_PATH)" $$i; done
+
+PHONY += __images_install
+
 $(flash-link): $(link-dest) FORCE
$(call if_changed,ln)
 
-- 
2.29.2


___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


Re: [PATCH v2 12/13] test: self: run selftests as part of the pytest suite

2021-06-02 Thread Rouven Czerwinski
Hi Ahamd,

On Mon, 2021-05-31 at 08:55 +0200, Ahmad Fatoum wrote:
> We don't want to enable self tests in the normal configs as they may,
> in future, bloat size needlessly. Enable it instead in the base.cfg
> fragment and add a test that verifies the selftest command
> runs without errors.
> 
> Selftests can be run on startup for CONFIG_SHELL_NONE systems. This is
> not implemented here. For such systems the test will be skipped
> as CONFIG_CMD_SELFTEST won't be defined. To manually skip with
> emulate.pl, add --no-kconfig-base.
> 
> Signed-off-by: Ahmad Fatoum 
> ---
>  test/kconfig/base.cfg  | 4 
>  test/py/test_bselftests.py | 8 
>  2 files changed, 12 insertions(+)
>  create mode 100644 test/py/test_bselftests.py
> 
> diff --git a/test/kconfig/base.cfg b/test/kconfig/base.cfg
> index e69de29bb2d1..6a9f68349816 100644
> --- a/test/kconfig/base.cfg
> +++ b/test/kconfig/base.cfg
> @@ -0,0 +1,4 @@
> +CONFIG_TEST=y
> +CONFIG_SELFTEST=y
> +CONFIG_CMD_SELFTEST=y
> +CONFIG_SELFTEST_ENABLE_ALL=y
> diff --git a/test/py/test_bselftests.py b/test/py/test_bselftests.py
> new file mode 100644
> index ..48e9d38c12fc
> --- /dev/null
> +++ b/test/py/test_bselftests.py
> @@ -0,0 +1,8 @@
> +import pytest
> +from .helper import *
> +
> +def test_bselftest(barebox, barebox_config):
> +skip_disabled(barebox_config, "CONFIG_CMD_SELFTEST")
> +
> +_, _, returncode = barebox.run('selftest')
> +assert returncode == 0

This will give you the default timeout of 30 seconds for the command
run. As long as you are confident the selftests are going to stay under
this limit this is fine, however you'll probably want to make this
explicit :-)

Also you probably want something akin to this:

stdout, stderr, returncode = barebox.run('selftest')
assert returncode == 0, "selftest failed:\n {}\n".format("".join(stdout))

Which will output the selftest stdout if the selftest fails.

- rcz



___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


Re: [PATCH v2 09/13] test: add labgrid configs for some emulated targets

2021-06-02 Thread Ahmad Fatoum
Hi,

On 02.06.21 13:27, Rouven Czerwinski wrote:
> On Mon, 2021-05-31 at 08:55 +0200, Ahmad Fatoum wrote:
>> There are various ways barebox can run under emulation, some of them are
>> described in the documentation and some more in the git history.
>>
>> Make running the emulators more convenient to use by collecting some
>> known-good emulator invocations in a machine readable format.
>>
>> These YAML files can be parsed by labgrid for running tests or executed
>> by a new test/emulate.pl runner script that will be added in a follow-up
>> commit.
>>
>> Using labgrid for this will allow using the same test suite for physical
>> targets as well in future.
>>

> All of these targets use a Shelldriver, but it is used only to satisfy
> the BareboxStrategy. Scrap the ShellDriver and write a short strategy
> or scrap the strategy as well or wholesale remove strategy usage
> alltogether. Since all thats done is activating the BareboxDriver, this
> should be doable without a strategy.

1) I get by with copy-pasting. Can you point me to an example of how that
   strategy or lack thereof should look like?

2) I want to actually test boots later on, mainly barebox image booting
   another, but I coud imagine loading a FIT and booting that as well.
   In that case I would need the ShellDriver, so why do extra work to
   remove it and add it again later?

Cheers,
Ahmad

> 
>> +  BareboxStrategy: {}
>> +images:
>> +  barebox-vexpress-ca15.img: $LG_BUILDDIR/images/barebox-vexpress-ca15.img
>> +tools:
>> +  qemu: /usr/bin/qemu-system-arm
>> diff --git a/test/arm/a9@vexpress_defconfig.yaml 
>> b/test/arm/a9@vexpress_defconfig.yaml
>> new file mode 100644
>> index ..88e09ca052e6
>> --- /dev/null
>> +++ b/test/arm/a9@vexpress_defconfig.yaml
>> @@ -0,0 +1,23 @@
>> +targets:
>> +  main:
>> +drivers:
>> +  QEMUDriver:
>> +qemu_bin: qemu
>> +machine: vexpress-a9
>> +cpu: cortex-a9
>> +memory: 1024M
>> +bios: barebox-vexpress-ca9.img
>> +extra_args: ''
>> +  BareboxDriver:
>> +prompt: 'barebox@[^:]+:[^ ]+ '
>> +bootstring: 'commandline:'
>> +  ShellDriver:
>> +prompt: 'root@\w+:[^ ]+ '
>> +login_prompt: ' login: '
>> +login_timeout: 90
>> +username: 'root'
>> +  BareboxStrategy: {}
>> +images:
>> +  barebox-vexpress-ca9.img: $LG_BUILDDIR/images/barebox-vexpress-ca9.img
>> +tools:
>> +  qemu: /usr/bin/qemu-system-arm
>> diff --git a/test/arm/qemu_virt64_defconfig.yaml 
>> b/test/arm/qemu_virt64_defconfig.yaml
>> new file mode 100644
>> index ..2dfaaedef61a
>> --- /dev/null
>> +++ b/test/arm/qemu_virt64_defconfig.yaml
>> @@ -0,0 +1,27 @@
>> +targets:
>> +  main:
>> +drivers:
>> +  QEMUDriver:
>> +qemu_bin: qemu
>> +machine: virt
>> +cpu: cortex-a57
>> +memory: 1024M
>> +kernel: barebox-dt-2nd.img
>> +extra_args: ''
>> +  BareboxDriver:
>> +prompt: 'barebox@[^:]+:[^ ]+ '
>> +bootstring: 'commandline:'
>> +  ShellDriver:
>> +prompt: 'root@\w+:[^ ]+ '
>> +login_prompt: ' login: '
>> +login_timeout: 90
>> +username: 'root'
>> +  BareboxStrategy: {}
>> +features:
>> +  - virtio-mmio
>> +runner:
>> +  tuxmake_arch: arm64
>> +images:
>> +  barebox-dt-2nd.img: $LG_BUILDDIR/images/barebox-dt-2nd.img
>> +tools:
>> +  qemu: /usr/bin/qemu-system-aarch64
>> diff --git a/test/arm/vexpress_defconfig.yaml 
>> b/test/arm/vexpress_defconfig.yaml
>> new file mode 12
>> index ..732f51b19dc9
>> --- /dev/null
>> +++ b/test/arm/vexpress_defconfig.yaml
>> @@ -0,0 +1 @@
>> +a9@vexpress_defconfig.yaml
>> \ No newline at end of file
>> diff --git a/test/arm/virt@vexpress_defconfig.yaml 
>> b/test/arm/virt@vexpress_defconfig.yaml
>> new file mode 100644
>> index ..9c2e727c9cb5
>> --- /dev/null
>> +++ b/test/arm/virt@vexpress_defconfig.yaml
>> @@ -0,0 +1,25 @@
>> +targets:
>> +  main:
>> +drivers:
>> +  QEMUDriver:
>> +qemu_bin: qemu
>> +machine: virt
>> +cpu: cortex-a7
>> +memory: 1024M
>> +kernel: barebox-dt-2nd.img
>> +extra_args: ''
>> +  BareboxDriver:
>> +prompt: 'barebox@[^:]+:[^ ]+ '
>> +bootstring: 'commandline:'
>> +  ShellDriver:
>> +prompt: 'root@\w+:[^ ]+ '
>> +login_prompt: ' login: '
>> +login_timeout: 90
>> +username: 'root'
>> +  BareboxStrategy: {}
>> +features:
>> +  - virtio-mmio
>> +images:
>> +  barebox-dt-2nd.img: $LG_BUILDDIR/images/barebox-dt-2nd.img
>> +tools:
>> +  qemu: /usr/bin/qemu-system-arm
>> diff --git a/test/kconfig/virtio-pci.cfg b/test/kconfig/virtio-pci.cfg
>> new file mode 100644
>> index ..78237b8fca26
>> --- /dev/null
>> +++ b/test/kconfig/virtio-pci.cfg
>> @@ -0,0 +1,6 @@
>> +CONFIG_VIRTIO_MENU=y
>> +CONFIG_VIRTIO_PCI=y
>> +CONFIG_VIRTIO_CONSOLE=y
>> +CONFIG_VIRTIO_BLK=y
>> +CONFIG_HWRN

Re: [PATCH v2 10/13] test: add first sample tests

2021-06-02 Thread Ahmad Fatoum
Hi,

On 02.06.21 13:33, Rouven Czerwinski wrote:
> Hi Ahmad,
> 
> On Mon, 2021-05-31 at 08:55 +0200, Ahmad Fatoum wrote:
>> The test can be run manually with e.g.
>>
>>   labgrid-pytest --lg-env test/arm/qemu_virt64_defconfig.yaml test/py
>>
>> Signed-off-by: Ahmad Fatoum 
>> ---
>>  test/.gitignore   |  1 +
>>  test/__init__.py  |  0
>>  test/conftest.py  | 34 ++
>>  test/py/__init__.py   |  0
>>  test/py/helper.py | 38 ++
>>  test/py/test_shell.py | 37 +
>>  6 files changed, 110 insertions(+)
>>  create mode 100644 test/.gitignore
>>  create mode 100644 test/__init__.py
>>  create mode 100644 test/conftest.py
>>  create mode 100644 test/py/__init__.py
>>  create mode 100644 test/py/helper.py
>>  create mode 100644 test/py/test_shell.py
>>
>> diff --git a/test/.gitignore b/test/.gitignore
>> new file mode 100644
>> index ..bee8a64b79a9
>> --- /dev/null
>> +++ b/test/.gitignore
>> @@ -0,0 +1 @@
>> +__pycache__
>> diff --git a/test/__init__.py b/test/__init__.py
>> new file mode 100644
>> index ..e69de29bb2d1
>> diff --git a/test/conftest.py b/test/conftest.py
>> new file mode 100644
>> index ..5acc1a99e18e
>> --- /dev/null
>> +++ b/test/conftest.py
>> @@ -0,0 +1,34 @@
>> +import pytest
>> +import os
>> +from .py import helper
>> +
>> +
>> +@pytest.fixture(scope='function')
>> +def barebox(strategy, target):
>> +strategy.transition('barebox')
>> +return target.get_driver('BareboxDriver')
>> +
>> +
>> +@pytest.fixture(scope='function')
>> +def shell(strategy, target):
>> +strategy.transition('shell')
>> +return target.get_driver('ShellDriver')
> 
> This fixture is not used anywhere, remove it.

See previous mail.

>> +
>> +
>> +@pytest.fixture(scope="session")
>> +def barebox_config(strategy, target):
>> +strategy.transition('barebox')
>> +command = target.get_driver("BareboxDriver")
>> +return helper.get_config(command)
>> +
>> +def pytest_configure(config):
>> +if 'LG_BUILDDIR' not in os.environ:
>> +if 'KBUILD_OUTPUT' in os.environ:
>> +os.environ['LG_BUILDDIR'] = os.environ['KBUILD_OUTPUT']
>> +elif os.path.isdir('build'):
>> +os.environ['LG_BUILDDIR'] = os.path.realpath('build')
>> +else:
>> +os.environ['LG_BUILDDIR'] = os.getcwd()
>> +
>> +if os.environ['LG_BUILDDIR'] is not None:
>> +os.environ['LG_BUILDDIR'] = 
>> os.path.realpath(os.environ['LG_BUILDDIR'])
>> diff --git a/test/py/__init__.py b/test/py/__init__.py
>> new file mode 100644
>> index ..e69de29bb2d1
>> diff --git a/test/py/helper.py b/test/py/helper.py
>> new file mode 100644
>> index ..4a68e83669ba
>> --- /dev/null
>> +++ b/test/py/helper.py
>> @@ -0,0 +1,38 @@
>> +from labgrid.driver import BareboxDriver
>> +import pytest
>> +import os
>> +from itertools import filterfalse
>> +
>> +
>> +def get_config(command):
>> +"""Returns the enabled config options of barebox, either from
>> +a running instance if supported or by looking into .config
>> +in the build directory.
>> +Args:
>> +command (BareboxDriver): An instance of the BareboxDriver
>> +Returns:
>> +list: list of the enabled config options
>> +"""
>> +assert isinstance(command, BareboxDriver)
>> +
>> +out, err, returncode = command.run("cat /env/data/config")
>> +if returncode != 0:
>> +try:
>> +with open(os.environ['LG_BUILDDIR'] + "/.config") as f:
> 
> Please don't use the LG_ namespace currently used by labgrid, this
> variable is introduced by your wrapper script, something like
> BB_LG_BUILDDIR indicates that this is only used in the barebox test
> scripts.

Labgrid filters out anything that doesn't start with LG_ AFAIK.

> 
>> +out = f.read().splitlines()
>> +except OSError:
>> +return set()
>> +
>> +options = set()
>> +for line in out:
>> +if line and line.startswith("CONFIG_"):
>> +options.add(line.split('=')[0])
>> +return options
>> +
>> +
>> +def skip_disabled(config, *options):
>> +if bool(config):
>> +undefined = list(filterfalse(config.__contains__, options))
>> +
>> +if bool(undefined):
>> +pytest.skip("skipping test due to disabled " + 
>> (",".join(undefined)) + " dependency")
>> diff --git a/test/py/test_shell.py b/test/py/test_shell.py
>> new file mode 100644
>> index ..0d2dfe38c5dd
>> --- /dev/null
>> +++ b/test/py/test_shell.py
>> @@ -0,0 +1,37 @@
>> +import pytest
>> +from .helper import *
>> +
>> +
>> +def test_barebox_true(barebox, barebox_config):
>> +skip_disabled(barebox_config, "CONFIG_CMD_TRUE")
>> +
>> +_, _, returncode = barebox.run('true')
>> +assert returncode == 0
>> +
>> +
>> +def test_barebox_false(barebox, barebox_config):
>> +skip_disabled(barebox_config, "CONFIG_CMD_FAL

Re: [PATCH v2 10/13] test: add first sample tests

2021-06-02 Thread Rouven Czerwinski
Hi Ahmad,

On Mon, 2021-05-31 at 08:55 +0200, Ahmad Fatoum wrote:
> The test can be run manually with e.g.
> 
>   labgrid-pytest --lg-env test/arm/qemu_virt64_defconfig.yaml test/py
> 
> Signed-off-by: Ahmad Fatoum 
> ---
>  test/.gitignore   |  1 +
>  test/__init__.py  |  0
>  test/conftest.py  | 34 ++
>  test/py/__init__.py   |  0
>  test/py/helper.py | 38 ++
>  test/py/test_shell.py | 37 +
>  6 files changed, 110 insertions(+)
>  create mode 100644 test/.gitignore
>  create mode 100644 test/__init__.py
>  create mode 100644 test/conftest.py
>  create mode 100644 test/py/__init__.py
>  create mode 100644 test/py/helper.py
>  create mode 100644 test/py/test_shell.py
> 
> diff --git a/test/.gitignore b/test/.gitignore
> new file mode 100644
> index ..bee8a64b79a9
> --- /dev/null
> +++ b/test/.gitignore
> @@ -0,0 +1 @@
> +__pycache__
> diff --git a/test/__init__.py b/test/__init__.py
> new file mode 100644
> index ..e69de29bb2d1
> diff --git a/test/conftest.py b/test/conftest.py
> new file mode 100644
> index ..5acc1a99e18e
> --- /dev/null
> +++ b/test/conftest.py
> @@ -0,0 +1,34 @@
> +import pytest
> +import os
> +from .py import helper
> +
> +
> +@pytest.fixture(scope='function')
> +def barebox(strategy, target):
> +strategy.transition('barebox')
> +return target.get_driver('BareboxDriver')
> +
> +
> +@pytest.fixture(scope='function')
> +def shell(strategy, target):
> +strategy.transition('shell')
> +return target.get_driver('ShellDriver')

This fixture is not used anywhere, remove it.

> +
> +
> +@pytest.fixture(scope="session")
> +def barebox_config(strategy, target):
> +strategy.transition('barebox')
> +command = target.get_driver("BareboxDriver")
> +return helper.get_config(command)
> +
> +def pytest_configure(config):
> +if 'LG_BUILDDIR' not in os.environ:
> +if 'KBUILD_OUTPUT' in os.environ:
> +os.environ['LG_BUILDDIR'] = os.environ['KBUILD_OUTPUT']
> +elif os.path.isdir('build'):
> +os.environ['LG_BUILDDIR'] = os.path.realpath('build')
> +else:
> +os.environ['LG_BUILDDIR'] = os.getcwd()
> +
> +if os.environ['LG_BUILDDIR'] is not None:
> +os.environ['LG_BUILDDIR'] = 
> os.path.realpath(os.environ['LG_BUILDDIR'])
> diff --git a/test/py/__init__.py b/test/py/__init__.py
> new file mode 100644
> index ..e69de29bb2d1
> diff --git a/test/py/helper.py b/test/py/helper.py
> new file mode 100644
> index ..4a68e83669ba
> --- /dev/null
> +++ b/test/py/helper.py
> @@ -0,0 +1,38 @@
> +from labgrid.driver import BareboxDriver
> +import pytest
> +import os
> +from itertools import filterfalse
> +
> +
> +def get_config(command):
> +"""Returns the enabled config options of barebox, either from
> +a running instance if supported or by looking into .config
> +in the build directory.
> +Args:
> +command (BareboxDriver): An instance of the BareboxDriver
> +Returns:
> +list: list of the enabled config options
> +"""
> +assert isinstance(command, BareboxDriver)
> +
> +out, err, returncode = command.run("cat /env/data/config")
> +if returncode != 0:
> +try:
> +with open(os.environ['LG_BUILDDIR'] + "/.config") as f:

Please don't use the LG_ namespace currently used by labgrid, this
variable is introduced by your wrapper script, something like
BB_LG_BUILDDIR indicates that this is only used in the barebox test
scripts.

> +out = f.read().splitlines()
> +except OSError:
> +return set()
> +
> +options = set()
> +for line in out:
> +if line and line.startswith("CONFIG_"):
> +options.add(line.split('=')[0])
> +return options
> +
> +
> +def skip_disabled(config, *options):
> +if bool(config):
> +undefined = list(filterfalse(config.__contains__, options))
> +
> +if bool(undefined):
> +pytest.skip("skipping test due to disabled " + 
> (",".join(undefined)) + " dependency")
> diff --git a/test/py/test_shell.py b/test/py/test_shell.py
> new file mode 100644
> index ..0d2dfe38c5dd
> --- /dev/null
> +++ b/test/py/test_shell.py
> @@ -0,0 +1,37 @@
> +import pytest
> +from .helper import *
> +
> +
> +def test_barebox_true(barebox, barebox_config):
> +skip_disabled(barebox_config, "CONFIG_CMD_TRUE")
> +
> +_, _, returncode = barebox.run('true')
> +assert returncode == 0
> +
> +
> +def test_barebox_false(barebox, barebox_config):
> +skip_disabled(barebox_config, "CONFIG_CMD_FALSE")
> +
> +_, _, returncode = barebox.run('false')
> +assert returncode == 1
> +
> +def test_barebox_md5sum(barebox, barebox_config):
> +skip_disabled(barebox_config, "CONFIG_CMD_MD5SUM", "CONFIG_CMD_ECHO")
> +
> +barebox.run_check("echo -o md5 test")
> +out = barebox.ru

Re: [PATCH v2 09/13] test: add labgrid configs for some emulated targets

2021-06-02 Thread Rouven Czerwinski
On Mon, 2021-05-31 at 08:55 +0200, Ahmad Fatoum wrote:
> There are various ways barebox can run under emulation, some of them are
> described in the documentation and some more in the git history.
> 
> Make running the emulators more convenient to use by collecting some
> known-good emulator invocations in a machine readable format.
> 
> These YAML files can be parsed by labgrid for running tests or executed
> by a new test/emulate.pl runner script that will be added in a follow-up
> commit.
> 
> Using labgrid for this will allow using the same test suite for physical
> targets as well in future.
> 
> Signed-off-by: Ahmad Fatoum 
> ---
>  test/arm/a15@vexpress_defconfig.yaml | 23 
>  test/arm/a9@vexpress_defconfig.yaml  | 23 
>  test/arm/qemu_virt64_defconfig.yaml  | 27 ++
>  test/arm/vexpress_defconfig.yaml |  1 +
>  test/arm/virt@vexpress_defconfig.yaml| 25 +
>  test/kconfig/virtio-pci.cfg  |  6 
>  test/mips/be@qemu-malta_defconfig.yaml   | 25 +
>  test/mips/le@qemu-malta_defconfig.yaml   | 28 +++
>  test/mips/qemu-malta_defconfig.yaml  |  1 +
>  test/openrisc/generic_defconfig.yaml | 23 
>  test/riscv/qemu@virt32_defconfig.yaml| 30 
>  test/riscv/qemu@virt64_defconfig.yaml| 30 
>  test/riscv/sifive_defconfig.yaml | 28 +++
>  test/riscv/tinyemu@virt32_defconfig.yaml | 25 +
>  test/riscv/tinyemu@virt64_defconfig.yaml | 25 +
>  test/riscv/virt32_defconfig.yaml |  1 +
>  test/riscv/virt64_defconfig.yaml |  1 +
>  test/sandbox/sandbox_defconfig.yaml  | 12 
>  test/x86/efi_defconfig.yaml  |  1 +
>  test/x86/pc@efi_defconfig.yaml   | 34 +++
>  test/x86/q35@efi_defconfig.yaml  | 34 +++
>  test/x86/virtio@efi_defconfig.yaml   | 35 
>  22 files changed, 438 insertions(+)
>  create mode 100644 test/arm/a15@vexpress_defconfig.yaml
>  create mode 100644 test/arm/a9@vexpress_defconfig.yaml
>  create mode 100644 test/arm/qemu_virt64_defconfig.yaml
>  create mode 12 test/arm/vexpress_defconfig.yaml
>  create mode 100644 test/arm/virt@vexpress_defconfig.yaml
>  create mode 100644 test/kconfig/virtio-pci.cfg
>  create mode 100644 test/mips/be@qemu-malta_defconfig.yaml
>  create mode 100644 test/mips/le@qemu-malta_defconfig.yaml
>  create mode 12 test/mips/qemu-malta_defconfig.yaml
>  create mode 100644 test/openrisc/generic_defconfig.yaml
>  create mode 100644 test/riscv/qemu@virt32_defconfig.yaml
>  create mode 100644 test/riscv/qemu@virt64_defconfig.yaml
>  create mode 100644 test/riscv/sifive_defconfig.yaml
>  create mode 100644 test/riscv/tinyemu@virt32_defconfig.yaml
>  create mode 100644 test/riscv/tinyemu@virt64_defconfig.yaml
>  create mode 12 test/riscv/virt32_defconfig.yaml
>  create mode 12 test/riscv/virt64_defconfig.yaml
>  create mode 100644 test/sandbox/sandbox_defconfig.yaml
>  create mode 12 test/x86/efi_defconfig.yaml
>  create mode 100644 test/x86/pc@efi_defconfig.yaml
>  create mode 100644 test/x86/q35@efi_defconfig.yaml
>  create mode 100644 test/x86/virtio@efi_defconfig.yaml
> 
> diff --git a/test/arm/a15@vexpress_defconfig.yaml 
> b/test/arm/a15@vexpress_defconfig.yaml
> new file mode 100644
> index ..7ed93ba0a19f
> --- /dev/null
> +++ b/test/arm/a15@vexpress_defconfig.yaml
> @@ -0,0 +1,23 @@
> +targets:
> +  main:
> +drivers:
> +  QEMUDriver:
> +qemu_bin: qemu
> +machine: vexpress-a15
> +cpu: cortex-a15
> +memory: 1024M
> +bios: barebox-vexpress-ca15.img
> +extra_args: ''
> +  BareboxDriver:
> +prompt: 'barebox@[^:]+:[^ ]+ '
> +bootstring: 'commandline:'
> +  ShellDriver:
> +prompt: 'root@\w+:[^ ]+ '
> +login_prompt: ' login: '
> +login_timeout: 90
> +username: 'root'

All of these targets use a Shelldriver, but it is used only to satisfy
the BareboxStrategy. Scrap the ShellDriver and write a short strategy
or scrap the strategy as well or wholesale remove strategy usage
alltogether. Since all thats done is activating the BareboxDriver, this
should be doable without a strategy.

> +  BareboxStrategy: {}
> +images:
> +  barebox-vexpress-ca15.img: $LG_BUILDDIR/images/barebox-vexpress-ca15.img
> +tools:
> +  qemu: /usr/bin/qemu-system-arm
> diff --git a/test/arm/a9@vexpress_defconfig.yaml 
> b/test/arm/a9@vexpress_defconfig.yaml
> new file mode 100644
> index ..88e09ca052e6
> --- /dev/null
> +++ b/test/arm/a9@vexpress_defconfig.yaml
> @@ -0,0 +1,23 @@
> +targets:
> +  main:
> +drivers:
> +  QEMUDriver:
> +qemu_bin: qemu
> +machine: vexpress-a9
> +cpu: cortex-a9
> +memory: 1024M
> +bios: barebox-vexpress-ca9.img
> +

Re: [PATCH master] usb: dwc2: increase timeout for waiting on host mode

2021-06-02 Thread Ahmad Fatoum
On 07.05.21 10:07, Sascha Hauer wrote:
> Instead of applying two patches I suggest we go with the following
> oneliner which is fine for master.

I'd have preferred two patches, because then I've more time to
procrastinate before testing the second one. ;)

FTR: I just tested v2021.05.0 and USB now works (tested by DHCP
over the USB-Ethernet controller).

Thanks,
Ahmad

> 
> Sascha
> 
> --8<
> 
> From 9fc2b0364c4b686d110b772be2245be3a513407d Mon Sep 17 00:00:00 2001
> From: Sascha Hauer 
> Date: Fri, 7 May 2021 09:58:43 +0200
> Subject: [PATCH] usb: dwc2: Fix wait for mode timeout
> 
> The timeout waiting for host mode should be 110ms instead of 110us.
> The timeout is the same as in Linux now and the same it was before
> fdd30cc251. It takes 49ms to go to host mode on the Raspberry Pi 3b,
> so host mode stopped working with 26459ab780 which removed an additional
> 100ms delay before dwc2_wait_for_mode() was actually called.
> 
> Fixes: fdd30cc251 ("usb: dwc2: Rework dwc2_wait_for_mode to use 
> wait_on_timeout")
> Fixes: 26459ab780 ("usb: dwc2: Rework wait for host mode during core reset")
> Reported-by: Ahmad Fatoum 
> Signed-off-by: Sascha Hauer 
> ---
>  drivers/usb/dwc2/core.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/usb/dwc2/core.c b/drivers/usb/dwc2/core.c
> index 5d04a07b03..8a78d8199e 100644
> --- a/drivers/usb/dwc2/core.c
> +++ b/drivers/usb/dwc2/core.c
> @@ -688,7 +688,7 @@ int dwc2_get_dr_mode(struct dwc2 *dwc2)
>   */
>  void dwc2_wait_for_mode(struct dwc2 *dwc2, bool host_mode)
>  {
> - unsigned int timeout = 110 * USECOND;
> + unsigned int timeout = 110 * MSECOND;
>   int ret;
>  
>   dev_vdbg(dwc2->dev, "Waiting for %s mode\n",
> 

-- 
Pengutronix e.K.   | |
Steuerwalder Str. 21   | http://www.pengutronix.de/  |
31137 Hildesheim, Germany  | Phone: +49-5121-206917-0|
Amtsgericht Hildesheim, HRA 2686   | Fax:   +49-5121-206917- |

___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


Re: [PATCH 2/3] ARM: at91: mmc-xload: allow overriding card capacity

2021-06-02 Thread Ahmad Fatoum
I see now that I botched, the title "mmc-xload" instead of "xload-mmc".
@Sascha, please tell me if I should resend.

On 02.06.21 12:25, Ahmad Fatoum wrote:
> The PBL MMC driver works with the assumption that the BootROM has left
> the SD-Card in transfer mode. There seems to be no definitive way
> to find out whether a running card is high capacity (> 2G) or not,
> but we need this info when reading, because default capacities accept
> their read offset in bytes while high capacity deal in 512 byte blocks.
> 
> For i.MX, this is elegantly solved by just reading from sector 0.
> For AT91, we chainload barebox from FAT, so we need to seek around.
> So far, we just had an assumption buried in the driver that we are
> always highcapacity.
> 
> Export a knob to change this, so we can move the hardcoding from
> driver to board code, where it's more prominent. This is done
> in a follow-up commit.
> 
> Signed-off-by: Ahmad Fatoum 
> ---
>  arch/arm/mach-at91/include/mach/xload.h | 3 +++
>  drivers/mci/atmel-sdhci-pbl.c   | 8 +++-
>  drivers/mci/atmel_mci_pbl.c | 8 +++-
>  3 files changed, 17 insertions(+), 2 deletions(-)
> 
> diff --git a/arch/arm/mach-at91/include/mach/xload.h 
> b/arch/arm/mach-at91/include/mach/xload.h
> index bbc70af2108a..038f32554568 100644
> --- a/arch/arm/mach-at91/include/mach/xload.h
> +++ b/arch/arm/mach-at91/include/mach/xload.h
> @@ -12,4 +12,7 @@ int at91_sdhci_bio_init(struct pbl_bio *bio, void __iomem 
> *base);
>  int at91_mci_bio_init(struct pbl_bio *bio, void __iomem *base,
> unsigned int clock, unsigned int slot);
>  
> +void at91_mci_set_highcapacity(struct pbl_bio *bio, bool highcap);
> +void at91_sdhci_set_highcapacity(struct pbl_bio *bio, bool highcap);
> +
>  #endif /* __MACH_XLOAD_H */
> diff --git a/drivers/mci/atmel-sdhci-pbl.c b/drivers/mci/atmel-sdhci-pbl.c
> index 626e4008fe85..317f26f8af0d 100644
> --- a/drivers/mci/atmel-sdhci-pbl.c
> +++ b/drivers/mci/atmel-sdhci-pbl.c
> @@ -99,6 +99,12 @@ static int at91_sdhci_bio_read(struct pbl_bio *bio, off_t 
> start,
>   return blocks_done;
>  }
>  
> +void at91_sdhci_set_highcapacity(struct pbl_bio *bio, bool highcap)
> +{
> + struct at91_sdhci_priv *priv = bio->priv;
> + priv->highcapacity_card = highcap;
> +}
> +
>  static struct at91_sdhci_priv atmel_sdcard;
>  
>  int at91_sdhci_bio_init(struct pbl_bio *bio, void __iomem *base)
> @@ -122,7 +128,7 @@ int at91_sdhci_bio_init(struct pbl_bio *bio, void __iomem 
> *base)
>   ret = at91_sdhci_set_ios(host, &ios);
>  
>// FIXME can we determine this without leaving SD transfer mode?
> - priv->highcapacity_card = 1;
> + at91_sdhci_set_highcapacity(bio, true);
>  
>   return 0;
>  }
> diff --git a/drivers/mci/atmel_mci_pbl.c b/drivers/mci/atmel_mci_pbl.c
> index 767d6f3ce2d7..82e45fd4e89e 100644
> --- a/drivers/mci/atmel_mci_pbl.c
> +++ b/drivers/mci/atmel_mci_pbl.c
> @@ -82,6 +82,12 @@ static int at91_mci_bio_read(struct pbl_bio *bio, off_t 
> start,
>   return blocks_done;
>  }
>  
> +void at91_mci_set_highcapacity(struct pbl_bio *bio, bool highcap)
> +{
> + struct atmel_mci_priv *priv = bio->priv;
> + priv->highcapacity_card = highcap;
> +}
> +
>  int at91_mci_bio_init(struct pbl_bio *bio, void __iomem *base,
> unsigned int clock, unsigned int slot)
>  {
> @@ -110,7 +116,7 @@ int at91_mci_bio_init(struct pbl_bio *bio, void __iomem 
> *base,
>  
>   atmci_common_set_ios(host, &ios);
>  
> - priv->highcapacity_card = 1;
> + at91_mci_set_highcapacity(bio, true);
>  
>   return 0;
>  }
> 

-- 
Pengutronix e.K.   | |
Steuerwalder Str. 21   | http://www.pengutronix.de/  |
31137 Hildesheim, Germany  | Phone: +49-5121-206917-0|
Amtsgericht Hildesheim, HRA 2686   | Fax:   +49-5121-206917- |

___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


Re: [PATCH 1/3] ARM: at91: microchip-ksz9477-evb: support PBL console

2021-06-02 Thread Ahmad Fatoum
Hello Oleksij,

On 02.06.21 12:25, Ahmad Fatoum wrote:
> First stage PBL does FAT file system accesses, so there is more that
> could go wrong compared with second stage PBL that just does extraction.
> 
> Enable PBL console to get some error messages out if things don't work
> out.

I don't have the HW handy to test this. Would be great if you could
boot this up once to see nothing broke. Thanks!

> 
> Signed-off-by: Ahmad Fatoum 
> ---
>  arch/arm/boards/microchip-ksz9477-evb/lowlevel.c | 5 +++--
>  1 file changed, 3 insertions(+), 2 deletions(-)
> 
> diff --git a/arch/arm/boards/microchip-ksz9477-evb/lowlevel.c 
> b/arch/arm/boards/microchip-ksz9477-evb/lowlevel.c
> index 93ae4819750c..7c5a69af05de 100644
> --- a/arch/arm/boards/microchip-ksz9477-evb/lowlevel.c
> +++ b/arch/arm/boards/microchip-ksz9477-evb/lowlevel.c
> @@ -41,8 +41,9 @@ 
> SAMA5_ENTRY_FUNCTION(start_sama5d3_xplained_ung8071_xload_mmc, r4)
>  
>   sama5d3_udelay_init(MASTER_CLOCK);
>   sama5d3_xplained_ddrconf();
> - if (IS_ENABLED(CONFIG_DEBUG_LL))
> - dbgu_init();
> +
> + dbgu_init();
> + pbl_set_putc(at91_dbgu_putc, IOMEM(AT91_BASE_DBGU1));
>  
>   sama5d3_atmci_start_image(0, MASTER_CLOCK, 0);
>  }
> 

-- 
Pengutronix e.K.   | |
Steuerwalder Str. 21   | http://www.pengutronix.de/  |
31137 Hildesheim, Germany  | Phone: +49-5121-206917-0|
Amtsgericht Hildesheim, HRA 2686   | Fax:   +49-5121-206917- |

___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


[PATCH 1/3] ARM: at91: microchip-ksz9477-evb: support PBL console

2021-06-02 Thread Ahmad Fatoum
First stage PBL does FAT file system accesses, so there is more that
could go wrong compared with second stage PBL that just does extraction.

Enable PBL console to get some error messages out if things don't work
out.

Signed-off-by: Ahmad Fatoum 
---
 arch/arm/boards/microchip-ksz9477-evb/lowlevel.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/arch/arm/boards/microchip-ksz9477-evb/lowlevel.c 
b/arch/arm/boards/microchip-ksz9477-evb/lowlevel.c
index 93ae4819750c..7c5a69af05de 100644
--- a/arch/arm/boards/microchip-ksz9477-evb/lowlevel.c
+++ b/arch/arm/boards/microchip-ksz9477-evb/lowlevel.c
@@ -41,8 +41,9 @@ 
SAMA5_ENTRY_FUNCTION(start_sama5d3_xplained_ung8071_xload_mmc, r4)
 
sama5d3_udelay_init(MASTER_CLOCK);
sama5d3_xplained_ddrconf();
-   if (IS_ENABLED(CONFIG_DEBUG_LL))
-   dbgu_init();
+
+   dbgu_init();
+   pbl_set_putc(at91_dbgu_putc, IOMEM(AT91_BASE_DBGU1));
 
sama5d3_atmci_start_image(0, MASTER_CLOCK, 0);
 }
-- 
2.29.2


___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


[PATCH 2/3] ARM: at91: mmc-xload: allow overriding card capacity

2021-06-02 Thread Ahmad Fatoum
The PBL MMC driver works with the assumption that the BootROM has left
the SD-Card in transfer mode. There seems to be no definitive way
to find out whether a running card is high capacity (> 2G) or not,
but we need this info when reading, because default capacities accept
their read offset in bytes while high capacity deal in 512 byte blocks.

For i.MX, this is elegantly solved by just reading from sector 0.
For AT91, we chainload barebox from FAT, so we need to seek around.
So far, we just had an assumption buried in the driver that we are
always highcapacity.

Export a knob to change this, so we can move the hardcoding from
driver to board code, where it's more prominent. This is done
in a follow-up commit.

Signed-off-by: Ahmad Fatoum 
---
 arch/arm/mach-at91/include/mach/xload.h | 3 +++
 drivers/mci/atmel-sdhci-pbl.c   | 8 +++-
 drivers/mci/atmel_mci_pbl.c | 8 +++-
 3 files changed, 17 insertions(+), 2 deletions(-)

diff --git a/arch/arm/mach-at91/include/mach/xload.h 
b/arch/arm/mach-at91/include/mach/xload.h
index bbc70af2108a..038f32554568 100644
--- a/arch/arm/mach-at91/include/mach/xload.h
+++ b/arch/arm/mach-at91/include/mach/xload.h
@@ -12,4 +12,7 @@ int at91_sdhci_bio_init(struct pbl_bio *bio, void __iomem 
*base);
 int at91_mci_bio_init(struct pbl_bio *bio, void __iomem *base,
  unsigned int clock, unsigned int slot);
 
+void at91_mci_set_highcapacity(struct pbl_bio *bio, bool highcap);
+void at91_sdhci_set_highcapacity(struct pbl_bio *bio, bool highcap);
+
 #endif /* __MACH_XLOAD_H */
diff --git a/drivers/mci/atmel-sdhci-pbl.c b/drivers/mci/atmel-sdhci-pbl.c
index 626e4008fe85..317f26f8af0d 100644
--- a/drivers/mci/atmel-sdhci-pbl.c
+++ b/drivers/mci/atmel-sdhci-pbl.c
@@ -99,6 +99,12 @@ static int at91_sdhci_bio_read(struct pbl_bio *bio, off_t 
start,
return blocks_done;
 }
 
+void at91_sdhci_set_highcapacity(struct pbl_bio *bio, bool highcap)
+{
+   struct at91_sdhci_priv *priv = bio->priv;
+   priv->highcapacity_card = highcap;
+}
+
 static struct at91_sdhci_priv atmel_sdcard;
 
 int at91_sdhci_bio_init(struct pbl_bio *bio, void __iomem *base)
@@ -122,7 +128,7 @@ int at91_sdhci_bio_init(struct pbl_bio *bio, void __iomem 
*base)
ret = at91_sdhci_set_ios(host, &ios);
 
 // FIXME can we determine this without leaving SD transfer mode?
-   priv->highcapacity_card = 1;
+   at91_sdhci_set_highcapacity(bio, true);
 
return 0;
 }
diff --git a/drivers/mci/atmel_mci_pbl.c b/drivers/mci/atmel_mci_pbl.c
index 767d6f3ce2d7..82e45fd4e89e 100644
--- a/drivers/mci/atmel_mci_pbl.c
+++ b/drivers/mci/atmel_mci_pbl.c
@@ -82,6 +82,12 @@ static int at91_mci_bio_read(struct pbl_bio *bio, off_t 
start,
return blocks_done;
 }
 
+void at91_mci_set_highcapacity(struct pbl_bio *bio, bool highcap)
+{
+   struct atmel_mci_priv *priv = bio->priv;
+   priv->highcapacity_card = highcap;
+}
+
 int at91_mci_bio_init(struct pbl_bio *bio, void __iomem *base,
  unsigned int clock, unsigned int slot)
 {
@@ -110,7 +116,7 @@ int at91_mci_bio_init(struct pbl_bio *bio, void __iomem 
*base,
 
atmci_common_set_ios(host, &ios);
 
-   priv->highcapacity_card = 1;
+   at91_mci_set_highcapacity(bio, true);
 
return 0;
 }
-- 
2.29.2


___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


[PATCH 3/3] ARM: at91: xload-mmc: add prominent note about PBL MMC limitation

2021-06-02 Thread Ahmad Fatoum
The PBL driver has an assumption about only being used with
high capacity cards. When using a default capacity card, the sama5d3
MCI controller hang when doing unaligned accesses.

This heuristic could add some support default capacity card support
without risking unaligned accesses:

  at91_mci_set_highcapacity(false);

  sector0 = read_sector(0);
  if (!is_mbr(sector0, &fatsect)
 abort();

  fatsector1 = read_sector(fatsect);
  at91_mci_set_highcapacity(false);

  if (is_fat(fatsect))
goto default_cap;
  else
goto high_cap;

This, of course, fails if fatsect * 512 on a high capacity card
happens to have a FAT signature. As it's unclear whether supporting
<= 2 GiB is worth the effort, hardcode it in xload-mmc.c and add
a note about the limitation to conserve future debugging effort.

Signed-off-by: Ahmad Fatoum 
---
 arch/arm/mach-at91/xload-mmc.c | 24 +++-
 1 file changed, 23 insertions(+), 1 deletion(-)

diff --git a/arch/arm/mach-at91/xload-mmc.c b/arch/arm/mach-at91/xload-mmc.c
index 1b641f3a47ac..89766cf6b910 100644
--- a/arch/arm/mach-at91/xload-mmc.c
+++ b/arch/arm/mach-at91/xload-mmc.c
@@ -76,8 +76,18 @@ void __noreturn sama5d2_sdhci_start_image(u32 r4)
if (ret)
goto out_panic;
 
-   /* TODO: eMMC boot partition handling: they are not FAT-formatted */
+   /*
+* There seems to be no definitive way to find out whether we are
+* connected to a default or high capacity card without resetting
+* the card. The PBL driver assumes however that the card is already
+* in transfer mode. For now assume all cards to be high capacity.
+* If support for cards smaller or equal to 2GiB becomes relevant,
+* this assumption can be revisited.
+*/
+   pr_debug("Assuming high capacity card\n");
+   at91_sdhci_set_highcapacity(&bio, true);
 
+   /* TODO: eMMC boot partition handling: they are not FAT-formatted */
at91_fat_start_image(&bio, buf, SZ_16M, r4);
 
 out_panic:
@@ -128,6 +138,18 @@ void __noreturn sama5d3_atmci_start_image(u32 boot_src, 
unsigned int clock,
if (ret)
goto out_panic;
 
+   /*
+* There seems to be no definitive way to find out whether we are
+* connected to a default or high capacity card without resetting
+* the card. The PBL driver assumes however that the card is already
+* in transfer mode. For now assume all cards to be high capacity.
+* If support for cards smaller or equal to 2GiB becomes relevant,
+* this assumption can be revisited.
+*/
+   pr_debug("Assuming high capacity card\n");
+   at91_mci_set_highcapacity(&bio, true);
+
+   /* TODO: eMMC boot partition handling: they are not FAT-formatted */
at91_fat_start_image(&bio, buf, SZ_16M, boot_src);
 
 out_panic:
-- 
2.29.2


___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


[PATCH 14/24] clk: Add Linux functions to register a divider

2021-06-02 Thread Sascha Hauer
Linux has clk_register_divider() and clk_register_divider_table(). Add
the same functions with the same prototypes for barebox to ease code
porting from Linux.

Signed-off-by: Sascha Hauer 
---
 drivers/clk/clk-divider.c | 19 +++
 include/linux/clk.h   |  9 +
 2 files changed, 28 insertions(+)

diff --git a/drivers/clk/clk-divider.c b/drivers/clk/clk-divider.c
index 447c43dc93..856b8a0648 100644
--- a/drivers/clk/clk-divider.c
+++ b/drivers/clk/clk-divider.c
@@ -409,3 +409,22 @@ struct clk *clk_divider_table(const char *name, const char 
*parent,
 
return &div->hw.clk;
 }
+
+struct clk *clk_register_divider_table(struct device_d *dev, const char *name,
+   const char *parent_name, unsigned long flags,
+   void __iomem *reg, u8 shift, u8 width,
+   u8 clk_divider_flags, const struct clk_div_table *table,
+   spinlock_t *lock)
+{
+   return clk_divider_table(name, parent_name, flags, reg, shift, width,
+table, clk_divider_flags);
+}
+
+struct clk *clk_register_divider(struct device_d *dev, const char *name,
+   const char *parent_name, unsigned long flags,
+   void __iomem *reg, u8 shift, u8 width,
+   u8 clk_divider_flags, spinlock_t *lock)
+{
+   return clk_divider(name, parent_name, flags, reg, shift, width,
+  clk_divider_flags);
+}
diff --git a/include/linux/clk.h b/include/linux/clk.h
index 9d09738dda..ba98c5307e 100644
--- a/include/linux/clk.h
+++ b/include/linux/clk.h
@@ -475,6 +475,10 @@ void clk_divider_free(struct clk *clk_divider);
 struct clk *clk_divider(const char *name, const char *parent,
unsigned clk_flags, void __iomem *reg, u8 shift,
u8 width, unsigned div_flags);
+struct clk *clk_register_divider(struct device_d *dev, const char *name,
+   const char *parent_name, unsigned long flags,
+   void __iomem *reg, u8 shift, u8 width,
+   u8 clk_divider_flags, spinlock_t *lock);
 struct clk *clk_divider_one_based(const char *name, const char *parent,
  unsigned clk_flags, void __iomem *reg,
  u8 shift, u8 width, unsigned div_flags);
@@ -482,6 +486,11 @@ struct clk *clk_divider_table(const char *name, const char 
*parent,
  unsigned clk_flags, void __iomem *reg, u8 shift,
  u8 width, const struct clk_div_table *table,
  unsigned div_flags);
+struct clk *clk_register_divider_table(struct device_d *dev, const char *name,
+   const char *parent_name, unsigned long flags,
+   void __iomem *reg, u8 shift, u8 width,
+   u8 clk_divider_flags, const struct clk_div_table *table,
+   spinlock_t *lock);
 
 struct clk_fixed_factor {
struct clk_hw hw;
-- 
2.29.2


___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


[PATCH 10/24] clk: move fixed_factor to include/linux/clk.h

2021-06-02 Thread Sascha Hauer
In Linux struct clk_fixed_factor is known to clk implementors. Do the
same in barebox.

Signed-off-by: Sascha Hauer 
---
 drivers/clk/clk-fixed-factor.c | 14 +-
 include/linux/clk.h| 15 +++
 2 files changed, 16 insertions(+), 13 deletions(-)

diff --git a/drivers/clk/clk-fixed-factor.c b/drivers/clk/clk-fixed-factor.c
index 506c4aea74..4f1a07c629 100644
--- a/drivers/clk/clk-fixed-factor.c
+++ b/drivers/clk/clk-fixed-factor.c
@@ -10,18 +10,6 @@
 #include 
 #include 
 
-struct clk_fixed_factor {
-   struct clk_hw hw;
-   int mult;
-   int div;
-   const char *parent;
-};
-
-static inline struct clk_fixed_factor *to_clk_fixed_factor(struct clk_hw *hw)
-{
-   return container_of(hw, struct clk_fixed_factor, hw);
-}
-
 static unsigned long clk_fixed_factor_recalc_rate(struct clk_hw *hw,
unsigned long parent_rate)
 {
@@ -59,7 +47,7 @@ static int clk_factor_set_rate(struct clk_hw *hw, unsigned 
long rate,
return 0;
 }
 
-static struct clk_ops clk_fixed_factor_ops = {
+struct clk_ops clk_fixed_factor_ops = {
.set_rate = clk_factor_set_rate,
.round_rate = clk_factor_round_rate,
.recalc_rate = clk_fixed_factor_recalc_rate,
diff --git a/include/linux/clk.h b/include/linux/clk.h
index edf64cd9aa..7140aa9509 100644
--- a/include/linux/clk.h
+++ b/include/linux/clk.h
@@ -480,6 +480,21 @@ struct clk *clk_divider_table(const char *name, const char 
*parent,
  unsigned clk_flags, void __iomem *reg, u8 shift,
  u8 width, const struct clk_div_table *table,
  unsigned div_flags);
+
+struct clk_fixed_factor {
+   struct clk_hw hw;
+   int mult;
+   int div;
+   const char *parent;
+};
+
+static inline struct clk_fixed_factor *to_clk_fixed_factor(struct clk_hw *hw)
+{
+   return container_of(hw, struct clk_fixed_factor, hw);
+}
+
+extern struct clk_ops clk_fixed_factor_ops;
+
 struct clk *clk_fixed_factor(const char *name,
const char *parent, unsigned int mult, unsigned int div,
unsigned flags);
-- 
2.29.2


___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


[PATCH 03/24] clk: rename clk_register() to bclk_register()

2021-06-02 Thread Sascha Hauer
Linux has a clk_register() function with a different semantics than
barebox. Rename this function to bclk_register() so that we later
can introduce a clk_register() function with the same semantics as
Linux.

Signed-off-by: Sascha Hauer 
Reviewed-by: Ahmad Fatoum 
---
 drivers/clk/at91/clk-audio-pll.c| 6 +++---
 drivers/clk/at91/clk-generated.c| 2 +-
 drivers/clk/at91/clk-h32mx.c| 2 +-
 drivers/clk/at91/clk-i2s-mux.c  | 2 +-
 drivers/clk/at91/clk-main.c | 8 
 drivers/clk/at91/clk-master.c   | 2 +-
 drivers/clk/at91/clk-peripheral.c   | 4 ++--
 drivers/clk/at91/clk-pll.c  | 2 +-
 drivers/clk/at91/clk-plldiv.c   | 2 +-
 drivers/clk/at91/clk-programmable.c | 2 +-
 drivers/clk/at91/clk-sam9x60-pll.c  | 2 +-
 drivers/clk/at91/clk-slow.c | 2 +-
 drivers/clk/at91/clk-smd.c  | 2 +-
 drivers/clk/at91/clk-system.c   | 2 +-
 drivers/clk/at91/clk-usb.c  | 6 +++---
 drivers/clk/at91/clk-utmi.c | 2 +-
 drivers/clk/at91/sckc.c | 8 
 drivers/clk/clk-ar933x.c| 2 +-
 drivers/clk/clk-ar9344.c| 2 +-
 drivers/clk/clk-composite.c | 2 +-
 drivers/clk/clk-divider.c   | 4 ++--
 drivers/clk/clk-fixed-factor.c  | 2 +-
 drivers/clk/clk-fixed.c | 2 +-
 drivers/clk/clk-fractional-divider.c| 2 +-
 drivers/clk/clk-gate-shared.c   | 2 +-
 drivers/clk/clk-gate.c  | 2 +-
 drivers/clk/clk-gpio.c  | 2 +-
 drivers/clk/clk-mux.c   | 2 +-
 drivers/clk/clk-qoric.c | 2 +-
 drivers/clk/clk-stm32mp1.c  | 6 +++---
 drivers/clk/clk.c   | 2 +-
 drivers/clk/imx/clk-cpu.c   | 2 +-
 drivers/clk/imx/clk-frac-pll.c  | 2 +-
 drivers/clk/imx/clk-gate-exclusive.c| 2 +-
 drivers/clk/imx/clk-gate2.c | 2 +-
 drivers/clk/imx/clk-pfd.c   | 2 +-
 drivers/clk/imx/clk-pll14xx.c   | 2 +-
 drivers/clk/imx/clk-pllv1.c | 2 +-
 drivers/clk/imx/clk-pllv2.c | 2 +-
 drivers/clk/imx/clk-pllv3.c | 2 +-
 drivers/clk/imx/clk-sccg-pll.c  | 2 +-
 drivers/clk/loongson/clk-ls1b200.c  | 2 +-
 drivers/clk/mvebu/corediv.c | 2 +-
 drivers/clk/mxs/clk-div.c   | 2 +-
 drivers/clk/mxs/clk-frac.c  | 2 +-
 drivers/clk/mxs/clk-lcdif.c | 2 +-
 drivers/clk/mxs/clk-pll.c   | 2 +-
 drivers/clk/mxs/clk-ref.c   | 2 +-
 drivers/clk/rockchip/clk-cpu.c  | 2 +-
 drivers/clk/rockchip/clk-pll.c  | 4 ++--
 drivers/clk/socfpga/clk-gate-a10.c  | 2 +-
 drivers/clk/socfpga/clk-periph-a10.c| 2 +-
 drivers/clk/socfpga/clk-pll-a10.c   | 2 +-
 drivers/clk/socfpga/clk.c   | 6 +++---
 drivers/clk/tegra/clk-divider.c | 2 +-
 drivers/clk/tegra/clk-periph.c  | 2 +-
 drivers/clk/tegra/clk-pll-out.c | 2 +-
 drivers/clk/tegra/clk-pll.c | 2 +-
 drivers/clk/vexpress/clk-sp810.c| 2 +-
 drivers/clk/zynq/clkc.c | 8 
 drivers/clk/zynqmp/clk-divider-zynqmp.c | 2 +-
 drivers/clk/zynqmp/clk-gate-zynqmp.c| 2 +-
 drivers/clk/zynqmp/clk-mux-zynqmp.c | 2 +-
 drivers/clk/zynqmp/clk-pll-zynqmp.c | 2 +-
 drivers/video/imx-ipu-v3/ipu-di.c   | 2 +-
 include/linux/clk.h | 2 +-
 66 files changed, 86 insertions(+), 86 deletions(-)

diff --git a/drivers/clk/at91/clk-audio-pll.c b/drivers/clk/at91/clk-audio-pll.c
index 47bff32fe8..25be69ec6f 100644
--- a/drivers/clk/at91/clk-audio-pll.c
+++ b/drivers/clk/at91/clk-audio-pll.c
@@ -442,7 +442,7 @@ at91_clk_register_audio_pll_frac(struct regmap *regmap, 
const char *name,
 
frac_ck->regmap = regmap;
 
-   ret = clk_register(&frac_ck->clk);
+   ret = bclk_register(&frac_ck->clk);
if (ret) {
kfree(frac_ck);
return ERR_PTR(ret);
@@ -472,7 +472,7 @@ at91_clk_register_audio_pll_pad(struct regmap *regmap, 
const char *name,
 
apad_ck->regmap = regmap;
 
-   ret = clk_register(&apad_ck->clk);
+   ret = bclk_register(&apad_ck->clk);
if (ret) {
kfree(apad_ck);
return ERR_PTR(ret);
@@ -502,7 +502,7 @@ at91_clk_register_audio_pll_pmc(struct regmap *regmap, 
const char *name,
 
apmc_ck->regmap = regmap;
 
-   ret = clk_register(&apmc_ck->clk);
+   ret = bclk_register(&apmc_ck->clk);
if (ret) {
kfree(apmc_ck);
return ERR_PTR(ret);
diff --git a/drivers/clk/at91/clk-generated.c b/drivers/clk/at91/clk-generated.c
index 56b800facb..23d193f9dd 100644
--- a/drivers/clk/at91/clk-generated.c
+++ b/drivers/clk/at91/clk-generated.c
@@ -191,7 +191,7 @@ at91_clk_register_generated(struct regmap *regmap,
 
clk_generated_startup(gck);
hw = &gck->hw;
-   ret = clk_regist

[PATCH 19/24] clk: Rename CLK_GATE_INVERTED to CLK_GATE_SET_TO_DISABLE

2021-06-02 Thread Sascha Hauer
Rename CLK_GATE_INVERTED to CLK_GATE_SET_TO_DISABLE as done in Linux.

Signed-off-by: Sascha Hauer 
---
 drivers/clk/clk-gate.c  | 8 
 drivers/clk/imx/clk-gate2.c | 9 -
 include/linux/clk.h | 2 +-
 3 files changed, 9 insertions(+), 10 deletions(-)

diff --git a/drivers/clk/clk-gate.c b/drivers/clk/clk-gate.c
index 87b7e73aa6..3cfd707238 100644
--- a/drivers/clk/clk-gate.c
+++ b/drivers/clk/clk-gate.c
@@ -13,7 +13,7 @@
 static void clk_gate_endisable(struct clk_hw *hw, int enable)
 {
struct clk_gate *gate = container_of(hw, struct clk_gate, hw);
-   int set = gate->flags & CLK_GATE_INVERTED ? 1 : 0;
+   int set = gate->flags & CLK_GATE_SET_TO_DISABLE ? 1 : 0;
u32 val;
 
set ^= enable;
@@ -54,9 +54,9 @@ int clk_gate_is_enabled(struct clk_hw *hw)
val = readl(g->reg);
 
if (val & (1 << g->shift))
-   return g->flags & CLK_GATE_INVERTED ? 0 : 1;
+   return g->flags & CLK_GATE_SET_TO_DISABLE ? 0 : 1;
else
-   return g->flags & CLK_GATE_INVERTED ? 1 : 0;
+   return g->flags & CLK_GATE_SET_TO_DISABLE ? 1 : 0;
 }
 
 struct clk_ops clk_gate_ops = {
@@ -114,7 +114,7 @@ struct clk *clk_gate(const char *name, const char *parent, 
void __iomem *reg,
 struct clk *clk_gate_inverted(const char *name, const char *parent,
void __iomem *reg, u8 shift, unsigned flags)
 {
-   return clk_gate(name, parent, reg, shift, flags, CLK_GATE_INVERTED);
+   return clk_gate(name, parent, reg, shift, flags, 
CLK_GATE_SET_TO_DISABLE);
 }
 
 struct clk *clk_register_gate(struct device_d *dev, const char *name,
diff --git a/drivers/clk/imx/clk-gate2.c b/drivers/clk/imx/clk-gate2.c
index 8866a5365b..af83e93b12 100644
--- a/drivers/clk/imx/clk-gate2.c
+++ b/drivers/clk/imx/clk-gate2.c
@@ -18,7 +18,6 @@ struct clk_gate2 {
int shift;
u8 cgr_val;
const char *parent;
-#define CLK_GATE_INVERTED  (1 << 0)
unsigned flags;
 };
 
@@ -34,7 +33,7 @@ static int clk_gate2_enable(struct clk_hw *hw)
 
val = readl(g->reg);
 
-   if (g->flags & CLK_GATE_INVERTED)
+   if (g->flags & CLK_GATE_SET_TO_DISABLE)
val &= ~(3 << g->shift);
else
val |= g->cgr_val << g->shift;
@@ -51,7 +50,7 @@ static void clk_gate2_disable(struct clk_hw *hw)
 
val = readl(g->reg);
 
-   if (g->flags & CLK_GATE_INVERTED)
+   if (g->flags & CLK_GATE_SET_TO_DISABLE)
val |= 3 << g->shift;
else
val &= ~(3 << g->shift);
@@ -67,9 +66,9 @@ static int clk_gate2_is_enabled(struct clk_hw *hw)
val = readl(g->reg);
 
if (val & (1 << g->shift))
-   return g->flags & CLK_GATE_INVERTED ? 0 : 1;
+   return g->flags & CLK_GATE_SET_TO_DISABLE ? 0 : 1;
else
-   return g->flags & CLK_GATE_INVERTED ? 1 : 0;
+   return g->flags & CLK_GATE_SET_TO_DISABLE ? 1 : 0;
 }
 
 static struct clk_ops clk_gate2_ops = {
diff --git a/include/linux/clk.h b/include/linux/clk.h
index c49604fe2d..d53699a874 100644
--- a/include/linux/clk.h
+++ b/include/linux/clk.h
@@ -339,7 +339,7 @@ static inline void clk_put(struct clk *clk)
 /* parents need enable during gate/ungate, set rate and re-parent */
 #define CLK_OPS_PARENT_ENABLE   (1 << 12)
 
-#define CLK_GATE_INVERTED  (1 << 0)
+#define CLK_GATE_SET_TO_DISABLE(1 << 0)
 #define CLK_GATE_HIWORD_MASK   (1 << 1)
 
 struct clk_ops {
-- 
2.29.2


___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


[PATCH 12/24] clk: Update fractional divider from Linux

2021-06-02 Thread Sascha Hauer
This updates the fractional divider implementation from Linux-5.12.

Signed-off-by: Sascha Hauer 
---
 drivers/clk/clk-fractional-divider.c | 111 +++
 include/linux/clk.h  |  44 +++
 2 files changed, 122 insertions(+), 33 deletions(-)

diff --git a/drivers/clk/clk-fractional-divider.c 
b/drivers/clk/clk-fractional-divider.c
index 65abf84b40..3d96360025 100644
--- a/drivers/clk/clk-fractional-divider.c
+++ b/drivers/clk/clk-fractional-divider.c
@@ -1,86 +1,129 @@
-// SPDX-License-Identifier: GPL-2.0-only
+// SPDX-License-Identifier: GPL-2.0
 /*
  * Copyright (C) 2014 Intel Corporation
  *
  * Adjustable fractional divider clock implementation.
  * Output rate = (m / n) * parent_rate.
+ * Uses rational best approximation algorithm.
  */
 
 #include 
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
+#include 
 #include 
 
-#define to_clk_fd(_hw) container_of(_hw, struct clk_fractional_divider, hw)
+static inline u32 clk_fd_readl(struct clk_fractional_divider *fd)
+{
+   if (fd->flags & CLK_FRAC_DIVIDER_BIG_ENDIAN)
+   return ioread32be(fd->reg);
 
-struct clk_fractional_divider {
-   struct clk_hw   hw;
-   void __iomem*reg;
-   u8  mshift;
-   u32 mmask;
-   u8  nshift;
-   u32 nmask;
-   u8  flags;
-};
+   return readl(fd->reg);
+}
+
+static inline void clk_fd_writel(struct clk_fractional_divider *fd, u32 val)
+{
+   if (fd->flags & CLK_FRAC_DIVIDER_BIG_ENDIAN)
+   iowrite32be(val, fd->reg);
+   else
+   writel(val, fd->reg);
+}
 
 static unsigned long clk_fd_recalc_rate(struct clk_hw *hw,
unsigned long parent_rate)
 {
struct clk_fractional_divider *fd = to_clk_fd(hw);
-   u32 val, m, n;
+   unsigned long m, n;
+   u32 val;
u64 ret;
 
-   val = readl(fd->reg);
+   val = clk_fd_readl(fd);
 
m = (val & fd->mmask) >> fd->mshift;
n = (val & fd->nmask) >> fd->nshift;
 
+   if (fd->flags & CLK_FRAC_DIVIDER_ZERO_BASED) {
+   m++;
+   n++;
+   }
+
+   if (!n || !m)
+   return parent_rate;
+
ret = (u64)parent_rate * m;
do_div(ret, n);
 
return ret;
 }
 
+static void clk_fd_general_approximation(struct clk_hw *hw, unsigned long rate,
+unsigned long *parent_rate,
+unsigned long *m, unsigned long *n)
+{
+   struct clk_fractional_divider *fd = to_clk_fd(hw);
+   unsigned long scale;
+
+   /*
+* Get rate closer to *parent_rate to guarantee there is no overflow
+* for m and n. In the result it will be the nearest rate left shifted
+* by (scale - fd->nwidth) bits.
+*/
+   scale = fls_long(*parent_rate / rate - 1);
+   if (scale > fd->nwidth)
+   rate <<= scale - fd->nwidth;
+
+   rational_best_approximation(rate, *parent_rate,
+   GENMASK(fd->mwidth - 1, 0), GENMASK(fd->nwidth - 1, 0),
+   m, n);
+}
+
 static long clk_fd_round_rate(struct clk_hw *hw, unsigned long rate,
- unsigned long *prate)
+ unsigned long *parent_rate)
 {
+   struct clk *clk = clk_hw_to_clk(hw);
struct clk_fractional_divider *fd = to_clk_fd(hw);
-   unsigned maxn = (fd->nmask >> fd->nshift) + 1;
-   unsigned div;
+   unsigned long m, n;
+   u64 ret;
 
-   if (!rate || rate >= *prate)
-   return *prate;
+   if (!rate || (!(clk->flags & CLK_SET_RATE_PARENT) && rate >= 
*parent_rate))
+   return *parent_rate;
 
-   div = gcd(*prate, rate);
+   if (fd->approximation)
+   fd->approximation(hw, rate, parent_rate, &m, &n);
+   else
+   clk_fd_general_approximation(hw, rate, parent_rate, &m, &n);
 
-   while ((*prate / div) > maxn) {
-   div <<= 1;
-   rate <<= 1;
-   }
+   ret = (u64)*parent_rate * m;
+   do_div(ret, n);
 
-   return rate;
+   return ret;
 }
 
 static int clk_fd_set_rate(struct clk_hw *hw, unsigned long rate,
   unsigned long parent_rate)
 {
struct clk_fractional_divider *fd = to_clk_fd(hw);
-   unsigned long div;
-   unsigned n, m;
+   unsigned long m, n;
u32 val;
 
-   div = gcd(parent_rate, rate);
-   m = rate / div;
-   n = parent_rate / div;
+   rational_best_approximation(rate, parent_rate,
+   GENMASK(fd->mwidth - 1, 0), GENMASK(fd->nwidth - 1, 0),
+   &m, &n);
+
+   if (fd->flags & CLK_FRAC_DIVIDER_ZERO_BASED) {
+   m--;
+   n--;
+   }
 
-   val = readl(fd->reg);
+   val = clk_fd_readl(fd);
val &= ~(fd->mmask | fd->nmask);
 

[PATCH 23/24] clk: rockchip: Update to current Linux

2021-06-02 Thread Sascha Hauer
This updates the Rockchip clk code to Linux-5.13-rc1. The code is
unchanged as much as possible. The pre- and post- change hooks are
removed and also the notifier blocks.

Signed-off-by: Sascha Hauer 
---
 drivers/clk/rockchip/Makefile|   2 +-
 drivers/clk/rockchip/clk-cpu.c   |  80 ++-
 drivers/clk/rockchip/clk-inverter.c  | 107 
 drivers/clk/rockchip/clk-mmc-phase.c | 192 ++
 drivers/clk/rockchip/clk-muxgrf.c|  95 +++
 drivers/clk/rockchip/clk-pll.c   | 839 +++
 drivers/clk/rockchip/clk-rk3188.c| 363 ++--
 drivers/clk/rockchip/clk-rk3288.c| 309 ++
 drivers/clk/rockchip/clk.c   | 489 
 drivers/clk/rockchip/clk.h   | 500 ++--
 10 files changed, 2396 insertions(+), 580 deletions(-)
 create mode 100644 drivers/clk/rockchip/clk-inverter.c
 create mode 100644 drivers/clk/rockchip/clk-mmc-phase.c
 create mode 100644 drivers/clk/rockchip/clk-muxgrf.c

diff --git a/drivers/clk/rockchip/Makefile b/drivers/clk/rockchip/Makefile
index 5fcf0c2515..9964b331f2 100644
--- a/drivers/clk/rockchip/Makefile
+++ b/drivers/clk/rockchip/Makefile
@@ -1,4 +1,4 @@
 # SPDX-License-Identifier: GPL-2.0-only
-obj-y += clk-cpu.o clk-pll.o clk.o
+obj-y += clk-cpu.o clk-pll.o clk.o clk-muxgrf.o clk-mmc-phase.o clk-inverter.o
 obj-$(CONFIG_ARCH_RK3188) += clk-rk3188.o
 obj-$(CONFIG_ARCH_RK3288) += clk-rk3288.o
diff --git a/drivers/clk/rockchip/clk-cpu.c b/drivers/clk/rockchip/clk-cpu.c
index 88564872f5..8b5d4a0330 100644
--- a/drivers/clk/rockchip/clk-cpu.c
+++ b/drivers/clk/rockchip/clk-cpu.c
@@ -34,8 +34,10 @@
 #include 
 #include 
 #include 
-#include "clk.h"
 #include 
+#include 
+#include 
+#include "clk.h"
 
 /**
  * struct rockchip_cpuclk: information about clock supplied to a CPU core.
@@ -43,31 +45,34 @@
  * @alt_parent:alternate parent clock to use when switching the speed
  * of the primary parent clock.
  * @reg_base:  base register for cpu-clock values.
+ * @clk_nb:clock notifier registered for changes in clock speed of the
+ * primary parent clock.
  * @rate_count:number of rates in the rate_table
  * @rate_table:pll-rates and their associated dividers
  * @reg_data:  cpu-specific register settings
+ * @lock:  clock lock
  */
 struct rockchip_cpuclk {
struct clk_hw   hw;
-
struct clk  *alt_parent;
void __iomem*reg_base;
unsigned intrate_count;
struct rockchip_cpuclk_rate_table   *rate_table;
const struct rockchip_cpuclk_reg_data   *reg_data;
+   spinlock_t  *lock;
 };
 
-#define to_rockchip_cpuclk_hw(_hw) container_of(_hw, struct rockchip_cpuclk, 
hw)
+#define to_rockchip_cpuclk_hw(hw) container_of(hw, struct rockchip_cpuclk, hw)
 
 static unsigned long rockchip_cpuclk_recalc_rate(struct clk_hw *hw,
unsigned long parent_rate)
 {
struct rockchip_cpuclk *cpuclk = to_rockchip_cpuclk_hw(hw);
const struct rockchip_cpuclk_reg_data *reg_data = cpuclk->reg_data;
-   u32 clksel0 = readl(cpuclk->reg_base + reg_data->core_reg);
+   u32 clksel0 = readl_relaxed(cpuclk->reg_base + reg_data->core_reg[0]);
 
-   clksel0 >>= reg_data->div_core_shift;
-   clksel0 &= reg_data->div_core_mask;
+   clksel0 >>= reg_data->div_core_shift[0];
+   clksel0 &= reg_data->div_core_mask[0];
return parent_rate / (clksel0 + 1);
 }
 
@@ -76,17 +81,18 @@ static const struct clk_ops rockchip_cpuclk_ops = {
 };
 
 struct clk *rockchip_clk_register_cpuclk(const char *name,
-   const char **parent_names, u8 num_parents,
+   const char *const *parent_names, u8 num_parents,
const struct rockchip_cpuclk_reg_data *reg_data,
const struct rockchip_cpuclk_rate_table *rates,
-   int nrates, void __iomem *reg_base)
+   int nrates, void __iomem *reg_base, spinlock_t *lock)
 {
struct rockchip_cpuclk *cpuclk;
-   struct clk *clk;
+   struct clk_init_data init;
+   struct clk *clk, *cclk;
int ret;
 
-   if (num_parents != 2) {
-   pr_err("%s: needs two parent clocks\n", __func__);
+   if (num_parents < 2) {
+   pr_err("%s: needs at least two parent clocks\n", __func__);
return ERR_PTR(-EINVAL);
}
 
@@ -94,21 +100,28 @@ struct clk *rockchip_clk_register_cpuclk(const char *name,
if (!cpuclk)
return ERR_PTR(-ENOMEM);
 
-   cpuclk->hw.clk.name = name;
-   cpuclk->hw.clk.parent_names = &parent_names[0];
-   cpuclk->hw.clk.num_parents = 1;
-   cpuclk->hw.clk.ops = &rockchip_cpuclk_ops;
+   init.name = name;
+   init.parent_names = &parent_names[reg_data->mux_core_main];
+   i

[PATCH 11/24] Add rational_best_approximation()

2021-06-02 Thread Sascha Hauer
Import rational_best_approximation() from Linux. This is used by an
upcoming update of the clk_fractional_divider code.

Signed-off-by: Sascha Hauer 
Reviewed-by: Ahmad Fatoum 
---
 include/linux/rational.h |  20 
 lib/math/Makefile|   1 +
 lib/math/rational.c  | 100 +++
 3 files changed, 121 insertions(+)
 create mode 100644 include/linux/rational.h
 create mode 100644 lib/math/rational.c

diff --git a/include/linux/rational.h b/include/linux/rational.h
new file mode 100644
index 00..33f5f5fc3e
--- /dev/null
+++ b/include/linux/rational.h
@@ -0,0 +1,20 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * rational fractions
+ *
+ * Copyright (C) 2009 emlix GmbH, Oskar Schirmer 
+ *
+ * helper functions when coping with rational numbers,
+ * e.g. when calculating optimum numerator/denominator pairs for
+ * pll configuration taking into account restricted register size
+ */
+
+#ifndef _LINUX_RATIONAL_H
+#define _LINUX_RATIONAL_H
+
+void rational_best_approximation(
+   unsigned long given_numerator, unsigned long given_denominator,
+   unsigned long max_numerator, unsigned long max_denominator,
+   unsigned long *best_numerator, unsigned long *best_denominator);
+
+#endif /* _LINUX_RATIONAL_H */
diff --git a/lib/math/Makefile b/lib/math/Makefile
index c2c892dd55..756d7dd90d 100644
--- a/lib/math/Makefile
+++ b/lib/math/Makefile
@@ -1,2 +1,3 @@
 obj-y += div64.o
 pbl-y += div64.o
+obj-y += rational.o
diff --git a/lib/math/rational.c b/lib/math/rational.c
new file mode 100644
index 00..e5367e6a8a
--- /dev/null
+++ b/lib/math/rational.c
@@ -0,0 +1,100 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * rational fractions
+ *
+ * Copyright (C) 2009 emlix GmbH, Oskar Schirmer 
+ * Copyright (C) 2019 Trent Piepho 
+ *
+ * helper functions when coping with rational numbers
+ */
+
+#include 
+#include 
+#include 
+#include 
+
+/*
+ * calculate best rational approximation for a given fraction
+ * taking into account restricted register size, e.g. to find
+ * appropriate values for a pll with 5 bit denominator and
+ * 8 bit numerator register fields, trying to set up with a
+ * frequency ratio of 3.1415, one would say:
+ *
+ * rational_best_approximation(31415, 1,
+ * (1 << 8) - 1, (1 << 5) - 1, &n, &d);
+ *
+ * you may look at given_numerator as a fixed point number,
+ * with the fractional part size described in given_denominator.
+ *
+ * for theoretical background, see:
+ * https://en.wikipedia.org/wiki/Continued_fraction
+ */
+
+void rational_best_approximation(
+   unsigned long given_numerator, unsigned long given_denominator,
+   unsigned long max_numerator, unsigned long max_denominator,
+   unsigned long *best_numerator, unsigned long *best_denominator)
+{
+   /* n/d is the starting rational, which is continually
+* decreased each iteration using the Euclidean algorithm.
+*
+* dp is the value of d from the prior iteration.
+*
+* n2/d2, n1/d1, and n0/d0 are our successively more accurate
+* approximations of the rational.  They are, respectively,
+* the current, previous, and two prior iterations of it.
+*
+* a is current term of the continued fraction.
+*/
+   unsigned long n, d, n0, d0, n1, d1, n2, d2;
+   n = given_numerator;
+   d = given_denominator;
+   n0 = d1 = 0;
+   n1 = d0 = 1;
+
+   for (;;) {
+   unsigned long dp, a;
+
+   if (d == 0)
+   break;
+   /* Find next term in continued fraction, 'a', via
+* Euclidean algorithm.
+*/
+   dp = d;
+   a = n / d;
+   d = n % d;
+   n = dp;
+
+   /* Calculate the current rational approximation (aka
+* convergent), n2/d2, using the term just found and
+* the two prior approximations.
+*/
+   n2 = n0 + a * n1;
+   d2 = d0 + a * d1;
+
+   /* If the current convergent exceeds the maxes, then
+* return either the previous convergent or the
+* largest semi-convergent, the final term of which is
+* found below as 't'.
+*/
+   if ((n2 > max_numerator) || (d2 > max_denominator)) {
+   unsigned long t = min((max_numerator - n0) / n1,
+ (max_denominator - d0) / d1);
+
+   /* This tests if the semi-convergent is closer
+* than the previous convergent.
+*/
+   if (2u * t > a || (2u * t == a && d0 * dp > d1 * d)) {
+   n1 = n0 + t * n1;
+   d1 = d0 + t * d1;
+   }
+   break;
+   }
+   n0 = n1;

[PATCH 24/24] clk: Rockchip: Add rk3568 clk support

2021-06-02 Thread Sascha Hauer
This adds clock support for the Rockchip rk3568 SoC. Based on the
corresponding Linux patch merged into v5.13-rc1.

Signed-off-by: Sascha Hauer 
---
 drivers/clk/rockchip/Makefile |1 +
 drivers/clk/rockchip/clk-rk3568.c | 1704 +
 2 files changed, 1705 insertions(+)
 create mode 100644 drivers/clk/rockchip/clk-rk3568.c

diff --git a/drivers/clk/rockchip/Makefile b/drivers/clk/rockchip/Makefile
index 9964b331f2..4c387b3a89 100644
--- a/drivers/clk/rockchip/Makefile
+++ b/drivers/clk/rockchip/Makefile
@@ -2,3 +2,4 @@
 obj-y += clk-cpu.o clk-pll.o clk.o clk-muxgrf.o clk-mmc-phase.o clk-inverter.o
 obj-$(CONFIG_ARCH_RK3188) += clk-rk3188.o
 obj-$(CONFIG_ARCH_RK3288) += clk-rk3288.o
+obj-$(CONFIG_ARCH_RK3568) += clk-rk3568.o
diff --git a/drivers/clk/rockchip/clk-rk3568.c 
b/drivers/clk/rockchip/clk-rk3568.c
new file mode 100644
index 00..d151437c12
--- /dev/null
+++ b/drivers/clk/rockchip/clk-rk3568.c
@@ -0,0 +1,1704 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (c) 2021 Rockchip Electronics Co. Ltd.
+ * Author: Elaine Zhang 
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include "clk.h"
+
+#define RK3568_GRF_SOC_STATUS0 0x580
+
+enum rk3568_pmu_plls {
+   ppll, hpll,
+};
+
+enum rk3568_plls {
+   apll, dpll, gpll, cpll, npll, vpll,
+};
+
+static struct rockchip_pll_rate_table rk3568_pll_rates[] = {
+   /* _mhz, _refdiv, _fbdiv, _postdiv1, _postdiv2, _dsmpd, _frac */
+   RK3036_PLL_RATE(220800, 1, 92, 1, 1, 1, 0),
+   RK3036_PLL_RATE(218400, 1, 91, 1, 1, 1, 0),
+   RK3036_PLL_RATE(216000, 1, 90, 1, 1, 1, 0),
+   RK3036_PLL_RATE(208800, 1, 87, 1, 1, 1, 0),
+   RK3036_PLL_RATE(206400, 1, 86, 1, 1, 1, 0),
+   RK3036_PLL_RATE(204000, 1, 85, 1, 1, 1, 0),
+   RK3036_PLL_RATE(201600, 1, 84, 1, 1, 1, 0),
+   RK3036_PLL_RATE(199200, 1, 83, 1, 1, 1, 0),
+   RK3036_PLL_RATE(192000, 1, 80, 1, 1, 1, 0),
+   RK3036_PLL_RATE(189600, 1, 79, 1, 1, 1, 0),
+   RK3036_PLL_RATE(18, 1, 75, 1, 1, 1, 0),
+   RK3036_PLL_RATE(170400, 1, 71, 1, 1, 1, 0),
+   RK3036_PLL_RATE(160800, 1, 67, 1, 1, 1, 0),
+   RK3036_PLL_RATE(16, 3, 200, 1, 1, 1, 0),
+   RK3036_PLL_RATE(158400, 1, 132, 2, 1, 1, 0),
+   RK3036_PLL_RATE(156000, 1, 130, 2, 1, 1, 0),
+   RK3036_PLL_RATE(153600, 1, 128, 2, 1, 1, 0),
+   RK3036_PLL_RATE(151200, 1, 126, 2, 1, 1, 0),
+   RK3036_PLL_RATE(148800, 1, 124, 2, 1, 1, 0),
+   RK3036_PLL_RATE(146400, 1, 122, 2, 1, 1, 0),
+   RK3036_PLL_RATE(144000, 1, 120, 2, 1, 1, 0),
+   RK3036_PLL_RATE(141600, 1, 118, 2, 1, 1, 0),
+   RK3036_PLL_RATE(14, 3, 350, 2, 1, 1, 0),
+   RK3036_PLL_RATE(139200, 1, 116, 2, 1, 1, 0),
+   RK3036_PLL_RATE(136800, 1, 114, 2, 1, 1, 0),
+   RK3036_PLL_RATE(134400, 1, 112, 2, 1, 1, 0),
+   RK3036_PLL_RATE(132000, 1, 110, 2, 1, 1, 0),
+   RK3036_PLL_RATE(129600, 1, 108, 2, 1, 1, 0),
+   RK3036_PLL_RATE(127200, 1, 106, 2, 1, 1, 0),
+   RK3036_PLL_RATE(124800, 1, 104, 2, 1, 1, 0),
+   RK3036_PLL_RATE(12, 1, 100, 2, 1, 1, 0),
+   RK3036_PLL_RATE(118800, 1, 99, 2, 1, 1, 0),
+   RK3036_PLL_RATE(110400, 1, 92, 2, 1, 1, 0),
+   RK3036_PLL_RATE(11, 3, 275, 2, 1, 1, 0),
+   RK3036_PLL_RATE(100800, 1, 84, 2, 1, 1, 0),
+   RK3036_PLL_RATE(10, 3, 250, 2, 1, 1, 0),
+   RK3036_PLL_RATE(91200, 1, 76, 2, 1, 1, 0),
+   RK3036_PLL_RATE(81600, 1, 68, 2, 1, 1, 0),
+   RK3036_PLL_RATE(8, 3, 200, 2, 1, 1, 0),
+   RK3036_PLL_RATE(7, 3, 350, 4, 1, 1, 0),
+   RK3036_PLL_RATE(69600, 1, 116, 4, 1, 1, 0),
+   RK3036_PLL_RATE(6, 1, 100, 4, 1, 1, 0),
+   RK3036_PLL_RATE(59400, 1, 99, 4, 1, 1, 0),
+   RK3036_PLL_RATE(5, 1, 125, 6, 1, 1, 0),
+   RK3036_PLL_RATE(40800, 1, 68, 2, 2, 1, 0),
+   RK3036_PLL_RATE(31200, 1, 78, 6, 1, 1, 0),
+   RK3036_PLL_RATE(21600, 1, 72, 4, 2, 1, 0),
+   RK3036_PLL_RATE(2, 1, 100, 3, 4, 1, 0),
+   RK3036_PLL_RATE(14850, 1, 99, 4, 4, 1, 0),
+   RK3036_PLL_RATE(1, 1, 150, 6, 6, 1, 0),
+   RK3036_PLL_RATE(9600, 1, 96, 6, 4, 1, 0),
+   RK3036_PLL_RATE(7425, 2, 99, 4, 4, 1, 0),
+   { /* sentinel */ },
+};
+
+#define RK3568_DIV_ATCLK_CORE_MASK 0x1f
+#define RK3568_DIV_ATCLK_CORE_SHIFT0
+#define RK3568_DIV_GICCLK_CORE_MASK0x1f
+#define RK3568_DIV_GICCLK_CORE_SHIFT   8
+#define RK3568_DIV_PCLK_CORE_MASK  0x1f
+#define RK3568_DIV_PCLK_CORE_SHIFT 0
+#define RK3568_DIV_PERIPHCLK_CORE_MASK 0x1f
+#define RK3568_DIV_PERIPHCLK_CORE_SHIFT8
+#define RK3568_DIV_ACLK_CORE_MASK  0x1f
+#define RK3568_DIV_ACLK_CORE_SHIFT 8
+
+#define RK3568_DIV_SCLK_CORE_MASK  0xf
+#define RK3568_DIV_SCLK_CORE_SHIFT 0

[PATCH 16/24] clk: Add Linux functions to register a gate

2021-06-02 Thread Sascha Hauer
Linux has clk_register_gate(). Add the same function with the same
prototype for barebox to ease code porting from Linux.

Signed-off-by: Sascha Hauer 
---
 drivers/clk/clk-gate.c | 8 
 include/linux/clk.h| 4 
 2 files changed, 12 insertions(+)

diff --git a/drivers/clk/clk-gate.c b/drivers/clk/clk-gate.c
index 6e1bf0b316..87b7e73aa6 100644
--- a/drivers/clk/clk-gate.c
+++ b/drivers/clk/clk-gate.c
@@ -116,3 +116,11 @@ struct clk *clk_gate_inverted(const char *name, const char 
*parent,
 {
return clk_gate(name, parent, reg, shift, flags, CLK_GATE_INVERTED);
 }
+
+struct clk *clk_register_gate(struct device_d *dev, const char *name,
+   const char *parent_name, unsigned long flags,
+   void __iomem *reg, u8 bit_idx,
+   u8 clk_gate_flags, spinlock_t *lock)
+{
+   return clk_gate(name, parent_name, reg, bit_idx, flags, clk_gate_flags);
+}
diff --git a/include/linux/clk.h b/include/linux/clk.h
index 3bb7efdc61..950fecd7e0 100644
--- a/include/linux/clk.h
+++ b/include/linux/clk.h
@@ -615,6 +615,10 @@ struct clk *clk_gate_inverted(const char *name, const char 
*parent, void __iomem
u8 shift, unsigned flags);
 struct clk *clk_gate_shared(const char *name, const char *parent, const char 
*shared,
unsigned flags);
+struct clk *clk_register_gate(struct device_d *dev, const char *name,
+   const char *parent_name, unsigned long flags,
+   void __iomem *reg, u8 bit_idx,
+   u8 clk_gate_flags, spinlock_t *lock);
 
 int clk_is_enabled(struct clk *clk);
 int clk_hw_is_enabled(struct clk_hw *hw);
-- 
2.29.2


___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


[PATCH 15/24] clk: Add Linux functions to register a fixed factor clock

2021-06-02 Thread Sascha Hauer
Linux has clk_register_fixed_factor(). Add the same function with the same
prototype for barebox to ease code porting from Linux.

Signed-off-by: Sascha Hauer 
---
 drivers/clk/clk-fixed-factor.c | 7 +++
 include/linux/clk.h| 3 +++
 2 files changed, 10 insertions(+)

diff --git a/drivers/clk/clk-fixed-factor.c b/drivers/clk/clk-fixed-factor.c
index 4f1a07c629..fd4a3805f1 100644
--- a/drivers/clk/clk-fixed-factor.c
+++ b/drivers/clk/clk-fixed-factor.c
@@ -77,6 +77,13 @@ struct clk *clk_fixed_factor(const char *name,
return &f->hw.clk;
 }
 
+struct clk *clk_register_fixed_factor(struct device_d *dev, const char *name,
+   const char *parent_name, unsigned long flags,
+   unsigned int mult, unsigned int div)
+{
+   return clk_fixed_factor(name, parent_name, mult, div, flags);
+}
+
 /**
  * of_fixed_factor_clk_setup() - Setup function for simple fixed factor clock
  */
diff --git a/include/linux/clk.h b/include/linux/clk.h
index ba98c5307e..3bb7efdc61 100644
--- a/include/linux/clk.h
+++ b/include/linux/clk.h
@@ -509,6 +509,9 @@ extern struct clk_ops clk_fixed_factor_ops;
 struct clk *clk_fixed_factor(const char *name,
const char *parent, unsigned int mult, unsigned int div,
unsigned flags);
+struct clk *clk_register_fixed_factor(struct device_d *dev, const char *name,
+   const char *parent_name, unsigned long flags,
+   unsigned int mult, unsigned int div);
 
 /**
  * struct clk_fractional_divider - adjustable fractional divider clock
-- 
2.29.2


___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


[PATCH 06/24] clk: divider: Make clk_divider_ops const

2021-06-02 Thread Sascha Hauer
clk_divider_ops shouldn't be changed, so make it const.

Signed-off-by: Sascha Hauer 
---
 drivers/clk/clk-divider.c | 2 +-
 include/linux/clk.h   | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/clk/clk-divider.c b/drivers/clk/clk-divider.c
index edbba941b7..446b35e1ed 100644
--- a/drivers/clk/clk-divider.c
+++ b/drivers/clk/clk-divider.c
@@ -296,7 +296,7 @@ static int clk_divider_set_rate(struct clk_hw *hw, unsigned 
long rate,
return 0;
 }
 
-struct clk_ops clk_divider_ops = {
+const struct clk_ops clk_divider_ops = {
.set_rate = clk_divider_set_rate,
.recalc_rate = clk_divider_recalc_rate,
.round_rate = clk_divider_round_rate,
diff --git a/include/linux/clk.h b/include/linux/clk.h
index 654845023a..b8674973fa 100644
--- a/include/linux/clk.h
+++ b/include/linux/clk.h
@@ -450,7 +450,7 @@ struct clk_divider {
 #define CLK_MUX_HIWORD_MASK(1 << 2)
 #define CLK_MUX_READ_ONLY  (1 << 3) /* mux can't be changed */
 
-extern struct clk_ops clk_divider_ops;
+extern const struct clk_ops clk_divider_ops;
 
 unsigned long divider_recalc_rate(struct clk *clk, unsigned long parent_rate,
unsigned int val,
-- 
2.29.2


___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


[PATCH 17/24] clk: Add Linux functions to register a mux

2021-06-02 Thread Sascha Hauer
Linux has clk_register_mux(). Add the same function with the same
prototype for barebox to ease code porting from Linux.

Signed-off-by: Sascha Hauer 
---
 drivers/clk/clk-mux.c | 10 ++
 include/linux/clk.h   |  5 +
 2 files changed, 15 insertions(+)

diff --git a/drivers/clk/clk-mux.c b/drivers/clk/clk-mux.c
index 464593b619..e7e4369ce0 100644
--- a/drivers/clk/clk-mux.c
+++ b/drivers/clk/clk-mux.c
@@ -98,3 +98,13 @@ struct clk *clk_mux(const char *name, unsigned clk_flags, 
void __iomem *reg,
 
return m;
 }
+
+struct clk *clk_register_mux(struct device_d *dev, const char *name,
+   const char * const *parent_names, u8 num_parents,
+   unsigned long flags,
+   void __iomem *reg, u8 shift, u8 width,
+   u8 clk_mux_flags, spinlock_t *lock)
+{
+   return clk_mux(name, flags, reg, shift, width, parent_names,
+  num_parents, clk_mux_flags);
+}
diff --git a/include/linux/clk.h b/include/linux/clk.h
index 950fecd7e0..0d2fb488bc 100644
--- a/include/linux/clk.h
+++ b/include/linux/clk.h
@@ -589,6 +589,11 @@ void clk_mux_free(struct clk *clk_mux);
 struct clk *clk_mux(const char *name, unsigned clk_flags, void __iomem *reg,
u8 shift, u8 width, const char * const *parents,
u8 num_parents, unsigned mux_flags);
+struct clk *clk_register_mux(struct device_d *dev, const char *name,
+   const char * const *parent_names, u8 num_parents,
+   unsigned long flags,
+   void __iomem *reg, u8 shift, u8 width,
+   u8 clk_mux_flags, spinlock_t *lock);
 
 struct clk_gate {
struct clk_hw hw;
-- 
2.29.2


___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


[PATCH 08/24] clk: divider: Make clk_mux_ops const

2021-06-02 Thread Sascha Hauer
clk_mux_ops shouldn't be changed, so make it const.

Signed-off-by: Sascha Hauer 
---
 drivers/clk/clk-mux.c | 2 +-
 include/linux/clk.h   | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/clk/clk-mux.c b/drivers/clk/clk-mux.c
index 981f3aa853..bd5b50ad18 100644
--- a/drivers/clk/clk-mux.c
+++ b/drivers/clk/clk-mux.c
@@ -41,7 +41,7 @@ static int clk_mux_set_parent(struct clk_hw *hw, u8 idx)
return 0;
 }
 
-struct clk_ops clk_mux_ops = {
+const struct clk_ops clk_mux_ops = {
.set_rate = clk_parent_set_rate,
.round_rate = clk_parent_round_rate,
.get_parent = clk_mux_get_parent,
diff --git a/include/linux/clk.h b/include/linux/clk.h
index e1db903d74..ede2b676fe 100644
--- a/include/linux/clk.h
+++ b/include/linux/clk.h
@@ -503,7 +503,7 @@ struct clk_mux {
 
 #define to_clk_mux(_hw) container_of(_hw, struct clk_mux, hw)
 
-extern struct clk_ops clk_mux_ops;
+extern const struct clk_ops clk_mux_ops;
 
 struct clk *clk_mux_alloc(const char *name, unsigned clk_flags,
  void __iomem *reg, u8 shift, u8 width,
-- 
2.29.2


___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


[PATCH 07/24] clk: divider: Add ro ops

2021-06-02 Thread Sascha Hauer
The Linux version of the clk divider exports a clk_divider_ro_ops. Do
the same for barebox.

Signed-off-by: Sascha Hauer 
---
 drivers/clk/clk-divider.c | 4 
 include/linux/clk.h   | 1 +
 2 files changed, 5 insertions(+)

diff --git a/drivers/clk/clk-divider.c b/drivers/clk/clk-divider.c
index 446b35e1ed..447c43dc93 100644
--- a/drivers/clk/clk-divider.c
+++ b/drivers/clk/clk-divider.c
@@ -302,6 +302,10 @@ const struct clk_ops clk_divider_ops = {
.round_rate = clk_divider_round_rate,
 };
 
+const struct clk_ops clk_divider_ro_ops = {
+   .recalc_rate = clk_divider_recalc_rate,
+};
+
 struct clk *clk_divider_alloc(const char *name, const char *parent,
  unsigned clk_flags, void __iomem *reg, u8 shift,
  u8 width, unsigned div_flags)
diff --git a/include/linux/clk.h b/include/linux/clk.h
index b8674973fa..e1db903d74 100644
--- a/include/linux/clk.h
+++ b/include/linux/clk.h
@@ -451,6 +451,7 @@ struct clk_divider {
 #define CLK_MUX_READ_ONLY  (1 << 3) /* mux can't be changed */
 
 extern const struct clk_ops clk_divider_ops;
+extern const struct clk_ops clk_divider_ro_ops;
 
 unsigned long divider_recalc_rate(struct clk *clk, unsigned long parent_rate,
unsigned int val,
-- 
2.29.2


___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


[PATCH 20/24] clk: implement CLK_SET_RATE_UNGATE

2021-06-02 Thread Sascha Hauer
Some clocks need to be turned on in oder to change their rate. Implement
CLK_SET_RATE_UNGATE to support these.

Signed-off-by: Sascha Hauer 
---
 drivers/clk/clk.c   | 12 +++-
 include/linux/clk.h |  1 +
 2 files changed, 12 insertions(+), 1 deletion(-)

diff --git a/drivers/clk/clk.c b/drivers/clk/clk.c
index 74e2f5d783..fe2424dc8a 100644
--- a/drivers/clk/clk.c
+++ b/drivers/clk/clk.c
@@ -172,6 +172,12 @@ int clk_set_rate(struct clk *clk, unsigned long rate)
if (!clk->ops->set_rate)
return -ENOSYS;
 
+   if (clk->flags & CLK_SET_RATE_UNGATE) {
+   ret = clk_enable(clk);
+   if (ret)
+   return ret;
+   }
+
parent = clk_get_parent(clk);
if (parent) {
parent_rate = clk_get_rate(parent);
@@ -179,7 +185,7 @@ int clk_set_rate(struct clk *clk, unsigned long rate)
if (clk->flags & CLK_OPS_PARENT_ENABLE) {
ret = clk_enable(parent);
if (ret)
-   return ret;
+   goto out;
}
}
 
@@ -190,6 +196,10 @@ int clk_set_rate(struct clk *clk, unsigned long rate)
if (parent && clk->flags & CLK_OPS_PARENT_ENABLE)
clk_disable(parent);
 
+out:
+   if (clk->flags & CLK_SET_RATE_UNGATE)
+   clk_disable(clk);
+
return ret;
 }
 
diff --git a/include/linux/clk.h b/include/linux/clk.h
index d53699a874..72971c8e27 100644
--- a/include/linux/clk.h
+++ b/include/linux/clk.h
@@ -335,6 +335,7 @@ static inline void clk_put(struct clk *clk)
 #define CLK_IGNORE_UNUSED   (1 << 3) /* do not gate even if unused */
 #define CLK_GET_RATE_NOCACHE(1 << 6) /* do not use the cached clk rate */
 #define CLK_SET_RATE_NO_REPARENT (1 << 7) /* don't re-parent on rate change */
+#define CLK_SET_RATE_UNGATE (1 << 10) /* clock needs to run to set rate */
 #define CLK_IS_CRITICAL (1 << 11) /* do not gate, ever */
 /* parents need enable during gate/ungate, set rate and re-parent */
 #define CLK_OPS_PARENT_ENABLE   (1 << 12)
-- 
2.29.2


___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


[PATCH v2 00/24] clk updates

2021-06-02 Thread Sascha Hauer
The barebox clk framework has diverged a lot from Linux. Specifically
the separation of a struct clk_hw from struct clk causes some pain when
porting over code from Linux. This series aims to reduce the differences
a bit. New this time is the addition of the rk3568 clock code which is
a first step to get rk3568 up.

Changes since v1:
- Make exported clk ops const
- Add more patches to get closer to Linux
- Update Rockchip Clock support to current Linux
- Add rk3568 clk support

Sascha Hauer (24):
  clk: clk-mux: Fix handling of CLK_MUX_HIWORD_MASK
  clk: introduce clk init op
  clk: rename clk_register() to bclk_register()
  clk: introduce struct clk_hw
  clk: introduce clk_register()
  clk: divider: Make clk_divider_ops const
  clk: divider: Add ro ops
  clk: divider: Make clk_mux_ops const
  clk: mux: Add ro ops
  clk: move fixed_factor to include/linux/clk.h
  Add rational_best_approximation()
  clk: Update fractional divider from Linux
  clk: Add lock to different clock types
  clk: Add Linux functions to register a divider
  clk: Add Linux functions to register a fixed factor clock
  clk: Add Linux functions to register a gate
  clk: Add Linux functions to register a mux
  clk: Add CLK_GET_RATE_NOCACHE
  clk: Rename CLK_GATE_INVERTED to CLK_GATE_SET_TO_DISABLE
  clk: implement CLK_SET_RATE_UNGATE
  clk: implement set/get phase
  regmap: Add regmap_read_poll_timeout
  clk: rockchip: Update to current Linux
  clk: Rockchip: Add rk3568 clk support

 drivers/clk/at91/clk-audio-pll.c|  103 +-
 drivers/clk/at91/clk-generated.c|   42 +-
 drivers/clk/at91/clk-h32mx.c|   22 +-
 drivers/clk/at91/clk-i2s-mux.c  |   24 +-
 drivers/clk/at91/clk-main.c |  126 +-
 drivers/clk/at91/clk-master.c   |   32 +-
 drivers/clk/at91/clk-peripheral.c   |   72 +-
 drivers/clk/at91/clk-pll.c  |   40 +-
 drivers/clk/at91/clk-plldiv.c   |   26 +-
 drivers/clk/at91/clk-programmable.c |   32 +-
 drivers/clk/at91/clk-sam9x60-pll.c  |   28 +-
 drivers/clk/at91/clk-slow.c |   20 +-
 drivers/clk/at91/clk-smd.c  |   36 +-
 drivers/clk/at91/clk-system.c   |   28 +-
 drivers/clk/at91/clk-usb.c  |   92 +-
 drivers/clk/at91/clk-utmi.c |   32 +-
 drivers/clk/at91/sckc.c |  114 +-
 drivers/clk/clk-ar933x.c|   18 +-
 drivers/clk/clk-ar9344.c|   18 +-
 drivers/clk/clk-composite.c |   74 +-
 drivers/clk/clk-divider.c   |   78 +-
 drivers/clk/clk-fixed-factor.c  |   44 +-
 drivers/clk/clk-fixed.c |   20 +-
 drivers/clk/clk-fractional-divider.c|  133 +-
 drivers/clk/clk-gate-shared.c   |   33 +-
 drivers/clk/clk-gate.c  |   52 +-
 drivers/clk/clk-gpio.c  |   32 +-
 drivers/clk/clk-mux.c   |   48 +-
 drivers/clk/clk-qoric.c |   34 +-
 drivers/clk/clk-stm32mp1.c  |  103 +-
 drivers/clk/clk.c   |  207 ++-
 drivers/clk/imx/clk-composite-8m.c  |   33 +-
 drivers/clk/imx/clk-cpu.c   |   32 +-
 drivers/clk/imx/clk-frac-pll.c  |   38 +-
 drivers/clk/imx/clk-gate-exclusive.c|   36 +-
 drivers/clk/imx/clk-gate2.c |   45 +-
 drivers/clk/imx/clk-imx6ul.c|4 +-
 drivers/clk/imx/clk-pfd.c   |   34 +-
 drivers/clk/imx/clk-pll14xx.c   |   47 +-
 drivers/clk/imx/clk-pllv1.c |   18 +-
 drivers/clk/imx/clk-pllv2.c |   24 +-
 drivers/clk/imx/clk-pllv3.c |   70 +-
 drivers/clk/imx/clk-sccg-pll.c  |   51 +-
 drivers/clk/loongson/clk-ls1b200.c  |   18 +-
 drivers/clk/mvebu/corediv.c |   30 +-
 drivers/clk/mxs/clk-div.c   |   36 +-
 drivers/clk/mxs/clk-frac.c  |   30 +-
 drivers/clk/mxs/clk-lcdif.c |   20 +-
 drivers/clk/mxs/clk-pll.c   |   32 +-
 drivers/clk/mxs/clk-ref.c   |   38 +-
 drivers/clk/rockchip/Makefile   |3 +-
 drivers/clk/rockchip/clk-cpu.c  |   82 +-
 drivers/clk/rockchip/clk-inverter.c |  107 ++
 drivers/clk/rockchip/clk-mmc-phase.c|  192 +++
 drivers/clk/rockchip/clk-muxgrf.c   |   95 ++
 drivers/clk/rockchip/clk-pll.c  |  853 ++--
 drivers/clk/rockchip/clk-rk3188.c   |  363 ++---
 drivers/clk/rockchip/clk-rk3288.c   |  309 ++--
 drivers/clk/rockchip/clk-rk3568.c   | 1704 +++
 drivers/clk/rockchip/clk.c  |  489 +--
 drivers/clk/rockchip/clk.h  |  500 ++-
 drivers/clk/socfpga/clk-gate-a10.c  |   32 +-
 drivers/clk/socfpga/clk-periph-a10.c|   28 +-
 drivers/clk/socfpga/clk-pll-a10.c   |   30 +-
 drivers/clk/socfpga/clk.c   |   74 +-
 drivers/clk/socfpga/clk.h   |6 +-
 drivers/clk/tegra/clk-divider.c |   17 +-
 drivers/clk/tegra/clk-periph.c  |   44 +-
 drivers

[PATCH 13/24] clk: Add lock to different clock types

2021-06-02 Thread Sascha Hauer
The different clock types in Linux have a spinlock_t *lock field. Add
the same in barebox to ease code porting from Linux.

Signed-off-by: Sascha Hauer 
---
 include/linux/clk.h | 5 +
 1 file changed, 5 insertions(+)

diff --git a/include/linux/clk.h b/include/linux/clk.h
index 39288f5025..9d09738dda 100644
--- a/include/linux/clk.h
+++ b/include/linux/clk.h
@@ -11,6 +11,7 @@
 #define __LINUX_CLK_H
 
 #include 
+#include 
 #include 
 
 struct device_d;
@@ -437,6 +438,7 @@ struct clk_divider {
const struct clk_div_table *table;
int max_div_index;
int table_size;
+   spinlock_t *lock;
 };
 
 #define to_clk_divider(_hw) container_of(_hw, struct clk_divider, hw)
@@ -533,6 +535,7 @@ struct clk_fractional_divider {
void(*approximation)(struct clk_hw *hw,
unsigned long rate, unsigned long *parent_rate,
unsigned long *m, unsigned long *n);
+   spinlock_t *lock;
 };
 
 #define CLK_FRAC_DIVIDER_ZERO_BASEDBIT(0)
@@ -558,6 +561,7 @@ struct clk_mux {
int shift;
int width;
unsigned flags;
+   spinlock_t *lock;
 };
 
 #define to_clk_mux(_hw) container_of(_hw, struct clk_mux, hw)
@@ -580,6 +584,7 @@ struct clk_gate {
int shift;
const char *parent;
unsigned flags;
+   spinlock_t *lock;
 };
 
 int clk_gate_is_enabled(struct clk_hw *hw);
-- 
2.29.2


___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


[PATCH 21/24] clk: implement set/get phase

2021-06-02 Thread Sascha Hauer
Linux has clk_set_phase() and clk_get_phase() along with the
corresponding callbacks in struct clk_ops. Implement the same for
barebox as well.

Signed-off-by: Sascha Hauer 
---
 drivers/clk/clk.c   | 55 +
 include/linux/clk.h |  5 +
 2 files changed, 60 insertions(+)

diff --git a/drivers/clk/clk.c b/drivers/clk/clk.c
index fe2424dc8a..8b90f30486 100644
--- a/drivers/clk/clk.c
+++ b/drivers/clk/clk.c
@@ -320,6 +320,61 @@ struct clk_hw *clk_hw_get_parent(struct clk_hw *hw)
return clk_to_clk_hw(clk);
 }
 
+/**
+ * clk_set_phase - adjust the phase shift of a clock signal
+ * @clk: clock signal source
+ * @degrees: number of degrees the signal is shifted
+ *
+ * Shifts the phase of a clock signal by the specified
+ * degrees. Returns 0 on success, -EERROR otherwise.
+ *
+ * This function makes no distinction about the input or reference
+ * signal that we adjust the clock signal phase against. For example
+ * phase locked-loop clock signal generators we may shift phase with
+ * respect to feedback clock signal input, but for other cases the
+ * clock phase may be shifted with respect to some other, unspecified
+ * signal.
+ *
+ * Additionally the concept of phase shift does not propagate through
+ * the clock tree hierarchy, which sets it apart from clock rates and
+ * clock accuracy. A parent clock phase attribute does not have an
+ * impact on the phase attribute of a child clock.
+ */
+int clk_set_phase(struct clk *clk, int degrees)
+{
+   if (!clk)
+   return 0;
+
+   /* sanity check degrees */
+   degrees %= 360;
+   if (degrees < 0)
+   degrees += 360;
+
+   if (!clk->ops->set_phase)
+   return -EINVAL;
+
+   return clk->ops->set_phase(clk_to_clk_hw(clk), degrees);
+}
+
+/**
+ * clk_get_phase - return the phase shift of a clock signal
+ * @clk: clock signal source
+ *
+ * Returns the phase shift of a clock node in degrees, otherwise returns
+ * -EERROR.
+ */
+int clk_get_phase(struct clk *clk)
+{
+   int ret;
+
+   if (!clk->ops->get_phase)
+   return 0;
+
+   ret = clk->ops->get_phase(clk_to_clk_hw(clk));
+
+   return ret;
+}
+
 int bclk_register(struct clk *clk)
 {
struct clk_hw *hw = clk_to_clk_hw(clk);
diff --git a/include/linux/clk.h b/include/linux/clk.h
index 72971c8e27..6b4c368231 100644
--- a/include/linux/clk.h
+++ b/include/linux/clk.h
@@ -237,6 +237,9 @@ int clk_hw_set_parent(struct clk_hw *hw, struct clk_hw 
*hwp);
 struct clk *clk_get_parent(struct clk *clk);
 struct clk_hw *clk_hw_get_parent(struct clk_hw *hw);
 
+int clk_set_phase(struct clk *clk, int degrees);
+int clk_get_phase(struct clk *clk);
+
 /**
  * clk_get_sys - get a clock based upon the device name
  * @dev_id: device name
@@ -356,6 +359,8 @@ struct clk_ops {
int (*get_parent)(struct clk_hw *hw);
int (*set_rate)(struct clk_hw *hw, unsigned long,
unsigned long);
+   int (*set_phase)(struct clk_hw *hw, int degrees);
+   int (*get_phase)(struct clk_hw *hw);
 };
 
 /**
-- 
2.29.2


___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


[PATCH 05/24] clk: introduce clk_register()

2021-06-02 Thread Sascha Hauer
This introduces a clk_register() with the same semantics as in Linux.
This also adds a struct clk_init_data. With this it becomes easier to
port over new clock drivers from Linux.

Signed-off-by: Sascha Hauer 
Reviewed-by: Ahmad Fatoum 
---
 drivers/clk/clk.c   | 37 +
 include/linux/clk.h |  2 ++
 2 files changed, 39 insertions(+)

diff --git a/drivers/clk/clk.c b/drivers/clk/clk.c
index 73682126bf..74e2f5d783 100644
--- a/drivers/clk/clk.c
+++ b/drivers/clk/clk.c
@@ -345,6 +345,43 @@ out:
return ret;
 }
 
+struct clk *clk_register(struct device_d *dev, struct clk_hw *hw)
+{
+   struct clk *clk;
+   const struct clk_init_data *init = hw->init;
+   char **parent_names;
+   int i, ret;
+
+   if (!hw->init)
+   return ERR_PTR(-EINVAL);
+
+   clk = clk_hw_to_clk(hw);
+
+   memset(clk, 0, sizeof(*clk));
+
+   clk->name = xstrdup(init->name);
+   clk->ops = init->ops;
+   clk->num_parents = init->num_parents;
+   parent_names = xzalloc(init->num_parents * sizeof(char *));
+
+   for (i = 0; i < init->num_parents; i++)
+   parent_names[i] = xstrdup(init->parent_names[i]);
+
+   clk->parent_names = (const char *const*)parent_names;
+
+   clk->flags = init->flags;
+
+   ret = bclk_register(clk);
+   if (ret) {
+   for (i = 0; i < init->num_parents; i++)
+   free(parent_names[i]);
+   free(parent_names);
+   return ERR_PTR(ret);
+   }
+
+   return clk;
+}
+
 int clk_is_enabled(struct clk *clk)
 {
int enabled;
diff --git a/include/linux/clk.h b/include/linux/clk.h
index 66ac6a9f14..654845023a 100644
--- a/include/linux/clk.h
+++ b/include/linux/clk.h
@@ -399,6 +399,7 @@ struct clk {
  */
 struct clk_hw {
struct clk clk;
+   const struct clk_init_data *init;
 };
 
 static inline struct clk *clk_hw_to_clk(struct clk_hw *hw)
@@ -547,6 +548,7 @@ int clk_parent_set_rate(struct clk_hw *hw, unsigned long 
rate,
unsigned long parent_rate);
 
 int bclk_register(struct clk *clk);
+struct clk *clk_register(struct device_d *dev, struct clk_hw *hw);
 
 struct clk *clk_lookup(const char *name);
 
-- 
2.29.2


___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


[PATCH 01/24] clk: clk-mux: Fix handling of CLK_MUX_HIWORD_MASK

2021-06-02 Thread Sascha Hauer
CLK_MUX_HIWORD_MASK is a flag of the mux clock, not a generic clock
flag.

Signed-off-by: Sascha Hauer 
---
 drivers/clk/clk-mux.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/clk/clk-mux.c b/drivers/clk/clk-mux.c
index a4743c51b0..7977a76db9 100644
--- a/drivers/clk/clk-mux.c
+++ b/drivers/clk/clk-mux.c
@@ -34,7 +34,7 @@ static int clk_mux_set_parent(struct clk *clk, u8 idx)
val &= ~(((1 << m->width) - 1) << m->shift);
val |= idx << m->shift;
 
-   if (clk->flags & CLK_MUX_HIWORD_MASK)
+   if (m->flags & CLK_MUX_HIWORD_MASK)
val |= ((1 << m->width) - 1) << (m->shift + 16);
writel(val, m->reg);
 
-- 
2.29.2


___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


[PATCH 22/24] regmap: Add regmap_read_poll_timeout

2021-06-02 Thread Sascha Hauer
Signed-off-by: Sascha Hauer 
---
 include/regmap.h | 24 
 1 file changed, 24 insertions(+)

diff --git a/include/regmap.h b/include/regmap.h
index b43cd936fa..057370afc7 100644
--- a/include/regmap.h
+++ b/include/regmap.h
@@ -116,4 +116,28 @@ int regmap_write_bits(struct regmap *map, unsigned int reg,
 int regmap_update_bits(struct regmap *map, unsigned int reg,
   unsigned int mask, unsigned int val);
 
+/**
+ * regmap_read_poll_timeout - Poll until a condition is met or a timeout occurs
+ *
+ * @map: Regmap to read from
+ * @addr: Address to poll
+ * @val: Unsigned integer variable to read the value into
+ * @cond: Break condition (usually involving @val)
+ * @timeout_us: Timeout in us, 0 means never timeout
+ *
+ * Returns 0 on success and -ETIMEDOUT upon a timeout or the regmap_read
+ * error return value in case of a error read. In the two former cases,
+ * the last read value at @addr is stored in @val. Must not be called
+ * from atomic context if sleep_us or timeout_us are used.
+ *
+ * This is modelled after the readx_poll_timeout macros in linux/iopoll.h.
+ */
+#define regmap_read_poll_timeout(map, addr, val, cond, timeout_us) \
+({ \
+   int __ret, __tmp; \
+   __tmp = read_poll_timeout(regmap_read, __ret, __ret || (cond), \
+   timeout_us, (map), (addr), &(val)); \
+   __ret ?: __tmp; \
+})
+
 #endif /* __REGMAP_H */
-- 
2.29.2


___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


[PATCH 02/24] clk: introduce clk init op

2021-06-02 Thread Sascha Hauer
Signed-off-by: Sascha Hauer 
Reviewed-by: Ahmad Fatoum 
---
 drivers/clk/clk.c   | 12 
 include/linux/clk.h |  1 +
 2 files changed, 13 insertions(+)

diff --git a/drivers/clk/clk.c b/drivers/clk/clk.c
index b04d44593b..fb5bf57e4b 100644
--- a/drivers/clk/clk.c
+++ b/drivers/clk/clk.c
@@ -261,6 +261,7 @@ struct clk *clk_get_parent(struct clk *clk)
 int clk_register(struct clk *clk)
 {
struct clk *c;
+   int ret;
 
list_for_each_entry(c, &clks, list) {
if (!strcmp(c->name, clk->name)) {
@@ -274,10 +275,21 @@ int clk_register(struct clk *clk)
 
list_add_tail(&clk->list, &clks);
 
+   if (clk->ops->init) {
+   ret = clk->ops->init(clk);
+   if (ret)
+   goto out;
+   }
+
if (clk->flags & CLK_IS_CRITICAL)
clk_enable(clk);
 
return 0;
+out:
+   list_del(&clk->list);
+   free(clk->parents);
+
+   return ret;
 }
 
 int clk_is_enabled(struct clk *clk)
diff --git a/include/linux/clk.h b/include/linux/clk.h
index c49fe9a54c..5d05ffd1b3 100644
--- a/include/linux/clk.h
+++ b/include/linux/clk.h
@@ -336,6 +336,7 @@ static inline void clk_put(struct clk *clk)
 #define CLK_GATE_HIWORD_MASK   (1 << 1)
 
 struct clk_ops {
+   int (*init)(struct clk *clk);
int (*enable)(struct clk *clk);
void(*disable)(struct clk *clk);
int (*is_enabled)(struct clk *clk);
-- 
2.29.2


___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


[PATCH 09/24] clk: mux: Add ro ops

2021-06-02 Thread Sascha Hauer
The Linux version of the clk muxr exports a clk_mux_ro_ops. Do
the same for barebox.

Signed-off-by: Sascha Hauer 
---
 drivers/clk/clk-mux.c | 4 
 include/linux/clk.h   | 1 +
 2 files changed, 5 insertions(+)

diff --git a/drivers/clk/clk-mux.c b/drivers/clk/clk-mux.c
index bd5b50ad18..464593b619 100644
--- a/drivers/clk/clk-mux.c
+++ b/drivers/clk/clk-mux.c
@@ -48,6 +48,10 @@ const struct clk_ops clk_mux_ops = {
.set_parent = clk_mux_set_parent,
 };
 
+const struct clk_ops clk_mux_ro_ops = {
+   .get_parent = clk_mux_get_parent,
+};
+
 struct clk *clk_mux_alloc(const char *name, unsigned clk_flags, void __iomem 
*reg,
u8 shift, u8 width, const char * const *parents, u8 num_parents,
unsigned mux_flags)
diff --git a/include/linux/clk.h b/include/linux/clk.h
index ede2b676fe..edf64cd9aa 100644
--- a/include/linux/clk.h
+++ b/include/linux/clk.h
@@ -504,6 +504,7 @@ struct clk_mux {
 #define to_clk_mux(_hw) container_of(_hw, struct clk_mux, hw)
 
 extern const struct clk_ops clk_mux_ops;
+extern const struct clk_ops clk_mux_ro_ops;
 
 struct clk *clk_mux_alloc(const char *name, unsigned clk_flags,
  void __iomem *reg, u8 shift, u8 width,
-- 
2.29.2


___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


[PATCH 18/24] clk: Add CLK_GET_RATE_NOCACHE

2021-06-02 Thread Sascha Hauer
Just add the flag. We don't need any implementation of this flag as we
do not cache the clock rate anyway.

Signed-off-by: Sascha Hauer 
---
 include/linux/clk.h | 1 +
 1 file changed, 1 insertion(+)

diff --git a/include/linux/clk.h b/include/linux/clk.h
index 0d2fb488bc..c49604fe2d 100644
--- a/include/linux/clk.h
+++ b/include/linux/clk.h
@@ -333,6 +333,7 @@ static inline void clk_put(struct clk *clk)
 
 #define CLK_SET_RATE_PARENT (1 << 0) /* propagate rate change up one level 
*/
 #define CLK_IGNORE_UNUSED   (1 << 3) /* do not gate even if unused */
+#define CLK_GET_RATE_NOCACHE(1 << 6) /* do not use the cached clk rate */
 #define CLK_SET_RATE_NO_REPARENT (1 << 7) /* don't re-parent on rate change */
 #define CLK_IS_CRITICAL (1 << 11) /* do not gate, ever */
 /* parents need enable during gate/ungate, set rate and re-parent */
-- 
2.29.2


___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


Re: [PATCH 1/2] partitions: Increase MAX_PARTITION to 128

2021-06-02 Thread Ahmad Fatoum
On 02.06.21 09:15, Sascha Hauer wrote:
> Having MAX_PARTITION defined to 8 is enough for a DOS partition table,
> but not for GPT. Increase it to the maximum GPT supports. It might be
> even better to allocate the partitions dynamically, but for nor take the
> easy way out.

Given that struct partition_desc is only ever dynamically allocated, I think
that's ok.

> Signed-off-by: Sascha Hauer 

Reviwed-by: Ahmad Fatoum 

> ---
>  common/partitions/parser.h | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/common/partitions/parser.h b/common/partitions/parser.h
> index 8ad134a9aa..69508932b3 100644
> --- a/common/partitions/parser.h
> +++ b/common/partitions/parser.h
> @@ -11,7 +11,7 @@
>  #include 
>  #include 
>  
> -#define MAX_PARTITION8
> +#define MAX_PARTITION128
>  #define MAX_PARTITION_NAME   38
>  
>  struct partition {
> 

-- 
Pengutronix e.K.   | |
Steuerwalder Str. 21   | http://www.pengutronix.de/  |
31137 Hildesheim, Germany  | Phone: +49-5121-206917-0|
Amtsgericht Hildesheim, HRA 2686   | Fax:   +49-5121-206917- |

___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


Re: [PATCH 2/2] partitions: efi: Fix MAX_PARTITION check

2021-06-02 Thread Ahmad Fatoum



On 02.06.21 09:15, Sascha Hauer wrote:
> The GPT partiton parser has a check which should check if the GPT has
> more partitions than we support. This doesn't work because the loop
> iterating over the partitions exits with a maximum i of MAX_PARTITION,
> i > MAX_PARTITION will never be true. Fix the check.
> 
> Signed-off-by: Sascha Hauer 

Reviewed-by: Ahmad Fatoum 

> ---
>  common/partitions/efi.c | 13 -
>  1 file changed, 8 insertions(+), 5 deletions(-)
> 
> diff --git a/common/partitions/efi.c b/common/partitions/efi.c
> index 437c3d64f8..135b08901a 100644
> --- a/common/partitions/efi.c
> +++ b/common/partitions/efi.c
> @@ -446,7 +446,14 @@ static void efi_partition(void *buf, struct block_device 
> *blk,
>   }
>  
>   nb_part = le32_to_cpu(gpt->num_partition_entries);
> - for (i = 0; i < MAX_PARTITION && i < nb_part; i++) {
> +
> + if (nb_part > MAX_PARTITION) {
> + dev_warn(blk->dev, "GPT has more partitions than we support 
> (%d) > max partition number (%d)\n",
> +  nb_part, MAX_PARTITION);
> + nb_part = MAX_PARTITION;
> + }
> +
> + for (i = 0; i < nb_part; i++) {
>   if (!is_pte_valid(&ptes[i], last_lba(blk))) {
>   dev_dbg(blk->dev, "Invalid pte %d\n", i);
>   return;
> @@ -460,10 +467,6 @@ static void efi_partition(void *buf, struct block_device 
> *blk,
>   snprintf(pentry->partuuid, sizeof(pentry->partuuid), "%pUl", 
> &ptes[i].unique_partition_guid);
>   pd->used_entries++;
>   }
> -
> - if (i > MAX_PARTITION)
> - dev_warn(blk->dev, "num_partition_entries (%d) > max partition 
> number (%d)\n",
> -  nb_part, MAX_PARTITION);
>  }
>  
>  static struct partition_parser efi_partition_parser = {
> 

-- 
Pengutronix e.K.   | |
Steuerwalder Str. 21   | http://www.pengutronix.de/  |
31137 Hildesheim, Germany  | Phone: +49-5121-206917-0|
Amtsgericht Hildesheim, HRA 2686   | Fax:   +49-5121-206917- |

___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


[PATCH 2/2] partitions: efi: Fix MAX_PARTITION check

2021-06-02 Thread Sascha Hauer
The GPT partiton parser has a check which should check if the GPT has
more partitions than we support. This doesn't work because the loop
iterating over the partitions exits with a maximum i of MAX_PARTITION,
i > MAX_PARTITION will never be true. Fix the check.

Signed-off-by: Sascha Hauer 
---
 common/partitions/efi.c | 13 -
 1 file changed, 8 insertions(+), 5 deletions(-)

diff --git a/common/partitions/efi.c b/common/partitions/efi.c
index 437c3d64f8..135b08901a 100644
--- a/common/partitions/efi.c
+++ b/common/partitions/efi.c
@@ -446,7 +446,14 @@ static void efi_partition(void *buf, struct block_device 
*blk,
}
 
nb_part = le32_to_cpu(gpt->num_partition_entries);
-   for (i = 0; i < MAX_PARTITION && i < nb_part; i++) {
+
+   if (nb_part > MAX_PARTITION) {
+   dev_warn(blk->dev, "GPT has more partitions than we support 
(%d) > max partition number (%d)\n",
+nb_part, MAX_PARTITION);
+   nb_part = MAX_PARTITION;
+   }
+
+   for (i = 0; i < nb_part; i++) {
if (!is_pte_valid(&ptes[i], last_lba(blk))) {
dev_dbg(blk->dev, "Invalid pte %d\n", i);
return;
@@ -460,10 +467,6 @@ static void efi_partition(void *buf, struct block_device 
*blk,
snprintf(pentry->partuuid, sizeof(pentry->partuuid), "%pUl", 
&ptes[i].unique_partition_guid);
pd->used_entries++;
}
-
-   if (i > MAX_PARTITION)
-   dev_warn(blk->dev, "num_partition_entries (%d) > max partition 
number (%d)\n",
-nb_part, MAX_PARTITION);
 }
 
 static struct partition_parser efi_partition_parser = {
-- 
2.29.2


___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


[PATCH 1/2] partitions: Increase MAX_PARTITION to 128

2021-06-02 Thread Sascha Hauer
Having MAX_PARTITION defined to 8 is enough for a DOS partition table,
but not for GPT. Increase it to the maximum GPT supports. It might be
even better to allocate the partitions dynamically, but for nor take the
easy way out.

Signed-off-by: Sascha Hauer 
---
 common/partitions/parser.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/common/partitions/parser.h b/common/partitions/parser.h
index 8ad134a9aa..69508932b3 100644
--- a/common/partitions/parser.h
+++ b/common/partitions/parser.h
@@ -11,7 +11,7 @@
 #include 
 #include 
 
-#define MAX_PARTITION  8
+#define MAX_PARTITION  128
 #define MAX_PARTITION_NAME 38
 
 struct partition {
-- 
2.29.2


___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


Re: [PATCH 2/3] ARM: Rockchip: Merge defconfig files

2021-06-02 Thread Sascha Hauer
On Tue, May 18, 2021 at 01:48:37PM +0200, Ahmad Fatoum wrote:
> Hello Sascha,
> 
> On 18.05.21 13:44, Sascha Hauer wrote:
> > Signed-off-by: Sascha Hauer 
> > ---
> >  arch/arm/configs/rk3188_defconfig | 98 ---
> >  .../{rk3288_defconfig => rockchip_defconfig}  |  5 +-
> 
> Perhaps call it rockchip_v7a_defconfig or similar to differentiate it from
> future 64-bit support?

Sounds good, Did that.

Sascha

-- 
Pengutronix e.K.   | |
Steuerwalder Str. 21   | http://www.pengutronix.de/  |
31137 Hildesheim, Germany  | Phone: +49-5121-206917-0|
Amtsgericht Hildesheim, HRA 2686   | Fax:   +49-5121-206917- |

___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox