1 // SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
3 * Copyright (C) 2018, STMicroelectronics - All Rights Reserved
12 #include <asm/secure.h>
14 #include <linux/bitops.h>
15 #include <linux/errno.h>
19 #define PWR_MPUCR 0x10
21 #define PWR_CR3_DDRSREN BIT(10)
22 #define PWR_CR3_DDRRETEN BIT(12)
24 #define PWR_MPUCR_PDDS BIT(0)
25 #define PWR_MPUCR_CSTDBYDIS BIT(3)
26 #define PWR_MPUCR_CSSF BIT(9)
29 #define RCC_MSSCKSELR 0x48
30 #define RCC_DDRITFCR 0xd8
32 #define RCC_DDRITFCR_DDRC1EN BIT(0)
33 #define RCC_DDRITFCR_DDRC1LPEN BIT(1)
34 #define RCC_DDRITFCR_DDRC2EN BIT(2)
35 #define RCC_DDRITFCR_DDRC2LPEN BIT(3)
36 #define RCC_DDRITFCR_DDRPHYCEN BIT(4)
37 #define RCC_DDRITFCR_DDRPHYCLPEN BIT(5)
38 #define RCC_DDRITFCR_DDRCAPBEN BIT(6)
39 #define RCC_DDRITFCR_DDRCAPBLPEN BIT(7)
40 #define RCC_DDRITFCR_AXIDCGEN BIT(8)
41 #define RCC_DDRITFCR_DDRPHYCAPBEN BIT(9)
42 #define RCC_DDRITFCR_DDRPHYCAPBLPEN BIT(10)
43 #define RCC_DDRITFCR_DDRCKMOD_MASK GENMASK(22, 20)
44 #define RCC_DDRITFCR_GSKPCTRL BIT(24)
46 #define RCC_MP_SREQSETR 0x104
47 #define RCC_MP_SREQCLRR 0x108
49 #define RCC_MP_CIER 0x414
50 #define RCC_MP_CIFR 0x418
51 #define RCC_MP_CIFR_WKUPF BIT(20)
53 #define RCC_MCUDIVR 0x830
54 #define RCC_PLL3CR 0x880
55 #define RCC_PLL4CR 0x894
58 #define SYSCFG_CMPCR 0x20
59 #define SYSCFG_CMPCR_SW_CTRL BIT(2)
60 #define SYSCFG_CMPENSETR 0x24
61 #define SYSCFG_CMPENCLRR 0x28
62 #define SYSCFG_CMPENR_MPUEN BIT(0)
64 /* DDR Controller registers offsets */
65 #define DDRCTRL_STAT 0x004
66 #define DDRCTRL_PWRCTL 0x030
67 #define DDRCTRL_PWRTMG 0x034
68 #define DDRCTRL_HWLPCTL 0x038
69 #define DDRCTRL_DFIMISC 0x1b0
70 #define DDRCTRL_SWCTL 0x320
71 #define DDRCTRL_SWSTAT 0x324
72 #define DDRCTRL_PSTAT 0x3fc
73 #define DDRCTRL_PCTRL_0 0x490
74 #define DDRCTRL_PCTRL_1 0x540
76 /* DDR Controller Register fields */
77 #define DDRCTRL_STAT_OPERATING_MODE_MASK GENMASK(2, 0)
78 #define DDRCTRL_STAT_OPERATING_MODE_NORMAL 0x1
79 #define DDRCTRL_STAT_OPERATING_MODE_SR 0x3
80 #define DDRCTRL_STAT_SELFREF_TYPE_MASK GENMASK(5, 4)
81 #define DDRCTRL_STAT_SELFREF_TYPE_ASR (0x3 << 4)
82 #define DDRCTRL_STAT_SELFREF_TYPE_SR (0x2 << 4)
84 #define DDRCTRL_PWRCTL_SELFREF_EN BIT(0)
85 #define DDRCTRL_PWRCTL_EN_DFI_DRAM_CLK_DISABLE BIT(3)
86 #define DDRCTRL_PWRCTL_SELFREF_SW BIT(5)
88 #define DDRCTRL_PWRTMG_SELFREF_TO_X32_MASK GENMASK(23, 16)
89 #define DDRCTRL_PWRTMG_SELFREF_TO_X32_0 BIT(16)
91 #define DDRCTRL_HWLPCTL_HW_LP_EN BIT(0)
93 #define DDRCTRL_DFIMISC_DFI_INIT_COMPLETE_EN BIT(0)
95 #define DDRCTRL_SWCTL_SW_DONE BIT(0)
97 #define DDRCTRL_SWSTAT_SW_DONE_ACK BIT(0)
99 #define DDRCTRL_PSTAT_RD_PORT_BUSY_0 BIT(0)
100 #define DDRCTRL_PSTAT_RD_PORT_BUSY_1 BIT(1)
101 #define DDRCTRL_PSTAT_WR_PORT_BUSY_0 BIT(16)
102 #define DDRCTRL_PSTAT_WR_PORT_BUSY_1 BIT(17)
104 #define DDRCTRL_PCTRL_N_PORT_EN BIT(0)
106 /* DDR PHY registers offsets */
107 #define DDRPHYC_PIR 0x004
108 #define DDRPHYC_PGSR 0x00c
109 #define DDRPHYC_ACDLLCR 0x014
110 #define DDRPHYC_ACIOCR 0x024
111 #define DDRPHYC_DXCCR 0x028
112 #define DDRPHYC_DSGCR 0x02c
113 #define DDRPHYC_ZQ0CR0 0x180
114 #define DDRPHYC_DX0DLLCR 0x1cc
115 #define DDRPHYC_DX1DLLCR 0x20c
116 #define DDRPHYC_DX2DLLCR 0x24c
117 #define DDRPHYC_DX3DLLCR 0x28c
119 /* DDR PHY Register fields */
120 #define DDRPHYC_PIR_INIT BIT(0)
121 #define DDRPHYC_PIR_DLLSRST BIT(1)
122 #define DDRPHYC_PIR_DLLLOCK BIT(2)
123 #define DDRPHYC_PIR_ITMSRST BIT(4)
125 #define DDRPHYC_PGSR_IDONE BIT(0)
127 #define DDRPHYC_ACDLLCR_DLLSRST BIT(30)
128 #define DDRPHYC_ACDLLCR_DLLDIS BIT(31)
130 #define DDRPHYC_ACIOCR_ACOE BIT(1)
131 #define DDRPHYC_ACIOCR_ACPDD BIT(3)
132 #define DDRPHYC_ACIOCR_ACPDR BIT(4)
133 #define DDRPHYC_ACIOCR_CKPDD_MASK GENMASK(10, 8)
134 #define DDRPHYC_ACIOCR_CKPDD_0 BIT(8)
135 #define DDRPHYC_ACIOCR_CKPDR_MASK GENMASK(13, 11)
136 #define DDRPHYC_ACIOCR_CKPDR_0 BIT(11)
137 #define DDRPHYC_ACIOCR_CSPDD_MASK GENMASK(20, 18)
138 #define DDRPHYC_ACIOCR_CSPDD_0 BIT(18)
140 #define DDRPHYC_DXCCR_DXPDD BIT(2)
141 #define DDRPHYC_DXCCR_DXPDR BIT(3)
143 #define DDRPHYC_DSGCR_CKEPDD_MASK GENMASK(19, 16)
144 #define DDRPHYC_DSGCR_CKEPDD_0 BIT(16)
145 #define DDRPHYC_DSGCR_ODTPDD_MASK GENMASK(23, 20)
146 #define DDRPHYC_DSGCR_ODTPDD_0 BIT(20)
147 #define DDRPHYC_DSGCR_NL2PD BIT(24)
148 #define DDRPHYC_DSGCR_CKOE BIT(28)
150 #define DDRPHYC_ZQ0CRN_ZQPD BIT(31)
152 #define DDRPHYC_DXNDLLCR_DLLDIS BIT(31)
154 #define BOOT_API_A7_CORE0_MAGIC_NUMBER 0xca7face0
155 #define BOOT_API_A7_CORE1_MAGIC_NUMBER 0xca7face1
157 #define MPIDR_AFF0 GENMASK(7, 0)
159 #define RCC_MP_GRSTCSETR (STM32_RCC_BASE + 0x0404)
160 #define RCC_MP_GRSTCSETR_MPSYSRST BIT(0)
161 #define RCC_MP_GRSTCSETR_MPUP0RST BIT(4)
162 #define RCC_MP_GRSTCSETR_MPUP1RST BIT(5)
166 #define IWDG_KR_RELOAD_KEY 0xaaaa
167 #define IWDG_EWCR 0x14
168 #define IWDG_EWCR_EWIC BIT(14)
170 #define STM32MP1_PSCI_NR_CPUS 2
171 #if STM32MP1_PSCI_NR_CPUS > CONFIG_ARMV7_PSCI_NR_CPUS
172 #error "invalid value for CONFIG_ARMV7_PSCI_NR_CPUS"
175 u8 psci_state[STM32MP1_PSCI_NR_CPUS] __secure_data = {
176 PSCI_AFFINITY_LEVEL_ON,
177 PSCI_AFFINITY_LEVEL_OFF};
179 static u32 __secure_data cntfrq;
181 static u32 __secure cp15_read_cntfrq(void)
185 asm volatile("mrc p15, 0, %0, c14, c0, 0" : "=r" (frq));
190 static void __secure cp15_write_cntfrq(u32 frq)
192 asm volatile ("mcr p15, 0, %0, c14, c0, 0" : : "r" (frq));
195 static inline void psci_set_state(int cpu, u8 state)
197 psci_state[cpu] = state;
202 static u32 __secure stm32mp_get_gicd_base_address(void)
206 /* get the GIC base address from the CBAR register */
207 asm("mrc p15, 4, %0, c15, c0, 0\n" : "=r" (periphbase));
209 return (periphbase & CBAR_MASK) + GIC_DIST_OFFSET;
212 static void __secure stm32mp_raise_sgi0(int cpu)
216 gic_dist_addr = stm32mp_get_gicd_base_address();
218 /* ask cpu with SGI0 */
219 writel((BIT(cpu) << 16), gic_dist_addr + GICD_SGIR);
222 void __secure psci_arch_cpu_entry(void)
224 u32 cpu = psci_get_cpu_id();
226 psci_set_state(cpu, PSCI_AFFINITY_LEVEL_ON);
228 /* write the saved cntfrq */
229 cp15_write_cntfrq(cntfrq);
231 /* reset magic in TAMP register */
232 writel(0xFFFFFFFF, TAMP_BACKUP_MAGIC_NUMBER);
235 s32 __secure psci_features(u32 function_id, u32 psci_fid)
238 case ARM_PSCI_0_2_FN_PSCI_VERSION:
239 case ARM_PSCI_0_2_FN_CPU_OFF:
240 case ARM_PSCI_0_2_FN_CPU_ON:
241 case ARM_PSCI_0_2_FN_AFFINITY_INFO:
242 case ARM_PSCI_0_2_FN_MIGRATE_INFO_TYPE:
243 case ARM_PSCI_0_2_FN_SYSTEM_OFF:
244 case ARM_PSCI_0_2_FN_SYSTEM_RESET:
245 case ARM_PSCI_1_0_FN_SYSTEM_SUSPEND:
248 return ARM_PSCI_RET_NI;
251 u32 __secure psci_version(void)
253 return ARM_PSCI_VER_1_0;
256 s32 __secure psci_affinity_info(u32 function_id, u32 target_affinity,
257 u32 lowest_affinity_level)
259 u32 cpu = target_affinity & MPIDR_AFF0;
261 if (lowest_affinity_level > 0)
262 return ARM_PSCI_RET_INVAL;
264 if (target_affinity & ~MPIDR_AFF0)
265 return ARM_PSCI_RET_INVAL;
267 if (cpu >= STM32MP1_PSCI_NR_CPUS)
268 return ARM_PSCI_RET_INVAL;
270 return psci_state[cpu];
273 u32 __secure psci_migrate_info_type(void)
276 * in Power_State_Coordination_Interface_PDD_v1_1_DEN0022D.pdf
277 * return 2 = Trusted OS is either not present or does not require
278 * migration, system of this type does not require the caller
279 * to use the MIGRATE function.
280 * MIGRATE function calls return NOT_SUPPORTED.
285 s32 __secure psci_cpu_on(u32 function_id, u32 target_cpu, u32 pc,
288 u32 cpu = target_cpu & MPIDR_AFF0;
290 if (target_cpu & ~MPIDR_AFF0)
291 return ARM_PSCI_RET_INVAL;
293 if (cpu >= STM32MP1_PSCI_NR_CPUS)
294 return ARM_PSCI_RET_INVAL;
296 if (psci_state[cpu] == PSCI_AFFINITY_LEVEL_ON)
297 return ARM_PSCI_RET_ALREADY_ON;
299 /* read and save cntfrq of current cpu to write on target cpu */
300 cntfrq = cp15_read_cntfrq();
302 /* reset magic in TAMP register */
303 if (readl(TAMP_BACKUP_MAGIC_NUMBER))
304 writel(0xFFFFFFFF, TAMP_BACKUP_MAGIC_NUMBER);
306 * ROM code need a first SGI0 after core reset
307 * core is ready when magic is set to 0 in ROM code
309 while (readl(TAMP_BACKUP_MAGIC_NUMBER))
310 stm32mp_raise_sgi0(cpu);
312 /* store target PC and context id*/
313 psci_save(cpu, pc, context_id);
315 /* write entrypoint in backup RAM register */
316 writel((u32)&psci_cpu_entry, TAMP_BACKUP_BRANCH_ADDRESS);
317 psci_set_state(cpu, PSCI_AFFINITY_LEVEL_ON_PENDING);
319 /* write magic number in backup register */
321 writel(BOOT_API_A7_CORE1_MAGIC_NUMBER,
322 TAMP_BACKUP_MAGIC_NUMBER);
324 writel(BOOT_API_A7_CORE0_MAGIC_NUMBER,
325 TAMP_BACKUP_MAGIC_NUMBER);
327 /* Generate an IT to start the core */
328 stm32mp_raise_sgi0(cpu);
330 return ARM_PSCI_RET_SUCCESS;
333 s32 __secure psci_cpu_off(void)
337 cpu = psci_get_cpu_id();
339 psci_cpu_off_common();
340 psci_set_state(cpu, PSCI_AFFINITY_LEVEL_OFF);
342 /* reset core: wfi is managed by BootRom */
344 writel(RCC_MP_GRSTCSETR_MPUP1RST, RCC_MP_GRSTCSETR);
346 writel(RCC_MP_GRSTCSETR_MPUP0RST, RCC_MP_GRSTCSETR);
348 /* just waiting reset */
353 void __secure psci_system_reset(void)
356 writel(RCC_MP_GRSTCSETR_MPSYSRST, RCC_MP_GRSTCSETR);
357 /* just waiting reset */
362 void __secure psci_system_off(void)
364 /* System Off is not managed, waiting user power off
365 * TODO: handle I2C write in PMIC Main Control register bit 0 = SWOFF
371 static void __secure secure_udelay(unsigned int delay)
373 u32 freq = cp15_read_cntfrq() / 1000000;
378 asm volatile("mrrc p15, 0, %Q0, %R0, c14" : "=r" (start));
380 asm volatile("mrrc p15, 0, %Q0, %R0, c14" : "=r" (end));
381 if ((end - start) > delay)
386 static int __secure secure_waitbits(u32 reg, u32 mask, u32 val)
388 u32 freq = cp15_read_cntfrq() / 1000000;
389 u32 delay = 500 * freq; /* 500 us */
393 asm volatile("mrrc p15, 0, %Q0, %R0, c14" : "=r" (start));
397 if ((tmp & val) == val)
399 asm volatile("mrrc p15, 0, %Q0, %R0, c14" : "=r" (end));
400 if ((end - start) > delay)
405 static void __secure ddr_sr_mode_ssr(u32 *saved_pwrctl)
407 setbits_le32(STM32_RCC_BASE + RCC_DDRITFCR,
408 RCC_DDRITFCR_DDRC1LPEN | RCC_DDRITFCR_DDRC1EN |
409 RCC_DDRITFCR_DDRC2LPEN | RCC_DDRITFCR_DDRC2EN |
410 RCC_DDRITFCR_DDRCAPBLPEN | RCC_DDRITFCR_DDRPHYCAPBLPEN |
411 RCC_DDRITFCR_DDRCAPBEN | RCC_DDRITFCR_DDRPHYCAPBEN |
412 RCC_DDRITFCR_DDRPHYCEN);
414 clrbits_le32(STM32_RCC_BASE + RCC_DDRITFCR,
415 RCC_DDRITFCR_AXIDCGEN | RCC_DDRITFCR_DDRCKMOD_MASK);
417 /* Disable HW LP interface of uMCTL2 */
418 clrbits_le32(STM32_DDRCTRL_BASE + DDRCTRL_HWLPCTL,
419 DDRCTRL_HWLPCTL_HW_LP_EN);
421 /* Configure Automatic LP modes of uMCTL2 */
422 clrsetbits_le32(STM32_DDRCTRL_BASE + DDRCTRL_PWRTMG,
423 DDRCTRL_PWRTMG_SELFREF_TO_X32_MASK,
424 DDRCTRL_PWRTMG_SELFREF_TO_X32_0);
426 /* Save PWRCTL register to restart ASR after suspend (if applicable) */
427 *saved_pwrctl = readl(STM32_DDRCTRL_BASE + DDRCTRL_PWRCTL);
430 * Disable Clock disable with LP modes
431 * (used in RUN mode for LPDDR2 with specific timing).
433 clrbits_le32(STM32_DDRCTRL_BASE + DDRCTRL_PWRCTL,
434 DDRCTRL_PWRCTL_EN_DFI_DRAM_CLK_DISABLE);
436 /* Disable automatic Self-Refresh mode */
437 clrbits_le32(STM32_DDRCTRL_BASE + DDRCTRL_PWRCTL,
438 DDRCTRL_PWRCTL_SELFREF_EN);
441 static void __secure ddr_sr_mode_restore(u32 saved_pwrctl)
443 saved_pwrctl &= DDRCTRL_PWRCTL_EN_DFI_DRAM_CLK_DISABLE |
444 DDRCTRL_PWRCTL_SELFREF_EN;
446 /* Restore ASR mode in case it was enabled before suspend. */
447 setbits_le32(STM32_DDRCTRL_BASE + DDRCTRL_PWRCTL, saved_pwrctl);
450 static int __secure ddr_sw_self_refresh_in(void)
454 clrbits_le32(STM32_RCC_BASE + RCC_DDRITFCR, RCC_DDRITFCR_AXIDCGEN);
456 /* Blocks AXI ports from taking anymore transactions */
457 clrbits_le32(STM32_DDRCTRL_BASE + DDRCTRL_PCTRL_0,
458 DDRCTRL_PCTRL_N_PORT_EN);
459 clrbits_le32(STM32_DDRCTRL_BASE + DDRCTRL_PCTRL_1,
460 DDRCTRL_PCTRL_N_PORT_EN);
463 * Waits unit all AXI ports are idle
464 * Poll PSTAT.rd_port_busy_n = 0
465 * Poll PSTAT.wr_port_busy_n = 0
467 ret = secure_waitbits(STM32_DDRCTRL_BASE + DDRCTRL_PSTAT,
468 DDRCTRL_PSTAT_RD_PORT_BUSY_0 |
469 DDRCTRL_PSTAT_RD_PORT_BUSY_1 |
470 DDRCTRL_PSTAT_WR_PORT_BUSY_0 |
471 DDRCTRL_PSTAT_WR_PORT_BUSY_1, 0);
475 /* SW Self-Refresh entry */
476 setbits_le32(STM32_DDRCTRL_BASE + DDRCTRL_PWRCTL, DDRCTRL_PWRCTL_SELFREF_SW);
479 * Wait operating mode change in self-refresh mode
480 * with STAT.operating_mode[1:0]==11.
481 * Ensure transition to self-refresh was due to software
482 * by checking also that STAT.selfref_type[1:0]=2.
484 ret = secure_waitbits(STM32_DDRCTRL_BASE + DDRCTRL_STAT,
485 DDRCTRL_STAT_OPERATING_MODE_MASK |
486 DDRCTRL_STAT_SELFREF_TYPE_MASK,
487 DDRCTRL_STAT_OPERATING_MODE_SR |
488 DDRCTRL_STAT_SELFREF_TYPE_SR);
490 goto selfref_sw_failed;
492 /* IOs powering down (PUBL registers) */
493 setbits_le32(STM32_DDRPHYC_BASE + DDRPHYC_ACIOCR, DDRPHYC_ACIOCR_ACPDD);
494 setbits_le32(STM32_DDRPHYC_BASE + DDRPHYC_ACIOCR, DDRPHYC_ACIOCR_ACPDR);
496 clrsetbits_le32(STM32_DDRPHYC_BASE + DDRPHYC_ACIOCR,
497 DDRPHYC_ACIOCR_CKPDD_MASK,
498 DDRPHYC_ACIOCR_CKPDD_0);
500 clrsetbits_le32(STM32_DDRPHYC_BASE + DDRPHYC_ACIOCR,
501 DDRPHYC_ACIOCR_CKPDR_MASK,
502 DDRPHYC_ACIOCR_CKPDR_0);
504 clrsetbits_le32(STM32_DDRPHYC_BASE + DDRPHYC_ACIOCR,
505 DDRPHYC_ACIOCR_CSPDD_MASK,
506 DDRPHYC_ACIOCR_CSPDD_0);
508 /* Disable command/address output driver */
509 clrbits_le32(STM32_DDRPHYC_BASE + DDRPHYC_ACIOCR, DDRPHYC_ACIOCR_ACOE);
511 setbits_le32(STM32_DDRPHYC_BASE + DDRPHYC_DXCCR, DDRPHYC_DXCCR_DXPDD);
513 setbits_le32(STM32_DDRPHYC_BASE + DDRPHYC_DXCCR, DDRPHYC_DXCCR_DXPDR);
515 clrsetbits_le32(STM32_DDRPHYC_BASE + DDRPHYC_DSGCR,
516 DDRPHYC_DSGCR_ODTPDD_MASK,
517 DDRPHYC_DSGCR_ODTPDD_0);
519 setbits_le32(STM32_DDRPHYC_BASE + DDRPHYC_DSGCR, DDRPHYC_DSGCR_NL2PD);
521 clrsetbits_le32(STM32_DDRPHYC_BASE + DDRPHYC_DSGCR,
522 DDRPHYC_DSGCR_CKEPDD_MASK,
523 DDRPHYC_DSGCR_CKEPDD_0);
525 /* Disable PZQ cell (PUBL register) */
526 setbits_le32(STM32_DDRPHYC_BASE + DDRPHYC_ZQ0CR0, DDRPHYC_ZQ0CRN_ZQPD);
529 clrbits_le32(STM32_DDRPHYC_BASE + DDRPHYC_DSGCR, DDRPHYC_DSGCR_CKOE);
531 /* Additional delay to avoid early latch */
534 /* Activate sw retention in PWRCTRL */
535 setbits_le32(STM32_PWR_BASE + PWR_CR3, PWR_CR3_DDRRETEN);
537 /* Switch controller clocks (uMCTL2/PUBL) to DLL ref clock */
538 setbits_le32(STM32_RCC_BASE + RCC_DDRITFCR, RCC_DDRITFCR_GSKPCTRL);
540 /* Disable all DLLs: GLITCH window */
541 setbits_le32(STM32_DDRPHYC_BASE + DDRPHYC_ACDLLCR, DDRPHYC_ACDLLCR_DLLDIS);
543 setbits_le32(STM32_DDRPHYC_BASE + DDRPHYC_DX0DLLCR, DDRPHYC_DXNDLLCR_DLLDIS);
545 setbits_le32(STM32_DDRPHYC_BASE + DDRPHYC_DX1DLLCR, DDRPHYC_DXNDLLCR_DLLDIS);
547 setbits_le32(STM32_DDRPHYC_BASE + DDRPHYC_DX2DLLCR, DDRPHYC_DXNDLLCR_DLLDIS);
549 setbits_le32(STM32_DDRPHYC_BASE + DDRPHYC_DX3DLLCR, DDRPHYC_DXNDLLCR_DLLDIS);
551 /* Switch controller clocks (uMCTL2/PUBL) to DLL output clock */
552 clrbits_le32(STM32_RCC_BASE + RCC_DDRITFCR, RCC_DDRITFCR_GSKPCTRL);
554 /* Deactivate all DDR clocks */
555 clrbits_le32(STM32_RCC_BASE + RCC_DDRITFCR,
556 RCC_DDRITFCR_DDRC1EN | RCC_DDRITFCR_DDRC2EN |
557 RCC_DDRITFCR_DDRCAPBEN | RCC_DDRITFCR_DDRPHYCAPBEN);
562 /* This bit should be cleared to restore DDR in its previous state */
563 clrbits_le32(STM32_DDRCTRL_BASE + DDRCTRL_PWRCTL,
564 DDRCTRL_PWRCTL_SELFREF_SW);
567 setbits_le32(STM32_DDRCTRL_BASE + DDRCTRL_PCTRL_0,
568 DDRCTRL_PCTRL_N_PORT_EN);
569 setbits_le32(STM32_DDRCTRL_BASE + DDRCTRL_PCTRL_1,
570 DDRCTRL_PCTRL_N_PORT_EN);
575 static void __secure ddr_sw_self_refresh_exit(void)
579 /* Enable all clocks */
580 setbits_le32(STM32_RCC_BASE + RCC_DDRITFCR,
581 RCC_DDRITFCR_DDRC1EN | RCC_DDRITFCR_DDRC2EN |
582 RCC_DDRITFCR_DDRPHYCEN | RCC_DDRITFCR_DDRPHYCAPBEN |
583 RCC_DDRITFCR_DDRCAPBEN);
586 clrbits_le32(STM32_DDRCTRL_BASE + DDRCTRL_SWCTL, DDRCTRL_SWCTL_SW_DONE);
588 /* Mask dfi_init_complete_en */
589 clrbits_le32(STM32_DDRCTRL_BASE + DDRCTRL_DFIMISC,
590 DDRCTRL_DFIMISC_DFI_INIT_COMPLETE_EN);
593 setbits_le32(STM32_DDRCTRL_BASE + DDRCTRL_SWCTL, DDRCTRL_SWCTL_SW_DONE);
594 ret = secure_waitbits(STM32_DDRCTRL_BASE + DDRCTRL_SWSTAT,
595 DDRCTRL_SWSTAT_SW_DONE_ACK,
596 DDRCTRL_SWSTAT_SW_DONE_ACK);
600 /* Switch controller clocks (uMCTL2/PUBL) to DLL ref clock */
601 setbits_le32(STM32_RCC_BASE + RCC_DDRITFCR, RCC_DDRITFCR_GSKPCTRL);
603 /* Enable all DLLs: GLITCH window */
604 clrbits_le32(STM32_DDRPHYC_BASE + DDRPHYC_ACDLLCR,
605 DDRPHYC_ACDLLCR_DLLDIS);
607 clrbits_le32(STM32_DDRPHYC_BASE + DDRPHYC_DX0DLLCR, DDRPHYC_DXNDLLCR_DLLDIS);
609 clrbits_le32(STM32_DDRPHYC_BASE + DDRPHYC_DX1DLLCR, DDRPHYC_DXNDLLCR_DLLDIS);
611 clrbits_le32(STM32_DDRPHYC_BASE + DDRPHYC_DX2DLLCR, DDRPHYC_DXNDLLCR_DLLDIS);
613 clrbits_le32(STM32_DDRPHYC_BASE + DDRPHYC_DX3DLLCR, DDRPHYC_DXNDLLCR_DLLDIS);
615 /* Additional delay to avoid early DLL clock switch */
618 /* Switch controller clocks (uMCTL2/PUBL) to DLL ref clock */
619 clrbits_le32(STM32_RCC_BASE + RCC_DDRITFCR, RCC_DDRITFCR_GSKPCTRL);
621 clrbits_le32(STM32_DDRPHYC_BASE + DDRPHYC_ACDLLCR, DDRPHYC_ACDLLCR_DLLSRST);
625 setbits_le32(STM32_DDRPHYC_BASE + DDRPHYC_ACDLLCR, DDRPHYC_ACDLLCR_DLLSRST);
627 /* PHY partial init: (DLL lock and ITM reset) */
628 writel(DDRPHYC_PIR_DLLSRST | DDRPHYC_PIR_DLLLOCK |
629 DDRPHYC_PIR_ITMSRST | DDRPHYC_PIR_INIT,
630 STM32_DDRPHYC_BASE + DDRPHYC_PIR);
632 /* Need to wait at least 10 clock cycles before accessing PGSR */
635 /* Pool end of init */
636 ret = secure_waitbits(STM32_DDRPHYC_BASE + DDRPHYC_PGSR,
637 DDRPHYC_PGSR_IDONE, DDRPHYC_PGSR_IDONE);
642 clrbits_le32(STM32_DDRCTRL_BASE + DDRCTRL_SWCTL, DDRCTRL_SWCTL_SW_DONE);
644 /* Unmask dfi_init_complete_en to uMCTL2 */
645 setbits_le32(STM32_DDRCTRL_BASE + DDRCTRL_DFIMISC, DDRCTRL_DFIMISC_DFI_INIT_COMPLETE_EN);
648 setbits_le32(STM32_DDRCTRL_BASE + DDRCTRL_SWCTL, DDRCTRL_SWCTL_SW_DONE);
649 ret = secure_waitbits(STM32_DDRCTRL_BASE + DDRCTRL_SWSTAT,
650 DDRCTRL_SWSTAT_SW_DONE_ACK,
651 DDRCTRL_SWSTAT_SW_DONE_ACK);
655 /* Deactivate sw retention in PWR */
656 clrbits_le32(STM32_PWR_BASE + PWR_CR3, PWR_CR3_DDRRETEN);
658 /* Enable PZQ cell (PUBL register) */
659 clrbits_le32(STM32_DDRPHYC_BASE + DDRPHYC_ZQ0CR0, DDRPHYC_ZQ0CRN_ZQPD);
661 /* Enable pad drivers */
662 clrbits_le32(STM32_DDRPHYC_BASE + DDRPHYC_ACIOCR, DDRPHYC_ACIOCR_ACPDD);
664 /* Enable command/address output driver */
665 setbits_le32(STM32_DDRPHYC_BASE + DDRPHYC_ACIOCR, DDRPHYC_ACIOCR_ACOE);
667 clrbits_le32(STM32_DDRPHYC_BASE + DDRPHYC_ACIOCR, DDRPHYC_ACIOCR_CKPDD_MASK);
669 clrbits_le32(STM32_DDRPHYC_BASE + DDRPHYC_ACIOCR, DDRPHYC_ACIOCR_CSPDD_MASK);
671 clrbits_le32(STM32_DDRPHYC_BASE + DDRPHYC_DXCCR, DDRPHYC_DXCCR_DXPDD);
673 clrbits_le32(STM32_DDRPHYC_BASE + DDRPHYC_DXCCR, DDRPHYC_DXCCR_DXPDR);
676 setbits_le32(STM32_DDRPHYC_BASE + DDRPHYC_DSGCR, DDRPHYC_DSGCR_CKOE);
678 clrbits_le32(STM32_DDRPHYC_BASE + DDRPHYC_DSGCR, DDRPHYC_DSGCR_ODTPDD_MASK);
680 clrbits_le32(STM32_DDRPHYC_BASE + DDRPHYC_DSGCR, DDRPHYC_DSGCR_NL2PD);
682 clrbits_le32(STM32_DDRPHYC_BASE + DDRPHYC_DSGCR, DDRPHYC_DSGCR_CKEPDD_MASK);
684 /* Remove selfrefresh */
685 clrbits_le32(STM32_DDRCTRL_BASE + DDRCTRL_PWRCTL, DDRCTRL_PWRCTL_SELFREF_SW);
687 /* Wait operating_mode == normal */
688 ret = secure_waitbits(STM32_DDRCTRL_BASE + DDRCTRL_STAT,
689 DDRCTRL_STAT_OPERATING_MODE_MASK,
690 DDRCTRL_STAT_OPERATING_MODE_NORMAL);
694 /* AXI ports are no longer blocked from taking transactions */
695 setbits_le32(STM32_DDRCTRL_BASE + DDRCTRL_PCTRL_0, DDRCTRL_PCTRL_N_PORT_EN);
696 setbits_le32(STM32_DDRCTRL_BASE + DDRCTRL_PCTRL_1, DDRCTRL_PCTRL_N_PORT_EN);
698 setbits_le32(STM32_RCC_BASE + RCC_DDRITFCR, RCC_DDRITFCR_AXIDCGEN);
701 void __secure psci_system_suspend(u32 __always_unused function_id,
702 u32 ep, u32 context_id)
704 u32 saved_mcudivr, saved_pll3cr, saved_pll4cr, saved_mssckselr;
705 u32 gicd_addr = stm32mp_get_gicd_base_address();
706 u32 cpu = psci_get_cpu_id();
707 u32 sp = (u32)__secure_stack_end - (cpu << ARM_PSCI_STACK_SHIFT);
708 bool iwdg1_wake = false;
709 bool iwdg2_wake = false;
710 bool other_wake = false;
711 u32 saved_pwrctl, reg;
716 /* Cache enable mask of all 256 SPI */
717 for (i = 0; i < ARRAY_SIZE(gic_enabled); i++)
718 gic_enabled[i] = readl(gicd_addr + GICD_ISENABLERn + 0x4 + 4 * i);
720 /* Disable IO compensation */
722 /* Place current APSRC/ANSRC into RAPSRC/RANSRC */
723 reg = readl(STM32_SYSCFG_BASE + SYSCFG_CMPCR);
726 reg |= SYSCFG_CMPCR_SW_CTRL;
727 writel(reg, STM32_SYSCFG_BASE + SYSCFG_CMPCR);
728 writel(SYSCFG_CMPENR_MPUEN, STM32_SYSCFG_BASE + SYSCFG_CMPENCLRR);
730 writel(RCC_MP_CIFR_WKUPF, STM32_RCC_BASE + RCC_MP_CIFR);
731 setbits_le32(STM32_RCC_BASE + RCC_MP_CIER, RCC_MP_CIFR_WKUPF);
733 setbits_le32(STM32_PWR_BASE + PWR_MPUCR,
734 PWR_MPUCR_CSSF | PWR_MPUCR_CSTDBYDIS);
736 saved_mcudivr = readl(STM32_RCC_BASE + RCC_MCUDIVR);
737 saved_pll3cr = readl(STM32_RCC_BASE + RCC_PLL3CR);
738 saved_pll4cr = readl(STM32_RCC_BASE + RCC_PLL4CR);
739 saved_mssckselr = readl(STM32_RCC_BASE + RCC_MSSCKSELR);
741 psci_v7_flush_dcache_all();
742 ddr_sr_mode_ssr(&saved_pwrctl);
743 ddr_sw_self_refresh_in();
744 setbits_le32(STM32_PWR_BASE + PWR_CR3, PWR_CR3_DDRSREN);
745 writel(0x3, STM32_RCC_BASE + RCC_MP_SREQSETR);
747 /* Ping the IWDG before entering suspend */
748 iwdg1_wake = !!(gic_enabled[4] & BIT(22)); /* SPI 150 */
749 iwdg2_wake = !!(gic_enabled[4] & BIT(23)); /* SPI 151 */
752 /* Ping IWDG1 and ACK pretimer IRQ */
754 writel(IWDG_KR_RELOAD_KEY, STM32_IWDG1_BASE + IWDG_KR);
755 writel(IWDG_EWCR_EWIC, STM32_IWDG1_BASE + IWDG_EWCR);
758 /* Ping IWDG2 and ACK pretimer IRQ */
760 writel(IWDG_KR_RELOAD_KEY, STM32_IWDG2_BASE + IWDG_KR);
761 writel(IWDG_EWCR_EWIC, STM32_IWDG2_BASE + IWDG_EWCR);
767 /* Zzz, enter stop mode */
773 /* Determine the wake up source */
774 for (i = 0; i < ARRAY_SIZE(gic_enabled); i++) {
775 irqs = readl(gicd_addr + GICR_IGROUPMODRn + 0x4 + 4 * i);
776 irqs &= gic_enabled[i];
780 /* Test whether IWDG pretimeout triggered the wake up. */
781 if (i == 4) { /* SPI Num 128..159 */
782 iwdg1_wake = !!(irqs & BIT(22)); /* SPI 150 */
783 iwdg2_wake = !!(irqs & BIT(23)); /* SPI 151 */
784 irqs &= ~(BIT(22) | BIT(23));
787 /* Test whether there is any other wake up trigger. */
794 /* Other wake up triggers pending, let OS deal with all of it. */
799 writel(0x3, STM32_RCC_BASE + RCC_MP_SREQCLRR);
800 ddr_sw_self_refresh_exit();
801 ddr_sr_mode_restore(saved_pwrctl);
803 writel(saved_mcudivr, STM32_RCC_BASE + RCC_MCUDIVR);
804 writel(saved_pll3cr, STM32_RCC_BASE + RCC_PLL3CR);
805 writel(saved_pll4cr, STM32_RCC_BASE + RCC_PLL4CR);
806 writel(saved_mssckselr, STM32_RCC_BASE + RCC_MSSCKSELR);
808 writel(SYSCFG_CMPENR_MPUEN, STM32_SYSCFG_BASE + SYSCFG_CMPENSETR);
809 clrbits_le32(STM32_SYSCFG_BASE + SYSCFG_CMPCR, SYSCFG_CMPCR_SW_CTRL);
812 * The system has resumed successfully. Rewrite LR register stored
813 * on stack with 'ep' value, so that on return from this PSCI call,
814 * the code would jump to that 'ep' resume entry point code path
815 * instead of the previous 'lr' register content which (e.g. with
816 * Linux) points to resume failure code path.
818 * See arch/arm/cpu/armv7/psci.S _smc_psci: for the stack layout
819 * used here, SP-4 is PC, SP-8 is LR, SP-12 is R7, and so on.