blob: 7197ab6585324c930b3410f880b7641860f702a7 [file] [log] [blame]
Kyungmin Parkd7e8ce12007-09-10 17:15:14 +09001/*
Wolfgang Denkea882ba2010-06-20 23:33:59 +02002 * (C) Copyright 2010 DENX Software Engineering
3 * Wolfgang Denk <wd@denx.de>
4 *
Kyungmin Park937076f2009-07-11 16:49:55 +09005 * (C) Copyright 2005-2009 Samsung Electronics
Kyungmin Parkd7e8ce12007-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 Parkd7e8ce12007-09-10 17:15:14 +090028#include <command.h>
29#include <environment.h>
30#include <linux/stddef.h>
31#include <malloc.h>
Wolfgang Denkea882ba2010-06-20 23:33:59 +020032#include <search.h>
33#include <errno.h>
Igor Grinbergb919ec22011-11-07 01:14:03 +000034#include <onenand_uboot.h>
Kyungmin Parkd7e8ce12007-09-10 17:15:14 +090035
Mike Frysinger7b15e2b2012-04-09 13:39:55 +000036#include <linux/compat.h>
Kyungmin Parkd7e8ce12007-09-10 17:15:14 +090037#include <linux/mtd/mtd.h>
38#include <linux/mtd/onenand.h>
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 Parka9da2b42008-03-31 10:40:19 +090045DECLARE_GLOBAL_DATA_PTR;
46
Kyungmin Parkd7e8ce12007-09-10 17:15:14 +090047void env_relocate_spec(void)
48{
Kyungmin Park937076f2009-07-11 16:49:55 +090049 struct mtd_info *mtd = &onenand_mtd;
Sanjeev Premib821cea2009-12-18 14:55:24 +053050#ifdef CONFIG_ENV_ADDR_FLEX
Amul Kumar Sahac758e942009-11-04 10:38:46 +053051 struct onenand_chip *this = &onenand_chip;
Sanjeev Premib821cea2009-12-18 14:55:24 +053052#endif
Wolfgang Denkea882ba2010-06-20 23:33:59 +020053 int rc;
Peter Pearse2ae64f52007-11-15 08:58:00 +000054 size_t retlen;
Wolfgang Denkea882ba2010-06-20 23:33:59 +020055#ifdef ENV_IS_EMBEDDED
Igor Grinberg994bc672011-11-17 06:07:23 +000056 char *buf = (char *)&environment;
Wolfgang Denkea882ba2010-06-20 23:33:59 +020057#else
58 loff_t env_addr = CONFIG_ENV_ADDR;
59 char onenand_env[ONENAND_MAX_ENV_SIZE];
60 char *buf = (char *)&onenand_env[0];
61#endif /* ENV_IS_EMBEDDED */
Kyungmin Parkd7e8ce12007-09-10 17:15:14 +090062
Wolfgang Denkea882ba2010-06-20 23:33:59 +020063#ifndef ENV_IS_EMBEDDED
64# ifdef CONFIG_ENV_ADDR_FLEX
Amul Kumar Sahac758e942009-11-04 10:38:46 +053065 if (FLEXONENAND(this))
66 env_addr = CONFIG_ENV_ADDR_FLEX;
Wolfgang Denkea882ba2010-06-20 23:33:59 +020067# endif
Kyungmin Parkd7e8ce12007-09-10 17:15:14 +090068 /* Check OneNAND exist */
Kyungmin Park937076f2009-07-11 16:49:55 +090069 if (mtd->writesize)
Kyungmin Parkd7e8ce12007-09-10 17:15:14 +090070 /* Ignore read fail */
Kyungmin Park937076f2009-07-11 16:49:55 +090071 mtd->read(mtd, env_addr, ONENAND_MAX_ENV_SIZE,
Igor Grinbergb919ec22011-11-07 01:14:03 +000072 &retlen, (u_char *)buf);
Kyungmin Parkd7e8ce12007-09-10 17:15:14 +090073 else
Kyungmin Park937076f2009-07-11 16:49:55 +090074 mtd->writesize = MAX_ONENAND_PAGESIZE;
Wolfgang Denkea882ba2010-06-20 23:33:59 +020075#endif /* !ENV_IS_EMBEDDED */
Kyungmin Parkd7e8ce12007-09-10 17:15:14 +090076
Wolfgang Denkea882ba2010-06-20 23:33:59 +020077 rc = env_import(buf, 1);
78 if (rc)
79 gd->env_valid = 1;
Kyungmin Parkd7e8ce12007-09-10 17:15:14 +090080}
81
82int saveenv(void)
83{
Wolfgang Denkea882ba2010-06-20 23:33:59 +020084 env_t env_new;
85 ssize_t len;
86 char *res;
Kyungmin Park937076f2009-07-11 16:49:55 +090087 struct mtd_info *mtd = &onenand_mtd;
Sanjeev Premib821cea2009-12-18 14:55:24 +053088#ifdef CONFIG_ENV_ADDR_FLEX
Amul Kumar Sahac758e942009-11-04 10:38:46 +053089 struct onenand_chip *this = &onenand_chip;
Sanjeev Premib821cea2009-12-18 14:55:24 +053090#endif
Wolfgang Denkea882ba2010-06-20 23:33:59 +020091 loff_t env_addr = CONFIG_ENV_ADDR;
92 size_t retlen;
Kyungmin Parka9da2b42008-03-31 10:40:19 +090093 struct erase_info instr = {
94 .callback = NULL,
95 };
Wolfgang Denkea882ba2010-06-20 23:33:59 +020096
97 res = (char *)&env_new.data;
Wolfgang Denk37f2fe72011-11-06 22:49:44 +010098 len = hexport_r(&env_htab, '\0', &res, ENV_SIZE, 0, NULL);
Wolfgang Denkea882ba2010-06-20 23:33:59 +020099 if (len < 0) {
100 error("Cannot export environment: errno = %d\n", errno);
101 return 1;
102 }
103 env_new.crc = crc32(0, env_new.data, ENV_SIZE);
Kyungmin Parkd7e8ce12007-09-10 17:15:14 +0900104
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200105 instr.len = CONFIG_ENV_SIZE;
Sanjeev Premib821cea2009-12-18 14:55:24 +0530106#ifdef CONFIG_ENV_ADDR_FLEX
Amul Kumar Sahac758e942009-11-04 10:38:46 +0530107 if (FLEXONENAND(this)) {
108 env_addr = CONFIG_ENV_ADDR_FLEX;
109 instr.len = CONFIG_ENV_SIZE_FLEX;
110 instr.len <<= onenand_mtd.eraseregions[0].numblocks == 1 ?
111 1 : 0;
112 }
Sanjeev Premib821cea2009-12-18 14:55:24 +0530113#endif
Kyungmin Parkd7e8ce12007-09-10 17:15:14 +0900114 instr.addr = env_addr;
Kyungmin Park937076f2009-07-11 16:49:55 +0900115 instr.mtd = mtd;
116 if (mtd->erase(mtd, &instr)) {
Kyungmin Park48287792009-07-20 09:47:47 +0900117 printf("OneNAND: erase failed at 0x%08llx\n", env_addr);
Kyungmin Parkd7e8ce12007-09-10 17:15:14 +0900118 return 1;
119 }
120
Kyungmin Park937076f2009-07-11 16:49:55 +0900121 if (mtd->write(mtd, env_addr, ONENAND_MAX_ENV_SIZE, &retlen,
Igor Grinbergb919ec22011-11-07 01:14:03 +0000122 (u_char *)&env_new)) {
Stefan Roese8d2effe2009-05-11 16:03:55 +0200123 printf("OneNAND: write failed at 0x%llx\n", instr.addr);
Kyungmin Parkd7e8ce12007-09-10 17:15:14 +0900124 return 2;
125 }
126
127 return 0;
128}
129
130int env_init(void)
131{
Kyungmin Parkd7e8ce12007-09-10 17:15:14 +0900132 /* use default */
Igor Grinbergb919ec22011-11-07 01:14:03 +0000133 gd->env_addr = (ulong)&default_environment[0];
Kyungmin Parkd7e8ce12007-09-10 17:15:14 +0900134 gd->env_valid = 1;
135
136 return 0;
137}