Re: [PATCH 2/3] efi: qemu: arm64: Add efi_rng_protocol implementation for the platform

2019-12-24 Thread Sughosh Ganu
hi Heinrich,
Thanks for the review.

On Tue, 24 Dec 2019 at 22:35, Heinrich Schuchardt 
wrote:

> On 12/24/19 4:54 PM, Sughosh Ganu wrote:
> > Add support for the EFI_RNG_PROTOCOL routines for the qemu arm64
> > platform. EFI_RNG_PROTOCOL is an uefi boottime service which is
> > invoked by the efi stub in the kernel for getting random seed for
> > kaslr.
> >
> > The routines are platform specific, and use the virtio-rng device on
> > the platform to get random data.
> >
> > The feature can be enabled through the following config
> > CONFIG_EFI_RNG_PROTOCOL
> >
> > Signed-off-by: Sughosh Ganu 
> > ---
> >   board/emulation/qemu-arm/qemu-arm.c | 50 +
> >   include/efi_rng.h   | 34 +
> >   lib/efi_loader/Kconfig  |  8 
> >   lib/efi_loader/Makefile |  1 +
> >   lib/efi_loader/efi_rng.c| 74
> +
> >   5 files changed, 167 insertions(+)
> >   create mode 100644 include/efi_rng.h
> >   create mode 100644 lib/efi_loader/efi_rng.c
> >
> > diff --git a/board/emulation/qemu-arm/qemu-arm.c
> b/board/emulation/qemu-arm/qemu-arm.c
> > index e1f4709..3176421 100644
> > --- a/board/emulation/qemu-arm/qemu-arm.c
> > +++ b/board/emulation/qemu-arm/qemu-arm.c
> > @@ -91,3 +91,53 @@ void *board_fdt_blob_setup(void)
> >   /* QEMU loads a generated DTB for us at the start of RAM. */
> >   return (void *)CONFIG_SYS_SDRAM_BASE;
> >   }
> > +
> > +#if defined(CONFIG_EFI_RNG_PROTOCOL)
> > +#include 
> > +#include 
> > +
> > +#include 
> > +
> > +#define VIRTIO_RNG_PCI_DEVICE"virtio-pci.l#0"
> > +
> > +void platform_rng_getinfo(efi_rng_algorithm *rng_algo)
>
> Thanks for working on the implementation of the EFI_RNG_PROTOCOL.
>
> Please, put an underscore after each word: platform_rng_get_info
>

Ok.


>
> > +{
> > + const efi_guid_t rng_raw_guid = EFI_RNG_ALGORITHM_RAW;
> > +
> > + guidcpy(rng_algo, _raw_guid);
>
> This function should be in efi_rng.c if it is needed at all.
>

Is the rng algorithm supported not platform specific. I believe different
platforms might use different algorithms for providing the random seed.


>
> > +}
> > +
> > +efi_status_t platform_get_rng_device(struct udevice **dev)
> > +{
>
> Here you are creating platform specific code. The idea of the driver
> model in U-Boot is to separate duties.
>
> So the implementation of the EFI_RNG_PROTOCOL should be platform
> agnostic and rely simply on looping of the devices of UCLASS_RNG.
>

The core implementation of efi_rng_protocol routines is indeed platform
agnostic. I have just separated the method of getting the rng device(struct
udevice), since different platforms might need different ways of getting
the rng device. For example, on the qemu arm64 platform, the rng device is
a child of a virtio device on the pci bus, while the rng module on the
stm32mp1 platform is a direct memory mapped peripheral. So the mechanism
used to fetch the rng udevice would be platform dependent. Hence the
platform_get_rng_device function. In fact, I see this kind of method
adopted in a lot many other boards, one example being
board/CZ.NIC/turris_omnia/turris_omnia.c(get_atsha204a_dev).


> Please, move function platform_get_rng_device into the RNG uclass.


> > + int ret;
> > + efi_status_t status = EFI_DEVICE_ERROR;
> > + struct udevice *bus, *devp;
> > +
> > + for (uclass_first_device(UCLASS_VIRTIO, ); bus;
> > +  uclass_next_device()) {
> > + for (device_find_first_child(bus, ); devp;
> device_find_next_child()) {
> > + if (device_get_uclass_id(devp) == UCLASS_RNG) {
> > + *dev = devp;
> > + status = EFI_SUCCESS;
> > + break;
> > + }
> > + }
> > + }
> > +
> > + if (status != EFI_SUCCESS) {
> > + debug("No rng device found\n");
> > + return EFI_DEVICE_ERROR;
> > + }
> > +
> > + if (*dev) {
> > + ret = device_probe(*dev);
> > + if (ret)
> > + return EFI_DEVICE_ERROR;
> > + } else {
> > + debug("Couldn't get child device\n");
> > + return EFI_DEVICE_ERROR;
> > + }
> > +
> > + return EFI_SUCCESS;
> > +}
> > +#endif /* CONFIG_EFI_RNG_PROTOCOL */
> > diff --git a/include/efi_rng.h b/include/efi_rng.h
> > new file mode 100644
> > index 000..df749dd
> > --- /dev/null
> > +++ b/include/efi_rng.h
> > @@ -0,0 +1,34 @@
> > +// SPDX-License-Identifier: GPL-2.0+
> > +/*
> > + * Copyright (c) 2019, Linaro Limited
> > + */
> > +
> > +#if !defined _EFI_RNG_H_
> > +#define _EFI_RNG_H_
> > +
> > +#include 
> > +#include 
> > +
> > +#define EFI_RNG_PROTOCOL_GUID \
> > + EFI_GUID(0x3152bca5, 0xeade, 0x433d, 0x86, 0x2e, \
> > +  0xc0, 0x1c, 0xdc, 0x29, 0x1f, 0x44)
> > +
> > +#define EFI_RNG_ALGORITHM_RAW \
> > + EFI_GUID(0xe43176d7, 

Re: [PATCH] clk: Check that ops of composite clock components exist, before calling

2019-12-24 Thread Sean Anderson
On 12/25/19 12:20 AM, Sean Anderson wrote:
>  int num_parents, struct clk *mux,
> @@ -94,7 +106,9 @@ struct clk *clk_register_composite(struct device *dev, 
> const char *name,
>   struct clk *clk;
>   struct clk_composite *composite;
>   int ret;
> - struct clk_ops *composite_ops = _composite_ops;
> +
> + if (!num_parents || (num_parents == 1 && !mux))

this should be

if (!num_parents || (num_parents != 1 && !mux))

> + return ERR_PTR(-EINVAL);
> 
>   composite = kzalloc(sizeof(*composite), GFP_KERNEL);
>   if (!composite)


[PATCH v2] riscv: Add option to print registers on exception

2019-12-24 Thread Sean Anderson
When debugging, it can be helpful to see more information about an
unhandled exception. This patch adds an option to view the registers at
the time of the trap, similar to the linux output on a kernel panic.

Signed-off-by: Sean Anderson 
---
Changes for v2:
   - Renamed "PC" to "EPC"

 arch/riscv/Kconfig  |  3 +++
 arch/riscv/cpu/mtrap.S  |  3 ++-
 arch/riscv/lib/interrupts.c | 50 +
 3 files changed, 44 insertions(+), 12 deletions(-)

diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig
index 85e15ebffa..3338b788f8 100644
--- a/arch/riscv/Kconfig
+++ b/arch/riscv/Kconfig
@@ -222,6 +222,9 @@ config XIP
  from a NOR flash memory without copying the code to ram.
  Say yes here if U-Boot boots from flash directly.

+config SHOW_REGS
+   bool "Show registers on unhandled exception"
+
 config STACK_SIZE_SHIFT
int
default 14
diff --git a/arch/riscv/cpu/mtrap.S b/arch/riscv/cpu/mtrap.S
index 407ecfa9c0..e40c7bd3f4 100644
--- a/arch/riscv/cpu/mtrap.S
+++ b/arch/riscv/cpu/mtrap.S
@@ -64,7 +64,8 @@ trap_entry:
SREG x31, 31 * REGBYTES(sp)
csrr a0, MODE_PREFIX(cause)
csrr a1, MODE_PREFIX(epc)
-   mv a2, sp
+   csrr a2, MODE_PREFIX(tval)
+   mv a3, sp
jal handle_trap
csrw MODE_PREFIX(epc), a0

diff --git a/arch/riscv/lib/interrupts.c b/arch/riscv/lib/interrupts.c
index 3b25c5b7a7..8352468f22 100644
--- a/arch/riscv/lib/interrupts.c
+++ b/arch/riscv/lib/interrupts.c
@@ -5,6 +5,8 @@
  *
  * Copyright (C) 2017 Andes Technology Corporation
  * Rick Chen, Andes Technology Corporation 
+ *
+ * Copyright (C) 2019 Sean Anderson 
  */

 #include 
@@ -13,7 +15,34 @@
 #include 
 #include 

-static void _exit_trap(ulong code, ulong epc, struct pt_regs *regs)
+static void show_regs(struct pt_regs *regs)
+{
+#ifdef CONFIG_SHOW_REGS
+   printf("RA: " REG_FMT " SP:  " REG_FMT " GP:  " REG_FMT "\n",
+  regs->ra, regs->sp, regs->gp);
+   printf("TP: " REG_FMT " T0:  " REG_FMT " T1:  " REG_FMT "\n",
+  regs->tp, regs->t0, regs->t1);
+   printf("T2: " REG_FMT " S0:  " REG_FMT " S1:  " REG_FMT "\n",
+  regs->t2, regs->s0, regs->s1);
+   printf("A0: " REG_FMT " A1:  " REG_FMT " A2:  " REG_FMT "\n",
+  regs->a0, regs->a1, regs->a2);
+   printf("A3: " REG_FMT " A4:  " REG_FMT " A5:  " REG_FMT "\n",
+  regs->a3, regs->a4, regs->a5);
+   printf("A6: " REG_FMT " A7:  " REG_FMT " S2:  " REG_FMT "\n",
+  regs->a6, regs->a7, regs->s2);
+   printf("S3: " REG_FMT " S4:  " REG_FMT " S5:  " REG_FMT "\n",
+  regs->s3, regs->s4, regs->s5);
+   printf("S6: " REG_FMT " S7:  " REG_FMT " S8:  " REG_FMT "\n",
+  regs->s6, regs->s7, regs->s8);
+   printf("S9: " REG_FMT " S10: " REG_FMT " S11: " REG_FMT "\n",
+  regs->s9, regs->s10, regs->s11);
+   printf("T3: " REG_FMT " T4:  " REG_FMT " T5:  " REG_FMT "\n",
+  regs->t3, regs->t4, regs->t5);
+   printf("T6: " REG_FMT "\n", regs->t6);
+#endif
+}
+
+static void _exit_trap(ulong code, ulong epc, ulong tval, struct pt_regs *regs)
 {
static const char * const exception_code[] = {
"Instruction address misaligned",
@@ -34,14 +63,13 @@ static void _exit_trap(ulong code, ulong epc, struct 
pt_regs *regs)
"Store/AMO page fault",
};

-   if (code < ARRAY_SIZE(exception_code)) {
-   printf("exception code: %ld , %s , epc %lx , ra %lx\n",
-  code, exception_code[code], epc, regs->ra);
-   } else {
-   printf("reserved exception code: %ld , epc %lx , ra %lx\n",
-  code, epc, regs->ra);
-   }
+   if (code < ARRAY_SIZE(exception_code))
+   printf("Unhandled exception: %s\n", exception_code[code]);
+   else
+   printf("Unhandled exception code: %ld\n", code);

+   printf("EPC: " REG_FMT " TVAL: " REG_FMT "\n", epc, tval);
+   show_regs(regs);
hang();
 }

@@ -65,7 +93,7 @@ int disable_interrupts(void)
return 0;
 }

-ulong handle_trap(ulong cause, ulong epc, struct pt_regs *regs)
+ulong handle_trap(ulong cause, ulong epc, ulong tval, struct pt_regs *regs)
 {
ulong is_irq, irq;

@@ -83,11 +111,11 @@ ulong handle_trap(ulong cause, ulong epc, struct pt_regs 
*regs)
timer_interrupt(0); /* handle timer interrupt */
break;
default:
-   _exit_trap(cause, epc, regs);
+   _exit_trap(cause, epc, tval, regs);
break;
};
} else {
-   _exit_trap(cause, epc, regs);
+   _exit_trap(cause, epc, tval, regs);
}

return epc;
-- 
2.24.1



Re: [PATCH 1/3] efi_loader: Add guidcpy function

2019-12-24 Thread Sughosh Ganu
On Tue, 24 Dec 2019 at 22:42, Heinrich Schuchardt 
wrote:

> On 12/24/19 4:54 PM, Sughosh Ganu wrote:
> > Add guidcpy function to copy the source guid to the destination
> > guid.
> >
> > Signed-off-by: Sughosh Ganu 
> > ---
> >   include/efi_loader.h | 5 +
> >   1 file changed, 5 insertions(+)
> >
> > diff --git a/include/efi_loader.h b/include/efi_loader.h
> > index 16a1b25..bec7873 100644
> > --- a/include/efi_loader.h
> > +++ b/include/efi_loader.h
> > @@ -17,6 +17,11 @@ static inline int guidcmp(const void *g1, const void
> *g2)
> >   return memcmp(g1, g2, sizeof(efi_guid_t));
> >   }
> >
> > +static inline void *guidcpy(efi_guid_t *dst, const efi_guid_t *src)
> > +{
> > + return memcpy(dst, src, sizeof(*dst));
>
> If we introduce this function, here are other places to use it:
>
> lib/efi_loader/efi_boottime.c:1404:
> memcpy(>protocol, protocol, sizeof(efi_guid_t));
> lib/efi_loader/efi_boottime.c:1635:
> memcpy([i].guid, guid, sizeof(*guid));
>

Ok. Will make changes to use guidcpy for these two instances as well in V2.

-sughosh


[PATCH] clk: Check that ops of composite clock components exist, before calling

2019-12-24 Thread Sean Anderson
clk_composite_ops was shared between all devices in the composite clock driver.
If one clock had a feature (such as supporting set_parent) which another clock
did not, it could call a null pointer dereference.

This patch does three things
1. It adds null-pointer checks to all composite clock functions.
2. It makes clk_composite_ops const and sets its functions at compile-time.
3. It adds some basic sanity checks to num_parents.

The combined effect of these changes is that any of mux, rate, or gate can be
NULL, and composite clocks will still function normally. Previously, at least
mux had to exist, since clk_composite_get_parent was used to determine the
parent for clk_register.

Signed-off-by: Sean Anderson 
---
 drivers/clk/clk-composite.c | 57 +++--
 1 file changed, 35 insertions(+), 22 deletions(-)

diff --git a/drivers/clk/clk-composite.c b/drivers/clk/clk-composite.c
index d0f273d47f..cb2ab6d83b 100644
--- a/drivers/clk/clk-composite.c
+++ b/drivers/clk/clk-composite.c
@@ -22,7 +22,10 @@ static u8 clk_composite_get_parent(struct clk *clk)
(struct clk *)dev_get_clk_ptr(clk->dev) : clk);
struct clk *mux = composite->mux;

-   return clk_mux_get_parent(mux);
+   if (mux)
+   return clk_mux_get_parent(mux);
+   else
+   return 0;
 }

 static int clk_composite_set_parent(struct clk *clk, struct clk *parent)
@@ -32,7 +35,10 @@ static int clk_composite_set_parent(struct clk *clk, struct 
clk *parent)
const struct clk_ops *mux_ops = composite->mux_ops;
struct clk *mux = composite->mux;

-   return mux_ops->set_parent(mux, parent);
+   if (mux && mux_ops)
+   return mux_ops->set_parent(mux, parent);
+   else
+   return -ENOTSUPP;
 }

 static unsigned long clk_composite_recalc_rate(struct clk *clk)
@@ -42,7 +48,10 @@ static unsigned long clk_composite_recalc_rate(struct clk 
*clk)
const struct clk_ops *rate_ops = composite->rate_ops;
struct clk *rate = composite->rate;

-   return rate_ops->get_rate(rate);
+   if (rate && rate_ops)
+   return rate_ops->get_rate(rate);
+   else
+   return clk_get_parent_rate(clk);
 }

 static ulong clk_composite_set_rate(struct clk *clk, unsigned long rate)
@@ -52,7 +61,10 @@ static ulong clk_composite_set_rate(struct clk *clk, 
unsigned long rate)
const struct clk_ops *rate_ops = composite->rate_ops;
struct clk *clk_rate = composite->rate;

-   return rate_ops->set_rate(clk_rate, rate);
+   if (rate && rate_ops)
+   return rate_ops->set_rate(clk_rate, rate);
+   else
+   return -ENOTSUPP;
 }

 static int clk_composite_enable(struct clk *clk)
@@ -62,7 +74,10 @@ static int clk_composite_enable(struct clk *clk)
const struct clk_ops *gate_ops = composite->gate_ops;
struct clk *gate = composite->gate;

-   return gate_ops->enable(gate);
+   if (gate && gate_ops)
+   return gate_ops->enable(gate);
+   else
+   return -ENOTSUPP;
 }

 static int clk_composite_disable(struct clk *clk)
@@ -72,15 +87,12 @@ static int clk_composite_disable(struct clk *clk)
const struct clk_ops *gate_ops = composite->gate_ops;
struct clk *gate = composite->gate;

-   gate_ops->disable(gate);
-
-   return 0;
+   if (gate && gate_ops)
+   return gate_ops->disable(gate);
+   else
+   return -ENOTSUPP;
 }

-struct clk_ops clk_composite_ops = {
-   /* This will be set according to clk_register_composite */
-};
-
 struct clk *clk_register_composite(struct device *dev, const char *name,
   const char * const *parent_names,
   int num_parents, struct clk *mux,
@@ -94,7 +106,9 @@ struct clk *clk_register_composite(struct device *dev, const 
char *name,
struct clk *clk;
struct clk_composite *composite;
int ret;
-   struct clk_ops *composite_ops = _composite_ops;
+
+   if (!num_parents || (num_parents == 1 && !mux))
+   return ERR_PTR(-EINVAL);

composite = kzalloc(sizeof(*composite), GFP_KERNEL);
if (!composite)
@@ -103,8 +117,6 @@ struct clk *clk_register_composite(struct device *dev, 
const char *name,
if (mux && mux_ops) {
composite->mux = mux;
composite->mux_ops = mux_ops;
-   if (mux_ops->set_parent)
-   composite_ops->set_parent = clk_composite_set_parent;
mux->data = (ulong)composite;
}

@@ -113,11 +125,6 @@ struct clk *clk_register_composite(struct device *dev, 
const char *name,
clk = ERR_PTR(-EINVAL);
goto err;
}
-   composite_ops->get_rate = clk_composite_recalc_rate;
-
-   /* .set_rate requires either .round_rate or .determine_rate */
-   

[PATCH] clk: Always use the supplied struct clk

2019-12-24 Thread Sean Anderson
CCF clocks should always use the struct clock passed to their methods for
extracting the driver-specific clock information struct. Previously, many
functions would use the clk->dev->priv if the device was bound. This could cause
problems with composite clocks. The individual clocks in a composite clock did
not have the ->dev field filled in. This was fine, because the device-specific
clock information would be used. However, since there was no ->dev, there was no
way to get the parent clock. This caused the recalc_rate method of the CCF
divider clock to fail. One option would be to use the clk->priv field to get the
composite clock and from there get the appropriate parent device. However, this
would tie the implementation to the composite clock. In general, different
devices should not rely on the contents of ->priv from another device.

The simple solution to this problem is to just always use the supplied struct
clock. The composite clock now fills in the ->dev pointer of its child clocks.
This allows child clocks to make calls like clk_get_parent() without issue.

imx avoided the above problem by using a custom get_rate function with composite
clocks.

Signed-off-by: Sean Anderson 
---
 drivers/clk/clk-composite.c|  8 
 drivers/clk/clk-divider.c  |  6 ++
 drivers/clk/clk-fixed-factor.c |  3 +--
 drivers/clk/clk-gate.c |  6 ++
 drivers/clk/clk-gate2.c|  4 ++--
 drivers/clk/clk-mux.c  | 12 
 6 files changed, 19 insertions(+), 20 deletions(-)

diff --git a/drivers/clk/clk-composite.c b/drivers/clk/clk-composite.c
index a5626c33d1..d0f273d47f 100644
--- a/drivers/clk/clk-composite.c
+++ b/drivers/clk/clk-composite.c
@@ -145,6 +145,14 @@ struct clk *clk_register_composite(struct device *dev, 
const char *name,
goto err;
}

+   if (composite->mux)
+   composite->mux->dev = clk->dev;
+   if (composite->rate)
+   composite->rate->dev = clk->dev;
+   if (composite->gate)
+   composite->gate->dev = clk->dev;
+
+
return clk;

 err:
diff --git a/drivers/clk/clk-divider.c b/drivers/clk/clk-divider.c
index 822e09b084..bfa05f24a3 100644
--- a/drivers/clk/clk-divider.c
+++ b/drivers/clk/clk-divider.c
@@ -70,8 +70,7 @@ unsigned long divider_recalc_rate(struct clk *hw, unsigned 
long parent_rate,

 static ulong clk_divider_recalc_rate(struct clk *clk)
 {
-   struct clk_divider *divider = to_clk_divider(clk_dev_binded(clk) ?
-   dev_get_clk_ptr(clk->dev) : clk);
+   struct clk_divider *divider = to_clk_divider(clk);
unsigned long parent_rate = clk_get_parent_rate(clk);
unsigned int val;

@@ -150,8 +149,7 @@ int divider_get_val(unsigned long rate, unsigned long 
parent_rate,

 static ulong clk_divider_set_rate(struct clk *clk, unsigned long rate)
 {
-   struct clk_divider *divider = to_clk_divider(clk_dev_binded(clk) ?
-   dev_get_clk_ptr(clk->dev) : clk);
+   struct clk_divider *divider = to_clk_divider(clk);
unsigned long parent_rate = clk_get_parent_rate(clk);
int value;
u32 val;
diff --git a/drivers/clk/clk-fixed-factor.c b/drivers/clk/clk-fixed-factor.c
index 711b0588bc..d2401cf440 100644
--- a/drivers/clk/clk-fixed-factor.c
+++ b/drivers/clk/clk-fixed-factor.c
@@ -18,8 +18,7 @@

 static ulong clk_factor_recalc_rate(struct clk *clk)
 {
-   struct clk_fixed_factor *fix =
-   to_clk_fixed_factor(dev_get_clk_ptr(clk->dev));
+   struct clk_fixed_factor *fix = to_clk_fixed_factor(clk);
unsigned long parent_rate = clk_get_parent_rate(clk);
unsigned long long int rate;

diff --git a/drivers/clk/clk-gate.c b/drivers/clk/clk-gate.c
index 70b8794554..b2933bc24a 100644
--- a/drivers/clk/clk-gate.c
+++ b/drivers/clk/clk-gate.c
@@ -43,8 +43,7 @@
  */
 static void clk_gate_endisable(struct clk *clk, int enable)
 {
-   struct clk_gate *gate = to_clk_gate(clk_dev_binded(clk) ?
-   dev_get_clk_ptr(clk->dev) : clk);
+   struct clk_gate *gate = to_clk_gate(clk);
int set = gate->flags & CLK_GATE_SET_TO_DISABLE ? 1 : 0;
u32 reg;

@@ -86,8 +85,7 @@ static int clk_gate_disable(struct clk *clk)

 int clk_gate_is_enabled(struct clk *clk)
 {
-   struct clk_gate *gate = to_clk_gate(clk_dev_binded(clk) ?
-   dev_get_clk_ptr(clk->dev) : clk);
+   struct clk_gate *gate = to_clk_gate(clk);
u32 reg;

 #if CONFIG_IS_ENABLED(SANDBOX_CLK_CCF)
diff --git a/drivers/clk/clk-gate2.c b/drivers/clk/clk-gate2.c
index 565d9e49ca..93ec862ad9 100644
--- a/drivers/clk/clk-gate2.c
+++ b/drivers/clk/clk-gate2.c
@@ -25,7 +25,7 @@

 static int clk_gate2_enable(struct clk *clk)
 {
-   struct clk_gate2 *gate = to_clk_gate2(dev_get_clk_ptr(clk->dev));
+   struct clk_gate2 *gate = to_clk_gate2(clk);
u32 reg;

reg = readl(gate->reg);
@@ -42,7 +42,7 @@ static int clk_gate2_enable(struct clk *clk)

 

[PATCH 2/2] clk: Add option to restrict clk-gate2 to one bit toggle

2019-12-24 Thread Sean Anderson
Currently clk-gate2 only works on clocks with a 2-bit enable/disable state. This
patch adds an option to work with clocks with a 1-bit enable.

Signed-off-by: Sean Anderson 
---
 drivers/clk/clk-gate2.c  | 13 ++---
 include/linux/clk-provider.h |  2 ++
 2 files changed, 12 insertions(+), 3 deletions(-)

diff --git a/drivers/clk/clk-gate2.c b/drivers/clk/clk-gate2.c
index cfe21e5496..565d9e49ca 100644
--- a/drivers/clk/clk-gate2.c
+++ b/drivers/clk/clk-gate2.c
@@ -29,8 +29,12 @@ static int clk_gate2_enable(struct clk *clk)
u32 reg;

reg = readl(gate->reg);
-   reg &= ~(3 << gate->bit_idx);
-   reg |= gate->cgr_val << gate->bit_idx;
+   if (gate->flags & CLK_GATE2_SINGLE_BIT) {
+   reg |= BIT(gate->bit_idx);
+   } else {
+   reg &= ~(3 << gate->bit_idx);
+   reg |= gate->cgr_val << gate->bit_idx;
+   }
writel(reg, gate->reg);

return 0;
@@ -42,7 +46,10 @@ static int clk_gate2_disable(struct clk *clk)
u32 reg;

reg = readl(gate->reg);
-   reg &= ~(3 << gate->bit_idx);
+   if (gate->flags & CLK_GATE2_SINGLE_BIT)
+   reg &= ~BIT(gate->bit_idx);
+   else
+   reg &= ~(3 << gate->bit_idx);
writel(reg, gate->reg);

return 0;
diff --git a/include/linux/clk-provider.h b/include/linux/clk-provider.h
index f510291018..c904586e2c 100644
--- a/include/linux/clk-provider.h
+++ b/include/linux/clk-provider.h
@@ -155,6 +155,8 @@ struct clk_composite {

 #define to_clk_composite(_clk) container_of(_clk, struct clk_composite, clk)

+#define CLK_GATE2_SINGLE_BIT BIT(0)
+
 struct clk_gate2 {
struct clk clk;
void __iomem*reg;
-- 
2.24.1



[PATCH 1/2] clk: Move clk-gate2 to clock driver directory

2019-12-24 Thread Sean Anderson
Make clk-gate2 available for use outside of imx.

Signed-off-by: Sean Anderson 
---
 drivers/clk/Makefile  |  1 +
 drivers/clk/{imx => }/clk-gate2.c | 20 
 drivers/clk/imx/Makefile  |  2 +-
 drivers/clk/imx/clk.h |  5 -
 include/linux/clk-provider.h  | 24 
 5 files changed, 26 insertions(+), 26 deletions(-)
 rename drivers/clk/{imx => }/clk-gate2.c (85%)

diff --git a/drivers/clk/Makefile b/drivers/clk/Makefile
index 06131edb9f..fef3280f16 100644
--- a/drivers/clk/Makefile
+++ b/drivers/clk/Makefile
@@ -9,6 +9,7 @@ obj-$(CONFIG_$(SPL_TPL_)CLK) += clk_fixed_rate.o
 obj-$(CONFIG_$(SPL_TPL_)CLK) += clk_fixed_factor.o
 obj-$(CONFIG_$(SPL_TPL_)CLK_CCF) += clk.o clk-divider.o clk-mux.o clk-gate.o
 obj-$(CONFIG_$(SPL_TPL_)CLK_CCF) += clk-fixed-factor.o
+obj-$(CONFIG_$(SPL_TPL_)CLK_CCF) += clk-gate2.o
 obj-$(CONFIG_$(SPL_TPL_)CLK_COMPOSITE_CCF) += clk-composite.o

 obj-y += analogbits/
diff --git a/drivers/clk/imx/clk-gate2.c b/drivers/clk/clk-gate2.c
similarity index 85%
rename from drivers/clk/imx/clk-gate2.c
rename to drivers/clk/clk-gate2.c
index 1b9db6e791..cfe21e5496 100644
--- a/drivers/clk/imx/clk-gate2.c
+++ b/drivers/clk/clk-gate2.c
@@ -10,8 +10,7 @@
  * it under the terms of the GNU General Public License version 2 as
  * published by the Free Software Foundation.
  *
- * Gated clock implementation
- *
+ * Gated clock which redirects rate functions to its parent clock
  */

 #include 
@@ -21,19 +20,8 @@
 #include 
 #include 
 #include 
-#include "clk.h"
-
-#define UBOOT_DM_CLK_IMX_GATE2 "imx_clk_gate2"
-
-struct clk_gate2 {
-   struct clk clk;
-   void __iomem*reg;
-   u8  bit_idx;
-   u8  cgr_val;
-   u8  flags;
-};

-#define to_clk_gate2(_clk) container_of(_clk, struct clk_gate2, clk)
+#define UBOOT_DM_CLK_GATE2 "clk_gate2"

 static int clk_gate2_enable(struct clk *clk)
 {
@@ -97,7 +85,7 @@ struct clk *clk_register_gate2(struct device *dev, const char 
*name,

clk = >clk;

-   ret = clk_register(clk, UBOOT_DM_CLK_IMX_GATE2, name, parent_name);
+   ret = clk_register(clk, UBOOT_DM_CLK_GATE2, name, parent_name);
if (ret) {
kfree(gate);
return ERR_PTR(ret);
@@ -107,7 +95,7 @@ struct clk *clk_register_gate2(struct device *dev, const 
char *name,
 }

 U_BOOT_DRIVER(clk_gate2) = {
-   .name   = UBOOT_DM_CLK_IMX_GATE2,
+   .name   = UBOOT_DM_CLK_GATE2,
.id = UCLASS_CLK,
.ops= _gate2_ops,
.flags = DM_FLAG_PRE_RELOC,
diff --git a/drivers/clk/imx/Makefile b/drivers/clk/imx/Makefile
index 222c5a4e08..5328e9265a 100644
--- a/drivers/clk/imx/Makefile
+++ b/drivers/clk/imx/Makefile
@@ -2,7 +2,7 @@
 #
 # SPDX-License-Identifier: GPL-2.0

-obj-$(CONFIG_$(SPL_TPL_)CLK_CCF) += clk-gate2.o clk-pllv3.o clk-pfd.o
+obj-$(CONFIG_$(SPL_TPL_)CLK_CCF) += clk-pllv3.o clk-pfd.o
 obj-$(CONFIG_$(SPL_TPL_)CLK_IMX6Q) += clk-imx6q.o
 obj-$(CONFIG_CLK_IMX8) += clk-imx8.o

diff --git a/drivers/clk/imx/clk.h b/drivers/clk/imx/clk.h
index 07dcf94ea5..c46570e9f4 100644
--- a/drivers/clk/imx/clk.h
+++ b/drivers/clk/imx/clk.h
@@ -45,11 +45,6 @@ struct clk *imx_clk_pll14xx(const char *name, const char 
*parent_name,
void __iomem *base,
const struct imx_pll14xx_clk *pll_clk);

-struct clk *clk_register_gate2(struct device *dev, const char *name,
-   const char *parent_name, unsigned long flags,
-   void __iomem *reg, u8 bit_idx, u8 cgr_val,
-   u8 clk_gate_flags);
-
 struct clk *imx_clk_pllv3(enum imx_pllv3_type type, const char *name,
  const char *parent_name, void __iomem *base,
  u32 div_mask);
diff --git a/include/linux/clk-provider.h b/include/linux/clk-provider.h
index 0ef6e685ad..f510291018 100644
--- a/include/linux/clk-provider.h
+++ b/include/linux/clk-provider.h
@@ -90,10 +90,6 @@ struct clk_gate {
 #define CLK_GATE_HIWORD_MASK   BIT(1)

 extern const struct clk_ops clk_gate_ops;
-struct clk *clk_register_gate(struct device *dev, const char *name,
- const char *parent_name, unsigned long flags,
- void __iomem *reg, u8 bit_idx,
- u8 clk_gate_flags, spinlock_t *lock);

 struct clk_div_table {
unsigned intval;
@@ -159,6 +155,16 @@ struct clk_composite {

 #define to_clk_composite(_clk) container_of(_clk, struct clk_composite, clk)

+struct clk_gate2 {
+   struct clk clk;
+   void __iomem*reg;
+   u8  bit_idx;
+   u8  cgr_val;
+   u8  flags;
+};
+
+#define to_clk_gate2(_clk) container_of(_clk, struct clk_gate2, clk)
+
 struct clk *clk_register_composite(struct device *dev, const char *name,
const char * const *parent_names, int num_parents,
struct clk *mux_clk, const 

[PATCH 0/2] clk: Generalize clock_gate2 for use outside of imx

2019-12-24 Thread Sean Anderson
I'm implementing a clock device and I would like to use the clock_gate2
from imx. This patch series adapts it to general use. An alternative is
to use the composite clock, but it is unsuitable for clocks with no mux.
I have a patch to adapt the composite to work without muxes (which will
be sent next), but I figured this might be useful on its own.

Sean Anderson (2):
  clk: Move clk-gate2 to clock driver directory
  clk: Add option to restrict clk-gate2 to one bit toggle

 drivers/clk/Makefile  |  1 +
 drivers/clk/{imx => }/clk-gate2.c | 33 +--
 drivers/clk/imx/Makefile  |  2 +-
 drivers/clk/imx/clk.h |  5 -
 include/linux/clk-provider.h  | 26 
 5 files changed, 38 insertions(+), 29 deletions(-)
 rename drivers/clk/{imx => }/clk-gate2.c (81%)

-- 
2.24.1





[PATCH] clk: Fix error checking of dev_read_addr_ptr

2019-12-24 Thread Sean Anderson
dev_read_addr_ptr returns NULL on error, not FDT_ADDR_T_NONE.

Signed-off-by: Sean Anderson 
---
 drivers/clk/imx/clk-imx6q.c  | 2 +-
 drivers/clk/imx/clk-imx8mm.c | 2 +-
 drivers/clk/imx/clk-imx8mn.c | 2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/drivers/clk/imx/clk-imx6q.c b/drivers/clk/imx/clk-imx6q.c
index 5ae4781d11..bd0d3e4f47 100644
--- a/drivers/clk/imx/clk-imx6q.c
+++ b/drivers/clk/imx/clk-imx6q.c
@@ -115,7 +115,7 @@ static int imx6q_clk_probe(struct udevice *dev)

/* CCM clocks */
base = dev_read_addr_ptr(dev);
-   if (base == (void *)FDT_ADDR_T_NONE)
+   if (!base)
return -EINVAL;

clk_dm(IMX6QDL_CLK_USDHC1_SEL,
diff --git a/drivers/clk/imx/clk-imx8mm.c b/drivers/clk/imx/clk-imx8mm.c
index a05dac7c7a..fc41a028f6 100644
--- a/drivers/clk/imx/clk-imx8mm.c
+++ b/drivers/clk/imx/clk-imx8mm.c
@@ -323,7 +323,7 @@ static int imx8mm_clk_probe(struct udevice *dev)
   imx_clk_fixed_factor("sys_pll2_1000m", "sys_pll2_out", 1, 1));

base = dev_read_addr_ptr(dev);
-   if (base == (void *)FDT_ADDR_T_NONE)
+   if (!base)
return -EINVAL;

clk_dm(IMX8MM_CLK_A53_SRC,
diff --git a/drivers/clk/imx/clk-imx8mn.c b/drivers/clk/imx/clk-imx8mn.c
index 4048cc63cc..eb43971ae6 100644
--- a/drivers/clk/imx/clk-imx8mn.c
+++ b/drivers/clk/imx/clk-imx8mn.c
@@ -293,7 +293,7 @@ static int imx8mn_clk_probe(struct udevice *dev)
   imx_clk_fixed_factor("sys_pll2_1000m", "sys_pll2_out", 1, 1));

base = dev_read_addr_ptr(dev);
-   if (base == (void *)FDT_ADDR_T_NONE)
+   if (!base)
return -EINVAL;

clk_dm(IMX8MN_CLK_A53_SRC,
-- 
2.24.1



clk: Include missing headers for linux/clk-provider.h

2019-12-24 Thread Sean Anderson
This header was missing a couple of include dependencies when included on its
own.

Signed-off-by: Sean Anderson 
---
 include/linux/clk-provider.h | 4 
 1 file changed, 4 insertions(+)

diff --git a/include/linux/clk-provider.h b/include/linux/clk-provider.h
index 02ff1a311a..0ef6e685ad 100644
--- a/include/linux/clk-provider.h
+++ b/include/linux/clk-provider.h
@@ -8,6 +8,10 @@
  */
 #ifndef __LINUX_CLK_PROVIDER_H
 #define __LINUX_CLK_PROVIDER_H
+
+#include 
+#include 
+#include 
 #include 

 static inline void clk_dm(ulong id, struct clk *clk)
-- 
2.24.1



[PATCH] log: Include missing header for log.h

2019-12-24 Thread Sean Anderson
log.h references cmd_tbl_t but command.h was not included

Signed-off-by: Sean Anderson 
---
 include/log.h | 1 +
 1 file changed, 1 insertion(+)

diff --git a/include/log.h b/include/log.h
index d8f18a6afd..820cdcc3a2 100644
--- a/include/log.h
+++ b/include/log.h
@@ -9,6 +9,7 @@
 #ifndef __LOG_H
 #define __LOG_H

+#include 
 #include 
 #include 

-- 
2.24.1



[PATCH] dm: Add a debug message when devices are skipped pre-reloc

2019-12-24 Thread Sean Anderson
This adds a message to lists_bind_fdt when it skips initializing a device
pre-relocation. I've had a couple errors where a device didn't initialize
properly because one of its dependencies was missing.

Signed-off-by: Sean Anderson 
---
 drivers/core/lists.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/drivers/core/lists.c b/drivers/core/lists.c
index 4681b3e5dd..68204c303f 100644
--- a/drivers/core/lists.c
+++ b/drivers/core/lists.c
@@ -176,8 +176,10 @@ int lists_bind_fdt(struct udevice *parent, ofnode node, 
struct udevice **devp,

if (pre_reloc_only) {
if (!dm_ofnode_pre_reloc(node) &&
-   !(entry->flags & DM_FLAG_PRE_RELOC))
+   !(entry->flags & DM_FLAG_PRE_RELOC)) {
+   log_debug("Skipping device pre-relocation\n");
return 0;
+   }
}

log_debug("   - found match at '%s': '%s' matches '%s'\n",
-- 
2.24.1



Re: [RFC 1/1] efi_loader: support building UEFI binaries on sandbox

2019-12-24 Thread Simon Glass
On Thu, 7 Nov 2019 at 17:34, Heinrich Schuchardt  wrote:
>
> On the sandbox the UEFI binaries must match the host architectures.
>
> Adjust the Makefiles. Provide the PE/COFF header and relocation files.
>
> Allow building helloworld.efi on the sandbox.
>
> Signed-off-by: Heinrich Schuchardt 
> ---
> include/host_arch.h provokes some false positives in checkpatch.pl as this
> include is also meant to be used in Makefile.
> ---
>  Makefile | 19 +
>  arch/sandbox/config.mk   | 28 
>  arch/sandbox/lib/crt0_sandbox_efi.S  | 32 
>  arch/sandbox/lib/reloc_sandbox_efi.c | 32 
>  cmd/Kconfig  |  2 +-
>  include/host_arch.h  | 24 +
>  lib/efi_loader/Makefile  |  3 +++
>  7 files changed, 139 insertions(+), 1 deletion(-)
>  create mode 100644 arch/sandbox/lib/crt0_sandbox_efi.S
>  create mode 100644 arch/sandbox/lib/reloc_sandbox_efi.c
>  create mode 100644 include/host_arch.h

Reviewed-by: Simon Glass 

Do you need the #if 0 bit?

Regards,
Simon


Re: [PATCH v4 8/8] virtio: rng: Add a random number generator(rng) driver

2019-12-24 Thread Heinrich Schuchardt

On 12/24/19 10:39 PM, Heinrich Schuchardt wrote:

On 12/17/19 12:52 PM, Sughosh Ganu wrote:

Add a driver for the virtio-rng device on the qemu platform. The
device uses pci as a transport medium. The driver can be enabled with
the following configs

CONFIG_VIRTIO
CONFIG_DM_RNG
CONFIG_VIRTIO_PCI
CONFIG_VIRTIO_RNG

Signed-off-by: Sughosh Ganu 
---
  drivers/virtio/Kconfig |  6 
  drivers/virtio/Makefile    |  1 +
  drivers/virtio/virtio-uclass.c |  1 +
  drivers/virtio/virtio_rng.c    | 72
++
  include/virtio.h   |  4 ++-
  5 files changed, 83 insertions(+), 1 deletion(-)
  create mode 100644 drivers/virtio/virtio_rng.c

diff --git a/drivers/virtio/Kconfig b/drivers/virtio/Kconfig
index a9d5fd0..2e3dd3b 100644
--- a/drivers/virtio/Kconfig
+++ b/drivers/virtio/Kconfig
@@ -59,4 +59,10 @@ config VIRTIO_BLK
    This is the virtual block driver for virtio. It can be used with
    QEMU based targets.

+config VIRTIO_RNG
+   bool "virtio rng driver"
+   depends on VIRTIO
+   help
+ This is the virtual random number generator driver. It can
be used
+ with Qemu based targets.
  endmenu
diff --git a/drivers/virtio/Makefile b/drivers/virtio/Makefile
index 4579044..dc88809 100644
--- a/drivers/virtio/Makefile
+++ b/drivers/virtio/Makefile
@@ -9,3 +9,4 @@ obj-$(CONFIG_VIRTIO_PCI) += virtio_pci_legacy.o
virtio_pci_modern.o
  obj-$(CONFIG_VIRTIO_SANDBOX) += virtio_sandbox.o
  obj-$(CONFIG_VIRTIO_NET) += virtio_net.o
  obj-$(CONFIG_VIRTIO_BLK) += virtio_blk.o
+obj-$(CONFIG_VIRTIO_RNG) += virtio_rng.o
diff --git a/drivers/virtio/virtio-uclass.c
b/drivers/virtio/virtio-uclass.c
index 34397d7..436faa4 100644
--- a/drivers/virtio/virtio-uclass.c
+++ b/drivers/virtio/virtio-uclass.c
@@ -24,6 +24,7 @@
  static const char *const virtio_drv_name[VIRTIO_ID_MAX_NUM] = {
  [VIRTIO_ID_NET]    = VIRTIO_NET_DRV_NAME,
  [VIRTIO_ID_BLOCK]    = VIRTIO_BLK_DRV_NAME,
+    [VIRTIO_ID_RNG]    = VIRTIO_RNG_DRV_NAME,
  };

  int virtio_get_config(struct udevice *vdev, unsigned int offset,
diff --git a/drivers/virtio/virtio_rng.c b/drivers/virtio/virtio_rng.c
new file mode 100644
index 000..19a0cc1
--- /dev/null
+++ b/drivers/virtio/virtio_rng.c
@@ -0,0 +1,72 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (c) 2019, Linaro Limited
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+struct virtio_rng_priv {
+    struct virtqueue *rng_vq;
+};
+
+static int virtio_rng_read(struct udevice *dev, void *data, size_t len)
+{
+    struct virtio_sg sg = { data, len };
+    struct virtio_sg *sgs[] = {  };
+    struct virtio_rng_priv *priv = dev_get_priv(dev);
+    unsigned int rsize;
+    int ret;
+
+    ret = virtqueue_add(priv->rng_vq, sgs, 0, 1);
+    if (ret)


Please, add a debug statement for each error path, e.g.

     debug("%s: virtqueue_add() failed\n", __func__)


+    return ret;
+
+    virtqueue_kick(priv->rng_vq);
+
+    while (!virtqueue_get_buf(priv->rng_vq, ))
+    ;
+
+    return rsize;


The return value is inconsistent with sandbox_rng_read() and
stm32_rng_read(). Both return 0 on success.

With "return 0;" the driver works fine.


I retested with
-device virtio-rng-pci,disable-legacy=on,max-bytes=16,period=1000

In this case virtqueue_get_buf() only returns a maximum of 16 bytes. All
other bytes of data remain unchanged. If the next call occurs within
1000 milli-seconds virtqueue_get_buf() blocks until the period is over.

So either you have to change the definition of the return parameter in
patch 1/8 (and change the other drivers accordingly) or you have to add
a loop to this driver that loops until len random bytes have been retrieved.

I would prefer to add a loop in the driver instead of adding a loop in
every consumer.

Best regards

Heinrich




+}
+
+static int virtio_rng_bind(struct udevice *dev)
+{
+    struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(dev->parent);
+
+    /* Indicate what driver features we support */
+    virtio_driver_features_init(uc_priv, NULL, 0, NULL, 0);
+
+    return 0;
+}
+
+static int virtio_rng_probe(struct udevice *dev)
+{
+    struct virtio_rng_priv *priv = dev_get_priv(dev);
+    int ret;
+
+    ret = virtio_find_vqs(dev, 1, >rng_vq);
+    if (ret < 0)


Add a debug statement here, please, e.g.

     debug("%s: virtio_find_vqs() failed\n", __func__)

Best regards

Heinrich


+    return ret;
+
+    return 0;
+}
+
+static const struct dm_rng_ops virtio_rng_ops = {
+    .read    = virtio_rng_read,
+};
+
+U_BOOT_DRIVER(virtio_rng) = {
+    .name    = VIRTIO_RNG_DRV_NAME,
+    .id    = UCLASS_RNG,
+    .bind    = virtio_rng_bind,
+    .probe    = virtio_rng_probe,
+    .remove = virtio_reset,
+    .ops    = _rng_ops,
+    .priv_auto_alloc_size = sizeof(struct virtio_rng_priv),
+    .flags    = DM_FLAG_ACTIVE_DMA,
+};
diff --git a/include/virtio.h b/include/virtio.h
index 654fdf1..561dcc3 100644
--- a/include/virtio.h
+++ 

Re: [PATCH v4 1/8] dm: rng: Add random number generator(rng) uclass

2019-12-24 Thread Heinrich Schuchardt

On 12/17/19 12:51 PM, Sughosh Ganu wrote:

Add a uclass for reading a random number seed from a random number
generator device.

Signed-off-by: Sughosh Ganu 
Reviewed-by: Patrice Chotard 
---
Changes since V3:
  Handle review comments from Patrick Delaunay.

  drivers/Kconfig  |  2 ++
  drivers/Makefile |  1 +
  drivers/rng/Kconfig  |  7 +++
  drivers/rng/Makefile |  6 ++
  drivers/rng/rng-uclass.c | 23 +++
  include/dm/uclass-id.h   |  1 +
  include/rng.h| 32 
  7 files changed, 72 insertions(+)
  create mode 100644 drivers/rng/Kconfig
  create mode 100644 drivers/rng/Makefile
  create mode 100644 drivers/rng/rng-uclass.c
  create mode 100644 include/rng.h

diff --git a/drivers/Kconfig b/drivers/Kconfig
index 9d99ce0..e34a227 100644
--- a/drivers/Kconfig
+++ b/drivers/Kconfig
@@ -90,6 +90,8 @@ source "drivers/remoteproc/Kconfig"

  source "drivers/reset/Kconfig"

+source "drivers/rng/Kconfig"
+
  source "drivers/rtc/Kconfig"

  source "drivers/scsi/Kconfig"
diff --git a/drivers/Makefile b/drivers/Makefile
index e977f19..6c619b1 100644
--- a/drivers/Makefile
+++ b/drivers/Makefile
@@ -115,4 +115,5 @@ obj-$(CONFIG_W1_EEPROM) += w1-eeprom/

  obj-$(CONFIG_MACH_PIC32) += ddr/microchip/
  obj-$(CONFIG_DM_HWSPINLOCK) += hwspinlock/
+obj-$(CONFIG_DM_RNG) += rng/
  endif
diff --git a/drivers/rng/Kconfig b/drivers/rng/Kconfig
new file mode 100644
index 000..dd44cc0
--- /dev/null
+++ b/drivers/rng/Kconfig
@@ -0,0 +1,7 @@
+config DM_RNG
+   bool "Driver support for Random Number Generator devices"
+   depends on DM
+   help
+ Enable driver model for random number generator(rng) devices.
+ This interface is used to initialise the rng device and to
+ read the random seed from the device.
diff --git a/drivers/rng/Makefile b/drivers/rng/Makefile
new file mode 100644
index 000..311705b
--- /dev/null
+++ b/drivers/rng/Makefile
@@ -0,0 +1,6 @@
+# SPDX-License-Identifier: GPL-2.0+
+#
+# Copyright (c) 2019, Linaro Limited
+#
+
+obj-$(CONFIG_DM_RNG) += rng-uclass.o
diff --git a/drivers/rng/rng-uclass.c b/drivers/rng/rng-uclass.c
new file mode 100644
index 000..b6af3b8
--- /dev/null
+++ b/drivers/rng/rng-uclass.c
@@ -0,0 +1,23 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (c) 2019, Linaro Limited
+ */
+
+#include 
+#include 
+#include 
+
+int dm_rng_read(struct udevice *dev, void *buffer, size_t size)
+{
+   const struct dm_rng_ops *ops = device_get_ops(dev);
+
+   if (!ops->read)
+   return -ENOSYS;
+
+   return ops->read(dev, buffer, size);
+}
+
+UCLASS_DRIVER(rng) = {
+   .name = "rng",
+   .id = UCLASS_RNG,
+};
diff --git a/include/dm/uclass-id.h b/include/dm/uclass-id.h
index 0c563d8..192202d 100644
--- a/include/dm/uclass-id.h
+++ b/include/dm/uclass-id.h
@@ -86,6 +86,7 @@ enum uclass_id {
UCLASS_REGULATOR,   /* Regulator device */
UCLASS_REMOTEPROC,  /* Remote Processor device */
UCLASS_RESET,   /* Reset controller device */
+   UCLASS_RNG, /* Random Number Generator */
UCLASS_RTC, /* Real time clock device */
UCLASS_SCSI,/* SCSI device */
UCLASS_SERIAL,  /* Serial UART */
diff --git a/include/rng.h b/include/rng.h
new file mode 100644
index 000..80891fc
--- /dev/null
+++ b/include/rng.h
@@ -0,0 +1,32 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (c) 2019, Linaro Limited
+ */
+
+#if !defined _RNG_H_
+#define _RNG_H_
+
+#include 
+
+/**
+ * dm_rng_read() - read a random number seed from the rng device
+ * @buffer:input buffer to put the read random seed into
+ * @size:  number of bytes of random seed read
+ * @return 0 if OK, -ve on error


Please, stick to the Spinx syntax (Return:). See
https://www.kernel.org/doc/html/latest/doc-guide/kernel-doc.html#function-documentation

The Linux kernel code uses the return value to return the number of
bytes retrieved.

Please, provide as description here of what will happen if the RNG
device blocks.


+ *
+ */
+int dm_rng_read(struct udevice *dev, void *buffer, size_t size);


We have different places where we retrieve the first RNG device and read
from it. Shouldn't we provide a single function in the uclass that
returns random bytes without a udevice parameter?

Linux uses an entropy pool where the hardware RNG is one of multiple
sources of randomness and the entropy runs through hash functions
(chacha20, sha1) before being consumed.

Shouldn't we call a hashing function in this uclass?

Shouldn't we further provide a function that can later be used to add
additional sources of entropy? E.g. a seed file, the MAC address, timing
of network accesses.

Please, take a look at Linux' drivers/char/random.c.

Best regards

Heinrich


+
+/* struct dm_rng_ops - Operations for the hwrng uclass */
+struct dm_rng_ops {
+   /**
+* @read() - read a random 

[PATCH 1/1] cmd: add rng command

2019-12-24 Thread Heinrich Schuchardt
For the RNG uclass we currently only have a test working on the sandbox.

Provide a command to test the hardware random number generator on
non-sandbox systems.

Signed-off-by: Heinrich Schuchardt 
---
This patch depends on Sughosh Ganu's:
[PATCH v4 8/8] virtio: rng: Add a random number generator(rng) driver
https://lists.denx.de/pipermail/u-boot/2019-December/394018.html
---
 cmd/Kconfig  |  7 +++
 cmd/Makefile |  1 +
 cmd/rng.c| 56 
 3 files changed, 64 insertions(+)
 create mode 100644 cmd/rng.c

diff --git a/cmd/Kconfig b/cmd/Kconfig
index 1e4cf146c5..d39cfaa13b 100644
--- a/cmd/Kconfig
+++ b/cmd/Kconfig
@@ -1657,6 +1657,13 @@ config CMD_GETTIME
  milliseconds. See also the 'bootstage' command which provides more
  flexibility for boot timing.

+config CMD_RNG
+   bool "rng command"
+   depends on DM_RNG
+   select HEXDUMP
+   help
+ Print bytes from the hardware random number generator.
+
 # TODO: rename to CMD_SLEEP
 config CMD_MISC
bool "sleep"
diff --git a/cmd/Makefile b/cmd/Makefile
index 3ac7104546..7cb182d915 100644
--- a/cmd/Makefile
+++ b/cmd/Makefile
@@ -116,6 +116,7 @@ obj-$(CONFIG_CMD_READ) += read.o
 obj-$(CONFIG_CMD_REGINFO) += reginfo.o
 obj-$(CONFIG_CMD_REISER) += reiser.o
 obj-$(CONFIG_CMD_REMOTEPROC) += remoteproc.o
+obj-$(CONFIG_CMD_RNG) += rng.o
 obj-$(CONFIG_CMD_ROCKUSB) += rockusb.o
 obj-$(CONFIG_SANDBOX) += host.o
 obj-$(CONFIG_CMD_SATA) += sata.o
diff --git a/cmd/rng.c b/cmd/rng.c
new file mode 100644
index 00..ce91af77a8
--- /dev/null
+++ b/cmd/rng.c
@@ -0,0 +1,56 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * The 'rng' command prints bytes from the hardware random number generator.
+ *
+ * Copyright (c) 2019, Heinrich Schuchardt 
+ */
+#include 
+#include 
+#include 
+#include 
+#include 
+
+static int do_rng(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+{
+   size_t n = 0x40;
+   struct udevice *dev;
+   void *buf;
+   int ret = CMD_RET_SUCCESS;
+
+   if (uclass_get_device(UCLASS_RNG, 0, ) || !dev) {
+   printf("No RNG device\n");
+   return CMD_RET_FAILURE;
+   }
+
+   if (argc >= 2)
+   n = simple_strtoul(argv[1], NULL, 16);
+
+   buf = malloc(n);
+   if (!buf) {
+   printf("Out of memory\n");
+   return CMD_RET_FAILURE;
+   }
+
+   if (dm_rng_read(dev, buf, n)) {
+   printf("Reading RNG failed\n");
+   ret = CMD_RET_FAILURE;
+   } else {
+   print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, buf, n);
+   }
+
+   free(buf);
+
+   return ret;
+}
+
+#ifdef CONFIG_SYS_LONGHELP
+static char rng_help_text[] =
+   "[n]\n"
+   "  - print n random bytes\n";
+#endif
+
+U_BOOT_CMD(
+   rng, 2, 0, do_rng,
+   "print bytes from the hardware random number generator",
+   rng_help_text
+);
--
2.24.0



Re: [PATCH v4 5/8] sandbox: rng: Add a random number generator(rng) driver

2019-12-24 Thread Heinrich Schuchardt

Resending with Cc Tom, Simon, Ilias

On 12/17/19 12:52 PM, Sughosh Ganu wrote:

Add a sandbox driver for random number generation. Mostly aimed at
providing a unit test for rng uclass.

Signed-off-by: Sughosh Ganu 
Reviewed-by: Patrice Chotard 
---
Changes since V3:
  Handle review comments from Patrick Delaunay.

  arch/sandbox/dts/test.dts |  4 
  drivers/rng/Kconfig   |  7 +++
  drivers/rng/Makefile  |  1 +
  drivers/rng/sandbox_rng.c | 39 +++
  4 files changed, 51 insertions(+)
  create mode 100644 drivers/rng/sandbox_rng.c

diff --git a/arch/sandbox/dts/test.dts b/arch/sandbox/dts/test.dts
index fdb08f2..2c85540 100644
--- a/arch/sandbox/dts/test.dts
+++ b/arch/sandbox/dts/test.dts
@@ -599,6 +599,10 @@
reset-names = "other", "test";
};

+   rng@0 {
+   compatible = "sandbox,sandbox-rng";
+   };
+
rproc_1: rproc@1 {
compatible = "sandbox,test-processor";
remoteproc-name = "remoteproc-test-dev1";
diff --git a/drivers/rng/Kconfig b/drivers/rng/Kconfig
index 5fc11db..3a1d3f0 100644
--- a/drivers/rng/Kconfig
+++ b/drivers/rng/Kconfig
@@ -6,6 +6,13 @@ config DM_RNG
  This interface is used to initialise the rng device and to
  read the random seed from the device.

+config RNG_SANDBOX
+   bool "Sandbox random number generator"
+   depends on SANDBOX && DM_RNG
+   help
+ Enable random number generator for sandbox. This is an
+emulation of a rng device.
+
  config RNG_STM32MP1
 bool "Enable random number generator for STM32MP1"
 depends on ARCH_STM32MP && DM_RNG
diff --git a/drivers/rng/Makefile b/drivers/rng/Makefile
index 699beb3..3517005 100644
--- a/drivers/rng/Makefile
+++ b/drivers/rng/Makefile
@@ -4,4 +4,5 @@
  #

  obj-$(CONFIG_DM_RNG) += rng-uclass.o
+obj-$(CONFIG_RNG_SANDBOX) += sandbox_rng.o
  obj-$(CONFIG_RNG_STM32MP1) += stm32mp1_rng.o
diff --git a/drivers/rng/sandbox_rng.c b/drivers/rng/sandbox_rng.c
new file mode 100644
index 000..8262e0d
--- /dev/null
+++ b/drivers/rng/sandbox_rng.c
@@ -0,0 +1,39 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2019, Linaro Limited
+ */
+
+#include 
+#include 
+#include 
+
+static unsigned long random = 0xdeadbeef;
+
+static int sandbox_rng_read(struct udevice *dev, void *data, size_t len)
+{
+   if (len != sizeof(random))
+   return -EINVAL;


This will drop on our feet once we use the RNG class for anything real.

Just imagine an EFI_RNG_PROTOCOL test failing on the sandbox because we
cannot read 128 bit.

Can't we use a PRNG here and reinitialize it when len == ~0UL?

Functions rand() and srand() are good enough for the job.

Best regards

Heinrich


+
+   random ^= ~0UL;
+   *(unsigned long *)data = random;
+
+   return 0;
+}
+
+static const struct dm_rng_ops sandbox_rng_ops = {
+   .read = sandbox_rng_read,
+};
+
+static const struct udevice_id sandbox_rng_match[] = {
+   {
+   .compatible = "sandbox,sandbox-rng",
+   },
+   {},
+};
+
+U_BOOT_DRIVER(sandbox_rng) = {
+   .name = "sandbox-rng",
+   .id = UCLASS_RNG,
+   .of_match = sandbox_rng_match,
+   .ops = _rng_ops,
+};






Re: [PATCH v4 5/8] sandbox: rng: Add a random number generator(rng) driver

2019-12-24 Thread Heinrich Schuchardt

On 12/17/19 12:52 PM, Sughosh Ganu wrote:

Add a sandbox driver for random number generation. Mostly aimed at
providing a unit test for rng uclass.

Signed-off-by: Sughosh Ganu 
Reviewed-by: Patrice Chotard 
---
Changes since V3:
  Handle review comments from Patrick Delaunay.

  arch/sandbox/dts/test.dts |  4 
  drivers/rng/Kconfig   |  7 +++
  drivers/rng/Makefile  |  1 +
  drivers/rng/sandbox_rng.c | 39 +++
  4 files changed, 51 insertions(+)
  create mode 100644 drivers/rng/sandbox_rng.c

diff --git a/arch/sandbox/dts/test.dts b/arch/sandbox/dts/test.dts
index fdb08f2..2c85540 100644
--- a/arch/sandbox/dts/test.dts
+++ b/arch/sandbox/dts/test.dts
@@ -599,6 +599,10 @@
reset-names = "other", "test";
};

+   rng@0 {
+   compatible = "sandbox,sandbox-rng";
+   };
+
rproc_1: rproc@1 {
compatible = "sandbox,test-processor";
remoteproc-name = "remoteproc-test-dev1";
diff --git a/drivers/rng/Kconfig b/drivers/rng/Kconfig
index 5fc11db..3a1d3f0 100644
--- a/drivers/rng/Kconfig
+++ b/drivers/rng/Kconfig
@@ -6,6 +6,13 @@ config DM_RNG
  This interface is used to initialise the rng device and to
  read the random seed from the device.

+config RNG_SANDBOX
+   bool "Sandbox random number generator"
+   depends on SANDBOX && DM_RNG
+   help
+ Enable random number generator for sandbox. This is an
+emulation of a rng device.
+
  config RNG_STM32MP1
 bool "Enable random number generator for STM32MP1"
 depends on ARCH_STM32MP && DM_RNG
diff --git a/drivers/rng/Makefile b/drivers/rng/Makefile
index 699beb3..3517005 100644
--- a/drivers/rng/Makefile
+++ b/drivers/rng/Makefile
@@ -4,4 +4,5 @@
  #

  obj-$(CONFIG_DM_RNG) += rng-uclass.o
+obj-$(CONFIG_RNG_SANDBOX) += sandbox_rng.o
  obj-$(CONFIG_RNG_STM32MP1) += stm32mp1_rng.o
diff --git a/drivers/rng/sandbox_rng.c b/drivers/rng/sandbox_rng.c
new file mode 100644
index 000..8262e0d
--- /dev/null
+++ b/drivers/rng/sandbox_rng.c
@@ -0,0 +1,39 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2019, Linaro Limited
+ */
+
+#include 
+#include 
+#include 
+
+static unsigned long random = 0xdeadbeef;
+
+static int sandbox_rng_read(struct udevice *dev, void *data, size_t len)
+{
+   if (len != sizeof(random))
+   return -EINVAL;


This will drop on our feet once we use the RNG class for anything real.

Just imagine an EFI_RNG_PROTOCOL test failing on the sandbox because we
cannot read 128 bit.

Can't we use a PRNG here and reinitialize it when len == ~0UL?

Functions rand() and srand() are good enough for the job.

Best regards

Heinrich


+
+   random ^= ~0UL;
+   *(unsigned long *)data = random;
+
+   return 0;
+}
+
+static const struct dm_rng_ops sandbox_rng_ops = {
+   .read = sandbox_rng_read,
+};
+
+static const struct udevice_id sandbox_rng_match[] = {
+   {
+   .compatible = "sandbox,sandbox-rng",
+   },
+   {},
+};
+
+U_BOOT_DRIVER(sandbox_rng) = {
+   .name = "sandbox-rng",
+   .id = UCLASS_RNG,
+   .of_match = sandbox_rng_match,
+   .ops = _rng_ops,
+};





Re: [PATCH v4 8/8] virtio: rng: Add a random number generator(rng) driver

2019-12-24 Thread Heinrich Schuchardt

On 12/17/19 12:52 PM, Sughosh Ganu wrote:

Add a driver for the virtio-rng device on the qemu platform. The
device uses pci as a transport medium. The driver can be enabled with
the following configs

CONFIG_VIRTIO
CONFIG_DM_RNG
CONFIG_VIRTIO_PCI
CONFIG_VIRTIO_RNG

Signed-off-by: Sughosh Ganu 
---
  drivers/virtio/Kconfig |  6 
  drivers/virtio/Makefile|  1 +
  drivers/virtio/virtio-uclass.c |  1 +
  drivers/virtio/virtio_rng.c| 72 ++
  include/virtio.h   |  4 ++-
  5 files changed, 83 insertions(+), 1 deletion(-)
  create mode 100644 drivers/virtio/virtio_rng.c

diff --git a/drivers/virtio/Kconfig b/drivers/virtio/Kconfig
index a9d5fd0..2e3dd3b 100644
--- a/drivers/virtio/Kconfig
+++ b/drivers/virtio/Kconfig
@@ -59,4 +59,10 @@ config VIRTIO_BLK
  This is the virtual block driver for virtio. It can be used with
  QEMU based targets.

+config VIRTIO_RNG
+   bool "virtio rng driver"
+   depends on VIRTIO
+   help
+ This is the virtual random number generator driver. It can be used
+with Qemu based targets.
  endmenu
diff --git a/drivers/virtio/Makefile b/drivers/virtio/Makefile
index 4579044..dc88809 100644
--- a/drivers/virtio/Makefile
+++ b/drivers/virtio/Makefile
@@ -9,3 +9,4 @@ obj-$(CONFIG_VIRTIO_PCI) += virtio_pci_legacy.o 
virtio_pci_modern.o
  obj-$(CONFIG_VIRTIO_SANDBOX) += virtio_sandbox.o
  obj-$(CONFIG_VIRTIO_NET) += virtio_net.o
  obj-$(CONFIG_VIRTIO_BLK) += virtio_blk.o
+obj-$(CONFIG_VIRTIO_RNG) += virtio_rng.o
diff --git a/drivers/virtio/virtio-uclass.c b/drivers/virtio/virtio-uclass.c
index 34397d7..436faa4 100644
--- a/drivers/virtio/virtio-uclass.c
+++ b/drivers/virtio/virtio-uclass.c
@@ -24,6 +24,7 @@
  static const char *const virtio_drv_name[VIRTIO_ID_MAX_NUM] = {
[VIRTIO_ID_NET] = VIRTIO_NET_DRV_NAME,
[VIRTIO_ID_BLOCK]   = VIRTIO_BLK_DRV_NAME,
+   [VIRTIO_ID_RNG] = VIRTIO_RNG_DRV_NAME,
  };

  int virtio_get_config(struct udevice *vdev, unsigned int offset,
diff --git a/drivers/virtio/virtio_rng.c b/drivers/virtio/virtio_rng.c
new file mode 100644
index 000..19a0cc1
--- /dev/null
+++ b/drivers/virtio/virtio_rng.c
@@ -0,0 +1,72 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (c) 2019, Linaro Limited
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+struct virtio_rng_priv {
+   struct virtqueue *rng_vq;
+};
+
+static int virtio_rng_read(struct udevice *dev, void *data, size_t len)
+{
+   struct virtio_sg sg = { data, len };
+   struct virtio_sg *sgs[] = {  };
+   struct virtio_rng_priv *priv = dev_get_priv(dev);
+   unsigned int rsize;
+   int ret;
+
+   ret = virtqueue_add(priv->rng_vq, sgs, 0, 1);
+   if (ret)


Please, add a debug statement for each error path, e.g.

debug("%s: virtqueue_add() failed\n", __func__)


+   return ret;
+
+   virtqueue_kick(priv->rng_vq);
+
+   while (!virtqueue_get_buf(priv->rng_vq, ))
+   ;
+
+   return rsize;


The return value is inconsistent with sandbox_rng_read() and
stm32_rng_read(). Both return 0 on success.

With "return 0;" the driver works fine.


+}
+
+static int virtio_rng_bind(struct udevice *dev)
+{
+   struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(dev->parent);
+
+   /* Indicate what driver features we support */
+   virtio_driver_features_init(uc_priv, NULL, 0, NULL, 0);
+
+   return 0;
+}
+
+static int virtio_rng_probe(struct udevice *dev)
+{
+   struct virtio_rng_priv *priv = dev_get_priv(dev);
+   int ret;
+
+   ret = virtio_find_vqs(dev, 1, >rng_vq);
+   if (ret < 0)


Add a debug statement here, please, e.g.

debug("%s: virtio_find_vqs() failed\n", __func__)

Best regards

Heinrich


+   return ret;
+
+   return 0;
+}
+
+static const struct dm_rng_ops virtio_rng_ops = {
+   .read   = virtio_rng_read,
+};
+
+U_BOOT_DRIVER(virtio_rng) = {
+   .name   = VIRTIO_RNG_DRV_NAME,
+   .id = UCLASS_RNG,
+   .bind   = virtio_rng_bind,
+   .probe  = virtio_rng_probe,
+   .remove = virtio_reset,
+   .ops= _rng_ops,
+   .priv_auto_alloc_size = sizeof(struct virtio_rng_priv),
+   .flags  = DM_FLAG_ACTIVE_DMA,
+};
diff --git a/include/virtio.h b/include/virtio.h
index 654fdf1..561dcc3 100644
--- a/include/virtio.h
+++ b/include/virtio.h
@@ -22,10 +22,12 @@

  #define VIRTIO_ID_NET 1 /* virtio net */
  #define VIRTIO_ID_BLOCK   2 /* virtio block */
-#define VIRTIO_ID_MAX_NUM  3
+#define VIRTIO_ID_RNG  4 /* virtio rng */
+#define VIRTIO_ID_MAX_NUM  5

  #define VIRTIO_NET_DRV_NAME   "virtio-net"
  #define VIRTIO_BLK_DRV_NAME   "virtio-blk"
+#define VIRTIO_RNG_DRV_NAME"virtio-rng"

  /* Status byte for guest to report progress, and synchronize features */






[PATCH v3 8/9] env: ti: boot: Boot Android with dynamic partitions

2019-12-24 Thread Sam Protsenko
Changes:
  - use boot.img instead of boot_fit.img
  - use .dtb from boot.img v2
  - implement recovery boot
  - always boot ramdisk from boot.img, we can't mount system as root
now, as system is a logical partition inside of super partition
  - don't add "skip_initramfs" to cmdline anymore
  - to boot into recovery, use boot image from recovery partition
  - prepare partition table:
- A/B scheme
- use 'super' partition instead of 'system' and 'vendor'
- add dtbo partitions
- introduce metadata partition

Not implemented: reading and applying dtbo blobs from dtbo partition.

Signed-off-by: Sam Protsenko 
---
 include/environment/ti/boot.h | 107 +-
 1 file changed, 42 insertions(+), 65 deletions(-)

diff --git a/include/environment/ti/boot.h b/include/environment/ti/boot.h
index da99215fbd..3ac8e3ff08 100644
--- a/include/environment/ti/boot.h
+++ b/include/environment/ti/boot.h
@@ -13,28 +13,6 @@
 #define CONSOLEDEV "ttyS2"
 #endif
 
-#define VBMETA_PART_SIZE   (64 * 1024)
-
-#if defined(CONFIG_LIBAVB)
-#define VBMETA_PART \
-   "name=vbmeta,size=" __stringify(VBMETA_PART_SIZE) \
-   ",uuid=${uuid_gpt_vbmeta};"
-#else
-#define VBMETA_PART""
-#endif
-
-#if defined(CONFIG_CMD_AB_SELECT)
-#define COMMON_PARTS \
-   "name=boot_a,size=20M,uuid=${uuid_gpt_boot_a};" \
-   "name=boot_b,size=20M,uuid=${uuid_gpt_boot_b};" \
-   "name=system_a,size=1024M,uuid=${uuid_gpt_system_a};" \
-   "name=system_b,size=1024M,uuid=${uuid_gpt_system_b};"
-#else
-#define COMMON_PARTS \
-   "name=boot,size=20M,uuid=${uuid_gpt_boot};" \
-   "name=system,size=1024M,uuid=${uuid_gpt_system};"
-#endif
-
 #ifndef PARTS_DEFAULT
 /* Define the default GPT table for eMMC */
 #define PARTS_DEFAULT \
@@ -49,10 +27,15 @@
"name=bootloader,size=2048K,uuid=${uuid_gpt_bootloader};" \
"name=uboot-env,start=2432K,size=256K,uuid=${uuid_gpt_reserved};" \
"name=misc,size=128K,uuid=${uuid_gpt_misc};" \
-   "name=recovery,size=40M,uuid=${uuid_gpt_recovery};" \
-   COMMON_PARTS \
-   "name=vendor,size=256M,uuid=${uuid_gpt_vendor};" \
-   VBMETA_PART \
+   "name=boot_a,size=20M,uuid=${uuid_gpt_boot_a};" \
+   "name=boot_b,size=20M,uuid=${uuid_gpt_boot_b};" \
+   "name=dtbo_a,size=8M,uuid=${uuid_gpt_dtbo_a};" \
+   "name=dtbo_b,size=8M,uuid=${uuid_gpt_dtbo_b};" \
+   "name=vbmeta_a,size=64K,uuid=${uuid_gpt_vbmeta_a};" \
+   "name=vbmeta_b,size=64K,uuid=${uuid_gpt_vbmeta_b};" \
+   "name=recovery,size=64M,uuid=${uuid_gpt_recovery};" \
+   "name=super,size=2560M,uuid=${uuid_gpt_super};" \
+   "name=metadata,size=16M,uuid=${uuid_gpt_metadata};" \
"name=userdata,size=-,uuid=${uuid_gpt_userdata}"
 #endif /* PARTS_DEFAULT */
 
@@ -72,7 +55,7 @@
 #define CONTROL_PARTITION "misc"
 
 #if defined(CONFIG_CMD_AB_SELECT)
-#define AB_SELECT \
+#define AB_SELECT_SLOT \
"if part number mmc 1 " CONTROL_PARTITION " control_part_number; " \
"then " \
"echo " CONTROL_PARTITION \
@@ -82,20 +65,14 @@
"echo " CONTROL_PARTITION " partition not found;" \
"exit;" \
"fi;" \
-   "setenv slot_suffix _${slot_name};" \
-   "if part number mmc ${mmcdev} system${slot_suffix} " \
-   "system_part_number; then " \
-   "setenv bootargs_ab " \
-   "ro root=/dev/mmcblk${mmcdev}p${system_part_number} " \
-   "rootwait init=/init skip_initramfs " \
-   "androidboot.slot_suffix=${slot_suffix};" \
-   "echo A/B cmdline addition: ${bootargs_ab};" \
-   "setenv bootargs ${bootargs} ${bootargs_ab};" \
-   "else " \
-   "echo system${slot_suffix} partition not found;" \
-   "fi;"
+   "setenv slot_suffix _${slot_name};"
+#define AB_SELECT_ARGS \
+   "setenv bootargs_ab androidboot.slot_suffix=${slot_suffix}; " \
+   "echo A/B cmdline addition: ${bootargs_ab};" \
+   "setenv bootargs ${bootargs} ${bootargs_ab};"
 #else
-#define AB_SELECT ""
+#define AB_SELECT_SLOT ""
+#define AB_SELECT_ARGS ""
 #endif
 
 #define FASTBOOT_CMD \
@@ -121,46 +98,46 @@
"setenv mmcroot /dev/mmcblk0p2 rw; " \
"run mmcboot;\0" \
"emmc_android_boot=" \
+   "setenv mmcdev 1; " \
+   "mmc dev $mmcdev; " \
+   "mmc rescan; " \
+   AB_SELECT_SLOT \
"if bcb load " __stringify(CONFIG_FASTBOOT_FLASH_MMC_DEV) " " \
CONTROL_PARTITION "; then " \
+   "setenv ardaddr -; " \
"if bcb test command = bootonce-bootloader; then " \
-   "echo BCB: Bootloader boot...; " \
+   "echo Android: Bootloader boot...; " \
"bcb clear command; bcb store; " \
FASTBOOT_CMD \
+   

[PATCH v3 9/9] arm: ti: boot: Use correct dtb and dtbo on Android boot

2019-12-24 Thread Sam Protsenko
Read correct dtb blob from boot.img/recovery.img and apply correct dtbo
blobs from dtbo partition.

Signed-off-by: Sam Protsenko 
---
 include/configs/ti_armv7_common.h |  7 +
 include/environment/ti/boot.h | 43 ---
 2 files changed, 47 insertions(+), 3 deletions(-)

diff --git a/include/configs/ti_armv7_common.h 
b/include/configs/ti_armv7_common.h
index adc7861539..b36526b8f3 100644
--- a/include/configs/ti_armv7_common.h
+++ b/include/configs/ti_armv7_common.h
@@ -37,11 +37,18 @@
  * seen large trees).  We say all of this must be within the first 256MB
  * as that will normally be within the kernel lowmem and thus visible via
  * bootm_size and we only run on platforms with 256MB or more of memory.
+ *
+ * As a temporary storage for DTBO blobs (which should be applied into DTB
+ * blob), we use the location 15.5 MB above the ramdisk. If someone wants to
+ * use ramdisk bigger than 15.5 MB, then DTBO can be loaded and applied to DTB
+ * blob before loading the ramdisk, as DTBO location is only used as a 
temporary
+ * storage, and can be re-used after 'fdt apply' command is done.
  */
 #define DEFAULT_LINUX_BOOT_ENV \
"loadaddr=0x8200\0" \
"kernel_addr_r=0x8200\0" \
"fdtaddr=0x8800\0" \
+   "dtboaddr=0x8900\0" \
"fdt_addr_r=0x8800\0" \
"rdaddr=0x8808\0" \
"ramdisk_addr_r=0x8808\0" \
diff --git a/include/environment/ti/boot.h b/include/environment/ti/boot.h
index 3ac8e3ff08..0bb670eec7 100644
--- a/include/environment/ti/boot.h
+++ b/include/environment/ti/boot.h
@@ -75,6 +75,45 @@
 #define AB_SELECT_ARGS ""
 #endif
 
+/*
+ * Prepares complete device tree blob for current board (for Android boot).
+ *
+ * Boot image or recovery image should be loaded into $loadaddr prior to 
running
+ * these commands. The logic of these commnads is next:
+ *
+ *   1. Read correct DTB for current SoC/board from boot image in $loadaddr
+ *  to $fdtaddr
+ *   2. Merge all needed DTBO for current board from 'dtbo' partition into read
+ *  DTB
+ *   3. User should provide $fdtaddr as 3rd argument to 'bootm'
+ */
+#define PREPARE_FDT \
+   "echo Preparing FDT...; " \
+   "if test $board_name = am57xx_evm_reva3; then " \
+   "echo \"  Reading DTBO partition...\"; " \
+   "part start mmc ${mmcdev} dtbo${slot_suffix} p_dtbo_start; " \
+   "part size mmc ${mmcdev} dtbo${slot_suffix} p_dtbo_size; " \
+   "mmc read ${dtboaddr} ${p_dtbo_start} ${p_dtbo_size}; " \
+   "echo \"  Reading DTB for AM57x EVM RevA3...\"; " \
+   "abootimg get dtb --index=0 dtb_start dtb_size; " \
+   "cp.b $dtb_start $fdtaddr $dtb_size; " \
+   "fdt addr $fdtaddr; " \
+   "echo \"  Applying DTBOs for AM57x EVM RevA3...\"; " \
+   "adtimg addr $dtboaddr; " \
+   "adtimg get dt --index=0 dtbo0_addr; " \
+   "fdt apply $dtbo0_addr; " \
+   "adtimg get dt --index=1 dtbo1_addr; " \
+   "fdt apply $dtbo1_addr; " \
+   "elif test $board_name = beagle_x15_revc; then " \
+   "echo \"  Reading DTB for Beagle X15 RevC...\"; " \
+   "abootimg get dtb --index=0 dtb_start dtb_size; " \
+   "cp.b $dtb_start $fdtaddr $dtb_size; " \
+   "fdt addr $fdtaddr; " \
+   "else " \
+   "echo Error: Android boot is not supported for $board_name; " \
+   "exit; " \
+   "fi; " \
+
 #define FASTBOOT_CMD \
"echo Booting into fastboot ...; " \
"fastboot " __stringify(CONFIG_FASTBOOT_USB_DEV) "; "
@@ -131,9 +170,7 @@
"if part start mmc $mmcdev $apart boot_start; then " \
"part size mmc $mmcdev $apart boot_size; " \
"mmc read $loadaddr $boot_start $boot_size; " \
-   "abootimg get dtb --index=0 dtb_start dtb_size; " \
-   "cp.b $dtb_start $fdtaddr $dtb_size; " \
-   "fdt addr $fdtaddr; " \
+   PREPARE_FDT \
"bootm $loadaddr $ardaddr $fdtaddr; " \
"else " \
"echo $apart partition not found; " \
-- 
2.24.0



[PATCH v3 7/9] env: ti: boot: Respect slot_suffix in AVB commands

2019-12-24 Thread Sam Protsenko
Signed-off-by: Sam Protsenko 
---
 include/environment/ti/boot.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/include/environment/ti/boot.h b/include/environment/ti/boot.h
index 684a744f31..da99215fbd 100644
--- a/include/environment/ti/boot.h
+++ b/include/environment/ti/boot.h
@@ -63,7 +63,7 @@
"else " \
"echo AVB verification failed.;" \
"exit; fi;"
-#define AVB_VERIFY_CMD "avb_verify=avb init 1; avb verify;\0"
+#define AVB_VERIFY_CMD "avb_verify=avb init 1; avb verify $slot_suffix;\0"
 #else
 #define AVB_VERIFY_CHECK ""
 #define AVB_VERIFY_CMD ""
-- 
2.24.0



[PATCH v3 6/9] configs: am57xx_evm: Enable Android commands

2019-12-24 Thread Sam Protsenko
Enable Android commands that will be needed for Android 10 boot flow
implementation, for all AM57x variants. Commands enabled:

  1. 'abootimg':
 - CONFIG_CMD_ABOOTIMG=y
  2. 'ab_select':
 - CONFIG_ANDROID_AB=y
 - CONFIG_CMD_AB_SELECT=y
  3. 'avb':
 - CONFIG_LIBAVB=y
 - CONFIG_AVB_VERIFY=y
 - CONFIG_CMD_AVB=y

While at it, resync defconfig files with "make savedefconfig".

Signed-off-by: Sam Protsenko 
---
 configs/am57xx_evm_defconfig| 6 ++
 configs/am57xx_hs_evm_defconfig | 6 ++
 configs/am57xx_hs_evm_usb_defconfig | 6 ++
 3 files changed, 18 insertions(+)

diff --git a/configs/am57xx_evm_defconfig b/configs/am57xx_evm_defconfig
index 0c6a2e9193..22eb58fc29 100644
--- a/configs/am57xx_evm_defconfig
+++ b/configs/am57xx_evm_defconfig
@@ -21,6 +21,8 @@ CONFIG_SYS_CONSOLE_INFO_QUIET=y
 # CONFIG_MISC_INIT_R is not set
 CONFIG_VERSION_VARIABLE=y
 CONFIG_BOARD_EARLY_INIT_F=y
+CONFIG_AVB_VERIFY=y
+CONFIG_ANDROID_AB=y
 CONFIG_SPL_SYS_MALLOC_SIMPLE=y
 CONFIG_SPL_SEPARATE_BSS=y
 CONFIG_SPL_DMA_SUPPORT=y
@@ -30,11 +32,14 @@ CONFIG_SPL_SPI_LOAD=y
 CONFIG_SYS_SPI_U_BOOT_OFFS=0x4
 CONFIG_SPL_YMODEM_SUPPORT=y
 CONFIG_CMD_ADTIMG=y
+CONFIG_CMD_ABOOTIMG=y
 CONFIG_CMD_SPL=y
 CONFIG_CMD_BCB=y
 # CONFIG_CMD_FLASH is not set
 # CONFIG_CMD_SETEXPR is not set
+CONFIG_CMD_AB_SELECT=y
 # CONFIG_CMD_PMIC is not set
+CONFIG_CMD_AVB=y
 CONFIG_OF_CONTROL=y
 CONFIG_SPL_OF_CONTROL=y
 CONFIG_DEFAULT_DEVICE_TREE="am572x-idk"
@@ -102,3 +107,4 @@ CONFIG_USB_GADGET=y
 CONFIG_USB_GADGET_MANUFACTURER="Texas Instruments"
 CONFIG_USB_GADGET_VENDOR_NUM=0x0451
 CONFIG_USB_GADGET_PRODUCT_NUM=0xd022
+CONFIG_LIBAVB=y
diff --git a/configs/am57xx_hs_evm_defconfig b/configs/am57xx_hs_evm_defconfig
index 3c57dfb031..bb26d46bfa 100644
--- a/configs/am57xx_hs_evm_defconfig
+++ b/configs/am57xx_hs_evm_defconfig
@@ -26,6 +26,8 @@ CONFIG_SYS_CONSOLE_INFO_QUIET=y
 # CONFIG_MISC_INIT_R is not set
 CONFIG_VERSION_VARIABLE=y
 CONFIG_BOARD_EARLY_INIT_F=y
+CONFIG_AVB_VERIFY=y
+CONFIG_ANDROID_AB=y
 CONFIG_SPL_SYS_MALLOC_SIMPLE=y
 CONFIG_SPL_SEPARATE_BSS=y
 CONFIG_SPL_DMA_SUPPORT=y
@@ -33,10 +35,13 @@ CONFIG_SPL_DMA_SUPPORT=y
 CONFIG_SPL_SPI_LOAD=y
 CONFIG_SYS_SPI_U_BOOT_OFFS=0x4
 CONFIG_CMD_ADTIMG=y
+CONFIG_CMD_ABOOTIMG=y
 CONFIG_CMD_BCB=y
 # CONFIG_CMD_FLASH is not set
 # CONFIG_CMD_SETEXPR is not set
+CONFIG_CMD_AB_SELECT=y
 # CONFIG_CMD_PMIC is not set
+CONFIG_CMD_AVB=y
 CONFIG_OF_CONTROL=y
 CONFIG_SPL_OF_CONTROL=y
 CONFIG_DEFAULT_DEVICE_TREE="am57xx-beagle-x15"
@@ -98,3 +103,4 @@ CONFIG_USB_GADGET=y
 CONFIG_USB_GADGET_MANUFACTURER="Texas Instruments"
 CONFIG_USB_GADGET_VENDOR_NUM=0x0451
 CONFIG_USB_GADGET_PRODUCT_NUM=0xd022
+CONFIG_LIBAVB=y
diff --git a/configs/am57xx_hs_evm_usb_defconfig 
b/configs/am57xx_hs_evm_usb_defconfig
index 87f391c2b0..73e42f9dcc 100644
--- a/configs/am57xx_hs_evm_usb_defconfig
+++ b/configs/am57xx_hs_evm_usb_defconfig
@@ -27,6 +27,8 @@ CONFIG_SYS_CONSOLE_INFO_QUIET=y
 # CONFIG_MISC_INIT_R is not set
 CONFIG_VERSION_VARIABLE=y
 CONFIG_BOARD_EARLY_INIT_F=y
+CONFIG_AVB_VERIFY=y
+CONFIG_ANDROID_AB=y
 CONFIG_SPL_SYS_MALLOC_SIMPLE=y
 CONFIG_SPL_SEPARATE_BSS=y
 CONFIG_SPL_DMA_SUPPORT=y
@@ -38,10 +40,13 @@ CONFIG_SPL_USB_GADGET=y
 CONFIG_SPL_DFU=y
 CONFIG_SPL_YMODEM_SUPPORT=y
 CONFIG_CMD_ADTIMG=y
+CONFIG_CMD_ABOOTIMG=y
 CONFIG_CMD_BCB=y
 # CONFIG_CMD_FLASH is not set
 # CONFIG_CMD_SETEXPR is not set
+CONFIG_CMD_AB_SELECT=y
 # CONFIG_CMD_PMIC is not set
+CONFIG_CMD_AVB=y
 CONFIG_OF_CONTROL=y
 CONFIG_SPL_OF_CONTROL=y
 CONFIG_DEFAULT_DEVICE_TREE="am57xx-beagle-x15"
@@ -105,3 +110,4 @@ CONFIG_USB_GADGET=y
 CONFIG_USB_GADGET_MANUFACTURER="Texas Instruments"
 CONFIG_USB_GADGET_VENDOR_NUM=0x0451
 CONFIG_USB_GADGET_PRODUCT_NUM=0xd022
+CONFIG_LIBAVB=y
-- 
2.24.0



[PATCH v3 2/9] image: android: Add routine to get dtbo params

2019-12-24 Thread Sam Protsenko
Android Boot Image v1 adds "Recovery DTB" field in image header and
associate payload in boot image itself [1]. Payload should be in
Android DTB/DTBO format [2]. That "Recovery DTB" area should be only
populated for non-A/B devices, and only in recovery image.

Add function to get an address and size of that payload. That function
can be further used e.g. in 'abootimg' command to provide the user a way
to get the address of recovery dtbo from U-Boot shell, which can be
further parsed using 'adtimg' command.

[1] https://source.android.com/devices/bootloader/boot-image-header
[2] https://source.android.com/devices/architecture/dto/partitions

Signed-off-by: Sam Protsenko 
---
 common/image-android.c | 61 ++
 include/image.h|  1 +
 2 files changed, 62 insertions(+)

diff --git a/common/image-android.c b/common/image-android.c
index 1ccad6c556..5d6669ceab 100644
--- a/common/image-android.c
+++ b/common/image-android.c
@@ -197,6 +197,67 @@ int android_image_get_second(const struct andr_img_hdr 
*hdr,
return 0;
 }
 
+/**
+ * android_image_get_dtbo() - Get address and size of recovery DTBO image.
+ * @hdr_addr: Boot image header address
+ * @addr: If not NULL, will contain address of recovery DTBO image
+ * @size: If not NULL, will contain size of recovery DTBO image
+ *
+ * Get the address and size of DTBO image in "Recovery DTBO" area of Android
+ * Boot Image in RAM. The format of this image is Android DTBO (see
+ * corresponding "DTB/DTBO Partitions" AOSP documentation for details). Once
+ * the address is obtained from this function, one can use 'adtimg' U-Boot
+ * command or android_dt_*() functions to extract desired DTBO blob.
+ *
+ * This DTBO (included in boot image) is only needed for non-A/B devices, and 
it
+ * only can be found in recovery image. On A/B devices we can always rely on
+ * "dtbo" partition. See "Including DTBO in Recovery for Non-A/B Devices" in
+ * AOSP documentation for details.
+ *
+ * Return: true on success or false on error.
+ */
+bool android_image_get_dtbo(ulong hdr_addr, ulong *addr, u32 *size)
+{
+   const struct andr_img_hdr *hdr;
+   ulong dtbo_img_addr;
+   bool res = true;
+
+   hdr = map_sysmem(hdr_addr, sizeof(*hdr));
+   if (android_image_check_header(hdr)) {
+   printf("Error: Boot Image header is incorrect\n");
+   res = false;
+   goto exit;
+   }
+
+   if (hdr->header_version < 1) {
+   printf("Error: header_version must be >= 1 to get dtbo\n");
+   res = false;
+   goto exit;
+   }
+
+   if (hdr->recovery_dtbo_size == 0) {
+   printf("Error: recovery_dtbo_size is 0\n");
+   res = false;
+   goto exit;
+   }
+
+   /* Calculate the address of DTB area in boot image */
+   dtbo_img_addr = hdr_addr;
+   dtbo_img_addr += hdr->page_size;
+   dtbo_img_addr += ALIGN(hdr->kernel_size, hdr->page_size);
+   dtbo_img_addr += ALIGN(hdr->ramdisk_size, hdr->page_size);
+   dtbo_img_addr += ALIGN(hdr->second_size, hdr->page_size);
+
+   if (addr)
+   *addr = dtbo_img_addr;
+   if (size)
+   *size = hdr->recovery_dtbo_size;
+
+exit:
+   unmap_sysmem(hdr);
+   return res;
+}
+
 /**
  * android_image_get_dtb_img_addr() - Get the address of DTB area in boot 
image.
  * @hdr_addr: Boot image header address
diff --git a/include/image.h b/include/image.h
index 8e81166be4..b8d821605b 100644
--- a/include/image.h
+++ b/include/image.h
@@ -1333,6 +1333,7 @@ int android_image_get_ramdisk(const struct andr_img_hdr 
*hdr,
  ulong *rd_data, ulong *rd_len);
 int android_image_get_second(const struct andr_img_hdr *hdr,
  ulong *second_data, ulong *second_len);
+bool android_image_get_dtbo(ulong hdr_addr, ulong *addr, u32 *size);
 bool android_image_get_dtb_by_index(ulong hdr_addr, u32 index, ulong *addr,
u32 *size);
 ulong android_image_get_end(const struct andr_img_hdr *hdr);
-- 
2.24.0



[PATCH v3 1/9] image: android: Add functions for handling dtb field

2019-12-24 Thread Sam Protsenko
Android Boot Image v2 adds "DTB" payload (and corresponding field in the
image header). Provide functions for its handling:

  - android_image_get_dtb_by_index(): Obtain DTB blob from "DTB" part of
boot image, by blob's index
  - android_image_print_dtb_contents(): Iterate over all DTB blobs in
"DTB" part of boot image and print those blobs info

"DTB" payload might be in one of the following formats:
  1. concatenated DTB blobs
  2. Android DTBO format

The latter requires "android-image-dt.c" functionality, so this commit
selects that file for building for CONFIG_ANDROID_BOOT_IMAGE option.

Right now this new functionality isn't used, but it can be used further.
As it's required to apply some specific dtbo blob(s) from "dtbo"
partition, we can't automate this process inside of "bootm" command. But
we can do next:
  - come up with some new command like "abootimg" to extract dtb blob
from boot image (using functions from this patch)
  - extract desired dtbo blobs from "dtbo" partition using "adtimg"
command
  - merge dtbo blobs into dtb blob using "fdt apply" command
  - pass resulting dtb blob into bootm command in order to boot the
Android kernel with Android ramdisk from boot image

Signed-off-by: Sam Protsenko 
---
 common/Makefile|   2 +-
 common/image-android.c | 214 +
 include/image.h|   5 +
 3 files changed, 220 insertions(+), 1 deletion(-)

diff --git a/common/Makefile b/common/Makefile
index 029cc0f2ce..1ffddc2f94 100644
--- a/common/Makefile
+++ b/common/Makefile
@@ -108,7 +108,7 @@ endif
 
 obj-y += image.o
 obj-$(CONFIG_ANDROID_AB) += android_ab.o
-obj-$(CONFIG_ANDROID_BOOT_IMAGE) += image-android.o
+obj-$(CONFIG_ANDROID_BOOT_IMAGE) += image-android.o image-android-dt.o
 obj-$(CONFIG_$(SPL_TPL_)OF_LIBFDT) += image-fdt.o
 obj-$(CONFIG_$(SPL_TPL_)FIT) += image-fit.o
 obj-$(CONFIG_$(SPL_)MULTI_DTB_FIT) += boot_fit.o common_fit.o
diff --git a/common/image-android.c b/common/image-android.c
index 3564a64221..1ccad6c556 100644
--- a/common/image-android.c
+++ b/common/image-android.c
@@ -6,10 +6,12 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
 #include 
+#include 
 
 #define ANDROID_IMAGE_DEFAULT_KERNEL_ADDR  0x10008000
 
@@ -195,6 +197,121 @@ int android_image_get_second(const struct andr_img_hdr 
*hdr,
return 0;
 }
 
+/**
+ * android_image_get_dtb_img_addr() - Get the address of DTB area in boot 
image.
+ * @hdr_addr: Boot image header address
+ * @addr: Will contain the address of DTB area in boot image
+ *
+ * Return: true on success or false on fail.
+ */
+static bool android_image_get_dtb_img_addr(ulong hdr_addr, ulong *addr)
+{
+   const struct andr_img_hdr *hdr;
+   ulong dtb_img_addr;
+   bool res = true;
+
+   hdr = map_sysmem(hdr_addr, sizeof(*hdr));
+   if (android_image_check_header(hdr)) {
+   printf("Error: Boot Image header is incorrect\n");
+   res = false;
+   goto exit;
+   }
+
+   if (hdr->header_version < 2) {
+   printf("Error: header_version must be >= 2 to get dtb\n");
+   res = false;
+   goto exit;
+   }
+
+   if (hdr->dtb_size == 0) {
+   printf("Error: dtb_size is 0\n");
+   res = false;
+   goto exit;
+   }
+
+   /* Calculate the address of DTB area in boot image */
+   dtb_img_addr = hdr_addr;
+   dtb_img_addr += hdr->page_size;
+   dtb_img_addr += ALIGN(hdr->kernel_size, hdr->page_size);
+   dtb_img_addr += ALIGN(hdr->ramdisk_size, hdr->page_size);
+   dtb_img_addr += ALIGN(hdr->second_size, hdr->page_size);
+   dtb_img_addr += ALIGN(hdr->recovery_dtbo_size, hdr->page_size);
+
+   *addr = dtb_img_addr;
+
+exit:
+   unmap_sysmem(hdr);
+   return res;
+}
+
+/**
+ * android_image_get_dtb_by_index() - Get address and size of blob in DTB area.
+ * @hdr_addr: Boot image header address
+ * @index: Index of desired DTB in DTB area (starting from 0)
+ * @addr: If not NULL, will contain address to specified DTB
+ * @size: If not NULL, will contain size of specified DTB
+ *
+ * Get the address and size of DTB blob by its index in DTB area of Android
+ * Boot Image in RAM.
+ *
+ * Return: true on success or false on error.
+ */
+bool android_image_get_dtb_by_index(ulong hdr_addr, u32 index, ulong *addr,
+   u32 *size)
+{
+   const struct andr_img_hdr *hdr;
+   bool res;
+   ulong dtb_img_addr; /* address of DTB part in boot image */
+   u32 dtb_img_size;   /* size of DTB payload in boot image */
+   ulong dtb_addr; /* address of DTB blob with specified index  */
+   u32 i;  /* index iterator */
+
+   res = android_image_get_dtb_img_addr(hdr_addr, _img_addr);
+   if (!res)
+   return false;
+
+   /* Check if DTB area of boot image is in DTBO format */
+   if 

[PATCH v3 3/9] cmd: abootimg: Add abootimg command

2019-12-24 Thread Sam Protsenko
This command can be used to extract fields and image payloads from
Android Boot Image. It can be used for example to implement boot flow
where dtb is taken from boot.img (as v2 incorporated dtb inside of
boot.img). Using this command, one can obtain needed dtb blob from
boot.img in scripting manner, and then apply needed dtbo's (from "dtbo"
partition) on top of that, providing then the resulting image to bootm
command in order to boot the Android.

Also right now this command has the sub-command to get an address and
size of recovery dtbo from recovery image (for non-A/B devices only,
see [1,2] for details).

It can be tested like this:

=> mmc dev 1
=> part start mmc 1 boot_a boot_start
=> part size mmc 1 boot_a boot_size
=> mmc read $loadaddr $boot_start $boot_size
=> abootimg get ver
=> abootimg dump dtb

[1] https://source.android.com/devices/bootloader/boot-image-header
[2] https://source.android.com/devices/architecture/dto/partitions

Signed-off-by: Sam Protsenko 
---
 cmd/Kconfig|   8 ++
 cmd/Makefile   |   1 +
 cmd/abootimg.c | 242 +
 3 files changed, 251 insertions(+)
 create mode 100644 cmd/abootimg.c

diff --git a/cmd/Kconfig b/cmd/Kconfig
index f63adbdc3a..e25ac7ee57 100644
--- a/cmd/Kconfig
+++ b/cmd/Kconfig
@@ -356,6 +356,14 @@ config CMD_ADTIMG
  files should be merged in one dtb further, which needs to be passed to
  the kernel, as part of a boot process.
 
+config CMD_ABOOTIMG
+   bool "abootimg"
+   depends on ANDROID_BOOT_IMAGE
+   help
+ Android Boot Image manipulation commands. Allows one to extract
+ images contained in boot.img, like kernel, ramdisk, dtb, etc, and
+ obtain corresponding meta-information from boot.img.
+
 config CMD_ELF
bool "bootelf, bootvx"
default y
diff --git a/cmd/Makefile b/cmd/Makefile
index c17ee20b25..8ccd8c7b87 100644
--- a/cmd/Makefile
+++ b/cmd/Makefile
@@ -48,6 +48,7 @@ ifdef CONFIG_POST
 obj-$(CONFIG_CMD_DIAG) += diag.o
 endif
 obj-$(CONFIG_CMD_ADTIMG) += adtimg.o
+obj-$(CONFIG_CMD_ABOOTIMG) += abootimg.o
 obj-$(CONFIG_CMD_ECHO) += echo.o
 obj-$(CONFIG_ENV_IS_IN_EEPROM) += eeprom.o
 obj-$(CONFIG_CMD_EEPROM) += eeprom.o
diff --git a/cmd/abootimg.c b/cmd/abootimg.c
new file mode 100644
index 00..d4675c275b
--- /dev/null
+++ b/cmd/abootimg.c
@@ -0,0 +1,242 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * (C) Copyright 2019 Linaro Ltd.
+ * Sam Protsenko 
+ */
+
+#include 
+#include 
+#include 
+
+#define abootimg_addr() (_abootimg_addr == -1 ? load_addr : _abootimg_addr)
+
+/* Please use abootimg_addr() macro to obtain the boot image address */
+static ulong _abootimg_addr = -1;
+
+static int abootimg_get_ver(int argc, char * const argv[])
+{
+   const struct andr_img_hdr *hdr;
+   int res = CMD_RET_SUCCESS;
+
+   if (argc > 1)
+   return CMD_RET_USAGE;
+
+   hdr = map_sysmem(abootimg_addr(), sizeof(*hdr));
+   if (android_image_check_header(hdr)) {
+   printf("Error: Boot Image header is incorrect\n");
+   res = CMD_RET_FAILURE;
+   goto exit;
+   }
+
+   if (argc == 0)
+   printf("%u\n", hdr->header_version);
+   else
+   env_set_ulong(argv[0], hdr->header_version);
+
+exit:
+   unmap_sysmem(hdr);
+   return res;
+}
+
+static int abootimg_get_recovery_dtbo(int argc, char * const argv[])
+{
+   ulong addr;
+   u32 size;
+
+   if (argc > 2)
+   return CMD_RET_USAGE;
+
+   if (!android_image_get_dtbo(abootimg_addr(), , ))
+   return CMD_RET_FAILURE;
+
+   if (argc == 0) {
+   printf("%lx\n", addr);
+   } else {
+   env_set_hex(argv[0], addr);
+   if (argc == 2)
+   env_set_hex(argv[1], size);
+   }
+
+   return CMD_RET_SUCCESS;
+}
+
+static int abootimg_get_dtb_load_addr(int argc, char * const argv[])
+{
+   const struct andr_img_hdr *hdr;
+   int res = CMD_RET_SUCCESS;
+
+   if (argc > 1)
+   return CMD_RET_USAGE;
+
+   hdr = map_sysmem(abootimg_addr(), sizeof(*hdr));
+   if (android_image_check_header(hdr)) {
+   printf("Error: Boot Image header is incorrect\n");
+   res = CMD_RET_FAILURE;
+   goto exit;
+   }
+
+   if (hdr->header_version < 2) {
+   printf("Error: header_version must be >= 2 for this\n");
+   res = CMD_RET_FAILURE;
+   goto exit;
+   }
+
+   if (argc == 0)
+   printf("%lx\n", (ulong)hdr->dtb_addr);
+   else
+   env_set_hex(argv[0], (ulong)hdr->dtb_addr);
+
+exit:
+   unmap_sysmem(hdr);
+   return res;
+}
+
+static int abootimg_get_dtb_by_index(int argc, char * const argv[])
+{
+   u32 num;
+   char *endp;
+   ulong addr;
+   u32 size;
+
+   if (argc < 1 || argc > 3)
+   return 

[PATCH v3 4/9] doc: android: Add documentation for Android Boot Image

2019-12-24 Thread Sam Protsenko
Describe Android Boot Image format, how its support is implemented in
U-Boot and associated commands usage.

Signed-off-by: Sam Protsenko 
---
 cmd/Kconfig|   2 +
 doc/android/boot-image.rst | 154 +
 2 files changed, 156 insertions(+)
 create mode 100644 doc/android/boot-image.rst

diff --git a/cmd/Kconfig b/cmd/Kconfig
index e25ac7ee57..18c1db3fd4 100644
--- a/cmd/Kconfig
+++ b/cmd/Kconfig
@@ -364,6 +364,8 @@ config CMD_ABOOTIMG
  images contained in boot.img, like kernel, ramdisk, dtb, etc, and
  obtain corresponding meta-information from boot.img.
 
+ See doc/android/boot-image.rst for details.
+
 config CMD_ELF
bool "bootelf, bootvx"
default y
diff --git a/doc/android/boot-image.rst b/doc/android/boot-image.rst
new file mode 100644
index 00..74937e4bfa
--- /dev/null
+++ b/doc/android/boot-image.rst
@@ -0,0 +1,154 @@
+.. SPDX-License-Identifier: GPL-2.0+
+.. sectionauthor:: Sam Protsenko 
+
+Android Boot Image
+==
+
+Overview
+
+
+Android Boot Image is used to boot Android OS. It usually contains kernel image
+(like ``zImage`` file) and ramdisk. Sometimes it can contain additional
+binaries. This image is built as a part of AOSP (called ``boot.img``), and 
being
+flashed into ``boot`` partition on eMMC. Bootloader then reads that image from
+``boot`` partition to RAM and boots the kernel from it. Kernel than starts
+``init`` process from the ramdisk. It should be mentioned that recovery image
+(``recovery.img``) also has Android Boot Image format.
+
+Android Boot Image format is described at [1]_. At the moment it can have one 
of
+next image headers:
+
+* v0: it's called *legacy* boot image header; used in devices launched before
+  Android 9; contains kernel image, ramdisk and second stage bootloader
+  (usually unused)
+* v1: used in devices launched with Android 9; adds ``recovery_dtbo`` field,
+  which should be used for non-A/B devices in ``recovery.img`` (see [2]_ for
+  details)
+* v2: used in devices launched with Android 10; adds ``dtb`` field, which
+  references payload containing DTB blobs (either concatenated one after the
+  other, or in Android DTBO image format)
+
+v2, v1 and v0 formats are backward compatible.
+
+Android Boot Image format is represented by :c:type:`struct andr_img_hdr` in
+U-Boot, and can be seen in ``include/android_image.h``. U-Boot supports booting
+Android Boot Image and also has associated command
+
+Booting
+---
+
+U-Boot is able to boot the Android OS from Android Boot Image using ``bootm``
+command. In order to use Android Boot Image format support, next option should
+be enabled::
+
+CONFIG_ANDROID_BOOT_IMAGE=y
+
+Then one can use next ``bootm`` command call to run Android:
+
+.. code-block:: bash
+
+=> bootm $loadaddr $loadaddr $fdtaddr
+
+where ``$loadaddr`` - address in RAM where boot image was loaded; ``$fdtaddr`` 
-
+address in RAM where DTB blob was loaded.
+
+And parameters are, correspondingly:
+
+  1. Where kernel image is located in RAM
+  2. Where ramdisk is located in RAM (can be ``"-"`` if not applicable)
+  3. Where DTB blob is located in RAM
+
+``bootm`` command will figure out that image located in ``$loadaddr`` has
+Android Boot Image format, will parse that and boot the kernel from it,
+providing DTB blob to kernel (from 3rd parameter), passing info about ramdisk 
to
+kernel via DTB.
+
+DTB and DTBO blobs
+--
+
+``bootm`` command can't just use DTB blob from Android Boot Image (``dtb``
+field), because:
+
+* there is no DTB area in Android Boot Image before v2
+* there may be several DTB blobs in DTB area (e.g. for different SoCs)
+* some DTBO blobs may have to be merged in DTB blobs before booting
+  (e.g. for different boards)
+
+So user has to prepare DTB blob manually and provide it in a 3rd parameter
+of ``bootm`` command. Next commands can be used to do so:
+
+1. ``abootimg``: manipulates Anroid Boot Image, allows one to extract
+   meta-information and payloads from it
+2. ``adtimg``: manipulates Android DTB/DTBO image [3]_, allows one to extract
+   DTB/DTBO blobs from it
+
+In order to use those, please enable next config options::
+
+CONFIG_CMD_ABOOTIMG=y
+CONFIG_CMD_ADTIMG=y
+
+For example, let's assume we have next Android partitions on eMMC:
+
+* ``boot``: contains Android Boot Image v2 (including DTB blobs)
+* ``dtbo``: contains DTBO blobs
+
+Then next command sequence can be used to boot Android:
+
+.. code-block:: bash
+
+=> mmc dev 1
+
+   # Read boot image to RAM (into $loadaddr)
+=> part start mmc 1 boot boot_start
+=> part size mmc 1 boot boot_size
+=> mmc read $loadaddr $boot_start $boot_size
+
+   # Read DTBO image to RAM (into $dtboaddr)
+=> part start mmc 1 dtbo dtbo_start
+=> part size mmc 1 dtbo dtbo_size
+=> mmc read $dtboaddr $dtbo_start $dtbo_size
+
+   # Copy required DTB blob (into $fdtaddr)
+=> abootimg get 

[PATCH v3 5/9] test/py: android: Add test for abootimg

2019-12-24 Thread Sam Protsenko
Unit test for 'abootimg' command. Right now it covers dtb/dtbo
functionality in Android Boot Image v2, which was added recently.

Running test:

$ ./test/py/test.py --bd sandbox --build -k test_abootimg

shows that 1/1 tests passes successfully.

Signed-off-by: Sam Protsenko 
---
 configs/sandbox_defconfig   |   1 +
 test/py/tests/test_android/test_abootimg.py | 159 
 2 files changed, 160 insertions(+)
 create mode 100644 test/py/tests/test_android/test_abootimg.py

diff --git a/configs/sandbox_defconfig b/configs/sandbox_defconfig
index 64245f7cdc..df5aad54bf 100644
--- a/configs/sandbox_defconfig
+++ b/configs/sandbox_defconfig
@@ -25,6 +25,7 @@ CONFIG_ANDROID_AB=y
 CONFIG_CMD_CPU=y
 CONFIG_CMD_LICENSE=y
 CONFIG_CMD_BOOTZ=y
+CONFIG_CMD_ABOOTIMG=y
 # CONFIG_CMD_ELF is not set
 CONFIG_CMD_ASKENV=y
 CONFIG_CMD_GREPENV=y
diff --git a/test/py/tests/test_android/test_abootimg.py 
b/test/py/tests/test_android/test_abootimg.py
new file mode 100644
index 00..8c0939afe9
--- /dev/null
+++ b/test/py/tests/test_android/test_abootimg.py
@@ -0,0 +1,159 @@
+# SPDX-License-Identifier:  GPL-2.0+
+# Copyright (c) 2019, Linaro Limited
+# Author: Sam Protsenko 
+
+# Test U-Boot's "abootimg" commands.
+
+import os
+import pytest
+import u_boot_utils
+
+"""
+These tests rely on disk image (boot.img), which is automatically created by
+the test from the stored hex dump. This is done to avoid the dependency on the
+most recent mkbootimg tool from AOSP/master. Here is the list of commands which
+was used to generate the boot.img and obtain compressed hex dump from it:
+
+$ echo '/dts-v1/; / { model = "x1"; compatible = "y1,z1"; };' > test1.dts
+$ echo '/dts-v1/; / { model = "x2"; compatible = "y2,z2"; };' > test2.dts
+$ dtc test1.dts > dt1.dtb
+$ dtc test2.dts > dt2.dtb
+$ cat dt1.dtb dt2.dtb > dtb.img
+$ echo 'kernel payload' > kernel
+$ echo 'ramdisk payload' > ramdisk.img
+$ mkbootimg --kernel ./kernel --ramdisk ./ramdisk.img  \
+--cmdline "cmdline test" --dtb ./dtb.img   \
+--os_version R --os_patch_level 2019-06-05 \
+--header_version 2 --output boot.img
+$ gzip -9 boot.img
+$ xxd -p boot.img.gz > boot.img.gz.hex
+
+Now one can obtain original boot.img from this hex dump like this:
+
+$ xxd -r -p boot.img.gz.hex boot.img.gz
+$ gunzip -9 boot.img.gz
+"""
+
+# boot.img.gz hex dump
+img_hex = """1f8b08084844af5d0203626f6f742e696d670073f47309f2f77451e46700
+820606010106301084501f04181819041838181898803c3346060c909c9b
+92939997aa50925a5cc2300a461c3078b2e1793c4b876fd92db97939fb6c
+b776207d345446c1281805e8a0868d81e117a45e111c0d8dc101b253
+8bf25273140a122b73f21353b8460364148c8251300a46c1281801a02831
+3725b3387bb401300a46c1281805a360148c207081f7df5b20550bc41640
+9c03c41a0c90f17fe85400986d82452b6c3680198a192a0ce17c3610ae34
+d4a9820881a70f3873f35352731892f3730b124b32937252a96bb9119ae5
+463a5546f82c1f05a360148c8251300a462e85bf67f20020"""
+# Expected response for "abootimg dtb_dump" command
+dtb_dump_resp="""## DTB area contents (concat format):
+ - DTB #0:
+   (DTB)size = 125
+  (DTB)model = x1
+ (DTB)compatible = y1,z1
+ - DTB #1:
+   (DTB)size = 125
+  (DTB)model = x2
+ (DTB)compatible = y2,z2"""
+# Address in RAM where to load the boot image ('abootimg' looks in $loadaddr)
+loadaddr = 0x1000
+# Expected DTB #1 offset from the boot image start address
+dtb1_offset = 0x187d
+# DTB #1 start address in RAM
+dtb1_addr = loadaddr + dtb1_offset
+
+class AbootimgTestDiskImage(object):
+"""Disk image used by abootimg tests."""
+
+def __init__(self, u_boot_console):
+"""Initialize a new AbootimgDiskImage object.
+
+Args:
+u_boot_console: A U-Boot console.
+
+Returns:
+Nothing.
+"""
+
+gz_hex = u_boot_console.config.persistent_data_dir + '/boot.img.gz.hex'
+gz = u_boot_console.config.persistent_data_dir + '/boot.img.gz'
+
+filename = 'boot.img'
+persistent = u_boot_console.config.persistent_data_dir + '/' + filename
+self.path = u_boot_console.config.result_dir  + '/' + filename
+
+with u_boot_utils.persistent_file_helper(u_boot_console.log, 
persistent):
+if os.path.exists(persistent):
+u_boot_console.log.action('Disk image file ' + persistent +
+' already exists')
+else:
+u_boot_console.log.action('Generating ' + persistent)
+
+f = open(gz_hex, "w")
+f.write(img_hex)
+f.close()
+
+cmd = ('xxd', '-r', '-p', gz_hex, gz)
+u_boot_utils.run_and_log(u_boot_console, cmd)
+
+cmd = ('gunzip', '-9', gz)
+u_boot_utils.run_and_log(u_boot_console, cmd)
+
+cmd = ('cp', persistent, self.path)
+u_boot_utils.run_and_log(u_boot_console, cmd)
+
+gtdi = None

[PATCH v3 0/9] am57xx: Implement Android 10 boot flow

2019-12-24 Thread Sam Protsenko
Android 10 brings a lot of new requirements for bootloaders: [1]. This
patch series attempts to implement such a boot process on BeagleBoard
X15 platform. Some common code is added too, which can be reused later
for other platforms (see "abootimg" command and associated C API).

This patch series must be applied on top of these recently sent patches
by Eugeniu:

[PATCH 0/3] cmd: dtimg: Rename to adtimg and refactor usage style

Changes in v3:
 - rename command to "abootimg" (requested by Simon Glass)
 - rework command interface (as discussed with Eugeniu)
 - add command documentation
 - address other comments

[1] https://source.android.com/devices/bootloader

Sam Protsenko (9):
  image: android: Add functions for handling dtb field
  image: android: Add routine to get dtbo params
  cmd: abootimg: Add abootimg command
  doc: android: Add documentation for Android Boot Image
  test/py: android: Add test for abootimg
  configs: am57xx_evm: Enable Android commands
  env: ti: boot: Respect slot_suffix in AVB commands
  env: ti: boot: Boot Android with dynamic partitions
  arm: ti: boot: Use correct dtb and dtbo on Android boot

 cmd/Kconfig |  10 +
 cmd/Makefile|   1 +
 cmd/abootimg.c  | 242 +
 common/Makefile |   2 +-
 common/image-android.c  | 275 
 configs/am57xx_evm_defconfig|   6 +
 configs/am57xx_hs_evm_defconfig |   6 +
 configs/am57xx_hs_evm_usb_defconfig |   6 +
 configs/sandbox_defconfig   |   1 +
 doc/android/boot-image.rst  | 154 +++
 include/configs/ti_armv7_common.h   |   7 +
 include/environment/ti/boot.h   | 146 ++-
 include/image.h |   6 +
 test/py/tests/test_android/test_abootimg.py | 159 +++
 14 files changed, 954 insertions(+), 67 deletions(-)
 create mode 100644 cmd/abootimg.c
 create mode 100644 doc/android/boot-image.rst
 create mode 100644 test/py/tests/test_android/test_abootimg.py

-- 
2.24.0



Re: [PATCH 0/3] cmd: dtimg: Rename to adtimg and refactor usage style

2019-12-24 Thread Sam Protsenko
Hi

On Tue, Dec 24, 2019 at 6:51 PM Eugeniu Rosca  wrote:
>
> The main focus of this series is to prepare the ground for:
>  - adding the id/rev-based DT lookup in dtimg/adtimg [1]
>  - adding the abootimg command [2]
>
> To be more clear, the above topics are handled _outside_ of this series,
> but they treat this series as hard dependency.
>
> The per-patch motivation and testing are described meticulously
> in each commit.
>
> [1] https://patchwork.ozlabs.org/patch/1202580/
> ("cmd: dtimg: Get start and size based on --id and --rev")
> [2] https://patchwork.ozlabs.org/patch/1182212/
> ("cmd: bootimg: Add bootimg command")
>
> Eugeniu Rosca (3):
>   dtimg/am57xx_evm_defconfig: Rename dtimg to adtimg
>   cmd: adtimg: Rename internal symbols
>   cmd: adtimg: Refactor usage style
>

For the whole series:
Reviewed-by: Sam Protsenko 

>  cmd/Kconfig |   4 +-
>  cmd/Makefile|   2 +-
>  cmd/adtimg.c| 242 
>  cmd/dtimg.c | 142 
>  common/Makefile |   2 +-
>  configs/am57xx_evm_defconfig|   2 +-
>  configs/am57xx_hs_evm_defconfig |   2 +-
>  configs/am57xx_hs_evm_usb_defconfig |   2 +-
>  8 files changed, 249 insertions(+), 149 deletions(-)
>  create mode 100644 cmd/adtimg.c
>  delete mode 100644 cmd/dtimg.c
>
> --
> 2.24.0
>


Re: [PATCH 1/1] test/py: Fix broken 'notbuildconfigspec' marker

2019-12-24 Thread Stephen Warren
On 12/24/19 8:19 AM, Cristian Ciocaltea wrote:
> Consider the following test sample:
> 
> @pytest.mark.buildconfigspec('fit')
> @pytest.mark.notbuildconfigspec('generate_acpi_table')
> def test_sample(u_boot_console):
> 
> Whatever the argument of the 'notbuildconfigspec' is,
> the test ends up being skipped with the message:
> 
> ('/uboot/test/py/conftest.py', 463,
>  'Skipped: .config feature "fit" enabled')

Reviewed-by: Stephen Warren 


Re: [PATCH 1/3] efi_loader: Add guidcpy function

2019-12-24 Thread Heinrich Schuchardt

On 12/24/19 4:54 PM, Sughosh Ganu wrote:

Add guidcpy function to copy the source guid to the destination
guid.

Signed-off-by: Sughosh Ganu 
---
  include/efi_loader.h | 5 +
  1 file changed, 5 insertions(+)

diff --git a/include/efi_loader.h b/include/efi_loader.h
index 16a1b25..bec7873 100644
--- a/include/efi_loader.h
+++ b/include/efi_loader.h
@@ -17,6 +17,11 @@ static inline int guidcmp(const void *g1, const void *g2)
return memcmp(g1, g2, sizeof(efi_guid_t));
  }

+static inline void *guidcpy(efi_guid_t *dst, const efi_guid_t *src)
+{
+   return memcpy(dst, src, sizeof(*dst));


If we introduce this function, here are other places to use it:

lib/efi_loader/efi_boottime.c:1404:
memcpy(>protocol, protocol, sizeof(efi_guid_t));
lib/efi_loader/efi_boottime.c:1635:
memcpy([i].guid, guid, sizeof(*guid));

Reviewed-by: Heinrich Schuchardt 


+}
+
  /* No need for efi loader support in SPL */
  #if CONFIG_IS_ENABLED(EFI_LOADER)






Re: [PATCH 2/3] efi: qemu: arm64: Add efi_rng_protocol implementation for the platform

2019-12-24 Thread Heinrich Schuchardt

On 12/24/19 4:54 PM, Sughosh Ganu wrote:

Add support for the EFI_RNG_PROTOCOL routines for the qemu arm64
platform. EFI_RNG_PROTOCOL is an uefi boottime service which is
invoked by the efi stub in the kernel for getting random seed for
kaslr.

The routines are platform specific, and use the virtio-rng device on
the platform to get random data.

The feature can be enabled through the following config
CONFIG_EFI_RNG_PROTOCOL

Signed-off-by: Sughosh Ganu 
---
  board/emulation/qemu-arm/qemu-arm.c | 50 +
  include/efi_rng.h   | 34 +
  lib/efi_loader/Kconfig  |  8 
  lib/efi_loader/Makefile |  1 +
  lib/efi_loader/efi_rng.c| 74 +
  5 files changed, 167 insertions(+)
  create mode 100644 include/efi_rng.h
  create mode 100644 lib/efi_loader/efi_rng.c

diff --git a/board/emulation/qemu-arm/qemu-arm.c 
b/board/emulation/qemu-arm/qemu-arm.c
index e1f4709..3176421 100644
--- a/board/emulation/qemu-arm/qemu-arm.c
+++ b/board/emulation/qemu-arm/qemu-arm.c
@@ -91,3 +91,53 @@ void *board_fdt_blob_setup(void)
/* QEMU loads a generated DTB for us at the start of RAM. */
return (void *)CONFIG_SYS_SDRAM_BASE;
  }
+
+#if defined(CONFIG_EFI_RNG_PROTOCOL)
+#include 
+#include 
+
+#include 
+
+#define VIRTIO_RNG_PCI_DEVICE  "virtio-pci.l#0"
+
+void platform_rng_getinfo(efi_rng_algorithm *rng_algo)


Thanks for working on the implementation of the EFI_RNG_PROTOCOL.

Please, put an underscore after each word: platform_rng_get_info


+{
+   const efi_guid_t rng_raw_guid = EFI_RNG_ALGORITHM_RAW;
+
+   guidcpy(rng_algo, _raw_guid);


This function should be in efi_rng.c if it is needed at all.


+}
+
+efi_status_t platform_get_rng_device(struct udevice **dev)
+{


Here you are creating platform specific code. The idea of the driver
model in U-Boot is to separate duties.

So the implementation of the EFI_RNG_PROTOCOL should be platform
agnostic and rely simply on looping of the devices of UCLASS_RNG.

Please, move function platform_get_rng_device into the RNG uclass.


+   int ret;
+   efi_status_t status = EFI_DEVICE_ERROR;
+   struct udevice *bus, *devp;
+
+   for (uclass_first_device(UCLASS_VIRTIO, ); bus;
+uclass_next_device()) {
+   for (device_find_first_child(bus, ); devp; 
device_find_next_child()) {
+   if (device_get_uclass_id(devp) == UCLASS_RNG) {
+   *dev = devp;
+   status = EFI_SUCCESS;
+   break;
+   }
+   }
+   }
+
+   if (status != EFI_SUCCESS) {
+   debug("No rng device found\n");
+   return EFI_DEVICE_ERROR;
+   }
+
+   if (*dev) {
+   ret = device_probe(*dev);
+   if (ret)
+   return EFI_DEVICE_ERROR;
+   } else {
+   debug("Couldn't get child device\n");
+   return EFI_DEVICE_ERROR;
+   }
+
+   return EFI_SUCCESS;
+}
+#endif /* CONFIG_EFI_RNG_PROTOCOL */
diff --git a/include/efi_rng.h b/include/efi_rng.h
new file mode 100644
index 000..df749dd
--- /dev/null
+++ b/include/efi_rng.h
@@ -0,0 +1,34 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (c) 2019, Linaro Limited
+ */
+
+#if !defined _EFI_RNG_H_
+#define _EFI_RNG_H_
+
+#include 
+#include 
+
+#define EFI_RNG_PROTOCOL_GUID \
+   EFI_GUID(0x3152bca5, 0xeade, 0x433d, 0x86, 0x2e, \
+0xc0, 0x1c, 0xdc, 0x29, 0x1f, 0x44)
+
+#define EFI_RNG_ALGORITHM_RAW \
+   EFI_GUID(0xe43176d7, 0xb6e8, 0x4827, 0xb7, 0x84, \
+0x7f, 0xfd, 0xc4, 0xb6, 0x85, 0x61)


Please, add a comment line for each of the GUIDs.


+
+typedef efi_guid_t efi_rng_algorithm;


We want to avoid typedefs in U-Boot.


+
+struct efi_rng_protocol {
+   efi_status_t (EFIAPI *getinfo)(struct efi_rng_protocol *this,


get_info


+  efi_uintn_t *rng_algo_size,


Please, use the parameter names from the UEFI 2.8 specification:

rng_algorithm_list_size


+  efi_rng_algorithm *rng_algo);


rng_algorithm_list


+   efi_status_t (EFIAPI *getrng)(struct efi_rng_protocol *this,


get_rng


+ efi_rng_algorithm *rng_algo,


efi_guid_t *rng_algorithm


+ efi_uintn_t rng_len, uint8_t *rng_data);


rng_value_length, rng_value


+};
+
+void platform_rng_getinfo(efi_rng_algorithm *rng_algo);
+efi_status_t platform_get_rng_device(struct udevice **dev);
+
+#endif /* _EFI_RNG_H_ */
diff --git a/lib/efi_loader/Kconfig b/lib/efi_loader/Kconfig
index 21ef440..7437442 100644
--- a/lib/efi_loader/Kconfig
+++ b/lib/efi_loader/Kconfig
@@ -120,4 +120,12 @@ config EFI_GRUB_ARM32_WORKAROUND
  GRUB prior to version 2.04 requires U-Boot to disable caches. This
  workaround 

Re: pull request u-boot-mpc85xx to next branch

2019-12-24 Thread Tom Rini
On Tue, Dec 24, 2019 at 10:17:57AM +, Priyanka Jain wrote:

> Dear Tom,
> 
> 
> 
> Please find my pull-request for u-boot-mpc85xx/master
> 
> https://travis-ci.org/p-priyanka-jain/u-boot/builds/628982496
> 
> Summary
> 
> enable DM driver on ppc/km boards
> 
> enable DM_USB for some of NXP powerpc platforms: P5040, T4240, T208x, T104x, 
> P4080, P2041, P2020, P1020, P3041
> 
> Some updates in mpc85xx-ddr driver, km boards
> 
> 
> 
> priyankajain
> 

Applied to u-boot/next, thanks!

-- 
Tom


signature.asc
Description: PGP signature


[PATCH 3/3] cmd: adtimg: Refactor usage style

2019-12-24 Thread Eugeniu Rosca
Trying to extend 'adtimg' functionality [1], we've been severely hit
by a major limitation in the command's usage scheme. Specifically, the
command's user interface appears to be too centric to getting the
DTB/DTBO entry [3] based on the index of the desired DT in the image,
which makes it really difficult retrieving the DT entry based on
alternative criteria (e.g. filtering by id/rev fields), the
latter being demanded by real life customer use-cases [1].

This went to the point of receiving below feedback from Sam [2]:

 -- snip --
 As for 'dtimg' command: after giving it some thought, I think not much
 people using it yet. So in this particular case I don't have some
 strong preference, and if you think the 'dtimg' interface is ugly, and
 it overcomes "don't break interfaces" rule, maybe now is a good time
 to rework it (before it gets widely used).
 -- snip --

Given the above, rework the usage pattern from [4] to [5], in order to
allow an intuitive enablement of "by id|rev" DT search [6].

[1] https://patchwork.ozlabs.org/cover/1202575/
("cmd: dtimg: Enhance with --id and --rev options (take #1)")
[2] https://patchwork.ozlabs.org/patch/1182207/#2317020
[3] https://source.android.com/devices/architecture/dto/partitions
[4] Old usage
adtimg dump - Print image contents
adtimg start- Get DT address by index
adtimg size - Get DT size by index

[5] New usage
adtimg addr   - Set image location to 
adtimg dump - Print out image contents
adtimg get dt --index= [avar [svar]] - Get DT address and size by index

[6] Soon-to-be-provided "by id|rev" add-on functionality
adtimg get dt --id= --rev= [avar [svar [ivar]]]
 - Get DT address/size/index by id|rev fields

Cc: Sam Protsenko 
Signed-off-by: Eugeniu Rosca 
---
 cmd/adtimg.c | 217 +--
 1 file changed, 158 insertions(+), 59 deletions(-)

diff --git a/cmd/adtimg.c b/cmd/adtimg.c
index 22b4f5e1a83f..60bb01c3498a 100644
--- a/cmd/adtimg.c
+++ b/cmd/adtimg.c
@@ -2,18 +2,22 @@
 /*
  * (C) Copyright 2018 Linaro Ltd.
  * Sam Protsenko 
+ * Eugeniu Rosca 
  */
 
 #include 
 #include 
 #include 
 
-enum cmd_adtimg_info {
-   CMD_ADTIMG_START = 0,
-   CMD_ADTIMG_SIZE,
-};
+#define OPT_INDEX  "--index"
 
-static int do_adtimg_dump(cmd_tbl_t *cmdtp, int flag, int argc,
+/*
+ * Current/working DTB/DTBO Android image address.
+ * Similar to 'working_fdt' variable in 'fdt' command.
+ */
+static ulong working_img;
+
+static int do_adtimg_addr(cmd_tbl_t *cmdtp, int flag, int argc,
  char * const argv[])
 {
char *endp;
@@ -24,86 +28,185 @@ static int do_adtimg_dump(cmd_tbl_t *cmdtp, int flag, int 
argc,
 
hdr_addr = simple_strtoul(argv[1], , 16);
if (*endp != '\0') {
-   printf("Error: Wrong image address\n");
+   printf("Error: Wrong image address '%s'\n", argv[1]);
return CMD_RET_FAILURE;
}
 
-   if (!android_dt_check_header(hdr_addr)) {
-   printf("Error: DT image header is incorrect\n");
+   /*
+* Allow users to set an address prior to copying the DTB/DTBO
+* image to that same address, i.e. skip header verification.
+*/
+
+   working_img = hdr_addr;
+   return CMD_RET_SUCCESS;
+}
+
+static int adtimg_check_working_img(void)
+{
+   if (!working_img) {
+   printf("Error: Please, call 'adtimg addr '. Aborting!\n");
return CMD_RET_FAILURE;
}
 
-   android_dt_print_contents(hdr_addr);
+   if (!android_dt_check_header(working_img)) {
+   printf("Error: Invalid image header at 0x%lx\n", working_img);
+   return CMD_RET_FAILURE;
+   }
 
return CMD_RET_SUCCESS;
 }
 
-static int adtimg_get_fdt(int argc, char * const argv[],
- enum cmd_adtimg_info cmd)
+static int do_adtimg_dump(cmd_tbl_t *cmdtp, int flag, int argc,
+ char * const argv[])
 {
-   ulong hdr_addr;
-   u32 index;
-   char *endp;
-   ulong fdt_addr;
-   u32 fdt_size;
-   char buf[65];
-
-   if (argc != 4)
+   if (argc != 1)
return CMD_RET_USAGE;
 
-   hdr_addr = simple_strtoul(argv[1], , 16);
-   if (*endp != '\0') {
-   printf("Error: Wrong image address\n");
+   if (adtimg_check_working_img() != CMD_RET_SUCCESS)
+   return CMD_RET_FAILURE;
+
+   android_dt_print_contents(working_img);
+
+   return CMD_RET_SUCCESS;
+}
+
+static int adtimg_getopt_u32(char * const opt, char * const name, u32 *optval)
+{
+   char *endp, *str;
+   u32 val;
+
+   if (!opt || !name || !optval)
+   return CMD_RET_FAILURE;
+
+   str = strchr(opt, '=');
+   if (!str) {
+   printf("Error: Option '%s' not followed by '='\n", name);
return CMD_RET_FAILURE;
}
 
-   if 

[PATCH 2/3] cmd: adtimg: Rename internal symbols

2019-12-24 Thread Eugeniu Rosca
With 'dtimg.c' renamed to 'adtimg.c', now ensure the naming
consistency in the internal implementation of 'adtimg.c'.

No functional change intended.

Cc: Sam Protsenko 
Signed-off-by: Eugeniu Rosca 
---
 cmd/adtimg.c | 51 ++-
 1 file changed, 26 insertions(+), 25 deletions(-)

diff --git a/cmd/adtimg.c b/cmd/adtimg.c
index 6c5d53cc6808..22b4f5e1a83f 100644
--- a/cmd/adtimg.c
+++ b/cmd/adtimg.c
@@ -8,13 +8,13 @@
 #include 
 #include 
 
-enum cmd_dtimg_info {
-   CMD_DTIMG_START = 0,
-   CMD_DTIMG_SIZE,
+enum cmd_adtimg_info {
+   CMD_ADTIMG_START = 0,
+   CMD_ADTIMG_SIZE,
 };
 
-static int do_dtimg_dump(cmd_tbl_t *cmdtp, int flag, int argc,
-char * const argv[])
+static int do_adtimg_dump(cmd_tbl_t *cmdtp, int flag, int argc,
+ char * const argv[])
 {
char *endp;
ulong hdr_addr;
@@ -38,7 +38,8 @@ static int do_dtimg_dump(cmd_tbl_t *cmdtp, int flag, int argc,
return CMD_RET_SUCCESS;
 }
 
-static int dtimg_get_fdt(int argc, char * const argv[], enum cmd_dtimg_info 
cmd)
+static int adtimg_get_fdt(int argc, char * const argv[],
+ enum cmd_adtimg_info cmd)
 {
ulong hdr_addr;
u32 index;
@@ -71,14 +72,14 @@ static int dtimg_get_fdt(int argc, char * const argv[], 
enum cmd_dtimg_info cmd)
return CMD_RET_FAILURE;
 
switch (cmd) {
-   case CMD_DTIMG_START:
+   case CMD_ADTIMG_START:
snprintf(buf, sizeof(buf), "%lx", fdt_addr);
break;
-   case CMD_DTIMG_SIZE:
+   case CMD_ADTIMG_SIZE:
snprintf(buf, sizeof(buf), "%x", fdt_size);
break;
default:
-   printf("Error: Unknown cmd_dtimg_info value: %d\n", cmd);
+   printf("Error: Unknown cmd_adtimg_info value: %d\n", cmd);
return CMD_RET_FAILURE;
}
 
@@ -87,31 +88,31 @@ static int dtimg_get_fdt(int argc, char * const argv[], 
enum cmd_dtimg_info cmd)
return CMD_RET_SUCCESS;
 }
 
-static int do_dtimg_start(cmd_tbl_t *cmdtp, int flag, int argc,
- char * const argv[])
+static int do_adtimg_start(cmd_tbl_t *cmdtp, int flag, int argc,
+  char * const argv[])
 {
-   return dtimg_get_fdt(argc, argv, CMD_DTIMG_START);
+   return adtimg_get_fdt(argc, argv, CMD_ADTIMG_START);
 }
 
-static int do_dtimg_size(cmd_tbl_t *cmdtp, int flag, int argc,
-char * const argv[])
+static int do_adtimg_size(cmd_tbl_t *cmdtp, int flag, int argc,
+ char * const argv[])
 {
-   return dtimg_get_fdt(argc, argv, CMD_DTIMG_SIZE);
+   return adtimg_get_fdt(argc, argv, CMD_ADTIMG_SIZE);
 }
 
-static cmd_tbl_t cmd_dtimg_sub[] = {
-   U_BOOT_CMD_MKENT(dump, 2, 0, do_dtimg_dump, "", ""),
-   U_BOOT_CMD_MKENT(start, 4, 0, do_dtimg_start, "", ""),
-   U_BOOT_CMD_MKENT(size, 4, 0, do_dtimg_size, "", ""),
+static cmd_tbl_t cmd_adtimg_sub[] = {
+   U_BOOT_CMD_MKENT(dump, 2, 0, do_adtimg_dump, "", ""),
+   U_BOOT_CMD_MKENT(start, 4, 0, do_adtimg_start, "", ""),
+   U_BOOT_CMD_MKENT(size, 4, 0, do_adtimg_size, "", ""),
 };
 
-static int do_dtimg(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+static int do_adtimg(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 {
cmd_tbl_t *cp;
 
-   cp = find_cmd_tbl(argv[1], cmd_dtimg_sub, ARRAY_SIZE(cmd_dtimg_sub));
+   cp = find_cmd_tbl(argv[1], cmd_adtimg_sub, ARRAY_SIZE(cmd_adtimg_sub));
 
-   /* Strip off leading 'dtimg' command argument */
+   /* Strip off leading 'adtimg' command argument */
argc--;
argv++;
 
@@ -124,17 +125,17 @@ static int do_dtimg(cmd_tbl_t *cmdtp, int flag, int argc, 
char * const argv[])
 }
 
 U_BOOT_CMD(
-   dtimg, CONFIG_SYS_MAXARGS, 0, do_dtimg,
+   adtimg, CONFIG_SYS_MAXARGS, 0, do_adtimg,
"manipulate dtb/dtbo Android image",
"dump \n"
"- parse specified image and print its structure info\n"
"  : image address in RAM, in hex\n"
-   "dtimg start   \n"
+   "adtimg start   \n"
"- get address (hex) of FDT in the image, by index\n"
"  : image address in RAM, in hex\n"
"  : index of desired FDT in the image\n"
"  : name of variable where to store address of FDT\n"
-   "dtimg size   \n"
+   "adtimg size   \n"
"- get size (hex, bytes) of FDT in the image, by index\n"
"  : image address in RAM, in hex\n"
"  : index of desired FDT in the image\n"
-- 
2.24.0



[PATCH 1/3] dtimg/am57xx_evm_defconfig: Rename dtimg to adtimg

2019-12-24 Thread Eugeniu Rosca
Rename the existing 'dtimg' command to 'adtimg', in order to:
 - Suggest the Android origins and scope
 - Be consistent with the upcoming 'abootimg' command (naming
   suggested by Simon [*])

The change in _not_ backward compatible, but its benefits outweigh its
downsides, given that we don't expect active users of 'dtimg' today.

Perform the rename in several steps:
 1. Rename *.c file and Kconfig symbol. This should allow
'git log --follow' to properly track the history of 'adtimg.c'
 2. 's/dtimg/adtimg/g' in the internal namespace of 'adtimg.c'

ELF comparison [**] before and after shows no functional change.

[*] https://patchwork.ozlabs.org/patch/1182212/#2291600
[**] diff -u <(objdump -d cmd/dtimg.o) <(objdump -d cmd/adtimg.o)

Cc: Tom Rini 
Cc: Sam Protsenko 
Cc: Simon Glass 
Signed-off-by: Eugeniu Rosca 
---
 cmd/Kconfig | 4 ++--
 cmd/Makefile| 2 +-
 cmd/{dtimg.c => adtimg.c}   | 0
 common/Makefile | 2 +-
 configs/am57xx_evm_defconfig| 2 +-
 configs/am57xx_hs_evm_defconfig | 2 +-
 configs/am57xx_hs_evm_usb_defconfig | 2 +-
 7 files changed, 7 insertions(+), 7 deletions(-)
 rename cmd/{dtimg.c => adtimg.c} (100%)

diff --git a/cmd/Kconfig b/cmd/Kconfig
index 1e4cf146c509..f63adbdc3a31 100644
--- a/cmd/Kconfig
+++ b/cmd/Kconfig
@@ -348,8 +348,8 @@ config CMD_BOOTMENU
help
  Add an ANSI terminal boot menu command.
 
-config CMD_DTIMG
-   bool "dtimg"
+config CMD_ADTIMG
+   bool "adtimg"
help
  Android DTB/DTBO image manipulation commands. Read dtb/dtbo files from
  image into RAM, dump image structure information, etc. Those dtb/dtbo
diff --git a/cmd/Makefile b/cmd/Makefile
index 3ac710454652..c17ee20b25c6 100644
--- a/cmd/Makefile
+++ b/cmd/Makefile
@@ -47,7 +47,7 @@ obj-$(CONFIG_CMD_SOUND) += sound.o
 ifdef CONFIG_POST
 obj-$(CONFIG_CMD_DIAG) += diag.o
 endif
-obj-$(CONFIG_CMD_DTIMG) += dtimg.o
+obj-$(CONFIG_CMD_ADTIMG) += adtimg.o
 obj-$(CONFIG_CMD_ECHO) += echo.o
 obj-$(CONFIG_ENV_IS_IN_EEPROM) += eeprom.o
 obj-$(CONFIG_CMD_EEPROM) += eeprom.o
diff --git a/cmd/dtimg.c b/cmd/adtimg.c
similarity index 100%
rename from cmd/dtimg.c
rename to cmd/adtimg.c
diff --git a/common/Makefile b/common/Makefile
index 302d8beaf356..029cc0f2ce6b 100644
--- a/common/Makefile
+++ b/common/Makefile
@@ -117,7 +117,7 @@ obj-$(CONFIG_IO_TRACE) += iotrace.o
 obj-y += memsize.o
 obj-y += stdio.o
 
-obj-$(CONFIG_CMD_DTIMG) += image-android-dt.o
+obj-$(CONFIG_CMD_ADTIMG) += image-android-dt.o
 
 ifdef CONFIG_CMD_EEPROM_LAYOUT
 obj-y += eeprom/eeprom_field.o eeprom/eeprom_layout.o
diff --git a/configs/am57xx_evm_defconfig b/configs/am57xx_evm_defconfig
index ae183e9b562c..0c6a2e9193b9 100644
--- a/configs/am57xx_evm_defconfig
+++ b/configs/am57xx_evm_defconfig
@@ -29,7 +29,7 @@ CONFIG_SPL_OS_BOOT=y
 CONFIG_SPL_SPI_LOAD=y
 CONFIG_SYS_SPI_U_BOOT_OFFS=0x4
 CONFIG_SPL_YMODEM_SUPPORT=y
-CONFIG_CMD_DTIMG=y
+CONFIG_CMD_ADTIMG=y
 CONFIG_CMD_SPL=y
 CONFIG_CMD_BCB=y
 # CONFIG_CMD_FLASH is not set
diff --git a/configs/am57xx_hs_evm_defconfig b/configs/am57xx_hs_evm_defconfig
index 800ec6c70b92..3c57dfb031a9 100644
--- a/configs/am57xx_hs_evm_defconfig
+++ b/configs/am57xx_hs_evm_defconfig
@@ -32,7 +32,7 @@ CONFIG_SPL_DMA_SUPPORT=y
 # CONFIG_SPL_NAND_SUPPORT is not set
 CONFIG_SPL_SPI_LOAD=y
 CONFIG_SYS_SPI_U_BOOT_OFFS=0x4
-CONFIG_CMD_DTIMG=y
+CONFIG_CMD_ADTIMG=y
 CONFIG_CMD_BCB=y
 # CONFIG_CMD_FLASH is not set
 # CONFIG_CMD_SETEXPR is not set
diff --git a/configs/am57xx_hs_evm_usb_defconfig 
b/configs/am57xx_hs_evm_usb_defconfig
index f2cbf2fe2ba1..87f391c2b029 100644
--- a/configs/am57xx_hs_evm_usb_defconfig
+++ b/configs/am57xx_hs_evm_usb_defconfig
@@ -37,7 +37,7 @@ CONFIG_SYS_SPI_U_BOOT_OFFS=0x4
 CONFIG_SPL_USB_GADGET=y
 CONFIG_SPL_DFU=y
 CONFIG_SPL_YMODEM_SUPPORT=y
-CONFIG_CMD_DTIMG=y
+CONFIG_CMD_ADTIMG=y
 CONFIG_CMD_BCB=y
 # CONFIG_CMD_FLASH is not set
 # CONFIG_CMD_SETEXPR is not set
-- 
2.24.0



[PATCH 0/3] cmd: dtimg: Rename to adtimg and refactor usage style

2019-12-24 Thread Eugeniu Rosca
The main focus of this series is to prepare the ground for:
 - adding the id/rev-based DT lookup in dtimg/adtimg [1]
 - adding the abootimg command [2]

To be more clear, the above topics are handled _outside_ of this series,
but they treat this series as hard dependency.

The per-patch motivation and testing are described meticulously
in each commit.

[1] https://patchwork.ozlabs.org/patch/1202580/
("cmd: dtimg: Get start and size based on --id and --rev")
[2] https://patchwork.ozlabs.org/patch/1182212/
("cmd: bootimg: Add bootimg command")

Eugeniu Rosca (3):
  dtimg/am57xx_evm_defconfig: Rename dtimg to adtimg
  cmd: adtimg: Rename internal symbols
  cmd: adtimg: Refactor usage style

 cmd/Kconfig |   4 +-
 cmd/Makefile|   2 +-
 cmd/adtimg.c| 242 
 cmd/dtimg.c | 142 
 common/Makefile |   2 +-
 configs/am57xx_evm_defconfig|   2 +-
 configs/am57xx_hs_evm_defconfig |   2 +-
 configs/am57xx_hs_evm_usb_defconfig |   2 +-
 8 files changed, 249 insertions(+), 149 deletions(-)
 create mode 100644 cmd/adtimg.c
 delete mode 100644 cmd/dtimg.c

-- 
2.24.0



[PATCH v4 5/5] test/py: Create a test for launching UEFI binaries from FIT images

2019-12-24 Thread Cristian Ciocaltea
This test verifies the implementation of the 'bootm' extension that
handles UEFI binaries inside FIT images (enabled via CONFIG_BOOTM_EFI).

Signed-off-by: Cristian Ciocaltea 
---
 test/py/tests/test_efi_fit.py | 459 ++
 1 file changed, 459 insertions(+)
 create mode 100644 test/py/tests/test_efi_fit.py

diff --git a/test/py/tests/test_efi_fit.py b/test/py/tests/test_efi_fit.py
new file mode 100644
index 00..e1f0e42694
--- /dev/null
+++ b/test/py/tests/test_efi_fit.py
@@ -0,0 +1,459 @@
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (c) 2019, Cristian Ciocaltea 
+#
+# Work based on:
+# - test_net.py
+# Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
+# - test_fit.py
+# Copyright (c) 2013, Google Inc.
+#
+# Test launching UEFI binaries from FIT images.
+
+import os.path
+import pytest
+import u_boot_utils as util
+
+"""
+Note: This test relies on boardenv_* containing configuration values to define
+which network environment is available for testing. Without this, the parts
+that rely on network will be automatically skipped.
+
+For example:
+
+# Boolean indicating whether the Ethernet device is attached to USB, and hence
+# USB enumeration needs to be performed prior to network tests.
+# This variable may be omitted if its value is False.
+env__net_uses_usb = False
+
+# Boolean indicating whether the Ethernet device is attached to PCI, and hence
+# PCI enumeration needs to be performed prior to network tests.
+# This variable may be omitted if its value is False.
+env__net_uses_pci = True
+
+# True if a DHCP server is attached to the network, and should be tested.
+# If DHCP testing is not possible or desired, this variable may be omitted or
+# set to False.
+env__net_dhcp_server = True
+
+# A list of environment variables that should be set in order to configure a
+# static IP. If solely relying on DHCP, this variable may be omitted or set to
+# an empty list.
+env__net_static_env_vars = [
+('ipaddr', '10.0.0.100'),
+('netmask', '255.255.255.0'),
+('serverip', '10.0.0.1'),
+]
+
+# Details regarding a file that may be read from a TFTP server. This variable
+# may be omitted or set to None if TFTP testing is not possible or desired.
+# Additionally, when the 'size' is not available, the file will be generated
+# automatically in the TFTP root directory, as specified by the 'dn' field.
+env__efi_fit_tftp_file = {
+'fn': 'test-efi-fit.img',   # File path relative to TFTP root
+'size': 3831,   # File size
+'crc32': '9fa3f79c',# Checksum using CRC-32 algorithm, optional
+'addr': '$kernel_addr_r',   # Loading address, optional
+'dn': 'tftp/root/dir',  # TFTP root directory path, optional
+}
+"""
+
+# Define the parametrized ITS data to be used for FIT images generation.
+its_data = '''
+/dts-v1/;
+
+/ {
+description = "EFI image with FDT blob";
+#address-cells = <1>;
+
+images {
+efi {
+description = "Test EFI";
+data = /incbin/("%(efi-bin)s");
+type = "%(kernel-type)s";
+arch = "%(sys-arch)s";
+os = "efi";
+compression = "%(efi-comp)s";
+load = <0x0>;
+entry = <0x0>;
+};
+fdt {
+description = "Test FDT";
+data = /incbin/("%(fdt-bin)s");
+type = "flat_dt";
+arch = "%(sys-arch)s";
+compression = "%(fdt-comp)s";
+};
+};
+
+configurations {
+default = "config-efi-fdt";
+config-efi-fdt {
+description = "EFI FIT w/ FDT";
+kernel = "efi";
+fdt = "fdt";
+};
+config-efi-nofdt {
+description = "EFI FIT w/o FDT";
+kernel = "efi";
+};
+};
+};
+'''
+
+# Define the parametrized FDT data to be used for DTB images generation.
+fdt_data = '''
+/dts-v1/;
+
+/ {
+#address-cells = <1>;
+#size-cells = <0>;
+
+model = "%(sys-arch)s %(fdt_type)s EFI FIT Boot Test";
+compatible = "%(sys-arch)s";
+
+reset@0 {
+compatible = "%(sys-arch)s,reset";
+reg = <0>;
+};
+};
+'''
+
+@pytest.mark.buildconfigspec('bootm_efi')
+@pytest.mark.buildconfigspec('cmd_bootefi_hello_compile')
+@pytest.mark.buildconfigspec('fit')
+@pytest.mark.notbuildconfigspec('generate_acpi_table')
+@pytest.mark.requiredtool('dtc')
+def test_efi_fit_launch(u_boot_console):
+"""Test handling of UEFI binaries inside FIT images.
+
+The tests are trying to launch U-Boot's helloworld.efi embedded into
+FIT images, in uncompressed or gzip compressed format.
+
+Additionally, a sample FDT blob is created and embedded into the above
+mentioned FIT images, in uncompressed or gzip compressed format.
+
+For more details, see launch_efi().
+
+The following test cases are currently defined and enabled:
+ - Launch uncompressed FIT EFI & internal FDT
+ - Launch uncompressed FIT EFI & FIT FDT
+ 

[PATCH v4 4/5] doc: uefi.rst: Document launching UEFI binaries from FIT images

2019-12-24 Thread Cristian Ciocaltea
This patch adds a new section "Launching a UEFI binary from a FIT image"
documenting the usage of the CONFIG_BOOTM_EFI extension to bootm command
that offers a verified boot alternative for UEFI binaries such as GRUB2.

Signed-off-by: Cristian Ciocaltea 
Reviewed-by: Heinrich Schuchardt 
---
 doc/uefi/uefi.rst | 34 ++
 1 file changed, 34 insertions(+)

diff --git a/doc/uefi/uefi.rst b/doc/uefi/uefi.rst
index db942df694..a8fd886d6b 100644
--- a/doc/uefi/uefi.rst
+++ b/doc/uefi/uefi.rst
@@ -63,6 +63,40 @@ The environment variable 'bootargs' is passed as load 
options in the UEFI system
 table. The Linux kernel EFI stub uses the load options as command line
 arguments.
 
+Launching a UEFI binary from a FIT image
+
+
+A signed FIT image can be used to securely boot a UEFI image via the
+bootm command. This feature is available if U-Boot is configured with::
+
+CONFIG_BOOTM_EFI=y
+
+A sample configuration is provided as file doc/uImage.FIT/uefi.its.
+
+Below you find the output of an example session starting GRUB::
+
+=> load mmc 0:1 ${kernel_addr_r} image.fit
+4620426 bytes read in 83 ms (53.1 MiB/s)
+=> bootm ${kernel_addr_r}#config-grub-nofdt
+## Loading kernel from FIT Image at 4040 ...
+   Using 'config-grub-nofdt' configuration
+   Verifying Hash Integrity ... sha256,rsa2048:dev+ OK
+   Trying 'efi-grub' kernel subimage
+ Description:  GRUB EFI Firmware
+ Created:  2019-11-20   8:18:16 UTC
+ Type: Kernel Image (no loading done)
+ Compression:  uncompressed
+ Data Start:   0x404000d0
+ Data Size:450560 Bytes = 440 KiB
+ Hash algo:sha256
+ Hash value:   
4dbee00021112df618f58b3f7cf5e1595533d543094064b9ce991e8b054a9eec
+   Verifying Hash Integrity ... sha256+ OK
+   XIP Kernel Image (no loading done)
+## Transferring control to EFI (at address 404000d0) ...
+Welcome to GRUB!
+
+See doc/uImage.FIT/howto.txt for an introduction to FIT images.
+
 Executing the boot manager
 ~~
 
-- 
2.17.1



[PATCH v4 3/5] doc: Add sample uefi.its image description file

2019-12-24 Thread Cristian Ciocaltea
This patch adds an example FIT image description file demonstrating
the usage of bootm command to securely launch UEFI binaries.

Signed-off-by: Cristian Ciocaltea 
Reviewed-by: Heinrich Schuchardt 
---
 doc/uImage.FIT/uefi.its | 67 +
 1 file changed, 67 insertions(+)
 create mode 100644 doc/uImage.FIT/uefi.its

diff --git a/doc/uImage.FIT/uefi.its b/doc/uImage.FIT/uefi.its
new file mode 100644
index 00..378ca4ed8d
--- /dev/null
+++ b/doc/uImage.FIT/uefi.its
@@ -0,0 +1,67 @@
+/*
+ * Example FIT image description file demonstrating the usage of the
+ * bootm command to launch UEFI binaries.
+ *
+ * Two boot configurations are available to enable booting GRUB2 on QEMU,
+ * the former uses a FDT blob contained in the FIT image, while the later
+ * relies on the FDT provided by the board emulator.
+ */
+
+/dts-v1/;
+
+/ {
+   description = "GRUB2 EFI and QEMU FDT blob";
+   #address-cells = <1>;
+
+   images {
+   efi-grub {
+   description = "GRUB EFI Firmware";
+   data = /incbin/("bootarm.efi");
+   type = "kernel_noload";
+   arch = "arm";
+   os = "efi";
+   compression = "none";
+   load = <0x0>;
+   entry = <0x0>;
+   hash-1 {
+   algo = "sha256";
+   };
+   };
+
+   fdt-qemu {
+   description = "QEMU DTB";
+   data = /incbin/("qemu-arm.dtb");
+   type = "flat_dt";
+   arch = "arm";
+   compression = "none";
+   hash-1 {
+   algo = "sha256";
+   };
+   };
+   };
+
+   configurations {
+   default = "config-grub-fdt";
+
+   config-grub-fdt {
+   description = "GRUB EFI Boot w/ FDT";
+   kernel = "efi-grub";
+   fdt = "fdt-qemu";
+   signature-1 {
+   algo = "sha256,rsa2048";
+   key-name-hint = "dev";
+   sign-images = "kernel", "fdt";
+   };
+   };
+
+   config-grub-nofdt {
+   description = "GRUB EFI Boot w/o FDT";
+   kernel = "efi-grub";
+   signature-1 {
+   algo = "sha256,rsa2048";
+   key-name-hint = "dev";
+   sign-images = "kernel";
+   };
+   };
+   };
+};
-- 
2.17.1



[PATCH v4 2/5] bootm: Add a bootm command for type IH_OS_EFI

2019-12-24 Thread Cristian Ciocaltea
Add support for booting EFI binaries contained in FIT images.
A typical usage scenario is chain-loading GRUB2 in a verified
boot environment.

Signed-off-by: Cristian Ciocaltea 
Reviewed-by: Heinrich Schuchardt 
---
 cmd/Kconfig   |  7 ++
 common/bootm_os.c | 56 +++
 2 files changed, 63 insertions(+)

diff --git a/cmd/Kconfig b/cmd/Kconfig
index 1e4cf146c5..87f2335a3c 100644
--- a/cmd/Kconfig
+++ b/cmd/Kconfig
@@ -263,6 +263,13 @@ config CMD_BOOTI
help
  Boot an AArch64 Linux Kernel image from memory.
 
+config BOOTM_EFI
+   bool "Support booting EFI OS images"
+   depends on CMD_BOOTEFI
+   default y
+   help
+ Support booting EFI images via the bootm command.
+
 config BOOTM_LINUX
bool "Support booting Linux OS images"
depends on CMD_BOOTM || CMD_BOOTZ || CMD_BOOTI
diff --git a/common/bootm_os.c b/common/bootm_os.c
index d89ddc32b0..1d58462509 100644
--- a/common/bootm_os.c
+++ b/common/bootm_os.c
@@ -7,10 +7,12 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 
@@ -498,6 +500,57 @@ static int do_bootm_tee(int flag, int argc, char * const 
argv[],
 }
 #endif
 
+#ifdef CONFIG_BOOTM_EFI
+static int do_bootm_efi(int flag, int argc, char * const argv[],
+   bootm_headers_t *images)
+{
+   int ret;
+   efi_status_t efi_ret;
+   void *image_buf;
+
+   if (flag != BOOTM_STATE_OS_GO)
+   return 0;
+
+   /* Locate FDT, if provided */
+   ret = bootm_find_images(flag, argc, argv);
+   if (ret)
+   return ret;
+
+   /* Initialize EFI drivers */
+   efi_ret = efi_init_obj_list();
+   if (efi_ret != EFI_SUCCESS) {
+   printf("## Failed to initialize UEFI sub-system: r = %lu\n",
+  efi_ret & ~EFI_ERROR_MASK);
+   return 1;
+   }
+
+   /* Install device tree */
+   efi_ret = efi_install_fdt(images->ft_len
+ ? images->ft_addr : EFI_FDT_USE_INTERNAL);
+   if (efi_ret != EFI_SUCCESS) {
+   printf("## Failed to install device tree: r = %lu\n",
+  efi_ret & ~EFI_ERROR_MASK);
+   return 1;
+   }
+
+   /* Run EFI image */
+   printf("## Transferring control to EFI (at address %08lx) ...\n",
+  images->ep);
+   bootstage_mark(BOOTSTAGE_ID_RUN_OS);
+
+   image_buf = map_sysmem(images->ep, images->os.image_len);
+
+   efi_ret = efi_run_image(image_buf, images->os.image_len);
+   if (efi_ret != EFI_SUCCESS) {
+   printf("## Failed to run EFI image: r = %lu\n",
+  efi_ret & ~EFI_ERROR_MASK);
+   return 1;
+   }
+
+   return 0;
+}
+#endif
+
 static boot_os_fn *boot_os[] = {
[IH_OS_U_BOOT] = do_bootm_standalone,
 #ifdef CONFIG_BOOTM_LINUX
@@ -534,6 +587,9 @@ static boot_os_fn *boot_os[] = {
 #ifdef CONFIG_BOOTM_OPTEE
[IH_OS_TEE] = do_bootm_tee,
 #endif
+#ifdef CONFIG_BOOTM_EFI
+   [IH_OS_EFI] = do_bootm_efi,
+#endif
 };
 
 /* Allow for arch specific config before we boot */
-- 
2.17.1



[PATCH v4 1/5] image: Add IH_OS_EFI for EFI chain-load boot

2019-12-24 Thread Cristian Ciocaltea
Add a new OS type to be used for chain-loading an EFI compatible
firmware or boot loader like GRUB2, possibly in a verified boot
scenario.

Bellow is sample ITS file that generates a FIT image supporting
secure boot. Please note the presence of 'os = "efi";' line, which
identifies the currently introduced OS type:

/ {
#address-cells = <1>;

images {
efi-grub {
description = "GRUB EFI";
data = /incbin/("bootarm.efi");
type = "kernel_noload";
arch = "arm";
os = "efi";
compression = "none";
load = <0x0>;
entry = <0x0>;
hash-1 {
algo = "sha256";
};
};
};

configurations {
default = "config-grub";
config-grub {
kernel = "efi-grub";
signature-1 {
algo = "sha256,rsa2048";
sign-images = "kernel";
};
};
};
};

Signed-off-by: Cristian Ciocaltea 
Reviewed-by: Heinrich Schuchardt 
---
 common/image-fit.c | 3 ++-
 common/image.c | 1 +
 include/image.h| 1 +
 3 files changed, 4 insertions(+), 1 deletion(-)

diff --git a/common/image-fit.c b/common/image-fit.c
index c52f945120..231612ff5f 100644
--- a/common/image-fit.c
+++ b/common/image-fit.c
@@ -1926,7 +1926,8 @@ int fit_image_load(bootm_headers_t *images, ulong addr,
image_type == IH_TYPE_FPGA ||
fit_image_check_os(fit, noffset, IH_OS_LINUX) ||
fit_image_check_os(fit, noffset, IH_OS_U_BOOT) ||
-   fit_image_check_os(fit, noffset, IH_OS_OPENRTOS);
+   fit_image_check_os(fit, noffset, IH_OS_OPENRTOS) ||
+   fit_image_check_os(fit, noffset, IH_OS_EFI);
 
/*
 * If either of the checks fail, we should report an error, but
diff --git a/common/image.c b/common/image.c
index eb626dcac9..75d5dd944f 100644
--- a/common/image.c
+++ b/common/image.c
@@ -137,6 +137,7 @@ static const table_entry_t uimage_os[] = {
{   IH_OS_OPENRTOS, "openrtos", "OpenRTOS", },
 #endif
{   IH_OS_OPENSBI,  "opensbi",  "RISC-V OpenSBI",   },
+   {   IH_OS_EFI,  "efi",  "EFI Firmware" },
 
{   -1, "", "", },
 };
diff --git a/include/image.h b/include/image.h
index f4d2aaf53e..4a280b78e7 100644
--- a/include/image.h
+++ b/include/image.h
@@ -157,6 +157,7 @@ enum {
IH_OS_ARM_TRUSTED_FIRMWARE, /* ARM Trusted Firmware */
IH_OS_TEE,  /* Trusted Execution Environment */
IH_OS_OPENSBI,  /* RISC-V OpenSBI */
+   IH_OS_EFI,  /* EFI Firmware (e.g. GRUB2) */
 
IH_OS_COUNT,
 };
-- 
2.17.1



[PATCH v4 0/5] Add support for booting EFI FIT images

2019-12-24 Thread Cristian Ciocaltea
Currently the only way to run an EFI binary like GRUB2 is via the
'bootefi' command, which cannot be used in a verified boot scenario.

The obvious solution to this limitation is to add support for
booting FIT images containing those EFI binaries.

The implementation relies on a new image type - IH_OS_EFI - which
can be created by using 'os = "efi"' inside an ITS file:

/ {
#address-cells = <1>;

images {
efi-grub {
description = "GRUB EFI";
data = /incbin/("bootarm.efi");
type = "kernel_noload";
arch = "arm";
os = "efi";
compression = "none";
load = <0x0>;
entry = <0x0>;
hash-1 {
algo = "sha256";
};
};
};

configurations {
default = "config-grub";
config-grub {
kernel = "efi-grub";
signature-1 {
algo = "sha256,rsa2048";
sign-images = "kernel";
};
};
};
};

The bootm command has been extended to handle the IH_OS_EFI images.
To enable this feature, a new configuration option has been added:
BOOTM_EFI

I tested the solution using the 'qemu_arm' board:

=> load scsi 0:1 ${kernel_addr_r} efi-image.fit
=> bootm ${kernel_addr_r}#config-grub

Changes in v4:
* Extend the python test to also run on real hardware, currently
  tested on qemu_arm

Changes in v3:
* Rebase patches on Heinrich Schuchardt's patch series v3:
   efi_loader: prepare for FIT images
   https://lists.denx.de/pipermail/u-boot/2019-December/393677.html
   This fixes implicitly the sandbox issue 'phys_to_virt: Cannot map
   sandbox address' since efi_install_fdt() is now expecting a pointer
   to addressable memory instead of a physical address.
* Get rid of 'EFI/BOOT/' prefix used in ITS samples
* Add a python test to verify the implementation in sandbox environment

Changes in v2:
* Rebase patches on Heinrich Schuchardt's patch series:
   efi_loader: prepare for FIT images
   https://lists.denx.de/pipermail/u-boot/2019-December/393192.html
* Add sample configuration: doc/uImage.FIT/uefi.its
* Update uefi documentation: doc/uefi/uefi.rst

Cristian Ciocaltea (5):
  image: Add IH_OS_EFI for EFI chain-load boot
  bootm: Add a bootm command for type IH_OS_EFI
  doc: Add sample uefi.its image description file
  doc: uefi.rst: Document launching UEFI binaries from FIT images
  test/py: Create a test for launching UEFI binaries from FIT images

 cmd/Kconfig   |   7 +
 common/bootm_os.c |  56 +
 common/image-fit.c|   3 +-
 common/image.c|   1 +
 doc/uImage.FIT/uefi.its   |  67 +
 doc/uefi/uefi.rst |  34 +++
 include/image.h   |   1 +
 test/py/tests/test_efi_fit.py | 459 ++
 8 files changed, 627 insertions(+), 1 deletion(-)
 create mode 100644 doc/uImage.FIT/uefi.its
 create mode 100644 test/py/tests/test_efi_fit.py

-- 
2.17.1



Re: [PATCH v2 4/4] test: Add tests for the multiplexer framework

2019-12-24 Thread Simon Glass
Hi Jean-Jacques,

On Tue, 24 Dec 2019 at 08:58, Simon Glass  wrote:
>
> Hi Jean-Jacques,
>
> On Tue, 5 Nov 2019 at 04:50, Jean-Jacques Hiblot  wrote:
> >
> > Provide tests to check the behavior of the multiplexer framework.
> > The test uses a mmio-based multiplexer.
> >
> > Signed-off-by: Jean-Jacques Hiblot 
> >
> > ---
> >
> > Changes in v2:
> > - Call sandbox_set_enable_memio(true) before running the test

One more thing...I think this multiplexer thing need some
documentation and an example (e.g. using sandbox).

Also we should have a mux command to list and control muxes.

Regards,
Simon


Re: [PATCH v2 3/4] drivers: mux: mmio-based syscon mux controller

2019-12-24 Thread Simon Glass
Hi Jean-Jacques,

On Tue, 5 Nov 2019 at 04:50, Jean-Jacques Hiblot  wrote:
>
> This adds a driver for mmio-based syscon multiplexers controlled by
> bitfields in a syscon register range.
> This is heavily based on the linux mmio-mux driver.
>
> Signed-off-by: Jean-Jacques Hiblot 
> ---
>
> Changes in v2: None
>
>  drivers/mux/Kconfig  |  15 +
>  drivers/mux/Makefile |   1 +
>  drivers/mux/mmio.c   | 155 +++
>  3 files changed, 171 insertions(+)
>  create mode 100644 drivers/mux/mmio.c

Reviewed-by: Simon Glass 

Nits below.

>
> diff --git a/drivers/mux/Kconfig b/drivers/mux/Kconfig
> index ad0199c058..bda6a2d9f5 100644
> --- a/drivers/mux/Kconfig
> +++ b/drivers/mux/Kconfig
> @@ -4,4 +4,19 @@ config MULTIPLEXER
> bool "Multiplexer Support"
> depends on DM
>
> +
> +if MULTIPLEXER
> +
> +config MUX_MMIO
> +   bool "MMIO register bitfield-controlled Multiplexer"
> +   depends on MULTIPLEXER && SYSCON
> +   help
> + MMIO register bitfield-controlled Multiplexer controller.
> +
> + The driver builds multiplexer controllers for bitfields in a syscon
> + register. For N bit wide bitfields, there will be 2^N possible
> + multiplexer states.
> +
> +endif
> +
>  endmenu
> diff --git a/drivers/mux/Makefile b/drivers/mux/Makefile
> index 351e4363d3..78ebf04c7a 100644
> --- a/drivers/mux/Makefile
> +++ b/drivers/mux/Makefile
> @@ -4,3 +4,4 @@
>  # Jean-Jacques Hiblot 
>
>  obj-$(CONFIG_$(SPL_)MULTIPLEXER) += mux-uclass.o
> +obj-$(CONFIG_$(SPL_)MUX_MMIO) += mmio.o
> diff --git a/drivers/mux/mmio.c b/drivers/mux/mmio.c
> new file mode 100644
> index 00..a9faaeb9fd
> --- /dev/null
> +++ b/drivers/mux/mmio.c
> @@ -0,0 +1,155 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/*
> + * MMIO register bitfield-controlled multiplexer driver
> + * Based on the linux mmio multiplexer driver
> + *
> + * Copyright (C) 2017 Pengutronix, Philipp Zabel 
> + * Copyright (C) 2019 Texas Instrument, Jean-jacques Hiblot 
> + */
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +
> +static int mux_mmio_set(struct mux_control *mux, int state)
> +{
> +   struct regmap_field **fields = dev_get_priv(mux->dev);
> +
> +   return regmap_field_write(fields[mux_control_get_index(mux)], state);
> +}
> +
> +static const struct mux_control_ops mux_mmio_ops = {
> +   .set = mux_mmio_set,
> +};
> +
> +static const struct udevice_id mmio_mux_of_match[] = {
> +   { .compatible = "mmio-mux" },
> +   { /* sentinel */ },
> +};
> +
> +static int mmio_mux_probe(struct udevice *dev)
> +{
> +   struct regmap_field **fields;
> +   struct mux_chip *mux_chip = dev_get_uclass_priv(dev);
> +   struct regmap *regmap;
> +   u32 *mux_reg_masks;
> +   u32 *idle_states;
> +   int num_fields;
> +   int ret;
> +   int i;
> +
> +   regmap = syscon_node_to_regmap(dev_ofnode(dev->parent));
> +   if (IS_ERR(regmap)) {
> +   ret = PTR_ERR(regmap);
> +   dev_err(dev, "failed to get regmap: %d\n", ret);
> +   return ret;
> +   }
> +
> +   num_fields = dev_read_size(dev, "mux-reg-masks");
> +   if (num_fields < 0) {
> +   dev_err(dev, "mux-reg-masks property missing or invalid: 
> %d\n",
> +   num_fields);

These seem like errors that should not happen in a real system.

You could use

   return log_msg_ret("mux-reg-masks" ,-EINVAL)

which reduces to nothing unless CONFIG_LOG_ERROR_RETURN is set.

That gives a nice backtrace of all errors that happened with a little
string tag to explain each one.

> +   return num_fields;
> +   }
> +   num_fields /= sizeof(u32);
> +   if (num_fields == 0 || num_fields % 2)
> +   ret = -EINVAL;
> +   num_fields = num_fields / 2;
> +
> +   ret = mux_alloc_controllers(dev, num_fields);
> +   if (ret < 0) {
> +   dev_err(dev, "failed to allocate mux controllers: %d\n",
> +   ret);

Same here and below. Seems like a waste to include this string when we
are just out of memory.

> +   return ret;
> +   }
> +
> +   fields = devm_kmalloc(dev, num_fields * sizeof(*fields), __GFP_ZERO);
> +   if (!fields)
> +   return -ENOMEM;
> +   dev->priv = fields;
> +
> +   mux_reg_masks = devm_kmalloc(dev, num_fields * 2 * sizeof(u32),
> +__GFP_ZERO);
> +   if (!mux_reg_masks)
> +   return -ENOMEM;
> +
> +   ret = dev_read_u32_array(dev, "mux-reg-masks", mux_reg_masks,
> +num_fields * 2);
> +   if (ret < 0) {
> +   dev_err(dev, "failed to read mux-reg-masks property: %d\n",
> +   ret);
> +   return ret;
> +   }
> +
> +   idle_states = devm_kmalloc(dev, num_fields * sizeof(u32), __GFP_ZERO);
> +   if 

Re: [PATCH v2 4/4] test: Add tests for the multiplexer framework

2019-12-24 Thread Simon Glass
Hi Jean-Jacques,

On Tue, 5 Nov 2019 at 04:50, Jean-Jacques Hiblot  wrote:
>
> Provide tests to check the behavior of the multiplexer framework.
> The test uses a mmio-based multiplexer.
>
> Signed-off-by: Jean-Jacques Hiblot 
>
> ---
>
> Changes in v2:
> - Call sandbox_set_enable_memio(true) before running the test
>
>  arch/sandbox/dts/test.dts |  26 +++
>  configs/sandbox_defconfig |   2 +
>  test/dm/Makefile  |   1 +
>  test/dm/mux-mmio.c| 147 ++
>  4 files changed, 176 insertions(+)
>  create mode 100644 test/dm/mux-mmio.c

Reviewed-by: Simon Glass 

nits below

>
> diff --git a/arch/sandbox/dts/test.dts b/arch/sandbox/dts/test.dts
> index aa9eaec338..3224a8389c 100644
> --- a/arch/sandbox/dts/test.dts
> +++ b/arch/sandbox/dts/test.dts
> @@ -1,3 +1,5 @@
> +#include 
> +
>  /dts-v1/;
>
>  / {
> @@ -93,6 +95,11 @@
> <_b 9 0xc 3 2 1>;
> int-value = <1234>;
> uint-value = <(-1234)>;
> +
> +   mux-controls = < 0>, < 1>,
> +  < 2>, < 3>;
> +   mux-control-names = "mux0", "mux1", "mux2", "mux3";
> +   mux-syscon = <>;
> };
>
> junk {
> @@ -129,6 +136,9 @@
> compatible = "denx,u-boot-fdt-test";
> ping-expect = <3>;
> ping-add = <3>;
> +
> +   mux-controls = < 0>;
> +   mux-control-names = "mux0";
> };
>
> phy_provider0: gen_phy@0 {
> @@ -665,6 +675,22 @@
> 0x58 8>;
> };
>
> +   syscon3: syscon@3 {
> +   compatible = "simple-mfd", "syscon";
> +   reg = <0x000100 0x10>;
> +
> +   muxcontroller0: a-mux-controller {
> +   compatible = "mmio-mux";
> +   #mux-control-cells = <1>;
> +
> +   mux-reg-masks = <0x0 0x30>, /* 0: reg 0x0, bits 5:4 */
> +   <0x3 0x1E>, /* 1: reg 0x3, bits 4:1 */
> +   <0x1 0xFF>; /* 2: reg 0x1, bits 7:0 */
> +   idle-states = , <0x02>, <0x73>;
> +   u-boot,mux-autoprobe;
> +   };
> +   };
> +
> timer {
> compatible = "sandbox,timer";
> clock-frequency = <100>;
> diff --git a/configs/sandbox_defconfig b/configs/sandbox_defconfig
> index 20ebc68997..2822dd9c74 100644
> --- a/configs/sandbox_defconfig
> +++ b/configs/sandbox_defconfig
> @@ -144,6 +144,8 @@ CONFIG_SPI_FLASH_SPANSION=y
>  CONFIG_SPI_FLASH_STMICRO=y
>  CONFIG_SPI_FLASH_SST=y
>  CONFIG_SPI_FLASH_WINBOND=y
> +CONFIG_MULTIPLEXER=y
> +CONFIG_MUX_MMIO=y
>  CONFIG_DM_ETH=y
>  CONFIG_NVME=y
>  CONFIG_PCI=y
> diff --git a/test/dm/Makefile b/test/dm/Makefile
> index 0c2fd5cb5e..a3fc23e527 100644
> --- a/test/dm/Makefile
> +++ b/test/dm/Makefile
> @@ -47,6 +47,7 @@ obj-$(CONFIG_DM_SPI_FLASH) += sf.o
>  obj-$(CONFIG_SMEM) += smem.o
>  obj-$(CONFIG_DM_SPI) += spi.o
>  obj-y += syscon.o
> +obj-$(CONFIG_MUX_MMIO) += mux-mmio.o
>  obj-$(CONFIG_DM_USB) += usb.o
>  obj-$(CONFIG_DM_PMIC) += pmic.o
>  obj-$(CONFIG_DM_REGULATOR) += regulator.o
> diff --git a/test/dm/mux-mmio.c b/test/dm/mux-mmio.c
> new file mode 100644
> index 00..a3dfd34120
> --- /dev/null
> +++ b/test/dm/mux-mmio.c
> @@ -0,0 +1,147 @@
> +// SPDX-License-Identifier: GPL-2.0+
> +/*
> + * Copyright (C) 2017-2018 Texas Instruments Incorporated - 
> http://www.ti.com/
> + * Jean-Jacques Hiblot 
> + */
> +
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 

Missing header file for device_remove() here.

> +
> +/* Test that mmio mux work correctly */
> +static int dm_test_mux_mmio(struct unit_test_state *uts)
> +{
> +   struct udevice *dev, *dev_b;
> +   struct regmap *map;
> +   struct mux_control *ctl0_a, *ctl0_b;
> +   struct mux_control *ctl1;
> +   struct mux_control *ctl_err;
> +   u32 val;
> +   int i;
> +
> +   sandbox_set_enable_memio(true);
> +
> +   ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 0, ));
> +   ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 1, _b));
> +   ut_asserteq_str("a-test", dev->name);
> +   ut_asserteq_str("b-test", dev_b->name);
> +   map = syscon_regmap_lookup_by_phandle(dev, "mux-syscon");
> +   ut_assert(!IS_ERR(map));
> +   ut_assert(map);
> +
> +   /* check default states */
> +   ut_assertok(regmap_read(map, 3, ));
> +   ut_asserteq(0x02, (val & 0x1E) >> 1);
> +   ut_assertok(regmap_read(map, 1, ));
> +   ut_asserteq(0x73, (val & 0xFF) >> 0);
> +
> +   ut_assertok(mux_control_get(dev, "mux0", _a));
> +   ut_assertok(mux_control_get(dev, "mux1", ));
> +   ut_asserteq(-ERANGE, mux_control_get(dev, "mux3", _err));
> +   ut_asserteq(-ENODATA, mux_control_get(dev, "dummy", _err));
> +  

Re: [PATCH v2 1/4] drivers: Add a new framework for multiplexer devices

2019-12-24 Thread Simon Glass
Hi Jean-Jacques,

On Tue, 5 Nov 2019 at 04:50, Jean-Jacques Hiblot  wrote:
>
> Add a new subsystem that handles multiplexer controllers. The API is the
> same as in Linux.
>
> Signed-off-by: Jean-Jacques Hiblot 
>
> ---
>
> Changes in v2:
> - Fixed warning in mux_of_xlate_default()
> - Improved documentation
> - Fixed SPL build
>
>  drivers/Kconfig   |   2 +
>  drivers/Makefile  |   1 +
>  drivers/mux/Kconfig   |   7 +
>  drivers/mux/Makefile  |   6 +
>  drivers/mux/mux-uclass.c  | 270 ++
>  include/dm/uclass-id.h|   1 +
>  include/dt-bindings/mux/mux.h |  17 +++
>  include/mux-internal.h| 103 +
>  include/mux.h | 113 ++
>  9 files changed, 520 insertions(+)
>  create mode 100644 drivers/mux/Kconfig
>  create mode 100644 drivers/mux/Makefile
>  create mode 100644 drivers/mux/mux-uclass.c
>  create mode 100644 include/dt-bindings/mux/mux.h
>  create mode 100644 include/mux-internal.h
>  create mode 100644 include/mux.h
>
> diff --git a/drivers/Kconfig b/drivers/Kconfig
> index 9d99ce0226..450aa76e82 100644
> --- a/drivers/Kconfig
> +++ b/drivers/Kconfig
> @@ -60,6 +60,8 @@ source "drivers/mmc/Kconfig"
>
>  source "drivers/mtd/Kconfig"
>
> +source "drivers/mux/Kconfig"
> +
>  source "drivers/net/Kconfig"
>
>  source "drivers/nvme/Kconfig"
> diff --git a/drivers/Makefile b/drivers/Makefile
> index 0befeddfcb..9d64742580 100644
> --- a/drivers/Makefile
> +++ b/drivers/Makefile
> @@ -12,6 +12,7 @@ obj-$(CONFIG_$(SPL_TPL_)INPUT) += input/
>  obj-$(CONFIG_$(SPL_TPL_)LED) += led/
>  obj-$(CONFIG_$(SPL_TPL_)MMC_SUPPORT) += mmc/
>  obj-$(CONFIG_$(SPL_TPL_)NAND_SUPPORT) += mtd/nand/raw/
> +obj-$(CONFIG_$(SPL_)MULTIPLEXER) += mux/
>  obj-$(CONFIG_$(SPL_TPL_)PCH_SUPPORT) += pch/
>  obj-$(CONFIG_$(SPL_TPL_)PCI) += pci/
>  obj-$(CONFIG_$(SPL_TPL_)PHY) += phy/
> diff --git a/drivers/mux/Kconfig b/drivers/mux/Kconfig
> new file mode 100644
> index 00..ad0199c058
> --- /dev/null
> +++ b/drivers/mux/Kconfig
> @@ -0,0 +1,7 @@
> +menu "Multiplexer drivers"
> +
> +config MULTIPLEXER
> +   bool "Multiplexer Support"
> +   depends on DM

Please add help here.

> +
> +endmenu
> diff --git a/drivers/mux/Makefile b/drivers/mux/Makefile
> new file mode 100644
> index 00..351e4363d3
> --- /dev/null
> +++ b/drivers/mux/Makefile
> @@ -0,0 +1,6 @@
> +# SPDX-License-Identifier: GPL-2.0+
> +#
> +# (C) Copyright 2019
> +# Jean-Jacques Hiblot 
> +
> +obj-$(CONFIG_$(SPL_)MULTIPLEXER) += mux-uclass.o
> diff --git a/drivers/mux/mux-uclass.c b/drivers/mux/mux-uclass.c
> new file mode 100644
> index 00..6aaf4dc964
> --- /dev/null
> +++ b/drivers/mux/mux-uclass.c
> @@ -0,0 +1,270 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/*
> + * Multiplexer subsystem
> + *
> + * Based on the linux multiplexer framework
> + *
> + * Copyright (C) 2017 Axentia Technologies AB
> + * Author: Peter Rosin 
> + *
> + * Copyright (C) 2017-2018 Texas Instruments Incorporated - 
> http://www.ti.com/
> + * Jean-Jacques Hiblot 
> + */
> +
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +
> +/*
> + * The idle-as-is "state" is not an actual state that may be selected, it
> + * only implies that the state should not be changed. So, use that state
> + * as indication that the cached state of the multiplexer is unknown.
> + */
> +#define MUX_CACHE_UNKNOWN MUX_IDLE_AS_IS
> +
> +static inline const struct mux_control_ops *mux_dev_ops(struct udevice *dev)
> +{
> +   return (const struct mux_control_ops *)dev->driver->ops;
> +}
> +
> +static int mux_control_set(struct mux_control *mux, int state)

Please add comments to these static functions.

> +{
> +   int ret = mux_dev_ops(mux->dev)->set(mux, state);
> +
> +   mux->cached_state = ret < 0 ? MUX_CACHE_UNKNOWN : state;
> +
> +   return ret;
> +}
> +
> +unsigned int mux_control_states(struct mux_control *mux)
> +{
> +   return mux->states;
> +}
> +
> +static int __mux_control_select(struct mux_control *mux, int state)
> +{
> +   int ret;
> +
> +   if (WARN_ON(state < 0 || state >= mux->states))
> +   return -EINVAL;
> +
> +   if (mux->cached_state == state)
> +   return 0;
> +
> +   ret = mux_control_set(mux, state);
> +   if (ret >= 0)
> +   return 0;
> +
> +   /* The mux update failed, try to revert if appropriate... */
> +   if (mux->idle_state != MUX_IDLE_AS_IS)
> +   mux_control_set(mux, mux->idle_state);
> +
> +   return ret;
> +}
> +
> +int mux_control_select(struct mux_control *mux, unsigned int state)
> +{
> +   int ret;
> +
> +   if (mux->in_use)
> +   return -EBUSY;
> +
> +   ret = __mux_control_select(mux, state);
> +
> +   if (ret < 0)
> +   return ret;
> +
> +   mux->in_use = true;
> +
> +   return 0;
> +}
> +
> +int mux_control_deselect(struct mux_control *mux)
> +{
> +   int ret = 0;
> 

Re: [PATCH v2 2/4] dm: board: complete the initialization of the muxes in initr_dm()

2019-12-24 Thread Simon Glass
Hi Jean-Jacques,

On Tue, 5 Nov 2019 at 04:50, Jean-Jacques Hiblot  wrote:
>
> This will probe the multiplexer devices that have a "u-boot,mux-autoprobe"
> property. As a consequence they will be put in their idle state.
>
> Signed-off-by: Jean-Jacques Hiblot 
>
> ---
>
> Changes in v2:
> - insert the mux initialization in init_sequence_r[], just before the
> console is initialized as its serial port may be muxed
> - moved the definition of dm_mux_init() in this commit
>
>  common/board_r.c | 16 
>  drivers/mux/mux-uclass.c | 22 ++
>  include/mux.h|  2 ++
>  3 files changed, 40 insertions(+)
>
> diff --git a/common/board_r.c b/common/board_r.c
> index c1ecb06b74..3d410f3504 100644
> --- a/common/board_r.c
> +++ b/common/board_r.c
> @@ -32,6 +32,7 @@
>  #include 
>  #endif
>  #include 
> +#include 
>  #include 
>  #include 
>  #include 
> @@ -178,6 +179,18 @@ static int initr_serial(void)
> return 0;
>  }
>
> +#if CONFIG_MULTIPLEXER
> +static int initr_mux(void)
> +{
> +   /*
> +* Initialize the multiplexer controls to their default state.
> +* This must be done early as other drivers may unknowingly rely on 
> it.
> +*/
> +   dm_mux_init();

Needs to get an error code and at least log_debug() it here, even if
it continues.

> +   return 0;
> +}

Can you rebase on x86/next? It has a initr_dm_devices() function which
you can add this to.

> +#endif
> +
>  #if defined(CONFIG_PPC) || defined(CONFIG_M68K) || defined(CONFIG_MIPS)
>  static int initr_trap(void)
>  {
> @@ -691,6 +704,9 @@ static init_fnc_t init_sequence_r[] = {
>  #endif
>  #ifdef CONFIG_EFI_LOADER
> efi_memory_init,
> +#endif
> +#if CONFIG_MULTIPLEXER
> +   initr_mux,
>  #endif
> stdio_init_tables,
> initr_serial,
> diff --git a/drivers/mux/mux-uclass.c b/drivers/mux/mux-uclass.c
> index 6aaf4dc964..71392e9e50 100644
> --- a/drivers/mux/mux-uclass.c
> +++ b/drivers/mux/mux-uclass.c
> @@ -262,6 +262,28 @@ int mux_uclass_post_probe(struct udevice *dev)
> return 0;
>  }
>
> +void dm_mux_init(void)
> +{
> +   struct uclass *uc;
> +   struct udevice *dev;
> +   int ret;
> +
> +   ret = uclass_get(UCLASS_MUX, );
> +   if (ret < 0) {
> +   debug("unable to get MUX uclass\n");
> +   return;

return ret

This should be a fatal error.

> +   }
> +   uclass_foreach_dev(dev, uc) {
> +   if (dev_read_bool(dev, "u-boot,mux-autoprobe")) {
> +   ret = device_probe(dev);
> +   if (ret)
> +   debug("unable to probe device %s\n", 
> dev->name);

Doesn't this need to be reported to the caller?

> +   } else {
> +   printf("not found for dev %s\n", dev->name);

What does this mean? If autoprobe is off we can't find the device? I
suggest changing the message, and debug() as Vignesh suggests.

> +   }
> +   }
> +}
> +
>  UCLASS_DRIVER(mux) = {
> .id = UCLASS_MUX,
> .name   = "mux",
> diff --git a/include/mux.h b/include/mux.h
> index 060f71a47c..2467723951 100644
> --- a/include/mux.h
> +++ b/include/mux.h
> @@ -75,6 +75,8 @@ void mux_control_put(struct mux_control *mux);
>
>  struct mux_control *devm_mux_control_get(struct udevice *dev,
>  const char *mux_name);
> +void dm_mux_init(void);

Function comments again.

> +
>  #else
>  unsigned int mux_control_states(struct mux_control *mux)
>  {
> --
> 2.17.1
>

Regards,
Simon


Re: `uclass_find_device_by_name` fails to find the device

2019-12-24 Thread Simon Glass
HI Nandor,

On Wed, 6 Nov 2019 at 06:09, Nandor Han  wrote:
>
> Hi everybody,
>I've noticed that commit 4213609cc7fb78f84b2ea63f4a5691b60d01c248
> changes how `uclass.c:uclass_find_device_by_name` function compares the
> names for finding devices.
>
> Because of this I have an issue when having a DTS and trying to find a
> device by name.
>
> The `lists.c:lists_bind_fdt` function will bind a device to a device
> node tree and use the node name as device name.
>
> ```
> lists.c:
> int lists_bind_fdt(..)
> ...
> name = ofnode_get_name(node);
> ...
>
> ```
> Because of this devices will end up having names like: i2c@021a,
> pfuze100@8...
>
> When using `uclass.c:uclass_find_device_by_name` to get a device, before
> the change, this was not an issue since `strncmp` was used with the
> length of the name making a call like
> `uclass_find_device_by_name(pfuze100)` successfully even if the device
> name was `pfuze100@8`.
> ```
> if (!strncmp(dev->name, name, strlen(name))) {
> ```
>
> However after the change (which I think is correct), the check fails,
> resulting in not finding the expected device, since the device name has
> also the suffix `@...` included.
>
> What's the proper fix here:
>
> 1. Changing the `lists.c:lists_bind_fdt` to drop the `@` suffix?
>  Note: Could this be a problem when having the same node names but
> different addressees in DTS {e.g.foo@1, foo@2}
>
> 2. When using `uclass.c:uclass_find_device_by_name` the user should take
> in consideration that `@..` suffix needs to be added.
> Note: This could be a problem when same code is used with different
> DTSs.
>
> 3. Changing the comparation in `uclass.c:uclass_find_device_by_name` to
> ignore the part after `@` for device names.
>
> 4. What else?

Can you use a phandle to refer to the device from another node,
perhaps a UCLASS_BOARD node if needed? It is not a good idea to use
this function.

Alternatively we could add a new function which finds by partial name.

Regards,
Simon


Re: [PATCH v2 2/4] regmap: Allow providing read/write callbacks through struct regmap_config

2019-12-24 Thread Simon Glass
Hi Jean-Jacques,

On Mon, 16 Dec 2019 at 03:10, Jean-Jacques Hiblot  wrote:
>
> Hi Simon,
>
> On 10/12/2019 16:18, Simon Glass wrote:
> > Hi Jean-Jacques,
> >
> > On Tue, 5 Nov 2019 at 04:47, Jean-Jacques Hiblot  wrote:
> >> Some linux drivers provide their own read/write functions to access data
> >> from/of the regmap. Adding support for it.
> >>
> >> Signed-off-by: Jean-Jacques Hiblot 
> >>
> >> ---
> >>
> >> Changes in v2:
> >> - Only use custom accessors if {,SPL,TPL}_REGMAP_ACCESSORS is enabled
> >>
> >>   drivers/core/Kconfig  | 25 +
> >>   drivers/core/regmap.c | 22 --
> >>   include/regmap.h  | 28 +---
> >>   3 files changed, 70 insertions(+), 5 deletions(-)
> > Coming back to the discussion on driver model
> >
> > How do you specify the fields? I would expect that this would be done
> > in the driver tree? Perhaps in a subnode of the device?
> >
> > Just to state what I see as the advantages of using a separate device
> > for access:
> >
> > - Remove the #ifdef in the regmap struct
> > - Easy to specify the behaviour in a device-tree node
> > - Easy to extend as the child device can do what it likes with respect to 
> > access
>
> That sure is a better abstraction. However the goal of this patch is
> only to use the same API as linux. It allows porting the drivers as-is
> and thus reduce the burden of maintenance.

So how do you specify the fields? See my question above.

It is not possible to use a similar API without importing the internal
implementation. Linux's driver model is less homogenous.

Regards,
Simon


[PATCH 3/3] efi_rng_protocol: Install the efi_rng_protocol on the root node

2019-12-24 Thread Sughosh Ganu
Install the EFI_RNG_PROTOCOL implementation for it's subsequent use by
the kernel for features like kaslr.

Signed-off-by: Sughosh Ganu 
---
 include/efi_loader.h   | 4 
 lib/efi_loader/efi_rng.c   | 2 ++
 lib/efi_loader/efi_root_node.c | 4 
 3 files changed, 10 insertions(+)

diff --git a/include/efi_loader.h b/include/efi_loader.h
index bec7873..71996ec 100644
--- a/include/efi_loader.h
+++ b/include/efi_loader.h
@@ -130,6 +130,7 @@ extern const struct efi_hii_config_routing_protocol 
efi_hii_config_routing;
 extern const struct efi_hii_config_access_protocol efi_hii_config_access;
 extern const struct efi_hii_database_protocol efi_hii_database;
 extern const struct efi_hii_string_protocol efi_hii_string;
+extern const struct efi_rng_protocol efi_rng_protocol_ops;
 
 uint16_t *efi_dp_str(struct efi_device_path *dp);
 
@@ -175,6 +176,9 @@ extern const efi_guid_t efi_guid_hii_config_access_protocol;
 extern const efi_guid_t efi_guid_hii_database_protocol;
 extern const efi_guid_t efi_guid_hii_string_protocol;
 
+/* GUID of RNG protocol */
+extern const efi_guid_t efi_guid_rng_protocol;
+
 extern unsigned int __efi_runtime_start, __efi_runtime_stop;
 extern unsigned int __efi_runtime_rel_start, __efi_runtime_rel_stop;
 
diff --git a/lib/efi_loader/efi_rng.c b/lib/efi_loader/efi_rng.c
index 8456592..80c8fc9 100644
--- a/lib/efi_loader/efi_rng.c
+++ b/lib/efi_loader/efi_rng.c
@@ -11,6 +11,8 @@
 
 DECLARE_GLOBAL_DATA_PTR;
 
+const efi_guid_t efi_guid_rng_protocol = EFI_RNG_PROTOCOL_GUID;
+
 static efi_status_t EFIAPI rng_getinfo(struct efi_rng_protocol *this,
efi_uintn_t *rng_algo_size,
efi_rng_algorithm *rng_algo)
diff --git a/lib/efi_loader/efi_root_node.c b/lib/efi_loader/efi_root_node.c
index f68b0fd..3d937ce 100644
--- a/lib/efi_loader/efi_root_node.c
+++ b/lib/efi_loader/efi_root_node.c
@@ -81,6 +81,10 @@ efi_status_t efi_root_node_register(void)
 _guid_hii_config_routing_protocol,
 (void *)_hii_config_routing,
 #endif
+#if CONFIG_IS_ENABLED(EFI_RNG_PROTOCOL)
+_guid_rng_protocol,
+(void *)_rng_protocol_ops,
+#endif
 NULL));
efi_root->type = EFI_OBJECT_TYPE_U_BOOT_FIRMWARE;
return ret;
-- 
2.7.4



[PATCH 2/3] efi: qemu: arm64: Add efi_rng_protocol implementation for the platform

2019-12-24 Thread Sughosh Ganu
Add support for the EFI_RNG_PROTOCOL routines for the qemu arm64
platform. EFI_RNG_PROTOCOL is an uefi boottime service which is
invoked by the efi stub in the kernel for getting random seed for
kaslr.

The routines are platform specific, and use the virtio-rng device on
the platform to get random data.

The feature can be enabled through the following config
CONFIG_EFI_RNG_PROTOCOL

Signed-off-by: Sughosh Ganu 
---
 board/emulation/qemu-arm/qemu-arm.c | 50 +
 include/efi_rng.h   | 34 +
 lib/efi_loader/Kconfig  |  8 
 lib/efi_loader/Makefile |  1 +
 lib/efi_loader/efi_rng.c| 74 +
 5 files changed, 167 insertions(+)
 create mode 100644 include/efi_rng.h
 create mode 100644 lib/efi_loader/efi_rng.c

diff --git a/board/emulation/qemu-arm/qemu-arm.c 
b/board/emulation/qemu-arm/qemu-arm.c
index e1f4709..3176421 100644
--- a/board/emulation/qemu-arm/qemu-arm.c
+++ b/board/emulation/qemu-arm/qemu-arm.c
@@ -91,3 +91,53 @@ void *board_fdt_blob_setup(void)
/* QEMU loads a generated DTB for us at the start of RAM. */
return (void *)CONFIG_SYS_SDRAM_BASE;
 }
+
+#if defined(CONFIG_EFI_RNG_PROTOCOL)
+#include 
+#include 
+
+#include 
+
+#define VIRTIO_RNG_PCI_DEVICE  "virtio-pci.l#0"
+
+void platform_rng_getinfo(efi_rng_algorithm *rng_algo)
+{
+   const efi_guid_t rng_raw_guid = EFI_RNG_ALGORITHM_RAW;
+
+   guidcpy(rng_algo, _raw_guid);
+}
+
+efi_status_t platform_get_rng_device(struct udevice **dev)
+{
+   int ret;
+   efi_status_t status = EFI_DEVICE_ERROR;
+   struct udevice *bus, *devp;
+
+   for (uclass_first_device(UCLASS_VIRTIO, ); bus;
+uclass_next_device()) {
+   for (device_find_first_child(bus, ); devp; 
device_find_next_child()) {
+   if (device_get_uclass_id(devp) == UCLASS_RNG) {
+   *dev = devp;
+   status = EFI_SUCCESS;
+   break;
+   }
+   }
+   }
+
+   if (status != EFI_SUCCESS) {
+   debug("No rng device found\n");
+   return EFI_DEVICE_ERROR;
+   }
+
+   if (*dev) {
+   ret = device_probe(*dev);
+   if (ret)
+   return EFI_DEVICE_ERROR;
+   } else {
+   debug("Couldn't get child device\n");
+   return EFI_DEVICE_ERROR;
+   }
+
+   return EFI_SUCCESS;
+}
+#endif /* CONFIG_EFI_RNG_PROTOCOL */
diff --git a/include/efi_rng.h b/include/efi_rng.h
new file mode 100644
index 000..df749dd
--- /dev/null
+++ b/include/efi_rng.h
@@ -0,0 +1,34 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (c) 2019, Linaro Limited
+ */
+
+#if !defined _EFI_RNG_H_
+#define _EFI_RNG_H_
+
+#include 
+#include 
+
+#define EFI_RNG_PROTOCOL_GUID \
+   EFI_GUID(0x3152bca5, 0xeade, 0x433d, 0x86, 0x2e, \
+0xc0, 0x1c, 0xdc, 0x29, 0x1f, 0x44)
+
+#define EFI_RNG_ALGORITHM_RAW \
+   EFI_GUID(0xe43176d7, 0xb6e8, 0x4827, 0xb7, 0x84, \
+0x7f, 0xfd, 0xc4, 0xb6, 0x85, 0x61)
+
+typedef efi_guid_t efi_rng_algorithm;
+
+struct efi_rng_protocol {
+   efi_status_t (EFIAPI *getinfo)(struct efi_rng_protocol *this,
+  efi_uintn_t *rng_algo_size,
+  efi_rng_algorithm *rng_algo);
+   efi_status_t (EFIAPI *getrng)(struct efi_rng_protocol *this,
+ efi_rng_algorithm *rng_algo,
+ efi_uintn_t rng_len, uint8_t *rng_data);
+};
+
+void platform_rng_getinfo(efi_rng_algorithm *rng_algo);
+efi_status_t platform_get_rng_device(struct udevice **dev);
+
+#endif /* _EFI_RNG_H_ */
diff --git a/lib/efi_loader/Kconfig b/lib/efi_loader/Kconfig
index 21ef440..7437442 100644
--- a/lib/efi_loader/Kconfig
+++ b/lib/efi_loader/Kconfig
@@ -120,4 +120,12 @@ config EFI_GRUB_ARM32_WORKAROUND
  GRUB prior to version 2.04 requires U-Boot to disable caches. This
  workaround currently is also needed on systems with caches that
  cannot be managed via CP15.
+
+config EFI_RNG_PROTOCOL
+   bool "EFI_RNG_PROTOCOL support"
+   depends on DM_RNG && TARGET_QEMU_ARM_64BIT
+   help
+ "Support for EFI_RNG_PROTOCOL implementation. Uses the rng
+  device on the platform"
+
 endif
diff --git a/lib/efi_loader/Makefile b/lib/efi_loader/Makefile
index 7db4060..04dc864 100644
--- a/lib/efi_loader/Makefile
+++ b/lib/efi_loader/Makefile
@@ -42,3 +42,4 @@ obj-$(CONFIG_PARTITIONS) += efi_disk.o
 obj-$(CONFIG_NET) += efi_net.o
 obj-$(CONFIG_GENERATE_ACPI_TABLE) += efi_acpi.o
 obj-$(CONFIG_GENERATE_SMBIOS_TABLE) += efi_smbios.o
+obj-$(CONFIG_EFI_RNG_PROTOCOL) += efi_rng.o
diff --git a/lib/efi_loader/efi_rng.c b/lib/efi_loader/efi_rng.c
new file mode 100644
index 000..8456592
--- /dev/null
+++ 

[PATCH 1/3] efi_loader: Add guidcpy function

2019-12-24 Thread Sughosh Ganu
Add guidcpy function to copy the source guid to the destination
guid.

Signed-off-by: Sughosh Ganu 
---
 include/efi_loader.h | 5 +
 1 file changed, 5 insertions(+)

diff --git a/include/efi_loader.h b/include/efi_loader.h
index 16a1b25..bec7873 100644
--- a/include/efi_loader.h
+++ b/include/efi_loader.h
@@ -17,6 +17,11 @@ static inline int guidcmp(const void *g1, const void *g2)
return memcmp(g1, g2, sizeof(efi_guid_t));
 }
 
+static inline void *guidcpy(efi_guid_t *dst, const efi_guid_t *src)
+{
+   return memcpy(dst, src, sizeof(*dst));
+}
+
 /* No need for efi loader support in SPL */
 #if CONFIG_IS_ENABLED(EFI_LOADER)
 
-- 
2.7.4



[PATCH 0/3] qemu: arm64: Add support for efi_rng_protocol

2019-12-24 Thread Sughosh Ganu
The patch series adds support for the EFI_RNG_PROTOCOL routines for
qemu arm64 platform. The getrng routine, used to get the random bytes,
uses the virtio-rng device found on the platform. The protocol, once
installed, can be used by the efi stub in the kernel for getting
random bytes needed for the kaslr feature.

These patches apply on top of the patch series to add random number
generator driver uclass[1]

[1] - https://lists.denx.de/pipermail/u-boot/2019-December/394010.html

Sughosh Ganu (3):
  efi_loader: Add guidcpy function
  efi: qemu: arm64: Add efi_rng_protocol implementation for the platform
  efi_rng_protocol: Install the efi_rng_protocol on the root node

 board/emulation/qemu-arm/qemu-arm.c | 50 
 include/efi_loader.h|  9 +
 include/efi_rng.h   | 34 +
 lib/efi_loader/Kconfig  |  8 
 lib/efi_loader/Makefile |  1 +
 lib/efi_loader/efi_rng.c| 76 +
 lib/efi_loader/efi_root_node.c  |  4 ++
 7 files changed, 182 insertions(+)
 create mode 100644 include/efi_rng.h
 create mode 100644 lib/efi_loader/efi_rng.c

-- 
2.7.4



[PATCH 1/1] test/py: Fix broken 'notbuildconfigspec' marker

2019-12-24 Thread Cristian Ciocaltea
Consider the following test sample:

@pytest.mark.buildconfigspec('fit')
@pytest.mark.notbuildconfigspec('generate_acpi_table')
def test_sample(u_boot_console):

Whatever the argument of the 'notbuildconfigspec' is,
the test ends up being skipped with the message:

('/uboot/test/py/conftest.py', 463,
 'Skipped: .config feature "fit" enabled')

Signed-off-by: Cristian Ciocaltea 
---
 test/py/conftest.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/test/py/conftest.py b/test/py/conftest.py
index bffee6b8a3..c7f361f7bf 100644
--- a/test/py/conftest.py
+++ b/test/py/conftest.py
@@ -457,7 +457,7 @@ def setup_buildconfigspec(item):
 option = options.args[0]
 if not ubconfig.buildconfig.get('config_' + option.lower(), None):
 pytest.skip('.config feature "%s" not enabled' % option.lower())
-for option in item.iter_markers('notbuildconfigspec'):
+for options in item.iter_markers('notbuildconfigspec'):
 option = options.args[0]
 if ubconfig.buildconfig.get('config_' + option.lower(), None):
 pytest.skip('.config feature "%s" enabled' % option.lower())
-- 
2.17.1



[U-Boot] [PATCHv2 3/3] board: amlogic: select PWRSEQ for all amlogic platform

2019-12-24 Thread Anand Moon
commit a10388dc6982 ("mmc: meson-gx: add support for mmc-pwrseq-emmc")
introduce CONFIG_PWESEQ for power sequence for eMMC module on
amlogic platform, so enable this to all amlogic boards.

Signed-off-by: Anand Moon 
Reviewed-by: Neil Armstrong 
---
no changes
---
 arch/arm/mach-meson/Kconfig | 1 +
 1 file changed, 1 insertion(+)

diff --git a/arch/arm/mach-meson/Kconfig b/arch/arm/mach-meson/Kconfig
index e29e4c0acc..513a33dae2 100644
--- a/arch/arm/mach-meson/Kconfig
+++ b/arch/arm/mach-meson/Kconfig
@@ -8,6 +8,7 @@ config MESON64_COMMON
select DM_SERIAL
select SYSCON
select REGMAP
+   select PWRSEQ
select BOARD_LATE_INIT
imply CMD_DM
 
-- 
2.24.1



[U-Boot] [PATCHv2 1/3] mmc: meson-gx: Fix clk phase tuning for MMC

2019-12-24 Thread Anand Moon
As per mainline line kernel fix the clk tunnig phase for
mmc, set Core=180, Tx=0, Rx=0 clk phase for mmc initialization.

Signed-off-by: Anand Moon 
---
Changes from previous
use the mainline kernel tuning for clk tuning.
Fixed the commmit messages.
Patch v1:
https://patchwork.ozlabs.org/patch/1201208/

Before these changes.
clock is enabled (380953Hz)
clock is enabled (2500Hz)
After these changes
clock is enabled (380953Hz)
clock is enabled (2500Hz)
clock is enabled (5200Hz)
Test on Odroid N2 and Odroid C2 with eMMC and microSD cards
---
 arch/arm/include/asm/arch-meson/sd_emmc.h | 14 ++
 drivers/mmc/meson_gx_mmc.c|  9 +
 2 files changed, 11 insertions(+), 12 deletions(-)

diff --git a/arch/arm/include/asm/arch-meson/sd_emmc.h 
b/arch/arm/include/asm/arch-meson/sd_emmc.h
index e3a72c8b66..d70fe4f03e 100644
--- a/arch/arm/include/asm/arch-meson/sd_emmc.h
+++ b/arch/arm/include/asm/arch-meson/sd_emmc.h
@@ -7,6 +7,7 @@
 #define __SD_EMMC_H__
 
 #include 
+#include 
 
 #define SDIO_PORT_A0
 #define SDIO_PORT_B1
@@ -19,14 +20,11 @@
 #define   CLK_MAX_DIV  63
 #define   CLK_SRC_24M  (0 << 6)
 #define   CLK_SRC_DIV2 (1 << 6)
-#define   CLK_CO_PHASE_000 (0 << 8)
-#define   CLK_CO_PHASE_090 (1 << 8)
-#define   CLK_CO_PHASE_180 (2 << 8)
-#define   CLK_CO_PHASE_270 (3 << 8)
-#define   CLK_TX_PHASE_000 (0 << 10)
-#define   CLK_TX_PHASE_090 (1 << 10)
-#define   CLK_TX_PHASE_180 (2 << 10)
-#define   CLK_TX_PHASE_270 (3 << 10)
+
+#define   CLK_CORE_PHASE_MASK  GENMASK(9, 8)
+#define   CLK_TX_PHASE_MASKGENMASK(11, 10)
+#define   CLK_RX_PHASE_MASKGENMASK(13, 12)
+
 #define   CLK_ALWAYS_ONBIT(24)
 
 #define MESON_SD_EMMC_CFG  0x44
diff --git a/drivers/mmc/meson_gx_mmc.c b/drivers/mmc/meson_gx_mmc.c
index 86c1a7164a..402981c3bb 100644
--- a/drivers/mmc/meson_gx_mmc.c
+++ b/drivers/mmc/meson_gx_mmc.c
@@ -52,10 +52,11 @@ static void meson_mmc_config_clock(struct mmc *mmc)
clk_div = DIV_ROUND_UP(clk, mmc->clock);
 
/* 180 phase core clock */
-   meson_mmc_clk |= CLK_CO_PHASE_180;
-
-   /* 180 phase tx clock */
-   meson_mmc_clk |= CLK_TX_PHASE_000;
+   meson_mmc_clk |= CLK_CORE_PHASE_MASK;
+   /* 000 phase rx clock */
+   meson_mmc_clk |= CLK_RX_PHASE_MASK;
+   /* 000 phase tx clock */
+   meson_mmc_clk |= CLK_TX_PHASE_MASK;
 
/* clock settings */
meson_mmc_clk |= clk_src;
-- 
2.24.1



[U-Boot] [PATCHv2 0/3] Odroid n2 using eMMC would fail to boot up

2019-12-24 Thread Anand Moon
Here are some small changes to fix booting of Odroid N2 using eMMC.
Fixed the clk tunnig during mmc initialization.

Build and tested on top of below patches
[0] https://patchwork.ozlabs.org/patch/1213648/
[1] https://patchwork.ozlabs.org/patch/1213650/

Tested on below eMMC module on Odroid N2 and C2
new orange - eMMC AJNB4R 14.6 GiB MMC 5.1
old back   - eMMC CGND3R 58.2 GiB MMC 5.0

Prevoius changes: [3] https://patchwork.ozlabs.org/cover/1201206/

-Anand

Anand Moon (3):
  mmc: meson-gx: Fix clk phase tuning for MMC
  configs: meson64: enable GIC support for G12A/G12B
  board: amlogic: select PWRSEQ for all amlogic platform

 arch/arm/include/asm/arch-meson/sd_emmc.h | 14 ++
 arch/arm/mach-meson/Kconfig   |  1 +
 drivers/mmc/meson_gx_mmc.c|  9 +
 include/configs/meson64.h |  2 +-
 4 files changed, 13 insertions(+), 13 deletions(-)

-- 
2.24.1



[U-Boot] [PATCHv2 2/3] configs: meson64: enable GIC support for G12A/G12B

2019-12-24 Thread Anand Moon
Enable GIC support for G12A/G12B platform.

Signed-off-by: Anand Moon 
Reviewed-by: Neil Armstrong 
---
no changes
---
 include/configs/meson64.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/include/configs/meson64.h b/include/configs/meson64.h
index 736081277d..50707a3197 100644
--- a/include/configs/meson64.h
+++ b/include/configs/meson64.h
@@ -8,7 +8,7 @@
 #define __MESON64_CONFIG_H
 
 /* Generic Interrupt Controller Definitions */
-#if defined(CONFIG_MESON_AXG)
+#if (defined(CONFIG_MESON_AXG) || defined(CONFIG_MESON_G12A))
 #define GICD_BASE  0xffc01000
 #define GICC_BASE  0xffc02000
 #else /* MESON GXL and GXBB */
-- 
2.24.1



Re: Garbage UART output on RPI 4 with upstream kernel DTB

2019-12-24 Thread Matthias Brugger



On 24/12/2019 12:56, Stefan Wahren wrote:
> Hi Matthias,
> 
> Am 23.12.19 um 21:20 schrieb Matthias Brugger:
>> Hi Stefan,
>>
>> On 23/12/2019 19:51, Stefan Wahren wrote:
>>> Am 20.12.19 um 14:58 schrieb Stefan Wahren:
 Hi,

 i tried to run current U-Boot (rpi_4_32b_defconfig) on my RPi 4 with
 bcm2711-rpi-4-b.dtb from the upstream kernel. Unfortunately i only see
 garbage on the debug UART (pin 14 & 15). Using the DTB from the
 downstream kernel has a proper UART output. The config.txt contains
 debug_uart=1

 I compared both and identified an offending Linux commit:
 ARM: dts: bcm283x: Remove brcm,bcm2835-pl011 compatible

 Unfortunately reverting this patch still doesn't fix the issue. Any ideas?
>>> After hours of playing with the DTB, i finally found the reason why the
>>> upstream kernel DTB doesn't work with U-Boot on RPI 4. The DTS must be
>>> compiled with flag "-@".
>>>
>>> It isn't clear to me, why this is necessary but it would be nice to make
>>> U-Boot work without this.
>>>
>> Can you provide the exact command you are using?
> 
> i didn't call dtc directly via command line. I just added this Makefile
> hack [1] to the Linux upstream tree. This will add a __symbols__ section
> to the DTB which is required for overlays. You can verify this with dtdiff.
> 
> Since the upstream kernel boots without the __symbols__ section, i think
> this garbage output issue could be fixed in u-boot.
> 

Little by little I grasp what you are doing. So you compile the DTB in the
kernel, then pass the blob to u-boot and add it with OF_EMBED? Is that correct?

Regards,
Matthias


Re: Garbage UART output on RPI 4 with upstream kernel DTB

2019-12-24 Thread Stefan Wahren
Hi Matthias,

Am 23.12.19 um 21:20 schrieb Matthias Brugger:
> Hi Stefan,
>
> On 23/12/2019 19:51, Stefan Wahren wrote:
>> Am 20.12.19 um 14:58 schrieb Stefan Wahren:
>>> Hi,
>>>
>>> i tried to run current U-Boot (rpi_4_32b_defconfig) on my RPi 4 with
>>> bcm2711-rpi-4-b.dtb from the upstream kernel. Unfortunately i only see
>>> garbage on the debug UART (pin 14 & 15). Using the DTB from the
>>> downstream kernel has a proper UART output. The config.txt contains
>>> debug_uart=1
>>>
>>> I compared both and identified an offending Linux commit:
>>> ARM: dts: bcm283x: Remove brcm,bcm2835-pl011 compatible
>>>
>>> Unfortunately reverting this patch still doesn't fix the issue. Any ideas?
>> After hours of playing with the DTB, i finally found the reason why the
>> upstream kernel DTB doesn't work with U-Boot on RPI 4. The DTS must be
>> compiled with flag "-@".
>>
>> It isn't clear to me, why this is necessary but it would be nice to make
>> U-Boot work without this.
>>
> Can you provide the exact command you are using?

i didn't call dtc directly via command line. I just added this Makefile
hack [1] to the Linux upstream tree. This will add a __symbols__ section
to the DTB which is required for overlays. You can verify this with dtdiff.

Since the upstream kernel boots without the __symbols__ section, i think
this garbage output issue could be fixed in u-boot.

Regards
Stefan

[1] -
https://github.com/raspberrypi/linux/blob/b85f76a63d5f1b13220c61244469d55487db84f1/arch/arm/boot/dts/Makefile#L1231

>
> Regards,
> Matthias



[PATCH 1/1] virtio: fix typo devicd

2019-12-24 Thread Heinrich Schuchardt
%s/devicd/device

Signed-off-by: Heinrich Schuchardt 
---
 drivers/virtio/virtio_mmio.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/virtio/virtio_mmio.c b/drivers/virtio/virtio_mmio.c
index 7b738703b8..a67b354122 100644
--- a/drivers/virtio/virtio_mmio.c
+++ b/drivers/virtio/virtio_mmio.c
@@ -363,7 +363,7 @@ static int virtio_mmio_probe(struct udevice *udev)
return 0;
}

-   /* Check devicd ID */
+   /* Check device ID */
uc_priv->device = readl(priv->base + VIRTIO_MMIO_DEVICE_ID);
if (uc_priv->device == 0) {
/*
--
2.24.0



RE: [PATCH] mpc85xx: ddr: Always start DDR RAM in Self Refresh mode

2019-12-24 Thread Priyanka Jain
>-Original Message-
>From: Joakim Tjernlund 
>Sent: Thursday, November 28, 2019 12:05 AM
>To: Priyanka Jain ; u-boot@lists.denx.de
>Cc: jo...@infinera.com 
>Subject: [PATCH] mpc85xx: ddr: Always start DDR RAM in Self Refresh mode
>
>Some of our t1042 boards fails DDR init with an Automatic calibration error
>every now and then. Investigations revealed that true Warm boots newer
>failed. Warm boots has some extra steps performed, one being to start DDRC
>in Self Refresh and then clearing SR right after.
>Applying this SR method unconditionally made all our boards stable again,
>regardless of Cold/Warm boot.
>
>Signed-off-by: Joakim Tjernlund 
>---
> drivers/ddr/fsl/mpc85xx_ddr_gen3.c | 13 ++---
> 1 file changed, 6 insertions(+), 7 deletions(-)
>
>diff --git a/drivers/ddr/fsl/mpc85xx_ddr_gen3.c
>b/drivers/ddr/fsl/mpc85xx_ddr_gen3.c
>index a9b085db8c..952b296dd8 100644
>--- a/drivers/ddr/fsl/mpc85xx_ddr_gen3.c
>+++ b/drivers/ddr/fsl/mpc85xx_ddr_gen3.c
>@@ -370,6 +370,8 @@ step2:
>   debug("Setting DEBUG_3[21] to 0x%08x\n", in_be32(
>>debug[2]));
>
> #endif/* part 1 of the workaound */
>+  /* Always start in self-refresh, clear after MEM_EN */
>+  setbits_be32(>sdram_cfg_2, SDRAM_CFG2_FRC_SR);
>
>   /*
>* 500 painful micro-seconds must elapse between @@ -382,8 +384,6
>@@ step2:
>
> #ifdef CONFIG_DEEP_SLEEP
>   if (is_warm_boot()) {
>-  /* enter self-refresh */
>-  setbits_be32(>sdram_cfg_2, SDRAM_CFG2_FRC_SR);
>   /* do board specific memory setup */
>   board_mem_sleep_setup();
>   temp_sdram_cfg = (in_be32(>sdram_cfg) |
>SDRAM_CFG_BI); @@ -395,6 +395,10 @@ step2:
>   out_be32(>sdram_cfg, temp_sdram_cfg |
>SDRAM_CFG_MEM_EN);
>   asm volatile("sync;isync");
>
>+  /* Exit self-refresh after DDR conf as some ddr memories can fail. */
>+  clrbits_be32(>sdram_cfg_2, SDRAM_CFG2_FRC_SR);
>+  asm volatile("sync;isync");
>+
>   total_gb_size_per_controller = 0;
>   for (i = 0; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
>   if (!(regs->cs[i].config & 0x8000)) @@ -544,9 +548,4 @@
>step2:
>   clrbits_be32(>sdram_cfg, 0x2);
>   }
> #endif /* CONFIG_SYS_FSL_ERRATUM_DDR111_DDR134 */ -#ifdef
>CONFIG_DEEP_SLEEP
>-  if (is_warm_boot())
>-  /* exit self-refresh */
>-  clrbits_be32(>sdram_cfg_2, SDRAM_CFG2_FRC_SR);
>-#endif
> }
>--
>2.23.0
Patch applied on u-boot-mpc85xx. Awaiting upstream
Thanks Priyanka


RE: [PATCH v2 01/19] arch: powerpc: add usb node in p3041 dts

2019-12-24 Thread Priyanka Jain
>-Original Message-
>From: Ran Wang 
>Sent: Thursday, December 12, 2019 3:01 PM
>To: Priyanka Jain 
>Cc: Ran Wang ; open list 
>Subject: [PATCH v2 01/19] arch: powerpc: add usb node in p3041 dts
>
>Signed-off-by: Ran Wang 
>---
>Change in v2:
>   - Rebase.
>
> arch/powerpc/dts/p3041.dtsi | 12 
> 1 file changed, 12 insertions(+)
>
>diff --git a/arch/powerpc/dts/p3041.dtsi b/arch/powerpc/dts/p3041.dtsi index
>e873db2..6736d00 100644
>--- a/arch/powerpc/dts/p3041.dtsi
>+++ b/arch/powerpc/dts/p3041.dtsi
>@@ -60,6 +60,18 @@
>   clock-frequency = <0x0>;
>   };
>
>+  usb0: usb@fe21 {
>+  compatible = "fsl-usb2-mph";
>+  reg = <0x21 0x1000>;
>+  phy_type = "utmi";
>+  };
>+
>+  usb1: usb@fe211000 {
>+  compatible = "fsl-usb2-dr";
>+  reg = <0x211000 0x1000>;
>+  phy_type = "utmi";
>+  };
>+
>   sata: sata@22 {
>   compatible = "fsl,pq-sata-v2";
>   reg = <0x22 0x1000>;
>--
>2.7.4
Series applied on u-boot-mpc85xx. Awaiting upstream
Thanks Priyanka


RE: [PATCH v3] ppc/km: enable DM driver support in all defconfigs

2019-12-24 Thread Priyanka Jain



>-Original Message-
>From: Holger Brunck 
>Sent: Monday, November 25, 2019 2:35 PM
>To: u-boot@lists.denx.de
>Cc: Holger Brunck ; Mario Six
>; Wolfgang Denk ; Valentin Longchamp
>; Priyanka Jain 
>Subject: [PATCH v3] ppc/km: enable DM driver support in all defconfigs
>
>This is the first step to use DM support for the KM powerpc boards.
>
>Signed-off-by: Holger Brunck 
>CC: Mario Six 
>CC: Wolfgang Denk 
>CC: Valentin Longchamp 
>CC: Priyanka Jain 
>---
>Changes for v3:
>  - rebase on latest master
>Changes for v2:
>  - fix typo in e-mail address
>
> configs/kmcoge4_defconfig   | 2 ++
> configs/kmcoge5ne_defconfig | 2 ++
> configs/kmeter1_defconfig   | 2 ++
> configs/kmopti2_defconfig   | 2 ++
> configs/kmsupx5_defconfig   | 2 ++
> configs/kmtegr1_defconfig   | 2 ++
> configs/kmtepr2_defconfig   | 2 ++
> configs/suvd3_defconfig | 2 ++
> configs/tuge1_defconfig | 2 ++
> configs/tuxx1_defconfig | 2 ++
> 10 files changed, 20 insertions(+)
>
>diff --git a/configs/kmcoge4_defconfig b/configs/kmcoge4_defconfig index
>ad2de2cd62..1635a51e7c 100644
>--- a/configs/kmcoge4_defconfig
>+++ b/configs/kmcoge4_defconfig
>@@ -6,6 +6,7 @@ CONFIG_SYS_BOOTCOUNT_ADDR=0xFB20
> CONFIG_ENV_SECT_SIZE=0x1
> CONFIG_MPC85xx=y
> CONFIG_TARGET_KMP204X=y
>+# CONFIG_SYS_MALLOC_F is not set
> CONFIG_FIT=y
> CONFIG_FIT_VERBOSE=y
> CONFIG_OF_BOARD_SETUP=y
>@@ -42,6 +43,7 @@ CONFIG_ENV_IS_IN_SPI_FLASH=y
>CONFIG_SYS_REDUNDAND_ENVIRONMENT=y
> CONFIG_ENV_OFFSET_REDUND=0x11
> CONFIG_SYS_RELOC_GD_ENV_ADDR=y
>+CONFIG_DM=y
> CONFIG_BOOTCOUNT_LIMIT=y
> CONFIG_BOOTCOUNT_BOOTLIMIT=3
> # CONFIG_MMC is not set
>diff --git a/configs/kmcoge5ne_defconfig b/configs/kmcoge5ne_defconfig
>index 0d41396c25..472dc88fbf 100644
>--- a/configs/kmcoge5ne_defconfig
>+++ b/configs/kmcoge5ne_defconfig
>@@ -154,6 +154,7 @@ CONFIG_ACR_PARKM_USB_I2C1_BOOT=y
>CONFIG_LCRR_DBYP_PLL_BYPASSED=y  CONFIG_LCRR_EADC_2=y
>CONFIG_LCRR_CLKDIV_4=y
>+# CONFIG_SYS_MALLOC_F is not set
> CONFIG_OF_BOARD_SETUP=y
> CONFIG_OF_STDOUT_VIA_ALIAS=y
> CONFIG_MISC_INIT_R=y
>@@ -182,6 +183,7 @@ CONFIG_CMD_UBI=y
> CONFIG_SYS_REDUNDAND_ENVIRONMENT=y
> CONFIG_ENV_ADDR=0xF00C
> CONFIG_ENV_ADDR_REDUND=0xF00E
>+CONFIG_DM=y
> CONFIG_BOOTCOUNT_LIMIT=y
> CONFIG_BOOTCOUNT_BOOTLIMIT=3
> # CONFIG_MMC is not set
>diff --git a/configs/kmeter1_defconfig b/configs/kmeter1_defconfig index
>4d9f412020..02b17ed624 100644
>--- a/configs/kmeter1_defconfig
>+++ b/configs/kmeter1_defconfig
>@@ -118,6 +118,7 @@ CONFIG_ACR_PARKM_USB_I2C1_BOOT=y
>CONFIG_LCRR_DBYP_PLL_BYPASSED=y  CONFIG_LCRR_EADC_2=y
>CONFIG_LCRR_CLKDIV_4=y
>+# CONFIG_SYS_MALLOC_F is not set
> CONFIG_OF_BOARD_SETUP=y
> CONFIG_OF_STDOUT_VIA_ALIAS=y
> CONFIG_MISC_INIT_R=y
>@@ -145,6 +146,7 @@ CONFIG_CMD_UBI=y
> CONFIG_SYS_REDUNDAND_ENVIRONMENT=y
> CONFIG_ENV_ADDR=0xF00C
> CONFIG_ENV_ADDR_REDUND=0xF00E
>+CONFIG_DM=y
> CONFIG_BOOTCOUNT_LIMIT=y
> CONFIG_BOOTCOUNT_BOOTLIMIT=3
> # CONFIG_MMC is not set
>diff --git a/configs/kmopti2_defconfig b/configs/kmopti2_defconfig index
>80af105584..21af286a85 100644
>--- a/configs/kmopti2_defconfig
>+++ b/configs/kmopti2_defconfig
>@@ -138,6 +138,7 @@ CONFIG_ACR_APARK_MASTER=y
>CONFIG_ACR_PARKM_USB_I2C1_BOOT=y  CONFIG_LCRR_EADC_1=y
>CONFIG_LCRR_CLKDIV_2=y
>+# CONFIG_SYS_MALLOC_F is not set
> CONFIG_OF_BOARD_SETUP=y
> CONFIG_OF_STDOUT_VIA_ALIAS=y
> CONFIG_MISC_INIT_R=y
>@@ -165,6 +166,7 @@ CONFIG_CMD_UBI=y
> CONFIG_SYS_REDUNDAND_ENVIRONMENT=y
> CONFIG_ENV_ADDR=0xF00C
> CONFIG_ENV_ADDR_REDUND=0xF00E
>+CONFIG_DM=y
> CONFIG_BOOTCOUNT_LIMIT=y
> CONFIG_BOOTCOUNT_BOOTLIMIT=3
> # CONFIG_MMC is not set
>diff --git a/configs/kmsupx5_defconfig b/configs/kmsupx5_defconfig index
>4e58aed96a..8eb6a7c38d 100644
>--- a/configs/kmsupx5_defconfig
>+++ b/configs/kmsupx5_defconfig
>@@ -118,6 +118,7 @@ CONFIG_ACR_APARK_MASTER=y
>CONFIG_ACR_PARKM_USB_I2C1_BOOT=y  CONFIG_LCRR_EADC_1=y
>CONFIG_LCRR_CLKDIV_2=y
>+# CONFIG_SYS_MALLOC_F is not set
> CONFIG_OF_BOARD_SETUP=y
> CONFIG_OF_STDOUT_VIA_ALIAS=y
> CONFIG_MISC_INIT_R=y
>@@ -145,6 +146,7 @@ CONFIG_CMD_UBI=y
> CONFIG_SYS_REDUNDAND_ENVIRONMENT=y
> CONFIG_ENV_ADDR=0xF00C
> CONFIG_ENV_ADDR_REDUND=0xF00E
>+CONFIG_DM=y
> CONFIG_BOOTCOUNT_LIMIT=y
> CONFIG_BOOTCOUNT_BOOTLIMIT=3
> # CONFIG_MMC is not set
>diff --git a/configs/kmtegr1_defconfig b/configs/kmtegr1_defconfig index
>9cf0cde923..27a58833f8 100644
>--- a/configs/kmtegr1_defconfig
>+++ b/configs/kmtegr1_defconfig
>@@ -116,6 +116,7 @@ CONFIG_ACR_APARK_MASTER=y
>CONFIG_ACR_PARKM_USB_I2C1_BOOT=y  CONFIG_LCRR_EADC_1=y
>CONFIG_LCRR_CLKDIV_2=y
>+# CONFIG_SYS_MALLOC_F is not set
> CONFIG_OF_BOARD_SETUP=y
> CONFIG_OF_STDOUT_VIA_ALIAS=y
> CONFIG_SYS_EXTRA_OPTIONS="KMTEGR1"
>@@ -146,6 +147,7 @@ CONFIG_CMD_UBI=y
> CONFIG_SYS_REDUNDAND_ENVIRONMENT=y
> CONFIG_ENV_ADDR=0xF010
> CONFIG_ENV_ADDR_REDUND=0xF012
>+CONFIG_DM=y
> CONFIG_BOOTCOUNT_LIMIT=y
> CONFIG_BOOTCOUNT_BOOTLIMIT=3
> # CONFIG_MMC is not set
>diff --git a/configs/kmtepr2_defconfig b/configs/kmtepr2_defconfig index

RE: [PATCH 1/4] ppc/km/tegr1: support second localbus clock signal

2019-12-24 Thread Priyanka Jain



>-Original Message-
>From: Holger Brunck 
>Sent: Tuesday, November 26, 2019 11:39 PM
>To: u-boot@lists.denx.de
>Cc: Holger Brunck ; Priyanka Jain
>; Valentin Longchamp
>
>Subject: [PATCH 1/4] ppc/km/tegr1: support second localbus clock signal
>
>On kmtegr1 we have to specify the second localbus clock signal also instead of
>using the default for our ppc 8309 boards.
>
>Signed-off-by: Holger Brunck 
>CC: Priyanka Jain 
>CC: Valentin Longchamp 
>---
> include/configs/km/km-mpc8309.h |  2 ++
> include/configs/kmtegr1.h   | 17 +
> 2 files changed, 19 insertions(+)
>
>diff --git a/include/configs/km/km-mpc8309.h b/include/configs/km/km-
>mpc8309.h index 9aaea273e0..e710c04493 100644
>--- a/include/configs/km/km-mpc8309.h
>+++ b/include/configs/km/km-mpc8309.h
>@@ -22,6 +22,7 @@
>  * System IO Config
>  */
> /* 0x14000180 SICR_1 */
>+#ifndef CONFIG_SYS_SICRL
> #define CONFIG_SYS_SICRL (0   \
>   | SICR_1_UART1_UART1RTS \
>   | SICR_1_I2C_CKSTOP \
>@@ -38,6 +39,7 @@
>   | SICR_1_FEC1_FEC1  \
>   | SICR_1_FEC2_FEC2  \
>   )
>+#endif
>
> /* 0x00080400 SICR_2 */
> #define CONFIG_SYS_SICRH (0   \
>diff --git a/include/configs/kmtegr1.h b/include/configs/kmtegr1.h index
>e627606222..a441fca121 100644
>--- a/include/configs/kmtegr1.h
>+++ b/include/configs/kmtegr1.h
>@@ -30,6 +30,23 @@
> #define CONFIG_SYS_MAX_NAND_DEVICE1
> #define NAND_MAX_CHIPS1
>
>+#define CONFIG_SYS_SICRL (0 \
>+  | SICR_1_UART1_UART1RTS   \
>+  | SICR_1_I2C_CKSTOP   \
>+  | SICR_1_IRQ_A_IRQ\
>+  | SICR_1_IRQ_B_IRQ\
>+  | SICR_1_GPIO_A_GPIO\
>+  | SICR_1_GPIO_B_GPIO\
>+  | SICR_1_GPIO_C_GPIO\
>+  | SICR_1_GPIO_D_GPIO\
>+  | SICR_1_GPIO_E_LCS\
>+  | SICR_1_GPIO_F_GPIO\
>+  | SICR_1_USB_A_UART2S   \
>+  | SICR_1_USB_B_UART2RTS   \
>+  | SICR_1_FEC1_FEC1\
>+  | SICR_1_FEC2_FEC2\
>+  )
>+
> /* include common defines/options for all Keymile boards */  #include
>"km/keymile-common.h"
> #include "km/km-powerpc.h"
>--
>2.24.0.rc1
Series applied on u-boot-mpc85xx. Awaiting upstream
Thanks
Priyanka


pull request u-boot-mpc85xx to next branch

2019-12-24 Thread Priyanka Jain
Dear Tom,



Please find my pull-request for u-boot-mpc85xx/master

https://travis-ci.org/p-priyanka-jain/u-boot/builds/628982496

Summary

enable DM driver on ppc/km boards

enable DM_USB for some of NXP powerpc platforms: P5040, T4240, T208x, T104x, 
P4080, P2041, P2020, P1020, P3041

Some updates in mpc85xx-ddr driver, km boards



priyankajain

---

The following changes since commit c0912f9bbfb26dd03d189953678691b799d35b6e:



  Merge branch 'next' of https://gitlab.denx.de/u-boot/custodians/u-boot-x86 
into next (2019-12-18 07:20:19 -0500)



are available in the Git repository at:



  https://gitlab.denx.de/u-boot/custodians/u-boot-mpc85xx.git HEAD



for you to fetch changes up to 4466b99703197c6258aded7728eed7c292343a43:



  ppc/km: enable DM driver support in all defconfigs (2019-12-23 14:08:27 +0530)





Holger Brunck (5):

  ppc/km/tegr1: support second localbus clock signal

  ppc/km: convert KM_DEF_NETDEV to Kconfig

  board/km: remove CONFIG_KM_BOARD_NAME

  ppc/kmeter1: remove unused define

  ppc/km: enable DM driver support in all defconfigs



Joakim Tjernlund (1):

  mpc85xx: ddr: Always start DDR RAM in Self Refresh mode



Ran Wang (19):

  arch: powerpc: add usb node in p3041 dts

  configs: p3041ds: enable CONFIG_DM_USB support

  arch: powerpc: add usb nodes to P1020 dts

  configs: p1020: enable CONFIG_DM_USB support

  arch: powerpc: add usb node to p2020 dts

  configs: P2020: enable CONFIG_DM_USB support

  arch: powerpc: add usb node to p2041 dts

  configs: P2041: enable CONFIG_DM_USB support

  arch: powerpc: add usb nodes to P4080 dts

  configs: P4080: enable CONFIG_DM_USB support

  powerpc: add usb nodes to T102x dts

  configs: T102x: enable CONFIG_DM_USB support

  arch: powerpc: add usb nodes to T104x dts

  configs: T104x: enable CONFIG_DM_USB support

  configs: T208x: enable CONFIG_DM_USB support

  arch: powerpc: add usb nodes to T4240 dts

  configs: T4240: enable CONFIG_DM_USB support

  arch: powerpc: add usb nodes to P5040 dts

  configs: P5040: enable CONFIG_DM_USB support



arch/powerpc/dts/p1020-post.dtsi | 13 +

arch/powerpc/dts/p2020-post.dtsi |  6 ++

arch/powerpc/dts/p2041.dtsi  | 12 

arch/powerpc/dts/p3041.dtsi  | 12 

arch/powerpc/dts/p4080.dtsi  | 12 

arch/powerpc/dts/p5040.dtsi  | 12 

arch/powerpc/dts/t102x.dtsi  | 12 

arch/powerpc/dts/t104x.dtsi  | 12 

arch/powerpc/dts/t4240.dtsi  | 12 

board/keymile/Kconfig|  6 ++

board/keymile/km83xx/km83xx.c|  2 +-

board/keymile/kmp204x/kmp204x.c  |  2 +-

configs/P1020MBG-PC_36BIT_SDCARD_defconfig   |  1 +

configs/P1020MBG-PC_36BIT_defconfig  |  1 +

configs/P1020MBG-PC_SDCARD_defconfig |  1 +

configs/P1020MBG-PC_defconfig|  1 +

configs/P1020RDB-PC_36BIT_NAND_defconfig |  1 +

configs/P1020RDB-PC_36BIT_SDCARD_defconfig   |  1 +

configs/P1020RDB-PC_36BIT_SPIFLASH_defconfig |  1 +

configs/P1020RDB-PC_36BIT_defconfig  |  1 +

configs/P1020RDB-PC_NAND_defconfig   |  1 +

configs/P1020RDB-PC_SDCARD_defconfig |  1 +

configs/P1020RDB-PC_SPIFLASH_defconfig   |  1 +

configs/P1020RDB-PC_defconfig|  1 +

configs/P1020RDB-PD_NAND_defconfig   |  1 +

configs/P1020RDB-PD_SDCARD_defconfig |  1 +

configs/P1020RDB-PD_SPIFLASH_defconfig   |  1 +

configs/P1020RDB-PD_defconfig|  1 +

configs/P1020UTM-PC_36BIT_SDCARD_defconfig   |  1 +

configs/P1020UTM-PC_36BIT_defconfig  |  1 +

configs/P1020UTM-PC_SDCARD_defconfig |  1 +

configs/P1020UTM-PC_defconfig|  1 +

configs/P2020RDB-PC_36BIT_NAND_defconfig |  1 +

configs/P2020RDB-PC_36BIT_SDCARD_defconfig   |  1 +

configs/P2020RDB-PC_36BIT_SPIFLASH_defconfig |  1 +

configs/P2020RDB-PC_36BIT_defconfig  |  1 +

configs/P2020RDB-PC_NAND_defconfig   |  1 +

configs/P2020RDB-PC_SDCARD_defconfig |  1 +

configs/P2020RDB-PC_SPIFLASH_defconfig   |  1 +

configs/P2020RDB-PC_defconfig|  1 +

configs/P2041RDB_NAND_defconfig  |  1 +

configs/P2041RDB_SDCARD_defconfig|  1 +

configs/P2041RDB_SPIFLASH_defconfig  |  1 +

configs/P2041RDB_SRIO_PCIE_BOOT_defconfig|  1 +

configs/P2041RDB_defconfig   |  1 +

configs/P3041DS_NAND_defconfig   |  1 +

configs/P3041DS_SDCARD_defconfig |  1 +

configs/P3041DS_SPIFLASH_defconfig   |  1 +

configs/P3041DS_SRIO_PCIE_BOOT_defconfig |  1 +

configs/P3041DS_defconfig|  1 +


Re: [PATCH v2 2/2] usb: dwc2: Add polling for reset status

2019-12-24 Thread Marek Vasut
On 12/24/19 9:23 AM, Ley Foon Tan wrote:
> In Cyclone 5 SoC platform, the first USB probing is failed but second
> probing is success. DWC2 driver read gsnpsid register right after de-assert
> reset, but controller is not ready yet and it returns gsnpsid 0.
> Polling reset status after de-assert reset to solve the issue.
> 
> Retry with this fix more than 10 times without issue.
> 
> => usb reset
> resetting USB...
> Bus usb@ffb4: usb probe
> SNPSID invalid (not DWC2 OTG device): 
> Port not available.
> => usb reset
> resetting USB...
> Bus usb@ffb4: usb probe
> scanning bus usb@ffb4 for devices... 2 USB Device(s) found
>scanning usb for storage devices... 1 Storage Device(s) found
> 
> Signed-off-by: Ley Foon Tan 
> 
> ---
> v2:
> - Change from constant delay to poll for reset status.
> ---
>  drivers/usb/host/dwc2.c | 10 +-
>  1 file changed, 9 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/usb/host/dwc2.c b/drivers/usb/host/dwc2.c
> index b9c56f763b..1bc41990ee 100644
> --- a/drivers/usb/host/dwc2.c
> +++ b/drivers/usb/host/dwc2.c
> @@ -1132,6 +1132,7 @@ int _submit_int_msg(struct dwc2_priv *priv, struct 
> usb_device *dev,
>  static int dwc2_reset(struct udevice *dev)
>  {
>   int ret;
> + int i = 1000;
>   struct dwc2_priv *priv = dev_get_priv(dev);
>  
>   ret = reset_get_bulk(dev, >resets);
> @@ -1153,7 +1154,14 @@ static int dwc2_reset(struct udevice *dev)
>   return ret;
>   }
>  
> - return 0;
> + /* Poll until reset is completed. */
> + do {
> + ret = reset_status(>resets.resets[0]);
> + if (!ret)
> + return 0;
> + } while (i--);
> +
> + return -ETIMEDOUT;

[...]

Can't we implement some sort of "reset_wait_clear()" API instead of
open-coding this in the driver ?

Or rather, shouldn't the socfpga_reset_deassert wait until the reset was
actually de-asserted ? This might be even better, I think it should.


Re: [PATCH v2 1/2] reset: socfpga: Add reset status callback function

2019-12-24 Thread Marek Vasut
On 12/24/19 9:23 AM, Ley Foon Tan wrote:
> Add .rst_status callback function.
> Read reset status from register, return 0 if deasserted,
> non-zero if asserted
> 
> Signed-off-by: Ley Foon Tan 
> ---
>  drivers/reset/reset-socfpga.c | 12 
>  1 file changed, 12 insertions(+)
> 
> diff --git a/drivers/reset/reset-socfpga.c b/drivers/reset/reset-socfpga.c
> index 93ec9cfdb6..b77c893b76 100644
> --- a/drivers/reset/reset-socfpga.c
> +++ b/drivers/reset/reset-socfpga.c
> @@ -99,11 +99,23 @@ static int socfpga_reset_free(struct reset_ctl *reset_ctl)
>   return 0;
>  }
>  
> +static int socfpga_reset_status(struct reset_ctl *reset_ctl)
> +{
> + struct socfpga_reset_data *data = dev_get_priv(reset_ctl->dev);
> + int id = reset_ctl->id;
> + int reg_width = sizeof(u32);
> + int bank = id / (reg_width * BITS_PER_BYTE);
> + int offset = id % (reg_width * BITS_PER_BYTE);

You want to flag these int values as const, since that's what they are.
I think some of them are unsigned.

Otherwise looks good, thanks.


[PATCH v2 2/2] usb: dwc2: Add polling for reset status

2019-12-24 Thread Ley Foon Tan
In Cyclone 5 SoC platform, the first USB probing is failed but second
probing is success. DWC2 driver read gsnpsid register right after de-assert
reset, but controller is not ready yet and it returns gsnpsid 0.
Polling reset status after de-assert reset to solve the issue.

Retry with this fix more than 10 times without issue.

=> usb reset
resetting USB...
Bus usb@ffb4: usb probe
SNPSID invalid (not DWC2 OTG device): 
Port not available.
=> usb reset
resetting USB...
Bus usb@ffb4: usb probe
scanning bus usb@ffb4 for devices... 2 USB Device(s) found
   scanning usb for storage devices... 1 Storage Device(s) found

Signed-off-by: Ley Foon Tan 

---
v2:
- Change from constant delay to poll for reset status.
---
 drivers/usb/host/dwc2.c | 10 +-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/drivers/usb/host/dwc2.c b/drivers/usb/host/dwc2.c
index b9c56f763b..1bc41990ee 100644
--- a/drivers/usb/host/dwc2.c
+++ b/drivers/usb/host/dwc2.c
@@ -1132,6 +1132,7 @@ int _submit_int_msg(struct dwc2_priv *priv, struct 
usb_device *dev,
 static int dwc2_reset(struct udevice *dev)
 {
int ret;
+   int i = 1000;
struct dwc2_priv *priv = dev_get_priv(dev);
 
ret = reset_get_bulk(dev, >resets);
@@ -1153,7 +1154,14 @@ static int dwc2_reset(struct udevice *dev)
return ret;
}
 
-   return 0;
+   /* Poll until reset is completed. */
+   do {
+   ret = reset_status(>resets.resets[0]);
+   if (!ret)
+   return 0;
+   } while (i--);
+
+   return -ETIMEDOUT;
 }
 
 static int dwc2_init_common(struct udevice *dev, struct dwc2_priv *priv)
-- 
2.19.0



[PATCH v2 1/2] reset: socfpga: Add reset status callback function

2019-12-24 Thread Ley Foon Tan
Add .rst_status callback function.
Read reset status from register, return 0 if deasserted,
non-zero if asserted

Signed-off-by: Ley Foon Tan 
---
 drivers/reset/reset-socfpga.c | 12 
 1 file changed, 12 insertions(+)

diff --git a/drivers/reset/reset-socfpga.c b/drivers/reset/reset-socfpga.c
index 93ec9cfdb6..b77c893b76 100644
--- a/drivers/reset/reset-socfpga.c
+++ b/drivers/reset/reset-socfpga.c
@@ -99,11 +99,23 @@ static int socfpga_reset_free(struct reset_ctl *reset_ctl)
return 0;
 }
 
+static int socfpga_reset_status(struct reset_ctl *reset_ctl)
+{
+   struct socfpga_reset_data *data = dev_get_priv(reset_ctl->dev);
+   int id = reset_ctl->id;
+   int reg_width = sizeof(u32);
+   int bank = id / (reg_width * BITS_PER_BYTE);
+   int offset = id % (reg_width * BITS_PER_BYTE);
+
+   return readl(data->modrst_base + (bank * BANK_INCREMENT)) & BIT(offset);
+}
+
 static const struct reset_ops socfpga_reset_ops = {
.request = socfpga_reset_request,
.free = socfpga_reset_free,
.rst_assert = socfpga_reset_assert,
.rst_deassert = socfpga_reset_deassert,
+   .rst_status = socfpga_reset_status,
 };
 
 static int socfpga_reset_probe(struct udevice *dev)
-- 
2.19.0



[PATCH v2 0/2] reset/usb: Add reset status

2019-12-24 Thread Ley Foon Tan
In Cyclone 5 SoC platform, the first USB probing is failed but second
probing is success. DWC2 driver read gsnpsid register right after de-assert
reset, but controller is not ready yet and it returns gsnpsid 0.
Polling reset status after de-assert reset to solve the issue.

This patchset also add .rst_status callback function to reset-socfpga driver
to support this get reset status feature.

v1->v2:
- Change from constant delay to poll for reset status;
- Add reset status callback function.

History:
v1: https://patchwork.ozlabs.org/patch/1214841/

Ley Foon Tan (2):
  reset: socfpga: Add reset status callback function
  usb: dwc2: Add polling for reset status

 drivers/reset/reset-socfpga.c | 12 
 drivers/usb/host/dwc2.c   | 10 +-
 2 files changed, 21 insertions(+), 1 deletion(-)

-- 
2.19.0