Skip to contentSkip to conversation

rockchip: Miscellaneous updates for RK3506

PATCH Superseded Jonas Karlman 6 participants 37 messages Aug 3 – Sep 15

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover rockchip: Miscellaneous updates for RK3506

This series includes miscellaneous updates for RK3506, such as:
- updated default load addresses to support small memory SoC variants
- request use of 1.5 GHz saradc clk rate to avoid field truncation
- changes to use FIELD_PREP_WM16 macro where appropriate

The series depends on following patches for a clean apply:
- clk: rockchip: rk3506: Fix trivial clock configuration errors [1]
- include: linux: Import hw_bitfield.h from Linux v6.18 [2] And a branch with all depends and board configs can be found at [3]

[1] https://patchwork.ozlabs.org/patch/2278946/
[2] https://patchwork.ozlabs.org/patch/2280368/
[3] https://git.u-boot-project.org/u-boot/contributors/kwiboo/u-boot/-/commits/rk3506

Jonas Karlman (6):
rockchip: rk3506: Imply OF_UPSTREAM
rockchip: rk3506: Update ENV_MEM_LAYOUT_SETTINGS
adc: rockchip-saradc: Add driver data for RK3506
clk: rockchip: rk3506: Fix CLK_SARADC set rate issues
clk: rockchip: rk3506: Use FIELD_PREP_WM16 macro
rockchip: rk3506: Use FIELD_PREP_WM16 macro

 arch/arm/mach-rockchip/Kconfig         |   1 +
 arch/arm/mach-rockchip/rk3506/rk3506.c |   7 +-
 drivers/adc/rockchip-saradc.c          |  10 ++
 drivers/clk/rockchip/clk_rk3506.c      | 208 +++++++++++--------------
 include/configs/rk3506_common.h        |  14 +-
 5 files changed, 117 insertions(+), 123 deletions(-)
Signature · 1 line
2.54.0
#1 lore raw

PATCH 1/6 rockchip: rk3506: Imply OF_UPSTREAM

The Rockchip platform typically require new board targets to use OF_UPSTREAM and board device trees from dts/upstream.

Imply OF_UPSTREAM at SoC level for RK3506.

---
 arch/arm/mach-rockchip/Kconfig | 1 +
 1 file changed, 1 insertion(+)
arch/arm/mach-rockchip/Kconfig +1 -0
index 1a2e7847c9e7..f60da2f41f24 100644
@@ -341,6 +341,7 @@ config ROCKCHIP_RK3506
 	imply MISC_INIT_R
 	imply OF_LIBFDT_OVERLAY
 	imply OF_LIVE
+	imply OF_UPSTREAM
 	imply RNG_ROCKCHIP
 	imply ROCKCHIP_COMMON_BOARD
 	imply ROCKCHIP_COMMON_STACK_ADDR
Signature · 1 line
2.54.0
Reviewed-by on v1 1/6
#2 lore raw

Re: [PATCH 1/6] rockchip: rk3506: Imply OF_UPSTREAM

Hi Jonas,

Quoted 3 lines · Jonas Karlman

The Rockchip platform typically require new board targets to use OF_UPSTREAM and board device trees from dts/upstream.

We should eventually make this clear in the docs, and list the possible exceptions (namely, the "generic" defconfigs that help with bring up; OrangePi 3B is such a corner case that i don't think it's worth documenting as an exception).

Quoted 2 lines

Imply OF_UPSTREAM at SoC level for RK3506.

Thanks!
Quentin

Reviewed-by on v1 1/6
#3 lore raw

Re: [PATCH 1/6] rockchip: rk3506: Imply OF_UPSTREAM

In reply to Jonas Karlman
Quoted 5 lines · Jonas Karlman

rockchip: rk3506: Imply OF_UPSTREAM

The Rockchip platform typically require new board targets to use OF_UPSTREAM and board device trees from dts/upstream.

Quoted 7 lines

Imply OF_UPSTREAM at SoC level for RK3506.

arch/arm/mach-rockchip/Kconfig | 1 +

 1 file changed, 1 insertion(+)
#4 lore raw

PATCH 2/6 rockchip: rk3506: Update ENV_MEM_LAYOUT_SETTINGS

In reply to Jonas Karlman

The RK3506G1 has 64 MiB integrated DDR2, the RK3506G2 has 128 MiB integrated DDR3 and the RK3506B support external memory up to 1 GiB.

Current memory layout cause issues using compressed kernels or ramdisk due to the default load addresses being used.

Change the default load addresses to also work on SoC variants with limited memory, keep all default addresses in the 0-64 MiB range.

old new
scriptaddr 5 MiB 2 MiB
pxefile_addr_r 6 MiB 1 MiB
kernel_addr_r 32.5 MiB 8 MiB
kernel_comp_addr_r 128 MiB 48 MiB
fdt_addr_r 30.5 MiB 4 MiB
fdtoverlay_addr_r 31.5 MiB 6 MiB
ramdisk_addr_r 96 MiB 48 MiB

---
 include/configs/rk3506_common.h | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
include/configs/rk3506_common.h +7 -7
index 5e4ef67289f0..210549504855 100644
@@ -18,15 +18,15 @@
 #endif
 
 #define ENV_MEM_LAYOUT_SETTINGS			\
-	"scriptaddr=0x00500000\0"		\
+	"scriptaddr=0x00200000\0"		\
 	"script_offset_f=0xffe000\0"		\
 	"script_size_f=0x2000\0"		\
-	"pxefile_addr_r=0x00600000\0"		\
-	"kernel_addr_r=0x02080000\0"		\
-	"kernel_comp_addr_r=0x08000000\0"	\
-	"fdt_addr_r=0x01e00000\0"		\
-	"fdtoverlay_addr_r=0x01f00000\0"	\
-	"ramdisk_addr_r=0x06000000\0"		\
+	"pxefile_addr_r=0x00100000\0"		\
+	"kernel_addr_r=0x00800000\0"		\
+	"kernel_comp_addr_r=0x03000000\0"	\
+	"fdt_addr_r=0x00400000\0"		\
+	"fdtoverlay_addr_r=0x00600000\0"	\
+	"ramdisk_addr_r=0x03000000\0"		\
 	"kernel_comp_size=0x2000000\0"
 
 #define CFG_EXTRA_ENV_SETTINGS			\
Signature · 1 line
2.54.0
#5 lore raw

Re: [PATCH 2/6] rockchip: rk3506: Update ENV_MEM_LAYOUT_SETTINGS

Hi Jonas,

Quoted 7 lines · Jonas Karlman

rockchip: rk3506: Update ENV_MEM_LAYOUT_SETTINGS

The RK3506G1 has 64 MiB integrated DDR2, the RK3506G2 has 128 MiB integrated DDR3 and the RK3506B support external memory up to 1 GiB.

Change the default load addresses to also work on SoC variants with limited memory, keep all default addresses in the 0-64 MiB range.

The addresses fit in 0-64 MiB, but kernel_comp_addr_r (48 MiB) + kernel_comp_size (32 MiB) reaches 80 MiB, past the end of DDR on the RK3506G1. Does this need separate values for each SoC ?

Quoted 23 lines

Current memory layout cause issues using compressed kernels or ramdisk due to the default load addresses being used.

Change the default load addresses to also work on SoC variants with limited memory, keep all default addresses in the 0-64 MiB range.

old new

  scriptaddr            5 MiB           2 MiB
  pxefile_addr_r        6 MiB           1 MiB
  kernel_addr_r         32.5 MiB        8 MiB
  kernel_comp_addr_r    128 MiB         48 MiB
  fdt_addr_r            30.5 MiB        4 MiB
  fdtoverlay_addr_r     31.5 MiB        6 MiB
  ramdisk_addr_r        96 MiB          48 MiB

[...]

include/configs/rk3506_common.h | 14 +++++++-------

 1 file changed, 7 insertions(+), 7 deletions(-)

include/configs/rk3506_common.h line 20

Quoted 5 lines
include/configs/rk3506_common.h +2 -2
@@ -18,15 +18,15 @@
-     "kernel_comp_addr_r=0x08000000\0"       \
-     "ramdisk_addr_r=0x06000000\0"           \
+     "kernel_comp_addr_r=0x03000000\0"       \
+     "ramdisk_addr_r=0x03000000\0"           \
      "kernel_comp_size=0x2000000\0"

kernel_comp_addr_r and ramdisk_addr_r are both at 0x03000000, so decompression will scribble over an already-loaded ramdisk. Every other rockchip *_common.h keeps these apart. Please can you space them so the kernel_comp window (0x03000000 + kernel_comp_size = 0x05000000) does not overlap the ramdisk?

Regards,
Simon

#6 lore raw

Re: [PATCH 2/6] rockchip: rk3506: Update ENV_MEM_LAYOUT_SETTINGS

Hi Simon,

Quoted 8 lines · Simon Glass

Hi Jonas,

Quoted 4 lines · Jonas Karlman

rockchip: rk3506: Update ENV_MEM_LAYOUT_SETTINGS

The RK3506G1 has 64 MiB integrated DDR2, the RK3506G2 has 128 MiB integrated DDR3 and the RK3506B support external memory up to 1 GiB.

Quoted 6 lines
Quoted 2 lines

Change the default load addresses to also work on SoC variants with limited memory, keep all default addresses in the 0-64 MiB range.

The addresses fit in 0-64 MiB, but kernel_comp_addr_r (48 MiB) + kernel_comp_size (32 MiB) reaches 80 MiB, past the end of DDR on the RK3506G1. Does this need separate values for each SoC ?

Please enlighten me on when kernel_comp_addr_r will be used when booting ARMv7, I normally use zImage so the kernel handles decompression.

I do not think there is a big issue for normal operation, and users likely have to adjust these values to fit their specific use-case.

The main issue this tries to solve is having decent space for a kernel and a small ramdisk on these low memory devices. And my thinking is that if you need a large kernel, you likely also need an variant having more memory.

Quoted 22 lines
Quoted 21 lines

Current memory layout cause issues using compressed kernels or ramdisk due to the default load addresses being used.

Change the default load addresses to also work on SoC variants with limited memory, keep all default addresses in the 0-64 MiB range.

old new

  scriptaddr            5 MiB           2 MiB
  pxefile_addr_r        6 MiB           1 MiB
  kernel_addr_r         32.5 MiB        8 MiB
  kernel_comp_addr_r    128 MiB         48 MiB
  fdt_addr_r            30.5 MiB        4 MiB
  fdtoverlay_addr_r     31.5 MiB        6 MiB
  ramdisk_addr_r        96 MiB          48 MiB

[...]

include/configs/rk3506_common.h | 14 +++++++-------

 1 file changed, 7 insertions(+), 7 deletions(-)
Quoted 13 lines

include/configs/rk3506_common.h line 20

Quoted 7 lines
include/configs/rk3506_common.h +2 -2
@@ -18,15 +18,15 @@
-     "kernel_comp_addr_r=0x08000000\0"       \
-     "ramdisk_addr_r=0x06000000\0"           \
+     "kernel_comp_addr_r=0x03000000\0"       \
+     "ramdisk_addr_r=0x03000000\0"           \
      "kernel_comp_size=0x2000000\0"

kernel_comp_addr_r and ramdisk_addr_r are both at 0x03000000, so decompression will scribble over an already-loaded ramdisk. Every other rockchip *_common.h keeps these apart. Please can you space them so the kernel_comp window (0x03000000 + kernel_comp_size = 0x05000000) does not overlap the ramdisk?

Based on my testing the kernel will normally be loaded and decompressed before the ramdisk is loaded, so having the addresses overlap seems to be working fine.

Regards,
Jonas

Quoted 3 lines

Regards,
Simon

#7 lore raw

Re: [PATCH 2/6] rockchip: rk3506: Update ENV_MEM_LAYOUT_SETTINGS

In reply to Jonas Karlman

Hi Jonas,

Quoted 6 lines · Jonas Karlman

The RK3506G1 has 64 MiB integrated DDR2, the RK3506G2 has 128 MiB integrated DDR3 and the RK3506B support external memory up to 1 GiB.

Current memory layout cause issues using compressed kernels or ramdisk due to the default load addresses being used.

RK3506 is an Aarch32 SoC, and Linux kernels on that architecture are self-decompressing, U-Boot doesn't do anything with compressed kernels. You can see that kernel_comp_addr_r is only ever used in cmd/booti.c which depends on ARM64 to be actually built.

I simply recommend getting rid of this variable as it's quite misleading. All other Aarch32 Rockchip SoCs we support don't seem to be setting this variable, so no additional cleanup to do. Same for kernel_comp_size.

Quoted 11 lines

Change the default load addresses to also work on SoC variants with limited memory, keep all default addresses in the 0-64 MiB range.

old new
scriptaddr 5 MiB 2 MiB
pxefile_addr_r 6 MiB 1 MiB
kernel_addr_r 32.5 MiB 8 MiB
kernel_comp_addr_r 128 MiB 48 MiB
fdt_addr_r 30.5 MiB 4 MiB
fdtoverlay_addr_r 31.5 MiB 6 MiB
ramdisk_addr_r 96 MiB 48 MiB

That leaves less than 18MiB for an initramfs (don't forget U-Boot proper is located at the end of the DRAM as far as I remember), which is very little.

Do we really need 2MiB for the DTB and DTBO? 1MiB ought to be enough for the DTB and I'm sure less than 1MiB ought to be enough per overlay (overlay are loaded and applied one after the other). This should free an additional 2MiB for either the kernel or initramfs.

The multi_v5_defconfig and multi_v7_defconfig zImage kernels as built by KernelCI are respectively 7.5 and 11.6MiB as of yesterday, c.f. https://dashboard.kernelci.org/build/maestro%3A6a7b3f4762f33761c0aedc06 https://dashboard.kernelci.org/build/maestro%3A6a7b3f6562f33761c0aedc2f
So maybe we can reserve a bit less than 40MiB for the kernel? But maybe kernel_addr_r is also used for FIT images which may be much bigger than the size of the kernel alone? The load/entry properties are required for images of type kernel according to the FIT spec so that part isn't something we need to deal with.

Have you figured out how to identify the variant of the RK3506 already? Another option is to dynamically modify those variables (if they aren't already part of the environment) based on the size of available DRAM (and/or SoC variant). I don't like the idea too much but that could be a way to stretch the available memory for each binary loaded by U-Boot as much as possible.

Cheers,
Quentin

#8 lore raw

Re: [PATCH 2/6] rockchip: rk3506: Update ENV_MEM_LAYOUT_SETTINGS

Hi Quentin,

Quoted 15 lines · Quentin Schulz

Hi Jonas,

Quoted 6 lines · Jonas Karlman

The RK3506G1 has 64 MiB integrated DDR2, the RK3506G2 has 128 MiB integrated DDR3 and the RK3506B support external memory up to 1 GiB.

Current memory layout cause issues using compressed kernels or ramdisk due to the default load addresses being used.

RK3506 is an Aarch32 SoC, and Linux kernels on that architecture are self-decompressing, U-Boot doesn't do anything with compressed kernels. You can see that kernel_comp_addr_r is only ever used in cmd/booti.c which depends on ARM64 to be actually built.

Quoted 4 lines

I simply recommend getting rid of this variable as it's quite misleading. All other Aarch32 Rockchip SoCs we support don't seem to be setting this variable, so no additional cleanup to do.
Same for kernel_comp_size.

Thanks for looking deeper, I will drop these two in v2.

Quoted 13 lines
Quoted 4 lines

Change the default load addresses to also work on SoC variants with limited memory, keep all default addresses in the 0-64 MiB range.

Quoted 8 lines

old new
scriptaddr 5 MiB 2 MiB
pxefile_addr_r 6 MiB 1 MiB
kernel_addr_r 32.5 MiB 8 MiB
kernel_comp_addr_r 128 MiB 48 MiB
fdt_addr_r 30.5 MiB 4 MiB
fdtoverlay_addr_r 31.5 MiB 6 MiB
ramdisk_addr_r 96 MiB 48 MiB

Quoted 8 lines

That leaves less than 18MiB for an initramfs (don't forget U-Boot proper is located at the end of the DRAM as far as I remember), which is very little.

Do we really need 2MiB for the DTB and DTBO? 1MiB ought to be enough for the DTB and I'm sure less than 1MiB ought to be enough per overlay (overlay are loaded and applied one after the other). This should free an additional 2MiB for either the kernel or initramfs.

Agree, I thought I had seen DTB may need to use a 4 MiB aligned address, e.g. for RISC-V, but ARM seem to only need it to be any 64-bit aligned address.

Quoted 10 lines

The multi_v5_defconfig and multi_v7_defconfig zImage kernels as built by KernelCI are respectively 7.5 and 11.6MiB as of yesterday, c.f.
https://dashboard.kernelci.org/build/maestro%3A6a7b3f4762f33761c0aedc06
https://dashboard.kernelci.org/build/maestro%3A6a7b3f6562f33761c0aedc2f
So maybe we can reserve a bit less than 40MiB for the kernel? But maybe kernel_addr_r is also used for FIT images which may be much bigger than the size of the kernel alone? The load/entry properties are required for images of type kernel according to the FIT spec so that part isn't something we need to deal with.

My local Rockchip ARMv7 kernel without loadable module support is now around 26M for Image and 9.9M for zImage, it used to be just below 32M for Image when I also enabled more debugging options and partial support for other SoCs. So support for up to 32M uncompressed would be nice :-)

I have made some minor adjustments, see below, that seem to work to boot into a minimal initramfs busybox rootfs in my lab.

Looking closer at ARM kernel booting, I can see that during decompression the kernel will be decompressed to close to start of DRAM, and if the compressed and decompressed images overlaps the compressed image will be copied to end of the target uncompressed image before it continues.

U-Boot also tries to move ramdisk and FDT to end of memory to reduce the chance of the kernel decompression to overwrite FDT or ramdisk.

For my simple zImage + FDT + initramfs.cpio.gz pxe boot setup following seem to work.

U-Boot 2026.10-rc2-00243-g77aab9b3d743-dirty (Aug 13 2026 - 08:08:37 +0000)

Model: Luckfox Lyra Plus
SoC: RK3506
DRAM: 64 MiB (board has 128M, code limits to 64M to emulate)

=> printenv
fdt_addr_r=0x00200000 2 MiB
fdtcontroladdr=3d39fa0
fdtfile=rockchip/rk3506-luckfox-lyra-plus.dtb
fdtoverlay_addr_r=0x00300000 3 MiB
kernel_addr_r=0x00400000 4 MiB
loadaddr=0x200000 2 MiB
pxefile_addr_r=0x00100000 1 MiB
ramdisk_addr_r=0x02400000 36 MiB
scriptaddr=0x00180000 1.5 MiB

CONFIG_SYS_MALLOC_LEN=0x200000 2 MiB
CONFIG_SYS_BOOTM_LEN=0x2000000 32 MiB
CONFIG_SYS_LOAD_ADDR=0x200000 2 MiB
CONFIG_STACK_SIZE=0x100000 1 MiB

=> meminfo
DRAM: 64 MiB

Region Base Size End Gap
--------------------------------------------------------------------

  code               3f63000         8c6fc       3fef6fc
  malloc             3d44000        21f000       3f63000             0
  board_info         3d43fc0            34       3d43ff4             c
  global_data        3d43e80           138       3d43fb8             8
  devicetree         3d39fa0          9ec8       3d43e68            18
  stack              3c39f80        100000       3d39f80            20
  lmb                3c39f80             0       3c39f80             0
  lmb                3c36000          3f80       3c39f80             0
  free                     0       3c36000       3c36000             0

Retrieving file: pxelinux.cfg/default-arm-rk3506 Load address: 0x100000
Bytes transferred = 192 (c0 hex)

Retrieving file: /armhf/zImage
Load address: 0x400000
Bytes transferred = 10318920 (9d7448 hex)

Retrieving file: /armhf/initramfs.cpio.gz
Load address: 0x2400000
Bytes transferred = 1756164 (1acc04 hex)

Retrieving file: /armhf/rockchip/rk3506-luckfox-lyra-plus.dtb Load address: 0x200000
Bytes transferred = 31752 (7c08 hex)

Kernel image @ 0x400000 [ 0x000000 - 0x9d7448 ]
## Flattened Device Tree blob at 00200000

     Booting using the fdt blob at 0x200000
  Working FDT set to 200000
     Loading Ramdisk to 03a53000, end 03bffc04 ... OK
     Loading Device Tree to 03c03000, end 03c0dc07 ... OK

Working FDT set to 3c03000

Starting kernel ...

  [    0.000000] Booting Linux on physical CPU 0xf00
  [    0.000000] Linux version 7.2.0-rc6-next-20260803 ...
  [    0.000000] CPU: ARMv7 Processor [410fc075] revision 5 (ARMv7), cr=10c5387d
  [    0.000000] CPU: div instructions available: patching division code
  [    0.000000] CPU: PIPT / VIPT nonaliasing data cache, VIPT aliasing instruction cache
  [    0.000000] OF: fdt: Machine model: Luckfox Lyra Plus
  [    0.000000] earlycon: uart8250 MMIO32:0xff0a0000 (options '')
  [    0.000000] printk: legacy bootconsole [uart8250] enabled
  [    0.000000] printk: debug: ignoring loglevel setting.
  [    0.000000] Memory policy: Data cache writealloc
  [    0.000000] cma: Reserved 4 MiB at 0x03000000
  [    0.000000] OF: reserved mem: Reserved memory: No reserved-memory node in the DT
  [    0.000000] Zone ranges:
  [    0.000000]   Normal   [mem 0x0000000000000000-0x0000000003ffffff]
  [    0.000000]   HighMem  empty
  [    0.000000] Movable zone start for each node
  [    0.000000] Early memory node ranges
  [    0.000000]   node   0: [mem 0x0000000000000000-0x0000000003ffffff]
  [    0.000000] Initmem setup node 0 [mem 0x0000000000000000-0x0000000003ffffff]

~ # free -m
total used free shared buff/cache available
Mem: 39 17 16 4 7 16
Swap: 0 0 0

There is likely other optimizations that can be done, e.g. reduce malloc and stack size. I have tried to keep following in mind when choosing above load addresses:

- Avoid first 1 MiB of DRAM for future OPTEE-OS or TF-A use
- Allow for use of 32 MiB uncompressed kernel image
- Keep scripts/pxe/fdt/overlays before kernel and ramdisk to allow for a larger ramdisk on boards with more memory
- Keep kernel load address as close to start of DRAM as possible

Quoted 7 lines

Have you figured out how to identify the variant of the RK3506 already? Another option is to dynamically modify those variables (if they aren't already part of the environment) based on the size of available DRAM (and/or SoC variant). I don't like the idea too much but that could be a way to stretch the available memory for each binary loaded by U-Boot as much as possible.

Agree, we correctly identify amount of DRAM so should be possible to add code that dynamically adjusts addresses, but I also want to avoid this if possible, hence not putting any other address after ramdisk, to allow for larger ramdisk on devices with more memory.

Any suggestions on a more production ready memory layout are appreciated, my use case and testing has mostly been for U-Boot and Linux hacking in my lab, not any real in the field production use ;-)

Regards,
Jonas

Quoted 3 lines

Cheers,
Quentin

#9 lore raw

Re: [PATCH 2/6] rockchip: rk3506: Update ENV_MEM_LAYOUT_SETTINGS

Hi Jonas,

Quoted 42 lines · Jonas Karlman

Hi Quentin,

Quoted 15 lines · Quentin Schulz

Hi Jonas,

Quoted 6 lines · Jonas Karlman

The RK3506G1 has 64 MiB integrated DDR2, the RK3506G2 has 128 MiB integrated DDR3 and the RK3506B support external memory up to 1 GiB.

Current memory layout cause issues using compressed kernels or ramdisk due to the default load addresses being used.

RK3506 is an Aarch32 SoC, and Linux kernels on that architecture are self-decompressing, U-Boot doesn't do anything with compressed kernels. You can see that kernel_comp_addr_r is only ever used in cmd/booti.c which depends on ARM64 to be actually built.

Quoted 4 lines

I simply recommend getting rid of this variable as it's quite misleading. All other Aarch32 Rockchip SoCs we support don't seem to be setting this variable, so no additional cleanup to do.
Same for kernel_comp_size.

Thanks for looking deeper, I will drop these two in v2.

Quoted 17 lines
Quoted 4 lines

Change the default load addresses to also work on SoC variants with limited memory, keep all default addresses in the 0-64 MiB range.

Quoted 8 lines
			 old		 new
    scriptaddr		5 MiB		2 MiB
    pxefile_addr_r	6 MiB		1 MiB
    kernel_addr_r		32.5 MiB	8 MiB
    kernel_comp_addr_r	128 MiB		48 MiB
    fdt_addr_r		30.5 MiB	4 MiB
    fdtoverlay_addr_r	31.5 MiB	6 MiB
    ramdisk_addr_r	96 MiB		48 MiB

That leaves less than 18MiB for an initramfs (don't forget U-Boot proper is located at the end of the DRAM as far as I remember), which is very little.

Quoted 9 lines
Quoted 4 lines

Do we really need 2MiB for the DTB and DTBO? 1MiB ought to be enough for the DTB and I'm sure less than 1MiB ought to be enough per overlay (overlay are loaded and applied one after the other). This should free an additional 2MiB for either the kernel or initramfs.

Agree, I thought I had seen DTB may need to use a 4 MiB aligned address, e.g. for RISC-V, but ARM seem to only need it to be any 64-bit aligned address.

8-byte alignment is required by the Device Tree spec, c.f. https://devicetree-specification.readthedocs.io/en/latest/chapter5-flattened-format.html#alignment

Quoted 11 lines
Quoted 10 lines

The multi_v5_defconfig and multi_v7_defconfig zImage kernels as built by KernelCI are respectively 7.5 and 11.6MiB as of yesterday, c.f.
https://dashboard.kernelci.org/build/maestro%3A6a7b3f4762f33761c0aedc06
https://dashboard.kernelci.org/build/maestro%3A6a7b3f6562f33761c0aedc2f
So maybe we can reserve a bit less than 40MiB for the kernel? But maybe kernel_addr_r is also used for FIT images which may be much bigger than the size of the kernel alone? The load/entry properties are required for images of type kernel according to the FIT spec so that part isn't something we need to deal with.

Quoted 5 lines

My local Rockchip ARMv7 kernel without loadable module support is now around 26M for Image and 9.9M for zImage, it used to be just below 32M for Image when I also enabled more debugging options and partial support for other SoCs. So support for up to 32M uncompressed would be nice :-)

What's the benefit of using an uncompressed image in your workflow?

Quoted 9 lines

I have made some minor adjustments, see below, that seem to work to boot into a minimal initramfs busybox rootfs in my lab.

Looking closer at ARM kernel booting, I can see that during decompression the kernel will be decompressed to close to start of DRAM, and if the compressed and decompressed images overlaps the compressed image will be copied to end of the target uncompressed image before it continues.

See https://people.kernel.org/linusw/how-the-arm32-linux-kernel-decompresses. "Close to start of DRAM" depends on TEXT_OFFSET of the kernel I think.

Quoted 3 lines

U-Boot also tries to move ramdisk and FDT to end of memory to reduce the chance of the kernel decompression to overwrite FDT or ramdisk.

Unless initrd_high and fdt_high are set I believe?

Quoted 109 lines

For my simple zImage + FDT + initramfs.cpio.gz pxe boot setup following seem to work.

   U-Boot 2026.10-rc2-00243-g77aab9b3d743-dirty (Aug 13 2026 - 08:08:37 +0000)

   Model: Luckfox Lyra Plus
   SoC:   RK3506
   DRAM:  64 MiB  (board has 128M, code limits to 64M to emulate)

   => printenv
   fdt_addr_r=0x00200000			2 MiB
   fdtcontroladdr=3d39fa0
   fdtfile=rockchip/rk3506-luckfox-lyra-plus.dtb
   fdtoverlay_addr_r=0x00300000		3 MiB
   kernel_addr_r=0x00400000		4 MiB
   loadaddr=0x200000			2 MiB
   pxefile_addr_r=0x00100000		1 MiB
   ramdisk_addr_r=0x02400000		36 MiB
   scriptaddr=0x00180000			1.5 MiB
   
   CONFIG_SYS_MALLOC_LEN=0x200000	2 MiB
   CONFIG_SYS_BOOTM_LEN=0x2000000	32 MiB
   CONFIG_SYS_LOAD_ADDR=0x200000		2 MiB
   CONFIG_STACK_SIZE=0x100000		1 MiB

   => meminfo
   DRAM:  64 MiB

   Region                Base          Size           End           Gap
   --------------------------------------------------------------------
   code               3f63000         8c6fc       3fef6fc
   malloc             3d44000        21f000       3f63000             0
   board_info         3d43fc0            34       3d43ff4             c
   global_data        3d43e80           138       3d43fb8             8
   devicetree         3d39fa0          9ec8       3d43e68            18
   stack              3c39f80        100000       3d39f80            20
   lmb                3c39f80             0       3c39f80             0
   lmb                3c36000          3f80       3c39f80             0
   free                     0       3c36000       3c36000             0

   Retrieving file: pxelinux.cfg/default-arm-rk3506
   Load address: 0x100000
   Bytes transferred = 192 (c0 hex)
   
   Retrieving file: /armhf/zImage
   Load address: 0x400000
   Bytes transferred = 10318920 (9d7448 hex)
   
   Retrieving file: /armhf/initramfs.cpio.gz
   Load address: 0x2400000
   Bytes transferred = 1756164 (1acc04 hex)
   
   Retrieving file: /armhf/rockchip/rk3506-luckfox-lyra-plus.dtb
   Load address: 0x200000
   Bytes transferred = 31752 (7c08 hex)
   
   Kernel image @ 0x400000 [ 0x000000 - 0x9d7448 ]
   ## Flattened Device Tree blob at 00200000
      Booting using the fdt blob at 0x200000
   Working FDT set to 200000
      Loading Ramdisk to 03a53000, end 03bffc04 ... OK
      Loading Device Tree to 03c03000, end 03c0dc07 ... OK
   Working FDT set to 3c03000
   
   Starting kernel ...
   
   [    0.000000] Booting Linux on physical CPU 0xf00
   [    0.000000] Linux version 7.2.0-rc6-next-20260803 ...
   [    0.000000] CPU: ARMv7 Processor [410fc075] revision 5 (ARMv7), cr=10c5387d
   [    0.000000] CPU: div instructions available: patching division code
   [    0.000000] CPU: PIPT / VIPT nonaliasing data cache, VIPT aliasing instruction cache
   [    0.000000] OF: fdt: Machine model: Luckfox Lyra Plus
   [    0.000000] earlycon: uart8250 MMIO32:0xff0a0000 (options '')
   [    0.000000] printk: legacy bootconsole [uart8250] enabled
   [    0.000000] printk: debug: ignoring loglevel setting.
   [    0.000000] Memory policy: Data cache writealloc
   [    0.000000] cma: Reserved 4 MiB at 0x03000000
   [    0.000000] OF: reserved mem: Reserved memory: No reserved-memory node in the DT
   [    0.000000] Zone ranges:
   [    0.000000]   Normal   [mem 0x0000000000000000-0x0000000003ffffff]
   [    0.000000]   HighMem  empty
   [    0.000000] Movable zone start for each node
   [    0.000000] Early memory node ranges
   [    0.000000]   node   0: [mem 0x0000000000000000-0x0000000003ffffff]
   [    0.000000] Initmem setup node 0 [mem 0x0000000000000000-0x0000000003ffffff]

   ~ # free -m
               total        used        free      shared  buff/cache   available
   Mem:             39          17          16           4           7          16
   Swap:             0           0           0

There is likely other optimizations that can be done, e.g. reduce malloc and stack size. I have tried to keep following in mind when choosing above load addresses:

- Avoid first 1 MiB of DRAM for future OPTEE-OS or TF-A use
- Allow for use of 32 MiB uncompressed kernel image
- Keep scripts/pxe/fdt/overlays before kernel and ramdisk to allow for a larger ramdisk on boards with more memory
- Keep kernel load address as close to start of DRAM as possible

Quoted 7 lines

Have you figured out how to identify the variant of the RK3506 already? Another option is to dynamically modify those variables (if they aren't already part of the environment) based on the size of available DRAM (and/or SoC variant). I don't like the idea too much but that could be a way to stretch the available memory for each binary loaded by U-Boot as much as possible.

Quoted 5 lines

Agree, we correctly identify amount of DRAM so should be possible to add code that dynamically adjusts addresses, but I also want to avoid this if possible, hence not putting any other address after ramdisk, to allow for larger ramdisk on devices with more memory.

Yeah, don't know what to do here. Another option is to have config fragments for variants of the SoC and increase/reduce them in that config.

Can you add a comment in the file just above ENV_MEM_LAYOUT_SETTINGS that the addresses are chosen to work with <insert name of SoC variants> with x MiB of DRAM already packaged in the silicon.

Cheers,
Quentin

#10 lore raw

Re: [PATCH 2/6] rockchip: rk3506: Update ENV_MEM_LAYOUT_SETTINGS

Quoted 90 lines · Quentin Schulz

Hi Jonas,

Quoted 42 lines · Jonas Karlman

Hi Quentin,

Quoted 15 lines · Quentin Schulz

Hi Jonas,

Quoted 6 lines · Jonas Karlman

The RK3506G1 has 64 MiB integrated DDR2, the RK3506G2 has 128 MiB integrated DDR3 and the RK3506B support external memory up to 1 GiB.

Current memory layout cause issues using compressed kernels or ramdisk due to the default load addresses being used.

RK3506 is an Aarch32 SoC, and Linux kernels on that architecture are self-decompressing, U-Boot doesn't do anything with compressed kernels. You can see that kernel_comp_addr_r is only ever used in cmd/booti.c which depends on ARM64 to be actually built.

Quoted 4 lines

I simply recommend getting rid of this variable as it's quite misleading. All other Aarch32 Rockchip SoCs we support don't seem to be setting this variable, so no additional cleanup to do.
Same for kernel_comp_size.

Thanks for looking deeper, I will drop these two in v2.

Quoted 17 lines
Quoted 4 lines

Change the default load addresses to also work on SoC variants with limited memory, keep all default addresses in the 0-64 MiB range.

Quoted 8 lines
			 old		 new
    scriptaddr		5 MiB		2 MiB
    pxefile_addr_r	6 MiB		1 MiB
    kernel_addr_r		32.5 MiB	8 MiB
    kernel_comp_addr_r	128 MiB		48 MiB
    fdt_addr_r		30.5 MiB	4 MiB
    fdtoverlay_addr_r	31.5 MiB	6 MiB
    ramdisk_addr_r	96 MiB		48 MiB

That leaves less than 18MiB for an initramfs (don't forget U-Boot proper is located at the end of the DRAM as far as I remember), which is very little.

Quoted 9 lines
Quoted 4 lines

Do we really need 2MiB for the DTB and DTBO? 1MiB ought to be enough for the DTB and I'm sure less than 1MiB ought to be enough per overlay (overlay are loaded and applied one after the other). This should free an additional 2MiB for either the kernel or initramfs.

Agree, I thought I had seen DTB may need to use a 4 MiB aligned address, e.g. for RISC-V, but ARM seem to only need it to be any 64-bit aligned address.

8-byte alignment is required by the Device Tree spec, c.f. https://devicetree-specification.readthedocs.io/en/latest/chapter5-flattened-format.html#alignment

Quoted 11 lines
Quoted 10 lines

The multi_v5_defconfig and multi_v7_defconfig zImage kernels as built by KernelCI are respectively 7.5 and 11.6MiB as of yesterday, c.f.
https://dashboard.kernelci.org/build/maestro%3A6a7b3f4762f33761c0aedc06
https://dashboard.kernelci.org/build/maestro%3A6a7b3f6562f33761c0aedc2f
So maybe we can reserve a bit less than 40MiB for the kernel? But maybe kernel_addr_r is also used for FIT images which may be much bigger than the size of the kernel alone? The load/entry properties are required for images of type kernel according to the FIT spec so that part isn't something we need to deal with.

Quoted 5 lines

My local Rockchip ARMv7 kernel without loadable module support is now around 26M for Image and 9.9M for zImage, it used to be just below 32M for Image when I also enabled more debugging options and partial support for other SoCs. So support for up to 32M uncompressed would be nice :-)

What's the benefit of using an uncompressed image in your workflow?

Quoted 9 lines

I have made some minor adjustments, see below, that seem to work to boot into a minimal initramfs busybox rootfs in my lab.

Looking closer at ARM kernel booting, I can see that during decompression the kernel will be decompressed to close to start of DRAM, and if the compressed and decompressed images overlaps the compressed image will be copied to end of the target uncompressed image before it continues.

See
https://people.kernel.org/linusw/how-the-arm32-linux-kernel-decompresses.
"Close to start of DRAM" depends on TEXT_OFFSET of the kernel I think.

Quoted 5 lines
Quoted 3 lines

U-Boot also tries to move ramdisk and FDT to end of memory to reduce the chance of the kernel decompression to overwrite FDT or ramdisk.

Unless initrd_high and fdt_high are set I believe?

And setting those in your default environment in tree gets me very cranky because it often ends up breaking users in unexpected ways (for example, yes, you put things in non-overlapping correctly aligned places, but some other user that just picked up the board didn't know and loaded some FIT image and ... now it blows up. And this is based on history of that happening).

Signature · 1 line
Tom
#11 lore raw

Re: [PATCH 2/6] rockchip: rk3506: Update ENV_MEM_LAYOUT_SETTINGS

Hi Tom,

Quoted 95 lines · Tom Rini
Quoted 94 lines · Quentin Schulz

Hi Jonas,

Quoted 42 lines · Jonas Karlman

Hi Quentin,

Quoted 15 lines · Quentin Schulz

Hi Jonas,

Quoted 6 lines · Jonas Karlman

The RK3506G1 has 64 MiB integrated DDR2, the RK3506G2 has 128 MiB integrated DDR3 and the RK3506B support external memory up to 1 GiB.

Current memory layout cause issues using compressed kernels or ramdisk due to the default load addresses being used.

RK3506 is an Aarch32 SoC, and Linux kernels on that architecture are self-decompressing, U-Boot doesn't do anything with compressed kernels. You can see that kernel_comp_addr_r is only ever used in cmd/booti.c which depends on ARM64 to be actually built.

Quoted 4 lines

I simply recommend getting rid of this variable as it's quite misleading. All other Aarch32 Rockchip SoCs we support don't seem to be setting this variable, so no additional cleanup to do.
Same for kernel_comp_size.

Thanks for looking deeper, I will drop these two in v2.

Quoted 17 lines
Quoted 4 lines

Change the default load addresses to also work on SoC variants with limited memory, keep all default addresses in the 0-64 MiB range.

Quoted 8 lines
			 old		 new
     scriptaddr		5 MiB		2 MiB
     pxefile_addr_r	6 MiB		1 MiB
     kernel_addr_r		32.5 MiB	8 MiB
     kernel_comp_addr_r	128 MiB		48 MiB
     fdt_addr_r		30.5 MiB	4 MiB
     fdtoverlay_addr_r	31.5 MiB	6 MiB
     ramdisk_addr_r	96 MiB		48 MiB

That leaves less than 18MiB for an initramfs (don't forget U-Boot proper is located at the end of the DRAM as far as I remember), which is very little.

Quoted 9 lines
Quoted 4 lines

Do we really need 2MiB for the DTB and DTBO? 1MiB ought to be enough for the DTB and I'm sure less than 1MiB ought to be enough per overlay (overlay are loaded and applied one after the other). This should free an additional 2MiB for either the kernel or initramfs.

Agree, I thought I had seen DTB may need to use a 4 MiB aligned address, e.g. for RISC-V, but ARM seem to only need it to be any 64-bit aligned address.

8-byte alignment is required by the Device Tree spec, c.f. https://devicetree-specification.readthedocs.io/en/latest/chapter5-flattened-format.html#alignment

Quoted 11 lines
Quoted 10 lines

The multi_v5_defconfig and multi_v7_defconfig zImage kernels as built by KernelCI are respectively 7.5 and 11.6MiB as of yesterday, c.f.
https://dashboard.kernelci.org/build/maestro%3A6a7b3f4762f33761c0aedc06
https://dashboard.kernelci.org/build/maestro%3A6a7b3f6562f33761c0aedc2f
So maybe we can reserve a bit less than 40MiB for the kernel? But maybe kernel_addr_r is also used for FIT images which may be much bigger than the size of the kernel alone? The load/entry properties are required for images of type kernel according to the FIT spec so that part isn't something we need to deal with.

Quoted 5 lines

My local Rockchip ARMv7 kernel without loadable module support is now around 26M for Image and 9.9M for zImage, it used to be just below 32M for Image when I also enabled more debugging options and partial support for other SoCs. So support for up to 32M uncompressed would be nice :-)

What's the benefit of using an uncompressed image in your workflow?

Quoted 9 lines

I have made some minor adjustments, see below, that seem to work to boot into a minimal initramfs busybox rootfs in my lab.

Looking closer at ARM kernel booting, I can see that during decompression the kernel will be decompressed to close to start of DRAM, and if the compressed and decompressed images overlaps the compressed image will be copied to end of the target uncompressed image before it continues.

See
https://people.kernel.org/linusw/how-the-arm32-linux-kernel-decompresses.
"Close to start of DRAM" depends on TEXT_OFFSET of the kernel I think.

Quoted 3 lines

U-Boot also tries to move ramdisk and FDT to end of memory to reduce the chance of the kernel decompression to overwrite FDT or ramdisk.

Quoted 9 lines
Quoted 1 line

Unless initrd_high and fdt_high are set I believe?

And setting those in your default environment in tree gets me very cranky because it often ends up breaking users in unexpected ways (for example, yes, you put things in non-overlapping correctly aligned places, but some other user that just picked up the board didn't know and loaded some FIT image and ... now it blows up. And this is based on history of that happening).

It was not a suggestion for people to use it, just to make sure I understood cases in which they aren't moved around. I vaguely remember those variables from 10y ago on Allwinner when somehow this fixed stuff/made things worse, so I am just simply aware of those variables :)

Cheers,
Quentin

#12 lore raw

PATCH 3/6 adc: rockchip-saradc: Add driver data for RK3506

In reply to Jonas Karlman

Current rockchip,rk3528-saradc fallback instruct the driver to use a 1 GHz clock rate. However, the CLK_SARADC used on RK3506 can only be configured to use a 32 KHz, 400 KHz or 1.5-24 GHz rate.

Add driver data for rockchip,rk3506-saradc that request use of a 1.5 GHz clock rate, to avoid clock divider overflow in the RK3506 clock driver.

---
 drivers/adc/rockchip-saradc.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
drivers/adc/rockchip-saradc.c +10 -0
index 1515951403c9..50861b61eea4 100644
@@ -339,6 +339,14 @@ static const struct rockchip_saradc_data rk3399_saradc_data = {
 	.stop = rockchip_saradc_stop_v1,
 };
 
+static const struct rockchip_saradc_data rk3506_saradc_data = {
+	.num_bits = 10,
+	.num_channels = 4,
+	.clk_rate = 1500000,
+	.channel_data = rockchip_saradc_channel_data_v2,
+	.start_channel = rockchip_saradc_start_channel_v2,
+};
+
 static const struct rockchip_saradc_data rk3528_saradc_data = {
 	.num_bits = 10,
 	.num_channels = 4,
@@ -362,6 +370,8 @@ static const struct udevice_id rockchip_saradc_ids[] = {
 	  .data = (ulong)&rk3066_tsadc_data },
 	{ .compatible = "rockchip,rk3399-saradc",
 	  .data = (ulong)&rk3399_saradc_data },
+	{ .compatible = "rockchip,rk3506-saradc",
+	  .data = (ulong)&rk3506_saradc_data },
 	{ .compatible = "rockchip,rk3528-saradc",
 	  .data = (ulong)&rk3528_saradc_data },
 	{ .compatible = "rockchip,rk3588-saradc",
Signature · 1 line
2.54.0
#13 lore raw

Re: [PATCH 3/6] adc: rockchip-saradc: Add driver data for RK3506

Hi Jonas,

Quoted 9 lines · Jonas Karlman

adc: rockchip-saradc: Add driver data for RK3506

Current rockchip,rk3528-saradc fallback instruct the driver to use a 1 GHz clock rate. However, the CLK_SARADC used on RK3506 can only be configured to use a 32 KHz, 400 KHz or 1.5-24 GHz rate.

Add driver data for rockchip,rk3506-saradc that request use of a 1.5 GHz clock rate, to avoid clock divider overflow in the RK3506 clock driver.

Units look wrong throughout - clk_rate is in Hz, so 1000000 is 1 MHz and 1500000 is 1.5 MHz, not GHz. Please s/GHz/MHz/ in all four places. Also 'instruct' -> 'instructs' and 'request' -> 'requests'.

Quoted 11 lines

Add driver data for rockchip,rk3506-saradc that request use of a 1.5 GHz clock rate, to avoid clock divider overflow in the RK3506 clock driver.

drivers/adc/rockchip-saradc.c | 10 ++++++++++

 1 file changed, 10 insertions(+)

drivers/adc/rockchip-saradc.c static const struct rockchip_saradc_data rk3399_saradc_data = { · line 345

Quoted 7 lines
drivers/adc/rockchip-saradc.c +7 -0
@@ -339,6 +339,14 @@ static const struct rockchip_saradc_data rk3399_saradc_data = {
+static const struct rockchip_saradc_data rk3506_saradc_data = {
+     .num_bits = 10,
+     .num_channels = 4,
+     .clk_rate = 1500000,
+     .channel_data = rockchip_saradc_channel_data_v2,
+     .start_channel = rockchip_saradc_start_channel_v2,
+};

Just to check - the other v2 entries (rk3528, rk3588) also omit .stop, so this matches the existing pattern. Is that deliberate because v2 hardware auto-stops, or is a stop_v2 callback missing? A brief note in the commit message would help.

Regards,
Simon

#14 lore raw

Re: [PATCH 3/6] adc: rockchip-saradc: Add driver data for RK3506

Hi Simon,

Quoted 10 lines · Simon Glass

Hi Jonas,

Quoted 7 lines · Jonas Karlman

adc: rockchip-saradc: Add driver data for RK3506

Current rockchip,rk3528-saradc fallback instruct the driver to use a 1 GHz clock rate. However, the CLK_SARADC used on RK3506 can only be configured to use a 32 KHz, 400 KHz or 1.5-24 GHz rate.

Quoted 6 lines
Quoted 2 lines

Add driver data for rockchip,rk3506-saradc that request use of a 1.5 GHz clock rate, to avoid clock divider overflow in the RK3506 clock driver.

Units look wrong throughout - clk_rate is in Hz, so 1000000 is 1 MHz and 1500000 is 1.5 MHz, not GHz. Please s/GHz/MHz/ in all four places. Also 'instruct' -> 'instructs' and 'request' -> 'requests'.

Hehe, you are correct, it should be MHz and not GHz :-)

Quoted 10 lines
Quoted 9 lines

Add driver data for rockchip,rk3506-saradc that request use of a 1.5 GHz clock rate, to avoid clock divider overflow in the RK3506 clock driver.

drivers/adc/rockchip-saradc.c | 10 ++++++++++

 1 file changed, 10 insertions(+)
Quoted 14 lines

drivers/adc/rockchip-saradc.c static const struct rockchip_saradc_data rk3399_saradc_data = { · line 345

Quoted 9 lines
drivers/adc/rockchip-saradc.c +7 -0
@@ -339,6 +339,14 @@ static const struct rockchip_saradc_data rk3399_saradc_data = {
+static const struct rockchip_saradc_data rk3506_saradc_data = {
+     .num_bits = 10,
+     .num_channels = 4,
+     .clk_rate = 1500000,
+     .channel_data = rockchip_saradc_channel_data_v2,
+     .start_channel = rockchip_saradc_start_channel_v2,
+};

Just to check - the other v2 entries (rk3528, rk3588) also omit .stop, so this matches the existing pattern. Is that deliberate because v2 hardware auto-stops, or is a stop_v2 callback missing? A brief note in the commit message would help.

To my knowledge the v1 needs to be power up/down, hence start/stop. And the v2 only need to do initial configuration for single read, and the interrupt is acknowledged as part channel_data, after first waiting for said interrupt and reading out the data. As you mention this is just a copy of the rk3528 (fallback) driver data, so not sure what you want me to mention related to this.

Regards,
Jonas

Quoted 3 lines

Regards,
Simon

#15 lore raw

Re: [PATCH 3/6] adc: rockchip-saradc: Add driver data for RK3506

Hi Jonas,

Quoted 48 lines · Jonas Karlman

Hi Simon,

Quoted 10 lines · Simon Glass

Hi Jonas,

Quoted 7 lines · Jonas Karlman

adc: rockchip-saradc: Add driver data for RK3506

Current rockchip,rk3528-saradc fallback instruct the driver to use a 1 GHz clock rate. However, the CLK_SARADC used on RK3506 can only be configured to use a 32 KHz, 400 KHz or 1.5-24 GHz rate.

Quoted 6 lines
Quoted 2 lines

Add driver data for rockchip,rk3506-saradc that request use of a 1.5 GHz clock rate, to avoid clock divider overflow in the RK3506 clock driver.

Units look wrong throughout - clk_rate is in Hz, so 1000000 is 1 MHz and 1500000 is 1.5 MHz, not GHz. Please s/GHz/MHz/ in all four places. Also 'instruct' -> 'instructs' and 'request' -> 'requests'.

Hehe, you are correct, it should be MHz and not GHz :-)

Quoted 10 lines
Quoted 9 lines

Add driver data for rockchip,rk3506-saradc that request use of a 1.5 GHz clock rate, to avoid clock divider overflow in the RK3506 clock driver.

drivers/adc/rockchip-saradc.c | 10 ++++++++++

 1 file changed, 10 insertions(+)
Quoted 14 lines

drivers/adc/rockchip-saradc.c static const struct rockchip_saradc_data rk3399_saradc_data = { · line 345

Quoted 9 lines
drivers/adc/rockchip-saradc.c +7 -0
@@ -339,6 +339,14 @@ static const struct rockchip_saradc_data rk3399_saradc_data = {
+static const struct rockchip_saradc_data rk3506_saradc_data = {
+     .num_bits = 10,
+     .num_channels = 4,
+     .clk_rate = 1500000,
+     .channel_data = rockchip_saradc_channel_data_v2,
+     .start_channel = rockchip_saradc_start_channel_v2,
+};

Just to check - the other v2 entries (rk3528, rk3588) also omit .stop, so this matches the existing pattern. Is that deliberate because v2 hardware auto-stops, or is a stop_v2 callback missing? A brief note in the commit message would help.

Quoted 6 lines

To my knowledge the v1 needs to be power up/down, hence start/stop. And the v2 only need to do initial configuration for single read, and the interrupt is acknowledged as part channel_data, after first waiting for said interrupt and reading out the data. As you mention this is just a copy of the rk3528 (fallback) driver data, so not sure what you want me to mention related to this.

Well it makes sense now. Since you are redoing this you could just mention that the start/stop is not needed.

Regards,
Simon

#16 lore raw

Re: [PATCH 3/6] adc: rockchip-saradc: Add driver data for RK3506

In reply to Jonas Karlman

Hi Jonas,

Quoted 1 line · Jonas Karlman

Current rockchip,rk3528-saradc fallback instruct the driver to use a

There is no rk3506 DTSI available either in Linux kernel or U-Boot, so this fallback technically doesn't even exist and we don't have an issue (yet). Just say that none of the the v2 SARADC data match what the RK3506 can do (due to the divider and parent clocks not being able to achieve the requested 1MHz). Please simply rename the commit title to reflect you're adding support for the SARADC found on the RK3506.

Quoted 3 lines

1 GHz clock rate. However, the CLK_SARADC used on RK3506 can only be configured to use a 32 KHz, 400 KHz or 1.5-24 GHz rate.

This is quite misleading. The parent clocks are 32KHz, 400KHz or 24MHz. The divider is between 1 and 16.

This means that 1MHz is not achievable with the dividers and we need to aim for something else. Please reword the commit log to reflect this. I'm also not sure where people got 32KHz, 400KHz and 24MHz but it's what's specified in the Linux kernel, so fine with me :)

Quoted 13 lines

Add driver data for rockchip,rk3506-saradc that request use of a 1.5 GHz clock rate, to avoid clock divider overflow in the RK3506 clock driver.

---
  drivers/adc/rockchip-saradc.c | 10 ++++++++++
  1 file changed, 10 insertions(+)
drivers/adc/rockchip-saradc.c +0 -0
index 1515951403c9..50861b61eea4 100644

drivers/adc/rockchip-saradc.c static const struct rockchip_saradc_data rk3399_saradc_data = { · line 343

Quoted 5 lines
drivers/adc/rockchip-saradc.c +2 -0
@@ -339,6 +339,14 @@ static const struct rockchip_saradc_data rk3399_saradc_data = {
  	.stop = rockchip_saradc_stop_v1,
  };
 
+static const struct rockchip_saradc_data rk3506_saradc_data = {
+	.num_bits = 10,

It has 12 bits according to the TRM.

Cheers,
Quentin

#17 lore raw

Re: [PATCH 3/6] adc: rockchip-saradc: Add driver data for RK3506

Hi Quentin,

Quoted 11 lines · Quentin Schulz

Hi Jonas,

Quoted 1 line · Jonas Karlman

Current rockchip,rk3528-saradc fallback instruct the driver to use a

There is no rk3506 DTSI available either in Linux kernel or U-Boot, so this fallback technically doesn't even exist and we don't have an issue (yet). Just say that none of the the v2 SARADC data match what the RK3506 can do (due to the divider and parent clocks not being able to achieve the requested 1MHz). Please simply rename the commit title to reflect you're adding support for the SARADC found on the RK3506.

Sure, I will update the commit message in a v2.

The rk3528 fallback mentioned was in reference to the dt-bindings:

- items:
- const: rockchip,rk3506-saradc
- const: rockchip,rk3528-saradc

Quoted 6 lines
Quoted 4 lines

1 GHz clock rate. However, the CLK_SARADC used on RK3506 can only be configured to use a 32 KHz, 400 KHz or 1.5-24 GHz rate.

This is quite misleading. The parent clocks are 32KHz, 400KHz or 24MHz. The divider is between 1 and 16.

You are correct, I must have mixed up this clock with a different clock that had multiple parent options and the divider was for one of those parent options.

Quoted 22 lines

This means that 1MHz is not achievable with the dividers and we need to aim for something else. Please reword the commit log to reflect this. I'm also not sure where people got 32KHz, 400KHz and 24MHz but it's what's specified in the Linux kernel, so fine with me :)

Quoted 16 lines

Add driver data for rockchip,rk3506-saradc that request use of a 1.5 GHz clock rate, to avoid clock divider overflow in the RK3506 clock driver.

---
  drivers/adc/rockchip-saradc.c | 10 ++++++++++
  1 file changed, 10 insertions(+)
drivers/adc/rockchip-saradc.c +0 -0
index 1515951403c9..50861b61eea4 100644
@@ -339,6 +339,14 @@ static const struct rockchip_saradc_data rk3399_saradc_data = {
  	.stop = rockchip_saradc_stop_v1,
  };
 
Quoted 4 lines

drivers/adc/rockchip-saradc.c static const struct rockchip_saradc_data rk3399_saradc_data = { · line 343

Quoted 2 lines
drivers/adc/rockchip-saradc.c +2 -0
+static const struct rockchip_saradc_data rk3506_saradc_data = {
+	.num_bits = 10,

It has 12 bits according to the TRM.

Hum, in the datasheet for RK3506J v1.2, RK3506B v1.4, RK3506G2 v1.5 and RK3506 TRM v1.1/v1.2 it lists 10-bit resolution.

Regrads,
Jonas

Quoted 3 lines

Cheers,
Quentin

#18 lore raw

Re: [PATCH 3/6] adc: rockchip-saradc: Add driver data for RK3506

Hi Jonas,

Quoted 15 lines · Jonas Karlman

Hi Quentin,

Quoted 11 lines · Quentin Schulz

Hi Jonas,

Quoted 1 line · Jonas Karlman

Current rockchip,rk3528-saradc fallback instruct the driver to use a

There is no rk3506 DTSI available either in Linux kernel or U-Boot, so this fallback technically doesn't even exist and we don't have an issue (yet). Just say that none of the the v2 SARADC data match what the RK3506 can do (due to the divider and parent clocks not being able to achieve the requested 1MHz). Please simply rename the commit title to reflect you're adding support for the SARADC found on the RK3506.

Quoted 8 lines

Sure, I will update the commit message in a v2.

The rk3528 fallback mentioned was in reference to the dt-bindings:

- items:
- const: rockchip,rk3506-saradc
- const: rockchip,rk3528-saradc

Ha! Completely missed that, thanks the clarification.

Quoted 33 lines
Quoted 6 lines
Quoted 4 lines

1 GHz clock rate. However, the CLK_SARADC used on RK3506 can only be configured to use a 32 KHz, 400 KHz or 1.5-24 GHz rate.

This is quite misleading. The parent clocks are 32KHz, 400KHz or 24MHz. The divider is between 1 and 16.

You are correct, I must have mixed up this clock with a different clock that had multiple parent options and the divider was for one of those parent options.

Quoted 22 lines

This means that 1MHz is not achievable with the dividers and we need to aim for something else. Please reword the commit log to reflect this. I'm also not sure where people got 32KHz, 400KHz and 24MHz but it's what's specified in the Linux kernel, so fine with me :)

Quoted 16 lines

Add driver data for rockchip,rk3506-saradc that request use of a 1.5 GHz clock rate, to avoid clock divider overflow in the RK3506 clock driver.

---
   drivers/adc/rockchip-saradc.c | 10 ++++++++++
   1 file changed, 10 insertions(+)
drivers/adc/rockchip-saradc.c +0 -0
index 1515951403c9..50861b61eea4 100644
@@ -339,6 +339,14 @@ static const struct rockchip_saradc_data rk3399_saradc_data = {
   	.stop = rockchip_saradc_stop_v1,
   };
 
Quoted 8 lines
Quoted 4 lines

drivers/adc/rockchip-saradc.c static const struct rockchip_saradc_data rk3399_saradc_data = { · line 343

Quoted 2 lines
drivers/adc/rockchip-saradc.c +2 -0
+static const struct rockchip_saradc_data rk3506_saradc_data = {
+	.num_bits = 10,

It has 12 bits according to the TRM.

Hum, in the datasheet for RK3506J v1.2, RK3506B v1.4, RK3506G2 v1.5 and RK3506 TRM v1.1/v1.2 it lists 10-bit resolution.

Well, I read the register description and it says 11:0. But the first sentence in the SARADC section says "10-bit"... sooooo /me shrugs. Ideally, we should check whether the ADC returns close to 1023 (10 bits) or 4095 (12 bits) when it's fed 1.8V and then we would know for sure :)

Cheers,
Quentin

#19 lore raw

Re: [PATCH 3/6] adc: rockchip-saradc: Add driver data for RK3506

Hi Quentin,

Quoted 67 lines · Quentin Schulz

Hi Jonas,

Quoted 15 lines · Jonas Karlman

Hi Quentin,

Quoted 11 lines · Quentin Schulz

Hi Jonas,

Quoted 1 line · Jonas Karlman

Current rockchip,rk3528-saradc fallback instruct the driver to use a

There is no rk3506 DTSI available either in Linux kernel or U-Boot, so this fallback technically doesn't even exist and we don't have an issue (yet). Just say that none of the the v2 SARADC data match what the RK3506 can do (due to the divider and parent clocks not being able to achieve the requested 1MHz). Please simply rename the commit title to reflect you're adding support for the SARADC found on the RK3506.

Quoted 8 lines

Sure, I will update the commit message in a v2.

The rk3528 fallback mentioned was in reference to the dt-bindings:

- items:
- const: rockchip,rk3506-saradc
- const: rockchip,rk3528-saradc

Ha! Completely missed that, thanks the clarification.

Quoted 38 lines
Quoted 6 lines
Quoted 4 lines

1 GHz clock rate. However, the CLK_SARADC used on RK3506 can only be configured to use a 32 KHz, 400 KHz or 1.5-24 GHz rate.

This is quite misleading. The parent clocks are 32KHz, 400KHz or 24MHz. The divider is between 1 and 16.

You are correct, I must have mixed up this clock with a different clock that had multiple parent options and the divider was for one of those parent options.

Quoted 26 lines

This means that 1MHz is not achievable with the dividers and we need to aim for something else. Please reword the commit log to reflect this. I'm also not sure where people got 32KHz, 400KHz and 24MHz but it's what's specified in the Linux kernel, so fine with me :)

Quoted 13 lines

Add driver data for rockchip,rk3506-saradc that request use of a 1.5 GHz clock rate, to avoid clock divider overflow in the RK3506 clock driver.

---
   drivers/adc/rockchip-saradc.c | 10 ++++++++++
   1 file changed, 10 insertions(+)
drivers/adc/rockchip-saradc.c +0 -0
index 1515951403c9..50861b61eea4 100644

drivers/adc/rockchip-saradc.c static const struct rockchip_saradc_data rk3399_saradc_data = { · line 343

Quoted 5 lines
drivers/adc/rockchip-saradc.c +2 -0
@@ -339,6 +339,14 @@ static const struct rockchip_saradc_data rk3399_saradc_data = {
   	.stop = rockchip_saradc_stop_v1,
   };
 
+static const struct rockchip_saradc_data rk3506_saradc_data = {
+	.num_bits = 10,

It has 12 bits according to the TRM.

Quoted 8 lines
Quoted 3 lines

Hum, in the datasheet for RK3506J v1.2, RK3506B v1.4, RK3506G2 v1.5 and RK3506 TRM v1.1/v1.2 it lists 10-bit resolution.

Well, I read the register description and it says 11:0. But the first sentence in the SARADC section says "10-bit"... sooooo /me shrugs. Ideally, we should check whether the ADC returns close to 1023 (10 bits) or 4095 (12 bits) when it's fed 1.8V and then we would know for sure :)

I did a quick 'adc scan' test on my ArmSoM Forge1.

With '.num_bits = 10':

=> adc scan adc@ff4e8000
[00]: 1019, 1792961 uV
[01]: 1019, 1792961 uV
[02]: 0, 0 uV
[03]: 1011, 1778885 uV

And with '.num_bits = 12':

=> adc scan adc@ff4e8000
[00]: 1019, 447912 uV
[01]: 1019, 447912 uV
[02]: 0, 0 uV
[03]: 1011, 444395 uV

RK3506G_REF_V11_20241114 lists ADC 0-1023 in the SARADC_IN0_BOOT table, with 1023 for FSPI--SDMMC(eMMC/SD Card)--USB/SPI2APB.

So with this data I think 10-bit should be correct, without having to measure or apply 1.8v on an ADC pin.

Regards,
Jonas

Quoted 3 lines

Cheers,
Quentin

Reviewed-by
#20 lore raw

Re: [PATCH 3/6] adc: rockchip-saradc: Add driver data for RK3506

Hi Jonas,

Quoted 97 lines · Jonas Karlman

Hi Quentin,

Quoted 67 lines · Quentin Schulz

Hi Jonas,

Quoted 15 lines · Jonas Karlman

Hi Quentin,

Quoted 11 lines · Quentin Schulz

Hi Jonas,

Quoted 1 line · Jonas Karlman

Current rockchip,rk3528-saradc fallback instruct the driver to use a

There is no rk3506 DTSI available either in Linux kernel or U-Boot, so this fallback technically doesn't even exist and we don't have an issue (yet). Just say that none of the the v2 SARADC data match what the RK3506 can do (due to the divider and parent clocks not being able to achieve the requested 1MHz). Please simply rename the commit title to reflect you're adding support for the SARADC found on the RK3506.

Quoted 8 lines

Sure, I will update the commit message in a v2.

The rk3528 fallback mentioned was in reference to the dt-bindings:

- items:
- const: rockchip,rk3506-saradc
- const: rockchip,rk3528-saradc

Ha! Completely missed that, thanks the clarification.

Quoted 38 lines
Quoted 6 lines
Quoted 4 lines

1 GHz clock rate. However, the CLK_SARADC used on RK3506 can only be configured to use a 32 KHz, 400 KHz or 1.5-24 GHz rate.

This is quite misleading. The parent clocks are 32KHz, 400KHz or 24MHz. The divider is between 1 and 16.

You are correct, I must have mixed up this clock with a different clock that had multiple parent options and the divider was for one of those parent options.

Quoted 26 lines

This means that 1MHz is not achievable with the dividers and we need to aim for something else. Please reword the commit log to reflect this. I'm also not sure where people got 32KHz, 400KHz and 24MHz but it's what's specified in the Linux kernel, so fine with me :)

Quoted 13 lines

Add driver data for rockchip,rk3506-saradc that request use of a 1.5 GHz clock rate, to avoid clock divider overflow in the RK3506 clock driver.

---
    drivers/adc/rockchip-saradc.c | 10 ++++++++++
    1 file changed, 10 insertions(+)
drivers/adc/rockchip-saradc.c +0 -0
index 1515951403c9..50861b61eea4 100644

drivers/adc/rockchip-saradc.c static const struct rockchip_saradc_data rk3399_saradc_data = { · line 343

Quoted 5 lines
drivers/adc/rockchip-saradc.c +2 -0
@@ -339,6 +339,14 @@ static const struct rockchip_saradc_data rk3399_saradc_data = {
    	.stop = rockchip_saradc_stop_v1,
    };
 
+static const struct rockchip_saradc_data rk3506_saradc_data = {
+	.num_bits = 10,

It has 12 bits according to the TRM.

Quoted 8 lines
Quoted 3 lines

Hum, in the datasheet for RK3506J v1.2, RK3506B v1.4, RK3506G2 v1.5 and RK3506 TRM v1.1/v1.2 it lists 10-bit resolution.

Well, I read the register description and it says 11:0. But the first sentence in the SARADC section says "10-bit"... sooooo /me shrugs. Ideally, we should check whether the ADC returns close to 1023 (10 bits) or 4095 (12 bits) when it's fed 1.8V and then we would know for sure :)

I did a quick 'adc scan' test on my ArmSoM Forge1.

With '.num_bits = 10':

   => adc scan adc@ff4e8000
   [00]: 1019, 1792961 uV
   [01]: 1019, 1792961 uV
   [02]: 0, 0 uV
   [03]: 1011, 1778885 uV

And with '.num_bits = 12':

   => adc scan adc@ff4e8000
   [00]: 1019, 447912 uV
   [01]: 1019, 447912 uV
   [02]: 0, 0 uV
   [03]: 1011, 444395 uV
Quoted 6 lines

RK3506G_REF_V11_20241114 lists ADC 0-1023 in the SARADC_IN0_BOOT table, with 1023 for FSPI--SDMMC(eMMC/SD Card)--USB/SPI2APB.

So with this data I think 10-bit should be correct, without having to measure or apply 1.8v on an ADC pin.

Yeah, that would be around 1.8V on channel 0.

Looknig at the schematics, you have a pull-up to VCC_1V8 so that checks out. Thanks for checking.

With the commit log fixed (misleading divider for only one parent, GHz->MHz, + add a small word that the fallback comes from the Device Tree binding):

Thanks!
Quentin

#21 lore raw

PATCH 4/6 clk: rockchip: rk3506: Fix CLK_SARADC set rate issues

In reply to Jonas Karlman

The set_rate ops for the CLK_SARADC clock in RK3506 clock driver force use of 32 KHz or 400 KHz rates when any requested rate is divisible with 32 KHz or 400 KHz.

Adjust logic to better match the 32 KHz, 400 KHz or 1.5-24 GHz rate limitation of the CLK_SARADC clock in RK3506.

---
 drivers/clk/rockchip/clk_rk3506.c | 13 +++++--------
 1 file changed, 5 insertions(+), 8 deletions(-)
drivers/clk/rockchip/clk_rk3506.c +5 -8
index 457082eea87b..e156bf19a6b4 100644
@@ -440,22 +440,19 @@ static ulong rk3506_sdmmc_set_rate(struct rk3506_clk_priv *priv, ulong clk_id,
 static ulong rk3506_saradc_get_rate(struct rk3506_clk_priv *priv, ulong clk_id)
 {
 	u32 con, div, sel;
-	ulong prate;
 
 	con = readl(RK3506_CLKSEL_CON(54));
 	sel = FIELD_GET(CLK_SARADC_SEL_MASK, con);
 	div = FIELD_GET(CLK_SARADC_DIV_MASK, con);
 
 	if (sel == CLK_SARADC_SEL_24M)
-		prate = OSC_HZ;
+		return DIV_TO_RATE(OSC_HZ, div);
 	else if (sel == CLK_SARADC_SEL_400K)
-		prate = 400000;
+		return 400000;
 	else if (sel == CLK_SARADC_SEL_32K)
-		prate = 32000;
+		return 32000;
 	else
 		return -EINVAL;
-
-	return DIV_TO_RATE(prate, div);
 }
 
 static ulong rk3506_saradc_set_rate(struct rk3506_clk_priv *priv, ulong clk_id,
@@ -463,10 +460,10 @@ static ulong rk3506_saradc_set_rate(struct rk3506_clk_priv *priv, ulong clk_id,
 {
 	u32 div, sel;
 
-	if (32000 % rate == 0) {
+	if (rate <= 32768) {
 		sel = CLK_SARADC_SEL_32K;
 		div = 1;
-	} else if (400000 % rate == 0) {
+	} else if (rate == 400000) {
 		sel = CLK_SARADC_SEL_400K;
 		div = 1;
 	} else {
Signature · 1 line
2.54.0
#22 lore raw

Re: [PATCH 4/6] clk: rockchip: rk3506: Fix CLK_SARADC set rate issues

Hi Jonas,

Quoted 8 lines · Jonas Karlman

clk: rockchip: rk3506: Fix CLK_SARADC set rate issues

The set_rate ops for the CLK_SARADC clock in RK3506 clock driver force use of 32 KHz or 400 KHz rates when any requested rate is divisible with 32 KHz or 400 KHz.

Adjust logic to better match the 32 KHz, 400 KHz or 1.5-24 GHz rate limitation of the CLK_SARADC clock in RK3506.

Shouldn't that should be 1.5-24 MHz, not GHz?

Quoted 17 lines

drivers/clk/rockchip/clk_rk3506.c | 13 +++++--------

 1 file changed, 5 insertions(+), 8 deletions(-)
drivers/clk/rockchip/clk_rk3506.c +0 -1
@@ -440,22 +440,19 @@ static ulong rk3506_sdmmc_set_rate(struct rk3506_clk_priv *priv, ulong clk_id,
 static ulong rk3506_saradc_get_rate(struct rk3506_clk_priv *priv, ulong clk_id)
 {
      u32 con, div, sel;
-     ulong prate;
 
 
      con = readl(RK3506_CLKSEL_CON(54));
      sel = FIELD_GET(CLK_SARADC_SEL_MASK, con);
      div = FIELD_GET(CLK_SARADC_DIV_MASK, con);

drivers/clk/rockchip/clk_rk3506.c static ulong rk3506_sdmmc_set_rate(struct rk3506_clk_priv *priv, ulong clk_id, · line 454

Quoted 10 lines
drivers/clk/rockchip/clk_rk3506.c +3 -3
 
      if (sel == CLK_SARADC_SEL_24M)
-             prate = OSC_HZ;
+             return DIV_TO_RATE(OSC_HZ, div);
      else if (sel == CLK_SARADC_SEL_400K)
-             prate = 400000;
+             return 400000;
      else if (sel == CLK_SARADC_SEL_32K)
-             prate = 32000;
+             return 32000;

The commit message only mentions set_rate, but this changes get_rate to ignore the divider on the 32K/400K paths. Please mention this and explain why - I assume the hardware divider is only wired to the 24 MHz mux input, but that isn't obvious from the code. Also div is now read but unused on those two paths.

drivers/clk/rockchip/clk_rk3506.c static ulong rk3506_saradc_set_rate(struct rk3506_clk_priv *priv, ulong clk_id, · line 466

Quoted 10 lines
drivers/clk/rockchip/clk_rk3506.c +1 -1
@@ -463,10 +460,10 @@ static ulong rk3506_saradc_set_rate(struct rk3506_clk_priv *priv, ulong clk_id,
 {
      u32 div, sel;
 
 
-     if (32000 % rate == 0) {
+     if (rate <= 32768) {
              sel = CLK_SARADC_SEL_32K;
              div = 1;

The threshold 32768 is odd given that get_rate reports this source as 32000 - please pick one value and use it consistently, or add a comment. I suspect the real source is 32.768 kHz, in which case get_rate should return 32768.

Regards,
Simon

#23 lore raw

Re: [PATCH 4/6] clk: rockchip: rk3506: Fix CLK_SARADC set rate issues

Hi Simon,

Quoted 42 lines · Simon Glass

Hi Jonas,

Quoted 8 lines · Jonas Karlman

clk: rockchip: rk3506: Fix CLK_SARADC set rate issues

The set_rate ops for the CLK_SARADC clock in RK3506 clock driver force use of 32 KHz or 400 KHz rates when any requested rate is divisible with 32 KHz or 400 KHz.

Adjust logic to better match the 32 KHz, 400 KHz or 1.5-24 GHz rate limitation of the CLK_SARADC clock in RK3506.

Shouldn't that should be 1.5-24 MHz, not GHz?

Quoted 17 lines

drivers/clk/rockchip/clk_rk3506.c | 13 +++++--------

 1 file changed, 5 insertions(+), 8 deletions(-)
drivers/clk/rockchip/clk_rk3506.c +0 -1
@@ -440,22 +440,19 @@ static ulong rk3506_sdmmc_set_rate(struct rk3506_clk_priv *priv, ulong clk_id,
 static ulong rk3506_saradc_get_rate(struct rk3506_clk_priv *priv, ulong clk_id)
 {
      u32 con, div, sel;
-     ulong prate;
 
 
      con = readl(RK3506_CLKSEL_CON(54));
      sel = FIELD_GET(CLK_SARADC_SEL_MASK, con);
      div = FIELD_GET(CLK_SARADC_DIV_MASK, con);

drivers/clk/rockchip/clk_rk3506.c static ulong rk3506_sdmmc_set_rate(struct rk3506_clk_priv *priv, ulong clk_id, · line 454

Quoted 10 lines
drivers/clk/rockchip/clk_rk3506.c +3 -3
 
      if (sel == CLK_SARADC_SEL_24M)
-             prate = OSC_HZ;
+             return DIV_TO_RATE(OSC_HZ, div);
      else if (sel == CLK_SARADC_SEL_400K)
-             prate = 400000;
+             return 400000;
      else if (sel == CLK_SARADC_SEL_32K)
-             prate = 32000;
+             return 32000;
Quoted 5 lines

The commit message only mentions set_rate, but this changes get_rate to ignore the divider on the 32K/400K paths. Please mention this and explain why - I assume the hardware divider is only wired to the 24 MHz mux input, but that isn't obvious from the code. Also div is now read but unused on those two paths.

The divider is configured to 1 in set_rate path, so this change is a simplification to ensure the ops for CLK_SARADC works as expected had set_rate been run.

The commit message already mentions that logic is adjusted to better match the limitation of the CLK_SARADC clock.

Quoted 12 lines

drivers/clk/rockchip/clk_rk3506.c static ulong rk3506_saradc_set_rate(struct rk3506_clk_priv *priv, ulong clk_id, · line 466

Quoted 11 lines
drivers/clk/rockchip/clk_rk3506.c +1 -1
@@ -463,10 +460,10 @@ static ulong rk3506_saradc_set_rate(struct rk3506_clk_priv *priv, ulong clk_id,
 {
      u32 div, sel;
 
 
-     if (32000 % rate == 0) {
+     if (rate <= 32768) {
              sel = CLK_SARADC_SEL_32K;
              div = 1;
Quoted 4 lines

The threshold 32768 is odd given that get_rate reports this source as 32000 - please pick one value and use it consistently, or add a comment. I suspect the real source is 32.768 kHz, in which case get_rate should return 32768.

I would also think it should be 32768, however TRM only state 32K and vendor U-Boot use 32000.

Due to this uncertainty and how the fallback use a very narrow divider field, 4-bits, it is more appropriate to check for <= 32768 or rate may have fallback to 1.5-24 MHz depending on bits [3:0] of the DIV_ROUND_UP(24000000, rate)-1 when close to 32 KHz is requested.

I still think this change fits under adjust logic to fit clock limitations.

Regards,
Jonas

Quoted 3 lines

Regards,
Simon

#24 lore raw

Re: [PATCH 4/6] clk: rockchip: rk3506: Fix CLK_SARADC set rate issues

Hi Jonas,

Quoted 79 lines · Jonas Karlman

Hi Simon,

Quoted 42 lines · Simon Glass

Hi Jonas,

Quoted 8 lines · Jonas Karlman

clk: rockchip: rk3506: Fix CLK_SARADC set rate issues

The set_rate ops for the CLK_SARADC clock in RK3506 clock driver force use of 32 KHz or 400 KHz rates when any requested rate is divisible with 32 KHz or 400 KHz.

Adjust logic to better match the 32 KHz, 400 KHz or 1.5-24 GHz rate limitation of the CLK_SARADC clock in RK3506.

Shouldn't that should be 1.5-24 MHz, not GHz?

Quoted 17 lines

drivers/clk/rockchip/clk_rk3506.c | 13 +++++--------

 1 file changed, 5 insertions(+), 8 deletions(-)
drivers/clk/rockchip/clk_rk3506.c +0 -1
@@ -440,22 +440,19 @@ static ulong rk3506_sdmmc_set_rate(struct rk3506_clk_priv *priv, ulong clk_id,
 static ulong rk3506_saradc_get_rate(struct rk3506_clk_priv *priv, ulong clk_id)
 {
      u32 con, div, sel;
-     ulong prate;
 
 
      con = readl(RK3506_CLKSEL_CON(54));
      sel = FIELD_GET(CLK_SARADC_SEL_MASK, con);
      div = FIELD_GET(CLK_SARADC_DIV_MASK, con);

drivers/clk/rockchip/clk_rk3506.c static ulong rk3506_sdmmc_set_rate(struct rk3506_clk_priv *priv, ulong clk_id, · line 454

Quoted 10 lines
drivers/clk/rockchip/clk_rk3506.c +3 -3
 
      if (sel == CLK_SARADC_SEL_24M)
-             prate = OSC_HZ;
+             return DIV_TO_RATE(OSC_HZ, div);
      else if (sel == CLK_SARADC_SEL_400K)
-             prate = 400000;
+             return 400000;
      else if (sel == CLK_SARADC_SEL_32K)
-             prate = 32000;
+             return 32000;
Quoted 5 lines

The commit message only mentions set_rate, but this changes get_rate to ignore the divider on the 32K/400K paths. Please mention this and explain why - I assume the hardware divider is only wired to the 24 MHz mux input, but that isn't obvious from the code. Also div is now read but unused on those two paths.

The divider is configured to 1 in set_rate path, so this change is a simplification to ensure the ops for CLK_SARADC works as expected had set_rate been run.

The commit message already mentions that logic is adjusted to better match the limitation of the CLK_SARADC clock.

Quoted 12 lines

drivers/clk/rockchip/clk_rk3506.c static ulong rk3506_saradc_set_rate(struct rk3506_clk_priv *priv, ulong clk_id, · line 466

Quoted 11 lines
drivers/clk/rockchip/clk_rk3506.c +1 -1
@@ -463,10 +460,10 @@ static ulong rk3506_saradc_set_rate(struct rk3506_clk_priv *priv, ulong clk_id,
 {
      u32 div, sel;
 
 
-     if (32000 % rate == 0) {
+     if (rate <= 32768) {
              sel = CLK_SARADC_SEL_32K;
              div = 1;
Quoted 4 lines

The threshold 32768 is odd given that get_rate reports this source as 32000 - please pick one value and use it consistently, or add a comment. I suspect the real source is 32.768 kHz, in which case get_rate should return 32768.

I would also think it should be 32768, however TRM only state 32K and vendor U-Boot use 32000.

Quoted 7 lines

Due to this uncertainty and how the fallback use a very narrow divider field, 4-bits, it is more appropriate to check for <= 32768 or rate may have fallback to 1.5-24 MHz depending on bits [3:0] of the DIV_ROUND_UP(24000000, rate)-1 when close to 32 KHz is requested.

I still think this change fits under adjust logic to fit clock limitations.

Yes, agreed.

Regards,
Simon

#25 lore raw

Re: [PATCH 4/6] clk: rockchip: rk3506: Fix CLK_SARADC set rate issues

In reply to Jonas Karlman

Hi Jonas,

Quoted 7 lines · Jonas Karlman

The set_rate ops for the CLK_SARADC clock in RK3506 clock driver force use of 32 KHz or 400 KHz rates when any requested rate is divisible with 32 KHz or 400 KHz.

Adjust logic to better match the 32 KHz, 400 KHz or 1.5-24 GHz rate limitation of the CLK_SARADC clock in RK3506.

Same typo as previous patch, it's MHz and not GHz. Same remark as previous patch, the divider applies to any of the parent clock so the 1.5-24MHz is kinda misleading.

Quoted 22 lines
---
  drivers/clk/rockchip/clk_rk3506.c | 13 +++++--------
  1 file changed, 5 insertions(+), 8 deletions(-)
drivers/clk/rockchip/clk_rk3506.c +1 -2
index 457082eea87b..e156bf19a6b4 100644
@@ -440,22 +440,19 @@ static ulong rk3506_sdmmc_set_rate(struct rk3506_clk_priv *priv, ulong clk_id,
  static ulong rk3506_saradc_get_rate(struct rk3506_clk_priv *priv, ulong clk_id)
  {
  	u32 con, div, sel;
-	ulong prate;
 
  	con = readl(RK3506_CLKSEL_CON(54));
  	sel = FIELD_GET(CLK_SARADC_SEL_MASK, con);
  	div = FIELD_GET(CLK_SARADC_DIV_MASK, con);
 
  	if (sel == CLK_SARADC_SEL_24M)
-		prate = OSC_HZ;
+		return DIV_TO_RATE(OSC_HZ, div);

drivers/clk/rockchip/clk_rk3506.c static ulong rk3506_sdmmc_set_rate(struct rk3506_clk_priv *priv, ulong clk_id, · line 458

Quoted 10 lines
drivers/clk/rockchip/clk_rk3506.c +2 -4
  	else if (sel == CLK_SARADC_SEL_400K)
-		prate = 400000;
+		return 400000;
  	else if (sel == CLK_SARADC_SEL_32K)
-		prate = 32000;
+		return 32000;
  	else
  		return -EINVAL;
-
-	return DIV_TO_RATE(prate, div);

I'm not sure this is correct. The divider applies to any parent clock and this applies the divider only to the 24MHz parent clock.

Quoted 5 lines
drivers/clk/rockchip/clk_rk3506.c +0 -0
  }
 
  static ulong rk3506_saradc_set_rate(struct rk3506_clk_priv *priv, ulong clk_id,
@@ -463,10 +460,10 @@ static ulong rk3506_saradc_set_rate(struct rk3506_clk_priv *priv, ulong clk_id,
  {

drivers/clk/rockchip/clk_rk3506.c static ulong rk3506_saradc_set_rate(struct rk3506_clk_priv *priv, ulong clk_id, · line 468

Quoted 10 lines
drivers/clk/rockchip/clk_rk3506.c +2 -2
  	u32 div, sel;
 
-	if (32000 % rate == 0) {
+	if (rate <= 32768) {
  		sel = CLK_SARADC_SEL_32K;
  		div = 1;
-	} else if (400000 % rate == 0) {
+	} else if (rate == 400000) {
  		sel = CLK_SARADC_SEL_400K;
  		div = 1;

This is also not correct.

I think what you want instead is to modify div to not be always 1 but rather 32000 / rate and 4000000 / rate.

Cheers,
Quentin

#26 lore raw

Re: [PATCH 4/6] clk: rockchip: rk3506: Fix CLK_SARADC set rate issues

Quoted 64 lines · Quentin Schulz

Hi Jonas,

Quoted 7 lines · Jonas Karlman

The set_rate ops for the CLK_SARADC clock in RK3506 clock driver force use of 32 KHz or 400 KHz rates when any requested rate is divisible with 32 KHz or 400 KHz.

Adjust logic to better match the 32 KHz, 400 KHz or 1.5-24 GHz rate limitation of the CLK_SARADC clock in RK3506.

Same typo as previous patch, it's MHz and not GHz. Same remark as previous patch, the divider applies to any of the parent clock so the 1.5-24MHz is kinda misleading.

Quoted 27 lines

---
  drivers/clk/rockchip/clk_rk3506.c | 13 +++++--------

  1 file changed, 5 insertions(+), 8 deletions(-)
drivers/clk/rockchip/clk_rk3506.c +3 -4
index 457082eea87b..e156bf19a6b4 100644
@@ -440,22 +440,19 @@ static ulong rk3506_sdmmc_set_rate(struct 
 rk3506_clk_priv *priv, ulong clk_id,
   static ulong rk3506_saradc_get_rate(struct rk3506_clk_priv *priv, ulong clk_id)
   {
       u32 con, div, sel;
-    ulong prate;
       con = readl(RK3506_CLKSEL_CON(54));
       sel = FIELD_GET(CLK_SARADC_SEL_MASK, con);
       div = FIELD_GET(CLK_SARADC_DIV_MASK, con);
       if (sel == CLK_SARADC_SEL_24M)
-        prate = OSC_HZ;
+        return DIV_TO_RATE(OSC_HZ, div);
       else if (sel == CLK_SARADC_SEL_400K)
-        prate = 400000;
+        return 400000;
       else if (sel == CLK_SARADC_SEL_32K)
-        prate = 32000;
+        return 32000;
Quoted 4 lines

      else
          return -EINVAL;
-
-    return DIV_TO_RATE(prate, div);

I'm not sure this is correct. The divider applies to any parent clock and this applies the divider only to the 24MHz parent clock.

Quoted 4 lines

  }
  static ulong rk3506_saradc_set_rate(struct rk3506_clk_priv *priv, ulong clk_id,

drivers/clk/rockchip/clk_rk3506.c +0 -0
@@ -463,10 +460,10 @@ static ulong rk3506_saradc_set_rate(struct 
 rk3506_clk_priv *priv, ulong clk_id,

drivers/clk/rockchip/clk_rk3506.c static ulong rk3506_saradc_set_rate(struct · line 468

Quoted 10 lines
drivers/clk/rockchip/clk_rk3506.c +2 -2
   {
       u32 div, sel;
-    if (32000 % rate == 0) {
+    if (rate <= 32768) {
           sel = CLK_SARADC_SEL_32K;
           div = 1;
-    } else if (400000 % rate == 0) {
+    } else if (rate == 400000) {
           sel = CLK_SARADC_SEL_400K;
           div = 1;

This is also not correct.

Quoted 1 line

I think what you want instead is to modify div to not be always 1 but rather 32000 / rate and 4000000 / rate.

Is any of those two clocks externally provided by any chance? Heiko told me the 32K clock is generally externally provided, usually by an RTC, and thus I believe we really shouldn't allow those clocks to be selected if they indeed are external. I don't think we have a mechanism in place to enable the external clock and make sure it's running at the expected clock rate. So maybe the answer is to actually remove support for anything but the 24MHz clock for the SARADC (and other IPs whose parent clocks can be clk_rc and/or clk_deepslow).

Cheers,
Quentin

#27 lore raw

PATCH 5/6 clk: rockchip: rk3506: Use FIELD_PREP_WM16 macro

In reply to Jonas Karlman

Change to use writel() together with the FIELD_PREP_WM16() macro instead of using the rk_clrsetreg() macro to avoid having to define the mask as a parameter to both rk_clrsetreg() and FIELD_PREP(). Also change to use u32 variables consistently.

No change in behavior is expected due to this code style change.

---
 drivers/clk/rockchip/clk_rk3506.c | 195 ++++++++++++++----------------
 1 file changed, 90 insertions(+), 105 deletions(-)
drivers/clk/rockchip/clk_rk3506.c +90 -105
index e156bf19a6b4..7ccab313e6ec 100644
@@ -9,10 +9,11 @@
 #include <clk-uclass.h>
 #include <asm/arch-rockchip/clock.h>
 #include <asm/arch-rockchip/cru_rk3506.h>
-#include <asm/arch-rockchip/hardware.h>
+#include <asm/io.h>
 #include <dm/device-internal.h>
 #include <dm/lists.h>
 #include <dt-bindings/clock/rockchip,rk3506-cru.h>
+#include <linux/hw_bitfield.h>
 
 #define DIV_TO_RATE(input_rate, div)    ((input_rate) / ((div) + 1))
 
@@ -122,10 +123,10 @@ static int rk3506_armclk_set_rate(struct rk3506_clk_priv *priv, ulong new_rate)
 	 */
 	old_rate = rk3506_armclk_get_rate(priv);
 	if (new_rate >= old_rate) {
-		rk_clrsetreg(RK3506_CLKSEL_CON(15), ACLK_CORE_DIV_MASK,
-			     FIELD_PREP(ACLK_CORE_DIV_MASK, rate->aclk_div));
-		rk_clrsetreg(RK3506_CLKSEL_CON(16), PCLK_CORE_DIV_MASK,
-			     FIELD_PREP(PCLK_CORE_DIV_MASK, rate->pclk_div));
+		writel(FIELD_PREP_WM16(ACLK_CORE_DIV_MASK, rate->aclk_div),
+		       RK3506_CLKSEL_CON(15));
+		writel(FIELD_PREP_WM16(PCLK_CORE_DIV_MASK, rate->pclk_div),
+		       RK3506_CLKSEL_CON(16));
 	}
 
 	if (new_rate == 589824000 || new_rate == 1179648000) {
@@ -146,22 +147,22 @@ static int rk3506_armclk_set_rate(struct rk3506_clk_priv *priv, ulong new_rate)
 	con = readl(RK3506_CLKSEL_CON(15));
 	old_div = FIELD_GET(CLK_CORE_SRC_DIV_MASK, con);
 	if (DIV_TO_RATE(prate, old_div) > new_rate) {
-		rk_clrsetreg(RK3506_CLKSEL_CON(15), CLK_CORE_SRC_DIV_MASK,
-			     FIELD_PREP(CLK_CORE_SRC_DIV_MASK, div - 1));
-		rk_clrsetreg(RK3506_CLKSEL_CON(15), CLK_CORE_SRC_SEL_MASK,
-			     FIELD_PREP(CLK_CORE_SRC_SEL_MASK, sel));
+		writel(FIELD_PREP_WM16(CLK_CORE_SRC_DIV_MASK, div - 1),
+		       RK3506_CLKSEL_CON(15));
+		writel(FIELD_PREP_WM16(CLK_CORE_SRC_SEL_MASK, sel),
+		       RK3506_CLKSEL_CON(15));
 	} else {
-		rk_clrsetreg(RK3506_CLKSEL_CON(15), CLK_CORE_SRC_SEL_MASK,
-			     FIELD_PREP(CLK_CORE_SRC_SEL_MASK, sel));
-		rk_clrsetreg(RK3506_CLKSEL_CON(15), CLK_CORE_SRC_DIV_MASK,
-			     FIELD_PREP(CLK_CORE_SRC_DIV_MASK, div - 1));
+		writel(FIELD_PREP_WM16(CLK_CORE_SRC_SEL_MASK, sel),
+		       RK3506_CLKSEL_CON(15));
+		writel(FIELD_PREP_WM16(CLK_CORE_SRC_DIV_MASK, div - 1),
+		       RK3506_CLKSEL_CON(15));
 	}
 
 	if (new_rate < old_rate) {
-		rk_clrsetreg(RK3506_CLKSEL_CON(15), ACLK_CORE_DIV_MASK,
-			     FIELD_PREP(ACLK_CORE_DIV_MASK, rate->aclk_div));
-		rk_clrsetreg(RK3506_CLKSEL_CON(16), PCLK_CORE_DIV_MASK,
-			     FIELD_PREP(PCLK_CORE_DIV_MASK, rate->pclk_div));
+		writel(FIELD_PREP_WM16(ACLK_CORE_DIV_MASK, rate->aclk_div),
+		       RK3506_CLKSEL_CON(15));
+		writel(FIELD_PREP_WM16(PCLK_CORE_DIV_MASK, rate->pclk_div),
+		       RK3506_CLKSEL_CON(16));
 	}
 
 	return rk3506_armclk_get_rate(priv);
@@ -209,26 +210,26 @@ static ulong rk3506_pll_div_set_rate(struct rk3506_clk_priv *priv, ulong clk_id,
 	case CLK_GPLL_DIV:
 		div = DIV_ROUND_UP(priv->gpll_hz, rate);
 		assert(div - 1 <= 15);
-		rk_clrsetreg(RK3506_CLKSEL_CON(0), CLK_GPLL_DIV_MASK,
-			     FIELD_PREP(CLK_GPLL_DIV_MASK, div - 1));
+		writel(FIELD_PREP_WM16(CLK_GPLL_DIV_MASK, div - 1),
+		       RK3506_CLKSEL_CON(0));
 		break;
 	case CLK_GPLL_DIV_100M:
 		div = DIV_ROUND_UP(priv->gpll_div_hz, rate);
 		assert(div - 1 <= 15);
-		rk_clrsetreg(RK3506_CLKSEL_CON(0), CLK_GPLL_DIV_100M_MASK,
-			     FIELD_PREP(CLK_GPLL_DIV_100M_MASK, div - 1));
+		writel(FIELD_PREP_WM16(CLK_GPLL_DIV_100M_MASK, div - 1),
+		       RK3506_CLKSEL_CON(0));
 		break;
 	case CLK_V0PLL_DIV:
 		div = DIV_ROUND_UP(priv->v0pll_hz, rate);
 		assert(div - 1 <= 15);
-		rk_clrsetreg(RK3506_CLKSEL_CON(1), CLK_V0PLL_DIV_MASK,
-			     FIELD_PREP(CLK_V0PLL_DIV_MASK, div - 1));
+		writel(FIELD_PREP_WM16(CLK_V0PLL_DIV_MASK, div - 1),
+		       RK3506_CLKSEL_CON(1));
 		break;
 	case CLK_V1PLL_DIV:
 		div = DIV_ROUND_UP(priv->v1pll_hz, rate);
 		assert(div - 1 <= 15);
-		rk_clrsetreg(RK3506_CLKSEL_CON(1), CLK_V1PLL_DIV_MASK,
-			     FIELD_PREP(CLK_V1PLL_DIV_MASK, div - 1));
+		writel(FIELD_PREP_WM16(CLK_V1PLL_DIV_MASK, div - 1),
+		       RK3506_CLKSEL_CON(1));
 		break;
 	default:
 		return -ENOENT;
@@ -293,22 +294,19 @@ static ulong rk3506_bus_set_rate(struct rk3506_clk_priv *priv, ulong clk_id,
 
 	switch (clk_id) {
 	case ACLK_BUS_ROOT:
-		rk_clrsetreg(RK3506_CLKSEL_CON(21),
-			     ACLK_BUS_SEL_MASK | ACLK_BUS_DIV_MASK,
-			     FIELD_PREP(ACLK_BUS_SEL_MASK, sel) |
-			     FIELD_PREP(ACLK_BUS_DIV_MASK, div - 1));
+		writel(FIELD_PREP_WM16(ACLK_BUS_SEL_MASK, sel) |
+		       FIELD_PREP_WM16(ACLK_BUS_DIV_MASK, div - 1),
+		       RK3506_CLKSEL_CON(21));
 		break;
 	case HCLK_BUS_ROOT:
-		rk_clrsetreg(RK3506_CLKSEL_CON(21),
-			     HCLK_BUS_SEL_MASK | HCLK_BUS_DIV_MASK,
-			     FIELD_PREP(HCLK_BUS_SEL_MASK, sel) |
-			     FIELD_PREP(HCLK_BUS_DIV_MASK, div - 1));
+		writel(FIELD_PREP_WM16(HCLK_BUS_SEL_MASK, sel) |
+		       FIELD_PREP_WM16(HCLK_BUS_DIV_MASK, div - 1),
+		       RK3506_CLKSEL_CON(21));
 		break;
 	case PCLK_BUS_ROOT:
-		rk_clrsetreg(RK3506_CLKSEL_CON(22),
-			     PCLK_BUS_SEL_MASK | PCLK_BUS_DIV_MASK,
-			     FIELD_PREP(PCLK_BUS_SEL_MASK, sel) |
-			     FIELD_PREP(PCLK_BUS_DIV_MASK, div - 1));
+		writel(FIELD_PREP_WM16(PCLK_BUS_SEL_MASK, sel) |
+		       FIELD_PREP_WM16(PCLK_BUS_DIV_MASK, div - 1),
+		       RK3506_CLKSEL_CON(22));
 		break;
 	default:
 		return -ENOENT;
@@ -368,16 +366,14 @@ static ulong rk3506_peri_set_rate(struct rk3506_clk_priv *priv, ulong clk_id,
 
 	switch (clk_id) {
 	case ACLK_HSPERI_ROOT:
-		rk_clrsetreg(RK3506_CLKSEL_CON(49),
-			     ACLK_HSPERI_SEL_MASK | ACLK_HSPERI_DIV_MASK,
-			     FIELD_PREP(ACLK_HSPERI_SEL_MASK, sel) |
-			     FIELD_PREP(ACLK_HSPERI_DIV_MASK, div - 1));
+		writel(FIELD_PREP_WM16(ACLK_HSPERI_SEL_MASK, sel) |
+		       FIELD_PREP_WM16(ACLK_HSPERI_DIV_MASK, div - 1),
+		       RK3506_CLKSEL_CON(49));
 		break;
 	case HCLK_LSPERI_ROOT:
-		rk_clrsetreg(RK3506_CLKSEL_CON(29),
-			     HCLK_LSPERI_SEL_MASK | HCLK_LSPERI_DIV_MASK,
-			     FIELD_PREP(HCLK_LSPERI_SEL_MASK, sel) |
-			     FIELD_PREP(HCLK_LSPERI_DIV_MASK, div - 1));
+		writel(FIELD_PREP_WM16(HCLK_LSPERI_SEL_MASK, sel) |
+		       FIELD_PREP_WM16(HCLK_LSPERI_DIV_MASK, div - 1),
+		       RK3506_CLKSEL_CON(29));
 		break;
 	default:
 		return -ENOENT;
@@ -429,10 +425,9 @@ static ulong rk3506_sdmmc_set_rate(struct rk3506_clk_priv *priv, ulong clk_id,
 	}
 	assert(div - 1 <= 63);
 
-	rk_clrsetreg(RK3506_CLKSEL_CON(49),
-		     CCLK_SDMMC_SEL_MASK | CCLK_SDMMC_DIV_MASK,
-		     FIELD_PREP(CCLK_SDMMC_SEL_MASK, sel) |
-		     FIELD_PREP(CCLK_SDMMC_DIV_MASK, div - 1));
+	writel(FIELD_PREP_WM16(CCLK_SDMMC_SEL_MASK, sel) |
+	       FIELD_PREP_WM16(CCLK_SDMMC_DIV_MASK, div - 1),
+	       RK3506_CLKSEL_CON(49));
 
 	return rk3506_sdmmc_get_rate(priv, clk_id);
 }
@@ -472,10 +467,9 @@ static ulong rk3506_saradc_set_rate(struct rk3506_clk_priv *priv, ulong clk_id,
 	}
 	assert(div - 1 <= 15);
 
-	rk_clrsetreg(RK3506_CLKSEL_CON(54),
-		     CLK_SARADC_SEL_MASK | CLK_SARADC_DIV_MASK,
-		     FIELD_PREP(CLK_SARADC_SEL_MASK, sel) |
-		     FIELD_PREP(CLK_SARADC_DIV_MASK, div - 1));
+	writel(FIELD_PREP_WM16(CLK_SARADC_SEL_MASK, sel) |
+	       FIELD_PREP_WM16(CLK_SARADC_DIV_MASK, div - 1),
+	       RK3506_CLKSEL_CON(54));
 
 	return rk3506_saradc_get_rate(priv, clk_id);
 }
@@ -485,6 +479,7 @@ static ulong rk3506_tsadc_get_rate(struct rk3506_clk_priv *priv, ulong clk_id)
 	u32 con, div;
 
 	con = readl(RK3506_CLKSEL_CON(61));
+
 	switch (clk_id) {
 	case CLK_TSADC_TSEN:
 		div = FIELD_GET(CLK_TSADC_TSEN_DIV_MASK, con);
@@ -508,14 +503,14 @@ static ulong rk3506_tsadc_set_rate(struct rk3506_clk_priv *priv, ulong clk_id,
 	case CLK_TSADC_TSEN:
 		div = DIV_ROUND_UP(OSC_HZ, rate);
 		assert(div - 1 <= 7);
-		rk_clrsetreg(RK3506_CLKSEL_CON(61), CLK_TSADC_TSEN_DIV_MASK,
-			     FIELD_PREP(CLK_TSADC_TSEN_DIV_MASK, div - 1));
+		writel(FIELD_PREP_WM16(CLK_TSADC_TSEN_DIV_MASK, div - 1),
+		       RK3506_CLKSEL_CON(61));
 		break;
 	case CLK_TSADC:
 		div = DIV_ROUND_UP(OSC_HZ, rate);
 		assert(div - 1 <= 255);
-		rk_clrsetreg(RK3506_CLKSEL_CON(61), CLK_TSADC_DIV_MASK,
-			     FIELD_PREP(CLK_TSADC_DIV_MASK, div - 1));
+		writel(FIELD_PREP_WM16(CLK_TSADC_DIV_MASK, div - 1),
+		       RK3506_CLKSEL_CON(61));
 		break;
 	default:
 		return -ENOENT;
@@ -580,22 +575,19 @@ static ulong rk3506_i2c_set_rate(struct rk3506_clk_priv *priv, ulong clk_id,
 
 	switch (clk_id) {
 	case CLK_I2C0:
-		rk_clrsetreg(RK3506_CLKSEL_CON(32),
-			     CLK_I2C0_SEL_MASK | CLK_I2C0_DIV_MASK,
-			     FIELD_PREP(CLK_I2C0_SEL_MASK, sel) |
-			     FIELD_PREP(CLK_I2C0_DIV_MASK, div - 1));
+		writel(FIELD_PREP_WM16(CLK_I2C0_SEL_MASK, sel) |
+		       FIELD_PREP_WM16(CLK_I2C0_DIV_MASK, div - 1),
+		       RK3506_CLKSEL_CON(32));
 		break;
 	case CLK_I2C1:
-		rk_clrsetreg(RK3506_CLKSEL_CON(32),
-			     CLK_I2C1_SEL_MASK | CLK_I2C1_DIV_MASK,
-			     FIELD_PREP(CLK_I2C1_SEL_MASK, sel) |
-			     FIELD_PREP(CLK_I2C1_DIV_MASK, div - 1));
+		writel(FIELD_PREP_WM16(CLK_I2C1_SEL_MASK, sel) |
+		       FIELD_PREP_WM16(CLK_I2C1_DIV_MASK, div - 1),
+		       RK3506_CLKSEL_CON(32));
 		break;
 	case CLK_I2C2:
-		rk_clrsetreg(RK3506_CLKSEL_CON(33),
-			     CLK_I2C2_SEL_MASK | CLK_I2C2_DIV_MASK,
-			     FIELD_PREP(CLK_I2C2_SEL_MASK, sel) |
-			     FIELD_PREP(CLK_I2C2_DIV_MASK, div - 1));
+		writel(FIELD_PREP_WM16(CLK_I2C2_SEL_MASK, sel) |
+		       FIELD_PREP_WM16(CLK_I2C2_DIV_MASK, div - 1),
+		       RK3506_CLKSEL_CON(33));
 		break;
 	default:
 		return -ENOENT;
@@ -644,8 +636,8 @@ static ulong rk3506_pwm_set_rate(struct rk3506_clk_priv *priv, ulong clk_id,
 	case CLK_PWM0:
 		div = DIV_ROUND_UP(priv->gpll_div_100mhz, rate);
 		assert(div - 1 <= 15);
-		rk_clrsetreg(RK3506_PMU_CLKSEL_CON(0), CLK_PWM0_DIV_MASK,
-			     FIELD_PREP(CLK_PWM0_DIV_MASK, div - 1));
+		writel(FIELD_PREP_WM16(CLK_PWM0_DIV_MASK, div - 1),
+		       RK3506_PMU_CLKSEL_CON(0));
 		break;
 	case CLK_PWM1:
 		if (priv->v0pll_hz % rate == 0) {
@@ -659,10 +651,9 @@ static ulong rk3506_pwm_set_rate(struct rk3506_clk_priv *priv, ulong clk_id,
 			div = DIV_ROUND_UP(priv->gpll_div_hz, rate);
 		}
 		assert(div - 1 <= 15);
-		rk_clrsetreg(RK3506_CLKSEL_CON(33),
-			     CLK_PWM1_SEL_MASK | CLK_PWM1_DIV_MASK,
-			     FIELD_PREP(CLK_PWM1_SEL_MASK, sel) |
-			     FIELD_PREP(CLK_PWM1_DIV_MASK, div - 1));
+		writel(FIELD_PREP_WM16(CLK_PWM1_SEL_MASK, sel) |
+		       FIELD_PREP_WM16(CLK_PWM1_DIV_MASK, div - 1),
+		       RK3506_CLKSEL_CON(33));
 		break;
 	default:
 		return -ENOENT;
@@ -727,16 +718,14 @@ static ulong rk3506_spi_set_rate(struct rk3506_clk_priv *priv, ulong clk_id,
 
 	switch (clk_id) {
 	case CLK_SPI0:
-		rk_clrsetreg(RK3506_CLKSEL_CON(34),
-			     CLK_SPI0_SEL_MASK | CLK_SPI0_DIV_MASK,
-			     FIELD_PREP(CLK_SPI0_SEL_MASK, sel) |
-			     FIELD_PREP(CLK_SPI0_DIV_MASK, div - 1));
+		writel(FIELD_PREP_WM16(CLK_SPI0_SEL_MASK, sel) |
+		       FIELD_PREP_WM16(CLK_SPI0_DIV_MASK, div - 1),
+		       RK3506_CLKSEL_CON(34));
 		break;
 	case CLK_SPI1:
-		rk_clrsetreg(RK3506_CLKSEL_CON(34),
-			     CLK_SPI1_SEL_MASK | CLK_SPI1_DIV_MASK,
-			     FIELD_PREP(CLK_SPI1_SEL_MASK, sel) |
-			     FIELD_PREP(CLK_SPI1_DIV_MASK, div - 1));
+		writel(FIELD_PREP_WM16(CLK_SPI1_SEL_MASK, sel) |
+		       FIELD_PREP_WM16(CLK_SPI1_DIV_MASK, div - 1),
+		       RK3506_CLKSEL_CON(34));
 		break;
 	default:
 		return -ENOENT;
@@ -770,7 +759,7 @@ static ulong rk3506_fspi_get_rate(struct rk3506_clk_priv *priv)
 
 static ulong rk3506_fspi_set_rate(struct rk3506_clk_priv *priv, ulong rate)
 {
-	int div, sel;
+	u32 div, sel;
 
 	if (OSC_HZ % rate == 0) {
 		sel = SCLK_FSPI_SEL_24M;
@@ -787,10 +776,9 @@ static ulong rk3506_fspi_set_rate(struct rk3506_clk_priv *priv, ulong rate)
 	}
 	assert(div - 1 <= 31);
 
-	rk_clrsetreg(RK3506_CLKSEL_CON(50),
-		     SCLK_FSPI_SEL_MASK | SCLK_FSPI_DIV_MASK,
-		     FIELD_PREP(SCLK_FSPI_SEL_MASK, sel) |
-		     FIELD_PREP(SCLK_FSPI_DIV_MASK, div - 1));
+	writel(FIELD_PREP_WM16(SCLK_FSPI_SEL_MASK, sel) |
+	       FIELD_PREP_WM16(SCLK_FSPI_DIV_MASK, div - 1),
+	       RK3506_CLKSEL_CON(50));
 
 	return rk3506_fspi_get_rate(priv);
 }
@@ -820,7 +808,7 @@ static ulong rk3506_vop_dclk_get_rate(struct rk3506_clk_priv *priv)
 
 static ulong rk3506_vop_dclk_set_rate(struct rk3506_clk_priv *priv, ulong rate)
 {
-	int div, sel;
+	u32 div, sel;
 
 	if (OSC_HZ % rate == 0) {
 		sel = DCLK_VOP_SEL_24M;
@@ -837,10 +825,9 @@ static ulong rk3506_vop_dclk_set_rate(struct rk3506_clk_priv *priv, ulong rate)
 	}
 	assert(div - 1 <= 255);
 
-	rk_clrsetreg(RK3506_CLKSEL_CON(60),
-		     DCLK_VOP_SEL_MASK | DCLK_VOP_DIV_MASK,
-		     FIELD_PREP(DCLK_VOP_SEL_MASK, sel) |
-		     FIELD_PREP(DCLK_VOP_DIV_MASK, div - 1));
+	writel(FIELD_PREP_WM16(DCLK_VOP_SEL_MASK, sel) |
+	       FIELD_PREP_WM16(DCLK_VOP_DIV_MASK, div - 1),
+	       RK3506_CLKSEL_CON(60));
 
 	return rk3506_vop_dclk_get_rate(priv);
 }
@@ -875,13 +862,13 @@ static ulong rk3506_mac_set_rate(struct rk3506_clk_priv *priv, ulong clk_id,
 	case CLK_MAC0:
 	case CLK_MAC1:
 		div = DIV_ROUND_UP(priv->gpll_hz, rate);
-		rk_clrsetreg(RK3506_CLKSEL_CON(50), CLK_MAC_DIV_MASK,
-			     FIELD_PREP(CLK_MAC_DIV_MASK, div - 1));
+		writel(FIELD_PREP_WM16(CLK_MAC_DIV_MASK, div - 1),
+		       RK3506_CLKSEL_CON(50));
 		break;
 	case CLK_MAC_OUT:
 		div = DIV_ROUND_UP(priv->gpll_hz, rate);
-		rk_clrsetreg(RK3506_PMU_CLKSEL_CON(0), CLK_MAC_OUT_DIV_MASK,
-			     FIELD_PREP(CLK_MAC_OUT_DIV_MASK, div - 1));
+		writel(FIELD_PREP_WM16(CLK_MAC_OUT_DIV_MASK, div - 1),
+		       RK3506_PMU_CLKSEL_CON(0));
 		break;
 	default:
 		return -ENOENT;
@@ -1091,16 +1078,14 @@ static void rk3506_clk_init(struct rk3506_clk_priv *priv)
 static void rk3506_clk_init_xpl(void)
 {
 	/* Init pka crypto rate, sel=v0pll, div=3 */
-	rk_clrsetreg(RK3506_SCRU_BASE + 0x0010,
-		     CLK_PKA_CRYPTO_SEL_MASK | CLK_PKA_CRYPTO_DIV_MASK,
-		     FIELD_PREP(CLK_PKA_CRYPTO_SEL_MASK, CLK_PKA_CRYPTO_SEL_V0PLL) |
-		     FIELD_PREP(CLK_PKA_CRYPTO_DIV_MASK, 3));
+	writel(FIELD_PREP_WM16(CLK_PKA_CRYPTO_SEL_MASK, CLK_PKA_CRYPTO_SEL_V0PLL) |
+	       FIELD_PREP_WM16(CLK_PKA_CRYPTO_DIV_MASK, 3),
+	       RK3506_SCRU_BASE + 0x0010);
 
 	/* Change clk core src rate, sel=gpll, div=3 */
-	rk_clrsetreg(RK3506_CLKSEL_CON(15),
-		     CLK_CORE_SRC_SEL_MASK | CLK_CORE_SRC_DIV_MASK,
-		     FIELD_PREP(CLK_CORE_SRC_SEL_MASK, CLK_CORE_SEL_GPLL) |
-		     FIELD_PREP(CLK_CORE_SRC_DIV_MASK, 3));
+	writel(FIELD_PREP_WM16(CLK_CORE_SRC_SEL_MASK, CLK_CORE_SEL_GPLL) |
+	       FIELD_PREP_WM16(CLK_CORE_SRC_DIV_MASK, 3),
+	       RK3506_CLKSEL_CON(15));
 }
 
 static int rk3506_clk_probe(struct udevice *dev)
Signature · 1 line
2.54.0
Reviewed-by on v1 5/6
#28 lore raw

Re: [PATCH 5/6] clk: rockchip: rk3506: Use FIELD_PREP_WM16 macro

Hi Jonas,

Quoted 16 lines · Jonas Karlman

clk: rockchip: rk3506: Use FIELD_PREP_WM16 macro

Change to use writel() together with the FIELD_PREP_WM16() macro instead of using the rk_clrsetreg() macro to avoid having to define the mask as a parameter to both rk_clrsetreg() and FIELD_PREP(). Also change to use u32 variables consistently.

No change in behavior is expected due to this code style change.

drivers/clk/rockchip/clk_rk3506.c | 195 ++++++++++++++++++--------------------

 1 file changed, 90 insertions(+), 105 deletions(-)

drivers/clk/rockchip/clk_rk3506.c static ulong rk3506_tsadc_get_rate(struct rk3506_clk_priv *priv, ulong clk_id) · line 483

Quoted 5 lines
drivers/clk/rockchip/clk_rk3506.c +1 -0
@@ -485,6 +479,7 @@ static ulong rk3506_tsadc_get_rate(struct rk3506_clk_priv *priv, ulong clk_id)
      u32 con, div;
 
      con = readl(RK3506_CLKSEL_CON(61));
+
      switch (clk_id) {

Unrelated to the FIELD_PREP_WM16 conversion - might be best to drop it. In any case:

Regards,
Simon

Reviewed-by on v1 5/6
#29 lore raw

Re: [PATCH 5/6] clk: rockchip: rk3506: Use FIELD_PREP_WM16 macro

In reply to Jonas Karlman

Hi Jonas,

Quoted 314 lines · Jonas Karlman

Change to use writel() together with the FIELD_PREP_WM16() macro instead of using the rk_clrsetreg() macro to avoid having to define the mask as a parameter to both rk_clrsetreg() and FIELD_PREP(). Also change to use u32 variables consistently.

No change in behavior is expected due to this code style change.

---
  drivers/clk/rockchip/clk_rk3506.c | 195 ++++++++++++++----------------
  1 file changed, 90 insertions(+), 105 deletions(-)
drivers/clk/rockchip/clk_rk3506.c +73 -84
index e156bf19a6b4..7ccab313e6ec 100644
@@ -9,10 +9,11 @@
  #include <clk-uclass.h>
  #include <asm/arch-rockchip/clock.h>
  #include <asm/arch-rockchip/cru_rk3506.h>
-#include <asm/arch-rockchip/hardware.h>
+#include <asm/io.h>
  #include <dm/device-internal.h>
  #include <dm/lists.h>
  #include <dt-bindings/clock/rockchip,rk3506-cru.h>
+#include <linux/hw_bitfield.h>
 
  #define DIV_TO_RATE(input_rate, div)    ((input_rate) / ((div) + 1))
 
@@ -122,10 +123,10 @@ static int rk3506_armclk_set_rate(struct rk3506_clk_priv *priv, ulong new_rate)
  	 */
  	old_rate = rk3506_armclk_get_rate(priv);
  	if (new_rate >= old_rate) {
-		rk_clrsetreg(RK3506_CLKSEL_CON(15), ACLK_CORE_DIV_MASK,
-			     FIELD_PREP(ACLK_CORE_DIV_MASK, rate->aclk_div));
-		rk_clrsetreg(RK3506_CLKSEL_CON(16), PCLK_CORE_DIV_MASK,
-			     FIELD_PREP(PCLK_CORE_DIV_MASK, rate->pclk_div));
+		writel(FIELD_PREP_WM16(ACLK_CORE_DIV_MASK, rate->aclk_div),
+		       RK3506_CLKSEL_CON(15));
+		writel(FIELD_PREP_WM16(PCLK_CORE_DIV_MASK, rate->pclk_div),
+		       RK3506_CLKSEL_CON(16));
  	}
 
  	if (new_rate == 589824000 || new_rate == 1179648000) {
@@ -146,22 +147,22 @@ static int rk3506_armclk_set_rate(struct rk3506_clk_priv *priv, ulong new_rate)
  	con = readl(RK3506_CLKSEL_CON(15));
  	old_div = FIELD_GET(CLK_CORE_SRC_DIV_MASK, con);
  	if (DIV_TO_RATE(prate, old_div) > new_rate) {
-		rk_clrsetreg(RK3506_CLKSEL_CON(15), CLK_CORE_SRC_DIV_MASK,
-			     FIELD_PREP(CLK_CORE_SRC_DIV_MASK, div - 1));
-		rk_clrsetreg(RK3506_CLKSEL_CON(15), CLK_CORE_SRC_SEL_MASK,
-			     FIELD_PREP(CLK_CORE_SRC_SEL_MASK, sel));
+		writel(FIELD_PREP_WM16(CLK_CORE_SRC_DIV_MASK, div - 1),
+		       RK3506_CLKSEL_CON(15));
+		writel(FIELD_PREP_WM16(CLK_CORE_SRC_SEL_MASK, sel),
+		       RK3506_CLKSEL_CON(15));
  	} else {
-		rk_clrsetreg(RK3506_CLKSEL_CON(15), CLK_CORE_SRC_SEL_MASK,
-			     FIELD_PREP(CLK_CORE_SRC_SEL_MASK, sel));
-		rk_clrsetreg(RK3506_CLKSEL_CON(15), CLK_CORE_SRC_DIV_MASK,
-			     FIELD_PREP(CLK_CORE_SRC_DIV_MASK, div - 1));
+		writel(FIELD_PREP_WM16(CLK_CORE_SRC_SEL_MASK, sel),
+		       RK3506_CLKSEL_CON(15));
+		writel(FIELD_PREP_WM16(CLK_CORE_SRC_DIV_MASK, div - 1),
+		       RK3506_CLKSEL_CON(15));
  	}
 
  	if (new_rate < old_rate) {
-		rk_clrsetreg(RK3506_CLKSEL_CON(15), ACLK_CORE_DIV_MASK,
-			     FIELD_PREP(ACLK_CORE_DIV_MASK, rate->aclk_div));
-		rk_clrsetreg(RK3506_CLKSEL_CON(16), PCLK_CORE_DIV_MASK,
-			     FIELD_PREP(PCLK_CORE_DIV_MASK, rate->pclk_div));
+		writel(FIELD_PREP_WM16(ACLK_CORE_DIV_MASK, rate->aclk_div),
+		       RK3506_CLKSEL_CON(15));
+		writel(FIELD_PREP_WM16(PCLK_CORE_DIV_MASK, rate->pclk_div),
+		       RK3506_CLKSEL_CON(16));
  	}
 
  	return rk3506_armclk_get_rate(priv);
@@ -209,26 +210,26 @@ static ulong rk3506_pll_div_set_rate(struct rk3506_clk_priv *priv, ulong clk_id,
  	case CLK_GPLL_DIV:
  		div = DIV_ROUND_UP(priv->gpll_hz, rate);
  		assert(div - 1 <= 15);
-		rk_clrsetreg(RK3506_CLKSEL_CON(0), CLK_GPLL_DIV_MASK,
-			     FIELD_PREP(CLK_GPLL_DIV_MASK, div - 1));
+		writel(FIELD_PREP_WM16(CLK_GPLL_DIV_MASK, div - 1),
+		       RK3506_CLKSEL_CON(0));
  		break;
  	case CLK_GPLL_DIV_100M:
  		div = DIV_ROUND_UP(priv->gpll_div_hz, rate);
  		assert(div - 1 <= 15);
-		rk_clrsetreg(RK3506_CLKSEL_CON(0), CLK_GPLL_DIV_100M_MASK,
-			     FIELD_PREP(CLK_GPLL_DIV_100M_MASK, div - 1));
+		writel(FIELD_PREP_WM16(CLK_GPLL_DIV_100M_MASK, div - 1),
+		       RK3506_CLKSEL_CON(0));
  		break;
  	case CLK_V0PLL_DIV:
  		div = DIV_ROUND_UP(priv->v0pll_hz, rate);
  		assert(div - 1 <= 15);
-		rk_clrsetreg(RK3506_CLKSEL_CON(1), CLK_V0PLL_DIV_MASK,
-			     FIELD_PREP(CLK_V0PLL_DIV_MASK, div - 1));
+		writel(FIELD_PREP_WM16(CLK_V0PLL_DIV_MASK, div - 1),
+		       RK3506_CLKSEL_CON(1));
  		break;
  	case CLK_V1PLL_DIV:
  		div = DIV_ROUND_UP(priv->v1pll_hz, rate);
  		assert(div - 1 <= 15);
-		rk_clrsetreg(RK3506_CLKSEL_CON(1), CLK_V1PLL_DIV_MASK,
-			     FIELD_PREP(CLK_V1PLL_DIV_MASK, div - 1));
+		writel(FIELD_PREP_WM16(CLK_V1PLL_DIV_MASK, div - 1),
+		       RK3506_CLKSEL_CON(1));
  		break;
  	default:
  		return -ENOENT;
@@ -293,22 +294,19 @@ static ulong rk3506_bus_set_rate(struct rk3506_clk_priv *priv, ulong clk_id,
 
  	switch (clk_id) {
  	case ACLK_BUS_ROOT:
-		rk_clrsetreg(RK3506_CLKSEL_CON(21),
-			     ACLK_BUS_SEL_MASK | ACLK_BUS_DIV_MASK,
-			     FIELD_PREP(ACLK_BUS_SEL_MASK, sel) |
-			     FIELD_PREP(ACLK_BUS_DIV_MASK, div - 1));
+		writel(FIELD_PREP_WM16(ACLK_BUS_SEL_MASK, sel) |
+		       FIELD_PREP_WM16(ACLK_BUS_DIV_MASK, div - 1),
+		       RK3506_CLKSEL_CON(21));
  		break;
  	case HCLK_BUS_ROOT:
-		rk_clrsetreg(RK3506_CLKSEL_CON(21),
-			     HCLK_BUS_SEL_MASK | HCLK_BUS_DIV_MASK,
-			     FIELD_PREP(HCLK_BUS_SEL_MASK, sel) |
-			     FIELD_PREP(HCLK_BUS_DIV_MASK, div - 1));
+		writel(FIELD_PREP_WM16(HCLK_BUS_SEL_MASK, sel) |
+		       FIELD_PREP_WM16(HCLK_BUS_DIV_MASK, div - 1),
+		       RK3506_CLKSEL_CON(21));
  		break;
  	case PCLK_BUS_ROOT:
-		rk_clrsetreg(RK3506_CLKSEL_CON(22),
-			     PCLK_BUS_SEL_MASK | PCLK_BUS_DIV_MASK,
-			     FIELD_PREP(PCLK_BUS_SEL_MASK, sel) |
-			     FIELD_PREP(PCLK_BUS_DIV_MASK, div - 1));
+		writel(FIELD_PREP_WM16(PCLK_BUS_SEL_MASK, sel) |
+		       FIELD_PREP_WM16(PCLK_BUS_DIV_MASK, div - 1),
+		       RK3506_CLKSEL_CON(22));
  		break;
  	default:
  		return -ENOENT;
@@ -368,16 +366,14 @@ static ulong rk3506_peri_set_rate(struct rk3506_clk_priv *priv, ulong clk_id,
 
  	switch (clk_id) {
  	case ACLK_HSPERI_ROOT:
-		rk_clrsetreg(RK3506_CLKSEL_CON(49),
-			     ACLK_HSPERI_SEL_MASK | ACLK_HSPERI_DIV_MASK,
-			     FIELD_PREP(ACLK_HSPERI_SEL_MASK, sel) |
-			     FIELD_PREP(ACLK_HSPERI_DIV_MASK, div - 1));
+		writel(FIELD_PREP_WM16(ACLK_HSPERI_SEL_MASK, sel) |
+		       FIELD_PREP_WM16(ACLK_HSPERI_DIV_MASK, div - 1),
+		       RK3506_CLKSEL_CON(49));
  		break;
  	case HCLK_LSPERI_ROOT:
-		rk_clrsetreg(RK3506_CLKSEL_CON(29),
-			     HCLK_LSPERI_SEL_MASK | HCLK_LSPERI_DIV_MASK,
-			     FIELD_PREP(HCLK_LSPERI_SEL_MASK, sel) |
-			     FIELD_PREP(HCLK_LSPERI_DIV_MASK, div - 1));
+		writel(FIELD_PREP_WM16(HCLK_LSPERI_SEL_MASK, sel) |
+		       FIELD_PREP_WM16(HCLK_LSPERI_DIV_MASK, div - 1),
+		       RK3506_CLKSEL_CON(29));
  		break;
  	default:
  		return -ENOENT;
@@ -429,10 +425,9 @@ static ulong rk3506_sdmmc_set_rate(struct rk3506_clk_priv *priv, ulong clk_id,
  	}
  	assert(div - 1 <= 63);
 
-	rk_clrsetreg(RK3506_CLKSEL_CON(49),
-		     CCLK_SDMMC_SEL_MASK | CCLK_SDMMC_DIV_MASK,
-		     FIELD_PREP(CCLK_SDMMC_SEL_MASK, sel) |
-		     FIELD_PREP(CCLK_SDMMC_DIV_MASK, div - 1));
+	writel(FIELD_PREP_WM16(CCLK_SDMMC_SEL_MASK, sel) |
+	       FIELD_PREP_WM16(CCLK_SDMMC_DIV_MASK, div - 1),
+	       RK3506_CLKSEL_CON(49));
 
  	return rk3506_sdmmc_get_rate(priv, clk_id);
  }
@@ -472,10 +467,9 @@ static ulong rk3506_saradc_set_rate(struct rk3506_clk_priv *priv, ulong clk_id,
  	}
  	assert(div - 1 <= 15);
 
-	rk_clrsetreg(RK3506_CLKSEL_CON(54),
-		     CLK_SARADC_SEL_MASK | CLK_SARADC_DIV_MASK,
-		     FIELD_PREP(CLK_SARADC_SEL_MASK, sel) |
-		     FIELD_PREP(CLK_SARADC_DIV_MASK, div - 1));
+	writel(FIELD_PREP_WM16(CLK_SARADC_SEL_MASK, sel) |
+	       FIELD_PREP_WM16(CLK_SARADC_DIV_MASK, div - 1),
+	       RK3506_CLKSEL_CON(54));
 
  	return rk3506_saradc_get_rate(priv, clk_id);
  }
@@ -485,6 +479,7 @@ static ulong rk3506_tsadc_get_rate(struct rk3506_clk_priv *priv, ulong clk_id)
  	u32 con, div;
 
  	con = readl(RK3506_CLKSEL_CON(61));
+
  	switch (clk_id) {
  	case CLK_TSADC_TSEN:
  		div = FIELD_GET(CLK_TSADC_TSEN_DIV_MASK, con);
@@ -508,14 +503,14 @@ static ulong rk3506_tsadc_set_rate(struct rk3506_clk_priv *priv, ulong clk_id,
  	case CLK_TSADC_TSEN:
  		div = DIV_ROUND_UP(OSC_HZ, rate);
  		assert(div - 1 <= 7);
-		rk_clrsetreg(RK3506_CLKSEL_CON(61), CLK_TSADC_TSEN_DIV_MASK,
-			     FIELD_PREP(CLK_TSADC_TSEN_DIV_MASK, div - 1));
+		writel(FIELD_PREP_WM16(CLK_TSADC_TSEN_DIV_MASK, div - 1),
+		       RK3506_CLKSEL_CON(61));
  		break;
  	case CLK_TSADC:
  		div = DIV_ROUND_UP(OSC_HZ, rate);
  		assert(div - 1 <= 255);
-		rk_clrsetreg(RK3506_CLKSEL_CON(61), CLK_TSADC_DIV_MASK,
-			     FIELD_PREP(CLK_TSADC_DIV_MASK, div - 1));
+		writel(FIELD_PREP_WM16(CLK_TSADC_DIV_MASK, div - 1),
+		       RK3506_CLKSEL_CON(61));
  		break;
  	default:
  		return -ENOENT;
@@ -580,22 +575,19 @@ static ulong rk3506_i2c_set_rate(struct rk3506_clk_priv *priv, ulong clk_id,
 
  	switch (clk_id) {
  	case CLK_I2C0:
-		rk_clrsetreg(RK3506_CLKSEL_CON(32),
-			     CLK_I2C0_SEL_MASK | CLK_I2C0_DIV_MASK,
-			     FIELD_PREP(CLK_I2C0_SEL_MASK, sel) |
-			     FIELD_PREP(CLK_I2C0_DIV_MASK, div - 1));
+		writel(FIELD_PREP_WM16(CLK_I2C0_SEL_MASK, sel) |
+		       FIELD_PREP_WM16(CLK_I2C0_DIV_MASK, div - 1),
+		       RK3506_CLKSEL_CON(32));
  		break;
  	case CLK_I2C1:
-		rk_clrsetreg(RK3506_CLKSEL_CON(32),
-			     CLK_I2C1_SEL_MASK | CLK_I2C1_DIV_MASK,
-			     FIELD_PREP(CLK_I2C1_SEL_MASK, sel) |
-			     FIELD_PREP(CLK_I2C1_DIV_MASK, div - 1));
+		writel(FIELD_PREP_WM16(CLK_I2C1_SEL_MASK, sel) |
+		       FIELD_PREP_WM16(CLK_I2C1_DIV_MASK, div - 1),
+		       RK3506_CLKSEL_CON(32));
  		break;
  	case CLK_I2C2:
-		rk_clrsetreg(RK3506_CLKSEL_CON(33),
-			     CLK_I2C2_SEL_MASK | CLK_I2C2_DIV_MASK,
-			     FIELD_PREP(CLK_I2C2_SEL_MASK, sel) |
-			     FIELD_PREP(CLK_I2C2_DIV_MASK, div - 1));
+		writel(FIELD_PREP_WM16(CLK_I2C2_SEL_MASK, sel) |
+		       FIELD_PREP_WM16(CLK_I2C2_DIV_MASK, div - 1),
+		       RK3506_CLKSEL_CON(33));
  		break;
  	default:
  		return -ENOENT;
@@ -644,8 +636,8 @@ static ulong rk3506_pwm_set_rate(struct rk3506_clk_priv *priv, ulong clk_id,
  	case CLK_PWM0:
  		div = DIV_ROUND_UP(priv->gpll_div_100mhz, rate);
  		assert(div - 1 <= 15);
-		rk_clrsetreg(RK3506_PMU_CLKSEL_CON(0), CLK_PWM0_DIV_MASK,
-			     FIELD_PREP(CLK_PWM0_DIV_MASK, div - 1));
+		writel(FIELD_PREP_WM16(CLK_PWM0_DIV_MASK, div - 1),
+		       RK3506_PMU_CLKSEL_CON(0));
  		break;
  	case CLK_PWM1:
  		if (priv->v0pll_hz % rate == 0) {
@@ -659,10 +651,9 @@ static ulong rk3506_pwm_set_rate(struct rk3506_clk_priv *priv, ulong clk_id,
  			div = DIV_ROUND_UP(priv->gpll_div_hz, rate);
  		}
  		assert(div - 1 <= 15);
-		rk_clrsetreg(RK3506_CLKSEL_CON(33),
-			     CLK_PWM1_SEL_MASK | CLK_PWM1_DIV_MASK,
-			     FIELD_PREP(CLK_PWM1_SEL_MASK, sel) |
-			     FIELD_PREP(CLK_PWM1_DIV_MASK, div - 1));
+		writel(FIELD_PREP_WM16(CLK_PWM1_SEL_MASK, sel) |
+		       FIELD_PREP_WM16(CLK_PWM1_DIV_MASK, div - 1),
+		       RK3506_CLKSEL_CON(33));
  		break;
  	default:
  		return -ENOENT;
@@ -727,16 +718,14 @@ static ulong rk3506_spi_set_rate(struct rk3506_clk_priv *priv, ulong clk_id,
 
  	switch (clk_id) {
  	case CLK_SPI0:
-		rk_clrsetreg(RK3506_CLKSEL_CON(34),
-			     CLK_SPI0_SEL_MASK | CLK_SPI0_DIV_MASK,
-			     FIELD_PREP(CLK_SPI0_SEL_MASK, sel) |
-			     FIELD_PREP(CLK_SPI0_DIV_MASK, div - 1));
+		writel(FIELD_PREP_WM16(CLK_SPI0_SEL_MASK, sel) |
+		       FIELD_PREP_WM16(CLK_SPI0_DIV_MASK, div - 1),
+		       RK3506_CLKSEL_CON(34));
  		break;
  	case CLK_SPI1:
-		rk_clrsetreg(RK3506_CLKSEL_CON(34),
-			     CLK_SPI1_SEL_MASK | CLK_SPI1_DIV_MASK,
-			     FIELD_PREP(CLK_SPI1_SEL_MASK, sel) |
-			     FIELD_PREP(CLK_SPI1_DIV_MASK, div - 1));
+		writel(FIELD_PREP_WM16(CLK_SPI1_SEL_MASK, sel) |
+		       FIELD_PREP_WM16(CLK_SPI1_DIV_MASK, div - 1),
+		       RK3506_CLKSEL_CON(34));
  		break;
  	default:
  		return -ENOENT;
@@ -770,7 +759,7 @@ static ulong rk3506_fspi_get_rate(struct rk3506_clk_priv *priv)
 
  static ulong rk3506_fspi_set_rate(struct rk3506_clk_priv *priv, ulong rate)
  {
-	int div, sel;
+	u32 div, sel;
 
  	if (OSC_HZ % rate == 0) {
  		sel = SCLK_FSPI_SEL_24M;

drivers/clk/rockchip/clk_rk3506.c static ulong rk3506_fspi_set_rate(struct rk3506_clk_priv *priv, ulong rate) · line 778

Quoted 3 lines
drivers/clk/rockchip/clk_rk3506.c +0 -0
@@ -787,10 +776,9 @@ static ulong rk3506_fspi_set_rate(struct rk3506_clk_priv *priv, ulong rate)
  	}
  	assert(div - 1 <= 31);
 

If somehow div = 0, then this will result in an underflow. Because it's an unsigned type, it'll wrap around and start at U32_MAX so assert() should catch it. However, assert() is a noop when DEBUG constant isn't defined, so this actually does nothing.

We should switch those assert to something that actually does something otherwise we could still very well attempt to write a divider that's too big and have weird behaviors instead of outright fail when we know we cannot do something that's requested.

This here doesn't change the logic anyway so this can be done in a follow-up patch, thus:

Thanks!
Quentin

#30 lore raw

Re: [PATCH 5/6] clk: rockchip: rk3506: Use FIELD_PREP_WM16 macro

Hi Quentin,

Quoted 19 lines · Quentin Schulz

Hi Jonas,

Quoted 16 lines · Jonas Karlman

Change to use writel() together with the FIELD_PREP_WM16() macro instead of using the rk_clrsetreg() macro to avoid having to define the mask as a parameter to both rk_clrsetreg() and FIELD_PREP(). Also change to use u32 variables consistently.

No change in behavior is expected due to this code style change.

---
  drivers/clk/rockchip/clk_rk3506.c | 195 ++++++++++++++----------------
  1 file changed, 90 insertions(+), 105 deletions(-)
drivers/clk/rockchip/clk_rk3506.c +0 -0
index e156bf19a6b4..7ccab313e6ec 100644

[snip]

Quoted 19 lines

drivers/clk/rockchip/clk_rk3506.c static ulong rk3506_fspi_set_rate(struct rk3506_clk_priv *priv, ulong rate) · line 778

Quoted 13 lines
drivers/clk/rockchip/clk_rk3506.c +1 -1
@@ -770,7 +759,7 @@ static ulong rk3506_fspi_get_rate(struct rk3506_clk_priv *priv)
 
  static ulong rk3506_fspi_set_rate(struct rk3506_clk_priv *priv, ulong rate)
  {
-	int div, sel;
+	u32 div, sel;
 
  	if (OSC_HZ % rate == 0) {
  		sel = SCLK_FSPI_SEL_24M;
@@ -787,10 +776,9 @@ static ulong rk3506_fspi_set_rate(struct rk3506_clk_priv *priv, ulong rate)
  	}
  	assert(div - 1 <= 31);
 

If somehow div = 0, then this will result in an underflow. Because it's an unsigned type, it'll wrap around and start at U32_MAX so assert() should catch it. However, assert() is a noop when DEBUG constant isn't defined, so this actually does nothing.

Quoted 4 lines

We should switch those assert to something that actually does something otherwise we could still very well attempt to write a divider that's too big and have weird behaviors instead of outright fail when we know we cannot do something that's requested.

I was looking into trying to use assert(FIELD_FIT(...)) to validate, but as you mention this does not help for non-DEBUG builds.

However, I have been playing around with something like following, still not sure I fully like it, please advise.

static inline u32 get_divider(ulong prate, ulong rate, u32 mask, bool divisible)

{
	u32 div;

	if (divisible && prate % rate)
		return 0;

	div = DIV_ROUND_UP(prate, rate);
	if (!div || ((div - 1) << __ffs(mask)) & ~mask)
		return 0;

	return div;
}

static ulong rk3506_saradc_set_rate(struct rk3506_clk_priv *priv, ulong clk_id,
ulong rate)

{
	u32 div, sel;

	if ((div = get_divider(32000, rate, CLK_SARADC_DIV_MASK, 1)))
		sel = CLK_SARADC_SEL_32K;
	else if ((div = get_divider(400000, rate, CLK_SARADC_DIV_MASK, 1)))
		sel = CLK_SARADC_SEL_400K;
	else if ((div = get_divider(OSC_HZ, rate, CLK_SARADC_DIV_MASK, 0)))
		sel = CLK_SARADC_SEL_24M;
	else
		return -EINVAL;

	writel(FIELD_PREP_WM16(CLK_SARADC_SEL_MASK, sel) |
	       FIELD_PREP_WM16(CLK_SARADC_DIV_MASK, div - 1),
	       RK3506_CLKSEL_CON(54));

	return rk3506_saradc_get_rate(priv, clk_id);
}

Here we use a helper function, could possible also be re-made into a macro so we actually can use FIELD_FIT() instead of bitwise ops.

Basic idea is that we assign div and test if the rate is divisible and fit inside mask at the same time, and if not we move on to next possible parent/divider.

With above and using the 1 MHz rate I would get something like following, provided I also added some log_debug() calls.

=> adc list
invalid divider 24 for prate 24000000, rate 1000000, mask f unsupported clk: id=193 rate=1000000 ret=-22
- adc@ff4e8000 status: -22

In the future I would also like to drop some priv->pll_hz uses and instead get that from a function, so would look something like:

	if ((div = get_divisor(rk3506_pll_get_rate(priv, PLL_V1PLL),
			       rate, CCLK_SDMMC_DIV_MASK, 1)))
		sel = CCLK_SDMMC_SEL_V1PLL;

Please let me know what you think of using something similar to above.

Regards,
Jonas

Quoted 8 lines

This here doesn't change the logic anyway so this can be done in a follow-up patch, thus:

Thanks!
Quentin

#31 lore raw

Re: [PATCH 5/6] clk: rockchip: rk3506: Use FIELD_PREP_WM16 macro

Hi Jonas,

Quoted 55 lines · Jonas Karlman

Hi Quentin,

Quoted 19 lines · Quentin Schulz

Hi Jonas,

Quoted 16 lines · Jonas Karlman

Change to use writel() together with the FIELD_PREP_WM16() macro instead of using the rk_clrsetreg() macro to avoid having to define the mask as a parameter to both rk_clrsetreg() and FIELD_PREP(). Also change to use u32 variables consistently.

No change in behavior is expected due to this code style change.

---
   drivers/clk/rockchip/clk_rk3506.c | 195 ++++++++++++++----------------
   1 file changed, 90 insertions(+), 105 deletions(-)
drivers/clk/rockchip/clk_rk3506.c +0 -0
index e156bf19a6b4..7ccab313e6ec 100644

[snip]

Quoted 19 lines

drivers/clk/rockchip/clk_rk3506.c static ulong rk3506_fspi_set_rate(struct rk3506_clk_priv *priv, ulong rate) · line 778

Quoted 13 lines
drivers/clk/rockchip/clk_rk3506.c +1 -1
@@ -770,7 +759,7 @@ static ulong rk3506_fspi_get_rate(struct rk3506_clk_priv *priv)
 
   static ulong rk3506_fspi_set_rate(struct rk3506_clk_priv *priv, ulong rate)
   {
-	int div, sel;
+	u32 div, sel;
 
   	if (OSC_HZ % rate == 0) {
   		sel = SCLK_FSPI_SEL_24M;
@@ -787,10 +776,9 @@ static ulong rk3506_fspi_set_rate(struct rk3506_clk_priv *priv, ulong rate)
   	}
   	assert(div - 1 <= 31);
 

If somehow div = 0, then this will result in an underflow. Because it's an unsigned type, it'll wrap around and start at U32_MAX so assert() should catch it. However, assert() is a noop when DEBUG constant isn't defined, so this actually does nothing.

Quoted 4 lines

We should switch those assert to something that actually does something otherwise we could still very well attempt to write a divider that's too big and have weird behaviors instead of outright fail when we know we cannot do something that's requested.

I was looking into trying to use assert(FIELD_FIT(...)) to validate, but as you mention this does not help for non-DEBUG builds.

However, I have been playing around with something like following, still not sure I fully like it, please advise.

Quoted 7 lines

static inline u32 get_divider(ulong prate, ulong rate, u32 mask, bool divisible)

{
	u32 div;

	if (divisible && prate % rate)
		return 0;

What is "divisible" supposed to represent? I don't understand from reading the code.

Quoted 2 lines
	div = DIV_ROUND_UP(prate, rate);
	if (!div || ((div - 1) << __ffs(mask)) & ~mask)

I scratch my head looking at this so not sure it would stand the test of time. Would:

((div - 1) & mask) == (div - 1)

work better instead? If your value masked (I'm assuming you want to use the bitfield mask here, so all bits are set in the bitfield) doesn't equal your value, then the mask cannot contain the value.

Quoted 7 lines
		return 0;

	return div;
}

static ulong rk3506_saradc_set_rate(struct rk3506_clk_priv *priv, ulong clk_id,
ulong rate)

Quoted 9 lines
{
	u32 div, sel;

	if ((div = get_divider(32000, rate, CLK_SARADC_DIV_MASK, 1)))
		sel = CLK_SARADC_SEL_32K;
	else if ((div = get_divider(400000, rate, CLK_SARADC_DIV_MASK, 1)))
		sel = CLK_SARADC_SEL_400K;
	else if ((div = get_divider(OSC_HZ, rate, CLK_SARADC_DIV_MASK, 0)))
		sel = CLK_SARADC_SEL_24M;

Can you really not divide from x24m as a parent?

Also, please do not assign a variable within an if condition, I find it very difficult to remember that checking an if condition can have side effects. I can suggest:

div = get_divider(32000, rate, CLK_SARADC_DIV_MASK, 1);

if (div) {
    sel = CLK_SARADC_SEL_32K;
    goto div_found;
}

div = get_divider(400000, rate, CLK_SARADC_DIV_MASK, 1);

if (div) {
    sel = CLK_SARADC_SEL_400K;
    goto div_found;
}

[...]

return -EINVAL;

div_found:
writel(FIELD_PREP_WM16(CLK_SARADC_SEL_MASK, sel) | [...]
return rk3506_saradc_get_rate(priv, clk_id);

Or use another function for

the writel+return currently at the end of your function and call that instead of a goto.

Quoted 20 lines
	else
		return -EINVAL;

	writel(FIELD_PREP_WM16(CLK_SARADC_SEL_MASK, sel) |
	       FIELD_PREP_WM16(CLK_SARADC_DIV_MASK, div - 1),
	       RK3506_CLKSEL_CON(54));

	return rk3506_saradc_get_rate(priv, clk_id);
}

Here we use a helper function, could possible also be re-made into a macro so we actually can use FIELD_FIT() instead of bitwise ops.

Basic idea is that we assign div and test if the rate is divisible and fit inside mask at the same time, and if not we move on to next possible parent/divider.

With above and using the 1 MHz rate I would get something like following, provided I also added some log_debug() calls.

Quoted 6 lines

=> adc list
invalid divider 24 for prate 24000000, rate 1000000, mask f unsupported clk: id=193 rate=1000000 ret=-22
- adc@ff4e8000 status: -22

In the future I would also like to drop some priv->pll_hz uses and

We should report what's the configured frequency for a PLL from the registers. I don't know if we can reliably cache it though (hopefully TF-A/OP-TEE doesn't change the ones we're interested in :) ). I don't know how much we would spare in boot time by avoiding reading the registers often and used a cache version instead.

Cheers,
Quentin

#32 lore raw

Re: [PATCH 5/6] clk: rockchip: rk3506: Use FIELD_PREP_WM16 macro

Hi Quentin,

Quoted 62 lines · Quentin Schulz

Hi Jonas,

Quoted 59 lines · Jonas Karlman

Hi Quentin,

Quoted 19 lines · Quentin Schulz

Hi Jonas,

Quoted 16 lines · Jonas Karlman

Change to use writel() together with the FIELD_PREP_WM16() macro instead of using the rk_clrsetreg() macro to avoid having to define the mask as a parameter to both rk_clrsetreg() and FIELD_PREP(). Also change to use u32 variables consistently.

No change in behavior is expected due to this code style change.

---
   drivers/clk/rockchip/clk_rk3506.c | 195 ++++++++++++++----------------
   1 file changed, 90 insertions(+), 105 deletions(-)
drivers/clk/rockchip/clk_rk3506.c +0 -0
index e156bf19a6b4..7ccab313e6ec 100644

[snip]

Quoted 19 lines

drivers/clk/rockchip/clk_rk3506.c static ulong rk3506_fspi_set_rate(struct rk3506_clk_priv *priv, ulong rate) · line 778

Quoted 13 lines
drivers/clk/rockchip/clk_rk3506.c +1 -1
@@ -770,7 +759,7 @@ static ulong rk3506_fspi_get_rate(struct rk3506_clk_priv *priv)
 
   static ulong rk3506_fspi_set_rate(struct rk3506_clk_priv *priv, ulong rate)
   {
-	int div, sel;
+	u32 div, sel;
 
   	if (OSC_HZ % rate == 0) {
   		sel = SCLK_FSPI_SEL_24M;
@@ -787,10 +776,9 @@ static ulong rk3506_fspi_set_rate(struct rk3506_clk_priv *priv, ulong rate)
   	}
   	assert(div - 1 <= 31);
 

If somehow div = 0, then this will result in an underflow. Because it's an unsigned type, it'll wrap around and start at U32_MAX so assert() should catch it. However, assert() is a noop when DEBUG constant isn't defined, so this actually does nothing.

Quoted 4 lines

We should switch those assert to something that actually does something otherwise we could still very well attempt to write a divider that's too big and have weird behaviors instead of outright fail when we know we cannot do something that's requested.

I was looking into trying to use assert(FIELD_FIT(...)) to validate, but as you mention this does not help for non-DEBUG builds.

However, I have been playing around with something like following, still not sure I fully like it, please advise.

static inline u32 get_divider(ulong prate, ulong rate, u32 mask, bool divisible)

{
	u32 div;
Quoted 6 lines
Quoted 3 lines
	if (divisible && prate % rate)
		return 0;

What is "divisible" supposed to represent? I don't understand from reading the code.

"A number is divisible by another number if it can be divided into equal parts with no remainder."

Basic idea is to keep existing pattern, we first check if rate can be divided exact without any reminder, and fallback to do best effort to get closest rate. Divisible was a word that seemed to match this meaning.

Quoted 4 lines
Quoted 3 lines
	div = DIV_ROUND_UP(prate, rate);
	if (!div || ((div - 1) << __ffs(mask)) & ~mask)
Quoted 8 lines

I scratch my head looking at this so not sure it would stand the test of time. Would:

((div - 1) & mask) == (div - 1)

work better instead? If your value masked (I'm assuming you want to use the bitfield mask here, so all bits are set in the bitfield) doesn't equal your value, then the mask cannot contain the value.

Correct, we want to ensure that div-1 fit inside the field, i.e. same as:

  if (!div || !FIELD_FIT(mask, div - 1))

However, FIELD_FIT only works with constants, so above was a quick and dirty unpack of FIELD_FIT to test the idea. Hopefully there is a way to instead make use of FIELD_FIT and not a custom re-implementation.

Quoted 11 lines
Quoted 11 lines
		return 0;

	return div;
}

static ulong rk3506_saradc_set_rate(struct rk3506_clk_priv *priv, ulong clk_id,
ulong rate)

{
	u32 div, sel;
Quoted 8 lines
Quoted 6 lines
	if ((div = get_divider(32000, rate, CLK_SARADC_DIV_MASK, 1)))
		sel = CLK_SARADC_SEL_32K;
	else if ((div = get_divider(400000, rate, CLK_SARADC_DIV_MASK, 1)))
		sel = CLK_SARADC_SEL_400K;
	else if ((div = get_divider(OSC_HZ, rate, CLK_SARADC_DIV_MASK, 0)))
		sel = CLK_SARADC_SEL_24M;

Can you really not divide from x24m as a parent?

As mentioned above, final param was just to signal if we need exact match / no reminder.

Quoted 25 lines

Also, please do not assign a variable within an if condition, I find it very difficult to remember that checking an if condition can have side effects. I can suggest:

div = get_divider(32000, rate, CLK_SARADC_DIV_MASK, 1);

if (div) {
     sel = CLK_SARADC_SEL_32K;
     goto div_found;
}

div = get_divider(400000, rate, CLK_SARADC_DIV_MASK, 1);

if (div) {
     sel = CLK_SARADC_SEL_400K;
     goto div_found;
}

[...]

return -EINVAL;

div_found:
writel(FIELD_PREP_WM16(CLK_SARADC_SEL_MASK, sel) | [...]
return rk3506_saradc_get_rate(priv, clk_id);

Quoted 4 lines

Or use another function for

the writel+return currently at the end of your function and call that instead of a goto.

Yeah, I was considering goto and extra functions, but wanted to keep the logic as small and simple as possible.

I have evolved my idea to take advantage of macros to help make the code more readable:

	if (RATE_TO_DIV(div, 32000, rate, CLK_SARADC_DIV_MASK, 1))
		sel = CLK_SARADC_SEL_32K;
	else if (RATE_TO_DIV(div, 400000, rate, CLK_SARADC_DIV_MASK, 1))
		sel = CLK_SARADC_SEL_400K;
	else if (RATE_TO_DIV_OSC(div, rate, CLK_SARADC_DIV_MASK, 0))
		sel = CLK_SARADC_SEL_24M;
	else
		return -EINVAL;

or

	if (RATE_TO_DIV_PLL(div, PLL_V0PLL, rate, CLK_I2C0_DIV_MASK, 1))
		sel = CLK_I2C_SEL_V0PLL;
	else if (RATE_TO_DIV_PLL(div, PLL_V1PLL, rate, CLK_I2C0_DIV_MASK, 1))
		sel = CLK_I2C_SEL_V1PLL;
	else if (RATE_TO_DIV_PLL(div, PLL_GPLL, rate, CLK_I2C0_DIV_MASK, 0))
		sel = CLK_I2C_SEL_GPLL;
	else
		return -EINVAL;

It still assigns div (first parameter) inside the if, but with the benefit that it keeps the code and code flow as simple as possible.

See rk3506 [1] or rk3538 [2] clock driver in my rk3562 branch for a full working example of an entire clock driver using above pattern.

[1] https://git.u-boot-project.org/u-boot/contributors/kwiboo/u-boot/-/blob/rk3562/drivers/clk/rockchip/clk_rk3506.c#L500-519
[2] https://git.u-boot-project.org/u-boot/contributors/kwiboo/u-boot/-/blob/rk3562/drivers/clk/rockchip/clk_rk3538.c

Quoted 26 lines
Quoted 26 lines
	else
		return -EINVAL;

	writel(FIELD_PREP_WM16(CLK_SARADC_SEL_MASK, sel) |
	       FIELD_PREP_WM16(CLK_SARADC_DIV_MASK, div - 1),
	       RK3506_CLKSEL_CON(54));

	return rk3506_saradc_get_rate(priv, clk_id);
}

Here we use a helper function, could possible also be re-made into a macro so we actually can use FIELD_FIT() instead of bitwise ops.

Basic idea is that we assign div and test if the rate is divisible and fit inside mask at the same time, and if not we move on to next possible parent/divider.

With above and using the 1 MHz rate I would get something like following, provided I also added some log_debug() calls.

=> adc list
invalid divider 24 for prate 24000000, rate 1000000, mask f unsupported clk: id=193 rate=1000000 ret=-22
- adc@ff4e8000 status: -22

Quoted 7 lines
Quoted 1 line

In the future I would also like to drop some priv->pll_hz uses and

We should report what's the configured frequency for a PLL from the registers. I don't know if we can reliably cache it though (hopefully TF-A/OP-TEE doesn't change the ones we're interested in :) ). I don't know how much we would spare in boot time by avoiding reading the registers often and used a cache version instead.

In the clock drivers at [1] and [2] I have changed to fetch all PLL rates via <soc>_pll_get_rate(), the calls is mostly hidden behind new macros to keep code cleaner and easier to follow, e.g.:

	con = readl(RK3506_CLKSEL_CON(49));
	sel = FIELD_GET(CCLK_SDMMC_SEL_MASK, con);
	div = FIELD_GET(CCLK_SDMMC_DIV_MASK, con);

	if (sel == CCLK_SDMMC_SEL_24M)
		return DIV_TO_RATE_OSC(div);
	else if (sel == CCLK_SDMMC_SEL_GPLL)
		return DIV_TO_RATE_PLL(PLL_GPLL, div);
	else if (sel == CCLK_SDMMC_SEL_V0PLL)
		return DIV_TO_RATE_PLL(PLL_V0PLL, div);
	else if (sel == CCLK_SDMMC_SEL_V1PLL)
		return DIV_TO_RATE_PLL(PLL_V1PLL, div);
	else
		return -EINVAL;

With something like this we no longer need to pre-cache PLL rates at probe time, instead PLL rates will be fetched when needed.

The rk3562 branch contains wip commit diffs that step by step transition current rk3506 clock driver to use above proposed patterns.

Please take a look at the full rk3506 driver to see what you think :-)

Regards,
Jonas

Quoted 3 lines

Cheers,
Quentin

#33 lore raw

Re: [PATCH 5/6] clk: rockchip: rk3506: Use FIELD_PREP_WM16 macro

Hi Jonas,

Quoted 69 lines · Jonas Karlman

Hi Quentin,

Quoted 66 lines · Quentin Schulz

Hi Jonas,

Quoted 62 lines · Jonas Karlman

Hi Quentin,

Quoted 19 lines · Quentin Schulz

Hi Jonas,

Quoted 16 lines · Jonas Karlman

Change to use writel() together with the FIELD_PREP_WM16() macro instead of using the rk_clrsetreg() macro to avoid having to define the mask as a parameter to both rk_clrsetreg() and FIELD_PREP(). Also change to use u32 variables consistently.

No change in behavior is expected due to this code style change.

---
    drivers/clk/rockchip/clk_rk3506.c | 195 ++++++++++++++----------------
    1 file changed, 90 insertions(+), 105 deletions(-)
drivers/clk/rockchip/clk_rk3506.c +0 -0
index e156bf19a6b4..7ccab313e6ec 100644

[snip]

Quoted 19 lines

drivers/clk/rockchip/clk_rk3506.c static ulong rk3506_fspi_set_rate(struct rk3506_clk_priv *priv, ulong rate) · line 778

Quoted 13 lines
drivers/clk/rockchip/clk_rk3506.c +1 -1
@@ -770,7 +759,7 @@ static ulong rk3506_fspi_get_rate(struct rk3506_clk_priv *priv)
 
    static ulong rk3506_fspi_set_rate(struct rk3506_clk_priv *priv, ulong rate)
    {
-	int div, sel;
+	u32 div, sel;
 
    	if (OSC_HZ % rate == 0) {
    		sel = SCLK_FSPI_SEL_24M;
@@ -787,10 +776,9 @@ static ulong rk3506_fspi_set_rate(struct rk3506_clk_priv *priv, ulong rate)
    	}
    	assert(div - 1 <= 31);
 

If somehow div = 0, then this will result in an underflow. Because it's an unsigned type, it'll wrap around and start at U32_MAX so assert() should catch it. However, assert() is a noop when DEBUG constant isn't defined, so this actually does nothing.

Quoted 4 lines

We should switch those assert to something that actually does something otherwise we could still very well attempt to write a divider that's too big and have weird behaviors instead of outright fail when we know we cannot do something that's requested.

I was looking into trying to use assert(FIELD_FIT(...)) to validate, but as you mention this does not help for non-DEBUG builds.

However, I have been playing around with something like following, still not sure I fully like it, please advise.

static inline u32 get_divider(ulong prate, ulong rate, u32 mask, bool divisible)

{
	u32 div;

	if (divisible && prate % rate)
		return 0;
Quoted 6 lines
Quoted 2 lines

What is "divisible" supposed to represent? I don't understand from reading the code.

"A number is divisible by another number if it can be divided into equal parts with no remainder."

Yes, which is what prate % rate == 0 would do.

Quoted 4 lines

Basic idea is to keep existing pattern, we first check if rate can be divided exact without any reminder, and fallback to do best effort to get closest rate. Divisible was a word that seemed to match this meaning.

You set divisible only for 32K and 400K but not 24M. And after reading the code 5 times I now understand that the only thing you care about is that the *last* call to get_divider() sets divisible to false.

If I remember correctly, we're supposed to take the clock configuration that allows for a frequency that is below the requested one, but as close as possible. So the logic here is kinda broken, you'd only select 32k or 400k if we can cleanly derive the frequency, and default to 24M otherwise.

Take me requesting 31KHz. It cannot be cleanly derived from neither 32KHz, nor 400Kz, nor 24MHz. We'll end up trying to get it from 24MHz, while the max divider is 16 and we clearly won't be able to derive 31KHz from it with that divider, so we return 0 and we error out. Instead, we can at least derive something from 400KHz by using divider 13, which amounts to ~30.77KHz. So I think we should instead just always return div if it fits inside the register and do maths to figure out which of the possible parents provide a rate that is lower but closest to the requested rate.

Quoted 69 lines
Quoted 4 lines
Quoted 3 lines
	div = DIV_ROUND_UP(prate, rate);
	if (!div || ((div - 1) << __ffs(mask)) & ~mask)
Quoted 8 lines

I scratch my head looking at this so not sure it would stand the test of time. Would:

((div - 1) & mask) == (div - 1)

work better instead? If your value masked (I'm assuming you want to use the bitfield mask here, so all bits are set in the bitfield) doesn't equal your value, then the mask cannot contain the value.

Correct, we want to ensure that div-1 fit inside the field, i.e. same as:

   if (!div || !FIELD_FIT(mask, div - 1))

However, FIELD_FIT only works with constants, so above was a quick and dirty unpack of FIELD_FIT to test the idea. Hopefully there is a way to instead make use of FIELD_FIT and not a custom re-implementation.

Quoted 11 lines
Quoted 11 lines
		return 0;

	return div;
}

static ulong rk3506_saradc_set_rate(struct rk3506_clk_priv *priv, ulong clk_id,
ulong rate)

{
	u32 div, sel;
Quoted 8 lines
Quoted 6 lines
	if ((div = get_divider(32000, rate, CLK_SARADC_DIV_MASK, 1)))
		sel = CLK_SARADC_SEL_32K;
	else if ((div = get_divider(400000, rate, CLK_SARADC_DIV_MASK, 1)))
		sel = CLK_SARADC_SEL_400K;
	else if ((div = get_divider(OSC_HZ, rate, CLK_SARADC_DIV_MASK, 0)))
		sel = CLK_SARADC_SEL_24M;

Can you really not divide from x24m as a parent?

As mentioned above, final param was just to signal if we need exact match / no reminder.

Quoted 25 lines

Also, please do not assign a variable within an if condition, I find it very difficult to remember that checking an if condition can have side effects. I can suggest:

div = get_divider(32000, rate, CLK_SARADC_DIV_MASK, 1);

if (div) {
      sel = CLK_SARADC_SEL_32K;
      goto div_found;
}

div = get_divider(400000, rate, CLK_SARADC_DIV_MASK, 1);

if (div) {
      sel = CLK_SARADC_SEL_400K;
      goto div_found;
}

[...]

return -EINVAL;

div_found:
writel(FIELD_PREP_WM16(CLK_SARADC_SEL_MASK, sel) | [...]
return rk3506_saradc_get_rate(priv, clk_id);

Quoted 8 lines
Quoted 4 lines

Or use another function for

the writel+return currently at the end of your function and call that instead of a goto.

Yeah, I was considering goto and extra functions, but wanted to keep the logic as small and simple as possible.

But I think the logic is broken unfortunately. And I really dislike that we need to remember the last call to get_divider must have divisible unset.

Quoted 85 lines

I have evolved my idea to take advantage of macros to help make the code more readable:

	if (RATE_TO_DIV(div, 32000, rate, CLK_SARADC_DIV_MASK, 1))
		sel = CLK_SARADC_SEL_32K;
	else if (RATE_TO_DIV(div, 400000, rate, CLK_SARADC_DIV_MASK, 1))
		sel = CLK_SARADC_SEL_400K;
	else if (RATE_TO_DIV_OSC(div, rate, CLK_SARADC_DIV_MASK, 0))
		sel = CLK_SARADC_SEL_24M;
	else
		return -EINVAL;

or

	if (RATE_TO_DIV_PLL(div, PLL_V0PLL, rate, CLK_I2C0_DIV_MASK, 1))
		sel = CLK_I2C_SEL_V0PLL;
	else if (RATE_TO_DIV_PLL(div, PLL_V1PLL, rate, CLK_I2C0_DIV_MASK, 1))
		sel = CLK_I2C_SEL_V1PLL;
	else if (RATE_TO_DIV_PLL(div, PLL_GPLL, rate, CLK_I2C0_DIV_MASK, 0))
		sel = CLK_I2C_SEL_GPLL;
	else
		return -EINVAL;

It still assigns div (first parameter) inside the if, but with the benefit that it keeps the code and code flow as simple as possible.

See rk3506 [1] or rk3538 [2] clock driver in my rk3562 branch for a full working example of an entire clock driver using above pattern.

[1] https://git.u-boot-project.org/u-boot/contributors/kwiboo/u-boot/-/blob/rk3562/drivers/clk/rockchip/clk_rk3506.c#L500-519
[2] https://git.u-boot-project.org/u-boot/contributors/kwiboo/u-boot/-/blob/rk3562/drivers/clk/rockchip/clk_rk3538.c

Quoted 26 lines
Quoted 26 lines
	else
		return -EINVAL;

	writel(FIELD_PREP_WM16(CLK_SARADC_SEL_MASK, sel) |
	       FIELD_PREP_WM16(CLK_SARADC_DIV_MASK, div - 1),
	       RK3506_CLKSEL_CON(54));

	return rk3506_saradc_get_rate(priv, clk_id);
}

Here we use a helper function, could possible also be re-made into a macro so we actually can use FIELD_FIT() instead of bitwise ops.

Basic idea is that we assign div and test if the rate is divisible and fit inside mask at the same time, and if not we move on to next possible parent/divider.

With above and using the 1 MHz rate I would get something like following, provided I also added some log_debug() calls.

=> adc list
invalid divider 24 for prate 24000000, rate 1000000, mask f unsupported clk: id=193 rate=1000000 ret=-22
- adc@ff4e8000 status: -22

Quoted 7 lines
Quoted 1 line

In the future I would also like to drop some priv->pll_hz uses and

We should report what's the configured frequency for a PLL from the registers. I don't know if we can reliably cache it though (hopefully TF-A/OP-TEE doesn't change the ones we're interested in :) ). I don't know how much we would spare in boot time by avoiding reading the registers often and used a cache version instead.

In the clock drivers at [1] and [2] I have changed to fetch all PLL rates via <soc>_pll_get_rate(), the calls is mostly hidden behind new macros to keep code cleaner and easier to follow, e.g.:

	con = readl(RK3506_CLKSEL_CON(49));
	sel = FIELD_GET(CCLK_SDMMC_SEL_MASK, con);
	div = FIELD_GET(CCLK_SDMMC_DIV_MASK, con);

	if (sel == CCLK_SDMMC_SEL_24M)
		return DIV_TO_RATE_OSC(div);
	else if (sel == CCLK_SDMMC_SEL_GPLL)
		return DIV_TO_RATE_PLL(PLL_GPLL, div);
	else if (sel == CCLK_SDMMC_SEL_V0PLL)
		return DIV_TO_RATE_PLL(PLL_V0PLL, div);
	else if (sel == CCLK_SDMMC_SEL_V1PLL)
		return DIV_TO_RATE_PLL(PLL_V1PLL, div);
	else
		return -EINVAL;
Quoted 3 lines

With something like this we no longer need to pre-cache PLL rates at probe time, instead PLL rates will be fetched when needed.

I think the kernel caches the clock rates by default (unless CLK_GET_RATE_NOCACHE is set). They are probably doing this for a reason, hopefully only performance? Gut feeling is this will increase boot time, but considering you've been hunting this recently, I'm guessing you had a look at the impact and decided the time increase (if any) wasn't meaningful?

Cheers,
Quentin

#34 lore raw

PATCH 6/6 rockchip: rk3506: Use FIELD_PREP_WM16 macro

In reply to Jonas Karlman

Change to use writel() together with the FIELD_PREP_WM16() macro instead of using the rk_clrreg() macro or custom write-enable bit handling.

No change in behavior is expected due to this code style change.

---
 arch/arm/mach-rockchip/rk3506/rk3506.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
arch/arm/mach-rockchip/rk3506/rk3506.c +4 -3
index 2ed1dcc128e0..ef0943434e5e 100644
@@ -6,7 +6,8 @@
 #include <dm.h>
 #include <misc.h>
 #include <asm/arch-rockchip/bootrom.h>
-#include <asm/arch-rockchip/hardware.h>
+#include <asm/io.h>
+#include <linux/hw_bitfield.h>
 
 #define SGRF_BASE			0xff210000
 
@@ -32,7 +33,7 @@ int arch_cpu_init(void)
 		return 0;
 
 	/* Select non-secure OTPC */
-	rk_clrreg(SGRF_BASE + 0x100, BIT(1));
+	writel(FIELD_PREP_WM16(BIT(1), 0), SGRF_BASE + 0x0100);
 
 	/* Set the sdmmc/emmc to access ddr memory */
 	val = readl(FIREWALL_DDR_BASE + FW_DDR_MST1_REG);
@@ -77,7 +78,7 @@ void rockchip_stimer_init(void)
 	asm volatile("mcr p15, 0, %0, c14, c0, 0" : : "r" (CONFIG_COUNTER_FREQUENCY));
 	writel(0xffffffff, HP_TIMER_BASE + HP_LOAD_COUNT0_REG);
 	writel(0xffffffff, HP_TIMER_BASE + HP_LOAD_COUNT1_REG);
-	writel((TIMER_EN << 16) | TIMER_EN, HP_TIMER_BASE + HP_CTRL_REG);
+	writel(FIELD_PREP_WM16(TIMER_EN, 1), HP_TIMER_BASE + HP_CTRL_REG);
 }
 
 #define RK3506_OTP_CPU_CODE_OFFSET		0x02
Signature · 1 line
2.54.0
Reviewed-by on v1 6/6
#35 lore raw

Re: [PATCH 6/6] rockchip: rk3506: Use FIELD_PREP_WM16 macro

Quoted 11 lines · Jonas Karlman

rockchip: rk3506: Use FIELD_PREP_WM16 macro

Change to use writel() together with the FIELD_PREP_WM16() macro instead of using the rk_clrreg() macro or custom write-enable bit handling.

No change in behavior is expected due to this code style change.

arch/arm/mach-rockchip/rk3506/rk3506.c | 7 ++++---

 1 file changed, 4 insertions(+), 3 deletions(-)
Reviewed-by on v1 6/6
#36 lore raw

Re: [PATCH 6/6] rockchip: rk3506: Use FIELD_PREP_WM16 macro

In reply to Jonas Karlman

Hi Jonas,

Quoted 5 lines · Jonas Karlman

Change to use writel() together with the FIELD_PREP_WM16() macro instead of using the rk_clrreg() macro or custom write-enable bit handling.

No change in behavior is expected due to this code style change.

Thanks!
Quentin