1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * linux/arch/unicore32/kernel/pm.c
4  *
5  * Code specific to PKUnity SoC and UniCore ISA
6  *
7  *	Maintained by GUAN Xue-tao <gxt@mprc.pku.edu.cn>
8  *	Copyright (C) 2001-2010 Guan Xuetao
9  */
10 #include <linux/init.h>
11 #include <linux/module.h>
12 #include <linux/suspend.h>
13 #include <linux/errno.h>
14 #include <linux/slab.h>
15 #include <linux/io.h>
16 
17 #include <mach/hardware.h>
18 #include <mach/pm.h>
19 
20 #include "setup.h"
21 
22 struct puv3_cpu_pm_fns *puv3_cpu_pm_fns;
23 static unsigned long *sleep_save;
24 
puv3_pm_enter(suspend_state_t state)25 int puv3_pm_enter(suspend_state_t state)
26 {
27 	unsigned long sleep_save_checksum = 0, checksum = 0;
28 	int i;
29 
30 	/* skip registers saving for standby */
31 	if (state != PM_SUSPEND_STANDBY) {
32 		puv3_cpu_pm_fns->save(sleep_save);
33 		/* before sleeping, calculate and save a checksum */
34 		for (i = 0; i < puv3_cpu_pm_fns->save_count - 1; i++)
35 			sleep_save_checksum += sleep_save[i];
36 	}
37 
38 	/* *** go zzz *** */
39 	puv3_cpu_pm_fns->enter(state);
40 	cpu_init();
41 #ifdef CONFIG_INPUT_KEYBOARD
42 	puv3_ps2_init();
43 #endif
44 #ifdef CONFIG_PCI
45 	pci_puv3_preinit();
46 #endif
47 	if (state != PM_SUSPEND_STANDBY) {
48 		/* after sleeping, validate the checksum */
49 		for (i = 0; i < puv3_cpu_pm_fns->save_count - 1; i++)
50 			checksum += sleep_save[i];
51 
52 		/* if invalid, display message and wait for a hardware reset */
53 		if (checksum != sleep_save_checksum) {
54 			while (1)
55 				puv3_cpu_pm_fns->enter(state);
56 		}
57 		puv3_cpu_pm_fns->restore(sleep_save);
58 	}
59 
60 	pr_debug("*** made it back from resume\n");
61 
62 	return 0;
63 }
64 EXPORT_SYMBOL_GPL(puv3_pm_enter);
65 
sleep_phys_sp(void * sp)66 unsigned long sleep_phys_sp(void *sp)
67 {
68 	return virt_to_phys(sp);
69 }
70 
puv3_pm_valid(suspend_state_t state)71 static int puv3_pm_valid(suspend_state_t state)
72 {
73 	if (puv3_cpu_pm_fns)
74 		return puv3_cpu_pm_fns->valid(state);
75 
76 	return -EINVAL;
77 }
78 
puv3_pm_prepare(void)79 static int puv3_pm_prepare(void)
80 {
81 	int ret = 0;
82 
83 	if (puv3_cpu_pm_fns && puv3_cpu_pm_fns->prepare)
84 		ret = puv3_cpu_pm_fns->prepare();
85 
86 	return ret;
87 }
88 
puv3_pm_finish(void)89 static void puv3_pm_finish(void)
90 {
91 	if (puv3_cpu_pm_fns && puv3_cpu_pm_fns->finish)
92 		puv3_cpu_pm_fns->finish();
93 }
94 
95 static struct platform_suspend_ops puv3_pm_ops = {
96 	.valid		= puv3_pm_valid,
97 	.enter		= puv3_pm_enter,
98 	.prepare	= puv3_pm_prepare,
99 	.finish		= puv3_pm_finish,
100 };
101 
puv3_pm_init(void)102 static int __init puv3_pm_init(void)
103 {
104 	if (!puv3_cpu_pm_fns) {
105 		printk(KERN_ERR "no valid puv3_cpu_pm_fns defined\n");
106 		return -EINVAL;
107 	}
108 
109 	sleep_save = kmalloc_array(puv3_cpu_pm_fns->save_count,
110 				   sizeof(unsigned long),
111 				   GFP_KERNEL);
112 	if (!sleep_save) {
113 		printk(KERN_ERR "failed to alloc memory for pm save\n");
114 		return -ENOMEM;
115 	}
116 
117 	suspend_set_ops(&puv3_pm_ops);
118 	return 0;
119 }
120 
121 device_initcall(puv3_pm_init);
122