spl_mmc.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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, unsigned long sector)
  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, sector, 1, header);
  42. if (err == 0)
  43. goto end;
  44. spl_parse_image_header(header);
  45. /* convert size to sectors - round up */
  46. image_size_sectors = (spl_image.size + mmc->read_bl_len - 1) /
  47. mmc->read_bl_len;
  48. /* Read the header too to avoid extra memcpy */
  49. err = mmc->block_dev.block_read(0, sector, image_size_sectors,
  50. (void *)spl_image.load_addr);
  51. end:
  52. if (err == 0)
  53. printf("spl: mmc blk read err - %lu\n", err);
  54. return (err == 0);
  55. }
  56. #ifdef CONFIG_SPL_OS_BOOT
  57. static int mmc_load_image_raw_os(struct mmc *mmc)
  58. {
  59. if (!mmc->block_dev.block_read(0,
  60. CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR,
  61. CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS,
  62. (void *)CONFIG_SYS_SPL_ARGS_ADDR)) {
  63. printf("mmc args blk read error\n");
  64. return -1;
  65. }
  66. return mmc_load_image_raw(mmc, CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR);
  67. }
  68. #endif
  69. #ifdef CONFIG_SPL_FAT_SUPPORT
  70. static int mmc_load_image_fat(struct mmc *mmc, const char *filename)
  71. {
  72. int err;
  73. struct image_header *header;
  74. header = (struct image_header *)(CONFIG_SYS_TEXT_BASE -
  75. sizeof(struct image_header));
  76. err = file_fat_read(filename, header, sizeof(struct image_header));
  77. if (err <= 0)
  78. goto end;
  79. spl_parse_image_header(header);
  80. err = file_fat_read(filename, (u8 *)spl_image.load_addr, 0);
  81. end:
  82. if (err <= 0)
  83. printf("spl: error reading image %s, err - %d\n",
  84. filename, err);
  85. return (err <= 0);
  86. }
  87. #ifdef CONFIG_SPL_OS_BOOT
  88. static int mmc_load_image_fat_os(struct mmc *mmc)
  89. {
  90. int err;
  91. err = file_fat_read(CONFIG_SPL_FAT_LOAD_ARGS_NAME,
  92. (void *)CONFIG_SYS_SPL_ARGS_ADDR, 0);
  93. if (err <= 0) {
  94. printf("spl: error reading image %s, err - %d\n",
  95. CONFIG_SPL_FAT_LOAD_ARGS_NAME, err);
  96. return -1;
  97. }
  98. return mmc_load_image_fat(mmc, CONFIG_SPL_FAT_LOAD_KERNEL_NAME);
  99. }
  100. #endif
  101. #endif
  102. void spl_mmc_load_image(void)
  103. {
  104. struct mmc *mmc;
  105. int err;
  106. u32 boot_mode;
  107. mmc_initialize(gd->bd);
  108. /* We register only one device. So, the dev id is always 0 */
  109. mmc = find_mmc_device(0);
  110. if (!mmc) {
  111. puts("spl: mmc device not found!!\n");
  112. hang();
  113. }
  114. err = mmc_init(mmc);
  115. if (err) {
  116. printf("spl: mmc init failed: err - %d\n", err);
  117. hang();
  118. }
  119. boot_mode = spl_boot_mode();
  120. if (boot_mode == MMCSD_MODE_RAW) {
  121. debug("boot mode - RAW\n");
  122. #ifdef CONFIG_SPL_OS_BOOT
  123. if (spl_start_uboot() || mmc_load_image_raw_os(mmc))
  124. #endif
  125. err = mmc_load_image_raw(mmc,
  126. CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
  127. #ifdef CONFIG_SPL_FAT_SUPPORT
  128. } else if (boot_mode == MMCSD_MODE_FAT) {
  129. debug("boot mode - FAT\n");
  130. err = fat_register_device(&mmc->block_dev,
  131. CONFIG_SYS_MMC_SD_FAT_BOOT_PARTITION);
  132. if (err) {
  133. printf("spl: fat register err - %d\n", err);
  134. hang();
  135. }
  136. #ifdef CONFIG_SPL_OS_BOOT
  137. if (spl_start_uboot() || mmc_load_image_fat_os(mmc))
  138. #endif
  139. err = mmc_load_image_fat(mmc, CONFIG_SPL_FAT_LOAD_PAYLOAD_NAME);
  140. #endif
  141. } else {
  142. puts("spl: wrong MMC boot mode\n");
  143. hang();
  144. }
  145. if (err)
  146. hang();
  147. }