exynos5422_clock.c revision 1.4.2.4 1 /* $NetBSD: exynos5422_clock.c,v 1.4.2.4 2017/08/28 17:51:32 skrll Exp $ */
2
3 /*-
4 * Copyright (c) 2015 Jared D. McNeill <jmcneill (at) invisible.ca>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include "locators.h"
30
31 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: exynos5422_clock.c,v 1.4.2.4 2017/08/28 17:51:32 skrll Exp $");
33
34 #include <sys/param.h>
35 #include <sys/bus.h>
36 #include <sys/device.h>
37 #include <sys/intr.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/atomic.h>
41
42 #include <dev/clk/clk_backend.h>
43
44 #include <arm/samsung/exynos_reg.h>
45 #include <arm/samsung/exynos_var.h>
46 #include <arm/samsung/exynos_clock.h>
47
48 #include <dev/fdt/fdtvar.h>
49
50 static struct clk *exynos5422_clock_decode(device_t, const void *, size_t);
51
52 static const struct fdtbus_clock_controller_func exynos5422_car_fdtclock_funcs = {
53 .decode = exynos5422_clock_decode
54 };
55
56 /* DT clock ID to clock name mappings */
57 static struct exynos5422_clock_id {
58 u_int id;
59 const char *name;
60 } exynos5422_clock_ids[] = {
61 { 1, "fin_pll" },
62 { 2, "fout_apll" },
63 { 3, "fout_cpll" },
64 { 4, "fout_dpll" },
65 { 5, "fout_epll" },
66 { 6, "fout_rpll" },
67 { 7, "fout_ipll" },
68 { 8, "fout_spll" },
69 { 9, "fout_vpll" },
70 { 10, "fout_mpll" },
71 { 11, "fout_bpll" },
72 { 12, "fout_kpll" },
73 { 128, "sclk_uart0" },
74 { 129, "sclk_uart1" },
75 { 130, "sclk_uart2" },
76 { 131, "sclk_uart3" },
77 { 132, "sclk_mmc0" },
78 { 133, "sclk_mmc1" },
79 { 134, "sclk_mmc2" },
80 { 135, "sclk_spi0" },
81 { 136, "sclk_spi1" },
82 { 137, "sclk_spi2" },
83 { 138, "sclk_i2s1" },
84 { 139, "sclk_i2s2" },
85 { 140, "sclk_pcm1" },
86 { 141, "sclk_pcm2" },
87 { 142, "sclk_spdif" },
88 { 143, "sclk_hdmi" },
89 { 144, "sclk_pixel" },
90 { 145, "sclk_dp1" },
91 { 146, "sclk_mipi1" },
92 { 147, "sclk_fimd1" },
93 { 148, "sclk_maudio0" },
94 { 149, "sclk_maupcm0" },
95 { 150, "sclk_usbd300" },
96 { 151, "sclk_usbd301" },
97 { 152, "sclk_usbphy300" },
98 { 153, "sclk_usbphy301" },
99 { 154, "sclk_unipro" },
100 { 155, "sclk_pwm" },
101 { 156, "sclk_gscl_wa" },
102 { 157, "sclk_gscl_wb" },
103 { 158, "sclk_hdmiphy" },
104 { 159, "mau_epll" },
105 { 160, "sclk_hsic_12m" },
106 { 161, "sclk_mphy_ixtal24" },
107 { 257, "uart0" },
108 { 258, "uart1" },
109 { 259, "uart2" },
110 { 260, "uart3" },
111 { 261, "i2c0" },
112 { 262, "i2c1" },
113 { 263, "i2c2" },
114 { 264, "i2c3" },
115 { 265, "usi0" },
116 { 266, "usi1" },
117 { 267, "usi2" },
118 { 268, "usi3" },
119 { 269, "i2c_hdmi" },
120 { 270, "tsadc" },
121 { 271, "spi0" },
122 { 272, "spi1" },
123 { 273, "spi2" },
124 { 274, "keyif" },
125 { 275, "i2s1" },
126 { 276, "i2s2" },
127 { 277, "pcm1" },
128 { 278, "pcm2" },
129 { 279, "pwm" },
130 { 280, "spdif" },
131 { 281, "usi4" },
132 { 282, "usi5" },
133 { 283, "usi6" },
134 { 300, "aclk66_psgen" },
135 { 301, "chipid" },
136 { 302, "sysreg" },
137 { 303, "tzpc0" },
138 { 304, "tzpc1" },
139 { 305, "tzpc2" },
140 { 306, "tzpc3" },
141 { 307, "tzpc4" },
142 { 308, "tzpc5" },
143 { 309, "tzpc6" },
144 { 310, "tzpc7" },
145 { 311, "tzpc8" },
146 { 312, "tzpc9" },
147 { 313, "hdmi_cec" },
148 { 314, "seckey" },
149 { 315, "mct" },
150 { 316, "wdt" },
151 { 317, "rtc" },
152 { 318, "tmu" },
153 { 319, "tmu_gpu" },
154 { 330, "pclk66_gpio" },
155 { 350, "aclk200_fsys2" },
156 { 351, "mout_mmc0" },
157 { 352, "mout_mmc1" },
158 { 353, "mout_mmc2" },
159 { 354, "sromc" },
160 { 355, "ufs" },
161 { 360, "aclk200_fsys" },
162 { 361, "tsi" },
163 { 362, "pdma0" },
164 { 363, "pdma1" },
165 { 364, "rtic" },
166 { 365, "usbh20" },
167 { 366, "usbd300" },
168 { 367, "usbd301" },
169 { 380, "aclk400_mscl" },
170 { 381, "mscl0" },
171 { 382, "mscl1" },
172 { 383, "mscl2" },
173 { 384, "smmu_mscl0" },
174 { 385, "smmu_mscl1" },
175 { 386, "smmu_mscl2" },
176 { 400, "aclk333" },
177 { 401, "mfc" },
178 { 402, "smmu_mfcl" },
179 { 403, "smmu_mfcr" },
180 { 410, "aclk200_disp1" },
181 { 411, "dsim1" },
182 { 412, "dp1" },
183 { 413, "hdmi" },
184 { 420, "aclk300_disp1" },
185 { 421, "fimd1" },
186 { 422, "smmu_fimd1m0" },
187 { 423, "smmu_fimd1m1" },
188 { 430, "aclk166" },
189 { 431, "mixer" },
190 { 440, "aclk266" },
191 { 441, "rotator" },
192 { 442, "mdma1" },
193 { 443, "smmu_rotator" },
194 { 444, "smmu_mdma1" },
195 { 450, "aclk300_jpeg" },
196 { 451, "jpeg" },
197 { 452, "jpeg2" },
198 { 453, "smmu_jpeg" },
199 { 454, "smmu_jpeg2" },
200 { 460, "aclk300_gscl" },
201 { 461, "smmu_gscl0" },
202 { 462, "smmu_gscl1" },
203 { 463, "gscl_wa" },
204 { 464, "gscl_wb" },
205 { 465, "gscl0" },
206 { 466, "gscl1" },
207 { 467, "fimc_3aa" },
208 { 470, "aclk266_g2d" },
209 { 471, "sss" },
210 { 472, "slim_sss" },
211 { 473, "mdma0" },
212 { 480, "aclk333_g2d" },
213 { 481, "g2d" },
214 { 490, "aclk333_432_gscl" },
215 { 491, "smmu_3aa" },
216 { 492, "smmu_fimcl0" },
217 { 493, "smmu_fimcl1" },
218 { 494, "smmu_fimcl3" },
219 { 495, "fimc_lite3" },
220 { 496, "fimc_lite0" },
221 { 497, "fimc_lite1" },
222 { 500, "aclk_g3d" },
223 { 501, "g3d" },
224 { 502, "smmu_mixer" },
225 { 503, "smmu_g2d" },
226 { 504, "smmu_mdma0" },
227 { 505, "mc" },
228 { 506, "top_rtc" },
229 { 510, "sclk_uart_isp" },
230 { 511, "sclk_spi0_isp" },
231 { 512, "sclk_spi1_isp" },
232 { 513, "sclk_pwm_isp" },
233 { 514, "sclk_isp_sensor0" },
234 { 515, "sclk_isp_sensor1" },
235 { 516, "sclk_isp_sensor2" },
236 { 517, "aclk432_scaler" },
237 { 518, "aclk432_cam" },
238 { 519, "aclk_fl1550_cam" },
239 { 520, "aclk550_cam" },
240 { 640, "mout_hdmi" },
241 { 641, "mout_g3d" },
242 { 642, "mout_vpll" },
243 { 643, "mout_maudio0" },
244 { 644, "mout_user_aclk333" },
245 { 645, "mout_sw_aclk333" },
246 { 646, "mout_user_aclk200_disp1" },
247 { 647, "mout_sw_aclk200" },
248 { 648, "mout_user_aclk300_disp1" },
249 { 649, "mout_sw_aclk300" },
250 { 650, "mout_user_aclk400_disp1" },
251 { 651, "mout_sw_aclk400" },
252 { 768, "dout_pixel" },
253 };
254
255 static struct clk *exynos5422_clock_get(void *, const char *);
256 static void exynos5422_clock_put(void *, struct clk *);
257 static u_int exynos5422_clock_get_rate(void *, struct clk *);
258 static int exynos5422_clock_set_rate(void *, struct clk *, u_int);
259 static int exynos5422_clock_enable(void *, struct clk *);
260 static int exynos5422_clock_disable(void *, struct clk *);
261 static int exynos5422_clock_set_parent(void *, struct clk *, struct clk *);
262 static struct clk *exynos5422_clock_get_parent(void *, struct clk *);
263
264 static const struct clk_funcs exynos5422_clock_funcs = {
265 .get = exynos5422_clock_get,
266 .put = exynos5422_clock_put,
267 .get_rate = exynos5422_clock_get_rate,
268 .set_rate = exynos5422_clock_set_rate,
269 .enable = exynos5422_clock_enable,
270 .disable = exynos5422_clock_disable,
271 .set_parent = exynos5422_clock_set_parent,
272 .get_parent = exynos5422_clock_get_parent,
273 };
274
275 #define CLK_FIXED(_name, _rate) { \
276 .base = { .name = (_name) }, .type = EXYNOS_CLK_FIXED, \
277 .u = { .fixed = { .rate = (_rate) } } \
278 }
279
280 #define CLK_PLL(_name, _parent, _lock, _con0) { \
281 .base = { .name = (_name) }, .type = EXYNOS_CLK_PLL, \
282 .parent = (_parent), \
283 .u = { \
284 .pll = { \
285 .lock_reg = (_lock), \
286 .con0_reg = (_con0), \
287 } \
288 } \
289 }
290
291 #define CLK_MUXF(_name, _alias, _reg, _bits, _f, _p) { \
292 .base = { .name = (_name), .flags = (_f) }, \
293 .type = EXYNOS_CLK_MUX, \
294 .alias = (_alias), \
295 .u = { \
296 .mux = { \
297 .nparents = __arraycount(_p), \
298 .parents = (_p), \
299 .reg = (_reg), \
300 .bits = (_bits) \
301 } \
302 } \
303 }
304
305 #define CLK_MUXA(_name, _alias, _reg, _bits, _p) \
306 CLK_MUXF(_name, _alias, _reg, _bits, 0, _p)
307
308 #define CLK_MUX(_name, _reg, _bits, _p) \
309 CLK_MUXF(_name, NULL, _reg, _bits, 0, _p)
310
311 #define CLK_DIV(_name, _parent, _reg, _bits) { \
312 .base = { .name = (_name) }, .type = EXYNOS_CLK_DIV, \
313 .parent = (_parent), \
314 .u = { \
315 .div = { \
316 .reg = (_reg), \
317 .bits = (_bits) \
318 } \
319 } \
320 }
321
322 #define CLK_GATE(_name, _parent, _reg, _bits, _f) { \
323 .base = { .name = (_name), .flags = (_f) }, \
324 .type = EXYNOS_CLK_GATE, \
325 .parent = (_parent), \
326 .u = { \
327 .gate = { \
328 .reg = (_reg), \
329 .bits = (_bits) \
330 } \
331 } \
332 }
333
334 #define EXYNOS5422_APLL_LOCK 0x00000
335 #define EXYNOS5422_APLL_CON0 0x00100
336 #define EXYNOS5422_CPLL_LOCK 0x10020
337 #define EXYNOS5422_DPLL_LOCK 0x10030
338 #define EXYNOS5422_EPLL_LOCK 0x10040
339 #define EXYNOS5422_RPLL_LOCK 0x10050
340 #define EXYNOS5422_IPLL_LOCK 0x10060
341 #define EXYNOS5422_SPLL_LOCK 0x10070
342 #define EXYNOS5422_VPLL_LOCK 0x10080
343 #define EXYNOS5422_MPLL_LOCK 0x10090
344 #define EXYNOS5422_CPLL_CON0 0x10120
345 #define EXYNOS5422_DPLL_CON0 0x10128
346 #define EXYNOS5422_EPLL_CON0 0x10130
347 #define EXYNOS5422_EPLL_CON1 0x10134
348 #define EXYNOS5422_EPLL_CON2 0x10138
349 #define EXYNOS5422_RPLL_CON0 0x10140
350 #define EXYNOS5422_RPLL_CON1 0x10144
351 #define EXYNOS5422_RPLL_CON2 0x10148
352 #define EXYNOS5422_IPLL_CON0 0x10150
353 #define EXYNOS5422_SPLL_CON0 0x10160
354 #define EXYNOS5422_VPLL_CON0 0x10170
355 #define EXYNOS5422_MPLL_CON0 0x10180
356 #define EXYNOS5422_BPLL_LOCK 0x20010
357 #define EXYNOS5422_BPLL_CON0 0x20110
358 #define EXYNOS5422_KPLL_LOCK 0x28000
359 #define EXYNOS5422_KPLL_CON0 0x28100
360
361 #define EXYNOS5422_SRC_CPU 0x00200
362 #define EXYNOS5422_SRC_TOP0 0x10200
363 #define EXYNOS5422_SRC_TOP1 0x10204
364 #define EXYNOS5422_SRC_TOP2 0x10208
365 #define EXYNOS5422_SRC_TOP3 0x1020c
366 #define EXYNOS5422_SRC_TOP4 0x10210
367 #define EXYNOS5422_SRC_TOP5 0x10214
368 #define EXYNOS5422_SRC_TOP6 0x10218
369 #define EXYNOS5422_SRC_TOP7 0x1021c
370 #define EXYNOS5422_SRC_DISP10 0x1022c
371 #define EXYNOS5422_SRC_MAU 0x10240
372 #define EXYNOS5422_SRC_FSYS 0x10244
373 #define EXYNOS5422_SRC_PERIC0 0x10250
374 #define EXYNOS5422_SRC_PERIC1 0x10254
375 #define EXYNOS5422_SRC_ISP 0x10270
376 #define EXYNOS5422_SRC_TOP10 0x10280
377 #define EXYNOS5422_SRC_TOP11 0x10280
378 #define EXYNOS5422_SRC_TOP12 0x10280
379
380 #define EXYNOS5422_DIV_FSYS1 0x1054c
381 #define EXYNOS5422_DIV_PERIC0 0x10558
382
383 #define EXYNOS5422_GATE_TOP_SCLK_FSYS 0x10840
384 #define EXYNOS5422_GATE_TOP_SCLK_PERIC 0x10850
385
386 static const char *mout_cpll_p[] = { "fin_pll", "fout_cpll" };
387 static const char *mout_dpll_p[] = { "fin_pll", "fout_dpll" };
388 static const char *mout_mpll_p[] = { "fin_pll", "fout_mpll" };
389 static const char *mout_spll_p[] = { "fin_pll", "fout_spll" };
390 static const char *mout_ipll_p[] = { "fin_pll", "fout_ipll" };
391 static const char *mout_epll_p[] = { "fin_pll", "fout_epll" };
392 static const char *mout_rpll_p[] = { "fin_pll", "fout_rpll" };
393 static const char *mout_group2_p[] =
394 { "fin_pll", "sclk_cpll", "sclk_dpll", "sclk_mpll",
395 "sclk_spll", "sclk_ipll", "sclk_epll", "sclk_rpll" };
396
397 static struct exynos_clk exynos5422_clocks[] = {
398 CLK_FIXED("fin_pll", EXYNOS_F_IN_FREQ),
399
400 CLK_PLL("fout_apll", "fin_pll", EXYNOS5422_APLL_LOCK,
401 EXYNOS5422_APLL_CON0),
402 CLK_PLL("fout_cpll", "fin_pll", EXYNOS5422_CPLL_LOCK,
403 EXYNOS5422_CPLL_CON0),
404 CLK_PLL("fout_dpll", "fin_pll", EXYNOS5422_DPLL_LOCK,
405 EXYNOS5422_DPLL_CON0),
406 CLK_PLL("fout_epll", "fin_pll", EXYNOS5422_EPLL_LOCK,
407 EXYNOS5422_EPLL_CON0),
408 CLK_PLL("fout_rpll", "fin_pll", EXYNOS5422_RPLL_LOCK,
409 EXYNOS5422_RPLL_CON0),
410 CLK_PLL("fout_ipll", "fin_pll", EXYNOS5422_IPLL_LOCK,
411 EXYNOS5422_IPLL_CON0),
412 CLK_PLL("fout_spll", "fin_pll", EXYNOS5422_SPLL_LOCK,
413 EXYNOS5422_SPLL_CON0),
414 CLK_PLL("fout_vpll", "fin_pll", EXYNOS5422_VPLL_LOCK,
415 EXYNOS5422_VPLL_CON0),
416 CLK_PLL("fout_mpll", "fin_pll", EXYNOS5422_MPLL_LOCK,
417 EXYNOS5422_MPLL_CON0),
418 CLK_PLL("fout_bpll", "fin_pll", EXYNOS5422_BPLL_LOCK,
419 EXYNOS5422_BPLL_CON0),
420 CLK_PLL("fout_kpll", "fin_pll", EXYNOS5422_KPLL_LOCK,
421 EXYNOS5422_KPLL_CON0),
422
423 CLK_MUXA("sclk_cpll", "mout_cpll", EXYNOS5422_SRC_TOP6, __BIT(28),
424 mout_cpll_p),
425 CLK_MUXA("sclk_dpll", "mout_dpll", EXYNOS5422_SRC_TOP6, __BIT(24),
426 mout_dpll_p),
427 CLK_MUXA("sclk_mpll", "mout_mpll", EXYNOS5422_SRC_TOP6, __BIT(0),
428 mout_mpll_p),
429 CLK_MUXA("sclk_spll", "mout_spll", EXYNOS5422_SRC_TOP6, __BIT(8),
430 mout_spll_p),
431 CLK_MUXA("sclk_ipll", "mout_ipll", EXYNOS5422_SRC_TOP6, __BIT(12),
432 mout_ipll_p),
433 CLK_MUXF("sclk_epll", "mout_epll", EXYNOS5422_SRC_TOP6, __BIT(20),
434 CLK_SET_RATE_PARENT, mout_epll_p),
435 CLK_MUXF("sclk_rpll", "mout_rpll", EXYNOS5422_SRC_TOP6, __BIT(16),
436 CLK_SET_RATE_PARENT, mout_rpll_p),
437
438 CLK_MUX("mout_mmc0", EXYNOS5422_SRC_FSYS, __BITS(10,8),
439 mout_group2_p),
440 CLK_MUX("mout_mmc1", EXYNOS5422_SRC_FSYS, __BITS(14,12),
441 mout_group2_p),
442 CLK_MUX("mout_mmc2", EXYNOS5422_SRC_FSYS, __BITS(18,16),
443 mout_group2_p),
444 CLK_MUX("mout_uart0", EXYNOS5422_SRC_PERIC0, __BITS(6,4),
445 mout_group2_p),
446 CLK_MUX("mout_uart1", EXYNOS5422_SRC_PERIC0, __BITS(10,8),
447 mout_group2_p),
448 CLK_MUX("mout_uart2", EXYNOS5422_SRC_PERIC0, __BITS(14,12),
449 mout_group2_p),
450 CLK_MUX("mout_uart3", EXYNOS5422_SRC_PERIC0, __BITS(18,16),
451 mout_group2_p),
452
453 CLK_DIV("dout_mmc0", "mout_mmc0", EXYNOS5422_DIV_FSYS1, __BITS(9,0)),
454 CLK_DIV("dout_mmc1", "mout_mmc1", EXYNOS5422_DIV_FSYS1, __BITS(19,10)),
455 CLK_DIV("dout_mmc2", "mout_mmc2", EXYNOS5422_DIV_FSYS1, __BITS(29,20)),
456 CLK_DIV("dout_uart0", "mout_uart0", EXYNOS5422_DIV_PERIC0,
457 __BITS(11,8)),
458 CLK_DIV("dout_uart1", "mout_uart1", EXYNOS5422_DIV_PERIC0,
459 __BITS(15,12)),
460 CLK_DIV("dout_uart2", "mout_uart2", EXYNOS5422_DIV_PERIC0,
461 __BITS(19,16)),
462 CLK_DIV("dout_uart3", "mout_uart3", EXYNOS5422_DIV_PERIC0,
463 __BITS(23,20)),
464
465 CLK_GATE("sclk_mmc0", "dout_mmc0", EXYNOS5422_GATE_TOP_SCLK_FSYS,
466 __BIT(0), CLK_SET_RATE_PARENT),
467 CLK_GATE("sclk_mmc1", "dout_mmc1", EXYNOS5422_GATE_TOP_SCLK_FSYS,
468 __BIT(1), CLK_SET_RATE_PARENT),
469 CLK_GATE("sclk_mmc2", "dout_mmc2", EXYNOS5422_GATE_TOP_SCLK_FSYS,
470 __BIT(2), CLK_SET_RATE_PARENT),
471 CLK_GATE("sclk_uart0", "dout_uart0", EXYNOS5422_GATE_TOP_SCLK_PERIC,
472 __BIT(0), CLK_SET_RATE_PARENT),
473 CLK_GATE("sclk_uart1", "dout_uart1", EXYNOS5422_GATE_TOP_SCLK_PERIC,
474 __BIT(1), CLK_SET_RATE_PARENT),
475 CLK_GATE("sclk_uart2", "dout_uart2", EXYNOS5422_GATE_TOP_SCLK_PERIC,
476 __BIT(2), CLK_SET_RATE_PARENT),
477 CLK_GATE("sclk_uart3", "dout_uart3", EXYNOS5422_GATE_TOP_SCLK_PERIC,
478 __BIT(3), CLK_SET_RATE_PARENT),
479 };
480
481 static int exynos5422_clock_match(device_t, cfdata_t, void *);
482 static void exynos5422_clock_attach(device_t, device_t, void *);
483
484 struct exynos5422_clock_softc {
485 device_t sc_dev;
486 bus_space_tag_t sc_bst;
487 bus_space_handle_t sc_bsh;
488
489 struct clk_domain sc_clkdom;
490 };
491
492 static void exynos5422_clock_print_header(void);
493 static void exynos5422_clock_print(struct exynos5422_clock_softc *,
494 struct exynos_clk *);
495
496 CFATTACH_DECL_NEW(exynos5422_clock, sizeof(struct exynos5422_clock_softc),
497 exynos5422_clock_match, exynos5422_clock_attach, NULL, NULL);
498
499 #define CLOCK_READ(sc, reg) \
500 bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
501 #define CLOCK_WRITE(sc, reg, val) \
502 bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
503
504 static int
505 exynos5422_clock_match(device_t parent, cfdata_t cf, void *aux)
506 {
507 const char * const compatible[] = { "samsung,exynos5800-clock", NULL };
508 struct fdt_attach_args * const faa = aux;
509
510 return of_match_compatible(faa->faa_phandle, compatible);
511 }
512
513 static void
514 exynos5422_clock_attach(device_t parent, device_t self, void *aux)
515 {
516 struct exynos5422_clock_softc * const sc = device_private(self);
517 struct fdt_attach_args * const faa = aux;
518 bus_addr_t addr;
519 bus_size_t size;
520 int error;
521
522 if (fdtbus_get_reg(faa->faa_phandle, 0, &addr, &size) != 0) {
523 aprint_error(": couldn't get registers\n");
524 return;
525 }
526
527 sc->sc_dev = self;
528 sc->sc_bst = faa->faa_bst;
529
530 error = bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh);
531 if (error) {
532 aprint_error(": couldn't map %#llx: %d",
533 (uint64_t)addr, error);
534 return;
535 }
536
537 aprint_naive("\n");
538 aprint_normal(": Exynos5422 Clock Controller\n");
539
540 sc->sc_clkdom.funcs = &exynos5422_clock_funcs;
541 sc->sc_clkdom.priv = sc;
542 for (u_int n = 0; n < __arraycount(exynos5422_clocks); n++) {
543 exynos5422_clocks[n].base.domain = &sc->sc_clkdom;
544 }
545
546 fdtbus_register_clock_controller(self, faa->faa_phandle,
547 &exynos5422_car_fdtclock_funcs);
548
549 exynos5422_clock_print_header();
550 for (u_int n = 0; n < __arraycount(exynos5422_clocks); n++) {
551 exynos5422_clock_print(sc, &exynos5422_clocks[n]);
552 }
553 }
554
555 static struct exynos_clk *
556 exynos5422_clock_find(const char *name)
557 {
558 u_int n;
559
560 for (n = 0; n < __arraycount(exynos5422_clocks); n++) {
561 if (strcmp(exynos5422_clocks[n].base.name, name) == 0) {
562 return &exynos5422_clocks[n];
563 }
564 }
565
566 return NULL;
567 }
568
569 static struct exynos_clk *
570 exynos5422_clock_find_by_id(u_int clock_id)
571 {
572 u_int n;
573
574 for (n = 0; n < __arraycount(exynos5422_clock_ids); n++) {
575 if (exynos5422_clock_ids[n].id == clock_id) {
576 const char *name = exynos5422_clock_ids[n].name;
577 return exynos5422_clock_find(name);
578 }
579 }
580
581 return NULL;
582 }
583
584 static void
585 exynos5422_clock_print_header(void)
586 {
587 printf(" %-10s %2s %-10s %-5s %10s\n",
588 "clock", "", "parent", "type", "rate");
589 printf(" %-10s %2s %-10s %-5s %10s\n",
590 "=====", "", "======", "====", "====");
591 }
592
593 static void
594 exynos5422_clock_print(struct exynos5422_clock_softc *sc,
595 struct exynos_clk *eclk)
596 {
597 struct exynos_clk *eclk_parent;
598 struct clk *clk_parent;
599 const char *type = "?";
600
601 switch (eclk->type) {
602 case EXYNOS_CLK_FIXED:
603 type = "fixed";
604 break;
605 case EXYNOS_CLK_PLL:
606 type = "pll";
607 break;
608 case EXYNOS_CLK_MUX:
609 type = "mux";
610 break;
611 case EXYNOS_CLK_DIV:
612 type = "div";
613 break;
614 case EXYNOS_CLK_GATE:
615 type = "gate";
616 break;
617 }
618
619 clk_parent = exynos5422_clock_get_parent(sc, &eclk->base);
620 eclk_parent = (struct exynos_clk *)clk_parent;
621
622 printf(" %-10s %2s %-10s %-5s %10d Hz\n",
623 eclk->base.name,
624 eclk_parent ? "<-" : "",
625 eclk_parent ? eclk_parent->base.name : "",
626 type, clk_get_rate(&eclk->base));
627 }
628
629 static struct clk *
630 exynos5422_clock_decode(device_t dev, const void *data, size_t len)
631 {
632 struct exynos_clk *eclk;
633
634 /* #clock-cells should be 1 */
635 if (len != 4) {
636 return NULL;
637 }
638
639 const u_int clock_id = be32dec(data);
640
641 eclk = exynos5422_clock_find_by_id(clock_id);
642 if (eclk)
643 return &eclk->base;
644
645 return NULL;
646 }
647
648 static u_int
649 exynos5422_clock_get_rate_pll(struct exynos5422_clock_softc *sc,
650 struct exynos_clk *eclk)
651 {
652 struct exynos_pll_clk *epll = &eclk->u.pll;
653 struct exynos_clk *clk_parent;
654
655 KASSERT(eclk->type == EXYNOS_CLK_PLL);
656
657 clk_parent = exynos5422_clock_find(eclk->parent);
658 KASSERT(clk_parent != NULL);
659 const u_int rate_parent = exynos5422_clock_get_rate(sc,
660 &clk_parent->base);
661
662 const uint32_t v = CLOCK_READ(sc, epll->con0_reg);
663
664 return PLL_FREQ(rate_parent, v);
665 }
666
667 static int
668 exynos5422_clock_set_rate_pll(struct exynos5422_clock_softc *sc,
669 struct exynos_clk *eclk, u_int rate)
670 {
671 /* TODO */
672 return EOPNOTSUPP;
673 }
674
675 static int
676 exynos5422_clock_set_parent_mux(struct exynos5422_clock_softc *sc,
677 struct exynos_clk *eclk, struct exynos_clk *eclk_parent)
678 {
679 struct exynos_mux_clk *emux = &eclk->u.mux;
680 const char *pname = eclk_parent->base.name;
681 u_int sel;
682
683 KASSERT(eclk->type == EXYNOS_CLK_MUX);
684
685 for (sel = 0; sel < emux->nparents; sel++) {
686 if (strcmp(pname, emux->parents[sel]) == 0) {
687 break;
688 }
689 }
690 if (sel == emux->nparents) {
691 return EINVAL;
692 }
693
694 uint32_t v = CLOCK_READ(sc, emux->reg);
695 v &= ~emux->bits;
696 v |= __SHIFTIN(sel, emux->bits);
697 CLOCK_WRITE(sc, emux->reg, v);
698
699 return 0;
700 }
701
702 static struct exynos_clk *
703 exynos5422_clock_get_parent_mux(struct exynos5422_clock_softc *sc,
704 struct exynos_clk *eclk)
705 {
706 struct exynos_mux_clk *emux = &eclk->u.mux;
707
708 KASSERT(eclk->type == EXYNOS_CLK_MUX);
709
710 const uint32_t v = CLOCK_READ(sc, emux->reg);
711 const u_int sel = __SHIFTOUT(v, emux->bits);
712
713 KASSERT(sel < emux->nparents);
714
715 return exynos5422_clock_find(emux->parents[sel]);
716 }
717
718 static u_int
719 exynos5422_clock_get_rate_div(struct exynos5422_clock_softc *sc,
720 struct exynos_clk *eclk)
721 {
722 struct exynos_div_clk *ediv = &eclk->u.div;
723 struct clk *clk_parent;
724
725 KASSERT(eclk->type == EXYNOS_CLK_DIV);
726
727 clk_parent = exynos5422_clock_get_parent(sc, &eclk->base);
728 const u_int parent_rate = exynos5422_clock_get_rate(sc, clk_parent);
729
730 const uint32_t v = CLOCK_READ(sc, ediv->reg);
731 const u_int div = __SHIFTOUT(v, ediv->bits);
732
733 return parent_rate / (div + 1);
734 }
735
736 static int
737 exynos5422_clock_set_rate_div(struct exynos5422_clock_softc *sc,
738 struct exynos_clk *eclk, u_int rate)
739 {
740 struct exynos_div_clk *ediv = &eclk->u.div;
741 struct clk *clk_parent;
742 int tmp_div, new_div = -1;
743 u_int tmp_rate;
744
745 KASSERT(eclk->type == EXYNOS_CLK_DIV);
746
747 clk_parent = exynos5422_clock_get_parent(sc, &eclk->base);
748 const u_int parent_rate = exynos5422_clock_get_rate(sc, clk_parent);
749
750 for (tmp_div = 0; tmp_div < popcount32(ediv->bits); tmp_div++) {
751 tmp_rate = parent_rate / (tmp_div + 1);
752 if (tmp_rate <= rate) {
753 new_div = tmp_div;
754 break;
755 }
756 }
757 if (new_div == -1)
758 return EINVAL;
759
760 uint32_t v = CLOCK_READ(sc, ediv->reg);
761 v &= ~ediv->bits;
762 v |= __SHIFTIN(new_div, ediv->bits);
763 CLOCK_WRITE(sc, ediv->reg, v);
764
765 return 0;
766 }
767
768 static int
769 exynos5422_clock_enable_gate(struct exynos5422_clock_softc *sc,
770 struct exynos_clk *eclk, bool enable)
771 {
772 struct exynos_gate_clk *egate = &eclk->u.gate;
773
774 KASSERT(eclk->type == EXYNOS_CLK_GATE);
775
776 uint32_t v = CLOCK_READ(sc, egate->reg);
777 if (enable) {
778 v |= egate->bits;
779 } else {
780 v &= ~egate->bits;
781 }
782 CLOCK_WRITE(sc, egate->reg, v);
783
784 return 0;
785 }
786
787 /*
788 * clk api
789 */
790
791 static struct clk *
792 exynos5422_clock_get(void *priv, const char *name)
793 {
794 struct exynos_clk *eclk;
795
796 eclk = exynos5422_clock_find(name);
797 if (eclk == NULL)
798 return NULL;
799
800 atomic_inc_uint(&eclk->refcnt);
801
802 return &eclk->base;
803 }
804
805 static void
806 exynos5422_clock_put(void *priv, struct clk *clk)
807 {
808 struct exynos_clk *eclk = (struct exynos_clk *)clk;
809
810 KASSERT(eclk->refcnt > 0);
811
812 atomic_dec_uint(&eclk->refcnt);
813 }
814
815 static u_int
816 exynos5422_clock_get_rate(void *priv, struct clk *clk)
817 {
818 struct exynos_clk *eclk = (struct exynos_clk *)clk;
819 struct clk *clk_parent;
820
821 switch (eclk->type) {
822 case EXYNOS_CLK_FIXED:
823 return eclk->u.fixed.rate;
824 case EXYNOS_CLK_PLL:
825 return exynos5422_clock_get_rate_pll(priv, eclk);
826 case EXYNOS_CLK_MUX:
827 case EXYNOS_CLK_GATE:
828 clk_parent = exynos5422_clock_get_parent(priv, clk);
829 return exynos5422_clock_get_rate(priv, clk_parent);
830 case EXYNOS_CLK_DIV:
831 return exynos5422_clock_get_rate_div(priv, eclk);
832 default:
833 panic("exynos5422: unknown eclk type %d", eclk->type);
834 }
835 }
836
837 static int
838 exynos5422_clock_set_rate(void *priv, struct clk *clk, u_int rate)
839 {
840 struct exynos_clk *eclk = (struct exynos_clk *)clk;
841
842 KASSERT((clk->flags & CLK_SET_RATE_PARENT) == 0);
843
844 switch (eclk->type) {
845 case EXYNOS_CLK_FIXED:
846 return EIO;
847 case EXYNOS_CLK_PLL:
848 return exynos5422_clock_set_rate_pll(priv, eclk, rate);
849 case EXYNOS_CLK_MUX:
850 return EIO;
851 case EXYNOS_CLK_DIV:
852 return exynos5422_clock_set_rate_div(priv, eclk, rate);
853 case EXYNOS_CLK_GATE:
854 return EINVAL;
855 default:
856 panic("exynos5422: unknown eclk type %d", eclk->type);
857 }
858 }
859
860 static int
861 exynos5422_clock_enable(void *priv, struct clk *clk)
862 {
863 struct exynos_clk *eclk = (struct exynos_clk *)clk;
864
865 switch (eclk->type) {
866 case EXYNOS_CLK_FIXED:
867 return 0; /* always on */
868 case EXYNOS_CLK_PLL:
869 return 0; /* XXX */
870 case EXYNOS_CLK_MUX:
871 case EXYNOS_CLK_DIV:
872 return 0;
873 case EXYNOS_CLK_GATE:
874 return exynos5422_clock_enable_gate(priv, eclk, true);
875 default:
876 panic("exynos5422: unknown eclk type %d", eclk->type);
877 }
878 }
879
880 static int
881 exynos5422_clock_disable(void *priv, struct clk *clk)
882 {
883 struct exynos_clk *eclk = (struct exynos_clk *)clk;
884
885 switch (eclk->type) {
886 case EXYNOS_CLK_FIXED:
887 return EINVAL; /* always on */
888 case EXYNOS_CLK_PLL:
889 return EINVAL; /* XXX */
890 case EXYNOS_CLK_MUX:
891 case EXYNOS_CLK_DIV:
892 return EINVAL;
893 case EXYNOS_CLK_GATE:
894 return exynos5422_clock_enable_gate(priv, eclk, false);
895 default:
896 panic("exynos5422: unknown eclk type %d", eclk->type);
897 }
898 }
899
900 static int
901 exynos5422_clock_set_parent(void *priv, struct clk *clk, struct clk *clk_parent)
902 {
903 struct exynos_clk *eclk = (struct exynos_clk *)clk;
904 struct exynos_clk *eclk_parent = (struct exynos_clk *)clk_parent;
905
906 switch (eclk->type) {
907 case EXYNOS_CLK_FIXED:
908 case EXYNOS_CLK_PLL:
909 case EXYNOS_CLK_DIV:
910 case EXYNOS_CLK_GATE:
911 return EINVAL;
912 case EXYNOS_CLK_MUX:
913 return exynos5422_clock_set_parent_mux(priv, eclk, eclk_parent);
914 default:
915 panic("exynos5422: unknown eclk type %d", eclk->type);
916 }
917 }
918
919 static struct clk *
920 exynos5422_clock_get_parent(void *priv, struct clk *clk)
921 {
922 struct exynos_clk *eclk = (struct exynos_clk *)clk;
923 struct exynos_clk *eclk_parent = NULL;
924
925 switch (eclk->type) {
926 case EXYNOS_CLK_FIXED:
927 case EXYNOS_CLK_PLL:
928 case EXYNOS_CLK_DIV:
929 case EXYNOS_CLK_GATE:
930 if (eclk->parent != NULL) {
931 eclk_parent = exynos5422_clock_find(eclk->parent);
932 }
933 break;
934 case EXYNOS_CLK_MUX:
935 eclk_parent = exynos5422_clock_get_parent_mux(priv, eclk);
936 break;
937 default:
938 panic("exynos5422: unknown eclk type %d", eclk->type);
939 }
940
941 return (struct clk *)eclk_parent;
942 }
943