fsl_espi_spl.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright 2013 Freescale Semiconductor, Inc.
  4. */
  5. #include <common.h>
  6. #include <cpu_func.h>
  7. #include <hang.h>
  8. #include <spi_flash.h>
  9. #include <malloc.h>
  10. #define ESPI_BOOT_IMAGE_SIZE 0x48
  11. #define ESPI_BOOT_IMAGE_ADDR 0x50
  12. #define CONFIG_CFG_DATA_SECTOR 0
  13. void fsl_spi_spl_load_image(uint32_t offs, unsigned int size, void *vdst)
  14. {
  15. struct spi_flash *flash;
  16. flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
  17. CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
  18. if (flash == NULL) {
  19. puts("\nspi_flash_probe failed");
  20. hang();
  21. }
  22. spi_flash_read(flash, offs, size, vdst);
  23. }
  24. /*
  25. * The main entry for SPI booting. It's necessary that SDRAM is already
  26. * configured and available since this code loads the main U-Boot image
  27. * from SPI into SDRAM and starts it from there.
  28. */
  29. void fsl_spi_boot(void)
  30. {
  31. void (*uboot)(void) __noreturn;
  32. u32 offset, code_len, copy_len = 0;
  33. #ifndef CONFIG_FSL_CORENET
  34. unsigned char *buf = NULL;
  35. #endif
  36. struct spi_flash *flash;
  37. flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
  38. CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
  39. if (flash == NULL) {
  40. puts("\nspi_flash_probe failed");
  41. hang();
  42. }
  43. #ifdef CONFIG_FSL_CORENET
  44. offset = CONFIG_SYS_SPI_FLASH_U_BOOT_OFFS;
  45. code_len = CONFIG_SYS_SPI_FLASH_U_BOOT_SIZE;
  46. #else
  47. /*
  48. * Load U-Boot image from SPI flash into RAM
  49. */
  50. buf = malloc(flash->page_size);
  51. if (buf == NULL) {
  52. puts("\nmalloc failed");
  53. hang();
  54. }
  55. memset(buf, 0, flash->page_size);
  56. spi_flash_read(flash, CONFIG_CFG_DATA_SECTOR,
  57. flash->page_size, (void *)buf);
  58. offset = *(u32 *)(buf + ESPI_BOOT_IMAGE_ADDR);
  59. /* Skip spl code */
  60. offset += CONFIG_SYS_SPI_FLASH_U_BOOT_OFFS;
  61. /* Get the code size from offset 0x48 */
  62. code_len = *(u32 *)(buf + ESPI_BOOT_IMAGE_SIZE);
  63. /* Skip spl code */
  64. code_len = code_len - CONFIG_SPL_MAX_SIZE;
  65. #endif
  66. /* copy code to DDR */
  67. printf("Loading second stage boot loader ");
  68. while (copy_len <= code_len) {
  69. spi_flash_read(flash, offset + copy_len, 0x2000,
  70. (void *)(CONFIG_SYS_SPI_FLASH_U_BOOT_DST
  71. + copy_len));
  72. copy_len = copy_len + 0x2000;
  73. putc('.');
  74. }
  75. /*
  76. * Jump to U-Boot image
  77. */
  78. flush_cache(CONFIG_SYS_SPI_FLASH_U_BOOT_DST, code_len);
  79. uboot = (void *)CONFIG_SYS_SPI_FLASH_U_BOOT_START;
  80. (*uboot)();
  81. }