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