clk: bcm281xx: initialize CCU structures statically
[linux-2.6-block.git] / drivers / clk / bcm / clk-kona.h
CommitLineData
1f27f152
AE
1/*
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
46/* Clock field state tests */
47
48#define gate_exists(gate) FLAG_TEST(gate, GATE, EXISTS)
49#define gate_is_enabled(gate) FLAG_TEST(gate, GATE, ENABLED)
50#define gate_is_hw_controllable(gate) FLAG_TEST(gate, GATE, HW)
51#define gate_is_sw_controllable(gate) FLAG_TEST(gate, GATE, SW)
52#define gate_is_sw_managed(gate) FLAG_TEST(gate, GATE, SW_MANAGED)
53#define gate_is_no_disable(gate) FLAG_TEST(gate, GATE, NO_DISABLE)
54
55#define gate_flip_enabled(gate) FLAG_FLIP(gate, GATE, ENABLED)
56
57#define divider_exists(div) FLAG_TEST(div, DIV, EXISTS)
58#define divider_is_fixed(div) FLAG_TEST(div, DIV, FIXED)
59#define divider_has_fraction(div) (!divider_is_fixed(div) && \
e813d49d 60 (div)->u.s.frac_width > 0)
1f27f152
AE
61
62#define selector_exists(sel) ((sel)->width != 0)
63#define trigger_exists(trig) FLAG_TEST(trig, TRIG, EXISTS)
64
65/* Clock type, used to tell common block what it's part of */
66enum bcm_clk_type {
67 bcm_clk_none, /* undefined clock type */
68 bcm_clk_bus,
69 bcm_clk_core,
70 bcm_clk_peri
71};
72
73/*
74 * Each CCU defines a mapped area of memory containing registers
75 * used to manage clocks implemented by the CCU. Access to memory
76 * within the CCU's space is serialized by a spinlock. Before any
77 * (other) address can be written, a special access "password" value
78 * must be written to its WR_ACCESS register (located at the base
79 * address of the range). We keep track of the name of each CCU as
80 * it is set up, and maintain them in a list.
81 */
82struct ccu_data {
83 void __iomem *base; /* base of mapped address space */
84 spinlock_t lock; /* serialization lock */
85 bool write_enabled; /* write access is currently enabled */
86 struct list_head links; /* for ccu_list */
87 struct device_node *node;
b12151ca 88 struct clk_onecell_data clk_data;
1f27f152
AE
89 const char *name;
90 u32 range; /* byte range of address space */
91};
92
b12151ca
AE
93/* Initialization for common fields in a Kona ccu_data structure */
94#define KONA_CCU_COMMON(_prefix, _name, _ucase_name) \
95 .name = #_name "_ccu", \
96 .lock = __SPIN_LOCK_UNLOCKED(_name ## _ccu_data.lock), \
97 .links = LIST_HEAD_INIT(_name ## _ccu_data.links), \
98 .clk_data = { \
99 .clk_num = _prefix ## _ ## _ucase_name ## _CCU_CLOCK_COUNT, \
100 }
101
1f27f152
AE
102/*
103 * Gating control and status is managed by a 32-bit gate register.
104 *
105 * There are several types of gating available:
106 * - (no gate)
107 * A clock with no gate is assumed to be always enabled.
108 * - hardware-only gating (auto-gating)
109 * Enabling or disabling clocks with this type of gate is
110 * managed automatically by the hardware. Such clocks can be
111 * considered by the software to be enabled. The current status
112 * of auto-gated clocks can be read from the gate status bit.
113 * - software-only gating
114 * Auto-gating is not available for this type of clock.
115 * Instead, software manages whether it's enabled by setting or
116 * clearing the enable bit. The current gate status of a gate
117 * under software control can be read from the gate status bit.
118 * To ensure a change to the gating status is complete, the
119 * status bit can be polled to verify that the gate has entered
120 * the desired state.
121 * - selectable hardware or software gating
122 * Gating for this type of clock can be configured to be either
123 * under software or hardware control. Which type is in use is
124 * determined by the hw_sw_sel bit of the gate register.
125 */
126struct bcm_clk_gate {
127 u32 offset; /* gate register offset */
128 u32 status_bit; /* 0: gate is disabled; 0: gatge is enabled */
129 u32 en_bit; /* 0: disable; 1: enable */
130 u32 hw_sw_sel_bit; /* 0: hardware gating; 1: software gating */
131 u32 flags; /* BCM_CLK_GATE_FLAGS_* below */
132};
133
134/*
135 * Gate flags:
136 * HW means this gate can be auto-gated
137 * SW means the state of this gate can be software controlled
138 * NO_DISABLE means this gate is (only) enabled if under software control
139 * SW_MANAGED means the status of this gate is under software control
140 * ENABLED means this software-managed gate is *supposed* to be enabled
141 */
142#define BCM_CLK_GATE_FLAGS_EXISTS ((u32)1 << 0) /* Gate is valid */
143#define BCM_CLK_GATE_FLAGS_HW ((u32)1 << 1) /* Can auto-gate */
144#define BCM_CLK_GATE_FLAGS_SW ((u32)1 << 2) /* Software control */
145#define BCM_CLK_GATE_FLAGS_NO_DISABLE ((u32)1 << 3) /* HW or enabled */
146#define BCM_CLK_GATE_FLAGS_SW_MANAGED ((u32)1 << 4) /* SW now in control */
147#define BCM_CLK_GATE_FLAGS_ENABLED ((u32)1 << 5) /* If SW_MANAGED */
148
149/*
150 * Gate initialization macros.
151 *
152 * Any gate initially under software control will be enabled.
153 */
154
155/* A hardware/software gate initially under software control */
156#define HW_SW_GATE(_offset, _status_bit, _en_bit, _hw_sw_sel_bit) \
157 { \
158 .offset = (_offset), \
159 .status_bit = (_status_bit), \
160 .en_bit = (_en_bit), \
161 .hw_sw_sel_bit = (_hw_sw_sel_bit), \
162 .flags = FLAG(GATE, HW)|FLAG(GATE, SW)| \
163 FLAG(GATE, SW_MANAGED)|FLAG(GATE, ENABLED)| \
164 FLAG(GATE, EXISTS), \
165 }
166
167/* A hardware/software gate initially under hardware control */
168#define HW_SW_GATE_AUTO(_offset, _status_bit, _en_bit, _hw_sw_sel_bit) \
169 { \
170 .offset = (_offset), \
171 .status_bit = (_status_bit), \
172 .en_bit = (_en_bit), \
173 .hw_sw_sel_bit = (_hw_sw_sel_bit), \
174 .flags = FLAG(GATE, HW)|FLAG(GATE, SW)| \
175 FLAG(GATE, EXISTS), \
176 }
177
178/* A hardware-or-enabled gate (enabled if not under hardware control) */
179#define HW_ENABLE_GATE(_offset, _status_bit, _en_bit, _hw_sw_sel_bit) \
180 { \
181 .offset = (_offset), \
182 .status_bit = (_status_bit), \
183 .en_bit = (_en_bit), \
184 .hw_sw_sel_bit = (_hw_sw_sel_bit), \
185 .flags = FLAG(GATE, HW)|FLAG(GATE, SW)| \
186 FLAG(GATE, NO_DISABLE)|FLAG(GATE, EXISTS), \
187 }
188
189/* A software-only gate */
190#define SW_ONLY_GATE(_offset, _status_bit, _en_bit) \
191 { \
192 .offset = (_offset), \
193 .status_bit = (_status_bit), \
194 .en_bit = (_en_bit), \
195 .flags = FLAG(GATE, SW)|FLAG(GATE, SW_MANAGED)| \
196 FLAG(GATE, ENABLED)|FLAG(GATE, EXISTS), \
197 }
198
199/* A hardware-only gate */
200#define HW_ONLY_GATE(_offset, _status_bit) \
201 { \
202 .offset = (_offset), \
203 .status_bit = (_status_bit), \
204 .flags = FLAG(GATE, HW)|FLAG(GATE, EXISTS), \
205 }
206
207/*
208 * Each clock can have zero, one, or two dividers which change the
209 * output rate of the clock. Each divider can be either fixed or
210 * variable. If there are two dividers, they are the "pre-divider"
211 * and the "regular" or "downstream" divider. If there is only one,
212 * there is no pre-divider.
213 *
214 * A fixed divider is any non-zero (positive) value, and it
215 * indicates how the input rate is affected by the divider.
216 *
217 * The value of a variable divider is maintained in a sub-field of a
218 * 32-bit divider register. The position of the field in the
219 * register is defined by its offset and width. The value recorded
220 * in this field is always 1 less than the value it represents.
221 *
222 * In addition, a variable divider can indicate that some subset
223 * of its bits represent a "fractional" part of the divider. Such
224 * bits comprise the low-order portion of the divider field, and can
225 * be viewed as representing the portion of the divider that lies to
226 * the right of the decimal point. Most variable dividers have zero
227 * fractional bits. Variable dividers with non-zero fraction width
228 * still record a value 1 less than the value they represent; the
229 * added 1 does *not* affect the low-order bit in this case, it
230 * affects the bits above the fractional part only. (Often in this
231 * code a divider field value is distinguished from the value it
232 * represents by referring to the latter as a "divisor".)
233 *
234 * In order to avoid dealing with fractions, divider arithmetic is
235 * performed using "scaled" values. A scaled value is one that's
236 * been left-shifted by the fractional width of a divider. Dividing
237 * a scaled value by a scaled divisor produces the desired quotient
238 * without loss of precision and without any other special handling
239 * for fractions.
240 *
241 * The recorded value of a variable divider can be modified. To
242 * modify either divider (or both), a clock must be enabled (i.e.,
243 * using its gate). In addition, a trigger register (described
244 * below) must be used to commit the change, and polled to verify
245 * the change is complete.
246 */
247struct bcm_clk_div {
248 union {
249 struct { /* variable divider */
250 u32 offset; /* divider register offset */
251 u32 shift; /* field shift */
252 u32 width; /* field width */
253 u32 frac_width; /* field fraction width */
254
255 u64 scaled_div; /* scaled divider value */
e813d49d 256 } s;
1f27f152 257 u32 fixed; /* non-zero fixed divider value */
e813d49d 258 } u;
1f27f152
AE
259 u32 flags; /* BCM_CLK_DIV_FLAGS_* below */
260};
261
262/*
263 * Divider flags:
264 * EXISTS means this divider exists
265 * FIXED means it is a fixed-rate divider
266 */
267#define BCM_CLK_DIV_FLAGS_EXISTS ((u32)1 << 0) /* Divider is valid */
268#define BCM_CLK_DIV_FLAGS_FIXED ((u32)1 << 1) /* Fixed-value */
269
270/* Divider initialization macros */
271
272/* A fixed (non-zero) divider */
273#define FIXED_DIVIDER(_value) \
274 { \
e813d49d 275 .u.fixed = (_value), \
1f27f152
AE
276 .flags = FLAG(DIV, EXISTS)|FLAG(DIV, FIXED), \
277 }
278
279/* A divider with an integral divisor */
280#define DIVIDER(_offset, _shift, _width) \
281 { \
e813d49d
AE
282 .u.s.offset = (_offset), \
283 .u.s.shift = (_shift), \
284 .u.s.width = (_width), \
285 .u.s.scaled_div = BAD_SCALED_DIV_VALUE, \
1f27f152
AE
286 .flags = FLAG(DIV, EXISTS), \
287 }
288
289/* A divider whose divisor has an integer and fractional part */
290#define FRAC_DIVIDER(_offset, _shift, _width, _frac_width) \
291 { \
e813d49d
AE
292 .u.s.offset = (_offset), \
293 .u.s.shift = (_shift), \
294 .u.s.width = (_width), \
295 .u.s.frac_width = (_frac_width), \
296 .u.s.scaled_div = BAD_SCALED_DIV_VALUE, \
1f27f152
AE
297 .flags = FLAG(DIV, EXISTS), \
298 }
299
300/*
301 * Clocks may have multiple "parent" clocks. If there is more than
302 * one, a selector must be specified to define which of the parent
303 * clocks is currently in use. The selected clock is indicated in a
304 * sub-field of a 32-bit selector register. The range of
305 * representable selector values typically exceeds the number of
306 * available parent clocks. Occasionally the reset value of a
307 * selector field is explicitly set to a (specific) value that does
308 * not correspond to a defined input clock.
309 *
310 * We register all known parent clocks with the common clock code
311 * using a packed array (i.e., no empty slots) of (parent) clock
312 * names, and refer to them later using indexes into that array.
313 * We maintain an array of selector values indexed by common clock
314 * index values in order to map between these common clock indexes
315 * and the selector values used by the hardware.
316 *
317 * Like dividers, a selector can be modified, but to do so a clock
318 * must be enabled, and a trigger must be used to commit the change.
319 */
320struct bcm_clk_sel {
321 u32 offset; /* selector register offset */
322 u32 shift; /* field shift */
323 u32 width; /* field width */
324
325 u32 parent_count; /* number of entries in parent_sel[] */
326 u32 *parent_sel; /* array of parent selector values */
327 u8 clk_index; /* current selected index in parent_sel[] */
328};
329
330/* Selector initialization macro */
331#define SELECTOR(_offset, _shift, _width) \
332 { \
333 .offset = (_offset), \
334 .shift = (_shift), \
335 .width = (_width), \
336 .clk_index = BAD_CLK_INDEX, \
337 }
338
339/*
340 * Making changes to a variable divider or a selector for a clock
341 * requires the use of a trigger. A trigger is defined by a single
342 * bit within a register. To signal a change, a 1 is written into
343 * that bit. To determine when the change has been completed, that
344 * trigger bit is polled; the read value will be 1 while the change
345 * is in progress, and 0 when it is complete.
346 *
347 * Occasionally a clock will have more than one trigger. In this
348 * case, the "pre-trigger" will be used when changing a clock's
349 * selector and/or its pre-divider.
350 */
351struct bcm_clk_trig {
352 u32 offset; /* trigger register offset */
353 u32 bit; /* trigger bit */
354 u32 flags; /* BCM_CLK_TRIG_FLAGS_* below */
355};
356
357/*
358 * Trigger flags:
359 * EXISTS means this trigger exists
360 */
361#define BCM_CLK_TRIG_FLAGS_EXISTS ((u32)1 << 0) /* Trigger is valid */
362
363/* Trigger initialization macro */
364#define TRIGGER(_offset, _bit) \
365 { \
366 .offset = (_offset), \
367 .bit = (_bit), \
368 .flags = FLAG(TRIG, EXISTS), \
369 }
370
371struct peri_clk_data {
372 struct bcm_clk_gate gate;
373 struct bcm_clk_trig pre_trig;
374 struct bcm_clk_div pre_div;
375 struct bcm_clk_trig trig;
376 struct bcm_clk_div div;
377 struct bcm_clk_sel sel;
378 const char *clocks[]; /* must be last; use CLOCKS() to declare */
379};
380#define CLOCKS(...) { __VA_ARGS__, NULL, }
381#define NO_CLOCKS { NULL, } /* Must use of no parent clocks */
382
383struct kona_clk {
384 struct clk_hw hw;
e7563252 385 struct clk_init_data init_data; /* includes name of this clock */
1f27f152
AE
386 struct ccu_data *ccu; /* ccu this clock is associated with */
387 enum bcm_clk_type type;
388 union {
389 void *data;
390 struct peri_clk_data *peri;
e813d49d 391 } u;
1f27f152
AE
392};
393#define to_kona_clk(_hw) \
394 container_of(_hw, struct kona_clk, hw)
395
396/* Exported globals */
397
398extern struct clk_ops kona_peri_clk_ops;
399
400/* Help functions */
401
b12151ca
AE
402#define KONA_CLK_SETUP(_ccu, _type, _name) \
403 kona_clk_setup((_ccu), #_name, bcm_clk_## _type, &_name ## _data)
404
405#define PERI_CLK_SETUP(_ccu, _id, _name) \
406 (_ccu)->clk_data.clks[_id] = KONA_CLK_SETUP((_ccu), peri, _name)
1f27f152
AE
407
408/* Externally visible functions */
409
410extern u64 do_div_round_closest(u64 dividend, unsigned long divisor);
411extern u64 scaled_div_max(struct bcm_clk_div *div);
412extern u64 scaled_div_build(struct bcm_clk_div *div, u32 div_value,
413 u32 billionths);
414
415extern struct clk *kona_clk_setup(struct ccu_data *ccu, const char *name,
416 enum bcm_clk_type type, void *data);
b12151ca
AE
417extern void __init kona_dt_ccu_setup(struct ccu_data *ccu,
418 struct device_node *node,
1f27f152
AE
419 int (*ccu_clks_setup)(struct ccu_data *));
420extern bool __init kona_ccu_init(struct ccu_data *ccu);
421
422#endif /* _CLK_KONA_H */