spl_mmc.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. #endif
  78. void spl_mmc_load_image(void)
  79. {
  80. struct mmc *mmc;
  81. int err;
  82. u32 boot_mode;
  83. mmc_initialize(gd->bd);
  84. /* We register only one device. So, the dev id is always 0 */
  85. mmc = find_mmc_device(0);
  86. if (!mmc) {
  87. puts("spl: mmc device not found!!\n");
  88. hang();
  89. }
  90. err = mmc_init(mmc);
  91. if (err) {
  92. printf("spl: mmc init failed: err - %d\n", err);
  93. hang();
  94. }
  95. boot_mode = spl_boot_mode();
  96. if (boot_mode == MMCSD_MODE_RAW) {
  97. debug("boot mode - RAW\n");
  98. err = mmc_load_image_raw(mmc);
  99. #ifdef CONFIG_SPL_FAT_SUPPORT
  100. } else if (boot_mode == MMCSD_MODE_FAT) {
  101. debug("boot mode - FAT\n");
  102. err = fat_register_device(&mmc->block_dev,
  103. CONFIG_SYS_MMC_SD_FAT_BOOT_PARTITION);
  104. if (err) {
  105. printf("spl: fat register err - %d\n", err);
  106. hang();
  107. }
  108. err = mmc_load_image_fat(mmc, CONFIG_SPL_FAT_LOAD_PAYLOAD_NAME);
  109. #endif
  110. } else {
  111. puts("spl: wrong MMC boot mode\n");
  112. hang();
  113. }
  114. if (err)
  115. hang();
  116. }