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