1 /* $NetBSD: imx_ccm_composite.c,v 1.3 2026/11/26 03:39:30 hkenken Exp $ */ 2 3 /*- 4 * Copyright (c) 2020 Jared 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 <sys/cdefs.h> 30 __KERNEL_RCSID(0, "$NetBSD: imx_ccm_composite.c,v 1.3 2026/11/26 03:39:30 hkenken Exp $"); 31 32 #include <sys/param.h> 33 #include <sys/bus.h> 34 35 #include <dev/clk/clk_backend.h> 36 37 #include <arm/nxp/imx_ccm.h> 38 39 #define CCM_TARGET_ROOT 0x00 40 #define TARGET_ROOT_ENABLE __BIT(28) 41 #define TARGET_ROOT_MUX __BITS(26,24) 42 #define TARGET_ROOT_PRE_PODF __BITS(18,16) 43 #define TARGET_ROOT_POST_PODF __BITS(5,0) 44 45 int 46 imx_ccm_composite_enable(struct imx_ccm_softc *sc, struct imx_ccm_clk *clk, 47 int enable) 48 { 49 struct imx_ccm_composite *composite = &clk->u.composite; 50 uint32_t val; 51 52 KASSERT(clk->type == IMX_CCM_COMPOSITE); 53 54 val = CCM_READ(sc, clk->regidx, composite->reg + CCM_TARGET_ROOT); 55 if (enable) 56 val |= TARGET_ROOT_ENABLE; 57 else 58 val &= ~TARGET_ROOT_ENABLE; 59 CCM_WRITE(sc, clk->regidx, composite->reg + CCM_TARGET_ROOT, val); 60 61 return 0; 62 } 63 64 u_int 65 imx_ccm_composite_get_rate(struct imx_ccm_softc *sc, 66 struct imx_ccm_clk *clk) 67 { 68 struct imx_ccm_composite *composite = &clk->u.composite; 69 struct clk *clkp, *clkp_parent; 70 71 KASSERT(clk->type == IMX_CCM_COMPOSITE); 72 73 clkp = &clk->base; 74 clkp_parent = clk_get_parent(clkp); 75 if (clkp_parent == NULL) 76 return 0; 77 78 const u_int prate = clk_get_rate(clkp_parent); 79 if (prate == 0) 80 return 0; 81 82 const uint32_t val = CCM_READ(sc, clk->regidx, composite->reg + CCM_TARGET_ROOT); 83 const u_int pre_div = __SHIFTOUT(val, TARGET_ROOT_PRE_PODF) + 1; 84 const u_int post_div = __SHIFTOUT(val, TARGET_ROOT_POST_PODF) + 1; 85 86 return prate / pre_div / post_div; 87 } 88 89 int 90 imx_ccm_composite_set_rate(struct imx_ccm_softc *sc, 91 struct imx_ccm_clk *clk, u_int rate) 92 { 93 struct imx_ccm_composite *composite = &clk->u.composite; 94 u_int best_prediv, best_postdiv, best_diff; 95 struct clk *clk_parent; 96 uint32_t val; 97 98 KASSERT(clk->type == IMX_CCM_COMPOSITE); 99 100 clk_parent = clk_get_parent(&clk->base); 101 if (clk_parent == NULL) 102 return ENXIO; 103 104 if (composite->flags & IMX_COMPOSITE_SET_RATE_PARENT) 105 return clk_set_rate(clk_parent, rate); 106 107 best_prediv = 0; 108 best_postdiv = 0; 109 best_diff = 0; 110 111 const u_int prate = clk_get_rate(clk_parent); 112 if (prate == 0) 113 return ERANGE; 114 115 bool found = false; 116 for (u_int prediv = 1; prediv <= __SHIFTOUT_MASK(TARGET_ROOT_PRE_PODF) + 1; prediv++) { 117 for (u_int postdiv = 1; postdiv <= __SHIFTOUT_MASK(TARGET_ROOT_POST_PODF) + 1; postdiv++) { 118 const u_int cur_rate = prate / prediv / postdiv; 119 if (composite->flags & IMX_COMPOSITE_ROUND_DOWN) 120 if (rate < cur_rate) 121 continue; 122 123 const u_int diff = (rate > cur_rate) ? rate - cur_rate : cur_rate - rate; 124 if (!found || diff < best_diff) { 125 best_diff = diff; 126 best_prediv = prediv; 127 best_postdiv = postdiv; 128 found = true; 129 } 130 } 131 } 132 if (!found) 133 return ERANGE; 134 135 val = CCM_READ(sc, clk->regidx, composite->reg + CCM_TARGET_ROOT); 136 val &= ~TARGET_ROOT_PRE_PODF; 137 val |= __SHIFTIN(best_prediv - 1, TARGET_ROOT_PRE_PODF); 138 val &= ~TARGET_ROOT_POST_PODF; 139 val |= __SHIFTIN(best_postdiv - 1, TARGET_ROOT_POST_PODF); 140 CCM_WRITE(sc, clk->regidx, composite->reg + CCM_TARGET_ROOT, val); 141 142 return 0; 143 } 144 145 const char * 146 imx_ccm_composite_get_parent(struct imx_ccm_softc *sc, 147 struct imx_ccm_clk *clk) 148 { 149 struct imx_ccm_composite *composite = &clk->u.composite; 150 151 KASSERT(clk->type == IMX_CCM_COMPOSITE); 152 153 const uint32_t val = CCM_READ(sc, clk->regidx, composite->reg + CCM_TARGET_ROOT); 154 const u_int mux = __SHIFTOUT(val, TARGET_ROOT_MUX); 155 156 if (mux >= composite->nparents) 157 return NULL; 158 159 return composite->parents[mux]; 160 } 161 162 int 163 imx_ccm_composite_set_parent(struct imx_ccm_softc *sc, 164 struct imx_ccm_clk *clk, const char *parent) 165 { 166 struct imx_ccm_composite *composite = &clk->u.composite; 167 uint32_t val; 168 169 KASSERT(clk->type == IMX_CCM_COMPOSITE); 170 171 for (u_int mux = 0; mux < composite->nparents; mux++) { 172 if (strcmp(composite->parents[mux], parent) == 0) { 173 val = CCM_READ(sc, clk->regidx, composite->reg + CCM_TARGET_ROOT); 174 val &= ~TARGET_ROOT_MUX; 175 val |= __SHIFTIN(mux, TARGET_ROOT_MUX); 176 CCM_WRITE(sc, clk->regidx, composite->reg + CCM_TARGET_ROOT, val); 177 return 0; 178 } 179 } 180 181 return EINVAL; 182 } 183