xref: /openbmc/linux/drivers/clk/ti/clk.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
15a729246SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2a8aceccbSTero Kristo /*
3a8aceccbSTero Kristo  * TI clock support
4a8aceccbSTero Kristo  *
5a8aceccbSTero Kristo  * Copyright (C) 2013 Texas Instruments, Inc.
6a8aceccbSTero Kristo  *
7a8aceccbSTero Kristo  * Tero Kristo <t-kristo@ti.com>
8a8aceccbSTero Kristo  */
9a8aceccbSTero Kristo 
101b29e601SStephen Boyd #include <linux/clk.h>
11a8aceccbSTero Kristo #include <linux/clk-provider.h>
12a8aceccbSTero Kristo #include <linux/clkdev.h>
13a8aceccbSTero Kristo #include <linux/clk/ti.h>
1462e59c4eSStephen Boyd #include <linux/io.h>
15a8aceccbSTero Kristo #include <linux/of.h>
16819b4861STero Kristo #include <linux/of_address.h>
17819b4861STero Kristo #include <linux/list.h>
18989feafbSTero Kristo #include <linux/regmap.h>
19*bb362d0eSAndy Shevchenko #include <linux/string_helpers.h>
2057c8a661SMike Rapoport #include <linux/memblock.h>
2121f0bf2dSTero Kristo #include <linux/device.h>
22a8aceccbSTero Kristo 
23c82f8957STero Kristo #include "clock.h"
24c82f8957STero Kristo 
25a8aceccbSTero Kristo #undef pr_fmt
26a8aceccbSTero Kristo #define pr_fmt(fmt) "%s: " fmt, __func__
27a8aceccbSTero Kristo 
2877b773aeSTero Kristo static LIST_HEAD(clk_hw_omap_clocks);
29819b4861STero Kristo struct ti_clk_ll_ops *ti_clk_ll_ops;
30c08ee14cSTero Kristo static struct device_node *clocks_node_ptr[CLK_MAX_MEMMAPS];
31819b4861STero Kristo 
3247b00dcfSTero Kristo struct ti_clk_features ti_clk_features;
33f3b19aa5STero Kristo 
34989feafbSTero Kristo struct clk_iomap {
35989feafbSTero Kristo 	struct regmap *regmap;
36989feafbSTero Kristo 	void __iomem *mem;
37989feafbSTero Kristo };
38989feafbSTero Kristo 
39989feafbSTero Kristo static struct clk_iomap *clk_memmaps[CLK_MAX_MEMMAPS];
40989feafbSTero Kristo 
clk_memmap_writel(u32 val,const struct clk_omap_reg * reg)416c0afb50STero Kristo static void clk_memmap_writel(u32 val, const struct clk_omap_reg *reg)
42989feafbSTero Kristo {
436c0afb50STero Kristo 	struct clk_iomap *io = clk_memmaps[reg->index];
44989feafbSTero Kristo 
456c0afb50STero Kristo 	if (reg->ptr)
466c0afb50STero Kristo 		writel_relaxed(val, reg->ptr);
476c0afb50STero Kristo 	else if (io->regmap)
486c0afb50STero Kristo 		regmap_write(io->regmap, reg->offset, val);
49989feafbSTero Kristo 	else
506c0afb50STero Kristo 		writel_relaxed(val, io->mem + reg->offset);
51989feafbSTero Kristo }
52989feafbSTero Kristo 
_clk_rmw(u32 val,u32 mask,void __iomem * ptr)534902c202STero Kristo static void _clk_rmw(u32 val, u32 mask, void __iomem *ptr)
544902c202STero Kristo {
554902c202STero Kristo 	u32 v;
564902c202STero Kristo 
574902c202STero Kristo 	v = readl_relaxed(ptr);
584902c202STero Kristo 	v &= ~mask;
594902c202STero Kristo 	v |= val;
604902c202STero Kristo 	writel_relaxed(v, ptr);
614902c202STero Kristo }
624902c202STero Kristo 
clk_memmap_rmw(u32 val,u32 mask,const struct clk_omap_reg * reg)634902c202STero Kristo static void clk_memmap_rmw(u32 val, u32 mask, const struct clk_omap_reg *reg)
644902c202STero Kristo {
654902c202STero Kristo 	struct clk_iomap *io = clk_memmaps[reg->index];
664902c202STero Kristo 
674902c202STero Kristo 	if (reg->ptr) {
684902c202STero Kristo 		_clk_rmw(val, mask, reg->ptr);
694902c202STero Kristo 	} else if (io->regmap) {
704902c202STero Kristo 		regmap_update_bits(io->regmap, reg->offset, mask, val);
714902c202STero Kristo 	} else {
724902c202STero Kristo 		_clk_rmw(val, mask, io->mem + reg->offset);
734902c202STero Kristo 	}
744902c202STero Kristo }
754902c202STero Kristo 
clk_memmap_readl(const struct clk_omap_reg * reg)766c0afb50STero Kristo static u32 clk_memmap_readl(const struct clk_omap_reg *reg)
77989feafbSTero Kristo {
78989feafbSTero Kristo 	u32 val;
796c0afb50STero Kristo 	struct clk_iomap *io = clk_memmaps[reg->index];
80989feafbSTero Kristo 
816c0afb50STero Kristo 	if (reg->ptr)
826c0afb50STero Kristo 		val = readl_relaxed(reg->ptr);
836c0afb50STero Kristo 	else if (io->regmap)
846c0afb50STero Kristo 		regmap_read(io->regmap, reg->offset, &val);
85989feafbSTero Kristo 	else
866c0afb50STero Kristo 		val = readl_relaxed(io->mem + reg->offset);
87989feafbSTero Kristo 
88989feafbSTero Kristo 	return val;
89989feafbSTero Kristo }
90989feafbSTero Kristo 
91a8aceccbSTero Kristo /**
92e9e63088STero Kristo  * ti_clk_setup_ll_ops - setup low level clock operations
93e9e63088STero Kristo  * @ops: low level clock ops descriptor
94e9e63088STero Kristo  *
95e9e63088STero Kristo  * Sets up low level clock operations for TI clock driver. This is used
96e9e63088STero Kristo  * to provide various callbacks for the clock driver towards platform
97e9e63088STero Kristo  * specific code. Returns 0 on success, -EBUSY if ll_ops have been
98e9e63088STero Kristo  * registered already.
99e9e63088STero Kristo  */
ti_clk_setup_ll_ops(struct ti_clk_ll_ops * ops)100e9e63088STero Kristo int ti_clk_setup_ll_ops(struct ti_clk_ll_ops *ops)
101e9e63088STero Kristo {
102e9e63088STero Kristo 	if (ti_clk_ll_ops) {
103e9e63088STero Kristo 		pr_err("Attempt to register ll_ops multiple times.\n");
104e9e63088STero Kristo 		return -EBUSY;
105e9e63088STero Kristo 	}
106e9e63088STero Kristo 
107e9e63088STero Kristo 	ti_clk_ll_ops = ops;
108989feafbSTero Kristo 	ops->clk_readl = clk_memmap_readl;
109989feafbSTero Kristo 	ops->clk_writel = clk_memmap_writel;
1104902c202STero Kristo 	ops->clk_rmw = clk_memmap_rmw;
111e9e63088STero Kristo 
112e9e63088STero Kristo 	return 0;
113e9e63088STero Kristo }
114e9e63088STero Kristo 
11551f661efSTony Lindgren /*
11651f661efSTony Lindgren  * Eventually we could standardize to using '_' for clk-*.c files to follow the
11751f661efSTony Lindgren  * TRM naming and leave out the tmp name here.
11851f661efSTony Lindgren  */
ti_find_clock_provider(struct device_node * from,const char * name)11951f661efSTony Lindgren static struct device_node *ti_find_clock_provider(struct device_node *from,
12051f661efSTony Lindgren 						  const char *name)
12151f661efSTony Lindgren {
12251f661efSTony Lindgren 	struct device_node *np;
12351f661efSTony Lindgren 	bool found = false;
12451f661efSTony Lindgren 	const char *n;
12551f661efSTony Lindgren 	char *tmp;
12651f661efSTony Lindgren 
127*bb362d0eSAndy Shevchenko 	tmp = kstrdup_and_replace(name, '-', '_', GFP_KERNEL);
12851f661efSTony Lindgren 	if (!tmp)
12951f661efSTony Lindgren 		return NULL;
13051f661efSTony Lindgren 
13151f661efSTony Lindgren 	/* Node named "clock" with "clock-output-names" */
13251f661efSTony Lindgren 	for_each_of_allnodes_from(from, np) {
13351f661efSTony Lindgren 		if (of_property_read_string_index(np, "clock-output-names",
13451f661efSTony Lindgren 						  0, &n))
13551f661efSTony Lindgren 			continue;
13651f661efSTony Lindgren 
13751f661efSTony Lindgren 		if (!strncmp(n, tmp, strlen(tmp))) {
13826f2da0dSTony Lindgren 			of_node_get(np);
13951f661efSTony Lindgren 			found = true;
14051f661efSTony Lindgren 			break;
14151f661efSTony Lindgren 		}
14251f661efSTony Lindgren 	}
14351f661efSTony Lindgren 	kfree(tmp);
14451f661efSTony Lindgren 
145058a3996SLiang He 	if (found) {
146058a3996SLiang He 		of_node_put(from);
14751f661efSTony Lindgren 		return np;
148058a3996SLiang He 	}
14951f661efSTony Lindgren 
15051f661efSTony Lindgren 	/* Fall back to using old node name base provider name */
15151f661efSTony Lindgren 	return of_find_node_by_name(from, name);
15251f661efSTony Lindgren }
15351f661efSTony Lindgren 
154a8aceccbSTero Kristo /**
155a8aceccbSTero Kristo  * ti_dt_clocks_register - register DT alias clocks during boot
156a8aceccbSTero Kristo  * @oclks: list of clocks to register
157a8aceccbSTero Kristo  *
158a8aceccbSTero Kristo  * Register alias or non-standard DT clock entries during boot. By
15951f661efSTony Lindgren  * default, DT clocks are found based on their clock-output-names
16051f661efSTony Lindgren  * property, or the clock node name for legacy cases. If any
161a8aceccbSTero Kristo  * additional con-id / dev-id -> clock mapping is required, use this
162a8aceccbSTero Kristo  * function to list these.
163a8aceccbSTero Kristo  */
ti_dt_clocks_register(struct ti_dt_clk oclks[])164a8aceccbSTero Kristo void __init ti_dt_clocks_register(struct ti_dt_clk oclks[])
165a8aceccbSTero Kristo {
166a8aceccbSTero Kristo 	struct ti_dt_clk *c;
16780864594STony Lindgren 	struct device_node *node, *parent, *child;
168a8aceccbSTero Kristo 	struct clk *clk;
169a8aceccbSTero Kristo 	struct of_phandle_args clkspec;
1705b385a45STero Kristo 	char buf[64];
1715b385a45STero Kristo 	char *ptr;
1725b385a45STero Kristo 	char *tags[2];
1735b385a45STero Kristo 	int i;
1745b385a45STero Kristo 	int num_args;
1755b385a45STero Kristo 	int ret;
1765b385a45STero Kristo 	static bool clkctrl_nodes_missing;
1775b385a45STero Kristo 	static bool has_clkctrl_data;
17847b00dcfSTero Kristo 	static bool compat_mode;
17947b00dcfSTero Kristo 
18047b00dcfSTero Kristo 	compat_mode = ti_clk_get_features()->flags & TI_CLK_CLKCTRL_COMPAT;
181a8aceccbSTero Kristo 
182a8aceccbSTero Kristo 	for (c = oclks; c->node_name != NULL; c++) {
1835b385a45STero Kristo 		strcpy(buf, c->node_name);
1845b385a45STero Kristo 		ptr = buf;
1855b385a45STero Kristo 		for (i = 0; i < 2; i++)
1865b385a45STero Kristo 			tags[i] = NULL;
1875b385a45STero Kristo 		num_args = 0;
1885b385a45STero Kristo 		while (*ptr) {
1895b385a45STero Kristo 			if (*ptr == ':') {
1905b385a45STero Kristo 				if (num_args >= 2) {
1915b385a45STero Kristo 					pr_warn("Bad number of tags on %s\n",
1925b385a45STero Kristo 						c->node_name);
1935b385a45STero Kristo 					return;
1945b385a45STero Kristo 				}
1955b385a45STero Kristo 				tags[num_args++] = ptr + 1;
1965b385a45STero Kristo 				*ptr = 0;
1975b385a45STero Kristo 			}
1985b385a45STero Kristo 			ptr++;
1995b385a45STero Kristo 		}
2005b385a45STero Kristo 
2015b385a45STero Kristo 		if (num_args && clkctrl_nodes_missing)
2025b385a45STero Kristo 			continue;
2035b385a45STero Kristo 
20451f661efSTony Lindgren 		node = ti_find_clock_provider(NULL, buf);
20547b00dcfSTero Kristo 		if (num_args && compat_mode) {
20600a461ccSJohan Hovold 			parent = node;
20780864594STony Lindgren 			child = of_get_child_by_name(parent, "clock");
20880864594STony Lindgren 			if (!child)
20980864594STony Lindgren 				child = of_get_child_by_name(parent, "clk");
21080864594STony Lindgren 			if (child) {
21100a461ccSJohan Hovold 				of_node_put(parent);
21280864594STony Lindgren 				node = child;
21380864594STony Lindgren 			}
21400a461ccSJohan Hovold 		}
21500a461ccSJohan Hovold 
216a8aceccbSTero Kristo 		clkspec.np = node;
2175b385a45STero Kristo 		clkspec.args_count = num_args;
2185b385a45STero Kristo 		for (i = 0; i < num_args; i++) {
2195b385a45STero Kristo 			ret = kstrtoint(tags[i], i ? 10 : 16, clkspec.args + i);
2205b385a45STero Kristo 			if (ret) {
2215b385a45STero Kristo 				pr_warn("Bad tag in %s at %d: %s\n",
2225b385a45STero Kristo 					c->node_name, i, tags[i]);
22300a461ccSJohan Hovold 				of_node_put(node);
2245b385a45STero Kristo 				return;
2255b385a45STero Kristo 			}
2265b385a45STero Kristo 		}
227a8aceccbSTero Kristo 		clk = of_clk_get_from_provider(&clkspec);
22800a461ccSJohan Hovold 		of_node_put(node);
229a8aceccbSTero Kristo 		if (!IS_ERR(clk)) {
230a8aceccbSTero Kristo 			c->lk.clk = clk;
231a8aceccbSTero Kristo 			clkdev_add(&c->lk);
232a8aceccbSTero Kristo 		} else {
2335b385a45STero Kristo 			if (num_args && !has_clkctrl_data) {
2342274d800SYangtao Li 				struct device_node *np;
2352274d800SYangtao Li 
2362274d800SYangtao Li 				np = of_find_compatible_node(NULL, NULL,
2372274d800SYangtao Li 							     "ti,clkctrl");
2382274d800SYangtao Li 				if (np) {
2395b385a45STero Kristo 					has_clkctrl_data = true;
2402274d800SYangtao Li 					of_node_put(np);
2415b385a45STero Kristo 				} else {
2425b385a45STero Kristo 					clkctrl_nodes_missing = true;
2435b385a45STero Kristo 
2445b385a45STero Kristo 					pr_warn("missing clkctrl nodes, please update your dts.\n");
2455b385a45STero Kristo 					continue;
2465b385a45STero Kristo 				}
2475b385a45STero Kristo 			}
2485b385a45STero Kristo 
2495b385a45STero Kristo 			pr_warn("failed to lookup clock node %s, ret=%ld\n",
2505b385a45STero Kristo 				c->node_name, PTR_ERR(clk));
251a8aceccbSTero Kristo 		}
252a8aceccbSTero Kristo 	}
253a8aceccbSTero Kristo }
254819b4861STero Kristo 
255819b4861STero Kristo struct clk_init_item {
256819b4861STero Kristo 	struct device_node *node;
257ffb009b2STero Kristo 	void *user;
258819b4861STero Kristo 	ti_of_clk_init_cb_t func;
259819b4861STero Kristo 	struct list_head link;
260819b4861STero Kristo };
261819b4861STero Kristo 
262819b4861STero Kristo static LIST_HEAD(retry_list);
263819b4861STero Kristo 
264819b4861STero Kristo /**
265819b4861STero Kristo  * ti_clk_retry_init - retries a failed clock init at later phase
26653949843SDario Binacchi  * @node: device node for the clock
267ffb009b2STero Kristo  * @user: user data pointer
268819b4861STero Kristo  * @func: init function to be called for the clock
269819b4861STero Kristo  *
270819b4861STero Kristo  * Adds a failed clock init to the retry list. The retry list is parsed
271819b4861STero Kristo  * once all the other clocks have been initialized.
272819b4861STero Kristo  */
ti_clk_retry_init(struct device_node * node,void * user,ti_of_clk_init_cb_t func)273ffb009b2STero Kristo int __init ti_clk_retry_init(struct device_node *node, void *user,
274819b4861STero Kristo 			     ti_of_clk_init_cb_t func)
275819b4861STero Kristo {
276819b4861STero Kristo 	struct clk_init_item *retry;
277819b4861STero Kristo 
278e665f029SRob Herring 	pr_debug("%pOFn: adding to retry list...\n", node);
279819b4861STero Kristo 	retry = kzalloc(sizeof(*retry), GFP_KERNEL);
280819b4861STero Kristo 	if (!retry)
281819b4861STero Kristo 		return -ENOMEM;
282819b4861STero Kristo 
283819b4861STero Kristo 	retry->node = node;
284819b4861STero Kristo 	retry->func = func;
285ffb009b2STero Kristo 	retry->user = user;
286819b4861STero Kristo 	list_add(&retry->link, &retry_list);
287819b4861STero Kristo 
288819b4861STero Kristo 	return 0;
289819b4861STero Kristo }
290819b4861STero Kristo 
291819b4861STero Kristo /**
292819b4861STero Kristo  * ti_clk_get_reg_addr - get register address for a clock register
293819b4861STero Kristo  * @node: device node for the clock
294819b4861STero Kristo  * @index: register index from the clock node
2956c0afb50STero Kristo  * @reg: pointer to target register struct
296819b4861STero Kristo  *
2976c0afb50STero Kristo  * Builds clock register address from device tree information, and returns
2986c0afb50STero Kristo  * the data via the provided output pointer @reg. Returns 0 on success,
2996c0afb50STero Kristo  * negative error value on failure.
300819b4861STero Kristo  */
ti_clk_get_reg_addr(struct device_node * node,int index,struct clk_omap_reg * reg)3016c0afb50STero Kristo int ti_clk_get_reg_addr(struct device_node *node, int index,
3026c0afb50STero Kristo 			struct clk_omap_reg *reg)
303819b4861STero Kristo {
304819b4861STero Kristo 	u32 val;
305c08ee14cSTero Kristo 	int i;
306819b4861STero Kristo 
307c08ee14cSTero Kristo 	for (i = 0; i < CLK_MAX_MEMMAPS; i++) {
308c08ee14cSTero Kristo 		if (clocks_node_ptr[i] == node->parent)
309c08ee14cSTero Kristo 			break;
310274d6798STony Lindgren 		if (clocks_node_ptr[i] == node->parent->parent)
311274d6798STony Lindgren 			break;
312c08ee14cSTero Kristo 	}
313c08ee14cSTero Kristo 
314c08ee14cSTero Kristo 	if (i == CLK_MAX_MEMMAPS) {
315e665f029SRob Herring 		pr_err("clk-provider not found for %pOFn!\n", node);
3166c0afb50STero Kristo 		return -ENOENT;
317c08ee14cSTero Kristo 	}
318c08ee14cSTero Kristo 
319c08ee14cSTero Kristo 	reg->index = i;
320819b4861STero Kristo 
321819b4861STero Kristo 	if (of_property_read_u32_index(node, "reg", index, &val)) {
322274d6798STony Lindgren 		if (of_property_read_u32_index(node->parent, "reg",
323274d6798STony Lindgren 					       index, &val)) {
324274d6798STony Lindgren 			pr_err("%pOFn or parent must have reg[%d]!\n",
325274d6798STony Lindgren 			       node, index);
3266c0afb50STero Kristo 			return -EINVAL;
327819b4861STero Kristo 		}
328274d6798STony Lindgren 	}
329819b4861STero Kristo 
330819b4861STero Kristo 	reg->offset = val;
3316c0afb50STero Kristo 	reg->ptr = NULL;
332819b4861STero Kristo 
3336c0afb50STero Kristo 	return 0;
334819b4861STero Kristo }
335819b4861STero Kristo 
ti_clk_latch(struct clk_omap_reg * reg,s8 shift)336e31922edSTero Kristo void ti_clk_latch(struct clk_omap_reg *reg, s8 shift)
337e31922edSTero Kristo {
338e31922edSTero Kristo 	u32 latch;
339e31922edSTero Kristo 
340e31922edSTero Kristo 	if (shift < 0)
341e31922edSTero Kristo 		return;
342e31922edSTero Kristo 
343e31922edSTero Kristo 	latch = 1 << shift;
344e31922edSTero Kristo 
345e31922edSTero Kristo 	ti_clk_ll_ops->clk_rmw(latch, latch, reg);
346e31922edSTero Kristo 	ti_clk_ll_ops->clk_rmw(0, latch, reg);
347e31922edSTero Kristo 	ti_clk_ll_ops->clk_readl(reg); /* OCP barrier */
348e31922edSTero Kristo }
349e31922edSTero Kristo 
350819b4861STero Kristo /**
351989feafbSTero Kristo  * omap2_clk_provider_init - init master clock provider
352819b4861STero Kristo  * @parent: master node
353819b4861STero Kristo  * @index: internal index for clk_reg_ops
354989feafbSTero Kristo  * @syscon: syscon regmap pointer for accessing clock registers
355989feafbSTero Kristo  * @mem: iomem pointer for the clock provider memory area, only used if
356989feafbSTero Kristo  *       syscon is not provided
357819b4861STero Kristo  *
358c08ee14cSTero Kristo  * Initializes a master clock IP block. This basically sets up the
359c08ee14cSTero Kristo  * mapping from clocks node to the memory map index. All the clocks
360c08ee14cSTero Kristo  * are then initialized through the common of_clk_init call, and the
361c08ee14cSTero Kristo  * clocks will access their memory maps based on the node layout.
362989feafbSTero Kristo  * Returns 0 in success.
363819b4861STero Kristo  */
omap2_clk_provider_init(struct device_node * parent,int index,struct regmap * syscon,void __iomem * mem)364989feafbSTero Kristo int __init omap2_clk_provider_init(struct device_node *parent, int index,
365989feafbSTero Kristo 				   struct regmap *syscon, void __iomem *mem)
366819b4861STero Kristo {
367819b4861STero Kristo 	struct device_node *clocks;
368989feafbSTero Kristo 	struct clk_iomap *io;
369819b4861STero Kristo 
370819b4861STero Kristo 	/* get clocks for this parent */
371819b4861STero Kristo 	clocks = of_get_child_by_name(parent, "clocks");
372819b4861STero Kristo 	if (!clocks) {
373e665f029SRob Herring 		pr_err("%pOFn missing 'clocks' child node.\n", parent);
374989feafbSTero Kristo 		return -EINVAL;
375819b4861STero Kristo 	}
376819b4861STero Kristo 
377c08ee14cSTero Kristo 	/* add clocks node info */
378c08ee14cSTero Kristo 	clocks_node_ptr[index] = clocks;
379989feafbSTero Kristo 
380989feafbSTero Kristo 	io = kzalloc(sizeof(*io), GFP_KERNEL);
381f645f72dSStephen Boyd 	if (!io)
382f645f72dSStephen Boyd 		return -ENOMEM;
383989feafbSTero Kristo 
384989feafbSTero Kristo 	io->regmap = syscon;
385989feafbSTero Kristo 	io->mem = mem;
386989feafbSTero Kristo 
387989feafbSTero Kristo 	clk_memmaps[index] = io;
388989feafbSTero Kristo 
389989feafbSTero Kristo 	return 0;
390989feafbSTero Kristo }
391989feafbSTero Kristo 
392989feafbSTero Kristo /**
393989feafbSTero Kristo  * omap2_clk_legacy_provider_init - initialize a legacy clock provider
394989feafbSTero Kristo  * @index: index for the clock provider
395989feafbSTero Kristo  * @mem: iomem pointer for the clock provider memory area
396989feafbSTero Kristo  *
397989feafbSTero Kristo  * Initializes a legacy clock provider memory mapping.
398989feafbSTero Kristo  */
omap2_clk_legacy_provider_init(int index,void __iomem * mem)399989feafbSTero Kristo void __init omap2_clk_legacy_provider_init(int index, void __iomem *mem)
400989feafbSTero Kristo {
401989feafbSTero Kristo 	struct clk_iomap *io;
402989feafbSTero Kristo 
4037e1c4e27SMike Rapoport 	io = memblock_alloc(sizeof(*io), SMP_CACHE_BYTES);
4048a7f97b9SMike Rapoport 	if (!io)
4058a7f97b9SMike Rapoport 		panic("%s: Failed to allocate %zu bytes\n", __func__,
4068a7f97b9SMike Rapoport 		      sizeof(*io));
407989feafbSTero Kristo 
408989feafbSTero Kristo 	io->mem = mem;
409989feafbSTero Kristo 
410989feafbSTero Kristo 	clk_memmaps[index] = io;
411819b4861STero Kristo }
412819b4861STero Kristo 
413c08ee14cSTero Kristo /**
414c08ee14cSTero Kristo  * ti_dt_clk_init_retry_clks - init clocks from the retry list
415c08ee14cSTero Kristo  *
416c08ee14cSTero Kristo  * Initializes any clocks that have failed to initialize before,
417c08ee14cSTero Kristo  * reasons being missing parent node(s) during earlier init. This
418c08ee14cSTero Kristo  * typically happens only for DPLLs which need to have both of their
419c08ee14cSTero Kristo  * parent clocks ready during init.
420c08ee14cSTero Kristo  */
ti_dt_clk_init_retry_clks(void)421c08ee14cSTero Kristo void ti_dt_clk_init_retry_clks(void)
422c08ee14cSTero Kristo {
423c08ee14cSTero Kristo 	struct clk_init_item *retry;
424c08ee14cSTero Kristo 	struct clk_init_item *tmp;
425c08ee14cSTero Kristo 	int retries = 5;
426c08ee14cSTero Kristo 
427c08ee14cSTero Kristo 	while (!list_empty(&retry_list) && retries) {
428819b4861STero Kristo 		list_for_each_entry_safe(retry, tmp, &retry_list, link) {
429e665f029SRob Herring 			pr_debug("retry-init: %pOFn\n", retry->node);
430ffb009b2STero Kristo 			retry->func(retry->user, retry->node);
431819b4861STero Kristo 			list_del(&retry->link);
432819b4861STero Kristo 			kfree(retry);
433819b4861STero Kristo 		}
434c08ee14cSTero Kristo 		retries--;
435c08ee14cSTero Kristo 	}
436819b4861STero Kristo }
437c82f8957STero Kristo 
438fc04f27dSArnd Bergmann static const struct of_device_id simple_clk_match_table[] __initconst = {
439fc04f27dSArnd Bergmann 	{ .compatible = "fixed-clock" },
440fc04f27dSArnd Bergmann 	{ .compatible = "fixed-factor-clock" },
441fc04f27dSArnd Bergmann 	{ }
442fc04f27dSArnd Bergmann };
443fc04f27dSArnd Bergmann 
444f3b19aa5STero Kristo /**
4452c159332STony Lindgren  * ti_dt_clk_name - init clock name from first output name or node name
4462c159332STony Lindgren  * @np: device node
4472c159332STony Lindgren  *
4482c159332STony Lindgren  * Use the first clock-output-name for the clock name if found. Fall back
4492c159332STony Lindgren  * to legacy naming based on node name.
4502c159332STony Lindgren  */
ti_dt_clk_name(struct device_node * np)4512c159332STony Lindgren const char *ti_dt_clk_name(struct device_node *np)
4522c159332STony Lindgren {
4532c159332STony Lindgren 	const char *name;
4542c159332STony Lindgren 
4552c159332STony Lindgren 	if (!of_property_read_string_index(np, "clock-output-names", 0,
4562c159332STony Lindgren 					   &name))
4572c159332STony Lindgren 		return name;
4582c159332STony Lindgren 
4592c159332STony Lindgren 	return np->name;
4602c159332STony Lindgren }
4612c159332STony Lindgren 
4622c159332STony Lindgren /**
463c17435c5STero Kristo  * ti_clk_add_aliases - setup clock aliases
464c17435c5STero Kristo  *
465c17435c5STero Kristo  * Sets up any missing clock aliases. No return value.
466c17435c5STero Kristo  */
ti_clk_add_aliases(void)467c17435c5STero Kristo void __init ti_clk_add_aliases(void)
468c17435c5STero Kristo {
469c17435c5STero Kristo 	struct device_node *np;
470c17435c5STero Kristo 	struct clk *clk;
471c17435c5STero Kristo 
472c17435c5STero Kristo 	for_each_matching_node(np, simple_clk_match_table) {
473c17435c5STero Kristo 		struct of_phandle_args clkspec;
474c17435c5STero Kristo 
475c17435c5STero Kristo 		clkspec.np = np;
476c17435c5STero Kristo 		clk = of_clk_get_from_provider(&clkspec);
477c17435c5STero Kristo 
4783400d546SDario Binacchi 		ti_clk_add_alias(clk, ti_dt_clk_name(np));
479c17435c5STero Kristo 	}
480c17435c5STero Kristo }
481c17435c5STero Kristo 
482c17435c5STero Kristo /**
483f3b19aa5STero Kristo  * ti_clk_setup_features - setup clock features flags
484f3b19aa5STero Kristo  * @features: features definition to use
485f3b19aa5STero Kristo  *
486f3b19aa5STero Kristo  * Initializes the clock driver features flags based on platform
487f3b19aa5STero Kristo  * provided data. No return value.
488f3b19aa5STero Kristo  */
ti_clk_setup_features(struct ti_clk_features * features)489f3b19aa5STero Kristo void __init ti_clk_setup_features(struct ti_clk_features *features)
490f3b19aa5STero Kristo {
491f3b19aa5STero Kristo 	memcpy(&ti_clk_features, features, sizeof(*features));
492f3b19aa5STero Kristo }
493f3b19aa5STero Kristo 
494f3b19aa5STero Kristo /**
495f3b19aa5STero Kristo  * ti_clk_get_features - get clock driver features flags
496f3b19aa5STero Kristo  *
497f3b19aa5STero Kristo  * Get TI clock driver features description. Returns a pointer
498f3b19aa5STero Kristo  * to the current feature setup.
499f3b19aa5STero Kristo  */
ti_clk_get_features(void)500f3b19aa5STero Kristo const struct ti_clk_features *ti_clk_get_features(void)
501f3b19aa5STero Kristo {
502f3b19aa5STero Kristo 	return &ti_clk_features;
503f3b19aa5STero Kristo }
504a5aa8a60STero Kristo 
505a5aa8a60STero Kristo /**
506a5aa8a60STero Kristo  * omap2_clk_enable_init_clocks - prepare & enable a list of clocks
507a5aa8a60STero Kristo  * @clk_names: ptr to an array of strings of clock names to enable
508a5aa8a60STero Kristo  * @num_clocks: number of clock names in @clk_names
509a5aa8a60STero Kristo  *
510a5aa8a60STero Kristo  * Prepare and enable a list of clocks, named by @clk_names.  No
511a5aa8a60STero Kristo  * return value. XXX Deprecated; only needed until these clocks are
512a5aa8a60STero Kristo  * properly claimed and enabled by the drivers or core code that uses
513a5aa8a60STero Kristo  * them.  XXX What code disables & calls clk_put on these clocks?
514a5aa8a60STero Kristo  */
omap2_clk_enable_init_clocks(const char ** clk_names,u8 num_clocks)515a5aa8a60STero Kristo void omap2_clk_enable_init_clocks(const char **clk_names, u8 num_clocks)
516a5aa8a60STero Kristo {
517a5aa8a60STero Kristo 	struct clk *init_clk;
518a5aa8a60STero Kristo 	int i;
519a5aa8a60STero Kristo 
520a5aa8a60STero Kristo 	for (i = 0; i < num_clocks; i++) {
521a5aa8a60STero Kristo 		init_clk = clk_get(NULL, clk_names[i]);
522a5aa8a60STero Kristo 		if (WARN(IS_ERR(init_clk), "could not find init clock %s\n",
523a5aa8a60STero Kristo 			 clk_names[i]))
524a5aa8a60STero Kristo 			continue;
525a5aa8a60STero Kristo 		clk_prepare_enable(init_clk);
526a5aa8a60STero Kristo 	}
527a5aa8a60STero Kristo }
52821f0bf2dSTero Kristo 
52921f0bf2dSTero Kristo /**
53021f0bf2dSTero Kristo  * ti_clk_add_alias - add a clock alias for a TI clock
53121f0bf2dSTero Kristo  * @clk: clock handle to create alias for
53221f0bf2dSTero Kristo  * @con: connection ID for this clock
53321f0bf2dSTero Kristo  *
53421f0bf2dSTero Kristo  * Creates a clock alias for a TI clock. Allocates the clock lookup entry
53521f0bf2dSTero Kristo  * and assigns the data to it. Returns 0 if successful, negative error
53621f0bf2dSTero Kristo  * value otherwise.
53721f0bf2dSTero Kristo  */
ti_clk_add_alias(struct clk * clk,const char * con)5383400d546SDario Binacchi int ti_clk_add_alias(struct clk *clk, const char *con)
53921f0bf2dSTero Kristo {
54021f0bf2dSTero Kristo 	struct clk_lookup *cl;
54121f0bf2dSTero Kristo 
54221f0bf2dSTero Kristo 	if (!clk)
54321f0bf2dSTero Kristo 		return 0;
54421f0bf2dSTero Kristo 
54521f0bf2dSTero Kristo 	if (IS_ERR(clk))
54621f0bf2dSTero Kristo 		return PTR_ERR(clk);
54721f0bf2dSTero Kristo 
54821f0bf2dSTero Kristo 	cl = kzalloc(sizeof(*cl), GFP_KERNEL);
54921f0bf2dSTero Kristo 	if (!cl)
55021f0bf2dSTero Kristo 		return -ENOMEM;
55121f0bf2dSTero Kristo 
55221f0bf2dSTero Kristo 	cl->con_id = con;
55321f0bf2dSTero Kristo 	cl->clk = clk;
55421f0bf2dSTero Kristo 
55521f0bf2dSTero Kristo 	clkdev_add(cl);
55621f0bf2dSTero Kristo 
55721f0bf2dSTero Kristo 	return 0;
55821f0bf2dSTero Kristo }
55921f0bf2dSTero Kristo 
56021f0bf2dSTero Kristo /**
5613400d546SDario Binacchi  * of_ti_clk_register - register a TI clock to the common clock framework
5623400d546SDario Binacchi  * @node: device node for this clock
56321f0bf2dSTero Kristo  * @hw: hardware clock handle
56421f0bf2dSTero Kristo  * @con: connection ID for this clock
56521f0bf2dSTero Kristo  *
56621f0bf2dSTero Kristo  * Registers a TI clock to the common clock framework, and adds a clock
56721f0bf2dSTero Kristo  * alias for it. Returns a handle to the registered clock if successful,
56821f0bf2dSTero Kristo  * ERR_PTR value in failure.
56921f0bf2dSTero Kristo  */
of_ti_clk_register(struct device_node * node,struct clk_hw * hw,const char * con)5703400d546SDario Binacchi struct clk *of_ti_clk_register(struct device_node *node, struct clk_hw *hw,
57121f0bf2dSTero Kristo 			       const char *con)
57221f0bf2dSTero Kristo {
57321f0bf2dSTero Kristo 	struct clk *clk;
57421f0bf2dSTero Kristo 	int ret;
57521f0bf2dSTero Kristo 
5763400d546SDario Binacchi 	ret = of_clk_hw_register(node, hw);
5773400d546SDario Binacchi 	if (ret)
5783400d546SDario Binacchi 		return ERR_PTR(ret);
57921f0bf2dSTero Kristo 
5803400d546SDario Binacchi 	clk = hw->clk;
5813400d546SDario Binacchi 	ret = ti_clk_add_alias(clk, con);
58221f0bf2dSTero Kristo 	if (ret) {
58321f0bf2dSTero Kristo 		clk_unregister(clk);
58421f0bf2dSTero Kristo 		return ERR_PTR(ret);
58521f0bf2dSTero Kristo 	}
58621f0bf2dSTero Kristo 
58721f0bf2dSTero Kristo 	return clk;
58821f0bf2dSTero Kristo }
58977b773aeSTero Kristo 
59077b773aeSTero Kristo /**
5913400d546SDario Binacchi  * of_ti_clk_register_omap_hw - register a clk_hw_omap to the clock framework
5923400d546SDario Binacchi  * @node: device node for this clock
593ead47825STero Kristo  * @hw: hardware clock handle
594ead47825STero Kristo  * @con: connection ID for this clock
59577b773aeSTero Kristo  *
596ead47825STero Kristo  * Registers a clk_hw_omap clock to the clock framewor, adds a clock alias
597ead47825STero Kristo  * for it, and adds the list to the available clk_hw_omap type clocks.
598ead47825STero Kristo  * Returns a handle to the registered clock if successful, ERR_PTR value
599ead47825STero Kristo  * in failure.
60077b773aeSTero Kristo  */
of_ti_clk_register_omap_hw(struct device_node * node,struct clk_hw * hw,const char * con)6013400d546SDario Binacchi struct clk *of_ti_clk_register_omap_hw(struct device_node *node,
6023400d546SDario Binacchi 				       struct clk_hw *hw, const char *con)
60377b773aeSTero Kristo {
604ead47825STero Kristo 	struct clk *clk;
605ead47825STero Kristo 	struct clk_hw_omap *oclk;
60677b773aeSTero Kristo 
6073400d546SDario Binacchi 	clk = of_ti_clk_register(node, hw, con);
608ead47825STero Kristo 	if (IS_ERR(clk))
609ead47825STero Kristo 		return clk;
610ead47825STero Kristo 
611ead47825STero Kristo 	oclk = to_clk_hw_omap(hw);
612ead47825STero Kristo 
613ead47825STero Kristo 	list_add(&oclk->node, &clk_hw_omap_clocks);
614ead47825STero Kristo 
615ead47825STero Kristo 	return clk;
61677b773aeSTero Kristo }
61777b773aeSTero Kristo 
61877b773aeSTero Kristo /**
61977b773aeSTero Kristo  * omap2_clk_for_each - call function for each registered clk_hw_omap
62077b773aeSTero Kristo  * @fn: pointer to a callback function
62177b773aeSTero Kristo  *
62277b773aeSTero Kristo  * Call @fn for each registered clk_hw_omap, passing @hw to each
62377b773aeSTero Kristo  * function.  @fn must return 0 for success or any other value for
62477b773aeSTero Kristo  * failure.  If @fn returns non-zero, the iteration across clocks
62577b773aeSTero Kristo  * will stop and the non-zero return value will be passed to the
62677b773aeSTero Kristo  * caller of omap2_clk_for_each().
62777b773aeSTero Kristo  */
omap2_clk_for_each(int (* fn)(struct clk_hw_omap * hw))62877b773aeSTero Kristo int omap2_clk_for_each(int (*fn)(struct clk_hw_omap *hw))
62977b773aeSTero Kristo {
63077b773aeSTero Kristo 	int ret;
63177b773aeSTero Kristo 	struct clk_hw_omap *hw;
63277b773aeSTero Kristo 
63377b773aeSTero Kristo 	list_for_each_entry(hw, &clk_hw_omap_clocks, node) {
63477b773aeSTero Kristo 		ret = (*fn)(hw);
63577b773aeSTero Kristo 		if (ret)
63677b773aeSTero Kristo 			break;
63777b773aeSTero Kristo 	}
63877b773aeSTero Kristo 
63977b773aeSTero Kristo 	return ret;
64077b773aeSTero Kristo }
6417fd79ee7STero Kristo 
6427fd79ee7STero Kristo /**
6437fd79ee7STero Kristo  * omap2_clk_is_hw_omap - check if the provided clk_hw is OMAP clock
6447fd79ee7STero Kristo  * @hw: clk_hw to check if it is an omap clock or not
6457fd79ee7STero Kristo  *
6467fd79ee7STero Kristo  * Checks if the provided clk_hw is OMAP clock or not. Returns true if
6477fd79ee7STero Kristo  * it is, false otherwise.
6487fd79ee7STero Kristo  */
omap2_clk_is_hw_omap(struct clk_hw * hw)6497fd79ee7STero Kristo bool omap2_clk_is_hw_omap(struct clk_hw *hw)
6507fd79ee7STero Kristo {
6517fd79ee7STero Kristo 	struct clk_hw_omap *oclk;
6527fd79ee7STero Kristo 
6537fd79ee7STero Kristo 	list_for_each_entry(oclk, &clk_hw_omap_clocks, node) {
6547fd79ee7STero Kristo 		if (&oclk->hw == hw)
6557fd79ee7STero Kristo 			return true;
6567fd79ee7STero Kristo 	}
6577fd79ee7STero Kristo 
6587fd79ee7STero Kristo 	return false;
6597fd79ee7STero Kristo }
660