]> git.karo-electronics.de Git - karo-tx-uboot.git/blob - common/env_onenand.c
618c642f29cf350a775c6c902d34f081f4ff4fe5
[karo-tx-uboot.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
35 #include <linux/mtd/compat.h>
36 #include <linux/mtd/mtd.h>
37 #include <linux/mtd/onenand.h>
38
39 extern struct mtd_info onenand_mtd;
40 extern struct onenand_chip onenand_chip;
41
42 char *env_name_spec = "OneNAND";
43
44 #define ONENAND_MAX_ENV_SIZE    4096
45 #define ONENAND_ENV_SIZE(mtd)   (ONENAND_MAX_ENV_SIZE - ENV_HEADER_SIZE)
46
47 DECLARE_GLOBAL_DATA_PTR;
48
49 uchar env_get_char_spec(int index)
50 {
51         return (*((uchar *)(gd->env_addr + index)));
52 }
53
54 void env_relocate_spec(void)
55 {
56         struct mtd_info *mtd = &onenand_mtd;
57 #ifdef CONFIG_ENV_ADDR_FLEX
58         struct onenand_chip *this = &onenand_chip;
59 #endif
60         int rc;
61         size_t retlen;
62 #ifdef ENV_IS_EMBEDDED
63         char *buf = (char *)&environment;
64 #else
65         loff_t env_addr = CONFIG_ENV_ADDR;
66         char onenand_env[ONENAND_MAX_ENV_SIZE];
67         char *buf = (char *)&onenand_env[0];
68 #endif /* ENV_IS_EMBEDDED */
69
70 #ifndef ENV_IS_EMBEDDED
71 # ifdef CONFIG_ENV_ADDR_FLEX
72         if (FLEXONENAND(this))
73                 env_addr = CONFIG_ENV_ADDR_FLEX;
74 # endif
75         /* Check OneNAND exist */
76         if (mtd->writesize)
77                 /* Ignore read fail */
78                 mtd->read(mtd, env_addr, ONENAND_MAX_ENV_SIZE,
79                              &retlen, (u_char *)buf);
80         else
81                 mtd->writesize = MAX_ONENAND_PAGESIZE;
82 #endif /* !ENV_IS_EMBEDDED */
83
84         rc = env_import(buf, 1);
85         if (rc)
86                 gd->env_valid = 1;
87 }
88
89 int saveenv(void)
90 {
91         env_t   env_new;
92         ssize_t len;
93         char    *res;
94         struct mtd_info *mtd = &onenand_mtd;
95 #ifdef CONFIG_ENV_ADDR_FLEX
96         struct onenand_chip *this = &onenand_chip;
97 #endif
98         loff_t  env_addr = CONFIG_ENV_ADDR;
99         size_t  retlen;
100         struct erase_info instr = {
101                 .callback       = NULL,
102         };
103
104         res = (char *)&env_new.data;
105         len = hexport_r(&env_htab, '\0', &res, ENV_SIZE, 0, NULL);
106         if (len < 0) {
107                 error("Cannot export environment: errno = %d\n", errno);
108                 return 1;
109         }
110         env_new.crc = crc32(0, env_new.data, ENV_SIZE);
111
112         instr.len = CONFIG_ENV_SIZE;
113 #ifdef CONFIG_ENV_ADDR_FLEX
114         if (FLEXONENAND(this)) {
115                 env_addr = CONFIG_ENV_ADDR_FLEX;
116                 instr.len = CONFIG_ENV_SIZE_FLEX;
117                 instr.len <<= onenand_mtd.eraseregions[0].numblocks == 1 ?
118                                 1 : 0;
119         }
120 #endif
121         instr.addr = env_addr;
122         instr.mtd = mtd;
123         if (mtd->erase(mtd, &instr)) {
124                 printf("OneNAND: erase failed at 0x%08llx\n", env_addr);
125                 return 1;
126         }
127
128         if (mtd->write(mtd, env_addr, ONENAND_MAX_ENV_SIZE, &retlen,
129              (u_char *)&env_new)) {
130                 printf("OneNAND: write failed at 0x%llx\n", instr.addr);
131                 return 2;
132         }
133
134         return 0;
135 }
136
137 int env_init(void)
138 {
139         /* use default */
140         gd->env_addr = (ulong) & default_environment[0];
141         gd->env_valid = 1;
142
143         return 0;
144 }