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