blob: dbccc791258b7f0b054c5a26377387d2e0a14a9c [file] [log] [blame]
Kyungmin Parkd7e8ce12007-09-10 17:15:14 +09001/*
2 * (C) Copyright 2005-2007 Samsung Electronics
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>
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 Parkd438d502008-08-13 09:11:02 +090040#define ONENAND_ENV_SIZE(mtd) (mtd.writesize - ENV_HEADER_SIZE)
Kyungmin Parkd7e8ce12007-09-10 17:15:14 +090041
42char *env_name_spec = "OneNAND";
43
44#ifdef ENV_IS_EMBEDDED
45extern uchar environment[];
46env_t *env_ptr = (env_t *) (&environment[0]);
47#else /* ! ENV_IS_EMBEDDED */
48static unsigned char onenand_env[MAX_ONENAND_PAGESIZE];
49env_t *env_ptr = (env_t *) onenand_env;
50#endif /* ENV_IS_EMBEDDED */
51
Kyungmin Parka9da2b42008-03-31 10:40:19 +090052DECLARE_GLOBAL_DATA_PTR;
53
Kyungmin Parkd7e8ce12007-09-10 17:15:14 +090054uchar env_get_char_spec(int index)
55{
Kyungmin Parkd7e8ce12007-09-10 17:15:14 +090056 return (*((uchar *) (gd->env_addr + index)));
57}
58
59void env_relocate_spec(void)
60{
Kyungmin Parkd7e8ce12007-09-10 17:15:14 +090061 unsigned long env_addr;
62 int use_default = 0;
Peter Pearse2ae64f52007-11-15 08:58:00 +000063 size_t retlen;
Kyungmin Parkd7e8ce12007-09-10 17:15:14 +090064
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020065 env_addr = CONFIG_ENV_ADDR;
Kyungmin Parkd7e8ce12007-09-10 17:15:14 +090066
67 /* Check OneNAND exist */
Kyungmin Parkd438d502008-08-13 09:11:02 +090068 if (onenand_mtd.writesize)
Kyungmin Parkd7e8ce12007-09-10 17:15:14 +090069 /* Ignore read fail */
Kyungmin Parkd438d502008-08-13 09:11:02 +090070 onenand_read(&onenand_mtd, env_addr, onenand_mtd.writesize,
Kyungmin Parkd7e8ce12007-09-10 17:15:14 +090071 &retlen, (u_char *) env_ptr);
72 else
Kyungmin Parkd438d502008-08-13 09:11:02 +090073 onenand_mtd.writesize = MAX_ONENAND_PAGESIZE;
Kyungmin Parkd7e8ce12007-09-10 17:15:14 +090074
75 if (crc32(0, env_ptr->data, ONENAND_ENV_SIZE(onenand_mtd)) !=
76 env_ptr->crc)
77 use_default = 1;
78
79 if (use_default) {
80 memcpy(env_ptr->data, default_environment,
81 ONENAND_ENV_SIZE(onenand_mtd));
82 env_ptr->crc =
83 crc32(0, env_ptr->data, ONENAND_ENV_SIZE(onenand_mtd));
84 }
85
86 gd->env_addr = (ulong) & env_ptr->data;
87 gd->env_valid = 1;
88}
89
90int saveenv(void)
91{
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020092 unsigned long env_addr = CONFIG_ENV_ADDR;
Kyungmin Parka9da2b42008-03-31 10:40:19 +090093 struct erase_info instr = {
94 .callback = NULL,
95 };
Peter Pearse2ae64f52007-11-15 08:58:00 +000096 size_t retlen;
Kyungmin Parkd7e8ce12007-09-10 17:15:14 +090097
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020098 instr.len = CONFIG_ENV_SIZE;
Kyungmin Parkd7e8ce12007-09-10 17:15:14 +090099 instr.addr = env_addr;
Stefan Roese5e46b1e2008-11-27 14:11:37 +0100100 instr.mtd = &onenand_mtd;
Kyungmin Parkd7e8ce12007-09-10 17:15:14 +0900101 if (onenand_erase(&onenand_mtd, &instr)) {
Jean-Christophe PLAGNIOL-VILLARD0a5676b2008-07-12 14:36:34 +0200102 printf("OneNAND: erase failed at 0x%08lx\n", env_addr);
Kyungmin Parkd7e8ce12007-09-10 17:15:14 +0900103 return 1;
104 }
105
106 /* update crc */
107 env_ptr->crc =
Kyungmin Parka9da2b42008-03-31 10:40:19 +0900108 crc32(0, env_ptr->data, ONENAND_ENV_SIZE(onenand_mtd));
Kyungmin Parkd7e8ce12007-09-10 17:15:14 +0900109
Kyungmin Parkd438d502008-08-13 09:11:02 +0900110 if (onenand_write(&onenand_mtd, env_addr, onenand_mtd.writesize, &retlen,
Kyungmin Parkd7e8ce12007-09-10 17:15:14 +0900111 (u_char *) env_ptr)) {
112 printf("OneNAND: write failed at 0x%08x\n", instr.addr);
113 return 2;
114 }
115
116 return 0;
117}
118
119int env_init(void)
120{
Kyungmin Parkd7e8ce12007-09-10 17:15:14 +0900121 /* use default */
122 gd->env_addr = (ulong) & default_environment[0];
123 gd->env_valid = 1;
124
125 return 0;
126}