blob: bf01aa5ee8fb155091c20b141ee939177039cc77 [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>
Simon Glass1d91ba72019-11-14 12:57:37 -07008#include <cpu_func.h>
Thomas Choue3b90262015-11-19 21:48:11 +08009#include <dm.h>
Simon Glass8e16b1e2019-12-28 10:45:05 -070010#include <init.h>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
Thomas Choue3b90262015-11-19 21:48:11 +080012#include <ns16550.h>
Simon Glasseec13c42015-05-13 07:02:29 -060013#include <spl.h>
Simon Glass274e0b02020-05-10 11:39:56 -060014#include <asm/cache.h>
Tom Warren41b68382011-01-27 10:58:05 +000015#include <asm/io.h>
Thierry Reding45ad0b02019-04-15 11:32:18 +020016#if IS_ENABLED(CONFIG_TEGRA_CLKRST)
Simon Glass96b7c432011-11-28 15:04:39 +000017#include <asm/arch/clock.h>
Thierry Reding45ad0b02019-04-15 11:32:18 +020018#endif
Thierry Reding7c0b1502019-04-15 11:32:21 +020019#if IS_ENABLED(CONFIG_TEGRA_PINCTRL)
Simon Glass96b7c432011-11-28 15:04:39 +000020#include <asm/arch/funcmux.h>
Thierry Reding7c0b1502019-04-15 11:32:21 +020021#endif
Thierry Reding17987bb2019-04-15 11:32:20 +020022#if IS_ENABLED(CONFIG_TEGRA_MC)
Marcel Ziswilerc5ecf272014-10-10 23:32:32 +020023#include <asm/arch/mc.h>
Thierry Reding17987bb2019-04-15 11:32:20 +020024#endif
Tom Warrenab371962012-09-19 15:50:56 -070025#include <asm/arch/tegra.h>
Stephen Warren8d1fb312015-01-19 16:25:52 -070026#include <asm/arch-tegra/ap.h>
Lucas Stache80f7ca2012-09-29 10:02:08 +000027#include <asm/arch-tegra/board.h>
Thierry Reding7cef2b22019-04-15 11:32:28 +020028#include <asm/arch-tegra/cboot.h>
Tom Warrenab371962012-09-19 15:50:56 -070029#include <asm/arch-tegra/pmc.h>
30#include <asm/arch-tegra/sys_proto.h>
31#include <asm/arch-tegra/warmboot.h>
Tom Warren41b68382011-01-27 10:58:05 +000032
Tom Warren021a8bb2015-07-08 08:05:35 -070033void save_boot_params_ret(void);
34
Tom Warren41b68382011-01-27 10:58:05 +000035DECLARE_GLOBAL_DATA_PTR;
36
Simon Glass96b7c432011-11-28 15:04:39 +000037enum {
38 /* UARTs which we can enable */
39 UARTA = 1 << 0,
40 UARTB = 1 << 1,
Tom Warrene3d95bc2013-01-28 13:32:10 +000041 UARTC = 1 << 2,
Simon Glass96b7c432011-11-28 15:04:39 +000042 UARTD = 1 << 3,
Tom Warrene3d95bc2013-01-28 13:32:10 +000043 UARTE = 1 << 4,
44 UART_COUNT = 5,
Simon Glass96b7c432011-11-28 15:04:39 +000045};
46
Simon Glasseec13c42015-05-13 07:02:29 -060047static bool from_spl __attribute__ ((section(".data")));
48
49#ifndef CONFIG_SPL_BUILD
Thierry Redingf6270a62019-04-15 11:32:23 +020050void save_boot_params(unsigned long r0, unsigned long r1, unsigned long r2,
51 unsigned long r3)
Simon Glasseec13c42015-05-13 07:02:29 -060052{
53 from_spl = r0 != UBOOT_NOT_LOADED_FROM_SPL;
Thierry Reding7cef2b22019-04-15 11:32:28 +020054
55 /*
56 * The logic for this is somewhat indirect. The purpose of the marker
57 * (UBOOT_NOT_LOADED_FROM_SPL) is in fact used to determine if U-Boot
58 * was loaded from a read-only instance of itself, which is something
59 * that can happen in secure boot setups. So basically the presence
60 * of the marker is an indication that U-Boot was loaded by one such
61 * special variant of U-Boot. Conversely, the absence of the marker
62 * indicates that this instance of U-Boot was loaded by something
63 * other than a special U-Boot. This could be SPL, but it could just
64 * as well be one of any number of other first stage bootloaders.
65 */
66 if (from_spl)
67 cboot_save_boot_params(r0, r1, r2, r3);
68
Simon Glasseec13c42015-05-13 07:02:29 -060069 save_boot_params_ret();
70}
71#endif
72
73bool spl_was_boot_source(void)
74{
75 return from_spl;
76}
77
Stephen Warren8d1fb312015-01-19 16:25:52 -070078#if defined(CONFIG_TEGRA_SUPPORT_NON_SECURE)
79#if !defined(CONFIG_TEGRA124)
80#error tegra_cpu_is_non_secure has only been validated on Tegra124
81#endif
82bool tegra_cpu_is_non_secure(void)
83{
84 /*
85 * This register reads 0xffffffff in non-secure mode. This register
86 * only implements bits 31:20, so the lower bits will always read 0 in
87 * secure mode. Thus, the lower bits are an indicator for secure vs.
88 * non-secure mode.
89 */
90 struct mc_ctlr *mc = (struct mc_ctlr *)NV_PA_MC_BASE;
91 uint32_t mc_s_cfg0 = readl(&mc->mc_security_cfg0);
92 return (mc_s_cfg0 & 1) == 1;
93}
94#endif
95
Thierry Reding17987bb2019-04-15 11:32:20 +020096#if IS_ENABLED(CONFIG_TEGRA_MC)
Stephen Warren1b4af6b2014-07-02 14:12:30 -060097/* Read the RAM size directly from the memory controller */
Stephen Warren6718af02015-08-07 16:12:44 -060098static phys_size_t query_sdram_size(void)
Stephen Warren1b4af6b2014-07-02 14:12:30 -060099{
100 struct mc_ctlr *const mc = (struct mc_ctlr *)NV_PA_MC_BASE;
Stephen Warren6718af02015-08-07 16:12:44 -0600101 u32 emem_cfg;
102 phys_size_t size_bytes;
Stephen Warren1b4af6b2014-07-02 14:12:30 -0600103
Stephen Warren210bdb22014-12-23 10:34:50 -0700104 emem_cfg = readl(&mc->mc_emem_cfg);
Marcel Ziswilerc5ecf272014-10-10 23:32:32 +0200105#if defined(CONFIG_TEGRA20)
Stephen Warren210bdb22014-12-23 10:34:50 -0700106 debug("mc->mc_emem_cfg (MEM_SIZE_KB) = 0x%08x\n", emem_cfg);
107 size_bytes = get_ram_size((void *)PHYS_SDRAM_1, emem_cfg * 1024);
Marcel Ziswilerc5ecf272014-10-10 23:32:32 +0200108#else
Stephen Warren210bdb22014-12-23 10:34:50 -0700109 debug("mc->mc_emem_cfg (MEM_SIZE_MB) = 0x%08x\n", emem_cfg);
Stephen Warren6718af02015-08-07 16:12:44 -0600110#ifndef CONFIG_PHYS_64BIT
Stephen Warrenc8018052014-12-23 10:34:51 -0700111 /*
112 * If >=4GB RAM is present, the byte RAM size won't fit into 32-bits
113 * and will wrap. Clip the reported size to the maximum that a 32-bit
114 * variable can represent (rounded to a page).
115 */
116 if (emem_cfg >= 4096) {
117 size_bytes = U32_MAX & ~(0x1000 - 1);
Stephen Warren6718af02015-08-07 16:12:44 -0600118 } else
119#endif
120 {
Stephen Warrenc8018052014-12-23 10:34:51 -0700121 /* RAM size EMC is programmed to. */
Stephen Warren6718af02015-08-07 16:12:44 -0600122 size_bytes = (phys_size_t)emem_cfg * 1024 * 1024;
123#ifndef CONFIG_ARM64
Stephen Warrenc8018052014-12-23 10:34:51 -0700124 /*
125 * If all RAM fits within 32-bits, it can be accessed without
126 * LPAE, so go test the RAM size. Otherwise, we can't access
127 * all the RAM, and get_ram_size() would get confused, so
128 * avoid using it. There's no reason we should need this
129 * validation step anyway.
130 */
131 if (emem_cfg <= (0 - PHYS_SDRAM_1) / (1024 * 1024))
132 size_bytes = get_ram_size((void *)PHYS_SDRAM_1,
133 size_bytes);
Stephen Warren6718af02015-08-07 16:12:44 -0600134#endif
Stephen Warrenc8018052014-12-23 10:34:51 -0700135 }
Marcel Ziswilerc5ecf272014-10-10 23:32:32 +0200136#endif
Stephen Warren1b4af6b2014-07-02 14:12:30 -0600137
Marcel Ziswilerc5ecf272014-10-10 23:32:32 +0200138#if defined(CONFIG_TEGRA30) || defined(CONFIG_TEGRA114)
139 /* External memory limited to 2047 MB due to IROM/HI-VEC */
Stephen Warren210bdb22014-12-23 10:34:50 -0700140 if (size_bytes == SZ_2G)
141 size_bytes -= SZ_1M;
Stephen Warren1b4af6b2014-07-02 14:12:30 -0600142#endif
Tom Warren41b68382011-01-27 10:58:05 +0000143
Stephen Warren210bdb22014-12-23 10:34:50 -0700144 return size_bytes;
Marcel Ziswilerc5ecf272014-10-10 23:32:32 +0200145}
Thierry Reding17987bb2019-04-15 11:32:20 +0200146#endif
Marcel Ziswilerc5ecf272014-10-10 23:32:32 +0200147
Tom Warren41b68382011-01-27 10:58:05 +0000148int dram_init(void)
149{
Thierry Reding7cef2b22019-04-15 11:32:28 +0200150 int err;
151
152 /* try to initialize DRAM from cboot DTB first */
153 err = cboot_dram_init();
154 if (err == 0)
155 return 0;
156
Thierry Reding17987bb2019-04-15 11:32:20 +0200157#if IS_ENABLED(CONFIG_TEGRA_MC)
Tom Warren41b68382011-01-27 10:58:05 +0000158 /* We do not initialise DRAM here. We just query the size */
Simon Glassf6fcbbd2011-11-05 03:56:57 +0000159 gd->ram_size = query_sdram_size();
Thierry Reding17987bb2019-04-15 11:32:20 +0200160#endif
161
Tom Warren41b68382011-01-27 10:58:05 +0000162 return 0;
163}
164
Thierry Reding7c0b1502019-04-15 11:32:21 +0200165#if IS_ENABLED(CONFIG_TEGRA_PINCTRL)
Stephen Warren59f90102012-05-14 13:13:45 +0000166static int uart_configs[] = {
Tom Warren61c6d0e2012-12-11 13:34:15 +0000167#if defined(CONFIG_TEGRA20)
168 #if defined(CONFIG_TEGRA_UARTA_UAA_UAB)
Stephen Warren59f90102012-05-14 13:13:45 +0000169 FUNCMUX_UART1_UAA_UAB,
Tom Warren61c6d0e2012-12-11 13:34:15 +0000170 #elif defined(CONFIG_TEGRA_UARTA_GPU)
Stephen Warrene4c01a82012-05-16 05:59:59 +0000171 FUNCMUX_UART1_GPU,
Tom Warren61c6d0e2012-12-11 13:34:15 +0000172 #elif defined(CONFIG_TEGRA_UARTA_SDIO1)
Lucas Stach4de6eec2012-05-16 08:21:02 +0000173 FUNCMUX_UART1_SDIO1,
Tom Warren61c6d0e2012-12-11 13:34:15 +0000174 #else
Stephen Warren59f90102012-05-14 13:13:45 +0000175 FUNCMUX_UART1_IRRX_IRTX,
Stephen Warren811af732013-01-22 06:20:08 +0000176#endif
177 FUNCMUX_UART2_UAD,
Stephen Warren59f90102012-05-14 13:13:45 +0000178 -1,
179 FUNCMUX_UART4_GMC,
180 -1,
Tom Warrene3d95bc2013-01-28 13:32:10 +0000181#elif defined(CONFIG_TEGRA30)
Tom Warren61c6d0e2012-12-11 13:34:15 +0000182 FUNCMUX_UART1_ULPI, /* UARTA */
183 -1,
184 -1,
185 -1,
186 -1,
Tom Warrene5ffffd2014-01-24 12:46:16 -0700187#elif defined(CONFIG_TEGRA114)
Tom Warrene3d95bc2013-01-28 13:32:10 +0000188 -1,
189 -1,
190 -1,
191 FUNCMUX_UART4_GMI, /* UARTD */
192 -1,
Tom Warrenab0cc6b2015-03-04 16:36:00 -0700193#elif defined(CONFIG_TEGRA124)
Tom Warrene5ffffd2014-01-24 12:46:16 -0700194 FUNCMUX_UART1_KBC, /* UARTA */
195 -1,
196 -1,
197 FUNCMUX_UART4_GPIO, /* UARTD */
198 -1,
Tom Warrenab0cc6b2015-03-04 16:36:00 -0700199#else /* Tegra210 */
200 FUNCMUX_UART1_UART1, /* UARTA */
201 -1,
202 -1,
203 FUNCMUX_UART4_UART4, /* UARTD */
204 -1,
Tom Warren61c6d0e2012-12-11 13:34:15 +0000205#endif
Stephen Warren59f90102012-05-14 13:13:45 +0000206};
207
Simon Glass96b7c432011-11-28 15:04:39 +0000208/**
209 * Set up the specified uarts
210 *
211 * @param uarts_ids Mask containing UARTs to init (UARTx)
212 */
213static void setup_uarts(int uart_ids)
214{
215 static enum periph_id id_for_uart[] = {
216 PERIPH_ID_UART1,
217 PERIPH_ID_UART2,
218 PERIPH_ID_UART3,
219 PERIPH_ID_UART4,
Tom Warrene3d95bc2013-01-28 13:32:10 +0000220 PERIPH_ID_UART5,
Simon Glass96b7c432011-11-28 15:04:39 +0000221 };
222 size_t i;
223
224 for (i = 0; i < UART_COUNT; i++) {
225 if (uart_ids & (1 << i)) {
226 enum periph_id id = id_for_uart[i];
227
Stephen Warren59f90102012-05-14 13:13:45 +0000228 funcmux_select(id, uart_configs[i]);
Simon Glass96b7c432011-11-28 15:04:39 +0000229 clock_ll_start_uart(id);
230 }
231 }
232}
Thierry Reding7c0b1502019-04-15 11:32:21 +0200233#endif
Simon Glass96b7c432011-11-28 15:04:39 +0000234
235void board_init_uart_f(void)
236{
Thierry Reding7c0b1502019-04-15 11:32:21 +0200237#if IS_ENABLED(CONFIG_TEGRA_PINCTRL)
Simon Glass96b7c432011-11-28 15:04:39 +0000238 int uart_ids = 0; /* bit mask of which UART ids to enable */
239
Tom Warren22562a42012-09-04 17:00:24 -0700240#ifdef CONFIG_TEGRA_ENABLE_UARTA
Simon Glass96b7c432011-11-28 15:04:39 +0000241 uart_ids |= UARTA;
242#endif
Tom Warren22562a42012-09-04 17:00:24 -0700243#ifdef CONFIG_TEGRA_ENABLE_UARTB
Simon Glass96b7c432011-11-28 15:04:39 +0000244 uart_ids |= UARTB;
245#endif
Tom Warrene3d95bc2013-01-28 13:32:10 +0000246#ifdef CONFIG_TEGRA_ENABLE_UARTC
247 uart_ids |= UARTC;
248#endif
Tom Warren22562a42012-09-04 17:00:24 -0700249#ifdef CONFIG_TEGRA_ENABLE_UARTD
Simon Glass96b7c432011-11-28 15:04:39 +0000250 uart_ids |= UARTD;
251#endif
Tom Warrene3d95bc2013-01-28 13:32:10 +0000252#ifdef CONFIG_TEGRA_ENABLE_UARTE
253 uart_ids |= UARTE;
254#endif
Simon Glass96b7c432011-11-28 15:04:39 +0000255 setup_uarts(uart_ids);
Thierry Reding7c0b1502019-04-15 11:32:21 +0200256#endif
Simon Glass96b7c432011-11-28 15:04:39 +0000257}
Simon Glass410012f2012-01-09 13:22:15 +0000258
Simon Glassf4402d02015-12-04 08:58:39 -0700259#if !CONFIG_IS_ENABLED(OF_CONTROL)
Simon Glassb75b15b2020-12-03 16:55:23 -0700260static struct ns16550_plat ns16550_com1_pdata = {
Thomas Choue3b90262015-11-19 21:48:11 +0800261 .base = CONFIG_SYS_NS16550_COM1,
262 .reg_shift = 2,
263 .clock = CONFIG_SYS_NS16550_CLK,
Heiko Schocher06f108e2017-01-18 08:05:49 +0100264 .fcr = UART_FCR_DEFVAL,
Thomas Choue3b90262015-11-19 21:48:11 +0800265};
266
267U_BOOT_DEVICE(ns16550_com1) = {
268 "ns16550_serial", &ns16550_com1_pdata
269};
270#endif
271
Trevor Woerner43ec7e02019-05-03 09:41:00 -0400272#if !CONFIG_IS_ENABLED(SYS_DCACHE_OFF) && !defined(CONFIG_ARM64)
Simon Glass410012f2012-01-09 13:22:15 +0000273void enable_caches(void)
274{
275 /* Enable D-cache. I-cache is already enabled in start.S */
276 dcache_enable();
277}
278#endif