sf.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2000-2010
  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
  10. */
  11. #include <common.h>
  12. #include <dm.h>
  13. #include <env.h>
  14. #include <env_internal.h>
  15. #include <malloc.h>
  16. #include <spi.h>
  17. #include <spi_flash.h>
  18. #include <search.h>
  19. #include <errno.h>
  20. #include <uuid.h>
  21. #include <asm/cache.h>
  22. #include <asm/global_data.h>
  23. #include <dm/device-internal.h>
  24. #include <u-boot/crc.h>
  25. #define OFFSET_INVALID (~(u32)0)
  26. #ifdef CONFIG_ENV_OFFSET_REDUND
  27. #define ENV_OFFSET_REDUND CONFIG_ENV_OFFSET_REDUND
  28. static ulong env_offset = CONFIG_ENV_OFFSET;
  29. static ulong env_new_offset = CONFIG_ENV_OFFSET_REDUND;
  30. #else
  31. #define ENV_OFFSET_REDUND OFFSET_INVALID
  32. #endif /* CONFIG_ENV_OFFSET_REDUND */
  33. DECLARE_GLOBAL_DATA_PTR;
  34. static int setup_flash_device(struct spi_flash **env_flash)
  35. {
  36. #if CONFIG_IS_ENABLED(DM_SPI_FLASH)
  37. struct udevice *new;
  38. int ret;
  39. /* speed and mode will be read from DT */
  40. ret = spi_flash_probe_bus_cs(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
  41. &new);
  42. if (ret) {
  43. env_set_default("spi_flash_probe_bus_cs() failed", 0);
  44. return ret;
  45. }
  46. *env_flash = dev_get_uclass_priv(new);
  47. #else
  48. *env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
  49. CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
  50. if (!*env_flash) {
  51. env_set_default("spi_flash_probe() failed", 0);
  52. return -EIO;
  53. }
  54. #endif
  55. return 0;
  56. }
  57. #if defined(CONFIG_ENV_OFFSET_REDUND)
  58. static int env_sf_save(void)
  59. {
  60. env_t env_new;
  61. char *saved_buffer = NULL, flag = ENV_REDUND_OBSOLETE;
  62. u32 saved_size = 0, saved_offset = 0, sector;
  63. u32 sect_size = CONFIG_ENV_SECT_SIZE;
  64. int ret;
  65. struct spi_flash *env_flash;
  66. ret = setup_flash_device(&env_flash);
  67. if (ret)
  68. return ret;
  69. if (IS_ENABLED(CONFIG_ENV_SECT_SIZE_AUTO))
  70. sect_size = env_flash->mtd.erasesize;
  71. ret = env_export(&env_new);
  72. if (ret)
  73. return -EIO;
  74. env_new.flags = ENV_REDUND_ACTIVE;
  75. if (gd->env_valid == ENV_VALID) {
  76. env_new_offset = CONFIG_ENV_OFFSET_REDUND;
  77. env_offset = CONFIG_ENV_OFFSET;
  78. } else {
  79. env_new_offset = CONFIG_ENV_OFFSET;
  80. env_offset = CONFIG_ENV_OFFSET_REDUND;
  81. }
  82. /* Is the sector larger than the env (i.e. embedded) */
  83. if (sect_size > CONFIG_ENV_SIZE) {
  84. saved_size = sect_size - CONFIG_ENV_SIZE;
  85. saved_offset = env_new_offset + CONFIG_ENV_SIZE;
  86. saved_buffer = memalign(ARCH_DMA_MINALIGN, saved_size);
  87. if (!saved_buffer) {
  88. ret = -ENOMEM;
  89. goto done;
  90. }
  91. ret = spi_flash_read(env_flash, saved_offset,
  92. saved_size, saved_buffer);
  93. if (ret)
  94. goto done;
  95. }
  96. sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, sect_size);
  97. puts("Erasing SPI flash...");
  98. ret = spi_flash_erase(env_flash, env_new_offset,
  99. sector * sect_size);
  100. if (ret)
  101. goto done;
  102. puts("Writing to SPI flash...");
  103. ret = spi_flash_write(env_flash, env_new_offset,
  104. CONFIG_ENV_SIZE, &env_new);
  105. if (ret)
  106. goto done;
  107. if (sect_size > CONFIG_ENV_SIZE) {
  108. ret = spi_flash_write(env_flash, saved_offset,
  109. saved_size, saved_buffer);
  110. if (ret)
  111. goto done;
  112. }
  113. ret = spi_flash_write(env_flash, env_offset + offsetof(env_t, flags),
  114. sizeof(env_new.flags), &flag);
  115. if (ret)
  116. goto done;
  117. puts("done\n");
  118. gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND;
  119. printf("Valid environment: %d\n", (int)gd->env_valid);
  120. done:
  121. spi_flash_free(env_flash);
  122. if (saved_buffer)
  123. free(saved_buffer);
  124. return ret;
  125. }
  126. static int env_sf_load(void)
  127. {
  128. int ret;
  129. int read1_fail, read2_fail;
  130. env_t *tmp_env1, *tmp_env2;
  131. struct spi_flash *env_flash;
  132. tmp_env1 = (env_t *)memalign(ARCH_DMA_MINALIGN,
  133. CONFIG_ENV_SIZE);
  134. tmp_env2 = (env_t *)memalign(ARCH_DMA_MINALIGN,
  135. CONFIG_ENV_SIZE);
  136. if (!tmp_env1 || !tmp_env2) {
  137. env_set_default("malloc() failed", 0);
  138. ret = -EIO;
  139. goto out;
  140. }
  141. ret = setup_flash_device(&env_flash);
  142. if (ret)
  143. goto out;
  144. read1_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET,
  145. CONFIG_ENV_SIZE, tmp_env1);
  146. read2_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET_REDUND,
  147. CONFIG_ENV_SIZE, tmp_env2);
  148. ret = env_import_redund((char *)tmp_env1, read1_fail, (char *)tmp_env2,
  149. read2_fail, H_EXTERNAL);
  150. spi_flash_free(env_flash);
  151. out:
  152. free(tmp_env1);
  153. free(tmp_env2);
  154. return ret;
  155. }
  156. #else
  157. static int env_sf_save(void)
  158. {
  159. u32 saved_size = 0, saved_offset = 0, sector;
  160. u32 sect_size = CONFIG_ENV_SECT_SIZE;
  161. char *saved_buffer = NULL;
  162. int ret = 1;
  163. env_t env_new;
  164. struct spi_flash *env_flash;
  165. ret = setup_flash_device(&env_flash);
  166. if (ret)
  167. return ret;
  168. if (IS_ENABLED(CONFIG_ENV_SECT_SIZE_AUTO))
  169. sect_size = env_flash->mtd.erasesize;
  170. /* Is the sector larger than the env (i.e. embedded) */
  171. if (sect_size > CONFIG_ENV_SIZE) {
  172. saved_size = sect_size - CONFIG_ENV_SIZE;
  173. saved_offset = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE;
  174. saved_buffer = malloc(saved_size);
  175. if (!saved_buffer)
  176. goto done;
  177. ret = spi_flash_read(env_flash, saved_offset,
  178. saved_size, saved_buffer);
  179. if (ret)
  180. goto done;
  181. }
  182. ret = env_export(&env_new);
  183. if (ret)
  184. goto done;
  185. sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, sect_size);
  186. puts("Erasing SPI flash...");
  187. ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET,
  188. sector * sect_size);
  189. if (ret)
  190. goto done;
  191. puts("Writing to SPI flash...");
  192. ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET,
  193. CONFIG_ENV_SIZE, &env_new);
  194. if (ret)
  195. goto done;
  196. if (sect_size > CONFIG_ENV_SIZE) {
  197. ret = spi_flash_write(env_flash, saved_offset,
  198. saved_size, saved_buffer);
  199. if (ret)
  200. goto done;
  201. }
  202. ret = 0;
  203. puts("done\n");
  204. done:
  205. spi_flash_free(env_flash);
  206. if (saved_buffer)
  207. free(saved_buffer);
  208. return ret;
  209. }
  210. static int env_sf_load(void)
  211. {
  212. int ret;
  213. char *buf = NULL;
  214. struct spi_flash *env_flash;
  215. buf = (char *)memalign(ARCH_DMA_MINALIGN, CONFIG_ENV_SIZE);
  216. if (!buf) {
  217. env_set_default("malloc() failed", 0);
  218. return -EIO;
  219. }
  220. ret = setup_flash_device(&env_flash);
  221. if (ret)
  222. goto out;
  223. ret = spi_flash_read(env_flash,
  224. CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf);
  225. if (ret) {
  226. env_set_default("spi_flash_read() failed", 0);
  227. goto err_read;
  228. }
  229. ret = env_import(buf, 1, H_EXTERNAL);
  230. if (!ret)
  231. gd->env_valid = ENV_VALID;
  232. err_read:
  233. spi_flash_free(env_flash);
  234. out:
  235. free(buf);
  236. return ret;
  237. }
  238. #endif
  239. static int env_sf_erase(void)
  240. {
  241. int ret;
  242. env_t env;
  243. struct spi_flash *env_flash;
  244. ret = setup_flash_device(&env_flash);
  245. if (ret)
  246. return ret;
  247. memset(&env, 0, sizeof(env_t));
  248. ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, &env);
  249. if (ret)
  250. goto done;
  251. if (ENV_OFFSET_REDUND != OFFSET_INVALID)
  252. ret = spi_flash_write(env_flash, ENV_OFFSET_REDUND, CONFIG_ENV_SIZE, &env);
  253. done:
  254. spi_flash_free(env_flash);
  255. return ret;
  256. }
  257. __weak void *env_sf_get_env_addr(void)
  258. {
  259. #ifndef CONFIG_SPL_BUILD
  260. return (void *)CONFIG_ENV_ADDR;
  261. #else
  262. return NULL;
  263. #endif
  264. }
  265. /*
  266. * check if Environment on CONFIG_ENV_ADDR is valid.
  267. */
  268. static int env_sf_init_addr(void)
  269. {
  270. env_t *env_ptr = (env_t *)env_sf_get_env_addr();
  271. if (!env_ptr)
  272. return -ENOENT;
  273. if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
  274. gd->env_addr = (ulong)&(env_ptr->data);
  275. gd->env_valid = ENV_VALID;
  276. } else {
  277. gd->env_valid = ENV_INVALID;
  278. }
  279. return 0;
  280. }
  281. #if defined(CONFIG_ENV_SPI_EARLY)
  282. /*
  283. * early load environment from SPI flash (before relocation)
  284. * and check if it is valid.
  285. */
  286. static int env_sf_init_early(void)
  287. {
  288. int ret;
  289. int read1_fail;
  290. int read2_fail;
  291. int crc1_ok;
  292. env_t *tmp_env2 = NULL;
  293. env_t *tmp_env1;
  294. struct spi_flash *env_flash;
  295. /*
  296. * if malloc is not ready yet, we cannot use
  297. * this part yet.
  298. */
  299. if (!gd->malloc_limit)
  300. return -ENOENT;
  301. tmp_env1 = (env_t *)memalign(ARCH_DMA_MINALIGN,
  302. CONFIG_ENV_SIZE);
  303. if (IS_ENABLED(CONFIG_SYS_REDUNDAND_ENVIRONMENT))
  304. tmp_env2 = (env_t *)memalign(ARCH_DMA_MINALIGN,
  305. CONFIG_ENV_SIZE);
  306. if (!tmp_env1 || !tmp_env2)
  307. goto out;
  308. ret = setup_flash_device(&env_flash);
  309. if (ret)
  310. goto out;
  311. read1_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET,
  312. CONFIG_ENV_SIZE, tmp_env1);
  313. if (IS_ENABLED(CONFIG_SYS_REDUNDAND_ENVIRONMENT)) {
  314. read2_fail = spi_flash_read(env_flash,
  315. CONFIG_ENV_OFFSET_REDUND,
  316. CONFIG_ENV_SIZE, tmp_env2);
  317. ret = env_check_redund((char *)tmp_env1, read1_fail,
  318. (char *)tmp_env2, read2_fail);
  319. if (ret < 0)
  320. goto err_read;
  321. if (gd->env_valid == ENV_VALID)
  322. gd->env_addr = (unsigned long)&tmp_env1->data;
  323. else
  324. gd->env_addr = (unsigned long)&tmp_env2->data;
  325. } else {
  326. if (read1_fail)
  327. goto err_read;
  328. crc1_ok = crc32(0, tmp_env1->data, ENV_SIZE) ==
  329. tmp_env1->crc;
  330. if (!crc1_ok)
  331. goto err_read;
  332. /* if valid -> this is our env */
  333. gd->env_valid = ENV_VALID;
  334. gd->env_addr = (unsigned long)&tmp_env1->data;
  335. }
  336. spi_flash_free(env_flash);
  337. return 0;
  338. err_read:
  339. spi_flash_free(env_flash);
  340. free(tmp_env1);
  341. if (IS_ENABLED(CONFIG_SYS_REDUNDAND_ENVIRONMENT))
  342. free(tmp_env2);
  343. out:
  344. /* env is not valid. always return 0 */
  345. gd->env_valid = ENV_INVALID;
  346. return 0;
  347. }
  348. #endif
  349. static int env_sf_init(void)
  350. {
  351. int ret = env_sf_init_addr();
  352. if (ret != -ENOENT)
  353. return ret;
  354. #ifdef CONFIG_ENV_SPI_EARLY
  355. return env_sf_init_early();
  356. #endif
  357. /*
  358. * return here -ENOENT, so env_init()
  359. * can set the init bit and later if no
  360. * other Environment storage is defined
  361. * can set the default environment
  362. */
  363. return -ENOENT;
  364. }
  365. U_BOOT_ENV_LOCATION(sf) = {
  366. .location = ENVL_SPI_FLASH,
  367. ENV_NAME("SPIFlash")
  368. .load = env_sf_load,
  369. .save = ENV_SAVE_PTR(env_sf_save),
  370. .erase = ENV_ERASE_PTR(env_sf_erase),
  371. .init = env_sf_init,
  372. };