2 * (C) Copyright 2010 DENX Software Engineering
3 * Wolfgang Denk <wd@denx.de>
5 * (C) Copyright 2005-2009 Samsung Electronics
6 * Kyungmin Park <kyungmin.park@samsung.com>
8 * SPDX-License-Identifier: GPL-2.0+
13 #include <environment.h>
14 #include <linux/stddef.h>
18 #include <onenand_uboot.h>
20 #include <linux/compat.h>
21 #include <linux/mtd/mtd.h>
22 #include <linux/mtd/onenand.h>
24 char *env_name_spec = "OneNAND";
26 #define ONENAND_MAX_ENV_SIZE CONFIG_ENV_SIZE
27 #define ONENAND_ENV_SIZE(mtd) (ONENAND_MAX_ENV_SIZE - ENV_HEADER_SIZE)
29 DECLARE_GLOBAL_DATA_PTR;
31 static void env_onenand_load(void)
33 struct mtd_info *mtd = &onenand_mtd;
34 #ifdef CONFIG_ENV_ADDR_FLEX
35 struct onenand_chip *this = &onenand_chip;
39 #ifdef ENV_IS_EMBEDDED
40 char *buf = (char *)&environment;
42 loff_t env_addr = CONFIG_ENV_ADDR;
43 char onenand_env[ONENAND_MAX_ENV_SIZE];
44 char *buf = (char *)&onenand_env[0];
45 #endif /* ENV_IS_EMBEDDED */
47 #ifndef ENV_IS_EMBEDDED
48 # ifdef CONFIG_ENV_ADDR_FLEX
49 if (FLEXONENAND(this))
50 env_addr = CONFIG_ENV_ADDR_FLEX;
52 /* Check OneNAND exist */
54 /* Ignore read fail */
55 mtd_read(mtd, env_addr, ONENAND_MAX_ENV_SIZE,
56 &retlen, (u_char *)buf);
58 mtd->writesize = MAX_ONENAND_PAGESIZE;
59 #endif /* !ENV_IS_EMBEDDED */
61 rc = env_import(buf, 1);
63 gd->env_valid = ENV_VALID;
66 static int env_onenand_save(void)
70 struct mtd_info *mtd = &onenand_mtd;
71 #ifdef CONFIG_ENV_ADDR_FLEX
72 struct onenand_chip *this = &onenand_chip;
74 loff_t env_addr = CONFIG_ENV_ADDR;
76 struct erase_info instr = {
80 ret = env_export(&env_new);
84 instr.len = CONFIG_ENV_SIZE;
85 #ifdef CONFIG_ENV_ADDR_FLEX
86 if (FLEXONENAND(this)) {
87 env_addr = CONFIG_ENV_ADDR_FLEX;
88 instr.len = CONFIG_ENV_SIZE_FLEX;
89 instr.len <<= onenand_mtd.eraseregions[0].numblocks == 1 ?
93 instr.addr = env_addr;
95 if (mtd_erase(mtd, &instr)) {
96 printf("OneNAND: erase failed at 0x%08llx\n", env_addr);
100 if (mtd_write(mtd, env_addr, ONENAND_MAX_ENV_SIZE, &retlen,
101 (u_char *)&env_new)) {
102 printf("OneNAND: write failed at 0x%llx\n", instr.addr);
109 U_BOOT_ENV_LOCATION(onenand) = {
110 .location = ENVL_ONENAND,
111 .load = env_onenand_load,
112 .save = env_save_ptr(env_onenand_save),