2 * clk-xgene.c - AppliedMicro X-Gene Clock Interface
4 * Copyright (c) 2013, Applied Micro Circuits Corporation
5 * Author: Loc Ho <lho@apm.com>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of
10 * the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
23 #include <linux/module.h>
24 #include <linux/spinlock.h>
27 #include <linux/clkdev.h>
28 #include <linux/clk-provider.h>
29 #include <linux/of_address.h>
30 #include <asm/setup.h>
32 /* Register SCU_PCPPLL bit fields */
33 #define N_DIV_RD(src) (((src) & 0x000001ff))
35 /* Register SCU_SOCPLL bit fields */
36 #define CLKR_RD(src) (((src) & 0x07000000)>>24)
37 #define CLKOD_RD(src) (((src) & 0x00300000)>>20)
38 #define REGSPEC_RESET_F1_MASK 0x00010000
39 #define CLKF_RD(src) (((src) & 0x000001ff))
41 #define XGENE_CLK_DRIVER_VER "0.1"
43 static DEFINE_SPINLOCK(clk_lock
);
45 static inline u32
xgene_clk_read(void __iomem
*csr
)
47 return readl_relaxed(csr
);
50 static inline void xgene_clk_write(u32 data
, void __iomem
*csr
)
52 return writel_relaxed(data
, csr
);
61 struct xgene_clk_pll
{
67 enum xgene_pll_type type
;
70 #define to_xgene_clk_pll(_hw) container_of(_hw, struct xgene_clk_pll, hw)
72 static int xgene_clk_pll_is_enabled(struct clk_hw
*hw
)
74 struct xgene_clk_pll
*pllclk
= to_xgene_clk_pll(hw
);
77 data
= xgene_clk_read(pllclk
->reg
+ pllclk
->pll_offset
);
78 pr_debug("%s pll %s\n", pllclk
->name
,
79 data
& REGSPEC_RESET_F1_MASK
? "disabled" : "enabled");
81 return data
& REGSPEC_RESET_F1_MASK
? 0 : 1;
84 static unsigned long xgene_clk_pll_recalc_rate(struct clk_hw
*hw
,
85 unsigned long parent_rate
)
87 struct xgene_clk_pll
*pllclk
= to_xgene_clk_pll(hw
);
95 pll
= xgene_clk_read(pllclk
->reg
+ pllclk
->pll_offset
);
97 if (pllclk
->type
== PLL_TYPE_PCP
) {
99 * PLL VCO = Reference clock * NF
100 * PCP PLL = PLL_VCO / 2
103 fvco
= parent_rate
* (N_DIV_RD(pll
) + 4);
106 * Fref = Reference Clock / NREF;
108 * Fout = Fvco / NOUT;
110 nref
= CLKR_RD(pll
) + 1;
111 nout
= CLKOD_RD(pll
) + 1;
113 fref
= parent_rate
/ nref
;
116 pr_debug("%s pll recalc rate %ld parent %ld\n", pllclk
->name
,
117 fvco
/ nout
, parent_rate
);
122 static const struct clk_ops xgene_clk_pll_ops
= {
123 .is_enabled
= xgene_clk_pll_is_enabled
,
124 .recalc_rate
= xgene_clk_pll_recalc_rate
,
127 static struct clk
*xgene_register_clk_pll(struct device
*dev
,
128 const char *name
, const char *parent_name
,
129 unsigned long flags
, void __iomem
*reg
, u32 pll_offset
,
130 u32 type
, spinlock_t
*lock
)
132 struct xgene_clk_pll
*apmclk
;
134 struct clk_init_data init
;
136 /* allocate the APM clock structure */
137 apmclk
= kzalloc(sizeof(*apmclk
), GFP_KERNEL
);
139 pr_err("%s: could not allocate APM clk\n", __func__
);
140 return ERR_PTR(-ENOMEM
);
144 init
.ops
= &xgene_clk_pll_ops
;
146 init
.parent_names
= parent_name
? &parent_name
: NULL
;
147 init
.num_parents
= parent_name
? 1 : 0;
152 apmclk
->pll_offset
= pll_offset
;
154 apmclk
->hw
.init
= &init
;
156 /* Register the clock */
157 clk
= clk_register(dev
, &apmclk
->hw
);
159 pr_err("%s: could not register clk %s\n", __func__
, name
);
166 static void xgene_pllclk_init(struct device_node
*np
, enum xgene_pll_type pll_type
)
168 const char *clk_name
= np
->full_name
;
172 reg
= of_iomap(np
, 0);
174 pr_err("Unable to map CSR register for %s\n", np
->full_name
);
177 of_property_read_string(np
, "clock-output-names", &clk_name
);
178 clk
= xgene_register_clk_pll(NULL
,
179 clk_name
, of_clk_get_parent_name(np
, 0),
180 CLK_IS_ROOT
, reg
, 0, pll_type
, &clk_lock
);
182 of_clk_add_provider(np
, of_clk_src_simple_get
, clk
);
183 clk_register_clkdev(clk
, clk_name
, NULL
);
184 pr_debug("Add %s clock PLL\n", clk_name
);
188 static void xgene_socpllclk_init(struct device_node
*np
)
190 xgene_pllclk_init(np
, PLL_TYPE_SOC
);
193 static void xgene_pcppllclk_init(struct device_node
*np
)
195 xgene_pllclk_init(np
, PLL_TYPE_PCP
);
199 struct xgene_dev_parameters
{
200 void __iomem
*csr_reg
; /* CSR for IP clock */
201 u32 reg_clk_offset
; /* Offset to clock enable CSR */
202 u32 reg_clk_mask
; /* Mask bit for clock enable */
203 u32 reg_csr_offset
; /* Offset to CSR reset */
204 u32 reg_csr_mask
; /* Mask bit for disable CSR reset */
205 void __iomem
*divider_reg
; /* CSR for divider */
206 u32 reg_divider_offset
; /* Offset to divider register */
207 u32 reg_divider_shift
; /* Bit shift to divider field */
208 u32 reg_divider_width
; /* Width of the bit to divider field */
215 struct xgene_dev_parameters param
;
218 #define to_xgene_clk(_hw) container_of(_hw, struct xgene_clk, hw)
220 static int xgene_clk_enable(struct clk_hw
*hw
)
222 struct xgene_clk
*pclk
= to_xgene_clk(hw
);
223 unsigned long flags
= 0;
228 spin_lock_irqsave(pclk
->lock
, flags
);
230 if (pclk
->param
.csr_reg
!= NULL
) {
231 pr_debug("%s clock enabled\n", pclk
->name
);
232 reg
= __pa(pclk
->param
.csr_reg
);
233 /* First enable the clock */
234 data
= xgene_clk_read(pclk
->param
.csr_reg
+
235 pclk
->param
.reg_clk_offset
);
236 data
|= pclk
->param
.reg_clk_mask
;
237 xgene_clk_write(data
, pclk
->param
.csr_reg
+
238 pclk
->param
.reg_clk_offset
);
239 pr_debug("%s clock PADDR base %pa clk offset 0x%08X mask 0x%08X value 0x%08X\n",
241 pclk
->param
.reg_clk_offset
, pclk
->param
.reg_clk_mask
,
244 /* Second enable the CSR */
245 data
= xgene_clk_read(pclk
->param
.csr_reg
+
246 pclk
->param
.reg_csr_offset
);
247 data
&= ~pclk
->param
.reg_csr_mask
;
248 xgene_clk_write(data
, pclk
->param
.csr_reg
+
249 pclk
->param
.reg_csr_offset
);
250 pr_debug("%s CSR RESET PADDR base %pa csr offset 0x%08X mask 0x%08X value 0x%08X\n",
252 pclk
->param
.reg_csr_offset
, pclk
->param
.reg_csr_mask
,
257 spin_unlock_irqrestore(pclk
->lock
, flags
);
262 static void xgene_clk_disable(struct clk_hw
*hw
)
264 struct xgene_clk
*pclk
= to_xgene_clk(hw
);
265 unsigned long flags
= 0;
269 spin_lock_irqsave(pclk
->lock
, flags
);
271 if (pclk
->param
.csr_reg
!= NULL
) {
272 pr_debug("%s clock disabled\n", pclk
->name
);
273 /* First put the CSR in reset */
274 data
= xgene_clk_read(pclk
->param
.csr_reg
+
275 pclk
->param
.reg_csr_offset
);
276 data
|= pclk
->param
.reg_csr_mask
;
277 xgene_clk_write(data
, pclk
->param
.csr_reg
+
278 pclk
->param
.reg_csr_offset
);
280 /* Second disable the clock */
281 data
= xgene_clk_read(pclk
->param
.csr_reg
+
282 pclk
->param
.reg_clk_offset
);
283 data
&= ~pclk
->param
.reg_clk_mask
;
284 xgene_clk_write(data
, pclk
->param
.csr_reg
+
285 pclk
->param
.reg_clk_offset
);
289 spin_unlock_irqrestore(pclk
->lock
, flags
);
292 static int xgene_clk_is_enabled(struct clk_hw
*hw
)
294 struct xgene_clk
*pclk
= to_xgene_clk(hw
);
297 if (pclk
->param
.csr_reg
!= NULL
) {
298 pr_debug("%s clock checking\n", pclk
->name
);
299 data
= xgene_clk_read(pclk
->param
.csr_reg
+
300 pclk
->param
.reg_clk_offset
);
301 pr_debug("%s clock is %s\n", pclk
->name
,
302 data
& pclk
->param
.reg_clk_mask
? "enabled" :
306 if (pclk
->param
.csr_reg
== NULL
)
308 return data
& pclk
->param
.reg_clk_mask
? 1 : 0;
311 static unsigned long xgene_clk_recalc_rate(struct clk_hw
*hw
,
312 unsigned long parent_rate
)
314 struct xgene_clk
*pclk
= to_xgene_clk(hw
);
317 if (pclk
->param
.divider_reg
) {
318 data
= xgene_clk_read(pclk
->param
.divider_reg
+
319 pclk
->param
.reg_divider_offset
);
320 data
>>= pclk
->param
.reg_divider_shift
;
321 data
&= (1 << pclk
->param
.reg_divider_width
) - 1;
323 pr_debug("%s clock recalc rate %ld parent %ld\n",
324 pclk
->name
, parent_rate
/ data
, parent_rate
);
325 return parent_rate
/ data
;
327 pr_debug("%s clock recalc rate %ld parent %ld\n",
328 pclk
->name
, parent_rate
, parent_rate
);
333 static int xgene_clk_set_rate(struct clk_hw
*hw
, unsigned long rate
,
334 unsigned long parent_rate
)
336 struct xgene_clk
*pclk
= to_xgene_clk(hw
);
337 unsigned long flags
= 0;
343 spin_lock_irqsave(pclk
->lock
, flags
);
345 if (pclk
->param
.divider_reg
) {
346 /* Let's compute the divider */
347 if (rate
> parent_rate
)
349 divider_save
= divider
= parent_rate
/ rate
; /* Rounded down */
350 divider
&= (1 << pclk
->param
.reg_divider_width
) - 1;
351 divider
<<= pclk
->param
.reg_divider_shift
;
353 /* Set new divider */
354 data
= xgene_clk_read(pclk
->param
.divider_reg
+
355 pclk
->param
.reg_divider_offset
);
356 data
&= ~((1 << pclk
->param
.reg_divider_width
) - 1);
358 xgene_clk_write(data
, pclk
->param
.divider_reg
+
359 pclk
->param
.reg_divider_offset
);
360 pr_debug("%s clock set rate %ld\n", pclk
->name
,
361 parent_rate
/ divider_save
);
367 spin_unlock_irqrestore(pclk
->lock
, flags
);
369 return parent_rate
/ divider_save
;
372 static long xgene_clk_round_rate(struct clk_hw
*hw
, unsigned long rate
,
373 unsigned long *prate
)
375 struct xgene_clk
*pclk
= to_xgene_clk(hw
);
376 unsigned long parent_rate
= *prate
;
379 if (pclk
->param
.divider_reg
) {
380 /* Let's compute the divider */
381 if (rate
> parent_rate
)
383 divider
= parent_rate
/ rate
; /* Rounded down */
388 return parent_rate
/ divider
;
391 static const struct clk_ops xgene_clk_ops
= {
392 .enable
= xgene_clk_enable
,
393 .disable
= xgene_clk_disable
,
394 .is_enabled
= xgene_clk_is_enabled
,
395 .recalc_rate
= xgene_clk_recalc_rate
,
396 .set_rate
= xgene_clk_set_rate
,
397 .round_rate
= xgene_clk_round_rate
,
400 static struct clk
*xgene_register_clk(struct device
*dev
,
401 const char *name
, const char *parent_name
,
402 struct xgene_dev_parameters
*parameters
, spinlock_t
*lock
)
404 struct xgene_clk
*apmclk
;
406 struct clk_init_data init
;
409 /* allocate the APM clock structure */
410 apmclk
= kzalloc(sizeof(*apmclk
), GFP_KERNEL
);
412 pr_err("%s: could not allocate APM clk\n", __func__
);
413 return ERR_PTR(-ENOMEM
);
417 init
.ops
= &xgene_clk_ops
;
419 init
.parent_names
= parent_name
? &parent_name
: NULL
;
420 init
.num_parents
= parent_name
? 1 : 0;
424 apmclk
->hw
.init
= &init
;
425 apmclk
->param
= *parameters
;
427 /* Register the clock */
428 clk
= clk_register(dev
, &apmclk
->hw
);
430 pr_err("%s: could not register clk %s\n", __func__
, name
);
435 /* Register the clock for lookup */
436 rc
= clk_register_clkdev(clk
, name
, NULL
);
438 pr_err("%s: could not register lookup clk %s\n",
444 static void __init
xgene_devclk_init(struct device_node
*np
)
446 const char *clk_name
= np
->full_name
;
450 struct xgene_dev_parameters parameters
;
453 /* Check if the entry is disabled */
454 if (!of_device_is_available(np
))
457 /* Parse the DTS register for resource */
458 parameters
.csr_reg
= NULL
;
459 parameters
.divider_reg
= NULL
;
460 for (i
= 0; i
< 2; i
++) {
461 void __iomem
*map_res
;
462 rc
= of_address_to_resource(np
, i
, &res
);
465 pr_err("no DTS register for %s\n",
471 map_res
= of_iomap(np
, i
);
472 if (map_res
== NULL
) {
473 pr_err("Unable to map resource %d for %s\n",
477 if (strcmp(res
.name
, "div-reg") == 0)
478 parameters
.divider_reg
= map_res
;
479 else /* if (strcmp(res->name, "csr-reg") == 0) */
480 parameters
.csr_reg
= map_res
;
482 if (of_property_read_u32(np
, "csr-offset", ¶meters
.reg_csr_offset
))
483 parameters
.reg_csr_offset
= 0;
484 if (of_property_read_u32(np
, "csr-mask", ¶meters
.reg_csr_mask
))
485 parameters
.reg_csr_mask
= 0xF;
486 if (of_property_read_u32(np
, "enable-offset",
487 ¶meters
.reg_clk_offset
))
488 parameters
.reg_clk_offset
= 0x8;
489 if (of_property_read_u32(np
, "enable-mask", ¶meters
.reg_clk_mask
))
490 parameters
.reg_clk_mask
= 0xF;
491 if (of_property_read_u32(np
, "divider-offset",
492 ¶meters
.reg_divider_offset
))
493 parameters
.reg_divider_offset
= 0;
494 if (of_property_read_u32(np
, "divider-width",
495 ¶meters
.reg_divider_width
))
496 parameters
.reg_divider_width
= 0;
497 if (of_property_read_u32(np
, "divider-shift",
498 ¶meters
.reg_divider_shift
))
499 parameters
.reg_divider_shift
= 0;
500 of_property_read_string(np
, "clock-output-names", &clk_name
);
502 clk
= xgene_register_clk(NULL
, clk_name
,
503 of_clk_get_parent_name(np
, 0), ¶meters
, &clk_lock
);
506 pr_debug("Add %s clock\n", clk_name
);
507 rc
= of_clk_add_provider(np
, of_clk_src_simple_get
, clk
);
509 pr_err("%s: could register provider clk %s\n", __func__
,
515 if (parameters
.csr_reg
)
516 iounmap(parameters
.csr_reg
);
517 if (parameters
.divider_reg
)
518 iounmap(parameters
.divider_reg
);
521 CLK_OF_DECLARE(xgene_socpll_clock
, "apm,xgene-socpll-clock", xgene_socpllclk_init
);
522 CLK_OF_DECLARE(xgene_pcppll_clock
, "apm,xgene-pcppll-clock", xgene_pcppllclk_init
);
523 CLK_OF_DECLARE(xgene_dev_clock
, "apm,xgene-device-clock", xgene_devclk_init
);