1 // SPDX-License-Identifier: GPL-2.0-only
3 * Driver for the ICST307 VCO clock found in the ARM Reference designs.
4 * We wrap the custom interface from <asm/hardware/icst.h> into the generic
7 * Copyright (C) 2012-2015 Linus Walleij
9 * TODO: when all ARM reference designs are migrated to generic clocks, the
10 * ICST clock code from the ARM tree should probably be merged into this
13 #include <linux/kernel.h>
14 #include <linux/slab.h>
15 #include <linux/export.h>
16 #include <linux/err.h>
17 #include <linux/clk-provider.h>
19 #include <linux/regmap.h>
20 #include <linux/mfd/syscon.h>
25 /* Magic unlocking token used on all Versatile boards */
26 #define VERSATILE_LOCK_VAL 0xA05F
28 #define VERSATILE_AUX_OSC_BITS 0x7FFFF
29 #define INTEGRATOR_AP_CM_BITS 0xFF
30 #define INTEGRATOR_AP_SYS_BITS 0xFF
31 #define INTEGRATOR_CP_CM_CORE_BITS 0x7FF
32 #define INTEGRATOR_CP_CM_MEM_BITS 0x7FF000
34 #define INTEGRATOR_AP_PCI_25_33_MHZ BIT(8)
37 * struct clk_icst - ICST VCO clock wrapper
38 * @hw: corresponding clock hardware entry
40 * @vcoreg_off: VCO register address
41 * @lockreg_off: VCO lock register address
42 * @params: parameters for this ICST instance
44 * @ctype: the type of control register for the ICST
51 struct icst_params
*params
;
53 enum icst_control_type ctype
;
56 #define to_icst(_hw) container_of(_hw, struct clk_icst, hw)
59 * vco_get() - get ICST VCO settings from a certain ICST
60 * @icst: the ICST clock to get
61 * @vco: the VCO struct to return the value in
63 static int vco_get(struct clk_icst
*icst
, struct icst_vco
*vco
)
68 ret
= regmap_read(icst
->map
, icst
->vcoreg_off
, &val
);
73 * The Integrator/AP core clock can only access the low eight
74 * bits of the v PLL divider. Bit 8 is tied low and always zero,
75 * r is hardwired to 22 and output divider s is hardwired to 1
76 * (divide by 2) according to the document
77 * "Integrator CM926EJ-S, CM946E-S, CM966E-S, CM1026EJ-S and
78 * CM1136JF-S User Guide" ARM DUI 0138E, page 3-13 thru 3-14.
80 if (icst
->ctype
== ICST_INTEGRATOR_AP_CM
) {
81 vco
->v
= val
& INTEGRATOR_AP_CM_BITS
;
88 * The Integrator/AP system clock on the base board can only
89 * access the low eight bits of the v PLL divider. Bit 8 is tied low
90 * and always zero, r is hardwired to 46, and the output divider is
91 * hardwired to 3 (divide by 4) according to the document
92 * "Integrator AP ASIC Development Motherboard" ARM DUI 0098B,
95 if (icst
->ctype
== ICST_INTEGRATOR_AP_SYS
) {
96 vco
->v
= val
& INTEGRATOR_AP_SYS_BITS
;
103 * The Integrator/AP PCI clock is using an odd pattern to create
104 * the child clock, basically a single bit called DIVX/Y is used
105 * to select between two different hardwired values: setting the
106 * bit to 0 yields v = 17, r = 22 and OD = 1, whereas setting the
107 * bit to 1 yields v = 14, r = 14 and OD = 1 giving the frequencies
108 * 33 or 25 MHz respectively.
110 if (icst
->ctype
== ICST_INTEGRATOR_AP_PCI
) {
111 bool divxy
= !!(val
& INTEGRATOR_AP_PCI_25_33_MHZ
);
113 vco
->v
= divxy
? 17 : 14;
114 vco
->r
= divxy
? 22 : 14;
120 * The Integrator/CP core clock can access the low eight bits
121 * of the v PLL divider. Bit 8 is tied low and always zero,
122 * r is hardwired to 22 and the output divider s is accessible
123 * in bits 8 thru 10 according to the document
124 * "Integrator/CM940T, CM920T, CM740T, and CM720T User Guide"
125 * ARM DUI 0157A, page 3-20 thru 3-23 and 4-10.
127 if (icst
->ctype
== ICST_INTEGRATOR_CP_CM_CORE
) {
130 vco
->s
= (val
>> 8) & 7;
134 if (icst
->ctype
== ICST_INTEGRATOR_CP_CM_MEM
) {
135 vco
->v
= (val
>> 12) & 0xFF;
137 vco
->s
= (val
>> 20) & 7;
141 vco
->v
= val
& 0x1ff;
142 vco
->r
= (val
>> 9) & 0x7f;
143 vco
->s
= (val
>> 16) & 03;
148 * vco_set() - commit changes to an ICST VCO
149 * @icst: the ICST clock to set
150 * @vco: the VCO struct to set the changes from
152 static int vco_set(struct clk_icst
*icst
, struct icst_vco vco
)
158 /* Mask the bits used by the VCO */
159 switch (icst
->ctype
) {
160 case ICST_INTEGRATOR_AP_CM
:
161 mask
= INTEGRATOR_AP_CM_BITS
;
164 pr_err("ICST error: tried to set bit 8 of VDW\n");
166 pr_err("ICST error: tried to use VOD != 1\n");
168 pr_err("ICST error: tried to use RDW != 22\n");
170 case ICST_INTEGRATOR_AP_SYS
:
171 mask
= INTEGRATOR_AP_SYS_BITS
;
174 pr_err("ICST error: tried to set bit 8 of VDW\n");
176 pr_err("ICST error: tried to use VOD != 1\n");
178 pr_err("ICST error: tried to use RDW != 22\n");
180 case ICST_INTEGRATOR_CP_CM_CORE
:
181 mask
= INTEGRATOR_CP_CM_CORE_BITS
; /* Uses 12 bits */
182 val
= (vco
.v
& 0xFF) | vco
.s
<< 8;
184 pr_err("ICST error: tried to set bit 8 of VDW\n");
186 pr_err("ICST error: tried to use RDW != 22\n");
188 case ICST_INTEGRATOR_CP_CM_MEM
:
189 mask
= INTEGRATOR_CP_CM_MEM_BITS
; /* Uses 12 bits */
190 val
= ((vco
.v
& 0xFF) << 12) | (vco
.s
<< 20);
192 pr_err("ICST error: tried to set bit 8 of VDW\n");
194 pr_err("ICST error: tried to use RDW != 22\n");
197 /* Regular auxilary oscillator */
198 mask
= VERSATILE_AUX_OSC_BITS
;
199 val
= vco
.v
| (vco
.r
<< 9) | (vco
.s
<< 16);
203 pr_debug("ICST: new val = 0x%08x\n", val
);
205 /* This magic unlocks the VCO so it can be controlled */
206 ret
= regmap_write(icst
->map
, icst
->lockreg_off
, VERSATILE_LOCK_VAL
);
209 ret
= regmap_update_bits(icst
->map
, icst
->vcoreg_off
, mask
, val
);
212 /* This locks the VCO again */
213 ret
= regmap_write(icst
->map
, icst
->lockreg_off
, 0);
219 static unsigned long icst_recalc_rate(struct clk_hw
*hw
,
220 unsigned long parent_rate
)
222 struct clk_icst
*icst
= to_icst(hw
);
227 icst
->params
->ref
= parent_rate
;
228 ret
= vco_get(icst
, &vco
);
230 pr_err("ICST: could not get VCO setting\n");
233 icst
->rate
= icst_hz(icst
->params
, vco
);
237 static long icst_round_rate(struct clk_hw
*hw
, unsigned long rate
,
238 unsigned long *prate
)
240 struct clk_icst
*icst
= to_icst(hw
);
243 if (icst
->ctype
== ICST_INTEGRATOR_AP_CM
||
244 icst
->ctype
== ICST_INTEGRATOR_CP_CM_CORE
) {
245 if (rate
<= 12000000)
247 if (rate
>= 160000000)
249 /* Slam to closest megahertz */
250 return DIV_ROUND_CLOSEST(rate
, 1000000) * 1000000;
253 if (icst
->ctype
== ICST_INTEGRATOR_CP_CM_MEM
) {
256 if (rate
>= 66000000)
258 /* Slam to closest 0.5 megahertz */
259 return DIV_ROUND_CLOSEST(rate
, 500000) * 500000;
262 if (icst
->ctype
== ICST_INTEGRATOR_AP_SYS
) {
263 /* Divides between 3 and 50 MHz in steps of 0.25 MHz */
266 if (rate
>= 50000000)
268 /* Slam to closest 0.25 MHz */
269 return DIV_ROUND_CLOSEST(rate
, 250000) * 250000;
272 if (icst
->ctype
== ICST_INTEGRATOR_AP_PCI
) {
274 * If we're below or less than halfway from 25 to 33 MHz
277 if (rate
<= 25000000 || rate
< 29000000)
279 /* Else just return the default frequency */
283 vco
= icst_hz_to_vco(icst
->params
, rate
);
284 return icst_hz(icst
->params
, vco
);
287 static int icst_set_rate(struct clk_hw
*hw
, unsigned long rate
,
288 unsigned long parent_rate
)
290 struct clk_icst
*icst
= to_icst(hw
);
293 if (icst
->ctype
== ICST_INTEGRATOR_AP_PCI
) {
294 /* This clock is especially primitive */
298 if (rate
== 25000000) {
300 } else if (rate
== 33000000) {
301 val
= INTEGRATOR_AP_PCI_25_33_MHZ
;
303 pr_err("ICST: cannot set PCI frequency %lu\n",
307 ret
= regmap_write(icst
->map
, icst
->lockreg_off
,
311 ret
= regmap_update_bits(icst
->map
, icst
->vcoreg_off
,
312 INTEGRATOR_AP_PCI_25_33_MHZ
,
316 /* This locks the VCO again */
317 ret
= regmap_write(icst
->map
, icst
->lockreg_off
, 0);
324 icst
->params
->ref
= parent_rate
;
325 vco
= icst_hz_to_vco(icst
->params
, rate
);
326 icst
->rate
= icst_hz(icst
->params
, vco
);
327 return vco_set(icst
, vco
);
330 static const struct clk_ops icst_ops
= {
331 .recalc_rate
= icst_recalc_rate
,
332 .round_rate
= icst_round_rate
,
333 .set_rate
= icst_set_rate
,
336 struct clk
*icst_clk_setup(struct device
*dev
,
337 const struct clk_icst_desc
*desc
,
339 const char *parent_name
,
341 enum icst_control_type ctype
)
344 struct clk_icst
*icst
;
345 struct clk_init_data init
;
346 struct icst_params
*pclone
;
348 icst
= kzalloc(sizeof(*icst
), GFP_KERNEL
);
350 return ERR_PTR(-ENOMEM
);
352 pclone
= kmemdup(desc
->params
, sizeof(*pclone
), GFP_KERNEL
);
355 return ERR_PTR(-ENOMEM
);
359 init
.ops
= &icst_ops
;
361 init
.parent_names
= (parent_name
? &parent_name
: NULL
);
362 init
.num_parents
= (parent_name
? 1 : 0);
364 icst
->hw
.init
= &init
;
365 icst
->params
= pclone
;
366 icst
->vcoreg_off
= desc
->vco_offset
;
367 icst
->lockreg_off
= desc
->lock_offset
;
370 clk
= clk_register(dev
, &icst
->hw
);
378 EXPORT_SYMBOL_GPL(icst_clk_setup
);
380 struct clk
*icst_clk_register(struct device
*dev
,
381 const struct clk_icst_desc
*desc
,
383 const char *parent_name
,
386 struct regmap_config icst_regmap_conf
= {
393 map
= regmap_init_mmio(dev
, base
, &icst_regmap_conf
);
395 pr_err("could not initialize ICST regmap\n");
396 return ERR_CAST(map
);
398 return icst_clk_setup(dev
, desc
, name
, parent_name
, map
,
401 EXPORT_SYMBOL_GPL(icst_clk_register
);
405 * In a device tree, an memory-mapped ICST clock appear as a child
406 * of a syscon node. Assume this and probe it only as a child of a
410 static const struct icst_params icst525_params
= {
411 .vco_max
= ICST525_VCO_MAX_5V
,
412 .vco_min
= ICST525_VCO_MIN
,
417 .s2div
= icst525_s2div
,
418 .idx2s
= icst525_idx2s
,
421 static const struct icst_params icst307_params
= {
422 .vco_max
= ICST307_VCO_MAX
,
423 .vco_min
= ICST307_VCO_MIN
,
428 .s2div
= icst307_s2div
,
429 .idx2s
= icst307_idx2s
,
433 * The core modules on the Integrator/AP and Integrator/CP have
434 * especially crippled ICST525 control.
436 static const struct icst_params icst525_apcp_cm_params
= {
437 .vco_max
= ICST525_VCO_MAX_5V
,
438 .vco_min
= ICST525_VCO_MIN
,
439 /* Minimum 12 MHz, VDW = 4 */
442 * Maximum 160 MHz, VDW = 152 for all core modules, but
443 * CM926EJ-S, CM1026EJ-S and CM1136JF-S can actually
444 * go to 200 MHz (max VDW = 192).
447 /* r is hardcoded to 22 and this is the actual divisor, +2 */
450 .s2div
= icst525_s2div
,
451 .idx2s
= icst525_idx2s
,
454 static const struct icst_params icst525_ap_sys_params
= {
455 .vco_max
= ICST525_VCO_MAX_5V
,
456 .vco_min
= ICST525_VCO_MIN
,
457 /* Minimum 3 MHz, VDW = 4 */
459 /* Maximum 50 MHz, VDW = 192 */
461 /* r is hardcoded to 46 and this is the actual divisor, +2 */
464 .s2div
= icst525_s2div
,
465 .idx2s
= icst525_idx2s
,
468 static const struct icst_params icst525_ap_pci_params
= {
469 .vco_max
= ICST525_VCO_MAX_5V
,
470 .vco_min
= ICST525_VCO_MIN
,
475 /* r is hardcoded to 14 or 22 and this is the actual divisors +2 */
478 .s2div
= icst525_s2div
,
479 .idx2s
= icst525_idx2s
,
482 static void __init
of_syscon_icst_setup(struct device_node
*np
)
484 struct device_node
*parent
;
486 struct clk_icst_desc icst_desc
;
488 const char *parent_name
;
490 enum icst_control_type ctype
;
492 /* We do not release this reference, we are using it perpetually */
493 parent
= of_get_parent(np
);
495 pr_err("no parent node for syscon ICST clock\n");
498 map
= syscon_node_to_regmap(parent
);
500 pr_err("no regmap for syscon ICST clock parent\n");
504 if (of_property_read_u32(np
, "reg", &icst_desc
.vco_offset
) &&
505 of_property_read_u32(np
, "vco-offset", &icst_desc
.vco_offset
)) {
506 pr_err("no VCO register offset for ICST clock\n");
509 if (of_property_read_u32(np
, "lock-offset", &icst_desc
.lock_offset
)) {
510 pr_err("no lock register offset for ICST clock\n");
514 if (of_device_is_compatible(np
, "arm,syscon-icst525")) {
515 icst_desc
.params
= &icst525_params
;
516 ctype
= ICST_VERSATILE
;
517 } else if (of_device_is_compatible(np
, "arm,syscon-icst307")) {
518 icst_desc
.params
= &icst307_params
;
519 ctype
= ICST_VERSATILE
;
520 } else if (of_device_is_compatible(np
, "arm,syscon-icst525-integratorap-cm")) {
521 icst_desc
.params
= &icst525_apcp_cm_params
;
522 ctype
= ICST_INTEGRATOR_AP_CM
;
523 } else if (of_device_is_compatible(np
, "arm,syscon-icst525-integratorap-sys")) {
524 icst_desc
.params
= &icst525_ap_sys_params
;
525 ctype
= ICST_INTEGRATOR_AP_SYS
;
526 } else if (of_device_is_compatible(np
, "arm,syscon-icst525-integratorap-pci")) {
527 icst_desc
.params
= &icst525_ap_pci_params
;
528 ctype
= ICST_INTEGRATOR_AP_PCI
;
529 } else if (of_device_is_compatible(np
, "arm,syscon-icst525-integratorcp-cm-core")) {
530 icst_desc
.params
= &icst525_apcp_cm_params
;
531 ctype
= ICST_INTEGRATOR_CP_CM_CORE
;
532 } else if (of_device_is_compatible(np
, "arm,syscon-icst525-integratorcp-cm-mem")) {
533 icst_desc
.params
= &icst525_apcp_cm_params
;
534 ctype
= ICST_INTEGRATOR_CP_CM_MEM
;
536 pr_err("unknown ICST clock %pOF\n", np
);
540 /* Parent clock name is not the same as node parent */
541 parent_name
= of_clk_get_parent_name(np
, 0);
542 name
= kasprintf(GFP_KERNEL
, "%pOFP", np
);
544 regclk
= icst_clk_setup(NULL
, &icst_desc
, name
, parent_name
, map
, ctype
);
545 if (IS_ERR(regclk
)) {
546 pr_err("error setting up syscon ICST clock %s\n", name
);
550 of_clk_add_provider(np
, of_clk_src_simple_get
, regclk
);
551 pr_debug("registered syscon ICST clock %s\n", name
);
554 CLK_OF_DECLARE(arm_syscon_icst525_clk
,
555 "arm,syscon-icst525", of_syscon_icst_setup
);
556 CLK_OF_DECLARE(arm_syscon_icst307_clk
,
557 "arm,syscon-icst307", of_syscon_icst_setup
);
558 CLK_OF_DECLARE(arm_syscon_integratorap_cm_clk
,
559 "arm,syscon-icst525-integratorap-cm", of_syscon_icst_setup
);
560 CLK_OF_DECLARE(arm_syscon_integratorap_sys_clk
,
561 "arm,syscon-icst525-integratorap-sys", of_syscon_icst_setup
);
562 CLK_OF_DECLARE(arm_syscon_integratorap_pci_clk
,
563 "arm,syscon-icst525-integratorap-pci", of_syscon_icst_setup
);
564 CLK_OF_DECLARE(arm_syscon_integratorcp_cm_core_clk
,
565 "arm,syscon-icst525-integratorcp-cm-core", of_syscon_icst_setup
);
566 CLK_OF_DECLARE(arm_syscon_integratorcp_cm_mem_clk
,
567 "arm,syscon-icst525-integratorcp-cm-mem", of_syscon_icst_setup
);