rk_cru.h revision 1.1.4.2 1 /* $NetBSD: rk_cru.h,v 1.1.4.2 2020/04/13 08:03:37 martin Exp $ */
2
3 /*-
4 * Copyright (c) 2018 Jared McNeill <jmcneill (at) invisible.ca>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #ifndef _ARM_RK_CRU_H
30 #define _ARM_RK_CRU_H
31
32 #include <dev/clk/clk_backend.h>
33 #include <dev/fdt/syscon.h>
34
35 struct rk_cru_softc;
36 struct rk_cru_clk;
37
38 /*
39 * Clocks
40 */
41
42 enum rk_cru_clktype {
43 RK_CRU_UNKNOWN,
44 RK_CRU_PLL,
45 RK_CRU_ARM,
46 RK_CRU_COMPOSITE,
47 RK_CRU_GATE,
48 RK_CRU_MUX,
49 };
50
51 /* PLL clocks */
52
53 struct rk_cru_pll_rate {
54 u_int rate;
55 u_int refdiv;
56 u_int fbdiv;
57 u_int postdiv1;
58 u_int postdiv2;
59 u_int dsmpd;
60 u_int fracdiv;
61 };
62
63 #define RK_PLL_RATE(_rate, _refdiv, _fbdiv, _postdiv1, _postdiv2, _dsmpd, _fracdiv) \
64 { \
65 .rate = (_rate), \
66 .refdiv = (_refdiv), \
67 .fbdiv = (_fbdiv), \
68 .postdiv1 = (_postdiv1), \
69 .postdiv2 = (_postdiv2), \
70 .dsmpd = (_dsmpd), \
71 .fracdiv = (_fracdiv), \
72 }
73
74 struct rk_cru_pll {
75 bus_size_t con_base;
76 bus_size_t mode_reg;
77 uint32_t mode_mask;
78 uint32_t lock_mask;
79 const struct rk_cru_pll_rate *rates;
80 u_int nrates;
81 const char **parents;
82 u_int nparents;
83 };
84
85 u_int rk_cru_pll_get_rate(struct rk_cru_softc *, struct rk_cru_clk *);
86 int rk_cru_pll_set_rate(struct rk_cru_softc *, struct rk_cru_clk *, u_int);
87 const char *rk_cru_pll_get_parent(struct rk_cru_softc *, struct rk_cru_clk *);
88
89 #define RK_PLL(_id, _name, _parents, _con_base, _mode_reg, _mode_mask, _lock_mask, _rates) \
90 { \
91 .id = (_id), \
92 .type = RK_CRU_PLL, \
93 .base.name = (_name), \
94 .base.flags = 0, \
95 .u.pll.parents = (_parents), \
96 .u.pll.nparents = __arraycount(_parents), \
97 .u.pll.con_base = (_con_base), \
98 .u.pll.mode_reg = (_mode_reg), \
99 .u.pll.mode_mask = (_mode_mask), \
100 .u.pll.lock_mask = (_lock_mask), \
101 .u.pll.rates = (_rates), \
102 .u.pll.nrates = __arraycount(_rates), \
103 .get_rate = rk_cru_pll_get_rate, \
104 .set_rate = rk_cru_pll_set_rate, \
105 .get_parent = rk_cru_pll_get_parent, \
106 }
107
108 /* ARM clocks */
109
110 struct rk_cru_arm_rate {
111 u_int rate;
112 u_int div;
113 };
114
115 #define RK_ARM_RATE(_rate, _div) \
116 { \
117 .rate = (_rate), \
118 .div = (_div), \
119 }
120
121 struct rk_cru_cpu_rate {
122 u_int rate;
123 u_int reg1, reg1_mask, reg1_val;
124 u_int reg2, reg2_mask, reg2_val;
125 };
126
127 #define RK_CPU_RATE(_rate, _reg1, _reg1_mask, _reg1_val, _reg2, _reg2_mask, _reg2_val) \
128 { \
129 .rate = (_rate), \
130 .reg1 = (_reg1), .reg1_mask = (_reg1_mask), .reg1_val = (_reg1_val), \
131 .reg2 = (_reg2), .reg2_mask = (_reg2_mask), .reg2_val = (_reg2_val), \
132 }
133
134 struct rk_cru_arm {
135 bus_size_t reg;
136 uint32_t mux_mask;
137 u_int mux_main;
138 u_int mux_alt;
139 uint32_t div_mask;
140 const char **parents;
141 u_int nparents;
142 const struct rk_cru_arm_rate *rates;
143 const struct rk_cru_cpu_rate *cpurates;
144 u_int nrates;
145 };
146
147 u_int rk_cru_arm_get_rate(struct rk_cru_softc *, struct rk_cru_clk *);
148 int rk_cru_arm_set_rate(struct rk_cru_softc *, struct rk_cru_clk *, u_int);
149 int rk_cru_arm_set_rate(struct rk_cru_softc *, struct rk_cru_clk *, u_int);
150 const char *rk_cru_arm_get_parent(struct rk_cru_softc *, struct rk_cru_clk *);
151 int rk_cru_arm_set_parent(struct rk_cru_softc *, struct rk_cru_clk *, const char *);
152
153 #define RK_ARM(_id, _name, _parents, _reg, _mux_mask, _mux_main, _mux_alt, _div_mask, _rates) \
154 { \
155 .id = (_id), \
156 .type = RK_CRU_ARM, \
157 .base.name = (_name), \
158 .base.flags = 0, \
159 .u.arm.parents = (_parents), \
160 .u.arm.nparents = __arraycount(_parents), \
161 .u.arm.reg = (_reg), \
162 .u.arm.mux_mask = (_mux_mask), \
163 .u.arm.mux_main = (_mux_main), \
164 .u.arm.mux_alt = (_mux_alt), \
165 .u.arm.div_mask = (_div_mask), \
166 .u.arm.rates = (_rates), \
167 .u.arm.nrates = __arraycount(_rates), \
168 .get_rate = rk_cru_arm_get_rate, \
169 .set_rate = rk_cru_arm_set_rate, \
170 .get_parent = rk_cru_arm_get_parent, \
171 .set_parent = rk_cru_arm_set_parent, \
172 }
173
174 #define RK_CPU(_id, _name, _parents, _reg, _mux_mask, _mux_main, _mux_alt, _div_mask, _cpurates) \
175 { \
176 .id = (_id), \
177 .type = RK_CRU_ARM, \
178 .base.name = (_name), \
179 .base.flags = 0, \
180 .u.arm.parents = (_parents), \
181 .u.arm.nparents = __arraycount(_parents), \
182 .u.arm.reg = (_reg), \
183 .u.arm.mux_mask = (_mux_mask), \
184 .u.arm.mux_main = (_mux_main), \
185 .u.arm.mux_alt = (_mux_alt), \
186 .u.arm.div_mask = (_div_mask), \
187 .u.arm.cpurates = (_cpurates), \
188 .u.arm.nrates = __arraycount(_cpurates), \
189 .get_rate = rk_cru_arm_get_rate, \
190 .set_rate = rk_cru_arm_set_rate, \
191 .get_parent = rk_cru_arm_get_parent, \
192 .set_parent = rk_cru_arm_set_parent, \
193 }
194
195 /* Composite clocks */
196
197 struct rk_cru_composite {
198 bus_size_t muxdiv_reg;
199 uint32_t mux_mask;
200 uint32_t div_mask;
201 bus_size_t gate_reg;
202 uint32_t gate_mask;
203 bus_size_t frac_reg;
204 const char **parents;
205 u_int nparents;
206 u_int flags;
207 #define RK_COMPOSITE_ROUND_DOWN 0x01
208 #define RK_COMPOSITE_SET_RATE_PARENT 0x02
209 #define RK_COMPOSITE_FRACDIV 0x04
210 };
211
212 int rk_cru_composite_enable(struct rk_cru_softc *, struct rk_cru_clk *, int);
213 u_int rk_cru_composite_get_rate(struct rk_cru_softc *, struct rk_cru_clk *);
214 int rk_cru_composite_set_rate(struct rk_cru_softc *, struct rk_cru_clk *, u_int);
215 const char *rk_cru_composite_get_parent(struct rk_cru_softc *, struct rk_cru_clk *);
216 int rk_cru_composite_set_parent(struct rk_cru_softc *, struct rk_cru_clk *, const char *);
217
218 #define _RK_COMPOSITE_INIT(_id, _name, _parents, _muxdiv_reg, _mux_mask, _div_mask, _gate_reg, _gate_mask, _frac_reg, _flags) \
219 { \
220 .id = (_id), \
221 .type = RK_CRU_COMPOSITE, \
222 .base.name = (_name), \
223 .base.flags = 0, \
224 .u.composite.parents = (_parents), \
225 .u.composite.nparents = __arraycount(_parents), \
226 .u.composite.muxdiv_reg = (_muxdiv_reg), \
227 .u.composite.mux_mask = (_mux_mask), \
228 .u.composite.div_mask = (_div_mask), \
229 .u.composite.gate_reg = (_gate_reg), \
230 .u.composite.gate_mask = (_gate_mask), \
231 .u.composite.frac_reg = (_frac_reg), \
232 .u.composite.flags = (_flags), \
233 .enable = rk_cru_composite_enable, \
234 .get_rate = rk_cru_composite_get_rate, \
235 .set_rate = rk_cru_composite_set_rate, \
236 .get_parent = rk_cru_composite_get_parent, \
237 .set_parent = rk_cru_composite_set_parent, \
238 }
239
240 #define RK_COMPOSITE(_id, _name, _parents, _muxdiv_reg, _mux_mask, _div_mask, _gate_reg, _gate_mask, _flags) \
241 _RK_COMPOSITE_INIT(_id, _name, _parents, _muxdiv_reg, _mux_mask, _div_mask, _gate_reg, _gate_mask, 0, _flags)
242
243 #define RK_COMPOSITE_NOMUX(_id, _name, _parent, _div_reg, _div_mask, _gate_reg, _gate_mask, _flags) \
244 _RK_COMPOSITE_INIT(_id, _name, (const char *[]){ _parent }, _div_reg, 0, _div_mask, _gate_reg, _gate_mask, 0, _flags)
245
246 #define RK_COMPOSITE_NOGATE(_id, _name, _parents, _muxdiv_reg, _mux_mask, _div_mask, _flags) \
247 _RK_COMPOSITE_INIT(_id, _name, _parents, _muxdiv_reg, _mux_mask, _div_mask, 0, 0, 0, _flags)
248
249 #define RK_COMPOSITE_FRAC(_id, _name, _parent, _frac_reg, _flags) \
250 _RK_COMPOSITE_INIT(_id, _name, (const char *[]){ _parent }, 0, 0, 0, 0, 0, _frac_reg, (_flags) | RK_COMPOSITE_FRACDIV)
251
252 #define RK_DIV(_id, _name, _parent, _div_reg, _div_mask, _flags) \
253 _RK_COMPOSITE_INIT(_id, _name, (const char *[]){ _parent }, _div_reg, 0, _div_mask, 0, 0, 0, _flags)
254
255 /* Gate clocks */
256
257 struct rk_cru_gate {
258 bus_size_t reg;
259 uint32_t mask;
260 const char *parent;
261 };
262
263 int rk_cru_gate_enable(struct rk_cru_softc *,
264 struct rk_cru_clk *, int);
265 const char *rk_cru_gate_get_parent(struct rk_cru_softc *,
266 struct rk_cru_clk *);
267
268 #define RK_GATE(_id, _name, _pname, _reg, _bit) \
269 { \
270 .id = (_id), \
271 .type = RK_CRU_GATE, \
272 .base.name = (_name), \
273 .base.flags = CLK_SET_RATE_PARENT, \
274 .u.gate.parent = (_pname), \
275 .u.gate.reg = (_reg), \
276 .u.gate.mask = __BIT(_bit), \
277 .enable = rk_cru_gate_enable, \
278 .get_parent = rk_cru_gate_get_parent, \
279 }
280
281 #define RK_SECURE_GATE(_id, _name, _pname) \
282 { \
283 .id = (_id), \
284 .type = RK_CRU_GATE, \
285 .base.name = (_name), \
286 .u.gate.parent = (_pname), \
287 .get_parent = rk_cru_gate_get_parent, \
288 }
289
290 /* Mux clocks */
291
292 struct rk_cru_mux {
293 bus_size_t reg;
294 uint32_t mask;
295 const char **parents;
296 u_int nparents;
297 u_int flags;
298 #define RK_MUX_GRF 0x01
299 };
300
301 const char *rk_cru_mux_get_parent(struct rk_cru_softc *, struct rk_cru_clk *);
302 int rk_cru_mux_set_parent(struct rk_cru_softc *, struct rk_cru_clk *, const char *);
303
304 #define RK_MUX_FLAGS(_id, _name, _parents, _reg, _mask, _flags) \
305 { \
306 .id = (_id), \
307 .type = RK_CRU_MUX, \
308 .base.name = (_name), \
309 .base.flags = CLK_SET_RATE_PARENT, \
310 .u.mux.parents = (_parents), \
311 .u.mux.nparents = __arraycount(_parents), \
312 .u.mux.reg = (_reg), \
313 .u.mux.mask = (_mask), \
314 .u.mux.flags = (_flags), \
315 .set_parent = rk_cru_mux_set_parent, \
316 .get_parent = rk_cru_mux_get_parent, \
317 }
318 #define RK_MUX(_id, _name, _parents, _reg, _mask) \
319 RK_MUX_FLAGS(_id, _name, _parents, _reg, _mask, 0)
320 #define RK_MUXGRF(_id, _name, _parents, _reg, _mask) \
321 RK_MUX_FLAGS(_id, _name, _parents, _reg, _mask, RK_MUX_GRF)
322
323 /*
324 * Rockchip clock definition
325 */
326
327 struct rk_cru_clk {
328 struct clk base;
329 u_int id;
330 enum rk_cru_clktype type;
331 union {
332 struct rk_cru_pll pll;
333 struct rk_cru_arm arm;
334 struct rk_cru_composite composite;
335 struct rk_cru_gate gate;
336 struct rk_cru_mux mux;
337 } u;
338
339 int (*enable)(struct rk_cru_softc *,
340 struct rk_cru_clk *, int);
341 u_int (*get_rate)(struct rk_cru_softc *,
342 struct rk_cru_clk *);
343 int (*set_rate)(struct rk_cru_softc *,
344 struct rk_cru_clk *, u_int);
345 u_int (*round_rate)(struct rk_cru_softc *,
346 struct rk_cru_clk *, u_int);
347 const char * (*get_parent)(struct rk_cru_softc *,
348 struct rk_cru_clk *);
349 int (*set_parent)(struct rk_cru_softc *,
350 struct rk_cru_clk *,
351 const char *);
352 };
353
354 /*
355 * Driver state
356 */
357
358 struct rk_cru_softc {
359 device_t sc_dev;
360 int sc_phandle;
361 bus_space_tag_t sc_bst;
362 bus_space_handle_t sc_bsh;
363 struct syscon *sc_grf;
364
365 struct clk_domain sc_clkdom;
366
367 struct rk_cru_clk *sc_clks;
368 u_int sc_nclks;
369
370 bus_size_t sc_softrst_base;
371 };
372
373 int rk_cru_attach(struct rk_cru_softc *);
374 struct rk_cru_clk *rk_cru_clock_find(struct rk_cru_softc *,
375 const char *);
376 void rk_cru_print(struct rk_cru_softc *);
377
378 #define CRU_READ(sc, reg) \
379 bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
380 #define CRU_WRITE(sc, reg, val) \
381 bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
382
383 #define HAS_GRF(sc) ((sc)->sc_grf != NULL)
384
385 #endif /* _ARM_RK_CRU_H */
386