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> |
Mike Frysinger | 9af10d5 | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 15 | #include <malloc.h> |
Simon Glass | def9215 | 2014-10-13 23:41:55 -0600 | [diff] [blame] | 16 | #include <spi.h> |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 17 | #include <spi_flash.h> |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 18 | #include <search.h> |
| 19 | #include <errno.h> |
Gong Qianyu | 6d85a60 | 2016-01-26 15:06:42 +0800 | [diff] [blame] | 20 | #include <dm/device-internal.h> |
Simon Glass | 48b6c6b | 2019-11-14 12:57:16 -0700 | [diff] [blame] | 21 | #include <u-boot/crc.h> |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 22 | |
Simon Glass | c10a88e | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 23 | #ifndef CONFIG_SPL_BUILD |
Ashish Kumar | a7bf68a | 2017-12-14 17:37:08 +0530 | [diff] [blame] | 24 | #define INITENV |
Simon Glass | c10a88e | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 25 | #endif |
| 26 | |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 27 | #ifdef CONFIG_ENV_OFFSET_REDUND |
Igor Grinberg | c3c1bed | 2011-11-07 01:14:08 +0000 | [diff] [blame] | 28 | static ulong env_offset = CONFIG_ENV_OFFSET; |
| 29 | static ulong env_new_offset = CONFIG_ENV_OFFSET_REDUND; |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 30 | #endif /* CONFIG_ENV_OFFSET_REDUND */ |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 31 | |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 32 | DECLARE_GLOBAL_DATA_PTR; |
| 33 | |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 34 | static struct spi_flash *env_flash; |
| 35 | |
Andreas Fenkart | 52b9444 | 2017-04-08 11:59:31 +0200 | [diff] [blame] | 36 | static int setup_flash_device(void) |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 37 | { |
Gong Qianyu | 6d85a60 | 2016-01-26 15:06:42 +0800 | [diff] [blame] | 38 | #ifdef CONFIG_DM_SPI_FLASH |
| 39 | struct udevice *new; |
Andreas Fenkart | 52b9444 | 2017-04-08 11:59:31 +0200 | [diff] [blame] | 40 | int ret; |
Gong Qianyu | 6d85a60 | 2016-01-26 15:06:42 +0800 | [diff] [blame] | 41 | |
Vignesh R | ae56979 | 2016-07-06 10:04:28 +0530 | [diff] [blame] | 42 | /* speed and mode will be read from DT */ |
Gong Qianyu | 6d85a60 | 2016-01-26 15:06:42 +0800 | [diff] [blame] | 43 | 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] | 44 | CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE, |
| 45 | &new); |
Gong Qianyu | 6d85a60 | 2016-01-26 15:06:42 +0800 | [diff] [blame] | 46 | if (ret) { |
Simon Glass | 9738586 | 2019-08-01 09:47:00 -0600 | [diff] [blame] | 47 | env_set_default("spi_flash_probe_bus_cs() failed", 0); |
Simon Glass | 9977849 | 2017-08-03 12:22:17 -0600 | [diff] [blame] | 48 | return ret; |
Gong Qianyu | 6d85a60 | 2016-01-26 15:06:42 +0800 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | env_flash = dev_get_uclass_priv(new); |
| 52 | #else |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 53 | |
| 54 | if (!env_flash) { |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 55 | env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, |
| 56 | CONFIG_ENV_SPI_CS, |
| 57 | CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE); |
| 58 | if (!env_flash) { |
Simon Glass | 9738586 | 2019-08-01 09:47:00 -0600 | [diff] [blame] | 59 | env_set_default("spi_flash_probe() failed", 0); |
Simon Glass | 9977849 | 2017-08-03 12:22:17 -0600 | [diff] [blame] | 60 | return -EIO; |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 61 | } |
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; |
Andreas Fenkart | df70191 | 2017-04-08 11:59:34 +0200 | [diff] [blame] | 72 | u32 saved_size, saved_offset, sector; |
Andreas Fenkart | 52b9444 | 2017-04-08 11:59:31 +0200 | [diff] [blame] | 73 | int ret; |
| 74 | |
| 75 | ret = setup_flash_device(); |
| 76 | if (ret) |
| 77 | return ret; |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 78 | |
Marek Vasut | d73c129 | 2014-03-05 19:59:50 +0100 | [diff] [blame] | 79 | ret = env_export(&env_new); |
| 80 | if (ret) |
Simon Glass | 9977849 | 2017-08-03 12:22:17 -0600 | [diff] [blame] | 81 | return -EIO; |
Simon Glass | 4686d8f | 2019-08-01 09:47:08 -0600 | [diff] [blame] | 82 | env_new.flags = ENV_REDUND_ACTIVE; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 83 | |
Simon Glass | 4bc2ad2 | 2017-08-03 12:21:56 -0600 | [diff] [blame] | 84 | if (gd->env_valid == ENV_VALID) { |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 85 | env_new_offset = CONFIG_ENV_OFFSET_REDUND; |
| 86 | env_offset = CONFIG_ENV_OFFSET; |
| 87 | } else { |
| 88 | env_new_offset = CONFIG_ENV_OFFSET; |
| 89 | env_offset = CONFIG_ENV_OFFSET_REDUND; |
| 90 | } |
| 91 | |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 92 | /* Is the sector larger than the env (i.e. embedded) */ |
| 93 | if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) { |
| 94 | saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE; |
| 95 | saved_offset = env_new_offset + CONFIG_ENV_SIZE; |
Ravi Babu | 20d93fc | 2015-08-17 13:29:49 +0530 | [diff] [blame] | 96 | saved_buffer = memalign(ARCH_DMA_MINALIGN, saved_size); |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 97 | if (!saved_buffer) { |
Simon Glass | 9977849 | 2017-08-03 12:22:17 -0600 | [diff] [blame] | 98 | ret = -ENOMEM; |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 99 | goto done; |
| 100 | } |
Heiko Schocher | f45775c | 2019-03-13 12:15:45 +0100 | [diff] [blame] | 101 | ret = spi_flash_read(env_flash, saved_offset, |
| 102 | saved_size, saved_buffer); |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 103 | if (ret) |
| 104 | goto done; |
| 105 | } |
| 106 | |
Andreas Fenkart | df70191 | 2017-04-08 11:59:34 +0200 | [diff] [blame] | 107 | sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, CONFIG_ENV_SECT_SIZE); |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 108 | |
| 109 | puts("Erasing SPI flash..."); |
| 110 | ret = spi_flash_erase(env_flash, env_new_offset, |
| 111 | sector * CONFIG_ENV_SECT_SIZE); |
| 112 | if (ret) |
| 113 | goto done; |
| 114 | |
| 115 | puts("Writing to SPI flash..."); |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 116 | |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 117 | ret = spi_flash_write(env_flash, env_new_offset, |
Tom Rini | a840b8e | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 118 | CONFIG_ENV_SIZE, &env_new); |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 119 | if (ret) |
| 120 | goto done; |
| 121 | |
| 122 | if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) { |
| 123 | ret = spi_flash_write(env_flash, saved_offset, |
| 124 | saved_size, saved_buffer); |
| 125 | if (ret) |
| 126 | goto done; |
| 127 | } |
| 128 | |
Igor Grinberg | c3c1bed | 2011-11-07 01:14:08 +0000 | [diff] [blame] | 129 | ret = spi_flash_write(env_flash, env_offset + offsetof(env_t, flags), |
Tom Rini | a840b8e | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 130 | sizeof(env_new.flags), &flag); |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 131 | if (ret) |
| 132 | goto done; |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 133 | |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 134 | puts("done\n"); |
| 135 | |
Simon Glass | 4bc2ad2 | 2017-08-03 12:21:56 -0600 | [diff] [blame] | 136 | gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND; |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 137 | |
thomas.langer@lantiq.com | 97dfdcf | 2011-03-29 02:35:14 +0000 | [diff] [blame] | 138 | printf("Valid environment: %d\n", (int)gd->env_valid); |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 139 | |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 140 | done: |
| 141 | if (saved_buffer) |
| 142 | free(saved_buffer); |
Igor Grinberg | c3c1bed | 2011-11-07 01:14:08 +0000 | [diff] [blame] | 143 | |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 144 | return ret; |
| 145 | } |
| 146 | |
Simon Glass | 9977849 | 2017-08-03 12:22:17 -0600 | [diff] [blame] | 147 | static int env_sf_load(void) |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 148 | { |
| 149 | int ret; |
Simon Goldschmidt | 8f9ce3a | 2018-01-31 14:47:13 +0100 | [diff] [blame] | 150 | int read1_fail, read2_fail; |
| 151 | env_t *tmp_env1, *tmp_env2; |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 152 | |
Ravi Babu | 20d93fc | 2015-08-17 13:29:49 +0530 | [diff] [blame] | 153 | tmp_env1 = (env_t *)memalign(ARCH_DMA_MINALIGN, |
| 154 | CONFIG_ENV_SIZE); |
| 155 | tmp_env2 = (env_t *)memalign(ARCH_DMA_MINALIGN, |
| 156 | CONFIG_ENV_SIZE); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 157 | if (!tmp_env1 || !tmp_env2) { |
Simon Glass | 9738586 | 2019-08-01 09:47:00 -0600 | [diff] [blame] | 158 | env_set_default("malloc() failed", 0); |
Simon Glass | 9977849 | 2017-08-03 12:22:17 -0600 | [diff] [blame] | 159 | ret = -EIO; |
thomas.langer@lantiq.com | 97dfdcf | 2011-03-29 02:35:14 +0000 | [diff] [blame] | 160 | goto out; |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 161 | } |
| 162 | |
Andreas Fenkart | a2d1883 | 2017-04-08 11:59:32 +0200 | [diff] [blame] | 163 | ret = setup_flash_device(); |
| 164 | if (ret) |
thomas.langer@lantiq.com | 97dfdcf | 2011-03-29 02:35:14 +0000 | [diff] [blame] | 165 | goto out; |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 166 | |
Heiko Schocher | f45775c | 2019-03-13 12:15:45 +0100 | [diff] [blame] | 167 | read1_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET, |
| 168 | CONFIG_ENV_SIZE, tmp_env1); |
| 169 | read2_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET_REDUND, |
| 170 | CONFIG_ENV_SIZE, tmp_env2); |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 171 | |
Simon Goldschmidt | 8f9ce3a | 2018-01-31 14:47:13 +0100 | [diff] [blame] | 172 | ret = env_import_redund((char *)tmp_env1, read1_fail, (char *)tmp_env2, |
| 173 | read2_fail); |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 174 | |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 175 | spi_flash_free(env_flash); |
| 176 | env_flash = NULL; |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 177 | out: |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 178 | free(tmp_env1); |
| 179 | free(tmp_env2); |
Simon Glass | 9977849 | 2017-08-03 12:22:17 -0600 | [diff] [blame] | 180 | |
| 181 | return ret; |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 182 | } |
| 183 | #else |
Simon Glass | 082af92 | 2017-08-03 12:22:01 -0600 | [diff] [blame] | 184 | static int env_sf_save(void) |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 185 | { |
Andreas Fenkart | df70191 | 2017-04-08 11:59:34 +0200 | [diff] [blame] | 186 | u32 saved_size, saved_offset, sector; |
Marek Vasut | d73c129 | 2014-03-05 19:59:50 +0100 | [diff] [blame] | 187 | char *saved_buffer = NULL; |
Igor Grinberg | c3c1bed | 2011-11-07 01:14:08 +0000 | [diff] [blame] | 188 | int ret = 1; |
Tom Rini | a840b8e | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 189 | env_t env_new; |
Gong Qianyu | 6d85a60 | 2016-01-26 15:06:42 +0800 | [diff] [blame] | 190 | |
Andreas Fenkart | 52b9444 | 2017-04-08 11:59:31 +0200 | [diff] [blame] | 191 | ret = setup_flash_device(); |
| 192 | if (ret) |
| 193 | return ret; |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 194 | |
Mike Frysinger | 9af10d5 | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 195 | /* Is the sector larger than the env (i.e. embedded) */ |
| 196 | if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) { |
| 197 | saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE; |
| 198 | saved_offset = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE; |
| 199 | saved_buffer = malloc(saved_size); |
Igor Grinberg | c3c1bed | 2011-11-07 01:14:08 +0000 | [diff] [blame] | 200 | if (!saved_buffer) |
Mike Frysinger | 9af10d5 | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 201 | goto done; |
Igor Grinberg | c3c1bed | 2011-11-07 01:14:08 +0000 | [diff] [blame] | 202 | |
Heiko Schocher | f45775c | 2019-03-13 12:15:45 +0100 | [diff] [blame] | 203 | ret = spi_flash_read(env_flash, saved_offset, |
| 204 | saved_size, saved_buffer); |
Mike Frysinger | 9af10d5 | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 205 | if (ret) |
| 206 | goto done; |
| 207 | } |
| 208 | |
Marek Vasut | d73c129 | 2014-03-05 19:59:50 +0100 | [diff] [blame] | 209 | ret = env_export(&env_new); |
| 210 | if (ret) |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 211 | goto done; |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 212 | |
Andreas Fenkart | df70191 | 2017-04-08 11:59:34 +0200 | [diff] [blame] | 213 | sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, CONFIG_ENV_SECT_SIZE); |
| 214 | |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 215 | puts("Erasing SPI flash..."); |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 216 | ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET, |
| 217 | sector * CONFIG_ENV_SECT_SIZE); |
Mike Frysinger | 9af10d5 | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 218 | if (ret) |
| 219 | goto done; |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 220 | |
| 221 | puts("Writing to SPI flash..."); |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 222 | ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET, |
Tom Rini | a840b8e | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 223 | CONFIG_ENV_SIZE, &env_new); |
Mike Frysinger | 9af10d5 | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 224 | if (ret) |
| 225 | goto done; |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 226 | |
Mike Frysinger | 9af10d5 | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 227 | if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) { |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 228 | ret = spi_flash_write(env_flash, saved_offset, |
| 229 | saved_size, saved_buffer); |
Mike Frysinger | 9af10d5 | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 230 | if (ret) |
| 231 | goto done; |
| 232 | } |
| 233 | |
| 234 | ret = 0; |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 235 | puts("done\n"); |
Mike Frysinger | 9af10d5 | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 236 | |
| 237 | done: |
| 238 | if (saved_buffer) |
| 239 | free(saved_buffer); |
Igor Grinberg | c3c1bed | 2011-11-07 01:14:08 +0000 | [diff] [blame] | 240 | |
Mike Frysinger | 9af10d5 | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 241 | return ret; |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 242 | } |
| 243 | |
Simon Glass | 9977849 | 2017-08-03 12:22:17 -0600 | [diff] [blame] | 244 | static int env_sf_load(void) |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 245 | { |
| 246 | int ret; |
Ying Zhang | 354846f | 2014-01-24 15:50:07 +0800 | [diff] [blame] | 247 | char *buf = NULL; |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 248 | |
Ravi Babu | 20d93fc | 2015-08-17 13:29:49 +0530 | [diff] [blame] | 249 | buf = (char *)memalign(ARCH_DMA_MINALIGN, CONFIG_ENV_SIZE); |
Andreas Fenkart | a2c6585 | 2017-04-08 11:59:33 +0200 | [diff] [blame] | 250 | if (!buf) { |
Simon Glass | 9738586 | 2019-08-01 09:47:00 -0600 | [diff] [blame] | 251 | env_set_default("malloc() failed", 0); |
Simon Glass | 9977849 | 2017-08-03 12:22:17 -0600 | [diff] [blame] | 252 | return -EIO; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 253 | } |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 254 | |
Andreas Fenkart | a2c6585 | 2017-04-08 11:59:33 +0200 | [diff] [blame] | 255 | ret = setup_flash_device(); |
| 256 | if (ret) |
| 257 | goto out; |
| 258 | |
Heiko Schocher | f45775c | 2019-03-13 12:15:45 +0100 | [diff] [blame] | 259 | ret = spi_flash_read(env_flash, |
| 260 | CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 261 | if (ret) { |
Simon Glass | 9738586 | 2019-08-01 09:47:00 -0600 | [diff] [blame] | 262 | env_set_default("spi_flash_read() failed", 0); |
Andreas Fenkart | a2c6585 | 2017-04-08 11:59:33 +0200 | [diff] [blame] | 263 | goto err_read; |
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 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 266 | ret = env_import(buf, 1); |
Simon Goldschmidt | 32c3eb5 | 2018-01-31 14:47:10 +0100 | [diff] [blame] | 267 | if (!ret) |
Simon Glass | 4bc2ad2 | 2017-08-03 12:21:56 -0600 | [diff] [blame] | 268 | gd->env_valid = ENV_VALID; |
Andreas Fenkart | a2c6585 | 2017-04-08 11:59:33 +0200 | [diff] [blame] | 269 | |
| 270 | err_read: |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 271 | spi_flash_free(env_flash); |
| 272 | env_flash = NULL; |
Andreas Fenkart | a2c6585 | 2017-04-08 11:59:33 +0200 | [diff] [blame] | 273 | out: |
| 274 | free(buf); |
Simon Glass | 9977849 | 2017-08-03 12:22:17 -0600 | [diff] [blame] | 275 | |
| 276 | return ret; |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 277 | } |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 278 | #endif |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 279 | |
Tom Rini | 5cd7ece | 2019-11-18 20:02:10 -0500 | [diff] [blame] | 280 | #if CONFIG_ENV_ADDR != 0x0 |
Rajesh Bhagat | 9a90cc9 | 2018-11-05 18:01:15 +0000 | [diff] [blame] | 281 | __weak void *env_sf_get_env_addr(void) |
| 282 | { |
| 283 | return (void *)CONFIG_ENV_ADDR; |
| 284 | } |
| 285 | #endif |
| 286 | |
Tom Rini | 5cd7ece | 2019-11-18 20:02:10 -0500 | [diff] [blame] | 287 | #if defined(INITENV) && (CONFIG_ENV_ADDR != 0x0) |
Ashish Kumar | a7bf68a | 2017-12-14 17:37:08 +0530 | [diff] [blame] | 288 | static int env_sf_init(void) |
| 289 | { |
Rajesh Bhagat | 9a90cc9 | 2018-11-05 18:01:15 +0000 | [diff] [blame] | 290 | env_t *env_ptr = (env_t *)env_sf_get_env_addr(); |
Ashish Kumar | a7bf68a | 2017-12-14 17:37:08 +0530 | [diff] [blame] | 291 | |
| 292 | if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) { |
| 293 | gd->env_addr = (ulong)&(env_ptr->data); |
| 294 | gd->env_valid = 1; |
| 295 | } else { |
| 296 | gd->env_addr = (ulong)&default_environment[0]; |
| 297 | gd->env_valid = 1; |
| 298 | } |
| 299 | |
| 300 | return 0; |
| 301 | } |
| 302 | #endif |
| 303 | |
Simon Glass | c10a88e | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 304 | U_BOOT_ENV_LOCATION(sf) = { |
| 305 | .location = ENVL_SPI_FLASH, |
Simon Glass | d8273ed | 2017-08-03 12:22:03 -0600 | [diff] [blame] | 306 | ENV_NAME("SPI Flash") |
Simon Glass | 082af92 | 2017-08-03 12:22:01 -0600 | [diff] [blame] | 307 | .load = env_sf_load, |
Rasmus Villemoes | 4886ebc | 2020-02-19 09:47:43 +0000 | [diff] [blame] | 308 | .save = ENV_SAVE_PTR(env_sf_save), |
Tom Rini | 5cd7ece | 2019-11-18 20:02:10 -0500 | [diff] [blame] | 309 | #if defined(INITENV) && (CONFIG_ENV_ADDR != 0x0) |
Ashish Kumar | a7bf68a | 2017-12-14 17:37:08 +0530 | [diff] [blame] | 310 | .init = env_sf_init, |
| 311 | #endif |
Simon Glass | c10a88e | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 312 | }; |