1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25#include <common.h>
26#include <clk-uclass.h>
27#include <clk.h>
28#include <dm.h>
29#include <dm/device_compat.h>
30#include <reset.h>
31#include <asm/io.h>
32#include <asm/arch/reset.h>
33#include <linux/delay.h>
34#include <linux/math64.h>
35#include <dt-bindings/clock/sifive-fu740-prci.h>
36
37#include "fu540-prci.h"
38#include "fu740-prci.h"
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57static u32 __prci_readl(struct __prci_data *pd, u32 offs)
58{
59 return readl(pd->va + offs);
60}
61
62static void __prci_writel(u32 v, u32 offs, struct __prci_data *pd)
63{
64 writel(v, pd->va + offs);
65}
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83static void __prci_wrpll_unpack(struct wrpll_cfg *c, u32 r)
84{
85 u32 v;
86
87 v = r & PRCI_COREPLLCFG0_DIVR_MASK;
88 v >>= PRCI_COREPLLCFG0_DIVR_SHIFT;
89 c->divr = v;
90
91 v = r & PRCI_COREPLLCFG0_DIVF_MASK;
92 v >>= PRCI_COREPLLCFG0_DIVF_SHIFT;
93 c->divf = v;
94
95 v = r & PRCI_COREPLLCFG0_DIVQ_MASK;
96 v >>= PRCI_COREPLLCFG0_DIVQ_SHIFT;
97 c->divq = v;
98
99 v = r & PRCI_COREPLLCFG0_RANGE_MASK;
100 v >>= PRCI_COREPLLCFG0_RANGE_SHIFT;
101 c->range = v;
102
103 c->flags &= (WRPLL_FLAGS_INT_FEEDBACK_MASK |
104 WRPLL_FLAGS_EXT_FEEDBACK_MASK);
105
106
107 c->flags |= WRPLL_FLAGS_INT_FEEDBACK_MASK;
108}
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125static u32 __prci_wrpll_pack(const struct wrpll_cfg *c)
126{
127 u32 r = 0;
128
129 r |= c->divr << PRCI_COREPLLCFG0_DIVR_SHIFT;
130 r |= c->divf << PRCI_COREPLLCFG0_DIVF_SHIFT;
131 r |= c->divq << PRCI_COREPLLCFG0_DIVQ_SHIFT;
132 r |= c->range << PRCI_COREPLLCFG0_RANGE_SHIFT;
133
134
135 r |= PRCI_COREPLLCFG0_FSE_MASK;
136
137 return r;
138}
139
140
141
142
143
144
145
146
147
148
149
150
151
152static void __prci_wrpll_read_cfg0(struct __prci_data *pd,
153 struct __prci_wrpll_data *pwd)
154{
155 __prci_wrpll_unpack(&pwd->c, __prci_readl(pd, pwd->cfg0_offs));
156}
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172static void __prci_wrpll_write_cfg0(struct __prci_data *pd,
173 struct __prci_wrpll_data *pwd,
174 struct wrpll_cfg *c)
175{
176 __prci_writel(__prci_wrpll_pack(c), pwd->cfg0_offs, pd);
177
178 memcpy(&pwd->c, c, sizeof(*c));
179}
180
181
182
183
184
185
186
187
188static void __prci_wrpll_write_cfg1(struct __prci_data *pd,
189 struct __prci_wrpll_data *pwd,
190 u32 enable)
191{
192 __prci_writel(enable, pwd->cfg1_offs, pd);
193}
194
195unsigned long sifive_prci_wrpll_recalc_rate(struct __prci_clock *pc,
196 unsigned long parent_rate)
197{
198 struct __prci_wrpll_data *pwd = pc->pwd;
199
200 return wrpll_calc_output_rate(&pwd->c, parent_rate);
201}
202
203unsigned long sifive_prci_wrpll_round_rate(struct __prci_clock *pc,
204 unsigned long rate,
205 unsigned long *parent_rate)
206{
207 struct __prci_wrpll_data *pwd = pc->pwd;
208 struct wrpll_cfg c;
209
210 memcpy(&c, &pwd->c, sizeof(c));
211
212 wrpll_configure_for_rate(&c, rate, *parent_rate);
213
214 return wrpll_calc_output_rate(&c, *parent_rate);
215}
216
217int sifive_prci_wrpll_set_rate(struct __prci_clock *pc,
218 unsigned long rate,
219 unsigned long parent_rate)
220{
221 struct __prci_wrpll_data *pwd = pc->pwd;
222 struct __prci_data *pd = pc->pd;
223 int r;
224
225 r = wrpll_configure_for_rate(&pwd->c, rate, parent_rate);
226 if (r)
227 return r;
228
229 if (pwd->enable_bypass)
230 pwd->enable_bypass(pd);
231
232 __prci_wrpll_write_cfg0(pd, pwd, &pwd->c);
233
234 udelay(wrpll_calc_max_lock_us(&pwd->c));
235
236 return 0;
237}
238
239int sifive_prci_clock_enable(struct __prci_clock *pc, bool enable)
240{
241 struct __prci_wrpll_data *pwd = pc->pwd;
242 struct __prci_data *pd = pc->pd;
243
244 if (enable) {
245 __prci_wrpll_write_cfg1(pd, pwd, PRCI_COREPLLCFG1_CKE_MASK);
246
247 if (pwd->disable_bypass)
248 pwd->disable_bypass(pd);
249
250 if (pwd->release_reset)
251 pwd->release_reset(pd);
252 } else {
253 u32 r;
254
255 if (pwd->enable_bypass)
256 pwd->enable_bypass(pd);
257
258 r = __prci_readl(pd, pwd->cfg1_offs);
259 r &= ~PRCI_COREPLLCFG1_CKE_MASK;
260
261 __prci_wrpll_write_cfg1(pd, pwd, r);
262 }
263
264 return 0;
265}
266
267
268
269unsigned long sifive_prci_tlclksel_recalc_rate(struct __prci_clock *pc,
270 unsigned long parent_rate)
271{
272 struct __prci_data *pd = pc->pd;
273 u32 v;
274 u8 div;
275
276 v = __prci_readl(pd, PRCI_CLKMUXSTATUSREG_OFFSET);
277 v &= PRCI_CLKMUXSTATUSREG_TLCLKSEL_STATUS_MASK;
278 div = v ? 1 : 2;
279
280 return div_u64(parent_rate, div);
281}
282
283
284
285unsigned long sifive_prci_hfpclkplldiv_recalc_rate(struct __prci_clock *pc,
286 unsigned long parent_rate)
287{
288 struct __prci_data *pd = pc->pd;
289 u32 div = __prci_readl(pd, PRCI_HFPCLKPLLDIV_OFFSET);
290
291 return div_u64(parent_rate, div + 2);
292}
293
294
295
296
297
298
299
300
301
302
303
304
305void sifive_prci_coreclksel_use_final_corepll(struct __prci_data *pd)
306{
307 u32 r;
308
309 r = __prci_readl(pd, PRCI_CORECLKSEL_OFFSET);
310 r &= ~PRCI_CORECLKSEL_CORECLKSEL_MASK;
311 __prci_writel(r, PRCI_CORECLKSEL_OFFSET, pd);
312
313 r = __prci_readl(pd, PRCI_CORECLKSEL_OFFSET);
314}
315
316
317
318
319
320
321
322
323
324
325
326void sifive_prci_corepllsel_use_dvfscorepll(struct __prci_data *pd)
327{
328 u32 r;
329
330 r = __prci_readl(pd, PRCI_COREPLLSEL_OFFSET);
331 r |= PRCI_COREPLLSEL_COREPLLSEL_MASK;
332 __prci_writel(r, PRCI_COREPLLSEL_OFFSET, pd);
333
334 r = __prci_readl(pd, PRCI_COREPLLSEL_OFFSET);
335}
336
337
338
339
340
341
342
343
344
345
346
347void sifive_prci_corepllsel_use_corepll(struct __prci_data *pd)
348{
349 u32 r;
350
351 r = __prci_readl(pd, PRCI_COREPLLSEL_OFFSET);
352 r &= ~PRCI_COREPLLSEL_COREPLLSEL_MASK;
353 __prci_writel(r, PRCI_COREPLLSEL_OFFSET, pd);
354
355 r = __prci_readl(pd, PRCI_COREPLLSEL_OFFSET);
356}
357
358
359
360
361
362
363
364
365
366
367
368void sifive_prci_hfpclkpllsel_use_hfclk(struct __prci_data *pd)
369{
370 u32 r;
371
372 r = __prci_readl(pd, PRCI_HFPCLKPLLSEL_OFFSET);
373 r |= PRCI_HFPCLKPLLSEL_HFPCLKPLLSEL_MASK;
374 __prci_writel(r, PRCI_HFPCLKPLLSEL_OFFSET, pd);
375
376 r = __prci_readl(pd, PRCI_HFPCLKPLLSEL_OFFSET);
377}
378
379
380
381
382
383
384
385
386
387
388
389void sifive_prci_hfpclkpllsel_use_hfpclkpll(struct __prci_data *pd)
390{
391 u32 r;
392
393 r = __prci_readl(pd, PRCI_HFPCLKPLLSEL_OFFSET);
394 r &= ~PRCI_HFPCLKPLLSEL_HFPCLKPLLSEL_MASK;
395 __prci_writel(r, PRCI_HFPCLKPLLSEL_OFFSET, pd);
396
397 r = __prci_readl(pd, PRCI_HFPCLKPLLSEL_OFFSET);
398}
399
400static int __prci_consumer_reset(const char *rst_name, bool trigger)
401{
402 struct udevice *dev;
403 struct reset_ctl rst_sig;
404 int ret;
405
406 ret = uclass_get_device_by_driver(UCLASS_RESET,
407 DM_DRIVER_GET(sifive_reset),
408 &dev);
409 if (ret) {
410 dev_err(dev, "Reset driver not found: %d\n", ret);
411 return ret;
412 }
413
414 ret = reset_get_by_name(dev, rst_name, &rst_sig);
415 if (ret) {
416 dev_err(dev, "failed to get %s reset\n", rst_name);
417 return ret;
418 }
419
420 if (reset_valid(&rst_sig)) {
421 if (trigger)
422 ret = reset_deassert(&rst_sig);
423 else
424 ret = reset_assert(&rst_sig);
425 if (ret) {
426 dev_err(dev, "failed to trigger reset id = %ld\n",
427 rst_sig.id);
428 return ret;
429 }
430 }
431
432 return ret;
433}
434
435
436
437
438
439
440void sifive_prci_ddr_release_reset(struct __prci_data *pd)
441{
442
443 __prci_consumer_reset("ddr_ctrl", true);
444
445
446 asm volatile ("fence");
447
448
449 __prci_consumer_reset("ddr_axi", true);
450
451
452 __prci_consumer_reset("ddr_ahb", true);
453
454
455 __prci_consumer_reset("ddr_phy", true);
456
457
458 asm volatile ("fence");
459
460
461
462
463
464 for (int i = 0; i < 256; i++)
465 asm volatile ("nop");
466}
467
468
469
470
471
472
473void sifive_prci_ethernet_release_reset(struct __prci_data *pd)
474{
475
476 __prci_consumer_reset("gemgxl_reset", true);
477
478
479 __prci_writel(PRCI_PROCMONCFG_CORE_CLOCK_MASK, PRCI_PROCMONCFG_OFFSET,
480 pd);
481
482
483 __prci_consumer_reset("cltx_reset", true);
484}
485
486
487
488
489
490
491void sifive_prci_cltx_release_reset(struct __prci_data *pd)
492{
493
494 __prci_consumer_reset("cltx_reset", true);
495}
496
497
498
499
500
501
502
503
504
505
506
507
508void sifive_prci_coreclksel_use_hfclk(struct __prci_data *pd)
509{
510 u32 r;
511
512 r = __prci_readl(pd, PRCI_CORECLKSEL_OFFSET);
513 r |= PRCI_CORECLKSEL_CORECLKSEL_MASK;
514 __prci_writel(r, PRCI_CORECLKSEL_OFFSET, pd);
515
516 r = __prci_readl(pd, PRCI_CORECLKSEL_OFFSET);
517}
518
519
520
521
522
523
524
525
526
527
528void sifive_prci_coreclksel_use_corepll(struct __prci_data *pd)
529{
530 u32 r;
531
532 r = __prci_readl(pd, PRCI_CORECLKSEL_OFFSET);
533 r &= ~PRCI_CORECLKSEL_CORECLKSEL_MASK;
534 __prci_writel(r, PRCI_CORECLKSEL_OFFSET, pd);
535
536 r = __prci_readl(pd, PRCI_CORECLKSEL_OFFSET);
537}
538
539static ulong sifive_prci_parent_rate(struct __prci_clock *pc, struct prci_clk_desc *data)
540{
541 ulong parent_rate;
542 ulong i;
543 struct __prci_clock *p;
544
545 if (strcmp(pc->parent_name, "corepll") == 0 ||
546 strcmp(pc->parent_name, "hfpclkpll") == 0) {
547 for (i = 0; i < data->num_clks; i++) {
548 if (strcmp(pc->parent_name, data->clks[i].name) == 0)
549 break;
550 }
551
552 if (i >= data->num_clks)
553 return -ENXIO;
554
555 p = &data->clks[i];
556 if (!p->pd || !p->ops->recalc_rate)
557 return -ENXIO;
558
559 return p->ops->recalc_rate(p, sifive_prci_parent_rate(p, data));
560 }
561
562 if (strcmp(pc->parent_name, "rtcclk") == 0)
563 parent_rate = clk_get_rate(&pc->pd->parent_rtcclk);
564 else
565 parent_rate = clk_get_rate(&pc->pd->parent_hfclk);
566
567 return parent_rate;
568}
569
570static ulong sifive_prci_get_rate(struct clk *clk)
571{
572 struct __prci_clock *pc;
573 struct prci_clk_desc *data =
574 (struct prci_clk_desc *)dev_get_driver_data(clk->dev);
575
576 if (data->num_clks <= clk->id)
577 return -ENXIO;
578
579 pc = &data->clks[clk->id];
580 if (!pc->pd || !pc->ops->recalc_rate)
581 return -ENXIO;
582
583 return pc->ops->recalc_rate(pc, sifive_prci_parent_rate(pc, data));
584}
585
586static ulong sifive_prci_set_rate(struct clk *clk, ulong rate)
587{
588 int err;
589 struct __prci_clock *pc;
590 struct prci_clk_desc *data =
591 (struct prci_clk_desc *)dev_get_driver_data(clk->dev);
592
593 if (data->num_clks <= clk->id)
594 return -ENXIO;
595
596 pc = &data->clks[clk->id];
597 if (!pc->pd || !pc->ops->set_rate)
598 return -ENXIO;
599
600 err = pc->ops->set_rate(pc, rate, sifive_prci_parent_rate(pc, data));
601 if (err)
602 return err;
603
604 return rate;
605}
606
607static int sifive_prci_enable(struct clk *clk)
608{
609 struct __prci_clock *pc;
610 int ret = 0;
611 struct prci_clk_desc *data =
612 (struct prci_clk_desc *)dev_get_driver_data(clk->dev);
613
614 if (data->num_clks <= clk->id)
615 return -ENXIO;
616
617 pc = &data->clks[clk->id];
618 if (!pc->pd)
619 return -ENXIO;
620
621 if (pc->ops->enable_clk)
622 ret = pc->ops->enable_clk(pc, 1);
623
624 return ret;
625}
626
627static int sifive_prci_disable(struct clk *clk)
628{
629 struct __prci_clock *pc;
630 int ret = 0;
631 struct prci_clk_desc *data =
632 (struct prci_clk_desc *)dev_get_driver_data(clk->dev);
633
634 if (data->num_clks <= clk->id)
635 return -ENXIO;
636
637 pc = &data->clks[clk->id];
638 if (!pc->pd)
639 return -ENXIO;
640
641 if (pc->ops->enable_clk)
642 ret = pc->ops->enable_clk(pc, 0);
643
644 return ret;
645}
646
647static int sifive_prci_probe(struct udevice *dev)
648{
649 int i, err;
650 struct __prci_clock *pc;
651 struct __prci_data *pd = dev_get_priv(dev);
652
653 struct prci_clk_desc *data =
654 (struct prci_clk_desc *)dev_get_driver_data(dev);
655
656 pd->va = dev_read_addr_ptr(dev);
657 if (!pd->va)
658 return -EINVAL;
659
660 err = clk_get_by_index(dev, 0, &pd->parent_hfclk);
661 if (err)
662 return err;
663
664 err = clk_get_by_index(dev, 1, &pd->parent_rtcclk);
665 if (err)
666 return err;
667
668 for (i = 0; i < data->num_clks; ++i) {
669 pc = &data->clks[i];
670 pc->pd = pd;
671 if (pc->pwd)
672 __prci_wrpll_read_cfg0(pd, pc->pwd);
673 }
674
675 if (IS_ENABLED(CONFIG_SPL_BUILD)) {
676 if (device_is_compatible(dev, "sifive,fu740-c000-prci")) {
677 u32 prci_pll_reg;
678 unsigned long parent_rate;
679
680 prci_pll_reg = readl(pd->va + PRCI_PRCIPLL_OFFSET);
681
682 if (prci_pll_reg & PRCI_PRCIPLL_HFPCLKPLL) {
683
684
685
686
687
688 pc = &data->clks[FU740_PRCI_CLK_HFPCLKPLL];
689 parent_rate = sifive_prci_parent_rate(pc, data);
690 sifive_prci_wrpll_set_rate(pc, 260000000,
691 parent_rate);
692 pc->ops->enable_clk(pc, 1);
693 } else if (prci_pll_reg & PRCI_PRCIPLL_CLTXPLL) {
694
695 pc = &data->clks[FU740_PRCI_CLK_CLTXPLL];
696 parent_rate = sifive_prci_parent_rate(pc, data);
697 sifive_prci_wrpll_set_rate(pc, 260000000,
698 parent_rate);
699 pc->ops->enable_clk(pc, 1);
700 }
701 }
702 }
703
704 return 0;
705}
706
707static struct clk_ops sifive_prci_ops = {
708 .set_rate = sifive_prci_set_rate,
709 .get_rate = sifive_prci_get_rate,
710 .enable = sifive_prci_enable,
711 .disable = sifive_prci_disable,
712};
713
714static int sifive_clk_bind(struct udevice *dev)
715{
716 return sifive_reset_bind(dev, PRCI_DEVICERESETCNT);
717}
718
719static const struct udevice_id sifive_prci_ids[] = {
720 { .compatible = "sifive,fu540-c000-prci", .data = (ulong)&prci_clk_fu540 },
721 { .compatible = "sifive,fu740-c000-prci", .data = (ulong)&prci_clk_fu740 },
722 { }
723};
724
725U_BOOT_DRIVER(sifive_prci) = {
726 .name = "sifive-prci",
727 .id = UCLASS_CLK,
728 .of_match = sifive_prci_ids,
729 .probe = sifive_prci_probe,
730 .ops = &sifive_prci_ops,
731 .priv_auto = sizeof(struct __prci_data),
732 .bind = sifive_clk_bind,
733};
734