Home | History | Annotate | Line # | Download | only in nxp
      1 /*	$NetBSD: imx6_ccm.c,v 1.7 2026/11/23 02:27:40 hkenken Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2010-2012, 2014  Genetec Corporation.  All rights reserved.
      5  * Written by Hashimoto Kenichi for Genetec Corporation.
      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 GENETEC CORPORATION ``AS IS'' AND
     17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL GENETEC CORPORATION
     20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  * POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 /*
     29  * Clock Controller Module (CCM) for i.MX6
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: imx6_ccm.c,v 1.7 2026/11/23 02:27:40 hkenken Exp $");
     34 
     35 #include "opt_imx.h"
     36 #include "opt_cputypes.h"
     37 
     38 #include "locators.h"
     39 
     40 #include <sys/types.h>
     41 #include <sys/time.h>
     42 #include <sys/bus.h>
     43 #include <sys/device.h>
     44 #include <sys/sysctl.h>
     45 #include <sys/cpufreq.h>
     46 #include <sys/param.h>
     47 
     48 #include <machine/cpu.h>
     49 
     50 #include <arm/nxp/imx6_ccmvar.h>
     51 #include <arm/nxp/imx6_ccmreg.h>
     52 
     53 #include <dev/clk/clk_backend.h>
     54 
     55 static void imxccm_init_clocks(struct imx6ccm_softc *,
     56 			       struct imxccm_init_parent *);
     57 static struct clk *imxccm_clk_get(void *, const char *);
     58 static void imxccm_clk_put(void *, struct clk *);
     59 static u_int imxccm_clk_get_rate(void *, struct clk *);
     60 static int imxccm_clk_set_rate(void *, struct clk *, u_int);
     61 static int imxccm_clk_enable(void *, struct clk *);
     62 static int imxccm_clk_disable(void *, struct clk *);
     63 static int imxccm_clk_set_parent(void *, struct clk *, struct clk *);
     64 static struct clk *imxccm_clk_get_parent(void *, struct clk *);
     65 
     66 static const struct clk_funcs imxccm_clk_funcs = {
     67 	.get = imxccm_clk_get,
     68 	.put = imxccm_clk_put,
     69 	.get_rate = imxccm_clk_get_rate,
     70 	.set_rate = imxccm_clk_set_rate,
     71 	.enable = imxccm_clk_enable,
     72 	.disable = imxccm_clk_disable,
     73 	.set_parent = imxccm_clk_set_parent,
     74 	.get_parent = imxccm_clk_get_parent,
     75 };
     76 
     77 void
     78 imx6ccm_attach_common(device_t self, struct imx6_clk *imx6_clks, int size,
     79     struct imxccm_init_parent *imxccm_init_parents)
     80 {
     81 	struct imx6ccm_softc * const sc = device_private(self);
     82 
     83 	sc->sc_dev = self;
     84 	sc->sc_imx6_clks = imx6_clks;
     85 	sc->sc_imx6_clksize = size;
     86 
     87 	sc->sc_clkdom.name = device_xname(self);
     88 	sc->sc_clkdom.funcs = &imxccm_clk_funcs;
     89 	sc->sc_clkdom.priv = sc;
     90 	for (u_int n = 0; n < size; n++) {
     91 		imx6_clks[n].base.domain = &sc->sc_clkdom;
     92 		clk_attach(&imx6_clks[n].base);
     93 	}
     94 
     95 	imxccm_init_clocks(sc, imxccm_init_parents);
     96 
     97 	for (int n = 0; n < size; n++) {
     98 		struct clk *clk = &imx6_clks[n].base;
     99 		struct clk *clk_parent = clk_get_parent(clk);
    100 		const char *parent_str = clk_parent ? clk_parent->name : "none";
    101 
    102 		aprint_verbose_dev(self, "%s (%s) : %u Hz\n", clk->name,
    103 		    parent_str, clk_get_rate(clk));
    104 	}
    105 }
    106 
    107 struct clk *
    108 imx6_get_clock(struct imx6ccm_softc *sc, const char *name)
    109 {
    110 	struct imx6_clk *iclk;
    111 	iclk = imx6_clk_find(sc, name);
    112 
    113 	if (iclk == NULL)
    114 		return NULL;
    115 
    116 	return &iclk->base;
    117 }
    118 
    119 struct imx6_clk *
    120 imx6_clk_find(struct imx6ccm_softc *sc, const char *name)
    121 {
    122 	if (name == NULL)
    123 		return NULL;
    124 
    125 	for (int n = 0; n < sc->sc_imx6_clksize; n++) {
    126 		if (strcmp(sc->sc_imx6_clks[n].base.name, name) == 0)
    127 			return &sc->sc_imx6_clks[n];
    128 	}
    129 
    130 	return NULL;
    131 }
    132 
    133 static void
    134 imxccm_init_clocks(struct imx6ccm_softc *sc,
    135     struct imxccm_init_parent *imxccm_init_parents)
    136 {
    137 	struct clk *clk;
    138 	struct clk *clk_parent;
    139 
    140 	for (u_int n = 0; imxccm_init_parents[n].clock != NULL; n++) {
    141 		clk = clk_get(&sc->sc_clkdom, imxccm_init_parents[n].clock);
    142 		KASSERT(clk != NULL);
    143 		clk_parent = clk_get(&sc->sc_clkdom, imxccm_init_parents[n].parent);
    144 		KASSERT(clk_parent != NULL);
    145 
    146 		int error = clk_set_parent(clk, clk_parent);
    147 		if (error) {
    148 			aprint_error_dev(sc->sc_dev,
    149 			    "couldn't set '%s' parent to '%s': %d\n",
    150 			    clk->name, clk_parent->name, error);
    151 		}
    152 		clk_put(clk_parent);
    153 		clk_put(clk);
    154 	}
    155 }
    156 
    157 static u_int
    158 imxccm_clk_get_rate_pll_generic(struct imx6ccm_softc *sc, struct imx6_clk *iclk,
    159     const u_int rate_parent)
    160 {
    161 	struct imx6_clk_pll *pll = &iclk->clk.pll;
    162 	uint64_t freq = rate_parent;
    163 
    164 	KASSERT((pll->type == IMX6_CLK_PLL_GENERIC) ||
    165 	    (pll->type == IMX6_CLK_PLL_USB));
    166 
    167 	uint32_t v = bus_space_read_4(sc->sc_iot, sc->sc_ioh_analog, pll->reg);
    168 	uint32_t div = __SHIFTOUT(v, pll->mask);
    169 
    170 	return freq * ((div == 1) ? 22 : 20);
    171 }
    172 
    173 static u_int
    174 imxccm_clk_get_rate_pll_sys(struct imx6ccm_softc *sc, struct imx6_clk *iclk,
    175     const u_int rate_parent)
    176 {
    177 	struct imx6_clk_pll *pll = &iclk->clk.pll;
    178 	uint64_t freq = rate_parent;
    179 
    180 	KASSERT(pll->type == IMX6_CLK_PLL_SYS);
    181 
    182 	uint32_t v = bus_space_read_4(sc->sc_iot, sc->sc_ioh_analog, pll->reg);
    183 	uint32_t div = __SHIFTOUT(v, pll->mask);
    184 
    185 	return freq * div / 2;
    186 }
    187 
    188 #define PLL_AUDIO_VIDEO_NUM_OFFSET	0x10
    189 #define PLL_AUDIO_VIDEO_DENOM_OFFSET	0x20
    190 
    191 static u_int
    192 imxccm_clk_get_rate_pll_audio_video(struct imx6ccm_softc *sc,
    193     struct imx6_clk *iclk, const u_int rate_parent)
    194 {
    195 	struct imx6_clk_pll *pll = &iclk->clk.pll;
    196 	uint64_t freq = rate_parent;
    197 
    198 	KASSERT(pll->type == IMX6_CLK_PLL_AUDIO_VIDEO);
    199 
    200 	uint32_t v = bus_space_read_4(sc->sc_iot, sc->sc_ioh_analog, pll->reg);
    201 	uint32_t div = __SHIFTOUT(v, pll->mask);
    202 	uint32_t num = bus_space_read_4(sc->sc_iot, sc->sc_ioh_analog,
    203 	    pll->reg + PLL_AUDIO_VIDEO_NUM_OFFSET);
    204 	uint32_t denom = bus_space_read_4(sc->sc_iot, sc->sc_ioh_analog,
    205 	    pll->reg + PLL_AUDIO_VIDEO_DENOM_OFFSET);
    206 
    207 	uint64_t tmp = freq * num / denom;
    208 
    209 	return freq * div + tmp;
    210 }
    211 
    212 static u_int
    213 imxccm_clk_get_rate_pll_enet(struct imx6ccm_softc *sc,
    214     struct imx6_clk *iclk, const u_int rate_parent)
    215 {
    216 	struct imx6_clk_pll *pll = &iclk->clk.pll;
    217 
    218 	KASSERT(pll->type == IMX6_CLK_PLL_ENET);
    219 
    220 	return pll->ref;
    221 }
    222 
    223 static u_int
    224 imxccm_clk_get_rate_fixed_factor(struct imx6ccm_softc *sc, struct imx6_clk *iclk)
    225 {
    226 	struct imx6_clk_fixed_factor *fixed_factor = &iclk->clk.fixed_factor;
    227 	struct imx6_clk *parent;
    228 
    229 	KASSERT(iclk->type == IMX6_CLK_FIXED_FACTOR);
    230 
    231 	parent = imx6_clk_find(sc, iclk->parent);
    232 	KASSERT(parent != NULL);
    233 
    234 	uint64_t rate_parent = imxccm_clk_get_rate(sc, &parent->base);
    235 
    236 	return rate_parent * fixed_factor->mult / fixed_factor->div;
    237 }
    238 
    239 static u_int
    240 imxccm_clk_get_rate_pll(struct imx6ccm_softc *sc, struct imx6_clk *iclk)
    241 {
    242 	struct imx6_clk_pll *pll = &iclk->clk.pll;
    243 	struct imx6_clk *parent;
    244 
    245 	KASSERT(iclk->type == IMX6_CLK_PLL);
    246 
    247 	parent = imx6_clk_find(sc, iclk->parent);
    248 	KASSERT(parent != NULL);
    249 
    250 	uint64_t rate_parent = imxccm_clk_get_rate(sc, &parent->base);
    251 
    252 	switch(pll->type) {
    253 	case IMX6_CLK_PLL_GENERIC:
    254 		return imxccm_clk_get_rate_pll_generic(sc, iclk, rate_parent);
    255 	case IMX6_CLK_PLL_SYS:
    256 		return imxccm_clk_get_rate_pll_sys(sc, iclk, rate_parent);
    257 	case IMX6_CLK_PLL_USB:
    258 		return imxccm_clk_get_rate_pll_generic(sc, iclk, rate_parent);
    259 	case IMX6_CLK_PLL_AUDIO_VIDEO:
    260 		return imxccm_clk_get_rate_pll_audio_video(sc, iclk, rate_parent);
    261 	case IMX6_CLK_PLL_ENET:
    262 		return imxccm_clk_get_rate_pll_enet(sc, iclk, rate_parent);
    263 	default:
    264 		panic("imx6: unknown pll type %d", iclk->type);
    265 	}
    266 }
    267 
    268 static u_int
    269 imxccm_clk_get_rate_div(struct imx6ccm_softc *sc, struct imx6_clk *iclk)
    270 {
    271 	struct imx6_clk_div *div = &iclk->clk.div;
    272 	struct imx6_clk *parent;
    273 
    274 	KASSERT(iclk->type == IMX6_CLK_DIV);
    275 
    276 	parent = imx6_clk_find(sc, iclk->parent);
    277 	KASSERT(parent != NULL);
    278 
    279 	u_int rate = imxccm_clk_get_rate(sc, &parent->base);
    280 
    281 	bus_space_handle_t ioh;
    282 	if (div->base == IMX6_CLK_REG_CCM_ANALOG)
    283 		ioh = sc->sc_ioh_analog;
    284 	else
    285 		ioh = sc->sc_ioh;
    286 
    287 	uint32_t v = bus_space_read_4(sc->sc_iot, ioh, div->reg);
    288 	uint32_t n = __SHIFTOUT(v, div->mask);
    289 
    290 	if (div->type == IMX6_CLK_DIV_TABLE) {
    291 		KASSERT(div->tbl != NULL);
    292 
    293 		for (int i = 0; div->tbl[i] != 0; i++)
    294 			if (i == n)
    295 				rate /= div->tbl[i];
    296 	} else {
    297 		rate /= n + 1;
    298 	}
    299 
    300 	return rate;
    301 }
    302 
    303 static u_int
    304 imxccm_clk_get_rate_pfd(struct imx6ccm_softc *sc, struct imx6_clk *iclk)
    305 {
    306 	struct imx6_clk_pfd *pfd = &iclk->clk.pfd;
    307 	struct imx6_clk *parent;
    308 
    309 	KASSERT(iclk->type == IMX6_CLK_PFD);
    310 
    311 	parent = imx6_clk_find(sc, iclk->parent);
    312 	KASSERT(parent != NULL);
    313 
    314 	uint64_t rate_parent = imxccm_clk_get_rate(sc, &parent->base);
    315 
    316 	uint32_t v = bus_space_read_4(sc->sc_iot, sc->sc_ioh_analog, pfd->reg);
    317 	uint32_t n = __SHIFTOUT(v, __BITS(5, 0) << (pfd->index * 8));
    318 
    319 	KASSERT(n != 0);
    320 
    321 	return (rate_parent * 18) / n;
    322 }
    323 
    324 static int
    325 imxccm_clk_wait(struct imx6ccm_softc *sc, enum imx6_clk_reg base,
    326     uint32_t busy_reg, uint32_t busy_mask)
    327 {
    328 	int tout = 0;
    329 
    330 	KASSERT(busy_reg != 0);
    331 	KASSERT(busy_mask != 0);
    332 
    333 	bus_space_handle_t ioh;
    334 	if (base == IMX6_CLK_REG_CCM_ANALOG)
    335 		ioh = sc->sc_ioh_analog;
    336 	else
    337 		ioh = sc->sc_ioh;
    338 
    339 	while (bus_space_read_4(sc->sc_iot, ioh, busy_reg) & busy_mask) {
    340 		if (++tout >= 1000)
    341 			return ETIMEDOUT;
    342 		delay(10);
    343 	}
    344 
    345 	return 0;
    346 }
    347 
    348 static int
    349 imxccm_clk_set_parent_mux(struct imx6ccm_softc *sc,
    350     struct imx6_clk *iclk, struct clk *parent)
    351 {
    352 	struct imx6_clk_mux *mux = &iclk->clk.mux;
    353 	const char *pname = parent->name;
    354 	u_int sel;
    355 	int ret = 0;
    356 
    357 	KASSERT(iclk->type == IMX6_CLK_MUX);
    358 
    359 	for (sel = 0; sel < mux->nparents; sel++)
    360 		if (strcmp(pname, mux->parents[sel]) == 0)
    361 			break;
    362 
    363 	if (sel == mux->nparents)
    364 		return EINVAL;
    365 
    366 	bus_space_handle_t ioh;
    367 	if (mux->base == IMX6_CLK_REG_CCM_ANALOG)
    368 		ioh = sc->sc_ioh_analog;
    369 	else
    370 		ioh = sc->sc_ioh;
    371 
    372 	uint32_t v = bus_space_read_4(sc->sc_iot, ioh, mux->reg);
    373 	v &= ~mux->mask;
    374 	v |= __SHIFTIN(sel, mux->mask);
    375 
    376 	bus_space_write_4(sc->sc_iot, ioh, mux->reg, v);
    377 
    378 	iclk->parent = pname;
    379 
    380 	if (mux->type == IMX6_CLK_MUX_BUSY)
    381 		ret = imxccm_clk_wait(sc, mux->base, mux->busy_reg, mux->busy_mask);
    382 
    383 	return ret;
    384 }
    385 
    386 static struct imx6_clk *
    387 imxccm_clk_get_parent_mux(struct imx6ccm_softc *sc, struct imx6_clk *iclk)
    388 {
    389 	struct imx6_clk_mux *mux = &iclk->clk.mux;
    390 
    391 	KASSERT(iclk->type == IMX6_CLK_MUX);
    392 
    393 	bus_space_handle_t ioh;
    394 	if (mux->base == IMX6_CLK_REG_CCM_ANALOG)
    395 		ioh = sc->sc_ioh_analog;
    396 	else
    397 		ioh = sc->sc_ioh;
    398 
    399 	uint32_t v = bus_space_read_4(sc->sc_iot, ioh, mux->reg);
    400 	u_int sel = __SHIFTOUT(v, mux->mask);
    401 
    402 	KASSERTMSG(sel < mux->nparents, "mux %s sel %d nparents %d",
    403 	    iclk->base.name, sel, mux->nparents);
    404 
    405 	iclk->parent = mux->parents[sel];
    406 
    407 	return imx6_clk_find(sc, iclk->parent);
    408 }
    409 
    410 static int
    411 imxccm_clk_set_rate_pll(struct imx6ccm_softc *sc,
    412     struct imx6_clk *iclk, u_int rate)
    413 {
    414 	/* ToDo */
    415 
    416 	return EOPNOTSUPP;
    417 }
    418 
    419 static int
    420 imxccm_clk_set_rate_div(struct imx6ccm_softc *sc,
    421     struct imx6_clk *iclk, u_int rate)
    422 {
    423 	struct imx6_clk_div *div = &iclk->clk.div;
    424 	struct imx6_clk *parent;
    425 	int ret = 0;
    426 
    427 	KASSERT(iclk->type == IMX6_CLK_DIV);
    428 
    429 	parent = imx6_clk_find(sc, iclk->parent);
    430 	KASSERT(parent != NULL);
    431 
    432 	u_int rate_parent = imxccm_clk_get_rate(sc, &parent->base);
    433 	u_int divider = uimax(1, rate_parent / rate);
    434 
    435 	bus_space_handle_t ioh;
    436 	if (div->base == IMX6_CLK_REG_CCM_ANALOG)
    437 		ioh = sc->sc_ioh_analog;
    438 	else
    439 		ioh = sc->sc_ioh;
    440 
    441 	uint32_t v = bus_space_read_4(sc->sc_iot, ioh, div->reg);
    442 	v &= ~div->mask;
    443 	if (div->type == IMX6_CLK_DIV_TABLE) {
    444 		int n = -1;
    445 
    446 		KASSERT(div->tbl != NULL);
    447 		for (int i = 0; div->tbl[i] != 0; i++)
    448 			if (div->tbl[i] == divider)
    449 				n = i;
    450 
    451 		if (n >= 0)
    452 			v |= __SHIFTIN(n, div->mask);
    453 		else
    454 			return EINVAL;
    455 	} else {
    456 		v |= __SHIFTIN(divider - 1, div->mask);
    457 	}
    458 	bus_space_write_4(sc->sc_iot, ioh, div->reg, v);
    459 
    460 	if (div->type == IMX6_CLK_DIV_BUSY)
    461 		ret = imxccm_clk_wait(sc, div->base, div->busy_reg, div->busy_mask);
    462 
    463 	return ret;
    464 }
    465 
    466 /*
    467  * CLK Driver APIs
    468  */
    469 static struct clk *
    470 imxccm_clk_get(void *priv, const char *name)
    471 {
    472 	struct imx6_clk *iclk;
    473 	struct imx6ccm_softc *sc = priv;
    474 
    475 	iclk = imx6_clk_find(sc, name);
    476 	if (iclk == NULL)
    477 		return NULL;
    478 
    479 	atomic_inc_uint(&iclk->refcnt);
    480 
    481 	return &iclk->base;
    482 }
    483 
    484 static void
    485 imxccm_clk_put(void *priv, struct clk *clk)
    486 {
    487 	struct imx6_clk *iclk = (struct imx6_clk *)clk;
    488 
    489 	KASSERT(iclk->refcnt > 0);
    490 
    491 	atomic_dec_uint(&iclk->refcnt);
    492 }
    493 
    494 static u_int
    495 imxccm_clk_get_rate(void *priv, struct clk *clk)
    496 {
    497 	struct imx6_clk *iclk = (struct imx6_clk *)clk;
    498 	struct clk *parent;
    499 	struct imx6ccm_softc *sc = priv;
    500 
    501 	switch (iclk->type) {
    502 	case IMX6_CLK_FIXED:
    503 		return iclk->clk.fixed.rate;
    504 	case IMX6_CLK_FIXED_FACTOR:
    505 		return imxccm_clk_get_rate_fixed_factor(sc, iclk);
    506 	case IMX6_CLK_PLL:
    507 		return imxccm_clk_get_rate_pll(sc, iclk);
    508 	case IMX6_CLK_MUX:
    509 	case IMX6_CLK_GATE:
    510 		parent = imxccm_clk_get_parent(sc, clk);
    511 		return imxccm_clk_get_rate(sc, parent);
    512 	case IMX6_CLK_DIV:
    513 		return imxccm_clk_get_rate_div(sc, iclk);
    514 	case IMX6_CLK_PFD:
    515 		return imxccm_clk_get_rate_pfd(sc, iclk);
    516 	default:
    517 		panic("imx6: unknown clk type %d", iclk->type);
    518 	}
    519 }
    520 
    521 static int
    522 imxccm_clk_set_rate(void *priv, struct clk *clk, u_int rate)
    523 {
    524 	struct imx6_clk *iclk = (struct imx6_clk *)clk;
    525 	struct imx6ccm_softc *sc = priv;
    526 
    527 	switch (iclk->type) {
    528 	case IMX6_CLK_FIXED:
    529 	case IMX6_CLK_FIXED_FACTOR:
    530 		return ENXIO;
    531 	case IMX6_CLK_PLL:
    532 		return imxccm_clk_set_rate_pll(sc, iclk, rate);
    533 	case IMX6_CLK_MUX:
    534 		return ENXIO;
    535 	case IMX6_CLK_GATE:
    536 		return ENXIO;
    537 	case IMX6_CLK_DIV:
    538 		return imxccm_clk_set_rate_div(sc, iclk, rate);
    539 	case IMX6_CLK_PFD:
    540 		return EINVAL;
    541 	default:
    542 		panic("imx6: unknown clk type %d", iclk->type);
    543 	}
    544 }
    545 
    546 static int
    547 imxccm_clk_enable_pll(struct imx6ccm_softc *sc, struct imx6_clk *iclk, bool enable)
    548 {
    549 	struct imx6_clk_pll *pll = &iclk->clk.pll;
    550 
    551 	KASSERT(iclk->type == IMX6_CLK_PLL);
    552 
    553 	/* Power up bit */
    554 	if (pll->type == IMX6_CLK_PLL_USB)
    555 		enable = !enable;
    556 
    557 	bus_space_handle_t ioh = sc->sc_ioh_analog;
    558 	uint32_t  v = bus_space_read_4(sc->sc_iot, ioh, pll->reg);
    559 	if (__SHIFTOUT(v, pll->powerdown) != enable)
    560 		return 0;
    561 	if (enable)
    562 		v &= ~pll->powerdown;
    563 	else
    564 		v |= pll->powerdown;
    565 	bus_space_write_4(sc->sc_iot, ioh, pll->reg, v);
    566 
    567 	/* wait look */
    568 	while (!(bus_space_read_4(sc->sc_iot, ioh, pll->reg) & CCM_ANALOG_PLL_LOCK))
    569 		delay(10);
    570 
    571 	return 0;
    572 }
    573 
    574 static int
    575 imxccm_clk_enable_gate(struct imx6ccm_softc *sc, struct imx6_clk *iclk, bool enable)
    576 {
    577 	struct imx6_clk_gate *gate = &iclk->clk.gate;
    578 
    579 	KASSERT(iclk->type == IMX6_CLK_GATE);
    580 
    581 	bus_space_handle_t ioh;
    582 	if (gate->base == IMX6_CLK_REG_CCM_ANALOG)
    583 		ioh = sc->sc_ioh_analog;
    584 	else
    585 		ioh = sc->sc_ioh;
    586 
    587 	uint32_t v = bus_space_read_4(sc->sc_iot, ioh, gate->reg);
    588 	if (enable) {
    589 		if (gate->exclusive_mask)
    590 			v &= ~gate->exclusive_mask;
    591 		v |= gate->mask;
    592 	} else {
    593 		if (gate->exclusive_mask)
    594 			v |= gate->exclusive_mask;
    595 		v &= ~gate->mask;
    596 	}
    597 	bus_space_write_4(sc->sc_iot, ioh, gate->reg, v);
    598 
    599 	return 0;
    600 }
    601 
    602 static int
    603 imxccm_clk_enable(void *priv, struct clk *clk)
    604 {
    605 	struct imx6_clk *iclk = (struct imx6_clk *)clk;
    606 	struct imx6_clk *parent = NULL;
    607 	struct imx6ccm_softc *sc = priv;
    608 
    609 	if ((parent = imx6_clk_find(sc, iclk->parent)) != NULL)
    610 		imxccm_clk_enable(sc, &parent->base);
    611 
    612 	switch (iclk->type) {
    613 	case IMX6_CLK_FIXED:
    614 	case IMX6_CLK_FIXED_FACTOR:
    615 		return 0;	/* always on */
    616 	case IMX6_CLK_PLL:
    617 		return imxccm_clk_enable_pll(sc, iclk, true);
    618 	case IMX6_CLK_MUX:
    619 	case IMX6_CLK_DIV:
    620 	case IMX6_CLK_PFD:
    621 		return 0;
    622 	case IMX6_CLK_GATE:
    623 		return imxccm_clk_enable_gate(sc, iclk, true);
    624 	default:
    625 		panic("imx6: unknown clk type %d", iclk->type);
    626 	}
    627 }
    628 
    629 static int
    630 imxccm_clk_disable(void *priv, struct clk *clk)
    631 {
    632 	struct imx6_clk *iclk = (struct imx6_clk *)clk;
    633 	struct imx6ccm_softc *sc = priv;
    634 
    635 	switch (iclk->type) {
    636 	case IMX6_CLK_FIXED:
    637 	case IMX6_CLK_FIXED_FACTOR:
    638 		return EINVAL;	/* always on */
    639 	case IMX6_CLK_PLL:
    640 		return imxccm_clk_enable_pll(sc, iclk, false);
    641 	case IMX6_CLK_MUX:
    642 	case IMX6_CLK_DIV:
    643 	case IMX6_CLK_PFD:
    644 		return EINVAL;
    645 	case IMX6_CLK_GATE:
    646 		return imxccm_clk_enable_gate(sc, iclk, false);
    647 	default:
    648 		panic("imx6: unknown clk type %d", iclk->type);
    649 	}
    650 }
    651 
    652 static int
    653 imxccm_clk_set_parent(void *priv, struct clk *clk, struct clk *parent)
    654 {
    655 	struct imx6_clk *iclk = (struct imx6_clk *)clk;
    656 	struct imx6ccm_softc *sc = priv;
    657 
    658 	switch (iclk->type) {
    659 	case IMX6_CLK_FIXED:
    660 	case IMX6_CLK_FIXED_FACTOR:
    661 	case IMX6_CLK_PLL:
    662 	case IMX6_CLK_GATE:
    663 	case IMX6_CLK_DIV:
    664 	case IMX6_CLK_PFD:
    665 		return EINVAL;
    666 	case IMX6_CLK_MUX:
    667 		return imxccm_clk_set_parent_mux(sc, iclk, parent);
    668 	default:
    669 		panic("imx6: unknown clk type %d", iclk->type);
    670 	}
    671 }
    672 
    673 static struct clk *
    674 imxccm_clk_get_parent(void *priv, struct clk *clk)
    675 {
    676 	struct imx6_clk *iclk = (struct imx6_clk *)clk;
    677 	struct imx6_clk *parent = NULL;
    678 	struct imx6ccm_softc *sc = priv;
    679 
    680 	switch (iclk->type) {
    681 	case IMX6_CLK_FIXED:
    682 	case IMX6_CLK_FIXED_FACTOR:
    683 	case IMX6_CLK_PLL:
    684 	case IMX6_CLK_GATE:
    685 	case IMX6_CLK_DIV:
    686 	case IMX6_CLK_PFD:
    687 		if (iclk->parent != NULL)
    688 			parent = imx6_clk_find(sc, iclk->parent);
    689 		break;
    690 	case IMX6_CLK_MUX:
    691 		parent = imxccm_clk_get_parent_mux(sc, iclk);
    692 		break;
    693 	default:
    694 		panic("imx6: unknown clk type %d", iclk->type);
    695 	}
    696 
    697 	return (struct clk *)parent;
    698 }
    699