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