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