1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 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 #include <sys/param.h> 31 #include <sys/systm.h> 32 #include <sys/bus.h> 33 #include <sys/malloc.h> 34 35 #include <dev/extres/clk/clk.h> 36 #include <dev/extres/clk/clk_fixed.h> 37 38 #include <arm64/qoriq/clk/qoriq_clkgen.h> 39 40 #include "clkdev_if.h" 41 42 struct qoriq_clk_pll_softc { 43 bus_addr_t offset; 44 45 uint32_t mask; 46 uint32_t shift; 47 48 uint32_t flags; 49 }; 50 51 #define WR4(_clk, offset, val) \ 52 CLKDEV_WRITE_4(clknode_get_device(_clk), offset, val) 53 #define RD4(_clk, offset, val) \ 54 CLKDEV_READ_4(clknode_get_device(_clk), offset, val) 55 #define DEVICE_LOCK(_clk) \ 56 CLKDEV_DEVICE_LOCK(clknode_get_device(_clk)) 57 #define DEVICE_UNLOCK(_clk) \ 58 CLKDEV_DEVICE_UNLOCK(clknode_get_device(_clk)) 59 60 #define QORIQ_PLL_KILL_BIT (1 << 31) 61 62 static int 63 qoriq_clk_pll_init(struct clknode *clk, device_t dev) 64 { 65 66 clknode_init_parent_idx(clk, 0); 67 68 return (0); 69 } 70 71 static int 72 qoriq_clk_pll_recalc_freq(struct clknode *clk, uint64_t *freq) 73 { 74 struct qoriq_clk_pll_softc *sc; 75 uint32_t mul; 76 77 sc = clknode_get_softc(clk); 78 79 RD4(clk, sc->offset, &mul); 80 81 if (sc->flags & QORIQ_CLK_PLL_HAS_KILL_BIT && mul & QORIQ_PLL_KILL_BIT) 82 return (0); 83 84 mul &= sc->mask; 85 mul >>= sc->shift; 86 87 *freq = *freq * mul; 88 89 return (0); 90 } 91 92 static clknode_method_t qoriq_clk_pll_clknode_methods[] = { 93 CLKNODEMETHOD(clknode_init, qoriq_clk_pll_init), 94 CLKNODEMETHOD(clknode_recalc_freq, qoriq_clk_pll_recalc_freq), 95 96 CLKNODEMETHOD_END 97 }; 98 99 DEFINE_CLASS_1(qoriq_clk_pll_clknode, qoriq_clk_pll_clknode_class, 100 qoriq_clk_pll_clknode_methods, sizeof(struct qoriq_clk_pll_softc), 101 clknode_class); 102 103 int 104 qoriq_clk_pll_register(struct clkdom *clkdom, 105 const struct qoriq_clk_pll_def *clkdef) 106 { 107 char namebuf[QORIQ_CLK_NAME_MAX_LEN]; 108 struct qoriq_clk_pll_softc *sc; 109 struct clk_fixed_def def; 110 const char *parent_name; 111 struct clknode *clk; 112 int error; 113 int i; 114 115 clk = clknode_create(clkdom, &qoriq_clk_pll_clknode_class, 116 &clkdef->clkdef); 117 if (clk == NULL) 118 return (1); 119 120 sc = clknode_get_softc(clk); 121 sc->mask = clkdef->mask; 122 sc->shift = clkdef->shift; 123 sc->flags = clkdef->flags; 124 sc->offset = clkdef->offset; 125 126 clknode_register(clkdom, clk); 127 128 parent_name = clkdef->clkdef.name; 129 130 def.clkdef.parent_names = &parent_name; 131 def.clkdef.parent_cnt = 1; 132 def.clkdef.name = namebuf; 133 def.mult = 1; 134 def.freq = 0; 135 136 i = 0; 137 while (clkdef->dividers[i] != 0) { 138 def.div = clkdef->dividers[i]; 139 def.clkdef.id = clkdef->clkdef.id + i; 140 snprintf(namebuf, QORIQ_CLK_NAME_MAX_LEN, "%s_div%d", 141 parent_name, clkdef->dividers[i]); 142 143 error = clknode_fixed_register(clkdom, &def); 144 if (error != 0) 145 return (error); 146 147 i++; 148 } 149 150 return (0); 151 } 152