blob: 4bcba13315619884817c7ce338681745baf1b000 [file] [log] [blame]
Alex Elder1f27f152014-02-14 12:29:18 -06001/*
2 * Copyright (C) 2013 Broadcom Corporation
3 * Copyright 2013 Linaro Limited
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation version 2.
8 *
9 * This program is distributed "as is" WITHOUT ANY WARRANTY of any
10 * kind, whether express or implied; without even the implied warranty
11 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 */
14
15#ifndef _CLK_KONA_H
16#define _CLK_KONA_H
17
18#include <linux/kernel.h>
19#include <linux/list.h>
20#include <linux/spinlock.h>
21#include <linux/slab.h>
22#include <linux/device.h>
23#include <linux/of.h>
24#include <linux/clk-provider.h>
25
26#define BILLION 1000000000
27
28/* The common clock framework uses u8 to represent a parent index */
29#define PARENT_COUNT_MAX ((u32)U8_MAX)
30
31#define BAD_CLK_INDEX U8_MAX /* Can't ever be valid */
32#define BAD_CLK_NAME ((const char *)-1)
33
34#define BAD_SCALED_DIV_VALUE U64_MAX
35
36/*
37 * Utility macros for object flag management. If possible, flags
38 * should be defined such that 0 is the desired default value.
39 */
40#define FLAG(type, flag) BCM_CLK_ ## type ## _FLAGS_ ## flag
41#define FLAG_SET(obj, type, flag) ((obj)->flags |= FLAG(type, flag))
42#define FLAG_CLEAR(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)))
45
Alex Eldera597fac2014-04-21 16:11:42 -050046/* CCU field state tests */
47
48#define ccu_policy_exists(ccu_policy) ((ccu_policy)->enable.offset != 0)
49
Alex Elder1f27f152014-02-14 12:29:18 -060050/* Clock field state tests */
51
Alex Eldera597fac2014-04-21 16:11:42 -050052#define policy_exists(policy) ((policy)->offset != 0)
53
Alex Elder1f27f152014-02-14 12:29:18 -060054#define gate_exists(gate) FLAG_TEST(gate, GATE, EXISTS)
55#define gate_is_enabled(gate) FLAG_TEST(gate, GATE, ENABLED)
56#define gate_is_hw_controllable(gate) FLAG_TEST(gate, GATE, HW)
57#define gate_is_sw_controllable(gate) FLAG_TEST(gate, GATE, SW)
58#define gate_is_sw_managed(gate) FLAG_TEST(gate, GATE, SW_MANAGED)
59#define gate_is_no_disable(gate) FLAG_TEST(gate, GATE, NO_DISABLE)
60
61#define gate_flip_enabled(gate) FLAG_FLIP(gate, GATE, ENABLED)
62
63#define divider_exists(div) FLAG_TEST(div, DIV, EXISTS)
64#define divider_is_fixed(div) FLAG_TEST(div, DIV, FIXED)
65#define divider_has_fraction(div) (!divider_is_fixed(div) && \
Alex Eldere813d492014-04-07 08:22:12 -050066 (div)->u.s.frac_width > 0)
Alex Elder1f27f152014-02-14 12:29:18 -060067
68#define selector_exists(sel) ((sel)->width != 0)
69#define trigger_exists(trig) FLAG_TEST(trig, TRIG, EXISTS)
70
Alex Eldera597fac2014-04-21 16:11:42 -050071#define policy_lvm_en_exists(enable) ((enable)->offset != 0)
72#define policy_ctl_exists(control) ((control)->offset != 0)
73
Alex Elder1f27f152014-02-14 12:29:18 -060074/* Clock type, used to tell common block what it's part of */
75enum bcm_clk_type {
76 bcm_clk_none, /* undefined clock type */
77 bcm_clk_bus,
78 bcm_clk_core,
79 bcm_clk_peri
80};
81
82/*
Alex Eldera597fac2014-04-21 16:11:42 -050083 * 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/*
Alex Elder1f27f152014-02-14 12:29:18 -0600104 * Gating control and status is managed by a 32-bit gate register.
105 *
106 * There are several types of gating available:
107 * - (no gate)
108 * A clock with no gate is assumed to be always enabled.
109 * - hardware-only gating (auto-gating)
110 * Enabling or disabling clocks with this type of gate is
111 * managed automatically by the hardware. Such clocks can be
112 * considered by the software to be enabled. The current status
113 * of auto-gated clocks can be read from the gate status bit.
114 * - software-only gating
115 * Auto-gating is not available for this type of clock.
116 * Instead, software manages whether it's enabled by setting or
117 * clearing the enable bit. The current gate status of a gate
118 * under software control can be read from the gate status bit.
119 * To ensure a change to the gating status is complete, the
120 * status bit can be polled to verify that the gate has entered
121 * the desired state.
122 * - selectable hardware or software gating
123 * Gating for this type of clock can be configured to be either
124 * under software or hardware control. Which type is in use is
125 * determined by the hw_sw_sel bit of the gate register.
126 */
127struct bcm_clk_gate {
128 u32 offset; /* gate register offset */
129 u32 status_bit; /* 0: gate is disabled; 0: gatge is enabled */
130 u32 en_bit; /* 0: disable; 1: enable */
131 u32 hw_sw_sel_bit; /* 0: hardware gating; 1: software gating */
132 u32 flags; /* BCM_CLK_GATE_FLAGS_* below */
133};
134
135/*
136 * Gate flags:
137 * HW means this gate can be auto-gated
138 * SW means the state of this gate can be software controlled
139 * NO_DISABLE means this gate is (only) enabled if under software control
140 * SW_MANAGED means the status of this gate is under software control
141 * ENABLED means this software-managed gate is *supposed* to be enabled
142 */
143#define BCM_CLK_GATE_FLAGS_EXISTS ((u32)1 << 0) /* Gate is valid */
144#define BCM_CLK_GATE_FLAGS_HW ((u32)1 << 1) /* Can auto-gate */
145#define BCM_CLK_GATE_FLAGS_SW ((u32)1 << 2) /* Software control */
146#define BCM_CLK_GATE_FLAGS_NO_DISABLE ((u32)1 << 3) /* HW or enabled */
147#define BCM_CLK_GATE_FLAGS_SW_MANAGED ((u32)1 << 4) /* SW now in control */
148#define BCM_CLK_GATE_FLAGS_ENABLED ((u32)1 << 5) /* If SW_MANAGED */
149
150/*
151 * Gate initialization macros.
152 *
153 * Any gate initially under software control will be enabled.
154 */
155
156/* A hardware/software gate initially under software control */
157#define HW_SW_GATE(_offset, _status_bit, _en_bit, _hw_sw_sel_bit) \
158 { \
159 .offset = (_offset), \
160 .status_bit = (_status_bit), \
161 .en_bit = (_en_bit), \
162 .hw_sw_sel_bit = (_hw_sw_sel_bit), \
163 .flags = FLAG(GATE, HW)|FLAG(GATE, SW)| \
164 FLAG(GATE, SW_MANAGED)|FLAG(GATE, ENABLED)| \
165 FLAG(GATE, EXISTS), \
166 }
167
168/* A hardware/software gate initially under hardware control */
169#define HW_SW_GATE_AUTO(_offset, _status_bit, _en_bit, _hw_sw_sel_bit) \
170 { \
171 .offset = (_offset), \
172 .status_bit = (_status_bit), \
173 .en_bit = (_en_bit), \
174 .hw_sw_sel_bit = (_hw_sw_sel_bit), \
175 .flags = FLAG(GATE, HW)|FLAG(GATE, SW)| \
176 FLAG(GATE, EXISTS), \
177 }
178
179/* A hardware-or-enabled gate (enabled if not under hardware control) */
180#define HW_ENABLE_GATE(_offset, _status_bit, _en_bit, _hw_sw_sel_bit) \
181 { \
182 .offset = (_offset), \
183 .status_bit = (_status_bit), \
184 .en_bit = (_en_bit), \
185 .hw_sw_sel_bit = (_hw_sw_sel_bit), \
186 .flags = FLAG(GATE, HW)|FLAG(GATE, SW)| \
187 FLAG(GATE, NO_DISABLE)|FLAG(GATE, EXISTS), \
188 }
189
190/* A software-only gate */
191#define SW_ONLY_GATE(_offset, _status_bit, _en_bit) \
192 { \
193 .offset = (_offset), \
194 .status_bit = (_status_bit), \
195 .en_bit = (_en_bit), \
196 .flags = FLAG(GATE, SW)|FLAG(GATE, SW_MANAGED)| \
197 FLAG(GATE, ENABLED)|FLAG(GATE, EXISTS), \
198 }
199
200/* A hardware-only gate */
201#define HW_ONLY_GATE(_offset, _status_bit) \
202 { \
203 .offset = (_offset), \
204 .status_bit = (_status_bit), \
205 .flags = FLAG(GATE, HW)|FLAG(GATE, EXISTS), \
206 }
207
208/*
209 * Each clock can have zero, one, or two dividers which change the
210 * output rate of the clock. Each divider can be either fixed or
211 * variable. If there are two dividers, they are the "pre-divider"
212 * and the "regular" or "downstream" divider. If there is only one,
213 * there is no pre-divider.
214 *
215 * A fixed divider is any non-zero (positive) value, and it
216 * indicates how the input rate is affected by the divider.
217 *
218 * The value of a variable divider is maintained in a sub-field of a
219 * 32-bit divider register. The position of the field in the
220 * register is defined by its offset and width. The value recorded
221 * in this field is always 1 less than the value it represents.
222 *
223 * In addition, a variable divider can indicate that some subset
224 * of its bits represent a "fractional" part of the divider. Such
225 * bits comprise the low-order portion of the divider field, and can
226 * be viewed as representing the portion of the divider that lies to
227 * the right of the decimal point. Most variable dividers have zero
228 * fractional bits. Variable dividers with non-zero fraction width
229 * still record a value 1 less than the value they represent; the
230 * added 1 does *not* affect the low-order bit in this case, it
231 * affects the bits above the fractional part only. (Often in this
232 * code a divider field value is distinguished from the value it
233 * represents by referring to the latter as a "divisor".)
234 *
235 * In order to avoid dealing with fractions, divider arithmetic is
236 * performed using "scaled" values. A scaled value is one that's
237 * been left-shifted by the fractional width of a divider. Dividing
238 * a scaled value by a scaled divisor produces the desired quotient
239 * without loss of precision and without any other special handling
240 * for fractions.
241 *
242 * The recorded value of a variable divider can be modified. To
243 * modify either divider (or both), a clock must be enabled (i.e.,
244 * using its gate). In addition, a trigger register (described
245 * below) must be used to commit the change, and polled to verify
246 * the change is complete.
247 */
248struct bcm_clk_div {
249 union {
250 struct { /* variable divider */
251 u32 offset; /* divider register offset */
252 u32 shift; /* field shift */
253 u32 width; /* field width */
254 u32 frac_width; /* field fraction width */
255
256 u64 scaled_div; /* scaled divider value */
Alex Eldere813d492014-04-07 08:22:12 -0500257 } s;
Alex Elder1f27f152014-02-14 12:29:18 -0600258 u32 fixed; /* non-zero fixed divider value */
Alex Eldere813d492014-04-07 08:22:12 -0500259 } u;
Alex Elder1f27f152014-02-14 12:29:18 -0600260 u32 flags; /* BCM_CLK_DIV_FLAGS_* below */
261};
262
263/*
264 * Divider flags:
265 * EXISTS means this divider exists
266 * FIXED means it is a fixed-rate divider
267 */
268#define BCM_CLK_DIV_FLAGS_EXISTS ((u32)1 << 0) /* Divider is valid */
269#define BCM_CLK_DIV_FLAGS_FIXED ((u32)1 << 1) /* Fixed-value */
270
271/* Divider initialization macros */
272
273/* A fixed (non-zero) divider */
274#define FIXED_DIVIDER(_value) \
275 { \
Alex Eldere813d492014-04-07 08:22:12 -0500276 .u.fixed = (_value), \
Alex Elder1f27f152014-02-14 12:29:18 -0600277 .flags = FLAG(DIV, EXISTS)|FLAG(DIV, FIXED), \
278 }
279
280/* A divider with an integral divisor */
281#define DIVIDER(_offset, _shift, _width) \
282 { \
Alex Eldere813d492014-04-07 08:22:12 -0500283 .u.s.offset = (_offset), \
284 .u.s.shift = (_shift), \
285 .u.s.width = (_width), \
286 .u.s.scaled_div = BAD_SCALED_DIV_VALUE, \
Alex Elder1f27f152014-02-14 12:29:18 -0600287 .flags = FLAG(DIV, EXISTS), \
288 }
289
290/* A divider whose divisor has an integer and fractional part */
291#define FRAC_DIVIDER(_offset, _shift, _width, _frac_width) \
292 { \
Alex Eldere813d492014-04-07 08:22:12 -0500293 .u.s.offset = (_offset), \
294 .u.s.shift = (_shift), \
295 .u.s.width = (_width), \
296 .u.s.frac_width = (_frac_width), \
297 .u.s.scaled_div = BAD_SCALED_DIV_VALUE, \
Alex Elder1f27f152014-02-14 12:29:18 -0600298 .flags = FLAG(DIV, EXISTS), \
299 }
300
301/*
302 * Clocks may have multiple "parent" clocks. If there is more than
303 * one, a selector must be specified to define which of the parent
304 * clocks is currently in use. The selected clock is indicated in a
305 * sub-field of a 32-bit selector register. The range of
306 * representable selector values typically exceeds the number of
307 * available parent clocks. Occasionally the reset value of a
308 * selector field is explicitly set to a (specific) value that does
309 * not correspond to a defined input clock.
310 *
311 * We register all known parent clocks with the common clock code
312 * using a packed array (i.e., no empty slots) of (parent) clock
313 * names, and refer to them later using indexes into that array.
314 * We maintain an array of selector values indexed by common clock
315 * index values in order to map between these common clock indexes
316 * and the selector values used by the hardware.
317 *
318 * Like dividers, a selector can be modified, but to do so a clock
319 * must be enabled, and a trigger must be used to commit the change.
320 */
321struct bcm_clk_sel {
322 u32 offset; /* selector register offset */
323 u32 shift; /* field shift */
324 u32 width; /* field width */
325
326 u32 parent_count; /* number of entries in parent_sel[] */
327 u32 *parent_sel; /* array of parent selector values */
328 u8 clk_index; /* current selected index in parent_sel[] */
329};
330
331/* Selector initialization macro */
332#define SELECTOR(_offset, _shift, _width) \
333 { \
334 .offset = (_offset), \
335 .shift = (_shift), \
336 .width = (_width), \
337 .clk_index = BAD_CLK_INDEX, \
338 }
339
340/*
341 * Making changes to a variable divider or a selector for a clock
342 * requires the use of a trigger. A trigger is defined by a single
343 * bit within a register. To signal a change, a 1 is written into
344 * that bit. To determine when the change has been completed, that
345 * trigger bit is polled; the read value will be 1 while the change
346 * is in progress, and 0 when it is complete.
347 *
348 * Occasionally a clock will have more than one trigger. In this
349 * case, the "pre-trigger" will be used when changing a clock's
350 * selector and/or its pre-divider.
351 */
352struct bcm_clk_trig {
353 u32 offset; /* trigger register offset */
354 u32 bit; /* trigger bit */
355 u32 flags; /* BCM_CLK_TRIG_FLAGS_* below */
356};
357
358/*
359 * Trigger flags:
360 * EXISTS means this trigger exists
361 */
362#define BCM_CLK_TRIG_FLAGS_EXISTS ((u32)1 << 0) /* Trigger is valid */
363
364/* Trigger initialization macro */
365#define TRIGGER(_offset, _bit) \
366 { \
367 .offset = (_offset), \
368 .bit = (_bit), \
369 .flags = FLAG(TRIG, EXISTS), \
370 }
371
372struct peri_clk_data {
Alex Eldera597fac2014-04-21 16:11:42 -0500373 struct bcm_clk_policy policy;
Alex Elder1f27f152014-02-14 12:29:18 -0600374 struct bcm_clk_gate gate;
375 struct bcm_clk_trig pre_trig;
376 struct bcm_clk_div pre_div;
377 struct bcm_clk_trig trig;
378 struct bcm_clk_div div;
379 struct bcm_clk_sel sel;
380 const char *clocks[]; /* must be last; use CLOCKS() to declare */
381};
382#define CLOCKS(...) { __VA_ARGS__, NULL, }
383#define NO_CLOCKS { NULL, } /* Must use of no parent clocks */
384
385struct kona_clk {
386 struct clk_hw hw;
Alex Eldere7563252014-04-21 16:11:38 -0500387 struct clk_init_data init_data; /* includes name of this clock */
Alex Elder1f27f152014-02-14 12:29:18 -0600388 struct ccu_data *ccu; /* ccu this clock is associated with */
389 enum bcm_clk_type type;
390 union {
391 void *data;
392 struct peri_clk_data *peri;
Alex Eldere813d492014-04-07 08:22:12 -0500393 } u;
Alex Elder1f27f152014-02-14 12:29:18 -0600394};
395#define to_kona_clk(_hw) \
396 container_of(_hw, struct kona_clk, hw)
397
Alex Elder03548ec2014-04-21 16:11:41 -0500398/* Initialization macro for an entry in a CCU's kona_clks[] array. */
399#define KONA_CLK(_ccu_name, _clk_name, _type) \
400 { \
401 .init_data = { \
402 .name = #_clk_name, \
403 .ops = &kona_ ## _type ## _clk_ops, \
404 }, \
405 .ccu = &_ccu_name ## _ccu_data, \
406 .type = bcm_clk_ ## _type, \
407 .u.data = &_clk_name ## _data, \
408 }
409#define LAST_KONA_CLK { .type = bcm_clk_none }
410
411/*
Alex Eldera597fac2014-04-21 16:11:42 -0500412 * 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/*
Alex Elder03548ec2014-04-21 16:11:41 -0500451 * Each CCU defines a mapped area of memory containing registers
452 * used to manage clocks implemented by the CCU. Access to memory
453 * within the CCU's space is serialized by a spinlock. Before any
454 * (other) address can be written, a special access "password" value
455 * must be written to its WR_ACCESS register (located at the base
456 * address of the range). We keep track of the name of each CCU as
457 * it is set up, and maintain them in a list.
458 */
459struct ccu_data {
460 void __iomem *base; /* base of mapped address space */
461 spinlock_t lock; /* serialization lock */
462 bool write_enabled; /* write access is currently enabled */
Alex Eldera597fac2014-04-21 16:11:42 -0500463 struct ccu_policy policy;
Alex Elder03548ec2014-04-21 16:11:41 -0500464 struct list_head links; /* for ccu_list */
465 struct device_node *node;
466 struct clk_onecell_data clk_data;
467 const char *name;
468 u32 range; /* byte range of address space */
469 struct kona_clk kona_clks[]; /* must be last */
470};
471
472/* Initialization for common fields in a Kona ccu_data structure */
473#define KONA_CCU_COMMON(_prefix, _name, _ccuname) \
474 .name = #_name "_ccu", \
475 .lock = __SPIN_LOCK_UNLOCKED(_name ## _ccu_data.lock), \
476 .links = LIST_HEAD_INIT(_name ## _ccu_data.links), \
477 .clk_data = { \
478 .clk_num = _prefix ## _ ## _ccuname ## _CCU_CLOCK_COUNT, \
479 }
480
Alex Elder1f27f152014-02-14 12:29:18 -0600481/* Exported globals */
482
483extern struct clk_ops kona_peri_clk_ops;
484
Alex Elder1f27f152014-02-14 12:29:18 -0600485/* Externally visible functions */
486
487extern u64 do_div_round_closest(u64 dividend, unsigned long divisor);
488extern u64 scaled_div_max(struct bcm_clk_div *div);
489extern u64 scaled_div_build(struct bcm_clk_div *div, u32 div_value,
490 u32 billionths);
491
Alex Elder03548ec2014-04-21 16:11:41 -0500492extern struct clk *kona_clk_setup(struct kona_clk *bcm_clk);
Alex Elderb12151c2014-04-21 16:11:40 -0500493extern void __init kona_dt_ccu_setup(struct ccu_data *ccu,
Alex Elder03548ec2014-04-21 16:11:41 -0500494 struct device_node *node);
Alex Elder1f27f152014-02-14 12:29:18 -0600495extern bool __init kona_ccu_init(struct ccu_data *ccu);
496
497#endif /* _CLK_KONA_H */