2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2019 Axiado Corporation
7 * This software was developed in part by Kristof Provost under contract for
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
36 #include <sys/systm.h>
38 #include <sys/kernel.h>
40 #include <sys/module.h>
41 #include <sys/mutex.h>
44 #include <machine/bus.h>
45 #include <machine/cpu.h>
47 #include <dev/extres/clk/clk.h>
49 #include <dev/ofw/ofw_bus.h>
50 #include <dev/ofw/ofw_bus_subr.h>
51 #include <dev/ofw/openfirm.h>
53 static struct resource_spec prci_spec[] = {
54 { SYS_RES_MEMORY, 0, RF_ACTIVE },
63 struct clkdom *clkdom;
66 bus_space_handle_t bsh;
69 struct prci_clk_pll_sc {
70 struct prci_softc *parent_sc;
73 #define PRCI_LOCK(sc) mtx_lock(&(sc)->mtx)
74 #define PRCI_UNLOCK(sc) mtx_unlock(&(sc)->mtx)
75 #define PRCI_ASSERT_LOCKED(sc) mtx_assert(&(sc)->mtx, MA_OWNED);
76 #define PRCI_ASSERT_UNLOCKED(sc) mtx_assert(&(sc)->mtx, MA_NOTOWNED);
78 #define PRCI_COREPLL 0x4
79 #define PRCI_COREPLL_DIVR_MASK 0x3f
80 #define PRCI_COREPLL_DIVR_SHIFT 0
81 #define PRCI_COREPLL_DIVF_MASK 0x7fc0
82 #define PRCI_COREPLL_DIVF_SHIFT 6
83 #define PRCI_COREPLL_DIVQ_MASK 0x38000
84 #define PRCI_COREPLL_DIVQ_SHIFT 15
86 #define PRCI_READ(_sc, _reg) \
87 bus_space_read_4((_sc)->bst, (_sc)->bsh, (_reg))
90 prci_clk_pll_init(struct clknode *clk, device_t dev)
93 clknode_init_parent_idx(clk, 0);
99 prci_clk_pll_recalc(struct clknode *clk, uint64_t *freq)
101 struct prci_clk_pll_sc *sc;
102 struct clknode *parent_clk;
104 uint64_t refclk, divf, divq, divr;
107 KASSERT(freq != NULL, ("freq cannot be NULL"));
109 sc = clknode_get_softc(clk);
111 PRCI_LOCK(sc->parent_sc);
113 /* Get refclock frequency. */
114 parent_clk = clknode_get_parent(clk);
115 err = clknode_get_freq(parent_clk, &refclk);
117 device_printf(sc->parent_sc->dev,
118 "Failed to get refclk frequency\n");
119 PRCI_UNLOCK(sc->parent_sc);
123 /* Calculate the PLL output */
124 val = PRCI_READ(sc->parent_sc, PRCI_COREPLL);
126 divf = (val & PRCI_COREPLL_DIVF_MASK) >> PRCI_COREPLL_DIVF_SHIFT;
127 divq = (val & PRCI_COREPLL_DIVQ_MASK) >> PRCI_COREPLL_DIVQ_SHIFT;
128 divr = (val & PRCI_COREPLL_DIVR_MASK) >> PRCI_COREPLL_DIVR_SHIFT;
130 *freq = refclk / (divr + 1) * (2 * (divf + 1)) / (1 << divq);
132 PRCI_UNLOCK(sc->parent_sc);
137 static clknode_method_t prci_clk_pll_clknode_methods[] = {
138 CLKNODEMETHOD(clknode_init, prci_clk_pll_init),
139 CLKNODEMETHOD(clknode_recalc_freq, prci_clk_pll_recalc),
143 DEFINE_CLASS_1(prci_clk_pll_clknode, prci_clk_pll_clknode_class,
144 prci_clk_pll_clknode_methods, sizeof(struct prci_clk_pll_sc),
148 prci_probe(device_t dev)
151 if (!ofw_bus_status_okay(dev))
154 if (!ofw_bus_is_compatible(dev, "sifive,aloeprci0"))
157 device_set_desc(dev, "SiFive FU540 Power Reset Clocking Interrupt");
159 return (BUS_PROBE_DEFAULT);
163 prci_pll_register(struct prci_softc *parent_sc, struct clknode_init_def *clkdef)
166 struct prci_clk_pll_sc *sc;
168 clk = clknode_create(parent_sc->clkdom, &prci_clk_pll_clknode_class,
171 panic("Failed to create clknode");
173 sc = clknode_get_softc(clk);
174 sc->parent_sc = parent_sc;
176 clknode_register(parent_sc->clkdom, clk);
180 prci_attach(device_t dev)
182 struct clknode_init_def clkdef;
183 struct prci_softc *sc;
186 int i, ncells, error;
188 sc = device_get_softc(dev);
191 mtx_init(&sc->mtx, device_get_nameunit(sc->dev), NULL, MTX_DEF);
193 error = bus_alloc_resources(dev, prci_spec, &sc->res);
195 device_printf(dev, "Couldn't allocate resources\n");
198 sc->bst = rman_get_bustag(sc->res);
199 sc->bsh = rman_get_bushandle(sc->res);
201 node = ofw_bus_get_node(dev);
202 error = ofw_bus_parse_xref_list_get_length(node, "clocks",
203 "#clock-cells", &ncells);
204 if (error != 0 || ncells != 1) {
205 device_printf(dev, "couldn't find parent clock\n");
209 bzero(&clkdef, sizeof(clkdef));
211 clkdef.name = "coreclk";
212 clkdef.parent_names = mallocarray(ncells, sizeof(char *), M_OFWPROP,
214 for (i = 0; i < ncells; i++) {
215 error = clk_get_by_ofw_index(dev, 0, i, &clk_parent);
217 device_printf(dev, "cannot get clock %d\n", error);
220 clkdef.parent_names[i] = clk_get_name(clk_parent);
222 device_printf(dev, "clk parent: %s\n",
223 clkdef.parent_names[i]);
224 clk_release(clk_parent);
226 clkdef.parent_cnt = ncells;
228 sc->clkdom = clkdom_create(dev);
229 if (sc->clkdom == NULL) {
230 device_printf(dev, "Couldn't create clock domain\n");
234 /* We can't free a clkdom, so from now on we cannot fail. */
235 prci_pll_register(sc, &clkdef);
237 error = clkdom_finit(sc->clkdom);
239 panic("Couldn't finalise clock domain");
244 free(clkdef.parent_names, M_OFWPROP);
247 bus_release_resources(dev, prci_spec, &sc->res);
248 mtx_destroy(&sc->mtx);
252 static device_method_t prci_methods[] = {
253 DEVMETHOD(device_probe, prci_probe),
254 DEVMETHOD(device_attach, prci_attach),
259 static driver_t prci_driver = {
262 sizeof(struct prci_softc)
265 static devclass_t prci_devclass;
267 EARLY_DRIVER_MODULE(fu540prci, simplebus, prci_driver, prci_devclass, 0, 0,