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 | * |
Jagannadha Sutradharudu Teki | b1df922 | 2013-07-29 22:12:52 +0530 | [diff] [blame] | 10 | * SPDX-License-Identifier: GPL-2.0+ |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 11 | */ |
| 12 | #include <common.h> |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 13 | #include <environment.h> |
Mike Frysinger | 9af10d5 | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 14 | #include <malloc.h> |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 15 | #include <spi_flash.h> |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 16 | #include <search.h> |
| 17 | #include <errno.h> |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 18 | |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 19 | #ifndef CONFIG_ENV_SPI_BUS |
| 20 | # define CONFIG_ENV_SPI_BUS 0 |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 21 | #endif |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 22 | #ifndef CONFIG_ENV_SPI_CS |
Igor Grinberg | c3c1bed | 2011-11-07 01:14:08 +0000 | [diff] [blame] | 23 | # define CONFIG_ENV_SPI_CS 0 |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 24 | #endif |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 25 | #ifndef CONFIG_ENV_SPI_MAX_HZ |
| 26 | # define CONFIG_ENV_SPI_MAX_HZ 1000000 |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 27 | #endif |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 28 | #ifndef CONFIG_ENV_SPI_MODE |
| 29 | # define CONFIG_ENV_SPI_MODE SPI_MODE_3 |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 30 | #endif |
| 31 | |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 32 | #ifdef CONFIG_ENV_OFFSET_REDUND |
Igor Grinberg | c3c1bed | 2011-11-07 01:14:08 +0000 | [diff] [blame] | 33 | static ulong env_offset = CONFIG_ENV_OFFSET; |
| 34 | static ulong env_new_offset = CONFIG_ENV_OFFSET_REDUND; |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 35 | |
Igor Grinberg | c3c1bed | 2011-11-07 01:14:08 +0000 | [diff] [blame] | 36 | #define ACTIVE_FLAG 1 |
| 37 | #define OBSOLETE_FLAG 0 |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 38 | #endif /* CONFIG_ENV_OFFSET_REDUND */ |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 39 | |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 40 | DECLARE_GLOBAL_DATA_PTR; |
| 41 | |
Igor Grinberg | c3c1bed | 2011-11-07 01:14:08 +0000 | [diff] [blame] | 42 | char *env_name_spec = "SPI Flash"; |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 43 | |
| 44 | static struct spi_flash *env_flash; |
| 45 | |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 46 | #if defined(CONFIG_ENV_OFFSET_REDUND) |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 47 | int saveenv(void) |
| 48 | { |
Tom Rini | a840b8e | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 49 | env_t env_new; |
Marek Vasut | d73c129 | 2014-03-05 19:59:50 +0100 | [diff] [blame] | 50 | char *saved_buffer = NULL, flag = OBSOLETE_FLAG; |
Igor Grinberg | c3c1bed | 2011-11-07 01:14:08 +0000 | [diff] [blame] | 51 | u32 saved_size, saved_offset, sector = 1; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 52 | int ret; |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 53 | |
| 54 | if (!env_flash) { |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 55 | env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, |
| 56 | CONFIG_ENV_SPI_CS, |
| 57 | CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE); |
| 58 | if (!env_flash) { |
| 59 | set_default_env("!spi_flash_probe() failed"); |
| 60 | return 1; |
| 61 | } |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 62 | } |
| 63 | |
Marek Vasut | d73c129 | 2014-03-05 19:59:50 +0100 | [diff] [blame] | 64 | ret = env_export(&env_new); |
| 65 | if (ret) |
| 66 | return ret; |
Tom Rini | a840b8e | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 67 | env_new.flags = ACTIVE_FLAG; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 68 | |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 69 | if (gd->env_valid == 1) { |
| 70 | env_new_offset = CONFIG_ENV_OFFSET_REDUND; |
| 71 | env_offset = CONFIG_ENV_OFFSET; |
| 72 | } else { |
| 73 | env_new_offset = CONFIG_ENV_OFFSET; |
| 74 | env_offset = CONFIG_ENV_OFFSET_REDUND; |
| 75 | } |
| 76 | |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 77 | /* Is the sector larger than the env (i.e. embedded) */ |
| 78 | if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) { |
| 79 | saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE; |
| 80 | saved_offset = env_new_offset + CONFIG_ENV_SIZE; |
| 81 | saved_buffer = malloc(saved_size); |
| 82 | if (!saved_buffer) { |
| 83 | ret = 1; |
| 84 | goto done; |
| 85 | } |
| 86 | ret = spi_flash_read(env_flash, saved_offset, |
| 87 | saved_size, saved_buffer); |
| 88 | if (ret) |
| 89 | goto done; |
| 90 | } |
| 91 | |
| 92 | if (CONFIG_ENV_SIZE > CONFIG_ENV_SECT_SIZE) { |
| 93 | sector = CONFIG_ENV_SIZE / CONFIG_ENV_SECT_SIZE; |
| 94 | if (CONFIG_ENV_SIZE % CONFIG_ENV_SECT_SIZE) |
| 95 | sector++; |
| 96 | } |
| 97 | |
| 98 | puts("Erasing SPI flash..."); |
| 99 | ret = spi_flash_erase(env_flash, env_new_offset, |
| 100 | sector * CONFIG_ENV_SECT_SIZE); |
| 101 | if (ret) |
| 102 | goto done; |
| 103 | |
| 104 | puts("Writing to SPI flash..."); |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 105 | |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 106 | ret = spi_flash_write(env_flash, env_new_offset, |
Tom Rini | a840b8e | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 107 | CONFIG_ENV_SIZE, &env_new); |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 108 | if (ret) |
| 109 | goto done; |
| 110 | |
| 111 | if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) { |
| 112 | ret = spi_flash_write(env_flash, saved_offset, |
| 113 | saved_size, saved_buffer); |
| 114 | if (ret) |
| 115 | goto done; |
| 116 | } |
| 117 | |
Igor Grinberg | c3c1bed | 2011-11-07 01:14:08 +0000 | [diff] [blame] | 118 | ret = spi_flash_write(env_flash, env_offset + offsetof(env_t, flags), |
Tom Rini | a840b8e | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 119 | sizeof(env_new.flags), &flag); |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 120 | if (ret) |
| 121 | goto done; |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 122 | |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 123 | puts("done\n"); |
| 124 | |
Igor Grinberg | c3c1bed | 2011-11-07 01:14:08 +0000 | [diff] [blame] | 125 | gd->env_valid = gd->env_valid == 2 ? 1 : 2; |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 126 | |
thomas.langer@lantiq.com | 97dfdcf | 2011-03-29 02:35:14 +0000 | [diff] [blame] | 127 | printf("Valid environment: %d\n", (int)gd->env_valid); |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 128 | |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 129 | done: |
| 130 | if (saved_buffer) |
| 131 | free(saved_buffer); |
Igor Grinberg | c3c1bed | 2011-11-07 01:14:08 +0000 | [diff] [blame] | 132 | |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 133 | return ret; |
| 134 | } |
| 135 | |
| 136 | void env_relocate_spec(void) |
| 137 | { |
| 138 | int ret; |
| 139 | int crc1_ok = 0, crc2_ok = 0; |
| 140 | env_t *tmp_env1 = NULL; |
| 141 | env_t *tmp_env2 = NULL; |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 142 | env_t *ep = NULL; |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 143 | |
| 144 | tmp_env1 = (env_t *)malloc(CONFIG_ENV_SIZE); |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 145 | tmp_env2 = (env_t *)malloc(CONFIG_ENV_SIZE); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 146 | |
| 147 | if (!tmp_env1 || !tmp_env2) { |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 148 | set_default_env("!malloc() failed"); |
thomas.langer@lantiq.com | 97dfdcf | 2011-03-29 02:35:14 +0000 | [diff] [blame] | 149 | goto out; |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS, |
| 153 | CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 154 | if (!env_flash) { |
| 155 | set_default_env("!spi_flash_probe() failed"); |
thomas.langer@lantiq.com | 97dfdcf | 2011-03-29 02:35:14 +0000 | [diff] [blame] | 156 | goto out; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 157 | } |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 158 | |
| 159 | ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET, |
| 160 | CONFIG_ENV_SIZE, tmp_env1); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 161 | if (ret) { |
| 162 | set_default_env("!spi_flash_read() failed"); |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 163 | goto err_read; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 164 | } |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 165 | |
| 166 | if (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc) |
| 167 | crc1_ok = 1; |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 168 | |
| 169 | ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET_REDUND, |
| 170 | CONFIG_ENV_SIZE, tmp_env2); |
| 171 | if (!ret) { |
| 172 | if (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc) |
| 173 | crc2_ok = 1; |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 174 | } |
| 175 | |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 176 | if (!crc1_ok && !crc2_ok) { |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 177 | set_default_env("!bad CRC"); |
thomas.langer@lantiq.com | 97dfdcf | 2011-03-29 02:35:14 +0000 | [diff] [blame] | 178 | goto err_read; |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 179 | } else if (crc1_ok && !crc2_ok) { |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 180 | gd->env_valid = 1; |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 181 | } else if (!crc1_ok && crc2_ok) { |
thomas.langer@lantiq.com | 97dfdcf | 2011-03-29 02:35:14 +0000 | [diff] [blame] | 182 | gd->env_valid = 2; |
| 183 | } else if (tmp_env1->flags == ACTIVE_FLAG && |
| 184 | tmp_env2->flags == OBSOLETE_FLAG) { |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 185 | gd->env_valid = 1; |
thomas.langer@lantiq.com | 97dfdcf | 2011-03-29 02:35:14 +0000 | [diff] [blame] | 186 | } else if (tmp_env1->flags == OBSOLETE_FLAG && |
| 187 | tmp_env2->flags == ACTIVE_FLAG) { |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 188 | gd->env_valid = 2; |
thomas.langer@lantiq.com | 97dfdcf | 2011-03-29 02:35:14 +0000 | [diff] [blame] | 189 | } else if (tmp_env1->flags == tmp_env2->flags) { |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 190 | gd->env_valid = 2; |
thomas.langer@lantiq.com | 97dfdcf | 2011-03-29 02:35:14 +0000 | [diff] [blame] | 191 | } else if (tmp_env1->flags == 0xFF) { |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 192 | gd->env_valid = 2; |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 193 | } else { |
| 194 | /* |
| 195 | * this differs from code in env_flash.c, but I think a sane |
| 196 | * default path is desirable. |
| 197 | */ |
| 198 | gd->env_valid = 2; |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 199 | } |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 200 | |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 201 | if (gd->env_valid == 1) |
| 202 | ep = tmp_env1; |
| 203 | else |
| 204 | ep = tmp_env2; |
| 205 | |
| 206 | ret = env_import((char *)ep, 0); |
| 207 | if (!ret) { |
| 208 | error("Cannot import environment: errno = %d\n", errno); |
| 209 | set_default_env("env_import failed"); |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 210 | } |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 211 | |
| 212 | err_read: |
| 213 | spi_flash_free(env_flash); |
| 214 | env_flash = NULL; |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 215 | out: |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 216 | free(tmp_env1); |
| 217 | free(tmp_env2); |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 218 | } |
| 219 | #else |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 220 | int saveenv(void) |
| 221 | { |
Igor Grinberg | c3c1bed | 2011-11-07 01:14:08 +0000 | [diff] [blame] | 222 | u32 saved_size, saved_offset, sector = 1; |
Marek Vasut | d73c129 | 2014-03-05 19:59:50 +0100 | [diff] [blame] | 223 | char *saved_buffer = NULL; |
Igor Grinberg | c3c1bed | 2011-11-07 01:14:08 +0000 | [diff] [blame] | 224 | int ret = 1; |
Tom Rini | a840b8e | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 225 | env_t env_new; |
TsiChung Liew | 08792d7 | 2008-08-06 19:37:17 -0500 | [diff] [blame] | 226 | |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 227 | if (!env_flash) { |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 228 | env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, |
| 229 | CONFIG_ENV_SPI_CS, |
| 230 | CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE); |
| 231 | if (!env_flash) { |
| 232 | set_default_env("!spi_flash_probe() failed"); |
| 233 | return 1; |
| 234 | } |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 235 | } |
| 236 | |
Mike Frysinger | 9af10d5 | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 237 | /* Is the sector larger than the env (i.e. embedded) */ |
| 238 | if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) { |
| 239 | saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE; |
| 240 | saved_offset = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE; |
| 241 | saved_buffer = malloc(saved_size); |
Igor Grinberg | c3c1bed | 2011-11-07 01:14:08 +0000 | [diff] [blame] | 242 | if (!saved_buffer) |
Mike Frysinger | 9af10d5 | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 243 | goto done; |
Igor Grinberg | c3c1bed | 2011-11-07 01:14:08 +0000 | [diff] [blame] | 244 | |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 245 | ret = spi_flash_read(env_flash, saved_offset, |
| 246 | saved_size, saved_buffer); |
Mike Frysinger | 9af10d5 | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 247 | if (ret) |
| 248 | goto done; |
| 249 | } |
| 250 | |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 251 | if (CONFIG_ENV_SIZE > CONFIG_ENV_SECT_SIZE) { |
| 252 | sector = CONFIG_ENV_SIZE / CONFIG_ENV_SECT_SIZE; |
| 253 | if (CONFIG_ENV_SIZE % CONFIG_ENV_SECT_SIZE) |
TsiChung Liew | 08792d7 | 2008-08-06 19:37:17 -0500 | [diff] [blame] | 254 | sector++; |
| 255 | } |
| 256 | |
Marek Vasut | d73c129 | 2014-03-05 19:59:50 +0100 | [diff] [blame] | 257 | ret = env_export(&env_new); |
| 258 | if (ret) |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 259 | goto done; |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 260 | |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 261 | puts("Erasing SPI flash..."); |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 262 | ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET, |
| 263 | sector * CONFIG_ENV_SECT_SIZE); |
Mike Frysinger | 9af10d5 | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 264 | if (ret) |
| 265 | goto done; |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 266 | |
| 267 | puts("Writing to SPI flash..."); |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 268 | ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET, |
Tom Rini | a840b8e | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 269 | CONFIG_ENV_SIZE, &env_new); |
Mike Frysinger | 9af10d5 | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 270 | if (ret) |
| 271 | goto done; |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 272 | |
Mike Frysinger | 9af10d5 | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 273 | if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) { |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 274 | ret = spi_flash_write(env_flash, saved_offset, |
| 275 | saved_size, saved_buffer); |
Mike Frysinger | 9af10d5 | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 276 | if (ret) |
| 277 | goto done; |
| 278 | } |
| 279 | |
| 280 | ret = 0; |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 281 | puts("done\n"); |
Mike Frysinger | 9af10d5 | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 282 | |
| 283 | done: |
| 284 | if (saved_buffer) |
| 285 | free(saved_buffer); |
Igor Grinberg | c3c1bed | 2011-11-07 01:14:08 +0000 | [diff] [blame] | 286 | |
Mike Frysinger | 9af10d5 | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 287 | return ret; |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | void env_relocate_spec(void) |
| 291 | { |
| 292 | int ret; |
Ying Zhang | 354846f | 2014-01-24 15:50:07 +0800 | [diff] [blame] | 293 | char *buf = NULL; |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 294 | |
Ying Zhang | 354846f | 2014-01-24 15:50:07 +0800 | [diff] [blame] | 295 | buf = (char *)malloc(CONFIG_ENV_SIZE); |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 296 | env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS, |
| 297 | CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 298 | if (!env_flash) { |
| 299 | set_default_env("!spi_flash_probe() failed"); |
Ying Zhang | 354846f | 2014-01-24 15:50:07 +0800 | [diff] [blame] | 300 | if (buf) |
| 301 | free(buf); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 302 | return; |
| 303 | } |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 304 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 305 | ret = spi_flash_read(env_flash, |
| 306 | CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf); |
| 307 | if (ret) { |
| 308 | set_default_env("!spi_flash_read() failed"); |
| 309 | goto out; |
| 310 | } |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 311 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 312 | ret = env_import(buf, 1); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 313 | if (ret) |
| 314 | gd->env_valid = 1; |
| 315 | out: |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 316 | spi_flash_free(env_flash); |
Ying Zhang | 354846f | 2014-01-24 15:50:07 +0800 | [diff] [blame] | 317 | if (buf) |
| 318 | free(buf); |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 319 | env_flash = NULL; |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 320 | } |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 321 | #endif |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 322 | |
| 323 | int env_init(void) |
| 324 | { |
| 325 | /* SPI flash isn't usable before relocation */ |
| 326 | gd->env_addr = (ulong)&default_environment[0]; |
| 327 | gd->env_valid = 1; |
| 328 | |
| 329 | return 0; |
| 330 | } |