1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18#include <linux/clk.h>
19#include <linux/clk-provider.h>
20#include <linux/clkdev.h>
21#include <linux/clk/ti.h>
22#include <linux/io.h>
23#include <linux/of.h>
24#include <linux/of_address.h>
25#include <linux/list.h>
26#include <linux/regmap.h>
27#include <linux/memblock.h>
28#include <linux/device.h>
29
30#include "clock.h"
31
32#undef pr_fmt
33#define pr_fmt(fmt) "%s: " fmt, __func__
34
35static LIST_HEAD(clk_hw_omap_clocks);
36struct ti_clk_ll_ops *ti_clk_ll_ops;
37static struct device_node *clocks_node_ptr[CLK_MAX_MEMMAPS];
38
39struct ti_clk_features ti_clk_features;
40
41struct clk_iomap {
42 struct regmap *regmap;
43 void __iomem *mem;
44};
45
46static struct clk_iomap *clk_memmaps[CLK_MAX_MEMMAPS];
47
48static void clk_memmap_writel(u32 val, const struct clk_omap_reg *reg)
49{
50 struct clk_iomap *io = clk_memmaps[reg->index];
51
52 if (reg->ptr)
53 writel_relaxed(val, reg->ptr);
54 else if (io->regmap)
55 regmap_write(io->regmap, reg->offset, val);
56 else
57 writel_relaxed(val, io->mem + reg->offset);
58}
59
60static void _clk_rmw(u32 val, u32 mask, void __iomem *ptr)
61{
62 u32 v;
63
64 v = readl_relaxed(ptr);
65 v &= ~mask;
66 v |= val;
67 writel_relaxed(v, ptr);
68}
69
70static void clk_memmap_rmw(u32 val, u32 mask, const struct clk_omap_reg *reg)
71{
72 struct clk_iomap *io = clk_memmaps[reg->index];
73
74 if (reg->ptr) {
75 _clk_rmw(val, mask, reg->ptr);
76 } else if (io->regmap) {
77 regmap_update_bits(io->regmap, reg->offset, mask, val);
78 } else {
79 _clk_rmw(val, mask, io->mem + reg->offset);
80 }
81}
82
83static u32 clk_memmap_readl(const struct clk_omap_reg *reg)
84{
85 u32 val;
86 struct clk_iomap *io = clk_memmaps[reg->index];
87
88 if (reg->ptr)
89 val = readl_relaxed(reg->ptr);
90 else if (io->regmap)
91 regmap_read(io->regmap, reg->offset, &val);
92 else
93 val = readl_relaxed(io->mem + reg->offset);
94
95 return val;
96}
97
98
99
100
101
102
103
104
105
106
107int ti_clk_setup_ll_ops(struct ti_clk_ll_ops *ops)
108{
109 if (ti_clk_ll_ops) {
110 pr_err("Attempt to register ll_ops multiple times.\n");
111 return -EBUSY;
112 }
113
114 ti_clk_ll_ops = ops;
115 ops->clk_readl = clk_memmap_readl;
116 ops->clk_writel = clk_memmap_writel;
117 ops->clk_rmw = clk_memmap_rmw;
118
119 return 0;
120}
121
122
123
124
125
126
127
128
129
130
131void __init ti_dt_clocks_register(struct ti_dt_clk oclks[])
132{
133 struct ti_dt_clk *c;
134 struct device_node *node, *parent;
135 struct clk *clk;
136 struct of_phandle_args clkspec;
137 char buf[64];
138 char *ptr;
139 char *tags[2];
140 int i;
141 int num_args;
142 int ret;
143 static bool clkctrl_nodes_missing;
144 static bool has_clkctrl_data;
145 static bool compat_mode;
146
147 compat_mode = ti_clk_get_features()->flags & TI_CLK_CLKCTRL_COMPAT;
148
149 for (c = oclks; c->node_name != NULL; c++) {
150 strcpy(buf, c->node_name);
151 ptr = buf;
152 for (i = 0; i < 2; i++)
153 tags[i] = NULL;
154 num_args = 0;
155 while (*ptr) {
156 if (*ptr == ':') {
157 if (num_args >= 2) {
158 pr_warn("Bad number of tags on %s\n",
159 c->node_name);
160 return;
161 }
162 tags[num_args++] = ptr + 1;
163 *ptr = 0;
164 }
165 ptr++;
166 }
167
168 if (num_args && clkctrl_nodes_missing)
169 continue;
170
171 node = of_find_node_by_name(NULL, buf);
172 if (num_args && compat_mode) {
173 parent = node;
174 node = of_get_child_by_name(parent, "clock");
175 if (!node)
176 node = of_get_child_by_name(parent, "clk");
177 of_node_put(parent);
178 }
179
180 clkspec.np = node;
181 clkspec.args_count = num_args;
182 for (i = 0; i < num_args; i++) {
183 ret = kstrtoint(tags[i], i ? 10 : 16, clkspec.args + i);
184 if (ret) {
185 pr_warn("Bad tag in %s at %d: %s\n",
186 c->node_name, i, tags[i]);
187 of_node_put(node);
188 return;
189 }
190 }
191 clk = of_clk_get_from_provider(&clkspec);
192 of_node_put(node);
193 if (!IS_ERR(clk)) {
194 c->lk.clk = clk;
195 clkdev_add(&c->lk);
196 } else {
197 if (num_args && !has_clkctrl_data) {
198 struct device_node *np;
199
200 np = of_find_compatible_node(NULL, NULL,
201 "ti,clkctrl");
202 if (np) {
203 has_clkctrl_data = true;
204 of_node_put(np);
205 } else {
206 clkctrl_nodes_missing = true;
207
208 pr_warn("missing clkctrl nodes, please update your dts.\n");
209 continue;
210 }
211 }
212
213 pr_warn("failed to lookup clock node %s, ret=%ld\n",
214 c->node_name, PTR_ERR(clk));
215 }
216 }
217}
218
219struct clk_init_item {
220 struct device_node *node;
221 void *user;
222 ti_of_clk_init_cb_t func;
223 struct list_head link;
224};
225
226static LIST_HEAD(retry_list);
227
228
229
230
231
232
233
234
235
236
237int __init ti_clk_retry_init(struct device_node *node, void *user,
238 ti_of_clk_init_cb_t func)
239{
240 struct clk_init_item *retry;
241
242 pr_debug("%pOFn: adding to retry list...\n", node);
243 retry = kzalloc(sizeof(*retry), GFP_KERNEL);
244 if (!retry)
245 return -ENOMEM;
246
247 retry->node = node;
248 retry->func = func;
249 retry->user = user;
250 list_add(&retry->link, &retry_list);
251
252 return 0;
253}
254
255
256
257
258
259
260
261
262
263
264
265int ti_clk_get_reg_addr(struct device_node *node, int index,
266 struct clk_omap_reg *reg)
267{
268 u32 val;
269 int i;
270
271 for (i = 0; i < CLK_MAX_MEMMAPS; i++) {
272 if (clocks_node_ptr[i] == node->parent)
273 break;
274 }
275
276 if (i == CLK_MAX_MEMMAPS) {
277 pr_err("clk-provider not found for %pOFn!\n", node);
278 return -ENOENT;
279 }
280
281 reg->index = i;
282
283 if (of_property_read_u32_index(node, "reg", index, &val)) {
284 pr_err("%pOFn must have reg[%d]!\n", node, index);
285 return -EINVAL;
286 }
287
288 reg->offset = val;
289 reg->ptr = NULL;
290
291 return 0;
292}
293
294void ti_clk_latch(struct clk_omap_reg *reg, s8 shift)
295{
296 u32 latch;
297
298 if (shift < 0)
299 return;
300
301 latch = 1 << shift;
302
303 ti_clk_ll_ops->clk_rmw(latch, latch, reg);
304 ti_clk_ll_ops->clk_rmw(0, latch, reg);
305 ti_clk_ll_ops->clk_readl(reg);
306}
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322int __init omap2_clk_provider_init(struct device_node *parent, int index,
323 struct regmap *syscon, void __iomem *mem)
324{
325 struct device_node *clocks;
326 struct clk_iomap *io;
327
328
329 clocks = of_get_child_by_name(parent, "clocks");
330 if (!clocks) {
331 pr_err("%pOFn missing 'clocks' child node.\n", parent);
332 return -EINVAL;
333 }
334
335
336 clocks_node_ptr[index] = clocks;
337
338 io = kzalloc(sizeof(*io), GFP_KERNEL);
339 if (!io)
340 return -ENOMEM;
341
342 io->regmap = syscon;
343 io->mem = mem;
344
345 clk_memmaps[index] = io;
346
347 return 0;
348}
349
350
351
352
353
354
355
356
357void __init omap2_clk_legacy_provider_init(int index, void __iomem *mem)
358{
359 struct clk_iomap *io;
360
361 io = memblock_alloc(sizeof(*io), SMP_CACHE_BYTES);
362 if (!io)
363 panic("%s: Failed to allocate %zu bytes\n", __func__,
364 sizeof(*io));
365
366 io->mem = mem;
367
368 clk_memmaps[index] = io;
369}
370
371
372
373
374
375
376
377
378
379void ti_dt_clk_init_retry_clks(void)
380{
381 struct clk_init_item *retry;
382 struct clk_init_item *tmp;
383 int retries = 5;
384
385 while (!list_empty(&retry_list) && retries) {
386 list_for_each_entry_safe(retry, tmp, &retry_list, link) {
387 pr_debug("retry-init: %pOFn\n", retry->node);
388 retry->func(retry->user, retry->node);
389 list_del(&retry->link);
390 kfree(retry);
391 }
392 retries--;
393 }
394}
395
396static const struct of_device_id simple_clk_match_table[] __initconst = {
397 { .compatible = "fixed-clock" },
398 { .compatible = "fixed-factor-clock" },
399 { }
400};
401
402
403
404
405
406
407void __init ti_clk_add_aliases(void)
408{
409 struct device_node *np;
410 struct clk *clk;
411
412 for_each_matching_node(np, simple_clk_match_table) {
413 struct of_phandle_args clkspec;
414
415 clkspec.np = np;
416 clk = of_clk_get_from_provider(&clkspec);
417
418 ti_clk_add_alias(NULL, clk, np->name);
419 }
420}
421
422
423
424
425
426
427
428
429void __init ti_clk_setup_features(struct ti_clk_features *features)
430{
431 memcpy(&ti_clk_features, features, sizeof(*features));
432}
433
434
435
436
437
438
439
440const struct ti_clk_features *ti_clk_get_features(void)
441{
442 return &ti_clk_features;
443}
444
445
446
447
448
449
450
451
452
453
454
455void omap2_clk_enable_init_clocks(const char **clk_names, u8 num_clocks)
456{
457 struct clk *init_clk;
458 int i;
459
460 for (i = 0; i < num_clocks; i++) {
461 init_clk = clk_get(NULL, clk_names[i]);
462 if (WARN(IS_ERR(init_clk), "could not find init clock %s\n",
463 clk_names[i]))
464 continue;
465 clk_prepare_enable(init_clk);
466 }
467}
468
469
470
471
472
473
474
475
476
477
478
479int ti_clk_add_alias(struct device *dev, struct clk *clk, const char *con)
480{
481 struct clk_lookup *cl;
482
483 if (!clk)
484 return 0;
485
486 if (IS_ERR(clk))
487 return PTR_ERR(clk);
488
489 cl = kzalloc(sizeof(*cl), GFP_KERNEL);
490 if (!cl)
491 return -ENOMEM;
492
493 if (dev)
494 cl->dev_id = dev_name(dev);
495 cl->con_id = con;
496 cl->clk = clk;
497
498 clkdev_add(cl);
499
500 return 0;
501}
502
503
504
505
506
507
508
509
510
511
512
513struct clk *ti_clk_register(struct device *dev, struct clk_hw *hw,
514 const char *con)
515{
516 struct clk *clk;
517 int ret;
518
519 clk = clk_register(dev, hw);
520 if (IS_ERR(clk))
521 return clk;
522
523 ret = ti_clk_add_alias(dev, clk, con);
524 if (ret) {
525 clk_unregister(clk);
526 return ERR_PTR(ret);
527 }
528
529 return clk;
530}
531
532
533
534
535
536
537
538
539
540
541
542
543struct clk *ti_clk_register_omap_hw(struct device *dev, struct clk_hw *hw,
544 const char *con)
545{
546 struct clk *clk;
547 struct clk_hw_omap *oclk;
548
549 clk = ti_clk_register(dev, hw, con);
550 if (IS_ERR(clk))
551 return clk;
552
553 oclk = to_clk_hw_omap(hw);
554
555 list_add(&oclk->node, &clk_hw_omap_clocks);
556
557 return clk;
558}
559
560
561
562
563
564
565
566
567
568
569
570int omap2_clk_for_each(int (*fn)(struct clk_hw_omap *hw))
571{
572 int ret;
573 struct clk_hw_omap *hw;
574
575 list_for_each_entry(hw, &clk_hw_omap_clocks, node) {
576 ret = (*fn)(hw);
577 if (ret)
578 break;
579 }
580
581 return ret;
582}
583
584
585
586
587
588
589
590
591bool omap2_clk_is_hw_omap(struct clk_hw *hw)
592{
593 struct clk_hw_omap *oclk;
594
595 list_for_each_entry(oclk, &clk_hw_omap_clocks, node) {
596 if (&oclk->hw == hw)
597 return true;
598 }
599
600 return false;
601}
602