spl_spi.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. DECLARE_GLOBAL_DATA_PTR;
  18. #ifdef CONFIG_SPL_OS_BOOT
  19. /*
  20. * Load the kernel, check for a valid header we can parse, and if found load
  21. * the kernel and then device tree.
  22. */
  23. static int spi_load_image_os(struct spl_image_info *spl_image,
  24. struct spi_flash *flash,
  25. struct image_header *header)
  26. {
  27. int err;
  28. /* Read for a header, parse or error out. */
  29. spi_flash_read(flash, CONFIG_SYS_SPI_KERNEL_OFFS, sizeof(*header),
  30. (void *)header);
  31. if (image_get_magic(header) != IH_MAGIC)
  32. return -1;
  33. err = spl_parse_image_header(spl_image, header);
  34. if (err)
  35. return err;
  36. spi_flash_read(flash, CONFIG_SYS_SPI_KERNEL_OFFS,
  37. spl_image->size, (void *)spl_image->load_addr);
  38. /* Read device tree. */
  39. spi_flash_read(flash, CONFIG_SYS_SPI_ARGS_OFFS,
  40. CONFIG_SYS_SPI_ARGS_SIZE,
  41. (void *)CONFIG_SYS_SPL_ARGS_ADDR);
  42. return 0;
  43. }
  44. #endif
  45. static ulong spl_spi_fit_read(struct spl_load_info *load, ulong sector,
  46. ulong count, void *buf)
  47. {
  48. struct spi_flash *flash = load->dev;
  49. ulong ret;
  50. ret = spi_flash_read(flash, sector, count, buf);
  51. if (!ret)
  52. return count;
  53. else
  54. return 0;
  55. }
  56. unsigned int __weak spl_spi_get_uboot_offs(struct spi_flash *flash)
  57. {
  58. return CONFIG_SYS_SPI_U_BOOT_OFFS;
  59. }
  60. /*
  61. * The main entry for SPI booting. It's necessary that SDRAM is already
  62. * configured and available since this code loads the main U-Boot image
  63. * from SPI into SDRAM and starts it from there.
  64. */
  65. static int spl_spi_load_image(struct spl_image_info *spl_image,
  66. struct spl_boot_device *bootdev)
  67. {
  68. int err = 0;
  69. unsigned int payload_offs;
  70. struct spi_flash *flash;
  71. struct image_header *header;
  72. /*
  73. * Load U-Boot image from SPI flash into RAM
  74. * In DM mode: defaults speed and mode will be
  75. * taken from DT when available
  76. */
  77. flash = spi_flash_probe(CONFIG_SF_DEFAULT_BUS,
  78. CONFIG_SF_DEFAULT_CS,
  79. CONFIG_SF_DEFAULT_SPEED,
  80. CONFIG_SF_DEFAULT_MODE);
  81. if (!flash) {
  82. puts("SPI probe failed.\n");
  83. return -ENODEV;
  84. }
  85. payload_offs = spl_spi_get_uboot_offs(flash);
  86. header = spl_get_load_buffer(-sizeof(*header), sizeof(*header));
  87. #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
  88. payload_offs = fdtdec_get_config_int(gd->fdt_blob,
  89. "u-boot,spl-payload-offset",
  90. payload_offs);
  91. #endif
  92. #ifdef CONFIG_SPL_OS_BOOT
  93. if (spl_start_uboot() || spi_load_image_os(spl_image, flash, header))
  94. #endif
  95. {
  96. /* Load u-boot, mkimage header is 64 bytes. */
  97. err = spi_flash_read(flash, payload_offs, sizeof(*header),
  98. (void *)header);
  99. if (err) {
  100. debug("%s: Failed to read from SPI flash (err=%d)\n",
  101. __func__, err);
  102. return err;
  103. }
  104. if (IS_ENABLED(CONFIG_SPL_LOAD_FIT_FULL) &&
  105. image_get_magic(header) == FDT_MAGIC) {
  106. err = spi_flash_read(flash, payload_offs,
  107. roundup(fdt_totalsize(header), 4),
  108. (void *)CONFIG_SYS_LOAD_ADDR);
  109. if (err)
  110. return err;
  111. err = spl_parse_image_header(spl_image,
  112. (struct image_header *)CONFIG_SYS_LOAD_ADDR);
  113. } else if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
  114. image_get_magic(header) == FDT_MAGIC) {
  115. struct spl_load_info load;
  116. debug("Found FIT\n");
  117. load.dev = flash;
  118. load.priv = NULL;
  119. load.filename = NULL;
  120. load.bl_len = 1;
  121. load.read = spl_spi_fit_read;
  122. err = spl_load_simple_fit(spl_image, &load,
  123. payload_offs,
  124. header);
  125. } else if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER)) {
  126. struct spl_load_info load;
  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_imx_container(spl_image, &load,
  133. payload_offs);
  134. } else {
  135. err = spl_parse_image_header(spl_image, header);
  136. if (err)
  137. return err;
  138. err = spi_flash_read(flash, payload_offs,
  139. spl_image->size,
  140. (void *)spl_image->load_addr);
  141. }
  142. }
  143. return err;
  144. }
  145. /* Use priorty 1 so that boards can override this */
  146. SPL_LOAD_IMAGE_METHOD("SPI", 1, BOOT_DEVICE_SPI, spl_spi_load_image);