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