blob: ce0769f2d136d9b482069ebefeb905e06afb85a6 [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>
38
39#include <linux/math64.h>
Anup Patel00a156d2019-06-25 06:31:02 +000040#include <linux/clk/analogbits-wrpll-cln28hpc.h>
Anup Patel83d5b502019-06-25 06:31:15 +000041#include <dt-bindings/clock/sifive-fu540-prci.h>
Anup Patel42fdf082019-02-25 08:14:49 +000042
Anup Patel42fdf082019-02-25 08:14:49 +000043/*
44 * EXPECTED_CLK_PARENT_COUNT: how many parent clocks this driver expects:
45 * hfclk and rtcclk
46 */
47#define EXPECTED_CLK_PARENT_COUNT 2
48
49/*
50 * Register offsets and bitmasks
51 */
52
53/* COREPLLCFG0 */
54#define PRCI_COREPLLCFG0_OFFSET 0x4
55#define PRCI_COREPLLCFG0_DIVR_SHIFT 0
56#define PRCI_COREPLLCFG0_DIVR_MASK (0x3f << PRCI_COREPLLCFG0_DIVR_SHIFT)
57#define PRCI_COREPLLCFG0_DIVF_SHIFT 6
58#define PRCI_COREPLLCFG0_DIVF_MASK (0x1ff << PRCI_COREPLLCFG0_DIVF_SHIFT)
59#define PRCI_COREPLLCFG0_DIVQ_SHIFT 15
60#define PRCI_COREPLLCFG0_DIVQ_MASK (0x7 << PRCI_COREPLLCFG0_DIVQ_SHIFT)
61#define PRCI_COREPLLCFG0_RANGE_SHIFT 18
62#define PRCI_COREPLLCFG0_RANGE_MASK (0x7 << PRCI_COREPLLCFG0_RANGE_SHIFT)
63#define PRCI_COREPLLCFG0_BYPASS_SHIFT 24
64#define PRCI_COREPLLCFG0_BYPASS_MASK (0x1 << PRCI_COREPLLCFG0_BYPASS_SHIFT)
65#define PRCI_COREPLLCFG0_FSE_SHIFT 25
66#define PRCI_COREPLLCFG0_FSE_MASK (0x1 << PRCI_COREPLLCFG0_FSE_SHIFT)
67#define PRCI_COREPLLCFG0_LOCK_SHIFT 31
68#define PRCI_COREPLLCFG0_LOCK_MASK (0x1 << PRCI_COREPLLCFG0_LOCK_SHIFT)
69
70/* DDRPLLCFG0 */
71#define PRCI_DDRPLLCFG0_OFFSET 0xc
72#define PRCI_DDRPLLCFG0_DIVR_SHIFT 0
73#define PRCI_DDRPLLCFG0_DIVR_MASK (0x3f << PRCI_DDRPLLCFG0_DIVR_SHIFT)
74#define PRCI_DDRPLLCFG0_DIVF_SHIFT 6
75#define PRCI_DDRPLLCFG0_DIVF_MASK (0x1ff << PRCI_DDRPLLCFG0_DIVF_SHIFT)
76#define PRCI_DDRPLLCFG0_DIVQ_SHIFT 15
77#define PRCI_DDRPLLCFG0_DIVQ_MASK (0x7 << PRCI_DDRPLLCFG0_DIVQ_SHIFT)
78#define PRCI_DDRPLLCFG0_RANGE_SHIFT 18
79#define PRCI_DDRPLLCFG0_RANGE_MASK (0x7 << PRCI_DDRPLLCFG0_RANGE_SHIFT)
80#define PRCI_DDRPLLCFG0_BYPASS_SHIFT 24
81#define PRCI_DDRPLLCFG0_BYPASS_MASK (0x1 << PRCI_DDRPLLCFG0_BYPASS_SHIFT)
82#define PRCI_DDRPLLCFG0_FSE_SHIFT 25
83#define PRCI_DDRPLLCFG0_FSE_MASK (0x1 << PRCI_DDRPLLCFG0_FSE_SHIFT)
84#define PRCI_DDRPLLCFG0_LOCK_SHIFT 31
85#define PRCI_DDRPLLCFG0_LOCK_MASK (0x1 << PRCI_DDRPLLCFG0_LOCK_SHIFT)
86
87/* DDRPLLCFG1 */
88#define PRCI_DDRPLLCFG1_OFFSET 0x10
89#define PRCI_DDRPLLCFG1_CKE_SHIFT 24
90#define PRCI_DDRPLLCFG1_CKE_MASK (0x1 << PRCI_DDRPLLCFG1_CKE_SHIFT)
91
92/* GEMGXLPLLCFG0 */
93#define PRCI_GEMGXLPLLCFG0_OFFSET 0x1c
94#define PRCI_GEMGXLPLLCFG0_DIVR_SHIFT 0
95#define PRCI_GEMGXLPLLCFG0_DIVR_MASK \
96 (0x3f << PRCI_GEMGXLPLLCFG0_DIVR_SHIFT)
97#define PRCI_GEMGXLPLLCFG0_DIVF_SHIFT 6
98#define PRCI_GEMGXLPLLCFG0_DIVF_MASK \
99 (0x1ff << PRCI_GEMGXLPLLCFG0_DIVF_SHIFT)
100#define PRCI_GEMGXLPLLCFG0_DIVQ_SHIFT 15
101#define PRCI_GEMGXLPLLCFG0_DIVQ_MASK (0x7 << PRCI_GEMGXLPLLCFG0_DIVQ_SHIFT)
102#define PRCI_GEMGXLPLLCFG0_RANGE_SHIFT 18
103#define PRCI_GEMGXLPLLCFG0_RANGE_MASK \
104 (0x7 << PRCI_GEMGXLPLLCFG0_RANGE_SHIFT)
105#define PRCI_GEMGXLPLLCFG0_BYPASS_SHIFT 24
106#define PRCI_GEMGXLPLLCFG0_BYPASS_MASK \
107 (0x1 << PRCI_GEMGXLPLLCFG0_BYPASS_SHIFT)
108#define PRCI_GEMGXLPLLCFG0_FSE_SHIFT 25
109#define PRCI_GEMGXLPLLCFG0_FSE_MASK \
110 (0x1 << PRCI_GEMGXLPLLCFG0_FSE_SHIFT)
111#define PRCI_GEMGXLPLLCFG0_LOCK_SHIFT 31
112#define PRCI_GEMGXLPLLCFG0_LOCK_MASK (0x1 << PRCI_GEMGXLPLLCFG0_LOCK_SHIFT)
113
114/* GEMGXLPLLCFG1 */
115#define PRCI_GEMGXLPLLCFG1_OFFSET 0x20
116#define PRCI_GEMGXLPLLCFG1_CKE_SHIFT 24
117#define PRCI_GEMGXLPLLCFG1_CKE_MASK (0x1 << PRCI_GEMGXLPLLCFG1_CKE_SHIFT)
118
119/* CORECLKSEL */
120#define PRCI_CORECLKSEL_OFFSET 0x24
121#define PRCI_CORECLKSEL_CORECLKSEL_SHIFT 0
122#define PRCI_CORECLKSEL_CORECLKSEL_MASK \
123 (0x1 << PRCI_CORECLKSEL_CORECLKSEL_SHIFT)
124
125/* DEVICESRESETREG */
126#define PRCI_DEVICESRESETREG_OFFSET 0x28
127#define PRCI_DEVICESRESETREG_DDR_CTRL_RST_N_SHIFT 0
128#define PRCI_DEVICESRESETREG_DDR_CTRL_RST_N_MASK \
129 (0x1 << PRCI_DEVICESRESETREG_DDR_CTRL_RST_N_SHIFT)
130#define PRCI_DEVICESRESETREG_DDR_AXI_RST_N_SHIFT 1
131#define PRCI_DEVICESRESETREG_DDR_AXI_RST_N_MASK \
132 (0x1 << PRCI_DEVICESRESETREG_DDR_AXI_RST_N_SHIFT)
133#define PRCI_DEVICESRESETREG_DDR_AHB_RST_N_SHIFT 2
134#define PRCI_DEVICESRESETREG_DDR_AHB_RST_N_MASK \
135 (0x1 << PRCI_DEVICESRESETREG_DDR_AHB_RST_N_SHIFT)
136#define PRCI_DEVICESRESETREG_DDR_PHY_RST_N_SHIFT 3
137#define PRCI_DEVICESRESETREG_DDR_PHY_RST_N_MASK \
138 (0x1 << PRCI_DEVICESRESETREG_DDR_PHY_RST_N_SHIFT)
139#define PRCI_DEVICESRESETREG_GEMGXL_RST_N_SHIFT 5
140#define PRCI_DEVICESRESETREG_GEMGXL_RST_N_MASK \
141 (0x1 << PRCI_DEVICESRESETREG_GEMGXL_RST_N_SHIFT)
142
143/* CLKMUXSTATUSREG */
144#define PRCI_CLKMUXSTATUSREG_OFFSET 0x2c
145#define PRCI_CLKMUXSTATUSREG_TLCLKSEL_STATUS_SHIFT 1
146#define PRCI_CLKMUXSTATUSREG_TLCLKSEL_STATUS_MASK \
147 (0x1 << PRCI_CLKMUXSTATUSREG_TLCLKSEL_STATUS_SHIFT)
148
149/*
150 * Private structures
151 */
152
153/**
154 * struct __prci_data - per-device-instance data
155 * @va: base virtual address of the PRCI IP block
156 * @parent: parent clk instance
157 *
158 * PRCI per-device instance data
159 */
160struct __prci_data {
Anup Patel9a99add2019-06-25 06:31:21 +0000161 void *va;
162 struct clk parent_hfclk;
163 struct clk parent_rtcclk;
Anup Patel42fdf082019-02-25 08:14:49 +0000164};
165
166/**
167 * struct __prci_wrpll_data - WRPLL configuration and integration data
168 * @c: WRPLL current configuration record
Anup Patel9a99add2019-06-25 06:31:21 +0000169 * @enable_bypass: fn ptr to code to bypass the WRPLL (if applicable; else NULL)
170 * @disable_bypass: fn ptr to code to not bypass the WRPLL (or NULL)
Anup Patel42fdf082019-02-25 08:14:49 +0000171 * @cfg0_offs: WRPLL CFG0 register offset (in bytes) from the PRCI base address
172 *
Anup Patel9a99add2019-06-25 06:31:21 +0000173 * @enable_bypass and @disable_bypass are used for WRPLL instances
174 * that contain a separate external glitchless clock mux downstream
175 * from the PLL. The WRPLL internal bypass mux is not glitchless.
Anup Patel42fdf082019-02-25 08:14:49 +0000176 */
177struct __prci_wrpll_data {
Anup Patel6f7b5a22019-06-25 06:31:08 +0000178 struct wrpll_cfg c;
Anup Patel9a99add2019-06-25 06:31:21 +0000179 void (*enable_bypass)(struct __prci_data *pd);
180 void (*disable_bypass)(struct __prci_data *pd);
Anup Patel42fdf082019-02-25 08:14:49 +0000181 u8 cfg0_offs;
182};
183
184struct __prci_clock;
185
Anup Patel9a99add2019-06-25 06:31:21 +0000186/* struct __prci_clock_ops - clock operations */
Anup Patel42fdf082019-02-25 08:14:49 +0000187struct __prci_clock_ops {
188 int (*set_rate)(struct __prci_clock *pc,
189 unsigned long rate,
190 unsigned long parent_rate);
191 unsigned long (*round_rate)(struct __prci_clock *pc,
192 unsigned long rate,
193 unsigned long *parent_rate);
194 unsigned long (*recalc_rate)(struct __prci_clock *pc,
195 unsigned long parent_rate);
196};
197
198/**
199 * struct __prci_clock - describes a clock device managed by PRCI
200 * @name: user-readable clock name string - should match the manual
201 * @parent_name: parent name for this clock
Anup Patel9a99add2019-06-25 06:31:21 +0000202 * @ops: struct __prci_clock_ops for control
Anup Patel42fdf082019-02-25 08:14:49 +0000203 * @pwd: WRPLL-specific data, associated with this clock (if not NULL)
204 * @pd: PRCI-specific data associated with this clock (if not NULL)
205 *
206 * PRCI clock data. Used by the PRCI driver to register PRCI-provided
207 * clocks to the Linux clock infrastructure.
208 */
209struct __prci_clock {
210 const char *name;
211 const char *parent_name;
212 const struct __prci_clock_ops *ops;
213 struct __prci_wrpll_data *pwd;
214 struct __prci_data *pd;
215};
216
217/*
218 * Private functions
219 */
220
221/**
222 * __prci_readl() - read from a PRCI register
223 * @pd: PRCI context
224 * @offs: register offset to read from (in bytes, from PRCI base address)
225 *
226 * Read the register located at offset @offs from the base virtual
227 * address of the PRCI register target described by @pd, and return
228 * the value to the caller.
229 *
230 * Context: Any context.
231 *
232 * Return: the contents of the register described by @pd and @offs.
233 */
234static u32 __prci_readl(struct __prci_data *pd, u32 offs)
235{
Anup Patel9a99add2019-06-25 06:31:21 +0000236 return readl(pd->va + offs);
Anup Patel42fdf082019-02-25 08:14:49 +0000237}
238
239static void __prci_writel(u32 v, u32 offs, struct __prci_data *pd)
240{
Anup Patel9a99add2019-06-25 06:31:21 +0000241 writel(v, pd->va + offs);
Anup Patel42fdf082019-02-25 08:14:49 +0000242}
243
244/* WRPLL-related private functions */
245
246/**
247 * __prci_wrpll_unpack() - unpack WRPLL configuration registers into parameters
Anup Patel6f7b5a22019-06-25 06:31:08 +0000248 * @c: ptr to a struct wrpll_cfg record to write config into
Anup Patel42fdf082019-02-25 08:14:49 +0000249 * @r: value read from the PRCI PLL configuration register
250 *
251 * Given a value @r read from an FU540 PRCI PLL configuration register,
252 * split it into fields and populate it into the WRPLL configuration record
253 * pointed to by @c.
254 *
255 * The COREPLLCFG0 macros are used below, but the other *PLLCFG0 macros
256 * have the same register layout.
257 *
258 * Context: Any context.
259 */
Anup Patel6f7b5a22019-06-25 06:31:08 +0000260static void __prci_wrpll_unpack(struct wrpll_cfg *c, u32 r)
Anup Patel42fdf082019-02-25 08:14:49 +0000261{
262 u32 v;
263
264 v = r & PRCI_COREPLLCFG0_DIVR_MASK;
265 v >>= PRCI_COREPLLCFG0_DIVR_SHIFT;
266 c->divr = v;
267
268 v = r & PRCI_COREPLLCFG0_DIVF_MASK;
269 v >>= PRCI_COREPLLCFG0_DIVF_SHIFT;
270 c->divf = v;
271
272 v = r & PRCI_COREPLLCFG0_DIVQ_MASK;
273 v >>= PRCI_COREPLLCFG0_DIVQ_SHIFT;
274 c->divq = v;
275
276 v = r & PRCI_COREPLLCFG0_RANGE_MASK;
277 v >>= PRCI_COREPLLCFG0_RANGE_SHIFT;
278 c->range = v;
279
280 c->flags &= (WRPLL_FLAGS_INT_FEEDBACK_MASK |
281 WRPLL_FLAGS_EXT_FEEDBACK_MASK);
282
Anup Patel9a99add2019-06-25 06:31:21 +0000283 /* external feedback mode not supported */
284 c->flags |= WRPLL_FLAGS_INT_FEEDBACK_MASK;
Anup Patel42fdf082019-02-25 08:14:49 +0000285}
286
287/**
288 * __prci_wrpll_pack() - pack PLL configuration parameters into a register value
Anup Patel6f7b5a22019-06-25 06:31:08 +0000289 * @c: pointer to a struct wrpll_cfg record containing the PLL's cfg
Anup Patel42fdf082019-02-25 08:14:49 +0000290 *
291 * Using a set of WRPLL configuration values pointed to by @c,
292 * assemble a PRCI PLL configuration register value, and return it to
293 * the caller.
294 *
295 * Context: Any context. Caller must ensure that the contents of the
296 * record pointed to by @c do not change during the execution
297 * of this function.
298 *
299 * Returns: a value suitable for writing into a PRCI PLL configuration
300 * register
301 */
Anup Patel9a99add2019-06-25 06:31:21 +0000302static u32 __prci_wrpll_pack(const struct wrpll_cfg *c)
Anup Patel42fdf082019-02-25 08:14:49 +0000303{
304 u32 r = 0;
305
306 r |= c->divr << PRCI_COREPLLCFG0_DIVR_SHIFT;
307 r |= c->divf << PRCI_COREPLLCFG0_DIVF_SHIFT;
308 r |= c->divq << PRCI_COREPLLCFG0_DIVQ_SHIFT;
309 r |= c->range << PRCI_COREPLLCFG0_RANGE_SHIFT;
Anup Patel9a99add2019-06-25 06:31:21 +0000310
311 /* external feedback mode not supported */
312 r |= PRCI_COREPLLCFG0_FSE_MASK;
Anup Patel42fdf082019-02-25 08:14:49 +0000313
314 return r;
315}
316
317/**
318 * __prci_wrpll_read_cfg() - read the WRPLL configuration from the PRCI
319 * @pd: PRCI context
320 * @pwd: PRCI WRPLL metadata
321 *
322 * Read the current configuration of the PLL identified by @pwd from
323 * the PRCI identified by @pd, and store it into the local configuration
324 * cache in @pwd.
325 *
326 * Context: Any context. Caller must prevent the records pointed to by
327 * @pd and @pwd from changing during execution.
328 */
329static void __prci_wrpll_read_cfg(struct __prci_data *pd,
330 struct __prci_wrpll_data *pwd)
331{
332 __prci_wrpll_unpack(&pwd->c, __prci_readl(pd, pwd->cfg0_offs));
333}
334
335/**
336 * __prci_wrpll_write_cfg() - write WRPLL configuration into the PRCI
337 * @pd: PRCI context
338 * @pwd: PRCI WRPLL metadata
339 * @c: WRPLL configuration record to write
340 *
341 * Write the WRPLL configuration described by @c into the WRPLL
342 * configuration register identified by @pwd in the PRCI instance
343 * described by @c. Make a cached copy of the WRPLL's current
344 * configuration so it can be used by other code.
345 *
346 * Context: Any context. Caller must prevent the records pointed to by
347 * @pd and @pwd from changing during execution.
348 */
349static void __prci_wrpll_write_cfg(struct __prci_data *pd,
350 struct __prci_wrpll_data *pwd,
Anup Patel6f7b5a22019-06-25 06:31:08 +0000351 struct wrpll_cfg *c)
Anup Patel42fdf082019-02-25 08:14:49 +0000352{
353 __prci_writel(__prci_wrpll_pack(c), pwd->cfg0_offs, pd);
354
Anup Patel9a99add2019-06-25 06:31:21 +0000355 memcpy(&pwd->c, c, sizeof(*c));
Anup Patel42fdf082019-02-25 08:14:49 +0000356}
357
358/* Core clock mux control */
359
360/**
361 * __prci_coreclksel_use_hfclk() - switch the CORECLK mux to output HFCLK
362 * @pd: struct __prci_data * for the PRCI containing the CORECLK mux reg
363 *
364 * Switch the CORECLK mux to the HFCLK input source; return once complete.
365 *
366 * Context: Any context. Caller must prevent concurrent changes to the
367 * PRCI_CORECLKSEL_OFFSET register.
368 */
369static void __prci_coreclksel_use_hfclk(struct __prci_data *pd)
370{
371 u32 r;
372
373 r = __prci_readl(pd, PRCI_CORECLKSEL_OFFSET);
374 r |= PRCI_CORECLKSEL_CORECLKSEL_MASK;
375 __prci_writel(r, PRCI_CORECLKSEL_OFFSET, pd);
376
377 r = __prci_readl(pd, PRCI_CORECLKSEL_OFFSET); /* barrier */
378}
379
380/**
381 * __prci_coreclksel_use_corepll() - switch the CORECLK mux to output COREPLL
382 * @pd: struct __prci_data * for the PRCI containing the CORECLK mux reg
383 *
384 * Switch the CORECLK mux to the PLL output clock; return once complete.
385 *
386 * Context: Any context. Caller must prevent concurrent changes to the
387 * PRCI_CORECLKSEL_OFFSET register.
388 */
389static void __prci_coreclksel_use_corepll(struct __prci_data *pd)
390{
391 u32 r;
392
393 r = __prci_readl(pd, PRCI_CORECLKSEL_OFFSET);
394 r &= ~PRCI_CORECLKSEL_CORECLKSEL_MASK;
395 __prci_writel(r, PRCI_CORECLKSEL_OFFSET, pd);
396
397 r = __prci_readl(pd, PRCI_CORECLKSEL_OFFSET); /* barrier */
398}
399
400static unsigned long sifive_fu540_prci_wrpll_recalc_rate(
401 struct __prci_clock *pc,
402 unsigned long parent_rate)
403{
404 struct __prci_wrpll_data *pwd = pc->pwd;
405
Anup Patel6f7b5a22019-06-25 06:31:08 +0000406 return wrpll_calc_output_rate(&pwd->c, parent_rate);
Anup Patel42fdf082019-02-25 08:14:49 +0000407}
408
409static unsigned long sifive_fu540_prci_wrpll_round_rate(
410 struct __prci_clock *pc,
411 unsigned long rate,
412 unsigned long *parent_rate)
413{
414 struct __prci_wrpll_data *pwd = pc->pwd;
Anup Patel6f7b5a22019-06-25 06:31:08 +0000415 struct wrpll_cfg c;
Anup Patel42fdf082019-02-25 08:14:49 +0000416
417 memcpy(&c, &pwd->c, sizeof(c));
418
Anup Patel6f7b5a22019-06-25 06:31:08 +0000419 wrpll_configure_for_rate(&c, rate, *parent_rate);
Anup Patel42fdf082019-02-25 08:14:49 +0000420
Anup Patel6f7b5a22019-06-25 06:31:08 +0000421 return wrpll_calc_output_rate(&c, *parent_rate);
Anup Patel42fdf082019-02-25 08:14:49 +0000422}
423
424static int sifive_fu540_prci_wrpll_set_rate(struct __prci_clock *pc,
425 unsigned long rate,
426 unsigned long parent_rate)
427{
428 struct __prci_wrpll_data *pwd = pc->pwd;
429 struct __prci_data *pd = pc->pd;
430 int r;
431
Anup Patel6f7b5a22019-06-25 06:31:08 +0000432 r = wrpll_configure_for_rate(&pwd->c, rate, parent_rate);
Anup Patel42fdf082019-02-25 08:14:49 +0000433 if (r)
Anup Patel9a99add2019-06-25 06:31:21 +0000434 return r;
Anup Patel42fdf082019-02-25 08:14:49 +0000435
Anup Patel9a99add2019-06-25 06:31:21 +0000436 if (pwd->enable_bypass)
437 pwd->enable_bypass(pd);
Anup Patel42fdf082019-02-25 08:14:49 +0000438
439 __prci_wrpll_write_cfg(pd, pwd, &pwd->c);
440
Anup Patel6f7b5a22019-06-25 06:31:08 +0000441 udelay(wrpll_calc_max_lock_us(&pwd->c));
Anup Patel42fdf082019-02-25 08:14:49 +0000442
Anup Patel9a99add2019-06-25 06:31:21 +0000443 if (pwd->disable_bypass)
444 pwd->disable_bypass(pd);
Anup Patel42fdf082019-02-25 08:14:49 +0000445
446 return 0;
447}
448
449static const struct __prci_clock_ops sifive_fu540_prci_wrpll_clk_ops = {
450 .set_rate = sifive_fu540_prci_wrpll_set_rate,
451 .round_rate = sifive_fu540_prci_wrpll_round_rate,
452 .recalc_rate = sifive_fu540_prci_wrpll_recalc_rate,
453};
454
455static const struct __prci_clock_ops sifive_fu540_prci_wrpll_ro_clk_ops = {
456 .recalc_rate = sifive_fu540_prci_wrpll_recalc_rate,
457};
458
459/* TLCLKSEL clock integration */
460
461static unsigned long sifive_fu540_prci_tlclksel_recalc_rate(
462 struct __prci_clock *pc,
463 unsigned long parent_rate)
464{
465 struct __prci_data *pd = pc->pd;
466 u32 v;
467 u8 div;
468
469 v = __prci_readl(pd, PRCI_CLKMUXSTATUSREG_OFFSET);
470 v &= PRCI_CLKMUXSTATUSREG_TLCLKSEL_STATUS_MASK;
471 div = v ? 1 : 2;
472
473 return div_u64(parent_rate, div);
474}
475
476static const struct __prci_clock_ops sifive_fu540_prci_tlclksel_clk_ops = {
477 .recalc_rate = sifive_fu540_prci_tlclksel_recalc_rate,
478};
479
480/*
481 * PRCI integration data for each WRPLL instance
482 */
483
484static struct __prci_wrpll_data __prci_corepll_data = {
485 .cfg0_offs = PRCI_COREPLLCFG0_OFFSET,
Anup Patel9a99add2019-06-25 06:31:21 +0000486 .enable_bypass = __prci_coreclksel_use_hfclk,
487 .disable_bypass = __prci_coreclksel_use_corepll,
Anup Patel42fdf082019-02-25 08:14:49 +0000488};
489
490static struct __prci_wrpll_data __prci_ddrpll_data = {
491 .cfg0_offs = PRCI_DDRPLLCFG0_OFFSET,
492};
493
494static struct __prci_wrpll_data __prci_gemgxlpll_data = {
495 .cfg0_offs = PRCI_GEMGXLPLLCFG0_OFFSET,
496};
497
498/*
499 * List of clock controls provided by the PRCI
500 */
501
502static struct __prci_clock __prci_init_clocks[] = {
503 [PRCI_CLK_COREPLL] = {
504 .name = "corepll",
505 .parent_name = "hfclk",
506 .ops = &sifive_fu540_prci_wrpll_clk_ops,
507 .pwd = &__prci_corepll_data,
508 },
509 [PRCI_CLK_DDRPLL] = {
510 .name = "ddrpll",
511 .parent_name = "hfclk",
512 .ops = &sifive_fu540_prci_wrpll_ro_clk_ops,
513 .pwd = &__prci_ddrpll_data,
514 },
515 [PRCI_CLK_GEMGXLPLL] = {
516 .name = "gemgxlpll",
517 .parent_name = "hfclk",
518 .ops = &sifive_fu540_prci_wrpll_clk_ops,
519 .pwd = &__prci_gemgxlpll_data,
520 },
521 [PRCI_CLK_TLCLK] = {
522 .name = "tlclk",
523 .parent_name = "corepll",
524 .ops = &sifive_fu540_prci_tlclksel_clk_ops,
525 },
526};
527
Anup Patel9a99add2019-06-25 06:31:21 +0000528static ulong sifive_fu540_prci_parent_rate(struct __prci_clock *pc)
529{
530 ulong parent_rate;
531 struct __prci_clock *p;
532
533 if (strcmp(pc->parent_name, "corepll") == 0) {
534 p = &__prci_init_clocks[PRCI_CLK_COREPLL];
535 if (!p->pd || !p->ops->recalc_rate)
536 return -ENXIO;
537
538 return p->ops->recalc_rate(p, sifive_fu540_prci_parent_rate(p));
539 }
540
541 if (strcmp(pc->parent_name, "rtcclk") == 0)
542 parent_rate = clk_get_rate(&pc->pd->parent_rtcclk);
543 else
544 parent_rate = clk_get_rate(&pc->pd->parent_hfclk);
545
546 return parent_rate;
547}
548
Anup Patel42fdf082019-02-25 08:14:49 +0000549static ulong sifive_fu540_prci_get_rate(struct clk *clk)
550{
551 struct __prci_clock *pc;
552
553 if (ARRAY_SIZE(__prci_init_clocks) <= clk->id)
554 return -ENXIO;
555
556 pc = &__prci_init_clocks[clk->id];
557 if (!pc->pd || !pc->ops->recalc_rate)
558 return -ENXIO;
559
Anup Patel9a99add2019-06-25 06:31:21 +0000560 return pc->ops->recalc_rate(pc, sifive_fu540_prci_parent_rate(pc));
Anup Patel42fdf082019-02-25 08:14:49 +0000561}
562
563static ulong sifive_fu540_prci_set_rate(struct clk *clk, ulong rate)
564{
565 int err;
566 struct __prci_clock *pc;
567
568 if (ARRAY_SIZE(__prci_init_clocks) <= clk->id)
569 return -ENXIO;
570
571 pc = &__prci_init_clocks[clk->id];
572 if (!pc->pd || !pc->ops->set_rate)
573 return -ENXIO;
574
Anup Patel9a99add2019-06-25 06:31:21 +0000575 err = pc->ops->set_rate(pc, rate, sifive_fu540_prci_parent_rate(pc));
Anup Patel42fdf082019-02-25 08:14:49 +0000576 if (err)
577 return err;
578
579 return rate;
580}
581
582static int sifive_fu540_prci_probe(struct udevice *dev)
583{
584 int i, err;
585 struct __prci_clock *pc;
586 struct __prci_data *pd = dev_get_priv(dev);
587
Anup Patel9a99add2019-06-25 06:31:21 +0000588 pd->va = (void *)dev_read_addr(dev);
589 if (IS_ERR(pd->va))
590 return PTR_ERR(pd->va);
Anup Patel42fdf082019-02-25 08:14:49 +0000591
Anup Patel9a99add2019-06-25 06:31:21 +0000592 err = clk_get_by_index(dev, 0, &pd->parent_hfclk);
Anup Patel42fdf082019-02-25 08:14:49 +0000593 if (err)
594 return err;
595
Anup Patel9a99add2019-06-25 06:31:21 +0000596 err = clk_get_by_index(dev, 1, &pd->parent_rtcclk);
597 if (err)
598 return err;
599
Anup Patel42fdf082019-02-25 08:14:49 +0000600 for (i = 0; i < ARRAY_SIZE(__prci_init_clocks); ++i) {
601 pc = &__prci_init_clocks[i];
602 pc->pd = pd;
603 if (pc->pwd)
604 __prci_wrpll_read_cfg(pd, pc->pwd);
605 }
606
607 return 0;
608}
609
610static struct clk_ops sifive_fu540_prci_ops = {
611 .set_rate = sifive_fu540_prci_set_rate,
612 .get_rate = sifive_fu540_prci_get_rate,
613};
614
615static const struct udevice_id sifive_fu540_prci_ids[] = {
Anup Patel9a99add2019-06-25 06:31:21 +0000616 { .compatible = "sifive,fu540-c000-prci" },
Anup Patel42fdf082019-02-25 08:14:49 +0000617 { }
618};
619
620U_BOOT_DRIVER(sifive_fu540_prci) = {
621 .name = "sifive-fu540-prci",
622 .id = UCLASS_CLK,
623 .of_match = sifive_fu540_prci_ids,
624 .probe = sifive_fu540_prci_probe,
625 .ops = &sifive_fu540_prci_ops,
626 .priv_auto_alloc_size = sizeof(struct __prci_data),
627};