1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2008-2011 Freescale Semiconductor, Inc.
9 #include <asm/global_data.h>
13 #include <env_internal.h>
15 #include <linux/stddef.h>
22 #include <dm/ofnode.h>
25 #define STR(X) __STR(X)
27 DECLARE_GLOBAL_DATA_PTR;
29 #if CONFIG_IS_ENABLED(OF_CONTROL)
30 static inline int mmc_offset_try_partition(const char *str, int copy, s64 *val)
32 struct disk_partition info;
33 struct blk_desc *desc;
37 snprintf(dev_str, sizeof(dev_str), "%d", mmc_get_env_dev());
38 ret = blk_get_device_by_str("mmc", dev_str, &desc);
43 ret = part_get_info(desc, i, &info);
47 if (!strncmp((const char *)info.name, str, sizeof(info.name)))
51 /* round up to info.blksz */
52 len = DIV_ROUND_UP(CONFIG_ENV_SIZE, info.blksz);
54 /* use the top of the partion for the environment */
55 *val = (info.start + info.size - (1 + copy) * len) * info.blksz;
60 static inline s64 mmc_offset(int copy)
63 const char *offset_redund;
64 const char *partition;
67 .offset_redund = "u-boot,mmc-env-offset-redundant",
68 .partition = "u-boot,mmc-env-partition",
69 .offset = "u-boot,mmc-env-offset",
71 s64 val = 0, defvalue;
76 /* look for the partition in mmc CONFIG_SYS_MMC_ENV_DEV */
77 str = ofnode_conf_read_str(dt_prop.partition);
79 /* try to place the environment at end of the partition */
80 err = mmc_offset_try_partition(str, copy, &val);
85 defvalue = CONFIG_ENV_OFFSET;
86 propname = dt_prop.offset;
88 #if defined(CONFIG_ENV_OFFSET_REDUND)
90 defvalue = CONFIG_ENV_OFFSET_REDUND;
91 propname = dt_prop.offset_redund;
94 return ofnode_conf_read_int(propname, defvalue);
97 static inline s64 mmc_offset(int copy)
99 s64 offset = CONFIG_ENV_OFFSET;
101 #if defined(CONFIG_ENV_OFFSET_REDUND)
103 offset = CONFIG_ENV_OFFSET_REDUND;
109 __weak int mmc_get_env_addr(struct mmc *mmc, int copy, u32 *env_addr)
111 s64 offset = mmc_offset(copy);
114 offset += mmc->capacity;
121 #ifdef CONFIG_SYS_MMC_ENV_PART
122 __weak uint mmc_get_env_part(struct mmc *mmc)
124 return CONFIG_SYS_MMC_ENV_PART;
127 static unsigned char env_mmc_orig_hwpart;
129 static int mmc_set_env_part(struct mmc *mmc)
131 uint part = mmc_get_env_part(mmc);
132 int dev = mmc_get_env_dev();
135 env_mmc_orig_hwpart = mmc_get_blk_desc(mmc)->hwpart;
136 ret = blk_select_hwpart_devnum(IF_TYPE_MMC, dev, part);
138 puts("MMC partition switch failed\n");
143 static inline int mmc_set_env_part(struct mmc *mmc) {return 0; };
146 static const char *init_mmc_for_env(struct mmc *mmc)
149 return "No MMC card found";
151 #if CONFIG_IS_ENABLED(BLK)
154 if (blk_get_from_parent(mmc->dev, &dev))
155 return "No block device";
158 return "MMC init failed";
160 if (mmc_set_env_part(mmc))
161 return "MMC partition switch failed";
166 static void fini_mmc_for_env(struct mmc *mmc)
168 #ifdef CONFIG_SYS_MMC_ENV_PART
169 int dev = mmc_get_env_dev();
171 blk_select_hwpart_devnum(IF_TYPE_MMC, dev, env_mmc_orig_hwpart);
175 #if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_SPL_BUILD)
176 static inline int write_env(struct mmc *mmc, unsigned long size,
177 unsigned long offset, const void *buffer)
179 uint blk_start, blk_cnt, n;
180 struct blk_desc *desc = mmc_get_blk_desc(mmc);
182 blk_start = ALIGN(offset, mmc->write_bl_len) / mmc->write_bl_len;
183 blk_cnt = ALIGN(size, mmc->write_bl_len) / mmc->write_bl_len;
185 n = blk_dwrite(desc, blk_start, blk_cnt, (u_char *)buffer);
187 return (n == blk_cnt) ? 0 : -1;
190 static int env_mmc_save(void)
192 ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
193 int dev = mmc_get_env_dev();
194 struct mmc *mmc = find_mmc_device(dev);
199 errmsg = init_mmc_for_env(mmc);
201 printf("%s\n", errmsg);
205 ret = env_export(env_new);
209 #ifdef CONFIG_ENV_OFFSET_REDUND
210 if (gd->env_valid == ENV_VALID)
214 if (mmc_get_env_addr(mmc, copy, &offset)) {
219 printf("Writing to %sMMC(%d)... ", copy ? "redundant " : "", dev);
220 if (write_env(mmc, CONFIG_ENV_SIZE, offset, (u_char *)env_new)) {
228 #ifdef CONFIG_ENV_OFFSET_REDUND
229 gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND;
233 fini_mmc_for_env(mmc);
237 static inline int erase_env(struct mmc *mmc, unsigned long size,
238 unsigned long offset)
240 uint blk_start, blk_cnt, n;
241 struct blk_desc *desc = mmc_get_blk_desc(mmc);
243 blk_start = ALIGN(offset, mmc->write_bl_len) / mmc->write_bl_len;
244 blk_cnt = ALIGN(size, mmc->write_bl_len) / mmc->write_bl_len;
246 n = blk_derase(desc, blk_start, blk_cnt);
247 printf("%d blocks erased: %s\n", n, (n == blk_cnt) ? "OK" : "ERROR");
249 return (n == blk_cnt) ? 0 : 1;
252 static int env_mmc_erase(void)
254 int dev = mmc_get_env_dev();
255 struct mmc *mmc = find_mmc_device(dev);
260 errmsg = init_mmc_for_env(mmc);
262 printf("%s\n", errmsg);
266 if (mmc_get_env_addr(mmc, copy, &offset)) {
267 ret = CMD_RET_FAILURE;
271 ret = erase_env(mmc, CONFIG_ENV_SIZE, offset);
273 #ifdef CONFIG_ENV_OFFSET_REDUND
276 if (mmc_get_env_addr(mmc, copy, &offset)) {
277 ret = CMD_RET_FAILURE;
281 ret |= erase_env(mmc, CONFIG_ENV_SIZE, offset);
285 fini_mmc_for_env(mmc);
288 #endif /* CONFIG_CMD_SAVEENV && !CONFIG_SPL_BUILD */
290 static inline int read_env(struct mmc *mmc, unsigned long size,
291 unsigned long offset, const void *buffer)
293 uint blk_start, blk_cnt, n;
294 struct blk_desc *desc = mmc_get_blk_desc(mmc);
296 blk_start = ALIGN(offset, mmc->read_bl_len) / mmc->read_bl_len;
297 blk_cnt = ALIGN(size, mmc->read_bl_len) / mmc->read_bl_len;
299 n = blk_dread(desc, blk_start, blk_cnt, (uchar *)buffer);
301 return (n == blk_cnt) ? 0 : -1;
304 #ifdef CONFIG_ENV_OFFSET_REDUND
305 static int env_mmc_load(void)
307 #if !defined(ENV_IS_EMBEDDED)
309 u32 offset1, offset2;
310 int read1_fail = 0, read2_fail = 0;
312 int dev = mmc_get_env_dev();
313 const char *errmsg = NULL;
315 ALLOC_CACHE_ALIGN_BUFFER(env_t, tmp_env1, 1);
316 ALLOC_CACHE_ALIGN_BUFFER(env_t, tmp_env2, 1);
318 mmc_initialize(NULL);
320 mmc = find_mmc_device(dev);
322 errmsg = init_mmc_for_env(mmc);
328 if (mmc_get_env_addr(mmc, 0, &offset1) ||
329 mmc_get_env_addr(mmc, 1, &offset2)) {
334 read1_fail = read_env(mmc, CONFIG_ENV_SIZE, offset1, tmp_env1);
335 read2_fail = read_env(mmc, CONFIG_ENV_SIZE, offset2, tmp_env2);
337 ret = env_import_redund((char *)tmp_env1, read1_fail, (char *)tmp_env2,
338 read2_fail, H_EXTERNAL);
341 fini_mmc_for_env(mmc);
344 env_set_default(errmsg, 0);
349 #else /* ! CONFIG_ENV_OFFSET_REDUND */
350 static int env_mmc_load(void)
352 #if !defined(ENV_IS_EMBEDDED)
353 ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
357 int dev = mmc_get_env_dev();
361 mmc = find_mmc_device(dev);
363 errmsg = init_mmc_for_env(mmc);
369 if (mmc_get_env_addr(mmc, 0, &offset)) {
374 if (read_env(mmc, CONFIG_ENV_SIZE, offset, buf)) {
375 errmsg = "!read failed";
380 ret = env_import(buf, 1, H_EXTERNAL);
383 gd->env_addr = (ulong)&ep->data;
387 fini_mmc_for_env(mmc);
390 env_set_default(errmsg, 0);
394 #endif /* CONFIG_ENV_OFFSET_REDUND */
396 U_BOOT_ENV_LOCATION(mmc) = {
397 .location = ENVL_MMC,
399 .load = env_mmc_load,
400 #ifndef CONFIG_SPL_BUILD
401 .save = env_save_ptr(env_mmc_save),
402 .erase = ENV_ERASE_PTR(env_mmc_erase)