18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright 2012 Freescale Semiconductor, Inc.
48c2ecf20Sopenharmony_ci */
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci#include <linux/clk/mxs.h>
78c2ecf20Sopenharmony_ci#include <linux/clk.h>
88c2ecf20Sopenharmony_ci#include <linux/clk-provider.h>
98c2ecf20Sopenharmony_ci#include <linux/err.h>
108c2ecf20Sopenharmony_ci#include <linux/init.h>
118c2ecf20Sopenharmony_ci#include <linux/io.h>
128c2ecf20Sopenharmony_ci#include <linux/of.h>
138c2ecf20Sopenharmony_ci#include <linux/of_address.h>
148c2ecf20Sopenharmony_ci#include "clk.h"
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_cistatic void __iomem *clkctrl;
178c2ecf20Sopenharmony_cistatic void __iomem *digctrl;
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci#define CLKCTRL clkctrl
208c2ecf20Sopenharmony_ci#define DIGCTRL digctrl
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci#define PLLCTRL0		(CLKCTRL + 0x0000)
238c2ecf20Sopenharmony_ci#define CPU			(CLKCTRL + 0x0020)
248c2ecf20Sopenharmony_ci#define HBUS			(CLKCTRL + 0x0030)
258c2ecf20Sopenharmony_ci#define XBUS			(CLKCTRL + 0x0040)
268c2ecf20Sopenharmony_ci#define XTAL			(CLKCTRL + 0x0050)
278c2ecf20Sopenharmony_ci#define PIX			(CLKCTRL + 0x0060)
288c2ecf20Sopenharmony_ci#define SSP			(CLKCTRL + 0x0070)
298c2ecf20Sopenharmony_ci#define GPMI			(CLKCTRL + 0x0080)
308c2ecf20Sopenharmony_ci#define SPDIF			(CLKCTRL + 0x0090)
318c2ecf20Sopenharmony_ci#define EMI			(CLKCTRL + 0x00a0)
328c2ecf20Sopenharmony_ci#define SAIF			(CLKCTRL + 0x00c0)
338c2ecf20Sopenharmony_ci#define TV			(CLKCTRL + 0x00d0)
348c2ecf20Sopenharmony_ci#define ETM			(CLKCTRL + 0x00e0)
358c2ecf20Sopenharmony_ci#define FRAC			(CLKCTRL + 0x00f0)
368c2ecf20Sopenharmony_ci#define CLKSEQ			(CLKCTRL + 0x0110)
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci#define BP_CPU_INTERRUPT_WAIT	12
398c2ecf20Sopenharmony_ci#define BP_CLKSEQ_BYPASS_SAIF	0
408c2ecf20Sopenharmony_ci#define BP_CLKSEQ_BYPASS_SSP	5
418c2ecf20Sopenharmony_ci#define BP_SAIF_DIV_FRAC_EN	16
428c2ecf20Sopenharmony_ci#define BP_FRAC_IOFRAC		24
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_cistatic void __init clk_misc_init(void)
458c2ecf20Sopenharmony_ci{
468c2ecf20Sopenharmony_ci	u32 val;
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci	/* Gate off cpu clock in WFI for power saving */
498c2ecf20Sopenharmony_ci	writel_relaxed(1 << BP_CPU_INTERRUPT_WAIT, CPU + SET);
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci	/* Clear BYPASS for SAIF */
528c2ecf20Sopenharmony_ci	writel_relaxed(1 << BP_CLKSEQ_BYPASS_SAIF, CLKSEQ + CLR);
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci	/* SAIF has to use frac div for functional operation */
558c2ecf20Sopenharmony_ci	val = readl_relaxed(SAIF);
568c2ecf20Sopenharmony_ci	val |= 1 << BP_SAIF_DIV_FRAC_EN;
578c2ecf20Sopenharmony_ci	writel_relaxed(val, SAIF);
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci	/*
608c2ecf20Sopenharmony_ci	 * Source ssp clock from ref_io than ref_xtal,
618c2ecf20Sopenharmony_ci	 * as ref_xtal only provides 24 MHz as maximum.
628c2ecf20Sopenharmony_ci	 */
638c2ecf20Sopenharmony_ci	writel_relaxed(1 << BP_CLKSEQ_BYPASS_SSP, CLKSEQ + CLR);
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci	/*
668c2ecf20Sopenharmony_ci	 * 480 MHz seems too high to be ssp clock source directly,
678c2ecf20Sopenharmony_ci	 * so set frac to get a 288 MHz ref_io.
688c2ecf20Sopenharmony_ci	 */
698c2ecf20Sopenharmony_ci	writel_relaxed(0x3f << BP_FRAC_IOFRAC, FRAC + CLR);
708c2ecf20Sopenharmony_ci	writel_relaxed(30 << BP_FRAC_IOFRAC, FRAC + SET);
718c2ecf20Sopenharmony_ci}
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_cistatic const char *const sel_pll[]  __initconst = { "pll", "ref_xtal", };
748c2ecf20Sopenharmony_cistatic const char *const sel_cpu[]  __initconst = { "ref_cpu", "ref_xtal", };
758c2ecf20Sopenharmony_cistatic const char *const sel_pix[]  __initconst = { "ref_pix", "ref_xtal", };
768c2ecf20Sopenharmony_cistatic const char *const sel_io[]   __initconst = { "ref_io", "ref_xtal", };
778c2ecf20Sopenharmony_cistatic const char *const cpu_sels[] __initconst = { "cpu_pll", "cpu_xtal", };
788c2ecf20Sopenharmony_cistatic const char *const emi_sels[] __initconst = { "emi_pll", "emi_xtal", };
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_cienum imx23_clk {
818c2ecf20Sopenharmony_ci	ref_xtal, pll, ref_cpu, ref_emi, ref_pix, ref_io, saif_sel,
828c2ecf20Sopenharmony_ci	lcdif_sel, gpmi_sel, ssp_sel, emi_sel, cpu, etm_sel, cpu_pll,
838c2ecf20Sopenharmony_ci	cpu_xtal, hbus, xbus, lcdif_div, ssp_div, gpmi_div, emi_pll,
848c2ecf20Sopenharmony_ci	emi_xtal, etm_div, saif_div, clk32k_div, rtc, adc, spdif_div,
858c2ecf20Sopenharmony_ci	clk32k, dri, pwm, filt, uart, ssp, gpmi, spdif, emi, saif,
868c2ecf20Sopenharmony_ci	lcdif, etm, usb, usb_phy,
878c2ecf20Sopenharmony_ci	clk_max
888c2ecf20Sopenharmony_ci};
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_cistatic struct clk *clks[clk_max];
918c2ecf20Sopenharmony_cistatic struct clk_onecell_data clk_data;
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_cistatic enum imx23_clk clks_init_on[] __initdata = {
948c2ecf20Sopenharmony_ci	cpu, hbus, xbus, emi, uart,
958c2ecf20Sopenharmony_ci};
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_cistatic void __init mx23_clocks_init(struct device_node *np)
988c2ecf20Sopenharmony_ci{
998c2ecf20Sopenharmony_ci	struct device_node *dcnp;
1008c2ecf20Sopenharmony_ci	u32 i;
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci	dcnp = of_find_compatible_node(NULL, NULL, "fsl,imx23-digctl");
1038c2ecf20Sopenharmony_ci	digctrl = of_iomap(dcnp, 0);
1048c2ecf20Sopenharmony_ci	WARN_ON(!digctrl);
1058c2ecf20Sopenharmony_ci	of_node_put(dcnp);
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci	clkctrl = of_iomap(np, 0);
1088c2ecf20Sopenharmony_ci	WARN_ON(!clkctrl);
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci	clk_misc_init();
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci	clks[ref_xtal] = mxs_clk_fixed("ref_xtal", 24000000);
1138c2ecf20Sopenharmony_ci	clks[pll] = mxs_clk_pll("pll", "ref_xtal", PLLCTRL0, 16, 480000000);
1148c2ecf20Sopenharmony_ci	clks[ref_cpu] = mxs_clk_ref("ref_cpu", "pll", FRAC, 0);
1158c2ecf20Sopenharmony_ci	clks[ref_emi] = mxs_clk_ref("ref_emi", "pll", FRAC, 1);
1168c2ecf20Sopenharmony_ci	clks[ref_pix] = mxs_clk_ref("ref_pix", "pll", FRAC, 2);
1178c2ecf20Sopenharmony_ci	clks[ref_io] = mxs_clk_ref("ref_io", "pll", FRAC, 3);
1188c2ecf20Sopenharmony_ci	clks[saif_sel] = mxs_clk_mux("saif_sel", CLKSEQ, 0, 1, sel_pll, ARRAY_SIZE(sel_pll));
1198c2ecf20Sopenharmony_ci	clks[lcdif_sel] = mxs_clk_mux("lcdif_sel", CLKSEQ, 1, 1, sel_pix, ARRAY_SIZE(sel_pix));
1208c2ecf20Sopenharmony_ci	clks[gpmi_sel] = mxs_clk_mux("gpmi_sel", CLKSEQ, 4, 1, sel_io, ARRAY_SIZE(sel_io));
1218c2ecf20Sopenharmony_ci	clks[ssp_sel] = mxs_clk_mux("ssp_sel", CLKSEQ, 5, 1, sel_io, ARRAY_SIZE(sel_io));
1228c2ecf20Sopenharmony_ci	clks[emi_sel] = mxs_clk_mux("emi_sel", CLKSEQ, 6, 1, emi_sels, ARRAY_SIZE(emi_sels));
1238c2ecf20Sopenharmony_ci	clks[cpu] = mxs_clk_mux("cpu", CLKSEQ, 7, 1, cpu_sels, ARRAY_SIZE(cpu_sels));
1248c2ecf20Sopenharmony_ci	clks[etm_sel] = mxs_clk_mux("etm_sel", CLKSEQ, 8, 1, sel_cpu, ARRAY_SIZE(sel_cpu));
1258c2ecf20Sopenharmony_ci	clks[cpu_pll] = mxs_clk_div("cpu_pll", "ref_cpu", CPU, 0, 6, 28);
1268c2ecf20Sopenharmony_ci	clks[cpu_xtal] = mxs_clk_div("cpu_xtal", "ref_xtal", CPU, 16, 10, 29);
1278c2ecf20Sopenharmony_ci	clks[hbus] = mxs_clk_div("hbus", "cpu", HBUS, 0, 5, 29);
1288c2ecf20Sopenharmony_ci	clks[xbus] = mxs_clk_div("xbus", "ref_xtal", XBUS, 0, 10, 31);
1298c2ecf20Sopenharmony_ci	clks[lcdif_div] = mxs_clk_div("lcdif_div", "lcdif_sel", PIX, 0, 12, 29);
1308c2ecf20Sopenharmony_ci	clks[ssp_div] = mxs_clk_div("ssp_div", "ssp_sel", SSP, 0, 9, 29);
1318c2ecf20Sopenharmony_ci	clks[gpmi_div] = mxs_clk_div("gpmi_div", "gpmi_sel", GPMI, 0, 10, 29);
1328c2ecf20Sopenharmony_ci	clks[emi_pll] = mxs_clk_div("emi_pll", "ref_emi", EMI, 0, 6, 28);
1338c2ecf20Sopenharmony_ci	clks[emi_xtal] = mxs_clk_div("emi_xtal", "ref_xtal", EMI, 8, 4, 29);
1348c2ecf20Sopenharmony_ci	clks[etm_div] = mxs_clk_div("etm_div", "etm_sel", ETM, 0, 6, 29);
1358c2ecf20Sopenharmony_ci	clks[saif_div] = mxs_clk_frac("saif_div", "saif_sel", SAIF, 0, 16, 29);
1368c2ecf20Sopenharmony_ci	clks[clk32k_div] = mxs_clk_fixed_factor("clk32k_div", "ref_xtal", 1, 750);
1378c2ecf20Sopenharmony_ci	clks[rtc] = mxs_clk_fixed_factor("rtc", "ref_xtal", 1, 768);
1388c2ecf20Sopenharmony_ci	clks[adc] = mxs_clk_fixed_factor("adc", "clk32k", 1, 16);
1398c2ecf20Sopenharmony_ci	clks[spdif_div] = mxs_clk_fixed_factor("spdif_div", "pll", 1, 4);
1408c2ecf20Sopenharmony_ci	clks[clk32k] = mxs_clk_gate("clk32k", "clk32k_div", XTAL, 26);
1418c2ecf20Sopenharmony_ci	clks[dri] = mxs_clk_gate("dri", "ref_xtal", XTAL, 28);
1428c2ecf20Sopenharmony_ci	clks[pwm] = mxs_clk_gate("pwm", "ref_xtal", XTAL, 29);
1438c2ecf20Sopenharmony_ci	clks[filt] = mxs_clk_gate("filt", "ref_xtal", XTAL, 30);
1448c2ecf20Sopenharmony_ci	clks[uart] = mxs_clk_gate("uart", "ref_xtal", XTAL, 31);
1458c2ecf20Sopenharmony_ci	clks[ssp] = mxs_clk_gate("ssp", "ssp_div", SSP, 31);
1468c2ecf20Sopenharmony_ci	clks[gpmi] = mxs_clk_gate("gpmi", "gpmi_div", GPMI, 31);
1478c2ecf20Sopenharmony_ci	clks[spdif] = mxs_clk_gate("spdif", "spdif_div", SPDIF, 31);
1488c2ecf20Sopenharmony_ci	clks[emi] = mxs_clk_gate("emi", "emi_sel", EMI, 31);
1498c2ecf20Sopenharmony_ci	clks[saif] = mxs_clk_gate("saif", "saif_div", SAIF, 31);
1508c2ecf20Sopenharmony_ci	clks[lcdif] = mxs_clk_gate("lcdif", "lcdif_div", PIX, 31);
1518c2ecf20Sopenharmony_ci	clks[etm] = mxs_clk_gate("etm", "etm_div", ETM, 31);
1528c2ecf20Sopenharmony_ci	clks[usb] = mxs_clk_gate("usb", "usb_phy", DIGCTRL, 2);
1538c2ecf20Sopenharmony_ci	clks[usb_phy] = clk_register_gate(NULL, "usb_phy", "pll", 0, PLLCTRL0, 18, 0, &mxs_lock);
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(clks); i++)
1568c2ecf20Sopenharmony_ci		if (IS_ERR(clks[i])) {
1578c2ecf20Sopenharmony_ci			pr_err("i.MX23 clk %d: register failed with %ld\n",
1588c2ecf20Sopenharmony_ci				i, PTR_ERR(clks[i]));
1598c2ecf20Sopenharmony_ci			return;
1608c2ecf20Sopenharmony_ci		}
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ci	clk_data.clks = clks;
1638c2ecf20Sopenharmony_ci	clk_data.clk_num = ARRAY_SIZE(clks);
1648c2ecf20Sopenharmony_ci	of_clk_add_provider(np, of_clk_src_onecell_get, &clk_data);
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(clks_init_on); i++)
1678c2ecf20Sopenharmony_ci		clk_prepare_enable(clks[clks_init_on[i]]);
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci}
1708c2ecf20Sopenharmony_ciCLK_OF_DECLARE(imx23_clkctrl, "fsl,imx23-clkctrl", mx23_clocks_init);
171