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