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