spl_nand.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 <fdt_support.h>
  9. #include <image.h>
  10. #include <log.h>
  11. #include <spl.h>
  12. #include <asm/io.h>
  13. #include <nand.h>
  14. #include <linux/libfdt_env.h>
  15. #include <fdt.h>
  16. uint32_t __weak spl_nand_get_uboot_raw_page(void)
  17. {
  18. return CONFIG_SYS_NAND_U_BOOT_OFFS;
  19. }
  20. #if defined(CONFIG_SPL_NAND_RAW_ONLY)
  21. static int spl_nand_load_image(struct spl_image_info *spl_image,
  22. struct spl_boot_device *bootdev)
  23. {
  24. nand_init();
  25. printf("Loading U-Boot from 0x%08x (size 0x%08x) to 0x%08x\n",
  26. CONFIG_SYS_NAND_U_BOOT_OFFS, CONFIG_SYS_NAND_U_BOOT_SIZE,
  27. CONFIG_SYS_NAND_U_BOOT_DST);
  28. nand_spl_load_image(spl_nand_get_uboot_raw_page(),
  29. CONFIG_SYS_NAND_U_BOOT_SIZE,
  30. (void *)CONFIG_SYS_NAND_U_BOOT_DST);
  31. spl_set_header_raw_uboot(spl_image);
  32. nand_deselect();
  33. return 0;
  34. }
  35. #else
  36. static ulong spl_nand_fit_read(struct spl_load_info *load, ulong offs,
  37. ulong size, void *dst)
  38. {
  39. ulong sector;
  40. int ret;
  41. sector = *(int *)load->priv;
  42. offs = sector + nand_spl_adjust_offset(sector, offs - sector);
  43. ret = nand_spl_load_image(offs, size, dst);
  44. if (!ret)
  45. return size;
  46. else
  47. return 0;
  48. }
  49. static int spl_nand_load_element(struct spl_image_info *spl_image,
  50. int offset, struct image_header *header)
  51. {
  52. int err;
  53. err = nand_spl_load_image(offset, sizeof(*header), (void *)header);
  54. if (err)
  55. return err;
  56. if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
  57. image_get_magic(header) == FDT_MAGIC) {
  58. struct spl_load_info load;
  59. debug("Found FIT\n");
  60. load.dev = NULL;
  61. load.priv = &offset;
  62. load.filename = NULL;
  63. load.bl_len = 1;
  64. load.read = spl_nand_fit_read;
  65. return spl_load_simple_fit(spl_image, &load, offset, header);
  66. } else if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER)) {
  67. struct spl_load_info load;
  68. load.dev = NULL;
  69. load.priv = NULL;
  70. load.filename = NULL;
  71. load.bl_len = 1;
  72. load.read = spl_nand_fit_read;
  73. return spl_load_imx_container(spl_image, &load, offset);
  74. } else {
  75. err = spl_parse_image_header(spl_image, header);
  76. if (err)
  77. return err;
  78. return nand_spl_load_image(offset, spl_image->size,
  79. (void *)(ulong)spl_image->load_addr);
  80. }
  81. }
  82. static int spl_nand_load_image(struct spl_image_info *spl_image,
  83. struct spl_boot_device *bootdev)
  84. {
  85. int err;
  86. struct image_header *header;
  87. int *src __attribute__((unused));
  88. int *dst __attribute__((unused));
  89. #ifdef CONFIG_SPL_NAND_SOFTECC
  90. debug("spl: nand - using sw ecc\n");
  91. #else
  92. debug("spl: nand - using hw ecc\n");
  93. #endif
  94. nand_init();
  95. header = spl_get_load_buffer(0, sizeof(*header));
  96. #ifdef CONFIG_SPL_OS_BOOT
  97. if (!spl_start_uboot()) {
  98. /*
  99. * load parameter image
  100. * load to temp position since nand_spl_load_image reads
  101. * a whole block which is typically larger than
  102. * CONFIG_CMD_SPL_WRITE_SIZE therefore may overwrite
  103. * following sections like BSS
  104. */
  105. nand_spl_load_image(CONFIG_CMD_SPL_NAND_OFS,
  106. CONFIG_CMD_SPL_WRITE_SIZE,
  107. (void *)CONFIG_SYS_TEXT_BASE);
  108. /* copy to destintion */
  109. for (dst = (int *)CONFIG_SYS_SPL_ARGS_ADDR,
  110. src = (int *)CONFIG_SYS_TEXT_BASE;
  111. src < (int *)(CONFIG_SYS_TEXT_BASE +
  112. CONFIG_CMD_SPL_WRITE_SIZE);
  113. src++, dst++) {
  114. writel(readl(src), dst);
  115. }
  116. /* load linux */
  117. nand_spl_load_image(CONFIG_SYS_NAND_SPL_KERNEL_OFFS,
  118. sizeof(*header), (void *)header);
  119. err = spl_parse_image_header(spl_image, header);
  120. if (err)
  121. return err;
  122. if (header->ih_os == IH_OS_LINUX) {
  123. /* happy - was a linux */
  124. err = nand_spl_load_image(
  125. CONFIG_SYS_NAND_SPL_KERNEL_OFFS,
  126. spl_image->size,
  127. (void *)spl_image->load_addr);
  128. nand_deselect();
  129. return err;
  130. } else {
  131. puts("The Expected Linux image was not "
  132. "found. Please check your NAND "
  133. "configuration.\n");
  134. puts("Trying to start u-boot now...\n");
  135. }
  136. }
  137. #endif
  138. #ifdef CONFIG_NAND_ENV_DST
  139. spl_nand_load_element(spl_image, CONFIG_ENV_OFFSET, header);
  140. #ifdef CONFIG_ENV_OFFSET_REDUND
  141. spl_nand_load_element(spl_image, CONFIG_ENV_OFFSET_REDUND, header);
  142. #endif
  143. #endif
  144. /* Load u-boot */
  145. err = spl_nand_load_element(spl_image, spl_nand_get_uboot_raw_page(),
  146. header);
  147. #ifdef CONFIG_SYS_NAND_U_BOOT_OFFS_REDUND
  148. #if CONFIG_SYS_NAND_U_BOOT_OFFS != CONFIG_SYS_NAND_U_BOOT_OFFS_REDUND
  149. if (err)
  150. err = spl_nand_load_element(spl_image,
  151. CONFIG_SYS_NAND_U_BOOT_OFFS_REDUND,
  152. header);
  153. #endif
  154. #endif
  155. nand_deselect();
  156. return err;
  157. }
  158. #endif
  159. /* Use priorty 1 so that Ubi can override this */
  160. SPL_LOAD_IMAGE_METHOD("NAND", 1, BOOT_DEVICE_NAND, spl_nand_load_image);