blob: e5a1c8297a1d256fd1d6fc006121b97a6d89e821 [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
Stephen Boyd1b29e602015-06-19 15:00:46 -070018#include <linux/clk.h>
Tero Kristoa8acecc2013-07-18 11:52:33 +030019#include <linux/clk-provider.h>
20#include <linux/clkdev.h>
21#include <linux/clk/ti.h>
22#include <linux/of.h>
Tero Kristo819b4862013-10-22 11:39:36 +030023#include <linux/of_address.h>
24#include <linux/list.h>
Tero Kristo989feaf2015-04-27 22:23:06 +030025#include <linux/regmap.h>
26#include <linux/bootmem.h>
Tero Kristo21f0bf22016-09-29 12:00:57 +030027#include <linux/device.h>
Tero Kristoa8acecc2013-07-18 11:52:33 +030028
Tero Kristoc82f8952014-12-16 18:20:46 +020029#include "clock.h"
30
Tero Kristoa8acecc2013-07-18 11:52:33 +030031#undef pr_fmt
32#define pr_fmt(fmt) "%s: " fmt, __func__
33
Tero Kristo819b4862013-10-22 11:39:36 +030034struct ti_clk_ll_ops *ti_clk_ll_ops;
Tero Kristoc08ee142014-09-12 15:01:57 +030035static struct device_node *clocks_node_ptr[CLK_MAX_MEMMAPS];
Tero Kristo819b4862013-10-22 11:39:36 +030036
Stephen Boyd3fe6d692015-07-15 12:03:52 -070037static struct ti_clk_features ti_clk_features;
Tero Kristof3b19aa2015-02-27 17:54:14 +020038
Tero Kristo989feaf2015-04-27 22:23:06 +030039struct clk_iomap {
40 struct regmap *regmap;
41 void __iomem *mem;
42};
43
44static struct clk_iomap *clk_memmaps[CLK_MAX_MEMMAPS];
45
Tero Kristo6c0afb52017-02-09 11:24:37 +020046static void clk_memmap_writel(u32 val, const struct clk_omap_reg *reg)
Tero Kristo989feaf2015-04-27 22:23:06 +030047{
Tero Kristo6c0afb52017-02-09 11:24:37 +020048 struct clk_iomap *io = clk_memmaps[reg->index];
Tero Kristo989feaf2015-04-27 22:23:06 +030049
Tero Kristo6c0afb52017-02-09 11:24:37 +020050 if (reg->ptr)
51 writel_relaxed(val, reg->ptr);
52 else if (io->regmap)
53 regmap_write(io->regmap, reg->offset, val);
Tero Kristo989feaf2015-04-27 22:23:06 +030054 else
Tero Kristo6c0afb52017-02-09 11:24:37 +020055 writel_relaxed(val, io->mem + reg->offset);
Tero Kristo989feaf2015-04-27 22:23:06 +030056}
57
Tero Kristo6c0afb52017-02-09 11:24:37 +020058static u32 clk_memmap_readl(const struct clk_omap_reg *reg)
Tero Kristo989feaf2015-04-27 22:23:06 +030059{
60 u32 val;
Tero Kristo6c0afb52017-02-09 11:24:37 +020061 struct clk_iomap *io = clk_memmaps[reg->index];
Tero Kristo989feaf2015-04-27 22:23:06 +030062
Tero Kristo6c0afb52017-02-09 11:24:37 +020063 if (reg->ptr)
64 val = readl_relaxed(reg->ptr);
65 else if (io->regmap)
66 regmap_read(io->regmap, reg->offset, &val);
Tero Kristo989feaf2015-04-27 22:23:06 +030067 else
Tero Kristo6c0afb52017-02-09 11:24:37 +020068 val = readl_relaxed(io->mem + reg->offset);
Tero Kristo989feaf2015-04-27 22:23:06 +030069
70 return val;
71}
72
Tero Kristoa8acecc2013-07-18 11:52:33 +030073/**
Tero Kristoe9e63082015-04-27 21:55:42 +030074 * 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 */
82int 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;
Tero Kristo989feaf2015-04-27 22:23:06 +030090 ops->clk_readl = clk_memmap_readl;
91 ops->clk_writel = clk_memmap_writel;
Tero Kristoe9e63082015-04-27 21:55:42 +030092
93 return 0;
94}
95
Tero Kristoa8acecc2013-07-18 11:52:33 +030096/**
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 */
105void __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
112 for (c = oclks; c->node_name != NULL; c++) {
113 node = of_find_node_by_name(NULL, c->node_name);
114 clkspec.np = node;
115 clk = of_clk_get_from_provider(&clkspec);
116
117 if (!IS_ERR(clk)) {
118 c->lk.clk = clk;
119 clkdev_add(&c->lk);
120 } else {
121 pr_warn("failed to lookup clock node %s\n",
122 c->node_name);
123 }
124 }
125}
Tero Kristo819b4862013-10-22 11:39:36 +0300126
127struct clk_init_item {
128 struct device_node *node;
129 struct clk_hw *hw;
130 ti_of_clk_init_cb_t func;
131 struct list_head link;
132};
133
134static LIST_HEAD(retry_list);
135
136/**
137 * ti_clk_retry_init - retries a failed clock init at later phase
138 * @node: device not for the clock
139 * @hw: partially initialized clk_hw struct for the clock
140 * @func: init function to be called for the clock
141 *
142 * Adds a failed clock init to the retry list. The retry list is parsed
143 * once all the other clocks have been initialized.
144 */
145int __init ti_clk_retry_init(struct device_node *node, struct clk_hw *hw,
146 ti_of_clk_init_cb_t func)
147{
148 struct clk_init_item *retry;
149
150 pr_debug("%s: adding to retry list...\n", node->name);
151 retry = kzalloc(sizeof(*retry), GFP_KERNEL);
152 if (!retry)
153 return -ENOMEM;
154
155 retry->node = node;
156 retry->func = func;
157 retry->hw = hw;
158 list_add(&retry->link, &retry_list);
159
160 return 0;
161}
162
163/**
164 * ti_clk_get_reg_addr - get register address for a clock register
165 * @node: device node for the clock
166 * @index: register index from the clock node
Tero Kristo6c0afb52017-02-09 11:24:37 +0200167 * @reg: pointer to target register struct
Tero Kristo819b4862013-10-22 11:39:36 +0300168 *
Tero Kristo6c0afb52017-02-09 11:24:37 +0200169 * Builds clock register address from device tree information, and returns
170 * the data via the provided output pointer @reg. Returns 0 on success,
171 * negative error value on failure.
Tero Kristo819b4862013-10-22 11:39:36 +0300172 */
Tero Kristo6c0afb52017-02-09 11:24:37 +0200173int ti_clk_get_reg_addr(struct device_node *node, int index,
174 struct clk_omap_reg *reg)
Tero Kristo819b4862013-10-22 11:39:36 +0300175{
Tero Kristo819b4862013-10-22 11:39:36 +0300176 u32 val;
Tero Kristoc08ee142014-09-12 15:01:57 +0300177 int i;
Tero Kristo819b4862013-10-22 11:39:36 +0300178
Tero Kristoc08ee142014-09-12 15:01:57 +0300179 for (i = 0; i < CLK_MAX_MEMMAPS; i++) {
180 if (clocks_node_ptr[i] == node->parent)
181 break;
182 }
183
184 if (i == CLK_MAX_MEMMAPS) {
185 pr_err("clk-provider not found for %s!\n", node->name);
Tero Kristo6c0afb52017-02-09 11:24:37 +0200186 return -ENOENT;
Tero Kristoc08ee142014-09-12 15:01:57 +0300187 }
188
189 reg->index = i;
Tero Kristo819b4862013-10-22 11:39:36 +0300190
191 if (of_property_read_u32_index(node, "reg", index, &val)) {
192 pr_err("%s must have reg[%d]!\n", node->name, index);
Tero Kristo6c0afb52017-02-09 11:24:37 +0200193 return -EINVAL;
Tero Kristo819b4862013-10-22 11:39:36 +0300194 }
195
196 reg->offset = val;
Tero Kristo6c0afb52017-02-09 11:24:37 +0200197 reg->ptr = NULL;
Tero Kristo819b4862013-10-22 11:39:36 +0300198
Tero Kristo6c0afb52017-02-09 11:24:37 +0200199 return 0;
Tero Kristo819b4862013-10-22 11:39:36 +0300200}
201
202/**
Tero Kristo989feaf2015-04-27 22:23:06 +0300203 * omap2_clk_provider_init - init master clock provider
Tero Kristo819b4862013-10-22 11:39:36 +0300204 * @parent: master node
205 * @index: internal index for clk_reg_ops
Tero Kristo989feaf2015-04-27 22:23:06 +0300206 * @syscon: syscon regmap pointer for accessing clock registers
207 * @mem: iomem pointer for the clock provider memory area, only used if
208 * syscon is not provided
Tero Kristo819b4862013-10-22 11:39:36 +0300209 *
Tero Kristoc08ee142014-09-12 15:01:57 +0300210 * Initializes a master clock IP block. This basically sets up the
211 * mapping from clocks node to the memory map index. All the clocks
212 * are then initialized through the common of_clk_init call, and the
213 * clocks will access their memory maps based on the node layout.
Tero Kristo989feaf2015-04-27 22:23:06 +0300214 * Returns 0 in success.
Tero Kristo819b4862013-10-22 11:39:36 +0300215 */
Tero Kristo989feaf2015-04-27 22:23:06 +0300216int __init omap2_clk_provider_init(struct device_node *parent, int index,
217 struct regmap *syscon, void __iomem *mem)
Tero Kristo819b4862013-10-22 11:39:36 +0300218{
Tero Kristo819b4862013-10-22 11:39:36 +0300219 struct device_node *clocks;
Tero Kristo989feaf2015-04-27 22:23:06 +0300220 struct clk_iomap *io;
Tero Kristo819b4862013-10-22 11:39:36 +0300221
222 /* get clocks for this parent */
223 clocks = of_get_child_by_name(parent, "clocks");
224 if (!clocks) {
225 pr_err("%s missing 'clocks' child node.\n", parent->name);
Tero Kristo989feaf2015-04-27 22:23:06 +0300226 return -EINVAL;
Tero Kristo819b4862013-10-22 11:39:36 +0300227 }
228
Tero Kristoc08ee142014-09-12 15:01:57 +0300229 /* add clocks node info */
230 clocks_node_ptr[index] = clocks;
Tero Kristo989feaf2015-04-27 22:23:06 +0300231
232 io = kzalloc(sizeof(*io), GFP_KERNEL);
Stephen Boydf645f72d2015-07-15 11:55:42 -0700233 if (!io)
234 return -ENOMEM;
Tero Kristo989feaf2015-04-27 22:23:06 +0300235
236 io->regmap = syscon;
237 io->mem = mem;
238
239 clk_memmaps[index] = io;
240
241 return 0;
242}
243
244/**
245 * omap2_clk_legacy_provider_init - initialize a legacy clock provider
246 * @index: index for the clock provider
247 * @mem: iomem pointer for the clock provider memory area
248 *
249 * Initializes a legacy clock provider memory mapping.
250 */
251void __init omap2_clk_legacy_provider_init(int index, void __iomem *mem)
252{
253 struct clk_iomap *io;
254
255 io = memblock_virt_alloc(sizeof(*io), 0);
256
257 io->mem = mem;
258
259 clk_memmaps[index] = io;
Tero Kristoc08ee142014-09-12 15:01:57 +0300260}
Tero Kristo819b4862013-10-22 11:39:36 +0300261
Tero Kristoc08ee142014-09-12 15:01:57 +0300262/**
263 * ti_dt_clk_init_retry_clks - init clocks from the retry list
264 *
265 * Initializes any clocks that have failed to initialize before,
266 * reasons being missing parent node(s) during earlier init. This
267 * typically happens only for DPLLs which need to have both of their
268 * parent clocks ready during init.
269 */
270void ti_dt_clk_init_retry_clks(void)
271{
272 struct clk_init_item *retry;
273 struct clk_init_item *tmp;
274 int retries = 5;
275
276 while (!list_empty(&retry_list) && retries) {
277 list_for_each_entry_safe(retry, tmp, &retry_list, link) {
278 pr_debug("retry-init: %s\n", retry->node->name);
279 retry->func(retry->hw, retry->node);
280 list_del(&retry->link);
281 kfree(retry);
282 }
283 retries--;
Tero Kristo819b4862013-10-22 11:39:36 +0300284 }
285}
Tero Kristoc82f8952014-12-16 18:20:46 +0200286
Arnd Bergmann6793a30a2015-02-03 17:59:32 +0100287#if defined(CONFIG_ARCH_OMAP3) && defined(CONFIG_ATAGS)
Tero Kristoc82f8952014-12-16 18:20:46 +0200288void __init ti_clk_patch_legacy_clks(struct ti_clk **patch)
289{
290 while (*patch) {
291 memcpy((*patch)->patch, *patch, sizeof(**patch));
292 patch++;
293 }
294}
295
296struct clk __init *ti_clk_register_clk(struct ti_clk *setup)
297{
298 struct clk *clk;
299 struct ti_clk_fixed *fixed;
300 struct ti_clk_fixed_factor *fixed_factor;
301 struct clk_hw *clk_hw;
Tero Kristo1ae79c42016-09-29 12:06:40 +0300302 int ret;
Tero Kristoc82f8952014-12-16 18:20:46 +0200303
304 if (setup->clk)
305 return setup->clk;
306
307 switch (setup->type) {
308 case TI_CLK_FIXED:
309 fixed = setup->data;
310
Stephen Boyd45e21152016-03-01 11:00:03 -0800311 clk = clk_register_fixed_rate(NULL, setup->name, NULL, 0,
312 fixed->frequency);
Tero Kristo1ae79c42016-09-29 12:06:40 +0300313 if (!IS_ERR(clk)) {
314 ret = ti_clk_add_alias(NULL, clk, setup->name);
315 if (ret) {
316 clk_unregister(clk);
317 clk = ERR_PTR(ret);
318 }
319 }
Tero Kristoc82f8952014-12-16 18:20:46 +0200320 break;
Tero Kristo7c18a652014-12-16 18:20:47 +0200321 case TI_CLK_MUX:
322 clk = ti_clk_register_mux(setup);
323 break;
Tero Kristod96f7742014-12-16 18:20:50 +0200324 case TI_CLK_DIVIDER:
325 clk = ti_clk_register_divider(setup);
326 break;
Tero Kristob26bcf92014-12-16 18:20:52 +0200327 case TI_CLK_COMPOSITE:
328 clk = ti_clk_register_composite(setup);
329 break;
Tero Kristoc82f8952014-12-16 18:20:46 +0200330 case TI_CLK_FIXED_FACTOR:
331 fixed_factor = setup->data;
332
333 clk = clk_register_fixed_factor(NULL, setup->name,
334 fixed_factor->parent,
335 0, fixed_factor->mult,
336 fixed_factor->div);
Tero Kristo1ae79c42016-09-29 12:06:40 +0300337 if (!IS_ERR(clk)) {
338 ret = ti_clk_add_alias(NULL, clk, setup->name);
339 if (ret) {
340 clk_unregister(clk);
341 clk = ERR_PTR(ret);
342 }
343 }
Tero Kristoc82f8952014-12-16 18:20:46 +0200344 break;
Tero Kristof1876162014-12-16 18:20:48 +0200345 case TI_CLK_GATE:
346 clk = ti_clk_register_gate(setup);
347 break;
Tero Kristoed405a22015-01-29 22:24:28 +0200348 case TI_CLK_DPLL:
349 clk = ti_clk_register_dpll(setup);
350 break;
Tero Kristoc82f8952014-12-16 18:20:46 +0200351 default:
352 pr_err("bad type for %s!\n", setup->name);
353 clk = ERR_PTR(-EINVAL);
354 }
355
356 if (!IS_ERR(clk)) {
357 setup->clk = clk;
358 if (setup->clkdm_name) {
Stephen Boyd98d8a602015-06-29 16:56:30 -0700359 clk_hw = __clk_get_hw(clk);
360 if (clk_hw_get_flags(clk_hw) & CLK_IS_BASIC) {
Tero Kristoc82f8952014-12-16 18:20:46 +0200361 pr_warn("can't setup clkdm for basic clk %s\n",
362 setup->name);
363 } else {
Tero Kristoc82f8952014-12-16 18:20:46 +0200364 to_clk_hw_omap(clk_hw)->clkdm_name =
365 setup->clkdm_name;
366 omap2_init_clk_clkdm(clk_hw);
367 }
368 }
369 }
370
371 return clk;
372}
373
374int __init ti_clk_register_legacy_clks(struct ti_clk_alias *clks)
375{
376 struct clk *clk;
377 bool retry;
378 struct ti_clk_alias *retry_clk;
379 struct ti_clk_alias *tmp;
380
381 while (clks->clk) {
382 clk = ti_clk_register_clk(clks->clk);
383 if (IS_ERR(clk)) {
384 if (PTR_ERR(clk) == -EAGAIN) {
385 list_add(&clks->link, &retry_list);
386 } else {
387 pr_err("register for %s failed: %ld\n",
388 clks->clk->name, PTR_ERR(clk));
389 return PTR_ERR(clk);
390 }
Tero Kristoc82f8952014-12-16 18:20:46 +0200391 }
392 clks++;
393 }
394
395 retry = true;
396
397 while (!list_empty(&retry_list) && retry) {
398 retry = false;
399 list_for_each_entry_safe(retry_clk, tmp, &retry_list, link) {
400 pr_debug("retry-init: %s\n", retry_clk->clk->name);
401 clk = ti_clk_register_clk(retry_clk->clk);
402 if (IS_ERR(clk)) {
403 if (PTR_ERR(clk) == -EAGAIN) {
404 continue;
405 } else {
406 pr_err("register for %s failed: %ld\n",
407 retry_clk->clk->name,
408 PTR_ERR(clk));
409 return PTR_ERR(clk);
410 }
411 } else {
412 retry = true;
Tero Kristoc82f8952014-12-16 18:20:46 +0200413 list_del(&retry_clk->link);
414 }
415 }
416 }
417
418 return 0;
419}
Arnd Bergmann6793a30a2015-02-03 17:59:32 +0100420#endif
Tero Kristof3b19aa2015-02-27 17:54:14 +0200421
Arnd Bergmannfc04f27d2017-04-19 19:44:56 +0200422static const struct of_device_id simple_clk_match_table[] __initconst = {
423 { .compatible = "fixed-clock" },
424 { .compatible = "fixed-factor-clock" },
425 { }
426};
427
Tero Kristof3b19aa2015-02-27 17:54:14 +0200428/**
Tero Kristoc17435c2016-09-29 12:05:00 +0300429 * ti_clk_add_aliases - setup clock aliases
430 *
431 * Sets up any missing clock aliases. No return value.
432 */
433void __init ti_clk_add_aliases(void)
434{
435 struct device_node *np;
436 struct clk *clk;
437
438 for_each_matching_node(np, simple_clk_match_table) {
439 struct of_phandle_args clkspec;
440
441 clkspec.np = np;
442 clk = of_clk_get_from_provider(&clkspec);
443
444 ti_clk_add_alias(NULL, clk, np->name);
445 }
446}
447
448/**
Tero Kristof3b19aa2015-02-27 17:54:14 +0200449 * ti_clk_setup_features - setup clock features flags
450 * @features: features definition to use
451 *
452 * Initializes the clock driver features flags based on platform
453 * provided data. No return value.
454 */
455void __init ti_clk_setup_features(struct ti_clk_features *features)
456{
457 memcpy(&ti_clk_features, features, sizeof(*features));
458}
459
460/**
461 * ti_clk_get_features - get clock driver features flags
462 *
463 * Get TI clock driver features description. Returns a pointer
464 * to the current feature setup.
465 */
466const struct ti_clk_features *ti_clk_get_features(void)
467{
468 return &ti_clk_features;
469}
Tero Kristoa5aa8a62015-03-03 10:51:01 +0200470
471/**
472 * omap2_clk_enable_init_clocks - prepare & enable a list of clocks
473 * @clk_names: ptr to an array of strings of clock names to enable
474 * @num_clocks: number of clock names in @clk_names
475 *
476 * Prepare and enable a list of clocks, named by @clk_names. No
477 * return value. XXX Deprecated; only needed until these clocks are
478 * properly claimed and enabled by the drivers or core code that uses
479 * them. XXX What code disables & calls clk_put on these clocks?
480 */
481void omap2_clk_enable_init_clocks(const char **clk_names, u8 num_clocks)
482{
483 struct clk *init_clk;
484 int i;
485
486 for (i = 0; i < num_clocks; i++) {
487 init_clk = clk_get(NULL, clk_names[i]);
488 if (WARN(IS_ERR(init_clk), "could not find init clock %s\n",
489 clk_names[i]))
490 continue;
491 clk_prepare_enable(init_clk);
492 }
493}
Tero Kristo21f0bf22016-09-29 12:00:57 +0300494
495/**
496 * ti_clk_add_alias - add a clock alias for a TI clock
497 * @dev: device alias for this clock
498 * @clk: clock handle to create alias for
499 * @con: connection ID for this clock
500 *
501 * Creates a clock alias for a TI clock. Allocates the clock lookup entry
502 * and assigns the data to it. Returns 0 if successful, negative error
503 * value otherwise.
504 */
505int ti_clk_add_alias(struct device *dev, struct clk *clk, const char *con)
506{
507 struct clk_lookup *cl;
508
509 if (!clk)
510 return 0;
511
512 if (IS_ERR(clk))
513 return PTR_ERR(clk);
514
515 cl = kzalloc(sizeof(*cl), GFP_KERNEL);
516 if (!cl)
517 return -ENOMEM;
518
519 if (dev)
520 cl->dev_id = dev_name(dev);
521 cl->con_id = con;
522 cl->clk = clk;
523
524 clkdev_add(cl);
525
526 return 0;
527}
528
529/**
530 * ti_clk_register - register a TI clock to the common clock framework
531 * @dev: device for this clock
532 * @hw: hardware clock handle
533 * @con: connection ID for this clock
534 *
535 * Registers a TI clock to the common clock framework, and adds a clock
536 * alias for it. Returns a handle to the registered clock if successful,
537 * ERR_PTR value in failure.
538 */
539struct clk *ti_clk_register(struct device *dev, struct clk_hw *hw,
540 const char *con)
541{
542 struct clk *clk;
543 int ret;
544
545 clk = clk_register(dev, hw);
546 if (IS_ERR(clk))
547 return clk;
548
549 ret = ti_clk_add_alias(dev, clk, con);
550 if (ret) {
551 clk_unregister(clk);
552 return ERR_PTR(ret);
553 }
554
555 return clk;
556}