Merge tag 'u-boot-atmel-fixes-2020.07-a' of https://gitlab.denx.de/u-boot/custodians...
[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 <cpu_func.h>
9 #include <dm.h>
10 #include <init.h>
11 #include <log.h>
12 #include <ns16550.h>
13 #include <spl.h>
14 #include <asm/cache.h>
15 #include <asm/io.h>
16 #if IS_ENABLED(CONFIG_TEGRA_CLKRST)
17 #include <asm/arch/clock.h>
18 #endif
19 #if IS_ENABLED(CONFIG_TEGRA_PINCTRL)
20 #include <asm/arch/funcmux.h>
21 #endif
22 #if IS_ENABLED(CONFIG_TEGRA_MC)
23 #include <asm/arch/mc.h>
24 #endif
25 #include <asm/arch/tegra.h>
26 #include <asm/arch-tegra/ap.h>
27 #include <asm/arch-tegra/board.h>
28 #include <asm/arch-tegra/cboot.h>
29 #include <asm/arch-tegra/pmc.h>
30 #include <asm/arch-tegra/sys_proto.h>
31 #include <asm/arch-tegra/warmboot.h>
32
33 void save_boot_params_ret(void);
34
35 DECLARE_GLOBAL_DATA_PTR;
36
37 enum {
38         /* UARTs which we can enable */
39         UARTA   = 1 << 0,
40         UARTB   = 1 << 1,
41         UARTC   = 1 << 2,
42         UARTD   = 1 << 3,
43         UARTE   = 1 << 4,
44         UART_COUNT = 5,
45 };
46
47 static bool from_spl __attribute__ ((section(".data")));
48
49 #ifndef CONFIG_SPL_BUILD
50 void save_boot_params(unsigned long r0, unsigned long r1, unsigned long r2,
51                       unsigned long r3)
52 {
53         from_spl = r0 != UBOOT_NOT_LOADED_FROM_SPL;
54
55         /*
56          * The logic for this is somewhat indirect. The purpose of the marker
57          * (UBOOT_NOT_LOADED_FROM_SPL) is in fact used to determine if U-Boot
58          * was loaded from a read-only instance of itself, which is something
59          * that can happen in secure boot setups. So basically the presence
60          * of the marker is an indication that U-Boot was loaded by one such
61          * special variant of U-Boot. Conversely, the absence of the marker
62          * indicates that this instance of U-Boot was loaded by something
63          * other than a special U-Boot. This could be SPL, but it could just
64          * as well be one of any number of other first stage bootloaders.
65          */
66         if (from_spl)
67                 cboot_save_boot_params(r0, r1, r2, r3);
68
69         save_boot_params_ret();
70 }
71 #endif
72
73 bool spl_was_boot_source(void)
74 {
75         return from_spl;
76 }
77
78 #if defined(CONFIG_TEGRA_SUPPORT_NON_SECURE)
79 #if !defined(CONFIG_TEGRA124)
80 #error tegra_cpu_is_non_secure has only been validated on Tegra124
81 #endif
82 bool tegra_cpu_is_non_secure(void)
83 {
84         /*
85          * This register reads 0xffffffff in non-secure mode. This register
86          * only implements bits 31:20, so the lower bits will always read 0 in
87          * secure mode. Thus, the lower bits are an indicator for secure vs.
88          * non-secure mode.
89          */
90         struct mc_ctlr *mc = (struct mc_ctlr *)NV_PA_MC_BASE;
91         uint32_t mc_s_cfg0 = readl(&mc->mc_security_cfg0);
92         return (mc_s_cfg0 & 1) == 1;
93 }
94 #endif
95
96 #if IS_ENABLED(CONFIG_TEGRA_MC)
97 /* Read the RAM size directly from the memory controller */
98 static phys_size_t query_sdram_size(void)
99 {
100         struct mc_ctlr *const mc = (struct mc_ctlr *)NV_PA_MC_BASE;
101         u32 emem_cfg;
102         phys_size_t size_bytes;
103
104         emem_cfg = readl(&mc->mc_emem_cfg);
105 #if defined(CONFIG_TEGRA20)
106         debug("mc->mc_emem_cfg (MEM_SIZE_KB) = 0x%08x\n", emem_cfg);
107         size_bytes = get_ram_size((void *)PHYS_SDRAM_1, emem_cfg * 1024);
108 #else
109         debug("mc->mc_emem_cfg (MEM_SIZE_MB) = 0x%08x\n", emem_cfg);
110 #ifndef CONFIG_PHYS_64BIT
111         /*
112          * If >=4GB RAM is present, the byte RAM size won't fit into 32-bits
113          * and will wrap. Clip the reported size to the maximum that a 32-bit
114          * variable can represent (rounded to a page).
115          */
116         if (emem_cfg >= 4096) {
117                 size_bytes = U32_MAX & ~(0x1000 - 1);
118         } else
119 #endif
120         {
121                 /* RAM size EMC is programmed to. */
122                 size_bytes = (phys_size_t)emem_cfg * 1024 * 1024;
123 #ifndef CONFIG_ARM64
124                 /*
125                  * If all RAM fits within 32-bits, it can be accessed without
126                  * LPAE, so go test the RAM size. Otherwise, we can't access
127                  * all the RAM, and get_ram_size() would get confused, so
128                  * avoid using it. There's no reason we should need this
129                  * validation step anyway.
130                  */
131                 if (emem_cfg <= (0 - PHYS_SDRAM_1) / (1024 * 1024))
132                         size_bytes = get_ram_size((void *)PHYS_SDRAM_1,
133                                                   size_bytes);
134 #endif
135         }
136 #endif
137
138 #if defined(CONFIG_TEGRA30) || defined(CONFIG_TEGRA114)
139         /* External memory limited to 2047 MB due to IROM/HI-VEC */
140         if (size_bytes == SZ_2G)
141                 size_bytes -= SZ_1M;
142 #endif
143
144         return size_bytes;
145 }
146 #endif
147
148 int dram_init(void)
149 {
150         int err;
151
152         /* try to initialize DRAM from cboot DTB first */
153         err = cboot_dram_init();
154         if (err == 0)
155                 return 0;
156
157 #if IS_ENABLED(CONFIG_TEGRA_MC)
158         /* We do not initialise DRAM here. We just query the size */
159         gd->ram_size = query_sdram_size();
160 #endif
161
162         return 0;
163 }
164
165 #if IS_ENABLED(CONFIG_TEGRA_PINCTRL)
166 static int uart_configs[] = {
167 #if defined(CONFIG_TEGRA20)
168  #if defined(CONFIG_TEGRA_UARTA_UAA_UAB)
169         FUNCMUX_UART1_UAA_UAB,
170  #elif defined(CONFIG_TEGRA_UARTA_GPU)
171         FUNCMUX_UART1_GPU,
172  #elif defined(CONFIG_TEGRA_UARTA_SDIO1)
173         FUNCMUX_UART1_SDIO1,
174  #else
175         FUNCMUX_UART1_IRRX_IRTX,
176 #endif
177         FUNCMUX_UART2_UAD,
178         -1,
179         FUNCMUX_UART4_GMC,
180         -1,
181 #elif defined(CONFIG_TEGRA30)
182         FUNCMUX_UART1_ULPI,     /* UARTA */
183         -1,
184         -1,
185         -1,
186         -1,
187 #elif defined(CONFIG_TEGRA114)
188         -1,
189         -1,
190         -1,
191         FUNCMUX_UART4_GMI,      /* UARTD */
192         -1,
193 #elif defined(CONFIG_TEGRA124)
194         FUNCMUX_UART1_KBC,      /* UARTA */
195         -1,
196         -1,
197         FUNCMUX_UART4_GPIO,     /* UARTD */
198         -1,
199 #else   /* Tegra210 */
200         FUNCMUX_UART1_UART1,    /* UARTA */
201         -1,
202         -1,
203         FUNCMUX_UART4_UART4,    /* UARTD */
204         -1,
205 #endif
206 };
207
208 /**
209  * Set up the specified uarts
210  *
211  * @param uarts_ids     Mask containing UARTs to init (UARTx)
212  */
213 static void setup_uarts(int uart_ids)
214 {
215         static enum periph_id id_for_uart[] = {
216                 PERIPH_ID_UART1,
217                 PERIPH_ID_UART2,
218                 PERIPH_ID_UART3,
219                 PERIPH_ID_UART4,
220                 PERIPH_ID_UART5,
221         };
222         size_t i;
223
224         for (i = 0; i < UART_COUNT; i++) {
225                 if (uart_ids & (1 << i)) {
226                         enum periph_id id = id_for_uart[i];
227
228                         funcmux_select(id, uart_configs[i]);
229                         clock_ll_start_uart(id);
230                 }
231         }
232 }
233 #endif
234
235 void board_init_uart_f(void)
236 {
237 #if IS_ENABLED(CONFIG_TEGRA_PINCTRL)
238         int uart_ids = 0;       /* bit mask of which UART ids to enable */
239
240 #ifdef CONFIG_TEGRA_ENABLE_UARTA
241         uart_ids |= UARTA;
242 #endif
243 #ifdef CONFIG_TEGRA_ENABLE_UARTB
244         uart_ids |= UARTB;
245 #endif
246 #ifdef CONFIG_TEGRA_ENABLE_UARTC
247         uart_ids |= UARTC;
248 #endif
249 #ifdef CONFIG_TEGRA_ENABLE_UARTD
250         uart_ids |= UARTD;
251 #endif
252 #ifdef CONFIG_TEGRA_ENABLE_UARTE
253         uart_ids |= UARTE;
254 #endif
255         setup_uarts(uart_ids);
256 #endif
257 }
258
259 #if !CONFIG_IS_ENABLED(OF_CONTROL)
260 static struct ns16550_platdata ns16550_com1_pdata = {
261         .base = CONFIG_SYS_NS16550_COM1,
262         .reg_shift = 2,
263         .clock = CONFIG_SYS_NS16550_CLK,
264         .fcr = UART_FCR_DEFVAL,
265 };
266
267 U_BOOT_DEVICE(ns16550_com1) = {
268         "ns16550_serial", &ns16550_com1_pdata
269 };
270 #endif
271
272 #if !CONFIG_IS_ENABLED(SYS_DCACHE_OFF) && !defined(CONFIG_ARM64)
273 void enable_caches(void)
274 {
275         /* Enable D-cache. I-cache is already enabled in start.S */
276         dcache_enable();
277 }
278 #endif