imx8qx_bl31_setup.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. /*
  2. * Copyright (c) 2015-2024, ARM Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <inttypes.h>
  8. #include <stdbool.h>
  9. #include <stdint.h>
  10. #include <platform_def.h>
  11. #include <arch_helpers.h>
  12. #include <common/bl_common.h>
  13. #include <common/debug.h>
  14. #include <context.h>
  15. #include <drivers/arm/cci.h>
  16. #include <drivers/console.h>
  17. #include <lib/el3_runtime/context_mgmt.h>
  18. #include <lib/mmio.h>
  19. #include <lib/xlat_tables/xlat_tables_v2.h>
  20. #include <plat/common/platform.h>
  21. #include <imx8qx_pads.h>
  22. #include <imx8_iomux.h>
  23. #include <imx8_lpuart.h>
  24. #include <plat_imx8.h>
  25. #include <sci/sci.h>
  26. #include <sec_rsrc.h>
  27. static const unsigned long BL31_COHERENT_RAM_START = BL_COHERENT_RAM_BASE;
  28. static const unsigned long BL31_COHERENT_RAM_END = BL_COHERENT_RAM_END;
  29. static const unsigned long BL31_RO_START = BL_CODE_BASE;
  30. static const unsigned long BL31_RO_END = BL_CODE_END;
  31. static const unsigned long BL31_RW_END = BL_END;
  32. IMPORT_SYM(unsigned long, __RW_START__, BL31_RW_START);
  33. static entry_point_info_t bl32_image_ep_info;
  34. static entry_point_info_t bl33_image_ep_info;
  35. /* Default configuration for i.MX8QM/QXP MEK */
  36. #if defined(IMX_USE_UART0)
  37. #define UART_PAD_CTRL (PADRING_IFMUX_EN_MASK | PADRING_GP_EN_MASK | \
  38. (SC_PAD_CONFIG_OUT_IN << PADRING_CONFIG_SHIFT) | \
  39. (SC_PAD_ISO_OFF << PADRING_LPCONFIG_SHIFT) | \
  40. (SC_PAD_28FDSOI_DSE_DV_LOW << PADRING_DSE_SHIFT) | \
  41. (SC_PAD_28FDSOI_PS_PD << PADRING_PULL_SHIFT))
  42. #define IMX_RES_UART SC_R_UART_0
  43. #define IMX_PAD_UART_RX SC_P_UART0_RX
  44. #define IMX_PAD_UART_TX SC_P_UART0_TX
  45. #elif defined(IMX_USE_UART1)
  46. #define UART_PAD_CTRL (PADRING_IFMUX_EN_MASK | PADRING_GP_EN_MASK | \
  47. (SC_PAD_CONFIG_OUT_IN << PADRING_CONFIG_SHIFT) | \
  48. (SC_PAD_ISO_OFF << PADRING_LPCONFIG_SHIFT) | \
  49. (SC_PAD_28FDSOI_DSE_DV_LOW << PADRING_DSE_SHIFT) | \
  50. (SC_PAD_28FDSOI_PS_PD << PADRING_PULL_SHIFT))
  51. #define IMX_RES_UART SC_R_UART_1
  52. #define IMX_PAD_UART_RX SC_P_UART1_RX
  53. #define IMX_PAD_UART_TX SC_P_UART1_TX
  54. /*
  55. * On Toradex Colibri i.MX8QXP UART3 on the FLEXCAN2.
  56. * Use custom pad control for this
  57. */
  58. #elif defined(IMX_USE_UART3)
  59. /*
  60. * FLEXCAN2_RX/TX pads are muxed to ADMA_UART3_RX/TX,
  61. * For ref:
  62. * 000b - ADMA_FLEXCAN2_RX
  63. * 001b - ADMA_SAI3_RXD
  64. * 010b - ADMA_UART3_RX
  65. * 011b - ADMA_SAI1_RXFS
  66. * 100b - LSIO_GPIO1_IO19
  67. */
  68. #define UART_PAD_CTRL (PADRING_IFMUX_EN_MASK | PADRING_GP_EN_MASK | \
  69. (SC_PAD_CONFIG_OUT_IN << PADRING_CONFIG_SHIFT) | \
  70. (2U << PADRING_IFMUX_SHIFT) | \
  71. (SC_PAD_ISO_OFF << PADRING_LPCONFIG_SHIFT) | \
  72. (SC_PAD_28FDSOI_DSE_DV_LOW << PADRING_DSE_SHIFT) | \
  73. (SC_PAD_28FDSOI_PS_PD << PADRING_PULL_SHIFT))
  74. #define IMX_RES_UART SC_R_UART_3
  75. #define IMX_PAD_UART_RX SC_P_FLEXCAN2_RX
  76. #define IMX_PAD_UART_TX SC_P_FLEXCAN2_TX
  77. #else
  78. #error "Provide proper UART configuration in IMX_DEBUG_UART"
  79. #endif
  80. static const mmap_region_t imx_mmap[] = {
  81. MAP_REGION_FLAT(IMX_REG_BASE, IMX_REG_SIZE, MT_DEVICE | MT_RW),
  82. {0}
  83. };
  84. static uint32_t get_spsr_for_bl33_entry(void)
  85. {
  86. unsigned long el_status;
  87. unsigned long mode;
  88. uint32_t spsr;
  89. /* figure out what mode we enter the non-secure world */
  90. el_status = read_id_aa64pfr0_el1() >> ID_AA64PFR0_EL2_SHIFT;
  91. el_status &= ID_AA64PFR0_ELX_MASK;
  92. mode = (el_status) ? MODE_EL2 : MODE_EL1;
  93. spsr = SPSR_64(mode, MODE_SP_ELX, DISABLE_ALL_EXCEPTIONS);
  94. return spsr;
  95. }
  96. #if DEBUG_CONSOLE_A35
  97. static void lpuart32_serial_setbrg(unsigned int base, int baudrate)
  98. {
  99. unsigned int sbr, osr, baud_diff, tmp_osr, tmp_sbr;
  100. unsigned int diff1, diff2, tmp, rate;
  101. if (baudrate == 0)
  102. panic();
  103. sc_pm_get_clock_rate(ipc_handle, IMX_RES_UART, 2, &rate);
  104. baud_diff = baudrate;
  105. osr = 0;
  106. sbr = 0;
  107. for (tmp_osr = 4; tmp_osr <= 32; tmp_osr++) {
  108. tmp_sbr = (rate / (baudrate * tmp_osr));
  109. if (tmp_sbr == 0)
  110. tmp_sbr = 1;
  111. /* calculate difference in actual baud w/ current values */
  112. diff1 = rate / (tmp_osr * tmp_sbr) - baudrate;
  113. diff2 = rate / (tmp_osr * (tmp_sbr + 1));
  114. /* select best values between sbr and sbr+1 */
  115. if (diff1 > (baudrate - diff2)) {
  116. diff1 = baudrate - diff2;
  117. tmp_sbr++;
  118. }
  119. if (diff1 <= baud_diff) {
  120. baud_diff = diff1;
  121. osr = tmp_osr;
  122. sbr = tmp_sbr;
  123. }
  124. }
  125. tmp = mmio_read_32(IMX_BOOT_UART_BASE + BAUD);
  126. if ((osr > 3) && (osr < 8))
  127. tmp |= LPUART_BAUD_BOTHEDGE_MASK;
  128. tmp &= ~LPUART_BAUD_OSR_MASK;
  129. tmp |= LPUART_BAUD_OSR(osr - 1);
  130. tmp &= ~LPUART_BAUD_SBR_MASK;
  131. tmp |= LPUART_BAUD_SBR(sbr);
  132. /* explicitly disable 10 bit mode & set 1 stop bit */
  133. tmp &= ~(LPUART_BAUD_M10_MASK | LPUART_BAUD_SBNS_MASK);
  134. mmio_write_32(IMX_BOOT_UART_BASE + BAUD, tmp);
  135. }
  136. static int lpuart32_serial_init(unsigned int base)
  137. {
  138. unsigned int tmp;
  139. /* disable TX & RX before enabling clocks */
  140. tmp = mmio_read_32(IMX_BOOT_UART_BASE + CTRL);
  141. tmp &= ~(CTRL_TE | CTRL_RE);
  142. mmio_write_32(IMX_BOOT_UART_BASE + CTRL, tmp);
  143. mmio_write_32(IMX_BOOT_UART_BASE + MODIR, 0);
  144. mmio_write_32(IMX_BOOT_UART_BASE + FIFO, ~(FIFO_TXFE | FIFO_RXFE));
  145. mmio_write_32(IMX_BOOT_UART_BASE + MATCH, 0);
  146. /* provide data bits, parity, stop bit, etc */
  147. lpuart32_serial_setbrg(base, IMX_BOOT_UART_BAUDRATE);
  148. /* eight data bits no parity bit */
  149. tmp = mmio_read_32(IMX_BOOT_UART_BASE + CTRL);
  150. tmp &= ~(LPUART_CTRL_PE_MASK | LPUART_CTRL_PT_MASK | LPUART_CTRL_M_MASK);
  151. mmio_write_32(IMX_BOOT_UART_BASE + CTRL, tmp);
  152. mmio_write_32(IMX_BOOT_UART_BASE + CTRL, CTRL_RE | CTRL_TE);
  153. mmio_write_32(IMX_BOOT_UART_BASE + DATA, 0x55);
  154. mmio_write_32(IMX_BOOT_UART_BASE + DATA, 0x55);
  155. mmio_write_32(IMX_BOOT_UART_BASE + DATA, 0x0A);
  156. return 0;
  157. }
  158. #endif
  159. void imx8_partition_resources(void)
  160. {
  161. sc_rm_pt_t secure_part, os_part;
  162. sc_rm_mr_t mr, mr_record = 64;
  163. sc_faddr_t start, end;
  164. sc_err_t err;
  165. bool owned;
  166. int i;
  167. err = sc_rm_get_partition(ipc_handle, &secure_part);
  168. if (err)
  169. ERROR("sc_rm_get_partition failed: %u\n", err);
  170. err = sc_rm_partition_alloc(ipc_handle, &os_part, false, false,
  171. false, false, false);
  172. if (err)
  173. ERROR("sc_rm_partition_alloc failed: %u\n", err);
  174. err = sc_rm_set_parent(ipc_handle, os_part, secure_part);
  175. if (err)
  176. ERROR("sc_rm_set_parent: %u\n", err);
  177. /* set secure resources to NOT-movable */
  178. for (i = 0; i < (ARRAY_SIZE(secure_rsrcs)); i++) {
  179. err = sc_rm_set_resource_movable(ipc_handle,
  180. secure_rsrcs[i], secure_rsrcs[i], false);
  181. if (err)
  182. ERROR("sc_rm_set_resource_movable: rsrc %u, ret %u\n",
  183. secure_rsrcs[i], err);
  184. }
  185. /* move all movable resources and pins to non-secure partition */
  186. err = sc_rm_move_all(ipc_handle, secure_part, os_part, true, true);
  187. if (err)
  188. ERROR("sc_rm_move_all: %u\n", err);
  189. /* iterate through peripherals to give NS OS part access */
  190. for (i = 0; i < ARRAY_SIZE(ns_access_allowed); i++) {
  191. err = sc_rm_set_peripheral_permissions(ipc_handle,
  192. ns_access_allowed[i], os_part, SC_RM_PERM_FULL);
  193. if (err)
  194. ERROR("sc_rm_set_peripheral_permissions: rsrc %u, \
  195. ret %u\n", ns_access_allowed[i], err);
  196. }
  197. /*
  198. * sc_rm_set_peripheral_permissions
  199. * sc_rm_set_memreg_permissions
  200. * sc_rm_set_pin_movable
  201. */
  202. for (mr = 0; mr < 64; mr++) {
  203. owned = sc_rm_is_memreg_owned(ipc_handle, mr);
  204. if (owned) {
  205. err = sc_rm_get_memreg_info(ipc_handle, mr, &start, &end);
  206. if (err)
  207. ERROR("Memreg get info failed, %u\n", mr);
  208. NOTICE("Memreg %u 0x%" PRIx64 " -- 0x%" PRIx64 "\n", mr, start, end);
  209. if (BL31_BASE >= start && (BL31_LIMIT - 1) <= end) {
  210. mr_record = mr; /* Record the mr for ATF running */
  211. } else {
  212. err = sc_rm_assign_memreg(ipc_handle, os_part, mr);
  213. if (err)
  214. ERROR("Memreg assign failed, 0x%" PRIx64 " -- 0x%" PRIx64 ", \
  215. err %d\n", start, end, err);
  216. }
  217. }
  218. }
  219. if (mr_record != 64) {
  220. err = sc_rm_get_memreg_info(ipc_handle, mr_record, &start, &end);
  221. if (err)
  222. ERROR("Memreg get info failed, %u\n", mr_record);
  223. if ((BL31_LIMIT - 1) < end) {
  224. err = sc_rm_memreg_alloc(ipc_handle, &mr, BL31_LIMIT, end);
  225. if (err)
  226. ERROR("sc_rm_memreg_alloc failed, 0x%" PRIx64 " -- 0x%" PRIx64 "\n",
  227. (sc_faddr_t)BL31_LIMIT, end);
  228. err = sc_rm_assign_memreg(ipc_handle, os_part, mr);
  229. if (err)
  230. ERROR("Memreg assign failed, 0x%" PRIx64 " -- 0x%" PRIx64 "\n",
  231. (sc_faddr_t)BL31_LIMIT, end);
  232. }
  233. if (start < (BL31_BASE - 1)) {
  234. err = sc_rm_memreg_alloc(ipc_handle, &mr, start, BL31_BASE - 1);
  235. if (err)
  236. ERROR("sc_rm_memreg_alloc failed, 0x%" PRIx64 " -- 0x%" PRIx64 "\n",
  237. start, (sc_faddr_t)BL31_BASE - 1);
  238. err = sc_rm_assign_memreg(ipc_handle, os_part, mr);
  239. if (err)
  240. ERROR("Memreg assign failed, 0x%" PRIx64 " -- 0x%" PRIx64 "\n",
  241. start, (sc_faddr_t)BL31_BASE - 1);
  242. }
  243. }
  244. if (err)
  245. NOTICE("Partitioning Failed\n");
  246. else
  247. NOTICE("Non-secure Partitioning Succeeded\n");
  248. }
  249. void bl31_early_platform_setup2(u_register_t arg0, u_register_t arg1,
  250. u_register_t arg2, u_register_t arg3)
  251. {
  252. #if DEBUG_CONSOLE
  253. static console_t console;
  254. #endif
  255. if (sc_ipc_open(&ipc_handle, SC_IPC_BASE) != SC_ERR_NONE)
  256. panic();
  257. #if DEBUG_CONSOLE_A35
  258. sc_pm_set_resource_power_mode(ipc_handle, IMX_RES_UART,
  259. SC_PM_PW_MODE_ON);
  260. sc_pm_clock_rate_t rate = 80000000;
  261. sc_pm_set_clock_rate(ipc_handle, IMX_RES_UART, 2, &rate);
  262. sc_pm_clock_enable(ipc_handle, IMX_RES_UART, 2, true, false);
  263. /* Configure UART pads */
  264. sc_pad_set(ipc_handle, IMX_PAD_UART_RX, UART_PAD_CTRL);
  265. sc_pad_set(ipc_handle, IMX_PAD_UART_TX, UART_PAD_CTRL);
  266. lpuart32_serial_init(IMX_BOOT_UART_BASE);
  267. #endif
  268. #if DEBUG_CONSOLE
  269. console_lpuart_register(IMX_BOOT_UART_BASE, IMX_BOOT_UART_CLK_IN_HZ,
  270. IMX_CONSOLE_BAUDRATE, &console);
  271. #endif
  272. /* Turn on MU1 for non-secure OS/Hypervisor */
  273. sc_pm_set_resource_power_mode(ipc_handle, SC_R_MU_1A, SC_PM_PW_MODE_ON);
  274. /* Turn on GPT_0's power & clock for non-secure OS/Hypervisor */
  275. sc_pm_set_resource_power_mode(ipc_handle, SC_R_GPT_0, SC_PM_PW_MODE_ON);
  276. sc_pm_clock_enable(ipc_handle, SC_R_GPT_0, SC_PM_CLK_PER, true, 0);
  277. mmio_write_32(IMX_GPT0_LPCG_BASE, mmio_read_32(IMX_GPT0_LPCG_BASE) | (1 << 25));
  278. /*
  279. * create new partition for non-secure OS/Hypervisor
  280. * uses global structs defined in sec_rsrc.h
  281. */
  282. imx8_partition_resources();
  283. bl33_image_ep_info.pc = PLAT_NS_IMAGE_OFFSET;
  284. bl33_image_ep_info.spsr = get_spsr_for_bl33_entry();
  285. SET_SECURITY_STATE(bl33_image_ep_info.h.attr, NON_SECURE);
  286. }
  287. void bl31_plat_arch_setup(void)
  288. {
  289. unsigned long ro_start = BL31_RO_START;
  290. unsigned long ro_size = BL31_RO_END - BL31_RO_START;
  291. unsigned long rw_start = BL31_RW_START;
  292. unsigned long rw_size = BL31_RW_END - BL31_RW_START;
  293. #if USE_COHERENT_MEM
  294. unsigned long coh_start = BL31_COHERENT_RAM_START;
  295. unsigned long coh_size = BL31_COHERENT_RAM_END - BL31_COHERENT_RAM_START;
  296. #endif
  297. mmap_add_region(ro_start, ro_start, ro_size,
  298. MT_RO | MT_MEMORY | MT_SECURE);
  299. mmap_add_region(rw_start, rw_start, rw_size,
  300. MT_RW | MT_MEMORY | MT_SECURE);
  301. mmap_add(imx_mmap);
  302. #if USE_COHERENT_MEM
  303. mmap_add_region(coh_start, coh_start, coh_size,
  304. MT_DEVICE | MT_RW | MT_SECURE);
  305. #endif
  306. init_xlat_tables();
  307. enable_mmu_el3(0);
  308. }
  309. void bl31_platform_setup(void)
  310. {
  311. plat_gic_driver_init();
  312. plat_gic_init();
  313. }
  314. entry_point_info_t *bl31_plat_get_next_image_ep_info(unsigned int type)
  315. {
  316. if (type == NON_SECURE)
  317. return &bl33_image_ep_info;
  318. if (type == SECURE)
  319. return &bl32_image_ep_info;
  320. return NULL;
  321. }
  322. unsigned int plat_get_syscnt_freq2(void)
  323. {
  324. return COUNTER_FREQUENCY;
  325. }