imx8qm_bl31_setup.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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 <stdint.h>
  9. #include <stdbool.h>
  10. #include <platform_def.h>
  11. #include <arch_helpers.h>
  12. #include <context.h>
  13. #include <common/bl_common.h>
  14. #include <common/debug.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 <imx8qm_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. #define UART_PAD_CTRL (PADRING_IFMUX_EN_MASK | PADRING_GP_EN_MASK | \
  36. (SC_PAD_CONFIG_OUT_IN << PADRING_CONFIG_SHIFT) | \
  37. (SC_PAD_ISO_OFF << PADRING_LPCONFIG_SHIFT) | \
  38. (SC_PAD_28FDSOI_DSE_DV_LOW << PADRING_DSE_SHIFT) | \
  39. (SC_PAD_28FDSOI_PS_PD << PADRING_PULL_SHIFT))
  40. #if defined(IMX_USE_UART0)
  41. #define IMX_RES_UART SC_R_UART_0
  42. #define IMX_PAD_UART_RX SC_P_UART0_RX
  43. #define IMX_PAD_UART_TX SC_P_UART0_TX
  44. #define IMX_PAD_UART_RTS_B SC_P_UART0_RTS_B
  45. #define IMX_PAD_UART_CTS_B SC_P_UART0_CTS_B
  46. #elif defined(IMX_USE_UART1)
  47. #define IMX_RES_UART SC_R_UART_1
  48. #define IMX_PAD_UART_RX SC_P_UART1_RX
  49. #define IMX_PAD_UART_TX SC_P_UART1_TX
  50. #define IMX_PAD_UART_RTS_B SC_P_UART1_RTS_B
  51. #define IMX_PAD_UART_CTS_B SC_P_UART1_CTS_B
  52. #else
  53. #error "Provide proper UART number in IMX_DEBUG_UART"
  54. #endif
  55. static const int imx8qm_cci_map[] = {
  56. CLUSTER0_CCI_SLVAE_IFACE,
  57. CLUSTER1_CCI_SLVAE_IFACE
  58. };
  59. static const mmap_region_t imx_mmap[] = {
  60. MAP_REGION_FLAT(IMX_REG_BASE, IMX_REG_SIZE, MT_DEVICE | MT_RW),
  61. {0}
  62. };
  63. static uint32_t get_spsr_for_bl33_entry(void)
  64. {
  65. unsigned long el_status;
  66. unsigned long mode;
  67. uint32_t spsr;
  68. /* figure out what mode we enter the non-secure world */
  69. el_status = read_id_aa64pfr0_el1() >> ID_AA64PFR0_EL2_SHIFT;
  70. el_status &= ID_AA64PFR0_ELX_MASK;
  71. mode = (el_status) ? MODE_EL2 : MODE_EL1;
  72. spsr = SPSR_64(mode, MODE_SP_ELX, DISABLE_ALL_EXCEPTIONS);
  73. return spsr;
  74. }
  75. #if DEBUG_CONSOLE_A53
  76. static void lpuart32_serial_setbrg(unsigned int base, int baudrate)
  77. {
  78. unsigned int sbr, osr, baud_diff, tmp_osr, tmp_sbr;
  79. unsigned int diff1, diff2, tmp, rate;
  80. if (baudrate == 0)
  81. panic();
  82. sc_pm_get_clock_rate(ipc_handle, IMX_RES_UART, 2, &rate);
  83. baud_diff = baudrate;
  84. osr = 0;
  85. sbr = 0;
  86. for (tmp_osr = 4; tmp_osr <= 32; tmp_osr++) {
  87. tmp_sbr = (rate / (baudrate * tmp_osr));
  88. if (tmp_sbr == 0)
  89. tmp_sbr = 1;
  90. /* calculate difference in actual baud w/ current values */
  91. diff1 = rate / (tmp_osr * tmp_sbr) - baudrate;
  92. diff2 = rate / (tmp_osr * (tmp_sbr + 1));
  93. /* select best values between sbr and sbr+1 */
  94. if (diff1 > (baudrate - diff2)) {
  95. diff1 = baudrate - diff2;
  96. tmp_sbr++;
  97. }
  98. if (diff1 <= baud_diff) {
  99. baud_diff = diff1;
  100. osr = tmp_osr;
  101. sbr = tmp_sbr;
  102. }
  103. }
  104. tmp = mmio_read_32(IMX_BOOT_UART_BASE + BAUD);
  105. if ((osr > 3) && (osr < 8))
  106. tmp |= LPUART_BAUD_BOTHEDGE_MASK;
  107. tmp &= ~LPUART_BAUD_OSR_MASK;
  108. tmp |= LPUART_BAUD_OSR(osr - 1);
  109. tmp &= ~LPUART_BAUD_SBR_MASK;
  110. tmp |= LPUART_BAUD_SBR(sbr);
  111. /* explicitly disable 10 bit mode & set 1 stop bit */
  112. tmp &= ~(LPUART_BAUD_M10_MASK | LPUART_BAUD_SBNS_MASK);
  113. mmio_write_32(IMX_BOOT_UART_BASE + BAUD, tmp);
  114. }
  115. static int lpuart32_serial_init(unsigned int base)
  116. {
  117. unsigned int tmp;
  118. /* disable TX & RX before enabling clocks */
  119. tmp = mmio_read_32(IMX_BOOT_UART_BASE + CTRL);
  120. tmp &= ~(CTRL_TE | CTRL_RE);
  121. mmio_write_32(IMX_BOOT_UART_BASE + CTRL, tmp);
  122. mmio_write_32(IMX_BOOT_UART_BASE + MODIR, 0);
  123. mmio_write_32(IMX_BOOT_UART_BASE + FIFO, ~(FIFO_TXFE | FIFO_RXFE));
  124. mmio_write_32(IMX_BOOT_UART_BASE + MATCH, 0);
  125. /* provide data bits, parity, stop bit, etc */
  126. lpuart32_serial_setbrg(base, IMX_BOOT_UART_BAUDRATE);
  127. /* eight data bits no parity bit */
  128. tmp = mmio_read_32(IMX_BOOT_UART_BASE + CTRL);
  129. tmp &= ~(LPUART_CTRL_PE_MASK | LPUART_CTRL_PT_MASK | LPUART_CTRL_M_MASK);
  130. mmio_write_32(IMX_BOOT_UART_BASE + CTRL, tmp);
  131. mmio_write_32(IMX_BOOT_UART_BASE + CTRL, CTRL_RE | CTRL_TE);
  132. mmio_write_32(IMX_BOOT_UART_BASE + DATA, 0x55);
  133. mmio_write_32(IMX_BOOT_UART_BASE + DATA, 0x55);
  134. mmio_write_32(IMX_BOOT_UART_BASE + DATA, 0x0A);
  135. return 0;
  136. }
  137. #endif
  138. void mx8_partition_resources(void)
  139. {
  140. sc_rm_pt_t secure_part, os_part;
  141. sc_rm_mr_t mr, mr_record = 64;
  142. sc_faddr_t start, end;
  143. bool owned, owned2;
  144. sc_err_t err;
  145. int i;
  146. err = sc_rm_get_partition(ipc_handle, &secure_part);
  147. err = sc_rm_partition_alloc(ipc_handle, &os_part, false, false,
  148. false, false, false);
  149. err = sc_rm_set_parent(ipc_handle, os_part, secure_part);
  150. /* set secure resources to NOT-movable */
  151. for (i = 0; i < ARRAY_SIZE(secure_rsrcs); i++) {
  152. err = sc_rm_set_resource_movable(ipc_handle, secure_rsrcs[i],
  153. secure_rsrcs[i], false);
  154. if (err)
  155. ERROR("sc_rm_set_resource_movable: rsrc %u, ret %u\n",
  156. secure_rsrcs[i], err);
  157. }
  158. owned = sc_rm_is_resource_owned(ipc_handle, SC_R_M4_0_PID0);
  159. if (owned) {
  160. err = sc_rm_set_resource_movable(ipc_handle, SC_R_M4_0_PID0,
  161. SC_R_M4_0_PID0, false);
  162. if (err)
  163. ERROR("sc_rm_set_resource_movable: rsrc %u, ret %u\n",
  164. SC_R_M4_0_PID0, err);
  165. }
  166. owned2 = sc_rm_is_resource_owned(ipc_handle, SC_R_M4_1_PID0);
  167. if (owned2) {
  168. err = sc_rm_set_resource_movable(ipc_handle, SC_R_M4_1_PID0,
  169. SC_R_M4_1_PID0, false);
  170. if (err)
  171. ERROR("sc_rm_set_resource_movable: rsrc %u, ret %u\n",
  172. SC_R_M4_1_PID0, err);
  173. }
  174. /* move all movable resources and pins to non-secure partition */
  175. err = sc_rm_move_all(ipc_handle, secure_part, os_part, true, true);
  176. if (err)
  177. ERROR("sc_rm_move_all: %u\n", err);
  178. /* iterate through peripherals to give NS OS part access */
  179. for (i = 0; i < ARRAY_SIZE(ns_access_allowed); i++) {
  180. err = sc_rm_set_peripheral_permissions(ipc_handle, ns_access_allowed[i],
  181. os_part, SC_RM_PERM_FULL);
  182. if (err)
  183. ERROR("sc_rm_set_peripheral_permissions: rsrc %u, \
  184. ret %u\n", ns_access_allowed[i], err);
  185. }
  186. if (owned) {
  187. err = sc_rm_set_resource_movable(ipc_handle, SC_R_M4_0_PID0,
  188. SC_R_M4_0_PID0, true);
  189. if (err)
  190. ERROR("sc_rm_set_resource_movable: rsrc %u, ret %u\n",
  191. SC_R_M4_0_PID0, err);
  192. err = sc_rm_assign_resource(ipc_handle, os_part, SC_R_M4_0_PID0);
  193. if (err)
  194. ERROR("sc_rm_assign_resource: rsrc %u, ret %u\n",
  195. SC_R_M4_0_PID0, err);
  196. }
  197. if (owned2) {
  198. err = sc_rm_set_resource_movable(ipc_handle, SC_R_M4_1_PID0,
  199. SC_R_M4_1_PID0, true);
  200. if (err)
  201. ERROR("sc_rm_set_resource_movable: rsrc %u, ret %u\n",
  202. SC_R_M4_1_PID0, err);
  203. err = sc_rm_assign_resource(ipc_handle, os_part, SC_R_M4_1_PID0);
  204. if (err)
  205. ERROR("sc_rm_assign_resource: rsrc %u, ret %u\n",
  206. SC_R_M4_1_PID0, err);
  207. }
  208. /*
  209. * sc_rm_set_peripheral_permissions
  210. * sc_rm_set_memreg_permissions
  211. * sc_rm_set_pin_movable
  212. */
  213. for (mr = 0; mr < 64; mr++) {
  214. owned = sc_rm_is_memreg_owned(ipc_handle, mr);
  215. if (owned) {
  216. err = sc_rm_get_memreg_info(ipc_handle, mr, &start, &end);
  217. if (err)
  218. ERROR("Memreg get info failed, %u\n", mr);
  219. NOTICE("Memreg %u 0x%" PRIx64 " -- 0x%" PRIx64 "\n", mr, start, end);
  220. if (BL31_BASE >= start && (BL31_LIMIT - 1) <= end) {
  221. mr_record = mr; /* Record the mr for ATF running */
  222. } else {
  223. err = sc_rm_assign_memreg(ipc_handle, os_part, mr);
  224. if (err)
  225. ERROR("Memreg assign failed, 0x%" PRIx64 " -- 0x%" PRIx64 ", \
  226. err %d\n", start, end, err);
  227. }
  228. }
  229. }
  230. if (mr_record != 64) {
  231. err = sc_rm_get_memreg_info(ipc_handle, mr_record, &start, &end);
  232. if (err)
  233. ERROR("Memreg get info failed, %u\n", mr_record);
  234. if ((BL31_LIMIT - 1) < end) {
  235. err = sc_rm_memreg_alloc(ipc_handle, &mr, BL31_LIMIT, end);
  236. if (err)
  237. ERROR("sc_rm_memreg_alloc failed, 0x%" PRIx64 " -- 0x%" PRIx64 "\n",
  238. (sc_faddr_t)BL31_LIMIT, end);
  239. err = sc_rm_assign_memreg(ipc_handle, os_part, mr);
  240. if (err)
  241. ERROR("Memreg assign failed, 0x%" PRIx64 " -- 0x%" PRIx64 "\n",
  242. (sc_faddr_t)BL31_LIMIT, end);
  243. }
  244. if (start < (BL31_BASE - 1)) {
  245. err = sc_rm_memreg_alloc(ipc_handle, &mr, start, BL31_BASE - 1);
  246. if (err)
  247. ERROR("sc_rm_memreg_alloc failed, 0x%" PRIx64 " -- 0x%" PRIx64 "\n",
  248. start, (sc_faddr_t)BL31_BASE - 1);
  249. err = sc_rm_assign_memreg(ipc_handle, os_part, mr);
  250. if (err)
  251. ERROR("Memreg assign failed, 0x%" PRIx64 " -- 0x%" PRIx64 "\n",
  252. start, (sc_faddr_t)BL31_BASE - 1);
  253. }
  254. }
  255. if (err)
  256. NOTICE("Partitioning Failed\n");
  257. else
  258. NOTICE("Non-secure Partitioning Succeeded\n");
  259. }
  260. void bl31_early_platform_setup2(u_register_t arg0, u_register_t arg1,
  261. u_register_t arg2, u_register_t arg3)
  262. {
  263. #if DEBUG_CONSOLE
  264. static console_t console;
  265. #endif
  266. if (sc_ipc_open(&ipc_handle, SC_IPC_BASE) != SC_ERR_NONE)
  267. panic();
  268. #if DEBUG_CONSOLE_A53
  269. sc_pm_set_resource_power_mode(ipc_handle, IMX_RES_UART,
  270. SC_PM_PW_MODE_ON);
  271. sc_pm_clock_rate_t rate = 80000000;
  272. sc_pm_set_clock_rate(ipc_handle, IMX_RES_UART, 2, &rate);
  273. sc_pm_clock_enable(ipc_handle, IMX_RES_UART, 2, true, false);
  274. /* configure UART pads */
  275. sc_pad_set(ipc_handle, IMX_PAD_UART_RX, UART_PAD_CTRL);
  276. sc_pad_set(ipc_handle, IMX_PAD_UART_TX, UART_PAD_CTRL);
  277. sc_pad_set(ipc_handle, IMX_PAD_UART_RTS_B, UART_PAD_CTRL);
  278. sc_pad_set(ipc_handle, IMX_PAD_UART_CTS_B, UART_PAD_CTRL);
  279. lpuart32_serial_init(IMX_BOOT_UART_BASE);
  280. #endif
  281. #if DEBUG_CONSOLE
  282. console_lpuart_register(IMX_BOOT_UART_BASE, IMX_BOOT_UART_CLK_IN_HZ,
  283. IMX_CONSOLE_BAUDRATE, &console);
  284. #endif
  285. /* turn on MU1 for non-secure OS/Hypervisor */
  286. sc_pm_set_resource_power_mode(ipc_handle, SC_R_MU_1A, SC_PM_PW_MODE_ON);
  287. /* Turn on GPT_0's power & clock for non-secure OS/Hypervisor */
  288. sc_pm_set_resource_power_mode(ipc_handle, SC_R_GPT_0, SC_PM_PW_MODE_ON);
  289. sc_pm_clock_enable(ipc_handle, SC_R_GPT_0, SC_PM_CLK_PER, true, 0);
  290. mmio_write_32(IMX_GPT_LPCG_BASE, mmio_read_32(IMX_GPT_LPCG_BASE) | (1 << 25));
  291. /*
  292. * create new partition for non-secure OS/Hypervisor
  293. * uses global structs defined in sec_rsrc.h
  294. */
  295. mx8_partition_resources();
  296. bl33_image_ep_info.pc = PLAT_NS_IMAGE_OFFSET;
  297. bl33_image_ep_info.spsr = get_spsr_for_bl33_entry();
  298. SET_SECURITY_STATE(bl33_image_ep_info.h.attr, NON_SECURE);
  299. /* init the first cluster's cci slave interface */
  300. cci_init(PLAT_CCI_BASE, imx8qm_cci_map, PLATFORM_CLUSTER_COUNT);
  301. cci_enable_snoop_dvm_reqs(MPIDR_AFFLVL1_VAL(read_mpidr_el1()));
  302. }
  303. void bl31_plat_arch_setup(void)
  304. {
  305. unsigned long ro_start = BL31_RO_START;
  306. unsigned long ro_size = BL31_RO_END - BL31_RO_START;
  307. unsigned long rw_start = BL31_RW_START;
  308. unsigned long rw_size = BL31_RW_END - BL31_RW_START;
  309. #if USE_COHERENT_MEM
  310. unsigned long coh_start = BL31_COHERENT_RAM_START;
  311. unsigned long coh_size = BL31_COHERENT_RAM_END - BL31_COHERENT_RAM_START;
  312. #endif
  313. mmap_add_region(ro_start, ro_start, ro_size,
  314. MT_RO | MT_MEMORY | MT_SECURE);
  315. mmap_add_region(rw_start, rw_start, rw_size,
  316. MT_RW | MT_MEMORY | MT_SECURE);
  317. mmap_add(imx_mmap);
  318. #if USE_COHERENT_MEM
  319. mmap_add_region(coh_start, coh_start, coh_size,
  320. MT_DEVICE | MT_RW | MT_SECURE);
  321. #endif
  322. /* setup xlat table */
  323. init_xlat_tables();
  324. /* enable the MMU */
  325. enable_mmu_el3(0);
  326. }
  327. void bl31_platform_setup(void)
  328. {
  329. plat_gic_driver_init();
  330. plat_gic_init();
  331. }
  332. entry_point_info_t *bl31_plat_get_next_image_ep_info(unsigned int type)
  333. {
  334. if (type == NON_SECURE)
  335. return &bl33_image_ep_info;
  336. if (type == SECURE)
  337. return &bl32_image_ep_info;
  338. return NULL;
  339. }
  340. unsigned int plat_get_syscnt_freq2(void)
  341. {
  342. return COUNTER_FREQUENCY;
  343. }