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