1*8b9844d7SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
211f68120SShawn Guo /*
311f68120SShawn Guo * Copyright (C) 2012 Sascha Hauer <kernel@pengutronix.de>
411f68120SShawn Guo */
511f68120SShawn Guo
611f68120SShawn Guo #include <linux/module.h>
711f68120SShawn Guo #include <linux/clk.h>
811f68120SShawn Guo #include <linux/clkdev.h>
911f68120SShawn Guo #include <linux/io.h>
1011f68120SShawn Guo #include <linux/err.h>
1111f68120SShawn Guo #include <linux/of.h>
126fe5aeb5SVladimir Zapolskiy #include <linux/of_address.h>
1311f68120SShawn Guo #include <soc/imx/revision.h>
1411f68120SShawn Guo #include <asm/irq.h>
1511f68120SShawn Guo
1611f68120SShawn Guo #include "clk.h"
1711f68120SShawn Guo
1811f68120SShawn Guo #define MX31_CCM_BASE_ADDR 0x53f80000
1911f68120SShawn Guo #define MX31_GPT1_BASE_ADDR 0x53f90000
2011f68120SShawn Guo #define MX31_INT_GPT (NR_IRQS_LEGACY + 29)
2111f68120SShawn Guo
2211f68120SShawn Guo #define MXC_CCM_CCMR 0x00
2311f68120SShawn Guo #define MXC_CCM_PDR0 0x04
2411f68120SShawn Guo #define MXC_CCM_PDR1 0x08
2511f68120SShawn Guo #define MXC_CCM_MPCTL 0x10
2611f68120SShawn Guo #define MXC_CCM_UPCTL 0x14
2711f68120SShawn Guo #define MXC_CCM_SRPCTL 0x18
2811f68120SShawn Guo #define MXC_CCM_CGR0 0x20
2911f68120SShawn Guo #define MXC_CCM_CGR1 0x24
3011f68120SShawn Guo #define MXC_CCM_CGR2 0x28
3111f68120SShawn Guo #define MXC_CCM_PMCR0 0x5c
3211f68120SShawn Guo
3311f68120SShawn Guo static const char *mcu_main_sel[] = { "spll", "mpll", };
3411f68120SShawn Guo static const char *per_sel[] = { "per_div", "ipg", };
3511f68120SShawn Guo static const char *csi_sel[] = { "upll", "spll", };
3611f68120SShawn Guo static const char *fir_sel[] = { "mcu_main", "upll", "spll" };
3711f68120SShawn Guo
3811f68120SShawn Guo enum mx31_clks {
3911f68120SShawn Guo dummy, ckih, ckil, mpll, spll, upll, mcu_main, hsp, ahb, nfc, ipg,
4011f68120SShawn Guo per_div, per, csi, fir, csi_div, usb_div_pre, usb_div_post, fir_div_pre,
4111f68120SShawn Guo fir_div_post, sdhc1_gate, sdhc2_gate, gpt_gate, epit1_gate, epit2_gate,
4211f68120SShawn Guo iim_gate, ata_gate, sdma_gate, cspi3_gate, rng_gate, uart1_gate,
4311f68120SShawn Guo uart2_gate, ssi1_gate, i2c1_gate, i2c2_gate, i2c3_gate, hantro_gate,
4411f68120SShawn Guo mstick1_gate, mstick2_gate, csi_gate, rtc_gate, wdog_gate, pwm_gate,
4511f68120SShawn Guo sim_gate, ect_gate, usb_gate, kpp_gate, ipu_gate, uart3_gate,
4611f68120SShawn Guo uart4_gate, uart5_gate, owire_gate, ssi2_gate, cspi1_gate, cspi2_gate,
4711f68120SShawn Guo gacc_gate, emi_gate, rtic_gate, firi_gate, clk_max
4811f68120SShawn Guo };
4911f68120SShawn Guo
5011f68120SShawn Guo static struct clk *clk[clk_max];
5111f68120SShawn Guo static struct clk_onecell_data clk_data;
5211f68120SShawn Guo
_mx31_clocks_init(void __iomem * base,unsigned long fref)536fe5aeb5SVladimir Zapolskiy static void __init _mx31_clocks_init(void __iomem *base, unsigned long fref)
5411f68120SShawn Guo {
5511f68120SShawn Guo clk[dummy] = imx_clk_fixed("dummy", 0);
5611f68120SShawn Guo clk[ckih] = imx_clk_fixed("ckih", fref);
5711f68120SShawn Guo clk[ckil] = imx_clk_fixed("ckil", 32768);
5811f68120SShawn Guo clk[mpll] = imx_clk_pllv1(IMX_PLLV1_IMX31, "mpll", "ckih", base + MXC_CCM_MPCTL);
5911f68120SShawn Guo clk[spll] = imx_clk_pllv1(IMX_PLLV1_IMX31, "spll", "ckih", base + MXC_CCM_SRPCTL);
6011f68120SShawn Guo clk[upll] = imx_clk_pllv1(IMX_PLLV1_IMX31, "upll", "ckih", base + MXC_CCM_UPCTL);
6111f68120SShawn Guo clk[mcu_main] = imx_clk_mux("mcu_main", base + MXC_CCM_PMCR0, 31, 1, mcu_main_sel, ARRAY_SIZE(mcu_main_sel));
6211f68120SShawn Guo clk[hsp] = imx_clk_divider("hsp", "mcu_main", base + MXC_CCM_PDR0, 11, 3);
6311f68120SShawn Guo clk[ahb] = imx_clk_divider("ahb", "mcu_main", base + MXC_CCM_PDR0, 3, 3);
6411f68120SShawn Guo clk[nfc] = imx_clk_divider("nfc", "ahb", base + MXC_CCM_PDR0, 8, 3);
6511f68120SShawn Guo clk[ipg] = imx_clk_divider("ipg", "ahb", base + MXC_CCM_PDR0, 6, 2);
6611f68120SShawn Guo clk[per_div] = imx_clk_divider("per_div", "upll", base + MXC_CCM_PDR0, 16, 5);
6711f68120SShawn Guo clk[per] = imx_clk_mux("per", base + MXC_CCM_CCMR, 24, 1, per_sel, ARRAY_SIZE(per_sel));
6811f68120SShawn Guo clk[csi] = imx_clk_mux("csi_sel", base + MXC_CCM_CCMR, 25, 1, csi_sel, ARRAY_SIZE(csi_sel));
6911f68120SShawn Guo clk[fir] = imx_clk_mux("fir_sel", base + MXC_CCM_CCMR, 11, 2, fir_sel, ARRAY_SIZE(fir_sel));
7011f68120SShawn Guo clk[csi_div] = imx_clk_divider("csi_div", "csi_sel", base + MXC_CCM_PDR0, 23, 9);
7111f68120SShawn Guo clk[usb_div_pre] = imx_clk_divider("usb_div_pre", "upll", base + MXC_CCM_PDR1, 30, 2);
7211f68120SShawn Guo clk[usb_div_post] = imx_clk_divider("usb_div_post", "usb_div_pre", base + MXC_CCM_PDR1, 27, 3);
7311f68120SShawn Guo clk[fir_div_pre] = imx_clk_divider("fir_div_pre", "fir_sel", base + MXC_CCM_PDR1, 24, 3);
7411f68120SShawn Guo clk[fir_div_post] = imx_clk_divider("fir_div_post", "fir_div_pre", base + MXC_CCM_PDR1, 23, 6);
7511f68120SShawn Guo clk[sdhc1_gate] = imx_clk_gate2("sdhc1_gate", "per", base + MXC_CCM_CGR0, 0);
7611f68120SShawn Guo clk[sdhc2_gate] = imx_clk_gate2("sdhc2_gate", "per", base + MXC_CCM_CGR0, 2);
7711f68120SShawn Guo clk[gpt_gate] = imx_clk_gate2("gpt_gate", "per", base + MXC_CCM_CGR0, 4);
7811f68120SShawn Guo clk[epit1_gate] = imx_clk_gate2("epit1_gate", "per", base + MXC_CCM_CGR0, 6);
7911f68120SShawn Guo clk[epit2_gate] = imx_clk_gate2("epit2_gate", "per", base + MXC_CCM_CGR0, 8);
8011f68120SShawn Guo clk[iim_gate] = imx_clk_gate2("iim_gate", "ipg", base + MXC_CCM_CGR0, 10);
8111f68120SShawn Guo clk[ata_gate] = imx_clk_gate2("ata_gate", "ipg", base + MXC_CCM_CGR0, 12);
8211f68120SShawn Guo clk[sdma_gate] = imx_clk_gate2("sdma_gate", "ahb", base + MXC_CCM_CGR0, 14);
8311f68120SShawn Guo clk[cspi3_gate] = imx_clk_gate2("cspi3_gate", "ipg", base + MXC_CCM_CGR0, 16);
8411f68120SShawn Guo clk[rng_gate] = imx_clk_gate2("rng_gate", "ipg", base + MXC_CCM_CGR0, 18);
8511f68120SShawn Guo clk[uart1_gate] = imx_clk_gate2("uart1_gate", "per", base + MXC_CCM_CGR0, 20);
8611f68120SShawn Guo clk[uart2_gate] = imx_clk_gate2("uart2_gate", "per", base + MXC_CCM_CGR0, 22);
8711f68120SShawn Guo clk[ssi1_gate] = imx_clk_gate2("ssi1_gate", "spll", base + MXC_CCM_CGR0, 24);
8811f68120SShawn Guo clk[i2c1_gate] = imx_clk_gate2("i2c1_gate", "per", base + MXC_CCM_CGR0, 26);
8911f68120SShawn Guo clk[i2c2_gate] = imx_clk_gate2("i2c2_gate", "per", base + MXC_CCM_CGR0, 28);
9011f68120SShawn Guo clk[i2c3_gate] = imx_clk_gate2("i2c3_gate", "per", base + MXC_CCM_CGR0, 30);
9111f68120SShawn Guo clk[hantro_gate] = imx_clk_gate2("hantro_gate", "per", base + MXC_CCM_CGR1, 0);
9211f68120SShawn Guo clk[mstick1_gate] = imx_clk_gate2("mstick1_gate", "per", base + MXC_CCM_CGR1, 2);
9311f68120SShawn Guo clk[mstick2_gate] = imx_clk_gate2("mstick2_gate", "per", base + MXC_CCM_CGR1, 4);
9411f68120SShawn Guo clk[csi_gate] = imx_clk_gate2("csi_gate", "csi_div", base + MXC_CCM_CGR1, 6);
9511f68120SShawn Guo clk[rtc_gate] = imx_clk_gate2("rtc_gate", "ipg", base + MXC_CCM_CGR1, 8);
9611f68120SShawn Guo clk[wdog_gate] = imx_clk_gate2("wdog_gate", "ipg", base + MXC_CCM_CGR1, 10);
9711f68120SShawn Guo clk[pwm_gate] = imx_clk_gate2("pwm_gate", "per", base + MXC_CCM_CGR1, 12);
9811f68120SShawn Guo clk[sim_gate] = imx_clk_gate2("sim_gate", "per", base + MXC_CCM_CGR1, 14);
9911f68120SShawn Guo clk[ect_gate] = imx_clk_gate2("ect_gate", "per", base + MXC_CCM_CGR1, 16);
10011f68120SShawn Guo clk[usb_gate] = imx_clk_gate2("usb_gate", "ahb", base + MXC_CCM_CGR1, 18);
10111f68120SShawn Guo clk[kpp_gate] = imx_clk_gate2("kpp_gate", "ipg", base + MXC_CCM_CGR1, 20);
10211f68120SShawn Guo clk[ipu_gate] = imx_clk_gate2("ipu_gate", "hsp", base + MXC_CCM_CGR1, 22);
10311f68120SShawn Guo clk[uart3_gate] = imx_clk_gate2("uart3_gate", "per", base + MXC_CCM_CGR1, 24);
10411f68120SShawn Guo clk[uart4_gate] = imx_clk_gate2("uart4_gate", "per", base + MXC_CCM_CGR1, 26);
10511f68120SShawn Guo clk[uart5_gate] = imx_clk_gate2("uart5_gate", "per", base + MXC_CCM_CGR1, 28);
10611f68120SShawn Guo clk[owire_gate] = imx_clk_gate2("owire_gate", "per", base + MXC_CCM_CGR1, 30);
10711f68120SShawn Guo clk[ssi2_gate] = imx_clk_gate2("ssi2_gate", "spll", base + MXC_CCM_CGR2, 0);
10811f68120SShawn Guo clk[cspi1_gate] = imx_clk_gate2("cspi1_gate", "ipg", base + MXC_CCM_CGR2, 2);
10911f68120SShawn Guo clk[cspi2_gate] = imx_clk_gate2("cspi2_gate", "ipg", base + MXC_CCM_CGR2, 4);
11011f68120SShawn Guo clk[gacc_gate] = imx_clk_gate2("gacc_gate", "per", base + MXC_CCM_CGR2, 6);
11111f68120SShawn Guo clk[emi_gate] = imx_clk_gate2("emi_gate", "ahb", base + MXC_CCM_CGR2, 8);
11211f68120SShawn Guo clk[rtic_gate] = imx_clk_gate2("rtic_gate", "ahb", base + MXC_CCM_CGR2, 10);
11311f68120SShawn Guo clk[firi_gate] = imx_clk_gate2("firi_gate", "upll", base+MXC_CCM_CGR2, 12);
11411f68120SShawn Guo
11511f68120SShawn Guo imx_check_clocks(clk, ARRAY_SIZE(clk));
11611f68120SShawn Guo
117d9388c84SAlexander Stein clk_set_parent(clk[csi], clk[upll]);
118d9388c84SAlexander Stein clk_prepare_enable(clk[emi_gate]);
119d9388c84SAlexander Stein clk_prepare_enable(clk[iim_gate]);
120d9388c84SAlexander Stein mx31_revision();
121d9388c84SAlexander Stein clk_disable_unprepare(clk[iim_gate]);
122d9388c84SAlexander Stein }
123d9388c84SAlexander Stein
mx31_clocks_init_dt(struct device_node * np)1246fe5aeb5SVladimir Zapolskiy static void __init mx31_clocks_init_dt(struct device_node *np)
12511f68120SShawn Guo {
1266fe5aeb5SVladimir Zapolskiy struct device_node *osc_np;
12711f68120SShawn Guo u32 fref = 26000000; /* default */
1286fe5aeb5SVladimir Zapolskiy void __iomem *ccm;
12911f68120SShawn Guo
1306fe5aeb5SVladimir Zapolskiy for_each_compatible_node(osc_np, NULL, "fixed-clock") {
1316fe5aeb5SVladimir Zapolskiy if (!of_device_is_compatible(osc_np, "fsl,imx-osc26m"))
13211f68120SShawn Guo continue;
13311f68120SShawn Guo
1346fe5aeb5SVladimir Zapolskiy if (!of_property_read_u32(osc_np, "clock-frequency", &fref)) {
1356fe5aeb5SVladimir Zapolskiy of_node_put(osc_np);
13611f68120SShawn Guo break;
13711f68120SShawn Guo }
138489e5d41SJulia Lawall }
13911f68120SShawn Guo
1406fe5aeb5SVladimir Zapolskiy ccm = of_iomap(np, 0);
1416fe5aeb5SVladimir Zapolskiy if (!ccm)
1426fe5aeb5SVladimir Zapolskiy panic("%s: failed to map registers\n", __func__);
143d9388c84SAlexander Stein
1446fe5aeb5SVladimir Zapolskiy _mx31_clocks_init(ccm, fref);
1456fe5aeb5SVladimir Zapolskiy
1466fe5aeb5SVladimir Zapolskiy clk_data.clks = clk;
1476fe5aeb5SVladimir Zapolskiy clk_data.clk_num = ARRAY_SIZE(clk);
1486fe5aeb5SVladimir Zapolskiy of_clk_add_provider(np, of_clk_src_onecell_get, &clk_data);
14911f68120SShawn Guo }
1506fe5aeb5SVladimir Zapolskiy
1516fe5aeb5SVladimir Zapolskiy CLK_OF_DECLARE(imx31_ccm, "fsl,imx31-ccm", mx31_clocks_init_dt);
152