1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2020 Alstom Group. 5 * Copyright (c) 2020 Semihalf. 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 AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, 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 __FBSDID("$FreeBSD$"); 31 32 #include <sys/param.h> 33 #include <sys/systm.h> 34 #include <sys/bus.h> 35 36 #include <dev/extres/clk/clk.h> 37 #include <dev/extres/clk/clk_fixed.h> 38 39 #include <arm64/qoriq/clk/qoriq_clkgen.h> 40 41 #include "clkdev_if.h" 42 43 struct qoriq_clk_pll_softc { 44 bus_addr_t offset; 45 46 uint32_t mask; 47 uint32_t shift; 48 49 uint32_t flags; 50 }; 51 52 #define WR4(_clk, offset, val) \ 53 CLKDEV_WRITE_4(clknode_get_device(_clk), offset, val) 54 #define RD4(_clk, offset, val) \ 55 CLKDEV_READ_4(clknode_get_device(_clk), offset, val) 56 #define DEVICE_LOCK(_clk) \ 57 CLKDEV_DEVICE_LOCK(clknode_get_device(_clk)) 58 #define DEVICE_UNLOCK(_clk) \ 59 CLKDEV_DEVICE_UNLOCK(clknode_get_device(_clk)) 60 61 #define QORIQ_PLL_KILL_BIT (1 << 31) 62 63 static int 64 qoriq_clk_pll_init(struct clknode *clk, device_t dev) 65 { 66 67 clknode_init_parent_idx(clk, 0); 68 69 return (0); 70 } 71 72 static int 73 qoriq_clk_pll_recalc_freq(struct clknode *clk, uint64_t *freq) 74 { 75 struct qoriq_clk_pll_softc *sc; 76 uint32_t mul; 77 78 sc = clknode_get_softc(clk); 79 80 RD4(clk, sc->offset, &mul); 81 82 if (sc->flags & QORIQ_CLK_PLL_HAS_KILL_BIT && mul & QORIQ_PLL_KILL_BIT) 83 return (0); 84 85 mul &= sc->mask; 86 mul >>= sc->shift; 87 88 *freq = *freq * mul; 89 90 return (0); 91 } 92 93 static clknode_method_t qoriq_clk_pll_clknode_methods[] = { 94 CLKNODEMETHOD(clknode_init, qoriq_clk_pll_init), 95 CLKNODEMETHOD(clknode_recalc_freq, qoriq_clk_pll_recalc_freq), 96 97 CLKNODEMETHOD_END 98 }; 99 100 DEFINE_CLASS_1(qoriq_clk_pll_clknode, qoriq_clk_pll_clknode_class, 101 qoriq_clk_pll_clknode_methods, sizeof(struct qoriq_clk_pll_softc), 102 clknode_class); 103 104 int 105 qoriq_clk_pll_register(struct clkdom *clkdom, 106 const struct qoriq_clk_pll_def *clkdef) 107 { 108 char namebuf[QORIQ_CLK_NAME_MAX_LEN]; 109 struct qoriq_clk_pll_softc *sc; 110 struct clk_fixed_def def; 111 const char *parent_name; 112 struct clknode *clk; 113 int error; 114 int i; 115 116 clk = clknode_create(clkdom, &qoriq_clk_pll_clknode_class, 117 &clkdef->clkdef); 118 if (clk == NULL) 119 return (1); 120 121 sc = clknode_get_softc(clk); 122 sc->mask = clkdef->mask; 123 sc->shift = clkdef->shift; 124 sc->flags = clkdef->flags; 125 sc->offset = clkdef->offset; 126 127 clknode_register(clkdom, clk); 128 129 parent_name = clkdef->clkdef.name; 130 131 def.clkdef.parent_names = &parent_name; 132 def.clkdef.parent_cnt = 1; 133 def.clkdef.name = namebuf; 134 def.mult = 1; 135 def.freq = 0; 136 137 i = 0; 138 while (clkdef->dividers[i] != 0) { 139 def.div = clkdef->dividers[i]; 140 def.clkdef.id = clkdef->clkdef.id + i; 141 snprintf(namebuf, QORIQ_CLK_NAME_MAX_LEN, "%s_div%d", 142 parent_name, clkdef->dividers[i]); 143 144 error = clknode_fixed_register(clkdom, &def); 145 if (error != 0) 146 return (error); 147 148 i++; 149 } 150 151 return (0); 152 } 153