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 */ | |
0a4b04dc AB |
23 | #define SPDCR IOMEM(0xe6180008) |
24 | #define SWUCR IOMEM(0xe6180014) | |
25 | #define PSTR IOMEM(0xe6180080) | |
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 | ||
42 | if (__raw_readl(PSTR) & mask) { | |
43 | unsigned int retry_count; | |
44 | __raw_writel(mask, SPDCR); | |
45 | ||
46 | for (retry_count = PSTR_RETRIES; retry_count; retry_count--) { | |
47 | if (!(__raw_readl(SPDCR) & mask)) | |
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", | |
55 | genpd->name, mask, __raw_readl(PSTR)); | |
56 | ||
57 | return 0; | |
58 | } | |
59 | ||
60 | static int __rmobile_pd_power_up(struct rmobile_pm_domain *rmobile_pd, | |
61 | bool do_resume) | |
62 | { | |
63 | unsigned int mask = 1 << rmobile_pd->bit_shift; | |
64 | unsigned int retry_count; | |
65 | int ret = 0; | |
66 | ||
67 | if (__raw_readl(PSTR) & mask) | |
68 | goto out; | |
69 | ||
70 | __raw_writel(mask, SWUCR); | |
71 | ||
72 | for (retry_count = 2 * PSTR_RETRIES; retry_count; retry_count--) { | |
73 | if (!(__raw_readl(SWUCR) & mask)) | |
74 | break; | |
75 | if (retry_count > PSTR_RETRIES) | |
76 | udelay(PSTR_DELAY_US); | |
77 | else | |
78 | cpu_relax(); | |
79 | } | |
80 | if (!retry_count) | |
81 | ret = -EIO; | |
82 | ||
83 | if (!rmobile_pd->no_debug) | |
84 | pr_debug("%s: Power on, 0x%08x -> PSTR = 0x%08x\n", | |
85 | rmobile_pd->genpd.name, mask, __raw_readl(PSTR)); | |
86 | ||
87 | out: | |
88 | if (ret == 0 && rmobile_pd->resume && do_resume) | |
89 | rmobile_pd->resume(); | |
90 | ||
91 | return ret; | |
92 | } | |
93 | ||
94 | static int rmobile_pd_power_up(struct generic_pm_domain *genpd) | |
95 | { | |
96 | return __rmobile_pd_power_up(to_rmobile_pd(genpd), true); | |
97 | } | |
98 | ||
99 | static bool rmobile_pd_active_wakeup(struct device *dev) | |
100 | { | |
ab496b9d | 101 | return true; |
8f45b112 KM |
102 | } |
103 | ||
4b9d62e0 GU |
104 | static int rmobile_pd_attach_dev(struct generic_pm_domain *domain, |
105 | struct device *dev) | |
106 | { | |
107 | int error; | |
108 | ||
109 | error = pm_clk_create(dev); | |
110 | if (error) { | |
111 | dev_err(dev, "pm_clk_create failed %d\n", error); | |
112 | return error; | |
113 | } | |
114 | ||
115 | error = pm_clk_add(dev, NULL); | |
116 | if (error) { | |
117 | dev_err(dev, "pm_clk_add failed %d\n", error); | |
118 | goto fail; | |
119 | } | |
120 | ||
121 | return 0; | |
122 | ||
123 | fail: | |
124 | pm_clk_destroy(dev); | |
125 | return error; | |
126 | } | |
127 | ||
128 | static void rmobile_pd_detach_dev(struct generic_pm_domain *domain, | |
129 | struct device *dev) | |
130 | { | |
131 | pm_clk_destroy(dev); | |
132 | } | |
133 | ||
a62595d3 | 134 | static void rmobile_init_pm_domain(struct rmobile_pm_domain *rmobile_pd) |
8f45b112 KM |
135 | { |
136 | struct generic_pm_domain *genpd = &rmobile_pd->genpd; | |
137 | struct dev_power_governor *gov = rmobile_pd->gov; | |
138 | ||
cffa9138 | 139 | genpd->flags = GENPD_FLAG_PM_CLK; |
8f45b112 | 140 | pm_genpd_init(genpd, gov ? : &simple_qos_governor, false); |
8f45b112 | 141 | genpd->dev_ops.active_wakeup = rmobile_pd_active_wakeup; |
8f45b112 KM |
142 | genpd->power_off = rmobile_pd_power_down; |
143 | genpd->power_on = rmobile_pd_power_up; | |
4b9d62e0 GU |
144 | genpd->attach_dev = rmobile_pd_attach_dev; |
145 | genpd->detach_dev = rmobile_pd_detach_dev; | |
8f45b112 KM |
146 | __rmobile_pd_power_up(rmobile_pd, false); |
147 | } | |
148 | ||
0d09f450 RW |
149 | void rmobile_init_domains(struct rmobile_pm_domain domains[], int num) |
150 | { | |
151 | int j; | |
152 | ||
153 | for (j = 0; j < num; j++) | |
154 | rmobile_init_pm_domain(&domains[j]); | |
155 | } | |
156 | ||
455ae3a5 RW |
157 | void rmobile_add_device_to_domain_td(const char *domain_name, |
158 | struct platform_device *pdev, | |
159 | struct gpd_timing_data *td) | |
8f45b112 KM |
160 | { |
161 | struct device *dev = &pdev->dev; | |
162 | ||
455ae3a5 | 163 | __pm_genpd_name_add_device(domain_name, dev, td); |
8f45b112 KM |
164 | } |
165 | ||
ac18e02d RW |
166 | void rmobile_add_devices_to_domains(struct pm_domain_device data[], |
167 | int size) | |
8f45b112 | 168 | { |
ac18e02d RW |
169 | struct gpd_timing_data latencies = { |
170 | .stop_latency_ns = DEFAULT_DEV_LATENCY_NS, | |
171 | .start_latency_ns = DEFAULT_DEV_LATENCY_NS, | |
172 | .save_state_latency_ns = DEFAULT_DEV_LATENCY_NS, | |
173 | .restore_state_latency_ns = DEFAULT_DEV_LATENCY_NS, | |
174 | }; | |
175 | int j; | |
176 | ||
177 | for (j = 0; j < size; j++) | |
178 | rmobile_add_device_to_domain_td(data[j].domain_name, | |
179 | data[j].pdev, &latencies); | |
8f45b112 | 180 | } |