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. |
| 4 | * Copyright 2004,2006,2008 Simtec Electronics |
| 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> |
| 23 | #include <mach/hardware.h> |
| 24 | |
| 25 | #include <plat/regs-serial.h> |
| 26 | #include <mach/regs-clock.h> |
| 27 | #include <mach/regs-gpio.h> |
| 28 | #include <mach/regs-mem.h> |
| 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 | 2261e0e | 2008-12-12 00:24:08 +0000 | [diff] [blame] | 33 | #include <plat/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 | 6419711 | 2008-12-12 00:24:06 +0000 | [diff] [blame] | 45 | #ifdef CONFIG_S3C2410_PM_DEBUG |
| 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); |
| 54 | vsprintf(buff, fmt, va); |
| 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 | 6419711 | 2008-12-12 00:24:06 +0000 | [diff] [blame] | 69 | #endif /* CONFIG_S3C2410_PM_DEBUG */ |
| 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 | |
| 73 | #ifdef CONFIG_S3C2410_PM_DEBUG |
| 74 | |
Ben Dooks | d2b07fe | 2008-12-12 00:24:20 +0000 | [diff] [blame] | 75 | struct pm_uart_save uart_save[CONFIG_SERIAL_SAMSUNG_UARTS]; |
Ben Dooks | 2261e0e | 2008-12-12 00:24:08 +0000 | [diff] [blame] | 76 | |
Ben Dooks | d2b07fe | 2008-12-12 00:24:20 +0000 | [diff] [blame] | 77 | 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] | 78 | { |
Ben Dooks | d2b07fe | 2008-12-12 00:24:20 +0000 | [diff] [blame] | 79 | void __iomem *regs = S3C_VA_UARTx(uart); |
| 80 | |
| 81 | save->ulcon = __raw_readl(regs + S3C2410_ULCON); |
| 82 | save->ucon = __raw_readl(regs + S3C2410_UCON); |
| 83 | save->ufcon = __raw_readl(regs + S3C2410_UFCON); |
| 84 | save->umcon = __raw_readl(regs + S3C2410_UMCON); |
| 85 | save->ubrdiv = __raw_readl(regs + S3C2410_UBRDIV); |
Ben Dooks | 57699e9 | 2008-12-12 00:24:23 +0000 | [diff] [blame^] | 86 | |
| 87 | S3C_PMDBG("UART[%d]: ULCON=%04x, UCON=%04x, UFCON=%04x, UBRDIV=%04x\n", |
| 88 | uart, save->ulcon, save->ucon, save->ufcon, save->ubrdiv); |
Ben Dooks | 2261e0e | 2008-12-12 00:24:08 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Ben Dooks | d2b07fe | 2008-12-12 00:24:20 +0000 | [diff] [blame] | 91 | static void s3c_pm_save_uarts(void) |
Ben Dooks | 2261e0e | 2008-12-12 00:24:08 +0000 | [diff] [blame] | 92 | { |
Ben Dooks | d2b07fe | 2008-12-12 00:24:20 +0000 | [diff] [blame] | 93 | struct pm_uart_save *save = uart_save; |
| 94 | unsigned int uart; |
| 95 | |
| 96 | for (uart = 0; uart < CONFIG_SERIAL_SAMSUNG_UARTS; uart++, save++) |
| 97 | s3c_pm_save_uart(uart, save); |
| 98 | } |
| 99 | |
| 100 | static void s3c_pm_restore_uart(unsigned int uart, struct pm_uart_save *save) |
| 101 | { |
| 102 | void __iomem *regs = S3C_VA_UARTx(uart); |
| 103 | |
Ben Dooks | 57699e9 | 2008-12-12 00:24:23 +0000 | [diff] [blame^] | 104 | s3c_pm_arch_update_uart(regs, save); |
| 105 | |
Ben Dooks | d2b07fe | 2008-12-12 00:24:20 +0000 | [diff] [blame] | 106 | __raw_writel(save->ulcon, regs + S3C2410_ULCON); |
| 107 | __raw_writel(save->ucon, regs + S3C2410_UCON); |
| 108 | __raw_writel(save->ufcon, regs + S3C2410_UFCON); |
| 109 | __raw_writel(save->umcon, regs + S3C2410_UMCON); |
| 110 | __raw_writel(save->ubrdiv, regs + S3C2410_UBRDIV); |
| 111 | } |
| 112 | |
| 113 | static void s3c_pm_restore_uarts(void) |
| 114 | { |
| 115 | struct pm_uart_save *save = uart_save; |
| 116 | unsigned int uart; |
| 117 | |
| 118 | for (uart = 0; uart < CONFIG_SERIAL_SAMSUNG_UARTS; uart++, save++) |
| 119 | s3c_pm_restore_uart(uart, save); |
Ben Dooks | 2261e0e | 2008-12-12 00:24:08 +0000 | [diff] [blame] | 120 | } |
| 121 | #else |
Ben Dooks | d2b07fe | 2008-12-12 00:24:20 +0000 | [diff] [blame] | 122 | static void s3c_pm_save_uarts(void) { } |
| 123 | static void s3c_pm_restore_uarts(void) { } |
Ben Dooks | 2261e0e | 2008-12-12 00:24:08 +0000 | [diff] [blame] | 124 | #endif |
| 125 | |
Ben Dooks | 56b3442 | 2008-12-12 00:24:12 +0000 | [diff] [blame] | 126 | /* The IRQ ext-int code goes here, it is too small to currently bother |
| 127 | * with its own file. */ |
| 128 | |
| 129 | unsigned long s3c_irqwake_intmask = 0xffffffffL; |
| 130 | unsigned long s3c_irqwake_eintmask = 0xffffffffL; |
| 131 | |
| 132 | int s3c_irqext_wake(unsigned int irqno, unsigned int state) |
| 133 | { |
| 134 | unsigned long bit = 1L << IRQ_EINT_BIT(irqno); |
| 135 | |
| 136 | if (!(s3c_irqwake_eintallow & bit)) |
| 137 | return -ENOENT; |
| 138 | |
| 139 | printk(KERN_INFO "wake %s for irq %d\n", |
| 140 | state ? "enabled" : "disabled", irqno); |
| 141 | |
| 142 | if (!state) |
| 143 | s3c_irqwake_eintmask |= bit; |
| 144 | else |
| 145 | s3c_irqwake_eintmask &= ~bit; |
| 146 | |
| 147 | return 0; |
| 148 | } |
Ben Dooks | 6419711 | 2008-12-12 00:24:06 +0000 | [diff] [blame] | 149 | |
| 150 | /* helper functions to save and restore register state */ |
| 151 | |
| 152 | /** |
| 153 | * s3c_pm_do_save() - save a set of registers for restoration on resume. |
| 154 | * @ptr: Pointer to an array of registers. |
| 155 | * @count: Size of the ptr array. |
| 156 | * |
| 157 | * Run through the list of registers given, saving their contents in the |
| 158 | * array for later restoration when we wakeup. |
| 159 | */ |
| 160 | void s3c_pm_do_save(struct sleep_save *ptr, int count) |
| 161 | { |
| 162 | for (; count > 0; count--, ptr++) { |
| 163 | ptr->val = __raw_readl(ptr->reg); |
| 164 | S3C_PMDBG("saved %p value %08lx\n", ptr->reg, ptr->val); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * s3c_pm_do_restore() - restore register values from the save list. |
| 170 | * @ptr: Pointer to an array of registers. |
| 171 | * @count: Size of the ptr array. |
| 172 | * |
| 173 | * Restore the register values saved from s3c_pm_do_save(). |
| 174 | * |
| 175 | * Note, we do not use S3C_PMDBG() in here, as the system may not have |
| 176 | * restore the UARTs state yet |
| 177 | */ |
| 178 | |
| 179 | void s3c_pm_do_restore(struct sleep_save *ptr, int count) |
| 180 | { |
| 181 | for (; count > 0; count--, ptr++) { |
| 182 | printk(KERN_DEBUG "restore %p (restore %08lx, was %08x)\n", |
| 183 | ptr->reg, ptr->val, __raw_readl(ptr->reg)); |
| 184 | |
| 185 | __raw_writel(ptr->val, ptr->reg); |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * s3c_pm_do_restore_core() - early restore register values from save list. |
| 191 | * |
| 192 | * This is similar to s3c_pm_do_restore() except we try and minimise the |
| 193 | * side effects of the function in case registers that hardware might need |
| 194 | * to work has been restored. |
| 195 | * |
| 196 | * WARNING: Do not put any debug in here that may effect memory or use |
| 197 | * peripherals, as things may be changing! |
| 198 | */ |
| 199 | |
| 200 | void s3c_pm_do_restore_core(struct sleep_save *ptr, int count) |
| 201 | { |
| 202 | for (; count > 0; count--, ptr++) |
| 203 | __raw_writel(ptr->val, ptr->reg); |
| 204 | } |
Ben Dooks | 2261e0e | 2008-12-12 00:24:08 +0000 | [diff] [blame] | 205 | |
| 206 | /* s3c2410_pm_show_resume_irqs |
| 207 | * |
| 208 | * print any IRQs asserted at resume time (ie, we woke from) |
| 209 | */ |
| 210 | static void s3c_pm_show_resume_irqs(int start, unsigned long which, |
| 211 | unsigned long mask) |
| 212 | { |
| 213 | int i; |
| 214 | |
| 215 | which &= ~mask; |
| 216 | |
| 217 | for (i = 0; i <= 31; i++) { |
| 218 | if (which & (1L<<i)) { |
| 219 | S3C_PMDBG("IRQ %d asserted at resume\n", start+i); |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | |
| 225 | void (*pm_cpu_prep)(void); |
| 226 | void (*pm_cpu_sleep)(void); |
| 227 | |
| 228 | #define any_allowed(mask, allow) (((mask) & (allow)) != (allow)) |
| 229 | |
| 230 | /* s3c_pm_enter |
| 231 | * |
| 232 | * central control for sleep/resume process |
| 233 | */ |
| 234 | |
| 235 | static int s3c_pm_enter(suspend_state_t state) |
| 236 | { |
Ben Dooks | fff94cd | 2009-03-10 11:48:07 +0000 | [diff] [blame] | 237 | static unsigned long regs_save[16]; |
Ben Dooks | 2261e0e | 2008-12-12 00:24:08 +0000 | [diff] [blame] | 238 | |
| 239 | /* ensure the debug is initialised (if enabled) */ |
| 240 | |
| 241 | s3c_pm_debug_init(); |
| 242 | |
| 243 | S3C_PMDBG("%s(%d)\n", __func__, state); |
| 244 | |
| 245 | if (pm_cpu_prep == NULL || pm_cpu_sleep == NULL) { |
| 246 | printk(KERN_ERR "%s: error: no cpu sleep function\n", __func__); |
| 247 | return -EINVAL; |
| 248 | } |
| 249 | |
| 250 | /* check if we have anything to wake-up with... bad things seem |
| 251 | * to happen if you suspend with no wakeup (system will often |
| 252 | * require a full power-cycle) |
| 253 | */ |
| 254 | |
| 255 | if (!any_allowed(s3c_irqwake_intmask, s3c_irqwake_intallow) && |
| 256 | !any_allowed(s3c_irqwake_eintmask, s3c_irqwake_eintallow)) { |
| 257 | printk(KERN_ERR "%s: No wake-up sources!\n", __func__); |
| 258 | printk(KERN_ERR "%s: Aborting sleep\n", __func__); |
| 259 | return -EINVAL; |
| 260 | } |
| 261 | |
Ben Dooks | 2261e0e | 2008-12-12 00:24:08 +0000 | [diff] [blame] | 262 | /* store the physical address of the register recovery block */ |
| 263 | |
| 264 | s3c_sleep_save_phys = virt_to_phys(regs_save); |
| 265 | |
| 266 | S3C_PMDBG("s3c_sleep_save_phys=0x%08lx\n", s3c_sleep_save_phys); |
| 267 | |
| 268 | /* save all necessary core registers not covered by the drivers */ |
| 269 | |
| 270 | s3c_pm_save_gpios(); |
Ben Dooks | d2b07fe | 2008-12-12 00:24:20 +0000 | [diff] [blame] | 271 | s3c_pm_save_uarts(); |
Ben Dooks | 2261e0e | 2008-12-12 00:24:08 +0000 | [diff] [blame] | 272 | s3c_pm_save_core(); |
| 273 | |
| 274 | /* set the irq configuration for wake */ |
| 275 | |
| 276 | s3c_pm_configure_extint(); |
| 277 | |
| 278 | S3C_PMDBG("sleep: irq wakeup masks: %08lx,%08lx\n", |
| 279 | s3c_irqwake_intmask, s3c_irqwake_eintmask); |
| 280 | |
| 281 | s3c_pm_arch_prepare_irqs(); |
| 282 | |
| 283 | /* call cpu specific preparation */ |
| 284 | |
| 285 | pm_cpu_prep(); |
| 286 | |
| 287 | /* flush cache back to ram */ |
| 288 | |
| 289 | flush_cache_all(); |
| 290 | |
| 291 | s3c_pm_check_store(); |
| 292 | |
| 293 | /* send the cpu to sleep... */ |
| 294 | |
| 295 | s3c_pm_arch_stop_clocks(); |
| 296 | |
Ben Dooks | fff94cd | 2009-03-10 11:48:07 +0000 | [diff] [blame] | 297 | /* s3c_cpu_save will also act as our return point from when |
| 298 | * we resume as it saves its own register state and restores it |
| 299 | * during the resume. */ |
Ben Dooks | 2261e0e | 2008-12-12 00:24:08 +0000 | [diff] [blame] | 300 | |
Ben Dooks | fff94cd | 2009-03-10 11:48:07 +0000 | [diff] [blame] | 301 | s3c_cpu_save(regs_save); |
Ben Dooks | 2261e0e | 2008-12-12 00:24:08 +0000 | [diff] [blame] | 302 | |
| 303 | /* restore the cpu state using the kernel's cpu init code. */ |
| 304 | |
| 305 | cpu_init(); |
| 306 | |
| 307 | /* restore the system state */ |
| 308 | |
| 309 | s3c_pm_restore_core(); |
Ben Dooks | d2b07fe | 2008-12-12 00:24:20 +0000 | [diff] [blame] | 310 | s3c_pm_restore_uarts(); |
Ben Dooks | 2261e0e | 2008-12-12 00:24:08 +0000 | [diff] [blame] | 311 | s3c_pm_restore_gpios(); |
| 312 | |
| 313 | s3c_pm_debug_init(); |
| 314 | |
| 315 | /* check what irq (if any) restored the system */ |
| 316 | |
| 317 | s3c_pm_arch_show_resume_irqs(); |
| 318 | |
| 319 | S3C_PMDBG("%s: post sleep, preparing to return\n", __func__); |
| 320 | |
| 321 | s3c_pm_check_restore(); |
| 322 | |
| 323 | /* ok, let's return from sleep */ |
| 324 | |
| 325 | S3C_PMDBG("S3C PM Resume (post-restore)\n"); |
| 326 | return 0; |
| 327 | } |
| 328 | |
Ben Dooks | fff94cd | 2009-03-10 11:48:07 +0000 | [diff] [blame] | 329 | /* callback from assembly code */ |
| 330 | void s3c_pm_cb_flushcache(void) |
| 331 | { |
| 332 | flush_cache_all(); |
| 333 | } |
| 334 | |
Ben Dooks | aa8aba6 | 2008-12-12 00:24:34 +0000 | [diff] [blame] | 335 | static int s3c_pm_prepare(void) |
| 336 | { |
| 337 | /* prepare check area if configured */ |
| 338 | |
| 339 | s3c_pm_check_prepare(); |
| 340 | return 0; |
| 341 | } |
| 342 | |
| 343 | static void s3c_pm_finish(void) |
| 344 | { |
| 345 | s3c_pm_check_cleanup(); |
| 346 | } |
| 347 | |
Ben Dooks | 2261e0e | 2008-12-12 00:24:08 +0000 | [diff] [blame] | 348 | static struct platform_suspend_ops s3c_pm_ops = { |
| 349 | .enter = s3c_pm_enter, |
Ben Dooks | aa8aba6 | 2008-12-12 00:24:34 +0000 | [diff] [blame] | 350 | .prepare = s3c_pm_prepare, |
| 351 | .finish = s3c_pm_finish, |
Ben Dooks | 2261e0e | 2008-12-12 00:24:08 +0000 | [diff] [blame] | 352 | .valid = suspend_valid_only_mem, |
| 353 | }; |
| 354 | |
Ben Dooks | 4e59c25 | 2008-12-12 00:24:18 +0000 | [diff] [blame] | 355 | /* s3c_pm_init |
Ben Dooks | 2261e0e | 2008-12-12 00:24:08 +0000 | [diff] [blame] | 356 | * |
| 357 | * Attach the power management functions. This should be called |
| 358 | * from the board specific initialisation if the board supports |
| 359 | * it. |
| 360 | */ |
| 361 | |
Ben Dooks | 4e59c25 | 2008-12-12 00:24:18 +0000 | [diff] [blame] | 362 | int __init s3c_pm_init(void) |
Ben Dooks | 2261e0e | 2008-12-12 00:24:08 +0000 | [diff] [blame] | 363 | { |
| 364 | printk("S3C Power Management, Copyright 2004 Simtec Electronics\n"); |
| 365 | |
| 366 | suspend_set_ops(&s3c_pm_ops); |
| 367 | return 0; |
| 368 | } |