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