blob: 03363b70d74e3b611a3b35353ea335a6794699aa [file] [log] [blame]
Weijie Gaoe75cc002022-05-20 11:22:36 +08001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (C) 2022 MediaTek Inc. All rights reserved.
4 *
5 * Author: Weijie Gao <weijie.gao@mediatek.com>
6 */
7
8#include <clk-uclass.h>
9#include <dm.h>
10#include <dm/device_compat.h>
11#include <regmap.h>
12#include <syscon.h>
13#include <dt-bindings/clock/mt7621-clk.h>
14#include <linux/io.h>
15#include <linux/bitops.h>
16#include <linux/bitfield.h>
17
18#define SYSC_MAP_SIZE 0x100
19#define MEMC_MAP_SIZE 0x1000
20
21/* SYSC */
22#define SYSCFG0_REG 0x10
23#define XTAL_MODE_SEL GENMASK(8, 6)
24
25#define CLKCFG0_REG 0x2c
26#define CPU_CLK_SEL GENMASK(31, 30)
27#define PERI_CLK_SEL BIT(4)
28
29#define CLKCFG1_REG 0x30
30
31#define CUR_CLK_STS_REG 0x44
32#define CUR_CPU_FDIV GENMASK(12, 8)
33#define CUR_CPU_FFRAC GENMASK(4, 0)
34
35/* MEMC */
36#define MEMPLL1_REG 0x0604
37#define RG_MEPL_DIV2_SEL GENMASK(2, 1)
38
39#define MEMPLL6_REG 0x0618
40#define MEMPLL18_REG 0x0648
41#define RG_MEPL_PREDIV GENMASK(13, 12)
42#define RG_MEPL_FBDIV GENMASK(10, 4)
43
44/* Fixed 500M clock */
45#define GMPLL_CLK 500000000
46
47struct mt7621_clk_priv {
48 void __iomem *sysc_base;
49 int cpu_clk;
50 int ddr_clk;
51 int sys_clk;
52 int xtal_clk;
53};
54
55enum mt7621_clk_src {
56 CLK_SRC_CPU,
57 CLK_SRC_DDR,
58 CLK_SRC_SYS,
59 CLK_SRC_XTAL,
60 CLK_SRC_PERI,
61 CLK_SRC_125M,
62 CLK_SRC_150M,
63 CLK_SRC_250M,
64 CLK_SRC_270M,
65
66 __CLK_SRC_MAX
67};
68
69struct mt7621_clk_map {
70 u32 cgbit;
71 enum mt7621_clk_src clksrc;
72};
73
74#define CLK_MAP(_id, _cg, _src) \
75 [_id] = { .cgbit = (_cg), .clksrc = (_src) }
76
77#define CLK_MAP_SRC(_id, _src) \
78 [_id] = { .cgbit = UINT32_MAX, .clksrc = (_src) }
79
80static const struct mt7621_clk_map mt7621_clk_mappings[] = {
81 CLK_MAP_SRC(MT7621_CLK_XTAL, CLK_SRC_XTAL),
82 CLK_MAP_SRC(MT7621_CLK_CPU, CLK_SRC_CPU),
83 CLK_MAP_SRC(MT7621_CLK_BUS, CLK_SRC_SYS),
84 CLK_MAP_SRC(MT7621_CLK_50M, CLK_SRC_PERI),
85 CLK_MAP_SRC(MT7621_CLK_125M, CLK_SRC_125M),
86 CLK_MAP_SRC(MT7621_CLK_150M, CLK_SRC_150M),
87 CLK_MAP_SRC(MT7621_CLK_250M, CLK_SRC_250M),
88 CLK_MAP_SRC(MT7621_CLK_270M, CLK_SRC_270M),
89
90 CLK_MAP(MT7621_CLK_HSDMA, 5, CLK_SRC_150M),
91 CLK_MAP(MT7621_CLK_FE, 6, CLK_SRC_250M),
92 CLK_MAP(MT7621_CLK_SP_DIVTX, 7, CLK_SRC_270M),
93 CLK_MAP(MT7621_CLK_TIMER, 8, CLK_SRC_PERI),
94 CLK_MAP(MT7621_CLK_PCM, 11, CLK_SRC_270M),
95 CLK_MAP(MT7621_CLK_PIO, 13, CLK_SRC_PERI),
96 CLK_MAP(MT7621_CLK_GDMA, 14, CLK_SRC_SYS),
97 CLK_MAP(MT7621_CLK_NAND, 15, CLK_SRC_125M),
98 CLK_MAP(MT7621_CLK_I2C, 16, CLK_SRC_PERI),
99 CLK_MAP(MT7621_CLK_I2S, 17, CLK_SRC_270M),
100 CLK_MAP(MT7621_CLK_SPI, 18, CLK_SRC_SYS),
101 CLK_MAP(MT7621_CLK_UART1, 19, CLK_SRC_PERI),
102 CLK_MAP(MT7621_CLK_UART2, 20, CLK_SRC_PERI),
103 CLK_MAP(MT7621_CLK_UART3, 21, CLK_SRC_PERI),
104 CLK_MAP(MT7621_CLK_ETH, 23, CLK_SRC_PERI),
105 CLK_MAP(MT7621_CLK_PCIE0, 24, CLK_SRC_125M),
106 CLK_MAP(MT7621_CLK_PCIE1, 25, CLK_SRC_125M),
107 CLK_MAP(MT7621_CLK_PCIE2, 26, CLK_SRC_125M),
108 CLK_MAP(MT7621_CLK_CRYPTO, 29, CLK_SRC_250M),
109 CLK_MAP(MT7621_CLK_SHXC, 30, CLK_SRC_PERI),
110
111 CLK_MAP_SRC(MT7621_CLK_MAX, __CLK_SRC_MAX),
112
113 CLK_MAP_SRC(MT7621_CLK_DDR, CLK_SRC_DDR),
114};
115
116static ulong mt7621_clk_get_rate(struct clk *clk)
117{
118 struct mt7621_clk_priv *priv = dev_get_priv(clk->dev);
119 u32 val;
120
121 switch (mt7621_clk_mappings[clk->id].clksrc) {
122 case CLK_SRC_CPU:
123 return priv->cpu_clk;
124 case CLK_SRC_DDR:
125 return priv->ddr_clk;
126 case CLK_SRC_SYS:
127 return priv->sys_clk;
128 case CLK_SRC_XTAL:
129 return priv->xtal_clk;
130 case CLK_SRC_PERI:
131 val = readl(priv->sysc_base + CLKCFG0_REG);
132 if (val & PERI_CLK_SEL)
133 return priv->xtal_clk;
134 else
135 return GMPLL_CLK / 10;
136 case CLK_SRC_125M:
137 return 125000000;
138 case CLK_SRC_150M:
139 return 150000000;
140 case CLK_SRC_250M:
141 return 250000000;
142 case CLK_SRC_270M:
143 return 270000000;
144 default:
145 return 0;
146 }
147}
148
149static int mt7621_clk_enable(struct clk *clk)
150{
151 struct mt7621_clk_priv *priv = dev_get_priv(clk->dev);
152 u32 cgbit;
153
154 cgbit = mt7621_clk_mappings[clk->id].cgbit;
155 if (cgbit == UINT32_MAX)
156 return -ENOSYS;
157
158 setbits_32(priv->sysc_base + CLKCFG1_REG, BIT(cgbit));
159
160 return 0;
161}
162
163static int mt7621_clk_disable(struct clk *clk)
164{
165 struct mt7621_clk_priv *priv = dev_get_priv(clk->dev);
166 u32 cgbit;
167
168 cgbit = mt7621_clk_mappings[clk->id].cgbit;
169 if (cgbit == UINT32_MAX)
170 return -ENOSYS;
171
172 clrbits_32(priv->sysc_base + CLKCFG1_REG, BIT(cgbit));
173
174 return 0;
175}
176
177static int mt7621_clk_request(struct clk *clk)
178{
179 if (clk->id >= ARRAY_SIZE(mt7621_clk_mappings))
180 return -EINVAL;
181 return 0;
182}
183
184const struct clk_ops mt7621_clk_ops = {
185 .request = mt7621_clk_request,
186 .enable = mt7621_clk_enable,
187 .disable = mt7621_clk_disable,
188 .get_rate = mt7621_clk_get_rate,
189};
190
191static void mt7621_get_clocks(struct mt7621_clk_priv *priv, struct regmap *memc)
192{
193 u32 bs, xtal_sel, clkcfg0, cur_clk, mempll, dividx, fb;
194 u32 xtal_clk, xtal_div, ffiv, ffrac, cpu_clk, ddr_clk;
195 static const u32 xtal_div_tbl[] = {0, 1, 2, 2};
196
197 bs = readl(priv->sysc_base + SYSCFG0_REG);
198 clkcfg0 = readl(priv->sysc_base + CLKCFG0_REG);
199 cur_clk = readl(priv->sysc_base + CUR_CLK_STS_REG);
200
201 xtal_sel = FIELD_GET(XTAL_MODE_SEL, bs);
202
203 if (xtal_sel <= 2)
204 xtal_clk = 20 * 1000 * 1000;
205 else if (xtal_sel <= 5)
206 xtal_clk = 40 * 1000 * 1000;
207 else
208 xtal_clk = 25 * 1000 * 1000;
209
210 switch (FIELD_GET(CPU_CLK_SEL, clkcfg0)) {
211 case 0:
212 cpu_clk = GMPLL_CLK;
213 break;
214 case 1:
215 regmap_read(memc, MEMPLL18_REG, &mempll);
216 dividx = FIELD_GET(RG_MEPL_PREDIV, mempll);
217 fb = FIELD_GET(RG_MEPL_FBDIV, mempll);
218 xtal_div = 1 << xtal_div_tbl[dividx];
219 cpu_clk = (fb + 1) * xtal_clk / xtal_div;
220 break;
221 default:
222 cpu_clk = xtal_clk;
223 }
224
225 ffiv = FIELD_GET(CUR_CPU_FDIV, cur_clk);
226 ffrac = FIELD_GET(CUR_CPU_FFRAC, cur_clk);
227 cpu_clk = cpu_clk / ffiv * ffrac;
228
229 regmap_read(memc, MEMPLL6_REG, &mempll);
230 dividx = FIELD_GET(RG_MEPL_PREDIV, mempll);
231 fb = FIELD_GET(RG_MEPL_FBDIV, mempll);
232 xtal_div = 1 << xtal_div_tbl[dividx];
233 ddr_clk = fb * xtal_clk / xtal_div;
234
235 regmap_read(memc, MEMPLL1_REG, &bs);
236 if (!FIELD_GET(RG_MEPL_DIV2_SEL, bs))
237 ddr_clk *= 2;
238
239 priv->cpu_clk = cpu_clk;
240 priv->sys_clk = cpu_clk / 4;
241 priv->ddr_clk = ddr_clk;
242 priv->xtal_clk = xtal_clk;
243}
244
245static int mt7621_clk_probe(struct udevice *dev)
246{
247 struct mt7621_clk_priv *priv = dev_get_priv(dev);
248 struct ofnode_phandle_args args;
249 struct udevice *pdev;
250 struct regmap *memc;
251 int ret;
252
253 pdev = dev_get_parent(dev);
254 if (!pdev)
255 return -ENODEV;
256
257 priv->sysc_base = dev_remap_addr(pdev);
258 if (!priv->sysc_base)
259 return -EINVAL;
260
261 /* get corresponding memc phandle */
262 ret = dev_read_phandle_with_args(dev, "mediatek,memc", NULL, 0, 0,
263 &args);
264 if (ret)
265 return ret;
266
267 memc = syscon_node_to_regmap(args.node);
268 if (IS_ERR(memc))
269 return PTR_ERR(memc);
270
271 mt7621_get_clocks(priv, memc);
272
273 return 0;
274}
275
276static const struct udevice_id mt7621_clk_ids[] = {
277 { .compatible = "mediatek,mt7621-clk" },
278 { }
279};
280
281U_BOOT_DRIVER(mt7621_clk) = {
282 .name = "mt7621-clk",
283 .id = UCLASS_CLK,
284 .of_match = mt7621_clk_ids,
285 .probe = mt7621_clk_probe,
286 .priv_auto = sizeof(struct mt7621_clk_priv),
287 .ops = &mt7621_clk_ops,
288};