spl.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2014 Gateworks Corporation
  4. * Copyright (C) 2011-2012 Freescale Semiconductor, Inc.
  5. *
  6. * Author: Tim Harvey <tharvey@gateworks.com>
  7. */
  8. #include <common.h>
  9. #include <hang.h>
  10. #include <init.h>
  11. #include <log.h>
  12. #include <asm/io.h>
  13. #include <asm/arch/imx-regs.h>
  14. #include <asm/arch/sys_proto.h>
  15. #include <asm/spl.h>
  16. #include <spl.h>
  17. #include <asm/mach-imx/hab.h>
  18. #include <asm/mach-imx/boot_mode.h>
  19. #include <g_dnl.h>
  20. DECLARE_GLOBAL_DATA_PTR;
  21. __weak int spl_board_boot_device(enum boot_device boot_dev_spl)
  22. {
  23. return 0;
  24. }
  25. #if defined(CONFIG_MX6)
  26. /* determine boot device from SRC_SBMR1 (BOOT_CFG[4:1]) or SRC_GPR9 register */
  27. u32 spl_boot_device(void)
  28. {
  29. unsigned int bmode = readl(&src_base->sbmr2);
  30. u32 reg = imx6_src_get_boot_mode();
  31. /*
  32. * Check for BMODE if serial downloader is enabled
  33. * BOOT_MODE - see IMX6DQRM Table 8-1
  34. */
  35. if (((bmode >> 24) & 0x03) == 0x01) /* Serial Downloader */
  36. return BOOT_DEVICE_BOARD;
  37. /*
  38. * The above method does not detect that the boot ROM used
  39. * serial downloader in case the boot ROM decided to use the
  40. * serial downloader as a fall back (primary boot source failed).
  41. *
  42. * Infer that the boot ROM used the USB serial downloader by
  43. * checking whether the USB PHY is currently active... This
  44. * assumes that SPL did not (yet) initialize the USB PHY...
  45. */
  46. if (is_usbotg_phy_active())
  47. return BOOT_DEVICE_BOARD;
  48. /* BOOT_CFG1[7:4] - see IMX6DQRM Table 8-8 */
  49. switch ((reg & IMX6_BMODE_MASK) >> IMX6_BMODE_SHIFT) {
  50. /* EIM: See 8.5.1, Table 8-9 */
  51. case IMX6_BMODE_EMI:
  52. /* BOOT_CFG1[3]: NOR/OneNAND Selection */
  53. switch ((reg & IMX6_BMODE_EMI_MASK) >> IMX6_BMODE_EMI_SHIFT) {
  54. case IMX6_BMODE_ONENAND:
  55. return BOOT_DEVICE_ONENAND;
  56. case IMX6_BMODE_NOR:
  57. return BOOT_DEVICE_NOR;
  58. break;
  59. }
  60. /* Reserved: Used to force Serial Downloader */
  61. case IMX6_BMODE_RESERVED:
  62. return BOOT_DEVICE_BOARD;
  63. /* SATA: See 8.5.4, Table 8-20 */
  64. #if !defined(CONFIG_MX6UL) && !defined(CONFIG_MX6ULL)
  65. case IMX6_BMODE_SATA:
  66. return BOOT_DEVICE_SATA;
  67. #endif
  68. /* Serial ROM: See 8.5.5.1, Table 8-22 */
  69. case IMX6_BMODE_SERIAL_ROM:
  70. /* BOOT_CFG4[2:0] */
  71. switch ((reg & IMX6_BMODE_SERIAL_ROM_MASK) >>
  72. IMX6_BMODE_SERIAL_ROM_SHIFT) {
  73. case IMX6_BMODE_ECSPI1:
  74. case IMX6_BMODE_ECSPI2:
  75. case IMX6_BMODE_ECSPI3:
  76. case IMX6_BMODE_ECSPI4:
  77. case IMX6_BMODE_ECSPI5:
  78. return BOOT_DEVICE_SPI;
  79. case IMX6_BMODE_I2C1:
  80. case IMX6_BMODE_I2C2:
  81. case IMX6_BMODE_I2C3:
  82. return BOOT_DEVICE_I2C;
  83. }
  84. break;
  85. /* SD/eSD: 8.5.3, Table 8-15 */
  86. case IMX6_BMODE_SD:
  87. case IMX6_BMODE_ESD:
  88. return BOOT_DEVICE_MMC1;
  89. /* MMC/eMMC: 8.5.3 */
  90. case IMX6_BMODE_MMC:
  91. case IMX6_BMODE_EMMC:
  92. return BOOT_DEVICE_MMC1;
  93. /* NAND Flash: 8.5.2, Table 8-10 */
  94. case IMX6_BMODE_NAND_MIN ... IMX6_BMODE_NAND_MAX:
  95. return BOOT_DEVICE_NAND;
  96. #if defined(CONFIG_MX6UL) || defined(CONFIG_MX6ULL)
  97. /* QSPI boot */
  98. case IMX6_BMODE_QSPI:
  99. return BOOT_DEVICE_SPI;
  100. #endif
  101. }
  102. return BOOT_DEVICE_NONE;
  103. }
  104. #elif defined(CONFIG_MX7) || defined(CONFIG_IMX8M) || defined(CONFIG_IMX8)
  105. /* Translate iMX7/i.MX8M boot device to the SPL boot device enumeration */
  106. u32 spl_boot_device(void)
  107. {
  108. #if defined(CONFIG_MX7)
  109. unsigned int bmode = readl(&src_base->sbmr2);
  110. /*
  111. * Check for BMODE if serial downloader is enabled
  112. * BOOT_MODE - see IMX7DRM Table 6-24
  113. */
  114. if (((bmode >> 24) & 0x03) == 0x01) /* Serial Downloader */
  115. return BOOT_DEVICE_BOARD;
  116. /*
  117. * The above method does not detect that the boot ROM used
  118. * serial downloader in case the boot ROM decided to use the
  119. * serial downloader as a fall back (primary boot source failed).
  120. *
  121. * Infer that the boot ROM used the USB serial downloader by
  122. * checking whether the USB PHY is currently active... This
  123. * assumes that SPL did not (yet) initialize the USB PHY...
  124. */
  125. if (is_boot_from_usb())
  126. return BOOT_DEVICE_BOARD;
  127. #endif
  128. enum boot_device boot_device_spl = get_boot_device();
  129. if (IS_ENABLED(CONFIG_IMX8MM) || IS_ENABLED(CONFIG_IMX8MN) ||
  130. IS_ENABLED(CONFIG_IMX8MP))
  131. return spl_board_boot_device(boot_device_spl);
  132. switch (boot_device_spl) {
  133. #if defined(CONFIG_MX7)
  134. case SD1_BOOT:
  135. case MMC1_BOOT:
  136. case SD2_BOOT:
  137. case MMC2_BOOT:
  138. case SD3_BOOT:
  139. case MMC3_BOOT:
  140. return BOOT_DEVICE_MMC1;
  141. #elif defined(CONFIG_IMX8)
  142. case MMC1_BOOT:
  143. return BOOT_DEVICE_MMC1;
  144. case SD2_BOOT:
  145. return BOOT_DEVICE_MMC2_2;
  146. case SD3_BOOT:
  147. return BOOT_DEVICE_MMC1;
  148. case FLEXSPI_BOOT:
  149. return BOOT_DEVICE_SPI;
  150. #elif defined(CONFIG_IMX8M)
  151. case SD1_BOOT:
  152. case MMC1_BOOT:
  153. return BOOT_DEVICE_MMC1;
  154. case SD2_BOOT:
  155. case MMC2_BOOT:
  156. return BOOT_DEVICE_MMC2;
  157. #endif
  158. case NAND_BOOT:
  159. return BOOT_DEVICE_NAND;
  160. case SPI_NOR_BOOT:
  161. return BOOT_DEVICE_SPI;
  162. case USB_BOOT:
  163. return BOOT_DEVICE_USB;
  164. default:
  165. return BOOT_DEVICE_NONE;
  166. }
  167. }
  168. #endif /* CONFIG_MX7 || CONFIG_IMX8M || CONFIG_IMX8 */
  169. #ifdef CONFIG_SPL_USB_GADGET
  170. int g_dnl_bind_fixup(struct usb_device_descriptor *dev, const char *name)
  171. {
  172. put_unaligned(CONFIG_USB_GADGET_PRODUCT_NUM + 0xfff, &dev->idProduct);
  173. return 0;
  174. }
  175. #endif
  176. #if defined(CONFIG_SPL_MMC_SUPPORT)
  177. /* called from spl_mmc to see type of boot mode for storage (RAW or FAT) */
  178. u32 spl_mmc_boot_mode(const u32 boot_device)
  179. {
  180. #if defined(CONFIG_MX7) || defined(CONFIG_IMX8M) || defined(CONFIG_IMX8)
  181. switch (get_boot_device()) {
  182. /* for MMC return either RAW or FAT mode */
  183. case SD1_BOOT:
  184. case SD2_BOOT:
  185. case SD3_BOOT:
  186. if (IS_ENABLED(CONFIG_SPL_FS_FAT))
  187. return MMCSD_MODE_FS;
  188. else
  189. return MMCSD_MODE_RAW;
  190. case MMC1_BOOT:
  191. case MMC2_BOOT:
  192. case MMC3_BOOT:
  193. if (IS_ENABLED(CONFIG_SPL_FS_FAT))
  194. return MMCSD_MODE_FS;
  195. else if (IS_ENABLED(CONFIG_SUPPORT_EMMC_BOOT))
  196. return MMCSD_MODE_EMMCBOOT;
  197. else
  198. return MMCSD_MODE_RAW;
  199. default:
  200. puts("spl: ERROR: unsupported device\n");
  201. hang();
  202. }
  203. #else
  204. switch (boot_device) {
  205. /* for MMC return either RAW or FAT mode */
  206. case BOOT_DEVICE_MMC1:
  207. case BOOT_DEVICE_MMC2:
  208. case BOOT_DEVICE_MMC2_2:
  209. if (IS_ENABLED(CONFIG_SPL_FS_FAT))
  210. return MMCSD_MODE_FS;
  211. else if (IS_ENABLED(CONFIG_SUPPORT_EMMC_BOOT))
  212. return MMCSD_MODE_EMMCBOOT;
  213. else
  214. return MMCSD_MODE_RAW;
  215. default:
  216. puts("spl: ERROR: unsupported device\n");
  217. hang();
  218. }
  219. #endif
  220. }
  221. #endif
  222. #if defined(CONFIG_IMX_HAB)
  223. /*
  224. * +------------+ 0x0 (DDR_UIMAGE_START) -
  225. * | Header | |
  226. * +------------+ 0x40 |
  227. * | | |
  228. * | | |
  229. * | | |
  230. * | | |
  231. * | Image Data | |
  232. * . | |
  233. * . | > Stuff to be authenticated ----+
  234. * . | | |
  235. * | | | |
  236. * | | | |
  237. * +------------+ | |
  238. * | | | |
  239. * | Fill Data | | |
  240. * | | | |
  241. * +------------+ Align to ALIGN_SIZE | |
  242. * | IVT | | |
  243. * +------------+ + IVT_SIZE - |
  244. * | | |
  245. * | CSF DATA | <---------------------------------------------------------+
  246. * | |
  247. * +------------+
  248. * | |
  249. * | Fill Data |
  250. * | |
  251. * +------------+ + CSF_PAD_SIZE
  252. */
  253. __weak void __noreturn jump_to_image_no_args(struct spl_image_info *spl_image)
  254. {
  255. typedef void __noreturn (*image_entry_noargs_t)(void);
  256. uint32_t offset;
  257. image_entry_noargs_t image_entry =
  258. (image_entry_noargs_t)(unsigned long)spl_image->entry_point;
  259. debug("image entry point: 0x%lX\n", spl_image->entry_point);
  260. if (spl_image->flags & SPL_FIT_FOUND) {
  261. image_entry();
  262. } else {
  263. /*
  264. * HAB looks for the CSF at the end of the authenticated
  265. * data therefore, we need to subtract the size of the
  266. * CSF from the actual filesize
  267. */
  268. offset = spl_image->size - CONFIG_CSF_SIZE;
  269. if (!imx_hab_authenticate_image(spl_image->load_addr,
  270. offset + IVT_SIZE +
  271. CSF_PAD_SIZE, offset)) {
  272. image_entry();
  273. } else {
  274. puts("spl: ERROR: image authentication fail\n");
  275. hang();
  276. }
  277. }
  278. }
  279. #if !defined(CONFIG_SPL_FIT_SIGNATURE)
  280. ulong board_spl_fit_size_align(ulong size)
  281. {
  282. /*
  283. * HAB authenticate_image requests the IVT offset is
  284. * aligned to 0x1000
  285. */
  286. size = ALIGN(size, 0x1000);
  287. size += CONFIG_CSF_SIZE;
  288. return size;
  289. }
  290. void board_spl_fit_post_load(ulong load_addr, size_t length)
  291. {
  292. u32 offset = length - CONFIG_CSF_SIZE;
  293. if (imx_hab_authenticate_image(load_addr,
  294. offset + IVT_SIZE + CSF_PAD_SIZE,
  295. offset)) {
  296. puts("spl: ERROR: image authentication unsuccessful\n");
  297. hang();
  298. }
  299. }
  300. #endif
  301. #endif
  302. #if defined(CONFIG_MX6) && defined(CONFIG_SPL_OS_BOOT)
  303. int dram_init_banksize(void)
  304. {
  305. gd->bd->bi_dram[0].start = CONFIG_SYS_SDRAM_BASE;
  306. gd->bd->bi_dram[0].size = imx_ddr_size();
  307. return 0;
  308. }
  309. #endif