spl_mmc.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 void mmc_load_image_raw(struct mmc *mmc)
  34. {
  35. u32 image_size_sectors, err;
  36. const struct image_header *header;
  37. header = (struct image_header *)(CONFIG_SYS_TEXT_BASE -
  38. sizeof(struct image_header));
  39. /* read image header to find the image size & load address */
  40. err = mmc->block_dev.block_read(0,
  41. CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR, 1,
  42. (void *)header);
  43. if (err <= 0)
  44. goto end;
  45. spl_parse_image_header(header);
  46. /* convert size to sectors - round up */
  47. image_size_sectors = (spl_image.size + mmc->read_bl_len - 1) /
  48. mmc->read_bl_len;
  49. /* Read the header too to avoid extra memcpy */
  50. err = mmc->block_dev.block_read(0,
  51. CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR,
  52. image_size_sectors, (void *)spl_image.load_addr);
  53. end:
  54. if (err <= 0) {
  55. printf("spl: mmc blk read err - %d\n", err);
  56. hang();
  57. }
  58. }
  59. #ifdef CONFIG_SPL_FAT_SUPPORT
  60. static void mmc_load_image_fat(struct mmc *mmc)
  61. {
  62. s32 err;
  63. struct image_header *header;
  64. header = (struct image_header *)(CONFIG_SYS_TEXT_BASE -
  65. sizeof(struct image_header));
  66. err = fat_register_device(&mmc->block_dev,
  67. CONFIG_SYS_MMC_SD_FAT_BOOT_PARTITION);
  68. if (err) {
  69. printf("spl: fat register err - %d\n", err);
  70. hang();
  71. }
  72. err = file_fat_read(CONFIG_SPL_FAT_LOAD_PAYLOAD_NAME,
  73. (u8 *)header, sizeof(struct image_header));
  74. if (err <= 0)
  75. goto end;
  76. spl_parse_image_header(header);
  77. err = file_fat_read(CONFIG_SPL_FAT_LOAD_PAYLOAD_NAME,
  78. (u8 *)spl_image.load_addr, 0);
  79. end:
  80. if (err <= 0) {
  81. printf("spl: error reading image %s, err - %d\n",
  82. CONFIG_SPL_FAT_LOAD_PAYLOAD_NAME, err);
  83. hang();
  84. }
  85. }
  86. #endif
  87. void spl_mmc_load_image(void)
  88. {
  89. struct mmc *mmc;
  90. int err;
  91. u32 boot_mode;
  92. mmc_initialize(gd->bd);
  93. /* We register only one device. So, the dev id is always 0 */
  94. mmc = find_mmc_device(0);
  95. if (!mmc) {
  96. puts("spl: mmc device not found!!\n");
  97. hang();
  98. }
  99. err = mmc_init(mmc);
  100. if (err) {
  101. printf("spl: mmc init failed: err - %d\n", err);
  102. hang();
  103. }
  104. boot_mode = spl_boot_mode();
  105. if (boot_mode == MMCSD_MODE_RAW) {
  106. debug("boot mode - RAW\n");
  107. mmc_load_image_raw(mmc);
  108. #ifdef CONFIG_SPL_FAT_SUPPORT
  109. } else if (boot_mode == MMCSD_MODE_FAT) {
  110. debug("boot mode - FAT\n");
  111. mmc_load_image_fat(mmc);
  112. #endif
  113. } else {
  114. puts("spl: wrong MMC boot mode\n");
  115. hang();
  116. }
  117. }