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