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