1 1.4 jmcneill /* $NetBSD: sunxi_ccu_nm.c,v 1.4 2017/10/28 13:13:45 jmcneill Exp $ */ 2 1.1 jmcneill 3 1.1 jmcneill /*- 4 1.1 jmcneill * Copyright (c) 2017 Jared McNeill <jmcneill (at) invisible.ca> 5 1.1 jmcneill * All rights reserved. 6 1.1 jmcneill * 7 1.1 jmcneill * Redistribution and use in source and binary forms, with or without 8 1.1 jmcneill * modification, are permitted provided that the following conditions 9 1.1 jmcneill * are met: 10 1.1 jmcneill * 1. Redistributions of source code must retain the above copyright 11 1.1 jmcneill * notice, this list of conditions and the following disclaimer. 12 1.1 jmcneill * 2. Redistributions in binary form must reproduce the above copyright 13 1.1 jmcneill * notice, this list of conditions and the following disclaimer in the 14 1.1 jmcneill * documentation and/or other materials provided with the distribution. 15 1.1 jmcneill * 16 1.1 jmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 1.1 jmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 1.1 jmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 1.1 jmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 1.1 jmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 21 1.1 jmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 1.1 jmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 23 1.1 jmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 1.1 jmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 1.1 jmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 1.1 jmcneill * SUCH DAMAGE. 27 1.1 jmcneill */ 28 1.1 jmcneill 29 1.1 jmcneill #include <sys/cdefs.h> 30 1.4 jmcneill __KERNEL_RCSID(0, "$NetBSD: sunxi_ccu_nm.c,v 1.4 2017/10/28 13:13:45 jmcneill Exp $"); 31 1.1 jmcneill 32 1.1 jmcneill #include <sys/param.h> 33 1.1 jmcneill #include <sys/bus.h> 34 1.1 jmcneill 35 1.1 jmcneill #include <dev/clk/clk_backend.h> 36 1.1 jmcneill 37 1.1 jmcneill #include <arm/sunxi/sunxi_ccu.h> 38 1.1 jmcneill 39 1.2 jmcneill int 40 1.2 jmcneill sunxi_ccu_nm_enable(struct sunxi_ccu_softc *sc, struct sunxi_ccu_clk *clk, 41 1.2 jmcneill int enable) 42 1.2 jmcneill { 43 1.2 jmcneill struct sunxi_ccu_nm *nm = &clk->u.nm; 44 1.2 jmcneill uint32_t val; 45 1.2 jmcneill 46 1.2 jmcneill KASSERT(clk->type == SUNXI_CCU_NM); 47 1.2 jmcneill 48 1.2 jmcneill if (!nm->enable) 49 1.2 jmcneill return enable ? 0 : EINVAL; 50 1.2 jmcneill 51 1.2 jmcneill val = CCU_READ(sc, nm->reg); 52 1.2 jmcneill if (enable) 53 1.2 jmcneill val |= nm->enable; 54 1.2 jmcneill else 55 1.2 jmcneill val &= ~nm->enable; 56 1.2 jmcneill CCU_WRITE(sc, nm->reg, val); 57 1.2 jmcneill 58 1.2 jmcneill return 0; 59 1.2 jmcneill } 60 1.2 jmcneill 61 1.1 jmcneill u_int 62 1.1 jmcneill sunxi_ccu_nm_get_rate(struct sunxi_ccu_softc *sc, 63 1.1 jmcneill struct sunxi_ccu_clk *clk) 64 1.1 jmcneill { 65 1.1 jmcneill struct sunxi_ccu_nm *nm = &clk->u.nm; 66 1.1 jmcneill struct clk *clkp, *clkp_parent; 67 1.1 jmcneill u_int rate, n, m; 68 1.1 jmcneill uint32_t val; 69 1.1 jmcneill 70 1.1 jmcneill KASSERT(clk->type == SUNXI_CCU_NM); 71 1.1 jmcneill 72 1.1 jmcneill clkp = &clk->base; 73 1.1 jmcneill clkp_parent = clk_get_parent(clkp); 74 1.1 jmcneill if (clkp_parent == NULL) 75 1.1 jmcneill return 0; 76 1.1 jmcneill 77 1.1 jmcneill rate = clk_get_rate(clkp_parent); 78 1.1 jmcneill if (rate == 0) 79 1.1 jmcneill return 0; 80 1.1 jmcneill 81 1.1 jmcneill val = CCU_READ(sc, nm->reg); 82 1.1 jmcneill n = __SHIFTOUT(val, nm->n); 83 1.1 jmcneill m = __SHIFTOUT(val, nm->m); 84 1.1 jmcneill 85 1.2 jmcneill if (nm->enable && !(val & nm->enable)) 86 1.2 jmcneill return 0; 87 1.2 jmcneill 88 1.1 jmcneill if (nm->flags & SUNXI_CCU_NM_POWER_OF_TWO) 89 1.2 jmcneill n = 1 << n; 90 1.1 jmcneill else 91 1.1 jmcneill n++; 92 1.1 jmcneill 93 1.1 jmcneill m++; 94 1.1 jmcneill 95 1.4 jmcneill if (nm->flags & SUNXI_CCU_NM_DIVIDE_BY_TWO) 96 1.4 jmcneill m *= 2; 97 1.4 jmcneill 98 1.1 jmcneill return rate / n / m; 99 1.1 jmcneill } 100 1.1 jmcneill 101 1.1 jmcneill int 102 1.1 jmcneill sunxi_ccu_nm_set_rate(struct sunxi_ccu_softc *sc, 103 1.2 jmcneill struct sunxi_ccu_clk *clk, u_int new_rate) 104 1.1 jmcneill { 105 1.2 jmcneill struct sunxi_ccu_nm *nm = &clk->u.nm; 106 1.2 jmcneill struct clk *clkp, *clkp_parent; 107 1.2 jmcneill u_int parent_rate, best_rate, best_n, best_m, best_parent; 108 1.2 jmcneill u_int n, m, pindex, rate; 109 1.2 jmcneill int best_diff; 110 1.2 jmcneill uint32_t val; 111 1.2 jmcneill 112 1.2 jmcneill const u_int n_max = __SHIFTOUT(nm->n, nm->n); 113 1.2 jmcneill const u_int m_max = __SHIFTOUT(nm->m, nm->m); 114 1.2 jmcneill 115 1.2 jmcneill clkp = &clk->base; 116 1.2 jmcneill clkp_parent = clk_get_parent(clkp); 117 1.2 jmcneill if (clkp_parent == NULL) 118 1.2 jmcneill return 0; 119 1.2 jmcneill 120 1.2 jmcneill rate = clk_get_rate(clkp_parent); 121 1.2 jmcneill if (rate == 0) 122 1.2 jmcneill return 0; 123 1.2 jmcneill 124 1.2 jmcneill best_rate = 0; 125 1.2 jmcneill best_diff = INT_MAX; 126 1.2 jmcneill for (pindex = 0; pindex < nm->nparents; pindex++) { 127 1.2 jmcneill /* XXX 128 1.2 jmcneill * Shouldn't have to set parent to get potential parent clock rate 129 1.2 jmcneill */ 130 1.2 jmcneill val = CCU_READ(sc, nm->reg); 131 1.2 jmcneill val &= ~nm->sel; 132 1.2 jmcneill val |= __SHIFTIN(pindex, nm->sel); 133 1.2 jmcneill CCU_WRITE(sc, nm->reg, val); 134 1.2 jmcneill 135 1.2 jmcneill clkp_parent = clk_get_parent(clkp); 136 1.2 jmcneill if (clkp_parent == NULL) 137 1.2 jmcneill continue; 138 1.2 jmcneill parent_rate = clk_get_rate(clkp_parent); 139 1.2 jmcneill if (parent_rate == 0) 140 1.2 jmcneill continue; 141 1.2 jmcneill 142 1.2 jmcneill for (n = 0; n <= n_max; n++) { 143 1.2 jmcneill for (m = 0; m <= m_max; m++) { 144 1.2 jmcneill if (nm->flags & SUNXI_CCU_NM_POWER_OF_TWO) 145 1.2 jmcneill rate = parent_rate / (1 << n) / (m + 1); 146 1.2 jmcneill else 147 1.2 jmcneill rate = parent_rate / (n + 1) / (m + 1); 148 1.4 jmcneill if (nm->flags & SUNXI_CCU_NM_DIVIDE_BY_TWO) 149 1.4 jmcneill rate /= 2; 150 1.2 jmcneill 151 1.2 jmcneill if (nm->flags & SUNXI_CCU_NM_ROUND_DOWN) { 152 1.2 jmcneill const int diff = new_rate - rate; 153 1.2 jmcneill if (diff >= 0 && rate > best_rate) { 154 1.3 jmcneill best_diff = diff; 155 1.2 jmcneill best_rate = rate; 156 1.2 jmcneill best_n = n; 157 1.2 jmcneill best_m = m; 158 1.2 jmcneill best_parent = pindex; 159 1.2 jmcneill } 160 1.2 jmcneill } else { 161 1.2 jmcneill const int diff = abs(new_rate - rate); 162 1.2 jmcneill if (diff < best_diff) { 163 1.3 jmcneill best_diff = diff; 164 1.2 jmcneill best_rate = rate; 165 1.2 jmcneill best_n = n; 166 1.2 jmcneill best_m = m; 167 1.2 jmcneill best_parent = pindex; 168 1.2 jmcneill } 169 1.2 jmcneill } 170 1.2 jmcneill } 171 1.2 jmcneill } 172 1.2 jmcneill } 173 1.2 jmcneill 174 1.2 jmcneill if (best_rate == 0) 175 1.2 jmcneill return ERANGE; 176 1.2 jmcneill 177 1.2 jmcneill val = CCU_READ(sc, nm->reg); 178 1.2 jmcneill val &= ~nm->sel; 179 1.2 jmcneill val |= __SHIFTIN(best_parent, nm->sel); 180 1.2 jmcneill val &= ~nm->n; 181 1.2 jmcneill val |= __SHIFTIN(best_n, nm->n); 182 1.2 jmcneill val &= ~nm->m; 183 1.2 jmcneill val |= __SHIFTIN(best_m, nm->m); 184 1.2 jmcneill CCU_WRITE(sc, nm->reg, val); 185 1.2 jmcneill 186 1.2 jmcneill return 0; 187 1.1 jmcneill } 188 1.1 jmcneill 189 1.1 jmcneill int 190 1.1 jmcneill sunxi_ccu_nm_set_parent(struct sunxi_ccu_softc *sc, 191 1.1 jmcneill struct sunxi_ccu_clk *clk, const char *name) 192 1.1 jmcneill { 193 1.1 jmcneill struct sunxi_ccu_nm *nm = &clk->u.nm; 194 1.1 jmcneill uint32_t val; 195 1.1 jmcneill u_int index; 196 1.1 jmcneill 197 1.1 jmcneill KASSERT(clk->type == SUNXI_CCU_NM); 198 1.1 jmcneill 199 1.1 jmcneill if (nm->sel == 0) 200 1.1 jmcneill return ENODEV; 201 1.1 jmcneill 202 1.1 jmcneill for (index = 0; index < nm->nparents; index++) { 203 1.1 jmcneill if (nm->parents[index] != NULL && 204 1.1 jmcneill strcmp(nm->parents[index], name) == 0) 205 1.1 jmcneill break; 206 1.1 jmcneill } 207 1.1 jmcneill if (index == nm->nparents) 208 1.1 jmcneill return EINVAL; 209 1.1 jmcneill 210 1.1 jmcneill val = CCU_READ(sc, nm->reg); 211 1.1 jmcneill val &= ~nm->sel; 212 1.1 jmcneill val |= __SHIFTIN(index, nm->sel); 213 1.1 jmcneill CCU_WRITE(sc, nm->reg, val); 214 1.1 jmcneill 215 1.1 jmcneill return 0; 216 1.1 jmcneill } 217 1.1 jmcneill 218 1.1 jmcneill const char * 219 1.1 jmcneill sunxi_ccu_nm_get_parent(struct sunxi_ccu_softc *sc, 220 1.1 jmcneill struct sunxi_ccu_clk *clk) 221 1.1 jmcneill { 222 1.1 jmcneill struct sunxi_ccu_nm *nm = &clk->u.nm; 223 1.1 jmcneill u_int index; 224 1.1 jmcneill uint32_t val; 225 1.1 jmcneill 226 1.1 jmcneill KASSERT(clk->type == SUNXI_CCU_NM); 227 1.1 jmcneill 228 1.1 jmcneill val = CCU_READ(sc, nm->reg); 229 1.1 jmcneill index = __SHIFTOUT(val, nm->sel); 230 1.1 jmcneill 231 1.1 jmcneill return nm->parents[index]; 232 1.1 jmcneill } 233