spl_spi.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. #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. struct spi_flash *flash;
  64. struct image_header *header;
  65. /*
  66. * Load U-Boot image from SPI flash into RAM
  67. */
  68. flash = spi_flash_probe(CONFIG_SF_DEFAULT_BUS,
  69. CONFIG_SF_DEFAULT_CS,
  70. CONFIG_SF_DEFAULT_SPEED,
  71. CONFIG_SF_DEFAULT_MODE);
  72. if (!flash) {
  73. puts("SPI probe failed.\n");
  74. return -ENODEV;
  75. }
  76. /* use CONFIG_SYS_TEXT_BASE as temporary storage area */
  77. header = (struct image_header *)(CONFIG_SYS_TEXT_BASE);
  78. #ifdef CONFIG_SPL_OS_BOOT
  79. if (spl_start_uboot() || spi_load_image_os(spl_image, flash, header))
  80. #endif
  81. {
  82. /* Load u-boot, mkimage header is 64 bytes. */
  83. err = spi_flash_read(flash, CONFIG_SYS_SPI_U_BOOT_OFFS, 0x40,
  84. (void *)header);
  85. if (err)
  86. return err;
  87. if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
  88. image_get_magic(header) == FDT_MAGIC) {
  89. struct spl_load_info load;
  90. debug("Found FIT\n");
  91. load.dev = flash;
  92. load.priv = NULL;
  93. load.filename = NULL;
  94. load.bl_len = 1;
  95. load.read = spl_spi_fit_read;
  96. err = spl_load_simple_fit(spl_image, &load,
  97. CONFIG_SYS_SPI_U_BOOT_OFFS,
  98. header);
  99. } else {
  100. err = spl_parse_image_header(spl_image, header);
  101. if (err)
  102. return err;
  103. err = spi_flash_read(flash, CONFIG_SYS_SPI_U_BOOT_OFFS,
  104. spl_image->size,
  105. (void *)spl_image->load_addr);
  106. }
  107. }
  108. return err;
  109. }
  110. /* Use priorty 1 so that boards can override this */
  111. SPL_LOAD_IMAGE_METHOD("SPI", 1, BOOT_DEVICE_SPI, spl_spi_load_image);