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