xref: /openbmc/linux/drivers/clk/imx/clk.c (revision 7effbd18)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/bits.h>
3 #include <linux/clk.h>
4 #include <linux/clk-provider.h>
5 #include <linux/err.h>
6 #include <linux/io.h>
7 #include <linux/module.h>
8 #include <linux/of.h>
9 #include <linux/slab.h>
10 #include <linux/spinlock.h>
11 #include "clk.h"
12 
13 #define CCM_CCDR			0x4
14 #define CCDR_MMDC_CH0_MASK		BIT(17)
15 #define CCDR_MMDC_CH1_MASK		BIT(16)
16 
17 DEFINE_SPINLOCK(imx_ccm_lock);
18 EXPORT_SYMBOL_GPL(imx_ccm_lock);
19 
20 bool mcore_booted;
21 EXPORT_SYMBOL_GPL(mcore_booted);
22 
23 void imx_unregister_clocks(struct clk *clks[], unsigned int count)
24 {
25 	unsigned int i;
26 
27 	for (i = 0; i < count; i++)
28 		clk_unregister(clks[i]);
29 }
30 
31 void imx_unregister_hw_clocks(struct clk_hw *hws[], unsigned int count)
32 {
33 	unsigned int i;
34 
35 	for (i = 0; i < count; i++)
36 		clk_hw_unregister(hws[i]);
37 }
38 EXPORT_SYMBOL_GPL(imx_unregister_hw_clocks);
39 
40 void imx_mmdc_mask_handshake(void __iomem *ccm_base,
41 				    unsigned int chn)
42 {
43 	unsigned int reg;
44 
45 	reg = readl_relaxed(ccm_base + CCM_CCDR);
46 	reg |= chn == 0 ? CCDR_MMDC_CH0_MASK : CCDR_MMDC_CH1_MASK;
47 	writel_relaxed(reg, ccm_base + CCM_CCDR);
48 }
49 
50 void imx_check_clocks(struct clk *clks[], unsigned int count)
51 {
52 	unsigned i;
53 
54 	for (i = 0; i < count; i++)
55 		if (IS_ERR(clks[i]))
56 			pr_err("i.MX clk %u: register failed with %ld\n",
57 			       i, PTR_ERR(clks[i]));
58 }
59 
60 void imx_check_clk_hws(struct clk_hw *clks[], unsigned int count)
61 {
62 	unsigned int i;
63 
64 	for (i = 0; i < count; i++)
65 		if (IS_ERR(clks[i]))
66 			pr_err("i.MX clk %u: register failed with %ld\n",
67 			       i, PTR_ERR(clks[i]));
68 }
69 EXPORT_SYMBOL_GPL(imx_check_clk_hws);
70 
71 static struct clk *imx_obtain_fixed_clock_from_dt(const char *name)
72 {
73 	struct of_phandle_args phandle;
74 	struct clk *clk = ERR_PTR(-ENODEV);
75 	char *path;
76 
77 	path = kasprintf(GFP_KERNEL, "/clocks/%s", name);
78 	if (!path)
79 		return ERR_PTR(-ENOMEM);
80 
81 	phandle.np = of_find_node_by_path(path);
82 	kfree(path);
83 
84 	if (phandle.np) {
85 		clk = of_clk_get_from_provider(&phandle);
86 		of_node_put(phandle.np);
87 	}
88 	return clk;
89 }
90 
91 struct clk *imx_obtain_fixed_clock(
92 			const char *name, unsigned long rate)
93 {
94 	struct clk *clk;
95 
96 	clk = imx_obtain_fixed_clock_from_dt(name);
97 	if (IS_ERR(clk))
98 		clk = imx_clk_fixed(name, rate);
99 	return clk;
100 }
101 
102 struct clk_hw *imx_obtain_fixed_clock_hw(
103 			const char *name, unsigned long rate)
104 {
105 	struct clk *clk;
106 
107 	clk = imx_obtain_fixed_clock_from_dt(name);
108 	if (IS_ERR(clk))
109 		clk = imx_clk_fixed(name, rate);
110 	return __clk_get_hw(clk);
111 }
112 
113 struct clk_hw *imx_obtain_fixed_of_clock(struct device_node *np,
114 					 const char *name, unsigned long rate)
115 {
116 	struct clk *clk = of_clk_get_by_name(np, name);
117 	struct clk_hw *hw;
118 
119 	if (IS_ERR(clk))
120 		hw = imx_obtain_fixed_clock_hw(name, rate);
121 	else
122 		hw = __clk_get_hw(clk);
123 
124 	return hw;
125 }
126 
127 struct clk_hw *imx_get_clk_hw_by_name(struct device_node *np, const char *name)
128 {
129 	struct clk *clk;
130 
131 	clk = of_clk_get_by_name(np, name);
132 	if (IS_ERR(clk))
133 		return ERR_PTR(-ENOENT);
134 
135 	return __clk_get_hw(clk);
136 }
137 EXPORT_SYMBOL_GPL(imx_get_clk_hw_by_name);
138 
139 /*
140  * This fixups the register CCM_CSCMR1 write value.
141  * The write/read/divider values of the aclk_podf field
142  * of that register have the relationship described by
143  * the following table:
144  *
145  * write value       read value        divider
146  * 3b'000            3b'110            7
147  * 3b'001            3b'111            8
148  * 3b'010            3b'100            5
149  * 3b'011            3b'101            6
150  * 3b'100            3b'010            3
151  * 3b'101            3b'011            4
152  * 3b'110            3b'000            1
153  * 3b'111            3b'001            2(default)
154  *
155  * That's why we do the xor operation below.
156  */
157 #define CSCMR1_FIXUP	0x00600000
158 
159 void imx_cscmr1_fixup(u32 *val)
160 {
161 	*val ^= CSCMR1_FIXUP;
162 	return;
163 }
164 
165 #ifndef MODULE
166 
167 static bool imx_keep_uart_clocks;
168 static int imx_enabled_uart_clocks;
169 static struct clk **imx_uart_clocks;
170 
171 static int __init imx_keep_uart_clocks_param(char *str)
172 {
173 	imx_keep_uart_clocks = 1;
174 
175 	return 0;
176 }
177 __setup_param("earlycon", imx_keep_uart_earlycon,
178 	      imx_keep_uart_clocks_param, 0);
179 __setup_param("earlyprintk", imx_keep_uart_earlyprintk,
180 	      imx_keep_uart_clocks_param, 0);
181 
182 void imx_register_uart_clocks(void)
183 {
184 	unsigned int num __maybe_unused;
185 
186 	imx_enabled_uart_clocks = 0;
187 
188 /* i.MX boards use device trees now.  For build tests without CONFIG_OF, do nothing */
189 #ifdef CONFIG_OF
190 	if (imx_keep_uart_clocks) {
191 		int i;
192 
193 		num = of_clk_get_parent_count(of_stdout);
194 		if (!num)
195 			return;
196 
197 		if (!of_stdout)
198 			return;
199 
200 		imx_uart_clocks = kcalloc(num, sizeof(struct clk *), GFP_KERNEL);
201 		if (!imx_uart_clocks)
202 			return;
203 
204 		for (i = 0; i < num; i++) {
205 			imx_uart_clocks[imx_enabled_uart_clocks] = of_clk_get(of_stdout, i);
206 
207 			/* Stop if there are no more of_stdout references */
208 			if (IS_ERR(imx_uart_clocks[imx_enabled_uart_clocks]))
209 				return;
210 
211 			/* Only enable the clock if it's not NULL */
212 			if (imx_uart_clocks[imx_enabled_uart_clocks])
213 				clk_prepare_enable(imx_uart_clocks[imx_enabled_uart_clocks++]);
214 		}
215 	}
216 #endif
217 }
218 
219 static int __init imx_clk_disable_uart(void)
220 {
221 	if (imx_keep_uart_clocks && imx_enabled_uart_clocks) {
222 		int i;
223 
224 		for (i = 0; i < imx_enabled_uart_clocks; i++) {
225 			clk_disable_unprepare(imx_uart_clocks[i]);
226 			clk_put(imx_uart_clocks[i]);
227 		}
228 	}
229 
230 	kfree(imx_uart_clocks);
231 
232 	return 0;
233 }
234 late_initcall_sync(imx_clk_disable_uart);
235 #endif
236 
237 MODULE_LICENSE("GPL v2");
238