spl_spi.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. header = spl_get_load_buffer(-sizeof(*header), 0x40);
  78. #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
  79. payload_offs = fdtdec_get_config_int(gd->fdt_blob,
  80. "u-boot,spl-payload-offset",
  81. payload_offs);
  82. #endif
  83. #ifdef CONFIG_SPL_OS_BOOT
  84. if (spl_start_uboot() || spi_load_image_os(spl_image, flash, header))
  85. #endif
  86. {
  87. /* Load u-boot, mkimage header is 64 bytes. */
  88. err = spi_flash_read(flash, payload_offs, 0x40,
  89. (void *)header);
  90. if (err) {
  91. debug("%s: Failed to read from SPI flash (err=%d)\n",
  92. __func__, err);
  93. return err;
  94. }
  95. if (IS_ENABLED(CONFIG_SPL_LOAD_FIT_FULL) &&
  96. image_get_magic(header) == FDT_MAGIC) {
  97. err = spi_flash_read(flash, payload_offs,
  98. roundup(fdt_totalsize(header), 4),
  99. (void *)CONFIG_SYS_LOAD_ADDR);
  100. if (err)
  101. return err;
  102. err = spl_parse_image_header(spl_image,
  103. (struct image_header *)CONFIG_SYS_LOAD_ADDR);
  104. } else if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
  105. image_get_magic(header) == FDT_MAGIC) {
  106. struct spl_load_info load;
  107. debug("Found FIT\n");
  108. load.dev = flash;
  109. load.priv = NULL;
  110. load.filename = NULL;
  111. load.bl_len = 1;
  112. load.read = spl_spi_fit_read;
  113. err = spl_load_simple_fit(spl_image, &load,
  114. payload_offs,
  115. header);
  116. } else {
  117. err = spl_parse_image_header(spl_image, header);
  118. if (err)
  119. return err;
  120. err = spi_flash_read(flash, payload_offs,
  121. spl_image->size,
  122. (void *)spl_image->load_addr);
  123. }
  124. }
  125. return err;
  126. }
  127. /* Use priorty 1 so that boards can override this */
  128. SPL_LOAD_IMAGE_METHOD("SPI", 1, BOOT_DEVICE_SPI, spl_spi_load_image);