blob: 5ae22f7018134c9cd82f1f0cc0c75ac1a6d40784 [file] [log] [blame]
Shawn Guo12bb34402012-12-04 22:55:14 +08001/*
2 * Copyright (C) 2012 Freescale Semiconductor, Inc.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 */
8
Shawn Guoe5f9dec2012-12-04 22:55:15 +08009#include <linux/clockchips.h>
Shawn Guo12bb34402012-12-04 22:55:14 +080010#include <linux/cpuidle.h>
11#include <linux/module.h>
12#include <asm/cpuidle.h>
Shawn Guoe5f9dec2012-12-04 22:55:15 +080013#include <asm/proc-fns.h>
Shawn Guo12bb34402012-12-04 22:55:14 +080014
Shawn Guoe5f9dec2012-12-04 22:55:15 +080015#include "common.h"
Shawn Guo12bb34402012-12-04 22:55:14 +080016#include "cpuidle.h"
17
Shawn Guoe5f9dec2012-12-04 22:55:15 +080018static atomic_t master = ATOMIC_INIT(0);
19static DEFINE_SPINLOCK(master_lock);
20
21static int imx6q_enter_wait(struct cpuidle_device *dev,
22 struct cpuidle_driver *drv, int index)
23{
Shawn Guoe5f9dec2012-12-04 22:55:15 +080024 if (atomic_inc_return(&master) == num_online_cpus()) {
25 /*
26 * With this lock, we prevent other cpu to exit and enter
27 * this function again and become the master.
28 */
29 if (!spin_trylock(&master_lock))
30 goto idle;
31 imx6q_set_lpm(WAIT_UNCLOCKED);
32 cpu_do_idle();
33 imx6q_set_lpm(WAIT_CLOCKED);
34 spin_unlock(&master_lock);
35 goto done;
36 }
37
38idle:
39 cpu_do_idle();
40done:
41 atomic_dec(&master);
Shawn Guoe5f9dec2012-12-04 22:55:15 +080042
43 return index;
44}
45
46/*
47 * For each cpu, setup the broadcast timer because local timer
48 * stops for the states other than WFI.
49 */
50static void imx6q_setup_broadcast_timer(void *arg)
51{
52 int cpu = smp_processor_id();
53
54 clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_ON, &cpu);
55}
56
Shawn Guo12bb34402012-12-04 22:55:14 +080057static struct cpuidle_driver imx6q_cpuidle_driver = {
58 .name = "imx6q_cpuidle",
59 .owner = THIS_MODULE,
60 .en_core_tk_irqen = 1,
Shawn Guoe5f9dec2012-12-04 22:55:15 +080061 .states = {
62 /* WFI */
63 ARM_CPUIDLE_WFI_STATE,
64 /* WAIT */
65 {
66 .exit_latency = 50,
67 .target_residency = 75,
Daniel Lezcano8de46ef2013-03-21 12:21:33 +000068 .flags = CPUIDLE_FLAG_TIME_VALID |
69 CPUIDLE_FLAG_TIMER_STOP,
Shawn Guoe5f9dec2012-12-04 22:55:15 +080070 .enter = imx6q_enter_wait,
71 .name = "WAIT",
72 .desc = "Clock off",
73 },
74 },
75 .state_count = 2,
76 .safe_state_index = 0,
Shawn Guo12bb34402012-12-04 22:55:14 +080077};
78
79int __init imx6q_cpuidle_init(void)
80{
Shawn Guoe5f9dec2012-12-04 22:55:15 +080081 /* Need to enable SCU standby for entering WAIT modes */
82 imx_scu_standby_enable();
83
84 /* Set chicken bit to get a reliable WAIT mode support */
85 imx6q_set_chicken_bit();
86
87 /* Configure the broadcast timer on each cpu */
88 on_each_cpu(imx6q_setup_broadcast_timer, NULL, 1);
89
Shawn Guo12bb34402012-12-04 22:55:14 +080090 return imx_cpuidle_init(&imx6q_cpuidle_driver);
91}