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