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