1 /*
2  * PXA250/210 Power Management Routines
3  *
4  * Original code for the SA11x0:
5  * Copyright (c) 2001 Cliff Brake <cbrake@accelent.com>
6  *
7  * Modified for the PXA250 by Nicolas Pitre:
8  * Copyright (c) 2002 Monta Vista Software, Inc.
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License.
12  */
13 #include <linux/init.h>
14 #include <linux/module.h>
15 #include <linux/suspend.h>
16 #include <linux/errno.h>
17 #include <linux/slab.h>
18 
19 #include <mach/pm.h>
20 
21 struct pxa_cpu_pm_fns *pxa_cpu_pm_fns;
22 static unsigned long *sleep_save;
23 
pxa_pm_enter(suspend_state_t state)24 int pxa_pm_enter(suspend_state_t state)
25 {
26 	unsigned long sleep_save_checksum = 0, checksum = 0;
27 	int i;
28 
29 #ifdef CONFIG_IWMMXT
30 	/* force any iWMMXt context to ram **/
31 	if (elf_hwcap & HWCAP_IWMMXT)
32 		iwmmxt_task_disable(NULL);
33 #endif
34 
35 	/* skip registers saving for standby */
36 	if (state != PM_SUSPEND_STANDBY && pxa_cpu_pm_fns->save) {
37 		pxa_cpu_pm_fns->save(sleep_save);
38 		/* before sleeping, calculate and save a checksum */
39 		for (i = 0; i < pxa_cpu_pm_fns->save_count - 1; i++)
40 			sleep_save_checksum += sleep_save[i];
41 	}
42 
43 	/* *** go zzz *** */
44 	pxa_cpu_pm_fns->enter(state);
45 	cpu_init();
46 
47 	if (state != PM_SUSPEND_STANDBY && pxa_cpu_pm_fns->restore) {
48 		/* after sleeping, validate the checksum */
49 		for (i = 0; i < pxa_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 
55 			lubbock_set_hexled(0xbadbadc5);
56 
57 			while (1)
58 				pxa_cpu_pm_fns->enter(state);
59 		}
60 		pxa_cpu_pm_fns->restore(sleep_save);
61 	}
62 
63 	pr_debug("*** made it back from resume\n");
64 
65 	return 0;
66 }
67 
68 EXPORT_SYMBOL_GPL(pxa_pm_enter);
69 
pxa_pm_valid(suspend_state_t state)70 static int pxa_pm_valid(suspend_state_t state)
71 {
72 	if (pxa_cpu_pm_fns)
73 		return pxa_cpu_pm_fns->valid(state);
74 
75 	return -EINVAL;
76 }
77 
pxa_pm_prepare(void)78 int pxa_pm_prepare(void)
79 {
80 	int ret = 0;
81 
82 	if (pxa_cpu_pm_fns && pxa_cpu_pm_fns->prepare)
83 		ret = pxa_cpu_pm_fns->prepare();
84 
85 	return ret;
86 }
87 
pxa_pm_finish(void)88 void pxa_pm_finish(void)
89 {
90 	if (pxa_cpu_pm_fns && pxa_cpu_pm_fns->finish)
91 		pxa_cpu_pm_fns->finish();
92 }
93 
94 static const struct platform_suspend_ops pxa_pm_ops = {
95 	.valid		= pxa_pm_valid,
96 	.enter		= pxa_pm_enter,
97 	.prepare	= pxa_pm_prepare,
98 	.finish		= pxa_pm_finish,
99 };
100 
pxa_pm_init(void)101 static int __init pxa_pm_init(void)
102 {
103 	if (!pxa_cpu_pm_fns) {
104 		printk(KERN_ERR "no valid pxa_cpu_pm_fns defined\n");
105 		return -EINVAL;
106 	}
107 
108 	sleep_save = kmalloc(pxa_cpu_pm_fns->save_count * sizeof(unsigned long),
109 			     GFP_KERNEL);
110 	if (!sleep_save) {
111 		printk(KERN_ERR "failed to alloc memory for pm save\n");
112 		return -ENOMEM;
113 	}
114 
115 	suspend_set_ops(&pxa_pm_ops);
116 	return 0;
117 }
118 
119 device_initcall(pxa_pm_init);
120