Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Haavard Skinnemoen | 8c66497 | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 2 | /* |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 3 | * (C) Copyright 2000-2010 |
Haavard Skinnemoen | 8c66497 | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 4 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 5 | * |
| 6 | * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com> |
| 7 | * Andreas Heppel <aheppel@sysgo.de> |
| 8 | * |
| 9 | * (C) Copyright 2008 Atmel Corporation |
Haavard Skinnemoen | 8c66497 | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 10 | */ |
| 11 | #include <common.h> |
Simon Glass | 9d92245 | 2017-05-17 17:18:03 -0600 | [diff] [blame] | 12 | #include <dm.h> |
Simon Glass | 0ac7d72 | 2019-08-01 09:47:00 -0600 | [diff] [blame^] | 13 | #include <env.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 | |
Simon Glass | 4415f1d | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 22 | #ifndef CONFIG_SPL_BUILD |
| 23 | #define CMD_SAVEENV |
Ashish Kumar | b500c92 | 2017-12-14 17:37:08 +0530 | [diff] [blame] | 24 | #define INITENV |
Simon Glass | 4415f1d | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 25 | #endif |
| 26 | |
Wolfgang Wegner | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 27 | #ifdef CONFIG_ENV_OFFSET_REDUND |
Simon Glass | 4415f1d | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 28 | #ifdef CMD_SAVEENV |
Igor Grinberg | eb58a7f | 2011-11-07 01:14:08 +0000 | [diff] [blame] | 29 | static ulong env_offset = CONFIG_ENV_OFFSET; |
| 30 | static ulong env_new_offset = CONFIG_ENV_OFFSET_REDUND; |
Simon Glass | 4415f1d | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 31 | #endif |
Wolfgang Wegner | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 32 | |
Igor Grinberg | eb58a7f | 2011-11-07 01:14:08 +0000 | [diff] [blame] | 33 | #define ACTIVE_FLAG 1 |
| 34 | #define OBSOLETE_FLAG 0 |
Stefano Babic | a3110f0 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 35 | #endif /* CONFIG_ENV_OFFSET_REDUND */ |
Wolfgang Wegner | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 36 | |
Haavard Skinnemoen | 8c66497 | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 37 | DECLARE_GLOBAL_DATA_PTR; |
| 38 | |
Haavard Skinnemoen | 8c66497 | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 39 | static struct spi_flash *env_flash; |
| 40 | |
Andreas Fenkart | afa81a7 | 2017-04-08 11:59:31 +0200 | [diff] [blame] | 41 | static int setup_flash_device(void) |
Wolfgang Wegner | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 42 | { |
Gong Qianyu | 19c3128 | 2016-01-26 15:06:42 +0800 | [diff] [blame] | 43 | #ifdef CONFIG_DM_SPI_FLASH |
| 44 | struct udevice *new; |
Andreas Fenkart | afa81a7 | 2017-04-08 11:59:31 +0200 | [diff] [blame] | 45 | int ret; |
Gong Qianyu | 19c3128 | 2016-01-26 15:06:42 +0800 | [diff] [blame] | 46 | |
Vignesh R | 96907c0 | 2016-07-06 10:04:28 +0530 | [diff] [blame] | 47 | /* speed and mode will be read from DT */ |
Gong Qianyu | 19c3128 | 2016-01-26 15:06:42 +0800 | [diff] [blame] | 48 | ret = spi_flash_probe_bus_cs(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS, |
Konstantin Porotchkin | 25a1765 | 2018-08-29 16:34:52 +0300 | [diff] [blame] | 49 | CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE, |
| 50 | &new); |
Gong Qianyu | 19c3128 | 2016-01-26 15:06:42 +0800 | [diff] [blame] | 51 | if (ret) { |
Simon Glass | 0ac7d72 | 2019-08-01 09:47:00 -0600 | [diff] [blame^] | 52 | env_set_default("spi_flash_probe_bus_cs() failed", 0); |
Simon Glass | c595199 | 2017-08-03 12:22:17 -0600 | [diff] [blame] | 53 | return ret; |
Gong Qianyu | 19c3128 | 2016-01-26 15:06:42 +0800 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | env_flash = dev_get_uclass_priv(new); |
| 57 | #else |
Wolfgang Wegner | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 58 | |
| 59 | if (!env_flash) { |
Stefano Babic | a3110f0 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 60 | env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, |
| 61 | CONFIG_ENV_SPI_CS, |
| 62 | CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE); |
| 63 | if (!env_flash) { |
Simon Glass | 0ac7d72 | 2019-08-01 09:47:00 -0600 | [diff] [blame^] | 64 | env_set_default("spi_flash_probe() failed", 0); |
Simon Glass | c595199 | 2017-08-03 12:22:17 -0600 | [diff] [blame] | 65 | return -EIO; |
Stefano Babic | a3110f0 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 66 | } |
Wolfgang Wegner | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 67 | } |
Gong Qianyu | 19c3128 | 2016-01-26 15:06:42 +0800 | [diff] [blame] | 68 | #endif |
Andreas Fenkart | afa81a7 | 2017-04-08 11:59:31 +0200 | [diff] [blame] | 69 | return 0; |
| 70 | } |
| 71 | |
| 72 | #if defined(CONFIG_ENV_OFFSET_REDUND) |
Simon Glass | 4415f1d | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 73 | #ifdef CMD_SAVEENV |
Simon Glass | e5bce24 | 2017-08-03 12:22:01 -0600 | [diff] [blame] | 74 | static int env_sf_save(void) |
Andreas Fenkart | afa81a7 | 2017-04-08 11:59:31 +0200 | [diff] [blame] | 75 | { |
| 76 | env_t env_new; |
| 77 | char *saved_buffer = NULL, flag = OBSOLETE_FLAG; |
Andreas Fenkart | 0b2e5bb | 2017-04-08 11:59:34 +0200 | [diff] [blame] | 78 | u32 saved_size, saved_offset, sector; |
Andreas Fenkart | afa81a7 | 2017-04-08 11:59:31 +0200 | [diff] [blame] | 79 | int ret; |
| 80 | |
| 81 | ret = setup_flash_device(); |
| 82 | if (ret) |
| 83 | return ret; |
Wolfgang Wegner | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 84 | |
Marek Vasut | 7ce1526 | 2014-03-05 19:59:50 +0100 | [diff] [blame] | 85 | ret = env_export(&env_new); |
| 86 | if (ret) |
Simon Glass | c595199 | 2017-08-03 12:22:17 -0600 | [diff] [blame] | 87 | return -EIO; |
Tom Rini | cd0f4fa | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 88 | env_new.flags = ACTIVE_FLAG; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 89 | |
Simon Glass | 203e94f | 2017-08-03 12:21:56 -0600 | [diff] [blame] | 90 | if (gd->env_valid == ENV_VALID) { |
Stefano Babic | a3110f0 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 91 | env_new_offset = CONFIG_ENV_OFFSET_REDUND; |
| 92 | env_offset = CONFIG_ENV_OFFSET; |
| 93 | } else { |
| 94 | env_new_offset = CONFIG_ENV_OFFSET; |
| 95 | env_offset = CONFIG_ENV_OFFSET_REDUND; |
| 96 | } |
| 97 | |
Wolfgang Wegner | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 98 | /* Is the sector larger than the env (i.e. embedded) */ |
| 99 | if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) { |
| 100 | saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE; |
| 101 | saved_offset = env_new_offset + CONFIG_ENV_SIZE; |
Ravi Babu | 7dd0174 | 2015-08-17 13:29:49 +0530 | [diff] [blame] | 102 | saved_buffer = memalign(ARCH_DMA_MINALIGN, saved_size); |
Wolfgang Wegner | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 103 | if (!saved_buffer) { |
Simon Glass | c595199 | 2017-08-03 12:22:17 -0600 | [diff] [blame] | 104 | ret = -ENOMEM; |
Wolfgang Wegner | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 105 | goto done; |
| 106 | } |
Heiko Schocher | 9ba5e5b | 2019-03-13 12:15:45 +0100 | [diff] [blame] | 107 | ret = spi_flash_read(env_flash, saved_offset, |
| 108 | saved_size, saved_buffer); |
Wolfgang Wegner | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 109 | if (ret) |
| 110 | goto done; |
| 111 | } |
| 112 | |
Andreas Fenkart | 0b2e5bb | 2017-04-08 11:59:34 +0200 | [diff] [blame] | 113 | sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, CONFIG_ENV_SECT_SIZE); |
Wolfgang Wegner | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 114 | |
| 115 | puts("Erasing SPI flash..."); |
| 116 | ret = spi_flash_erase(env_flash, env_new_offset, |
| 117 | sector * CONFIG_ENV_SECT_SIZE); |
| 118 | if (ret) |
| 119 | goto done; |
| 120 | |
| 121 | puts("Writing to SPI flash..."); |
Wolfgang Wegner | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 122 | |
Stefano Babic | a3110f0 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 123 | ret = spi_flash_write(env_flash, env_new_offset, |
Tom Rini | cd0f4fa | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 124 | CONFIG_ENV_SIZE, &env_new); |
Wolfgang Wegner | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 125 | if (ret) |
| 126 | goto done; |
| 127 | |
| 128 | if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) { |
| 129 | ret = spi_flash_write(env_flash, saved_offset, |
| 130 | saved_size, saved_buffer); |
| 131 | if (ret) |
| 132 | goto done; |
| 133 | } |
| 134 | |
Igor Grinberg | eb58a7f | 2011-11-07 01:14:08 +0000 | [diff] [blame] | 135 | ret = spi_flash_write(env_flash, env_offset + offsetof(env_t, flags), |
Tom Rini | cd0f4fa | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 136 | sizeof(env_new.flags), &flag); |
Stefano Babic | a3110f0 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 137 | if (ret) |
| 138 | goto done; |
Wolfgang Wegner | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 139 | |
Wolfgang Wegner | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 140 | puts("done\n"); |
| 141 | |
Simon Glass | 203e94f | 2017-08-03 12:21:56 -0600 | [diff] [blame] | 142 | gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND; |
Stefano Babic | a3110f0 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 143 | |
thomas.langer@lantiq.com | 2dc55d9 | 2011-03-29 02:35:14 +0000 | [diff] [blame] | 144 | printf("Valid environment: %d\n", (int)gd->env_valid); |
Stefano Babic | a3110f0 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 145 | |
Wolfgang Wegner | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 146 | done: |
| 147 | if (saved_buffer) |
| 148 | free(saved_buffer); |
Igor Grinberg | eb58a7f | 2011-11-07 01:14:08 +0000 | [diff] [blame] | 149 | |
Wolfgang Wegner | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 150 | return ret; |
| 151 | } |
Simon Glass | 4415f1d | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 152 | #endif /* CMD_SAVEENV */ |
Wolfgang Wegner | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 153 | |
Simon Glass | c595199 | 2017-08-03 12:22:17 -0600 | [diff] [blame] | 154 | static int env_sf_load(void) |
Wolfgang Wegner | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 155 | { |
| 156 | int ret; |
Simon Goldschmidt | 8071993 | 2018-01-31 14:47:13 +0100 | [diff] [blame] | 157 | int read1_fail, read2_fail; |
| 158 | env_t *tmp_env1, *tmp_env2; |
Wolfgang Wegner | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 159 | |
Ravi Babu | 7dd0174 | 2015-08-17 13:29:49 +0530 | [diff] [blame] | 160 | tmp_env1 = (env_t *)memalign(ARCH_DMA_MINALIGN, |
| 161 | CONFIG_ENV_SIZE); |
| 162 | tmp_env2 = (env_t *)memalign(ARCH_DMA_MINALIGN, |
| 163 | CONFIG_ENV_SIZE); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 164 | if (!tmp_env1 || !tmp_env2) { |
Simon Glass | 0ac7d72 | 2019-08-01 09:47:00 -0600 | [diff] [blame^] | 165 | env_set_default("malloc() failed", 0); |
Simon Glass | c595199 | 2017-08-03 12:22:17 -0600 | [diff] [blame] | 166 | ret = -EIO; |
thomas.langer@lantiq.com | 2dc55d9 | 2011-03-29 02:35:14 +0000 | [diff] [blame] | 167 | goto out; |
Wolfgang Wegner | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 168 | } |
| 169 | |
Andreas Fenkart | 8fee884 | 2017-04-08 11:59:32 +0200 | [diff] [blame] | 170 | ret = setup_flash_device(); |
| 171 | if (ret) |
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 | |
Heiko Schocher | 9ba5e5b | 2019-03-13 12:15:45 +0100 | [diff] [blame] | 174 | read1_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET, |
| 175 | CONFIG_ENV_SIZE, tmp_env1); |
| 176 | read2_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET_REDUND, |
| 177 | CONFIG_ENV_SIZE, tmp_env2); |
Wolfgang Wegner | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 178 | |
Simon Goldschmidt | 8071993 | 2018-01-31 14:47:13 +0100 | [diff] [blame] | 179 | ret = env_import_redund((char *)tmp_env1, read1_fail, (char *)tmp_env2, |
| 180 | read2_fail); |
Wolfgang Wegner | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 181 | |
Wolfgang Wegner | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 182 | spi_flash_free(env_flash); |
| 183 | env_flash = NULL; |
Wolfgang Wegner | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 184 | out: |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 185 | free(tmp_env1); |
| 186 | free(tmp_env2); |
Simon Glass | c595199 | 2017-08-03 12:22:17 -0600 | [diff] [blame] | 187 | |
| 188 | return ret; |
Wolfgang Wegner | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 189 | } |
| 190 | #else |
Simon Glass | 4415f1d | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 191 | #ifdef CMD_SAVEENV |
Simon Glass | e5bce24 | 2017-08-03 12:22:01 -0600 | [diff] [blame] | 192 | static int env_sf_save(void) |
Haavard Skinnemoen | 8c66497 | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 193 | { |
Andreas Fenkart | 0b2e5bb | 2017-04-08 11:59:34 +0200 | [diff] [blame] | 194 | u32 saved_size, saved_offset, sector; |
Marek Vasut | 7ce1526 | 2014-03-05 19:59:50 +0100 | [diff] [blame] | 195 | char *saved_buffer = NULL; |
Igor Grinberg | eb58a7f | 2011-11-07 01:14:08 +0000 | [diff] [blame] | 196 | int ret = 1; |
Tom Rini | cd0f4fa | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 197 | env_t env_new; |
Gong Qianyu | 19c3128 | 2016-01-26 15:06:42 +0800 | [diff] [blame] | 198 | |
Andreas Fenkart | afa81a7 | 2017-04-08 11:59:31 +0200 | [diff] [blame] | 199 | ret = setup_flash_device(); |
| 200 | if (ret) |
| 201 | return ret; |
Haavard Skinnemoen | 8c66497 | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 202 | |
Mike Frysinger | 5b3375a | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 203 | /* Is the sector larger than the env (i.e. embedded) */ |
| 204 | if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) { |
| 205 | saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE; |
| 206 | saved_offset = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE; |
| 207 | saved_buffer = malloc(saved_size); |
Igor Grinberg | eb58a7f | 2011-11-07 01:14:08 +0000 | [diff] [blame] | 208 | if (!saved_buffer) |
Mike Frysinger | 5b3375a | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 209 | goto done; |
Igor Grinberg | eb58a7f | 2011-11-07 01:14:08 +0000 | [diff] [blame] | 210 | |
Heiko Schocher | 9ba5e5b | 2019-03-13 12:15:45 +0100 | [diff] [blame] | 211 | ret = spi_flash_read(env_flash, saved_offset, |
| 212 | saved_size, saved_buffer); |
Mike Frysinger | 5b3375a | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 213 | if (ret) |
| 214 | goto done; |
| 215 | } |
| 216 | |
Marek Vasut | 7ce1526 | 2014-03-05 19:59:50 +0100 | [diff] [blame] | 217 | ret = env_export(&env_new); |
| 218 | if (ret) |
Stefano Babic | a3110f0 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 219 | goto done; |
Stefano Babic | a3110f0 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 220 | |
Andreas Fenkart | 0b2e5bb | 2017-04-08 11:59:34 +0200 | [diff] [blame] | 221 | sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, CONFIG_ENV_SECT_SIZE); |
| 222 | |
Haavard Skinnemoen | 8c66497 | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 223 | puts("Erasing SPI flash..."); |
Stefano Babic | a3110f0 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 224 | ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET, |
| 225 | sector * CONFIG_ENV_SECT_SIZE); |
Mike Frysinger | 5b3375a | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 226 | if (ret) |
| 227 | goto done; |
Haavard Skinnemoen | 8c66497 | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 228 | |
| 229 | puts("Writing to SPI flash..."); |
Stefano Babic | a3110f0 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 230 | ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET, |
Tom Rini | cd0f4fa | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 231 | CONFIG_ENV_SIZE, &env_new); |
Mike Frysinger | 5b3375a | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 232 | if (ret) |
| 233 | goto done; |
Haavard Skinnemoen | 8c66497 | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 234 | |
Mike Frysinger | 5b3375a | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 235 | if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) { |
Stefano Babic | a3110f0 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 236 | ret = spi_flash_write(env_flash, saved_offset, |
| 237 | saved_size, saved_buffer); |
Mike Frysinger | 5b3375a | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 238 | if (ret) |
| 239 | goto done; |
| 240 | } |
| 241 | |
| 242 | ret = 0; |
Haavard Skinnemoen | 8c66497 | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 243 | puts("done\n"); |
Mike Frysinger | 5b3375a | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 244 | |
| 245 | done: |
| 246 | if (saved_buffer) |
| 247 | free(saved_buffer); |
Igor Grinberg | eb58a7f | 2011-11-07 01:14:08 +0000 | [diff] [blame] | 248 | |
Mike Frysinger | 5b3375a | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 249 | return ret; |
Haavard Skinnemoen | 8c66497 | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 250 | } |
Simon Glass | 4415f1d | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 251 | #endif /* CMD_SAVEENV */ |
Haavard Skinnemoen | 8c66497 | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 252 | |
Simon Glass | c595199 | 2017-08-03 12:22:17 -0600 | [diff] [blame] | 253 | static int env_sf_load(void) |
Haavard Skinnemoen | 8c66497 | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 254 | { |
| 255 | int ret; |
Ying Zhang | 5a89fa9 | 2014-01-24 15:50:07 +0800 | [diff] [blame] | 256 | char *buf = NULL; |
Haavard Skinnemoen | 8c66497 | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 257 | |
Ravi Babu | 7dd0174 | 2015-08-17 13:29:49 +0530 | [diff] [blame] | 258 | buf = (char *)memalign(ARCH_DMA_MINALIGN, CONFIG_ENV_SIZE); |
Andreas Fenkart | c041c60 | 2017-04-08 11:59:33 +0200 | [diff] [blame] | 259 | if (!buf) { |
Simon Glass | 0ac7d72 | 2019-08-01 09:47:00 -0600 | [diff] [blame^] | 260 | env_set_default("malloc() failed", 0); |
Simon Glass | c595199 | 2017-08-03 12:22:17 -0600 | [diff] [blame] | 261 | return -EIO; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 262 | } |
Haavard Skinnemoen | 8c66497 | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 263 | |
Andreas Fenkart | c041c60 | 2017-04-08 11:59:33 +0200 | [diff] [blame] | 264 | ret = setup_flash_device(); |
| 265 | if (ret) |
| 266 | goto out; |
| 267 | |
Heiko Schocher | 9ba5e5b | 2019-03-13 12:15:45 +0100 | [diff] [blame] | 268 | ret = spi_flash_read(env_flash, |
| 269 | CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 270 | if (ret) { |
Simon Glass | 0ac7d72 | 2019-08-01 09:47:00 -0600 | [diff] [blame^] | 271 | env_set_default("spi_flash_read() failed", 0); |
Andreas Fenkart | c041c60 | 2017-04-08 11:59:33 +0200 | [diff] [blame] | 272 | goto err_read; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | ret = env_import(buf, 1); |
Simon Goldschmidt | 42a1820 | 2018-01-31 14:47:10 +0100 | [diff] [blame] | 276 | if (!ret) |
Simon Glass | 203e94f | 2017-08-03 12:21:56 -0600 | [diff] [blame] | 277 | gd->env_valid = ENV_VALID; |
Andreas Fenkart | c041c60 | 2017-04-08 11:59:33 +0200 | [diff] [blame] | 278 | |
| 279 | err_read: |
Haavard Skinnemoen | 8c66497 | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 280 | spi_flash_free(env_flash); |
| 281 | env_flash = NULL; |
Andreas Fenkart | c041c60 | 2017-04-08 11:59:33 +0200 | [diff] [blame] | 282 | out: |
| 283 | free(buf); |
Simon Glass | c595199 | 2017-08-03 12:22:17 -0600 | [diff] [blame] | 284 | |
| 285 | return ret; |
Haavard Skinnemoen | 8c66497 | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 286 | } |
Wolfgang Wegner | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 287 | #endif |
Haavard Skinnemoen | 8c66497 | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 288 | |
Rajesh Bhagat | 119c01c2 | 2018-11-05 18:01:15 +0000 | [diff] [blame] | 289 | #ifdef CONFIG_ENV_ADDR |
| 290 | __weak void *env_sf_get_env_addr(void) |
| 291 | { |
| 292 | return (void *)CONFIG_ENV_ADDR; |
| 293 | } |
| 294 | #endif |
| 295 | |
Ashish Kumar | b500c92 | 2017-12-14 17:37:08 +0530 | [diff] [blame] | 296 | #if defined(INITENV) && defined(CONFIG_ENV_ADDR) |
| 297 | static int env_sf_init(void) |
| 298 | { |
Rajesh Bhagat | 119c01c2 | 2018-11-05 18:01:15 +0000 | [diff] [blame] | 299 | env_t *env_ptr = (env_t *)env_sf_get_env_addr(); |
Ashish Kumar | b500c92 | 2017-12-14 17:37:08 +0530 | [diff] [blame] | 300 | |
| 301 | if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) { |
| 302 | gd->env_addr = (ulong)&(env_ptr->data); |
| 303 | gd->env_valid = 1; |
| 304 | } else { |
| 305 | gd->env_addr = (ulong)&default_environment[0]; |
| 306 | gd->env_valid = 1; |
| 307 | } |
| 308 | |
| 309 | return 0; |
| 310 | } |
| 311 | #endif |
| 312 | |
Simon Glass | 4415f1d | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 313 | U_BOOT_ENV_LOCATION(sf) = { |
| 314 | .location = ENVL_SPI_FLASH, |
Simon Glass | ac358be | 2017-08-03 12:22:03 -0600 | [diff] [blame] | 315 | ENV_NAME("SPI Flash") |
Simon Glass | e5bce24 | 2017-08-03 12:22:01 -0600 | [diff] [blame] | 316 | .load = env_sf_load, |
Simon Glass | 4415f1d | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 317 | #ifdef CMD_SAVEENV |
Simon Glass | e5bce24 | 2017-08-03 12:22:01 -0600 | [diff] [blame] | 318 | .save = env_save_ptr(env_sf_save), |
Simon Glass | 4415f1d | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 319 | #endif |
Ashish Kumar | b500c92 | 2017-12-14 17:37:08 +0530 | [diff] [blame] | 320 | #if defined(INITENV) && defined(CONFIG_ENV_ADDR) |
| 321 | .init = env_sf_init, |
| 322 | #endif |
Simon Glass | 4415f1d | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 323 | }; |