blob: 521e330e304689050c3874f1dbc05a3c0c985469 [file] [log] [blame]
Tero Kristoa8acecc2013-07-18 11:52:33 +03001/*
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-provider.h>
19#include <linux/clkdev.h>
20#include <linux/clk/ti.h>
21#include <linux/of.h>
Tero Kristo819b4862013-10-22 11:39:36 +030022#include <linux/of_address.h>
23#include <linux/list.h>
Tero Kristo989feaf2015-04-27 22:23:06 +030024#include <linux/regmap.h>
25#include <linux/bootmem.h>
Tero Kristoa8acecc2013-07-18 11:52:33 +030026
Tero Kristoc82f8952014-12-16 18:20:46 +020027#include "clock.h"
28
Tero Kristoa8acecc2013-07-18 11:52:33 +030029#undef pr_fmt
30#define pr_fmt(fmt) "%s: " fmt, __func__
31
Tero Kristo819b4862013-10-22 11:39:36 +030032struct ti_clk_ll_ops *ti_clk_ll_ops;
Tero Kristoc08ee142014-09-12 15:01:57 +030033static struct device_node *clocks_node_ptr[CLK_MAX_MEMMAPS];
Tero Kristo819b4862013-10-22 11:39:36 +030034
Tero Kristof3b19aa2015-02-27 17:54:14 +020035struct ti_clk_features ti_clk_features;
36
Tero Kristo989feaf2015-04-27 22:23:06 +030037struct clk_iomap {
38 struct regmap *regmap;
39 void __iomem *mem;
40};
41
42static struct clk_iomap *clk_memmaps[CLK_MAX_MEMMAPS];
43
44static void clk_memmap_writel(u32 val, void __iomem *reg)
45{
46 struct clk_omap_reg *r = (struct clk_omap_reg *)&reg;
47 struct clk_iomap *io = clk_memmaps[r->index];
48
49 if (io->regmap)
50 regmap_write(io->regmap, r->offset, val);
51 else
52 writel_relaxed(val, io->mem + r->offset);
53}
54
55static u32 clk_memmap_readl(void __iomem *reg)
56{
57 u32 val;
58 struct clk_omap_reg *r = (struct clk_omap_reg *)&reg;
59 struct clk_iomap *io = clk_memmaps[r->index];
60
61 if (io->regmap)
62 regmap_read(io->regmap, r->offset, &val);
63 else
64 val = readl_relaxed(io->mem + r->offset);
65
66 return val;
67}
68
Tero Kristoa8acecc2013-07-18 11:52:33 +030069/**
Tero Kristoe9e63082015-04-27 21:55:42 +030070 * ti_clk_setup_ll_ops - setup low level clock operations
71 * @ops: low level clock ops descriptor
72 *
73 * Sets up low level clock operations for TI clock driver. This is used
74 * to provide various callbacks for the clock driver towards platform
75 * specific code. Returns 0 on success, -EBUSY if ll_ops have been
76 * registered already.
77 */
78int ti_clk_setup_ll_ops(struct ti_clk_ll_ops *ops)
79{
80 if (ti_clk_ll_ops) {
81 pr_err("Attempt to register ll_ops multiple times.\n");
82 return -EBUSY;
83 }
84
85 ti_clk_ll_ops = ops;
Tero Kristo989feaf2015-04-27 22:23:06 +030086 ops->clk_readl = clk_memmap_readl;
87 ops->clk_writel = clk_memmap_writel;
Tero Kristoe9e63082015-04-27 21:55:42 +030088
89 return 0;
90}
91
Tero Kristoa8acecc2013-07-18 11:52:33 +030092/**
93 * ti_dt_clocks_register - register DT alias clocks during boot
94 * @oclks: list of clocks to register
95 *
96 * Register alias or non-standard DT clock entries during boot. By
97 * default, DT clocks are found based on their node name. If any
98 * additional con-id / dev-id -> clock mapping is required, use this
99 * function to list these.
100 */
101void __init ti_dt_clocks_register(struct ti_dt_clk oclks[])
102{
103 struct ti_dt_clk *c;
104 struct device_node *node;
105 struct clk *clk;
106 struct of_phandle_args clkspec;
107
108 for (c = oclks; c->node_name != NULL; c++) {
109 node = of_find_node_by_name(NULL, c->node_name);
110 clkspec.np = node;
111 clk = of_clk_get_from_provider(&clkspec);
112
113 if (!IS_ERR(clk)) {
114 c->lk.clk = clk;
115 clkdev_add(&c->lk);
116 } else {
117 pr_warn("failed to lookup clock node %s\n",
118 c->node_name);
119 }
120 }
121}
Tero Kristo819b4862013-10-22 11:39:36 +0300122
123struct clk_init_item {
124 struct device_node *node;
125 struct clk_hw *hw;
126 ti_of_clk_init_cb_t func;
127 struct list_head link;
128};
129
130static LIST_HEAD(retry_list);
131
132/**
133 * ti_clk_retry_init - retries a failed clock init at later phase
134 * @node: device not for the clock
135 * @hw: partially initialized clk_hw struct for the clock
136 * @func: init function to be called for the clock
137 *
138 * Adds a failed clock init to the retry list. The retry list is parsed
139 * once all the other clocks have been initialized.
140 */
141int __init ti_clk_retry_init(struct device_node *node, struct clk_hw *hw,
142 ti_of_clk_init_cb_t func)
143{
144 struct clk_init_item *retry;
145
146 pr_debug("%s: adding to retry list...\n", node->name);
147 retry = kzalloc(sizeof(*retry), GFP_KERNEL);
148 if (!retry)
149 return -ENOMEM;
150
151 retry->node = node;
152 retry->func = func;
153 retry->hw = hw;
154 list_add(&retry->link, &retry_list);
155
156 return 0;
157}
158
159/**
160 * ti_clk_get_reg_addr - get register address for a clock register
161 * @node: device node for the clock
162 * @index: register index from the clock node
163 *
164 * Builds clock register address from device tree information. This
Tero Kristoc807dbe2015-02-23 21:06:08 +0200165 * is a struct of type clk_omap_reg. Returns a pointer to the register
166 * address, or a pointer error value in failure.
Tero Kristo819b4862013-10-22 11:39:36 +0300167 */
168void __iomem *ti_clk_get_reg_addr(struct device_node *node, int index)
169{
170 struct clk_omap_reg *reg;
171 u32 val;
172 u32 tmp;
Tero Kristoc08ee142014-09-12 15:01:57 +0300173 int i;
Tero Kristo819b4862013-10-22 11:39:36 +0300174
175 reg = (struct clk_omap_reg *)&tmp;
Tero Kristoc08ee142014-09-12 15:01:57 +0300176
177 for (i = 0; i < CLK_MAX_MEMMAPS; i++) {
178 if (clocks_node_ptr[i] == node->parent)
179 break;
180 }
181
182 if (i == CLK_MAX_MEMMAPS) {
183 pr_err("clk-provider not found for %s!\n", node->name);
Stephen Boyd412d6b42015-05-01 12:59:32 -0700184 return IOMEM_ERR_PTR(-ENOENT);
Tero Kristoc08ee142014-09-12 15:01:57 +0300185 }
186
187 reg->index = i;
Tero Kristo819b4862013-10-22 11:39:36 +0300188
189 if (of_property_read_u32_index(node, "reg", index, &val)) {
190 pr_err("%s must have reg[%d]!\n", node->name, index);
Stephen Boyd412d6b42015-05-01 12:59:32 -0700191 return IOMEM_ERR_PTR(-EINVAL);
Tero Kristo819b4862013-10-22 11:39:36 +0300192 }
193
194 reg->offset = val;
195
196 return (void __iomem *)tmp;
197}
198
199/**
Tero Kristo989feaf2015-04-27 22:23:06 +0300200 * omap2_clk_provider_init - init master clock provider
Tero Kristo819b4862013-10-22 11:39:36 +0300201 * @parent: master node
202 * @index: internal index for clk_reg_ops
Tero Kristo989feaf2015-04-27 22:23:06 +0300203 * @syscon: syscon regmap pointer for accessing clock registers
204 * @mem: iomem pointer for the clock provider memory area, only used if
205 * syscon is not provided
Tero Kristo819b4862013-10-22 11:39:36 +0300206 *
Tero Kristoc08ee142014-09-12 15:01:57 +0300207 * Initializes a master clock IP block. This basically sets up the
208 * mapping from clocks node to the memory map index. All the clocks
209 * are then initialized through the common of_clk_init call, and the
210 * clocks will access their memory maps based on the node layout.
Tero Kristo989feaf2015-04-27 22:23:06 +0300211 * Returns 0 in success.
Tero Kristo819b4862013-10-22 11:39:36 +0300212 */
Tero Kristo989feaf2015-04-27 22:23:06 +0300213int __init omap2_clk_provider_init(struct device_node *parent, int index,
214 struct regmap *syscon, void __iomem *mem)
Tero Kristo819b4862013-10-22 11:39:36 +0300215{
Tero Kristo819b4862013-10-22 11:39:36 +0300216 struct device_node *clocks;
Tero Kristo989feaf2015-04-27 22:23:06 +0300217 struct clk_iomap *io;
Tero Kristo819b4862013-10-22 11:39:36 +0300218
219 /* get clocks for this parent */
220 clocks = of_get_child_by_name(parent, "clocks");
221 if (!clocks) {
222 pr_err("%s missing 'clocks' child node.\n", parent->name);
Tero Kristo989feaf2015-04-27 22:23:06 +0300223 return -EINVAL;
Tero Kristo819b4862013-10-22 11:39:36 +0300224 }
225
Tero Kristoc08ee142014-09-12 15:01:57 +0300226 /* add clocks node info */
227 clocks_node_ptr[index] = clocks;
Tero Kristo989feaf2015-04-27 22:23:06 +0300228
229 io = kzalloc(sizeof(*io), GFP_KERNEL);
Stephen Boydf645f722015-07-15 11:55:42 -0700230 if (!io)
231 return -ENOMEM;
Tero Kristo989feaf2015-04-27 22:23:06 +0300232
233 io->regmap = syscon;
234 io->mem = mem;
235
236 clk_memmaps[index] = io;
237
238 return 0;
239}
240
241/**
242 * omap2_clk_legacy_provider_init - initialize a legacy clock provider
243 * @index: index for the clock provider
244 * @mem: iomem pointer for the clock provider memory area
245 *
246 * Initializes a legacy clock provider memory mapping.
247 */
248void __init omap2_clk_legacy_provider_init(int index, void __iomem *mem)
249{
250 struct clk_iomap *io;
251
252 io = memblock_virt_alloc(sizeof(*io), 0);
253
254 io->mem = mem;
255
256 clk_memmaps[index] = io;
Tero Kristoc08ee142014-09-12 15:01:57 +0300257}
Tero Kristo819b4862013-10-22 11:39:36 +0300258
Tero Kristoc08ee142014-09-12 15:01:57 +0300259/**
260 * ti_dt_clk_init_retry_clks - init clocks from the retry list
261 *
262 * Initializes any clocks that have failed to initialize before,
263 * reasons being missing parent node(s) during earlier init. This
264 * typically happens only for DPLLs which need to have both of their
265 * parent clocks ready during init.
266 */
267void ti_dt_clk_init_retry_clks(void)
268{
269 struct clk_init_item *retry;
270 struct clk_init_item *tmp;
271 int retries = 5;
272
273 while (!list_empty(&retry_list) && retries) {
274 list_for_each_entry_safe(retry, tmp, &retry_list, link) {
275 pr_debug("retry-init: %s\n", retry->node->name);
276 retry->func(retry->hw, retry->node);
277 list_del(&retry->link);
278 kfree(retry);
279 }
280 retries--;
Tero Kristo819b4862013-10-22 11:39:36 +0300281 }
282}
Tero Kristoc82f8952014-12-16 18:20:46 +0200283
Arnd Bergmann6793a30a2015-02-03 17:59:32 +0100284#if defined(CONFIG_ARCH_OMAP3) && defined(CONFIG_ATAGS)
Tero Kristoc82f8952014-12-16 18:20:46 +0200285void __init ti_clk_patch_legacy_clks(struct ti_clk **patch)
286{
287 while (*patch) {
288 memcpy((*patch)->patch, *patch, sizeof(**patch));
289 patch++;
290 }
291}
292
293struct clk __init *ti_clk_register_clk(struct ti_clk *setup)
294{
295 struct clk *clk;
296 struct ti_clk_fixed *fixed;
297 struct ti_clk_fixed_factor *fixed_factor;
298 struct clk_hw *clk_hw;
299
300 if (setup->clk)
301 return setup->clk;
302
303 switch (setup->type) {
304 case TI_CLK_FIXED:
305 fixed = setup->data;
306
307 clk = clk_register_fixed_rate(NULL, setup->name, NULL,
308 CLK_IS_ROOT, fixed->frequency);
309 break;
Tero Kristo7c18a652014-12-16 18:20:47 +0200310 case TI_CLK_MUX:
311 clk = ti_clk_register_mux(setup);
312 break;
Tero Kristod96f7742014-12-16 18:20:50 +0200313 case TI_CLK_DIVIDER:
314 clk = ti_clk_register_divider(setup);
315 break;
Tero Kristob26bcf92014-12-16 18:20:52 +0200316 case TI_CLK_COMPOSITE:
317 clk = ti_clk_register_composite(setup);
318 break;
Tero Kristoc82f8952014-12-16 18:20:46 +0200319 case TI_CLK_FIXED_FACTOR:
320 fixed_factor = setup->data;
321
322 clk = clk_register_fixed_factor(NULL, setup->name,
323 fixed_factor->parent,
324 0, fixed_factor->mult,
325 fixed_factor->div);
326 break;
Tero Kristof1876162014-12-16 18:20:48 +0200327 case TI_CLK_GATE:
328 clk = ti_clk_register_gate(setup);
329 break;
Tero Kristoed405a22015-01-29 22:24:28 +0200330 case TI_CLK_DPLL:
331 clk = ti_clk_register_dpll(setup);
332 break;
Tero Kristoc82f8952014-12-16 18:20:46 +0200333 default:
334 pr_err("bad type for %s!\n", setup->name);
335 clk = ERR_PTR(-EINVAL);
336 }
337
338 if (!IS_ERR(clk)) {
339 setup->clk = clk;
340 if (setup->clkdm_name) {
341 if (__clk_get_flags(clk) & CLK_IS_BASIC) {
342 pr_warn("can't setup clkdm for basic clk %s\n",
343 setup->name);
344 } else {
345 clk_hw = __clk_get_hw(clk);
346 to_clk_hw_omap(clk_hw)->clkdm_name =
347 setup->clkdm_name;
348 omap2_init_clk_clkdm(clk_hw);
349 }
350 }
351 }
352
353 return clk;
354}
355
356int __init ti_clk_register_legacy_clks(struct ti_clk_alias *clks)
357{
358 struct clk *clk;
359 bool retry;
360 struct ti_clk_alias *retry_clk;
361 struct ti_clk_alias *tmp;
362
363 while (clks->clk) {
364 clk = ti_clk_register_clk(clks->clk);
365 if (IS_ERR(clk)) {
366 if (PTR_ERR(clk) == -EAGAIN) {
367 list_add(&clks->link, &retry_list);
368 } else {
369 pr_err("register for %s failed: %ld\n",
370 clks->clk->name, PTR_ERR(clk));
371 return PTR_ERR(clk);
372 }
373 } else {
374 clks->lk.clk = clk;
375 clkdev_add(&clks->lk);
376 }
377 clks++;
378 }
379
380 retry = true;
381
382 while (!list_empty(&retry_list) && retry) {
383 retry = false;
384 list_for_each_entry_safe(retry_clk, tmp, &retry_list, link) {
385 pr_debug("retry-init: %s\n", retry_clk->clk->name);
386 clk = ti_clk_register_clk(retry_clk->clk);
387 if (IS_ERR(clk)) {
388 if (PTR_ERR(clk) == -EAGAIN) {
389 continue;
390 } else {
391 pr_err("register for %s failed: %ld\n",
392 retry_clk->clk->name,
393 PTR_ERR(clk));
394 return PTR_ERR(clk);
395 }
396 } else {
397 retry = true;
398 retry_clk->lk.clk = clk;
399 clkdev_add(&retry_clk->lk);
400 list_del(&retry_clk->link);
401 }
402 }
403 }
404
405 return 0;
406}
Arnd Bergmann6793a30a2015-02-03 17:59:32 +0100407#endif
Tero Kristof3b19aa2015-02-27 17:54:14 +0200408
409/**
410 * ti_clk_setup_features - setup clock features flags
411 * @features: features definition to use
412 *
413 * Initializes the clock driver features flags based on platform
414 * provided data. No return value.
415 */
416void __init ti_clk_setup_features(struct ti_clk_features *features)
417{
418 memcpy(&ti_clk_features, features, sizeof(*features));
419}
420
421/**
422 * ti_clk_get_features - get clock driver features flags
423 *
424 * Get TI clock driver features description. Returns a pointer
425 * to the current feature setup.
426 */
427const struct ti_clk_features *ti_clk_get_features(void)
428{
429 return &ti_clk_features;
430}
Tero Kristoa5aa8a62015-03-03 10:51:01 +0200431
432/**
433 * omap2_clk_enable_init_clocks - prepare & enable a list of clocks
434 * @clk_names: ptr to an array of strings of clock names to enable
435 * @num_clocks: number of clock names in @clk_names
436 *
437 * Prepare and enable a list of clocks, named by @clk_names. No
438 * return value. XXX Deprecated; only needed until these clocks are
439 * properly claimed and enabled by the drivers or core code that uses
440 * them. XXX What code disables & calls clk_put on these clocks?
441 */
442void omap2_clk_enable_init_clocks(const char **clk_names, u8 num_clocks)
443{
444 struct clk *init_clk;
445 int i;
446
447 for (i = 0; i < num_clocks; i++) {
448 init_clk = clk_get(NULL, clk_names[i]);
449 if (WARN(IS_ERR(init_clk), "could not find init clock %s\n",
450 clk_names[i]))
451 continue;
452 clk_prepare_enable(init_clk);
453 }
454}