blob: 209416e6ad8cd3fb216bf2852a96345b3cb8b483 [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
Simon Glass96b7c432011-11-28 15:04:39 +000015#include <asm/arch/funcmux.h>
Marcel Ziswilerc5ecf272014-10-10 23:32:32 +020016#include <asm/arch/mc.h>
Tom Warrenab371962012-09-19 15:50:56 -070017#include <asm/arch/tegra.h>
Stephen Warren8d1fb312015-01-19 16:25:52 -070018#include <asm/arch-tegra/ap.h>
Lucas Stache80f7ca2012-09-29 10:02:08 +000019#include <asm/arch-tegra/board.h>
Tom Warrenab371962012-09-19 15:50:56 -070020#include <asm/arch-tegra/pmc.h>
21#include <asm/arch-tegra/sys_proto.h>
22#include <asm/arch-tegra/warmboot.h>
Tom Warren41b68382011-01-27 10:58:05 +000023
Tom Warren021a8bb2015-07-08 08:05:35 -070024void save_boot_params_ret(void);
25
Tom Warren41b68382011-01-27 10:58:05 +000026DECLARE_GLOBAL_DATA_PTR;
27
Simon Glass96b7c432011-11-28 15:04:39 +000028enum {
29 /* UARTs which we can enable */
30 UARTA = 1 << 0,
31 UARTB = 1 << 1,
Tom Warrene3d95bc2013-01-28 13:32:10 +000032 UARTC = 1 << 2,
Simon Glass96b7c432011-11-28 15:04:39 +000033 UARTD = 1 << 3,
Tom Warrene3d95bc2013-01-28 13:32:10 +000034 UARTE = 1 << 4,
35 UART_COUNT = 5,
Simon Glass96b7c432011-11-28 15:04:39 +000036};
37
Simon Glasseec13c42015-05-13 07:02:29 -060038static bool from_spl __attribute__ ((section(".data")));
39
40#ifndef CONFIG_SPL_BUILD
41void save_boot_params(u32 r0, u32 r1, u32 r2, u32 r3)
42{
43 from_spl = r0 != UBOOT_NOT_LOADED_FROM_SPL;
44 save_boot_params_ret();
45}
46#endif
47
48bool spl_was_boot_source(void)
49{
50 return from_spl;
51}
52
Stephen Warren8d1fb312015-01-19 16:25:52 -070053#if defined(CONFIG_TEGRA_SUPPORT_NON_SECURE)
54#if !defined(CONFIG_TEGRA124)
55#error tegra_cpu_is_non_secure has only been validated on Tegra124
56#endif
57bool tegra_cpu_is_non_secure(void)
58{
59 /*
60 * This register reads 0xffffffff in non-secure mode. This register
61 * only implements bits 31:20, so the lower bits will always read 0 in
62 * secure mode. Thus, the lower bits are an indicator for secure vs.
63 * non-secure mode.
64 */
65 struct mc_ctlr *mc = (struct mc_ctlr *)NV_PA_MC_BASE;
66 uint32_t mc_s_cfg0 = readl(&mc->mc_security_cfg0);
67 return (mc_s_cfg0 & 1) == 1;
68}
69#endif
70
Stephen Warren1b4af6b2014-07-02 14:12:30 -060071/* Read the RAM size directly from the memory controller */
Stephen Warren6718af02015-08-07 16:12:44 -060072static phys_size_t query_sdram_size(void)
Stephen Warren1b4af6b2014-07-02 14:12:30 -060073{
74 struct mc_ctlr *const mc = (struct mc_ctlr *)NV_PA_MC_BASE;
Stephen Warren6718af02015-08-07 16:12:44 -060075 u32 emem_cfg;
76 phys_size_t size_bytes;
Stephen Warren1b4af6b2014-07-02 14:12:30 -060077
Stephen Warren210bdb22014-12-23 10:34:50 -070078 emem_cfg = readl(&mc->mc_emem_cfg);
Marcel Ziswilerc5ecf272014-10-10 23:32:32 +020079#if defined(CONFIG_TEGRA20)
Stephen Warren210bdb22014-12-23 10:34:50 -070080 debug("mc->mc_emem_cfg (MEM_SIZE_KB) = 0x%08x\n", emem_cfg);
81 size_bytes = get_ram_size((void *)PHYS_SDRAM_1, emem_cfg * 1024);
Marcel Ziswilerc5ecf272014-10-10 23:32:32 +020082#else
Stephen Warren210bdb22014-12-23 10:34:50 -070083 debug("mc->mc_emem_cfg (MEM_SIZE_MB) = 0x%08x\n", emem_cfg);
Stephen Warren6718af02015-08-07 16:12:44 -060084#ifndef CONFIG_PHYS_64BIT
Stephen Warrenc8018052014-12-23 10:34:51 -070085 /*
86 * If >=4GB RAM is present, the byte RAM size won't fit into 32-bits
87 * and will wrap. Clip the reported size to the maximum that a 32-bit
88 * variable can represent (rounded to a page).
89 */
90 if (emem_cfg >= 4096) {
91 size_bytes = U32_MAX & ~(0x1000 - 1);
Stephen Warren6718af02015-08-07 16:12:44 -060092 } else
93#endif
94 {
Stephen Warrenc8018052014-12-23 10:34:51 -070095 /* RAM size EMC is programmed to. */
Stephen Warren6718af02015-08-07 16:12:44 -060096 size_bytes = (phys_size_t)emem_cfg * 1024 * 1024;
97#ifndef CONFIG_ARM64
Stephen Warrenc8018052014-12-23 10:34:51 -070098 /*
99 * If all RAM fits within 32-bits, it can be accessed without
100 * LPAE, so go test the RAM size. Otherwise, we can't access
101 * all the RAM, and get_ram_size() would get confused, so
102 * avoid using it. There's no reason we should need this
103 * validation step anyway.
104 */
105 if (emem_cfg <= (0 - PHYS_SDRAM_1) / (1024 * 1024))
106 size_bytes = get_ram_size((void *)PHYS_SDRAM_1,
107 size_bytes);
Stephen Warren6718af02015-08-07 16:12:44 -0600108#endif
Stephen Warrenc8018052014-12-23 10:34:51 -0700109 }
Marcel Ziswilerc5ecf272014-10-10 23:32:32 +0200110#endif
Stephen Warren1b4af6b2014-07-02 14:12:30 -0600111
Marcel Ziswilerc5ecf272014-10-10 23:32:32 +0200112#if defined(CONFIG_TEGRA30) || defined(CONFIG_TEGRA114)
113 /* External memory limited to 2047 MB due to IROM/HI-VEC */
Stephen Warren210bdb22014-12-23 10:34:50 -0700114 if (size_bytes == SZ_2G)
115 size_bytes -= SZ_1M;
Stephen Warren1b4af6b2014-07-02 14:12:30 -0600116#endif
Tom Warren41b68382011-01-27 10:58:05 +0000117
Stephen Warren210bdb22014-12-23 10:34:50 -0700118 return size_bytes;
Marcel Ziswilerc5ecf272014-10-10 23:32:32 +0200119}
120
Tom Warren41b68382011-01-27 10:58:05 +0000121int dram_init(void)
122{
Tom Warren41b68382011-01-27 10:58:05 +0000123 /* We do not initialise DRAM here. We just query the size */
Simon Glassf6fcbbd2011-11-05 03:56:57 +0000124 gd->ram_size = query_sdram_size();
Tom Warren41b68382011-01-27 10:58:05 +0000125 return 0;
126}
127
Stephen Warren59f90102012-05-14 13:13:45 +0000128static int uart_configs[] = {
Tom Warren61c6d0e2012-12-11 13:34:15 +0000129#if defined(CONFIG_TEGRA20)
130 #if defined(CONFIG_TEGRA_UARTA_UAA_UAB)
Stephen Warren59f90102012-05-14 13:13:45 +0000131 FUNCMUX_UART1_UAA_UAB,
Tom Warren61c6d0e2012-12-11 13:34:15 +0000132 #elif defined(CONFIG_TEGRA_UARTA_GPU)
Stephen Warrene4c01a82012-05-16 05:59:59 +0000133 FUNCMUX_UART1_GPU,
Tom Warren61c6d0e2012-12-11 13:34:15 +0000134 #elif defined(CONFIG_TEGRA_UARTA_SDIO1)
Lucas Stach4de6eec2012-05-16 08:21:02 +0000135 FUNCMUX_UART1_SDIO1,
Tom Warren61c6d0e2012-12-11 13:34:15 +0000136 #else
Stephen Warren59f90102012-05-14 13:13:45 +0000137 FUNCMUX_UART1_IRRX_IRTX,
Stephen Warren811af732013-01-22 06:20:08 +0000138#endif
139 FUNCMUX_UART2_UAD,
Stephen Warren59f90102012-05-14 13:13:45 +0000140 -1,
141 FUNCMUX_UART4_GMC,
142 -1,
Tom Warrene3d95bc2013-01-28 13:32:10 +0000143#elif defined(CONFIG_TEGRA30)
Tom Warren61c6d0e2012-12-11 13:34:15 +0000144 FUNCMUX_UART1_ULPI, /* UARTA */
145 -1,
146 -1,
147 -1,
148 -1,
Tom Warrene5ffffd2014-01-24 12:46:16 -0700149#elif defined(CONFIG_TEGRA114)
Tom Warrene3d95bc2013-01-28 13:32:10 +0000150 -1,
151 -1,
152 -1,
153 FUNCMUX_UART4_GMI, /* UARTD */
154 -1,
Tom Warrenab0cc6b2015-03-04 16:36:00 -0700155#elif defined(CONFIG_TEGRA124)
Tom Warrene5ffffd2014-01-24 12:46:16 -0700156 FUNCMUX_UART1_KBC, /* UARTA */
157 -1,
158 -1,
159 FUNCMUX_UART4_GPIO, /* UARTD */
160 -1,
Tom Warrenab0cc6b2015-03-04 16:36:00 -0700161#else /* Tegra210 */
162 FUNCMUX_UART1_UART1, /* UARTA */
163 -1,
164 -1,
165 FUNCMUX_UART4_UART4, /* UARTD */
166 -1,
Tom Warren61c6d0e2012-12-11 13:34:15 +0000167#endif
Stephen Warren59f90102012-05-14 13:13:45 +0000168};
169
Simon Glass96b7c432011-11-28 15:04:39 +0000170/**
171 * Set up the specified uarts
172 *
173 * @param uarts_ids Mask containing UARTs to init (UARTx)
174 */
175static void setup_uarts(int uart_ids)
176{
177 static enum periph_id id_for_uart[] = {
178 PERIPH_ID_UART1,
179 PERIPH_ID_UART2,
180 PERIPH_ID_UART3,
181 PERIPH_ID_UART4,
Tom Warrene3d95bc2013-01-28 13:32:10 +0000182 PERIPH_ID_UART5,
Simon Glass96b7c432011-11-28 15:04:39 +0000183 };
184 size_t i;
185
186 for (i = 0; i < UART_COUNT; i++) {
187 if (uart_ids & (1 << i)) {
188 enum periph_id id = id_for_uart[i];
189
Stephen Warren59f90102012-05-14 13:13:45 +0000190 funcmux_select(id, uart_configs[i]);
Simon Glass96b7c432011-11-28 15:04:39 +0000191 clock_ll_start_uart(id);
192 }
193 }
194}
195
196void board_init_uart_f(void)
197{
198 int uart_ids = 0; /* bit mask of which UART ids to enable */
199
Tom Warren22562a42012-09-04 17:00:24 -0700200#ifdef CONFIG_TEGRA_ENABLE_UARTA
Simon Glass96b7c432011-11-28 15:04:39 +0000201 uart_ids |= UARTA;
202#endif
Tom Warren22562a42012-09-04 17:00:24 -0700203#ifdef CONFIG_TEGRA_ENABLE_UARTB
Simon Glass96b7c432011-11-28 15:04:39 +0000204 uart_ids |= UARTB;
205#endif
Tom Warrene3d95bc2013-01-28 13:32:10 +0000206#ifdef CONFIG_TEGRA_ENABLE_UARTC
207 uart_ids |= UARTC;
208#endif
Tom Warren22562a42012-09-04 17:00:24 -0700209#ifdef CONFIG_TEGRA_ENABLE_UARTD
Simon Glass96b7c432011-11-28 15:04:39 +0000210 uart_ids |= UARTD;
211#endif
Tom Warrene3d95bc2013-01-28 13:32:10 +0000212#ifdef CONFIG_TEGRA_ENABLE_UARTE
213 uart_ids |= UARTE;
214#endif
Simon Glass96b7c432011-11-28 15:04:39 +0000215 setup_uarts(uart_ids);
216}
Simon Glass410012f2012-01-09 13:22:15 +0000217
Simon Glassf4402d02015-12-04 08:58:39 -0700218#if !CONFIG_IS_ENABLED(OF_CONTROL)
Thomas Choue3b90262015-11-19 21:48:11 +0800219static struct ns16550_platdata ns16550_com1_pdata = {
220 .base = CONFIG_SYS_NS16550_COM1,
221 .reg_shift = 2,
222 .clock = CONFIG_SYS_NS16550_CLK,
Heiko Schocher06f108e2017-01-18 08:05:49 +0100223 .fcr = UART_FCR_DEFVAL,
Thomas Choue3b90262015-11-19 21:48:11 +0800224};
225
226U_BOOT_DEVICE(ns16550_com1) = {
227 "ns16550_serial", &ns16550_com1_pdata
228};
229#endif
230
Trevor Woerner43ec7e02019-05-03 09:41:00 -0400231#if !CONFIG_IS_ENABLED(SYS_DCACHE_OFF) && !defined(CONFIG_ARM64)
Simon Glass410012f2012-01-09 13:22:15 +0000232void enable_caches(void)
233{
234 /* Enable D-cache. I-cache is already enabled in start.S */
235 dcache_enable();
236}
237#endif