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