aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Elder <elder@linaro.org>2014-04-21 17:11:42 -0400
committerMike Turquette <mturquette@linaro.org>2014-04-30 14:51:36 -0400
commita597faccc7eedd406313e880ed05ff75bc522910 (patch)
tree4e463bcfde259380a3ea5b50534272357f007dc8
parent03548ec06ad3ec75d5b212fa832e4e617334ea09 (diff)
clk: bcm281xx: add clock policy support
Add support for CCU policy engine control, and also for setting the mask bits for bus clocks that require a policy change to get activated. This includes adding validity checking framework for CCUs, to validate the policy fields if defined. Signed-off-by: Alex Elder <elder@linaro.org> Signed-off-by: Mike Turquette <mturquette@linaro.org>
-rw-r--r--drivers/clk/bcm/clk-kona-setup.c92
-rw-r--r--drivers/clk/bcm/clk-kona.c155
-rw-r--r--drivers/clk/bcm/clk-kona.h71
3 files changed, 318 insertions, 0 deletions
diff --git a/drivers/clk/bcm/clk-kona-setup.c b/drivers/clk/bcm/clk-kona-setup.c
index 825a2f2ab052..773ad7c7dd59 100644
--- a/drivers/clk/bcm/clk-kona-setup.c
+++ b/drivers/clk/bcm/clk-kona-setup.c
@@ -25,6 +25,31 @@ LIST_HEAD(ccu_list); /* The list of set up CCUs */
25 25
26/* Validity checking */ 26/* Validity checking */
27 27
28static bool ccu_data_offsets_valid(struct ccu_data *ccu)
29{
30 struct ccu_policy *ccu_policy = &ccu->policy;
31 u32 limit;
32
33 limit = ccu->range - sizeof(u32);
34 limit = round_down(limit, sizeof(u32));
35 if (ccu_policy_exists(ccu_policy)) {
36 if (ccu_policy->enable.offset > limit) {
37 pr_err("%s: bad policy enable offset for %s "
38 "(%u > %u)\n", __func__,
39 ccu->name, ccu_policy->enable.offset, limit);
40 return false;
41 }
42 if (ccu_policy->control.offset > limit) {
43 pr_err("%s: bad policy control offset for %s "
44 "(%u > %u)\n", __func__,
45 ccu->name, ccu_policy->control.offset, limit);
46 return false;
47 }
48 }
49
50 return true;
51}
52
28static bool clk_requires_trigger(struct kona_clk *bcm_clk) 53static bool clk_requires_trigger(struct kona_clk *bcm_clk)
29{ 54{
30 struct peri_clk_data *peri = bcm_clk->u.peri; 55 struct peri_clk_data *peri = bcm_clk->u.peri;
@@ -54,6 +79,7 @@ static bool clk_requires_trigger(struct kona_clk *bcm_clk)
54static bool peri_clk_data_offsets_valid(struct kona_clk *bcm_clk) 79static bool peri_clk_data_offsets_valid(struct kona_clk *bcm_clk)
55{ 80{
56 struct peri_clk_data *peri; 81 struct peri_clk_data *peri;
82 struct bcm_clk_policy *policy;
57 struct bcm_clk_gate *gate; 83 struct bcm_clk_gate *gate;
58 struct bcm_clk_div *div; 84 struct bcm_clk_div *div;
59 struct bcm_clk_sel *sel; 85 struct bcm_clk_sel *sel;
@@ -70,6 +96,15 @@ static bool peri_clk_data_offsets_valid(struct kona_clk *bcm_clk)
70 limit = range - sizeof(u32); 96 limit = range - sizeof(u32);
71 limit = round_down(limit, sizeof(u32)); 97 limit = round_down(limit, sizeof(u32));
72 98
99 policy = &peri->policy;
100 if (policy_exists(policy)) {
101 if (policy->offset > limit) {
102 pr_err("%s: bad policy offset for %s (%u > %u)\n",
103 __func__, name, policy->offset, limit);
104 return false;
105 }
106 }
107
73 gate = &peri->gate; 108 gate = &peri->gate;
74 if (gate_exists(gate)) { 109 if (gate_exists(gate)) {
75 if (gate->offset > limit) { 110 if (gate->offset > limit) {
@@ -167,6 +202,36 @@ static bool bitfield_valid(u32 shift, u32 width, const char *field_name,
167 return true; 202 return true;
168} 203}
169 204
205static bool
206ccu_policy_valid(struct ccu_policy *ccu_policy, const char *ccu_name)
207{
208 struct bcm_lvm_en *enable = &ccu_policy->enable;
209 struct bcm_policy_ctl *control;
210
211 if (!bit_posn_valid(enable->bit, "policy enable", ccu_name))
212 return false;
213
214 control = &ccu_policy->control;
215 if (!bit_posn_valid(control->go_bit, "policy control GO", ccu_name))
216 return false;
217
218 if (!bit_posn_valid(control->atl_bit, "policy control ATL", ccu_name))
219 return false;
220
221 if (!bit_posn_valid(control->ac_bit, "policy control AC", ccu_name))
222 return false;
223
224 return true;
225}
226
227static bool policy_valid(struct bcm_clk_policy *policy, const char *clock_name)
228{
229 if (!bit_posn_valid(policy->bit, "policy", clock_name))
230 return false;
231
232 return true;
233}
234
170/* 235/*
171 * All gates, if defined, have a status bit, and for hardware-only 236 * All gates, if defined, have a status bit, and for hardware-only
172 * gates, that's it. Gates that can be software controlled also 237 * gates, that's it. Gates that can be software controlled also
@@ -312,6 +377,7 @@ static bool
312peri_clk_data_valid(struct kona_clk *bcm_clk) 377peri_clk_data_valid(struct kona_clk *bcm_clk)
313{ 378{
314 struct peri_clk_data *peri; 379 struct peri_clk_data *peri;
380 struct bcm_clk_policy *policy;
315 struct bcm_clk_gate *gate; 381 struct bcm_clk_gate *gate;
316 struct bcm_clk_sel *sel; 382 struct bcm_clk_sel *sel;
317 struct bcm_clk_div *div; 383 struct bcm_clk_div *div;
@@ -331,6 +397,11 @@ peri_clk_data_valid(struct kona_clk *bcm_clk)
331 397
332 peri = bcm_clk->u.peri; 398 peri = bcm_clk->u.peri;
333 name = bcm_clk->init_data.name; 399 name = bcm_clk->init_data.name;
400
401 policy = &peri->policy;
402 if (policy_exists(policy) && !policy_valid(policy, name))
403 return false;
404
334 gate = &peri->gate; 405 gate = &peri->gate;
335 if (gate_exists(gate) && !gate_valid(gate, "gate", name)) 406 if (gate_exists(gate) && !gate_valid(gate, "gate", name))
336 return false; 407 return false;
@@ -679,6 +750,21 @@ static void kona_ccu_teardown(struct ccu_data *ccu)
679 ccu->base = NULL; 750 ccu->base = NULL;
680} 751}
681 752
753static bool ccu_data_valid(struct ccu_data *ccu)
754{
755 struct ccu_policy *ccu_policy;
756
757 if (!ccu_data_offsets_valid(ccu))
758 return false;
759
760 ccu_policy = &ccu->policy;
761 if (ccu_policy_exists(ccu_policy))
762 if (!ccu_policy_valid(ccu_policy, ccu->name))
763 return false;
764
765 return true;
766}
767
682/* 768/*
683 * Set up a CCU. Call the provided ccu_clks_setup callback to 769 * Set up a CCU. Call the provided ccu_clks_setup callback to
684 * initialize the array of clocks provided by the CCU. 770 * initialize the array of clocks provided by the CCU.
@@ -718,6 +804,12 @@ void __init kona_dt_ccu_setup(struct ccu_data *ccu,
718 } 804 }
719 805
720 ccu->range = (u32)range; 806 ccu->range = (u32)range;
807
808 if (!ccu_data_valid(ccu)) {
809 pr_err("%s: ccu data not valid for %s\n", __func__, node->name);
810 goto out_err;
811 }
812
721 ccu->base = ioremap(res.start, ccu->range); 813 ccu->base = ioremap(res.start, ccu->range);
722 if (!ccu->base) { 814 if (!ccu->base) {
723 pr_err("%s: unable to map CCU registers for %s\n", __func__, 815 pr_err("%s: unable to map CCU registers for %s\n", __func__,
diff --git a/drivers/clk/bcm/clk-kona.c b/drivers/clk/bcm/clk-kona.c
index f8bc6bc0784f..7d90c34d1336 100644
--- a/drivers/clk/bcm/clk-kona.c
+++ b/drivers/clk/bcm/clk-kona.c
@@ -16,6 +16,14 @@
16 16
17#include <linux/delay.h> 17#include <linux/delay.h>
18 18
19/*
20 * "Policies" affect the frequencies of bus clocks provided by a
21 * CCU. (I believe these polices are named "Deep Sleep", "Economy",
22 * "Normal", and "Turbo".) A lower policy number has lower power
23 * consumption, and policy 2 is the default.
24 */
25#define CCU_POLICY_COUNT 4
26
19#define CCU_ACCESS_PASSWORD 0xA5A500 27#define CCU_ACCESS_PASSWORD 0xA5A500
20#define CLK_GATE_DELAY_LOOP 2000 28#define CLK_GATE_DELAY_LOOP 2000
21 29
@@ -213,6 +221,148 @@ __ccu_wait_bit(struct ccu_data *ccu, u32 reg_offset, u32 bit, bool want)
213 return false; 221 return false;
214} 222}
215 223
224/* Policy operations */
225
226static bool __ccu_policy_engine_start(struct ccu_data *ccu, bool sync)
227{
228 struct bcm_policy_ctl *control = &ccu->policy.control;
229 u32 offset;
230 u32 go_bit;
231 u32 mask;
232 bool ret;
233
234 /* If we don't need to control policy for this CCU, we're done. */
235 if (!policy_ctl_exists(control))
236 return true;
237
238 offset = control->offset;
239 go_bit = control->go_bit;
240
241 /* Ensure we're not busy before we start */
242 ret = __ccu_wait_bit(ccu, offset, go_bit, false);
243 if (!ret) {
244 pr_err("%s: ccu %s policy engine wouldn't go idle\n",
245 __func__, ccu->name);
246 return false;
247 }
248
249 /*
250 * If it's a synchronous request, we'll wait for the voltage
251 * and frequency of the active load to stabilize before
252 * returning. To do this we select the active load by
253 * setting the ATL bit.
254 *
255 * An asynchronous request instead ramps the voltage in the
256 * background, and when that process stabilizes, the target
257 * load is copied to the active load and the CCU frequency
258 * is switched. We do this by selecting the target load
259 * (ATL bit clear) and setting the request auto-copy (AC bit
260 * set).
261 *
262 * Note, we do NOT read-modify-write this register.
263 */
264 mask = (u32)1 << go_bit;
265 if (sync)
266 mask |= 1 << control->atl_bit;
267 else
268 mask |= 1 << control->ac_bit;
269 __ccu_write(ccu, offset, mask);
270
271 /* Wait for indication that operation is complete. */
272 ret = __ccu_wait_bit(ccu, offset, go_bit, false);
273 if (!ret)
274 pr_err("%s: ccu %s policy engine never started\n",
275 __func__, ccu->name);
276
277 return ret;
278}
279
280static bool __ccu_policy_engine_stop(struct ccu_data *ccu)
281{
282 struct bcm_lvm_en *enable = &ccu->policy.enable;
283 u32 offset;
284 u32 enable_bit;
285 bool ret;
286
287 /* If we don't need to control policy for this CCU, we're done. */
288 if (!policy_lvm_en_exists(enable))
289 return true;
290
291 /* Ensure we're not busy before we start */
292 offset = enable->offset;
293 enable_bit = enable->bit;
294 ret = __ccu_wait_bit(ccu, offset, enable_bit, false);
295 if (!ret) {
296 pr_err("%s: ccu %s policy engine already stopped\n",
297 __func__, ccu->name);
298 return false;
299 }
300
301 /* Now set the bit to stop the engine (NO read-modify-write) */
302 __ccu_write(ccu, offset, (u32)1 << enable_bit);
303
304 /* Wait for indication that it has stopped. */
305 ret = __ccu_wait_bit(ccu, offset, enable_bit, false);
306 if (!ret)
307 pr_err("%s: ccu %s policy engine never stopped\n",
308 __func__, ccu->name);
309
310 return ret;
311}
312
313/*
314 * A CCU has four operating conditions ("policies"), and some clocks
315 * can be disabled or enabled based on which policy is currently in
316 * effect. Such clocks have a bit in a "policy mask" register for
317 * each policy indicating whether the clock is enabled for that
318 * policy or not. The bit position for a clock is the same for all
319 * four registers, and the 32-bit registers are at consecutive
320 * addresses.
321 */
322static bool policy_init(struct ccu_data *ccu, struct bcm_clk_policy *policy)
323{
324 u32 offset;
325 u32 mask;
326 int i;
327 bool ret;
328
329 if (!policy_exists(policy))
330 return true;
331
332 /*
333 * We need to stop the CCU policy engine to allow update
334 * of our policy bits.
335 */
336 if (!__ccu_policy_engine_stop(ccu)) {
337 pr_err("%s: unable to stop CCU %s policy engine\n",
338 __func__, ccu->name);
339 return false;
340 }
341
342 /*
343 * For now, if a clock defines its policy bit we just mark
344 * it "enabled" for all four policies.
345 */
346 offset = policy->offset;
347 mask = (u32)1 << policy->bit;
348 for (i = 0; i < CCU_POLICY_COUNT; i++) {
349 u32 reg_val;
350
351 reg_val = __ccu_read(ccu, offset);
352 reg_val |= mask;
353 __ccu_write(ccu, offset, reg_val);
354 offset += sizeof(u32);
355 }
356
357 /* We're done updating; fire up the policy engine again. */
358 ret = __ccu_policy_engine_start(ccu, true);
359 if (!ret)
360 pr_err("%s: unable to restart CCU %s policy engine\n",
361 __func__, ccu->name);
362
363 return ret;
364}
365
216/* Gate operations */ 366/* Gate operations */
217 367
218/* Determine whether a clock is gated. CCU lock must be held. */ 368/* Determine whether a clock is gated. CCU lock must be held. */
@@ -972,6 +1122,11 @@ static bool __peri_clk_init(struct kona_clk *bcm_clk)
972 1122
973 BUG_ON(bcm_clk->type != bcm_clk_peri); 1123 BUG_ON(bcm_clk->type != bcm_clk_peri);
974 1124
1125 if (!policy_init(ccu, &peri->policy)) {
1126 pr_err("%s: error initializing policy for %s\n",
1127 __func__, name);
1128 return false;
1129 }
975 if (!gate_init(ccu, &peri->gate)) { 1130 if (!gate_init(ccu, &peri->gate)) {
976 pr_err("%s: error initializing gate for %s\n", __func__, name); 1131 pr_err("%s: error initializing gate for %s\n", __func__, name);
977 return false; 1132 return false;
diff --git a/drivers/clk/bcm/clk-kona.h b/drivers/clk/bcm/clk-kona.h
index 05d74779a564..4bcba1331561 100644
--- a/drivers/clk/bcm/clk-kona.h
+++ b/drivers/clk/bcm/clk-kona.h
@@ -43,8 +43,14 @@
43#define FLAG_FLIP(obj, type, flag) ((obj)->flags ^= FLAG(type, flag)) 43#define FLAG_FLIP(obj, type, flag) ((obj)->flags ^= FLAG(type, flag))
44#define FLAG_TEST(obj, type, flag) (!!((obj)->flags & FLAG(type, flag))) 44#define FLAG_TEST(obj, type, flag) (!!((obj)->flags & FLAG(type, flag)))
45 45
46/* CCU field state tests */
47
48#define ccu_policy_exists(ccu_policy) ((ccu_policy)->enable.offset != 0)
49
46/* Clock field state tests */ 50/* Clock field state tests */
47 51
52#define policy_exists(policy) ((policy)->offset != 0)
53
48#define gate_exists(gate) FLAG_TEST(gate, GATE, EXISTS) 54#define gate_exists(gate) FLAG_TEST(gate, GATE, EXISTS)
49#define gate_is_enabled(gate) FLAG_TEST(gate, GATE, ENABLED) 55#define gate_is_enabled(gate) FLAG_TEST(gate, GATE, ENABLED)
50#define gate_is_hw_controllable(gate) FLAG_TEST(gate, GATE, HW) 56#define gate_is_hw_controllable(gate) FLAG_TEST(gate, GATE, HW)
@@ -62,6 +68,9 @@
62#define selector_exists(sel) ((sel)->width != 0) 68#define selector_exists(sel) ((sel)->width != 0)
63#define trigger_exists(trig) FLAG_TEST(trig, TRIG, EXISTS) 69#define trigger_exists(trig) FLAG_TEST(trig, TRIG, EXISTS)
64 70
71#define policy_lvm_en_exists(enable) ((enable)->offset != 0)
72#define policy_ctl_exists(control) ((control)->offset != 0)
73
65/* Clock type, used to tell common block what it's part of */ 74/* Clock type, used to tell common block what it's part of */
66enum bcm_clk_type { 75enum bcm_clk_type {
67 bcm_clk_none, /* undefined clock type */ 76 bcm_clk_none, /* undefined clock type */
@@ -71,6 +80,27 @@ enum bcm_clk_type {
71}; 80};
72 81
73/* 82/*
83 * CCU policy control for clocks. Clocks can be enabled or disabled
84 * based on the CCU policy in effect. One bit in each policy mask
85 * register (one per CCU policy) represents whether the clock is
86 * enabled when that policy is effect or not. The CCU policy engine
87 * must be stopped to update these bits, and must be restarted again
88 * afterward.
89 */
90struct bcm_clk_policy {
91 u32 offset; /* first policy mask register offset */
92 u32 bit; /* bit used in all mask registers */
93};
94
95/* Policy initialization macro */
96
97#define POLICY(_offset, _bit) \
98 { \
99 .offset = (_offset), \
100 .bit = (_bit), \
101 }
102
103/*
74 * Gating control and status is managed by a 32-bit gate register. 104 * Gating control and status is managed by a 32-bit gate register.
75 * 105 *
76 * There are several types of gating available: 106 * There are several types of gating available:
@@ -340,6 +370,7 @@ struct bcm_clk_trig {
340 } 370 }
341 371
342struct peri_clk_data { 372struct peri_clk_data {
373 struct bcm_clk_policy policy;
343 struct bcm_clk_gate gate; 374 struct bcm_clk_gate gate;
344 struct bcm_clk_trig pre_trig; 375 struct bcm_clk_trig pre_trig;
345 struct bcm_clk_div pre_div; 376 struct bcm_clk_div pre_div;
@@ -378,6 +409,45 @@ struct kona_clk {
378#define LAST_KONA_CLK { .type = bcm_clk_none } 409#define LAST_KONA_CLK { .type = bcm_clk_none }
379 410
380/* 411/*
412 * CCU policy control. To enable software update of the policy
413 * tables the CCU policy engine must be stopped by setting the
414 * software update enable bit (LVM_EN). After an update the engine
415 * is restarted using the GO bit and either the GO_ATL or GO_AC bit.
416 */
417struct bcm_lvm_en {
418 u32 offset; /* LVM_EN register offset */
419 u32 bit; /* POLICY_CONFIG_EN bit in register */
420};
421
422/* Policy enable initialization macro */
423#define CCU_LVM_EN(_offset, _bit) \
424 { \
425 .offset = (_offset), \
426 .bit = (_bit), \
427 }
428
429struct bcm_policy_ctl {
430 u32 offset; /* POLICY_CTL register offset */
431 u32 go_bit;
432 u32 atl_bit; /* GO, GO_ATL, and GO_AC bits */
433 u32 ac_bit;
434};
435
436/* Policy control initialization macro */
437#define CCU_POLICY_CTL(_offset, _go_bit, _ac_bit, _atl_bit) \
438 { \
439 .offset = (_offset), \
440 .go_bit = (_go_bit), \
441 .ac_bit = (_ac_bit), \
442 .atl_bit = (_atl_bit), \
443 }
444
445struct ccu_policy {
446 struct bcm_lvm_en enable;
447 struct bcm_policy_ctl control;
448};
449
450/*
381 * Each CCU defines a mapped area of memory containing registers 451 * Each CCU defines a mapped area of memory containing registers
382 * used to manage clocks implemented by the CCU. Access to memory 452 * used to manage clocks implemented by the CCU. Access to memory
383 * within the CCU's space is serialized by a spinlock. Before any 453 * within the CCU's space is serialized by a spinlock. Before any
@@ -390,6 +460,7 @@ struct ccu_data {
390 void __iomem *base; /* base of mapped address space */ 460 void __iomem *base; /* base of mapped address space */
391 spinlock_t lock; /* serialization lock */ 461 spinlock_t lock; /* serialization lock */
392 bool write_enabled; /* write access is currently enabled */ 462 bool write_enabled; /* write access is currently enabled */
463 struct ccu_policy policy;
393 struct list_head links; /* for ccu_list */ 464 struct list_head links; /* for ccu_list */
394 struct device_node *node; 465 struct device_node *node;
395 struct clk_onecell_data clk_data; 466 struct clk_onecell_data clk_data;