spl.c 9.3 KB

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