spl.c 9.5 KB

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