Blame view

drivers/clk/qcom/gdsc.c 6.8 KB
45dd0e553   Stephen Boyd   clk: qcom: Add su...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  /*
   * Copyright (c) 2015, The Linux Foundation. All rights reserved.
   *
   * This program is free software; you can redistribute it and/or modify
   * it under the terms of the GNU General Public License version 2 and
   * only version 2 as published by the Free Software Foundation.
   *
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU General Public License for more details.
   */
  
  #include <linux/bitops.h>
  #include <linux/delay.h>
  #include <linux/err.h>
  #include <linux/jiffies.h>
  #include <linux/kernel.h>
77b1067a1   Rajendra Nayak   clk: qcom: gdsc: ...
19
  #include <linux/ktime.h>
45dd0e553   Stephen Boyd   clk: qcom: Add su...
20
21
  #include <linux/pm_domain.h>
  #include <linux/regmap.h>
3c53f5e21   Rajendra Nayak   clk: qcom: gdsc: ...
22
  #include <linux/reset-controller.h>
45dd0e553   Stephen Boyd   clk: qcom: Add su...
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
  #include <linux/slab.h>
  #include "gdsc.h"
  
  #define PWR_ON_MASK		BIT(31)
  #define EN_REST_WAIT_MASK	GENMASK_ULL(23, 20)
  #define EN_FEW_WAIT_MASK	GENMASK_ULL(19, 16)
  #define CLK_DIS_WAIT_MASK	GENMASK_ULL(15, 12)
  #define SW_OVERRIDE_MASK	BIT(2)
  #define HW_CONTROL_MASK		BIT(1)
  #define SW_COLLAPSE_MASK	BIT(0)
  
  /* Wait 2^n CXO cycles between all states. Here, n=2 (4 cycles). */
  #define EN_REST_WAIT_VAL	(0x2 << 20)
  #define EN_FEW_WAIT_VAL		(0x8 << 16)
  #define CLK_DIS_WAIT_VAL	(0x2 << 12)
014e193cc   Rajendra Nayak   clk: qcom: gdsc: ...
38
39
  #define RETAIN_MEM		BIT(14)
  #define RETAIN_PERIPH		BIT(13)
45dd0e553   Stephen Boyd   clk: qcom: Add su...
40
41
42
  #define TIMEOUT_US		100
  
  #define domain_to_gdsc(domain) container_of(domain, struct gdsc, pd)
77b1067a1   Rajendra Nayak   clk: qcom: gdsc: ...
43
  static int gdsc_is_enabled(struct gdsc *sc, unsigned int reg)
45dd0e553   Stephen Boyd   clk: qcom: Add su...
44
45
46
  {
  	u32 val;
  	int ret;
77b1067a1   Rajendra Nayak   clk: qcom: gdsc: ...
47
  	ret = regmap_read(sc->regmap, reg, &val);
45dd0e553   Stephen Boyd   clk: qcom: Add su...
48
49
50
51
52
53
54
55
56
57
  	if (ret)
  		return ret;
  
  	return !!(val & PWR_ON_MASK);
  }
  
  static int gdsc_toggle_logic(struct gdsc *sc, bool en)
  {
  	int ret;
  	u32 val = en ? 0 : SW_COLLAPSE_MASK;
77b1067a1   Rajendra Nayak   clk: qcom: gdsc: ...
58
59
  	ktime_t start;
  	unsigned int status_reg = sc->gdscr;
45dd0e553   Stephen Boyd   clk: qcom: Add su...
60
61
62
63
  
  	ret = regmap_update_bits(sc->regmap, sc->gdscr, SW_COLLAPSE_MASK, val);
  	if (ret)
  		return ret;
a823bb9fb   Rajendra Nayak   clk: qcom: gdsc: ...
64
65
66
67
68
69
70
71
72
73
  	/* If disabling votable gdscs, don't poll on status */
  	if ((sc->flags & VOTABLE) && !en) {
  		/*
  		 * Add a short delay here to ensure that an enable
  		 * right after it was disabled does not put it in an
  		 * unknown state
  		 */
  		udelay(TIMEOUT_US);
  		return 0;
  	}
77b1067a1   Rajendra Nayak   clk: qcom: gdsc: ...
74
75
76
77
78
79
80
81
82
83
84
85
86
87
  	if (sc->gds_hw_ctrl) {
  		status_reg = sc->gds_hw_ctrl;
  		/*
  		 * The gds hw controller asserts/de-asserts the status bit soon
  		 * after it receives a power on/off request from a master.
  		 * The controller then takes around 8 xo cycles to start its
  		 * internal state machine and update the status bit. During
  		 * this time, the status bit does not reflect the true status
  		 * of the core.
  		 * Add a delay of 1 us between writing to the SW_COLLAPSE bit
  		 * and polling the status bit.
  		 */
  		udelay(1);
  	}
45dd0e553   Stephen Boyd   clk: qcom: Add su...
88

77b1067a1   Rajendra Nayak   clk: qcom: gdsc: ...
89
90
91
  	start = ktime_get();
  	do {
  		if (gdsc_is_enabled(sc, status_reg) == en)
45dd0e553   Stephen Boyd   clk: qcom: Add su...
92
  			return 0;
77b1067a1   Rajendra Nayak   clk: qcom: gdsc: ...
93
  	} while (ktime_us_delta(ktime_get(), start) < TIMEOUT_US);
45dd0e553   Stephen Boyd   clk: qcom: Add su...
94

77b1067a1   Rajendra Nayak   clk: qcom: gdsc: ...
95
  	if (gdsc_is_enabled(sc, status_reg) == en)
45dd0e553   Stephen Boyd   clk: qcom: Add su...
96
97
98
99
  		return 0;
  
  	return -ETIMEDOUT;
  }
3c53f5e21   Rajendra Nayak   clk: qcom: gdsc: ...
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
  static inline int gdsc_deassert_reset(struct gdsc *sc)
  {
  	int i;
  
  	for (i = 0; i < sc->reset_count; i++)
  		sc->rcdev->ops->deassert(sc->rcdev, sc->resets[i]);
  	return 0;
  }
  
  static inline int gdsc_assert_reset(struct gdsc *sc)
  {
  	int i;
  
  	for (i = 0; i < sc->reset_count; i++)
  		sc->rcdev->ops->assert(sc->rcdev, sc->resets[i]);
  	return 0;
  }
014e193cc   Rajendra Nayak   clk: qcom: gdsc: ...
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
  static inline void gdsc_force_mem_on(struct gdsc *sc)
  {
  	int i;
  	u32 mask = RETAIN_MEM | RETAIN_PERIPH;
  
  	for (i = 0; i < sc->cxc_count; i++)
  		regmap_update_bits(sc->regmap, sc->cxcs[i], mask, mask);
  }
  
  static inline void gdsc_clear_mem_on(struct gdsc *sc)
  {
  	int i;
  	u32 mask = RETAIN_MEM | RETAIN_PERIPH;
  
  	for (i = 0; i < sc->cxc_count; i++)
  		regmap_update_bits(sc->regmap, sc->cxcs[i], mask, 0);
  }
45dd0e553   Stephen Boyd   clk: qcom: Add su...
134
135
136
137
  static int gdsc_enable(struct generic_pm_domain *domain)
  {
  	struct gdsc *sc = domain_to_gdsc(domain);
  	int ret;
3c53f5e21   Rajendra Nayak   clk: qcom: gdsc: ...
138
139
  	if (sc->pwrsts == PWRSTS_ON)
  		return gdsc_deassert_reset(sc);
45dd0e553   Stephen Boyd   clk: qcom: Add su...
140
141
142
  	ret = gdsc_toggle_logic(sc, true);
  	if (ret)
  		return ret;
014e193cc   Rajendra Nayak   clk: qcom: gdsc: ...
143
144
145
  
  	if (sc->pwrsts & PWRSTS_OFF)
  		gdsc_force_mem_on(sc);
45dd0e553   Stephen Boyd   clk: qcom: Add su...
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
  	/*
  	 * If clocks to this power domain were already on, they will take an
  	 * additional 4 clock cycles to re-enable after the power domain is
  	 * enabled. Delay to account for this. A delay is also needed to ensure
  	 * clocks are not enabled within 400ns of enabling power to the
  	 * memories.
  	 */
  	udelay(1);
  
  	return 0;
  }
  
  static int gdsc_disable(struct generic_pm_domain *domain)
  {
  	struct gdsc *sc = domain_to_gdsc(domain);
3c53f5e21   Rajendra Nayak   clk: qcom: gdsc: ...
161
162
  	if (sc->pwrsts == PWRSTS_ON)
  		return gdsc_assert_reset(sc);
014e193cc   Rajendra Nayak   clk: qcom: gdsc: ...
163
164
  	if (sc->pwrsts & PWRSTS_OFF)
  		gdsc_clear_mem_on(sc);
45dd0e553   Stephen Boyd   clk: qcom: Add su...
165
166
167
168
169
170
171
  	return gdsc_toggle_logic(sc, false);
  }
  
  static int gdsc_init(struct gdsc *sc)
  {
  	u32 mask, val;
  	int on, ret;
77b1067a1   Rajendra Nayak   clk: qcom: gdsc: ...
172
  	unsigned int reg;
45dd0e553   Stephen Boyd   clk: qcom: Add su...
173
174
175
176
177
178
179
180
181
182
183
184
  
  	/*
  	 * Disable HW trigger: collapse/restore occur based on registers writes.
  	 * Disable SW override: Use hardware state-machine for sequencing.
  	 * Configure wait time between states.
  	 */
  	mask = HW_CONTROL_MASK | SW_OVERRIDE_MASK |
  	       EN_REST_WAIT_MASK | EN_FEW_WAIT_MASK | CLK_DIS_WAIT_MASK;
  	val = EN_REST_WAIT_VAL | EN_FEW_WAIT_VAL | CLK_DIS_WAIT_VAL;
  	ret = regmap_update_bits(sc->regmap, sc->gdscr, mask, val);
  	if (ret)
  		return ret;
3c53f5e21   Rajendra Nayak   clk: qcom: gdsc: ...
185
186
187
188
189
190
  	/* Force gdsc ON if only ON state is supported */
  	if (sc->pwrsts == PWRSTS_ON) {
  		ret = gdsc_toggle_logic(sc, true);
  		if (ret)
  			return ret;
  	}
77b1067a1   Rajendra Nayak   clk: qcom: gdsc: ...
191
192
  	reg = sc->gds_hw_ctrl ? sc->gds_hw_ctrl : sc->gdscr;
  	on = gdsc_is_enabled(sc, reg);
45dd0e553   Stephen Boyd   clk: qcom: Add su...
193
194
  	if (on < 0)
  		return on;
a823bb9fb   Rajendra Nayak   clk: qcom: gdsc: ...
195
196
197
198
199
200
  	/*
  	 * Votable GDSCs can be ON due to Vote from other masters.
  	 * If a Votable GDSC is ON, make sure we have a Vote.
  	 */
  	if ((sc->flags & VOTABLE) && on)
  		gdsc_enable(&sc->pd);
014e193cc   Rajendra Nayak   clk: qcom: gdsc: ...
201
202
203
204
  	if (on || (sc->pwrsts & PWRSTS_RET))
  		gdsc_force_mem_on(sc);
  	else
  		gdsc_clear_mem_on(sc);
45dd0e553   Stephen Boyd   clk: qcom: Add su...
205
206
207
208
209
210
  	sc->pd.power_off = gdsc_disable;
  	sc->pd.power_on = gdsc_enable;
  	pm_genpd_init(&sc->pd, NULL, !on);
  
  	return 0;
  }
c2c7f0a47   Rajendra Nayak   clk: qcom: gdsc: ...
211
  int gdsc_register(struct gdsc_desc *desc,
3c53f5e21   Rajendra Nayak   clk: qcom: gdsc: ...
212
  		  struct reset_controller_dev *rcdev, struct regmap *regmap)
45dd0e553   Stephen Boyd   clk: qcom: Add su...
213
214
215
  {
  	int i, ret;
  	struct genpd_onecell_data *data;
c2c7f0a47   Rajendra Nayak   clk: qcom: gdsc: ...
216
217
218
  	struct device *dev = desc->dev;
  	struct gdsc **scs = desc->scs;
  	size_t num = desc->num;
45dd0e553   Stephen Boyd   clk: qcom: Add su...
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
  
  	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  	if (!data)
  		return -ENOMEM;
  
  	data->domains = devm_kcalloc(dev, num, sizeof(*data->domains),
  				     GFP_KERNEL);
  	if (!data->domains)
  		return -ENOMEM;
  
  	data->num_domains = num;
  	for (i = 0; i < num; i++) {
  		if (!scs[i])
  			continue;
  		scs[i]->regmap = regmap;
3c53f5e21   Rajendra Nayak   clk: qcom: gdsc: ...
234
  		scs[i]->rcdev = rcdev;
45dd0e553   Stephen Boyd   clk: qcom: Add su...
235
236
237
238
239
  		ret = gdsc_init(scs[i]);
  		if (ret)
  			return ret;
  		data->domains[i] = &scs[i]->pd;
  	}
c2c7f0a47   Rajendra Nayak   clk: qcom: gdsc: ...
240
241
242
243
244
245
246
  	/* Add subdomains */
  	for (i = 0; i < num; i++) {
  		if (!scs[i])
  			continue;
  		if (scs[i]->parent)
  			pm_genpd_add_subdomain(scs[i]->parent, &scs[i]->pd);
  	}
45dd0e553   Stephen Boyd   clk: qcom: Add su...
247
248
  	return of_genpd_add_provider_onecell(dev->of_node, data);
  }
c2c7f0a47   Rajendra Nayak   clk: qcom: gdsc: ...
249
  void gdsc_unregister(struct gdsc_desc *desc)
45dd0e553   Stephen Boyd   clk: qcom: Add su...
250
  {
c2c7f0a47   Rajendra Nayak   clk: qcom: gdsc: ...
251
252
253
254
255
256
257
258
259
260
261
262
  	int i;
  	struct device *dev = desc->dev;
  	struct gdsc **scs = desc->scs;
  	size_t num = desc->num;
  
  	/* Remove subdomains */
  	for (i = 0; i < num; i++) {
  		if (!scs[i])
  			continue;
  		if (scs[i]->parent)
  			pm_genpd_remove_subdomain(scs[i]->parent, &scs[i]->pd);
  	}
45dd0e553   Stephen Boyd   clk: qcom: Add su...
263
264
  	of_genpd_del_provider(dev->of_node);
  }