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> |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 13 | #include <environment.h> |
Mike Frysinger | 9af10d5 | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 14 | #include <malloc.h> |
Simon Glass | def9215 | 2014-10-13 23:41:55 -0600 | [diff] [blame] | 15 | #include <spi.h> |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 16 | #include <spi_flash.h> |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 17 | #include <search.h> |
| 18 | #include <errno.h> |
Gong Qianyu | 6d85a60 | 2016-01-26 15:06:42 +0800 | [diff] [blame] | 19 | #include <dm/device-internal.h> |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 20 | |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 21 | #ifndef CONFIG_ENV_SPI_BUS |
| 22 | # define CONFIG_ENV_SPI_BUS 0 |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 23 | #endif |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 24 | #ifndef CONFIG_ENV_SPI_CS |
Igor Grinberg | c3c1bed | 2011-11-07 01:14:08 +0000 | [diff] [blame] | 25 | # define CONFIG_ENV_SPI_CS 0 |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 26 | #endif |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 27 | #ifndef CONFIG_ENV_SPI_MAX_HZ |
| 28 | # define CONFIG_ENV_SPI_MAX_HZ 1000000 |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 29 | #endif |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 30 | #ifndef CONFIG_ENV_SPI_MODE |
| 31 | # define CONFIG_ENV_SPI_MODE SPI_MODE_3 |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 32 | #endif |
| 33 | |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 34 | #ifdef CONFIG_ENV_OFFSET_REDUND |
Igor Grinberg | c3c1bed | 2011-11-07 01:14:08 +0000 | [diff] [blame] | 35 | static ulong env_offset = CONFIG_ENV_OFFSET; |
| 36 | static ulong env_new_offset = CONFIG_ENV_OFFSET_REDUND; |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 37 | |
Igor Grinberg | c3c1bed | 2011-11-07 01:14:08 +0000 | [diff] [blame] | 38 | #define ACTIVE_FLAG 1 |
| 39 | #define OBSOLETE_FLAG 0 |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 40 | #endif /* CONFIG_ENV_OFFSET_REDUND */ |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 41 | |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 42 | DECLARE_GLOBAL_DATA_PTR; |
| 43 | |
Igor Grinberg | c3c1bed | 2011-11-07 01:14:08 +0000 | [diff] [blame] | 44 | char *env_name_spec = "SPI Flash"; |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 45 | |
| 46 | static struct spi_flash *env_flash; |
| 47 | |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 48 | #if defined(CONFIG_ENV_OFFSET_REDUND) |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 49 | int saveenv(void) |
| 50 | { |
Tom Rini | a840b8e | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 51 | env_t env_new; |
Marek Vasut | d73c129 | 2014-03-05 19:59:50 +0100 | [diff] [blame] | 52 | char *saved_buffer = NULL, flag = OBSOLETE_FLAG; |
Igor Grinberg | c3c1bed | 2011-11-07 01:14:08 +0000 | [diff] [blame] | 53 | u32 saved_size, saved_offset, sector = 1; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 54 | int ret; |
Gong Qianyu | 6d85a60 | 2016-01-26 15:06:42 +0800 | [diff] [blame] | 55 | #ifdef CONFIG_DM_SPI_FLASH |
| 56 | struct udevice *new; |
| 57 | |
Vignesh R | ae56979 | 2016-07-06 10:04:28 +0530 | [diff] [blame] | 58 | /* speed and mode will be read from DT */ |
Gong Qianyu | 6d85a60 | 2016-01-26 15:06:42 +0800 | [diff] [blame] | 59 | 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] | 60 | 0, 0, &new); |
Gong Qianyu | 6d85a60 | 2016-01-26 15:06:42 +0800 | [diff] [blame] | 61 | if (ret) { |
| 62 | set_default_env("!spi_flash_probe_bus_cs() failed"); |
| 63 | return 1; |
| 64 | } |
| 65 | |
| 66 | env_flash = dev_get_uclass_priv(new); |
| 67 | #else |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 68 | |
| 69 | if (!env_flash) { |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 70 | env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, |
| 71 | CONFIG_ENV_SPI_CS, |
| 72 | CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE); |
| 73 | if (!env_flash) { |
| 74 | set_default_env("!spi_flash_probe() failed"); |
| 75 | return 1; |
| 76 | } |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 77 | } |
Gong Qianyu | 6d85a60 | 2016-01-26 15:06:42 +0800 | [diff] [blame] | 78 | #endif |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 79 | |
Marek Vasut | d73c129 | 2014-03-05 19:59:50 +0100 | [diff] [blame] | 80 | ret = env_export(&env_new); |
| 81 | if (ret) |
| 82 | return ret; |
Tom Rini | a840b8e | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 83 | env_new.flags = ACTIVE_FLAG; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 84 | |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 85 | if (gd->env_valid == 1) { |
| 86 | env_new_offset = CONFIG_ENV_OFFSET_REDUND; |
| 87 | env_offset = CONFIG_ENV_OFFSET; |
| 88 | } else { |
| 89 | env_new_offset = CONFIG_ENV_OFFSET; |
| 90 | env_offset = CONFIG_ENV_OFFSET_REDUND; |
| 91 | } |
| 92 | |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 93 | /* Is the sector larger than the env (i.e. embedded) */ |
| 94 | if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) { |
| 95 | saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE; |
| 96 | saved_offset = env_new_offset + CONFIG_ENV_SIZE; |
Ravi Babu | 20d93fc | 2015-08-17 13:29:49 +0530 | [diff] [blame] | 97 | saved_buffer = memalign(ARCH_DMA_MINALIGN, saved_size); |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 98 | if (!saved_buffer) { |
| 99 | ret = 1; |
| 100 | goto done; |
| 101 | } |
| 102 | ret = spi_flash_read(env_flash, saved_offset, |
| 103 | saved_size, saved_buffer); |
| 104 | if (ret) |
| 105 | goto done; |
| 106 | } |
| 107 | |
| 108 | if (CONFIG_ENV_SIZE > CONFIG_ENV_SECT_SIZE) { |
| 109 | sector = CONFIG_ENV_SIZE / CONFIG_ENV_SECT_SIZE; |
| 110 | if (CONFIG_ENV_SIZE % CONFIG_ENV_SECT_SIZE) |
| 111 | sector++; |
| 112 | } |
| 113 | |
| 114 | puts("Erasing SPI flash..."); |
| 115 | ret = spi_flash_erase(env_flash, env_new_offset, |
| 116 | sector * CONFIG_ENV_SECT_SIZE); |
| 117 | if (ret) |
| 118 | goto done; |
| 119 | |
| 120 | puts("Writing to SPI flash..."); |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 121 | |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 122 | ret = spi_flash_write(env_flash, env_new_offset, |
Tom Rini | a840b8e | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 123 | CONFIG_ENV_SIZE, &env_new); |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 124 | if (ret) |
| 125 | goto done; |
| 126 | |
| 127 | if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) { |
| 128 | ret = spi_flash_write(env_flash, saved_offset, |
| 129 | saved_size, saved_buffer); |
| 130 | if (ret) |
| 131 | goto done; |
| 132 | } |
| 133 | |
Igor Grinberg | c3c1bed | 2011-11-07 01:14:08 +0000 | [diff] [blame] | 134 | ret = spi_flash_write(env_flash, env_offset + offsetof(env_t, flags), |
Tom Rini | a840b8e | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 135 | sizeof(env_new.flags), &flag); |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 136 | if (ret) |
| 137 | goto done; |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 138 | |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 139 | puts("done\n"); |
| 140 | |
Igor Grinberg | c3c1bed | 2011-11-07 01:14:08 +0000 | [diff] [blame] | 141 | gd->env_valid = gd->env_valid == 2 ? 1 : 2; |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 142 | |
thomas.langer@lantiq.com | 97dfdcf | 2011-03-29 02:35:14 +0000 | [diff] [blame] | 143 | printf("Valid environment: %d\n", (int)gd->env_valid); |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 144 | |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 145 | done: |
| 146 | if (saved_buffer) |
| 147 | free(saved_buffer); |
Igor Grinberg | c3c1bed | 2011-11-07 01:14:08 +0000 | [diff] [blame] | 148 | |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 149 | return ret; |
| 150 | } |
| 151 | |
| 152 | void env_relocate_spec(void) |
| 153 | { |
| 154 | int ret; |
| 155 | int crc1_ok = 0, crc2_ok = 0; |
| 156 | env_t *tmp_env1 = NULL; |
| 157 | env_t *tmp_env2 = NULL; |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 158 | env_t *ep = NULL; |
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) { |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 165 | set_default_env("!malloc() failed"); |
thomas.langer@lantiq.com | 97dfdcf | 2011-03-29 02:35:14 +0000 | [diff] [blame] | 166 | goto out; |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS, |
| 170 | CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 171 | if (!env_flash) { |
| 172 | set_default_env("!spi_flash_probe() failed"); |
thomas.langer@lantiq.com | 97dfdcf | 2011-03-29 02:35:14 +0000 | [diff] [blame] | 173 | goto out; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 174 | } |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 175 | |
| 176 | ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET, |
| 177 | CONFIG_ENV_SIZE, tmp_env1); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 178 | if (ret) { |
| 179 | set_default_env("!spi_flash_read() failed"); |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 180 | goto err_read; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 181 | } |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 182 | |
| 183 | if (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc) |
| 184 | crc1_ok = 1; |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 185 | |
| 186 | ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET_REDUND, |
| 187 | CONFIG_ENV_SIZE, tmp_env2); |
| 188 | if (!ret) { |
| 189 | if (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc) |
| 190 | crc2_ok = 1; |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 191 | } |
| 192 | |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 193 | if (!crc1_ok && !crc2_ok) { |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 194 | set_default_env("!bad CRC"); |
thomas.langer@lantiq.com | 97dfdcf | 2011-03-29 02:35:14 +0000 | [diff] [blame] | 195 | goto err_read; |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 196 | } else if (crc1_ok && !crc2_ok) { |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 197 | gd->env_valid = 1; |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 198 | } else if (!crc1_ok && crc2_ok) { |
thomas.langer@lantiq.com | 97dfdcf | 2011-03-29 02:35:14 +0000 | [diff] [blame] | 199 | gd->env_valid = 2; |
| 200 | } else if (tmp_env1->flags == ACTIVE_FLAG && |
| 201 | tmp_env2->flags == OBSOLETE_FLAG) { |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 202 | gd->env_valid = 1; |
thomas.langer@lantiq.com | 97dfdcf | 2011-03-29 02:35:14 +0000 | [diff] [blame] | 203 | } else if (tmp_env1->flags == OBSOLETE_FLAG && |
| 204 | tmp_env2->flags == ACTIVE_FLAG) { |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 205 | gd->env_valid = 2; |
thomas.langer@lantiq.com | 97dfdcf | 2011-03-29 02:35:14 +0000 | [diff] [blame] | 206 | } else if (tmp_env1->flags == tmp_env2->flags) { |
Mario Schuknecht | 12f1a54 | 2015-03-24 08:59:00 +0100 | [diff] [blame] | 207 | gd->env_valid = 1; |
thomas.langer@lantiq.com | 97dfdcf | 2011-03-29 02:35:14 +0000 | [diff] [blame] | 208 | } else if (tmp_env1->flags == 0xFF) { |
Mario Schuknecht | 12f1a54 | 2015-03-24 08:59:00 +0100 | [diff] [blame] | 209 | gd->env_valid = 1; |
| 210 | } else if (tmp_env2->flags == 0xFF) { |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 211 | gd->env_valid = 2; |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 212 | } else { |
| 213 | /* |
| 214 | * this differs from code in env_flash.c, but I think a sane |
| 215 | * default path is desirable. |
| 216 | */ |
Mario Schuknecht | 12f1a54 | 2015-03-24 08:59:00 +0100 | [diff] [blame] | 217 | gd->env_valid = 1; |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 218 | } |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 219 | |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 220 | if (gd->env_valid == 1) |
| 221 | ep = tmp_env1; |
| 222 | else |
| 223 | ep = tmp_env2; |
| 224 | |
| 225 | ret = env_import((char *)ep, 0); |
| 226 | if (!ret) { |
| 227 | error("Cannot import environment: errno = %d\n", errno); |
Peng Fan | 3e21c72 | 2016-03-28 17:26:27 +0800 | [diff] [blame] | 228 | set_default_env("!env_import failed"); |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 229 | } |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 230 | |
| 231 | err_read: |
| 232 | spi_flash_free(env_flash); |
| 233 | env_flash = NULL; |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 234 | out: |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 235 | free(tmp_env1); |
| 236 | free(tmp_env2); |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 237 | } |
| 238 | #else |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 239 | int saveenv(void) |
| 240 | { |
Igor Grinberg | c3c1bed | 2011-11-07 01:14:08 +0000 | [diff] [blame] | 241 | u32 saved_size, saved_offset, sector = 1; |
Marek Vasut | d73c129 | 2014-03-05 19:59:50 +0100 | [diff] [blame] | 242 | char *saved_buffer = NULL; |
Igor Grinberg | c3c1bed | 2011-11-07 01:14:08 +0000 | [diff] [blame] | 243 | int ret = 1; |
Tom Rini | a840b8e | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 244 | env_t env_new; |
Gong Qianyu | 6d85a60 | 2016-01-26 15:06:42 +0800 | [diff] [blame] | 245 | #ifdef CONFIG_DM_SPI_FLASH |
| 246 | struct udevice *new; |
| 247 | |
Vignesh R | ae56979 | 2016-07-06 10:04:28 +0530 | [diff] [blame] | 248 | /* speed and mode will be read from DT */ |
Gong Qianyu | 6d85a60 | 2016-01-26 15:06:42 +0800 | [diff] [blame] | 249 | 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] | 250 | 0, 0, &new); |
Gong Qianyu | 6d85a60 | 2016-01-26 15:06:42 +0800 | [diff] [blame] | 251 | if (ret) { |
| 252 | set_default_env("!spi_flash_probe_bus_cs() failed"); |
| 253 | return 1; |
| 254 | } |
| 255 | |
| 256 | env_flash = dev_get_uclass_priv(new); |
| 257 | #else |
TsiChung Liew | 08792d7 | 2008-08-06 19:37:17 -0500 | [diff] [blame] | 258 | |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 259 | if (!env_flash) { |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 260 | env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, |
| 261 | CONFIG_ENV_SPI_CS, |
| 262 | CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE); |
| 263 | if (!env_flash) { |
| 264 | set_default_env("!spi_flash_probe() failed"); |
| 265 | return 1; |
| 266 | } |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 267 | } |
Gong Qianyu | 6d85a60 | 2016-01-26 15:06:42 +0800 | [diff] [blame] | 268 | #endif |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 269 | |
Mike Frysinger | 9af10d5 | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 270 | /* Is the sector larger than the env (i.e. embedded) */ |
| 271 | if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) { |
| 272 | saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE; |
| 273 | saved_offset = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE; |
| 274 | saved_buffer = malloc(saved_size); |
Igor Grinberg | c3c1bed | 2011-11-07 01:14:08 +0000 | [diff] [blame] | 275 | if (!saved_buffer) |
Mike Frysinger | 9af10d5 | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 276 | goto done; |
Igor Grinberg | c3c1bed | 2011-11-07 01:14:08 +0000 | [diff] [blame] | 277 | |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 278 | ret = spi_flash_read(env_flash, saved_offset, |
| 279 | saved_size, saved_buffer); |
Mike Frysinger | 9af10d5 | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 280 | if (ret) |
| 281 | goto done; |
| 282 | } |
| 283 | |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 284 | if (CONFIG_ENV_SIZE > CONFIG_ENV_SECT_SIZE) { |
| 285 | sector = CONFIG_ENV_SIZE / CONFIG_ENV_SECT_SIZE; |
| 286 | if (CONFIG_ENV_SIZE % CONFIG_ENV_SECT_SIZE) |
TsiChung Liew | 08792d7 | 2008-08-06 19:37:17 -0500 | [diff] [blame] | 287 | sector++; |
| 288 | } |
| 289 | |
Marek Vasut | d73c129 | 2014-03-05 19:59:50 +0100 | [diff] [blame] | 290 | ret = env_export(&env_new); |
| 291 | if (ret) |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 292 | goto done; |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 293 | |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 294 | puts("Erasing SPI flash..."); |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 295 | ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET, |
| 296 | sector * CONFIG_ENV_SECT_SIZE); |
Mike Frysinger | 9af10d5 | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 297 | if (ret) |
| 298 | goto done; |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 299 | |
| 300 | puts("Writing to SPI flash..."); |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 301 | ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET, |
Tom Rini | a840b8e | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 302 | CONFIG_ENV_SIZE, &env_new); |
Mike Frysinger | 9af10d5 | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 303 | if (ret) |
| 304 | goto done; |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 305 | |
Mike Frysinger | 9af10d5 | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 306 | if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) { |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 307 | ret = spi_flash_write(env_flash, saved_offset, |
| 308 | saved_size, saved_buffer); |
Mike Frysinger | 9af10d5 | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 309 | if (ret) |
| 310 | goto done; |
| 311 | } |
| 312 | |
| 313 | ret = 0; |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 314 | puts("done\n"); |
Mike Frysinger | 9af10d5 | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 315 | |
| 316 | done: |
| 317 | if (saved_buffer) |
| 318 | free(saved_buffer); |
Igor Grinberg | c3c1bed | 2011-11-07 01:14:08 +0000 | [diff] [blame] | 319 | |
Mike Frysinger | 9af10d5 | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 320 | return ret; |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | void env_relocate_spec(void) |
| 324 | { |
| 325 | int ret; |
Ying Zhang | 354846f | 2014-01-24 15:50:07 +0800 | [diff] [blame] | 326 | char *buf = NULL; |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 327 | |
Ravi Babu | 20d93fc | 2015-08-17 13:29:49 +0530 | [diff] [blame] | 328 | buf = (char *)memalign(ARCH_DMA_MINALIGN, CONFIG_ENV_SIZE); |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 329 | env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS, |
| 330 | CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 331 | if (!env_flash) { |
| 332 | set_default_env("!spi_flash_probe() failed"); |
Ying Zhang | 354846f | 2014-01-24 15:50:07 +0800 | [diff] [blame] | 333 | if (buf) |
| 334 | free(buf); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 335 | return; |
| 336 | } |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 337 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 338 | ret = spi_flash_read(env_flash, |
| 339 | CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf); |
| 340 | if (ret) { |
| 341 | set_default_env("!spi_flash_read() failed"); |
| 342 | goto out; |
| 343 | } |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 344 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 345 | ret = env_import(buf, 1); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 346 | if (ret) |
| 347 | gd->env_valid = 1; |
| 348 | out: |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 349 | spi_flash_free(env_flash); |
Ying Zhang | 354846f | 2014-01-24 15:50:07 +0800 | [diff] [blame] | 350 | if (buf) |
| 351 | free(buf); |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 352 | env_flash = NULL; |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 353 | } |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 354 | #endif |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 355 | |
| 356 | int env_init(void) |
| 357 | { |
| 358 | /* SPI flash isn't usable before relocation */ |
| 359 | gd->env_addr = (ulong)&default_environment[0]; |
| 360 | gd->env_valid = 1; |
| 361 | |
| 362 | return 0; |
| 363 | } |