spl_mmc.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 <asm/spl.h>
  27. #include <asm/u-boot.h>
  28. #include <asm/utils.h>
  29. #include <asm/arch/sys_proto.h>
  30. #include <mmc.h>
  31. #include <fat.h>
  32. #include <version.h>
  33. #include <asm/omap_common.h>
  34. #include <asm/arch/mmc_host_def.h>
  35. DECLARE_GLOBAL_DATA_PTR;
  36. #ifdef CONFIG_GENERIC_MMC
  37. int board_mmc_init(bd_t *bis)
  38. {
  39. switch (spl_boot_device()) {
  40. case BOOT_DEVICE_MMC1:
  41. omap_mmc_init(0, 0, 0);
  42. break;
  43. case BOOT_DEVICE_MMC2:
  44. case BOOT_DEVICE_MMC2_2:
  45. omap_mmc_init(1, 0, 0);
  46. break;
  47. }
  48. return 0;
  49. }
  50. #endif
  51. static void mmc_load_image_raw(struct mmc *mmc)
  52. {
  53. u32 image_size_sectors, err;
  54. const struct image_header *header;
  55. header = (struct image_header *)(CONFIG_SYS_TEXT_BASE -
  56. sizeof(struct image_header));
  57. /* read image header to find the image size & load address */
  58. err = mmc->block_dev.block_read(0,
  59. CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR, 1,
  60. (void *)header);
  61. if (err <= 0)
  62. goto end;
  63. spl_parse_image_header(header);
  64. /* convert size to sectors - round up */
  65. image_size_sectors = (spl_image.size + MMCSD_SECTOR_SIZE - 1) /
  66. MMCSD_SECTOR_SIZE;
  67. /* Read the header too to avoid extra memcpy */
  68. err = mmc->block_dev.block_read(0,
  69. CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR,
  70. image_size_sectors, (void *)spl_image.load_addr);
  71. end:
  72. if (err <= 0) {
  73. printf("spl: mmc blk read err - %d\n", err);
  74. hang();
  75. }
  76. }
  77. #ifdef CONFIG_SPL_FAT_SUPPORT
  78. static void mmc_load_image_fat(struct mmc *mmc)
  79. {
  80. s32 err;
  81. struct image_header *header;
  82. header = (struct image_header *)(CONFIG_SYS_TEXT_BASE -
  83. sizeof(struct image_header));
  84. err = fat_register_device(&mmc->block_dev,
  85. CONFIG_SYS_MMC_SD_FAT_BOOT_PARTITION);
  86. if (err) {
  87. printf("spl: fat register err - %d\n", err);
  88. hang();
  89. }
  90. err = file_fat_read(CONFIG_SPL_FAT_LOAD_PAYLOAD_NAME,
  91. (u8 *)header, sizeof(struct image_header));
  92. if (err <= 0)
  93. goto end;
  94. spl_parse_image_header(header);
  95. err = file_fat_read(CONFIG_SPL_FAT_LOAD_PAYLOAD_NAME,
  96. (u8 *)spl_image.load_addr, 0);
  97. end:
  98. if (err <= 0) {
  99. printf("spl: error reading image %s, err - %d\n",
  100. CONFIG_SPL_FAT_LOAD_PAYLOAD_NAME, err);
  101. hang();
  102. }
  103. }
  104. #endif
  105. void spl_mmc_load_image(void)
  106. {
  107. struct mmc *mmc;
  108. int err;
  109. u32 boot_mode;
  110. mmc_initialize(gd->bd);
  111. /* We register only one device. So, the dev id is always 0 */
  112. mmc = find_mmc_device(0);
  113. if (!mmc) {
  114. puts("spl: mmc device not found!!\n");
  115. hang();
  116. }
  117. err = mmc_init(mmc);
  118. if (err) {
  119. printf("spl: mmc init failed: err - %d\n", err);
  120. hang();
  121. }
  122. boot_mode = spl_boot_mode();
  123. if (boot_mode == MMCSD_MODE_RAW) {
  124. debug("boot mode - RAW\n");
  125. mmc_load_image_raw(mmc);
  126. #ifdef CONFIG_SPL_FAT_SUPPORT
  127. } else if (boot_mode == MMCSD_MODE_FAT) {
  128. debug("boot mode - FAT\n");
  129. mmc_load_image_fat(mmc);
  130. #endif
  131. } else {
  132. puts("spl: wrong MMC boot mode\n");
  133. hang();
  134. }
  135. }