blob: 4835824f7249d62a1c381d3199ede921def5b5d5 [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
Tom Rinidec7ea02024-05-20 13:35:03 -06007#include <config.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>
Simon Glass3ba929a2020-10-30 21:38:53 -060015#include <asm/global_data.h>
Tom Warren41b68382011-01-27 10:58:05 +000016#include <asm/io.h>
Thierry Reding45ad0b02019-04-15 11:32:18 +020017#if IS_ENABLED(CONFIG_TEGRA_CLKRST)
Simon Glass96b7c432011-11-28 15:04:39 +000018#include <asm/arch/clock.h>
Thierry Reding45ad0b02019-04-15 11:32:18 +020019#endif
Svyatoslav Ryhel13961102023-11-27 11:54:21 +020020#if CONFIG_IS_ENABLED(PINCTRL_TEGRA)
Simon Glass96b7c432011-11-28 15:04:39 +000021#include <asm/arch/funcmux.h>
Thierry Reding7c0b1502019-04-15 11:32:21 +020022#endif
Thierry Reding17987bb2019-04-15 11:32:20 +020023#if IS_ENABLED(CONFIG_TEGRA_MC)
Marcel Ziswilerc5ecf272014-10-10 23:32:32 +020024#include <asm/arch/mc.h>
Thierry Reding17987bb2019-04-15 11:32:20 +020025#endif
Tom Warrenab371962012-09-19 15:50:56 -070026#include <asm/arch/tegra.h>
Stephen Warren8d1fb312015-01-19 16:25:52 -070027#include <asm/arch-tegra/ap.h>
Lucas Stache80f7ca2012-09-29 10:02:08 +000028#include <asm/arch-tegra/board.h>
Thierry Reding7cef2b22019-04-15 11:32:28 +020029#include <asm/arch-tegra/cboot.h>
Tom Warrenab371962012-09-19 15:50:56 -070030#include <asm/arch-tegra/pmc.h>
31#include <asm/arch-tegra/sys_proto.h>
32#include <asm/arch-tegra/warmboot.h>
Tom Warren41b68382011-01-27 10:58:05 +000033
Tom Warren021a8bb2015-07-08 08:05:35 -070034void save_boot_params_ret(void);
35
Tom Warren41b68382011-01-27 10:58:05 +000036DECLARE_GLOBAL_DATA_PTR;
37
Simon Glass96b7c432011-11-28 15:04:39 +000038enum {
39 /* UARTs which we can enable */
40 UARTA = 1 << 0,
41 UARTB = 1 << 1,
Tom Warrene3d95bc2013-01-28 13:32:10 +000042 UARTC = 1 << 2,
Simon Glass96b7c432011-11-28 15:04:39 +000043 UARTD = 1 << 3,
Tom Warrene3d95bc2013-01-28 13:32:10 +000044 UARTE = 1 << 4,
45 UART_COUNT = 5,
Simon Glass96b7c432011-11-28 15:04:39 +000046};
47
Marek Behún4bebdd32021-05-20 13:23:52 +020048static bool from_spl __section(".data");
Simon Glasseec13c42015-05-13 07:02:29 -060049
Simon Glass85ed77d2024-09-29 19:49:46 -060050#ifndef CONFIG_XPL_BUILD
Thierry Redingf6270a62019-04-15 11:32:23 +020051void save_boot_params(unsigned long r0, unsigned long r1, unsigned long r2,
52 unsigned long r3)
Simon Glasseec13c42015-05-13 07:02:29 -060053{
54 from_spl = r0 != UBOOT_NOT_LOADED_FROM_SPL;
Thierry Reding7cef2b22019-04-15 11:32:28 +020055
56 /*
57 * The logic for this is somewhat indirect. The purpose of the marker
58 * (UBOOT_NOT_LOADED_FROM_SPL) is in fact used to determine if U-Boot
59 * was loaded from a read-only instance of itself, which is something
60 * that can happen in secure boot setups. So basically the presence
61 * of the marker is an indication that U-Boot was loaded by one such
62 * special variant of U-Boot. Conversely, the absence of the marker
63 * indicates that this instance of U-Boot was loaded by something
64 * other than a special U-Boot. This could be SPL, but it could just
65 * as well be one of any number of other first stage bootloaders.
66 */
67 if (from_spl)
68 cboot_save_boot_params(r0, r1, r2, r3);
69
Simon Glasseec13c42015-05-13 07:02:29 -060070 save_boot_params_ret();
71}
72#endif
73
74bool spl_was_boot_source(void)
75{
76 return from_spl;
77}
78
Stephen Warren8d1fb312015-01-19 16:25:52 -070079#if defined(CONFIG_TEGRA_SUPPORT_NON_SECURE)
Stephen Warren8d1fb312015-01-19 16:25:52 -070080bool 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 Reding17987bb2019-04-15 11:32:20 +020094#if IS_ENABLED(CONFIG_TEGRA_MC)
Stephen Warren1b4af6b2014-07-02 14:12:30 -060095/* Read the RAM size directly from the memory controller */
Stephen Warren6718af02015-08-07 16:12:44 -060096static phys_size_t query_sdram_size(void)
Stephen Warren1b4af6b2014-07-02 14:12:30 -060097{
98 struct mc_ctlr *const mc = (struct mc_ctlr *)NV_PA_MC_BASE;
Stephen Warren6718af02015-08-07 16:12:44 -060099 u32 emem_cfg;
100 phys_size_t size_bytes;
Stephen Warren1b4af6b2014-07-02 14:12:30 -0600101
Stephen Warren210bdb22014-12-23 10:34:50 -0700102 emem_cfg = readl(&mc->mc_emem_cfg);
Marcel Ziswilerc5ecf272014-10-10 23:32:32 +0200103#if defined(CONFIG_TEGRA20)
Stephen Warren210bdb22014-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 Ziswilerc5ecf272014-10-10 23:32:32 +0200106#else
Stephen Warren210bdb22014-12-23 10:34:50 -0700107 debug("mc->mc_emem_cfg (MEM_SIZE_MB) = 0x%08x\n", emem_cfg);
Stephen Warren6718af02015-08-07 16:12:44 -0600108#ifndef CONFIG_PHYS_64BIT
Stephen Warrenc8018052014-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 Warren6718af02015-08-07 16:12:44 -0600116 } else
117#endif
118 {
Stephen Warrenc8018052014-12-23 10:34:51 -0700119 /* RAM size EMC is programmed to. */
Stephen Warren6718af02015-08-07 16:12:44 -0600120 size_bytes = (phys_size_t)emem_cfg * 1024 * 1024;
121#ifndef CONFIG_ARM64
Stephen Warrenc8018052014-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 Warren6718af02015-08-07 16:12:44 -0600132#endif
Stephen Warrenc8018052014-12-23 10:34:51 -0700133 }
Marcel Ziswilerc5ecf272014-10-10 23:32:32 +0200134#endif
Stephen Warren1b4af6b2014-07-02 14:12:30 -0600135
Marcel Ziswilerc5ecf272014-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 Warren210bdb22014-12-23 10:34:50 -0700138 if (size_bytes == SZ_2G)
139 size_bytes -= SZ_1M;
Stephen Warren1b4af6b2014-07-02 14:12:30 -0600140#endif
Tom Warren41b68382011-01-27 10:58:05 +0000141
Stephen Warren210bdb22014-12-23 10:34:50 -0700142 return size_bytes;
Marcel Ziswilerc5ecf272014-10-10 23:32:32 +0200143}
Thierry Reding17987bb2019-04-15 11:32:20 +0200144#endif
Marcel Ziswilerc5ecf272014-10-10 23:32:32 +0200145
Tom Warren41b68382011-01-27 10:58:05 +0000146int dram_init(void)
147{
Thierry Reding7cef2b22019-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 Reding17987bb2019-04-15 11:32:20 +0200155#if IS_ENABLED(CONFIG_TEGRA_MC)
Tom Warren41b68382011-01-27 10:58:05 +0000156 /* We do not initialise DRAM here. We just query the size */
Simon Glassf6fcbbd2011-11-05 03:56:57 +0000157 gd->ram_size = query_sdram_size();
Thierry Reding17987bb2019-04-15 11:32:20 +0200158#endif
159
Tom Warren41b68382011-01-27 10:58:05 +0000160 return 0;
161}
162
Svyatoslav Ryhel13961102023-11-27 11:54:21 +0200163#if CONFIG_IS_ENABLED(PINCTRL_TEGRA)
Stephen Warren59f90102012-05-14 13:13:45 +0000164static int uart_configs[] = {
Tom Warren61c6d0e2012-12-11 13:34:15 +0000165#if defined(CONFIG_TEGRA20)
166 #if defined(CONFIG_TEGRA_UARTA_UAA_UAB)
Stephen Warren59f90102012-05-14 13:13:45 +0000167 FUNCMUX_UART1_UAA_UAB,
Tom Warren61c6d0e2012-12-11 13:34:15 +0000168 #elif defined(CONFIG_TEGRA_UARTA_GPU)
Stephen Warrene4c01a82012-05-16 05:59:59 +0000169 FUNCMUX_UART1_GPU,
Tom Warren61c6d0e2012-12-11 13:34:15 +0000170 #elif defined(CONFIG_TEGRA_UARTA_SDIO1)
Lucas Stach4de6eec2012-05-16 08:21:02 +0000171 FUNCMUX_UART1_SDIO1,
Artur Kowalski420e9d82025-03-30 21:26:39 +0200172 #elif defined(CONFIG_TEGRA_UARTA_SDB_SDD)
173 FUNCMUX_UART1_SDB_SDD,
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,
Jonas Schwöbela2c9b972024-01-22 14:40:27 +0200186 FUNCMUX_UART5_SDMMC1, /* UARTE */
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{
Svyatoslav Ryhel13961102023-11-27 11:54:21 +0200237#if CONFIG_IS_ENABLED(PINCTRL_TEGRA)
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
Svyatoslav Ryhel1adfa542025-03-31 09:33:17 +0300241#define CFG_SYS_NS16550_COM1 NV_PA_APB_UARTA_BASE
Simon Glass96b7c432011-11-28 15:04:39 +0000242 uart_ids |= UARTA;
243#endif
Tom Warren22562a42012-09-04 17:00:24 -0700244#ifdef CONFIG_TEGRA_ENABLE_UARTB
Svyatoslav Ryhel1adfa542025-03-31 09:33:17 +0300245#define CFG_SYS_NS16550_COM1 NV_PA_APB_UARTB_BASE
Simon Glass96b7c432011-11-28 15:04:39 +0000246 uart_ids |= UARTB;
247#endif
Tom Warrene3d95bc2013-01-28 13:32:10 +0000248#ifdef CONFIG_TEGRA_ENABLE_UARTC
Svyatoslav Ryhel1adfa542025-03-31 09:33:17 +0300249#define CFG_SYS_NS16550_COM1 NV_PA_APB_UARTC_BASE
Tom Warrene3d95bc2013-01-28 13:32:10 +0000250 uart_ids |= UARTC;
251#endif
Tom Warren22562a42012-09-04 17:00:24 -0700252#ifdef CONFIG_TEGRA_ENABLE_UARTD
Svyatoslav Ryhel1adfa542025-03-31 09:33:17 +0300253#define CFG_SYS_NS16550_COM1 NV_PA_APB_UARTD_BASE
Simon Glass96b7c432011-11-28 15:04:39 +0000254 uart_ids |= UARTD;
255#endif
Tom Warrene3d95bc2013-01-28 13:32:10 +0000256#ifdef CONFIG_TEGRA_ENABLE_UARTE
Svyatoslav Ryhel1adfa542025-03-31 09:33:17 +0300257#define CFG_SYS_NS16550_COM1 NV_PA_APB_UARTE_BASE
Tom Warrene3d95bc2013-01-28 13:32:10 +0000258 uart_ids |= UARTE;
259#endif
Simon Glass96b7c432011-11-28 15:04:39 +0000260 setup_uarts(uart_ids);
Thierry Reding7c0b1502019-04-15 11:32:21 +0200261#endif
Simon Glass96b7c432011-11-28 15:04:39 +0000262}
Simon Glass410012f2012-01-09 13:22:15 +0000263
Simon Glassf4402d02015-12-04 08:58:39 -0700264#if !CONFIG_IS_ENABLED(OF_CONTROL)
Simon Glassb75b15b2020-12-03 16:55:23 -0700265static struct ns16550_plat ns16550_com1_pdata = {
Tom Rinidf6a2152022-11-16 13:10:28 -0500266 .base = CFG_SYS_NS16550_COM1,
Thomas Choue3b90262015-11-19 21:48:11 +0800267 .reg_shift = 2,
Tom Rinidf6a2152022-11-16 13:10:28 -0500268 .clock = CFG_SYS_NS16550_CLK,
Heiko Schocher06f108e2017-01-18 08:05:49 +0100269 .fcr = UART_FCR_DEFVAL,
Thomas Choue3b90262015-11-19 21:48:11 +0800270};
271
Simon Glass1d8364a2020-12-28 20:34:54 -0700272U_BOOT_DRVINFO(ns16550_com1) = {
Thomas Choue3b90262015-11-19 21:48:11 +0800273 "ns16550_serial", &ns16550_com1_pdata
274};
275#endif
276
Trevor Woerner43ec7e02019-05-03 09:41:00 -0400277#if !CONFIG_IS_ENABLED(SYS_DCACHE_OFF) && !defined(CONFIG_ARM64)
Simon Glass410012f2012-01-09 13:22:15 +0000278void enable_caches(void)
279{
280 /* Enable D-cache. I-cache is already enabled in start.S */
281 dcache_enable();
282}
283#endif