Magnus Damm | 9799165 | 2011-04-29 02:28:08 +0900 | [diff] [blame] | 1 | /* |
| 2 | * sh7372 Power management support |
| 3 | * |
| 4 | * Copyright (C) 2011 Magnus Damm |
| 5 | * |
| 6 | * This file is subject to the terms and conditions of the GNU General Public |
| 7 | * License. See the file "COPYING" in the main directory of this archive |
| 8 | * for more details. |
| 9 | */ |
| 10 | |
| 11 | #include <linux/pm.h> |
| 12 | #include <linux/suspend.h> |
Magnus Damm | 082a8ca | 2011-04-29 02:39:32 +0900 | [diff] [blame] | 13 | #include <linux/cpuidle.h> |
Magnus Damm | 9799165 | 2011-04-29 02:28:08 +0900 | [diff] [blame] | 14 | #include <linux/module.h> |
| 15 | #include <linux/list.h> |
| 16 | #include <linux/err.h> |
| 17 | #include <linux/slab.h> |
Rafael J. Wysocki | b5e8d26 | 2011-08-25 15:34:19 +0200 | [diff] [blame] | 18 | #include <linux/pm_clock.h> |
Rafael J. Wysocki | e3e0109 | 2011-07-01 22:13:56 +0200 | [diff] [blame] | 19 | #include <linux/platform_device.h> |
| 20 | #include <linux/delay.h> |
Magnus Damm | 9799165 | 2011-04-29 02:28:08 +0900 | [diff] [blame] | 21 | #include <asm/system.h> |
| 22 | #include <asm/io.h> |
| 23 | #include <asm/tlbflush.h> |
Magnus Damm | 06b8416 | 2011-09-25 23:18:42 +0200 | [diff] [blame^] | 24 | #include <asm/suspend.h> |
Magnus Damm | 9799165 | 2011-04-29 02:28:08 +0900 | [diff] [blame] | 25 | #include <mach/common.h> |
Rafael J. Wysocki | e3e0109 | 2011-07-01 22:13:56 +0200 | [diff] [blame] | 26 | #include <mach/sh7372.h> |
Magnus Damm | 9799165 | 2011-04-29 02:28:08 +0900 | [diff] [blame] | 27 | |
| 28 | #define SMFRAM 0xe6a70000 |
| 29 | #define SYSTBCR 0xe6150024 |
| 30 | #define SBAR 0xe6180020 |
| 31 | #define APARMBAREA 0xe6f10020 |
| 32 | |
Rafael J. Wysocki | e3e0109 | 2011-07-01 22:13:56 +0200 | [diff] [blame] | 33 | #define SPDCR 0xe6180008 |
| 34 | #define SWUCR 0xe6180014 |
| 35 | #define PSTR 0xe6180080 |
| 36 | |
| 37 | #define PSTR_RETRIES 100 |
| 38 | #define PSTR_DELAY_US 10 |
| 39 | |
| 40 | #ifdef CONFIG_PM |
| 41 | |
| 42 | static int pd_power_down(struct generic_pm_domain *genpd) |
| 43 | { |
| 44 | struct sh7372_pm_domain *sh7372_pd = to_sh7372_pd(genpd); |
| 45 | unsigned int mask = 1 << sh7372_pd->bit_shift; |
| 46 | |
| 47 | if (__raw_readl(PSTR) & mask) { |
| 48 | unsigned int retry_count; |
| 49 | |
| 50 | __raw_writel(mask, SPDCR); |
| 51 | |
| 52 | for (retry_count = PSTR_RETRIES; retry_count; retry_count--) { |
| 53 | if (!(__raw_readl(SPDCR) & mask)) |
| 54 | break; |
| 55 | cpu_relax(); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | pr_debug("sh7372 power domain down 0x%08x -> PSTR = 0x%08x\n", |
| 60 | mask, __raw_readl(PSTR)); |
| 61 | |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | static int pd_power_up(struct generic_pm_domain *genpd) |
| 66 | { |
| 67 | struct sh7372_pm_domain *sh7372_pd = to_sh7372_pd(genpd); |
| 68 | unsigned int mask = 1 << sh7372_pd->bit_shift; |
| 69 | unsigned int retry_count; |
| 70 | int ret = 0; |
| 71 | |
| 72 | if (__raw_readl(PSTR) & mask) |
| 73 | goto out; |
| 74 | |
| 75 | __raw_writel(mask, SWUCR); |
| 76 | |
| 77 | for (retry_count = 2 * PSTR_RETRIES; retry_count; retry_count--) { |
| 78 | if (!(__raw_readl(SWUCR) & mask)) |
| 79 | goto out; |
| 80 | if (retry_count > PSTR_RETRIES) |
| 81 | udelay(PSTR_DELAY_US); |
| 82 | else |
| 83 | cpu_relax(); |
| 84 | } |
| 85 | if (__raw_readl(SWUCR) & mask) |
| 86 | ret = -EIO; |
| 87 | |
| 88 | out: |
| 89 | pr_debug("sh7372 power domain up 0x%08x -> PSTR = 0x%08x\n", |
| 90 | mask, __raw_readl(PSTR)); |
| 91 | |
| 92 | return ret; |
| 93 | } |
| 94 | |
| 95 | static bool pd_active_wakeup(struct device *dev) |
| 96 | { |
| 97 | return true; |
| 98 | } |
| 99 | |
| 100 | void sh7372_init_pm_domain(struct sh7372_pm_domain *sh7372_pd) |
| 101 | { |
| 102 | struct generic_pm_domain *genpd = &sh7372_pd->genpd; |
| 103 | |
| 104 | pm_genpd_init(genpd, NULL, false); |
| 105 | genpd->stop_device = pm_clk_suspend; |
| 106 | genpd->start_device = pm_clk_resume; |
Rafael J. Wysocki | 0aa2a22 | 2011-08-25 15:37:04 +0200 | [diff] [blame] | 107 | genpd->dev_irq_safe = true; |
Rafael J. Wysocki | e3e0109 | 2011-07-01 22:13:56 +0200 | [diff] [blame] | 108 | genpd->active_wakeup = pd_active_wakeup; |
Rafael J. Wysocki | 111058c | 2011-08-14 13:35:39 +0200 | [diff] [blame] | 109 | genpd->power_off = pd_power_down; |
| 110 | genpd->power_on = pd_power_up; |
Magnus Damm | 775b8ae | 2011-07-10 10:39:32 +0200 | [diff] [blame] | 111 | genpd->power_on(&sh7372_pd->genpd); |
Rafael J. Wysocki | e3e0109 | 2011-07-01 22:13:56 +0200 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | void sh7372_add_device_to_domain(struct sh7372_pm_domain *sh7372_pd, |
| 115 | struct platform_device *pdev) |
| 116 | { |
| 117 | struct device *dev = &pdev->dev; |
| 118 | |
Rafael J. Wysocki | e3e0109 | 2011-07-01 22:13:56 +0200 | [diff] [blame] | 119 | pm_genpd_add_device(&sh7372_pd->genpd, dev); |
Rafael J. Wysocki | 4605ab6 | 2011-08-25 15:34:12 +0200 | [diff] [blame] | 120 | if (pm_clk_no_clocks(dev)) |
| 121 | pm_clk_add(dev, NULL); |
Rafael J. Wysocki | e3e0109 | 2011-07-01 22:13:56 +0200 | [diff] [blame] | 122 | } |
| 123 | |
Rafael J. Wysocki | 111058c | 2011-08-14 13:35:39 +0200 | [diff] [blame] | 124 | void sh7372_pm_add_subdomain(struct sh7372_pm_domain *sh7372_pd, |
| 125 | struct sh7372_pm_domain *sh7372_sd) |
| 126 | { |
| 127 | pm_genpd_add_subdomain(&sh7372_pd->genpd, &sh7372_sd->genpd); |
| 128 | } |
| 129 | |
Rafael J. Wysocki | e3e0109 | 2011-07-01 22:13:56 +0200 | [diff] [blame] | 130 | struct sh7372_pm_domain sh7372_a4lc = { |
| 131 | .bit_shift = 1, |
| 132 | }; |
| 133 | |
Kuninori Morimoto | c1ba5bb | 2011-07-10 10:12:08 +0200 | [diff] [blame] | 134 | struct sh7372_pm_domain sh7372_a4mp = { |
| 135 | .bit_shift = 2, |
| 136 | }; |
| 137 | |
Magnus Damm | d24771d | 2011-07-10 10:38:22 +0200 | [diff] [blame] | 138 | struct sh7372_pm_domain sh7372_d4 = { |
| 139 | .bit_shift = 3, |
| 140 | }; |
| 141 | |
Magnus Damm | 33afebf | 2011-07-01 22:14:45 +0200 | [diff] [blame] | 142 | struct sh7372_pm_domain sh7372_a3rv = { |
| 143 | .bit_shift = 6, |
| 144 | }; |
| 145 | |
Magnus Damm | 082517a | 2011-07-01 22:14:53 +0200 | [diff] [blame] | 146 | struct sh7372_pm_domain sh7372_a3ri = { |
| 147 | .bit_shift = 8, |
| 148 | }; |
| 149 | |
Magnus Damm | c47586b | 2011-07-01 22:15:01 +0200 | [diff] [blame] | 150 | struct sh7372_pm_domain sh7372_a3sg = { |
| 151 | .bit_shift = 13, |
| 152 | }; |
| 153 | |
Rafael J. Wysocki | e3e0109 | 2011-07-01 22:13:56 +0200 | [diff] [blame] | 154 | #endif /* CONFIG_PM */ |
| 155 | |
Magnus Damm | 06b8416 | 2011-09-25 23:18:42 +0200 | [diff] [blame^] | 156 | static int sh7372_do_idle_core_standby(unsigned long unused) |
| 157 | { |
| 158 | cpu_do_idle(); /* WFI when SYSTBCR == 0x10 -> Core Standby */ |
| 159 | return 0; |
| 160 | } |
| 161 | |
Paul Mundt | 66ad129 | 2011-05-25 11:22:58 +0900 | [diff] [blame] | 162 | static void sh7372_enter_core_standby(void) |
Magnus Damm | 9799165 | 2011-04-29 02:28:08 +0900 | [diff] [blame] | 163 | { |
Magnus Damm | 06b8416 | 2011-09-25 23:18:42 +0200 | [diff] [blame^] | 164 | /* set reset vector, translate 4k */ |
| 165 | __raw_writel(__pa(sh7372_resume_core_standby), SBAR); |
| 166 | __raw_writel(0, APARMBAREA); |
Magnus Damm | 9799165 | 2011-04-29 02:28:08 +0900 | [diff] [blame] | 167 | |
Magnus Damm | 06b8416 | 2011-09-25 23:18:42 +0200 | [diff] [blame^] | 168 | /* enter sleep mode with SYSTBCR to 0x10 */ |
| 169 | __raw_writel(0x10, SYSTBCR); |
| 170 | cpu_suspend(0, sh7372_do_idle_core_standby); |
| 171 | __raw_writel(0, SYSTBCR); |
Magnus Damm | 9799165 | 2011-04-29 02:28:08 +0900 | [diff] [blame] | 172 | |
Magnus Damm | 06b8416 | 2011-09-25 23:18:42 +0200 | [diff] [blame^] | 173 | /* disable reset vector translation */ |
| 174 | __raw_writel(0, SBAR); |
Magnus Damm | 9799165 | 2011-04-29 02:28:08 +0900 | [diff] [blame] | 175 | } |
| 176 | |
Magnus Damm | 082a8ca | 2011-04-29 02:39:32 +0900 | [diff] [blame] | 177 | #ifdef CONFIG_CPU_IDLE |
| 178 | static void sh7372_cpuidle_setup(struct cpuidle_device *dev) |
| 179 | { |
| 180 | struct cpuidle_state *state; |
| 181 | int i = dev->state_count; |
| 182 | |
| 183 | state = &dev->states[i]; |
| 184 | snprintf(state->name, CPUIDLE_NAME_LEN, "C2"); |
| 185 | strncpy(state->desc, "Core Standby Mode", CPUIDLE_DESC_LEN); |
| 186 | state->exit_latency = 10; |
| 187 | state->target_residency = 20 + 10; |
| 188 | state->power_usage = 1; /* perhaps not */ |
| 189 | state->flags = 0; |
| 190 | state->flags |= CPUIDLE_FLAG_TIME_VALID; |
| 191 | shmobile_cpuidle_modes[i] = sh7372_enter_core_standby; |
| 192 | |
| 193 | dev->state_count = i + 1; |
| 194 | } |
| 195 | |
| 196 | static void sh7372_cpuidle_init(void) |
| 197 | { |
| 198 | shmobile_cpuidle_setup = sh7372_cpuidle_setup; |
| 199 | } |
| 200 | #else |
| 201 | static void sh7372_cpuidle_init(void) {} |
| 202 | #endif |
| 203 | |
| 204 | #ifdef CONFIG_SUSPEND |
Magnus Damm | 9799165 | 2011-04-29 02:28:08 +0900 | [diff] [blame] | 205 | static int sh7372_enter_suspend(suspend_state_t suspend_state) |
| 206 | { |
| 207 | sh7372_enter_core_standby(); |
| 208 | return 0; |
| 209 | } |
| 210 | |
| 211 | static void sh7372_suspend_init(void) |
| 212 | { |
| 213 | shmobile_suspend_ops.enter = sh7372_enter_suspend; |
| 214 | } |
| 215 | #else |
| 216 | static void sh7372_suspend_init(void) {} |
| 217 | #endif |
| 218 | |
| 219 | #define DBGREG1 0xe6100020 |
| 220 | #define DBGREG9 0xe6100040 |
| 221 | |
| 222 | void __init sh7372_pm_init(void) |
| 223 | { |
| 224 | /* enable DBG hardware block to kick SYSC */ |
| 225 | __raw_writel(0x0000a500, DBGREG9); |
| 226 | __raw_writel(0x0000a501, DBGREG9); |
| 227 | __raw_writel(0x00000000, DBGREG1); |
| 228 | |
| 229 | sh7372_suspend_init(); |
Magnus Damm | 082a8ca | 2011-04-29 02:39:32 +0900 | [diff] [blame] | 230 | sh7372_cpuidle_init(); |
Magnus Damm | 9799165 | 2011-04-29 02:28:08 +0900 | [diff] [blame] | 231 | } |