Skip to contentSkip to conversation

rockchip: rk3576: Disable force_jtag by default

PATCH In review Jonas Karlman 4 participants 8 messages Aug 3 – Aug 13

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

v2 cover rockchip: rk3576: Disable force_jtag by default

This series import linux/hw_bitfield.h from Linux and disables the force_jtag flag by default on RK3576.

Changes in v2:
- Import linux/hw_bitfield.h from Linux
- Define TOP_IOC_FORCE_JTAG and use FIELD_PREP_WM16
- Collect r-b tag

Jonas Karlman (2):
include: linux: Import hw_bitfield.h from Linux v6.18
rockchip: rk3576: Disable force_jtag by default

 arch/arm/mach-rockchip/rk3576/rk3576.c | 12 ++++-
 include/linux/hw_bitfield.h            | 61 ++++++++++++++++++++++++++
 2 files changed, 72 insertions(+), 1 deletion(-)
 create mode 100644 include/linux/hw_bitfield.h
Signature · 1 line
2.54.0
#1 lore raw

PATCH v2 1/2 include: linux: Import hw_bitfield.h from Linux v6.18

Hardware of various vendors, but very notably Rockchip, often uses 32-bit registers where the upper 16-bit half of the register is a write-enable mask for the lower half.

Add hw_bitfield.h with two macros: FIELD_PREP_WM16, and FIELD_PREP_WM16_CONST. The latter is a version that can be used in initializers, like FIELD_PREP_CONST.

Import hw_bitfield.h from Linux commit 21368fcbb124 ("bitmap: introduce hardware-specific bitfield operations"), first introduced in v6.18-rc1.

This contains two changes compared to the Linux version:
- include of linux/limit.h was dropped
- use of const_true() in FIELD_PREP_WM16_CONST() was dropped

---
v2: New patch

---
 include/linux/hw_bitfield.h | 61 +++++++++++++++++++++++++++++++++++++
 1 file changed, 61 insertions(+)
 create mode 100644 include/linux/hw_bitfield.h
include/linux/hw_bitfield.h new file +61 -0
new file mode 100644
index 000000000000..6e74d902ed33
@@ -0,0 +1,61 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * Copyright (C) 2025, Collabora Ltd.
+ */
+
+#ifndef _LINUX_HW_BITFIELD_H
+#define _LINUX_HW_BITFIELD_H
+
+#include <linux/bitfield.h>
+#include <linux/build_bug.h>
+
+/**
+ * FIELD_PREP_WM16() - prepare a bitfield element with a mask in the upper half
+ * @_mask: shifted mask defining the field's length and position
+ * @_val:  value to put in the field
+ *
+ * FIELD_PREP_WM16() masks and shifts up the value, as well as bitwise ORs the
+ * result with the mask shifted up by 16.
+ *
+ * This is useful for a common design of hardware registers where the upper
+ * 16-bit half of a 32-bit register is used as a write-enable mask. In such a
+ * register, a bit in the lower half is only updated if the corresponding bit
+ * in the upper half is high.
+ */
+#define FIELD_PREP_WM16(_mask, _val)					     \
+	({								     \
+		typeof(_val) __val = _val;				     \
+		typeof(_mask) __mask = _mask;				     \
+		__BF_FIELD_CHECK(__mask, ((u16)0U), __val,		     \
+				 "HWORD_UPDATE: ");			     \
+		(((typeof(__mask))(__val) << __bf_shf(__mask)) & (__mask)) | \
+		((__mask) << 16);					     \
+	})
+
+/**
+ * FIELD_PREP_WM16_CONST() - prepare a constant bitfield element with a mask in
+ *                           the upper half
+ * @_mask: shifted mask defining the field's length and position
+ * @_val:  value to put in the field
+ *
+ * FIELD_PREP_WM16_CONST() masks and shifts up the value, as well as bitwise ORs
+ * the result with the mask shifted up by 16.
+ *
+ * This is useful for a common design of hardware registers where the upper
+ * 16-bit half of a 32-bit register is used as a write-enable mask. In such a
+ * register, a bit in the lower half is only updated if the corresponding bit
+ * in the upper half is high.
+ *
+ * Unlike FIELD_PREP_WM16(), this is a constant expression and can therefore
+ * be used in initializers. Error checking is less comfortable for this
+ * version.
+ */
+#define FIELD_PREP_WM16_CONST(_mask, _val)				 \
+	(								 \
+		FIELD_PREP_CONST(_mask, _val) |				 \
+		(BUILD_BUG_ON_ZERO((u64)(_mask) > U16_MAX) + \
+		 ((_mask) << 16))					 \
+	)
+
+
+#endif /* _LINUX_HW_BITFIELD_H */
Signature · 1 line
2.54.0
Acked-by on v2 1/2
#2 lore raw

Re: [PATCH v2 1/2] include: linux: Import hw_bitfield.h from Linux v6.18

Hi Jonas,

Quoted 8 lines · Jonas Karlman

Hardware of various vendors, but very notably Rockchip, often uses 32-bit registers where the upper 16-bit half of the register is a write-enable mask for the lower half.

Add hw_bitfield.h with two macros: FIELD_PREP_WM16, and FIELD_PREP_WM16_CONST. The latter is a version that can be used in initializers, like FIELD_PREP_CONST.

Quoted 7 lines

Import hw_bitfield.h from Linux commit 21368fcbb124 ("bitmap: introduce hardware-specific bitfield operations"), first introduced in v6.18-rc1.

This contains two changes compared to the Linux version:
- include of linux/limit.h was dropped
- use of const_true() in FIELD_PREP_WM16_CONST() was dropped

The content in this patch is as described in the commit log.

I'm wondering whether we should try to get include/linux/limit.h (and include/vdso/limit.h) in. Also wondering if we could also get const_true() in somehow.

No changes made to this file in Linux kernel since v6.18-rc1, so could as well have said it's taken from 7.2-rc6 (which also means no missed fixes).

I'll wait on Tom's Reviewed-by before merging this though since it's not a straight import from the Linux kernel. I'm targeting the next branch for this.

Cheers,
Quentin

#3 lore raw

Re: [PATCH v2 1/2] include: linux: Import hw_bitfield.h from Linux v6.18

Quoted 21 lines · Quentin Schulz

Hi Jonas,

Quoted 8 lines · Jonas Karlman

Hardware of various vendors, but very notably Rockchip, often uses 32-bit registers where the upper 16-bit half of the register is a write-enable mask for the lower half.

Add hw_bitfield.h with two macros: FIELD_PREP_WM16, and FIELD_PREP_WM16_CONST. The latter is a version that can be used in initializers, like FIELD_PREP_CONST.

Quoted 7 lines

Import hw_bitfield.h from Linux commit 21368fcbb124 ("bitmap: introduce hardware-specific bitfield operations"), first introduced in v6.18-rc1.

This contains two changes compared to the Linux version:
- include of linux/limit.h was dropped
- use of const_true() in FIELD_PREP_WM16_CONST() was dropped

Quoted 8 lines

The content in this patch is as described in the commit log.

I'm wondering whether we should try to get include/linux/limit.h (and include/vdso/limit.h) in. Also wondering if we could also get const_true() in somehow.

No changes made to this file in Linux kernel since v6.18-rc1, so could as well have said it's taken from 7.2-rc6 (which also means no missed fixes).

I'll wait on Tom's Reviewed-by before merging this though since it's not a straight import from the Linux kernel. I'm targeting the next branch for this.

Read patch 2 again and you convinced me that this should rather go in main, so will wait for Tom's R-b and target one of the 2026.10-rcX.

Sorry for the noise.

Cheers,
Quentin

#4 lore raw

Re: [PATCH v2 1/2] include: linux: Import hw_bitfield.h from Linux v6.18

In reply to Quentin Schulz

Hi Quentin,

Quoted 19 lines · Quentin Schulz

Hi Jonas,

Quoted 8 lines · Jonas Karlman

Hardware of various vendors, but very notably Rockchip, often uses 32-bit registers where the upper 16-bit half of the register is a write-enable mask for the lower half.

Add hw_bitfield.h with two macros: FIELD_PREP_WM16, and FIELD_PREP_WM16_CONST. The latter is a version that can be used in initializers, like FIELD_PREP_CONST.

Quoted 7 lines

Import hw_bitfield.h from Linux commit 21368fcbb124 ("bitmap: introduce hardware-specific bitfield operations"), first introduced in v6.18-rc1.

This contains two changes compared to the Linux version:
- include of linux/limit.h was dropped
- use of const_true() in FIELD_PREP_WM16_CONST() was dropped

Quoted 7 lines

The content in this patch is as described in the commit log.

I'm wondering whether we should try to get include/linux/limit.h (and include/vdso/limit.h) in. Also wondering if we could also get const_true() in somehow.

U16_MAX, used in FIELD_PREP_WM16_CONST(), is what would have been imported by linux/limit.h, there is also a U-Boot specific limits.h that defines U16_MAX but it felt wrong to depend on the U-Boot header inside the linux header. Using the FIELD_PREP_WM16_CONST() instead of the non const variant also never reported any missing U16_MAX define.

const_true() would have been nice to import, but that was a separate issue, linux/compiler.h is very outdated and looks harder to sync without risking introducing new possible issues.

Testing FIELD_PREP_WM16_CONST() without const_true() and mask > U16_MAX do produce a compile time error, but the error message may look a little bit strange:

In file included from arch/arm/mach-rockchip/rk3576/rk3576.c:12:
arch/arm/mach-rockchip/rk3576/rk3576.c: In function ‘arch_cpu_init’:
include/linux/build_bug.h:15:50: error: negative width in bit-field ‘<anonymous>’

     15 | #define BUILD_BUG_ON_ZERO(e) ((int)sizeof(struct { int:(-!!(e)); }))
        |                                                  ^
  ./arch/arm/include/asm/io.h:178:38: note: in definition of macro ‘writel’
    178 | #define writel(v, c)    ({ u32 __v = v; __iowmb(); writel_relaxed(__v, c); __v; })
        |                                      ^
  include/linux/hw_bitfield.h:56:18: note: in expansion of macro ‘BUILD_BUG_ON_ZERO’
     56 |                 (BUILD_BUG_ON_ZERO((u64)(_mask) > U16_MAX) + \
        |                  ^~~~~~~~~~~~~~~~~
  arch/arm/mach-rockchip/rk3576/rk3576.c:200:24: note: in expansion of macro ‘FIELD_PREP_WM16_CONST’
    200 |                 writel(FIELD_PREP_WM16_CONST(BIT(16), 0),
        |                        ^~~~~~~~~~~~~~~~~~~~~
  include/linux/hw_bitfield.h:54:9: warning: conversion from ‘long unsigned int’ to ‘u32’ {aka ‘unsigned int’} changes value from ‘4294967300’ to ‘4’ [-Woverflow]
     54 |         (                                                                \
        |         ^
  ./arch/arm/include/asm/io.h:178:38: note: in definition of macro ‘writel’
    178 | #define writel(v, c)    ({ u32 __v = v; __iowmb(); writel_relaxed(__v, c); __v; })
        |                                      ^
  arch/arm/mach-rockchip/rk3576/rk3576.c:200:24: note: in expansion of macro ‘FIELD_PREP_WM16_CONST’
    200 |                 writel(FIELD_PREP_WM16_CONST(BIT(16), 0),
        |                        ^~~~~~~~~~~~~~~~~~~~~
Quoted 4 lines

No changes made to this file in Linux kernel since v6.18-rc1, so could as well have said it's taken from 7.2-rc6 (which also means no missed fixes).

Hum, I thought I mentioned that it has not changed up to v7.1, but looks like that comment did not made it in.

Regards,
Jonas

Quoted 7 lines

I'll wait on Tom's Reviewed-by before merging this though since it's not a straight import from the Linux kernel. I'm targeting the next branch for this.

Cheers,
Quentin

Reviewed-by on v2 1/2
#5 lore raw

Re: [PATCH v2 1/2] include: linux: Import hw_bitfield.h from Linux v6.18

In reply to Jonas Karlman
Quoted 8 lines · Jonas Karlman

Hardware of various vendors, but very notably Rockchip, often uses 32-bit registers where the upper 16-bit half of the register is a write-enable mask for the lower half.

Add hw_bitfield.h with two macros: FIELD_PREP_WM16, and FIELD_PREP_WM16_CONST. The latter is a version that can be used in initializers, like FIELD_PREP_CONST.

Quoted 8 lines

Import hw_bitfield.h from Linux commit 21368fcbb124 ("bitmap: introduce hardware-specific bitfield operations"), first introduced in v6.18-rc1.

This contains two changes compared to the Linux version:
- include of linux/limit.h was dropped
- use of const_true() in FIELD_PREP_WM16_CONST() was dropped

Signature · 1 line
Tom
#6 lore raw

PATCH v2 2/2 rockchip: rk3576: Disable force_jtag by default

In reply to Jonas Karlman

Rockchip SoCs can automatically switch between jtag and sdmmc based on the following rules:
- all the SDMMC pins including SDMMC_DET set as SDMMC function in GRF,
- force_jtag bit in GRF is 1,
- SDMMC_DET is low (no card detected),

Note that the BootROM may mux all SDMMC pins in their SDMMC function or not, depending on the boot medium that were tried.

Because SDMMC_DET pin is not guaranteed to be used as an SD card card detect pin, it could be low at boot or even switch at runtime, which would enable the jtag function and render the SD card unusable.

Or boards using cd-gpios may switch the SDMMC_DET pin to GPIO function, which would enable the jtag function and render the SD card unusable.

With commit d0a838bdc629 ("Subtree merge tag 'v7.1-dts' of dts repo [1] into dts/upstream") there are now RK3576 boards that have changed to use cd-gpios for the SDMMC_DET pin, e.g. NanoPi R76S, that may have issues detecting SD card unless force_jtag is disabled.

---
Changes in v2:
- Define TOP_IOC_FORCE_JTAG and use FIELD_PREP_WM16
- Collect r-b tag

---
 arch/arm/mach-rockchip/rk3576/rk3576.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
arch/arm/mach-rockchip/rk3576/rk3576.c +11 -1
index e3e93f663959..76119cdb1aec 100644
@@ -9,7 +9,8 @@
 #include <misc.h>
 #include <asm/armv8/mmu.h>
 #include <asm/arch-rockchip/bootrom.h>
-#include <asm/arch-rockchip/hardware.h>
+#include <asm/io.h>
+#include <linux/hw_bitfield.h>
 
 #define SYS_GRF_BASE		0x2600A000
 #define SYS_GRF_SOC_CON2	0x0008
@@ -21,6 +22,10 @@
 #define GPIO0B_PULL_L		0x0024
 #define GPIO0B_IE_L		0x002C
 
+#define TOP_IOC_BASE		0x26044000
+#define IOC_MISC_CON		0x00F0
+#define TOP_IOC_FORCE_JTAG	BIT(1)
+
 #define SYS_SGRF_BASE		0x26004000
 #define SYS_SGRF_SOC_CON14	0x0058
 #define SYS_SGRF_SOC_CON15	0x005C
@@ -190,6 +195,11 @@ int arch_cpu_init(void)
 	 */
 	writel(0xffffff00, SYS_SGRF_BASE + SYS_SGRF_SOC_CON20);
 
+	/* Disable JTAG exposed on SDMMC pins (GPIO2A2 and GPIO2A3) */
+	if (IS_ENABLED(CONFIG_ROCKCHIP_DISABLE_FORCE_JTAG))
+		writel(FIELD_PREP_WM16(TOP_IOC_FORCE_JTAG, 0),
+		       TOP_IOC_BASE + IOC_MISC_CON);
+
 	/* Disable USB3OTG0 U3 port, later enabled by USBDP PHY driver */
 	writel(0xffff0188, USB_GRF_BASE + USB3OTG0_CON1);
 
Signature · 1 line
2.54.0
#7 lore raw
In reply to Jonas Karlman
Quoted 9 lines · Jonas Karlman

This series import linux/hw_bitfield.h from Linux and disables the force_jtag flag by default on RK3576.

Changes in v2:
- Import linux/hw_bitfield.h from Linux
- Define TOP_IOC_FORCE_JTAG and use FIELD_PREP_WM16
- Collect r-b tag

[...]

Applied to https://git.u-boot-project.org/u-boot/custodians/u-boot-rockchip (u-boot-rockchip-2026.10), thanks!

[1/2] include: linux: Import hw_bitfield.h from Linux v6.18

      https://git.u-boot-project.org/u-boot/custodians/u-boot-rockchip/-/commit/0c68b88ce574ddaa2f94f3e5d747c25db693dc18
[2/2] rockchip: rk3576: Disable force_jtag by default
      https://git.u-boot-project.org/u-boot/custodians/u-boot-rockchip/-/commit/c8cac405f29896362786c7ced1417fd15e7b8923

Best regards,

Signature · 1 line
Quentin Schulz <[email protected]>