blob: 0ad2fc7a4cd206785449c80584bb182c1dba0a7f [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>
Igor Grinbergd0ed0032011-11-07 01:14:03 +000034#include <onenand_uboot.h>
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +090035
36#include <linux/mtd/compat.h>
37#include <linux/mtd/mtd.h>
38#include <linux/mtd/onenand.h>
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 Park012f2aa2008-03-31 10:40:19 +090045DECLARE_GLOBAL_DATA_PTR;
46
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +090047uchar env_get_char_spec(int index)
48{
Igor Grinbergd0ed0032011-11-07 01:14:03 +000049 return *((uchar *)(gd->env_addr + index));
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +090050}
51
52void env_relocate_spec(void)
53{
Kyungmin Parka1f416c2009-07-11 16:49:55 +090054 struct mtd_info *mtd = &onenand_mtd;
Sanjeev Premi6173ce82009-12-18 14:55:24 +053055#ifdef CONFIG_ENV_ADDR_FLEX
Amul Kumar Saha47735662009-11-04 10:38:46 +053056 struct onenand_chip *this = &onenand_chip;
Sanjeev Premi6173ce82009-12-18 14:55:24 +053057#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020058 int rc;
Peter Pearsef4026142007-11-15 08:58:00 +000059 size_t retlen;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020060#ifdef ENV_IS_EMBEDDED
Igor Grinberg23b54b92011-11-17 06:07:23 +000061 char *buf = (char *)&environment;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020062#else
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 */
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +090067
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020068#ifndef ENV_IS_EMBEDDED
69# ifdef CONFIG_ENV_ADDR_FLEX
Amul Kumar Saha47735662009-11-04 10:38:46 +053070 if (FLEXONENAND(this))
71 env_addr = CONFIG_ENV_ADDR_FLEX;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020072# endif
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +090073 /* Check OneNAND exist */
Kyungmin Parka1f416c2009-07-11 16:49:55 +090074 if (mtd->writesize)
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +090075 /* Ignore read fail */
Kyungmin Parka1f416c2009-07-11 16:49:55 +090076 mtd->read(mtd, env_addr, ONENAND_MAX_ENV_SIZE,
Igor Grinbergd0ed0032011-11-07 01:14:03 +000077 &retlen, (u_char *)buf);
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +090078 else
Kyungmin Parka1f416c2009-07-11 16:49:55 +090079 mtd->writesize = MAX_ONENAND_PAGESIZE;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020080#endif /* !ENV_IS_EMBEDDED */
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +090081
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020082 rc = env_import(buf, 1);
83 if (rc)
84 gd->env_valid = 1;
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +090085}
86
87int saveenv(void)
88{
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020089 env_t env_new;
90 ssize_t len;
91 char *res;
Kyungmin Parka1f416c2009-07-11 16:49:55 +090092 struct mtd_info *mtd = &onenand_mtd;
Sanjeev Premi6173ce82009-12-18 14:55:24 +053093#ifdef CONFIG_ENV_ADDR_FLEX
Amul Kumar Saha47735662009-11-04 10:38:46 +053094 struct onenand_chip *this = &onenand_chip;
Sanjeev Premi6173ce82009-12-18 14:55:24 +053095#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020096 loff_t env_addr = CONFIG_ENV_ADDR;
97 size_t retlen;
Kyungmin Park012f2aa2008-03-31 10:40:19 +090098 struct erase_info instr = {
99 .callback = NULL,
100 };
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200101
102 res = (char *)&env_new.data;
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100103 len = hexport_r(&env_htab, '\0', &res, ENV_SIZE, 0, NULL);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200104 if (len < 0) {
105 error("Cannot export environment: errno = %d\n", errno);
106 return 1;
107 }
108 env_new.crc = crc32(0, env_new.data, ENV_SIZE);
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900109
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200110 instr.len = CONFIG_ENV_SIZE;
Sanjeev Premi6173ce82009-12-18 14:55:24 +0530111#ifdef CONFIG_ENV_ADDR_FLEX
Amul Kumar Saha47735662009-11-04 10:38:46 +0530112 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 ?
116 1 : 0;
117 }
Sanjeev Premi6173ce82009-12-18 14:55:24 +0530118#endif
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900119 instr.addr = env_addr;
Kyungmin Parka1f416c2009-07-11 16:49:55 +0900120 instr.mtd = mtd;
121 if (mtd->erase(mtd, &instr)) {
Kyungmin Park458e0ff2009-07-20 09:47:47 +0900122 printf("OneNAND: erase failed at 0x%08llx\n", env_addr);
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900123 return 1;
124 }
125
Kyungmin Parka1f416c2009-07-11 16:49:55 +0900126 if (mtd->write(mtd, env_addr, ONENAND_MAX_ENV_SIZE, &retlen,
Igor Grinbergd0ed0032011-11-07 01:14:03 +0000127 (u_char *)&env_new)) {
Stefan Roese586b3a62009-05-11 16:03:55 +0200128 printf("OneNAND: write failed at 0x%llx\n", instr.addr);
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900129 return 2;
130 }
131
132 return 0;
133}
134
135int env_init(void)
136{
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900137 /* use default */
Igor Grinbergd0ed0032011-11-07 01:14:03 +0000138 gd->env_addr = (ulong)&default_environment[0];
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900139 gd->env_valid = 1;
140
141 return 0;
142}