Sekhar Nori | a6c0f6e | 2009-11-03 15:14:13 +0530 | [diff] [blame] | 1 | /* |
| 2 | * CPU idle for DaVinci SoCs |
| 3 | * |
| 4 | * Copyright (C) 2009 Texas Instruments Incorporated. http://www.ti.com/ |
| 5 | * |
| 6 | * Derived from Marvell Kirkwood CPU idle code |
| 7 | * (arch/arm/mach-kirkwood/cpuidle.c) |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License version 2 as |
| 11 | * published by the Free Software Foundation. |
| 12 | */ |
| 13 | |
| 14 | #include <linux/kernel.h> |
| 15 | #include <linux/init.h> |
| 16 | #include <linux/platform_device.h> |
| 17 | #include <linux/cpuidle.h> |
| 18 | #include <linux/io.h> |
Paul Gortmaker | dc28094 | 2011-07-31 16:17:29 -0400 | [diff] [blame] | 19 | #include <linux/export.h> |
Sekhar Nori | a6c0f6e | 2009-11-03 15:14:13 +0530 | [diff] [blame] | 20 | #include <asm/proc-fns.h> |
Robert Lee | 19976c2 | 2012-03-20 15:22:45 -0500 | [diff] [blame^] | 21 | #include <asm/cpuidle.h> |
Sekhar Nori | a6c0f6e | 2009-11-03 15:14:13 +0530 | [diff] [blame] | 22 | |
| 23 | #include <mach/cpuidle.h> |
Nicolas Pitre | 0020afb | 2011-07-05 22:52:57 -0400 | [diff] [blame] | 24 | #include <mach/ddr2.h> |
Sekhar Nori | a6c0f6e | 2009-11-03 15:14:13 +0530 | [diff] [blame] | 25 | |
| 26 | #define DAVINCI_CPUIDLE_MAX_STATES 2 |
| 27 | |
| 28 | struct davinci_ops { |
| 29 | void (*enter) (u32 flags); |
| 30 | void (*exit) (u32 flags); |
| 31 | u32 flags; |
| 32 | }; |
| 33 | |
Robert Lee | 19976c2 | 2012-03-20 15:22:45 -0500 | [diff] [blame^] | 34 | /* Actual code that puts the SoC in different idle states */ |
| 35 | static int davinci_enter_idle(struct cpuidle_device *dev, |
| 36 | struct cpuidle_driver *drv, |
| 37 | int index) |
| 38 | { |
| 39 | struct cpuidle_state_usage *state_usage = &dev->states_usage[index]; |
| 40 | struct davinci_ops *ops = cpuidle_get_statedata(state_usage); |
| 41 | |
| 42 | if (ops && ops->enter) |
| 43 | ops->enter(ops->flags); |
| 44 | |
| 45 | index = cpuidle_wrap_enter(dev, drv, index, |
| 46 | arm_cpuidle_simple_enter); |
| 47 | |
| 48 | if (ops && ops->exit) |
| 49 | ops->exit(ops->flags); |
| 50 | |
| 51 | return index; |
| 52 | } |
| 53 | |
Sekhar Nori | a6c0f6e | 2009-11-03 15:14:13 +0530 | [diff] [blame] | 54 | /* fields in davinci_ops.flags */ |
| 55 | #define DAVINCI_CPUIDLE_FLAGS_DDR2_PWDN BIT(0) |
| 56 | |
| 57 | static struct cpuidle_driver davinci_idle_driver = { |
Robert Lee | 19976c2 | 2012-03-20 15:22:45 -0500 | [diff] [blame^] | 58 | .name = "cpuidle-davinci", |
| 59 | .owner = THIS_MODULE, |
| 60 | .states[0] = ARM_CPUIDLE_WFI_STATE, |
| 61 | .states[1] = { |
| 62 | .enter = davinci_enter_idle, |
| 63 | .exit_latency = 10, |
| 64 | .target_residency = 100000, |
| 65 | .flags = CPUIDLE_FLAG_TIME_VALID, |
| 66 | .name = "DDR SR", |
| 67 | .desc = "WFI and DDR Self Refresh", |
| 68 | }, |
| 69 | .state_count = DAVINCI_CPUIDLE_MAX_STATES, |
Sekhar Nori | a6c0f6e | 2009-11-03 15:14:13 +0530 | [diff] [blame] | 70 | }; |
| 71 | |
| 72 | static DEFINE_PER_CPU(struct cpuidle_device, davinci_cpuidle_device); |
| 73 | static void __iomem *ddr2_reg_base; |
| 74 | |
Sekhar Nori | a6c0f6e | 2009-11-03 15:14:13 +0530 | [diff] [blame] | 75 | static void davinci_save_ddr_power(int enter, bool pdown) |
| 76 | { |
| 77 | u32 val; |
| 78 | |
| 79 | val = __raw_readl(ddr2_reg_base + DDR2_SDRCR_OFFSET); |
| 80 | |
| 81 | if (enter) { |
| 82 | if (pdown) |
| 83 | val |= DDR2_SRPD_BIT; |
| 84 | else |
| 85 | val &= ~DDR2_SRPD_BIT; |
| 86 | val |= DDR2_LPMODEN_BIT; |
| 87 | } else { |
| 88 | val &= ~(DDR2_SRPD_BIT | DDR2_LPMODEN_BIT); |
| 89 | } |
| 90 | |
| 91 | __raw_writel(val, ddr2_reg_base + DDR2_SDRCR_OFFSET); |
| 92 | } |
| 93 | |
| 94 | static void davinci_c2state_enter(u32 flags) |
| 95 | { |
| 96 | davinci_save_ddr_power(1, !!(flags & DAVINCI_CPUIDLE_FLAGS_DDR2_PWDN)); |
| 97 | } |
| 98 | |
| 99 | static void davinci_c2state_exit(u32 flags) |
| 100 | { |
| 101 | davinci_save_ddr_power(0, !!(flags & DAVINCI_CPUIDLE_FLAGS_DDR2_PWDN)); |
| 102 | } |
| 103 | |
| 104 | static struct davinci_ops davinci_states[DAVINCI_CPUIDLE_MAX_STATES] = { |
| 105 | [1] = { |
| 106 | .enter = davinci_c2state_enter, |
| 107 | .exit = davinci_c2state_exit, |
| 108 | }, |
| 109 | }; |
| 110 | |
Sekhar Nori | a6c0f6e | 2009-11-03 15:14:13 +0530 | [diff] [blame] | 111 | static int __init davinci_cpuidle_probe(struct platform_device *pdev) |
| 112 | { |
| 113 | int ret; |
| 114 | struct cpuidle_device *device; |
| 115 | struct davinci_cpuidle_config *pdata = pdev->dev.platform_data; |
Sekhar Nori | a6c0f6e | 2009-11-03 15:14:13 +0530 | [diff] [blame] | 116 | |
| 117 | device = &per_cpu(davinci_cpuidle_device, smp_processor_id()); |
| 118 | |
| 119 | if (!pdata) { |
| 120 | dev_err(&pdev->dev, "cannot get platform data\n"); |
| 121 | return -ENOENT; |
| 122 | } |
| 123 | |
Sekhar Nori | 948c66d | 2009-11-16 17:21:37 +0530 | [diff] [blame] | 124 | ddr2_reg_base = pdata->ddr2_ctlr_base; |
Sekhar Nori | a6c0f6e | 2009-11-03 15:14:13 +0530 | [diff] [blame] | 125 | |
Sekhar Nori | a6c0f6e | 2009-11-03 15:14:13 +0530 | [diff] [blame] | 126 | if (pdata->ddr2_pdown) |
| 127 | davinci_states[1].flags |= DAVINCI_CPUIDLE_FLAGS_DDR2_PWDN; |
Deepthi Dharwar | 4202735 | 2011-10-28 16:20:33 +0530 | [diff] [blame] | 128 | cpuidle_set_statedata(&device->states_usage[1], &davinci_states[1]); |
Sekhar Nori | a6c0f6e | 2009-11-03 15:14:13 +0530 | [diff] [blame] | 129 | |
| 130 | device->state_count = DAVINCI_CPUIDLE_MAX_STATES; |
Deepthi Dharwar | 46bcfad | 2011-10-28 16:20:42 +0530 | [diff] [blame] | 131 | |
Sekhar Nori | a6c0f6e | 2009-11-03 15:14:13 +0530 | [diff] [blame] | 132 | ret = cpuidle_register_driver(&davinci_idle_driver); |
| 133 | if (ret) { |
| 134 | dev_err(&pdev->dev, "failed to register driver\n"); |
| 135 | return ret; |
| 136 | } |
| 137 | |
Sekhar Nori | a6c0f6e | 2009-11-03 15:14:13 +0530 | [diff] [blame] | 138 | ret = cpuidle_register_device(device); |
| 139 | if (ret) { |
| 140 | dev_err(&pdev->dev, "failed to register device\n"); |
Sekhar Nori | 948c66d | 2009-11-16 17:21:37 +0530 | [diff] [blame] | 141 | cpuidle_unregister_driver(&davinci_idle_driver); |
| 142 | return ret; |
Sekhar Nori | a6c0f6e | 2009-11-03 15:14:13 +0530 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | return 0; |
Sekhar Nori | a6c0f6e | 2009-11-03 15:14:13 +0530 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | static struct platform_driver davinci_cpuidle_driver = { |
| 149 | .driver = { |
| 150 | .name = "cpuidle-davinci", |
| 151 | .owner = THIS_MODULE, |
| 152 | }, |
| 153 | }; |
| 154 | |
| 155 | static int __init davinci_cpuidle_init(void) |
| 156 | { |
| 157 | return platform_driver_probe(&davinci_cpuidle_driver, |
| 158 | davinci_cpuidle_probe); |
| 159 | } |
| 160 | device_initcall(davinci_cpuidle_init); |
| 161 | |