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