env: Move env_set() to env.h
[oweals/u-boot.git] / arch / arm / mach-tegra / board2.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  *  (C) Copyright 2010,2011
4  *  NVIDIA Corporation <www.nvidia.com>
5  */
6
7 #include <common.h>
8 #include <dm.h>
9 #include <efi_loader.h>
10 #include <env.h>
11 #include <errno.h>
12 #include <ns16550.h>
13 #include <usb.h>
14 #include <asm/io.h>
15 #include <asm/arch-tegra/ap.h>
16 #include <asm/arch-tegra/board.h>
17 #include <asm/arch-tegra/cboot.h>
18 #include <asm/arch-tegra/clk_rst.h>
19 #include <asm/arch-tegra/pmc.h>
20 #include <asm/arch-tegra/pmu.h>
21 #include <asm/arch-tegra/sys_proto.h>
22 #include <asm/arch-tegra/uart.h>
23 #include <asm/arch-tegra/warmboot.h>
24 #include <asm/arch-tegra/gpu.h>
25 #include <asm/arch-tegra/usb.h>
26 #include <asm/arch-tegra/xusb-padctl.h>
27 #if IS_ENABLED(CONFIG_TEGRA_CLKRST)
28 #include <asm/arch/clock.h>
29 #endif
30 #if IS_ENABLED(CONFIG_TEGRA_PINCTRL)
31 #include <asm/arch/funcmux.h>
32 #include <asm/arch/pinmux.h>
33 #endif
34 #include <asm/arch/tegra.h>
35 #ifdef CONFIG_TEGRA_CLOCK_SCALING
36 #include <asm/arch/emc.h>
37 #endif
38 #include "emc.h"
39
40 DECLARE_GLOBAL_DATA_PTR;
41
42 #ifdef CONFIG_SPL_BUILD
43 /* TODO(sjg@chromium.org): Remove once SPL supports device tree */
44 U_BOOT_DEVICE(tegra_gpios) = {
45         "gpio_tegra"
46 };
47 #endif
48
49 __weak void pinmux_init(void) {}
50 __weak void pin_mux_usb(void) {}
51 __weak void pin_mux_spi(void) {}
52 __weak void pin_mux_mmc(void) {}
53 __weak void gpio_early_init_uart(void) {}
54 __weak void pin_mux_display(void) {}
55 __weak void start_cpu_fan(void) {}
56 __weak void cboot_late_init(void) {}
57
58 #if defined(CONFIG_TEGRA_NAND)
59 __weak void pin_mux_nand(void)
60 {
61         funcmux_select(PERIPH_ID_NDFLASH, FUNCMUX_DEFAULT);
62 }
63 #endif
64
65 /*
66  * Routine: power_det_init
67  * Description: turn off power detects
68  */
69 static void power_det_init(void)
70 {
71 #if defined(CONFIG_TEGRA20)
72         struct pmc_ctlr *const pmc = (struct pmc_ctlr *)NV_PA_PMC_BASE;
73
74         /* turn off power detects */
75         writel(0, &pmc->pmc_pwr_det_latch);
76         writel(0, &pmc->pmc_pwr_det);
77 #endif
78 }
79
80 __weak int tegra_board_id(void)
81 {
82         return -1;
83 }
84
85 #ifdef CONFIG_DISPLAY_BOARDINFO
86 int checkboard(void)
87 {
88         int board_id = tegra_board_id();
89
90         printf("Board: %s", CONFIG_TEGRA_BOARD_STRING);
91         if (board_id != -1)
92                 printf(", ID: %d\n", board_id);
93         printf("\n");
94
95         return 0;
96 }
97 #endif  /* CONFIG_DISPLAY_BOARDINFO */
98
99 __weak int tegra_lcd_pmic_init(int board_it)
100 {
101         return 0;
102 }
103
104 __weak int nvidia_board_init(void)
105 {
106         return 0;
107 }
108
109 /*
110  * Routine: board_init
111  * Description: Early hardware init.
112  */
113 int board_init(void)
114 {
115         __maybe_unused int err;
116         __maybe_unused int board_id;
117
118         /* Do clocks and UART first so that printf() works */
119 #if IS_ENABLED(CONFIG_TEGRA_CLKRST)
120         clock_init();
121         clock_verify();
122 #endif
123
124         tegra_gpu_config();
125
126 #ifdef CONFIG_TEGRA_SPI
127         pin_mux_spi();
128 #endif
129
130 #ifdef CONFIG_MMC_SDHCI_TEGRA
131         pin_mux_mmc();
132 #endif
133
134         /* Init is handled automatically in the driver-model case */
135 #if defined(CONFIG_DM_VIDEO)
136         pin_mux_display();
137 #endif
138         /* boot param addr */
139         gd->bd->bi_boot_params = (NV_PA_SDRAM_BASE + 0x100);
140
141         power_det_init();
142
143 #ifdef CONFIG_SYS_I2C_TEGRA
144 # ifdef CONFIG_TEGRA_PMU
145         if (pmu_set_nominal())
146                 debug("Failed to select nominal voltages\n");
147 #  ifdef CONFIG_TEGRA_CLOCK_SCALING
148         err = board_emc_init();
149         if (err)
150                 debug("Memory controller init failed: %d\n", err);
151 #  endif
152 # endif /* CONFIG_TEGRA_PMU */
153 #endif /* CONFIG_SYS_I2C_TEGRA */
154
155 #ifdef CONFIG_USB_EHCI_TEGRA
156         pin_mux_usb();
157 #endif
158
159 #if defined(CONFIG_DM_VIDEO)
160         board_id = tegra_board_id();
161         err = tegra_lcd_pmic_init(board_id);
162         if (err) {
163                 debug("Failed to set up LCD PMIC\n");
164                 return err;
165         }
166 #endif
167
168 #ifdef CONFIG_TEGRA_NAND
169         pin_mux_nand();
170 #endif
171
172         tegra_xusb_padctl_init();
173
174 #ifdef CONFIG_TEGRA_LP0
175         /* save Sdram params to PMC 2, 4, and 24 for WB0 */
176         warmboot_save_sdram_params();
177
178         /* prepare the WB code to LP0 location */
179         warmboot_prepare_code(TEGRA_LP0_ADDR, TEGRA_LP0_SIZE);
180 #endif
181         return nvidia_board_init();
182 }
183
184 #ifdef CONFIG_BOARD_EARLY_INIT_F
185 static void __gpio_early_init(void)
186 {
187 }
188
189 void gpio_early_init(void) __attribute__((weak, alias("__gpio_early_init")));
190
191 int board_early_init_f(void)
192 {
193 #if IS_ENABLED(CONFIG_TEGRA_CLKRST)
194         if (!clock_early_init_done())
195                 clock_early_init();
196 #endif
197
198 #if defined(CONFIG_TEGRA_DISCONNECT_UDC_ON_BOOT)
199 #define USBCMD_FS2 (1 << 15)
200         {
201                 struct usb_ctlr *usbctlr = (struct usb_ctlr *)0x7d000000;
202                 writel(USBCMD_FS2, &usbctlr->usb_cmd);
203         }
204 #endif
205
206         /* Do any special system timer/TSC setup */
207 #if IS_ENABLED(CONFIG_TEGRA_CLKRST)
208 #  if defined(CONFIG_TEGRA_SUPPORT_NON_SECURE)
209         if (!tegra_cpu_is_non_secure())
210 #  endif
211                 arch_timer_init();
212 #endif
213
214         pinmux_init();
215         board_init_uart_f();
216
217         /* Initialize periph GPIOs */
218         gpio_early_init();
219         gpio_early_init_uart();
220
221         return 0;
222 }
223 #endif  /* EARLY_INIT */
224
225 int board_late_init(void)
226 {
227 #if CONFIG_IS_ENABLED(EFI_LOADER)
228         if (gd->bd->bi_dram[1].start) {
229                 /*
230                  * Only bank 0 is below board_get_usable_ram_top(), so all of
231                  * bank 1 is not mapped by the U-Boot MMU configuration, and so
232                  * we must prevent EFI from using it.
233                  */
234                 efi_add_memory_map(gd->bd->bi_dram[1].start,
235                                    gd->bd->bi_dram[1].size >> EFI_PAGE_SHIFT,
236                                    EFI_BOOT_SERVICES_DATA, false);
237         }
238 #endif
239
240 #if defined(CONFIG_TEGRA_SUPPORT_NON_SECURE)
241         if (tegra_cpu_is_non_secure()) {
242                 printf("CPU is in NS mode\n");
243                 env_set("cpu_ns_mode", "1");
244         } else {
245                 env_set("cpu_ns_mode", "");
246         }
247 #endif
248         start_cpu_fan();
249         cboot_late_init();
250
251         return 0;
252 }
253
254 /*
255  * In some SW environments, a memory carve-out exists to house a secure
256  * monitor, a trusted OS, and/or various statically allocated media buffers.
257  *
258  * This carveout exists at the highest possible address that is within a
259  * 32-bit physical address space.
260  *
261  * This function returns the total size of this carve-out. At present, the
262  * returned value is hard-coded for simplicity. In the future, it may be
263  * possible to determine the carve-out size:
264  * - By querying some run-time information source, such as:
265  *   - A structure passed to U-Boot by earlier boot software.
266  *   - SoC registers.
267  *   - A call into the secure monitor.
268  * - In the per-board U-Boot configuration header, based on knowledge of the
269  *   SW environment that U-Boot is being built for.
270  *
271  * For now, we support two configurations in U-Boot:
272  * - 32-bit ports without any form of carve-out.
273  * - 64 bit ports which are assumed to use a carve-out of a conservatively
274  *   hard-coded size.
275  */
276 static ulong carveout_size(void)
277 {
278 #ifdef CONFIG_ARM64
279         return SZ_512M;
280 #elif defined(CONFIG_ARMV7_SECURE_RESERVE_SIZE)
281         // BASE+SIZE might not == 4GB. If so, we want the carveout to cover
282         // from BASE to 4GB, not BASE to BASE+SIZE.
283         return (0 - CONFIG_ARMV7_SECURE_BASE) & ~(SZ_2M - 1);
284 #else
285         return 0;
286 #endif
287 }
288
289 /*
290  * Determine the amount of usable RAM below 4GiB, taking into account any
291  * carve-out that may be assigned.
292  */
293 static ulong usable_ram_size_below_4g(void)
294 {
295         ulong total_size_below_4g;
296         ulong usable_size_below_4g;
297
298         /*
299          * The total size of RAM below 4GiB is the lesser address of:
300          * (a) 2GiB itself (RAM starts at 2GiB, and 4GiB - 2GiB == 2GiB).
301          * (b) The size RAM physically present in the system.
302          */
303         if (gd->ram_size < SZ_2G)
304                 total_size_below_4g = gd->ram_size;
305         else
306                 total_size_below_4g = SZ_2G;
307
308         /* Calculate usable RAM by subtracting out any carve-out size */
309         usable_size_below_4g = total_size_below_4g - carveout_size();
310
311         return usable_size_below_4g;
312 }
313
314 /*
315  * Represent all available RAM in either one or two banks.
316  *
317  * The first bank describes any usable RAM below 4GiB.
318  * The second bank describes any RAM above 4GiB.
319  *
320  * This split is driven by the following requirements:
321  * - The NVIDIA L4T kernel requires separate entries in the DT /memory/reg
322  *   property for memory below and above the 4GiB boundary. The layout of that
323  *   DT property is directly driven by the entries in the U-Boot bank array.
324  * - The potential existence of a carve-out at the end of RAM below 4GiB can
325  *   only be represented using multiple banks.
326  *
327  * Explicitly removing the carve-out RAM from the bank entries makes the RAM
328  * layout a bit more obvious, e.g. when running "bdinfo" at the U-Boot
329  * command-line.
330  *
331  * This does mean that the DT U-Boot passes to the Linux kernel will not
332  * include this RAM in /memory/reg at all. An alternative would be to include
333  * all RAM in the U-Boot banks (and hence DT), and add a /memreserve/ node
334  * into DT to stop the kernel from using the RAM. IIUC, I don't /think/ the
335  * Linux kernel will ever need to access any RAM in* the carve-out via a CPU
336  * mapping, so either way is acceptable.
337  *
338  * On 32-bit systems, we never define a bank for RAM above 4GiB, since the
339  * start address of that bank cannot be represented in the 32-bit .size
340  * field.
341  */
342 int dram_init_banksize(void)
343 {
344         int err;
345
346         /* try to compute DRAM bank size based on cboot DTB first */
347         err = cboot_dram_init_banksize();
348         if (err == 0)
349                 return err;
350
351         /* fall back to default DRAM bank size computation */
352
353         gd->bd->bi_dram[0].start = CONFIG_SYS_SDRAM_BASE;
354         gd->bd->bi_dram[0].size = usable_ram_size_below_4g();
355
356 #ifdef CONFIG_PCI
357         gd->pci_ram_top = gd->bd->bi_dram[0].start + gd->bd->bi_dram[0].size;
358 #endif
359
360 #ifdef CONFIG_PHYS_64BIT
361         if (gd->ram_size > SZ_2G) {
362                 gd->bd->bi_dram[1].start = 0x100000000;
363                 gd->bd->bi_dram[1].size = gd->ram_size - SZ_2G;
364         } else
365 #endif
366         {
367                 gd->bd->bi_dram[1].start = 0;
368                 gd->bd->bi_dram[1].size = 0;
369         }
370
371         return 0;
372 }
373
374 /*
375  * Most hardware on 64-bit Tegra is still restricted to DMA to the lower
376  * 32-bits of the physical address space. Cap the maximum usable RAM area
377  * at 4 GiB to avoid DMA buffers from being allocated beyond the 32-bit
378  * boundary that most devices can address. Also, don't let U-Boot use any
379  * carve-out, as mentioned above.
380  *
381  * This function is called before dram_init_banksize(), so we can't simply
382  * return gd->bd->bi_dram[1].start + gd->bd->bi_dram[1].size.
383  */
384 ulong board_get_usable_ram_top(ulong total_size)
385 {
386         ulong ram_top;
387
388         /* try to get top of usable RAM based on cboot DTB first */
389         ram_top = cboot_get_usable_ram_top(total_size);
390         if (ram_top > 0)
391                 return ram_top;
392
393         /* fall back to default usable RAM computation */
394
395         return CONFIG_SYS_SDRAM_BASE + usable_ram_size_below_4g();
396 }