spl_spi.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * Copyright (C) 2011 OMICRON electronics GmbH
  3. *
  4. * based on drivers/mtd/nand/nand_spl_load.c
  5. *
  6. * Copyright (C) 2011
  7. * Heiko Schocher, DENX Software Engineering, hs@denx.de.
  8. *
  9. * SPDX-License-Identifier: GPL-2.0+
  10. */
  11. #include <common.h>
  12. #include <spi.h>
  13. #include <spi_flash.h>
  14. #include <errno.h>
  15. #include <spl.h>
  16. DECLARE_GLOBAL_DATA_PTR;
  17. #ifdef CONFIG_SPL_OS_BOOT
  18. /*
  19. * Load the kernel, check for a valid header we can parse, and if found load
  20. * the kernel and then device tree.
  21. */
  22. static int spi_load_image_os(struct spl_image_info *spl_image,
  23. struct spi_flash *flash,
  24. struct image_header *header)
  25. {
  26. int err;
  27. /* Read for a header, parse or error out. */
  28. spi_flash_read(flash, CONFIG_SYS_SPI_KERNEL_OFFS, 0x40,
  29. (void *)header);
  30. if (image_get_magic(header) != IH_MAGIC)
  31. return -1;
  32. err = spl_parse_image_header(spl_image, header);
  33. if (err)
  34. return err;
  35. spi_flash_read(flash, CONFIG_SYS_SPI_KERNEL_OFFS,
  36. spl_image->size, (void *)spl_image->load_addr);
  37. /* Read device tree. */
  38. spi_flash_read(flash, CONFIG_SYS_SPI_ARGS_OFFS,
  39. CONFIG_SYS_SPI_ARGS_SIZE,
  40. (void *)CONFIG_SYS_SPL_ARGS_ADDR);
  41. return 0;
  42. }
  43. #endif
  44. static ulong spl_spi_fit_read(struct spl_load_info *load, ulong sector,
  45. ulong count, void *buf)
  46. {
  47. struct spi_flash *flash = load->dev;
  48. ulong ret;
  49. ret = spi_flash_read(flash, sector, count, buf);
  50. if (!ret)
  51. return count;
  52. else
  53. return 0;
  54. }
  55. /*
  56. * The main entry for SPI booting. It's necessary that SDRAM is already
  57. * configured and available since this code loads the main U-Boot image
  58. * from SPI into SDRAM and starts it from there.
  59. */
  60. static int spl_spi_load_image(struct spl_image_info *spl_image,
  61. struct spl_boot_device *bootdev)
  62. {
  63. int err = 0;
  64. unsigned payload_offs = CONFIG_SYS_SPI_U_BOOT_OFFS;
  65. struct spi_flash *flash;
  66. struct image_header *header;
  67. /*
  68. * Load U-Boot image from SPI flash into RAM
  69. */
  70. flash = spi_flash_probe(CONFIG_SF_DEFAULT_BUS,
  71. CONFIG_SF_DEFAULT_CS,
  72. CONFIG_SF_DEFAULT_SPEED,
  73. CONFIG_SF_DEFAULT_MODE);
  74. if (!flash) {
  75. puts("SPI probe failed.\n");
  76. return -ENODEV;
  77. }
  78. /* use CONFIG_SYS_TEXT_BASE as temporary storage area */
  79. header = (struct image_header *)(CONFIG_SYS_TEXT_BASE);
  80. #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
  81. payload_offs = fdtdec_get_config_int(gd->fdt_blob,
  82. "u-boot,spl-payload-offset",
  83. payload_offs);
  84. #endif
  85. #ifdef CONFIG_SPL_OS_BOOT
  86. if (spl_start_uboot() || spi_load_image_os(spl_image, flash, header))
  87. #endif
  88. {
  89. /* Load u-boot, mkimage header is 64 bytes. */
  90. err = spi_flash_read(flash, payload_offs, 0x40,
  91. (void *)header);
  92. if (err) {
  93. debug("%s: Failed to read from SPI flash (err=%d)\n",
  94. __func__, err);
  95. return err;
  96. }
  97. if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
  98. image_get_magic(header) == FDT_MAGIC) {
  99. struct spl_load_info load;
  100. debug("Found FIT\n");
  101. load.dev = flash;
  102. load.priv = NULL;
  103. load.filename = NULL;
  104. load.bl_len = 1;
  105. load.read = spl_spi_fit_read;
  106. err = spl_load_simple_fit(spl_image, &load,
  107. payload_offs,
  108. header);
  109. } else {
  110. err = spl_parse_image_header(spl_image, header);
  111. if (err)
  112. return err;
  113. err = spi_flash_read(flash, payload_offs,
  114. spl_image->size,
  115. (void *)spl_image->load_addr);
  116. }
  117. }
  118. return err;
  119. }
  120. /* Use priorty 1 so that boards can override this */
  121. SPL_LOAD_IMAGE_METHOD("SPI", 1, BOOT_DEVICE_SPI, spl_spi_load_image);