Commit | Line | Data |
---|---|---|
8f45b112 KM |
1 | /* |
2 | * rmobile power management support | |
3 | * | |
4 | * Copyright (C) 2012 Renesas Solutions Corp. | |
5 | * Copyright (C) 2012 Kuninori Morimoto <kuninori.morimoto.gx@renesas.com> | |
6 | * | |
7 | * based on pm-sh7372.c | |
8 | * Copyright (C) 2011 Magnus Damm | |
9 | * | |
10 | * This file is subject to the terms and conditions of the GNU General Public | |
11 | * License. See the file "COPYING" in the main directory of this archive | |
12 | * for more details. | |
13 | */ | |
14 | #include <linux/console.h> | |
15 | #include <linux/delay.h> | |
16 | #include <linux/platform_device.h> | |
17 | #include <linux/pm.h> | |
18 | #include <linux/pm_clock.h> | |
19 | #include <asm/io.h> | |
6b8b0cb4 | 20 | #include "pm-rmobile.h" |
8f45b112 KM |
21 | |
22 | /* SYSC */ | |
25717b85 GU |
23 | #define SPDCR 0x08 /* SYS Power Down Control Register */ |
24 | #define SWUCR 0x14 /* SYS Wakeup Control Register */ | |
25 | #define PSTR 0x80 /* Power Status Register */ | |
8f45b112 KM |
26 | |
27 | #define PSTR_RETRIES 100 | |
28 | #define PSTR_DELAY_US 10 | |
29 | ||
8f45b112 KM |
30 | static int rmobile_pd_power_down(struct generic_pm_domain *genpd) |
31 | { | |
32 | struct rmobile_pm_domain *rmobile_pd = to_rmobile_pd(genpd); | |
33 | unsigned int mask = 1 << rmobile_pd->bit_shift; | |
34 | ||
35 | if (rmobile_pd->suspend) { | |
36 | int ret = rmobile_pd->suspend(); | |
37 | ||
38 | if (ret) | |
39 | return ret; | |
40 | } | |
41 | ||
25717b85 | 42 | if (__raw_readl(rmobile_pd->base + PSTR) & mask) { |
8f45b112 | 43 | unsigned int retry_count; |
25717b85 | 44 | __raw_writel(mask, rmobile_pd->base + SPDCR); |
8f45b112 KM |
45 | |
46 | for (retry_count = PSTR_RETRIES; retry_count; retry_count--) { | |
25717b85 | 47 | if (!(__raw_readl(rmobile_pd->base + SPDCR) & mask)) |
8f45b112 KM |
48 | break; |
49 | cpu_relax(); | |
50 | } | |
51 | } | |
52 | ||
53 | if (!rmobile_pd->no_debug) | |
54 | pr_debug("%s: Power off, 0x%08x -> PSTR = 0x%08x\n", | |
25717b85 GU |
55 | genpd->name, mask, |
56 | __raw_readl(rmobile_pd->base + PSTR)); | |
8f45b112 KM |
57 | |
58 | return 0; | |
59 | } | |
60 | ||
61 | static int __rmobile_pd_power_up(struct rmobile_pm_domain *rmobile_pd, | |
62 | bool do_resume) | |
63 | { | |
64 | unsigned int mask = 1 << rmobile_pd->bit_shift; | |
65 | unsigned int retry_count; | |
66 | int ret = 0; | |
67 | ||
25717b85 | 68 | if (__raw_readl(rmobile_pd->base + PSTR) & mask) |
8f45b112 KM |
69 | goto out; |
70 | ||
25717b85 | 71 | __raw_writel(mask, rmobile_pd->base + SWUCR); |
8f45b112 KM |
72 | |
73 | for (retry_count = 2 * PSTR_RETRIES; retry_count; retry_count--) { | |
25717b85 | 74 | if (!(__raw_readl(rmobile_pd->base + SWUCR) & mask)) |
8f45b112 KM |
75 | break; |
76 | if (retry_count > PSTR_RETRIES) | |
77 | udelay(PSTR_DELAY_US); | |
78 | else | |
79 | cpu_relax(); | |
80 | } | |
81 | if (!retry_count) | |
82 | ret = -EIO; | |
83 | ||
84 | if (!rmobile_pd->no_debug) | |
85 | pr_debug("%s: Power on, 0x%08x -> PSTR = 0x%08x\n", | |
25717b85 GU |
86 | rmobile_pd->genpd.name, mask, |
87 | __raw_readl(rmobile_pd->base + PSTR)); | |
8f45b112 KM |
88 | |
89 | out: | |
90 | if (ret == 0 && rmobile_pd->resume && do_resume) | |
91 | rmobile_pd->resume(); | |
92 | ||
93 | return ret; | |
94 | } | |
95 | ||
96 | static int rmobile_pd_power_up(struct generic_pm_domain *genpd) | |
97 | { | |
98 | return __rmobile_pd_power_up(to_rmobile_pd(genpd), true); | |
99 | } | |
100 | ||
101 | static bool rmobile_pd_active_wakeup(struct device *dev) | |
102 | { | |
ab496b9d | 103 | return true; |
8f45b112 KM |
104 | } |
105 | ||
4b9d62e0 GU |
106 | static int rmobile_pd_attach_dev(struct generic_pm_domain *domain, |
107 | struct device *dev) | |
108 | { | |
109 | int error; | |
110 | ||
111 | error = pm_clk_create(dev); | |
112 | if (error) { | |
113 | dev_err(dev, "pm_clk_create failed %d\n", error); | |
114 | return error; | |
115 | } | |
116 | ||
117 | error = pm_clk_add(dev, NULL); | |
118 | if (error) { | |
119 | dev_err(dev, "pm_clk_add failed %d\n", error); | |
120 | goto fail; | |
121 | } | |
122 | ||
123 | return 0; | |
124 | ||
125 | fail: | |
126 | pm_clk_destroy(dev); | |
127 | return error; | |
128 | } | |
129 | ||
130 | static void rmobile_pd_detach_dev(struct generic_pm_domain *domain, | |
131 | struct device *dev) | |
132 | { | |
133 | pm_clk_destroy(dev); | |
134 | } | |
135 | ||
a62595d3 | 136 | static void rmobile_init_pm_domain(struct rmobile_pm_domain *rmobile_pd) |
8f45b112 KM |
137 | { |
138 | struct generic_pm_domain *genpd = &rmobile_pd->genpd; | |
139 | struct dev_power_governor *gov = rmobile_pd->gov; | |
140 | ||
cffa9138 | 141 | genpd->flags = GENPD_FLAG_PM_CLK; |
8f45b112 | 142 | pm_genpd_init(genpd, gov ? : &simple_qos_governor, false); |
8f45b112 | 143 | genpd->dev_ops.active_wakeup = rmobile_pd_active_wakeup; |
8f45b112 KM |
144 | genpd->power_off = rmobile_pd_power_down; |
145 | genpd->power_on = rmobile_pd_power_up; | |
4b9d62e0 GU |
146 | genpd->attach_dev = rmobile_pd_attach_dev; |
147 | genpd->detach_dev = rmobile_pd_detach_dev; | |
8f45b112 KM |
148 | __rmobile_pd_power_up(rmobile_pd, false); |
149 | } | |
150 | ||
0d09f450 RW |
151 | void rmobile_init_domains(struct rmobile_pm_domain domains[], int num) |
152 | { | |
153 | int j; | |
154 | ||
155 | for (j = 0; j < num; j++) | |
156 | rmobile_init_pm_domain(&domains[j]); | |
157 | } | |
158 | ||
455ae3a5 RW |
159 | void rmobile_add_device_to_domain_td(const char *domain_name, |
160 | struct platform_device *pdev, | |
161 | struct gpd_timing_data *td) | |
8f45b112 KM |
162 | { |
163 | struct device *dev = &pdev->dev; | |
164 | ||
455ae3a5 | 165 | __pm_genpd_name_add_device(domain_name, dev, td); |
8f45b112 KM |
166 | } |
167 | ||
ac18e02d RW |
168 | void rmobile_add_devices_to_domains(struct pm_domain_device data[], |
169 | int size) | |
8f45b112 | 170 | { |
ac18e02d RW |
171 | struct gpd_timing_data latencies = { |
172 | .stop_latency_ns = DEFAULT_DEV_LATENCY_NS, | |
173 | .start_latency_ns = DEFAULT_DEV_LATENCY_NS, | |
174 | .save_state_latency_ns = DEFAULT_DEV_LATENCY_NS, | |
175 | .restore_state_latency_ns = DEFAULT_DEV_LATENCY_NS, | |
176 | }; | |
177 | int j; | |
178 | ||
179 | for (j = 0; j < size; j++) | |
180 | rmobile_add_device_to_domain_td(data[j].domain_name, | |
181 | data[j].pdev, &latencies); | |
8f45b112 | 182 | } |