blob: 03faacbd577227a147368889b8878601c25efbd6 [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>
Simon Glassf7ae49f2020-05-10 11:40:05 -060011#include <log.h>
Thomas Chou18746262015-11-19 21:48:11 +080012#include <ns16550.h>
Simon Glass537e9672015-05-13 07:02:29 -060013#include <spl.h>
Simon Glass90526e92020-05-10 11:39:56 -060014#include <asm/cache.h>
Tom Warren3f82b1d2011-01-27 10:58:05 +000015#include <asm/io.h>
Thierry Redingb64e0b92019-04-15 11:32:18 +020016#if IS_ENABLED(CONFIG_TEGRA_CLKRST)
Simon Glassbb6997f2011-11-28 15:04:39 +000017#include <asm/arch/clock.h>
Thierry Redingb64e0b92019-04-15 11:32:18 +020018#endif
Thierry Reding07ea02b2019-04-15 11:32:21 +020019#if IS_ENABLED(CONFIG_TEGRA_PINCTRL)
Simon Glassbb6997f2011-11-28 15:04:39 +000020#include <asm/arch/funcmux.h>
Thierry Reding07ea02b2019-04-15 11:32:21 +020021#endif
Thierry Reding1a869c72019-04-15 11:32:20 +020022#if IS_ENABLED(CONFIG_TEGRA_MC)
Marcel Ziswiler8c33ba72014-10-10 23:32:32 +020023#include <asm/arch/mc.h>
Thierry Reding1a869c72019-04-15 11:32:20 +020024#endif
Tom Warren150c2492012-09-19 15:50:56 -070025#include <asm/arch/tegra.h>
Stephen Warren73c38932015-01-19 16:25:52 -070026#include <asm/arch-tegra/ap.h>
Lucas Stach516f00b2012-09-29 10:02:08 +000027#include <asm/arch-tegra/board.h>
Thierry Redinga0dbc132019-04-15 11:32:28 +020028#include <asm/arch-tegra/cboot.h>
Tom Warren150c2492012-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 Warren3f82b1d2011-01-27 10:58:05 +000032
Tom Warren659a0752015-07-08 08:05:35 -070033void save_boot_params_ret(void);
34
Tom Warren3f82b1d2011-01-27 10:58:05 +000035DECLARE_GLOBAL_DATA_PTR;
36
Simon Glassbb6997f2011-11-28 15:04:39 +000037enum {
38 /* UARTs which we can enable */
39 UARTA = 1 << 0,
40 UARTB = 1 << 1,
Tom Warrene23bb6a2013-01-28 13:32:10 +000041 UARTC = 1 << 2,
Simon Glassbb6997f2011-11-28 15:04:39 +000042 UARTD = 1 << 3,
Tom Warrene23bb6a2013-01-28 13:32:10 +000043 UARTE = 1 << 4,
44 UART_COUNT = 5,
Simon Glassbb6997f2011-11-28 15:04:39 +000045};
46
Simon Glass537e9672015-05-13 07:02:29 -060047static bool from_spl __attribute__ ((section(".data")));
48
49#ifndef CONFIG_SPL_BUILD
Thierry Reding8f60d182019-04-15 11:32:23 +020050void save_boot_params(unsigned long r0, unsigned long r1, unsigned long r2,
51 unsigned long r3)
Simon Glass537e9672015-05-13 07:02:29 -060052{
53 from_spl = r0 != UBOOT_NOT_LOADED_FROM_SPL;
Thierry Redinga0dbc132019-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 Glass537e9672015-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 Warren73c38932015-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 Reding1a869c72019-04-15 11:32:20 +020096#if IS_ENABLED(CONFIG_TEGRA_MC)
Stephen Warrenaeb3fcb2014-07-02 14:12:30 -060097/* Read the RAM size directly from the memory controller */
Stephen Warrena5fc3d02015-08-07 16:12:44 -060098static phys_size_t query_sdram_size(void)
Stephen Warrenaeb3fcb2014-07-02 14:12:30 -060099{
100 struct mc_ctlr *const mc = (struct mc_ctlr *)NV_PA_MC_BASE;
Stephen Warrena5fc3d02015-08-07 16:12:44 -0600101 u32 emem_cfg;
102 phys_size_t size_bytes;
Stephen Warrenaeb3fcb2014-07-02 14:12:30 -0600103
Stephen Warren3a2cab52014-12-23 10:34:50 -0700104 emem_cfg = readl(&mc->mc_emem_cfg);
Marcel Ziswiler8c33ba72014-10-10 23:32:32 +0200105#if defined(CONFIG_TEGRA20)
Stephen Warren3a2cab52014-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 Ziswiler8c33ba72014-10-10 23:32:32 +0200108#else
Stephen Warren3a2cab52014-12-23 10:34:50 -0700109 debug("mc->mc_emem_cfg (MEM_SIZE_MB) = 0x%08x\n", emem_cfg);
Stephen Warrena5fc3d02015-08-07 16:12:44 -0600110#ifndef CONFIG_PHYS_64BIT
Stephen Warren56519c42014-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 Warrena5fc3d02015-08-07 16:12:44 -0600118 } else
119#endif
120 {
Stephen Warren56519c42014-12-23 10:34:51 -0700121 /* RAM size EMC is programmed to. */
Stephen Warrena5fc3d02015-08-07 16:12:44 -0600122 size_bytes = (phys_size_t)emem_cfg * 1024 * 1024;
123#ifndef CONFIG_ARM64
Stephen Warren56519c42014-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 Warrena5fc3d02015-08-07 16:12:44 -0600134#endif
Stephen Warren56519c42014-12-23 10:34:51 -0700135 }
Stephen Warrenaeb3fcb2014-07-02 14:12:30 -0600136#endif
Tom Warren3f82b1d2011-01-27 10:58:05 +0000137
Marcel Ziswiler8c33ba72014-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 Warren3a2cab52014-12-23 10:34:50 -0700140 if (size_bytes == SZ_2G)
141 size_bytes -= SZ_1M;
Marcel Ziswiler8c33ba72014-10-10 23:32:32 +0200142#endif
143
Stephen Warren3a2cab52014-12-23 10:34:50 -0700144 return size_bytes;
Marcel Ziswiler8c33ba72014-10-10 23:32:32 +0200145}
Thierry Reding1a869c72019-04-15 11:32:20 +0200146#endif
Marcel Ziswiler8c33ba72014-10-10 23:32:32 +0200147
Tom Warren3f82b1d2011-01-27 10:58:05 +0000148int dram_init(void)
149{
Thierry Redinga0dbc132019-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 Reding1a869c72019-04-15 11:32:20 +0200157#if IS_ENABLED(CONFIG_TEGRA_MC)
Tom Warren3f82b1d2011-01-27 10:58:05 +0000158 /* We do not initialise DRAM here. We just query the size */
Simon Glass7f8c0702011-11-05 03:56:57 +0000159 gd->ram_size = query_sdram_size();
Thierry Reding1a869c72019-04-15 11:32:20 +0200160#endif
161
Tom Warren3f82b1d2011-01-27 10:58:05 +0000162 return 0;
163}
164
Thierry Reding07ea02b2019-04-15 11:32:21 +0200165#if IS_ENABLED(CONFIG_TEGRA_PINCTRL)
Stephen Warrenb9607e72012-05-14 13:13:45 +0000166static int uart_configs[] = {
Tom Warrenb2871032012-12-11 13:34:15 +0000167#if defined(CONFIG_TEGRA20)
168 #if defined(CONFIG_TEGRA_UARTA_UAA_UAB)
Stephen Warrenb9607e72012-05-14 13:13:45 +0000169 FUNCMUX_UART1_UAA_UAB,
Tom Warrenb2871032012-12-11 13:34:15 +0000170 #elif defined(CONFIG_TEGRA_UARTA_GPU)
Stephen Warrene21649b2012-05-16 05:59:59 +0000171 FUNCMUX_UART1_GPU,
Tom Warrenb2871032012-12-11 13:34:15 +0000172 #elif defined(CONFIG_TEGRA_UARTA_SDIO1)
Lucas Stacha2cfe632012-05-16 08:21:02 +0000173 FUNCMUX_UART1_SDIO1,
Tom Warrenb2871032012-12-11 13:34:15 +0000174 #else
Stephen Warrenb9607e72012-05-14 13:13:45 +0000175 FUNCMUX_UART1_IRRX_IRTX,
Stephen Warren4727a132013-01-22 06:20:08 +0000176#endif
177 FUNCMUX_UART2_UAD,
Stephen Warrenb9607e72012-05-14 13:13:45 +0000178 -1,
179 FUNCMUX_UART4_GMC,
180 -1,
Tom Warrene23bb6a2013-01-28 13:32:10 +0000181#elif defined(CONFIG_TEGRA30)
Tom Warrenb2871032012-12-11 13:34:15 +0000182 FUNCMUX_UART1_ULPI, /* UARTA */
183 -1,
184 -1,
185 -1,
186 -1,
Tom Warren2f5dac92014-01-24 12:46:16 -0700187#elif defined(CONFIG_TEGRA114)
Tom Warrene23bb6a2013-01-28 13:32:10 +0000188 -1,
189 -1,
190 -1,
191 FUNCMUX_UART4_GMI, /* UARTD */
192 -1,
Tom Warren7aaa5a62015-03-04 16:36:00 -0700193#elif defined(CONFIG_TEGRA124)
Tom Warren2f5dac92014-01-24 12:46:16 -0700194 FUNCMUX_UART1_KBC, /* UARTA */
195 -1,
196 -1,
197 FUNCMUX_UART4_GPIO, /* UARTD */
198 -1,
Tom Warren7aaa5a62015-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 Warrenb2871032012-12-11 13:34:15 +0000205#endif
Stephen Warrenb9607e72012-05-14 13:13:45 +0000206};
207
Simon Glassbb6997f2011-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 Warrene23bb6a2013-01-28 13:32:10 +0000220 PERIPH_ID_UART5,
Simon Glassbb6997f2011-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 Warrenb9607e72012-05-14 13:13:45 +0000228 funcmux_select(id, uart_configs[i]);
Simon Glassbb6997f2011-11-28 15:04:39 +0000229 clock_ll_start_uart(id);
230 }
231 }
232}
Thierry Reding07ea02b2019-04-15 11:32:21 +0200233#endif
Simon Glassbb6997f2011-11-28 15:04:39 +0000234
235void board_init_uart_f(void)
236{
Thierry Reding07ea02b2019-04-15 11:32:21 +0200237#if IS_ENABLED(CONFIG_TEGRA_PINCTRL)
Simon Glassbb6997f2011-11-28 15:04:39 +0000238 int uart_ids = 0; /* bit mask of which UART ids to enable */
239
Tom Warren29f3e3f2012-09-04 17:00:24 -0700240#ifdef CONFIG_TEGRA_ENABLE_UARTA
Simon Glassbb6997f2011-11-28 15:04:39 +0000241 uart_ids |= UARTA;
242#endif
Tom Warren29f3e3f2012-09-04 17:00:24 -0700243#ifdef CONFIG_TEGRA_ENABLE_UARTB
Simon Glassbb6997f2011-11-28 15:04:39 +0000244 uart_ids |= UARTB;
245#endif
Tom Warrene23bb6a2013-01-28 13:32:10 +0000246#ifdef CONFIG_TEGRA_ENABLE_UARTC
247 uart_ids |= UARTC;
248#endif
Tom Warren29f3e3f2012-09-04 17:00:24 -0700249#ifdef CONFIG_TEGRA_ENABLE_UARTD
Simon Glassbb6997f2011-11-28 15:04:39 +0000250 uart_ids |= UARTD;
251#endif
Tom Warrene23bb6a2013-01-28 13:32:10 +0000252#ifdef CONFIG_TEGRA_ENABLE_UARTE
253 uart_ids |= UARTE;
254#endif
Simon Glassbb6997f2011-11-28 15:04:39 +0000255 setup_uarts(uart_ids);
Thierry Reding07ea02b2019-04-15 11:32:21 +0200256#endif
Simon Glassbb6997f2011-11-28 15:04:39 +0000257}
Simon Glassbd29cb02012-01-09 13:22:15 +0000258
Simon Glass878a3ed2015-12-04 08:58:39 -0700259#if !CONFIG_IS_ENABLED(OF_CONTROL)
Thomas Chou18746262015-11-19 21:48:11 +0800260static struct ns16550_platdata ns16550_com1_pdata = {
261 .base = CONFIG_SYS_NS16550_COM1,
262 .reg_shift = 2,
263 .clock = CONFIG_SYS_NS16550_CLK,
Heiko Schocher17fa0322017-01-18 08:05:49 +0100264 .fcr = UART_FCR_DEFVAL,
Thomas Chou18746262015-11-19 21:48:11 +0800265};
266
267U_BOOT_DEVICE(ns16550_com1) = {
268 "ns16550_serial", &ns16550_com1_pdata
269};
270#endif
271
Trevor Woerner10015022019-05-03 09:41:00 -0400272#if !CONFIG_IS_ENABLED(SYS_DCACHE_OFF) && !defined(CONFIG_ARM64)
Simon Glassbd29cb02012-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