blob: cf997bf7e507bbda71d3f839fcfcbf3cd004fcc7 [file] [log] [blame]
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +09001/*
Kyungmin Parka1f416c2009-07-11 16:49:55 +09002 * (C) Copyright 2005-2009 Samsung Electronics
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +09003 * 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>
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +090025#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
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +090040char *env_name_spec = "OneNAND";
41
Kyungmin Parka1f416c2009-07-11 16:49:55 +090042#define ONENAND_MAX_ENV_SIZE 4096
43#define ONENAND_ENV_SIZE(mtd) (ONENAND_MAX_ENV_SIZE - ENV_HEADER_SIZE)
44
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +090045#ifdef ENV_IS_EMBEDDED
46extern uchar environment[];
47env_t *env_ptr = (env_t *) (&environment[0]);
48#else /* ! ENV_IS_EMBEDDED */
Kyungmin Parka1f416c2009-07-11 16:49:55 +090049static unsigned char onenand_env[ONENAND_MAX_ENV_SIZE];
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +090050env_t *env_ptr = (env_t *) onenand_env;
51#endif /* ENV_IS_EMBEDDED */
52
Kyungmin Park012f2aa2008-03-31 10:40:19 +090053DECLARE_GLOBAL_DATA_PTR;
54
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +090055uchar env_get_char_spec(int index)
56{
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +090057 return (*((uchar *) (gd->env_addr + index)));
58}
59
60void env_relocate_spec(void)
61{
Kyungmin Parka1f416c2009-07-11 16:49:55 +090062 struct mtd_info *mtd = &onenand_mtd;
Sanjeev Premi6173ce82009-12-18 14:55:24 +053063#ifdef CONFIG_ENV_ADDR_FLEX
Amul Kumar Saha47735662009-11-04 10:38:46 +053064 struct onenand_chip *this = &onenand_chip;
Sanjeev Premi6173ce82009-12-18 14:55:24 +053065#endif
HeungJun Kim4dbb4f82009-06-30 14:42:22 +090066 loff_t env_addr;
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +090067 int use_default = 0;
Peter Pearsef4026142007-11-15 08:58:00 +000068 size_t retlen;
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +090069
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +020070 env_addr = CONFIG_ENV_ADDR;
Sanjeev Premi6173ce82009-12-18 14:55:24 +053071#ifdef CONFIG_ENV_ADDR_FLEX
Amul Kumar Saha47735662009-11-04 10:38:46 +053072 if (FLEXONENAND(this))
73 env_addr = CONFIG_ENV_ADDR_FLEX;
Sanjeev Premi6173ce82009-12-18 14:55:24 +053074#endif
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +090075 /* Check OneNAND exist */
Kyungmin Parka1f416c2009-07-11 16:49:55 +090076 if (mtd->writesize)
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +090077 /* Ignore read fail */
Kyungmin Parka1f416c2009-07-11 16:49:55 +090078 mtd->read(mtd, env_addr, ONENAND_MAX_ENV_SIZE,
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +090079 &retlen, (u_char *) env_ptr);
80 else
Kyungmin Parka1f416c2009-07-11 16:49:55 +090081 mtd->writesize = MAX_ONENAND_PAGESIZE;
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +090082
Kyungmin Parka1f416c2009-07-11 16:49:55 +090083 if (crc32(0, env_ptr->data, ONENAND_ENV_SIZE(mtd)) != env_ptr->crc)
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +090084 use_default = 1;
85
86 if (use_default) {
87 memcpy(env_ptr->data, default_environment,
Kyungmin Parka1f416c2009-07-11 16:49:55 +090088 ONENAND_ENV_SIZE(mtd));
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +090089 env_ptr->crc =
Kyungmin Parka1f416c2009-07-11 16:49:55 +090090 crc32(0, env_ptr->data, ONENAND_ENV_SIZE(mtd));
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +090091 }
92
93 gd->env_addr = (ulong) & env_ptr->data;
94 gd->env_valid = 1;
95}
96
97int saveenv(void)
98{
Kyungmin Parka1f416c2009-07-11 16:49:55 +090099 struct mtd_info *mtd = &onenand_mtd;
Sanjeev Premi6173ce82009-12-18 14:55:24 +0530100#ifdef CONFIG_ENV_ADDR_FLEX
Amul Kumar Saha47735662009-11-04 10:38:46 +0530101 struct onenand_chip *this = &onenand_chip;
Sanjeev Premi6173ce82009-12-18 14:55:24 +0530102#endif
Kyungmin Parka1f416c2009-07-11 16:49:55 +0900103 loff_t env_addr = CONFIG_ENV_ADDR;
Kyungmin Park012f2aa2008-03-31 10:40:19 +0900104 struct erase_info instr = {
105 .callback = NULL,
106 };
Peter Pearsef4026142007-11-15 08:58:00 +0000107 size_t retlen;
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900108
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200109 instr.len = CONFIG_ENV_SIZE;
Sanjeev Premi6173ce82009-12-18 14:55:24 +0530110#ifdef CONFIG_ENV_ADDR_FLEX
Amul Kumar Saha47735662009-11-04 10:38:46 +0530111 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 }
Sanjeev Premi6173ce82009-12-18 14:55:24 +0530117#endif
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900118 instr.addr = env_addr;
Kyungmin Parka1f416c2009-07-11 16:49:55 +0900119 instr.mtd = mtd;
120 if (mtd->erase(mtd, &instr)) {
Kyungmin Park458e0ff2009-07-20 09:47:47 +0900121 printf("OneNAND: erase failed at 0x%08llx\n", env_addr);
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900122 return 1;
123 }
124
125 /* update crc */
Kyungmin Parka1f416c2009-07-11 16:49:55 +0900126 env_ptr->crc = crc32(0, env_ptr->data, ONENAND_ENV_SIZE(mtd));
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900127
Kyungmin Parka1f416c2009-07-11 16:49:55 +0900128 if (mtd->write(mtd, env_addr, ONENAND_MAX_ENV_SIZE, &retlen,
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900129 (u_char *) env_ptr)) {
Stefan Roese586b3a62009-05-11 16:03:55 +0200130 printf("OneNAND: write failed at 0x%llx\n", instr.addr);
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900131 return 2;
132 }
133
134 return 0;
135}
136
137int env_init(void)
138{
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900139 /* use default */
140 gd->env_addr = (ulong) & default_environment[0];
141 gd->env_valid = 1;
142
143 return 0;
144}