ARM: tegra: Guard clock code with a Kconfig symbol
[oweals/u-boot.git] / arch / arm / mach-tegra / board.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  *  (C) Copyright 2010-2015
4  *  NVIDIA Corporation <www.nvidia.com>
5  */
6
7 #include <common.h>
8 #include <dm.h>
9 #include <ns16550.h>
10 #include <spl.h>
11 #include <asm/io.h>
12 #if IS_ENABLED(CONFIG_TEGRA_CLKRST)
13 #include <asm/arch/clock.h>
14 #endif
15 #include <asm/arch/funcmux.h>
16 #include <asm/arch/mc.h>
17 #include <asm/arch/tegra.h>
18 #include <asm/arch-tegra/ap.h>
19 #include <asm/arch-tegra/board.h>
20 #include <asm/arch-tegra/pmc.h>
21 #include <asm/arch-tegra/sys_proto.h>
22 #include <asm/arch-tegra/warmboot.h>
23
24 void save_boot_params_ret(void);
25
26 DECLARE_GLOBAL_DATA_PTR;
27
28 enum {
29         /* UARTs which we can enable */
30         UARTA   = 1 << 0,
31         UARTB   = 1 << 1,
32         UARTC   = 1 << 2,
33         UARTD   = 1 << 3,
34         UARTE   = 1 << 4,
35         UART_COUNT = 5,
36 };
37
38 static bool from_spl __attribute__ ((section(".data")));
39
40 #ifndef CONFIG_SPL_BUILD
41 void save_boot_params(u32 r0, u32 r1, u32 r2, u32 r3)
42 {
43         from_spl = r0 != UBOOT_NOT_LOADED_FROM_SPL;
44         save_boot_params_ret();
45 }
46 #endif
47
48 bool spl_was_boot_source(void)
49 {
50         return from_spl;
51 }
52
53 #if defined(CONFIG_TEGRA_SUPPORT_NON_SECURE)
54 #if !defined(CONFIG_TEGRA124)
55 #error tegra_cpu_is_non_secure has only been validated on Tegra124
56 #endif
57 bool tegra_cpu_is_non_secure(void)
58 {
59         /*
60          * This register reads 0xffffffff in non-secure mode. This register
61          * only implements bits 31:20, so the lower bits will always read 0 in
62          * secure mode. Thus, the lower bits are an indicator for secure vs.
63          * non-secure mode.
64          */
65         struct mc_ctlr *mc = (struct mc_ctlr *)NV_PA_MC_BASE;
66         uint32_t mc_s_cfg0 = readl(&mc->mc_security_cfg0);
67         return (mc_s_cfg0 & 1) == 1;
68 }
69 #endif
70
71 /* Read the RAM size directly from the memory controller */
72 static phys_size_t query_sdram_size(void)
73 {
74         struct mc_ctlr *const mc = (struct mc_ctlr *)NV_PA_MC_BASE;
75         u32 emem_cfg;
76         phys_size_t size_bytes;
77
78         emem_cfg = readl(&mc->mc_emem_cfg);
79 #if defined(CONFIG_TEGRA20)
80         debug("mc->mc_emem_cfg (MEM_SIZE_KB) = 0x%08x\n", emem_cfg);
81         size_bytes = get_ram_size((void *)PHYS_SDRAM_1, emem_cfg * 1024);
82 #else
83         debug("mc->mc_emem_cfg (MEM_SIZE_MB) = 0x%08x\n", emem_cfg);
84 #ifndef CONFIG_PHYS_64BIT
85         /*
86          * If >=4GB RAM is present, the byte RAM size won't fit into 32-bits
87          * and will wrap. Clip the reported size to the maximum that a 32-bit
88          * variable can represent (rounded to a page).
89          */
90         if (emem_cfg >= 4096) {
91                 size_bytes = U32_MAX & ~(0x1000 - 1);
92         } else
93 #endif
94         {
95                 /* RAM size EMC is programmed to. */
96                 size_bytes = (phys_size_t)emem_cfg * 1024 * 1024;
97 #ifndef CONFIG_ARM64
98                 /*
99                  * If all RAM fits within 32-bits, it can be accessed without
100                  * LPAE, so go test the RAM size. Otherwise, we can't access
101                  * all the RAM, and get_ram_size() would get confused, so
102                  * avoid using it. There's no reason we should need this
103                  * validation step anyway.
104                  */
105                 if (emem_cfg <= (0 - PHYS_SDRAM_1) / (1024 * 1024))
106                         size_bytes = get_ram_size((void *)PHYS_SDRAM_1,
107                                                   size_bytes);
108 #endif
109         }
110 #endif
111
112 #if defined(CONFIG_TEGRA30) || defined(CONFIG_TEGRA114)
113         /* External memory limited to 2047 MB due to IROM/HI-VEC */
114         if (size_bytes == SZ_2G)
115                 size_bytes -= SZ_1M;
116 #endif
117
118         return size_bytes;
119 }
120
121 int dram_init(void)
122 {
123         /* We do not initialise DRAM here. We just query the size */
124         gd->ram_size = query_sdram_size();
125         return 0;
126 }
127
128 static int uart_configs[] = {
129 #if defined(CONFIG_TEGRA20)
130  #if defined(CONFIG_TEGRA_UARTA_UAA_UAB)
131         FUNCMUX_UART1_UAA_UAB,
132  #elif defined(CONFIG_TEGRA_UARTA_GPU)
133         FUNCMUX_UART1_GPU,
134  #elif defined(CONFIG_TEGRA_UARTA_SDIO1)
135         FUNCMUX_UART1_SDIO1,
136  #else
137         FUNCMUX_UART1_IRRX_IRTX,
138 #endif
139         FUNCMUX_UART2_UAD,
140         -1,
141         FUNCMUX_UART4_GMC,
142         -1,
143 #elif defined(CONFIG_TEGRA30)
144         FUNCMUX_UART1_ULPI,     /* UARTA */
145         -1,
146         -1,
147         -1,
148         -1,
149 #elif defined(CONFIG_TEGRA114)
150         -1,
151         -1,
152         -1,
153         FUNCMUX_UART4_GMI,      /* UARTD */
154         -1,
155 #elif defined(CONFIG_TEGRA124)
156         FUNCMUX_UART1_KBC,      /* UARTA */
157         -1,
158         -1,
159         FUNCMUX_UART4_GPIO,     /* UARTD */
160         -1,
161 #else   /* Tegra210 */
162         FUNCMUX_UART1_UART1,    /* UARTA */
163         -1,
164         -1,
165         FUNCMUX_UART4_UART4,    /* UARTD */
166         -1,
167 #endif
168 };
169
170 /**
171  * Set up the specified uarts
172  *
173  * @param uarts_ids     Mask containing UARTs to init (UARTx)
174  */
175 static void setup_uarts(int uart_ids)
176 {
177         static enum periph_id id_for_uart[] = {
178                 PERIPH_ID_UART1,
179                 PERIPH_ID_UART2,
180                 PERIPH_ID_UART3,
181                 PERIPH_ID_UART4,
182                 PERIPH_ID_UART5,
183         };
184         size_t i;
185
186         for (i = 0; i < UART_COUNT; i++) {
187                 if (uart_ids & (1 << i)) {
188                         enum periph_id id = id_for_uart[i];
189
190                         funcmux_select(id, uart_configs[i]);
191                         clock_ll_start_uart(id);
192                 }
193         }
194 }
195
196 void board_init_uart_f(void)
197 {
198         int uart_ids = 0;       /* bit mask of which UART ids to enable */
199
200 #ifdef CONFIG_TEGRA_ENABLE_UARTA
201         uart_ids |= UARTA;
202 #endif
203 #ifdef CONFIG_TEGRA_ENABLE_UARTB
204         uart_ids |= UARTB;
205 #endif
206 #ifdef CONFIG_TEGRA_ENABLE_UARTC
207         uart_ids |= UARTC;
208 #endif
209 #ifdef CONFIG_TEGRA_ENABLE_UARTD
210         uart_ids |= UARTD;
211 #endif
212 #ifdef CONFIG_TEGRA_ENABLE_UARTE
213         uart_ids |= UARTE;
214 #endif
215         setup_uarts(uart_ids);
216 }
217
218 #if !CONFIG_IS_ENABLED(OF_CONTROL)
219 static struct ns16550_platdata ns16550_com1_pdata = {
220         .base = CONFIG_SYS_NS16550_COM1,
221         .reg_shift = 2,
222         .clock = CONFIG_SYS_NS16550_CLK,
223         .fcr = UART_FCR_DEFVAL,
224 };
225
226 U_BOOT_DEVICE(ns16550_com1) = {
227         "ns16550_serial", &ns16550_com1_pdata
228 };
229 #endif
230
231 #if !CONFIG_IS_ENABLED(SYS_DCACHE_OFF) && !defined(CONFIG_ARM64)
232 void enable_caches(void)
233 {
234         /* Enable D-cache. I-cache is already enabled in start.S */
235         dcache_enable();
236 }
237 #endif