1 /*- 2 * Copyright (c) 2021 Adrian Chadd <adrian@FreeBSD.org>. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23 * SUCH DAMAGE. 24 */ 25 26 #include <sys/cdefs.h> 27 #include <sys/param.h> 28 #include <sys/systm.h> 29 #include <sys/bus.h> 30 #include <sys/lock.h> 31 #include <sys/mutex.h> 32 #include <sys/rman.h> 33 #include <machine/bus.h> 34 35 #include <dev/extres/clk/clk.h> 36 #include <dev/extres/clk/clk_div.h> 37 #include <dev/extres/clk/clk_fixed.h> 38 #include <dev/extres/clk/clk_mux.h> 39 40 #include "qcom_clk_ro_div.h" 41 42 #include "clkdev_if.h" 43 44 #if 0 45 #define DPRINTF(dev, msg...) device_printf(dev, "cpufreq_dt: " msg); 46 #else 47 #define DPRINTF(dev, msg...) 48 #endif 49 50 /* 51 * This is a read-only divisor table node. 52 * It represents some divisor that is setup by the boot environment 53 * and we don't have any need for the driver to go and fiddle with. 54 * 55 * It likely should just live in the extres/clk code. 56 */ 57 58 struct qcom_clk_ro_div_sc { 59 struct clknode *clknode; 60 uint32_t offset; 61 uint32_t shift; 62 uint32_t width; 63 struct qcom_clk_ro_div_tbl *div_tbl; 64 }; 65 66 static int 67 qcom_clk_ro_div_recalc(struct clknode *clk, uint64_t *freq) 68 { 69 struct qcom_clk_ro_div_sc *sc; 70 uint32_t reg, idx, div = 1; 71 int i; 72 73 sc = clknode_get_softc(clk); 74 75 if (freq == NULL || *freq == 0) { 76 printf("%s: called; NULL or 0 frequency\n", __func__); 77 return (ENXIO); 78 } 79 80 CLKDEV_DEVICE_LOCK(clknode_get_device(sc->clknode)); 81 CLKDEV_READ_4(clknode_get_device(sc->clknode), sc->offset, ®); 82 CLKDEV_DEVICE_UNLOCK(clknode_get_device(sc->clknode)); 83 84 idx = (reg >> sc->shift) & ((1U << sc->width) - 1); 85 86 for (i = 0; (sc->div_tbl[i].div != 0); i++) { 87 if (idx == sc->div_tbl[i].val) { 88 div = sc->div_tbl[i].div; 89 break; 90 } 91 } 92 93 DPRINTF(clknode_get_device(sc->clknode), 94 "%s: freq=%llu, idx=%u, div=%u, out_freq=%llu\n", 95 __func__, 96 *freq, 97 idx, 98 div, 99 *freq / div); 100 101 *freq = *freq / div; 102 return (0); 103 } 104 105 static int 106 qcom_clk_ro_div_init(struct clknode *clk, device_t dev) 107 { 108 109 /* 110 * There's only a single parent here for this divisor, 111 * so just set it to 0; the caller doesn't need to supply it. 112 */ 113 clknode_init_parent_idx(clk, 0); 114 115 return (0); 116 } 117 118 static clknode_method_t qcom_clk_ro_div_methods[] = { 119 /* Device interface */ 120 CLKNODEMETHOD(clknode_init, qcom_clk_ro_div_init), 121 CLKNODEMETHOD(clknode_recalc_freq, qcom_clk_ro_div_recalc), 122 CLKNODEMETHOD_END 123 }; 124 125 DEFINE_CLASS_1(qcom_clk_fepll, qcom_clk_ro_div_class, 126 qcom_clk_ro_div_methods, sizeof(struct qcom_clk_ro_div_sc), 127 clknode_class); 128 129 int 130 qcom_clk_ro_div_register(struct clkdom *clkdom, 131 struct qcom_clk_ro_div_def *clkdef) 132 { 133 struct clknode *clk; 134 struct qcom_clk_ro_div_sc *sc; 135 136 clk = clknode_create(clkdom, &qcom_clk_ro_div_class, 137 &clkdef->clkdef); 138 if (clk == NULL) 139 return (1); 140 141 sc = clknode_get_softc(clk); 142 sc->clknode = clk; 143 sc->offset = clkdef->offset; 144 sc->shift = clkdef->shift; 145 sc->width = clkdef->width; 146 sc->div_tbl = clkdef->div_tbl; 147 148 clknode_register(clkdom, clk); 149 150 return (0); 151 } 152