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