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 | */ |
Simon Glass | 9d92245 | 2017-05-17 17:18:03 -0600 | [diff] [blame] | 11 | #include <dm.h> |
Simon Glass | 0ac7d72 | 2019-08-01 09:47:00 -0600 | [diff] [blame] | 12 | #include <env.h> |
Simon Glass | f3998fd | 2019-08-02 09:44:25 -0600 | [diff] [blame] | 13 | #include <env_internal.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> |
Caleb Connolly | 58d825f | 2024-08-30 13:34:37 +0100 | [diff] [blame] | 19 | #include <u-boot/uuid.h> |
Simon Glass | 90526e9 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 20 | #include <asm/cache.h> |
Simon Glass | 401d1c4 | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 21 | #include <asm/global_data.h> |
Gong Qianyu | 19c3128 | 2016-01-26 15:06:42 +0800 | [diff] [blame] | 22 | #include <dm/device-internal.h> |
Simon Glass | 3db7110 | 2019-11-14 12:57:16 -0700 | [diff] [blame] | 23 | #include <u-boot/crc.h> |
Haavard Skinnemoen | 8c66497 | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 24 | |
Patrick Delaunay | 25d90ad | 2021-02-09 11:48:52 +0100 | [diff] [blame] | 25 | #define OFFSET_INVALID (~(u32)0) |
| 26 | |
Wolfgang Wegner | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 27 | #ifdef CONFIG_ENV_OFFSET_REDUND |
Patrick Delaunay | 25d90ad | 2021-02-09 11:48:52 +0100 | [diff] [blame] | 28 | #define ENV_OFFSET_REDUND CONFIG_ENV_OFFSET_REDUND |
| 29 | |
Igor Grinberg | eb58a7f | 2011-11-07 01:14:08 +0000 | [diff] [blame] | 30 | static ulong env_offset = CONFIG_ENV_OFFSET; |
| 31 | static ulong env_new_offset = CONFIG_ENV_OFFSET_REDUND; |
Patrick Delaunay | 25d90ad | 2021-02-09 11:48:52 +0100 | [diff] [blame] | 32 | |
| 33 | #else |
| 34 | |
| 35 | #define ENV_OFFSET_REDUND OFFSET_INVALID |
| 36 | |
Stefano Babic | a3110f0 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 37 | #endif /* CONFIG_ENV_OFFSET_REDUND */ |
Wolfgang Wegner | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 38 | |
Haavard Skinnemoen | 8c66497 | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 39 | DECLARE_GLOBAL_DATA_PTR; |
| 40 | |
Venkatesh Yadav Abbarapu | 7d84ad1 | 2024-06-14 18:18:10 +0530 | [diff] [blame] | 41 | __weak int spi_get_env_dev(void) |
| 42 | { |
| 43 | #ifdef CONFIG_ENV_SPI_BUS |
| 44 | return CONFIG_ENV_SPI_BUS; |
| 45 | #else |
| 46 | return 0; |
| 47 | #endif |
| 48 | } |
| 49 | |
Patrick Delaunay | ecf1544 | 2021-02-24 11:52:36 +0100 | [diff] [blame] | 50 | static int setup_flash_device(struct spi_flash **env_flash) |
Wolfgang Wegner | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 51 | { |
Lukasz Majewski | 56c4046 | 2020-06-04 23:11:53 +0800 | [diff] [blame] | 52 | #if CONFIG_IS_ENABLED(DM_SPI_FLASH) |
Gong Qianyu | 19c3128 | 2016-01-26 15:06:42 +0800 | [diff] [blame] | 53 | struct udevice *new; |
Andreas Fenkart | afa81a7 | 2017-04-08 11:59:31 +0200 | [diff] [blame] | 54 | int ret; |
Venkatesh Yadav Abbarapu | 7d84ad1 | 2024-06-14 18:18:10 +0530 | [diff] [blame] | 55 | int dev = spi_get_env_dev(); |
Gong Qianyu | 19c3128 | 2016-01-26 15:06:42 +0800 | [diff] [blame] | 56 | |
Vignesh R | 96907c0 | 2016-07-06 10:04:28 +0530 | [diff] [blame] | 57 | /* speed and mode will be read from DT */ |
Venkatesh Yadav Abbarapu | 7d84ad1 | 2024-06-14 18:18:10 +0530 | [diff] [blame] | 58 | ret = spi_flash_probe_bus_cs(dev, CONFIG_ENV_SPI_CS, |
Konstantin Porotchkin | 25a1765 | 2018-08-29 16:34:52 +0300 | [diff] [blame] | 59 | &new); |
Gong Qianyu | 19c3128 | 2016-01-26 15:06:42 +0800 | [diff] [blame] | 60 | if (ret) { |
Simon Glass | 0ac7d72 | 2019-08-01 09:47:00 -0600 | [diff] [blame] | 61 | env_set_default("spi_flash_probe_bus_cs() failed", 0); |
Simon Glass | c595199 | 2017-08-03 12:22:17 -0600 | [diff] [blame] | 62 | return ret; |
Gong Qianyu | 19c3128 | 2016-01-26 15:06:42 +0800 | [diff] [blame] | 63 | } |
| 64 | |
Patrick Delaunay | ecf1544 | 2021-02-24 11:52:36 +0100 | [diff] [blame] | 65 | *env_flash = dev_get_uclass_priv(new); |
Gong Qianyu | 19c3128 | 2016-01-26 15:06:42 +0800 | [diff] [blame] | 66 | #else |
Patrick Delaunay | ecf1544 | 2021-02-24 11:52:36 +0100 | [diff] [blame] | 67 | *env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS, |
| 68 | CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE); |
| 69 | if (!*env_flash) { |
Jagan Teki | c8cbf17 | 2020-05-13 16:32:42 +0530 | [diff] [blame] | 70 | env_set_default("spi_flash_probe() failed", 0); |
| 71 | return -EIO; |
Wolfgang Wegner | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 72 | } |
Gong Qianyu | 19c3128 | 2016-01-26 15:06:42 +0800 | [diff] [blame] | 73 | #endif |
Andreas Fenkart | afa81a7 | 2017-04-08 11:59:31 +0200 | [diff] [blame] | 74 | return 0; |
| 75 | } |
| 76 | |
| 77 | #if defined(CONFIG_ENV_OFFSET_REDUND) |
Simon Glass | e5bce24 | 2017-08-03 12:22:01 -0600 | [diff] [blame] | 78 | static int env_sf_save(void) |
Andreas Fenkart | afa81a7 | 2017-04-08 11:59:31 +0200 | [diff] [blame] | 79 | { |
| 80 | env_t env_new; |
Simon Glass | d3716dd | 2019-08-01 09:47:08 -0600 | [diff] [blame] | 81 | char *saved_buffer = NULL, flag = ENV_REDUND_OBSOLETE; |
Rasmus Villemoes | bcb44f6 | 2021-04-14 20:51:43 +0200 | [diff] [blame] | 82 | u32 saved_size = 0, saved_offset = 0, sector; |
Rasmus Villemoes | 4da7347 | 2021-04-14 20:51:42 +0200 | [diff] [blame] | 83 | u32 sect_size = CONFIG_ENV_SECT_SIZE; |
Andreas Fenkart | afa81a7 | 2017-04-08 11:59:31 +0200 | [diff] [blame] | 84 | int ret; |
Patrick Delaunay | ecf1544 | 2021-02-24 11:52:36 +0100 | [diff] [blame] | 85 | struct spi_flash *env_flash; |
Andreas Fenkart | afa81a7 | 2017-04-08 11:59:31 +0200 | [diff] [blame] | 86 | |
Patrick Delaunay | ecf1544 | 2021-02-24 11:52:36 +0100 | [diff] [blame] | 87 | ret = setup_flash_device(&env_flash); |
Andreas Fenkart | afa81a7 | 2017-04-08 11:59:31 +0200 | [diff] [blame] | 88 | if (ret) |
| 89 | return ret; |
Wolfgang Wegner | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 90 | |
Rasmus Villemoes | bcb44f6 | 2021-04-14 20:51:43 +0200 | [diff] [blame] | 91 | if (IS_ENABLED(CONFIG_ENV_SECT_SIZE_AUTO)) |
| 92 | sect_size = env_flash->mtd.erasesize; |
| 93 | |
Marek Vasut | 7ce1526 | 2014-03-05 19:59:50 +0100 | [diff] [blame] | 94 | ret = env_export(&env_new); |
| 95 | if (ret) |
Simon Glass | c595199 | 2017-08-03 12:22:17 -0600 | [diff] [blame] | 96 | return -EIO; |
Simon Glass | d3716dd | 2019-08-01 09:47:08 -0600 | [diff] [blame] | 97 | env_new.flags = ENV_REDUND_ACTIVE; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 98 | |
Simon Glass | 203e94f | 2017-08-03 12:21:56 -0600 | [diff] [blame] | 99 | if (gd->env_valid == ENV_VALID) { |
Stefano Babic | a3110f0 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 100 | env_new_offset = CONFIG_ENV_OFFSET_REDUND; |
| 101 | env_offset = CONFIG_ENV_OFFSET; |
| 102 | } else { |
| 103 | env_new_offset = CONFIG_ENV_OFFSET; |
| 104 | env_offset = CONFIG_ENV_OFFSET_REDUND; |
| 105 | } |
| 106 | |
Wolfgang Wegner | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 107 | /* Is the sector larger than the env (i.e. embedded) */ |
Rasmus Villemoes | 4da7347 | 2021-04-14 20:51:42 +0200 | [diff] [blame] | 108 | if (sect_size > CONFIG_ENV_SIZE) { |
| 109 | saved_size = sect_size - CONFIG_ENV_SIZE; |
Wolfgang Wegner | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 110 | saved_offset = env_new_offset + CONFIG_ENV_SIZE; |
Ravi Babu | 7dd0174 | 2015-08-17 13:29:49 +0530 | [diff] [blame] | 111 | saved_buffer = memalign(ARCH_DMA_MINALIGN, saved_size); |
Wolfgang Wegner | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 112 | if (!saved_buffer) { |
Simon Glass | c595199 | 2017-08-03 12:22:17 -0600 | [diff] [blame] | 113 | ret = -ENOMEM; |
Wolfgang Wegner | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 114 | goto done; |
| 115 | } |
Heiko Schocher | 9ba5e5b | 2019-03-13 12:15:45 +0100 | [diff] [blame] | 116 | ret = spi_flash_read(env_flash, saved_offset, |
| 117 | saved_size, saved_buffer); |
Wolfgang Wegner | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 118 | if (ret) |
| 119 | goto done; |
| 120 | } |
| 121 | |
Rasmus Villemoes | 4da7347 | 2021-04-14 20:51:42 +0200 | [diff] [blame] | 122 | sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, sect_size); |
Wolfgang Wegner | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 123 | |
| 124 | puts("Erasing SPI flash..."); |
| 125 | ret = spi_flash_erase(env_flash, env_new_offset, |
Rasmus Villemoes | 4da7347 | 2021-04-14 20:51:42 +0200 | [diff] [blame] | 126 | sector * sect_size); |
Wolfgang Wegner | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 127 | if (ret) |
| 128 | goto done; |
| 129 | |
| 130 | puts("Writing to SPI flash..."); |
Wolfgang Wegner | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 131 | |
Stefano Babic | a3110f0 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 132 | ret = spi_flash_write(env_flash, env_new_offset, |
Tom Rini | cd0f4fa | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 133 | CONFIG_ENV_SIZE, &env_new); |
Wolfgang Wegner | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 134 | if (ret) |
| 135 | goto done; |
| 136 | |
Rasmus Villemoes | 4da7347 | 2021-04-14 20:51:42 +0200 | [diff] [blame] | 137 | if (sect_size > CONFIG_ENV_SIZE) { |
Wolfgang Wegner | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 138 | ret = spi_flash_write(env_flash, saved_offset, |
| 139 | saved_size, saved_buffer); |
| 140 | if (ret) |
| 141 | goto done; |
| 142 | } |
| 143 | |
Igor Grinberg | eb58a7f | 2011-11-07 01:14:08 +0000 | [diff] [blame] | 144 | ret = spi_flash_write(env_flash, env_offset + offsetof(env_t, flags), |
Tom Rini | cd0f4fa | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 145 | sizeof(env_new.flags), &flag); |
Stefano Babic | a3110f0 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 146 | if (ret) |
| 147 | goto done; |
Wolfgang Wegner | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 148 | |
Wolfgang Wegner | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 149 | puts("done\n"); |
| 150 | |
Simon Glass | 203e94f | 2017-08-03 12:21:56 -0600 | [diff] [blame] | 151 | gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND; |
Stefano Babic | a3110f0 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 152 | |
thomas.langer@lantiq.com | 2dc55d9 | 2011-03-29 02:35:14 +0000 | [diff] [blame] | 153 | printf("Valid environment: %d\n", (int)gd->env_valid); |
Stefano Babic | a3110f0 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 154 | |
Patrick Delaunay | ea5641e | 2021-01-04 14:41:31 +0100 | [diff] [blame] | 155 | done: |
Patrick Delaunay | c2d0036 | 2021-02-24 11:52:35 +0100 | [diff] [blame] | 156 | spi_flash_free(env_flash); |
Patrick Delaunay | c2d0036 | 2021-02-24 11:52:35 +0100 | [diff] [blame] | 157 | |
Wolfgang Wegner | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 158 | if (saved_buffer) |
| 159 | free(saved_buffer); |
Igor Grinberg | eb58a7f | 2011-11-07 01:14:08 +0000 | [diff] [blame] | 160 | |
Wolfgang Wegner | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 161 | return ret; |
| 162 | } |
| 163 | |
Simon Glass | c595199 | 2017-08-03 12:22:17 -0600 | [diff] [blame] | 164 | static int env_sf_load(void) |
Wolfgang Wegner | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 165 | { |
| 166 | int ret; |
Simon Goldschmidt | 8071993 | 2018-01-31 14:47:13 +0100 | [diff] [blame] | 167 | int read1_fail, read2_fail; |
| 168 | env_t *tmp_env1, *tmp_env2; |
Patrick Delaunay | ecf1544 | 2021-02-24 11:52:36 +0100 | [diff] [blame] | 169 | struct spi_flash *env_flash; |
Wolfgang Wegner | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 170 | |
Ravi Babu | 7dd0174 | 2015-08-17 13:29:49 +0530 | [diff] [blame] | 171 | tmp_env1 = (env_t *)memalign(ARCH_DMA_MINALIGN, |
| 172 | CONFIG_ENV_SIZE); |
| 173 | tmp_env2 = (env_t *)memalign(ARCH_DMA_MINALIGN, |
| 174 | CONFIG_ENV_SIZE); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 175 | if (!tmp_env1 || !tmp_env2) { |
Simon Glass | 0ac7d72 | 2019-08-01 09:47:00 -0600 | [diff] [blame] | 176 | env_set_default("malloc() failed", 0); |
Simon Glass | c595199 | 2017-08-03 12:22:17 -0600 | [diff] [blame] | 177 | ret = -EIO; |
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 | |
Patrick Delaunay | ecf1544 | 2021-02-24 11:52:36 +0100 | [diff] [blame] | 181 | ret = setup_flash_device(&env_flash); |
Andreas Fenkart | 8fee884 | 2017-04-08 11:59:32 +0200 | [diff] [blame] | 182 | if (ret) |
thomas.langer@lantiq.com | 2dc55d9 | 2011-03-29 02:35:14 +0000 | [diff] [blame] | 183 | goto out; |
Wolfgang Wegner | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 184 | |
Heiko Schocher | 9ba5e5b | 2019-03-13 12:15:45 +0100 | [diff] [blame] | 185 | read1_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET, |
| 186 | CONFIG_ENV_SIZE, tmp_env1); |
| 187 | read2_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET_REDUND, |
| 188 | CONFIG_ENV_SIZE, tmp_env2); |
Wolfgang Wegner | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 189 | |
Simon Goldschmidt | 8071993 | 2018-01-31 14:47:13 +0100 | [diff] [blame] | 190 | ret = env_import_redund((char *)tmp_env1, read1_fail, (char *)tmp_env2, |
Marek Vasut | 890feec | 2020-07-07 20:51:35 +0200 | [diff] [blame] | 191 | read2_fail, H_EXTERNAL); |
Wolfgang Wegner | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 192 | |
Wolfgang Wegner | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 193 | spi_flash_free(env_flash); |
Wolfgang Wegner | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 194 | out: |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 195 | free(tmp_env1); |
| 196 | free(tmp_env2); |
Simon Glass | c595199 | 2017-08-03 12:22:17 -0600 | [diff] [blame] | 197 | |
| 198 | return ret; |
Wolfgang Wegner | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 199 | } |
| 200 | #else |
Simon Glass | e5bce24 | 2017-08-03 12:22:01 -0600 | [diff] [blame] | 201 | static int env_sf_save(void) |
Haavard Skinnemoen | 8c66497 | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 202 | { |
Rasmus Villemoes | bcb44f6 | 2021-04-14 20:51:43 +0200 | [diff] [blame] | 203 | u32 saved_size = 0, saved_offset = 0, sector; |
Rasmus Villemoes | 4da7347 | 2021-04-14 20:51:42 +0200 | [diff] [blame] | 204 | u32 sect_size = CONFIG_ENV_SECT_SIZE; |
Marek Vasut | 7ce1526 | 2014-03-05 19:59:50 +0100 | [diff] [blame] | 205 | char *saved_buffer = NULL; |
Igor Grinberg | eb58a7f | 2011-11-07 01:14:08 +0000 | [diff] [blame] | 206 | int ret = 1; |
Tom Rini | cd0f4fa | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 207 | env_t env_new; |
Patrick Delaunay | ecf1544 | 2021-02-24 11:52:36 +0100 | [diff] [blame] | 208 | struct spi_flash *env_flash; |
Gong Qianyu | 19c3128 | 2016-01-26 15:06:42 +0800 | [diff] [blame] | 209 | |
Patrick Delaunay | ecf1544 | 2021-02-24 11:52:36 +0100 | [diff] [blame] | 210 | ret = setup_flash_device(&env_flash); |
Andreas Fenkart | afa81a7 | 2017-04-08 11:59:31 +0200 | [diff] [blame] | 211 | if (ret) |
| 212 | return ret; |
Haavard Skinnemoen | 8c66497 | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 213 | |
Rasmus Villemoes | bcb44f6 | 2021-04-14 20:51:43 +0200 | [diff] [blame] | 214 | if (IS_ENABLED(CONFIG_ENV_SECT_SIZE_AUTO)) |
| 215 | sect_size = env_flash->mtd.erasesize; |
| 216 | |
Mike Frysinger | 5b3375a | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 217 | /* Is the sector larger than the env (i.e. embedded) */ |
Rasmus Villemoes | 4da7347 | 2021-04-14 20:51:42 +0200 | [diff] [blame] | 218 | if (sect_size > CONFIG_ENV_SIZE) { |
| 219 | saved_size = sect_size - CONFIG_ENV_SIZE; |
Mike Frysinger | 5b3375a | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 220 | saved_offset = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE; |
| 221 | saved_buffer = malloc(saved_size); |
Ralph Siemsen | 04add62e | 2024-01-19 16:32:17 -0500 | [diff] [blame] | 222 | if (!saved_buffer) { |
| 223 | ret = -ENOMEM; |
Mike Frysinger | 5b3375a | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 224 | goto done; |
Ralph Siemsen | 04add62e | 2024-01-19 16:32:17 -0500 | [diff] [blame] | 225 | } |
Igor Grinberg | eb58a7f | 2011-11-07 01:14:08 +0000 | [diff] [blame] | 226 | |
Heiko Schocher | 9ba5e5b | 2019-03-13 12:15:45 +0100 | [diff] [blame] | 227 | ret = spi_flash_read(env_flash, saved_offset, |
| 228 | saved_size, saved_buffer); |
Mike Frysinger | 5b3375a | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 229 | if (ret) |
| 230 | goto done; |
| 231 | } |
| 232 | |
Marek Vasut | 7ce1526 | 2014-03-05 19:59:50 +0100 | [diff] [blame] | 233 | ret = env_export(&env_new); |
| 234 | if (ret) |
Stefano Babic | a3110f0 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 235 | goto done; |
Stefano Babic | a3110f0 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 236 | |
Rasmus Villemoes | 4da7347 | 2021-04-14 20:51:42 +0200 | [diff] [blame] | 237 | sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, sect_size); |
Andreas Fenkart | 0b2e5bb | 2017-04-08 11:59:34 +0200 | [diff] [blame] | 238 | |
Haavard Skinnemoen | 8c66497 | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 239 | puts("Erasing SPI flash..."); |
Stefano Babic | a3110f0 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 240 | ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET, |
Rasmus Villemoes | 4da7347 | 2021-04-14 20:51:42 +0200 | [diff] [blame] | 241 | sector * sect_size); |
Mike Frysinger | 5b3375a | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 242 | if (ret) |
| 243 | goto done; |
Haavard Skinnemoen | 8c66497 | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 244 | |
| 245 | puts("Writing to SPI flash..."); |
Stefano Babic | a3110f0 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 246 | ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET, |
Tom Rini | cd0f4fa | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 247 | CONFIG_ENV_SIZE, &env_new); |
Mike Frysinger | 5b3375a | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 248 | if (ret) |
| 249 | goto done; |
Haavard Skinnemoen | 8c66497 | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 250 | |
Rasmus Villemoes | 4da7347 | 2021-04-14 20:51:42 +0200 | [diff] [blame] | 251 | if (sect_size > CONFIG_ENV_SIZE) { |
Stefano Babic | a3110f0 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 252 | ret = spi_flash_write(env_flash, saved_offset, |
| 253 | saved_size, saved_buffer); |
Mike Frysinger | 5b3375a | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 254 | if (ret) |
| 255 | goto done; |
| 256 | } |
| 257 | |
| 258 | ret = 0; |
Haavard Skinnemoen | 8c66497 | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 259 | puts("done\n"); |
Mike Frysinger | 5b3375a | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 260 | |
Patrick Delaunay | ea5641e | 2021-01-04 14:41:31 +0100 | [diff] [blame] | 261 | done: |
Patrick Delaunay | c2d0036 | 2021-02-24 11:52:35 +0100 | [diff] [blame] | 262 | spi_flash_free(env_flash); |
Patrick Delaunay | c2d0036 | 2021-02-24 11:52:35 +0100 | [diff] [blame] | 263 | |
Mike Frysinger | 5b3375a | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 264 | if (saved_buffer) |
| 265 | free(saved_buffer); |
Igor Grinberg | eb58a7f | 2011-11-07 01:14:08 +0000 | [diff] [blame] | 266 | |
Mike Frysinger | 5b3375a | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 267 | return ret; |
Haavard Skinnemoen | 8c66497 | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 268 | } |
| 269 | |
Simon Glass | c595199 | 2017-08-03 12:22:17 -0600 | [diff] [blame] | 270 | static int env_sf_load(void) |
Haavard Skinnemoen | 8c66497 | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 271 | { |
| 272 | int ret; |
Ying Zhang | 5a89fa9 | 2014-01-24 15:50:07 +0800 | [diff] [blame] | 273 | char *buf = NULL; |
Patrick Delaunay | ecf1544 | 2021-02-24 11:52:36 +0100 | [diff] [blame] | 274 | struct spi_flash *env_flash; |
Haavard Skinnemoen | 8c66497 | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 275 | |
Ravi Babu | 7dd0174 | 2015-08-17 13:29:49 +0530 | [diff] [blame] | 276 | buf = (char *)memalign(ARCH_DMA_MINALIGN, CONFIG_ENV_SIZE); |
Andreas Fenkart | c041c60 | 2017-04-08 11:59:33 +0200 | [diff] [blame] | 277 | if (!buf) { |
Simon Glass | 0ac7d72 | 2019-08-01 09:47:00 -0600 | [diff] [blame] | 278 | env_set_default("malloc() failed", 0); |
Simon Glass | c595199 | 2017-08-03 12:22:17 -0600 | [diff] [blame] | 279 | return -EIO; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 280 | } |
Haavard Skinnemoen | 8c66497 | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 281 | |
Patrick Delaunay | ecf1544 | 2021-02-24 11:52:36 +0100 | [diff] [blame] | 282 | ret = setup_flash_device(&env_flash); |
Andreas Fenkart | c041c60 | 2017-04-08 11:59:33 +0200 | [diff] [blame] | 283 | if (ret) |
| 284 | goto out; |
| 285 | |
Heiko Schocher | 9ba5e5b | 2019-03-13 12:15:45 +0100 | [diff] [blame] | 286 | ret = spi_flash_read(env_flash, |
| 287 | CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 288 | if (ret) { |
Simon Glass | 0ac7d72 | 2019-08-01 09:47:00 -0600 | [diff] [blame] | 289 | env_set_default("spi_flash_read() failed", 0); |
Andreas Fenkart | c041c60 | 2017-04-08 11:59:33 +0200 | [diff] [blame] | 290 | goto err_read; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 291 | } |
| 292 | |
Marek Vasut | 890feec | 2020-07-07 20:51:35 +0200 | [diff] [blame] | 293 | ret = env_import(buf, 1, H_EXTERNAL); |
Simon Goldschmidt | 42a1820 | 2018-01-31 14:47:10 +0100 | [diff] [blame] | 294 | if (!ret) |
Simon Glass | 203e94f | 2017-08-03 12:21:56 -0600 | [diff] [blame] | 295 | gd->env_valid = ENV_VALID; |
Andreas Fenkart | c041c60 | 2017-04-08 11:59:33 +0200 | [diff] [blame] | 296 | |
| 297 | err_read: |
Haavard Skinnemoen | 8c66497 | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 298 | spi_flash_free(env_flash); |
Andreas Fenkart | c041c60 | 2017-04-08 11:59:33 +0200 | [diff] [blame] | 299 | out: |
| 300 | free(buf); |
Simon Glass | c595199 | 2017-08-03 12:22:17 -0600 | [diff] [blame] | 301 | |
| 302 | return ret; |
Haavard Skinnemoen | 8c66497 | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 303 | } |
Wolfgang Wegner | 7319bca | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 304 | #endif |
Haavard Skinnemoen | 8c66497 | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 305 | |
Patrick Delaunay | 25d90ad | 2021-02-09 11:48:52 +0100 | [diff] [blame] | 306 | static int env_sf_erase(void) |
| 307 | { |
| 308 | int ret; |
| 309 | env_t env; |
Patrick Delaunay | ecf1544 | 2021-02-24 11:52:36 +0100 | [diff] [blame] | 310 | struct spi_flash *env_flash; |
Patrick Delaunay | 25d90ad | 2021-02-09 11:48:52 +0100 | [diff] [blame] | 311 | |
Patrick Delaunay | ecf1544 | 2021-02-24 11:52:36 +0100 | [diff] [blame] | 312 | ret = setup_flash_device(&env_flash); |
Patrick Delaunay | 25d90ad | 2021-02-09 11:48:52 +0100 | [diff] [blame] | 313 | if (ret) |
| 314 | return ret; |
| 315 | |
| 316 | memset(&env, 0, sizeof(env_t)); |
| 317 | ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, &env); |
| 318 | if (ret) |
| 319 | goto done; |
| 320 | |
| 321 | if (ENV_OFFSET_REDUND != OFFSET_INVALID) |
| 322 | ret = spi_flash_write(env_flash, ENV_OFFSET_REDUND, CONFIG_ENV_SIZE, &env); |
| 323 | |
| 324 | done: |
| 325 | spi_flash_free(env_flash); |
Patrick Delaunay | 25d90ad | 2021-02-09 11:48:52 +0100 | [diff] [blame] | 326 | |
| 327 | return ret; |
| 328 | } |
| 329 | |
Rajesh Bhagat | 119c01c2 | 2018-11-05 18:01:15 +0000 | [diff] [blame] | 330 | __weak void *env_sf_get_env_addr(void) |
| 331 | { |
Simon Glass | 1d6132e | 2024-09-29 19:49:50 -0600 | [diff] [blame] | 332 | #ifndef CONFIG_XPL_BUILD |
Rajesh Bhagat | 119c01c2 | 2018-11-05 18:01:15 +0000 | [diff] [blame] | 333 | return (void *)CONFIG_ENV_ADDR; |
Pali Rohár | f4bf81b | 2022-03-02 12:47:51 +0100 | [diff] [blame] | 334 | #else |
| 335 | return NULL; |
Rajesh Bhagat | 119c01c2 | 2018-11-05 18:01:15 +0000 | [diff] [blame] | 336 | #endif |
Pali Rohár | f4bf81b | 2022-03-02 12:47:51 +0100 | [diff] [blame] | 337 | } |
Rajesh Bhagat | 119c01c2 | 2018-11-05 18:01:15 +0000 | [diff] [blame] | 338 | |
Heiko Schocher | 92765f45 | 2020-10-10 10:28:05 +0200 | [diff] [blame] | 339 | /* |
| 340 | * check if Environment on CONFIG_ENV_ADDR is valid. |
| 341 | */ |
| 342 | static int env_sf_init_addr(void) |
Ashish Kumar | b500c92 | 2017-12-14 17:37:08 +0530 | [diff] [blame] | 343 | { |
Rajesh Bhagat | 119c01c2 | 2018-11-05 18:01:15 +0000 | [diff] [blame] | 344 | env_t *env_ptr = (env_t *)env_sf_get_env_addr(); |
Ashish Kumar | b500c92 | 2017-12-14 17:37:08 +0530 | [diff] [blame] | 345 | |
Pali Rohár | f4bf81b | 2022-03-02 12:47:51 +0100 | [diff] [blame] | 346 | if (!env_ptr) |
| 347 | return -ENOENT; |
| 348 | |
Ashish Kumar | b500c92 | 2017-12-14 17:37:08 +0530 | [diff] [blame] | 349 | if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) { |
Marek Behún | 9ab30d2 | 2021-10-22 15:47:12 +0200 | [diff] [blame] | 350 | gd->env_addr = (ulong)&(env_ptr->data); |
Marek Behún | 87221bd | 2021-10-22 15:47:13 +0200 | [diff] [blame] | 351 | gd->env_valid = ENV_VALID; |
Ashish Kumar | b500c92 | 2017-12-14 17:37:08 +0530 | [diff] [blame] | 352 | } else { |
Marek Behún | af8149e | 2021-10-22 15:47:14 +0200 | [diff] [blame] | 353 | gd->env_valid = ENV_INVALID; |
Ashish Kumar | b500c92 | 2017-12-14 17:37:08 +0530 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | return 0; |
| 357 | } |
Ashish Kumar | b500c92 | 2017-12-14 17:37:08 +0530 | [diff] [blame] | 358 | |
Heiko Schocher | 92765f45 | 2020-10-10 10:28:05 +0200 | [diff] [blame] | 359 | #if defined(CONFIG_ENV_SPI_EARLY) |
| 360 | /* |
| 361 | * early load environment from SPI flash (before relocation) |
| 362 | * and check if it is valid. |
| 363 | */ |
| 364 | static int env_sf_init_early(void) |
| 365 | { |
| 366 | int ret; |
| 367 | int read1_fail; |
| 368 | int read2_fail; |
| 369 | int crc1_ok; |
| 370 | env_t *tmp_env2 = NULL; |
| 371 | env_t *tmp_env1; |
Patrick Delaunay | ecf1544 | 2021-02-24 11:52:36 +0100 | [diff] [blame] | 372 | struct spi_flash *env_flash; |
Heiko Schocher | 92765f45 | 2020-10-10 10:28:05 +0200 | [diff] [blame] | 373 | |
| 374 | /* |
| 375 | * if malloc is not ready yet, we cannot use |
| 376 | * this part yet. |
| 377 | */ |
| 378 | if (!gd->malloc_limit) |
| 379 | return -ENOENT; |
| 380 | |
| 381 | tmp_env1 = (env_t *)memalign(ARCH_DMA_MINALIGN, |
| 382 | CONFIG_ENV_SIZE); |
| 383 | if (IS_ENABLED(CONFIG_SYS_REDUNDAND_ENVIRONMENT)) |
| 384 | tmp_env2 = (env_t *)memalign(ARCH_DMA_MINALIGN, |
| 385 | CONFIG_ENV_SIZE); |
| 386 | |
| 387 | if (!tmp_env1 || !tmp_env2) |
| 388 | goto out; |
| 389 | |
Patrick Delaunay | ecf1544 | 2021-02-24 11:52:36 +0100 | [diff] [blame] | 390 | ret = setup_flash_device(&env_flash); |
Heiko Schocher | 92765f45 | 2020-10-10 10:28:05 +0200 | [diff] [blame] | 391 | if (ret) |
| 392 | goto out; |
| 393 | |
| 394 | read1_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET, |
| 395 | CONFIG_ENV_SIZE, tmp_env1); |
| 396 | |
| 397 | if (IS_ENABLED(CONFIG_SYS_REDUNDAND_ENVIRONMENT)) { |
| 398 | read2_fail = spi_flash_read(env_flash, |
| 399 | CONFIG_ENV_OFFSET_REDUND, |
| 400 | CONFIG_ENV_SIZE, tmp_env2); |
| 401 | ret = env_check_redund((char *)tmp_env1, read1_fail, |
| 402 | (char *)tmp_env2, read2_fail); |
| 403 | |
Brandon Maier | b9c3052 | 2020-12-17 17:19:18 -0600 | [diff] [blame] | 404 | if (ret < 0) |
Heiko Schocher | 92765f45 | 2020-10-10 10:28:05 +0200 | [diff] [blame] | 405 | goto err_read; |
| 406 | |
| 407 | if (gd->env_valid == ENV_VALID) |
| 408 | gd->env_addr = (unsigned long)&tmp_env1->data; |
| 409 | else |
| 410 | gd->env_addr = (unsigned long)&tmp_env2->data; |
| 411 | } else { |
| 412 | if (read1_fail) |
| 413 | goto err_read; |
| 414 | |
| 415 | crc1_ok = crc32(0, tmp_env1->data, ENV_SIZE) == |
| 416 | tmp_env1->crc; |
| 417 | if (!crc1_ok) |
| 418 | goto err_read; |
| 419 | |
| 420 | /* if valid -> this is our env */ |
| 421 | gd->env_valid = ENV_VALID; |
| 422 | gd->env_addr = (unsigned long)&tmp_env1->data; |
| 423 | } |
| 424 | |
Patrick Delaunay | c2d0036 | 2021-02-24 11:52:35 +0100 | [diff] [blame] | 425 | spi_flash_free(env_flash); |
Patrick Delaunay | c2d0036 | 2021-02-24 11:52:35 +0100 | [diff] [blame] | 426 | |
Heiko Schocher | 92765f45 | 2020-10-10 10:28:05 +0200 | [diff] [blame] | 427 | return 0; |
| 428 | err_read: |
| 429 | spi_flash_free(env_flash); |
Patrick Delaunay | ecf1544 | 2021-02-24 11:52:36 +0100 | [diff] [blame] | 430 | |
Heiko Schocher | 92765f45 | 2020-10-10 10:28:05 +0200 | [diff] [blame] | 431 | free(tmp_env1); |
| 432 | if (IS_ENABLED(CONFIG_SYS_REDUNDAND_ENVIRONMENT)) |
| 433 | free(tmp_env2); |
| 434 | out: |
| 435 | /* env is not valid. always return 0 */ |
| 436 | gd->env_valid = ENV_INVALID; |
| 437 | return 0; |
| 438 | } |
| 439 | #endif |
| 440 | |
| 441 | static int env_sf_init(void) |
| 442 | { |
Pali Rohár | f4bf81b | 2022-03-02 12:47:51 +0100 | [diff] [blame] | 443 | int ret = env_sf_init_addr(); |
| 444 | if (ret != -ENOENT) |
| 445 | return ret; |
| 446 | #ifdef CONFIG_ENV_SPI_EARLY |
Heiko Schocher | 92765f45 | 2020-10-10 10:28:05 +0200 | [diff] [blame] | 447 | return env_sf_init_early(); |
| 448 | #endif |
| 449 | /* |
Heiko Schocher | 46ce9e7 | 2020-11-03 15:22:36 +0100 | [diff] [blame] | 450 | * return here -ENOENT, so env_init() |
| 451 | * can set the init bit and later if no |
| 452 | * other Environment storage is defined |
| 453 | * can set the default environment |
Heiko Schocher | 92765f45 | 2020-10-10 10:28:05 +0200 | [diff] [blame] | 454 | */ |
Heiko Schocher | 46ce9e7 | 2020-11-03 15:22:36 +0100 | [diff] [blame] | 455 | return -ENOENT; |
Heiko Schocher | 92765f45 | 2020-10-10 10:28:05 +0200 | [diff] [blame] | 456 | } |
| 457 | |
Simon Glass | 4415f1d | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 458 | U_BOOT_ENV_LOCATION(sf) = { |
| 459 | .location = ENVL_SPI_FLASH, |
Patrick Delaunay | 22140d1 | 2020-07-28 11:51:16 +0200 | [diff] [blame] | 460 | ENV_NAME("SPIFlash") |
Simon Glass | e5bce24 | 2017-08-03 12:22:01 -0600 | [diff] [blame] | 461 | .load = env_sf_load, |
Patrick Delaunay | e41f55b | 2021-02-09 11:48:51 +0100 | [diff] [blame] | 462 | .save = ENV_SAVE_PTR(env_sf_save), |
Patrick Delaunay | 25d90ad | 2021-02-09 11:48:52 +0100 | [diff] [blame] | 463 | .erase = ENV_ERASE_PTR(env_sf_erase), |
Ashish Kumar | b500c92 | 2017-12-14 17:37:08 +0530 | [diff] [blame] | 464 | .init = env_sf_init, |
Simon Glass | 4415f1d | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 465 | }; |