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