1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2010 DENX Software Engineering
4 * Wolfgang Denk <wd@denx.de>
6 * (C) Copyright 2005-2009 Samsung Electronics
7 * Kyungmin Park <kyungmin.park@samsung.com>
12 #include <env_internal.h>
13 #include <linux/stddef.h>
17 #include <onenand_uboot.h>
19 #include <linux/compat.h>
20 #include <linux/mtd/mtd.h>
21 #include <linux/mtd/onenand.h>
23 #define ONENAND_MAX_ENV_SIZE CONFIG_ENV_SIZE
24 #define ONENAND_ENV_SIZE(mtd) (ONENAND_MAX_ENV_SIZE - ENV_HEADER_SIZE)
26 DECLARE_GLOBAL_DATA_PTR;
28 static int env_onenand_load(void)
30 struct mtd_info *mtd = &onenand_mtd;
31 #ifdef CONFIG_ENV_ADDR_FLEX
32 struct onenand_chip *this = &onenand_chip;
36 #ifdef ENV_IS_EMBEDDED
37 char *buf = (char *)&environment;
39 loff_t env_addr = CONFIG_ENV_ADDR;
40 char onenand_env[ONENAND_MAX_ENV_SIZE];
41 char *buf = (char *)&onenand_env[0];
42 #endif /* ENV_IS_EMBEDDED */
44 #ifndef ENV_IS_EMBEDDED
45 # ifdef CONFIG_ENV_ADDR_FLEX
46 if (FLEXONENAND(this))
47 env_addr = CONFIG_ENV_ADDR_FLEX;
49 /* Check OneNAND exist */
51 /* Ignore read fail */
52 mtd_read(mtd, env_addr, ONENAND_MAX_ENV_SIZE,
53 &retlen, (u_char *)buf);
55 mtd->writesize = MAX_ONENAND_PAGESIZE;
56 #endif /* !ENV_IS_EMBEDDED */
58 rc = env_import(buf, 1, H_EXTERNAL);
60 gd->env_valid = ENV_VALID;
65 static int env_onenand_save(void)
69 struct mtd_info *mtd = &onenand_mtd;
70 #ifdef CONFIG_ENV_ADDR_FLEX
71 struct onenand_chip *this = &onenand_chip;
73 loff_t env_addr = CONFIG_ENV_ADDR;
75 struct erase_info instr = {
79 ret = env_export(&env_new);
83 instr.len = CONFIG_ENV_SIZE;
84 #ifdef CONFIG_ENV_ADDR_FLEX
85 if (FLEXONENAND(this)) {
86 env_addr = CONFIG_ENV_ADDR_FLEX;
87 instr.len = CONFIG_ENV_SIZE_FLEX;
88 instr.len <<= onenand_mtd.eraseregions[0].numblocks == 1 ?
92 instr.addr = env_addr;
94 if (mtd_erase(mtd, &instr)) {
95 printf("OneNAND: erase failed at 0x%08llx\n", env_addr);
99 if (mtd_write(mtd, env_addr, ONENAND_MAX_ENV_SIZE, &retlen,
100 (u_char *)&env_new)) {
101 printf("OneNAND: write failed at 0x%llx\n", instr.addr);
108 U_BOOT_ENV_LOCATION(onenand) = {
109 .location = ENVL_ONENAND,
111 .load = env_onenand_load,
112 .save = env_save_ptr(env_onenand_save),