1 // 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
31 #include <common.h>
32 #include <asm/io.h>
33 #include <clk-uclass.h>
34 #include <clk.h>
35 #include <div64.h>
36 #include <dm.h>
37 #include <errno.h>
38
39 #include <linux/math64.h>
40 #include <linux/clk/analogbits-wrpll-cln28hpc.h>
41 #include <dt-bindings/clock/sifive-fu540-prci.h>
42
43 /*
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 */
160 struct __prci_data {
161 void *va;
162 struct clk parent_hfclk;
163 struct clk parent_rtcclk;
164 };
165
166 /**
167 * struct __prci_wrpll_data - WRPLL configuration and integration data
168 * @c: WRPLL current configuration record
169 * @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)
171 * @cfg0_offs: WRPLL CFG0 register offset (in bytes) from the PRCI base address
172 *
173 * @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.
176 */
177 struct __prci_wrpll_data {
178 struct wrpll_cfg c;
179 void (*enable_bypass)(struct __prci_data *pd);
180 void (*disable_bypass)(struct __prci_data *pd);
181 u8 cfg0_offs;
182 };
183
184 struct __prci_clock;
185
186 /* struct __prci_clock_ops - clock operations */
187 struct __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
202 * @ops: struct __prci_clock_ops for control
203 * @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 */
209 struct __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 */
__prci_readl(struct __prci_data * pd,u32 offs)234 static u32 __prci_readl(struct __prci_data *pd, u32 offs)
235 {
236 return readl(pd->va + offs);
237 }
238
__prci_writel(u32 v,u32 offs,struct __prci_data * pd)239 static void __prci_writel(u32 v, u32 offs, struct __prci_data *pd)
240 {
241 writel(v, pd->va + offs);
242 }
243
244 /* WRPLL-related private functions */
245
246 /**
247 * __prci_wrpll_unpack() - unpack WRPLL configuration registers into parameters
248 * @c: ptr to a struct wrpll_cfg record to write config into
249 * @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 */
__prci_wrpll_unpack(struct wrpll_cfg * c,u32 r)260 static void __prci_wrpll_unpack(struct wrpll_cfg *c, u32 r)
261 {
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
283 /* external feedback mode not supported */
284 c->flags |= WRPLL_FLAGS_INT_FEEDBACK_MASK;
285 }
286
287 /**
288 * __prci_wrpll_pack() - pack PLL configuration parameters into a register value
289 * @c: pointer to a struct wrpll_cfg record containing the PLL's cfg
290 *
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 */
__prci_wrpll_pack(const struct wrpll_cfg * c)302 static u32 __prci_wrpll_pack(const struct wrpll_cfg *c)
303 {
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;
310
311 /* external feedback mode not supported */
312 r |= PRCI_COREPLLCFG0_FSE_MASK;
313
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 */
__prci_wrpll_read_cfg(struct __prci_data * pd,struct __prci_wrpll_data * pwd)329 static 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 */
__prci_wrpll_write_cfg(struct __prci_data * pd,struct __prci_wrpll_data * pwd,struct wrpll_cfg * c)349 static void __prci_wrpll_write_cfg(struct __prci_data *pd,
350 struct __prci_wrpll_data *pwd,
351 struct wrpll_cfg *c)
352 {
353 __prci_writel(__prci_wrpll_pack(c), pwd->cfg0_offs, pd);
354
355 memcpy(&pwd->c, c, sizeof(*c));
356 }
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 */
__prci_coreclksel_use_hfclk(struct __prci_data * pd)369 static 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 */
__prci_coreclksel_use_corepll(struct __prci_data * pd)389 static 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
sifive_fu540_prci_wrpll_recalc_rate(struct __prci_clock * pc,unsigned long parent_rate)400 static 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
406 return wrpll_calc_output_rate(&pwd->c, parent_rate);
407 }
408
sifive_fu540_prci_wrpll_round_rate(struct __prci_clock * pc,unsigned long rate,unsigned long * parent_rate)409 static 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;
415 struct wrpll_cfg c;
416
417 memcpy(&c, &pwd->c, sizeof(c));
418
419 wrpll_configure_for_rate(&c, rate, *parent_rate);
420
421 return wrpll_calc_output_rate(&c, *parent_rate);
422 }
423
sifive_fu540_prci_wrpll_set_rate(struct __prci_clock * pc,unsigned long rate,unsigned long parent_rate)424 static 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
432 r = wrpll_configure_for_rate(&pwd->c, rate, parent_rate);
433 if (r)
434 return r;
435
436 if (pwd->enable_bypass)
437 pwd->enable_bypass(pd);
438
439 __prci_wrpll_write_cfg(pd, pwd, &pwd->c);
440
441 udelay(wrpll_calc_max_lock_us(&pwd->c));
442
443 if (pwd->disable_bypass)
444 pwd->disable_bypass(pd);
445
446 return 0;
447 }
448
449 static 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
455 static 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
sifive_fu540_prci_tlclksel_recalc_rate(struct __prci_clock * pc,unsigned long parent_rate)461 static 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
476 static 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
484 static struct __prci_wrpll_data __prci_corepll_data = {
485 .cfg0_offs = PRCI_COREPLLCFG0_OFFSET,
486 .enable_bypass = __prci_coreclksel_use_hfclk,
487 .disable_bypass = __prci_coreclksel_use_corepll,
488 };
489
490 static struct __prci_wrpll_data __prci_ddrpll_data = {
491 .cfg0_offs = PRCI_DDRPLLCFG0_OFFSET,
492 };
493
494 static 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
502 static 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
sifive_fu540_prci_parent_rate(struct __prci_clock * pc)528 static 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
sifive_fu540_prci_get_rate(struct clk * clk)549 static 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
560 return pc->ops->recalc_rate(pc, sifive_fu540_prci_parent_rate(pc));
561 }
562
sifive_fu540_prci_set_rate(struct clk * clk,ulong rate)563 static 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
575 err = pc->ops->set_rate(pc, rate, sifive_fu540_prci_parent_rate(pc));
576 if (err)
577 return err;
578
579 return rate;
580 }
581
sifive_fu540_prci_probe(struct udevice * dev)582 static 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
588 pd->va = (void *)dev_read_addr(dev);
589 if (IS_ERR(pd->va))
590 return PTR_ERR(pd->va);
591
592 err = clk_get_by_index(dev, 0, &pd->parent_hfclk);
593 if (err)
594 return err;
595
596 err = clk_get_by_index(dev, 1, &pd->parent_rtcclk);
597 if (err)
598 return err;
599
600 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
610 static 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
615 static const struct udevice_id sifive_fu540_prci_ids[] = {
616 { .compatible = "sifive,fu540-c000-prci" },
617 { }
618 };
619
620 U_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 };
628