blob: a851e6c984218d14533fd5157c7e58fc76c5fc81 [file] [log] [blame]
Albin Tonnerre1ea60cf2009-11-01 18:40:50 +01001/*
2 * based on arch/arm/mach-kirkwood/cpuidle.c
3 *
4 * CPU idle support for AT91 SoC
5 *
6 * This file is licensed under the terms of the GNU General Public
7 * License version 2. This program is licensed "as is" without any
8 * warranty of any kind, whether express or implied.
9 *
10 * The cpu idle uses wait-for-interrupt and RAM self refresh in order
11 * to implement two idle states -
12 * #1 wait-for-interrupt
13 * #2 wait-for-interrupt and RAM self refresh
14 */
15
16#include <linux/kernel.h>
17#include <linux/init.h>
18#include <linux/platform_device.h>
19#include <linux/cpuidle.h>
20#include <asm/proc-fns.h>
21#include <linux/io.h>
Paul Gortmakerdc280942011-07-31 16:17:29 -040022#include <linux/export.h>
Albin Tonnerre1ea60cf2009-11-01 18:40:50 +010023
24#include "pm.h"
25
26#define AT91_MAX_STATES 2
27
28static DEFINE_PER_CPU(struct cpuidle_device, at91_cpuidle_device);
29
30static struct cpuidle_driver at91_idle_driver = {
31 .name = "at91_idle",
32 .owner = THIS_MODULE,
33};
34
35/* Actual code that puts the SoC in different idle states */
36static int at91_enter_idle(struct cpuidle_device *dev,
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +053037 struct cpuidle_driver *drv,
Deepthi Dharware978aa72011-10-28 16:20:09 +053038 int index)
Albin Tonnerre1ea60cf2009-11-01 18:40:50 +010039{
40 struct timeval before, after;
41 int idle_time;
42 u32 saved_lpr;
43
44 local_irq_disable();
45 do_gettimeofday(&before);
Deepthi Dharware978aa72011-10-28 16:20:09 +053046 if (index == 0)
Albin Tonnerre1ea60cf2009-11-01 18:40:50 +010047 /* Wait for interrupt state */
48 cpu_do_idle();
Deepthi Dharware978aa72011-10-28 16:20:09 +053049 else if (index == 1) {
Albin Tonnerre1ea60cf2009-11-01 18:40:50 +010050 asm("b 1f; .align 5; 1:");
51 asm("mcr p15, 0, r0, c7, c10, 4"); /* drain write buffer */
52 saved_lpr = sdram_selfrefresh_enable();
53 cpu_do_idle();
54 sdram_selfrefresh_disable(saved_lpr);
55 }
56 do_gettimeofday(&after);
57 local_irq_enable();
58 idle_time = (after.tv_sec - before.tv_sec) * USEC_PER_SEC +
59 (after.tv_usec - before.tv_usec);
Deepthi Dharware978aa72011-10-28 16:20:09 +053060
61 dev->last_residency = idle_time;
62 return index;
Albin Tonnerre1ea60cf2009-11-01 18:40:50 +010063}
64
65/* Initialize CPU idle by registering the idle states */
66static int at91_init_cpuidle(void)
67{
68 struct cpuidle_device *device;
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +053069 struct cpuidle_driver *driver = &at91_idle_driver;
Albin Tonnerre1ea60cf2009-11-01 18:40:50 +010070
71 device = &per_cpu(at91_cpuidle_device, smp_processor_id());
72 device->state_count = AT91_MAX_STATES;
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +053073 driver->state_count = AT91_MAX_STATES;
Albin Tonnerre1ea60cf2009-11-01 18:40:50 +010074
75 /* Wait for interrupt state */
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +053076 driver->states[0].enter = at91_enter_idle;
77 driver->states[0].exit_latency = 1;
78 driver->states[0].target_residency = 10000;
79 driver->states[0].flags = CPUIDLE_FLAG_TIME_VALID;
80 strcpy(driver->states[0].name, "WFI");
81 strcpy(driver->states[0].desc, "Wait for interrupt");
Albin Tonnerre1ea60cf2009-11-01 18:40:50 +010082
83 /* Wait for interrupt and RAM self refresh state */
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +053084 driver->states[1].enter = at91_enter_idle;
85 driver->states[1].exit_latency = 10;
86 driver->states[1].target_residency = 10000;
87 driver->states[1].flags = CPUIDLE_FLAG_TIME_VALID;
88 strcpy(driver->states[1].name, "RAM_SR");
89 strcpy(driver->states[1].desc, "WFI and RAM Self Refresh");
90
91 cpuidle_register_driver(&at91_idle_driver);
Albin Tonnerre1ea60cf2009-11-01 18:40:50 +010092
93 if (cpuidle_register_device(device)) {
94 printk(KERN_ERR "at91_init_cpuidle: Failed registering\n");
95 return -EIO;
96 }
97 return 0;
98}
99
100device_initcall(at91_init_cpuidle);