sunxi_ccu.h revision 1.9 1 1.9 jmcneill /* $NetBSD: sunxi_ccu.h,v 1.9 2017/08/13 19:18:08 jmcneill Exp $ */
2 1.1 jmcneill
3 1.1 jmcneill /*-
4 1.1 jmcneill * Copyright (c) 2017 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_SUNXI_CCU_H
30 1.1 jmcneill #define _ARM_SUNXI_CCU_H
31 1.1 jmcneill
32 1.1 jmcneill #include <dev/clk/clk_backend.h>
33 1.1 jmcneill
34 1.1 jmcneill struct sunxi_ccu_softc;
35 1.1 jmcneill struct sunxi_ccu_clk;
36 1.1 jmcneill struct sunxi_ccu_reset;
37 1.1 jmcneill
38 1.1 jmcneill /*
39 1.1 jmcneill * Resets
40 1.1 jmcneill */
41 1.1 jmcneill
42 1.1 jmcneill struct sunxi_ccu_reset {
43 1.1 jmcneill bus_size_t reg;
44 1.1 jmcneill uint32_t mask;
45 1.1 jmcneill };
46 1.1 jmcneill
47 1.1 jmcneill #define SUNXI_CCU_RESET(_id, _reg, _bit) \
48 1.1 jmcneill [_id] = { \
49 1.1 jmcneill .reg = (_reg), \
50 1.1 jmcneill .mask = __BIT(_bit), \
51 1.1 jmcneill }
52 1.1 jmcneill
53 1.1 jmcneill /*
54 1.1 jmcneill * Clocks
55 1.1 jmcneill */
56 1.1 jmcneill
57 1.1 jmcneill enum sunxi_ccu_clktype {
58 1.1 jmcneill SUNXI_CCU_UNKNOWN,
59 1.1 jmcneill SUNXI_CCU_GATE,
60 1.1 jmcneill SUNXI_CCU_NM,
61 1.2 jmcneill SUNXI_CCU_NKMP,
62 1.2 jmcneill SUNXI_CCU_PREDIV,
63 1.5 jmcneill SUNXI_CCU_DIV,
64 1.7 jmcneill SUNXI_CCU_PHASE,
65 1.1 jmcneill };
66 1.1 jmcneill
67 1.1 jmcneill struct sunxi_ccu_gate {
68 1.1 jmcneill bus_size_t reg;
69 1.1 jmcneill uint32_t mask;
70 1.1 jmcneill const char *parent;
71 1.1 jmcneill };
72 1.1 jmcneill
73 1.1 jmcneill int sunxi_ccu_gate_enable(struct sunxi_ccu_softc *,
74 1.1 jmcneill struct sunxi_ccu_clk *, int);
75 1.1 jmcneill const char *sunxi_ccu_gate_get_parent(struct sunxi_ccu_softc *,
76 1.1 jmcneill struct sunxi_ccu_clk *);
77 1.1 jmcneill
78 1.1 jmcneill #define SUNXI_CCU_GATE(_id, _name, _pname, _reg, _bit) \
79 1.1 jmcneill [_id] = { \
80 1.1 jmcneill .type = SUNXI_CCU_GATE, \
81 1.1 jmcneill .base.name = (_name), \
82 1.8 jmcneill .base.flags = CLK_SET_RATE_PARENT, \
83 1.1 jmcneill .u.gate.parent = (_pname), \
84 1.1 jmcneill .u.gate.reg = (_reg), \
85 1.1 jmcneill .u.gate.mask = __BIT(_bit), \
86 1.1 jmcneill .enable = sunxi_ccu_gate_enable, \
87 1.1 jmcneill .get_parent = sunxi_ccu_gate_get_parent, \
88 1.1 jmcneill }
89 1.1 jmcneill
90 1.8 jmcneill struct sunxi_ccu_nkmp_tbl {
91 1.8 jmcneill u_int rate;
92 1.8 jmcneill uint32_t n;
93 1.8 jmcneill uint32_t k;
94 1.8 jmcneill uint32_t m;
95 1.8 jmcneill uint32_t p;
96 1.8 jmcneill };
97 1.8 jmcneill
98 1.2 jmcneill struct sunxi_ccu_nkmp {
99 1.2 jmcneill bus_size_t reg;
100 1.2 jmcneill const char *parent;
101 1.2 jmcneill uint32_t n;
102 1.2 jmcneill uint32_t k;
103 1.2 jmcneill uint32_t m;
104 1.2 jmcneill uint32_t p;
105 1.2 jmcneill uint32_t lock;
106 1.2 jmcneill uint32_t enable;
107 1.2 jmcneill uint32_t flags;
108 1.8 jmcneill const struct sunxi_ccu_nkmp_tbl *table;
109 1.3 jmcneill #define SUNXI_CCU_NKMP_DIVIDE_BY_TWO __BIT(0)
110 1.6 jmcneill #define SUNXI_CCU_NKMP_FACTOR_N_EXACT __BIT(1)
111 1.9 jmcneill #define SUNXI_CCU_NKMP_SCALE_CLOCK __BIT(2)
112 1.9 jmcneill #define SUNXI_CCU_NKMP_FACTOR_P_POW2 __BIT(3)
113 1.2 jmcneill };
114 1.2 jmcneill
115 1.2 jmcneill int sunxi_ccu_nkmp_enable(struct sunxi_ccu_softc *,
116 1.2 jmcneill struct sunxi_ccu_clk *, int);
117 1.2 jmcneill u_int sunxi_ccu_nkmp_get_rate(struct sunxi_ccu_softc *,
118 1.2 jmcneill struct sunxi_ccu_clk *);
119 1.2 jmcneill int sunxi_ccu_nkmp_set_rate(struct sunxi_ccu_softc *,
120 1.2 jmcneill struct sunxi_ccu_clk *, u_int);
121 1.2 jmcneill const char *sunxi_ccu_nkmp_get_parent(struct sunxi_ccu_softc *,
122 1.2 jmcneill struct sunxi_ccu_clk *);
123 1.2 jmcneill
124 1.8 jmcneill #define SUNXI_CCU_NKMP_TABLE(_id, _name, _parent, _reg, _n, _k, _m, \
125 1.8 jmcneill _p, _enable, _lock, _tbl, _flags) \
126 1.2 jmcneill [_id] = { \
127 1.2 jmcneill .type = SUNXI_CCU_NKMP, \
128 1.2 jmcneill .base.name = (_name), \
129 1.2 jmcneill .u.nkmp.reg = (_reg), \
130 1.2 jmcneill .u.nkmp.parent = (_parent), \
131 1.2 jmcneill .u.nkmp.n = (_n), \
132 1.2 jmcneill .u.nkmp.k = (_k), \
133 1.2 jmcneill .u.nkmp.m = (_m), \
134 1.2 jmcneill .u.nkmp.p = (_p), \
135 1.2 jmcneill .u.nkmp.enable = (_enable), \
136 1.2 jmcneill .u.nkmp.flags = (_flags), \
137 1.8 jmcneill .u.nkmp.lock = (_lock), \
138 1.8 jmcneill .u.nkmp.table = (_tbl), \
139 1.2 jmcneill .enable = sunxi_ccu_nkmp_enable, \
140 1.2 jmcneill .get_rate = sunxi_ccu_nkmp_get_rate, \
141 1.2 jmcneill .set_rate = sunxi_ccu_nkmp_set_rate, \
142 1.2 jmcneill .get_parent = sunxi_ccu_nkmp_get_parent, \
143 1.2 jmcneill }
144 1.2 jmcneill
145 1.8 jmcneill #define SUNXI_CCU_NKMP(_id, _name, _parent, _reg, _n, _k, _m, \
146 1.8 jmcneill _p, _enable, _flags) \
147 1.8 jmcneill SUNXI_CCU_NKMP_TABLE(_id, _name, _parent, _reg, _n, _k, _m, \
148 1.8 jmcneill _p, _enable, 0, NULL, _flags)
149 1.8 jmcneill
150 1.8 jmcneill
151 1.1 jmcneill struct sunxi_ccu_nm {
152 1.1 jmcneill bus_size_t reg;
153 1.1 jmcneill const char **parents;
154 1.1 jmcneill u_int nparents;
155 1.1 jmcneill uint32_t n;
156 1.1 jmcneill uint32_t m;
157 1.1 jmcneill uint32_t sel;
158 1.2 jmcneill uint32_t enable;
159 1.1 jmcneill uint32_t flags;
160 1.1 jmcneill #define SUNXI_CCU_NM_POWER_OF_TWO __BIT(0)
161 1.2 jmcneill #define SUNXI_CCU_NM_ROUND_DOWN __BIT(1)
162 1.1 jmcneill };
163 1.1 jmcneill
164 1.2 jmcneill int sunxi_ccu_nm_enable(struct sunxi_ccu_softc *,
165 1.2 jmcneill struct sunxi_ccu_clk *, int);
166 1.1 jmcneill u_int sunxi_ccu_nm_get_rate(struct sunxi_ccu_softc *,
167 1.1 jmcneill struct sunxi_ccu_clk *);
168 1.1 jmcneill int sunxi_ccu_nm_set_rate(struct sunxi_ccu_softc *,
169 1.1 jmcneill struct sunxi_ccu_clk *, u_int);
170 1.1 jmcneill int sunxi_ccu_nm_set_parent(struct sunxi_ccu_softc *,
171 1.1 jmcneill struct sunxi_ccu_clk *,
172 1.1 jmcneill const char *);
173 1.1 jmcneill const char *sunxi_ccu_nm_get_parent(struct sunxi_ccu_softc *,
174 1.1 jmcneill struct sunxi_ccu_clk *);
175 1.1 jmcneill
176 1.1 jmcneill #define SUNXI_CCU_NM(_id, _name, _parents, _reg, _n, _m, _sel, \
177 1.2 jmcneill _enable, _flags) \
178 1.1 jmcneill [_id] = { \
179 1.1 jmcneill .type = SUNXI_CCU_NM, \
180 1.1 jmcneill .base.name = (_name), \
181 1.1 jmcneill .u.nm.reg = (_reg), \
182 1.1 jmcneill .u.nm.parents = (_parents), \
183 1.1 jmcneill .u.nm.nparents = __arraycount(_parents), \
184 1.1 jmcneill .u.nm.n = (_n), \
185 1.1 jmcneill .u.nm.m = (_m), \
186 1.1 jmcneill .u.nm.sel = (_sel), \
187 1.2 jmcneill .u.nm.enable = (_enable), \
188 1.1 jmcneill .u.nm.flags = (_flags), \
189 1.2 jmcneill .enable = sunxi_ccu_nm_enable, \
190 1.2 jmcneill .get_rate = sunxi_ccu_nm_get_rate, \
191 1.2 jmcneill .set_rate = sunxi_ccu_nm_set_rate, \
192 1.1 jmcneill .set_parent = sunxi_ccu_nm_set_parent, \
193 1.1 jmcneill .get_parent = sunxi_ccu_nm_get_parent, \
194 1.1 jmcneill }
195 1.1 jmcneill
196 1.5 jmcneill struct sunxi_ccu_div {
197 1.5 jmcneill bus_size_t reg;
198 1.5 jmcneill const char **parents;
199 1.5 jmcneill u_int nparents;
200 1.5 jmcneill uint32_t div;
201 1.5 jmcneill uint32_t sel;
202 1.5 jmcneill uint32_t flags;
203 1.5 jmcneill #define SUNXI_CCU_DIV_POWER_OF_TWO __BIT(0)
204 1.5 jmcneill #define SUNXI_CCU_DIV_ZERO_IS_ONE __BIT(1)
205 1.5 jmcneill };
206 1.5 jmcneill
207 1.5 jmcneill u_int sunxi_ccu_div_get_rate(struct sunxi_ccu_softc *,
208 1.5 jmcneill struct sunxi_ccu_clk *);
209 1.5 jmcneill int sunxi_ccu_div_set_rate(struct sunxi_ccu_softc *,
210 1.5 jmcneill struct sunxi_ccu_clk *, u_int);
211 1.5 jmcneill int sunxi_ccu_div_set_parent(struct sunxi_ccu_softc *,
212 1.5 jmcneill struct sunxi_ccu_clk *,
213 1.5 jmcneill const char *);
214 1.5 jmcneill const char *sunxi_ccu_div_get_parent(struct sunxi_ccu_softc *,
215 1.5 jmcneill struct sunxi_ccu_clk *);
216 1.5 jmcneill
217 1.5 jmcneill #define SUNXI_CCU_DIV(_id, _name, _parents, _reg, _div, \
218 1.5 jmcneill _sel, _flags) \
219 1.5 jmcneill [_id] = { \
220 1.5 jmcneill .type = SUNXI_CCU_DIV, \
221 1.5 jmcneill .base.name = (_name), \
222 1.5 jmcneill .u.div.reg = (_reg), \
223 1.5 jmcneill .u.div.parents = (_parents), \
224 1.5 jmcneill .u.div.nparents = __arraycount(_parents), \
225 1.5 jmcneill .u.div.div = (_div), \
226 1.5 jmcneill .u.div.sel = (_sel), \
227 1.5 jmcneill .u.div.flags = (_flags), \
228 1.5 jmcneill .get_rate = sunxi_ccu_div_get_rate, \
229 1.5 jmcneill .set_rate = sunxi_ccu_div_set_rate, \
230 1.5 jmcneill .set_parent = sunxi_ccu_div_set_parent, \
231 1.5 jmcneill .get_parent = sunxi_ccu_div_get_parent, \
232 1.5 jmcneill }
233 1.5 jmcneill
234 1.2 jmcneill struct sunxi_ccu_prediv {
235 1.2 jmcneill bus_size_t reg;
236 1.2 jmcneill const char **parents;
237 1.2 jmcneill u_int nparents;
238 1.2 jmcneill uint32_t prediv;
239 1.2 jmcneill uint32_t prediv_sel;
240 1.2 jmcneill uint32_t div;
241 1.2 jmcneill uint32_t sel;
242 1.2 jmcneill uint32_t flags;
243 1.2 jmcneill #define SUNXI_CCU_PREDIV_POWER_OF_TWO __BIT(0)
244 1.4 jmcneill #define SUNXI_CCU_PREDIV_DIVIDE_BY_TWO __BIT(1)
245 1.2 jmcneill };
246 1.2 jmcneill
247 1.2 jmcneill u_int sunxi_ccu_prediv_get_rate(struct sunxi_ccu_softc *,
248 1.2 jmcneill struct sunxi_ccu_clk *);
249 1.2 jmcneill int sunxi_ccu_prediv_set_rate(struct sunxi_ccu_softc *,
250 1.2 jmcneill struct sunxi_ccu_clk *, u_int);
251 1.2 jmcneill int sunxi_ccu_prediv_set_parent(struct sunxi_ccu_softc *,
252 1.2 jmcneill struct sunxi_ccu_clk *,
253 1.2 jmcneill const char *);
254 1.2 jmcneill const char *sunxi_ccu_prediv_get_parent(struct sunxi_ccu_softc *,
255 1.2 jmcneill struct sunxi_ccu_clk *);
256 1.2 jmcneill
257 1.2 jmcneill #define SUNXI_CCU_PREDIV(_id, _name, _parents, _reg, _prediv, \
258 1.2 jmcneill _prediv_sel, _div, _sel, _flags) \
259 1.2 jmcneill [_id] = { \
260 1.2 jmcneill .type = SUNXI_CCU_PREDIV, \
261 1.2 jmcneill .base.name = (_name), \
262 1.2 jmcneill .u.prediv.reg = (_reg), \
263 1.2 jmcneill .u.prediv.parents = (_parents), \
264 1.2 jmcneill .u.prediv.nparents = __arraycount(_parents), \
265 1.2 jmcneill .u.prediv.prediv = (_prediv), \
266 1.2 jmcneill .u.prediv.prediv_sel = (_prediv_sel), \
267 1.2 jmcneill .u.prediv.div = (_div), \
268 1.2 jmcneill .u.prediv.sel = (_sel), \
269 1.2 jmcneill .u.prediv.flags = (_flags), \
270 1.2 jmcneill .get_rate = sunxi_ccu_prediv_get_rate, \
271 1.2 jmcneill .set_rate = sunxi_ccu_prediv_set_rate, \
272 1.2 jmcneill .set_parent = sunxi_ccu_prediv_set_parent, \
273 1.2 jmcneill .get_parent = sunxi_ccu_prediv_get_parent, \
274 1.2 jmcneill }
275 1.2 jmcneill
276 1.7 jmcneill struct sunxi_ccu_phase {
277 1.7 jmcneill bus_size_t reg;
278 1.7 jmcneill const char *parent;
279 1.7 jmcneill uint32_t mask;
280 1.7 jmcneill };
281 1.7 jmcneill
282 1.7 jmcneill u_int sunxi_ccu_phase_get_rate(struct sunxi_ccu_softc *,
283 1.7 jmcneill struct sunxi_ccu_clk *);
284 1.7 jmcneill int sunxi_ccu_phase_set_rate(struct sunxi_ccu_softc *,
285 1.7 jmcneill struct sunxi_ccu_clk *, u_int);
286 1.7 jmcneill const char *sunxi_ccu_phase_get_parent(struct sunxi_ccu_softc *,
287 1.7 jmcneill struct sunxi_ccu_clk *);
288 1.7 jmcneill
289 1.7 jmcneill #define SUNXI_CCU_PHASE(_id, _name, _parent, _reg, _mask) \
290 1.7 jmcneill [_id] = { \
291 1.7 jmcneill .type = SUNXI_CCU_PHASE, \
292 1.7 jmcneill .base.name = (_name), \
293 1.7 jmcneill .u.phase.reg = (_reg), \
294 1.7 jmcneill .u.phase.parent = (_parent), \
295 1.7 jmcneill .u.phase.mask = (_mask), \
296 1.7 jmcneill .get_rate = sunxi_ccu_phase_get_rate, \
297 1.7 jmcneill .set_rate = sunxi_ccu_phase_set_rate, \
298 1.7 jmcneill .get_parent = sunxi_ccu_phase_get_parent, \
299 1.7 jmcneill }
300 1.7 jmcneill
301 1.1 jmcneill struct sunxi_ccu_clk {
302 1.1 jmcneill struct clk base;
303 1.1 jmcneill enum sunxi_ccu_clktype type;
304 1.1 jmcneill union {
305 1.1 jmcneill struct sunxi_ccu_gate gate;
306 1.1 jmcneill struct sunxi_ccu_nm nm;
307 1.2 jmcneill struct sunxi_ccu_nkmp nkmp;
308 1.2 jmcneill struct sunxi_ccu_prediv prediv;
309 1.5 jmcneill struct sunxi_ccu_div div;
310 1.7 jmcneill struct sunxi_ccu_phase phase;
311 1.1 jmcneill } u;
312 1.1 jmcneill
313 1.1 jmcneill int (*enable)(struct sunxi_ccu_softc *,
314 1.1 jmcneill struct sunxi_ccu_clk *, int);
315 1.1 jmcneill u_int (*get_rate)(struct sunxi_ccu_softc *,
316 1.1 jmcneill struct sunxi_ccu_clk *);
317 1.1 jmcneill int (*set_rate)(struct sunxi_ccu_softc *,
318 1.1 jmcneill struct sunxi_ccu_clk *, u_int);
319 1.1 jmcneill const char * (*get_parent)(struct sunxi_ccu_softc *,
320 1.1 jmcneill struct sunxi_ccu_clk *);
321 1.1 jmcneill int (*set_parent)(struct sunxi_ccu_softc *,
322 1.1 jmcneill struct sunxi_ccu_clk *,
323 1.1 jmcneill const char *);
324 1.1 jmcneill };
325 1.1 jmcneill
326 1.1 jmcneill struct sunxi_ccu_softc {
327 1.1 jmcneill device_t sc_dev;
328 1.1 jmcneill int sc_phandle;
329 1.1 jmcneill bus_space_tag_t sc_bst;
330 1.1 jmcneill bus_space_handle_t sc_bsh;
331 1.1 jmcneill
332 1.1 jmcneill struct clk_domain sc_clkdom;
333 1.1 jmcneill
334 1.1 jmcneill struct sunxi_ccu_reset *sc_resets;
335 1.1 jmcneill u_int sc_nresets;
336 1.1 jmcneill
337 1.1 jmcneill struct sunxi_ccu_clk *sc_clks;
338 1.1 jmcneill u_int sc_nclks;
339 1.1 jmcneill };
340 1.1 jmcneill
341 1.1 jmcneill int sunxi_ccu_attach(struct sunxi_ccu_softc *);
342 1.1 jmcneill struct sunxi_ccu_clk *sunxi_ccu_clock_find(struct sunxi_ccu_softc *,
343 1.1 jmcneill const char *);
344 1.1 jmcneill void sunxi_ccu_print(struct sunxi_ccu_softc *);
345 1.1 jmcneill
346 1.1 jmcneill #define CCU_READ(sc, reg) \
347 1.1 jmcneill bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
348 1.1 jmcneill #define CCU_WRITE(sc, reg, val) \
349 1.1 jmcneill bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
350 1.1 jmcneill
351 1.1 jmcneill #endif /* _ARM_SUNXI_CCU_H */
352