1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/clk-provider.h>
7 #include <linux/slab.h>
8 #include <linux/spinlock.h>
12 #define CCDR_MMDC_CH0_MASK BIT(17)
13 #define CCDR_MMDC_CH1_MASK BIT(16)
15 DEFINE_SPINLOCK(imx_ccm_lock);
17 void imx_unregister_clocks(struct clk *clks[], unsigned int count)
21 for (i = 0; i < count; i++)
22 clk_unregister(clks[i]);
25 void imx_unregister_hw_clocks(struct clk_hw *hws[], unsigned int count)
29 for (i = 0; i < count; i++)
30 clk_hw_unregister(hws[i]);
33 void __init imx_mmdc_mask_handshake(void __iomem *ccm_base,
38 reg = readl_relaxed(ccm_base + CCM_CCDR);
39 reg |= chn == 0 ? CCDR_MMDC_CH0_MASK : CCDR_MMDC_CH1_MASK;
40 writel_relaxed(reg, ccm_base + CCM_CCDR);
43 void imx_check_clocks(struct clk *clks[], unsigned int count)
47 for (i = 0; i < count; i++)
49 pr_err("i.MX clk %u: register failed with %ld\n",
53 void imx_check_clk_hws(struct clk_hw *clks[], unsigned int count)
57 for (i = 0; i < count; i++)
59 pr_err("i.MX clk %u: register failed with %ld\n",
63 static struct clk * __init imx_obtain_fixed_clock_from_dt(const char *name)
65 struct of_phandle_args phandle;
66 struct clk *clk = ERR_PTR(-ENODEV);
69 path = kasprintf(GFP_KERNEL, "/clocks/%s", name);
71 return ERR_PTR(-ENOMEM);
73 phandle.np = of_find_node_by_path(path);
77 clk = of_clk_get_from_provider(&phandle);
78 of_node_put(phandle.np);
83 struct clk * __init imx_obtain_fixed_clock(
84 const char *name, unsigned long rate)
88 clk = imx_obtain_fixed_clock_from_dt(name);
90 clk = imx_clk_fixed(name, rate);
94 struct clk_hw * __init imx_obtain_fixed_clock_hw(
95 const char *name, unsigned long rate)
99 clk = imx_obtain_fixed_clock_from_dt(name);
101 clk = imx_clk_fixed(name, rate);
102 return __clk_get_hw(clk);
105 struct clk_hw * imx_obtain_fixed_clk_hw(struct device_node *np,
110 clk = of_clk_get_by_name(np, name);
112 return ERR_PTR(-ENOENT);
114 return __clk_get_hw(clk);
118 * This fixups the register CCM_CSCMR1 write value.
119 * The write/read/divider values of the aclk_podf field
120 * of that register have the relationship described by
121 * the following table:
123 * write value read value divider
131 * 3b'111 3b'001 2(default)
133 * That's why we do the xor operation below.
135 #define CSCMR1_FIXUP 0x00600000
137 void imx_cscmr1_fixup(u32 *val)
139 *val ^= CSCMR1_FIXUP;
143 static int imx_keep_uart_clocks;
144 static struct clk ** const *imx_uart_clocks;
146 static int __init imx_keep_uart_clocks_param(char *str)
148 imx_keep_uart_clocks = 1;
152 __setup_param("earlycon", imx_keep_uart_earlycon,
153 imx_keep_uart_clocks_param, 0);
154 __setup_param("earlyprintk", imx_keep_uart_earlyprintk,
155 imx_keep_uart_clocks_param, 0);
157 void imx_register_uart_clocks(struct clk ** const clks[])
159 if (imx_keep_uart_clocks) {
162 imx_uart_clocks = clks;
163 for (i = 0; imx_uart_clocks[i]; i++)
164 clk_prepare_enable(*imx_uart_clocks[i]);
168 static int __init imx_clk_disable_uart(void)
170 if (imx_keep_uart_clocks && imx_uart_clocks) {
173 for (i = 0; imx_uart_clocks[i]; i++)
174 clk_disable_unprepare(*imx_uart_clocks[i]);
179 late_initcall_sync(imx_clk_disable_uart);