4 #include <linux/delay.h>
6 #include <linux/kernel.h>
7 #include <linux/init.h>
9 #include <linux/suspend.h>
10 #include <linux/random.h>
11 #include <linux/crc32.h>
13 #include <linux/wakelock.h>
14 #include <asm/tlbflush.h>
15 #include <asm/hardware/gic.h>
17 #include <mach/rk29_iomap.h>
20 #include <mach/board.h>
21 #include <mach/system.h>
22 #include <mach/sram.h>
23 #include <mach/gpio.h>
25 #include <mach/memtester.h>
26 #include <mach/iomux.h>
27 #include <mach/pm-vol.h>
31 #define grf_readl(offset) readl(RK29_GRF_BASE + offset)
32 #define grf_writel(v, offset) do { writel(v, RK29_GRF_BASE + offset); readl(RK29_GRF_BASE + offset); } while (0)
34 static unsigned long save_sp;
36 static inline void delay_500ns(void)
41 static inline void delay_300us(void)
43 LOOP(300 * LOOPS_PER_USEC);
47 void/* inline*/ __sramfunc sram_printch(char byte)
52 local_irq_save(flags);
53 gate1 = cru_readl(CRU_CLKGATE1_CON);
54 gate2 = cru_readl(CRU_CLKGATE2_CON);
55 cru_writel(gate1 & ~((1 << CLK_GATE_PCLK_PEIRPH % 32) | (1 << CLK_GATE_ACLK_PEIRPH % 32) | (1 << CLK_GATE_ACLK_CPU_PERI % 32)), CRU_CLKGATE1_CON);
56 cru_writel(gate2 & ~(1 << CLK_GATE_UART1 % 32), CRU_CLKGATE2_CON);
59 writel(byte, RK29_UART1_BASE);
61 /* loop check LSR[6], Transmitter Empty bit */
62 while (!(readl(RK29_UART1_BASE + 0x14) & 0x40))
65 cru_writel(gate2, CRU_CLKGATE2_CON);
66 cru_writel(gate1, CRU_CLKGATE1_CON);
67 local_irq_restore(flags);
72 void __sramfunc sram_printascii(const char *s)
83 void print(const char *s)
88 void __sramfunc print_Hex(unsigned int hex)
94 unsigned char c = (hex & 0xF0000000) >> 28;
95 sram_printch(c < 0xa ? c + '0' : c - 0xa + 'a');
100 void __sramfunc print_Dec (uint32_t n)
107 sram_printch((char)(n + '0'));
110 void print_Dec_3(uint32_t value)
126 static void /* inline*/ __sramfunc printhex(unsigned int hex)
132 unsigned char c = (hex & 0xF0000000) >> 28;
133 sram_printch(c < 0xa ? c + '0' : c - 0xa + 'a');
138 static void inline sram_printch(char byte) {}
139 static void inline sram_printascii(const char *s) {}
140 static void inline printhex(unsigned int hex) {}
143 /*volatile __sramdata */int ddr_debug;
144 module_param(ddr_debug, int, 0644);
146 static int inline calc_crc32(u32 addr, size_t len)
148 return crc32_le(~0,(const unsigned char *)addr,len);
150 void __sramfunc ddr_testmode(void)
152 int32_t g_crc1,g_crc2;
155 extern char _stext[], _etext[];
165 g_crc1 = calc_crc32((u32)_stext, (size_t)(_etext-_stext));
166 nMHz = 333 + (random32()>>25);
172 //ddr_print("%s change freq to: %d MHz\n", __func__, nMHz);
173 ddr_change_freq(nMHz);
174 g_crc2 = calc_crc32((u32)_stext, (size_t)(_etext-_stext));
183 //ddr_print("check image crc32 success--crc value = 0x%x!, count:%d\n",g_crc1, n++);
184 // sram_printascii("change freq success\n");
187 else if(ddr_debug == 2)
196 g_crc1 = calc_crc32((u32)_stext, (size_t)(_etext-_stext));
197 nMHz = (random32()>>13);// 16.7s max
204 g_crc2 = calc_crc32((u32)_stext, (size_t)(_etext-_stext));
213 // ddr_print("check image crc32 fail!, count:%d\n", n++);
214 // sram_printascii("self refresh fail\n");
216 //ddr_print("check image crc32 success--crc value = 0x%x!, count:%d\n",g_crc1, n++);
217 // sram_printascii("self refresh success\n");
220 else if(ddr_debug == 3)
226 void __sramfunc ddr_testmode(void)
230 void __sramfunc pm_clk_switch_32k(void);
232 void __sramfunc pm_wfi(void)
236 clksel0 = cru_readl(CRU_CLKSEL0_CON);
237 /* set arm clk 24MHz/32 = 750KHz */
238 cru_writel(clksel0 | 0x1F, CRU_CLKSEL0_CON);
246 cru_writel(clksel0, CRU_CLKSEL0_CON);
252 static void __sramfunc rk29_sram_suspend(void)
256 if ((ddr_debug == 1) || (ddr_debug == 2))
263 vol=rk29_suspend_voltage_set(1000000);
264 #ifdef CONFIG_RK29_CLK_SWITCH_TO_32K
269 rk29_suspend_voltage_resume(vol);
276 static void noinline rk29_suspend(void)
278 DDR_SAVE_SP(save_sp);
280 DDR_RESTORE_SP(save_sp);
283 static void dump_irq(void)
285 u32 irq_gpio = (readl(RK29_GICPERI_BASE + GIC_DIST_PENDING_SET + 8) >> 23) & 0x7F;
286 printk("wakeup irq: %08x %08x %01x\n",
287 readl(RK29_GICPERI_BASE + GIC_DIST_PENDING_SET + 4),
288 readl(RK29_GICPERI_BASE + GIC_DIST_PENDING_SET + 8),
289 readl(RK29_GICPERI_BASE + GIC_DIST_PENDING_SET + 12) & 0xf);
291 printk("wakeup gpio0: %08x\n", readl(RK29_GPIO0_BASE + GPIO_INT_STATUS));
293 printk("wakeup gpio1: %08x\n", readl(RK29_GPIO1_BASE + GPIO_INT_STATUS));
295 printk("wakeup gpio2: %08x\n", readl(RK29_GPIO2_BASE + GPIO_INT_STATUS));
297 printk("wakeup gpio3: %08x\n", readl(RK29_GPIO3_BASE + GPIO_INT_STATUS));
299 printk("wakeup gpio4: %08x\n", readl(RK29_GPIO4_BASE + GPIO_INT_STATUS));
301 printk("wakeup gpio5: %08x\n", readl(RK29_GPIO5_BASE + GPIO_INT_STATUS));
303 printk("wakeup gpio6: %08x\n", readl(RK29_GPIO6_BASE + GPIO_INT_STATUS));
306 #define DUMP_GPIO_INTEN(ID) \
308 u32 en = readl(RK29_GPIO##ID##_BASE + GPIO_INTEN); \
310 sram_printascii("GPIO" #ID "_INTEN: "); \
312 sram_printch('\n'); \
316 static void dump_inten(void)
329 #define DUMP_GPIO_PULL(ID) \
331 u32 state = readl(RK29_GRF_BASE + GRF_GPIO0_PULL + (ID<<2)); \
332 sram_printascii("GPIO" #ID "_PULL: "); \
334 sram_printch('\n'); \
337 static void dump_io_pull(void)
347 #ifdef CONFIG_RK29_NEON_POWERDOMAIN_SET
348 /*******************************neon powermain***********************/
349 #define pmu_read(offset) readl(RK29_PMU_BASE + (offset))
350 #define pmu_write(offset, value) writel((value), RK29_PMU_BASE + (offset))
351 #define PMU_PG_CON 0x10
352 #define vfpreg(_vfp_) #_vfp_
354 #define fmrx(_vfp_) ({ \
356 asm("mrc p10, 7, %0, " vfpreg(_vfp_) ", cr0, 0 @ fmrx %0, " #_vfp_ \
357 : "=r" (__v) : : "cc"); \
361 #define fmxr(_vfp_,_var_) \
362 asm("mcr p10, 7, %0, " vfpreg(_vfp_) ", cr0, 0 @ fmxr " #_vfp_ ", %0" \
363 : : "r" (_var_) : "cc")
364 extern void vfp_save_state(void *location, u32 fpexc);
365 extern void vfp_load_state(void *location, u32 fpexc);
366 // extern __sramdata u64 saveptr[33];
367 static u32 saveptr[2][60]={};
368 void neon_powerdomain_off(void)
374 unsigned int fpexc = fmrx(FPEXC); //get neon Logic gate
376 fmxr(FPEXC, fpexc | FPEXC_EN); //open neon Logic gate
378 vfp_save_state(p,fpexc); //save neon reg,32 D reg,2 control reg
381 fmxr(FPEXC, fpexc & ~FPEXC_EN); //close neon Logic gate
383 ret=pmu_read(PMU_PG_CON); //get power domain state
384 pmu_write(PMU_PG_CON,ret|(0x1<<1)); //powerdomain off neon
385 printk("neon powerdomain is off\n");
387 void neon_powerdomain_on(void)
393 ret=pmu_read(PMU_PG_CON); //get power domain state
394 pmu_write(PMU_PG_CON,ret&~(0x1<<1)); //powerdomain on neon
397 unsigned int fpexc = fmrx(FPEXC); //get neon Logic gate
398 fmxr(FPEXC, fpexc | FPEXC_EN); //open neon Logic gate
400 vfp_load_state(p,fpexc); //recovery neon reg, 32 D reg,2 control reg
403 fmxr(FPEXC, fpexc | FPEXC_EN); //open neon Logic gate
404 printk("neon powerdomain is on\n");
408 void pm_gpio_suspend(void);
409 void pm_gpio_resume(void);
411 static int rk29_pm_enter(suspend_state_t state)
413 u32 apll, cpll, gpll, mode, clksel0;
416 #ifdef CONFIG_RK29_NEON_POWERDOMAIN_SET
417 neon_powerdomain_off();
424 // dump GPIO INTEN for debug
426 // dump GPIO PULL state for debug
427 //if you want to display the information, please enable the code.
434 interface_ctr_reg_pread();
437 clkgate[0] = cru_readl(CRU_CLKGATE0_CON);
438 clkgate[1] = cru_readl(CRU_CLKGATE1_CON);
439 clkgate[2] = cru_readl(CRU_CLKGATE2_CON);
440 clkgate[3] = cru_clkgate3_con_mirror;
441 cru_writel(~((1 << CLK_GATE_CORE)
442 | (1 << CLK_GATE_ACLK_CPU)
443 | (1 << CLK_GATE_ACLK_CPU2)
444 | (1 << CLK_GATE_PCLK_CPU)
445 | (1 << CLK_GATE_GIC)
446 | (1 << CLK_GATE_INTMEM)
447 | (1 << CLK_GATE_DDR_PHY)
448 | (1 << CLK_GATE_DDR_REG)
449 | (1 << CLK_GATE_DDR_CPU)
450 | (1 << CLK_GATE_GPIO0)
451 | (1 << CLK_GATE_RTC)
452 | (1 << CLK_GATE_GRF)
453 #ifdef CONFIG_RK29_JTAG
454 | (1 << CLK_GATE_PCLK_CORE)
455 | (1 << CLK_GATE_ATCLK_CORE)
456 | (1 << CLK_GATE_ATCLK_CPU)
457 | (1 << CLK_GATE_DEBUG)
458 | (1 << CLK_GATE_TPIU)
460 ) | clkgate[0], CRU_CLKGATE0_CON);
461 cru_writel(~0, CRU_CLKGATE1_CON);
462 cru_writel(~((1 << CLK_GATE_GPIO1 % 32)
463 | (1 << CLK_GATE_GPIO2 % 32)
464 | (1 << CLK_GATE_GPIO3 % 32)
465 | (1 << CLK_GATE_GPIO4 % 32)
466 | (1 << CLK_GATE_GPIO5 % 32)
467 | (1 << CLK_GATE_GPIO6 % 32)
468 | (1 << CLK_GATE_PWM % 32)
469 #ifdef CONFIG_RK29_JTAG
470 | (1 << CLK_GATE_JTAG % 32)
472 ) | clkgate[2], CRU_CLKGATE2_CON);
473 cru_writel(~0, CRU_CLKGATE3_CON);
476 mode = cru_readl(CRU_MODE_CON);
477 clksel0 = cru_readl(CRU_CLKSEL0_CON);
479 /* suspend arm pll */
480 apll = cru_readl(CRU_APLL_CON);
481 cru_writel((cru_readl(CRU_MODE_CON) & ~CRU_CPU_MODE_MASK) | CRU_CPU_MODE_SLOW, CRU_MODE_CON);
482 cru_writel(apll | PLL_BYPASS, CRU_APLL_CON);
483 cru_writel(apll | PLL_PD | PLL_BYPASS, CRU_APLL_CON);
485 /* set core = aclk_cpu = hclk_cpu = pclk_cpu = 24MHz */
486 cru_writel(clksel0 & 0xFFFFF000, CRU_CLKSEL0_CON);
489 /* suspend codec pll */
490 cpll = cru_readl(CRU_CPLL_CON);
491 cru_writel((cru_readl(CRU_MODE_CON) & ~CRU_CODEC_MODE_MASK) | CRU_CODEC_MODE_SLOW, CRU_MODE_CON);
492 cru_writel(cpll | PLL_BYPASS, CRU_CPLL_CON);
493 cru_writel(cpll | PLL_PD | PLL_BYPASS, CRU_CPLL_CON);
497 /* suspend general pll */
498 gpll = cru_readl(CRU_GPLL_CON);
499 cru_writel((cru_readl(CRU_MODE_CON) & ~CRU_GENERAL_MODE_MASK) | CRU_GENERAL_MODE_SLOW, CRU_MODE_CON);
500 cru_writel(gpll | PLL_BYPASS, CRU_GPLL_CON);
501 cru_writel(gpll | PLL_PD | PLL_BYPASS, CRU_GPLL_CON);
503 /* set aclk_periph = hclk_periph = pclk_periph = 24MHz */
504 cru_writel(clksel0 & ~0x7FC000, CRU_CLKSEL0_CON);
512 /* resume general pll */
513 cru_writel(gpll, CRU_GPLL_CON);
515 /* restore aclk_periph/hclk_periph/pclk_periph */
516 cru_writel(cru_readl(CRU_CLKSEL0_CON) | (clksel0 & 0x7FC000), CRU_CLKSEL0_CON);
517 cru_writel((cru_readl(CRU_MODE_CON) & ~CRU_GENERAL_MODE_MASK) | (mode & CRU_GENERAL_MODE_MASK), CRU_MODE_CON);
520 /* resume codec pll */
521 cru_writel(cpll, CRU_CPLL_CON);
523 cru_writel((cru_readl(CRU_MODE_CON) & ~CRU_CODEC_MODE_MASK) | (mode & CRU_CODEC_MODE_MASK), CRU_MODE_CON);
527 cru_writel(apll, CRU_APLL_CON);
529 /* restore core/aclk_cpu/hclk_cpu/pclk_cpu */
530 cru_writel(cru_readl(CRU_CLKSEL0_CON) | (clksel0 & 0xFFF), CRU_CLKSEL0_CON);
531 cru_writel((cru_readl(CRU_MODE_CON) & ~CRU_CPU_MODE_MASK) | (mode & CRU_CPU_MODE_MASK), CRU_MODE_CON);
535 cru_writel(clkgate[0], CRU_CLKGATE0_CON);
536 cru_writel(clkgate[1], CRU_CLKGATE1_CON);
537 cru_writel(clkgate[2], CRU_CLKGATE2_CON);
538 cru_writel(clkgate[3], CRU_CLKGATE3_CON);
539 sram_printascii("0\n");
543 #ifdef CONFIG_RK29_NEON_POWERDOMAIN_SET
544 neon_powerdomain_on();
550 static int rk29_pm_prepare(void)
552 /* disable entering rk29_idle() by disable_hlt() */
557 static void rk29_pm_finish(void)
562 static struct platform_suspend_ops rk29_pm_ops = {
563 .enter = rk29_pm_enter,
564 .valid = suspend_valid_only_mem,
565 .prepare = rk29_pm_prepare,
566 .finish = rk29_pm_finish,
569 static void rk29_idle(void)
571 if (!need_resched()) {
573 #ifdef CONFIG_HAS_WAKELOCK
574 allow_sleep = !has_wake_lock(WAKE_LOCK_IDLE);
577 u32 mode_con = cru_readl(CRU_MODE_CON);
578 cru_writel((mode_con & ~CRU_CPU_MODE_MASK) | CRU_CPU_MODE_SLOW, CRU_MODE_CON);
580 cru_writel(mode_con, CRU_MODE_CON);
588 static int __init rk29_pm_init(void)
590 suspend_set_ops(&rk29_pm_ops);
592 /* set idle function */
598 __initcall(rk29_pm_init);