cycv_clkmgr.c revision 1.2 1 1.2 skrll /* $NetBSD: cycv_clkmgr.c,v 1.2 2019/01/17 12:49:53 skrll Exp $ */
2 1.1 aymeric
3 1.1 aymeric /* This file is in the public domain. */
4 1.1 aymeric
5 1.1 aymeric #include <sys/cdefs.h>
6 1.2 skrll __KERNEL_RCSID(0, "$NetBSD: cycv_clkmgr.c,v 1.2 2019/01/17 12:49:53 skrll Exp $");
7 1.1 aymeric
8 1.1 aymeric #include <sys/param.h>
9 1.1 aymeric #include <sys/bus.h>
10 1.1 aymeric #include <sys/device.h>
11 1.1 aymeric #include <sys/intr.h>
12 1.1 aymeric #include <sys/systm.h>
13 1.1 aymeric #include <sys/kernel.h>
14 1.1 aymeric #include <sys/atomic.h>
15 1.1 aymeric #include <sys/kmem.h>
16 1.1 aymeric
17 1.1 aymeric #include <dev/clk/clk_backend.h>
18 1.1 aymeric
19 1.1 aymeric #include <arm/altera/cycv_reg.h>
20 1.1 aymeric #include <arm/altera/cycv_var.h>
21 1.1 aymeric
22 1.1 aymeric #include <dev/fdt/fdtvar.h>
23 1.1 aymeric
24 1.1 aymeric #define CYCV_CLOCK_OSC1 25000000
25 1.1 aymeric
26 1.1 aymeric static int cycv_clkmgr_match(device_t, cfdata_t, void *);
27 1.1 aymeric static void cycv_clkmgr_attach(device_t, device_t, void *);
28 1.1 aymeric
29 1.1 aymeric static struct clk *cycv_clkmgr_clock_decode(device_t, int, const void *,
30 1.1 aymeric size_t);
31 1.1 aymeric
32 1.1 aymeric static const struct fdtbus_clock_controller_func cycv_clkmgr_fdtclock_funcs = {
33 1.1 aymeric .decode = cycv_clkmgr_clock_decode
34 1.1 aymeric };
35 1.1 aymeric
36 1.1 aymeric static struct clk *cycv_clkmgr_clock_get(void *, const char *);
37 1.1 aymeric static void cycv_clkmgr_clock_put(void *, struct clk *);
38 1.1 aymeric static u_int cycv_clkmgr_clock_get_rate(void *, struct clk *);
39 1.1 aymeric static int cycv_clkmgr_clock_set_rate(void *, struct clk *, u_int);
40 1.1 aymeric static int cycv_clkmgr_clock_enable(void *, struct clk *);
41 1.1 aymeric static int cycv_clkmgr_clock_disable(void *, struct clk *);
42 1.1 aymeric static int cycv_clkmgr_clock_set_parent(void *, struct clk *, struct clk *);
43 1.1 aymeric static struct clk *cycv_clkmgr_clock_get_parent(void *, struct clk *);
44 1.1 aymeric
45 1.1 aymeric static const struct clk_funcs cycv_clkmgr_clock_funcs = {
46 1.1 aymeric .get = cycv_clkmgr_clock_get,
47 1.1 aymeric .put = cycv_clkmgr_clock_put,
48 1.1 aymeric .get_rate = cycv_clkmgr_clock_get_rate,
49 1.1 aymeric .set_rate = cycv_clkmgr_clock_set_rate,
50 1.1 aymeric .enable = cycv_clkmgr_clock_enable,
51 1.1 aymeric .disable = cycv_clkmgr_clock_disable,
52 1.1 aymeric .get_parent = cycv_clkmgr_clock_get_parent,
53 1.1 aymeric .set_parent = cycv_clkmgr_clock_set_parent,
54 1.1 aymeric };
55 1.1 aymeric
56 1.1 aymeric struct cycv_clk {
57 1.1 aymeric struct clk base;
58 1.1 aymeric
59 1.1 aymeric int id;
60 1.1 aymeric u_int refcnt;
61 1.1 aymeric
62 1.1 aymeric struct cycv_clk *parent; /* cached and valid if not NULL */
63 1.1 aymeric /* parent_id is not zero and filled with dtb if only one parent clock */
64 1.1 aymeric int parent_id;
65 1.1 aymeric
66 1.1 aymeric int type;
67 1.1 aymeric #define CYCV_CLK_TYPE_PLL 0x0001
68 1.1 aymeric #define CYCV_CLK_TYPE_FIXED 0x0002
69 1.1 aymeric #define CYCV_CLK_TYPE_FIXED_DIV 0x0003
70 1.1 aymeric #define CYCV_CLK_TYPE_DIV 0x0004
71 1.1 aymeric
72 1.1 aymeric int flags;
73 1.1 aymeric #define CYCV_CLK_FLAG_HAVE_GATE 0x0001
74 1.1 aymeric #define CYCV_CLK_FLAG_IS_AVAIL 0x0002
75 1.1 aymeric
76 1.1 aymeric union {
77 1.1 aymeric bus_addr_t pll_addr;
78 1.1 aymeric uint32_t fixed_freq;
79 1.1 aymeric uint32_t fixed_div;
80 1.1 aymeric struct {
81 1.1 aymeric bus_addr_t addr;
82 1.1 aymeric uint32_t mask;
83 1.1 aymeric int shift;
84 1.1 aymeric } div;
85 1.1 aymeric } u;
86 1.1 aymeric
87 1.1 aymeric bus_addr_t gate_addr;
88 1.1 aymeric int gate_shift;
89 1.1 aymeric };
90 1.1 aymeric
91 1.1 aymeric struct cycv_clkmgr_softc {
92 1.1 aymeric device_t sc_dev;
93 1.1 aymeric struct clk_domain sc_clkdom;
94 1.1 aymeric
95 1.1 aymeric bus_space_tag_t sc_bst;
96 1.1 aymeric bus_space_handle_t sc_bsh;
97 1.1 aymeric
98 1.1 aymeric struct cycv_clk *sc_clocks;
99 1.1 aymeric u_int sc_nclocks;
100 1.1 aymeric };
101 1.1 aymeric
102 1.1 aymeric static void cycv_clkmgr_init(struct cycv_clkmgr_softc *, int);
103 1.1 aymeric static void cycv_clkmgr_clock_parse(struct cycv_clkmgr_softc *, int, u_int);
104 1.1 aymeric static u_int cycv_clkmgr_clocks_traverse(struct cycv_clkmgr_softc *, int,
105 1.1 aymeric void (*)(struct cycv_clkmgr_softc *, int, u_int), u_int);
106 1.1 aymeric static struct cycv_clk_mux_info *cycv_clkmgr_get_mux_info(const char *);
107 1.1 aymeric static void cycv_clkmgr_clock_print(struct cycv_clkmgr_softc *,
108 1.1 aymeric struct cycv_clk *);
109 1.1 aymeric
110 1.1 aymeric CFATTACH_DECL_NEW(cycvclkmgr, sizeof (struct cycv_clkmgr_softc),
111 1.1 aymeric cycv_clkmgr_match, cycv_clkmgr_attach, NULL, NULL);
112 1.1 aymeric
113 1.1 aymeric static int
114 1.1 aymeric cycv_clkmgr_match(device_t parent, cfdata_t cf, void *aux)
115 1.1 aymeric {
116 1.1 aymeric const char *compatible[] = { "altr,clk-mgr", NULL };
117 1.1 aymeric struct fdt_attach_args *faa = aux;
118 1.1 aymeric
119 1.1 aymeric return of_match_compatible(faa->faa_phandle, compatible);
120 1.1 aymeric }
121 1.1 aymeric
122 1.1 aymeric static void
123 1.1 aymeric cycv_clkmgr_attach(device_t parent, device_t self, void *aux)
124 1.1 aymeric {
125 1.1 aymeric struct cycv_clkmgr_softc *sc = device_private(self);
126 1.1 aymeric struct fdt_attach_args *faa = aux;
127 1.1 aymeric int phandle = faa->faa_phandle;
128 1.1 aymeric bus_addr_t addr;
129 1.1 aymeric bus_size_t size;
130 1.1 aymeric int error;
131 1.1 aymeric
132 1.1 aymeric if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
133 1.1 aymeric aprint_error(": couldn't get registers\n");
134 1.1 aymeric return;
135 1.1 aymeric }
136 1.1 aymeric
137 1.1 aymeric sc->sc_dev = self;
138 1.1 aymeric sc->sc_bst = faa->faa_bst;
139 1.1 aymeric error = bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh);
140 1.1 aymeric if (error) {
141 1.1 aymeric aprint_error(": couldn't map %#llx: %d",
142 1.1 aymeric (uint64_t) addr, error);
143 1.1 aymeric return;
144 1.1 aymeric }
145 1.1 aymeric
146 1.1 aymeric aprint_normal(": clock manager\n");
147 1.1 aymeric
148 1.1 aymeric sc->sc_clkdom.funcs = &cycv_clkmgr_clock_funcs;
149 1.1 aymeric sc->sc_clkdom.priv = sc;
150 1.1 aymeric
151 1.1 aymeric cycv_clkmgr_init(sc, phandle);
152 1.1 aymeric }
153 1.1 aymeric
154 1.1 aymeric static void
155 1.1 aymeric cycv_clkmgr_init(struct cycv_clkmgr_softc *sc, int clkmgr_handle)
156 1.1 aymeric {
157 1.1 aymeric int clocks_handle;
158 1.1 aymeric
159 1.1 aymeric clocks_handle = of_find_firstchild_byname(clkmgr_handle, "clocks");
160 1.1 aymeric if (clocks_handle == -1) {
161 1.1 aymeric aprint_error_dev(sc->sc_dev, "no clocks property\n");
162 1.1 aymeric return;
163 1.1 aymeric }
164 1.1 aymeric
165 1.1 aymeric sc->sc_nclocks = cycv_clkmgr_clocks_traverse(sc, clocks_handle, NULL,
166 1.1 aymeric 0);
167 1.1 aymeric
168 1.1 aymeric sc->sc_clocks = kmem_zalloc(sc->sc_nclocks * sizeof *sc->sc_clocks,
169 1.1 aymeric KM_NOSLEEP);
170 1.1 aymeric if (sc->sc_clocks == NULL) {
171 1.1 aymeric aprint_error_dev(sc->sc_dev, "no memory\n");
172 1.1 aymeric sc->sc_nclocks = 0;
173 1.1 aymeric return;
174 1.1 aymeric }
175 1.1 aymeric
176 1.1 aymeric cycv_clkmgr_clocks_traverse(sc, clocks_handle, cycv_clkmgr_clock_parse,
177 1.1 aymeric 0);
178 1.1 aymeric
179 1.1 aymeric #if 1
180 1.1 aymeric for (int i = 0; i < sc->sc_nclocks; i++)
181 1.1 aymeric cycv_clkmgr_clock_print(sc, &sc->sc_clocks[i]);
182 1.1 aymeric #else
183 1.1 aymeric (void) cycv_clkmgr_clock_print;
184 1.1 aymeric #endif
185 1.1 aymeric }
186 1.1 aymeric
187 1.1 aymeric #define CYCV_CLK_MAX_PARENTS 3
188 1.1 aymeric
189 1.1 aymeric static struct cycv_clk_mux_info {
190 1.1 aymeric const char *name;
191 1.1 aymeric const char *parents[CYCV_CLK_MAX_PARENTS];
192 1.1 aymeric int nparents;
193 1.1 aymeric bus_addr_t addr;
194 1.1 aymeric uint32_t mask;
195 1.1 aymeric } cycv_clk_mux_tree[] = {
196 1.1 aymeric { "periph_pll", { "osc1", "osc2", "f2s_periph_ref_clk" }, 3,
197 1.1 aymeric 0x80, 0x00c00000 },
198 1.1 aymeric { "sdram_pll", { "osc1", "osc2", "f2s_sdram_ref_clk" }, 3,
199 1.1 aymeric 0xc0, 0x00c00000 },
200 1.1 aymeric { "l4_mp_clk", { "mainclk", "per_base_clk" }, 2, 0x70, 0x00000001 },
201 1.1 aymeric { "l4_sp_clk", { "mainclk", "per_base_clk" }, 2, 0x70, 0x00000002 },
202 1.1 aymeric { "sdmmc_clk",
203 1.1 aymeric { "f2s_periph_ref_clk", "main_nand_sdmmc_clk", "per_nand_mmc_clk" },
204 1.1 aymeric 3, 0xac, 0x00000003 },
205 1.1 aymeric { "nand_x_clk",
206 1.1 aymeric { "f2s_periph_ref_clk", "main_nand_sdmmc_clk", "per_nand_mmc_clk" },
207 1.1 aymeric 3, 0xac, 0x0000000c },
208 1.1 aymeric { "qspi_clk", { "f2s_periph_ref_clk", "main_qspi_clk", "per_qsi_clk" },
209 1.1 aymeric 3, 0xac, 0x00000030 },
210 1.1 aymeric
211 1.1 aymeric /* Don't special case bypass */
212 1.1 aymeric { "dbg_base_clk", { "main_pll" }, 1, 0, 0 },
213 1.1 aymeric /* Bug in dtb */
214 1.1 aymeric { "nand_clk", { "nand_x_clk" }, 1, 0, 0 },
215 1.1 aymeric };
216 1.1 aymeric
217 1.1 aymeric static const char * const cycv_clkmgr_compat_fixed[] = { "fixed-clock", NULL };
218 1.1 aymeric static const char * const cycv_clkmgr_compat_pll[] = { "altr,socfpga-pll-clock",
219 1.1 aymeric NULL };
220 1.1 aymeric static const char * const cycv_clkmgr_compat_perip[] = {
221 1.1 aymeric "altr,socfpga-perip-clk",
222 1.1 aymeric "altr,socfpga-gate-clk",
223 1.1 aymeric NULL
224 1.1 aymeric };
225 1.1 aymeric
226 1.1 aymeric static void
227 1.1 aymeric cycv_clkmgr_clock_parse(struct cycv_clkmgr_softc *sc, int handle, u_int clkno)
228 1.1 aymeric {
229 1.1 aymeric struct cycv_clk *clk = &sc->sc_clocks[clkno];
230 1.1 aymeric int flags = 0;
231 1.1 aymeric const uint8_t *buf;
232 1.1 aymeric int len;
233 1.1 aymeric
234 1.1 aymeric clk->base.domain = &sc->sc_clkdom;
235 1.1 aymeric clk->base.name = fdtbus_get_string(handle, "name");
236 1.1 aymeric clk->base.flags = 0;
237 1.1 aymeric
238 1.1 aymeric clk->id = handle;
239 1.1 aymeric clk->parent = NULL;
240 1.1 aymeric clk->parent_id = 0;
241 1.1 aymeric clk->refcnt = 0;
242 1.1 aymeric
243 1.1 aymeric if (of_compatible(handle, cycv_clkmgr_compat_fixed) != -1) {
244 1.1 aymeric clk->type = CYCV_CLK_TYPE_FIXED;
245 1.1 aymeric if (of_getprop_uint32(handle, "clock-frequency",
246 1.1 aymeric &clk->u.fixed_freq) == 0) {
247 1.1 aymeric flags |= CYCV_CLK_FLAG_IS_AVAIL;
248 1.1 aymeric }
249 1.1 aymeric } else if (of_compatible(handle, cycv_clkmgr_compat_pll) != -1) {
250 1.1 aymeric if (fdtbus_get_reg(handle, 0, &clk->u.pll_addr, NULL) != 0)
251 1.1 aymeric goto err;
252 1.1 aymeric clk->type = CYCV_CLK_TYPE_PLL;
253 1.1 aymeric flags |= CYCV_CLK_FLAG_IS_AVAIL;
254 1.1 aymeric } else if (of_compatible(handle, cycv_clkmgr_compat_perip) != -1) {
255 1.1 aymeric if (of_getprop_uint32(handle, "fixed-divider",
256 1.1 aymeric &clk->u.fixed_div) == 0) {
257 1.1 aymeric clk->type = CYCV_CLK_TYPE_FIXED_DIV;
258 1.1 aymeric } else if (fdtbus_get_reg(handle, 0, &clk->u.div.addr, NULL) ==
259 1.1 aymeric 0) {
260 1.1 aymeric clk->type = CYCV_CLK_TYPE_DIV;
261 1.1 aymeric clk->u.div.shift = 0;
262 1.1 aymeric clk->u.div.mask = 0xff;
263 1.1 aymeric } else if ((buf = fdtbus_get_prop(handle, "div-reg", &len)) !=
264 1.1 aymeric NULL) {
265 1.1 aymeric if (len != 3 * 4)
266 1.1 aymeric goto err;
267 1.1 aymeric
268 1.1 aymeric clk->type = CYCV_CLK_TYPE_DIV;
269 1.1 aymeric clk->u.div.addr = of_decode_int(buf);
270 1.1 aymeric clk->u.div.shift = of_decode_int(buf + 4);
271 1.1 aymeric clk->u.div.mask = ((1 << of_decode_int(buf + 8)) - 1) <<
272 1.1 aymeric clk->u.div.shift;
273 1.1 aymeric } else {
274 1.1 aymeric /* Simply a gate and/or a mux */
275 1.1 aymeric clk->type = CYCV_CLK_TYPE_FIXED_DIV;
276 1.1 aymeric clk->u.fixed_div = 1;
277 1.1 aymeric }
278 1.1 aymeric flags |= CYCV_CLK_FLAG_IS_AVAIL;
279 1.1 aymeric } else
280 1.1 aymeric goto err;
281 1.1 aymeric
282 1.1 aymeric if ((buf = fdtbus_get_prop(handle, "clk-gate", &len)) != NULL) {
283 1.1 aymeric clk->gate_addr = of_decode_int(buf);
284 1.1 aymeric clk->gate_shift = of_decode_int(buf + 4);
285 1.1 aymeric flags |= CYCV_CLK_FLAG_HAVE_GATE;
286 1.1 aymeric }
287 1.1 aymeric
288 1.1 aymeric buf = fdtbus_get_prop(handle, "clocks", &len);
289 1.1 aymeric if (buf != NULL && len == sizeof (uint32_t)) {
290 1.1 aymeric clk->parent_id =
291 1.1 aymeric fdtbus_get_phandle_from_native(of_decode_int(buf));
292 1.1 aymeric }
293 1.1 aymeric
294 1.1 aymeric clk->flags = flags;
295 1.1 aymeric
296 1.1 aymeric fdtbus_register_clock_controller(sc->sc_dev, handle,
297 1.1 aymeric &cycv_clkmgr_fdtclock_funcs);
298 1.1 aymeric
299 1.1 aymeric return;
300 1.1 aymeric err:
301 1.1 aymeric aprint_debug_dev(sc->sc_dev, "(%s) error parsing phandle %d\n",
302 1.1 aymeric clk->base.name, handle);
303 1.1 aymeric }
304 1.1 aymeric
305 1.1 aymeric static u_int
306 1.1 aymeric cycv_clkmgr_clocks_traverse(struct cycv_clkmgr_softc *sc, int clocks_handle,
307 1.1 aymeric void func(struct cycv_clkmgr_softc *, int, u_int),
308 1.1 aymeric u_int nclocks)
309 1.1 aymeric {
310 1.1 aymeric int clk_handle;
311 1.1 aymeric
312 1.1 aymeric for (clk_handle = OF_child(clocks_handle); clk_handle != 0;
313 1.1 aymeric clk_handle = OF_peer(clk_handle)) {
314 1.1 aymeric if (func != NULL)
315 1.1 aymeric func(sc, clk_handle, nclocks);
316 1.1 aymeric nclocks++;
317 1.1 aymeric
318 1.1 aymeric nclocks = cycv_clkmgr_clocks_traverse(sc, clk_handle, func,
319 1.1 aymeric nclocks);
320 1.1 aymeric }
321 1.1 aymeric
322 1.1 aymeric return nclocks;
323 1.1 aymeric }
324 1.1 aymeric
325 1.1 aymeric static struct cycv_clk_mux_info *
326 1.2 skrll cycv_clkmgr_get_mux_info(const char *name)
327 1.2 skrll {
328 1.1 aymeric size_t i;
329 1.1 aymeric
330 1.1 aymeric for (i = 0; i < __arraycount(cycv_clk_mux_tree); i++) {
331 1.1 aymeric struct cycv_clk_mux_info *cand = &cycv_clk_mux_tree[i];
332 1.1 aymeric if (strncmp(name, cand->name, strlen(cand->name)) == 0)
333 1.1 aymeric return cand;
334 1.1 aymeric }
335 1.1 aymeric
336 1.1 aymeric return NULL;
337 1.1 aymeric }
338 1.1 aymeric
339 1.1 aymeric static struct cycv_clk *
340 1.1 aymeric cycv_clkmgr_clock_lookup_by_id(struct cycv_clkmgr_softc *sc, int id)
341 1.1 aymeric {
342 1.1 aymeric size_t i;
343 1.1 aymeric
344 1.1 aymeric for (i = 0; i < sc->sc_nclocks; i++) {
345 1.1 aymeric if (sc->sc_clocks[i].id == id)
346 1.1 aymeric return &sc->sc_clocks[i];
347 1.1 aymeric }
348 1.1 aymeric
349 1.1 aymeric return NULL;
350 1.1 aymeric }
351 1.1 aymeric
352 1.1 aymeric static struct cycv_clk *
353 1.1 aymeric cycv_clkmgr_clock_lookup_by_name(struct cycv_clkmgr_softc *sc, const char *name)
354 1.1 aymeric {
355 1.1 aymeric size_t i;
356 1.1 aymeric
357 1.1 aymeric for (i = 0; i < sc->sc_nclocks; i++) {
358 1.1 aymeric struct cycv_clk *cand = &sc->sc_clocks[i];
359 1.1 aymeric if (strncmp(cand->base.name, name, strlen(name)) == 0)
360 1.1 aymeric return cand;
361 1.1 aymeric }
362 1.1 aymeric
363 1.1 aymeric return NULL;
364 1.1 aymeric }
365 1.1 aymeric
366 1.1 aymeric static void
367 1.2 skrll cycv_clkmgr_clock_print(struct cycv_clkmgr_softc *sc, struct cycv_clk *clk)
368 1.2 skrll {
369 1.1 aymeric uint32_t numer;
370 1.1 aymeric uint32_t denom;
371 1.1 aymeric uint32_t tmp;
372 1.1 aymeric
373 1.1 aymeric aprint_debug("clock %s, id %d, frequency %uHz:\n", clk->base.name,
374 1.1 aymeric clk->id, cycv_clkmgr_clock_get_rate(sc, &clk->base));
375 1.1 aymeric if (clk->parent != NULL)
376 1.1 aymeric aprint_debug("parent: %s", clk->parent->base.name);
377 1.1 aymeric else
378 1.1 aymeric aprint_debug("parent_id: %d", clk->parent_id);
379 1.1 aymeric aprint_debug(", flags: %d\n", clk->flags);
380 1.1 aymeric switch (clk->type) {
381 1.1 aymeric case CYCV_CLK_TYPE_PLL:
382 1.1 aymeric tmp = bus_space_read_4(sc->sc_bst, sc->sc_bsh, clk->u.pll_addr);
383 1.1 aymeric numer = __SHIFTOUT(tmp, CYCV_CLKMGR_PLL_VCO_NUMER) + 1;
384 1.1 aymeric denom = __SHIFTOUT(tmp, CYCV_CLKMGR_PLL_VCO_DENOM) + 1;
385 1.1 aymeric aprint_debug(" PLL num = %u, den = %u\n", numer, denom);
386 1.1 aymeric break;
387 1.1 aymeric case CYCV_CLK_TYPE_FIXED:
388 1.1 aymeric aprint_debug(" Fixed frequency = %u\n", clk->u.fixed_freq);
389 1.1 aymeric break;
390 1.1 aymeric case CYCV_CLK_TYPE_FIXED_DIV:
391 1.1 aymeric aprint_debug(" Fixed divisor = %u\n", clk->u.fixed_div);
392 1.1 aymeric break;
393 1.1 aymeric case CYCV_CLK_TYPE_DIV:
394 1.1 aymeric tmp = bus_space_read_4(sc->sc_bst, sc->sc_bsh, clk->u.div.addr);
395 1.1 aymeric tmp = (tmp & clk->u.div.mask) >> clk->u.div.shift;
396 1.1 aymeric if (__SHIFTOUT_MASK(clk->u.div.mask) > 0xf)
397 1.1 aymeric tmp += 1;
398 1.1 aymeric else
399 1.1 aymeric tmp = (1 << tmp);
400 1.1 aymeric aprint_debug(" Divisor = %u\n", tmp);
401 1.1 aymeric break;
402 1.1 aymeric default:
403 1.1 aymeric aprint_debug(" Unknown!!!\n");
404 1.1 aymeric break;
405 1.1 aymeric }
406 1.1 aymeric
407 1.1 aymeric if (clk->flags & CYCV_CLK_FLAG_HAVE_GATE) {
408 1.1 aymeric tmp = bus_space_read_4(sc->sc_bst, sc->sc_bsh, clk->gate_addr);
409 1.1 aymeric tmp &= (1 << clk->gate_shift);
410 1.1 aymeric aprint_debug(" Gate %s\n", tmp? "on" : "off");
411 1.1 aymeric }
412 1.1 aymeric
413 1.1 aymeric
414 1.1 aymeric }
415 1.1 aymeric
416 1.1 aymeric static struct clk *
417 1.1 aymeric cycv_clkmgr_clock_decode(device_t dev, int cc_phandle, const void *data,
418 1.1 aymeric size_t len)
419 1.1 aymeric {
420 1.1 aymeric struct cycv_clkmgr_softc *sc = device_private(dev);
421 1.1 aymeric struct cycv_clk *clk;
422 1.1 aymeric
423 1.1 aymeric if (data != NULL || len != 0) {
424 1.1 aymeric aprint_debug_dev(dev, "can't decode clock entry\n");
425 1.1 aymeric return NULL;
426 1.1 aymeric }
427 1.1 aymeric
428 1.1 aymeric clk = cycv_clkmgr_clock_lookup_by_id(sc, cc_phandle);
429 1.1 aymeric
430 1.1 aymeric return clk == NULL? NULL : &clk->base;
431 1.1 aymeric }
432 1.1 aymeric
433 1.1 aymeric static struct clk *
434 1.1 aymeric cycv_clkmgr_clock_get(void *priv, const char *name)
435 1.1 aymeric {
436 1.1 aymeric aprint_debug("%s: called and not implemented\n", __func__);
437 1.1 aymeric (void) cycv_clkmgr_get_mux_info;
438 1.1 aymeric (void) cycv_clkmgr_clock_lookup_by_name;
439 1.1 aymeric
440 1.1 aymeric return NULL;
441 1.1 aymeric }
442 1.1 aymeric
443 1.1 aymeric static void
444 1.1 aymeric cycv_clkmgr_clock_put(void *priv, struct clk *clk)
445 1.1 aymeric {
446 1.1 aymeric aprint_debug("%s: called and not implemented\n", __func__);
447 1.1 aymeric }
448 1.1 aymeric
449 1.1 aymeric static u_int
450 1.1 aymeric cycv_clkmgr_clock_get_rate(void *priv, struct clk *base_clk)
451 1.1 aymeric {
452 1.1 aymeric struct cycv_clkmgr_softc *sc = priv;
453 1.1 aymeric struct cycv_clk *clk = (struct cycv_clk *) base_clk;
454 1.1 aymeric struct cycv_clk *parent;
455 1.1 aymeric uint32_t parent_rate = 0;
456 1.1 aymeric uint32_t divisor = 0;
457 1.1 aymeric uint32_t numer;
458 1.1 aymeric uint32_t tmp;
459 1.1 aymeric
460 1.1 aymeric if (clk->type == CYCV_CLK_TYPE_FIXED)
461 1.1 aymeric return clk->u.fixed_freq;
462 1.1 aymeric
463 1.1 aymeric parent = (struct cycv_clk *)
464 1.1 aymeric cycv_clkmgr_clock_get_parent(priv, base_clk);
465 1.1 aymeric if (parent == NULL) {
466 1.1 aymeric aprint_debug_dev(sc->sc_dev, "can't get parent of clock %s\n",
467 1.1 aymeric clk->base.name);
468 1.1 aymeric return 0;
469 1.1 aymeric }
470 1.1 aymeric parent_rate = cycv_clkmgr_clock_get_rate(priv, &parent->base);
471 1.1 aymeric
472 1.1 aymeric if (strncmp(clk->base.name, "mpuclk@", strlen("mpuclk@")) == 0)
473 1.1 aymeric parent_rate /= 2;
474 1.1 aymeric else if (strncmp(clk->base.name, "mainclk@", strlen("mainclk@")) == 0)
475 1.1 aymeric parent_rate /= 4;
476 1.1 aymeric else if (strncmp(clk->base.name, "dbgatclk@", strlen("dbgatclk@")) == 0)
477 1.1 aymeric parent_rate /= 4;
478 1.1 aymeric
479 1.1 aymeric switch (clk->type) {
480 1.1 aymeric case CYCV_CLK_TYPE_FIXED_DIV:
481 1.1 aymeric return parent_rate / clk->u.fixed_div;
482 1.1 aymeric
483 1.1 aymeric case CYCV_CLK_TYPE_DIV:
484 1.1 aymeric divisor = bus_space_read_4(sc->sc_bst, sc->sc_bsh,
485 1.1 aymeric clk->u.div.addr);
486 1.1 aymeric divisor = (divisor & clk->u.div.mask) >> clk->u.div.shift;
487 1.1 aymeric if (__SHIFTOUT_MASK(clk->u.div.mask) > 0xf)
488 1.1 aymeric divisor += 1;
489 1.1 aymeric else
490 1.1 aymeric divisor = (1 << divisor);
491 1.1 aymeric
492 1.1 aymeric return parent_rate / divisor;
493 1.1 aymeric
494 1.1 aymeric case CYCV_CLK_TYPE_PLL:
495 1.1 aymeric tmp = bus_space_read_4(sc->sc_bst, sc->sc_bsh, clk->u.pll_addr);
496 1.1 aymeric numer = __SHIFTOUT(tmp, CYCV_CLKMGR_PLL_VCO_NUMER) + 1;
497 1.1 aymeric divisor = __SHIFTOUT(tmp, CYCV_CLKMGR_PLL_VCO_DENOM) + 1;
498 1.1 aymeric
499 1.1 aymeric return (uint64_t) parent_rate * numer / divisor;
500 1.1 aymeric }
501 1.1 aymeric
502 1.1 aymeric aprint_debug_dev(sc->sc_dev, "unknown clock type %d\n", clk->type);
503 1.1 aymeric
504 1.1 aymeric return 0;
505 1.1 aymeric }
506 1.1 aymeric
507 1.1 aymeric static int
508 1.1 aymeric cycv_clkmgr_clock_set_rate(void *priv, struct clk *clk, u_int rate)
509 1.1 aymeric {
510 1.1 aymeric aprint_debug("%s: called and not implemented\n", __func__);
511 1.1 aymeric return EINVAL;
512 1.1 aymeric }
513 1.1 aymeric
514 1.1 aymeric static int
515 1.2 skrll cycv_clkmgr_clock_set(void *priv, struct clk *base_clk, int val)
516 1.2 skrll {
517 1.1 aymeric struct cycv_clkmgr_softc *sc = priv;
518 1.1 aymeric struct cycv_clk *clk = (struct cycv_clk *) base_clk;
519 1.1 aymeric
520 1.1 aymeric if (clk->flags & CYCV_CLK_FLAG_HAVE_GATE) {
521 1.1 aymeric uint32_t tmp = bus_space_read_4(sc->sc_bst, sc->sc_bsh,
522 1.1 aymeric clk->gate_addr);
523 1.1 aymeric tmp &= ~(1 << clk->gate_shift);
524 1.1 aymeric tmp |= val << clk->gate_shift;
525 1.1 aymeric bus_space_write_4(sc->sc_bst, sc->sc_bsh, clk->gate_addr, tmp);
526 1.1 aymeric } else
527 1.1 aymeric /* XXX should iterate to the root of the clock domain */
528 1.1 aymeric return 0;
529 1.1 aymeric
530 1.1 aymeric return 0;
531 1.1 aymeric }
532 1.1 aymeric
533 1.1 aymeric static int
534 1.1 aymeric cycv_clkmgr_clock_enable(void *priv, struct clk *clk)
535 1.1 aymeric {
536 1.1 aymeric return cycv_clkmgr_clock_set(priv, clk, 1);
537 1.1 aymeric }
538 1.1 aymeric
539 1.1 aymeric static int
540 1.1 aymeric cycv_clkmgr_clock_disable(void *priv, struct clk *clk)
541 1.1 aymeric {
542 1.1 aymeric return cycv_clkmgr_clock_set(priv, clk, 0);
543 1.1 aymeric }
544 1.1 aymeric
545 1.1 aymeric static int
546 1.1 aymeric cycv_clkmgr_clock_set_parent(void *priv, struct clk *clk,
547 1.1 aymeric struct clk *clk_parent)
548 1.1 aymeric {
549 1.1 aymeric /* lookup clk in muxinfo table */
550 1.1 aymeric /* if not found, parent is not settable */
551 1.1 aymeric /* check if clk_parent can be a parent (by name) */
552 1.1 aymeric /* beware of special case where there is only one parent in mux */
553 1.1 aymeric /* enact reparenting h/w wise, update clk->parent */
554 1.1 aymeric aprint_debug("%s: called and not implemented\n", __func__);
555 1.1 aymeric return EINVAL;
556 1.1 aymeric }
557 1.1 aymeric
558 1.1 aymeric static struct clk *
559 1.1 aymeric cycv_clkmgr_clock_get_parent(void *priv, struct clk *base_clk)
560 1.1 aymeric {
561 1.1 aymeric struct cycv_clkmgr_softc *sc = priv;
562 1.1 aymeric struct cycv_clk *clk = (struct cycv_clk *) base_clk;
563 1.1 aymeric struct cycv_clk_mux_info *mux;
564 1.1 aymeric struct cycv_clk *parent = clk->parent;
565 1.1 aymeric int parent_index;
566 1.1 aymeric uint32_t tmp;
567 1.1 aymeric
568 1.1 aymeric if (parent != NULL)
569 1.1 aymeric goto update;
570 1.1 aymeric
571 1.1 aymeric if (clk->parent_id != 0) {
572 1.1 aymeric parent = cycv_clkmgr_clock_lookup_by_id(sc, clk->parent_id);
573 1.1 aymeric goto update;
574 1.1 aymeric }
575 1.1 aymeric
576 1.1 aymeric mux = cycv_clkmgr_get_mux_info(clk->base.name);
577 1.1 aymeric
578 1.1 aymeric if (mux == NULL)
579 1.1 aymeric goto update;
580 1.1 aymeric
581 1.1 aymeric if (mux->nparents == 1)
582 1.1 aymeric parent_index = 0;
583 1.1 aymeric else {
584 1.1 aymeric tmp = bus_space_read_4(sc->sc_bst, sc->sc_bsh, mux->addr);
585 1.1 aymeric parent_index = __SHIFTOUT(tmp, mux->mask);
586 1.1 aymeric }
587 1.1 aymeric
588 1.1 aymeric if (parent_index >= mux->nparents) {
589 1.1 aymeric aprint_error_dev(sc->sc_dev,
590 1.1 aymeric "clock %s parent has non existent index %d\n",
591 1.1 aymeric clk->base.name, parent_index);
592 1.1 aymeric goto update;
593 1.1 aymeric }
594 1.1 aymeric
595 1.1 aymeric parent = cycv_clkmgr_clock_lookup_by_name(sc,
596 1.1 aymeric mux->parents[parent_index]);
597 1.1 aymeric
598 1.1 aymeric update:
599 1.1 aymeric clk->parent = parent;
600 1.1 aymeric return &parent->base;
601 1.1 aymeric }
602 1.1 aymeric
603 1.1 aymeric /*
604 1.1 aymeric * Functions called during early startup, possibly before autoconfiguration.
605 1.1 aymeric */
606 1.1 aymeric
607 1.1 aymeric uint32_t
608 1.2 skrll cycv_clkmgr_early_get_mpu_clk(void)
609 1.2 skrll {
610 1.1 aymeric bus_space_tag_t bst = &armv7_generic_bs_tag;
611 1.1 aymeric bus_space_handle_t bsh;
612 1.1 aymeric uint32_t tmp;
613 1.1 aymeric uint64_t vco;
614 1.1 aymeric
615 1.1 aymeric bus_space_map(bst, CYCV_CLKMGR_BASE, CYCV_CLKMGR_SIZE, 0, &bsh);
616 1.1 aymeric
617 1.1 aymeric tmp = bus_space_read_4(bst, bsh, CYCV_CLKMGR_MAIN_PLL_VCO);
618 1.1 aymeric vco = (uint64_t) CYCV_CLOCK_OSC1 *
619 1.1 aymeric (__SHIFTOUT(tmp, CYCV_CLKMGR_PLL_VCO_NUMER) + 1) /
620 1.1 aymeric (__SHIFTOUT(tmp, CYCV_CLKMGR_PLL_VCO_DENOM) + 1);
621 1.1 aymeric tmp = bus_space_read_4(bst, bsh, CYCV_CLKMGR_MAIN_PLL_MPUCLK);
622 1.1 aymeric
623 1.1 aymeric return vco / 2 / (__SHIFTOUT(tmp, CYCV_CLKMGR_MAIN_PLL_MPUCLK_CNT) + 1);
624 1.1 aymeric }
625 1.1 aymeric
626 1.1 aymeric uint32_t
627 1.2 skrll cycv_clkmgr_early_get_l4_sp_clk(void)
628 1.2 skrll {
629 1.1 aymeric bus_space_tag_t bst = &armv7_generic_bs_tag;
630 1.1 aymeric bus_space_handle_t bsh;
631 1.1 aymeric uint32_t tmp;
632 1.1 aymeric uint32_t res;
633 1.1 aymeric uint64_t vco;
634 1.1 aymeric
635 1.1 aymeric bus_space_map(bst, CYCV_CLKMGR_BASE, CYCV_CLKMGR_SIZE, 0, &bsh);
636 1.1 aymeric
637 1.1 aymeric tmp = bus_space_read_4(bst, bsh, CYCV_CLKMGR_MAIN_PLL_L4SRC);
638 1.1 aymeric if (__SHIFTOUT(tmp, CYCV_CLKMGR_MAIN_PLL_L4SRC_L4SP) == 0) {
639 1.1 aymeric /* L4 SP clock driven by main clock */
640 1.1 aymeric vco = (uint64_t) CYCV_CLOCK_OSC1 *
641 1.1 aymeric (__SHIFTOUT(tmp, CYCV_CLKMGR_PLL_VCO_NUMER) + 1) /
642 1.1 aymeric (__SHIFTOUT(tmp, CYCV_CLKMGR_PLL_VCO_DENOM) + 1);
643 1.1 aymeric tmp = bus_space_read_4(bst, bsh, CYCV_CLKMGR_MAIN_PLL_MAINCLK);
644 1.1 aymeric tmp = __SHIFTOUT(tmp, CYCV_CLKMGR_MAIN_PLL_MAINCLK_CNT);
645 1.1 aymeric
646 1.1 aymeric res = vco / 4 / (tmp + 1);
647 1.1 aymeric } else {
648 1.1 aymeric /* L4 SP clock driven by periph clock */
649 1.1 aymeric vco = (uint64_t) CYCV_CLOCK_OSC1 *
650 1.1 aymeric (__SHIFTOUT(tmp, CYCV_CLKMGR_PLL_VCO_NUMER) + 1) /
651 1.1 aymeric (__SHIFTOUT(tmp, CYCV_CLKMGR_PLL_VCO_DENOM) + 1);
652 1.1 aymeric tmp = bus_space_read_4(bst, bsh,
653 1.1 aymeric CYCV_CLKMGR_PERI_PLL_PERBASECLK);
654 1.1 aymeric tmp = __SHIFTOUT(tmp, CYCV_CLKMGR_PERI_PLL_PERBASECLK_CNT);
655 1.1 aymeric
656 1.1 aymeric res = vco / (tmp + 1);
657 1.1 aymeric }
658 1.1 aymeric
659 1.1 aymeric tmp = bus_space_read_4(bst, bsh, CYCV_CLKMGR_MAIN_PLL_MAINDIV);
660 1.1 aymeric tmp = __SHIFTOUT(tmp, CYCV_CLKMGR_MAIN_PLL_MAINDIV_L4SP);
661 1.1 aymeric
662 1.1 aymeric printf("%s: returning %u\n", __func__, (uint32_t)
663 1.1 aymeric (res / (1 << tmp)));
664 1.1 aymeric return res / (1 << tmp);
665 1.1 aymeric }
666