162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Copyright (C) 2012 Sascha Hauer <kernel@pengutronix.de>
462306a36Sopenharmony_ci */
562306a36Sopenharmony_ci
662306a36Sopenharmony_ci#include <linux/module.h>
762306a36Sopenharmony_ci#include <linux/clk.h>
862306a36Sopenharmony_ci#include <linux/clkdev.h>
962306a36Sopenharmony_ci#include <linux/io.h>
1062306a36Sopenharmony_ci#include <linux/err.h>
1162306a36Sopenharmony_ci#include <linux/of.h>
1262306a36Sopenharmony_ci#include <linux/of_address.h>
1362306a36Sopenharmony_ci#include <soc/imx/revision.h>
1462306a36Sopenharmony_ci#include <asm/irq.h>
1562306a36Sopenharmony_ci
1662306a36Sopenharmony_ci#include "clk.h"
1762306a36Sopenharmony_ci
1862306a36Sopenharmony_ci#define MX31_CCM_BASE_ADDR	0x53f80000
1962306a36Sopenharmony_ci#define MX31_GPT1_BASE_ADDR	0x53f90000
2062306a36Sopenharmony_ci#define MX31_INT_GPT		(NR_IRQS_LEGACY + 29)
2162306a36Sopenharmony_ci
2262306a36Sopenharmony_ci#define MXC_CCM_CCMR		0x00
2362306a36Sopenharmony_ci#define MXC_CCM_PDR0		0x04
2462306a36Sopenharmony_ci#define MXC_CCM_PDR1		0x08
2562306a36Sopenharmony_ci#define MXC_CCM_MPCTL		0x10
2662306a36Sopenharmony_ci#define MXC_CCM_UPCTL		0x14
2762306a36Sopenharmony_ci#define MXC_CCM_SRPCTL		0x18
2862306a36Sopenharmony_ci#define MXC_CCM_CGR0		0x20
2962306a36Sopenharmony_ci#define MXC_CCM_CGR1		0x24
3062306a36Sopenharmony_ci#define MXC_CCM_CGR2		0x28
3162306a36Sopenharmony_ci#define MXC_CCM_PMCR0		0x5c
3262306a36Sopenharmony_ci
3362306a36Sopenharmony_cistatic const char *mcu_main_sel[] = { "spll", "mpll", };
3462306a36Sopenharmony_cistatic const char *per_sel[] = { "per_div", "ipg", };
3562306a36Sopenharmony_cistatic const char *csi_sel[] = { "upll", "spll", };
3662306a36Sopenharmony_cistatic const char *fir_sel[] = { "mcu_main", "upll", "spll" };
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_cienum mx31_clks {
3962306a36Sopenharmony_ci	dummy, ckih, ckil, mpll, spll, upll, mcu_main, hsp, ahb, nfc, ipg,
4062306a36Sopenharmony_ci	per_div, per, csi, fir, csi_div, usb_div_pre, usb_div_post, fir_div_pre,
4162306a36Sopenharmony_ci	fir_div_post, sdhc1_gate, sdhc2_gate, gpt_gate, epit1_gate, epit2_gate,
4262306a36Sopenharmony_ci	iim_gate, ata_gate, sdma_gate, cspi3_gate, rng_gate, uart1_gate,
4362306a36Sopenharmony_ci	uart2_gate, ssi1_gate, i2c1_gate, i2c2_gate, i2c3_gate, hantro_gate,
4462306a36Sopenharmony_ci	mstick1_gate, mstick2_gate, csi_gate, rtc_gate, wdog_gate, pwm_gate,
4562306a36Sopenharmony_ci	sim_gate, ect_gate, usb_gate, kpp_gate, ipu_gate, uart3_gate,
4662306a36Sopenharmony_ci	uart4_gate, uart5_gate, owire_gate, ssi2_gate, cspi1_gate, cspi2_gate,
4762306a36Sopenharmony_ci	gacc_gate, emi_gate, rtic_gate, firi_gate, clk_max
4862306a36Sopenharmony_ci};
4962306a36Sopenharmony_ci
5062306a36Sopenharmony_cistatic struct clk *clk[clk_max];
5162306a36Sopenharmony_cistatic struct clk_onecell_data clk_data;
5262306a36Sopenharmony_ci
5362306a36Sopenharmony_cistatic void __init _mx31_clocks_init(void __iomem *base, unsigned long fref)
5462306a36Sopenharmony_ci{
5562306a36Sopenharmony_ci	clk[dummy] = imx_clk_fixed("dummy", 0);
5662306a36Sopenharmony_ci	clk[ckih] = imx_clk_fixed("ckih", fref);
5762306a36Sopenharmony_ci	clk[ckil] = imx_clk_fixed("ckil", 32768);
5862306a36Sopenharmony_ci	clk[mpll] = imx_clk_pllv1(IMX_PLLV1_IMX31, "mpll", "ckih", base + MXC_CCM_MPCTL);
5962306a36Sopenharmony_ci	clk[spll] = imx_clk_pllv1(IMX_PLLV1_IMX31, "spll", "ckih", base + MXC_CCM_SRPCTL);
6062306a36Sopenharmony_ci	clk[upll] = imx_clk_pllv1(IMX_PLLV1_IMX31, "upll", "ckih", base + MXC_CCM_UPCTL);
6162306a36Sopenharmony_ci	clk[mcu_main] = imx_clk_mux("mcu_main", base + MXC_CCM_PMCR0, 31, 1, mcu_main_sel, ARRAY_SIZE(mcu_main_sel));
6262306a36Sopenharmony_ci	clk[hsp] = imx_clk_divider("hsp", "mcu_main", base + MXC_CCM_PDR0, 11, 3);
6362306a36Sopenharmony_ci	clk[ahb] = imx_clk_divider("ahb", "mcu_main", base + MXC_CCM_PDR0, 3, 3);
6462306a36Sopenharmony_ci	clk[nfc] = imx_clk_divider("nfc", "ahb", base + MXC_CCM_PDR0, 8, 3);
6562306a36Sopenharmony_ci	clk[ipg] = imx_clk_divider("ipg", "ahb", base + MXC_CCM_PDR0, 6, 2);
6662306a36Sopenharmony_ci	clk[per_div] = imx_clk_divider("per_div", "upll", base + MXC_CCM_PDR0, 16, 5);
6762306a36Sopenharmony_ci	clk[per] = imx_clk_mux("per", base + MXC_CCM_CCMR, 24, 1, per_sel, ARRAY_SIZE(per_sel));
6862306a36Sopenharmony_ci	clk[csi] = imx_clk_mux("csi_sel", base + MXC_CCM_CCMR, 25, 1, csi_sel, ARRAY_SIZE(csi_sel));
6962306a36Sopenharmony_ci	clk[fir] = imx_clk_mux("fir_sel", base + MXC_CCM_CCMR, 11, 2, fir_sel, ARRAY_SIZE(fir_sel));
7062306a36Sopenharmony_ci	clk[csi_div] = imx_clk_divider("csi_div", "csi_sel", base + MXC_CCM_PDR0, 23, 9);
7162306a36Sopenharmony_ci	clk[usb_div_pre] = imx_clk_divider("usb_div_pre", "upll", base + MXC_CCM_PDR1, 30, 2);
7262306a36Sopenharmony_ci	clk[usb_div_post] = imx_clk_divider("usb_div_post", "usb_div_pre", base + MXC_CCM_PDR1, 27, 3);
7362306a36Sopenharmony_ci	clk[fir_div_pre] = imx_clk_divider("fir_div_pre", "fir_sel", base + MXC_CCM_PDR1, 24, 3);
7462306a36Sopenharmony_ci	clk[fir_div_post] = imx_clk_divider("fir_div_post", "fir_div_pre", base + MXC_CCM_PDR1, 23, 6);
7562306a36Sopenharmony_ci	clk[sdhc1_gate] = imx_clk_gate2("sdhc1_gate", "per", base + MXC_CCM_CGR0, 0);
7662306a36Sopenharmony_ci	clk[sdhc2_gate] = imx_clk_gate2("sdhc2_gate", "per", base + MXC_CCM_CGR0, 2);
7762306a36Sopenharmony_ci	clk[gpt_gate] = imx_clk_gate2("gpt_gate", "per", base + MXC_CCM_CGR0, 4);
7862306a36Sopenharmony_ci	clk[epit1_gate] = imx_clk_gate2("epit1_gate", "per", base + MXC_CCM_CGR0, 6);
7962306a36Sopenharmony_ci	clk[epit2_gate] = imx_clk_gate2("epit2_gate", "per", base + MXC_CCM_CGR0, 8);
8062306a36Sopenharmony_ci	clk[iim_gate] = imx_clk_gate2("iim_gate", "ipg", base + MXC_CCM_CGR0, 10);
8162306a36Sopenharmony_ci	clk[ata_gate] = imx_clk_gate2("ata_gate", "ipg", base + MXC_CCM_CGR0, 12);
8262306a36Sopenharmony_ci	clk[sdma_gate] = imx_clk_gate2("sdma_gate", "ahb", base + MXC_CCM_CGR0, 14);
8362306a36Sopenharmony_ci	clk[cspi3_gate] = imx_clk_gate2("cspi3_gate", "ipg", base + MXC_CCM_CGR0, 16);
8462306a36Sopenharmony_ci	clk[rng_gate] = imx_clk_gate2("rng_gate", "ipg", base + MXC_CCM_CGR0, 18);
8562306a36Sopenharmony_ci	clk[uart1_gate] = imx_clk_gate2("uart1_gate", "per", base + MXC_CCM_CGR0, 20);
8662306a36Sopenharmony_ci	clk[uart2_gate] = imx_clk_gate2("uart2_gate", "per", base + MXC_CCM_CGR0, 22);
8762306a36Sopenharmony_ci	clk[ssi1_gate] = imx_clk_gate2("ssi1_gate", "spll", base + MXC_CCM_CGR0, 24);
8862306a36Sopenharmony_ci	clk[i2c1_gate] = imx_clk_gate2("i2c1_gate", "per", base + MXC_CCM_CGR0, 26);
8962306a36Sopenharmony_ci	clk[i2c2_gate] = imx_clk_gate2("i2c2_gate", "per", base + MXC_CCM_CGR0, 28);
9062306a36Sopenharmony_ci	clk[i2c3_gate] = imx_clk_gate2("i2c3_gate", "per", base + MXC_CCM_CGR0, 30);
9162306a36Sopenharmony_ci	clk[hantro_gate] = imx_clk_gate2("hantro_gate", "per", base + MXC_CCM_CGR1, 0);
9262306a36Sopenharmony_ci	clk[mstick1_gate] = imx_clk_gate2("mstick1_gate", "per", base + MXC_CCM_CGR1, 2);
9362306a36Sopenharmony_ci	clk[mstick2_gate] = imx_clk_gate2("mstick2_gate", "per", base + MXC_CCM_CGR1, 4);
9462306a36Sopenharmony_ci	clk[csi_gate] = imx_clk_gate2("csi_gate", "csi_div", base + MXC_CCM_CGR1, 6);
9562306a36Sopenharmony_ci	clk[rtc_gate] = imx_clk_gate2("rtc_gate", "ipg", base + MXC_CCM_CGR1, 8);
9662306a36Sopenharmony_ci	clk[wdog_gate] = imx_clk_gate2("wdog_gate", "ipg", base + MXC_CCM_CGR1, 10);
9762306a36Sopenharmony_ci	clk[pwm_gate] = imx_clk_gate2("pwm_gate", "per", base + MXC_CCM_CGR1, 12);
9862306a36Sopenharmony_ci	clk[sim_gate] = imx_clk_gate2("sim_gate", "per", base + MXC_CCM_CGR1, 14);
9962306a36Sopenharmony_ci	clk[ect_gate] = imx_clk_gate2("ect_gate", "per", base + MXC_CCM_CGR1, 16);
10062306a36Sopenharmony_ci	clk[usb_gate] = imx_clk_gate2("usb_gate", "ahb", base + MXC_CCM_CGR1, 18);
10162306a36Sopenharmony_ci	clk[kpp_gate] = imx_clk_gate2("kpp_gate", "ipg", base + MXC_CCM_CGR1, 20);
10262306a36Sopenharmony_ci	clk[ipu_gate] = imx_clk_gate2("ipu_gate", "hsp", base + MXC_CCM_CGR1, 22);
10362306a36Sopenharmony_ci	clk[uart3_gate] = imx_clk_gate2("uart3_gate", "per", base + MXC_CCM_CGR1, 24);
10462306a36Sopenharmony_ci	clk[uart4_gate] = imx_clk_gate2("uart4_gate", "per", base + MXC_CCM_CGR1, 26);
10562306a36Sopenharmony_ci	clk[uart5_gate] = imx_clk_gate2("uart5_gate", "per", base + MXC_CCM_CGR1, 28);
10662306a36Sopenharmony_ci	clk[owire_gate] = imx_clk_gate2("owire_gate", "per", base + MXC_CCM_CGR1, 30);
10762306a36Sopenharmony_ci	clk[ssi2_gate] = imx_clk_gate2("ssi2_gate", "spll", base + MXC_CCM_CGR2, 0);
10862306a36Sopenharmony_ci	clk[cspi1_gate] = imx_clk_gate2("cspi1_gate", "ipg", base + MXC_CCM_CGR2, 2);
10962306a36Sopenharmony_ci	clk[cspi2_gate] = imx_clk_gate2("cspi2_gate", "ipg", base + MXC_CCM_CGR2, 4);
11062306a36Sopenharmony_ci	clk[gacc_gate] = imx_clk_gate2("gacc_gate", "per", base + MXC_CCM_CGR2, 6);
11162306a36Sopenharmony_ci	clk[emi_gate] = imx_clk_gate2("emi_gate", "ahb", base + MXC_CCM_CGR2, 8);
11262306a36Sopenharmony_ci	clk[rtic_gate] = imx_clk_gate2("rtic_gate", "ahb", base + MXC_CCM_CGR2, 10);
11362306a36Sopenharmony_ci	clk[firi_gate] = imx_clk_gate2("firi_gate", "upll", base+MXC_CCM_CGR2, 12);
11462306a36Sopenharmony_ci
11562306a36Sopenharmony_ci	imx_check_clocks(clk, ARRAY_SIZE(clk));
11662306a36Sopenharmony_ci
11762306a36Sopenharmony_ci	clk_set_parent(clk[csi], clk[upll]);
11862306a36Sopenharmony_ci	clk_prepare_enable(clk[emi_gate]);
11962306a36Sopenharmony_ci	clk_prepare_enable(clk[iim_gate]);
12062306a36Sopenharmony_ci	mx31_revision();
12162306a36Sopenharmony_ci	clk_disable_unprepare(clk[iim_gate]);
12262306a36Sopenharmony_ci}
12362306a36Sopenharmony_ci
12462306a36Sopenharmony_cistatic void __init mx31_clocks_init_dt(struct device_node *np)
12562306a36Sopenharmony_ci{
12662306a36Sopenharmony_ci	struct device_node *osc_np;
12762306a36Sopenharmony_ci	u32 fref = 26000000; /* default */
12862306a36Sopenharmony_ci	void __iomem *ccm;
12962306a36Sopenharmony_ci
13062306a36Sopenharmony_ci	for_each_compatible_node(osc_np, NULL, "fixed-clock") {
13162306a36Sopenharmony_ci		if (!of_device_is_compatible(osc_np, "fsl,imx-osc26m"))
13262306a36Sopenharmony_ci			continue;
13362306a36Sopenharmony_ci
13462306a36Sopenharmony_ci		if (!of_property_read_u32(osc_np, "clock-frequency", &fref)) {
13562306a36Sopenharmony_ci			of_node_put(osc_np);
13662306a36Sopenharmony_ci			break;
13762306a36Sopenharmony_ci		}
13862306a36Sopenharmony_ci	}
13962306a36Sopenharmony_ci
14062306a36Sopenharmony_ci	ccm = of_iomap(np, 0);
14162306a36Sopenharmony_ci	if (!ccm)
14262306a36Sopenharmony_ci		panic("%s: failed to map registers\n", __func__);
14362306a36Sopenharmony_ci
14462306a36Sopenharmony_ci	_mx31_clocks_init(ccm, fref);
14562306a36Sopenharmony_ci
14662306a36Sopenharmony_ci	clk_data.clks = clk;
14762306a36Sopenharmony_ci	clk_data.clk_num = ARRAY_SIZE(clk);
14862306a36Sopenharmony_ci	of_clk_add_provider(np, of_clk_src_onecell_get, &clk_data);
14962306a36Sopenharmony_ci}
15062306a36Sopenharmony_ci
15162306a36Sopenharmony_ciCLK_OF_DECLARE(imx31_ccm, "fsl,imx31-ccm", mx31_clocks_init_dt);
152