1 From c2e579662748cb5d3bf3e31f58d99c4db4d102c1 Mon Sep 17 00:00:00 2001
2 From: Weijie Gao <weijie.gao@mediatek.com>
3 Date: Fri, 20 May 2022 11:22:36 +0800
4 Subject: [PATCH 08/25] clk: mtmips: add clock driver for MediaTek MT7621 SoC
6 This patch adds a clock driver for MediaTek MT7621 SoC.
7 This driver provides clock gate control as well as getting clock frequency
8 for CPU/SYS/XTAL and some peripherals.
10 Reviewed-by: Sean Anderson <seanga2@gmail.com>
11 Signed-off-by: Weijie Gao <weijie.gao@mediatek.com>
13 drivers/clk/mtmips/Makefile | 1 +
14 drivers/clk/mtmips/clk-mt7621.c | 288 +++++++++++++++++++++++++
15 include/dt-bindings/clock/mt7621-clk.h | 46 ++++
16 3 files changed, 335 insertions(+)
17 create mode 100644 drivers/clk/mtmips/clk-mt7621.c
18 create mode 100644 include/dt-bindings/clock/mt7621-clk.h
20 --- a/drivers/clk/mtmips/Makefile
21 +++ b/drivers/clk/mtmips/Makefile
23 # SPDX-License-Identifier: GPL-2.0
25 obj-$(CONFIG_SOC_MT7620) += clk-mt7620.o
26 +obj-$(CONFIG_SOC_MT7621) += clk-mt7621.o
27 obj-$(CONFIG_SOC_MT7628) += clk-mt7628.o
29 +++ b/drivers/clk/mtmips/clk-mt7621.c
31 +// SPDX-License-Identifier: GPL-2.0
33 + * Copyright (C) 2022 MediaTek Inc. All rights reserved.
35 + * Author: Weijie Gao <weijie.gao@mediatek.com>
38 +#include <clk-uclass.h>
40 +#include <dm/device_compat.h>
43 +#include <dt-bindings/clock/mt7621-clk.h>
44 +#include <linux/io.h>
45 +#include <linux/bitops.h>
46 +#include <linux/bitfield.h>
48 +#define SYSC_MAP_SIZE 0x100
49 +#define MEMC_MAP_SIZE 0x1000
52 +#define SYSCFG0_REG 0x10
53 +#define XTAL_MODE_SEL GENMASK(8, 6)
55 +#define CLKCFG0_REG 0x2c
56 +#define CPU_CLK_SEL GENMASK(31, 30)
57 +#define PERI_CLK_SEL BIT(4)
59 +#define CLKCFG1_REG 0x30
61 +#define CUR_CLK_STS_REG 0x44
62 +#define CUR_CPU_FDIV GENMASK(12, 8)
63 +#define CUR_CPU_FFRAC GENMASK(4, 0)
66 +#define MEMPLL1_REG 0x0604
67 +#define RG_MEPL_DIV2_SEL GENMASK(2, 1)
69 +#define MEMPLL6_REG 0x0618
70 +#define MEMPLL18_REG 0x0648
71 +#define RG_MEPL_PREDIV GENMASK(13, 12)
72 +#define RG_MEPL_FBDIV GENMASK(10, 4)
74 +/* Fixed 500M clock */
75 +#define GMPLL_CLK 500000000
77 +struct mt7621_clk_priv {
78 + void __iomem *sysc_base;
85 +enum mt7621_clk_src {
99 +struct mt7621_clk_map {
101 + enum mt7621_clk_src clksrc;
104 +#define CLK_MAP(_id, _cg, _src) \
105 + [_id] = { .cgbit = (_cg), .clksrc = (_src) }
107 +#define CLK_MAP_SRC(_id, _src) \
108 + [_id] = { .cgbit = UINT32_MAX, .clksrc = (_src) }
110 +static const struct mt7621_clk_map mt7621_clk_mappings[] = {
111 + CLK_MAP_SRC(MT7621_CLK_XTAL, CLK_SRC_XTAL),
112 + CLK_MAP_SRC(MT7621_CLK_CPU, CLK_SRC_CPU),
113 + CLK_MAP_SRC(MT7621_CLK_BUS, CLK_SRC_SYS),
114 + CLK_MAP_SRC(MT7621_CLK_50M, CLK_SRC_PERI),
115 + CLK_MAP_SRC(MT7621_CLK_125M, CLK_SRC_125M),
116 + CLK_MAP_SRC(MT7621_CLK_150M, CLK_SRC_150M),
117 + CLK_MAP_SRC(MT7621_CLK_250M, CLK_SRC_250M),
118 + CLK_MAP_SRC(MT7621_CLK_270M, CLK_SRC_270M),
120 + CLK_MAP(MT7621_CLK_HSDMA, 5, CLK_SRC_150M),
121 + CLK_MAP(MT7621_CLK_FE, 6, CLK_SRC_250M),
122 + CLK_MAP(MT7621_CLK_SP_DIVTX, 7, CLK_SRC_270M),
123 + CLK_MAP(MT7621_CLK_TIMER, 8, CLK_SRC_PERI),
124 + CLK_MAP(MT7621_CLK_PCM, 11, CLK_SRC_270M),
125 + CLK_MAP(MT7621_CLK_PIO, 13, CLK_SRC_PERI),
126 + CLK_MAP(MT7621_CLK_GDMA, 14, CLK_SRC_SYS),
127 + CLK_MAP(MT7621_CLK_NAND, 15, CLK_SRC_125M),
128 + CLK_MAP(MT7621_CLK_I2C, 16, CLK_SRC_PERI),
129 + CLK_MAP(MT7621_CLK_I2S, 17, CLK_SRC_270M),
130 + CLK_MAP(MT7621_CLK_SPI, 18, CLK_SRC_SYS),
131 + CLK_MAP(MT7621_CLK_UART1, 19, CLK_SRC_PERI),
132 + CLK_MAP(MT7621_CLK_UART2, 20, CLK_SRC_PERI),
133 + CLK_MAP(MT7621_CLK_UART3, 21, CLK_SRC_PERI),
134 + CLK_MAP(MT7621_CLK_ETH, 23, CLK_SRC_PERI),
135 + CLK_MAP(MT7621_CLK_PCIE0, 24, CLK_SRC_125M),
136 + CLK_MAP(MT7621_CLK_PCIE1, 25, CLK_SRC_125M),
137 + CLK_MAP(MT7621_CLK_PCIE2, 26, CLK_SRC_125M),
138 + CLK_MAP(MT7621_CLK_CRYPTO, 29, CLK_SRC_250M),
139 + CLK_MAP(MT7621_CLK_SHXC, 30, CLK_SRC_PERI),
141 + CLK_MAP_SRC(MT7621_CLK_MAX, __CLK_SRC_MAX),
143 + CLK_MAP_SRC(MT7621_CLK_DDR, CLK_SRC_DDR),
146 +static ulong mt7621_clk_get_rate(struct clk *clk)
148 + struct mt7621_clk_priv *priv = dev_get_priv(clk->dev);
151 + switch (mt7621_clk_mappings[clk->id].clksrc) {
153 + return priv->cpu_clk;
155 + return priv->ddr_clk;
157 + return priv->sys_clk;
159 + return priv->xtal_clk;
161 + val = readl(priv->sysc_base + CLKCFG0_REG);
162 + if (val & PERI_CLK_SEL)
163 + return priv->xtal_clk;
165 + return GMPLL_CLK / 10;
179 +static int mt7621_clk_enable(struct clk *clk)
181 + struct mt7621_clk_priv *priv = dev_get_priv(clk->dev);
184 + cgbit = mt7621_clk_mappings[clk->id].cgbit;
185 + if (cgbit == UINT32_MAX)
188 + setbits_32(priv->sysc_base + CLKCFG1_REG, BIT(cgbit));
193 +static int mt7621_clk_disable(struct clk *clk)
195 + struct mt7621_clk_priv *priv = dev_get_priv(clk->dev);
198 + cgbit = mt7621_clk_mappings[clk->id].cgbit;
199 + if (cgbit == UINT32_MAX)
202 + clrbits_32(priv->sysc_base + CLKCFG1_REG, BIT(cgbit));
207 +static int mt7621_clk_request(struct clk *clk)
209 + if (clk->id >= ARRAY_SIZE(mt7621_clk_mappings))
214 +const struct clk_ops mt7621_clk_ops = {
215 + .request = mt7621_clk_request,
216 + .enable = mt7621_clk_enable,
217 + .disable = mt7621_clk_disable,
218 + .get_rate = mt7621_clk_get_rate,
221 +static void mt7621_get_clocks(struct mt7621_clk_priv *priv, struct regmap *memc)
223 + u32 bs, xtal_sel, clkcfg0, cur_clk, mempll, dividx, fb;
224 + u32 xtal_clk, xtal_div, ffiv, ffrac, cpu_clk, ddr_clk;
225 + static const u32 xtal_div_tbl[] = {0, 1, 2, 2};
227 + bs = readl(priv->sysc_base + SYSCFG0_REG);
228 + clkcfg0 = readl(priv->sysc_base + CLKCFG0_REG);
229 + cur_clk = readl(priv->sysc_base + CUR_CLK_STS_REG);
231 + xtal_sel = FIELD_GET(XTAL_MODE_SEL, bs);
234 + xtal_clk = 20 * 1000 * 1000;
235 + else if (xtal_sel <= 5)
236 + xtal_clk = 40 * 1000 * 1000;
238 + xtal_clk = 25 * 1000 * 1000;
240 + switch (FIELD_GET(CPU_CLK_SEL, clkcfg0)) {
242 + cpu_clk = GMPLL_CLK;
245 + regmap_read(memc, MEMPLL18_REG, &mempll);
246 + dividx = FIELD_GET(RG_MEPL_PREDIV, mempll);
247 + fb = FIELD_GET(RG_MEPL_FBDIV, mempll);
248 + xtal_div = 1 << xtal_div_tbl[dividx];
249 + cpu_clk = (fb + 1) * xtal_clk / xtal_div;
252 + cpu_clk = xtal_clk;
255 + ffiv = FIELD_GET(CUR_CPU_FDIV, cur_clk);
256 + ffrac = FIELD_GET(CUR_CPU_FFRAC, cur_clk);
257 + cpu_clk = cpu_clk / ffiv * ffrac;
259 + regmap_read(memc, MEMPLL6_REG, &mempll);
260 + dividx = FIELD_GET(RG_MEPL_PREDIV, mempll);
261 + fb = FIELD_GET(RG_MEPL_FBDIV, mempll);
262 + xtal_div = 1 << xtal_div_tbl[dividx];
263 + ddr_clk = fb * xtal_clk / xtal_div;
265 + regmap_read(memc, MEMPLL1_REG, &bs);
266 + if (!FIELD_GET(RG_MEPL_DIV2_SEL, bs))
269 + priv->cpu_clk = cpu_clk;
270 + priv->sys_clk = cpu_clk / 4;
271 + priv->ddr_clk = ddr_clk;
272 + priv->xtal_clk = xtal_clk;
275 +static int mt7621_clk_probe(struct udevice *dev)
277 + struct mt7621_clk_priv *priv = dev_get_priv(dev);
278 + struct ofnode_phandle_args args;
279 + struct udevice *pdev;
280 + struct regmap *memc;
283 + pdev = dev_get_parent(dev);
287 + priv->sysc_base = dev_remap_addr(pdev);
288 + if (!priv->sysc_base)
291 + /* get corresponding memc phandle */
292 + ret = dev_read_phandle_with_args(dev, "mediatek,memc", NULL, 0, 0,
297 + memc = syscon_node_to_regmap(args.node);
299 + return PTR_ERR(memc);
301 + mt7621_get_clocks(priv, memc);
306 +static const struct udevice_id mt7621_clk_ids[] = {
307 + { .compatible = "mediatek,mt7621-clk" },
311 +U_BOOT_DRIVER(mt7621_clk) = {
312 + .name = "mt7621-clk",
314 + .of_match = mt7621_clk_ids,
315 + .probe = mt7621_clk_probe,
316 + .priv_auto = sizeof(struct mt7621_clk_priv),
317 + .ops = &mt7621_clk_ops,
320 +++ b/include/dt-bindings/clock/mt7621-clk.h
322 +/* SPDX-License-Identifier: GPL-2.0 */
324 + * Copyright (C) 2022 MediaTek Inc. All rights reserved.
326 + * Author: Weijie Gao <weijie.gao@mediatek.com>
329 +#ifndef _DT_BINDINGS_MT7621_CLK_H_
330 +#define _DT_BINDINGS_MT7621_CLK_H_
332 +#define MT7621_CLK_XTAL 0
333 +#define MT7621_CLK_CPU 1
334 +#define MT7621_CLK_BUS 2
335 +#define MT7621_CLK_50M 3
336 +#define MT7621_CLK_125M 4
337 +#define MT7621_CLK_150M 5
338 +#define MT7621_CLK_250M 6
339 +#define MT7621_CLK_270M 7
341 +#define MT7621_CLK_HSDMA 8
342 +#define MT7621_CLK_FE 9
343 +#define MT7621_CLK_SP_DIVTX 10
344 +#define MT7621_CLK_TIMER 11
345 +#define MT7621_CLK_PCM 12
346 +#define MT7621_CLK_PIO 13
347 +#define MT7621_CLK_GDMA 14
348 +#define MT7621_CLK_NAND 15
349 +#define MT7621_CLK_I2C 16
350 +#define MT7621_CLK_I2S 17
351 +#define MT7621_CLK_SPI 18
352 +#define MT7621_CLK_UART1 19
353 +#define MT7621_CLK_UART2 20
354 +#define MT7621_CLK_UART3 21
355 +#define MT7621_CLK_ETH 22
356 +#define MT7621_CLK_PCIE0 23
357 +#define MT7621_CLK_PCIE1 24
358 +#define MT7621_CLK_PCIE2 25
359 +#define MT7621_CLK_CRYPTO 26
360 +#define MT7621_CLK_SHXC 27
362 +#define MT7621_CLK_MAX 28
364 +/* for u-boot only */
365 +#define MT7621_CLK_DDR 29
367 +#endif /* _DT_BINDINGS_MT7621_CLK_H_ */