pmu.c 28.3 KB
Newer Older
XiaoDong Huang's avatar
XiaoDong Huang committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/*
 * Copyright (c) 2019, ARM Limited and Contributors. All rights reserved.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include <assert.h>
#include <errno.h>

#include <platform_def.h>

#include <arch_helpers.h>
#include <bl31/bl31.h>
#include <common/debug.h>
#include <drivers/console.h>
#include <drivers/delay_timer.h>
#include <lib/bakery_lock.h>
#include <lib/mmio.h>
#include <plat/common/platform.h>

#include <cpus_on_fixed_addr.h>
#include <plat_private.h>
#include <pmu.h>
#include <px30_def.h>
25
#include <secure.h>
XiaoDong Huang's avatar
XiaoDong Huang committed
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
#include <soc.h>

DEFINE_BAKERY_LOCK(rockchip_pd_lock);
#define rockchip_pd_lock_init()	bakery_lock_init(&rockchip_pd_lock)
#define rockchip_pd_lock_get()	bakery_lock_get(&rockchip_pd_lock)
#define rockchip_pd_lock_rls()	bakery_lock_release(&rockchip_pd_lock)

static struct psram_data_t *psram_boot_cfg =
	(struct psram_data_t *)&sys_sleep_flag_sram;

/*
 * There are two ways to powering on or off on core.
 * 1) Control it power domain into on or off in PMU_PWRDN_CON reg,
 *    it is core_pwr_pd mode
 * 2) Enable the core power manage in PMU_CORE_PM_CON reg,
 *     then, if the core enter into wfi, it power domain will be
 *     powered off automatically. it is core_pwr_wfi or core_pwr_wfi_int mode
 * so we need core_pm_cfg_info to distinguish which method be used now.
 */

static uint32_t cores_pd_cfg_info[PLATFORM_CORE_COUNT]
#if USE_COHERENT_MEM
__attribute__ ((section("tzfw_coherent_mem")))
#endif
;

struct px30_sleep_ddr_data {
	uint32_t clk_sel0;
	uint32_t cru_mode_save;
	uint32_t cru_pmu_mode_save;
	uint32_t ddrc_hwlpctl;
	uint32_t ddrc_pwrctrl;
	uint32_t ddrgrf_con0;
	uint32_t ddrgrf_con1;
	uint32_t ddrstdby_con0;
	uint32_t gpio0b_iomux;
	uint32_t gpio0c_iomux;
	uint32_t pmu_pwrmd_core_l;
	uint32_t pmu_pwrmd_core_h;
	uint32_t pmu_pwrmd_cmm_l;
	uint32_t pmu_pwrmd_cmm_h;
	uint32_t pmu_wkup_cfg2_l;
	uint32_t pmu_cru_clksel_con0;
	uint32_t pmugrf_soc_con0;
	uint32_t pmusgrf_soc_con0;
	uint32_t pmic_slp_iomux;
	uint32_t pgrf_pvtm_con[2];
	uint32_t cru_clk_gate[CRU_CLKGATES_CON_CNT];
	uint32_t cru_pmu_clk_gate[CRU_PMU_CLKGATE_CON_CNT];
	uint32_t cru_plls_con_save[END_PLL_ID][PLL_CON_CNT];
	uint32_t cpu_qos[CPU_AXI_QOS_NUM_REGS];
	uint32_t gpu_qos[CPU_AXI_QOS_NUM_REGS];
	uint32_t isp_128m_qos[CPU_AXI_QOS_NUM_REGS];
	uint32_t isp_rd_qos[CPU_AXI_QOS_NUM_REGS];
	uint32_t isp_wr_qos[CPU_AXI_QOS_NUM_REGS];
	uint32_t isp_m1_qos[CPU_AXI_QOS_NUM_REGS];
	uint32_t vip_qos[CPU_AXI_QOS_NUM_REGS];
	uint32_t rga_rd_qos[CPU_AXI_QOS_NUM_REGS];
	uint32_t rga_wr_qos[CPU_AXI_QOS_NUM_REGS];
	uint32_t vop_m0_qos[CPU_AXI_QOS_NUM_REGS];
	uint32_t vop_m1_qos[CPU_AXI_QOS_NUM_REGS];
	uint32_t vpu_qos[CPU_AXI_QOS_NUM_REGS];
	uint32_t vpu_r128_qos[CPU_AXI_QOS_NUM_REGS];
	uint32_t dcf_qos[CPU_AXI_QOS_NUM_REGS];
	uint32_t dmac_qos[CPU_AXI_QOS_NUM_REGS];
	uint32_t crypto_qos[CPU_AXI_QOS_NUM_REGS];
	uint32_t gmac_qos[CPU_AXI_QOS_NUM_REGS];
	uint32_t emmc_qos[CPU_AXI_QOS_NUM_REGS];
	uint32_t nand_qos[CPU_AXI_QOS_NUM_REGS];
	uint32_t sdio_qos[CPU_AXI_QOS_NUM_REGS];
	uint32_t sfc_qos[CPU_AXI_QOS_NUM_REGS];
	uint32_t sdmmc_qos[CPU_AXI_QOS_NUM_REGS];
	uint32_t usb_host_qos[CPU_AXI_QOS_NUM_REGS];
	uint32_t usb_otg_qos[CPU_AXI_QOS_NUM_REGS];
};

static struct px30_sleep_ddr_data ddr_data
#if USE_COHERENT_MEM
__attribute__ ((section("tzfw_coherent_mem")))
#endif
;

static inline uint32_t get_cpus_pwr_domain_cfg_info(uint32_t cpu_id)
{
	assert(cpu_id < PLATFORM_CORE_COUNT);
	return cores_pd_cfg_info[cpu_id];
}

static inline void set_cpus_pwr_domain_cfg_info(uint32_t cpu_id, uint32_t value)
{
	assert(cpu_id < PLATFORM_CORE_COUNT);
	cores_pd_cfg_info[cpu_id] = value;
#if !USE_COHERENT_MEM
	flush_dcache_range((uintptr_t)&cores_pd_cfg_info[cpu_id],
			   sizeof(uint32_t));
#endif
}

static inline uint32_t pmu_power_domain_st(uint32_t pd)
{
	return mmio_read_32(PMU_BASE + PMU_PWRDN_ST) & BIT(pd) ?
	       pmu_pd_off :
	       pmu_pd_on;
}

static int pmu_power_domain_ctr(uint32_t pd, uint32_t pd_state)
{
	uint32_t loop = 0;
	int ret = 0;

	rockchip_pd_lock_get();

	mmio_write_32(PMU_BASE + PMU_PWRDN_CON,
		      BITS_WITH_WMASK(pd_state, 0x1, pd));
	dsb();

	while ((pmu_power_domain_st(pd) != pd_state) && (loop < PD_CTR_LOOP)) {
		udelay(1);
		loop++;
	}

	if (pmu_power_domain_st(pd) != pd_state) {
		WARN("%s: %d, %d, error!\n", __func__, pd, pd_state);
		ret = -EINVAL;
	}

	rockchip_pd_lock_rls();

	return ret;
}

static inline uint32_t pmu_bus_idle_st(uint32_t bus)
{
	return !!((mmio_read_32(PMU_BASE + PMU_BUS_IDLE_ST) & BIT(bus)) &&
		  (mmio_read_32(PMU_BASE + PMU_BUS_IDLE_ST) & BIT(bus + 16)));
}

static void pmu_bus_idle_req(uint32_t bus, uint32_t state)
{
	uint32_t wait_cnt = 0;

	mmio_write_32(PMU_BASE + PMU_BUS_IDLE_REQ,
		      BITS_WITH_WMASK(state, 0x1, bus));

	while (pmu_bus_idle_st(bus) != state &&
	       wait_cnt < BUS_IDLE_LOOP) {
		udelay(1);
		wait_cnt++;
	}

	if (pmu_bus_idle_st(bus) != state)
		WARN("%s:idle_st=0x%x, bus_id=%d\n",
		     __func__, mmio_read_32(PMU_BASE + PMU_BUS_IDLE_ST), bus);
}

static void qos_save(void)
{
	/* scu powerdomain will power off, so cpu qos should be saved */
	SAVE_QOS(ddr_data.cpu_qos, CPU);

	if (pmu_power_domain_st(PD_GPU) == pmu_pd_on)
		SAVE_QOS(ddr_data.gpu_qos, GPU);
	if (pmu_power_domain_st(PD_VI) == pmu_pd_on) {
		SAVE_QOS(ddr_data.isp_128m_qos, ISP_128M);
		SAVE_QOS(ddr_data.isp_rd_qos, ISP_RD);
		SAVE_QOS(ddr_data.isp_wr_qos, ISP_WR);
		SAVE_QOS(ddr_data.isp_m1_qos, ISP_M1);
		SAVE_QOS(ddr_data.vip_qos, VIP);
	}
	if (pmu_power_domain_st(PD_VO) == pmu_pd_on) {
		SAVE_QOS(ddr_data.rga_rd_qos, RGA_RD);
		SAVE_QOS(ddr_data.rga_wr_qos, RGA_WR);
		SAVE_QOS(ddr_data.vop_m0_qos, VOP_M0);
		SAVE_QOS(ddr_data.vop_m1_qos, VOP_M1);
	}
	if (pmu_power_domain_st(PD_VPU) == pmu_pd_on) {
		SAVE_QOS(ddr_data.vpu_qos, VPU);
		SAVE_QOS(ddr_data.vpu_r128_qos, VPU_R128);
	}
	if (pmu_power_domain_st(PD_MMC_NAND) == pmu_pd_on) {
		SAVE_QOS(ddr_data.emmc_qos, EMMC);
		SAVE_QOS(ddr_data.nand_qos, NAND);
		SAVE_QOS(ddr_data.sdio_qos, SDIO);
		SAVE_QOS(ddr_data.sfc_qos, SFC);
	}
	if (pmu_power_domain_st(PD_GMAC) == pmu_pd_on)
		SAVE_QOS(ddr_data.gmac_qos, GMAC);
	if (pmu_power_domain_st(PD_CRYPTO) == pmu_pd_on)
		SAVE_QOS(ddr_data.crypto_qos, CRYPTO);
	if (pmu_power_domain_st(PD_SDCARD) == pmu_pd_on)
		SAVE_QOS(ddr_data.sdmmc_qos, SDMMC);
	if (pmu_power_domain_st(PD_USB) == pmu_pd_on) {
		SAVE_QOS(ddr_data.usb_host_qos, USB_HOST);
		SAVE_QOS(ddr_data.usb_otg_qos, USB_OTG);
	}
}

static void qos_restore(void)
{
	RESTORE_QOS(ddr_data.cpu_qos, CPU);

	if (pmu_power_domain_st(PD_GPU) == pmu_pd_on)
		RESTORE_QOS(ddr_data.gpu_qos, GPU);
	if (pmu_power_domain_st(PD_VI) == pmu_pd_on) {
		RESTORE_QOS(ddr_data.isp_128m_qos, ISP_128M);
		RESTORE_QOS(ddr_data.isp_rd_qos, ISP_RD);
		RESTORE_QOS(ddr_data.isp_wr_qos, ISP_WR);
		RESTORE_QOS(ddr_data.isp_m1_qos, ISP_M1);
		RESTORE_QOS(ddr_data.vip_qos, VIP);
	}
	if (pmu_power_domain_st(PD_VO) == pmu_pd_on) {
		RESTORE_QOS(ddr_data.rga_rd_qos, RGA_RD);
		RESTORE_QOS(ddr_data.rga_wr_qos, RGA_WR);
		RESTORE_QOS(ddr_data.vop_m0_qos, VOP_M0);
		RESTORE_QOS(ddr_data.vop_m1_qos, VOP_M1);
	}
	if (pmu_power_domain_st(PD_VPU) == pmu_pd_on) {
		RESTORE_QOS(ddr_data.vpu_qos, VPU);
		RESTORE_QOS(ddr_data.vpu_r128_qos, VPU_R128);
	}
	if (pmu_power_domain_st(PD_MMC_NAND) == pmu_pd_on) {
		RESTORE_QOS(ddr_data.emmc_qos, EMMC);
		RESTORE_QOS(ddr_data.nand_qos, NAND);
		RESTORE_QOS(ddr_data.sdio_qos, SDIO);
		RESTORE_QOS(ddr_data.sfc_qos, SFC);
	}
	if (pmu_power_domain_st(PD_GMAC) == pmu_pd_on)
		RESTORE_QOS(ddr_data.gmac_qos, GMAC);
	if (pmu_power_domain_st(PD_CRYPTO) == pmu_pd_on)
		RESTORE_QOS(ddr_data.crypto_qos, CRYPTO);
	if (pmu_power_domain_st(PD_SDCARD) == pmu_pd_on)
		RESTORE_QOS(ddr_data.sdmmc_qos, SDMMC);
	if (pmu_power_domain_st(PD_USB) == pmu_pd_on) {
		RESTORE_QOS(ddr_data.usb_host_qos, USB_HOST);
		RESTORE_QOS(ddr_data.usb_otg_qos, USB_OTG);
	}
}

static int pmu_set_power_domain(uint32_t pd_id, uint32_t pd_state)
{
	uint32_t state;

	if (pmu_power_domain_st(pd_id) == pd_state)
		goto out;

	if (pd_state == pmu_pd_on)
		pmu_power_domain_ctr(pd_id, pd_state);

	state = (pd_state == pmu_pd_off) ? bus_idle : bus_active;

	switch (pd_id) {
	case PD_GPU:
		pmu_bus_idle_req(BUS_ID_GPU, state);
		break;
	case PD_VI:
		pmu_bus_idle_req(BUS_ID_VI, state);
		break;
	case PD_VO:
		pmu_bus_idle_req(BUS_ID_VO, state);
		break;
	case PD_VPU:
		pmu_bus_idle_req(BUS_ID_VPU, state);
		break;
	case PD_MMC_NAND:
		pmu_bus_idle_req(BUS_ID_MMC, state);
		break;
	case PD_GMAC:
		pmu_bus_idle_req(BUS_ID_GMAC, state);
		break;
	case PD_CRYPTO:
		pmu_bus_idle_req(BUS_ID_CRYPTO, state);
		break;
	case PD_SDCARD:
		pmu_bus_idle_req(BUS_ID_SDCARD, state);
		break;
	case PD_USB:
		pmu_bus_idle_req(BUS_ID_USB, state);
		break;
	default:
		break;
	}

	if (pd_state == pmu_pd_off)
		pmu_power_domain_ctr(pd_id, pd_state);

out:
	return 0;
}

static uint32_t pmu_powerdomain_state;

static void pmu_power_domains_suspend(void)
{
	uint32_t clkgt_save[CRU_CLKGATES_CON_CNT + CRU_PMU_CLKGATE_CON_CNT];

	clk_gate_con_save(clkgt_save);
	clk_gate_con_disable();
	qos_save();

	pmu_powerdomain_state = mmio_read_32(PMU_BASE + PMU_PWRDN_ST);
	pmu_set_power_domain(PD_GPU, pmu_pd_off);
	pmu_set_power_domain(PD_VI, pmu_pd_off);
	pmu_set_power_domain(PD_VO, pmu_pd_off);
	pmu_set_power_domain(PD_VPU, pmu_pd_off);
	pmu_set_power_domain(PD_MMC_NAND, pmu_pd_off);
	pmu_set_power_domain(PD_GMAC, pmu_pd_off);
	pmu_set_power_domain(PD_CRYPTO, pmu_pd_off);
	pmu_set_power_domain(PD_SDCARD, pmu_pd_off);
	pmu_set_power_domain(PD_USB, pmu_pd_off);

	clk_gate_con_restore(clkgt_save);
}

static void pmu_power_domains_resume(void)
{
	uint32_t clkgt_save[CRU_CLKGATES_CON_CNT + CRU_PMU_CLKGATE_CON_CNT];

	clk_gate_con_save(clkgt_save);
	clk_gate_con_disable();

	if (!(pmu_powerdomain_state & BIT(PD_USB)))
		pmu_set_power_domain(PD_USB, pmu_pd_on);
	if (!(pmu_powerdomain_state & BIT(PD_SDCARD)))
		pmu_set_power_domain(PD_SDCARD, pmu_pd_on);
	if (!(pmu_powerdomain_state & BIT(PD_CRYPTO)))
		pmu_set_power_domain(PD_CRYPTO, pmu_pd_on);
	if (!(pmu_powerdomain_state & BIT(PD_GMAC)))
		pmu_set_power_domain(PD_GMAC, pmu_pd_on);
	if (!(pmu_powerdomain_state & BIT(PD_MMC_NAND)))
		pmu_set_power_domain(PD_MMC_NAND, pmu_pd_on);
	if (!(pmu_powerdomain_state & BIT(PD_VPU)))
		pmu_set_power_domain(PD_VPU, pmu_pd_on);
	if (!(pmu_powerdomain_state & BIT(PD_VO)))
		pmu_set_power_domain(PD_VO, pmu_pd_on);
	if (!(pmu_powerdomain_state & BIT(PD_VI)))
		pmu_set_power_domain(PD_VI, pmu_pd_on);
	if (!(pmu_powerdomain_state & BIT(PD_GPU)))
		pmu_set_power_domain(PD_GPU, pmu_pd_on);

	qos_restore();
	clk_gate_con_restore(clkgt_save);
}

static int check_cpu_wfie(uint32_t cpu)
{
	uint32_t loop = 0, wfie_msk = CKECK_WFEI_MSK << cpu;

	while (!(mmio_read_32(GRF_BASE + GRF_CPU_STATUS1) & wfie_msk) &&
	       (loop < WFEI_CHECK_LOOP)) {
		udelay(1);
		loop++;
	}

	if ((mmio_read_32(GRF_BASE + GRF_CPU_STATUS1) & wfie_msk) == 0) {
		WARN("%s: %d, %d, error!\n", __func__, cpu, wfie_msk);
		return -EINVAL;
	}

	return 0;
}

static int cpus_power_domain_on(uint32_t cpu_id)
{
	uint32_t cpu_pd, apm_value, cfg_info, loop = 0;

	cpu_pd = PD_CPU0 + cpu_id;
	cfg_info = get_cpus_pwr_domain_cfg_info(cpu_id);

	if (cfg_info == core_pwr_pd) {
		/* disable apm cfg */
		mmio_write_32(PMU_BASE + PMU_CPUAPM_CON(cpu_id),
			      WITH_16BITS_WMSK(CORES_PM_DISABLE));
		if (pmu_power_domain_st(cpu_pd) == pmu_pd_on) {
			mmio_write_32(PMU_BASE + PMU_CPUAPM_CON(cpu_id),
				      WITH_16BITS_WMSK(CORES_PM_DISABLE));
			pmu_power_domain_ctr(cpu_pd, pmu_pd_off);
		}
		pmu_power_domain_ctr(cpu_pd, pmu_pd_on);
	} else {
		/* wait cpu down */
		while (pmu_power_domain_st(cpu_pd) == pmu_pd_on && loop < 100) {
			udelay(2);
			loop++;
		}

		/* return error if can't wait cpu down */
		if (pmu_power_domain_st(cpu_pd) == pmu_pd_on) {
			WARN("%s:can't wait cpu down\n", __func__);
			return -EINVAL;
		}

		/* power up cpu in power down state */
		apm_value = BIT(core_pm_sft_wakeup_en);
		mmio_write_32(PMU_BASE + PMU_CPUAPM_CON(cpu_id),
			      WITH_16BITS_WMSK(apm_value));
	}

	return 0;
}

static int cpus_power_domain_off(uint32_t cpu_id, uint32_t pd_cfg)
{
	uint32_t cpu_pd, apm_value;

	cpu_pd = PD_CPU0 + cpu_id;
	if (pmu_power_domain_st(cpu_pd) == pmu_pd_off)
		return 0;

	if (pd_cfg == core_pwr_pd) {
		if (check_cpu_wfie(cpu_id))
			return -EINVAL;
		/* disable apm cfg */
		mmio_write_32(PMU_BASE + PMU_CPUAPM_CON(cpu_id),
			      WITH_16BITS_WMSK(CORES_PM_DISABLE));
		set_cpus_pwr_domain_cfg_info(cpu_id, pd_cfg);
		pmu_power_domain_ctr(cpu_pd, pmu_pd_off);
	} else {
		set_cpus_pwr_domain_cfg_info(cpu_id, pd_cfg);
		apm_value = BIT(core_pm_en) | BIT(core_pm_dis_int);
		if (pd_cfg == core_pwr_wfi_int)
			apm_value |= BIT(core_pm_int_wakeup_en);
		mmio_write_32(PMU_BASE + PMU_CPUAPM_CON(cpu_id),
			      WITH_16BITS_WMSK(apm_value));
	}

	return 0;
}

static void nonboot_cpus_off(void)
{
	uint32_t boot_cpu, cpu;

	boot_cpu = plat_my_core_pos();

	for (cpu = 0; cpu < PLATFORM_CORE_COUNT; cpu++) {
		if (cpu == boot_cpu)
			continue;
		cpus_power_domain_off(cpu, core_pwr_pd);
	}
}

int rockchip_soc_cores_pwr_dm_on(unsigned long mpidr,
				 uint64_t entrypoint)
{
	uint32_t cpu_id = plat_core_pos_by_mpidr(mpidr);

	assert(cpu_id < PLATFORM_CORE_COUNT);
	assert(cpuson_flags[cpu_id] == 0);
	cpuson_flags[cpu_id] = PMU_CPU_HOTPLUG;
	cpuson_entry_point[cpu_id] = entrypoint;
	dsb();

	cpus_power_domain_on(cpu_id);

	return PSCI_E_SUCCESS;
}

int rockchip_soc_cores_pwr_dm_on_finish(void)
{
	uint32_t cpu_id = plat_my_core_pos();

	mmio_write_32(PMU_BASE + PMU_CPUAPM_CON(cpu_id),
		      WITH_16BITS_WMSK(CORES_PM_DISABLE));
	return PSCI_E_SUCCESS;
}

int rockchip_soc_cores_pwr_dm_off(void)
{
	uint32_t cpu_id = plat_my_core_pos();

	cpus_power_domain_off(cpu_id, core_pwr_wfi);

	return PSCI_E_SUCCESS;
}

int rockchip_soc_cores_pwr_dm_suspend(void)
{
	uint32_t cpu_id = plat_my_core_pos();

	assert(cpu_id < PLATFORM_CORE_COUNT);
	assert(cpuson_flags[cpu_id] == 0);
	cpuson_flags[cpu_id] = PMU_CPU_AUTO_PWRDN;
	cpuson_entry_point[cpu_id] = plat_get_sec_entrypoint();
	dsb();

	cpus_power_domain_off(cpu_id, core_pwr_wfi_int);

	return PSCI_E_SUCCESS;
}

int rockchip_soc_cores_pwr_dm_resume(void)
{
	uint32_t cpu_id = plat_my_core_pos();

	/* Disable core_pm */
	mmio_write_32(PMU_BASE + PMU_CPUAPM_CON(cpu_id),
		      WITH_16BITS_WMSK(CORES_PM_DISABLE));

	return PSCI_E_SUCCESS;
}

#define CLK_MSK_GATING(msk, con) \
	mmio_write_32(CRU_BASE + (con), ((msk) << 16) | 0xffff)
#define CLK_MSK_UNGATING(msk, con) \
	mmio_write_32(CRU_BASE + (con), ((~(msk)) << 16) | 0xffff)

static uint32_t clk_ungt_msk[CRU_CLKGATES_CON_CNT] = {
	0xe0ff, 0xffff, 0x0000, 0x0000,
	0x0000, 0x0380, 0x0000, 0x0000,
	0x07c0, 0x0000, 0x0000, 0x000f,
	0x0061, 0x1f02, 0x0440, 0x1801,
	0x004b, 0x0000
};

static uint32_t clk_pmu_ungt_msk[CRU_PMU_CLKGATE_CON_CNT] = {
	0xf1ff, 0x0310
};

void clk_gate_suspend(void)
{
	int i;

	for (i = 0; i < CRU_CLKGATES_CON_CNT; i++) {
		ddr_data.cru_clk_gate[i] =
			mmio_read_32(CRU_BASE + CRU_CLKGATES_CON(i));
			mmio_write_32(CRU_BASE + CRU_CLKGATES_CON(i),
				      WITH_16BITS_WMSK(~clk_ungt_msk[i]));
	}

	for (i = 0; i < CRU_PMU_CLKGATE_CON_CNT; i++) {
		ddr_data.cru_pmu_clk_gate[i] =
			mmio_read_32(PMUCRU_BASE + CRU_PMU_CLKGATES_CON(i));
			mmio_write_32(PMUCRU_BASE + CRU_PMU_CLKGATES_CON(i),
				      WITH_16BITS_WMSK(~clk_pmu_ungt_msk[i]));
	}
}

void clk_gate_resume(void)
{
	int i;

	for (i = 0; i < CRU_PMU_CLKGATE_CON_CNT; i++)
		mmio_write_32(PMUCRU_BASE + CRU_PMU_CLKGATES_CON(i),
			      WITH_16BITS_WMSK(ddr_data.cru_pmu_clk_gate[i]));

	for (i = 0; i < CRU_CLKGATES_CON_CNT; i++)
		mmio_write_32(CRU_BASE + CRU_CLKGATES_CON(i),
			      WITH_16BITS_WMSK(ddr_data.cru_clk_gate[i]));
}

static void pvtm_32k_config(void)
{
	uint32_t  pvtm_freq_khz, pvtm_div;

	ddr_data.pmu_cru_clksel_con0 =
		mmio_read_32(PMUCRU_BASE + CRU_PMU_CLKSELS_CON(0));

	ddr_data.pgrf_pvtm_con[0] =
		mmio_read_32(PMUGRF_BASE + PMUGRF_PVTM_CON0);
	ddr_data.pgrf_pvtm_con[1] =
		mmio_read_32(PMUGRF_BASE + PMUGRF_PVTM_CON1);

	mmio_write_32(PMUGRF_BASE + PMUGRF_PVTM_CON0,
		      BITS_WITH_WMASK(0, 0x3, pgrf_pvtm_st));
	dsb();
	mmio_write_32(PMUGRF_BASE + PMUGRF_PVTM_CON0,
		      BITS_WITH_WMASK(1, 0x1, pgrf_pvtm_en));
	dsb();
	mmio_write_32(PMUGRF_BASE + PMUGRF_PVTM_CON1, PVTM_CALC_CNT);
	dsb();

	mmio_write_32(PMUGRF_BASE + PMUGRF_PVTM_CON0,
		      BITS_WITH_WMASK(1, 0x1, pgrf_pvtm_st));

	/* pmugrf_pvtm_st0 will be clear after PVTM start,
	 * which will cost about 6 cycles of pvtm at least.
	 * So we wait 30 cycles of pvtm for security.
	 */
	while (mmio_read_32(PMUGRF_BASE + PMUGRF_PVTM_ST1) < 30)
		;

	dsb();
	while (!(mmio_read_32(PMUGRF_BASE + PMUGRF_PVTM_ST0) & 0x1))
		;

	pvtm_freq_khz =
		(mmio_read_32(PMUGRF_BASE + PMUGRF_PVTM_ST1) * 24000 +
		PVTM_CALC_CNT / 2) / PVTM_CALC_CNT;
	pvtm_div = (pvtm_freq_khz + 16) / 32;

	/* pvtm_div = div_factor << 2 + 1,
	 * so div_factor = (pvtm_div - 1) >> 2.
	 * But the operation ">> 2" will clear the low bit of pvtm_div,
	 * so we don't have to do "- 1" for compasation
	 */
	pvtm_div = pvtm_div >> 2;
	if (pvtm_div > 0x3f)
		pvtm_div = 0x3f;

	mmio_write_32(PMUGRF_BASE + PMUGRF_PVTM_CON0,
		      BITS_WITH_WMASK(pvtm_div, 0x3f, pgrf_pvtm_div));

	/* select pvtm as 32k source */
	mmio_write_32(PMUCRU_BASE + CRU_PMU_CLKSELS_CON(0),
630
		      BITS_WITH_WMASK(1, 0x3U, 14));
XiaoDong Huang's avatar
XiaoDong Huang committed
631
632
633
634
635
}

static void pvtm_32k_config_restore(void)
{
	mmio_write_32(PMUCRU_BASE + CRU_PMU_CLKSELS_CON(0),
636
		      ddr_data.pmu_cru_clksel_con0 | BITS_WMSK(0x3U, 14));
XiaoDong Huang's avatar
XiaoDong Huang committed
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872

	mmio_write_32(PMUGRF_BASE + PMUGRF_PVTM_CON0,
		      WITH_16BITS_WMSK(ddr_data.pgrf_pvtm_con[0]));
	mmio_write_32(PMUGRF_BASE + PMUGRF_PVTM_CON1,
		      ddr_data.pgrf_pvtm_con[1]);
}

static void ddr_sleep_config(void)
{
	/* disable ddr pd, sr */
	ddr_data.ddrc_pwrctrl = mmio_read_32(DDR_UPCTL_BASE + 0x30);
	mmio_write_32(DDR_UPCTL_BASE + 0x30, BITS_WITH_WMASK(0x0, 0x3, 0));

	/* disable ddr auto gt */
	ddr_data.ddrgrf_con1 = mmio_read_32(DDRGRF_BASE + 0x4);
	mmio_write_32(DDRGRF_BASE + 0x4, BITS_WITH_WMASK(0x0, 0x1f, 0));

	/* disable ddr standby */
	ddr_data.ddrstdby_con0 = mmio_read_32(DDR_STDBY_BASE + 0x0);
	mmio_write_32(DDR_STDBY_BASE + 0x0, BITS_WITH_WMASK(0x0, 0x1, 0));
	while ((mmio_read_32(DDR_UPCTL_BASE + 0x4) & 0x7) != 1)
		;

	/* ddr pmu ctrl */
	ddr_data.ddrgrf_con0 = mmio_read_32(DDRGRF_BASE + 0x0);
	mmio_write_32(DDRGRF_BASE + 0x0, BITS_WITH_WMASK(0x0, 0x1, 5));
	dsb();
	mmio_write_32(DDRGRF_BASE + 0x0, BITS_WITH_WMASK(0x1, 0x1, 4));

	/* ddr ret sel */
	ddr_data.pmugrf_soc_con0 =
		mmio_read_32(PMUGRF_BASE + PMUGRF_SOC_CON(0));
	mmio_write_32(PMUGRF_BASE + PMUGRF_SOC_CON(0),
		      BITS_WITH_WMASK(0x0, 0x1, 12));
}

static void ddr_sleep_config_restore(void)
{
	/* restore ddr ret sel */
	mmio_write_32(PMUGRF_BASE + PMUGRF_SOC_CON(0),
		      ddr_data.pmugrf_soc_con0 | BITS_WMSK(0x1, 12));

	/* restore ddr pmu ctrl */
	mmio_write_32(DDRGRF_BASE + 0x0,
		      ddr_data.ddrgrf_con0 | BITS_WMSK(0x1, 4));
	dsb();
	mmio_write_32(DDRGRF_BASE + 0x0,
		      ddr_data.ddrgrf_con0 | BITS_WMSK(0x1, 5));

	/* restore ddr standby */
	mmio_write_32(DDR_STDBY_BASE + 0x0,
		      ddr_data.ddrstdby_con0 | BITS_WMSK(0x1, 0));

	/* restore ddr auto gt */
	mmio_write_32(DDRGRF_BASE + 0x4,
		      ddr_data.ddrgrf_con1 | BITS_WMSK(0x1f, 0));

	/* restore ddr pd, sr */
	mmio_write_32(DDR_UPCTL_BASE + 0x30,
		      ddr_data.ddrc_pwrctrl | BITS_WMSK(0x3, 0));
}

static void pmu_sleep_config(void)
{
	uint32_t pwrmd_core_lo, pwrmd_core_hi, pwrmd_com_lo, pwrmd_com_hi;
	uint32_t pmu_wkup_cfg2_lo;
	uint32_t clk_freq_khz;

	/* save pmic_sleep iomux gpio0_a4 */
	ddr_data.pmic_slp_iomux = mmio_read_32(PMUGRF_BASE + GPIO0A_IOMUX);

	ddr_data.pmu_pwrmd_core_l =
			mmio_read_32(PMU_BASE + PMU_PWRMODE_CORE_LO);
	ddr_data.pmu_pwrmd_core_h =
			mmio_read_32(PMU_BASE + PMU_PWRMODE_CORE_HI);
	ddr_data.pmu_pwrmd_cmm_l =
			mmio_read_32(PMU_BASE + PMU_PWRMODE_COMMON_CON_LO);
	ddr_data.pmu_pwrmd_cmm_h =
			mmio_read_32(PMU_BASE + PMU_PWRMODE_COMMON_CON_HI);
	ddr_data.pmu_wkup_cfg2_l = mmio_read_32(PMU_BASE + PMU_WKUP_CFG2_LO);

	pwrmd_core_lo = BIT(pmu_global_int_dis) |
			BIT(pmu_core_src_gt) |
			BIT(pmu_cpu0_pd) |
			BIT(pmu_clr_core) |
			BIT(pmu_scu_pd) |
			BIT(pmu_l2_idle) |
			BIT(pmu_l2_flush) |
			BIT(pmu_clr_bus2main) |
			BIT(pmu_clr_peri2msch);

	pwrmd_core_hi = BIT(pmu_dpll_pd_en) |
			BIT(pmu_apll_pd_en) |
			BIT(pmu_cpll_pd_en) |
			BIT(pmu_gpll_pd_en) |
			BIT(pmu_npll_pd_en);

	pwrmd_com_lo = BIT(pmu_mode_en) |
		       BIT(pmu_pll_pd) |
		       BIT(pmu_pmu_use_if) |
		       BIT(pmu_alive_use_if) |
		       BIT(pmu_osc_dis) |
		       BIT(pmu_sref_enter) |
		       BIT(pmu_ddrc_gt) |
		       BIT(pmu_clr_pmu) |
		       BIT(pmu_clr_peri_pmu);

	pwrmd_com_hi = BIT(pmu_clr_bus) |
		       BIT(pmu_clr_msch) |
		       BIT(pmu_wakeup_begin_cfg);

	pmu_wkup_cfg2_lo = BIT(pmu_cluster_wkup_en) |
			   BIT(pmu_gpio_wkup_en) |
			   BIT(pmu_timer_wkup_en);

	/* set pmic_sleep iomux gpio0_a4 */
	mmio_write_32(PMUGRF_BASE + GPIO0A_IOMUX,
		      BITS_WITH_WMASK(1, 0x3, 8));

	clk_freq_khz = 32;

	mmio_write_32(PMU_BASE + PMU_OSC_CNT_LO,
		      WITH_16BITS_WMSK(clk_freq_khz * 32 & 0xffff));
	mmio_write_32(PMU_BASE + PMU_OSC_CNT_HI,
		      WITH_16BITS_WMSK(clk_freq_khz * 32 >> 16));

	mmio_write_32(PMU_BASE + PMU_STABLE_CNT_LO,
		      WITH_16BITS_WMSK(clk_freq_khz * 32 & 0xffff));
	mmio_write_32(PMU_BASE + PMU_STABLE_CNT_HI,
		      WITH_16BITS_WMSK(clk_freq_khz * 32 >> 16));

	mmio_write_32(PMU_BASE + PMU_WAKEUP_RST_CLR_LO,
		      WITH_16BITS_WMSK(clk_freq_khz * 2 & 0xffff));
	mmio_write_32(PMU_BASE + PMU_WAKEUP_RST_CLR_HI,
		      WITH_16BITS_WMSK(clk_freq_khz * 2 >> 16));

	/* Pmu's clk has switched to 24M back When pmu FSM counts
	 * the follow counters, so we should use 24M to calculate
	 * these counters.
	 */
	mmio_write_32(PMU_BASE + PMU_SCU_PWRDN_CNT_LO,
		      WITH_16BITS_WMSK(24000 * 2 & 0xffff));
	mmio_write_32(PMU_BASE + PMU_SCU_PWRDN_CNT_HI,
		      WITH_16BITS_WMSK(24000 * 2 >> 16));

	mmio_write_32(PMU_BASE + PMU_SCU_PWRUP_CNT_LO,
		      WITH_16BITS_WMSK(24000 * 2 & 0xffff));
	mmio_write_32(PMU_BASE + PMU_SCU_PWRUP_CNT_HI,
		      WITH_16BITS_WMSK(24000 * 2 >> 16));

	mmio_write_32(PMU_BASE + PMU_PLLLOCK_CNT_LO,
		      WITH_16BITS_WMSK(24000 * 5 & 0xffff));
	mmio_write_32(PMU_BASE + PMU_PLLLOCK_CNT_HI,
		      WITH_16BITS_WMSK(24000 * 5 >> 16));

	mmio_write_32(PMU_BASE + PMU_PLLRST_CNT_LO,
		      WITH_16BITS_WMSK(24000 * 2 & 0xffff));
	mmio_write_32(PMU_BASE + PMU_PLLRST_CNT_HI,
		      WITH_16BITS_WMSK(24000 * 2 >> 16));

	/* Config pmu power mode and pmu wakeup source */
	mmio_write_32(PMU_BASE + PMU_PWRMODE_CORE_LO,
		      WITH_16BITS_WMSK(pwrmd_core_lo));
	mmio_write_32(PMU_BASE + PMU_PWRMODE_CORE_HI,
		      WITH_16BITS_WMSK(pwrmd_core_hi));

	mmio_write_32(PMU_BASE + PMU_PWRMODE_COMMON_CON_LO,
		      WITH_16BITS_WMSK(pwrmd_com_lo));
	mmio_write_32(PMU_BASE + PMU_PWRMODE_COMMON_CON_HI,
		      WITH_16BITS_WMSK(pwrmd_com_hi));

	mmio_write_32(PMU_BASE + PMU_WKUP_CFG2_LO,
		      WITH_16BITS_WMSK(pmu_wkup_cfg2_lo));
}

static void pmu_sleep_restore(void)
{
	mmio_write_32(PMU_BASE + PMU_PWRMODE_CORE_LO,
		      WITH_16BITS_WMSK(ddr_data.pmu_pwrmd_core_l));
	mmio_write_32(PMU_BASE + PMU_PWRMODE_CORE_HI,
		      WITH_16BITS_WMSK(ddr_data.pmu_pwrmd_core_h));
	mmio_write_32(PMU_BASE + PMU_PWRMODE_COMMON_CON_LO,
		      WITH_16BITS_WMSK(ddr_data.pmu_pwrmd_cmm_l));
	mmio_write_32(PMU_BASE + PMU_PWRMODE_COMMON_CON_HI,
		      WITH_16BITS_WMSK(ddr_data.pmu_pwrmd_cmm_h));
	mmio_write_32(PMU_BASE + PMU_WKUP_CFG2_LO,
		      WITH_16BITS_WMSK(ddr_data.pmu_wkup_cfg2_l));

	/* restore pmic_sleep iomux */
	mmio_write_32(PMUGRF_BASE + GPIO0A_IOMUX,
		      WITH_16BITS_WMSK(ddr_data.pmic_slp_iomux));
}

static void soc_sleep_config(void)
{
	ddr_data.gpio0c_iomux = mmio_read_32(PMUGRF_BASE + GPIO0C_IOMUX);

	pmu_sleep_config();

	ddr_sleep_config();

	pvtm_32k_config();
}

static void soc_sleep_restore(void)
{
	secure_timer_init();

	pvtm_32k_config_restore();

	ddr_sleep_config_restore();

	pmu_sleep_restore();

	mmio_write_32(PMUGRF_BASE + GPIO0C_IOMUX,
		      WITH_16BITS_WMSK(ddr_data.gpio0c_iomux));
}

static inline void pm_pll_wait_lock(uint32_t pll_base, uint32_t pll_id)
{
	uint32_t delay = PLL_LOCKED_TIMEOUT;

	while (delay > 0) {
		if (mmio_read_32(pll_base + PLL_CON(1)) &
		    PLL_LOCK_MSK)
			break;
		delay--;
	}

	if (delay == 0)
		ERROR("Can't wait pll:%d lock\n", pll_id);
}

static inline void pll_pwr_ctr(uint32_t pll_base, uint32_t pll_id, uint32_t pd)
{
	mmio_write_32(pll_base + PLL_CON(1),
873
		      BITS_WITH_WMASK(1, 1U, 15));
XiaoDong Huang's avatar
XiaoDong Huang committed
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
	if (pd)
		mmio_write_32(pll_base + PLL_CON(1),
			      BITS_WITH_WMASK(1, 1, 14));
	else
		mmio_write_32(pll_base + PLL_CON(1),
			      BITS_WITH_WMASK(0, 1, 14));
}

static inline void pll_set_mode(uint32_t pll_id, uint32_t mode)
{
	uint32_t val = BITS_WITH_WMASK(mode, 0x3, PLL_MODE_SHIFT(pll_id));

	if (pll_id != GPLL_ID)
		mmio_write_32(CRU_BASE + CRU_MODE, val);
	else
		mmio_write_32(PMUCRU_BASE + CRU_PMU_MODE,
			      BITS_WITH_WMASK(mode, 0x3, 0));
}

static inline void pll_suspend(uint32_t pll_id)
{
	int i;
	uint32_t pll_base;

	if (pll_id != GPLL_ID)
		pll_base = CRU_BASE + CRU_PLL_CONS(pll_id, 0);
	else
		pll_base = PMUCRU_BASE + CRU_PLL_CONS(0, 0);

	/* save pll con */
	for (i = 0; i < PLL_CON_CNT; i++)
		ddr_data.cru_plls_con_save[pll_id][i] =
				mmio_read_32(pll_base + PLL_CON(i));

	/* slow mode */
	pll_set_mode(pll_id, SLOW_MODE);
}

static inline void pll_resume(uint32_t pll_id)
{
	uint32_t mode, pll_base;

	if (pll_id != GPLL_ID) {
		pll_base = CRU_BASE + CRU_PLL_CONS(pll_id, 0);
		mode = (ddr_data.cru_mode_save >> PLL_MODE_SHIFT(pll_id)) & 0x3;
	} else {
		pll_base = PMUCRU_BASE + CRU_PLL_CONS(0, 0);
		mode = ddr_data.cru_pmu_mode_save & 0x3;
	}

	/* if pll locked before suspend, we should wait atfer resume */
	if (ddr_data.cru_plls_con_save[pll_id][1] & PLL_LOCK_MSK)
		pm_pll_wait_lock(pll_base, pll_id);

	pll_set_mode(pll_id, mode);
}

static void pm_plls_suspend(void)
{
	ddr_data.cru_mode_save = mmio_read_32(CRU_BASE + CRU_MODE);
	ddr_data.cru_pmu_mode_save = mmio_read_32(PMUCRU_BASE + CRU_PMU_MODE);
	ddr_data.clk_sel0 = mmio_read_32(CRU_BASE + CRU_CLKSELS_CON(0));

	pll_suspend(GPLL_ID);
	pll_suspend(NPLL_ID);
	pll_suspend(CPLL_ID);
	pll_suspend(APLL_ID);

	/* core */
	mmio_write_32(CRU_BASE + CRU_CLKSELS_CON(0),
		      BITS_WITH_WMASK(0, 0xf, 0));

	/* pclk_dbg */
	mmio_write_32(CRU_BASE + CRU_CLKSELS_CON(0),
		      BITS_WITH_WMASK(0, 0xf, 8));
}

static void pm_plls_resume(void)
{
	/* pclk_dbg */
	mmio_write_32(CRU_BASE + CRU_CLKSELS_CON(0),
		      ddr_data.clk_sel0 | BITS_WMSK(0xf, 8));

	/* core */
	mmio_write_32(CRU_BASE + CRU_CLKSELS_CON(0),
		      ddr_data.clk_sel0 | BITS_WMSK(0xf, 0));

	pll_resume(APLL_ID);
	pll_resume(CPLL_ID);
	pll_resume(NPLL_ID);
	pll_resume(GPLL_ID);
}

int rockchip_soc_sys_pwr_dm_suspend(void)
{
	pmu_power_domains_suspend();

	clk_gate_suspend();

	soc_sleep_config();

	pm_plls_suspend();

	psram_boot_cfg->pm_flag &= ~PM_WARM_BOOT_BIT;

	return 0;
}

int rockchip_soc_sys_pwr_dm_resume(void)
{
	psram_boot_cfg->pm_flag |= PM_WARM_BOOT_BIT;

	pm_plls_resume();

	soc_sleep_restore();

	clk_gate_resume();

	pmu_power_domains_resume();

	plat_rockchip_gic_cpuif_enable();

	return 0;
}

void __dead2 rockchip_soc_soft_reset(void)
{
	pll_set_mode(GPLL_ID, SLOW_MODE);
	pll_set_mode(CPLL_ID, SLOW_MODE);
	pll_set_mode(NPLL_ID, SLOW_MODE);
	pll_set_mode(APLL_ID, SLOW_MODE);
	dsb();

	mmio_write_32(CRU_BASE + CRU_GLB_SRST_FST, CRU_GLB_SRST_FST_VALUE);
	dsb();

	/*
	 * Maybe the HW needs some times to reset the system,
	 * so we do not hope the core to execute valid codes.
	 */
	psci_power_down_wfi();
}

void __dead2 rockchip_soc_system_off(void)
{
	uint32_t val;

	/* set pmic_sleep pin(gpio0_a4) to gpio mode */
	mmio_write_32(PMUGRF_BASE + GPIO0A_IOMUX, BITS_WITH_WMASK(0, 0x3, 8));

	/* config output */
	val = mmio_read_32(GPIO0_BASE + SWPORTA_DDR);
	val |= BIT(4);
	mmio_write_32(GPIO0_BASE + SWPORTA_DDR, val);

	/* config output high level */
	val = mmio_read_32(GPIO0_BASE);
	val |= BIT(4);
	mmio_write_32(GPIO0_BASE, val);
	dsb();

	/*
	 * Maybe the HW needs some times to reset the system,
	 * so we do not hope the core to execute valid codes.
	 */
	psci_power_down_wfi();
}

void rockchip_plat_mmu_el3(void)
{
	/* TODO: support the el3 for px30 SoCs */
}

void plat_rockchip_pmu_init(void)
{
	uint32_t cpu;

	rockchip_pd_lock_init();

	for (cpu = 0; cpu < PLATFORM_CORE_COUNT; cpu++)
		cpuson_flags[cpu] = 0;

	psram_boot_cfg->ddr_func = (uint64_t)0;
	psram_boot_cfg->ddr_data = (uint64_t)0;
	psram_boot_cfg->sp = PSRAM_SP_TOP;
	psram_boot_cfg->ddr_flag = 0x0;
	psram_boot_cfg->boot_mpidr = read_mpidr_el1() & 0xffff;
	psram_boot_cfg->pm_flag = PM_WARM_BOOT_BIT;

	nonboot_cpus_off();

	/* Remap pmu_sram's base address to boot address */
	mmio_write_32(PMUSGRF_BASE + PMUSGRF_SOC_CON(0),
		      BITS_WITH_WMASK(1, 0x1, 13));

	INFO("%s: pd status %x\n",
	     __func__, mmio_read_32(PMU_BASE + PMU_PWRDN_ST));
}