spi_nand.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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 2008
  7. * Stuart Wood, Lab X Technologies <stuart.wood@labxtechnologies.com>
  8. *
  9. * (C) Copyright 2004
  10. * Jian Zhang, Texas Instruments, jzhang@ti.com.
  11. *
  12. * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  13. * Andreas Heppel <aheppel@sysgo.de>
  14. */
  15. #include <common.h>
  16. #include <command.h>
  17. #include <env.h>
  18. #include <env_internal.h>
  19. #include <asm/global_data.h>
  20. #include <linux/stddef.h>
  21. #include <malloc.h>
  22. #include <memalign.h>
  23. #include <nand.h>
  24. #include <search.h>
  25. #include <errno.h>
  26. #include <u-boot/crc.h>
  27. #include <mtd.h>
  28. #if defined(ENV_IS_EMBEDDED)
  29. static env_t *env_ptr = &environment;
  30. #endif /* ENV_IS_EMBEDDED */
  31. DECLARE_GLOBAL_DATA_PTR;
  32. static int setup_mtd_device(struct mtd_info **mtd_env)
  33. {
  34. struct mtd_info *mtd;
  35. mtd_probe_devices();
  36. mtd = get_mtd_device_nm(CONFIG_SYS_SNAND_ENV_DEV);
  37. if (IS_ERR_OR_NULL(mtd)) {
  38. env_set_default("get_mtd_device_nm() failed", 0);
  39. return mtd ? PTR_ERR(mtd) : -EINVAL;
  40. }
  41. *mtd_env = mtd;
  42. return 0;
  43. }
  44. /*
  45. * This is called before nand_init() so we can't read NAND to
  46. * validate env data.
  47. *
  48. * Mark it OK for now. env_relocate() in env_common.c will call our
  49. * relocate function which does the real validation.
  50. *
  51. * When using a NAND boot image (like sequoia_nand), the environment
  52. * can be embedded or attached to the U-Boot image in NAND flash.
  53. * This way the SPL loads not only the U-Boot image from NAND but
  54. * also the environment.
  55. */
  56. static int env_spinand_init(void)
  57. {
  58. #if defined(ENV_IS_EMBEDDED)
  59. int crc1_ok = 0, crc2_ok = 0;
  60. env_t *tmp_env1;
  61. #ifdef CONFIG_ENV_OFFSET_REDUND
  62. env_t *tmp_env2;
  63. tmp_env2 = (env_t *)((ulong)env_ptr + CONFIG_ENV_SIZE);
  64. crc2_ok = crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc;
  65. #endif
  66. tmp_env1 = env_ptr;
  67. crc1_ok = crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc;
  68. if (!crc1_ok && !crc2_ok) {
  69. gd->env_addr = 0;
  70. gd->env_valid = ENV_INVALID;
  71. return 0;
  72. } else if (crc1_ok && !crc2_ok) {
  73. gd->env_valid = ENV_VALID;
  74. }
  75. #ifdef CONFIG_ENV_OFFSET_REDUND
  76. else if (!crc1_ok && crc2_ok) {
  77. gd->env_valid = ENV_REDUND;
  78. } else {
  79. /* both ok - check serial */
  80. if (tmp_env1->flags == 255 && tmp_env2->flags == 0)
  81. gd->env_valid = ENV_REDUND;
  82. else if (tmp_env2->flags == 255 && tmp_env1->flags == 0)
  83. gd->env_valid = ENV_VALID;
  84. else if (tmp_env1->flags > tmp_env2->flags)
  85. gd->env_valid = ENV_VALID;
  86. else if (tmp_env2->flags > tmp_env1->flags)
  87. gd->env_valid = ENV_REDUND;
  88. else /* flags are equal - almost impossible */
  89. gd->env_valid = ENV_VALID;
  90. }
  91. if (gd->env_valid == ENV_REDUND)
  92. env_ptr = tmp_env2;
  93. else
  94. #endif
  95. if (gd->env_valid == ENV_VALID)
  96. env_ptr = tmp_env1;
  97. gd->env_addr = (ulong)env_ptr->data;
  98. #else /* ENV_IS_EMBEDDED || CONFIG_NAND_ENV_DST */
  99. gd->env_valid = ENV_INVALID;
  100. #endif /* ENV_IS_EMBEDDED || CONFIG_NAND_ENV_DST */
  101. return 0;
  102. }
  103. struct spinand_erase_options {
  104. loff_t length; /* number of bytes to erase */
  105. loff_t offset; /* first address in SPINAND to erase */
  106. };
  107. struct spinand_env_location {
  108. const char *name;
  109. const struct spinand_erase_options erase_opts;
  110. };
  111. static int spinandwriteenv(size_t offset, u_char *buf)
  112. {
  113. size_t end = offset + CONFIG_ENV_RANGE;
  114. size_t amount_saved = 0;
  115. size_t blocksize, len;
  116. struct mtd_info *mtd;
  117. u_char *char_ptr;
  118. int ret;
  119. ret = setup_mtd_device(&mtd);
  120. if (ret)
  121. return ret;
  122. blocksize = mtd->erasesize;
  123. len = min(blocksize, (size_t)CONFIG_ENV_SIZE);
  124. while (amount_saved < CONFIG_ENV_SIZE && offset < end) {
  125. if (mtd_block_isbad(mtd, offset)) {
  126. offset += blocksize;
  127. } else {
  128. char_ptr = &buf[amount_saved];
  129. if (mtd_write(mtd, offset, mtd->writesize, &len,
  130. char_ptr))
  131. return 1;
  132. offset += len;
  133. amount_saved += len;
  134. }
  135. }
  136. if (amount_saved != CONFIG_ENV_SIZE)
  137. return 1;
  138. return 0;
  139. }
  140. int spinand_erase_opts(struct mtd_info *mtd_env,
  141. const struct spinand_erase_options *opts)
  142. {
  143. u32 sect_size, sect_num;
  144. struct erase_info ei;
  145. int ret;
  146. sect_size = mtd_env->erasesize;
  147. sect_num = DIV_ROUND_UP(CONFIG_ENV_SIZE, sect_size);
  148. ei.mtd = mtd_env;
  149. ei.addr = opts->offset;
  150. ei.len = sect_num * sect_size;
  151. ret = mtd_erase(mtd_env, &ei);
  152. if (ret)
  153. goto done;
  154. return 0;
  155. done:
  156. return ret;
  157. }
  158. static int env_spinand_erase_save(const struct spinand_env_location *location, u_char *env_new)
  159. {
  160. struct mtd_info *mtd_env;
  161. int ret = 0;
  162. ret = setup_mtd_device(&mtd_env);
  163. if (ret)
  164. return ret;
  165. printf("Erasing %s...\n", location->name);
  166. if (spinand_erase_opts(mtd_env, &location->erase_opts))
  167. return 1;
  168. printf("Writing to %s... ", location->name);
  169. ret = spinandwriteenv(location->erase_opts.offset, env_new);
  170. puts(ret ? "FAILED!\n" : "OK\n");
  171. return ret;
  172. }
  173. static int env_spinand_save(void)
  174. {
  175. int ret = 0;
  176. ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
  177. int env_idx = 0;
  178. static const struct spinand_env_location location[] = {
  179. {
  180. .name = "SPINAND",
  181. .erase_opts = {
  182. .length = CONFIG_ENV_RANGE,
  183. .offset = CONFIG_ENV_OFFSET,
  184. },
  185. },
  186. #ifdef CONFIG_ENV_OFFSET_REDUND
  187. {
  188. .name = "redundant SPINAND",
  189. .erase_opts = {
  190. .length = CONFIG_ENV_RANGE,
  191. .offset = CONFIG_ENV_OFFSET_REDUND,
  192. },
  193. },
  194. #endif
  195. };
  196. ret = env_export(env_new);
  197. if (ret)
  198. return ret;
  199. #ifdef CONFIG_ENV_OFFSET_REDUND
  200. env_idx = (gd->env_valid == ENV_VALID);
  201. #endif
  202. ret = env_spinand_erase_save(&location[env_idx], (u_char *)env_new);
  203. #ifdef CONFIG_ENV_OFFSET_REDUND
  204. if (!ret) {
  205. /* preset other copy for next write */
  206. gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID :
  207. ENV_REDUND;
  208. return ret;
  209. }
  210. env_idx = (env_idx + 1) & 1;
  211. ret = env_spinand_erase_save(&location[env_idx], (u_char *)env_new);
  212. if (!ret)
  213. printf("Warning: primary env write failed, redundancy is lost!\n");
  214. #endif
  215. return ret;
  216. }
  217. static int spinandreadenv(size_t offset, u_char *buf)
  218. {
  219. size_t end = offset + CONFIG_ENV_RANGE;
  220. size_t amount_loaded = 0;
  221. size_t blocksize, len;
  222. struct mtd_info *mtd_env;
  223. u_char *char_ptr;
  224. size_t ret_len;
  225. int ret;
  226. ret = setup_mtd_device(&mtd_env);
  227. if (ret)
  228. return 1;
  229. blocksize = mtd_env->erasesize;
  230. len = min(blocksize, (size_t)CONFIG_ENV_SIZE);
  231. while (amount_loaded < CONFIG_ENV_SIZE && offset < end) {
  232. if (mtd_block_isbad(mtd_env, offset)) {
  233. offset += blocksize;
  234. } else {
  235. char_ptr = &buf[amount_loaded];
  236. if (mtd_read(mtd_env, offset, mtd_env->writesize,
  237. &ret_len, char_ptr))
  238. return 1;
  239. offset += ret_len;
  240. amount_loaded += ret_len;
  241. }
  242. }
  243. if (amount_loaded != CONFIG_ENV_SIZE)
  244. return 1;
  245. return 0;
  246. }
  247. static int env_spinand_load(void)
  248. {
  249. int read1_fail, read2_fail;
  250. env_t *tmp_env1, *tmp_env2;
  251. int ret = 0;
  252. tmp_env1 = (env_t *)memalign(ARCH_DMA_MINALIGN, CONFIG_ENV_SIZE);
  253. if (!tmp_env1) {
  254. env_set_default("tmp_env1 malloc() failed", 0);
  255. return -EIO;
  256. }
  257. tmp_env2 = (env_t *)memalign(ARCH_DMA_MINALIGN, CONFIG_ENV_SIZE);
  258. if (!tmp_env2) {
  259. env_set_default("tmp_env2 malloc() failed", 0);
  260. return -EIO;
  261. }
  262. if (tmp_env1 == NULL || tmp_env2 == NULL) {
  263. puts("Can't allocate buffers for environment\n");
  264. env_set_default("malloc() failed", 0);
  265. ret = -EIO;
  266. goto done;
  267. }
  268. read1_fail = spinandreadenv(CONFIG_ENV_OFFSET, (u_char *) tmp_env1);
  269. read2_fail = spinandreadenv(CONFIG_ENV_OFFSET_REDUND, (u_char *) tmp_env2);
  270. ret = env_import_redund((char *)tmp_env1, read1_fail, (char *)tmp_env2,
  271. read2_fail, H_EXTERNAL);
  272. done:
  273. free(tmp_env1);
  274. free(tmp_env2);
  275. return ret;
  276. }
  277. U_BOOT_ENV_LOCATION(snand) = {
  278. .location = ENVL_SPI_NAND_FLASH,
  279. ENV_NAME("SPINAND")
  280. .load = env_spinand_load,
  281. .save = env_save_ptr(env_spinand_save),
  282. .init = env_spinand_init,
  283. };