spl_spi.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2011 OMICRON electronics GmbH
  4. *
  5. * based on drivers/mtd/nand/raw/nand_spl_load.c
  6. *
  7. * Copyright (C) 2011
  8. * Heiko Schocher, DENX Software Engineering, hs@denx.de.
  9. */
  10. #include <common.h>
  11. #include <image.h>
  12. #include <log.h>
  13. #include <spi.h>
  14. #include <spi_flash.h>
  15. #include <errno.h>
  16. #include <spl.h>
  17. #include <asm/global_data.h>
  18. #include <dm/ofnode.h>
  19. #if CONFIG_IS_ENABLED(OS_BOOT)
  20. /*
  21. * Load the kernel, check for a valid header we can parse, and if found load
  22. * the kernel and then device tree.
  23. */
  24. static int spi_load_image_os(struct spl_image_info *spl_image,
  25. struct spl_boot_device *bootdev,
  26. struct spi_flash *flash,
  27. struct legacy_img_hdr *header)
  28. {
  29. int err;
  30. /* Read for a header, parse or error out. */
  31. spi_flash_read(flash, CFG_SYS_SPI_KERNEL_OFFS, sizeof(*header),
  32. (void *)header);
  33. if (image_get_magic(header) != IH_MAGIC)
  34. return -1;
  35. err = spl_parse_image_header(spl_image, bootdev, header);
  36. if (err)
  37. return err;
  38. spi_flash_read(flash, CFG_SYS_SPI_KERNEL_OFFS,
  39. spl_image->size, (void *)spl_image->load_addr);
  40. /* Read device tree. */
  41. spi_flash_read(flash, CFG_SYS_SPI_ARGS_OFFS,
  42. CFG_SYS_SPI_ARGS_SIZE,
  43. (void *)CONFIG_SYS_SPL_ARGS_ADDR);
  44. return 0;
  45. }
  46. #endif
  47. static ulong spl_spi_fit_read(struct spl_load_info *load, ulong sector,
  48. ulong count, void *buf)
  49. {
  50. struct spi_flash *flash = load->dev;
  51. ulong ret;
  52. ret = spi_flash_read(flash, sector, count, buf);
  53. if (!ret)
  54. return count;
  55. else
  56. return 0;
  57. }
  58. unsigned int __weak spl_spi_get_uboot_offs(struct spi_flash *flash)
  59. {
  60. return CONFIG_SYS_SPI_U_BOOT_OFFS;
  61. }
  62. u32 __weak spl_spi_boot_bus(void)
  63. {
  64. return CONFIG_SF_DEFAULT_BUS;
  65. }
  66. u32 __weak spl_spi_boot_cs(void)
  67. {
  68. return CONFIG_SF_DEFAULT_CS;
  69. }
  70. /*
  71. * The main entry for SPI booting. It's necessary that SDRAM is already
  72. * configured and available since this code loads the main U-Boot image
  73. * from SPI into SDRAM and starts it from there.
  74. */
  75. static int spl_spi_load_image(struct spl_image_info *spl_image,
  76. struct spl_boot_device *bootdev)
  77. {
  78. int err = 0;
  79. unsigned int payload_offs;
  80. struct spi_flash *flash;
  81. struct legacy_img_hdr *header;
  82. unsigned int sf_bus = spl_spi_boot_bus();
  83. unsigned int sf_cs = spl_spi_boot_cs();
  84. /*
  85. * Load U-Boot image from SPI flash into RAM
  86. * In DM mode: defaults speed and mode will be
  87. * taken from DT when available
  88. */
  89. flash = spi_flash_probe(sf_bus, sf_cs,
  90. CONFIG_SF_DEFAULT_SPEED,
  91. CONFIG_SF_DEFAULT_MODE);
  92. if (!flash) {
  93. puts("SPI probe failed.\n");
  94. return -ENODEV;
  95. }
  96. payload_offs = spl_spi_get_uboot_offs(flash);
  97. header = spl_get_load_buffer(-sizeof(*header), sizeof(*header));
  98. if (CONFIG_IS_ENABLED(OF_REAL)) {
  99. payload_offs = ofnode_conf_read_int("u-boot,spl-payload-offset",
  100. payload_offs);
  101. }
  102. #if CONFIG_IS_ENABLED(OS_BOOT)
  103. if (spl_start_uboot() || spi_load_image_os(spl_image, bootdev, flash, header))
  104. #endif
  105. {
  106. /* Load u-boot, mkimage header is 64 bytes. */
  107. err = spi_flash_read(flash, payload_offs, sizeof(*header),
  108. (void *)header);
  109. if (err) {
  110. debug("%s: Failed to read from SPI flash (err=%d)\n",
  111. __func__, err);
  112. return err;
  113. }
  114. if (IS_ENABLED(CONFIG_SPL_LOAD_FIT_FULL) &&
  115. image_get_magic(header) == FDT_MAGIC) {
  116. err = spi_flash_read(flash, payload_offs,
  117. roundup(fdt_totalsize(header), 4),
  118. (void *)CONFIG_SYS_LOAD_ADDR);
  119. if (err)
  120. return err;
  121. err = spl_parse_image_header(spl_image, bootdev,
  122. (struct legacy_img_hdr *)CONFIG_SYS_LOAD_ADDR);
  123. } else if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
  124. image_get_magic(header) == FDT_MAGIC) {
  125. struct spl_load_info load;
  126. debug("Found FIT\n");
  127. load.dev = flash;
  128. load.priv = NULL;
  129. load.filename = NULL;
  130. load.bl_len = 1;
  131. load.read = spl_spi_fit_read;
  132. err = spl_load_simple_fit(spl_image, &load,
  133. payload_offs,
  134. header);
  135. } else if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER)) {
  136. struct spl_load_info load;
  137. load.dev = flash;
  138. load.priv = NULL;
  139. load.filename = NULL;
  140. load.bl_len = 1;
  141. load.read = spl_spi_fit_read;
  142. err = spl_load_imx_container(spl_image, &load,
  143. payload_offs);
  144. } else {
  145. err = spl_parse_image_header(spl_image, bootdev, header);
  146. if (err)
  147. return err;
  148. err = spi_flash_read(flash, payload_offs + spl_image->offset,
  149. spl_image->size,
  150. (void *)spl_image->load_addr);
  151. }
  152. if (IS_ENABLED(CONFIG_SPI_FLASH_SOFT_RESET)) {
  153. err = spi_nor_remove(flash);
  154. if (err)
  155. return err;
  156. }
  157. }
  158. return err;
  159. }
  160. /* Use priorty 1 so that boards can override this */
  161. SPL_LOAD_IMAGE_METHOD("SPI", 1, BOOT_DEVICE_SPI, spl_spi_load_image);