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