1 From 240d98e6ad0aed3c11236aa40a60bbd6fe01fae5 Mon Sep 17 00:00:00 2001
2 From: Weijie Gao <weijie.gao@mediatek.com>
3 Date: Mon, 25 Jul 2022 10:50:46 +0800
4 Subject: [PATCH 45/71] env: add support for NMBM upper MTD layer
6 Add an env driver for NMBM upper MTD layer
8 Signed-off-by: Weijie Gao <weijie.gao@mediatek.com>
11 env/Kconfig | 19 ++++-
14 env/nmbm.c | 155 +++++++++++++++++++++++++++++++++++++++++
15 include/env_internal.h | 1 +
17 7 files changed, 180 insertions(+), 3 deletions(-)
18 create mode 100644 env/nmbm.c
22 @@ -59,6 +59,7 @@ config ENV_IS_DEFAULT
23 def_bool y if !ENV_IS_IN_EEPROM && !ENV_IS_IN_EXT4 && \
24 !ENV_IS_IN_FAT && !ENV_IS_IN_FLASH && \
25 !ENV_IS_IN_MMC && !ENV_IS_IN_NAND && \
26 + !ENV_IS_IN_NMBM && \
27 !ENV_IS_IN_NVRAM && !ENV_IS_IN_ONENAND && \
28 !ENV_IS_IN_REMOTE && !ENV_IS_IN_SPI_FLASH && \
29 !ENV_IS_IN_UBI && !ENV_IS_IN_MTD
30 @@ -315,6 +316,21 @@ config ENV_RANGE
31 Specifying a range with more erase blocks than are needed to hold
32 CONFIG_ENV_SIZE allows bad blocks within the range to be avoided.
34 +config ENV_IS_IN_NMBM
35 + bool "Environment in a NMBM upper MTD layer"
36 + depends on !CHAIN_OF_TRUST
39 + Define this if you have a NMBM upper MTD which you want to use for
42 + - CONFIG_ENV_OFFSET:
45 + These two #defines specify the offset and size of the environment
46 + area within the first NAND device. CONFIG_ENV_OFFSET must be
47 + aligned to an erase block boundary.
49 config ENV_IS_IN_NVRAM
50 bool "Environment in a non-volatile RAM"
51 depends on !CHAIN_OF_TRUST
52 @@ -591,7 +607,7 @@ config ENV_MTD_NAME
54 hex "Environment offset"
55 depends on ENV_IS_IN_EEPROM || ENV_IS_IN_MMC || ENV_IS_IN_NAND || \
56 - ENV_IS_IN_SPI_FLASH || ENV_IS_IN_MTD
57 + ENV_IS_IN_SPI_FLASH || ENV_IS_IN_NMBM || ENV_IS_IN_MTD
58 default 0x3f8000 if ARCH_ROCKCHIP && ENV_IS_IN_MMC
59 default 0x140000 if ARCH_ROCKCHIP && ENV_IS_IN_SPI_FLASH
60 default 0xF0000 if ARCH_SUNXI
63 @@ -26,6 +26,7 @@ obj-$(CONFIG_$(SPL_TPL_)ENV_IS_IN_FAT) +
64 obj-$(CONFIG_$(SPL_TPL_)ENV_IS_IN_EXT4) += ext4.o
65 obj-$(CONFIG_$(SPL_TPL_)ENV_IS_IN_MTD) += mtd.o
66 obj-$(CONFIG_$(SPL_TPL_)ENV_IS_IN_NAND) += nand.o
67 +obj-$(CONFIG_$(SPL_TPL_)ENV_IS_IN_NMBM) += nmbm.o
68 obj-$(CONFIG_$(SPL_TPL_)ENV_IS_IN_SPI_FLASH) += sf.o
69 obj-$(CONFIG_$(SPL_TPL_)ENV_IS_IN_FLASH) += flash.o
73 @@ -52,6 +52,9 @@ static enum env_location env_locations[]
74 #ifdef CONFIG_ENV_IS_IN_NAND
77 +#ifdef CONFIG_ENV_IS_IN_NMBM
80 #ifdef CONFIG_ENV_IS_IN_NVRAM
86 +/* SPDX-License-Identifier: GPL-2.0 */
88 + * Copyright (C) 2020 MediaTek Inc. All Rights Reserved.
90 + * Author: Weijie Gao <weijie.gao@mediatek.com>
95 +#include <env_internal.h>
97 +#include <linux/kernel.h>
98 +#include <linux/stddef.h>
99 +#include <linux/types.h>
101 +#include <memalign.h>
104 +#include <nmbm/nmbm-mtd.h>
106 +#if defined(CONFIG_CMD_SAVEENV) && defined(CONFIG_NMBM_MTD)
110 +#if defined(ENV_IS_EMBEDDED)
111 +env_t *env_ptr = &environment;
112 +#else /* ! ENV_IS_EMBEDDED */
114 +#endif /* ENV_IS_EMBEDDED */
116 +DECLARE_GLOBAL_DATA_PTR;
118 +static int env_nmbm_init(void)
120 +#if defined(ENV_IS_EMBEDDED)
121 + int crc1_ok = 0, crc2_ok = 0;
124 + tmp_env1 = env_ptr;
125 + crc1_ok = crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc;
127 + if (!crc1_ok && !crc2_ok) {
129 + gd->env_valid = ENV_INVALID;
132 + } else if (crc1_ok && !crc2_ok) {
133 + gd->env_valid = ENV_VALID;
136 + if (gd->env_valid == ENV_VALID)
137 + env_ptr = tmp_env1;
139 + gd->env_addr = (ulong)env_ptr->data;
141 +#else /* ENV_IS_EMBEDDED */
142 + gd->env_addr = (ulong)&default_environment[0];
143 + gd->env_valid = ENV_VALID;
144 +#endif /* ENV_IS_EMBEDDED */
150 +static int env_nmbm_save(void)
152 + ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
153 + struct mtd_info *mtd;
154 + struct erase_info ei;
157 + ret = env_export(env_new);
161 + mtd = nmbm_mtd_get_upper_by_index(0);
165 + printf("Erasing on NMBM...\n");
166 + memset(&ei, 0, sizeof(ei));
169 + ei.addr = CONFIG_ENV_OFFSET;
170 + ei.len = CONFIG_ENV_SIZE;
172 + if (mtd_erase(mtd, &ei))
175 + printf("Writing on NMBM... ");
176 + ret = mtd_write(mtd, CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, NULL,
177 + (u_char *)env_new);
178 + puts(ret ? "FAILED!\n" : "OK\n");
182 +#endif /* CMD_SAVEENV */
184 +static int readenv(size_t offset, u_char *buf)
186 + struct mtd_info *mtd;
187 + struct mtd_oob_ops ops;
189 + size_t len = CONFIG_ENV_SIZE;
191 + mtd = nmbm_mtd_get_upper_by_index(0);
195 + ops.mode = MTD_OPS_AUTO_OOB;
199 + ops.len = min(len, (size_t)mtd->writesize);
202 + ret = mtd_read_oob(mtd, offset, &ops);
206 + buf += mtd->writesize;
207 + len -= mtd->writesize;
208 + offset += mtd->writesize;
214 +static int env_nmbm_load(void)
216 +#if !defined(ENV_IS_EMBEDDED)
217 + ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
220 + ret = readenv(CONFIG_ENV_OFFSET, (u_char *)buf);
222 + env_set_default("readenv() failed", 0);
226 + return env_import(buf, 1, H_EXTERNAL);
227 +#endif /* ! ENV_IS_EMBEDDED */
232 +U_BOOT_ENV_LOCATION(nmbm) = {
233 + .location = ENVL_NMBM,
235 + .load = env_nmbm_load,
236 +#if defined(CMD_SAVEENV)
237 + .save = env_save_ptr(env_nmbm_save),
239 + .init = env_nmbm_init,
241 --- a/include/env_internal.h
242 +++ b/include/env_internal.h
243 @@ -110,6 +110,7 @@ enum env_location {
253 @@ -39,6 +39,7 @@ ENVCRC-$(CONFIG_ENV_IS_IN_FLASH) = y
254 ENVCRC-$(CONFIG_ENV_IS_IN_ONENAND) = y
255 ENVCRC-$(CONFIG_ENV_IS_IN_MTD) = y
256 ENVCRC-$(CONFIG_ENV_IS_IN_NAND) = y
257 +ENVCRC-$(CONFIG_ENV_IS_IN_NMBM) = y
258 ENVCRC-$(CONFIG_ENV_IS_IN_NVRAM) = y
259 ENVCRC-$(CONFIG_ENV_IS_IN_SPI_FLASH) = y
260 BUILD_ENVCRC ?= $(ENVCRC-y)