Haavard Skinnemoen | 8c66497 | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 1 | /* |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 2 | * (C) Copyright 2000-2010 |
Haavard Skinnemoen | 8c66497 | 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 | 8c66497 | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 29 | #include <environment.h> |
Mike Frysinger | 5b3375a | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 30 | #include <malloc.h> |
Haavard Skinnemoen | 8c66497 | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 31 | #include <spi_flash.h> |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 32 | #include <search.h> |
| 33 | #include <errno.h> |
Haavard Skinnemoen | 8c66497 | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 34 | |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 35 | #ifndef CONFIG_ENV_SPI_BUS |
| 36 | # define CONFIG_ENV_SPI_BUS 0 |
Haavard Skinnemoen | 8c66497 | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 37 | #endif |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 38 | #ifndef CONFIG_ENV_SPI_CS |
| 39 | # define CONFIG_ENV_SPI_CS 0 |
Haavard Skinnemoen | 8c66497 | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 40 | #endif |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 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 | 8c66497 | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 43 | #endif |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 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 | 8c66497 | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 46 | #endif |
| 47 | |
Wolfgang Wegner | 7319bca | 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 |
| 54 | #endif /* CONFIG_ENV_ADDR_REDUND */ |
| 55 | |
Haavard Skinnemoen | 8c66497 | 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 | 8c66497 | 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 | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 71 | #if defined(CONFIG_ENV_OFFSET_REDUND) |
| 72 | void swap_env(void) |
| 73 | { |
| 74 | ulong tmp_offset = env_offset; |
| 75 | |
| 76 | env_offset = env_new_offset; |
| 77 | env_new_offset = tmp_offset; |
| 78 | } |
| 79 | |
| 80 | int saveenv(void) |
| 81 | { |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 82 | env_t env_new; |
| 83 | ssize_t len; |
| 84 | char *res; |
| 85 | u32 saved_size, saved_offset; |
| 86 | char *saved_buffer = NULL; |
| 87 | u32 sector = 1; |
| 88 | int ret; |
| 89 | char flag = OBSOLETE_FLAG, new_flag = ACTIVE_FLAG; |
Wolfgang Wegner | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 90 | |
| 91 | if (!env_flash) { |
| 92 | puts("Environment SPI flash not initialized\n"); |
| 93 | return 1; |
| 94 | } |
| 95 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 96 | res = (char *)&env_new.data; |
| 97 | len = hexport('\0', &res, ENV_SIZE); |
| 98 | if (len < 0) { |
| 99 | error("Cannot export environment: errno = %d\n", errno); |
| 100 | return 1; |
| 101 | } |
| 102 | env_new.crc = crc32(0, env_new.data, ENV_SIZE); |
| 103 | env_new.flags = ACTIVE_FLAG; |
| 104 | |
Wolfgang Wegner | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 105 | /* Is the sector larger than the env (i.e. embedded) */ |
| 106 | if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) { |
| 107 | saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE; |
| 108 | saved_offset = env_new_offset + CONFIG_ENV_SIZE; |
| 109 | saved_buffer = malloc(saved_size); |
| 110 | if (!saved_buffer) { |
| 111 | ret = 1; |
| 112 | goto done; |
| 113 | } |
| 114 | ret = spi_flash_read(env_flash, saved_offset, |
| 115 | saved_size, saved_buffer); |
| 116 | if (ret) |
| 117 | goto done; |
| 118 | } |
| 119 | |
| 120 | if (CONFIG_ENV_SIZE > CONFIG_ENV_SECT_SIZE) { |
| 121 | sector = CONFIG_ENV_SIZE / CONFIG_ENV_SECT_SIZE; |
| 122 | if (CONFIG_ENV_SIZE % CONFIG_ENV_SECT_SIZE) |
| 123 | sector++; |
| 124 | } |
| 125 | |
| 126 | puts("Erasing SPI flash..."); |
| 127 | ret = spi_flash_erase(env_flash, env_new_offset, |
| 128 | sector * CONFIG_ENV_SECT_SIZE); |
| 129 | if (ret) |
| 130 | goto done; |
| 131 | |
| 132 | puts("Writing to SPI flash..."); |
| 133 | ret = spi_flash_write(env_flash, |
| 134 | env_new_offset + offsetof(env_t, data), |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 135 | sizeof(env_new.data), env_new.data); |
Wolfgang Wegner | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 136 | if (ret) |
| 137 | goto done; |
| 138 | |
| 139 | ret = spi_flash_write(env_flash, |
| 140 | env_new_offset + offsetof(env_t, crc), |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 141 | sizeof(env_new.crc), &env_new.crc); |
Wolfgang Wegner | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 142 | if (ret) |
| 143 | goto done; |
| 144 | |
| 145 | ret = spi_flash_write(env_flash, |
| 146 | env_offset + offsetof(env_t, flags), |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 147 | sizeof(env_new.flags), &flag); |
Wolfgang Wegner | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 148 | if (ret) |
| 149 | goto done; |
| 150 | |
| 151 | ret = spi_flash_write(env_flash, |
| 152 | env_new_offset + offsetof(env_t, flags), |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 153 | sizeof(env_new.flags), &new_flag); |
Wolfgang Wegner | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 154 | if (ret) |
| 155 | goto done; |
| 156 | |
| 157 | if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) { |
| 158 | ret = spi_flash_write(env_flash, saved_offset, |
| 159 | saved_size, saved_buffer); |
| 160 | if (ret) |
| 161 | goto done; |
| 162 | } |
| 163 | |
| 164 | swap_env(); |
| 165 | |
| 166 | ret = 0; |
| 167 | puts("done\n"); |
| 168 | |
| 169 | done: |
| 170 | if (saved_buffer) |
| 171 | free(saved_buffer); |
| 172 | return ret; |
| 173 | } |
| 174 | |
| 175 | void env_relocate_spec(void) |
| 176 | { |
| 177 | int ret; |
| 178 | int crc1_ok = 0, crc2_ok = 0; |
| 179 | env_t *tmp_env1 = NULL; |
| 180 | env_t *tmp_env2 = NULL; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 181 | env_t ep; |
Wolfgang Wegner | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 182 | uchar flag1, flag2; |
| 183 | /* current_env is set only in case both areas are valid! */ |
| 184 | int current_env = 0; |
| 185 | |
| 186 | tmp_env1 = (env_t *)malloc(CONFIG_ENV_SIZE); |
Wolfgang Wegner | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 187 | tmp_env2 = (env_t *)malloc(CONFIG_ENV_SIZE); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 188 | |
| 189 | if (!tmp_env1 || !tmp_env2) { |
| 190 | free(tmp_env1); |
| 191 | free(tmp_env2); |
| 192 | set_default_env("!malloc() failed"); |
| 193 | return; |
Wolfgang Wegner | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS, |
| 197 | CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 198 | if (!env_flash) { |
| 199 | set_default_env("!spi_flash_probe() failed"); |
| 200 | return; |
| 201 | } |
Wolfgang Wegner | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 202 | |
| 203 | ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET, |
| 204 | CONFIG_ENV_SIZE, tmp_env1); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 205 | if (ret) { |
| 206 | set_default_env("!spi_flash_read() failed"); |
Wolfgang Wegner | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 207 | goto err_read; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 208 | } |
Wolfgang Wegner | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 209 | |
| 210 | if (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc) |
| 211 | crc1_ok = 1; |
| 212 | flag1 = tmp_env1->flags; |
| 213 | |
| 214 | ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET_REDUND, |
| 215 | CONFIG_ENV_SIZE, tmp_env2); |
| 216 | if (!ret) { |
| 217 | if (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc) |
| 218 | crc2_ok = 1; |
| 219 | flag2 = tmp_env2->flags; |
| 220 | } |
| 221 | |
| 222 | if (!crc1_ok && !crc2_ok) |
| 223 | goto err_crc; |
| 224 | else if (crc1_ok && !crc2_ok) { |
| 225 | gd->env_valid = 1; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 226 | ep = tmp_env1; |
Wolfgang Wegner | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 227 | } else if (!crc1_ok && crc2_ok) { |
| 228 | gd->env_valid = 1; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 229 | ep = tmp_env2; |
Wolfgang Wegner | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 230 | swap_env(); |
| 231 | } else if (flag1 == ACTIVE_FLAG && flag2 == OBSOLETE_FLAG) { |
| 232 | gd->env_valid = 1; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 233 | ep = tmp_env1; |
Wolfgang Wegner | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 234 | } else if (flag1 == OBSOLETE_FLAG && flag2 == ACTIVE_FLAG) { |
| 235 | gd->env_valid = 1; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 236 | ep = tmp_env2; |
Wolfgang Wegner | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 237 | swap_env(); |
| 238 | } else if (flag1 == flag2) { |
| 239 | gd->env_valid = 2; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 240 | ep = tmp_env1; |
Wolfgang Wegner | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 241 | current_env = 1; |
| 242 | } else if (flag1 == 0xFF) { |
| 243 | gd->env_valid = 2; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 244 | ep = tmp_env1; |
Wolfgang Wegner | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 245 | current_env = 1; |
| 246 | } else { |
| 247 | /* |
| 248 | * this differs from code in env_flash.c, but I think a sane |
| 249 | * default path is desirable. |
| 250 | */ |
| 251 | gd->env_valid = 2; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 252 | ep = tmp_env2; |
Wolfgang Wegner | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 253 | swap_env(); |
| 254 | current_env = 2; |
| 255 | } |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 256 | |
| 257 | rc = env_import((char *)ep, 0); |
| 258 | if (!rc) { |
| 259 | error("Cannot import environment: errno = %d\n", errno); |
| 260 | goto out; |
| 261 | } |
| 262 | |
Wolfgang Wegner | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 263 | if (current_env == 1) { |
| 264 | if (flag2 != OBSOLETE_FLAG) { |
| 265 | flag2 = OBSOLETE_FLAG; |
| 266 | spi_flash_write(env_flash, |
| 267 | env_new_offset + offsetof(env_t, flags), |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 268 | sizeof(env_new.flags), &flag2); |
Wolfgang Wegner | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 269 | } |
| 270 | if (flag1 != ACTIVE_FLAG) { |
| 271 | flag1 = ACTIVE_FLAG; |
| 272 | spi_flash_write(env_flash, |
| 273 | env_offset + offsetof(env_t, flags), |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 274 | sizeof(env_new.flags), &flag1); |
Wolfgang Wegner | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 275 | } |
| 276 | } else if (current_env == 2) { |
| 277 | if (flag1 != OBSOLETE_FLAG) { |
| 278 | flag1 = OBSOLETE_FLAG; |
| 279 | spi_flash_write(env_flash, |
| 280 | env_new_offset + offsetof(env_t, flags), |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 281 | sizeof(env_new.flags), &flag1); |
Wolfgang Wegner | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 282 | } |
| 283 | if (flag2 != ACTIVE_FLAG) { |
| 284 | flag2 = ACTIVE_FLAG; |
| 285 | spi_flash_write(env_flash, |
| 286 | env_offset + offsetof(env_t, flags), |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 287 | sizeof(env_new.flags), &flag2); |
Wolfgang Wegner | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 288 | } |
| 289 | } |
| 290 | if (gd->env_valid == 2) { |
| 291 | puts("*** Warning - some problems detected " |
| 292 | "reading environment; recovered successfully\n\n"); |
| 293 | } |
| 294 | if (tmp_env1) |
| 295 | free(tmp_env1); |
| 296 | if (tmp_env2) |
| 297 | free(tmp_env2); |
| 298 | return; |
| 299 | |
| 300 | err_read: |
| 301 | spi_flash_free(env_flash); |
| 302 | env_flash = NULL; |
Wolfgang Wegner | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 303 | out: |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 304 | free(tmp_env1); |
| 305 | free(tmp_env2); |
Wolfgang Wegner | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 306 | } |
| 307 | #else |
Haavard Skinnemoen | 8c66497 | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 308 | int saveenv(void) |
| 309 | { |
Mike Frysinger | 5b3375a | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 310 | u32 saved_size, saved_offset; |
| 311 | char *saved_buffer = NULL; |
TsiChung Liew | 07efc9e | 2008-08-06 19:37:17 -0500 | [diff] [blame] | 312 | u32 sector = 1; |
Mike Frysinger | 5b3375a | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 313 | int ret; |
TsiChung Liew | 07efc9e | 2008-08-06 19:37:17 -0500 | [diff] [blame] | 314 | |
Haavard Skinnemoen | 8c66497 | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 315 | if (!env_flash) { |
| 316 | puts("Environment SPI flash not initialized\n"); |
| 317 | return 1; |
| 318 | } |
| 319 | |
Mike Frysinger | 5b3375a | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 320 | /* Is the sector larger than the env (i.e. embedded) */ |
| 321 | if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) { |
| 322 | saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE; |
| 323 | saved_offset = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE; |
| 324 | saved_buffer = malloc(saved_size); |
| 325 | if (!saved_buffer) { |
| 326 | ret = 1; |
| 327 | goto done; |
| 328 | } |
| 329 | ret = spi_flash_read(env_flash, saved_offset, saved_size, saved_buffer); |
| 330 | if (ret) |
| 331 | goto done; |
| 332 | } |
| 333 | |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 334 | if (CONFIG_ENV_SIZE > CONFIG_ENV_SECT_SIZE) { |
| 335 | sector = CONFIG_ENV_SIZE / CONFIG_ENV_SECT_SIZE; |
| 336 | if (CONFIG_ENV_SIZE % CONFIG_ENV_SECT_SIZE) |
TsiChung Liew | 07efc9e | 2008-08-06 19:37:17 -0500 | [diff] [blame] | 337 | sector++; |
| 338 | } |
| 339 | |
Haavard Skinnemoen | 8c66497 | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 340 | puts("Erasing SPI flash..."); |
Mike Frysinger | 5b3375a | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 341 | ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET, sector * CONFIG_ENV_SECT_SIZE); |
| 342 | if (ret) |
| 343 | goto done; |
Haavard Skinnemoen | 8c66497 | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 344 | |
| 345 | puts("Writing to SPI flash..."); |
Mike Frysinger | 5b3375a | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 346 | ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, env_ptr); |
| 347 | if (ret) |
| 348 | goto done; |
Haavard Skinnemoen | 8c66497 | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 349 | |
Mike Frysinger | 5b3375a | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 350 | if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) { |
| 351 | ret = spi_flash_write(env_flash, saved_offset, saved_size, saved_buffer); |
| 352 | if (ret) |
| 353 | goto done; |
| 354 | } |
| 355 | |
| 356 | ret = 0; |
Haavard Skinnemoen | 8c66497 | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 357 | puts("done\n"); |
Mike Frysinger | 5b3375a | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 358 | |
| 359 | done: |
| 360 | if (saved_buffer) |
| 361 | free(saved_buffer); |
| 362 | return ret; |
Haavard Skinnemoen | 8c66497 | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | void env_relocate_spec(void) |
| 366 | { |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 367 | char buf[CONFIG_ENV_SIZE]; |
Haavard Skinnemoen | 8c66497 | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 368 | int ret; |
| 369 | |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 370 | env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS, |
| 371 | CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 372 | if (!env_flash) { |
| 373 | set_default_env("!spi_flash_probe() failed"); |
| 374 | return; |
| 375 | } |
Haavard Skinnemoen | 8c66497 | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 376 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 377 | ret = spi_flash_read(env_flash, |
| 378 | CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf); |
| 379 | if (ret) { |
| 380 | set_default_env("!spi_flash_read() failed"); |
| 381 | goto out; |
| 382 | } |
| 383 | |
| 384 | ret = env_import(buf, 1); |
| 385 | |
Haavard Skinnemoen | 8c66497 | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 386 | if (ret) |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 387 | gd->env_valid = 1; |
| 388 | out: |
Haavard Skinnemoen | 8c66497 | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 389 | spi_flash_free(env_flash); |
| 390 | env_flash = NULL; |
Haavard Skinnemoen | 8c66497 | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 391 | } |
Wolfgang Wegner | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 392 | #endif |
Haavard Skinnemoen | 8c66497 | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 393 | |
| 394 | int env_init(void) |
| 395 | { |
| 396 | /* SPI flash isn't usable before relocation */ |
| 397 | gd->env_addr = (ulong)&default_environment[0]; |
| 398 | gd->env_valid = 1; |
| 399 | |
| 400 | return 0; |
| 401 | } |