blob: dcf09dee1bcf9d3162ef20974677e5ed98a72752 [file] [log] [blame]
Kyungmin Parkd7e8ce12007-09-10 17:15:14 +09001/*
Kyungmin Park937076f2009-07-11 16:49:55 +09002 * (C) Copyright 2005-2009 Samsung Electronics
Kyungmin Parkd7e8ce12007-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 Parkd7e8ce12007-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 Parkd7e8ce12007-09-10 17:15:14 +090040char *env_name_spec = "OneNAND";
41
Kyungmin Park937076f2009-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 Parkd7e8ce12007-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 Park937076f2009-07-11 16:49:55 +090049static unsigned char onenand_env[ONENAND_MAX_ENV_SIZE];
Kyungmin Parkd7e8ce12007-09-10 17:15:14 +090050env_t *env_ptr = (env_t *) onenand_env;
51#endif /* ENV_IS_EMBEDDED */
52
Kyungmin Parka9da2b42008-03-31 10:40:19 +090053DECLARE_GLOBAL_DATA_PTR;
54
Kyungmin Parkd7e8ce12007-09-10 17:15:14 +090055uchar env_get_char_spec(int index)
56{
Kyungmin Parkd7e8ce12007-09-10 17:15:14 +090057 return (*((uchar *) (gd->env_addr + index)));
58}
59
60void env_relocate_spec(void)
61{
Kyungmin Park937076f2009-07-11 16:49:55 +090062 struct mtd_info *mtd = &onenand_mtd;
HeungJun Kim2e8a6f52009-06-30 14:42:22 +090063 loff_t env_addr;
Kyungmin Parkd7e8ce12007-09-10 17:15:14 +090064 int use_default = 0;
Peter Pearse2ae64f52007-11-15 08:58:00 +000065 size_t retlen;
Kyungmin Parkd7e8ce12007-09-10 17:15:14 +090066
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020067 env_addr = CONFIG_ENV_ADDR;
Kyungmin Parkd7e8ce12007-09-10 17:15:14 +090068
69 /* Check OneNAND exist */
Kyungmin Park937076f2009-07-11 16:49:55 +090070 if (mtd->writesize)
Kyungmin Parkd7e8ce12007-09-10 17:15:14 +090071 /* Ignore read fail */
Kyungmin Park937076f2009-07-11 16:49:55 +090072 mtd->read(mtd, env_addr, ONENAND_MAX_ENV_SIZE,
Kyungmin Parkd7e8ce12007-09-10 17:15:14 +090073 &retlen, (u_char *) env_ptr);
74 else
Kyungmin Park937076f2009-07-11 16:49:55 +090075 mtd->writesize = MAX_ONENAND_PAGESIZE;
Kyungmin Parkd7e8ce12007-09-10 17:15:14 +090076
Kyungmin Park937076f2009-07-11 16:49:55 +090077 if (crc32(0, env_ptr->data, ONENAND_ENV_SIZE(mtd)) != env_ptr->crc)
Kyungmin Parkd7e8ce12007-09-10 17:15:14 +090078 use_default = 1;
79
80 if (use_default) {
81 memcpy(env_ptr->data, default_environment,
Kyungmin Park937076f2009-07-11 16:49:55 +090082 ONENAND_ENV_SIZE(mtd));
Kyungmin Parkd7e8ce12007-09-10 17:15:14 +090083 env_ptr->crc =
Kyungmin Park937076f2009-07-11 16:49:55 +090084 crc32(0, env_ptr->data, ONENAND_ENV_SIZE(mtd));
Kyungmin Parkd7e8ce12007-09-10 17:15:14 +090085 }
86
87 gd->env_addr = (ulong) & env_ptr->data;
88 gd->env_valid = 1;
89}
90
91int saveenv(void)
92{
Kyungmin Park937076f2009-07-11 16:49:55 +090093 struct mtd_info *mtd = &onenand_mtd;
94 loff_t env_addr = CONFIG_ENV_ADDR;
Kyungmin Parka9da2b42008-03-31 10:40:19 +090095 struct erase_info instr = {
96 .callback = NULL,
97 };
Peter Pearse2ae64f52007-11-15 08:58:00 +000098 size_t retlen;
Kyungmin Parkd7e8ce12007-09-10 17:15:14 +090099
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200100 instr.len = CONFIG_ENV_SIZE;
Kyungmin Parkd7e8ce12007-09-10 17:15:14 +0900101 instr.addr = env_addr;
Kyungmin Park937076f2009-07-11 16:49:55 +0900102 instr.mtd = mtd;
103 if (mtd->erase(mtd, &instr)) {
Kyungmin Park48287792009-07-20 09:47:47 +0900104 printf("OneNAND: erase failed at 0x%08llx\n", env_addr);
Kyungmin Parkd7e8ce12007-09-10 17:15:14 +0900105 return 1;
106 }
107
108 /* update crc */
Kyungmin Park937076f2009-07-11 16:49:55 +0900109 env_ptr->crc = crc32(0, env_ptr->data, ONENAND_ENV_SIZE(mtd));
Kyungmin Parkd7e8ce12007-09-10 17:15:14 +0900110
Kyungmin Park937076f2009-07-11 16:49:55 +0900111 if (mtd->write(mtd, env_addr, ONENAND_MAX_ENV_SIZE, &retlen,
Kyungmin Parkd7e8ce12007-09-10 17:15:14 +0900112 (u_char *) env_ptr)) {
Stefan Roese8d2effe2009-05-11 16:03:55 +0200113 printf("OneNAND: write failed at 0x%llx\n", instr.addr);
Kyungmin Parkd7e8ce12007-09-10 17:15:14 +0900114 return 2;
115 }
116
117 return 0;
118}
119
120int env_init(void)
121{
Kyungmin Parkd7e8ce12007-09-10 17:15:14 +0900122 /* use default */
123 gd->env_addr = (ulong) & default_environment[0];
124 gd->env_valid = 1;
125
126 return 0;
127}