spl.c 9.1 KB

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