2 * (C) Copyright 2010 DENX Software Engineering
3 * Wolfgang Denk <wd@denx.de>
5 * (C) Copyright 2005-2009 Samsung Electronics
6 * Kyungmin Park <kyungmin.park@samsung.com>
8 * See file CREDITS for list of people who contributed to this
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.
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.
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,
29 #include <environment.h>
30 #include <linux/stddef.h>
34 #include <onenand_uboot.h>
36 #include <linux/mtd/compat.h>
37 #include <linux/mtd/mtd.h>
38 #include <linux/mtd/onenand.h>
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 DECLARE_GLOBAL_DATA_PTR;
47 uchar env_get_char_spec(int index)
49 return *((uchar *)(gd->env_addr + index));
52 void env_relocate_spec(void)
54 struct mtd_info *mtd = &onenand_mtd;
55 #ifdef CONFIG_ENV_ADDR_FLEX
56 struct onenand_chip *this = &onenand_chip;
60 #ifdef ENV_IS_EMBEDDED
61 char *buf = (char *)&environment;
63 loff_t env_addr = CONFIG_ENV_ADDR;
64 char onenand_env[ONENAND_MAX_ENV_SIZE];
65 char *buf = (char *)&onenand_env[0];
66 #endif /* ENV_IS_EMBEDDED */
68 #ifndef ENV_IS_EMBEDDED
69 # ifdef CONFIG_ENV_ADDR_FLEX
70 if (FLEXONENAND(this))
71 env_addr = CONFIG_ENV_ADDR_FLEX;
73 /* Check OneNAND exist */
75 /* Ignore read fail */
76 mtd->read(mtd, env_addr, ONENAND_MAX_ENV_SIZE,
77 &retlen, (u_char *)buf);
79 mtd->writesize = MAX_ONENAND_PAGESIZE;
80 #endif /* !ENV_IS_EMBEDDED */
82 rc = env_import(buf, 1);
92 struct mtd_info *mtd = &onenand_mtd;
93 #ifdef CONFIG_ENV_ADDR_FLEX
94 struct onenand_chip *this = &onenand_chip;
96 loff_t env_addr = CONFIG_ENV_ADDR;
98 struct erase_info instr = {
102 res = (char *)&env_new.data;
103 len = hexport_r(&env_htab, '\0', &res, ENV_SIZE, 0, NULL);
105 error("Cannot export environment: errno = %d\n", errno);
108 env_new.crc = crc32(0, env_new.data, ENV_SIZE);
110 instr.len = CONFIG_ENV_SIZE;
111 #ifdef CONFIG_ENV_ADDR_FLEX
112 if (FLEXONENAND(this)) {
113 env_addr = CONFIG_ENV_ADDR_FLEX;
114 instr.len = CONFIG_ENV_SIZE_FLEX;
115 instr.len <<= onenand_mtd.eraseregions[0].numblocks == 1 ?
119 instr.addr = env_addr;
121 if (mtd->erase(mtd, &instr)) {
122 printf("OneNAND: erase failed at 0x%08llx\n", env_addr);
126 if (mtd->write(mtd, env_addr, ONENAND_MAX_ENV_SIZE, &retlen,
127 (u_char *)&env_new)) {
128 printf("OneNAND: write failed at 0x%llx\n", instr.addr);
138 gd->env_addr = (ulong)&default_environment[0];