spl_nand.c 3.8 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. static 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. header = spl_get_load_buffer(0, sizeof(*header));
  75. #ifdef CONFIG_SPL_OS_BOOT
  76. if (!spl_start_uboot()) {
  77. /*
  78. * load parameter image
  79. * load to temp position since nand_spl_load_image reads
  80. * a whole block which is typically larger than
  81. * CONFIG_CMD_SPL_WRITE_SIZE therefore may overwrite
  82. * following sections like BSS
  83. */
  84. nand_spl_load_image(CONFIG_CMD_SPL_NAND_OFS,
  85. CONFIG_CMD_SPL_WRITE_SIZE,
  86. (void *)CONFIG_SYS_TEXT_BASE);
  87. /* copy to destintion */
  88. for (dst = (int *)CONFIG_SYS_SPL_ARGS_ADDR,
  89. src = (int *)CONFIG_SYS_TEXT_BASE;
  90. src < (int *)(CONFIG_SYS_TEXT_BASE +
  91. CONFIG_CMD_SPL_WRITE_SIZE);
  92. src++, dst++) {
  93. writel(readl(src), dst);
  94. }
  95. /* load linux */
  96. nand_spl_load_image(CONFIG_SYS_NAND_SPL_KERNEL_OFFS,
  97. sizeof(*header), (void *)header);
  98. err = spl_parse_image_header(spl_image, header);
  99. if (err)
  100. return err;
  101. if (header->ih_os == IH_OS_LINUX) {
  102. /* happy - was a linux */
  103. err = nand_spl_load_image(
  104. CONFIG_SYS_NAND_SPL_KERNEL_OFFS,
  105. spl_image->size,
  106. (void *)spl_image->load_addr);
  107. nand_deselect();
  108. return err;
  109. } else {
  110. puts("The Expected Linux image was not "
  111. "found. Please check your NAND "
  112. "configuration.\n");
  113. puts("Trying to start u-boot now...\n");
  114. }
  115. }
  116. #endif
  117. #ifdef CONFIG_NAND_ENV_DST
  118. spl_nand_load_element(spl_image, CONFIG_ENV_OFFSET, header);
  119. #ifdef CONFIG_ENV_OFFSET_REDUND
  120. spl_nand_load_element(spl_image, CONFIG_ENV_OFFSET_REDUND, header);
  121. #endif
  122. #endif
  123. /* Load u-boot */
  124. err = spl_nand_load_element(spl_image, CONFIG_SYS_NAND_U_BOOT_OFFS,
  125. header);
  126. #ifdef CONFIG_SYS_NAND_U_BOOT_OFFS_REDUND
  127. #if CONFIG_SYS_NAND_U_BOOT_OFFS != CONFIG_SYS_NAND_U_BOOT_OFFS_REDUND
  128. if (err)
  129. err = spl_nand_load_element(spl_image,
  130. CONFIG_SYS_NAND_U_BOOT_OFFS_REDUND,
  131. header);
  132. #endif
  133. #endif
  134. nand_deselect();
  135. return err;
  136. }
  137. #endif
  138. /* Use priorty 1 so that Ubi can override this */
  139. SPL_LOAD_IMAGE_METHOD("NAND", 1, BOOT_DEVICE_NAND, spl_nand_load_image);