]>
Commit | Line | Data |
---|---|---|
83d290c5 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
d7e8ce10 | 2 | /* |
ea882baf WD |
3 | * (C) Copyright 2010 DENX Software Engineering |
4 | * Wolfgang Denk <[email protected]> | |
5 | * | |
937076f8 | 6 | * (C) Copyright 2005-2009 Samsung Electronics |
d7e8ce10 | 7 | * Kyungmin Park <[email protected]> |
d7e8ce10 KP |
8 | */ |
9 | ||
10 | #include <common.h> | |
d7e8ce10 | 11 | #include <command.h> |
f3998fdc | 12 | #include <env_internal.h> |
401d1c4f | 13 | #include <asm/global_data.h> |
d7e8ce10 KP |
14 | #include <linux/stddef.h> |
15 | #include <malloc.h> | |
ea882baf WD |
16 | #include <search.h> |
17 | #include <errno.h> | |
b919ec25 | 18 | #include <onenand_uboot.h> |
d7e8ce10 | 19 | |
7b15e2bb | 20 | #include <linux/compat.h> |
d7e8ce10 KP |
21 | #include <linux/mtd/mtd.h> |
22 | #include <linux/mtd/onenand.h> | |
23 | ||
034afbcc | 24 | #define ONENAND_MAX_ENV_SIZE CONFIG_ENV_SIZE |
937076f8 KP |
25 | #define ONENAND_ENV_SIZE(mtd) (ONENAND_MAX_ENV_SIZE - ENV_HEADER_SIZE) |
26 | ||
a9da2b41 KP |
27 | DECLARE_GLOBAL_DATA_PTR; |
28 | ||
c5951991 | 29 | static int env_onenand_load(void) |
d7e8ce10 | 30 | { |
937076f8 | 31 | struct mtd_info *mtd = &onenand_mtd; |
b821cead | 32 | #ifdef CONFIG_ENV_ADDR_FLEX |
c758e947 | 33 | struct onenand_chip *this = &onenand_chip; |
b821cead | 34 | #endif |
ea882baf | 35 | int rc; |
2ae64f51 | 36 | size_t retlen; |
ea882baf | 37 | #ifdef ENV_IS_EMBEDDED |
994bc671 | 38 | char *buf = (char *)&environment; |
ea882baf WD |
39 | #else |
40 | loff_t env_addr = CONFIG_ENV_ADDR; | |
cd0f4fa1 TR |
41 | char onenand_env[ONENAND_MAX_ENV_SIZE]; |
42 | char *buf = (char *)&onenand_env[0]; | |
ea882baf | 43 | #endif /* ENV_IS_EMBEDDED */ |
d7e8ce10 | 44 | |
ea882baf WD |
45 | #ifndef ENV_IS_EMBEDDED |
46 | # ifdef CONFIG_ENV_ADDR_FLEX | |
c758e947 AKS |
47 | if (FLEXONENAND(this)) |
48 | env_addr = CONFIG_ENV_ADDR_FLEX; | |
ea882baf | 49 | # endif |
d7e8ce10 | 50 | /* Check OneNAND exist */ |
937076f8 | 51 | if (mtd->writesize) |
d7e8ce10 | 52 | /* Ignore read fail */ |
dfe64e2c | 53 | mtd_read(mtd, env_addr, ONENAND_MAX_ENV_SIZE, |
b919ec25 | 54 | &retlen, (u_char *)buf); |
d7e8ce10 | 55 | else |
937076f8 | 56 | mtd->writesize = MAX_ONENAND_PAGESIZE; |
ea882baf | 57 | #endif /* !ENV_IS_EMBEDDED */ |
d7e8ce10 | 58 | |
890feeca | 59 | rc = env_import(buf, 1, H_EXTERNAL); |
42a1820b | 60 | if (!rc) |
203e94f6 | 61 | gd->env_valid = ENV_VALID; |
c5951991 | 62 | |
42a1820b | 63 | return rc; |
d7e8ce10 KP |
64 | } |
65 | ||
e5bce247 | 66 | static int env_onenand_save(void) |
d7e8ce10 | 67 | { |
cd0f4fa1 | 68 | env_t env_new; |
7ce1526e | 69 | int ret; |
937076f8 | 70 | struct mtd_info *mtd = &onenand_mtd; |
b821cead | 71 | #ifdef CONFIG_ENV_ADDR_FLEX |
c758e947 | 72 | struct onenand_chip *this = &onenand_chip; |
b821cead | 73 | #endif |
ea882baf WD |
74 | loff_t env_addr = CONFIG_ENV_ADDR; |
75 | size_t retlen; | |
0d1ecc99 | 76 | struct erase_info instr = {}; |
ea882baf | 77 | |
7ce1526e MV |
78 | ret = env_export(&env_new); |
79 | if (ret) | |
80 | return ret; | |
d7e8ce10 | 81 | |
0e8d1586 | 82 | instr.len = CONFIG_ENV_SIZE; |
b821cead | 83 | #ifdef CONFIG_ENV_ADDR_FLEX |
c758e947 AKS |
84 | if (FLEXONENAND(this)) { |
85 | env_addr = CONFIG_ENV_ADDR_FLEX; | |
86 | instr.len = CONFIG_ENV_SIZE_FLEX; | |
87 | instr.len <<= onenand_mtd.eraseregions[0].numblocks == 1 ? | |
88 | 1 : 0; | |
89 | } | |
b821cead | 90 | #endif |
d7e8ce10 | 91 | instr.addr = env_addr; |
937076f8 | 92 | instr.mtd = mtd; |
dfe64e2c | 93 | if (mtd_erase(mtd, &instr)) { |
48287792 | 94 | printf("OneNAND: erase failed at 0x%08llx\n", env_addr); |
d7e8ce10 KP |
95 | return 1; |
96 | } | |
97 | ||
dfe64e2c | 98 | if (mtd_write(mtd, env_addr, ONENAND_MAX_ENV_SIZE, &retlen, |
cd0f4fa1 | 99 | (u_char *)&env_new)) { |
8d2effea | 100 | printf("OneNAND: write failed at 0x%llx\n", instr.addr); |
d7e8ce10 KP |
101 | return 2; |
102 | } | |
103 | ||
104 | return 0; | |
105 | } | |
106 | ||
4415f1d1 SG |
107 | U_BOOT_ENV_LOCATION(onenand) = { |
108 | .location = ENVL_ONENAND, | |
ac358beb | 109 | ENV_NAME("OneNAND") |
e5bce247 SG |
110 | .load = env_onenand_load, |
111 | .save = env_save_ptr(env_onenand_save), | |
4415f1d1 | 112 | }; |