18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 2012 Sascha Hauer <kernel@pengutronix.de>
48c2ecf20Sopenharmony_ci */
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci#include <linux/module.h>
78c2ecf20Sopenharmony_ci#include <linux/clk.h>
88c2ecf20Sopenharmony_ci#include <linux/clkdev.h>
98c2ecf20Sopenharmony_ci#include <linux/io.h>
108c2ecf20Sopenharmony_ci#include <linux/err.h>
118c2ecf20Sopenharmony_ci#include <linux/of.h>
128c2ecf20Sopenharmony_ci#include <linux/of_address.h>
138c2ecf20Sopenharmony_ci#include <soc/imx/revision.h>
148c2ecf20Sopenharmony_ci#include <soc/imx/timer.h>
158c2ecf20Sopenharmony_ci#include <asm/irq.h>
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci#include "clk.h"
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci#define MX31_CCM_BASE_ADDR	0x53f80000
208c2ecf20Sopenharmony_ci#define MX31_GPT1_BASE_ADDR	0x53f90000
218c2ecf20Sopenharmony_ci#define MX31_INT_GPT		(NR_IRQS_LEGACY + 29)
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci#define MXC_CCM_CCMR		0x00
248c2ecf20Sopenharmony_ci#define MXC_CCM_PDR0		0x04
258c2ecf20Sopenharmony_ci#define MXC_CCM_PDR1		0x08
268c2ecf20Sopenharmony_ci#define MXC_CCM_MPCTL		0x10
278c2ecf20Sopenharmony_ci#define MXC_CCM_UPCTL		0x14
288c2ecf20Sopenharmony_ci#define MXC_CCM_SRPCTL		0x18
298c2ecf20Sopenharmony_ci#define MXC_CCM_CGR0		0x20
308c2ecf20Sopenharmony_ci#define MXC_CCM_CGR1		0x24
318c2ecf20Sopenharmony_ci#define MXC_CCM_CGR2		0x28
328c2ecf20Sopenharmony_ci#define MXC_CCM_PMCR0		0x5c
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_cistatic const char *mcu_main_sel[] = { "spll", "mpll", };
358c2ecf20Sopenharmony_cistatic const char *per_sel[] = { "per_div", "ipg", };
368c2ecf20Sopenharmony_cistatic const char *csi_sel[] = { "upll", "spll", };
378c2ecf20Sopenharmony_cistatic const char *fir_sel[] = { "mcu_main", "upll", "spll" };
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_cienum mx31_clks {
408c2ecf20Sopenharmony_ci	dummy, ckih, ckil, mpll, spll, upll, mcu_main, hsp, ahb, nfc, ipg,
418c2ecf20Sopenharmony_ci	per_div, per, csi, fir, csi_div, usb_div_pre, usb_div_post, fir_div_pre,
428c2ecf20Sopenharmony_ci	fir_div_post, sdhc1_gate, sdhc2_gate, gpt_gate, epit1_gate, epit2_gate,
438c2ecf20Sopenharmony_ci	iim_gate, ata_gate, sdma_gate, cspi3_gate, rng_gate, uart1_gate,
448c2ecf20Sopenharmony_ci	uart2_gate, ssi1_gate, i2c1_gate, i2c2_gate, i2c3_gate, hantro_gate,
458c2ecf20Sopenharmony_ci	mstick1_gate, mstick2_gate, csi_gate, rtc_gate, wdog_gate, pwm_gate,
468c2ecf20Sopenharmony_ci	sim_gate, ect_gate, usb_gate, kpp_gate, ipu_gate, uart3_gate,
478c2ecf20Sopenharmony_ci	uart4_gate, uart5_gate, owire_gate, ssi2_gate, cspi1_gate, cspi2_gate,
488c2ecf20Sopenharmony_ci	gacc_gate, emi_gate, rtic_gate, firi_gate, clk_max
498c2ecf20Sopenharmony_ci};
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_cistatic struct clk *clk[clk_max];
528c2ecf20Sopenharmony_cistatic struct clk_onecell_data clk_data;
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_cistatic struct clk ** const uart_clks[] __initconst = {
558c2ecf20Sopenharmony_ci	&clk[ipg],
568c2ecf20Sopenharmony_ci	&clk[uart1_gate],
578c2ecf20Sopenharmony_ci	&clk[uart2_gate],
588c2ecf20Sopenharmony_ci	&clk[uart3_gate],
598c2ecf20Sopenharmony_ci	&clk[uart4_gate],
608c2ecf20Sopenharmony_ci	&clk[uart5_gate],
618c2ecf20Sopenharmony_ci	NULL
628c2ecf20Sopenharmony_ci};
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_cistatic void __init _mx31_clocks_init(void __iomem *base, unsigned long fref)
658c2ecf20Sopenharmony_ci{
668c2ecf20Sopenharmony_ci	clk[dummy] = imx_clk_fixed("dummy", 0);
678c2ecf20Sopenharmony_ci	clk[ckih] = imx_clk_fixed("ckih", fref);
688c2ecf20Sopenharmony_ci	clk[ckil] = imx_clk_fixed("ckil", 32768);
698c2ecf20Sopenharmony_ci	clk[mpll] = imx_clk_pllv1(IMX_PLLV1_IMX31, "mpll", "ckih", base + MXC_CCM_MPCTL);
708c2ecf20Sopenharmony_ci	clk[spll] = imx_clk_pllv1(IMX_PLLV1_IMX31, "spll", "ckih", base + MXC_CCM_SRPCTL);
718c2ecf20Sopenharmony_ci	clk[upll] = imx_clk_pllv1(IMX_PLLV1_IMX31, "upll", "ckih", base + MXC_CCM_UPCTL);
728c2ecf20Sopenharmony_ci	clk[mcu_main] = imx_clk_mux("mcu_main", base + MXC_CCM_PMCR0, 31, 1, mcu_main_sel, ARRAY_SIZE(mcu_main_sel));
738c2ecf20Sopenharmony_ci	clk[hsp] = imx_clk_divider("hsp", "mcu_main", base + MXC_CCM_PDR0, 11, 3);
748c2ecf20Sopenharmony_ci	clk[ahb] = imx_clk_divider("ahb", "mcu_main", base + MXC_CCM_PDR0, 3, 3);
758c2ecf20Sopenharmony_ci	clk[nfc] = imx_clk_divider("nfc", "ahb", base + MXC_CCM_PDR0, 8, 3);
768c2ecf20Sopenharmony_ci	clk[ipg] = imx_clk_divider("ipg", "ahb", base + MXC_CCM_PDR0, 6, 2);
778c2ecf20Sopenharmony_ci	clk[per_div] = imx_clk_divider("per_div", "upll", base + MXC_CCM_PDR0, 16, 5);
788c2ecf20Sopenharmony_ci	clk[per] = imx_clk_mux("per", base + MXC_CCM_CCMR, 24, 1, per_sel, ARRAY_SIZE(per_sel));
798c2ecf20Sopenharmony_ci	clk[csi] = imx_clk_mux("csi_sel", base + MXC_CCM_CCMR, 25, 1, csi_sel, ARRAY_SIZE(csi_sel));
808c2ecf20Sopenharmony_ci	clk[fir] = imx_clk_mux("fir_sel", base + MXC_CCM_CCMR, 11, 2, fir_sel, ARRAY_SIZE(fir_sel));
818c2ecf20Sopenharmony_ci	clk[csi_div] = imx_clk_divider("csi_div", "csi_sel", base + MXC_CCM_PDR0, 23, 9);
828c2ecf20Sopenharmony_ci	clk[usb_div_pre] = imx_clk_divider("usb_div_pre", "upll", base + MXC_CCM_PDR1, 30, 2);
838c2ecf20Sopenharmony_ci	clk[usb_div_post] = imx_clk_divider("usb_div_post", "usb_div_pre", base + MXC_CCM_PDR1, 27, 3);
848c2ecf20Sopenharmony_ci	clk[fir_div_pre] = imx_clk_divider("fir_div_pre", "fir_sel", base + MXC_CCM_PDR1, 24, 3);
858c2ecf20Sopenharmony_ci	clk[fir_div_post] = imx_clk_divider("fir_div_post", "fir_div_pre", base + MXC_CCM_PDR1, 23, 6);
868c2ecf20Sopenharmony_ci	clk[sdhc1_gate] = imx_clk_gate2("sdhc1_gate", "per", base + MXC_CCM_CGR0, 0);
878c2ecf20Sopenharmony_ci	clk[sdhc2_gate] = imx_clk_gate2("sdhc2_gate", "per", base + MXC_CCM_CGR0, 2);
888c2ecf20Sopenharmony_ci	clk[gpt_gate] = imx_clk_gate2("gpt_gate", "per", base + MXC_CCM_CGR0, 4);
898c2ecf20Sopenharmony_ci	clk[epit1_gate] = imx_clk_gate2("epit1_gate", "per", base + MXC_CCM_CGR0, 6);
908c2ecf20Sopenharmony_ci	clk[epit2_gate] = imx_clk_gate2("epit2_gate", "per", base + MXC_CCM_CGR0, 8);
918c2ecf20Sopenharmony_ci	clk[iim_gate] = imx_clk_gate2("iim_gate", "ipg", base + MXC_CCM_CGR0, 10);
928c2ecf20Sopenharmony_ci	clk[ata_gate] = imx_clk_gate2("ata_gate", "ipg", base + MXC_CCM_CGR0, 12);
938c2ecf20Sopenharmony_ci	clk[sdma_gate] = imx_clk_gate2("sdma_gate", "ahb", base + MXC_CCM_CGR0, 14);
948c2ecf20Sopenharmony_ci	clk[cspi3_gate] = imx_clk_gate2("cspi3_gate", "ipg", base + MXC_CCM_CGR0, 16);
958c2ecf20Sopenharmony_ci	clk[rng_gate] = imx_clk_gate2("rng_gate", "ipg", base + MXC_CCM_CGR0, 18);
968c2ecf20Sopenharmony_ci	clk[uart1_gate] = imx_clk_gate2("uart1_gate", "per", base + MXC_CCM_CGR0, 20);
978c2ecf20Sopenharmony_ci	clk[uart2_gate] = imx_clk_gate2("uart2_gate", "per", base + MXC_CCM_CGR0, 22);
988c2ecf20Sopenharmony_ci	clk[ssi1_gate] = imx_clk_gate2("ssi1_gate", "spll", base + MXC_CCM_CGR0, 24);
998c2ecf20Sopenharmony_ci	clk[i2c1_gate] = imx_clk_gate2("i2c1_gate", "per", base + MXC_CCM_CGR0, 26);
1008c2ecf20Sopenharmony_ci	clk[i2c2_gate] = imx_clk_gate2("i2c2_gate", "per", base + MXC_CCM_CGR0, 28);
1018c2ecf20Sopenharmony_ci	clk[i2c3_gate] = imx_clk_gate2("i2c3_gate", "per", base + MXC_CCM_CGR0, 30);
1028c2ecf20Sopenharmony_ci	clk[hantro_gate] = imx_clk_gate2("hantro_gate", "per", base + MXC_CCM_CGR1, 0);
1038c2ecf20Sopenharmony_ci	clk[mstick1_gate] = imx_clk_gate2("mstick1_gate", "per", base + MXC_CCM_CGR1, 2);
1048c2ecf20Sopenharmony_ci	clk[mstick2_gate] = imx_clk_gate2("mstick2_gate", "per", base + MXC_CCM_CGR1, 4);
1058c2ecf20Sopenharmony_ci	clk[csi_gate] = imx_clk_gate2("csi_gate", "csi_div", base + MXC_CCM_CGR1, 6);
1068c2ecf20Sopenharmony_ci	clk[rtc_gate] = imx_clk_gate2("rtc_gate", "ipg", base + MXC_CCM_CGR1, 8);
1078c2ecf20Sopenharmony_ci	clk[wdog_gate] = imx_clk_gate2("wdog_gate", "ipg", base + MXC_CCM_CGR1, 10);
1088c2ecf20Sopenharmony_ci	clk[pwm_gate] = imx_clk_gate2("pwm_gate", "per", base + MXC_CCM_CGR1, 12);
1098c2ecf20Sopenharmony_ci	clk[sim_gate] = imx_clk_gate2("sim_gate", "per", base + MXC_CCM_CGR1, 14);
1108c2ecf20Sopenharmony_ci	clk[ect_gate] = imx_clk_gate2("ect_gate", "per", base + MXC_CCM_CGR1, 16);
1118c2ecf20Sopenharmony_ci	clk[usb_gate] = imx_clk_gate2("usb_gate", "ahb", base + MXC_CCM_CGR1, 18);
1128c2ecf20Sopenharmony_ci	clk[kpp_gate] = imx_clk_gate2("kpp_gate", "ipg", base + MXC_CCM_CGR1, 20);
1138c2ecf20Sopenharmony_ci	clk[ipu_gate] = imx_clk_gate2("ipu_gate", "hsp", base + MXC_CCM_CGR1, 22);
1148c2ecf20Sopenharmony_ci	clk[uart3_gate] = imx_clk_gate2("uart3_gate", "per", base + MXC_CCM_CGR1, 24);
1158c2ecf20Sopenharmony_ci	clk[uart4_gate] = imx_clk_gate2("uart4_gate", "per", base + MXC_CCM_CGR1, 26);
1168c2ecf20Sopenharmony_ci	clk[uart5_gate] = imx_clk_gate2("uart5_gate", "per", base + MXC_CCM_CGR1, 28);
1178c2ecf20Sopenharmony_ci	clk[owire_gate] = imx_clk_gate2("owire_gate", "per", base + MXC_CCM_CGR1, 30);
1188c2ecf20Sopenharmony_ci	clk[ssi2_gate] = imx_clk_gate2("ssi2_gate", "spll", base + MXC_CCM_CGR2, 0);
1198c2ecf20Sopenharmony_ci	clk[cspi1_gate] = imx_clk_gate2("cspi1_gate", "ipg", base + MXC_CCM_CGR2, 2);
1208c2ecf20Sopenharmony_ci	clk[cspi2_gate] = imx_clk_gate2("cspi2_gate", "ipg", base + MXC_CCM_CGR2, 4);
1218c2ecf20Sopenharmony_ci	clk[gacc_gate] = imx_clk_gate2("gacc_gate", "per", base + MXC_CCM_CGR2, 6);
1228c2ecf20Sopenharmony_ci	clk[emi_gate] = imx_clk_gate2("emi_gate", "ahb", base + MXC_CCM_CGR2, 8);
1238c2ecf20Sopenharmony_ci	clk[rtic_gate] = imx_clk_gate2("rtic_gate", "ahb", base + MXC_CCM_CGR2, 10);
1248c2ecf20Sopenharmony_ci	clk[firi_gate] = imx_clk_gate2("firi_gate", "upll", base+MXC_CCM_CGR2, 12);
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci	imx_check_clocks(clk, ARRAY_SIZE(clk));
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci	clk_set_parent(clk[csi], clk[upll]);
1298c2ecf20Sopenharmony_ci	clk_prepare_enable(clk[emi_gate]);
1308c2ecf20Sopenharmony_ci	clk_prepare_enable(clk[iim_gate]);
1318c2ecf20Sopenharmony_ci	mx31_revision();
1328c2ecf20Sopenharmony_ci	clk_disable_unprepare(clk[iim_gate]);
1338c2ecf20Sopenharmony_ci}
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_cistatic void __init mx31_clocks_init_dt(struct device_node *np)
1368c2ecf20Sopenharmony_ci{
1378c2ecf20Sopenharmony_ci	struct device_node *osc_np;
1388c2ecf20Sopenharmony_ci	u32 fref = 26000000; /* default */
1398c2ecf20Sopenharmony_ci	void __iomem *ccm;
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci	for_each_compatible_node(osc_np, NULL, "fixed-clock") {
1428c2ecf20Sopenharmony_ci		if (!of_device_is_compatible(osc_np, "fsl,imx-osc26m"))
1438c2ecf20Sopenharmony_ci			continue;
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci		if (!of_property_read_u32(osc_np, "clock-frequency", &fref)) {
1468c2ecf20Sopenharmony_ci			of_node_put(osc_np);
1478c2ecf20Sopenharmony_ci			break;
1488c2ecf20Sopenharmony_ci		}
1498c2ecf20Sopenharmony_ci	}
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci	ccm = of_iomap(np, 0);
1528c2ecf20Sopenharmony_ci	if (!ccm)
1538c2ecf20Sopenharmony_ci		panic("%s: failed to map registers\n", __func__);
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci	_mx31_clocks_init(ccm, fref);
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci	clk_data.clks = clk;
1588c2ecf20Sopenharmony_ci	clk_data.clk_num = ARRAY_SIZE(clk);
1598c2ecf20Sopenharmony_ci	of_clk_add_provider(np, of_clk_src_onecell_get, &clk_data);
1608c2ecf20Sopenharmony_ci}
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ciCLK_OF_DECLARE(imx31_ccm, "fsl,imx31-ccm", mx31_clocks_init_dt);
163