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