xref: /freebsd/sys/arm/mv/clk/a37x0_tbg_pll.c (revision 22cf89c938886d14f5796fc49f9f020c23ea8eaf)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2021 Semihalf.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 #include <sys/param.h>
30 #include <sys/bus.h>
31 #include <sys/rman.h>
32 #include <machine/bus.h>
33 
34 #include <dev/extres/clk/clk.h>
35 
36 #include <dev/ofw/ofw_bus.h>
37 #include <dev/ofw/ofw_bus_subr.h>
38 
39 #include "clkdev_if.h"
40 
41 #include "a37x0_tbg_pll.h"
42 
43 #define RD4(_clk, offset, val)			\
44 	CLKDEV_READ_4(clknode_get_device(_clk), offset, val)
45 
46 struct a37x0_tbg_pll_softc {
47 	struct a37x0_tbg_pll_reg_def		vcodiv;
48 	struct a37x0_tbg_pll_reg_def		refdiv;
49 	struct a37x0_tbg_pll_reg_def		fbdiv;
50 	struct a37x0_tbg_pll_reg_def		tbg_bypass;
51 };
52 
53 static int
54 a37x0_tbg_pll_recalc_freq(struct clknode *clk, uint64_t *freq)
55 {
56 	struct a37x0_tbg_pll_softc *sc;
57 	uint32_t vcodiv, fbdiv, refdiv;
58 	unsigned int val;
59 
60 	sc = clknode_get_softc(clk);
61 
62 	RD4(clk, sc->tbg_bypass.offset, &val);
63 	if ((val >> sc->tbg_bypass.shift) & sc->tbg_bypass.mask)
64 		return 0;
65 
66 	RD4(clk, sc->vcodiv.offset, &val);
67 	vcodiv = 1 << ((val >> sc->vcodiv.shift) & sc->vcodiv.mask);
68 
69 	RD4(clk, sc->refdiv.offset, &val);
70 	refdiv = (val >> sc->refdiv.shift) & sc->refdiv.mask;
71 
72 	RD4(clk, sc->fbdiv.offset, &val);
73 	fbdiv = (val >> sc->fbdiv.shift) & sc->fbdiv.mask;
74 
75 	if (refdiv == 0)
76 		refdiv = 1;
77 
78 	*freq = *freq * (fbdiv / refdiv) * 4;
79 	*freq /= vcodiv;
80 
81 	return (0);
82 }
83 
84 static int
85 a37x0_tbg_pll_init(struct clknode *clk, device_t dev)
86 {
87 
88 	clknode_init_parent_idx(clk, 0);
89 
90 	return (0);
91 }
92 
93 static clknode_method_t a37x0_tbg_pll_clknode_methods[] = {
94 	CLKNODEMETHOD(clknode_recalc_freq,	a37x0_tbg_pll_recalc_freq),
95 	CLKNODEMETHOD(clknode_init,		a37x0_tbg_pll_init),
96 
97 	CLKNODEMETHOD_END
98 };
99 
100 DEFINE_CLASS_1(a37x0_tbg_pll__clknode, a37x0_tbg_pll_clknode_class,
101     a37x0_tbg_pll_clknode_methods, sizeof(struct a37x0_tbg_pll_softc),
102     clknode_class);
103 
104 int
105 a37x0_tbg_pll_clk_register(struct clkdom *clkdom,
106     const struct a37x0_tbg_pll_clk_def *clkdef)
107 {
108 	struct a37x0_tbg_pll_softc *sc;
109 	struct clknode *clk;
110 
111 	clk = clknode_create(clkdom, &a37x0_tbg_pll_clknode_class,
112 	    &clkdef->clkdef);
113 
114 	if (clk == NULL)
115 		return (1);
116 
117 	sc = clknode_get_softc(clk);
118 
119 	sc->vcodiv = clkdef->vcodiv;
120 	sc->refdiv = clkdef->refdiv;
121 	sc->fbdiv = clkdef->fbdiv;
122 	sc->tbg_bypass = clkdef->tbg_bypass;
123 
124 	if (clknode_register(clkdom, clk) == NULL)
125 		return (1);
126 
127 	return (0);
128 }
129