spi_spl_load.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 spi_flash *flash,
  22. struct image_header *header)
  23. {
  24. int err;
  25. /* Read for a header, parse or error out. */
  26. spi_flash_read(flash, CONFIG_SYS_SPI_KERNEL_OFFS, 0x40,
  27. (void *)header);
  28. if (image_get_magic(header) != IH_MAGIC)
  29. return -1;
  30. err = spl_parse_image_header(&spl_image, header);
  31. if (err)
  32. return err;
  33. spi_flash_read(flash, CONFIG_SYS_SPI_KERNEL_OFFS,
  34. spl_image.size, (void *)spl_image.load_addr);
  35. /* Read device tree. */
  36. spi_flash_read(flash, CONFIG_SYS_SPI_ARGS_OFFS,
  37. CONFIG_SYS_SPI_ARGS_SIZE,
  38. (void *)CONFIG_SYS_SPL_ARGS_ADDR);
  39. return 0;
  40. }
  41. #endif
  42. static ulong spl_spi_fit_read(struct spl_load_info *load, ulong sector,
  43. ulong count, void *buf)
  44. {
  45. struct spi_flash *flash = load->dev;
  46. ulong ret;
  47. ret = spi_flash_read(flash, sector, count, buf);
  48. if (!ret)
  49. return count;
  50. else
  51. return 0;
  52. }
  53. /*
  54. * The main entry for SPI booting. It's necessary that SDRAM is already
  55. * configured and available since this code loads the main U-Boot image
  56. * from SPI into SDRAM and starts it from there.
  57. */
  58. int spl_spi_load_image(void)
  59. {
  60. int err = 0;
  61. struct spi_flash *flash;
  62. struct image_header *header;
  63. /*
  64. * Load U-Boot image from SPI flash into RAM
  65. */
  66. flash = spi_flash_probe(CONFIG_SF_DEFAULT_BUS,
  67. CONFIG_SF_DEFAULT_CS,
  68. CONFIG_SF_DEFAULT_SPEED,
  69. CONFIG_SF_DEFAULT_MODE);
  70. if (!flash) {
  71. puts("SPI probe failed.\n");
  72. return -ENODEV;
  73. }
  74. /* use CONFIG_SYS_TEXT_BASE as temporary storage area */
  75. header = (struct image_header *)(CONFIG_SYS_TEXT_BASE);
  76. #ifdef CONFIG_SPL_OS_BOOT
  77. if (spl_start_uboot() || spi_load_image_os(flash, header))
  78. #endif
  79. {
  80. /* Load u-boot, mkimage header is 64 bytes. */
  81. err = spi_flash_read(flash, CONFIG_SYS_SPI_U_BOOT_OFFS, 0x40,
  82. (void *)header);
  83. if (err)
  84. return err;
  85. if (IS_ENABLED(CONFIG_SPL_LOAD_FIT)) {
  86. struct spl_load_info load;
  87. debug("Found FIT\n");
  88. load.dev = flash;
  89. load.priv = NULL;
  90. load.filename = NULL;
  91. load.bl_len = 1;
  92. load.read = spl_spi_fit_read;
  93. err = spl_load_simple_fit(&load,
  94. CONFIG_SYS_SPI_U_BOOT_OFFS,
  95. header);
  96. } else {
  97. err = spl_parse_image_header(&spl_image, header);
  98. if (err)
  99. return err;
  100. err = spi_flash_read(flash, CONFIG_SYS_SPI_U_BOOT_OFFS,
  101. spl_image.size,
  102. (void *)spl_image.load_addr);
  103. }
  104. }
  105. return err;
  106. }