18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * arch/arm/mach-tegra/reset.c
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2011,2012 NVIDIA Corporation.
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include <linux/bitops.h>
98c2ecf20Sopenharmony_ci#include <linux/cpumask.h>
108c2ecf20Sopenharmony_ci#include <linux/init.h>
118c2ecf20Sopenharmony_ci#include <linux/io.h>
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#include <linux/firmware/trusted_foundations.h>
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci#include <soc/tegra/fuse.h>
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci#include <asm/cacheflush.h>
188c2ecf20Sopenharmony_ci#include <asm/firmware.h>
198c2ecf20Sopenharmony_ci#include <asm/hardware/cache-l2x0.h>
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci#include "iomap.h"
228c2ecf20Sopenharmony_ci#include "irammap.h"
238c2ecf20Sopenharmony_ci#include "reset.h"
248c2ecf20Sopenharmony_ci#include "sleep.h"
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci#define TEGRA_IRAM_RESET_BASE (TEGRA_IRAM_BASE + \
278c2ecf20Sopenharmony_ci				TEGRA_IRAM_RESET_HANDLER_OFFSET)
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_cistatic bool is_enabled;
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_cistatic void __init tegra_cpu_reset_handler_set(const u32 reset_address)
328c2ecf20Sopenharmony_ci{
338c2ecf20Sopenharmony_ci	void __iomem *evp_cpu_reset =
348c2ecf20Sopenharmony_ci		IO_ADDRESS(TEGRA_EXCEPTION_VECTORS_BASE + 0x100);
358c2ecf20Sopenharmony_ci	void __iomem *sb_ctrl = IO_ADDRESS(TEGRA_SB_BASE);
368c2ecf20Sopenharmony_ci	u32 reg;
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci	/*
398c2ecf20Sopenharmony_ci	 * NOTE: This must be the one and only write to the EVP CPU reset
408c2ecf20Sopenharmony_ci	 *       vector in the entire system.
418c2ecf20Sopenharmony_ci	 */
428c2ecf20Sopenharmony_ci	writel(reset_address, evp_cpu_reset);
438c2ecf20Sopenharmony_ci	wmb();
448c2ecf20Sopenharmony_ci	reg = readl(evp_cpu_reset);
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci	/*
478c2ecf20Sopenharmony_ci	 * Prevent further modifications to the physical reset vector.
488c2ecf20Sopenharmony_ci	 *  NOTE: Has no effect on chips prior to Tegra30.
498c2ecf20Sopenharmony_ci	 */
508c2ecf20Sopenharmony_ci	reg = readl(sb_ctrl);
518c2ecf20Sopenharmony_ci	reg |= 2;
528c2ecf20Sopenharmony_ci	writel(reg, sb_ctrl);
538c2ecf20Sopenharmony_ci	wmb();
548c2ecf20Sopenharmony_ci}
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_cistatic void __init tegra_cpu_reset_handler_enable(void)
578c2ecf20Sopenharmony_ci{
588c2ecf20Sopenharmony_ci	void __iomem *iram_base = IO_ADDRESS(TEGRA_IRAM_RESET_BASE);
598c2ecf20Sopenharmony_ci	const u32 reset_address = TEGRA_IRAM_RESET_BASE +
608c2ecf20Sopenharmony_ci						tegra_cpu_reset_handler_offset;
618c2ecf20Sopenharmony_ci	int err;
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci	BUG_ON(is_enabled);
648c2ecf20Sopenharmony_ci	BUG_ON(tegra_cpu_reset_handler_size > TEGRA_IRAM_RESET_HANDLER_SIZE);
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci	memcpy(iram_base, (void *)__tegra_cpu_reset_handler_start,
678c2ecf20Sopenharmony_ci			tegra_cpu_reset_handler_size);
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci	err = call_firmware_op(set_cpu_boot_addr, 0, reset_address);
708c2ecf20Sopenharmony_ci	switch (err) {
718c2ecf20Sopenharmony_ci	case -ENOSYS:
728c2ecf20Sopenharmony_ci		tegra_cpu_reset_handler_set(reset_address);
738c2ecf20Sopenharmony_ci		fallthrough;
748c2ecf20Sopenharmony_ci	case 0:
758c2ecf20Sopenharmony_ci		is_enabled = true;
768c2ecf20Sopenharmony_ci		break;
778c2ecf20Sopenharmony_ci	default:
788c2ecf20Sopenharmony_ci		pr_crit("Cannot set CPU reset handler: %d\n", err);
798c2ecf20Sopenharmony_ci		BUG();
808c2ecf20Sopenharmony_ci	}
818c2ecf20Sopenharmony_ci}
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_civoid __init tegra_cpu_reset_handler_init(void)
848c2ecf20Sopenharmony_ci{
858c2ecf20Sopenharmony_ci	__tegra_cpu_reset_handler_data[TEGRA_RESET_TF_PRESENT] =
868c2ecf20Sopenharmony_ci		trusted_foundations_registered();
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci#ifdef CONFIG_SMP
898c2ecf20Sopenharmony_ci	__tegra_cpu_reset_handler_data[TEGRA_RESET_MASK_PRESENT] =
908c2ecf20Sopenharmony_ci		*((u32 *)cpu_possible_mask);
918c2ecf20Sopenharmony_ci	__tegra_cpu_reset_handler_data[TEGRA_RESET_STARTUP_SECONDARY] =
928c2ecf20Sopenharmony_ci		__pa_symbol((void *)secondary_startup);
938c2ecf20Sopenharmony_ci#endif
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci#ifdef CONFIG_PM_SLEEP
968c2ecf20Sopenharmony_ci	__tegra_cpu_reset_handler_data[TEGRA_RESET_STARTUP_LP1] =
978c2ecf20Sopenharmony_ci		TEGRA_IRAM_LPx_RESUME_AREA;
988c2ecf20Sopenharmony_ci	__tegra_cpu_reset_handler_data[TEGRA_RESET_STARTUP_LP2] =
998c2ecf20Sopenharmony_ci		__pa_symbol((void *)tegra_resume);
1008c2ecf20Sopenharmony_ci#endif
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci	tegra_cpu_reset_handler_enable();
1038c2ecf20Sopenharmony_ci}
104