RE: [PATCH v8 18/21] configs: fu540: Add config options for U-Boot SPL

2020-05-10 Thread Pragnesh Patel
>-Original Message-
>From: Jagan Teki 
>Sent: 10 May 2020 14:50
>To: Pragnesh Patel 
>Cc: U-Boot-Denx ; Atish Patra
>; Palmer Dabbelt ; Bin
>Meng ; Paul Walmsley ;
>Troy Benjegerdes ; Anup Patel
>; Sagar Kadam ; Rick Chen
>; Palmer Dabbelt ; Joe
>Hershberger 
>Subject: Re: [PATCH v8 18/21] configs: fu540: Add config options for U-Boot
>SPL
>
>[External Email] Do not click links or attachments unless you recognize the
>sender and know the content is safe
>
>On Sat, May 9, 2020 at 8:02 PM Pragnesh Patel 
>wrote:
>>
>> With sifive_fu540_defconfig:
>>
>> User can use FSBL or u-boot-spl.bin anyone at a time.
>>
>> For FSBL,
>> fsbl->fw_payload.bin (opensbi + U-Boot)
>>
>> For u-boot-spl.bin,
>> u-boot-spl.bin->FIT image (opensbi + U-Boot + dtb)
>
>s/U-Boot/U-Boot proper

Will update in v9.

>
>>
>> U-Boot SPL will be loaded by ZSBL from SD card (replace fsbl.bin with
>> u-boot-spl.bin) and runs in L2 LIM in machine mode and then load FIT
>> image u-boot.itb from SD card into RAM.
>>
>> U-Boot SPL expects u-boot.itb FIT image at the starting of SD card
>> sector number (0x822) of GUID type "2E54B353-1271-4842-806F-
>E436D6AF6985"
>>
>> Signed-off-by: Pragnesh Patel 
>> Signed-off-by: Jagan Teki 
>> ---
>
>Reviewed-by: Jagan Teki 


RE: [PATCH v7 14/22] riscv: Add place-holder for driver compilation

2020-05-10 Thread Pragnesh Patel
>-Original Message-
>From: Jagan Teki 
>Sent: 10 May 2020 14:42
>To: Pragnesh Patel 
>Cc: U-Boot-Denx ; Atish Patra
>; Palmer Dabbelt ; Bin
>Meng ; Paul Walmsley ;
>Troy Benjegerdes ; Anup Patel
>; Sagar Kadam ; Rick Chen
>
>Subject: Re: [PATCH v7 14/22] riscv: Add place-holder for driver compilation
>
>[External Email] Do not click links or attachments unless you recognize the
>sender and know the content is safe
>
>On Sun, May 3, 2020 at 2:47 PM Pragnesh Patel 
>wrote:
>>
>> Hi Jagan,
>>
>> >-Original Message-
>> >From: Jagan Teki 
>> >Sent: 02 May 2020 21:58
>> >To: Pragnesh Patel 
>> >Cc: U-Boot-Denx ; Atish Patra
>> >; Palmer Dabbelt ;
>Bin
>> >Meng ; Paul Walmsley
>;
>> >Troy Benjegerdes ; Anup Patel
>> >; Sagar Kadam ; Rick
>Chen
>> >
>> >Subject: Re: [PATCH v7 14/22] riscv: Add place-holder for driver
>> >compilation
>> >
>> >[External Email] Do not click links or attachments unless you
>> >recognize the sender and know the content is safe
>> >
>> >On Sat, May 2, 2020 at 3:38 PM Pragnesh Patel
>> >
>> >wrote:
>> >>
>> >> Add place-holder for RISC-V fu540 CPU
>> >>
>> >> Signed-off-by: Pragnesh Patel 
>> >> ---
>> >>  arch/riscv/include/asm/arch-fu540/clk.h  | 14 +
>> >> arch/riscv/include/asm/arch-fu540/gpio.h | 38
>> >> 
>> >
>> >Where is it using exactly?
>>
>> "drivers/gpio/sifive-gpio.c"
>
>I mean which patch used these headers?

This headers are necessary for existing SiFive drivers.

Earlier CONFIG_SYS_CPU is "generic", so drivers use headers from " 
arch/riscv/include/asm/arch-generic/" now
We have changed the CONFIG_SYS_CPU to "fu540" so now drivers will use headers 
from
"arch/riscv/include/asm/arch-fu540/".

>
>Jagan.


[PATCH 1/2] imx: imx8mp_evk: fix boot issue

2020-05-10 Thread Peng Fan
The u-boot-spl.bin pad with ddr firmware conflicts with the
CONFIG_MALLOC_F_ADDR area, the ddr firmware will be overwritten
by malloc in SPL stage and cause ddr initialization not able
to finish. So update the related addresses to fix the issue.

Reported-by: Fabio Estevam 
Signed-off-by: Peng Fan 
---
 include/configs/imx8mp_evk.h | 8 
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/include/configs/imx8mp_evk.h b/include/configs/imx8mp_evk.h
index 80e5738961..b90a4f6932 100644
--- a/include/configs/imx8mp_evk.h
+++ b/include/configs/imx8mp_evk.h
@@ -23,15 +23,15 @@
 #ifdef CONFIG_SPL_BUILD
 /*#define CONFIG_ENABLE_DDR_TRAINING_DEBUG*/
 #define CONFIG_SPL_LDSCRIPT"arch/arm/cpu/armv8/u-boot-spl.lds"
-#define CONFIG_SPL_STACK   0x99
-#define CONFIG_SPL_BSS_START_ADDR  0x0095e000
-#define CONFIG_SPL_BSS_MAX_SIZE0x2000  /* 8 KB */
+#define CONFIG_SPL_STACK   0x98fc00
+#define CONFIG_SPL_BSS_START_ADDR  0x0098fc00
+#define CONFIG_SPL_BSS_MAX_SIZE0x400   /* 1 KB */
 #define CONFIG_SYS_SPL_MALLOC_START0x4220
 #define CONFIG_SYS_SPL_MALLOC_SIZE SZ_512K /* 512 KB */
 #define CONFIG_SYS_ICACHE_OFF
 #define CONFIG_SYS_DCACHE_OFF
 
-#define CONFIG_MALLOC_F_ADDR   0x94
+#define CONFIG_MALLOC_F_ADDR   0x95
 
 #define CONFIG_SPL_ABORT_ON_RAW_IMAGE
 
-- 
2.16.4



[PATCH 2/2] imx8mp_evk: simplify board spl code

2020-05-10 Thread Peng Fan
Simplify board SPL code
 - BSS area will be cleared by crt0_64.S
 - No need to get ccm device in spl_board_init
 - Use spl_early_init, not spl_init
 - timer_init has been invoked in arch_cpu_init

Signed-off-by: Peng Fan 
---
 board/freescale/imx8mp_evk/spl.c | 28 ++--
 1 file changed, 2 insertions(+), 26 deletions(-)

diff --git a/board/freescale/imx8mp_evk/spl.c b/board/freescale/imx8mp_evk/spl.c
index c5f640dc7b..5f3ea0b6a1 100644
--- a/board/freescale/imx8mp_evk/spl.c
+++ b/board/freescale/imx8mp_evk/spl.c
@@ -25,11 +25,6 @@
 #include 
 #include 
 
-#include 
-#include 
-#include 
-#include 
-
 DECLARE_GLOBAL_DATA_PTR;
 
 int spl_board_boot_device(enum boot_device boot_dev_spl)
@@ -44,16 +39,7 @@ void spl_dram_init(void)
 
 void spl_board_init(void)
 {
-   struct udevice *dev;
-   int ret;
-
puts("Normal Boot\n");
-
-   ret = uclass_get_device_by_name(UCLASS_CLK,
-   "clock-controller@3038",
-   );
-   if (ret < 0)
-   printf("Failed to find clock node. Check device tree\n");
 }
 
 #define I2C_PAD_CTRL (PAD_CTL_DSE6 | PAD_CTL_HYS | PAD_CTL_PUE | PAD_CTL_PE)
@@ -114,6 +100,7 @@ int board_fit_config_name_match(const char *name)
 }
 #endif
 
+/* Do not use BSS area in this phase */
 void board_init_f(ulong dummy)
 {
int ret;
@@ -124,19 +111,10 @@ void board_init_f(ulong dummy)
 
board_early_init_f();
 
-   timer_init();
+   spl_early_init();
 
preloader_console_init();
 
-   /* Clear the BSS. */
-   memset(__bss_start, 0, __bss_end - __bss_start);
-
-   ret = spl_init();
-   if (ret) {
-   debug("spl_init() failed: %d\n", ret);
-   hang();
-   }
-
enable_tzc380();
 
setup_i2c(0, CONFIG_SYS_I2C_SPEED, 0x7f, _pad_info1);
@@ -145,6 +123,4 @@ void board_init_f(ulong dummy)
 
/* DDR initialization */
spl_dram_init();
-
-   board_init_r(NULL, 0);
 }
-- 
2.16.4



RE: [PATCH v7 01/22] misc: add driver for the SiFive otp controller

2020-05-10 Thread Pragnesh Patel
>-Original Message-
>From: Jagan Teki 
>Sent: 10 May 2020 14:34
>To: Pragnesh Patel 
>Cc: U-Boot-Denx ; Atish Patra
>; Palmer Dabbelt ; Bin
>Meng ; Paul Walmsley ;
>Troy Benjegerdes ; Anup Patel
>; Sagar Kadam ; Rick Chen
>; Tero Kristo ; Simon Glass
>; Adam Ford ; Eugen Hristev
>; Peng Fan ; Finley Xiao
>; Kever Yang 
>Subject: Re: [PATCH v7 01/22] misc: add driver for the SiFive otp controller
>
>[External Email] Do not click links or attachments unless you recognize the
>sender and know the content is safe
>
>On Sat, May 2, 2020 at 9:48 PM Pragnesh Patel 
>wrote:
>>
>> Hi Jagan,
>>
>> >-Original Message-
>> >From: Jagan Teki 
>> >Sent: 02 May 2020 21:21
>> >To: Pragnesh Patel 
>> >Cc: U-Boot-Denx ; Atish Patra
>> >; Palmer Dabbelt ;
>Bin
>> >Meng ; Paul Walmsley
>;
>> >Troy Benjegerdes ; Anup Patel
>> >; Sagar Kadam ; Rick
>Chen
>> >; Tero Kristo ; Simon Glass
>> >; Adam Ford ; Eugen Hristev
>> >; Peng Fan ; Finley
>> >Xiao ; Kever Yang
>> >
>> >Subject: Re: [PATCH v7 01/22] misc: add driver for the SiFive otp
>> >controller
>> >
>> >[External Email] Do not click links or attachments unless you
>> >recognize the sender and know the content is safe
>> >
>> >On Sat, May 2, 2020 at 9:12 PM Pragnesh Patel
>> >
>> >wrote:
>> >>
>> >> Hi Jagan,
>> >>
>> >> >-Original Message-
>> >> >From: Jagan Teki 
>> >> >Sent: 02 May 2020 21:07
>> >> >To: Pragnesh Patel 
>> >> >Cc: U-Boot-Denx ; Atish Patra
>> >> >; Palmer Dabbelt
>;
>> >Bin
>> >> >Meng ; Paul Walmsley
>> >;
>> >> >Troy Benjegerdes ; Anup Patel
>> >> >; Sagar Kadam ; Rick
>> >Chen
>> >> >; Tero Kristo ; Simon Glass
>> >> >; Adam Ford ; Eugen
>Hristev
>> >> >; Peng Fan ;
>Finley
>> >> >Xiao ; Kever Yang
>> >> >
>> >> >Subject: Re: [PATCH v7 01/22] misc: add driver for the SiFive otp
>> >> >controller
>> >> >
>> >> >[External Email] Do not click links or attachments unless you
>> >> >recognize the sender and know the content is safe
>> >> >
>> >> >On Sat, May 2, 2020 at 3:37 PM Pragnesh Patel
>> >> >
>> >> >wrote:
>> >> >>
>> >> >> Added a misc driver to handle OTP memory in SiFive SoCs.
>> >> >>
>> >> >> Signed-off-by: Pragnesh Patel 
>> >> >> ---
>> >> >>  drivers/misc/Kconfig  |   7 +
>> >> >>  drivers/misc/Makefile |   1 +
>> >> >>  drivers/misc/sifive-otp.c | 273
>> >> >> ++
>> >> >>  3 files changed, 281 insertions(+)  create mode 100644
>> >> >> drivers/misc/sifive-otp.c
>> >> >>
>> >> >> diff --git a/drivers/misc/Kconfig b/drivers/misc/Kconfig index
>> >> >> 766402745d..03464f1010 100644
>> >> >> --- a/drivers/misc/Kconfig
>> >> >> +++ b/drivers/misc/Kconfig
>> >> >> @@ -68,6 +68,13 @@ config ROCKCHIP_OTP
>> >> >>   addressing and a length or through child-nodes that are
>generated
>> >> >>   based on the e-fuse map retrieved from the DTS.
>> >> >>
>> >> >> +config SIFIVE_OTP
>> >> >> +   bool "SiFive eMemory OTP driver"
>> >> >> +   depends on RISCV && MISC
>> >> >
>> >> >This can be something like this.
>> >> >
>> >> >config SIFIVE_OTP
>> >> >   bool "SiFive eMemory OTP driver"
>> >> >   depends on SIFIVE_FU540
>> >>
>> >> This config option is not FU540 specific but it's for all SiFive SoCs.
>> >
>> >Well RISCV is not SIFIVE, better add SIFIVE_FU540 as of now if you
>> >have more SoC's in future will update by adding one more or add new
>symbol SIFIVE.
>>
>> IMHO it's better not to make it SoC specific, Board Kconfig file will imply 
>> this
>option if needed.
>> This is something we follow for FU540.
>>
>> SIFIVE_OTP is depends on SIFIVE SoCs and all SIFIVE SoCs are RISCV
>> specific that's why I added depends on RISCV.
>
>Well not all RISCV SoC's are SiFive, since you have SIFIVE_FU540 now add
>depends for now rest will follow similar or common config item for SiFive socs
>in future. Check any driver in drivers or drivers/misc it is common notation.

Already remove the depends on RISCV in v8.
Making a common config for SiFive SoCs is good option that we will do in future 
and that common
Kconfig will imply or select all the necessary config option.

If you will see, other Kconfig in drivers/misc then they are not depend on SoC 
but SoC config (configs/**defconfig) file
Will select it. 

Ideally, SIFIVE_OTP is also not depends on SIFIVE_FU540 so it's better to 
select or implied SIFIVE_OTP by board Kconfig.
Right now, SIFIVE_OTP is implied by "board/sifive/fu540/Kconfig".

>
>Jagan.


Re: wandboard does not boot with current mainline

2020-05-10 Thread Heiko Schocher

Hello Simon,

Am 08.05.2020 um 20:33 schrieb Simon Glass:

Hi Heiko

On Thu, 7 May 2020 at 21:35, Heiko Schocher  wrote:


Hello Tom,

Am 07.05.2020 um 16:38 schrieb Tom Rini:

On Thu, May 07, 2020 at 02:53:49PM +0200, Heiko Schocher wrote:

Hello Fabio,

Am 07.05.2020 um 14:12 schrieb Heiko Schocher:

Hello Fabio,

I have my wandboard DL in my automated daily build setup example:

http://xeidos.ddns.net/ubtestresults/

and wondered, why my last result is from may 4th ...

(Ok, there is a bug, that I do not see not booting boards in this page,
but this is just a proof of concept page ...)


Ok, found this error, now I have one broken build in above webpage.
Also, the newest commit adds > 300 bytes imge size...

http://xeidos.ddns.net/ubtestresults/stats/wandboard_defconfig/6


That's not what I see:
$ (export SOURCE_DATE_EPOCH=`date +%s`; ./tools/buildman/buildman -o /tmp/wandboard3 
--step 0 -SBC -b 125956..69bf66 -devl wandboard && ./tools/buildman/buildman -o 
/tmp/wandboard3 --step 0 -SsB -b 125956..69bf66 -devl wandboard)
Building 2 commits for 1 boards (1 thread, 16 jobs per thread)
01: scripts/get_default_envs.sh: preserve order of multiple entries for same 
variable
15: Merge branch '2020-05-06-master-imports'
  200 /2  wandboard
Completed: 2 total built, duration 0:00:23
Summary of 2 commits for 1 boards (1 thread, 16 jobs per thread)
01: scripts/get_default_envs.sh: preserve order of multiple entries for same 
variable
15: Merge branch '2020-05-06-master-imports'
 arm: (for 1/1 boards) bss -16.0 text +16.0
  wandboard  : bss -16 text +16
 u-boot: add: 0/0, grow: 3/0 bytes: 12/0 (12)
   function   old new   
delta
   menu_create 84  88  
+4
   handle_pxe_menu284 288  
+4
   do_dcache   92  96  
+4
(no errors to report)
$

What details are you recording in your setup?


I simply track the size of u-boot.img for the wandboard:

https://github.com/EmbLux-Kft/tbot-tbot2go/blob/wandboard-devel-messe/tc/wandboard/tc_wandboard.py#L95




Unfortunately I cannot automate git bisect with tbot:

http://tbot.tools/modules/tc.html#tbot.tc.git.GitRepository.bisect

as I do not know how to switch for example with an gpio and a relay
bootmode on wandboard ... do you know, if this is possible?


In general, or on the wandboard?  For the general side, I have a network
controllable relay controller that I can use to toggle a GPIO on
hardware that has a force reset exposed.  I don't have any imx platforms
exposed however as I wasn't able to rig up some way to boot via USB on
the platforms I have here.  That was a while ago tho.


Only for the wandboard. I have a collection of usbrelais and simple
relais connected to gpios, my preffered setup currently:

https://www.amazon.de/gp/product/B07RNWLXJM/ref=ppx_yo_dt_b_asin_title_o06_s00?ie=UTF8=1

attached to a raspberry PI, so I have with the PI a complete "lab host"
which runs tbot incl. wifi to connect to the rest of the world ;-)

But it seems wandboard has only sd card boot, so I need for this
a sd card simulator... what I do not have yet.


I have some SDwire units and you can buy them from the designer in Poland.

I am happy to make some more but need to know how many. I made some as
a test run at about $40 each.


Marek is also working on such stuff, added Marek and Wolfgang to cc...

bye,
Heiko
--
DENX Software Engineering GmbH,  Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: +49-8142-66989-52   Fax: +49-8142-66989-80   Email: h...@denx.de


Re: [PATCH v3 21/22] Use __ASSEMBLY__ as the assembly macros

2020-05-10 Thread Masahiro Yamada
On Mon, May 11, 2020 at 2:43 AM Simon Glass  wrote:
>
> Some places use __ASSEMBLER__ instead which does not work since the
> Makefile does not define it. Fix them.
>
> Signed-off-by: Simon Glass 


I think we decided to not do this in v2, didn't we?

http://patchwork.ozlabs.org/project/uboot/patch/20200409201506.133129-6-...@chromium.org/


> ---
>
> Changes in v3: None
> Changes in v2:
> - Add new patch to fix occurances of __ASSEMBLER__
>
>  arch/arm/include/asm/arch-fsl-layerscape/immap_lsch3.h | 2 +-
>  arch/arm/include/asm/arch-mx27/regs-rtc.h  | 2 +-
>  arch/arm/include/asm/arch-mx5/imx-regs.h   | 2 +-
>  arch/arm/include/asm/arch-mx6/imx-regs.h   | 2 +-
>  arch/arm/include/asm/arch-mx7/imx-regs.h   | 2 +-
>  arch/arm/include/asm/arch-s32v234/imx-regs.h   | 2 +-
>  arch/arm/include/asm/arch-vf610/imx-regs.h | 2 +-
>  arch/arm/mach-socfpga/include/mach/clock_manager.h | 2 +-
>  arch/arm/mach-socfpga/include/mach/clock_manager_arria10.h | 4 ++--
>  arch/arm/mach-socfpga/include/mach/clock_manager_gen5.h| 4 ++--
>  arch/arm/mach-socfpga/include/mach/sdram_arria10.h | 2 +-
>  arch/arm/mach-stm32mp/include/mach/stm32.h | 2 +-
>  arch/x86/include/asm/mtrr.h| 2 +-
>  arch/x86/include/asm/sipi.h| 4 ++--
>  include/atf_common.h   | 2 +-
>  include/elf.h  | 4 ++--
>  16 files changed, 20 insertions(+), 20 deletions(-)
>
> diff --git a/arch/arm/include/asm/arch-fsl-layerscape/immap_lsch3.h 
> b/arch/arm/include/asm/arch-fsl-layerscape/immap_lsch3.h
> index c2fbc23b11..baa9fa8529 100644
> --- a/arch/arm/include/asm/arch-fsl-layerscape/immap_lsch3.h
> +++ b/arch/arm/include/asm/arch-fsl-layerscape/immap_lsch3.h
> @@ -589,5 +589,5 @@ struct ccsr_serdes {
> u8 res5[0x19fc - 0xa00];
>  };
>
> -#endif /*__ASSEMBLY__*/
> +#endif /*__ASSEMBLY__ */
>  #endif /* __ARCH_FSL_LSCH3_IMMAP_H_ */
> diff --git a/arch/arm/include/asm/arch-mx27/regs-rtc.h 
> b/arch/arm/include/asm/arch-mx27/regs-rtc.h
> index d373ab1583..8434f4c1cb 100644
> --- a/arch/arm/include/asm/arch-mx27/regs-rtc.h
> +++ b/arch/arm/include/asm/arch-mx27/regs-rtc.h
> @@ -21,6 +21,6 @@ struct rtc_regs {
> u32 dayr;
> u32 dayalarm;
>  };
> -#endif /* __ASSEMBLY__*/
> +#endif /* __ASSEMBLY__ */
>
>  #endif /* __MX28_REGS_RTC_H__ */
> diff --git a/arch/arm/include/asm/arch-mx5/imx-regs.h 
> b/arch/arm/include/asm/arch-mx5/imx-regs.h
> index fbb6e599b6..3d1cc68322 100644
> --- a/arch/arm/include/asm/arch-mx5/imx-regs.h
> +++ b/arch/arm/include/asm/arch-mx5/imx-regs.h
> @@ -557,6 +557,6 @@ struct pwm_regs {
> u32 cnr;
>  };
>
> -#endif /* __ASSEMBLER__*/
> +#endif /* __ASSEMBLY__ */
>
>  #endif /* __ASM_ARCH_MX5_IMX_REGS_H__ */
> diff --git a/arch/arm/include/asm/arch-mx6/imx-regs.h 
> b/arch/arm/include/asm/arch-mx6/imx-regs.h
> index 4f01b20aed..5b41a7a4b8 100644
> --- a/arch/arm/include/asm/arch-mx6/imx-regs.h
> +++ b/arch/arm/include/asm/arch-mx6/imx-regs.h
> @@ -1003,5 +1003,5 @@ struct pwm_regs {
>   */
>  #defineis_boot_from_usb(void) (!(readl(USB_PHY0_BASE_ADDR) & 
> (1<<20)))
>
> -#endif /* __ASSEMBLER__*/
> +#endif /* __ASSEMBLY__ */
>  #endif /* __ASM_ARCH_MX6_IMX_REGS_H__ */
> diff --git a/arch/arm/include/asm/arch-mx7/imx-regs.h 
> b/arch/arm/include/asm/arch-mx7/imx-regs.h
> index 6336514007..7b23abbc7e 100644
> --- a/arch/arm/include/asm/arch-mx7/imx-regs.h
> +++ b/arch/arm/include/asm/arch-mx7/imx-regs.h
> @@ -1223,5 +1223,5 @@ struct bootrom_sw_info {
> u32 reserved_3[3];
>  };
>
> -#endif /* __ASSEMBLER__*/
> +#endif /* __ASSEMBLY__ */
>  #endif /* __ASM_ARCH_MX7_IMX_REGS_H__ */
> diff --git a/arch/arm/include/asm/arch-s32v234/imx-regs.h 
> b/arch/arm/include/asm/arch-s32v234/imx-regs.h
> index 9a779cce96..1472a43f1b 100644
> --- a/arch/arm/include/asm/arch-s32v234/imx-regs.h
> +++ b/arch/arm/include/asm/arch-s32v234/imx-regs.h
> @@ -323,6 +323,6 @@ struct mscm_ir {
> u32 ipcie[4];   /* Interconnect Parity Checking Injection 
> Enable Register   */
>  };
>
> -#endif /* __ASSEMBLER__ */
> +#endif /* __ASSEMBLY__ */
>
>  #endif /* __ASM_ARCH_IMX_REGS_H__ */
> diff --git a/arch/arm/include/asm/arch-vf610/imx-regs.h 
> b/arch/arm/include/asm/arch-vf610/imx-regs.h
> index ae0a187c4d..03def8e3c5 100644
> --- a/arch/arm/include/asm/arch-vf610/imx-regs.h
> +++ b/arch/arm/include/asm/arch-vf610/imx-regs.h
> @@ -474,6 +474,6 @@ struct mscm {
> u32 cpxcfg3;
>  };
>
> -#endif /* __ASSEMBLER__*/
> +#endif /* __ASSEMBLY__ */
>
>  #endif /* __ASM_ARCH_IMX_REGS_H__ */
> diff --git a/arch/arm/mach-socfpga/include/mach/clock_manager.h 
> b/arch/arm/mach-socfpga/include/mach/clock_manager.h
> index c6830582a5..1f734bcd65 100644
> --- a/arch/arm/mach-socfpga/include/mach/clock_manager.h
> 

Re: [PATCH] omap3logic: Return omap3logic boards to OF_PLATDATA in SPL

2020-05-10 Thread Lokesh Vutla



On 25/04/20 6:29 PM, Adam Ford wrote:
> SPL is just simply too tight and we don't really have any
> room to shave off any more functionality, so we need OF_PLATDATA
> in SPL.
> 
> This patch adds the manual enumeration of serial and MMC while
> removing the device tree support in SPL and enabling
> SPL_OF_PLATDATA.
> 
> Signed-off-by: Adam Ford 

I see the following build failure with this patch:

➜  u-boot-ti git:(for-rc) v7make omap35_logic_somlv_defconfig
#
# configuration written to .config
#
➜  u-boot-ti git:(for-rc) v7make -j4 -s
arm-none-linux-gnueabihf-ld.bfd: drivers/built-in.o: in function 
`ofnode_get_name':
/home/lokesh/working/mainline/u-boot-ti/drivers/core/ofnode.c:272: undefined
reference to `fdt_get_name'
arm-none-linux-gnueabihf-ld.bfd: drivers/built-in.o: in function
`ofnode_parse_phandle_with_args':
/home/lokesh/working/mainline/u-boot-ti/drivers/core/ofnode.c:420: undefined
reference to `fdtdec_parse_phandle_with_args'
scripts/Makefile.spl:422: recipe for target 'spl/u-boot-spl' failed
make[1]: *** [spl/u-boot-spl] Error 1
Makefile:1940: recipe for target 'spl/u-boot-spl' failed
make: *** [spl/u-boot-spl] Error 2
➜  u-boot-ti git:(for-rc)

Thanks and regards,
Lokesh

> 
> diff --git a/board/logicpd/omap3som/omap3logic.c 
> b/board/logicpd/omap3som/omap3logic.c
> index 21d8a21010..5a1fa292fc 100644
> --- a/board/logicpd/omap3som/omap3logic.c
> +++ b/board/logicpd/omap3som/omap3logic.c
> @@ -239,3 +239,33 @@ int board_eth_init(bd_t *bis)
>   return smc911x_initialize(0, CONFIG_SMC911X_BASE);
>  }
>  #endif
> +
> +/* This is only needed until SPL gets OF support that fits */
> +#ifdef CONFIG_SPL_BUILD
> +static const struct ns16550_platdata omap3logic_serial = {
> + .base = OMAP34XX_UART1,
> + .reg_shift = 2,
> + .clock = V_NS16550_CLK,
> + .fcr = UART_FCR_DEFVAL,
> +};
> +
> +U_BOOT_DEVICE(omap3logic_uart) = {
> + "ns16550_serial",
> + _serial
> +};
> +
> +static const struct omap_hsmmc_plat omap3_logic_mmc0_platdata = {
> + .base_addr = (struct hsmmc *)OMAP_HSMMC1_BASE,
> + .cfg.host_caps = MMC_MODE_HS_52MHz | MMC_MODE_HS | MMC_MODE_4BIT,
> + .cfg.f_min = 40,
> + .cfg.f_max = 5200,
> + .cfg.voltages = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195,
> + .cfg.b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT,
> +};
> +
> +U_BOOT_DEVICE(am335x_mmc0) = {
> + .name = "omap_hsmmc",
> + .platdata = _logic_mmc0_platdata,
> +};
> +
> +#endif
> diff --git a/configs/omap35_logic_defconfig b/configs/omap35_logic_defconfig
> index bcf4efc621..fbf60f9be2 100644
> --- a/configs/omap35_logic_defconfig
> +++ b/configs/omap35_logic_defconfig
> @@ -43,6 +43,7 @@ CONFIG_CMD_UBI=y
>  CONFIG_OF_CONTROL=y
>  CONFIG_SPL_OF_CONTROL=y
>  CONFIG_DEFAULT_DEVICE_TREE="logicpd-torpedo-35xx-devkit"
> +CONFIG_SPL_OF_PLATDATA=y
>  # CONFIG_ENV_IS_IN_FAT is not set
>  CONFIG_ENV_IS_IN_NAND=y
>  CONFIG_SPL_DM=y
> @@ -79,3 +80,5 @@ CONFIG_USB_MUSB_HOST=y
>  CONFIG_USB_MUSB_OMAP2PLUS=y
>  CONFIG_TWL4030_USB=y
>  CONFIG_BCH=y
> +CONFIG_SPL_TINY_MEMSET=y
> +# CONFIG_SPL_OF_LIBFDT is not set
> diff --git a/configs/omap35_logic_somlv_defconfig 
> b/configs/omap35_logic_somlv_defconfig
> index 51c5d449e0..07c6bf9039 100644
> --- a/configs/omap35_logic_somlv_defconfig
> +++ b/configs/omap35_logic_somlv_defconfig
> @@ -42,6 +42,7 @@ CONFIG_CMD_UBI=y
>  CONFIG_OF_CONTROL=y
>  CONFIG_SPL_OF_CONTROL=y
>  CONFIG_DEFAULT_DEVICE_TREE="logicpd-som-lv-35xx-devkit"
> +CONFIG_SPL_OF_PLATDATA=y
>  # CONFIG_ENV_IS_IN_FAT is not set
>  CONFIG_ENV_IS_IN_NAND=y
>  CONFIG_SPL_DM=y
> @@ -82,3 +83,5 @@ CONFIG_USB_MUSB_HOST=y
>  CONFIG_USB_MUSB_OMAP2PLUS=y
>  CONFIG_TWL4030_USB=y
>  CONFIG_BCH=y
> +CONFIG_SPL_TINY_MEMSET=y
> +# CONFIG_SPL_OF_LIBFDT is not set
> diff --git a/configs/omap3_logic_defconfig b/configs/omap3_logic_defconfig
> index 15925fb7db..275e7c65ab 100644
> --- a/configs/omap3_logic_defconfig
> +++ b/configs/omap3_logic_defconfig
> @@ -42,6 +42,7 @@ CONFIG_CMD_UBI=y
>  CONFIG_OF_CONTROL=y
>  CONFIG_SPL_OF_CONTROL=y
>  CONFIG_DEFAULT_DEVICE_TREE="logicpd-torpedo-37xx-devkit"
> +CONFIG_SPL_OF_PLATDATA=y
>  # CONFIG_ENV_IS_IN_FAT is not set
>  CONFIG_ENV_IS_IN_NAND=y
>  CONFIG_SPL_DM=y
> @@ -78,3 +79,5 @@ CONFIG_USB_MUSB_HOST=y
>  CONFIG_USB_MUSB_OMAP2PLUS=y
>  CONFIG_TWL4030_USB=y
>  CONFIG_BCH=y
> +CONFIG_SPL_TINY_MEMSET=y
> +# CONFIG_SPL_OF_LIBFDT is not set
> diff --git a/configs/omap3_logic_somlv_defconfig 
> b/configs/omap3_logic_somlv_defconfig
> index ea8fc37f12..ef23831ed4 100644
> --- a/configs/omap3_logic_somlv_defconfig
> +++ b/configs/omap3_logic_somlv_defconfig
> @@ -43,6 +43,7 @@ CONFIG_CMD_UBI=y
>  CONFIG_OF_CONTROL=y
>  CONFIG_SPL_OF_CONTROL=y
>  CONFIG_DEFAULT_DEVICE_TREE="logicpd-som-lv-37xx-devkit"
> +CONFIG_SPL_OF_PLATDATA=y
>  # CONFIG_ENV_IS_IN_FAT is not set
>  CONFIG_ENV_IS_IN_NAND=y
>  CONFIG_SPL_DM=y
> @@ -84,3 +85,5 @@ CONFIG_USB_MUSB_HOST=y
>  CONFIG_USB_MUSB_OMAP2PLUS=y
>  CONFIG_TWL4030_USB=y
>  CONFIG_BCH=y
> +CONFIG_SPL_TINY_MEMSET=y
> +# 

Re: [PATCH 1/8] semihosting: Change semihosting file operation functions into global symbols

2020-05-10 Thread Akashi Takahiro
On Thu, Apr 30, 2020 at 11:06:23PM +0530, Sughosh Ganu wrote:
> Change the semihosting file operation functions into external symbols
> so that they can be called from outside the file. These functions
> would be required to be called for implementing firmware update
> functionality for the qemu arm64 platform.
> 
> Signed-off-by: Sughosh Ganu 
> ---
>  arch/arm/lib/semihosting.c |  7 ---
>  include/semihosting.h  | 13 +
>  2 files changed, 17 insertions(+), 3 deletions(-)
>  create mode 100644 include/semihosting.h
> 
> diff --git a/arch/arm/lib/semihosting.c b/arch/arm/lib/semihosting.c
> index 2658026cf4..3aeda1303a 100644
> --- a/arch/arm/lib/semihosting.c
> +++ b/arch/arm/lib/semihosting.c
> @@ -13,6 +13,7 @@
>   */
>  #include 
>  #include 
> +#include 

Do we need this file here?

-Takahiro Akashi

>  #define SYSOPEN  0x01
>  #define SYSCLOSE 0x02
> @@ -43,7 +44,7 @@ static noinline long smh_trap(unsigned int sysnum, void 
> *addr)
>   * Open a file on the host. Mode is "r" or "rb" currently. Returns a file
>   * descriptor or -1 on error.
>   */
> -static long smh_open(const char *fname, char *modestr)
> +long smh_open(const char *fname, char *modestr)
>  {
>   long fd;
>   unsigned long mode;
> @@ -82,7 +83,7 @@ static long smh_open(const char *fname, char *modestr)
>  /*
>   * Read 'len' bytes of file into 'memp'. Returns 0 on success, else failure
>   */
> -static long smh_read(long fd, void *memp, size_t len)
> +long smh_read(long fd, void *memp, size_t len)
>  {
>   long ret;
>   struct smh_read_s {
> @@ -116,7 +117,7 @@ static long smh_read(long fd, void *memp, size_t len)
>  /*
>   * Close the file using the file descriptor
>   */
> -static long smh_close(long fd)
> +long smh_close(long fd)
>  {
>   long ret;
>  
> diff --git a/include/semihosting.h b/include/semihosting.h
> new file mode 100644
> index 00..f1bf419275
> --- /dev/null
> +++ b/include/semihosting.h
> @@ -0,0 +1,13 @@
> +// SPDX-License-Identifier: GPL-2.0+
> +/*
> + * Copyright (c) 2020, Linaro Limited
> + */
> +
> +#if !defined _SEMIHOSTING_H_
> +#define _SEMIHOSTING_H_
> +
> +long smh_open(const char *fname, char *modestr);
> +long smh_read(long fd, void *memp, size_t len);
> +long smh_close(long fd);
> +
> +#endif /* _SEMIHOSTING_H_ */
> -- 
> 2.17.1
> 


Re: [PATCH 6/8] efi: capsule: Add support for uefi capsule authentication

2020-05-10 Thread Akashi Takahiro
On Sun, May 10, 2020 at 04:56:21PM +0530, Sughosh Ganu wrote:
> On Fri, 8 May 2020 at 06:12, Akashi Takahiro 
> wrote:
> 
> > On Thu, May 07, 2020 at 05:20:35PM +0530, Sughosh Ganu wrote:
> > > On Thu, 7 May 2020 at 13:49, Akashi Takahiro  > >
> > > wrote:
> > >
> > > > Sughosh,
> > > >
> > > > On Thu, Apr 30, 2020 at 11:06:28PM +0530, Sughosh Ganu wrote:
> > > > > Add support for authenticating uefi capsules. Most of the signature
> > > > > verification functionality is shared with the uefi secure boot
> > > > > feature.
> > > > >
> > > > > The root certificate containing the public key used for the signature
> > > > > verification is stored as an efi variable, similar to the variables
> > > > > used for uefi secure boot. The root certificate is stored as an efi
> > > > > signature list(esl) file -- this file contains the x509 certificate
> > > > > which is the root certificate.
> > > > >
> > > > > Signed-off-by: Sughosh Ganu 
> > > > > ---
> > > > >  include/efi_api.h  |  17 +
> > > > >  include/efi_loader.h   |   8 ++-
> > > > >  lib/efi_loader/Kconfig |  16 +
> > > > >  lib/efi_loader/efi_capsule.c   | 126
> > +
> > > > >  lib/efi_loader/efi_signature.c |   4 +-
> > > > >  5 files changed, 167 insertions(+), 4 deletions(-)
> > > > >
> > > >
> > >
> > > 
> > >
> > > > diff --git a/lib/efi_loader/Kconfig b/lib/efi_loader/Kconfig
> > > > > index ec2976ceba..245deabbed 100644
> > > > > --- a/lib/efi_loader/Kconfig
> > > > > +++ b/lib/efi_loader/Kconfig
> > > > > @@ -110,6 +110,22 @@ config EFI_CAPSULE_FIT_DEVICE
> > > > >   help
> > > > > Define storage device, say 1:1, for storing FIT image
> > > > >
> > > > > +config EFI_CAPSULE_AUTHENTICATE
> > > > > + bool "Update Capsule authentication"
> > > > > + depends on EFI_HAVE_CAPSULE_SUPPORT
> > > > > + depends on EFI_CAPSULE_ON_DISK
> > > > > + depends on EFI_FIRMWARE_MANAGEMENT_PROTOCOL
> > > >
> > > > Do we need those two configurations?
> > > >
> > >
> > > Right, I think we can just do with the EFI_CAPSULE_ON_DISK. Will change.
> >
> > Actually I don't think we need EFI_CAPSULE_ON_DISK neither.
> >
> 
> We at least need EFI_HAVE_CAPSULE_SUPPORT, isn't it.

Ah, yes of course.

-Takahiro Akashi
> 
> >
> > >
> > > >
> > > > > + select SHA256
> > > > > + select RSA
> > > > > + select RSA_VERIFY
> > > > > + select RSA_VERIFY_WITH_PKEY
> > > > > + select X509_CERTIFICATE_PARSER
> > > > > + select PKCS7_MESSAGE_PARSER
> > > > > + default n
> > > > > + help
> > > > > +   Select this option if you want to enable capsule
> > > > > +   authentication
> > > > > +
> > > > >  config EFI_DEVICE_PATH_TO_TEXT
> > > > >   bool "Device path to text protocol"
> > > > >   default y
> > > > > diff --git a/lib/efi_loader/efi_capsule.c
> > b/lib/efi_loader/efi_capsule.c
> > > > > index 931d363edc..a265d36ff0 100644
> > > > > --- a/lib/efi_loader/efi_capsule.c
> > > > > +++ b/lib/efi_loader/efi_capsule.c
> > > > > @@ -12,6 +12,10 @@
> > > > >  #include 
> > > > >  #include 
> > > > >  #include 
> > > > > +#include "../lib/crypto/pkcs7_parser.h"
> > > > > +
> > > >
> > > > As you might notice, the location was changed by
> > > > my recent patch.
> > > >
> > >
> > > Will check and update.
> > >
> > >
> > > >
> > > > > +#include 
> > > > > +#include 
> > > > >
> > > > >  const efi_guid_t efi_guid_capsule_report = EFI_CAPSULE_REPORT_GUID;
> > > > >  static const efi_guid_t efi_guid_firmware_management_capsule_id =
> > > > > @@ -245,6 +249,128 @@ out:
> > > > >
> > > > >   return ret;
> > > > >  }
> > > > > +
> > > > > +#if defined(CONFIG_EFI_CAPSULE_AUTHENTICATE)
> > > > > +
> > > > > +const efi_guid_t efi_guid_capsule_root_cert_guid =
> > > > > + EFI_FIRMWARE_MANAGEMENT_CAPSULE_ID_GUID;
> > > > > +
> > > > > +__weak u16 *efi_get_truststore_name(void)
> > > > > +{
> > > > > + return L"CRT";
> > > >
> > > > This variable is not defined by UEFI specification, isn't it?
> > > > How can this variable be protected?
> > > >
> > >
> > > This is not part of the uefi specification. The uefi specification does
> > not
> > > mandate any particular method for storing the root certificate to be used
> > > for capsule authentication. The edk2 code gets the root certificate
> > through
> > > a pcd. I had tried using KEK variable for storing the root certificate,
> > and
> > > had also come up with an implementation. However, the addition/deletion
> > and
> > > update of the secure variables is very closely tied with the secure boot
> > > feature and the secure boot state of the system, which is the reason why
> > i
> > > dropped that solution and used a separate variable, which can be defined
> > by
> > > the vendor to store the root certificate.
> >
> > My concern is that, without any protection, the value of this variable
> > can be modified by a mal-user.
> > (One simple solution would be to set this variable read-only.)
> >
> 
> Yes, that 

Re: efi_loader: tighten PE verification code?

2020-05-10 Thread AKASHI Takahiro
Patrick,

On Fri, May 08, 2020 at 08:56:37PM +0200, Patrick Wildt wrote:
> Hi,
> 
> even though this mail has a diff, it's not supposed to be a patch.  I
> have started adjusting my fuzzer to the upstreamed EFI Secure Boot code
> and I hit a few issues right away.  Two of those I have already sent and
> were reviewed.  I have seen more, but since I needed to reschedule some
> things I couldn't continue and unfortunately have to postpone fuzzing
> the code.  I can assure you if someone starts fuzzing that code, we will
> find a few more bugs.

Thank you for examining the code.

> Especially since this is "Secure Boot", this part should definitely be
> air tight.

Yeah, we definitely need more eyes on the code.

> One thing I saw is that the virt_size is smaller then the memcpy below
> at the "Copy PE headers" line then actually copies.
> 
> Another thing I saw is SizeOfRawData can be bigger then the VirtualSize,
> and PointerToRawData + SizeOfRawData bigger then the allocated size.
> 
> I'm not sure if this is the right place to do the checks.  Maybe they
> need to be at the place where we are adding the image regions.  I guess
> the fields should be properly verified in view of "does it make sense?".
>
> Also I'm questioning whether it's a good idea to continue parsing the
> file if it's already clear that the signature can't be verified against
> the "db".  I can understand that you'd like to finish loading the file
> into an object, and some other instance decides whether or not it's fine
> to start that image, but you also open yourself to issues when you're
> parsing a file that obviously is against the current security policy.

One comment:
I have changed the logic in a past version when Heinrich pointed
out that we should return EFI_SECURITY_VIOLATION and that image
execution information table must also be supported.

"Status Codes Returned" by LoadImage API in UEFI specification says,

EFI_ACCESS_DENIED:Image was not loaded because the platform policy prohibits
  the image from being loaded. NULL is returned in *ImageHandle.
EFI_SECURITY_VIOLATION:Image was loaded and an ImageHandle was created with
  a valid EFI_LOADED_IMAGE_PROTOCOL. However, the current
  platform policy specifies that the image should not be
  started.

Now the code returns EFI_SECURITY_VIOLATION, instead of EFI_ACCESS_DENIED,
when image's signature can not be verified but yet the image itself will be
loaded.
When invoking the binary with StartImage API, it fails and put a record
in image execution information table.
(I have not yet submitted a patch for table support though.)

As UEFI specification doesn't say anything about "policy,"
I don't know which error code should be returned here.

-Takahiro Akashi

> If you keep on parsing a file that obviously (because tested against the
> "db") cannot be allowed to boot anyway, the checks for the headers need
> to be even tighter.
> 
> I'd be unhappy to see U-Boot CVEs come up regarding PE parsing issues.
> 
> Best regards,
> Patrick
> 
> diff --git a/lib/efi_loader/efi_image_loader.c 
> b/lib/efi_loader/efi_image_loader.c
> index 43a53d3dd1..d134b748be 100644
> --- a/lib/efi_loader/efi_image_loader.c
> +++ b/lib/efi_loader/efi_image_loader.c
> @@ -681,10 +681,11 @@ efi_status_t efi_load_pe(struct efi_loaded_image_obj 
> *handle,
>   }
>  
>   /* Authenticate an image */
> - if (efi_image_authenticate(efi, efi_size))
> - handle->auth_status = EFI_IMAGE_AUTH_PASSED;
> - else
> + if (!efi_image_authenticate(efi, efi_size)) {
>   handle->auth_status = EFI_IMAGE_AUTH_FAILED;
> + ret = EFI_SECURITY_VIOLATION;
> + goto err;
> + }
>  
>   /* Calculate upper virtual address boundary */
>   for (i = num_sections - 1; i >= 0; i--) {
> @@ -736,6 +737,15 @@ efi_status_t efi_load_pe(struct efi_loaded_image_obj 
> *handle,
>   goto err;
>   }
>  
> + if (virt_size < sizeof(*dos) + sizeof(*nt) +
> + nt->FileHeader.SizeOfOptionalHeader +
> + num_sections * sizeof(IMAGE_SECTION_HEADER)) {
> + efi_free_pages((uintptr_t) efi_reloc,
> +(virt_size + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT);
> + ret = EFI_LOAD_ERROR;
> + goto err;
> + }
> +
>   /* Copy PE headers */
>   memcpy(efi_reloc, efi,
>  sizeof(*dos)
> @@ -746,6 +756,13 @@ efi_status_t efi_load_pe(struct efi_loaded_image_obj 
> *handle,
>   /* Load sections into RAM */
>   for (i = num_sections - 1; i >= 0; i--) {
>   IMAGE_SECTION_HEADER *sec = [i];
> + if (sec->SizeOfRawData > sec->Misc.VirtualSize ||
> + sec->PointerToRawData + sec->SizeOfRawData > efi_size) {
> + efi_free_pages((uintptr_t) efi_reloc,
> +(virt_size + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT);
> + ret = EFI_LOAD_ERROR;
> +  

Re: [PATCH] kbuild: add -Werror=implicit-function-declaration

2020-05-10 Thread Marek Vasut
On 5/11/20 3:59 AM, Masahiro Yamada wrote:
> Hi Simon,
> 
> On Mon, May 11, 2020 at 5:37 AM Simon Glass  wrote:
>>
>> Hi Masahiro,
>>
>> On Sat, 9 May 2020 at 05:00, Masahiro Yamada  wrote:
>>>
>>> On Sat, May 9, 2020 at 3:16 AM Tom Rini  wrote:

 On Thu, May 07, 2020 at 09:16:40PM -0600, Simon Glass wrote:
> Hi Masahiro,
>
> On Thu, 7 May 2020 at 19:54, Masahiro Yamada  wrote:
>>
>> On Fri, May 8, 2020 at 10:39 AM Simon Glass  wrote:
>>>
>>> Hi Masahiro,
>>>
>>> On Thu, 7 May 2020 at 06:21, Masahiro Yamada
>>>  wrote:

 Add -Werror=implicit-function-declaration as Linux does.

 If you do not check the prototype, it may go wrong run-time.
 It is better to break the build, and require to include correct
 headers.

 Signed-off-by: Masahiro Yamada 
 ---

  Makefile | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
>>>
>>> NAK
>>>
>>> We already get a warning in this situation. This makes it painful for
>>> development since things that should be warnings end up being errors.
>>> So your build fails when really it should work well enough to do a
>>> test run with your new code. I don't think it has any benefit on code
>>> quality since we already detect warnings in gitlab, etc.
>>>
>>> U-Boot is set up so that warnings are reported and are easy to spot if
>>> you use 'make -s' (i.e. not buried in the output).
>>>
>>> Regards,
>>> Simon
>>
>>
>>
>> Linux added this flag in 2007.
>>
>> The intention seems to break the build earlier
>> when a non-existing function is used.
>>
>> I have not seen compliant about this flag in Linux.
>> What does it make different for U-Boot ?
>
> Well that commit message is quite misleading. The author is presumably
> ignoring the warnings that come out in the compile phase. For me they
> come up loud and clear. I don't know why it takes half an hour to get
> to the link stage. My average incremental build time is under 4
> seconds including the link.
>
> Finally, the warning does not tell you anything about whether the
> function doesn't exist. It just tells you you have left out a header
> file.
>
> I know how much of a pain this is, because coreboot does this. It does
> it partly because there is so much build output that the warnings are
> invisible unless they actually halt the build. Even then you have to
> search for what went wrong.

 I'm not immediately sure of the right answer here.  Part of the problem
 is that even with 'make -s' U-Boot can be horribly noisy due to device
 tree warnings.  I assume Yamada-san ran in to a problem and was
 expecting the build to have failed but instead was chasing down a
 run-time debug until finding this.
>>>
>>>
>>> I did not run into a problem.
>>>
>>> When I was replacing  with some lighter headers,
>>> I missed some warnings ( but I noticed them after all).
>>>
>>> In Linux, if I miss to include a header, it fails to build.
>>> In U-Boot, it does not.
>>>
>>> Personally, I like to align with Linux policy,
>>> but if you are not comfortable with this patch,
>>> please feel free to ignore it.
>>
>> I really don't understand the point of warnings if we are just going
>> to turn them into errors.
>>
>> How about adding an option to tell U-Boot to use -Werror, etc.? Then
>> those that what it can enable it.
> 
> 
> OK.  We can do it with
> 
> 
> make KCFLAGS=-Werror

I've had a few of these failures due to implicit fn declaration, so I'd
be all for adding the error by default. And if things error out and you
are too lazy to spot the error, use make -k ; make -k and the error will
be right there at the end.

So I'm fine with this patch as-is.

-- 
Best regards,
Marek Vasut


Re: [PATCH] kbuild: add -Werror=implicit-function-declaration

2020-05-10 Thread Masahiro Yamada
Hi Simon,

On Mon, May 11, 2020 at 5:37 AM Simon Glass  wrote:
>
> Hi Masahiro,
>
> On Sat, 9 May 2020 at 05:00, Masahiro Yamada  wrote:
> >
> > On Sat, May 9, 2020 at 3:16 AM Tom Rini  wrote:
> > >
> > > On Thu, May 07, 2020 at 09:16:40PM -0600, Simon Glass wrote:
> > > > Hi Masahiro,
> > > >
> > > > On Thu, 7 May 2020 at 19:54, Masahiro Yamada  
> > > > wrote:
> > > > >
> > > > > On Fri, May 8, 2020 at 10:39 AM Simon Glass  wrote:
> > > > > >
> > > > > > Hi Masahiro,
> > > > > >
> > > > > > On Thu, 7 May 2020 at 06:21, Masahiro Yamada
> > > > > >  wrote:
> > > > > > >
> > > > > > > Add -Werror=implicit-function-declaration as Linux does.
> > > > > > >
> > > > > > > If you do not check the prototype, it may go wrong run-time.
> > > > > > > It is better to break the build, and require to include correct
> > > > > > > headers.
> > > > > > >
> > > > > > > Signed-off-by: Masahiro Yamada 
> > > > > > > ---
> > > > > > >
> > > > > > >  Makefile | 2 +-
> > > > > > >  1 file changed, 1 insertion(+), 1 deletion(-)
> > > > > >
> > > > > > NAK
> > > > > >
> > > > > > We already get a warning in this situation. This makes it painful 
> > > > > > for
> > > > > > development since things that should be warnings end up being 
> > > > > > errors.
> > > > > > So your build fails when really it should work well enough to do a
> > > > > > test run with your new code. I don't think it has any benefit on 
> > > > > > code
> > > > > > quality since we already detect warnings in gitlab, etc.
> > > > > >
> > > > > > U-Boot is set up so that warnings are reported and are easy to spot 
> > > > > > if
> > > > > > you use 'make -s' (i.e. not buried in the output).
> > > > > >
> > > > > > Regards,
> > > > > > Simon
> > > > >
> > > > >
> > > > >
> > > > > Linux added this flag in 2007.
> > > > >
> > > > > The intention seems to break the build earlier
> > > > > when a non-existing function is used.
> > > > >
> > > > > I have not seen compliant about this flag in Linux.
> > > > > What does it make different for U-Boot ?
> > > >
> > > > Well that commit message is quite misleading. The author is presumably
> > > > ignoring the warnings that come out in the compile phase. For me they
> > > > come up loud and clear. I don't know why it takes half an hour to get
> > > > to the link stage. My average incremental build time is under 4
> > > > seconds including the link.
> > > >
> > > > Finally, the warning does not tell you anything about whether the
> > > > function doesn't exist. It just tells you you have left out a header
> > > > file.
> > > >
> > > > I know how much of a pain this is, because coreboot does this. It does
> > > > it partly because there is so much build output that the warnings are
> > > > invisible unless they actually halt the build. Even then you have to
> > > > search for what went wrong.
> > >
> > > I'm not immediately sure of the right answer here.  Part of the problem
> > > is that even with 'make -s' U-Boot can be horribly noisy due to device
> > > tree warnings.  I assume Yamada-san ran in to a problem and was
> > > expecting the build to have failed but instead was chasing down a
> > > run-time debug until finding this.
> >
> >
> > I did not run into a problem.
> >
> > When I was replacing  with some lighter headers,
> > I missed some warnings ( but I noticed them after all).
> >
> > In Linux, if I miss to include a header, it fails to build.
> > In U-Boot, it does not.
> >
> > Personally, I like to align with Linux policy,
> > but if you are not comfortable with this patch,
> > please feel free to ignore it.
>
> I really don't understand the point of warnings if we are just going
> to turn them into errors.
>
> How about adding an option to tell U-Boot to use -Werror, etc.? Then
> those that what it can enable it.


OK.  We can do it with


make KCFLAGS=-Werror




-- 
Best Regards
Masahiro Yamada


Re: TPM: remove duplicate definitions

2020-05-10 Thread Simon Glass
Hi Patrick,

On Wed, 6 May 2020 at 16:58, Patrick Wildt  wrote:
>
> With the recent change to tpm-v2.h, some enums are now defined twice
> and tpm2_tis_spi.c fails to build.  Unfortunately I fear removing
> the defines from tpm_tis.h, like in this diff, will break the TPMv1
> drivers tpm_tis_infineon.c and pm_tis_st33zp24_i2c.c, which depend
> on those defines.  Maybe they should be added to tpm-v1.h as well?
> Or maybe they should be moved from tpm-v2.h to tpm_tis.h?  I have
> no solution.
>
> Signed-off-by: Patrick Wildt 

There is a tpm_common.h so they could go in there perhaps.

Regards,
Simon


Re: USB keyboard with XHCI on x86?

2020-05-10 Thread Simon Glass
Hi,

On Thu, 7 May 2020 at 21:02, Bin Meng  wrote:
>
> Hi Simon,
>
> On Fri, May 8, 2020 at 10:38 AM Simon Glass  wrote:
> >
> > Hi Bin,
> >
> > Does this work for you? It doesn't work for me in interrupt mode, only
> > in control-endpoint mode. Is that expected?
> >
> > Also I notice that some broken devices don't work or cause a crash.
> >
>
> I don't have a board to test xHCI right now, but from the commit
> message I wrote the interrupt mode should be working.
>
> commit 1897d60130976ece389d5875187b78ba0d41428f
> Author: Bin Meng 
> Date:   Mon Sep 18 06:40:41 2017 -0700
>
> usb: xhci: Add interrupt transfer support
>
> xHCI uses normal TRBs for both bulk and interrupt. This adds the
> missing interrupt transfer support to xHCI so that devices like
> USB keyboard that uses interrupt transfer can work.
>
> Signed-off-by: Bin Meng 

Yes it does work, but it seems a bit fragile. E.g. if I unplug the
keyboard I get:

=> WARN halted endpoint, queueing URB anyway.
Unexpected XHCI event TRB, skipping... (78832cd0  1300 01008400)
BUG at 
/scratch/sglass/cosarm/src/third_party/u-boot/files/drivers/usb/host/xhci-ring.c:496/abort_td()!
BUG!
resetting ...


Marek, I don't have EHCI so far as I know. This is Intel Apollo Lake.

Regards,
Simon


Re: [PATCH v2 2/2] arch: x86: apl: Use devicetree for FSP configuration

2020-05-10 Thread Simon Glass
Hi Bernhard,

On Sun, 10 May 2020 at 14:44, Simon Glass  wrote:
>
> On Thu, 7 May 2020 at 02:00, Bernhard Messerklinger
>  wrote:
> >
> > A the moment the FSP configuration is a mix of hard coded values and
> > devicetree properties.
> > This patch makes FSP-M and FSP-S full configurable from devicetree by
> > adding binding properties for all FSP parameters.
> > Co-developed-by: Wolfgang Wallner 
> > Signed-off-by: Wolfgang Wallner 
> > Signed-off-by: Bernhard Messerklinger 
> > 
> >
> > ---
> >
> > Changes in v2:
> > Integration of the review comments
> > Fix swizzle size bug
> >
> >  arch/x86/cpu/apollolake/Makefile  |1 +
> >  arch/x86/cpu/apollolake/fsp_bindings.c| 1803 +
> >  arch/x86/cpu/apollolake/fsp_m.c   |  169 +-
> >  arch/x86/cpu/apollolake/fsp_s.c   |  386 +---
> >  arch/x86/dts/chromebook_coral.dts |   72 +-
> >  .../asm/arch-apollolake/fsp/fsp_m_upd.h   |  168 ++
> >  .../asm/arch-apollolake/fsp/fsp_s_upd.h   |  202 ++
> >  .../asm/arch-apollolake/fsp_bindings.h|  110 +
> >  .../fsp/fsp2/apollolake/fsp-m.txt |  320 +++
> >  .../fsp/fsp2/apollolake/fsp-s.txt |  483 +
> >  10 files changed, 3163 insertions(+), 551 deletions(-)
> >  create mode 100644 arch/x86/cpu/apollolake/fsp_bindings.c
> >  create mode 100644 arch/x86/include/asm/arch-apollolake/fsp_bindings.h
> >  create mode 100644 doc/device-tree-bindings/fsp/fsp2/apollolake/fsp-m.txt
> >  create mode 100644 doc/device-tree-bindings/fsp/fsp2/apollolake/fsp-s.txt
> >
>
> Reviewed-by: Simon Glass 
> Tested on chromebook_coral:
> Tested-by: Simon Glass 

I notice that dual rank support is not enabled but I think it should
be. Sometimes I see a hang after FSP-S init, and putting that back in
seems to stop it happening.

One more thing - can I suggest splitting this into the conversion for
M and then for S, two separate patches?

I pushed my tree to u-boot-dm/try-fsp

Regards,
Simon


Re: [PATCH 13/18] common: Drop asm/ptrace.h from common header

2020-05-10 Thread Simon Glass
Hi Michal,

On Tue, 7 Apr 2020 at 00:50, Michal Simek  wrote:
>
> On 07. 04. 20 4:40, Simon Glass wrote:
> > Move this uncommon header out of the common header.
> >
> > Signed-off-by: Simon Glass 
> > ---
> >
> >  arch/arm/cpu/arm1136/mx35/timer.c   | 1 +
> >  arch/arm/cpu/arm926ejs/mx25/timer.c | 1 +
> >  arch/arm/cpu/arm926ejs/mx27/timer.c | 1 +
> >  arch/arm/cpu/arm926ejs/spear/spl.c  | 1 +
> >  arch/arm/cpu/arm926ejs/spear/timer.c| 1 +
> >  arch/arm/cpu/armv8/fsl-layerscape/cpu.c | 1 +
> >  arch/arm/cpu/armv8/sec_firmware.c   | 1 +
> >  arch/arm/lib/interrupts.c   | 1 +
> >  arch/arm/lib/interrupts_64.c| 1 +
> >  arch/arm/mach-imx/sip.c | 1 +
> >  arch/arm/mach-meson/board-common.c  | 1 +
> >  arch/arm/mach-meson/sm.c| 1 +
> >  arch/arm/mach-mvebu/arm64-common.c  | 1 +
> >  arch/m68k/lib/interrupts.c  | 1 +
> >  arch/m68k/lib/traps.c   | 1 +
> >  arch/mips/lib/traps.c   | 1 +
> >  arch/powerpc/cpu/mpc83xx/interrupts.c   | 1 +
> >  arch/powerpc/cpu/mpc83xx/traps.c| 1 +
> >  arch/powerpc/cpu/mpc85xx/interrupts.c   | 1 +
> >  arch/powerpc/cpu/mpc85xx/traps.c| 1 +
> >  arch/powerpc/cpu/mpc86xx/interrupts.c   | 1 +
> >  arch/powerpc/cpu/mpc86xx/traps.c| 1 +
> >  arch/powerpc/cpu/mpc8xx/interrupts.c| 1 +
> >  arch/powerpc/cpu/mpc8xx/traps.c | 1 +
> >  arch/powerpc/lib/interrupts.c   | 1 +
> >  arch/powerpc/lib/kgdb.c | 1 +
> >  arch/x86/cpu/i386/interrupt.c   | 1 +
> >  arch/xtensa/cpu/exceptions.c| 1 +
> >  board/cavium/thunderx/atf.c | 1 +
> >  board/xilinx/zynqmp/zynqmp.c| 1 +
> >  cmd/bedbug.c| 1 +
> >  common/bedbug.c | 1 +
> >  common/kgdb.c   | 1 +
> >  common/kgdb_stubs.c | 1 +
> >  drivers/clk/clk_versal.c| 1 +
> >  drivers/firmware/firmware-zynqmp.c  | 1 +
> >  drivers/timer/mpc83xx_timer.c   | 1 +
> >  include/common.h| 1 -
> >  38 files changed, 37 insertions(+), 1 deletion(-)
> >

[...]

> > diff --git a/board/xilinx/zynqmp/zynqmp.c b/board/xilinx/zynqmp/zynqmp.c
> > index 76758237c00..4f8a0453f46 100644
> > --- a/board/xilinx/zynqmp/zynqmp.c
> > +++ b/board/xilinx/zynqmp/zynqmp.c
> > @@ -22,6 +22,7 @@
> >  #include 
> >  #include 
> >  #include 
> > +#include 
>
> I did a grep and I can't see ptrace macros used anywhere in the code.
> I see show_regs used by architecture and common code but not by boards.
> That's why at least for zynqmp please remove this header because nothing
> is used from it.

For example: xilinx_zynqmp_virt

ilinx_zynqmp_virt
+WARNING: BL31 file bl31.bin NOT found, U-Boot will run in EL3
+board/xilinx/zynqmp/zynqmp.c: In function 'chip_id':
+board/xilinx/zynqmp/zynqmp.c:198:17: error: storage size of 'regs' isn't known
+  struct pt_regs regs;
+ ^~~~
+make[2]: *** [board/xilinx/zynqmp/zynqmp.o] Error 1
+make[1]: *** [board/xilinx/zynqmp] Error 2
+make: *** [sub-make] Error 2



+WARNING: BL31 file bl31.bin NOT found, U-Boot will run in EL3
+drivers/firmware/firmware-zynqmp.c: In function 'xilinx_pm_request':
+drivers/firmware/firmware-zynqmp.c:159:17: error: storage size of
'regs' isn't known
+  struct pt_regs regs;
+ ^~~~
+ }
+ ^
+make[3]: *** [drivers/firmware/firmware-zynqmp.o] Error 1
+make[2]: *** [drivers/firmware] Error 2
+make[1]: *** [drivers] Error 2
+make: *** [sub-make] Error 2
w+drivers/firmware/firmware-zynqmp.c:159:17: warning: unused variable
'regs' [-Wunused-variable]
w+drivers/firmware/firmware-zynqmp.c:181:1: warning: control reaches
end of non-void function [-Wreturn-type]

Regards,
Simon


Re: efi_loader: tighten PE verification code?

2020-05-10 Thread Patrick Wildt
On Sun, May 10, 2020 at 02:36:51PM -0600, Simon Glass wrote:
> Hi Patrick,
> 
> On Fri, 8 May 2020 at 12:56, Patrick Wildt  wrote:
> >
> > Hi,
> >
> > even though this mail has a diff, it's not supposed to be a patch.  I
> > have started adjusting my fuzzer to the upstreamed EFI Secure Boot code
> > and I hit a few issues right away.  Two of those I have already sent and
> > were reviewed.  I have seen more, but since I needed to reschedule some
> > things I couldn't continue and unfortunately have to postpone fuzzing
> > the code.  I can assure you if someone starts fuzzing that code, we will
> > find a few more bugs.
> >
> > Especially since this is "Secure Boot", this part should definitely be
> > air tight.
> >
> > One thing I saw is that the virt_size is smaller then the memcpy below
> > at the "Copy PE headers" line then actually copies.
> >
> > Another thing I saw is SizeOfRawData can be bigger then the VirtualSize,
> > and PointerToRawData + SizeOfRawData bigger then the allocated size.
> >
> > I'm not sure if this is the right place to do the checks.  Maybe they
> > need to be at the place where we are adding the image regions.  I guess
> > the fields should be properly verified in view of "does it make sense?".
> >
> > Also I'm questioning whether it's a good idea to continue parsing the
> > file if it's already clear that the signature can't be verified against
> > the "db".  I can understand that you'd like to finish loading the file
> > into an object, and some other instance decides whether or not it's fine
> > to start that image, but you also open yourself to issues when you're
> > parsing a file that obviously is against the current security policy.
> >
> > If you keep on parsing a file that obviously (because tested against the
> > "db") cannot be allowed to boot anyway, the checks for the headers need
> > to be even tighter.
> >
> > I'd be unhappy to see U-Boot CVEs come up regarding PE parsing issues.
> >
> > Best regards,
> > Patrick
> 
> Can I suggest adding some more unit tests?
> 
> Regards,
> Simon

Hi,

It's a good suggestion.  Now it only needs someone with enough time to
actually do it. :-)  I'll come back to this in a few weeks when my
current project becomes quieter.  I wish I could right now.

Regards,
Patrick


Re: [PATCH v2 2/2] arch: x86: apl: Use devicetree for FSP configuration

2020-05-10 Thread Simon Glass
On Thu, 7 May 2020 at 02:00, Bernhard Messerklinger
 wrote:
>
> A the moment the FSP configuration is a mix of hard coded values and
> devicetree properties.
> This patch makes FSP-M and FSP-S full configurable from devicetree by
> adding binding properties for all FSP parameters.
> Co-developed-by: Wolfgang Wallner 
> Signed-off-by: Wolfgang Wallner 
> Signed-off-by: Bernhard Messerklinger 
> 
>
> ---
>
> Changes in v2:
> Integration of the review comments
> Fix swizzle size bug
>
>  arch/x86/cpu/apollolake/Makefile  |1 +
>  arch/x86/cpu/apollolake/fsp_bindings.c| 1803 +
>  arch/x86/cpu/apollolake/fsp_m.c   |  169 +-
>  arch/x86/cpu/apollolake/fsp_s.c   |  386 +---
>  arch/x86/dts/chromebook_coral.dts |   72 +-
>  .../asm/arch-apollolake/fsp/fsp_m_upd.h   |  168 ++
>  .../asm/arch-apollolake/fsp/fsp_s_upd.h   |  202 ++
>  .../asm/arch-apollolake/fsp_bindings.h|  110 +
>  .../fsp/fsp2/apollolake/fsp-m.txt |  320 +++
>  .../fsp/fsp2/apollolake/fsp-s.txt |  483 +
>  10 files changed, 3163 insertions(+), 551 deletions(-)
>  create mode 100644 arch/x86/cpu/apollolake/fsp_bindings.c
>  create mode 100644 arch/x86/include/asm/arch-apollolake/fsp_bindings.h
>  create mode 100644 doc/device-tree-bindings/fsp/fsp2/apollolake/fsp-m.txt
>  create mode 100644 doc/device-tree-bindings/fsp/fsp2/apollolake/fsp-s.txt
>

Reviewed-by: Simon Glass 
Tested on chromebook_coral:
Tested-by: Simon Glass 


Re: [PATCH v2 1/2] arch: x86: apl: Only load VBT if CONFIG_HAVE_VBT is enabled

2020-05-10 Thread Simon Glass
On Thu, 7 May 2020 at 02:00, Bernhard Messerklinger
 wrote:
>
> Only load VBT if it's present in the u-boot.rom.
>
> Signed-off-by: Bernhard Messerklinger 
> 
> ---
>
> Changes in v2: None
>
>  arch/x86/cpu/apollolake/fsp_s.c | 46 ++---
>  1 file changed, 25 insertions(+), 21 deletions(-)
>

Reviewed-by: Simon Glass 
Tested on chromebook_coral:
Tested-by: Simon Glass 


Re: [PATCH] kbuild: add -Werror=implicit-function-declaration

2020-05-10 Thread Simon Glass
Hi Masahiro,

On Sat, 9 May 2020 at 05:00, Masahiro Yamada  wrote:
>
> On Sat, May 9, 2020 at 3:16 AM Tom Rini  wrote:
> >
> > On Thu, May 07, 2020 at 09:16:40PM -0600, Simon Glass wrote:
> > > Hi Masahiro,
> > >
> > > On Thu, 7 May 2020 at 19:54, Masahiro Yamada  wrote:
> > > >
> > > > On Fri, May 8, 2020 at 10:39 AM Simon Glass  wrote:
> > > > >
> > > > > Hi Masahiro,
> > > > >
> > > > > On Thu, 7 May 2020 at 06:21, Masahiro Yamada
> > > > >  wrote:
> > > > > >
> > > > > > Add -Werror=implicit-function-declaration as Linux does.
> > > > > >
> > > > > > If you do not check the prototype, it may go wrong run-time.
> > > > > > It is better to break the build, and require to include correct
> > > > > > headers.
> > > > > >
> > > > > > Signed-off-by: Masahiro Yamada 
> > > > > > ---
> > > > > >
> > > > > >  Makefile | 2 +-
> > > > > >  1 file changed, 1 insertion(+), 1 deletion(-)
> > > > >
> > > > > NAK
> > > > >
> > > > > We already get a warning in this situation. This makes it painful for
> > > > > development since things that should be warnings end up being errors.
> > > > > So your build fails when really it should work well enough to do a
> > > > > test run with your new code. I don't think it has any benefit on code
> > > > > quality since we already detect warnings in gitlab, etc.
> > > > >
> > > > > U-Boot is set up so that warnings are reported and are easy to spot if
> > > > > you use 'make -s' (i.e. not buried in the output).
> > > > >
> > > > > Regards,
> > > > > Simon
> > > >
> > > >
> > > >
> > > > Linux added this flag in 2007.
> > > >
> > > > The intention seems to break the build earlier
> > > > when a non-existing function is used.
> > > >
> > > > I have not seen compliant about this flag in Linux.
> > > > What does it make different for U-Boot ?
> > >
> > > Well that commit message is quite misleading. The author is presumably
> > > ignoring the warnings that come out in the compile phase. For me they
> > > come up loud and clear. I don't know why it takes half an hour to get
> > > to the link stage. My average incremental build time is under 4
> > > seconds including the link.
> > >
> > > Finally, the warning does not tell you anything about whether the
> > > function doesn't exist. It just tells you you have left out a header
> > > file.
> > >
> > > I know how much of a pain this is, because coreboot does this. It does
> > > it partly because there is so much build output that the warnings are
> > > invisible unless they actually halt the build. Even then you have to
> > > search for what went wrong.
> >
> > I'm not immediately sure of the right answer here.  Part of the problem
> > is that even with 'make -s' U-Boot can be horribly noisy due to device
> > tree warnings.  I assume Yamada-san ran in to a problem and was
> > expecting the build to have failed but instead was chasing down a
> > run-time debug until finding this.
>
>
> I did not run into a problem.
>
> When I was replacing  with some lighter headers,
> I missed some warnings ( but I noticed them after all).
>
> In Linux, if I miss to include a header, it fails to build.
> In U-Boot, it does not.
>
> Personally, I like to align with Linux policy,
> but if you are not comfortable with this patch,
> please feel free to ignore it.

I really don't understand the point of warnings if we are just going
to turn them into errors.

How about adding an option to tell U-Boot to use -Werror, etc.? Then
those that what it can enable it.

Regards,
Simon


Re: [PATCH 07/36] bdinfo: microblaze: Use the generic bd command

2020-05-10 Thread Simon Glass
Hi Daniel,

On Wed, 6 May 2020 at 09:56, Daniel Schwierzeck
 wrote:
>
>
>
> Am 05.05.20 um 01:17 schrieb Simon Glass:
> > Microblaze prints out ethernet and FDT information. This is useful to
> > most archs, so move it into the generic code and move microblaze over to
> > use it.
> >
> > Signed-off-by: Simon Glass 
> > ---
> >
> >  cmd/bdinfo.c | 30 +++---
> >  1 file changed, 7 insertions(+), 23 deletions(-)
> >
> > diff --git a/cmd/bdinfo.c b/cmd/bdinfo.c
> > index 405a915861..dc5a09f8ce 100644
> > --- a/cmd/bdinfo.c
> > +++ b/cmd/bdinfo.c
> > @@ -224,29 +224,7 @@ int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, 
> > char * const argv[])
> >
> >  #elif defined(CONFIG_MICROBLAZE)
> >
> > -int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
> > -{
> > - bd_t *bd = gd->bd;
> > -
> > - print_bi_dram(bd);
> > - print_bi_flash(bd);
> > -#if defined(CONFIG_SYS_SRAM_BASE)
> > - print_num("sram start ",(ulong)bd->bi_sramstart);
> > - print_num("sram size  ",(ulong)bd->bi_sramsize);
> > -#endif
> > -#if defined(CONFIG_CMD_NET) && !defined(CONFIG_DM_ETH)
> > - print_eths();
> > -#endif
> > - print_baudrate();
> > - print_num("relocaddr", gd->relocaddr);
> > - print_num("reloc off", gd->reloc_off);
> > - print_num("fdt_blob", (ulong)gd->fdt_blob);
> > - print_num("new_fdt", (ulong)gd->new_fdt);
> > - print_num("fdt_size", (ulong)gd->fdt_size);
> > - print_cpu_word_size();
> > -
> > - return 0;
> > -}
> > +#define USE_GENERIC
> >
> >  #elif defined(CONFIG_M68K)
> >
> > @@ -463,6 +441,12 @@ int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, 
> > char *const argv[])
> >   print_num("relocaddr", gd->relocaddr);
> >   print_num("reloc off", gd->reloc_off);
> >   print_cpu_word_size();
> > +#if defined(CONFIG_CMD_NET) && !defined(CONFIG_DM_ETH)
> > + print_eths();
> > +#endif
> > + print_num("fdt_blob", (ulong)gd->fdt_blob);
> > + print_num("new_fdt", (ulong)gd->new_fdt);
> > + print_num("fdt_size", (ulong)gd->fdt_size);
>
> shouldn't that be guarded with CONFIG_IS_ENABLED(OF_LIBFDT) ?

No it doesn't need to be. The commands are not available in SPL, and
U-Boot proper is supposed to use device tree for all boards.

Regards,
Simon


Re: efi_loader: tighten PE verification code?

2020-05-10 Thread Simon Glass
Hi Patrick,

On Fri, 8 May 2020 at 12:56, Patrick Wildt  wrote:
>
> Hi,
>
> even though this mail has a diff, it's not supposed to be a patch.  I
> have started adjusting my fuzzer to the upstreamed EFI Secure Boot code
> and I hit a few issues right away.  Two of those I have already sent and
> were reviewed.  I have seen more, but since I needed to reschedule some
> things I couldn't continue and unfortunately have to postpone fuzzing
> the code.  I can assure you if someone starts fuzzing that code, we will
> find a few more bugs.
>
> Especially since this is "Secure Boot", this part should definitely be
> air tight.
>
> One thing I saw is that the virt_size is smaller then the memcpy below
> at the "Copy PE headers" line then actually copies.
>
> Another thing I saw is SizeOfRawData can be bigger then the VirtualSize,
> and PointerToRawData + SizeOfRawData bigger then the allocated size.
>
> I'm not sure if this is the right place to do the checks.  Maybe they
> need to be at the place where we are adding the image regions.  I guess
> the fields should be properly verified in view of "does it make sense?".
>
> Also I'm questioning whether it's a good idea to continue parsing the
> file if it's already clear that the signature can't be verified against
> the "db".  I can understand that you'd like to finish loading the file
> into an object, and some other instance decides whether or not it's fine
> to start that image, but you also open yourself to issues when you're
> parsing a file that obviously is against the current security policy.
>
> If you keep on parsing a file that obviously (because tested against the
> "db") cannot be allowed to boot anyway, the checks for the headers need
> to be even tighter.
>
> I'd be unhappy to see U-Boot CVEs come up regarding PE parsing issues.
>
> Best regards,
> Patrick

Can I suggest adding some more unit tests?

Regards,
Simon


Re: [PATCH 1/1] common/board_f: avoid -Wtype-limits warning

2020-05-10 Thread Simon Glass
On Sat, 9 May 2020 at 13:27, Heinrich Schuchardt  wrote:
>
> CONFIG_SYS_SDRAM_BASE maybe zero. Avoid a build warning when compiling with
> -Wtype-limits.
>
> Signed-off-by: Heinrich Schuchardt 
> ---
>  common/board_f.c | 2 +-

Reviewed-by: Simon Glass 


[PATCH v2 34/35] dm: acpi: Enhance acpi_get_name()

2020-05-10 Thread Simon Glass
For many device types it is possible to figure out the name just by
looking at its uclass or parent. Add a function to handle this, since it
allows us to cover the vast majority of cases automatically.

However it is sometimes impossible to figure out an ACPI name for a device
just by looking at its uclass. For example a touch device may have a
vendor-specific name. Add a new "acpi,name" property to allow a custom
name to be created.

With this new feature we can drop the get_name() methods in the sandbox
I2C and SPI drivers. They were only added for testing purposes. Update the
tests to use the new values.

Signed-off-by: Simon Glass 
---

Changes in v2: None
Changes in v1:
- Use acpi,ddn instead of acpi,desc
- Rename to acpi_device_infer_name()
- Update newly created sandbox tests

 arch/sandbox/dts/test.dts   |   1 +
 doc/device-tree-bindings/device.txt |  13 
 drivers/core/acpi.c |  13 +++-
 drivers/i2c/sandbox_i2c.c   |  10 ---
 drivers/spi/sandbox_spi.c   |  10 ---
 include/acpi/acpi_device.h  |  15 
 lib/acpi/acpi_device.c  | 106 
 test/dm/acpi.c  |  42 ++-
 test/dm/acpigen.c   |   4 +-
 9 files changed, 189 insertions(+), 25 deletions(-)

diff --git a/arch/sandbox/dts/test.dts b/arch/sandbox/dts/test.dts
index aab3fb7471..750381bcac 100644
--- a/arch/sandbox/dts/test.dts
+++ b/arch/sandbox/dts/test.dts
@@ -111,6 +111,7 @@
int-array = <5678 9123 4567>;
str-value = "test string";
interrupts-extended = < 3 0>;
+   acpi,name = "GHIJ";
};
 
junk {
diff --git a/doc/device-tree-bindings/device.txt 
b/doc/device-tree-bindings/device.txt
index 27bd3978d9..7140339623 100644
--- a/doc/device-tree-bindings/device.txt
+++ b/doc/device-tree-bindings/device.txt
@@ -17,6 +17,8 @@ the acpi,compatible property.
 System) Device Name)
  - acpi,hid : Contains the string to use as the HID (Hardware ID)
 identifier _HID
+ - acpi,name : Provides the ACPI name for a device, which is a string 
consisting
+   of four alphanumeric character (upper case)
  - acpi,uid : _UID value for device
  - linux,probed : Tells U-Boot to add 'linux,probed' to the ACPI tables so that
 Linux will only load the driver if the device can be detected (e.g. on I2C
@@ -34,3 +36,14 @@ elan_touchscreen: elan-touchscreen@10 {
interrupts-extended = <_gpe GPIO_21_IRQ IRQ_TYPE_EDGE_FALLING>;
linux,probed;
 };
+
+pcie-a0@14,0 {
+   reg = <0xa000 0 0 0 0>;
+   acpi,name = "RP01";
+   wifi: wifi {
+   compatible = "intel,generic-wifi";
+   acpi,ddn = "Intel WiFi";
+   acpi,name = "WF00";
+   interrupts-extended = <_gpe 0x3c 0>;
+   };
+};
diff --git a/drivers/core/acpi.c b/drivers/core/acpi.c
index ebe9fde68e..ec70db3951 100644
--- a/drivers/core/acpi.c
+++ b/drivers/core/acpi.c
@@ -9,9 +9,10 @@
 #define LOG_CATEOGRY   LOGC_ACPI
 
 #include 
-#include 
 #include 
 #include 
+#include 
+#include 
 #include 
 #include 
 #include 
@@ -65,12 +66,20 @@ int acpi_copy_name(char *out_name, const char *name)
 int acpi_get_name(const struct udevice *dev, char *out_name)
 {
struct acpi_ops *aops;
+   const char *name;
+   int ret;
 
aops = device_get_acpi_ops(dev);
if (aops && aops->get_name)
return aops->get_name(dev, out_name);
+   name = dev_read_string(dev, "acpi,name");
+   if (name)
+   return acpi_copy_name(out_name, name);
+   ret = acpi_device_infer_name(dev, out_name);
+   if (ret)
+   return log_msg_ret("dev", ret);
 
-   return -ENOSYS;
+   return 0;
 }
 
 /**
diff --git a/drivers/i2c/sandbox_i2c.c b/drivers/i2c/sandbox_i2c.c
index 125026da90..57b1c60fde 100644
--- a/drivers/i2c/sandbox_i2c.c
+++ b/drivers/i2c/sandbox_i2c.c
@@ -84,15 +84,6 @@ static int sandbox_i2c_xfer(struct udevice *bus, struct 
i2c_msg *msg,
return ops->xfer(emul, msg, nmsgs);
 }
 
-static int sandbox_i2c_get_name(const struct udevice *dev, char *out_name)
-{
-   return acpi_copy_name(out_name, "SI2C");
-}
-
-struct acpi_ops sandbox_i2c_acpi_ops = {
-   .get_name   = sandbox_i2c_get_name,
-};
-
 static const struct dm_i2c_ops sandbox_i2c_ops = {
.xfer   = sandbox_i2c_xfer,
 };
@@ -108,5 +99,4 @@ U_BOOT_DRIVER(i2c_sandbox) = {
.of_match = sandbox_i2c_ids,
.ops= _i2c_ops,
.priv_auto_alloc_size = sizeof(struct sandbox_i2c_priv),
-   ACPI_OPS_PTR(_i2c_acpi_ops)
 };
diff --git a/drivers/spi/sandbox_spi.c b/drivers/spi/sandbox_spi.c
index 4264acc953..5aeb6bdf43 100644
--- a/drivers/spi/sandbox_spi.c
+++ b/drivers/spi/sandbox_spi.c
@@ -134,15 +134,6 @@ static int sandbox_spi_get_mmap(struct udevice *dev, ulong 
*map_basep,
return 0;
 }
 
-static int sandbox_spi_get_name(const struct udevice *dev, char *out_name)
-{

[PATCH v2 29/35] acpi: Support ordering SSDT data by device

2020-05-10 Thread Simon Glass
Add a /chosen property to control the order in which the data appears
in the SSDT. This allows matching up U-Boot's output from a dump of the
known-good data obtained from within Linux.

Signed-off-by: Simon Glass 
---

Changes in v2: None
Changes in v1:
- Generalise the ACPI function recursion with acpi_recurse_method()

 arch/sandbox/dts/test.dts   |  5 ++-
 doc/device-tree-bindings/chosen.txt |  9 +
 drivers/core/acpi.c | 62 +
 test/dm/acpi.c  | 15 ---
 4 files changed, 83 insertions(+), 8 deletions(-)

diff --git a/arch/sandbox/dts/test.dts b/arch/sandbox/dts/test.dts
index f6520a54e8..30eb749671 100644
--- a/arch/sandbox/dts/test.dts
+++ b/arch/sandbox/dts/test.dts
@@ -252,7 +252,7 @@
compatible = "denx,u-boot-devres-test";
};
 
-   acpi-test {
+   acpi_test1: acpi-test {
compatible = "denx,u-boot-acpi-test";
acpi-ssdt-test-data = "ab";
child {
@@ -260,7 +260,7 @@
};
};
 
-   acpi-test2 {
+   acpi_test2: acpi-test2 {
compatible = "denx,u-boot-acpi-test";
acpi-ssdt-test-data = "cd";
};
@@ -893,6 +893,7 @@
setting = "sunrise ohoka";
other-node = "/some-bus/c-test@5";
int-values = <0x1937 72993>;
+   u-boot,acpi-ssdt-order = <_test2 _test1>;
chosen-test {
compatible = "denx,u-boot-fdt-test";
reg = <9 1>;
diff --git a/doc/device-tree-bindings/chosen.txt 
b/doc/device-tree-bindings/chosen.txt
index 395c9501e3..d4dfc05847 100644
--- a/doc/device-tree-bindings/chosen.txt
+++ b/doc/device-tree-bindings/chosen.txt
@@ -134,3 +134,12 @@ Example
phandlepart = < 1>;
};
 };
+
+u-boot,acpi-ssdt-order
+--
+
+This provides the ordering to use when writing device data to the ACPI SSDT
+(Secondary System Descriptor Table). Each cell is a phandle pointer to a device
+node to add. The ACPI information is written in this order.
+
+If the ordering does not include all nodes, an error is generated.
diff --git a/drivers/core/acpi.c b/drivers/core/acpi.c
index fa16be8154..a0b598b66b 100644
--- a/drivers/core/acpi.c
+++ b/drivers/core/acpi.c
@@ -108,6 +108,63 @@ static int acpi_add_item(struct acpi_ctx *ctx, struct 
udevice *dev,
return 0;
 }
 
+struct acpi_item *find_item(const char *devname)
+{
+   int i;
+
+   for (i = 0; i < item_count; i++) {
+   struct acpi_item *item = _item[i];
+
+   if (!strcmp(devname, item->dev->name))
+   return item;
+   }
+
+   return NULL;
+}
+
+static int build_type(struct acpi_ctx *ctx, void *start, enum gen_type_t type)
+{
+   const u32 *order;
+   int size;
+   int count;
+   void *ptr;
+   void *end = ctx->current;
+
+   ptr = start;
+   order = ofnode_read_chosen_prop("u-boot,acpi-ssdt-order", );
+   if (!order) {
+   log_warning("Failed to find ordering, leaving as is\n");
+   return 0;
+   }
+
+   count = size / sizeof(u32);
+   while (count--) {
+   struct acpi_item *item;
+   const char *name;
+   ofnode node;
+
+   node = ofnode_get_by_phandle(fdt32_to_cpu(*order++));
+   name = ofnode_get_name(node);
+   item = find_item(name);
+   if (!item) {
+   log_err("Failed to find item '%s'\n", name);
+   return log_msg_ret("find", -ENOENT);
+   }
+   if (item->type == type) {
+   log_debug("   - add %s\n", item->dev->name);
+   memcpy(ptr, item->buf, item->size);
+   ptr += item->size;
+   }
+   }
+
+   if (ptr != end) {
+   log_warning("*** Missing bytes: ptr=%p, end=%p\n", ptr, end);
+   return -ENXIO;
+   }
+
+   return 0;
+}
+
 acpi_method acpi_get_method(struct udevice *dev, enum method_t method)
 {
struct acpi_ops *aops;
@@ -163,11 +220,16 @@ int acpi_recurse_method(struct acpi_ctx *ctx, struct 
udevice *parent,
 
 int acpi_fill_ssdt(struct acpi_ctx *ctx)
 {
+   void *start = ctx->current;
int ret;
 
log_debug("Writing SSDT tables\n");
+   item_count = 0;
ret = acpi_recurse_method(ctx, dm_root(), METHOD_FILL_SSDT, TYPE_SSDT);
log_debug("Writing SSDT finished, err=%d\n", ret);
+   ret = build_type(ctx, start, TYPE_SSDT);
+   if (ret)
+   return log_msg_ret("build", ret);
 
return ret;
 }
diff --git a/test/dm/acpi.c b/test/dm/acpi.c
index d1bd108223..8df128706d 100644
--- a/test/dm/acpi.c
+++ b/test/dm/acpi.c
@@ -425,13 +425,16 @@ static int dm_test_acpi_fill_ssdt(struct unit_test_state 
*uts)
buf[4] = 'z';   /* sentinel */
 

[PATCH v2 32/35] x86: Allow devices to write to DSDT

2020-05-10 Thread Simon Glass
Call the new core function to inject ASL programmatically into the DSDT.
This is made up of fragments generated by devices that have the
inject_dsdt() method. The normal, compiled ASL file is added after this.

Signed-off-by: Simon Glass 
---

Changes in v2: None
Changes in v1: None

 arch/x86/lib/acpi_table.c | 15 ++-
 1 file changed, 14 insertions(+), 1 deletion(-)

diff --git a/arch/x86/lib/acpi_table.c b/arch/x86/lib/acpi_table.c
index 4658d88351..a7ec6d2b15 100644
--- a/arch/x86/lib/acpi_table.c
+++ b/arch/x86/lib/acpi_table.c
@@ -411,7 +411,20 @@ ulong write_acpi_tables(ulong start_addr)
memcpy(ctx->current,
   (char *) + sizeof(struct acpi_table_header),
   dsdt->length - sizeof(struct acpi_table_header));
-   acpi_inc_align(ctx, dsdt->length - sizeof(struct acpi_table_header));
+
+   if (dsdt->length >= sizeof(struct acpi_table_header)) {
+   acpi_inject_dsdt(ctx);
+   memcpy(ctx->current,
+  (char *)AmlCode + sizeof(struct acpi_table_header),
+  dsdt->length - sizeof(struct acpi_table_header));
+   acpi_inc(ctx, dsdt->length - sizeof(struct acpi_table_header));
+
+   /* (Re)calculate length and checksum. */
+   dsdt->length = ctx->current - (void *)dsdt;
+   dsdt->checksum = 0;
+   dsdt->checksum = table_compute_checksum(dsdt, dsdt->length);
+   }
+   acpi_align(ctx);
 
/* Pack GNVS into the ACPI table area */
for (i = 0; i < dsdt->length; i++) {
-- 
2.26.2.645.ge9eca65c58-goog



[PATCH v2 31/35] acpi: Add support for DSDT generation

2020-05-10 Thread Simon Glass
Some devices need to inject extra code into the Differentiated System
Descriptor Table (DSDT). Add a method to handle this.

Signed-off-by: Simon Glass 
---

Changes in v2: None
Changes in v1:
- Generalise the ACPI function recursion with acpi_recurse_method()

 arch/sandbox/dts/test.dts |  2 ++
 drivers/core/acpi.c   | 25 +-
 include/dm/acpi.h | 23 
 test/dm/acpi.c| 44 +++
 4 files changed, 93 insertions(+), 1 deletion(-)

diff --git a/arch/sandbox/dts/test.dts b/arch/sandbox/dts/test.dts
index 30eb749671..aab3fb7471 100644
--- a/arch/sandbox/dts/test.dts
+++ b/arch/sandbox/dts/test.dts
@@ -255,6 +255,7 @@
acpi_test1: acpi-test {
compatible = "denx,u-boot-acpi-test";
acpi-ssdt-test-data = "ab";
+   acpi-dsdt-test-data = "hi";
child {
compatible = "denx,u-boot-acpi-test";
};
@@ -263,6 +264,7 @@
acpi_test2: acpi-test2 {
compatible = "denx,u-boot-acpi-test";
acpi-ssdt-test-data = "cd";
+   acpi-dsdt-test-data = "jk";
};
 
clocks {
diff --git a/drivers/core/acpi.c b/drivers/core/acpi.c
index a0b598b66b..ebe9fde68e 100644
--- a/drivers/core/acpi.c
+++ b/drivers/core/acpi.c
@@ -22,12 +22,14 @@
 enum gen_type_t {
TYPE_NONE,
TYPE_SSDT,
+   TYPE_DSDT,
 };
 
 /* Type of method to call */
 enum method_t {
METHOD_WRITE_TABLES,
METHOD_FILL_SSDT,
+   METHOD_INJECT_DSDT,
 };
 
 /* Prototype for all methods */
@@ -131,7 +133,9 @@ static int build_type(struct acpi_ctx *ctx, void *start, 
enum gen_type_t type)
void *end = ctx->current;
 
ptr = start;
-   order = ofnode_read_chosen_prop("u-boot,acpi-ssdt-order", );
+   order = ofnode_read_chosen_prop(type == TYPE_DSDT ?
+   "u-boot,acpi-dsdt-order" :
+   "u-boot,acpi-ssdt-order", );
if (!order) {
log_warning("Failed to find ordering, leaving as is\n");
return 0;
@@ -176,6 +180,8 @@ acpi_method acpi_get_method(struct udevice *dev, enum 
method_t method)
return aops->write_tables;
case METHOD_FILL_SSDT:
return aops->fill_ssdt;
+   case METHOD_INJECT_DSDT:
+   return aops->inject_dsdt;
}
}
 
@@ -234,6 +240,23 @@ int acpi_fill_ssdt(struct acpi_ctx *ctx)
return ret;
 }
 
+int acpi_inject_dsdt(struct acpi_ctx *ctx)
+{
+   void *start = ctx->current;
+   int ret;
+
+   log_debug("Writing DSDT tables\n");
+   item_count = 0;
+   ret = acpi_recurse_method(ctx, dm_root(), METHOD_INJECT_DSDT,
+ TYPE_DSDT);
+   log_debug("Writing DSDT finished, err=%d\n", ret);
+   ret = build_type(ctx, start, TYPE_DSDT);
+   if (ret)
+   return log_msg_ret("build", ret);
+
+   return ret;
+}
+
 int acpi_write_dev_tables(struct acpi_ctx *ctx)
 {
int ret;
diff --git a/include/dm/acpi.h b/include/dm/acpi.h
index dac1635cd3..73bad2e763 100644
--- a/include/dm/acpi.h
+++ b/include/dm/acpi.h
@@ -85,6 +85,19 @@ struct acpi_ops {
 * @return 0 if OK, -ve on error
 */
int (*fill_ssdt)(const struct udevice *dev, struct acpi_ctx *ctx);
+
+   /**
+* inject_dsdt() - Generate DSDT code for a device
+*
+* This is called to create the DSDT code. THe method should write out
+* whatever ACPI code is needed by this device. It will end up in the
+* DSDT table.
+*
+* @dev: Device to write
+* @ctx: ACPI context to use
+* @return 0 if OK, -ve on error
+*/
+   int (*inject_dsdt)(const struct udevice *dev, struct acpi_ctx *ctx);
 };
 
 #define device_get_acpi_ops(dev)   ((dev)->driver->acpi_ops)
@@ -139,6 +152,16 @@ int acpi_write_dev_tables(struct acpi_ctx *ctx);
  */
 int acpi_fill_ssdt(struct acpi_ctx *ctx);
 
+/**
+ * acpi_inject_dsdt() - Generate ACPI tables for DSDT
+ *
+ * This is called to create the DSDT code for all devices.
+ *
+ * @ctx: ACPI context to use
+ * @return 0 if OK, -ve on error
+ */
+int acpi_inject_dsdt(struct acpi_ctx *ctx);
+
 #endif /* __ACPI__ */
 
 #endif
diff --git a/test/dm/acpi.c b/test/dm/acpi.c
index 8df128706d..3fe0a0c9ff 100644
--- a/test/dm/acpi.c
+++ b/test/dm/acpi.c
@@ -81,10 +81,24 @@ static int testacpi_fill_ssdt(const struct udevice *dev, 
struct acpi_ctx *ctx)
return 0;
 }
 
+static int testacpi_inject_dsdt(const struct udevice *dev, struct acpi_ctx 
*ctx)
+{
+   const char *data;
+
+   data = dev_read_string(dev, "acpi-dsdt-test-data");
+   if (data) {
+   while (*data)
+   acpigen_emit_byte(ctx, *data++);
+   }
+
+   return 0;
+}
+
 struct acpi_ops 

[PATCH v2 35/35] acpi: Add an acpi split command

2020-05-10 Thread Simon Glass
Add a command that shows the individual blocks of data generated by each
device. This can be helpful for debugging.

Signed-off-by: Simon Glass 
---

Changes in v2: None
Changes in v1: None

 cmd/acpi.c  | 15 +--
 drivers/core/acpi.c | 16 
 include/dm/acpi.h   | 10 ++
 test/dm/acpi.c  | 39 +++
 4 files changed, 78 insertions(+), 2 deletions(-)

diff --git a/cmd/acpi.c b/cmd/acpi.c
index e9a9161a91..3204b2ec43 100644
--- a/cmd/acpi.c
+++ b/cmd/acpi.c
@@ -153,6 +153,17 @@ static int do_acpi_list(struct cmd_tbl *cmdtp, int flag, 
int argc,
return 0;
 }
 
+static int do_acpi_items(struct cmd_tbl *cmdtp, int flag, int argc,
+char *const argv[])
+{
+   bool dump;
+
+   dump = argc >= 2 && !strcmp("-d", argv[1]);
+   acpi_dump_items(dump);
+
+   return 0;
+}
+
 static int do_acpi_dump(struct cmd_tbl *cmdtp, int flag, int argc,
char *const argv[])
 {
@@ -160,8 +171,6 @@ static int do_acpi_dump(struct cmd_tbl *cmdtp, int flag, 
int argc,
char sig[ACPI_NAME_LEN];
int ret;
 
-   if (argc < 2)
-   return CMD_RET_USAGE;
name = argv[1];
if (strlen(name) != ACPI_NAME_LEN) {
printf("Table name '%s' must be four characters\n", name);
@@ -179,8 +188,10 @@ static int do_acpi_dump(struct cmd_tbl *cmdtp, int flag, 
int argc,
 
 static char acpi_help_text[] =
"list - list ACPI tables\n"
+   "acpi items [-d]  - List/dump each piece of ACPI data from devices\n"
"acpi dump  - Dump ACPI table";
 
 U_BOOT_CMD_WITH_SUBCMDS(acpi, "ACPI tables", acpi_help_text,
U_BOOT_SUBCMD_MKENT(list, 1, 1, do_acpi_list),
+   U_BOOT_SUBCMD_MKENT(items, 2, 1, do_acpi_items),
U_BOOT_SUBCMD_MKENT(dump, 2, 1, do_acpi_dump));
diff --git a/drivers/core/acpi.c b/drivers/core/acpi.c
index ec70db3951..aa11001a8d 100644
--- a/drivers/core/acpi.c
+++ b/drivers/core/acpi.c
@@ -119,6 +119,22 @@ static int acpi_add_item(struct acpi_ctx *ctx, struct 
udevice *dev,
return 0;
 }
 
+void acpi_dump_items(bool dump_contents)
+{
+   int i;
+
+   for (i = 0; i < item_count; i++) {
+   struct acpi_item *item = _item[i];
+
+   printf("dev '%s', type %d, size %x\n", item->dev->name,
+  item->type, item->size);
+   if (dump_contents) {
+   print_buffer(0, item->buf, 1, item->size, 0);
+   printf("\n");
+   }
+   }
+}
+
 struct acpi_item *find_item(const char *devname)
 {
int i;
diff --git a/include/dm/acpi.h b/include/dm/acpi.h
index 73bad2e763..678391ccfc 100644
--- a/include/dm/acpi.h
+++ b/include/dm/acpi.h
@@ -162,6 +162,16 @@ int acpi_fill_ssdt(struct acpi_ctx *ctx);
  */
 int acpi_inject_dsdt(struct acpi_ctx *ctx);
 
+/**
+ * acpi_dump_items() - Dump out the collected ACPI items
+ *
+ * This lists the ACPI DSDT and SSDT items generated by the various U-Boot
+ * drivers.
+ *
+ * @dump_contents: true to dump the binary contents, false to just show the 
list
+ */
+void acpi_dump_items(bool dump_contents);
+
 #endif /* __ACPI__ */
 
 #endif
diff --git a/test/dm/acpi.c b/test/dm/acpi.c
index bd4c66c471..4952256ce1 100644
--- a/test/dm/acpi.c
+++ b/test/dm/acpi.c
@@ -525,3 +525,42 @@ static int dm_test_acpi_inject_dsdt(struct unit_test_state 
*uts)
return 0;
 }
 DM_TEST(dm_test_acpi_inject_dsdt, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
+
+/* Test 'acpi items' command */
+static int dm_test_acpi_cmd_items(struct unit_test_state *uts)
+{
+   struct acpi_ctx ctx;
+   void *buf;
+
+   buf = malloc(BUF_SIZE);
+   ut_assertnonnull(buf);
+
+   ctx.current = buf;
+   ut_assertok(acpi_fill_ssdt());
+   console_record_reset();
+   run_command("acpi items", 0);
+   ut_assert_nextline("dev 'acpi-test', type 1, size 2");
+   ut_assert_nextline("dev 'acpi-test2', type 1, size 2");
+   ut_assert_console_end();
+
+   ctx.current = buf;
+   ut_assertok(acpi_inject_dsdt());
+   console_record_reset();
+   run_command("acpi items", 0);
+   ut_assert_nextline("dev 'acpi-test', type 2, size 2");
+   ut_assert_nextline("dev 'acpi-test2', type 2, size 2");
+   ut_assert_console_end();
+
+   console_record_reset();
+   run_command("acpi items -d", 0);
+   ut_assert_nextline("dev 'acpi-test', type 2, size 2");
+   ut_assert_nextlines_are_dump(2);
+   ut_assert_nextline("%s", "");
+   ut_assert_nextline("dev 'acpi-test2', type 2, size 2");
+   ut_assert_nextlines_are_dump(2);
+   ut_assert_nextline("%s", "");
+   ut_assert_console_end();
+
+   return 0;
+}
+DM_TEST(dm_test_acpi_cmd_items, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
-- 
2.26.2.645.ge9eca65c58-goog



[PATCH v2 33/35] pci: Avoid a crash in device_is_on_pci_bus()

2020-05-10 Thread Simon Glass
This function cannot currently be called on the root node. Add a check
for this as well as a test.

Signed-off-by: Simon Glass 
---

Changes in v2: None
Changes in v1: None

 include/dm/device.h |  2 +-
 test/dm/pci.c   | 14 ++
 2 files changed, 15 insertions(+), 1 deletion(-)

diff --git a/include/dm/device.h b/include/dm/device.h
index 975eec5d0e..6a41ee5500 100644
--- a/include/dm/device.h
+++ b/include/dm/device.h
@@ -742,7 +742,7 @@ int dev_enable_by_path(const char *path);
  */
 static inline bool device_is_on_pci_bus(const struct udevice *dev)
 {
-   return device_get_uclass_id(dev->parent) == UCLASS_PCI;
+   return dev->parent && device_get_uclass_id(dev->parent) == UCLASS_PCI;
 }
 
 /**
diff --git a/test/dm/pci.c b/test/dm/pci.c
index fb93e4c78a..39e82b3699 100644
--- a/test/dm/pci.c
+++ b/test/dm/pci.c
@@ -339,3 +339,17 @@ static int dm_test_pci_addr_live(struct unit_test_state 
*uts)
 }
 DM_TEST(dm_test_pci_addr_live, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT |
DM_TESTF_LIVE_TREE);
+
+/* Test device_is_on_pci_bus() */
+static int dm_test_pci_on_bus(struct unit_test_state *uts)
+{
+   struct udevice *dev;
+
+   ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(0, 0x1f, 0), ));
+   ut_asserteq(true, device_is_on_pci_bus(dev));
+   ut_asserteq(false, device_is_on_pci_bus(dev_get_parent(dev)));
+   ut_asserteq(true, device_is_on_pci_bus(dev));
+
+   return 0;
+}
+DM_TEST(dm_test_pci_on_bus, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
-- 
2.26.2.645.ge9eca65c58-goog



[PATCH v2 28/35] acpi: Record the items added to SSDT

2020-05-10 Thread Simon Glass
It is useful to be able to control the order of data written to the SSDT
so that we can compare the output against known-good kernel dumps.

Add code to record each item that is added along with the device that
added it. That allows us to reorder things later if needed.

Signed-off-by: Simon Glass 
---

Changes in v2: None
Changes in v1:
- Generalise the ACPI function recursion with acpi_recurse_method()

 drivers/core/acpi.c | 83 ++---
 1 file changed, 79 insertions(+), 4 deletions(-)

diff --git a/drivers/core/acpi.c b/drivers/core/acpi.c
index 4497b5cb2f..fa16be8154 100644
--- a/drivers/core/acpi.c
+++ b/drivers/core/acpi.c
@@ -9,12 +9,21 @@
 #define LOG_CATEOGRY   LOGC_ACPI
 
 #include 
+#include 
 #include 
 #include 
 #include 
 #include 
 #include 
 
+#define MAX_ITEMS  100
+
+/* Type of table that we collected */
+enum gen_type_t {
+   TYPE_NONE,
+   TYPE_SSDT,
+};
+
 /* Type of method to call */
 enum method_t {
METHOD_WRITE_TABLES,
@@ -24,6 +33,25 @@ enum method_t {
 /* Prototype for all methods */
 typedef int (*acpi_method)(const struct udevice *dev, struct acpi_ctx *ctx);
 
+/**
+ * struct acpi_item - Holds info about ACPI data generated by a driver method
+ *
+ * @dev: Device that generated this data
+ * @type: Table type it refers to
+ * @buf: Buffer containing the data
+ * @size: Size of the data in bytes
+ */
+struct acpi_item {
+   struct udevice *dev;
+   enum gen_type_t type;
+   char *buf;
+   int size;
+};
+
+/* List of ACPI items collected */
+static struct acpi_item acpi_item[MAX_ITEMS];
+static int item_count;
+
 int acpi_copy_name(char *out_name, const char *name)
 {
strncpy(out_name, name, ACPI_NAME_LEN);
@@ -43,6 +71,43 @@ int acpi_get_name(const struct udevice *dev, char *out_name)
return -ENOSYS;
 }
 
+/**
+ * acpi_add_item() - Add a new item to the list of data collected
+ *
+ * @ctx: ACPI context
+ * @dev: Device that generated the data
+ * @type: Table type it refers to
+ * @start: The start of the data (the end is obtained from ctx->current)
+ * @return 0 if OK, -ENOSPC if too many items, -ENOMEM if out of memory
+ */
+static int acpi_add_item(struct acpi_ctx *ctx, struct udevice *dev,
+enum gen_type_t type, void *start)
+{
+   struct acpi_item *item;
+   void *end = ctx->current;
+
+   if (item_count == MAX_ITEMS) {
+   log_err("Too many items\n");
+   return log_msg_ret("mem", -ENOSPC);
+   }
+
+   item = _item[item_count];
+   item->dev = dev;
+   item->type = type;
+   item->size = end - start;
+   if (!item->size)
+   return 0;
+   item->buf = malloc(item->size);
+   if (!item->buf)
+   return log_msg_ret("mem", -ENOMEM);
+   memcpy(item->buf, start, item->size);
+   item_count++;
+   log_debug("* %s: Added type %d, %p, size %x\n", dev->name, type, start,
+ item->size);
+
+   return 0;
+}
+
 acpi_method acpi_get_method(struct udevice *dev, enum method_t method)
 {
struct acpi_ops *aops;
@@ -61,7 +126,7 @@ acpi_method acpi_get_method(struct udevice *dev, enum 
method_t method)
 }
 
 int acpi_recurse_method(struct acpi_ctx *ctx, struct udevice *parent,
-   enum method_t method)
+   enum method_t method, enum gen_type_t type)
 {
struct udevice *dev;
acpi_method func;
@@ -69,6 +134,8 @@ int acpi_recurse_method(struct acpi_ctx *ctx, struct udevice 
*parent,
 
func = acpi_get_method(parent, method);
if (func) {
+   void *start = ctx->current;
+
log_debug("\n");
log_debug("- %s %p\n", parent->name, func);
ret = device_ofdata_to_platdata(parent);
@@ -77,9 +144,16 @@ int acpi_recurse_method(struct acpi_ctx *ctx, struct 
udevice *parent,
ret = func(parent, ctx);
if (ret)
return log_msg_ret("func", ret);
+
+   /* Add the item to the internal list */
+   if (type != TYPE_NONE) {
+   ret = acpi_add_item(ctx, parent, type, start);
+   if (ret)
+   return log_msg_ret("add", ret);
+   }
}
device_foreach_child(dev, parent) {
-   ret = acpi_recurse_method(ctx, dev, method);
+   ret = acpi_recurse_method(ctx, dev, method, type);
if (ret)
return log_msg_ret("recurse", ret);
}
@@ -92,7 +166,7 @@ int acpi_fill_ssdt(struct acpi_ctx *ctx)
int ret;
 
log_debug("Writing SSDT tables\n");
-   ret = acpi_recurse_method(ctx, dm_root(), METHOD_FILL_SSDT);
+   ret = acpi_recurse_method(ctx, dm_root(), METHOD_FILL_SSDT, TYPE_SSDT);
log_debug("Writing SSDT finished, err=%d\n", ret);
 
return ret;
@@ -103,7 +177,8 @@ int 

[PATCH v2 30/35] x86: Allow devices to write an SSDT

2020-05-10 Thread Simon Glass
Call the new core function to write the SSDT. This is made up of fragments
generated by devices that have the fill_ssdt() method.

Signed-off-by: Simon Glass 
---

Changes in v2: None
Changes in v1:
- Use OEM_TABLE_ID instead of ACPI_TABLE_CREATOR
- Update ACPI_DSTATUS enum
- Drop writing of coreboot tables

 arch/x86/lib/acpi_table.c | 29 +
 include/acpi/acpi_table.h |  6 ++
 2 files changed, 35 insertions(+)

diff --git a/arch/x86/lib/acpi_table.c b/arch/x86/lib/acpi_table.c
index e96acf08d4..4658d88351 100644
--- a/arch/x86/lib/acpi_table.c
+++ b/arch/x86/lib/acpi_table.c
@@ -14,6 +14,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -354,6 +355,25 @@ static void acpi_create_spcr(struct acpi_spcr *spcr)
header->checksum = table_compute_checksum((void *)spcr, header->length);
 }
 
+void acpi_create_ssdt(struct acpi_ctx *ctx, struct acpi_table_header *ssdt,
+ const char *oem_table_id)
+{
+   memset((void *)ssdt, '\0', sizeof(struct acpi_table_header));
+
+   acpi_fill_header(ssdt, "SSDT");
+   ssdt->revision = acpi_get_table_revision(ACPITAB_SSDT);
+   ssdt->aslc_revision = 1;
+   ssdt->length = sizeof(struct acpi_table_header);
+
+   acpi_inc(ctx, sizeof(struct acpi_table_header));
+
+   acpi_fill_ssdt(ctx);
+
+   /* (Re)calculate length and checksum. */
+   ssdt->length = ctx->current - (void *)ssdt;
+   ssdt->checksum = table_compute_checksum((void *)ssdt, ssdt->length);
+}
+
 /*
  * QEMU's version of write_acpi_tables is defined in drivers/misc/qfw.c
  */
@@ -363,6 +383,7 @@ ulong write_acpi_tables(ulong start_addr)
struct acpi_facs *facs;
struct acpi_table_header *dsdt;
struct acpi_fadt *fadt;
+   struct acpi_table_header *ssdt;
struct acpi_mcfg *mcfg;
struct acpi_madt *madt;
struct acpi_csrt *csrt;
@@ -418,6 +439,14 @@ ulong write_acpi_tables(ulong start_addr)
acpi_create_fadt(fadt, facs, dsdt);
acpi_add_table(ctx, fadt);
 
+   debug("ACPI: * SSDT\n");
+   ssdt = (struct acpi_table_header *)ctx->current;
+   acpi_create_ssdt(ctx, ssdt, OEM_TABLE_ID);
+   if (ssdt->length > sizeof(struct acpi_table_header)) {
+   acpi_inc_align(ctx, ssdt->length);
+   acpi_add_table(ctx, ssdt);
+   }
+
debug("ACPI:* MCFG\n");
mcfg = ctx->current;
acpi_create_mcfg(mcfg);
diff --git a/include/acpi/acpi_table.h b/include/acpi/acpi_table.h
index fe9b29f3f8..9068da24f5 100644
--- a/include/acpi/acpi_table.h
+++ b/include/acpi/acpi_table.h
@@ -27,6 +27,12 @@
 
 struct acpi_ctx;
 
+/* List of ACPI HID that use the coreboot ACPI ID */
+enum coreboot_acpi_ids {
+   COREBOOT_ACPI_ID_CBTABLE= 0x, /* BOOT */
+   COREBOOT_ACPI_ID_MAX= 0x, /* BOOT */
+};
+
 /*
  * RSDP (Root System Description Pointer)
  * Note: ACPI 1.0 didn't have length, xsdt_address, and ext_checksum
-- 
2.26.2.645.ge9eca65c58-goog



[PATCH v2 26/35] acpi: Add support for SSDT generation

2020-05-10 Thread Simon Glass
Some devices need to generate code for the Secondary System Descriptor
Table (SSDT). Add a method to handle this.

Signed-off-by: Simon Glass 
Reviewed-by: Wolfgang Wallner 
---

Changes in v2: None
Changes in v1:
- Switch parameter order of _acpi_fill_ssdt() and make it static
- Fix 'sentinal' and 'METHOD_FILL_SDDT' typos

 arch/sandbox/dts/test.dts |  2 ++
 drivers/core/acpi.c   | 14 +
 include/dm/acpi.h | 23 +
 test/dm/acpi.c| 42 +++
 4 files changed, 81 insertions(+)

diff --git a/arch/sandbox/dts/test.dts b/arch/sandbox/dts/test.dts
index a90395fa62..f6520a54e8 100644
--- a/arch/sandbox/dts/test.dts
+++ b/arch/sandbox/dts/test.dts
@@ -254,6 +254,7 @@
 
acpi-test {
compatible = "denx,u-boot-acpi-test";
+   acpi-ssdt-test-data = "ab";
child {
compatible = "denx,u-boot-acpi-test";
};
@@ -261,6 +262,7 @@
 
acpi-test2 {
compatible = "denx,u-boot-acpi-test";
+   acpi-ssdt-test-data = "cd";
};
 
clocks {
diff --git a/drivers/core/acpi.c b/drivers/core/acpi.c
index 8ae61575dd..4497b5cb2f 100644
--- a/drivers/core/acpi.c
+++ b/drivers/core/acpi.c
@@ -18,6 +18,7 @@
 /* Type of method to call */
 enum method_t {
METHOD_WRITE_TABLES,
+   METHOD_FILL_SSDT,
 };
 
 /* Prototype for all methods */
@@ -51,6 +52,8 @@ acpi_method acpi_get_method(struct udevice *dev, enum 
method_t method)
switch (method) {
case METHOD_WRITE_TABLES:
return aops->write_tables;
+   case METHOD_FILL_SSDT:
+   return aops->fill_ssdt;
}
}
 
@@ -84,6 +87,17 @@ int acpi_recurse_method(struct acpi_ctx *ctx, struct udevice 
*parent,
return 0;
 }
 
+int acpi_fill_ssdt(struct acpi_ctx *ctx)
+{
+   int ret;
+
+   log_debug("Writing SSDT tables\n");
+   ret = acpi_recurse_method(ctx, dm_root(), METHOD_FILL_SSDT);
+   log_debug("Writing SSDT finished, err=%d\n", ret);
+
+   return ret;
+}
+
 int acpi_write_dev_tables(struct acpi_ctx *ctx)
 {
int ret;
diff --git a/include/dm/acpi.h b/include/dm/acpi.h
index 2bd03eaa0a..dac1635cd3 100644
--- a/include/dm/acpi.h
+++ b/include/dm/acpi.h
@@ -72,6 +72,19 @@ struct acpi_ops {
 * @return 0 if OK, -ve on error
 */
int (*write_tables)(const struct udevice *dev, struct acpi_ctx *ctx);
+
+   /**
+* fill_ssdt() - Generate SSDT code for a device
+*
+* This is called to create the SSDT code. THe method should write out
+* whatever ACPI code is needed by this device. It will end up in the
+* SSDT table.
+*
+* @dev: Device to write
+* @ctx: ACPI context to use
+* @return 0 if OK, -ve on error
+*/
+   int (*fill_ssdt)(const struct udevice *dev, struct acpi_ctx *ctx);
 };
 
 #define device_get_acpi_ops(dev)   ((dev)->driver->acpi_ops)
@@ -116,6 +129,16 @@ int acpi_copy_name(char *out_name, const char *name);
  */
 int acpi_write_dev_tables(struct acpi_ctx *ctx);
 
+/**
+ * acpi_fill_ssdt() - Generate ACPI tables for SSDT
+ *
+ * This is called to create the SSDT code for all devices.
+ *
+ * @ctx: ACPI context to use
+ * @return 0 if OK, -ve on error
+ */
+int acpi_fill_ssdt(struct acpi_ctx *ctx);
+
 #endif /* __ACPI__ */
 
 #endif
diff --git a/test/dm/acpi.c b/test/dm/acpi.c
index 0c2e12d170..d1bd108223 100644
--- a/test/dm/acpi.c
+++ b/test/dm/acpi.c
@@ -14,6 +14,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -67,9 +68,23 @@ static int testacpi_get_name(const struct udevice *dev, char 
*out_name)
return acpi_copy_name(out_name, ACPI_TEST_DEV_NAME);
 }
 
+static int testacpi_fill_ssdt(const struct udevice *dev, struct acpi_ctx *ctx)
+{
+   const char *data;
+
+   data = dev_read_string(dev, "acpi-ssdt-test-data");
+   if (data) {
+   while (*data)
+   acpigen_emit_byte(ctx, *data++);
+   }
+
+   return 0;
+}
+
 struct acpi_ops testacpi_ops = {
.get_name   = testacpi_get_name,
.write_tables   = testacpi_write_tables,
+   .fill_ssdt  = testacpi_fill_ssdt,
 };
 
 static const struct udevice_id testacpi_ids[] = {
@@ -396,3 +411,30 @@ static int dm_test_acpi_device_status(struct 
unit_test_state *uts)
return 0;
 }
 DM_TEST(dm_test_acpi_device_status, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
+
+/* Test acpi_fill_ssdt() */
+static int dm_test_acpi_fill_ssdt(struct unit_test_state *uts)
+{
+   struct acpi_ctx ctx;
+   u8 *buf;
+
+   buf = malloc(BUF_SIZE);
+   ut_assertnonnull(buf);
+
+   ctx.current = buf;
+   buf[4] = 'z';   /* sentinel */
+   ut_assertok(acpi_fill_ssdt());
+
+   /* These values come from acpi-test's acpi-ssdt-test-data property */
+ 

[PATCH v2 27/35] x86: acpi: Move MADT down a bit

2020-05-10 Thread Simon Glass
Put this table before MCFG so that it matches the order that coreboot uses
when passing tables to Linux. This is a cosmetic change since the order of
the tables does not otherwise matter.

Signed-off-by: Simon Glass 
---

Changes in v2: None
Changes in v1:
- Correct the commit subject

 arch/x86/lib/acpi_table.c | 12 ++--
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/arch/x86/lib/acpi_table.c b/arch/x86/lib/acpi_table.c
index 6985ef4ba5..e96acf08d4 100644
--- a/arch/x86/lib/acpi_table.c
+++ b/arch/x86/lib/acpi_table.c
@@ -418,18 +418,18 @@ ulong write_acpi_tables(ulong start_addr)
acpi_create_fadt(fadt, facs, dsdt);
acpi_add_table(ctx, fadt);
 
-   debug("ACPI:* MADT\n");
-   madt = ctx->current;
-   acpi_create_madt(madt);
-   acpi_inc_align(ctx, madt->header.length);
-   acpi_add_table(ctx, madt);
-
debug("ACPI:* MCFG\n");
mcfg = ctx->current;
acpi_create_mcfg(mcfg);
acpi_inc_align(ctx, mcfg->header.length);
acpi_add_table(ctx, mcfg);
 
+   debug("ACPI:* MADT\n");
+   madt = ctx->current;
+   acpi_create_madt(madt);
+   acpi_inc_align(ctx, madt->header.length);
+   acpi_add_table(ctx, madt);
+
debug("ACPI:* CSRT\n");
csrt = ctx->current;
acpi_create_csrt(csrt);
-- 
2.26.2.645.ge9eca65c58-goog



[PATCH v2 25/35] acpi: Add support for a generic power sequence

2020-05-10 Thread Simon Glass
Add a way for devices to enable and disable themselves using ACPI code
that updates GPIOs. This takes several timing parameters and supports
enable, reset and stop.

Signed-off-by: Simon Glass 
---

Changes in v2: None
Changes in v1: None

 include/acpi/acpi_device.h | 41 
 lib/acpi/acpi_device.c | 99 ++
 test/dm/acpigen.c  | 68 ++
 3 files changed, 208 insertions(+)

diff --git a/include/acpi/acpi_device.h b/include/acpi/acpi_device.h
index d43ab4ba36..adebd63c15 100644
--- a/include/acpi/acpi_device.h
+++ b/include/acpi/acpi_device.h
@@ -342,4 +342,45 @@ int acpi_device_write_i2c_dev(struct acpi_ctx *ctx, const 
struct udevice *dev);
  */
 int acpi_device_write_spi_dev(struct acpi_ctx *ctx, const struct udevice *dev);
 
+/**
+ * acpi_device_add_power_res() - Add a basic PowerResource block for a device
+ *
+ * This includes GPIOs to control enable, reset and stop operation of the
+ * device. Each GPIO is optional, but at least one must be provided.
+ * This can be applied to any device that has power control, so is fairly
+ * generic.
+ *
+ * Reset - Put the device into / take the device out of reset.
+ * Enable - Enable / disable power to device.
+ * Stop - Stop / start operation of device.
+ *
+ * @ctx: ACPI context pointer
+ * @tx_state_val: Mask to use to toggle the TX state on the GPIO pin, e,g.
+ * PAD_CFG0_TX_STATE
+ * @dw0_name: Name to use for access to dw0, e.g. "\\_SB.GPC0"
+ * @reset_gpio: GPIO used to take device out of reset or to put it into reset
+ * @reset_delay_ms: Delay to be inserted after device is taken out of reset
+ * (_ON method delay)
+ * @reset_off_delay_ms: Delay to be inserted after device is put into reset
+ * (_OFF method delay)
+ * @enable_gpio: GPIO used to enable device
+ * @enable_delay_ms: Delay to be inserted after device is enabled
+ * @enable_off_delay_ms: Delay to be inserted after device is disabled
+ * (_OFF method delay)
+ * @stop_gpio: GPIO used to stop operation of device
+ * @stop_delay_ms: Delay to be inserted after disabling stop (_ON method delay)
+ * @stop_off_delay_ms: Delay to be inserted after enabling stop.
+ * (_OFF method delay)
+ *
+ * @return 0 if OK, -ve if at least one GPIO is not provided
+ */
+int acpi_device_add_power_res(struct acpi_ctx *ctx, u32 tx_state_val,
+ const char *dw0_name,
+ const struct gpio_desc *reset_gpio,
+ uint reset_delay_ms, uint reset_off_delay_ms,
+ const struct gpio_desc *enable_gpio,
+ uint enable_delay_ms, uint enable_off_delay_ms,
+ const struct gpio_desc *stop_gpio,
+ uint stop_delay_ms, uint stop_off_delay_ms);
+
 #endif
diff --git a/lib/acpi/acpi_device.c b/lib/acpi/acpi_device.c
index 3d5dc746f8..e5968bd3aa 100644
--- a/lib/acpi/acpi_device.c
+++ b/lib/acpi/acpi_device.c
@@ -382,6 +382,105 @@ int acpi_device_write_interrupt_or_gpio(struct acpi_ctx 
*ctx,
return 0;
 }
 
+/* PowerResource() with Enable and/or Reset control */
+int acpi_device_add_power_res(struct acpi_ctx *ctx, u32 tx_state_val,
+ const char *dw0_name,
+ const struct gpio_desc *reset_gpio,
+ uint reset_delay_ms, uint reset_off_delay_ms,
+ const struct gpio_desc *enable_gpio,
+ uint enable_delay_ms, uint enable_off_delay_ms,
+ const struct gpio_desc *stop_gpio,
+ uint stop_delay_ms, uint stop_off_delay_ms)
+{
+   static const char *const power_res_dev_states[] = { "_PR0", "_PR3" };
+   struct acpi_gpio reset, enable, stop;
+   bool has_reset, has_enable, has_stop;
+   int ret;
+
+   gpio_get_acpi(reset_gpio, );
+   gpio_get_acpi(enable_gpio, );
+   gpio_get_acpi(stop_gpio, );
+   has_reset = reset.pins[0];
+   has_enable = enable.pins[0];
+   has_stop = stop.pins[0];
+
+   if (!has_reset && !has_enable && !has_stop)
+   return -EINVAL;
+
+   /* PowerResource (PRIC, 0, 0) */
+   acpigen_write_power_res(ctx, "PRIC", 0, 0, power_res_dev_states,
+   ARRAY_SIZE(power_res_dev_states));
+
+   /* Method (_STA, 0, NotSerialized) { Return (0x1) } */
+   acpigen_write_sta(ctx, 0x1);
+
+   /* Method (_ON, 0, Serialized) */
+   acpigen_write_method_serialized(ctx, "_ON", 0);
+   if (reset_gpio) {
+   ret = acpigen_set_enable_tx_gpio(ctx, tx_state_val, dw0_name,
+, true);
+   if (ret)
+   return log_msg_ret("reset1", ret);
+   }
+   if (has_enable) {
+   ret = acpigen_set_enable_tx_gpio(ctx, tx_state_val, dw0_name,
+ 

[PATCH v2 19/35] acpi: Support writing Device Properties objects via _DSD

2020-05-10 Thread Simon Glass
More complex device properties can be provided to drivers via a
device-specific data (_DSD) object.

To create this we need to build it up in a separate data structure and
then generate the ACPI code, due to its recursive nature.

Add an implementation of this.

Signed-off-by: Simon Glass 
---

Changes in v2: None
Changes in v1: None

 include/acpi/acpi_dp.h | 215 ++
 include/acpi/acpigen.h |   1 +
 lib/acpi/Makefile  |   1 +
 lib/acpi/acpi_dp.c | 324 +
 lib/acpi/acpigen.c |   3 +
 test/dm/Makefile   |   1 +
 test/dm/acpi_dp.c  | 405 +
 7 files changed, 950 insertions(+)
 create mode 100644 include/acpi/acpi_dp.h
 create mode 100644 lib/acpi/acpi_dp.c
 create mode 100644 test/dm/acpi_dp.c

diff --git a/include/acpi/acpi_dp.h b/include/acpi/acpi_dp.h
new file mode 100644
index 00..840be855c5
--- /dev/null
+++ b/include/acpi/acpi_dp.h
@@ -0,0 +1,215 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Device properties, a temporary data structure for adding to ACPI code
+ *
+ * Copyright 2019 Google LLC
+ * Mostly taken from coreboot file acpi_device.h
+ */
+
+#ifndef __ACPI_DP_H
+#define __ACPI_DP_H
+
+struct acpi_ctx;
+
+/*
+ * Writing Device Properties objects via _DSD
+ *
+ * This is described in ACPI 6.3 section 6.2.5
+ *
+ * This provides a structure to handle nested device-specific data which ends
+ * up in a _DSD table.
+ *
+ * 
https://www.kernel.org/doc/html/latest/firmware-guide/acpi/DSD-properties-rules.html
+ * 
https://uefi.org/sites/default/files/resources/_DSD-device-properties-UUID.pdf
+ * 
https://uefi.org/sites/default/files/resources/_DSD-hierarchical-data-extension-UUID-v1.1.pdf
+ *
+ * The Device Property Hierarchy can be multiple levels deep with multiple
+ * children possible in each level.  In order to support this flexibility
+ * the device property hierarchy must be built up before being written out.
+ *
+ * For example:
+ *
+ * // Child table with string and integer
+ * struct acpi_dp *child = acpi_dp_new_table("CHLD");
+ * acpi_dp_add_string(child, "childstring", "CHILD");
+ * acpi_dp_add_integer(child, "childint", 100);
+ *
+ * // _DSD table with integer and gpio and child pointer
+ * struct acpi_dp *dsd = acpi_dp_new_table("_DSD");
+ * acpi_dp_add_integer(dsd, "number1", 1);
+ * acpi_dp_add_gpio(dsd, "gpio", "\_SB.PCI0.GPIO", 0, 0, 1);
+ * acpi_dp_add_child(dsd, "child", child);
+ *
+ * // Write entries into SSDT and clean up resources
+ * acpi_dp_write(dsd);
+ *
+ * Name(_DSD, Package() {
+ *   ToUUID("daffd814-6eba-4d8c-8a91-bc9bbf4aa301")
+ *   Package() {
+ * Package() { "gpio", Package() { \_SB.PCI0.GPIO, 0, 0, 0 } }
+ * Package() { "number1", 1 }
+ *   }
+ *   ToUUID("dbb8e3e6-5886-4ba6-8795-1319f52a966b")
+ *   Package() {
+ * Package() { "child", CHLD }
+ *   }
+ * }
+ * Name(CHLD, Package() {
+ *   ToUUID("daffd814-6eba-4d8c-8a91-bc9bbf4aa301")
+ *   Package() {
+ * Package() { "childstring", "CHILD" }
+ * Package() { "childint", 100 }
+ *   }
+ * }
+ */
+
+#define ACPI_DP_UUID   "daffd814-6eba-4d8c-8a91-bc9bbf4aa301"
+#define ACPI_DP_CHILD_UUID "dbb8e3e6-5886-4ba6-8795-1319f52a966b"
+
+/**
+ * enum acpi_dp_type - types of device property objects
+ *
+ * These refer to the types defined by struct acpi_dp below
+ *
+ * @ACPI_DP_TYPE_UNKNOWN: Unknown / do not use
+ * @ACPI_DP_TYPE_INTEGER: Integer value (u64) in @integer
+ * @ACPI_DP_TYPE_STRING: String value in @string
+ * @ACPI_DP_TYPE_REFERENCE: Reference to another object, with value in @string
+ * @ACPI_DP_TYPE_TABLE: Type for a top-level table which may have children
+ * @ACPI_DP_TYPE_ARRAY: Array of items with first item in @array and following
+ * items linked from that item's @next
+ * @ACPI_DP_TYPE_CHILD: Child object, with siblings in that child's @next
+ */
+enum acpi_dp_type {
+   ACPI_DP_TYPE_UNKNOWN,
+   ACPI_DP_TYPE_INTEGER,
+   ACPI_DP_TYPE_STRING,
+   ACPI_DP_TYPE_REFERENCE,
+   ACPI_DP_TYPE_TABLE,
+   ACPI_DP_TYPE_ARRAY,
+   ACPI_DP_TYPE_CHILD,
+};
+
+/**
+ * struct acpi_dp - ACPI device properties
+ *
+ * @type: Table type
+ * @name: Name of object, typically _DSD but could be CHLD for a child object
+ * @next: Next object in list (next array element or next sibling)
+ * @child: Pointer to first child, if @type == ACPI_DP_TYPE_CHILD, else NULL
+ * @array: First array element, if @type == ACPI_DP_TYPE_ARRAY, else NULL
+ * @integer: Integer value of the property, if @type == ACPI_DP_TYPE_INTEGER
+ * @string: String value of the property, if @type == ACPI_DP_TYPE_STRING;
+ * child name if @type == ACPI_DP_TYPE_CHILD;
+ * reference name if @type == ACPI_DP_TYPE_REFERENCE;
+ */
+struct acpi_dp {
+   enum acpi_dp_type type;
+   const char *name;
+   struct acpi_dp *next;
+   union {
+   struct acpi_dp *child;
+   struct acpi_dp *array;
+   };
+   union {
+   

[PATCH v2 23/35] acpi: Add support for writing a Power Resource

2020-05-10 Thread Simon Glass
These are used in ACPI to disable power to various pats of the system when
in sleep. Add a way to create a power resource, with the caller finishing
off the details.

Signed-off-by: Simon Glass 
---

Changes in v2: None
Changes in v1: None

 include/acpi/acpigen.h | 22 ++
 lib/acpi/acpigen.c | 22 ++
 test/dm/acpigen.c  | 41 +
 3 files changed, 85 insertions(+)

diff --git a/include/acpi/acpigen.h b/include/acpi/acpigen.h
index c595bb9efa..7d9a02805b 100644
--- a/include/acpi/acpigen.h
+++ b/include/acpi/acpigen.h
@@ -45,6 +45,7 @@ enum {
AND_OP  = 0x7b,
OR_OP   = 0x7d,
NOT_OP  = 0x80,
+   POWER_RES_OP= 0x84,
RETURN_OP   = 0xa4,
 };
 
@@ -310,4 +311,25 @@ void acpigen_write_and(struct acpi_ctx *ctx, u8 arg1, u8 
arg2, u8 res);
  */
 void acpigen_write_not(struct acpi_ctx *ctx, u8 arg, u8 res);
 
+/**
+ * acpigen_write_power_res() - Write a power resource
+ *
+ * Name (_PRx, Package(One) { name })
+ * ...
+ * PowerResource (name, level, order)
+ *
+ * The caller should fill in the rest of the power resource and then call
+ * acpigen_pop_len() to close it off
+ *
+ * @ctx: ACPI context pointer
+ * @name: Name of power resource (e.g. "PRIC")
+ * @level: Deepest sleep level that this resource must be kept on (0=S0, 3=S3)
+ * @order: Order that this must be enabled/disabled (e.g. 0)
+ * @dev_stats: List of states to define, e.g. {"_PR0", "_PR3"}
+ * @dev_states_count: Number of dev states
+ */
+void acpigen_write_power_res(struct acpi_ctx *ctx, const char *name, uint 
level,
+uint order, const char *const dev_states[],
+size_t dev_states_count);
+
 #endif
diff --git a/lib/acpi/acpigen.c b/lib/acpi/acpigen.c
index 57b295aa4e..824a2c97cd 100644
--- a/lib/acpi/acpigen.c
+++ b/lib/acpi/acpigen.c
@@ -328,6 +328,28 @@ int acpigen_write_uuid(struct acpi_ctx *ctx, const char 
*uuid)
return 0;
 }
 
+void acpigen_write_power_res(struct acpi_ctx *ctx, const char *name, uint 
level,
+uint order, const char *const dev_states[],
+size_t dev_states_count)
+{
+   size_t i;
+
+   for (i = 0; i < dev_states_count; i++) {
+   acpigen_write_name(ctx, dev_states[i]);
+   acpigen_write_package(ctx, 1);
+   acpigen_emit_simple_namestring(ctx, name);
+   acpigen_pop_len(ctx);   /* Package */
+   }
+
+   acpigen_emit_ext_op(ctx, POWER_RES_OP);
+
+   acpigen_write_len_f(ctx);
+
+   acpigen_emit_simple_namestring(ctx, name);
+   acpigen_emit_byte(ctx, level);
+   acpigen_emit_word(ctx, order);
+}
+
 /* Sleep (ms) */
 void acpigen_write_sleep(struct acpi_ctx *ctx, u64 sleep_ms)
 {
diff --git a/test/dm/acpigen.c b/test/dm/acpigen.c
index e059d6f80d..c92f6cdb0b 100644
--- a/test/dm/acpigen.c
+++ b/test/dm/acpigen.c
@@ -696,3 +696,44 @@ static int dm_test_acpi_misc(struct unit_test_state *uts)
return 0;
 }
 DM_TEST(dm_test_acpi_misc, 0);
+
+/* Test writing an ACPI power resource */
+static int dm_test_acpi_power_res(struct unit_test_state *uts)
+{
+   const char *const states[] = { "_PR0", "_PR3" };
+   const char *name = "PRIC";
+   const int level = 3;
+   const int order = 2;
+   struct acpi_ctx *ctx;
+   u8 *ptr;
+
+   ut_assertok(alloc_context());
+
+   ptr = acpigen_get_current(ctx);
+
+   /* PowerResource (PRIC, 0, 0) */
+   acpigen_write_power_res(ctx, name, level, order, states,
+   ARRAY_SIZE(states));
+   ut_asserteq(0x28, acpigen_get_current(ctx) - ptr);
+   ut_asserteq(NAME_OP, ptr[0]);
+   ut_asserteq_strn(states[0], (char *)ptr + 1);
+   ut_asserteq(8, get_length(ptr + 6));
+   ut_asserteq_strn(name, (char *)ptr + 0xa);
+
+   ut_asserteq_strn(states[1], (char *)ptr + 0xf);
+   ut_asserteq(8, get_length(ptr + 0x14));
+   ut_asserteq_strn(name, (char *)ptr + 0x18);
+
+   ut_asserteq(POWER_RES_OP, ptr[0x1d]);
+   ut_asserteq_strn(name, (char *)ptr + 0x21);
+   ut_asserteq(level, ptr[0x25]);
+   ut_asserteq(order, get_unaligned((u16 *)(ptr + 0x26)));
+
+   /* The length is not set - caller must use acpigen_pop_len() */
+   ut_asserteq(1, ctx->ltop);
+
+   free_context();
+
+   return 0;
+}
+DM_TEST(dm_test_acpi_power_res, 0);
-- 
2.26.2.645.ge9eca65c58-goog



[PATCH v2 22/35] acpi: Add support for various misc ACPI opcodes

2020-05-10 Thread Simon Glass
Add more functions to handle some miscellaneous ACPI opcodes.

Signed-off-by: Simon Glass 
---

Changes in v2: None
Changes in v1: None

 include/acpi/acpigen.h | 114 +
 lib/acpi/acpigen.c |  83 ++
 test/dm/acpigen.c  |  75 +++
 3 files changed, 272 insertions(+)

diff --git a/include/acpi/acpigen.h b/include/acpi/acpigen.h
index 16b33ebe66..c595bb9efa 100644
--- a/include/acpi/acpigen.h
+++ b/include/acpi/acpigen.h
@@ -26,9 +26,26 @@ enum {
QWORD_PREFIX= 0x0e,
BUFFER_OP   = 0x11,
PACKAGE_OP  = 0x12,
+   METHOD_OP   = 0x14,
+   SLEEP_OP= 0x22,
DUAL_NAME_PREFIX= 0x2e,
MULTI_NAME_PREFIX   = 0x2f,
+   DEBUG_OP= 0x31,
+   EXT_OP_PREFIX   = 0x5b,
ROOT_PREFIX = 0x5c,
+   LOCAL0_OP   = 0x60,
+   LOCAL1_OP   = 0x61,
+   LOCAL2_OP   = 0x62,
+   LOCAL3_OP   = 0x63,
+   LOCAL4_OP   = 0x64,
+   LOCAL5_OP   = 0x65,
+   LOCAL6_OP   = 0x66,
+   LOCAL7_OP   = 0x67,
+   STORE_OP= 0x70,
+   AND_OP  = 0x7b,
+   OR_OP   = 0x7d,
+   NOT_OP  = 0x80,
+   RETURN_OP   = 0xa4,
 };
 
 /**
@@ -196,4 +213,101 @@ void acpigen_write_name(struct acpi_ctx *ctx, const char 
*namepath);
  */
 int acpigen_write_uuid(struct acpi_ctx *ctx, const char *uuid);
 
+/**
+ * acpigen_emit_ext_op() - Emit an extended op with the EXT_OP_PREFIX prefix
+ *
+ * @ctx: ACPI context pointer
+ * @op: Operation code (e.g. SLEEP_OP)
+ */
+void acpigen_emit_ext_op(struct acpi_ctx *ctx, uint op);
+
+/**
+ * acpigen_write_method() - Write a method header
+ *
+ * @ctx: ACPI context pointer
+ * @name: Method name (4 characters)
+ * @nargs: Number of method arguments (0 if none)
+ */
+void acpigen_write_method(struct acpi_ctx *ctx, const char *name, int nargs);
+
+/**
+ * acpigen_write_method_serialized() - Write a method header
+ *
+ * This sets the 'serialized' flag so that the method is thread-safe
+ *
+ * @ctx: ACPI context pointer
+ * @name: Method name (4 characters)
+ * @nargs: Number of method arguments (0 if none)
+ */
+void acpigen_write_method_serialized(struct acpi_ctx *ctx, const char *name,
+int nargs);
+
+/**
+ * acpigen_write_sta() - Write a _STA method
+ *
+ * @ctx: ACPI context pointer
+ * @status: Status value to return
+ */
+void acpigen_write_sta(struct acpi_ctx *ctx, uint status);
+
+/**
+ * acpigen_write_sleep() - Write a sleep operation
+ *
+ * @ctx: ACPI context pointer
+ * @sleep_ms: Number of milliseconds to sleep for
+ */
+void acpigen_write_sleep(struct acpi_ctx *ctx, u64 sleep_ms);
+
+/**
+ * acpigen_write_store() - Write a store operation
+ *
+ * @ctx: ACPI context pointer
+ */
+void acpigen_write_store(struct acpi_ctx *ctx);
+
+/**
+ * acpigen_write_debug_string() - Write a debug string
+ *
+ * This writes a debug operation with an associated string
+ *
+ * @ctx: ACPI context pointer
+ * @str: String to write
+ */
+void acpigen_write_debug_string(struct acpi_ctx *ctx, const char *str);
+
+/**
+ * acpigen_write_or() - Write a bitwise OR operation
+ *
+ * res = arg1 | arg2
+ *
+ * @ctx: ACPI context pointer
+ * @arg1: ACPI opcode for operand 1 (e.g. LOCAL0_OP)
+ * @arg2: ACPI opcode for operand 2 (e.g. LOCAL1_OP)
+ * @res: ACPI opcode for result (e.g. LOCAL2_OP)
+ */
+void acpigen_write_or(struct acpi_ctx *ctx, u8 arg1, u8 arg2, u8 res);
+
+/**
+ * acpigen_write_and() - Write a bitwise AND operation
+ *
+ * res = arg1 & arg2
+ *
+ * @ctx: ACPI context pointer
+ * @arg1: ACPI opcode for operand 1 (e.g. LOCAL0_OP)
+ * @arg2: ACPI opcode for operand 2 (e.g. LOCAL1_OP)
+ * @res: ACPI opcode for result (e.g. LOCAL2_OP)
+ */
+void acpigen_write_and(struct acpi_ctx *ctx, u8 arg1, u8 arg2, u8 res);
+
+/**
+ * acpigen_write_or() - Write a bitwise NOT operation
+ *
+ * res = ~arg1
+ *
+ * @ctx: ACPI context pointer
+ * @arg: ACPI opcode for operand (e.g. LOCAL0_OP)
+ * @res: ACPI opcode for result (e.g. LOCAL2_OP)
+ */
+void acpigen_write_not(struct acpi_ctx *ctx, u8 arg, u8 res);
+
 #endif
diff --git a/lib/acpi/acpigen.c b/lib/acpi/acpigen.c
index 7321a98c0f..57b295aa4e 100644
--- a/lib/acpi/acpigen.c
+++ b/lib/acpi/acpigen.c
@@ -74,6 +74,12 @@ void acpigen_pop_len(struct acpi_ctx *ctx)
p[2] = (len >> 12 & 0xff);
 }
 
+void acpigen_emit_ext_op(struct acpi_ctx *ctx, uint op)
+{
+   acpigen_emit_byte(ctx, EXT_OP_PREFIX);
+   acpigen_emit_byte(ctx, op);
+}
+
 char *acpigen_write_package(struct acpi_ctx *ctx, int nr_el)
 {
char *p;
@@ -253,6 +259,37 @@ void acpigen_write_name(struct acpi_ctx *ctx, const char 
*namepath)
acpigen_emit_namestring(ctx, namepath);
 }
 
+static void 

[PATCH v2 24/35] acpi: Add support for writing a GPIO power sequence

2020-05-10 Thread Simon Glass
Power to some devices is controlled by GPIOs. Add a way to generate ACPI
code to enable and disable a GPIO so that this can be handled within an
ACPI method.

Signed-off-by: Simon Glass 
---

Changes in v2: None
Changes in v1: None

 include/acpi/acpigen.h | 12 +++
 lib/acpi/acpigen.c | 80 ++
 test/dm/acpigen.c  | 54 
 3 files changed, 146 insertions(+)

diff --git a/include/acpi/acpigen.h b/include/acpi/acpigen.h
index 7d9a02805b..1205836f58 100644
--- a/include/acpi/acpigen.h
+++ b/include/acpi/acpigen.h
@@ -13,6 +13,7 @@
 #include 
 
 struct acpi_ctx;
+struct acpi_gpio;
 
 /* ACPI Op/Prefix codes */
 enum {
@@ -332,4 +333,15 @@ void acpigen_write_power_res(struct acpi_ctx *ctx, const 
char *name, uint level,
 uint order, const char *const dev_states[],
 size_t dev_states_count);
 
+/*
+ * Helper functions for enabling/disabling Tx GPIOs based on the GPIO
+ * polarity. These functions end up calling acpigen_soc_{set,clear}_tx_gpio to
+ * make callbacks into SoC acpigen code.
+ *
+ * Returns 0 on success and -1 on error.
+ */
+int acpigen_set_enable_tx_gpio(struct acpi_ctx *ctx, u32 tx_state_val,
+  const char *dw0_name, struct acpi_gpio *gpio,
+  bool enable);
+
 #endif
diff --git a/lib/acpi/acpigen.c b/lib/acpi/acpigen.c
index 824a2c97cd..1c72306bd1 100644
--- a/lib/acpi/acpigen.c
+++ b/lib/acpi/acpigen.c
@@ -13,6 +13,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 
 /* CPU path format */
@@ -395,3 +396,82 @@ void acpigen_write_debug_string(struct acpi_ctx *ctx, 
const char *str)
acpigen_write_string(ctx, str);
acpigen_emit_ext_op(ctx, DEBUG_OP);
 }
+
+/**
+ * acpigen_get_dw0_in_local5() - Generate code to put dw0 cfg0 in local5
+ *
+ * @ctx: ACPI context pointer
+ * @dw0_name: Name to use (e.g. "\\_SB.GPC0")
+ * @addr: GPIO pin configuration register address
+ *
+ * Store (\_SB.GPC0 (addr), Local5)
+ * \_SB.GPC0 is used to read cfg0 value from dw0. It is typically defined in
+ * the board's gpiolib.asl
+ */
+static void acpigen_get_dw0_in_local5(struct acpi_ctx *ctx,
+ const char *dw0_name, ulong addr)
+{
+   acpigen_write_store(ctx);
+   acpigen_emit_namestring(ctx, dw0_name);
+   acpigen_write_integer(ctx, addr);
+   acpigen_emit_byte(ctx, LOCAL5_OP);
+}
+
+/**
+ * acpigen_set_gpio_val() - Set value of TX GPIO to on/off
+ *
+ * @ctx: ACPI context pointer
+ * @dw0_name: Name to use (e.g. "\\_SB.GPC0")
+ * @gpio_num: GPIO number to adjust
+ * @vaL: true to set on, false to set off
+ */
+static int acpigen_set_gpio_val(struct acpi_ctx *ctx, u32 tx_state_val,
+   const char *dw0_name, struct acpi_gpio *gpio,
+   bool val)
+{
+   acpigen_get_dw0_in_local5(ctx, dw0_name, gpio->pin0_addr);
+
+   if (val) {
+   /* Or (Local5, PAD_CFG0_TX_STATE, Local5) */
+   acpigen_write_or(ctx, LOCAL5_OP, tx_state_val, LOCAL5_OP);
+   } else {
+   /* Not (PAD_CFG0_TX_STATE, Local6) */
+   acpigen_write_not(ctx, tx_state_val, LOCAL6_OP);
+
+   /* And (Local5, Local6, Local5) */
+   acpigen_write_and(ctx, LOCAL5_OP, LOCAL6_OP, LOCAL5_OP);
+   }
+
+   /*
+* \_SB.SPC0 (addr, Local5)
+* \_SB.SPC0 is used to write cfg0 value in dw0. It is defined in
+* gpiolib.asl.
+*/
+   acpigen_emit_namestring(ctx, dw0_name);
+   acpigen_write_integer(ctx, gpio->pin0_addr);
+   acpigen_emit_byte(ctx, LOCAL5_OP);
+
+   return 0;
+}
+
+/*
+ * Helper functions for enabling/disabling Tx GPIOs based on the GPIO
+ * polarity. These functions end up calling acpigen_{set,clear}_tx_gpio to
+ * make callbacks into SoC acpigen code.
+ *
+ * Returns 0 on success and -1 on error.
+ */
+int acpigen_set_enable_tx_gpio(struct acpi_ctx *ctx, u32 tx_state_val,
+  const char *dw0_name, struct acpi_gpio *gpio,
+  bool enable)
+{
+   bool set;
+   int ret;
+
+   set = gpio->polarity == ACPI_GPIO_ACTIVE_HIGH ? enable : !enable;
+   ret = acpigen_set_gpio_val(ctx, tx_state_val, dw0_name, gpio, set);
+   if (ret)
+   return log_msg_ret("call", ret);
+
+   return 0;
+}
diff --git a/test/dm/acpigen.c b/test/dm/acpigen.c
index c92f6cdb0b..06bdd63739 100644
--- a/test/dm/acpigen.c
+++ b/test/dm/acpigen.c
@@ -737,3 +737,57 @@ static int dm_test_acpi_power_res(struct unit_test_state 
*uts)
return 0;
 }
 DM_TEST(dm_test_acpi_power_res, 0);
+
+/* Test writing ACPI code to toggle a GPIO */
+static int dm_test_acpi_gpio_toggle(struct unit_test_state *uts)
+{
+   const uint addr = 0x80012;
+   const int txbit = BIT(2);
+   struct gpio_desc desc;
+   struct acpi_gpio gpio;
+   struct acpi_ctx *ctx;

[PATCH v2 20/35] acpi: Support writing a GPIO

2020-05-10 Thread Simon Glass
Allowing writing out a reference to a GPIO within the ACPI output. This
can be used by ACPI code to access a GPIO at runtime.

Signed-off-by: Simon Glass 
---

Changes in v2: None
Changes in v1: None

 include/acpi/acpi_dp.h | 18 ++
 lib/acpi/acpi_dp.c | 21 +
 test/dm/acpi_dp.c  | 39 +++
 3 files changed, 78 insertions(+)

diff --git a/include/acpi/acpi_dp.h b/include/acpi/acpi_dp.h
index 840be855c5..f42602405a 100644
--- a/include/acpi/acpi_dp.h
+++ b/include/acpi/acpi_dp.h
@@ -202,6 +202,24 @@ struct acpi_dp *acpi_dp_add_integer_array(struct acpi_dp 
*dp, const char *name,
 struct acpi_dp *acpi_dp_add_child(struct acpi_dp *dp, const char *name,
  struct acpi_dp *child);
 
+/**
+ * acpi_dp_add_gpio() - Add a GPIO to a list of Device Properties
+ *
+ * A new node is added to the end of the property list of @dp, with the
+ * GPIO properties added to the the new node
+ *
+ * @dp: Table to add this property to
+ * @name: Name of property
+ * @ref: Reference to device with a _CRS containing GpioIO or GpioInt
+ * @index: Index of the GPIO resource in _CRS starting from zero
+ * @pin: Pin in the GPIO resource, typically zero
+ * @active_low: true if pin is active low
+ * @return pointer to new node, or NULL if out of memory
+ */
+struct acpi_dp *acpi_dp_add_gpio(struct acpi_dp *dp, const char *name,
+const char *ref, int index, int pin,
+bool active_low);
+
 /**
  * acpi_dp_write() - Write Device Property hierarchy and clean up resources
  *
diff --git a/lib/acpi/acpi_dp.c b/lib/acpi/acpi_dp.c
index 479cb6743c..4ba5d555a0 100644
--- a/lib/acpi/acpi_dp.c
+++ b/lib/acpi/acpi_dp.c
@@ -322,3 +322,24 @@ struct acpi_dp *acpi_dp_add_integer_array(struct acpi_dp 
*dp, const char *name,
 
return dp_array;
 }
+
+struct acpi_dp *acpi_dp_add_gpio(struct acpi_dp *dp, const char *name,
+const char *ref, int index, int pin,
+bool active_low)
+{
+   struct acpi_dp *gpio;
+
+   assert(dp);
+   gpio = acpi_dp_new_table(name);
+   if (!gpio)
+   return NULL;
+
+   acpi_dp_add_reference(gpio, NULL, ref);
+   acpi_dp_add_integer(gpio, NULL, index);
+   acpi_dp_add_integer(gpio, NULL, pin);
+   acpi_dp_add_integer(gpio, NULL, active_low);
+
+   acpi_dp_add_array(dp, gpio);
+
+   return gpio;
+}
diff --git a/test/dm/acpi_dp.c b/test/dm/acpi_dp.c
index c11394786f..d6a054a98b 100644
--- a/test/dm/acpi_dp.c
+++ b/test/dm/acpi_dp.c
@@ -403,3 +403,42 @@ static int dm_test_acpi_dp_child(struct unit_test_state 
*uts)
return 0;
 }
 DM_TEST(dm_test_acpi_dp_child, 0);
+
+/* Test emitting a GPIO */
+static int dm_test_acpi_dp_gpio(struct unit_test_state *uts)
+{
+   struct acpi_ctx *ctx;
+   struct acpi_dp *dp;
+   u8 *ptr, *pptr;
+
+   ut_assertok(alloc_context());
+
+   dp = acpi_dp_new_table("FRED");
+   ut_assertnonnull(dp);
+
+   /* Try a few different parameters */
+   ut_assertnonnull(acpi_dp_add_gpio(dp, "reset", TEST_REF, 0x23, 0x24,
+ false));
+   ut_assertnonnull(acpi_dp_add_gpio(dp, "allow", TEST_REF, 0, 0, true));
+
+   ptr = acpigen_get_current(ctx);
+   ut_assertok(acpi_dp_write(ctx, dp));
+   ut_asserteq(0x6e, acpigen_get_current(ctx) - ptr);
+
+   pptr = ptr + 0x2c; //0x3a;
+   ut_asserteq_str("reset", (char *)pptr);
+   ut_asserteq_strn(EXPECT_REF, (char *)pptr + 0xe);
+   ut_asserteq(0x23, pptr[0x1b]);
+   ut_asserteq(0x24, pptr[0x1d]);
+   ut_asserteq(ZERO_OP, pptr[0x1e]);
+
+   pptr = ptr + 0x51;
+   ut_asserteq_str("allow", (char *)pptr);
+   ut_asserteq_strn(EXPECT_REF, (char *)pptr + 0xe);
+   ut_asserteq(ZERO_OP, pptr[0x1a]);
+   ut_asserteq(ZERO_OP, pptr[0x1b]);
+   ut_asserteq(ONE_OP, pptr[0x1c]);
+
+   return 0;
+}
+DM_TEST(dm_test_acpi_dp_gpio, 0);
-- 
2.26.2.645.ge9eca65c58-goog



[PATCH v2 17/35] acpi: Support writing a name

2020-05-10 Thread Simon Glass
ACPI supports storing names which are made up of multiple path components.
Several special cases are supported. Add a function to emit a name.

Signed-off-by: Simon Glass 
---

Changes in v2: None
Changes in v1: None

 include/acpi/acpigen.h | 25 +++
 include/test/ut.h  | 17 
 lib/acpi/acpigen.c | 96 ++
 test/dm/acpigen.c  | 93 
 4 files changed, 231 insertions(+)

diff --git a/include/acpi/acpigen.h b/include/acpi/acpigen.h
index f65de53a76..0fecb7d57a 100644
--- a/include/acpi/acpigen.h
+++ b/include/acpi/acpigen.h
@@ -18,12 +18,15 @@ struct acpi_ctx;
 enum {
ZERO_OP = 0x00,
ONE_OP  = 0x01,
+   NAME_OP = 0x08,
BYTE_PREFIX = 0x0a,
WORD_PREFIX = 0x0b,
DWORD_PREFIX= 0x0c,
STRING_PREFIX   = 0x0d,
QWORD_PREFIX= 0x0e,
PACKAGE_OP  = 0x12,
+   DUAL_NAME_PREFIX= 0x2e,
+   MULTI_NAME_PREFIX   = 0x2f,
 };
 
 /**
@@ -157,4 +160,26 @@ void acpigen_write_integer(struct acpi_ctx *ctx, u64 data);
  * @str: String to write
  */
 void acpigen_write_string(struct acpi_ctx *ctx, const char *str);
+
+/**
+ * acpigen_emit_namestring() - Emit an ACPI name
+ *
+ * This writes out an ACPI name or path in the required special format. It does
+ * not add the NAME_OP prefix.
+ *
+ * @ctx: ACPI context pointer
+ * @namepath: Name / path to emit
+ */
+void acpigen_emit_namestring(struct acpi_ctx *ctx, const char *namepath);
+
+/**
+ * acpigen_write_name() - Write out an ACPI name
+ *
+ * This writes out an ACPI name or path in the required special format with a
+ * NAME_OP prefix.
+ *
+ * @ctx: ACPI context pointer
+ * @namepath: Name / path to emit
+ */
+void acpigen_write_name(struct acpi_ctx *ctx, const char *namepath);
 #endif
diff --git a/include/test/ut.h b/include/test/ut.h
index 7ddd6e8872..99bbb1230c 100644
--- a/include/test/ut.h
+++ b/include/test/ut.h
@@ -134,6 +134,23 @@ int ut_check_console_dump(struct unit_test_state *uts, int 
total_bytes);
}   \
 }
 
+/*
+ * Assert that two string expressions are equal, up to length of the
+ * first
+ */
+#define ut_asserteq_strn(expr1, expr2) {   \
+   const char *_val1 = (expr1), *_val2 = (expr2);  \
+   int _len = strlen(_val1);   \
+   \
+   if (memcmp(_val1, _val2, _len)) {   \
+   ut_failf(uts, __FILE__, __LINE__, __func__, \
+#expr1 " = " #expr2,   \
+"Expected \"%.*s\", got \"%.*s\"", \
+_len, _val1, _len, _val2); \
+   return CMD_RET_FAILURE; \
+   }   \
+}
+
 /* Assert that two memory areas are equal */
 #define ut_asserteq_mem(expr1, expr2, len) {   \
const u8 *_val1 = (u8 *)(expr1), *_val2 = (u8 *)(expr2);\
diff --git a/lib/acpi/acpigen.c b/lib/acpi/acpigen.c
index a4ac3c8875..eae2f605ed 100644
--- a/lib/acpi/acpigen.c
+++ b/lib/acpi/acpigen.c
@@ -152,3 +152,99 @@ void acpigen_write_string(struct acpi_ctx *ctx, const char 
*str)
acpigen_emit_byte(ctx, STRING_PREFIX);
acpigen_emit_string(ctx, str);
 }
+
+/*
+ * The naming conventions for ACPI namespace names are a bit tricky as
+ * each element has to be 4 chars wide ("All names are a fixed 32 bits.")
+ * and "By convention, when an ASL compiler pads a name shorter than 4
+ * characters, it is done so with trailing underscores ('_')".
+ *
+ * Check sections 5.3, 20.2.2 and 20.4 of ACPI spec 6.3 for details.
+ */
+static void acpigen_emit_simple_namestring(struct acpi_ctx *ctx,
+  const char *name)
+{
+   const char *ptr;
+   int i;
+
+   for (i = 0, ptr = name; i < 4; i++) {
+   if (!*ptr || *ptr == '.')
+   acpigen_emit_byte(ctx, '_');
+   else
+   acpigen_emit_byte(ctx, *ptr++);
+   }
+}
+
+static void acpigen_emit_double_namestring(struct acpi_ctx *ctx,
+  const char *name, int dotpos)
+{
+   acpigen_emit_byte(ctx, DUAL_NAME_PREFIX);
+   acpigen_emit_simple_namestring(ctx, name);
+   acpigen_emit_simple_namestring(ctx, [dotpos + 1]);
+}
+
+static void acpigen_emit_multi_namestring(struct acpi_ctx *ctx,
+ const char *name)
+{
+   unsigned char *pathlen;
+   int count = 0;
+
+   acpigen_emit_byte(ctx, MULTI_NAME_PREFIX);
+   

[PATCH v2 18/35] acpi: Support writing a UUID

2020-05-10 Thread Simon Glass
ACPI supports writing a UUID in a special format. Add a function to handle
this.

Signed-off-by: Simon Glass 
---

Changes in v2: None
Changes in v1: None

 include/acpi/acpigen.h | 13 +
 lib/acpi/acpigen.c | 39 +++
 test/dm/acpigen.c  | 33 +
 3 files changed, 85 insertions(+)

diff --git a/include/acpi/acpigen.h b/include/acpi/acpigen.h
index 0fecb7d57a..f35467f029 100644
--- a/include/acpi/acpigen.h
+++ b/include/acpi/acpigen.h
@@ -24,6 +24,7 @@ enum {
DWORD_PREFIX= 0x0c,
STRING_PREFIX   = 0x0d,
QWORD_PREFIX= 0x0e,
+   BUFFER_OP   = 0x11,
PACKAGE_OP  = 0x12,
DUAL_NAME_PREFIX= 0x2e,
MULTI_NAME_PREFIX   = 0x2f,
@@ -182,4 +183,16 @@ void acpigen_emit_namestring(struct acpi_ctx *ctx, const 
char *namepath);
  * @namepath: Name / path to emit
  */
 void acpigen_write_name(struct acpi_ctx *ctx, const char *namepath);
+
+/**
+ * acpigen_write_uuid() - Write a UUID
+ *
+ * This writes out a UUID in the format used by ACPI, with a BUFFER_OP prefix.
+ *
+ * @ctx: ACPI context pointer
+ * @uuid: UUID to write in the form aabbccdd-eeff-gghh-iijj-kkllmmnnoopp
+ * @return 0 if OK, -EINVAL if the format is incorrect
+ */
+int acpigen_write_uuid(struct acpi_ctx *ctx, const char *uuid);
+
 #endif
diff --git a/lib/acpi/acpigen.c b/lib/acpi/acpigen.c
index eae2f605ed..f781ad4d87 100644
--- a/lib/acpi/acpigen.c
+++ b/lib/acpi/acpigen.c
@@ -11,6 +11,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 
@@ -248,3 +249,41 @@ void acpigen_write_name(struct acpi_ctx *ctx, const char 
*namepath)
acpigen_emit_byte(ctx, NAME_OP);
acpigen_emit_namestring(ctx, namepath);
 }
+
+/*
+ * ToUUID(uuid)
+ *
+ * ACPI 6.3 Section 19.6.142 table 19-438 defines a special output order for 
the
+ * bytes that make up a UUID Buffer object:
+ *
+ * UUID byte order for input to this function:
+ *   aabbccdd-eeff-gghh-iijj-kkllmmnnoopp
+ *
+ * UUID byte order output by this function:
+ *   ddccbbaa-ffee-hhgg-iijj-kkllmmnnoopp
+ */
+#define UUID_LEN 16
+int acpigen_write_uuid(struct acpi_ctx *ctx, const char *uuid)
+{
+   u8 buf[UUID_LEN];
+   int ret;
+
+   /* Parse UUID string into bytes */
+   ret = uuid_str_to_bin(uuid, buf, UUID_STR_FORMAT_GUID);
+   if (ret)
+   return log_msg_ret("bad hex", -EINVAL);
+
+   /* BufferOp */
+   acpigen_emit_byte(ctx, BUFFER_OP);
+   acpigen_write_len_f(ctx);
+
+   /* Buffer length in bytes */
+   acpigen_write_word(ctx, UUID_LEN);
+
+   /* Output UUID in expected order */
+   acpigen_emit_stream(ctx, (char *)buf, UUID_LEN);
+
+   acpigen_pop_len(ctx);
+
+   return 0;
+}
diff --git a/test/dm/acpigen.c b/test/dm/acpigen.c
index 0f56262a28..db8cad47d8 100644
--- a/test/dm/acpigen.c
+++ b/test/dm/acpigen.c
@@ -588,3 +588,36 @@ static int dm_test_acpi_name(struct unit_test_state *uts)
return 0;
 }
 DM_TEST(dm_test_acpi_name, 0);
+
+/* Test writing a UUID */
+static int dm_test_acpi_uuid(struct unit_test_state *uts)
+{
+   struct acpi_ctx *ctx;
+   u8 *ptr;
+
+   ut_assertok(alloc_context());
+
+   ptr = acpigen_get_current(ctx);
+
+   ut_assertok(acpigen_write_uuid(ctx,
+  "dbb8e3e6-5886-4ba6-8795-1319f52a966b"));
+   ut_asserteq(23, acpigen_get_current(ctx) - ptr);
+   ut_asserteq(BUFFER_OP, ptr[0]);
+   ut_asserteq(22, get_length(ptr + 1));
+   ut_asserteq(0xdbb8e3e6, get_unaligned((u32 *)(ptr + 7)));
+   ut_asserteq(0x5886, get_unaligned((u16 *)(ptr + 11)));
+   ut_asserteq(0x4ba6, get_unaligned((u16 *)(ptr + 13)));
+   ut_asserteq(0x9587, get_unaligned((u16 *)(ptr + 15)));
+   ut_asserteq(0x2af51913, get_unaligned((u32 *)(ptr + 17)));
+   ut_asserteq(0x6b96, get_unaligned((u16 *)(ptr + 21)));
+
+   /* Try a bad UUID */
+   ut_asserteq(-EINVAL,
+   acpigen_write_uuid(ctx,
+  "dbb8e3e6-5886-4ba6x8795-1319f52a966b"));
+
+   free_context();
+
+   return 0;
+}
+DM_TEST(dm_test_acpi_uuid, 0);
-- 
2.26.2.645.ge9eca65c58-goog



[PATCH v2 21/35] acpi: Support copying properties from device tree to ACPI

2020-05-10 Thread Simon Glass
Some drivers in Linux support both device tree and ACPI. U-Boot itself
uses Linux device-tree bindings for its own configuration but does not use
ACPI.

It is convenient to copy these values over to the device tree for passing
to linux. Add some convenience functions to help with this.

Signed-off-by: Simon Glass 
---

Changes in v2: None
Changes in v1: None

 arch/sandbox/dts/test.dts |  1 +
 include/acpi/acpi_dp.h| 50 +
 lib/acpi/acpi_dp.c| 52 ++
 test/dm/acpi_dp.c | 67 +++
 4 files changed, 170 insertions(+)

diff --git a/arch/sandbox/dts/test.dts b/arch/sandbox/dts/test.dts
index b802c1c909..a90395fa62 100644
--- a/arch/sandbox/dts/test.dts
+++ b/arch/sandbox/dts/test.dts
@@ -109,6 +109,7 @@
uint-value = <(-1234)>;
int64-value = /bits/ 64 <0x>;
int-array = <5678 9123 4567>;
+   str-value = "test string";
interrupts-extended = < 3 0>;
};
 
diff --git a/include/acpi/acpi_dp.h b/include/acpi/acpi_dp.h
index f42602405a..9e5a70b286 100644
--- a/include/acpi/acpi_dp.h
+++ b/include/acpi/acpi_dp.h
@@ -230,4 +230,54 @@ struct acpi_dp *acpi_dp_add_gpio(struct acpi_dp *dp, const 
char *name,
  */
 int acpi_dp_write(struct acpi_ctx *ctx, struct acpi_dp *table);
 
+/**
+ * acpi_dp_ofnode_copy_int() - Copy a property from device tree to DP
+ *
+ * This copies an integer property from the device tree to the ACPI DP table.
+ *
+ * @dev: Device to copy from
+ * @dp: DP to copy to
+ * @prop: Property name to copy
+ * @return 0 if OK, -ve on error
+ */
+int acpi_dp_ofnode_copy_int(ofnode node, struct acpi_dp *dp, const char *prop);
+
+/**
+ * acpi_dp_dev_copy_str() - Copy a property from device tree to DP
+ *
+ * This copies a string property from the device tree to the ACPI DP table.
+ *
+ * @dev: Device to copy from
+ * @dp: DP to copy to
+ * @prop: Property name to copy
+ * @return 0 if OK, -ve on error
+ */
+int acpi_dp_ofnode_copy_str(ofnode node, struct acpi_dp *dp, const char *prop);
+
+/**
+ * acpi_dp_dev_copy_int() - Copy a property from device tree to DP
+ *
+ * This copies an integer property from the device tree to the ACPI DP table.
+ *
+ * @dev: Device to copy from
+ * @dp: DP to copy to
+ * @prop: Property name to copy
+ * @return 0 if OK, -ve on error
+ */
+int acpi_dp_dev_copy_int(const struct udevice *dev, struct acpi_dp *dp,
+const char *prop);
+
+/**
+ * acpi_dp_dev_copy_str() - Copy a property from device tree to DP
+ *
+ * This copies a string property from the device tree to the ACPI DP table.
+ *
+ * @dev: Device to copy from
+ * @dp: DP to copy to
+ * @prop: Property name to copy
+ * @return 0 if OK, -ve on error
+ */
+int acpi_dp_dev_copy_str(const struct udevice *dev, struct acpi_dp *dp,
+const char *prop);
+
 #endif
diff --git a/lib/acpi/acpi_dp.c b/lib/acpi/acpi_dp.c
index 4ba5d555a0..819e51005a 100644
--- a/lib/acpi/acpi_dp.c
+++ b/lib/acpi/acpi_dp.c
@@ -343,3 +343,55 @@ struct acpi_dp *acpi_dp_add_gpio(struct acpi_dp *dp, const 
char *name,
 
return gpio;
 }
+
+int acpi_dp_ofnode_copy_int(ofnode node, struct acpi_dp *dp, const char *prop)
+{
+   int ret;
+   u32 val = 0;
+
+   ret = ofnode_read_u32(node, prop, );
+   if (ret)
+   return ret;
+   acpi_dp_add_integer(dp, prop, val);
+
+   return ret;
+}
+
+int acpi_dp_ofnode_copy_str(ofnode node, struct acpi_dp *dp, const char *prop)
+{
+   const char *val;
+
+   val = ofnode_read_string(node, prop);
+   if (!val)
+   return -EINVAL;
+   acpi_dp_add_string(dp, prop, val);
+
+   return 0;
+}
+
+int acpi_dp_dev_copy_int(const struct udevice *dev, struct acpi_dp *dp,
+const char *prop)
+{
+   int ret;
+   u32 val = 0;
+
+   ret = dev_read_u32(dev, prop, );
+   if (ret)
+   return ret;
+   acpi_dp_add_integer(dp, prop, val);
+
+   return ret;
+}
+
+int acpi_dp_dev_copy_str(const struct udevice *dev, struct acpi_dp *dp,
+const char *prop)
+{
+   const char *val;
+
+   val = dev_read_string(dev, prop);
+   if (!val)
+   return -EINVAL;
+   acpi_dp_add_string(dp, prop, val);
+
+   return 0;
+}
diff --git a/test/dm/acpi_dp.c b/test/dm/acpi_dp.c
index d6a054a98b..c1ab580cd2 100644
--- a/test/dm/acpi_dp.c
+++ b/test/dm/acpi_dp.c
@@ -442,3 +442,70 @@ static int dm_test_acpi_dp_gpio(struct unit_test_state 
*uts)
return 0;
 }
 DM_TEST(dm_test_acpi_dp_gpio, 0);
+
+/* Test copying info from the device tree to ACPI tables */
+static int dm_test_acpi_dp_copy(struct unit_test_state *uts)
+{
+   struct acpi_ctx *ctx;
+   struct udevice *dev;
+   struct acpi_dp *dp;
+   ofnode node;
+   u8 *ptr;
+
+   ut_assertok(alloc_context());
+
+   dp = acpi_dp_new_table("FRED");
+   

[PATCH v2 16/35] acpi: Support writing a string

2020-05-10 Thread Simon Glass
ACPI supports storing a simple nul-terminated string. Add support for
this.

Signed-off-by: Simon Glass 
---

Changes in v2: None
Changes in v1: None

 include/acpi/acpigen.h | 10 ++
 lib/acpi/acpigen.c |  6 ++
 test/dm/acpigen.c  | 32 ++--
 3 files changed, 46 insertions(+), 2 deletions(-)

diff --git a/include/acpi/acpigen.h b/include/acpi/acpigen.h
index a4edcb097b..f65de53a76 100644
--- a/include/acpi/acpigen.h
+++ b/include/acpi/acpigen.h
@@ -21,6 +21,7 @@ enum {
BYTE_PREFIX = 0x0a,
WORD_PREFIX = 0x0b,
DWORD_PREFIX= 0x0c,
+   STRING_PREFIX   = 0x0d,
QWORD_PREFIX= 0x0e,
PACKAGE_OP  = 0x12,
 };
@@ -147,4 +148,13 @@ char *acpigen_write_package(struct acpi_ctx *ctx, int 
nr_el);
  */
 void acpigen_write_integer(struct acpi_ctx *ctx, u64 data);
 
+/**
+ * acpigen_write_string() - Write a string
+ *
+ * This writes a STRING_PREFIX followed by a nul-terminated string
+ *
+ * @ctx: ACPI context pointer
+ * @str: String to write
+ */
+void acpigen_write_string(struct acpi_ctx *ctx, const char *str);
 #endif
diff --git a/lib/acpi/acpigen.c b/lib/acpi/acpigen.c
index ece0111320..a4ac3c8875 100644
--- a/lib/acpi/acpigen.c
+++ b/lib/acpi/acpigen.c
@@ -146,3 +146,9 @@ void acpigen_emit_string(struct acpi_ctx *ctx, const char 
*str)
acpigen_emit_stream(ctx, str, str ? strlen(str) : 0);
acpigen_emit_byte(ctx, '\0'); /* NUL */
 }
+
+void acpigen_write_string(struct acpi_ctx *ctx, const char *str)
+{
+   acpigen_emit_byte(ctx, STRING_PREFIX);
+   acpigen_emit_string(ctx, str);
+}
diff --git a/test/dm/acpigen.c b/test/dm/acpigen.c
index 9350943a12..dc6993b7a7 100644
--- a/test/dm/acpigen.c
+++ b/test/dm/acpigen.c
@@ -20,6 +20,7 @@
 #include 
 
 #define TEST_STRING"frogmore"
+#define TEST_STRING2   "ranch"
 #define TEST_STREAM2   "\xfa\xde"
 
 #define TEST_INT8  0x7d
@@ -400,7 +401,7 @@ static int dm_test_acpi_len(struct unit_test_state *uts)
 }
 DM_TEST(dm_test_acpi_len, 0);
 
-/* Test emitting a package */
+/* Test writing a package */
 static int dm_test_acpi_package(struct unit_test_state *uts)
 {
struct acpi_ctx *ctx;
@@ -427,7 +428,7 @@ static int dm_test_acpi_package(struct unit_test_state *uts)
 }
 DM_TEST(dm_test_acpi_package, 0);
 
-/* Test emitting an integer */
+/* Test writing an integer */
 static int dm_test_acpi_integer(struct unit_test_state *uts)
 {
struct acpi_ctx *ctx;
@@ -467,3 +468,30 @@ static int dm_test_acpi_integer(struct unit_test_state 
*uts)
return 0;
 }
 DM_TEST(dm_test_acpi_integer, 0);
+
+/* Test writing a string */
+static int dm_test_acpi_string(struct unit_test_state *uts)
+{
+   struct acpi_ctx *ctx;
+   u8 *ptr;
+
+   ut_assertok(alloc_context());
+
+   ptr = acpigen_get_current(ctx);
+
+   acpigen_write_string(ctx, TEST_STRING);
+   acpigen_write_string(ctx, TEST_STRING2);
+
+   ut_asserteq(2 + sizeof(TEST_STRING) + sizeof(TEST_STRING2),
+   acpigen_get_current(ctx) - ptr);
+   ut_asserteq(STRING_PREFIX, ptr[0]);
+   ut_asserteq_str(TEST_STRING, (char *)ptr + 1);
+   ptr += 1 + sizeof(TEST_STRING);
+   ut_asserteq(STRING_PREFIX, ptr[0]);
+   ut_asserteq_str(TEST_STRING2, (char *)ptr + 1);
+
+   free_context();
+
+   return 0;
+}
+DM_TEST(dm_test_acpi_string, 0);
-- 
2.26.2.645.ge9eca65c58-goog



[PATCH v2 14/35] acpigen: Support writing a package

2020-05-10 Thread Simon Glass
A package collects together several elements. Add an easy way of writing
a package header and updating its length later.

Signed-off-by: Simon Glass 
---

Changes in v2: None
Changes in v1: None

 include/acpi/acpigen.h | 62 ++
 lib/acpi/acpigen.c | 12 
 test/dm/acpigen.c  | 27 ++
 3 files changed, 101 insertions(+)

diff --git a/include/acpi/acpigen.h b/include/acpi/acpigen.h
index 31366f5e34..52e0b75128 100644
--- a/include/acpi/acpigen.h
+++ b/include/acpi/acpigen.h
@@ -14,6 +14,11 @@
 
 struct acpi_ctx;
 
+/* ACPI Op/Prefix codes */
+enum {
+   PACKAGE_OP  = 0x12,
+};
+
 /**
  * acpigen_get_current() - Get the current ACPI code output pointer
  *
@@ -65,7 +70,64 @@ void acpigen_emit_stream(struct acpi_ctx *ctx, const char 
*data, int size);
  */
 void acpigen_emit_string(struct acpi_ctx *ctx, const char *str);
 
+/**
+ * acpigen_write_len_f() - Write a 'forward' length placeholder
+ *
+ * This adds space for a length value in the ACPI stream and pushes the current
+ * position (before the length) on the stack. After calling this you can write
+ * some data and then call acpigen_pop_len() to update the length value.
+ *
+ * Usage:
+ *
+ *acpigen_write_len_f() --\
+ *acpigen_write...()  |
+ *acpigen_write...()  |
+ *  acpigen_write_len_f() --\ |
+ *  acpigen_write...()  | |
+ *  acpigen_write...()  | |
+ *  acpigen_pop_len() --/ |
+ *acpigen_write...()  |
+ *acpigen_pop_len() --/
+ *
+ * @ctx: ACPI context pointer
+ */
 void acpigen_write_len_f(struct acpi_ctx *ctx);
+
+/**
+ * acpigen_pop_len() - Update the previously stacked length placeholder
+ *
+ * Call this after the data for the block gas been written. It updates the
+ * top length value in the stack and pops it off.
+ *
+ * @ctx: ACPI context pointer
+ */
 void acpigen_pop_len(struct acpi_ctx *ctx);
 
+/**
+ * acpigen_write_package() - Start writing a package
+ *
+ * A package collects together a number of elements in the ACPI code. To write
+ * a package use:
+ *
+ * acpigen_write_package(ctx, 3);
+ * ...write things
+ * acpigen_pop_len()
+ *
+ * If you don't know the number of elements in advance, acpigen_write_package()
+ * returns a pointer to the value so you can update it later:
+ *
+ * char *num_elements = acpigen_write_package(ctx, 0);
+ * ...write things
+ * *num_elements += 1;
+ * ...write things
+ * *num_elements += 1;
+ * acpigen_pop_len()
+ *
+ * @ctx: ACPI context pointer
+ * @nr_el: Number of elements (0 if not known)
+ * @returns pointer to the number of elements, which can be updated by the
+ * caller if needed
+ */
+char *acpigen_write_package(struct acpi_ctx *ctx, int nr_el);
+
 #endif
diff --git a/lib/acpi/acpigen.c b/lib/acpi/acpigen.c
index bd1fa24fb6..e1fd1f6b6a 100644
--- a/lib/acpi/acpigen.c
+++ b/lib/acpi/acpigen.c
@@ -70,6 +70,18 @@ void acpigen_pop_len(struct acpi_ctx *ctx)
p[2] = (len >> 12 & 0xff);
 }
 
+char *acpigen_write_package(struct acpi_ctx *ctx, int nr_el)
+{
+   char *p;
+
+   acpigen_emit_byte(ctx, PACKAGE_OP);
+   acpigen_write_len_f(ctx);
+   p = ctx->current;
+   acpigen_emit_byte(ctx, nr_el);
+
+   return p;
+}
+
 void acpigen_emit_stream(struct acpi_ctx *ctx, const char *data, int size)
 {
int i;
diff --git a/test/dm/acpigen.c b/test/dm/acpigen.c
index f9c15b7503..be81068759 100644
--- a/test/dm/acpigen.c
+++ b/test/dm/acpigen.c
@@ -394,3 +394,30 @@ static int dm_test_acpi_len(struct unit_test_state *uts)
return 0;
 }
 DM_TEST(dm_test_acpi_len, 0);
+
+/* Test emitting a package */
+static int dm_test_acpi_package(struct unit_test_state *uts)
+{
+   struct acpi_ctx *ctx;
+   char *num_elements;
+   u8 *ptr;
+
+   ut_assertok(alloc_context());
+
+   ptr = acpigen_get_current(ctx);
+
+   num_elements = acpigen_write_package(ctx, 3);
+   ut_asserteq_ptr(num_elements, ptr + 4);
+
+   /* For easy of testing, just emit a byte, not valid package contents */
+   acpigen_emit_byte(ctx, 0x23);
+   acpigen_pop_len(ctx);
+   ut_asserteq(PACKAGE_OP, ptr[0]);
+   ut_asserteq(5, get_length(ptr + 1));
+   ut_asserteq(3, ptr[4]);
+
+   free_context();
+
+   return 0;
+}
+DM_TEST(dm_test_acpi_package, 0);
-- 
2.26.2.645.ge9eca65c58-goog



[PATCH v2 12/35] acpi: Support generation of SPI descriptor

2020-05-10 Thread Simon Glass
Add a function to write a SPI descriptor to the generated ACPI code.

Signed-off-by: Simon Glass 
---

Changes in v2:
- Fix memset of SPI descriptor

Changes in v1: None

 drivers/spi/sandbox_spi.c  |  11 
 include/acpi/acpi_device.h |  36 +++
 include/spi.h  |   4 +-
 lib/acpi/acpi_device.c | 121 +
 test/dm/acpigen.c  |  36 +++
 5 files changed, 206 insertions(+), 2 deletions(-)

diff --git a/drivers/spi/sandbox_spi.c b/drivers/spi/sandbox_spi.c
index b0a46c8868..4264acc953 100644
--- a/drivers/spi/sandbox_spi.c
+++ b/drivers/spi/sandbox_spi.c
@@ -21,6 +21,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 
 #ifndef CONFIG_SPI_IDLE_VAL
@@ -133,6 +134,15 @@ static int sandbox_spi_get_mmap(struct udevice *dev, ulong 
*map_basep,
return 0;
 }
 
+static int sandbox_spi_get_name(const struct udevice *dev, char *out_name)
+{
+   return acpi_copy_name(out_name, "SSPI");
+}
+
+struct acpi_ops sandbox_spi_acpi_ops = {
+   .get_name   = sandbox_spi_get_name,
+};
+
 static const struct dm_spi_ops sandbox_spi_ops = {
.xfer   = sandbox_spi_xfer,
.set_speed  = sandbox_spi_set_speed,
@@ -151,4 +161,5 @@ U_BOOT_DRIVER(spi_sandbox) = {
.id = UCLASS_SPI,
.of_match = sandbox_spi_ids,
.ops= _spi_ops,
+   ACPI_OPS_PTR(_spi_acpi_ops)
 };
diff --git a/include/acpi/acpi_device.h b/include/acpi/acpi_device.h
index cf1ac695a7..d43ab4ba36 100644
--- a/include/acpi/acpi_device.h
+++ b/include/acpi/acpi_device.h
@@ -10,6 +10,7 @@
 #define __ACPI_DEVICE_H
 
 #include 
+#include 
 #include 
 
 struct acpi_ctx;
@@ -207,6 +208,29 @@ struct acpi_i2c {
const char *resource;
 };
 
+/**
+ * struct acpi_spi - representation of an ACPI SPI device
+ *
+ * @device_select: Chip select used by this device (typically 0)
+ * @device_select_polarity: Polarity for the device
+ * @wire_mode: Number of wires used for SPI
+ * @speed: Bus speed in Hz
+ * @data_bit_length: Word length for SPI (typically 8)
+ * @clock_phase: Clock phase to capture data
+ * @clock_polarity: Bus polarity
+ * @resource: Resource name for the SPI controller
+ */
+struct acpi_spi {
+   u16 device_select;
+   enum spi_polarity device_select_polarity;
+   enum spi_wire_mode wire_mode;
+   unsigned int speed;
+   u8 data_bit_length;
+   enum spi_clock_phase clock_phase;
+   enum spi_polarity clock_polarity;
+   const char *resource;
+};
+
 /**
  * acpi_device_path() - Get the full path to an ACPI device
  *
@@ -306,4 +330,16 @@ int acpi_device_write_interrupt_or_gpio(struct acpi_ctx 
*ctx,
  */
 int acpi_device_write_i2c_dev(struct acpi_ctx *ctx, const struct udevice *dev);
 
+/**
+ * acpi_device_write_spi_dev() - Write a SPI device to ACPI
+ *
+ * This writes a serial bus descriptor for the SPI device so that ACPI can use
+ * it
+ *
+ * @ctx: ACPI context pointer
+ * @dev: SPI device to write
+ * @return 0 if OK, -ve on error
+ */
+int acpi_device_write_spi_dev(struct acpi_ctx *ctx, const struct udevice *dev);
+
 #endif
diff --git a/include/spi.h b/include/spi.h
index 5cc6d6e008..f34533f54e 100644
--- a/include/spi.h
+++ b/include/spi.h
@@ -13,8 +13,8 @@
 #include 
 
 /* SPI mode flags */
-#define SPI_CPHA   BIT(0)  /* clock phase */
-#define SPI_CPOL   BIT(1)  /* clock polarity */
+#define SPI_CPHA   BIT(0)  /* clock phase (1 = SPI_CLOCK_PHASE_SECOND) */
+#define SPI_CPOL   BIT(1)  /* clock polarity (1 = SPI_POLARITY_HIGH) */
 #define SPI_MODE_0 (0|0)   /* (original MicroWire) */
 #define SPI_MODE_1 (0|SPI_CPHA)
 #define SPI_MODE_2 (SPI_CPOL|0)
diff --git a/lib/acpi/acpi_device.c b/lib/acpi/acpi_device.c
index 0136a0bdc9..3d5dc746f8 100644
--- a/lib/acpi/acpi_device.c
+++ b/lib/acpi/acpi_device.c
@@ -484,3 +484,124 @@ int acpi_device_write_i2c_dev(struct acpi_ctx *ctx, const 
struct udevice *dev)
 
return 0;
 }
+
+#ifdef CONFIG_SPI
+/* ACPI 6.1 section 6.4.3.8.2.2 - SpiSerialBus() */
+void acpi_device_write_spi(struct acpi_ctx *ctx, const struct acpi_spi *spi)
+{
+   void *desc_length, *type_length;
+   u16 flags = 0;
+
+   /* Byte 0: Descriptor Type */
+   acpigen_emit_byte(ctx, ACPI_DESCRIPTOR_SERIAL_BUS);
+
+   /* Byte 1+2: Length (filled in later) */
+   desc_length = acpi_device_write_zero_len(ctx);
+
+   /* Byte 3: Revision ID */
+   acpigen_emit_byte(ctx, ACPI_SPI_SERIAL_BUS_REVISION_ID);
+
+   /* Byte 4: Resource Source Index is Reserved */
+   acpigen_emit_byte(ctx, 0);
+
+   /* Byte 5: Serial Bus Type is SPI */
+   acpigen_emit_byte(ctx, ACPI_SERIAL_BUS_TYPE_SPI);
+
+   /*
+* Byte 6: Flags
+*  [7:2]: 0 => Reserved
+*[1]: 1 => ResourceConsumer
+*[0]: 0 => ControllerInitiated
+*/
+   acpigen_emit_byte(ctx, 1 << 1);
+
+   /*
+* Byte 7-8: Type Specific Flags
+

[PATCH v2 11/35] acpi: Support generation of I2C descriptor

2020-05-10 Thread Simon Glass
Add a function to write a GPIO descriptor to the generated ACPI code.

Signed-off-by: Simon Glass 
---

Changes in v2:
- Fix memset of I2C descriptor

Changes in v1: None

 drivers/i2c/sandbox_i2c.c  |  11 
 drivers/rtc/sandbox_rtc.c  |  13 +
 include/acpi/acpi_device.h |  36 +
 lib/acpi/acpi_device.c | 103 +
 test/dm/acpigen.c  |  32 
 5 files changed, 195 insertions(+)

diff --git a/drivers/i2c/sandbox_i2c.c b/drivers/i2c/sandbox_i2c.c
index f4ae2397a0..125026da90 100644
--- a/drivers/i2c/sandbox_i2c.c
+++ b/drivers/i2c/sandbox_i2c.c
@@ -11,6 +11,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 
@@ -83,6 +84,15 @@ static int sandbox_i2c_xfer(struct udevice *bus, struct 
i2c_msg *msg,
return ops->xfer(emul, msg, nmsgs);
 }
 
+static int sandbox_i2c_get_name(const struct udevice *dev, char *out_name)
+{
+   return acpi_copy_name(out_name, "SI2C");
+}
+
+struct acpi_ops sandbox_i2c_acpi_ops = {
+   .get_name   = sandbox_i2c_get_name,
+};
+
 static const struct dm_i2c_ops sandbox_i2c_ops = {
.xfer   = sandbox_i2c_xfer,
 };
@@ -98,4 +108,5 @@ U_BOOT_DRIVER(i2c_sandbox) = {
.of_match = sandbox_i2c_ids,
.ops= _i2c_ops,
.priv_auto_alloc_size = sizeof(struct sandbox_i2c_priv),
+   ACPI_OPS_PTR(_i2c_acpi_ops)
 };
diff --git a/drivers/rtc/sandbox_rtc.c b/drivers/rtc/sandbox_rtc.c
index b08d758a74..f2906c3397 100644
--- a/drivers/rtc/sandbox_rtc.c
+++ b/drivers/rtc/sandbox_rtc.c
@@ -9,6 +9,7 @@
 #include 
 #include 
 #include 
+#include 
 
 #define REG_COUNT 0x80
 
@@ -84,6 +85,17 @@ static int sandbox_rtc_write8(struct udevice *dev, unsigned 
int reg, int val)
return dm_i2c_reg_write(dev, reg, val);
 }
 
+#if CONFIG_IS_ENABLED(ACPIGEN)
+static int sandbox_rtc_get_name(const struct udevice *dev, char *out_name)
+{
+   return acpi_copy_name(out_name, "RTCC");
+}
+
+struct acpi_ops sandbox_rtc_acpi_ops = {
+   .get_name   = sandbox_rtc_get_name,
+};
+#endif
+
 static const struct rtc_ops sandbox_rtc_ops = {
.get = sandbox_rtc_get,
.set = sandbox_rtc_set,
@@ -102,4 +114,5 @@ U_BOOT_DRIVER(rtc_sandbox) = {
.id = UCLASS_RTC,
.of_match = sandbox_rtc_ids,
.ops= _rtc_ops,
+   ACPI_OPS_PTR(_rtc_acpi_ops)
 };
diff --git a/include/acpi/acpi_device.h b/include/acpi/acpi_device.h
index 70c151d150..cf1ac695a7 100644
--- a/include/acpi/acpi_device.h
+++ b/include/acpi/acpi_device.h
@@ -9,6 +9,7 @@
 #ifndef __ACPI_DEVICE_H
 #define __ACPI_DEVICE_H
 
+#include 
 #include 
 
 struct acpi_ctx;
@@ -183,6 +184,29 @@ struct acpi_gpio {
enum acpi_gpio_polarity polarity;
 };
 
+/* ACPI Descriptors for Serial Bus interfaces */
+#define ACPI_SERIAL_BUS_TYPE_I2C   1
+#define ACPI_SERIAL_BUS_TYPE_SPI   2
+#define ACPI_I2C_SERIAL_BUS_REVISION_ID1 /* TODO: upgrade to 2 
*/
+#define ACPI_I2C_TYPE_SPECIFIC_REVISION_ID 1
+#define ACPI_SPI_SERIAL_BUS_REVISION_ID1
+#define ACPI_SPI_TYPE_SPECIFIC_REVISION_ID 1
+
+/**
+ * struct acpi_i2c - representation of an ACPI I2C device
+ *
+ * @address: 7-bit or 10-bit I2C address
+ * @mode_10bit: Which address size is used
+ * @speed: Bus speed in Hz
+ * @resource: Resource name for the I2C controller
+ */
+struct acpi_i2c {
+   u16 address;
+   enum i2c_address_mode mode_10bit;
+   enum i2c_speed_rate speed;
+   const char *resource;
+};
+
 /**
  * acpi_device_path() - Get the full path to an ACPI device
  *
@@ -270,4 +294,16 @@ int acpi_device_write_gpio_desc(struct acpi_ctx *ctx,
 int acpi_device_write_interrupt_or_gpio(struct acpi_ctx *ctx,
struct udevice *dev, const char *prop);
 
+/**
+ * acpi_device_write_i2c_dev() - Write an I2C device to ACPI
+ *
+ * This creates a I2cSerialBus descriptor for an I2C device, including
+ * information ACPI needs to use it.
+ *
+ * @ctx: ACPI context pointer
+ * @dev: I2C device to write
+ * @return 0 if OK, -ve on error
+ */
+int acpi_device_write_i2c_dev(struct acpi_ctx *ctx, const struct udevice *dev);
+
 #endif
diff --git a/lib/acpi/acpi_device.c b/lib/acpi/acpi_device.c
index 423b91cfd2..0136a0bdc9 100644
--- a/lib/acpi/acpi_device.c
+++ b/lib/acpi/acpi_device.c
@@ -381,3 +381,106 @@ int acpi_device_write_interrupt_or_gpio(struct acpi_ctx 
*ctx,
 
return 0;
 }
+
+/* ACPI 6.3 section 6.4.3.8.2.1 - I2cSerialBus() */
+static void acpi_device_write_i2c(struct acpi_ctx *ctx,
+ const struct acpi_i2c *i2c)
+{
+   void *desc_length, *type_length;
+
+   /* Byte 0: Descriptor Type */
+   acpigen_emit_byte(ctx, ACPI_DESCRIPTOR_SERIAL_BUS);
+
+   /* Byte 1+2: Length (filled in later) */
+   desc_length = acpi_device_write_zero_len(ctx);
+
+   /* Byte 3: Revision ID */
+   acpigen_emit_byte(ctx, ACPI_I2C_SERIAL_BUS_REVISION_ID);
+
+   /* Byte 

[PATCH v2 13/35] acpigen: Support writing a length

2020-05-10 Thread Simon Glass
It is convenient to write a length value for preceding a block of data.
Of course the length is not known or is hard to calculate a priori. So add
a way to mark the start on a stack, so the length can be updated when
known.

Signed-off-by: Simon Glass 
---

Changes in v2: None
Changes in v1: None

 include/acpi/acpigen.h |  3 ++
 include/dm/acpi.h  |  7 +
 lib/acpi/acpigen.c | 33 ++
 test/dm/acpigen.c  | 64 --
 4 files changed, 105 insertions(+), 2 deletions(-)

diff --git a/include/acpi/acpigen.h b/include/acpi/acpigen.h
index 7365cce738..31366f5e34 100644
--- a/include/acpi/acpigen.h
+++ b/include/acpi/acpigen.h
@@ -65,4 +65,7 @@ void acpigen_emit_stream(struct acpi_ctx *ctx, const char 
*data, int size);
  */
 void acpigen_emit_string(struct acpi_ctx *ctx, const char *str);
 
+void acpigen_write_len_f(struct acpi_ctx *ctx);
+void acpigen_pop_len(struct acpi_ctx *ctx);
+
 #endif
diff --git a/include/dm/acpi.h b/include/dm/acpi.h
index 7563a4c60a..2bd03eaa0a 100644
--- a/include/dm/acpi.h
+++ b/include/dm/acpi.h
@@ -22,6 +22,9 @@
 /* Length of an ACPI name string including nul terminator */
 #define ACPI_NAME_MAX  (ACPI_NAME_LEN + 1)
 
+/* Number of nested objects supported */
+#define ACPIGEN_LENSTACK_SIZE 10
+
 #if !defined(__ACPI__)
 
 /**
@@ -34,12 +37,16 @@
  * adding a new table. The RSDP holds pointers to the RSDT and XSDT.
  * @rsdt: Pointer to the Root System Description Table
  * @xsdt: Pointer to the Extended System Description Table
+ * @len_stack: Stack of 'length' words to fix up later
+ * @ltop: Points to current top of stack (0 = empty)
  */
 struct acpi_ctx {
void *current;
struct acpi_rsdp *rsdp;
struct acpi_rsdt *rsdt;
struct acpi_xsdt *xsdt;
+   char *len_stack[ACPIGEN_LENSTACK_SIZE];
+   int ltop;
 };
 
 /**
diff --git a/lib/acpi/acpigen.c b/lib/acpi/acpigen.c
index 1223f0d1c4..bd1fa24fb6 100644
--- a/lib/acpi/acpigen.c
+++ b/lib/acpi/acpigen.c
@@ -10,6 +10,7 @@
 
 #include 
 #include 
+#include 
 #include 
 #include 
 
@@ -37,6 +38,38 @@ void acpigen_emit_dword(struct acpi_ctx *ctx, uint data)
acpigen_emit_byte(ctx, (data >> 24) & 0xff);
 }
 
+/*
+ * Maximum length for an ACPI object generated by this code,
+ *
+ * If you need to change this, change acpigen_write_len_f(ctx) and
+ * acpigen_pop_len(ctx)
+ */
+#define ACPIGEN_MAXLEN 0xf
+
+void acpigen_write_len_f(struct acpi_ctx *ctx)
+{
+   assert(ctx->ltop < (ACPIGEN_LENSTACK_SIZE - 1));
+   ctx->len_stack[ctx->ltop++] = ctx->current;
+   acpigen_emit_byte(ctx, 0);
+   acpigen_emit_byte(ctx, 0);
+   acpigen_emit_byte(ctx, 0);
+}
+
+void acpigen_pop_len(struct acpi_ctx *ctx)
+{
+   int len;
+   char *p;
+
+   assert(ctx->ltop > 0);
+   p = ctx->len_stack[--ctx->ltop];
+   len = ctx->current - (void *)p;
+   assert(len <= ACPIGEN_MAXLEN);
+   /* generate store length for 0xf max */
+   p[0] = (0x80 | (len & 0xf));
+   p[1] = (len >> 4 & 0xff);
+   p[2] = (len >> 12 & 0xff);
+}
+
 void acpigen_emit_stream(struct acpi_ctx *ctx, const char *data, int size)
 {
int i;
diff --git a/test/dm/acpigen.c b/test/dm/acpigen.c
index 3d580a23a5..f9c15b7503 100644
--- a/test/dm/acpigen.c
+++ b/test/dm/acpigen.c
@@ -22,7 +22,7 @@
 #define TEST_STRING"frogmore"
 #define TEST_STREAM2   "\xfa\xde"
 
-static int alloc_context(struct acpi_ctx **ctxp)
+static int alloc_context_size(struct acpi_ctx **ctxp, int size)
 {
struct acpi_ctx *ctx;
 
@@ -30,7 +30,8 @@ static int alloc_context(struct acpi_ctx **ctxp)
ctx = malloc(sizeof(*ctx));
if (!ctx)
return -ENOMEM;
-   ctx->current = malloc(150);
+   ctx->current = malloc(size);
+   ctx->ltop = 0;
if (!ctx->current)
return -ENOMEM;
*ctxp = ctx;
@@ -38,6 +39,11 @@ static int alloc_context(struct acpi_ctx **ctxp)
return 0;
 }
 
+static int alloc_context(struct acpi_ctx **ctxp)
+{
+   return alloc_context_size(ctxp, 150);
+}
+
 static void free_context(struct acpi_ctx **ctxp)
 {
free(*ctxp);
@@ -334,3 +340,57 @@ static int dm_test_acpi_spi(struct unit_test_state *uts)
return 0;
 }
 DM_TEST(dm_test_acpi_spi, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
+
+/**
+ * get_length() - decode a three-byte length field
+ *
+ * @ptr: Length encoded as per ACPI
+ * @return decoded length, or -EINVAL on error
+ */
+static int get_length(u8 *ptr)
+{
+   if (!(*ptr & 0x80))
+   return -EINVAL;
+
+   return (*ptr & 0xf) | ptr[1] << 4 | ptr[2] << 12;
+}
+
+/* Test emitting a length */
+static int dm_test_acpi_len(struct unit_test_state *uts)
+{
+   const int size = 0xc;
+   struct acpi_ctx *ctx;
+   u8 *ptr;
+   int i;
+
+   ut_assertok(alloc_context_size(, size));
+
+   ptr = acpigen_get_current(ctx);
+
+   /* Write a byte and a 3-byte length */
+   

[PATCH v2 15/35] acpi: Support writing an integer

2020-05-10 Thread Simon Glass
ACPI supports storing integers in various ways. Add a function to handle
this.

Signed-off-by: Simon Glass 
---

Changes in v2: None
Changes in v1: None

 include/acpi/acpigen.h | 17 ++
 lib/acpi/acpigen.c | 51 ++
 test/dm/acpigen.c  | 46 +
 3 files changed, 114 insertions(+)

diff --git a/include/acpi/acpigen.h b/include/acpi/acpigen.h
index 52e0b75128..a4edcb097b 100644
--- a/include/acpi/acpigen.h
+++ b/include/acpi/acpigen.h
@@ -16,6 +16,12 @@ struct acpi_ctx;
 
 /* ACPI Op/Prefix codes */
 enum {
+   ZERO_OP = 0x00,
+   ONE_OP  = 0x01,
+   BYTE_PREFIX = 0x0a,
+   WORD_PREFIX = 0x0b,
+   DWORD_PREFIX= 0x0c,
+   QWORD_PREFIX= 0x0e,
PACKAGE_OP  = 0x12,
 };
 
@@ -130,4 +136,15 @@ void acpigen_pop_len(struct acpi_ctx *ctx);
  */
 char *acpigen_write_package(struct acpi_ctx *ctx, int nr_el);
 
+/**
+ * acpigen_write_integer() - Write an integer
+ *
+ * This writes an operation (BYTE_OP, WORD_OP, DWORD_OP, QWORD_OP depending on
+ * the integer size) and an integer value. Note that WORD means 16 bits in 
ACPI.
+ *
+ * @ctx: ACPI context pointer
+ * @data: Integer to write
+ */
+void acpigen_write_integer(struct acpi_ctx *ctx, u64 data);
+
 #endif
diff --git a/lib/acpi/acpigen.c b/lib/acpi/acpigen.c
index e1fd1f6b6a..ece0111320 100644
--- a/lib/acpi/acpigen.c
+++ b/lib/acpi/acpigen.c
@@ -82,6 +82,57 @@ char *acpigen_write_package(struct acpi_ctx *ctx, int nr_el)
return p;
 }
 
+void acpigen_write_byte(struct acpi_ctx *ctx, unsigned int data)
+{
+   acpigen_emit_byte(ctx, BYTE_PREFIX);
+   acpigen_emit_byte(ctx, data & 0xff);
+}
+
+void acpigen_write_word(struct acpi_ctx *ctx, unsigned int data)
+{
+   acpigen_emit_byte(ctx, WORD_PREFIX);
+   acpigen_emit_word(ctx, data);
+}
+
+void acpigen_write_dword(struct acpi_ctx *ctx, unsigned int data)
+{
+   acpigen_emit_byte(ctx, DWORD_PREFIX);
+   acpigen_emit_dword(ctx, data);
+}
+
+void acpigen_write_qword(struct acpi_ctx *ctx, u64 data)
+{
+   acpigen_emit_byte(ctx, QWORD_PREFIX);
+   acpigen_emit_dword(ctx, data & 0x);
+   acpigen_emit_dword(ctx, (data >> 32) & 0x);
+}
+
+void acpigen_write_zero(struct acpi_ctx *ctx)
+{
+   acpigen_emit_byte(ctx, ZERO_OP);
+}
+
+void acpigen_write_one(struct acpi_ctx *ctx)
+{
+   acpigen_emit_byte(ctx, ONE_OP);
+}
+
+void acpigen_write_integer(struct acpi_ctx *ctx, u64 data)
+{
+   if (data == 0)
+   acpigen_write_zero(ctx);
+   else if (data == 1)
+   acpigen_write_one(ctx);
+   else if (data <= 0xff)
+   acpigen_write_byte(ctx, (unsigned char)data);
+   else if (data <= 0x)
+   acpigen_write_word(ctx, (unsigned int)data);
+   else if (data <= 0x)
+   acpigen_write_dword(ctx, (unsigned int)data);
+   else
+   acpigen_write_qword(ctx, data);
+}
+
 void acpigen_emit_stream(struct acpi_ctx *ctx, const char *data, int size)
 {
int i;
diff --git a/test/dm/acpigen.c b/test/dm/acpigen.c
index be81068759..9350943a12 100644
--- a/test/dm/acpigen.c
+++ b/test/dm/acpigen.c
@@ -22,6 +22,11 @@
 #define TEST_STRING"frogmore"
 #define TEST_STREAM2   "\xfa\xde"
 
+#define TEST_INT8  0x7d
+#define TEST_INT16 0x2345
+#define TEST_INT32 0x12345678
+#define TEST_INT64 0x4567890123456
+
 static int alloc_context_size(struct acpi_ctx **ctxp, int size)
 {
struct acpi_ctx *ctx;
@@ -421,3 +426,44 @@ static int dm_test_acpi_package(struct unit_test_state 
*uts)
return 0;
 }
 DM_TEST(dm_test_acpi_package, 0);
+
+/* Test emitting an integer */
+static int dm_test_acpi_integer(struct unit_test_state *uts)
+{
+   struct acpi_ctx *ctx;
+   u8 *ptr;
+
+   ut_assertok(alloc_context());
+
+   ptr = acpigen_get_current(ctx);
+
+   acpigen_write_integer(ctx, 0);
+   acpigen_write_integer(ctx, 1);
+   acpigen_write_integer(ctx, TEST_INT8);
+   acpigen_write_integer(ctx, TEST_INT16);
+   acpigen_write_integer(ctx, TEST_INT32);
+   acpigen_write_integer(ctx, TEST_INT64);
+
+   ut_asserteq(6 + 1 + 2 + 4 + 8, acpigen_get_current(ctx) - ptr);
+
+   ut_asserteq(ZERO_OP, ptr[0]);
+
+   ut_asserteq(ONE_OP, ptr[1]);
+
+   ut_asserteq(BYTE_PREFIX, ptr[2]);
+   ut_asserteq(TEST_INT8, ptr[3]);
+
+   ut_asserteq(WORD_PREFIX, ptr[4]);
+   ut_asserteq(TEST_INT16, get_unaligned((u16 *)(ptr + 5)));
+
+   ut_asserteq(DWORD_PREFIX, ptr[7]);
+   ut_asserteq(TEST_INT32, get_unaligned((u32 *)(ptr + 8)));
+
+   ut_asserteq(QWORD_PREFIX, ptr[12]);
+   ut_asserteq_64(TEST_INT64, get_unaligned((u64 *)(ptr + 13)));
+
+   free_context();
+
+   return 0;
+}
+DM_TEST(dm_test_acpi_integer, 0);
-- 
2.26.2.645.ge9eca65c58-goog



[PATCH v2 07/35] gpio: Add a method to convert a GPIO to ACPI

2020-05-10 Thread Simon Glass
When generating ACPI tables we need to convert GPIOs in U-Boot to the ACPI
structures required by ACPI. This is a SoC-specific conversion and cannot
be handled by generic code, so add a new GPIO method to do the conversion.

Signed-off-by: Simon Glass 
Reviewed-by: Wolfgang Wallner 
---

Changes in v2: None
Changes in v1:
- Update sandbox driver slightly for testing

 drivers/gpio/gpio-uclass.c | 21 +
 drivers/gpio/sandbox.c | 86 
 include/acpi/acpi_device.h | 90 ++
 include/asm-generic/gpio.h | 27 
 test/dm/gpio.c | 62 ++
 test/dm/irq.c  |  1 +
 6 files changed, 287 insertions(+)

diff --git a/drivers/gpio/gpio-uclass.c b/drivers/gpio/gpio-uclass.c
index aaaeb5940b..d50e677947 100644
--- a/drivers/gpio/gpio-uclass.c
+++ b/drivers/gpio/gpio-uclass.c
@@ -13,6 +13,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -794,6 +795,26 @@ int gpio_get_status(struct udevice *dev, int offset, char 
*buf, int buffsize)
return 0;
 }
 
+#if CONFIG_IS_ENABLED(ACPIGEN)
+int gpio_get_acpi(const struct gpio_desc *desc, struct acpi_gpio *gpio)
+{
+   struct dm_gpio_ops *ops;
+
+   if (!dm_gpio_is_valid(desc)) {
+   /* Indicate that the GPIO is not valid */
+   gpio->pin_count = 0;
+   gpio->pins[0] = 0;
+   return -EINVAL;
+   }
+
+   ops = gpio_get_ops(desc->dev);
+   if (!ops->get_acpi)
+   return -ENOSYS;
+
+   return ops->get_acpi(desc, gpio);
+}
+#endif
+
 int gpio_claim_vector(const int *gpio_num_array, const char *fmt)
 {
int i, ret;
diff --git a/drivers/gpio/sandbox.c b/drivers/gpio/sandbox.c
index 98b7fa4bb3..6cd2a1455a 100644
--- a/drivers/gpio/sandbox.c
+++ b/drivers/gpio/sandbox.c
@@ -8,7 +8,9 @@
 #include 
 #include 
 #include 
+#include 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -197,6 +199,72 @@ static int sb_gpio_get_dir_flags(struct udevice *dev, 
unsigned int offset,
return 0;
 }
 
+#if CONFIG_IS_ENABLED(ACPIGEN)
+static int sb_gpio_get_acpi(const struct gpio_desc *desc,
+   struct acpi_gpio *gpio)
+{
+   int ret;
+
+   /* All of these values are just used for testing */
+   if (desc->flags & GPIOD_ACTIVE_LOW) {
+   gpio->pin_count = 1;
+   gpio->pins[0] = desc->offset;
+   gpio->pin0_addr = 0x80012 + desc->offset;
+   gpio->type = ACPI_GPIO_TYPE_INTERRUPT;
+   gpio->pull = ACPI_GPIO_PULL_DOWN;
+   ret = acpi_device_scope(desc->dev, gpio->resource,
+   sizeof(gpio->resource));
+   if (ret)
+   return log_ret(ret);
+   gpio->interrupt_debounce_timeout = 4321;
+   memset(>irq, '\0', sizeof(gpio->irq));
+
+   /* We use the GpioInt part */
+   gpio->irq.pin = desc->offset;
+   gpio->irq.polarity = ACPI_IRQ_ACTIVE_BOTH;
+   gpio->irq.shared = ACPI_IRQ_SHARED;
+   gpio->irq.wake = ACPI_IRQ_WAKE;
+
+   /* The GpioIo part is not used */
+   gpio->output_drive_strength = 0;
+   gpio->io_shared = false;
+   gpio->io_restrict = 0;
+   gpio->polarity = ACPI_GPIO_ACTIVE_LOW;
+   } else {
+   gpio->pin_count = 1;
+   gpio->pins[0] = desc->offset;
+   gpio->pin0_addr = 0xc00dc + desc->offset;
+   gpio->type = ACPI_GPIO_TYPE_IO;
+   gpio->pull = ACPI_GPIO_PULL_UP;
+   ret = acpi_device_scope(desc->dev, gpio->resource,
+   sizeof(gpio->resource));
+   if (ret)
+   return log_ret(ret);
+   gpio->interrupt_debounce_timeout = 0;
+
+   /* The GpioInt part is not used */
+   memset(>irq, '\0', sizeof(gpio->irq));
+
+   /* We use the GpioIo part */
+   gpio->output_drive_strength = 1234;
+   gpio->io_shared = true;
+   gpio->io_restrict = ACPI_GPIO_IO_RESTRICT_INPUT;
+   gpio->polarity = 0;
+   }
+
+   return 0;
+}
+
+static int sb_gpio_get_name(const struct udevice *dev, char *out_name)
+{
+   return acpi_copy_name(out_name, "GPIO");
+}
+
+struct acpi_ops gpio_sandbox_acpi_ops = {
+   .get_name   = sb_gpio_get_name,
+};
+#endif /* ACPIGEN */
+
 static const struct dm_gpio_ops gpio_sandbox_ops = {
.direction_input= sb_gpio_direction_input,
.direction_output   = sb_gpio_direction_output,
@@ -206,6 +274,9 @@ static const struct dm_gpio_ops gpio_sandbox_ops = {
.xlate  = sb_gpio_xlate,
.set_dir_flags  = sb_gpio_set_dir_flags,
.get_dir_flags  = sb_gpio_get_dir_flags,
+#if 

[PATCH v2 08/35] acpi: Support string output

2020-05-10 Thread Simon Glass
Add support for output of strings and streams of bytes.

Signed-off-by: Simon Glass 
---

Changes in v2: None
Changes in v1: None

 include/acpi/acpigen.h | 19 +++
 lib/acpi/acpigen.c | 14 ++
 test/dm/acpigen.c  | 42 ++
 3 files changed, 75 insertions(+)

diff --git a/include/acpi/acpigen.h b/include/acpi/acpigen.h
index 8809cdb4e1..7365cce738 100644
--- a/include/acpi/acpigen.h
+++ b/include/acpi/acpigen.h
@@ -46,4 +46,23 @@ void acpigen_emit_word(struct acpi_ctx *ctx, uint data);
  */
 void acpigen_emit_dword(struct acpi_ctx *ctx, uint data);
 
+/**
+ * acpigen_emit_stream() - Emit a stream of bytes
+ *
+ * @ctx: ACPI context pointer
+ * @data: Data to output
+ * @size: Size of data in bytes
+ */
+void acpigen_emit_stream(struct acpi_ctx *ctx, const char *data, int size);
+
+/**
+ * acpigen_emit_string() - Emit a string
+ *
+ * Emit a string with a nul terminator
+ *
+ * @ctx: ACPI context pointer
+ * @str: String to output, or NULL for an empty string
+ */
+void acpigen_emit_string(struct acpi_ctx *ctx, const char *str);
+
 #endif
diff --git a/lib/acpi/acpigen.c b/lib/acpi/acpigen.c
index 59bd3af0b7..1223f0d1c4 100644
--- a/lib/acpi/acpigen.c
+++ b/lib/acpi/acpigen.c
@@ -36,3 +36,17 @@ void acpigen_emit_dword(struct acpi_ctx *ctx, uint data)
acpigen_emit_byte(ctx, (data >> 16) & 0xff);
acpigen_emit_byte(ctx, (data >> 24) & 0xff);
 }
+
+void acpigen_emit_stream(struct acpi_ctx *ctx, const char *data, int size)
+{
+   int i;
+
+   for (i = 0; i < size; i++)
+   acpigen_emit_byte(ctx, data[i]);
+}
+
+void acpigen_emit_string(struct acpi_ctx *ctx, const char *str)
+{
+   acpigen_emit_stream(ctx, str, str ? strlen(str) : 0);
+   acpigen_emit_byte(ctx, '\0'); /* NUL */
+}
diff --git a/test/dm/acpigen.c b/test/dm/acpigen.c
index ab1261db9f..77d1a3bba6 100644
--- a/test/dm/acpigen.c
+++ b/test/dm/acpigen.c
@@ -17,6 +17,9 @@
 #include 
 #include 
 
+#define TEST_STRING"frogmore"
+#define TEST_STREAM2   "\xfa\xde"
+
 static int alloc_context(struct acpi_ctx **ctxp)
 {
struct acpi_ctx *ctx;
@@ -66,6 +69,45 @@ static int dm_test_acpi_emit_simple(struct unit_test_state 
*uts)
 }
 DM_TEST(dm_test_acpi_emit_simple, 0);
 
+/* Test emitting a stream */
+static int dm_test_acpi_emit_stream(struct unit_test_state *uts)
+{
+   struct acpi_ctx *ctx;
+   u8 *ptr;
+
+   ut_assertok(alloc_context());
+
+   ptr = acpigen_get_current(ctx);
+   acpigen_emit_stream(ctx, TEST_STREAM2, 2);
+   ut_asserteq(2, acpigen_get_current(ctx) - ptr);
+   ut_asserteq((u8)TEST_STREAM2[0], ptr[0]);
+   ut_asserteq((u8)TEST_STREAM2[1], ptr[1]);
+
+   free_context();
+
+   return 0;
+}
+DM_TEST(dm_test_acpi_emit_stream, 0);
+
+/* Test emitting a string */
+static int dm_test_acpi_emit_string(struct unit_test_state *uts)
+{
+   struct acpi_ctx *ctx;
+   u8 *ptr;
+
+   ut_assertok(alloc_context());
+
+   ptr = acpigen_get_current(ctx);
+   acpigen_emit_string(ctx, TEST_STRING);
+   ut_asserteq(sizeof(TEST_STRING), acpigen_get_current(ctx) - ptr);
+   ut_asserteq_str(TEST_STRING, (char *)ptr);
+
+   free_context();
+
+   return 0;
+}
+DM_TEST(dm_test_acpi_emit_string, 0);
+
 /* Test emitting an interrupt descriptor */
 static int dm_test_acpi_interrupt(struct unit_test_state *uts)
 {
-- 
2.26.2.645.ge9eca65c58-goog



[PATCH v2 09/35] acpi: Support generation of GPIO descriptor

2020-05-10 Thread Simon Glass
Add a function to write a GPIO descriptor to the generated ACPI code.

Signed-off-by: Simon Glass 
---

Changes in v2: None
Changes in v1: None

 include/acpi/acpi_device.h |  22 ++
 lib/acpi/acpi_device.c | 151 +
 test/dm/acpigen.c  |  89 ++
 3 files changed, 262 insertions(+)

diff --git a/include/acpi/acpi_device.h b/include/acpi/acpi_device.h
index cb9166aeae..5f229d79cf 100644
--- a/include/acpi/acpi_device.h
+++ b/include/acpi/acpi_device.h
@@ -12,6 +12,7 @@
 #include 
 
 struct acpi_ctx;
+struct gpio_desc;
 struct irq;
 struct udevice;
 
@@ -233,4 +234,25 @@ enum acpi_dev_status acpi_device_status(const struct 
udevice *dev);
 int acpi_device_write_interrupt_irq(struct acpi_ctx *ctx,
const struct irq *req_irq);
 
+/**
+ * acpi_device_write_gpio() - Write GpioIo() or GpioInt() descriptor
+ *
+ * @gpio: GPIO information to write
+ * @return 0 if OK, -ve on error
+ */
+int acpi_device_write_gpio(struct acpi_ctx *ctx, const struct acpi_gpio *gpio);
+
+/**
+ * acpi_device_write_gpio_desc() - Write a GPIO to ACPI
+ *
+ * This creates a GPIO descriptor for a GPIO, including information ACPI needs
+ * to use it. The type is always ACPI_GPIO_TYPE_IO.
+ *
+ * @ctx: ACPI context pointer
+ * @desc: GPIO to write
+ * @return 0 if OK, -ve on error
+ */
+int acpi_device_write_gpio_desc(struct acpi_ctx *ctx,
+   const struct gpio_desc *desc);
+
 #endif
diff --git a/lib/acpi/acpi_device.c b/lib/acpi/acpi_device.c
index 6b2fb7e47e..4c5bfdb9a2 100644
--- a/lib/acpi/acpi_device.c
+++ b/lib/acpi/acpi_device.c
@@ -12,6 +12,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 
 /**
@@ -204,3 +205,153 @@ int acpi_device_write_interrupt_irq(struct acpi_ctx *ctx,
 
return 0;
 }
+
+/* ACPI 6.3 section 6.4.3.8.1 - GPIO Interrupt or I/O */
+int acpi_device_write_gpio(struct acpi_ctx *ctx, const struct acpi_gpio *gpio)
+{
+   void *start, *desc_length;
+   void *pin_table_offset, *vendor_data_offset, *resource_offset;
+   u16 flags = 0;
+   int pin;
+
+   if (gpio->type > ACPI_GPIO_TYPE_IO)
+   return -EINVAL;
+
+   start = acpigen_get_current(ctx);
+
+   /* Byte 0: Descriptor Type */
+   acpigen_emit_byte(ctx, ACPI_DESCRIPTOR_GPIO);
+
+   /* Byte 1-2: Length (fill in later) */
+   desc_length = acpi_device_write_zero_len(ctx);
+
+   /* Byte 3: Revision ID */
+   acpigen_emit_byte(ctx, ACPI_GPIO_REVISION_ID);
+
+   /* Byte 4: GpioIo or GpioInt */
+   acpigen_emit_byte(ctx, gpio->type);
+
+   /*
+* Byte 5-6: General Flags
+*   [15:1]: 0 => Reserved
+*  [0]: 1 => ResourceConsumer
+*/
+   acpigen_emit_word(ctx, 1 << 0);
+
+   switch (gpio->type) {
+   case ACPI_GPIO_TYPE_INTERRUPT:
+   /*
+* Byte 7-8: GPIO Interrupt Flags
+*   [15:5]: 0 => Reserved
+*  [4]: Wake (0=NO_WAKE   1=WAKE)
+*  [3]: Sharing  (0=EXCLUSIVE 1=SHARED)
+*[2:1]: Polarity (0=HIGH  1=LOW 2=BOTH)
+*  [0]: Mode (0=LEVEL 1=EDGE)
+*/
+   if (gpio->irq.mode == ACPI_IRQ_EDGE_TRIGGERED)
+   flags |= 1 << 0;
+   if (gpio->irq.shared == ACPI_IRQ_SHARED)
+   flags |= 1 << 3;
+   if (gpio->irq.wake == ACPI_IRQ_WAKE)
+   flags |= 1 << 4;
+
+   switch (gpio->irq.polarity) {
+   case ACPI_IRQ_ACTIVE_HIGH:
+   flags |= 0 << 1;
+   break;
+   case ACPI_IRQ_ACTIVE_LOW:
+   flags |= 1 << 1;
+   break;
+   case ACPI_IRQ_ACTIVE_BOTH:
+   flags |= 2 << 1;
+   break;
+   }
+   break;
+
+   case ACPI_GPIO_TYPE_IO:
+   /*
+* Byte 7-8: GPIO IO Flags
+*   [15:4]: 0 => Reserved
+*  [3]: Sharing  (0=EXCLUSIVE 1=SHARED)
+*  [2]: 0 => Reserved
+*[1:0]: IO Restriction
+*   0 => IoRestrictionNone
+*   1 => IoRestrictionInputOnly
+*   2 => IoRestrictionOutputOnly
+*   3 => IoRestrictionNoneAndPreserve
+*/
+   flags |= gpio->io_restrict & 3;
+   if (gpio->io_shared)
+   flags |= 1 << 3;
+   break;
+   }
+   acpigen_emit_word(ctx, flags);
+
+   /*
+* Byte 9: Pin Configuration
+*  0x01 => Default (no configuration applied)
+*  0x02 => Pull-up
+*  0x03 => Pull-down
+*  0x04-0x7F => Reserved
+*  0x80-0xff => Vendor defined
+*/
+   acpigen_emit_byte(ctx, 

[PATCH v2 10/35] acpi: Support generation of a GPIO/irq for a device

2020-05-10 Thread Simon Glass
Some devices use interrupts but some use GPIOs. Since these are fully
specified in the device tree we can automatically produce the correct ACPI
descriptor for a device.

Add a function to handle this.

Signed-off-by: Simon Glass 
---

Changes in v2: None
Changes in v1: None

 include/acpi/acpi_device.h | 15 ++
 lib/acpi/acpi_device.c | 26 
 test/dm/acpigen.c  | 41 ++
 3 files changed, 82 insertions(+)

diff --git a/include/acpi/acpi_device.h b/include/acpi/acpi_device.h
index 5f229d79cf..70c151d150 100644
--- a/include/acpi/acpi_device.h
+++ b/include/acpi/acpi_device.h
@@ -255,4 +255,19 @@ int acpi_device_write_gpio(struct acpi_ctx *ctx, const 
struct acpi_gpio *gpio);
 int acpi_device_write_gpio_desc(struct acpi_ctx *ctx,
const struct gpio_desc *desc);
 
+/**
+ * acpi_device_write_interrupt_or_gpio() - Write interrupt or GPIO to ACPI
+ *
+ * This reads an interrupt from the device tree "interrupts-extended" property,
+ * if available. If not it reads the first GPIO with the name @prop.
+ *
+ * If an interrupt is found, an ACPI interrupt descriptor is written to the 
ACPI
+ * output. If not, but an GPIO is found, a GPIO descriptor is written.
+ *
+ * @return 0 if OK, -ve if neither an interrupt nor a GPIO could be found, or
+ * some other error occurred
+ */
+int acpi_device_write_interrupt_or_gpio(struct acpi_ctx *ctx,
+   struct udevice *dev, const char *prop);
+
 #endif
diff --git a/lib/acpi/acpi_device.c b/lib/acpi/acpi_device.c
index 4c5bfdb9a2..423b91cfd2 100644
--- a/lib/acpi/acpi_device.c
+++ b/lib/acpi/acpi_device.c
@@ -355,3 +355,29 @@ int acpi_device_write_gpio_desc(struct acpi_ctx *ctx,
 
return 0;
 }
+
+int acpi_device_write_interrupt_or_gpio(struct acpi_ctx *ctx,
+   struct udevice *dev, const char *prop)
+{
+   struct irq req_irq;
+   int ret;
+
+   ret = irq_get_by_index(dev, 0, _irq);
+   if (!ret) {
+   ret = acpi_device_write_interrupt_irq(ctx, _irq);
+   if (ret)
+   return log_msg_ret("irq", ret);
+   } else {
+   struct gpio_desc req_gpio;
+
+   ret = gpio_request_by_name(dev, prop, 0, _gpio,
+  GPIOD_IS_IN);
+   if (ret)
+   return log_msg_ret("no gpio", ret);
+   ret = acpi_device_write_gpio_desc(ctx, _gpio);
+   if (ret)
+   return log_msg_ret("gpio", ret);
+   }
+
+   return 0;
+}
diff --git a/test/dm/acpigen.c b/test/dm/acpigen.c
index 73fe6c9f4d..6aefa6845d 100644
--- a/test/dm/acpigen.c
+++ b/test/dm/acpigen.c
@@ -16,6 +16,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 
 #define TEST_STRING"frogmore"
@@ -225,3 +226,43 @@ static int dm_test_acpi_gpio_irq(struct unit_test_state 
*uts)
return 0;
 }
 DM_TEST(dm_test_acpi_gpio_irq, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
+
+/* Test emitting either a GPIO or interrupt descriptor */
+static int dm_test_acpi_interrupt_or_gpio(struct unit_test_state *uts)
+{
+   struct acpi_ctx *ctx;
+   struct udevice *dev;
+   u8 *ptr;
+
+   ut_assertok(alloc_context());
+
+   ptr = acpigen_get_current(ctx);
+
+   /* This should produce an interrupt, even though it also has a GPIO */
+   ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 0, ));
+   ut_asserteq_str("a-test", dev->name);
+   ut_assertok(acpi_device_write_interrupt_or_gpio(ctx, dev,
+   "test2-gpios"));
+   ut_asserteq(ACPI_DESCRIPTOR_INTERRUPT, ptr[0]);
+
+   /* This has no interrupt so should produce a GPIO */
+   ptr = ctx->current;
+   ut_assertok(uclass_find_first_device(UCLASS_PANEL_BACKLIGHT, ));
+   ut_assertok(acpi_device_write_interrupt_or_gpio(ctx, dev,
+   "enable-gpios"));
+   ut_asserteq(ACPI_DESCRIPTOR_GPIO, ptr[0]);
+
+   /* This one has neither */
+   ptr = acpigen_get_current(ctx);
+   ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT, 3, ));
+   ut_asserteq_str("b-test", dev->name);
+   ut_asserteq(-ENOENT,
+   acpi_device_write_interrupt_or_gpio(ctx, dev,
+   "enable-gpios"));
+
+   free_context();
+
+   return 0;
+}
+DM_TEST(dm_test_acpi_interrupt_or_gpio,
+   DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
-- 
2.26.2.645.ge9eca65c58-goog



[PATCH v2 02/35] acpi: Add a function to get a device path and scope

2020-05-10 Thread Simon Glass
Add a function to build up the ACPI path for a device and another for its
scope.

Signed-off-by: Simon Glass 
Reviewed-by: Wolfgang Wallner 
---

Changes in v2: None
Changes in v1:
- Split into more patches for review
- Add tests
- Rebase on top of common.h series

 arch/sandbox/dts/test.dts  |  3 ++
 include/acpi/acpi_device.h | 44 ++
 lib/acpi/Makefile  |  1 +
 lib/acpi/acpi_device.c | 83 +
 test/dm/acpi.c | 95 --
 5 files changed, 213 insertions(+), 13 deletions(-)
 create mode 100644 include/acpi/acpi_device.h
 create mode 100644 lib/acpi/acpi_device.c

diff --git a/arch/sandbox/dts/test.dts b/arch/sandbox/dts/test.dts
index 15cd2330a3..b802c1c909 100644
--- a/arch/sandbox/dts/test.dts
+++ b/arch/sandbox/dts/test.dts
@@ -253,6 +253,9 @@
 
acpi-test {
compatible = "denx,u-boot-acpi-test";
+   child {
+   compatible = "denx,u-boot-acpi-test";
+   };
};
 
acpi-test2 {
diff --git a/include/acpi/acpi_device.h b/include/acpi/acpi_device.h
new file mode 100644
index 00..37a675f101
--- /dev/null
+++ b/include/acpi/acpi_device.h
@@ -0,0 +1,44 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Generation of tables for particular device types
+ *
+ * Copyright 2019 Google LLC
+ * Mostly taken from coreboot file of the same name
+ */
+
+#ifndef __ACPI_DEVICE_H
+#define __ACPI_DEVICE_H
+
+struct udevice;
+
+/* Length of a full path to an ACPI device */
+#define ACPI_PATH_MAX  30
+
+/**
+ * acpi_device_path() - Get the full path to an ACPI device
+ *
+ * This gets the full path in the form .. where  is the root
+ * and  is the device. All parent devices are added to the path.
+ *
+ * @dev: Device to check
+ * @buf: Buffer to place the path in (should be ACPI_PATH_MAX long)
+ * @maxlen: Size of buffer (typically ACPI_PATH_MAX)
+ * @return 0 if OK, -ve on error
+ */
+int acpi_device_path(const struct udevice *dev, char *buf, int maxlen);
+
+/**
+ * acpi_device_scope() - Get the scope of an ACPI device
+ *
+ * This gets the scope which is the full path of the parent device, as per
+ * acpi_device_path().
+ *
+ * @dev: Device to check
+ * @buf: Buffer to place the path in (should be ACPI_PATH_MAX long)
+ * @maxlen: Size of buffer (typically ACPI_PATH_MAX)
+ * @return 0 if OK, -EINVAL if the device has no parent, other -ve on other
+ * error
+ */
+int acpi_device_scope(const struct udevice *dev, char *scope, int maxlen);
+
+#endif
diff --git a/lib/acpi/Makefile b/lib/acpi/Makefile
index 660491ef71..caae6c01bd 100644
--- a/lib/acpi/Makefile
+++ b/lib/acpi/Makefile
@@ -1,4 +1,5 @@
 # SPDX-License-Identifier: GPL-2.0+
 #
 
+obj-y += acpi_device.o
 obj-y += acpi_table.o
diff --git a/lib/acpi/acpi_device.c b/lib/acpi/acpi_device.c
new file mode 100644
index 00..f9af2343c1
--- /dev/null
+++ b/lib/acpi/acpi_device.c
@@ -0,0 +1,83 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Generation of tables for particular device types
+ *
+ * Copyright 2019 Google LLC
+ * Mostly taken from coreboot file of the same name
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+
+/**
+ * acpi_device_path_fill() - Find the root device and build a path from there
+ *
+ * This recursively reaches back to the root device and progressively adds path
+ * elements until the device is reached.
+ *
+ * @dev: Device to return path of
+ * @buf: Buffer to hold the path
+ * @buf_len: Length of buffer
+ * @cur: Current position in the buffer
+ * @return new position in buffer after adding @dev, or -ve on error
+ */
+static int acpi_device_path_fill(const struct udevice *dev, char *buf,
+size_t buf_len, int cur)
+{
+   char name[ACPI_NAME_MAX];
+   int next = 0;
+   int ret;
+
+   ret = acpi_get_name(dev, name);
+   if (ret)
+   return ret;
+
+   /*
+* Make sure this name segment will fit, including the path segment
+* separator and possible NUL terminator, if this is the last segment.
+*/
+   if (cur + strlen(name) + 2 > buf_len)
+   return -ENOSPC;
+
+   /* Walk up the tree to the root device */
+   if (dev_get_parent(dev)) {
+   next = acpi_device_path_fill(dev_get_parent(dev), buf, buf_len,
+cur);
+   if (next < 0)
+   return next;
+   }
+
+   /* Fill in the path from the root device */
+   next += snprintf(buf + next, buf_len - next, "%s%s",
+dev_get_parent(dev) && *name ? "." : "", name);
+
+   return next;
+}
+
+int acpi_device_path(const struct udevice *dev, char *buf, int maxlen)
+{
+   int ret;
+
+   ret = acpi_device_path_fill(dev, buf, maxlen, 0);
+   if (ret < 0)
+   return ret;
+
+   return 0;
+}
+
+int acpi_device_scope(const 

[PATCH v2 06/35] acpi: Support generation of interrupt descriptor

2020-05-10 Thread Simon Glass
Add a function to write an interrupt descriptor to the generated ACPI
code.

Signed-off-by: Simon Glass 
---

Changes in v2: None
Changes in v1: None

 include/acpi/acpi_device.h |  15 +
 lib/acpi/acpi_device.c | 118 +
 test/dm/acpigen.c  |  31 ++
 3 files changed, 164 insertions(+)

diff --git a/include/acpi/acpi_device.h b/include/acpi/acpi_device.h
index 24895de0da..4f87cd003a 100644
--- a/include/acpi/acpi_device.h
+++ b/include/acpi/acpi_device.h
@@ -11,6 +11,8 @@
 
 #include 
 
+struct acpi_ctx;
+struct irq;
 struct udevice;
 
 /* ACPI descriptor values for common descriptors: SERIAL_BUS means I2C */
@@ -128,4 +130,17 @@ int acpi_device_scope(const struct udevice *dev, char 
*scope, int maxlen);
  */
 enum acpi_dev_status acpi_device_status(const struct udevice *dev);
 
+/**
+ * acpi_device_write_interrupt_irq() - Write an interrupt descriptor
+ *
+ * This writes an ACPI interrupt descriptor for the given interrupt, converting
+ * fields as needed.
+ *
+ * @ctx: ACPI context pointer
+ * @req_irq: Interrupt to output
+ * @return 0 if OK, -ve on error
+ */
+int acpi_device_write_interrupt_irq(struct acpi_ctx *ctx,
+   const struct irq *req_irq);
+
 #endif
diff --git a/lib/acpi/acpi_device.c b/lib/acpi/acpi_device.c
index 7b18cbf148..6b2fb7e47e 100644
--- a/lib/acpi/acpi_device.c
+++ b/lib/acpi/acpi_device.c
@@ -8,8 +8,10 @@
 
 #include 
 #include 
+#include 
 #include 
 #include 
+#include 
 #include 
 
 /**
@@ -86,3 +88,119 @@ enum acpi_dev_status acpi_device_status(const struct 
udevice *dev)
 {
return ACPI_DSTATUS_ALL_ON;
 }
+
+/**
+ * acpi_device_write_zero_len() - Write a placeholder word value
+ *
+ * @return pointer to the zero word (for fixing up later)
+ */
+static void *acpi_device_write_zero_len(struct acpi_ctx *ctx)
+{
+   u8 *p = acpigen_get_current(ctx);
+
+   acpigen_emit_word(ctx, 0);
+
+   return p;
+}
+
+/**
+ * acpi_device_fill_from_len() - Fill in a length value
+ *
+ * This calculated the number of bytes since the provided @start and writes it
+ * to @ptr, which was previous returned by acpi_device_write_zero_len().
+ *
+ * @ptr: Word to update
+ * @start: Start address to count from to calculated the length
+ */
+static void acpi_device_fill_from_len(struct acpi_ctx *ctx, char *ptr,
+ u8 *start)
+{
+   u16 len = acpigen_get_current(ctx) - start;
+
+   ptr[0] = len & 0xff;
+   ptr[1] = (len >> 8) & 0xff;
+}
+
+/**
+ * acpi_device_fill_len() - Fill in a length value, excluding the length itself
+ *
+ * Fill in the length field with the value calculated from after the 16bit
+ * field to acpigen current. This is useful since the length value does not
+ * include the length field itself.
+ *
+ * This calls acpi_device_fill_from_len() passing @ptr + 2 as @start
+ *
+ * @ptr: Word to update.
+ */
+static void acpi_device_fill_len(struct acpi_ctx *ctx, void *ptr)
+{
+   acpi_device_fill_from_len(ctx, ptr, ptr + sizeof(u16));
+}
+
+/* ACPI 6.3 section 6.4.3.6: Extended Interrupt Descriptor */
+static int acpi_device_write_interrupt(struct acpi_ctx *ctx,
+  const struct acpi_irq *irq)
+{
+   void *desc_length;
+   u8 flags;
+
+   if (!irq->pin)
+   return -ENOENT;
+
+   /* This is supported by GpioInt() but not Interrupt() */
+   if (irq->polarity == ACPI_IRQ_ACTIVE_BOTH)
+   return -EINVAL;
+
+   /* Byte 0: Descriptor Type */
+   acpigen_emit_byte(ctx, ACPI_DESCRIPTOR_INTERRUPT);
+
+   /* Byte 1-2: Length (filled in later) */
+   desc_length = acpi_device_write_zero_len(ctx);
+
+   /*
+* Byte 3: Flags
+*  [7:5]: Reserved
+*[4]: Wake (0=NO_WAKE   1=WAKE)
+*[3]: Sharing  (0=EXCLUSIVE 1=SHARED)
+*[2]: Polarity (0=HIGH  1=LOW)
+*[1]: Mode (0=LEVEL 1=EDGE)
+*[0]: Resource (0=PRODUCER  1=CONSUMER)
+*/
+   flags = 1 << 0; /* ResourceConsumer */
+   if (irq->mode == ACPI_IRQ_EDGE_TRIGGERED)
+   flags |= 1 << 1;
+   if (irq->polarity == ACPI_IRQ_ACTIVE_LOW)
+   flags |= 1 << 2;
+   if (irq->shared == ACPI_IRQ_SHARED)
+   flags |= 1 << 3;
+   if (irq->wake == ACPI_IRQ_WAKE)
+   flags |= 1 << 4;
+   acpigen_emit_byte(ctx, flags);
+
+   /* Byte 4: Interrupt Table Entry Count */
+   acpigen_emit_byte(ctx, 1);
+
+   /* Byte 5-8: Interrupt Number */
+   acpigen_emit_dword(ctx, irq->pin);
+
+   /* Fill in Descriptor Length (account for len word) */
+   acpi_device_fill_len(ctx, desc_length);
+
+   return 0;
+}
+
+int acpi_device_write_interrupt_irq(struct acpi_ctx *ctx,
+   const struct irq *req_irq)
+{
+   struct acpi_irq irq;
+   int ret;
+
+   ret = irq_get_acpi(req_irq, );
+   if (ret)
+

[PATCH v2 04/35] irq: Add a method to convert an interrupt to ACPI

2020-05-10 Thread Simon Glass
When generating ACPI tables we need to convert IRQs in U-Boot to the ACPI
structures required by ACPI. This is a SoC-specific conversion and cannot
be handled by generic code, so add a new IRQ method to do the conversion.

Signed-off-by: Simon Glass 
---

Changes in v2: None
Changes in v1:
- Fix 'the an' typo
- Move header definitions into this patch

 drivers/misc/irq-uclass.c  | 18 ++--
 drivers/misc/irq_sandbox.c | 16 +++
 include/acpi/acpi_device.h | 59 ++
 include/irq.h  | 43 +++
 test/dm/irq.c  | 22 ++
 5 files changed, 156 insertions(+), 2 deletions(-)

diff --git a/drivers/misc/irq-uclass.c b/drivers/misc/irq-uclass.c
index 16dc0be75c..98bc79eaba 100644
--- a/drivers/misc/irq-uclass.c
+++ b/drivers/misc/irq-uclass.c
@@ -154,8 +154,6 @@ int irq_request(struct udevice *dev, struct irq *irq)
const struct irq_ops *ops;
 
log_debug("(dev=%p, irq=%p)\n", dev, irq);
-   if (!irq)
-   return 0;
ops = irq_get_ops(dev);
 
irq->dev = dev;
@@ -177,6 +175,22 @@ int irq_first_device_type(enum irq_dev_t type, struct 
udevice **devp)
return 0;
 }
 
+#if CONFIG_IS_ENABLED(ACPIGEN)
+int irq_get_acpi(const struct irq *irq, struct acpi_irq *acpi_irq)
+{
+   struct irq_ops *ops;
+
+   if (!irq_is_valid(irq))
+   return -EINVAL;
+
+   ops = irq_get_ops(irq->dev);
+   if (!ops->get_acpi)
+   return -ENOSYS;
+
+   return ops->get_acpi(irq, acpi_irq);
+}
+#endif
+
 UCLASS_DRIVER(irq) = {
.id = UCLASS_IRQ,
.name   = "irq",
diff --git a/drivers/misc/irq_sandbox.c b/drivers/misc/irq_sandbox.c
index 54bc47c8d8..a2511b32fc 100644
--- a/drivers/misc/irq_sandbox.c
+++ b/drivers/misc/irq_sandbox.c
@@ -8,6 +8,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 
 /**
@@ -73,6 +74,18 @@ static int sandbox_irq_of_xlate(struct irq *irq,
return 0;
 }
 
+static __maybe_unused int sandbox_get_acpi(const struct irq *irq,
+  struct acpi_irq *acpi_irq)
+{
+   acpi_irq->pin = irq->id;
+   acpi_irq->mode = ACPI_IRQ_LEVEL_TRIGGERED;
+   acpi_irq->polarity = ACPI_IRQ_ACTIVE_HIGH;
+   acpi_irq->shared = ACPI_IRQ_SHARED;
+   acpi_irq->wake = ACPI_IRQ_WAKE;
+
+   return 0;
+}
+
 static const struct irq_ops sandbox_irq_ops = {
.route_pmc_gpio_gpe = sandbox_route_pmc_gpio_gpe,
.set_polarity   = sandbox_set_polarity,
@@ -80,6 +93,9 @@ static const struct irq_ops sandbox_irq_ops = {
.restore_polarities = sandbox_restore_polarities,
.read_and_clear = sandbox_irq_read_and_clear,
.of_xlate   = sandbox_irq_of_xlate,
+#if CONFIG_IS_ENABLED(ACPIGEN)
+   .get_acpi   = sandbox_get_acpi,
+#endif
 };
 
 static const struct udevice_id sandbox_irq_ids[] = {
diff --git a/include/acpi/acpi_device.h b/include/acpi/acpi_device.h
index 09c227489a..24895de0da 100644
--- a/include/acpi/acpi_device.h
+++ b/include/acpi/acpi_device.h
@@ -13,6 +13,12 @@
 
 struct udevice;
 
+/* ACPI descriptor values for common descriptors: SERIAL_BUS means I2C */
+#define ACPI_DESCRIPTOR_LARGE  BIT(7)
+#define ACPI_DESCRIPTOR_INTERRUPT  (ACPI_DESCRIPTOR_LARGE | 9)
+#define ACPI_DESCRIPTOR_GPIO   (ACPI_DESCRIPTOR_LARGE | 12)
+#define ACPI_DESCRIPTOR_SERIAL_BUS (ACPI_DESCRIPTOR_LARGE | 14)
+
 /* Length of a full path to an ACPI device */
 #define ACPI_PATH_MAX  30
 
@@ -31,6 +37,59 @@ enum acpi_dev_status {
ACPI_DSTATUS_SHOW_IN_UI,
 };
 
+/** enum acpi_irq_mode - edge/level trigger mode */
+enum acpi_irq_mode {
+   ACPI_IRQ_EDGE_TRIGGERED,
+   ACPI_IRQ_LEVEL_TRIGGERED,
+};
+
+/**
+ * enum acpi_irq_polarity - polarity of interrupt
+ *
+ * @ACPI_IRQ_ACTIVE_LOW - for ACPI_IRQ_EDGE_TRIGGERED this means falling edge
+ * @ACPI_IRQ_ACTIVE_HIGH - for ACPI_IRQ_EDGE_TRIGGERED this means rising edge
+ * @ACPI_IRQ_ACTIVE_BOTH - not meaningful for ACPI_IRQ_EDGE_TRIGGERED
+ */
+enum acpi_irq_polarity {
+   ACPI_IRQ_ACTIVE_LOW,
+   ACPI_IRQ_ACTIVE_HIGH,
+   ACPI_IRQ_ACTIVE_BOTH,
+};
+
+/**
+ * enum acpi_irq_shared - whether interrupt is shared or not
+ *
+ * @ACPI_IRQ_EXCLUSIVE: only this device uses the interrupt
+ * @ACPI_IRQ_SHARED: other devices may use this interrupt
+ */
+enum acpi_irq_shared {
+   ACPI_IRQ_EXCLUSIVE,
+   ACPI_IRQ_SHARED,
+};
+
+/** enum acpi_irq_wake - indicates whether this interrupt can wake the device 
*/
+enum acpi_irq_wake {
+   ACPI_IRQ_NO_WAKE,
+   ACPI_IRQ_WAKE,
+};
+
+/**
+ * struct acpi_irq - representation of an ACPI interrupt
+ *
+ * @pin: ACPI pin that is monitored for the interrupt
+ * @mode: Edge/level triggering
+ * @polarity: Interrupt polarity
+ * @shared: Whether interrupt is shared or not
+ * @wake: Whether interrupt can wake the device from sleep
+ */
+struct 

[PATCH v2 03/35] acpi: Add a way to check device status

2020-05-10 Thread Simon Glass
At present U-Boot does not support the different ACPI status values, but
it is best to put this logic in a central place. Add a function to get the
device status.

Signed-off-by: Simon Glass 
---

Changes in v2: None
Changes in v1: None

 include/acpi/acpi_device.h | 28 
 lib/acpi/acpi_device.c |  5 +
 test/dm/acpi.c | 12 
 3 files changed, 45 insertions(+)

diff --git a/include/acpi/acpi_device.h b/include/acpi/acpi_device.h
index 37a675f101..09c227489a 100644
--- a/include/acpi/acpi_device.h
+++ b/include/acpi/acpi_device.h
@@ -9,11 +9,28 @@
 #ifndef __ACPI_DEVICE_H
 #define __ACPI_DEVICE_H
 
+#include 
+
 struct udevice;
 
 /* Length of a full path to an ACPI device */
 #define ACPI_PATH_MAX  30
 
+/* Values that can be returned for ACPI device _STA method */
+enum acpi_dev_status {
+   ACPI_DSTATUS_PRESENT= BIT(0),
+   ACPI_DSTATUS_ENABLED= BIT(1),
+   ACPI_DSTATUS_SHOW_IN_UI = BIT(2),
+   ACPI_DSTATUS_OK = BIT(3),
+   ACPI_DSTATUS_HAS_BATTERY= BIT(4),
+
+   ACPI_DSTATUS_ALL_OFF= 0,
+   ACPI_DSTATUS_HIDDEN_ON  = ACPI_DSTATUS_PRESENT | ACPI_DSTATUS_ENABLED |
+   ACPI_DSTATUS_OK,
+   ACPI_DSTATUS_ALL_ON = ACPI_DSTATUS_HIDDEN_ON |
+   ACPI_DSTATUS_SHOW_IN_UI,
+};
+
 /**
  * acpi_device_path() - Get the full path to an ACPI device
  *
@@ -41,4 +58,15 @@ int acpi_device_path(const struct udevice *dev, char *buf, 
int maxlen);
  */
 int acpi_device_scope(const struct udevice *dev, char *scope, int maxlen);
 
+/**
+ * acpi_device_status() - Get the status of a device
+ *
+ * This currently just returns ACPI_DSTATUS_ALL_ON. It does not support
+ * inactive or hidden devices.
+ *
+ * @dev: Device to check
+ * @return device status, as ACPI_DSTATUS_...
+ */
+enum acpi_dev_status acpi_device_status(const struct udevice *dev);
+
 #endif
diff --git a/lib/acpi/acpi_device.c b/lib/acpi/acpi_device.c
index f9af2343c1..7b18cbf148 100644
--- a/lib/acpi/acpi_device.c
+++ b/lib/acpi/acpi_device.c
@@ -81,3 +81,8 @@ int acpi_device_scope(const struct udevice *dev, char *scope, 
int maxlen)
 
return 0;
 }
+
+enum acpi_dev_status acpi_device_status(const struct udevice *dev)
+{
+   return ACPI_DSTATUS_ALL_ON;
+}
diff --git a/test/dm/acpi.c b/test/dm/acpi.c
index c25fe805c4..0c2e12d170 100644
--- a/test/dm/acpi.c
+++ b/test/dm/acpi.c
@@ -384,3 +384,15 @@ static int dm_test_acpi_device_path(struct unit_test_state 
*uts)
return 0;
 }
 DM_TEST(dm_test_acpi_device_path, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
+
+/* Test acpi_device_status() */
+static int dm_test_acpi_device_status(struct unit_test_state *uts)
+{
+   struct udevice *dev;
+
+   ut_assertok(uclass_first_device_err(UCLASS_TEST_ACPI, ));
+   ut_asserteq(ACPI_DSTATUS_ALL_ON, acpi_device_status(dev));
+
+   return 0;
+}
+DM_TEST(dm_test_acpi_device_status, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
-- 
2.26.2.645.ge9eca65c58-goog



[PATCH v2 05/35] acpi: Support generation of ACPI code

2020-05-10 Thread Simon Glass
Add a new file to handle generating ACPI code programatically. This is
used when information must be dynamically added to the tables, e.g. the
SSDT.

Initial support is just for writing simple values.

Signed-off-by: Simon Glass 
---

Changes in v2: None
Changes in v1: None

 include/acpi/acpigen.h | 49 +++
 lib/acpi/Makefile  |  1 +
 lib/acpi/acpigen.c | 38 
 test/dm/Makefile   |  1 +
 test/dm/acpigen.c  | 65 ++
 5 files changed, 154 insertions(+)
 create mode 100644 include/acpi/acpigen.h
 create mode 100644 lib/acpi/acpigen.c
 create mode 100644 test/dm/acpigen.c

diff --git a/include/acpi/acpigen.h b/include/acpi/acpigen.h
new file mode 100644
index 00..8809cdb4e1
--- /dev/null
+++ b/include/acpi/acpigen.h
@@ -0,0 +1,49 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Core ACPI (Advanced Configuration and Power Interface) support
+ *
+ * Copyright 2019 Google LLC
+ *
+ * Modified from coreboot file acpigen.h
+ */
+
+#ifndef __ACPI_ACPIGEN_H
+#define __ACPI_ACPIGEN_H
+
+#include 
+
+struct acpi_ctx;
+
+/**
+ * acpigen_get_current() - Get the current ACPI code output pointer
+ *
+ * @ctx: ACPI context pointer
+ * @return output pointer
+ */
+u8 *acpigen_get_current(struct acpi_ctx *ctx);
+
+/**
+ * acpigen_emit_byte() - Emit a byte to the ACPI code
+ *
+ * @ctx: ACPI context pointer
+ * @data: Value to output
+ */
+void acpigen_emit_byte(struct acpi_ctx *ctx, uint data);
+
+/**
+ * acpigen_emit_word() - Emit a 16-bit word to the ACPI code
+ *
+ * @ctx: ACPI context pointer
+ * @data: Value to output
+ */
+void acpigen_emit_word(struct acpi_ctx *ctx, uint data);
+
+/**
+ * acpigen_emit_dword() - Emit a 32-bit 'double word' to the ACPI code
+ *
+ * @ctx: ACPI context pointer
+ * @data: Value to output
+ */
+void acpigen_emit_dword(struct acpi_ctx *ctx, uint data);
+
+#endif
diff --git a/lib/acpi/Makefile b/lib/acpi/Makefile
index caae6c01bd..85a1f774ad 100644
--- a/lib/acpi/Makefile
+++ b/lib/acpi/Makefile
@@ -1,5 +1,6 @@
 # SPDX-License-Identifier: GPL-2.0+
 #
 
+obj-y += acpigen.o
 obj-y += acpi_device.o
 obj-y += acpi_table.o
diff --git a/lib/acpi/acpigen.c b/lib/acpi/acpigen.c
new file mode 100644
index 00..59bd3af0b7
--- /dev/null
+++ b/lib/acpi/acpigen.c
@@ -0,0 +1,38 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Generation of ACPI (Advanced Configuration and Power Interface) tables
+ *
+ * Copyright 2019 Google LLC
+ * Mostly taken from coreboot
+ */
+
+#define LOG_CATEGORY LOGC_ACPI
+
+#include 
+#include 
+#include 
+#include 
+
+u8 *acpigen_get_current(struct acpi_ctx *ctx)
+{
+   return ctx->current;
+}
+
+void acpigen_emit_byte(struct acpi_ctx *ctx, uint data)
+{
+   *(u8 *)ctx->current++ = data;
+}
+
+void acpigen_emit_word(struct acpi_ctx *ctx, uint data)
+{
+   acpigen_emit_byte(ctx, data & 0xff);
+   acpigen_emit_byte(ctx, (data >> 8) & 0xff);
+}
+
+void acpigen_emit_dword(struct acpi_ctx *ctx, uint data)
+{
+   acpigen_emit_byte(ctx, data & 0xff);
+   acpigen_emit_byte(ctx, (data >> 8) & 0xff);
+   acpigen_emit_byte(ctx, (data >> 16) & 0xff);
+   acpigen_emit_byte(ctx, (data >> 24) & 0xff);
+}
diff --git a/test/dm/Makefile b/test/dm/Makefile
index 6c18fd04ce..e3e0cccf01 100644
--- a/test/dm/Makefile
+++ b/test/dm/Makefile
@@ -14,6 +14,7 @@ obj-$(CONFIG_UT_DM) += test-uclass.o
 obj-$(CONFIG_UT_DM) += core.o
 ifneq ($(CONFIG_SANDBOX),)
 obj-$(CONFIG_ACPIGEN) += acpi.o
+obj-$(CONFIG_ACPIGEN) += acpigen.o
 obj-$(CONFIG_SOUND) += audio.o
 obj-$(CONFIG_BLK) += blk.o
 obj-$(CONFIG_BOARD) += board.o
diff --git a/test/dm/acpigen.c b/test/dm/acpigen.c
new file mode 100644
index 00..68f2b73132
--- /dev/null
+++ b/test/dm/acpigen.c
@@ -0,0 +1,65 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Tests for ACPI code generation
+ *
+ * Copyright 2019 Google LLC
+ * Written by Simon Glass 
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+static int alloc_context(struct acpi_ctx **ctxp)
+{
+   struct acpi_ctx *ctx;
+
+   *ctxp = NULL;
+   ctx = malloc(sizeof(*ctx));
+   if (!ctx)
+   return -ENOMEM;
+   ctx->current = malloc(150);
+   if (!ctx->current)
+   return -ENOMEM;
+   *ctxp = ctx;
+
+   return 0;
+}
+
+static void free_context(struct acpi_ctx **ctxp)
+{
+   free(*ctxp);
+   *ctxp = NULL;
+}
+
+/* Test emitting simple types and acpigen_get_current() */
+static int dm_test_acpi_emit_simple(struct unit_test_state *uts)
+{
+   struct acpi_ctx *ctx;
+   u8 *ptr;
+
+   ut_assertok(alloc_context());
+
+   ptr = acpigen_get_current(ctx);
+   acpigen_emit_byte(ctx, 0x23);
+   ut_asserteq(1, acpigen_get_current(ctx) - ptr);
+   ut_asserteq(0x23, *(u8 *)ptr);
+
+   acpigen_emit_word(ctx, 0x1234);
+   ut_asserteq(3, acpigen_get_current(ctx) - ptr);
+   ut_asserteq(0x1234, get_unaligned((u16 

[PATCH v2 00/35] dm: Add programmatic generation of ACPI tables (part B)

2020-05-10 Thread Simon Glass
NOTE: I have resent this as v1 to avoid confusion

This is split from the original series in an attempt to get things applied
in chunks.

This part includes:
- writing basic ACPI code for integers, strings, names, packages
- writing descriptors for GPIO, I2C, interrupts, SPI
- writing code to enable/disable ACPI peripherals via GPIOs
- writing SSDT and DSDT tables
- additional ways to determine ACPI device names

Much of this code is taken from coreboot and I have tried to avoid
changing the original code for no reason. Changes include:
- splitting the acpi_dp functions into their own file
- adding tests
- adding (lots of) comments
- using a context pointer instead of global variables
- tidying up some code where couldn't resist (e.g. acpigen_emit_namestring())

Changes in v2:
- Fix memset of I2C descriptor
- Fix memset of SPI descriptor

Changes in v1:
- Capitalise ACPI_OPS_PTR
- Split into more patches for review
- Add tests
- Rebase on top of common.h series
- Fix 'the an' typo
- Move header definitions into this patch
- Update sandbox driver slightly for testing
- Switch parameter order of _acpi_fill_ssdt() and make it static
- Fix 'sentinal' and 'METHOD_FILL_SDDT' typos
- Correct the commit subject
- Generalise the ACPI function recursion with acpi_recurse_method()
- Generalise the ACPI function recursion with acpi_recurse_method()
- Use OEM_TABLE_ID instead of ACPI_TABLE_CREATOR
- Update ACPI_DSTATUS enum
- Drop writing of coreboot tables
- Generalise the ACPI function recursion with acpi_recurse_method()
- Use acpi,ddn instead of acpi,desc
- Rename to acpi_device_infer_name()
- Update newly created sandbox tests

Simon Glass (35):
  dm: core: Add an ACPI name for the root node
  acpi: Add a function to get a device path and scope
  acpi: Add a way to check device status
  irq: Add a method to convert an interrupt to ACPI
  acpi: Support generation of ACPI code
  acpi: Support generation of interrupt descriptor
  gpio: Add a method to convert a GPIO to ACPI
  acpi: Support string output
  acpi: Support generation of GPIO descriptor
  acpi: Support generation of a GPIO/irq for a device
  acpi: Support generation of I2C descriptor
  acpi: Support generation of SPI descriptor
  acpigen: Support writing a length
  acpigen: Support writing a package
  acpi: Support writing an integer
  acpi: Support writing a string
  acpi: Support writing a name
  acpi: Support writing a UUID
  acpi: Support writing Device Properties objects via _DSD
  acpi: Support writing a GPIO
  acpi: Support copying properties from device tree to ACPI
  acpi: Add support for various misc ACPI opcodes
  acpi: Add support for writing a Power Resource
  acpi: Add support for writing a GPIO power sequence
  acpi: Add support for a generic power sequence
  acpi: Add support for SSDT generation
  x86: acpi: Move MADT down a bit
  acpi: Record the items added to SSDT
  acpi: Support ordering SSDT data by device
  x86: Allow devices to write an SSDT
  acpi: Add support for DSDT generation
  x86: Allow devices to write to DSDT
  pci: Avoid a crash in device_is_on_pci_bus()
  dm: acpi: Enhance acpi_get_name()
  acpi: Add an acpi split command

 arch/sandbox/dts/test.dts   |  14 +-
 arch/x86/lib/acpi_table.c   |  54 +-
 cmd/acpi.c  |  15 +-
 doc/device-tree-bindings/chosen.txt |   9 +
 doc/device-tree-bindings/device.txt |  13 +
 drivers/core/acpi.c | 207 ++-
 drivers/core/root.c |  13 +
 drivers/gpio/gpio-uclass.c  |  21 +
 drivers/gpio/sandbox.c  |  86 +++
 drivers/i2c/sandbox_i2c.c   |   1 +
 drivers/misc/irq-uclass.c   |  18 +-
 drivers/misc/irq_sandbox.c  |  16 +
 drivers/rtc/sandbox_rtc.c   |  13 +
 drivers/spi/sandbox_spi.c   |   1 +
 include/acpi/acpi_device.h  | 401 +
 include/acpi/acpi_dp.h  | 283 +
 include/acpi/acpi_table.h   |   6 +
 include/acpi/acpigen.h  | 347 +++
 include/asm-generic/gpio.h  |  27 +
 include/dm/acpi.h   |  63 ++
 include/dm/device.h |   2 +-
 include/irq.h   |  43 ++
 include/spi.h   |   4 +-
 include/test/ut.h   |  17 +
 lib/acpi/Makefile   |   3 +
 lib/acpi/acpi_device.c  | 812 ++
 lib/acpi/acpi_dp.c  | 397 +
 lib/acpi/acpigen.c  | 477 +++
 test/dm/Makefile|   2 +
 test/dm/acpi.c  | 277 -
 test/dm/acpi_dp.c   | 511 +
 test/dm/acpigen.c   | 861 
 test/dm/gpio.c  |  62 ++
 test/dm/irq.c   |  23 +
 test/dm/pci.c   |  14 +
 35 files changed, 5080 insertions(+), 33 deletions(-)
 create mode 100644 include/acpi/acpi_device.h
 create 

[PATCH v2 01/35] dm: core: Add an ACPI name for the root node

2020-05-10 Thread Simon Glass
This always has a fixed ACPI name so add it as a driver function.

Signed-off-by: Simon Glass 
Reviewed-by: Wolfgang Wallner 
---

Changes in v2: None
Changes in v1:
- Capitalise ACPI_OPS_PTR

 drivers/core/root.c | 13 +
 1 file changed, 13 insertions(+)

diff --git a/drivers/core/root.c b/drivers/core/root.c
index 7d257ea887..14ae8a953b 100644
--- a/drivers/core/root.c
+++ b/drivers/core/root.c
@@ -12,6 +12,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -373,10 +374,22 @@ int dm_init_and_scan(bool pre_reloc_only)
return 0;
 }
 
+#ifdef CONFIG_ACPIGEN
+static int root_acpi_get_name(const struct udevice *dev, char *out_name)
+{
+   return acpi_copy_name(out_name, "\\_SB");
+}
+
+struct acpi_ops root_acpi_ops = {
+   .get_name   = root_acpi_get_name,
+};
+#endif
+
 /* This is the root driver - all drivers are children of this */
 U_BOOT_DRIVER(root_driver) = {
.name   = "root_driver",
.id = UCLASS_ROOT,
+   ACPI_OPS_PTR(_acpi_ops)
 };
 
 /* This is the root uclass */
-- 
2.26.2.645.ge9eca65c58-goog



Re: [PATCH v3 1/6] clk: rk3399: Add enable/disable clks

2020-05-10 Thread Suniel Mahesh
On Sat, May 9, 2020 at 10:26 PM Jagan Teki 
wrote:

> Yes, most of the high speed peripheral clocks
> in rk3399 enabled by default.
>
> But it would be better to handle them via clk
> enable/disable API for handling proper reset
> conditions like 'usb reset' over command line.
>
> So, enable USB, GMAC clock via enable/disable ops.
>
> Signed-off-by: Jagan Teki 
> ---
> Changes for v3:
> - Update USB clock registers
>
>  drivers/clk/rockchip/clk_rk3399.c | 148 ++
>  1 file changed, 148 insertions(+)
>
> diff --git a/drivers/clk/rockchip/clk_rk3399.c
> b/drivers/clk/rockchip/clk_rk3399.c
> index d822acace1..5d2bdb42c7 100644
> --- a/drivers/clk/rockchip/clk_rk3399.c
> +++ b/drivers/clk/rockchip/clk_rk3399.c
> @@ -1071,12 +1071,160 @@ static int __maybe_unused
> rk3399_clk_set_parent(struct clk *clk,
> return -ENOENT;
>  }
>
> +static int rk3399_clk_enable(struct clk *clk)
> +{
> +   struct rk3399_clk_priv *priv = dev_get_priv(clk->dev);
> +
> +   switch (clk->id) {
> +   case SCLK_MAC:
> +   rk_clrreg(>cru->clkgate_con[5], BIT(5));
> +   break;
> +   case SCLK_MAC_RX:
> +   rk_clrreg(>cru->clkgate_con[5], BIT(8));
> +   break;
> +   case SCLK_MAC_TX:
> +   rk_clrreg(>cru->clkgate_con[5], BIT(9));
> +   break;
> +   case SCLK_MACREF:
> +   rk_clrreg(>cru->clkgate_con[5], BIT(7));
> +   break;
> +   case SCLK_MACREF_OUT:
> +   rk_clrreg(>cru->clkgate_con[5], BIT(6));
> +   break;
> +   case ACLK_GMAC:
> +   rk_clrreg(>cru->clkgate_con[32], BIT(0));
> +   break;
> +   case PCLK_GMAC:
> +   rk_clrreg(>cru->clkgate_con[32], BIT(2));
> +   break;
> +   case SCLK_USB3OTG0_REF:
> +   rk_clrreg(>cru->clkgate_con[12], BIT(1));
> +   break;
> +   case SCLK_USB3OTG1_REF:
> +   rk_clrreg(>cru->clkgate_con[12], BIT(2));
> +   break;
> +   case SCLK_USB3OTG0_SUSPEND:
> +   rk_clrreg(>cru->clkgate_con[12], BIT(3));
> +   break;
> +   case SCLK_USB3OTG1_SUSPEND:
> +   rk_clrreg(>cru->clkgate_con[12], BIT(4));
> +   break;
> +   case ACLK_USB3OTG0:
> +   rk_clrreg(>cru->clkgate_con[30], BIT(1));
> +   break;
> +   case ACLK_USB3OTG1:
> +   rk_clrreg(>cru->clkgate_con[30], BIT(2));
> +   break;
> +   case ACLK_USB3_RKSOC_AXI_PERF:
> +   rk_clrreg(>cru->clkgate_con[30], BIT(3));
> +   break;
> +   case ACLK_USB3:
> +   rk_clrreg(>cru->clkgate_con[12], BIT(0));
> +   break;
> +   case ACLK_USB3_GRF:
> +   rk_clrreg(>cru->clkgate_con[30], BIT(4));
> +   break;
> +   case HCLK_HOST0:
> +   rk_clrreg(>cru->clksel_con[20], BIT(5));
> +   break;
> +   case HCLK_HOST0_ARB:
> +   rk_clrreg(>cru->clksel_con[20], BIT(6));
> +   break;
> +   case HCLK_HOST1:
> +   rk_clrreg(>cru->clksel_con[20], BIT(7));
> +   break;
> +   case HCLK_HOST1_ARB:
> +   rk_clrreg(>cru->clksel_con[20], BIT(8));
> +   break;
> +   default:
> +   debug("%s: unsupported clk %ld\n", __func__, clk->id);
> +   return -ENOENT;
> +   }
> +
> +   return 0;
> +}
> +
> +static int rk3399_clk_disable(struct clk *clk)
> +{
> +   struct rk3399_clk_priv *priv = dev_get_priv(clk->dev);
> +
> +   switch (clk->id) {
> +   case SCLK_MAC:
> +   rk_setreg(>cru->clkgate_con[5], BIT(5));
> +   break;
> +   case SCLK_MAC_RX:
> +   rk_setreg(>cru->clkgate_con[5], BIT(8));
> +   break;
> +   case SCLK_MAC_TX:
> +   rk_setreg(>cru->clkgate_con[5], BIT(9));
> +   break;
> +   case SCLK_MACREF:
> +   rk_setreg(>cru->clkgate_con[5], BIT(7));
> +   break;
> +   case SCLK_MACREF_OUT:
> +   rk_setreg(>cru->clkgate_con[5], BIT(6));
> +   break;
> +   case ACLK_GMAC:
> +   rk_setreg(>cru->clkgate_con[32], BIT(0));
> +   break;
> +   case PCLK_GMAC:
> +   rk_setreg(>cru->clkgate_con[32], BIT(2));
> +   break;
> +   case SCLK_USB3OTG0_REF:
> +   rk_setreg(>cru->clkgate_con[12], BIT(1));
> +   break;
> +   case SCLK_USB3OTG1_REF:
> +   rk_setreg(>cru->clkgate_con[12], BIT(2));
> +   break;
> +   case SCLK_USB3OTG0_SUSPEND:
> +   rk_setreg(>cru->clkgate_con[12], BIT(3));
> +   break;
> +   case SCLK_USB3OTG1_SUSPEND:
> +   rk_setreg(>cru->clkgate_con[12], BIT(4));
> +   break;
> +   case ACLK_USB3OTG0:
> +   rk_setreg(>cru->clkgate_con[30], 

Re: [PATCH v3 5/6] rockchip: Enable PCIe/M.2 on rk3399 board w/ M.2

2020-05-10 Thread Suniel Mahesh
On Sat, May 9, 2020 at 10:26 PM Jagan Teki 
wrote:

> Enable PCIe/M.2 support on
> - NanoPC-T4
> - ROC-RK3399-PC Mezzanine boards.
>
> Signed-off-by: Jagan Teki 
> ---
> Changes for v3:
> - none
>
>  arch/arm/dts/rk3399-u-boot.dtsi   | 1 +
>  configs/nanopc-t4-rk3399_defconfig| 4 
>  configs/roc-pc-mezzanine-rk3399_defconfig | 4 
>  3 files changed, 9 insertions(+)
>
> diff --git a/arch/arm/dts/rk3399-u-boot.dtsi
> b/arch/arm/dts/rk3399-u-boot.dtsi
> index 9bb130a92a..d66d5b6a38 100644
> --- a/arch/arm/dts/rk3399-u-boot.dtsi
> +++ b/arch/arm/dts/rk3399-u-boot.dtsi
> @@ -8,6 +8,7 @@
> aliases {
> mmc0 = 
> mmc1 = 
> +   pci0 = 
> };
>
> cic: syscon@ff62 {
> diff --git a/configs/nanopc-t4-rk3399_defconfig
> b/configs/nanopc-t4-rk3399_defconfig
> index ae865908d8..3bb5b498f2 100644
> --- a/configs/nanopc-t4-rk3399_defconfig
> +++ b/configs/nanopc-t4-rk3399_defconfig
> @@ -18,6 +18,7 @@ CONFIG_CMD_BOOTZ=y
>  CONFIG_CMD_GPT=y
>  CONFIG_CMD_MMC=y
>  CONFIG_CMD_USB=y
> +CONFIG_CMD_PCI=y
>  # CONFIG_CMD_SETEXPR is not set
>  CONFIG_CMD_TIME=y
>  CONFIG_SPL_OF_CONTROL=y
> @@ -34,10 +35,13 @@ CONFIG_MMC_SDHCI_ROCKCHIP=y
>  CONFIG_DM_ETH=y
>  CONFIG_ETH_DESIGNWARE=y
>  CONFIG_GMAC_ROCKCHIP=y
> +CONFIG_NVME=y
> +CONFIG_PCI=y
>  CONFIG_PMIC_RK8XX=y
>  CONFIG_REGULATOR_PWM=y
>  CONFIG_REGULATOR_RK8XX=y
>  CONFIG_PWM_ROCKCHIP=y
> +CONFIG_DM_RESET=y
>  CONFIG_BAUDRATE=150
>  CONFIG_DEBUG_UART_SHIFT=2
>  CONFIG_SYSRESET=y
> diff --git a/configs/roc-pc-mezzanine-rk3399_defconfig
> b/configs/roc-pc-mezzanine-rk3399_defconfig
> index 5a694edc03..0b853805f3 100644
> --- a/configs/roc-pc-mezzanine-rk3399_defconfig
> +++ b/configs/roc-pc-mezzanine-rk3399_defconfig
> @@ -19,6 +19,7 @@ CONFIG_CMD_BOOTZ=y
>  CONFIG_CMD_GPT=y
>  CONFIG_CMD_MMC=y
>  CONFIG_CMD_USB=y
> +CONFIG_CMD_PCI=y
>  # CONFIG_CMD_SETEXPR is not set
>  CONFIG_CMD_TIME=y
>  CONFIG_SPL_OF_CONTROL=y
> @@ -36,11 +37,14 @@ CONFIG_SPI_FLASH_WINBOND=y
>  CONFIG_DM_ETH=y
>  CONFIG_ETH_DESIGNWARE=y
>  CONFIG_GMAC_ROCKCHIP=y
> +CONFIG_NVME=y
> +CONFIG_PCI=y
>  CONFIG_PMIC_RK8XX=y
>  CONFIG_REGULATOR_PWM=y
>  CONFIG_REGULATOR_RK8XX=y
>  CONFIG_PWM_ROCKCHIP=y
>  CONFIG_RAM_RK3399_LPDDR4=y
> +CONFIG_DM_RESET=y
>  CONFIG_BAUDRATE=150
>  CONFIG_DEBUG_UART_SHIFT=2
>  CONFIG_ROCKCHIP_SPI=y
> --
> 2.17.1
>

Tested-by: Suniel Mahesh  #roc-rk3399-pc


Re: [PATCH v3 4/6] pci: Add Rockchip PCIe PHY controller driver

2020-05-10 Thread Suniel Mahesh
On Sat, May 9, 2020 at 10:26 PM Jagan Teki 
wrote:

> Yes, it is possible to have a dedicated UCLASS PHY driver
> for this Rockchip PCIe PHY but there are some issues on
> Generic PHY framework to support the same.
>
> The Generic PHY framework is unable to get the PHY if
> the PHY parent is of a different uclass.
>
> Say if we try to get the PCIe PHY then the phy-uclass
> will look for PHY in the first instance if it is not
> in the root node it will try to probe the parent by
> assuming that the actual PHY is inside the parent PHY
> of UCLASS_PHY. But, in rk3399 hardware representation
> PHY like emmc, usb and pcie are part of syscon which
> is completely a different of UCLASS_SYSCON.
>
> Example:
>
> grf: syscon@ff77 {
>compatible = "rockchip,rk3399-grf", "syscon", "simple-mfd";
>reg = <0x0 0xff77 0x0 0x1>;
>#address-cells = <1>;
>#size-cells = <1>;
>
>pcie_phy: pcie-phy {
>compatible = "rockchip,rk3399-pcie-phy";
>clocks = < SCLK_PCIEPHY_REF>;
>clock-names = "refclk";
>#phy-cells = <1>;
>resets = < SRST_PCIEPHY>;
>drive-impedance-ohm = <50>;
>reset-names = "phy";
>status = "disabled";
>};
> };
>
> Due to this limitation, this patch adds a separate PHY
> driver for Rockchip PCIe. This might be removed in future
> once Generic PHY supports this limitation.
>
> Signed-off-by: Jagan Teki 
> ---
> Changes for v3:
> - none
>
>  drivers/pci/Makefile|   2 +-
>  drivers/pci/pcie_rockchip.c |  50 ++--
>  drivers/pci/pcie_rockchip.h |  63 ++
>  drivers/pci/pcie_rockchip_phy.c | 205 
>  4 files changed, 306 insertions(+), 14 deletions(-)
>  create mode 100644 drivers/pci/pcie_rockchip_phy.c
>
> diff --git a/drivers/pci/Makefile b/drivers/pci/Makefile
> index 493e9354dd..955351c5c2 100644
> --- a/drivers/pci/Makefile
> +++ b/drivers/pci/Makefile
> @@ -43,4 +43,4 @@ obj-$(CONFIG_PCI_PHYTIUM) += pcie_phytium.o
>  obj-$(CONFIG_PCIE_INTEL_FPGA) += pcie_intel_fpga.o
>  obj-$(CONFIG_PCI_KEYSTONE) += pcie_dw_ti.o
>  obj-$(CONFIG_PCIE_MEDIATEK) += pcie_mediatek.o
> -obj-$(CONFIG_PCIE_ROCKCHIP) += pcie_rockchip.o
> +obj-$(CONFIG_PCIE_ROCKCHIP) += pcie_rockchip.o pcie_rockchip_phy.o
> diff --git a/drivers/pci/pcie_rockchip.c b/drivers/pci/pcie_rockchip.c
> index 3f06f783ca..82a8396e42 100644
> --- a/drivers/pci/pcie_rockchip.c
> +++ b/drivers/pci/pcie_rockchip.c
> @@ -159,6 +159,8 @@ static int rockchip_pcie_atr_init(struct rockchip_pcie
> *priv)
>  static int rockchip_pcie_init_port(struct udevice *dev)
>  {
> struct rockchip_pcie *priv = dev_get_priv(dev);
> +   struct rockchip_pcie_phy *phy = pcie_get_phy(priv);
> +   struct rockchip_pcie_phy_ops *ops = phy_get_ops(phy);
> u32 cr, val, status;
> int ret;
>
> @@ -183,29 +185,35 @@ static int rockchip_pcie_init_port(struct udevice
> *dev)
> return ret;
> }
>
> +   ret = ops->init(phy);
> +   if (ret) {
> +   dev_err(dev, "failed to init phy (ret=%d)\n", ret);
> +   goto err_exit_phy;
> +   }
> +
> ret = reset_assert(>core_rst);
> if (ret) {
> dev_err(dev, "failed to assert core reset (ret=%d)\n",
> ret);
> -   return ret;
> +   goto err_exit_phy;
> }
>
> ret = reset_assert(>mgmt_rst);
> if (ret) {
> dev_err(dev, "failed to assert mgmt reset (ret=%d)\n",
> ret);
> -   return ret;
> +   goto err_exit_phy;
> }
>
> ret = reset_assert(>mgmt_sticky_rst);
> if (ret) {
> dev_err(dev, "failed to assert mgmt-sticky reset
> (ret=%d)\n",
> ret);
> -   return ret;
> +   goto err_exit_phy;
> }
>
> ret = reset_assert(>pipe_rst);
> if (ret) {
> dev_err(dev, "failed to assert pipe reset (ret=%d)\n",
> ret);
> -   return ret;
> +   goto err_exit_phy;
> }
>
> udelay(10);
> @@ -213,19 +221,19 @@ static int rockchip_pcie_init_port(struct udevice
> *dev)
> ret = reset_deassert(>pm_rst);
> if (ret) {
> dev_err(dev, "failed to deassert pm reset (ret=%d)\n",
> ret);
> -   return ret;
> +   goto err_exit_phy;
> }
>
> ret = reset_deassert(>aclk_rst);
> if (ret) {
> dev_err(dev, "failed to deassert aclk reset (ret=%d)\n",
> ret);
> -   return ret;
> +   goto err_exit_phy;
> }
>
> ret = reset_deassert(>pclk_rst);
> if (ret) {
> dev_err(dev, "failed to deassert pclk reset (ret=%d)\n",
> ret);
> -   return ret;
> +   goto err_exit_phy;
> }
>
> /* Select GEN1 for now */
> @@ -234,29 +242,35 @@ static int rockchip_pcie_init_port(struct udevice
> *dev)
> cr |= 

Re: [PATCH v3 3/6] pci: Add Rockchip PCIe controller driver

2020-05-10 Thread Suniel Mahesh
On Sat, May 9, 2020 at 10:26 PM Jagan Teki 
wrote:

> Add Rockchip PCIe controller driver for rk3399 platform.
>
> Driver support Gen1 by operating as a Root complex.
>
> Thanks to Patrick for initial work.
>
> Signed-off-by: Patrick Wildt 
> Signed-off-by: Jagan Teki 
> Reviewed-by: Kever Yang 
> ---
> Changes for v3:
> - none
>
>  drivers/pci/Kconfig |   8 +
>  drivers/pci/Makefile|   1 +
>  drivers/pci/pcie_rockchip.c | 467 
>  drivers/pci/pcie_rockchip.h |  79 ++
>  4 files changed, 555 insertions(+)
>  create mode 100644 drivers/pci/pcie_rockchip.c
>  create mode 100644 drivers/pci/pcie_rockchip.h
>
> diff --git a/drivers/pci/Kconfig b/drivers/pci/Kconfig
> index 437cd9a055..3dba84103b 100644
> --- a/drivers/pci/Kconfig
> +++ b/drivers/pci/Kconfig
> @@ -197,4 +197,12 @@ config PCIE_MEDIATEK
>   Say Y here if you want to enable Gen2 PCIe controller,
>   which could be found on MT7623 SoC family.
>
> +config PCIE_ROCKCHIP
> +   bool "Enable Rockchip PCIe driver"
> +   select DM_PCI
> +   default y if ROCKCHIP_RK3399
> +   help
> + Say Y here if you want to enable PCIe controller support on
> + Rockchip SoCs.
> +
>  endif
> diff --git a/drivers/pci/Makefile b/drivers/pci/Makefile
> index c051ecc9f3..493e9354dd 100644
> --- a/drivers/pci/Makefile
> +++ b/drivers/pci/Makefile
> @@ -43,3 +43,4 @@ obj-$(CONFIG_PCI_PHYTIUM) += pcie_phytium.o
>  obj-$(CONFIG_PCIE_INTEL_FPGA) += pcie_intel_fpga.o
>  obj-$(CONFIG_PCI_KEYSTONE) += pcie_dw_ti.o
>  obj-$(CONFIG_PCIE_MEDIATEK) += pcie_mediatek.o
> +obj-$(CONFIG_PCIE_ROCKCHIP) += pcie_rockchip.o
> diff --git a/drivers/pci/pcie_rockchip.c b/drivers/pci/pcie_rockchip.c
> new file mode 100644
> index 00..3f06f783ca
> --- /dev/null
> +++ b/drivers/pci/pcie_rockchip.c
> @@ -0,0 +1,467 @@
> +// SPDX-License-Identifier: GPL-2.0+
> +/*
> + * Rockchip AXI PCIe host controller driver
> + *
> + * Copyright (c) 2016 Rockchip, Inc.
> + * Copyright (c) 2020 Amarula Solutions(India)
> + * Copyright (c) 2020 Jagan Teki 
> + * Copyright (c) 2019 Patrick Wildt 
> + * Copyright (c) 2018 Mark Kettenis 
> + *
> + * Bits taken from Linux Rockchip PCIe host controller.
> + */
> +
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +
> +#include "pcie_rockchip.h"
> +
> +DECLARE_GLOBAL_DATA_PTR;
> +
> +static int rockchip_pcie_off_conf(pci_dev_t bdf, uint offset)
> +{
> +   unsigned int bus = PCI_BUS(bdf);
> +   unsigned int dev = PCI_DEV(bdf);
> +   unsigned int func = PCI_FUNC(bdf);
> +
> +   return (bus << 20) | (dev << 15) | (func << 12) | (offset & ~0x3);
> +}
> +
> +static int rockchip_pcie_rd_conf(const struct udevice *udev, pci_dev_t
> bdf,
> +uint offset, ulong *valuep,
> +enum pci_size_t size)
> +{
> +   struct rockchip_pcie *priv = dev_get_priv(udev);
> +   unsigned int bus = PCI_BUS(bdf);
> +   unsigned int dev = PCI_DEV(bdf);
> +   int where = rockchip_pcie_off_conf(bdf, offset);
> +   ulong value;
> +
> +   if (bus == priv->first_busno && dev == 0) {
> +   value = readl(priv->apb_base + PCIE_RC_NORMAL_BASE +
> where);
> +   *valuep = pci_conv_32_to_size(value, offset, size);
> +   return 0;
> +   }
> +
> +   if ((bus == priv->first_busno + 1) && dev == 0) {
> +   value = readl(priv->axi_base + where);
> +   *valuep = pci_conv_32_to_size(value, offset, size);
> +   return 0;
> +   }
> +
> +   *valuep = pci_get_ff(size);
> +
> +   return 0;
> +}
> +
> +static int rockchip_pcie_wr_conf(struct udevice *udev, pci_dev_t bdf,
> +uint offset, ulong value,
> +enum pci_size_t size)
> +{
> +   struct rockchip_pcie *priv = dev_get_priv(udev);
> +   unsigned int bus = PCI_BUS(bdf);
> +   unsigned int dev = PCI_DEV(bdf);
> +   int where = rockchip_pcie_off_conf(bdf, offset);
> +   ulong old;
> +
> +   if (bus == priv->first_busno && dev == 0) {
> +   old = readl(priv->apb_base + PCIE_RC_NORMAL_BASE + where);
> +   value = pci_conv_size_to_32(old, value, offset, size);
> +   writel(value, priv->apb_base + PCIE_RC_NORMAL_BASE +
> where);
> +   return 0;
> +   }
> +
> +   if ((bus == priv->first_busno + 1) && dev == 0) {
> +   old = readl(priv->axi_base + where);
> +   value = pci_conv_size_to_32(old, value, offset, size);
> +   writel(value, priv->axi_base + where);
> +   return 0;
> +   }
> +
> +   return 0;
> +}
> +
> +static int rockchip_pcie_atr_init(struct rockchip_pcie *priv)
> +{
> +   struct udevice *ctlr = pci_get_controller(priv->dev);
> +   struct 

Re: [PATCH v3 2/6] clk: rk3399: Enable/Disable the PCIEPHY clk

2020-05-10 Thread Suniel Mahesh
On Sat, May 9, 2020 at 10:26 PM Jagan Teki 
wrote:

> Enable/Disable the PCIEPHY clk for rk3399.
>
> CLK is clear in both enable and disable functionality.
>
> Signed-off-by: Jagan Teki 
> ---
> Changes for v3:
> - none
>
>  drivers/clk/rockchip/clk_rk3399.c | 6 ++
>  1 file changed, 6 insertions(+)
>
> diff --git a/drivers/clk/rockchip/clk_rk3399.c
> b/drivers/clk/rockchip/clk_rk3399.c
> index 5d2bdb42c7..5fb72d83c2 100644
> --- a/drivers/clk/rockchip/clk_rk3399.c
> +++ b/drivers/clk/rockchip/clk_rk3399.c
> @@ -1136,6 +1136,9 @@ static int rk3399_clk_enable(struct clk *clk)
> case HCLK_HOST1_ARB:
> rk_clrreg(>cru->clksel_con[20], BIT(8));
> break;
> +   case SCLK_PCIEPHY_REF:
> +   rk_clrreg(>cru->clksel_con[18], BIT(10));
> +   break;
> default:
> debug("%s: unsupported clk %ld\n", __func__, clk->id);
> return -ENOENT;
> @@ -1209,6 +1212,9 @@ static int rk3399_clk_disable(struct clk *clk)
> case HCLK_HOST1_ARB:
> rk_setreg(>cru->clksel_con[20], BIT(8));
> break;
> +   case SCLK_PCIEPHY_REF:
> +   rk_clrreg(>cru->clksel_con[18], BIT(10));
> +   break;
> default:
> debug("%s: unsupported clk %ld\n", __func__, clk->id);
> return -ENOENT;
> --
> 2.17.1
>

Tested-by: Suniel Mahesh  #roc-rk3399-pc


[PATCH v2 38/39] bdinfo: dm: Update fb_base when using driver model

2020-05-10 Thread Simon Glass
Update this value with the address of a video device so that it shows with
the 'bd' command.

It would be better to obtain the address from the uclass by looking in
struct video_uc_platdata for each device. We can move over to that once
DM_VIDEO migration is complete.

Signed-off-by: Simon Glass 
Reviewed-by: Bin Meng 
---

Changes in v2: None

 drivers/video/video-uclass.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/drivers/video/video-uclass.c b/drivers/video/video-uclass.c
index 3d658e61d7..e7229cefaf 100644
--- a/drivers/video/video-uclass.c
+++ b/drivers/video/video-uclass.c
@@ -82,6 +82,7 @@ int video_reserve(ulong *addrp)
  __func__, size, *addrp, dev->name);
}
gd->video_bottom = *addrp;
+   gd->fb_base = *addrp;
debug("Video frame buffers from %lx to %lx\n", gd->video_bottom,
  gd->video_top);
 
-- 
2.26.2.645.ge9eca65c58-goog



Re: [PATCH v3 1/6] clk: rk3399: Add enable/disable clks

2020-05-10 Thread Suniel Mahesh
On Sat, May 9, 2020 at 10:26 PM Jagan Teki 
wrote:

> Yes, most of the high speed peripheral clocks
> in rk3399 enabled by default.
>
> But it would be better to handle them via clk
> enable/disable API for handling proper reset
> conditions like 'usb reset' over command line.
>
> So, enable USB, GMAC clock via enable/disable ops.
>
> Signed-off-by: Jagan Teki 
> ---
> Changes for v3:
> - Update USB clock registers
>
>  drivers/clk/rockchip/clk_rk3399.c | 148 ++
>  1 file changed, 148 insertions(+)
>
> diff --git a/drivers/clk/rockchip/clk_rk3399.c
> b/drivers/clk/rockchip/clk_rk3399.c
> index d822acace1..5d2bdb42c7 100644
> --- a/drivers/clk/rockchip/clk_rk3399.c
> +++ b/drivers/clk/rockchip/clk_rk3399.c
> @@ -1071,12 +1071,160 @@ static int __maybe_unused
> rk3399_clk_set_parent(struct clk *clk,
> return -ENOENT;
>  }
>
> +static int rk3399_clk_enable(struct clk *clk)
> +{
> +   struct rk3399_clk_priv *priv = dev_get_priv(clk->dev);
> +
> +   switch (clk->id) {
> +   case SCLK_MAC:
> +   rk_clrreg(>cru->clkgate_con[5], BIT(5));
> +   break;
> +   case SCLK_MAC_RX:
> +   rk_clrreg(>cru->clkgate_con[5], BIT(8));
> +   break;
> +   case SCLK_MAC_TX:
> +   rk_clrreg(>cru->clkgate_con[5], BIT(9));
> +   break;
> +   case SCLK_MACREF:
> +   rk_clrreg(>cru->clkgate_con[5], BIT(7));
> +   break;
> +   case SCLK_MACREF_OUT:
> +   rk_clrreg(>cru->clkgate_con[5], BIT(6));
> +   break;
> +   case ACLK_GMAC:
> +   rk_clrreg(>cru->clkgate_con[32], BIT(0));
> +   break;
> +   case PCLK_GMAC:
> +   rk_clrreg(>cru->clkgate_con[32], BIT(2));
> +   break;
> +   case SCLK_USB3OTG0_REF:
> +   rk_clrreg(>cru->clkgate_con[12], BIT(1));
> +   break;
> +   case SCLK_USB3OTG1_REF:
> +   rk_clrreg(>cru->clkgate_con[12], BIT(2));
> +   break;
> +   case SCLK_USB3OTG0_SUSPEND:
> +   rk_clrreg(>cru->clkgate_con[12], BIT(3));
> +   break;
> +   case SCLK_USB3OTG1_SUSPEND:
> +   rk_clrreg(>cru->clkgate_con[12], BIT(4));
> +   break;
> +   case ACLK_USB3OTG0:
> +   rk_clrreg(>cru->clkgate_con[30], BIT(1));
> +   break;
> +   case ACLK_USB3OTG1:
> +   rk_clrreg(>cru->clkgate_con[30], BIT(2));
> +   break;
> +   case ACLK_USB3_RKSOC_AXI_PERF:
> +   rk_clrreg(>cru->clkgate_con[30], BIT(3));
> +   break;
> +   case ACLK_USB3:
> +   rk_clrreg(>cru->clkgate_con[12], BIT(0));
> +   break;
> +   case ACLK_USB3_GRF:
> +   rk_clrreg(>cru->clkgate_con[30], BIT(4));
> +   break;
> +   case HCLK_HOST0:
> +   rk_clrreg(>cru->clksel_con[20], BIT(5));
> +   break;
> +   case HCLK_HOST0_ARB:
> +   rk_clrreg(>cru->clksel_con[20], BIT(6));
> +   break;
> +   case HCLK_HOST1:
> +   rk_clrreg(>cru->clksel_con[20], BIT(7));
> +   break;
> +   case HCLK_HOST1_ARB:
> +   rk_clrreg(>cru->clksel_con[20], BIT(8));
> +   break;
> +   default:
> +   debug("%s: unsupported clk %ld\n", __func__, clk->id);
> +   return -ENOENT;
> +   }
> +
> +   return 0;
> +}
> +
> +static int rk3399_clk_disable(struct clk *clk)
> +{
> +   struct rk3399_clk_priv *priv = dev_get_priv(clk->dev);
> +
> +   switch (clk->id) {
> +   case SCLK_MAC:
> +   rk_setreg(>cru->clkgate_con[5], BIT(5));
> +   break;
> +   case SCLK_MAC_RX:
> +   rk_setreg(>cru->clkgate_con[5], BIT(8));
> +   break;
> +   case SCLK_MAC_TX:
> +   rk_setreg(>cru->clkgate_con[5], BIT(9));
> +   break;
> +   case SCLK_MACREF:
> +   rk_setreg(>cru->clkgate_con[5], BIT(7));
> +   break;
> +   case SCLK_MACREF_OUT:
> +   rk_setreg(>cru->clkgate_con[5], BIT(6));
> +   break;
> +   case ACLK_GMAC:
> +   rk_setreg(>cru->clkgate_con[32], BIT(0));
> +   break;
> +   case PCLK_GMAC:
> +   rk_setreg(>cru->clkgate_con[32], BIT(2));
> +   break;
> +   case SCLK_USB3OTG0_REF:
> +   rk_setreg(>cru->clkgate_con[12], BIT(1));
> +   break;
> +   case SCLK_USB3OTG1_REF:
> +   rk_setreg(>cru->clkgate_con[12], BIT(2));
> +   break;
> +   case SCLK_USB3OTG0_SUSPEND:
> +   rk_setreg(>cru->clkgate_con[12], BIT(3));
> +   break;
> +   case SCLK_USB3OTG1_SUSPEND:
> +   rk_setreg(>cru->clkgate_con[12], BIT(4));
> +   break;
> +   case ACLK_USB3OTG0:
> +   rk_setreg(>cru->clkgate_con[30], 

[PATCH v2 39/39] bdinfo: x86: vesa: Update fb_base to the correct value

2020-05-10 Thread Simon Glass
Set this value in global_data so that it is reported correctly on x86
boards.

In fact, U-Boot allocates space for the frame buffer even though it is not
used. Then the FSP picks the address itself (e.g. 0xb000). So the
value set by U-Boot (high in memory with everything else that is
relocated), is not actually the correct value.

Signed-off-by: Simon Glass 
Reviewed-by: Bin Meng 
---

Changes in v2:
- Update the commit message to explain the address more

 arch/x86/lib/fsp/fsp_graphics.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/arch/x86/lib/fsp/fsp_graphics.c b/arch/x86/lib/fsp/fsp_graphics.c
index 98b762209f..46fb907dc3 100644
--- a/arch/x86/lib/fsp/fsp_graphics.c
+++ b/arch/x86/lib/fsp/fsp_graphics.c
@@ -96,6 +96,7 @@ static int fsp_video_probe(struct udevice *dev)
 * For IGD, it seems to be always on BAR2.
 */
vesa->phys_base_ptr = dm_pci_read_bar32(dev, 2);
+   gd->fb_base = vesa->phys_base_ptr;
 
ret = vbe_setup_video_priv(vesa, uc_priv, plat);
if (ret)
@@ -104,8 +105,8 @@ static int fsp_video_probe(struct udevice *dev)
mtrr_add_request(MTRR_TYPE_WRCOMB, vesa->phys_base_ptr, 256 << 20);
mtrr_commit(true);
 
-   printf("%dx%dx%d\n", uc_priv->xsize, uc_priv->ysize,
-  vesa->bits_per_pixel);
+   printf("%dx%dx%d @ %x\n", uc_priv->xsize, uc_priv->ysize,
+  vesa->bits_per_pixel, vesa->phys_base_ptr);
 
return 0;
 
-- 
2.26.2.645.ge9eca65c58-goog



[PATCH v2 35/39] bdinfo: m68k: Move m68k-specific info into its own file

2020-05-10 Thread Simon Glass
We don't really want to have m68k-specific code in a generic file. Create
a new arch-specific function to hold it, and move it into that.

Make the function weak so that any arch can implement it.

Signed-off-by: Simon Glass 
Reviewed-by: Bin Meng 
Tested-by: Angelo Dureghello 
---

Changes in v2: None

 arch/m68k/lib/Makefile |  1 +
 arch/m68k/lib/bdinfo.c | 29 +
 cmd/bdinfo.c   | 15 ---
 3 files changed, 30 insertions(+), 15 deletions(-)
 create mode 100644 arch/m68k/lib/bdinfo.c

diff --git a/arch/m68k/lib/Makefile b/arch/m68k/lib/Makefile
index a040f40eb8..b66d66afd2 100644
--- a/arch/m68k/lib/Makefile
+++ b/arch/m68k/lib/Makefile
@@ -7,6 +7,7 @@
 ## if the user asked for it
 lib-$(CONFIG_USE_PRIVATE_LIBGCC) += lshrdi3.o muldi3.o ashldi3.o
 
+obj-y  += bdinfo.o
 obj-$(CONFIG_CMD_BOOTM) += bootm.o
 obj-y  += cache.o
 obj-y  += interrupts.o
diff --git a/arch/m68k/lib/bdinfo.c b/arch/m68k/lib/bdinfo.c
new file mode 100644
index 00..09a1abfc21
--- /dev/null
+++ b/arch/m68k/lib/bdinfo.c
@@ -0,0 +1,29 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * PPC-specific information for the 'bd' command
+ *
+ * (C) Copyright 2003
+ * Wolfgang Denk, DENX Software Engineering, w...@denx.de.
+ */
+
+#include 
+#include 
+
+DECLARE_GLOBAL_DATA_PTR;
+
+void arch_print_bdinfo(void)
+{
+   bd_t *bd = gd->bd;
+
+#if defined(CONFIG_SYS_MBAR)
+   bdinfo_print_num("mbar", bd->bi_mbar_base);
+#endif
+   bdinfo_print_mhz("cpufreq", bd->bi_intfreq);
+   if (IS_ENABLED(CONFIG_PCI))
+   bdinfo_print_mhz("pcifreq", bd->bi_pcifreq);
+#ifdef CONFIG_EXTRA_CLOCK
+   bdinfo_print_mhz("flbfreq", bd->bi_flbfreq);
+   bdinfo_print_mhz("inpfreq", bd->bi_inpfreq);
+   bdinfo_print_mhz("vcofreq", bd->bi_vcofreq);
+#endif
+}
diff --git a/cmd/bdinfo.c b/cmd/bdinfo.c
index 97748c012e..b31ea66b74 100644
--- a/cmd/bdinfo.c
+++ b/cmd/bdinfo.c
@@ -107,21 +107,6 @@ int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char 
*const argv[])
if (IS_ENABLED(CONFIG_PPC) || IS_ENABLED(CONFIG_M68K))
bdinfo_print_mhz("busfreq", bd->bi_busfreq);
 
-   /* The rest are used only by m68k */
-#ifdef CONFIG_M68K
-#if defined(CONFIG_SYS_MBAR)
-   bdinfo_print_num("mbar", bd->bi_mbar_base);
-#endif
-   bdinfo_print_mhz("cpufreq", bd->bi_intfreq);
-   if (IS_ENABLED(CONFIG_PCI))
-   bdinfo_print_mhz("pcifreq", bd->bi_pcifreq);
-#ifdef CONFIG_EXTRA_CLOCK
-   bdinfo_print_mhz("flbfreq", bd->bi_flbfreq);
-   bdinfo_print_mhz("inpfreq", bd->bi_inpfreq);
-   bdinfo_print_mhz("vcofreq", bd->bi_vcofreq);
-#endif
-#endif
-
return 0;
 }
 
-- 
2.26.2.645.ge9eca65c58-goog



[PATCH v2 37/39] bdinfo: Update the file comments

2020-05-10 Thread Simon Glass
Update the comment at the top of the file to indicate what this file does.
Also drop the line at the bottom and an unnecessary header include.

Signed-off-by: Simon Glass 
Reviewed-by: Bin Meng 
---

Changes in v2: None

 cmd/bdinfo.c | 8 ++--
 1 file changed, 2 insertions(+), 6 deletions(-)

diff --git a/cmd/bdinfo.c b/cmd/bdinfo.c
index e8c800c30c..66d78fd74d 100644
--- a/cmd/bdinfo.c
+++ b/cmd/bdinfo.c
@@ -1,17 +1,15 @@
 // SPDX-License-Identifier: GPL-2.0+
 /*
+ * Implements the 'bd' command to show board information
+ *
  * (C) Copyright 2003
  * Wolfgang Denk, DENX Software Engineering, w...@denx.de.
  */
 
-/*
- * Boot support
- */
 #include 
 #include 
 #include 
 #include 
-#include 
 
 DECLARE_GLOBAL_DATA_PTR;
 
@@ -102,8 +100,6 @@ int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char 
*const argv[])
return 0;
 }
 
-/*  */
-
 U_BOOT_CMD(
bdinfo, 1,  1,  do_bdinfo,
"print Board Info structure",
-- 
2.26.2.645.ge9eca65c58-goog



[PATCH v2 36/39] bdinfo: m68k: ppc: Move arch-specific code from bdinfo

2020-05-10 Thread Simon Glass
We don't have an easy way to share these three lines of code with two
architectures. We also want to make it clear that this code is actually
arch-specific.

So just duplicate it in each arch-specific file.

Signed-off-by: Simon Glass 
Reviewed-by: Bin Meng 
Reviewed-by: Stefan Roese 
---

Changes in v2: None

 arch/m68k/lib/bdinfo.c| 5 +
 arch/powerpc/lib/bdinfo.c | 5 +
 cmd/bdinfo.c  | 8 
 3 files changed, 10 insertions(+), 8 deletions(-)

diff --git a/arch/m68k/lib/bdinfo.c b/arch/m68k/lib/bdinfo.c
index 09a1abfc21..971c47c306 100644
--- a/arch/m68k/lib/bdinfo.c
+++ b/arch/m68k/lib/bdinfo.c
@@ -15,6 +15,11 @@ void arch_print_bdinfo(void)
 {
bd_t *bd = gd->bd;
 
+#if defined(CONFIG_SYS_INIT_RAM_ADDR)
+   bdinfo_print_num("sramstart", (ulong)bd->bi_sramstart);
+   bdinfo_print_num("sramsize", (ulong)bd->bi_sramsize);
+#endif
+   bdinfo_print_mhz("busfreq", bd->bi_busfreq);
 #if defined(CONFIG_SYS_MBAR)
bdinfo_print_num("mbar", bd->bi_mbar_base);
 #endif
diff --git a/arch/powerpc/lib/bdinfo.c b/arch/powerpc/lib/bdinfo.c
index da09bb276f..d8c64155f0 100644
--- a/arch/powerpc/lib/bdinfo.c
+++ b/arch/powerpc/lib/bdinfo.c
@@ -20,6 +20,11 @@ void arch_print_bdinfo(void)
 {
bd_t *bd = gd->bd;
 
+#if defined(CONFIG_SYS_INIT_RAM_ADDR)
+   bdinfo_print_num("sramstart", (ulong)bd->bi_sramstart);
+   bdinfo_print_num("sramsize", (ulong)bd->bi_sramsize);
+#endif
+   bdinfo_print_mhz("busfreq", bd->bi_busfreq);
 #if defined(CONFIG_MPC8xx) || defined(CONFIG_E500)
bdinfo_print_num("immr_base", bd->bi_immr_base);
 #endif
diff --git a/cmd/bdinfo.c b/cmd/bdinfo.c
index b31ea66b74..e8c800c30c 100644
--- a/cmd/bdinfo.c
+++ b/cmd/bdinfo.c
@@ -99,14 +99,6 @@ int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char 
*const argv[])
 
arch_print_bdinfo();
 
-   /* This is used by m68k and ppc */
-#if defined(CONFIG_SYS_INIT_RAM_ADDR)
-   bdinfo_print_num("sramstart", (ulong)bd->bi_sramstart);
-   bdinfo_print_num("sramsize", (ulong)bd->bi_sramsize);
-#endif
-   if (IS_ENABLED(CONFIG_PPC) || IS_ENABLED(CONFIG_M68K))
-   bdinfo_print_mhz("busfreq", bd->bi_busfreq);
-
return 0;
 }
 
-- 
2.26.2.645.ge9eca65c58-goog



[PATCH v2 32/39] bdinfo: Export some basic printing functions

2020-05-10 Thread Simon Glass
At present the functions to print a number and a frequency are static. We
want to move some of the code in here to an arch-specific file. For
consistency that code should use these same functions. So export them with
an appropriate name.

Signed-off-by: Simon Glass 
Reviewed-by: Bin Meng 
---

Changes in v2: None

 cmd/bdinfo.c   | 78 +-
 include/init.h |  6 
 2 files changed, 45 insertions(+), 39 deletions(-)

diff --git a/cmd/bdinfo.c b/cmd/bdinfo.c
index e30d530411..c6096a5fd3 100644
--- a/cmd/bdinfo.c
+++ b/cmd/bdinfo.c
@@ -15,7 +15,7 @@
 
 DECLARE_GLOBAL_DATA_PTR;
 
-static void print_num(const char *name, ulong value)
+void bdinfo_print_num(const char *name, ulong value)
 {
printf("%-12s= 0x%0*lx\n", name, 2 * (int)sizeof(value), value);
 }
@@ -38,7 +38,7 @@ static void print_lnum(const char *name, unsigned long long 
value)
printf("%-12s= 0x%.8llX\n", name, value);
 }
 
-static void print_mhz(const char *name, unsigned long hz)
+void bdinfo_print_mhz(const char *name, unsigned long hz)
 {
char buf[32];
 
@@ -52,9 +52,9 @@ static void print_bi_dram(const bd_t *bd)
 
for (i = 0; i < CONFIG_NR_DRAM_BANKS; ++i) {
if (bd->bi_dram[i].size) {
-   print_num("DRAM bank",  i);
-   print_num("-> start",   bd->bi_dram[i].start);
-   print_num("-> size",bd->bi_dram[i].size);
+   bdinfo_print_num("DRAM bank",   i);
+   bdinfo_print_num("-> start",bd->bi_dram[i].start);
+   bdinfo_print_num("-> size", bd->bi_dram[i].size);
}
}
 #endif
@@ -70,50 +70,50 @@ int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char 
*const argv[])
bd_t *bd = gd->bd;
 
 #ifdef DEBUG
-   print_num("bd address", (ulong)bd);
+   bdinfo_print_num("bd address", (ulong)bd);
 #endif
if (IS_ENABLED(CONFIG_ARM))
-   print_num("arch_number", bd->bi_arch_number);
-   print_num("boot_params", (ulong)bd->bi_boot_params);
+   bdinfo_print_num("arch_number", bd->bi_arch_number);
+   bdinfo_print_num("boot_params", (ulong)bd->bi_boot_params);
print_bi_dram(bd);
-   print_num("memstart", (ulong)bd->bi_memstart);
+   bdinfo_print_num("memstart", (ulong)bd->bi_memstart);
print_lnum("memsize", (u64)bd->bi_memsize);
-   print_num("flashstart", (ulong)bd->bi_flashstart);
-   print_num("flashsize", (ulong)bd->bi_flashsize);
-   print_num("flashoffset", (ulong)bd->bi_flashoffset);
+   bdinfo_print_num("flashstart", (ulong)bd->bi_flashstart);
+   bdinfo_print_num("flashsize", (ulong)bd->bi_flashsize);
+   bdinfo_print_num("flashoffset", (ulong)bd->bi_flashoffset);
printf("baudrate= %u bps\n", gd->baudrate);
-   print_num("relocaddr", gd->relocaddr);
-   print_num("reloc off", gd->reloc_off);
+   bdinfo_print_num("relocaddr", gd->relocaddr);
+   bdinfo_print_num("reloc off", gd->reloc_off);
printf("%-12s= %u-bit\n", "Build", (uint)sizeof(void *) * 8);
if (IS_ENABLED(CONFIG_CMD_NET)) {
printf("current eth = %s\n", eth_get_name());
print_eth(0);
printf("IP addr = %s\n", env_get("ipaddr"));
}
-   print_num("fdt_blob", (ulong)gd->fdt_blob);
-   print_num("new_fdt", (ulong)gd->new_fdt);
-   print_num("fdt_size", (ulong)gd->fdt_size);
+   bdinfo_print_num("fdt_blob", (ulong)gd->fdt_blob);
+   bdinfo_print_num("new_fdt", (ulong)gd->new_fdt);
+   bdinfo_print_num("fdt_size", (ulong)gd->fdt_size);
 #if defined(CONFIG_LCD) || defined(CONFIG_VIDEO) || defined(CONFIG_DM_VIDEO)
-   print_num("FB base  ", gd->fb_base);
+   bdinfo_print_num("FB base  ", gd->fb_base);
 #endif
 
/* This section is used only by ARM */
 #ifdef CONFIG_ARM
 #ifdef CONFIG_SYS_MEM_RESERVE_SECURE
if (gd->arch.secure_ram & MEM_RESERVE_SECURE_SECURED) {
-   print_num("Secure ram",
+   bdinfo_print_num("Secure ram",
  gd->arch.secure_ram & MEM_RESERVE_SECURE_ADDR_MASK);
}
 #endif
 #ifdef CONFIG_RESV_RAM
if (gd->arch.resv_ram)
-   print_num("Reserved ram", gd->arch.resv_ram);
+   bdinfo_print_num("Reserved ram", gd->arch.resv_ram);
 #endif
 #if !(CONFIG_IS_ENABLED(SYS_ICACHE_OFF) && CONFIG_IS_ENABLED(SYS_DCACHE_OFF))
-   print_num("TLB addr", gd->arch.tlb_addr);
+   bdinfo_print_num("TLB addr", gd->arch.tlb_addr);
 #endif
-   print_num("irq_sp", gd->irq_sp);/* irq stack pointer */
-   print_num("sp start ", gd->start_addr_sp);
+   bdinfo_print_num("irq_sp", gd->irq_sp); /* irq stack pointer */
+   bdinfo_print_num("sp start ", gd->start_addr_sp);
/*
 * TODO: Currently only support for davinci SOC's is added.
 * Remove this check once all the board implement 

[PATCH v2 33/39] bdinfo: arm: Move ARM-specific info into its own file

2020-05-10 Thread Simon Glass
We don't really want to have ARM-specific code in a generic file. Create
a new arch-specific function to hold it, and move it into that.

Make the function weak so that any arch can implement it.

Signed-off-by: Simon Glass 
Reviewed-by: Bin Meng 
---

Changes in v2:
- Leave MULTI_DTB_FIT in the generic file as it is not really ARM-specific
- Move ARM's bi_arch_number over to ARM-specific code as well

 arch/arm/lib/Makefile |  1 +
 arch/arm/lib/bdinfo.c | 51 +++
 cmd/bdinfo.c  | 45 ++
 include/init.h|  3 +++
 4 files changed, 62 insertions(+), 38 deletions(-)
 create mode 100644 arch/arm/lib/bdinfo.c

diff --git a/arch/arm/lib/Makefile b/arch/arm/lib/Makefile
index b839aa7a50..27b12e7f2b 100644
--- a/arch/arm/lib/Makefile
+++ b/arch/arm/lib/Makefile
@@ -43,6 +43,7 @@ obj-$(CONFIG_$(SPL_TPL_)USE_ARCH_MEMSET) += memset.o
 obj-$(CONFIG_$(SPL_TPL_)USE_ARCH_MEMCPY) += memcpy.o
 obj-$(CONFIG_SEMIHOSTING) += semihosting.o
 
+obj-y  += bdinfo.o
 obj-y  += sections.o
 obj-y  += stack.o
 ifdef CONFIG_CPU_V7M
diff --git a/arch/arm/lib/bdinfo.c b/arch/arm/lib/bdinfo.c
new file mode 100644
index 00..81c9291584
--- /dev/null
+++ b/arch/arm/lib/bdinfo.c
@@ -0,0 +1,51 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * ARM-specific information for the 'bd' command
+ *
+ * (C) Copyright 2003
+ * Wolfgang Denk, DENX Software Engineering, w...@denx.de.
+ */
+
+#include 
+#include 
+
+DECLARE_GLOBAL_DATA_PTR;
+
+void arch_print_bdinfo(void)
+{
+   bd_t *bd = gd->bd;
+
+   bdinfo_print_num("arch_number", bd->bi_arch_number);
+#ifdef CONFIG_SYS_MEM_RESERVE_SECURE
+   if (gd->arch.secure_ram & MEM_RESERVE_SECURE_SECURED) {
+   bdinfo_print_num("Secure ram",
+gd->arch.secure_ram &
+MEM_RESERVE_SECURE_ADDR_MASK);
+   }
+#endif
+#ifdef CONFIG_RESV_RAM
+   if (gd->arch.resv_ram)
+   bdinfo_print_num("Reserved ram", gd->arch.resv_ram);
+#endif
+#if !(CONFIG_IS_ENABLED(SYS_ICACHE_OFF) && CONFIG_IS_ENABLED(SYS_DCACHE_OFF))
+   bdinfo_print_num("TLB addr", gd->arch.tlb_addr);
+#endif
+   bdinfo_print_num("irq_sp", gd->irq_sp); /* irq stack pointer */
+   bdinfo_print_num("sp start ", gd->start_addr_sp);
+   /*
+* TODO: Currently only support for davinci SOC's is added.
+* Remove this check once all the board implement this.
+*/
+#ifdef CONFIG_CLOCKS
+   printf("ARM frequency = %ld MHz\n", bd->bi_arm_freq);
+   printf("DSP frequency = %ld MHz\n", bd->bi_dsp_freq);
+   printf("DDR frequency = %ld MHz\n", bd->bi_ddr_freq);
+#endif
+#ifdef CONFIG_BOARD_TYPES
+   printf("Board Type  = %ld\n", gd->board_type);
+#endif
+#if CONFIG_VAL(SYS_MALLOC_F_LEN)
+   printf("Early malloc usage: %lx / %x\n", gd->malloc_ptr,
+  CONFIG_VAL(SYS_MALLOC_F_LEN));
+#endif
+}
diff --git a/cmd/bdinfo.c b/cmd/bdinfo.c
index c6096a5fd3..05e2da098e 100644
--- a/cmd/bdinfo.c
+++ b/cmd/bdinfo.c
@@ -65,6 +65,10 @@ void __weak board_detail(void)
/* Please define board_detail() for your PPC platform */
 }
 
+__weak void arch_print_bdinfo(void)
+{
+}
+
 int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
 {
bd_t *bd = gd->bd;
@@ -72,8 +76,6 @@ int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char 
*const argv[])
 #ifdef DEBUG
bdinfo_print_num("bd address", (ulong)bd);
 #endif
-   if (IS_ENABLED(CONFIG_ARM))
-   bdinfo_print_num("arch_number", bd->bi_arch_number);
bdinfo_print_num("boot_params", (ulong)bd->bi_boot_params);
print_bi_dram(bd);
bdinfo_print_num("memstart", (ulong)bd->bi_memstart);
@@ -96,44 +98,11 @@ int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char 
*const argv[])
 #if defined(CONFIG_LCD) || defined(CONFIG_VIDEO) || defined(CONFIG_DM_VIDEO)
bdinfo_print_num("FB base  ", gd->fb_base);
 #endif
-
-   /* This section is used only by ARM */
-#ifdef CONFIG_ARM
-#ifdef CONFIG_SYS_MEM_RESERVE_SECURE
-   if (gd->arch.secure_ram & MEM_RESERVE_SECURE_SECURED) {
-   bdinfo_print_num("Secure ram",
- gd->arch.secure_ram & MEM_RESERVE_SECURE_ADDR_MASK);
-   }
-#endif
-#ifdef CONFIG_RESV_RAM
-   if (gd->arch.resv_ram)
-   bdinfo_print_num("Reserved ram", gd->arch.resv_ram);
-#endif
-#if !(CONFIG_IS_ENABLED(SYS_ICACHE_OFF) && CONFIG_IS_ENABLED(SYS_DCACHE_OFF))
-   bdinfo_print_num("TLB addr", gd->arch.tlb_addr);
-#endif
-   bdinfo_print_num("irq_sp", gd->irq_sp); /* irq stack pointer */
-   bdinfo_print_num("sp start ", gd->start_addr_sp);
-   /*
-* TODO: Currently only support for davinci SOC's is added.
-* Remove this check once all the board implement this.
-*/
-#ifdef CONFIG_CLOCKS
-   printf("ARM frequency = %ld MHz\n", gd->bd->bi_arm_freq);
-   printf("DSP frequency = %ld MHz\n", 

[PATCH v2 31/39] bdinfo: net: Inline print_eth_ip_addr()

2020-05-10 Thread Simon Glass
This function only has two lines of code now, so inline it.

Signed-off-by: Simon Glass 
Reviewed-by: Bin Meng 
---

Changes in v2: None

 cmd/bdinfo.c | 15 ---
 1 file changed, 4 insertions(+), 11 deletions(-)

diff --git a/cmd/bdinfo.c b/cmd/bdinfo.c
index e6237daf93..e30d530411 100644
--- a/cmd/bdinfo.c
+++ b/cmd/bdinfo.c
@@ -20,7 +20,6 @@ static void print_num(const char *name, ulong value)
printf("%-12s= 0x%0*lx\n", name, 2 * (int)sizeof(value), value);
 }
 
-__maybe_unused
 static void print_eth(int idx)
 {
char name[10], *val;
@@ -61,14 +60,6 @@ static void print_bi_dram(const bd_t *bd)
 #endif
 }
 
-static void print_eth_ip_addr(void)
-{
-#if defined(CONFIG_CMD_NET)
-   print_eth(0);
-   printf("IP addr = %s\n", env_get("ipaddr"));
-#endif
-}
-
 void __weak board_detail(void)
 {
/* Please define board_detail() for your PPC platform */
@@ -90,13 +81,15 @@ int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char 
*const argv[])
print_num("flashstart", (ulong)bd->bi_flashstart);
print_num("flashsize", (ulong)bd->bi_flashsize);
print_num("flashoffset", (ulong)bd->bi_flashoffset);
-   print_eth_ip_addr();
printf("baudrate= %u bps\n", gd->baudrate);
print_num("relocaddr", gd->relocaddr);
print_num("reloc off", gd->reloc_off);
printf("%-12s= %u-bit\n", "Build", (uint)sizeof(void *) * 8);
-   if (IS_ENABLED(CONFIG_CMD_NET))
+   if (IS_ENABLED(CONFIG_CMD_NET)) {
printf("current eth = %s\n", eth_get_name());
+   print_eth(0);
+   printf("IP addr = %s\n", env_get("ipaddr"));
+   }
print_num("fdt_blob", (ulong)gd->fdt_blob);
print_num("new_fdt", (ulong)gd->new_fdt);
print_num("fdt_size", (ulong)gd->fdt_size);
-- 
2.26.2.645.ge9eca65c58-goog



[PATCH v2 34/39] bdinfo: ppc: Move PPC-specific info into its own file

2020-05-10 Thread Simon Glass
We don't really want to have PPC-specific code in a generic file. Create
a new arch-specific function to hold it, and move it into that.

Make the function weak so that any arch can implement it.

Signed-off-by: Simon Glass 
Reviewed-by: Bin Meng 
Reviewed-by: Stefan Roese 
---

Changes in v2: None

 arch/powerpc/lib/Makefile |  2 ++
 arch/powerpc/lib/bdinfo.c | 41 +++
 cmd/bdinfo.c  | 27 --
 3 files changed, 43 insertions(+), 27 deletions(-)
 create mode 100644 arch/powerpc/lib/bdinfo.c

diff --git a/arch/powerpc/lib/Makefile b/arch/powerpc/lib/Makefile
index 01c9dd51be..f61809ab05 100644
--- a/arch/powerpc/lib/Makefile
+++ b/arch/powerpc/lib/Makefile
@@ -15,6 +15,8 @@ MINIMAL=y
 endif
 endif
 
+obj-y  += bdinfo.o
+
 ifdef MINIMAL
 obj-y += cache.o time.o
 ifndef CONFIG_TIMER
diff --git a/arch/powerpc/lib/bdinfo.c b/arch/powerpc/lib/bdinfo.c
new file mode 100644
index 00..da09bb276f
--- /dev/null
+++ b/arch/powerpc/lib/bdinfo.c
@@ -0,0 +1,41 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * PPC-specific information for the 'bd' command
+ *
+ * (C) Copyright 2003
+ * Wolfgang Denk, DENX Software Engineering, w...@denx.de.
+ */
+
+#include 
+#include 
+
+DECLARE_GLOBAL_DATA_PTR;
+
+void __weak board_detail(void)
+{
+   /* Please define board_detail() for your PPC platform */
+}
+
+void arch_print_bdinfo(void)
+{
+   bd_t *bd = gd->bd;
+
+#if defined(CONFIG_MPC8xx) || defined(CONFIG_E500)
+   bdinfo_print_num("immr_base", bd->bi_immr_base);
+#endif
+   bdinfo_print_num("bootflags", bd->bi_bootflags);
+   bdinfo_print_mhz("intfreq", bd->bi_intfreq);
+#ifdef CONFIG_ENABLE_36BIT_PHYS
+   if (IS_ENABLED(CONFIG_PHYS_64BIT))
+   puts("addressing  = 36-bit\n");
+   else
+   puts("addressing  = 32-bit\n");
+#endif
+   board_detail();
+#if defined(CONFIG_CPM2)
+   bdinfo_print_mhz("cpmfreq", bd->bi_cpmfreq);
+   bdinfo_print_mhz("vco", bd->bi_vco);
+   bdinfo_print_mhz("sccfreq", bd->bi_sccfreq);
+   bdinfo_print_mhz("brgfreq", bd->bi_brgfreq);
+#endif
+}
diff --git a/cmd/bdinfo.c b/cmd/bdinfo.c
index 05e2da098e..97748c012e 100644
--- a/cmd/bdinfo.c
+++ b/cmd/bdinfo.c
@@ -60,11 +60,6 @@ static void print_bi_dram(const bd_t *bd)
 #endif
 }
 
-void __weak board_detail(void)
-{
-   /* Please define board_detail() for your PPC platform */
-}
-
 __weak void arch_print_bdinfo(void)
 {
 }
@@ -104,28 +99,6 @@ int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char 
*const argv[])
 
arch_print_bdinfo();
 
-   /* This section is used only by ppc */
-#if defined(CONFIG_MPC8xx) || defined(CONFIG_E500)
-   bdinfo_print_num("immr_base", bd->bi_immr_base);
-#endif
-   if (IS_ENABLED(CONFIG_PPC)) {
-   bdinfo_print_num("bootflags", bd->bi_bootflags);
-   bdinfo_print_mhz("intfreq", bd->bi_intfreq);
-#ifdef CONFIG_ENABLE_36BIT_PHYS
-   if (IS_ENABLED(CONFIG_PHYS_64BIT))
-   puts("addressing  = 36-bit\n");
-   else
-   puts("addressing  = 32-bit\n");
-#endif
-   board_detail();
-   }
-#if defined(CONFIG_CPM2)
-   bdinfo_print_mhz("cpmfreq", bd->bi_cpmfreq);
-   bdinfo_print_mhz("vco", bd->bi_vco);
-   bdinfo_print_mhz("sccfreq", bd->bi_sccfreq);
-   bdinfo_print_mhz("brgfreq", bd->bi_brgfreq);
-#endif
-
/* This is used by m68k and ppc */
 #if defined(CONFIG_SYS_INIT_RAM_ADDR)
bdinfo_print_num("sramstart", (ulong)bd->bi_sramstart);
-- 
2.26.2.645.ge9eca65c58-goog



[PATCH v2 30/39] bdinfo: net: Drop legacy ethernet bdinfo

2020-05-10 Thread Simon Glass
This code pre-dates driver model and the migration date is nearly upon us.
Pare the print_eths() function down and enable it for driver model, since
it works correctly.

The IP address is already printed in print_eth_ip_addr() so we can remove
that.

Since this results in a one-line print_eths() function, inline it.

Signed-off-by: Simon Glass 
---

Changes in v2:
- Squash in the next patch to inline print_eths()

 cmd/bdinfo.c | 26 ++
 1 file changed, 2 insertions(+), 24 deletions(-)

diff --git a/cmd/bdinfo.c b/cmd/bdinfo.c
index 6fa8b32389..e6237daf93 100644
--- a/cmd/bdinfo.c
+++ b/cmd/bdinfo.c
@@ -34,27 +34,6 @@ static void print_eth(int idx)
printf("%-12s= %s\n", name, val);
 }
 
-#ifndef CONFIG_DM_ETH
-__maybe_unused
-static void print_eths(void)
-{
-   struct eth_device *dev;
-   int i = 0;
-
-   do {
-   dev = eth_get_dev_by_index(i);
-   if (dev) {
-   printf("eth%dname= %s\n", i, dev->name);
-   print_eth(i);
-   i++;
-   }
-   } while (dev);
-
-   printf("current eth = %s\n", eth_get_name());
-   printf("ip_addr = %s\n", env_get("ipaddr"));
-}
-#endif
-
 static void print_lnum(const char *name, unsigned long long value)
 {
printf("%-12s= 0x%.8llX\n", name, value);
@@ -116,9 +95,8 @@ int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char 
*const argv[])
print_num("relocaddr", gd->relocaddr);
print_num("reloc off", gd->reloc_off);
printf("%-12s= %u-bit\n", "Build", (uint)sizeof(void *) * 8);
-#if defined(CONFIG_CMD_NET) && !defined(CONFIG_DM_ETH)
-   print_eths();
-#endif
+   if (IS_ENABLED(CONFIG_CMD_NET))
+   printf("current eth = %s\n", eth_get_name());
print_num("fdt_blob", (ulong)gd->fdt_blob);
print_num("new_fdt", (ulong)gd->new_fdt);
print_num("fdt_size", (ulong)gd->fdt_size);
-- 
2.26.2.645.ge9eca65c58-goog



[PATCH v2 29/39] bdinfo: net: ppc: Drop prints for CONFIG_HAS_ETHn

2020-05-10 Thread Simon Glass
These config options have not been migrated to Kconfig. This should be
handled using driver model, iterating over the available Ethernet devices.
For now, remove the code.

Signed-off-by: Simon Glass 
Reviewed-by: Bin Meng 
Reviewed-by: Stefan Roese 
---

Changes in v2: None

 cmd/bdinfo.c | 15 ---
 1 file changed, 15 deletions(-)

diff --git a/cmd/bdinfo.c b/cmd/bdinfo.c
index d0afef5b5e..6fa8b32389 100644
--- a/cmd/bdinfo.c
+++ b/cmd/bdinfo.c
@@ -86,21 +86,6 @@ static void print_eth_ip_addr(void)
 {
 #if defined(CONFIG_CMD_NET)
print_eth(0);
-#if defined(CONFIG_HAS_ETH1)
-   print_eth(1);
-#endif
-#if defined(CONFIG_HAS_ETH2)
-   print_eth(2);
-#endif
-#if defined(CONFIG_HAS_ETH3)
-   print_eth(3);
-#endif
-#if defined(CONFIG_HAS_ETH4)
-   print_eth(4);
-#endif
-#if defined(CONFIG_HAS_ETH5)
-   print_eth(5);
-#endif
printf("IP addr = %s\n", env_get("ipaddr"));
 #endif
 }
-- 
2.26.2.645.ge9eca65c58-goog



[PATCH v2 28/39] bdinfo: net: ppc: Drop bi_enet1addr and other similar info

2020-05-10 Thread Simon Glass
These values were 'old' in 2013 so it should be safe to remove them. They
are never set in U-Boot anyway, so the values will always be zero.

Signed-off-by: Simon Glass 
Reviewed-by: Bin Meng 
Reviewed-by: Stefan Roese 
---

Changes in v2: None

 common/board_r.c | 16 +---
 include/asm-generic/u-boot.h | 17 -
 2 files changed, 1 insertion(+), 32 deletions(-)

diff --git a/common/board_r.c b/common/board_r.c
index d9015cd057..4876afba6b 100644
--- a/common/board_r.c
+++ b/common/board_r.c
@@ -525,21 +525,7 @@ static int initr_ethaddr(void)
 
/* kept around for legacy kernels only ... ignore the next section */
eth_env_get_enetaddr("ethaddr", bd->bi_enetaddr);
-#ifdef CONFIG_HAS_ETH1
-   eth_env_get_enetaddr("eth1addr", bd->bi_enet1addr);
-#endif
-#ifdef CONFIG_HAS_ETH2
-   eth_env_get_enetaddr("eth2addr", bd->bi_enet2addr);
-#endif
-#ifdef CONFIG_HAS_ETH3
-   eth_env_get_enetaddr("eth3addr", bd->bi_enet3addr);
-#endif
-#ifdef CONFIG_HAS_ETH4
-   eth_env_get_enetaddr("eth4addr", bd->bi_enet4addr);
-#endif
-#ifdef CONFIG_HAS_ETH5
-   eth_env_get_enetaddr("eth5addr", bd->bi_enet5addr);
-#endif
+
return 0;
 }
 #endif /* CONFIG_CMD_NET */
diff --git a/include/asm-generic/u-boot.h b/include/asm-generic/u-boot.h
index be0014c3b3..0ec4522653 100644
--- a/include/asm-generic/u-boot.h
+++ b/include/asm-generic/u-boot.h
@@ -67,23 +67,6 @@ typedef struct bd_info {
unsigned long bi_vcofreq;   /* vco Freq in MHz */
unsigned long bi_flbfreq;   /* Flexbus Freq in MHz */
 #endif
-
-#ifdef CONFIG_HAS_ETH1
-   unsigned char   bi_enet1addr[6];/* OLD: see README.enetaddr */
-#endif
-#ifdef CONFIG_HAS_ETH2
-   unsigned char   bi_enet2addr[6];/* OLD: see README.enetaddr */
-#endif
-#ifdef CONFIG_HAS_ETH3
-   unsigned char   bi_enet3addr[6];/* OLD: see README.enetaddr */
-#endif
-#ifdef CONFIG_HAS_ETH4
-   unsigned char   bi_enet4addr[6];/* OLD: see README.enetaddr */
-#endif
-#ifdef CONFIG_HAS_ETH5
-   unsigned char   bi_enet5addr[6];/* OLD: see README.enetaddr */
-#endif
-
ulong   bi_arch_number; /* unique id for this board */
ulong   bi_boot_params; /* where this board expects params */
 #ifdef CONFIG_NR_DRAM_BANKS
-- 
2.26.2.645.ge9eca65c58-goog



[PATCH v2 25/39] bdinfo: Drop print_bi_boot_params()

2020-05-10 Thread Simon Glass
This function only has one line of code in it so inline it.

Signed-off-by: Simon Glass 
Reviewed-by: Bin Meng 
---

Changes in v2: None

 cmd/bdinfo.c | 8 +---
 1 file changed, 1 insertion(+), 7 deletions(-)

diff --git a/cmd/bdinfo.c b/cmd/bdinfo.c
index 5ffd86e508..b65eec332d 100644
--- a/cmd/bdinfo.c
+++ b/cmd/bdinfo.c
@@ -72,12 +72,6 @@ static void print_mhz(const char *name, unsigned long hz)
printf("%-12s= %6s MHz\n", name, strmhz(buf, hz));
 }
 
-
-static void print_bi_boot_params(const bd_t *bd)
-{
-   print_num("boot_params",(ulong)bd->bi_boot_params);
-}
-
 static void print_bi_dram(const bd_t *bd)
 {
 #ifdef CONFIG_NR_DRAM_BANKS
@@ -137,7 +131,7 @@ int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char 
*const argv[])
 #endif
if (IS_ENABLED(CONFIG_ARM))
print_num("arch_number", bd->bi_arch_number);
-   print_bi_boot_params(bd);
+   print_num("boot_params", (ulong)bd->bi_boot_params);
print_bi_dram(bd);
print_num("memstart", (ulong)bd->bi_memstart);
print_lnum("memsize", (u64)bd->bi_memsize);
-- 
2.26.2.645.ge9eca65c58-goog



[PATCH v2 26/39] bdinfo: Drop print_bi_flash()

2020-05-10 Thread Simon Glass
This function only has three lines of code in it so inline it. This helps
to put all the code in one place for future partitioning.

Signed-off-by: Simon Glass 
---

Changes in v2:
- Update commit message with an extra motivation

 cmd/bdinfo.c | 11 +++
 1 file changed, 3 insertions(+), 8 deletions(-)

diff --git a/cmd/bdinfo.c b/cmd/bdinfo.c
index b65eec332d..f22dc7a129 100644
--- a/cmd/bdinfo.c
+++ b/cmd/bdinfo.c
@@ -87,13 +87,6 @@ static void print_bi_dram(const bd_t *bd)
 #endif
 }
 
-static void print_bi_flash(const bd_t *bd)
-{
-   print_num("flashstart", (ulong)bd->bi_flashstart);
-   print_num("flashsize", (ulong)bd->bi_flashsize);
-   print_num("flashoffset", (ulong)bd->bi_flashoffset);
-}
-
 static void print_eth_ip_addr(void)
 {
 #if defined(CONFIG_CMD_NET)
@@ -135,7 +128,9 @@ int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char 
*const argv[])
print_bi_dram(bd);
print_num("memstart", (ulong)bd->bi_memstart);
print_lnum("memsize", (u64)bd->bi_memsize);
-   print_bi_flash(bd);
+   print_num("flashstart", (ulong)bd->bi_flashstart);
+   print_num("flashsize", (ulong)bd->bi_flashsize);
+   print_num("flashoffset", (ulong)bd->bi_flashoffset);
print_eth_ip_addr();
printf("baudrate= %u bps\n", gd->baudrate);
print_num("relocaddr", gd->relocaddr);
-- 
2.26.2.645.ge9eca65c58-goog



[PATCH v2 27/39] bdinfo: Drop print_cpu_word_size()

2020-05-10 Thread Simon Glass
This function only has one line of code in it so inline it.

Signed-off-by: Simon Glass 
Reviewed-by: Bin Meng 
---

Changes in v2: None

 cmd/bdinfo.c | 7 +--
 1 file changed, 1 insertion(+), 6 deletions(-)

diff --git a/cmd/bdinfo.c b/cmd/bdinfo.c
index f22dc7a129..d0afef5b5e 100644
--- a/cmd/bdinfo.c
+++ b/cmd/bdinfo.c
@@ -15,11 +15,6 @@
 
 DECLARE_GLOBAL_DATA_PTR;
 
-void print_cpu_word_size(void)
-{
-   printf("%-12s= %u-bit\n", "Build", (uint)sizeof(void *) * 8);
-}
-
 static void print_num(const char *name, ulong value)
 {
printf("%-12s= 0x%0*lx\n", name, 2 * (int)sizeof(value), value);
@@ -135,7 +130,7 @@ int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char 
*const argv[])
printf("baudrate= %u bps\n", gd->baudrate);
print_num("relocaddr", gd->relocaddr);
print_num("reloc off", gd->reloc_off);
-   print_cpu_word_size();
+   printf("%-12s= %u-bit\n", "Build", (uint)sizeof(void *) * 8);
 #if defined(CONFIG_CMD_NET) && !defined(CONFIG_DM_ETH)
print_eths();
 #endif
-- 
2.26.2.645.ge9eca65c58-goog



[PATCH v2 23/39] bdinfo: ppc: Drop arch-specific print_baudrate()

2020-05-10 Thread Simon Glass
This function outputs the same basic info. Since the baud rate is commonly
115200 these is often no difference. Drop the arch-specific code and
inline it to avoid a one-line function.

Signed-off-by: Simon Glass 
Reviewed-by: Bin Meng 
Reviewed-by: Stefan Roese 
---

Changes in v2: None

 cmd/bdinfo.c | 11 +--
 1 file changed, 1 insertion(+), 10 deletions(-)

diff --git a/cmd/bdinfo.c b/cmd/bdinfo.c
index e8e20f4155..62eea010c5 100644
--- a/cmd/bdinfo.c
+++ b/cmd/bdinfo.c
@@ -137,15 +137,6 @@ static void print_eth_ip_addr(void)
 #endif
 }
 
-static void print_baudrate(void)
-{
-#if defined(CONFIG_PPC)
-   printf("baudrate= %6u bps\n", gd->baudrate);
-#else
-   printf("baudrate= %u bps\n", gd->baudrate);
-#endif
-}
-
 void __weak board_detail(void)
 {
/* Please define board_detail() for your PPC platform */
@@ -165,7 +156,7 @@ int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char 
*const argv[])
print_bi_mem(bd);
print_bi_flash(bd);
print_eth_ip_addr();
-   print_baudrate();
+   printf("baudrate= %u bps\n", gd->baudrate);
print_num("relocaddr", gd->relocaddr);
print_num("reloc off", gd->reloc_off);
print_cpu_word_size();
-- 
2.26.2.645.ge9eca65c58-goog



[PATCH v2 19/39] bdinfo: Drop unused __maybe_unused

2020-05-10 Thread Simon Glass
Some of these are not needed now. Drop them to avoid cluttering the code.

Signed-off-by: Simon Glass 
Reviewed-by: Bin Meng 
---

Changes in v2:
- Drop __maybe_unused on print_lnum() too

 cmd/bdinfo.c | 7 ++-
 1 file changed, 2 insertions(+), 5 deletions(-)

diff --git a/cmd/bdinfo.c b/cmd/bdinfo.c
index 13b4f2ebba..d41547ec6e 100644
--- a/cmd/bdinfo.c
+++ b/cmd/bdinfo.c
@@ -15,12 +15,11 @@
 
 DECLARE_GLOBAL_DATA_PTR;
 
-__maybe_unused void print_cpu_word_size(void)
+void print_cpu_word_size(void)
 {
printf("%-12s= %u-bit\n", "Build", (uint)sizeof(void *) * 8);
 }
 
-__maybe_unused
 static void print_num(const char *name, ulong value)
 {
printf("%-12s= 0x%0*lx\n", name, 2 * (int)sizeof(value), value);
@@ -61,13 +60,11 @@ static void print_eths(void)
 }
 #endif
 
-__maybe_unused
 static void print_lnum(const char *name, unsigned long long value)
 {
printf("%-12s= 0x%.8llX\n", name, value);
 }
 
-__maybe_unused
 static void print_mhz(const char *name, unsigned long hz)
 {
char buf[32];
@@ -160,7 +157,7 @@ static inline void print_baudrate(void)
 #endif
 }
 
-static inline void __maybe_unused print_std_bdinfo(const bd_t *bd)
+static inline void print_std_bdinfo(const bd_t *bd)
 {
print_bi_boot_params(bd);
print_bi_mem(bd);
-- 
2.26.2.645.ge9eca65c58-goog



[PATCH v2 20/39] bdinfo: microblaze: sh: nios2: Drop arch-specific flash info

2020-05-10 Thread Simon Glass
The differences these architectures have are minor and not worth the extra
code. Use the generic version always.

Tidy up the code style by removing unnecessary tabs.

Signed-off-by: Simon Glass 
Reviewed-by: Bin Meng 
---

Changes in v2: None

 cmd/bdinfo.c | 17 +++--
 1 file changed, 3 insertions(+), 14 deletions(-)

diff --git a/cmd/bdinfo.c b/cmd/bdinfo.c
index d41547ec6e..25397c2e95 100644
--- a/cmd/bdinfo.c
+++ b/cmd/bdinfo.c
@@ -109,20 +109,9 @@ static inline void print_bi_dram(const bd_t *bd)
 
 static inline void print_bi_flash(const bd_t *bd)
 {
-#if defined(CONFIG_MICROBLAZE) || defined(CONFIG_SH)
-   print_num("flash start",(ulong)bd->bi_flashstart);
-   print_num("flash size ",(ulong)bd->bi_flashsize);
-   print_num("flash offset   ",(ulong)bd->bi_flashoffset);
-
-#elif defined(CONFIG_NIOS2)
-   print_num("flash start",(ulong)bd->bi_flashstart);
-   print_num("flash size", (ulong)bd->bi_flashsize);
-   print_num("flash offset",   (ulong)bd->bi_flashoffset);
-#else
-   print_num("flashstart", (ulong)bd->bi_flashstart);
-   print_num("flashsize",  (ulong)bd->bi_flashsize);
-   print_num("flashoffset",(ulong)bd->bi_flashoffset);
-#endif
+   print_num("flashstart", (ulong)bd->bi_flashstart);
+   print_num("flashsize", (ulong)bd->bi_flashsize);
+   print_num("flashoffset", (ulong)bd->bi_flashoffset);
 }
 
 static inline void print_eth_ip_addr(void)
-- 
2.26.2.645.ge9eca65c58-goog



[PATCH v2 24/39] bdinfo: sh: arc: Drop arch-specific print_bi_mem()

2020-05-10 Thread Simon Glass
It isn't worth having arch-specific code for such minor output
differences. In fact it is better if all archs are consistent.

Drop the arch-specific code in print_bi_mem() and inline it to avoid a
two-line function.

Signed-off-by: Simon Glass 
Reviewed-by: Bin Meng 
Acked-by: Alexey Brodkin 
---

Changes in v2: None

 cmd/bdinfo.c | 17 ++---
 1 file changed, 2 insertions(+), 15 deletions(-)

diff --git a/cmd/bdinfo.c b/cmd/bdinfo.c
index 62eea010c5..5ffd86e508 100644
--- a/cmd/bdinfo.c
+++ b/cmd/bdinfo.c
@@ -78,20 +78,6 @@ static void print_bi_boot_params(const bd_t *bd)
print_num("boot_params",(ulong)bd->bi_boot_params);
 }
 
-static void print_bi_mem(const bd_t *bd)
-{
-#if defined(CONFIG_SH)
-   print_num("mem start  ",(ulong)bd->bi_memstart);
-   print_lnum("mem size   ",   (u64)bd->bi_memsize);
-#elif defined(CONFIG_ARC)
-   print_num("mem start",  (ulong)bd->bi_memstart);
-   print_lnum("mem size",  (u64)bd->bi_memsize);
-#else
-   print_num("memstart",   (ulong)bd->bi_memstart);
-   print_lnum("memsize",   (u64)bd->bi_memsize);
-#endif
-}
-
 static void print_bi_dram(const bd_t *bd)
 {
 #ifdef CONFIG_NR_DRAM_BANKS
@@ -153,7 +139,8 @@ int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char 
*const argv[])
print_num("arch_number", bd->bi_arch_number);
print_bi_boot_params(bd);
print_bi_dram(bd);
-   print_bi_mem(bd);
+   print_num("memstart", (ulong)bd->bi_memstart);
+   print_lnum("memsize", (u64)bd->bi_memsize);
print_bi_flash(bd);
print_eth_ip_addr();
printf("baudrate= %u bps\n", gd->baudrate);
-- 
2.26.2.645.ge9eca65c58-goog



[PATCH v2 18/39] bdinfo: Drop the option to not use the generic 'bd' command

2020-05-10 Thread Simon Glass
Now that all architectures are using the generic do_bdinfo(), drop the
option to not use it. When new architectures are added, they will get at
least some useful information from the generic implementation.

Signed-off-by: Simon Glass 
Reviewed-by: Bin Meng 
---

Changes in v2: None

 cmd/bdinfo.c | 59 
 1 file changed, 59 deletions(-)

diff --git a/cmd/bdinfo.c b/cmd/bdinfo.c
index f07f92463c..13b4f2ebba 100644
--- a/cmd/bdinfo.c
+++ b/cmd/bdinfo.c
@@ -169,64 +169,6 @@ static inline void __maybe_unused print_std_bdinfo(const 
bd_t *bd)
print_baudrate();
 }
 
-#if defined(CONFIG_PPC)
-
-#define USE_GENERIC
-
-#elif defined(CONFIG_NIOS2)
-
-#define USE_GENERIC
-
-#elif defined(CONFIG_MICROBLAZE)
-
-#define USE_GENERIC
-
-#elif defined(CONFIG_M68K)
-
-#define USE_GENERIC
-
-#elif defined(CONFIG_MIPS)
-
-#define USE_GENERIC
-
-#elif defined(CONFIG_ARM)
-
-#define USE_GENERIC
-
-#elif defined(CONFIG_SH)
-
-#define USE_GENERIC
-
-#elif defined(CONFIG_X86)
-
-#define USE_GENERIC
-
-#elif defined(CONFIG_SANDBOX)
-
-#define USE_GENERIC
-
-#elif defined(CONFIG_NDS32)
-
-#define USE_GENERIC
-
-#elif defined(CONFIG_RISCV)
-
-#define USE_GENERIC
-
-#elif defined(CONFIG_ARC)
-
-#define USE_GENERIC
-
-#elif defined(CONFIG_XTENSA)
-
-#define USE_GENERIC
-
-#else
- #error "a case for this architecture does not exist!"
-#endif
-
-/* Temporary check for archs that use generic bdinfo. Eventually all will */
-#ifdef USE_GENERIC
 void __weak board_detail(void)
 {
/* Please define board_detail() for your PPC platform */
@@ -341,7 +283,6 @@ int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char 
*const argv[])
 
return 0;
 }
-#endif
 
 /*  */
 
-- 
2.26.2.645.ge9eca65c58-goog



[PATCH v2 21/39] bdinfo: Drop unnecessary inline on functions

2020-05-10 Thread Simon Glass
This serves no purpose since the compiler will inline the functions
automatically. Drop use of inline in this file.

Signed-off-by: Simon Glass 
Reviewed-by: Bin Meng 
---

Changes in v2:
- Move __maybe_unused on print_lnum() to the early patch

 cmd/bdinfo.c | 14 +++---
 1 file changed, 7 insertions(+), 7 deletions(-)

diff --git a/cmd/bdinfo.c b/cmd/bdinfo.c
index 25397c2e95..6e5b3d997d 100644
--- a/cmd/bdinfo.c
+++ b/cmd/bdinfo.c
@@ -73,12 +73,12 @@ static void print_mhz(const char *name, unsigned long hz)
 }
 
 
-static inline void print_bi_boot_params(const bd_t *bd)
+static void print_bi_boot_params(const bd_t *bd)
 {
print_num("boot_params",(ulong)bd->bi_boot_params);
 }
 
-static inline void print_bi_mem(const bd_t *bd)
+static void print_bi_mem(const bd_t *bd)
 {
 #if defined(CONFIG_SH)
print_num("mem start  ",(ulong)bd->bi_memstart);
@@ -92,7 +92,7 @@ static inline void print_bi_mem(const bd_t *bd)
 #endif
 }
 
-static inline void print_bi_dram(const bd_t *bd)
+static void print_bi_dram(const bd_t *bd)
 {
 #ifdef CONFIG_NR_DRAM_BANKS
int i;
@@ -107,14 +107,14 @@ static inline void print_bi_dram(const bd_t *bd)
 #endif
 }
 
-static inline void print_bi_flash(const bd_t *bd)
+static void print_bi_flash(const bd_t *bd)
 {
print_num("flashstart", (ulong)bd->bi_flashstart);
print_num("flashsize", (ulong)bd->bi_flashsize);
print_num("flashoffset", (ulong)bd->bi_flashoffset);
 }
 
-static inline void print_eth_ip_addr(void)
+static void print_eth_ip_addr(void)
 {
 #if defined(CONFIG_CMD_NET)
print_eth(0);
@@ -137,7 +137,7 @@ static inline void print_eth_ip_addr(void)
 #endif
 }
 
-static inline void print_baudrate(void)
+static void print_baudrate(void)
 {
 #if defined(CONFIG_PPC)
printf("baudrate= %6u bps\n", gd->baudrate);
@@ -146,7 +146,7 @@ static inline void print_baudrate(void)
 #endif
 }
 
-static inline void print_std_bdinfo(const bd_t *bd)
+static void print_std_bdinfo(const bd_t *bd)
 {
print_bi_boot_params(bd);
print_bi_mem(bd);
-- 
2.26.2.645.ge9eca65c58-goog



[PATCH v2 22/39] bdinfo: Drop print_std_bdinfo()

2020-05-10 Thread Simon Glass
Everything in this function is standard now so it serves no purpose. Move
it into the generic do_bdinfo() function.

Signed-off-by: Simon Glass 
Reviewed-by: Bin Meng 
---

Changes in v2: None

 cmd/bdinfo.c | 15 +--
 1 file changed, 5 insertions(+), 10 deletions(-)

diff --git a/cmd/bdinfo.c b/cmd/bdinfo.c
index 6e5b3d997d..e8e20f4155 100644
--- a/cmd/bdinfo.c
+++ b/cmd/bdinfo.c
@@ -146,15 +146,6 @@ static void print_baudrate(void)
 #endif
 }
 
-static void print_std_bdinfo(const bd_t *bd)
-{
-   print_bi_boot_params(bd);
-   print_bi_mem(bd);
-   print_bi_flash(bd);
-   print_eth_ip_addr();
-   print_baudrate();
-}
-
 void __weak board_detail(void)
 {
/* Please define board_detail() for your PPC platform */
@@ -169,8 +160,12 @@ int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char 
*const argv[])
 #endif
if (IS_ENABLED(CONFIG_ARM))
print_num("arch_number", bd->bi_arch_number);
+   print_bi_boot_params(bd);
print_bi_dram(bd);
-   print_std_bdinfo(bd);
+   print_bi_mem(bd);
+   print_bi_flash(bd);
+   print_eth_ip_addr();
+   print_baudrate();
print_num("relocaddr", gd->relocaddr);
print_num("reloc off", gd->reloc_off);
print_cpu_word_size();
-- 
2.26.2.645.ge9eca65c58-goog



[PATCH v2 17/39] bdinfo: arc: Use the generic bd command

2020-05-10 Thread Simon Glass
There is nothing new in the arc 'bd' command beyond what is already there.
Switch it over to use the generic code.

Signed-off-by: Simon Glass 
---

Changes in v2:
- Fix up inadvertent merge of PowerPC/ARM/m68k/arc patches

 cmd/bdinfo.c | 12 +---
 1 file changed, 1 insertion(+), 11 deletions(-)

diff --git a/cmd/bdinfo.c b/cmd/bdinfo.c
index 9cf3da4725..f07f92463c 100644
--- a/cmd/bdinfo.c
+++ b/cmd/bdinfo.c
@@ -215,17 +215,7 @@ static inline void __maybe_unused print_std_bdinfo(const 
bd_t *bd)
 
 #elif defined(CONFIG_ARC)
 
-int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
-{
-   bd_t *bd = gd->bd;
-
-   print_bi_mem(bd);
-   print_eth_ip_addr();
-   print_baudrate();
-   print_cpu_word_size();
-
-   return 0;
-}
+#define USE_GENERIC
 
 #elif defined(CONFIG_XTENSA)
 
-- 
2.26.2.645.ge9eca65c58-goog



[PATCH v2 15/39] bdinfo: m68k: Use the generic bd command

2020-05-10 Thread Simon Glass
Unfortunately m68k has a lot of special stuff. Move it into the generic
function for now, so we can have it all in one place.

Signed-off-by: Simon Glass 
---

Changes in v2:
- Fix up inadvertent merge of PowerPC/ARM/m68k/arc patches

 cmd/bdinfo.c | 49 +++--
 1 file changed, 19 insertions(+), 30 deletions(-)

diff --git a/cmd/bdinfo.c b/cmd/bdinfo.c
index 05e4a05b77..646d8807bd 100644
--- a/cmd/bdinfo.c
+++ b/cmd/bdinfo.c
@@ -183,35 +183,7 @@ static inline void __maybe_unused print_std_bdinfo(const 
bd_t *bd)
 
 #elif defined(CONFIG_M68K)
 
-int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
-{
-   bd_t *bd = gd->bd;
-
-   print_bi_mem(bd);
-   print_bi_flash(bd);
-#if defined(CONFIG_SYS_INIT_RAM_ADDR)
-   print_num("sramstart",  (ulong)bd->bi_sramstart);
-   print_num("sramsize",   (ulong)bd->bi_sramsize);
-#endif
-#if defined(CONFIG_SYS_MBAR)
-   print_num("mbar",   bd->bi_mbar_base);
-#endif
-   print_mhz("cpufreq",bd->bi_intfreq);
-   print_mhz("busfreq",bd->bi_busfreq);
-#ifdef CONFIG_PCI
-   print_mhz("pcifreq",bd->bi_pcifreq);
-#endif
-#ifdef CONFIG_EXTRA_CLOCK
-   print_mhz("flbfreq",bd->bi_flbfreq);
-   print_mhz("inpfreq",bd->bi_inpfreq);
-   print_mhz("vcofreq",bd->bi_vcofreq);
-#endif
-   print_eth_ip_addr();
-   print_baudrate();
-   print_cpu_word_size();
-
-   return 0;
-}
+#define USE_GENERIC
 
 #elif defined(CONFIG_MIPS)
 
@@ -359,7 +331,6 @@ int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char 
*const argv[])
else
puts("addressing  = 32-bit\n");
 #endif
-   print_mhz("busfreq", bd->bi_busfreq);
board_detail();
}
 #if defined(CONFIG_CPM2)
@@ -369,9 +340,27 @@ int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char 
*const argv[])
print_mhz("brgfreq", bd->bi_brgfreq);
 #endif
 
+   /* This is used by m68k and ppc */
 #if defined(CONFIG_SYS_INIT_RAM_ADDR)
print_num("sramstart", (ulong)bd->bi_sramstart);
print_num("sramsize", (ulong)bd->bi_sramsize);
+#endif
+   if (IS_ENABLED(CONFIG_PPC) || IS_ENABLED(CONFIG_M68K))
+   print_mhz("busfreq", bd->bi_busfreq);
+
+   /* The rest are used only by m68k */
+#ifdef CONFIG_M68K
+#if defined(CONFIG_SYS_MBAR)
+   print_num("mbar", bd->bi_mbar_base);
+#endif
+   print_mhz("cpufreq", bd->bi_intfreq);
+   if (IS_ENABLED(CONFIG_PCI))
+   print_mhz("pcifreq", bd->bi_pcifreq);
+#ifdef CONFIG_EXTRA_CLOCK
+   print_mhz("flbfreq", bd->bi_flbfreq);
+   print_mhz("inpfreq", bd->bi_inpfreq);
+   print_mhz("vcofreq", bd->bi_vcofreq);
+#endif
 #endif
 
return 0;
-- 
2.26.2.645.ge9eca65c58-goog



[PATCH v2 14/39] bdinfo: powerpc: Use the generic bd command

2020-05-10 Thread Simon Glass
Unfortunately PowerPC has a lot of special stuff. Move it into the generic
function for now, so we can have it all in one place.

Signed-off-by: Simon Glass 
---

Changes in v2:
- Fix up inadvertent merge of PowerPC/ARM/m68k/arc patches

 cmd/bdinfo.c | 89 
 1 file changed, 41 insertions(+), 48 deletions(-)

diff --git a/cmd/bdinfo.c b/cmd/bdinfo.c
index 0e96dd619c..05e4a05b77 100644
--- a/cmd/bdinfo.c
+++ b/cmd/bdinfo.c
@@ -170,53 +170,8 @@ static inline void __maybe_unused print_std_bdinfo(const 
bd_t *bd)
 }
 
 #if defined(CONFIG_PPC)
-void __weak board_detail(void)
-{
-   /* Please define board_detail() for your platform */
-}
-
-int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
-{
-   bd_t *bd = gd->bd;
-
-#ifdef DEBUG
-   print_num("bd address", (ulong)bd);
-#endif
-   print_bi_mem(bd);
-   print_bi_flash(bd);
-   print_num("sramstart",  bd->bi_sramstart);
-   print_num("sramsize",   bd->bi_sramsize);
-#ifdefined(CONFIG_MPC8xx) || defined(CONFIG_E500)
-   print_num("immr_base",  bd->bi_immr_base);
-#endif
-   print_num("bootflags",  bd->bi_bootflags);
-#if defined(CONFIG_CPM2)
-   print_mhz("vco",bd->bi_vco);
-   print_mhz("sccfreq",bd->bi_sccfreq);
-   print_mhz("brgfreq",bd->bi_brgfreq);
-#endif
-   print_mhz("intfreq",bd->bi_intfreq);
-#if defined(CONFIG_CPM2)
-   print_mhz("cpmfreq",bd->bi_cpmfreq);
-#endif
-   print_mhz("busfreq",bd->bi_busfreq);
-
-#ifdef CONFIG_ENABLE_36BIT_PHYS
-#ifdef CONFIG_PHYS_64BIT
-   puts("addressing  = 36-bit\n");
-#else
-   puts("addressing  = 32-bit\n");
-#endif
-#endif
-
-   print_eth_ip_addr();
-   print_baudrate();
-   print_num("relocaddr", gd->relocaddr);
-   board_detail();
-   print_cpu_word_size();
 
-   return 0;
-}
+#define USE_GENERIC
 
 #elif defined(CONFIG_NIOS2)
 
@@ -367,10 +322,20 @@ int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char 
* const argv[])
 
 /* Temporary check for archs that use generic bdinfo. Eventually all will */
 #ifdef USE_GENERIC
+void __weak board_detail(void)
+{
+   /* Please define board_detail() for your PPC platform */
+}
+
 int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
 {
-   print_bi_dram(gd->bd);
-   print_std_bdinfo(gd->bd);
+   bd_t *bd = gd->bd;
+
+#ifdef DEBUG
+   print_num("bd address", (ulong)bd);
+#endif
+   print_bi_dram(bd);
+   print_std_bdinfo(bd);
print_num("relocaddr", gd->relocaddr);
print_num("reloc off", gd->reloc_off);
print_cpu_word_size();
@@ -381,6 +346,34 @@ int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char 
*const argv[])
print_num("new_fdt", (ulong)gd->new_fdt);
print_num("fdt_size", (ulong)gd->fdt_size);
 
+   /* This section is used only by ppc */
+#if defined(CONFIG_MPC8xx) || defined(CONFIG_E500)
+   print_num("immr_base", bd->bi_immr_base);
+#endif
+   if (IS_ENABLED(CONFIG_PPC)) {
+   print_num("bootflags", bd->bi_bootflags);
+   print_mhz("intfreq", bd->bi_intfreq);
+#ifdef CONFIG_ENABLE_36BIT_PHYS
+   if (IS_ENABLED(CONFIG_PHYS_64BIT))
+   puts("addressing  = 36-bit\n");
+   else
+   puts("addressing  = 32-bit\n");
+#endif
+   print_mhz("busfreq", bd->bi_busfreq);
+   board_detail();
+   }
+#if defined(CONFIG_CPM2)
+   print_mhz("cpmfreq", bd->bi_cpmfreq);
+   print_mhz("vco", bd->bi_vco);
+   print_mhz("sccfreq", bd->bi_sccfreq);
+   print_mhz("brgfreq", bd->bi_brgfreq);
+#endif
+
+#if defined(CONFIG_SYS_INIT_RAM_ADDR)
+   print_num("sramstart", (ulong)bd->bi_sramstart);
+   print_num("sramsize", (ulong)bd->bi_sramsize);
+#endif
+
return 0;
 }
 #endif
-- 
2.26.2.645.ge9eca65c58-goog



[PATCH v2 16/39] bdinfo: arm: Use the generic bd command

2020-05-10 Thread Simon Glass
Unfortunately ARM has a lot of special stuff. Move it into the generic
function for now, so we can have it all in one place.

Signed-off-by: Simon Glass 
---

Changes in v2:
- Fix up inadvertent merge of PowerPC/ARM/m68k/arc patches

 cmd/bdinfo.c | 102 ++-
 1 file changed, 44 insertions(+), 58 deletions(-)

diff --git a/cmd/bdinfo.c b/cmd/bdinfo.c
index 646d8807bd..9cf3da4725 100644
--- a/cmd/bdinfo.c
+++ b/cmd/bdinfo.c
@@ -191,64 +191,7 @@ static inline void __maybe_unused print_std_bdinfo(const 
bd_t *bd)
 
 #elif defined(CONFIG_ARM)
 
-static int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc,
-   char * const argv[])
-{
-   bd_t *bd = gd->bd;
-
-   print_num("arch_number",bd->bi_arch_number);
-   print_bi_boot_params(bd);
-   print_bi_dram(bd);
-
-#ifdef CONFIG_SYS_MEM_RESERVE_SECURE
-   if (gd->arch.secure_ram & MEM_RESERVE_SECURE_SECURED) {
-   print_num("Secure ram",
- gd->arch.secure_ram & MEM_RESERVE_SECURE_ADDR_MASK);
-   }
-#endif
-#ifdef CONFIG_RESV_RAM
-   if (gd->arch.resv_ram)
-   print_num("Reserved ram", gd->arch.resv_ram);
-#endif
-#if defined(CONFIG_CMD_NET) && !defined(CONFIG_DM_ETH)
-   print_eths();
-#endif
-   print_baudrate();
-#if !(CONFIG_IS_ENABLED(SYS_ICACHE_OFF) && CONFIG_IS_ENABLED(SYS_DCACHE_OFF))
-   print_num("TLB addr", gd->arch.tlb_addr);
-#endif
-   print_num("relocaddr", gd->relocaddr);
-   print_num("reloc off", gd->reloc_off);
-   print_num("irq_sp", gd->irq_sp);/* irq stack pointer */
-   print_num("sp start ", gd->start_addr_sp);
-#if defined(CONFIG_LCD) || defined(CONFIG_VIDEO) || defined(CONFIG_DM_VIDEO)
-   print_num("FB base  ", gd->fb_base);
-#endif
-   /*
-* TODO: Currently only support for davinci SOC's is added.
-* Remove this check once all the board implement this.
-*/
-#ifdef CONFIG_CLOCKS
-   printf("ARM frequency = %ld MHz\n", gd->bd->bi_arm_freq);
-   printf("DSP frequency = %ld MHz\n", gd->bd->bi_dsp_freq);
-   printf("DDR frequency = %ld MHz\n", gd->bd->bi_ddr_freq);
-#endif
-#ifdef CONFIG_BOARD_TYPES
-   printf("Board Type  = %ld\n", gd->board_type);
-#endif
-#if CONFIG_VAL(SYS_MALLOC_F_LEN)
-   printf("Early malloc usage: %lx / %x\n", gd->malloc_ptr,
-  CONFIG_VAL(SYS_MALLOC_F_LEN));
-#endif
-#if CONFIG_IS_ENABLED(MULTI_DTB_FIT)
-   print_num("multi_dtb_fit", (ulong)gd->multi_dtb_fit);
-#endif
-   if (gd->fdt_blob)
-   print_num("fdt_blob", (ulong)gd->fdt_blob);
-   print_cpu_word_size();
-
-   return 0;
-}
+#define USE_GENERIC
 
 #elif defined(CONFIG_SH)
 
@@ -306,6 +249,8 @@ int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char 
*const argv[])
 #ifdef DEBUG
print_num("bd address", (ulong)bd);
 #endif
+   if (IS_ENABLED(CONFIG_ARM))
+   print_num("arch_number", bd->bi_arch_number);
print_bi_dram(bd);
print_std_bdinfo(bd);
print_num("relocaddr", gd->relocaddr);
@@ -317,6 +262,47 @@ int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char 
*const argv[])
print_num("fdt_blob", (ulong)gd->fdt_blob);
print_num("new_fdt", (ulong)gd->new_fdt);
print_num("fdt_size", (ulong)gd->fdt_size);
+#if defined(CONFIG_LCD) || defined(CONFIG_VIDEO) || defined(CONFIG_DM_VIDEO)
+   print_num("FB base  ", gd->fb_base);
+#endif
+
+   /* This section is used only by ARM */
+#ifdef CONFIG_ARM
+#ifdef CONFIG_SYS_MEM_RESERVE_SECURE
+   if (gd->arch.secure_ram & MEM_RESERVE_SECURE_SECURED) {
+   print_num("Secure ram",
+ gd->arch.secure_ram & MEM_RESERVE_SECURE_ADDR_MASK);
+   }
+#endif
+#ifdef CONFIG_RESV_RAM
+   if (gd->arch.resv_ram)
+   print_num("Reserved ram", gd->arch.resv_ram);
+#endif
+#if !(CONFIG_IS_ENABLED(SYS_ICACHE_OFF) && CONFIG_IS_ENABLED(SYS_DCACHE_OFF))
+   print_num("TLB addr", gd->arch.tlb_addr);
+#endif
+   print_num("irq_sp", gd->irq_sp);/* irq stack pointer */
+   print_num("sp start ", gd->start_addr_sp);
+   /*
+* TODO: Currently only support for davinci SOC's is added.
+* Remove this check once all the board implement this.
+*/
+#ifdef CONFIG_CLOCKS
+   printf("ARM frequency = %ld MHz\n", gd->bd->bi_arm_freq);
+   printf("DSP frequency = %ld MHz\n", gd->bd->bi_dsp_freq);
+   printf("DDR frequency = %ld MHz\n", gd->bd->bi_ddr_freq);
+#endif
+#ifdef CONFIG_BOARD_TYPES
+   printf("Board Type  = %ld\n", gd->board_type);
+#endif
+#if CONFIG_VAL(SYS_MALLOC_F_LEN)
+   printf("Early malloc usage: %lx / %x\n", gd->malloc_ptr,
+  CONFIG_VAL(SYS_MALLOC_F_LEN));
+#endif
+#if CONFIG_IS_ENABLED(MULTI_DTB_FIT)
+   print_num("multi_dtb_fit", (ulong)gd->multi_dtb_fit);
+#endif
+#endif /* CONFIG_ARM */
 
/* This section is used only by ppc */
 #if 

[PATCH v2 12/39] bdinfo: nds32: Use the generic bd command

2020-05-10 Thread Simon Glass
This arch has none of its own info to show. Move it over to use the
generic do_bdinfo().

Signed-off-by: Simon Glass 
Reviewed-by: Rick Chen 
Reviewed-by: Bin Meng 
---

Changes in v2: None

 cmd/bdinfo.c | 14 +-
 1 file changed, 1 insertion(+), 13 deletions(-)

diff --git a/cmd/bdinfo.c b/cmd/bdinfo.c
index dce24b43fa..6a2bbfb348 100644
--- a/cmd/bdinfo.c
+++ b/cmd/bdinfo.c
@@ -337,19 +337,7 @@ static int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc,
 
 #elif defined(CONFIG_NDS32)
 
-int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
-{
-   bd_t *bd = gd->bd;
-
-   print_num("arch_number",bd->bi_arch_number);
-   print_bi_boot_params(bd);
-   print_bi_dram(bd);
-   print_eth_ip_addr();
-   print_baudrate();
-   print_cpu_word_size();
-
-   return 0;
-}
+#define USE_GENERIC
 
 #elif defined(CONFIG_RISCV)
 
-- 
2.26.2.645.ge9eca65c58-goog



[PATCH v2 09/39] bdinfo: sh: Use the generic bd command

2020-05-10 Thread Simon Glass
This arch has no code that is not already in the generic function. Drop
the arch-specific function and change sh over to use the generic one.

Signed-off-by: Simon Glass 
Reviewed-by: Bin Meng 
---

Changes in v2: None

 cmd/bdinfo.c | 13 +
 1 file changed, 1 insertion(+), 12 deletions(-)

diff --git a/cmd/bdinfo.c b/cmd/bdinfo.c
index dc5a09f8ce..0b9ac21848 100644
--- a/cmd/bdinfo.c
+++ b/cmd/bdinfo.c
@@ -325,18 +325,7 @@ static int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc,
 
 #elif defined(CONFIG_SH)
 
-int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
-{
-   bd_t *bd = gd->bd;
-
-   print_bi_mem(bd);
-   print_bi_flash(bd);
-   print_eth_ip_addr();
-   print_baudrate();
-   print_cpu_word_size();
-
-   return 0;
-}
+#define USE_GENERIC
 
 #elif defined(CONFIG_X86)
 
-- 
2.26.2.645.ge9eca65c58-goog



[PATCH v2 13/39] bdinfo: riscv: Use the generic bd command

2020-05-10 Thread Simon Glass
This arch has none of its own info to show. Move it over to use the
generic do_bdinfo().

Signed-off-by: Simon Glass 
Reviewed-by: Rick Chen 
Reviewed-by: Bin Meng 
---

Changes in v2: None

 cmd/bdinfo.c | 15 +--
 1 file changed, 1 insertion(+), 14 deletions(-)

diff --git a/cmd/bdinfo.c b/cmd/bdinfo.c
index 6a2bbfb348..0e96dd619c 100644
--- a/cmd/bdinfo.c
+++ b/cmd/bdinfo.c
@@ -341,20 +341,7 @@ static int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc,
 
 #elif defined(CONFIG_RISCV)
 
-int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
-{
-   bd_t *bd = gd->bd;
-
-   print_bi_boot_params(bd);
-   print_bi_dram(bd);
-   print_num("relocaddr", gd->relocaddr);
-   print_num("reloc off", gd->reloc_off);
-   print_eth_ip_addr();
-   print_baudrate();
-   print_cpu_word_size();
-
-   return 0;
-}
+#define USE_GENERIC
 
 #elif defined(CONFIG_ARC)
 
-- 
2.26.2.645.ge9eca65c58-goog



[PATCH v2 10/39] bdinfo: x86: Use the generic bd command

2020-05-10 Thread Simon Glass
This arch shows 'ethspeed' info but only the freescale drivers use it, so
it can be dropped.

It also calls print_bi_dram() which is safe to call from any arch since it
has an #ifdef inside it. Add this to the generic do_bdinfo() and move x86
over to use it. Put it first since pytests rely on seeing it before
memstart in find_ram_base().

Signed-off-by: Simon Glass 
Reviewed-by: Bin Meng 
---

Changes in v2: None

 cmd/bdinfo.c | 21 ++---
 1 file changed, 2 insertions(+), 19 deletions(-)

diff --git a/cmd/bdinfo.c b/cmd/bdinfo.c
index 0b9ac21848..0b903d234e 100644
--- a/cmd/bdinfo.c
+++ b/cmd/bdinfo.c
@@ -329,25 +329,7 @@ static int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc,
 
 #elif defined(CONFIG_X86)
 
-int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
-{
-   bd_t *bd = gd->bd;
-
-   print_bi_boot_params(bd);
-
-   print_bi_dram(bd);
-
-   print_num("relocaddr", gd->relocaddr);
-   print_num("reloc off", gd->reloc_off);
-#if defined(CONFIG_CMD_NET)
-   print_eth_ip_addr();
-   print_mhz("ethspeed",   bd->bi_ethspeed);
-#endif
-   print_baudrate();
-   print_cpu_word_size();
-
-   return 0;
-}
+#define USE_GENERIC
 
 #elif defined(CONFIG_SANDBOX)
 
@@ -426,6 +408,7 @@ int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char * 
const argv[])
 #ifdef USE_GENERIC
 int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
 {
+   print_bi_dram(gd->bd);
print_std_bdinfo(gd->bd);
print_num("relocaddr", gd->relocaddr);
print_num("reloc off", gd->reloc_off);
-- 
2.26.2.645.ge9eca65c58-goog



[PATCH v2 11/39] bdinfo: sandbox: Use the generic bd command

2020-05-10 Thread Simon Glass
Sandbox has a printout of 'FB base' but this code is not used since
sandbox uses driver model for everything.

Move sandbox over to use the generic do_bdinfo().

Signed-off-by: Simon Glass 
Reviewed-by: Bin Meng 
---

Changes in v2: None

 cmd/bdinfo.c | 16 +---
 1 file changed, 1 insertion(+), 15 deletions(-)

diff --git a/cmd/bdinfo.c b/cmd/bdinfo.c
index 0b903d234e..dce24b43fa 100644
--- a/cmd/bdinfo.c
+++ b/cmd/bdinfo.c
@@ -333,21 +333,7 @@ static int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc,
 
 #elif defined(CONFIG_SANDBOX)
 
-int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
-{
-   bd_t *bd = gd->bd;
-
-   print_bi_boot_params(bd);
-   print_bi_dram(bd);
-   print_eth_ip_addr();
-
-#if defined(CONFIG_LCD) || defined(CONFIG_VIDEO)
-   print_num("FB base  ", gd->fb_base);
-#endif
-   print_cpu_word_size();
-
-   return 0;
-}
+#define USE_GENERIC
 
 #elif defined(CONFIG_NDS32)
 
-- 
2.26.2.645.ge9eca65c58-goog



[PATCH v2 06/39] bdinfo: mips: Use the generic bd command

2020-05-10 Thread Simon Glass
MIPS currently has a few extra things which are generally useful. Add them
to the generic function and move MIPS over to use it.

Signed-off-by: Simon Glass 
Reviewed-by: Bin Meng 
Reviewed-by: Daniel Schwierzeck 
---

Changes in v2: None

 cmd/bdinfo.c | 13 -
 1 file changed, 4 insertions(+), 9 deletions(-)

diff --git a/cmd/bdinfo.c b/cmd/bdinfo.c
index 9247180a29..6ccbd2f50f 100644
--- a/cmd/bdinfo.c
+++ b/cmd/bdinfo.c
@@ -299,15 +299,7 @@ int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char * 
const argv[])
 
 #elif defined(CONFIG_MIPS)
 
-int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
-{
-   print_std_bdinfo(gd->bd);
-   print_num("relocaddr", gd->relocaddr);
-   print_num("reloc off", gd->reloc_off);
-   print_cpu_word_size();
-
-   return 0;
-}
+#define USE_GENERIC
 
 #elif defined(CONFIG_ARM)
 
@@ -485,6 +477,9 @@ int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char * 
const argv[])
 int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
 {
print_std_bdinfo(gd->bd);
+   print_num("relocaddr", gd->relocaddr);
+   print_num("reloc off", gd->reloc_off);
+   print_cpu_word_size();
 
return 0;
 }
-- 
2.26.2.645.ge9eca65c58-goog



[PATCH v2 07/39] bdinfo: nios2: Use the generic bd command

2020-05-10 Thread Simon Glass
Nios2 currently has some code to output SRAM information which is behind
an #ifdef. No nios2 boards define this option, so the code can be removed.

Move Nios2 over to use the generic function.

Signed-off-by: Simon Glass 
Reviewed-by: Bin Meng 
---

Changes in v2: None

 cmd/bdinfo.c | 19 +--
 1 file changed, 1 insertion(+), 18 deletions(-)

diff --git a/cmd/bdinfo.c b/cmd/bdinfo.c
index 6ccbd2f50f..405a915861 100644
--- a/cmd/bdinfo.c
+++ b/cmd/bdinfo.c
@@ -220,24 +220,7 @@ int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char * 
const argv[])
 
 #elif defined(CONFIG_NIOS2)
 
-int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
-{
-   bd_t *bd = gd->bd;
-
-   print_bi_dram(bd);
-   print_bi_flash(bd);
-
-#if defined(CONFIG_SYS_SRAM_BASE)
-   print_num ("sram start",(ulong)bd->bi_sramstart);
-   print_num ("sram size", (ulong)bd->bi_sramsize);
-#endif
-
-   print_eth_ip_addr();
-   print_baudrate();
-   print_cpu_word_size();
-
-   return 0;
-}
+#define USE_GENERIC
 
 #elif defined(CONFIG_MICROBLAZE)
 
-- 
2.26.2.645.ge9eca65c58-goog



[PATCH v2 05/39] bdinfo: xtensa: Create a generic do_bdinfo for xtensa

2020-05-10 Thread Simon Glass
This arch uses only the generic function. It would be nice if all the
archs did the same. As a first step, create a new generic function for the
'bd' command and make xtensa use it.

Signed-off-by: Simon Glass 
Reviewed-by: Bin Meng 
---

Changes in v2: None

 cmd/bdinfo.c | 14 ++
 1 file changed, 10 insertions(+), 4 deletions(-)

diff --git a/cmd/bdinfo.c b/cmd/bdinfo.c
index 9edcf8d74e..9247180a29 100644
--- a/cmd/bdinfo.c
+++ b/cmd/bdinfo.c
@@ -474,14 +474,20 @@ int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char 
* const argv[])
 
 #elif defined(CONFIG_XTENSA)
 
-int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+#define USE_GENERIC
+
+#else
+ #error "a case for this architecture does not exist!"
+#endif
+
+/* Temporary check for archs that use generic bdinfo. Eventually all will */
+#ifdef USE_GENERIC
+int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
 {
print_std_bdinfo(gd->bd);
+
return 0;
 }
-
-#else
- #error "a case for this architecture does not exist!"
 #endif
 
 /*  */
-- 
2.26.2.645.ge9eca65c58-goog



  1   2   >