clk: sifive: fu540-prci: Release ethernet clock reset
[pandora-u-boot.git] / drivers / clk / sifive / fu540-prci.c
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 #include <linux/delay.h>
39 #include <linux/err.h>
40
41 #include <linux/math64.h>
42 #include <linux/clk/analogbits-wrpll-cln28hpc.h>
43 #include <dt-bindings/clock/sifive-fu540-prci.h>
44
45 /*
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
72 /* 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
77 /* 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
96 #define PRCI_DDRPLLCFG1_CKE_SHIFT       31
97 #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
123 #define PRCI_GEMGXLPLLCFG1_CKE_SHIFT    31
124 #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 */
151 #define PRCI_CLKMUXSTATUSREG_OFFSET     0x2c
152 #define PRCI_CLKMUXSTATUSREG_TLCLKSEL_STATUS_SHIFT 1
153 #define PRCI_CLKMUXSTATUSREG_TLCLKSEL_STATUS_MASK \
154                         (0x1 << PRCI_CLKMUXSTATUSREG_TLCLKSEL_STATUS_SHIFT)
155
156 /* 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
162 /*
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  */
173 struct __prci_data {
174         void *va;
175         struct clk parent_hfclk;
176         struct clk parent_rtcclk;
177 };
178
179 /**
180  * struct __prci_wrpll_data - WRPLL configuration and integration data
181  * @c: WRPLL current configuration record
182  * @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)
184  * @cfg0_offs: WRPLL CFG0 register offset (in bytes) from the PRCI base address
185  * @cfg1_offs: WRPLL CFG1 register offset (in bytes) from the PRCI base address
186  * @release_reset: fn ptr to code to release clock reset
187  *
188  * @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.
191  */
192 struct __prci_wrpll_data {
193         struct wrpll_cfg c;
194         void (*enable_bypass)(struct __prci_data *pd);
195         void (*disable_bypass)(struct __prci_data *pd);
196         u8 cfg0_offs;
197         u8 cfg1_offs;
198         void (*release_reset)(struct __prci_data *pd);
199 };
200
201 struct __prci_clock;
202
203 /* struct __prci_clock_ops - clock operations */
204 struct __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);
213         int (*enable_clk)(struct __prci_clock *pc, bool enable);
214 };
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
220  * @ops: struct __prci_clock_ops for control
221  * @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  */
227 struct __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  */
252 static u32 __prci_readl(struct __prci_data *pd, u32 offs)
253 {
254         return readl(pd->va + offs);
255 }
256
257 static void __prci_writel(u32 v, u32 offs, struct __prci_data *pd)
258 {
259         writel(v, pd->va + offs);
260 }
261
262 /* WRPLL-related private functions */
263
264 /**
265  * __prci_wrpll_unpack() - unpack WRPLL configuration registers into parameters
266  * @c: ptr to a struct wrpll_cfg record to write config into
267  * @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  */
278 static void __prci_wrpll_unpack(struct wrpll_cfg *c, u32 r)
279 {
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
301         /* external feedback mode not supported */
302         c->flags |= WRPLL_FLAGS_INT_FEEDBACK_MASK;
303 }
304
305 /**
306  * __prci_wrpll_pack() - pack PLL configuration parameters into a register value
307  * @c: pointer to a struct wrpll_cfg record containing the PLL's cfg
308  *
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  */
320 static u32 __prci_wrpll_pack(const struct wrpll_cfg *c)
321 {
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;
328
329         /* external feedback mode not supported */
330         r |= PRCI_COREPLLCFG0_FSE_MASK;
331
332         return r;
333 }
334
335 /**
336  * __prci_wrpll_read_cfg0() - read the WRPLL configuration from the PRCI
337  * @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  */
347 static void __prci_wrpll_read_cfg0(struct __prci_data *pd,
348                                    struct __prci_wrpll_data *pwd)
349 {
350         __prci_wrpll_unpack(&pwd->c, __prci_readl(pd, pwd->cfg0_offs));
351 }
352
353 /**
354  * __prci_wrpll_write_cfg0() - write WRPLL configuration into the PRCI
355  * @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  */
367 static void __prci_wrpll_write_cfg0(struct __prci_data *pd,
368                                     struct __prci_wrpll_data *pwd,
369                                     struct wrpll_cfg *c)
370 {
371         __prci_writel(__prci_wrpll_pack(c), pwd->cfg0_offs, pd);
372
373         memcpy(&pwd->c, c, sizeof(*c));
374 }
375
376 /**
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  */
383 static 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
390 /* 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  */
401 static 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  */
421 static 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
432 static 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
438         return wrpll_calc_output_rate(&pwd->c, parent_rate);
439 }
440
441 static 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;
447         struct wrpll_cfg c;
448
449         memcpy(&c, &pwd->c, sizeof(c));
450
451         wrpll_configure_for_rate(&c, rate, *parent_rate);
452
453         return wrpll_calc_output_rate(&c, *parent_rate);
454 }
455
456 static 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
464         r = wrpll_configure_for_rate(&pwd->c, rate, parent_rate);
465         if (r)
466                 return r;
467
468         if (pwd->enable_bypass)
469                 pwd->enable_bypass(pd);
470
471         __prci_wrpll_write_cfg0(pd, pwd, &pwd->c);
472
473         udelay(wrpll_calc_max_lock_us(&pwd->c));
474
475         if (pwd->disable_bypass)
476                 pwd->disable_bypass(pd);
477
478         return 0;
479 }
480
481 static 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);
488
489                 if (pwd->release_reset)
490                         pwd->release_reset(pd);
491         } 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
503 static 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,
507         .enable_clk = sifive_fu540_prci_clock_enable,
508 };
509
510 /* TLCLKSEL clock integration */
511
512 static 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
527 static const struct __prci_clock_ops sifive_fu540_prci_tlclksel_clk_ops = {
528         .recalc_rate = sifive_fu540_prci_tlclksel_recalc_rate,
529 };
530
531 /**
532  * __prci_ddr_release_reset() - Release DDR reset
533  * @pd: struct __prci_data * for the PRCI containing the DDRCLK mux reg
534  *
535  */
536 static 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
563 /**
564  * __prci_ethernet_release_reset() - Release ethernet reset
565  * @pd: struct __prci_data * for the PRCI containing the Ethernet CLK mux reg
566  *
567  */
568 static 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
582 /*
583  * PRCI integration data for each WRPLL instance
584  */
585
586 static struct __prci_wrpll_data __prci_corepll_data = {
587         .cfg0_offs = PRCI_COREPLLCFG0_OFFSET,
588         .cfg1_offs = PRCI_COREPLLCFG1_OFFSET,
589         .enable_bypass = __prci_coreclksel_use_hfclk,
590         .disable_bypass = __prci_coreclksel_use_corepll,
591 };
592
593 static struct __prci_wrpll_data __prci_ddrpll_data = {
594         .cfg0_offs = PRCI_DDRPLLCFG0_OFFSET,
595         .cfg1_offs = PRCI_DDRPLLCFG1_OFFSET,
596         .release_reset = __prci_ddr_release_reset,
597 };
598
599 static struct __prci_wrpll_data __prci_gemgxlpll_data = {
600         .cfg0_offs = PRCI_GEMGXLPLLCFG0_OFFSET,
601         .cfg1_offs = PRCI_GEMGXLPLLCFG1_OFFSET,
602         .release_reset = __prci_ethernet_release_reset,
603 };
604
605 /*
606  * List of clock controls provided by the PRCI
607  */
608
609 static 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",
619                 .ops = &sifive_fu540_prci_wrpll_clk_ops,
620                 .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
635 static 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
656 static 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
667         return pc->ops->recalc_rate(pc, sifive_fu540_prci_parent_rate(pc));
668 }
669
670 static 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
682         err = pc->ops->set_rate(pc, rate, sifive_fu540_prci_parent_rate(pc));
683         if (err)
684                 return err;
685
686         return rate;
687 }
688
689 static 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
707 static 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
725 static 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
731         pd->va = (void *)dev_read_addr(dev);
732         if (IS_ERR(pd->va))
733                 return PTR_ERR(pd->va);
734
735         err = clk_get_by_index(dev, 0, &pd->parent_hfclk);
736         if (err)
737                 return err;
738
739         err = clk_get_by_index(dev, 1, &pd->parent_rtcclk);
740         if (err)
741                 return err;
742
743         for (i = 0; i < ARRAY_SIZE(__prci_init_clocks); ++i) {
744                 pc = &__prci_init_clocks[i];
745                 pc->pd = pd;
746                 if (pc->pwd)
747                         __prci_wrpll_read_cfg0(pd, pc->pwd);
748         }
749
750         return 0;
751 }
752
753 static struct clk_ops sifive_fu540_prci_ops = {
754         .set_rate = sifive_fu540_prci_set_rate,
755         .get_rate = sifive_fu540_prci_get_rate,
756         .enable = sifive_fu540_prci_enable,
757         .disable = sifive_fu540_prci_disable,
758 };
759
760 static const struct udevice_id sifive_fu540_prci_ids[] = {
761         { .compatible = "sifive,fu540-c000-prci" },
762         { }
763 };
764
765 U_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 };