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 |
Stefano Babic | a3110f0 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 54 | #endif /* CONFIG_ENV_OFFSET_REDUND */ |
Wolfgang Wegner | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 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) |
Wolfgang Wegner | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 72 | |
| 73 | int saveenv(void) |
| 74 | { |
Wolfgang Denk | ea882ba | 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 | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 83 | |
| 84 | if (!env_flash) { |
Stefano Babic | a3110f0 | 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 | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 92 | } |
| 93 | |
Wolfgang Denk | ea882ba | 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 | a3110f0 | 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 | 7319bca | 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 | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 139 | |
Stefano Babic | a3110f0 | 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 | 7319bca | 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 | a3110f0 | 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 | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 157 | |
Wolfgang Wegner | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 158 | puts("done\n"); |
| 159 | |
Stefano Babic | a3110f0 | 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 | 7319bca | 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 | a3110f0 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 176 | env_t *ep = NULL; |
Wolfgang Wegner | 7319bca | 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 | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 182 | tmp_env2 = (env_t *)malloc(CONFIG_ENV_SIZE); |
Wolfgang Denk | ea882ba | 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 | 7319bca | 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 | ea882ba | 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 | 7319bca | 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 | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 200 | if (ret) { |
| 201 | set_default_env("!spi_flash_read() failed"); |
Wolfgang Wegner | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 202 | goto err_read; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 203 | } |
Wolfgang Wegner | 7319bca | 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 | a3110f0 | 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 | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 223 | gd->env_valid = 1; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 224 | ep = tmp_env1; |
Wolfgang Wegner | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 225 | } else if (!crc1_ok && crc2_ok) { |
| 226 | gd->env_valid = 1; |
Wolfgang Wegner | 7319bca | 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 | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 229 | } else if (flag1 == OBSOLETE_FLAG && flag2 == ACTIVE_FLAG) { |
Stefano Babic | a3110f0 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 230 | gd->env_valid = 2; |
Wolfgang Wegner | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 231 | } else if (flag1 == flag2) { |
| 232 | gd->env_valid = 2; |
Wolfgang Wegner | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 233 | } else if (flag1 == 0xFF) { |
| 234 | gd->env_valid = 2; |
Wolfgang Wegner | 7319bca | 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; |
Stefano Babic | a3110f0 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | free(env_ptr); |
| 244 | |
| 245 | if (gd->env_valid == 1) |
| 246 | ep = tmp_env1; |
| 247 | else |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 248 | ep = tmp_env2; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 249 | |
Stefano Babic | a3110f0 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 250 | ret = env_import((char *)ep, 0); |
| 251 | if (!ret) { |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 252 | error("Cannot import environment: errno = %d\n", errno); |
Stefano Babic | a3110f0 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 253 | set_default_env("env_import failed"); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 254 | } |
| 255 | |
Wolfgang Wegner | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 256 | err_read: |
| 257 | spi_flash_free(env_flash); |
| 258 | env_flash = NULL; |
Wolfgang Wegner | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 259 | out: |
Stefano Babic | a3110f0 | 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 | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 264 | free(tmp_env1); |
| 265 | free(tmp_env2); |
Stefano Babic | a3110f0 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 266 | |
| 267 | return; |
Wolfgang Wegner | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 268 | } |
| 269 | #else |
Haavard Skinnemoen | 8c66497 | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 270 | int saveenv(void) |
| 271 | { |
Mike Frysinger | 5b3375a | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 272 | u32 saved_size, saved_offset; |
| 273 | char *saved_buffer = NULL; |
TsiChung Liew | 07efc9e | 2008-08-06 19:37:17 -0500 | [diff] [blame] | 274 | u32 sector = 1; |
Stefano Babic | 590084a | 2010-10-29 11:49:45 +0200 | [diff] [blame^] | 275 | int ret = 1; |
Stefano Babic | a3110f0 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 276 | env_t env_new; |
| 277 | char *res; |
| 278 | ssize_t len; |
TsiChung Liew | 07efc9e | 2008-08-06 19:37:17 -0500 | [diff] [blame] | 279 | |
Haavard Skinnemoen | 8c66497 | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 280 | if (!env_flash) { |
Stefano Babic | a3110f0 | 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 | 8c66497 | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 288 | } |
| 289 | |
Mike Frysinger | 5b3375a | 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) { |
Mike Frysinger | 5b3375a | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 296 | goto done; |
| 297 | } |
Stefano Babic | a3110f0 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 298 | ret = spi_flash_read(env_flash, saved_offset, |
| 299 | saved_size, saved_buffer); |
Mike Frysinger | 5b3375a | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 300 | if (ret) |
| 301 | goto done; |
| 302 | } |
| 303 | |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 304 | if (CONFIG_ENV_SIZE > CONFIG_ENV_SECT_SIZE) { |
| 305 | sector = CONFIG_ENV_SIZE / CONFIG_ENV_SECT_SIZE; |
| 306 | if (CONFIG_ENV_SIZE % CONFIG_ENV_SECT_SIZE) |
TsiChung Liew | 07efc9e | 2008-08-06 19:37:17 -0500 | [diff] [blame] | 307 | sector++; |
| 308 | } |
| 309 | |
Stefano Babic | a3110f0 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 310 | res = (char *)&env_new.data; |
| 311 | len = hexport('\0', &res, ENV_SIZE); |
| 312 | if (len < 0) { |
| 313 | error("Cannot export environment: errno = %d\n", errno); |
| 314 | goto done; |
| 315 | } |
| 316 | env_new.crc = crc32(0, env_new.data, ENV_SIZE); |
| 317 | |
Haavard Skinnemoen | 8c66497 | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 318 | puts("Erasing SPI flash..."); |
Stefano Babic | a3110f0 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 319 | ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET, |
| 320 | sector * CONFIG_ENV_SECT_SIZE); |
Mike Frysinger | 5b3375a | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 321 | if (ret) |
| 322 | goto done; |
Haavard Skinnemoen | 8c66497 | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 323 | |
| 324 | puts("Writing to SPI flash..."); |
Stefano Babic | a3110f0 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 325 | ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET, |
| 326 | CONFIG_ENV_SIZE, &env_new); |
Mike Frysinger | 5b3375a | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 327 | if (ret) |
| 328 | goto done; |
Haavard Skinnemoen | 8c66497 | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 329 | |
Mike Frysinger | 5b3375a | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 330 | if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) { |
Stefano Babic | a3110f0 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 331 | ret = spi_flash_write(env_flash, saved_offset, |
| 332 | saved_size, saved_buffer); |
Mike Frysinger | 5b3375a | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 333 | if (ret) |
| 334 | goto done; |
| 335 | } |
| 336 | |
| 337 | ret = 0; |
Haavard Skinnemoen | 8c66497 | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 338 | puts("done\n"); |
Mike Frysinger | 5b3375a | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 339 | |
| 340 | done: |
| 341 | if (saved_buffer) |
| 342 | free(saved_buffer); |
| 343 | return ret; |
Haavard Skinnemoen | 8c66497 | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | void env_relocate_spec(void) |
| 347 | { |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 348 | char buf[CONFIG_ENV_SIZE]; |
Haavard Skinnemoen | 8c66497 | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 349 | int ret; |
| 350 | |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 351 | env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS, |
| 352 | CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 353 | if (!env_flash) { |
| 354 | set_default_env("!spi_flash_probe() failed"); |
| 355 | return; |
| 356 | } |
Haavard Skinnemoen | 8c66497 | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 357 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 358 | ret = spi_flash_read(env_flash, |
| 359 | CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf); |
| 360 | if (ret) { |
| 361 | set_default_env("!spi_flash_read() failed"); |
| 362 | goto out; |
| 363 | } |
| 364 | |
| 365 | ret = env_import(buf, 1); |
| 366 | |
Haavard Skinnemoen | 8c66497 | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 367 | if (ret) |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 368 | gd->env_valid = 1; |
| 369 | out: |
Haavard Skinnemoen | 8c66497 | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 370 | spi_flash_free(env_flash); |
| 371 | env_flash = NULL; |
Haavard Skinnemoen | 8c66497 | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 372 | } |
Wolfgang Wegner | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 373 | #endif |
Haavard Skinnemoen | 8c66497 | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 374 | |
| 375 | int env_init(void) |
| 376 | { |
| 377 | /* SPI flash isn't usable before relocation */ |
| 378 | gd->env_addr = (ulong)&default_environment[0]; |
| 379 | gd->env_valid = 1; |
| 380 | |
| 381 | return 0; |
| 382 | } |