Ben Dooks | 6419711 | 2008-12-12 00:24:06 +0000 | [diff] [blame] | 1 | /* linux/arch/arm/plat-s3c/pm.c |
| 2 | * |
| 3 | * Copyright 2008 Openmoko, Inc. |
Ben Dooks | ccae941 | 2009-11-13 22:54:14 +0000 | [diff] [blame] | 4 | * Copyright 2004-2008 Simtec Electronics |
Ben Dooks | 6419711 | 2008-12-12 00:24:06 +0000 | [diff] [blame] | 5 | * Ben Dooks <ben@simtec.co.uk> |
| 6 | * http://armlinux.simtec.co.uk/ |
| 7 | * |
| 8 | * S3C common power management (suspend to ram) support. |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License version 2 as |
| 12 | * published by the Free Software Foundation. |
| 13 | */ |
| 14 | |
| 15 | #include <linux/init.h> |
| 16 | #include <linux/suspend.h> |
| 17 | #include <linux/errno.h> |
Ben Dooks | 2261e0e | 2008-12-12 00:24:08 +0000 | [diff] [blame] | 18 | #include <linux/delay.h> |
| 19 | #include <linux/serial_core.h> |
Ben Dooks | 6419711 | 2008-12-12 00:24:06 +0000 | [diff] [blame] | 20 | #include <linux/io.h> |
| 21 | |
Ben Dooks | 2261e0e | 2008-12-12 00:24:08 +0000 | [diff] [blame] | 22 | #include <asm/cacheflush.h> |
Russell King | 2c74a0c | 2011-06-22 17:41:48 +0100 | [diff] [blame] | 23 | #include <asm/suspend.h> |
Ben Dooks | 2261e0e | 2008-12-12 00:24:08 +0000 | [diff] [blame] | 24 | #include <mach/hardware.h> |
Ben Dooks | bd117bd | 2009-03-10 18:19:35 +0000 | [diff] [blame] | 25 | #include <mach/map.h> |
Ben Dooks | 2261e0e | 2008-12-12 00:24:08 +0000 | [diff] [blame] | 26 | |
| 27 | #include <plat/regs-serial.h> |
| 28 | #include <mach/regs-clock.h> |
Ben Dooks | 2261e0e | 2008-12-12 00:24:08 +0000 | [diff] [blame] | 29 | #include <mach/regs-irq.h> |
Ben Dooks | 56b3442 | 2008-12-12 00:24:12 +0000 | [diff] [blame] | 30 | #include <asm/irq.h> |
Ben Dooks | 2261e0e | 2008-12-12 00:24:08 +0000 | [diff] [blame] | 31 | |
Ben Dooks | 6419711 | 2008-12-12 00:24:06 +0000 | [diff] [blame] | 32 | #include <plat/pm.h> |
Ben Dooks | 431fb7d | 2010-02-05 13:52:53 +0100 | [diff] [blame] | 33 | #include <mach/pm-core.h> |
Ben Dooks | 6419711 | 2008-12-12 00:24:06 +0000 | [diff] [blame] | 34 | |
| 35 | /* for external use */ |
| 36 | |
| 37 | unsigned long s3c_pm_flags; |
| 38 | |
Ben Dooks | 2261e0e | 2008-12-12 00:24:08 +0000 | [diff] [blame] | 39 | /* Debug code: |
| 40 | * |
| 41 | * This code supports debug output to the low level UARTs for use on |
| 42 | * resume before the console layer is available. |
| 43 | */ |
| 44 | |
Ben Dooks | 8005745 | 2010-01-20 12:29:25 +0900 | [diff] [blame] | 45 | #ifdef CONFIG_SAMSUNG_PM_DEBUG |
Ben Dooks | 6419711 | 2008-12-12 00:24:06 +0000 | [diff] [blame] | 46 | extern void printascii(const char *); |
| 47 | |
| 48 | void s3c_pm_dbg(const char *fmt, ...) |
| 49 | { |
| 50 | va_list va; |
| 51 | char buff[256]; |
| 52 | |
| 53 | va_start(va, fmt); |
Chen Gang | 8baaa26 | 2013-01-25 10:29:01 -0800 | [diff] [blame] | 54 | vsnprintf(buff, sizeof(buff), fmt, va); |
Ben Dooks | 6419711 | 2008-12-12 00:24:06 +0000 | [diff] [blame] | 55 | va_end(va); |
| 56 | |
| 57 | printascii(buff); |
| 58 | } |
Ben Dooks | 2261e0e | 2008-12-12 00:24:08 +0000 | [diff] [blame] | 59 | |
| 60 | static inline void s3c_pm_debug_init(void) |
| 61 | { |
| 62 | /* restart uart clocks so we can use them to output */ |
| 63 | s3c_pm_debug_init_uart(); |
| 64 | } |
| 65 | |
| 66 | #else |
| 67 | #define s3c_pm_debug_init() do { } while(0) |
| 68 | |
Ben Dooks | 8005745 | 2010-01-20 12:29:25 +0900 | [diff] [blame] | 69 | #endif /* CONFIG_SAMSUNG_PM_DEBUG */ |
Ben Dooks | 6419711 | 2008-12-12 00:24:06 +0000 | [diff] [blame] | 70 | |
Ben Dooks | 2261e0e | 2008-12-12 00:24:08 +0000 | [diff] [blame] | 71 | /* Save the UART configurations if we are configured for debug. */ |
| 72 | |
Ben Dooks | 4b637dc | 2008-12-12 00:24:24 +0000 | [diff] [blame] | 73 | unsigned char pm_uart_udivslot; |
| 74 | |
Ben Dooks | 8005745 | 2010-01-20 12:29:25 +0900 | [diff] [blame] | 75 | #ifdef CONFIG_SAMSUNG_PM_DEBUG |
Ben Dooks | 2261e0e | 2008-12-12 00:24:08 +0000 | [diff] [blame] | 76 | |
Sachin Kamat | c040124 | 2012-08-07 19:17:14 +0900 | [diff] [blame] | 77 | static struct pm_uart_save uart_save[CONFIG_SERIAL_SAMSUNG_UARTS]; |
Ben Dooks | 2261e0e | 2008-12-12 00:24:08 +0000 | [diff] [blame] | 78 | |
Ben Dooks | d2b07fe | 2008-12-12 00:24:20 +0000 | [diff] [blame] | 79 | static void s3c_pm_save_uart(unsigned int uart, struct pm_uart_save *save) |
Ben Dooks | 2261e0e | 2008-12-12 00:24:08 +0000 | [diff] [blame] | 80 | { |
Ben Dooks | d2b07fe | 2008-12-12 00:24:20 +0000 | [diff] [blame] | 81 | void __iomem *regs = S3C_VA_UARTx(uart); |
| 82 | |
| 83 | save->ulcon = __raw_readl(regs + S3C2410_ULCON); |
| 84 | save->ucon = __raw_readl(regs + S3C2410_UCON); |
| 85 | save->ufcon = __raw_readl(regs + S3C2410_UFCON); |
| 86 | save->umcon = __raw_readl(regs + S3C2410_UMCON); |
| 87 | save->ubrdiv = __raw_readl(regs + S3C2410_UBRDIV); |
Ben Dooks | 57699e9 | 2008-12-12 00:24:23 +0000 | [diff] [blame] | 88 | |
Ben Dooks | 4b637dc | 2008-12-12 00:24:24 +0000 | [diff] [blame] | 89 | if (pm_uart_udivslot) |
| 90 | save->udivslot = __raw_readl(regs + S3C2443_DIVSLOT); |
| 91 | |
Ben Dooks | 57699e9 | 2008-12-12 00:24:23 +0000 | [diff] [blame] | 92 | S3C_PMDBG("UART[%d]: ULCON=%04x, UCON=%04x, UFCON=%04x, UBRDIV=%04x\n", |
| 93 | uart, save->ulcon, save->ucon, save->ufcon, save->ubrdiv); |
Ben Dooks | 2261e0e | 2008-12-12 00:24:08 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Ben Dooks | d2b07fe | 2008-12-12 00:24:20 +0000 | [diff] [blame] | 96 | static void s3c_pm_save_uarts(void) |
Ben Dooks | 2261e0e | 2008-12-12 00:24:08 +0000 | [diff] [blame] | 97 | { |
Ben Dooks | d2b07fe | 2008-12-12 00:24:20 +0000 | [diff] [blame] | 98 | struct pm_uart_save *save = uart_save; |
| 99 | unsigned int uart; |
| 100 | |
| 101 | for (uart = 0; uart < CONFIG_SERIAL_SAMSUNG_UARTS; uart++, save++) |
| 102 | s3c_pm_save_uart(uart, save); |
| 103 | } |
| 104 | |
| 105 | static void s3c_pm_restore_uart(unsigned int uart, struct pm_uart_save *save) |
| 106 | { |
| 107 | void __iomem *regs = S3C_VA_UARTx(uart); |
| 108 | |
Ben Dooks | 57699e9 | 2008-12-12 00:24:23 +0000 | [diff] [blame] | 109 | s3c_pm_arch_update_uart(regs, save); |
| 110 | |
Ben Dooks | d2b07fe | 2008-12-12 00:24:20 +0000 | [diff] [blame] | 111 | __raw_writel(save->ulcon, regs + S3C2410_ULCON); |
| 112 | __raw_writel(save->ucon, regs + S3C2410_UCON); |
| 113 | __raw_writel(save->ufcon, regs + S3C2410_UFCON); |
| 114 | __raw_writel(save->umcon, regs + S3C2410_UMCON); |
| 115 | __raw_writel(save->ubrdiv, regs + S3C2410_UBRDIV); |
Ben Dooks | 4b637dc | 2008-12-12 00:24:24 +0000 | [diff] [blame] | 116 | |
| 117 | if (pm_uart_udivslot) |
| 118 | __raw_writel(save->udivslot, regs + S3C2443_DIVSLOT); |
Ben Dooks | d2b07fe | 2008-12-12 00:24:20 +0000 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | static void s3c_pm_restore_uarts(void) |
| 122 | { |
| 123 | struct pm_uart_save *save = uart_save; |
| 124 | unsigned int uart; |
| 125 | |
| 126 | for (uart = 0; uart < CONFIG_SERIAL_SAMSUNG_UARTS; uart++, save++) |
| 127 | s3c_pm_restore_uart(uart, save); |
Ben Dooks | 2261e0e | 2008-12-12 00:24:08 +0000 | [diff] [blame] | 128 | } |
| 129 | #else |
Ben Dooks | d2b07fe | 2008-12-12 00:24:20 +0000 | [diff] [blame] | 130 | static void s3c_pm_save_uarts(void) { } |
| 131 | static void s3c_pm_restore_uarts(void) { } |
Ben Dooks | 2261e0e | 2008-12-12 00:24:08 +0000 | [diff] [blame] | 132 | #endif |
| 133 | |
Ben Dooks | 56b3442 | 2008-12-12 00:24:12 +0000 | [diff] [blame] | 134 | /* The IRQ ext-int code goes here, it is too small to currently bother |
| 135 | * with its own file. */ |
| 136 | |
| 137 | unsigned long s3c_irqwake_intmask = 0xffffffffL; |
| 138 | unsigned long s3c_irqwake_eintmask = 0xffffffffL; |
| 139 | |
Mark Brown | f5aeffb | 2010-12-02 14:35:38 +0900 | [diff] [blame] | 140 | int s3c_irqext_wake(struct irq_data *data, unsigned int state) |
Ben Dooks | 56b3442 | 2008-12-12 00:24:12 +0000 | [diff] [blame] | 141 | { |
Mark Brown | f5aeffb | 2010-12-02 14:35:38 +0900 | [diff] [blame] | 142 | unsigned long bit = 1L << IRQ_EINT_BIT(data->irq); |
Ben Dooks | 56b3442 | 2008-12-12 00:24:12 +0000 | [diff] [blame] | 143 | |
| 144 | if (!(s3c_irqwake_eintallow & bit)) |
| 145 | return -ENOENT; |
| 146 | |
| 147 | printk(KERN_INFO "wake %s for irq %d\n", |
Mark Brown | f5aeffb | 2010-12-02 14:35:38 +0900 | [diff] [blame] | 148 | state ? "enabled" : "disabled", data->irq); |
Ben Dooks | 56b3442 | 2008-12-12 00:24:12 +0000 | [diff] [blame] | 149 | |
| 150 | if (!state) |
| 151 | s3c_irqwake_eintmask |= bit; |
| 152 | else |
| 153 | s3c_irqwake_eintmask &= ~bit; |
| 154 | |
| 155 | return 0; |
| 156 | } |
Ben Dooks | 6419711 | 2008-12-12 00:24:06 +0000 | [diff] [blame] | 157 | |
| 158 | /* helper functions to save and restore register state */ |
| 159 | |
| 160 | /** |
| 161 | * s3c_pm_do_save() - save a set of registers for restoration on resume. |
| 162 | * @ptr: Pointer to an array of registers. |
| 163 | * @count: Size of the ptr array. |
| 164 | * |
| 165 | * Run through the list of registers given, saving their contents in the |
| 166 | * array for later restoration when we wakeup. |
| 167 | */ |
| 168 | void s3c_pm_do_save(struct sleep_save *ptr, int count) |
| 169 | { |
| 170 | for (; count > 0; count--, ptr++) { |
| 171 | ptr->val = __raw_readl(ptr->reg); |
| 172 | S3C_PMDBG("saved %p value %08lx\n", ptr->reg, ptr->val); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * s3c_pm_do_restore() - restore register values from the save list. |
| 178 | * @ptr: Pointer to an array of registers. |
| 179 | * @count: Size of the ptr array. |
| 180 | * |
| 181 | * Restore the register values saved from s3c_pm_do_save(). |
| 182 | * |
| 183 | * Note, we do not use S3C_PMDBG() in here, as the system may not have |
| 184 | * restore the UARTs state yet |
| 185 | */ |
| 186 | |
| 187 | void s3c_pm_do_restore(struct sleep_save *ptr, int count) |
| 188 | { |
| 189 | for (; count > 0; count--, ptr++) { |
| 190 | printk(KERN_DEBUG "restore %p (restore %08lx, was %08x)\n", |
| 191 | ptr->reg, ptr->val, __raw_readl(ptr->reg)); |
| 192 | |
| 193 | __raw_writel(ptr->val, ptr->reg); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * s3c_pm_do_restore_core() - early restore register values from save list. |
| 199 | * |
| 200 | * This is similar to s3c_pm_do_restore() except we try and minimise the |
| 201 | * side effects of the function in case registers that hardware might need |
| 202 | * to work has been restored. |
| 203 | * |
| 204 | * WARNING: Do not put any debug in here that may effect memory or use |
| 205 | * peripherals, as things may be changing! |
| 206 | */ |
| 207 | |
| 208 | void s3c_pm_do_restore_core(struct sleep_save *ptr, int count) |
| 209 | { |
| 210 | for (; count > 0; count--, ptr++) |
| 211 | __raw_writel(ptr->val, ptr->reg); |
| 212 | } |
Ben Dooks | 2261e0e | 2008-12-12 00:24:08 +0000 | [diff] [blame] | 213 | |
| 214 | /* s3c2410_pm_show_resume_irqs |
| 215 | * |
| 216 | * print any IRQs asserted at resume time (ie, we woke from) |
| 217 | */ |
Maurus Cuelenaere | baab730 | 2011-04-02 10:50:22 +0900 | [diff] [blame] | 218 | static void __maybe_unused s3c_pm_show_resume_irqs(int start, |
| 219 | unsigned long which, |
| 220 | unsigned long mask) |
Ben Dooks | 2261e0e | 2008-12-12 00:24:08 +0000 | [diff] [blame] | 221 | { |
| 222 | int i; |
| 223 | |
| 224 | which &= ~mask; |
| 225 | |
| 226 | for (i = 0; i <= 31; i++) { |
| 227 | if (which & (1L<<i)) { |
| 228 | S3C_PMDBG("IRQ %d asserted at resume\n", start+i); |
| 229 | } |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | |
| 234 | void (*pm_cpu_prep)(void); |
Russell King | 29cb3cd | 2011-07-02 09:54:01 +0100 | [diff] [blame] | 235 | int (*pm_cpu_sleep)(unsigned long); |
Ben Dooks | 2261e0e | 2008-12-12 00:24:08 +0000 | [diff] [blame] | 236 | |
| 237 | #define any_allowed(mask, allow) (((mask) & (allow)) != (allow)) |
| 238 | |
| 239 | /* s3c_pm_enter |
| 240 | * |
| 241 | * central control for sleep/resume process |
| 242 | */ |
| 243 | |
| 244 | static int s3c_pm_enter(suspend_state_t state) |
| 245 | { |
Abhilash Kesavan | d3fcacf | 2013-01-25 10:40:19 -0800 | [diff] [blame] | 246 | int ret; |
Ben Dooks | 2261e0e | 2008-12-12 00:24:08 +0000 | [diff] [blame] | 247 | /* ensure the debug is initialised (if enabled) */ |
| 248 | |
| 249 | s3c_pm_debug_init(); |
| 250 | |
| 251 | S3C_PMDBG("%s(%d)\n", __func__, state); |
| 252 | |
| 253 | if (pm_cpu_prep == NULL || pm_cpu_sleep == NULL) { |
| 254 | printk(KERN_ERR "%s: error: no cpu sleep function\n", __func__); |
| 255 | return -EINVAL; |
| 256 | } |
| 257 | |
| 258 | /* check if we have anything to wake-up with... bad things seem |
| 259 | * to happen if you suspend with no wakeup (system will often |
| 260 | * require a full power-cycle) |
| 261 | */ |
| 262 | |
| 263 | if (!any_allowed(s3c_irqwake_intmask, s3c_irqwake_intallow) && |
| 264 | !any_allowed(s3c_irqwake_eintmask, s3c_irqwake_eintallow)) { |
| 265 | printk(KERN_ERR "%s: No wake-up sources!\n", __func__); |
| 266 | printk(KERN_ERR "%s: Aborting sleep\n", __func__); |
| 267 | return -EINVAL; |
| 268 | } |
| 269 | |
Ben Dooks | 2261e0e | 2008-12-12 00:24:08 +0000 | [diff] [blame] | 270 | /* save all necessary core registers not covered by the drivers */ |
| 271 | |
Kukjin Kim | 782d8a3 | 2011-08-30 20:47:32 +0900 | [diff] [blame] | 272 | samsung_pm_save_gpios(); |
| 273 | samsung_pm_saved_gpios(); |
Ben Dooks | d2b07fe | 2008-12-12 00:24:20 +0000 | [diff] [blame] | 274 | s3c_pm_save_uarts(); |
Ben Dooks | 2261e0e | 2008-12-12 00:24:08 +0000 | [diff] [blame] | 275 | s3c_pm_save_core(); |
| 276 | |
| 277 | /* set the irq configuration for wake */ |
| 278 | |
| 279 | s3c_pm_configure_extint(); |
| 280 | |
| 281 | S3C_PMDBG("sleep: irq wakeup masks: %08lx,%08lx\n", |
| 282 | s3c_irqwake_intmask, s3c_irqwake_eintmask); |
| 283 | |
| 284 | s3c_pm_arch_prepare_irqs(); |
| 285 | |
| 286 | /* call cpu specific preparation */ |
| 287 | |
| 288 | pm_cpu_prep(); |
| 289 | |
| 290 | /* flush cache back to ram */ |
| 291 | |
| 292 | flush_cache_all(); |
| 293 | |
| 294 | s3c_pm_check_store(); |
| 295 | |
| 296 | /* send the cpu to sleep... */ |
| 297 | |
| 298 | s3c_pm_arch_stop_clocks(); |
| 299 | |
Russell King | e7089da | 2011-06-21 19:29:26 +0100 | [diff] [blame] | 300 | /* this will also act as our return point from when |
Ben Dooks | fff94cd | 2009-03-10 11:48:07 +0000 | [diff] [blame] | 301 | * we resume as it saves its own register state and restores it |
| 302 | * during the resume. */ |
Ben Dooks | 2261e0e | 2008-12-12 00:24:08 +0000 | [diff] [blame] | 303 | |
Abhilash Kesavan | d3fcacf | 2013-01-25 10:40:19 -0800 | [diff] [blame] | 304 | ret = cpu_suspend(0, pm_cpu_sleep); |
| 305 | if (ret) |
| 306 | return ret; |
Ben Dooks | 2261e0e | 2008-12-12 00:24:08 +0000 | [diff] [blame] | 307 | |
Ben Dooks | 2261e0e | 2008-12-12 00:24:08 +0000 | [diff] [blame] | 308 | /* restore the system state */ |
| 309 | |
| 310 | s3c_pm_restore_core(); |
Ben Dooks | d2b07fe | 2008-12-12 00:24:20 +0000 | [diff] [blame] | 311 | s3c_pm_restore_uarts(); |
Kukjin Kim | 782d8a3 | 2011-08-30 20:47:32 +0900 | [diff] [blame] | 312 | samsung_pm_restore_gpios(); |
Ben Dooks | 1bac282 | 2011-06-01 10:44:50 +0100 | [diff] [blame] | 313 | s3c_pm_restored_gpios(); |
Ben Dooks | 2261e0e | 2008-12-12 00:24:08 +0000 | [diff] [blame] | 314 | |
| 315 | s3c_pm_debug_init(); |
| 316 | |
| 317 | /* check what irq (if any) restored the system */ |
| 318 | |
| 319 | s3c_pm_arch_show_resume_irqs(); |
| 320 | |
| 321 | S3C_PMDBG("%s: post sleep, preparing to return\n", __func__); |
| 322 | |
Ben Dooks | bd117bd | 2009-03-10 18:19:35 +0000 | [diff] [blame] | 323 | /* LEDs should now be 1110 */ |
| 324 | s3c_pm_debug_smdkled(1 << 1, 0); |
| 325 | |
Ben Dooks | 2261e0e | 2008-12-12 00:24:08 +0000 | [diff] [blame] | 326 | s3c_pm_check_restore(); |
| 327 | |
| 328 | /* ok, let's return from sleep */ |
| 329 | |
| 330 | S3C_PMDBG("S3C PM Resume (post-restore)\n"); |
| 331 | return 0; |
| 332 | } |
| 333 | |
Ben Dooks | aa8aba6 | 2008-12-12 00:24:34 +0000 | [diff] [blame] | 334 | static int s3c_pm_prepare(void) |
| 335 | { |
| 336 | /* prepare check area if configured */ |
| 337 | |
| 338 | s3c_pm_check_prepare(); |
| 339 | return 0; |
| 340 | } |
| 341 | |
| 342 | static void s3c_pm_finish(void) |
| 343 | { |
| 344 | s3c_pm_check_cleanup(); |
| 345 | } |
| 346 | |
Lionel Debroux | 2f55ac0 | 2010-11-16 14:14:02 +0100 | [diff] [blame] | 347 | static const struct platform_suspend_ops s3c_pm_ops = { |
Ben Dooks | 2261e0e | 2008-12-12 00:24:08 +0000 | [diff] [blame] | 348 | .enter = s3c_pm_enter, |
Ben Dooks | aa8aba6 | 2008-12-12 00:24:34 +0000 | [diff] [blame] | 349 | .prepare = s3c_pm_prepare, |
| 350 | .finish = s3c_pm_finish, |
Ben Dooks | 2261e0e | 2008-12-12 00:24:08 +0000 | [diff] [blame] | 351 | .valid = suspend_valid_only_mem, |
| 352 | }; |
| 353 | |
Ben Dooks | 4e59c25 | 2008-12-12 00:24:18 +0000 | [diff] [blame] | 354 | /* s3c_pm_init |
Ben Dooks | 2261e0e | 2008-12-12 00:24:08 +0000 | [diff] [blame] | 355 | * |
| 356 | * Attach the power management functions. This should be called |
| 357 | * from the board specific initialisation if the board supports |
| 358 | * it. |
| 359 | */ |
| 360 | |
Ben Dooks | 4e59c25 | 2008-12-12 00:24:18 +0000 | [diff] [blame] | 361 | int __init s3c_pm_init(void) |
Ben Dooks | 2261e0e | 2008-12-12 00:24:08 +0000 | [diff] [blame] | 362 | { |
| 363 | printk("S3C Power Management, Copyright 2004 Simtec Electronics\n"); |
| 364 | |
| 365 | suspend_set_ops(&s3c_pm_ops); |
| 366 | return 0; |
| 367 | } |