blob: efb80354f3034d856ab259bb1568dab41173d3b3 [file] [log] [blame]
Sekhar Noriefc1bb82009-12-17 18:29:31 +05301/*
2 * DaVinci Power Management Routines
3 *
4 * Copyright (C) 2009 Texas Instruments, Inc. http://www.ti.com/
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10
11#include <linux/pm.h>
12#include <linux/suspend.h>
13#include <linux/module.h>
14#include <linux/platform_device.h>
15#include <linux/clk.h>
16#include <linux/spinlock.h>
17
18#include <asm/cacheflush.h>
19#include <asm/delay.h>
Alexey Dobriyanb7f080c2011-06-16 11:01:34 +000020#include <asm/io.h>
Sekhar Noriefc1bb82009-12-17 18:29:31 +053021
Sekhar Nori215a0842013-04-10 14:57:15 +053022#include <mach/common.h>
Sekhar Noriefc1bb82009-12-17 18:29:31 +053023#include <mach/da8xx.h>
Kevin Hilmanaa9aa1e2016-11-15 11:54:19 -080024#include <mach/mux.h>
Sekhar Noriefc1bb82009-12-17 18:29:31 +053025#include <mach/pm.h>
26
27#include "clock.h"
Kevin Hilmanaa9aa1e2016-11-15 11:54:19 -080028#include "psc.h"
29#include "sram.h"
Sekhar Noriefc1bb82009-12-17 18:29:31 +053030
Kevin Hilmanaa9aa1e2016-11-15 11:54:19 -080031#define DA850_PLL1_BASE 0x01e1a000
Sekhar Noriefc1bb82009-12-17 18:29:31 +053032#define DEEPSLEEP_SLEEPCOUNT_MASK 0xFFFF
Kevin Hilmanaa9aa1e2016-11-15 11:54:19 -080033#define DEEPSLEEP_SLEEPCOUNT 128
Sekhar Noriefc1bb82009-12-17 18:29:31 +053034
35static void (*davinci_sram_suspend) (struct davinci_pm_config *);
Kevin Hilmanaa9aa1e2016-11-15 11:54:19 -080036static struct davinci_pm_config pm_config = {
37 .sleepcount = DEEPSLEEP_SLEEPCOUNT,
38 .ddrpsc_num = DA8XX_LPSC1_EMIF3C,
39};
40
Sekhar Noriefc1bb82009-12-17 18:29:31 +053041static void davinci_sram_push(void *dest, void *src, unsigned int size)
42{
43 memcpy(dest, src, size);
44 flush_icache_range((unsigned long)dest, (unsigned long)(dest + size));
45}
46
47static void davinci_pm_suspend(void)
48{
49 unsigned val;
50
Kevin Hilman1428ed12016-11-15 11:54:20 -080051 if (pm_config.cpupll_reg_base != pm_config.ddrpll_reg_base) {
Sekhar Noriefc1bb82009-12-17 18:29:31 +053052
53 /* Switch CPU PLL to bypass mode */
Kevin Hilman1428ed12016-11-15 11:54:20 -080054 val = __raw_readl(pm_config.cpupll_reg_base + PLLCTL);
Sekhar Noriefc1bb82009-12-17 18:29:31 +053055 val &= ~(PLLCTL_PLLENSRC | PLLCTL_PLLEN);
Kevin Hilman1428ed12016-11-15 11:54:20 -080056 __raw_writel(val, pm_config.cpupll_reg_base + PLLCTL);
Sekhar Noriefc1bb82009-12-17 18:29:31 +053057
58 udelay(PLL_BYPASS_TIME);
59
60 /* Powerdown CPU PLL */
Kevin Hilman1428ed12016-11-15 11:54:20 -080061 val = __raw_readl(pm_config.cpupll_reg_base + PLLCTL);
Sekhar Noriefc1bb82009-12-17 18:29:31 +053062 val |= PLLCTL_PLLPWRDN;
Kevin Hilman1428ed12016-11-15 11:54:20 -080063 __raw_writel(val, pm_config.cpupll_reg_base + PLLCTL);
Sekhar Noriefc1bb82009-12-17 18:29:31 +053064 }
65
66 /* Configure sleep count in deep sleep register */
Kevin Hilman1428ed12016-11-15 11:54:20 -080067 val = __raw_readl(pm_config.deepsleep_reg);
Sekhar Noriefc1bb82009-12-17 18:29:31 +053068 val &= ~DEEPSLEEP_SLEEPCOUNT_MASK,
Kevin Hilman1428ed12016-11-15 11:54:20 -080069 val |= pm_config.sleepcount;
70 __raw_writel(val, pm_config.deepsleep_reg);
Sekhar Noriefc1bb82009-12-17 18:29:31 +053071
72 /* System goes to sleep in this call */
Kevin Hilman1428ed12016-11-15 11:54:20 -080073 davinci_sram_suspend(&pm_config);
Sekhar Noriefc1bb82009-12-17 18:29:31 +053074
Kevin Hilman1428ed12016-11-15 11:54:20 -080075 if (pm_config.cpupll_reg_base != pm_config.ddrpll_reg_base) {
Sekhar Noriefc1bb82009-12-17 18:29:31 +053076
77 /* put CPU PLL in reset */
Kevin Hilman1428ed12016-11-15 11:54:20 -080078 val = __raw_readl(pm_config.cpupll_reg_base + PLLCTL);
Sekhar Noriefc1bb82009-12-17 18:29:31 +053079 val &= ~PLLCTL_PLLRST;
Kevin Hilman1428ed12016-11-15 11:54:20 -080080 __raw_writel(val, pm_config.cpupll_reg_base + PLLCTL);
Sekhar Noriefc1bb82009-12-17 18:29:31 +053081
82 /* put CPU PLL in power down */
Kevin Hilman1428ed12016-11-15 11:54:20 -080083 val = __raw_readl(pm_config.cpupll_reg_base + PLLCTL);
Sekhar Noriefc1bb82009-12-17 18:29:31 +053084 val &= ~PLLCTL_PLLPWRDN;
Kevin Hilman1428ed12016-11-15 11:54:20 -080085 __raw_writel(val, pm_config.cpupll_reg_base + PLLCTL);
Sekhar Noriefc1bb82009-12-17 18:29:31 +053086
87 /* wait for CPU PLL reset */
88 udelay(PLL_RESET_TIME);
89
90 /* bring CPU PLL out of reset */
Kevin Hilman1428ed12016-11-15 11:54:20 -080091 val = __raw_readl(pm_config.cpupll_reg_base + PLLCTL);
Sekhar Noriefc1bb82009-12-17 18:29:31 +053092 val |= PLLCTL_PLLRST;
Kevin Hilman1428ed12016-11-15 11:54:20 -080093 __raw_writel(val, pm_config.cpupll_reg_base + PLLCTL);
Sekhar Noriefc1bb82009-12-17 18:29:31 +053094
95 /* Wait for CPU PLL to lock */
96 udelay(PLL_LOCK_TIME);
97
98 /* Remove CPU PLL from bypass mode */
Kevin Hilman1428ed12016-11-15 11:54:20 -080099 val = __raw_readl(pm_config.cpupll_reg_base + PLLCTL);
Sekhar Noriefc1bb82009-12-17 18:29:31 +0530100 val &= ~PLLCTL_PLLENSRC;
101 val |= PLLCTL_PLLEN;
Kevin Hilman1428ed12016-11-15 11:54:20 -0800102 __raw_writel(val, pm_config.cpupll_reg_base + PLLCTL);
Sekhar Noriefc1bb82009-12-17 18:29:31 +0530103 }
104}
105
106static int davinci_pm_enter(suspend_state_t state)
107{
108 int ret = 0;
109
110 switch (state) {
Sekhar Noriefc1bb82009-12-17 18:29:31 +0530111 case PM_SUSPEND_MEM:
112 davinci_pm_suspend();
113 break;
114 default:
115 ret = -EINVAL;
116 }
117
118 return ret;
119}
120
Lionel Debroux2f55ac02010-11-16 14:14:02 +0100121static const struct platform_suspend_ops davinci_pm_ops = {
Sekhar Noriefc1bb82009-12-17 18:29:31 +0530122 .enter = davinci_pm_enter,
123 .valid = suspend_valid_only_mem,
124};
125
Kevin Hilmanaa9aa1e2016-11-15 11:54:19 -0800126int __init davinci_pm_init(void)
Sekhar Noriefc1bb82009-12-17 18:29:31 +0530127{
Kevin Hilmanaa9aa1e2016-11-15 11:54:19 -0800128 int ret;
129
130 ret = davinci_cfg_reg(DA850_RTC_ALARM);
131 if (ret)
132 return ret;
133
Kevin Hilman1428ed12016-11-15 11:54:20 -0800134 pm_config.ddr2_ctlr_base = da8xx_get_mem_ctlr();
135 pm_config.deepsleep_reg = DA8XX_SYSCFG1_VIRT(DA8XX_DEEPSLEEP_REG);
Kevin Hilmanaa9aa1e2016-11-15 11:54:19 -0800136
Kevin Hilman1428ed12016-11-15 11:54:20 -0800137 pm_config.cpupll_reg_base = ioremap(DA8XX_PLL0_BASE, SZ_4K);
138 if (!pm_config.cpupll_reg_base)
Kevin Hilmanaa9aa1e2016-11-15 11:54:19 -0800139 return -ENOMEM;
140
Kevin Hilman1428ed12016-11-15 11:54:20 -0800141 pm_config.ddrpll_reg_base = ioremap(DA850_PLL1_BASE, SZ_4K);
142 if (!pm_config.ddrpll_reg_base) {
Kevin Hilmanaa9aa1e2016-11-15 11:54:19 -0800143 ret = -ENOMEM;
144 goto no_ddrpll_mem;
145 }
146
Kevin Hilman1428ed12016-11-15 11:54:20 -0800147 pm_config.ddrpsc_reg_base = ioremap(DA8XX_PSC1_BASE, SZ_4K);
148 if (!pm_config.ddrpsc_reg_base) {
Kevin Hilmanaa9aa1e2016-11-15 11:54:19 -0800149 ret = -ENOMEM;
150 goto no_ddrpsc_mem;
Sekhar Noriefc1bb82009-12-17 18:29:31 +0530151 }
152
153 davinci_sram_suspend = sram_alloc(davinci_cpu_suspend_sz, NULL);
154 if (!davinci_sram_suspend) {
Kevin Hilmanaa9aa1e2016-11-15 11:54:19 -0800155 pr_err("PM: cannot allocate SRAM memory\n");
Sekhar Noriefc1bb82009-12-17 18:29:31 +0530156 return -ENOMEM;
157 }
158
159 davinci_sram_push(davinci_sram_suspend, davinci_cpu_suspend,
160 davinci_cpu_suspend_sz);
161
162 suspend_set_ops(&davinci_pm_ops);
163
Kevin Hilmanaa9aa1e2016-11-15 11:54:19 -0800164no_ddrpsc_mem:
Kevin Hilman1428ed12016-11-15 11:54:20 -0800165 iounmap(pm_config.ddrpll_reg_base);
Kevin Hilmanaa9aa1e2016-11-15 11:54:19 -0800166no_ddrpll_mem:
Kevin Hilman1428ed12016-11-15 11:54:20 -0800167 iounmap(pm_config.cpupll_reg_base);
Kevin Hilmanaa9aa1e2016-11-15 11:54:19 -0800168 return ret;
Sekhar Noriefc1bb82009-12-17 18:29:31 +0530169}