]> git.ipfire.org Git - people/ms/u-boot.git/blame - env/onenand.c
Merge tag 'signed-efi-next' of git://github.com/agraf/u-boot
[people/ms/u-boot.git] / env / onenand.c
CommitLineData
d7e8ce10 1/*
ea882baf
WD
2 * (C) Copyright 2010 DENX Software Engineering
3 * Wolfgang Denk <wd@denx.de>
4 *
937076f8 5 * (C) Copyright 2005-2009 Samsung Electronics
d7e8ce10
KP
6 * Kyungmin Park <kyungmin.park@samsung.com>
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
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
27DECLARE_GLOBAL_DATA_PTR;
28
c5951991 29static 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
ea882baf
WD
59 rc = env_import(buf, 1);
60 if (rc)
203e94f6 61 gd->env_valid = ENV_VALID;
c5951991
SG
62
63 return rc ? 0 : -EIO;
d7e8ce10
KP
64}
65
e5bce247 66static 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
109U_BOOT_ENV_LOCATION(onenand) = {
110 .location = ENVL_ONENAND,
ac358beb 111 ENV_NAME("OneNAND")
e5bce247
SG
112 .load = env_onenand_load,
113 .save = env_save_ptr(env_onenand_save),
4415f1d1 114};