Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 1 | /* |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 2 | * (C) Copyright 2000-2010 |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 3 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 4 | * |
| 5 | * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com> |
| 6 | * Andreas Heppel <aheppel@sysgo.de> |
| 7 | * |
| 8 | * (C) Copyright 2008 Atmel Corporation |
| 9 | * |
| 10 | * See file CREDITS for list of people who contributed to this |
| 11 | * project. |
| 12 | * |
| 13 | * This program is free software; you can redistribute it and/or |
| 14 | * modify it under the terms of the GNU General Public License as |
| 15 | * published by the Free Software Foundation; either version 2 of |
| 16 | * the License, or (at your option) any later version. |
| 17 | * |
| 18 | * This program is distributed in the hope that it will be useful, |
| 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | * GNU General Public License for more details. |
| 22 | * |
| 23 | * You should have received a copy of the GNU General Public License |
| 24 | * along with this program; if not, write to the Free Software |
| 25 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 26 | * MA 02111-1307 USA |
| 27 | */ |
| 28 | #include <common.h> |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 29 | #include <environment.h> |
Mike Frysinger | 9af10d5 | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 30 | #include <malloc.h> |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 31 | #include <spi_flash.h> |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 32 | #include <search.h> |
| 33 | #include <errno.h> |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 34 | |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 35 | #ifndef CONFIG_ENV_SPI_BUS |
| 36 | # define CONFIG_ENV_SPI_BUS 0 |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 37 | #endif |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 38 | #ifndef CONFIG_ENV_SPI_CS |
| 39 | # define CONFIG_ENV_SPI_CS 0 |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 40 | #endif |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 41 | #ifndef CONFIG_ENV_SPI_MAX_HZ |
| 42 | # define CONFIG_ENV_SPI_MAX_HZ 1000000 |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 43 | #endif |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 44 | #ifndef CONFIG_ENV_SPI_MODE |
| 45 | # define CONFIG_ENV_SPI_MODE SPI_MODE_3 |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 46 | #endif |
| 47 | |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 48 | #ifdef CONFIG_ENV_OFFSET_REDUND |
| 49 | static ulong env_offset = CONFIG_ENV_OFFSET; |
| 50 | static ulong env_new_offset = CONFIG_ENV_OFFSET_REDUND; |
| 51 | |
| 52 | #define ACTIVE_FLAG 1 |
| 53 | #define OBSOLETE_FLAG 0 |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 54 | #endif /* CONFIG_ENV_OFFSET_REDUND */ |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 55 | |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 56 | DECLARE_GLOBAL_DATA_PTR; |
| 57 | |
| 58 | /* references to names in env_common.c */ |
| 59 | extern uchar default_environment[]; |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 60 | |
| 61 | char * env_name_spec = "SPI Flash"; |
| 62 | env_t *env_ptr; |
| 63 | |
| 64 | static struct spi_flash *env_flash; |
| 65 | |
| 66 | uchar env_get_char_spec(int index) |
| 67 | { |
| 68 | return *((uchar *)(gd->env_addr + index)); |
| 69 | } |
| 70 | |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 71 | #if defined(CONFIG_ENV_OFFSET_REDUND) |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 72 | |
| 73 | int saveenv(void) |
| 74 | { |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 75 | env_t env_new; |
| 76 | ssize_t len; |
| 77 | char *res; |
| 78 | u32 saved_size, saved_offset; |
| 79 | char *saved_buffer = NULL; |
| 80 | u32 sector = 1; |
| 81 | int ret; |
| 82 | char flag = OBSOLETE_FLAG, new_flag = ACTIVE_FLAG; |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 83 | |
| 84 | if (!env_flash) { |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 85 | env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, |
| 86 | CONFIG_ENV_SPI_CS, |
| 87 | CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE); |
| 88 | if (!env_flash) { |
| 89 | set_default_env("!spi_flash_probe() failed"); |
| 90 | return 1; |
| 91 | } |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 92 | } |
| 93 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 94 | res = (char *)&env_new.data; |
| 95 | len = hexport('\0', &res, ENV_SIZE); |
| 96 | if (len < 0) { |
| 97 | error("Cannot export environment: errno = %d\n", errno); |
| 98 | return 1; |
| 99 | } |
| 100 | env_new.crc = crc32(0, env_new.data, ENV_SIZE); |
| 101 | env_new.flags = ACTIVE_FLAG; |
| 102 | |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 103 | if (gd->env_valid == 1) { |
| 104 | env_new_offset = CONFIG_ENV_OFFSET_REDUND; |
| 105 | env_offset = CONFIG_ENV_OFFSET; |
| 106 | } else { |
| 107 | env_new_offset = CONFIG_ENV_OFFSET; |
| 108 | env_offset = CONFIG_ENV_OFFSET_REDUND; |
| 109 | } |
| 110 | |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 111 | /* Is the sector larger than the env (i.e. embedded) */ |
| 112 | if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) { |
| 113 | saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE; |
| 114 | saved_offset = env_new_offset + CONFIG_ENV_SIZE; |
| 115 | saved_buffer = malloc(saved_size); |
| 116 | if (!saved_buffer) { |
| 117 | ret = 1; |
| 118 | goto done; |
| 119 | } |
| 120 | ret = spi_flash_read(env_flash, saved_offset, |
| 121 | saved_size, saved_buffer); |
| 122 | if (ret) |
| 123 | goto done; |
| 124 | } |
| 125 | |
| 126 | if (CONFIG_ENV_SIZE > CONFIG_ENV_SECT_SIZE) { |
| 127 | sector = CONFIG_ENV_SIZE / CONFIG_ENV_SECT_SIZE; |
| 128 | if (CONFIG_ENV_SIZE % CONFIG_ENV_SECT_SIZE) |
| 129 | sector++; |
| 130 | } |
| 131 | |
| 132 | puts("Erasing SPI flash..."); |
| 133 | ret = spi_flash_erase(env_flash, env_new_offset, |
| 134 | sector * CONFIG_ENV_SECT_SIZE); |
| 135 | if (ret) |
| 136 | goto done; |
| 137 | |
| 138 | puts("Writing to SPI flash..."); |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 139 | |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 140 | ret = spi_flash_write(env_flash, env_new_offset, |
| 141 | CONFIG_ENV_SIZE, &env_new); |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 142 | if (ret) |
| 143 | goto done; |
| 144 | |
| 145 | if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) { |
| 146 | ret = spi_flash_write(env_flash, saved_offset, |
| 147 | saved_size, saved_buffer); |
| 148 | if (ret) |
| 149 | goto done; |
| 150 | } |
| 151 | |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 152 | ret = spi_flash_write(env_flash, |
| 153 | env_offset + offsetof(env_t, flags), |
| 154 | sizeof(env_new.flags), &flag); |
| 155 | if (ret) |
| 156 | goto done; |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 157 | |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 158 | puts("done\n"); |
| 159 | |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 160 | gd->env_valid = (gd->env_valid == 2 ? 1 : 2); |
| 161 | |
| 162 | printf("Valid environment: %d\n", gd->env_valid); |
| 163 | |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 164 | done: |
| 165 | if (saved_buffer) |
| 166 | free(saved_buffer); |
| 167 | return ret; |
| 168 | } |
| 169 | |
| 170 | void env_relocate_spec(void) |
| 171 | { |
| 172 | int ret; |
| 173 | int crc1_ok = 0, crc2_ok = 0; |
| 174 | env_t *tmp_env1 = NULL; |
| 175 | env_t *tmp_env2 = NULL; |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 176 | env_t *ep = NULL; |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 177 | uchar flag1, flag2; |
| 178 | /* current_env is set only in case both areas are valid! */ |
| 179 | int current_env = 0; |
| 180 | |
| 181 | tmp_env1 = (env_t *)malloc(CONFIG_ENV_SIZE); |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 182 | tmp_env2 = (env_t *)malloc(CONFIG_ENV_SIZE); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 183 | |
| 184 | if (!tmp_env1 || !tmp_env2) { |
| 185 | free(tmp_env1); |
| 186 | free(tmp_env2); |
| 187 | set_default_env("!malloc() failed"); |
| 188 | return; |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS, |
| 192 | CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 193 | if (!env_flash) { |
| 194 | set_default_env("!spi_flash_probe() failed"); |
| 195 | return; |
| 196 | } |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 197 | |
| 198 | ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET, |
| 199 | CONFIG_ENV_SIZE, tmp_env1); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 200 | if (ret) { |
| 201 | set_default_env("!spi_flash_read() failed"); |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 202 | goto err_read; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 203 | } |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 204 | |
| 205 | if (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc) |
| 206 | crc1_ok = 1; |
| 207 | flag1 = tmp_env1->flags; |
| 208 | |
| 209 | ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET_REDUND, |
| 210 | CONFIG_ENV_SIZE, tmp_env2); |
| 211 | if (!ret) { |
| 212 | if (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc) |
| 213 | crc2_ok = 1; |
| 214 | flag2 = tmp_env2->flags; |
| 215 | } |
| 216 | |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 217 | if (!crc1_ok && !crc2_ok) { |
| 218 | free(tmp_env1); |
| 219 | free(tmp_env2); |
| 220 | set_default_env("!bad CRC"); |
| 221 | return; |
| 222 | } else if (crc1_ok && !crc2_ok) { |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 223 | gd->env_valid = 1; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 224 | ep = tmp_env1; |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 225 | } else if (!crc1_ok && crc2_ok) { |
| 226 | gd->env_valid = 1; |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 227 | } else if (flag1 == ACTIVE_FLAG && flag2 == OBSOLETE_FLAG) { |
| 228 | gd->env_valid = 1; |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 229 | } else if (flag1 == OBSOLETE_FLAG && flag2 == ACTIVE_FLAG) { |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 230 | gd->env_valid = 2; |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 231 | } else if (flag1 == flag2) { |
| 232 | gd->env_valid = 2; |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 233 | } else if (flag1 == 0xFF) { |
| 234 | gd->env_valid = 2; |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 235 | } else { |
| 236 | /* |
| 237 | * this differs from code in env_flash.c, but I think a sane |
| 238 | * default path is desirable. |
| 239 | */ |
| 240 | gd->env_valid = 2; |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 241 | } |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 242 | |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 243 | free(env_ptr); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 244 | |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 245 | if (gd->env_valid == 1) |
| 246 | ep = tmp_env1; |
| 247 | else |
| 248 | ep = tmp_env2; |
| 249 | |
| 250 | ret = env_import((char *)ep, 0); |
| 251 | if (!ret) { |
| 252 | error("Cannot import environment: errno = %d\n", errno); |
| 253 | set_default_env("env_import failed"); |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 254 | } |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 255 | |
| 256 | err_read: |
| 257 | spi_flash_free(env_flash); |
| 258 | env_flash = NULL; |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 259 | out: |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 260 | if (tmp_env1) |
| 261 | free(tmp_env1); |
| 262 | if (tmp_env2) |
| 263 | free(tmp_env2); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 264 | free(tmp_env1); |
| 265 | free(tmp_env2); |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 266 | |
| 267 | return; |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 268 | } |
| 269 | #else |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 270 | int saveenv(void) |
| 271 | { |
Mike Frysinger | 9af10d5 | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 272 | u32 saved_size, saved_offset; |
| 273 | char *saved_buffer = NULL; |
TsiChung Liew | 08792d7 | 2008-08-06 19:37:17 -0500 | [diff] [blame] | 274 | u32 sector = 1; |
Mike Frysinger | 9af10d5 | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 275 | int ret; |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 276 | env_t env_new; |
| 277 | char *res; |
| 278 | ssize_t len; |
TsiChung Liew | 08792d7 | 2008-08-06 19:37:17 -0500 | [diff] [blame] | 279 | |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 280 | if (!env_flash) { |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 281 | env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, |
| 282 | CONFIG_ENV_SPI_CS, |
| 283 | CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE); |
| 284 | if (!env_flash) { |
| 285 | set_default_env("!spi_flash_probe() failed"); |
| 286 | return 1; |
| 287 | } |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 288 | } |
| 289 | |
Mike Frysinger | 9af10d5 | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 290 | /* Is the sector larger than the env (i.e. embedded) */ |
| 291 | if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) { |
| 292 | saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE; |
| 293 | saved_offset = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE; |
| 294 | saved_buffer = malloc(saved_size); |
| 295 | if (!saved_buffer) { |
| 296 | ret = 1; |
| 297 | goto done; |
| 298 | } |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 299 | ret = spi_flash_read(env_flash, saved_offset, |
| 300 | saved_size, saved_buffer); |
Mike Frysinger | 9af10d5 | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 301 | if (ret) |
| 302 | goto done; |
| 303 | } |
| 304 | |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 305 | if (CONFIG_ENV_SIZE > CONFIG_ENV_SECT_SIZE) { |
| 306 | sector = CONFIG_ENV_SIZE / CONFIG_ENV_SECT_SIZE; |
| 307 | if (CONFIG_ENV_SIZE % CONFIG_ENV_SECT_SIZE) |
TsiChung Liew | 08792d7 | 2008-08-06 19:37:17 -0500 | [diff] [blame] | 308 | sector++; |
| 309 | } |
| 310 | |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 311 | res = (char *)&env_new.data; |
| 312 | len = hexport('\0', &res, ENV_SIZE); |
| 313 | if (len < 0) { |
| 314 | error("Cannot export environment: errno = %d\n", errno); |
| 315 | goto done; |
| 316 | } |
| 317 | env_new.crc = crc32(0, env_new.data, ENV_SIZE); |
| 318 | |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 319 | puts("Erasing SPI flash..."); |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 320 | ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET, |
| 321 | sector * CONFIG_ENV_SECT_SIZE); |
Mike Frysinger | 9af10d5 | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 322 | if (ret) |
| 323 | goto done; |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 324 | |
| 325 | puts("Writing to SPI flash..."); |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 326 | ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET, |
| 327 | CONFIG_ENV_SIZE, &env_new); |
Mike Frysinger | 9af10d5 | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 328 | if (ret) |
| 329 | goto done; |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 330 | |
Mike Frysinger | 9af10d5 | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 331 | if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) { |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 332 | ret = spi_flash_write(env_flash, saved_offset, |
| 333 | saved_size, saved_buffer); |
Mike Frysinger | 9af10d5 | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 334 | if (ret) |
| 335 | goto done; |
| 336 | } |
| 337 | |
| 338 | ret = 0; |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 339 | puts("done\n"); |
Mike Frysinger | 9af10d5 | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 340 | |
| 341 | done: |
| 342 | if (saved_buffer) |
| 343 | free(saved_buffer); |
| 344 | return ret; |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | void env_relocate_spec(void) |
| 348 | { |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 349 | char buf[CONFIG_ENV_SIZE]; |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 350 | int ret; |
| 351 | |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 352 | env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS, |
| 353 | CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 354 | if (!env_flash) { |
| 355 | set_default_env("!spi_flash_probe() failed"); |
| 356 | return; |
| 357 | } |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 358 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 359 | ret = spi_flash_read(env_flash, |
| 360 | CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf); |
| 361 | if (ret) { |
| 362 | set_default_env("!spi_flash_read() failed"); |
| 363 | goto out; |
| 364 | } |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 365 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 366 | ret = env_import(buf, 1); |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 367 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 368 | if (ret) |
| 369 | gd->env_valid = 1; |
| 370 | out: |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 371 | spi_flash_free(env_flash); |
| 372 | env_flash = NULL; |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 373 | } |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 374 | #endif |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 375 | |
| 376 | int env_init(void) |
| 377 | { |
| 378 | /* SPI flash isn't usable before relocation */ |
| 379 | gd->env_addr = (ulong)&default_environment[0]; |
| 380 | gd->env_valid = 1; |
| 381 | |
| 382 | return 0; |
| 383 | } |