blob: 6baf6655b5c383598c4eae85f48de7c079f5c6c2 [file] [log] [blame]
Heiko Stübnera245fec2014-07-03 01:58:39 +02001/*
2 * Copyright (c) 2014 MundoReader S.L.
3 * Author: Heiko Stuebner <heiko@sntech.de>
4 *
5 * based on
6 *
7 * samsung/clk.h
8 * Copyright (c) 2013 Samsung Electronics Co., Ltd.
9 * Copyright (c) 2013 Linaro Ltd.
10 * Author: Thomas Abraham <thomas.ab@samsung.com>
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 */
22
23#ifndef CLK_ROCKCHIP_CLK_H
24#define CLK_ROCKCHIP_CLK_H
25
26#include <linux/io.h>
27#include <linux/clk.h>
28#include <linux/clk-provider.h>
29
30#define HIWORD_UPDATE(val, mask, shift) \
31 ((val) << (shift) | (mask) << ((shift) + 16))
32
33/* register positions shared by RK2928, RK3066 and RK3188 */
34#define RK2928_PLL_CON(x) (x * 0x4)
35#define RK2928_MODE_CON 0x40
36#define RK2928_CLKSEL_CON(x) (x * 0x4 + 0x44)
37#define RK2928_CLKGATE_CON(x) (x * 0x4 + 0xd0)
38#define RK2928_GLB_SRST_FST 0x100
39#define RK2928_GLB_SRST_SND 0x104
40#define RK2928_SOFTRST_CON(x) (x * 0x4 + 0x110)
41#define RK2928_MISC_CON 0x134
42
Heiko Stübnerb9e4ba52014-07-03 02:02:37 +020043#define RK3288_PLL_CON(x) RK2928_PLL_CON(x)
44#define RK3288_MODE_CON 0x50
45#define RK3288_CLKSEL_CON(x) (x * 0x4 + 0x60)
46#define RK3288_CLKGATE_CON(x) (x * 0x4 + 0x160)
47#define RK3288_GLB_SRST_FST 0x1b0
48#define RK3288_GLB_SRST_SND 0x1b4
49#define RK3288_SOFTRST_CON(x) (x * 0x4 + 0x1b8)
50#define RK3288_MISC_CON 0x1e8
51
Heiko Stübner90c59022014-07-03 01:59:10 +020052enum rockchip_pll_type {
53 pll_rk3066,
54};
55
56#define RK3066_PLL_RATE(_rate, _nr, _nf, _no) \
57{ \
58 .rate = _rate##U, \
59 .nr = _nr, \
60 .nf = _nf, \
61 .no = _no, \
62 .bwadj = (_nf >> 1), \
63}
64
Kever Yang49ed9ee2014-10-09 22:23:57 -070065#define RK3066_PLL_RATE_BWADJ(_rate, _nr, _nf, _no, _bw) \
66{ \
67 .rate = _rate##U, \
68 .nr = _nr, \
69 .nf = _nf, \
70 .no = _no, \
71 .bwadj = _bw, \
72}
73
Heiko Stübner90c59022014-07-03 01:59:10 +020074struct rockchip_pll_rate_table {
75 unsigned long rate;
76 unsigned int nr;
77 unsigned int nf;
78 unsigned int no;
79 unsigned int bwadj;
80};
81
82/**
83 * struct rockchip_pll_clock: information about pll clock
84 * @id: platform specific id of the clock.
85 * @name: name of this pll clock.
86 * @parent_name: name of the parent clock.
87 * @flags: optional flags for basic clock.
88 * @con_offset: offset of the register for configuring the PLL.
89 * @mode_offset: offset of the register for configuring the PLL-mode.
90 * @mode_shift: offset inside the mode-register for the mode of this pll.
91 * @lock_shift: offset inside the lock register for the lock status.
92 * @type: Type of PLL to be registered.
93 * @rate_table: Table of usable pll rates
94 */
95struct rockchip_pll_clock {
96 unsigned int id;
97 const char *name;
98 const char **parent_names;
99 u8 num_parents;
100 unsigned long flags;
101 int con_offset;
102 int mode_offset;
103 int mode_shift;
104 int lock_shift;
105 enum rockchip_pll_type type;
106 struct rockchip_pll_rate_table *rate_table;
107};
108
109#define PLL(_type, _id, _name, _pnames, _flags, _con, _mode, _mshift, \
110 _lshift, _rtable) \
111 { \
112 .id = _id, \
113 .type = _type, \
114 .name = _name, \
115 .parent_names = _pnames, \
116 .num_parents = ARRAY_SIZE(_pnames), \
117 .flags = CLK_GET_RATE_NOCACHE | _flags, \
118 .con_offset = _con, \
119 .mode_offset = _mode, \
120 .mode_shift = _mshift, \
121 .lock_shift = _lshift, \
122 .rate_table = _rtable, \
123 }
124
125struct clk *rockchip_clk_register_pll(enum rockchip_pll_type pll_type,
126 const char *name, const char **parent_names, u8 num_parents,
127 void __iomem *base, int con_offset, int grf_lock_offset,
128 int lock_shift, int reg_mode, int mode_shift,
129 struct rockchip_pll_rate_table *rate_table,
130 spinlock_t *lock);
131
Heiko Stuebnerf6fba5f2014-09-04 22:10:43 +0200132struct rockchip_cpuclk_clksel {
133 int reg;
134 u32 val;
135};
136
137#define ROCKCHIP_CPUCLK_NUM_DIVIDERS 2
138struct rockchip_cpuclk_rate_table {
139 unsigned long prate;
140 struct rockchip_cpuclk_clksel divs[ROCKCHIP_CPUCLK_NUM_DIVIDERS];
141};
142
143/**
144 * struct rockchip_cpuclk_reg_data: describes register offsets and masks of the cpuclock
145 * @core_reg: register offset of the core settings register
146 * @div_core_shift: core divider offset used to divide the pll value
147 * @div_core_mask: core divider mask
148 * @mux_core_shift: offset of the core multiplexer
149 */
150struct rockchip_cpuclk_reg_data {
151 int core_reg;
152 u8 div_core_shift;
153 u32 div_core_mask;
154 int mux_core_reg;
155 u8 mux_core_shift;
156};
157
158struct clk *rockchip_clk_register_cpuclk(const char *name,
159 const char **parent_names, u8 num_parents,
160 const struct rockchip_cpuclk_reg_data *reg_data,
161 const struct rockchip_cpuclk_rate_table *rates,
162 int nrates, void __iomem *reg_base, spinlock_t *lock);
163
Heiko Stübnera245fec2014-07-03 01:58:39 +0200164#define PNAME(x) static const char *x[] __initconst
165
166enum rockchip_clk_branch_type {
167 branch_composite,
168 branch_mux,
169 branch_divider,
170 branch_fraction_divider,
171 branch_gate,
172};
173
174struct rockchip_clk_branch {
175 unsigned int id;
176 enum rockchip_clk_branch_type branch_type;
177 const char *name;
178 const char **parent_names;
179 u8 num_parents;
180 unsigned long flags;
181 int muxdiv_offset;
182 u8 mux_shift;
183 u8 mux_width;
184 u8 mux_flags;
185 u8 div_shift;
186 u8 div_width;
187 u8 div_flags;
188 struct clk_div_table *div_table;
189 int gate_offset;
190 u8 gate_shift;
191 u8 gate_flags;
192};
193
194#define COMPOSITE(_id, cname, pnames, f, mo, ms, mw, mf, ds, dw,\
195 df, go, gs, gf) \
196 { \
197 .id = _id, \
198 .branch_type = branch_composite, \
199 .name = cname, \
200 .parent_names = pnames, \
201 .num_parents = ARRAY_SIZE(pnames), \
202 .flags = f, \
203 .muxdiv_offset = mo, \
204 .mux_shift = ms, \
205 .mux_width = mw, \
206 .mux_flags = mf, \
207 .div_shift = ds, \
208 .div_width = dw, \
209 .div_flags = df, \
210 .gate_offset = go, \
211 .gate_shift = gs, \
212 .gate_flags = gf, \
213 }
214
215#define COMPOSITE_NOMUX(_id, cname, pname, f, mo, ds, dw, df, \
216 go, gs, gf) \
217 { \
218 .id = _id, \
219 .branch_type = branch_composite, \
220 .name = cname, \
221 .parent_names = (const char *[]){ pname }, \
222 .num_parents = 1, \
223 .flags = f, \
224 .muxdiv_offset = mo, \
225 .div_shift = ds, \
226 .div_width = dw, \
227 .div_flags = df, \
228 .gate_offset = go, \
229 .gate_shift = gs, \
230 .gate_flags = gf, \
231 }
232
233#define COMPOSITE_NOMUX_DIVTBL(_id, cname, pname, f, mo, ds, dw,\
234 df, dt, go, gs, gf) \
235 { \
236 .id = _id, \
237 .branch_type = branch_composite, \
238 .name = cname, \
239 .parent_names = (const char *[]){ pname }, \
240 .num_parents = 1, \
241 .flags = f, \
242 .muxdiv_offset = mo, \
243 .div_shift = ds, \
244 .div_width = dw, \
245 .div_flags = df, \
246 .div_table = dt, \
247 .gate_offset = go, \
248 .gate_shift = gs, \
249 .gate_flags = gf, \
250 }
251
252#define COMPOSITE_NODIV(_id, cname, pnames, f, mo, ms, mw, mf, \
253 go, gs, gf) \
254 { \
255 .id = _id, \
256 .branch_type = branch_composite, \
257 .name = cname, \
258 .parent_names = pnames, \
259 .num_parents = ARRAY_SIZE(pnames), \
260 .flags = f, \
261 .muxdiv_offset = mo, \
262 .mux_shift = ms, \
263 .mux_width = mw, \
264 .mux_flags = mf, \
265 .gate_offset = go, \
266 .gate_shift = gs, \
267 .gate_flags = gf, \
268 }
269
270#define COMPOSITE_NOGATE(_id, cname, pnames, f, mo, ms, mw, mf, \
271 ds, dw, df) \
272 { \
273 .id = _id, \
274 .branch_type = branch_composite, \
275 .name = cname, \
276 .parent_names = pnames, \
277 .num_parents = ARRAY_SIZE(pnames), \
278 .flags = f, \
279 .muxdiv_offset = mo, \
280 .mux_shift = ms, \
281 .mux_width = mw, \
282 .mux_flags = mf, \
283 .div_shift = ds, \
284 .div_width = dw, \
285 .div_flags = df, \
286 .gate_offset = -1, \
287 }
288
289#define COMPOSITE_FRAC(_id, cname, pname, f, mo, df, go, gs, gf)\
290 { \
291 .id = _id, \
292 .branch_type = branch_fraction_divider, \
293 .name = cname, \
294 .parent_names = (const char *[]){ pname }, \
295 .num_parents = 1, \
296 .flags = f, \
297 .muxdiv_offset = mo, \
298 .div_shift = 16, \
299 .div_width = 16, \
300 .div_flags = df, \
301 .gate_offset = go, \
302 .gate_shift = gs, \
303 .gate_flags = gf, \
304 }
305
306#define MUX(_id, cname, pnames, f, o, s, w, mf) \
307 { \
308 .id = _id, \
309 .branch_type = branch_mux, \
310 .name = cname, \
311 .parent_names = pnames, \
312 .num_parents = ARRAY_SIZE(pnames), \
313 .flags = f, \
314 .muxdiv_offset = o, \
315 .mux_shift = s, \
316 .mux_width = w, \
317 .mux_flags = mf, \
318 .gate_offset = -1, \
319 }
320
321#define DIV(_id, cname, pname, f, o, s, w, df) \
322 { \
323 .id = _id, \
324 .branch_type = branch_divider, \
325 .name = cname, \
326 .parent_names = (const char *[]){ pname }, \
327 .num_parents = 1, \
328 .flags = f, \
329 .muxdiv_offset = o, \
330 .div_shift = s, \
331 .div_width = w, \
332 .div_flags = df, \
333 .gate_offset = -1, \
334 }
335
336#define DIVTBL(_id, cname, pname, f, o, s, w, df, dt) \
337 { \
338 .id = _id, \
339 .branch_type = branch_divider, \
340 .name = cname, \
341 .parent_names = (const char *[]){ pname }, \
342 .num_parents = 1, \
343 .flags = f, \
344 .muxdiv_offset = o, \
345 .div_shift = s, \
346 .div_width = w, \
347 .div_flags = df, \
348 .div_table = dt, \
349 }
350
351#define GATE(_id, cname, pname, f, o, b, gf) \
352 { \
353 .id = _id, \
354 .branch_type = branch_gate, \
355 .name = cname, \
356 .parent_names = (const char *[]){ pname }, \
357 .num_parents = 1, \
358 .flags = f, \
359 .gate_offset = o, \
360 .gate_shift = b, \
361 .gate_flags = gf, \
362 }
363
364
365void rockchip_clk_init(struct device_node *np, void __iomem *base,
366 unsigned long nr_clks);
Heiko Stübner90c59022014-07-03 01:59:10 +0200367struct regmap *rockchip_clk_get_grf(void);
Heiko Stübnera245fec2014-07-03 01:58:39 +0200368void rockchip_clk_add_lookup(struct clk *clk, unsigned int id);
369void rockchip_clk_register_branches(struct rockchip_clk_branch *clk_list,
370 unsigned int nr_clk);
Heiko Stübner90c59022014-07-03 01:59:10 +0200371void rockchip_clk_register_plls(struct rockchip_pll_clock *pll_list,
372 unsigned int nr_pll, int grf_lock_offset);
Heiko Stuebnerf6fba5f2014-09-04 22:10:43 +0200373void rockchip_clk_register_armclk(unsigned int lookup_id, const char *name,
374 const char **parent_names, u8 num_parents,
375 const struct rockchip_cpuclk_reg_data *reg_data,
376 const struct rockchip_cpuclk_rate_table *rates,
377 int nrates);
Heiko Stübnerfe94f972014-08-14 23:00:26 +0200378void rockchip_clk_protect_critical(const char *clocks[], int nclocks);
Heiko Stübner6f1294b2014-08-19 17:45:38 -0700379void rockchip_register_restart_notifier(unsigned int reg);
Heiko Stübnera245fec2014-07-03 01:58:39 +0200380
Heiko Stübner85fa0c72014-07-03 01:59:39 +0200381#define ROCKCHIP_SOFTRST_HIWORD_MASK BIT(0)
382
383#ifdef CONFIG_RESET_CONTROLLER
384void rockchip_register_softrst(struct device_node *np,
385 unsigned int num_regs,
386 void __iomem *base, u8 flags);
387#else
388static inline void rockchip_register_softrst(struct device_node *np,
389 unsigned int num_regs,
390 void __iomem *base, u8 flags)
391{
392}
393#endif
394
Heiko Stübnera245fec2014-07-03 01:58:39 +0200395#endif