blob: fe6e0d4073f9948388bc2ae5d2f9e8ba749fd357 [file] [log] [blame]
Anup Patel42fdf082019-02-25 08:14:49 +00001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (c) 2019 Western Digital Corporation or its affiliates.
4 *
5 * Copyright (C) 2018 SiFive, Inc.
6 * Wesley Terpstra
7 * Paul Walmsley
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * The FU540 PRCI implements clock and reset control for the SiFive
19 * FU540-C000 chip. This driver assumes that it has sole control
20 * over all PRCI resources.
21 *
22 * This driver is based on the PRCI driver written by Wesley Terpstra.
23 *
24 * Refer, commit 999529edf517ed75b56659d456d221b2ee56bb60 of:
25 * https://github.com/riscv/riscv-linux
26 *
27 * References:
28 * - SiFive FU540-C000 manual v1p0, Chapter 7 "Clocking and Reset"
29 */
30
Jagan Teki72be9862019-05-08 19:52:18 +053031#include <common.h>
Anup Patel42fdf082019-02-25 08:14:49 +000032#include <asm/io.h>
33#include <clk-uclass.h>
34#include <clk.h>
Anup Patel42fdf082019-02-25 08:14:49 +000035#include <div64.h>
36#include <dm.h>
37#include <errno.h>
Simon Glassdbd79542020-05-10 11:40:11 -060038#include <linux/delay.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070039#include <linux/err.h>
Anup Patel42fdf082019-02-25 08:14:49 +000040
41#include <linux/math64.h>
Anup Patel00a156d2019-06-25 06:31:02 +000042#include <linux/clk/analogbits-wrpll-cln28hpc.h>
Anup Patel83d5b502019-06-25 06:31:15 +000043#include <dt-bindings/clock/sifive-fu540-prci.h>
Anup Patel42fdf082019-02-25 08:14:49 +000044
Anup Patel42fdf082019-02-25 08:14:49 +000045/*
46 * EXPECTED_CLK_PARENT_COUNT: how many parent clocks this driver expects:
47 * hfclk and rtcclk
48 */
49#define EXPECTED_CLK_PARENT_COUNT 2
50
51/*
52 * Register offsets and bitmasks
53 */
54
55/* COREPLLCFG0 */
56#define PRCI_COREPLLCFG0_OFFSET 0x4
57#define PRCI_COREPLLCFG0_DIVR_SHIFT 0
58#define PRCI_COREPLLCFG0_DIVR_MASK (0x3f << PRCI_COREPLLCFG0_DIVR_SHIFT)
59#define PRCI_COREPLLCFG0_DIVF_SHIFT 6
60#define PRCI_COREPLLCFG0_DIVF_MASK (0x1ff << PRCI_COREPLLCFG0_DIVF_SHIFT)
61#define PRCI_COREPLLCFG0_DIVQ_SHIFT 15
62#define PRCI_COREPLLCFG0_DIVQ_MASK (0x7 << PRCI_COREPLLCFG0_DIVQ_SHIFT)
63#define PRCI_COREPLLCFG0_RANGE_SHIFT 18
64#define PRCI_COREPLLCFG0_RANGE_MASK (0x7 << PRCI_COREPLLCFG0_RANGE_SHIFT)
65#define PRCI_COREPLLCFG0_BYPASS_SHIFT 24
66#define PRCI_COREPLLCFG0_BYPASS_MASK (0x1 << PRCI_COREPLLCFG0_BYPASS_SHIFT)
67#define PRCI_COREPLLCFG0_FSE_SHIFT 25
68#define PRCI_COREPLLCFG0_FSE_MASK (0x1 << PRCI_COREPLLCFG0_FSE_SHIFT)
69#define PRCI_COREPLLCFG0_LOCK_SHIFT 31
70#define PRCI_COREPLLCFG0_LOCK_MASK (0x1 << PRCI_COREPLLCFG0_LOCK_SHIFT)
71
Pragnesh Patel54ce0e02020-05-29 11:33:29 +053072/* COREPLLCFG1 */
73#define PRCI_COREPLLCFG1_OFFSET 0x8
74#define PRCI_COREPLLCFG1_CKE_SHIFT 31
75#define PRCI_COREPLLCFG1_CKE_MASK (0x1 << PRCI_COREPLLCFG1_CKE_SHIFT)
76
Anup Patel42fdf082019-02-25 08:14:49 +000077/* DDRPLLCFG0 */
78#define PRCI_DDRPLLCFG0_OFFSET 0xc
79#define PRCI_DDRPLLCFG0_DIVR_SHIFT 0
80#define PRCI_DDRPLLCFG0_DIVR_MASK (0x3f << PRCI_DDRPLLCFG0_DIVR_SHIFT)
81#define PRCI_DDRPLLCFG0_DIVF_SHIFT 6
82#define PRCI_DDRPLLCFG0_DIVF_MASK (0x1ff << PRCI_DDRPLLCFG0_DIVF_SHIFT)
83#define PRCI_DDRPLLCFG0_DIVQ_SHIFT 15
84#define PRCI_DDRPLLCFG0_DIVQ_MASK (0x7 << PRCI_DDRPLLCFG0_DIVQ_SHIFT)
85#define PRCI_DDRPLLCFG0_RANGE_SHIFT 18
86#define PRCI_DDRPLLCFG0_RANGE_MASK (0x7 << PRCI_DDRPLLCFG0_RANGE_SHIFT)
87#define PRCI_DDRPLLCFG0_BYPASS_SHIFT 24
88#define PRCI_DDRPLLCFG0_BYPASS_MASK (0x1 << PRCI_DDRPLLCFG0_BYPASS_SHIFT)
89#define PRCI_DDRPLLCFG0_FSE_SHIFT 25
90#define PRCI_DDRPLLCFG0_FSE_MASK (0x1 << PRCI_DDRPLLCFG0_FSE_SHIFT)
91#define PRCI_DDRPLLCFG0_LOCK_SHIFT 31
92#define PRCI_DDRPLLCFG0_LOCK_MASK (0x1 << PRCI_DDRPLLCFG0_LOCK_SHIFT)
93
94/* DDRPLLCFG1 */
95#define PRCI_DDRPLLCFG1_OFFSET 0x10
Pragnesh Patel54ce0e02020-05-29 11:33:29 +053096#define PRCI_DDRPLLCFG1_CKE_SHIFT 31
Anup Patel42fdf082019-02-25 08:14:49 +000097#define PRCI_DDRPLLCFG1_CKE_MASK (0x1 << PRCI_DDRPLLCFG1_CKE_SHIFT)
98
99/* GEMGXLPLLCFG0 */
100#define PRCI_GEMGXLPLLCFG0_OFFSET 0x1c
101#define PRCI_GEMGXLPLLCFG0_DIVR_SHIFT 0
102#define PRCI_GEMGXLPLLCFG0_DIVR_MASK \
103 (0x3f << PRCI_GEMGXLPLLCFG0_DIVR_SHIFT)
104#define PRCI_GEMGXLPLLCFG0_DIVF_SHIFT 6
105#define PRCI_GEMGXLPLLCFG0_DIVF_MASK \
106 (0x1ff << PRCI_GEMGXLPLLCFG0_DIVF_SHIFT)
107#define PRCI_GEMGXLPLLCFG0_DIVQ_SHIFT 15
108#define PRCI_GEMGXLPLLCFG0_DIVQ_MASK (0x7 << PRCI_GEMGXLPLLCFG0_DIVQ_SHIFT)
109#define PRCI_GEMGXLPLLCFG0_RANGE_SHIFT 18
110#define PRCI_GEMGXLPLLCFG0_RANGE_MASK \
111 (0x7 << PRCI_GEMGXLPLLCFG0_RANGE_SHIFT)
112#define PRCI_GEMGXLPLLCFG0_BYPASS_SHIFT 24
113#define PRCI_GEMGXLPLLCFG0_BYPASS_MASK \
114 (0x1 << PRCI_GEMGXLPLLCFG0_BYPASS_SHIFT)
115#define PRCI_GEMGXLPLLCFG0_FSE_SHIFT 25
116#define PRCI_GEMGXLPLLCFG0_FSE_MASK \
117 (0x1 << PRCI_GEMGXLPLLCFG0_FSE_SHIFT)
118#define PRCI_GEMGXLPLLCFG0_LOCK_SHIFT 31
119#define PRCI_GEMGXLPLLCFG0_LOCK_MASK (0x1 << PRCI_GEMGXLPLLCFG0_LOCK_SHIFT)
120
121/* GEMGXLPLLCFG1 */
122#define PRCI_GEMGXLPLLCFG1_OFFSET 0x20
Pragnesh Patel54ce0e02020-05-29 11:33:29 +0530123#define PRCI_GEMGXLPLLCFG1_CKE_SHIFT 31
Anup Patel42fdf082019-02-25 08:14:49 +0000124#define PRCI_GEMGXLPLLCFG1_CKE_MASK (0x1 << PRCI_GEMGXLPLLCFG1_CKE_SHIFT)
125
126/* CORECLKSEL */
127#define PRCI_CORECLKSEL_OFFSET 0x24
128#define PRCI_CORECLKSEL_CORECLKSEL_SHIFT 0
129#define PRCI_CORECLKSEL_CORECLKSEL_MASK \
130 (0x1 << PRCI_CORECLKSEL_CORECLKSEL_SHIFT)
131
132/* DEVICESRESETREG */
133#define PRCI_DEVICESRESETREG_OFFSET 0x28
134#define PRCI_DEVICESRESETREG_DDR_CTRL_RST_N_SHIFT 0
135#define PRCI_DEVICESRESETREG_DDR_CTRL_RST_N_MASK \
136 (0x1 << PRCI_DEVICESRESETREG_DDR_CTRL_RST_N_SHIFT)
137#define PRCI_DEVICESRESETREG_DDR_AXI_RST_N_SHIFT 1
138#define PRCI_DEVICESRESETREG_DDR_AXI_RST_N_MASK \
139 (0x1 << PRCI_DEVICESRESETREG_DDR_AXI_RST_N_SHIFT)
140#define PRCI_DEVICESRESETREG_DDR_AHB_RST_N_SHIFT 2
141#define PRCI_DEVICESRESETREG_DDR_AHB_RST_N_MASK \
142 (0x1 << PRCI_DEVICESRESETREG_DDR_AHB_RST_N_SHIFT)
143#define PRCI_DEVICESRESETREG_DDR_PHY_RST_N_SHIFT 3
144#define PRCI_DEVICESRESETREG_DDR_PHY_RST_N_MASK \
145 (0x1 << PRCI_DEVICESRESETREG_DDR_PHY_RST_N_SHIFT)
146#define PRCI_DEVICESRESETREG_GEMGXL_RST_N_SHIFT 5
147#define PRCI_DEVICESRESETREG_GEMGXL_RST_N_MASK \
148 (0x1 << PRCI_DEVICESRESETREG_GEMGXL_RST_N_SHIFT)
149
150/* CLKMUXSTATUSREG */
Pragnesh Patel54ce0e02020-05-29 11:33:29 +0530151#define PRCI_CLKMUXSTATUSREG_OFFSET 0x2c
Anup Patel42fdf082019-02-25 08:14:49 +0000152#define PRCI_CLKMUXSTATUSREG_TLCLKSEL_STATUS_SHIFT 1
153#define PRCI_CLKMUXSTATUSREG_TLCLKSEL_STATUS_MASK \
154 (0x1 << PRCI_CLKMUXSTATUSREG_TLCLKSEL_STATUS_SHIFT)
155
Pragnesh Patel1790bce2020-05-29 11:33:30 +0530156/* PROCMONCFG */
157#define PRCI_PROCMONCFG_OFFSET 0xF0
158#define PRCI_PROCMONCFG_CORE_CLOCK_SHIFT 24
159#define PRCI_PROCMONCFG_CORE_CLOCK_MASK \
160 (0x1 << PRCI_PROCMONCFG_CORE_CLOCK_SHIFT)
161
Anup Patel42fdf082019-02-25 08:14:49 +0000162/*
163 * Private structures
164 */
165
166/**
167 * struct __prci_data - per-device-instance data
168 * @va: base virtual address of the PRCI IP block
169 * @parent: parent clk instance
170 *
171 * PRCI per-device instance data
172 */
173struct __prci_data {
Anup Patel9a99add2019-06-25 06:31:21 +0000174 void *va;
175 struct clk parent_hfclk;
176 struct clk parent_rtcclk;
Anup Patel42fdf082019-02-25 08:14:49 +0000177};
178
179/**
180 * struct __prci_wrpll_data - WRPLL configuration and integration data
181 * @c: WRPLL current configuration record
Anup Patel9a99add2019-06-25 06:31:21 +0000182 * @enable_bypass: fn ptr to code to bypass the WRPLL (if applicable; else NULL)
183 * @disable_bypass: fn ptr to code to not bypass the WRPLL (or NULL)
Anup Patel42fdf082019-02-25 08:14:49 +0000184 * @cfg0_offs: WRPLL CFG0 register offset (in bytes) from the PRCI base address
Pragnesh Patel54ce0e02020-05-29 11:33:29 +0530185 * @cfg1_offs: WRPLL CFG1 register offset (in bytes) from the PRCI base address
Pragnesh Patel1790bce2020-05-29 11:33:30 +0530186 * @release_reset: fn ptr to code to release clock reset
Anup Patel42fdf082019-02-25 08:14:49 +0000187 *
Anup Patel9a99add2019-06-25 06:31:21 +0000188 * @enable_bypass and @disable_bypass are used for WRPLL instances
189 * that contain a separate external glitchless clock mux downstream
190 * from the PLL. The WRPLL internal bypass mux is not glitchless.
Anup Patel42fdf082019-02-25 08:14:49 +0000191 */
192struct __prci_wrpll_data {
Anup Patel6f7b5a22019-06-25 06:31:08 +0000193 struct wrpll_cfg c;
Anup Patel9a99add2019-06-25 06:31:21 +0000194 void (*enable_bypass)(struct __prci_data *pd);
195 void (*disable_bypass)(struct __prci_data *pd);
Anup Patel42fdf082019-02-25 08:14:49 +0000196 u8 cfg0_offs;
Pragnesh Patel54ce0e02020-05-29 11:33:29 +0530197 u8 cfg1_offs;
Pragnesh Patel1790bce2020-05-29 11:33:30 +0530198 void (*release_reset)(struct __prci_data *pd);
Anup Patel42fdf082019-02-25 08:14:49 +0000199};
200
201struct __prci_clock;
202
Anup Patel9a99add2019-06-25 06:31:21 +0000203/* struct __prci_clock_ops - clock operations */
Anup Patel42fdf082019-02-25 08:14:49 +0000204struct __prci_clock_ops {
205 int (*set_rate)(struct __prci_clock *pc,
206 unsigned long rate,
207 unsigned long parent_rate);
208 unsigned long (*round_rate)(struct __prci_clock *pc,
209 unsigned long rate,
210 unsigned long *parent_rate);
211 unsigned long (*recalc_rate)(struct __prci_clock *pc,
212 unsigned long parent_rate);
Pragnesh Patel54ce0e02020-05-29 11:33:29 +0530213 int (*enable_clk)(struct __prci_clock *pc, bool enable);
Anup Patel42fdf082019-02-25 08:14:49 +0000214};
215
216/**
217 * struct __prci_clock - describes a clock device managed by PRCI
218 * @name: user-readable clock name string - should match the manual
219 * @parent_name: parent name for this clock
Anup Patel9a99add2019-06-25 06:31:21 +0000220 * @ops: struct __prci_clock_ops for control
Anup Patel42fdf082019-02-25 08:14:49 +0000221 * @pwd: WRPLL-specific data, associated with this clock (if not NULL)
222 * @pd: PRCI-specific data associated with this clock (if not NULL)
223 *
224 * PRCI clock data. Used by the PRCI driver to register PRCI-provided
225 * clocks to the Linux clock infrastructure.
226 */
227struct __prci_clock {
228 const char *name;
229 const char *parent_name;
230 const struct __prci_clock_ops *ops;
231 struct __prci_wrpll_data *pwd;
232 struct __prci_data *pd;
233};
234
235/*
236 * Private functions
237 */
238
239/**
240 * __prci_readl() - read from a PRCI register
241 * @pd: PRCI context
242 * @offs: register offset to read from (in bytes, from PRCI base address)
243 *
244 * Read the register located at offset @offs from the base virtual
245 * address of the PRCI register target described by @pd, and return
246 * the value to the caller.
247 *
248 * Context: Any context.
249 *
250 * Return: the contents of the register described by @pd and @offs.
251 */
252static u32 __prci_readl(struct __prci_data *pd, u32 offs)
253{
Anup Patel9a99add2019-06-25 06:31:21 +0000254 return readl(pd->va + offs);
Anup Patel42fdf082019-02-25 08:14:49 +0000255}
256
257static void __prci_writel(u32 v, u32 offs, struct __prci_data *pd)
258{
Anup Patel9a99add2019-06-25 06:31:21 +0000259 writel(v, pd->va + offs);
Anup Patel42fdf082019-02-25 08:14:49 +0000260}
261
262/* WRPLL-related private functions */
263
264/**
265 * __prci_wrpll_unpack() - unpack WRPLL configuration registers into parameters
Anup Patel6f7b5a22019-06-25 06:31:08 +0000266 * @c: ptr to a struct wrpll_cfg record to write config into
Anup Patel42fdf082019-02-25 08:14:49 +0000267 * @r: value read from the PRCI PLL configuration register
268 *
269 * Given a value @r read from an FU540 PRCI PLL configuration register,
270 * split it into fields and populate it into the WRPLL configuration record
271 * pointed to by @c.
272 *
273 * The COREPLLCFG0 macros are used below, but the other *PLLCFG0 macros
274 * have the same register layout.
275 *
276 * Context: Any context.
277 */
Anup Patel6f7b5a22019-06-25 06:31:08 +0000278static void __prci_wrpll_unpack(struct wrpll_cfg *c, u32 r)
Anup Patel42fdf082019-02-25 08:14:49 +0000279{
280 u32 v;
281
282 v = r & PRCI_COREPLLCFG0_DIVR_MASK;
283 v >>= PRCI_COREPLLCFG0_DIVR_SHIFT;
284 c->divr = v;
285
286 v = r & PRCI_COREPLLCFG0_DIVF_MASK;
287 v >>= PRCI_COREPLLCFG0_DIVF_SHIFT;
288 c->divf = v;
289
290 v = r & PRCI_COREPLLCFG0_DIVQ_MASK;
291 v >>= PRCI_COREPLLCFG0_DIVQ_SHIFT;
292 c->divq = v;
293
294 v = r & PRCI_COREPLLCFG0_RANGE_MASK;
295 v >>= PRCI_COREPLLCFG0_RANGE_SHIFT;
296 c->range = v;
297
298 c->flags &= (WRPLL_FLAGS_INT_FEEDBACK_MASK |
299 WRPLL_FLAGS_EXT_FEEDBACK_MASK);
300
Anup Patel9a99add2019-06-25 06:31:21 +0000301 /* external feedback mode not supported */
302 c->flags |= WRPLL_FLAGS_INT_FEEDBACK_MASK;
Anup Patel42fdf082019-02-25 08:14:49 +0000303}
304
305/**
306 * __prci_wrpll_pack() - pack PLL configuration parameters into a register value
Anup Patel6f7b5a22019-06-25 06:31:08 +0000307 * @c: pointer to a struct wrpll_cfg record containing the PLL's cfg
Anup Patel42fdf082019-02-25 08:14:49 +0000308 *
309 * Using a set of WRPLL configuration values pointed to by @c,
310 * assemble a PRCI PLL configuration register value, and return it to
311 * the caller.
312 *
313 * Context: Any context. Caller must ensure that the contents of the
314 * record pointed to by @c do not change during the execution
315 * of this function.
316 *
317 * Returns: a value suitable for writing into a PRCI PLL configuration
318 * register
319 */
Anup Patel9a99add2019-06-25 06:31:21 +0000320static u32 __prci_wrpll_pack(const struct wrpll_cfg *c)
Anup Patel42fdf082019-02-25 08:14:49 +0000321{
322 u32 r = 0;
323
324 r |= c->divr << PRCI_COREPLLCFG0_DIVR_SHIFT;
325 r |= c->divf << PRCI_COREPLLCFG0_DIVF_SHIFT;
326 r |= c->divq << PRCI_COREPLLCFG0_DIVQ_SHIFT;
327 r |= c->range << PRCI_COREPLLCFG0_RANGE_SHIFT;
Anup Patel9a99add2019-06-25 06:31:21 +0000328
329 /* external feedback mode not supported */
330 r |= PRCI_COREPLLCFG0_FSE_MASK;
Anup Patel42fdf082019-02-25 08:14:49 +0000331
332 return r;
333}
334
335/**
Pragnesh Patel54ce0e02020-05-29 11:33:29 +0530336 * __prci_wrpll_read_cfg0() - read the WRPLL configuration from the PRCI
Anup Patel42fdf082019-02-25 08:14:49 +0000337 * @pd: PRCI context
338 * @pwd: PRCI WRPLL metadata
339 *
340 * Read the current configuration of the PLL identified by @pwd from
341 * the PRCI identified by @pd, and store it into the local configuration
342 * cache in @pwd.
343 *
344 * Context: Any context. Caller must prevent the records pointed to by
345 * @pd and @pwd from changing during execution.
346 */
Pragnesh Patel54ce0e02020-05-29 11:33:29 +0530347static void __prci_wrpll_read_cfg0(struct __prci_data *pd,
348 struct __prci_wrpll_data *pwd)
Anup Patel42fdf082019-02-25 08:14:49 +0000349{
350 __prci_wrpll_unpack(&pwd->c, __prci_readl(pd, pwd->cfg0_offs));
351}
352
353/**
Pragnesh Patel54ce0e02020-05-29 11:33:29 +0530354 * __prci_wrpll_write_cfg0() - write WRPLL configuration into the PRCI
Anup Patel42fdf082019-02-25 08:14:49 +0000355 * @pd: PRCI context
356 * @pwd: PRCI WRPLL metadata
357 * @c: WRPLL configuration record to write
358 *
359 * Write the WRPLL configuration described by @c into the WRPLL
360 * configuration register identified by @pwd in the PRCI instance
361 * described by @c. Make a cached copy of the WRPLL's current
362 * configuration so it can be used by other code.
363 *
364 * Context: Any context. Caller must prevent the records pointed to by
365 * @pd and @pwd from changing during execution.
366 */
Pragnesh Patel54ce0e02020-05-29 11:33:29 +0530367static void __prci_wrpll_write_cfg0(struct __prci_data *pd,
368 struct __prci_wrpll_data *pwd,
369 struct wrpll_cfg *c)
Anup Patel42fdf082019-02-25 08:14:49 +0000370{
371 __prci_writel(__prci_wrpll_pack(c), pwd->cfg0_offs, pd);
372
Anup Patel9a99add2019-06-25 06:31:21 +0000373 memcpy(&pwd->c, c, sizeof(*c));
Anup Patel42fdf082019-02-25 08:14:49 +0000374}
375
Pragnesh Patel54ce0e02020-05-29 11:33:29 +0530376/**
377 * __prci_wrpll_write_cfg1() - write Clock enable/disable configuration
378 * into the PRCI
379 * @pd: PRCI context
380 * @pwd: PRCI WRPLL metadata
381 * @enable: Clock enable or disable value
382 */
383static void __prci_wrpll_write_cfg1(struct __prci_data *pd,
384 struct __prci_wrpll_data *pwd,
385 u32 enable)
386{
387 __prci_writel(enable, pwd->cfg1_offs, pd);
388}
389
Anup Patel42fdf082019-02-25 08:14:49 +0000390/* Core clock mux control */
391
392/**
393 * __prci_coreclksel_use_hfclk() - switch the CORECLK mux to output HFCLK
394 * @pd: struct __prci_data * for the PRCI containing the CORECLK mux reg
395 *
396 * Switch the CORECLK mux to the HFCLK input source; return once complete.
397 *
398 * Context: Any context. Caller must prevent concurrent changes to the
399 * PRCI_CORECLKSEL_OFFSET register.
400 */
401static void __prci_coreclksel_use_hfclk(struct __prci_data *pd)
402{
403 u32 r;
404
405 r = __prci_readl(pd, PRCI_CORECLKSEL_OFFSET);
406 r |= PRCI_CORECLKSEL_CORECLKSEL_MASK;
407 __prci_writel(r, PRCI_CORECLKSEL_OFFSET, pd);
408
409 r = __prci_readl(pd, PRCI_CORECLKSEL_OFFSET); /* barrier */
410}
411
412/**
413 * __prci_coreclksel_use_corepll() - switch the CORECLK mux to output COREPLL
414 * @pd: struct __prci_data * for the PRCI containing the CORECLK mux reg
415 *
416 * Switch the CORECLK mux to the PLL output clock; return once complete.
417 *
418 * Context: Any context. Caller must prevent concurrent changes to the
419 * PRCI_CORECLKSEL_OFFSET register.
420 */
421static void __prci_coreclksel_use_corepll(struct __prci_data *pd)
422{
423 u32 r;
424
425 r = __prci_readl(pd, PRCI_CORECLKSEL_OFFSET);
426 r &= ~PRCI_CORECLKSEL_CORECLKSEL_MASK;
427 __prci_writel(r, PRCI_CORECLKSEL_OFFSET, pd);
428
429 r = __prci_readl(pd, PRCI_CORECLKSEL_OFFSET); /* barrier */
430}
431
432static unsigned long sifive_fu540_prci_wrpll_recalc_rate(
433 struct __prci_clock *pc,
434 unsigned long parent_rate)
435{
436 struct __prci_wrpll_data *pwd = pc->pwd;
437
Anup Patel6f7b5a22019-06-25 06:31:08 +0000438 return wrpll_calc_output_rate(&pwd->c, parent_rate);
Anup Patel42fdf082019-02-25 08:14:49 +0000439}
440
441static unsigned long sifive_fu540_prci_wrpll_round_rate(
442 struct __prci_clock *pc,
443 unsigned long rate,
444 unsigned long *parent_rate)
445{
446 struct __prci_wrpll_data *pwd = pc->pwd;
Anup Patel6f7b5a22019-06-25 06:31:08 +0000447 struct wrpll_cfg c;
Anup Patel42fdf082019-02-25 08:14:49 +0000448
449 memcpy(&c, &pwd->c, sizeof(c));
450
Anup Patel6f7b5a22019-06-25 06:31:08 +0000451 wrpll_configure_for_rate(&c, rate, *parent_rate);
Anup Patel42fdf082019-02-25 08:14:49 +0000452
Anup Patel6f7b5a22019-06-25 06:31:08 +0000453 return wrpll_calc_output_rate(&c, *parent_rate);
Anup Patel42fdf082019-02-25 08:14:49 +0000454}
455
456static int sifive_fu540_prci_wrpll_set_rate(struct __prci_clock *pc,
457 unsigned long rate,
458 unsigned long parent_rate)
459{
460 struct __prci_wrpll_data *pwd = pc->pwd;
461 struct __prci_data *pd = pc->pd;
462 int r;
463
Anup Patel6f7b5a22019-06-25 06:31:08 +0000464 r = wrpll_configure_for_rate(&pwd->c, rate, parent_rate);
Anup Patel42fdf082019-02-25 08:14:49 +0000465 if (r)
Anup Patel9a99add2019-06-25 06:31:21 +0000466 return r;
Anup Patel42fdf082019-02-25 08:14:49 +0000467
Anup Patel9a99add2019-06-25 06:31:21 +0000468 if (pwd->enable_bypass)
469 pwd->enable_bypass(pd);
Anup Patel42fdf082019-02-25 08:14:49 +0000470
Pragnesh Patel54ce0e02020-05-29 11:33:29 +0530471 __prci_wrpll_write_cfg0(pd, pwd, &pwd->c);
Anup Patel42fdf082019-02-25 08:14:49 +0000472
Anup Patel6f7b5a22019-06-25 06:31:08 +0000473 udelay(wrpll_calc_max_lock_us(&pwd->c));
Anup Patel42fdf082019-02-25 08:14:49 +0000474
Anup Patel9a99add2019-06-25 06:31:21 +0000475 if (pwd->disable_bypass)
476 pwd->disable_bypass(pd);
Anup Patel42fdf082019-02-25 08:14:49 +0000477
478 return 0;
479}
480
Pragnesh Patel54ce0e02020-05-29 11:33:29 +0530481static int sifive_fu540_prci_clock_enable(struct __prci_clock *pc, bool enable)
482{
483 struct __prci_wrpll_data *pwd = pc->pwd;
484 struct __prci_data *pd = pc->pd;
485
486 if (enable) {
487 __prci_wrpll_write_cfg1(pd, pwd, PRCI_COREPLLCFG1_CKE_MASK);
Pragnesh Patel1790bce2020-05-29 11:33:30 +0530488
489 if (pwd->release_reset)
490 pwd->release_reset(pd);
Pragnesh Patel54ce0e02020-05-29 11:33:29 +0530491 } else {
492 u32 r;
493
494 r = __prci_readl(pd, pwd->cfg1_offs);
495 r &= ~PRCI_COREPLLCFG1_CKE_MASK;
496
497 __prci_wrpll_write_cfg1(pd, pwd, r);
498 }
499
500 return 0;
501}
502
Anup Patel42fdf082019-02-25 08:14:49 +0000503static const struct __prci_clock_ops sifive_fu540_prci_wrpll_clk_ops = {
504 .set_rate = sifive_fu540_prci_wrpll_set_rate,
505 .round_rate = sifive_fu540_prci_wrpll_round_rate,
506 .recalc_rate = sifive_fu540_prci_wrpll_recalc_rate,
Pragnesh Patel54ce0e02020-05-29 11:33:29 +0530507 .enable_clk = sifive_fu540_prci_clock_enable,
Anup Patel42fdf082019-02-25 08:14:49 +0000508};
509
Anup Patel42fdf082019-02-25 08:14:49 +0000510/* TLCLKSEL clock integration */
511
512static unsigned long sifive_fu540_prci_tlclksel_recalc_rate(
513 struct __prci_clock *pc,
514 unsigned long parent_rate)
515{
516 struct __prci_data *pd = pc->pd;
517 u32 v;
518 u8 div;
519
520 v = __prci_readl(pd, PRCI_CLKMUXSTATUSREG_OFFSET);
521 v &= PRCI_CLKMUXSTATUSREG_TLCLKSEL_STATUS_MASK;
522 div = v ? 1 : 2;
523
524 return div_u64(parent_rate, div);
525}
526
527static const struct __prci_clock_ops sifive_fu540_prci_tlclksel_clk_ops = {
528 .recalc_rate = sifive_fu540_prci_tlclksel_recalc_rate,
529};
530
Pragnesh Patel1790bce2020-05-29 11:33:30 +0530531/**
532 * __prci_ddr_release_reset() - Release DDR reset
533 * @pd: struct __prci_data * for the PRCI containing the DDRCLK mux reg
534 *
535 */
536static void __prci_ddr_release_reset(struct __prci_data *pd)
537{
538 u32 v;
539
540 v = __prci_readl(pd, PRCI_DEVICESRESETREG_OFFSET);
541 v |= PRCI_DEVICESRESETREG_DDR_CTRL_RST_N_MASK;
542 __prci_writel(v, PRCI_DEVICESRESETREG_OFFSET, pd);
543
544 /* HACK to get the '1 full controller clock cycle'. */
545 asm volatile ("fence");
546 v = __prci_readl(pd, PRCI_DEVICESRESETREG_OFFSET);
547 v |= (PRCI_DEVICESRESETREG_DDR_AXI_RST_N_MASK |
548 PRCI_DEVICESRESETREG_DDR_AHB_RST_N_MASK |
549 PRCI_DEVICESRESETREG_DDR_PHY_RST_N_MASK);
550 __prci_writel(v, PRCI_DEVICESRESETREG_OFFSET, pd);
551
552 /* HACK to get the '1 full controller clock cycle'. */
553 asm volatile ("fence");
554
555 /*
556 * These take like 16 cycles to actually propagate. We can't go sending
557 * stuff before they come out of reset. So wait.
558 */
559 for (int i = 0; i < 256; i++)
560 asm volatile ("nop");
561}
562
Pragnesh Patele848dba2020-05-29 11:33:31 +0530563/**
564 * __prci_ethernet_release_reset() - Release ethernet reset
565 * @pd: struct __prci_data * for the PRCI containing the Ethernet CLK mux reg
566 *
567 */
568static void __prci_ethernet_release_reset(struct __prci_data *pd)
569{
570 u32 v;
571
572 /* Release GEMGXL reset */
573 v = __prci_readl(pd, PRCI_DEVICESRESETREG_OFFSET);
574 v |= PRCI_DEVICESRESETREG_GEMGXL_RST_N_MASK;
575 __prci_writel(v, PRCI_DEVICESRESETREG_OFFSET, pd);
576
577 /* Procmon => core clock */
578 __prci_writel(PRCI_PROCMONCFG_CORE_CLOCK_MASK, PRCI_PROCMONCFG_OFFSET,
579 pd);
580}
581
Anup Patel42fdf082019-02-25 08:14:49 +0000582/*
583 * PRCI integration data for each WRPLL instance
584 */
585
586static struct __prci_wrpll_data __prci_corepll_data = {
587 .cfg0_offs = PRCI_COREPLLCFG0_OFFSET,
Pragnesh Patel54ce0e02020-05-29 11:33:29 +0530588 .cfg1_offs = PRCI_COREPLLCFG1_OFFSET,
Anup Patel9a99add2019-06-25 06:31:21 +0000589 .enable_bypass = __prci_coreclksel_use_hfclk,
590 .disable_bypass = __prci_coreclksel_use_corepll,
Anup Patel42fdf082019-02-25 08:14:49 +0000591};
592
593static struct __prci_wrpll_data __prci_ddrpll_data = {
594 .cfg0_offs = PRCI_DDRPLLCFG0_OFFSET,
Pragnesh Patel54ce0e02020-05-29 11:33:29 +0530595 .cfg1_offs = PRCI_DDRPLLCFG1_OFFSET,
Pragnesh Patel1790bce2020-05-29 11:33:30 +0530596 .release_reset = __prci_ddr_release_reset,
Anup Patel42fdf082019-02-25 08:14:49 +0000597};
598
599static struct __prci_wrpll_data __prci_gemgxlpll_data = {
600 .cfg0_offs = PRCI_GEMGXLPLLCFG0_OFFSET,
Pragnesh Patel54ce0e02020-05-29 11:33:29 +0530601 .cfg1_offs = PRCI_GEMGXLPLLCFG1_OFFSET,
Pragnesh Patele848dba2020-05-29 11:33:31 +0530602 .release_reset = __prci_ethernet_release_reset,
Anup Patel42fdf082019-02-25 08:14:49 +0000603};
604
605/*
606 * List of clock controls provided by the PRCI
607 */
608
609static struct __prci_clock __prci_init_clocks[] = {
610 [PRCI_CLK_COREPLL] = {
611 .name = "corepll",
612 .parent_name = "hfclk",
613 .ops = &sifive_fu540_prci_wrpll_clk_ops,
614 .pwd = &__prci_corepll_data,
615 },
616 [PRCI_CLK_DDRPLL] = {
617 .name = "ddrpll",
618 .parent_name = "hfclk",
Pragnesh Patel1790bce2020-05-29 11:33:30 +0530619 .ops = &sifive_fu540_prci_wrpll_clk_ops,
Anup Patel42fdf082019-02-25 08:14:49 +0000620 .pwd = &__prci_ddrpll_data,
621 },
622 [PRCI_CLK_GEMGXLPLL] = {
623 .name = "gemgxlpll",
624 .parent_name = "hfclk",
625 .ops = &sifive_fu540_prci_wrpll_clk_ops,
626 .pwd = &__prci_gemgxlpll_data,
627 },
628 [PRCI_CLK_TLCLK] = {
629 .name = "tlclk",
630 .parent_name = "corepll",
631 .ops = &sifive_fu540_prci_tlclksel_clk_ops,
632 },
633};
634
Anup Patel9a99add2019-06-25 06:31:21 +0000635static ulong sifive_fu540_prci_parent_rate(struct __prci_clock *pc)
636{
637 ulong parent_rate;
638 struct __prci_clock *p;
639
640 if (strcmp(pc->parent_name, "corepll") == 0) {
641 p = &__prci_init_clocks[PRCI_CLK_COREPLL];
642 if (!p->pd || !p->ops->recalc_rate)
643 return -ENXIO;
644
645 return p->ops->recalc_rate(p, sifive_fu540_prci_parent_rate(p));
646 }
647
648 if (strcmp(pc->parent_name, "rtcclk") == 0)
649 parent_rate = clk_get_rate(&pc->pd->parent_rtcclk);
650 else
651 parent_rate = clk_get_rate(&pc->pd->parent_hfclk);
652
653 return parent_rate;
654}
655
Anup Patel42fdf082019-02-25 08:14:49 +0000656static ulong sifive_fu540_prci_get_rate(struct clk *clk)
657{
658 struct __prci_clock *pc;
659
660 if (ARRAY_SIZE(__prci_init_clocks) <= clk->id)
661 return -ENXIO;
662
663 pc = &__prci_init_clocks[clk->id];
664 if (!pc->pd || !pc->ops->recalc_rate)
665 return -ENXIO;
666
Anup Patel9a99add2019-06-25 06:31:21 +0000667 return pc->ops->recalc_rate(pc, sifive_fu540_prci_parent_rate(pc));
Anup Patel42fdf082019-02-25 08:14:49 +0000668}
669
670static ulong sifive_fu540_prci_set_rate(struct clk *clk, ulong rate)
671{
672 int err;
673 struct __prci_clock *pc;
674
675 if (ARRAY_SIZE(__prci_init_clocks) <= clk->id)
676 return -ENXIO;
677
678 pc = &__prci_init_clocks[clk->id];
679 if (!pc->pd || !pc->ops->set_rate)
680 return -ENXIO;
681
Anup Patel9a99add2019-06-25 06:31:21 +0000682 err = pc->ops->set_rate(pc, rate, sifive_fu540_prci_parent_rate(pc));
Anup Patel42fdf082019-02-25 08:14:49 +0000683 if (err)
684 return err;
685
686 return rate;
687}
688
Pragnesh Patel54ce0e02020-05-29 11:33:29 +0530689static int sifive_fu540_prci_enable(struct clk *clk)
690{
691 struct __prci_clock *pc;
692 int ret = 0;
693
694 if (ARRAY_SIZE(__prci_init_clocks) <= clk->id)
695 return -ENXIO;
696
697 pc = &__prci_init_clocks[clk->id];
698 if (!pc->pd)
699 return -ENXIO;
700
701 if (pc->ops->enable_clk)
702 ret = pc->ops->enable_clk(pc, 1);
703
704 return ret;
705}
706
707static int sifive_fu540_prci_disable(struct clk *clk)
708{
709 struct __prci_clock *pc;
710 int ret = 0;
711
712 if (ARRAY_SIZE(__prci_init_clocks) <= clk->id)
713 return -ENXIO;
714
715 pc = &__prci_init_clocks[clk->id];
716 if (!pc->pd)
717 return -ENXIO;
718
719 if (pc->ops->enable_clk)
720 ret = pc->ops->enable_clk(pc, 0);
721
722 return ret;
723}
724
Anup Patel42fdf082019-02-25 08:14:49 +0000725static int sifive_fu540_prci_probe(struct udevice *dev)
726{
727 int i, err;
728 struct __prci_clock *pc;
729 struct __prci_data *pd = dev_get_priv(dev);
730
Anup Patel9a99add2019-06-25 06:31:21 +0000731 pd->va = (void *)dev_read_addr(dev);
732 if (IS_ERR(pd->va))
733 return PTR_ERR(pd->va);
Anup Patel42fdf082019-02-25 08:14:49 +0000734
Anup Patel9a99add2019-06-25 06:31:21 +0000735 err = clk_get_by_index(dev, 0, &pd->parent_hfclk);
Anup Patel42fdf082019-02-25 08:14:49 +0000736 if (err)
737 return err;
738
Anup Patel9a99add2019-06-25 06:31:21 +0000739 err = clk_get_by_index(dev, 1, &pd->parent_rtcclk);
740 if (err)
741 return err;
742
Anup Patel42fdf082019-02-25 08:14:49 +0000743 for (i = 0; i < ARRAY_SIZE(__prci_init_clocks); ++i) {
744 pc = &__prci_init_clocks[i];
745 pc->pd = pd;
746 if (pc->pwd)
Pragnesh Patel54ce0e02020-05-29 11:33:29 +0530747 __prci_wrpll_read_cfg0(pd, pc->pwd);
Anup Patel42fdf082019-02-25 08:14:49 +0000748 }
749
750 return 0;
751}
752
753static struct clk_ops sifive_fu540_prci_ops = {
754 .set_rate = sifive_fu540_prci_set_rate,
755 .get_rate = sifive_fu540_prci_get_rate,
Pragnesh Patel54ce0e02020-05-29 11:33:29 +0530756 .enable = sifive_fu540_prci_enable,
757 .disable = sifive_fu540_prci_disable,
Anup Patel42fdf082019-02-25 08:14:49 +0000758};
759
760static const struct udevice_id sifive_fu540_prci_ids[] = {
Anup Patel9a99add2019-06-25 06:31:21 +0000761 { .compatible = "sifive,fu540-c000-prci" },
Anup Patel42fdf082019-02-25 08:14:49 +0000762 { }
763};
764
765U_BOOT_DRIVER(sifive_fu540_prci) = {
766 .name = "sifive-fu540-prci",
767 .id = UCLASS_CLK,
768 .of_match = sifive_fu540_prci_ids,
769 .probe = sifive_fu540_prci_probe,
770 .ops = &sifive_fu540_prci_ops,
771 .priv_auto_alloc_size = sizeof(struct __prci_data),
772};