clk: qcom: gdsc: Add support to poll for higher timeout value
[linux-2.6-microblaze.git] / drivers / clk / qcom / gdsc.c
1 /*
2  * Copyright (c) 2015, 2017-2018, The Linux Foundation. All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 and
6  * only version 2 as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  */
13
14 #include <linux/bitops.h>
15 #include <linux/delay.h>
16 #include <linux/err.h>
17 #include <linux/jiffies.h>
18 #include <linux/kernel.h>
19 #include <linux/ktime.h>
20 #include <linux/pm_domain.h>
21 #include <linux/regmap.h>
22 #include <linux/reset-controller.h>
23 #include <linux/slab.h>
24 #include "gdsc.h"
25
26 #define PWR_ON_MASK             BIT(31)
27 #define EN_REST_WAIT_MASK       GENMASK_ULL(23, 20)
28 #define EN_FEW_WAIT_MASK        GENMASK_ULL(19, 16)
29 #define CLK_DIS_WAIT_MASK       GENMASK_ULL(15, 12)
30 #define SW_OVERRIDE_MASK        BIT(2)
31 #define HW_CONTROL_MASK         BIT(1)
32 #define SW_COLLAPSE_MASK        BIT(0)
33 #define GMEM_CLAMP_IO_MASK      BIT(0)
34 #define GMEM_RESET_MASK         BIT(4)
35
36 /* Wait 2^n CXO cycles between all states. Here, n=2 (4 cycles). */
37 #define EN_REST_WAIT_VAL        (0x2 << 20)
38 #define EN_FEW_WAIT_VAL         (0x8 << 16)
39 #define CLK_DIS_WAIT_VAL        (0x2 << 12)
40
41 #define RETAIN_MEM              BIT(14)
42 #define RETAIN_PERIPH           BIT(13)
43
44 #define TIMEOUT_US              500
45
46 #define domain_to_gdsc(domain) container_of(domain, struct gdsc, pd)
47
48 static int gdsc_is_enabled(struct gdsc *sc, unsigned int reg)
49 {
50         u32 val;
51         int ret;
52
53         ret = regmap_read(sc->regmap, reg, &val);
54         if (ret)
55                 return ret;
56
57         return !!(val & PWR_ON_MASK);
58 }
59
60 static int gdsc_hwctrl(struct gdsc *sc, bool en)
61 {
62         u32 val = en ? HW_CONTROL_MASK : 0;
63
64         return regmap_update_bits(sc->regmap, sc->gdscr, HW_CONTROL_MASK, val);
65 }
66
67 static int gdsc_poll_status(struct gdsc *sc, unsigned int reg, bool en)
68 {
69         ktime_t start;
70
71         start = ktime_get();
72         do {
73                 if (gdsc_is_enabled(sc, reg) == en)
74                         return 0;
75         } while (ktime_us_delta(ktime_get(), start) < TIMEOUT_US);
76
77         if (gdsc_is_enabled(sc, reg) == en)
78                 return 0;
79
80         return -ETIMEDOUT;
81 }
82
83 static int gdsc_toggle_logic(struct gdsc *sc, bool en)
84 {
85         int ret;
86         u32 val = en ? 0 : SW_COLLAPSE_MASK;
87         unsigned int status_reg = sc->gdscr;
88
89         ret = regmap_update_bits(sc->regmap, sc->gdscr, SW_COLLAPSE_MASK, val);
90         if (ret)
91                 return ret;
92
93         /* If disabling votable gdscs, don't poll on status */
94         if ((sc->flags & VOTABLE) && !en) {
95                 /*
96                  * Add a short delay here to ensure that an enable
97                  * right after it was disabled does not put it in an
98                  * unknown state
99                  */
100                 udelay(TIMEOUT_US);
101                 return 0;
102         }
103
104         if (sc->gds_hw_ctrl) {
105                 status_reg = sc->gds_hw_ctrl;
106                 /*
107                  * The gds hw controller asserts/de-asserts the status bit soon
108                  * after it receives a power on/off request from a master.
109                  * The controller then takes around 8 xo cycles to start its
110                  * internal state machine and update the status bit. During
111                  * this time, the status bit does not reflect the true status
112                  * of the core.
113                  * Add a delay of 1 us between writing to the SW_COLLAPSE bit
114                  * and polling the status bit.
115                  */
116                 udelay(1);
117         }
118
119         return gdsc_poll_status(sc, status_reg, en);
120 }
121
122 static inline int gdsc_deassert_reset(struct gdsc *sc)
123 {
124         int i;
125
126         for (i = 0; i < sc->reset_count; i++)
127                 sc->rcdev->ops->deassert(sc->rcdev, sc->resets[i]);
128         return 0;
129 }
130
131 static inline int gdsc_assert_reset(struct gdsc *sc)
132 {
133         int i;
134
135         for (i = 0; i < sc->reset_count; i++)
136                 sc->rcdev->ops->assert(sc->rcdev, sc->resets[i]);
137         return 0;
138 }
139
140 static inline void gdsc_force_mem_on(struct gdsc *sc)
141 {
142         int i;
143         u32 mask = RETAIN_MEM | RETAIN_PERIPH;
144
145         for (i = 0; i < sc->cxc_count; i++)
146                 regmap_update_bits(sc->regmap, sc->cxcs[i], mask, mask);
147 }
148
149 static inline void gdsc_clear_mem_on(struct gdsc *sc)
150 {
151         int i;
152         u32 mask = RETAIN_MEM | RETAIN_PERIPH;
153
154         for (i = 0; i < sc->cxc_count; i++)
155                 regmap_update_bits(sc->regmap, sc->cxcs[i], mask, 0);
156 }
157
158 static inline void gdsc_deassert_clamp_io(struct gdsc *sc)
159 {
160         regmap_update_bits(sc->regmap, sc->clamp_io_ctrl,
161                            GMEM_CLAMP_IO_MASK, 0);
162 }
163
164 static inline void gdsc_assert_clamp_io(struct gdsc *sc)
165 {
166         regmap_update_bits(sc->regmap, sc->clamp_io_ctrl,
167                            GMEM_CLAMP_IO_MASK, 1);
168 }
169
170 static inline void gdsc_assert_reset_aon(struct gdsc *sc)
171 {
172         regmap_update_bits(sc->regmap, sc->clamp_io_ctrl,
173                            GMEM_RESET_MASK, 1);
174         udelay(1);
175         regmap_update_bits(sc->regmap, sc->clamp_io_ctrl,
176                            GMEM_RESET_MASK, 0);
177 }
178 static int gdsc_enable(struct generic_pm_domain *domain)
179 {
180         struct gdsc *sc = domain_to_gdsc(domain);
181         int ret;
182
183         if (sc->pwrsts == PWRSTS_ON)
184                 return gdsc_deassert_reset(sc);
185
186         if (sc->flags & SW_RESET) {
187                 gdsc_assert_reset(sc);
188                 udelay(1);
189                 gdsc_deassert_reset(sc);
190         }
191
192         if (sc->flags & CLAMP_IO) {
193                 if (sc->flags & AON_RESET)
194                         gdsc_assert_reset_aon(sc);
195                 gdsc_deassert_clamp_io(sc);
196         }
197
198         ret = gdsc_toggle_logic(sc, true);
199         if (ret)
200                 return ret;
201
202         if (sc->pwrsts & PWRSTS_OFF)
203                 gdsc_force_mem_on(sc);
204
205         /*
206          * If clocks to this power domain were already on, they will take an
207          * additional 4 clock cycles to re-enable after the power domain is
208          * enabled. Delay to account for this. A delay is also needed to ensure
209          * clocks are not enabled within 400ns of enabling power to the
210          * memories.
211          */
212         udelay(1);
213
214         /* Turn on HW trigger mode if supported */
215         if (sc->flags & HW_CTRL) {
216                 ret = gdsc_hwctrl(sc, true);
217                 if (ret)
218                         return ret;
219                 /*
220                  * Wait for the GDSC to go through a power down and
221                  * up cycle.  In case a firmware ends up polling status
222                  * bits for the gdsc, it might read an 'on' status before
223                  * the GDSC can finish the power cycle.
224                  * We wait 1us before returning to ensure the firmware
225                  * can't immediately poll the status bits.
226                  */
227                 udelay(1);
228         }
229
230         return 0;
231 }
232
233 static int gdsc_disable(struct generic_pm_domain *domain)
234 {
235         struct gdsc *sc = domain_to_gdsc(domain);
236         int ret;
237
238         if (sc->pwrsts == PWRSTS_ON)
239                 return gdsc_assert_reset(sc);
240
241         /* Turn off HW trigger mode if supported */
242         if (sc->flags & HW_CTRL) {
243                 unsigned int reg;
244
245                 ret = gdsc_hwctrl(sc, false);
246                 if (ret < 0)
247                         return ret;
248                 /*
249                  * Wait for the GDSC to go through a power down and
250                  * up cycle.  In case we end up polling status
251                  * bits for the gdsc before the power cycle is completed
252                  * it might read an 'on' status wrongly.
253                  */
254                 udelay(1);
255
256                 reg = sc->gds_hw_ctrl ? sc->gds_hw_ctrl : sc->gdscr;
257                 ret = gdsc_poll_status(sc, reg, true);
258                 if (ret)
259                         return ret;
260         }
261
262         if (sc->pwrsts & PWRSTS_OFF)
263                 gdsc_clear_mem_on(sc);
264
265         ret = gdsc_toggle_logic(sc, false);
266         if (ret)
267                 return ret;
268
269         if (sc->flags & CLAMP_IO)
270                 gdsc_assert_clamp_io(sc);
271
272         return 0;
273 }
274
275 static int gdsc_init(struct gdsc *sc)
276 {
277         u32 mask, val;
278         int on, ret;
279         unsigned int reg;
280
281         /*
282          * Disable HW trigger: collapse/restore occur based on registers writes.
283          * Disable SW override: Use hardware state-machine for sequencing.
284          * Configure wait time between states.
285          */
286         mask = HW_CONTROL_MASK | SW_OVERRIDE_MASK |
287                EN_REST_WAIT_MASK | EN_FEW_WAIT_MASK | CLK_DIS_WAIT_MASK;
288         val = EN_REST_WAIT_VAL | EN_FEW_WAIT_VAL | CLK_DIS_WAIT_VAL;
289         ret = regmap_update_bits(sc->regmap, sc->gdscr, mask, val);
290         if (ret)
291                 return ret;
292
293         /* Force gdsc ON if only ON state is supported */
294         if (sc->pwrsts == PWRSTS_ON) {
295                 ret = gdsc_toggle_logic(sc, true);
296                 if (ret)
297                         return ret;
298         }
299
300         reg = sc->gds_hw_ctrl ? sc->gds_hw_ctrl : sc->gdscr;
301         on = gdsc_is_enabled(sc, reg);
302         if (on < 0)
303                 return on;
304
305         /*
306          * Votable GDSCs can be ON due to Vote from other masters.
307          * If a Votable GDSC is ON, make sure we have a Vote.
308          */
309         if ((sc->flags & VOTABLE) && on)
310                 gdsc_enable(&sc->pd);
311
312         if (on || (sc->pwrsts & PWRSTS_RET))
313                 gdsc_force_mem_on(sc);
314         else
315                 gdsc_clear_mem_on(sc);
316
317         sc->pd.power_off = gdsc_disable;
318         sc->pd.power_on = gdsc_enable;
319         pm_genpd_init(&sc->pd, NULL, !on);
320
321         return 0;
322 }
323
324 int gdsc_register(struct gdsc_desc *desc,
325                   struct reset_controller_dev *rcdev, struct regmap *regmap)
326 {
327         int i, ret;
328         struct genpd_onecell_data *data;
329         struct device *dev = desc->dev;
330         struct gdsc **scs = desc->scs;
331         size_t num = desc->num;
332
333         data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
334         if (!data)
335                 return -ENOMEM;
336
337         data->domains = devm_kcalloc(dev, num, sizeof(*data->domains),
338                                      GFP_KERNEL);
339         if (!data->domains)
340                 return -ENOMEM;
341
342         data->num_domains = num;
343         for (i = 0; i < num; i++) {
344                 if (!scs[i])
345                         continue;
346                 scs[i]->regmap = regmap;
347                 scs[i]->rcdev = rcdev;
348                 ret = gdsc_init(scs[i]);
349                 if (ret)
350                         return ret;
351                 data->domains[i] = &scs[i]->pd;
352         }
353
354         /* Add subdomains */
355         for (i = 0; i < num; i++) {
356                 if (!scs[i])
357                         continue;
358                 if (scs[i]->parent)
359                         pm_genpd_add_subdomain(scs[i]->parent, &scs[i]->pd);
360         }
361
362         return of_genpd_add_provider_onecell(dev->of_node, data);
363 }
364
365 void gdsc_unregister(struct gdsc_desc *desc)
366 {
367         int i;
368         struct device *dev = desc->dev;
369         struct gdsc **scs = desc->scs;
370         size_t num = desc->num;
371
372         /* Remove subdomains */
373         for (i = 0; i < num; i++) {
374                 if (!scs[i])
375                         continue;
376                 if (scs[i]->parent)
377                         pm_genpd_remove_subdomain(scs[i]->parent, &scs[i]->pd);
378         }
379         of_genpd_del_provider(dev->of_node);
380 }