spl_nand.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2011
  4. * Corscience GmbH & Co. KG - Simon Schwarz <schwarz@corscience.de>
  5. */
  6. #include <common.h>
  7. #include <config.h>
  8. #include <spl.h>
  9. #include <asm/io.h>
  10. #include <nand.h>
  11. #include <linux/libfdt_env.h>
  12. #include <fdt.h>
  13. #if defined(CONFIG_SPL_NAND_RAW_ONLY)
  14. int spl_nand_load_image(struct spl_image_info *spl_image,
  15. struct spl_boot_device *bootdev)
  16. {
  17. nand_init();
  18. nand_spl_load_image(CONFIG_SYS_NAND_U_BOOT_OFFS,
  19. CONFIG_SYS_NAND_U_BOOT_SIZE,
  20. (void *)CONFIG_SYS_NAND_U_BOOT_DST);
  21. spl_set_header_raw_uboot(spl_image);
  22. nand_deselect();
  23. return 0;
  24. }
  25. #else
  26. static ulong spl_nand_fit_read(struct spl_load_info *load, ulong offs,
  27. ulong size, void *dst)
  28. {
  29. int ret;
  30. ret = nand_spl_load_image(offs, size, dst);
  31. if (!ret)
  32. return size;
  33. else
  34. return 0;
  35. }
  36. static int spl_nand_load_element(struct spl_image_info *spl_image,
  37. int offset, struct image_header *header)
  38. {
  39. int err;
  40. err = nand_spl_load_image(offset, sizeof(*header), (void *)header);
  41. if (err)
  42. return err;
  43. if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
  44. image_get_magic(header) == FDT_MAGIC) {
  45. struct spl_load_info load;
  46. debug("Found FIT\n");
  47. load.dev = NULL;
  48. load.priv = NULL;
  49. load.filename = NULL;
  50. load.bl_len = 1;
  51. load.read = spl_nand_fit_read;
  52. return spl_load_simple_fit(spl_image, &load, offset, header);
  53. } else {
  54. err = spl_parse_image_header(spl_image, header);
  55. if (err)
  56. return err;
  57. return nand_spl_load_image(offset, spl_image->size,
  58. (void *)(ulong)spl_image->load_addr);
  59. }
  60. }
  61. static int spl_nand_load_image(struct spl_image_info *spl_image,
  62. struct spl_boot_device *bootdev)
  63. {
  64. int err;
  65. struct image_header *header;
  66. int *src __attribute__((unused));
  67. int *dst __attribute__((unused));
  68. #ifdef CONFIG_SPL_NAND_SOFTECC
  69. debug("spl: nand - using sw ecc\n");
  70. #else
  71. debug("spl: nand - using hw ecc\n");
  72. #endif
  73. nand_init();
  74. /*use CONFIG_SYS_TEXT_BASE as temporary storage area */
  75. header = (struct image_header *)(CONFIG_SYS_TEXT_BASE);
  76. #ifdef CONFIG_SPL_OS_BOOT
  77. if (!spl_start_uboot()) {
  78. /*
  79. * load parameter image
  80. * load to temp position since nand_spl_load_image reads
  81. * a whole block which is typically larger than
  82. * CONFIG_CMD_SPL_WRITE_SIZE therefore may overwrite
  83. * following sections like BSS
  84. */
  85. nand_spl_load_image(CONFIG_CMD_SPL_NAND_OFS,
  86. CONFIG_CMD_SPL_WRITE_SIZE,
  87. (void *)CONFIG_SYS_TEXT_BASE);
  88. /* copy to destintion */
  89. for (dst = (int *)CONFIG_SYS_SPL_ARGS_ADDR,
  90. src = (int *)CONFIG_SYS_TEXT_BASE;
  91. src < (int *)(CONFIG_SYS_TEXT_BASE +
  92. CONFIG_CMD_SPL_WRITE_SIZE);
  93. src++, dst++) {
  94. writel(readl(src), dst);
  95. }
  96. /* load linux */
  97. nand_spl_load_image(CONFIG_SYS_NAND_SPL_KERNEL_OFFS,
  98. sizeof(*header), (void *)header);
  99. err = spl_parse_image_header(spl_image, header);
  100. if (err)
  101. return err;
  102. if (header->ih_os == IH_OS_LINUX) {
  103. /* happy - was a linux */
  104. err = nand_spl_load_image(
  105. CONFIG_SYS_NAND_SPL_KERNEL_OFFS,
  106. spl_image->size,
  107. (void *)spl_image->load_addr);
  108. nand_deselect();
  109. return err;
  110. } else {
  111. puts("The Expected Linux image was not "
  112. "found. Please check your NAND "
  113. "configuration.\n");
  114. puts("Trying to start u-boot now...\n");
  115. }
  116. }
  117. #endif
  118. #ifdef CONFIG_NAND_ENV_DST
  119. spl_nand_load_element(spl_image, CONFIG_ENV_OFFSET, header);
  120. #ifdef CONFIG_ENV_OFFSET_REDUND
  121. spl_nand_load_element(spl_image, CONFIG_ENV_OFFSET_REDUND, header);
  122. #endif
  123. #endif
  124. /* Load u-boot */
  125. err = spl_nand_load_element(spl_image, CONFIG_SYS_NAND_U_BOOT_OFFS,
  126. header);
  127. #ifdef CONFIG_SYS_NAND_U_BOOT_OFFS_REDUND
  128. #if CONFIG_SYS_NAND_U_BOOT_OFFS != CONFIG_SYS_NAND_U_BOOT_OFFS_REDUND
  129. if (err)
  130. err = spl_nand_load_element(spl_image,
  131. CONFIG_SYS_NAND_U_BOOT_OFFS_REDUND,
  132. header);
  133. #endif
  134. #endif
  135. nand_deselect();
  136. return err;
  137. }
  138. #endif
  139. /* Use priorty 1 so that Ubi can override this */
  140. SPL_LOAD_IMAGE_METHOD("NAND", 1, BOOT_DEVICE_NAND, spl_nand_load_image);