blob: abcae15ea3315c8ab925056b5b24e935a4019c05 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Tom Warren41b68382011-01-27 10:58:05 +00002/*
Tom Warrenab0cc6b2015-03-04 16:36:00 -07003 * (C) Copyright 2010-2015
Tom Warren41b68382011-01-27 10:58:05 +00004 * NVIDIA Corporation <www.nvidia.com>
Tom Warren41b68382011-01-27 10:58:05 +00005 */
6
7#include <common.h>
Thomas Choue3b90262015-11-19 21:48:11 +08008#include <dm.h>
9#include <ns16550.h>
Simon Glasseec13c42015-05-13 07:02:29 -060010#include <spl.h>
Tom Warren41b68382011-01-27 10:58:05 +000011#include <asm/io.h>
Thierry Reding45ad0b02019-04-15 11:32:18 +020012#if IS_ENABLED(CONFIG_TEGRA_CLKRST)
Simon Glass96b7c432011-11-28 15:04:39 +000013#include <asm/arch/clock.h>
Thierry Reding45ad0b02019-04-15 11:32:18 +020014#endif
Thierry Reding7c0b1502019-04-15 11:32:21 +020015#if IS_ENABLED(CONFIG_TEGRA_PINCTRL)
Simon Glass96b7c432011-11-28 15:04:39 +000016#include <asm/arch/funcmux.h>
Thierry Reding7c0b1502019-04-15 11:32:21 +020017#endif
Thierry Reding17987bb2019-04-15 11:32:20 +020018#if IS_ENABLED(CONFIG_TEGRA_MC)
Marcel Ziswilerc5ecf272014-10-10 23:32:32 +020019#include <asm/arch/mc.h>
Thierry Reding17987bb2019-04-15 11:32:20 +020020#endif
Tom Warrenab371962012-09-19 15:50:56 -070021#include <asm/arch/tegra.h>
Stephen Warren8d1fb312015-01-19 16:25:52 -070022#include <asm/arch-tegra/ap.h>
Lucas Stache80f7ca2012-09-29 10:02:08 +000023#include <asm/arch-tegra/board.h>
Thierry Reding7cef2b22019-04-15 11:32:28 +020024#include <asm/arch-tegra/cboot.h>
Tom Warrenab371962012-09-19 15:50:56 -070025#include <asm/arch-tegra/pmc.h>
26#include <asm/arch-tegra/sys_proto.h>
27#include <asm/arch-tegra/warmboot.h>
Tom Warren41b68382011-01-27 10:58:05 +000028
Tom Warren021a8bb2015-07-08 08:05:35 -070029void save_boot_params_ret(void);
30
Tom Warren41b68382011-01-27 10:58:05 +000031DECLARE_GLOBAL_DATA_PTR;
32
Simon Glass96b7c432011-11-28 15:04:39 +000033enum {
34 /* UARTs which we can enable */
35 UARTA = 1 << 0,
36 UARTB = 1 << 1,
Tom Warrene3d95bc2013-01-28 13:32:10 +000037 UARTC = 1 << 2,
Simon Glass96b7c432011-11-28 15:04:39 +000038 UARTD = 1 << 3,
Tom Warrene3d95bc2013-01-28 13:32:10 +000039 UARTE = 1 << 4,
40 UART_COUNT = 5,
Simon Glass96b7c432011-11-28 15:04:39 +000041};
42
Simon Glasseec13c42015-05-13 07:02:29 -060043static bool from_spl __attribute__ ((section(".data")));
44
45#ifndef CONFIG_SPL_BUILD
Thierry Redingf6270a62019-04-15 11:32:23 +020046void save_boot_params(unsigned long r0, unsigned long r1, unsigned long r2,
47 unsigned long r3)
Simon Glasseec13c42015-05-13 07:02:29 -060048{
49 from_spl = r0 != UBOOT_NOT_LOADED_FROM_SPL;
Thierry Reding7cef2b22019-04-15 11:32:28 +020050
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
Simon Glasseec13c42015-05-13 07:02:29 -060065 save_boot_params_ret();
66}
67#endif
68
69bool spl_was_boot_source(void)
70{
71 return from_spl;
72}
73
Stephen Warren8d1fb312015-01-19 16:25:52 -070074#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
78bool 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
Thierry Reding17987bb2019-04-15 11:32:20 +020092#if IS_ENABLED(CONFIG_TEGRA_MC)
Stephen Warren1b4af6b2014-07-02 14:12:30 -060093/* Read the RAM size directly from the memory controller */
Stephen Warren6718af02015-08-07 16:12:44 -060094static phys_size_t query_sdram_size(void)
Stephen Warren1b4af6b2014-07-02 14:12:30 -060095{
96 struct mc_ctlr *const mc = (struct mc_ctlr *)NV_PA_MC_BASE;
Stephen Warren6718af02015-08-07 16:12:44 -060097 u32 emem_cfg;
98 phys_size_t size_bytes;
Stephen Warren1b4af6b2014-07-02 14:12:30 -060099
Stephen Warren210bdb22014-12-23 10:34:50 -0700100 emem_cfg = readl(&mc->mc_emem_cfg);
Marcel Ziswilerc5ecf272014-10-10 23:32:32 +0200101#if defined(CONFIG_TEGRA20)
Stephen Warren210bdb22014-12-23 10:34:50 -0700102 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);
Marcel Ziswilerc5ecf272014-10-10 23:32:32 +0200104#else
Stephen Warren210bdb22014-12-23 10:34:50 -0700105 debug("mc->mc_emem_cfg (MEM_SIZE_MB) = 0x%08x\n", emem_cfg);
Stephen Warren6718af02015-08-07 16:12:44 -0600106#ifndef CONFIG_PHYS_64BIT
Stephen Warrenc8018052014-12-23 10:34:51 -0700107 /*
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);
Stephen Warren6718af02015-08-07 16:12:44 -0600114 } else
115#endif
116 {
Stephen Warrenc8018052014-12-23 10:34:51 -0700117 /* RAM size EMC is programmed to. */
Stephen Warren6718af02015-08-07 16:12:44 -0600118 size_bytes = (phys_size_t)emem_cfg * 1024 * 1024;
119#ifndef CONFIG_ARM64
Stephen Warrenc8018052014-12-23 10:34:51 -0700120 /*
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);
Stephen Warren6718af02015-08-07 16:12:44 -0600130#endif
Stephen Warrenc8018052014-12-23 10:34:51 -0700131 }
Marcel Ziswilerc5ecf272014-10-10 23:32:32 +0200132#endif
Stephen Warren1b4af6b2014-07-02 14:12:30 -0600133
Marcel Ziswilerc5ecf272014-10-10 23:32:32 +0200134#if defined(CONFIG_TEGRA30) || defined(CONFIG_TEGRA114)
135 /* External memory limited to 2047 MB due to IROM/HI-VEC */
Stephen Warren210bdb22014-12-23 10:34:50 -0700136 if (size_bytes == SZ_2G)
137 size_bytes -= SZ_1M;
Stephen Warren1b4af6b2014-07-02 14:12:30 -0600138#endif
Tom Warren41b68382011-01-27 10:58:05 +0000139
Stephen Warren210bdb22014-12-23 10:34:50 -0700140 return size_bytes;
Marcel Ziswilerc5ecf272014-10-10 23:32:32 +0200141}
Thierry Reding17987bb2019-04-15 11:32:20 +0200142#endif
Marcel Ziswilerc5ecf272014-10-10 23:32:32 +0200143
Tom Warren41b68382011-01-27 10:58:05 +0000144int dram_init(void)
145{
Thierry Reding7cef2b22019-04-15 11:32:28 +0200146 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
Thierry Reding17987bb2019-04-15 11:32:20 +0200153#if IS_ENABLED(CONFIG_TEGRA_MC)
Tom Warren41b68382011-01-27 10:58:05 +0000154 /* We do not initialise DRAM here. We just query the size */
Simon Glassf6fcbbd2011-11-05 03:56:57 +0000155 gd->ram_size = query_sdram_size();
Thierry Reding17987bb2019-04-15 11:32:20 +0200156#endif
157
Tom Warren41b68382011-01-27 10:58:05 +0000158 return 0;
159}
160
Thierry Reding7c0b1502019-04-15 11:32:21 +0200161#if IS_ENABLED(CONFIG_TEGRA_PINCTRL)
Stephen Warren59f90102012-05-14 13:13:45 +0000162static int uart_configs[] = {
Tom Warren61c6d0e2012-12-11 13:34:15 +0000163#if defined(CONFIG_TEGRA20)
164 #if defined(CONFIG_TEGRA_UARTA_UAA_UAB)
Stephen Warren59f90102012-05-14 13:13:45 +0000165 FUNCMUX_UART1_UAA_UAB,
Tom Warren61c6d0e2012-12-11 13:34:15 +0000166 #elif defined(CONFIG_TEGRA_UARTA_GPU)
Stephen Warrene4c01a82012-05-16 05:59:59 +0000167 FUNCMUX_UART1_GPU,
Tom Warren61c6d0e2012-12-11 13:34:15 +0000168 #elif defined(CONFIG_TEGRA_UARTA_SDIO1)
Lucas Stach4de6eec2012-05-16 08:21:02 +0000169 FUNCMUX_UART1_SDIO1,
Tom Warren61c6d0e2012-12-11 13:34:15 +0000170 #else
Stephen Warren59f90102012-05-14 13:13:45 +0000171 FUNCMUX_UART1_IRRX_IRTX,
Stephen Warren811af732013-01-22 06:20:08 +0000172#endif
173 FUNCMUX_UART2_UAD,
Stephen Warren59f90102012-05-14 13:13:45 +0000174 -1,
175 FUNCMUX_UART4_GMC,
176 -1,
Tom Warrene3d95bc2013-01-28 13:32:10 +0000177#elif defined(CONFIG_TEGRA30)
Tom Warren61c6d0e2012-12-11 13:34:15 +0000178 FUNCMUX_UART1_ULPI, /* UARTA */
179 -1,
180 -1,
181 -1,
182 -1,
Tom Warrene5ffffd2014-01-24 12:46:16 -0700183#elif defined(CONFIG_TEGRA114)
Tom Warrene3d95bc2013-01-28 13:32:10 +0000184 -1,
185 -1,
186 -1,
187 FUNCMUX_UART4_GMI, /* UARTD */
188 -1,
Tom Warrenab0cc6b2015-03-04 16:36:00 -0700189#elif defined(CONFIG_TEGRA124)
Tom Warrene5ffffd2014-01-24 12:46:16 -0700190 FUNCMUX_UART1_KBC, /* UARTA */
191 -1,
192 -1,
193 FUNCMUX_UART4_GPIO, /* UARTD */
194 -1,
Tom Warrenab0cc6b2015-03-04 16:36:00 -0700195#else /* Tegra210 */
196 FUNCMUX_UART1_UART1, /* UARTA */
197 -1,
198 -1,
199 FUNCMUX_UART4_UART4, /* UARTD */
200 -1,
Tom Warren61c6d0e2012-12-11 13:34:15 +0000201#endif
Stephen Warren59f90102012-05-14 13:13:45 +0000202};
203
Simon Glass96b7c432011-11-28 15:04:39 +0000204/**
205 * Set up the specified uarts
206 *
207 * @param uarts_ids Mask containing UARTs to init (UARTx)
208 */
209static 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,
Tom Warrene3d95bc2013-01-28 13:32:10 +0000216 PERIPH_ID_UART5,
Simon Glass96b7c432011-11-28 15:04:39 +0000217 };
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
Stephen Warren59f90102012-05-14 13:13:45 +0000224 funcmux_select(id, uart_configs[i]);
Simon Glass96b7c432011-11-28 15:04:39 +0000225 clock_ll_start_uart(id);
226 }
227 }
228}
Thierry Reding7c0b1502019-04-15 11:32:21 +0200229#endif
Simon Glass96b7c432011-11-28 15:04:39 +0000230
231void board_init_uart_f(void)
232{
Thierry Reding7c0b1502019-04-15 11:32:21 +0200233#if IS_ENABLED(CONFIG_TEGRA_PINCTRL)
Simon Glass96b7c432011-11-28 15:04:39 +0000234 int uart_ids = 0; /* bit mask of which UART ids to enable */
235
Tom Warren22562a42012-09-04 17:00:24 -0700236#ifdef CONFIG_TEGRA_ENABLE_UARTA
Simon Glass96b7c432011-11-28 15:04:39 +0000237 uart_ids |= UARTA;
238#endif
Tom Warren22562a42012-09-04 17:00:24 -0700239#ifdef CONFIG_TEGRA_ENABLE_UARTB
Simon Glass96b7c432011-11-28 15:04:39 +0000240 uart_ids |= UARTB;
241#endif
Tom Warrene3d95bc2013-01-28 13:32:10 +0000242#ifdef CONFIG_TEGRA_ENABLE_UARTC
243 uart_ids |= UARTC;
244#endif
Tom Warren22562a42012-09-04 17:00:24 -0700245#ifdef CONFIG_TEGRA_ENABLE_UARTD
Simon Glass96b7c432011-11-28 15:04:39 +0000246 uart_ids |= UARTD;
247#endif
Tom Warrene3d95bc2013-01-28 13:32:10 +0000248#ifdef CONFIG_TEGRA_ENABLE_UARTE
249 uart_ids |= UARTE;
250#endif
Simon Glass96b7c432011-11-28 15:04:39 +0000251 setup_uarts(uart_ids);
Thierry Reding7c0b1502019-04-15 11:32:21 +0200252#endif
Simon Glass96b7c432011-11-28 15:04:39 +0000253}
Simon Glass410012f2012-01-09 13:22:15 +0000254
Simon Glassf4402d02015-12-04 08:58:39 -0700255#if !CONFIG_IS_ENABLED(OF_CONTROL)
Thomas Choue3b90262015-11-19 21:48:11 +0800256static struct ns16550_platdata ns16550_com1_pdata = {
257 .base = CONFIG_SYS_NS16550_COM1,
258 .reg_shift = 2,
259 .clock = CONFIG_SYS_NS16550_CLK,
Heiko Schocher06f108e2017-01-18 08:05:49 +0100260 .fcr = UART_FCR_DEFVAL,
Thomas Choue3b90262015-11-19 21:48:11 +0800261};
262
263U_BOOT_DEVICE(ns16550_com1) = {
264 "ns16550_serial", &ns16550_com1_pdata
265};
266#endif
267
Trevor Woerner43ec7e02019-05-03 09:41:00 -0400268#if !CONFIG_IS_ENABLED(SYS_DCACHE_OFF) && !defined(CONFIG_ARM64)
Simon Glass410012f2012-01-09 13:22:15 +0000269void enable_caches(void)
270{
271 /* Enable D-cache. I-cache is already enabled in start.S */
272 dcache_enable();
273}
274#endif