spi_spl_load.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. /* Read for a header, parse or error out. */
  25. spi_flash_read(flash, CONFIG_SYS_SPI_KERNEL_OFFS, 0x40,
  26. (void *)header);
  27. if (image_get_magic(header) != IH_MAGIC)
  28. return -1;
  29. spl_parse_image_header(header);
  30. spi_flash_read(flash, CONFIG_SYS_SPI_KERNEL_OFFS,
  31. spl_image.size, (void *)spl_image.load_addr);
  32. /* Read device tree. */
  33. spi_flash_read(flash, CONFIG_SYS_SPI_ARGS_OFFS,
  34. CONFIG_SYS_SPI_ARGS_SIZE,
  35. (void *)CONFIG_SYS_SPL_ARGS_ADDR);
  36. return 0;
  37. }
  38. #endif
  39. /*
  40. * The main entry for SPI booting. It's necessary that SDRAM is already
  41. * configured and available since this code loads the main U-Boot image
  42. * from SPI into SDRAM and starts it from there.
  43. */
  44. int spl_spi_load_image(void)
  45. {
  46. int err = 0;
  47. struct spi_flash *flash;
  48. struct image_header *header;
  49. /*
  50. * Load U-Boot image from SPI flash into RAM
  51. */
  52. flash = spi_flash_probe(CONFIG_SF_DEFAULT_BUS,
  53. CONFIG_SF_DEFAULT_CS,
  54. CONFIG_SF_DEFAULT_SPEED,
  55. CONFIG_SF_DEFAULT_MODE);
  56. if (!flash) {
  57. puts("SPI probe failed.\n");
  58. return -ENODEV;
  59. }
  60. /* use CONFIG_SYS_TEXT_BASE as temporary storage area */
  61. header = (struct image_header *)(CONFIG_SYS_TEXT_BASE);
  62. #ifdef CONFIG_SPL_OS_BOOT
  63. if (spl_start_uboot() || spi_load_image_os(flash, header))
  64. #endif
  65. {
  66. /* Load u-boot, mkimage header is 64 bytes. */
  67. err = spi_flash_read(flash, CONFIG_SYS_SPI_U_BOOT_OFFS, 0x40,
  68. (void *)header);
  69. if (err)
  70. return err;
  71. spl_parse_image_header(header);
  72. err = spi_flash_read(flash, CONFIG_SYS_SPI_U_BOOT_OFFS,
  73. spl_image.size, (void *)spl_image.load_addr);
  74. }
  75. return err;
  76. }