]> git.ipfire.org Git - people/ms/u-boot.git/blob - common/env_onenand.c
6fd5613eebef0dfa1a607f16848fabe92ead28af
[people/ms/u-boot.git] / common / env_onenand.c
1 /*
2 * (C) Copyright 2010 DENX Software Engineering
3 * Wolfgang Denk <wd@denx.de>
4 *
5 * (C) Copyright 2005-2009 Samsung Electronics
6 * 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>
28 #include <command.h>
29 #include <environment.h>
30 #include <linux/stddef.h>
31 #include <malloc.h>
32 #include <search.h>
33 #include <errno.h>
34 #include <onenand_uboot.h>
35
36 #include <linux/compat.h>
37 #include <linux/mtd/mtd.h>
38 #include <linux/mtd/onenand.h>
39
40 char *env_name_spec = "OneNAND";
41
42 #define ONENAND_MAX_ENV_SIZE CONFIG_ENV_SIZE
43 #define ONENAND_ENV_SIZE(mtd) (ONENAND_MAX_ENV_SIZE - ENV_HEADER_SIZE)
44
45 static char env_buf[CONFIG_ENV_SIZE];
46
47 DECLARE_GLOBAL_DATA_PTR;
48
49 void env_relocate_spec(void)
50 {
51 struct mtd_info *mtd = &onenand_mtd;
52 #ifdef CONFIG_ENV_ADDR_FLEX
53 struct onenand_chip *this = &onenand_chip;
54 #endif
55 int rc;
56 size_t retlen;
57 #ifdef ENV_IS_EMBEDDED
58 char *buf = (char *)&environment;
59 #else
60 loff_t env_addr = CONFIG_ENV_ADDR;
61 char *buf = env_buf;
62 #endif /* ENV_IS_EMBEDDED */
63
64 #ifndef ENV_IS_EMBEDDED
65 # ifdef CONFIG_ENV_ADDR_FLEX
66 if (FLEXONENAND(this))
67 env_addr = CONFIG_ENV_ADDR_FLEX;
68 # endif
69 /* Check OneNAND exist */
70 if (mtd->writesize)
71 /* Ignore read fail */
72 mtd->read(mtd, env_addr, ONENAND_MAX_ENV_SIZE,
73 &retlen, (u_char *)buf);
74 else
75 mtd->writesize = MAX_ONENAND_PAGESIZE;
76 #endif /* !ENV_IS_EMBEDDED */
77
78 rc = env_import(buf, 1);
79 if (rc)
80 gd->env_valid = 1;
81 }
82
83 int saveenv(void)
84 {
85 env_t *env_new = env_buf;
86 ssize_t len;
87 char *res;
88 struct mtd_info *mtd = &onenand_mtd;
89 #ifdef CONFIG_ENV_ADDR_FLEX
90 struct onenand_chip *this = &onenand_chip;
91 #endif
92 loff_t env_addr = CONFIG_ENV_ADDR;
93 size_t retlen;
94 struct erase_info instr = {
95 .callback = NULL,
96 };
97
98 res = (char *)env_new->data;
99 len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL);
100 if (len < 0) {
101 error("Cannot export environment: errno = %d\n", errno);
102 return 1;
103 }
104 env_new->crc = crc32(0, env_new->data, ENV_SIZE);
105
106 instr.len = CONFIG_ENV_SIZE;
107 #ifdef CONFIG_ENV_ADDR_FLEX
108 if (FLEXONENAND(this)) {
109 env_addr = CONFIG_ENV_ADDR_FLEX;
110 instr.len = CONFIG_ENV_SIZE_FLEX;
111 instr.len <<= onenand_mtd.eraseregions[0].numblocks == 1 ?
112 1 : 0;
113 }
114 #endif
115 instr.addr = env_addr;
116 instr.mtd = mtd;
117 if (mtd->erase(mtd, &instr)) {
118 printf("OneNAND: erase failed at 0x%08llx\n", env_addr);
119 return 1;
120 }
121
122 if (mtd->write(mtd, env_addr, ONENAND_MAX_ENV_SIZE, &retlen,
123 (u_char *)env_new)) {
124 printf("OneNAND: write failed at 0x%llx\n", instr.addr);
125 return 2;
126 }
127
128 return 0;
129 }
130
131 int env_init(void)
132 {
133 /* use default */
134 gd->env_addr = (ulong)&default_environment[0];
135 gd->env_valid = 1;
136
137 return 0;
138 }