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 | |
| 9 | #include <linux/cpuidle.h> |
| 10 | #include <linux/module.h> |
| 11 | #include <asm/cpuidle.h> |
| 12 | |
Ben Dooks | 4709610 | 2016-06-21 13:13:11 +0100 | [diff] [blame] | 13 | #include <soc/imx/cpuidle.h> |
| 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" |
Anson Huang | a25d67a | 2014-06-20 13:44:05 +0800 | [diff] [blame] | 17 | #include "hardware.h" |
Shawn Guo | 12bb3440 | 2012-12-04 22:55:14 +0800 | [diff] [blame] | 18 | |
Shawn Guo | e5f9dec | 2012-12-04 22:55:15 +0800 | [diff] [blame] | 19 | static atomic_t master = ATOMIC_INIT(0); |
| 20 | static DEFINE_SPINLOCK(master_lock); |
| 21 | |
| 22 | static int imx6q_enter_wait(struct cpuidle_device *dev, |
| 23 | struct cpuidle_driver *drv, int index) |
| 24 | { |
Shawn Guo | e5f9dec | 2012-12-04 22:55:15 +0800 | [diff] [blame] | 25 | if (atomic_inc_return(&master) == num_online_cpus()) { |
| 26 | /* |
| 27 | * With this lock, we prevent other cpu to exit and enter |
| 28 | * this function again and become the master. |
| 29 | */ |
| 30 | if (!spin_trylock(&master_lock)) |
| 31 | goto idle; |
Shawn Guo | 8fb76a0 | 2015-04-25 22:59:19 +0800 | [diff] [blame] | 32 | imx6_set_lpm(WAIT_UNCLOCKED); |
Shawn Guo | e5f9dec | 2012-12-04 22:55:15 +0800 | [diff] [blame] | 33 | cpu_do_idle(); |
Shawn Guo | 8fb76a0 | 2015-04-25 22:59:19 +0800 | [diff] [blame] | 34 | imx6_set_lpm(WAIT_CLOCKED); |
Shawn Guo | e5f9dec | 2012-12-04 22:55:15 +0800 | [diff] [blame] | 35 | spin_unlock(&master_lock); |
| 36 | goto done; |
| 37 | } |
| 38 | |
| 39 | idle: |
| 40 | cpu_do_idle(); |
| 41 | done: |
| 42 | atomic_dec(&master); |
Shawn Guo | e5f9dec | 2012-12-04 22:55:15 +0800 | [diff] [blame] | 43 | |
| 44 | return index; |
| 45 | } |
| 46 | |
Shawn Guo | 12bb3440 | 2012-12-04 22:55:14 +0800 | [diff] [blame] | 47 | static struct cpuidle_driver imx6q_cpuidle_driver = { |
| 48 | .name = "imx6q_cpuidle", |
| 49 | .owner = THIS_MODULE, |
Shawn Guo | e5f9dec | 2012-12-04 22:55:15 +0800 | [diff] [blame] | 50 | .states = { |
| 51 | /* WFI */ |
| 52 | ARM_CPUIDLE_WFI_STATE, |
| 53 | /* WAIT */ |
| 54 | { |
| 55 | .exit_latency = 50, |
| 56 | .target_residency = 75, |
Daniel Lezcano | b82b6cc | 2014-11-12 16:03:50 +0100 | [diff] [blame] | 57 | .flags = CPUIDLE_FLAG_TIMER_STOP, |
Shawn Guo | e5f9dec | 2012-12-04 22:55:15 +0800 | [diff] [blame] | 58 | .enter = imx6q_enter_wait, |
| 59 | .name = "WAIT", |
| 60 | .desc = "Clock off", |
| 61 | }, |
| 62 | }, |
| 63 | .state_count = 2, |
| 64 | .safe_state_index = 0, |
Shawn Guo | 12bb3440 | 2012-12-04 22:55:14 +0800 | [diff] [blame] | 65 | }; |
| 66 | |
Lucas Stach | 2938090 | 2016-06-03 18:31:19 +0200 | [diff] [blame] | 67 | /* |
| 68 | * i.MX6 Q/DL has an erratum (ERR006687) that prevents the FEC from waking the |
| 69 | * CPUs when they are in wait(unclocked) state. As the hardware workaround isn't |
| 70 | * applicable to all boards, disable the deeper idle state when the workaround |
| 71 | * isn't present and the FEC is in use. |
| 72 | */ |
| 73 | void imx6q_cpuidle_fec_irqs_used(void) |
| 74 | { |
| 75 | imx6q_cpuidle_driver.states[1].disabled = true; |
| 76 | } |
Shawn Guo | 2cb9caa | 2016-06-21 10:30:44 +0800 | [diff] [blame] | 77 | EXPORT_SYMBOL_GPL(imx6q_cpuidle_fec_irqs_used); |
Lucas Stach | 2938090 | 2016-06-03 18:31:19 +0200 | [diff] [blame] | 78 | |
| 79 | void imx6q_cpuidle_fec_irqs_unused(void) |
| 80 | { |
| 81 | imx6q_cpuidle_driver.states[1].disabled = false; |
| 82 | } |
Shawn Guo | 2cb9caa | 2016-06-21 10:30:44 +0800 | [diff] [blame] | 83 | EXPORT_SYMBOL_GPL(imx6q_cpuidle_fec_irqs_unused); |
Lucas Stach | 2938090 | 2016-06-03 18:31:19 +0200 | [diff] [blame] | 84 | |
Shawn Guo | 12bb3440 | 2012-12-04 22:55:14 +0800 | [diff] [blame] | 85 | int __init imx6q_cpuidle_init(void) |
| 86 | { |
Fabio Estevam | fa6be65 | 2014-01-07 08:00:40 -0200 | [diff] [blame] | 87 | /* Set INT_MEM_CLK_LPM bit to get a reliable WAIT mode support */ |
Anson Huang | 8765caa | 2016-08-29 21:49:56 +0800 | [diff] [blame] | 88 | imx6_set_int_mem_clk_lpm(true); |
Shawn Guo | e5f9dec | 2012-12-04 22:55:15 +0800 | [diff] [blame] | 89 | |
Daniel Lezcano | 54a4644 | 2013-04-23 08:54:45 +0000 | [diff] [blame] | 90 | return cpuidle_register(&imx6q_cpuidle_driver, NULL); |
Shawn Guo | 12bb3440 | 2012-12-04 22:55:14 +0800 | [diff] [blame] | 91 | } |