Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * FR-V Power Management Routines |
| 3 | * |
| 4 | * Copyright (c) 2004 Red Hat, Inc. |
| 5 | * |
| 6 | * Based on SA1100 version: |
| 7 | * Copyright (c) 2001 Cliff Brake <cbrake@accelent.com> |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or |
| 10 | * modify it under the terms of the GNU General Public License. |
| 11 | * |
| 12 | */ |
| 13 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/init.h> |
David Howells | 4023440 | 2006-01-08 01:01:19 -0800 | [diff] [blame] | 15 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/pm.h> |
Jeff Garzik | bca73e4 | 2005-11-13 16:06:25 -0800 | [diff] [blame] | 17 | #include <linux/pm_legacy.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/sched.h> |
| 19 | #include <linux/interrupt.h> |
| 20 | #include <linux/sysctl.h> |
| 21 | #include <linux/errno.h> |
| 22 | #include <linux/delay.h> |
| 23 | #include <asm/uaccess.h> |
| 24 | |
| 25 | #include <asm/mb86943a.h> |
| 26 | |
| 27 | #include "local.h" |
| 28 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | /* |
| 30 | * Debug macros |
| 31 | */ |
| 32 | #define DEBUG |
| 33 | |
| 34 | int pm_do_suspend(void) |
| 35 | { |
| 36 | local_irq_disable(); |
| 37 | |
| 38 | __set_LEDS(0xb1); |
| 39 | |
| 40 | /* go zzz */ |
| 41 | frv_cpu_suspend(pdm_suspend_mode); |
| 42 | |
| 43 | __set_LEDS(0xb2); |
| 44 | |
| 45 | local_irq_enable(); |
| 46 | |
| 47 | return 0; |
| 48 | } |
| 49 | |
| 50 | static unsigned long __irq_mask; |
| 51 | |
| 52 | /* |
| 53 | * Setup interrupt masks, etc to enable wakeup by power switch |
| 54 | */ |
| 55 | static void __default_power_switch_setup(void) |
| 56 | { |
| 57 | /* default is to mask all interrupt sources. */ |
| 58 | __irq_mask = *(unsigned long *)0xfeff9820; |
| 59 | *(unsigned long *)0xfeff9820 = 0xfffe0000; |
| 60 | } |
| 61 | |
| 62 | /* |
| 63 | * Cleanup interrupt masks, etc after wakeup by power switch |
| 64 | */ |
| 65 | static void __default_power_switch_cleanup(void) |
| 66 | { |
| 67 | *(unsigned long *)0xfeff9820 = __irq_mask; |
| 68 | } |
| 69 | |
| 70 | /* |
| 71 | * Return non-zero if wakeup irq was caused by power switch |
| 72 | */ |
| 73 | static int __default_power_switch_check(void) |
| 74 | { |
| 75 | return 1; |
| 76 | } |
| 77 | |
| 78 | void (*__power_switch_wake_setup)(void) = __default_power_switch_setup; |
| 79 | int (*__power_switch_wake_check)(void) = __default_power_switch_check; |
| 80 | void (*__power_switch_wake_cleanup)(void) = __default_power_switch_cleanup; |
| 81 | |
| 82 | int pm_do_bus_sleep(void) |
| 83 | { |
| 84 | local_irq_disable(); |
| 85 | |
| 86 | /* |
| 87 | * Here is where we need some platform-dependent setup |
| 88 | * of the interrupt state so that appropriate wakeup |
| 89 | * sources are allowed and all others are masked. |
| 90 | */ |
| 91 | __power_switch_wake_setup(); |
| 92 | |
| 93 | __set_LEDS(0xa1); |
| 94 | |
| 95 | /* go zzz |
| 96 | * |
| 97 | * This is in a loop in case power switch shares an irq with other |
| 98 | * devices. The wake_check() tells us if we need to finish waking |
| 99 | * or go back to sleep. |
| 100 | */ |
| 101 | do { |
| 102 | frv_cpu_suspend(HSR0_PDM_BUS_SLEEP); |
| 103 | } while (__power_switch_wake_check && !__power_switch_wake_check()); |
| 104 | |
| 105 | __set_LEDS(0xa2); |
| 106 | |
| 107 | /* |
| 108 | * Here is where we need some platform-dependent restore |
| 109 | * of the interrupt state prior to being called. |
| 110 | */ |
| 111 | __power_switch_wake_cleanup(); |
| 112 | |
| 113 | local_irq_enable(); |
| 114 | |
| 115 | return 0; |
| 116 | } |
| 117 | |
| 118 | unsigned long sleep_phys_sp(void *sp) |
| 119 | { |
| 120 | return virt_to_phys(sp); |
| 121 | } |
| 122 | |
| 123 | #ifdef CONFIG_SYSCTL |
| 124 | /* |
| 125 | * Use a temporary sysctl number. Horrid, but will be cleaned up in 2.6 |
| 126 | * when all the PM interfaces exist nicely. |
| 127 | */ |
| 128 | #define CTL_PM 9899 |
| 129 | #define CTL_PM_SUSPEND 1 |
| 130 | #define CTL_PM_CMODE 2 |
| 131 | #define CTL_PM_P0 4 |
| 132 | #define CTL_PM_CM 5 |
| 133 | |
Al Viro | 7ab76d7 | 2006-06-23 02:04:05 -0700 | [diff] [blame] | 134 | static int user_atoi(char __user *ubuf, size_t len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | { |
| 136 | char buf[16]; |
| 137 | unsigned long ret; |
| 138 | |
| 139 | if (len > 15) |
| 140 | return -EINVAL; |
| 141 | |
| 142 | if (copy_from_user(buf, ubuf, len)) |
| 143 | return -EFAULT; |
| 144 | |
| 145 | buf[len] = 0; |
| 146 | ret = simple_strtoul(buf, NULL, 0); |
| 147 | if (ret > INT_MAX) |
| 148 | return -ERANGE; |
| 149 | return ret; |
| 150 | } |
| 151 | |
| 152 | /* |
| 153 | * Send us to sleep. |
| 154 | */ |
| 155 | static int sysctl_pm_do_suspend(ctl_table *ctl, int write, struct file *filp, |
Al Viro | 7ab76d7 | 2006-06-23 02:04:05 -0700 | [diff] [blame] | 156 | void __user *buffer, size_t *lenp, loff_t *fpos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | { |
| 158 | int retval, mode; |
| 159 | |
| 160 | if (*lenp <= 0) |
| 161 | return -EIO; |
| 162 | |
| 163 | mode = user_atoi(buffer, *lenp); |
| 164 | if ((mode != 1) && (mode != 5)) |
| 165 | return -EINVAL; |
| 166 | |
| 167 | retval = pm_send_all(PM_SUSPEND, (void *)3); |
| 168 | |
| 169 | if (retval == 0) { |
| 170 | if (mode == 5) |
| 171 | retval = pm_do_bus_sleep(); |
| 172 | else |
| 173 | retval = pm_do_suspend(); |
| 174 | pm_send_all(PM_RESUME, (void *)0); |
| 175 | } |
| 176 | |
| 177 | return retval; |
| 178 | } |
| 179 | |
| 180 | static int try_set_cmode(int new_cmode) |
| 181 | { |
| 182 | if (new_cmode > 15) |
| 183 | return -EINVAL; |
| 184 | if (!(clock_cmodes_permitted & (1<<new_cmode))) |
| 185 | return -EINVAL; |
| 186 | |
| 187 | /* tell all the drivers we're suspending */ |
| 188 | pm_send_all(PM_SUSPEND, (void *)3); |
| 189 | |
| 190 | /* now change cmode */ |
| 191 | local_irq_disable(); |
| 192 | frv_dma_pause_all(); |
| 193 | |
| 194 | frv_change_cmode(new_cmode); |
| 195 | |
| 196 | determine_clocks(0); |
| 197 | time_divisor_init(); |
| 198 | |
| 199 | #ifdef DEBUG |
| 200 | determine_clocks(1); |
| 201 | #endif |
| 202 | frv_dma_resume_all(); |
| 203 | local_irq_enable(); |
| 204 | |
| 205 | /* tell all the drivers we're resuming */ |
| 206 | pm_send_all(PM_RESUME, (void *)0); |
| 207 | return 0; |
| 208 | } |
| 209 | |
| 210 | |
| 211 | static int cmode_procctl(ctl_table *ctl, int write, struct file *filp, |
Al Viro | 7ab76d7 | 2006-06-23 02:04:05 -0700 | [diff] [blame] | 212 | void __user *buffer, size_t *lenp, loff_t *fpos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | { |
| 214 | int new_cmode; |
| 215 | |
| 216 | if (!write) |
| 217 | return proc_dointvec(ctl, write, filp, buffer, lenp, fpos); |
| 218 | |
| 219 | new_cmode = user_atoi(buffer, *lenp); |
| 220 | |
| 221 | return try_set_cmode(new_cmode)?:*lenp; |
| 222 | } |
| 223 | |
Al Viro | 7ab76d7 | 2006-06-23 02:04:05 -0700 | [diff] [blame] | 224 | static int cmode_sysctl(ctl_table *table, int __user *name, int nlen, |
| 225 | void __user *oldval, size_t __user *oldlenp, |
| 226 | void __user *newval, size_t newlen, void **context) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | { |
| 228 | if (oldval && oldlenp) { |
| 229 | size_t oldlen; |
| 230 | |
| 231 | if (get_user(oldlen, oldlenp)) |
| 232 | return -EFAULT; |
| 233 | |
| 234 | if (oldlen != sizeof(int)) |
| 235 | return -EINVAL; |
| 236 | |
Al Viro | 7ab76d7 | 2006-06-23 02:04:05 -0700 | [diff] [blame] | 237 | if (put_user(clock_cmode_current, (unsigned __user *)oldval) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | put_user(sizeof(int), oldlenp)) |
| 239 | return -EFAULT; |
| 240 | } |
| 241 | if (newval && newlen) { |
| 242 | int new_cmode; |
| 243 | |
| 244 | if (newlen != sizeof(int)) |
| 245 | return -EINVAL; |
| 246 | |
Al Viro | 7ab76d7 | 2006-06-23 02:04:05 -0700 | [diff] [blame] | 247 | if (get_user(new_cmode, (int __user *)newval)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | return -EFAULT; |
| 249 | |
| 250 | return try_set_cmode(new_cmode)?:1; |
| 251 | } |
| 252 | return 1; |
| 253 | } |
| 254 | |
| 255 | static int try_set_p0(int new_p0) |
| 256 | { |
| 257 | unsigned long flags, clkc; |
| 258 | |
| 259 | if (new_p0 < 0 || new_p0 > 1) |
| 260 | return -EINVAL; |
| 261 | |
| 262 | local_irq_save(flags); |
| 263 | __set_PSR(flags & ~PSR_ET); |
| 264 | |
| 265 | frv_dma_pause_all(); |
| 266 | |
| 267 | clkc = __get_CLKC(); |
| 268 | if (new_p0) |
| 269 | clkc |= CLKC_P0; |
| 270 | else |
| 271 | clkc &= ~CLKC_P0; |
| 272 | __set_CLKC(clkc); |
| 273 | |
| 274 | determine_clocks(0); |
| 275 | time_divisor_init(); |
| 276 | |
| 277 | #ifdef DEBUG |
| 278 | determine_clocks(1); |
| 279 | #endif |
| 280 | frv_dma_resume_all(); |
| 281 | local_irq_restore(flags); |
| 282 | return 0; |
| 283 | } |
| 284 | |
| 285 | static int try_set_cm(int new_cm) |
| 286 | { |
| 287 | unsigned long flags, clkc; |
| 288 | |
| 289 | if (new_cm < 0 || new_cm > 1) |
| 290 | return -EINVAL; |
| 291 | |
| 292 | local_irq_save(flags); |
| 293 | __set_PSR(flags & ~PSR_ET); |
| 294 | |
| 295 | frv_dma_pause_all(); |
| 296 | |
| 297 | clkc = __get_CLKC(); |
| 298 | clkc &= ~CLKC_CM; |
| 299 | clkc |= new_cm; |
| 300 | __set_CLKC(clkc); |
| 301 | |
| 302 | determine_clocks(0); |
| 303 | time_divisor_init(); |
| 304 | |
| 305 | #if 1 //def DEBUG |
| 306 | determine_clocks(1); |
| 307 | #endif |
| 308 | |
| 309 | frv_dma_resume_all(); |
| 310 | local_irq_restore(flags); |
| 311 | return 0; |
| 312 | } |
| 313 | |
| 314 | static int p0_procctl(ctl_table *ctl, int write, struct file *filp, |
Al Viro | 7ab76d7 | 2006-06-23 02:04:05 -0700 | [diff] [blame] | 315 | void __user *buffer, size_t *lenp, loff_t *fpos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | { |
| 317 | int new_p0; |
| 318 | |
| 319 | if (!write) |
| 320 | return proc_dointvec(ctl, write, filp, buffer, lenp, fpos); |
| 321 | |
| 322 | new_p0 = user_atoi(buffer, *lenp); |
| 323 | |
| 324 | return try_set_p0(new_p0)?:*lenp; |
| 325 | } |
| 326 | |
Al Viro | 7ab76d7 | 2006-06-23 02:04:05 -0700 | [diff] [blame] | 327 | static int p0_sysctl(ctl_table *table, int __user *name, int nlen, |
| 328 | void __user *oldval, size_t __user *oldlenp, |
| 329 | void __user *newval, size_t newlen, void **context) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | { |
| 331 | if (oldval && oldlenp) { |
| 332 | size_t oldlen; |
| 333 | |
| 334 | if (get_user(oldlen, oldlenp)) |
| 335 | return -EFAULT; |
| 336 | |
| 337 | if (oldlen != sizeof(int)) |
| 338 | return -EINVAL; |
| 339 | |
Al Viro | 7ab76d7 | 2006-06-23 02:04:05 -0700 | [diff] [blame] | 340 | if (put_user(clock_p0_current, (unsigned __user *)oldval) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | put_user(sizeof(int), oldlenp)) |
| 342 | return -EFAULT; |
| 343 | } |
| 344 | if (newval && newlen) { |
| 345 | int new_p0; |
| 346 | |
| 347 | if (newlen != sizeof(int)) |
| 348 | return -EINVAL; |
| 349 | |
Al Viro | 7ab76d7 | 2006-06-23 02:04:05 -0700 | [diff] [blame] | 350 | if (get_user(new_p0, (int __user *)newval)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | return -EFAULT; |
| 352 | |
| 353 | return try_set_p0(new_p0)?:1; |
| 354 | } |
| 355 | return 1; |
| 356 | } |
| 357 | |
| 358 | static int cm_procctl(ctl_table *ctl, int write, struct file *filp, |
Al Viro | 7ab76d7 | 2006-06-23 02:04:05 -0700 | [diff] [blame] | 359 | void __user *buffer, size_t *lenp, loff_t *fpos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | { |
| 361 | int new_cm; |
| 362 | |
| 363 | if (!write) |
| 364 | return proc_dointvec(ctl, write, filp, buffer, lenp, fpos); |
| 365 | |
| 366 | new_cm = user_atoi(buffer, *lenp); |
| 367 | |
| 368 | return try_set_cm(new_cm)?:*lenp; |
| 369 | } |
| 370 | |
Al Viro | 7ab76d7 | 2006-06-23 02:04:05 -0700 | [diff] [blame] | 371 | static int cm_sysctl(ctl_table *table, int __user *name, int nlen, |
| 372 | void __user *oldval, size_t __user *oldlenp, |
| 373 | void __user *newval, size_t newlen, void **context) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | { |
| 375 | if (oldval && oldlenp) { |
| 376 | size_t oldlen; |
| 377 | |
| 378 | if (get_user(oldlen, oldlenp)) |
| 379 | return -EFAULT; |
| 380 | |
| 381 | if (oldlen != sizeof(int)) |
| 382 | return -EINVAL; |
| 383 | |
Al Viro | 7ab76d7 | 2006-06-23 02:04:05 -0700 | [diff] [blame] | 384 | if (put_user(clock_cm_current, (unsigned __user *)oldval) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | put_user(sizeof(int), oldlenp)) |
| 386 | return -EFAULT; |
| 387 | } |
| 388 | if (newval && newlen) { |
| 389 | int new_cm; |
| 390 | |
| 391 | if (newlen != sizeof(int)) |
| 392 | return -EINVAL; |
| 393 | |
Al Viro | 7ab76d7 | 2006-06-23 02:04:05 -0700 | [diff] [blame] | 394 | if (get_user(new_cm, (int __user *)newval)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | return -EFAULT; |
| 396 | |
| 397 | return try_set_cm(new_cm)?:1; |
| 398 | } |
| 399 | return 1; |
| 400 | } |
| 401 | |
| 402 | |
| 403 | static struct ctl_table pm_table[] = |
| 404 | { |
| 405 | {CTL_PM_SUSPEND, "suspend", NULL, 0, 0200, NULL, &sysctl_pm_do_suspend}, |
| 406 | {CTL_PM_CMODE, "cmode", &clock_cmode_current, sizeof(int), 0644, NULL, &cmode_procctl, &cmode_sysctl, NULL}, |
| 407 | {CTL_PM_P0, "p0", &clock_p0_current, sizeof(int), 0644, NULL, &p0_procctl, &p0_sysctl, NULL}, |
| 408 | {CTL_PM_CM, "cm", &clock_cm_current, sizeof(int), 0644, NULL, &cm_procctl, &cm_sysctl, NULL}, |
| 409 | {0} |
| 410 | }; |
| 411 | |
| 412 | static struct ctl_table pm_dir_table[] = |
| 413 | { |
| 414 | {CTL_PM, "pm", NULL, 0, 0555, pm_table}, |
| 415 | {0} |
| 416 | }; |
| 417 | |
| 418 | /* |
| 419 | * Initialize power interface |
| 420 | */ |
| 421 | static int __init pm_init(void) |
| 422 | { |
| 423 | register_sysctl_table(pm_dir_table, 1); |
| 424 | return 0; |
| 425 | } |
| 426 | |
| 427 | __initcall(pm_init); |
| 428 | |
| 429 | #endif |