Shawn Guo | 12bb3440 | 2012-12-04 22:55:14 +0800 | [diff] [blame] | 1 | /* |
| 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 Guo | e5f9dec | 2012-12-04 22:55:15 +0800 | [diff] [blame] | 9 | #include <linux/clockchips.h> |
Shawn Guo | 12bb3440 | 2012-12-04 22:55:14 +0800 | [diff] [blame] | 10 | #include <linux/cpuidle.h> |
| 11 | #include <linux/module.h> |
| 12 | #include <asm/cpuidle.h> |
Shawn Guo | e5f9dec | 2012-12-04 22:55:15 +0800 | [diff] [blame] | 13 | #include <asm/proc-fns.h> |
Shawn Guo | 12bb3440 | 2012-12-04 22:55:14 +0800 | [diff] [blame] | 14 | |
Shawn Guo | e5f9dec | 2012-12-04 22:55:15 +0800 | [diff] [blame] | 15 | #include "common.h" |
Shawn Guo | 12bb3440 | 2012-12-04 22:55:14 +0800 | [diff] [blame] | 16 | #include "cpuidle.h" |
| 17 | |
Shawn Guo | e5f9dec | 2012-12-04 22:55:15 +0800 | [diff] [blame] | 18 | static atomic_t master = ATOMIC_INIT(0); |
| 19 | static DEFINE_SPINLOCK(master_lock); |
| 20 | |
| 21 | static int imx6q_enter_wait(struct cpuidle_device *dev, |
| 22 | struct cpuidle_driver *drv, int index) |
| 23 | { |
Shawn Guo | e5f9dec | 2012-12-04 22:55:15 +0800 | [diff] [blame] | 24 | 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 | |
| 38 | idle: |
| 39 | cpu_do_idle(); |
| 40 | done: |
| 41 | atomic_dec(&master); |
Shawn Guo | e5f9dec | 2012-12-04 22:55:15 +0800 | [diff] [blame] | 42 | |
| 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 | */ |
| 50 | static 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 Guo | 12bb3440 | 2012-12-04 22:55:14 +0800 | [diff] [blame] | 57 | static struct cpuidle_driver imx6q_cpuidle_driver = { |
| 58 | .name = "imx6q_cpuidle", |
| 59 | .owner = THIS_MODULE, |
| 60 | .en_core_tk_irqen = 1, |
Shawn Guo | e5f9dec | 2012-12-04 22:55:15 +0800 | [diff] [blame] | 61 | .states = { |
| 62 | /* WFI */ |
| 63 | ARM_CPUIDLE_WFI_STATE, |
| 64 | /* WAIT */ |
| 65 | { |
| 66 | .exit_latency = 50, |
| 67 | .target_residency = 75, |
Daniel Lezcano | 8de46ef | 2013-03-21 12:21:33 +0000 | [diff] [blame^] | 68 | .flags = CPUIDLE_FLAG_TIME_VALID | |
| 69 | CPUIDLE_FLAG_TIMER_STOP, |
Shawn Guo | e5f9dec | 2012-12-04 22:55:15 +0800 | [diff] [blame] | 70 | .enter = imx6q_enter_wait, |
| 71 | .name = "WAIT", |
| 72 | .desc = "Clock off", |
| 73 | }, |
| 74 | }, |
| 75 | .state_count = 2, |
| 76 | .safe_state_index = 0, |
Shawn Guo | 12bb3440 | 2012-12-04 22:55:14 +0800 | [diff] [blame] | 77 | }; |
| 78 | |
| 79 | int __init imx6q_cpuidle_init(void) |
| 80 | { |
Shawn Guo | e5f9dec | 2012-12-04 22:55:15 +0800 | [diff] [blame] | 81 | /* 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 Guo | 12bb3440 | 2012-12-04 22:55:14 +0800 | [diff] [blame] | 90 | return imx_cpuidle_init(&imx6q_cpuidle_driver); |
| 91 | } |