fsl_esdhc_spl.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * Copyright 2013 Freescale Semiconductor, Inc.
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. */
  6. #include <common.h>
  7. #include <mmc.h>
  8. #include <malloc.h>
  9. /*
  10. * The environment variables are written to just after the u-boot image
  11. * on SDCard, so we must read the MBR to get the start address and code
  12. * length of the u-boot image, then calculate the address of the env.
  13. */
  14. #define ESDHC_BOOT_IMAGE_SIZE 0x48
  15. #define ESDHC_BOOT_IMAGE_ADDR 0x50
  16. #define MBRDBR_BOOT_SIG_55 0x1fe
  17. #define MBRDBR_BOOT_SIG_AA 0x1ff
  18. #define CONFIG_CFG_DATA_SECTOR 0
  19. void mmc_spl_load_image(uint32_t offs, unsigned int size, void *vdst)
  20. {
  21. uint blk_start, blk_cnt, err;
  22. struct mmc *mmc = find_mmc_device(0);
  23. if (!mmc) {
  24. puts("spl: mmc device not found!!\n");
  25. hang();
  26. }
  27. if (mmc_init(mmc)) {
  28. puts("MMC init failed\n");
  29. return;
  30. }
  31. blk_start = ALIGN(offs, mmc->read_bl_len) / mmc->read_bl_len;
  32. blk_cnt = ALIGN(size, mmc->read_bl_len) / mmc->read_bl_len;
  33. err = mmc->block_dev.block_read(0, blk_start, blk_cnt, vdst);
  34. if (err != blk_cnt) {
  35. puts("spl: mmc read failed!!\n");
  36. hang();
  37. }
  38. }
  39. /*
  40. * The main entry for mmc booting. It's necessary that SDRAM is already
  41. * configured and available since this code loads the main U-Boot image
  42. * from mmc into SDRAM and starts it from there.
  43. */
  44. void __noreturn mmc_boot(void)
  45. {
  46. __attribute__((noreturn)) void (*uboot)(void);
  47. uint blk_start, blk_cnt, err;
  48. #ifndef CONFIG_FSL_CORENET
  49. uchar *tmp_buf;
  50. u32 blklen;
  51. uchar val;
  52. uint i, byte_num;
  53. #endif
  54. u32 offset, code_len;
  55. struct mmc *mmc;
  56. mmc = find_mmc_device(0);
  57. if (!mmc) {
  58. puts("spl: mmc device not found!!\n");
  59. hang();
  60. }
  61. #ifdef CONFIG_FSL_CORENET
  62. offset = CONFIG_SYS_MMC_U_BOOT_OFFS;
  63. code_len = CONFIG_SYS_MMC_U_BOOT_SIZE;
  64. #else
  65. blklen = mmc->read_bl_len;
  66. tmp_buf = malloc(blklen);
  67. if (!tmp_buf) {
  68. puts("spl: malloc memory failed!!\n");
  69. hang();
  70. }
  71. memset(tmp_buf, 0, blklen);
  72. /*
  73. * Read source addr from sd card
  74. */
  75. err = mmc->block_dev.block_read(0, CONFIG_CFG_DATA_SECTOR, 1, tmp_buf);
  76. if (err != 1) {
  77. puts("spl: mmc read failed!!\n");
  78. free(tmp_buf);
  79. hang();
  80. }
  81. val = *(tmp_buf + MBRDBR_BOOT_SIG_55);
  82. if (0x55 != val) {
  83. puts("spl: mmc signature is not valid!!\n");
  84. free(tmp_buf);
  85. hang();
  86. }
  87. val = *(tmp_buf + MBRDBR_BOOT_SIG_AA);
  88. if (0xAA != val) {
  89. puts("spl: mmc signature is not valid!!\n");
  90. free(tmp_buf);
  91. hang();
  92. }
  93. byte_num = 4;
  94. offset = 0;
  95. for (i = 0; i < byte_num; i++) {
  96. val = *(tmp_buf + ESDHC_BOOT_IMAGE_ADDR + i);
  97. offset = (offset << 8) + val;
  98. }
  99. offset += CONFIG_SYS_MMC_U_BOOT_OFFS;
  100. /* Get the code size from offset 0x48 */
  101. byte_num = 4;
  102. code_len = 0;
  103. for (i = 0; i < byte_num; i++) {
  104. val = *(tmp_buf + ESDHC_BOOT_IMAGE_SIZE + i);
  105. code_len = (code_len << 8) + val;
  106. }
  107. code_len -= CONFIG_SYS_MMC_U_BOOT_OFFS;
  108. /*
  109. * Load U-Boot image from mmc into RAM
  110. */
  111. #endif
  112. blk_start = ALIGN(offset, mmc->read_bl_len) / mmc->read_bl_len;
  113. blk_cnt = ALIGN(code_len, mmc->read_bl_len) / mmc->read_bl_len;
  114. err = mmc->block_dev.block_read(0, blk_start, blk_cnt,
  115. (uchar *)CONFIG_SYS_MMC_U_BOOT_DST);
  116. if (err != blk_cnt) {
  117. puts("spl: mmc read failed!!\n");
  118. #ifndef CONFIG_FSL_CORENET
  119. free(tmp_buf);
  120. #endif
  121. hang();
  122. }
  123. /*
  124. * Clean d-cache and invalidate i-cache, to
  125. * make sure that no stale data is executed.
  126. */
  127. flush_cache(CONFIG_SYS_MMC_U_BOOT_DST, CONFIG_SYS_MMC_U_BOOT_SIZE);
  128. /*
  129. * Jump to U-Boot image
  130. */
  131. uboot = (void *)CONFIG_SYS_MMC_U_BOOT_START;
  132. (*uboot)();
  133. }