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