xref: /arm-trusted-firmware/plat/st/stm32mp2/aarch64/stm32mp2_helper.S (revision 91f16700b400a8c0651d24a598fc48ee2997a0d7)
1*91f16700Schasinglulu/*
2*91f16700Schasinglulu * Copyright (c) 2023, STMicroelectronics - All Rights Reserved
3*91f16700Schasinglulu *
4*91f16700Schasinglulu * SPDX-License-Identifier: BSD-3-Clause
5*91f16700Schasinglulu */
6*91f16700Schasinglulu
7*91f16700Schasinglulu#include <asm_macros.S>
8*91f16700Schasinglulu#include <drivers/st/stm32_gpio.h>
9*91f16700Schasinglulu
10*91f16700Schasinglulu#include <platform_def.h>
11*91f16700Schasinglulu
12*91f16700Schasinglulu#define GPIO_TX_SHIFT		(DEBUG_UART_TX_GPIO_PORT << 1)
13*91f16700Schasinglulu
14*91f16700Schasinglulu	.globl	platform_mem_init
15*91f16700Schasinglulu	.globl	plat_secondary_cold_boot_setup
16*91f16700Schasinglulu	.globl	plat_is_my_cpu_primary
17*91f16700Schasinglulu	.globl	plat_crash_console_init
18*91f16700Schasinglulu	.globl	plat_crash_console_flush
19*91f16700Schasinglulu	.globl	plat_crash_console_putc
20*91f16700Schasinglulu	.globl	plat_report_exception
21*91f16700Schasinglulu
22*91f16700Schasinglulufunc platform_mem_init
23*91f16700Schasinglulu	/* Nothing to do, don't need to init SYSRAM */
24*91f16700Schasinglulu	ret
25*91f16700Schasingluluendfunc platform_mem_init
26*91f16700Schasinglulu
27*91f16700Schasinglulu	/* ---------------------------------------------
28*91f16700Schasinglulu	 * void plat_secondary_cold_boot_setup (void);
29*91f16700Schasinglulu	 *
30*91f16700Schasinglulu	 * Set secondary core in WFI waiting for core reset.
31*91f16700Schasinglulu	 * ---------------------------------------------
32*91f16700Schasinglulu	 */
33*91f16700Schasinglulufunc plat_secondary_cold_boot_setup
34*91f16700Schasinglulu	dsb	sy
35*91f16700Schasinglulu	wfi
36*91f16700Schasinglulu	/* This shouldn't be reached */
37*91f16700Schasinglulu	b	.
38*91f16700Schasingluluendfunc plat_secondary_cold_boot_setup
39*91f16700Schasinglulu
40*91f16700Schasinglulu	/* ----------------------------------------------
41*91f16700Schasinglulu	 * unsigned int plat_is_my_cpu_primary(void);
42*91f16700Schasinglulu	 * This function checks if this is the primary CPU
43*91f16700Schasinglulu	 * ----------------------------------------------
44*91f16700Schasinglulu	 */
45*91f16700Schasinglulufunc plat_is_my_cpu_primary
46*91f16700Schasinglulu	mrs	x0, mpidr_el1
47*91f16700Schasinglulu	and	x0, x0, #(MPIDR_CPU_MASK)
48*91f16700Schasinglulu	cmp	x0, #STM32MP_PRIMARY_CPU
49*91f16700Schasinglulu	cset	x0, eq
50*91f16700Schasinglulu	ret
51*91f16700Schasingluluendfunc plat_is_my_cpu_primary
52*91f16700Schasinglulu
53*91f16700Schasinglulu	/* ---------------------------------------------
54*91f16700Schasinglulu	 * int plat_crash_console_init(void)
55*91f16700Schasinglulu	 *
56*91f16700Schasinglulu	 * Initialize the crash console without a C Runtime stack.
57*91f16700Schasinglulu	 * ---------------------------------------------
58*91f16700Schasinglulu	 */
59*91f16700Schasinglulufunc plat_crash_console_init
60*91f16700Schasinglulu	/* Reset UART peripheral */
61*91f16700Schasinglulu	mov_imm	x1, (RCC_BASE + DEBUG_UART_RST_REG)
62*91f16700Schasinglulu	ldr	x2, =DEBUG_UART_RST_BIT
63*91f16700Schasinglulu	ldr	x0, [x1]
64*91f16700Schasinglulu	orr	x0, x0, x2
65*91f16700Schasinglulu	str	x0, [x1]
66*91f16700Schasinglulu1:
67*91f16700Schasinglulu	ldr	x0, [x1]
68*91f16700Schasinglulu	ands	x2, x0, x2
69*91f16700Schasinglulu	beq	1b
70*91f16700Schasinglulu	bic	x2, x2, #DEBUG_UART_RST_BIT
71*91f16700Schasinglulu	str	x2, [x1]
72*91f16700Schasinglulu2:
73*91f16700Schasinglulu	ldr	x0, [x1]
74*91f16700Schasinglulu	ands	x2, x0, x2
75*91f16700Schasinglulu	bne	2b
76*91f16700Schasinglulu	/* Enable GPIOs for UART TX */
77*91f16700Schasinglulu	mov_imm	x1, (RCC_BASE + DEBUG_UART_TX_GPIO_BANK_CLK_REG)
78*91f16700Schasinglulu	ldr	w2, [x1]
79*91f16700Schasinglulu	/* Configure GPIO */
80*91f16700Schasinglulu	orr	w2, w2, #DEBUG_UART_TX_GPIO_BANK_CLK_EN
81*91f16700Schasinglulu	str	w2, [x1]
82*91f16700Schasinglulu	mov_imm	x1, DEBUG_UART_TX_GPIO_BANK_ADDRESS
83*91f16700Schasinglulu	/* Set GPIO mode alternate */
84*91f16700Schasinglulu	ldr	w2, [x1, #GPIO_MODE_OFFSET]
85*91f16700Schasinglulu	bic	w2, w2, #(GPIO_MODE_MASK << GPIO_TX_SHIFT)
86*91f16700Schasinglulu	orr	w2, w2, #(GPIO_MODE_ALTERNATE << GPIO_TX_SHIFT)
87*91f16700Schasinglulu	str	w2, [x1, #GPIO_MODE_OFFSET]
88*91f16700Schasinglulu	/* Set GPIO speed low */
89*91f16700Schasinglulu	ldr	w2, [x1, #GPIO_SPEED_OFFSET]
90*91f16700Schasinglulu	bic	w2, w2, #(GPIO_SPEED_MASK << GPIO_TX_SHIFT)
91*91f16700Schasinglulu	str	w2, [x1, #GPIO_SPEED_OFFSET]
92*91f16700Schasinglulu	/* Set no-pull */
93*91f16700Schasinglulu	ldr	w2, [x1, #GPIO_PUPD_OFFSET]
94*91f16700Schasinglulu	bic	w2, w2, #(GPIO_PULL_MASK << GPIO_TX_SHIFT)
95*91f16700Schasinglulu	str	w2, [x1, #GPIO_PUPD_OFFSET]
96*91f16700Schasinglulu	/* Set alternate */
97*91f16700Schasinglulu#if DEBUG_UART_TX_GPIO_PORT >= GPIO_ALT_LOWER_LIMIT
98*91f16700Schasinglulu	ldr	w2, [x1, #GPIO_AFRH_OFFSET]
99*91f16700Schasinglulu	bic	w2, w2, #(GPIO_ALTERNATE_MASK << \
100*91f16700Schasinglulu				((DEBUG_UART_TX_GPIO_PORT - GPIO_ALT_LOWER_LIMIT) << 2))
101*91f16700Schasinglulu	orr	w2, w2, #(DEBUG_UART_TX_GPIO_ALTERNATE << \
102*91f16700Schasinglulu				((DEBUG_UART_TX_GPIO_PORT - GPIO_ALT_LOWER_LIMIT) << 2))
103*91f16700Schasinglulu	str	w2, [x1, #GPIO_AFRH_OFFSET]
104*91f16700Schasinglulu#else
105*91f16700Schasinglulu	ldr	w2, [x1, #GPIO_AFRL_OFFSET]
106*91f16700Schasinglulu	bic	w2, w2, #(GPIO_ALTERNATE_MASK << (DEBUG_UART_TX_GPIO_PORT << 2))
107*91f16700Schasinglulu	orr	w2, w2, #(DEBUG_UART_TX_GPIO_ALTERNATE << (DEBUG_UART_TX_GPIO_PORT << 2))
108*91f16700Schasinglulu	str	w2, [x1, #GPIO_AFRL_OFFSET]
109*91f16700Schasinglulu#endif
110*91f16700Schasinglulu	/* Clear UART clock flexgen divisors, keep enable bit */
111*91f16700Schasinglulu	mov_imm	x1, (RCC_BASE + DEBUG_UART_PREDIV_CFGR)
112*91f16700Schasinglulu	mov	x2, #0
113*91f16700Schasinglulu	str	w2, [x1]
114*91f16700Schasinglulu	mov_imm	x1, (RCC_BASE + DEBUG_UART_FINDIV_CFGR)
115*91f16700Schasinglulu	mov	x2, #0x40
116*91f16700Schasinglulu	str	w2, [x1]
117*91f16700Schasinglulu	/* Enable UART clock, with its source */
118*91f16700Schasinglulu	mov_imm	x1, (RCC_BASE + DEBUG_UART_TX_CLKSRC_REG)
119*91f16700Schasinglulu	mov_imm	w2, (DEBUG_UART_TX_CLKSRC | RCC_XBARxCFGR_XBARxEN)
120*91f16700Schasinglulu	str	w2, [x1]
121*91f16700Schasinglulu	mov_imm	x1, (RCC_BASE + DEBUG_UART_TX_EN_REG)
122*91f16700Schasinglulu	ldr	w2, [x1]
123*91f16700Schasinglulu	orr	w2, w2, #DEBUG_UART_TX_EN
124*91f16700Schasinglulu	str	w2, [x1]
125*91f16700Schasinglulu
126*91f16700Schasinglulu	mov_imm	x0, STM32MP_DEBUG_USART_BASE
127*91f16700Schasinglulu	mov_imm	x1, STM32MP_DEBUG_USART_CLK_FRQ
128*91f16700Schasinglulu	mov_imm	x2, STM32MP_UART_BAUDRATE
129*91f16700Schasinglulu	b	console_stm32_core_init
130*91f16700Schasingluluendfunc plat_crash_console_init
131*91f16700Schasinglulu
132*91f16700Schasinglulufunc plat_crash_console_flush
133*91f16700Schasinglulu	mov_imm	x0, STM32MP_DEBUG_USART_BASE
134*91f16700Schasinglulu	b	console_stm32_core_flush
135*91f16700Schasingluluendfunc plat_crash_console_flush
136*91f16700Schasinglulu
137*91f16700Schasinglulufunc plat_crash_console_putc
138*91f16700Schasinglulu	mov_imm	x1, STM32MP_DEBUG_USART_BASE
139*91f16700Schasinglulu	cmp	x0, #'\n'
140*91f16700Schasinglulu	b.ne	1f
141*91f16700Schasinglulu	mov	x15, x30
142*91f16700Schasinglulu	mov	x0, #'\r'
143*91f16700Schasinglulu	bl	console_stm32_core_putc
144*91f16700Schasinglulu	mov	x30, x15
145*91f16700Schasinglulu	mov	x0, #'\n'
146*91f16700Schasinglulu1:
147*91f16700Schasinglulu	b	console_stm32_core_putc
148*91f16700Schasingluluendfunc plat_crash_console_putc
149*91f16700Schasinglulu
150*91f16700Schasinglulu#ifdef IMAGE_BL2
151*91f16700Schasinglulu	/* ---------------------------------------------
152*91f16700Schasinglulu	 * void plat_report_exception(unsigned int type)
153*91f16700Schasinglulu	 * Function to report an unhandled exception
154*91f16700Schasinglulu	 * with platform-specific means.
155*91f16700Schasinglulu	 * ---------------------------------------------
156*91f16700Schasinglulu	 */
157*91f16700Schasinglulufunc plat_report_exception
158*91f16700Schasinglulu	mov	x8, x30
159*91f16700Schasinglulu
160*91f16700Schasinglulu	adr	x4, plat_err_str
161*91f16700Schasinglulu	bl	asm_print_str
162*91f16700Schasinglulu
163*91f16700Schasinglulu	adr	x4, esr_el3_str
164*91f16700Schasinglulu	bl	asm_print_str
165*91f16700Schasinglulu
166*91f16700Schasinglulu	mrs	x4, esr_el3
167*91f16700Schasinglulu	bl	asm_print_hex
168*91f16700Schasinglulu
169*91f16700Schasinglulu	adr	x4, elr_el3_str
170*91f16700Schasinglulu	bl	asm_print_str
171*91f16700Schasinglulu
172*91f16700Schasinglulu	mrs	x4, elr_el3
173*91f16700Schasinglulu	bl	asm_print_hex
174*91f16700Schasinglulu
175*91f16700Schasinglulu	adr	x4, far_el3_str
176*91f16700Schasinglulu	bl	asm_print_str
177*91f16700Schasinglulu
178*91f16700Schasinglulu	mrs	x4, far_el3
179*91f16700Schasinglulu	bl	asm_print_hex
180*91f16700Schasinglulu
181*91f16700Schasinglulu	mov	x30, x8
182*91f16700Schasinglulu	ret
183*91f16700Schasingluluendfunc plat_report_exception
184*91f16700Schasinglulu
185*91f16700Schasinglulu.section .rodata.rev_err_str, "aS"
186*91f16700Schasingluluplat_err_str:
187*91f16700Schasinglulu	.asciz "\nPlatform exception reporting:"
188*91f16700Schasingluluesr_el3_str:
189*91f16700Schasinglulu	.asciz "\nESR_EL3: "
190*91f16700Schasingluluelr_el3_str:
191*91f16700Schasinglulu	.asciz "\nELR_EL3: "
192*91f16700Schasinglulufar_el3_str:
193*91f16700Schasinglulu	.asciz "\nFAR_EL3: "
194*91f16700Schasinglulu#endif /* IMAGE_BL2 */
195