Merge branch 'linux-tegra-2.6.36' into android-tegra-2.6.36
[firefly-linux-kernel-4.4.55.git] / arch / arm / mach-tegra / suspend.c
1 /*
2  * arch/arm/mach-tegra/suspend.c
3  *
4  * CPU complex suspend & resume functions for Tegra SoCs
5  *
6  * Copyright (c) 2009-2010, NVIDIA Corporation.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
16  * more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21  */
22
23 #include <linux/kernel.h>
24 #include <linux/init.h>
25 #include <linux/io.h>
26 #include <linux/sched.h>
27 #include <linux/smp.h>
28 #include <linux/irq.h>
29 #include <linux/interrupt.h>
30 #include <linux/clk.h>
31 #include <linux/err.h>
32 #include <linux/debugfs.h>
33 #include <linux/delay.h>
34 #include <linux/suspend.h>
35 #include <linux/slab.h>
36 #include <linux/serial_reg.h>
37 #include <linux/seq_file.h>
38 #include <linux/uaccess.h>
39
40 #include <linux/regulator/machine.h>
41
42 #include <asm/cacheflush.h>
43 #include <asm/hardware/cache-l2x0.h>
44 #include <asm/hardware/gic.h>
45 #include <asm/localtimer.h>
46 #include <asm/pgalloc.h>
47 #include <asm/tlbflush.h>
48
49 #include <mach/clk.h>
50 #include <mach/iomap.h>
51 #include <mach/iovmm.h>
52 #include <mach/irqs.h>
53 #include <mach/legacy_irq.h>
54 #include <mach/suspend.h>
55
56 #include "board.h"
57 #include "power.h"
58
59 struct suspend_context {
60         /*
61          * The next 7 values are referenced by offset in __restart_plls
62          * in headsmp-t2.S, and should not be moved
63          */
64         u32 pllx_misc;
65         u32 pllx_base;
66         u32 pllp_misc;
67         u32 pllp_base;
68         u32 pllp_outa;
69         u32 pllp_outb;
70         u32 pll_timeout;
71
72         u32 cpu_burst;
73         u32 clk_csite_src;
74         u32 twd_ctrl;
75         u32 twd_load;
76         u32 cclk_divider;
77 };
78
79 volatile struct suspend_context tegra_sctx;
80
81 static void __iomem *pmc = IO_ADDRESS(TEGRA_PMC_BASE);
82 static void __iomem *clk_rst = IO_ADDRESS(TEGRA_CLK_RESET_BASE);
83 static void __iomem *flow_ctrl = IO_ADDRESS(TEGRA_FLOW_CTRL_BASE);
84 static void __iomem *evp_reset = IO_ADDRESS(TEGRA_EXCEPTION_VECTORS_BASE)+0x100;
85 static void __iomem *tmrus = IO_ADDRESS(TEGRA_TMRUS_BASE);
86
87 #define PMC_CTRL                0x0
88 #define PMC_CTRL_LATCH_WAKEUPS  (1 << 5)
89 #define PMC_WAKE_MASK           0xc
90 #define PMC_WAKE_LEVEL          0x10
91 #define PMC_DPAD_ORIDE          0x1C
92 #define PMC_WAKE_DELAY          0xe0
93 #define PMC_DPD_SAMPLE          0x20
94
95 #define PMC_WAKE_STATUS         0x14
96 #define PMC_SW_WAKE_STATUS      0x18
97 #define PMC_COREPWRGOOD_TIMER   0x3c
98 #define PMC_SCRATCH0            0x50
99 #define PMC_SCRATCH1            0x54
100 #define PMC_CPUPWRGOOD_TIMER    0xc8
101 #define PMC_CPUPWROFF_TIMER     0xcc
102 #define PMC_COREPWROFF_TIMER    PMC_WAKE_DELAY
103 #define PMC_SCRATCH38           0x134
104 #define PMC_SCRATCH39           0x138
105 #define PMC_SCRATCH41           0x140
106
107 #define CLK_RESET_CCLK_BURST    0x20
108 #define CLK_RESET_CCLK_DIVIDER  0x24
109 #define CLK_RESET_PLLC_BASE     0x80
110 #define CLK_RESET_PLLM_BASE     0x90
111 #define CLK_RESET_PLLX_BASE     0xe0
112 #define CLK_RESET_PLLX_MISC     0xe4
113 #define CLK_RESET_PLLP_BASE     0xa0
114 #define CLK_RESET_PLLP_OUTA     0xa4
115 #define CLK_RESET_PLLP_OUTB     0xa8
116 #define CLK_RESET_PLLP_MISC     0xac
117
118 #define CLK_RESET_SOURCE_CSITE  0x1d4
119
120
121 #define CLK_RESET_CCLK_BURST_POLICY_SHIFT 28
122 #define CLK_RESET_CCLK_BURST_POLICY_PLLM   3
123 #define CLK_RESET_CCLK_BURST_POLICY_PLLX   8
124
125 #define FLOW_CTRL_CPU_CSR       0x8
126 #define FLOW_CTRL_CPU1_CSR      0x18
127
128 unsigned long tegra_pgd_phys;  /* pgd used by hotplug & LP2 bootup */
129 static pgd_t *tegra_pgd;
130 void *tegra_context_area = NULL;
131
132 static struct clk *tegra_pclk = NULL;
133 static const struct tegra_suspend_platform_data *pdata = NULL;
134 static unsigned long wb0_restore = 0;
135 static enum tegra_suspend_mode current_suspend_mode;
136
137 static unsigned int tegra_time_in_suspend[32];
138
139 static inline unsigned int time_to_bin(unsigned int time)
140 {
141         return fls(time);
142 }
143
144 unsigned long tegra_cpu_power_good_time(void)
145 {
146         if (WARN_ON_ONCE(!pdata))
147                 return 5000;
148
149         return pdata->cpu_timer;
150 }
151
152 unsigned long tegra_cpu_power_off_time(void)
153 {
154         if (WARN_ON_ONCE(!pdata))
155                 return 5000;
156
157         return pdata->cpu_off_timer;
158 }
159
160 enum tegra_suspend_mode tegra_get_suspend_mode(void)
161 {
162         if (!pdata)
163                 return TEGRA_SUSPEND_NONE;
164
165         return pdata->suspend_mode;
166 }
167
168 static void set_power_timers(unsigned long us_on, unsigned long us_off,
169                              long rate)
170 {
171         static int last_pclk = 0;
172         unsigned long long ticks;
173         unsigned long long pclk;
174
175         if (WARN_ON_ONCE(rate <= 0))
176                 pclk = 100000000;
177         else
178                 pclk = rate;
179
180         if (rate != last_pclk) {
181                 ticks = (us_on * pclk) + 999999ull;
182                 do_div(ticks, 1000000);
183                 writel((unsigned long)ticks, pmc + PMC_CPUPWRGOOD_TIMER);
184
185                 ticks = (us_off * pclk) + 999999ull;
186                 do_div(ticks, 1000000);
187                 writel((unsigned long)ticks, pmc + PMC_CPUPWROFF_TIMER);
188                 wmb();
189         }
190         last_pclk = pclk;
191 }
192
193 static int create_suspend_pgtable(void)
194 {
195         int i;
196         pmd_t *pmd;
197         /* arrays of virtual-to-physical mappings which must be
198          * present to safely boot hotplugged / LP2-idled CPUs.
199          * tegra_hotplug_startup (hotplug reset vector) is mapped
200          * VA=PA so that the translation post-MMU is the same as
201          * pre-MMU, IRAM is mapped VA=PA so that SDRAM self-refresh
202          * can safely disable the MMU */
203         unsigned long addr_v[] = {
204                 PHYS_OFFSET,
205                 IO_IRAM_PHYS,
206                 (unsigned long)tegra_context_area,
207 #ifdef CONFIG_HOTPLUG_CPU
208                 (unsigned long)virt_to_phys(tegra_hotplug_startup),
209 #endif
210                 (unsigned long)__cortex_a9_restore,
211                 (unsigned long)virt_to_phys(__shut_off_mmu),
212         };
213         unsigned long addr_p[] = {
214                 PHYS_OFFSET,
215                 IO_IRAM_PHYS,
216                 (unsigned long)virt_to_phys(tegra_context_area),
217 #ifdef CONFIG_HOTPLUG_CPU
218                 (unsigned long)virt_to_phys(tegra_hotplug_startup),
219 #endif
220                 (unsigned long)virt_to_phys(__cortex_a9_restore),
221                 (unsigned long)virt_to_phys(__shut_off_mmu),
222         };
223         unsigned int flags = PMD_TYPE_SECT | PMD_SECT_AP_WRITE |
224                 PMD_SECT_WBWA | PMD_SECT_S;
225
226         tegra_pgd = pgd_alloc(&init_mm);
227         if (!tegra_pgd)
228                 return -ENOMEM;
229
230         for (i=0; i<ARRAY_SIZE(addr_p); i++) {
231                 unsigned long v = addr_v[i];
232                 pmd = pmd_offset(tegra_pgd + pgd_index(v), v);
233                 *pmd = __pmd((addr_p[i] & PGDIR_MASK) | flags);
234                 flush_pmd_entry(pmd);
235                 outer_clean_range(__pa(pmd), __pa(pmd + 1));
236         }
237
238         tegra_pgd_phys = virt_to_phys(tegra_pgd);
239         __cpuc_flush_dcache_area(&tegra_pgd_phys,
240                 sizeof(tegra_pgd_phys));
241         outer_clean_range(__pa(&tegra_pgd_phys),
242                 __pa(&tegra_pgd_phys+1));
243
244         __cpuc_flush_dcache_area(&tegra_context_area,
245                 sizeof(tegra_context_area));
246         outer_clean_range(__pa(&tegra_context_area),
247                 __pa(&tegra_context_area+1));
248
249         return 0;
250 }
251
252
253
254 /*
255  * suspend_cpu_complex
256  *
257  *   disable periodic IRQs used for DVFS to prevent suspend wakeups
258  *   disable coresight debug interface
259  *
260  *
261  */
262 static noinline void restore_cpu_complex(void)
263 {
264         unsigned int reg;
265
266         /* restore original burst policy setting; PLLX state restored
267          * by CPU boot-up code - wait for PLL stabilization if PLLX
268          * was enabled, or if explicitly requested by caller */
269
270         BUG_ON(readl(clk_rst + CLK_RESET_PLLX_BASE) != tegra_sctx.pllx_base);
271
272         if (tegra_sctx.pllx_base & (1<<30)) {
273                 while (readl(tmrus)-tegra_sctx.pll_timeout >= 0x80000000UL)
274                         cpu_relax();
275         }
276         writel(tegra_sctx.cclk_divider, clk_rst + CLK_RESET_CCLK_DIVIDER);
277         writel(tegra_sctx.cpu_burst, clk_rst + CLK_RESET_CCLK_BURST);
278         writel(tegra_sctx.clk_csite_src, clk_rst + CLK_RESET_SOURCE_CSITE);
279
280         /* do not power-gate the CPU when flow controlled */
281         reg = readl(flow_ctrl + FLOW_CTRL_CPU_CSR);
282         reg &= ~((1<<5) | (1<<4) | 1); /* clear WFE bitmask */
283         reg |= (1<<14); /* write-1-clear event flag */
284         writel(reg, flow_ctrl + FLOW_CTRL_CPU_CSR);
285         wmb();
286
287 #ifdef CONFIG_HAVE_ARM_TWD
288         writel(tegra_sctx.twd_ctrl, twd_base + 0x8);
289         writel(tegra_sctx.twd_load, twd_base + 0);
290 #endif
291
292         gic_dist_restore(0);
293         get_irq_chip(IRQ_LOCALTIMER)->unmask(IRQ_LOCALTIMER);
294
295         enable_irq(INT_SYS_STATS_MON);
296 }
297
298 static noinline void suspend_cpu_complex(void)
299 {
300         unsigned int reg;
301         int i;
302
303         disable_irq(INT_SYS_STATS_MON);
304
305         /* switch coresite to clk_m, save off original source */
306         tegra_sctx.clk_csite_src = readl(clk_rst + CLK_RESET_SOURCE_CSITE);
307         writel(3<<30, clk_rst + CLK_RESET_SOURCE_CSITE);
308
309         tegra_sctx.cpu_burst = readl(clk_rst + CLK_RESET_CCLK_BURST);
310         tegra_sctx.pllx_base = readl(clk_rst + CLK_RESET_PLLX_BASE);
311         tegra_sctx.pllx_misc = readl(clk_rst + CLK_RESET_PLLX_MISC);
312         tegra_sctx.pllp_base = readl(clk_rst + CLK_RESET_PLLP_BASE);
313         tegra_sctx.pllp_outa = readl(clk_rst + CLK_RESET_PLLP_OUTA);
314         tegra_sctx.pllp_outb = readl(clk_rst + CLK_RESET_PLLP_OUTB);
315         tegra_sctx.pllp_misc = readl(clk_rst + CLK_RESET_PLLP_MISC);
316         tegra_sctx.cclk_divider = readl(clk_rst + CLK_RESET_CCLK_DIVIDER);
317
318 #ifdef CONFIG_HAVE_ARM_TWD
319         tegra_sctx.twd_ctrl = readl(twd_base + 0x8);
320         tegra_sctx.twd_load = readl(twd_base + 0);
321         local_timer_stop();
322 #endif
323
324         reg = readl(flow_ctrl + FLOW_CTRL_CPU_CSR);
325         /* clear any pending events, set the WFE bitmap to specify just
326          * CPU0, and clear any pending events for this CPU */
327         reg &= ~(1<<5); /* clear CPU1 WFE */
328         reg |= (1<<14) | (1<<4) | 1; /* enable CPU0 WFE */
329         writel(reg, flow_ctrl + FLOW_CTRL_CPU_CSR);
330         wmb();
331
332         for (i=1; i<num_present_cpus(); i++) {
333                 unsigned int offs = FLOW_CTRL_CPU1_CSR + (i-1)*8;
334                 reg = readl(flow_ctrl + offs);
335                 writel(reg | (1<<14), flow_ctrl + offs);
336                 wmb();
337         }
338
339         gic_cpu_exit(0);
340         gic_dist_save(0);
341 }
342
343 unsigned int tegra_suspend_lp2(unsigned int us)
344 {
345         unsigned int mode;
346         unsigned long orig, reg;
347         unsigned int remain;
348
349         reg = readl(pmc + PMC_CTRL);
350         mode = (reg >> TEGRA_POWER_PMC_SHIFT) & TEGRA_POWER_PMC_MASK;
351         mode |= TEGRA_POWER_CPU_PWRREQ_OE;
352         if (pdata->separate_req)
353                 mode |= TEGRA_POWER_PWRREQ_OE;
354         else
355                 mode &= ~TEGRA_POWER_PWRREQ_OE;
356         mode &= ~TEGRA_POWER_EFFECT_LP0;
357
358         orig = readl(evp_reset);
359         writel(virt_to_phys(tegra_lp2_startup), evp_reset);
360
361         set_power_timers(pdata->cpu_timer, pdata->cpu_off_timer,
362                          clk_get_rate_all_locked(tegra_pclk));
363
364         if (us)
365                 tegra_lp2_set_trigger(us);
366
367         suspend_cpu_complex();
368         stop_critical_timings();
369         flush_cache_all();
370         /* structure is written by reset code, so the L2 lines
371          * must be invalidated */
372         outer_flush_range(__pa(&tegra_sctx),__pa(&tegra_sctx+1));
373         barrier();
374
375         __cortex_a9_save(mode);
376         /* return from __cortex_a9_restore */
377         barrier();
378         restore_cpu_complex();
379         start_critical_timings();
380
381         remain = tegra_lp2_timer_remain();
382         if (us)
383                 tegra_lp2_set_trigger(0);
384
385         writel(orig, evp_reset);
386
387         return remain;
388 }
389
390 #ifdef CONFIG_PM
391
392 /* ensures that sufficient time is passed for a register write to
393  * serialize into the 32KHz domain */
394 static void pmc_32kwritel(u32 val, unsigned long offs)
395 {
396         writel(val, pmc + offs);
397         udelay(130);
398 }
399
400 static u8 *iram_save = NULL;
401 static unsigned int iram_save_size = 0;
402 static void __iomem *iram_code = IO_ADDRESS(TEGRA_IRAM_CODE_AREA);
403
404 static void tegra_suspend_dram(bool do_lp0)
405 {
406         unsigned int mode = TEGRA_POWER_SDRAM_SELFREFRESH;
407         unsigned long orig, reg;
408
409         orig = readl(evp_reset);
410         /* copy the reset vector and SDRAM shutdown code into IRAM */
411         memcpy(iram_save, iram_code, iram_save_size);
412         memcpy(iram_code, (void *)__tegra_lp1_reset, iram_save_size);
413
414         set_power_timers(pdata->cpu_timer, pdata->cpu_off_timer, 32768);
415
416         reg = readl(pmc + PMC_CTRL);
417         mode |= ((reg >> TEGRA_POWER_PMC_SHIFT) & TEGRA_POWER_PMC_MASK);
418
419         if (!do_lp0) {
420                 writel(TEGRA_IRAM_CODE_AREA, evp_reset);
421
422                 mode |= TEGRA_POWER_CPU_PWRREQ_OE;
423                 if (pdata->separate_req)
424                         mode |= TEGRA_POWER_PWRREQ_OE;
425                 else
426                         mode &= ~TEGRA_POWER_PWRREQ_OE;
427                 mode &= ~TEGRA_POWER_EFFECT_LP0;
428
429                 tegra_legacy_irq_set_lp1_wake_mask();
430         } else {
431                 u32 boot_flag = readl(pmc + PMC_SCRATCH0);
432                 pmc_32kwritel(boot_flag | 1, PMC_SCRATCH0);
433                 pmc_32kwritel(wb0_restore, PMC_SCRATCH1);
434                 writel(0x0, pmc + PMC_SCRATCH39);
435                 mode |= TEGRA_POWER_CPU_PWRREQ_OE;
436                 mode |= TEGRA_POWER_PWRREQ_OE;
437                 mode |= TEGRA_POWER_EFFECT_LP0;
438
439                 /* for platforms where the core & CPU power requests are
440                  * combined as a single request to the PMU, transition to
441                  * LP0 state by temporarily enabling both requests
442                  */
443                 if (!pdata->separate_req) {
444                         reg |= ((mode & TEGRA_POWER_PMC_MASK) <<
445                                 TEGRA_POWER_PMC_SHIFT);
446                         pmc_32kwritel(reg, PMC_CTRL);
447                         mode &= ~TEGRA_POWER_CPU_PWRREQ_OE;
448                 }
449
450                 tegra_set_lp0_wake_pads(pdata->wake_enb, pdata->wake_high,
451                         pdata->wake_any);
452         }
453
454         suspend_cpu_complex();
455         flush_cache_all();
456         l2x0_shutdown();
457
458         __cortex_a9_save(mode);
459         restore_cpu_complex();
460
461         writel(orig, evp_reset);
462         l2x0_restart();
463
464         if (!do_lp0) {
465                 memcpy(iram_code, iram_save, iram_save_size);
466                 tegra_legacy_irq_restore_mask();
467         } else {
468                 /* for platforms where the core & CPU power requests are
469                  * combined as a single request to the PMU, transition out
470                  * of LP0 state by temporarily enabling both requests
471                  */
472                 if (!pdata->separate_req) {
473                         reg = readl(pmc + PMC_CTRL);
474                         reg |= (TEGRA_POWER_CPU_PWRREQ_OE << TEGRA_POWER_PMC_SHIFT);
475                         pmc_32kwritel(reg, PMC_CTRL);
476                         reg &= ~(TEGRA_POWER_PWRREQ_OE << TEGRA_POWER_PMC_SHIFT);
477                         writel(reg, pmc + PMC_CTRL);
478                 }
479         }
480
481         wmb();
482 }
483
484 static int tegra_suspend_begin(suspend_state_t state)
485 {
486         return regulator_suspend_prepare(state);
487 }
488
489 static int tegra_suspend_prepare_late(void)
490 {
491         disable_irq(INT_SYS_STATS_MON);
492         return tegra_iovmm_suspend();
493 }
494
495 static void tegra_suspend_wake(void)
496 {
497         tegra_iovmm_resume();
498         enable_irq(INT_SYS_STATS_MON);
499 }
500
501 static u8 uart_state[5];
502
503 static int tegra_debug_uart_suspend(void)
504 {
505         void __iomem *uart;
506         u32 lcr;
507
508         if (TEGRA_DEBUG_UART_BASE == 0)
509                 return 0;
510
511         uart = IO_ADDRESS(TEGRA_DEBUG_UART_BASE);
512
513         lcr = readb(uart + UART_LCR * 4);
514
515         uart_state[0] = lcr;
516         uart_state[1] = readb(uart + UART_MCR * 4);
517
518         /* DLAB = 0 */
519         writeb(lcr & ~UART_LCR_DLAB, uart + UART_LCR * 4);
520
521         uart_state[2] = readb(uart + UART_IER * 4);
522
523         /* DLAB = 1 */
524         writeb(lcr | UART_LCR_DLAB, uart + UART_LCR * 4);
525
526         uart_state[3] = readb(uart + UART_DLL * 4);
527         uart_state[4] = readb(uart + UART_DLM * 4);
528
529         writeb(lcr, uart + UART_LCR * 4);
530
531         return 0;
532 }
533
534 static void tegra_debug_uart_resume(void)
535 {
536         void __iomem *uart;
537         u32 lcr;
538
539         if (TEGRA_DEBUG_UART_BASE == 0)
540                 return;
541
542         uart = IO_ADDRESS(TEGRA_DEBUG_UART_BASE);
543
544         lcr = uart_state[0];
545
546         writeb(uart_state[1], uart + UART_MCR * 4);
547
548         /* DLAB = 0 */
549         writeb(lcr & ~UART_LCR_DLAB, uart + UART_LCR * 4);
550
551         writeb(uart_state[2], uart + UART_IER * 4);
552
553         /* DLAB = 1 */
554         writeb(lcr | UART_LCR_DLAB, uart + UART_LCR * 4);
555
556         writeb(uart_state[3], uart + UART_DLL * 4);
557         writeb(uart_state[4], uart + UART_DLM * 4);
558
559         writeb(lcr, uart + UART_LCR * 4);
560 }
561
562 #define MC_SECURITY_START       0x6c
563 #define MC_SECURITY_SIZE        0x70
564 #define MC_SECURITY_CFG2        0x7c
565
566 static int tegra_suspend_enter(suspend_state_t state)
567 {
568         struct irq_desc *desc;
569         void __iomem *mc = IO_ADDRESS(TEGRA_MC_BASE);
570         unsigned long flags;
571         u32 mc_data[3] = {0, 0, 0};
572         int irq;
573         bool do_lp0 = (current_suspend_mode == TEGRA_SUSPEND_LP0);
574         bool do_lp2 = (current_suspend_mode == TEGRA_SUSPEND_LP2);
575         int lp_state;
576         u64 rtc_before;
577         u64 rtc_after;
578         u64 secs;
579         u32 ms;
580
581         if (do_lp2)
582                 lp_state = 2;
583         else if (do_lp0)
584                 lp_state = 0;
585         else
586                 lp_state = 1;
587
588         local_irq_save(flags);
589         local_fiq_disable();
590
591         pr_info("Entering suspend state LP%d\n", lp_state);
592         if (do_lp0) {
593                 tegra_irq_suspend();
594                 tegra_dma_suspend();
595                 tegra_debug_uart_suspend();
596                 tegra_pinmux_suspend();
597                 tegra_timer_suspend();
598                 tegra_gpio_suspend();
599                 tegra_clk_suspend();
600
601                 mc_data[0] = readl(mc + MC_SECURITY_START);
602                 mc_data[1] = readl(mc + MC_SECURITY_SIZE);
603                 mc_data[2] = readl(mc + MC_SECURITY_CFG2);
604         }
605
606         for_each_irq_desc(irq, desc) {
607                 if ((desc->status & IRQ_WAKEUP) &&
608                     (desc->status & IRQ_SUSPENDED)) {
609                         get_irq_chip(irq)->unmask(irq);
610                 }
611         }
612
613         rtc_before = tegra_rtc_read_ms();
614
615         if (do_lp2)
616                 tegra_suspend_lp2(0);
617         else
618                 tegra_suspend_dram(do_lp0);
619
620         rtc_after = tegra_rtc_read_ms();
621
622         for_each_irq_desc(irq, desc) {
623                 if ((desc->status & IRQ_WAKEUP) &&
624                     (desc->status & IRQ_SUSPENDED)) {
625                         get_irq_chip(irq)->mask(irq);
626                 }
627         }
628
629         /* Clear DPD sample */
630         writel(0x0, pmc + PMC_DPD_SAMPLE);
631
632         if (do_lp0) {
633                 writel(mc_data[0], mc + MC_SECURITY_START);
634                 writel(mc_data[1], mc + MC_SECURITY_SIZE);
635                 writel(mc_data[2], mc + MC_SECURITY_CFG2);
636
637                 tegra_clk_resume();
638                 tegra_gpio_resume();
639                 tegra_timer_resume();
640                 tegra_pinmux_resume();
641                 tegra_debug_uart_resume();
642                 tegra_dma_resume();
643                 tegra_irq_resume();
644         }
645
646         secs = rtc_after - rtc_before;
647         ms = do_div(secs, 1000);
648         pr_info("Suspended for %llu.%03u seconds\n", secs, ms);
649
650         tegra_time_in_suspend[time_to_bin(secs)]++;
651
652         local_fiq_enable();
653         local_irq_restore(flags);
654
655         return 0;
656 }
657
658 static struct platform_suspend_ops tegra_suspend_ops = {
659         .valid          = suspend_valid_only_mem,
660         .begin          = tegra_suspend_begin,
661         .prepare_late   = tegra_suspend_prepare_late,
662         .wake           = tegra_suspend_wake,
663         .enter          = tegra_suspend_enter,
664 };
665 #endif
666
667 void __init tegra_init_suspend(struct tegra_suspend_platform_data *plat)
668 {
669         u32 reg, mode;
670
671         tegra_pclk = clk_get_sys(NULL, "pclk");
672         BUG_ON(!tegra_pclk);
673         pdata = plat;
674         (void)reg;
675         (void)mode;
676
677         if (plat->suspend_mode == TEGRA_SUSPEND_LP0 && tegra_lp0_vec_size) {
678                 wb0_restore = tegra_lp0_vec_start;
679         } else {
680                 pr_warning("Suspend mode LP0 requested, but missing lp0_vec\n");
681                 pr_warning("Disabling LP0\n");
682                 plat->suspend_mode = TEGRA_SUSPEND_LP1;
683         }
684
685         tegra_context_area = kzalloc(CONTEXT_SIZE_BYTES * NR_CPUS, GFP_KERNEL);
686
687         if (tegra_context_area && create_suspend_pgtable()) {
688                 kfree(tegra_context_area);
689                 tegra_context_area = NULL;
690         }
691
692 #ifdef CONFIG_PM
693         iram_save_size = (unsigned long)__tegra_iram_end;
694         iram_save_size -= (unsigned long)__tegra_lp1_reset;
695
696         iram_save = kmalloc(iram_save_size, GFP_KERNEL);
697         if (!iram_save) {
698                 pr_err("%s: unable to allocate memory for SDRAM self-refresh "
699                        "LP0/LP1 unavailable\n", __func__);
700                 plat->suspend_mode = TEGRA_SUSPEND_LP2;
701         }
702         /* CPU reset vector for LP0 and LP1 */
703         writel(virt_to_phys(tegra_lp2_startup), pmc + PMC_SCRATCH41);
704
705         /* Always enable CPU power request; just normal polarity is supported */
706         reg = readl(pmc + PMC_CTRL);
707         BUG_ON(reg & (TEGRA_POWER_CPU_PWRREQ_POLARITY << TEGRA_POWER_PMC_SHIFT));
708         reg |= (TEGRA_POWER_CPU_PWRREQ_OE << TEGRA_POWER_PMC_SHIFT);
709         pmc_32kwritel(reg, PMC_CTRL);
710
711         /* Configure core power request and system clock control if LP0
712            is supported */
713         writel(pdata->core_timer, pmc + PMC_COREPWRGOOD_TIMER);
714         writel(pdata->core_off_timer, pmc + PMC_COREPWROFF_TIMER);
715         reg = readl(pmc + PMC_CTRL);
716         mode = (reg >> TEGRA_POWER_PMC_SHIFT) & TEGRA_POWER_PMC_MASK;
717
718         mode &= ~TEGRA_POWER_SYSCLK_POLARITY;
719         mode &= ~TEGRA_POWER_PWRREQ_POLARITY;
720
721         if (!pdata->sysclkreq_high)
722                 mode |= TEGRA_POWER_SYSCLK_POLARITY;
723         if (!pdata->corereq_high)
724                 mode |= TEGRA_POWER_PWRREQ_POLARITY;
725
726         /* configure output inverters while the request is tristated */
727         reg |= (mode << TEGRA_POWER_PMC_SHIFT);
728         pmc_32kwritel(reg, PMC_CTRL);
729
730         /* now enable requests */
731         reg |= (TEGRA_POWER_SYSCLK_OE << TEGRA_POWER_PMC_SHIFT);
732         if (pdata->separate_req)
733                 reg |= (TEGRA_POWER_PWRREQ_OE << TEGRA_POWER_PMC_SHIFT);
734         writel(reg, pmc + PMC_CTRL);
735
736         if (pdata->suspend_mode == TEGRA_SUSPEND_LP0)
737                 lp0_suspend_init();
738
739         suspend_set_ops(&tegra_suspend_ops);
740 #endif
741
742         current_suspend_mode = plat->suspend_mode;
743 }
744
745 #ifdef CONFIG_DEBUG_FS
746 static const char *tegra_suspend_name[TEGRA_MAX_SUSPEND_MODE] = {
747         [TEGRA_SUSPEND_NONE]    = "none",
748         [TEGRA_SUSPEND_LP2]     = "lp2",
749         [TEGRA_SUSPEND_LP1]     = "lp1",
750         [TEGRA_SUSPEND_LP0]     = "lp0",
751 };
752
753 static int tegra_suspend_debug_show(struct seq_file *s, void *data)
754 {
755         seq_printf(s, "%s\n", tegra_suspend_name[*(int *)s->private]);
756         return 0;
757 }
758
759 static int tegra_suspend_debug_open(struct inode *inode, struct file *file)
760 {
761         return single_open(file, tegra_suspend_debug_show, inode->i_private);
762 }
763
764 static int tegra_suspend_debug_write(struct file *file,
765         const char __user *user_buf, size_t count, loff_t *ppos)
766 {
767         char buf[32];
768         int buf_size;
769         int i;
770         struct seq_file *s = file->private_data;
771         enum tegra_suspend_mode *val = s->private;
772
773         memset(buf, 0x00, sizeof(buf));
774         buf_size = min(count, (sizeof(buf)-1));
775         if (copy_from_user(buf, user_buf, buf_size))
776                 return -EFAULT;
777
778         for (i = 0; i < TEGRA_MAX_SUSPEND_MODE; i++) {
779                 if (!strnicmp(buf, tegra_suspend_name[i],
780                     strlen(tegra_suspend_name[i]))) {
781                         if (i > pdata->suspend_mode)
782                                 return -EINVAL;
783                         *val = i;
784                         return count;
785                 }
786         }
787
788         return -EINVAL;
789 }
790
791 static const struct file_operations tegra_suspend_debug_fops = {
792         .open           = tegra_suspend_debug_open,
793         .write          = tegra_suspend_debug_write,
794         .read           = seq_read,
795         .llseek         = seq_lseek,
796         .release        = single_release,
797 };
798
799 static int tegra_suspend_time_debug_show(struct seq_file *s, void *data)
800 {
801         int bin;
802         seq_printf(s, "time (secs)  count\n");
803         seq_printf(s, "------------------\n");
804         for (bin = 0; bin < 32; bin++) {
805                 if (tegra_time_in_suspend[bin] == 0)
806                         continue;
807                 seq_printf(s, "%4d - %4d %4u\n",
808                         bin ? 1 << (bin - 1) : 0, 1 << bin,
809                         tegra_time_in_suspend[bin]);
810         }
811         return 0;
812 }
813
814 static int tegra_suspend_time_debug_open(struct inode *inode, struct file *file)
815 {
816         return single_open(file, tegra_suspend_time_debug_show, NULL);
817 }
818
819 static const struct file_operations tegra_suspend_time_debug_fops = {
820         .open           = tegra_suspend_time_debug_open,
821         .read           = seq_read,
822         .llseek         = seq_lseek,
823         .release        = single_release,
824 };
825
826 static int __init tegra_suspend_debug_init(void)
827 {
828         struct dentry *d;
829
830         d = debugfs_create_file("suspend_mode", 0755, NULL,
831                 (void *)&current_suspend_mode, &tegra_suspend_debug_fops);
832         if (!d) {
833                 pr_info("Failed to create suspend_mode debug file\n");
834                 return -ENOMEM;
835         }
836
837         d = debugfs_create_file("suspend_time", 0755, NULL, NULL,
838                 &tegra_suspend_time_debug_fops);
839         if (!d) {
840                 pr_info("Failed to create suspend_time debug file\n");
841                 return -ENOMEM;
842         }
843
844         return 0;
845 }
846
847 late_initcall(tegra_suspend_debug_init);
848 #endif