spl_spi.c 4.2 KB

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