1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Copyright (C) 2018-2019 SiFive, Inc.
4*4882a593Smuzhiyun * Wesley Terpstra
5*4882a593Smuzhiyun * Paul Walmsley
6*4882a593Smuzhiyun *
7*4882a593Smuzhiyun * This program is free software; you can redistribute it and/or modify
8*4882a593Smuzhiyun * it under the terms of the GNU General Public License version 2 as
9*4882a593Smuzhiyun * published by the Free Software Foundation.
10*4882a593Smuzhiyun *
11*4882a593Smuzhiyun * This program is distributed in the hope that it will be useful,
12*4882a593Smuzhiyun * but WITHOUT ANY WARRANTY; without even the implied warranty of
13*4882a593Smuzhiyun * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14*4882a593Smuzhiyun * GNU General Public License for more details.
15*4882a593Smuzhiyun *
16*4882a593Smuzhiyun * The FU540 PRCI implements clock and reset control for the SiFive
17*4882a593Smuzhiyun * FU540-C000 chip. This driver assumes that it has sole control
18*4882a593Smuzhiyun * over all PRCI resources.
19*4882a593Smuzhiyun *
20*4882a593Smuzhiyun * This driver is based on the PRCI driver written by Wesley Terpstra:
21*4882a593Smuzhiyun * https://github.com/riscv/riscv-linux/commit/999529edf517ed75b56659d456d221b2ee56bb60
22*4882a593Smuzhiyun *
23*4882a593Smuzhiyun * References:
24*4882a593Smuzhiyun * - SiFive FU540-C000 manual v1p0, Chapter 7 "Clocking and Reset"
25*4882a593Smuzhiyun */
26*4882a593Smuzhiyun
27*4882a593Smuzhiyun #include <dt-bindings/clock/sifive-fu540-prci.h>
28*4882a593Smuzhiyun #include <linux/clkdev.h>
29*4882a593Smuzhiyun #include <linux/clk-provider.h>
30*4882a593Smuzhiyun #include <linux/clk/analogbits-wrpll-cln28hpc.h>
31*4882a593Smuzhiyun #include <linux/delay.h>
32*4882a593Smuzhiyun #include <linux/err.h>
33*4882a593Smuzhiyun #include <linux/io.h>
34*4882a593Smuzhiyun #include <linux/module.h>
35*4882a593Smuzhiyun #include <linux/of.h>
36*4882a593Smuzhiyun #include <linux/of_clk.h>
37*4882a593Smuzhiyun #include <linux/platform_device.h>
38*4882a593Smuzhiyun #include <linux/slab.h>
39*4882a593Smuzhiyun
40*4882a593Smuzhiyun /*
41*4882a593Smuzhiyun * EXPECTED_CLK_PARENT_COUNT: how many parent clocks this driver expects:
42*4882a593Smuzhiyun * hfclk and rtcclk
43*4882a593Smuzhiyun */
44*4882a593Smuzhiyun #define EXPECTED_CLK_PARENT_COUNT 2
45*4882a593Smuzhiyun
46*4882a593Smuzhiyun /*
47*4882a593Smuzhiyun * Register offsets and bitmasks
48*4882a593Smuzhiyun */
49*4882a593Smuzhiyun
50*4882a593Smuzhiyun /* COREPLLCFG0 */
51*4882a593Smuzhiyun #define PRCI_COREPLLCFG0_OFFSET 0x4
52*4882a593Smuzhiyun # define PRCI_COREPLLCFG0_DIVR_SHIFT 0
53*4882a593Smuzhiyun # define PRCI_COREPLLCFG0_DIVR_MASK (0x3f << PRCI_COREPLLCFG0_DIVR_SHIFT)
54*4882a593Smuzhiyun # define PRCI_COREPLLCFG0_DIVF_SHIFT 6
55*4882a593Smuzhiyun # define PRCI_COREPLLCFG0_DIVF_MASK (0x1ff << PRCI_COREPLLCFG0_DIVF_SHIFT)
56*4882a593Smuzhiyun # define PRCI_COREPLLCFG0_DIVQ_SHIFT 15
57*4882a593Smuzhiyun # define PRCI_COREPLLCFG0_DIVQ_MASK (0x7 << PRCI_COREPLLCFG0_DIVQ_SHIFT)
58*4882a593Smuzhiyun # define PRCI_COREPLLCFG0_RANGE_SHIFT 18
59*4882a593Smuzhiyun # define PRCI_COREPLLCFG0_RANGE_MASK (0x7 << PRCI_COREPLLCFG0_RANGE_SHIFT)
60*4882a593Smuzhiyun # define PRCI_COREPLLCFG0_BYPASS_SHIFT 24
61*4882a593Smuzhiyun # define PRCI_COREPLLCFG0_BYPASS_MASK (0x1 << PRCI_COREPLLCFG0_BYPASS_SHIFT)
62*4882a593Smuzhiyun # define PRCI_COREPLLCFG0_FSE_SHIFT 25
63*4882a593Smuzhiyun # define PRCI_COREPLLCFG0_FSE_MASK (0x1 << PRCI_COREPLLCFG0_FSE_SHIFT)
64*4882a593Smuzhiyun # define PRCI_COREPLLCFG0_LOCK_SHIFT 31
65*4882a593Smuzhiyun # define PRCI_COREPLLCFG0_LOCK_MASK (0x1 << PRCI_COREPLLCFG0_LOCK_SHIFT)
66*4882a593Smuzhiyun
67*4882a593Smuzhiyun /* DDRPLLCFG0 */
68*4882a593Smuzhiyun #define PRCI_DDRPLLCFG0_OFFSET 0xc
69*4882a593Smuzhiyun # define PRCI_DDRPLLCFG0_DIVR_SHIFT 0
70*4882a593Smuzhiyun # define PRCI_DDRPLLCFG0_DIVR_MASK (0x3f << PRCI_DDRPLLCFG0_DIVR_SHIFT)
71*4882a593Smuzhiyun # define PRCI_DDRPLLCFG0_DIVF_SHIFT 6
72*4882a593Smuzhiyun # define PRCI_DDRPLLCFG0_DIVF_MASK (0x1ff << PRCI_DDRPLLCFG0_DIVF_SHIFT)
73*4882a593Smuzhiyun # define PRCI_DDRPLLCFG0_DIVQ_SHIFT 15
74*4882a593Smuzhiyun # define PRCI_DDRPLLCFG0_DIVQ_MASK (0x7 << PRCI_DDRPLLCFG0_DIVQ_SHIFT)
75*4882a593Smuzhiyun # define PRCI_DDRPLLCFG0_RANGE_SHIFT 18
76*4882a593Smuzhiyun # define PRCI_DDRPLLCFG0_RANGE_MASK (0x7 << PRCI_DDRPLLCFG0_RANGE_SHIFT)
77*4882a593Smuzhiyun # define PRCI_DDRPLLCFG0_BYPASS_SHIFT 24
78*4882a593Smuzhiyun # define PRCI_DDRPLLCFG0_BYPASS_MASK (0x1 << PRCI_DDRPLLCFG0_BYPASS_SHIFT)
79*4882a593Smuzhiyun # define PRCI_DDRPLLCFG0_FSE_SHIFT 25
80*4882a593Smuzhiyun # define PRCI_DDRPLLCFG0_FSE_MASK (0x1 << PRCI_DDRPLLCFG0_FSE_SHIFT)
81*4882a593Smuzhiyun # define PRCI_DDRPLLCFG0_LOCK_SHIFT 31
82*4882a593Smuzhiyun # define PRCI_DDRPLLCFG0_LOCK_MASK (0x1 << PRCI_DDRPLLCFG0_LOCK_SHIFT)
83*4882a593Smuzhiyun
84*4882a593Smuzhiyun /* DDRPLLCFG1 */
85*4882a593Smuzhiyun #define PRCI_DDRPLLCFG1_OFFSET 0x10
86*4882a593Smuzhiyun # define PRCI_DDRPLLCFG1_CKE_SHIFT 24
87*4882a593Smuzhiyun # define PRCI_DDRPLLCFG1_CKE_MASK (0x1 << PRCI_DDRPLLCFG1_CKE_SHIFT)
88*4882a593Smuzhiyun
89*4882a593Smuzhiyun /* GEMGXLPLLCFG0 */
90*4882a593Smuzhiyun #define PRCI_GEMGXLPLLCFG0_OFFSET 0x1c
91*4882a593Smuzhiyun # define PRCI_GEMGXLPLLCFG0_DIVR_SHIFT 0
92*4882a593Smuzhiyun # define PRCI_GEMGXLPLLCFG0_DIVR_MASK (0x3f << PRCI_GEMGXLPLLCFG0_DIVR_SHIFT)
93*4882a593Smuzhiyun # define PRCI_GEMGXLPLLCFG0_DIVF_SHIFT 6
94*4882a593Smuzhiyun # define PRCI_GEMGXLPLLCFG0_DIVF_MASK (0x1ff << PRCI_GEMGXLPLLCFG0_DIVF_SHIFT)
95*4882a593Smuzhiyun # define PRCI_GEMGXLPLLCFG0_DIVQ_SHIFT 15
96*4882a593Smuzhiyun # define PRCI_GEMGXLPLLCFG0_DIVQ_MASK (0x7 << PRCI_GEMGXLPLLCFG0_DIVQ_SHIFT)
97*4882a593Smuzhiyun # define PRCI_GEMGXLPLLCFG0_RANGE_SHIFT 18
98*4882a593Smuzhiyun # define PRCI_GEMGXLPLLCFG0_RANGE_MASK (0x7 << PRCI_GEMGXLPLLCFG0_RANGE_SHIFT)
99*4882a593Smuzhiyun # define PRCI_GEMGXLPLLCFG0_BYPASS_SHIFT 24
100*4882a593Smuzhiyun # define PRCI_GEMGXLPLLCFG0_BYPASS_MASK (0x1 << PRCI_GEMGXLPLLCFG0_BYPASS_SHIFT)
101*4882a593Smuzhiyun # define PRCI_GEMGXLPLLCFG0_FSE_SHIFT 25
102*4882a593Smuzhiyun # define PRCI_GEMGXLPLLCFG0_FSE_MASK (0x1 << PRCI_GEMGXLPLLCFG0_FSE_SHIFT)
103*4882a593Smuzhiyun # define PRCI_GEMGXLPLLCFG0_LOCK_SHIFT 31
104*4882a593Smuzhiyun # define PRCI_GEMGXLPLLCFG0_LOCK_MASK (0x1 << PRCI_GEMGXLPLLCFG0_LOCK_SHIFT)
105*4882a593Smuzhiyun
106*4882a593Smuzhiyun /* GEMGXLPLLCFG1 */
107*4882a593Smuzhiyun #define PRCI_GEMGXLPLLCFG1_OFFSET 0x20
108*4882a593Smuzhiyun # define PRCI_GEMGXLPLLCFG1_CKE_SHIFT 24
109*4882a593Smuzhiyun # define PRCI_GEMGXLPLLCFG1_CKE_MASK (0x1 << PRCI_GEMGXLPLLCFG1_CKE_SHIFT)
110*4882a593Smuzhiyun
111*4882a593Smuzhiyun /* CORECLKSEL */
112*4882a593Smuzhiyun #define PRCI_CORECLKSEL_OFFSET 0x24
113*4882a593Smuzhiyun # define PRCI_CORECLKSEL_CORECLKSEL_SHIFT 0
114*4882a593Smuzhiyun # define PRCI_CORECLKSEL_CORECLKSEL_MASK (0x1 << PRCI_CORECLKSEL_CORECLKSEL_SHIFT)
115*4882a593Smuzhiyun
116*4882a593Smuzhiyun /* DEVICESRESETREG */
117*4882a593Smuzhiyun #define PRCI_DEVICESRESETREG_OFFSET 0x28
118*4882a593Smuzhiyun # define PRCI_DEVICESRESETREG_DDR_CTRL_RST_N_SHIFT 0
119*4882a593Smuzhiyun # define PRCI_DEVICESRESETREG_DDR_CTRL_RST_N_MASK (0x1 << PRCI_DEVICESRESETREG_DDR_CTRL_RST_N_SHIFT)
120*4882a593Smuzhiyun # define PRCI_DEVICESRESETREG_DDR_AXI_RST_N_SHIFT 1
121*4882a593Smuzhiyun # define PRCI_DEVICESRESETREG_DDR_AXI_RST_N_MASK (0x1 << PRCI_DEVICESRESETREG_DDR_AXI_RST_N_SHIFT)
122*4882a593Smuzhiyun # define PRCI_DEVICESRESETREG_DDR_AHB_RST_N_SHIFT 2
123*4882a593Smuzhiyun # define PRCI_DEVICESRESETREG_DDR_AHB_RST_N_MASK (0x1 << PRCI_DEVICESRESETREG_DDR_AHB_RST_N_SHIFT)
124*4882a593Smuzhiyun # define PRCI_DEVICESRESETREG_DDR_PHY_RST_N_SHIFT 3
125*4882a593Smuzhiyun # define PRCI_DEVICESRESETREG_DDR_PHY_RST_N_MASK (0x1 << PRCI_DEVICESRESETREG_DDR_PHY_RST_N_SHIFT)
126*4882a593Smuzhiyun # define PRCI_DEVICESRESETREG_GEMGXL_RST_N_SHIFT 5
127*4882a593Smuzhiyun # define PRCI_DEVICESRESETREG_GEMGXL_RST_N_MASK (0x1 << PRCI_DEVICESRESETREG_GEMGXL_RST_N_SHIFT)
128*4882a593Smuzhiyun
129*4882a593Smuzhiyun /* CLKMUXSTATUSREG */
130*4882a593Smuzhiyun #define PRCI_CLKMUXSTATUSREG_OFFSET 0x2c
131*4882a593Smuzhiyun # define PRCI_CLKMUXSTATUSREG_TLCLKSEL_STATUS_SHIFT 1
132*4882a593Smuzhiyun # define PRCI_CLKMUXSTATUSREG_TLCLKSEL_STATUS_MASK (0x1 << PRCI_CLKMUXSTATUSREG_TLCLKSEL_STATUS_SHIFT)
133*4882a593Smuzhiyun
134*4882a593Smuzhiyun /*
135*4882a593Smuzhiyun * Private structures
136*4882a593Smuzhiyun */
137*4882a593Smuzhiyun
138*4882a593Smuzhiyun /**
139*4882a593Smuzhiyun * struct __prci_data - per-device-instance data
140*4882a593Smuzhiyun * @va: base virtual address of the PRCI IP block
141*4882a593Smuzhiyun * @hw_clks: encapsulates struct clk_hw records
142*4882a593Smuzhiyun *
143*4882a593Smuzhiyun * PRCI per-device instance data
144*4882a593Smuzhiyun */
145*4882a593Smuzhiyun struct __prci_data {
146*4882a593Smuzhiyun void __iomem *va;
147*4882a593Smuzhiyun struct clk_hw_onecell_data hw_clks;
148*4882a593Smuzhiyun };
149*4882a593Smuzhiyun
150*4882a593Smuzhiyun /**
151*4882a593Smuzhiyun * struct __prci_wrpll_data - WRPLL configuration and integration data
152*4882a593Smuzhiyun * @c: WRPLL current configuration record
153*4882a593Smuzhiyun * @enable_bypass: fn ptr to code to bypass the WRPLL (if applicable; else NULL)
154*4882a593Smuzhiyun * @disable_bypass: fn ptr to code to not bypass the WRPLL (or NULL)
155*4882a593Smuzhiyun * @cfg0_offs: WRPLL CFG0 register offset (in bytes) from the PRCI base address
156*4882a593Smuzhiyun *
157*4882a593Smuzhiyun * @enable_bypass and @disable_bypass are used for WRPLL instances
158*4882a593Smuzhiyun * that contain a separate external glitchless clock mux downstream
159*4882a593Smuzhiyun * from the PLL. The WRPLL internal bypass mux is not glitchless.
160*4882a593Smuzhiyun */
161*4882a593Smuzhiyun struct __prci_wrpll_data {
162*4882a593Smuzhiyun struct wrpll_cfg c;
163*4882a593Smuzhiyun void (*enable_bypass)(struct __prci_data *pd);
164*4882a593Smuzhiyun void (*disable_bypass)(struct __prci_data *pd);
165*4882a593Smuzhiyun u8 cfg0_offs;
166*4882a593Smuzhiyun };
167*4882a593Smuzhiyun
168*4882a593Smuzhiyun /**
169*4882a593Smuzhiyun * struct __prci_clock - describes a clock device managed by PRCI
170*4882a593Smuzhiyun * @name: user-readable clock name string - should match the manual
171*4882a593Smuzhiyun * @parent_name: parent name for this clock
172*4882a593Smuzhiyun * @ops: struct clk_ops for the Linux clock framework to use for control
173*4882a593Smuzhiyun * @hw: Linux-private clock data
174*4882a593Smuzhiyun * @pwd: WRPLL-specific data, associated with this clock (if not NULL)
175*4882a593Smuzhiyun * @pd: PRCI-specific data associated with this clock (if not NULL)
176*4882a593Smuzhiyun *
177*4882a593Smuzhiyun * PRCI clock data. Used by the PRCI driver to register PRCI-provided
178*4882a593Smuzhiyun * clocks to the Linux clock infrastructure.
179*4882a593Smuzhiyun */
180*4882a593Smuzhiyun struct __prci_clock {
181*4882a593Smuzhiyun const char *name;
182*4882a593Smuzhiyun const char *parent_name;
183*4882a593Smuzhiyun const struct clk_ops *ops;
184*4882a593Smuzhiyun struct clk_hw hw;
185*4882a593Smuzhiyun struct __prci_wrpll_data *pwd;
186*4882a593Smuzhiyun struct __prci_data *pd;
187*4882a593Smuzhiyun };
188*4882a593Smuzhiyun
189*4882a593Smuzhiyun #define clk_hw_to_prci_clock(pwd) container_of(pwd, struct __prci_clock, hw)
190*4882a593Smuzhiyun
191*4882a593Smuzhiyun /*
192*4882a593Smuzhiyun * Private functions
193*4882a593Smuzhiyun */
194*4882a593Smuzhiyun
195*4882a593Smuzhiyun /**
196*4882a593Smuzhiyun * __prci_readl() - read from a PRCI register
197*4882a593Smuzhiyun * @pd: PRCI context
198*4882a593Smuzhiyun * @offs: register offset to read from (in bytes, from PRCI base address)
199*4882a593Smuzhiyun *
200*4882a593Smuzhiyun * Read the register located at offset @offs from the base virtual
201*4882a593Smuzhiyun * address of the PRCI register target described by @pd, and return
202*4882a593Smuzhiyun * the value to the caller.
203*4882a593Smuzhiyun *
204*4882a593Smuzhiyun * Context: Any context.
205*4882a593Smuzhiyun *
206*4882a593Smuzhiyun * Return: the contents of the register described by @pd and @offs.
207*4882a593Smuzhiyun */
__prci_readl(struct __prci_data * pd,u32 offs)208*4882a593Smuzhiyun static u32 __prci_readl(struct __prci_data *pd, u32 offs)
209*4882a593Smuzhiyun {
210*4882a593Smuzhiyun return readl_relaxed(pd->va + offs);
211*4882a593Smuzhiyun }
212*4882a593Smuzhiyun
__prci_writel(u32 v,u32 offs,struct __prci_data * pd)213*4882a593Smuzhiyun static void __prci_writel(u32 v, u32 offs, struct __prci_data *pd)
214*4882a593Smuzhiyun {
215*4882a593Smuzhiyun writel_relaxed(v, pd->va + offs);
216*4882a593Smuzhiyun }
217*4882a593Smuzhiyun
218*4882a593Smuzhiyun /* WRPLL-related private functions */
219*4882a593Smuzhiyun
220*4882a593Smuzhiyun /**
221*4882a593Smuzhiyun * __prci_wrpll_unpack() - unpack WRPLL configuration registers into parameters
222*4882a593Smuzhiyun * @c: ptr to a struct wrpll_cfg record to write config into
223*4882a593Smuzhiyun * @r: value read from the PRCI PLL configuration register
224*4882a593Smuzhiyun *
225*4882a593Smuzhiyun * Given a value @r read from an FU540 PRCI PLL configuration register,
226*4882a593Smuzhiyun * split it into fields and populate it into the WRPLL configuration record
227*4882a593Smuzhiyun * pointed to by @c.
228*4882a593Smuzhiyun *
229*4882a593Smuzhiyun * The COREPLLCFG0 macros are used below, but the other *PLLCFG0 macros
230*4882a593Smuzhiyun * have the same register layout.
231*4882a593Smuzhiyun *
232*4882a593Smuzhiyun * Context: Any context.
233*4882a593Smuzhiyun */
__prci_wrpll_unpack(struct wrpll_cfg * c,u32 r)234*4882a593Smuzhiyun static void __prci_wrpll_unpack(struct wrpll_cfg *c, u32 r)
235*4882a593Smuzhiyun {
236*4882a593Smuzhiyun u32 v;
237*4882a593Smuzhiyun
238*4882a593Smuzhiyun v = r & PRCI_COREPLLCFG0_DIVR_MASK;
239*4882a593Smuzhiyun v >>= PRCI_COREPLLCFG0_DIVR_SHIFT;
240*4882a593Smuzhiyun c->divr = v;
241*4882a593Smuzhiyun
242*4882a593Smuzhiyun v = r & PRCI_COREPLLCFG0_DIVF_MASK;
243*4882a593Smuzhiyun v >>= PRCI_COREPLLCFG0_DIVF_SHIFT;
244*4882a593Smuzhiyun c->divf = v;
245*4882a593Smuzhiyun
246*4882a593Smuzhiyun v = r & PRCI_COREPLLCFG0_DIVQ_MASK;
247*4882a593Smuzhiyun v >>= PRCI_COREPLLCFG0_DIVQ_SHIFT;
248*4882a593Smuzhiyun c->divq = v;
249*4882a593Smuzhiyun
250*4882a593Smuzhiyun v = r & PRCI_COREPLLCFG0_RANGE_MASK;
251*4882a593Smuzhiyun v >>= PRCI_COREPLLCFG0_RANGE_SHIFT;
252*4882a593Smuzhiyun c->range = v;
253*4882a593Smuzhiyun
254*4882a593Smuzhiyun c->flags &= (WRPLL_FLAGS_INT_FEEDBACK_MASK |
255*4882a593Smuzhiyun WRPLL_FLAGS_EXT_FEEDBACK_MASK);
256*4882a593Smuzhiyun
257*4882a593Smuzhiyun /* external feedback mode not supported */
258*4882a593Smuzhiyun c->flags |= WRPLL_FLAGS_INT_FEEDBACK_MASK;
259*4882a593Smuzhiyun }
260*4882a593Smuzhiyun
261*4882a593Smuzhiyun /**
262*4882a593Smuzhiyun * __prci_wrpll_pack() - pack PLL configuration parameters into a register value
263*4882a593Smuzhiyun * @c: pointer to a struct wrpll_cfg record containing the PLL's cfg
264*4882a593Smuzhiyun *
265*4882a593Smuzhiyun * Using a set of WRPLL configuration values pointed to by @c,
266*4882a593Smuzhiyun * assemble a PRCI PLL configuration register value, and return it to
267*4882a593Smuzhiyun * the caller.
268*4882a593Smuzhiyun *
269*4882a593Smuzhiyun * Context: Any context. Caller must ensure that the contents of the
270*4882a593Smuzhiyun * record pointed to by @c do not change during the execution
271*4882a593Smuzhiyun * of this function.
272*4882a593Smuzhiyun *
273*4882a593Smuzhiyun * Returns: a value suitable for writing into a PRCI PLL configuration
274*4882a593Smuzhiyun * register
275*4882a593Smuzhiyun */
__prci_wrpll_pack(const struct wrpll_cfg * c)276*4882a593Smuzhiyun static u32 __prci_wrpll_pack(const struct wrpll_cfg *c)
277*4882a593Smuzhiyun {
278*4882a593Smuzhiyun u32 r = 0;
279*4882a593Smuzhiyun
280*4882a593Smuzhiyun r |= c->divr << PRCI_COREPLLCFG0_DIVR_SHIFT;
281*4882a593Smuzhiyun r |= c->divf << PRCI_COREPLLCFG0_DIVF_SHIFT;
282*4882a593Smuzhiyun r |= c->divq << PRCI_COREPLLCFG0_DIVQ_SHIFT;
283*4882a593Smuzhiyun r |= c->range << PRCI_COREPLLCFG0_RANGE_SHIFT;
284*4882a593Smuzhiyun
285*4882a593Smuzhiyun /* external feedback mode not supported */
286*4882a593Smuzhiyun r |= PRCI_COREPLLCFG0_FSE_MASK;
287*4882a593Smuzhiyun
288*4882a593Smuzhiyun return r;
289*4882a593Smuzhiyun }
290*4882a593Smuzhiyun
291*4882a593Smuzhiyun /**
292*4882a593Smuzhiyun * __prci_wrpll_read_cfg() - read the WRPLL configuration from the PRCI
293*4882a593Smuzhiyun * @pd: PRCI context
294*4882a593Smuzhiyun * @pwd: PRCI WRPLL metadata
295*4882a593Smuzhiyun *
296*4882a593Smuzhiyun * Read the current configuration of the PLL identified by @pwd from
297*4882a593Smuzhiyun * the PRCI identified by @pd, and store it into the local configuration
298*4882a593Smuzhiyun * cache in @pwd.
299*4882a593Smuzhiyun *
300*4882a593Smuzhiyun * Context: Any context. Caller must prevent the records pointed to by
301*4882a593Smuzhiyun * @pd and @pwd from changing during execution.
302*4882a593Smuzhiyun */
__prci_wrpll_read_cfg(struct __prci_data * pd,struct __prci_wrpll_data * pwd)303*4882a593Smuzhiyun static void __prci_wrpll_read_cfg(struct __prci_data *pd,
304*4882a593Smuzhiyun struct __prci_wrpll_data *pwd)
305*4882a593Smuzhiyun {
306*4882a593Smuzhiyun __prci_wrpll_unpack(&pwd->c, __prci_readl(pd, pwd->cfg0_offs));
307*4882a593Smuzhiyun }
308*4882a593Smuzhiyun
309*4882a593Smuzhiyun /**
310*4882a593Smuzhiyun * __prci_wrpll_write_cfg() - write WRPLL configuration into the PRCI
311*4882a593Smuzhiyun * @pd: PRCI context
312*4882a593Smuzhiyun * @pwd: PRCI WRPLL metadata
313*4882a593Smuzhiyun * @c: WRPLL configuration record to write
314*4882a593Smuzhiyun *
315*4882a593Smuzhiyun * Write the WRPLL configuration described by @c into the WRPLL
316*4882a593Smuzhiyun * configuration register identified by @pwd in the PRCI instance
317*4882a593Smuzhiyun * described by @c. Make a cached copy of the WRPLL's current
318*4882a593Smuzhiyun * configuration so it can be used by other code.
319*4882a593Smuzhiyun *
320*4882a593Smuzhiyun * Context: Any context. Caller must prevent the records pointed to by
321*4882a593Smuzhiyun * @pd and @pwd from changing during execution.
322*4882a593Smuzhiyun */
__prci_wrpll_write_cfg(struct __prci_data * pd,struct __prci_wrpll_data * pwd,struct wrpll_cfg * c)323*4882a593Smuzhiyun static void __prci_wrpll_write_cfg(struct __prci_data *pd,
324*4882a593Smuzhiyun struct __prci_wrpll_data *pwd,
325*4882a593Smuzhiyun struct wrpll_cfg *c)
326*4882a593Smuzhiyun {
327*4882a593Smuzhiyun __prci_writel(__prci_wrpll_pack(c), pwd->cfg0_offs, pd);
328*4882a593Smuzhiyun
329*4882a593Smuzhiyun memcpy(&pwd->c, c, sizeof(*c));
330*4882a593Smuzhiyun }
331*4882a593Smuzhiyun
332*4882a593Smuzhiyun /* Core clock mux control */
333*4882a593Smuzhiyun
334*4882a593Smuzhiyun /**
335*4882a593Smuzhiyun * __prci_coreclksel_use_hfclk() - switch the CORECLK mux to output HFCLK
336*4882a593Smuzhiyun * @pd: struct __prci_data * for the PRCI containing the CORECLK mux reg
337*4882a593Smuzhiyun *
338*4882a593Smuzhiyun * Switch the CORECLK mux to the HFCLK input source; return once complete.
339*4882a593Smuzhiyun *
340*4882a593Smuzhiyun * Context: Any context. Caller must prevent concurrent changes to the
341*4882a593Smuzhiyun * PRCI_CORECLKSEL_OFFSET register.
342*4882a593Smuzhiyun */
__prci_coreclksel_use_hfclk(struct __prci_data * pd)343*4882a593Smuzhiyun static void __prci_coreclksel_use_hfclk(struct __prci_data *pd)
344*4882a593Smuzhiyun {
345*4882a593Smuzhiyun u32 r;
346*4882a593Smuzhiyun
347*4882a593Smuzhiyun r = __prci_readl(pd, PRCI_CORECLKSEL_OFFSET);
348*4882a593Smuzhiyun r |= PRCI_CORECLKSEL_CORECLKSEL_MASK;
349*4882a593Smuzhiyun __prci_writel(r, PRCI_CORECLKSEL_OFFSET, pd);
350*4882a593Smuzhiyun
351*4882a593Smuzhiyun r = __prci_readl(pd, PRCI_CORECLKSEL_OFFSET); /* barrier */
352*4882a593Smuzhiyun }
353*4882a593Smuzhiyun
354*4882a593Smuzhiyun /**
355*4882a593Smuzhiyun * __prci_coreclksel_use_corepll() - switch the CORECLK mux to output COREPLL
356*4882a593Smuzhiyun * @pd: struct __prci_data * for the PRCI containing the CORECLK mux reg
357*4882a593Smuzhiyun *
358*4882a593Smuzhiyun * Switch the CORECLK mux to the PLL output clock; return once complete.
359*4882a593Smuzhiyun *
360*4882a593Smuzhiyun * Context: Any context. Caller must prevent concurrent changes to the
361*4882a593Smuzhiyun * PRCI_CORECLKSEL_OFFSET register.
362*4882a593Smuzhiyun */
__prci_coreclksel_use_corepll(struct __prci_data * pd)363*4882a593Smuzhiyun static void __prci_coreclksel_use_corepll(struct __prci_data *pd)
364*4882a593Smuzhiyun {
365*4882a593Smuzhiyun u32 r;
366*4882a593Smuzhiyun
367*4882a593Smuzhiyun r = __prci_readl(pd, PRCI_CORECLKSEL_OFFSET);
368*4882a593Smuzhiyun r &= ~PRCI_CORECLKSEL_CORECLKSEL_MASK;
369*4882a593Smuzhiyun __prci_writel(r, PRCI_CORECLKSEL_OFFSET, pd);
370*4882a593Smuzhiyun
371*4882a593Smuzhiyun r = __prci_readl(pd, PRCI_CORECLKSEL_OFFSET); /* barrier */
372*4882a593Smuzhiyun }
373*4882a593Smuzhiyun
374*4882a593Smuzhiyun /*
375*4882a593Smuzhiyun * Linux clock framework integration
376*4882a593Smuzhiyun *
377*4882a593Smuzhiyun * See the Linux clock framework documentation for more information on
378*4882a593Smuzhiyun * these functions.
379*4882a593Smuzhiyun */
380*4882a593Smuzhiyun
sifive_fu540_prci_wrpll_recalc_rate(struct clk_hw * hw,unsigned long parent_rate)381*4882a593Smuzhiyun static unsigned long sifive_fu540_prci_wrpll_recalc_rate(struct clk_hw *hw,
382*4882a593Smuzhiyun unsigned long parent_rate)
383*4882a593Smuzhiyun {
384*4882a593Smuzhiyun struct __prci_clock *pc = clk_hw_to_prci_clock(hw);
385*4882a593Smuzhiyun struct __prci_wrpll_data *pwd = pc->pwd;
386*4882a593Smuzhiyun
387*4882a593Smuzhiyun return wrpll_calc_output_rate(&pwd->c, parent_rate);
388*4882a593Smuzhiyun }
389*4882a593Smuzhiyun
sifive_fu540_prci_wrpll_round_rate(struct clk_hw * hw,unsigned long rate,unsigned long * parent_rate)390*4882a593Smuzhiyun static long sifive_fu540_prci_wrpll_round_rate(struct clk_hw *hw,
391*4882a593Smuzhiyun unsigned long rate,
392*4882a593Smuzhiyun unsigned long *parent_rate)
393*4882a593Smuzhiyun {
394*4882a593Smuzhiyun struct __prci_clock *pc = clk_hw_to_prci_clock(hw);
395*4882a593Smuzhiyun struct __prci_wrpll_data *pwd = pc->pwd;
396*4882a593Smuzhiyun struct wrpll_cfg c;
397*4882a593Smuzhiyun
398*4882a593Smuzhiyun memcpy(&c, &pwd->c, sizeof(c));
399*4882a593Smuzhiyun
400*4882a593Smuzhiyun wrpll_configure_for_rate(&c, rate, *parent_rate);
401*4882a593Smuzhiyun
402*4882a593Smuzhiyun return wrpll_calc_output_rate(&c, *parent_rate);
403*4882a593Smuzhiyun }
404*4882a593Smuzhiyun
sifive_fu540_prci_wrpll_set_rate(struct clk_hw * hw,unsigned long rate,unsigned long parent_rate)405*4882a593Smuzhiyun static int sifive_fu540_prci_wrpll_set_rate(struct clk_hw *hw,
406*4882a593Smuzhiyun unsigned long rate,
407*4882a593Smuzhiyun unsigned long parent_rate)
408*4882a593Smuzhiyun {
409*4882a593Smuzhiyun struct __prci_clock *pc = clk_hw_to_prci_clock(hw);
410*4882a593Smuzhiyun struct __prci_wrpll_data *pwd = pc->pwd;
411*4882a593Smuzhiyun struct __prci_data *pd = pc->pd;
412*4882a593Smuzhiyun int r;
413*4882a593Smuzhiyun
414*4882a593Smuzhiyun r = wrpll_configure_for_rate(&pwd->c, rate, parent_rate);
415*4882a593Smuzhiyun if (r)
416*4882a593Smuzhiyun return r;
417*4882a593Smuzhiyun
418*4882a593Smuzhiyun if (pwd->enable_bypass)
419*4882a593Smuzhiyun pwd->enable_bypass(pd);
420*4882a593Smuzhiyun
421*4882a593Smuzhiyun __prci_wrpll_write_cfg(pd, pwd, &pwd->c);
422*4882a593Smuzhiyun
423*4882a593Smuzhiyun udelay(wrpll_calc_max_lock_us(&pwd->c));
424*4882a593Smuzhiyun
425*4882a593Smuzhiyun if (pwd->disable_bypass)
426*4882a593Smuzhiyun pwd->disable_bypass(pd);
427*4882a593Smuzhiyun
428*4882a593Smuzhiyun return 0;
429*4882a593Smuzhiyun }
430*4882a593Smuzhiyun
431*4882a593Smuzhiyun static const struct clk_ops sifive_fu540_prci_wrpll_clk_ops = {
432*4882a593Smuzhiyun .set_rate = sifive_fu540_prci_wrpll_set_rate,
433*4882a593Smuzhiyun .round_rate = sifive_fu540_prci_wrpll_round_rate,
434*4882a593Smuzhiyun .recalc_rate = sifive_fu540_prci_wrpll_recalc_rate,
435*4882a593Smuzhiyun };
436*4882a593Smuzhiyun
437*4882a593Smuzhiyun static const struct clk_ops sifive_fu540_prci_wrpll_ro_clk_ops = {
438*4882a593Smuzhiyun .recalc_rate = sifive_fu540_prci_wrpll_recalc_rate,
439*4882a593Smuzhiyun };
440*4882a593Smuzhiyun
441*4882a593Smuzhiyun /* TLCLKSEL clock integration */
442*4882a593Smuzhiyun
sifive_fu540_prci_tlclksel_recalc_rate(struct clk_hw * hw,unsigned long parent_rate)443*4882a593Smuzhiyun static unsigned long sifive_fu540_prci_tlclksel_recalc_rate(struct clk_hw *hw,
444*4882a593Smuzhiyun unsigned long parent_rate)
445*4882a593Smuzhiyun {
446*4882a593Smuzhiyun struct __prci_clock *pc = clk_hw_to_prci_clock(hw);
447*4882a593Smuzhiyun struct __prci_data *pd = pc->pd;
448*4882a593Smuzhiyun u32 v;
449*4882a593Smuzhiyun u8 div;
450*4882a593Smuzhiyun
451*4882a593Smuzhiyun v = __prci_readl(pd, PRCI_CLKMUXSTATUSREG_OFFSET);
452*4882a593Smuzhiyun v &= PRCI_CLKMUXSTATUSREG_TLCLKSEL_STATUS_MASK;
453*4882a593Smuzhiyun div = v ? 1 : 2;
454*4882a593Smuzhiyun
455*4882a593Smuzhiyun return div_u64(parent_rate, div);
456*4882a593Smuzhiyun }
457*4882a593Smuzhiyun
458*4882a593Smuzhiyun static const struct clk_ops sifive_fu540_prci_tlclksel_clk_ops = {
459*4882a593Smuzhiyun .recalc_rate = sifive_fu540_prci_tlclksel_recalc_rate,
460*4882a593Smuzhiyun };
461*4882a593Smuzhiyun
462*4882a593Smuzhiyun /*
463*4882a593Smuzhiyun * PRCI integration data for each WRPLL instance
464*4882a593Smuzhiyun */
465*4882a593Smuzhiyun
466*4882a593Smuzhiyun static struct __prci_wrpll_data __prci_corepll_data = {
467*4882a593Smuzhiyun .cfg0_offs = PRCI_COREPLLCFG0_OFFSET,
468*4882a593Smuzhiyun .enable_bypass = __prci_coreclksel_use_hfclk,
469*4882a593Smuzhiyun .disable_bypass = __prci_coreclksel_use_corepll,
470*4882a593Smuzhiyun };
471*4882a593Smuzhiyun
472*4882a593Smuzhiyun static struct __prci_wrpll_data __prci_ddrpll_data = {
473*4882a593Smuzhiyun .cfg0_offs = PRCI_DDRPLLCFG0_OFFSET,
474*4882a593Smuzhiyun };
475*4882a593Smuzhiyun
476*4882a593Smuzhiyun static struct __prci_wrpll_data __prci_gemgxlpll_data = {
477*4882a593Smuzhiyun .cfg0_offs = PRCI_GEMGXLPLLCFG0_OFFSET,
478*4882a593Smuzhiyun };
479*4882a593Smuzhiyun
480*4882a593Smuzhiyun /*
481*4882a593Smuzhiyun * List of clock controls provided by the PRCI
482*4882a593Smuzhiyun */
483*4882a593Smuzhiyun
484*4882a593Smuzhiyun static struct __prci_clock __prci_init_clocks[] = {
485*4882a593Smuzhiyun [PRCI_CLK_COREPLL] = {
486*4882a593Smuzhiyun .name = "corepll",
487*4882a593Smuzhiyun .parent_name = "hfclk",
488*4882a593Smuzhiyun .ops = &sifive_fu540_prci_wrpll_clk_ops,
489*4882a593Smuzhiyun .pwd = &__prci_corepll_data,
490*4882a593Smuzhiyun },
491*4882a593Smuzhiyun [PRCI_CLK_DDRPLL] = {
492*4882a593Smuzhiyun .name = "ddrpll",
493*4882a593Smuzhiyun .parent_name = "hfclk",
494*4882a593Smuzhiyun .ops = &sifive_fu540_prci_wrpll_ro_clk_ops,
495*4882a593Smuzhiyun .pwd = &__prci_ddrpll_data,
496*4882a593Smuzhiyun },
497*4882a593Smuzhiyun [PRCI_CLK_GEMGXLPLL] = {
498*4882a593Smuzhiyun .name = "gemgxlpll",
499*4882a593Smuzhiyun .parent_name = "hfclk",
500*4882a593Smuzhiyun .ops = &sifive_fu540_prci_wrpll_clk_ops,
501*4882a593Smuzhiyun .pwd = &__prci_gemgxlpll_data,
502*4882a593Smuzhiyun },
503*4882a593Smuzhiyun [PRCI_CLK_TLCLK] = {
504*4882a593Smuzhiyun .name = "tlclk",
505*4882a593Smuzhiyun .parent_name = "corepll",
506*4882a593Smuzhiyun .ops = &sifive_fu540_prci_tlclksel_clk_ops,
507*4882a593Smuzhiyun },
508*4882a593Smuzhiyun };
509*4882a593Smuzhiyun
510*4882a593Smuzhiyun /**
511*4882a593Smuzhiyun * __prci_register_clocks() - register clock controls in the PRCI with Linux
512*4882a593Smuzhiyun * @dev: Linux struct device *
513*4882a593Smuzhiyun *
514*4882a593Smuzhiyun * Register the list of clock controls described in __prci_init_plls[] with
515*4882a593Smuzhiyun * the Linux clock framework.
516*4882a593Smuzhiyun *
517*4882a593Smuzhiyun * Return: 0 upon success or a negative error code upon failure.
518*4882a593Smuzhiyun */
__prci_register_clocks(struct device * dev,struct __prci_data * pd)519*4882a593Smuzhiyun static int __prci_register_clocks(struct device *dev, struct __prci_data *pd)
520*4882a593Smuzhiyun {
521*4882a593Smuzhiyun struct clk_init_data init = { };
522*4882a593Smuzhiyun struct __prci_clock *pic;
523*4882a593Smuzhiyun int parent_count, i, r;
524*4882a593Smuzhiyun
525*4882a593Smuzhiyun parent_count = of_clk_get_parent_count(dev->of_node);
526*4882a593Smuzhiyun if (parent_count != EXPECTED_CLK_PARENT_COUNT) {
527*4882a593Smuzhiyun dev_err(dev, "expected only two parent clocks, found %d\n",
528*4882a593Smuzhiyun parent_count);
529*4882a593Smuzhiyun return -EINVAL;
530*4882a593Smuzhiyun }
531*4882a593Smuzhiyun
532*4882a593Smuzhiyun /* Register PLLs */
533*4882a593Smuzhiyun for (i = 0; i < ARRAY_SIZE(__prci_init_clocks); ++i) {
534*4882a593Smuzhiyun pic = &__prci_init_clocks[i];
535*4882a593Smuzhiyun
536*4882a593Smuzhiyun init.name = pic->name;
537*4882a593Smuzhiyun init.parent_names = &pic->parent_name;
538*4882a593Smuzhiyun init.num_parents = 1;
539*4882a593Smuzhiyun init.ops = pic->ops;
540*4882a593Smuzhiyun pic->hw.init = &init;
541*4882a593Smuzhiyun
542*4882a593Smuzhiyun pic->pd = pd;
543*4882a593Smuzhiyun
544*4882a593Smuzhiyun if (pic->pwd)
545*4882a593Smuzhiyun __prci_wrpll_read_cfg(pd, pic->pwd);
546*4882a593Smuzhiyun
547*4882a593Smuzhiyun r = devm_clk_hw_register(dev, &pic->hw);
548*4882a593Smuzhiyun if (r) {
549*4882a593Smuzhiyun dev_warn(dev, "Failed to register clock %s: %d\n",
550*4882a593Smuzhiyun init.name, r);
551*4882a593Smuzhiyun return r;
552*4882a593Smuzhiyun }
553*4882a593Smuzhiyun
554*4882a593Smuzhiyun r = clk_hw_register_clkdev(&pic->hw, pic->name, dev_name(dev));
555*4882a593Smuzhiyun if (r) {
556*4882a593Smuzhiyun dev_warn(dev, "Failed to register clkdev for %s: %d\n",
557*4882a593Smuzhiyun init.name, r);
558*4882a593Smuzhiyun return r;
559*4882a593Smuzhiyun }
560*4882a593Smuzhiyun
561*4882a593Smuzhiyun pd->hw_clks.hws[i] = &pic->hw;
562*4882a593Smuzhiyun }
563*4882a593Smuzhiyun
564*4882a593Smuzhiyun pd->hw_clks.num = i;
565*4882a593Smuzhiyun
566*4882a593Smuzhiyun r = devm_of_clk_add_hw_provider(dev, of_clk_hw_onecell_get,
567*4882a593Smuzhiyun &pd->hw_clks);
568*4882a593Smuzhiyun if (r) {
569*4882a593Smuzhiyun dev_err(dev, "could not add hw_provider: %d\n", r);
570*4882a593Smuzhiyun return r;
571*4882a593Smuzhiyun }
572*4882a593Smuzhiyun
573*4882a593Smuzhiyun return 0;
574*4882a593Smuzhiyun }
575*4882a593Smuzhiyun
576*4882a593Smuzhiyun /*
577*4882a593Smuzhiyun * Linux device model integration
578*4882a593Smuzhiyun *
579*4882a593Smuzhiyun * See the Linux device model documentation for more information about
580*4882a593Smuzhiyun * these functions.
581*4882a593Smuzhiyun */
sifive_fu540_prci_probe(struct platform_device * pdev)582*4882a593Smuzhiyun static int sifive_fu540_prci_probe(struct platform_device *pdev)
583*4882a593Smuzhiyun {
584*4882a593Smuzhiyun struct device *dev = &pdev->dev;
585*4882a593Smuzhiyun struct resource *res;
586*4882a593Smuzhiyun struct __prci_data *pd;
587*4882a593Smuzhiyun int r;
588*4882a593Smuzhiyun
589*4882a593Smuzhiyun pd = devm_kzalloc(dev,
590*4882a593Smuzhiyun struct_size(pd, hw_clks.hws,
591*4882a593Smuzhiyun ARRAY_SIZE(__prci_init_clocks)),
592*4882a593Smuzhiyun GFP_KERNEL);
593*4882a593Smuzhiyun if (!pd)
594*4882a593Smuzhiyun return -ENOMEM;
595*4882a593Smuzhiyun
596*4882a593Smuzhiyun res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
597*4882a593Smuzhiyun pd->va = devm_ioremap_resource(dev, res);
598*4882a593Smuzhiyun if (IS_ERR(pd->va))
599*4882a593Smuzhiyun return PTR_ERR(pd->va);
600*4882a593Smuzhiyun
601*4882a593Smuzhiyun r = __prci_register_clocks(dev, pd);
602*4882a593Smuzhiyun if (r) {
603*4882a593Smuzhiyun dev_err(dev, "could not register clocks: %d\n", r);
604*4882a593Smuzhiyun return r;
605*4882a593Smuzhiyun }
606*4882a593Smuzhiyun
607*4882a593Smuzhiyun dev_dbg(dev, "SiFive FU540 PRCI probed\n");
608*4882a593Smuzhiyun
609*4882a593Smuzhiyun return 0;
610*4882a593Smuzhiyun }
611*4882a593Smuzhiyun
612*4882a593Smuzhiyun static const struct of_device_id sifive_fu540_prci_of_match[] = {
613*4882a593Smuzhiyun { .compatible = "sifive,fu540-c000-prci", },
614*4882a593Smuzhiyun {}
615*4882a593Smuzhiyun };
616*4882a593Smuzhiyun MODULE_DEVICE_TABLE(of, sifive_fu540_prci_of_match);
617*4882a593Smuzhiyun
618*4882a593Smuzhiyun static struct platform_driver sifive_fu540_prci_driver = {
619*4882a593Smuzhiyun .driver = {
620*4882a593Smuzhiyun .name = "sifive-fu540-prci",
621*4882a593Smuzhiyun .of_match_table = sifive_fu540_prci_of_match,
622*4882a593Smuzhiyun },
623*4882a593Smuzhiyun .probe = sifive_fu540_prci_probe,
624*4882a593Smuzhiyun };
625*4882a593Smuzhiyun
sifive_fu540_prci_init(void)626*4882a593Smuzhiyun static int __init sifive_fu540_prci_init(void)
627*4882a593Smuzhiyun {
628*4882a593Smuzhiyun return platform_driver_register(&sifive_fu540_prci_driver);
629*4882a593Smuzhiyun }
630*4882a593Smuzhiyun core_initcall(sifive_fu540_prci_init);
631