spl_mmc.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*
  2. * (C) Copyright 2010
  3. * Texas Instruments, <www.ti.com>
  4. *
  5. * Aneesh V <aneesh@ti.com>
  6. *
  7. * SPDX-License-Identifier: GPL-2.0+
  8. */
  9. #include <common.h>
  10. #include <dm.h>
  11. #include <spl.h>
  12. #include <linux/compiler.h>
  13. #include <errno.h>
  14. #include <asm/u-boot.h>
  15. #include <errno.h>
  16. #include <mmc.h>
  17. #include <image.h>
  18. DECLARE_GLOBAL_DATA_PTR;
  19. static int mmc_load_image_raw_sector(struct mmc *mmc, unsigned long sector)
  20. {
  21. unsigned long count;
  22. u32 image_size_sectors;
  23. struct image_header *header;
  24. int dev_num = mmc->block_dev.dev;
  25. header = (struct image_header *)(CONFIG_SYS_TEXT_BASE -
  26. sizeof(struct image_header));
  27. /* read image header to find the image size & load address */
  28. count = mmc->block_dev.block_read(dev_num, sector, 1, header);
  29. debug("read sector %lx, count=%lu\n", sector, count);
  30. if (count == 0)
  31. goto end;
  32. if (image_get_magic(header) != IH_MAGIC) {
  33. puts("bad magic\n");
  34. return -1;
  35. }
  36. spl_parse_image_header(header);
  37. /* convert size to sectors - round up */
  38. image_size_sectors = (spl_image.size + mmc->read_bl_len - 1) /
  39. mmc->read_bl_len;
  40. /* Read the header too to avoid extra memcpy */
  41. count = mmc->block_dev.block_read(dev_num, sector, image_size_sectors,
  42. (void *)(ulong)spl_image.load_addr);
  43. debug("read %x sectors to %x\n", image_size_sectors,
  44. spl_image.load_addr);
  45. end:
  46. if (count == 0) {
  47. #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
  48. puts("spl: mmc block read error\n");
  49. #endif
  50. return -1;
  51. }
  52. return 0;
  53. }
  54. int spl_mmc_get_device_index(u32 boot_device)
  55. {
  56. switch (boot_device) {
  57. case BOOT_DEVICE_MMC1:
  58. return 0;
  59. case BOOT_DEVICE_MMC2:
  60. case BOOT_DEVICE_MMC2_2:
  61. return 1;
  62. }
  63. #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
  64. printf("spl: unsupported mmc boot device.\n");
  65. #endif
  66. return -ENODEV;
  67. }
  68. static int spl_mmc_find_device(struct mmc **mmcp, u32 boot_device)
  69. {
  70. #ifdef CONFIG_DM_MMC
  71. struct udevice *dev;
  72. #endif
  73. int err, mmc_dev;
  74. mmc_dev = spl_mmc_get_device_index(boot_device);
  75. if (mmc_dev < 0)
  76. return mmc_dev;
  77. err = mmc_initialize(NULL);
  78. if (err) {
  79. #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
  80. printf("spl: could not initialize mmc. error: %d\n", err);
  81. #endif
  82. return err;
  83. }
  84. #ifdef CONFIG_DM_MMC
  85. err = uclass_get_device(UCLASS_MMC, mmc_dev, &dev);
  86. if (!err)
  87. *mmcp = mmc_get_mmc_dev(dev);
  88. #else
  89. *mmcp = find_mmc_device(mmc_dev);
  90. err = *mmcp ? 0 : -ENODEV;
  91. #endif
  92. if (err) {
  93. #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
  94. printf("spl: could not find mmc device. error: %d\n", err);
  95. #endif
  96. return err;
  97. }
  98. return 0;
  99. }
  100. #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION
  101. static int mmc_load_image_raw_partition(struct mmc *mmc, int partition)
  102. {
  103. disk_partition_t info;
  104. int err;
  105. err = get_partition_info(&mmc->block_dev, partition, &info);
  106. if (err) {
  107. #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
  108. puts("spl: partition error\n");
  109. #endif
  110. return -1;
  111. }
  112. #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR
  113. return mmc_load_image_raw_sector(mmc, info.start +
  114. CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
  115. #else
  116. return mmc_load_image_raw_sector(mmc, info.start);
  117. #endif
  118. }
  119. #else
  120. #define CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION -1
  121. static int mmc_load_image_raw_partition(struct mmc *mmc, int partition)
  122. {
  123. return -ENOSYS;
  124. }
  125. #endif
  126. #ifdef CONFIG_SPL_OS_BOOT
  127. static int mmc_load_image_raw_os(struct mmc *mmc)
  128. {
  129. unsigned long count;
  130. count = mmc->block_dev.block_read(
  131. mmc->block_dev.dev,
  132. CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR,
  133. CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS,
  134. (void *) CONFIG_SYS_SPL_ARGS_ADDR);
  135. if (count == 0) {
  136. #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
  137. puts("spl: mmc block read error\n");
  138. #endif
  139. return -1;
  140. }
  141. return mmc_load_image_raw_sector(mmc,
  142. CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR);
  143. }
  144. #else
  145. int spl_start_uboot(void)
  146. {
  147. return 1;
  148. }
  149. static int mmc_load_image_raw_os(struct mmc *mmc)
  150. {
  151. return -ENOSYS;
  152. }
  153. #endif
  154. #ifdef CONFIG_SYS_MMCSD_FS_BOOT_PARTITION
  155. int spl_mmc_do_fs_boot(struct mmc *mmc)
  156. {
  157. int err = -ENOSYS;
  158. #ifdef CONFIG_SPL_FAT_SUPPORT
  159. if (!spl_start_uboot()) {
  160. err = spl_load_image_fat_os(&mmc->block_dev,
  161. CONFIG_SYS_MMCSD_FS_BOOT_PARTITION);
  162. if (!err)
  163. return err;
  164. }
  165. #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
  166. err = spl_load_image_fat(&mmc->block_dev,
  167. CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
  168. CONFIG_SPL_FS_LOAD_PAYLOAD_NAME);
  169. if (!err)
  170. return err;
  171. #endif
  172. #endif
  173. #ifdef CONFIG_SPL_EXT_SUPPORT
  174. if (!spl_start_uboot()) {
  175. err = spl_load_image_ext_os(&mmc->block_dev,
  176. CONFIG_SYS_MMCSD_FS_BOOT_PARTITION);
  177. if (!err)
  178. return err;
  179. }
  180. #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
  181. err = spl_load_image_ext(&mmc->block_dev,
  182. CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
  183. CONFIG_SPL_FS_LOAD_PAYLOAD_NAME);
  184. if (!err)
  185. return err;
  186. #endif
  187. #endif
  188. #if defined(CONFIG_SPL_FAT_SUPPORT) || defined(CONFIG_SPL_EXT_SUPPORT)
  189. err = -ENOENT;
  190. #endif
  191. return err;
  192. }
  193. #else
  194. int spl_mmc_do_fs_boot(struct mmc *mmc)
  195. {
  196. return -ENOSYS;
  197. }
  198. #endif
  199. int spl_mmc_load_image(u32 boot_device)
  200. {
  201. struct mmc *mmc = NULL;
  202. u32 boot_mode;
  203. int err = 0;
  204. __maybe_unused int part;
  205. err = spl_mmc_find_device(&mmc, boot_device);
  206. if (err)
  207. return err;
  208. err = mmc_init(mmc);
  209. if (err) {
  210. #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
  211. printf("spl: mmc init failed with error: %d\n", err);
  212. #endif
  213. return err;
  214. }
  215. boot_mode = spl_boot_mode();
  216. err = -EINVAL;
  217. switch (boot_mode) {
  218. case MMCSD_MODE_EMMCBOOT:
  219. /*
  220. * We need to check what the partition is configured to.
  221. * 1 and 2 match up to boot0 / boot1 and 7 is user data
  222. * which is the first physical partition (0).
  223. */
  224. part = (mmc->part_config >> 3) & PART_ACCESS_MASK;
  225. if (part == 7)
  226. part = 0;
  227. err = mmc_switch_part(0, part);
  228. if (err) {
  229. #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
  230. puts("spl: mmc partition switch failed\n");
  231. #endif
  232. return err;
  233. }
  234. /* Fall through */
  235. case MMCSD_MODE_RAW:
  236. debug("spl: mmc boot mode: raw\n");
  237. if (!spl_start_uboot()) {
  238. err = mmc_load_image_raw_os(mmc);
  239. if (!err)
  240. return err;
  241. }
  242. err = mmc_load_image_raw_partition(mmc,
  243. CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION);
  244. if (!err)
  245. return err;
  246. #if defined(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR)
  247. err = mmc_load_image_raw_sector(mmc,
  248. CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
  249. if (!err)
  250. return err;
  251. #endif
  252. break;
  253. case MMCSD_MODE_FS:
  254. debug("spl: mmc boot mode: fs\n");
  255. err = spl_mmc_do_fs_boot(mmc);
  256. if (!err)
  257. return err;
  258. break;
  259. case MMCSD_MODE_UNDEFINED:
  260. #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
  261. default:
  262. puts("spl: mmc: wrong boot mode\n");
  263. #endif
  264. }
  265. return err;
  266. }