sf.c 9.5 KB

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