162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Copyright 2012 Freescale Semiconductor, Inc.
462306a36Sopenharmony_ci */
562306a36Sopenharmony_ci
662306a36Sopenharmony_ci#include <linux/clk/mxs.h>
762306a36Sopenharmony_ci#include <linux/clk.h>
862306a36Sopenharmony_ci#include <linux/clk-provider.h>
962306a36Sopenharmony_ci#include <linux/err.h>
1062306a36Sopenharmony_ci#include <linux/init.h>
1162306a36Sopenharmony_ci#include <linux/io.h>
1262306a36Sopenharmony_ci#include <linux/of.h>
1362306a36Sopenharmony_ci#include <linux/of_address.h>
1462306a36Sopenharmony_ci#include "clk.h"
1562306a36Sopenharmony_ci
1662306a36Sopenharmony_cistatic void __iomem *clkctrl;
1762306a36Sopenharmony_cistatic void __iomem *digctrl;
1862306a36Sopenharmony_ci
1962306a36Sopenharmony_ci#define CLKCTRL clkctrl
2062306a36Sopenharmony_ci#define DIGCTRL digctrl
2162306a36Sopenharmony_ci
2262306a36Sopenharmony_ci#define PLLCTRL0		(CLKCTRL + 0x0000)
2362306a36Sopenharmony_ci#define CPU			(CLKCTRL + 0x0020)
2462306a36Sopenharmony_ci#define HBUS			(CLKCTRL + 0x0030)
2562306a36Sopenharmony_ci#define XBUS			(CLKCTRL + 0x0040)
2662306a36Sopenharmony_ci#define XTAL			(CLKCTRL + 0x0050)
2762306a36Sopenharmony_ci#define PIX			(CLKCTRL + 0x0060)
2862306a36Sopenharmony_ci#define SSP			(CLKCTRL + 0x0070)
2962306a36Sopenharmony_ci#define GPMI			(CLKCTRL + 0x0080)
3062306a36Sopenharmony_ci#define SPDIF			(CLKCTRL + 0x0090)
3162306a36Sopenharmony_ci#define EMI			(CLKCTRL + 0x00a0)
3262306a36Sopenharmony_ci#define SAIF			(CLKCTRL + 0x00c0)
3362306a36Sopenharmony_ci#define TV			(CLKCTRL + 0x00d0)
3462306a36Sopenharmony_ci#define ETM			(CLKCTRL + 0x00e0)
3562306a36Sopenharmony_ci#define FRAC			(CLKCTRL + 0x00f0)
3662306a36Sopenharmony_ci#define CLKSEQ			(CLKCTRL + 0x0110)
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_ci#define BP_CPU_INTERRUPT_WAIT	12
3962306a36Sopenharmony_ci#define BP_CLKSEQ_BYPASS_SAIF	0
4062306a36Sopenharmony_ci#define BP_CLKSEQ_BYPASS_SSP	5
4162306a36Sopenharmony_ci#define BP_SAIF_DIV_FRAC_EN	16
4262306a36Sopenharmony_ci#define BP_FRAC_IOFRAC		24
4362306a36Sopenharmony_ci
4462306a36Sopenharmony_cistatic void __init clk_misc_init(void)
4562306a36Sopenharmony_ci{
4662306a36Sopenharmony_ci	u32 val;
4762306a36Sopenharmony_ci
4862306a36Sopenharmony_ci	/* Gate off cpu clock in WFI for power saving */
4962306a36Sopenharmony_ci	writel_relaxed(1 << BP_CPU_INTERRUPT_WAIT, CPU + SET);
5062306a36Sopenharmony_ci
5162306a36Sopenharmony_ci	/* Clear BYPASS for SAIF */
5262306a36Sopenharmony_ci	writel_relaxed(1 << BP_CLKSEQ_BYPASS_SAIF, CLKSEQ + CLR);
5362306a36Sopenharmony_ci
5462306a36Sopenharmony_ci	/* SAIF has to use frac div for functional operation */
5562306a36Sopenharmony_ci	val = readl_relaxed(SAIF);
5662306a36Sopenharmony_ci	val |= 1 << BP_SAIF_DIV_FRAC_EN;
5762306a36Sopenharmony_ci	writel_relaxed(val, SAIF);
5862306a36Sopenharmony_ci
5962306a36Sopenharmony_ci	/*
6062306a36Sopenharmony_ci	 * Source ssp clock from ref_io than ref_xtal,
6162306a36Sopenharmony_ci	 * as ref_xtal only provides 24 MHz as maximum.
6262306a36Sopenharmony_ci	 */
6362306a36Sopenharmony_ci	writel_relaxed(1 << BP_CLKSEQ_BYPASS_SSP, CLKSEQ + CLR);
6462306a36Sopenharmony_ci
6562306a36Sopenharmony_ci	/*
6662306a36Sopenharmony_ci	 * 480 MHz seems too high to be ssp clock source directly,
6762306a36Sopenharmony_ci	 * so set frac to get a 288 MHz ref_io.
6862306a36Sopenharmony_ci	 */
6962306a36Sopenharmony_ci	writel_relaxed(0x3f << BP_FRAC_IOFRAC, FRAC + CLR);
7062306a36Sopenharmony_ci	writel_relaxed(30 << BP_FRAC_IOFRAC, FRAC + SET);
7162306a36Sopenharmony_ci}
7262306a36Sopenharmony_ci
7362306a36Sopenharmony_cistatic const char *const sel_pll[]  __initconst = { "pll", "ref_xtal", };
7462306a36Sopenharmony_cistatic const char *const sel_cpu[]  __initconst = { "ref_cpu", "ref_xtal", };
7562306a36Sopenharmony_cistatic const char *const sel_pix[]  __initconst = { "ref_pix", "ref_xtal", };
7662306a36Sopenharmony_cistatic const char *const sel_io[]   __initconst = { "ref_io", "ref_xtal", };
7762306a36Sopenharmony_cistatic const char *const cpu_sels[] __initconst = { "cpu_pll", "cpu_xtal", };
7862306a36Sopenharmony_cistatic const char *const emi_sels[] __initconst = { "emi_pll", "emi_xtal", };
7962306a36Sopenharmony_ci
8062306a36Sopenharmony_cienum imx23_clk {
8162306a36Sopenharmony_ci	ref_xtal, pll, ref_cpu, ref_emi, ref_pix, ref_io, saif_sel,
8262306a36Sopenharmony_ci	lcdif_sel, gpmi_sel, ssp_sel, emi_sel, cpu, etm_sel, cpu_pll,
8362306a36Sopenharmony_ci	cpu_xtal, hbus, xbus, lcdif_div, ssp_div, gpmi_div, emi_pll,
8462306a36Sopenharmony_ci	emi_xtal, etm_div, saif_div, clk32k_div, rtc, adc, spdif_div,
8562306a36Sopenharmony_ci	clk32k, dri, pwm, filt, uart, ssp, gpmi, spdif, emi, saif,
8662306a36Sopenharmony_ci	lcdif, etm, usb, usb_phy,
8762306a36Sopenharmony_ci	clk_max
8862306a36Sopenharmony_ci};
8962306a36Sopenharmony_ci
9062306a36Sopenharmony_cistatic struct clk *clks[clk_max];
9162306a36Sopenharmony_cistatic struct clk_onecell_data clk_data;
9262306a36Sopenharmony_ci
9362306a36Sopenharmony_cistatic enum imx23_clk clks_init_on[] __initdata = {
9462306a36Sopenharmony_ci	cpu, hbus, xbus, emi, uart,
9562306a36Sopenharmony_ci};
9662306a36Sopenharmony_ci
9762306a36Sopenharmony_cistatic void __init mx23_clocks_init(struct device_node *np)
9862306a36Sopenharmony_ci{
9962306a36Sopenharmony_ci	struct device_node *dcnp;
10062306a36Sopenharmony_ci	u32 i;
10162306a36Sopenharmony_ci
10262306a36Sopenharmony_ci	dcnp = of_find_compatible_node(NULL, NULL, "fsl,imx23-digctl");
10362306a36Sopenharmony_ci	digctrl = of_iomap(dcnp, 0);
10462306a36Sopenharmony_ci	WARN_ON(!digctrl);
10562306a36Sopenharmony_ci	of_node_put(dcnp);
10662306a36Sopenharmony_ci
10762306a36Sopenharmony_ci	clkctrl = of_iomap(np, 0);
10862306a36Sopenharmony_ci	WARN_ON(!clkctrl);
10962306a36Sopenharmony_ci
11062306a36Sopenharmony_ci	clk_misc_init();
11162306a36Sopenharmony_ci
11262306a36Sopenharmony_ci	clks[ref_xtal] = mxs_clk_fixed("ref_xtal", 24000000);
11362306a36Sopenharmony_ci	clks[pll] = mxs_clk_pll("pll", "ref_xtal", PLLCTRL0, 16, 480000000);
11462306a36Sopenharmony_ci	clks[ref_cpu] = mxs_clk_ref("ref_cpu", "pll", FRAC, 0);
11562306a36Sopenharmony_ci	clks[ref_emi] = mxs_clk_ref("ref_emi", "pll", FRAC, 1);
11662306a36Sopenharmony_ci	clks[ref_pix] = mxs_clk_ref("ref_pix", "pll", FRAC, 2);
11762306a36Sopenharmony_ci	clks[ref_io] = mxs_clk_ref("ref_io", "pll", FRAC, 3);
11862306a36Sopenharmony_ci	clks[saif_sel] = mxs_clk_mux("saif_sel", CLKSEQ, 0, 1, sel_pll, ARRAY_SIZE(sel_pll));
11962306a36Sopenharmony_ci	clks[lcdif_sel] = mxs_clk_mux("lcdif_sel", CLKSEQ, 1, 1, sel_pix, ARRAY_SIZE(sel_pix));
12062306a36Sopenharmony_ci	clks[gpmi_sel] = mxs_clk_mux("gpmi_sel", CLKSEQ, 4, 1, sel_io, ARRAY_SIZE(sel_io));
12162306a36Sopenharmony_ci	clks[ssp_sel] = mxs_clk_mux("ssp_sel", CLKSEQ, 5, 1, sel_io, ARRAY_SIZE(sel_io));
12262306a36Sopenharmony_ci	clks[emi_sel] = mxs_clk_mux("emi_sel", CLKSEQ, 6, 1, emi_sels, ARRAY_SIZE(emi_sels));
12362306a36Sopenharmony_ci	clks[cpu] = mxs_clk_mux("cpu", CLKSEQ, 7, 1, cpu_sels, ARRAY_SIZE(cpu_sels));
12462306a36Sopenharmony_ci	clks[etm_sel] = mxs_clk_mux("etm_sel", CLKSEQ, 8, 1, sel_cpu, ARRAY_SIZE(sel_cpu));
12562306a36Sopenharmony_ci	clks[cpu_pll] = mxs_clk_div("cpu_pll", "ref_cpu", CPU, 0, 6, 28);
12662306a36Sopenharmony_ci	clks[cpu_xtal] = mxs_clk_div("cpu_xtal", "ref_xtal", CPU, 16, 10, 29);
12762306a36Sopenharmony_ci	clks[hbus] = mxs_clk_div("hbus", "cpu", HBUS, 0, 5, 29);
12862306a36Sopenharmony_ci	clks[xbus] = mxs_clk_div("xbus", "ref_xtal", XBUS, 0, 10, 31);
12962306a36Sopenharmony_ci	clks[lcdif_div] = mxs_clk_div("lcdif_div", "lcdif_sel", PIX, 0, 12, 29);
13062306a36Sopenharmony_ci	clks[ssp_div] = mxs_clk_div("ssp_div", "ssp_sel", SSP, 0, 9, 29);
13162306a36Sopenharmony_ci	clks[gpmi_div] = mxs_clk_div("gpmi_div", "gpmi_sel", GPMI, 0, 10, 29);
13262306a36Sopenharmony_ci	clks[emi_pll] = mxs_clk_div("emi_pll", "ref_emi", EMI, 0, 6, 28);
13362306a36Sopenharmony_ci	clks[emi_xtal] = mxs_clk_div("emi_xtal", "ref_xtal", EMI, 8, 4, 29);
13462306a36Sopenharmony_ci	clks[etm_div] = mxs_clk_div("etm_div", "etm_sel", ETM, 0, 6, 29);
13562306a36Sopenharmony_ci	clks[saif_div] = mxs_clk_frac("saif_div", "saif_sel", SAIF, 0, 16, 29);
13662306a36Sopenharmony_ci	clks[clk32k_div] = mxs_clk_fixed_factor("clk32k_div", "ref_xtal", 1, 750);
13762306a36Sopenharmony_ci	clks[rtc] = mxs_clk_fixed_factor("rtc", "ref_xtal", 1, 768);
13862306a36Sopenharmony_ci	clks[adc] = mxs_clk_fixed_factor("adc", "clk32k", 1, 16);
13962306a36Sopenharmony_ci	clks[spdif_div] = mxs_clk_fixed_factor("spdif_div", "pll", 1, 4);
14062306a36Sopenharmony_ci	clks[clk32k] = mxs_clk_gate("clk32k", "clk32k_div", XTAL, 26);
14162306a36Sopenharmony_ci	clks[dri] = mxs_clk_gate("dri", "ref_xtal", XTAL, 28);
14262306a36Sopenharmony_ci	clks[pwm] = mxs_clk_gate("pwm", "ref_xtal", XTAL, 29);
14362306a36Sopenharmony_ci	clks[filt] = mxs_clk_gate("filt", "ref_xtal", XTAL, 30);
14462306a36Sopenharmony_ci	clks[uart] = mxs_clk_gate("uart", "ref_xtal", XTAL, 31);
14562306a36Sopenharmony_ci	clks[ssp] = mxs_clk_gate("ssp", "ssp_div", SSP, 31);
14662306a36Sopenharmony_ci	clks[gpmi] = mxs_clk_gate("gpmi", "gpmi_div", GPMI, 31);
14762306a36Sopenharmony_ci	clks[spdif] = mxs_clk_gate("spdif", "spdif_div", SPDIF, 31);
14862306a36Sopenharmony_ci	clks[emi] = mxs_clk_gate("emi", "emi_sel", EMI, 31);
14962306a36Sopenharmony_ci	clks[saif] = mxs_clk_gate("saif", "saif_div", SAIF, 31);
15062306a36Sopenharmony_ci	clks[lcdif] = mxs_clk_gate("lcdif", "lcdif_div", PIX, 31);
15162306a36Sopenharmony_ci	clks[etm] = mxs_clk_gate("etm", "etm_div", ETM, 31);
15262306a36Sopenharmony_ci	clks[usb] = mxs_clk_gate("usb", "usb_phy", DIGCTRL, 2);
15362306a36Sopenharmony_ci	clks[usb_phy] = clk_register_gate(NULL, "usb_phy", "pll", 0, PLLCTRL0, 18, 0, &mxs_lock);
15462306a36Sopenharmony_ci
15562306a36Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(clks); i++)
15662306a36Sopenharmony_ci		if (IS_ERR(clks[i])) {
15762306a36Sopenharmony_ci			pr_err("i.MX23 clk %d: register failed with %ld\n",
15862306a36Sopenharmony_ci				i, PTR_ERR(clks[i]));
15962306a36Sopenharmony_ci			return;
16062306a36Sopenharmony_ci		}
16162306a36Sopenharmony_ci
16262306a36Sopenharmony_ci	clk_data.clks = clks;
16362306a36Sopenharmony_ci	clk_data.clk_num = ARRAY_SIZE(clks);
16462306a36Sopenharmony_ci	of_clk_add_provider(np, of_clk_src_onecell_get, &clk_data);
16562306a36Sopenharmony_ci
16662306a36Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(clks_init_on); i++)
16762306a36Sopenharmony_ci		clk_prepare_enable(clks[clks_init_on[i]]);
16862306a36Sopenharmony_ci
16962306a36Sopenharmony_ci}
17062306a36Sopenharmony_ciCLK_OF_DECLARE(imx23_clkctrl, "fsl,imx23-clkctrl", mx23_clocks_init);
171