blob: b60ccab276c7835910869b4a7c7a510ef05bfe5c [file] [log] [blame]
Jorge Ramirez-Ortiz5ff5eee2018-09-23 09:41:10 +02001/*
Toshiyuki Ogasahara77626582020-11-30 20:39:21 +09002 * Copyright (c) 2015-2021, Renesas Electronics Corporation. All rights reserved.
Jorge Ramirez-Ortiz5ff5eee2018-09-23 09:41:10 +02003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
Jorge Ramirez-Ortiz5ff5eee2018-09-23 09:41:10 +02007#include <assert.h>
Jorge Ramirez-Ortiz5ff5eee2018-09-23 09:41:10 +02008#include <string.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +00009
10#include <arch.h>
11#include <arch_helpers.h>
12#include <common/debug.h>
13#include <lib/bakery_lock.h>
14#include <lib/mmio.h>
15#include <lib/xlat_tables/xlat_tables_v2.h>
Toshiyuki Ogasaharab67a8ca2019-03-22 16:14:00 +090016#include <plat/common/platform.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000017
Jorge Ramirez-Ortiz5ff5eee2018-09-23 09:41:10 +020018#include "iic_dvfs.h"
Marek Vasut4bc543c2018-12-28 20:15:33 +010019#include "micro_delay.h"
Jorge Ramirez-Ortiz5ff5eee2018-09-23 09:41:10 +020020#include "pwrc.h"
Biju Dasb99eb052020-12-13 20:24:19 +000021#include "rcar_def.h"
22#include "rcar_private.h"
Toshiyuki Ogasahara77626582020-11-30 20:39:21 +090023#include "cpg_registers.h"
Jorge Ramirez-Ortiz5ff5eee2018-09-23 09:41:10 +020024
25/*
26 * Someday there will be a generic power controller api. At the moment each
27 * platform has its own pwrc so just exporting functions should be acceptable.
28 */
29RCAR_INSTANTIATE_LOCK
30
Biju Dasb99eb052020-12-13 20:24:19 +000031#define WUP_IRQ_SHIFT (0U)
32#define WUP_FIQ_SHIFT (8U)
33#define WUP_CSD_SHIFT (16U)
34#define BIT_SOFTRESET (1U << 15)
35#define BIT_CA53_SCU (1U << 21)
36#define BIT_CA57_SCU (1U << 12)
37#define REQ_RESUME (1U << 1)
38#define REQ_OFF (1U << 0)
39#define STATUS_PWRUP (1U << 4)
40#define STATUS_PWRDOWN (1U << 0)
41#define STATE_CA57_CPU (27U)
42#define STATE_CA53_CPU (22U)
43#define MODE_L2_DOWN (0x00000002U)
44#define CPU_PWR_OFF (0x00000003U)
45#define RCAR_PSTR_MASK (0x00000003U)
46#define ST_ALL_STANDBY (0x00003333U)
Toshiyuki Ogasaharac2dfd8e2021-07-12 19:13:17 +090047#define SYSCEXTMASK_EXTMSK0 (0x00000001U)
Jorge Ramirez-Ortiz5ff5eee2018-09-23 09:41:10 +020048/* Suspend to ram */
Biju Dasb99eb052020-12-13 20:24:19 +000049#define DBSC4_REG_BASE (0xE6790000U)
50#define DBSC4_REG_DBSYSCNT0 (DBSC4_REG_BASE + 0x0100U)
51#define DBSC4_REG_DBACEN (DBSC4_REG_BASE + 0x0200U)
52#define DBSC4_REG_DBCMD (DBSC4_REG_BASE + 0x0208U)
53#define DBSC4_REG_DBRFEN (DBSC4_REG_BASE + 0x0204U)
54#define DBSC4_REG_DBWAIT (DBSC4_REG_BASE + 0x0210U)
55#define DBSC4_REG_DBCALCNF (DBSC4_REG_BASE + 0x0424U)
56#define DBSC4_REG_DBDFIPMSTRCNF (DBSC4_REG_BASE + 0x0520U)
57#define DBSC4_REG_DBPDLK0 (DBSC4_REG_BASE + 0x0620U)
58#define DBSC4_REG_DBPDRGA0 (DBSC4_REG_BASE + 0x0624U)
59#define DBSC4_REG_DBPDRGD0 (DBSC4_REG_BASE + 0x0628U)
60#define DBSC4_REG_DBCAM0CTRL0 (DBSC4_REG_BASE + 0x0940U)
61#define DBSC4_REG_DBCAM0STAT0 (DBSC4_REG_BASE + 0x0980U)
62#define DBSC4_REG_DBCAM1STAT0 (DBSC4_REG_BASE + 0x0990U)
63#define DBSC4_REG_DBCAM2STAT0 (DBSC4_REG_BASE + 0x09A0U)
64#define DBSC4_REG_DBCAM3STAT0 (DBSC4_REG_BASE + 0x09B0U)
65#define DBSC4_BIT_DBACEN_ACCEN ((uint32_t)(1U << 0))
66#define DBSC4_BIT_DBRFEN_ARFEN ((uint32_t)(1U << 0))
67#define DBSC4_BIT_DBCAMxSTAT0 (0x00000001U)
68#define DBSC4_BIT_DBDFIPMSTRCNF_PMSTREN (0x00000001U)
69#define DBSC4_SET_DBCMD_OPC_PRE (0x04000000U)
70#define DBSC4_SET_DBCMD_OPC_SR (0x0A000000U)
71#define DBSC4_SET_DBCMD_OPC_PD (0x08000000U)
72#define DBSC4_SET_DBCMD_OPC_MRW (0x0E000000U)
73#define DBSC4_SET_DBCMD_CH_ALL (0x00800000U)
74#define DBSC4_SET_DBCMD_RANK_ALL (0x00040000U)
75#define DBSC4_SET_DBCMD_ARG_ALL (0x00000010U)
76#define DBSC4_SET_DBCMD_ARG_ENTER (0x00000000U)
77#define DBSC4_SET_DBCMD_ARG_MRW_ODTC (0x00000B00U)
78#define DBSC4_SET_DBSYSCNT0_WRITE_ENABLE (0x00001234U)
79#define DBSC4_SET_DBSYSCNT0_WRITE_DISABLE (0x00000000U)
80#define DBSC4_SET_DBPDLK0_PHY_ACCESS (0x0000A55AU)
81#define DBSC4_SET_DBPDRGA0_ACIOCR0 (0x0000001AU)
82#define DBSC4_SET_DBPDRGD0_ACIOCR0 (0x33C03C11U)
83#define DBSC4_SET_DBPDRGA0_DXCCR (0x00000020U)
84#define DBSC4_SET_DBPDRGD0_DXCCR (0x00181006U)
85#define DBSC4_SET_DBPDRGA0_PGCR1 (0x00000003U)
86#define DBSC4_SET_DBPDRGD0_PGCR1 (0x0380C600U)
87#define DBSC4_SET_DBPDRGA0_ACIOCR1 (0x0000001BU)
88#define DBSC4_SET_DBPDRGD0_ACIOCR1 (0xAAAAAAAAU)
89#define DBSC4_SET_DBPDRGA0_ACIOCR3 (0x0000001DU)
90#define DBSC4_SET_DBPDRGD0_ACIOCR3 (0xAAAAAAAAU)
91#define DBSC4_SET_DBPDRGA0_ACIOCR5 (0x0000001FU)
92#define DBSC4_SET_DBPDRGD0_ACIOCR5 (0x000000AAU)
93#define DBSC4_SET_DBPDRGA0_DX0GCR2 (0x000000A2U)
94#define DBSC4_SET_DBPDRGD0_DX0GCR2 (0xAAAA0000U)
95#define DBSC4_SET_DBPDRGA0_DX1GCR2 (0x000000C2U)
96#define DBSC4_SET_DBPDRGD0_DX1GCR2 (0xAAAA0000U)
97#define DBSC4_SET_DBPDRGA0_DX2GCR2 (0x000000E2U)
98#define DBSC4_SET_DBPDRGD0_DX2GCR2 (0xAAAA0000U)
99#define DBSC4_SET_DBPDRGA0_DX3GCR2 (0x00000102U)
100#define DBSC4_SET_DBPDRGD0_DX3GCR2 (0xAAAA0000U)
101#define DBSC4_SET_DBPDRGA0_ZQCR (0x00000090U)
102#define DBSC4_SET_DBPDRGD0_ZQCR_MD19_0 (0x04058904U)
103#define DBSC4_SET_DBPDRGD0_ZQCR_MD19_1 (0x04058A04U)
104#define DBSC4_SET_DBPDRGA0_DX0GCR0 (0x000000A0U)
105#define DBSC4_SET_DBPDRGD0_DX0GCR0 (0x7C0002E5U)
106#define DBSC4_SET_DBPDRGA0_DX1GCR0 (0x000000C0U)
107#define DBSC4_SET_DBPDRGD0_DX1GCR0 (0x7C0002E5U)
108#define DBSC4_SET_DBPDRGA0_DX2GCR0 (0x000000E0U)
109#define DBSC4_SET_DBPDRGD0_DX2GCR0 (0x7C0002E5U)
110#define DBSC4_SET_DBPDRGA0_DX3GCR0 (0x00000100U)
111#define DBSC4_SET_DBPDRGD0_DX3GCR0 (0x7C0002E5U)
112#define DBSC4_SET_DBPDRGA0_DX0GCR1 (0x000000A1U)
113#define DBSC4_SET_DBPDRGD0_DX0GCR1 (0x55550000U)
114#define DBSC4_SET_DBPDRGA0_DX1GCR1 (0x000000C1U)
115#define DBSC4_SET_DBPDRGD0_DX1GCR1 (0x55550000U)
116#define DBSC4_SET_DBPDRGA0_DX2GCR1 (0x000000E1U)
117#define DBSC4_SET_DBPDRGD0_DX2GCR1 (0x55550000U)
118#define DBSC4_SET_DBPDRGA0_DX3GCR1 (0x00000101U)
119#define DBSC4_SET_DBPDRGD0_DX3GCR1 (0x55550000U)
120#define DBSC4_SET_DBPDRGA0_DX0GCR3 (0x000000A3U)
121#define DBSC4_SET_DBPDRGD0_DX0GCR3 (0x00008484U)
122#define DBSC4_SET_DBPDRGA0_DX1GCR3 (0x000000C3U)
123#define DBSC4_SET_DBPDRGD0_DX1GCR3 (0x00008484U)
124#define DBSC4_SET_DBPDRGA0_DX2GCR3 (0x000000E3U)
125#define DBSC4_SET_DBPDRGD0_DX2GCR3 (0x00008484U)
126#define DBSC4_SET_DBPDRGA0_DX3GCR3 (0x00000103U)
127#define DBSC4_SET_DBPDRGD0_DX3GCR3 (0x00008484U)
128#define RST_BASE (0xE6160000U)
129#define RST_MODEMR (RST_BASE + 0x0060U)
130#define RST_MODEMR_BIT0 (0x00000001U)
Jorge Ramirez-Ortiz5ff5eee2018-09-23 09:41:10 +0200131
Toshiyuki Ogasaharab67a8ca2019-03-22 16:14:00 +0900132#define RCAR_CNTCR_OFF (0x00U)
133#define RCAR_CNTCVL_OFF (0x08U)
134#define RCAR_CNTCVU_OFF (0x0CU)
135#define RCAR_CNTFID_OFF (0x20U)
136
137#define RCAR_CNTCR_EN ((uint32_t)1U << 0U)
138#define RCAR_CNTCR_FCREQ(x) ((uint32_t)(x) << 8U)
139
Jorge Ramirez-Ortiz5ff5eee2018-09-23 09:41:10 +0200140#if PMIC_ROHM_BD9571
Biju Dasb99eb052020-12-13 20:24:19 +0000141#define BIT_BKUP_CTRL_OUT ((uint8_t)(1U << 4))
142#define PMIC_BKUP_MODE_CNT (0x20U)
143#define PMIC_QLLM_CNT (0x27U)
144#define PMIC_RETRY_MAX (100U)
145#endif /* PMIC_ROHM_BD9571 */
146#define SCTLR_EL3_M_BIT ((uint32_t)1U << 0)
147#define RCAR_CA53CPU_NUM_MAX (4U)
148#define RCAR_CA57CPU_NUM_MAX (4U)
149#define IS_A53A57(c) ((c) == RCAR_CLUSTER_A53A57)
150#define IS_CA57(c) ((c) == RCAR_CLUSTER_CA57)
151#define IS_CA53(c) ((c) == RCAR_CLUSTER_CA53)
Jorge Ramirez-Ortiz5ff5eee2018-09-23 09:41:10 +0200152
Julius Werner53456fc2019-07-09 13:49:11 -0700153#ifndef __ASSEMBLER__
Jorge Ramirez-Ortiz5ff5eee2018-09-23 09:41:10 +0200154IMPORT_SYM(unsigned long, __system_ram_start__, SYSTEM_RAM_START);
155IMPORT_SYM(unsigned long, __system_ram_end__, SYSTEM_RAM_END);
156IMPORT_SYM(unsigned long, __SRAM_COPY_START__, SRAM_COPY_START);
157#endif
158
Takuya Sakata59197472021-11-02 20:30:39 +0900159uint32_t rcar_pwrc_status(u_register_t mpidr)
Jorge Ramirez-Ortiz5ff5eee2018-09-23 09:41:10 +0200160{
161 uint32_t ret = 0;
162 uint64_t cm, cpu;
163 uint32_t reg;
164 uint32_t c;
165
166 rcar_lock_get();
167
168 c = rcar_pwrc_get_cluster();
169 cm = mpidr & MPIDR_CLUSTER_MASK;
170
171 if (!IS_A53A57(c) && cm != 0) {
172 ret = RCAR_INVALID;
173 goto done;
174 }
175
176 reg = mmio_read_32(RCAR_PRR);
177 cpu = mpidr & MPIDR_CPU_MASK;
178
179 if (IS_CA53(c))
180 if (reg & (1 << (STATE_CA53_CPU + cpu)))
181 ret = RCAR_INVALID;
182 if (IS_CA57(c))
183 if (reg & (1 << (STATE_CA57_CPU + cpu)))
184 ret = RCAR_INVALID;
185done:
186 rcar_lock_release();
187
188 return ret;
189}
190
Takuya Sakata59197472021-11-02 20:30:39 +0900191static void scu_power_up(u_register_t mpidr)
Jorge Ramirez-Ortiz5ff5eee2018-09-23 09:41:10 +0200192{
193 uintptr_t reg_pwrsr, reg_cpumcr, reg_pwron, reg_pwrer;
194 uint32_t c, sysc_reg_bit;
Toshiyuki Ogasaharac2dfd8e2021-07-12 19:13:17 +0900195 uint32_t lsi_product;
196 uint32_t lsi_cut;
Jorge Ramirez-Ortiz5ff5eee2018-09-23 09:41:10 +0200197
198 c = rcar_pwrc_get_mpidr_cluster(mpidr);
199 reg_cpumcr = IS_CA57(c) ? RCAR_CA57CPUCMCR : RCAR_CA53CPUCMCR;
200 sysc_reg_bit = IS_CA57(c) ? BIT_CA57_SCU : BIT_CA53_SCU;
201 reg_pwron = IS_CA57(c) ? RCAR_PWRONCR5 : RCAR_PWRONCR3;
202 reg_pwrer = IS_CA57(c) ? RCAR_PWRER5 : RCAR_PWRER3;
203 reg_pwrsr = IS_CA57(c) ? RCAR_PWRSR5 : RCAR_PWRSR3;
204
205 if ((mmio_read_32(reg_pwrsr) & STATUS_PWRDOWN) == 0)
206 return;
207
208 if (mmio_read_32(reg_cpumcr) != 0)
209 mmio_write_32(reg_cpumcr, 0);
210
Toshiyuki Ogasaharac2dfd8e2021-07-12 19:13:17 +0900211 lsi_product = mmio_read_32((uintptr_t)RCAR_PRR);
212 lsi_cut = lsi_product & PRR_CUT_MASK;
213 lsi_product &= PRR_PRODUCT_MASK;
214
215 if ((lsi_product == PRR_PRODUCT_M3 && lsi_cut >= PRR_PRODUCT_30) ||
216 lsi_product == PRR_PRODUCT_H3 ||
217 lsi_product == PRR_PRODUCT_M3N ||
218 lsi_product == PRR_PRODUCT_E3) {
219 mmio_setbits_32(RCAR_SYSCEXTMASK, SYSCEXTMASK_EXTMSK0);
220 }
221
Jorge Ramirez-Ortiz5ff5eee2018-09-23 09:41:10 +0200222 mmio_setbits_32(RCAR_SYSCIER, sysc_reg_bit);
223 mmio_setbits_32(RCAR_SYSCIMR, sysc_reg_bit);
224
225 do {
226 while ((mmio_read_32(RCAR_SYSCSR) & REQ_RESUME) == 0)
227 ;
228 mmio_write_32(reg_pwron, 1);
229 } while (mmio_read_32(reg_pwrer) & 1);
230
231 while ((mmio_read_32(RCAR_SYSCISR) & sysc_reg_bit) == 0)
232 ;
Toshiyuki Ogasahara4198e752021-07-12 19:14:11 +0900233 mmio_write_32(RCAR_SYSCISCR, sysc_reg_bit);
Toshiyuki Ogasaharac2dfd8e2021-07-12 19:13:17 +0900234
235 if ((lsi_product == PRR_PRODUCT_M3 && lsi_cut >= PRR_PRODUCT_30) ||
236 lsi_product == PRR_PRODUCT_H3 ||
237 lsi_product == PRR_PRODUCT_M3N ||
238 lsi_product == PRR_PRODUCT_E3) {
239 mmio_clrbits_32(RCAR_SYSCEXTMASK, SYSCEXTMASK_EXTMSK0);
240 }
241
Jorge Ramirez-Ortiz5ff5eee2018-09-23 09:41:10 +0200242 while ((mmio_read_32(reg_pwrsr) & STATUS_PWRUP) == 0)
243 ;
244}
245
Takuya Sakata59197472021-11-02 20:30:39 +0900246void rcar_pwrc_cpuon(u_register_t mpidr)
Jorge Ramirez-Ortiz5ff5eee2018-09-23 09:41:10 +0200247{
248 uint32_t res_data, on_data;
249 uintptr_t res_reg, on_reg;
250 uint32_t limit, c;
251 uint64_t cpu;
252
253 rcar_lock_get();
254
255 c = rcar_pwrc_get_mpidr_cluster(mpidr);
256 res_reg = IS_CA53(c) ? RCAR_CA53RESCNT : RCAR_CA57RESCNT;
257 on_reg = IS_CA53(c) ? RCAR_CA53WUPCR : RCAR_CA57WUPCR;
258 limit = IS_CA53(c) ? 0x5A5A0000 : 0xA5A50000;
259
260 res_data = mmio_read_32(res_reg) | limit;
261 scu_power_up(mpidr);
262 cpu = mpidr & MPIDR_CPU_MASK;
263 on_data = 1 << cpu;
Toshiyuki Ogasahara77626582020-11-30 20:39:21 +0900264 mmio_write_32(CPG_CPGWPR, ~on_data);
Jorge Ramirez-Ortiz5ff5eee2018-09-23 09:41:10 +0200265 mmio_write_32(on_reg, on_data);
266 mmio_write_32(res_reg, res_data & (~(1 << (3 - cpu))));
267
268 rcar_lock_release();
269}
270
Takuya Sakata59197472021-11-02 20:30:39 +0900271void rcar_pwrc_cpuoff(u_register_t mpidr)
Jorge Ramirez-Ortiz5ff5eee2018-09-23 09:41:10 +0200272{
273 uint32_t c;
274 uintptr_t reg;
275 uint64_t cpu;
276
277 rcar_lock_get();
278
279 cpu = mpidr & MPIDR_CPU_MASK;
280 c = rcar_pwrc_get_mpidr_cluster(mpidr);
281 reg = IS_CA53(c) ? RCAR_CA53CPU0CR : RCAR_CA57CPU0CR;
282
283 if (read_mpidr_el1() != mpidr)
284 panic();
285
Toshiyuki Ogasahara77626582020-11-30 20:39:21 +0900286 mmio_write_32(CPG_CPGWPR, ~CPU_PWR_OFF);
Jorge Ramirez-Ortiz5ff5eee2018-09-23 09:41:10 +0200287 mmio_write_32(reg + cpu * 0x0010, CPU_PWR_OFF);
288
289 rcar_lock_release();
290}
291
Takuya Sakata59197472021-11-02 20:30:39 +0900292void rcar_pwrc_enable_interrupt_wakeup(u_register_t mpidr)
Jorge Ramirez-Ortiz5ff5eee2018-09-23 09:41:10 +0200293{
294 uint32_t c, shift_irq, shift_fiq;
295 uintptr_t reg;
296 uint64_t cpu;
297
298 rcar_lock_get();
299
300 cpu = mpidr & MPIDR_CPU_MASK;
301 c = rcar_pwrc_get_mpidr_cluster(mpidr);
302 reg = IS_CA53(c) ? RCAR_WUPMSKCA53 : RCAR_WUPMSKCA57;
303
304 shift_irq = WUP_IRQ_SHIFT + cpu;
305 shift_fiq = WUP_FIQ_SHIFT + cpu;
306
Takuya Sakata0e478452021-11-02 20:25:53 +0900307 mmio_clrbits_32(reg, ((uint32_t) 1 << shift_irq) |
308 ((uint32_t) 1 << shift_fiq));
Jorge Ramirez-Ortiz5ff5eee2018-09-23 09:41:10 +0200309 rcar_lock_release();
310}
311
Takuya Sakata59197472021-11-02 20:30:39 +0900312void rcar_pwrc_disable_interrupt_wakeup(u_register_t mpidr)
Jorge Ramirez-Ortiz5ff5eee2018-09-23 09:41:10 +0200313{
314 uint32_t c, shift_irq, shift_fiq;
315 uintptr_t reg;
316 uint64_t cpu;
317
318 rcar_lock_get();
319
320 cpu = mpidr & MPIDR_CPU_MASK;
321 c = rcar_pwrc_get_mpidr_cluster(mpidr);
322 reg = IS_CA53(c) ? RCAR_WUPMSKCA53 : RCAR_WUPMSKCA57;
323
324 shift_irq = WUP_IRQ_SHIFT + cpu;
325 shift_fiq = WUP_FIQ_SHIFT + cpu;
326
Takuya Sakata0e478452021-11-02 20:25:53 +0900327 mmio_setbits_32(reg, ((uint32_t) 1 << shift_irq) |
Jorge Ramirez-Ortiz5ff5eee2018-09-23 09:41:10 +0200328 ((uint32_t) 1 << shift_fiq));
329 rcar_lock_release();
330}
331
Takuya Sakata0034f602021-11-02 20:30:02 +0900332void rcar_pwrc_all_disable_interrupt_wakeup(void)
333{
Takuya Sakata59197472021-11-02 20:30:39 +0900334 uint32_t cpu_num;
335 u_register_t cl, cpu, mpidr;
Takuya Sakata0034f602021-11-02 20:30:02 +0900336
337 const uint32_t cluster[PLATFORM_CLUSTER_COUNT] = {
338 RCAR_CLUSTER_CA57,
339 RCAR_CLUSTER_CA53
340 };
341
342 for (cl = 0; cl < PLATFORM_CLUSTER_COUNT; cl++) {
343 cpu_num = rcar_pwrc_get_cpu_num(cluster[cl]);
344 for (cpu = 0; cpu < cpu_num; cpu++) {
Takuya Sakata59197472021-11-02 20:30:39 +0900345 mpidr = ((cl << MPIDR_AFFINITY_BITS) | cpu);
346 if (mpidr == rcar_boot_mpidr) {
Takuya Sakata0034f602021-11-02 20:30:02 +0900347 rcar_pwrc_enable_interrupt_wakeup(mpidr);
348 } else {
349 rcar_pwrc_disable_interrupt_wakeup(mpidr);
350 }
351 }
352 }
353}
354
Takuya Sakata59197472021-11-02 20:30:39 +0900355void rcar_pwrc_clusteroff(u_register_t mpidr)
Jorge Ramirez-Ortiz5ff5eee2018-09-23 09:41:10 +0200356{
357 uint32_t c, product, cut, reg;
358 uintptr_t dst;
359
360 rcar_lock_get();
361
362 reg = mmio_read_32(RCAR_PRR);
Marek Vasut9cadc782019-08-06 19:13:22 +0200363 product = reg & PRR_PRODUCT_MASK;
364 cut = reg & PRR_CUT_MASK;
Jorge Ramirez-Ortiz5ff5eee2018-09-23 09:41:10 +0200365
366 c = rcar_pwrc_get_mpidr_cluster(mpidr);
367 dst = IS_CA53(c) ? RCAR_CA53CPUCMCR : RCAR_CA57CPUCMCR;
368
Biju Dasb99eb052020-12-13 20:24:19 +0000369 if (product == PRR_PRODUCT_M3 && cut < PRR_PRODUCT_30) {
Jorge Ramirez-Ortiz5ff5eee2018-09-23 09:41:10 +0200370 goto done;
Biju Dasb99eb052020-12-13 20:24:19 +0000371 }
Jorge Ramirez-Ortiz5ff5eee2018-09-23 09:41:10 +0200372
Biju Dasb99eb052020-12-13 20:24:19 +0000373 if (product == PRR_PRODUCT_H3 && cut <= PRR_PRODUCT_20) {
Jorge Ramirez-Ortiz5ff5eee2018-09-23 09:41:10 +0200374 goto done;
Biju Dasb99eb052020-12-13 20:24:19 +0000375 }
Jorge Ramirez-Ortiz5ff5eee2018-09-23 09:41:10 +0200376
377 /* all of the CPUs in the cluster is in the CoreStandby mode */
378 mmio_write_32(dst, MODE_L2_DOWN);
379done:
380 rcar_lock_release();
381}
382
Toshiyuki Ogasaharab67a8ca2019-03-22 16:14:00 +0900383static uint64_t rcar_pwrc_saved_cntpct_el0;
384static uint32_t rcar_pwrc_saved_cntfid;
385
386#if RCAR_SYSTEM_SUSPEND
387static void rcar_pwrc_save_timer_state(void)
388{
389 rcar_pwrc_saved_cntpct_el0 = read_cntpct_el0();
390
391 rcar_pwrc_saved_cntfid =
392 mmio_read_32((uintptr_t)(RCAR_CNTC_BASE + RCAR_CNTFID_OFF));
393}
Biju Dasb99eb052020-12-13 20:24:19 +0000394#endif /* RCAR_SYSTEM_SUSPEND */
Toshiyuki Ogasaharab67a8ca2019-03-22 16:14:00 +0900395
396void rcar_pwrc_restore_timer_state(void)
397{
398 /* Stop timer before restoring counter value */
399 mmio_write_32((uintptr_t)(RCAR_CNTC_BASE + RCAR_CNTCR_OFF), 0U);
400
401 mmio_write_32((uintptr_t)(RCAR_CNTC_BASE + RCAR_CNTCVL_OFF),
402 (uint32_t)(rcar_pwrc_saved_cntpct_el0 & 0xFFFFFFFFU));
403 mmio_write_32((uintptr_t)(RCAR_CNTC_BASE + RCAR_CNTCVU_OFF),
404 (uint32_t)(rcar_pwrc_saved_cntpct_el0 >> 32U));
405
406 mmio_write_32((uintptr_t)(RCAR_CNTC_BASE + RCAR_CNTFID_OFF),
407 rcar_pwrc_saved_cntfid);
408
409 /* Start generic timer back */
410 write_cntfrq_el0((u_register_t)plat_get_syscnt_freq2());
411
412 mmio_write_32((uintptr_t)(RCAR_CNTC_BASE + RCAR_CNTCR_OFF),
413 (RCAR_CNTCR_FCREQ(0U) | RCAR_CNTCR_EN));
414}
415
Jorge Ramirez-Ortiz5ff5eee2018-09-23 09:41:10 +0200416#if !PMIC_ROHM_BD9571
417void rcar_pwrc_system_reset(void)
418{
419 mmio_write_32(RCAR_SRESCR, 0x5AA50000U | BIT_SOFTRESET);
420}
421#endif /* PMIC_ROHM_BD9571 */
422
Biju Dasb99eb052020-12-13 20:24:19 +0000423#define RST_CA53_CPU0_BARH (0xE6160080U)
424#define RST_CA53_CPU0_BARL (0xE6160084U)
425#define RST_CA57_CPU0_BARH (0xE61600C0U)
426#define RST_CA57_CPU0_BARL (0xE61600C4U)
Jorge Ramirez-Ortiz5ff5eee2018-09-23 09:41:10 +0200427
428void rcar_pwrc_setup(void)
429{
430 uintptr_t rst_barh;
431 uintptr_t rst_barl;
432 uint32_t i, j;
433 uint64_t reset = (uint64_t) (&plat_secondary_reset) & 0xFFFFFFFF;
434
435 const uint32_t cluster[PLATFORM_CLUSTER_COUNT] = {
436 RCAR_CLUSTER_CA53,
437 RCAR_CLUSTER_CA57
438 };
439 const uintptr_t reg_barh[PLATFORM_CLUSTER_COUNT] = {
440 RST_CA53_CPU0_BARH,
441 RST_CA57_CPU0_BARH
442 };
443 const uintptr_t reg_barl[PLATFORM_CLUSTER_COUNT] = {
444 RST_CA53_CPU0_BARL,
445 RST_CA57_CPU0_BARL
446 };
447
448 for (i = 0; i < PLATFORM_CLUSTER_COUNT; i++) {
449 rst_barh = reg_barh[i];
450 rst_barl = reg_barl[i];
451 for (j = 0; j < rcar_pwrc_get_cpu_num(cluster[i]); j++) {
452 mmio_write_32(rst_barh, 0);
453 mmio_write_32(rst_barl, (uint32_t) reset);
454 rst_barh += 0x10;
455 rst_barl += 0x10;
456 }
457 }
458
459 rcar_lock_init();
460}
461
462#if RCAR_SYSTEM_SUSPEND
463#define DBCAM_FLUSH(__bit) \
464do { \
465 ; \
466} while (!(mmio_read_32(DBSC4_REG_DBCAM##__bit##STAT0) & DBSC4_BIT_DBCAMxSTAT0))
467
468
469static void __attribute__ ((section(".system_ram")))
470 rcar_pwrc_set_self_refresh(void)
471{
472 uint32_t reg = mmio_read_32(RCAR_PRR);
473 uint32_t cut, product;
474
Marek Vasut9cadc782019-08-06 19:13:22 +0200475 product = reg & PRR_PRODUCT_MASK;
476 cut = reg & PRR_CUT_MASK;
Jorge Ramirez-Ortiz5ff5eee2018-09-23 09:41:10 +0200477
Biju Dasb99eb052020-12-13 20:24:19 +0000478 if (product == PRR_PRODUCT_M3 && cut < PRR_PRODUCT_30) {
Jorge Ramirez-Ortiz5ff5eee2018-09-23 09:41:10 +0200479 goto self_refresh;
Biju Dasb99eb052020-12-13 20:24:19 +0000480 }
Jorge Ramirez-Ortiz5ff5eee2018-09-23 09:41:10 +0200481
Biju Dasb99eb052020-12-13 20:24:19 +0000482 if (product == PRR_PRODUCT_H3 && cut < PRR_PRODUCT_20) {
Jorge Ramirez-Ortiz5ff5eee2018-09-23 09:41:10 +0200483 goto self_refresh;
Biju Dasb99eb052020-12-13 20:24:19 +0000484 }
Jorge Ramirez-Ortiz5ff5eee2018-09-23 09:41:10 +0200485
486 mmio_write_32(DBSC4_REG_DBSYSCNT0, DBSC4_SET_DBSYSCNT0_WRITE_ENABLE);
487
488self_refresh:
489
Yoshifumi Hosoya4d61a122019-03-15 23:19:28 +0900490 /* DFI_PHYMSTR_ACK setting */
491 mmio_write_32(DBSC4_REG_DBDFIPMSTRCNF,
492 mmio_read_32(DBSC4_REG_DBDFIPMSTRCNF) &
493 (~DBSC4_BIT_DBDFIPMSTRCNF_PMSTREN));
494
Jorge Ramirez-Ortiz5ff5eee2018-09-23 09:41:10 +0200495 /* Set the Self-Refresh mode */
496 mmio_write_32(DBSC4_REG_DBACEN, 0);
497
Marek Vasut9cadc782019-08-06 19:13:22 +0200498 if (product == PRR_PRODUCT_H3 && cut < PRR_PRODUCT_20)
Marek Vasut4bc543c2018-12-28 20:15:33 +0100499 rcar_micro_delay(100);
Marek Vasut9cadc782019-08-06 19:13:22 +0200500 else if (product == PRR_PRODUCT_H3) {
Jorge Ramirez-Ortiz5ff5eee2018-09-23 09:41:10 +0200501 mmio_write_32(DBSC4_REG_DBCAM0CTRL0, 1);
502 DBCAM_FLUSH(0);
503 DBCAM_FLUSH(1);
504 DBCAM_FLUSH(2);
505 DBCAM_FLUSH(3);
506 mmio_write_32(DBSC4_REG_DBCAM0CTRL0, 0);
Marek Vasut9cadc782019-08-06 19:13:22 +0200507 } else if (product == PRR_PRODUCT_M3) {
Jorge Ramirez-Ortiz5ff5eee2018-09-23 09:41:10 +0200508 mmio_write_32(DBSC4_REG_DBCAM0CTRL0, 1);
509 DBCAM_FLUSH(0);
510 DBCAM_FLUSH(1);
511 mmio_write_32(DBSC4_REG_DBCAM0CTRL0, 0);
512 } else {
513 mmio_write_32(DBSC4_REG_DBCAM0CTRL0, 1);
514 DBCAM_FLUSH(0);
515 mmio_write_32(DBSC4_REG_DBCAM0CTRL0, 0);
516 }
517
518 /* Set the SDRAM calibration configuration register */
519 mmio_write_32(DBSC4_REG_DBCALCNF, 0);
520
521 reg = DBSC4_SET_DBCMD_OPC_PRE | DBSC4_SET_DBCMD_CH_ALL |
522 DBSC4_SET_DBCMD_RANK_ALL | DBSC4_SET_DBCMD_ARG_ALL;
523 mmio_write_32(DBSC4_REG_DBCMD, reg);
524 while (mmio_read_32(DBSC4_REG_DBWAIT))
525 ;
526
527 /* Self-Refresh entry command */
528 reg = DBSC4_SET_DBCMD_OPC_SR | DBSC4_SET_DBCMD_CH_ALL |
529 DBSC4_SET_DBCMD_RANK_ALL | DBSC4_SET_DBCMD_ARG_ENTER;
530 mmio_write_32(DBSC4_REG_DBCMD, reg);
531 while (mmio_read_32(DBSC4_REG_DBWAIT))
532 ;
533
534 /* Mode Register Write command. (ODT disabled) */
535 reg = DBSC4_SET_DBCMD_OPC_MRW | DBSC4_SET_DBCMD_CH_ALL |
536 DBSC4_SET_DBCMD_RANK_ALL | DBSC4_SET_DBCMD_ARG_MRW_ODTC;
537 mmio_write_32(DBSC4_REG_DBCMD, reg);
538 while (mmio_read_32(DBSC4_REG_DBWAIT))
539 ;
540
541 /* Power Down entry command */
542 reg = DBSC4_SET_DBCMD_OPC_PD | DBSC4_SET_DBCMD_CH_ALL |
543 DBSC4_SET_DBCMD_RANK_ALL | DBSC4_SET_DBCMD_ARG_ENTER;
544 mmio_write_32(DBSC4_REG_DBCMD, reg);
545 while (mmio_read_32(DBSC4_REG_DBWAIT))
546 ;
547
548 /* Set the auto-refresh enable register */
549 mmio_write_32(DBSC4_REG_DBRFEN, 0U);
Marek Vasut4bc543c2018-12-28 20:15:33 +0100550 rcar_micro_delay(1U);
Jorge Ramirez-Ortiz5ff5eee2018-09-23 09:41:10 +0200551
Marek Vasut9cadc782019-08-06 19:13:22 +0200552 if (product == PRR_PRODUCT_M3 && cut < PRR_PRODUCT_30)
Jorge Ramirez-Ortiz5ff5eee2018-09-23 09:41:10 +0200553 return;
554
Marek Vasut9cadc782019-08-06 19:13:22 +0200555 if (product == PRR_PRODUCT_H3 && cut < PRR_PRODUCT_20)
Jorge Ramirez-Ortiz5ff5eee2018-09-23 09:41:10 +0200556 return;
557
558 mmio_write_32(DBSC4_REG_DBSYSCNT0, DBSC4_SET_DBSYSCNT0_WRITE_DISABLE);
559}
560
561static void __attribute__ ((section(".system_ram")))
Biju Dasb99eb052020-12-13 20:24:19 +0000562rcar_pwrc_set_self_refresh_e3(void)
Jorge Ramirez-Ortiz5ff5eee2018-09-23 09:41:10 +0200563{
564 uint32_t ddr_md;
565 uint32_t reg;
566
567 ddr_md = (mmio_read_32(RST_MODEMR) >> 19) & RST_MODEMR_BIT0;
568
569 /* Write enable */
570 mmio_write_32(DBSC4_REG_DBSYSCNT0, DBSC4_SET_DBSYSCNT0_WRITE_ENABLE);
571 mmio_write_32(DBSC4_REG_DBACEN, 0);
572 DBCAM_FLUSH(0);
573
574 reg = DBSC4_SET_DBCMD_OPC_PRE | DBSC4_SET_DBCMD_CH_ALL |
575 DBSC4_SET_DBCMD_RANK_ALL | DBSC4_SET_DBCMD_ARG_ALL;
576 mmio_write_32(DBSC4_REG_DBCMD, reg);
577 while (mmio_read_32(DBSC4_REG_DBWAIT))
578 ;
579
580 reg = DBSC4_SET_DBCMD_OPC_SR | DBSC4_SET_DBCMD_CH_ALL |
581 DBSC4_SET_DBCMD_RANK_ALL | DBSC4_SET_DBCMD_ARG_ENTER;
582 mmio_write_32(DBSC4_REG_DBCMD, reg);
583 while (mmio_read_32(DBSC4_REG_DBWAIT))
584 ;
585
Biju Dasb99eb052020-12-13 20:24:19 +0000586 /*
587 * Set the auto-refresh enable register
588 * Set the ARFEN bit to 0 in the DBRFEN
589 */
Jorge Ramirez-Ortiz5ff5eee2018-09-23 09:41:10 +0200590 mmio_write_32(DBSC4_REG_DBRFEN, 0);
591
592 mmio_write_32(DBSC4_REG_DBPDLK0, DBSC4_SET_DBPDLK0_PHY_ACCESS);
593
594 mmio_write_32(DBSC4_REG_DBPDRGA0, DBSC4_SET_DBPDRGA0_ACIOCR0);
595 mmio_write_32(DBSC4_REG_DBPDRGD0, DBSC4_SET_DBPDRGD0_ACIOCR0);
596
597 /* DDR_DXCCR */
598 mmio_write_32(DBSC4_REG_DBPDRGA0, DBSC4_SET_DBPDRGA0_DXCCR);
599 mmio_write_32(DBSC4_REG_DBPDRGD0, DBSC4_SET_DBPDRGD0_DXCCR);
600
601 /* DDR_PGCR1 */
602 mmio_write_32(DBSC4_REG_DBPDRGA0, DBSC4_SET_DBPDRGA0_PGCR1);
603 mmio_write_32(DBSC4_REG_DBPDRGD0, DBSC4_SET_DBPDRGD0_PGCR1);
604
605 /* DDR_ACIOCR1 */
606 mmio_write_32(DBSC4_REG_DBPDRGA0, DBSC4_SET_DBPDRGA0_ACIOCR1);
607 mmio_write_32(DBSC4_REG_DBPDRGD0, DBSC4_SET_DBPDRGD0_ACIOCR1);
608
609 /* DDR_ACIOCR3 */
610 mmio_write_32(DBSC4_REG_DBPDRGA0, DBSC4_SET_DBPDRGA0_ACIOCR3);
611 mmio_write_32(DBSC4_REG_DBPDRGD0, DBSC4_SET_DBPDRGD0_ACIOCR3);
612
613 /* DDR_ACIOCR5 */
614 mmio_write_32(DBSC4_REG_DBPDRGA0, DBSC4_SET_DBPDRGA0_ACIOCR5);
615 mmio_write_32(DBSC4_REG_DBPDRGD0, DBSC4_SET_DBPDRGD0_ACIOCR5);
616
617 /* DDR_DX0GCR2 */
618 mmio_write_32(DBSC4_REG_DBPDRGA0, DBSC4_SET_DBPDRGA0_DX0GCR2);
619 mmio_write_32(DBSC4_REG_DBPDRGD0, DBSC4_SET_DBPDRGD0_DX0GCR2);
620
621 /* DDR_DX1GCR2 */
622 mmio_write_32(DBSC4_REG_DBPDRGA0, DBSC4_SET_DBPDRGA0_DX1GCR2);
623 mmio_write_32(DBSC4_REG_DBPDRGD0, DBSC4_SET_DBPDRGD0_DX1GCR2);
624
625 /* DDR_DX2GCR2 */
626 mmio_write_32(DBSC4_REG_DBPDRGA0, DBSC4_SET_DBPDRGA0_DX2GCR2);
627 mmio_write_32(DBSC4_REG_DBPDRGD0, DBSC4_SET_DBPDRGD0_DX2GCR2);
628
629 /* DDR_DX3GCR2 */
630 mmio_write_32(DBSC4_REG_DBPDRGA0, DBSC4_SET_DBPDRGA0_DX3GCR2);
631 mmio_write_32(DBSC4_REG_DBPDRGD0, DBSC4_SET_DBPDRGD0_DX3GCR2);
632
633 /* DDR_ZQCR */
634 mmio_write_32(DBSC4_REG_DBPDRGA0, DBSC4_SET_DBPDRGA0_ZQCR);
635
636 mmio_write_32(DBSC4_REG_DBPDRGD0, ddr_md == 0 ?
637 DBSC4_SET_DBPDRGD0_ZQCR_MD19_0 :
638 DBSC4_SET_DBPDRGD0_ZQCR_MD19_1);
639
640 /* DDR_DX0GCR0 */
641 mmio_write_32(DBSC4_REG_DBPDRGA0, DBSC4_SET_DBPDRGA0_DX0GCR0);
642 mmio_write_32(DBSC4_REG_DBPDRGD0, DBSC4_SET_DBPDRGD0_DX0GCR0);
643
644 /* DDR_DX1GCR0 */
645 mmio_write_32(DBSC4_REG_DBPDRGA0, DBSC4_SET_DBPDRGA0_DX1GCR0);
646 mmio_write_32(DBSC4_REG_DBPDRGD0, DBSC4_SET_DBPDRGD0_DX1GCR0);
647
648 /* DDR_DX2GCR0 */
649 mmio_write_32(DBSC4_REG_DBPDRGA0, DBSC4_SET_DBPDRGA0_DX2GCR0);
650 mmio_write_32(DBSC4_REG_DBPDRGD0, DBSC4_SET_DBPDRGD0_DX2GCR0);
651
652 /* DDR_DX3GCR0 */
653 mmio_write_32(DBSC4_REG_DBPDRGA0, DBSC4_SET_DBPDRGA0_DX3GCR0);
654 mmio_write_32(DBSC4_REG_DBPDRGD0, DBSC4_SET_DBPDRGD0_DX3GCR0);
655
656 /* DDR_DX0GCR1 */
657 mmio_write_32(DBSC4_REG_DBPDRGA0, DBSC4_SET_DBPDRGA0_DX0GCR1);
658 mmio_write_32(DBSC4_REG_DBPDRGD0, DBSC4_SET_DBPDRGD0_DX0GCR1);
659
660 /* DDR_DX1GCR1 */
661 mmio_write_32(DBSC4_REG_DBPDRGA0, DBSC4_SET_DBPDRGA0_DX1GCR1);
662 mmio_write_32(DBSC4_REG_DBPDRGD0, DBSC4_SET_DBPDRGD0_DX1GCR1);
663
664 /* DDR_DX2GCR1 */
665 mmio_write_32(DBSC4_REG_DBPDRGA0, DBSC4_SET_DBPDRGA0_DX2GCR1);
666 mmio_write_32(DBSC4_REG_DBPDRGD0, DBSC4_SET_DBPDRGD0_DX2GCR1);
667
668 /* DDR_DX3GCR1 */
669 mmio_write_32(DBSC4_REG_DBPDRGA0, DBSC4_SET_DBPDRGA0_DX3GCR1);
670 mmio_write_32(DBSC4_REG_DBPDRGD0, DBSC4_SET_DBPDRGD0_DX3GCR1);
671
672 /* DDR_DX0GCR3 */
673 mmio_write_32(DBSC4_REG_DBPDRGA0, DBSC4_SET_DBPDRGA0_DX0GCR3);
674 mmio_write_32(DBSC4_REG_DBPDRGD0, DBSC4_SET_DBPDRGD0_DX0GCR3);
675
676 /* DDR_DX1GCR3 */
677 mmio_write_32(DBSC4_REG_DBPDRGA0, DBSC4_SET_DBPDRGA0_DX1GCR3);
678 mmio_write_32(DBSC4_REG_DBPDRGD0, DBSC4_SET_DBPDRGD0_DX1GCR3);
679
680 /* DDR_DX2GCR3 */
681 mmio_write_32(DBSC4_REG_DBPDRGA0, DBSC4_SET_DBPDRGA0_DX2GCR3);
682 mmio_write_32(DBSC4_REG_DBPDRGD0, DBSC4_SET_DBPDRGD0_DX2GCR3);
683
684 /* DDR_DX3GCR3 */
685 mmio_write_32(DBSC4_REG_DBPDRGA0, DBSC4_SET_DBPDRGA0_DX3GCR3);
686 mmio_write_32(DBSC4_REG_DBPDRGD0, DBSC4_SET_DBPDRGD0_DX3GCR3);
687
688 /* Write disable */
689 mmio_write_32(DBSC4_REG_DBSYSCNT0, DBSC4_SET_DBSYSCNT0_WRITE_DISABLE);
690}
691
692void __attribute__ ((section(".system_ram"))) __attribute__ ((noinline))
Biju Dasb99eb052020-12-13 20:24:19 +0000693rcar_pwrc_go_suspend_to_ram(void)
Jorge Ramirez-Ortiz5ff5eee2018-09-23 09:41:10 +0200694{
695#if PMIC_ROHM_BD9571
696 int32_t rc = -1, qllm = -1;
697 uint8_t mode;
698 uint32_t i;
699#endif
700 uint32_t reg, product;
701
702 reg = mmio_read_32(RCAR_PRR);
Marek Vasut9cadc782019-08-06 19:13:22 +0200703 product = reg & PRR_PRODUCT_MASK;
Jorge Ramirez-Ortiz5ff5eee2018-09-23 09:41:10 +0200704
Marek Vasut9cadc782019-08-06 19:13:22 +0200705 if (product != PRR_PRODUCT_E3)
Jorge Ramirez-Ortiz5ff5eee2018-09-23 09:41:10 +0200706 rcar_pwrc_set_self_refresh();
707 else
708 rcar_pwrc_set_self_refresh_e3();
709
710#if PMIC_ROHM_BD9571
711 /* Set QLLM Cnt Disable */
712 for (i = 0; (i < PMIC_RETRY_MAX) && (qllm != 0); i++)
713 qllm = rcar_iic_dvfs_send(PMIC, PMIC_QLLM_CNT, 0);
714
715 /* Set trigger of power down to PMIV */
716 for (i = 0; (i < PMIC_RETRY_MAX) && (rc != 0) && (qllm == 0); i++) {
717 rc = rcar_iic_dvfs_receive(PMIC, PMIC_BKUP_MODE_CNT, &mode);
718 if (rc == 0) {
719 mode |= BIT_BKUP_CTRL_OUT;
720 rc = rcar_iic_dvfs_send(PMIC, PMIC_BKUP_MODE_CNT, mode);
721 }
722 }
723#endif
724 wfi();
725
726 while (1)
727 ;
728}
729
730void rcar_pwrc_set_suspend_to_ram(void)
731{
732 uintptr_t jump = (uintptr_t) &rcar_pwrc_go_suspend_to_ram;
733 uintptr_t stack = (uintptr_t) (DEVICE_SRAM_STACK_BASE +
734 DEVICE_SRAM_STACK_SIZE);
735 uint32_t sctlr;
736
Toshiyuki Ogasaharab67a8ca2019-03-22 16:14:00 +0900737 rcar_pwrc_save_timer_state();
Jorge Ramirez-Ortiz5ff5eee2018-09-23 09:41:10 +0200738
739 /* disable MMU */
740 sctlr = (uint32_t) read_sctlr_el3();
741 sctlr &= (uint32_t) ~SCTLR_EL3_M_BIT;
742 write_sctlr_el3((uint64_t) sctlr);
743
744 rcar_pwrc_switch_stack(jump, stack, NULL);
745}
746
747void rcar_pwrc_init_suspend_to_ram(void)
748{
749#if PMIC_ROHM_BD9571
750 uint8_t mode;
Jorge Ramirez-Ortiz5ff5eee2018-09-23 09:41:10 +0200751
Jorge Ramirez-Ortiz5ff5eee2018-09-23 09:41:10 +0200752 if (rcar_iic_dvfs_receive(PMIC, PMIC_BKUP_MODE_CNT, &mode))
753 panic();
754
755 mode &= (uint8_t) (~BIT_BKUP_CTRL_OUT);
756 if (rcar_iic_dvfs_send(PMIC, PMIC_BKUP_MODE_CNT, mode))
757 panic();
758#endif
759}
760
761void rcar_pwrc_suspend_to_ram(void)
762{
763#if RCAR_SYSTEM_RESET_KEEPON_DDR
764 int32_t error;
765
Jorge Ramirez-Ortiz5ff5eee2018-09-23 09:41:10 +0200766 error = rcar_iic_dvfs_send(PMIC, REG_KEEP10, 0);
767 if (error) {
Biju Dasb99eb052020-12-13 20:24:19 +0000768 ERROR("Failed send KEEP10 init ret=%d\n", error);
Jorge Ramirez-Ortiz5ff5eee2018-09-23 09:41:10 +0200769 return;
770 }
771#endif
772 rcar_pwrc_set_suspend_to_ram();
773}
774#endif
775
776void rcar_pwrc_code_copy_to_system_ram(void)
777{
778 int ret __attribute__ ((unused)); /* in assert */
779 uint32_t attr;
780 struct device_sram_t {
781 uintptr_t base;
782 size_t len;
783 } sram = {
784 .base = (uintptr_t) DEVICE_SRAM_BASE,
785 .len = DEVICE_SRAM_SIZE,
786 };
787 struct ddr_code_t {
788 void *base;
789 size_t len;
790 } code = {
791 .base = (void *) SRAM_COPY_START,
792 .len = SYSTEM_RAM_END - SYSTEM_RAM_START,
793 };
794
795 attr = MT_MEMORY | MT_RW | MT_SECURE | MT_EXECUTE_NEVER;
796 ret = xlat_change_mem_attributes(sram.base, sram.len, attr);
797 assert(ret == 0);
798
799 memcpy((void *)sram.base, code.base, code.len);
800 flush_dcache_range((uint64_t) sram.base, code.len);
801
Toshiyuki Ogasaharacb4e82e2021-07-12 19:04:01 +0900802 attr = MT_MEMORY | MT_RO | MT_SECURE | MT_EXECUTE;
803 ret = xlat_change_mem_attributes(sram.base, sram.len, attr);
804 assert(ret == 0);
805
Jorge Ramirez-Ortiz5ff5eee2018-09-23 09:41:10 +0200806 /* Invalidate instruction cache */
807 plat_invalidate_icache();
808 dsb();
809 isb();
Jorge Ramirez-Ortiz5ff5eee2018-09-23 09:41:10 +0200810}
811
812uint32_t rcar_pwrc_get_cluster(void)
813{
814 uint32_t reg;
815
816 reg = mmio_read_32(RCAR_PRR);
817
Justin Chadwelle454beb2019-07-03 14:11:06 +0100818 if (reg & (1U << (STATE_CA53_CPU + RCAR_CA53CPU_NUM_MAX)))
Jorge Ramirez-Ortiz5ff5eee2018-09-23 09:41:10 +0200819 return RCAR_CLUSTER_CA57;
820
Justin Chadwelle454beb2019-07-03 14:11:06 +0100821 if (reg & (1U << (STATE_CA57_CPU + RCAR_CA57CPU_NUM_MAX)))
Jorge Ramirez-Ortiz5ff5eee2018-09-23 09:41:10 +0200822 return RCAR_CLUSTER_CA53;
823
824 return RCAR_CLUSTER_A53A57;
825}
826
Takuya Sakata59197472021-11-02 20:30:39 +0900827uint32_t rcar_pwrc_get_mpidr_cluster(u_register_t mpidr)
Jorge Ramirez-Ortiz5ff5eee2018-09-23 09:41:10 +0200828{
829 uint32_t c = rcar_pwrc_get_cluster();
830
831 if (IS_A53A57(c)) {
832 if (mpidr & MPIDR_CLUSTER_MASK)
833 return RCAR_CLUSTER_CA53;
834
835 return RCAR_CLUSTER_CA57;
836 }
837
838 return c;
839}
840
Marek Vasut2e1a49e2019-01-05 14:13:23 +0100841#if RCAR_LSI == RCAR_D3
Jorge Ramirez-Ortiz5ff5eee2018-09-23 09:41:10 +0200842uint32_t rcar_pwrc_get_cpu_num(uint32_t c)
843{
Marek Vasut2e1a49e2019-01-05 14:13:23 +0100844 return 1;
845}
846#else
847uint32_t rcar_pwrc_get_cpu_num(uint32_t c)
848{
Jorge Ramirez-Ortiz5ff5eee2018-09-23 09:41:10 +0200849 uint32_t reg = mmio_read_32(RCAR_PRR);
850 uint32_t count = 0, i;
851
852 if (IS_A53A57(c) || IS_CA53(c)) {
853 if (reg & (1 << (STATE_CA53_CPU + RCAR_CA53CPU_NUM_MAX)))
854 goto count_ca57;
855
856 for (i = 0; i < RCAR_CA53CPU_NUM_MAX; i++) {
857 if (reg & (1 << (STATE_CA53_CPU + i)))
858 continue;
859 count++;
860 }
861 }
862
863count_ca57:
864 if (IS_A53A57(c) || IS_CA57(c)) {
Justin Chadwelle454beb2019-07-03 14:11:06 +0100865 if (reg & (1U << (STATE_CA57_CPU + RCAR_CA57CPU_NUM_MAX)))
Jorge Ramirez-Ortiz5ff5eee2018-09-23 09:41:10 +0200866 goto done;
867
868 for (i = 0; i < RCAR_CA57CPU_NUM_MAX; i++) {
869 if (reg & (1 << (STATE_CA57_CPU + i)))
870 continue;
871 count++;
872 }
873 }
874
875done:
876 return count;
877}
Marek Vasut2e1a49e2019-01-05 14:13:23 +0100878#endif
Marek Vasut122555f2019-01-05 16:21:14 +0100879
Takuya Sakata59197472021-11-02 20:30:39 +0900880int32_t rcar_pwrc_cpu_on_check(u_register_t mpidr)
Marek Vasut122555f2019-01-05 16:21:14 +0100881{
882 uint64_t i;
883 uint64_t j;
884 uint64_t cpu_count;
885 uintptr_t reg_PSTR;
886 uint32_t status;
887 uint64_t my_cpu;
888 int32_t rtn;
889 uint32_t my_cluster_type;
Marek Vasut122555f2019-01-05 16:21:14 +0100890 const uint32_t cluster_type[PLATFORM_CLUSTER_COUNT] = {
891 RCAR_CLUSTER_CA53,
892 RCAR_CLUSTER_CA57
893 };
894 const uintptr_t registerPSTR[PLATFORM_CLUSTER_COUNT] = {
895 RCAR_CA53PSTR,
896 RCAR_CA57PSTR
897 };
898
899 my_cluster_type = rcar_pwrc_get_cluster();
900
901 rtn = 0;
902 my_cpu = mpidr & ((uint64_t)(MPIDR_CPU_MASK));
903 for (i = 0U; i < ((uint64_t)(PLATFORM_CLUSTER_COUNT)); i++) {
904 cpu_count = rcar_pwrc_get_cpu_num(cluster_type[i]);
905 reg_PSTR = registerPSTR[i];
906 for (j = 0U; j < cpu_count; j++) {
907 if ((my_cluster_type != cluster_type[i]) || (my_cpu != j)) {
908 status = mmio_read_32(reg_PSTR) >> (j * 4U);
909 if ((status & 0x00000003U) == 0U) {
910 rtn--;
911 }
912 }
913 }
914 }
Marek Vasut122555f2019-01-05 16:21:14 +0100915
Biju Dasb99eb052020-12-13 20:24:19 +0000916 return rtn;
Marek Vasut122555f2019-01-05 16:21:14 +0100917}