17426394fSMagnus Damm /* 27426394fSMagnus Damm * arch/sh/kernel/cpu/shmobile/cpuidle.c 37426394fSMagnus Damm * 47426394fSMagnus Damm * Cpuidle support code for SuperH Mobile 57426394fSMagnus Damm * 67426394fSMagnus Damm * Copyright (C) 2009 Magnus Damm 77426394fSMagnus Damm * 87426394fSMagnus Damm * This file is subject to the terms and conditions of the GNU General Public 97426394fSMagnus Damm * License. See the file "COPYING" in the main directory of this archive 107426394fSMagnus Damm * for more details. 117426394fSMagnus Damm */ 127426394fSMagnus Damm #include <linux/init.h> 137426394fSMagnus Damm #include <linux/kernel.h> 147426394fSMagnus Damm #include <linux/io.h> 157426394fSMagnus Damm #include <linux/suspend.h> 167426394fSMagnus Damm #include <linux/cpuidle.h> 177426394fSMagnus Damm #include <asm/suspend.h> 187426394fSMagnus Damm #include <asm/uaccess.h> 197426394fSMagnus Damm #include <asm/hwblk.h> 207426394fSMagnus Damm 217426394fSMagnus Damm static unsigned long cpuidle_mode[] = { 227426394fSMagnus Damm SUSP_SH_SLEEP, /* regular sleep mode */ 237426394fSMagnus Damm SUSP_SH_SLEEP | SUSP_SH_SF, /* sleep mode + self refresh */ 24*63cd91dfSMagnus Damm SUSP_SH_STANDBY | SUSP_SH_SF, /* software standby mode + self refresh */ 257426394fSMagnus Damm }; 267426394fSMagnus Damm 277426394fSMagnus Damm static int cpuidle_sleep_enter(struct cpuidle_device *dev, 287426394fSMagnus Damm struct cpuidle_state *state) 297426394fSMagnus Damm { 307426394fSMagnus Damm unsigned long allowed_mode = arch_hwblk_sleep_mode(); 317426394fSMagnus Damm ktime_t before, after; 327426394fSMagnus Damm int requested_state = state - &dev->states[0]; 337426394fSMagnus Damm int allowed_state; 347426394fSMagnus Damm int k; 357426394fSMagnus Damm 367426394fSMagnus Damm /* convert allowed mode to allowed state */ 377426394fSMagnus Damm for (k = ARRAY_SIZE(cpuidle_mode) - 1; k > 0; k--) 387426394fSMagnus Damm if (cpuidle_mode[k] == allowed_mode) 397426394fSMagnus Damm break; 407426394fSMagnus Damm 417426394fSMagnus Damm allowed_state = k; 427426394fSMagnus Damm 437426394fSMagnus Damm /* take the following into account for sleep mode selection: 447426394fSMagnus Damm * - allowed_state: best mode allowed by hardware (clock deps) 457426394fSMagnus Damm * - requested_state: best mode allowed by software (latencies) 467426394fSMagnus Damm */ 477426394fSMagnus Damm k = min_t(int, allowed_state, requested_state); 487426394fSMagnus Damm 497426394fSMagnus Damm dev->last_state = &dev->states[k]; 507426394fSMagnus Damm before = ktime_get(); 517426394fSMagnus Damm sh_mobile_call_standby(cpuidle_mode[k]); 527426394fSMagnus Damm after = ktime_get(); 537426394fSMagnus Damm return ktime_to_ns(ktime_sub(after, before)) >> 10; 547426394fSMagnus Damm } 557426394fSMagnus Damm 567426394fSMagnus Damm static struct cpuidle_device cpuidle_dev; 577426394fSMagnus Damm static struct cpuidle_driver cpuidle_driver = { 587426394fSMagnus Damm .name = "sh_idle", 597426394fSMagnus Damm .owner = THIS_MODULE, 607426394fSMagnus Damm }; 617426394fSMagnus Damm 627426394fSMagnus Damm void sh_mobile_setup_cpuidle(void) 637426394fSMagnus Damm { 647426394fSMagnus Damm struct cpuidle_device *dev = &cpuidle_dev; 657426394fSMagnus Damm struct cpuidle_state *state; 667426394fSMagnus Damm int i; 677426394fSMagnus Damm 687426394fSMagnus Damm cpuidle_register_driver(&cpuidle_driver); 697426394fSMagnus Damm 707426394fSMagnus Damm for (i = 0; i < CPUIDLE_STATE_MAX; i++) { 717426394fSMagnus Damm dev->states[i].name[0] = '\0'; 727426394fSMagnus Damm dev->states[i].desc[0] = '\0'; 737426394fSMagnus Damm } 747426394fSMagnus Damm 757426394fSMagnus Damm i = CPUIDLE_DRIVER_STATE_START; 767426394fSMagnus Damm 777426394fSMagnus Damm state = &dev->states[i++]; 787426394fSMagnus Damm snprintf(state->name, CPUIDLE_NAME_LEN, "C0"); 797426394fSMagnus Damm strncpy(state->desc, "SuperH Sleep Mode", CPUIDLE_DESC_LEN); 807426394fSMagnus Damm state->exit_latency = 1; 817426394fSMagnus Damm state->target_residency = 1 * 2; 827426394fSMagnus Damm state->power_usage = 3; 837426394fSMagnus Damm state->flags = 0; 847426394fSMagnus Damm state->flags |= CPUIDLE_FLAG_SHALLOW; 857426394fSMagnus Damm state->flags |= CPUIDLE_FLAG_TIME_VALID; 867426394fSMagnus Damm state->enter = cpuidle_sleep_enter; 877426394fSMagnus Damm 887426394fSMagnus Damm dev->safe_state = state; 897426394fSMagnus Damm 907426394fSMagnus Damm state = &dev->states[i++]; 917426394fSMagnus Damm snprintf(state->name, CPUIDLE_NAME_LEN, "C1"); 927426394fSMagnus Damm strncpy(state->desc, "SuperH Sleep Mode [SF]", CPUIDLE_DESC_LEN); 937426394fSMagnus Damm state->exit_latency = 100; 947426394fSMagnus Damm state->target_residency = 1 * 2; 957426394fSMagnus Damm state->power_usage = 1; 967426394fSMagnus Damm state->flags = 0; 977426394fSMagnus Damm state->flags |= CPUIDLE_FLAG_TIME_VALID; 987426394fSMagnus Damm state->enter = cpuidle_sleep_enter; 997426394fSMagnus Damm 100*63cd91dfSMagnus Damm state = &dev->states[i++]; 101*63cd91dfSMagnus Damm snprintf(state->name, CPUIDLE_NAME_LEN, "C2"); 102*63cd91dfSMagnus Damm strncpy(state->desc, "SuperH Mobile Standby Mode [SF]", CPUIDLE_DESC_LEN); 103*63cd91dfSMagnus Damm state->exit_latency = 2300; 104*63cd91dfSMagnus Damm state->target_residency = 1 * 2; 105*63cd91dfSMagnus Damm state->power_usage = 1; 106*63cd91dfSMagnus Damm state->flags = 0; 107*63cd91dfSMagnus Damm state->flags |= CPUIDLE_FLAG_TIME_VALID; 108*63cd91dfSMagnus Damm state->enter = cpuidle_sleep_enter; 109*63cd91dfSMagnus Damm 1107426394fSMagnus Damm dev->state_count = i; 1117426394fSMagnus Damm 1127426394fSMagnus Damm cpuidle_register_device(dev); 1137426394fSMagnus Damm } 114