]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/clk/versatile/clk-icst.c
clk: versatile/icst: add Integrator core module clocks
[karo-tx-linux.git] / drivers / clk / versatile / clk-icst.c
1 /*
2  * Driver for the ICST307 VCO clock found in the ARM Reference designs.
3  * We wrap the custom interface from <asm/hardware/icst.h> into the generic
4  * clock framework.
5  *
6  * Copyright (C) 2012-2015 Linus Walleij
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  *
12  * TODO: when all ARM reference designs are migrated to generic clocks, the
13  * ICST clock code from the ARM tree should probably be merged into this
14  * file.
15  */
16 #include <linux/kernel.h>
17 #include <linux/slab.h>
18 #include <linux/export.h>
19 #include <linux/err.h>
20 #include <linux/clk-provider.h>
21 #include <linux/io.h>
22 #include <linux/regmap.h>
23 #include <linux/mfd/syscon.h>
24
25 #include "clk-icst.h"
26
27 /* Magic unlocking token used on all Versatile boards */
28 #define VERSATILE_LOCK_VAL      0xA05F
29
30 #define VERSATILE_AUX_OSC_BITS 0x7FFFF
31 #define INTEGRATOR_AP_CM_BITS 0xFF
32 #define INTEGRATOR_CP_CM_CORE_BITS 0x7FF
33 #define INTEGRATOR_CP_CM_MEM_BITS 0x7FF000
34
35 /**
36  * enum icst_control_type - the type of ICST control register
37  */
38 enum icst_control_type {
39         ICST_VERSATILE, /* The standard type, all control bits available */
40         ICST_INTEGRATOR_AP_CM, /* Only 8 bits of VDW available */
41         ICST_INTEGRATOR_CP_CM_CORE, /* Only 8 bits of VDW and 3 bits of OD */
42         ICST_INTEGRATOR_CP_CM_MEM, /* Only 8 bits of VDW and 3 bits of OD */
43 };
44
45 /**
46  * struct clk_icst - ICST VCO clock wrapper
47  * @hw: corresponding clock hardware entry
48  * @vcoreg: VCO register address
49  * @lockreg: VCO lock register address
50  * @params: parameters for this ICST instance
51  * @rate: current rate
52  * @ctype: the type of control register for the ICST
53  */
54 struct clk_icst {
55         struct clk_hw hw;
56         struct regmap *map;
57         u32 vcoreg_off;
58         u32 lockreg_off;
59         struct icst_params *params;
60         unsigned long rate;
61         enum icst_control_type ctype;
62 };
63
64 #define to_icst(_hw) container_of(_hw, struct clk_icst, hw)
65
66 /**
67  * vco_get() - get ICST VCO settings from a certain ICST
68  * @icst: the ICST clock to get
69  * @vco: the VCO struct to return the value in
70  */
71 static int vco_get(struct clk_icst *icst, struct icst_vco *vco)
72 {
73         u32 val;
74         int ret;
75
76         ret = regmap_read(icst->map, icst->vcoreg_off, &val);
77         if (ret)
78                 return ret;
79
80         /*
81          * The Integrator/AP core clock can only access the low eight
82          * bits of the v PLL divider. Bit 8 is tied low and always zero,
83          * r is hardwired to 22 and output divider s is hardwired to 1
84          * (divide by 2) according to the document
85          * "Integrator CM926EJ-S, CM946E-S, CM966E-S, CM1026EJ-S and
86          * CM1136JF-S User Guide" ARM DUI 0138E, page 3-13 thru 3-14.
87          */
88         if (icst->ctype == ICST_INTEGRATOR_AP_CM) {
89                 vco->v = val & INTEGRATOR_AP_CM_BITS;
90                 vco->r = 22;
91                 vco->s = 1;
92                 return 0;
93         }
94
95         /*
96          * The Integrator/CP core clock can access the low eight bits
97          * of the v PLL divider. Bit 8 is tied low and always zero,
98          * r is hardwired to 22 and the output divider s is accessible
99          * in bits 8 thru 10 according to the document
100          * "Integrator/CM940T, CM920T, CM740T, and CM720T User Guide"
101          * ARM DUI 0157A, page 3-20 thru 3-23 and 4-10.
102          */
103         if (icst->ctype == ICST_INTEGRATOR_CP_CM_CORE) {
104                 vco->v = val & 0xFF;
105                 vco->r = 22;
106                 vco->s = (val >> 8) & 7;
107                 return 0;
108         }
109
110         if (icst->ctype == ICST_INTEGRATOR_CP_CM_MEM) {
111                 vco->v = (val >> 12) & 0xFF;
112                 vco->r = 22;
113                 vco->s = (val >> 20) & 7;
114                 return 0;
115         }
116
117         vco->v = val & 0x1ff;
118         vco->r = (val >> 9) & 0x7f;
119         vco->s = (val >> 16) & 03;
120         return 0;
121 }
122
123 /**
124  * vco_set() - commit changes to an ICST VCO
125  * @icst: the ICST clock to set
126  * @vco: the VCO struct to set the changes from
127  */
128 static int vco_set(struct clk_icst *icst, struct icst_vco vco)
129 {
130         u32 mask;
131         u32 val;
132         int ret;
133
134         /* Mask the bits used by the VCO */
135         switch (icst->ctype) {
136         case ICST_INTEGRATOR_AP_CM:
137                 mask = INTEGRATOR_AP_CM_BITS;
138                 val = vco.v & 0xFF;
139                 if (vco.v & 0x100)
140                         pr_err("ICST error: tried to set bit 8 of VDW\n");
141                 if (vco.s != 1)
142                         pr_err("ICST error: tried to use VOD != 1\n");
143                 if (vco.r != 22)
144                         pr_err("ICST error: tried to use RDW != 22\n");
145                 break;
146         case ICST_INTEGRATOR_CP_CM_CORE:
147                 mask = INTEGRATOR_CP_CM_CORE_BITS; /* Uses 12 bits */
148                 val = (vco.v & 0xFF) | vco.s << 8;
149                 if (vco.v & 0x100)
150                         pr_err("ICST error: tried to set bit 8 of VDW\n");
151                 if (vco.r != 22)
152                         pr_err("ICST error: tried to use RDW != 22\n");
153                 break;
154         case ICST_INTEGRATOR_CP_CM_MEM:
155                 mask = INTEGRATOR_CP_CM_MEM_BITS; /* Uses 12 bits */
156                 val = ((vco.v & 0xFF) << 12) | (vco.s << 20);
157                 if (vco.v & 0x100)
158                         pr_err("ICST error: tried to set bit 8 of VDW\n");
159                 if (vco.r != 22)
160                         pr_err("ICST error: tried to use RDW != 22\n");
161                 break;
162         default:
163                 /* Regular auxilary oscillator */
164                 mask = VERSATILE_AUX_OSC_BITS;
165                 val = vco.v | (vco.r << 9) | (vco.s << 16);
166                 break;
167         }
168
169         pr_debug("ICST: new val = 0x%08x\n", val);
170
171         /* This magic unlocks the VCO so it can be controlled */
172         ret = regmap_write(icst->map, icst->lockreg_off, VERSATILE_LOCK_VAL);
173         if (ret)
174                 return ret;
175         ret = regmap_update_bits(icst->map, icst->vcoreg_off, mask, val);
176         if (ret)
177                 return ret;
178         /* This locks the VCO again */
179         ret = regmap_write(icst->map, icst->lockreg_off, 0);
180         if (ret)
181                 return ret;
182         return 0;
183 }
184
185 static unsigned long icst_recalc_rate(struct clk_hw *hw,
186                                       unsigned long parent_rate)
187 {
188         struct clk_icst *icst = to_icst(hw);
189         struct icst_vco vco;
190         int ret;
191
192         if (parent_rate)
193                 icst->params->ref = parent_rate;
194         ret = vco_get(icst, &vco);
195         if (ret) {
196                 pr_err("ICST: could not get VCO setting\n");
197                 return 0;
198         }
199         icst->rate = icst_hz(icst->params, vco);
200         return icst->rate;
201 }
202
203 static long icst_round_rate(struct clk_hw *hw, unsigned long rate,
204                             unsigned long *prate)
205 {
206         struct clk_icst *icst = to_icst(hw);
207         struct icst_vco vco;
208
209         if (icst->ctype == ICST_INTEGRATOR_AP_CM ||
210             icst->ctype == ICST_INTEGRATOR_CP_CM_CORE) {
211                 if (rate <= 12000000)
212                         return 12000000;
213                 if (rate >= 160000000)
214                         return 160000000;
215                 /* Slam to closest megahertz */
216                 return DIV_ROUND_CLOSEST(rate, 1000000) * 1000000;
217         }
218
219         if (icst->ctype == ICST_INTEGRATOR_CP_CM_MEM) {
220                 if (rate <= 6000000)
221                         return 6000000;
222                 if (rate >= 66000000)
223                         return 66000000;
224                 /* Slam to closest 0.5 megahertz */
225                 return DIV_ROUND_CLOSEST(rate, 500000) * 500000;
226         }
227
228         vco = icst_hz_to_vco(icst->params, rate);
229         return icst_hz(icst->params, vco);
230 }
231
232 static int icst_set_rate(struct clk_hw *hw, unsigned long rate,
233                          unsigned long parent_rate)
234 {
235         struct clk_icst *icst = to_icst(hw);
236         struct icst_vco vco;
237
238         if (parent_rate)
239                 icst->params->ref = parent_rate;
240         vco = icst_hz_to_vco(icst->params, rate);
241         icst->rate = icst_hz(icst->params, vco);
242         return vco_set(icst, vco);
243 }
244
245 static const struct clk_ops icst_ops = {
246         .recalc_rate = icst_recalc_rate,
247         .round_rate = icst_round_rate,
248         .set_rate = icst_set_rate,
249 };
250
251 static struct clk *icst_clk_setup(struct device *dev,
252                                   const struct clk_icst_desc *desc,
253                                   const char *name,
254                                   const char *parent_name,
255                                   struct regmap *map,
256                                   enum icst_control_type ctype)
257 {
258         struct clk *clk;
259         struct clk_icst *icst;
260         struct clk_init_data init;
261         struct icst_params *pclone;
262
263         icst = kzalloc(sizeof(struct clk_icst), GFP_KERNEL);
264         if (!icst) {
265                 pr_err("could not allocate ICST clock!\n");
266                 return ERR_PTR(-ENOMEM);
267         }
268
269         pclone = kmemdup(desc->params, sizeof(*pclone), GFP_KERNEL);
270         if (!pclone) {
271                 kfree(icst);
272                 pr_err("could not clone ICST params\n");
273                 return ERR_PTR(-ENOMEM);
274         }
275
276         init.name = name;
277         init.ops = &icst_ops;
278         init.flags = 0;
279         init.parent_names = (parent_name ? &parent_name : NULL);
280         init.num_parents = (parent_name ? 1 : 0);
281         icst->map = map;
282         icst->hw.init = &init;
283         icst->params = pclone;
284         icst->vcoreg_off = desc->vco_offset;
285         icst->lockreg_off = desc->lock_offset;
286         icst->ctype = ctype;
287
288         clk = clk_register(dev, &icst->hw);
289         if (IS_ERR(clk)) {
290                 kfree(pclone);
291                 kfree(icst);
292         }
293
294         return clk;
295 }
296
297 struct clk *icst_clk_register(struct device *dev,
298                         const struct clk_icst_desc *desc,
299                         const char *name,
300                         const char *parent_name,
301                         void __iomem *base)
302 {
303         struct regmap_config icst_regmap_conf = {
304                 .reg_bits = 32,
305                 .val_bits = 32,
306                 .reg_stride = 4,
307         };
308         struct regmap *map;
309
310         map = regmap_init_mmio(dev, base, &icst_regmap_conf);
311         if (IS_ERR(map)) {
312                 pr_err("could not initialize ICST regmap\n");
313                 return ERR_CAST(map);
314         }
315         return icst_clk_setup(dev, desc, name, parent_name, map,
316                               ICST_VERSATILE);
317 }
318 EXPORT_SYMBOL_GPL(icst_clk_register);
319
320 #ifdef CONFIG_OF
321 /*
322  * In a device tree, an memory-mapped ICST clock appear as a child
323  * of a syscon node. Assume this and probe it only as a child of a
324  * syscon.
325  */
326
327 static const struct icst_params icst525_params = {
328         .vco_max        = ICST525_VCO_MAX_5V,
329         .vco_min        = ICST525_VCO_MIN,
330         .vd_min         = 8,
331         .vd_max         = 263,
332         .rd_min         = 3,
333         .rd_max         = 65,
334         .s2div          = icst525_s2div,
335         .idx2s          = icst525_idx2s,
336 };
337
338 static const struct icst_params icst307_params = {
339         .vco_max        = ICST307_VCO_MAX,
340         .vco_min        = ICST307_VCO_MIN,
341         .vd_min         = 4 + 8,
342         .vd_max         = 511 + 8,
343         .rd_min         = 1 + 2,
344         .rd_max         = 127 + 2,
345         .s2div          = icst307_s2div,
346         .idx2s          = icst307_idx2s,
347 };
348
349 /**
350  * The core modules on the Integrator/AP and Integrator/CP have
351  * especially crippled ICST525 control.
352  */
353 static const struct icst_params icst525_apcp_cm_params = {
354         .vco_max        = ICST525_VCO_MAX_5V,
355         .vco_min        = ICST525_VCO_MIN,
356         /* Minimum 12 MHz, VDW = 4 */
357         .vd_min         = 12,
358         /*
359          * Maximum 160 MHz, VDW = 152 for all core modules, but
360          * CM926EJ-S, CM1026EJ-S and CM1136JF-S can actually
361          * go to 200 MHz (max VDW = 192).
362          */
363         .vd_max         = 192,
364         /* r is hardcoded to 22 and this is the actual divisor, +2 */
365         .rd_min         = 24,
366         .rd_max         = 24,
367         .s2div          = icst525_s2div,
368         .idx2s          = icst525_idx2s,
369 };
370
371 static void __init of_syscon_icst_setup(struct device_node *np)
372 {
373         struct device_node *parent;
374         struct regmap *map;
375         struct clk_icst_desc icst_desc;
376         const char *name = np->name;
377         const char *parent_name;
378         struct clk *regclk;
379         enum icst_control_type ctype;
380
381         /* We do not release this reference, we are using it perpetually */
382         parent = of_get_parent(np);
383         if (!parent) {
384                 pr_err("no parent node for syscon ICST clock\n");
385                 return;
386         }
387         map = syscon_node_to_regmap(parent);
388         if (IS_ERR(map)) {
389                 pr_err("no regmap for syscon ICST clock parent\n");
390                 return;
391         }
392
393         if (of_property_read_u32(np, "vco-offset", &icst_desc.vco_offset)) {
394                 pr_err("no VCO register offset for ICST clock\n");
395                 return;
396         }
397         if (of_property_read_u32(np, "lock-offset", &icst_desc.lock_offset)) {
398                 pr_err("no lock register offset for ICST clock\n");
399                 return;
400         }
401
402         if (of_device_is_compatible(np, "arm,syscon-icst525")) {
403                 icst_desc.params = &icst525_params;
404                 ctype = ICST_VERSATILE;
405         } else if (of_device_is_compatible(np, "arm,syscon-icst307")) {
406                 icst_desc.params = &icst307_params;
407                 ctype = ICST_VERSATILE;
408         } else if (of_device_is_compatible(np, "arm,syscon-icst525-integratorap-cm")) {
409                 icst_desc.params = &icst525_apcp_cm_params;
410                 ctype = ICST_INTEGRATOR_AP_CM;
411         } else if (of_device_is_compatible(np, "arm,syscon-icst525-integratorcp-cm-core")) {
412                 icst_desc.params = &icst525_apcp_cm_params;
413                 ctype = ICST_INTEGRATOR_CP_CM_CORE;
414         } else if (of_device_is_compatible(np, "arm,syscon-icst525-integratorcp-cm-mem")) {
415                 icst_desc.params = &icst525_apcp_cm_params;
416                 ctype = ICST_INTEGRATOR_CP_CM_MEM;
417         } else {
418                 pr_err("unknown ICST clock %s\n", name);
419                 return;
420         }
421
422         /* Parent clock name is not the same as node parent */
423         parent_name = of_clk_get_parent_name(np, 0);
424
425         regclk = icst_clk_setup(NULL, &icst_desc, name, parent_name, map, ctype);
426         if (IS_ERR(regclk)) {
427                 pr_err("error setting up syscon ICST clock %s\n", name);
428                 return;
429         }
430         of_clk_add_provider(np, of_clk_src_simple_get, regclk);
431         pr_debug("registered syscon ICST clock %s\n", name);
432 }
433
434 CLK_OF_DECLARE(arm_syscon_icst525_clk,
435                "arm,syscon-icst525", of_syscon_icst_setup);
436 CLK_OF_DECLARE(arm_syscon_icst307_clk,
437                "arm,syscon-icst307", of_syscon_icst_setup);
438 CLK_OF_DECLARE(arm_syscon_integratorap_cm_clk,
439                "arm,syscon-icst525-integratorap-cm", of_syscon_icst_setup);
440 CLK_OF_DECLARE(arm_syscon_integratorcp_cm_core_clk,
441                "arm,syscon-icst525-integratorcp-cm-core", of_syscon_icst_setup);
442 CLK_OF_DECLARE(arm_syscon_integratorcp_cm_mem_clk,
443                "arm,syscon-icst525-integratorcp-cm-mem", of_syscon_icst_setup);
444 #endif