2 * (C) Copyright 2005-2009 Samsung Electronics
3 * Kyungmin Park <kyungmin.park@samsung.com>
5 * See file CREDITS for list of people who contributed to this
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.
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.
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,
26 #include <environment.h>
27 #include <linux/stddef.h>
30 #include <linux/mtd/compat.h>
31 #include <linux/mtd/mtd.h>
32 #include <linux/mtd/onenand.h>
34 extern struct mtd_info onenand_mtd;
35 extern struct onenand_chip onenand_chip;
37 /* References to names in env_common.c */
38 extern uchar default_environment[];
40 char *env_name_spec = "OneNAND";
42 #define ONENAND_MAX_ENV_SIZE 4096
43 #define ONENAND_ENV_SIZE(mtd) (ONENAND_MAX_ENV_SIZE - ENV_HEADER_SIZE)
45 #ifdef ENV_IS_EMBEDDED
46 extern uchar environment[];
47 env_t *env_ptr = (env_t *) (&environment[0]);
48 #else /* ! ENV_IS_EMBEDDED */
49 static unsigned char onenand_env[ONENAND_MAX_ENV_SIZE];
50 env_t *env_ptr = (env_t *) onenand_env;
51 #endif /* ENV_IS_EMBEDDED */
53 DECLARE_GLOBAL_DATA_PTR;
55 uchar env_get_char_spec(int index)
57 return (*((uchar *) (gd->env_addr + index)));
60 void env_relocate_spec(void)
62 struct mtd_info *mtd = &onenand_mtd;
63 #ifdef CONFIG_ENV_ADDR_FLEX
64 struct onenand_chip *this = &onenand_chip;
70 env_addr = CONFIG_ENV_ADDR;
71 #ifdef CONFIG_ENV_ADDR_FLEX
72 if (FLEXONENAND(this))
73 env_addr = CONFIG_ENV_ADDR_FLEX;
75 /* Check OneNAND exist */
77 /* Ignore read fail */
78 mtd->read(mtd, env_addr, ONENAND_MAX_ENV_SIZE,
79 &retlen, (u_char *) env_ptr);
81 mtd->writesize = MAX_ONENAND_PAGESIZE;
83 if (crc32(0, env_ptr->data, ONENAND_ENV_SIZE(mtd)) != env_ptr->crc)
87 memcpy(env_ptr->data, default_environment,
88 ONENAND_ENV_SIZE(mtd));
90 crc32(0, env_ptr->data, ONENAND_ENV_SIZE(mtd));
93 gd->env_addr = (ulong) & env_ptr->data;
99 struct mtd_info *mtd = &onenand_mtd;
100 #ifdef CONFIG_ENV_ADDR_FLEX
101 struct onenand_chip *this = &onenand_chip;
103 loff_t env_addr = CONFIG_ENV_ADDR;
104 struct erase_info instr = {
109 instr.len = CONFIG_ENV_SIZE;
110 #ifdef CONFIG_ENV_ADDR_FLEX
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 ?
118 instr.addr = env_addr;
120 if (mtd->erase(mtd, &instr)) {
121 printf("OneNAND: erase failed at 0x%08llx\n", env_addr);
126 env_ptr->crc = crc32(0, env_ptr->data, ONENAND_ENV_SIZE(mtd));
128 if (mtd->write(mtd, env_addr, ONENAND_MAX_ENV_SIZE, &retlen,
129 (u_char *) env_ptr)) {
130 printf("OneNAND: write failed at 0x%llx\n", instr.addr);
140 gd->env_addr = (ulong) & default_environment[0];