1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * PLL clock driver for Keystone devices
5 * Copyright (C) 2013 Texas Instruments Inc.
6 * Murali Karicheri <m-karicheri2@ti.com>
7 * Santosh Shilimkar <santosh.shilimkar@ti.com>
9 #include <linux/clk-provider.h>
10 #include <linux/err.h>
12 #include <linux/slab.h>
13 #include <linux/of_address.h>
15 #include <linux/module.h>
17 #define PLLM_LOW_MASK 0x3f
18 #define PLLM_HIGH_MASK 0x7ffc0
19 #define MAIN_PLLM_HIGH_MASK 0x7f000
20 #define PLLM_HIGH_SHIFT 6
21 #define PLLD_MASK 0x3f
22 #define CLKOD_MASK 0x780000
23 #define CLKOD_SHIFT 19
26 * struct clk_pll_data - pll data structure
27 * @has_pllctrl: If set to non zero, lower 6 bits of multiplier is in pllm
28 * register of pll controller, else it is in the pll_ctrl0((bit 11-6)
29 * @phy_pllm: Physical address of PLLM in pll controller. Used when
30 * has_pllctrl is non zero.
31 * @phy_pll_ctl0: Physical address of PLL ctrl0. This could be that of
32 * Main PLL or any other PLLs in the device such as ARM PLL, DDR PLL
33 * or PA PLL available on keystone2. These PLLs are controlled by
34 * this register. Main PLL is controlled by a PLL controller.
35 * @pllm: PLL register map address for multiplier bits
36 * @pllod: PLL register map address for post divider bits
37 * @pll_ctl0: PLL controller map address
38 * @pllm_lower_mask: multiplier lower mask
39 * @pllm_upper_mask: multiplier upper mask
40 * @pllm_upper_shift: multiplier upper shift
41 * @plld_mask: divider mask
42 * @clkod_mask: output divider mask
43 * @clkod_shift: output divider shift
44 * @plld_mask: divider mask
45 * @postdiv: Fixed post divider
53 void __iomem
*pll_ctl0
;
64 * struct clk_pll - Main pll clock
65 * @hw: clk_hw for the pll
66 * @pll_data: PLL driver specific data
70 struct clk_pll_data
*pll_data
;
73 #define to_clk_pll(_hw) container_of(_hw, struct clk_pll, hw)
75 static unsigned long clk_pllclk_recalc(struct clk_hw
*hw
,
76 unsigned long parent_rate
)
78 struct clk_pll
*pll
= to_clk_pll(hw
);
79 struct clk_pll_data
*pll_data
= pll
->pll_data
;
80 unsigned long rate
= parent_rate
;
81 u32 mult
= 0, prediv
, postdiv
, val
;
84 * get bits 0-5 of multiplier from pllctrl PLLM register
85 * if has_pllctrl is non zero
87 if (pll_data
->has_pllctrl
) {
88 val
= readl(pll_data
->pllm
);
89 mult
= (val
& pll_data
->pllm_lower_mask
);
92 /* bit6-12 of PLLM is in Main PLL control register */
93 val
= readl(pll_data
->pll_ctl0
);
94 mult
|= ((val
& pll_data
->pllm_upper_mask
)
95 >> pll_data
->pllm_upper_shift
);
96 prediv
= (val
& pll_data
->plld_mask
);
98 if (!pll_data
->has_pllctrl
)
99 /* read post divider from od bits*/
100 postdiv
= ((val
& pll_data
->clkod_mask
) >>
101 pll_data
->clkod_shift
) + 1;
102 else if (pll_data
->pllod
) {
103 postdiv
= readl(pll_data
->pllod
);
104 postdiv
= ((postdiv
& pll_data
->clkod_mask
) >>
105 pll_data
->clkod_shift
) + 1;
107 postdiv
= pll_data
->postdiv
;
109 rate
/= (prediv
+ 1);
110 rate
= (rate
* (mult
+ 1));
116 static const struct clk_ops clk_pll_ops
= {
117 .recalc_rate
= clk_pllclk_recalc
,
120 static struct clk
*clk_register_pll(struct device
*dev
,
122 const char *parent_name
,
123 struct clk_pll_data
*pll_data
)
125 struct clk_init_data init
;
129 pll
= kzalloc(sizeof(*pll
), GFP_KERNEL
);
131 return ERR_PTR(-ENOMEM
);
134 init
.ops
= &clk_pll_ops
;
136 init
.parent_names
= (parent_name
? &parent_name
: NULL
);
137 init
.num_parents
= (parent_name
? 1 : 0);
139 pll
->pll_data
= pll_data
;
140 pll
->hw
.init
= &init
;
142 clk
= clk_register(NULL
, &pll
->hw
);
153 * _of_pll_clk_init - PLL initialisation via DT
154 * @node: device tree node for this clock
155 * @pllctrl: If true, lower 6 bits of multiplier is in pllm register of
156 * pll controller, else it is in the control register0(bit 11-6)
158 static void __init
_of_pll_clk_init(struct device_node
*node
, bool pllctrl
)
160 struct clk_pll_data
*pll_data
;
161 const char *parent_name
;
165 pll_data
= kzalloc(sizeof(*pll_data
), GFP_KERNEL
);
167 pr_err("%s: Out of memory\n", __func__
);
171 parent_name
= of_clk_get_parent_name(node
, 0);
172 if (of_property_read_u32(node
, "fixed-postdiv", &pll_data
->postdiv
)) {
173 /* assume the PLL has output divider register bits */
174 pll_data
->clkod_mask
= CLKOD_MASK
;
175 pll_data
->clkod_shift
= CLKOD_SHIFT
;
178 * Check if there is an post-divider register. If not
179 * assume od bits are part of control register.
181 i
= of_property_match_string(node
, "reg-names",
183 pll_data
->pllod
= of_iomap(node
, i
);
186 i
= of_property_match_string(node
, "reg-names", "control");
187 pll_data
->pll_ctl0
= of_iomap(node
, i
);
188 if (!pll_data
->pll_ctl0
) {
189 pr_err("%s: ioremap failed\n", __func__
);
190 iounmap(pll_data
->pllod
);
194 pll_data
->pllm_lower_mask
= PLLM_LOW_MASK
;
195 pll_data
->pllm_upper_shift
= PLLM_HIGH_SHIFT
;
196 pll_data
->plld_mask
= PLLD_MASK
;
197 pll_data
->has_pllctrl
= pllctrl
;
198 if (!pll_data
->has_pllctrl
) {
199 pll_data
->pllm_upper_mask
= PLLM_HIGH_MASK
;
201 pll_data
->pllm_upper_mask
= MAIN_PLLM_HIGH_MASK
;
202 i
= of_property_match_string(node
, "reg-names", "multiplier");
203 pll_data
->pllm
= of_iomap(node
, i
);
204 if (!pll_data
->pllm
) {
205 iounmap(pll_data
->pll_ctl0
);
206 iounmap(pll_data
->pllod
);
211 clk
= clk_register_pll(NULL
, node
->name
, parent_name
, pll_data
);
213 of_clk_add_provider(node
, of_clk_src_simple_get
, clk
);
218 pr_err("%s: error initializing pll %pOFn\n", __func__
, node
);
223 * of_keystone_pll_clk_init - PLL initialisation DT wrapper
224 * @node: device tree node for this clock
226 static void __init
of_keystone_pll_clk_init(struct device_node
*node
)
228 _of_pll_clk_init(node
, false);
230 CLK_OF_DECLARE(keystone_pll_clock
, "ti,keystone,pll-clock",
231 of_keystone_pll_clk_init
);
234 * of_keystone_main_pll_clk_init - Main PLL initialisation DT wrapper
235 * @node: device tree node for this clock
237 static void __init
of_keystone_main_pll_clk_init(struct device_node
*node
)
239 _of_pll_clk_init(node
, true);
241 CLK_OF_DECLARE(keystone_main_pll_clock
, "ti,keystone,main-pll-clock",
242 of_keystone_main_pll_clk_init
);
245 * of_pll_div_clk_init - PLL divider setup function
246 * @node: device tree node for this clock
248 static void __init
of_pll_div_clk_init(struct device_node
*node
)
250 const char *parent_name
;
254 const char *clk_name
= node
->name
;
256 of_property_read_string(node
, "clock-output-names", &clk_name
);
257 reg
= of_iomap(node
, 0);
259 pr_err("%s: ioremap failed\n", __func__
);
263 parent_name
= of_clk_get_parent_name(node
, 0);
265 pr_err("%s: missing parent clock\n", __func__
);
270 if (of_property_read_u32(node
, "bit-shift", &shift
)) {
271 pr_err("%s: missing 'shift' property\n", __func__
);
276 if (of_property_read_u32(node
, "bit-mask", &mask
)) {
277 pr_err("%s: missing 'bit-mask' property\n", __func__
);
282 clk
= clk_register_divider(NULL
, clk_name
, parent_name
, 0, reg
, shift
,
285 of_clk_add_provider(node
, of_clk_src_simple_get
, clk
);
287 pr_err("%s: error registering divider %s\n", __func__
, clk_name
);
291 CLK_OF_DECLARE(pll_divider_clock
, "ti,keystone,pll-divider-clock", of_pll_div_clk_init
);
294 * of_pll_mux_clk_init - PLL mux setup function
295 * @node: device tree node for this clock
297 static void __init
of_pll_mux_clk_init(struct device_node
*node
)
302 const char *parents
[2];
303 const char *clk_name
= node
->name
;
305 of_property_read_string(node
, "clock-output-names", &clk_name
);
306 reg
= of_iomap(node
, 0);
308 pr_err("%s: ioremap failed\n", __func__
);
312 of_clk_parent_fill(node
, parents
, 2);
313 if (!parents
[0] || !parents
[1]) {
314 pr_err("%s: missing parent clocks\n", __func__
);
318 if (of_property_read_u32(node
, "bit-shift", &shift
)) {
319 pr_err("%s: missing 'shift' property\n", __func__
);
323 if (of_property_read_u32(node
, "bit-mask", &mask
)) {
324 pr_err("%s: missing 'bit-mask' property\n", __func__
);
328 clk
= clk_register_mux(NULL
, clk_name
, (const char **)&parents
,
329 ARRAY_SIZE(parents
) , 0, reg
, shift
, mask
,
332 of_clk_add_provider(node
, of_clk_src_simple_get
, clk
);
334 pr_err("%s: error registering mux %s\n", __func__
, clk_name
);
336 CLK_OF_DECLARE(pll_mux_clock
, "ti,keystone,pll-mux-clock", of_pll_mux_clk_init
);
338 MODULE_LICENSE("GPL");
339 MODULE_DESCRIPTION("PLL clock driver for Keystone devices");
340 MODULE_AUTHOR("Murali Karicheri <m-karicheri2@ti.com>");
341 MODULE_AUTHOR("Santosh Shilimkar <santosh.shilimkar@ti.com>");