1 /*
2 * TI clock support
3 *
4 * Copyright (C) 2013 Texas Instruments, Inc.
5 *
6 * Tero Kristo <t-kristo@ti.com>
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 * This program is distributed "as is" WITHOUT ANY WARRANTY of any
13 * kind, whether express or implied; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
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
35 static LIST_HEAD(clk_hw_omap_clocks);
36 struct ti_clk_ll_ops *ti_clk_ll_ops;
37 static struct device_node *clocks_node_ptr[CLK_MAX_MEMMAPS];
38
39 struct ti_clk_features ti_clk_features;
40
41 struct clk_iomap {
42 struct regmap *regmap;
43 void __iomem *mem;
44 };
45
46 static struct clk_iomap *clk_memmaps[CLK_MAX_MEMMAPS];
47
clk_memmap_writel(u32 val,const struct clk_omap_reg * reg)48 static 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
_clk_rmw(u32 val,u32 mask,void __iomem * ptr)60 static 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
clk_memmap_rmw(u32 val,u32 mask,const struct clk_omap_reg * reg)70 static 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
clk_memmap_readl(const struct clk_omap_reg * reg)83 static 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 * ti_clk_setup_ll_ops - setup low level clock operations
100 * @ops: low level clock ops descriptor
101 *
102 * Sets up low level clock operations for TI clock driver. This is used
103 * to provide various callbacks for the clock driver towards platform
104 * specific code. Returns 0 on success, -EBUSY if ll_ops have been
105 * registered already.
106 */
ti_clk_setup_ll_ops(struct ti_clk_ll_ops * ops)107 int 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 * Eventually we could standardize to using '_' for clk-*.c files to follow the
124 * TRM naming and leave out the tmp name here.
125 */
ti_find_clock_provider(struct device_node * from,const char * name)126 static struct device_node *ti_find_clock_provider(struct device_node *from,
127 const char *name)
128 {
129 struct device_node *np;
130 bool found = false;
131 const char *n;
132 char *tmp;
133
134 tmp = kstrdup(name, GFP_KERNEL);
135 if (!tmp)
136 return NULL;
137 strreplace(tmp, '-', '_');
138
139 /* Node named "clock" with "clock-output-names" */
140 for_each_of_allnodes_from(from, np) {
141 if (of_property_read_string_index(np, "clock-output-names",
142 0, &n))
143 continue;
144
145 if (!strncmp(n, tmp, strlen(tmp))) {
146 of_node_get(np);
147 found = true;
148 break;
149 }
150 }
151 of_node_put(from);
152 kfree(tmp);
153
154 if (found)
155 return np;
156
157 /* Fall back to using old node name base provider name */
158 return of_find_node_by_name(from, name);
159 }
160
161 /**
162 * ti_dt_clocks_register - register DT alias clocks during boot
163 * @oclks: list of clocks to register
164 *
165 * Register alias or non-standard DT clock entries during boot. By
166 * default, DT clocks are found based on their clock-output-names
167 * property, or the clock node name for legacy cases. If any
168 * additional con-id / dev-id -> clock mapping is required, use this
169 * function to list these.
170 */
ti_dt_clocks_register(struct ti_dt_clk oclks[])171 void __init ti_dt_clocks_register(struct ti_dt_clk oclks[])
172 {
173 struct ti_dt_clk *c;
174 struct device_node *node, *parent, *child;
175 struct clk *clk;
176 struct of_phandle_args clkspec;
177 char buf[64];
178 char *ptr;
179 char *tags[2];
180 int i;
181 int num_args;
182 int ret;
183 static bool clkctrl_nodes_missing;
184 static bool has_clkctrl_data;
185 static bool compat_mode;
186
187 compat_mode = ti_clk_get_features()->flags & TI_CLK_CLKCTRL_COMPAT;
188
189 for (c = oclks; c->node_name != NULL; c++) {
190 strcpy(buf, c->node_name);
191 ptr = buf;
192 for (i = 0; i < 2; i++)
193 tags[i] = NULL;
194 num_args = 0;
195 while (*ptr) {
196 if (*ptr == ':') {
197 if (num_args >= 2) {
198 pr_warn("Bad number of tags on %s\n",
199 c->node_name);
200 return;
201 }
202 tags[num_args++] = ptr + 1;
203 *ptr = 0;
204 }
205 ptr++;
206 }
207
208 if (num_args && clkctrl_nodes_missing)
209 continue;
210
211 node = ti_find_clock_provider(NULL, buf);
212 if (num_args && compat_mode) {
213 parent = node;
214 child = of_get_child_by_name(parent, "clock");
215 if (!child)
216 child = of_get_child_by_name(parent, "clk");
217 if (child) {
218 of_node_put(parent);
219 node = child;
220 }
221 }
222
223 clkspec.np = node;
224 clkspec.args_count = num_args;
225 for (i = 0; i < num_args; i++) {
226 ret = kstrtoint(tags[i], i ? 10 : 16, clkspec.args + i);
227 if (ret) {
228 pr_warn("Bad tag in %s at %d: %s\n",
229 c->node_name, i, tags[i]);
230 of_node_put(node);
231 return;
232 }
233 }
234 clk = of_clk_get_from_provider(&clkspec);
235 of_node_put(node);
236 if (!IS_ERR(clk)) {
237 c->lk.clk = clk;
238 clkdev_add(&c->lk);
239 } else {
240 if (num_args && !has_clkctrl_data) {
241 struct device_node *np;
242
243 np = of_find_compatible_node(NULL, NULL,
244 "ti,clkctrl");
245 if (np) {
246 has_clkctrl_data = true;
247 of_node_put(np);
248 } else {
249 clkctrl_nodes_missing = true;
250
251 pr_warn("missing clkctrl nodes, please update your dts.\n");
252 continue;
253 }
254 }
255
256 pr_warn("failed to lookup clock node %s, ret=%ld\n",
257 c->node_name, PTR_ERR(clk));
258 }
259 }
260 }
261
262 struct clk_init_item {
263 struct device_node *node;
264 void *user;
265 ti_of_clk_init_cb_t func;
266 struct list_head link;
267 };
268
269 static LIST_HEAD(retry_list);
270
271 /**
272 * ti_clk_retry_init - retries a failed clock init at later phase
273 * @node: device not for the clock
274 * @user: user data pointer
275 * @func: init function to be called for the clock
276 *
277 * Adds a failed clock init to the retry list. The retry list is parsed
278 * once all the other clocks have been initialized.
279 */
ti_clk_retry_init(struct device_node * node,void * user,ti_of_clk_init_cb_t func)280 int __init ti_clk_retry_init(struct device_node *node, void *user,
281 ti_of_clk_init_cb_t func)
282 {
283 struct clk_init_item *retry;
284
285 pr_debug("%pOFn: adding to retry list...\n", node);
286 retry = kzalloc(sizeof(*retry), GFP_KERNEL);
287 if (!retry)
288 return -ENOMEM;
289
290 retry->node = node;
291 retry->func = func;
292 retry->user = user;
293 list_add(&retry->link, &retry_list);
294
295 return 0;
296 }
297
298 /**
299 * ti_clk_get_reg_addr - get register address for a clock register
300 * @node: device node for the clock
301 * @index: register index from the clock node
302 * @reg: pointer to target register struct
303 *
304 * Builds clock register address from device tree information, and returns
305 * the data via the provided output pointer @reg. Returns 0 on success,
306 * negative error value on failure.
307 */
ti_clk_get_reg_addr(struct device_node * node,int index,struct clk_omap_reg * reg)308 int ti_clk_get_reg_addr(struct device_node *node, int index,
309 struct clk_omap_reg *reg)
310 {
311 u32 val;
312 int i;
313
314 for (i = 0; i < CLK_MAX_MEMMAPS; i++) {
315 if (clocks_node_ptr[i] == node->parent)
316 break;
317 if (clocks_node_ptr[i] == node->parent->parent)
318 break;
319 }
320
321 if (i == CLK_MAX_MEMMAPS) {
322 pr_err("clk-provider not found for %pOFn!\n", node);
323 return -ENOENT;
324 }
325
326 reg->index = i;
327
328 if (of_property_read_u32_index(node, "reg", index, &val)) {
329 if (of_property_read_u32_index(node->parent, "reg",
330 index, &val)) {
331 pr_err("%pOFn or parent must have reg[%d]!\n",
332 node, index);
333 return -EINVAL;
334 }
335 }
336
337 reg->offset = val;
338 reg->ptr = NULL;
339
340 return 0;
341 }
342
ti_clk_latch(struct clk_omap_reg * reg,s8 shift)343 void ti_clk_latch(struct clk_omap_reg *reg, s8 shift)
344 {
345 u32 latch;
346
347 if (shift < 0)
348 return;
349
350 latch = 1 << shift;
351
352 ti_clk_ll_ops->clk_rmw(latch, latch, reg);
353 ti_clk_ll_ops->clk_rmw(0, latch, reg);
354 ti_clk_ll_ops->clk_readl(reg); /* OCP barrier */
355 }
356
357 /**
358 * omap2_clk_provider_init - init master clock provider
359 * @parent: master node
360 * @index: internal index for clk_reg_ops
361 * @syscon: syscon regmap pointer for accessing clock registers
362 * @mem: iomem pointer for the clock provider memory area, only used if
363 * syscon is not provided
364 *
365 * Initializes a master clock IP block. This basically sets up the
366 * mapping from clocks node to the memory map index. All the clocks
367 * are then initialized through the common of_clk_init call, and the
368 * clocks will access their memory maps based on the node layout.
369 * Returns 0 in success.
370 */
omap2_clk_provider_init(struct device_node * parent,int index,struct regmap * syscon,void __iomem * mem)371 int __init omap2_clk_provider_init(struct device_node *parent, int index,
372 struct regmap *syscon, void __iomem *mem)
373 {
374 struct device_node *clocks;
375 struct clk_iomap *io;
376
377 /* get clocks for this parent */
378 clocks = of_get_child_by_name(parent, "clocks");
379 if (!clocks) {
380 pr_err("%pOFn missing 'clocks' child node.\n", parent);
381 return -EINVAL;
382 }
383
384 /* add clocks node info */
385 clocks_node_ptr[index] = clocks;
386
387 io = kzalloc(sizeof(*io), GFP_KERNEL);
388 if (!io)
389 return -ENOMEM;
390
391 io->regmap = syscon;
392 io->mem = mem;
393
394 clk_memmaps[index] = io;
395
396 return 0;
397 }
398
399 /**
400 * omap2_clk_legacy_provider_init - initialize a legacy clock provider
401 * @index: index for the clock provider
402 * @mem: iomem pointer for the clock provider memory area
403 *
404 * Initializes a legacy clock provider memory mapping.
405 */
omap2_clk_legacy_provider_init(int index,void __iomem * mem)406 void __init omap2_clk_legacy_provider_init(int index, void __iomem *mem)
407 {
408 struct clk_iomap *io;
409
410 io = memblock_alloc(sizeof(*io), SMP_CACHE_BYTES);
411 if (!io)
412 panic("%s: Failed to allocate %zu bytes\n", __func__,
413 sizeof(*io));
414
415 io->mem = mem;
416
417 clk_memmaps[index] = io;
418 }
419
420 /**
421 * ti_dt_clk_init_retry_clks - init clocks from the retry list
422 *
423 * Initializes any clocks that have failed to initialize before,
424 * reasons being missing parent node(s) during earlier init. This
425 * typically happens only for DPLLs which need to have both of their
426 * parent clocks ready during init.
427 */
ti_dt_clk_init_retry_clks(void)428 void ti_dt_clk_init_retry_clks(void)
429 {
430 struct clk_init_item *retry;
431 struct clk_init_item *tmp;
432 int retries = 5;
433
434 while (!list_empty(&retry_list) && retries) {
435 list_for_each_entry_safe(retry, tmp, &retry_list, link) {
436 pr_debug("retry-init: %pOFn\n", retry->node);
437 retry->func(retry->user, retry->node);
438 list_del(&retry->link);
439 kfree(retry);
440 }
441 retries--;
442 }
443 }
444
445 static const struct of_device_id simple_clk_match_table[] __initconst = {
446 { .compatible = "fixed-clock" },
447 { .compatible = "fixed-factor-clock" },
448 { }
449 };
450
451 /**
452 * ti_dt_clk_name - init clock name from first output name or node name
453 * @np: device node
454 *
455 * Use the first clock-output-name for the clock name if found. Fall back
456 * to legacy naming based on node name.
457 */
ti_dt_clk_name(struct device_node * np)458 const char *ti_dt_clk_name(struct device_node *np)
459 {
460 const char *name;
461
462 if (!of_property_read_string_index(np, "clock-output-names", 0,
463 &name))
464 return name;
465
466 return np->name;
467 }
468
469 /**
470 * ti_clk_add_aliases - setup clock aliases
471 *
472 * Sets up any missing clock aliases. No return value.
473 */
ti_clk_add_aliases(void)474 void __init ti_clk_add_aliases(void)
475 {
476 struct device_node *np;
477 struct clk *clk;
478
479 for_each_matching_node(np, simple_clk_match_table) {
480 struct of_phandle_args clkspec;
481
482 clkspec.np = np;
483 clk = of_clk_get_from_provider(&clkspec);
484
485 ti_clk_add_alias(NULL, clk, ti_dt_clk_name(np));
486 }
487 }
488
489 /**
490 * ti_clk_setup_features - setup clock features flags
491 * @features: features definition to use
492 *
493 * Initializes the clock driver features flags based on platform
494 * provided data. No return value.
495 */
ti_clk_setup_features(struct ti_clk_features * features)496 void __init ti_clk_setup_features(struct ti_clk_features *features)
497 {
498 memcpy(&ti_clk_features, features, sizeof(*features));
499 }
500
501 /**
502 * ti_clk_get_features - get clock driver features flags
503 *
504 * Get TI clock driver features description. Returns a pointer
505 * to the current feature setup.
506 */
ti_clk_get_features(void)507 const struct ti_clk_features *ti_clk_get_features(void)
508 {
509 return &ti_clk_features;
510 }
511
512 /**
513 * omap2_clk_enable_init_clocks - prepare & enable a list of clocks
514 * @clk_names: ptr to an array of strings of clock names to enable
515 * @num_clocks: number of clock names in @clk_names
516 *
517 * Prepare and enable a list of clocks, named by @clk_names. No
518 * return value. XXX Deprecated; only needed until these clocks are
519 * properly claimed and enabled by the drivers or core code that uses
520 * them. XXX What code disables & calls clk_put on these clocks?
521 */
omap2_clk_enable_init_clocks(const char ** clk_names,u8 num_clocks)522 void omap2_clk_enable_init_clocks(const char **clk_names, u8 num_clocks)
523 {
524 struct clk *init_clk;
525 int i;
526
527 for (i = 0; i < num_clocks; i++) {
528 init_clk = clk_get(NULL, clk_names[i]);
529 if (WARN(IS_ERR(init_clk), "could not find init clock %s\n",
530 clk_names[i]))
531 continue;
532 clk_prepare_enable(init_clk);
533 }
534 }
535
536 /**
537 * ti_clk_add_alias - add a clock alias for a TI clock
538 * @dev: device alias for this clock
539 * @clk: clock handle to create alias for
540 * @con: connection ID for this clock
541 *
542 * Creates a clock alias for a TI clock. Allocates the clock lookup entry
543 * and assigns the data to it. Returns 0 if successful, negative error
544 * value otherwise.
545 */
ti_clk_add_alias(struct device * dev,struct clk * clk,const char * con)546 int ti_clk_add_alias(struct device *dev, struct clk *clk, const char *con)
547 {
548 struct clk_lookup *cl;
549
550 if (!clk)
551 return 0;
552
553 if (IS_ERR(clk))
554 return PTR_ERR(clk);
555
556 cl = kzalloc(sizeof(*cl), GFP_KERNEL);
557 if (!cl)
558 return -ENOMEM;
559
560 if (dev)
561 cl->dev_id = dev_name(dev);
562 cl->con_id = con;
563 cl->clk = clk;
564
565 clkdev_add(cl);
566
567 return 0;
568 }
569
570 /**
571 * ti_clk_register - register a TI clock to the common clock framework
572 * @dev: device for this clock
573 * @hw: hardware clock handle
574 * @con: connection ID for this clock
575 *
576 * Registers a TI clock to the common clock framework, and adds a clock
577 * alias for it. Returns a handle to the registered clock if successful,
578 * ERR_PTR value in failure.
579 */
ti_clk_register(struct device * dev,struct clk_hw * hw,const char * con)580 struct clk *ti_clk_register(struct device *dev, struct clk_hw *hw,
581 const char *con)
582 {
583 struct clk *clk;
584 int ret;
585
586 clk = clk_register(dev, hw);
587 if (IS_ERR(clk))
588 return clk;
589
590 ret = ti_clk_add_alias(dev, clk, con);
591 if (ret) {
592 clk_unregister(clk);
593 return ERR_PTR(ret);
594 }
595
596 return clk;
597 }
598
599 /**
600 * ti_clk_register_omap_hw - register a clk_hw_omap to the clock framework
601 * @dev: device for this clock
602 * @hw: hardware clock handle
603 * @con: connection ID for this clock
604 *
605 * Registers a clk_hw_omap clock to the clock framewor, adds a clock alias
606 * for it, and adds the list to the available clk_hw_omap type clocks.
607 * Returns a handle to the registered clock if successful, ERR_PTR value
608 * in failure.
609 */
ti_clk_register_omap_hw(struct device * dev,struct clk_hw * hw,const char * con)610 struct clk *ti_clk_register_omap_hw(struct device *dev, struct clk_hw *hw,
611 const char *con)
612 {
613 struct clk *clk;
614 struct clk_hw_omap *oclk;
615
616 clk = ti_clk_register(dev, hw, con);
617 if (IS_ERR(clk))
618 return clk;
619
620 oclk = to_clk_hw_omap(hw);
621
622 list_add(&oclk->node, &clk_hw_omap_clocks);
623
624 return clk;
625 }
626
627 /**
628 * omap2_clk_for_each - call function for each registered clk_hw_omap
629 * @fn: pointer to a callback function
630 *
631 * Call @fn for each registered clk_hw_omap, passing @hw to each
632 * function. @fn must return 0 for success or any other value for
633 * failure. If @fn returns non-zero, the iteration across clocks
634 * will stop and the non-zero return value will be passed to the
635 * caller of omap2_clk_for_each().
636 */
omap2_clk_for_each(int (* fn)(struct clk_hw_omap * hw))637 int omap2_clk_for_each(int (*fn)(struct clk_hw_omap *hw))
638 {
639 int ret;
640 struct clk_hw_omap *hw;
641
642 list_for_each_entry(hw, &clk_hw_omap_clocks, node) {
643 ret = (*fn)(hw);
644 if (ret)
645 break;
646 }
647
648 return ret;
649 }
650
651 /**
652 * omap2_clk_is_hw_omap - check if the provided clk_hw is OMAP clock
653 * @hw: clk_hw to check if it is an omap clock or not
654 *
655 * Checks if the provided clk_hw is OMAP clock or not. Returns true if
656 * it is, false otherwise.
657 */
omap2_clk_is_hw_omap(struct clk_hw * hw)658 bool omap2_clk_is_hw_omap(struct clk_hw *hw)
659 {
660 struct clk_hw_omap *oclk;
661
662 list_for_each_entry(oclk, &clk_hw_omap_clocks, node) {
663 if (&oclk->hw == hw)
664 return true;
665 }
666
667 return false;
668 }
669