spl_mmc.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * (C) Copyright 2010
  3. * Texas Instruments, <www.ti.com>
  4. *
  5. * Aneesh V <aneesh@ti.com>
  6. *
  7. * See file CREDITS for list of people who contributed to this
  8. * project.
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License as
  12. * published by the Free Software Foundation; either version 2 of
  13. * the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  23. * MA 02111-1307 USA
  24. */
  25. #include <common.h>
  26. #include <spl.h>
  27. #include <asm/u-boot.h>
  28. #include <asm/utils.h>
  29. #include <mmc.h>
  30. #include <fat.h>
  31. #include <version.h>
  32. DECLARE_GLOBAL_DATA_PTR;
  33. static int mmc_load_image_raw(struct mmc *mmc)
  34. {
  35. unsigned long err;
  36. u32 image_size_sectors;
  37. struct image_header *header;
  38. header = (struct image_header *)(CONFIG_SYS_TEXT_BASE -
  39. sizeof(struct image_header));
  40. /* read image header to find the image size & load address */
  41. err = mmc->block_dev.block_read(0,
  42. CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR, 1,
  43. header);
  44. if (err == 0)
  45. goto end;
  46. spl_parse_image_header(header);
  47. /* convert size to sectors - round up */
  48. image_size_sectors = (spl_image.size + mmc->read_bl_len - 1) /
  49. mmc->read_bl_len;
  50. /* Read the header too to avoid extra memcpy */
  51. err = mmc->block_dev.block_read(0,
  52. CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR,
  53. image_size_sectors, (void *)spl_image.load_addr);
  54. end:
  55. if (err == 0)
  56. printf("spl: mmc blk read err - %lu\n", err);
  57. return (err == 0);
  58. }
  59. #ifdef CONFIG_SPL_FAT_SUPPORT
  60. static int mmc_load_image_fat(struct mmc *mmc, const char *filename)
  61. {
  62. int err;
  63. struct image_header *header;
  64. header = (struct image_header *)(CONFIG_SYS_TEXT_BASE -
  65. sizeof(struct image_header));
  66. err = file_fat_read(filename, header, sizeof(struct image_header));
  67. if (err <= 0)
  68. goto end;
  69. spl_parse_image_header(header);
  70. err = file_fat_read(filename, (u8 *)spl_image.load_addr, 0);
  71. end:
  72. if (err <= 0)
  73. printf("spl: error reading image %s, err - %d\n",
  74. filename, err);
  75. return (err <= 0);
  76. }
  77. #ifdef CONFIG_SPL_OS_BOOT
  78. static int mmc_load_image_fat_os(struct mmc *mmc)
  79. {
  80. int err;
  81. err = file_fat_read(CONFIG_SPL_FAT_LOAD_ARGS_NAME,
  82. (void *)CONFIG_SYS_SPL_ARGS_ADDR, 0);
  83. if (err <= 0) {
  84. printf("spl: error reading image %s, err - %d\n",
  85. CONFIG_SPL_FAT_LOAD_ARGS_NAME, err);
  86. return -1;
  87. }
  88. return mmc_load_image_fat(mmc, CONFIG_SPL_FAT_LOAD_KERNEL_NAME);
  89. }
  90. #endif
  91. #endif
  92. void spl_mmc_load_image(void)
  93. {
  94. struct mmc *mmc;
  95. int err;
  96. u32 boot_mode;
  97. mmc_initialize(gd->bd);
  98. /* We register only one device. So, the dev id is always 0 */
  99. mmc = find_mmc_device(0);
  100. if (!mmc) {
  101. puts("spl: mmc device not found!!\n");
  102. hang();
  103. }
  104. err = mmc_init(mmc);
  105. if (err) {
  106. printf("spl: mmc init failed: err - %d\n", err);
  107. hang();
  108. }
  109. boot_mode = spl_boot_mode();
  110. if (boot_mode == MMCSD_MODE_RAW) {
  111. debug("boot mode - RAW\n");
  112. err = mmc_load_image_raw(mmc);
  113. #ifdef CONFIG_SPL_FAT_SUPPORT
  114. } else if (boot_mode == MMCSD_MODE_FAT) {
  115. debug("boot mode - FAT\n");
  116. err = fat_register_device(&mmc->block_dev,
  117. CONFIG_SYS_MMC_SD_FAT_BOOT_PARTITION);
  118. if (err) {
  119. printf("spl: fat register err - %d\n", err);
  120. hang();
  121. }
  122. #ifdef CONFIG_SPL_OS_BOOT
  123. if (spl_start_uboot() || mmc_load_image_fat_os(mmc))
  124. #endif
  125. err = mmc_load_image_fat(mmc, CONFIG_SPL_FAT_LOAD_PAYLOAD_NAME);
  126. #endif
  127. } else {
  128. puts("spl: wrong MMC boot mode\n");
  129. hang();
  130. }
  131. if (err)
  132. hang();
  133. }