xref: /freebsd/sys/dev/qcom_clk/qcom_clk_fepll.c (revision e6bfd18d21b225af6a0ed67ceeaf1293b7b9eba5)
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 __FBSDID("$FreeBSD$");
28 
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/bus.h>
32 #include <sys/lock.h>
33 #include <sys/mutex.h>
34 #include <sys/rman.h>
35 #include <machine/bus.h>
36 
37 #include <dev/extres/clk/clk.h>
38 #include <dev/extres/clk/clk_div.h>
39 #include <dev/extres/clk/clk_fixed.h>
40 #include <dev/extres/clk/clk_mux.h>
41 
42 #include "qcom_clk_fepll.h"
43 
44 #include "clkdev_if.h"
45 
46 #if 0
47 #define DPRINTF(dev, msg...) device_printf(dev, "cpufreq_dt: " msg);
48 #else
49 #define DPRINTF(dev, msg...)
50 #endif
51 
52 /*
53  * This is the top-level PLL clock on the IPQ4018/IPQ4019.
54  * It's a fixed PLL clock that feeds a bunch of divisors into
55  * downstrem FEPLL* and DDR clocks.
56  *
57  * Now, on Linux the clock code creates multiple instances of this
58  * with an inbuilt divisor.  Here instead there'll be a single
59  * instance of the FEPLL, and then normal divisors will feed into
60  * the multiple PLL nodes.
61  */
62 
63 struct qcom_clk_fepll_sc {
64 	struct clknode	*clknode;
65 	uint32_t offset;
66 	uint32_t fdbkdiv_shift; /* FDBKDIV base */
67 	uint32_t fdbkdiv_width; /* FDBKDIV width */
68 	uint32_t refclkdiv_shift; /* REFCLKDIV base */
69 	uint32_t refclkdiv_width; /* REFCLKDIV width */
70 };
71 
72 static int
73 qcom_clk_fepll_recalc(struct clknode *clk, uint64_t *freq)
74 {
75 	struct qcom_clk_fepll_sc *sc;
76 	uint64_t vco, parent_rate;
77 	uint32_t reg, fdbkdiv, refclkdiv;
78 
79 	sc = clknode_get_softc(clk);
80 
81 	if (freq == NULL || *freq == 0) {
82 		device_printf(clknode_get_device(sc->clknode),
83 		    "%s: called; NULL or 0 frequency\n",
84 		    __func__);
85 		return (ENXIO);
86 	}
87 
88 	parent_rate = *freq;
89 
90 	CLKDEV_DEVICE_LOCK(clknode_get_device(sc->clknode));
91 	CLKDEV_READ_4(clknode_get_device(sc->clknode), sc->offset, &reg);
92 	CLKDEV_DEVICE_UNLOCK(clknode_get_device(sc->clknode));
93 
94 	fdbkdiv = (reg >> sc->fdbkdiv_shift) &
95 	    ((1U << sc->fdbkdiv_width) - 1);
96 	refclkdiv = (reg >> sc->refclkdiv_shift) &
97 	    ((1U << sc->refclkdiv_width) - 1);
98 
99 	vco = parent_rate / refclkdiv;
100 	vco = vco * 2;
101 	vco = vco * fdbkdiv;
102 
103 	*freq = vco;
104 	return (0);
105 }
106 
107 static int
108 qcom_clk_fepll_init(struct clknode *clk, device_t dev)
109 {
110 
111 	/*
112 	 * There's only a single parent here for an FEPLL, so just set it
113 	 * to 0; the caller doesn't need to supply it.
114 	 */
115 	clknode_init_parent_idx(clk, 0);
116 
117 	return (0);
118 }
119 
120 static clknode_method_t qcom_clk_fepll_methods[] = {
121 	/* Device interface */
122 	CLKNODEMETHOD(clknode_init,		qcom_clk_fepll_init),
123 	CLKNODEMETHOD(clknode_recalc_freq,	qcom_clk_fepll_recalc),
124 	CLKNODEMETHOD_END
125 };
126 
127 DEFINE_CLASS_1(qcom_clk_fepll, qcom_clk_fepll_class, qcom_clk_fepll_methods,
128    sizeof(struct qcom_clk_fepll_sc), clknode_class);
129 
130 int
131 qcom_clk_fepll_register(struct clkdom *clkdom,
132     struct qcom_clk_fepll_def *clkdef)
133 {
134 	struct clknode *clk;
135 	struct qcom_clk_fepll_sc *sc;
136 
137 	clk = clknode_create(clkdom, &qcom_clk_fepll_class, &clkdef->clkdef);
138 	if (clk == NULL)
139 		return (1);
140 
141 	sc = clknode_get_softc(clk);
142 	sc->clknode = clk;
143 
144 	sc->offset = clkdef->offset;
145 	sc->fdbkdiv_shift = clkdef->fdbkdiv_shift;
146 	sc->fdbkdiv_width = clkdef->fdbkdiv_width;
147 	sc->refclkdiv_shift = clkdef->refclkdiv_shift;
148 	sc->refclkdiv_width = clkdef->refclkdiv_width;
149 
150 	clknode_register(clkdom, clk);
151 
152 	return (0);
153 }
154