Re: [PATCH 1/2] pinctrl: k210: Fix inverted IE and OE for I2C

2020-11-12 Thread Rick Chen
Hi Sean

> > I2C and SCCB previously shared defaults. However, SCCB needs OE_INV and
> > IE_INV set, but I2C cannot have those bits set. This adds a separate
> > default for SCCB.
> >
> > Signed-off-by: Sean Anderson 
> > Reported-by: Damien Le Moal 
> > ---
> >
> >  drivers/pinctrl/pinctrl-kendryte.c | 10 ++
> >  1 file changed, 6 insertions(+), 4 deletions(-)
> >
> > diff --git a/drivers/pinctrl/pinctrl-kendryte.c 
> > b/drivers/pinctrl/pinctrl-kendryte.c
> > index 5ad049d955..259a0b507b 100644
> > --- a/drivers/pinctrl/pinctrl-kendryte.c
> > +++ b/drivers/pinctrl/pinctrl-kendryte.c
> > @@ -55,8 +55,9 @@
> >
>
> Reviewed-by: Rick Chen 

Please check about the CI failure items:
https://travis-ci.org/github/rickchen36/u-boot-riscv/builds/742884254

+drivers/pinctrl/pinctrl-kendryte.c:187:3: error:
'K210_PC_DEFAULT_SCCB' undeclared here (not in a function); did you
mean 'K210_PC_DEFAULT_SPI'?
1282+  187 |  [K210_PC_DEFAULT_##mode] = K210_PC_MODE_##mode
1283+  |   ^~~~
1284+drivers/pinctrl/pinctrl-kendryte.c:193:2: note: in expansion of
macro 'DEFAULT'
1285+  193 |  DEFAULT(SCCB),
1286+  |  ^~~
1287+drivers/pinctrl/pinctrl-kendryte.c:187:3: error: array index in
initializer not of integer type
1288+drivers/pinctrl/pinctrl-kendryte.c:187:3: note: (near
initialization for 'k210_pc_mode_id_to_mode')
1289+make[3]: *** [drivers/pinctrl/pinctrl-kendryte.o] Error 1
1290+make[2]: *** [drivers/pinctrl] Error 2
1291+make[1]: *** [drivers] Error 2
1292+make: *** [sub-make] Error 2
1293 riscv:  +   sipeed_maix_smode
1294+drivers/pinctrl/pinctrl-kendryte.c:187:3: error:
'K210_PC_DEFAULT_SCCB' undeclared here (not in a function); did you
mean 'K210_PC_DEFAULT_SPI'?
1295+  187 |  [K210_PC_DEFAULT_##mode] = K210_PC_MODE_##mode
1296+  |   ^~~~
1297+drivers/pinctrl/pinctrl-kendryte.c:193:2: note: in expansion of
macro 'DEFAULT'
1298+  193 |  DEFAULT(SCCB),
1299+  |  ^~~
1300+drivers/pinctrl/pinctrl-kendryte.c:187:3: error: array index in
initializer not of integer type
1301+drivers/pinctrl/pinctrl-kendryte.c:187:3: note: (near
initialization for 'k210_pc_mode_id_to_mode')
1302+make[3]: *** [drivers/pinctrl/pinctrl-kendryte.o] Error 1

Thanks,
Rick


Re: [PATCH v8 00/18] efi_loader: add capsule update support

2020-11-12 Thread Heinrich Schuchardt
On 11/13/20 5:14 AM, AKASHI Takahiro wrote:
> Summary
> ===
> 'UpdateCapsule' is one of runtime services defined in UEFI specification
> and its aim is to allow a caller (OS) to pass information to the firmware,
> i.e. U-Boot. This is mostly used to update firmware binary on devices by
> instructions from OS.
>
> While 'UpdateCapsule' is a runtime services function, it is, at least
> initially, supported only before exiting boot services alike other runtime
> functions, [Get/]SetVariable. This is because modifying storage which may
> be shared with OS must be carefully designed and there is no general
> assumption that we can do it.
>
> Therefore, we practically support only "capsule on disk"; any capsule can
> be handed over to UEFI subsystem as a file on a specific file system.
>
> In this patch series, all the related definitions and structures are given
> as UEFI specification describes, and basic framework for capsule support
> is provided. Currently supported is
>  * firmware update (Firmware Management Protocol or simply FMP)
>
> Most of functionality of firmware update is provided by FMP driver and
> it can be, by nature, system/platform-specific. So you can and should
> implement your own FMP driver(s) based on your system requirements.
> Under the current implementation, we provide two basic but generic
> drivers with two formats:
>   * FIT image format (as used in TFTP update and dfu)
>   * raw image format
>
> It's totally up to users which one, or both, should be used on users'
> system depending on user requirements.
>
> Quick usage
> ===
> 1. You can create a capsule file with the following host command:
>
>   $ mkeficapsule [--fit  | --raw ] 
>
> 2. Put the file under:
>
>   /EFI/UpdateCapsule of UEFI system partition
>
> 3. Specify firmware storage to be updated in "dfu_alt_info" variable
>(Please follow README.dfu for details.)
>
>   ==> env set dfu_alt_info '...'
>
> 4. After setting up UEFI's OsIndications variable, reboot U-Boot:
>
>   OsIndications <= EFI_OS_INDICATIONS_FILE_CAPSULE_DELIVERY_SUPPORTED
>
> Patch structure
> ===
> Patch#1-#4,#12: preparatory patches
> Patch#5-#11,#13: main part of implementation
> Patch#14-#15: utilities
> Patch#16-#17: pytests
> Patch#18: for sandbox test
>
> [1] https://git.linaro.org/people/takahiro.akashi/u-boot.git efi/capsule
>
> Prerequisite patches
> 
> None
>
> Test
> 
> * passed all the pytests which are included in this patch series
>   on sandbox build locally.
> * skipped (or 'S', but it's not a failure, 'F') in Travis CI because
>   "virt-make-fs" cannot be executed.

Dear Takahiro,

please, rebase your series on origin/master. A prior version of the
first patches is already applied.

Testing on Gitlab CI, Travis CI, Amazon CI must be addressed for merging
the remaining patches.

Best regards

Heinrich

>
> Issues
> ==
> * Timing of executing capsules-on-disk
>   Currently, processing a capsule is triggered only as part of
>   UEFI subsystem initialization. This means that, for example,
>   firmware update, may not take place at system booting time and
>   will potentially be delayed until a first call of any UEFI functions.
> => See patch#5 for my proposal
> * A bunch of warnings like
> WARNING: Use 'if (IS_ENABLED(CONFIG...))' instead of '#if or #ifdef'
> where possible
>   I don't think that fixing those improves anything.
> * Add a document in uefi.rst
>
> TODO's
> ==
> (Won't be addressed in this series.)
> * capsule authentication
> * capsule dependency (dependency expression instruction set, or depex)
> * loading drivers in a capsule
> * handling RESET flag in a capsule and QeuryCapsuleCaps
> * full semantics of ESRT (EFI System Resource Table)
> * enabling capsule API at runtime
> * json capsule
> * recovery from update failure
>
> Changes
> ===
> v8 (November 13, 2020)
> * fix a "not used" warning against update_load() in some configuration
>   (Patch#3)
> * fix failures (marked as 'F') in *secure boot* test in Travis CI by
>   making "EFI_CAPSULE_ON_DISK_EARLY" 'default n' (Patch#8)
> * fix failures (marked as 'E') at pytest setup in Travis CI by changing
>   a python file's name along with removing unused definitions (Patch#16)
> * add Patch#18 to enable tests to be run with default sandbox config
>   (Patch#18)
>
> v7 (October 29, 2020)
> * rename CONFIG_DFU_ALT to CONFIG_DFU_WRITE_ALT (Patch#1,#3,#13)
>
> v6 RESEND (October 29, 2020)
> * rebased on v2021.01-rc1
>
> v6 (September 7, 2020)
> * temporarily drop the prerequisite patch[2]
> * add a missing field (dependencies) in efi_api.h (but never used) (Patch#10)
> * add a missing field (image_capsule_support) and related definitions
>   in efi_api.h (Patch#10, #15)
> * cosmetic changes on constant definitions in efi_api.h (Patch#10)
> * strict check for INVALID_PARAMETER at GET_IMAGE_INFO api (Patch#11,#13)
> * fix warnings in pytest (Patch#16,#17)
>
> v5 (August 3, 2020)
> * removed superfluous type 

Re: [PATCH v2 1/2] i2c: ocores: add i2c driver for OpenCores I2C controller

2020-11-12 Thread Rick Chen
Hi Pragnesh

> > From: Pragnesh Patel [mailto:pragnesh.pa...@sifive.com]
> > Sent: Thursday, October 22, 2020 2:55 PM
> > To: u-boot@lists.denx.de
> > Cc: atish.pa...@wdc.com; palmerdabb...@google.com; bmeng...@gmail.com; 
> > paul.walms...@sifive.com; anup.pa...@wdc.com; sagar.ka...@sifive.com; Rick 
> > Jian-Zhi Chen(陳建志); pe...@korsgaard.com; Pragnesh Patel; Heiko Schocher
> > Subject: [PATCH v2 1/2] i2c: ocores: add i2c driver for OpenCores I2C 
> > controller
> >
> > Add support for the OpenCores I2C controller IP core
> > (See http://www.opencores.org/projects.cgi/web/i2c/overview).
> >
> > This driver implementation is inspired from the Linux OpenCores
> > I2C driver available.
> >
> > Thanks to Peter Korsgaard  for writing Linux
> > OpenCores I2C driver.
> >
> > Signed-off-by: Pragnesh Patel 
> > ---
> >
> > Changes in v2:
> > - Remove TYPE_SIFIVE_REV0 flag
> > - Update the Opencores I2C Controller Link
> >
> >  drivers/i2c/Kconfig  |   7 +
> >  drivers/i2c/Makefile |   1 +
> >  drivers/i2c/ocores_i2c.c | 636 +++
> >  3 files changed, 644 insertions(+)
> >  create mode 100644 drivers/i2c/ocores_i2c.c
> >
>
> Reviewed-by: Rick Chen 

Please check the CI failure item:
https://travis-ci.org/github/rickchen36/u-boot-riscv/jobs/743114190

+drivers/i2c/ocores_i2c.c: In function 'ocores_i2c_probe':
1291+drivers/i2c/ocores_i2c.c:523:4: error: implicit declaration of
function 'dev_err' [-Werror=implicit-function-declaration]
1292+  523 |dev_err(dev,
1293+  |^~~
1294+drivers/i2c/ocores_i2c.c:528:4: error: implicit declaration of
function 'dev_warn' [-Werror=implicit-function-declaration]
1295+  528 |dev_warn(dev,
1296+  |^~~~
1297+cc1: all warnings being treated as errors

Thanks,
Rick


RE: [PATCH] usb: xhci: fix event trb handling missed

2020-11-12 Thread Ran Wang
Hi Bin,

On Friday, November 13, 2020 2:25 PM Bin Meng wrote:
> 
> Hi Ran,
> 
> On Wed, Nov 11, 2020 at 4:07 PM Ran Wang  wrote:
> >
> > Hi Bin
> >
> > On Tuesday, November 10, 2020 5:55 PM, Ran Wang wrote:
> > >
> > > Hi Bin,
> > >
> >
> > 
> >
> > > > > record_transfer_result():comp_code: 1 csw org fbb4ec90,
> > > > > adjusted to fbb4fffe
> > > > > record_transfer_result():comp_code: 1
> > > > > record_transfer_result():comp_code: 1
> > > > > record_transfer_result():comp_code: 1 csw org fbb4ec90,
> > > > > adjusted to fbb4fffe
> > > > > record_transfer_result():comp_code: 1
> > > > > record_transfer_result():comp_code: 1
> > > > > record_transfer_result():comp_code: 1 csw org fbb4ec90,
> > > > > adjusted to fbb4fffe
> > > > > record_transfer_result():comp_code: 1
> > > > > record_transfer_result():comp_code: 1
> > > > > record_transfer_result():comp_code: 1
> > > >
> > > > In the earlier log you sent, you have the following:
> > > >
> > > > dev=fbb4f3c0, pipe=c0010283, buffer=fbb4fd80,
> > > > length=2048buffer would cross 64KB
> > > > boundary, so we will send request in more than 1 TRB, this is the
> > > > key issue trigger condition
> > > > xhci_bulk_tx()#0.1.running_total:0x280
> > > > xhci_bulk_tx()#0.2.trb_buff_len:0x280
> > > > xhci_bulk_tx()#0.3.running_total:0x280
> > > > xhci_bulk_tx()#0.4.num_trbs:0x2--2
> > > > Transfer TRB
> > > > xhci_bulk_tx()#0.5.running_total:0x10280
> > > > xhci_bulk_tx()#0.start_trb:0xfbb47140
> > > > --xhci_bulk_tx()#0.>enqueue->generic:0xfbb47
> > > > 140-
> > > > 
> > > > -Assemble
> > > > 1st Transfer TRB
> > > > xhci_bulk_tx()#0.addr:0xfbb4fd80
> > > > xhci_bulk_tx()#0.trb_buff_len:0x280
> > > > xhci_bulk_tx()#0.running_total:0x280
> > > > xhci_bulk_tx()#0.length:0x800
> > > > xhci_bulk_tx()#0.TRB_MAX_BUFF_SIZE:0x1
> > > > --xhci_bulk_tx()#0.>enqueue->generic:0xfbb47
> > > > 150-
> > > > 
> > > > -Assemble
> > > > 2nd Transfer TRB
> > > > xhci_bulk_tx()#0.addr:0xfbb5
> > > > xhci_bulk_tx()#0.trb_buff_len:0x580
> > > > xhci_bulk_tx()#0.running_total:0x800
> > > > xhci_bulk_tx()#0.length:0x800
> > > > xhci_bulk_tx()#0.TRB_MAX_BUFF_SIZE:0x1
> > > >
> > > > Could you please specify where this output is for?
> > >
> > > All this prints is within function xhci_bulk_tx(),
> > >
> > > Let me open those verbose output and check if that crossing 64KB
> > > case has been triggered or not.
> > > But I have to get back to you later (tomorrow).
> >
> > With your test commit and my verbose print code, issue cannot be
> > reproduced in mass storage usage (although crossing 64KB boundary
> > condition has met), so looks like the only different to ethernet device is 
> > that if
> xHC would receive short packet:
> >
> 
> Thank you.
> 
> > => pri t
> > t=usb read 0x8200 0 30
> > => run t
> >
> > usb read: device 0 block # 0, count 3145728 ...
> > usb_stor_BBB_transport()csw org fbb4ac90, adjusted to
> > fbb4fffe xhci_bulk_tx()dev=fbb49950, pipe=c0010203,
> > buffer=fbb1a9c0, length=0x1f
> > xhci_bulk_tx()#0.4.num_trbs:0x1
> >
> xhci_bulk_tx()#0.>enqueue->generic:0xfbb4957
> > 0
> > xhci_bulk_tx()#0.last_transfer_trb_addr:0xfbb49570
> > --xhci_bulk_tx()#0.event->trans_event.buffer:0xfbb
> > 49570
> > xhci_bulk_tx()#0.event->complete_code:1
> > xhci_bulk_tx()#3.ep_index:0x3
> > xhci_bulk_tx()dev=fbb49950, pipe=c0008283,
> > buffer=fbb1ab00, length=0x200
> > xhci_bulk_tx()#0.4.num_trbs:0x1
> > xhci_bulk_tx()#set TRB_ISP
> >
> xhci_bulk_tx()#0.>enqueue->generic:0xfbb4918
> > 0
> > xhci_bulk_tx()#0.last_transfer_trb_addr:0xfbb49180
> > --xhci_bulk_tx()#0.event->trans_event.buffer:0xfbb
> > 49180
> > xhci_bulk_tx()#0.event->complete_code:1
> > xhci_bulk_tx()#3.ep_index:0x2
> > xhci_bulk_tx()dev=fbb49950, pipe=c0008283,
> > buffer=fbb4fffe, length=0xd-buffer cross 64KB
> > boundary
> > xhci_bulk_tx()#0.4.num_trbs:0x2
> > xhci_bulk_tx()#set
> > TRB_ISP---
> > Set ISP
> >
> xhci_bulk_tx()#0.>enqueue->generic:0xfbb4919
> > 01st transfer TRB queued
> > xhci_bulk_tx()#0.last_transfer_trb_addr:0xfbb49190
> > xhci_bulk_tx()#set
> > TRB_ISP---
> > Set ISP
> >
> xhci_bulk_tx()#0.>enqueue->generic:0xfbb491a
> > 02nd transfer TRB queued
> > xhci_bulk_tx()#0.last_transfer_trb_addr:0xfbb491a0
> > --xhci_bulk_tx()#0.event->trans_event.buffer:0xfbb
> > 491a0-event TRB 

Re: [PATCH] usb: xhci: fix event trb handling missed

2020-11-12 Thread Bin Meng
Hi Ran,

On Wed, Nov 11, 2020 at 4:07 PM Ran Wang  wrote:
>
> Hi Bin
>
> On Tuesday, November 10, 2020 5:55 PM, Ran Wang wrote:
> >
> > Hi Bin,
> >
>
> 
>
> > > > record_transfer_result():comp_code: 1 csw org fbb4ec90,
> > > > adjusted to fbb4fffe
> > > > record_transfer_result():comp_code: 1
> > > > record_transfer_result():comp_code: 1
> > > > record_transfer_result():comp_code: 1 csw org fbb4ec90,
> > > > adjusted to fbb4fffe
> > > > record_transfer_result():comp_code: 1
> > > > record_transfer_result():comp_code: 1
> > > > record_transfer_result():comp_code: 1 csw org fbb4ec90,
> > > > adjusted to fbb4fffe
> > > > record_transfer_result():comp_code: 1
> > > > record_transfer_result():comp_code: 1
> > > > record_transfer_result():comp_code: 1
> > >
> > > In the earlier log you sent, you have the following:
> > >
> > > dev=fbb4f3c0, pipe=c0010283, buffer=fbb4fd80,
> > > length=2048buffer would cross 64KB
> > > boundary, so we will send request in more than 1 TRB, this is the key
> > > issue trigger condition
> > > xhci_bulk_tx()#0.1.running_total:0x280
> > > xhci_bulk_tx()#0.2.trb_buff_len:0x280
> > > xhci_bulk_tx()#0.3.running_total:0x280
> > > xhci_bulk_tx()#0.4.num_trbs:0x2--2 Transfer
> > > TRB
> > > xhci_bulk_tx()#0.5.running_total:0x10280
> > > xhci_bulk_tx()#0.start_trb:0xfbb47140
> > > --xhci_bulk_tx()#0.>enqueue->generic:0xfbb47140-
> > > 
> > > -Assemble
> > > 1st Transfer TRB
> > > xhci_bulk_tx()#0.addr:0xfbb4fd80
> > > xhci_bulk_tx()#0.trb_buff_len:0x280
> > > xhci_bulk_tx()#0.running_total:0x280
> > > xhci_bulk_tx()#0.length:0x800
> > > xhci_bulk_tx()#0.TRB_MAX_BUFF_SIZE:0x1
> > > --xhci_bulk_tx()#0.>enqueue->generic:0xfbb47150-
> > > 
> > > -Assemble
> > > 2nd Transfer TRB
> > > xhci_bulk_tx()#0.addr:0xfbb5
> > > xhci_bulk_tx()#0.trb_buff_len:0x580
> > > xhci_bulk_tx()#0.running_total:0x800
> > > xhci_bulk_tx()#0.length:0x800
> > > xhci_bulk_tx()#0.TRB_MAX_BUFF_SIZE:0x1
> > >
> > > Could you please specify where this output is for?
> >
> > All this prints is within function xhci_bulk_tx(),
> >
> > Let me open those verbose output and check if that crossing 64KB case has 
> > been
> > triggered or not.
> > But I have to get back to you later (tomorrow).
>
> With your test commit and my verbose print code, issue cannot be reproduced 
> in mass storage usage
> (although crossing 64KB boundary condition has met), so looks like the only 
> different to ethernet device
> is that if xHC would receive short packet:
>

Thank you.

> => pri t
> t=usb read 0x8200 0 30
> => run t
>
> usb read: device 0 block # 0, count 3145728 ...
> usb_stor_BBB_transport()csw org fbb4ac90, adjusted to fbb4fffe
> xhci_bulk_tx()dev=fbb49950, pipe=c0010203, buffer=fbb1a9c0, 
> length=0x1f
> xhci_bulk_tx()#0.4.num_trbs:0x1
> xhci_bulk_tx()#0.>enqueue->generic:0xfbb49570
> xhci_bulk_tx()#0.last_transfer_trb_addr:0xfbb49570
> --xhci_bulk_tx()#0.event->trans_event.buffer:0xfbb49570
> xhci_bulk_tx()#0.event->complete_code:1
> xhci_bulk_tx()#3.ep_index:0x3
> xhci_bulk_tx()dev=fbb49950, pipe=c0008283, buffer=fbb1ab00, 
> length=0x200
> xhci_bulk_tx()#0.4.num_trbs:0x1
> xhci_bulk_tx()#set TRB_ISP
> xhci_bulk_tx()#0.>enqueue->generic:0xfbb49180
> xhci_bulk_tx()#0.last_transfer_trb_addr:0xfbb49180
> --xhci_bulk_tx()#0.event->trans_event.buffer:0xfbb49180
> xhci_bulk_tx()#0.event->complete_code:1
> xhci_bulk_tx()#3.ep_index:0x2
> xhci_bulk_tx()dev=fbb49950, pipe=c0008283, buffer=fbb4fffe, 
> length=0xd-buffer cross 64KB boundary
> xhci_bulk_tx()#0.4.num_trbs:0x2
> xhci_bulk_tx()#set 
> TRB_ISP---Set
>  ISP
> xhci_bulk_tx()#0.>enqueue->generic:0xfbb491901st
>  transfer TRB queued
> xhci_bulk_tx()#0.last_transfer_trb_addr:0xfbb49190
> xhci_bulk_tx()#set 
> TRB_ISP---Set
>  ISP
> xhci_bulk_tx()#0.>enqueue->generic:0xfbb491a02nd
>  transfer TRB queued
> xhci_bulk_tx()#0.last_transfer_trb_addr:0xfbb491a0
> --xhci_bulk_tx()#0.event->trans_event.buffer:0xfbb491a0-event
>  TRB that pointing to 2nd transfer TRB
> xhci_bulk_tx()#0.event->complete_code:1-not
>  short packet
> xhci_bulk_tx()#3.ep_index:0x2
> num_trbs_count > 1!
> ...
> usb_stor_BBB_transport()csw org fbb4aea0, adjusted to fbb4fffe
> xhci_bulk_tx()dev=fbb49950, pipe=c0010203, 

[PATCH v8 16/18] test/py: efi_capsule: test for FIT image capsule

2020-11-12 Thread AKASHI Takahiro
The test can run on sandbox build and it attempts to execute a firmware
update via a capsule-on-disk, using a FIT image capsule,
CONFIG_EFI_CAPSULE_FIT.

To run this test successfully, you need configure U-Boot specifically;
See test_capsule_firmware.py for requirements, and hence it won't run
on Travis CI, at least, for now.

Signed-off-by: AKASHI Takahiro 
---
 .../py/tests/test_efi_capsule/capsule_defs.py |   5 +
 test/py/tests/test_efi_capsule/conftest.py|  71 +++
 .../test_efi_capsule/test_capsule_firmware.py | 178 ++
 .../tests/test_efi_capsule/uboot_bin_env.its  |  36 
 tools/mkeficapsule.c  |   3 +-
 5 files changed, 292 insertions(+), 1 deletion(-)
 create mode 100644 test/py/tests/test_efi_capsule/capsule_defs.py
 create mode 100644 test/py/tests/test_efi_capsule/conftest.py
 create mode 100644 test/py/tests/test_efi_capsule/test_capsule_firmware.py
 create mode 100644 test/py/tests/test_efi_capsule/uboot_bin_env.its

diff --git a/test/py/tests/test_efi_capsule/capsule_defs.py 
b/test/py/tests/test_efi_capsule/capsule_defs.py
new file mode 100644
index ..4fd6353c2040
--- /dev/null
+++ b/test/py/tests/test_efi_capsule/capsule_defs.py
@@ -0,0 +1,5 @@
+# SPDX-License-Identifier:  GPL-2.0+
+
+# Directories
+CAPSULE_DATA_DIR = '/EFI/CapsuleTestData'
+CAPSULE_INSTALL_DIR = '/EFI/UpdateCapsule'
diff --git a/test/py/tests/test_efi_capsule/conftest.py 
b/test/py/tests/test_efi_capsule/conftest.py
new file mode 100644
index ..147266189912
--- /dev/null
+++ b/test/py/tests/test_efi_capsule/conftest.py
@@ -0,0 +1,71 @@
+# SPDX-License-Identifier:  GPL-2.0+
+# Copyright (c) 2020, Linaro Limited
+# Author: AKASHI Takahiro 
+
+import os
+import os.path
+import re
+from subprocess import call, check_call, check_output, CalledProcessError
+import pytest
+from capsule_defs import *
+
+#
+# Fixture for UEFI secure boot test
+#
+
+
+@pytest.fixture(scope='session')
+def efi_capsule_data(request, u_boot_config):
+"""Set up a file system to be used in UEFI capsule test.
+
+Args:
+request: Pytest request object.
+u_boot_config: U-boot configuration.
+
+Return:
+A path to disk image to be used for testing
+"""
+global CAPSULE_DATA_DIR, CAPSULE_INSTALL_DIR
+
+mnt_point = u_boot_config.persistent_data_dir + '/test_efi_capsule'
+data_dir = mnt_point + CAPSULE_DATA_DIR
+install_dir = mnt_point + CAPSULE_INSTALL_DIR
+image_path = u_boot_config.persistent_data_dir + '/test_efi_capsule.img'
+
+try:
+# Create a target device
+check_call('dd if=/dev/zero of=./spi.bin bs=1MiB count=16', shell=True)
+
+check_call('rm -rf %s' % mnt_point, shell=True)
+check_call('mkdir -p %s' % data_dir, shell=True)
+check_call('mkdir -p %s' % install_dir, shell=True)
+
+# Create capsule files
+# two regions: one for u-boot.bin and the other for u-boot.env
+check_call('cd %s; echo -n u-boot:Old > u-boot.bin.old; echo -n 
u-boot:New > u-boot.bin.new; echo -n u-boot-env:Old -> u-boot.env.old; echo -n 
u-boot-env:New > u-boot.env.new' % data_dir,
+   shell=True)
+check_call('sed -e \"s?BINFILE1?u-boot.bin.new?\" -e 
\"s?BINFILE2?u-boot.env.new?\" 
%s/test/py/tests/test_efi_capsule/uboot_bin_env.its > %s/uboot_bin_env.its' %
+   (u_boot_config.source_dir, data_dir),
+   shell=True)
+check_call('cd %s; %s/tools/mkimage -f uboot_bin_env.its 
uboot_bin_env.itb' %
+   (data_dir, u_boot_config.build_dir),
+   shell=True)
+check_call('cd %s; %s/tools/mkeficapsule --fit uboot_bin_env.itb 
--version 1 --index 1 Test01' %
+   (data_dir, u_boot_config.build_dir),
+   shell=True)
+
+# Create a disk image with EFI system partition
+check_call('virt-make-fs --partition=gpt --size=+1M --type=vfat %s %s' 
%
+   (mnt_point, image_path), shell=True)
+check_call('sgdisk %s -A 1:set:0 -t 
1:C12A7328-F81F-11D2-BA4B-00A0C93EC93B' %
+   image_path, shell=True)
+
+except CalledProcessError as exception:
+pytest.skip('Setup failed: %s' % exception.cmd)
+return
+else:
+yield image_path
+finally:
+call('rm -rf %s' % mnt_point, shell=True)
+call('rm -f %s' % image_path, shell=True)
+call('rm -f ./spi.bin', shell=True)
diff --git a/test/py/tests/test_efi_capsule/test_capsule_firmware.py 
b/test/py/tests/test_efi_capsule/test_capsule_firmware.py
new file mode 100644
index ..d8e27707fd79
--- /dev/null
+++ b/test/py/tests/test_efi_capsule/test_capsule_firmware.py
@@ -0,0 +1,178 @@
+# SPDX-License-Identifier:  GPL-2.0+
+# Copyright (c) 2020, Linaro Limited
+# Author: AKASHI Takahiro 
+#
+# U-Boot UEFI: Firmware Update Test
+
+"""
+This test verifies capsule-on-disk firmware update
+"""
+
+from subprocess import 

[PATCH v8 15/18] tools: add mkeficapsule command for UEFI capsule update

2020-11-12 Thread AKASHI Takahiro
This is a utility mainly for test purpose.
  mkeficapsule -f: create a test capsule file for FIT image firmware

Having said that, you will be able to customize the code to fit
your specific requirements for your platform.

Signed-off-by: AKASHI Takahiro 
---
 tools/Makefile   |   2 +
 tools/mkeficapsule.c | 238 +++
 2 files changed, 240 insertions(+)
 create mode 100644 tools/mkeficapsule.c

diff --git a/tools/Makefile b/tools/Makefile
index 51123fd92983..66d9376803e3 100644
--- a/tools/Makefile
+++ b/tools/Makefile
@@ -218,6 +218,8 @@ hostprogs-$(CONFIG_MIPS) += mips-relocs
 hostprogs-$(CONFIG_ASN1_COMPILER)  += asn1_compiler
 HOSTCFLAGS_asn1_compiler.o = -idirafter $(srctree)/include
 
+hostprogs-$(CONFIG_EFI_HAVE_CAPSULE_SUPPORT) += mkeficapsule
+
 # We build some files with extra pedantic flags to try to minimize things
 # that won't build on some weird host compiler -- though there are lots of
 # exceptions for files that aren't complaint.
diff --git a/tools/mkeficapsule.c b/tools/mkeficapsule.c
new file mode 100644
index ..db95426457cc
--- /dev/null
+++ b/tools/mkeficapsule.c
@@ -0,0 +1,238 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright 2018 Linaro Limited
+ * Author: AKASHI Takahiro
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+typedef __u8 u8;
+typedef __u16 u16;
+typedef __u32 u32;
+typedef __u64 u64;
+typedef __s16 s16;
+typedef __s32 s32;
+
+#define aligned_u64 __aligned_u64
+
+#ifndef __packed
+#define __packed __attribute__((packed))
+#endif
+
+#include 
+#include 
+
+static const char *tool_name = "mkeficapsule";
+
+efi_guid_t efi_guid_fm_capsule = EFI_FIRMWARE_MANAGEMENT_CAPSULE_ID_GUID;
+efi_guid_t efi_guid_image_type_uboot_fit =
+   EFI_FIRMWARE_IMAGE_TYPE_UBOOT_FIT_GUID;
+efi_guid_t efi_guid_image_type_uboot_raw =
+   EFI_FIRMWARE_IMAGE_TYPE_UBOOT_RAW_GUID;
+
+static struct option options[] = {
+   {"fit", required_argument, NULL, 'f'},
+   {"raw", required_argument, NULL, 'r'},
+   {"index", required_argument, NULL, 'i'},
+   {"instance", required_argument, NULL, 'I'},
+   {"version", required_argument, NULL, 'v'},
+   {"help", no_argument, NULL, 'h'},
+   {NULL, 0, NULL, 0},
+};
+
+static void print_usage(void)
+{
+   printf("Usage: %s [options] \n"
+  "Options:\n"
+  "\t--fit   new FIT image file\n"
+  "\t--raw   new raw image file\n"
+  "\t--index update image index\n"
+  "\t--instance   update hardware instance\n"
+  "\t--version firmware version\n"
+  "\t--help print a help message\n",
+  tool_name);
+}
+
+static int create_fwbin(char *path, char *bin, efi_guid_t *guid,
+   unsigned long version, unsigned long index,
+   unsigned long instance)
+{
+   struct efi_capsule_header header;
+   struct efi_firmware_management_capsule_header capsule;
+   struct efi_firmware_management_capsule_image_header image;
+   FILE *f, *g;
+   struct stat bin_stat;
+   u8 *data;
+   size_t size;
+
+#ifdef DEBUG
+   printf("For output: %s\n", path);
+   printf("\tbin: %s\n\ttype: %pUl\n" bin, guid);
+   printf("\tversion: %ld\n\tindex: %ld\n\tinstance: %ld\n",
+  version, index, instance);
+#endif
+
+   g = fopen(bin, "r");
+   if (!g) {
+   printf("cannot open %s\n", bin);
+   return -1;
+   }
+   if (stat(bin, _stat) < 0) {
+   printf("cannot determine the size of %s\n", bin);
+   goto err_1;
+   }
+   data = malloc(bin_stat.st_size);
+   if (!data) {
+   printf("cannot allocate memory: %lx\n", bin_stat.st_size);
+   goto err_1;
+   }
+   f = fopen(path, "w");
+   if (!f) {
+   printf("cannot open %s\n", path);
+   goto err_2;
+   }
+   header.capsule_guid = efi_guid_fm_capsule;
+   header.header_size = sizeof(header);
+   header.flags = CAPSULE_FLAGS_PERSIST_ACROSS_RESET; /* TODO */
+   header.capsule_image_size = sizeof(header)
+   + sizeof(capsule) + sizeof(u64)
+   + sizeof(image)
+   + bin_stat.st_size;
+
+   size = fwrite(, 1, sizeof(header), f);
+   if (size < sizeof(header)) {
+   printf("write failed (%lx)\n", size);
+   goto err_3;
+   }
+
+   capsule.version = 0x0001;
+   capsule.embedded_driver_count = 0;
+   capsule.payload_item_count = 1;
+   capsule.item_offset_list[0] = sizeof(capsule) + sizeof(u64);
+   size = fwrite(, 1, sizeof(capsule) + sizeof(u64), f);
+   if (size < (sizeof(capsule) + sizeof(u64))) {
+   printf("write failed 

[PATCH v8 18/18] sandbox: enable capsule update for testing

2020-11-12 Thread AKASHI Takahiro
Add more configuration options to allow for efi capsule update
on sandbox.

Signed-off-by: AKASHI Takahiro 
---
 configs/sandbox64_defconfig | 6 ++
 configs/sandbox_defconfig   | 6 ++
 2 files changed, 12 insertions(+)

diff --git a/configs/sandbox64_defconfig b/configs/sandbox64_defconfig
index e9928d5fc2e7..332fbb11e4e3 100644
--- a/configs/sandbox64_defconfig
+++ b/configs/sandbox64_defconfig
@@ -231,3 +231,9 @@ CONFIG_TEST_FDTDEC=y
 CONFIG_UNIT_TEST=y
 CONFIG_UT_TIME=y
 CONFIG_UT_DM=y
+#
+CONFIG_DFU_SF=y
+CONFIG_EFI_RUNTIME_UPDATE_CAPSULE=y
+CONFIG_EFI_CAPSULE_ON_DISK=y
+CONFIG_EFI_CAPSULE_FIRMWARE_FIT=y
+CONFIG_EFI_CAPSULE_FIRMWARE_RAW=y
diff --git a/configs/sandbox_defconfig b/configs/sandbox_defconfig
index 3043208b4b72..1011545304d0 100644
--- a/configs/sandbox_defconfig
+++ b/configs/sandbox_defconfig
@@ -274,3 +274,9 @@ CONFIG_TEST_FDTDEC=y
 CONFIG_UNIT_TEST=y
 CONFIG_UT_TIME=y
 CONFIG_UT_DM=y
+#
+CONFIG_DFU_SF=y
+CONFIG_EFI_RUNTIME_UPDATE_CAPSULE=y
+CONFIG_EFI_CAPSULE_ON_DISK=y
+CONFIG_EFI_CAPSULE_FIRMWARE_FIT=y
+CONFIG_EFI_CAPSULE_FIRMWARE_RAW=y
-- 
2.28.0



[PATCH v8 14/18] cmd: add "efidebug capsule" command

2020-11-12 Thread AKASHI Takahiro
"efidebug capsule" is more or less a debugging utility.
  efidebug capsule update: invoke UpdateCapsule against data on memory
  efidebug capsule show: show a capsule header
  efidebug capsule result: dump a capsule result variable

Signed-off-by: AKASHI Takahiro 
---
 cmd/efidebug.c | 235 +
 1 file changed, 235 insertions(+)

diff --git a/cmd/efidebug.c b/cmd/efidebug.c
index 5288b9920b4d..7d327c82681f 100644
--- a/cmd/efidebug.c
+++ b/cmd/efidebug.c
@@ -19,6 +19,228 @@
 #include 
 
 #define BS systab.boottime
+#define RT systab.runtime
+
+#ifdef CONFIG_EFI_HAVE_CAPSULE_SUPPORT
+/**
+ * do_efi_capsule_update() - process a capsule update
+ *
+ * @cmdtp: Command table
+ * @flag:  Command flag
+ * @argc:  Number of arguments
+ * @argv:  Argument array
+ * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
+ *
+ * Implement efidebug "capsule update" sub-command.
+ * process a capsule update.
+ *
+ * efidebug capsule update [-v] 
+ */
+static int do_efi_capsule_update(struct cmd_tbl *cmdtp, int flag,
+int argc, char * const argv[])
+{
+   struct efi_capsule_header *capsule;
+   int verbose = 0;
+   char *endp;
+   efi_status_t ret;
+
+   if (argc != 2 && argc != 3)
+   return CMD_RET_USAGE;
+
+   if (argc == 3) {
+   if (strcmp(argv[1], "-v"))
+   return CMD_RET_USAGE;
+
+   verbose = 1;
+   argc--;
+   argv++;
+   }
+
+   capsule = (typeof(capsule))simple_strtoul(argv[1], , 16);
+   if (endp == argv[1]) {
+   printf("Invalid address: %s", argv[1]);
+   return CMD_RET_FAILURE;
+   }
+
+   if (verbose) {
+   printf("Capsule guid: %pUl\n", >capsule_guid);
+   printf("Capsule flags: 0x%x\n", capsule->flags);
+   printf("Capsule header size: 0x%x\n", capsule->header_size);
+   printf("Capsule image size: 0x%x\n",
+  capsule->capsule_image_size);
+   }
+
+   ret = EFI_CALL(RT->update_capsule(, 1, (u64)NULL));
+   if (ret) {
+   printf("Cannot handle a capsule at %p", capsule);
+   return CMD_RET_FAILURE;
+   }
+
+   return CMD_RET_SUCCESS;
+}
+
+/**
+ * do_efi_capsule_show() - show capsule information
+ *
+ * @cmdtp: Command table
+ * @flag:  Command flag
+ * @argc:  Number of arguments
+ * @argv:  Argument array
+ * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
+ *
+ * Implement efidebug "capsule show" sub-command.
+ * show capsule information.
+ *
+ * efidebug capsule show 
+ */
+static int do_efi_capsule_show(struct cmd_tbl *cmdtp, int flag,
+  int argc, char * const argv[])
+{
+   struct efi_capsule_header *capsule;
+   char *endp;
+
+   if (argc != 2)
+   return CMD_RET_USAGE;
+
+   capsule = (typeof(capsule))simple_strtoul(argv[1], , 16);
+   if (endp == argv[1]) {
+   printf("Invalid address: %s", argv[1]);
+   return CMD_RET_FAILURE;
+   }
+
+   printf("Capsule guid: %pUl\n", >capsule_guid);
+   printf("Capsule flags: 0x%x\n", capsule->flags);
+   printf("Capsule header size: 0x%x\n", capsule->header_size);
+   printf("Capsule image size: 0x%x\n",
+  capsule->capsule_image_size);
+
+   return CMD_RET_SUCCESS;
+}
+
+/**
+ * do_efi_capsule_res() - show a capsule update result
+ *
+ * @cmdtp: Command table
+ * @flag:  Command flag
+ * @argc:  Number of arguments
+ * @argv:  Argument array
+ * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
+ *
+ * Implement efidebug "capsule result" sub-command.
+ * show a capsule update result.
+ * If result number is not specified, CapsuleLast will be shown.
+ *
+ * efidebug capsule result []
+ */
+static int do_efi_capsule_res(struct cmd_tbl *cmdtp, int flag,
+ int argc, char * const argv[])
+{
+   int capsule_id;
+   char *endp;
+   char var_name[12];
+   u16 var_name16[12], *p;
+   efi_guid_t guid;
+   struct efi_capsule_result_variable_header *result = NULL;
+   efi_uintn_t size;
+   efi_status_t ret;
+
+   if (argc != 1 && argc != 2)
+   return CMD_RET_USAGE;
+
+   guid = efi_guid_capsule_report;
+   if (argc == 1) {
+   size = sizeof(var_name16);
+   ret = EFI_CALL(RT->get_variable(L"CapsuleLast", , NULL,
+   , var_name16));
+   if (ret != EFI_SUCCESS) {
+   if (ret == EFI_NOT_FOUND)
+   printf("CapsuleLast doesn't exist\n");
+   else
+   printf("Failed to get CapsuleLast\n");
+
+   return CMD_RET_FAILURE;
+  

[PATCH v8 13/18] efi_loader: add firmware management protocol for raw image

2020-11-12 Thread AKASHI Takahiro
In this commit, a very simple firmware management protocol driver
is implemented. It will take a binary image in a capsule file and
apply the data using dfu backend storage drivers via dfu_write_by_alt()
interface.

So "dfu_alt_info" variable should be properly set to specify a device
and location to be updated. Please read README.dfu.

Signed-off-by: AKASHI Takahiro 
---
 include/efi_api.h |   4 +
 include/efi_loader.h  |   1 +
 lib/efi_loader/Kconfig|  16 +++
 lib/efi_loader/Makefile   |   2 +-
 lib/efi_loader/efi_capsule.c  |   8 ++
 lib/efi_loader/efi_firmware.c | 226 +-
 6 files changed, 199 insertions(+), 58 deletions(-)

diff --git a/include/efi_api.h b/include/efi_api.h
index 071d0ba866c7..c7038f863ab2 100644
--- a/include/efi_api.h
+++ b/include/efi_api.h
@@ -1853,6 +1853,10 @@ struct efi_signature_list {
EFI_GUID(0xae13ff2d, 0x9ad4, 0x4e25, 0x9a, 0xc8, \
 0x6d, 0x80, 0xb3, 0xb2, 0x21, 0x47)
 
+#define EFI_FIRMWARE_IMAGE_TYPE_UBOOT_RAW_GUID \
+   EFI_GUID(0xe2bb9c06, 0x70e9, 0x4b14, 0x97, 0xa3, \
+0x5a, 0x79, 0x13, 0x17, 0x6e, 0x3f)
+
 #define IMAGE_ATTRIBUTE_IMAGE_UPDATABLE0x0001
 #define IMAGE_ATTRIBUTE_RESET_REQUIRED 0x0002
 #define IMAGE_ATTRIBUTE_AUTHENTICATION_REQUIRED0x0004
diff --git a/include/efi_loader.h b/include/efi_loader.h
index de6d353de3b1..0f85873a72f8 100644
--- a/include/efi_loader.h
+++ b/include/efi_loader.h
@@ -812,6 +812,7 @@ void efi_memcpy_runtime(void *dest, const void *src, size_t 
n);
 u16 *efi_create_indexed_name(u16 *buffer, const u16 *name, unsigned int index);
 
 extern const struct efi_firmware_management_protocol efi_fmp_fit;
+extern const struct efi_firmware_management_protocol efi_fmp_raw;
 
 /* Capsule update */
 efi_status_t EFIAPI efi_update_capsule(
diff --git a/lib/efi_loader/Kconfig b/lib/efi_loader/Kconfig
index 159400fec39e..8332a5072d42 100644
--- a/lib/efi_loader/Kconfig
+++ b/lib/efi_loader/Kconfig
@@ -126,6 +126,10 @@ config EFI_CAPSULE_ON_DISK_EARLY
  executed as part of U-Boot initialisation so that they will
  surely take place whatever is set to distro_bootcmd.
 
+config EFI_CAPSULE_FIRMWARE
+   bool
+   default n
+
 config EFI_CAPSULE_FIRMWARE_MANAGEMENT
bool "Capsule: Firmware Management Protocol"
depends on EFI_HAVE_CAPSULE_SUPPORT
@@ -140,11 +144,23 @@ config EFI_CAPSULE_FIRMWARE_FIT
depends on FIT
select UPDATE_FIT
select DFU
+   select EFI_CAPSULE_FIRMWARE
default n
help
  Select this option if you want to enable firmware management protocol
  driver for FIT image
 
+config EFI_CAPSULE_FIRMWARE_RAW
+   bool "FMP driver for raw image"
+   depends on EFI_CAPSULE_FIRMWARE_MANAGEMENT
+   select DFU
+   select DFU_WRITE_ALT
+   select EFI_CAPSULE_FIRMWARE
+   default n
+   help
+ Select this option if you want to enable firmware management protocol
+ driver for raw image
+
 config EFI_DEVICE_PATH_TO_TEXT
bool "Device path to text protocol"
default y
diff --git a/lib/efi_loader/Makefile b/lib/efi_loader/Makefile
index 23b1828b0fe0..a7631d92a3b0 100644
--- a/lib/efi_loader/Makefile
+++ b/lib/efi_loader/Makefile
@@ -24,7 +24,7 @@ obj-$(CONFIG_CMD_BOOTEFI_HELLO) += helloworld_efi.o
 obj-y += efi_bootmgr.o
 obj-y += efi_boottime.o
 obj-$(CONFIG_EFI_HAVE_CAPSULE_SUPPORT) += efi_capsule.o
-obj-$(CONFIG_EFI_CAPSULE_FIRMWARE_FIT) += efi_firmware.o
+obj-$(CONFIG_EFI_CAPSULE_FIRMWARE) += efi_firmware.o
 obj-y += efi_console.o
 obj-y += efi_device_path.o
 obj-$(CONFIG_EFI_DEVICE_PATH_TO_TEXT) += efi_device_path_to_text.o
diff --git a/lib/efi_loader/efi_capsule.c b/lib/efi_loader/efi_capsule.c
index a44f81daaee9..af40b4a1599e 100644
--- a/lib/efi_loader/efi_capsule.c
+++ b/lib/efi_loader/efi_capsule.c
@@ -814,6 +814,14 @@ efi_status_t __weak arch_efi_load_capsule_drivers(void)
_fmp_fit, NULL));
}
 
+   if (IS_ENABLED(CONFIG_EFI_CAPSULE_FIRMWARE_RAW)) {
+   handle = NULL;
+   ret = EFI_CALL(efi_install_multiple_protocol_interfaces(
+   _root,
+   _guid_firmware_management_protocol,
+   _fmp_raw, NULL));
+   }
+
return ret;
 }
 
diff --git a/lib/efi_loader/efi_firmware.c b/lib/efi_loader/efi_firmware.c
index 4c395f4eb5d9..7e5607738319 100644
--- a/lib/efi_loader/efi_firmware.c
+++ b/lib/efi_loader/efi_firmware.c
@@ -13,16 +13,66 @@
 #include 
 #include 
 
-/*
- * This FIRMWARE_MANAGEMENT_PROTOCOL driver provides a firmware update
- * method with existing FIT image format, and handles
- *   - multiple regions of firmware via DFU
- * but doesn't support
- *   - versioning of firmware image
- *   - package information
- */
-const efi_guid_t 

[PATCH v8 17/18] test/py: efi_capsule: test for raw image capsule

2020-11-12 Thread AKASHI Takahiro
The test can run on sandbox build and it attempts to execute a firmware
update via a capsule-on-disk, using a raw image capsule,
CONFIG_EFI_CAPSULE_RAW.

To run this test successfully, you need configure U-Boot specifically;
See test_capsule_firmware.py for requirements, and hence it won't run
on Travis CI, at least, for now.

Signed-off-by: AKASHI Takahiro 
---
 test/py/tests/test_efi_capsule/conftest.py|  3 +
 .../test_efi_capsule/test_capsule_firmware.py | 63 +++
 2 files changed, 66 insertions(+)

diff --git a/test/py/tests/test_efi_capsule/conftest.py 
b/test/py/tests/test_efi_capsule/conftest.py
index 147266189912..e037bd20560d 100644
--- a/test/py/tests/test_efi_capsule/conftest.py
+++ b/test/py/tests/test_efi_capsule/conftest.py
@@ -53,6 +53,9 @@ def efi_capsule_data(request, u_boot_config):
 check_call('cd %s; %s/tools/mkeficapsule --fit uboot_bin_env.itb 
--version 1 --index 1 Test01' %
(data_dir, u_boot_config.build_dir),
shell=True)
+check_call('cd %s; %s/tools/mkeficapsule --raw u-boot.bin.new 
--version 1 --index 1 Test02' %
+   (data_dir, u_boot_config.build_dir),
+   shell=True)
 
 # Create a disk image with EFI system partition
 check_call('virt-make-fs --partition=gpt --size=+1M --type=vfat %s %s' 
%
diff --git a/test/py/tests/test_efi_capsule/test_capsule_firmware.py 
b/test/py/tests/test_efi_capsule/test_capsule_firmware.py
index d8e27707fd79..f006fa95d650 100644
--- a/test/py/tests/test_efi_capsule/test_capsule_firmware.py
+++ b/test/py/tests/test_efi_capsule/test_capsule_firmware.py
@@ -15,6 +15,7 @@ from capsule_defs import *
 
 @pytest.mark.boardspec('sandbox')
 @pytest.mark.buildconfigspec('efi_capsule_firmware_fit')
+@pytest.mark.buildconfigspec('efi_capsule_firmware_raw')
 @pytest.mark.buildconfigspec('efi_capsule_on_disk')
 @pytest.mark.buildconfigspec('dfu')
 @pytest.mark.buildconfigspec('dfu_sf')
@@ -176,3 +177,65 @@ class TestEfiCapsuleFirmwareFit(object):
 'sf read 400 15 10',
 'md.b 400 10'])
 assert 'u-boot-env:New' in ''.join(output)
+
+def test_efi_capsule_fw3(
+self, u_boot_config, u_boot_console, efi_capsule_data):
+"""
+Test Case 3 - Update U-Boot on SPI Flash, raw image format
+  0x10-0x15: U-Boot binary (but dummy)
+"""
+disk_img = efi_capsule_data
+with u_boot_console.log.section('Test Case 3-a, before reboot'):
+output = u_boot_console.run_command_list([
+'host bind 0 %s' % disk_img,
+'efidebug boot add 1 TEST host 0:1 /helloworld.efi ""',
+'efidebug boot order 1',
+'env set -e -nv -bs -rt OsIndications =0x0004',
+'env set dfu_alt_info "sf 0:0=u-boot-bin raw 0x10 
0x5;u-boot-env raw 0x15 0x20"',
+'env save'])
+
+# initialize content
+output = u_boot_console.run_command_list([
+'sf probe 0:0',
+'fatload host 0:1 400 %s/u-boot.bin.old' % 
CAPSULE_DATA_DIR,
+'sf write 400 10 10',
+'sf read 500 10 10',
+'md.b 500 10'])
+assert 'Old' in ''.join(output)
+
+# place a capsule file
+output = u_boot_console.run_command_list([
+'fatload host 0:1 400 %s/Test02' % CAPSULE_DATA_DIR,
+'fatwrite host 0:1 400 %s/Test02 $filesize' % 
CAPSULE_INSTALL_DIR,
+'fatls host 0:1 %s' % CAPSULE_INSTALL_DIR])
+assert 'Test02' in ''.join(output)
+
+# reboot
+u_boot_console.restart_uboot()
+
+capsule_early = u_boot_config.buildconfig.get(
+'config_efi_capsule_on_disk_early')
+with u_boot_console.log.section('Test Case 3-b, after reboot'):
+if not capsule_early:
+# make sure that dfu_alt_info exists even persistent variables
+# are not available.
+output = u_boot_console.run_command_list([
+'env set dfu_alt_info "sf 0:0=u-boot-bin raw 0x10 
0x5;u-boot-env raw 0x15 0x20"',
+'host bind 0 %s' % disk_img,
+'fatls host 0:1 %s' % CAPSULE_INSTALL_DIR])
+assert 'Test02' in ''.join(output)
+
+# need to run uefi command to initiate capsule handling
+output = u_boot_console.run_command(
+'env print -e -all Capsule')
+
+output = u_boot_console.run_command_list([
+'host bind 0 %s' % disk_img,
+'fatls host 0:1 %s' % CAPSULE_INSTALL_DIR])
+assert 'Test02' not in ''.join(output)
+
+output = u_boot_console.run_command_list([
+'sf probe 0:0',
+'sf 

[PATCH v8 10/18] efi_loader: capsule: support firmware update

2020-11-12 Thread AKASHI Takahiro
A capsule tagged with the guid, EFI_FIRMWARE_MANAGEMENT_CAPSULE_ID_GUID,
is handled as a firmware update object.
What efi_update_capsule() basically does is to load any firmware management
protocol (or fmp) drivers contained in a capsule, find out an appropriate
fmp driver and then invoke its set_image() interface against each binary
in a capsule.
In this commit, however, loading drivers is not supported.

The result of applying a capsule is set to be stored in "Capsule"
variable, but its implementation is deferred to a fmp driver.

Signed-off-by: AKASHI Takahiro 
---
 include/efi_api.h| 129 +++
 include/efi_loader.h |   2 +
 lib/efi_loader/Kconfig   |   8 ++
 lib/efi_loader/efi_capsule.c | 238 ++-
 lib/efi_loader/efi_setup.c   |   4 +
 5 files changed, 380 insertions(+), 1 deletion(-)

diff --git a/include/efi_api.h b/include/efi_api.h
index 7a2a087c60ed..966bc6e590bf 100644
--- a/include/efi_api.h
+++ b/include/efi_api.h
@@ -217,6 +217,9 @@ enum efi_reset_type {
 #define CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE0x0002
 #define CAPSULE_FLAGS_INITIATE_RESET   0x0004
 
+#define CAPSULE_SUPPORT_AUTHENTICATION 0x0001
+#define CAPSULE_SUPPORT_DEPENDENCY 0x0002
+
 #define EFI_CAPSULE_REPORT_GUID \
EFI_GUID(0x39b68c46, 0xf7fb, 0x441b, 0xb6, 0xec, \
 0x16, 0xb0, 0xf6, 0x98, 0x21, 0xf3)
@@ -225,6 +228,10 @@ enum efi_reset_type {
EFI_GUID(0xde9f0ec, 0x88b6, 0x428f, 0x97, 0x7a, \
 0x25, 0x8f, 0x1d, 0xe, 0x5e, 0x72)
 
+#define EFI_FIRMWARE_MANAGEMENT_CAPSULE_ID_GUID \
+   EFI_GUID(0x6dcbd5ed, 0xe82d, 0x4c44, 0xbd, 0xa1, \
+0x71, 0x94, 0x19, 0x9a, 0xd9, 0x2a)
+
 struct efi_capsule_header {
efi_guid_t capsule_guid;
u32 header_size;
@@ -253,6 +260,33 @@ struct efi_memory_range_capsule {
struct efi_memory_range memory_ranges[];
 } __packed;
 
+struct efi_firmware_management_capsule_header {
+   u32 version;
+   u16 embedded_driver_count;
+   u16 payload_item_count;
+   u64 item_offset_list[];
+} __packed;
+
+struct efi_firmware_management_capsule_image_header {
+   u32 version;
+   efi_guid_t update_image_type_id;
+   u8 update_image_index;
+   u8 reserved[3];
+   u32 update_image_size;
+   u32 update_vendor_code_size;
+   u64 update_hardware_instance;
+   u64 image_capsule_support;
+} __packed;
+
+struct efi_capsule_result_variable_fmp {
+   u16 version;
+   u8 payload_index;
+   u8 update_image_index;
+   efi_guid_t update_image_type_id;
+   // u16 capsule_file_name[];
+   // u16 capsule_target[];
+} __packed;
+
 #define EFI_RT_SUPPORTED_GET_TIME  0x0001
 #define EFI_RT_SUPPORTED_SET_TIME  0x0002
 #define EFI_RT_SUPPORTED_GET_WAKEUP_TIME   0x0004
@@ -1808,4 +1842,99 @@ struct efi_signature_list {
 /* struct efi_signature_data signatures[...][signature_size]; */
 } __attribute__((__packed__));
 
+/*
+ * Firmware management protocol
+ */
+#define EFI_FIRMWARE_MANAGEMENT_PROTOCOL_GUID \
+   EFI_GUID(0x86c77a67, 0x0b97, 0x4633, 0xa1, 0x87, \
+0x49, 0x10, 0x4d, 0x06, 0x85, 0xc7)
+
+#define IMAGE_ATTRIBUTE_IMAGE_UPDATABLE0x0001
+#define IMAGE_ATTRIBUTE_RESET_REQUIRED 0x0002
+#define IMAGE_ATTRIBUTE_AUTHENTICATION_REQUIRED0x0004
+#define IMAGE_ATTRIBUTE_IN_USE 0x0008
+#define IMAGE_ATTRIBUTE_UEFI_IMAGE 0x0010
+#define IMAGE_ATTRIBUTE_DEPENDENCY 0x0020
+
+#define IMAGE_COMPATIBILITY_CHECK_SUPPORTED0x0001
+
+#define IMAGE_UPDATABLE_VALID  0x0001
+#define IMAGE_UPDATABLE_INVALID0x0002
+#define IMAGE_UPDATABLE_INVALID_TYPE   0x0004
+#define IMAGE_UPDATABLE_INVALID_OLLD   0x0008
+#define IMAGE_UPDATABLE_VALID_WITH_VENDOR_CODE 0x0010
+
+#define PACKAGE_ATTRIBUTE_VERSION_UPDATABLE0x0001
+#define PACKAGE_ATTRIBUTE_RESET_REQUIRED   0x0002
+#define PACKAGE_ATTRIBUTE_AUTHENTICATION_REQUIRED  0x0004
+
+#define EFI_FIRMWARE_IMAGE_DESCRIPTOR_VERSION  4
+
+typedef struct efi_firmware_image_dependencies {
+   u8 dependencies[0];
+} efi_firmware_image_dep_t;
+
+struct efi_firmware_image_descriptor {
+   u8 image_index;
+   efi_guid_t image_type_id;
+   u64 image_id;
+   u16 *image_id_name;
+   u32 version;
+   u16 *version_name;
+   efi_uintn_t size;
+   u64 attributes_supported;
+   u64 attributes_setting;
+   u64 compatibilities;
+   u32 lowest_supported_image_version;
+   u32 last_attempt_version;
+   u32 last_attempt_status;
+   u64 hardware_instance;
+   

[PATCH v8 12/18] dfu: add dfu_write_by_alt()

2020-11-12 Thread AKASHI Takahiro
This function is a variant of dfu_write_by_name() and takes a DFU alt
setting number for dfu configuration.

It will be utilised to implement UEFI capsule management protocol for
raw image in a later commit.

Signed-off-by: AKASHI Takahiro 
---
 drivers/dfu/dfu_alt.c | 47 +++
 include/dfu.h | 26 +++-
 2 files changed, 72 insertions(+), 1 deletion(-)

diff --git a/drivers/dfu/dfu_alt.c b/drivers/dfu/dfu_alt.c
index 7528806cd163..d5827812070c 100644
--- a/drivers/dfu/dfu_alt.c
+++ b/drivers/dfu/dfu_alt.c
@@ -76,3 +76,50 @@ done:
 
return ret;
 }
+
+/**
+ * dfu_write_by_alt() - write data to DFU medium
+ * @dfu_alt_num:DFU alt setting number
+ * @addr:   Address of data buffer to write
+ * @len:Number of bytes
+ * @interface:  Destination DFU medium (e.g. "mmc")
+ * @devstring:  Instance number of destination DFU medium (e.g. "1")
+ *
+ * This function is storing data received on DFU supported medium which
+ * is specified by @dfu_alt_name.
+ *
+ * Return:  0 - on success, error code - otherwise
+ */
+int dfu_write_by_alt(int dfu_alt_num, void *addr, unsigned int len,
+char *interface, char *devstring)
+{
+   struct dfu_entity *dfu;
+   int ret;
+
+   debug("%s: alt: %d addr: 0x%p len: %d device: %s:%s\n", __func__,
+ dfu_alt_num, addr, len, interface, devstring);
+
+   ret = dfu_init_env_entities(interface, devstring);
+   if (ret)
+   goto done;
+
+   if (dfu_alt_num < 0) {
+   pr_err("Invalid alt number: %d", dfu_alt_num);
+   ret = -ENODEV;
+   goto done;
+   }
+
+   dfu = dfu_get_entity(dfu_alt_num);
+   if (!dfu) {
+   pr_err("DFU entity for alt: %d not found!", dfu_alt_num);
+   ret = -ENODEV;
+   goto done;
+   }
+
+   ret = dfu_write_from_mem_addr(dfu, (void *)(uintptr_t)addr, len);
+
+done:
+   dfu_free_entities();
+
+   return ret;
+}
diff --git a/include/dfu.h b/include/dfu.h
index eaf4bfc0d5ed..a767adee4107 100644
--- a/include/dfu.h
+++ b/include/dfu.h
@@ -496,6 +496,7 @@ static inline int dfu_fill_entity_virt(struct dfu_entity 
*dfu, char *devstr,
 }
 #endif
 
+#if CONFIG_IS_ENABLED(DFU_WRITE_ALT)
 /**
  * dfu_write_by_name() - write data to DFU medium
  * @dfu_entity_name:   Name of DFU entity to write
@@ -509,9 +510,24 @@ static inline int dfu_fill_entity_virt(struct dfu_entity 
*dfu, char *devstr,
  *
  * Return: 0 - on success, error code - otherwise
  */
-#if CONFIG_IS_ENABLED(DFU_WRITE_ALT)
 int dfu_write_by_name(char *dfu_entity_name, void *addr,
  unsigned int len, char *interface, char *devstring);
+
+/**
+ * dfu_write_by_alt() - write data to DFU medium
+ * @dfu_alt_num:   DFU alt setting number
+ * @addr:  Address of data buffer to write
+ * @len:   Number of bytes
+ * @interface: Destination DFU medium (e.g. "mmc")
+ * @devstring: Instance number of destination DFU medium (e.g. "1")
+ *
+ * This function is storing data received on DFU supported medium which
+ * is specified by @dfu_alt_name.
+ *
+ * Return: 0 - on success, error code - otherwise
+ */
+int dfu_write_by_alt(int dfu_alt_num, void *addr, unsigned int len,
+char *interface, char *devstring);
 #else
 static inline int dfu_write_by_name(char *dfu_entity_name, void *addr,
unsigned int len, char *interface,
@@ -520,6 +536,14 @@ static inline int dfu_write_by_name(char *dfu_entity_name, 
void *addr,
puts("write support for DFU not available!\n");
return -ENOSYS;
 }
+
+static inline int dfu_write_by_alt(int dfu_alt_num, void *addr,
+  unsigned int len, char *interface,
+  char *devstring)
+{
+   puts("write support for DFU not available!\n");
+   return -ENOSYS;
+}
 #endif
 
 int dfu_add(struct usb_configuration *c);
-- 
2.28.0



[PATCH v8 08/18] efi_loader: capsule: add capsule_on_disk support

2020-11-12 Thread AKASHI Takahiro
Capsule data can be loaded into the system either via UpdateCapsule
runtime service or files on a file system (of boot device).
The latter case is called "capsules on disk", and actual updates will
take place at the next boot time.

In this commit, we will support capsule on disk mechanism.

Please note that U-Boot itself has no notion of "boot device" and
all the capsule files to be executed will be detected only if they
are located in a specific directory, \EFI\UpdateCapsule, on a device
that is identified as a boot device by "Boot" variables.

Signed-off-by: AKASHI Takahiro 
---
 common/main.c|   4 +
 include/efi_loader.h |   9 +
 lib/efi_loader/Kconfig   |  22 ++
 lib/efi_loader/efi_capsule.c | 498 +++
 lib/efi_loader/efi_setup.c   |   8 +
 5 files changed, 541 insertions(+)

diff --git a/common/main.c b/common/main.c
index 4b3cd302c3e2..ae5bcdb32f8b 100644
--- a/common/main.c
+++ b/common/main.c
@@ -16,6 +16,7 @@
 #include 
 #include 
 #include 
+#include 
 
 static void run_preboot_environment_command(void)
 {
@@ -53,6 +54,9 @@ void main_loop(void)
if (IS_ENABLED(CONFIG_UPDATE_TFTP))
update_tftp(0UL, NULL, NULL);
 
+   if (IS_ENABLED(CONFIG_EFI_CAPSULE_ON_DISK_EARLY))
+   efi_launch_capsules();
+
s = bootdelay_process();
if (cli_process_fdt())
cli_secure_boot_cmd(s);
diff --git a/include/efi_loader.h b/include/efi_loader.h
index ffe772866675..572cfadd491a 100644
--- a/include/efi_loader.h
+++ b/include/efi_loader.h
@@ -820,6 +820,11 @@ efi_status_t EFIAPI efi_query_capsule_caps(
u64 *maximum_capsule_size,
u32 *reset_type);
 
+#define EFI_CAPSULE_DIR L"\\EFI\\UpdateCapsule\\"
+
+/* Hook at initialization */
+efi_status_t efi_launch_capsules(void);
+
 #else /* CONFIG_IS_ENABLED(EFI_LOADER) */
 
 /* Without CONFIG_EFI_LOADER we don't have a runtime section, stub it out */
@@ -836,6 +841,10 @@ static inline void efi_set_bootdev(const char *dev, const 
char *devnr,
   const char *path) { }
 static inline void efi_net_set_dhcp_ack(void *pkt, int len) { }
 static inline void efi_print_image_infos(void *pc) { }
+static inline efi_status_t efi_launch_capsules(void)
+{
+   return EFI_SUCCESS;
+}
 
 #endif /* CONFIG_IS_ENABLED(EFI_LOADER) */
 
diff --git a/lib/efi_loader/Kconfig b/lib/efi_loader/Kconfig
index 3ca396df3646..e1ac5ac055de 100644
--- a/lib/efi_loader/Kconfig
+++ b/lib/efi_loader/Kconfig
@@ -104,6 +104,28 @@ config EFI_RUNTIME_UPDATE_CAPSULE
  Select this option if you want to use UpdateCapsule and
  QueryCapsuleCapabilities API's.
 
+config EFI_CAPSULE_ON_DISK
+   bool "Enable capsule-on-disk support"
+   select EFI_HAVE_CAPSULE_SUPPORT
+   default n
+   help
+ Select this option if you want to use capsule-on-disk feature,
+ that is, capsules can be fetched and executed from files
+ under a specific directory on UEFI system partition instead of
+ via UpdateCapsule API.
+
+config EFI_CAPSULE_ON_DISK_EARLY
+   bool "Initiate capsule-on-disk at U-Boot boottime"
+   depends on EFI_CAPSULE_ON_DISK
+   default n
+   select EFI_SETUP_EARLY
+   help
+ Normally, without this option enabled, capsules will be
+ executed only at the first time of invoking one of efi command.
+ If this option is enabled, capsules will be enforced to be
+ executed as part of U-Boot initialisation so that they will
+ surely take place whatever is set to distro_bootcmd.
+
 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 02a8435cd3d1..2259bfbf3d19 100644
--- a/lib/efi_loader/efi_capsule.c
+++ b/lib/efi_loader/efi_capsule.c
@@ -11,10 +11,16 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 
 const efi_guid_t efi_guid_capsule_report = EFI_CAPSULE_REPORT_GUID;
 
+#ifdef CONFIG_EFI_CAPSULE_ON_DISK
+/* for file system access */
+static struct efi_file_handle *bootdev_root;
+#endif
+
 /**
  * get_last_capsule - get the last capsule index
  *
@@ -163,3 +169,495 @@ efi_status_t EFIAPI efi_query_capsule_caps(
 out:
return EFI_EXIT(ret);
 }
+
+#ifdef CONFIG_EFI_CAPSULE_ON_DISK
+/**
+ * get_dp_device - retrieve a device  path from boot variable
+ * @boot_var:  Boot variable name
+ * @device_dp  Device path
+ *
+ * Retrieve a device patch from boot variable, @boot_var.
+ *
+ * Return: status code
+ */
+static efi_status_t get_dp_device(u16 *boot_var,
+ struct efi_device_path **device_dp)
+{
+   void *buf = NULL;
+   efi_uintn_t size;
+   struct efi_load_option lo;
+   struct efi_device_path *file_dp;
+   efi_status_t ret;
+
+   size = 0;
+   ret = efi_get_variable_int(boot_var, _global_variable_guid,
+   

[PATCH v8 11/18] efi_loader: add firmware management protocol for FIT image

2020-11-12 Thread AKASHI Takahiro
In this commit, a very simple firmware management protocol driver
is implemented. It will take a common FIT image firmware in a capsule
file and apply the data using dfu backend storage drivers via
update_fit() interface.

So "dfu_alt_info" variable should be properly set to specify a device
and location to be updated. Please read README.dfu.

Fit image is a common file format for firmware update on U-Boot, and
this protocol works neatly just as a wrapper for one.

Signed-off-by: AKASHI Takahiro 
---
 include/efi_api.h |   4 +
 include/efi_loader.h  |   2 +
 lib/efi_loader/Kconfig|  11 ++
 lib/efi_loader/Makefile   |   1 +
 lib/efi_loader/efi_capsule.c  |  12 +-
 lib/efi_loader/efi_firmware.c | 291 ++
 6 files changed, 320 insertions(+), 1 deletion(-)
 create mode 100644 lib/efi_loader/efi_firmware.c

diff --git a/include/efi_api.h b/include/efi_api.h
index 966bc6e590bf..071d0ba866c7 100644
--- a/include/efi_api.h
+++ b/include/efi_api.h
@@ -1849,6 +1849,10 @@ struct efi_signature_list {
EFI_GUID(0x86c77a67, 0x0b97, 0x4633, 0xa1, 0x87, \
 0x49, 0x10, 0x4d, 0x06, 0x85, 0xc7)
 
+#define EFI_FIRMWARE_IMAGE_TYPE_UBOOT_FIT_GUID \
+   EFI_GUID(0xae13ff2d, 0x9ad4, 0x4e25, 0x9a, 0xc8, \
+0x6d, 0x80, 0xb3, 0xb2, 0x21, 0x47)
+
 #define IMAGE_ATTRIBUTE_IMAGE_UPDATABLE0x0001
 #define IMAGE_ATTRIBUTE_RESET_REQUIRED 0x0002
 #define IMAGE_ATTRIBUTE_AUTHENTICATION_REQUIRED0x0004
diff --git a/include/efi_loader.h b/include/efi_loader.h
index b3b575f10c6a..de6d353de3b1 100644
--- a/include/efi_loader.h
+++ b/include/efi_loader.h
@@ -811,6 +811,8 @@ void efi_memcpy_runtime(void *dest, const void *src, size_t 
n);
 /* commonly used helper function */
 u16 *efi_create_indexed_name(u16 *buffer, const u16 *name, unsigned int index);
 
+extern const struct efi_firmware_management_protocol efi_fmp_fit;
+
 /* Capsule update */
 efi_status_t EFIAPI efi_update_capsule(
struct efi_capsule_header **capsule_header_array,
diff --git a/lib/efi_loader/Kconfig b/lib/efi_loader/Kconfig
index 5cb34687c26a..159400fec39e 100644
--- a/lib/efi_loader/Kconfig
+++ b/lib/efi_loader/Kconfig
@@ -134,6 +134,17 @@ config EFI_CAPSULE_FIRMWARE_MANAGEMENT
  Select this option if you want to enable capsule-based
  firmware update using Firmware Management Protocol.
 
+config EFI_CAPSULE_FIRMWARE_FIT
+   bool "FMP driver for FIT image"
+   depends on EFI_CAPSULE_FIRMWARE_MANAGEMENT
+   depends on FIT
+   select UPDATE_FIT
+   select DFU
+   default n
+   help
+ Select this option if you want to enable firmware management protocol
+ driver for FIT image
+
 config EFI_DEVICE_PATH_TO_TEXT
bool "Device path to text protocol"
default y
diff --git a/lib/efi_loader/Makefile b/lib/efi_loader/Makefile
index c7d231f0e57d..23b1828b0fe0 100644
--- a/lib/efi_loader/Makefile
+++ b/lib/efi_loader/Makefile
@@ -24,6 +24,7 @@ obj-$(CONFIG_CMD_BOOTEFI_HELLO) += helloworld_efi.o
 obj-y += efi_bootmgr.o
 obj-y += efi_boottime.o
 obj-$(CONFIG_EFI_HAVE_CAPSULE_SUPPORT) += efi_capsule.o
+obj-$(CONFIG_EFI_CAPSULE_FIRMWARE_FIT) += efi_firmware.o
 obj-y += efi_console.o
 obj-y += efi_device_path.o
 obj-$(CONFIG_EFI_DEVICE_PATH_TO_TEXT) += efi_device_path_to_text.o
diff --git a/lib/efi_loader/efi_capsule.c b/lib/efi_loader/efi_capsule.c
index 2015929e3708..a44f81daaee9 100644
--- a/lib/efi_loader/efi_capsule.c
+++ b/lib/efi_loader/efi_capsule.c
@@ -804,7 +804,17 @@ static void efi_capsule_scan_done(void)
  */
 efi_status_t __weak arch_efi_load_capsule_drivers(void)
 {
-   return EFI_SUCCESS;
+   __maybe_unused efi_handle_t handle;
+   efi_status_t ret = EFI_SUCCESS;
+
+   if (IS_ENABLED(CONFIG_EFI_CAPSULE_FIRMWARE_FIT)) {
+   handle = NULL;
+   ret = EFI_CALL(efi_install_multiple_protocol_interfaces(
+   , _guid_firmware_management_protocol,
+   _fmp_fit, NULL));
+   }
+
+   return ret;
 }
 
 /**
diff --git a/lib/efi_loader/efi_firmware.c b/lib/efi_loader/efi_firmware.c
new file mode 100644
index ..4c395f4eb5d9
--- /dev/null
+++ b/lib/efi_loader/efi_firmware.c
@@ -0,0 +1,291 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * EFI Firmware management protocol
+ *
+ *  Copyright (c) 2020 Linaro Limited
+ * Author: AKASHI Takahiro
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+/*
+ * This FIRMWARE_MANAGEMENT_PROTOCOL driver provides a firmware update
+ * method with existing FIT image format, and handles
+ *   - multiple regions of firmware via DFU
+ * but doesn't support
+ *   - versioning of firmware image
+ *   - package information
+ */
+const efi_guid_t efi_firmware_image_type_uboot_fit =
+   EFI_FIRMWARE_IMAGE_TYPE_UBOOT_FIT_GUID;
+
+/**
+ * 

[PATCH v8 09/18] efi_loader: capsule: add memory range capsule definitions

2020-11-12 Thread AKASHI Takahiro
Memory range capsule gives us a way to notify that some memory regions
should be left untouched across the next reset.
See UEFI specification, section 8.5.3.

Since how we should handle this kind of capsule is totally up to
the system, no implementation will be added in this commit.

Signed-off-by: AKASHI Takahiro 
---
 include/efi_api.h | 17 +
 1 file changed, 17 insertions(+)

diff --git a/include/efi_api.h b/include/efi_api.h
index c128a0a66ce8..7a2a087c60ed 100644
--- a/include/efi_api.h
+++ b/include/efi_api.h
@@ -221,6 +221,10 @@ enum efi_reset_type {
EFI_GUID(0x39b68c46, 0xf7fb, 0x441b, 0xb6, 0xec, \
 0x16, 0xb0, 0xf6, 0x98, 0x21, 0xf3)
 
+#define EFI_MEMORY_RANGE_CAPSULE_GUID \
+   EFI_GUID(0xde9f0ec, 0x88b6, 0x428f, 0x97, 0x7a, \
+0x25, 0x8f, 0x1d, 0xe, 0x5e, 0x72)
+
 struct efi_capsule_header {
efi_guid_t capsule_guid;
u32 header_size;
@@ -236,6 +240,19 @@ struct efi_capsule_result_variable_header {
efi_status_t capsule_status;
 } __packed;
 
+struct efi_memory_range {
+   efi_physical_addr_t address;
+   u64 length;
+};
+
+struct efi_memory_range_capsule {
+   struct efi_capsule_header *header;
+   /* EFI_MEMORY_TYPE: 0x8000-0x */
+   enum efi_mem_type os_requested_memory_type;
+   u64 number_of_memory_ranges;
+   struct efi_memory_range memory_ranges[];
+} __packed;
+
 #define EFI_RT_SUPPORTED_GET_TIME  0x0001
 #define EFI_RT_SUPPORTED_SET_TIME  0x0002
 #define EFI_RT_SUPPORTED_GET_WAKEUP_TIME   0x0004
-- 
2.28.0



[PATCH v8 07/18] efi_loader: define UpdateCapsule api

2020-11-12 Thread AKASHI Takahiro
In this commit, skeleton functions for capsule-related API's are
added under CONFIG_EFI_UPDATE_CAPSULE configuration.
Detailed implementation for a specific capsule type will be added
in the succeeding patches.

Signed-off-by: AKASHI Takahiro 
---
 include/efi_api.h|  12 +++
 include/efi_loader.h |  13 +++
 lib/efi_loader/Kconfig   |  11 +++
 lib/efi_loader/Makefile  |   1 +
 lib/efi_loader/efi_capsule.c | 165 +++
 lib/efi_loader/efi_runtime.c | 104 --
 lib/efi_loader/efi_setup.c   |  64 +++---
 7 files changed, 316 insertions(+), 54 deletions(-)
 create mode 100644 lib/efi_loader/efi_capsule.c

diff --git a/include/efi_api.h b/include/efi_api.h
index 5744f6aed86d..c128a0a66ce8 100644
--- a/include/efi_api.h
+++ b/include/efi_api.h
@@ -217,6 +217,10 @@ enum efi_reset_type {
 #define CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE0x0002
 #define CAPSULE_FLAGS_INITIATE_RESET   0x0004
 
+#define EFI_CAPSULE_REPORT_GUID \
+   EFI_GUID(0x39b68c46, 0xf7fb, 0x441b, 0xb6, 0xec, \
+0x16, 0xb0, 0xf6, 0x98, 0x21, 0xf3)
+
 struct efi_capsule_header {
efi_guid_t capsule_guid;
u32 header_size;
@@ -224,6 +228,14 @@ struct efi_capsule_header {
u32 capsule_image_size;
 } __packed;
 
+struct efi_capsule_result_variable_header {
+   u32 variable_total_size;
+   u32 reserved;
+   efi_guid_t capsule_guid;
+   struct efi_time capsule_processed;
+   efi_status_t capsule_status;
+} __packed;
+
 #define EFI_RT_SUPPORTED_GET_TIME  0x0001
 #define EFI_RT_SUPPORTED_SET_TIME  0x0002
 #define EFI_RT_SUPPORTED_GET_WAKEUP_TIME   0x0004
diff --git a/include/efi_loader.h b/include/efi_loader.h
index 6865a4847d53..ffe772866675 100644
--- a/include/efi_loader.h
+++ b/include/efi_loader.h
@@ -207,6 +207,8 @@ extern const efi_guid_t efi_guid_cert_type_pkcs7;
 
 /* GUID of RNG protocol */
 extern const efi_guid_t efi_guid_rng_protocol;
+/* GUID of capsule update result */
+extern const efi_guid_t efi_guid_capsule_report;
 
 extern unsigned int __efi_runtime_start, __efi_runtime_stop;
 extern unsigned int __efi_runtime_rel_start, __efi_runtime_rel_stop;
@@ -807,6 +809,17 @@ void efi_memcpy_runtime(void *dest, const void *src, 
size_t n);
 /* commonly used helper function */
 u16 *efi_create_indexed_name(u16 *buffer, const u16 *name, unsigned int index);
 
+/* Capsule update */
+efi_status_t EFIAPI efi_update_capsule(
+   struct efi_capsule_header **capsule_header_array,
+   efi_uintn_t capsule_count,
+   u64 scatter_gather_list);
+efi_status_t EFIAPI efi_query_capsule_caps(
+   struct efi_capsule_header **capsule_header_array,
+   efi_uintn_t capsule_count,
+   u64 *maximum_capsule_size,
+   u32 *reset_type);
+
 #else /* CONFIG_IS_ENABLED(EFI_LOADER) */
 
 /* Without CONFIG_EFI_LOADER we don't have a runtime section, stub it out */
diff --git a/lib/efi_loader/Kconfig b/lib/efi_loader/Kconfig
index 075481428cdf..3ca396df3646 100644
--- a/lib/efi_loader/Kconfig
+++ b/lib/efi_loader/Kconfig
@@ -93,6 +93,17 @@ config EFI_SET_TIME
  Provide the SetTime() runtime service at boottime. This service
  can be used by an EFI application to adjust the real time clock.
 
+config EFI_HAVE_CAPSULE_SUPPORT
+   bool
+
+config EFI_RUNTIME_UPDATE_CAPSULE
+   bool "UpdateCapsule() runtime service"
+   default n
+   select EFI_HAVE_CAPSULE_SUPPORT
+   help
+ Select this option if you want to use UpdateCapsule and
+ QueryCapsuleCapabilities API's.
+
 config EFI_DEVICE_PATH_TO_TEXT
bool "Device path to text protocol"
default y
diff --git a/lib/efi_loader/Makefile b/lib/efi_loader/Makefile
index 9bad1d159b03..c7d231f0e57d 100644
--- a/lib/efi_loader/Makefile
+++ b/lib/efi_loader/Makefile
@@ -23,6 +23,7 @@ endif
 obj-$(CONFIG_CMD_BOOTEFI_HELLO) += helloworld_efi.o
 obj-y += efi_bootmgr.o
 obj-y += efi_boottime.o
+obj-$(CONFIG_EFI_HAVE_CAPSULE_SUPPORT) += efi_capsule.o
 obj-y += efi_console.o
 obj-y += efi_device_path.o
 obj-$(CONFIG_EFI_DEVICE_PATH_TO_TEXT) += efi_device_path_to_text.o
diff --git a/lib/efi_loader/efi_capsule.c b/lib/efi_loader/efi_capsule.c
new file mode 100644
index ..02a8435cd3d1
--- /dev/null
+++ b/lib/efi_loader/efi_capsule.c
@@ -0,0 +1,165 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ *  EFI Capsule
+ *
+ *  Copyright (c) 2018 Linaro Limited
+ * Author: AKASHI Takahiro
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+const efi_guid_t efi_guid_capsule_report = EFI_CAPSULE_REPORT_GUID;
+
+/**
+ * get_last_capsule - get the last capsule index
+ *
+ * Retrieve the index of the capsule invoked last time from "CapsuleLast"
+ * variable.
+ *
+ * Return:
+ * * > 0   - the last capsule index invoked
+ * * 0x- on error, or no 

[PATCH v8 06/18] efi_loader: add efi_create_indexed_name()

2020-11-12 Thread AKASHI Takahiro
This function will be used from several places in UEFI subsystem
to generate some specific form of utf-16 variable name.
For example, L"Capsule0001"

Signed-off-by: AKASHI Takahiro 
---
 include/efi_loader.h   |  3 +++
 lib/efi_loader/efi_setup.c | 30 ++
 2 files changed, 33 insertions(+)

diff --git a/include/efi_loader.h b/include/efi_loader.h
index 7eea5566fdc9..6865a4847d53 100644
--- a/include/efi_loader.h
+++ b/include/efi_loader.h
@@ -804,6 +804,9 @@ bool efi_image_parse(void *efi, size_t len, struct 
efi_image_regions **regp,
 /* runtime implementation of memcpy() */
 void efi_memcpy_runtime(void *dest, const void *src, size_t n);
 
+/* commonly used helper function */
+u16 *efi_create_indexed_name(u16 *buffer, const u16 *name, unsigned int index);
+
 #else /* CONFIG_IS_ENABLED(EFI_LOADER) */
 
 /* Without CONFIG_EFI_LOADER we don't have a runtime section, stub it out */
diff --git a/lib/efi_loader/efi_setup.c b/lib/efi_loader/efi_setup.c
index 45226c5c1a53..6346eda771d0 100644
--- a/lib/efi_loader/efi_setup.c
+++ b/lib/efi_loader/efi_setup.c
@@ -6,6 +6,7 @@
  */
 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -235,3 +236,32 @@ out:
efi_obj_list_initialized = ret;
return ret;
 }
+
+/**
+ * efi_create_indexed_name - create a string name with an index
+ * @buffer:Buffer
+ * @name:  Name string
+ * @index: Index
+ *
+ * Create a utf-16 string with @name, appending @index.
+ * For example, L"Capsule0001"
+ * This function is expected to be called only from several places
+ * in EFI subsystem. A caller should ensure that the buffer have
+ * enough space for a resulting string, including L"\0".
+ * No strict check against the length will be done here.
+ *
+ * Return: A pointer to the next position after the created string
+ * in @buffer, or NULL otherwise
+ */
+u16 *efi_create_indexed_name(u16 *buffer, const u16 *name, unsigned int index)
+{
+   u16 *p;
+   char index_buf[5];
+
+   u16_strcpy(buffer, name);
+   p = buffer + utf16_strnlen(name, SIZE_MAX);
+   sprintf(index_buf, "%04X", index);
+   utf8_utf16_strcpy(, index_buf);
+
+   return p;
+}
-- 
2.28.0



[PATCH v8 05/18] efi_loader: add option to initialise EFI subsystem early

2020-11-12 Thread AKASHI Takahiro
If this option, CONFIG_EFI_SETUP_EARLY, is enabled, the initialisation
of UEFI subsystem will be done as part of U-Boot initialisation.

Please note that this option won't be enabled explicitly by users,
instead, should be enabled implicitly by other configuration options.

Specifically, this feature will be utilised in implementing capsule-on-disk
feature.

Signed-off-by: AKASHI Takahiro 
---
 common/board_r.c   | 6 ++
 lib/efi_loader/Kconfig | 4 
 2 files changed, 10 insertions(+)

diff --git a/common/board_r.c b/common/board_r.c
index b9217b2e27f0..2212d981e5ea 100644
--- a/common/board_r.c
+++ b/common/board_r.c
@@ -73,6 +73,9 @@
 #if defined(CONFIG_GPIO_HOG)
 #include 
 #endif
+#ifdef CONFIG_EFI_SETUP_EARLY
+#include 
+#endif
 
 DECLARE_GLOBAL_DATA_PTR;
 
@@ -889,6 +892,9 @@ static init_fnc_t init_sequence_r[] = {
 #endif
 #if defined(CONFIG_PRAM)
initr_mem,
+#endif
+#ifdef CONFIG_EFI_SETUP_EARLY
+   (init_fnc_t)efi_init_obj_list,
 #endif
run_main_loop,
 };
diff --git a/lib/efi_loader/Kconfig b/lib/efi_loader/Kconfig
index ab42f3ba75b1..075481428cdf 100644
--- a/lib/efi_loader/Kconfig
+++ b/lib/efi_loader/Kconfig
@@ -27,6 +27,10 @@ config EFI_LOADER
 
 if EFI_LOADER
 
+config EFI_SETUP_EARLY
+   bool
+   default n
+
 choice
prompt "Store for non-volatile UEFI variables"
default EFI_VARIABLE_FILE_STORE
-- 
2.28.0



[PATCH v8 04/18] dfu: export dfu_list

2020-11-12 Thread AKASHI Takahiro
This variable will be utilized to enumerate all dfu entities
for UEFI capsule firmware update in a later commit.

Signed-off-by: AKASHI Takahiro 
---
 drivers/dfu/dfu.c | 2 +-
 include/dfu.h | 3 +++
 2 files changed, 4 insertions(+), 1 deletion(-)

diff --git a/drivers/dfu/dfu.c b/drivers/dfu/dfu.c
index a298c2c43999..501a60b34400 100644
--- a/drivers/dfu/dfu.c
+++ b/drivers/dfu/dfu.c
@@ -18,7 +18,7 @@
 #include 
 #include 
 
-static LIST_HEAD(dfu_list);
+LIST_HEAD(dfu_list);
 static int dfu_alt_num;
 static int alt_num_cnt;
 static struct hash_algo *dfu_hash_algo;
diff --git a/include/dfu.h b/include/dfu.h
index d3d7e07b60d8..eaf4bfc0d5ed 100644
--- a/include/dfu.h
+++ b/include/dfu.h
@@ -158,6 +158,9 @@ struct dfu_entity {
unsigned int inited:1;
 };
 
+struct list_head;
+extern struct list_head dfu_list;
+
 #ifdef CONFIG_SET_DFU_ALT_INFO
 /**
  * set_dfu_alt_info() - set dfu_alt_info environment variable
-- 
2.28.0



[PATCH v8 03/18] common: update: add a generic interface for FIT image

2020-11-12 Thread AKASHI Takahiro
The main purpose of this patch is to separate a generic interface for
updating firmware using DFU drivers from "auto-update" via tftp.

This function will also be used in implementing UEFI capsule update
in a later commit.

Signed-off-by: AKASHI Takahiro 
---
 common/Kconfig  | 15 ++
 common/Makefile |  3 +-
 common/update.c | 71 +
 drivers/dfu/Kconfig |  2 +-
 include/image.h | 12 
 5 files changed, 100 insertions(+), 3 deletions(-)

diff --git a/common/Kconfig b/common/Kconfig
index 318d372a481b..2bce8c9ba1b9 100644
--- a/common/Kconfig
+++ b/common/Kconfig
@@ -599,9 +599,15 @@ endmenu
 
 menu "Update support"
 
+config UPDATE_COMMON
+   bool
+   default n
+   select DFU_WRITE_ALT
+
 config UPDATE_TFTP
bool "Auto-update using fitImage via TFTP"
depends on FIT
+   select UPDATE_COMMON
help
  This option allows performing update of NOR with data in fitImage
  sent via TFTP boot.
@@ -616,6 +622,15 @@ config UPDATE_TFTP_MSEC_MAX
default 100
depends on UPDATE_TFTP
 
+config UPDATE_FIT
+   bool "Firmware update using fitImage"
+   depends on FIT
+   depends on DFU
+   select UPDATE_COMMON
+   help
+ This option allows performing update of DFU-capable storage with
+ data in fitImage.
+
 config ANDROID_AB
bool "Android A/B updates"
default n
diff --git a/common/Makefile b/common/Makefile
index 2e7a090588d9..bcf352d01652 100644
--- a/common/Makefile
+++ b/common/Makefile
@@ -53,8 +53,7 @@ obj-$(CONFIG_LCD_ROTATION) += lcd_console_rotation.o
 obj-$(CONFIG_LCD_DT_SIMPLEFB) += lcd_simplefb.o
 obj-$(CONFIG_LYNXKDI) += lynxkdi.o
 obj-$(CONFIG_MENU) += menu.o
-obj-$(CONFIG_UPDATE_TFTP) += update.o
-obj-$(CONFIG_DFU_TFTP) += update.o
+obj-$(CONFIG_UPDATE_COMMON) += update.o
 obj-$(CONFIG_USB_KEYBOARD) += usb_kbd.o
 obj-$(CONFIG_CMDLINE) += cli_readline.o cli_simple.o
 
diff --git a/common/update.c b/common/update.c
index 8dd6ee8b7ddb..a5879cb52c41 100644
--- a/common/update.c
+++ b/common/update.c
@@ -29,6 +29,7 @@
 #include 
 #include 
 
+#if defined(CONFIG_DFU_TFTP) || defined(CONFIG_UPDATE_TFTP)
 /* env variable holding the location of the update file */
 #define UPDATE_FILE_ENV"updatefile"
 
@@ -214,6 +215,7 @@ static int update_flash(ulong addr_source, ulong 
addr_first, ulong size)
 #endif
return 0;
 }
+#endif /* CONFIG_DFU_TFTP || CONFIG_UPDATE_TFTP */
 
 static int update_fit_getparams(const void *fit, int noffset, ulong *addr,
ulong *fladdr, ulong *size)
@@ -231,6 +233,7 @@ static int update_fit_getparams(const void *fit, int 
noffset, ulong *addr,
return 0;
 }
 
+#if defined(CONFIG_DFU_TFTP) || defined(CONFIG_UPDATE_TFTP)
 int update_tftp(ulong addr, char *interface, char *devstring)
 {
char *filename, *env_addr, *fit_image_name;
@@ -337,3 +340,71 @@ next_node:
 
return ret;
 }
+#endif /* CONFIG_DFU_UPDATE || CONFIG_UPDATE_TFTP */
+
+#ifdef CONFIG_UPDATE_FIT
+/**
+ * fit_update - update storage with FIT image
+ * @fit:   Pointer to FIT image
+ *
+ * Update firmware on storage using FIT image as input.
+ * The storage area to be update will be identified by the name
+ * in FIT and matching it to "dfu_alt_info" variable.
+ *
+ * Return:  0 - on success, non-zero - otherwise
+ */
+int fit_update(const void *fit)
+{
+   char *fit_image_name;
+   ulong update_addr, update_fladdr, update_size;
+   int images_noffset, ndepth, noffset;
+   int ret = 0;
+
+   if (!fit)
+   return -EINVAL;
+
+   if (!fit_check_format((void *)fit)) {
+   printf("Bad FIT format of the update file, aborting 
auto-update\n");
+   return -EINVAL;
+   }
+
+   /* process updates */
+   images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
+
+   ndepth = 0;
+   noffset = fdt_next_node(fit, images_noffset, );
+   while (noffset >= 0 && ndepth > 0) {
+   if (ndepth != 1)
+   goto next_node;
+
+   fit_image_name = (char *)fit_get_name(fit, noffset, NULL);
+   printf("Processing update '%s' :", fit_image_name);
+
+   if (!fit_image_verify(fit, noffset)) {
+   printf("Error: invalid update hash, aborting\n");
+   ret = 1;
+   goto next_node;
+   }
+
+   printf("\n");
+   if (update_fit_getparams(fit, noffset, _addr,
+_fladdr, _size)) {
+   printf("Error: can't get update parameters, 
aborting\n");
+   ret = 1;
+   goto next_node;
+   }
+
+   if (fit_image_check_type(fit, noffset, IH_TYPE_FIRMWARE)) {
+   ret = dfu_write_by_name(fit_image_name,
+

[PATCH v8 02/18] dfu: modify an argument type for an address

2020-11-12 Thread AKASHI Takahiro
The range of an addressable pointer can go beyond 'integer'.
So change the argument type to a void pointer.

Signed-off-by: AKASHI Takahiro 
Reviewed-by: Heinrich Schuchardt 
---
 common/update.c   | 3 ++-
 drivers/dfu/dfu_alt.c | 6 +++---
 include/dfu.h | 4 ++--
 3 files changed, 7 insertions(+), 6 deletions(-)

diff --git a/common/update.c b/common/update.c
index 39946776d74f..8dd6ee8b7ddb 100644
--- a/common/update.c
+++ b/common/update.c
@@ -324,7 +324,8 @@ got_update_file:
}
} else if (fit_image_check_type(fit, noffset,
IH_TYPE_FIRMWARE)) {
-   ret = dfu_write_by_name(fit_image_name, update_addr,
+   ret = dfu_write_by_name(fit_image_name,
+   (void *)update_addr,
update_size, interface,
devstring);
if (ret)
diff --git a/drivers/dfu/dfu_alt.c b/drivers/dfu/dfu_alt.c
index 5b1b13d7170d..7528806cd163 100644
--- a/drivers/dfu/dfu_alt.c
+++ b/drivers/dfu/dfu_alt.c
@@ -23,14 +23,14 @@
  *
  * Return:  0 - on success, error code - otherwise
  */
-int dfu_write_by_name(char *dfu_entity_name, unsigned int addr,
+int dfu_write_by_name(char *dfu_entity_name, void *addr,
  unsigned int len, char *interface, char *devstring)
 {
char *s, *sb;
int alt_setting_num, ret;
struct dfu_entity *dfu;
 
-   debug("%s: name: %s addr: 0x%x len: %d device: %s:%s\n", __func__,
+   debug("%s: name: %s addr: 0x%p len: %d device: %s:%s\n", __func__,
  dfu_entity_name, addr, len, interface, devstring);
 
ret = dfu_init_env_entities(interface, devstring);
@@ -69,7 +69,7 @@ int dfu_write_by_name(char *dfu_entity_name, unsigned int 
addr,
goto done;
}
 
-   ret = dfu_write_from_mem_addr(dfu, (void *)(uintptr_t)addr, len);
+   ret = dfu_write_from_mem_addr(dfu, (void *)addr, len);
 
 done:
dfu_free_entities();
diff --git a/include/dfu.h b/include/dfu.h
index a4cd86c0a6c4..d3d7e07b60d8 100644
--- a/include/dfu.h
+++ b/include/dfu.h
@@ -507,10 +507,10 @@ static inline int dfu_fill_entity_virt(struct dfu_entity 
*dfu, char *devstr,
  * Return: 0 - on success, error code - otherwise
  */
 #if CONFIG_IS_ENABLED(DFU_WRITE_ALT)
-int dfu_write_by_name(char *dfu_entity_name, unsigned int addr,
+int dfu_write_by_name(char *dfu_entity_name, void *addr,
  unsigned int len, char *interface, char *devstring);
 #else
-static inline int dfu_write_by_name(char *dfu_entity_name, unsigned int addr,
+static inline int dfu_write_by_name(char *dfu_entity_name, void *addr,
unsigned int len, char *interface,
char *devstring)
 {
-- 
2.28.0



[PATCH v8 01/18] dfu: rename dfu_tftp_write() to dfu_write_by_name()

2020-11-12 Thread AKASHI Takahiro
This function is essentially independent from tftp, and will also be
utilised in implementing UEFI capsule update in a later commit.
So just give it a more generic name.
In addition, a new configuration option, CONFIG_DFU_WRITE_ALT, was
introduced so that the file will be compiled with different options,
particularly one added in a later commit.

Signed-off-by: AKASHI Takahiro 
---
 common/update.c   |  5 +++--
 drivers/dfu/Kconfig   |  5 +
 drivers/dfu/Makefile  |  2 +-
 drivers/dfu/{dfu_tftp.c => dfu_alt.c} | 17 --
 include/dfu.h | 32 +--
 5 files changed, 40 insertions(+), 21 deletions(-)
 rename drivers/dfu/{dfu_tftp.c => dfu_alt.c} (67%)

diff --git a/common/update.c b/common/update.c
index 36b6b7523d50..39946776d74f 100644
--- a/common/update.c
+++ b/common/update.c
@@ -324,8 +324,9 @@ got_update_file:
}
} else if (fit_image_check_type(fit, noffset,
IH_TYPE_FIRMWARE)) {
-   ret = dfu_tftp_write(fit_image_name, update_addr,
-update_size, interface, devstring);
+   ret = dfu_write_by_name(fit_image_name, update_addr,
+   update_size, interface,
+   devstring);
if (ret)
return ret;
}
diff --git a/drivers/dfu/Kconfig b/drivers/dfu/Kconfig
index 0eec00ba734d..10196f390fcd 100644
--- a/drivers/dfu/Kconfig
+++ b/drivers/dfu/Kconfig
@@ -14,8 +14,13 @@ config DFU_OVER_TFTP
depends on NET
 
 if DFU
+config DFU_WRITE_ALT
+   bool
+   default n
+
 config DFU_TFTP
bool "DFU via TFTP"
+   select DFU_WRITE_ALT
select DFU_OVER_TFTP
help
  This option allows performing update of DFU-managed medium with data
diff --git a/drivers/dfu/Makefile b/drivers/dfu/Makefile
index 0d7925c083ef..dfbf64da6677 100644
--- a/drivers/dfu/Makefile
+++ b/drivers/dfu/Makefile
@@ -9,5 +9,5 @@ obj-$(CONFIG_$(SPL_)DFU_MTD) += dfu_mtd.o
 obj-$(CONFIG_$(SPL_)DFU_NAND) += dfu_nand.o
 obj-$(CONFIG_$(SPL_)DFU_RAM) += dfu_ram.o
 obj-$(CONFIG_$(SPL_)DFU_SF) += dfu_sf.o
-obj-$(CONFIG_$(SPL_)DFU_TFTP) += dfu_tftp.o
+obj-$(CONFIG_$(SPL_)DFU_WRITE_ALT) += dfu_alt.o
 obj-$(CONFIG_$(SPL_)DFU_VIRT) += dfu_virt.o
diff --git a/drivers/dfu/dfu_tftp.c b/drivers/dfu/dfu_alt.c
similarity index 67%
rename from drivers/dfu/dfu_tftp.c
rename to drivers/dfu/dfu_alt.c
index ffae4bb54f80..5b1b13d7170d 100644
--- a/drivers/dfu/dfu_tftp.c
+++ b/drivers/dfu/dfu_alt.c
@@ -10,8 +10,21 @@
 #include 
 #include 
 
-int dfu_tftp_write(char *dfu_entity_name, unsigned int addr, unsigned int len,
-  char *interface, char *devstring)
+/**
+ * dfu_write_by_name() - write data to DFU medium
+ * @dfu_entity_name:Name of DFU entity to write
+ * @addr:   Address of data buffer to write
+ * @len:Number of bytes
+ * @interface:  Destination DFU medium (e.g. "mmc")
+ * @devstring:  Instance number of destination DFU medium (e.g. "1")
+ *
+ * This function is storing data received on DFU supported medium which
+ * is specified by @dfu_entity_name.
+ *
+ * Return:  0 - on success, error code - otherwise
+ */
+int dfu_write_by_name(char *dfu_entity_name, unsigned int addr,
+ unsigned int len, char *interface, char *devstring)
 {
char *s, *sb;
int alt_setting_num, ret;
diff --git a/include/dfu.h b/include/dfu.h
index 84abdc79acd1..a4cd86c0a6c4 100644
--- a/include/dfu.h
+++ b/include/dfu.h
@@ -494,27 +494,27 @@ static inline int dfu_fill_entity_virt(struct dfu_entity 
*dfu, char *devstr,
 #endif
 
 /**
- * dfu_tftp_write() - write TFTP data to DFU medium
+ * dfu_write_by_name() - write data to DFU medium
+ * @dfu_entity_name:   Name of DFU entity to write
+ * @addr:  Address of data buffer to write
+ * @len:   Number of bytes
+ * @interface: Destination DFU medium (e.g. "mmc")
+ * @devstring: Instance number of destination DFU medium (e.g. "1")
  *
- * This function is storing data received via TFTP on DFU supported medium.
+ * This function is storing data received on DFU supported medium which
+ * is specified by @dfu_entity_name.
  *
- * @dfu_entity_name:   name of DFU entity to write
- * @addr:  address of data buffer to write
- * @len:   number of bytes
- * @interface: destination DFU medium (e.g. "mmc")
- * @devstring: instance number of destination DFU medium (e.g. "1")
- *
- * Return: 0 on success, otherwise error code
+ * Return: 0 - on success, error code - otherwise
  */
-#if CONFIG_IS_ENABLED(DFU_TFTP)
-int dfu_tftp_write(char *dfu_entity_name, unsigned int addr, unsigned int 

[PATCH v8 00/18] efi_loader: add capsule update support

2020-11-12 Thread AKASHI Takahiro
Summary
===
'UpdateCapsule' is one of runtime services defined in UEFI specification
and its aim is to allow a caller (OS) to pass information to the firmware,
i.e. U-Boot. This is mostly used to update firmware binary on devices by
instructions from OS.

While 'UpdateCapsule' is a runtime services function, it is, at least
initially, supported only before exiting boot services alike other runtime
functions, [Get/]SetVariable. This is because modifying storage which may
be shared with OS must be carefully designed and there is no general
assumption that we can do it.

Therefore, we practically support only "capsule on disk"; any capsule can
be handed over to UEFI subsystem as a file on a specific file system.

In this patch series, all the related definitions and structures are given
as UEFI specification describes, and basic framework for capsule support
is provided. Currently supported is
 * firmware update (Firmware Management Protocol or simply FMP)

Most of functionality of firmware update is provided by FMP driver and
it can be, by nature, system/platform-specific. So you can and should
implement your own FMP driver(s) based on your system requirements.
Under the current implementation, we provide two basic but generic
drivers with two formats:
  * FIT image format (as used in TFTP update and dfu)
  * raw image format

It's totally up to users which one, or both, should be used on users'
system depending on user requirements.

Quick usage
===
1. You can create a capsule file with the following host command:

  $ mkeficapsule [--fit  | --raw ] 

2. Put the file under:

  /EFI/UpdateCapsule of UEFI system partition

3. Specify firmware storage to be updated in "dfu_alt_info" variable
   (Please follow README.dfu for details.)

  ==> env set dfu_alt_info '...'

4. After setting up UEFI's OsIndications variable, reboot U-Boot:

  OsIndications <= EFI_OS_INDICATIONS_FILE_CAPSULE_DELIVERY_SUPPORTED

Patch structure
===
Patch#1-#4,#12: preparatory patches
Patch#5-#11,#13: main part of implementation
Patch#14-#15: utilities
Patch#16-#17: pytests
Patch#18: for sandbox test

[1] https://git.linaro.org/people/takahiro.akashi/u-boot.git efi/capsule

Prerequisite patches

None

Test

* passed all the pytests which are included in this patch series
  on sandbox build locally.
* skipped (or 'S', but it's not a failure, 'F') in Travis CI because
  "virt-make-fs" cannot be executed.

Issues
==
* Timing of executing capsules-on-disk
  Currently, processing a capsule is triggered only as part of
  UEFI subsystem initialization. This means that, for example,
  firmware update, may not take place at system booting time and
  will potentially be delayed until a first call of any UEFI functions.
=> See patch#5 for my proposal
* A bunch of warnings like
WARNING: Use 'if (IS_ENABLED(CONFIG...))' instead of '#if or #ifdef'
where possible
  I don't think that fixing those improves anything.
* Add a document in uefi.rst

TODO's
==
(Won't be addressed in this series.)
* capsule authentication
* capsule dependency (dependency expression instruction set, or depex)
* loading drivers in a capsule
* handling RESET flag in a capsule and QeuryCapsuleCaps
* full semantics of ESRT (EFI System Resource Table)
* enabling capsule API at runtime
* json capsule
* recovery from update failure

Changes
===
v8 (November 13, 2020)
* fix a "not used" warning against update_load() in some configuration
  (Patch#3)
* fix failures (marked as 'F') in *secure boot* test in Travis CI by
  making "EFI_CAPSULE_ON_DISK_EARLY" 'default n' (Patch#8)
* fix failures (marked as 'E') at pytest setup in Travis CI by changing
  a python file's name along with removing unused definitions (Patch#16)
* add Patch#18 to enable tests to be run with default sandbox config
  (Patch#18)

v7 (October 29, 2020)
* rename CONFIG_DFU_ALT to CONFIG_DFU_WRITE_ALT (Patch#1,#3,#13)

v6 RESEND (October 29, 2020)
* rebased on v2021.01-rc1

v6 (September 7, 2020)
* temporarily drop the prerequisite patch[2]
* add a missing field (dependencies) in efi_api.h (but never used) (Patch#10)
* add a missing field (image_capsule_support) and related definitions
  in efi_api.h (Patch#10, #15)
* cosmetic changes on constant definitions in efi_api.h (Patch#10)
* strict check for INVALID_PARAMETER at GET_IMAGE_INFO api (Patch#11,#13)
* fix warnings in pytest (Patch#16,#17)

v5 (August 3, 2020)
* removed superfluous type conversion at dfu_write_from_mem_addr()
  (Patch#2)
* introduced a common helper function, efi_create_indexed_name()
  (Patch#6,#7,#8)
* use efi_[get|set]_variable_int(), if necessary, with READ_ONLY
  (Patch#7,#8)
* return EFI_UNSUPPORTED at Patch#7
* changed the word, number, to 'index' (Patch#7,#8)
* removed 'ifdef CONFIG_EFI_CAPSULE_ON_DISK' from a header (Patch#8)
* initialize 'CapsuleLast' in efi_init_obj_list() (Patch#7,#8)
* added 'const' qualifier for filename argument at
  

Re: [PATCH] env: mmc: Correct partition comparison in mmc_offset_try_partition

2020-11-12 Thread Hoyeonjiki Kim
On Fri, Nov 13, 2020 at 5:06 AM Wolfgang Denk  wrote:
>
> Dear Hoyeonjiki Kim,
>
> In message <20201112123026.458-1-jigi@gmail.com> you wrote:
> > The function mmc_offset_try_partition searches MMC partition to save the
> > environment data by name.  However, it only compares the first word-size
> > bytes (size of 'const char *'), which may make the function to find
> > unintended partition.
> >
> > Correct the function not to partially compare the partition name with
> > config "u-boot,mmc-env-partition".
> >
> > Fixes: c9e87ba66540 ("env: Save environment at the end of an MMC partition")
> > Signed-off-by: Hoyeonjiki Kim 
> > Reviewed-by: Jaehoon Chung 
> > ---
> >  env/mmc.c | 2 +-
> >  1 file changed, 1 insertion(+), 1 deletion(-)
> >
> > diff --git a/env/mmc.c b/env/mmc.c
> > index 4e67180b23..505f7aa2b8 100644
> > --- a/env/mmc.c
> > +++ b/env/mmc.c
> > @@ -42,7 +42,7 @@ static inline int mmc_offset_try_partition(const char 
> > *str, int copy, s64 *val)
> >   if (ret < 0)
> >   return ret;
> >
> > - if (!strncmp((const char *)info.name, str, sizeof(str)))
> > + if (!strcmp((const char *)info.name, str))
>
> This looks dangerous to me.  If the used length  sizeof(str) is not
> correct, that should be fixed, but switching to strcmp() means we
> may run into problems if the buffer is not NUL terminated.  Do we
> have a guarantee here thai it always ist??  The fact that strncmp()
> has been used before suggests otyherwise.
>
> Please double-check.
>
> Best regards,
>
> Wolfgang Denk
>
> --
> DENX Software Engineering GmbH,  Managing Director: Wolfgang Denk
> HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
> Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: w...@denx.de
> Computers are not intelligent. They only think they are.

Dear Wolfgang Denk,

I left my reply on the thread for patch v2.
Please refer to that.

We can keep discussing it on that thread from now on.
Again, sorry for the confusion.

Best Regards,
Hoyeonjiki Kim


Re: [PATCH v2] env: mmc: Correct partition comparison in mmc_offset_try_partition

2020-11-12 Thread Hoyeonjiki Kim
On Fri, Nov 13, 2020 at 5:07 AM Wolfgang Denk  wrote:
>
> Dear Hoyeonjiki Kim,
>
> In message <20201112131237.1239-1-jigi@gmail.com> you wrote:
> > The function mmc_offset_try_partition searches MMC partition to save the
> > environment data by name.  However, it only compares the first word-size
> > bytes (size of 'const char *'), which may make the function to find
> > unintended partition.
> >
> > Correct the function not to partially compare the partition name with
> > config "u-boot,mmc-env-partition".
> >
> > Fixes: c9e87ba66540 ("env: Save environment at the end of an MMC partition")
> > Signed-off-by: Hoyeonjiki Kim 
> > Reviewed-by: Jaehoon Chung 
> > ---
> >  env/mmc.c | 2 +-
> >  1 file changed, 1 insertion(+), 1 deletion(-)
> >
> > diff --git a/env/mmc.c b/env/mmc.c
> > index 4e67180b23..505f7aa2b8 100644
> > --- a/env/mmc.c
> > +++ b/env/mmc.c
> > @@ -42,7 +42,7 @@ static inline int mmc_offset_try_partition(const char 
> > *str, int copy, s64 *val)
> >   if (ret < 0)
> >   return ret;
> >
> > - if (!strncmp((const char *)info.name, str, sizeof(str)))
> > + if (!strcmp((const char *)info.name, str))
>
> Resend my comment, too.  This looks dangerous, please double check!!

Dear Wolfgang Denk,

Thanks for your feedback.

As you referred, `strcmp` suffers with non-null terminated string(s).
I'd also checked if using `strcmp` can cause some issues and
seems it's **guaranteed** that there is no such issue in this context.

Here's why:
- the first input string, `info.name` comes from fn `part_get_info`
  which gets the partition info from one of the partition driver.

  Each driver will return the partition info with null terminated
  partition name (actually it must, or every part in U-Boot referring
  `info.name` will have potential issues), so the first input string
  is safe to use in `strcmp`.

- The second one, `str` comes from fn `fdtdec_get_config_string` which
  gets the 'u-boot,mmc-env-offset' property value from FDT.

  When you keep tracking that function, you will meet `fdt_get_string`
  which returns error (-FDT_ERR_TRUNCATED) if the property value is
  non-null terminated. So the second input string also is safe to use.

  fdtdec_get_config_string
  --> fdt_getprop
  --> fdt_getprop_namelen
  --> fdt_get_property_namelen_
  --> fdt_string_eq_
  --> fdt_get_stringa

For this reason, I think we can use `strcmp` in this context.

But if we need to specify that the context will not suffer anyway, there
is an option to use `strncmp` with `PART_NAME_LEN` as max count param.

`PART_NAME_LEN` is the size of `info.name` which is a character buffer.

Please let me know your opinion.

>
> Best regards,
>
> Wolfgang Denk
>
> --
> DENX Software Engineering GmbH,  Managing Director: Wolfgang Denk
> HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
> Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: w...@denx.de
> In Nature there are neither rewards nor punishments, there are conse-
> quences.-- R.G. Ingersoll


RE: [RESEND v2 07/22] arm: socfpga: Rearrange sequence of macros in handoff_soc64.h

2020-11-12 Thread Tan, Ley Foon



> -Original Message-
> From: Lim, Elly Siew Chin 
> Sent: Tuesday, November 10, 2020 2:44 PM
> To: u-boot@lists.denx.de
> Cc: Marek Vasut ; Tan, Ley Foon
> ; See, Chin Liang ;
> Simon Goldschmidt ; Chee, Tien Fong
> ; Westergreen, Dalon
> ; Simon Glass ; Gan,
> Yau Wai ; Lim, Elly Siew Chin
> 
> Subject: [RESEND v2 07/22] arm: socfpga: Rearrange sequence of macros in
> handoff_soc64.h
> 
> No functionality change. In preparation for Stratix10 and Agilex handoff
> function restructuring.
> 

This patch adding new macros, please add description for this in commit message.

Regards
Ley Foon


RE: [RESEND v2 06/22] arm: socfpga: Changed system_manager_s10.c to system_manager_soc64.c

2020-11-12 Thread Tan, Ley Foon



> -Original Message-
> From: Lim, Elly Siew Chin 
> Sent: Tuesday, November 10, 2020 2:44 PM
> To: u-boot@lists.denx.de
> Cc: Marek Vasut ; Tan, Ley Foon
> ; See, Chin Liang ;
> Simon Goldschmidt ; Chee, Tien Fong
> ; Westergreen, Dalon
> ; Simon Glass ; Gan,
> Yau Wai ; Lim, Elly Siew Chin
> 
> Subject: [RESEND v2 06/22] arm: socfpga: Changed system_manager_s10.c
> to system_manager_soc64.c
> 
> Rename to common file name to used by all SOC64 devices.
> No functionality change.
> 
> Signed-off-by: Siew Chin Lim 
> ---
>  arch/arm/mach-socfpga/Makefile   | 5 
> +++--
>  .../mach-socfpga/{system_manager_s10.c => system_manager_soc64.c}|
> 0
>  2 files changed, 3 insertions(+), 2 deletions(-)  rename arch/arm/mach-
> socfpga/{system_manager_s10.c => system_manager_soc64.c} (100%)

Reviewed-by: Ley Foon Tan 

Regards
Ley Foon


RE: [RESEND v2 05/22] arm: socfpga: Changed wrap_pll_config_s10.c to wrap_pll_config_soc64.c

2020-11-12 Thread Tan, Ley Foon



> -Original Message-
> From: Lim, Elly Siew Chin 
> Sent: Tuesday, November 10, 2020 2:44 PM
> To: u-boot@lists.denx.de
> Cc: Marek Vasut ; Tan, Ley Foon
> ; See, Chin Liang ;
> Simon Goldschmidt ; Chee, Tien Fong
> ; Westergreen, Dalon
> ; Simon Glass ; Gan,
> Yau Wai ; Lim, Elly Siew Chin
> 
> Subject: [RESEND v2 05/22] arm: socfpga: Changed wrap_pll_config_s10.c to
> wrap_pll_config_soc64.c
> 
> Rename to common file name to used by all SOC64 devices.
> 
> Signed-off-by: Siew Chin Lim 
> ---
>  arch/arm/mach-socfpga/Makefile| 4 
> ++--
>  .../mach-socfpga/{wrap_pll_config_s10.c => wrap_pll_config_soc64.c}   | 2
> +-
>  2 files changed, 3 insertions(+), 3 deletions(-)  rename arch/arm/mach-
> socfpga/{wrap_pll_config_s10.c => wrap_pll_config_soc64.c} (96%)

Reviewed-by: Ley Foon Tan 

Regards
Ley Foon


RE: [RESEND v2 04/22] arm: socfpga: Rename Stratix10 and Agilex handoff common macros

2020-11-12 Thread Tan, Ley Foon



> -Original Message-
> From: Lim, Elly Siew Chin 
> Sent: Tuesday, November 10, 2020 2:44 PM
> To: u-boot@lists.denx.de
> Cc: Marek Vasut ; Tan, Ley Foon
> ; See, Chin Liang ;
> Simon Goldschmidt ; Chee, Tien Fong
> ; Westergreen, Dalon
> ; Simon Glass ; Gan,
> Yau Wai ; Lim, Elly Siew Chin
> 
> Subject: [RESEND v2 04/22] arm: socfpga: Rename Stratix10 and Agilex
> handoff common macros
> 
> Rename handoff_s10.h to handoff_soc64.h. Changed macros prefix from
> S10_HANDOFF to SOC64_HANDOFF.
> 
> Signed-off-by: Siew Chin Lim 
> ---
>  arch/arm/mach-socfpga/clock_manager_s10.c  |  2 +-
>  arch/arm/mach-socfpga/include/mach/handoff_s10.h   | 39 ---
> ---
>  arch/arm/mach-socfpga/include/mach/handoff_soc64.h | 39
> ++
>  arch/arm/mach-socfpga/wrap_pinmux_config_s10.c | 18 +-
>  arch/arm/mach-socfpga/wrap_pll_config_s10.c| 16 -
>  include/configs/socfpga_soc64_common.h |  4 +--
>  6 files changed, 59 insertions(+), 59 deletions(-)  delete mode 100644
> arch/arm/mach-socfpga/include/mach/handoff_s10.h
>  create mode 100644 arch/arm/mach-
> socfpga/include/mach/handoff_soc64.h

Reviewed-by: Ley Foon Tan 

Regards
Ley Foon


RE: [RESEND v2 03/22] arm: socfpga: dm: Add firewall support for Agilex and Diamond Mesa

2020-11-12 Thread Tan, Ley Foon



> -Original Message-
> From: Lim, Elly Siew Chin 
> Sent: Tuesday, November 10, 2020 2:44 PM
> To: u-boot@lists.denx.de
> Cc: Marek Vasut ; Tan, Ley Foon
> ; See, Chin Liang ;
> Simon Goldschmidt ; Chee, Tien Fong
> ; Westergreen, Dalon
> ; Simon Glass ; Gan,
> Yau Wai ; Lim, Elly Siew Chin
> 
> Subject: [RESEND v2 03/22] arm: socfpga: dm: Add firewall support for Agilex
> and Diamond Mesa
> 
> Disable the MPFE firewall for SMMU and HMC adapter for Agilex and
> Diamond Mesa.
> 
> Signed-off-by: Siew Chin Lim 
> ---
>  arch/arm/mach-socfpga/firewall.c   | 10 ++
>  arch/arm/mach-socfpga/include/mach/base_addr_s10.h |  1 +
>  arch/arm/mach-socfpga/include/mach/firewall.h  |  6 ++
>  3 files changed, 17 insertions(+)
> 
> diff --git a/arch/arm/mach-socfpga/firewall.c b/arch/arm/mach-
> socfpga/firewall.c
> index 69229dc651..b87cc8aa69 100644
> --- a/arch/arm/mach-socfpga/firewall.c
> +++ b/arch/arm/mach-socfpga/firewall.c
> @@ -104,4 +104,14 @@ void firewall_setup(void)
>  socfpga_get_sysmgr_addr() + SYSMGR_SOC64_DMA);
>   writel(SYSMGR_DMAPERIPH_ALL_NS,
>  socfpga_get_sysmgr_addr() + SYSMGR_SOC64_DMA_PERIPH);
> +
> +#if defined(CONFIG_TARGET_SOCFPGA_AGILEX) ||
> defined(CONFIG_TARGET_SOCFPGA_DM)
> + /* Disable the MPFE Firewall for SMMU */
> + writel(FIREWALL_MPFE_SCR_DISABLE_ALL,
> SOCFPGA_FW_MPFE_SCR_ADDRESS +
> +   FW_MPFE_SCR_HMC);
> + /* Disable MPFE Firewall for HMC adapter (ECC) */
> + writel(FIREWALL_MPFE_SCR_DISABLE_MPU,
> SOCFPGA_FW_MPFE_SCR_ADDRESS +
> +   FW_MPFE_SCR_HMC_ADAPTOR);
> +#endif
Stratix 10 also has these registers. Why Stratix 10 doesn't need this?

Regards
Ley Foon


[PATCH 2/2] arm: dts: enable MTK SPI NOR controller driver

2020-11-12 Thread SkyLake Huang
From: "SkyLake.Huang" 

Enable MTK SPI NOR controller driver on mt7622 & mt7629.

Signed-off-by: SkyLake.Huang 
---
 arch/arm/dts/mt7622-rfb.dts  | 14 +-
 arch/arm/dts/mt7622.dtsi |  7 +++
 arch/arm/dts/mt7629-rfb.dts  | 14 +-
 arch/arm/dts/mt7629.dtsi |  9 +
 configs/mt7622_rfb_defconfig |  3 ++-
 configs/mt7629_rfb_defconfig |  3 ++-
 6 files changed, 46 insertions(+), 4 deletions(-)

diff --git a/arch/arm/dts/mt7622-rfb.dts b/arch/arm/dts/mt7622-rfb.dts
index 52b34fa3b3..9322c54a3c 100644
--- a/arch/arm/dts/mt7622-rfb.dts
+++ b/arch/arm/dts/mt7622-rfb.dts
@@ -19,7 +19,7 @@
};
 
aliases {
-   spi0 = 
+   spi0 = 
};
 
memory@4000 {
@@ -151,6 +151,18 @@
quad-spi;
 };
 
+ {
+   pinctrl-names = "default";
+   pinctrl-0 = <_pins>;
+   status = "okay";
+
+   spi-flash@0{
+   compatible = "jedec,spi-nor";
+   reg = <0>;
+   u-boot,dm-pre-reloc;
+   };
+};
+
  {
pinctrl-names = "default";
pinctrl-0 = <_pins>;
diff --git a/arch/arm/dts/mt7622.dtsi b/arch/arm/dts/mt7622.dtsi
index e511432da9..53d24ac63d 100644
--- a/arch/arm/dts/mt7622.dtsi
+++ b/arch/arm/dts/mt7622.dtsi
@@ -68,7 +68,14 @@
 
assigned-clock-parents = < CLK_TOP_SYSPLL1_D2>,
 < CLK_TOP_UNIVPLL2_D8>;
+   };
 
+   snor: snor@11014000 {
+   compatible = "mediatek,mtk-snor";
+   reg = <0x11014000 0x1000>;
+   clocks = < CLK_PERI_FLASH_PD>,
+< CLK_TOP_FLASH_SEL>;
+   clock-names = "spi", "sf";
status = "disabled";
};
 
diff --git a/arch/arm/dts/mt7629-rfb.dts b/arch/arm/dts/mt7629-rfb.dts
index 687fe1c029..c75c4c7a38 100644
--- a/arch/arm/dts/mt7629-rfb.dts
+++ b/arch/arm/dts/mt7629-rfb.dts
@@ -14,7 +14,7 @@
compatible = "mediatek,mt7629-rfb", "mediatek,mt7629";
 
aliases {
-   spi0 = 
+   spi0 = 
};
 
chosen {
@@ -76,6 +76,18 @@
};
 };
 
+ {
+   pinctrl-names = "default";
+   pinctrl-0 = <_pins>;
+   status = "okay";
+
+   spi-flash@0{
+   compatible = "jedec,spi-nor";
+   reg = <0>;
+   u-boot,dm-pre-reloc;
+   };
+};
+
  {
pinctrl-names = "default";
pinctrl-0 = <_pins>;
diff --git a/arch/arm/dts/mt7629.dtsi b/arch/arm/dts/mt7629.dtsi
index a33a74a556..b268db7498 100644
--- a/arch/arm/dts/mt7629.dtsi
+++ b/arch/arm/dts/mt7629.dtsi
@@ -222,6 +222,15 @@
#size-cells = <0>;
};
 
+   snor: snor@11014000 {
+   compatible = "mediatek,mtk-snor";
+   reg = <0x11014000 0x1000>;
+   clocks = < CLK_PERI_FLASH_PD>,
+< CLK_TOP_FLASH_SEL>;
+   clock-names = "spi", "sf";
+   status = "disabled";
+   };
+
ethsys: syscon@1b00 {
compatible = "mediatek,mt7629-ethsys", "syscon";
reg = <0x1b00 0x1000>;
diff --git a/configs/mt7622_rfb_defconfig b/configs/mt7622_rfb_defconfig
index ae5fda52d7..4747470f08 100644
--- a/configs/mt7622_rfb_defconfig
+++ b/configs/mt7622_rfb_defconfig
@@ -54,7 +54,8 @@ CONFIG_DM_SERIAL=y
 CONFIG_MTK_SERIAL=y
 CONFIG_SPI=y
 CONFIG_DM_SPI=y
-CONFIG_MTK_SNFI_SPI=y
+# CONFIG_MTK_SNFI_SPI is not set
+CONFIG_MTK_SNOR=y
 CONFIG_SYSRESET_WATCHDOG=y
 CONFIG_TIMER=y
 CONFIG_MTK_TIMER=y
diff --git a/configs/mt7629_rfb_defconfig b/configs/mt7629_rfb_defconfig
index 9b94c2038e..b5a5059998 100644
--- a/configs/mt7629_rfb_defconfig
+++ b/configs/mt7629_rfb_defconfig
@@ -65,7 +65,8 @@ CONFIG_DM_SERIAL=y
 CONFIG_MTK_SERIAL=y
 CONFIG_SPI=y
 CONFIG_DM_SPI=y
-CONFIG_MTK_SNFI_SPI=y
+# CONFIG_MTK_SNFI_SPI is not set
+CONFIG_MTK_SNOR=y
 CONFIG_SYSRESET=y
 CONFIG_SPL_SYSRESET=y
 CONFIG_SYSRESET_WATCHDOG=y
-- 
2.18.0



[PATCH 1/2] spi: mtk_snor: add support for MTK SPI NOR controller

2020-11-12 Thread SkyLake Huang
From: "SkyLake.Huang" 

This patch adds support for MTK SPI NOR controller, which you
can see on mt7622 & mt7629.

This controller is designed only for SPI NOR. We can't adjust
its bus clock dynamically. Set clock in dts instead.

Signed-off-by: SkyLake.Huang 
---
 drivers/spi/Kconfig|   7 +
 drivers/spi/Makefile   |   1 +
 drivers/spi/mtk_snor.c | 597 +
 3 files changed, 605 insertions(+)
 create mode 100644 drivers/spi/mtk_snor.c

diff --git a/drivers/spi/Kconfig b/drivers/spi/Kconfig
index fae2040af8..670af450c1 100644
--- a/drivers/spi/Kconfig
+++ b/drivers/spi/Kconfig
@@ -174,6 +174,13 @@ config MT7621_SPI
  the SPI NOR flash on platforms embedding this Ralink / MediaTek
  SPI core, like MT7621/7628/7688.
 
+config MTK_SNOR
+   bool "Mediatek SPI-NOR controller driver"
+   depends on SPI_MEM
+   help
+ Enable the Mediatek SPINOR controller driver. This driver has
+  better read/write performance with NOR.
+
 config MTK_SNFI_SPI
bool "Mediatek SPI memory controller driver"
depends on SPI_MEM
diff --git a/drivers/spi/Makefile b/drivers/spi/Makefile
index ae4f2958f8..efe92f6b18 100644
--- a/drivers/spi/Makefile
+++ b/drivers/spi/Makefile
@@ -38,6 +38,7 @@ obj-$(CONFIG_MESON_SPIFC) += meson_spifc.o
 obj-$(CONFIG_MPC8XX_SPI) += mpc8xx_spi.o
 obj-$(CONFIG_MPC8XXX_SPI) += mpc8xxx_spi.o
 obj-$(CONFIG_MTK_SNFI_SPI) += mtk_snfi_spi.o
+obj-$(CONFIG_MTK_SNOR) += mtk_snor.o
 obj-$(CONFIG_MT7621_SPI) += mt7621_spi.o
 obj-$(CONFIG_MSCC_BB_SPI) += mscc_bb_spi.o
 obj-$(CONFIG_MVEBU_A3700_SPI) += mvebu_a3700_spi.o
diff --git a/drivers/spi/mtk_snor.c b/drivers/spi/mtk_snor.c
new file mode 100644
index 00..0a92f1c5a8
--- /dev/null
+++ b/drivers/spi/mtk_snor.c
@@ -0,0 +1,597 @@
+// SPDX-License-Identifier: GPL-2.0
+//
+// Mediatek SPI-NOR controller driver
+//
+// Copyright (C) 2020 SkyLake Huang 
+//
+// Some parts are based on drivers/spi/spi-mtk-nor.c of linux version
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#define DRIVER_NAME "mtk-spi-nor"
+
+#define MTK_NOR_REG_CMD0x00
+#define MTK_NOR_CMD_WRSR   BIT(5)
+#define MTK_NOR_CMD_WRITE  BIT(4)
+#define MTK_NOR_CMD_PROGRAMBIT(2)
+#define MTK_NOR_CMD_RDSR   BIT(1)
+#define MTK_NOR_CMD_READ   BIT(0)
+#define MTK_NOR_CMD_MASK   GENMASK(5, 0)
+
+#define MTK_NOR_REG_PRG_CNT0x04
+#define MTK_NOR_REG_RDSR   0x08
+#define MTK_NOR_REG_RDATA  0x0c
+
+#define MTK_NOR_REG_RADR0  0x10
+#define MTK_NOR_REG_RADR(n)(MTK_NOR_REG_RADR0 + 4 * (n))
+#define MTK_NOR_REG_RADR3  0xc8
+
+#define MTK_NOR_REG_WDATA  0x1c
+
+#define MTK_NOR_REG_PRGDATA0   0x20
+#define MTK_NOR_REG_PRGDATA(n) (MTK_NOR_REG_PRGDATA0 + 4 * (n))
+#define MTK_NOR_REG_PRGDATA_MAX5
+
+#define MTK_NOR_REG_SHIFT0 0x38
+#define MTK_NOR_REG_SHIFT(n)   (MTK_NOR_REG_SHIFT0 + 4 * (n))
+#define MTK_NOR_REG_SHIFT_MAX  9
+
+#define MTK_NOR_REG_CFG1   0x60
+#define MTK_NOR_FAST_READ  BIT(0)
+
+#define MTK_NOR_REG_CFG2   0x64
+#define MTK_NOR_WR_CUSTOM_OP_ENBIT(4)
+#define MTK_NOR_WR_BUF_EN  BIT(0)
+
+#define MTK_NOR_REG_PP_DATA0x98
+
+#define MTK_NOR_REG_IRQ_STAT   0xa8
+#define MTK_NOR_REG_IRQ_EN 0xac
+#define MTK_NOR_IRQ_DMABIT(7)
+#define MTK_NOR_IRQ_WRSR   BIT(5)
+#define MTK_NOR_IRQ_MASK   GENMASK(7, 0)
+
+#define MTK_NOR_REG_CFG3   0xb4
+#define MTK_NOR_DISABLE_WREN   BIT(7)
+#define MTK_NOR_DISABLE_SR_POLLBIT(5)
+
+#define MTK_NOR_REG_WP 0xc4
+#define MTK_NOR_ENABLE_SF_CMD  0x30
+
+#define MTK_NOR_REG_BUSCFG 0xcc
+#define MTK_NOR_4B_ADDRBIT(4)
+#define MTK_NOR_QUAD_ADDR  BIT(3)
+#define MTK_NOR_QUAD_READ  BIT(2)
+#define MTK_NOR_DUAL_ADDR  BIT(1)
+#define MTK_NOR_DUAL_READ  BIT(0)
+#define MTK_NOR_BUS_MODE_MASK  GENMASK(4, 0)
+
+#define MTK_NOR_REG_DMA_CTL0x718
+#define MTK_NOR_DMA_START  BIT(0)
+
+#define MTK_NOR_REG_DMA_FADR   0x71c
+#define MTK_NOR_REG_DMA_DADR   0x720
+#define MTK_NOR_REG_DMA_END_DADR   0x724
+
+#define MTK_NOR_PRG_MAX_SIZE   6
+// Reading DMA src/dst addresses have to be 16-byte aligned
+#define MTK_NOR_DMA_ALIGN  16
+#define MTK_NOR_DMA_ALIGN_MASK (MTK_NOR_DMA_ALIGN - 1)
+// and we allocate a bounce buffer if destination address isn't aligned.
+#define MTK_NOR_BOUNCE_BUF_SIZEPAGE_SIZE
+
+// Buffered page program can do one 128-byte 

[PATCH 0/2] *** Add support for Mediatek mt762x SoC ***

2020-11-12 Thread SkyLake Huang
From: "SkyLake.Huang" 

This patch adds support MTK's SPI NOR controller on
mt7622 & mt7629. With this controller, you can access
SPI NOR with better performance on mt762x platform.

SkyLake.Huang (2):
  spi: mtk_snor: add support for MTK SPI NOR controller
  arm: dts: enable MTK SPI NOR controller driver

 arch/arm/dts/mt7622-rfb.dts  |  14 +-
 arch/arm/dts/mt7622.dtsi |   7 +
 arch/arm/dts/mt7629-rfb.dts  |  14 +-
 arch/arm/dts/mt7629.dtsi |   9 +
 configs/mt7622_rfb_defconfig |   3 +-
 configs/mt7629_rfb_defconfig |   3 +-
 drivers/spi/Kconfig  |   7 +
 drivers/spi/Makefile |   1 +
 drivers/spi/mtk_snor.c   | 597 +++
 9 files changed, 651 insertions(+), 4 deletions(-)
 create mode 100644 drivers/spi/mtk_snor.c

-- 
2.18.0



Re: [PATCH 5/5] dtoc: Tidy up Python style in dtb_platdata

2020-11-12 Thread Walter Lozano

Hi Simon,

Thanks for this series. I've tried to test it but I had issues to apply 
it. I have tried in u-boot, both master and next, and u-boot-dm.


Could you please point me to the right tree/version?

On 9/11/20 00:36, Simon Glass wrote:

Update this, mostly to add comments for argument and return types. It is
probably still too early to use type hinting since it was introduced in
3.5.

Signed-off-by: Simon Glass 
---

  tools/dtoc/dtb_platdata.py | 71 ++
  1 file changed, 42 insertions(+), 29 deletions(-)

diff --git a/tools/dtoc/dtb_platdata.py b/tools/dtoc/dtb_platdata.py
index 0ef245397a9..6b10eabafb9 100644
--- a/tools/dtoc/dtb_platdata.py
+++ b/tools/dtoc/dtb_platdata.py
@@ -9,6 +9,8 @@
  
  This supports converting device tree data to C structures definitions and

  static data.
+
+See doc/driver-model/of-plat.rst for more informaiton

*information

  """
  
  import collections

@@ -19,9 +21,9 @@ import sys
  
  from dtoc import fdt

  from dtoc import fdt_util
-from patman import tools
  
-# When we see these properties we ignore them - i.e. do not create a structure member

+# When we see these properties we ignore them - i.e. do not create a structure
+# member
  PROP_IGNORE_LIST = [
  '#address-cells',
  '#gpio-cells',
@@ -69,9 +71,9 @@ def conv_name_to_c(name):
  (400ms for 1m calls versus 1000ms for the 're' version).
  
  Args:

-name:   Name to convert
+name (str): Name to convert
  Return:
-String containing the C version of this name
+str: String containing the C version of this name
  """
  new = name.replace('@', '_at_')
  new = new.replace('-', '_')
@@ -83,11 +85,11 @@ def tab_to(num_tabs, line):
  """Append tabs to a line of text to reach a tab stop.
  
  Args:

-num_tabs: Tab stop to obtain (0 = column 0, 1 = column 8, etc.)
-line: Line of text to append to
+num_tabs (int): Tab stop to obtain (0 = column 0, 1 = column 8, etc.)
+line (str): Line of text to append to
  
  Returns:

-line with the correct number of tabs appeneded. If the line already
+str: line with the correct number of tabs appeneded. If the line 
already
  extends past that tab stop then a single space is appended.
  """
  if len(line) >= num_tabs * 8:
@@ -103,28 +105,31 @@ def get_value(ftype, value):
  For booleans this return 'true'
  
  Args:

-type: Data type (fdt_util)
-value: Data value, as a string of bytes
+ftype (fdt.Type): Data type (fdt_util)
+value (bytes): Data value, as a string of bytes
+
+Returns:
+str: String representation of the value
  """
  if ftype == fdt.Type.INT:
  return '%#x' % fdt_util.fdt32_to_cpu(value)
  elif ftype == fdt.Type.BYTE:
  ch = value[0]
-return '%#x' % (ord(ch) if type(ch) == str else ch)
+return '%#x' % (ord(ch) if isinstance(ch, str) else ch)
  elif ftype == fdt.Type.STRING:
  # Handle evil ACPI backslashes by adding another backslash before 
them.
  # So "\\_SB.GPO0" in the device tree effectively stays like that in C
  return '"%s"' % value.replace('\\', '')
  elif ftype == fdt.Type.BOOL:
  return 'true'
-elif ftype == fdt.Type.INT64:
+else:  # ftype == fdt.Type.INT64:
  return '%#x' % value
  
  def get_compat_name(node):

  """Get the node's list of compatible string as a C identifiers
  
  Args:

-node: Node object to check
+node (fdt.Node): Node object to check
  Return:
  List of C identifiers for all the compatible strings
  """
@@ -213,7 +218,7 @@ class DtbPlatdata(object):
  file.
  
  Args:

-fname: Filename to send output to, or '-' for stdout
+fname (str): Filename to send output to, or '-' for stdout
  """
  if fname == '-':
  self._outfile = sys.stdout
@@ -224,7 +229,7 @@ class DtbPlatdata(object):
  """Output a string to the output file
  
  Args:

-line: String to output
+line (str): String to output
  """
  self._outfile.write(line)
  
@@ -232,7 +237,7 @@ class DtbPlatdata(object):

  """Buffer up a string to send later
  
  Args:

-line: String to add to our 'buffer' list
+line (str): String to add to our 'buffer' list
  """
  self._lines.append(line)
  
@@ -240,7 +245,7 @@ class DtbPlatdata(object):

  """Get the contents of the output buffer, and clear it
  
  Returns:

-The output buffer, which is then cleared for future use
+list(str): The output buffer, which is then cleared for future use
  """
  lines = self._lines
  self._lines = []
@@ -263,9 +268,14 @@ class DtbPlatdata(object):
  or not. As an interim measure, use a list of 

RE: [RESEND v2 02/22] arm: socfpga: dm: Add base address for Intel Diamond Mesa

2020-11-12 Thread Tan, Ley Foon



> -Original Message-
> From: Lim, Elly Siew Chin 
> Sent: Tuesday, November 10, 2020 2:44 PM
> To: u-boot@lists.denx.de
> Cc: Marek Vasut ; Tan, Ley Foon
> ; See, Chin Liang ;
> Simon Goldschmidt ; Chee, Tien Fong
> ; Westergreen, Dalon
> ; Simon Glass ; Gan,
> Yau Wai ; Lim, Elly Siew Chin
> 
> Subject: [RESEND v2 02/22] arm: socfpga: dm: Add base address for Intel
> Diamond Mesa
> 
> Reuse base_addr_s10.h for Diamond Mesa, the address is the same as Agilex.
> 
> Signed-off-by: Siew Chin Lim 
> ---
>  arch/arm/mach-socfpga/include/mach/base_addr_s10.h | 2 +-

Can consider change filename to _soc64.h

Regards
Ley Foon


RE: [RESEND v2 01/22] arm: socfpga: Move Stratix10 and Agilex to use TARGET_SOCFPGA_SOC64

2020-11-12 Thread Tan, Ley Foon



> -Original Message-
> From: Lim, Elly Siew Chin 
> Sent: Tuesday, November 10, 2020 2:44 PM
> To: u-boot@lists.denx.de
> Cc: Marek Vasut ; Tan, Ley Foon
> ; See, Chin Liang ;
> Simon Goldschmidt ; Chee, Tien Fong
> ; Westergreen, Dalon
> ; Simon Glass ; Gan,
> Yau Wai ; Lim, Elly Siew Chin
> 
> Subject: [RESEND v2 01/22] arm: socfpga: Move Stratix10 and Agilex to use
> TARGET_SOCFPGA_SOC64
> 
> Create common macro TARGET_SOCFPGA_SOC64 for Stratix10 and Agilex.
> 
> Signed-off-by: Siew Chin Lim 
> ---
>  arch/arm/Kconfig| 6 +++---
>  arch/arm/mach-socfpga/Kconfig   | 5 +
>  arch/arm/mach-socfpga/include/mach/reset_manager.h  | 3 +--
> arch/arm/mach-socfpga/include/mach/system_manager.h | 3 +--
>  drivers/ddr/altera/Kconfig  | 6 +++---
>  drivers/fpga/Kconfig| 2 +-
>  drivers/sysreset/Kconfig| 2 +-
>  7 files changed, 15 insertions(+), 12 deletions(-)
> 
> diff --git a/arch/arm/Kconfig b/arch/arm/Kconfig index
> b2f7fcbd6e..663ea07341 100644
> --- a/arch/arm/Kconfig
> +++ b/arch/arm/Kconfig
> @@ -974,7 +974,7 @@ config ARCH_SOCFPGA
>   bool "Altera SOCFPGA family"
>   select ARCH_EARLY_INIT_R
>   select ARCH_MISC_INIT if !TARGET_SOCFPGA_ARRIA10
> - select ARM64 if TARGET_SOCFPGA_STRATIX10 ||
> TARGET_SOCFPGA_AGILEX
> + select ARM64 if TARGET_SOCFPGA_SOC64
>   select CPU_V7A if TARGET_SOCFPGA_GEN5 ||
> TARGET_SOCFPGA_ARRIA10
>   select DM
>   select DM_SERIAL
> @@ -986,7 +986,7 @@ config ARCH_SOCFPGA
>   select SPL_LIBGENERIC_SUPPORT
>   select SPL_NAND_SUPPORT if SPL_NAND_DENALI
>   select SPL_OF_CONTROL
> - select SPL_SEPARATE_BSS if TARGET_SOCFPGA_STRATIX10 ||
> TARGET_SOCFPGA_AGILEX
> + select SPL_SEPARATE_BSS if TARGET_SOCFPGA_SOC64
>   select SPL_SERIAL_SUPPORT
>   select SPL_SYSRESET
>   select SPL_WATCHDOG_SUPPORT
> @@ -995,7 +995,7 @@ config ARCH_SOCFPGA
>   select SYS_THUMB_BUILD if TARGET_SOCFPGA_GEN5 ||
> TARGET_SOCFPGA_ARRIA10
>   select SYSRESET
>   select SYSRESET_SOCFPGA if TARGET_SOCFPGA_GEN5 ||
> TARGET_SOCFPGA_ARRIA10
> - select SYSRESET_SOCFPGA_SOC64 if TARGET_SOCFPGA_STRATIX10
> || TARGET_SOCFPGA_AGILEX
> + select SYSRESET_SOCFPGA_SOC64 if TARGET_SOCFPGA_SOC64
>   imply CMD_DM
>   imply CMD_MTDPARTS
>   imply CRC32_VERIFY
> diff --git a/arch/arm/mach-socfpga/Kconfig b/arch/arm/mach-
> socfpga/Kconfig index 7fdb52dd83..4d061a9d0d 100644
> --- a/arch/arm/mach-socfpga/Kconfig
> +++ b/arch/arm/mach-socfpga/Kconfig
> @@ -31,6 +31,7 @@ config SYS_TEXT_BASE
> 
>  config TARGET_SOCFPGA_AGILEX
>   bool
> + select TARGET_SOCFPGA_SOC64
Sort by alphabetical order. 

>   select ARMV8_MULTIENTRY
>   select ARMV8_SET_SMPEN
>   select CLK
> @@ -75,8 +76,12 @@ config TARGET_SOCFPGA_GEN5
>   imply SPL_SYS_MALLOC_SIMPLE
>   imply SPL_USE_TINY_PRINTF
> 
> +config TARGET_SOCFPGA_SOC64
> + bool
> +
>  config TARGET_SOCFPGA_STRATIX10
>   bool
> + select TARGET_SOCFPGA_SOC64
Sort by alphabetical order.

>   select ARMV8_MULTIENTRY
>   select ARMV8_SET_SMPEN
>   select FPGA_INTEL_SDM_MAILBOX

Regards
Ley Foon


RE: [v3 16/16] configs: socfpga: Add defconfig for Agilex and Stratix 10 with ATF support

2020-11-12 Thread Tan, Ley Foon



> -Original Message-
> From: Lim, Elly Siew Chin 
> Sent: Thursday, October 15, 2020 8:30 PM
> To: u-boot@lists.denx.de
> Cc: Marek Vasut ; Tan, Ley Foon
> ; See, Chin Liang ;
> Simon Goldschmidt ; Chee, Tien Fong
> ; Westergreen, Dalon
> ; Simon Glass ; Ang,
> Chee Hong ; Lim, Elly Siew Chin
> 
> Subject: [v3 16/16] configs: socfpga: Add defconfig for Agilex and Stratix 10
> with ATF support
> 
> From: Chee Hong Ang 
> 
> Booting Agilex and Stratix 10 with ATF support.
> 
> SPL now loads ATF (BL31), U-Boot proper and DTB from FIT image. The new
> boot flow with ATF support is as follow:
> 
> SPL -> ATF (BL31) -> U-Boot proper -> OS (Linux)
> 
> U-Boot proper now starts at 0x20 (CONFIG_SYS_TEXT_BASE).
> ATF will occupy the address range starting from 0x1000.
> 
> Signed-off-by: Chee Hong Ang 
> Signed-off-by: Siew Chin Lim 
> ---
>  configs/socfpga_agilex_atf_defconfig| 72
> 
>  configs/socfpga_stratix10_atf_defconfig | 74
> +
>  2 files changed, 146 insertions(+)
>  create mode 100644 configs/socfpga_agilex_atf_defconfig
>  create mode 100644 configs/socfpga_stratix10_atf_defconfig

Reviewed-by: Ley Foon Tan 

Regards
Ley Foon


RE: [v3 15/16] arm: socfpga: soc64: Enable FIT image generation using binman

2020-11-12 Thread Tan, Ley Foon



> -Original Message-
> From: Lim, Elly Siew Chin 
> Sent: Thursday, October 15, 2020 8:30 PM
> To: u-boot@lists.denx.de
> Cc: Marek Vasut ; Tan, Ley Foon
> ; See, Chin Liang ;
> Simon Goldschmidt ; Chee, Tien Fong
> ; Westergreen, Dalon
> ; Simon Glass ; Lim,
> Elly Siew Chin 
> Subject: [v3 15/16] arm: socfpga: soc64: Enable FIT image generation using
> binman
> 
> Add new build target "fit-itb" for FIT image generation.
> 
> In preparation to support Vendor Authorized Boot (VAB) for Intel
> SOC64 device in near future. With VAB, u-boot proper, dtb and arm trusted
> firmware need to be signed before FIT image generation.
> 
> To align user experience for ATF boot with and without VAB feature, we skip
> binman for ARCH_SOCFPGA in default Makefile flow. User always use 'make
> fit-itb' to generate FIT image after successfully compile u-boot.
> 
> Signed-off-by: Siew Chin Lim 
> ---
>  Makefile  | 7 +++
>  arch/arm/mach-socfpga/Kconfig | 2 ++
>  2 files changed, 9 insertions(+)
> 
> diff --git a/Makefile b/Makefile
> index 28c9f31fb4..ed05becc9e 100644
> --- a/Makefile
> +++ b/Makefile
> @@ -1020,8 +1020,10 @@ inputs: $(INPUTS-y)
> 
>  all: .binman_stamp inputs
>  ifeq ($(CONFIG_BINMAN),y)
> +ifneq ($(CONFIG_ARCH_SOCFPGA),y)
>   $(call if_changed,binman)
>  endif
> +endif
> 
>  # Timestamp file to make sure that binman always runs
>  .binman_stamp: FORCE
> @@ -1600,6 +1602,11 @@ cmd_socnandboot = cat  u-boot-spl-padx4.sfp
> u-boot.img > $@ || rm -f $@
>  u-boot-with-nand-spl.sfp: u-boot-spl-padx4.sfp u-boot.img FORCE
>   $(call if_changed,socnandboot)
> 
> +ifeq ($(CONFIG_FIT),y)
> +fit-itb:
> + $(call if_changed,binman)
> +endif
> +
>  endif
> 
>  ifeq ($(CONFIG_MPC85xx)$(CONFIG_OF_SEPARATE),yy)
> diff --git a/arch/arm/mach-socfpga/Kconfig b/arch/arm/mach-
> socfpga/Kconfig index 01f5a1fc41..7fdb52dd83 100644
> --- a/arch/arm/mach-socfpga/Kconfig
> +++ b/arch/arm/mach-socfpga/Kconfig
> @@ -37,6 +37,7 @@ config TARGET_SOCFPGA_AGILEX
>   select FPGA_INTEL_SDM_MAILBOX
>   select NCORE_CACHE
>   select SPL_CLK if SPL
> + select BINMAN
Sort by alphabetical order

>  config TARGET_SOCFPGA_ARRIA5
>   bool
> @@ -79,6 +80,7 @@ config TARGET_SOCFPGA_STRATIX10
>   select ARMV8_MULTIENTRY
>   select ARMV8_SET_SMPEN
>   select FPGA_INTEL_SDM_MAILBOX
> + select BINMAN

Sort by alphabetical order


Regards
Ley Foon


RE: [v3 14/16] arm: socfpga: dts: soc64: Add binman node of FIT image with ATF support

2020-11-12 Thread Tan, Ley Foon



> -Original Message-
> From: Lim, Elly Siew Chin 
> Sent: Thursday, October 15, 2020 8:30 PM
> To: u-boot@lists.denx.de
> Cc: Marek Vasut ; Tan, Ley Foon
> ; See, Chin Liang ;
> Simon Goldschmidt ; Chee, Tien Fong
> ; Westergreen, Dalon
> ; Simon Glass ; Lim,
> Elly Siew Chin 
> Subject: [v3 14/16] arm: socfpga: dts: soc64: Add binman node of FIT image
> with ATF support
> 
> Add binman node to device tree to generate the FIT image for u-boot
> (u-boot.itb) and OS kernel (kernel.itb).
> 
> u-boot.itb contains arm trusted firmware (ATF), u-boot proper and u-boot
> device tree for ATF u-boot flow.
> 
> kernel.itb contains Linux Image and Linux device tree.
> 
> Signed-off-by: Siew Chin Lim 
> ---
>  arch/arm/dts/socfpga_agilex-u-boot.dtsi  |   4 +-
>  arch/arm/dts/socfpga_soc64_fit-u-boot.dtsi   | 120
> +++
>  arch/arm/dts/socfpga_stratix10-u-boot.dtsi   |   8 ++
>  arch/arm/dts/socfpga_stratix10_socdk-u-boot.dtsi |   4 +-
>  4 files changed, 134 insertions(+), 2 deletions(-)  create mode 100644
> arch/arm/dts/socfpga_soc64_fit-u-boot.dtsi
>  create mode 100644 arch/arm/dts/socfpga_stratix10-u-boot.dtsi

Reviewed-by: Ley Foon Tan 

Regards
Ley Foon


[PATCH 2/2 v5] efi: Add basic EFI_TCG2_PROTOCOL support

2020-11-12 Thread Ilias Apalodimas
Since U-boot EFI implementation is getting richer it makes sense to
add support for EFI_TCG2_PROTOCOL taking advantage of any hardware TPM
available on the device.

This is the initial implementation of the protocol which only adds
support for GetCapability(). It's limited in the newer and safer
TPMv2 devices.

Signed-off-by: Ilias Apalodimas 
---
Heinrich has a basic EFI selftest [1] which works on actual hardware.
Unfortunately the sandbox TPMv2 doesn't support any of the current 
capability or PCR information calls the EFI protocol requires.
Specifically it does not support:
- TPM2_CAP_TPM_PROPERTIES for max command size, max response size, vendor id
  number of PCRs.
- TPM2_CAP_PCRS for getting info on the supported and active PCR banks.
[1] 
https://github.com/xypron/u-boot-patches/blob/efi-next/0001-efi_selftest-provide-unit-test-for-the-EFI_TCG2_PROT.patch
Adding those on a software implementation is not trivial. 

* changes since v4:
- scan all tpm devices on platform_get_tpm2_device() to find a tpmv2 device
- add efi_tcg2_ prefix on all efi protocol functions
- put algorithm related info in an array of structs and simplify calculation
  of supported and active PCR banks in tpm2_get_pcr_info()
* changes since v3:
- added check for maximum number of PCRs allowed
- replaced multiple return Xl with goto out tags
* changes since v2:
- added description about include/efi_tcg2.h
- switch bool to u8 for tpm_present_flag
- removed superfluous 'default n' from Kconfig
- use 'goto 'tag' when possible
* changes since v1: 
- change return variable of platform_get_tpm2_device() when used
- since more headers were included in patch #2 use them in offset 
  calculations for all tpm commands
- change the size of the response buffer regardless of what 
  tpm2_get_capability() is doing
 include/efi_loader.h   |   2 +
 include/efi_tcg2.h |  92 ++
 lib/efi_loader/Kconfig |   7 +
 lib/efi_loader/Makefile|   1 +
 lib/efi_loader/efi_setup.c |   8 +
 lib/efi_loader/efi_tcg2.c  | 558 +
 6 files changed, 668 insertions(+)
 create mode 100644 include/efi_tcg2.h
 create mode 100644 lib/efi_loader/efi_tcg2.c

diff --git a/include/efi_loader.h b/include/efi_loader.h
index f550ced56876..e5015d865ec9 100644
--- a/include/efi_loader.h
+++ b/include/efi_loader.h
@@ -405,6 +405,8 @@ efi_status_t efi_console_register(void);
 efi_status_t efi_disk_register(void);
 /* Called by efi_init_obj_list() to install EFI_RNG_PROTOCOL */
 efi_status_t efi_rng_register(void);
+/* Called by efi_init_obj_list() to install EFI_TCG2_PROTOCOL */
+efi_status_t efi_tcg2_register(void);
 /* Create handles and protocols for the partitions of a block device */
 int efi_disk_create_partitions(efi_handle_t parent, struct blk_desc *desc,
   const char *if_typename, int diskid,
diff --git a/include/efi_tcg2.h b/include/efi_tcg2.h
new file mode 100644
index ..86b8fe4c01af
--- /dev/null
+++ b/include/efi_tcg2.h
@@ -0,0 +1,92 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Defines data structures and APIs that allow an OS to interact with UEFI
+ * firmware to query information about the device
+ *
+ * Copyright (c) 2020, Linaro Limited
+ */
+
+#if !defined _EFI_TCG2_PROTOCOL_H_
+#define _EFI_TCG2_PROTOCOL_H_
+
+#include 
+
+#define EFI_TCG2_PROTOCOL_GUID \
+   EFI_GUID(0x607f766c, 0x7455, 0x42be, 0x93, \
+0x0b, 0xe4, 0xd7, 0x6d, 0xb2, 0x72, 0x0f)
+
+/* TPMV2 only */
+#define TCG2_EVENT_LOG_FORMAT_TCG_2 0x0002
+
+/* Algorithm Registry */
+#define EFI_TCG2_BOOT_HASH_ALG_SHA10x0001
+#define EFI_TCG2_BOOT_HASH_ALG_SHA256  0x0002
+#define EFI_TCG2_BOOT_HASH_ALG_SHA384  0x0004
+#define EFI_TCG2_BOOT_HASH_ALG_SHA512  0x0008
+#define EFI_TCG2_BOOT_HASH_ALG_SM3_256 0x0010
+
+typedef u32 efi_tcg_event_log_bitmap;
+typedef u32 efi_tcg_event_log_format;
+typedef u32 efi_tcg_event_algorithm_bitmap;
+
+struct efi_tcg2_version {
+   u8 major;
+   u8 minor;
+};
+
+struct efi_tcg2_event_header {
+   u32 header_size;
+   u16 header_version;
+   u32 pcr_index;
+   u32 event_type;
+} __packed;
+
+struct efi_tcg2_event {
+   u32 size;
+   struct efi_tcg2_event_header header;
+   u8 event[];
+} __packed;
+
+struct efi_tcg2_boot_service_capability {
+   u8 size;
+   struct efi_tcg2_version structure_version;
+   struct efi_tcg2_version protocol_version;
+   efi_tcg_event_algorithm_bitmap hash_algorithm_bitmap;
+   efi_tcg_event_log_bitmap supported_event_logs;
+   u8 tpm_present_flag;
+   u16 max_command_size;
+   u16 max_response_size;
+   u32 manufacturer_id;
+   u32 number_of_pcr_banks;
+   efi_tcg_event_algorithm_bitmap active_pcr_banks;
+};
+
+#define boot_service_capability_min \
+   sizeof(struct efi_tcg2_boot_service_capability) - \
+   offsetof(struct efi_tcg2_boot_service_capability, number_of_pcr_banks)
+
+struct efi_tcg2_protocol {
+ 

[PATCH 1/2 v5] tpm: Add some headers from the spec

2020-11-12 Thread Ilias Apalodimas
A following patch introduces EFI_TCG2_PROTOCOL.
Add the required TPMv2 headers to support it.

Signed-off-by: Ilias Apalodimas 
Reviewed-by: Heinrich Schuchardt 
Reviewed-by: Simon Glass 
---
* changes since v3:
- Add TPM2 prefix on PT_GROUP, PT_FIXED
* changes since v2:
- Added description and pointers to TCG specs
- updated copyright info
 include/tpm-v2.h | 77 
 1 file changed, 77 insertions(+)

diff --git a/include/tpm-v2.h b/include/tpm-v2.h
index f6c045d35480..74c14fe7c51d 100644
--- a/include/tpm-v2.h
+++ b/include/tpm-v2.h
@@ -1,6 +1,13 @@
 /* SPDX-License-Identifier: GPL-2.0+ */
 /*
+ * Defines APIs and structures that allow software to interact with a
+ * TPM2 device
+ *
+ * Copyright (c) 2020 Linaro
  * Copyright (c) 2018 Bootlin
+ *
+ * 
https://trustedcomputinggroup.org/resource/tss-overview-common-structures-specification/
+ *
  * Author: Miquel Raynal 
  */
 
@@ -11,6 +18,74 @@
 
 #define TPM2_DIGEST_LEN32
 
+#define TPM2_MAX_PCRS 32
+#define TPM2_PCR_SELECT_MAX ((TPM2_MAX_PCRS + 7) / 8)
+#define TPM2_MAX_CAP_BUFFER 1024
+#define TPM2_MAX_TPM_PROPERTIES ((TPM2_MAX_CAP_BUFFER - sizeof(u32) /* 
TPM2_CAP */ - \
+sizeof(u32)) / sizeof(struct 
tpms_tagged_property))
+
+/*
+ *  We deviate from this draft of the specification by increasing the value of
+ *  TPM2_NUM_PCR_BANKS from 3 to 16 to ensure compatibility with TPM2
+ *  implementations that have enabled a larger than typical number of PCR
+ *  banks. This larger value for TPM2_NUM_PCR_BANKS is expected to be included
+ *  in a future revision of the specification.
+ */
+#define TPM2_NUM_PCR_BANKS 16
+
+/* Definition of (UINT32) TPM2_CAP Constants */
+#define TPM2_CAP_PCRS 0x0005U
+#define TPM2_CAP_TPM_PROPERTIES 0x0006U
+
+/* Definition of (UINT32) TPM2_PT Constants */
+#define TPM2_PT_GROUP  (u32)(0x0100)
+#define TPM2_PT_FIXED  (u32)(TPM2_PT_GROUP * 1)
+#define TPM2_PT_MANUFACTURER   (u32)(TPM2_PT_FIXED + 5)
+#define TPM2_PT_PCR_COUNT  (u32)(TPM2_PT_FIXED + 18)
+#define TPM2_PT_MAX_COMMAND_SIZE   (u32)(TPM2_PT_FIXED + 30)
+#define TPM2_PT_MAX_RESPONSE_SIZE  (u32)(TPM2_PT_FIXED + 31)
+
+/* TPMS_TAGGED_PROPERTY Structure */
+struct tpms_tagged_property {
+   u32 property;
+   u32 value;
+} __packed;
+
+/* TPMS_PCR_SELECTION Structure */
+struct tpms_pcr_selection {
+   u16 hash;
+   u8 size_of_select;
+   u8 pcr_select[TPM2_PCR_SELECT_MAX];
+} __packed;
+
+/* TPML_PCR_SELECTION Structure */
+struct tpml_pcr_selection {
+   u32 count;
+   struct tpms_pcr_selection selection[TPM2_NUM_PCR_BANKS];
+} __packed;
+
+/* TPML_TAGGED_TPM_PROPERTY Structure */
+struct tpml_tagged_tpm_property {
+   u32 count;
+   struct tpms_tagged_property tpm_property[TPM2_MAX_TPM_PROPERTIES];
+} __packed;
+
+/* TPMU_CAPABILITIES Union */
+union tpmu_capabilities {
+   /*
+* Non exhaustive. Only added the structs needed for our
+* current code
+*/
+   struct tpml_pcr_selection assigned_pcr;
+   struct tpml_tagged_tpm_property tpm_properties;
+} __packed;
+
+/* TPMS_CAPABILITY_DATA Structure */
+struct tpms_capability_data {
+   u32 capability;
+   union tpmu_capabilities data;
+} __packed;
+
 /**
  * TPM2 Structure Tags for command/response buffers.
  *
@@ -123,11 +198,13 @@ enum tpm2_return_codes {
  * TPM2 algorithms.
  */
 enum tpm2_algorithms {
+   TPM2_ALG_SHA1   = 0x04,
TPM2_ALG_XOR= 0x0A,
TPM2_ALG_SHA256 = 0x0B,
TPM2_ALG_SHA384 = 0x0C,
TPM2_ALG_SHA512 = 0x0D,
TPM2_ALG_NULL   = 0x10,
+   TPM2_ALG_SM3_256= 0x12,
 };
 
 /* NV index attributes */
-- 
2.29.2



Secure the environment variables

2020-11-12 Thread Jorge Cisneros
Hi All

  I'm new in this mail list, this is my first post, I have a question is
there any way to protect the variables on the environment file? I search on
the Internet and look like is possible to make the variables read-only or
write-one but is not working for me, maybe I doing something wrong?

My main goal is to make all variables read-only except for one. is this
possible? if, yes, is there any documentation or example?

Thanks.


Re: [PATCH 1/1] efi_selftest: keep devices in ExitBootServices()

2020-11-12 Thread Ilias Apalodimas
On Thu, Nov 12, 2020 at 09:29:21PM +0100, Heinrich Schuchardt wrote:
> When calling ExitBootServices during out unit tests we should not detach
> devices as we need console output for runtime tests.
> 
> Fixes: 529441ca89b1 ("efi_loader: Disable devices before handing over 
> control")
> Signed-off-by: Heinrich Schuchardt 
> ---
>  include/efi_loader.h|  3 +++
>  lib/efi_loader/efi_boottime.c   | 13 +
>  lib/efi_selftest/efi_selftest.c |  3 +++
>  3 files changed, 15 insertions(+), 4 deletions(-)
> 
> diff --git a/include/efi_loader.h b/include/efi_loader.h
> index f550ced568..455b9c8031 100644
> --- a/include/efi_loader.h
> +++ b/include/efi_loader.h
> @@ -59,6 +59,9 @@ extern efi_handle_t efi_root;
>  /* Set to EFI_SUCCESS when initialized */
>  extern efi_status_t efi_obj_list_initialized;
> 
> +/* Flag used by the selftest to avoid detaching devices in 
> ExitBootServices() */
> +extern bool efi_st_keep_devices;
> +
>  /* EFI system partition */
>  extern struct efi_system_partition {
>   enum if_type if_type;
> diff --git a/lib/efi_loader/efi_boottime.c b/lib/efi_loader/efi_boottime.c
> index dfa71b1774..246b59d3b3 100644
> --- a/lib/efi_loader/efi_boottime.c
> +++ b/lib/efi_loader/efi_boottime.c
> @@ -38,6 +38,9 @@ LIST_HEAD(efi_event_queue);
>  /* Flag to disable timer activity in ExitBootServices() */
>  static bool timers_enabled = true;
> 
> +/* Flag used by the selftest to avoid detaching devices in 
> ExitBootServices() */
> +bool efi_st_keep_devices;
> +
>  /* List of all events registered by RegisterProtocolNotify() */
>  LIST_HEAD(efi_register_notify_events);
> 
> @@ -1996,10 +1999,12 @@ static efi_status_t EFIAPI 
> efi_exit_boot_services(efi_handle_t image_handle,
>   list_del(>link);
>   }
> 
> - if IS_ENABLED(CONFIG_USB_DEVICE)
> - udc_disconnect();
> - board_quiesce_devices();
> - dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL);
> + if (!efi_st_keep_devices) {
> + if IS_ENABLED(CONFIG_USB_DEVICE)
> + udc_disconnect();
> + board_quiesce_devices();
> + dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL);
> + }
> 
>   /* Patch out unsupported runtime function */
>   efi_runtime_detach();
> diff --git a/lib/efi_selftest/efi_selftest.c b/lib/efi_selftest/efi_selftest.c
> index 85e819bdfa..b8eed048c2 100644
> --- a/lib/efi_selftest/efi_selftest.c
> +++ b/lib/efi_selftest/efi_selftest.c
> @@ -38,6 +38,9 @@ void efi_st_exit_boot_services(void)
>   efi_status_t ret;
>   struct efi_mem_desc *memory_map;
> 
> + /* Do not detach devices in ExitBootServices. We need the console. */
> + efi_st_keep_devices = true;
> +
>   ret = boottime->get_memory_map(_size, NULL, _key, _size,
>  _version);
>   if (ret != EFI_BUFFER_TOO_SMALL) {
> --
> 2.28.0
> 

Acked-by: Ilias Apalodimas 


Re: [U-Boot] Issue in u-boot; TFTP error: trying to overwrite reserved memory...

2020-11-12 Thread Bernhard Kirchen
Hello Christopher, hello SImon,

today I debugged this problem, as tftp was not working in my u-boot SPL
(AM335x based custom board) for the reasons described in this thread.

It turns out that lmb_init_and_reserve() could not set proper values
as bd->bi_dram[0].size = . The reason IMHO is that
dram_init_banksize() is NOT called in SPL unless CONFIG_SPL_OS_BOOT or
CONFIG_HANDOFF or CONFIG_SPL_ATF is defined (see
common/spl/spl.c:board_init_r(). As I have no use to set any of those,
dram_init_banksize() is never called in my case. Subsequently, tftp fails
when checking available memory.

By calling dram_init_banksize() from my board's am33xx_spl_board_init() I
can work around the issue.

However, I think that dram_init_banksize() should be called in
common/spl/spl.c:board_init_r() also if CONFIG_LBM is defined.

Best,
Bernhard


[PATCH 1/1] efi_selftest: keep devices in ExitBootServices()

2020-11-12 Thread Heinrich Schuchardt
When calling ExitBootServices during out unit tests we should not detach
devices as we need console output for runtime tests.

Fixes: 529441ca89b1 ("efi_loader: Disable devices before handing over control")
Signed-off-by: Heinrich Schuchardt 
---
 include/efi_loader.h|  3 +++
 lib/efi_loader/efi_boottime.c   | 13 +
 lib/efi_selftest/efi_selftest.c |  3 +++
 3 files changed, 15 insertions(+), 4 deletions(-)

diff --git a/include/efi_loader.h b/include/efi_loader.h
index f550ced568..455b9c8031 100644
--- a/include/efi_loader.h
+++ b/include/efi_loader.h
@@ -59,6 +59,9 @@ extern efi_handle_t efi_root;
 /* Set to EFI_SUCCESS when initialized */
 extern efi_status_t efi_obj_list_initialized;

+/* Flag used by the selftest to avoid detaching devices in ExitBootServices() 
*/
+extern bool efi_st_keep_devices;
+
 /* EFI system partition */
 extern struct efi_system_partition {
enum if_type if_type;
diff --git a/lib/efi_loader/efi_boottime.c b/lib/efi_loader/efi_boottime.c
index dfa71b1774..246b59d3b3 100644
--- a/lib/efi_loader/efi_boottime.c
+++ b/lib/efi_loader/efi_boottime.c
@@ -38,6 +38,9 @@ LIST_HEAD(efi_event_queue);
 /* Flag to disable timer activity in ExitBootServices() */
 static bool timers_enabled = true;

+/* Flag used by the selftest to avoid detaching devices in ExitBootServices() 
*/
+bool efi_st_keep_devices;
+
 /* List of all events registered by RegisterProtocolNotify() */
 LIST_HEAD(efi_register_notify_events);

@@ -1996,10 +1999,12 @@ static efi_status_t EFIAPI 
efi_exit_boot_services(efi_handle_t image_handle,
list_del(>link);
}

-   if IS_ENABLED(CONFIG_USB_DEVICE)
-   udc_disconnect();
-   board_quiesce_devices();
-   dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL);
+   if (!efi_st_keep_devices) {
+   if IS_ENABLED(CONFIG_USB_DEVICE)
+   udc_disconnect();
+   board_quiesce_devices();
+   dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL);
+   }

/* Patch out unsupported runtime function */
efi_runtime_detach();
diff --git a/lib/efi_selftest/efi_selftest.c b/lib/efi_selftest/efi_selftest.c
index 85e819bdfa..b8eed048c2 100644
--- a/lib/efi_selftest/efi_selftest.c
+++ b/lib/efi_selftest/efi_selftest.c
@@ -38,6 +38,9 @@ void efi_st_exit_boot_services(void)
efi_status_t ret;
struct efi_mem_desc *memory_map;

+   /* Do not detach devices in ExitBootServices. We need the console. */
+   efi_st_keep_devices = true;
+
ret = boottime->get_memory_map(_size, NULL, _key, _size,
   _version);
if (ret != EFI_BUFFER_TOO_SMALL) {
--
2.28.0



Re: [PATCH v2] env: mmc: Correct partition comparison in mmc_offset_try_partition

2020-11-12 Thread Wolfgang Denk
Dear Hoyeonjiki Kim,

In message <20201112131237.1239-1-jigi@gmail.com> you wrote:
> The function mmc_offset_try_partition searches MMC partition to save the
> environment data by name.  However, it only compares the first word-size
> bytes (size of 'const char *'), which may make the function to find
> unintended partition.
>
> Correct the function not to partially compare the partition name with
> config "u-boot,mmc-env-partition".
>
> Fixes: c9e87ba66540 ("env: Save environment at the end of an MMC partition")
> Signed-off-by: Hoyeonjiki Kim 
> Reviewed-by: Jaehoon Chung 
> ---
>  env/mmc.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/env/mmc.c b/env/mmc.c
> index 4e67180b23..505f7aa2b8 100644
> --- a/env/mmc.c
> +++ b/env/mmc.c
> @@ -42,7 +42,7 @@ static inline int mmc_offset_try_partition(const char *str, 
> int copy, s64 *val)
>   if (ret < 0)
>   return ret;
>  
> - if (!strncmp((const char *)info.name, str, sizeof(str)))
> + if (!strcmp((const char *)info.name, str))

Resend my comment, too.  This looks dangerous, please double check!!

Best regards,

Wolfgang Denk

-- 
DENX Software Engineering GmbH,  Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: w...@denx.de
In Nature there are neither rewards nor punishments, there are conse-
quences.-- R.G. Ingersoll


Re: [PATCH] env: mmc: Correct partition comparison in mmc_offset_try_partition

2020-11-12 Thread Wolfgang Denk
Dear Hoyeonjiki Kim,

In message <20201112123026.458-1-jigi@gmail.com> you wrote:
> The function mmc_offset_try_partition searches MMC partition to save the
> environment data by name.  However, it only compares the first word-size
> bytes (size of 'const char *'), which may make the function to find
> unintended partition.
>
> Correct the function not to partially compare the partition name with
> config "u-boot,mmc-env-partition".
>
> Fixes: c9e87ba66540 ("env: Save environment at the end of an MMC partition")
> Signed-off-by: Hoyeonjiki Kim 
> Reviewed-by: Jaehoon Chung 
> ---
>  env/mmc.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/env/mmc.c b/env/mmc.c
> index 4e67180b23..505f7aa2b8 100644
> --- a/env/mmc.c
> +++ b/env/mmc.c
> @@ -42,7 +42,7 @@ static inline int mmc_offset_try_partition(const char *str, 
> int copy, s64 *val)
>   if (ret < 0)
>   return ret;
>  
> - if (!strncmp((const char *)info.name, str, sizeof(str)))
> + if (!strcmp((const char *)info.name, str))

This looks dangerous to me.  If the used length  sizeof(str) is not
correct, that should be fixed, but switching to strcmp() means we
may run into problems if the buffer is not NUL terminated.  Do we
have a guarantee here thai it always ist??  The fact that strncmp()
has been used before suggests otyherwise.

Please double-check.

Best regards,

Wolfgang Denk

-- 
DENX Software Engineering GmbH,  Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: w...@denx.de
Computers are not intelligent. They only think they are.


Re: [PATCH] env: mmc: Correct partition comparison in mmc_offset_try_partition

2020-11-12 Thread Wolfgang Denk
Dear Jaehoon Chung,

In message <21adc771-9660-da52-65c8-c2029de9a...@samsung.com> you wrote:
> On 11/10/20 11:28 PM, Hoyeonjiki Kim wrote:
> > The function mmc_offset_try_partition searches MMC partition to save the
> > environment data by name.  However, it only compares the first word-size
> > bytes (size of 'const char *'), which may make the function to find
> > unintended partition.
> > 
> > Correct the function not to partially compare the partition name with
> > config "u-boot,,mmc-env-partition".
> > 
> > Signed-off-by: Hoyeonjiki Kim 
> > ---
> >  env/mmc.c | 2 +-
> >  1 file changed, 1 insertion(+), 1 deletion(-)
> > 
> > diff --git a/env/mmc.c b/env/mmc.c
> > index 4e67180b23..505f7aa2b8 100644
> > --- a/env/mmc.c
> > +++ b/env/mmc.c
> > @@ -42,7 +42,7 @@ static inline int mmc_offset_try_partition(const char 
> > *str, int copy, s64 *val)
> > if (ret < 0)
> > return ret;
> >  
> > -   if (!strncmp((const char *)info.name, str, sizeof(str)))
> > +   if (!strcmp((const char *)info.name, str))
>
> Using "strlen(str)" is better than changing to strcmp.
>
> strncmp(..., ..., strlen(str))

Is either of this a good idea? I mean, if you pass in random data,
this will run forever and eventually create undefined behaviour. We
know the maximum size, so why not limit it to that, as strncmp() did?

Best regards,

Wolfgang Denk

-- 
DENX Software Engineering GmbH,  Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: w...@denx.de
"To IBM, 'open' means there is a modicum  of  interoperability  among
some of their equipment."- Harv Masterson


Re: [PATCH 4/5] env: allow default environment to be amended from control dtb

2020-11-12 Thread Wolfgang Denk
Dear Rasmus Villemoes,

In message <20201110202603.20944-5-rasmus.villem...@prevas.dk> you wrote:
> It can be useful to use the same U-Boot binary for multiple purposes,
> say the normal one, one for developers that allow breaking into the
> U-Boot shell, and one for use during bootstrapping which runs a
> special-purpose bootcmd. To that end, allow the control dtb to contain
> a /config/default-enviroment property, whose value will be used to
> amend the default environment baked into the U-Boot binary itself.

No, this is not what should be done.

Please try to get used to the idea behind the so called "default
environment".  Only now I realize that this was a badly chosen name,
but last_resort_in_case_of_emergencies_environment would have had
other problems.

The default environment is something which is NOT INTENDED for
regular use.  it is what you will fall back to in case (and ONLY in
that case) when your regular persistent environment cannot be used,
for example because it is not readable (I/O errors or such) or not
properly initialized or corrupted (CRC checksum error).

It is not the intended use but still somewhat acceptable to use it
as initial data to populate the regular environment in other cases,
too.  But that's it.

Apending data to it is not acceptable.  If you need to append data,
then only to the regular environment.


And please, for the sake of avoiding further confusiion, please do
not name this "default-environment".

Thanks.

Best regards,

Wolfgang Denk

-- 
DENX Software Engineering GmbH,  Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: w...@denx.de
Real Programmers always confuse Christmas and Halloween because
OCT 31 == DEC 25 !  - Andrew Rutherford (andr...@ucs.adelaide.edu.au)


Re: [PATCH 3/5] env: make env_set_default_vars() return void

2020-11-12 Thread Wolfgang Denk
Dear Rasmus,

In message <20201110202603.20944-4-rasmus.villem...@prevas.dk> you wrote:
> Unlike most other functions declared in env.h, the return value of
> this one also isn't documented. It only has a single caller, which
> ignores the return value. And the companion, env_set_default(),
> already has void as return type.
>
> Signed-off-by: Rasmus Villemoes 

Naked-by: Wolfgang Denk 

This is not a good idea.  himport_r() can run into problems, and
then it returns an error code. Ignoring errors is never correct.

Instead, the caller should be fixed to add proper error handling.

env_set_default() at least prints a proper error message.

Best regards,

Wolfgang Denk

-- 
DENX Software Engineering GmbH,  Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: w...@denx.de
Don't have a battle of wits with an unarmed opponent.


Re: [PATCH 2/2 v4] efi: Add basic EFI_TCG2_PROTOCOL support

2020-11-12 Thread Ilias Apalodimas
On Thu, 12 Nov 2020 at 21:22, Heinrich Schuchardt 
wrote:

> On 11/12/20 8:10 PM, Ilias Apalodimas wrote:
> > Hi Heinrich,
> >
> >
> > [...]
> >>> +   return EFI_SUCCESS;
> >>> +
> >>> +   tpm_ver = tpm_get_version(dev);
> >>> +   if (tpm_ver != TPM_V2) {
> >>> +   log_warning("Only TPMv2 supported for EFI_TCG2_PROTOCOL");
> >>
> >> The message should end with \n.
> >>
> >> This message becomes superfluous if you correct
> platform_get_tpm2_device().
> >>
> >
> > platform_get_tpm2_device() is used in EFI calls, won't that break
> printing from
> > EFI apps?
>
> If platform_get_tpm2_device() only return TPM2v2 devices, you don't need
> to check the type and hence you won't need the log_warning() above.
>

Well you still need to warn the user that only TPMv2 devices are supported
no?
Hence my question on what's the best way to do that.


> Regards
>
> Heinrich
>
> >
> >>> +   return EFI_SUCCESS;
> >>> +   }
> >>> +
> >>> +   ret = efi_add_protocol(efi_root, _guid_tcg2_protocol,
> >>> +  (void *)_tcg2_protocol);
> >>> +   if (ret != EFI_SUCCESS)
> >>> +   log_err("Cannot install EFI_TCG2_PROTOCOL");
> >>
> >> The message should end with \n.
> >>
> >> Best regards
> >>
> >> Heinrich
> >>
> >>> +
> >>> +   return ret;
> >>> +}
> >>>
> >>
> >
> > Cheers
> > /Ilias
> >
>
>


Re: [PATCH 2/2 v4] efi: Add basic EFI_TCG2_PROTOCOL support

2020-11-12 Thread Heinrich Schuchardt
On 11/11/20 10:18 AM, Ilias Apalodimas wrote:
> Since U-boot EFI implementation is getting richer it makes sense to
> add support for EFI_TCG2_PROTOCOL taking advantage of any hardware TPM
> available on the device.
>
> This is the initial implementation of the protocol which only adds
> support for GetCapability(). It's limited in the newer and safer
> TPMv2 devices.
>
> Signed-off-by: Ilias Apalodimas 
> ---
> * changes since v3:
> - added check for maximum number of PCRs allowed
> - replaced multiple return Xl with goto out tags
> * changes since v2:
> - added description about include/efi_tcg2.h
> - switch bool to u8 for tpm_present_flag
> - removed superfluous 'default n' from Kconfig
> - use 'goto 'tag' when possible
>
> * changes since v1:
> - change return variable of platform_get_tpm2_device() when used
> - since more headers were included in patch #2 use them in offset
>   calculations for all tpm commands
> - change the size of the response buffer regardless of what
>   tpm2_get_capability() is doing
>  include/efi_loader.h   |   2 +
>  include/efi_tcg2.h |  94 +++
>  lib/efi_loader/Kconfig |   7 +
>  lib/efi_loader/Makefile|   1 +
>  lib/efi_loader/efi_setup.c |   7 +
>  lib/efi_loader/efi_tcg2.c  | 539 +
>  6 files changed, 650 insertions(+)
>  create mode 100644 include/efi_tcg2.h
>  create mode 100644 lib/efi_loader/efi_tcg2.c
>
> diff --git a/include/efi_loader.h b/include/efi_loader.h
> index f550ced56876..e5015d865ec9 100644
> --- a/include/efi_loader.h
> +++ b/include/efi_loader.h
> @@ -405,6 +405,8 @@ efi_status_t efi_console_register(void);
>  efi_status_t efi_disk_register(void);
>  /* Called by efi_init_obj_list() to install EFI_RNG_PROTOCOL */
>  efi_status_t efi_rng_register(void);
> +/* Called by efi_init_obj_list() to install EFI_TCG2_PROTOCOL */
> +efi_status_t efi_tcg2_register(void);
>  /* Create handles and protocols for the partitions of a block device */
>  int efi_disk_create_partitions(efi_handle_t parent, struct blk_desc *desc,
>  const char *if_typename, int diskid,
> diff --git a/include/efi_tcg2.h b/include/efi_tcg2.h
> new file mode 100644
> index ..4214f767eaba
> --- /dev/null
> +++ b/include/efi_tcg2.h
> @@ -0,0 +1,94 @@
> +/* SPDX-License-Identifier: GPL-2.0+ */
> +/*
> + * Defines data structures and APIs that allow an OS to interact with UEFI
> + * firmware to query information about the device
> + *
> + * Copyright (c) 2020, Linaro Limited
> + */
> +
> +#if !defined _EFI_TCG2_PROTOCOL_H_
> +#define _EFI_TCG2_PROTOCOL_H_
> +
> +#include 
> +
> +#define EFI_TCG2_PROTOCOL_GUID \
> + EFI_GUID(0x607f766c, 0x7455, 0x42be, 0x93, \
> +  0x0b, 0xe4, 0xd7, 0x6d, 0xb2, 0x72, 0x0f)
> +
> +/* TPMV2 only */
> +#define TCG2_EVENT_LOG_FORMAT_TCG_2 0x0002
> +
> +/* SHA1, SHA256, SHA384, SHA512, TPM_ALG_SM3_256 */
> +#define MAX_HASH_COUNT 5
> +/* Algorithm Registry */
> +#define EFI_TCG2_BOOT_HASH_ALG_SHA10x0001
> +#define EFI_TCG2_BOOT_HASH_ALG_SHA256  0x0002
> +#define EFI_TCG2_BOOT_HASH_ALG_SHA384  0x0004
> +#define EFI_TCG2_BOOT_HASH_ALG_SHA512  0x0008
> +#define EFI_TCG2_BOOT_HASH_ALG_SM3_256 0x0010
> +
> +typedef u32 efi_tcg_event_log_bitmap;
> +typedef u32 efi_tcg_event_log_format;
> +typedef u32 efi_tcg_event_algorithm_bitmap;
> +
> +struct efi_tcg2_version {
> + u8 major;
> + u8 minor;
> +};
> +
> +struct efi_tcg2_event_header {
> + u32 header_size;
> + u16 header_version;
> + u32 pcr_index;
> + u32 event_type;
> +} __packed;
> +
> +struct efi_tcg2_event {
> + u32 size;
> + struct efi_tcg2_event_header header;
> + u8 event[];
> +} __packed;
> +
> +struct efi_tcg2_boot_service_capability {
> + u8 size;
> + struct efi_tcg2_version structure_version;
> + struct efi_tcg2_version protocol_version;
> + efi_tcg_event_algorithm_bitmap hash_algorithm_bitmap;
> + efi_tcg_event_log_bitmap supported_event_logs;
> + u8 tpm_present_flag;
> + u16 max_command_size;
> + u16 max_response_size;
> + u32 manufacturer_id;
> + u32 number_of_pcr_banks;
> + efi_tcg_event_algorithm_bitmap active_pcr_banks;
> +};
> +
> +#define boot_service_capability_min \
> + sizeof(struct efi_tcg2_boot_service_capability) - \
> + offsetof(struct efi_tcg2_boot_service_capability, number_of_pcr_banks)
> +
> +struct efi_tcg2_protocol {
> + efi_status_t (EFIAPI * get_capability)(struct efi_tcg2_protocol *this,
> +struct 
> efi_tcg2_boot_service_capability *capability);
> + efi_status_t (EFIAPI * get_eventlog)(struct efi_tcg2_protocol *this,
> +  efi_tcg_event_log_format 
> log_format,
> +  u64 *event_log_location, u64 
> *event_log_last_entry,
> +  bool *event_log_truncated);
> + efi_status_t (EFIAPI * 

Re: [PATCH 2/2 v4] efi: Add basic EFI_TCG2_PROTOCOL support

2020-11-12 Thread Heinrich Schuchardt
On 11/12/20 8:10 PM, Ilias Apalodimas wrote:
> Hi Heinrich,
>
>
> [...]
>>> +   return EFI_SUCCESS;
>>> +
>>> +   tpm_ver = tpm_get_version(dev);
>>> +   if (tpm_ver != TPM_V2) {
>>> +   log_warning("Only TPMv2 supported for EFI_TCG2_PROTOCOL");
>>
>> The message should end with \n.
>>
>> This message becomes superfluous if you correct platform_get_tpm2_device().
>>
>
> platform_get_tpm2_device() is used in EFI calls, won't that break printing 
> from
> EFI apps?

If platform_get_tpm2_device() only return TPM2v2 devices, you don't need
to check the type and hence you won't need the log_warning() above.

Regards

Heinrich

>
>>> +   return EFI_SUCCESS;
>>> +   }
>>> +
>>> +   ret = efi_add_protocol(efi_root, _guid_tcg2_protocol,
>>> +  (void *)_tcg2_protocol);
>>> +   if (ret != EFI_SUCCESS)
>>> +   log_err("Cannot install EFI_TCG2_PROTOCOL");
>>
>> The message should end with \n.
>>
>> Best regards
>>
>> Heinrich
>>
>>> +
>>> +   return ret;
>>> +}
>>>
>>
>
> Cheers
> /Ilias
>



Re: [PATCH 2/2 v4] efi: Add basic EFI_TCG2_PROTOCOL support

2020-11-12 Thread Heinrich Schuchardt
On 11/12/20 7:49 PM, Heinrich Schuchardt wrote:
> On 11/11/20 10:18 AM, Ilias Apalodimas wrote:
>> Since U-boot EFI implementation is getting richer it makes sense to
>> add support for EFI_TCG2_PROTOCOL taking advantage of any hardware TPM
>> available on the device.
>>
>> This is the initial implementation of the protocol which only adds
>> support for GetCapability(). It's limited in the newer and safer
>> TPMv2 devices.
>>
>> Signed-off-by: Ilias Apalodimas 
>> ---
>> * changes since v3:
>> - added check for maximum number of PCRs allowed
>> - replaced multiple return Xl with goto out tags
>> * changes since v2:
>> - added description about include/efi_tcg2.h
>> - switch bool to u8 for tpm_present_flag
>> - removed superfluous 'default n' from Kconfig
>> - use 'goto 'tag' when possible
>>
>> * changes since v1:
>> - change return variable of platform_get_tpm2_device() when used
>> - since more headers were included in patch #2 use them in offset
>>   calculations for all tpm commands
>> - change the size of the response buffer regardless of what
>>   tpm2_get_capability() is doing
>>  include/efi_loader.h   |   2 +
>>  include/efi_tcg2.h |  94 +++
>>  lib/efi_loader/Kconfig |   7 +
>>  lib/efi_loader/Makefile|   1 +
>>  lib/efi_loader/efi_setup.c |   7 +
>>  lib/efi_loader/efi_tcg2.c  | 539 +
>>  6 files changed, 650 insertions(+)
>>  create mode 100644 include/efi_tcg2.h
>>  create mode 100644 lib/efi_loader/efi_tcg2.c
>>
>> diff --git a/include/efi_loader.h b/include/efi_loader.h
>> index f550ced56876..e5015d865ec9 100644
>> --- a/include/efi_loader.h
>> +++ b/include/efi_loader.h
>> @@ -405,6 +405,8 @@ efi_status_t efi_console_register(void);
>>  efi_status_t efi_disk_register(void);
>>  /* Called by efi_init_obj_list() to install EFI_RNG_PROTOCOL */
>>  efi_status_t efi_rng_register(void);
>> +/* Called by efi_init_obj_list() to install EFI_TCG2_PROTOCOL */
>> +efi_status_t efi_tcg2_register(void);
>>  /* Create handles and protocols for the partitions of a block device */
>>  int efi_disk_create_partitions(efi_handle_t parent, struct blk_desc *desc,
>> const char *if_typename, int diskid,
>> diff --git a/include/efi_tcg2.h b/include/efi_tcg2.h
>> new file mode 100644
>> index ..4214f767eaba
>> --- /dev/null
>> +++ b/include/efi_tcg2.h
>> @@ -0,0 +1,94 @@
>> +/* SPDX-License-Identifier: GPL-2.0+ */
>> +/*
>> + * Defines data structures and APIs that allow an OS to interact with UEFI
>> + * firmware to query information about the device
>> + *
>> + * Copyright (c) 2020, Linaro Limited
>> + */
>> +
>> +#if !defined _EFI_TCG2_PROTOCOL_H_
>> +#define _EFI_TCG2_PROTOCOL_H_
>> +
>> +#include 
>> +
>> +#define EFI_TCG2_PROTOCOL_GUID \
>> +EFI_GUID(0x607f766c, 0x7455, 0x42be, 0x93, \
>> + 0x0b, 0xe4, 0xd7, 0x6d, 0xb2, 0x72, 0x0f)
>> +
>> +/* TPMV2 only */
>> +#define TCG2_EVENT_LOG_FORMAT_TCG_2 0x0002
>> +
>> +/* SHA1, SHA256, SHA384, SHA512, TPM_ALG_SM3_256 */
>> +#define MAX_HASH_COUNT 5
>> +/* Algorithm Registry */
>> +#define EFI_TCG2_BOOT_HASH_ALG_SHA10x0001
>> +#define EFI_TCG2_BOOT_HASH_ALG_SHA256  0x0002
>> +#define EFI_TCG2_BOOT_HASH_ALG_SHA384  0x0004
>> +#define EFI_TCG2_BOOT_HASH_ALG_SHA512  0x0008
>> +#define EFI_TCG2_BOOT_HASH_ALG_SM3_256 0x0010
>> +
>> +typedef u32 efi_tcg_event_log_bitmap;
>> +typedef u32 efi_tcg_event_log_format;
>> +typedef u32 efi_tcg_event_algorithm_bitmap;
>> +
>> +struct efi_tcg2_version {
>> +u8 major;
>> +u8 minor;
>> +};
>> +
>> +struct efi_tcg2_event_header {
>> +u32 header_size;
>> +u16 header_version;
>> +u32 pcr_index;
>> +u32 event_type;
>> +} __packed;
>> +
>> +struct efi_tcg2_event {
>> +u32 size;
>> +struct efi_tcg2_event_header header;
>> +u8 event[];
>> +} __packed;
>> +
>> +struct efi_tcg2_boot_service_capability {
>> +u8 size;
>> +struct efi_tcg2_version structure_version;
>> +struct efi_tcg2_version protocol_version;
>> +efi_tcg_event_algorithm_bitmap hash_algorithm_bitmap;
>> +efi_tcg_event_log_bitmap supported_event_logs;
>> +u8 tpm_present_flag;
>> +u16 max_command_size;
>> +u16 max_response_size;
>> +u32 manufacturer_id;
>> +u32 number_of_pcr_banks;
>> +efi_tcg_event_algorithm_bitmap active_pcr_banks;
>> +};
>> +
>> +#define boot_service_capability_min \
>> +sizeof(struct efi_tcg2_boot_service_capability) - \
>> +offsetof(struct efi_tcg2_boot_service_capability, number_of_pcr_banks)
>> +
>> +struct efi_tcg2_protocol {
>> +efi_status_t (EFIAPI * get_capability)(struct efi_tcg2_protocol *this,
>> +   struct 
>> efi_tcg2_boot_service_capability *capability);
>> +efi_status_t (EFIAPI * get_eventlog)(struct efi_tcg2_protocol *this,
>> + efi_tcg_event_log_format 
>> log_format,
>> + u64 

Re: [PATCH 2/2 v4] efi: Add basic EFI_TCG2_PROTOCOL support

2020-11-12 Thread Ilias Apalodimas
Hi Heinrich, 


[...]
> > +   return EFI_SUCCESS;
> > +
> > +   tpm_ver = tpm_get_version(dev);
> > +   if (tpm_ver != TPM_V2) {
> > +   log_warning("Only TPMv2 supported for EFI_TCG2_PROTOCOL");
> 
> The message should end with \n.
> 
> This message becomes superfluous if you correct platform_get_tpm2_device().
> 

platform_get_tpm2_device() is used in EFI calls, won't that break printing from 
EFI apps?

> > +   return EFI_SUCCESS;
> > +   }
> > +
> > +   ret = efi_add_protocol(efi_root, _guid_tcg2_protocol,
> > +  (void *)_tcg2_protocol);
> > +   if (ret != EFI_SUCCESS)
> > +   log_err("Cannot install EFI_TCG2_PROTOCOL");
> 
> The message should end with \n.
> 
> Best regards
> 
> Heinrich
> 
> > +
> > +   return ret;
> > +}
> >
> 

Cheers
/Ilias


RE: [PATCH v3 4/4] dm: soc: SoC identification driver for Renesas SoC's

2020-11-12 Thread Biju Das
Hi All,

I need to update copyright information for this patch. This work is derived 
from linux
and linux  has extra copyright information. I have missed that.
 
Will send V4 for fixing it.

Regards,
Biju

> -Original Message-
> From: Biju Das 
> Sent: 04 November 2020 10:56
> To: Simon Glass ; Marek Vasut
> 
> Cc: Biju Das ; Dave Gerlach  gerl...@ti.com>; Prabhakar Mahadev Lad  lad...@bp.renesas.com>; u-boot@lists.denx.de; Nobuhiro Iwamatsu
> ; Chris Paterson 
> Subject: [PATCH v3 4/4] dm: soc: SoC identification driver for Renesas
> SoC's
> 
> Add SoC identification driver for Renesas SoC's. This allows to identify
> the SoC type and revision based on Product Register.
> 
> This can be checked where needed using soc_device_match().
> 
> Signed-off-by: Biju Das 
> Reviewed-by: Lad Prabhakar 
> ---
> v2->v3: No Change.
> v2: New patch
> ---
>  drivers/soc/Kconfig   |   7 ++
>  drivers/soc/Makefile  |   1 +
>  drivers/soc/soc_renesas.c | 239 ++
>  3 files changed, 247 insertions(+)
>  create mode 100644 drivers/soc/soc_renesas.c
> 
> diff --git a/drivers/soc/Kconfig b/drivers/soc/Kconfig index
> 864d00a885..475e94cd77 100644
> --- a/drivers/soc/Kconfig
> +++ b/drivers/soc/Kconfig
> @@ -16,6 +16,13 @@ config SOC_DEVICE_TI_K3
> This allows Texas Instruments Keystone 3 SoCs to identify
> specifics about the SoC in use.
> 
> +config SOC_DEVICE_RENESAS
> + depends on SOC_DEVICE
> + bool "Enable SoC driver for Renesas SoCs"
> + help
> +   This allows Renesas SoCs to identify specifics about the
> +   SoC in use.
> +
>  source "drivers/soc/ti/Kconfig"
> 
>  endmenu
> diff --git a/drivers/soc/Makefile b/drivers/soc/Makefile index
> 9ef20ca506..b143eac5fd 100644
> --- a/drivers/soc/Makefile
> +++ b/drivers/soc/Makefile
> @@ -4,5 +4,6 @@
> 
>  obj-$(CONFIG_SOC_TI) += ti/
>  obj-$(CONFIG_SOC_DEVICE) += soc-uclass.o
> +obj-$(CONFIG_$(SPL_)SOC_DEVICE_RENESAS) += soc_renesas.o
>  obj-$(CONFIG_SOC_DEVICE_TI_K3) += soc_ti_k3.o
>  obj-$(CONFIG_SANDBOX) += soc_sandbox.o
> diff --git a/drivers/soc/soc_renesas.c b/drivers/soc/soc_renesas.c new
> file mode 100644 index 00..4543e0dbfd
> --- /dev/null
> +++ b/drivers/soc/soc_renesas.c
> @@ -0,0 +1,239 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/*
> + * Copyright (C) 2020 Renesas Electronics Corp.
> + *
> + */
> +
> +#include 
> +#include 
> +#include 
> +#include 
> +
> +#include 
> +
> +struct soc_renesas_priv {
> + const char *family;
> + const char *soc_id;
> + char revision[6];
> +};
> +
> +struct renesas_family {
> + const char name[16];
> + u32 reg;/* CCCR or PRR, if not in DT */
> +};
> +
> +static const struct renesas_family fam_rcar_gen3 __maybe_unused = {
> + .name   = "R-Car Gen3",
> + .reg= 0xfff00044,   /* PRR (Product Register) */
> +};
> +
> +static const struct renesas_family fam_rzg2 __maybe_unused = {
> + .name   = "RZ/G2",
> + .reg= 0xfff00044,   /* PRR (Product Register) */
> +};
> +
> +struct renesas_soc {
> + const struct renesas_family *family;
> + u8 id;
> +};
> +
> +#ifdef CONFIG_R8A774A1
> +static const struct renesas_soc soc_rz_g2m = {
> + .family = _rzg2,
> + .id = 0x52,
> +};
> +#endif
> +
> +#ifdef CONFIG_R8A774B1
> +static const struct renesas_soc soc_rz_g2n = {
> + .family = _rzg2,
> + .id = 0x55,
> +};
> +#endif
> +
> +#ifdef CONFIG_R8A774C0
> +static const struct renesas_soc soc_rz_g2e = {
> + .family = _rzg2,
> + .id = 0x57,
> +};
> +#endif
> +
> +#ifdef CONFIG_R8A774E1
> +static const struct renesas_soc soc_rz_g2h = {
> + .family = _rzg2,
> + .id = 0x4f,
> +};
> +#endif
> +
> +#ifdef CONFIG_R8A7795
> +static const struct renesas_soc soc_rcar_h3 = {
> + .family = _rcar_gen3,
> + .id = 0x4f,
> +};
> +#endif
> +
> +#ifdef CONFIG_R8A7796
> +static const struct renesas_soc soc_rcar_m3_w = {
> + .family = _rcar_gen3,
> + .id = 0x52,
> +};
> +#endif
> +
> +#ifdef CONFIG_R8A77965
> +static const struct renesas_soc soc_rcar_m3_n = {
> + .family = _rcar_gen3,
> + .id = 0x55,
> +};
> +#endif
> +
> +#ifdef CONFIG_R8A77970
> +static const struct renesas_soc soc_rcar_v3m = {
> + .family = _rcar_gen3,
> + .id = 0x54,
> +};
> +#endif
> +
> +#ifdef CONFIG_R8A77980
> +static const struct renesas_soc soc_rcar_v3h = {
> + .family = _rcar_gen3,
> + .id = 0x56,
> +};
> +#endif
> +
> +#ifdef CONFIG_R8A77990
> +static const struct renesas_soc soc_rcar_e3 = {
> + .family = _rcar_gen3,
> + .id = 0x57,
> +};
> +#endif
> +
> +#ifdef CONFIG_R8A77995
> +static const struct renesas_soc soc_rcar_d3 = {
> + .family = _rcar_gen3,
> + .id = 0x58,
> +};
> +#endif
> +
> +static int soc_renesas_get_family(struct udevice *dev, char *buf, int
> +size) {
> + struct soc_renesas_priv *priv = dev_get_priv(dev);
> +
> + snprintf(buf, size, "%s", priv->family);
> 

Re: [PATCH 2/2 v4] efi: Add basic EFI_TCG2_PROTOCOL support

2020-11-12 Thread Heinrich Schuchardt
On 11/11/20 10:18 AM, Ilias Apalodimas wrote:
> Since U-boot EFI implementation is getting richer it makes sense to
> add support for EFI_TCG2_PROTOCOL taking advantage of any hardware TPM
> available on the device.
>
> This is the initial implementation of the protocol which only adds
> support for GetCapability(). It's limited in the newer and safer
> TPMv2 devices.
>
> Signed-off-by: Ilias Apalodimas 
> ---
> * changes since v3:
> - added check for maximum number of PCRs allowed
> - replaced multiple return Xl with goto out tags
> * changes since v2:
> - added description about include/efi_tcg2.h
> - switch bool to u8 for tpm_present_flag
> - removed superfluous 'default n' from Kconfig
> - use 'goto 'tag' when possible
>
> * changes since v1:
> - change return variable of platform_get_tpm2_device() when used
> - since more headers were included in patch #2 use them in offset
>   calculations for all tpm commands
> - change the size of the response buffer regardless of what
>   tpm2_get_capability() is doing
>  include/efi_loader.h   |   2 +
>  include/efi_tcg2.h |  94 +++
>  lib/efi_loader/Kconfig |   7 +
>  lib/efi_loader/Makefile|   1 +
>  lib/efi_loader/efi_setup.c |   7 +
>  lib/efi_loader/efi_tcg2.c  | 539 +
>  6 files changed, 650 insertions(+)
>  create mode 100644 include/efi_tcg2.h
>  create mode 100644 lib/efi_loader/efi_tcg2.c
>
> diff --git a/include/efi_loader.h b/include/efi_loader.h
> index f550ced56876..e5015d865ec9 100644
> --- a/include/efi_loader.h
> +++ b/include/efi_loader.h
> @@ -405,6 +405,8 @@ efi_status_t efi_console_register(void);
>  efi_status_t efi_disk_register(void);
>  /* Called by efi_init_obj_list() to install EFI_RNG_PROTOCOL */
>  efi_status_t efi_rng_register(void);
> +/* Called by efi_init_obj_list() to install EFI_TCG2_PROTOCOL */
> +efi_status_t efi_tcg2_register(void);
>  /* Create handles and protocols for the partitions of a block device */
>  int efi_disk_create_partitions(efi_handle_t parent, struct blk_desc *desc,
>  const char *if_typename, int diskid,
> diff --git a/include/efi_tcg2.h b/include/efi_tcg2.h
> new file mode 100644
> index ..4214f767eaba
> --- /dev/null
> +++ b/include/efi_tcg2.h
> @@ -0,0 +1,94 @@
> +/* SPDX-License-Identifier: GPL-2.0+ */
> +/*
> + * Defines data structures and APIs that allow an OS to interact with UEFI
> + * firmware to query information about the device
> + *
> + * Copyright (c) 2020, Linaro Limited
> + */
> +
> +#if !defined _EFI_TCG2_PROTOCOL_H_
> +#define _EFI_TCG2_PROTOCOL_H_
> +
> +#include 
> +
> +#define EFI_TCG2_PROTOCOL_GUID \
> + EFI_GUID(0x607f766c, 0x7455, 0x42be, 0x93, \
> +  0x0b, 0xe4, 0xd7, 0x6d, 0xb2, 0x72, 0x0f)
> +
> +/* TPMV2 only */
> +#define TCG2_EVENT_LOG_FORMAT_TCG_2 0x0002
> +
> +/* SHA1, SHA256, SHA384, SHA512, TPM_ALG_SM3_256 */
> +#define MAX_HASH_COUNT 5
> +/* Algorithm Registry */
> +#define EFI_TCG2_BOOT_HASH_ALG_SHA10x0001
> +#define EFI_TCG2_BOOT_HASH_ALG_SHA256  0x0002
> +#define EFI_TCG2_BOOT_HASH_ALG_SHA384  0x0004
> +#define EFI_TCG2_BOOT_HASH_ALG_SHA512  0x0008
> +#define EFI_TCG2_BOOT_HASH_ALG_SM3_256 0x0010
> +
> +typedef u32 efi_tcg_event_log_bitmap;
> +typedef u32 efi_tcg_event_log_format;
> +typedef u32 efi_tcg_event_algorithm_bitmap;
> +
> +struct efi_tcg2_version {
> + u8 major;
> + u8 minor;
> +};
> +
> +struct efi_tcg2_event_header {
> + u32 header_size;
> + u16 header_version;
> + u32 pcr_index;
> + u32 event_type;
> +} __packed;
> +
> +struct efi_tcg2_event {
> + u32 size;
> + struct efi_tcg2_event_header header;
> + u8 event[];
> +} __packed;
> +
> +struct efi_tcg2_boot_service_capability {
> + u8 size;
> + struct efi_tcg2_version structure_version;
> + struct efi_tcg2_version protocol_version;
> + efi_tcg_event_algorithm_bitmap hash_algorithm_bitmap;
> + efi_tcg_event_log_bitmap supported_event_logs;
> + u8 tpm_present_flag;
> + u16 max_command_size;
> + u16 max_response_size;
> + u32 manufacturer_id;
> + u32 number_of_pcr_banks;
> + efi_tcg_event_algorithm_bitmap active_pcr_banks;
> +};
> +
> +#define boot_service_capability_min \
> + sizeof(struct efi_tcg2_boot_service_capability) - \
> + offsetof(struct efi_tcg2_boot_service_capability, number_of_pcr_banks)
> +
> +struct efi_tcg2_protocol {
> + efi_status_t (EFIAPI * get_capability)(struct efi_tcg2_protocol *this,
> +struct 
> efi_tcg2_boot_service_capability *capability);
> + efi_status_t (EFIAPI * get_eventlog)(struct efi_tcg2_protocol *this,
> +  efi_tcg_event_log_format 
> log_format,
> +  u64 *event_log_location, u64 
> *event_log_last_entry,
> +  bool *event_log_truncated);
> + efi_status_t (EFIAPI * 

[PATCH v2] arm: vexpress: don't reset flags in board_init for vexpress boards

2020-11-12 Thread Arnaud Aujon Chevallier
Re-submitted because of missing description and signed-off

flags reset in board_init caused bugs when executing command like editenv
because the reallocated flag was lost.

Signed-off-by: Arnaud Aujon Chevallier 




[PATCH] vexpress: don't reset flags in board_init to avoid losing previous ones

2020-11-12 Thread Arnaud Aujon Chevallier
---
 board/armltd/vexpress/vexpress_common.c | 1 -
 1 file changed, 1 deletion(-)

diff --git a/board/armltd/vexpress/vexpress_common.c 
b/board/armltd/vexpress/vexpress_common.c
index 70f6cd80d5..8fea8ff352 100644
--- a/board/armltd/vexpress/vexpress_common.c
+++ b/board/armltd/vexpress/vexpress_common.c
@@ -55,7 +55,6 @@ int board_init(void)
 {
gd->bd->bi_boot_params = LINUX_BOOT_PARAM_ADDR;
gd->bd->bi_arch_number = MACH_TYPE_VEXPRESS;
-   gd->flags = 0;
 
icache_enable();
flash__init();
-- 
2.26.2



RE: [PATCH v3 1/4] dm: core: Add of_match_node helper function

2020-11-12 Thread Biju Das
Hi Simon,

Gentle ping. Are you happy with this patch?


Regards,
Biju

> -Original Message-
> From: Biju Das 
> Sent: 04 November 2020 10:56
> To: Simon Glass 
> Cc: Biju Das ; u-boot@lists.denx.de; Marek
> Vasut ; Nobuhiro Iwamatsu
> ; Chris Paterson ;
> Prabhakar Mahadev Lad 
> Subject: [PATCH v3 1/4] dm: core: Add of_match_node helper function
> 
> Add of_match_node() helper function to iterate over the device tree and
> tell if a device_node has a matching of_match structure.
> 
> Signed-off-by: Biju Das 
> Reviewed-by: Lad Prabhakar 
> ---
> v2->v3:
>  * Added a test case for of_match_node helper function.
>  (Ref:
> https://jpn01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fpatchwor
> k.ozlabs.org%2Fproject%2Fuboot%2Fpatch%2F20201102150959.4793-2-
> biju.das.jz%40bp.renesas.com%2Fdata=04%7C01%7Cbiju.das.jz%40bp.renesa
> s.com%7C1ce4430428ba43c09d5808d880b0454e%7C53d82571da1947e49cb4625a166a4a2
> a%7C0%7C0%7C637400841846720504%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDA
> iLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000sdata=zrG4RLHHr
> MYhbDwohsqNoctNZLnoMK5ZPvieFergq2A%3Dreserved=0)
> v1->v2:
>   * No Change.
> ---
>  drivers/core/device.c | 21 +
>  include/dm/device.h   | 13 +
>  test/dm/core.c| 31 +++
>  3 files changed, 65 insertions(+)
> 
> diff --git a/drivers/core/device.c b/drivers/core/device.c index
> 4b3dcb3b37..5db4c5e78b 100644
> --- a/drivers/core/device.c
> +++ b/drivers/core/device.c
> @@ -1010,6 +1010,27 @@ bool of_machine_is_compatible(const char *compat)
>   return !fdt_node_check_compatible(fdt, 0, compat);  }
> 
> +static
> +const struct udevice_id *__of_match_node(const struct udevice_id
> *matches,
> +  const ofnode node)
> +{
> + if (!matches)
> + return NULL;
> +
> + for (; matches->compatible; matches++) {
> + if (ofnode_device_is_compatible(node, matches->compatible))
> + return matches;
> + }
> +
> + return NULL;
> +}
> +
> +const struct udevice_id *of_match_node(const struct udevice_id *matches,
> +const ofnode node)
> +{
> + return __of_match_node(matches, node); }
> +
>  int dev_disable_by_path(const char *path)  {
>   struct uclass *uc;
> diff --git a/include/dm/device.h b/include/dm/device.h index
> 5bef484247..4c357d46ec 100644
> --- a/include/dm/device.h
> +++ b/include/dm/device.h
> @@ -754,6 +754,19 @@ bool device_is_compatible(const struct udevice *dev,
> const char *compat);
>   */
>  bool of_machine_is_compatible(const char *compat);
> 
> +/**
> + * of_match_node() - Tell if a device_node has a matching of_match
> +structure
> + *
> + *
> + * Low level utility function used by device matching.
> + *
> + * @matches: array of of device match structures to search in
> + * @node:the of device structure to match against
> + * @return matching structure on success, NULL if the match is not
> +found  */ const struct udevice_id *of_match_node(const struct
> +udevice_id *matches,
> +const ofnode node);
> +
>  /**
>   * dev_disable_by_path() - Disable a device given its device tree path
>   *
> diff --git a/test/dm/core.c b/test/dm/core.c index 6f380a574c..b94b78d9ba
> 100644
> --- a/test/dm/core.c
> +++ b/test/dm/core.c
> @@ -11,6 +11,7 @@
>  #include 
>  #include 
>  #include 
> +#include 
>  #include 
>  #include 
>  #include 
> @@ -1066,3 +1067,33 @@ static int dm_test_inactive_child(struct
> unit_test_state *uts)
>   return 0;
>  }
>  DM_TEST(dm_test_inactive_child, UT_TESTF_SCAN_PDATA);
> +
> +static int dm_test_of_match_node(struct unit_test_state *uts) {
> + const ulong test_data_expected = 0x1234;
> + ofnode root_node = ofnode_path("/");
> + const struct udevice_id *match;
> + unsigned long match_data;
> +
> + const struct udevice_id soc_device_ids[] = {
> + { .compatible = "sandbox", .data = test_data_expected, },
> + { /* sentinel */ }
> + };
> +
> + const struct udevice_id soc_device_nomatch_ids[] = {
> + { .compatible = "sandbox123", .data = test_data_expected, },
> + { /* sentinel */ }
> + };
> +
> + match = of_match_node(soc_device_ids, root_node);
> + ut_assert(match);
> +
> + match_data = match->data;
> + ut_asserteq(match_data, test_data_expected);
> +
> + match = of_match_node(soc_device_nomatch_ids, root_node);
> + ut_asserteq_ptr(match, NULL);
> +
> + return 0;
> +}
> +DM_TEST(dm_test_of_match_node, UT_TESTF_SCAN_PDATA |
> +UT_TESTF_SCAN_FDT);
> --
> 2.17.1



Properly using gpio_request and pinmux for GPIO

2020-11-12 Thread Oliver Westermann
Hey,

I'm wondering if there is documentation or a good example on how to
configure pin muxing in combination with the gpio_request functions. Our
current implementation is incomplete as it doesn't handle the muxing, but
before randomly copying code I would like to know what the "upstream" way
of doing things like this is, so maybe someone can advise.

The goal is to define N pins as "revision pins" (basically pull up/down
resistors for board revisions) in the dts and based upon this definition
read the pins and calculate a value. Our current implementation just has a
pin list like this:

board-rev-gpios = <
   1 GPIO_ACTIVE_HIGH
   2 GPIO_ACTIVE_HIGH
   3 GPIO_ACTIVE_HIGH
>;

and uses gpio_request_list_by_name() to access those by name, this works as
long as no muxing is required.

My current understanding of the full solution would be to define not only
the gpios, but also a pinctrl, sth like this:

DTS:
board_rev {
pinctrl-names = "gpio";
pinctrl-0 = <_board_rev>;
gpios = <
   1 GPIO_ACTIVE_HIGH
   2 GPIO_ACTIVE_HIGH
   3 GPIO_ACTIVE_HIGH
>;
};

pinctrl_board_rev: i2c1grp {
fsl,pins = <
MX8MM_IOMUXC_GPIO1_IO00_GPIO1_IO0   0x41c3
MX8MM_IOMUXC_GPIO1_IO00_GPIO1_IO2   0x41c3
MX8MM_IOMUXC_GPIO1_IO00_GPIO1_IO3   0x41c3
>;
};

Code:
/* get board_rev node as board_rev, prepare arrays, ... */
gpio_request_list_by_name(board_rev, "gpios", gpios, ARRAY_SIZE(gpios),
GPIOD_IS_IN);
pinctrl_select_state(board_rev, "gpio");

Would this be the correct way to use the muxing functionality?

Best regards, Olli


Re: [PATCH 1/1] riscv: Fix efi header size for RV32

2020-11-12 Thread Heinrich Schuchardt
On 12.11.20 14:43, Leo Liang wrote:
> Date: Thu, 12 Nov 2020 10:09:52 +0800
> From: Leo Yu-Chi Liang 
> Subject: [PATCH 1/1] riscv: Fix efi header size for RV32
>
> This patch depends on Atish's patch.
> (https://patchwork.ozlabs.org/project/uboot/patch/20201013192331.3236458-1-atish.pa...@wdc.com/)
>
> Modify the size of the Optional Header "Windows-Specific Fields" to fit with 
> the specification.
> (https://docs.microsoft.com/en-us/windows/win32/debug/pe-format)

With both patches applied on RISCV-64 I get SizeOfOptionalHeader = 0xa0
instead of expected 0xf0.

Best regards

Heinrich

>
> Signed-off-by: Leo Yu-Chi Liang 
> Cc: r...@andestech.com
> Cc: alan...@andestech.com
> Cc: atish.pa...@wdc.com
> Cc: xypron.g...@gmx.de
> Cc: bmeng...@gmail.com
> ---
>  arch/riscv/lib/crt0_riscv_efi.S | 17 ++---
>  1 file changed, 14 insertions(+), 3 deletions(-)
>
> diff --git a/arch/riscv/lib/crt0_riscv_efi.S b/arch/riscv/lib/crt0_riscv_efi.S
> index 4aaa49ad07..48ff89553b 100644
> --- a/arch/riscv/lib/crt0_riscv_efi.S
> +++ b/arch/riscv/lib/crt0_riscv_efi.S
> @@ -15,13 +15,13 @@
>  #define SAVE_LONG(reg, idx)  sd  reg, (idx*SIZE_LONG)(sp)
>  #define LOAD_LONG(reg, idx)  ld  reg, (idx*SIZE_LONG)(sp)
>  #define PE_MACHINE   IMAGE_FILE_MACHINE_RISCV64
> -#define PE_MAGICIMAGE_NT_OPTIONAL_HDR64_MAGIC
> +#define PE_MAGICIMAGE_NT_OPTIONAL_HDR64_MAGIC
>  #else
>  #define SIZE_LONG4
>  #define SAVE_LONG(reg, idx)  sw  reg, (idx*SIZE_LONG)(sp)
>  #define LOAD_LONG(reg, idx)  lw  reg, (idx*SIZE_LONG)(sp)
>  #define PE_MACHINE   IMAGE_FILE_MACHINE_RISCV32
> -#define PE_MAGICIMAGE_NT_OPTIONAL_HDR32_MAGIC
> +#define PE_MAGICIMAGE_NT_OPTIONAL_HDR32_MAGIC
>  #endif
>
>
> @@ -50,7 +50,7 @@ coff_header:
>IMAGE_FILE_LOCAL_SYMS_STRIPPED | \
>IMAGE_FILE_DEBUG_STRIPPED)
>  optional_header:
> - .short  PE_MAGIC/* PE32+ format */
> + .short  PE_MAGIC/* PE32(+) format */
>   .byte   0x02/* MajorLinkerVersion */
>   .byte   0x14/* MinorLinkerVersion */
>   .long   _edata - _start /* SizeOfCode */
> @@ -63,7 +63,11 @@ optional_header:
>  #endif
>
>  extra_header_fields:
> +#if __riscv_xlen == 32
> + .long   0   /* ImageBase */
> +#else
>   .quad   0   /* ImageBase */
> +#endif
>   .long   0x20/* SectionAlignment */
>   .long   0x8 /* FileAlignment */
>   .short  0   /* MajorOperatingSystemVersion 
> */
> @@ -83,10 +87,17 @@ extra_header_fields:
>   .long   0   /* CheckSum */
>   .short  IMAGE_SUBSYSTEM_EFI_APPLICATION /* Subsystem */
>   .short  0   /* DllCharacteristics */
> +#if __riscv_xlen == 32
> + .long   0   /* SizeOfStackReserve */
> + .long   0   /* SizeOfStackCommit */
> + .long   0   /* SizeOfHeapReserve */
> + .long   0   /* SizeOfHeapCommit */
> +#else
>   .quad   0   /* SizeOfStackReserve */
>   .quad   0   /* SizeOfStackCommit */
>   .quad   0   /* SizeOfHeapReserve */
>   .quad   0   /* SizeOfHeapCommit */
> +#endif
>   .long   0   /* LoaderFlags */
>   .long   0x6 /* NumberOfRvaAndSizes */
>
>



[PATCH 1/1] riscv: Fix efi header size for RV32

2020-11-12 Thread Leo Liang
Date: Thu, 12 Nov 2020 10:09:52 +0800
From: Leo Yu-Chi Liang 
Subject: [PATCH 1/1] riscv: Fix efi header size for RV32

This patch depends on Atish's patch.
(https://patchwork.ozlabs.org/project/uboot/patch/20201013192331.3236458-1-atish.pa...@wdc.com/)

Modify the size of the Optional Header "Windows-Specific Fields" to fit with 
the specification.
(https://docs.microsoft.com/en-us/windows/win32/debug/pe-format)

Signed-off-by: Leo Yu-Chi Liang 
Cc: r...@andestech.com
Cc: alan...@andestech.com
Cc: atish.pa...@wdc.com
Cc: xypron.g...@gmx.de
Cc: bmeng...@gmail.com
---
 arch/riscv/lib/crt0_riscv_efi.S | 17 ++---
 1 file changed, 14 insertions(+), 3 deletions(-)

diff --git a/arch/riscv/lib/crt0_riscv_efi.S b/arch/riscv/lib/crt0_riscv_efi.S
index 4aaa49ad07..48ff89553b 100644
--- a/arch/riscv/lib/crt0_riscv_efi.S
+++ b/arch/riscv/lib/crt0_riscv_efi.S
@@ -15,13 +15,13 @@
 #define SAVE_LONG(reg, idx)sd  reg, (idx*SIZE_LONG)(sp)
 #define LOAD_LONG(reg, idx)ld  reg, (idx*SIZE_LONG)(sp)
 #define PE_MACHINE IMAGE_FILE_MACHINE_RISCV64
-#define PE_MAGICIMAGE_NT_OPTIONAL_HDR64_MAGIC
+#define PE_MAGICIMAGE_NT_OPTIONAL_HDR64_MAGIC
 #else
 #define SIZE_LONG  4
 #define SAVE_LONG(reg, idx)sw  reg, (idx*SIZE_LONG)(sp)
 #define LOAD_LONG(reg, idx)lw  reg, (idx*SIZE_LONG)(sp)
 #define PE_MACHINE IMAGE_FILE_MACHINE_RISCV32
-#define PE_MAGICIMAGE_NT_OPTIONAL_HDR32_MAGIC
+#define PE_MAGICIMAGE_NT_OPTIONAL_HDR32_MAGIC
 #endif
 
 
@@ -50,7 +50,7 @@ coff_header:
 IMAGE_FILE_LOCAL_SYMS_STRIPPED | \
 IMAGE_FILE_DEBUG_STRIPPED)
 optional_header:
-   .short  PE_MAGIC/* PE32+ format */
+   .short  PE_MAGIC/* PE32(+) format */
.byte   0x02/* MajorLinkerVersion */
.byte   0x14/* MinorLinkerVersion */
.long   _edata - _start /* SizeOfCode */
@@ -63,7 +63,11 @@ optional_header:
 #endif
 
 extra_header_fields:
+#if __riscv_xlen == 32
+   .long   0   /* ImageBase */
+#else
.quad   0   /* ImageBase */
+#endif
.long   0x20/* SectionAlignment */
.long   0x8 /* FileAlignment */
.short  0   /* MajorOperatingSystemVersion 
*/
@@ -83,10 +87,17 @@ extra_header_fields:
.long   0   /* CheckSum */
.short  IMAGE_SUBSYSTEM_EFI_APPLICATION /* Subsystem */
.short  0   /* DllCharacteristics */
+#if __riscv_xlen == 32
+   .long   0   /* SizeOfStackReserve */
+   .long   0   /* SizeOfStackCommit */
+   .long   0   /* SizeOfHeapReserve */
+   .long   0   /* SizeOfHeapCommit */
+#else
.quad   0   /* SizeOfStackReserve */
.quad   0   /* SizeOfStackCommit */
.quad   0   /* SizeOfHeapReserve */
.quad   0   /* SizeOfHeapCommit */
+#endif
.long   0   /* LoaderFlags */
.long   0x6 /* NumberOfRvaAndSizes */
 
-- 
2.17.0



Re: [PATCH RESEND v2 0/2] arm64: meson: add libretech cc v2 support

2020-11-12 Thread Neil Armstrong
On 06/11/2020 10:45, Jerome Brunet wrote:
> This patchset adds support for the Amlogic based libretech cc v2.
> Like the v1, this revised platform used the s905x SoC.
> 
> Changes since v1:
> - Move Makefile change to patch #1
> - Amend documentation for libretech cc v2
> 
> Jerome Brunet (2):
>   arm64: dts: import libretech-cc-v2 from linux v5.10-rc1
>   arm64: meson: add support for libretech-cc v2
> 
>  arch/arm/dts/Makefile |   1 +
>  ...eson-gxl-s905x-libretech-cc-v2-u-boot.dtsi |   7 +
>  .../dts/meson-gxl-s905x-libretech-cc-v2.dts   | 318 ++
>  board/amlogic/p212/MAINTAINERS|   1 +
>  configs/libretech-cc_v2_defconfig |  82 +
>  doc/board/amlogic/index.rst   | 117 +++
>  doc/board/amlogic/libretech-cc.rst|  15 +-
>  7 files changed, 480 insertions(+), 61 deletions(-)
>  create mode 100644 arch/arm/dts/meson-gxl-s905x-libretech-cc-v2-u-boot.dtsi
>  create mode 100644 arch/arm/dts/meson-gxl-s905x-libretech-cc-v2.dts
>  create mode 100644 configs/libretech-cc_v2_defconfig
> 

Thanks,
Applied to u-boot-amlogic-next


Re: [PATCH] video: add TDO tl070wsh30 panel driver

2020-11-12 Thread Neil Armstrong
On 06/11/2020 20:02, Anatolij Gustschin wrote:
> On Fri,  6 Nov 2020 11:29:47 +0100
> Neil Armstrong narmstr...@baylibre.com wrote:
> 
>> This adds support for the TDO TL070WSH30 TFT-LCD panel module.
>> The panel has a 1024×600 resolution and uses 24 bit RGB per pixel.
>> It provides a MIPI DSI interface to the host, a built-in LED backlight
>> and touch controller.
>>
>> Signed-off-by: Neil Armstrong 
> 
> Reviewed-by: Anatolij Gustschin 
> 

Thanks.

Applied to u-boot-amlogic-next


Re: [PATCH 0/4] ARM: upgrade Amlogic DT to Linux 5.10

2020-11-12 Thread Neil Armstrong
On 06/11/2020 11:18, Neil Armstrong wrote:
> This syncs the Amlogic DT to 5.10, and :
> - adds support for the new common MESON_EE_POWER_DOMAIN synced on 5.10
> - adds Odroid-N2+ DT using the Odroid-N2 DT as base
> 
> Neil Armstrong (4):
>   ARM: dts: sync Amlogic GX & AXG from Linux 5.10-rc1
>   power: domain: meson-ee-pwrc: add support for the Meson GX SoCs
>   power: domain: meson-ee-pwrc: add support for the Meson AXG SoCs
>   configs: use the new MESON_EE_POWER_DOMAIN driver for Amlogic
> GXBB/GXL/GXM boards
> 
>  arch/arm/dts/Makefile |   1 +
>  arch/arm/dts/meson-axg.dtsi   |   6 +-
>  arch/arm/dts/meson-g12b-khadas-vim3.dtsi  |  88 ---
>  arch/arm/dts/meson-g12b-odroid-n2-plus.dts|  31 +
>  arch/arm/dts/meson-g12b-odroid-n2.dts | 618 +
>  arch/arm/dts/meson-g12b-odroid-n2.dtsi| 625 ++
>  arch/arm/dts/meson-gx-mali450.dtsi|  61 ++
>  arch/arm/dts/meson-gx.dtsi|  18 +-
>  arch/arm/dts/meson-gxbb.dtsi  |  63 +-
>  arch/arm/dts/meson-gxl-mali.dtsi  |  46 +-
>  arch/arm/dts/meson-gxl-s805x-libretech-ac.dts |   2 +-
>  arch/arm/dts/meson-gxl-s805x.dtsi |  23 +
>  arch/arm/dts/meson-gxl.dtsi   |  17 +-
>  arch/arm/dts/meson-gxm.dtsi   |  45 +-
>  arch/arm/dts/meson-khadas-vim3.dtsi   |  94 ++-
>  arch/arm/dts/meson-sm1-khadas-vim3l.dts   |  86 ---
>  configs/libretech-ac_defconfig|   2 +-
>  configs/libretech-cc_defconfig|   2 +-
>  configs/libretech-s905d-pc_defconfig  |   2 +-
>  configs/libretech-s912-pc_defconfig   |   2 +-
>  configs/odroid-c2_defconfig   |   2 +-
>  drivers/power/domain/meson-ee-pwrc.c  |  52 +-
>  include/dt-bindings/power/meson-axg-power.h   |  14 +
>  include/dt-bindings/power/meson-gxbb-power.h  |  13 +
>  24 files changed, 1002 insertions(+), 911 deletions(-)
>  create mode 100644 arch/arm/dts/meson-g12b-odroid-n2-plus.dts
>  create mode 100644 arch/arm/dts/meson-g12b-odroid-n2.dtsi
>  create mode 100644 arch/arm/dts/meson-gx-mali450.dtsi
>  create mode 100644 arch/arm/dts/meson-gxl-s805x.dtsi
>  create mode 100644 include/dt-bindings/power/meson-axg-power.h
>  create mode 100644 include/dt-bindings/power/meson-gxbb-power.h
> 


Applied to u-boot-amlogic-next



Re: i.MX USB SDP broken on imx6q-sabresd

2020-11-12 Thread Heiko Schocher

Hello Fabio,

Am 12.11.2020 um 13:52 schrieb Fabio Estevam:

Hi Heiko,

On Thu, Nov 12, 2020 at 2:12 AM Heiko Schocher  wrote:


Hmm... just tried cuurent mainline on an imx6ull based board, works
fine for me, see log [1] ... Ok, I boot signed image: u-boot-ivt.img.signed


Thanks for your test.

I suppose your board does not use CONFIG_SPL_LOAD_FIT=y.


No ...


If you have a chance to try loading SPL and u-boot-dtb.img via
imx_usb_loader using a wandboard, I believe you will be able to
reproduce the problem.


I have here a wandboard which I have in my daily U-Boot mainline
testing:

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

But I boot from sd card ...


It looks like the issue I am having is the same as the one reported by Tim:
http://u-boot.10912.n7.nabble.com/iMX8MM-USB-support-td405349.html


Hmm... I can not say here more without trying out ... currently
it is hard to find some freetime ... I can just speculate now ...

your log:
"""
load_size 0 bytes
load_addr 0x
dcd 1
clear_dcd 0
plug 1
jump_mode 3
jump_addr 0x
== end work item
header not found 8400:2e77d044, 4000
do_download failed, err=-22
HAB security state: development mode (0x56787856)
"""

Hmm... there is no line:

loading binary file(/srv/tftpboot/imx6ull_pat_defconfig/20201016/u-boot-ivt.img.signed) to 84c0, 
skip=0, fsize=94960 type=aa


instead:

header not found 8400:2e77d044, 4000

So this seems to me failing in imx_usb_loader code ...

The "header not found" comes from imx_sdp.c

May you enable dbg_printf() in imx_usb_loader code ... it seems
to me, that there is a problem with missing some header in your
file you try to download  may you can compare with working one?

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 v2 0/3] mmc: meson-gx: fix mmc & sdcard failur on SM1 SoCs

2020-11-12 Thread Neil Armstrong
On 11/11/2020 00:22, Jaehoon Chung wrote:
> Amlogic SM1 SoCs doesn't work over 50MHz. When phase sets to 270', it's
> working fine over 50MHz on Amlogic SM1 SoCs.
> 
> To use a proper phase value adds an u-boot only sm1 compatible.
> 
> In future, it needs to find what value is a proper about each SoCs.
> 
> According to Neil's suggestion, applied code based on below patch.
> https://patchwork.ozlabs.org/project/uboot/patch/20201109031233.25320-1-jh80.ch...@samsung.com
> 
> Tested on Odroid-C4/N2, VIM3.
> 
> Changelog v2:
> - Rewrote "[2/3] mmc: meson-gx: limit max frequency on SM1 SoCs" 
> - Applied "mmc: meson_gx_mmc: change a clock phase to stable value"
> - Add Jaehoon's Signed-off tag
> 
> Neil Armstrong (3):
>   mmc: meson-gx: move arch header to local header
>   mmc: meson-gx: change clock phase value on SM1 SoCs
>   ARM: dts: meson-sm1: add u-boot specific MMC controller compatible
> 
>  .../meson-g12b-a311d-khadas-vim3-u-boot.dtsi  |  1 +
>  arch/arm/dts/meson-khadas-vim3-u-boot.dtsi|  2 --
>  .../dts/meson-sm1-khadas-vim3l-u-boot.dtsi|  1 +
>  arch/arm/dts/meson-sm1-odroid-c4-u-boot.dtsi  |  2 +-
>  arch/arm/dts/meson-sm1-sei610-u-boot.dtsi |  2 +-
>  arch/arm/dts/meson-sm1-u-boot.dtsi| 20 +
>  drivers/mmc/meson_gx_mmc.c| 29 +++
>  .../sd_emmc.h => drivers/mmc/meson_gx_mmc.h   | 10 ---
>  8 files changed, 54 insertions(+), 13 deletions(-)
>  create mode 100644 arch/arm/dts/meson-sm1-u-boot.dtsi
>  rename arch/arm/include/asm/arch-meson/sd_emmc.h => 
> drivers/mmc/meson_gx_mmc.h (95%)
> 

Thanks !

Applying to u-boot-amlogic


Re: [PATCH] ARM: mach-meson: update SoC IDs

2020-11-12 Thread Neil Armstrong
On 09/11/2020 14:33, Neil Armstrong wrote:
> Update From Linux commits
> - 240051cb833b ("soc: amlogic: meson-gx-socinfo: Add A1 and A113L IDs")
> - 1d7c541b8a5b ("soc: amlogic: meson-gx-socinfo: Add S905X3 ID for VIM3L")
> - fdfc6997bd08 ("soc: amlogic: meson-gx-socinfo: Fix S905D3 ID for VIM3L")
> - d16d0481e6ba ("soc: amlogic: meson-gx-socinfo: Fix S905X3 and S905D3 ID's")
> 
> Signed-off-by: Neil Armstrong 
> ---
>  arch/arm/mach-meson/board-info.c | 7 ++-
>  1 file changed, 6 insertions(+), 1 deletion(-)
> 
> diff --git a/arch/arm/mach-meson/board-info.c 
> b/arch/arm/mach-meson/board-info.c
> index 3abb27e03b..e61d1adb10 100644
> --- a/arch/arm/mach-meson/board-info.c
> +++ b/arch/arm/mach-meson/board-info.c
> @@ -38,6 +38,7 @@ static const struct meson_gx_soc_id {
>   { "G12A",   0x28 },
>   { "G12B",   0x29 },
>   { "SM1",0x2b },
> + { "A1", 0x2c },
>  };
>  
>  static const struct meson_gx_package_id {
> @@ -65,7 +66,11 @@ static const struct meson_gx_package_id {
>   { "S905X2", 0x28, 0x40, 0xf0 },
>   { "A311D",  0x29, 0x10, 0xf0 },
>   { "S922X",  0x29, 0x40, 0xf0 },
> - { "S905X3", 0x2b, 0x5, 0xf },
> + { "S905D3", 0x2b, 0x4, 0xf5 },
> + { "S905X3", 0x2b, 0x5, 0xf5 },
> + { "S905X3", 0x2b, 0x10, 0x3f },
> + { "S905D3", 0x2b, 0x30, 0x3f },
> + { "A113L", 0x2c, 0x0, 0xf8 },
>  };
>  
>  DECLARE_GLOBAL_DATA_PTR;
> 

Applied to u-boot-amlogic


[PATCH v2] env: mmc: Correct partition comparison in mmc_offset_try_partition

2020-11-12 Thread Hoyeonjiki Kim
The function mmc_offset_try_partition searches MMC partition to save the
environment data by name.  However, it only compares the first word-size
bytes (size of 'const char *'), which may make the function to find
unintended partition.

Correct the function not to partially compare the partition name with
config "u-boot,mmc-env-partition".

Fixes: c9e87ba66540 ("env: Save environment at the end of an MMC partition")
Signed-off-by: Hoyeonjiki Kim 
Reviewed-by: Jaehoon Chung 
---
 env/mmc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/env/mmc.c b/env/mmc.c
index 4e67180b23..505f7aa2b8 100644
--- a/env/mmc.c
+++ b/env/mmc.c
@@ -42,7 +42,7 @@ static inline int mmc_offset_try_partition(const char *str, 
int copy, s64 *val)
if (ret < 0)
return ret;
 
-   if (!strncmp((const char *)info.name, str, sizeof(str)))
+   if (!strcmp((const char *)info.name, str))
break;
}
 
-- 
2.25.1



Re: [PATCH] env: mmc: Correct partition comparison in mmc_offset_try_partition

2020-11-12 Thread Hoyeonjiki Kim
Mis-submitted the patch without versioning... :(
I'll re-submit my patch again.

Sorry for interrupting you.

Best Regards,
Hoyeonjiki Kim

On Thu, Nov 12, 2020 at 9:30 PM Hoyeonjiki Kim  wrote:
>
> The function mmc_offset_try_partition searches MMC partition to save the
> environment data by name.  However, it only compares the first word-size
> bytes (size of 'const char *'), which may make the function to find
> unintended partition.
>
> Correct the function not to partially compare the partition name with
> config "u-boot,mmc-env-partition".
>
> Fixes: c9e87ba66540 ("env: Save environment at the end of an MMC partition")
> Signed-off-by: Hoyeonjiki Kim 
> Reviewed-by: Jaehoon Chung 
> ---
>  env/mmc.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/env/mmc.c b/env/mmc.c
> index 4e67180b23..505f7aa2b8 100644
> --- a/env/mmc.c
> +++ b/env/mmc.c
> @@ -42,7 +42,7 @@ static inline int mmc_offset_try_partition(const char *str, 
> int copy, s64 *val)
> if (ret < 0)
> return ret;
>
> -   if (!strncmp((const char *)info.name, str, sizeof(str)))
> +   if (!strcmp((const char *)info.name, str))
> break;
> }
>
> --
> 2.25.1
>


Re: i.MX USB SDP broken on imx6q-sabresd

2020-11-12 Thread Fabio Estevam
Hi Heiko,

On Thu, Nov 12, 2020 at 2:12 AM Heiko Schocher  wrote:

> Hmm... just tried cuurent mainline on an imx6ull based board, works
> fine for me, see log [1] ... Ok, I boot signed image: u-boot-ivt.img.signed

Thanks for your test.

I suppose your board does not use CONFIG_SPL_LOAD_FIT=y.

If you have a chance to try loading SPL and u-boot-dtb.img via
imx_usb_loader using a wandboard, I believe you will be able to
reproduce the problem.

It looks like the issue I am having is the same as the one reported by Tim:
http://u-boot.10912.n7.nabble.com/iMX8MM-USB-support-td405349.html

Thanks,

Fabio Estevam


Properly using gpio_request and muxing

2020-11-12 Thread Oliver Westermann
Hey,

I'm wondering if there is documentation or a good example on how to
configure pin muxing in combination with the gpio_request functions. Our
current implementation is incomplete as it doesn't handle the muxing, but
before randomly copying code I would like to know what the "upstream" way
of doing things like this is, so maybe someone can advise.

The goal is to define N pins as "revision pins" (basically pull up/down
resistors for board revisions) in the dts and based upon this definition
read the pins and calculate a value. Our current implementation just has a
pin list like this:

board-rev-gpios = <
   1 GPIO_ACTIVE_HIGH
   2 GPIO_ACTIVE_HIGH
   3 GPIO_ACTIVE_HIGH
>;

and uses gpio_request_list_by_name() to access those by name, this works as
long as no muxing is required.

My current understanding of the full solution would be to define not only
the gpios, but also a pinctrl, sth like this:

DTS:
board_rev {
pinctrl-names = "gpio";
pinctrl-0 = <_board_rev>;
gpios = <
   1 GPIO_ACTIVE_HIGH
   2 GPIO_ACTIVE_HIGH
   3 GPIO_ACTIVE_HIGH
>;
};

pinctrl_board_rev: i2c1grp {
fsl,pins = <
MX8MM_IOMUXC_GPIO1_IO00_GPIO1_IO0   0x41c3
MX8MM_IOMUXC_GPIO1_IO00_GPIO1_IO2   0x41c3
MX8MM_IOMUXC_GPIO1_IO00_GPIO1_IO3   0x41c3
>;
};

Code:
/* get board_rev node as board_rev, prepare arrays, ... */
gpio_request_list_by_name(board_rev, "gpios", gpios, ARRAY_SIZE(gpios),
GPIOD_IS_IN);
pinctrl_select_state(board_rev, "gpio");

Would this be the correct way to use the muxing functionality?

Best regards, Olli


Re: [PATCH] common/board_r: make sure to call initr_dm() before initr_trace()

2020-11-12 Thread Heinrich Schuchardt
On 11/12/20 12:18 PM, Pragnesh Patel wrote:
> Tracing need timer ticks and initr_dm() will make gd->timer and
> gd->dm_root is equal to NULL, so make sure that initr_dm() to
> call before tracing got enabled.
>
> Signed-off-by: Pragnesh Patel 
> ---
>  common/board_r.c | 6 +++---
>  1 file changed, 3 insertions(+), 3 deletions(-)
>
> diff --git a/common/board_r.c b/common/board_r.c
> index 29dd7d26d9..7140a39947 100644
> --- a/common/board_r.c
> +++ b/common/board_r.c
> @@ -693,6 +693,9 @@ static int run_main_loop(void)
>   * TODO: perhaps reset the watchdog in the initcall function after each call?
>   */
>  static init_fnc_t init_sequence_r[] = {
> +#ifdef CONFIG_DM
> + initr_dm,
> +#endif
>   initr_trace,
>   initr_reloc,
>   /* TODO: could x86/PPC have this also perhaps? */
> @@ -718,9 +721,6 @@ static init_fnc_t init_sequence_r[] = {
>   initr_noncached,
>  #endif
>   initr_of_live,
> -#ifdef CONFIG_DM
> - initr_dm,
> -#endif

You are moving initr_of_live before initr_of_live. I doubt this will
work for boards that have CONFIG_OF_LIVE=y.

Can't we move initr_trace down in the code to after both initr_of_live
and initr_dm?

@Simon:
Please, advise.

Best regards

Heinrich

>  #if defined(CONFIG_ARM) || defined(CONFIG_NDS32) || defined(CONFIG_RISCV) || 
> \
>   defined(CONFIG_SANDBOX)
>   board_init, /* Setup chipselects */
>



[PATCH] env: mmc: Correct partition comparison in mmc_offset_try_partition

2020-11-12 Thread Hoyeonjiki Kim
The function mmc_offset_try_partition searches MMC partition to save the
environment data by name.  However, it only compares the first word-size
bytes (size of 'const char *'), which may make the function to find
unintended partition.

Correct the function not to partially compare the partition name with
config "u-boot,mmc-env-partition".

Fixes: c9e87ba66540 ("env: Save environment at the end of an MMC partition")
Signed-off-by: Hoyeonjiki Kim 
Reviewed-by: Jaehoon Chung 
---
 env/mmc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/env/mmc.c b/env/mmc.c
index 4e67180b23..505f7aa2b8 100644
--- a/env/mmc.c
+++ b/env/mmc.c
@@ -42,7 +42,7 @@ static inline int mmc_offset_try_partition(const char *str, 
int copy, s64 *val)
if (ret < 0)
return ret;
 
-   if (!strncmp((const char *)info.name, str, sizeof(str)))
+   if (!strcmp((const char *)info.name, str))
break;
}
 
-- 
2.25.1



RE: [PATCH v3 1/1] riscv: Add timer_get_us() for tracing

2020-11-12 Thread Pragnesh Patel
>-Original Message-
>From: U-Boot  On Behalf Of Pragnesh Patel
>Sent: 12 November 2020 13:06
>To: Heinrich Schuchardt ; Simon Glass
>
>Cc: atish.pa...@wdc.com; palmerdabb...@google.com; u-boot@lists.denx.de;
>bmeng...@gmail.com; Paul Walmsley ( Sifive) ;
>anup.pa...@wdc.com; Sagar Kadam ;
>r...@andestech.com; Sean Anderson 
>Subject: RE: [PATCH v3 1/1] riscv: Add timer_get_us() for tracing
>
>Hi Heinrich,
>
>>-Original Message-
>>From: Heinrich Schuchardt 
>>Sent: 12 November 2020 12:49
>>To: Pragnesh Patel ; Simon Glass
>>
>>Cc: atish.pa...@wdc.com; palmerdabb...@google.com;
>>u-boot@lists.denx.de; bmeng...@gmail.com; Paul Walmsley ( Sifive)
>>; anup.pa...@wdc.com; Sagar Kadam
>>; r...@andestech.com; Sean Anderson
>>
>>Subject: Re: [PATCH v3 1/1] riscv: Add timer_get_us() for tracing
>>
>>[External Email] Do not click links or attachments unless you recognize
>>the sender and know the content is safe
>>
>>On 11/12/20 8:09 AM, Heinrich Schuchardt wrote:
>>> On 11/12/20 7:34 AM, Pragnesh Patel wrote:
 Hi Heinrich,

> -Original Message-
> From: Heinrich Schuchardt 
> Sent: 11 November 2020 19:18
> To: Pragnesh Patel 
> Cc: atish.pa...@wdc.com; palmerdabb...@google.com;
> u-boot@lists.denx.de; bmeng...@gmail.com; Paul Walmsley ( Sifive)
> ; anup.pa...@wdc.com; Sagar Kadam
> ; r...@andestech.com; Sean Anderson
> ; Simon Glass 
> Subject: Re: [PATCH v3 1/1] riscv: Add timer_get_us() for tracing
>
> [External Email] Do not click links or attachments unless you
> recognize the sender and know the content is safe
>
> On 11/11/20 12:56 PM, Pragnesh Patel wrote:
>> Hi Heinrich,
>>
>>> -Original Message-
>>> From: Heinrich Schuchardt 
>>> Sent: 11 November 2020 16:51
>>> To: Pragnesh Patel ;
>>> u-boot@lists.denx.de
>>> Cc: atish.pa...@wdc.com; palmerdabb...@google.com;
>>> bmeng...@gmail.com; Paul Walmsley ( Sifive)
>>> ; anup.pa...@wdc.com; Sagar Kadam
>>> ; r...@andestech.com; Sean Anderson
>>> ; Simon Glass 
>>> Subject: Re: [PATCH v3 1/1] riscv: Add timer_get_us() for tracing
>>>
>>> [External Email] Do not click links or attachments unless you
>>> recognize the sender and know the content is safe
>>>
>>> On 11.11.20 11:14, Pragnesh Patel wrote:
 Add timer_get_us() which is useful for tracing.
 For S-mode U-Boot, CSR_TIMEH and CSR_TIME will provide a timer
 ticks and For M-mode U-Boot, mtime register will provide the same.

 Signed-off-by: Pragnesh Patel 
>>>
>>> The default implementation of get_timer_us() in lib/time.c calls
>>> get_ticks() which calls timer_get_count(). The get_count()
>>> operation is implemented in drivers/timer/andes_plmt_timer.c,
>>> drivers/timer/sifive_clint_timer.c, drivers/timer/riscv_timer.c.
>>>
>>> Why do you need special timer_get_us() implementations?
>>> Isn't it enough to supply the get_count() operation in the drivers?
>>
>> get_ticks() is depend on gd->timer and there are 2 cases
>>
>> 1) if gd->timer== NULL then dm_timer_init() gets called and it
>> will call functions which are not marked with "notrace" so tracing got
>stuck.
>
> Thanks for the background information.
>
> Please, identify the existing functions that lack "notrace" and fix
> them. This will give us a solution for all existing boards and not
> for a small
>>selection.
> Furthermore it will avoid code duplication.

 I tried but There are so many functions which need "notrace".
>>>
>>> Let's start with the problem statement:
>>>
>>> When tracing functions is enabled this adds calls to
>>> __cyg_profile_func_enter() and __cyg_profile_func_exit() to the
>>> traced functions.
>>>
>>> __cyg_profile_func_exit() and __cyg_profile_func_exit() invoke
>>> timer_get_us() to record the entry and exit time.
>>>
>>> If timer_get_us() or any function used to implement does not carry
>>> __attribute__((no_instrument_function)) this will lead to an
>>> indefinite recursion.
>>>
>>> We have to change __cyg_profile_func_enter() and
>>> __cyg_profile_func_exit() such that during their execution no
>>> function is traced. We can do so by temporarily setting trace_enabled to 
>>> false.
>>>
>>> Does this match your observation?
>>
>>I just tried to put this into a patch
>>
>>[PATCH 1/1] trace: avoid infinite recursion
>>https://lists.denx.de/pipermail/u-
>>boot/2020-November/432589.html
>>https://patchwork.ozlabs.org/project/uboot/patch/20201112071411.4202-1-
>>xypron.g...@gmx.de/
>>
>>Does this solve you problem?
>
>Getting error, "Could not initialize timer (err -11)" from get_ticks() 
>function.
>
>Below are my boot logs,
>
>U-Boot 2021.01-rc1-00244-g794c155581-dirty (Nov 12 2020 - 12:56:27 +0530)
>
>CPU:   rv64imafdc
>Model: SiFive HiFive Unleashed A00
>DRAM:  8 GiB
>trace: enabled
>Could not initialize timer (err -11)
>
>Could not 

Re: [PATCH] env: mmc: Correct partition comparison in mmc_offset_try_partition

2020-11-12 Thread Hoyeonjiki Kim
On Thu, Nov 12, 2020 at 8:34 AM Jaehoon Chung  wrote:
>
> Hi,
>
> On 11/11/20 7:25 PM, 김호연지기 wrote:
> > On Wed, Nov 11, 2020 at 7:07 PM Jorge Ramirez-Ortiz, Gmail
> >  wrote:
> >>
> >> On 11/11/20, Jaehoon Chung wrote:
> >>> On 11/10/20 11:28 PM, Hoyeonjiki Kim wrote:
>  The function mmc_offset_try_partition searches MMC partition to save the
>  environment data by name.  However, it only compares the first word-size
>  bytes (size of 'const char *'), which may make the function to find
>  unintended partition.
> 
>  Correct the function not to partially compare the partition name with
>  config "u-boot,,mmc-env-partition".
> 
>  Signed-off-by: Hoyeonjiki Kim 
>  ---
>   env/mmc.c | 2 +-
>   1 file changed, 1 insertion(+), 1 deletion(-)
> 
>  diff --git a/env/mmc.c b/env/mmc.c
>  index 4e67180b23..505f7aa2b8 100644
>  --- a/env/mmc.c
>  +++ b/env/mmc.c
>  @@ -42,7 +42,7 @@ static inline int mmc_offset_try_partition(const char 
>  *str, int copy, s64 *val)
>  if (ret < 0)
>  return ret;
> 
>  -   if (!strncmp((const char *)info.name, str, sizeof(str)))
>  +   if (!strcmp((const char *)info.name, str))
> >>>
> >>> Using "strlen(str)" is better than changing to strcmp.
> >>>
> >>> strncmp(..., ..., strlen(str))
> >>
> >> absolutely.
> >> maybe also modify the commit like to indicate a fix to the current bug?
> >
> > Thanks for the feedback for both.
> >
> > However, when we do so, isn't there still the possibility for the
> > function searching incorrect partition if,
> > 1) "str" is shorter than "info.name", and
> > 2) the first "strlen(str)" letters of "info.name" is same with those of 
> > "str"?
>
> Make sense. You're right. :)
>
> There are two approach. One is that choose one of those length what is longer.
> Other is your approach. I don't have any objection to fix whatever.
>
> Just resend your patch with Jorge's comment, plz.
>
>
> Best Regards,
> Jaehoon Chung

Sure, thanks for your opinion.
Will be back with v2.

Best Regards,
Hoyeonjiki Kim

>
> >
> > This commit is for fixing the current bug, but also I wanna make sure
> > that partition name matches fully.
> >
> > Let me know your opinion.
> >
> > Best Regards,
> > Hoeyonjiki Kim
> >
> >>
> >>>
> >>>
> >>> Best Regards,
> >>> Jaehoon Chung
> >>>
>  break;
>  }
> 
> 
> >>>
> >
>


RE: [v3 09/16] arm: socfpga: soc64: Add ATF support for Reset Manager driver

2020-11-12 Thread Tan, Ley Foon



> -Original Message-
> From: Lim, Elly Siew Chin 
> Sent: Thursday, October 15, 2020 8:30 PM
> To: u-boot@lists.denx.de
> Cc: Marek Vasut ; Tan, Ley Foon
> ; See, Chin Liang ;
> Simon Goldschmidt ; Chee, Tien Fong
> ; Westergreen, Dalon
> ; Simon Glass ; Ang,
> Chee Hong ; Lim, Elly Siew Chin
> 
> Subject: [v3 09/16] arm: socfpga: soc64: Add ATF support for Reset Manager
> driver
> 
> From: Chee Hong Ang 
> 
> In non-secure mode (EL2), Reset Manager driver calls the SMC/PSCI service
> provided by ATF to enable/disable the SOCFPGA bridges.
> 
> Signed-off-by: Chee Hong Ang 
> Signed-off-by: Siew Chin Lim 
> ---
>  arch/arm/mach-socfpga/reset_manager_s10.c | 13 +
>  1 file changed, 13 insertions(+)
> 
> diff --git a/arch/arm/mach-socfpga/reset_manager_s10.c
> b/arch/arm/mach-socfpga/reset_manager_s10.c
> index 3746e6a60c..af8f2c0873 100644
> --- a/arch/arm/mach-socfpga/reset_manager_s10.c
> +++ b/arch/arm/mach-socfpga/reset_manager_s10.c
> @@ -5,11 +5,14 @@
>   */
> 

Reviewed-by: Ley Foon Tan 


[PATCH v1] imx: clk: added IPG Clock for I2C on imx8qm

2020-11-12 Thread Oliver Graute
This patch fixes this clk issue on I2C on imx8qm

 => i2c bus
 Bus 3:  i2c@5a83
 => i2c dev 3
 Setting bus to 3
 Failed to enable ipg clk
 Failure changing bus number (-524)

Signed-off-by: Oliver Graute 
Cc: Stefano Babic 
Cc: Peng Fan 
Cc: uboot-imx 
---
 drivers/clk/imx/clk-imx8qm.c | 24 
 1 file changed, 24 insertions(+)

diff --git a/drivers/clk/imx/clk-imx8qm.c b/drivers/clk/imx/clk-imx8qm.c
index 54fb09fda4..7e466d630a 100644
--- a/drivers/clk/imx/clk-imx8qm.c
+++ b/drivers/clk/imx/clk-imx8qm.c
@@ -53,19 +53,27 @@ ulong imx8_clk_get_rate(struct clk *clk)
resource = SC_R_A53;
pm_clk = SC_PM_CLK_CPU;
break;
+   case IMX8QM_I2C0_IPG_CLK:
case IMX8QM_I2C0_CLK:
+   case IMX8QM_I2C0_DIV:
resource = SC_R_I2C_0;
pm_clk = SC_PM_CLK_PER;
break;
+   case IMX8QM_I2C1_IPG_CLK:
case IMX8QM_I2C1_CLK:
+   case IMX8QM_I2C1_DIV:
resource = SC_R_I2C_1;
pm_clk = SC_PM_CLK_PER;
break;
+   case IMX8QM_I2C2_IPG_CLK:
case IMX8QM_I2C2_CLK:
+   case IMX8QM_I2C2_DIV:
resource = SC_R_I2C_2;
pm_clk = SC_PM_CLK_PER;
break;
+   case IMX8QM_I2C3_IPG_CLK:
case IMX8QM_I2C3_CLK:
+   case IMX8QM_I2C3_DIV:
resource = SC_R_I2C_3;
pm_clk = SC_PM_CLK_PER;
break;
@@ -148,19 +156,27 @@ ulong imx8_clk_set_rate(struct clk *clk, unsigned long 
rate)
debug("%s(#%lu), rate: %lu\n", __func__, clk->id, rate);
 
switch (clk->id) {
+   case IMX8QM_I2C0_IPG_CLK:
case IMX8QM_I2C0_CLK:
+   case IMX8QM_I2C0_DIV:
resource = SC_R_I2C_0;
pm_clk = SC_PM_CLK_PER;
break;
+   case IMX8QM_I2C1_IPG_CLK:
case IMX8QM_I2C1_CLK:
+   case IMX8QM_I2C1_DIV:
resource = SC_R_I2C_1;
pm_clk = SC_PM_CLK_PER;
break;
+   case IMX8QM_I2C2_IPG_CLK:
case IMX8QM_I2C2_CLK:
+   case IMX8QM_I2C2_DIV:
resource = SC_R_I2C_2;
pm_clk = SC_PM_CLK_PER;
break;
+   case IMX8QM_I2C3_IPG_CLK:
case IMX8QM_I2C3_CLK:
+   case IMX8QM_I2C3_DIV:
resource = SC_R_I2C_3;
pm_clk = SC_PM_CLK_PER;
break;
@@ -242,19 +258,27 @@ int __imx8_clk_enable(struct clk *clk, bool enable)
debug("%s(#%lu)\n", __func__, clk->id);
 
switch (clk->id) {
+   case IMX8QM_I2C0_IPG_CLK:
case IMX8QM_I2C0_CLK:
+   case IMX8QM_I2C0_DIV:
resource = SC_R_I2C_0;
pm_clk = SC_PM_CLK_PER;
break;
+   case IMX8QM_I2C1_IPG_CLK:
case IMX8QM_I2C1_CLK:
+   case IMX8QM_I2C1_DIV:
resource = SC_R_I2C_1;
pm_clk = SC_PM_CLK_PER;
break;
+   case IMX8QM_I2C2_IPG_CLK:
case IMX8QM_I2C2_CLK:
+   case IMX8QM_I2C2_DIV:
resource = SC_R_I2C_2;
pm_clk = SC_PM_CLK_PER;
break;
+   case IMX8QM_I2C3_IPG_CLK:
case IMX8QM_I2C3_CLK:
+   case IMX8QM_I2C3_DIV:
resource = SC_R_I2C_3;
pm_clk = SC_PM_CLK_PER;
break;
-- 
2.17.1



Re: I2C on imx8qm - Failed to enable ipg clk

2020-11-12 Thread Oliver Graute
On 10/08/20, Oliver Graute wrote:
> On 05/08/20, Anatolij Gustschin wrote:
> > Hi Oliver,
> > 
> > On Wed, 5 Aug 2020 15:47:07 +0200
> > Oliver Graute oliver.gra...@gmail.com wrote:
> > 
> > > Hello,
> > > 
> > > I try to get my I2C working on imx8qm. But I run into this issue:
> > > 
> > > => i2c bus  
> > > Bus 3:  i2c@5a83
> > > => i2c dev 3  
> > > Setting bus to 3
> > > Failed to enable ipg clk
> > > Failure changing bus number (-524)
> > > 
> > > Some idea how to fix that?
> > > 
> > > I'am using U-Boot 2020.04
> > 
> > I think the clock driver (in drivers/clk/imx/clk-imx8qm.c) does not
> > have support for IMX8QM_I2C*_IPG_CLK clocks yet.
> 
> ok, what I have to do to add support for it? just adding
> IMX8QM_I2C*_IPG_CLK to the imx8_clk_names[]?

ok I solved this issue this way:

diff --git a/drivers/clk/imx/clk-imx8qm.c b/drivers/clk/imx/clk-imx8qm.c
index 54fb09fda4..7e466d630a 100644
--- a/drivers/clk/imx/clk-imx8qm.c
+++ b/drivers/clk/imx/clk-imx8qm.c
@@ -53,19 +53,27 @@ ulong imx8_clk_get_rate(struct clk *clk)
resource = SC_R_A53;
pm_clk = SC_PM_CLK_CPU;
break;
+   case IMX8QM_I2C0_IPG_CLK:
case IMX8QM_I2C0_CLK:
+   case IMX8QM_I2C0_DIV:
resource = SC_R_I2C_0;
pm_clk = SC_PM_CLK_PER;
break;
+   case IMX8QM_I2C1_IPG_CLK:
case IMX8QM_I2C1_CLK:
+   case IMX8QM_I2C1_DIV:
resource = SC_R_I2C_1;
pm_clk = SC_PM_CLK_PER;
break;
+   case IMX8QM_I2C2_IPG_CLK:
case IMX8QM_I2C2_CLK:
+   case IMX8QM_I2C2_DIV:
resource = SC_R_I2C_2;
pm_clk = SC_PM_CLK_PER;
break;
+   case IMX8QM_I2C3_IPG_CLK:
case IMX8QM_I2C3_CLK:
+   case IMX8QM_I2C3_DIV:
resource = SC_R_I2C_3;
pm_clk = SC_PM_CLK_PER;
break;
@@ -148,19 +156,27 @@ ulong imx8_clk_set_rate(struct clk *clk, unsigned long 
rate)
debug("%s(#%lu), rate: %lu\n", __func__, clk->id, rate);
 
switch (clk->id) {
+   case IMX8QM_I2C0_IPG_CLK:
case IMX8QM_I2C0_CLK:
+   case IMX8QM_I2C0_DIV:
resource = SC_R_I2C_0;
pm_clk = SC_PM_CLK_PER;
break;
+   case IMX8QM_I2C1_IPG_CLK:
case IMX8QM_I2C1_CLK:
+   case IMX8QM_I2C1_DIV:
resource = SC_R_I2C_1;
pm_clk = SC_PM_CLK_PER;
break;
+   case IMX8QM_I2C2_IPG_CLK:
case IMX8QM_I2C2_CLK:
+   case IMX8QM_I2C2_DIV:
resource = SC_R_I2C_2;
pm_clk = SC_PM_CLK_PER;
break;
+   case IMX8QM_I2C3_IPG_CLK:
case IMX8QM_I2C3_CLK:
+   case IMX8QM_I2C3_DIV:
resource = SC_R_I2C_3;
pm_clk = SC_PM_CLK_PER;
break;
@@ -242,19 +258,27 @@ int __imx8_clk_enable(struct clk *clk, bool enable)
debug("%s(#%lu)\n", __func__, clk->id);
 
switch (clk->id) {
+   case IMX8QM_I2C0_IPG_CLK:
case IMX8QM_I2C0_CLK:
+   case IMX8QM_I2C0_DIV:
resource = SC_R_I2C_0;
pm_clk = SC_PM_CLK_PER;
break;
+   case IMX8QM_I2C1_IPG_CLK:
case IMX8QM_I2C1_CLK:
+   case IMX8QM_I2C1_DIV:
resource = SC_R_I2C_1;
pm_clk = SC_PM_CLK_PER;
break;
+   case IMX8QM_I2C2_IPG_CLK:
case IMX8QM_I2C2_CLK:
+   case IMX8QM_I2C2_DIV:
resource = SC_R_I2C_2;
pm_clk = SC_PM_CLK_PER;
break;
+   case IMX8QM_I2C3_IPG_CLK:
case IMX8QM_I2C3_CLK:
+   case IMX8QM_I2C3_DIV:
resource = SC_R_I2C_3;
pm_clk = SC_PM_CLK_PER;
break;

Best regards,

Oliver


binbmGAU2GcZ3.bin
Description: PGP-Schlüssel 0x00C0A76606F3A5480813234EEF50CD4BA60B1294.


RE: [v3 08/16] net: designware: socfpga: Add ATF support for MAC driver

2020-11-12 Thread Tan, Ley Foon



> -Original Message-
> From: Lim, Elly Siew Chin 
> Sent: Thursday, October 15, 2020 8:30 PM
> To: u-boot@lists.denx.de
> Cc: Marek Vasut ; Tan, Ley Foon
> ; See, Chin Liang ;
> Simon Goldschmidt ; Chee, Tien Fong
> ; Westergreen, Dalon
> ; Simon Glass ; Ang,
> Chee Hong ; Lim, Elly Siew Chin
> 
> Subject: [v3 08/16] net: designware: socfpga: Add ATF support for MAC
> driver
> 
> From: Chee Hong Ang 
> 
> In non-secure mode (EL2), MAC driver calls the SMC/PSCI services provided
> by ATF to setup the PHY interface.
> 
> Signed-off-by: Chee Hong Ang 
> Signed-off-by: Siew Chin Lim 
> ---
>  drivers/net/dwmac_socfpga.c | 30 ++
>  1 file changed, 26 insertions(+), 4 deletions(-)
> 
> diff --git a/drivers/net/dwmac_socfpga.c b/drivers/net/dwmac_socfpga.c
> index e93561dffa..a2d09d5c4a 100644
> --- a/drivers/net/dwmac_socfpga.c
> +++ b/drivers/net/dwmac_socfpga.c
> @@ -17,7 +17,9 @@
>  #include 
>  #include 
> 
> +#include 
>  #include 
> +#include 
Move this to after . 
> 
>  struct dwmac_socfpga_platdata {
>   struct dw_eth_pdata dw_eth_pdata;
> @@ -64,6 +66,27 @@ static int dwmac_socfpga_ofdata_to_platdata(struct
> udevice *dev)
>   return designware_eth_ofdata_to_platdata(dev);
>  }
> 
> +static int dwmac_socfpga_do_setphy(struct udevice *dev, u32 modereg) {
> + struct dwmac_socfpga_platdata *pdata = dev_get_platdata(dev);
> +
> +#if !defined(CONFIG_SPL_BUILD) && defined(CONFIG_SPL_ATF)
> + u64 args[2];
> +
> + args[0] = ((u64)pdata->phy_intf - socfpga_get_sysmgr_addr() -
> +SYSMGR_SOC64_EMAC0) >> 2;
> + args[1] = modereg;
> +
> + if (invoke_smc(INTEL_SIP_SMC_HPS_SET_PHYINTF, args, 2, NULL, 0))
> + return -EIO;
Can you if check ATF support this SMC call already? I didn't see it.


Regards
Ley Foon


RE: [v3 07/16] mmc: dwmmc: socfpga: Add ATF support for MMC driver

2020-11-12 Thread Tan, Ley Foon



> -Original Message-
> From: Lim, Elly Siew Chin 
> Sent: Thursday, October 15, 2020 8:30 PM
> To: u-boot@lists.denx.de
> Cc: Marek Vasut ; Tan, Ley Foon
> ; See, Chin Liang ;
> Simon Goldschmidt ; Chee, Tien Fong
> ; Westergreen, Dalon
> ; Simon Glass ; Ang,
> Chee Hong ; Lim, Elly Siew Chin
> 
> Subject: [v3 07/16] mmc: dwmmc: socfpga: Add ATF support for MMC driver
> 
> From: Chee Hong Ang 
> 
> In non-secure mode (EL2), MMC driver calls the SMC/PSCI services provided
> by ATF to set SDMMC's DRVSEL and SMPLSEL.
> 
> Signed-off-by: Chee Hong Ang 
> Signed-off-by: Siew Chin Lim 
> ---
>  drivers/mmc/socfpga_dw_mmc.c | 17 +
>  1 file changed, 17 insertions(+)
> 
> diff --git a/drivers/mmc/socfpga_dw_mmc.c
> b/drivers/mmc/socfpga_dw_mmc.c index 0022f943bd..e5f68fa7fb 100644
> --- a/drivers/mmc/socfpga_dw_mmc.c
> +++ b/drivers/mmc/socfpga_dw_mmc.c
> @@ -6,6 +6,7 @@
>  #include 
>  #include 
>  #include 
> +#include 
>  #include 
>  #include 
>  #include 
> @@ -13,6 +14,7 @@
>  #include 
>  #include 
>  #include 
> +#include 
>  #include 
>  #include 
>  #include 
> @@ -48,6 +50,10 @@ static void socfpga_dwmci_reset(struct udevice *dev)
> 
>  static void socfpga_dwmci_clksel(struct dwmci_host *host)  {
> +#if !defined(CONFIG_SPL_BUILD) && defined(CONFIG_SPL_ATF)
> + u64 args[2];
> +#endif
> +
>   struct dwmci_socfpga_priv_data *priv = host->priv;
>   u32 sdmmc_mask = ((priv->smplsel & 0x7) <<
> SYSMGR_SDMMC_SMPLSEL_SHIFT) |
>((priv->drvsel & 0x7) <<
> SYSMGR_SDMMC_DRVSEL_SHIFT); @@ -58,10 +64,21 @@ static void
> socfpga_dwmci_clksel(struct dwmci_host *host)
> 
>   debug("%s: drvsel %d smplsel %d\n", __func__,
> priv->drvsel, priv->smplsel);
> +
> +#if !defined(CONFIG_SPL_BUILD) && defined(CONFIG_SPL_ATF)
> + /* drvsel */
> + args[0] = (sdmmc_mask >> SYSMGR_SDMMC_DRVSEL_SHIFT) & 0x7;
> + /* smplsel */
> + args[1] = (sdmmc_mask >> SYSMGR_SDMMC_SMPLSEL_SHIFT) & 0x7;
> + if (invoke_smc(INTEL_SIP_SMC_HPS_SET_SDMMC_CCLK, args, 2,
> NULL, 0))
> + dev_err(host->mmc->dev, "SMC call failed in %s\n",
> __func__);
> +
> +#else
>   writel(sdmmc_mask, socfpga_get_sysmgr_addr() +
> SYSMGR_SDMMC);
> 
>   debug("%s: SYSMGR_SDMMCGRP_CTRL_REG = 0x%x\n", __func__,
>   readl(socfpga_get_sysmgr_addr() + SYSMGR_SDMMC));
> +#endif
Can move these code to new function, easier to read.

Regards
Ley Foon



RE: [v3 02/16] arm: socfpga: soc64: Load FIT image with ATF support

2020-11-12 Thread Tan, Ley Foon



> -Original Message-
> From: Lim, Elly Siew Chin 
> Sent: Thursday, October 15, 2020 8:30 PM
> To: u-boot@lists.denx.de
> Cc: Marek Vasut ; Tan, Ley Foon
> ; See, Chin Liang ;
> Simon Goldschmidt ; Chee, Tien Fong
> ; Westergreen, Dalon
> ; Simon Glass ; Ang,
> Chee Hong ; Lim, Elly Siew Chin
> 
> Subject: [v3 02/16] arm: socfpga: soc64: Load FIT image with ATF support
> 
> From: Chee Hong Ang 
> 
> Instead of loading u-boot proper image (u-boot.img), SPL now loads FIT
> image (u-boot.itb) which includes u-boot proper, ATF and u-boot proper's
> DTB.
> 
> For OS, u-boot now loads FIT images (kernel.itb) which includes Linux Image
> and Linux's DTB.
> 
> Signed-off-by: Chee Hong Ang 
> Signed-off-by: Siew Chin Lim 

Reviewed-by: Ley Foon Tan 


Re: [PATCH v2 2/3] mmc: meson-gx: change clock phase value on SM1 SoCs

2020-11-12 Thread Anand Moon
Hi Jaehoon,

On Wed, 11 Nov 2020 at 04:52, Jaehoon Chung  wrote:
>
> From: Neil Armstrong 
>
> Amlogic SM1 SoCs doesn't work over 50MHz. When phase sets to 270', it's
> working fine over 50MHz on Amlogic SM1 SoCs.
> Since Other Amlogic SoCs doens't report an issue, phase value is using
> to 180' by default.
>
> To distinguish which value is used adds an u-boot only sm1 compatible.
>
> In future, it needs to find what value is a proper about each SoCs.
>
> Signed-off-by: Neil Armstrong 
> Signed-off-by: Jaehoon Chung 
> ---

Thanks for this patch series.
I have tested this on Odroid N2 and c4 eMMC module
so Please add my for this series,

Tested-by: Anand Moon 

>  drivers/mmc/meson_gx_mmc.c | 27 +++
>  drivers/mmc/meson_gx_mmc.h |  5 +
>  2 files changed, 28 insertions(+), 4 deletions(-)
>
> diff --git a/drivers/mmc/meson_gx_mmc.c b/drivers/mmc/meson_gx_mmc.c
> index eedebb317b5f..a5e9ac5637b9 100644
> --- a/drivers/mmc/meson_gx_mmc.c
> +++ b/drivers/mmc/meson_gx_mmc.c
> @@ -17,6 +17,14 @@
>  #include 
>  #include "meson_gx_mmc.h"
>
> +bool meson_gx_mmc_is_compatible(struct udevice *dev,
> +   enum meson_gx_mmc_compatible family)
> +{
> +   enum meson_gx_mmc_compatible compat = dev_get_driver_data(dev);
> +
> +   return compat == family;
> +}
> +
>  static inline void *get_regbase(const struct mmc *mmc)
>  {
> struct meson_mmc_platdata *pdata = mmc->priv;
> @@ -42,6 +50,8 @@ static void meson_mmc_config_clock(struct mmc *mmc)
> if (!mmc->clock)
> return;
>
> +   /* TOFIX This should use the proper clock taken from DT */
> +
> /* 1GHz / CLK_MAX_DIV = 15,9 MHz */
> if (mmc->clock > 1600) {
> clk = SD_EMMC_CLKSRC_DIV2;
> @@ -52,8 +62,16 @@ static void meson_mmc_config_clock(struct mmc *mmc)
> }
> clk_div = DIV_ROUND_UP(clk, mmc->clock);
>
> -   /* 180 phase core clock */
> -   meson_mmc_clk |= CLK_CO_PHASE_180;
> +   /*
> +* SM1 SoCs doesn't work fine over 50MHz with CLK_CO_PHASE_180
> +* If CLK_CO_PHASE_270 is used, it's more stable than other.
> +* Other SoCs use CLK_CO_PHASE_180 by default.
> +* It needs to find what is a proper value about each SoCs.
> +*/
> +   if (meson_gx_mmc_is_compatible(mmc->dev, MMC_COMPATIBLE_SM1))
> +   meson_mmc_clk |= CLK_CO_PHASE_270;
> +   else
> +   meson_mmc_clk |= CLK_CO_PHASE_180;
>
> /* 180 phase tx clock */
> meson_mmc_clk |= CLK_TX_PHASE_000;
> @@ -308,8 +326,9 @@ int meson_mmc_bind(struct udevice *dev)
>  }
>
>  static const struct udevice_id meson_mmc_match[] = {
> -   { .compatible = "amlogic,meson-gx-mmc" },
> -   { .compatible = "amlogic,meson-axg-mmc" },
> +   { .compatible = "amlogic,meson-gx-mmc", .data = MMC_COMPATIBLE_GX },
> +   { .compatible = "amlogic,meson-axg-mmc", .data = MMC_COMPATIBLE_GX },
> +   { .compatible = "amlogic,meson-sm1-mmc", .data = MMC_COMPATIBLE_SM1 },
> { /* sentinel */ }
>  };
>
> diff --git a/drivers/mmc/meson_gx_mmc.h b/drivers/mmc/meson_gx_mmc.h
> index b4544b55628f..92aec5329f6e 100644
> --- a/drivers/mmc/meson_gx_mmc.h
> +++ b/drivers/mmc/meson_gx_mmc.h
> @@ -9,6 +9,11 @@
>  #include 
>  #include 
>
> +enum meson_gx_mmc_compatible {
> +   MMC_COMPATIBLE_GX,
> +   MMC_COMPATIBLE_SM1,
> +};
> +
>  #define SDIO_PORT_A0
>  #define SDIO_PORT_B1
>  #define SDIO_PORT_C2
> --
> 2.29.0
>


[PATCH v4 22/23] reset: reset-mtmips: add DM_FLAG_PRE_RELOC flag

2020-11-12 Thread Weijie Gao
Add DM_FLAG_PRE_RELOC flag for reset-mtmips to make sure this driver can
be probed before relocation even if u-boot,dm-pre-reloc is not present
in the dts.

Reviewed-by: Stefan Roese 
Signed-off-by: Weijie Gao 
---
v4 changes: none
v3 changes: none
v2 changes: new
---
 drivers/reset/reset-mtmips.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/drivers/reset/reset-mtmips.c b/drivers/reset/reset-mtmips.c
index bc20e3335c..0d6a446465 100644
--- a/drivers/reset/reset-mtmips.c
+++ b/drivers/reset/reset-mtmips.c
@@ -82,4 +82,5 @@ U_BOOT_DRIVER(mtmips_reset) = {
.probe = mtmips_reset_probe,
.priv_auto_alloc_size = sizeof(struct mtmips_reset_priv),
.ops = _reset_ops,
+   .flags = DM_FLAG_PRE_RELOC,
 };
-- 
2.17.1


[PATCH v4 23/23] MAINTAINERS: add maintainer for MediaTek MIPS platform

2020-11-12 Thread Weijie Gao
Update maintainer for MediaTek MIPS platform

Acked-by: Stefan Roese 
Reviewed-by: Stefan Roese 
Signed-off-by: Weijie Gao 
---
v4 changes: none
v3 changes: none
v2 changes: none
---
 MAINTAINERS | 23 +++
 1 file changed, 23 insertions(+)

diff --git a/MAINTAINERS b/MAINTAINERS
index 874cf2c0e5..0123afe7cf 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -802,6 +802,29 @@ F: drivers/mmc/ca_dw_mmc.c
 F: drivers/i2c/i2c-cortina.c
 F: drivers/i2c/i2c-cortina.h
 
+MIPS MEDIATEK
+M: Weijie Gao 
+R: GSS_MTK_Uboot_upstream 
+S: Maintained
+F: arch/mips/mach-mtmips/
+F: arch/mips/dts/mt7620.dtsi
+F: arch/mips/dts/mt7620-u-boot.dtsi
+F: include/configs/mt7620.h
+F: include/dt-bindings/clock/mt7620-clk.h
+F: include/dt-bindings/clock/mt7628-clk.h
+F: include/dt-bindings/reset/mt7620-reset.h
+F: include/dt-bindings/reset/mt7628-reset.h
+F: drivers/clk/mtmips/
+F: drivers/pinctrl/mtmips/
+F: drivers/gpio/mt7620_gpio.c
+F: drivers/net/mt7620-eth.c
+F: drivers/phy/mt7620-usb-phy.c
+F: drivers/reset/reset-mtmips.c
+F: drivers/serial/serial_mt7620.c
+F: drivers/spi/mt7620_spi.c
+F: drivers/sysreset/sysreset_resetctl.c
+F: drivers/watchdog/mt7620_wdt.c
+
 MIPS MSCC
 M: Gregory CLEMENT 
 M: Lars Povlsen 
-- 
2.17.1


[PATCH v4 19/23] mmc: mtk-sd: fix sclk cycles shift value

2020-11-12 Thread Weijie Gao
It turns out that the sclk cycles used by mt7620/mt7628 is the same as
other chips (20 bits, 1048576), not 65536.

This patch removes sclk_cycle_shift from dev_comp, and uses a macro with
a value 20 instead.

Reviewed-by: Stefan Roese 
Signed-off-by: Weijie Gao 
---
v4 changes: none
v3 changes: new
---
 drivers/mmc/mtk-sd.c | 13 -
 1 file changed, 4 insertions(+), 9 deletions(-)

diff --git a/drivers/mmc/mtk-sd.c b/drivers/mmc/mtk-sd.c
index 4f9fa7d0ec..63ff68272c 100644
--- a/drivers/mmc/mtk-sd.c
+++ b/drivers/mmc/mtk-sd.c
@@ -166,6 +166,8 @@
 
 #define DEFAULT_CD_DEBOUNCE8
 
+#define SCLK_CYCLES_SHIFT  20
+
 #define CMD_INTS_MASK  \
(MSDC_INT_CMDRDY | MSDC_INT_RSPCRCERR | MSDC_INT_CMDTMO)
 
@@ -256,7 +258,6 @@ struct msdc_top_regs {
 
 struct msdc_compatible {
u8 clk_div_bits;
-   u8 sclk_cycle_shift;
bool pad_tune0;
bool async_fifo;
bool data_tune;
@@ -722,7 +723,7 @@ static int msdc_ops_send_cmd(struct udevice *dev, struct 
mmc_cmd *cmd,
 
 static void msdc_set_timeout(struct msdc_host *host, u32 ns, u32 clks)
 {
-   u32 timeout, clk_ns, shift;
+   u32 timeout, clk_ns, shift = SCLK_CYCLES_SHIFT;
u32 mode = 0;
 
host->timeout_ns = ns;
@@ -731,7 +732,6 @@ static void msdc_set_timeout(struct msdc_host *host, u32 
ns, u32 clks)
if (host->sclk == 0) {
timeout = 0;
} else {
-   shift = host->dev_comp->sclk_cycle_shift;
clk_ns = 10UL / host->sclk;
timeout = (ns + clk_ns - 1) / clk_ns + clks;
/* unit is 1048576 sclk cycles */
@@ -1531,7 +1531,7 @@ static int msdc_drv_probe(struct udevice *dev)
 
host->mmc = >mmc;
host->timeout_ns = 1;
-   host->timeout_clks = 3 * (1 << host->dev_comp->sclk_cycle_shift);
+   host->timeout_clks = 3 * (1 << SCLK_CYCLES_SHIFT);
 
 #ifdef CONFIG_PINCTRL
pinctrl_select_state(dev, "default");
@@ -1615,7 +1615,6 @@ static const struct dm_mmc_ops msdc_ops = {
 
 static const struct msdc_compatible mt7620_compat = {
.clk_div_bits = 8,
-   .sclk_cycle_shift = 16,
.pad_tune0 = false,
.async_fifo = false,
.data_tune = false,
@@ -1635,7 +1634,6 @@ static const struct msdc_compatible mt7622_compat = {
 
 static const struct msdc_compatible mt7623_compat = {
.clk_div_bits = 12,
-   .sclk_cycle_shift = 20,
.pad_tune0 = true,
.async_fifo = true,
.data_tune = true,
@@ -1646,7 +1644,6 @@ static const struct msdc_compatible mt7623_compat = {
 
 static const struct msdc_compatible mt8512_compat = {
.clk_div_bits = 12,
-   .sclk_cycle_shift = 20,
.pad_tune0 = true,
.async_fifo = true,
.data_tune = true,
@@ -1656,7 +1653,6 @@ static const struct msdc_compatible mt8512_compat = {
 
 static const struct msdc_compatible mt8516_compat = {
.clk_div_bits = 12,
-   .sclk_cycle_shift = 20,
.pad_tune0 = true,
.async_fifo = true,
.data_tune = true,
@@ -1666,7 +1662,6 @@ static const struct msdc_compatible mt8516_compat = {
 
 static const struct msdc_compatible mt8183_compat = {
.clk_div_bits = 12,
-   .sclk_cycle_shift = 20,
.pad_tune0 = true,
.async_fifo = true,
.data_tune = true,
-- 
2.17.1


[PATCH v4 21/23] mmc: mtk-sd: assign plat->cfg.f_max with a correct value

2020-11-12 Thread Weijie Gao
Currently this driver does not set the value of plat->cfg.f_max, which
results in that MMC framework will always run at the lowest frequency.

This patch sets plat->cfg.f_max to the maximum allowed frequency, which
equals to the source clock frequency.

Reviewed-by: Stefan Roese 
Signed-off-by: Weijie Gao 
---
v4 changes: change plat->cfg.f_max to equal to host->src_clk_freq
v3 changes: new
---
 drivers/mmc/mtk-sd.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/drivers/mmc/mtk-sd.c b/drivers/mmc/mtk-sd.c
index f4ff8822f2..77fda0eab0 100644
--- a/drivers/mmc/mtk-sd.c
+++ b/drivers/mmc/mtk-sd.c
@@ -1639,6 +1639,8 @@ static int msdc_drv_probe(struct udevice *dev)
else
cfg->f_min = host->src_clk_freq / (4 * 4095);
 
+   cfg->f_max = host->src_clk_freq;
+
cfg->b_max = 1024;
cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
 
-- 
2.17.1


[PATCH v4 20/23] mmc: mtk-sd: add pad control settings for MediaTek MT7620/MT76x8 SoCs

2020-11-12 Thread Weijie Gao
The driver is missing pad control settings (pad delay and pad conf) for
the mt7620 and mt76x8. Although mt76x8 still works well, mt7620 will
encounter CRC error on data transfers.

This patch adds default pad control settings for mt7620_compat.

Reviewed-by: Stefan Roese 
Signed-off-by: Weijie Gao 
---
v4 changes: none
v3 changes: none
v2 changes: none
---
 drivers/mmc/mtk-sd.c | 121 +--
 1 file changed, 118 insertions(+), 3 deletions(-)

diff --git a/drivers/mmc/mtk-sd.c b/drivers/mmc/mtk-sd.c
index 63ff68272c..f4ff8822f2 100644
--- a/drivers/mmc/mtk-sd.c
+++ b/drivers/mmc/mtk-sd.c
@@ -113,7 +113,51 @@
 #define MSDC_PB2_RESPWAIT_M0x0c
 #define MSDC_PB2_RESPWAIT_S2
 
+/* MSDC_PAD_CTRL0 */
+#define MSDC_PAD_CTRL0_CLKRDSEL_M  0xff00
+#define MSDC_PAD_CTRL0_CLKRDSEL_S  24
+#define MSDC_PAD_CTRL0_CLKTDSELBIT(20)
+#define MSDC_PAD_CTRL0_CLKIES  BIT(19)
+#define MSDC_PAD_CTRL0_CLKSMT  BIT(18)
+#define MSDC_PAD_CTRL0_CLKPU   BIT(17)
+#define MSDC_PAD_CTRL0_CLKPD   BIT(16)
+#define MSDC_PAD_CTRL0_CLKSR   BIT(8)
+#define MSDC_PAD_CTRL0_CLKDRVP_M   0x70
+#define MSDC_PAD_CTRL0_CLKDRVP_S   4
+#define MSDC_PAD_CTRL0_CLKDRVN_M   0x7
+#define MSDC_PAD_CTRL0_CLKDRVN_S   0
+
+/* MSDC_PAD_CTRL1 */
+#define MSDC_PAD_CTRL1_CMDRDSEL_M  0xff00
+#define MSDC_PAD_CTRL1_CMDRDSEL_S  24
+#define MSDC_PAD_CTRL1_CMDTDSELBIT(20)
+#define MSDC_PAD_CTRL1_CMDIES  BIT(19)
+#define MSDC_PAD_CTRL1_CMDSMT  BIT(18)
+#define MSDC_PAD_CTRL1_CMDPU   BIT(17)
+#define MSDC_PAD_CTRL1_CMDPD   BIT(16)
+#define MSDC_PAD_CTRL1_CMDSR   BIT(8)
+#define MSDC_PAD_CTRL1_CMDDRVP_M   0x70
+#define MSDC_PAD_CTRL1_CMDDRVP_S   4
+#define MSDC_PAD_CTRL1_CMDDRVN_M   0x7
+#define MSDC_PAD_CTRL1_CMDDRVN_S   0
+
+/* MSDC_PAD_CTRL2 */
+#define MSDC_PAD_CTRL2_DATRDSEL_M  0xff00
+#define MSDC_PAD_CTRL2_DATRDSEL_S  24
+#define MSDC_PAD_CTRL2_DATTDSELBIT(20)
+#define MSDC_PAD_CTRL2_DATIES  BIT(19)
+#define MSDC_PAD_CTRL2_DATSMT  BIT(18)
+#define MSDC_PAD_CTRL2_DATPU   BIT(17)
+#define MSDC_PAD_CTRL2_DATPD   BIT(16)
+#define MSDC_PAD_CTRL2_DATSR   BIT(8)
+#define MSDC_PAD_CTRL2_DATDRVP_M   0x70
+#define MSDC_PAD_CTRL2_DATDRVP_S   4
+#define MSDC_PAD_CTRL2_DATDRVN_M   0x7
+#define MSDC_PAD_CTRL2_DATDRVN_S   0
+
 /* PAD_TUNE */
+#define MSDC_PAD_TUNE_CLKTDLY_M0xf800
+#define MSDC_PAD_TUNE_CLKTDLY_S27
 #define MSDC_PAD_TUNE_CMDRRDLY_M   0x7c0
 #define MSDC_PAD_TUNE_CMDRRDLY_S   22
 #define MSDC_PAD_TUNE_CMD_SEL  BIT(21)
@@ -129,6 +173,26 @@
 #define PAD_CMD_TUNE_RX_DLY3   0x3E
 #define PAD_CMD_TUNE_RX_DLY3_S 1
 
+/* PAD_TUNE0 */
+#define MSDC_PAD_TUNE0_DAT0RDDLY_M 0x1f00
+#define MSDC_PAD_TUNE0_DAT0RDDLY_S 24
+#define MSDC_PAD_TUNE0_DAT1RDDLY_M 0x1f
+#define MSDC_PAD_TUNE0_DAT1RDDLY_S 16
+#define MSDC_PAD_TUNE0_DAT2RDDLY_M 0x1f00
+#define MSDC_PAD_TUNE0_DAT2RDDLY_S 8
+#define MSDC_PAD_TUNE0_DAT3RDDLY_M 0x1f
+#define MSDC_PAD_TUNE0_DAT3RDDLY_S 0
+
+/* PAD_TUNE1 */
+#define MSDC_PAD_TUNE1_DAT4RDDLY_M 0x1f00
+#define MSDC_PAD_TUNE1_DAT4RDDLY_S 24
+#define MSDC_PAD_TUNE1_DAT5RDDLY_M 0x1f
+#define MSDC_PAD_TUNE1_DAT5RDDLY_S 16
+#define MSDC_PAD_TUNE1_DAT6RDDLY_M 0x1f00
+#define MSDC_PAD_TUNE1_DAT6RDDLY_S 8
+#define MSDC_PAD_TUNE1_DAT7RDDLY_M 0x1f
+#define MSDC_PAD_TUNE1_DAT7RDDLY_S 0
+
 /* EMMC50_CFG0 */
 #define EMMC50_CFG_CFCSTS_SEL  BIT(4)
 
@@ -223,7 +287,10 @@ struct mtk_sd_regs {
u32 dat3_tune_crc;
u32 cmd_tune_crc;
u32 sdio_tune_wind;
-   u32 reserved4[5];
+   u32 reserved4[2];
+   u32 pad_ctrl0;
+   u32 pad_ctrl1;
+   u32 pad_ctrl2;
u32 pad_tune;
u32 pad_tune0;
u32 pad_tune1;
@@ -264,6 +331,8 @@ struct msdc_compatible {
bool busy_check;
bool stop_clk_fix;
bool enhance_rx;
+   bool builtin_pad_ctrl;
+   bool default_pad_dly;
 };
 
 struct msdc_delay_phase {
@@ -1391,9 +1460,14 @@ static void msdc_init_hw(struct msdc_host *host)
 {
u32 val;
void __iomem *tune_reg = >base->pad_tune;
+   void __iomem *rd_dly0_reg = >base->pad_tune0;
+   void __iomem *rd_dly1_reg = >base->pad_tune1;
 
-   if (host->dev_comp->pad_tune0)
+   if (host->dev_comp->pad_tune0) {
tune_reg = >base->pad_tune0;
+   rd_dly0_reg = >base->dat_rd_dly[0];
+   rd_dly1_reg = >base->dat_rd_dly[1];
+   }
 
/* Configure to MMC/SD mode, clock free running */
setbits_le32(>base->msdc_cfg, MSDC_CFG_MODE);
@@ -1479,6 +1553,45 @@ static void msdc_init_hw(struct msdc_host *host)
setbits_le32(tune_reg, MSDC_PAD_TUNE_RXDLYSEL);
}
 
+   if 

[PATCH v4 17/23] phy: add USB PHY driver for MediaTek MT7620 SoC

2020-11-12 Thread Weijie Gao
This patch adds USB PHY driver for MediaTek MT7620 SoC

Reviewed-by: Stefan Roese 
Signed-off-by: Weijie Gao 
---
v4 changes: none
v3 changes: remove duplicated dev field in mt7620_usb_phy
v2 changes: none
---
 drivers/phy/Kconfig  |   7 +++
 drivers/phy/Makefile |   1 +
 drivers/phy/mt7620-usb-phy.c | 110 +++
 3 files changed, 118 insertions(+)
 create mode 100644 drivers/phy/mt7620-usb-phy.c

diff --git a/drivers/phy/Kconfig b/drivers/phy/Kconfig
index d12a6b02ad..ab638f0e7d 100644
--- a/drivers/phy/Kconfig
+++ b/drivers/phy/Kconfig
@@ -218,6 +218,13 @@ config KEYSTONE_USB_PHY
 
  This PHY is found on some Keystone (K2) devices supporting USB.
 
+config MT7620_USB_PHY
+   bool "MediaTek MT7620 USB PHY support"
+   depends on PHY
+   depends on SOC_MT7620
+   help
+  Support the intergated USB PHY in MediaTek MT7620 SoC
+
 config MT76X8_USB_PHY
bool "MediaTek MT76x8 (7628/88) USB PHY support"
depends on PHY
diff --git a/drivers/phy/Makefile b/drivers/phy/Makefile
index 45a7fe5b56..6b3761b8c8 100644
--- a/drivers/phy/Makefile
+++ b/drivers/phy/Makefile
@@ -24,6 +24,7 @@ obj-$(CONFIG_MESON_G12A_USB_PHY) += meson-g12a-usb2.o 
meson-g12a-usb3-pcie.o
 obj-$(CONFIG_MSM8916_USB_PHY) += msm8916-usbh-phy.o
 obj-$(CONFIG_OMAP_USB2_PHY) += omap-usb2-phy.o
 obj-$(CONFIG_KEYSTONE_USB_PHY) += keystone-usb-phy.o
+obj-$(CONFIG_MT7620_USB_PHY) += mt7620-usb-phy.o
 obj-$(CONFIG_MT76X8_USB_PHY) += mt76x8-usb-phy.o
 obj-$(CONFIG_PHY_DA8XX_USB) += phy-da8xx-usb.o
 obj-$(CONFIG_PHY_MTK_TPHY) += phy-mtk-tphy.o
diff --git a/drivers/phy/mt7620-usb-phy.c b/drivers/phy/mt7620-usb-phy.c
new file mode 100644
index 00..8320df177d
--- /dev/null
+++ b/drivers/phy/mt7620-usb-phy.c
@@ -0,0 +1,110 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2020 MediaTek Inc. All Rights Reserved.
+ *
+ * Author: Weijie Gao 
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+struct mt7620_usb_phy {
+   struct udevice *sysc;
+   struct clk_bulk clocks;
+   struct reset_ctl_bulk resets;
+};
+
+static int mt7620_usb_phy_power_on(struct phy *_phy)
+{
+   struct mt7620_usb_phy *phy = dev_get_priv(_phy->dev);
+   u32 mode = MT7620_SYSC_USB_HOST_MODE;
+   int ret;
+
+   reset_deassert_bulk(>resets);
+
+   clk_enable_bulk(>clocks);
+
+   mdelay(10);
+
+   ret = misc_ioctl(phy->sysc, MT7620_SYSC_IOCTL_SET_USB_MODE, );
+   if (ret) {
+   dev_err(_phy->dev,
+   "mt7620_usbphy: failed to set USB host mode\n");
+   return ret;
+   }
+
+   mdelay(10);
+
+   return 0;
+}
+
+static int mt7620_usb_phy_power_off(struct phy *_phy)
+{
+   struct mt7620_usb_phy *phy = dev_get_priv(_phy->dev);
+
+   clk_disable_bulk(>clocks);
+
+   reset_assert_bulk(>resets);
+
+   return 0;
+}
+
+static int mt7620_usb_phy_probe(struct udevice *dev)
+{
+   struct mt7620_usb_phy *phy = dev_get_priv(dev);
+   struct ofnode_phandle_args sysc_args;
+   int ret;
+
+   ret = ofnode_parse_phandle_with_args(dev->node, "mediatek,sysc", NULL,
+0, 0, _args);
+   if (ret) {
+   dev_err(dev, "mt7620_usbphy: sysc property not found\n");
+   return ret;
+   }
+
+   ret = uclass_get_device_by_ofnode(UCLASS_MISC, sysc_args.node,
+ >sysc);
+   if (ret) {
+   dev_err(dev, "mt7620_usbphy: failed to sysc device\n");
+   return ret;
+   }
+
+   ret = clk_get_bulk(dev, >clocks);
+   if (ret) {
+   dev_err(dev, "mt7620_usbphy: failed to get clocks\n");
+   return ret;
+   }
+
+   ret = reset_get_bulk(dev, >resets);
+   if (ret) {
+   dev_err(dev, "mt7620_usbphy: failed to get reset control\n");
+   return ret;
+   }
+
+   return 0;
+}
+
+static struct phy_ops mt7620_usb_phy_ops = {
+   .power_on = mt7620_usb_phy_power_on,
+   .power_off = mt7620_usb_phy_power_off,
+};
+
+static const struct udevice_id mt7620_usb_phy_ids[] = {
+   { .compatible = "mediatek,mt7620-usbphy" },
+   { }
+};
+
+U_BOOT_DRIVER(mt7620_usb_phy) = {
+   .name   = "mt7620_usb_phy",
+   .id = UCLASS_PHY,
+   .of_match   = mt7620_usb_phy_ids,
+   .ops= _usb_phy_ops,
+   .probe  = mt7620_usb_phy_probe,
+   .priv_auto_alloc_size = sizeof(struct mt7620_usb_phy),
+};
-- 
2.17.1


[PATCH v4 18/23] net: add ethernet driver for MediaTek MT7620 SoC

2020-11-12 Thread Weijie Gao
This patch adds  ethernet driver for MediaTek MT7620 SoC.

The MT7620 SoC has a built-in ethernet (Frame Engine) and a built-in
7-port switch and two xMII interfaces (can be MII/RMII/RGMII).

The port 0-3 of the switch connects to intergrited FE PHYs. Port 4 can be
configured to connect to either the intergrited FE PHY, or the xMII.
Port 5 always connects to the xMII. Port 6 is the CPU port.

This driver supports MT7530 giga switch connects to port 5.

Reviewed-by: Stefan Roese 
Signed-off-by: Weijie Gao 
---
v4 changes: none
v3 changes: none
v2 changes: none
---
 drivers/net/Kconfig  |   12 +
 drivers/net/Makefile |1 +
 drivers/net/mt7620-eth.c | 1222 ++
 3 files changed, 1235 insertions(+)
 create mode 100644 drivers/net/mt7620-eth.c

diff --git a/drivers/net/Kconfig b/drivers/net/Kconfig
index 3a5e036880..d1a52c7264 100644
--- a/drivers/net/Kconfig
+++ b/drivers/net/Kconfig
@@ -399,6 +399,18 @@ config MACB_ZYNQ
  The Cadence MACB ethernet interface was used on Zynq platform.
  Say Y to enable support for the MACB/GEM in Zynq chip.
 
+config MT7620_ETH
+   bool "MediaTek MT7620 Ethernet Interface"
+   depends on SOC_MT7620
+   select PHYLIB
+   select DM_RESET
+   select DM_GPIO
+   select CLK
+   help
+ The MediaTek MT7620 ethernet interface is used on MT7620 based
+ boards. It has a built-in switch with two configurable ports which
+ can connect to external PHY/MACs.
+
 config MT7628_ETH
bool "MediaTek MT7628 Ethernet Interface"
depends on SOC_MT7628
diff --git a/drivers/net/Makefile b/drivers/net/Makefile
index e3bdda359d..f2a0df509d 100644
--- a/drivers/net/Makefile
+++ b/drivers/net/Makefile
@@ -44,6 +44,7 @@ obj-$(CONFIG_MDIO_IPQ4019) += mdio-ipq4019.o
 obj-$(CONFIG_MDIO_MUX_I2CREG) += mdio_mux_i2creg.o
 obj-$(CONFIG_MDIO_MUX_SANDBOX) += mdio_mux_sandbox.o
 obj-$(CONFIG_MPC8XX_FEC) += mpc8xx_fec.o
+obj-$(CONFIG_MT7620_ETH) += mt7620-eth.o
 obj-$(CONFIG_MT7628_ETH) += mt7628-eth.o
 obj-$(CONFIG_MVGBE) += mvgbe.o
 obj-$(CONFIG_MVMDIO) += mvmdio.o
diff --git a/drivers/net/mt7620-eth.c b/drivers/net/mt7620-eth.c
new file mode 100644
index 00..2c148a3b35
--- /dev/null
+++ b/drivers/net/mt7620-eth.c
@@ -0,0 +1,1222 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2020 MediaTek Inc.
+ *
+ * Author: Weijie Gao 
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+/* Frame Engine block */
+#define GDMA_BASE  0x600
+#define PDMA_BASE  0x800
+
+/* GDMA registers */
+#define GDMA_FWD_CFG   0x00
+#define GDMA_DST_PORT  GENMASK(2, 0)
+#define   GDMA_DST_PORT_CPU0
+
+#define GDMA_MAC_ADRL  0x08
+#define GDMA_MAC_ADRH  0x0c
+
+/* PDMA registers */
+#define TX_BASE_PTR0   0x000
+#define TX_MAX_CNT00x004
+#define TX_CTX_IDX00x008
+#define TX_DTX_IDX00x00c
+#define RX_BASE_PTR0   0x100
+#define RX_MAX_CNT00x104
+#define RX_CALC_IDX0   0x108
+#define RX_DRX_IDX00x10c
+
+#define PDMA_GLO_CFG   0x204
+#define TX_WB_DDONEBIT(6)
+#define PDMA_BT_SIZE   GENMASK(5, 4)
+#define   PDMA_BT_SIZE_32B 1
+#define RX_DMA_BUSYBIT(3)
+#define RX_DMA_EN  BIT(2)
+#define TX_DMA_BUSYBIT(1)
+#define TX_DMA_EN  BIT(0)
+
+#define PDMA_RST_IDX   0x208
+#define RST_DRX_IDX0   BIT(16)
+#define RST_DTX_IDX0   BIT(0)
+
+/* Built-in giga ethernet switch block */
+
+/* ARL registers */
+#define GSW_MFC0x0010
+#define BC_FFP GENMASK(31, 24)
+#define UNM_FFPGENMASK(23, 16)
+#define UNU_FFPGENMASK(15, 8)
+#define CPU_EN BIT(7)
+#define CPU_PORT   GENMASK(6, 4)
+
+/* Port registers */
+#define GSW_PCR(p) (0x2004 + (p) * 0x100)
+#define PORT_MATRIXGENMASK(23, 16)
+
+#define GSW_PVC(p) (0x2010 + (p) * 0x100)
+#define STAG_VPID  GENMASK(31, 16)
+#define VLAN_ATTR  GENMASK(7, 6)
+#define   VLAN_ATTR_USER   0
+
+/* MAC registers */
+#define GSW_PMCR(p)(0x3000 + (p) * 0x100)
+#define IPG_CFGGENMASK(19, 18)
+#define IPG_96BIT_WITH_SHORT_IPG   1
+#define MAC_MODE   BIT(16)
+#define FORCE_MODE BIT(15)

[PATCH v4 16/23] spi: add spi controller support for MediaTek MT7620 SoC

2020-11-12 Thread Weijie Gao
This patch adds spi controller support for MediaTek MT7620 SoC.

The SPI controller supports two chip selects. These two chip selects are
implemented as two separate register groups, but they share the same bus
(DI/DO/CLK), only CS pins are dedicated for each register group.
Appearently these two register groups cannot operates simulataneously so
they are implemented as one controller.

Reviewed-by: Stefan Roese 
Signed-off-by: Weijie Gao 
---
v4 changes: none
v3 changes: use bool instead of int in for mt7620_spi_set_cs()
v2 changes: none
---
 drivers/spi/Kconfig  |   7 +
 drivers/spi/Makefile |   1 +
 drivers/spi/mt7620_spi.c | 281 +++
 3 files changed, 289 insertions(+)
 create mode 100644 drivers/spi/mt7620_spi.c

diff --git a/drivers/spi/Kconfig b/drivers/spi/Kconfig
index f7a9852565..ec50d843ef 100644
--- a/drivers/spi/Kconfig
+++ b/drivers/spi/Kconfig
@@ -202,6 +202,13 @@ config MSCC_BB_SPI
  Enable MSCC bitbang SPI driver. This driver can be used on
  MSCC SOCs.
 
+config MT7620_SPI
+   bool "MediaTek MT7620 SPI driver"
+   depends on SOC_MT7620
+   help
+ Enable the MT7620 SPI driver. This driver can be used to access
+ generic SPI devices on MediaTek MT7620 SoC.
+
 config MT7621_SPI
bool "MediaTek MT7621 SPI driver"
depends on SOC_MT7628
diff --git a/drivers/spi/Makefile b/drivers/spi/Makefile
index d9b5bd9b79..bfd142d153 100644
--- a/drivers/spi/Makefile
+++ b/drivers/spi/Makefile
@@ -36,6 +36,7 @@ obj-$(CONFIG_MESON_SPIFC) += meson_spifc.o
 obj-$(CONFIG_MPC8XX_SPI) += mpc8xx_spi.o
 obj-$(CONFIG_MPC8XXX_SPI) += mpc8xxx_spi.o
 obj-$(CONFIG_MTK_SNFI_SPI) += mtk_snfi_spi.o
+obj-$(CONFIG_MT7620_SPI) += mt7620_spi.o
 obj-$(CONFIG_MT7621_SPI) += mt7621_spi.o
 obj-$(CONFIG_MSCC_BB_SPI) += mscc_bb_spi.o
 obj-$(CONFIG_MVEBU_A3700_SPI) += mvebu_a3700_spi.o
diff --git a/drivers/spi/mt7620_spi.c b/drivers/spi/mt7620_spi.c
new file mode 100644
index 00..bc61b4fd13
--- /dev/null
+++ b/drivers/spi/mt7620_spi.c
@@ -0,0 +1,281 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2020 MediaTek Inc.
+ *
+ * Author: Weijie Gao 
+ *
+ * Generic SPI driver for MediaTek MT7620 SoC
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#define MT7620_SPI_NUM_CS  2
+#define MT7620_SPI_MASTER1_OFF 0x00
+#define MT7620_SPI_MASTER2_OFF 0x40
+
+/* SPI_STAT */
+#define   SPI_BUSY BIT(0)
+
+/* SPI_CFG */
+#define   MSB_FIRSTBIT(8)
+#define   SPI_CLK_POL  BIT(6)
+#define   RX_CLK_EDGE  BIT(5)
+#define   TX_CLK_EDGE  BIT(4)
+#define   SPI_CLK_S0
+#define   SPI_CLK_MGENMASK(2, 0)
+
+/* SPI_CTL */
+#define   START_WR BIT(2)
+#define   START_RD BIT(1)
+#define   SPI_HIGH BIT(0)
+
+#define SPI_ARB0xf0
+#define   ARB_EN   BIT(31)
+
+#define POLLING_SCALE  10
+#define POLLING_FRAC_USEC  100
+
+struct mt7620_spi_master_regs {
+   u32 stat;
+   u32 reserved0[3];
+   u32 cfg;
+   u32 ctl;
+   u32 reserved1[2];
+   u32 data;
+};
+
+struct mt7620_spi {
+   void __iomem *regs;
+   struct mt7620_spi_master_regs *m[MT7620_SPI_NUM_CS];
+   unsigned int sys_freq;
+   u32 wait_us;
+   uint mode;
+   uint speed;
+};
+
+static void mt7620_spi_master_setup(struct mt7620_spi *ms, int cs)
+{
+   u32 rate, prescale, freq, tmo, cfg;
+
+   /* Calculate the clock divsior */
+   rate = DIV_ROUND_UP(ms->sys_freq, ms->speed);
+   rate = roundup_pow_of_two(rate);
+
+   prescale = ilog2(rate / 2);
+   if (prescale > 6)
+   prescale = 6;
+
+   /* Calculate the real clock, and usecs for one byte transaction */
+   freq = ms->sys_freq >> (prescale + 1);
+   tmo = DIV_ROUND_UP(8 * 100, freq);
+
+   /* 10 times tolerance plus 100us */
+   ms->wait_us = POLLING_SCALE * tmo + POLLING_FRAC_USEC;
+
+   /* set SPI_CFG */
+   cfg = prescale << SPI_CLK_S;
+
+   switch (ms->mode & (SPI_CPOL | SPI_CPHA)) {
+   case SPI_MODE_0:
+   cfg |= TX_CLK_EDGE;
+   break;
+   case SPI_MODE_1:
+   cfg |= RX_CLK_EDGE;
+   break;
+   case SPI_MODE_2:
+   cfg |= SPI_CLK_POL | RX_CLK_EDGE;
+   break;
+   case SPI_MODE_3:
+   cfg |= SPI_CLK_POL | TX_CLK_EDGE;
+   break;
+   }
+
+   if (!(ms->mode & SPI_LSB_FIRST))
+   cfg |= MSB_FIRST;
+
+   writel(cfg, >m[cs]->cfg);
+
+   writel(SPI_HIGH, >m[cs]->ctl);
+}
+
+static void mt7620_spi_set_cs(struct mt7620_spi *ms, int cs, bool enable)
+{
+   if (enable)
+   mt7620_spi_master_setup(ms, cs);
+
+   if (ms->mode & SPI_CS_HIGH)
+   enable = !enable;
+
+   if (enable)
+   clrbits_32(>m[cs]->ctl, SPI_HIGH);
+   else
+

[PATCH v4 13/23] pinctrl: mtmips: add support for MediaTek MT7620 SoC

2020-11-12 Thread Weijie Gao
This patch adds pinctrl support for MediaTek MT7620 SoC.
The MT7620 SoC supports only pinmux.

Reviewed-by: Stefan Roese 
Signed-off-by: Weijie Gao 
---
v4 changes: none
v3 changes: none
v2 changes: none
---
 drivers/pinctrl/mtmips/Kconfig  |   9 ++
 drivers/pinctrl/mtmips/Makefile |   1 +
 drivers/pinctrl/mtmips/pinctrl-mt7620.c | 200 
 3 files changed, 210 insertions(+)
 create mode 100644 drivers/pinctrl/mtmips/pinctrl-mt7620.c

diff --git a/drivers/pinctrl/mtmips/Kconfig b/drivers/pinctrl/mtmips/Kconfig
index 8482a38ebc..844d5b743f 100644
--- a/drivers/pinctrl/mtmips/Kconfig
+++ b/drivers/pinctrl/mtmips/Kconfig
@@ -3,6 +3,15 @@ config PINCTRL_MTMIPS
depends on ARCH_MTMIPS
bool
 
+config PINCTRL_MT7620
+   bool "MediaTek MT7620 pin control driver"
+   select PINCTRL_MTMIPS
+   depends on SOC_MT7620 && PINCTRL_GENERIC
+   help
+ Support pin multiplexing control on MediaTek MT7620.
+ The driver is controlled by a device tree node which contains
+ the pin mux functions for each available pin groups.
+
 config PINCTRL_MT7628
bool "MediaTek MT7628 pin control driver"
select PINCTRL_MTMIPS
diff --git a/drivers/pinctrl/mtmips/Makefile b/drivers/pinctrl/mtmips/Makefile
index 3ba5c0c66d..ba945a89a7 100644
--- a/drivers/pinctrl/mtmips/Makefile
+++ b/drivers/pinctrl/mtmips/Makefile
@@ -4,4 +4,5 @@
 obj-$(CONFIG_PINCTRL_MTMIPS) += pinctrl-mtmips-common.o
 
 # SoC Drivers
+obj-$(CONFIG_PINCTRL_MT7620) += pinctrl-mt7620.o
 obj-$(CONFIG_PINCTRL_MT7628) += pinctrl-mt7628.o
diff --git a/drivers/pinctrl/mtmips/pinctrl-mt7620.c 
b/drivers/pinctrl/mtmips/pinctrl-mt7620.c
new file mode 100644
index 00..10a4fc6c19
--- /dev/null
+++ b/drivers/pinctrl/mtmips/pinctrl-mt7620.c
@@ -0,0 +1,200 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2020 MediaTek Inc. All Rights Reserved.
+ *
+ * Author: Weijie Gao 
+ */
+
+#include 
+#include 
+#include 
+#include 
+
+#include "pinctrl-mtmips-common.h"
+
+#define SUTIF_SHIFT30
+#define WDT_RST_SHIFT  21
+#define PA_G_SHIFT 20
+#define NAND_SD_SHIFT  18
+#define PERST_SHIFT16
+#define EPHY_LED_SHIFT 15
+#define WLED_SHIFT 13
+#define SPI_CS1_SHIFT  12
+#define SPI_SHIFT  11
+#define RGMII2_SHIFT   10
+#define RGMII1_SHIFT   9
+#define MDIO_SHIFT 7
+#define UARTL_SHIFT5
+#define UARTF_SHIFT2
+#define I2C_SHIFT  0
+
+#define GM4_MASK   3
+#define GM8_MASK   7
+
+#if CONFIG_IS_ENABLED(PINMUX)
+static const struct mtmips_pmx_func sutif_grp[] = {
+   FUNC("i2c", 2),
+   FUNC("uartl", 1),
+   FUNC("none", 0),
+};
+
+static const struct mtmips_pmx_func wdt_rst_grp[] = {
+   FUNC("gpio", 2),
+   FUNC("refclk", 1),
+   FUNC("wdt rst", 0),
+};
+
+static const struct mtmips_pmx_func pa_g_grp[] = {
+   FUNC("gpio", 1),
+   FUNC("pa", 0),
+};
+
+static const struct mtmips_pmx_func nand_sd_grp[] = {
+   FUNC("gpio", 2),
+   FUNC("sd", 1),
+   FUNC("nand", 0),
+};
+
+static const struct mtmips_pmx_func perst_grp[] = {
+   FUNC("gpio", 2),
+   FUNC("refclk", 1),
+   FUNC("perst", 0),
+};
+
+static const struct mtmips_pmx_func ephy_led_grp[] = {
+   FUNC("gpio", 1),
+   FUNC("led", 0),
+};
+
+static const struct mtmips_pmx_func wled_grp[] = {
+   FUNC("gpio", 1),
+   FUNC("led", 0),
+};
+
+static const struct mtmips_pmx_func spi_cs1_grp[] = {
+   FUNC("refclk", 1),
+   FUNC("spi cs1", 0),
+};
+
+static const struct mtmips_pmx_func spi_grp[] = {
+   FUNC("gpio", 1),
+   FUNC("spi", 0),
+};
+
+static const struct mtmips_pmx_func rgmii2_grp[] = {
+   FUNC("gpio", 1),
+   FUNC("rgmii2", 0),
+};
+
+static const struct mtmips_pmx_func rgmii1_grp[] = {
+   FUNC("gpio", 1),
+   FUNC("rgmii1", 0),
+};
+
+static const struct mtmips_pmx_func mdio_grp[] = {
+   FUNC("gpio", 2),
+   FUNC("refclk", 1),
+   FUNC("mdio", 0),
+};
+
+static const struct mtmips_pmx_func uartl_grp[] = {
+   FUNC("gpio", 1),
+   FUNC("uartl", 0),
+};
+
+static const struct mtmips_pmx_func uartf_grp[] = {
+   FUNC("gpio", 7),
+   FUNC("i2s gpio", 6),
+   FUNC("uartf gpio", 5),
+   FUNC("gpio pcm", 4),
+   FUNC("i2s uartf", 3),
+   FUNC("i2s pcm", 2),
+   FUNC("uartf pcm", 1),
+   FUNC("uartf", 0),
+};
+
+static const struct mtmips_pmx_func i2c_grp[] = {
+   FUNC("gpio", 1),
+   FUNC("i2c", 0),
+};
+
+static const struct mtmips_pmx_group mt7620_pinmux_data[] = {
+   GRP("sutif", sutif_grp, 0, SUTIF_SHIFT, GM4_MASK),
+   GRP("wdt rst", wdt_rst_grp, 0, WDT_RST_SHIFT, GM4_MASK),
+   GRP("pa", pa_g_grp, 0, PA_G_SHIFT, 1),
+   

[PATCH v4 15/23] gpio: add GPIO controller driver for MediaTek MT7620 SoC

2020-11-12 Thread Weijie Gao
This patch adds GPIO controller driver for MediaTek MT7620 SoC

Reviewed-by: Stefan Roese 
Reviewed-by: Simon Glass 
Signed-off-by: Weijie Gao 
---
v4 changes: none
v3 changes: none
v2 changes: none
---
 drivers/gpio/Kconfig   |   8 ++
 drivers/gpio/Makefile  |   1 +
 drivers/gpio/mt7620_gpio.c | 146 +
 3 files changed, 155 insertions(+)
 create mode 100644 drivers/gpio/mt7620_gpio.c

diff --git a/drivers/gpio/Kconfig b/drivers/gpio/Kconfig
index 202fcc6f47..7e94759a79 100644
--- a/drivers/gpio/Kconfig
+++ b/drivers/gpio/Kconfig
@@ -471,6 +471,14 @@ config MPC83XX_SPISEL_BOOT
 
  This pin is typically used as spi chip select to a spi nor flash.
 
+config MT7620_GPIO
+   bool "MediaTek MT7620 GPIO driver"
+   depends on DM_GPIO && SOC_MT7620
+   default y
+   help
+ Device model driver for GPIO controller present in MediaTek MT7620
+ and earlier SoCs.
+
 config MT7621_GPIO
bool "MediaTek MT7621 GPIO driver"
depends on DM_GPIO && SOC_MT7628
diff --git a/drivers/gpio/Makefile b/drivers/gpio/Makefile
index d3d0d3cacf..8541ba0b0a 100644
--- a/drivers/gpio/Makefile
+++ b/drivers/gpio/Makefile
@@ -64,6 +64,7 @@ obj-$(CONFIG_MVEBU_GPIO)  += mvebu_gpio.o
 obj-$(CONFIG_MSM_GPIO) += msm_gpio.o
 obj-$(CONFIG_$(SPL_)PCF8575_GPIO)  += pcf8575_gpio.o
 obj-$(CONFIG_PM8916_GPIO)  += pm8916_gpio.o
+obj-$(CONFIG_MT7620_GPIO)  += mt7620_gpio.o
 obj-$(CONFIG_MT7621_GPIO)  += mt7621_gpio.o
 obj-$(CONFIG_MSCC_SGPIO)   += mscc_sgpio.o
 obj-$(CONFIG_NX_GPIO)  += nx_gpio.o
diff --git a/drivers/gpio/mt7620_gpio.c b/drivers/gpio/mt7620_gpio.c
new file mode 100644
index 00..508851ea5f
--- /dev/null
+++ b/drivers/gpio/mt7620_gpio.c
@@ -0,0 +1,146 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2020 MediaTek Inc. All Rights Reserved.
+ *
+ * Author: Weijie Gao 
+ *
+ * GPIO controller driver for MediaTek MT7620 SoC
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+enum mt7620_regs {
+   GPIO_REG_DATA,
+   GPIO_REG_DIR,
+   GPIO_REG_SET,
+   GPIO_REG_CLR,
+
+   __GPIO_REG_MAX
+};
+
+struct mt7620_gpio_priv {
+   void __iomem *base;
+   u32 regs[__GPIO_REG_MAX];
+   u32 count;
+};
+
+static int mt7620_gpio_get_value(struct udevice *dev, unsigned int offset)
+{
+   struct mt7620_gpio_priv *priv = dev_get_priv(dev);
+
+   return !!(readl(priv->base + priv->regs[GPIO_REG_DATA]) & BIT(offset));
+}
+
+static int mt7620_gpio_set_value(struct udevice *dev, unsigned int offset,
+int value)
+{
+   struct mt7620_gpio_priv *priv = dev_get_priv(dev);
+   u32 reg;
+
+   reg = value ? priv->regs[GPIO_REG_SET] : priv->regs[GPIO_REG_CLR];
+
+   writel(BIT(offset), priv->base + reg);
+
+   return 0;
+}
+
+static int mt7620_gpio_direction_input(struct udevice *dev, unsigned int 
offset)
+{
+   struct mt7620_gpio_priv *priv = dev_get_priv(dev);
+
+   clrbits_32(priv->base + priv->regs[GPIO_REG_DIR], BIT(offset));
+
+   return 0;
+}
+
+static int mt7620_gpio_direction_output(struct udevice *dev,
+   unsigned int offset, int value)
+{
+   struct mt7620_gpio_priv *priv = dev_get_priv(dev);
+
+   /* Set value first */
+   mt7620_gpio_set_value(dev, offset, value);
+
+   setbits_32(priv->base + priv->regs[GPIO_REG_DIR], BIT(offset));
+
+   return 0;
+}
+
+static int mt7620_gpio_get_function(struct udevice *dev, unsigned int offset)
+{
+   struct mt7620_gpio_priv *priv = dev_get_priv(dev);
+
+   return (readl(priv->base + priv->regs[GPIO_REG_DIR]) & BIT(offset)) ?
+  GPIOF_OUTPUT : GPIOF_INPUT;
+}
+
+static const struct dm_gpio_ops mt7620_gpio_ops = {
+   .direction_input= mt7620_gpio_direction_input,
+   .direction_output   = mt7620_gpio_direction_output,
+   .get_value  = mt7620_gpio_get_value,
+   .set_value  = mt7620_gpio_set_value,
+   .get_function   = mt7620_gpio_get_function,
+};
+
+static int mt7620_gpio_probe(struct udevice *dev)
+{
+   struct mt7620_gpio_priv *priv = dev_get_priv(dev);
+   struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
+   const char *name;
+
+   name = dev_read_string(dev, "mediatek,bank-name");
+   if (!name)
+   name = dev->name;
+
+   uc_priv->gpio_count = priv->count;
+   uc_priv->bank_name = name;
+
+   return 0;
+}
+
+static int mt7620_gpio_ofdata_to_platdata(struct udevice *dev)
+{
+   struct mt7620_gpio_priv *priv = dev_get_priv(dev);
+   int ret;
+
+   priv->base = dev_remap_addr_index(dev, 0);
+   if (!priv->base) {
+   dev_err(dev, "mt7620_gpio: unable to map registers\n");
+   return -EINVAL;
+   }
+
+   ret = dev_read_u32(dev, "mediatek,gpio-num", >count);
+   if (ret) {
+   

[PATCH v4 14/23] watchdog: add watchdog driver for MediaTek MT7620 SoC

2020-11-12 Thread Weijie Gao
This patch adds watchdog support for the Mediatek MT7620 SoC

Reviewed-by: Stefan Roese 
Reviewed-by: Jagan Teki 
Signed-off-by: Weijie Gao 
---
v4 changes: none
v3 changes: add detail comments for mt7620_wdt_expire_now()
v2 changes: add expire_now
---
 drivers/watchdog/Kconfig  |   7 ++
 drivers/watchdog/Makefile |   1 +
 drivers/watchdog/mt7620_wdt.c | 142 ++
 3 files changed, 150 insertions(+)
 create mode 100644 drivers/watchdog/mt7620_wdt.c

diff --git a/drivers/watchdog/Kconfig b/drivers/watchdog/Kconfig
index 4532a40e45..7d7bd4dcb7 100644
--- a/drivers/watchdog/Kconfig
+++ b/drivers/watchdog/Kconfig
@@ -125,6 +125,13 @@ config WDT_MPC8xx
help
  Select this to enable mpc8xx watchdog timer
 
+config WDT_MT7620
+   bool "MediaTek MT7620 watchdog timer support"
+   depends on WDT && SOC_MT7620
+   help
+ Select this to enable watchdog timer on MediaTek MT7620 and earlier
+ SoC chips.
+
 config WDT_MT7621
bool "MediaTek MT7621 watchdog timer support"
depends on WDT && SOC_MT7628
diff --git a/drivers/watchdog/Makefile b/drivers/watchdog/Makefile
index 01b8231f2b..825b50e626 100644
--- a/drivers/watchdog/Makefile
+++ b/drivers/watchdog/Makefile
@@ -24,6 +24,7 @@ obj-$(CONFIG_WDT_CORTINA) += cortina_wdt.o
 obj-$(CONFIG_WDT_ORION) += orion_wdt.o
 obj-$(CONFIG_WDT_CDNS) += cdns_wdt.o
 obj-$(CONFIG_WDT_MPC8xx) += mpc8xx_wdt.o
+obj-$(CONFIG_WDT_MT7620) += mt7620_wdt.o
 obj-$(CONFIG_WDT_MT7621) += mt7621_wdt.o
 obj-$(CONFIG_WDT_MTK) += mtk_wdt.o
 obj-$(CONFIG_WDT_OCTEONTX) += octeontx_wdt.o
diff --git a/drivers/watchdog/mt7620_wdt.c b/drivers/watchdog/mt7620_wdt.c
new file mode 100644
index 00..8d39010ee7
--- /dev/null
+++ b/drivers/watchdog/mt7620_wdt.c
@@ -0,0 +1,142 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2020 MediaTek Inc.
+ *
+ * Author:  Weijie Gao 
+ *
+ * Watchdog timer for MT7620 and earlier SoCs
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+struct mt7620_wdt {
+   void __iomem *regs;
+   u64 timeout;
+};
+
+#define TIMER_FREQ 4000
+#define TIMER_MASK 0x
+#define TIMER_PRESCALE 65536
+
+#define TIMER_LOAD 0x00
+#define TIMER_CTL  0x08
+
+#define TIMER_ENABLE   BIT(7)
+#define TIMER_MODE_SHIFT   4
+#define   TIMER_MODE_WDT   3
+#define TIMER_PRESCALE_SHIFT   0
+#define   TIMER_PRESCALE_65536 15
+
+static void mt7620_wdt_ping(struct mt7620_wdt *priv)
+{
+   u64 val;
+
+   val = (TIMER_FREQ / TIMER_PRESCALE) * priv->timeout;
+   do_div(val, 1000);
+
+   if (val > TIMER_MASK)
+   val = TIMER_MASK;
+
+   writel(val, priv->regs + TIMER_LOAD);
+}
+
+static int mt7620_wdt_start(struct udevice *dev, u64 ms, ulong flags)
+{
+   struct mt7620_wdt *priv = dev_get_priv(dev);
+
+   priv->timeout = ms;
+   mt7620_wdt_ping(priv);
+
+   writel(TIMER_ENABLE | (TIMER_MODE_WDT << TIMER_MODE_SHIFT) |
+  (TIMER_PRESCALE_65536 << TIMER_PRESCALE_SHIFT),
+  priv->regs + TIMER_CTL);
+
+   return 0;
+}
+
+static int mt7620_wdt_stop(struct udevice *dev)
+{
+   struct mt7620_wdt *priv = dev_get_priv(dev);
+
+   mt7620_wdt_ping(priv);
+
+   clrbits_32(priv->regs + TIMER_CTL, TIMER_ENABLE);
+
+   return 0;
+}
+
+static int mt7620_wdt_reset(struct udevice *dev)
+{
+   struct mt7620_wdt *priv = dev_get_priv(dev);
+
+   mt7620_wdt_ping(priv);
+
+   return 0;
+}
+
+static int mt7620_wdt_expire_now(struct udevice *dev, ulong flags)
+{
+   struct mt7620_wdt *priv = dev_get_priv(dev);
+
+   mt7620_wdt_start(dev, 1, flags);
+
+   /*
+* 0 will disable the timer directly, a positive number must be used
+* instead. Since the timer is a countdown timer, 1 (tick) is used.
+*
+* For a timer with input clock = 40MHz, 1 timer tick is short
+* enough to trigger a timeout immediately.
+*
+* Restore prescale to 1, and load timer with 1 to trigger timeout.
+*/
+   writel(TIMER_ENABLE | (TIMER_MODE_WDT << TIMER_MODE_SHIFT),
+  priv->regs + TIMER_CTL);
+   writel(1, priv->regs + TIMER_LOAD);
+
+   return 0;
+}
+
+static int mt7620_wdt_probe(struct udevice *dev)
+{
+   struct mt7620_wdt *priv = dev_get_priv(dev);
+   struct reset_ctl reset_wdt;
+   int ret;
+
+   ret = reset_get_by_index(dev, 0, _wdt);
+   if (!ret)
+   reset_deassert(_wdt);
+
+   priv->regs = dev_remap_addr(dev);
+   if (!priv->regs)
+   return -EINVAL;
+
+   mt7620_wdt_stop(dev);
+
+   return 0;
+}
+
+static const struct wdt_ops mt7620_wdt_ops = {
+   .start = mt7620_wdt_start,
+   .reset = mt7620_wdt_reset,
+   .stop = mt7620_wdt_stop,
+   .expire_now = mt7620_wdt_expire_now,
+};
+
+static 

[PATCH v4 11/23] clk: add clock driver for MediaTek MT7620 SoC

2020-11-12 Thread Weijie Gao
This patch adds a clock driver for MediaTek MT7620 SoC.
This driver provides clock gate control as well as getting clock frequency
for CPU/SYS/XTAL and some peripherals.

Reviewed-by: Stefan Roese 
Signed-off-by: Weijie Gao 
---
v4 changes: none
v3 changes: none
v2 changes: none
---
 drivers/clk/mtmips/Makefile|   1 +
 drivers/clk/mtmips/clk-mt7620.c| 159 +
 include/dt-bindings/clock/mt7620-clk.h |  40 +++
 3 files changed, 200 insertions(+)
 create mode 100644 drivers/clk/mtmips/clk-mt7620.c
 create mode 100644 include/dt-bindings/clock/mt7620-clk.h

diff --git a/drivers/clk/mtmips/Makefile b/drivers/clk/mtmips/Makefile
index e1938418da..732e7f2545 100644
--- a/drivers/clk/mtmips/Makefile
+++ b/drivers/clk/mtmips/Makefile
@@ -1,3 +1,4 @@
 # SPDX-License-Identifier: GPL-2.0
 
+obj-$(CONFIG_SOC_MT7620) += clk-mt7620.o
 obj-$(CONFIG_SOC_MT7628) += clk-mt7628.o
diff --git a/drivers/clk/mtmips/clk-mt7620.c b/drivers/clk/mtmips/clk-mt7620.c
new file mode 100644
index 00..44e4b53c21
--- /dev/null
+++ b/drivers/clk/mtmips/clk-mt7620.c
@@ -0,0 +1,159 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2020 MediaTek Inc. All Rights Reserved.
+ *
+ * Author: Weijie Gao 
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+/* CLKCFG1 */
+#define CLKCFG1_REG0x30
+
+#define CLK_SRC_CPU-1
+#define CLK_SRC_CPU_D2 -2
+#define CLK_SRC_SYS-3
+#define CLK_SRC_XTAL   -4
+#define CLK_SRC_PERI   -5
+
+struct mt7620_clk_priv {
+   struct udevice *dev;
+   struct udevice *sysc;
+   struct mt7620_sysc_clks clks;
+};
+
+static const int mt7620_clks[] = {
+   [CLK_SYS] = CLK_SRC_SYS,
+   [CLK_CPU] = CLK_SRC_CPU,
+   [CLK_XTAL] = CLK_SRC_XTAL,
+   [CLK_MIPS_CNT] = CLK_SRC_CPU_D2,
+   [CLK_UARTF] = CLK_SRC_PERI,
+   [CLK_UARTL] = CLK_SRC_PERI,
+   [CLK_SPI] = CLK_SRC_SYS,
+   [CLK_I2C] = CLK_SRC_PERI,
+   [CLK_I2S] = CLK_SRC_PERI,
+};
+
+static ulong mt7620_clk_get_rate(struct clk *clk)
+{
+   struct mt7620_clk_priv *priv = dev_get_priv(clk->dev);
+
+   if (clk->id >= ARRAY_SIZE(mt7620_clks))
+   return 0;
+
+   switch (mt7620_clks[clk->id]) {
+   case CLK_SRC_CPU:
+   return priv->clks.cpu_clk;
+   case CLK_SRC_CPU_D2:
+   return priv->clks.cpu_clk / 2;
+   case CLK_SRC_SYS:
+   return priv->clks.sys_clk;
+   case CLK_SRC_XTAL:
+   return priv->clks.xtal_clk;
+   case CLK_SRC_PERI:
+   return priv->clks.peri_clk;
+   default:
+   return mt7620_clks[clk->id];
+   }
+}
+
+static int mt7620_clkcfg1_rmw(struct mt7620_clk_priv *priv, u32 clr, u32 set)
+{
+   u32 val;
+   int ret;
+
+   ret = misc_read(priv->sysc, CLKCFG1_REG, , sizeof(val));
+   if (ret) {
+   dev_err(priv->dev, "mt7620_clk: failed to read CLKCFG1\n");
+   return ret;
+   }
+
+   val &= ~clr;
+   val |= set;
+
+   ret = misc_write(priv->sysc, CLKCFG1_REG, , sizeof(val));
+   if (ret) {
+   dev_err(priv->dev, "mt7620_clk: failed to write CLKCFG1\n");
+   return ret;
+   }
+
+   return 0;
+}
+
+static int mt7620_clk_enable(struct clk *clk)
+{
+   struct mt7620_clk_priv *priv = dev_get_priv(clk->dev);
+
+   if (clk->id > 30)
+   return -1;
+
+   return mt7620_clkcfg1_rmw(priv, 0, BIT(clk->id));
+}
+
+static int mt7620_clk_disable(struct clk *clk)
+{
+   struct mt7620_clk_priv *priv = dev_get_priv(clk->dev);
+
+   if (clk->id > 30)
+   return -1;
+
+   return mt7620_clkcfg1_rmw(priv, BIT(clk->id), 0);
+}
+
+const struct clk_ops mt7620_clk_ops = {
+   .enable = mt7620_clk_enable,
+   .disable = mt7620_clk_disable,
+   .get_rate = mt7620_clk_get_rate,
+};
+
+static int mt7620_clk_probe(struct udevice *dev)
+{
+   struct mt7620_clk_priv *priv = dev_get_priv(dev);
+   struct ofnode_phandle_args sysc_args;
+   int ret;
+
+   ret = ofnode_parse_phandle_with_args(dev->node, "mediatek,sysc", NULL,
+0, 0, _args);
+   if (ret) {
+   dev_err(dev, "mt7620_clk: sysc property not found\n");
+   return ret;
+   }
+
+   ret = uclass_get_device_by_ofnode(UCLASS_MISC, sysc_args.node,
+ >sysc);
+   if (ret) {
+   dev_err(dev, "mt7620_clk: failed to sysc device\n");
+   return ret;
+   }
+
+   ret = misc_ioctl(priv->sysc, MT7620_SYSC_IOCTL_GET_CLK,
+>clks);
+   if (ret) {
+   dev_err(dev, "mt7620_clk: failed to get base clocks\n");
+   return ret;
+   }
+
+   priv->dev = dev;
+
+   return 0;
+}
+
+static const struct udevice_id mt7620_clk_ids[] = {
+   

[PATCH v4 12/23] reset: mtmips: add reset controller support for MediaTek MT7620 SoC

2020-11-12 Thread Weijie Gao
This patch adds reset controller bits definition header file for MediaTek
MT7620 SoC

Reviewed-by: Stefan Roese 
Signed-off-by: Weijie Gao 
---
v4 changes: none
v3 changes: none
v2 changes: none
---
 include/dt-bindings/reset/mt7620-reset.h | 35 
 1 file changed, 35 insertions(+)
 create mode 100644 include/dt-bindings/reset/mt7620-reset.h

diff --git a/include/dt-bindings/reset/mt7620-reset.h 
b/include/dt-bindings/reset/mt7620-reset.h
new file mode 100644
index 00..3096b29cdb
--- /dev/null
+++ b/include/dt-bindings/reset/mt7620-reset.h
@@ -0,0 +1,35 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright (C) 2020 MediaTek Inc.
+ *
+ * Author:  Weijie Gao 
+ */
+
+#ifndef _DT_BINDINGS_MT7620_RESET_H_
+#define _DT_BINDINGS_MT7620_RESET_H_
+
+#define PPE_RST31
+#define SDHC_RST   30
+#define MIPS_CNT_RST   28
+#define PCIE_RST   26
+#define UHST_RST   25
+#define EPHY_RST   24
+#define ESW_RST23
+#define UDEV_RST   22
+#define FE_RST 21
+#define WLAN_RST   20
+#define UARTL_RST  19
+#define SPI_RST18
+#define I2S_RST17
+#define I2C_RST16
+#define NAND_RST   15
+#define DMA_RST14
+#define PIO_RST13
+#define UARTF_RST  12
+#define PCM_RST11
+#define MC_RST 10
+#define INTC_RST   9
+#define TIMER_RST  8
+#define SYS_RST0
+
+#endif /* _DT_BINDINGS_MT7620_RESET_H_ */
-- 
2.17.1


[PATCH v4 10/23] serial: add uart driver for MediaTek MT7620 SoC

2020-11-12 Thread Weijie Gao
This patch adds uart support for MediaTek MT7620 and earlier SoCs.

The UART used by MT7620 is incompatible with the ns16550a driver.
All registers of this UART have different addresses. A special 16-bit
register for Divisor Latch is used to set the baudrate instead of the
original two 8-bit registers (DLL and DLM).

The driver supports of-platdata which is useful for tiny SPL.

Reviewed-by: Stefan Roese 
Reviewed-by: Simon Glass 
Signed-off-by: Weijie Gao 
---
v4 changes: remove unused non-DM code in drivers/serial/serial.c
v3 changes: none
v2 changes: replace non-DM code with of-platdata
---
 drivers/serial/Kconfig |  20 +++
 drivers/serial/Makefile|   1 +
 drivers/serial/serial_mt7620.c | 246 +
 3 files changed, 267 insertions(+)
 create mode 100644 drivers/serial/serial_mt7620.c

diff --git a/drivers/serial/Kconfig b/drivers/serial/Kconfig
index b4805a2e4e..44fff8a3cd 100644
--- a/drivers/serial/Kconfig
+++ b/drivers/serial/Kconfig
@@ -401,6 +401,16 @@ config DEBUG_UART_MTK
  driver will be available until the real driver model serial is
  running.
 
+config DEBUG_UART_MT7620
+   bool "UART driver for MediaTek MT7620 and earlier SoCs"
+   depends on MT7620_SERIAL
+   help
+ Select this to enable a debug UART using the UART driver for
+ MediaTek MT7620 and earlier SoCs.
+ You will need to provide parameters to make this work. The
+ driver will be available until the real driver model serial is
+ running.
+
 endchoice
 
 config DEBUG_UART_BASE
@@ -817,6 +827,16 @@ config MTK_SERIAL
  The High-speed UART is compatible with the ns16550a UART and have
  its own high-speed registers.
 
+config MT7620_SERIAL
+   bool "UART driver for MediaTek MT7620 and earlier SoCs"
+   depends on DM_SERIAL
+   help
+ Select this to enable UART support for MediaTek MT7620 and earlier
+ SoCs. This driver uses driver model and requires a device tree
+ binding to operate.
+ The UART driver for MediaTek MT7620 and earlier SoCs is *NOT*
+ compatible with the ns16550a UART.
+
 config MPC8XX_CONS
bool "Console driver for MPC8XX"
depends on MPC8xx
diff --git a/drivers/serial/Makefile b/drivers/serial/Makefile
index 25f7f8d342..0c3810f5d5 100644
--- a/drivers/serial/Makefile
+++ b/drivers/serial/Makefile
@@ -69,6 +69,7 @@ obj-$(CONFIG_NULLDEV_SERIAL) += serial_nulldev.o
 obj-$(CONFIG_OWL_SERIAL) += serial_owl.o
 obj-$(CONFIG_OMAP_SERIAL) += serial_omap.o
 obj-$(CONFIG_MTK_SERIAL) += serial_mtk.o
+obj-$(CONFIG_MT7620_SERIAL) += serial_mt7620.o
 obj-$(CONFIG_SIFIVE_SERIAL) += serial_sifive.o
 obj-$(CONFIG_XEN_SERIAL) += serial_xen.o
 
diff --git a/drivers/serial/serial_mt7620.c b/drivers/serial/serial_mt7620.c
new file mode 100644
index 00..4f6b5de4cf
--- /dev/null
+++ b/drivers/serial/serial_mt7620.c
@@ -0,0 +1,246 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * UART driver for MediaTek MT7620 and earlier SoCs
+ *
+ * Copyright (C) 2020 MediaTek Inc.
+ * Author: Weijie Gao 
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#if CONFIG_IS_ENABLED(OF_PLATDATA)
+#include 
+#endif
+
+struct mt7620_serial_regs {
+   u32 rbr;
+   u32 thr;
+   u32 ier;
+   u32 iir;
+   u32 fcr;
+   u32 lcr;
+   u32 mcr;
+   u32 lsr;
+   u32 msr;
+   u32 scratch;
+   u32 dl;
+   u32 dll;
+   u32 dlm;
+   u32 ifctl;
+};
+
+#define UART_LCR_WLS_8 0x03/* 8 bit character length */
+
+#define UART_LSR_DR0x01/* Data ready */
+#define UART_LSR_THRE  0x20/* Xmit holding register empty */
+#define UART_LSR_TEMT  0x40/* Xmitter empty */
+
+#define UART_MCR_DTR   0x01/* DTR */
+#define UART_MCR_RTS   0x02/* RTS */
+
+#define UART_FCR_FIFO_EN   0x01/* Fifo enable */
+#define UART_FCR_RXSR  0x02/* Receiver soft reset */
+#define UART_FCR_TXSR  0x04/* Transmitter soft reset */
+
+#define UART_MCRVAL (UART_MCR_DTR | \
+UART_MCR_RTS)
+
+/* Clear & enable FIFOs */
+#define UART_FCRVAL (UART_FCR_FIFO_EN | \
+UART_FCR_RXSR |\
+UART_FCR_TXSR)
+
+struct mt7620_serial_plat {
+#if CONFIG_IS_ENABLED(OF_PLATDATA)
+   struct dtd_serial_mt7620 dtplat;
+#endif
+
+   struct mt7620_serial_regs __iomem *regs;
+   u32 clock;
+};
+
+static void _mt7620_serial_setbrg(struct mt7620_serial_plat *plat, int baud)
+{
+   u32 quot;
+
+   /* set divisor */
+   quot = DIV_ROUND_CLOSEST(plat->clock, 16 * baud);
+   writel(quot, >regs->dl);
+
+   /* set character length and stop bits */
+   writel(UART_LCR_WLS_8, >regs->lcr);
+}
+
+static int mt7620_serial_setbrg(struct udevice *dev, int baudrate)
+{
+   struct mt7620_serial_plat 

[PATCH v4 09/23] configs: mtmips: refresh for mt7628 based boards

2020-11-12 Thread Weijie Gao
Since mt7620 is added into Kconfig, the CONFIG_SOC_MT7628=y which is
omitted by default must be added back, otherwise make xxx_defconfig for
these boards will be configured for mt7620 platform.

Reviewed-by: Stefan Roese 
Signed-off-by: Weijie Gao 
---
v4 changes: none
v3 changes: none
v2 changes: none
---
 configs/gardena-smart-gateway-mt7688_defconfig | 1 +
 configs/linkit-smart-7688_defconfig| 1 +
 configs/mt7628_rfb_defconfig   | 1 +
 configs/vocore2_defconfig  | 1 +
 4 files changed, 4 insertions(+)

diff --git a/configs/gardena-smart-gateway-mt7688_defconfig 
b/configs/gardena-smart-gateway-mt7688_defconfig
index 001a66d0f6..c03123f6e4 100644
--- a/configs/gardena-smart-gateway-mt7688_defconfig
+++ b/configs/gardena-smart-gateway-mt7688_defconfig
@@ -12,6 +12,7 @@ CONFIG_SPL=y
 CONFIG_SYS_BOOTCOUNT_SINGLEWORD=y
 CONFIG_ENV_OFFSET_REDUND=0xB
 CONFIG_ARCH_MTMIPS=y
+CONFIG_SOC_MT7628=y
 CONFIG_DEFAULT_DEVICE_TREE="gardena-smart-gateway-mt7688"
 CONFIG_RESTORE_EXCEPTION_VECTOR_BASE=y
 # CONFIG_MIPS_BOOT_ENV_LEGACY is not set
diff --git a/configs/linkit-smart-7688_defconfig 
b/configs/linkit-smart-7688_defconfig
index 437000b4c7..dd2524e4ec 100644
--- a/configs/linkit-smart-7688_defconfig
+++ b/configs/linkit-smart-7688_defconfig
@@ -9,6 +9,7 @@ CONFIG_SPL_SERIAL_SUPPORT=y
 CONFIG_SPL_SYS_MALLOC_F_LEN=0x4
 CONFIG_SPL=y
 CONFIG_ARCH_MTMIPS=y
+CONFIG_SOC_MT7628=y
 CONFIG_BOARD_LINKIT_SMART_7688=y
 CONFIG_DEFAULT_DEVICE_TREE="linkit-smart-7688"
 CONFIG_RESTORE_EXCEPTION_VECTOR_BASE=y
diff --git a/configs/mt7628_rfb_defconfig b/configs/mt7628_rfb_defconfig
index 69025728b2..831e54e9f3 100644
--- a/configs/mt7628_rfb_defconfig
+++ b/configs/mt7628_rfb_defconfig
@@ -9,6 +9,7 @@ CONFIG_SPL_SERIAL_SUPPORT=y
 CONFIG_SPL_SYS_MALLOC_F_LEN=0x4
 CONFIG_SPL=y
 CONFIG_ARCH_MTMIPS=y
+CONFIG_SOC_MT7628=y
 CONFIG_BOARD_MT7628_RFB=y
 CONFIG_DEFAULT_DEVICE_TREE="mediatek,mt7628-rfb"
 CONFIG_RESTORE_EXCEPTION_VECTOR_BASE=y
diff --git a/configs/vocore2_defconfig b/configs/vocore2_defconfig
index acb9abf1be..e255829366 100644
--- a/configs/vocore2_defconfig
+++ b/configs/vocore2_defconfig
@@ -10,6 +10,7 @@ CONFIG_SPL_SYS_MALLOC_F_LEN=0x4
 CONFIG_SPL=y
 CONFIG_SYS_BOOTCOUNT_SINGLEWORD=y
 CONFIG_ARCH_MTMIPS=y
+CONFIG_SOC_MT7628=y
 CONFIG_BOARD_VOCORE2=y
 CONFIG_DEFAULT_DEVICE_TREE="vocore_vocore2"
 CONFIG_RESTORE_EXCEPTION_VECTOR_BASE=y
-- 
2.17.1


  1   2   >