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