blob: 5e04a06cf537c1faef56b1613ff66d6488d174b3 [file] [log] [blame]
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +09001/*
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02002 * (C) Copyright 2010 DENX Software Engineering
3 * Wolfgang Denk <wd@denx.de>
4 *
Kyungmin Parka1f416c2009-07-11 16:49:55 +09005 * (C) Copyright 2005-2009 Samsung Electronics
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +09006 * Kyungmin Park <kyungmin.park@samsung.com>
7 *
8 * See file CREDITS for list of people who contributed to this
9 * project.
10 *
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.
15 *
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.
20 *
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,
24 * MA 02111-1307 USA
25 */
26
27#include <common.h>
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +090028#include <command.h>
29#include <environment.h>
30#include <linux/stddef.h>
31#include <malloc.h>
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020032#include <search.h>
33#include <errno.h>
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +090034
35#include <linux/mtd/compat.h>
36#include <linux/mtd/mtd.h>
37#include <linux/mtd/onenand.h>
38
39extern struct mtd_info onenand_mtd;
40extern struct onenand_chip onenand_chip;
41
42/* References to names in env_common.c */
43extern uchar default_environment[];
44
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +090045char *env_name_spec = "OneNAND";
46
Kyungmin Parka1f416c2009-07-11 16:49:55 +090047#define ONENAND_MAX_ENV_SIZE 4096
48#define ONENAND_ENV_SIZE(mtd) (ONENAND_MAX_ENV_SIZE - ENV_HEADER_SIZE)
49
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +090050#ifdef ENV_IS_EMBEDDED
51extern uchar environment[];
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +090052#endif /* ENV_IS_EMBEDDED */
53
Kyungmin Park012f2aa2008-03-31 10:40:19 +090054DECLARE_GLOBAL_DATA_PTR;
55
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +090056uchar env_get_char_spec(int index)
57{
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020058 return (*((uchar *)(gd->env_addr + index)));
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +090059}
60
61void env_relocate_spec(void)
62{
Kyungmin Parka1f416c2009-07-11 16:49:55 +090063 struct mtd_info *mtd = &onenand_mtd;
Sanjeev Premi6173ce82009-12-18 14:55:24 +053064#ifdef CONFIG_ENV_ADDR_FLEX
Amul Kumar Saha47735662009-11-04 10:38:46 +053065 struct onenand_chip *this = &onenand_chip;
Sanjeev Premi6173ce82009-12-18 14:55:24 +053066#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020067 int rc;
Peter Pearsef4026142007-11-15 08:58:00 +000068 size_t retlen;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020069#ifdef ENV_IS_EMBEDDED
70 char *buf = (char *)&environment[0];
71#else
72 loff_t env_addr = CONFIG_ENV_ADDR;
73 char onenand_env[ONENAND_MAX_ENV_SIZE];
74 char *buf = (char *)&onenand_env[0];
75#endif /* ENV_IS_EMBEDDED */
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +090076
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020077#ifndef ENV_IS_EMBEDDED
78# ifdef CONFIG_ENV_ADDR_FLEX
Amul Kumar Saha47735662009-11-04 10:38:46 +053079 if (FLEXONENAND(this))
80 env_addr = CONFIG_ENV_ADDR_FLEX;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020081# endif
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +090082 /* Check OneNAND exist */
Kyungmin Parka1f416c2009-07-11 16:49:55 +090083 if (mtd->writesize)
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +090084 /* Ignore read fail */
Kyungmin Parka1f416c2009-07-11 16:49:55 +090085 mtd->read(mtd, env_addr, ONENAND_MAX_ENV_SIZE,
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020086 &retlen, (u_char *)buf);
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +090087 else
Kyungmin Parka1f416c2009-07-11 16:49:55 +090088 mtd->writesize = MAX_ONENAND_PAGESIZE;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020089#endif /* !ENV_IS_EMBEDDED */
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +090090
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020091 rc = env_import(buf, 1);
92 if (rc)
93 gd->env_valid = 1;
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +090094}
95
96int saveenv(void)
97{
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020098 env_t env_new;
99 ssize_t len;
100 char *res;
Kyungmin Parka1f416c2009-07-11 16:49:55 +0900101 struct mtd_info *mtd = &onenand_mtd;
Sanjeev Premi6173ce82009-12-18 14:55:24 +0530102#ifdef CONFIG_ENV_ADDR_FLEX
Amul Kumar Saha47735662009-11-04 10:38:46 +0530103 struct onenand_chip *this = &onenand_chip;
Sanjeev Premi6173ce82009-12-18 14:55:24 +0530104#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200105 loff_t env_addr = CONFIG_ENV_ADDR;
106 size_t retlen;
Kyungmin Park012f2aa2008-03-31 10:40:19 +0900107 struct erase_info instr = {
108 .callback = NULL,
109 };
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200110
111 res = (char *)&env_new.data;
Mike Frysinger92d7a992010-12-08 06:26:04 -0500112 len = hexport_r(&env_htab, '\0', &res, ENV_SIZE);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200113 if (len < 0) {
114 error("Cannot export environment: errno = %d\n", errno);
115 return 1;
116 }
117 env_new.crc = crc32(0, env_new.data, ENV_SIZE);
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900118
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200119 instr.len = CONFIG_ENV_SIZE;
Sanjeev Premi6173ce82009-12-18 14:55:24 +0530120#ifdef CONFIG_ENV_ADDR_FLEX
Amul Kumar Saha47735662009-11-04 10:38:46 +0530121 if (FLEXONENAND(this)) {
122 env_addr = CONFIG_ENV_ADDR_FLEX;
123 instr.len = CONFIG_ENV_SIZE_FLEX;
124 instr.len <<= onenand_mtd.eraseregions[0].numblocks == 1 ?
125 1 : 0;
126 }
Sanjeev Premi6173ce82009-12-18 14:55:24 +0530127#endif
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900128 instr.addr = env_addr;
Kyungmin Parka1f416c2009-07-11 16:49:55 +0900129 instr.mtd = mtd;
130 if (mtd->erase(mtd, &instr)) {
Kyungmin Park458e0ff2009-07-20 09:47:47 +0900131 printf("OneNAND: erase failed at 0x%08llx\n", env_addr);
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900132 return 1;
133 }
134
Kyungmin Parka1f416c2009-07-11 16:49:55 +0900135 if (mtd->write(mtd, env_addr, ONENAND_MAX_ENV_SIZE, &retlen,
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200136 (u_char *)&env_new)) {
Stefan Roese586b3a62009-05-11 16:03:55 +0200137 printf("OneNAND: write failed at 0x%llx\n", instr.addr);
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900138 return 2;
139 }
140
141 return 0;
142}
143
144int env_init(void)
145{
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900146 /* use default */
147 gd->env_addr = (ulong) & default_environment[0];
148 gd->env_valid = 1;
149
150 return 0;
151}