env_onenand.c 3.33 KB
Newer Older
Kyungmin Park's avatar
Kyungmin Park committed
1
/*
2
3
4
 * (C) Copyright 2010 DENX Software Engineering
 * Wolfgang Denk <wd@denx.de>
 *
5
 * (C) Copyright 2005-2009 Samsung Electronics
Kyungmin Park's avatar
Kyungmin Park committed
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
 * Kyungmin Park <kyungmin.park@samsung.com>
 *
 * See file CREDITS for list of people who contributed to this
 * project.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 * MA 02111-1307 USA
 */

#include <common.h>
#include <command.h>
#include <environment.h>
#include <linux/stddef.h>
#include <malloc.h>
32
33
#include <search.h>
#include <errno.h>
34
#include <onenand_uboot.h>
Kyungmin Park's avatar
Kyungmin Park committed
35

36
#include <linux/compat.h>
Kyungmin Park's avatar
Kyungmin Park committed
37
38
39
40
41
#include <linux/mtd/mtd.h>
#include <linux/mtd/onenand.h>

char *env_name_spec = "OneNAND";

42
#define ONENAND_MAX_ENV_SIZE	CONFIG_ENV_SIZE
43
44
#define ONENAND_ENV_SIZE(mtd)	(ONENAND_MAX_ENV_SIZE - ENV_HEADER_SIZE)

Kyungmin Park's avatar
Kyungmin Park committed
45
46
DECLARE_GLOBAL_DATA_PTR;

Kyungmin Park's avatar
Kyungmin Park committed
47
48
void env_relocate_spec(void)
{
49
	struct mtd_info *mtd = &onenand_mtd;
50
#ifdef CONFIG_ENV_ADDR_FLEX
51
	struct onenand_chip *this = &onenand_chip;
52
#endif
53
	int rc;
54
	size_t retlen;
55
#ifdef ENV_IS_EMBEDDED
56
	char *buf = (char *)&environment;
57
58
59
60
61
#else
	loff_t env_addr = CONFIG_ENV_ADDR;
	char onenand_env[ONENAND_MAX_ENV_SIZE];
	char *buf = (char *)&onenand_env[0];
#endif /* ENV_IS_EMBEDDED */
Kyungmin Park's avatar
Kyungmin Park committed
62

63
64
#ifndef ENV_IS_EMBEDDED
# ifdef CONFIG_ENV_ADDR_FLEX
65
66
	if (FLEXONENAND(this))
		env_addr = CONFIG_ENV_ADDR_FLEX;
67
# endif
Kyungmin Park's avatar
Kyungmin Park committed
68
	/* Check OneNAND exist */
69
	if (mtd->writesize)
Kyungmin Park's avatar
Kyungmin Park committed
70
		/* Ignore read fail */
71
		mtd->read(mtd, env_addr, ONENAND_MAX_ENV_SIZE,
72
				&retlen, (u_char *)buf);
Kyungmin Park's avatar
Kyungmin Park committed
73
	else
74
		mtd->writesize = MAX_ONENAND_PAGESIZE;
75
#endif /* !ENV_IS_EMBEDDED */
Kyungmin Park's avatar
Kyungmin Park committed
76

77
78
79
	rc = env_import(buf, 1);
	if (rc)
		gd->env_valid = 1;
Kyungmin Park's avatar
Kyungmin Park committed
80
81
82
83
}

int saveenv(void)
{
84
85
86
	env_t	env_new;
	ssize_t	len;
	char	*res;
87
	struct mtd_info *mtd = &onenand_mtd;
88
#ifdef CONFIG_ENV_ADDR_FLEX
89
	struct onenand_chip *this = &onenand_chip;
90
#endif
91
92
	loff_t	env_addr = CONFIG_ENV_ADDR;
	size_t	retlen;
Kyungmin Park's avatar
Kyungmin Park committed
93
94
95
	struct erase_info instr = {
		.callback	= NULL,
	};
96
97

	res = (char *)&env_new.data;
98
	len = hexport_r(&env_htab, '\0', &res, ENV_SIZE, 0, NULL);
99
100
101
102
103
	if (len < 0) {
		error("Cannot export environment: errno = %d\n", errno);
		return 1;
	}
	env_new.crc = crc32(0, env_new.data, ENV_SIZE);
Kyungmin Park's avatar
Kyungmin Park committed
104

105
	instr.len = CONFIG_ENV_SIZE;
106
#ifdef CONFIG_ENV_ADDR_FLEX
107
108
109
110
111
112
	if (FLEXONENAND(this)) {
		env_addr = CONFIG_ENV_ADDR_FLEX;
		instr.len = CONFIG_ENV_SIZE_FLEX;
		instr.len <<= onenand_mtd.eraseregions[0].numblocks == 1 ?
				1 : 0;
	}
113
#endif
Kyungmin Park's avatar
Kyungmin Park committed
114
	instr.addr = env_addr;
115
116
	instr.mtd = mtd;
	if (mtd->erase(mtd, &instr)) {
117
		printf("OneNAND: erase failed at 0x%08llx\n", env_addr);
Kyungmin Park's avatar
Kyungmin Park committed
118
119
120
		return 1;
	}

121
	if (mtd->write(mtd, env_addr, ONENAND_MAX_ENV_SIZE, &retlen,
122
			(u_char *)&env_new)) {
123
		printf("OneNAND: write failed at 0x%llx\n", instr.addr);
Kyungmin Park's avatar
Kyungmin Park committed
124
125
126
127
128
129
130
131
132
		return 2;
	}

	return 0;
}

int env_init(void)
{
	/* use default */
133
	gd->env_addr = (ulong)&default_environment[0];
Kyungmin Park's avatar
Kyungmin Park committed
134
135
136
137
	gd->env_valid = 1;

	return 0;
}