blob: ea96d739c53d46494b42545caf2b08c5ba6a204b [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Masahiro Yamada682e09f2016-09-17 03:33:11 +09002/*
3 * Copyright (C) 2016 Socionext Inc.
4 * Author: Masahiro Yamada <yamada.masahiro@socionext.com>
Masahiro Yamada682e09f2016-09-17 03:33:11 +09005 */
6
Masahiro Yamada7f8e7532017-12-06 14:16:34 +09007#include <linux/bitfield.h>
Masahiro Yamada682e09f2016-09-17 03:33:11 +09008#include <linux/bitops.h>
Masahiro Yamadad9a70362017-01-21 18:05:25 +09009#include <linux/delay.h>
Masahiro Yamadaf2ce50b2017-12-06 14:16:33 +090010#include <linux/kernel.h>
Masahiro Yamadad9a70362017-01-21 18:05:25 +090011#include <linux/errno.h>
Masahiro Yamada682e09f2016-09-17 03:33:11 +090012#include <linux/io.h>
13#include <linux/sizes.h>
14
Masahiro Yamada739ba412019-07-10 20:07:41 +090015#include "../sc64-regs.h"
Masahiro Yamada682e09f2016-09-17 03:33:11 +090016#include "pll.h"
17
18/* PLL type: SSC */
19#define SC_PLLCTRL_SSC_DK_MASK GENMASK(14, 0)
20#define SC_PLLCTRL_SSC_EN BIT(31)
21#define SC_PLLCTRL2_NRSTDS BIT(28)
22#define SC_PLLCTRL2_SSC_JK_MASK GENMASK(26, 0)
Masahiro Yamadabc647952017-02-21 23:00:35 +090023#define SC_PLLCTRL3_REGI_MASK GENMASK(19, 16)
Masahiro Yamada682e09f2016-09-17 03:33:11 +090024
25/* PLL type: VPLL27 */
26#define SC_VPLL27CTRL_WP BIT(0)
27#define SC_VPLL27CTRL3_K_LD BIT(28)
28
29/* PLL type: DSPLL */
30#define SC_DSPLLCTRL2_K_LD BIT(28)
31
32int uniphier_ld20_sscpll_init(unsigned long reg_base, unsigned int freq,
33 unsigned int ssc_rate, unsigned int divn)
34{
Masahiro Yamada739ba412019-07-10 20:07:41 +090035 void __iomem *base = sc_base + reg_base;
Masahiro Yamada682e09f2016-09-17 03:33:11 +090036 u32 tmp;
37
Masahiro Yamada682e09f2016-09-17 03:33:11 +090038 if (freq != UNIPHIER_PLL_FREQ_DEFAULT) {
39 tmp = readl(base); /* SSCPLLCTRL */
40 tmp &= ~SC_PLLCTRL_SSC_DK_MASK;
Masahiro Yamada7f8e7532017-12-06 14:16:34 +090041 tmp |= FIELD_PREP(SC_PLLCTRL_SSC_DK_MASK,
42 DIV_ROUND_CLOSEST(487UL * freq * ssc_rate,
43 divn * 512));
Masahiro Yamada682e09f2016-09-17 03:33:11 +090044 writel(tmp, base);
45
46 tmp = readl(base + 4);
47 tmp &= ~SC_PLLCTRL2_SSC_JK_MASK;
Masahiro Yamada7f8e7532017-12-06 14:16:34 +090048 tmp |= FIELD_PREP(SC_PLLCTRL2_SSC_JK_MASK,
49 DIV_ROUND_CLOSEST(21431887UL * freq,
50 divn * 512));
Dai Okamurac30c44e2017-12-06 14:16:32 +090051 writel(tmp, base + 4);
Masahiro Yamada682e09f2016-09-17 03:33:11 +090052
53 udelay(50);
54 }
55
56 tmp = readl(base + 4); /* SSCPLLCTRL2 */
57 tmp |= SC_PLLCTRL2_NRSTDS;
58 writel(tmp, base + 4);
59
Masahiro Yamada682e09f2016-09-17 03:33:11 +090060 return 0;
61}
62
63int uniphier_ld20_sscpll_ssc_en(unsigned long reg_base)
64{
Masahiro Yamada739ba412019-07-10 20:07:41 +090065 void __iomem *base = sc_base + reg_base;
Masahiro Yamada682e09f2016-09-17 03:33:11 +090066 u32 tmp;
67
Masahiro Yamada682e09f2016-09-17 03:33:11 +090068 tmp = readl(base); /* SSCPLLCTRL */
69 tmp |= SC_PLLCTRL_SSC_EN;
70 writel(tmp, base);
71
Masahiro Yamada682e09f2016-09-17 03:33:11 +090072 return 0;
73}
74
Masahiro Yamadabc647952017-02-21 23:00:35 +090075int uniphier_ld20_sscpll_set_regi(unsigned long reg_base, unsigned regi)
76{
Masahiro Yamada739ba412019-07-10 20:07:41 +090077 void __iomem *base = sc_base + reg_base;
Masahiro Yamadabc647952017-02-21 23:00:35 +090078 u32 tmp;
79
Dai Okamura4013bbb2017-08-28 21:57:15 +090080 tmp = readl(base + 8); /* SSCPLLCTRL3 */
Masahiro Yamadabc647952017-02-21 23:00:35 +090081 tmp &= ~SC_PLLCTRL3_REGI_MASK;
Masahiro Yamada7f8e7532017-12-06 14:16:34 +090082 tmp |= FIELD_PREP(SC_PLLCTRL3_REGI_MASK, regi);
Masahiro Yamadabc647952017-02-21 23:00:35 +090083 writel(tmp, base + 8);
84
Masahiro Yamadabc647952017-02-21 23:00:35 +090085 return 0;
86}
87
Masahiro Yamada682e09f2016-09-17 03:33:11 +090088int uniphier_ld20_vpll27_init(unsigned long reg_base)
89{
Masahiro Yamada739ba412019-07-10 20:07:41 +090090 void __iomem *base = sc_base + reg_base;
Masahiro Yamada682e09f2016-09-17 03:33:11 +090091 u32 tmp;
92
Masahiro Yamada682e09f2016-09-17 03:33:11 +090093 tmp = readl(base); /* VPLL27CTRL */
94 tmp |= SC_VPLL27CTRL_WP; /* write protect off */
95 writel(tmp, base);
96
97 tmp = readl(base + 8); /* VPLL27CTRL3 */
98 tmp |= SC_VPLL27CTRL3_K_LD;
99 writel(tmp, base + 8);
100
101 tmp = readl(base); /* VPLL27CTRL */
102 tmp &= ~SC_VPLL27CTRL_WP; /* write protect on */
103 writel(tmp, base);
104
Masahiro Yamada682e09f2016-09-17 03:33:11 +0900105 return 0;
106}
107
108int uniphier_ld20_dspll_init(unsigned long reg_base)
109{
Masahiro Yamada739ba412019-07-10 20:07:41 +0900110 void __iomem *base = sc_base + reg_base;
Masahiro Yamada682e09f2016-09-17 03:33:11 +0900111 u32 tmp;
112
Dai Okamura4013bbb2017-08-28 21:57:15 +0900113 tmp = readl(base + 4); /* DSPLLCTRL2 */
Masahiro Yamada682e09f2016-09-17 03:33:11 +0900114 tmp |= SC_DSPLLCTRL2_K_LD;
Dai Okamura4013bbb2017-08-28 21:57:15 +0900115 writel(tmp, base + 4);
Masahiro Yamada682e09f2016-09-17 03:33:11 +0900116
Masahiro Yamada682e09f2016-09-17 03:33:11 +0900117 return 0;
118}