blob: 93ae096c4ab2e879e22e58a049e7f530c5aa96be [file] [log] [blame]
Sekhar Noria6c0f6e2009-11-03 15:14:13 +05301/*
2 * CPU idle for DaVinci SoCs
3 *
4 * Copyright (C) 2009 Texas Instruments Incorporated. http://www.ti.com/
5 *
6 * Derived from Marvell Kirkwood CPU idle code
7 * (arch/arm/mach-kirkwood/cpuidle.c)
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
14#include <linux/kernel.h>
15#include <linux/init.h>
16#include <linux/platform_device.h>
17#include <linux/cpuidle.h>
18#include <linux/io.h>
Paul Gortmakerdc280942011-07-31 16:17:29 -040019#include <linux/export.h>
Sekhar Noria6c0f6e2009-11-03 15:14:13 +053020#include <asm/proc-fns.h>
Robert Lee19976c22012-03-20 15:22:45 -050021#include <asm/cpuidle.h>
Sekhar Noria6c0f6e2009-11-03 15:14:13 +053022
23#include <mach/cpuidle.h>
Nicolas Pitre0020afb2011-07-05 22:52:57 -040024#include <mach/ddr2.h>
Sekhar Noria6c0f6e2009-11-03 15:14:13 +053025
26#define DAVINCI_CPUIDLE_MAX_STATES 2
27
28struct davinci_ops {
29 void (*enter) (u32 flags);
30 void (*exit) (u32 flags);
31 u32 flags;
32};
33
Robert Lee19976c22012-03-20 15:22:45 -050034/* Actual code that puts the SoC in different idle states */
35static int davinci_enter_idle(struct cpuidle_device *dev,
36 struct cpuidle_driver *drv,
37 int index)
38{
39 struct cpuidle_state_usage *state_usage = &dev->states_usage[index];
40 struct davinci_ops *ops = cpuidle_get_statedata(state_usage);
41
42 if (ops && ops->enter)
43 ops->enter(ops->flags);
44
45 index = cpuidle_wrap_enter(dev, drv, index,
46 arm_cpuidle_simple_enter);
47
48 if (ops && ops->exit)
49 ops->exit(ops->flags);
50
51 return index;
52}
53
Sekhar Noria6c0f6e2009-11-03 15:14:13 +053054/* fields in davinci_ops.flags */
55#define DAVINCI_CPUIDLE_FLAGS_DDR2_PWDN BIT(0)
56
57static struct cpuidle_driver davinci_idle_driver = {
Robert Lee19976c22012-03-20 15:22:45 -050058 .name = "cpuidle-davinci",
59 .owner = THIS_MODULE,
60 .states[0] = ARM_CPUIDLE_WFI_STATE,
61 .states[1] = {
62 .enter = davinci_enter_idle,
63 .exit_latency = 10,
64 .target_residency = 100000,
65 .flags = CPUIDLE_FLAG_TIME_VALID,
66 .name = "DDR SR",
67 .desc = "WFI and DDR Self Refresh",
68 },
69 .state_count = DAVINCI_CPUIDLE_MAX_STATES,
Sekhar Noria6c0f6e2009-11-03 15:14:13 +053070};
71
72static DEFINE_PER_CPU(struct cpuidle_device, davinci_cpuidle_device);
73static void __iomem *ddr2_reg_base;
74
Sekhar Noria6c0f6e2009-11-03 15:14:13 +053075static void davinci_save_ddr_power(int enter, bool pdown)
76{
77 u32 val;
78
79 val = __raw_readl(ddr2_reg_base + DDR2_SDRCR_OFFSET);
80
81 if (enter) {
82 if (pdown)
83 val |= DDR2_SRPD_BIT;
84 else
85 val &= ~DDR2_SRPD_BIT;
86 val |= DDR2_LPMODEN_BIT;
87 } else {
88 val &= ~(DDR2_SRPD_BIT | DDR2_LPMODEN_BIT);
89 }
90
91 __raw_writel(val, ddr2_reg_base + DDR2_SDRCR_OFFSET);
92}
93
94static void davinci_c2state_enter(u32 flags)
95{
96 davinci_save_ddr_power(1, !!(flags & DAVINCI_CPUIDLE_FLAGS_DDR2_PWDN));
97}
98
99static void davinci_c2state_exit(u32 flags)
100{
101 davinci_save_ddr_power(0, !!(flags & DAVINCI_CPUIDLE_FLAGS_DDR2_PWDN));
102}
103
104static struct davinci_ops davinci_states[DAVINCI_CPUIDLE_MAX_STATES] = {
105 [1] = {
106 .enter = davinci_c2state_enter,
107 .exit = davinci_c2state_exit,
108 },
109};
110
Sekhar Noria6c0f6e2009-11-03 15:14:13 +0530111static int __init davinci_cpuidle_probe(struct platform_device *pdev)
112{
113 int ret;
114 struct cpuidle_device *device;
115 struct davinci_cpuidle_config *pdata = pdev->dev.platform_data;
Sekhar Noria6c0f6e2009-11-03 15:14:13 +0530116
117 device = &per_cpu(davinci_cpuidle_device, smp_processor_id());
118
119 if (!pdata) {
120 dev_err(&pdev->dev, "cannot get platform data\n");
121 return -ENOENT;
122 }
123
Sekhar Nori948c66d2009-11-16 17:21:37 +0530124 ddr2_reg_base = pdata->ddr2_ctlr_base;
Sekhar Noria6c0f6e2009-11-03 15:14:13 +0530125
Sekhar Noria6c0f6e2009-11-03 15:14:13 +0530126 if (pdata->ddr2_pdown)
127 davinci_states[1].flags |= DAVINCI_CPUIDLE_FLAGS_DDR2_PWDN;
Deepthi Dharwar42027352011-10-28 16:20:33 +0530128 cpuidle_set_statedata(&device->states_usage[1], &davinci_states[1]);
Sekhar Noria6c0f6e2009-11-03 15:14:13 +0530129
130 device->state_count = DAVINCI_CPUIDLE_MAX_STATES;
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530131
Sekhar Noria6c0f6e2009-11-03 15:14:13 +0530132 ret = cpuidle_register_driver(&davinci_idle_driver);
133 if (ret) {
134 dev_err(&pdev->dev, "failed to register driver\n");
135 return ret;
136 }
137
Sekhar Noria6c0f6e2009-11-03 15:14:13 +0530138 ret = cpuidle_register_device(device);
139 if (ret) {
140 dev_err(&pdev->dev, "failed to register device\n");
Sekhar Nori948c66d2009-11-16 17:21:37 +0530141 cpuidle_unregister_driver(&davinci_idle_driver);
142 return ret;
Sekhar Noria6c0f6e2009-11-03 15:14:13 +0530143 }
144
145 return 0;
Sekhar Noria6c0f6e2009-11-03 15:14:13 +0530146}
147
148static struct platform_driver davinci_cpuidle_driver = {
149 .driver = {
150 .name = "cpuidle-davinci",
151 .owner = THIS_MODULE,
152 },
153};
154
155static int __init davinci_cpuidle_init(void)
156{
157 return platform_driver_probe(&davinci_cpuidle_driver,
158 davinci_cpuidle_probe);
159}
160device_initcall(davinci_cpuidle_init);
161