1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
|
/*
* Copyright (C) 2010 Freescale Semiconductor, Inc.
* Copyright 2008 Embedded Alley Solutions, Inc All Rights Reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <linux/suspend.h>
#include <linux/rtc.h>
#include <linux/pm.h>
#include <linux/delay.h>
#include <linux/clk.h>
#include <linux/io.h>
#include <linux/irq.h>
#include <linux/kthread.h>
#include <asm/cacheflush.h>
#include <asm/mach-types.h>
#include <asm/mach/time.h>
#include <mach/hardware.h>
#include <mach/dma.h>
#include <mach/regs-rtc.h>
#include "regs-clkctrl.h"
#include <mach/regs-power.h>
#include <mach/regs-pwm.h>
#include <mach/regs-rtc.h>
#include <mach/../../regs-icoll.h>
#include "regs-dram.h"
#include "mx28_pins.h"
#include "mx28evk.h"
#include "sleep.h"
#define PENDING_IRQ_RETRY 100
static void *saved_sram;
static int saved_sleep_state;
#define WAIT_DC_OK_CYCLES 24000
#define WAIT_CYCLE(n) for (i = 0; i < n; i++);
#define LOWER_VDDIO 10
#define LOWER_VDDA 9
#define LOWER_VDDD 0xa
#define MAX_POWEROFF_CODE_SIZE (6 * 1024)
#define REGS_CLKCTRL_BASE IO_ADDRESS(CLKCTRL_PHYS_ADDR)
#define dbgc(ch) __raw_writel(ch, IO_ADDRESS(0x80074000));
inline void dbgnum(u32 num)
{
dbgc((num / 1000) + '0');
dbgc(((num%1000) / 100) + '0');
dbgc(((num%100) / 10) + '0');
dbgc((num%10) + '0');
dbgc('\n');
}
static inline void do_standby(void)
{
void (*mx28_cpu_standby_ptr) (void);
struct clk *cpu_clk;
struct clk *osc_clk;
struct clk *pll_clk;
struct clk *hbus_clk;
struct clk *cpu_parent = NULL;
int cpu_rate = 0;
int hbus_rate = 0;
u32 reg_clkctrl_clkseq, reg_clkctrl_xtal;
unsigned long iram_phy_addr;
void *iram_virtual_addr;
int wakeupirq;
mx28evk_enet_io_lowerpower_enter();
/*
* 1) switch clock domains from PLL to 24MHz
* 2) lower voltage (TODO)
* 3) switch EMI to 24MHz and turn PLL off (done in sleep.S)
*/
/* make sure SRAM copy gets physically written into SDRAM.
* SDRAM will be placed into self-refresh during power down
*/
flush_cache_all();
iram_virtual_addr = iram_alloc(MAX_POWEROFF_CODE_SIZE, &iram_phy_addr);
if (iram_virtual_addr == NULL) {
pr_info("can not get iram for suspend\n");
return;
}
/* copy suspend function into SRAM */
memcpy(iram_virtual_addr, mx28_cpu_standby, mx28_standby_alloc_sz);
/* now switch the CPU to ref_xtal */
cpu_clk = clk_get(NULL, "cpu");
osc_clk = clk_get(NULL, "ref_xtal");
pll_clk = clk_get(NULL, "pll.0");
hbus_clk = clk_get(NULL, "h");
if (!IS_ERR(cpu_clk) && !IS_ERR(osc_clk)) {
cpu_rate = clk_get_rate(cpu_clk);
cpu_parent = clk_get_parent(cpu_clk);
hbus_rate = clk_get_rate(hbus_clk);
clk_set_parent(cpu_clk, osc_clk);
} else
pr_err("fail to get cpu clk\n");
local_fiq_disable();
__raw_writel(BM_POWER_CTRL_ENIRQ_PSWITCH,
REGS_POWER_BASE + HW_POWER_CTRL_SET);
reg_clkctrl_clkseq = __raw_readl(REGS_CLKCTRL_BASE +
HW_CLKCTRL_CLKSEQ);
reg_clkctrl_xtal = __raw_readl(REGS_CLKCTRL_BASE + HW_CLKCTRL_XTAL);
/* do suspend */
mx28_cpu_standby_ptr = iram_virtual_addr;
mx28_cpu_standby_ptr();
wakeupirq = __raw_readl(IO_ADDRESS(ICOLL_PHYS_ADDR) + HW_ICOLL_STAT);
pr_info("wakeup irq = %d\n", wakeupirq);
__raw_writel(reg_clkctrl_clkseq, REGS_CLKCTRL_BASE + HW_CLKCTRL_CLKSEQ);
__raw_writel(reg_clkctrl_xtal, REGS_CLKCTRL_BASE + HW_CLKCTRL_XTAL);
saved_sleep_state = 0; /* waking from standby */
__raw_writel(BM_POWER_CTRL_PSWITCH_IRQ,
REGS_POWER_BASE + HW_POWER_CTRL_CLR);
local_fiq_enable();
if (cpu_parent) {
clk_set_parent(cpu_clk, cpu_parent);
clk_set_rate(cpu_clk, cpu_rate);
clk_set_rate(hbus_clk, hbus_rate);
}
clk_put(hbus_clk);
clk_put(pll_clk);
clk_put(osc_clk);
clk_put(cpu_clk);
iram_free(iram_phy_addr, MAX_POWEROFF_CODE_SIZE);
mx28evk_enet_io_lowerpower_exit();
}
static noinline void do_mem(void)
{
/* TODO */
}
static int mx28_pm_enter(suspend_state_t state)
{
switch (state) {
case PM_SUSPEND_STANDBY:
do_standby();
break;
case PM_SUSPEND_MEM:
do_mem();
break;
}
return 0;
}
static int mx28_pm_valid(suspend_state_t state)
{
return (state == PM_SUSPEND_STANDBY) ||
(state == PM_SUSPEND_MEM);
}
static suspend_state_t saved_state;
static int mx28_pm_begin(suspend_state_t state)
{
saved_state = state;
return 0;
}
static void mx28_pm_end(void)
{
/*XXX: Nothing to do */
}
suspend_state_t mx28_pm_get_target(void)
{
return saved_state;
}
EXPORT_SYMBOL(mx28_pm_get_target);
/**
* mx28_pm_get_sleep_state - get sleep state we waking from
*
* returns boolean: 0 if waking up from standby, 1 otherwise
*/
int mx28_pm_sleep_was_deep(void)
{
return saved_sleep_state;
}
EXPORT_SYMBOL(mx28_pm_sleep_was_deep);
static struct platform_suspend_ops mx28_suspend_ops = {
.enter = mx28_pm_enter,
.valid = mx28_pm_valid,
.begin = mx28_pm_begin,
.end = mx28_pm_end,
};
void mx28_pm_idle(void)
{
local_irq_disable();
local_fiq_disable();
if (need_resched()) {
local_fiq_enable();
local_irq_enable();
return;
}
__raw_writel(BM_CLKCTRL_CPU_INTERRUPT_WAIT,
REGS_CLKCTRL_BASE + HW_CLKCTRL_CPU_SET);
__asm__ __volatile__ ("mcr p15, 0, r0, c7, c0, 4");
local_fiq_enable();
local_irq_enable();
}
static void mx28_pm_power_off(void)
{
__raw_writel(BF_POWER_RESET_UNLOCK(0x3e77) | BM_POWER_RESET_PWD,
REGS_POWER_BASE + HW_POWER_RESET);
}
struct mx28_pswitch_state {
int dev_running;
};
static DECLARE_COMPLETION(suspend_request);
static int suspend_thread_fn(void *data)
{
while (1) {
wait_for_completion_interruptible(&suspend_request);
pm_suspend(PM_SUSPEND_STANDBY);
}
return 0;
}
static struct mx28_pswitch_state pswitch_state = {
.dev_running = 0,
};
#define PSWITCH_POWER_DOWN_DELAY 30
static struct delayed_work pswitch_work;
static void pswitch_check_work(struct work_struct *work)
{
int pin_value, i;
for (i = 0; i < PSWITCH_POWER_DOWN_DELAY; i++) {
pin_value = __raw_readl(REGS_POWER_BASE + HW_POWER_STS) &
BF_POWER_STS_PSWITCH(0x1);
if (pin_value == 0)
break;
msleep(100);
}
if (i < PSWITCH_POWER_DOWN_DELAY) {
pr_info("pswitch goto suspend\n");
complete(&suspend_request);
} else {
pr_info("release pswitch to power down\n");
for (i = 0; i < 500; i++) {
pin_value = __raw_readl(REGS_POWER_BASE + HW_POWER_STS)
& BF_POWER_STS_PSWITCH(0x1);
if (pin_value == 0)
break;
msleep(10);
}
pr_info("pswitch power down\n");
mx28_pm_power_off();
}
__raw_writel(BM_POWER_CTRL_PSWITCH_IRQ,
REGS_POWER_BASE + HW_POWER_CTRL_CLR);
__raw_writel(BM_POWER_CTRL_ENIRQ_PSWITCH,
REGS_POWER_BASE + HW_POWER_CTRL_SET);
__raw_writel(BM_POWER_CTRL_PSWITCH_IRQ,
REGS_POWER_BASE + HW_POWER_CTRL_CLR);
}
static irqreturn_t pswitch_interrupt(int irq, void *dev)
{
/* check if irq by pswitch */
if (!(__raw_readl(REGS_POWER_BASE + HW_POWER_CTRL) &
BM_POWER_CTRL_PSWITCH_IRQ))
return IRQ_HANDLED;
__raw_writel(BM_POWER_CTRL_ENIRQ_PSWITCH,
REGS_POWER_BASE + HW_POWER_CTRL_CLR);
schedule_delayed_work(&pswitch_work, 1);
return IRQ_HANDLED;
}
static struct irqaction pswitch_irq = {
.name = "pswitch",
.flags = IRQF_DISABLED | IRQF_SHARED,
.handler = pswitch_interrupt,
.dev_id = &pswitch_state,
};
static void init_pswitch(void)
{
INIT_DELAYED_WORK(&pswitch_work, pswitch_check_work);
kthread_run(suspend_thread_fn, NULL, "pswitch");
__raw_writel(BM_POWER_CTRL_PSWITCH_IRQ,
REGS_POWER_BASE + HW_POWER_CTRL_CLR);
__raw_writel(BM_POWER_CTRL_POLARITY_PSWITCH |
BM_POWER_CTRL_ENIRQ_PSWITCH,
REGS_POWER_BASE + HW_POWER_CTRL_SET);
__raw_writel(BM_POWER_CTRL_PSWITCH_IRQ,
REGS_POWER_BASE + HW_POWER_CTRL_CLR);
setup_irq(IRQ_VDD5V, &pswitch_irq);
}
static int __init mx28_pm_init(void)
{
saved_sram = kmalloc(0x4000, GFP_ATOMIC);
if (!saved_sram) {
printk(KERN_ERR
"PM Suspend: can't allocate memory to save portion of SRAM\n");
return -ENOMEM;
}
pm_power_off = mx28_pm_power_off;
pm_idle = mx28_pm_idle;
suspend_set_ops(&mx28_suspend_ops);
init_pswitch();
return 0;
}
late_initcall(mx28_pm_init);
|