]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/riscv/sifive/fu540_prci.c
Merge llvm, clang, compiler-rt, libc++, libunwind, lld, lldb and openmp
[FreeBSD/FreeBSD.git] / sys / riscv / sifive / fu540_prci.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2019 Axiado Corporation
5  * All rights reserved.
6  *
7  * This software was developed in part by Kristof Provost under contract for
8  * Axiado Corporation.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
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.
18  *
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
29  * SUCH DAMAGE.
30  */
31
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/bus.h>
38 #include <sys/kernel.h>
39 #include <sys/lock.h>
40 #include <sys/module.h>
41 #include <sys/mutex.h>
42 #include <sys/rman.h>
43
44 #include <machine/bus.h>
45 #include <machine/cpu.h>
46
47 #include <dev/extres/clk/clk.h>
48
49 #include <dev/ofw/ofw_bus.h>
50 #include <dev/ofw/ofw_bus_subr.h>
51 #include <dev/ofw/openfirm.h>
52
53 static struct resource_spec prci_spec[] = {
54         { SYS_RES_MEMORY, 0, RF_ACTIVE },
55         RESOURCE_SPEC_END
56 };
57
58 struct prci_softc {
59         device_t                dev;
60
61         struct mtx              mtx;
62
63         struct clkdom           *clkdom;
64         struct resource         *res;
65         bus_space_tag_t         bst;
66         bus_space_handle_t      bsh;
67 };
68
69 struct prci_clk_pll_sc {
70         struct prci_softc       *parent_sc;
71 };
72
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);
77
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
85
86 #define PRCI_READ(_sc, _reg)            \
87     bus_space_read_4((_sc)->bst, (_sc)->bsh, (_reg))
88
89 static int
90 prci_clk_pll_init(struct clknode *clk, device_t dev)
91 {
92
93         clknode_init_parent_idx(clk, 0);
94
95         return (0);
96 }
97
98 static int
99 prci_clk_pll_recalc(struct clknode *clk, uint64_t *freq)
100 {
101         struct prci_clk_pll_sc *sc;
102         struct clknode *parent_clk;
103         uint32_t val;
104         uint64_t refclk, divf, divq, divr;
105         int err;
106
107         KASSERT(freq != NULL, ("freq cannot be NULL"));
108
109         sc = clknode_get_softc(clk);
110
111         PRCI_LOCK(sc->parent_sc);
112
113         /* Get refclock frequency. */
114         parent_clk = clknode_get_parent(clk);
115         err = clknode_get_freq(parent_clk, &refclk);
116         if (err) {
117                 device_printf(sc->parent_sc->dev,
118                     "Failed to get refclk frequency\n");
119                 PRCI_UNLOCK(sc->parent_sc);
120                 return (err);
121         }
122
123         /* Calculate the PLL output */
124         val = PRCI_READ(sc->parent_sc, PRCI_COREPLL);
125
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;
129
130         *freq = refclk / (divr + 1) * (2 * (divf + 1)) / (1 << divq);
131
132         PRCI_UNLOCK(sc->parent_sc);
133
134         return (0);
135 }
136
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),
140         CLKNODEMETHOD_END
141 };
142
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),
145     clknode_class);
146
147 static int
148 prci_probe(device_t dev)
149 {
150
151         if (!ofw_bus_status_okay(dev))
152                 return (ENXIO);
153
154         if (!ofw_bus_is_compatible(dev, "sifive,aloeprci0"))
155                 return (ENXIO);
156
157         device_set_desc(dev, "SiFive FU540 Power Reset Clocking Interrupt");
158
159         return (BUS_PROBE_DEFAULT);
160 }
161
162 static void
163 prci_pll_register(struct prci_softc *parent_sc, struct clknode_init_def *clkdef)
164 {
165         struct clknode *clk;
166         struct prci_clk_pll_sc *sc;
167
168         clk = clknode_create(parent_sc->clkdom, &prci_clk_pll_clknode_class,
169             clkdef);
170         if (clk == NULL)
171                 panic("Failed to create clknode");
172
173         sc = clknode_get_softc(clk);
174         sc->parent_sc = parent_sc;
175
176         clknode_register(parent_sc->clkdom, clk);
177 }
178
179 static int
180 prci_attach(device_t dev)
181 {
182         struct clknode_init_def clkdef;
183         struct prci_softc *sc;
184         clk_t clk_parent;
185         phandle_t node;
186         int i, ncells, error;
187
188         sc = device_get_softc(dev);
189         sc->dev = dev;
190
191         mtx_init(&sc->mtx, device_get_nameunit(sc->dev), NULL, MTX_DEF);
192
193         error = bus_alloc_resources(dev, prci_spec, &sc->res);
194         if (error) {
195                 device_printf(dev, "Couldn't allocate resources\n");
196                 goto fail;
197         }
198         sc->bst = rman_get_bustag(sc->res);
199         sc->bsh = rman_get_bushandle(sc->res);
200
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");
206                 goto fail;
207         }
208
209         bzero(&clkdef, sizeof(clkdef));
210         clkdef.id = 0;
211         clkdef.name = "coreclk";
212         clkdef.parent_names = mallocarray(ncells, sizeof(char *), M_OFWPROP,
213             M_WAITOK);
214         for (i = 0; i < ncells; i++) {
215                 error = clk_get_by_ofw_index(dev, 0, i, &clk_parent);
216                 if (error != 0) {
217                         device_printf(dev, "cannot get clock %d\n", error);
218                         goto fail1;
219                 }
220                 clkdef.parent_names[i] = clk_get_name(clk_parent);
221                 if (bootverbose)
222                         device_printf(dev, "clk parent: %s\n",
223                             clkdef.parent_names[i]);
224                 clk_release(clk_parent);
225         }
226         clkdef.parent_cnt = ncells;
227
228         sc->clkdom = clkdom_create(dev);
229         if (sc->clkdom == NULL) {
230                 device_printf(dev, "Couldn't create clock domain\n");
231                 goto fail;
232         }
233
234         /* We can't free a clkdom, so from now on we cannot fail. */
235         prci_pll_register(sc, &clkdef);
236
237         error = clkdom_finit(sc->clkdom);
238         if (error)
239                 panic("Couldn't finalise clock domain");
240
241         return (0);
242
243 fail1:
244         free(clkdef.parent_names, M_OFWPROP);
245
246 fail:
247         bus_release_resources(dev, prci_spec, &sc->res);
248         mtx_destroy(&sc->mtx);
249         return (error);
250 }
251
252 static device_method_t prci_methods[] = {
253         DEVMETHOD(device_probe,         prci_probe),
254         DEVMETHOD(device_attach,        prci_attach),
255
256         DEVMETHOD_END
257 };
258
259 static driver_t prci_driver = {
260         "fu540prci",
261         prci_methods,
262         sizeof(struct prci_softc)
263 };
264
265 static devclass_t prci_devclass;
266
267 EARLY_DRIVER_MODULE(fu540prci, simplebus, prci_driver, prci_devclass, 0, 0,
268     BUS_PASS_BUS);