spl.c 9.3 KB

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