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