2020
2121#define pr_fmt (fmt ) "tegra-pmc: " fmt
2222
23+ #include <linux/arm-smccc.h>
2324#include <linux/clk.h>
2425#include <linux/clk/tegra.h>
2526#include <linux/debugfs.h>
145146#define WAKE_AOWAKE_CTRL 0x4f4
146147#define WAKE_AOWAKE_CTRL_INTR_POLARITY BIT(0)
147148
149+ /* for secure PMC */
150+ #define TEGRA_SMC_PMC 0xc2fffe00
151+ #define TEGRA_SMC_PMC_READ 0xaa
152+ #define TEGRA_SMC_PMC_WRITE 0xbb
153+
148154struct tegra_powergate {
149155 struct generic_pm_domain genpd ;
150156 struct tegra_pmc * pmc ;
@@ -216,6 +222,7 @@ struct tegra_pmc_soc {
216222 bool has_gpu_clamps ;
217223 bool needs_mbist_war ;
218224 bool has_impl_33v_pwr ;
225+ bool maybe_tz_only ;
219226
220227 const struct tegra_io_pad_soc * io_pads ;
221228 unsigned int num_io_pads ;
@@ -278,6 +285,7 @@ static const char * const tegra30_reset_sources[] = {
278285 * @scratch: pointer to I/O remapped region for scratch registers
279286 * @clk: pointer to pclk clock
280287 * @soc: pointer to SoC data structure
288+ * @tz_only: flag specifying if the PMC can only be accessed via TrustZone
281289 * @debugfs: pointer to debugfs entry
282290 * @rate: currently configured rate of pclk
283291 * @suspend_mode: lowest suspend mode available
@@ -308,6 +316,7 @@ struct tegra_pmc {
308316 struct dentry * debugfs ;
309317
310318 const struct tegra_pmc_soc * soc ;
319+ bool tz_only ;
311320
312321 unsigned long rate ;
313322
@@ -346,13 +355,62 @@ to_powergate(struct generic_pm_domain *domain)
346355
347356static u32 tegra_pmc_readl (struct tegra_pmc * pmc , unsigned long offset )
348357{
358+ struct arm_smccc_res res ;
359+
360+ if (pmc -> tz_only ) {
361+ arm_smccc_smc (TEGRA_SMC_PMC , TEGRA_SMC_PMC_READ , offset , 0 , 0 ,
362+ 0 , 0 , 0 , & res );
363+ if (res .a0 ) {
364+ if (pmc -> dev )
365+ dev_warn (pmc -> dev , "%s(): SMC failed: %lu\n" ,
366+ __func__ , res .a0 );
367+ else
368+ pr_warn ("%s(): SMC failed: %lu\n" , __func__ ,
369+ res .a0 );
370+ }
371+
372+ return res .a1 ;
373+ }
374+
349375 return readl (pmc -> base + offset );
350376}
351377
352378static void tegra_pmc_writel (struct tegra_pmc * pmc , u32 value ,
353379 unsigned long offset )
354380{
355- writel (value , pmc -> base + offset );
381+ struct arm_smccc_res res ;
382+
383+ if (pmc -> tz_only ) {
384+ arm_smccc_smc (TEGRA_SMC_PMC , TEGRA_SMC_PMC_WRITE , offset ,
385+ value , 0 , 0 , 0 , 0 , & res );
386+ if (res .a0 ) {
387+ if (pmc -> dev )
388+ dev_warn (pmc -> dev , "%s(): SMC failed: %lu\n" ,
389+ __func__ , res .a0 );
390+ else
391+ pr_warn ("%s(): SMC failed: %lu\n" , __func__ ,
392+ res .a0 );
393+ }
394+ } else {
395+ writel (value , pmc -> base + offset );
396+ }
397+ }
398+
399+ static u32 tegra_pmc_scratch_readl (struct tegra_pmc * pmc , unsigned long offset )
400+ {
401+ if (pmc -> tz_only )
402+ return tegra_pmc_readl (pmc , offset );
403+
404+ return readl (pmc -> scratch + offset );
405+ }
406+
407+ static void tegra_pmc_scratch_writel (struct tegra_pmc * pmc , u32 value ,
408+ unsigned long offset )
409+ {
410+ if (pmc -> tz_only )
411+ tegra_pmc_writel (pmc , value , offset );
412+ else
413+ writel (value , pmc -> scratch + offset );
356414}
357415
358416/*
@@ -776,7 +834,7 @@ static int tegra_pmc_restart_notify(struct notifier_block *this,
776834 const char * cmd = data ;
777835 u32 value ;
778836
779- value = readl (pmc -> scratch + pmc -> soc -> regs -> scratch0 );
837+ value = tegra_pmc_scratch_readl (pmc , pmc -> soc -> regs -> scratch0 );
780838 value &= ~PMC_SCRATCH0_MODE_MASK ;
781839
782840 if (cmd ) {
@@ -790,7 +848,7 @@ static int tegra_pmc_restart_notify(struct notifier_block *this,
790848 value |= PMC_SCRATCH0_MODE_RCM ;
791849 }
792850
793- writel ( value , pmc -> scratch + pmc -> soc -> regs -> scratch0 );
851+ tegra_pmc_scratch_writel ( pmc , value , pmc -> soc -> regs -> scratch0 );
794852
795853 /* reset everything but PMC_SCRATCH0 and PMC_RST_STATUS */
796854 value = tegra_pmc_readl (pmc , PMC_CNTRL );
@@ -2071,6 +2129,7 @@ static const struct tegra_pmc_soc tegra20_pmc_soc = {
20712129 .has_gpu_clamps = false,
20722130 .needs_mbist_war = false,
20732131 .has_impl_33v_pwr = false,
2132+ .maybe_tz_only = false,
20742133 .num_io_pads = 0 ,
20752134 .io_pads = NULL ,
20762135 .num_pin_descs = 0 ,
@@ -2117,6 +2176,7 @@ static const struct tegra_pmc_soc tegra30_pmc_soc = {
21172176 .has_gpu_clamps = false,
21182177 .needs_mbist_war = false,
21192178 .has_impl_33v_pwr = false,
2179+ .maybe_tz_only = false,
21202180 .num_io_pads = 0 ,
21212181 .io_pads = NULL ,
21222182 .num_pin_descs = 0 ,
@@ -2167,6 +2227,7 @@ static const struct tegra_pmc_soc tegra114_pmc_soc = {
21672227 .has_gpu_clamps = false,
21682228 .needs_mbist_war = false,
21692229 .has_impl_33v_pwr = false,
2230+ .maybe_tz_only = false,
21702231 .num_io_pads = 0 ,
21712232 .io_pads = NULL ,
21722233 .num_pin_descs = 0 ,
@@ -2277,6 +2338,7 @@ static const struct tegra_pmc_soc tegra124_pmc_soc = {
22772338 .has_gpu_clamps = true,
22782339 .needs_mbist_war = false,
22792340 .has_impl_33v_pwr = false,
2341+ .maybe_tz_only = false,
22802342 .num_io_pads = ARRAY_SIZE (tegra124_io_pads ),
22812343 .io_pads = tegra124_io_pads ,
22822344 .num_pin_descs = ARRAY_SIZE (tegra124_pin_descs ),
@@ -2382,6 +2444,7 @@ static const struct tegra_pmc_soc tegra210_pmc_soc = {
23822444 .has_gpu_clamps = true,
23832445 .needs_mbist_war = true,
23842446 .has_impl_33v_pwr = false,
2447+ .maybe_tz_only = true,
23852448 .num_io_pads = ARRAY_SIZE (tegra210_io_pads ),
23862449 .io_pads = tegra210_io_pads ,
23872450 .num_pin_descs = ARRAY_SIZE (tegra210_pin_descs ),
@@ -2506,6 +2569,7 @@ static const struct tegra_pmc_soc tegra186_pmc_soc = {
25062569 .has_gpu_clamps = false,
25072570 .needs_mbist_war = false,
25082571 .has_impl_33v_pwr = true,
2572+ .maybe_tz_only = false,
25092573 .num_io_pads = ARRAY_SIZE (tegra186_io_pads ),
25102574 .io_pads = tegra186_io_pads ,
25112575 .num_pin_descs = ARRAY_SIZE (tegra186_pin_descs ),
@@ -2585,6 +2649,7 @@ static const struct tegra_pmc_soc tegra194_pmc_soc = {
25852649 .has_gpu_clamps = false,
25862650 .needs_mbist_war = false,
25872651 .has_impl_33v_pwr = false,
2652+ .maybe_tz_only = false,
25882653 .num_io_pads = ARRAY_SIZE (tegra194_io_pads ),
25892654 .io_pads = tegra194_io_pads ,
25902655 .regs = & tegra186_pmc_regs ,
@@ -2619,6 +2684,32 @@ static struct platform_driver tegra_pmc_driver = {
26192684};
26202685builtin_platform_driver (tegra_pmc_driver );
26212686
2687+ static bool __init tegra_pmc_detect_tz_only (struct tegra_pmc * pmc )
2688+ {
2689+ u32 value , saved ;
2690+
2691+ saved = readl (pmc -> base + pmc -> soc -> regs -> scratch0 );
2692+ value = saved ^ 0xffffffff ;
2693+
2694+ if (value == 0xffffffff )
2695+ value = 0xdeadbeef ;
2696+
2697+ /* write pattern and read it back */
2698+ writel (value , pmc -> base + pmc -> soc -> regs -> scratch0 );
2699+ value = readl (pmc -> base + pmc -> soc -> regs -> scratch0 );
2700+
2701+ /* if we read all-zeroes, access is restricted to TZ only */
2702+ if (value == 0 ) {
2703+ pr_info ("access to PMC is restricted to TZ\n" );
2704+ return true;
2705+ }
2706+
2707+ /* restore original value */
2708+ writel (saved , pmc -> base + pmc -> soc -> regs -> scratch0 );
2709+
2710+ return false;
2711+ }
2712+
26222713/*
26232714 * Early initialization to allow access to registers in the very early boot
26242715 * process.
@@ -2681,6 +2772,9 @@ static int __init tegra_pmc_early_init(void)
26812772 if (np ) {
26822773 pmc -> soc = match -> data ;
26832774
2775+ if (pmc -> soc -> maybe_tz_only )
2776+ pmc -> tz_only = tegra_pmc_detect_tz_only (pmc );
2777+
26842778 tegra_powergate_init (pmc , np );
26852779
26862780 /*
0 commit comments