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