blob: 31c49a7c84981928b30fdd5c3e0450c61d128ef1 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Tom Warren3f82b1d2011-01-27 10:58:05 +00002/*
Tom Warren7aaa5a62015-03-04 16:36:00 -07003 * (C) Copyright 2010-2015
Tom Warren3f82b1d2011-01-27 10:58:05 +00004 * NVIDIA Corporation <www.nvidia.com>
Tom Warren3f82b1d2011-01-27 10:58:05 +00005 */
6
7#include <common.h>
Simon Glass9edefc22019-11-14 12:57:37 -07008#include <cpu_func.h>
Thomas Chou18746262015-11-19 21:48:11 +08009#include <dm.h>
Simon Glass9b4a2052019-12-28 10:45:05 -070010#include <init.h>
Thomas Chou18746262015-11-19 21:48:11 +080011#include <ns16550.h>
Simon Glass537e9672015-05-13 07:02:29 -060012#include <spl.h>
Tom Warren3f82b1d2011-01-27 10:58:05 +000013#include <asm/io.h>
Thierry Redingb64e0b92019-04-15 11:32:18 +020014#if IS_ENABLED(CONFIG_TEGRA_CLKRST)
Simon Glassbb6997f2011-11-28 15:04:39 +000015#include <asm/arch/clock.h>
Thierry Redingb64e0b92019-04-15 11:32:18 +020016#endif
Thierry Reding07ea02b2019-04-15 11:32:21 +020017#if IS_ENABLED(CONFIG_TEGRA_PINCTRL)
Simon Glassbb6997f2011-11-28 15:04:39 +000018#include <asm/arch/funcmux.h>
Thierry Reding07ea02b2019-04-15 11:32:21 +020019#endif
Thierry Reding1a869c72019-04-15 11:32:20 +020020#if IS_ENABLED(CONFIG_TEGRA_MC)
Marcel Ziswiler8c33ba72014-10-10 23:32:32 +020021#include <asm/arch/mc.h>
Thierry Reding1a869c72019-04-15 11:32:20 +020022#endif
Tom Warren150c2492012-09-19 15:50:56 -070023#include <asm/arch/tegra.h>
Stephen Warren73c38932015-01-19 16:25:52 -070024#include <asm/arch-tegra/ap.h>
Lucas Stach516f00b2012-09-29 10:02:08 +000025#include <asm/arch-tegra/board.h>
Thierry Redinga0dbc132019-04-15 11:32:28 +020026#include <asm/arch-tegra/cboot.h>
Tom Warren150c2492012-09-19 15:50:56 -070027#include <asm/arch-tegra/pmc.h>
28#include <asm/arch-tegra/sys_proto.h>
29#include <asm/arch-tegra/warmboot.h>
Tom Warren3f82b1d2011-01-27 10:58:05 +000030
Tom Warren659a0752015-07-08 08:05:35 -070031void save_boot_params_ret(void);
32
Tom Warren3f82b1d2011-01-27 10:58:05 +000033DECLARE_GLOBAL_DATA_PTR;
34
Simon Glassbb6997f2011-11-28 15:04:39 +000035enum {
36 /* UARTs which we can enable */
37 UARTA = 1 << 0,
38 UARTB = 1 << 1,
Tom Warrene23bb6a2013-01-28 13:32:10 +000039 UARTC = 1 << 2,
Simon Glassbb6997f2011-11-28 15:04:39 +000040 UARTD = 1 << 3,
Tom Warrene23bb6a2013-01-28 13:32:10 +000041 UARTE = 1 << 4,
42 UART_COUNT = 5,
Simon Glassbb6997f2011-11-28 15:04:39 +000043};
44
Simon Glass537e9672015-05-13 07:02:29 -060045static bool from_spl __attribute__ ((section(".data")));
46
47#ifndef CONFIG_SPL_BUILD
Thierry Reding8f60d182019-04-15 11:32:23 +020048void save_boot_params(unsigned long r0, unsigned long r1, unsigned long r2,
49 unsigned long r3)
Simon Glass537e9672015-05-13 07:02:29 -060050{
51 from_spl = r0 != UBOOT_NOT_LOADED_FROM_SPL;
Thierry Redinga0dbc132019-04-15 11:32:28 +020052
53 /*
54 * The logic for this is somewhat indirect. The purpose of the marker
55 * (UBOOT_NOT_LOADED_FROM_SPL) is in fact used to determine if U-Boot
56 * was loaded from a read-only instance of itself, which is something
57 * that can happen in secure boot setups. So basically the presence
58 * of the marker is an indication that U-Boot was loaded by one such
59 * special variant of U-Boot. Conversely, the absence of the marker
60 * indicates that this instance of U-Boot was loaded by something
61 * other than a special U-Boot. This could be SPL, but it could just
62 * as well be one of any number of other first stage bootloaders.
63 */
64 if (from_spl)
65 cboot_save_boot_params(r0, r1, r2, r3);
66
Simon Glass537e9672015-05-13 07:02:29 -060067 save_boot_params_ret();
68}
69#endif
70
71bool spl_was_boot_source(void)
72{
73 return from_spl;
74}
75
Stephen Warren73c38932015-01-19 16:25:52 -070076#if defined(CONFIG_TEGRA_SUPPORT_NON_SECURE)
77#if !defined(CONFIG_TEGRA124)
78#error tegra_cpu_is_non_secure has only been validated on Tegra124
79#endif
80bool tegra_cpu_is_non_secure(void)
81{
82 /*
83 * This register reads 0xffffffff in non-secure mode. This register
84 * only implements bits 31:20, so the lower bits will always read 0 in
85 * secure mode. Thus, the lower bits are an indicator for secure vs.
86 * non-secure mode.
87 */
88 struct mc_ctlr *mc = (struct mc_ctlr *)NV_PA_MC_BASE;
89 uint32_t mc_s_cfg0 = readl(&mc->mc_security_cfg0);
90 return (mc_s_cfg0 & 1) == 1;
91}
92#endif
93
Thierry Reding1a869c72019-04-15 11:32:20 +020094#if IS_ENABLED(CONFIG_TEGRA_MC)
Stephen Warrenaeb3fcb2014-07-02 14:12:30 -060095/* Read the RAM size directly from the memory controller */
Stephen Warrena5fc3d02015-08-07 16:12:44 -060096static phys_size_t query_sdram_size(void)
Stephen Warrenaeb3fcb2014-07-02 14:12:30 -060097{
98 struct mc_ctlr *const mc = (struct mc_ctlr *)NV_PA_MC_BASE;
Stephen Warrena5fc3d02015-08-07 16:12:44 -060099 u32 emem_cfg;
100 phys_size_t size_bytes;
Stephen Warrenaeb3fcb2014-07-02 14:12:30 -0600101
Stephen Warren3a2cab52014-12-23 10:34:50 -0700102 emem_cfg = readl(&mc->mc_emem_cfg);
Marcel Ziswiler8c33ba72014-10-10 23:32:32 +0200103#if defined(CONFIG_TEGRA20)
Stephen Warren3a2cab52014-12-23 10:34:50 -0700104 debug("mc->mc_emem_cfg (MEM_SIZE_KB) = 0x%08x\n", emem_cfg);
105 size_bytes = get_ram_size((void *)PHYS_SDRAM_1, emem_cfg * 1024);
Marcel Ziswiler8c33ba72014-10-10 23:32:32 +0200106#else
Stephen Warren3a2cab52014-12-23 10:34:50 -0700107 debug("mc->mc_emem_cfg (MEM_SIZE_MB) = 0x%08x\n", emem_cfg);
Stephen Warrena5fc3d02015-08-07 16:12:44 -0600108#ifndef CONFIG_PHYS_64BIT
Stephen Warren56519c42014-12-23 10:34:51 -0700109 /*
110 * If >=4GB RAM is present, the byte RAM size won't fit into 32-bits
111 * and will wrap. Clip the reported size to the maximum that a 32-bit
112 * variable can represent (rounded to a page).
113 */
114 if (emem_cfg >= 4096) {
115 size_bytes = U32_MAX & ~(0x1000 - 1);
Stephen Warrena5fc3d02015-08-07 16:12:44 -0600116 } else
117#endif
118 {
Stephen Warren56519c42014-12-23 10:34:51 -0700119 /* RAM size EMC is programmed to. */
Stephen Warrena5fc3d02015-08-07 16:12:44 -0600120 size_bytes = (phys_size_t)emem_cfg * 1024 * 1024;
121#ifndef CONFIG_ARM64
Stephen Warren56519c42014-12-23 10:34:51 -0700122 /*
123 * If all RAM fits within 32-bits, it can be accessed without
124 * LPAE, so go test the RAM size. Otherwise, we can't access
125 * all the RAM, and get_ram_size() would get confused, so
126 * avoid using it. There's no reason we should need this
127 * validation step anyway.
128 */
129 if (emem_cfg <= (0 - PHYS_SDRAM_1) / (1024 * 1024))
130 size_bytes = get_ram_size((void *)PHYS_SDRAM_1,
131 size_bytes);
Stephen Warrena5fc3d02015-08-07 16:12:44 -0600132#endif
Stephen Warren56519c42014-12-23 10:34:51 -0700133 }
Stephen Warrenaeb3fcb2014-07-02 14:12:30 -0600134#endif
Tom Warren3f82b1d2011-01-27 10:58:05 +0000135
Marcel Ziswiler8c33ba72014-10-10 23:32:32 +0200136#if defined(CONFIG_TEGRA30) || defined(CONFIG_TEGRA114)
137 /* External memory limited to 2047 MB due to IROM/HI-VEC */
Stephen Warren3a2cab52014-12-23 10:34:50 -0700138 if (size_bytes == SZ_2G)
139 size_bytes -= SZ_1M;
Marcel Ziswiler8c33ba72014-10-10 23:32:32 +0200140#endif
141
Stephen Warren3a2cab52014-12-23 10:34:50 -0700142 return size_bytes;
Marcel Ziswiler8c33ba72014-10-10 23:32:32 +0200143}
Thierry Reding1a869c72019-04-15 11:32:20 +0200144#endif
Marcel Ziswiler8c33ba72014-10-10 23:32:32 +0200145
Tom Warren3f82b1d2011-01-27 10:58:05 +0000146int dram_init(void)
147{
Thierry Redinga0dbc132019-04-15 11:32:28 +0200148 int err;
149
150 /* try to initialize DRAM from cboot DTB first */
151 err = cboot_dram_init();
152 if (err == 0)
153 return 0;
154
Thierry Reding1a869c72019-04-15 11:32:20 +0200155#if IS_ENABLED(CONFIG_TEGRA_MC)
Tom Warren3f82b1d2011-01-27 10:58:05 +0000156 /* We do not initialise DRAM here. We just query the size */
Simon Glass7f8c0702011-11-05 03:56:57 +0000157 gd->ram_size = query_sdram_size();
Thierry Reding1a869c72019-04-15 11:32:20 +0200158#endif
159
Tom Warren3f82b1d2011-01-27 10:58:05 +0000160 return 0;
161}
162
Thierry Reding07ea02b2019-04-15 11:32:21 +0200163#if IS_ENABLED(CONFIG_TEGRA_PINCTRL)
Stephen Warrenb9607e72012-05-14 13:13:45 +0000164static int uart_configs[] = {
Tom Warrenb2871032012-12-11 13:34:15 +0000165#if defined(CONFIG_TEGRA20)
166 #if defined(CONFIG_TEGRA_UARTA_UAA_UAB)
Stephen Warrenb9607e72012-05-14 13:13:45 +0000167 FUNCMUX_UART1_UAA_UAB,
Tom Warrenb2871032012-12-11 13:34:15 +0000168 #elif defined(CONFIG_TEGRA_UARTA_GPU)
Stephen Warrene21649b2012-05-16 05:59:59 +0000169 FUNCMUX_UART1_GPU,
Tom Warrenb2871032012-12-11 13:34:15 +0000170 #elif defined(CONFIG_TEGRA_UARTA_SDIO1)
Lucas Stacha2cfe632012-05-16 08:21:02 +0000171 FUNCMUX_UART1_SDIO1,
Tom Warrenb2871032012-12-11 13:34:15 +0000172 #else
Stephen Warrenb9607e72012-05-14 13:13:45 +0000173 FUNCMUX_UART1_IRRX_IRTX,
Stephen Warren4727a132013-01-22 06:20:08 +0000174#endif
175 FUNCMUX_UART2_UAD,
Stephen Warrenb9607e72012-05-14 13:13:45 +0000176 -1,
177 FUNCMUX_UART4_GMC,
178 -1,
Tom Warrene23bb6a2013-01-28 13:32:10 +0000179#elif defined(CONFIG_TEGRA30)
Tom Warrenb2871032012-12-11 13:34:15 +0000180 FUNCMUX_UART1_ULPI, /* UARTA */
181 -1,
182 -1,
183 -1,
184 -1,
Tom Warren2f5dac92014-01-24 12:46:16 -0700185#elif defined(CONFIG_TEGRA114)
Tom Warrene23bb6a2013-01-28 13:32:10 +0000186 -1,
187 -1,
188 -1,
189 FUNCMUX_UART4_GMI, /* UARTD */
190 -1,
Tom Warren7aaa5a62015-03-04 16:36:00 -0700191#elif defined(CONFIG_TEGRA124)
Tom Warren2f5dac92014-01-24 12:46:16 -0700192 FUNCMUX_UART1_KBC, /* UARTA */
193 -1,
194 -1,
195 FUNCMUX_UART4_GPIO, /* UARTD */
196 -1,
Tom Warren7aaa5a62015-03-04 16:36:00 -0700197#else /* Tegra210 */
198 FUNCMUX_UART1_UART1, /* UARTA */
199 -1,
200 -1,
201 FUNCMUX_UART4_UART4, /* UARTD */
202 -1,
Tom Warrenb2871032012-12-11 13:34:15 +0000203#endif
Stephen Warrenb9607e72012-05-14 13:13:45 +0000204};
205
Simon Glassbb6997f2011-11-28 15:04:39 +0000206/**
207 * Set up the specified uarts
208 *
209 * @param uarts_ids Mask containing UARTs to init (UARTx)
210 */
211static void setup_uarts(int uart_ids)
212{
213 static enum periph_id id_for_uart[] = {
214 PERIPH_ID_UART1,
215 PERIPH_ID_UART2,
216 PERIPH_ID_UART3,
217 PERIPH_ID_UART4,
Tom Warrene23bb6a2013-01-28 13:32:10 +0000218 PERIPH_ID_UART5,
Simon Glassbb6997f2011-11-28 15:04:39 +0000219 };
220 size_t i;
221
222 for (i = 0; i < UART_COUNT; i++) {
223 if (uart_ids & (1 << i)) {
224 enum periph_id id = id_for_uart[i];
225
Stephen Warrenb9607e72012-05-14 13:13:45 +0000226 funcmux_select(id, uart_configs[i]);
Simon Glassbb6997f2011-11-28 15:04:39 +0000227 clock_ll_start_uart(id);
228 }
229 }
230}
Thierry Reding07ea02b2019-04-15 11:32:21 +0200231#endif
Simon Glassbb6997f2011-11-28 15:04:39 +0000232
233void board_init_uart_f(void)
234{
Thierry Reding07ea02b2019-04-15 11:32:21 +0200235#if IS_ENABLED(CONFIG_TEGRA_PINCTRL)
Simon Glassbb6997f2011-11-28 15:04:39 +0000236 int uart_ids = 0; /* bit mask of which UART ids to enable */
237
Tom Warren29f3e3f2012-09-04 17:00:24 -0700238#ifdef CONFIG_TEGRA_ENABLE_UARTA
Simon Glassbb6997f2011-11-28 15:04:39 +0000239 uart_ids |= UARTA;
240#endif
Tom Warren29f3e3f2012-09-04 17:00:24 -0700241#ifdef CONFIG_TEGRA_ENABLE_UARTB
Simon Glassbb6997f2011-11-28 15:04:39 +0000242 uart_ids |= UARTB;
243#endif
Tom Warrene23bb6a2013-01-28 13:32:10 +0000244#ifdef CONFIG_TEGRA_ENABLE_UARTC
245 uart_ids |= UARTC;
246#endif
Tom Warren29f3e3f2012-09-04 17:00:24 -0700247#ifdef CONFIG_TEGRA_ENABLE_UARTD
Simon Glassbb6997f2011-11-28 15:04:39 +0000248 uart_ids |= UARTD;
249#endif
Tom Warrene23bb6a2013-01-28 13:32:10 +0000250#ifdef CONFIG_TEGRA_ENABLE_UARTE
251 uart_ids |= UARTE;
252#endif
Simon Glassbb6997f2011-11-28 15:04:39 +0000253 setup_uarts(uart_ids);
Thierry Reding07ea02b2019-04-15 11:32:21 +0200254#endif
Simon Glassbb6997f2011-11-28 15:04:39 +0000255}
Simon Glassbd29cb02012-01-09 13:22:15 +0000256
Simon Glass878a3ed2015-12-04 08:58:39 -0700257#if !CONFIG_IS_ENABLED(OF_CONTROL)
Thomas Chou18746262015-11-19 21:48:11 +0800258static struct ns16550_platdata ns16550_com1_pdata = {
259 .base = CONFIG_SYS_NS16550_COM1,
260 .reg_shift = 2,
261 .clock = CONFIG_SYS_NS16550_CLK,
Heiko Schocher17fa0322017-01-18 08:05:49 +0100262 .fcr = UART_FCR_DEFVAL,
Thomas Chou18746262015-11-19 21:48:11 +0800263};
264
265U_BOOT_DEVICE(ns16550_com1) = {
266 "ns16550_serial", &ns16550_com1_pdata
267};
268#endif
269
Trevor Woerner10015022019-05-03 09:41:00 -0400270#if !CONFIG_IS_ENABLED(SYS_DCACHE_OFF) && !defined(CONFIG_ARM64)
Simon Glassbd29cb02012-01-09 13:22:15 +0000271void enable_caches(void)
272{
273 /* Enable D-cache. I-cache is already enabled in start.S */
274 dcache_enable();
275}
276#endif