spl_spi.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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, 0x40,
  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. /*
  55. * The main entry for SPI booting. It's necessary that SDRAM is already
  56. * configured and available since this code loads the main U-Boot image
  57. * from SPI into SDRAM and starts it from there.
  58. */
  59. static int spl_spi_load_image(struct spl_image_info *spl_image,
  60. struct spl_boot_device *bootdev)
  61. {
  62. int err = 0;
  63. unsigned payload_offs = CONFIG_SYS_SPI_U_BOOT_OFFS;
  64. struct spi_flash *flash;
  65. struct image_header *header;
  66. /*
  67. * Load U-Boot image from SPI flash into RAM
  68. */
  69. flash = spi_flash_probe(CONFIG_SF_DEFAULT_BUS,
  70. CONFIG_SF_DEFAULT_CS,
  71. CONFIG_SF_DEFAULT_SPEED,
  72. CONFIG_SF_DEFAULT_MODE);
  73. if (!flash) {
  74. puts("SPI probe failed.\n");
  75. return -ENODEV;
  76. }
  77. /* use CONFIG_SYS_TEXT_BASE as temporary storage area */
  78. header = (struct image_header *)(CONFIG_SYS_TEXT_BASE);
  79. #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
  80. payload_offs = fdtdec_get_config_int(gd->fdt_blob,
  81. "u-boot,spl-payload-offset",
  82. payload_offs);
  83. #endif
  84. #ifdef CONFIG_SPL_OS_BOOT
  85. if (spl_start_uboot() || spi_load_image_os(spl_image, flash, header))
  86. #endif
  87. {
  88. /* Load u-boot, mkimage header is 64 bytes. */
  89. err = spi_flash_read(flash, payload_offs, 0x40,
  90. (void *)header);
  91. if (err) {
  92. debug("%s: Failed to read from SPI flash (err=%d)\n",
  93. __func__, err);
  94. return err;
  95. }
  96. if (IS_ENABLED(CONFIG_SPL_LOAD_FIT_FULL) &&
  97. image_get_magic(header) == FDT_MAGIC) {
  98. err = spi_flash_read(flash, payload_offs,
  99. roundup(fdt_totalsize(header), 4),
  100. (void *)CONFIG_SYS_LOAD_ADDR);
  101. if (err)
  102. return err;
  103. err = spl_parse_image_header(spl_image,
  104. (struct image_header *)CONFIG_SYS_LOAD_ADDR);
  105. } else if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
  106. image_get_magic(header) == FDT_MAGIC) {
  107. struct spl_load_info load;
  108. debug("Found FIT\n");
  109. load.dev = flash;
  110. load.priv = NULL;
  111. load.filename = NULL;
  112. load.bl_len = 1;
  113. load.read = spl_spi_fit_read;
  114. err = spl_load_simple_fit(spl_image, &load,
  115. payload_offs,
  116. header);
  117. } else {
  118. err = spl_parse_image_header(spl_image, header);
  119. if (err)
  120. return err;
  121. err = spi_flash_read(flash, payload_offs,
  122. spl_image->size,
  123. (void *)spl_image->load_addr);
  124. }
  125. }
  126. return err;
  127. }
  128. /* Use priorty 1 so that boards can override this */
  129. SPL_LOAD_IMAGE_METHOD("SPI", 1, BOOT_DEVICE_SPI, spl_spi_load_image);