1 /*
2  * Copyright (c) 2023, Pengutronix. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include <lib/mmio.h>
8 #include <platform_def.h>
9 
10 #define UCR1    		0x80
11 #define UCR1_UARTEN		BIT(0)
12 #define DOMAIN0_RUNNING(d)	(((d) & 0x3) != 0)
13 
14 static struct imx_uart {
15 	unsigned int ccm_reg;
16 	unsigned int uart_base;
17 } imx8m_uart_info[] = {
18 	{	/* UART 1 */
19 		.ccm_reg = 0x4490,
20 		.uart_base = 0x30860000,
21 	}, {	/* UART 2 */
22 		.ccm_reg = 0x44a0,
23 		.uart_base = 0x30890000,
24 	}, {	/* UART 3 */
25 		.ccm_reg = 0x44b0,
26 		.uart_base = 0x30880000,
27 	}, {	/* UART 4 */
28 		.ccm_reg = 0x44c0,
29 		.uart_base = 0x30a60000,
30 	}
31 };
32 
imx8m_uart_get_base(void)33 unsigned int imx8m_uart_get_base(void)
34 {
35 	unsigned int i;
36 
37 	for (i = 0; i < ARRAY_SIZE(imx8m_uart_info); i++) {
38 		uint32_t val;
39 
40 		/*
41 		 * At least check that the clock-gate is ungated before we
42 		 * access the UART register.
43 		 */
44 		val = mmio_read_32(IMX_CCM_BASE + imx8m_uart_info[i].ccm_reg);
45 		if (DOMAIN0_RUNNING(val)) {
46 			val = mmio_read_32(imx8m_uart_info[i].uart_base + UCR1);
47 			if (val & UCR1_UARTEN) {
48 				return imx8m_uart_info[i].uart_base;
49 			}
50 		}
51 	}
52 
53 	/*
54 	 * We should return an error and inform the user but we can't do it
55 	 * this early.
56 	 */
57 	return 0;
58 }
59