spl_mmc.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. 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. hang();
  58. }
  59. }
  60. #ifdef CONFIG_SPL_FAT_SUPPORT
  61. static void mmc_load_image_fat(struct mmc *mmc)
  62. {
  63. int err;
  64. struct image_header *header;
  65. header = (struct image_header *)(CONFIG_SYS_TEXT_BASE -
  66. sizeof(struct image_header));
  67. err = fat_register_device(&mmc->block_dev,
  68. CONFIG_SYS_MMC_SD_FAT_BOOT_PARTITION);
  69. if (err) {
  70. printf("spl: fat register err - %d\n", err);
  71. hang();
  72. }
  73. err = file_fat_read(CONFIG_SPL_FAT_LOAD_PAYLOAD_NAME,
  74. header, sizeof(struct image_header));
  75. if (err <= 0)
  76. goto end;
  77. spl_parse_image_header(header);
  78. err = file_fat_read(CONFIG_SPL_FAT_LOAD_PAYLOAD_NAME,
  79. (u8 *)spl_image.load_addr, 0);
  80. end:
  81. if (err <= 0) {
  82. printf("spl: error reading image %s, err - %d\n",
  83. CONFIG_SPL_FAT_LOAD_PAYLOAD_NAME, err);
  84. hang();
  85. }
  86. }
  87. #endif
  88. void spl_mmc_load_image(void)
  89. {
  90. struct mmc *mmc;
  91. int err;
  92. u32 boot_mode;
  93. mmc_initialize(gd->bd);
  94. /* We register only one device. So, the dev id is always 0 */
  95. mmc = find_mmc_device(0);
  96. if (!mmc) {
  97. puts("spl: mmc device not found!!\n");
  98. hang();
  99. }
  100. err = mmc_init(mmc);
  101. if (err) {
  102. printf("spl: mmc init failed: err - %d\n", err);
  103. hang();
  104. }
  105. boot_mode = spl_boot_mode();
  106. if (boot_mode == MMCSD_MODE_RAW) {
  107. debug("boot mode - RAW\n");
  108. mmc_load_image_raw(mmc);
  109. #ifdef CONFIG_SPL_FAT_SUPPORT
  110. } else if (boot_mode == MMCSD_MODE_FAT) {
  111. debug("boot mode - FAT\n");
  112. mmc_load_image_fat(mmc);
  113. #endif
  114. } else {
  115. puts("spl: wrong MMC boot mode\n");
  116. hang();
  117. }
  118. }