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