spl.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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 <asm/io.h>
  10. #include <asm/arch/imx-regs.h>
  11. #include <asm/arch/sys_proto.h>
  12. #include <asm/spl.h>
  13. #include <spl.h>
  14. #include <asm/mach-imx/hab.h>
  15. #include <asm/mach-imx/boot_mode.h>
  16. #include <g_dnl.h>
  17. DECLARE_GLOBAL_DATA_PTR;
  18. __weak int spl_board_boot_device(enum boot_device boot_dev_spl)
  19. {
  20. return 0;
  21. }
  22. #if defined(CONFIG_MX6)
  23. /* determine boot device from SRC_SBMR1 (BOOT_CFG[4:1]) or SRC_GPR9 register */
  24. u32 spl_boot_device(void)
  25. {
  26. unsigned int bmode = readl(&src_base->sbmr2);
  27. u32 reg = imx6_src_get_boot_mode();
  28. u32 mmc_index = ((reg >> 11) & 0x03);
  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. case IMX6_BMODE_MMC:
  87. case IMX6_BMODE_EMMC:
  88. if (mmc_index == 1)
  89. return BOOT_DEVICE_MMC2;
  90. else
  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))
  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. /*
  179. * When CONFIG_SPL_FORCE_MMC_BOOT is defined the 'boot_device' is used
  180. * unconditionally to decide about device to use for booting.
  181. * This is crucial for falcon boot mode, when board boots up (i.e. ROM
  182. * loads SPL) from slow SPI-NOR memory and afterwards the SPL's 'falcon' boot
  183. * mode is used to load Linux OS from eMMC partition.
  184. */
  185. #ifdef CONFIG_SPL_FORCE_MMC_BOOT
  186. switch (boot_device) {
  187. #else
  188. switch (spl_boot_device()) {
  189. #endif
  190. /* for MMC return either RAW or FAT mode */
  191. case BOOT_DEVICE_MMC1:
  192. case BOOT_DEVICE_MMC2:
  193. case BOOT_DEVICE_MMC2_2:
  194. #if defined(CONFIG_SPL_FS_FAT)
  195. return MMCSD_MODE_FS;
  196. #elif defined(CONFIG_SUPPORT_EMMC_BOOT)
  197. return MMCSD_MODE_EMMCBOOT;
  198. #else
  199. return MMCSD_MODE_RAW;
  200. #endif
  201. break;
  202. default:
  203. puts("spl: ERROR: unsupported device\n");
  204. hang();
  205. }
  206. }
  207. #endif
  208. #if defined(CONFIG_IMX_HAB)
  209. /*
  210. * +------------+ 0x0 (DDR_UIMAGE_START) -
  211. * | Header | |
  212. * +------------+ 0x40 |
  213. * | | |
  214. * | | |
  215. * | | |
  216. * | | |
  217. * | Image Data | |
  218. * . | |
  219. * . | > Stuff to be authenticated ----+
  220. * . | | |
  221. * | | | |
  222. * | | | |
  223. * +------------+ | |
  224. * | | | |
  225. * | Fill Data | | |
  226. * | | | |
  227. * +------------+ Align to ALIGN_SIZE | |
  228. * | IVT | | |
  229. * +------------+ + IVT_SIZE - |
  230. * | | |
  231. * | CSF DATA | <---------------------------------------------------------+
  232. * | |
  233. * +------------+
  234. * | |
  235. * | Fill Data |
  236. * | |
  237. * +------------+ + CSF_PAD_SIZE
  238. */
  239. __weak void __noreturn jump_to_image_no_args(struct spl_image_info *spl_image)
  240. {
  241. typedef void __noreturn (*image_entry_noargs_t)(void);
  242. uint32_t offset;
  243. image_entry_noargs_t image_entry =
  244. (image_entry_noargs_t)(unsigned long)spl_image->entry_point;
  245. debug("image entry point: 0x%lX\n", spl_image->entry_point);
  246. if (spl_image->flags & SPL_FIT_FOUND) {
  247. image_entry();
  248. } else {
  249. /*
  250. * HAB looks for the CSF at the end of the authenticated
  251. * data therefore, we need to subtract the size of the
  252. * CSF from the actual filesize
  253. */
  254. offset = spl_image->size - CONFIG_CSF_SIZE;
  255. if (!imx_hab_authenticate_image(spl_image->load_addr,
  256. offset + IVT_SIZE +
  257. CSF_PAD_SIZE, offset)) {
  258. image_entry();
  259. } else {
  260. puts("spl: ERROR: image authentication fail\n");
  261. hang();
  262. }
  263. }
  264. }
  265. #if !defined(CONFIG_SPL_FIT_SIGNATURE)
  266. ulong board_spl_fit_size_align(ulong size)
  267. {
  268. /*
  269. * HAB authenticate_image requests the IVT offset is
  270. * aligned to 0x1000
  271. */
  272. size = ALIGN(size, 0x1000);
  273. size += CONFIG_CSF_SIZE;
  274. return size;
  275. }
  276. void board_spl_fit_post_load(ulong load_addr, size_t length)
  277. {
  278. u32 offset = length - CONFIG_CSF_SIZE;
  279. if (imx_hab_authenticate_image(load_addr,
  280. offset + IVT_SIZE + CSF_PAD_SIZE,
  281. offset)) {
  282. puts("spl: ERROR: image authentication unsuccessful\n");
  283. hang();
  284. }
  285. }
  286. #endif
  287. #endif
  288. #if defined(CONFIG_MX6) && defined(CONFIG_SPL_OS_BOOT)
  289. int dram_init_banksize(void)
  290. {
  291. gd->bd->bi_dram[0].start = CONFIG_SYS_SDRAM_BASE;
  292. gd->bd->bi_dram[0].size = imx_ddr_size();
  293. return 0;
  294. }
  295. #endif