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