blob: 6dfc0b69f4adc5e642764ed4ec13ea5d6ac42e5a [file] [log] [blame]
Jorge Ramirez-Ortiz5ff5eee2018-09-23 09:41:10 +02001/*
2 * Copyright (c) 2015-2018, Renesas Electronics Corporation. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#include <arch.h>
8#include <arch_helpers.h>
9#include <assert.h>
10#include <bakery_lock.h>
11#include <debug.h>
12#include <mmio.h>
13#include <string.h>
14#include <xlat_tables_v2.h>
15#include "iic_dvfs.h"
16#include "rcar_def.h"
17#include "rcar_private.h"
18#include "pwrc.h"
19
20/*
21 * Someday there will be a generic power controller api. At the moment each
22 * platform has its own pwrc so just exporting functions should be acceptable.
23 */
24RCAR_INSTANTIATE_LOCK
25
26#define WUP_IRQ_SHIFT (0U)
27#define WUP_FIQ_SHIFT (8U)
28#define WUP_CSD_SHIFT (16U)
29#define BIT_SOFTRESET (1U<<15)
30#define BIT_CA53_SCU (1U<<21)
31#define BIT_CA57_SCU (1U<<12)
32#define REQ_RESUME (1U<<1)
33#define REQ_OFF (1U<<0)
34#define STATUS_PWRUP (1U<<4)
35#define STATUS_PWRDOWN (1U<<0)
36#define STATE_CA57_CPU (27U)
37#define STATE_CA53_CPU (22U)
38#define MODE_L2_DOWN (0x00000002U)
39#define CPU_PWR_OFF (0x00000003U)
40#define RCAR_PSTR_MASK (0x00000003U)
41#define ST_ALL_STANDBY (0x00003333U)
42/* Suspend to ram */
43#define DBSC4_REG_BASE (0xE6790000U)
44#define DBSC4_REG_DBSYSCNT0 (DBSC4_REG_BASE + 0x0100U)
45#define DBSC4_REG_DBACEN (DBSC4_REG_BASE + 0x0200U)
46#define DBSC4_REG_DBCMD (DBSC4_REG_BASE + 0x0208U)
47#define DBSC4_REG_DBRFEN (DBSC4_REG_BASE + 0x0204U)
48#define DBSC4_REG_DBWAIT (DBSC4_REG_BASE + 0x0210U)
49#define DBSC4_REG_DBCALCNF (DBSC4_REG_BASE + 0x0424U)
50#define DBSC4_REG_DBPDLK0 (DBSC4_REG_BASE + 0x0620U)
51#define DBSC4_REG_DBPDRGA0 (DBSC4_REG_BASE + 0x0624U)
52#define DBSC4_REG_DBPDRGD0 (DBSC4_REG_BASE + 0x0628U)
53#define DBSC4_REG_DBCAM0CTRL0 (DBSC4_REG_BASE + 0x0940U)
54#define DBSC4_REG_DBCAM0STAT0 (DBSC4_REG_BASE + 0x0980U)
55#define DBSC4_REG_DBCAM1STAT0 (DBSC4_REG_BASE + 0x0990U)
56#define DBSC4_REG_DBCAM2STAT0 (DBSC4_REG_BASE + 0x09A0U)
57#define DBSC4_REG_DBCAM3STAT0 (DBSC4_REG_BASE + 0x09B0U)
58#define DBSC4_BIT_DBACEN_ACCEN ((uint32_t)(1U << 0))
59#define DBSC4_BIT_DBRFEN_ARFEN ((uint32_t)(1U << 0))
60#define DBSC4_BIT_DBCAMxSTAT0 (0x00000001U)
61#define DBSC4_SET_DBCMD_OPC_PRE (0x04000000U)
62#define DBSC4_SET_DBCMD_OPC_SR (0x0A000000U)
63#define DBSC4_SET_DBCMD_OPC_PD (0x08000000U)
64#define DBSC4_SET_DBCMD_OPC_MRW (0x0E000000U)
65#define DBSC4_SET_DBCMD_CH_ALL (0x00800000U)
66#define DBSC4_SET_DBCMD_RANK_ALL (0x00040000U)
67#define DBSC4_SET_DBCMD_ARG_ALL (0x00000010U)
68#define DBSC4_SET_DBCMD_ARG_ENTER (0x00000000U)
69#define DBSC4_SET_DBCMD_ARG_MRW_ODTC (0x00000B00U)
70#define DBSC4_SET_DBSYSCNT0_WRITE_ENABLE (0x00001234U)
71#define DBSC4_SET_DBSYSCNT0_WRITE_DISABLE (0x00000000U)
72#define DBSC4_SET_DBPDLK0_PHY_ACCESS (0x0000A55AU)
73#define DBSC4_SET_DBPDRGA0_ACIOCR0 (0x0000001AU)
74#define DBSC4_SET_DBPDRGD0_ACIOCR0 (0x33C03C11U)
75#define DBSC4_SET_DBPDRGA0_DXCCR (0x00000020U)
76#define DBSC4_SET_DBPDRGD0_DXCCR (0x00181006U)
77#define DBSC4_SET_DBPDRGA0_PGCR1 (0x00000003U)
78#define DBSC4_SET_DBPDRGD0_PGCR1 (0x0380C600U)
79#define DBSC4_SET_DBPDRGA0_ACIOCR1 (0x0000001BU)
80#define DBSC4_SET_DBPDRGD0_ACIOCR1 (0xAAAAAAAAU)
81#define DBSC4_SET_DBPDRGA0_ACIOCR3 (0x0000001DU)
82#define DBSC4_SET_DBPDRGD0_ACIOCR3 (0xAAAAAAAAU)
83#define DBSC4_SET_DBPDRGA0_ACIOCR5 (0x0000001FU)
84#define DBSC4_SET_DBPDRGD0_ACIOCR5 (0x000000AAU)
85#define DBSC4_SET_DBPDRGA0_DX0GCR2 (0x000000A2U)
86#define DBSC4_SET_DBPDRGD0_DX0GCR2 (0xAAAA0000U)
87#define DBSC4_SET_DBPDRGA0_DX1GCR2 (0x000000C2U)
88#define DBSC4_SET_DBPDRGD0_DX1GCR2 (0xAAAA0000U)
89#define DBSC4_SET_DBPDRGA0_DX2GCR2 (0x000000E2U)
90#define DBSC4_SET_DBPDRGD0_DX2GCR2 (0xAAAA0000U)
91#define DBSC4_SET_DBPDRGA0_DX3GCR2 (0x00000102U)
92#define DBSC4_SET_DBPDRGD0_DX3GCR2 (0xAAAA0000U)
93#define DBSC4_SET_DBPDRGA0_ZQCR (0x00000090U)
94#define DBSC4_SET_DBPDRGD0_ZQCR_MD19_0 (0x04058904U)
95#define DBSC4_SET_DBPDRGD0_ZQCR_MD19_1 (0x04058A04U)
96#define DBSC4_SET_DBPDRGA0_DX0GCR0 (0x000000A0U)
97#define DBSC4_SET_DBPDRGD0_DX0GCR0 (0x7C0002E5U)
98#define DBSC4_SET_DBPDRGA0_DX1GCR0 (0x000000C0U)
99#define DBSC4_SET_DBPDRGD0_DX1GCR0 (0x7C0002E5U)
100#define DBSC4_SET_DBPDRGA0_DX2GCR0 (0x000000E0U)
101#define DBSC4_SET_DBPDRGD0_DX2GCR0 (0x7C0002E5U)
102#define DBSC4_SET_DBPDRGA0_DX3GCR0 (0x00000100U)
103#define DBSC4_SET_DBPDRGD0_DX3GCR0 (0x7C0002E5U)
104#define DBSC4_SET_DBPDRGA0_DX0GCR1 (0x000000A1U)
105#define DBSC4_SET_DBPDRGD0_DX0GCR1 (0x55550000U)
106#define DBSC4_SET_DBPDRGA0_DX1GCR1 (0x000000C1U)
107#define DBSC4_SET_DBPDRGD0_DX1GCR1 (0x55550000U)
108#define DBSC4_SET_DBPDRGA0_DX2GCR1 (0x000000E1U)
109#define DBSC4_SET_DBPDRGD0_DX2GCR1 (0x55550000U)
110#define DBSC4_SET_DBPDRGA0_DX3GCR1 (0x00000101U)
111#define DBSC4_SET_DBPDRGD0_DX3GCR1 (0x55550000U)
112#define DBSC4_SET_DBPDRGA0_DX0GCR3 (0x000000A3U)
113#define DBSC4_SET_DBPDRGD0_DX0GCR3 (0x00008484U)
114#define DBSC4_SET_DBPDRGA0_DX1GCR3 (0x000000C3U)
115#define DBSC4_SET_DBPDRGD0_DX1GCR3 (0x00008484U)
116#define DBSC4_SET_DBPDRGA0_DX2GCR3 (0x000000E3U)
117#define DBSC4_SET_DBPDRGD0_DX2GCR3 (0x00008484U)
118#define DBSC4_SET_DBPDRGA0_DX3GCR3 (0x00000103U)
119#define DBSC4_SET_DBPDRGD0_DX3GCR3 (0x00008484U)
120#define RST_BASE (0xE6160000U)
121#define RST_MODEMR (RST_BASE + 0x0060U)
122#define RST_MODEMR_BIT0 (0x00000001U)
123#define RCAR_CONV_MICROSEC (1000000U)
124
125#if PMIC_ROHM_BD9571
126#define BIT_BKUP_CTRL_OUT ((uint8_t)(1U << 4))
127#define PMIC_BKUP_MODE_CNT (0x20U)
128#define PMIC_QLLM_CNT (0x27U)
129#define PMIC_RETRY_MAX (100U)
130#endif
131#define SCTLR_EL3_M_BIT ((uint32_t)1U << 0)
132#define RCAR_CA53CPU_NUM_MAX (4U)
133#define RCAR_CA57CPU_NUM_MAX (4U)
134#define IS_A53A57(c) ((c) == RCAR_CLUSTER_A53A57)
135#define IS_CA57(c) ((c) == RCAR_CLUSTER_CA57)
136#define IS_CA53(c) ((c) == RCAR_CLUSTER_CA53)
137
138#ifndef __ASSEMBLY__
139IMPORT_SYM(unsigned long, __system_ram_start__, SYSTEM_RAM_START);
140IMPORT_SYM(unsigned long, __system_ram_end__, SYSTEM_RAM_END);
141IMPORT_SYM(unsigned long, __SRAM_COPY_START__, SRAM_COPY_START);
142#endif
143
144#if RCAR_SYSTEM_SUSPEND
145static void __attribute__ ((section (".system_ram")))
146 rcar_pwrc_micro_delay(uint64_t micro_sec)
147{
148 uint64_t freq, base, val;
149 uint64_t wait_time = 0;
150
151 freq = read_cntfrq_el0();
152 base = read_cntpct_el0();
153
154 while (micro_sec > wait_time) {
155 val = read_cntpct_el0() - base;
156 wait_time = val * RCAR_CONV_MICROSEC / freq;
157 }
158}
159#endif
160
161uint32_t rcar_pwrc_status(uint64_t mpidr)
162{
163 uint32_t ret = 0;
164 uint64_t cm, cpu;
165 uint32_t reg;
166 uint32_t c;
167
168 rcar_lock_get();
169
170 c = rcar_pwrc_get_cluster();
171 cm = mpidr & MPIDR_CLUSTER_MASK;
172
173 if (!IS_A53A57(c) && cm != 0) {
174 ret = RCAR_INVALID;
175 goto done;
176 }
177
178 reg = mmio_read_32(RCAR_PRR);
179 cpu = mpidr & MPIDR_CPU_MASK;
180
181 if (IS_CA53(c))
182 if (reg & (1 << (STATE_CA53_CPU + cpu)))
183 ret = RCAR_INVALID;
184 if (IS_CA57(c))
185 if (reg & (1 << (STATE_CA57_CPU + cpu)))
186 ret = RCAR_INVALID;
187done:
188 rcar_lock_release();
189
190 return ret;
191}
192
193static void scu_power_up(uint64_t mpidr)
194{
195 uintptr_t reg_pwrsr, reg_cpumcr, reg_pwron, reg_pwrer;
196 uint32_t c, sysc_reg_bit;
197
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
211 mmio_setbits_32(RCAR_SYSCIER, sysc_reg_bit);
212 mmio_setbits_32(RCAR_SYSCIMR, sysc_reg_bit);
213
214 do {
215 while ((mmio_read_32(RCAR_SYSCSR) & REQ_RESUME) == 0)
216 ;
217 mmio_write_32(reg_pwron, 1);
218 } while (mmio_read_32(reg_pwrer) & 1);
219
220 while ((mmio_read_32(RCAR_SYSCISR) & sysc_reg_bit) == 0)
221 ;
222 mmio_write_32(RCAR_SYSCISR, sysc_reg_bit);
223 while ((mmio_read_32(reg_pwrsr) & STATUS_PWRUP) == 0)
224 ;
225}
226
227void rcar_pwrc_cpuon(uint64_t mpidr)
228{
229 uint32_t res_data, on_data;
230 uintptr_t res_reg, on_reg;
231 uint32_t limit, c;
232 uint64_t cpu;
233
234 rcar_lock_get();
235
236 c = rcar_pwrc_get_mpidr_cluster(mpidr);
237 res_reg = IS_CA53(c) ? RCAR_CA53RESCNT : RCAR_CA57RESCNT;
238 on_reg = IS_CA53(c) ? RCAR_CA53WUPCR : RCAR_CA57WUPCR;
239 limit = IS_CA53(c) ? 0x5A5A0000 : 0xA5A50000;
240
241 res_data = mmio_read_32(res_reg) | limit;
242 scu_power_up(mpidr);
243 cpu = mpidr & MPIDR_CPU_MASK;
244 on_data = 1 << cpu;
245 mmio_write_32(RCAR_CPGWPR, ~on_data);
246 mmio_write_32(on_reg, on_data);
247 mmio_write_32(res_reg, res_data & (~(1 << (3 - cpu))));
248
249 rcar_lock_release();
250}
251
252void rcar_pwrc_cpuoff(uint64_t mpidr)
253{
254 uint32_t c;
255 uintptr_t reg;
256 uint64_t cpu;
257
258 rcar_lock_get();
259
260 cpu = mpidr & MPIDR_CPU_MASK;
261 c = rcar_pwrc_get_mpidr_cluster(mpidr);
262 reg = IS_CA53(c) ? RCAR_CA53CPU0CR : RCAR_CA57CPU0CR;
263
264 if (read_mpidr_el1() != mpidr)
265 panic();
266
267 mmio_write_32(RCAR_CPGWPR, ~CPU_PWR_OFF);
268 mmio_write_32(reg + cpu * 0x0010, CPU_PWR_OFF);
269
270 rcar_lock_release();
271}
272
273void rcar_pwrc_enable_interrupt_wakeup(uint64_t mpidr)
274{
275 uint32_t c, shift_irq, shift_fiq;
276 uintptr_t reg;
277 uint64_t cpu;
278
279 rcar_lock_get();
280
281 cpu = mpidr & MPIDR_CPU_MASK;
282 c = rcar_pwrc_get_mpidr_cluster(mpidr);
283 reg = IS_CA53(c) ? RCAR_WUPMSKCA53 : RCAR_WUPMSKCA57;
284
285 shift_irq = WUP_IRQ_SHIFT + cpu;
286 shift_fiq = WUP_FIQ_SHIFT + cpu;
287
288 mmio_write_32(reg, ~((uint32_t) 1 << shift_irq) &
289 ~((uint32_t) 1 << shift_fiq));
290 rcar_lock_release();
291}
292
293void rcar_pwrc_disable_interrupt_wakeup(uint64_t mpidr)
294{
295 uint32_t c, shift_irq, shift_fiq;
296 uintptr_t reg;
297 uint64_t cpu;
298
299 rcar_lock_get();
300
301 cpu = mpidr & MPIDR_CPU_MASK;
302 c = rcar_pwrc_get_mpidr_cluster(mpidr);
303 reg = IS_CA53(c) ? RCAR_WUPMSKCA53 : RCAR_WUPMSKCA57;
304
305 shift_irq = WUP_IRQ_SHIFT + cpu;
306 shift_fiq = WUP_FIQ_SHIFT + cpu;
307
308 mmio_write_32(reg, ((uint32_t) 1 << shift_irq) |
309 ((uint32_t) 1 << shift_fiq));
310 rcar_lock_release();
311}
312
313void rcar_pwrc_clusteroff(uint64_t mpidr)
314{
315 uint32_t c, product, cut, reg;
316 uintptr_t dst;
317
318 rcar_lock_get();
319
320 reg = mmio_read_32(RCAR_PRR);
321 product = reg & RCAR_PRODUCT_MASK;
322 cut = reg & RCAR_CUT_MASK;
323
324 c = rcar_pwrc_get_mpidr_cluster(mpidr);
325 dst = IS_CA53(c) ? RCAR_CA53CPUCMCR : RCAR_CA57CPUCMCR;
326
327 if (RCAR_PRODUCT_M3 == product && cut <= RCAR_M3_CUT_VER11)
328 goto done;
329
330 if (RCAR_PRODUCT_H3 == product && cut <= RCAR_CUT_VER20)
331 goto done;
332
333 /* all of the CPUs in the cluster is in the CoreStandby mode */
334 mmio_write_32(dst, MODE_L2_DOWN);
335done:
336 rcar_lock_release();
337}
338
339#if !PMIC_ROHM_BD9571
340void rcar_pwrc_system_reset(void)
341{
342 mmio_write_32(RCAR_SRESCR, 0x5AA50000U | BIT_SOFTRESET);
343}
344#endif /* PMIC_ROHM_BD9571 */
345
346#define RST_CA53_CPU0_BARH (0xE6160080U)
347#define RST_CA53_CPU0_BARL (0xE6160084U)
348#define RST_CA57_CPU0_BARH (0xE61600C0U)
349#define RST_CA57_CPU0_BARL (0xE61600C4U)
350
351void rcar_pwrc_setup(void)
352{
353 uintptr_t rst_barh;
354 uintptr_t rst_barl;
355 uint32_t i, j;
356 uint64_t reset = (uint64_t) (&plat_secondary_reset) & 0xFFFFFFFF;
357
358 const uint32_t cluster[PLATFORM_CLUSTER_COUNT] = {
359 RCAR_CLUSTER_CA53,
360 RCAR_CLUSTER_CA57
361 };
362 const uintptr_t reg_barh[PLATFORM_CLUSTER_COUNT] = {
363 RST_CA53_CPU0_BARH,
364 RST_CA57_CPU0_BARH
365 };
366 const uintptr_t reg_barl[PLATFORM_CLUSTER_COUNT] = {
367 RST_CA53_CPU0_BARL,
368 RST_CA57_CPU0_BARL
369 };
370
371 for (i = 0; i < PLATFORM_CLUSTER_COUNT; i++) {
372 rst_barh = reg_barh[i];
373 rst_barl = reg_barl[i];
374 for (j = 0; j < rcar_pwrc_get_cpu_num(cluster[i]); j++) {
375 mmio_write_32(rst_barh, 0);
376 mmio_write_32(rst_barl, (uint32_t) reset);
377 rst_barh += 0x10;
378 rst_barl += 0x10;
379 }
380 }
381
382 rcar_lock_init();
383}
384
385#if RCAR_SYSTEM_SUSPEND
386#define DBCAM_FLUSH(__bit) \
387do { \
388 ; \
389} while (!(mmio_read_32(DBSC4_REG_DBCAM##__bit##STAT0) & DBSC4_BIT_DBCAMxSTAT0))
390
391
392static void __attribute__ ((section(".system_ram")))
393 rcar_pwrc_set_self_refresh(void)
394{
395 uint32_t reg = mmio_read_32(RCAR_PRR);
396 uint32_t cut, product;
397
398 product = reg & RCAR_PRODUCT_MASK;
399 cut = reg & RCAR_CUT_MASK;
400
401 if (product == RCAR_PRODUCT_M3)
402 goto self_refresh;
403
404 if (product == RCAR_PRODUCT_H3 && cut < RCAR_CUT_VER20)
405 goto self_refresh;
406
407 mmio_write_32(DBSC4_REG_DBSYSCNT0, DBSC4_SET_DBSYSCNT0_WRITE_ENABLE);
408
409self_refresh:
410
411 /* Set the Self-Refresh mode */
412 mmio_write_32(DBSC4_REG_DBACEN, 0);
413
414 if (product == RCAR_PRODUCT_H3 && cut < RCAR_CUT_VER20)
415 rcar_pwrc_micro_delay(100);
416 else if (product == RCAR_PRODUCT_H3) {
417 mmio_write_32(DBSC4_REG_DBCAM0CTRL0, 1);
418 DBCAM_FLUSH(0);
419 DBCAM_FLUSH(1);
420 DBCAM_FLUSH(2);
421 DBCAM_FLUSH(3);
422 mmio_write_32(DBSC4_REG_DBCAM0CTRL0, 0);
423 } else if (product == RCAR_PRODUCT_M3) {
424 mmio_write_32(DBSC4_REG_DBCAM0CTRL0, 1);
425 DBCAM_FLUSH(0);
426 DBCAM_FLUSH(1);
427 mmio_write_32(DBSC4_REG_DBCAM0CTRL0, 0);
428 } else {
429 mmio_write_32(DBSC4_REG_DBCAM0CTRL0, 1);
430 DBCAM_FLUSH(0);
431 mmio_write_32(DBSC4_REG_DBCAM0CTRL0, 0);
432 }
433
434 /* Set the SDRAM calibration configuration register */
435 mmio_write_32(DBSC4_REG_DBCALCNF, 0);
436
437 reg = DBSC4_SET_DBCMD_OPC_PRE | DBSC4_SET_DBCMD_CH_ALL |
438 DBSC4_SET_DBCMD_RANK_ALL | DBSC4_SET_DBCMD_ARG_ALL;
439 mmio_write_32(DBSC4_REG_DBCMD, reg);
440 while (mmio_read_32(DBSC4_REG_DBWAIT))
441 ;
442
443 /* Self-Refresh entry command */
444 reg = DBSC4_SET_DBCMD_OPC_SR | DBSC4_SET_DBCMD_CH_ALL |
445 DBSC4_SET_DBCMD_RANK_ALL | DBSC4_SET_DBCMD_ARG_ENTER;
446 mmio_write_32(DBSC4_REG_DBCMD, reg);
447 while (mmio_read_32(DBSC4_REG_DBWAIT))
448 ;
449
450 /* Mode Register Write command. (ODT disabled) */
451 reg = DBSC4_SET_DBCMD_OPC_MRW | DBSC4_SET_DBCMD_CH_ALL |
452 DBSC4_SET_DBCMD_RANK_ALL | DBSC4_SET_DBCMD_ARG_MRW_ODTC;
453 mmio_write_32(DBSC4_REG_DBCMD, reg);
454 while (mmio_read_32(DBSC4_REG_DBWAIT))
455 ;
456
457 /* Power Down entry command */
458 reg = DBSC4_SET_DBCMD_OPC_PD | DBSC4_SET_DBCMD_CH_ALL |
459 DBSC4_SET_DBCMD_RANK_ALL | DBSC4_SET_DBCMD_ARG_ENTER;
460 mmio_write_32(DBSC4_REG_DBCMD, reg);
461 while (mmio_read_32(DBSC4_REG_DBWAIT))
462 ;
463
464 /* Set the auto-refresh enable register */
465 mmio_write_32(DBSC4_REG_DBRFEN, 0U);
466 rcar_pwrc_micro_delay(1U);
467
468 if (product == RCAR_PRODUCT_M3)
469 return;
470
471 if (product == RCAR_PRODUCT_H3 && cut < RCAR_CUT_VER20)
472 return;
473
474 mmio_write_32(DBSC4_REG_DBSYSCNT0, DBSC4_SET_DBSYSCNT0_WRITE_DISABLE);
475}
476
477static void __attribute__ ((section(".system_ram")))
478 rcar_pwrc_set_self_refresh_e3(void)
479{
480 uint32_t ddr_md;
481 uint32_t reg;
482
483 ddr_md = (mmio_read_32(RST_MODEMR) >> 19) & RST_MODEMR_BIT0;
484
485 /* Write enable */
486 mmio_write_32(DBSC4_REG_DBSYSCNT0, DBSC4_SET_DBSYSCNT0_WRITE_ENABLE);
487 mmio_write_32(DBSC4_REG_DBACEN, 0);
488 DBCAM_FLUSH(0);
489
490 reg = DBSC4_SET_DBCMD_OPC_PRE | DBSC4_SET_DBCMD_CH_ALL |
491 DBSC4_SET_DBCMD_RANK_ALL | DBSC4_SET_DBCMD_ARG_ALL;
492 mmio_write_32(DBSC4_REG_DBCMD, reg);
493 while (mmio_read_32(DBSC4_REG_DBWAIT))
494 ;
495
496 reg = DBSC4_SET_DBCMD_OPC_SR | DBSC4_SET_DBCMD_CH_ALL |
497 DBSC4_SET_DBCMD_RANK_ALL | DBSC4_SET_DBCMD_ARG_ENTER;
498 mmio_write_32(DBSC4_REG_DBCMD, reg);
499 while (mmio_read_32(DBSC4_REG_DBWAIT))
500 ;
501
502 /* Set the auto-refresh enable register */
503 /* Set the ARFEN bit to 0 in the DBRFEN */
504 mmio_write_32(DBSC4_REG_DBRFEN, 0);
505
506 mmio_write_32(DBSC4_REG_DBPDLK0, DBSC4_SET_DBPDLK0_PHY_ACCESS);
507
508 mmio_write_32(DBSC4_REG_DBPDRGA0, DBSC4_SET_DBPDRGA0_ACIOCR0);
509 mmio_write_32(DBSC4_REG_DBPDRGD0, DBSC4_SET_DBPDRGD0_ACIOCR0);
510
511 /* DDR_DXCCR */
512 mmio_write_32(DBSC4_REG_DBPDRGA0, DBSC4_SET_DBPDRGA0_DXCCR);
513 mmio_write_32(DBSC4_REG_DBPDRGD0, DBSC4_SET_DBPDRGD0_DXCCR);
514
515 /* DDR_PGCR1 */
516 mmio_write_32(DBSC4_REG_DBPDRGA0, DBSC4_SET_DBPDRGA0_PGCR1);
517 mmio_write_32(DBSC4_REG_DBPDRGD0, DBSC4_SET_DBPDRGD0_PGCR1);
518
519 /* DDR_ACIOCR1 */
520 mmio_write_32(DBSC4_REG_DBPDRGA0, DBSC4_SET_DBPDRGA0_ACIOCR1);
521 mmio_write_32(DBSC4_REG_DBPDRGD0, DBSC4_SET_DBPDRGD0_ACIOCR1);
522
523 /* DDR_ACIOCR3 */
524 mmio_write_32(DBSC4_REG_DBPDRGA0, DBSC4_SET_DBPDRGA0_ACIOCR3);
525 mmio_write_32(DBSC4_REG_DBPDRGD0, DBSC4_SET_DBPDRGD0_ACIOCR3);
526
527 /* DDR_ACIOCR5 */
528 mmio_write_32(DBSC4_REG_DBPDRGA0, DBSC4_SET_DBPDRGA0_ACIOCR5);
529 mmio_write_32(DBSC4_REG_DBPDRGD0, DBSC4_SET_DBPDRGD0_ACIOCR5);
530
531 /* DDR_DX0GCR2 */
532 mmio_write_32(DBSC4_REG_DBPDRGA0, DBSC4_SET_DBPDRGA0_DX0GCR2);
533 mmio_write_32(DBSC4_REG_DBPDRGD0, DBSC4_SET_DBPDRGD0_DX0GCR2);
534
535 /* DDR_DX1GCR2 */
536 mmio_write_32(DBSC4_REG_DBPDRGA0, DBSC4_SET_DBPDRGA0_DX1GCR2);
537 mmio_write_32(DBSC4_REG_DBPDRGD0, DBSC4_SET_DBPDRGD0_DX1GCR2);
538
539 /* DDR_DX2GCR2 */
540 mmio_write_32(DBSC4_REG_DBPDRGA0, DBSC4_SET_DBPDRGA0_DX2GCR2);
541 mmio_write_32(DBSC4_REG_DBPDRGD0, DBSC4_SET_DBPDRGD0_DX2GCR2);
542
543 /* DDR_DX3GCR2 */
544 mmio_write_32(DBSC4_REG_DBPDRGA0, DBSC4_SET_DBPDRGA0_DX3GCR2);
545 mmio_write_32(DBSC4_REG_DBPDRGD0, DBSC4_SET_DBPDRGD0_DX3GCR2);
546
547 /* DDR_ZQCR */
548 mmio_write_32(DBSC4_REG_DBPDRGA0, DBSC4_SET_DBPDRGA0_ZQCR);
549
550 mmio_write_32(DBSC4_REG_DBPDRGD0, ddr_md == 0 ?
551 DBSC4_SET_DBPDRGD0_ZQCR_MD19_0 :
552 DBSC4_SET_DBPDRGD0_ZQCR_MD19_1);
553
554 /* DDR_DX0GCR0 */
555 mmio_write_32(DBSC4_REG_DBPDRGA0, DBSC4_SET_DBPDRGA0_DX0GCR0);
556 mmio_write_32(DBSC4_REG_DBPDRGD0, DBSC4_SET_DBPDRGD0_DX0GCR0);
557
558 /* DDR_DX1GCR0 */
559 mmio_write_32(DBSC4_REG_DBPDRGA0, DBSC4_SET_DBPDRGA0_DX1GCR0);
560 mmio_write_32(DBSC4_REG_DBPDRGD0, DBSC4_SET_DBPDRGD0_DX1GCR0);
561
562 /* DDR_DX2GCR0 */
563 mmio_write_32(DBSC4_REG_DBPDRGA0, DBSC4_SET_DBPDRGA0_DX2GCR0);
564 mmio_write_32(DBSC4_REG_DBPDRGD0, DBSC4_SET_DBPDRGD0_DX2GCR0);
565
566 /* DDR_DX3GCR0 */
567 mmio_write_32(DBSC4_REG_DBPDRGA0, DBSC4_SET_DBPDRGA0_DX3GCR0);
568 mmio_write_32(DBSC4_REG_DBPDRGD0, DBSC4_SET_DBPDRGD0_DX3GCR0);
569
570 /* DDR_DX0GCR1 */
571 mmio_write_32(DBSC4_REG_DBPDRGA0, DBSC4_SET_DBPDRGA0_DX0GCR1);
572 mmio_write_32(DBSC4_REG_DBPDRGD0, DBSC4_SET_DBPDRGD0_DX0GCR1);
573
574 /* DDR_DX1GCR1 */
575 mmio_write_32(DBSC4_REG_DBPDRGA0, DBSC4_SET_DBPDRGA0_DX1GCR1);
576 mmio_write_32(DBSC4_REG_DBPDRGD0, DBSC4_SET_DBPDRGD0_DX1GCR1);
577
578 /* DDR_DX2GCR1 */
579 mmio_write_32(DBSC4_REG_DBPDRGA0, DBSC4_SET_DBPDRGA0_DX2GCR1);
580 mmio_write_32(DBSC4_REG_DBPDRGD0, DBSC4_SET_DBPDRGD0_DX2GCR1);
581
582 /* DDR_DX3GCR1 */
583 mmio_write_32(DBSC4_REG_DBPDRGA0, DBSC4_SET_DBPDRGA0_DX3GCR1);
584 mmio_write_32(DBSC4_REG_DBPDRGD0, DBSC4_SET_DBPDRGD0_DX3GCR1);
585
586 /* DDR_DX0GCR3 */
587 mmio_write_32(DBSC4_REG_DBPDRGA0, DBSC4_SET_DBPDRGA0_DX0GCR3);
588 mmio_write_32(DBSC4_REG_DBPDRGD0, DBSC4_SET_DBPDRGD0_DX0GCR3);
589
590 /* DDR_DX1GCR3 */
591 mmio_write_32(DBSC4_REG_DBPDRGA0, DBSC4_SET_DBPDRGA0_DX1GCR3);
592 mmio_write_32(DBSC4_REG_DBPDRGD0, DBSC4_SET_DBPDRGD0_DX1GCR3);
593
594 /* DDR_DX2GCR3 */
595 mmio_write_32(DBSC4_REG_DBPDRGA0, DBSC4_SET_DBPDRGA0_DX2GCR3);
596 mmio_write_32(DBSC4_REG_DBPDRGD0, DBSC4_SET_DBPDRGD0_DX2GCR3);
597
598 /* DDR_DX3GCR3 */
599 mmio_write_32(DBSC4_REG_DBPDRGA0, DBSC4_SET_DBPDRGA0_DX3GCR3);
600 mmio_write_32(DBSC4_REG_DBPDRGD0, DBSC4_SET_DBPDRGD0_DX3GCR3);
601
602 /* Write disable */
603 mmio_write_32(DBSC4_REG_DBSYSCNT0, DBSC4_SET_DBSYSCNT0_WRITE_DISABLE);
604}
605
606void __attribute__ ((section(".system_ram"))) __attribute__ ((noinline))
607 rcar_pwrc_go_suspend_to_ram(void)
608{
609#if PMIC_ROHM_BD9571
610 int32_t rc = -1, qllm = -1;
611 uint8_t mode;
612 uint32_t i;
613#endif
614 uint32_t reg, product;
615
616 reg = mmio_read_32(RCAR_PRR);
617 product = reg & RCAR_PRODUCT_MASK;
618
619 if (product != RCAR_PRODUCT_E3)
620 rcar_pwrc_set_self_refresh();
621 else
622 rcar_pwrc_set_self_refresh_e3();
623
624#if PMIC_ROHM_BD9571
625 /* Set QLLM Cnt Disable */
626 for (i = 0; (i < PMIC_RETRY_MAX) && (qllm != 0); i++)
627 qllm = rcar_iic_dvfs_send(PMIC, PMIC_QLLM_CNT, 0);
628
629 /* Set trigger of power down to PMIV */
630 for (i = 0; (i < PMIC_RETRY_MAX) && (rc != 0) && (qllm == 0); i++) {
631 rc = rcar_iic_dvfs_receive(PMIC, PMIC_BKUP_MODE_CNT, &mode);
632 if (rc == 0) {
633 mode |= BIT_BKUP_CTRL_OUT;
634 rc = rcar_iic_dvfs_send(PMIC, PMIC_BKUP_MODE_CNT, mode);
635 }
636 }
637#endif
638 wfi();
639
640 while (1)
641 ;
642}
643
644void rcar_pwrc_set_suspend_to_ram(void)
645{
646 uintptr_t jump = (uintptr_t) &rcar_pwrc_go_suspend_to_ram;
647 uintptr_t stack = (uintptr_t) (DEVICE_SRAM_STACK_BASE +
648 DEVICE_SRAM_STACK_SIZE);
649 uint32_t sctlr;
650
651 rcar_pwrc_code_copy_to_system_ram();
652 rcar_pwrc_save_generic_timer(rcar_stack_generic_timer);
653
654 /* disable MMU */
655 sctlr = (uint32_t) read_sctlr_el3();
656 sctlr &= (uint32_t) ~SCTLR_EL3_M_BIT;
657 write_sctlr_el3((uint64_t) sctlr);
658
659 rcar_pwrc_switch_stack(jump, stack, NULL);
660}
661
662void rcar_pwrc_init_suspend_to_ram(void)
663{
664#if PMIC_ROHM_BD9571
665 uint8_t mode;
666#endif
667 rcar_pwrc_code_copy_to_system_ram();
668
669#if PMIC_ROHM_BD9571
670 if (rcar_iic_dvfs_receive(PMIC, PMIC_BKUP_MODE_CNT, &mode))
671 panic();
672
673 mode &= (uint8_t) (~BIT_BKUP_CTRL_OUT);
674 if (rcar_iic_dvfs_send(PMIC, PMIC_BKUP_MODE_CNT, mode))
675 panic();
676#endif
677}
678
679void rcar_pwrc_suspend_to_ram(void)
680{
681#if RCAR_SYSTEM_RESET_KEEPON_DDR
682 int32_t error;
683
684 rcar_pwrc_code_copy_to_system_ram();
685 error = rcar_iic_dvfs_send(PMIC, REG_KEEP10, 0);
686 if (error) {
687 ERROR("Failed send KEEP10 init ret=%d \n", error);
688 return;
689 }
690#endif
691 rcar_pwrc_set_suspend_to_ram();
692}
693#endif
694
695void rcar_pwrc_code_copy_to_system_ram(void)
696{
697 int ret __attribute__ ((unused)); /* in assert */
698 uint32_t attr;
699 struct device_sram_t {
700 uintptr_t base;
701 size_t len;
702 } sram = {
703 .base = (uintptr_t) DEVICE_SRAM_BASE,
704 .len = DEVICE_SRAM_SIZE,
705 };
706 struct ddr_code_t {
707 void *base;
708 size_t len;
709 } code = {
710 .base = (void *) SRAM_COPY_START,
711 .len = SYSTEM_RAM_END - SYSTEM_RAM_START,
712 };
713
714 attr = MT_MEMORY | MT_RW | MT_SECURE | MT_EXECUTE_NEVER;
715 ret = xlat_change_mem_attributes(sram.base, sram.len, attr);
716 assert(ret == 0);
717
718 memcpy((void *)sram.base, code.base, code.len);
719 flush_dcache_range((uint64_t) sram.base, code.len);
720
721 /* Invalidate instruction cache */
722 plat_invalidate_icache();
723 dsb();
724 isb();
725
726 attr = MT_MEMORY | MT_RO | MT_SECURE | MT_EXECUTE;
727 ret = xlat_change_mem_attributes(sram.base, sram.len, attr);
728 assert(ret == 0);
729}
730
731uint32_t rcar_pwrc_get_cluster(void)
732{
733 uint32_t reg;
734
735 reg = mmio_read_32(RCAR_PRR);
736
737 if (reg & (1 << (STATE_CA53_CPU + RCAR_CA53CPU_NUM_MAX)))
738 return RCAR_CLUSTER_CA57;
739
740 if (reg & (1 << (STATE_CA57_CPU + RCAR_CA57CPU_NUM_MAX)))
741 return RCAR_CLUSTER_CA53;
742
743 return RCAR_CLUSTER_A53A57;
744}
745
746uint32_t rcar_pwrc_get_mpidr_cluster(uint64_t mpidr)
747{
748 uint32_t c = rcar_pwrc_get_cluster();
749
750 if (IS_A53A57(c)) {
751 if (mpidr & MPIDR_CLUSTER_MASK)
752 return RCAR_CLUSTER_CA53;
753
754 return RCAR_CLUSTER_CA57;
755 }
756
757 return c;
758}
759
760uint32_t rcar_pwrc_get_cpu_num(uint32_t c)
761{
762 uint32_t reg = mmio_read_32(RCAR_PRR);
763 uint32_t count = 0, i;
764
765 if (IS_A53A57(c) || IS_CA53(c)) {
766 if (reg & (1 << (STATE_CA53_CPU + RCAR_CA53CPU_NUM_MAX)))
767 goto count_ca57;
768
769 for (i = 0; i < RCAR_CA53CPU_NUM_MAX; i++) {
770 if (reg & (1 << (STATE_CA53_CPU + i)))
771 continue;
772 count++;
773 }
774 }
775
776count_ca57:
777 if (IS_A53A57(c) || IS_CA57(c)) {
778 if (reg & (1 << (STATE_CA57_CPU + RCAR_CA57CPU_NUM_MAX)))
779 goto done;
780
781 for (i = 0; i < RCAR_CA57CPU_NUM_MAX; i++) {
782 if (reg & (1 << (STATE_CA57_CPU + i)))
783 continue;
784 count++;
785 }
786 }
787
788done:
789 return count;
790}