Sekhar Nori | efc1bb8 | 2009-12-17 18:29:31 +0530 | [diff] [blame] | 1 | /* |
| 2 | * DaVinci Power Management Routines |
| 3 | * |
| 4 | * Copyright (C) 2009 Texas Instruments, Inc. http://www.ti.com/ |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2 as |
| 8 | * published by the Free Software Foundation. |
| 9 | */ |
| 10 | |
| 11 | #include <linux/pm.h> |
| 12 | #include <linux/suspend.h> |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/platform_device.h> |
| 15 | #include <linux/clk.h> |
| 16 | #include <linux/spinlock.h> |
| 17 | |
| 18 | #include <asm/cacheflush.h> |
| 19 | #include <asm/delay.h> |
Alexey Dobriyan | b7f080c | 2011-06-16 11:01:34 +0000 | [diff] [blame] | 20 | #include <asm/io.h> |
Sekhar Nori | efc1bb8 | 2009-12-17 18:29:31 +0530 | [diff] [blame] | 21 | |
Sekhar Nori | 215a084 | 2013-04-10 14:57:15 +0530 | [diff] [blame] | 22 | #include <mach/common.h> |
Sekhar Nori | efc1bb8 | 2009-12-17 18:29:31 +0530 | [diff] [blame] | 23 | #include <mach/da8xx.h> |
Kevin Hilman | aa9aa1e | 2016-11-15 11:54:19 -0800 | [diff] [blame] | 24 | #include <mach/mux.h> |
Sekhar Nori | efc1bb8 | 2009-12-17 18:29:31 +0530 | [diff] [blame] | 25 | #include <mach/pm.h> |
| 26 | |
| 27 | #include "clock.h" |
Kevin Hilman | aa9aa1e | 2016-11-15 11:54:19 -0800 | [diff] [blame] | 28 | #include "psc.h" |
| 29 | #include "sram.h" |
Sekhar Nori | efc1bb8 | 2009-12-17 18:29:31 +0530 | [diff] [blame] | 30 | |
Kevin Hilman | aa9aa1e | 2016-11-15 11:54:19 -0800 | [diff] [blame] | 31 | #define DA850_PLL1_BASE 0x01e1a000 |
Sekhar Nori | efc1bb8 | 2009-12-17 18:29:31 +0530 | [diff] [blame] | 32 | #define DEEPSLEEP_SLEEPCOUNT_MASK 0xFFFF |
Kevin Hilman | aa9aa1e | 2016-11-15 11:54:19 -0800 | [diff] [blame] | 33 | #define DEEPSLEEP_SLEEPCOUNT 128 |
Sekhar Nori | efc1bb8 | 2009-12-17 18:29:31 +0530 | [diff] [blame] | 34 | |
| 35 | static void (*davinci_sram_suspend) (struct davinci_pm_config *); |
Kevin Hilman | aa9aa1e | 2016-11-15 11:54:19 -0800 | [diff] [blame] | 36 | static struct davinci_pm_config pm_config = { |
| 37 | .sleepcount = DEEPSLEEP_SLEEPCOUNT, |
| 38 | .ddrpsc_num = DA8XX_LPSC1_EMIF3C, |
| 39 | }; |
| 40 | |
Sekhar Nori | efc1bb8 | 2009-12-17 18:29:31 +0530 | [diff] [blame] | 41 | static void davinci_sram_push(void *dest, void *src, unsigned int size) |
| 42 | { |
| 43 | memcpy(dest, src, size); |
| 44 | flush_icache_range((unsigned long)dest, (unsigned long)(dest + size)); |
| 45 | } |
| 46 | |
| 47 | static void davinci_pm_suspend(void) |
| 48 | { |
| 49 | unsigned val; |
| 50 | |
Kevin Hilman | 1428ed1 | 2016-11-15 11:54:20 -0800 | [diff] [blame] | 51 | if (pm_config.cpupll_reg_base != pm_config.ddrpll_reg_base) { |
Sekhar Nori | efc1bb8 | 2009-12-17 18:29:31 +0530 | [diff] [blame] | 52 | |
| 53 | /* Switch CPU PLL to bypass mode */ |
Kevin Hilman | 1428ed1 | 2016-11-15 11:54:20 -0800 | [diff] [blame] | 54 | val = __raw_readl(pm_config.cpupll_reg_base + PLLCTL); |
Sekhar Nori | efc1bb8 | 2009-12-17 18:29:31 +0530 | [diff] [blame] | 55 | val &= ~(PLLCTL_PLLENSRC | PLLCTL_PLLEN); |
Kevin Hilman | 1428ed1 | 2016-11-15 11:54:20 -0800 | [diff] [blame] | 56 | __raw_writel(val, pm_config.cpupll_reg_base + PLLCTL); |
Sekhar Nori | efc1bb8 | 2009-12-17 18:29:31 +0530 | [diff] [blame] | 57 | |
| 58 | udelay(PLL_BYPASS_TIME); |
| 59 | |
| 60 | /* Powerdown CPU PLL */ |
Kevin Hilman | 1428ed1 | 2016-11-15 11:54:20 -0800 | [diff] [blame] | 61 | val = __raw_readl(pm_config.cpupll_reg_base + PLLCTL); |
Sekhar Nori | efc1bb8 | 2009-12-17 18:29:31 +0530 | [diff] [blame] | 62 | val |= PLLCTL_PLLPWRDN; |
Kevin Hilman | 1428ed1 | 2016-11-15 11:54:20 -0800 | [diff] [blame] | 63 | __raw_writel(val, pm_config.cpupll_reg_base + PLLCTL); |
Sekhar Nori | efc1bb8 | 2009-12-17 18:29:31 +0530 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | /* Configure sleep count in deep sleep register */ |
Kevin Hilman | 1428ed1 | 2016-11-15 11:54:20 -0800 | [diff] [blame] | 67 | val = __raw_readl(pm_config.deepsleep_reg); |
Sekhar Nori | efc1bb8 | 2009-12-17 18:29:31 +0530 | [diff] [blame] | 68 | val &= ~DEEPSLEEP_SLEEPCOUNT_MASK, |
Kevin Hilman | 1428ed1 | 2016-11-15 11:54:20 -0800 | [diff] [blame] | 69 | val |= pm_config.sleepcount; |
| 70 | __raw_writel(val, pm_config.deepsleep_reg); |
Sekhar Nori | efc1bb8 | 2009-12-17 18:29:31 +0530 | [diff] [blame] | 71 | |
| 72 | /* System goes to sleep in this call */ |
Kevin Hilman | 1428ed1 | 2016-11-15 11:54:20 -0800 | [diff] [blame] | 73 | davinci_sram_suspend(&pm_config); |
Sekhar Nori | efc1bb8 | 2009-12-17 18:29:31 +0530 | [diff] [blame] | 74 | |
Kevin Hilman | 1428ed1 | 2016-11-15 11:54:20 -0800 | [diff] [blame] | 75 | if (pm_config.cpupll_reg_base != pm_config.ddrpll_reg_base) { |
Sekhar Nori | efc1bb8 | 2009-12-17 18:29:31 +0530 | [diff] [blame] | 76 | |
| 77 | /* put CPU PLL in reset */ |
Kevin Hilman | 1428ed1 | 2016-11-15 11:54:20 -0800 | [diff] [blame] | 78 | val = __raw_readl(pm_config.cpupll_reg_base + PLLCTL); |
Sekhar Nori | efc1bb8 | 2009-12-17 18:29:31 +0530 | [diff] [blame] | 79 | val &= ~PLLCTL_PLLRST; |
Kevin Hilman | 1428ed1 | 2016-11-15 11:54:20 -0800 | [diff] [blame] | 80 | __raw_writel(val, pm_config.cpupll_reg_base + PLLCTL); |
Sekhar Nori | efc1bb8 | 2009-12-17 18:29:31 +0530 | [diff] [blame] | 81 | |
| 82 | /* put CPU PLL in power down */ |
Kevin Hilman | 1428ed1 | 2016-11-15 11:54:20 -0800 | [diff] [blame] | 83 | val = __raw_readl(pm_config.cpupll_reg_base + PLLCTL); |
Sekhar Nori | efc1bb8 | 2009-12-17 18:29:31 +0530 | [diff] [blame] | 84 | val &= ~PLLCTL_PLLPWRDN; |
Kevin Hilman | 1428ed1 | 2016-11-15 11:54:20 -0800 | [diff] [blame] | 85 | __raw_writel(val, pm_config.cpupll_reg_base + PLLCTL); |
Sekhar Nori | efc1bb8 | 2009-12-17 18:29:31 +0530 | [diff] [blame] | 86 | |
| 87 | /* wait for CPU PLL reset */ |
| 88 | udelay(PLL_RESET_TIME); |
| 89 | |
| 90 | /* bring CPU PLL out of reset */ |
Kevin Hilman | 1428ed1 | 2016-11-15 11:54:20 -0800 | [diff] [blame] | 91 | val = __raw_readl(pm_config.cpupll_reg_base + PLLCTL); |
Sekhar Nori | efc1bb8 | 2009-12-17 18:29:31 +0530 | [diff] [blame] | 92 | val |= PLLCTL_PLLRST; |
Kevin Hilman | 1428ed1 | 2016-11-15 11:54:20 -0800 | [diff] [blame] | 93 | __raw_writel(val, pm_config.cpupll_reg_base + PLLCTL); |
Sekhar Nori | efc1bb8 | 2009-12-17 18:29:31 +0530 | [diff] [blame] | 94 | |
| 95 | /* Wait for CPU PLL to lock */ |
| 96 | udelay(PLL_LOCK_TIME); |
| 97 | |
| 98 | /* Remove CPU PLL from bypass mode */ |
Kevin Hilman | 1428ed1 | 2016-11-15 11:54:20 -0800 | [diff] [blame] | 99 | val = __raw_readl(pm_config.cpupll_reg_base + PLLCTL); |
Sekhar Nori | efc1bb8 | 2009-12-17 18:29:31 +0530 | [diff] [blame] | 100 | val &= ~PLLCTL_PLLENSRC; |
| 101 | val |= PLLCTL_PLLEN; |
Kevin Hilman | 1428ed1 | 2016-11-15 11:54:20 -0800 | [diff] [blame] | 102 | __raw_writel(val, pm_config.cpupll_reg_base + PLLCTL); |
Sekhar Nori | efc1bb8 | 2009-12-17 18:29:31 +0530 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | |
| 106 | static int davinci_pm_enter(suspend_state_t state) |
| 107 | { |
| 108 | int ret = 0; |
| 109 | |
| 110 | switch (state) { |
Sekhar Nori | efc1bb8 | 2009-12-17 18:29:31 +0530 | [diff] [blame] | 111 | case PM_SUSPEND_MEM: |
| 112 | davinci_pm_suspend(); |
| 113 | break; |
| 114 | default: |
| 115 | ret = -EINVAL; |
| 116 | } |
| 117 | |
| 118 | return ret; |
| 119 | } |
| 120 | |
Lionel Debroux | 2f55ac0 | 2010-11-16 14:14:02 +0100 | [diff] [blame] | 121 | static const struct platform_suspend_ops davinci_pm_ops = { |
Sekhar Nori | efc1bb8 | 2009-12-17 18:29:31 +0530 | [diff] [blame] | 122 | .enter = davinci_pm_enter, |
| 123 | .valid = suspend_valid_only_mem, |
| 124 | }; |
| 125 | |
Kevin Hilman | aa9aa1e | 2016-11-15 11:54:19 -0800 | [diff] [blame] | 126 | int __init davinci_pm_init(void) |
Sekhar Nori | efc1bb8 | 2009-12-17 18:29:31 +0530 | [diff] [blame] | 127 | { |
Kevin Hilman | aa9aa1e | 2016-11-15 11:54:19 -0800 | [diff] [blame] | 128 | int ret; |
| 129 | |
| 130 | ret = davinci_cfg_reg(DA850_RTC_ALARM); |
| 131 | if (ret) |
| 132 | return ret; |
| 133 | |
Kevin Hilman | 1428ed1 | 2016-11-15 11:54:20 -0800 | [diff] [blame] | 134 | pm_config.ddr2_ctlr_base = da8xx_get_mem_ctlr(); |
| 135 | pm_config.deepsleep_reg = DA8XX_SYSCFG1_VIRT(DA8XX_DEEPSLEEP_REG); |
Kevin Hilman | aa9aa1e | 2016-11-15 11:54:19 -0800 | [diff] [blame] | 136 | |
Kevin Hilman | 1428ed1 | 2016-11-15 11:54:20 -0800 | [diff] [blame] | 137 | pm_config.cpupll_reg_base = ioremap(DA8XX_PLL0_BASE, SZ_4K); |
| 138 | if (!pm_config.cpupll_reg_base) |
Kevin Hilman | aa9aa1e | 2016-11-15 11:54:19 -0800 | [diff] [blame] | 139 | return -ENOMEM; |
| 140 | |
Kevin Hilman | 1428ed1 | 2016-11-15 11:54:20 -0800 | [diff] [blame] | 141 | pm_config.ddrpll_reg_base = ioremap(DA850_PLL1_BASE, SZ_4K); |
| 142 | if (!pm_config.ddrpll_reg_base) { |
Kevin Hilman | aa9aa1e | 2016-11-15 11:54:19 -0800 | [diff] [blame] | 143 | ret = -ENOMEM; |
| 144 | goto no_ddrpll_mem; |
| 145 | } |
| 146 | |
Kevin Hilman | 1428ed1 | 2016-11-15 11:54:20 -0800 | [diff] [blame] | 147 | pm_config.ddrpsc_reg_base = ioremap(DA8XX_PSC1_BASE, SZ_4K); |
| 148 | if (!pm_config.ddrpsc_reg_base) { |
Kevin Hilman | aa9aa1e | 2016-11-15 11:54:19 -0800 | [diff] [blame] | 149 | ret = -ENOMEM; |
| 150 | goto no_ddrpsc_mem; |
Sekhar Nori | efc1bb8 | 2009-12-17 18:29:31 +0530 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | davinci_sram_suspend = sram_alloc(davinci_cpu_suspend_sz, NULL); |
| 154 | if (!davinci_sram_suspend) { |
Kevin Hilman | aa9aa1e | 2016-11-15 11:54:19 -0800 | [diff] [blame] | 155 | pr_err("PM: cannot allocate SRAM memory\n"); |
Sekhar Nori | efc1bb8 | 2009-12-17 18:29:31 +0530 | [diff] [blame] | 156 | return -ENOMEM; |
| 157 | } |
| 158 | |
| 159 | davinci_sram_push(davinci_sram_suspend, davinci_cpu_suspend, |
| 160 | davinci_cpu_suspend_sz); |
| 161 | |
| 162 | suspend_set_ops(&davinci_pm_ops); |
| 163 | |
Kevin Hilman | aa9aa1e | 2016-11-15 11:54:19 -0800 | [diff] [blame] | 164 | no_ddrpsc_mem: |
Kevin Hilman | 1428ed1 | 2016-11-15 11:54:20 -0800 | [diff] [blame] | 165 | iounmap(pm_config.ddrpll_reg_base); |
Kevin Hilman | aa9aa1e | 2016-11-15 11:54:19 -0800 | [diff] [blame] | 166 | no_ddrpll_mem: |
Kevin Hilman | 1428ed1 | 2016-11-15 11:54:20 -0800 | [diff] [blame] | 167 | iounmap(pm_config.cpupll_reg_base); |
Kevin Hilman | aa9aa1e | 2016-11-15 11:54:19 -0800 | [diff] [blame] | 168 | return ret; |
Sekhar Nori | efc1bb8 | 2009-12-17 18:29:31 +0530 | [diff] [blame] | 169 | } |