sf.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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. ret = -ENOMEM;
  177. goto done;
  178. }
  179. ret = spi_flash_read(env_flash, saved_offset,
  180. saved_size, saved_buffer);
  181. if (ret)
  182. goto done;
  183. }
  184. ret = env_export(&env_new);
  185. if (ret)
  186. goto done;
  187. sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, sect_size);
  188. puts("Erasing SPI flash...");
  189. ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET,
  190. sector * sect_size);
  191. if (ret)
  192. goto done;
  193. puts("Writing to SPI flash...");
  194. ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET,
  195. CONFIG_ENV_SIZE, &env_new);
  196. if (ret)
  197. goto done;
  198. if (sect_size > CONFIG_ENV_SIZE) {
  199. ret = spi_flash_write(env_flash, saved_offset,
  200. saved_size, saved_buffer);
  201. if (ret)
  202. goto done;
  203. }
  204. ret = 0;
  205. puts("done\n");
  206. done:
  207. spi_flash_free(env_flash);
  208. if (saved_buffer)
  209. free(saved_buffer);
  210. return ret;
  211. }
  212. static int env_sf_load(void)
  213. {
  214. int ret;
  215. char *buf = NULL;
  216. struct spi_flash *env_flash;
  217. buf = (char *)memalign(ARCH_DMA_MINALIGN, CONFIG_ENV_SIZE);
  218. if (!buf) {
  219. env_set_default("malloc() failed", 0);
  220. return -EIO;
  221. }
  222. ret = setup_flash_device(&env_flash);
  223. if (ret)
  224. goto out;
  225. ret = spi_flash_read(env_flash,
  226. CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf);
  227. if (ret) {
  228. env_set_default("spi_flash_read() failed", 0);
  229. goto err_read;
  230. }
  231. ret = env_import(buf, 1, H_EXTERNAL);
  232. if (!ret)
  233. gd->env_valid = ENV_VALID;
  234. err_read:
  235. spi_flash_free(env_flash);
  236. out:
  237. free(buf);
  238. return ret;
  239. }
  240. #endif
  241. static int env_sf_erase(void)
  242. {
  243. int ret;
  244. env_t env;
  245. struct spi_flash *env_flash;
  246. ret = setup_flash_device(&env_flash);
  247. if (ret)
  248. return ret;
  249. memset(&env, 0, sizeof(env_t));
  250. ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, &env);
  251. if (ret)
  252. goto done;
  253. if (ENV_OFFSET_REDUND != OFFSET_INVALID)
  254. ret = spi_flash_write(env_flash, ENV_OFFSET_REDUND, CONFIG_ENV_SIZE, &env);
  255. done:
  256. spi_flash_free(env_flash);
  257. return ret;
  258. }
  259. __weak void *env_sf_get_env_addr(void)
  260. {
  261. #ifndef CONFIG_SPL_BUILD
  262. return (void *)CONFIG_ENV_ADDR;
  263. #else
  264. return NULL;
  265. #endif
  266. }
  267. /*
  268. * check if Environment on CONFIG_ENV_ADDR is valid.
  269. */
  270. static int env_sf_init_addr(void)
  271. {
  272. env_t *env_ptr = (env_t *)env_sf_get_env_addr();
  273. if (!env_ptr)
  274. return -ENOENT;
  275. if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
  276. gd->env_addr = (ulong)&(env_ptr->data);
  277. gd->env_valid = ENV_VALID;
  278. } else {
  279. gd->env_valid = ENV_INVALID;
  280. }
  281. return 0;
  282. }
  283. #if defined(CONFIG_ENV_SPI_EARLY)
  284. /*
  285. * early load environment from SPI flash (before relocation)
  286. * and check if it is valid.
  287. */
  288. static int env_sf_init_early(void)
  289. {
  290. int ret;
  291. int read1_fail;
  292. int read2_fail;
  293. int crc1_ok;
  294. env_t *tmp_env2 = NULL;
  295. env_t *tmp_env1;
  296. struct spi_flash *env_flash;
  297. /*
  298. * if malloc is not ready yet, we cannot use
  299. * this part yet.
  300. */
  301. if (!gd->malloc_limit)
  302. return -ENOENT;
  303. tmp_env1 = (env_t *)memalign(ARCH_DMA_MINALIGN,
  304. CONFIG_ENV_SIZE);
  305. if (IS_ENABLED(CONFIG_SYS_REDUNDAND_ENVIRONMENT))
  306. tmp_env2 = (env_t *)memalign(ARCH_DMA_MINALIGN,
  307. CONFIG_ENV_SIZE);
  308. if (!tmp_env1 || !tmp_env2)
  309. goto out;
  310. ret = setup_flash_device(&env_flash);
  311. if (ret)
  312. goto out;
  313. read1_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET,
  314. CONFIG_ENV_SIZE, tmp_env1);
  315. if (IS_ENABLED(CONFIG_SYS_REDUNDAND_ENVIRONMENT)) {
  316. read2_fail = spi_flash_read(env_flash,
  317. CONFIG_ENV_OFFSET_REDUND,
  318. CONFIG_ENV_SIZE, tmp_env2);
  319. ret = env_check_redund((char *)tmp_env1, read1_fail,
  320. (char *)tmp_env2, read2_fail);
  321. if (ret < 0)
  322. goto err_read;
  323. if (gd->env_valid == ENV_VALID)
  324. gd->env_addr = (unsigned long)&tmp_env1->data;
  325. else
  326. gd->env_addr = (unsigned long)&tmp_env2->data;
  327. } else {
  328. if (read1_fail)
  329. goto err_read;
  330. crc1_ok = crc32(0, tmp_env1->data, ENV_SIZE) ==
  331. tmp_env1->crc;
  332. if (!crc1_ok)
  333. goto err_read;
  334. /* if valid -> this is our env */
  335. gd->env_valid = ENV_VALID;
  336. gd->env_addr = (unsigned long)&tmp_env1->data;
  337. }
  338. spi_flash_free(env_flash);
  339. return 0;
  340. err_read:
  341. spi_flash_free(env_flash);
  342. free(tmp_env1);
  343. if (IS_ENABLED(CONFIG_SYS_REDUNDAND_ENVIRONMENT))
  344. free(tmp_env2);
  345. out:
  346. /* env is not valid. always return 0 */
  347. gd->env_valid = ENV_INVALID;
  348. return 0;
  349. }
  350. #endif
  351. static int env_sf_init(void)
  352. {
  353. int ret = env_sf_init_addr();
  354. if (ret != -ENOENT)
  355. return ret;
  356. #ifdef CONFIG_ENV_SPI_EARLY
  357. return env_sf_init_early();
  358. #endif
  359. /*
  360. * return here -ENOENT, so env_init()
  361. * can set the init bit and later if no
  362. * other Environment storage is defined
  363. * can set the default environment
  364. */
  365. return -ENOENT;
  366. }
  367. U_BOOT_ENV_LOCATION(sf) = {
  368. .location = ENVL_SPI_FLASH,
  369. ENV_NAME("SPIFlash")
  370. .load = env_sf_load,
  371. .save = ENV_SAVE_PTR(env_sf_save),
  372. .erase = ENV_ERASE_PTR(env_sf_erase),
  373. .init = env_sf_init,
  374. };