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