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 *csr
)
47 return readl_relaxed(csr
);
50 static inline void xgene_clk_write(u32 data
, void *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 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;
227 spin_lock_irqsave(pclk
->lock
, flags
);
229 if (pclk
->param
.csr_reg
!= NULL
) {
230 pr_debug("%s clock enabled\n", pclk
->name
);
231 /* First enable the clock */
232 data
= xgene_clk_read(pclk
->param
.csr_reg
+
233 pclk
->param
.reg_clk_offset
);
234 data
|= pclk
->param
.reg_clk_mask
;
235 xgene_clk_write(data
, pclk
->param
.csr_reg
+
236 pclk
->param
.reg_clk_offset
);
237 pr_debug("%s clock PADDR base 0x%016LX clk offset 0x%08X mask 0x%08X value 0x%08X\n",
238 pclk
->name
, __pa(pclk
->param
.csr_reg
),
239 pclk
->param
.reg_clk_offset
, pclk
->param
.reg_clk_mask
,
242 /* Second enable the CSR */
243 data
= xgene_clk_read(pclk
->param
.csr_reg
+
244 pclk
->param
.reg_csr_offset
);
245 data
&= ~pclk
->param
.reg_csr_mask
;
246 xgene_clk_write(data
, pclk
->param
.csr_reg
+
247 pclk
->param
.reg_csr_offset
);
248 pr_debug("%s CSR RESET PADDR base 0x%016LX csr offset 0x%08X mask 0x%08X value 0x%08X\n",
249 pclk
->name
, __pa(pclk
->param
.csr_reg
),
250 pclk
->param
.reg_csr_offset
, pclk
->param
.reg_csr_mask
,
255 spin_unlock_irqrestore(pclk
->lock
, flags
);
260 static void xgene_clk_disable(struct clk_hw
*hw
)
262 struct xgene_clk
*pclk
= to_xgene_clk(hw
);
263 unsigned long flags
= 0;
267 spin_lock_irqsave(pclk
->lock
, flags
);
269 if (pclk
->param
.csr_reg
!= NULL
) {
270 pr_debug("%s clock disabled\n", pclk
->name
);
271 /* First put the CSR in reset */
272 data
= xgene_clk_read(pclk
->param
.csr_reg
+
273 pclk
->param
.reg_csr_offset
);
274 data
|= pclk
->param
.reg_csr_mask
;
275 xgene_clk_write(data
, pclk
->param
.csr_reg
+
276 pclk
->param
.reg_csr_offset
);
278 /* Second disable the clock */
279 data
= xgene_clk_read(pclk
->param
.csr_reg
+
280 pclk
->param
.reg_clk_offset
);
281 data
&= ~pclk
->param
.reg_clk_mask
;
282 xgene_clk_write(data
, pclk
->param
.csr_reg
+
283 pclk
->param
.reg_clk_offset
);
287 spin_unlock_irqrestore(pclk
->lock
, flags
);
290 static int xgene_clk_is_enabled(struct clk_hw
*hw
)
292 struct xgene_clk
*pclk
= to_xgene_clk(hw
);
295 if (pclk
->param
.csr_reg
!= NULL
) {
296 pr_debug("%s clock checking\n", pclk
->name
);
297 data
= xgene_clk_read(pclk
->param
.csr_reg
+
298 pclk
->param
.reg_clk_offset
);
299 pr_debug("%s clock is %s\n", pclk
->name
,
300 data
& pclk
->param
.reg_clk_mask
? "enabled" :
304 if (pclk
->param
.csr_reg
== NULL
)
306 return data
& pclk
->param
.reg_clk_mask
? 1 : 0;
309 static unsigned long xgene_clk_recalc_rate(struct clk_hw
*hw
,
310 unsigned long parent_rate
)
312 struct xgene_clk
*pclk
= to_xgene_clk(hw
);
315 if (pclk
->param
.divider_reg
) {
316 data
= xgene_clk_read(pclk
->param
.divider_reg
+
317 pclk
->param
.reg_divider_offset
);
318 data
>>= pclk
->param
.reg_divider_shift
;
319 data
&= (1 << pclk
->param
.reg_divider_width
) - 1;
321 pr_debug("%s clock recalc rate %ld parent %ld\n",
322 pclk
->name
, parent_rate
/ data
, parent_rate
);
323 return parent_rate
/ data
;
325 pr_debug("%s clock recalc rate %ld parent %ld\n",
326 pclk
->name
, parent_rate
, parent_rate
);
331 static int xgene_clk_set_rate(struct clk_hw
*hw
, unsigned long rate
,
332 unsigned long parent_rate
)
334 struct xgene_clk
*pclk
= to_xgene_clk(hw
);
335 unsigned long flags
= 0;
341 spin_lock_irqsave(pclk
->lock
, flags
);
343 if (pclk
->param
.divider_reg
) {
344 /* Let's compute the divider */
345 if (rate
> parent_rate
)
347 divider_save
= divider
= parent_rate
/ rate
; /* Rounded down */
348 divider
&= (1 << pclk
->param
.reg_divider_width
) - 1;
349 divider
<<= pclk
->param
.reg_divider_shift
;
351 /* Set new divider */
352 data
= xgene_clk_read(pclk
->param
.divider_reg
+
353 pclk
->param
.reg_divider_offset
);
354 data
&= ~((1 << pclk
->param
.reg_divider_width
) - 1);
356 xgene_clk_write(data
, pclk
->param
.divider_reg
+
357 pclk
->param
.reg_divider_offset
);
358 pr_debug("%s clock set rate %ld\n", pclk
->name
,
359 parent_rate
/ divider_save
);
365 spin_unlock_irqrestore(pclk
->lock
, flags
);
367 return parent_rate
/ divider_save
;
370 static long xgene_clk_round_rate(struct clk_hw
*hw
, unsigned long rate
,
371 unsigned long *prate
)
373 struct xgene_clk
*pclk
= to_xgene_clk(hw
);
374 unsigned long parent_rate
= *prate
;
377 if (pclk
->param
.divider_reg
) {
378 /* Let's compute the divider */
379 if (rate
> parent_rate
)
381 divider
= parent_rate
/ rate
; /* Rounded down */
386 return parent_rate
/ divider
;
389 const struct clk_ops xgene_clk_ops
= {
390 .enable
= xgene_clk_enable
,
391 .disable
= xgene_clk_disable
,
392 .is_enabled
= xgene_clk_is_enabled
,
393 .recalc_rate
= xgene_clk_recalc_rate
,
394 .set_rate
= xgene_clk_set_rate
,
395 .round_rate
= xgene_clk_round_rate
,
398 static struct clk
*xgene_register_clk(struct device
*dev
,
399 const char *name
, const char *parent_name
,
400 struct xgene_dev_parameters
*parameters
, spinlock_t
*lock
)
402 struct xgene_clk
*apmclk
;
404 struct clk_init_data init
;
407 /* allocate the APM clock structure */
408 apmclk
= kzalloc(sizeof(*apmclk
), GFP_KERNEL
);
410 pr_err("%s: could not allocate APM clk\n", __func__
);
411 return ERR_PTR(-ENOMEM
);
415 init
.ops
= &xgene_clk_ops
;
417 init
.parent_names
= parent_name
? &parent_name
: NULL
;
418 init
.num_parents
= parent_name
? 1 : 0;
422 apmclk
->hw
.init
= &init
;
423 apmclk
->param
= *parameters
;
425 /* Register the clock */
426 clk
= clk_register(dev
, &apmclk
->hw
);
428 pr_err("%s: could not register clk %s\n", __func__
, name
);
433 /* Register the clock for lookup */
434 rc
= clk_register_clkdev(clk
, name
, NULL
);
436 pr_err("%s: could not register lookup clk %s\n",
442 static void __init
xgene_devclk_init(struct device_node
*np
)
444 const char *clk_name
= np
->full_name
;
448 struct xgene_dev_parameters parameters
;
451 /* Check if the entry is disabled */
452 if (!of_device_is_available(np
))
455 /* Parse the DTS register for resource */
456 parameters
.csr_reg
= NULL
;
457 parameters
.divider_reg
= NULL
;
458 for (i
= 0; i
< 2; i
++) {
460 rc
= of_address_to_resource(np
, i
, &res
);
463 pr_err("no DTS register for %s\n",
469 map_res
= of_iomap(np
, i
);
470 if (map_res
== NULL
) {
471 pr_err("Unable to map resource %d for %s\n",
475 if (strcmp(res
.name
, "div-reg") == 0)
476 parameters
.divider_reg
= map_res
;
477 else /* if (strcmp(res->name, "csr-reg") == 0) */
478 parameters
.csr_reg
= map_res
;
480 if (of_property_read_u32(np
, "csr-offset", ¶meters
.reg_csr_offset
))
481 parameters
.reg_csr_offset
= 0;
482 if (of_property_read_u32(np
, "csr-mask", ¶meters
.reg_csr_mask
))
483 parameters
.reg_csr_mask
= 0xF;
484 if (of_property_read_u32(np
, "enable-offset",
485 ¶meters
.reg_clk_offset
))
486 parameters
.reg_clk_offset
= 0x8;
487 if (of_property_read_u32(np
, "enable-mask", ¶meters
.reg_clk_mask
))
488 parameters
.reg_clk_mask
= 0xF;
489 if (of_property_read_u32(np
, "divider-offset",
490 ¶meters
.reg_divider_offset
))
491 parameters
.reg_divider_offset
= 0;
492 if (of_property_read_u32(np
, "divider-width",
493 ¶meters
.reg_divider_width
))
494 parameters
.reg_divider_width
= 0;
495 if (of_property_read_u32(np
, "divider-shift",
496 ¶meters
.reg_divider_shift
))
497 parameters
.reg_divider_shift
= 0;
498 of_property_read_string(np
, "clock-output-names", &clk_name
);
500 clk
= xgene_register_clk(NULL
, clk_name
,
501 of_clk_get_parent_name(np
, 0), ¶meters
, &clk_lock
);
504 pr_debug("Add %s clock\n", clk_name
);
505 rc
= of_clk_add_provider(np
, of_clk_src_simple_get
, clk
);
507 pr_err("%s: could register provider clk %s\n", __func__
,
513 if (parameters
.csr_reg
)
514 iounmap(parameters
.csr_reg
);
515 if (parameters
.divider_reg
)
516 iounmap(parameters
.divider_reg
);
519 CLK_OF_DECLARE(xgene_socpll_clock
, "apm,xgene-socpll-clock", xgene_socpllclk_init
);
520 CLK_OF_DECLARE(xgene_pcppll_clock
, "apm,xgene-pcppll-clock", xgene_pcppllclk_init
);
521 CLK_OF_DECLARE(xgene_dev_clock
, "apm,xgene-device-clock", xgene_devclk_init
);