]> git.ipfire.org Git - people/ms/u-boot.git/blame - common/env_onenand.c
linux/compat.h: rename from linux/mtd/compat.h
[people/ms/u-boot.git] / common / 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 *
8 * See file CREDITS for list of people who contributed to this
9 * project.
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation; either version 2 of
14 * the License, or (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
24 * MA 02111-1307 USA
25 */
26
27#include <common.h>
d7e8ce10
KP
28#include <command.h>
29#include <environment.h>
30#include <linux/stddef.h>
31#include <malloc.h>
ea882baf
WD
32#include <search.h>
33#include <errno.h>
b919ec25 34#include <onenand_uboot.h>
d7e8ce10 35
7b15e2bb 36#include <linux/compat.h>
d7e8ce10
KP
37#include <linux/mtd/mtd.h>
38#include <linux/mtd/onenand.h>
39
d7e8ce10
KP
40char *env_name_spec = "OneNAND";
41
937076f8
KP
42#define ONENAND_MAX_ENV_SIZE 4096
43#define ONENAND_ENV_SIZE(mtd) (ONENAND_MAX_ENV_SIZE - ENV_HEADER_SIZE)
44
a9da2b41
KP
45DECLARE_GLOBAL_DATA_PTR;
46
d7e8ce10
KP
47void env_relocate_spec(void)
48{
937076f8 49 struct mtd_info *mtd = &onenand_mtd;
b821cead 50#ifdef CONFIG_ENV_ADDR_FLEX
c758e947 51 struct onenand_chip *this = &onenand_chip;
b821cead 52#endif
ea882baf 53 int rc;
2ae64f51 54 size_t retlen;
ea882baf 55#ifdef ENV_IS_EMBEDDED
994bc671 56 char *buf = (char *)&environment;
ea882baf
WD
57#else
58 loff_t env_addr = CONFIG_ENV_ADDR;
59 char onenand_env[ONENAND_MAX_ENV_SIZE];
60 char *buf = (char *)&onenand_env[0];
61#endif /* ENV_IS_EMBEDDED */
d7e8ce10 62
ea882baf
WD
63#ifndef ENV_IS_EMBEDDED
64# ifdef CONFIG_ENV_ADDR_FLEX
c758e947
AKS
65 if (FLEXONENAND(this))
66 env_addr = CONFIG_ENV_ADDR_FLEX;
ea882baf 67# endif
d7e8ce10 68 /* Check OneNAND exist */
937076f8 69 if (mtd->writesize)
d7e8ce10 70 /* Ignore read fail */
937076f8 71 mtd->read(mtd, env_addr, ONENAND_MAX_ENV_SIZE,
b919ec25 72 &retlen, (u_char *)buf);
d7e8ce10 73 else
937076f8 74 mtd->writesize = MAX_ONENAND_PAGESIZE;
ea882baf 75#endif /* !ENV_IS_EMBEDDED */
d7e8ce10 76
ea882baf
WD
77 rc = env_import(buf, 1);
78 if (rc)
79 gd->env_valid = 1;
d7e8ce10
KP
80}
81
82int saveenv(void)
83{
ea882baf
WD
84 env_t env_new;
85 ssize_t len;
86 char *res;
937076f8 87 struct mtd_info *mtd = &onenand_mtd;
b821cead 88#ifdef CONFIG_ENV_ADDR_FLEX
c758e947 89 struct onenand_chip *this = &onenand_chip;
b821cead 90#endif
ea882baf
WD
91 loff_t env_addr = CONFIG_ENV_ADDR;
92 size_t retlen;
a9da2b41
KP
93 struct erase_info instr = {
94 .callback = NULL,
95 };
ea882baf
WD
96
97 res = (char *)&env_new.data;
37f2fe74 98 len = hexport_r(&env_htab, '\0', &res, ENV_SIZE, 0, NULL);
ea882baf
WD
99 if (len < 0) {
100 error("Cannot export environment: errno = %d\n", errno);
101 return 1;
102 }
103 env_new.crc = crc32(0, env_new.data, ENV_SIZE);
d7e8ce10 104
0e8d1586 105 instr.len = CONFIG_ENV_SIZE;
b821cead 106#ifdef CONFIG_ENV_ADDR_FLEX
c758e947
AKS
107 if (FLEXONENAND(this)) {
108 env_addr = CONFIG_ENV_ADDR_FLEX;
109 instr.len = CONFIG_ENV_SIZE_FLEX;
110 instr.len <<= onenand_mtd.eraseregions[0].numblocks == 1 ?
111 1 : 0;
112 }
b821cead 113#endif
d7e8ce10 114 instr.addr = env_addr;
937076f8
KP
115 instr.mtd = mtd;
116 if (mtd->erase(mtd, &instr)) {
48287792 117 printf("OneNAND: erase failed at 0x%08llx\n", env_addr);
d7e8ce10
KP
118 return 1;
119 }
120
937076f8 121 if (mtd->write(mtd, env_addr, ONENAND_MAX_ENV_SIZE, &retlen,
b919ec25 122 (u_char *)&env_new)) {
8d2effea 123 printf("OneNAND: write failed at 0x%llx\n", instr.addr);
d7e8ce10
KP
124 return 2;
125 }
126
127 return 0;
128}
129
130int env_init(void)
131{
d7e8ce10 132 /* use default */
b919ec25 133 gd->env_addr = (ulong)&default_environment[0];
d7e8ce10
KP
134 gd->env_valid = 1;
135
136 return 0;
137}