Home | History | Annotate | Line # | Download | only in samsung
exynos5422_clock.c revision 1.5
      1 /* $NetBSD: exynos5422_clock.c,v 1.5 2017/04/16 15:52:43 jmcneill 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.5 2017/04/16 15:52:43 jmcneill 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, _base) {			\
    281 	.base = { .name = (_name) }, .type = EXYNOS_CLK_PLL,	\
    282 	.parent = (_parent),					\
    283 	.u = {							\
    284 		.pll = {					\
    285 			.con0_reg = (_base) + PLL_CON0_OFFSET,	\
    286 			.lock_reg = (_base) + PLL_LOCK_OFFSET,	\
    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_BASE		0x00000
    335 #define EXYNOS5422_CPLL_BASE		0x10020
    336 #define EXYNOS5422_DPLL_BASE		0x10030
    337 #define EXYNOS5422_EPLL_BASE		0x10040
    338 #define EXYNOS5422_RPLL_BASE		0x10050
    339 #define EXYNOS5422_IPLL_BASE		0x10060
    340 #define EXYNOS5422_SPLL_BASE		0x10070
    341 #define EXYNOS5422_VPLL_BASE		0x10080
    342 #define EXYNOS5422_MPLL_BASE		0x10090
    343 #define EXYNOS5422_BPLL_BASE		0x20010
    344 #define EXYNOS5422_KPLL_BASE		0x28000
    345 
    346 #define EXYNOS5422_SRC_CPU		0x00200
    347 #define EXYNOS5422_SRC_TOP0		0x10200
    348 #define EXYNOS5422_SRC_TOP1		0x10204
    349 #define EXYNOS5422_SRC_TOP2		0x10208
    350 #define EXYNOS5422_SRC_TOP3		0x1020c
    351 #define EXYNOS5422_SRC_TOP4		0x10210
    352 #define EXYNOS5422_SRC_TOP5		0x10214
    353 #define EXYNOS5422_SRC_TOP6		0x10218
    354 #define EXYNOS5422_SRC_TOP7		0x1021c
    355 #define EXYNOS5422_SRC_DISP10		0x1022c
    356 #define EXYNOS5422_SRC_MAU		0x10240
    357 #define EXYNOS5422_SRC_FSYS		0x10244
    358 #define EXYNOS5422_SRC_PERIC0		0x10250
    359 #define EXYNOS5422_SRC_PERIC1		0x10254
    360 #define EXYNOS5422_SRC_ISP		0x10270
    361 #define EXYNOS5422_SRC_TOP10		0x10280
    362 #define EXYNOS5422_SRC_TOP11		0x10280
    363 #define EXYNOS5422_SRC_TOP12		0x10280
    364 
    365 #define EXYNOS5422_DIV_FSYS1		0x1054c
    366 
    367 #define EXYNOS5422_GATE_TOP_SCLK_FSYS	0x10840
    368 
    369 static const char *mout_cpll_p[] = { "fin_pll", "fout_cpll" };
    370 static const char *mout_dpll_p[] = { "fin_pll", "fout_dpll" };
    371 static const char *mout_mpll_p[] = { "fin_pll", "fout_mpll" };
    372 static const char *mout_spll_p[] = { "fin_pll", "fout_spll" };
    373 static const char *mout_ipll_p[] = { "fin_pll", "fout_ipll" };
    374 static const char *mout_epll_p[] = { "fin_pll", "fout_epll" };
    375 static const char *mout_rpll_p[] = { "fin_pll", "fout_rpll" };
    376 static const char *mout_group2_p[] =
    377 	{ "fin_pll", "sclk_cpll", "sclk_dpll", "sclk_mpll",
    378 	  "sclk_spll", "sclk_ipll", "sclk_epll", "sclk_rpll" };
    379 
    380 static struct exynos_clk exynos5422_clocks[] = {
    381 	CLK_FIXED("fin_pll", EXYNOS_F_IN_FREQ),
    382 
    383 	CLK_PLL("fout_apll", "fin_pll", EXYNOS5422_APLL_BASE),
    384 	CLK_PLL("fout_cpll", "fin_pll", EXYNOS5422_CPLL_BASE),
    385 	CLK_PLL("fout_dpll", "fin_pll", EXYNOS5422_DPLL_BASE),
    386 	CLK_PLL("fout_epll", "fin_pll", EXYNOS5422_EPLL_BASE),
    387 	CLK_PLL("fout_rpll", "fin_pll", EXYNOS5422_RPLL_BASE),
    388 	CLK_PLL("fout_ipll", "fin_pll", EXYNOS5422_IPLL_BASE),
    389 	CLK_PLL("fout_spll", "fin_pll", EXYNOS5422_SPLL_BASE),
    390 	CLK_PLL("fout_vpll", "fin_pll", EXYNOS5422_VPLL_BASE),
    391 	CLK_PLL("fout_mpll", "fin_pll", EXYNOS5422_MPLL_BASE),
    392 	CLK_PLL("fout_bpll", "fin_pll", EXYNOS5422_BPLL_BASE),
    393 	CLK_PLL("fout_kpll", "fin_pll", EXYNOS5422_KPLL_BASE),
    394 
    395 	CLK_MUXA("sclk_cpll", "mout_cpll", EXYNOS5422_SRC_TOP6, __BIT(28),
    396 	    mout_cpll_p),
    397 	CLK_MUXA("sclk_dpll", "mout_dpll", EXYNOS5422_SRC_TOP6, __BIT(24),
    398 	    mout_dpll_p),
    399 	CLK_MUXA("sclk_mpll", "mout_mpll", EXYNOS5422_SRC_TOP6, __BIT(0),
    400 	    mout_mpll_p),
    401 	CLK_MUXA("sclk_spll", "mout_spll", EXYNOS5422_SRC_TOP6, __BIT(8),
    402 	    mout_spll_p),
    403 	CLK_MUXA("sclk_ipll", "mout_ipll", EXYNOS5422_SRC_TOP6, __BIT(12),
    404 	    mout_ipll_p),
    405 	CLK_MUXF("sclk_epll", "mout_epll", EXYNOS5422_SRC_TOP6, __BIT(20),
    406 	    CLK_SET_RATE_PARENT, mout_epll_p),
    407 	CLK_MUXF("sclk_rpll", "mout_rpll", EXYNOS5422_SRC_TOP6, __BIT(16),
    408 	    CLK_SET_RATE_PARENT, mout_rpll_p),
    409 
    410 	CLK_MUX("mout_mmc0", EXYNOS5422_SRC_FSYS, __BITS(10,8),
    411 	    mout_group2_p),
    412 	CLK_MUX("mout_mmc1", EXYNOS5422_SRC_FSYS, __BITS(14,12),
    413 	    mout_group2_p),
    414 	CLK_MUX("mout_mmc2", EXYNOS5422_SRC_FSYS, __BITS(18,16),
    415 	    mout_group2_p),
    416 
    417 	CLK_DIV("dout_mmc0", "mout_mmc0", EXYNOS5422_DIV_FSYS1, __BITS(9,0)),
    418 	CLK_DIV("dout_mmc1", "mout_mmc1", EXYNOS5422_DIV_FSYS1, __BITS(19,10)),
    419 	CLK_DIV("dout_mmc2", "mout_mmc2", EXYNOS5422_DIV_FSYS1, __BITS(29,20)),
    420 
    421 	CLK_GATE("sclk_mmc0", "dout_mmc0", EXYNOS5422_GATE_TOP_SCLK_FSYS,
    422 	    __BIT(0), CLK_SET_RATE_PARENT),
    423 	CLK_GATE("sclk_mmc1", "dout_mmc1", EXYNOS5422_GATE_TOP_SCLK_FSYS,
    424 	    __BIT(1), CLK_SET_RATE_PARENT),
    425 	CLK_GATE("sclk_mmc2", "dout_mmc2", EXYNOS5422_GATE_TOP_SCLK_FSYS,
    426 	    __BIT(2), CLK_SET_RATE_PARENT),
    427 };
    428 
    429 static int	exynos5422_clock_match(device_t, cfdata_t, void *);
    430 static void	exynos5422_clock_attach(device_t, device_t, void *);
    431 
    432 struct exynos5422_clock_softc {
    433 	device_t		sc_dev;
    434 	bus_space_tag_t		sc_bst;
    435 	bus_space_handle_t	sc_bsh;
    436 
    437 	struct clk_domain	sc_clkdom;
    438 };
    439 
    440 static void	exynos5422_clock_print_header(void);
    441 static void	exynos5422_clock_print(struct exynos5422_clock_softc *,
    442 		    struct exynos_clk *);
    443 
    444 CFATTACH_DECL_NEW(exynos5422_clock, sizeof(struct exynos5422_clock_softc),
    445 	exynos5422_clock_match, exynos5422_clock_attach, NULL, NULL);
    446 
    447 #define CLOCK_READ(sc, reg)		\
    448     bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
    449 #define CLOCK_WRITE(sc, reg, val)	\
    450     bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
    451 
    452 static int
    453 exynos5422_clock_match(device_t parent, cfdata_t cf, void *aux)
    454 {
    455 	const char * const compatible[] = { "samsung,exynos5800-clock", NULL };
    456 	struct fdt_attach_args * const faa = aux;
    457 
    458 	return of_match_compatible(faa->faa_phandle, compatible);
    459 }
    460 
    461 static void
    462 exynos5422_clock_attach(device_t parent, device_t self, void *aux)
    463 {
    464 	struct exynos5422_clock_softc * const sc = device_private(self);
    465 	struct fdt_attach_args * const faa = aux;
    466 	bus_addr_t addr;
    467 	bus_size_t size;
    468 	int error;
    469 
    470 	if (fdtbus_get_reg(faa->faa_phandle, 0, &addr, &size) != 0) {
    471 		aprint_error(": couldn't get registers\n");
    472 		return;
    473 	}
    474 
    475 	sc->sc_dev = self;
    476 	sc->sc_bst = faa->faa_bst;
    477 
    478 	error = bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh);
    479 	if (error) {
    480 		aprint_error(": couldn't map %#llx: %d",
    481 			     (uint64_t)addr, error);
    482 		return;
    483 	}
    484 
    485 	aprint_naive("\n");
    486 	aprint_normal(": Exynos5422 Clock Controller\n");
    487 
    488 	sc->sc_clkdom.funcs = &exynos5422_clock_funcs;
    489 	sc->sc_clkdom.priv = sc;
    490 	for (u_int n = 0; n < __arraycount(exynos5422_clocks); n++) {
    491 		exynos5422_clocks[n].base.domain = &sc->sc_clkdom;
    492 	}
    493 
    494 	fdtbus_register_clock_controller(self, faa->faa_phandle,
    495 	    &exynos5422_car_fdtclock_funcs);
    496 
    497 	exynos5422_clock_print_header();
    498 	for (u_int n = 0; n < __arraycount(exynos5422_clocks); n++) {
    499 		exynos5422_clock_print(sc, &exynos5422_clocks[n]);
    500 	}
    501 }
    502 
    503 static struct exynos_clk *
    504 exynos5422_clock_find(const char *name)
    505 {
    506 	u_int n;
    507 
    508 	for (n = 0; n < __arraycount(exynos5422_clocks); n++) {
    509 		if (strcmp(exynos5422_clocks[n].base.name, name) == 0) {
    510 			return &exynos5422_clocks[n];
    511 		}
    512 	}
    513 
    514 	return NULL;
    515 }
    516 
    517 static struct exynos_clk *
    518 exynos5422_clock_find_by_id(u_int clock_id)
    519 {
    520 	u_int n;
    521 
    522 	for (n = 0; n < __arraycount(exynos5422_clock_ids); n++) {
    523 		if (exynos5422_clock_ids[n].id == clock_id) {
    524 			const char *name = exynos5422_clock_ids[n].name;
    525 			return exynos5422_clock_find(name);
    526 		}
    527 	}
    528 
    529 	return NULL;
    530 }
    531 
    532 static void
    533 exynos5422_clock_print_header(void)
    534 {
    535 	printf("  %-10s %2s %-10s %-5s %10s\n",
    536 	    "clock", "", "parent", "type", "rate");
    537 	printf("  %-10s %2s %-10s %-5s %10s\n",
    538 	    "=====", "", "======", "====", "====");
    539 }
    540 
    541 static void
    542 exynos5422_clock_print(struct exynos5422_clock_softc *sc,
    543     struct exynos_clk *eclk)
    544 {
    545 	struct exynos_clk *eclk_parent;
    546 	struct clk *clk_parent;
    547 	const char *type = "?";
    548 
    549 	switch (eclk->type) {
    550 	case EXYNOS_CLK_FIXED:
    551 		type = "fixed";
    552 		break;
    553 	case EXYNOS_CLK_PLL:
    554 		type = "pll";
    555 		break;
    556 	case EXYNOS_CLK_MUX:
    557 		type = "mux";
    558 		break;
    559 	case EXYNOS_CLK_DIV:
    560 		type = "div";
    561 		break;
    562 	case EXYNOS_CLK_GATE:
    563 		type = "gate";
    564 		break;
    565 	}
    566 
    567 	clk_parent = exynos5422_clock_get_parent(sc, &eclk->base);
    568 	eclk_parent = (struct exynos_clk *)clk_parent;
    569 
    570 	printf("  %-10s %2s %-10s %-5s %10d Hz\n",
    571 	    eclk->base.name,
    572 	    eclk_parent ? "<-" : "",
    573 	    eclk_parent ? eclk_parent->base.name : "",
    574 	    type, clk_get_rate(&eclk->base));
    575 }
    576 
    577 static struct clk *
    578 exynos5422_clock_decode(device_t dev, const void *data, size_t len)
    579 {
    580 	struct exynos_clk *eclk;
    581 
    582 	/* #clock-cells should be 1 */
    583 	if (len != 4) {
    584 		return NULL;
    585 	}
    586 
    587 	const u_int clock_id = be32dec(data);
    588 
    589 	eclk = exynos5422_clock_find_by_id(clock_id);
    590 	if (eclk)
    591 		return &eclk->base;
    592 
    593 	return NULL;
    594 }
    595 
    596 static u_int
    597 exynos5422_clock_get_rate_pll(struct exynos5422_clock_softc *sc,
    598     struct exynos_clk *eclk)
    599 {
    600 	struct exynos_pll_clk *epll = &eclk->u.pll;
    601 	struct exynos_clk *clk_parent;
    602 
    603 	KASSERT(eclk->type == EXYNOS_CLK_PLL);
    604 
    605 	clk_parent = exynos5422_clock_find(eclk->parent);
    606 	KASSERT(clk_parent != NULL);
    607 	const u_int rate_parent = exynos5422_clock_get_rate(sc,
    608 	    &clk_parent->base);
    609 
    610 	const uint32_t v = CLOCK_READ(sc, epll->con0_reg);
    611 
    612 	return PLL_FREQ(rate_parent, v);
    613 }
    614 
    615 static int
    616 exynos5422_clock_set_rate_pll(struct exynos5422_clock_softc *sc,
    617     struct exynos_clk *eclk, u_int rate)
    618 {
    619 	/* TODO */
    620 	return EOPNOTSUPP;
    621 }
    622 
    623 static int
    624 exynos5422_clock_set_parent_mux(struct exynos5422_clock_softc *sc,
    625     struct exynos_clk *eclk, struct exynos_clk *eclk_parent)
    626 {
    627 	struct exynos_mux_clk *emux = &eclk->u.mux;
    628 	const char *pname = eclk_parent->base.name;
    629 	u_int sel;
    630 
    631 	KASSERT(eclk->type == EXYNOS_CLK_MUX);
    632 
    633 	for (sel = 0; sel < emux->nparents; sel++) {
    634 		if (strcmp(pname, emux->parents[sel]) == 0) {
    635 			break;
    636 		}
    637 	}
    638 	if (sel == emux->nparents) {
    639 		return EINVAL;
    640 	}
    641 
    642 	uint32_t v = CLOCK_READ(sc, emux->reg);
    643 	v &= ~emux->bits;
    644 	v |= __SHIFTIN(sel, emux->bits);
    645 	CLOCK_WRITE(sc, emux->reg, v);
    646 
    647 	return 0;
    648 }
    649 
    650 static struct exynos_clk *
    651 exynos5422_clock_get_parent_mux(struct exynos5422_clock_softc *sc,
    652     struct exynos_clk *eclk)
    653 {
    654 	struct exynos_mux_clk *emux = &eclk->u.mux;
    655 
    656 	KASSERT(eclk->type == EXYNOS_CLK_MUX);
    657 
    658 	const uint32_t v = CLOCK_READ(sc, emux->reg);
    659 	const u_int sel = __SHIFTOUT(v, emux->bits);
    660 
    661 	KASSERT(sel < emux->nparents);
    662 
    663 	return exynos5422_clock_find(emux->parents[sel]);
    664 }
    665 
    666 static u_int
    667 exynos5422_clock_get_rate_div(struct exynos5422_clock_softc *sc,
    668     struct exynos_clk *eclk)
    669 {
    670 	struct exynos_div_clk *ediv = &eclk->u.div;
    671 	struct clk *clk_parent;
    672 
    673 	KASSERT(eclk->type == EXYNOS_CLK_DIV);
    674 
    675 	clk_parent = exynos5422_clock_get_parent(sc, &eclk->base);
    676 	const u_int parent_rate = exynos5422_clock_get_rate(sc, clk_parent);
    677 
    678 	const uint32_t v = CLOCK_READ(sc, ediv->reg);
    679 	const u_int div = __SHIFTOUT(v, ediv->bits);
    680 
    681 	return parent_rate / (div + 1);
    682 }
    683 
    684 static int
    685 exynos5422_clock_set_rate_div(struct exynos5422_clock_softc *sc,
    686     struct exynos_clk *eclk, u_int rate)
    687 {
    688 	struct exynos_div_clk *ediv = &eclk->u.div;
    689 	struct clk *clk_parent;
    690 	int tmp_div, new_div = -1;
    691 	u_int tmp_rate;
    692 
    693 	KASSERT(eclk->type == EXYNOS_CLK_DIV);
    694 
    695 	clk_parent = exynos5422_clock_get_parent(sc, &eclk->base);
    696 	const u_int parent_rate = exynos5422_clock_get_rate(sc, clk_parent);
    697 
    698 	for (tmp_div = 0; tmp_div < popcount32(ediv->bits); tmp_div++) {
    699 		tmp_rate = parent_rate / (tmp_div + 1);
    700 		if (tmp_rate <= rate) {
    701 			new_div = tmp_div;
    702 			break;
    703 		}
    704 	}
    705 	if (new_div == -1)
    706 		return EINVAL;
    707 
    708 	uint32_t v = CLOCK_READ(sc, ediv->reg);
    709 	v &= ~ediv->bits;
    710 	v |= __SHIFTIN(new_div, ediv->bits);
    711 	CLOCK_WRITE(sc, ediv->reg, v);
    712 
    713 	return 0;
    714 }
    715 
    716 static int
    717 exynos5422_clock_enable_gate(struct exynos5422_clock_softc *sc,
    718     struct exynos_clk *eclk, bool enable)
    719 {
    720 	struct exynos_gate_clk *egate = &eclk->u.gate;
    721 
    722 	KASSERT(eclk->type == EXYNOS_CLK_GATE);
    723 
    724 	uint32_t v = CLOCK_READ(sc, egate->reg);
    725 	if (enable) {
    726 		v |= egate->bits;
    727 	} else {
    728 		v &= ~egate->bits;
    729 	}
    730 	CLOCK_WRITE(sc, egate->reg, v);
    731 
    732 	return 0;
    733 }
    734 
    735 /*
    736  * clk api
    737  */
    738 
    739 static struct clk *
    740 exynos5422_clock_get(void *priv, const char *name)
    741 {
    742 	struct exynos_clk *eclk;
    743 
    744 	eclk = exynos5422_clock_find(name);
    745 	if (eclk == NULL)
    746 		return NULL;
    747 
    748 	atomic_inc_uint(&eclk->refcnt);
    749 
    750 	return &eclk->base;
    751 }
    752 
    753 static void
    754 exynos5422_clock_put(void *priv, struct clk *clk)
    755 {
    756 	struct exynos_clk *eclk = (struct exynos_clk *)clk;
    757 
    758 	KASSERT(eclk->refcnt > 0);
    759 
    760 	atomic_dec_uint(&eclk->refcnt);
    761 }
    762 
    763 static u_int
    764 exynos5422_clock_get_rate(void *priv, struct clk *clk)
    765 {
    766 	struct exynos_clk *eclk = (struct exynos_clk *)clk;
    767 	struct clk *clk_parent;
    768 
    769 	switch (eclk->type) {
    770 	case EXYNOS_CLK_FIXED:
    771 		return eclk->u.fixed.rate;
    772 	case EXYNOS_CLK_PLL:
    773 		return exynos5422_clock_get_rate_pll(priv, eclk);
    774 	case EXYNOS_CLK_MUX:
    775 	case EXYNOS_CLK_GATE:
    776 		clk_parent = exynos5422_clock_get_parent(priv, clk);
    777 		return exynos5422_clock_get_rate(priv, clk_parent);
    778 	case EXYNOS_CLK_DIV:
    779 		return exynos5422_clock_get_rate_div(priv, eclk);
    780 	default:
    781 		panic("exynos5422: unknown eclk type %d", eclk->type);
    782 	}
    783 }
    784 
    785 static int
    786 exynos5422_clock_set_rate(void *priv, struct clk *clk, u_int rate)
    787 {
    788 	struct exynos_clk *eclk = (struct exynos_clk *)clk;
    789 
    790 	KASSERT((clk->flags & CLK_SET_RATE_PARENT) == 0);
    791 
    792 	switch (eclk->type) {
    793 	case EXYNOS_CLK_FIXED:
    794 		return EIO;
    795 	case EXYNOS_CLK_PLL:
    796 		return exynos5422_clock_set_rate_pll(priv, eclk, rate);
    797 	case EXYNOS_CLK_MUX:
    798 		return EIO;
    799 	case EXYNOS_CLK_DIV:
    800 		return exynos5422_clock_set_rate_div(priv, eclk, rate);
    801 	case EXYNOS_CLK_GATE:
    802 		return EINVAL;
    803 	default:
    804 		panic("exynos5422: unknown eclk type %d", eclk->type);
    805 	}
    806 }
    807 
    808 static int
    809 exynos5422_clock_enable(void *priv, struct clk *clk)
    810 {
    811 	struct exynos_clk *eclk = (struct exynos_clk *)clk;
    812 
    813 	switch (eclk->type) {
    814 	case EXYNOS_CLK_FIXED:
    815 		return 0;	/* always on */
    816 	case EXYNOS_CLK_PLL:
    817 		return 0;	/* XXX */
    818 	case EXYNOS_CLK_MUX:
    819 	case EXYNOS_CLK_DIV:
    820 		return 0;
    821 	case EXYNOS_CLK_GATE:
    822 		return exynos5422_clock_enable_gate(priv, eclk, true);
    823 	default:
    824 		panic("exynos5422: unknown eclk type %d", eclk->type);
    825 	}
    826 }
    827 
    828 static int
    829 exynos5422_clock_disable(void *priv, struct clk *clk)
    830 {
    831 	struct exynos_clk *eclk = (struct exynos_clk *)clk;
    832 
    833 	switch (eclk->type) {
    834 	case EXYNOS_CLK_FIXED:
    835 		return EINVAL;	/* always on */
    836 	case EXYNOS_CLK_PLL:
    837 		return EINVAL;	/* XXX */
    838 	case EXYNOS_CLK_MUX:
    839 	case EXYNOS_CLK_DIV:
    840 		return EINVAL;
    841 	case EXYNOS_CLK_GATE:
    842 		return exynos5422_clock_enable_gate(priv, eclk, false);
    843 	default:
    844 		panic("exynos5422: unknown eclk type %d", eclk->type);
    845 	}
    846 }
    847 
    848 static int
    849 exynos5422_clock_set_parent(void *priv, struct clk *clk, struct clk *clk_parent)
    850 {
    851 	struct exynos_clk *eclk = (struct exynos_clk *)clk;
    852 	struct exynos_clk *eclk_parent = (struct exynos_clk *)clk_parent;
    853 
    854 	switch (eclk->type) {
    855 	case EXYNOS_CLK_FIXED:
    856 	case EXYNOS_CLK_PLL:
    857 	case EXYNOS_CLK_DIV:
    858 	case EXYNOS_CLK_GATE:
    859 		return EINVAL;
    860 	case EXYNOS_CLK_MUX:
    861 		return exynos5422_clock_set_parent_mux(priv, eclk, eclk_parent);
    862 	default:
    863 		panic("exynos5422: unknown eclk type %d", eclk->type);
    864 	}
    865 }
    866 
    867 static struct clk *
    868 exynos5422_clock_get_parent(void *priv, struct clk *clk)
    869 {
    870 	struct exynos_clk *eclk = (struct exynos_clk *)clk;
    871 	struct exynos_clk *eclk_parent = NULL;
    872 
    873 	switch (eclk->type) {
    874 	case EXYNOS_CLK_FIXED:
    875 	case EXYNOS_CLK_PLL:
    876 	case EXYNOS_CLK_DIV:
    877 	case EXYNOS_CLK_GATE:
    878 		if (eclk->parent != NULL) {
    879 			eclk_parent = exynos5422_clock_find(eclk->parent);
    880 		}
    881 		break;
    882 	case EXYNOS_CLK_MUX:
    883 		eclk_parent = exynos5422_clock_get_parent_mux(priv, eclk);
    884 		break;
    885 	default:
    886 		panic("exynos5422: unknown eclk type %d", eclk->type);
    887 	}
    888 
    889 	return &eclk_parent->base;
    890 }
    891