Home | History | Annotate | Line # | Download | only in samsung
exynos5422_clock.c revision 1.4
      1 /* $NetBSD: exynos5422_clock.c,v 1.4 2015/12/26 22:57:09 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.4 2015/12/26 22:57:09 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 
    438 static void	exynos5422_clock_print_header(void);
    439 static void	exynos5422_clock_print(struct exynos5422_clock_softc *,
    440 		    struct exynos_clk *);
    441 
    442 CFATTACH_DECL_NEW(exynos5422_clock, sizeof(struct exynos5422_clock_softc),
    443 	exynos5422_clock_match, exynos5422_clock_attach, NULL, NULL);
    444 
    445 #define CLOCK_READ(sc, reg)		\
    446     bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
    447 #define CLOCK_WRITE(sc, reg, val)	\
    448     bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
    449 
    450 static int
    451 exynos5422_clock_match(device_t parent, cfdata_t cf, void *aux)
    452 {
    453 	const char * const compatible[] = { "samsung,exynos5800-clock", NULL };
    454 	struct fdt_attach_args * const faa = aux;
    455 
    456 	return of_match_compatible(faa->faa_phandle, compatible);
    457 }
    458 
    459 static void
    460 exynos5422_clock_attach(device_t parent, device_t self, void *aux)
    461 {
    462 	struct exynos5422_clock_softc * const sc = device_private(self);
    463 	struct fdt_attach_args * const faa = aux;
    464 	bus_addr_t addr;
    465 	bus_size_t size;
    466 	int error;
    467 
    468 	if (fdtbus_get_reg(faa->faa_phandle, 0, &addr, &size) != 0) {
    469 		aprint_error(": couldn't get registers\n");
    470 		return;
    471 	}
    472 
    473 	sc->sc_dev = self;
    474 	sc->sc_bst = faa->faa_bst;
    475 
    476 	error = bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh);
    477 	if (error) {
    478 		aprint_error(": couldn't map %#llx: %d",
    479 			     (uint64_t)addr, error);
    480 		return;
    481 	}
    482 
    483 	aprint_naive("\n");
    484 	aprint_normal(": Exynos5422 Clock Controller\n");
    485 
    486 	clk_backend_register("exynos5422", &exynos5422_clock_funcs, sc);
    487 
    488 	fdtbus_register_clock_controller(self, faa->faa_phandle,
    489 	    &exynos5422_car_fdtclock_funcs);
    490 
    491 	exynos5422_clock_print_header();
    492 	for (u_int n = 0; n < __arraycount(exynos5422_clocks); n++) {
    493 		exynos5422_clock_print(sc, &exynos5422_clocks[n]);
    494 	}
    495 }
    496 
    497 static struct exynos_clk *
    498 exynos5422_clock_find(const char *name)
    499 {
    500 	u_int n;
    501 
    502 	for (n = 0; n < __arraycount(exynos5422_clocks); n++) {
    503 		if (strcmp(exynos5422_clocks[n].base.name, name) == 0) {
    504 			return &exynos5422_clocks[n];
    505 		}
    506 	}
    507 
    508 	return NULL;
    509 }
    510 
    511 static struct exynos_clk *
    512 exynos5422_clock_find_by_id(u_int clock_id)
    513 {
    514 	u_int n;
    515 
    516 	for (n = 0; n < __arraycount(exynos5422_clock_ids); n++) {
    517 		if (exynos5422_clock_ids[n].id == clock_id) {
    518 			const char *name = exynos5422_clock_ids[n].name;
    519 			return exynos5422_clock_find(name);
    520 		}
    521 	}
    522 
    523 	return NULL;
    524 }
    525 
    526 static void
    527 exynos5422_clock_print_header(void)
    528 {
    529 	printf("  %-10s %2s %-10s %-5s %10s\n",
    530 	    "clock", "", "parent", "type", "rate");
    531 	printf("  %-10s %2s %-10s %-5s %10s\n",
    532 	    "=====", "", "======", "====", "====");
    533 }
    534 
    535 static void
    536 exynos5422_clock_print(struct exynos5422_clock_softc *sc,
    537     struct exynos_clk *eclk)
    538 {
    539 	struct exynos_clk *eclk_parent;
    540 	struct clk *clk_parent;
    541 	const char *type = "?";
    542 
    543 	switch (eclk->type) {
    544 	case EXYNOS_CLK_FIXED:
    545 		type = "fixed";
    546 		break;
    547 	case EXYNOS_CLK_PLL:
    548 		type = "pll";
    549 		break;
    550 	case EXYNOS_CLK_MUX:
    551 		type = "mux";
    552 		break;
    553 	case EXYNOS_CLK_DIV:
    554 		type = "div";
    555 		break;
    556 	case EXYNOS_CLK_GATE:
    557 		type = "gate";
    558 		break;
    559 	}
    560 
    561 	clk_parent = exynos5422_clock_get_parent(sc, &eclk->base);
    562 	eclk_parent = (struct exynos_clk *)clk_parent;
    563 
    564 	printf("  %-10s %2s %-10s %-5s %10d Hz\n",
    565 	    eclk->base.name,
    566 	    eclk_parent ? "<-" : "",
    567 	    eclk_parent ? eclk_parent->base.name : "",
    568 	    type, clk_get_rate(&eclk->base));
    569 }
    570 
    571 static struct clk *
    572 exynos5422_clock_decode(device_t dev, const void *data, size_t len)
    573 {
    574 	struct exynos_clk *eclk;
    575 
    576 	/* #clock-cells should be 1 */
    577 	if (len != 4) {
    578 		return NULL;
    579 	}
    580 
    581 	const u_int clock_id = be32dec(data);
    582 
    583 	eclk = exynos5422_clock_find_by_id(clock_id);
    584 	if (eclk)
    585 		return &eclk->base;
    586 
    587 	return NULL;
    588 }
    589 
    590 static u_int
    591 exynos5422_clock_get_rate_pll(struct exynos5422_clock_softc *sc,
    592     struct exynos_clk *eclk)
    593 {
    594 	struct exynos_pll_clk *epll = &eclk->u.pll;
    595 	struct exynos_clk *clk_parent;
    596 
    597 	KASSERT(eclk->type == EXYNOS_CLK_PLL);
    598 
    599 	clk_parent = exynos5422_clock_find(eclk->parent);
    600 	KASSERT(clk_parent != NULL);
    601 	const u_int rate_parent = exynos5422_clock_get_rate(sc,
    602 	    &clk_parent->base);
    603 
    604 	const uint32_t v = CLOCK_READ(sc, epll->con0_reg);
    605 
    606 	return PLL_FREQ(rate_parent, v);
    607 }
    608 
    609 static int
    610 exynos5422_clock_set_rate_pll(struct exynos5422_clock_softc *sc,
    611     struct exynos_clk *eclk, u_int rate)
    612 {
    613 	/* TODO */
    614 	return EOPNOTSUPP;
    615 }
    616 
    617 static int
    618 exynos5422_clock_set_parent_mux(struct exynos5422_clock_softc *sc,
    619     struct exynos_clk *eclk, struct exynos_clk *eclk_parent)
    620 {
    621 	struct exynos_mux_clk *emux = &eclk->u.mux;
    622 	const char *pname = eclk_parent->base.name;
    623 	u_int sel;
    624 
    625 	KASSERT(eclk->type == EXYNOS_CLK_MUX);
    626 
    627 	for (sel = 0; sel < emux->nparents; sel++) {
    628 		if (strcmp(pname, emux->parents[sel]) == 0) {
    629 			break;
    630 		}
    631 	}
    632 	if (sel == emux->nparents) {
    633 		return EINVAL;
    634 	}
    635 
    636 	uint32_t v = CLOCK_READ(sc, emux->reg);
    637 	v &= ~emux->bits;
    638 	v |= __SHIFTIN(sel, emux->bits);
    639 	CLOCK_WRITE(sc, emux->reg, v);
    640 
    641 	return 0;
    642 }
    643 
    644 static struct exynos_clk *
    645 exynos5422_clock_get_parent_mux(struct exynos5422_clock_softc *sc,
    646     struct exynos_clk *eclk)
    647 {
    648 	struct exynos_mux_clk *emux = &eclk->u.mux;
    649 
    650 	KASSERT(eclk->type == EXYNOS_CLK_MUX);
    651 
    652 	const uint32_t v = CLOCK_READ(sc, emux->reg);
    653 	const u_int sel = __SHIFTOUT(v, emux->bits);
    654 
    655 	KASSERT(sel < emux->nparents);
    656 
    657 	return exynos5422_clock_find(emux->parents[sel]);
    658 }
    659 
    660 static u_int
    661 exynos5422_clock_get_rate_div(struct exynos5422_clock_softc *sc,
    662     struct exynos_clk *eclk)
    663 {
    664 	struct exynos_div_clk *ediv = &eclk->u.div;
    665 	struct clk *clk_parent;
    666 
    667 	KASSERT(eclk->type == EXYNOS_CLK_DIV);
    668 
    669 	clk_parent = exynos5422_clock_get_parent(sc, &eclk->base);
    670 	const u_int parent_rate = exynos5422_clock_get_rate(sc, clk_parent);
    671 
    672 	const uint32_t v = CLOCK_READ(sc, ediv->reg);
    673 	const u_int div = __SHIFTOUT(v, ediv->bits);
    674 
    675 	return parent_rate / (div + 1);
    676 }
    677 
    678 static int
    679 exynos5422_clock_set_rate_div(struct exynos5422_clock_softc *sc,
    680     struct exynos_clk *eclk, u_int rate)
    681 {
    682 	struct exynos_div_clk *ediv = &eclk->u.div;
    683 	struct clk *clk_parent;
    684 	int tmp_div, new_div = -1;
    685 	u_int tmp_rate;
    686 
    687 	KASSERT(eclk->type == EXYNOS_CLK_DIV);
    688 
    689 	clk_parent = exynos5422_clock_get_parent(sc, &eclk->base);
    690 	const u_int parent_rate = exynos5422_clock_get_rate(sc, clk_parent);
    691 
    692 	for (tmp_div = 0; tmp_div < popcount32(ediv->bits); tmp_div++) {
    693 		tmp_rate = parent_rate / (tmp_div + 1);
    694 		if (tmp_rate <= rate) {
    695 			new_div = tmp_div;
    696 			break;
    697 		}
    698 	}
    699 	if (new_div == -1)
    700 		return EINVAL;
    701 
    702 	uint32_t v = CLOCK_READ(sc, ediv->reg);
    703 	v &= ~ediv->bits;
    704 	v |= __SHIFTIN(new_div, ediv->bits);
    705 	CLOCK_WRITE(sc, ediv->reg, v);
    706 
    707 	return 0;
    708 }
    709 
    710 static int
    711 exynos5422_clock_enable_gate(struct exynos5422_clock_softc *sc,
    712     struct exynos_clk *eclk, bool enable)
    713 {
    714 	struct exynos_gate_clk *egate = &eclk->u.gate;
    715 
    716 	KASSERT(eclk->type == EXYNOS_CLK_GATE);
    717 
    718 	uint32_t v = CLOCK_READ(sc, egate->reg);
    719 	if (enable) {
    720 		v |= egate->bits;
    721 	} else {
    722 		v &= ~egate->bits;
    723 	}
    724 	CLOCK_WRITE(sc, egate->reg, v);
    725 
    726 	return 0;
    727 }
    728 
    729 /*
    730  * clk api
    731  */
    732 
    733 static struct clk *
    734 exynos5422_clock_get(void *priv, const char *name)
    735 {
    736 	struct exynos_clk *eclk;
    737 
    738 	eclk = exynos5422_clock_find(name);
    739 	if (eclk == NULL)
    740 		return NULL;
    741 
    742 	atomic_inc_uint(&eclk->refcnt);
    743 
    744 	return &eclk->base;
    745 }
    746 
    747 static void
    748 exynos5422_clock_put(void *priv, struct clk *clk)
    749 {
    750 	struct exynos_clk *eclk = (struct exynos_clk *)clk;
    751 
    752 	KASSERT(eclk->refcnt > 0);
    753 
    754 	atomic_dec_uint(&eclk->refcnt);
    755 }
    756 
    757 static u_int
    758 exynos5422_clock_get_rate(void *priv, struct clk *clk)
    759 {
    760 	struct exynos_clk *eclk = (struct exynos_clk *)clk;
    761 	struct clk *clk_parent;
    762 
    763 	switch (eclk->type) {
    764 	case EXYNOS_CLK_FIXED:
    765 		return eclk->u.fixed.rate;
    766 	case EXYNOS_CLK_PLL:
    767 		return exynos5422_clock_get_rate_pll(priv, eclk);
    768 	case EXYNOS_CLK_MUX:
    769 	case EXYNOS_CLK_GATE:
    770 		clk_parent = exynos5422_clock_get_parent(priv, clk);
    771 		return exynos5422_clock_get_rate(priv, clk_parent);
    772 	case EXYNOS_CLK_DIV:
    773 		return exynos5422_clock_get_rate_div(priv, eclk);
    774 	default:
    775 		panic("exynos5422: unknown eclk type %d", eclk->type);
    776 	}
    777 }
    778 
    779 static int
    780 exynos5422_clock_set_rate(void *priv, struct clk *clk, u_int rate)
    781 {
    782 	struct exynos_clk *eclk = (struct exynos_clk *)clk;
    783 
    784 	KASSERT((clk->flags & CLK_SET_RATE_PARENT) == 0);
    785 
    786 	switch (eclk->type) {
    787 	case EXYNOS_CLK_FIXED:
    788 		return EIO;
    789 	case EXYNOS_CLK_PLL:
    790 		return exynos5422_clock_set_rate_pll(priv, eclk, rate);
    791 	case EXYNOS_CLK_MUX:
    792 		return EIO;
    793 	case EXYNOS_CLK_DIV:
    794 		return exynos5422_clock_set_rate_div(priv, eclk, rate);
    795 	case EXYNOS_CLK_GATE:
    796 		return EINVAL;
    797 	default:
    798 		panic("exynos5422: unknown eclk type %d", eclk->type);
    799 	}
    800 }
    801 
    802 static int
    803 exynos5422_clock_enable(void *priv, struct clk *clk)
    804 {
    805 	struct exynos_clk *eclk = (struct exynos_clk *)clk;
    806 
    807 	switch (eclk->type) {
    808 	case EXYNOS_CLK_FIXED:
    809 		return 0;	/* always on */
    810 	case EXYNOS_CLK_PLL:
    811 		return 0;	/* XXX */
    812 	case EXYNOS_CLK_MUX:
    813 	case EXYNOS_CLK_DIV:
    814 		return 0;
    815 	case EXYNOS_CLK_GATE:
    816 		return exynos5422_clock_enable_gate(priv, eclk, true);
    817 	default:
    818 		panic("exynos5422: unknown eclk type %d", eclk->type);
    819 	}
    820 }
    821 
    822 static int
    823 exynos5422_clock_disable(void *priv, struct clk *clk)
    824 {
    825 	struct exynos_clk *eclk = (struct exynos_clk *)clk;
    826 
    827 	switch (eclk->type) {
    828 	case EXYNOS_CLK_FIXED:
    829 		return EINVAL;	/* always on */
    830 	case EXYNOS_CLK_PLL:
    831 		return EINVAL;	/* XXX */
    832 	case EXYNOS_CLK_MUX:
    833 	case EXYNOS_CLK_DIV:
    834 		return EINVAL;
    835 	case EXYNOS_CLK_GATE:
    836 		return exynos5422_clock_enable_gate(priv, eclk, false);
    837 	default:
    838 		panic("exynos5422: unknown eclk type %d", eclk->type);
    839 	}
    840 }
    841 
    842 static int
    843 exynos5422_clock_set_parent(void *priv, struct clk *clk, struct clk *clk_parent)
    844 {
    845 	struct exynos_clk *eclk = (struct exynos_clk *)clk;
    846 	struct exynos_clk *eclk_parent = (struct exynos_clk *)clk_parent;
    847 
    848 	switch (eclk->type) {
    849 	case EXYNOS_CLK_FIXED:
    850 	case EXYNOS_CLK_PLL:
    851 	case EXYNOS_CLK_DIV:
    852 	case EXYNOS_CLK_GATE:
    853 		return EINVAL;
    854 	case EXYNOS_CLK_MUX:
    855 		return exynos5422_clock_set_parent_mux(priv, eclk, eclk_parent);
    856 	default:
    857 		panic("exynos5422: unknown eclk type %d", eclk->type);
    858 	}
    859 }
    860 
    861 static struct clk *
    862 exynos5422_clock_get_parent(void *priv, struct clk *clk)
    863 {
    864 	struct exynos_clk *eclk = (struct exynos_clk *)clk;
    865 	struct exynos_clk *eclk_parent = NULL;
    866 
    867 	switch (eclk->type) {
    868 	case EXYNOS_CLK_FIXED:
    869 	case EXYNOS_CLK_PLL:
    870 	case EXYNOS_CLK_DIV:
    871 	case EXYNOS_CLK_GATE:
    872 		if (eclk->parent != NULL) {
    873 			eclk_parent = exynos5422_clock_find(eclk->parent);
    874 		}
    875 		break;
    876 	case EXYNOS_CLK_MUX:
    877 		eclk_parent = exynos5422_clock_get_parent_mux(priv, eclk);
    878 		break;
    879 	default:
    880 		panic("exynos5422: unknown eclk type %d", eclk->type);
    881 	}
    882 
    883 	return &eclk_parent->base;
    884 }
    885