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