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