Skip to contentSkip to conversation

Add rkmtd command

PATCH In review Johan Jonker 3 participants 12 messages Oct 18, 2023 – Oct 23, 2023

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

v5 cover Add rkmtd command

The command rkmtd creates a virtual block device to transfer Rockchip boot block data to and from NAND with block orientated tools like "ums" and "rockusb".

It uses the Rockchip MTD driver to scan for boot blocks and copies data from the first block in a GPT formatted virtual disk. Data must be written in U-boot "idbloader.img" format and start at partition "loader1" offset 64. The data header is parsed for length and offset. When the last sector is received it erases up to 5 erase blocks on NAND and writes boot blocks in a pattern depending on the NAND ID. Data is then verified. When a block turns out bad the block header is discarded.

Changed V5:
Add bounce buffer flag
Use devres_alloc in bind
Restyle

Changed V4:
Sort includes
Replace constant by define
Enable rkmtd command in sandbox defconfig
Fix minor things in test

Changed V3:
Add documetation
Add test
Split driver from command
Split header
Use devm_kzalloc
Remove out of memory debug
Restyle

Changed V2:
Rename to rkmtd

Johan Jonker (8):
mtd: nand: raw: rockchip_nfc: add NAND_SKIP_BBTSCAN option
rockchip: dm: prepare rkmtd UCLASS
rockchip: block: add rkmtd class and drivers
rockchip: block: blk-uclass: add bounce buffer flag to blk_desc
rockchip: cmd: add rkmtd command
rockchip: test: dm: add rkmtd test
rockchip: doc: add rkmtd.rst
rockchip: configs: sandbox: enable rkmtd command

 cmd/Kconfig                         |    8 +
 cmd/Makefile                        |    1 +
 cmd/rkmtd.c                         |  204 +++++
 configs/sandbox64_defconfig         |    1 +
 configs/sandbox_defconfig           |    1 +
 disk/part.c                         |    4 +
 doc/board/rockchip/index.rst        |    1 +
 doc/board/rockchip/rkmtd.rst        |  105 +++
 drivers/block/Kconfig               |    7 +
 drivers/block/Makefile              |    2 +
 drivers/block/blk-uclass.c          |    5 +-
 drivers/block/rkmtd.c               | 1152 +++++++++++++++++++++++++++
 drivers/mtd/nand/raw/Kconfig        |    9 +
 drivers/mtd/nand/raw/rockchip_nfc.c |    3 +
 drivers/scsi/scsi.c                 |    4 +
 include/blk.h                       |    1 +
 include/dm/uclass-id.h              |    1 +
 include/rkmtd.h                     |  191 +++++
 test/dm/Makefile                    |    1 +
 test/dm/rkmtd.c                     |  200 +++++
 20 files changed, 1899 insertions(+), 2 deletions(-)
 create mode 100644 cmd/rkmtd.c
 create mode 100644 doc/board/rockchip/rkmtd.rst
 create mode 100644 drivers/block/rkmtd.c
 create mode 100644 include/rkmtd.h
 create mode 100644 test/dm/rkmtd.c

--
2.39.2

#1 lore raw

PATCH v5 1/8 mtd: nand: raw: rockchip_nfc: add NAND_SKIP_BBTSCAN option

On Rockchip SoCs the first boot stages are written on NAND with help of manufacturer software that uses a different format then the MTD framework. Skip the automatic BBT scan with the NAND_SKIP_BBTSCAN option to be able to pass the driver probe function and to let the original data unchanged.

---
 drivers/mtd/nand/raw/Kconfig        | 9 +++++++++
 drivers/mtd/nand/raw/rockchip_nfc.c | 3 +++
 2 files changed, 12 insertions(+)
drivers/mtd/nand/raw/Kconfig +9 -0
index d624589a892b..72547f00fbec 100644
@@ -611,6 +611,15 @@ config ROCKCHIP_NAND
 	    NFC v800: RK3308, RV1108
 	    NFC v900: PX30, RK3326
 
+config ROCKCHIP_NAND_SKIP_BBTSCAN
+	bool "Skip the automatic BBT scan with Rockchip NAND controllers"
+	depends on ROCKCHIP_NAND
+	default n
+	help
+	  Skip the automatic BBT scan with the NAND_SKIP_BBTSCAN
+	  option when data content is not in MTD format or
+	  must remain unchanged.
+
 config TEGRA_NAND
 	bool "Support for NAND controller on Tegra SoCs"
 	depends on ARCH_TEGRA
drivers/mtd/nand/raw/rockchip_nfc.c +3 -0
index 6ad51df4acff..df6742c2f9bb 100644
@@ -981,6 +981,9 @@ static int rk_nfc_nand_chip_init(ofnode node, struct rk_nfc *nfc, int devnum)
 	chip->bbt_options = NAND_BBT_USE_FLASH | NAND_BBT_NO_OOB;
 	chip->options |= NAND_NO_SUBPAGE_WRITE | NAND_USE_BOUNCE_BUFFER;
 
+	if (IS_ENABLED(CONFIG_ROCKCHIP_NAND_SKIP_BBTSCAN))
+		chip->options |= NAND_SKIP_BBTSCAN;
+
 	rk_nfc_hw_init(nfc);
 	ret = nand_scan_ident(mtd, nsels, NULL);
 	if (ret)

--
2.39.2

#2 lore raw

PATCH v5 2/8 rockchip: dm: prepare rkmtd UCLASS

In reply to Johan Jonker

Prepare a rkmtd UCLASS in use for writing Rockchip boot blocks in combination with existing userspace tools and rockusb command.

---
 disk/part.c                | 4 ++++
 drivers/block/blk-uclass.c | 1 +
 include/dm/uclass-id.h     | 1 +
 3 files changed, 6 insertions(+)
disk/part.c +4 -0
index 85244b09f359..36b88205eca7 100644
@@ -197,6 +197,7 @@ void dev_print(struct blk_desc *desc)
 	case UCLASS_PVBLOCK:
 	case UCLASS_HOST:
 	case UCLASS_BLKMAP:
+	case UCLASS_RKMTD:
 		printf ("Vendor: %s Rev: %s Prod: %s\n",
 			desc->vendor,
 			desc->revision,
@@ -330,6 +331,9 @@ static void print_part_header(const char *type, struct blk_desc *desc)
 	case UCLASS_PVBLOCK:
 		puts("PV BLOCK");
 		break;
+	case UCLASS_RKMTD:
+		puts("RKMTD");
+		break;
 	case UCLASS_VIRTIO:
 		puts("VirtIO");
 		break;
drivers/block/blk-uclass.c +1 -0
index f126547cc7e6..30ad5bbb0024 100644
@@ -36,6 +36,7 @@ static struct {
 	{ UCLASS_VIRTIO, "virtio" },
 	{ UCLASS_PVBLOCK, "pvblock" },
 	{ UCLASS_BLKMAP, "blkmap" },
+	{ UCLASS_RKMTD, "rkmtd" },
 };
 
 static enum uclass_id uclass_name_to_iftype(const char *uclass_idname)
include/dm/uclass-id.h +1 -0
index 0432c95c9edc..2fc672df0a3a 100644
@@ -120,6 +120,7 @@ enum uclass_id {
 	UCLASS_REGULATOR,	/* Regulator device */
 	UCLASS_REMOTEPROC,	/* Remote Processor device */
 	UCLASS_RESET,		/* Reset controller device */
+	UCLASS_RKMTD,		/* Rockchip MTD device */
 	UCLASS_RNG,		/* Random Number Generator */
 	UCLASS_RTC,		/* Real time clock device */
 	UCLASS_SCMI_AGENT,	/* Interface with an SCMI server */

--
2.39.2

Reviewed-by on v5 2/8
#3 lore raw

Re: [PATCH v5 2/8] rockchip: dm: prepare rkmtd UCLASS

Quoted 11 lines · Johan Jonker

Prepare a rkmtd UCLASS in use for writing Rockchip boot blocks in combination with existing userspace tools and rockusb command.

---
 disk/part.c                | 4 ++++
 drivers/block/blk-uclass.c | 1 +
 include/dm/uclass-id.h     | 1 +
 3 files changed, 6 insertions(+)
#4 lore raw

PATCH v5 3/8 rockchip: block: add rkmtd class and drivers

In reply to Johan Jonker

Add rkmtd class and drivers to create a virtual block device to transfer Rockchip boot block data to and from NAND with block orientated tools like "ums" and "rockusb".

---

Changed V5:
Use devres_alloc in bind
Restyle

Changed V4:
Sort includes
Replace constant by define

Changed V3:
New patch
Split driver from command
Split header
Use devm_kzalloc
Remove out of memory debug
Restyle

---
 drivers/block/Kconfig  |    7 +
 drivers/block/Makefile |    2 +
 drivers/block/rkmtd.c  | 1152 ++++++++++++++++++++++++++++++++++++++++
 include/rkmtd.h        |  191 +++++++
 4 files changed, 1352 insertions(+)
 create mode 100644 drivers/block/rkmtd.c
 create mode 100644 include/rkmtd.h
drivers/block/Kconfig +7 -0
index 1abea3f10db4..048a6caef00f 100644
@@ -262,3 +262,10 @@ config SYS_64BIT_LBA
 	help
 	  Make the block subsystem use 64bit sector addresses, rather than the
 	  default of 32bit.
+
+config RKMTD
+	bool "Rockchip rkmtd virtual block device"
+	help
+	  Enable "rkmtd" class and driver to create a virtual block device
+	  to transfer Rockchip boot block data to and from NAND with block
+	  orientate tools like "ums" and "rockusb".
drivers/block/Makefile +2 -0
index a161d145fd39..fdcba5c8318f 100644
@@ -11,6 +11,7 @@ endif
 
 ifndef CONFIG_SPL_BUILD
 obj-$(CONFIG_IDE) += ide.o
+obj-$(CONFIG_RKMTD) += rkmtd.o
 endif
 obj-$(CONFIG_SANDBOX) += sandbox.o host-uclass.o host_dev.o
 obj-$(CONFIG_$(SPL_TPL_)BLOCK_CACHE) += blkcache.o
@@ -19,3 +20,4 @@ obj-$(CONFIG_BLKMAP) += blkmap.o
 obj-$(CONFIG_EFI_MEDIA) += efi-media-uclass.o
 obj-$(CONFIG_EFI_MEDIA_SANDBOX) += sb_efi_media.o
 obj-$(CONFIG_EFI_MEDIA_BLK) += efi_blk.o
+
drivers/block/rkmtd.c new file +1152 -0

1,152 lines not on this page · raw message

include/rkmtd.h new file +191 -0

191 lines not on this page · raw message

--
2.39.2

#5 lore raw

PATCH v5 4/8 rockchip: block: blk-uclass: add bounce buffer flag to blk_desc

In reply to Johan Jonker

Currently bounce buffer support is enabled for all block devices when available. Add a flag to blk_desc to enable only on demand.

---

Changed V5:
New patch

---
 drivers/block/blk-uclass.c | 4 ++--
 drivers/scsi/scsi.c        | 4 ++++
 include/blk.h              | 1 +
 3 files changed, 7 insertions(+), 2 deletions(-)
drivers/block/blk-uclass.c +2 -2
index 30ad5bbb0024..77066da352a3 100644
@@ -441,7 +441,7 @@ long blk_read(struct udevice *dev, lbaint_t start, lbaint_t blkcnt, void *buf)
 			  start, blkcnt, desc->blksz, buf))
 		return blkcnt;
 
-	if (IS_ENABLED(CONFIG_BOUNCE_BUFFER)) {
+	if (IS_ENABLED(CONFIG_BOUNCE_BUFFER) && desc->bb) {
 		struct blk_bounce_buffer bbstate = { .dev = dev };
 		int ret;
 
@@ -478,7 +478,7 @@ long blk_write(struct udevice *dev, lbaint_t start, lbaint_t blkcnt,
 
 	blkcache_invalidate(desc->uclass_id, desc->devnum);
 
-	if (IS_ENABLED(CONFIG_BOUNCE_BUFFER)) {
+	if (IS_ENABLED(CONFIG_BOUNCE_BUFFER) && desc->bb) {
 		struct blk_bounce_buffer bbstate = { .dev = dev };
 		int ret;
 
drivers/scsi/scsi.c +4 -0
index 7411660d465e..779a34bd2f1c 100644
@@ -459,6 +459,9 @@ static void scsi_init_dev_desc_priv(struct blk_desc *dev_desc)
 	dev_desc->product[0] = 0;
 	dev_desc->revision[0] = 0;
 	dev_desc->removable = false;
+#if IS_ENABLED(CONFIG_BOUNCE_BUFFER)
+	dev_desc->bb = true;
+#endif	/* CONFIG_BOUNCE_BUFFER */
 }
 
 #if !defined(CONFIG_DM_SCSI)
@@ -606,6 +609,7 @@ static int do_scsi_scan_one(struct udevice *dev, int id, int lun, bool verbose)
 	bdesc->lun = lun;
 	bdesc->removable = bd.removable;
 	bdesc->type = bd.type;
+	bdesc->bb = bd.bb;
 	memcpy(&bdesc->vendor, &bd.vendor, sizeof(bd.vendor));
 	memcpy(&bdesc->product, &bd.product, sizeof(bd.product));
 	memcpy(&bdesc->revision, &bd.revision,	sizeof(bd.revision));
include/blk.h +1 -0
index 76bd5baf9959..7c7cf7f2b102 100644
@@ -68,6 +68,7 @@ struct blk_desc {
 	/* device can use 48bit addr (ATA/ATAPI v7) */
 	bool	lba48;
 	unsigned char	atapi;		/* Use ATAPI protocol */
+	unsigned char	bb;		/* Use bounce buffer */
 	lbaint_t	lba;		/* number of blocks */
 	unsigned long	blksz;		/* block size */
 	int		log2blksz;	/* for convenience: log2(blksz) */

--
2.39.2

Reviewed-by on v5 4/8
#6 lore raw

Re: [PATCH v5 4/8] rockchip: block: blk-uclass: add bounce buffer flag to blk_desc

Quoted 7 lines · Johan Jonker

Currently bounce buffer support is enabled for all block devices when available. Add a flag to blk_desc to enable only on demand.

---

Quoted 7 lines

Changed V5:
New patch

---
 drivers/block/blk-uclass.c | 4 ++--
 drivers/scsi/scsi.c        | 4 ++++
 include/blk.h              | 1 +
 3 files changed, 7 insertions(+), 2 deletions(-)
Reviewed-by on v5 4/8
#7 lore raw

Re: [PATCH v5 4/8] rockchip: block: blk-uclass: add bounce buffer flag to blk_desc

In reply to Johan Jonker
Quoted 4 lines · Johan Jonker

Currently bounce buffer support is enabled for all block devices when available. Add a flag to blk_desc to enable only on demand.

Thanks,
- Kever

Quoted 69 lines

---

Changed V5:
New patch

---
  drivers/block/blk-uclass.c | 4 ++--
  drivers/scsi/scsi.c        | 4 ++++
  include/blk.h              | 1 +
  3 files changed, 7 insertions(+), 2 deletions(-)
drivers/block/blk-uclass.c +2 -2
index 30ad5bbb0024..77066da352a3 100644
@@ -441,7 +441,7 @@ long blk_read(struct udevice *dev, lbaint_t start, lbaint_t blkcnt, void *buf)
  			  start, blkcnt, desc->blksz, buf))
  		return blkcnt;
 
-	if (IS_ENABLED(CONFIG_BOUNCE_BUFFER)) {
+	if (IS_ENABLED(CONFIG_BOUNCE_BUFFER) && desc->bb) {
  		struct blk_bounce_buffer bbstate = { .dev = dev };
  		int ret;
 
@@ -478,7 +478,7 @@ long blk_write(struct udevice *dev, lbaint_t start, lbaint_t blkcnt,
 
  	blkcache_invalidate(desc->uclass_id, desc->devnum);
 
-	if (IS_ENABLED(CONFIG_BOUNCE_BUFFER)) {
+	if (IS_ENABLED(CONFIG_BOUNCE_BUFFER) && desc->bb) {
  		struct blk_bounce_buffer bbstate = { .dev = dev };
  		int ret;
 
drivers/scsi/scsi.c +4 -0
index 7411660d465e..779a34bd2f1c 100644
@@ -459,6 +459,9 @@ static void scsi_init_dev_desc_priv(struct blk_desc *dev_desc)
  	dev_desc->product[0] = 0;
  	dev_desc->revision[0] = 0;
  	dev_desc->removable = false;
+#if IS_ENABLED(CONFIG_BOUNCE_BUFFER)
+	dev_desc->bb = true;
+#endif	/* CONFIG_BOUNCE_BUFFER */
  }
 
  #if !defined(CONFIG_DM_SCSI)
@@ -606,6 +609,7 @@ static int do_scsi_scan_one(struct udevice *dev, int id, int lun, bool verbose)
  	bdesc->lun = lun;
  	bdesc->removable = bd.removable;
  	bdesc->type = bd.type;
+	bdesc->bb = bd.bb;
  	memcpy(&bdesc->vendor, &bd.vendor, sizeof(bd.vendor));
  	memcpy(&bdesc->product, &bd.product, sizeof(bd.product));
  	memcpy(&bdesc->revision, &bd.revision,	sizeof(bd.revision));
include/blk.h +1 -1
index 76bd5baf9959..7c7cf7f2b102 100644
@@ -68,6 +68,7 @@ struct blk_desc {
  	/* device can use 48bit addr (ATA/ATAPI v7) */
  	bool	lba48;
  	unsigned char	atapi;		/* Use ATAPI protocol */
+	unsigned char	bb;		/* Use bounce buffer */
  	lbaint_t	lba;		/* number of blocks */
  	unsigned long	blksz;		/* block size */
  	int		log2blksz;	/* for convenience: log2(blksz) */
--

2.39.2

#8 lore raw

PATCH v5 5/8 rockchip: cmd: add rkmtd command

In reply to Johan Jonker

The command rkmtd creates a virtual block device to transfer Rockchip boot block data to and from NAND with block orientated tools like "ums" and "rockusb".

It uses the Rockchip MTD driver to scan for boot blocks and copies data from the first block in a GPT formated virtual disk. Data must be written in U-boot "idbloader.img" format and start at partition "loader1" offset 64. The data header is parsed for length and offset. When the last sector is received it erases up to 5 erase blocks on NAND and writes bootblocks in a pattern depending on the NAND ID. Data is then verified. When a block turns out bad the block header is discarded.

---

Changed V4:
Sort includes

Changed V3:
Split driver from command
Split header
Restyle

---
 cmd/Kconfig  |   8 ++
 cmd/Makefile |   1 +
 cmd/rkmtd.c  | 204 +++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 213 insertions(+)
 create mode 100644 cmd/rkmtd.c
cmd/Kconfig +8 -0
index 6470b138d2f8..1979c6c62aa7 100644
@@ -1561,6 +1561,14 @@ config CMD_USB_SDP
 	  Enables the command "sdp" which is used to have U-Boot emulating the
 	  Serial Download Protocol (SDP) via USB.
 
+config CMD_RKMTD
+	bool "rkmtd"
+	select RKMTD
+	help
+	  Enable the command "rkmtd" to create a virtual block device to transfer
+	  Rockchip boot block data to and from NAND with block orientated tools
+	  like "ums" and "rockusb".
+
 config CMD_ROCKUSB
 	bool "rockusb"
 	depends on USB_FUNCTION_ROCKUSB
cmd/Makefile +1 -0
index 9bebf321c397..11927a5904e6 100644
@@ -151,6 +151,7 @@ obj-$(CONFIG_CMD_REISER) += reiser.o
 obj-$(CONFIG_CMD_REMOTEPROC) += remoteproc.o
 obj-$(CONFIG_CMD_RNG) += rng.o
 obj-$(CONFIG_CMD_KASLRSEED) += kaslrseed.o
+obj-$(CONFIG_CMD_RKMTD) += rkmtd.o
 obj-$(CONFIG_CMD_ROCKUSB) += rockusb.o
 obj-$(CONFIG_CMD_RTC) += rtc.o
 obj-$(CONFIG_SANDBOX) += host.o
cmd/rkmtd.c new file +204 -0

204 lines not on this page · raw message

--
2.39.2

#9 lore raw

PATCH v5 6/8 rockchip: test: dm: add rkmtd test

In reply to Johan Jonker

Add Rockchip rkmtd test:
Create/attach/detach RKMTD device. Send/read data with Rockchip boot block header. Test that reusing the same label should work. Basic test of 'rkmtd' commands.

---

Changed V4:
sort includes
rename define
use '\0'
use UT_TESTF_CONSOLE_REC flag
check RC4 result

Changed V3:
New patch

---
 test/dm/Makefile |   1 +
 test/dm/rkmtd.c  | 200 +++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 201 insertions(+)
 create mode 100644 test/dm/rkmtd.c
test/dm/Makefile +1 -0
index 7ed00733c1a6..a5e0a2744375 100644
@@ -100,6 +100,7 @@ obj-$(CONFIG_REMOTEPROC) += remoteproc.o
 obj-$(CONFIG_DM_RESET) += reset.o
 obj-$(CONFIG_SYSRESET) += sysreset.o
 obj-$(CONFIG_DM_REGULATOR) += regulator.o
+obj-$(CONFIG_CMD_RKMTD) += rkmtd.o
 obj-$(CONFIG_DM_RNG) += rng.o
 obj-$(CONFIG_DM_RTC) += rtc.o
 obj-$(CONFIG_SCMI_FIRMWARE) += scmi.o
test/dm/rkmtd.c new file +200 -0

200 lines not on this page · raw message

--
2.39.2

#10 lore raw

PATCH v5 7/8 rockchip: doc: add rkmtd.rst

In reply to Johan Jonker

Add documention for Rockchip rkmtd virtual block device.

---

Changed V3:
New patch

---
 doc/board/rockchip/index.rst |   1 +
 doc/board/rockchip/rkmtd.rst | 105 +++++++++++++++++++++++++++++++++++
 2 files changed, 106 insertions(+)
 create mode 100644 doc/board/rockchip/rkmtd.rst
doc/board/rockchip/index.rst +1 -0
index 0c377e9bbba0..9a87a035e95e 100644
@@ -8,3 +8,4 @@ Rockchip
    :maxdepth: 2
 
    rockchip
+   rkmtd
doc/board/rockchip/rkmtd.rst new file +105 -0

105 lines not on this page · raw message

--
2.39.2

#11 lore raw

PATCH v5 8/8 rockchip: configs: sandbox: enable rkmtd command

In reply to Johan Jonker

Enable rkmtd command for testing with sandbox_defconfig and sandbox64_defconfig.

---

Changed V3:
New patch

---
 configs/sandbox64_defconfig | 1 +
 configs/sandbox_defconfig   | 1 +
 2 files changed, 2 insertions(+)
configs/sandbox64_defconfig +1 -0
index 1a033b22018b..1a01f51a0b75 100644
@@ -58,6 +58,7 @@ CONFIG_CMD_REMOTEPROC=y
 CONFIG_CMD_SPI=y
 CONFIG_CMD_TEMPERATURE=y
 CONFIG_CMD_USB=y
+CONFIG_CMD_RKMTD=y
 CONFIG_CMD_WDT=y
 CONFIG_CMD_WRITE=y
 CONFIG_CMD_CAT=y
configs/sandbox_defconfig +1 -0
index 01830c7bd255..cc54e6dec5af 100644
@@ -84,6 +84,7 @@ CONFIG_CMD_REMOTEPROC=y
 CONFIG_CMD_SPI=y
 CONFIG_CMD_TEMPERATURE=y
 CONFIG_CMD_USB=y
+CONFIG_CMD_RKMTD=y
 CONFIG_CMD_WDT=y
 CONFIG_CMD_WRITE=y
 CONFIG_CMD_AXI=y

--
2.39.2