blob: 94a4949be9b051937df94b2bb3971771e2c03b6d [file] [log] [blame]
Paul Walmsley543d9372008-03-18 10:22:06 +02001/*
2 * linux/arch/arm/mach-omap2/clock.c
3 *
Tony Lindgrena16e9702008-03-18 11:56:39 +02004 * Copyright (C) 2005-2008 Texas Instruments, Inc.
Paul Walmsley8c349742010-02-22 22:09:24 -07005 * Copyright (C) 2004-2010 Nokia Corporation
Tony Lindgrena16e9702008-03-18 11:56:39 +02006 *
7 * Contacts:
Paul Walmsley543d9372008-03-18 10:22:06 +02008 * Richard Woodruff <r-woodruff2@ti.com>
Paul Walmsley543d9372008-03-18 10:22:06 +02009 * Paul Walmsley
10 *
Paul Walmsley543d9372008-03-18 10:22:06 +020011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
14 */
15#undef DEBUG
16
Paul Walmsley543d9372008-03-18 10:22:06 +020017#include <linux/kernel.h>
Paul Walmsley1fe9be82012-09-27 10:33:33 -060018#include <linux/export.h>
Paul Walmsley543d9372008-03-18 10:22:06 +020019#include <linux/list.h>
20#include <linux/errno.h>
Paul Walmsley4d30e822010-02-22 22:09:36 -070021#include <linux/err.h>
22#include <linux/delay.h>
Mike Turquette32cc0022012-11-10 16:58:41 -070023#include <linux/clk-provider.h>
Russell Kingfced80c2008-09-06 12:10:45 +010024#include <linux/io.h>
Russell Kingfbd3bdb2008-09-06 12:13:59 +010025#include <linux/bitops.h>
Tero Kristo80cbb222015-02-06 16:00:32 +020026#include <linux/regmap.h>
Tero Kristo9f029b12014-10-22 15:15:36 +030027#include <linux/of_address.h>
Tero Kristo80cbb222015-02-06 16:00:32 +020028#include <linux/bootmem.h>
Jean Pihet5e7c58d2011-03-03 11:25:43 +010029#include <asm/cpu.h>
Tony Lindgrendbc04162012-08-31 10:59:07 -070030
Tony Lindgrendbc04162012-08-31 10:59:07 -070031#include <trace/events/power.h>
32
33#include "soc.h"
34#include "clockdomain.h"
Paul Walmsley543d9372008-03-18 10:22:06 +020035#include "clock.h"
Paul Walmsleyc4ceedc2012-10-29 20:56:29 -060036#include "cm.h"
Paul Walmsleyff4ae5d2012-10-21 01:01:11 -060037#include "cm2xxx.h"
38#include "cm3xxx.h"
Paul Walmsley543d9372008-03-18 10:22:06 +020039#include "cm-regbits-24xx.h"
40#include "cm-regbits-34xx.h"
Paul Walmsleyc4ceedc2012-10-29 20:56:29 -060041#include "common.h"
42
43/*
44 * MAX_MODULE_ENABLE_WAIT: maximum of number of microseconds to wait
45 * for a module to indicate that it is no longer in idle
46 */
47#define MAX_MODULE_ENABLE_WAIT 100000
Paul Walmsley543d9372008-03-18 10:22:06 +020048
Afzal Mohammed99541192011-12-13 10:46:43 -080049u16 cpu_mask;
Paul Walmsley543d9372008-03-18 10:22:06 +020050
Tero Kristoa24886e2014-07-02 11:47:40 +030051/* DPLL valid Fint frequency band limits - from 34xx TRM Section 4.7.6.2 */
52#define OMAP3430_DPLL_FINT_BAND1_MIN 750000
53#define OMAP3430_DPLL_FINT_BAND1_MAX 2100000
54#define OMAP3430_DPLL_FINT_BAND2_MIN 7500000
55#define OMAP3430_DPLL_FINT_BAND2_MAX 21000000
56
57/*
58 * DPLL valid Fint frequency range for OMAP36xx and OMAP4xxx.
59 * From device data manual section 4.3 "DPLL and DLL Specifications".
60 */
61#define OMAP3PLUS_DPLL_FINT_MIN 32000
62#define OMAP3PLUS_DPLL_FINT_MAX 52000000
63
Tero Kristo8111e012014-07-02 11:47:39 +030064/*
Paul Walmsley12706c52011-07-10 05:57:06 -060065 * clkdm_control: if true, then when a clock is enabled in the
66 * hardware, its clockdomain will first be enabled; and when a clock
67 * is disabled in the hardware, its clockdomain will be disabled
68 * afterwards.
69 */
70static bool clkdm_control = true;
71
Tero Kristo80cbb222015-02-06 16:00:32 +020072struct clk_iomap {
73 struct regmap *regmap;
74 void __iomem *mem;
75};
76
77static struct clk_iomap *clk_memmaps[CLK_MAX_MEMMAPS];
Tero Kristo9f029b12014-10-22 15:15:36 +030078
79static void clk_memmap_writel(u32 val, void __iomem *reg)
80{
81 struct clk_omap_reg *r = (struct clk_omap_reg *)&reg;
Tero Kristo80cbb222015-02-06 16:00:32 +020082 struct clk_iomap *io = clk_memmaps[r->index];
Tero Kristo9f029b12014-10-22 15:15:36 +030083
Tero Kristo80cbb222015-02-06 16:00:32 +020084 if (io->regmap)
85 regmap_write(io->regmap, r->offset, val);
86 else
87 writel_relaxed(val, io->mem + r->offset);
Tero Kristo9f029b12014-10-22 15:15:36 +030088}
89
90static u32 clk_memmap_readl(void __iomem *reg)
91{
Tero Kristo80cbb222015-02-06 16:00:32 +020092 u32 val;
Tero Kristo9f029b12014-10-22 15:15:36 +030093 struct clk_omap_reg *r = (struct clk_omap_reg *)&reg;
Tero Kristo80cbb222015-02-06 16:00:32 +020094 struct clk_iomap *io = clk_memmaps[r->index];
Tero Kristo9f029b12014-10-22 15:15:36 +030095
Tero Kristo80cbb222015-02-06 16:00:32 +020096 if (io->regmap)
97 regmap_read(io->regmap, r->offset, &val);
98 else
99 val = readl_relaxed(io->mem + r->offset);
100
101 return val;
Tero Kristo9f029b12014-10-22 15:15:36 +0300102}
Tero Kristo3ada6b102013-10-22 11:47:08 +0300103
104void omap2_clk_writel(u32 val, struct clk_hw_omap *clk, void __iomem *reg)
105{
Tero Kristo9f029b12014-10-22 15:15:36 +0300106 if (WARN_ON_ONCE(!(clk->flags & MEMMAP_ADDRESSING)))
Tero Kristo3ada6b102013-10-22 11:47:08 +0300107 writel_relaxed(val, reg);
Tero Kristo9f029b12014-10-22 15:15:36 +0300108 else
109 clk_memmap_writel(val, reg);
Tero Kristo3ada6b102013-10-22 11:47:08 +0300110}
111
112u32 omap2_clk_readl(struct clk_hw_omap *clk, void __iomem *reg)
113{
Tero Kristo9f029b12014-10-22 15:15:36 +0300114 if (WARN_ON_ONCE(!(clk->flags & MEMMAP_ADDRESSING)))
115 return readl_relaxed(reg);
116 else
117 return clk_memmap_readl(reg);
118}
Tero Kristo3ada6b102013-10-22 11:47:08 +0300119
Tero Kristo9f029b12014-10-22 15:15:36 +0300120static struct ti_clk_ll_ops omap_clk_ll_ops = {
121 .clk_readl = clk_memmap_readl,
122 .clk_writel = clk_memmap_writel,
123};
Tero Kristo3ada6b102013-10-22 11:47:08 +0300124
Tero Kristo9f029b12014-10-22 15:15:36 +0300125/**
126 * omap2_clk_provider_init - initialize a clock provider
127 * @match_table: DT device table to match for devices to init
128 * @np: device node pointer for the this clock provider
129 * @index: index for the clock provider
Tero Kristo80cbb222015-02-06 16:00:32 +0200130 + @syscon: syscon regmap pointer
131 * @mem: iomem pointer for the clock provider memory area, only used if
132 * syscon is not provided
Tero Kristo9f029b12014-10-22 15:15:36 +0300133 *
134 * Initializes a clock provider module (CM/PRM etc.), registering
135 * the memory mapping at specified index and initializing the
136 * low level driver infrastructure. Returns 0 in success.
137 */
138int __init omap2_clk_provider_init(struct device_node *np, int index,
Tero Kristo80cbb222015-02-06 16:00:32 +0200139 struct regmap *syscon, void __iomem *mem)
Tero Kristo9f029b12014-10-22 15:15:36 +0300140{
Tero Kristo80cbb222015-02-06 16:00:32 +0200141 struct clk_iomap *io;
142
Tero Kristo9f029b12014-10-22 15:15:36 +0300143 ti_clk_ll_ops = &omap_clk_ll_ops;
144
Tero Kristo80cbb222015-02-06 16:00:32 +0200145 io = kzalloc(sizeof(*io), GFP_KERNEL);
146
147 io->regmap = syscon;
148 io->mem = mem;
149
150 clk_memmaps[index] = io;
Tero Kristo9f029b12014-10-22 15:15:36 +0300151
152 ti_dt_clk_init_provider(np, index);
153
154 return 0;
155}
156
157/**
158 * omap2_clk_legacy_provider_init - initialize a legacy clock provider
159 * @index: index for the clock provider
160 * @mem: iomem pointer for the clock provider memory area
161 *
162 * Initializes a legacy clock provider memory mapping.
163 */
164void __init omap2_clk_legacy_provider_init(int index, void __iomem *mem)
165{
Tero Kristo80cbb222015-02-06 16:00:32 +0200166 struct clk_iomap *io;
167
Tero Kristo9f029b12014-10-22 15:15:36 +0300168 ti_clk_ll_ops = &omap_clk_ll_ops;
169
Tero Kristo80cbb222015-02-06 16:00:32 +0200170 io = memblock_virt_alloc(sizeof(*io), 0);
171
172 io->mem = mem;
173
174 clk_memmaps[index] = io;
Tero Kristo3ada6b102013-10-22 11:47:08 +0300175}
Mike Turquette32cc0022012-11-10 16:58:41 -0700176
177/*
Paul Walmsley30962d92010-02-22 22:09:38 -0700178 * OMAP2+ specific clock functions
179 */
Paul Walmsley543d9372008-03-18 10:22:06 +0200180
Paul Walmsley4b1f76e2010-01-26 20:13:04 -0700181/* Private functions */
182
Paul Walmsleyc4ceedc2012-10-29 20:56:29 -0600183
184/**
185 * _wait_idlest_generic - wait for a module to leave the idle state
Tero Kristo519ab8b2013-10-22 11:49:58 +0300186 * @clk: module clock to wait for (needed for register offsets)
Paul Walmsleyc4ceedc2012-10-29 20:56:29 -0600187 * @reg: virtual address of module IDLEST register
188 * @mask: value to mask against to determine if the module is active
189 * @idlest: idle state indicator (0 or 1) for the clock
190 * @name: name of the clock (for printk)
191 *
192 * Wait for a module to leave idle, where its idle-status register is
193 * not inside the CM module. Returns 1 if the module left idle
194 * promptly, or 0 if the module did not leave idle before the timeout
195 * elapsed. XXX Deprecated - should be moved into drivers for the
196 * individual IP block that the IDLEST register exists in.
197 */
Tero Kristo519ab8b2013-10-22 11:49:58 +0300198static int _wait_idlest_generic(struct clk_hw_omap *clk, void __iomem *reg,
199 u32 mask, u8 idlest, const char *name)
Paul Walmsleyc4ceedc2012-10-29 20:56:29 -0600200{
201 int i = 0, ena = 0;
202
203 ena = (idlest) ? 0 : mask;
204
Tero Kristo519ab8b2013-10-22 11:49:58 +0300205 omap_test_timeout(((omap2_clk_readl(clk, reg) & mask) == ena),
Paul Walmsleyc4ceedc2012-10-29 20:56:29 -0600206 MAX_MODULE_ENABLE_WAIT, i);
207
208 if (i < MAX_MODULE_ENABLE_WAIT)
209 pr_debug("omap clock: module associated with clock %s ready after %d loops\n",
210 name, i);
211 else
212 pr_err("omap clock: module associated with clock %s didn't enable in %d tries\n",
213 name, MAX_MODULE_ENABLE_WAIT);
214
215 return (i < MAX_MODULE_ENABLE_WAIT) ? 1 : 0;
216};
217
Paul Walmsley4b1f76e2010-01-26 20:13:04 -0700218/**
219 * _omap2_module_wait_ready - wait for an OMAP module to leave IDLE
220 * @clk: struct clk * belonging to the module
221 *
222 * If the necessary clocks for the OMAP hardware IP block that
223 * corresponds to clock @clk are enabled, then wait for the module to
224 * indicate readiness (i.e., to leave IDLE). This code does not
225 * belong in the clock code and will be moved in the medium term to
226 * module-dependent code. No return value.
227 */
Mike Turquette32cc0022012-11-10 16:58:41 -0700228static void _omap2_module_wait_ready(struct clk_hw_omap *clk)
Paul Walmsley4b1f76e2010-01-26 20:13:04 -0700229{
230 void __iomem *companion_reg, *idlest_reg;
Paul Walmsleyc4ceedc2012-10-29 20:56:29 -0600231 u8 other_bit, idlest_bit, idlest_val, idlest_reg_id;
232 s16 prcm_mod;
233 int r;
Paul Walmsley4b1f76e2010-01-26 20:13:04 -0700234
235 /* Not all modules have multiple clocks that their IDLEST depends on */
236 if (clk->ops->find_companion) {
237 clk->ops->find_companion(clk, &companion_reg, &other_bit);
Tero Kristo519ab8b2013-10-22 11:49:58 +0300238 if (!(omap2_clk_readl(clk, companion_reg) & (1 << other_bit)))
Paul Walmsley4b1f76e2010-01-26 20:13:04 -0700239 return;
240 }
241
Ranjith Lohithakshan419cc972010-02-24 12:05:54 -0700242 clk->ops->find_idlest(clk, &idlest_reg, &idlest_bit, &idlest_val);
Paul Walmsleyc4ceedc2012-10-29 20:56:29 -0600243 r = cm_split_idlest_reg(idlest_reg, &prcm_mod, &idlest_reg_id);
244 if (r) {
245 /* IDLEST register not in the CM module */
Tero Kristo519ab8b2013-10-22 11:49:58 +0300246 _wait_idlest_generic(clk, idlest_reg, (1 << idlest_bit),
247 idlest_val, __clk_get_name(clk->hw.clk));
Paul Walmsleyc4ceedc2012-10-29 20:56:29 -0600248 } else {
Tero Kristo021b6ff2014-10-27 08:39:23 -0700249 omap_cm_wait_module_ready(0, prcm_mod, idlest_reg_id,
250 idlest_bit);
Paul Walmsleyc4ceedc2012-10-29 20:56:29 -0600251 };
Paul Walmsley4b1f76e2010-01-26 20:13:04 -0700252}
253
Paul Walmsley4b1f76e2010-01-26 20:13:04 -0700254/* Public functions */
255
Paul Walmsley543d9372008-03-18 10:22:06 +0200256/**
Paul Walmsley333943b2008-08-19 11:08:45 +0300257 * omap2_init_clk_clkdm - look up a clockdomain name, store pointer in clk
258 * @clk: OMAP clock struct ptr to use
259 *
260 * Convert a clockdomain name stored in a struct clk 'clk' into a
261 * clockdomain pointer, and save it into the struct clk. Intended to be
262 * called during clk_register(). No return value.
263 */
Mike Turquette32cc0022012-11-10 16:58:41 -0700264void omap2_init_clk_clkdm(struct clk_hw *hw)
265{
266 struct clk_hw_omap *clk = to_clk_hw_omap(hw);
Paul Walmsley333943b2008-08-19 11:08:45 +0300267 struct clockdomain *clkdm;
Rajendra Nayak5dcc3b92012-09-22 02:24:17 -0600268 const char *clk_name;
Paul Walmsley333943b2008-08-19 11:08:45 +0300269
270 if (!clk->clkdm_name)
271 return;
272
Mike Turquette32cc0022012-11-10 16:58:41 -0700273 clk_name = __clk_get_name(hw->clk);
Rajendra Nayak5dcc3b92012-09-22 02:24:17 -0600274
Paul Walmsley333943b2008-08-19 11:08:45 +0300275 clkdm = clkdm_lookup(clk->clkdm_name);
276 if (clkdm) {
277 pr_debug("clock: associated clk %s to clkdm %s\n",
Rajendra Nayak5dcc3b92012-09-22 02:24:17 -0600278 clk_name, clk->clkdm_name);
Paul Walmsley333943b2008-08-19 11:08:45 +0300279 clk->clkdm = clkdm;
280 } else {
Paul Walmsley7852ec02012-07-26 00:54:26 -0600281 pr_debug("clock: could not associate clk %s to clkdm %s\n",
Rajendra Nayak5dcc3b92012-09-22 02:24:17 -0600282 clk_name, clk->clkdm_name);
Paul Walmsley333943b2008-08-19 11:08:45 +0300283 }
284}
285
286/**
Paul Walmsley12706c52011-07-10 05:57:06 -0600287 * omap2_clk_disable_clkdm_control - disable clkdm control on clk enable/disable
288 *
289 * Prevent the OMAP clock code from calling into the clockdomain code
290 * when a hardware clock in that clockdomain is enabled or disabled.
291 * Intended to be called at init time from omap*_clk_init(). No
292 * return value.
293 */
294void __init omap2_clk_disable_clkdm_control(void)
295{
296 clkdm_control = false;
297}
298
299/**
Paul Walmsley72350b22009-07-24 19:44:03 -0600300 * omap2_clk_dflt_find_companion - find companion clock to @clk
301 * @clk: struct clk * to find the companion clock of
302 * @other_reg: void __iomem ** to return the companion clock CM_*CLKEN va in
303 * @other_bit: u8 ** to return the companion clock bit shift in
Paul Walmsley543d9372008-03-18 10:22:06 +0200304 *
Paul Walmsley72350b22009-07-24 19:44:03 -0600305 * Note: We don't need special code here for INVERT_ENABLE for the
306 * time being since INVERT_ENABLE only applies to clocks enabled by
Paul Walmsley543d9372008-03-18 10:22:06 +0200307 * CM_CLKEN_PLL
Paul Walmsley72350b22009-07-24 19:44:03 -0600308 *
309 * Convert CM_ICLKEN* <-> CM_FCLKEN*. This conversion assumes it's
310 * just a matter of XORing the bits.
311 *
312 * Some clocks don't have companion clocks. For example, modules with
313 * only an interface clock (such as MAILBOXES) don't have a companion
314 * clock. Right now, this code relies on the hardware exporting a bit
315 * in the correct companion register that indicates that the
316 * nonexistent 'companion clock' is active. Future patches will
317 * associate this type of code with per-module data structures to
318 * avoid this issue, and remove the casts. No return value.
Paul Walmsley543d9372008-03-18 10:22:06 +0200319 */
Mike Turquette32cc0022012-11-10 16:58:41 -0700320void omap2_clk_dflt_find_companion(struct clk_hw_omap *clk,
Mike Turquette32cc0022012-11-10 16:58:41 -0700321 void __iomem **other_reg, u8 *other_bit)
Paul Walmsley543d9372008-03-18 10:22:06 +0200322{
Paul Walmsley72350b22009-07-24 19:44:03 -0600323 u32 r;
Paul Walmsley543d9372008-03-18 10:22:06 +0200324
Russell Kingc1168dc2008-11-04 21:24:00 +0000325 /*
326 * Convert CM_ICLKEN* <-> CM_FCLKEN*. This conversion assumes
327 * it's just a matter of XORing the bits.
328 */
Paul Walmsley72350b22009-07-24 19:44:03 -0600329 r = ((__force u32)clk->enable_reg ^ (CM_FCLKEN ^ CM_ICLKEN));
Paul Walmsley543d9372008-03-18 10:22:06 +0200330
Paul Walmsley72350b22009-07-24 19:44:03 -0600331 *other_reg = (__force void __iomem *)r;
332 *other_bit = clk->enable_bit;
Paul Walmsley543d9372008-03-18 10:22:06 +0200333}
334
Paul Walmsley72350b22009-07-24 19:44:03 -0600335/**
336 * omap2_clk_dflt_find_idlest - find CM_IDLEST reg va, bit shift for @clk
337 * @clk: struct clk * to find IDLEST info for
338 * @idlest_reg: void __iomem ** to return the CM_IDLEST va in
Ranjith Lohithakshan419cc972010-02-24 12:05:54 -0700339 * @idlest_bit: u8 * to return the CM_IDLEST bit shift in
340 * @idlest_val: u8 * to return the idle status indicator
Paul Walmsley72350b22009-07-24 19:44:03 -0600341 *
342 * Return the CM_IDLEST register address and bit shift corresponding
343 * to the module that "owns" this clock. This default code assumes
344 * that the CM_IDLEST bit shift is the CM_*CLKEN bit shift, and that
345 * the IDLEST register address ID corresponds to the CM_*CLKEN
346 * register address ID (e.g., that CM_FCLKEN2 corresponds to
347 * CM_IDLEST2). This is not true for all modules. No return value.
348 */
Mike Turquette32cc0022012-11-10 16:58:41 -0700349void omap2_clk_dflt_find_idlest(struct clk_hw_omap *clk,
Mike Turquette32cc0022012-11-10 16:58:41 -0700350 void __iomem **idlest_reg, u8 *idlest_bit, u8 *idlest_val)
Paul Walmsley72350b22009-07-24 19:44:03 -0600351{
352 u32 r;
353
354 r = (((__force u32)clk->enable_reg & ~0xf0) | 0x20);
355 *idlest_reg = (__force void __iomem *)r;
356 *idlest_bit = clk->enable_bit;
Ranjith Lohithakshan419cc972010-02-24 12:05:54 -0700357
358 /*
359 * 24xx uses 0 to indicate not ready, and 1 to indicate ready.
360 * 34xx reverses this, just to keep us on our toes
361 * AM35xx uses both, depending on the module.
362 */
Tero Kristof3b19aa2015-02-27 17:54:14 +0200363 *idlest_val = ti_clk_get_features()->cm_idlest_val;
Paul Walmsley72350b22009-07-24 19:44:03 -0600364}
365
Mike Turquette32cc0022012-11-10 16:58:41 -0700366/**
367 * omap2_dflt_clk_enable - enable a clock in the hardware
368 * @hw: struct clk_hw * of the clock to enable
369 *
370 * Enable the clock @hw in the hardware. We first call into the OMAP
371 * clockdomain code to "enable" the corresponding clockdomain if this
372 * is the first enabled user of the clockdomain. Then program the
373 * hardware to enable the clock. Then wait for the IP block that uses
374 * this clock to leave idle (if applicable). Returns the error value
375 * from clkdm_clk_enable() if it terminated with an error, or -EINVAL
376 * if @hw has a null clock enable_reg, or zero upon success.
377 */
378int omap2_dflt_clk_enable(struct clk_hw *hw)
379{
380 struct clk_hw_omap *clk;
381 u32 v;
382 int ret = 0;
383
384 clk = to_clk_hw_omap(hw);
385
386 if (clkdm_control && clk->clkdm) {
387 ret = clkdm_clk_enable(clk->clkdm, hw->clk);
388 if (ret) {
389 WARN(1, "%s: could not enable %s's clockdomain %s: %d\n",
390 __func__, __clk_get_name(hw->clk),
391 clk->clkdm->name, ret);
392 return ret;
393 }
394 }
395
396 if (unlikely(clk->enable_reg == NULL)) {
397 pr_err("%s: %s missing enable_reg\n", __func__,
398 __clk_get_name(hw->clk));
399 ret = -EINVAL;
400 goto err;
401 }
402
403 /* FIXME should not have INVERT_ENABLE bit here */
Tero Kristo519ab8b2013-10-22 11:49:58 +0300404 v = omap2_clk_readl(clk, clk->enable_reg);
Mike Turquette32cc0022012-11-10 16:58:41 -0700405 if (clk->flags & INVERT_ENABLE)
406 v &= ~(1 << clk->enable_bit);
407 else
408 v |= (1 << clk->enable_bit);
Tero Kristo519ab8b2013-10-22 11:49:58 +0300409 omap2_clk_writel(v, clk, clk->enable_reg);
410 v = omap2_clk_readl(clk, clk->enable_reg); /* OCP barrier */
Mike Turquette32cc0022012-11-10 16:58:41 -0700411
412 if (clk->ops && clk->ops->find_idlest)
413 _omap2_module_wait_ready(clk);
414
415 return 0;
416
417err:
418 if (clkdm_control && clk->clkdm)
419 clkdm_clk_disable(clk->clkdm, hw->clk);
420 return ret;
421}
422
423/**
424 * omap2_dflt_clk_disable - disable a clock in the hardware
425 * @hw: struct clk_hw * of the clock to disable
426 *
427 * Disable the clock @hw in the hardware, and call into the OMAP
428 * clockdomain code to "disable" the corresponding clockdomain if all
429 * clocks/hwmods in that clockdomain are now disabled. No return
430 * value.
431 */
432void omap2_dflt_clk_disable(struct clk_hw *hw)
433{
434 struct clk_hw_omap *clk;
435 u32 v;
436
437 clk = to_clk_hw_omap(hw);
438 if (!clk->enable_reg) {
439 /*
440 * 'independent' here refers to a clock which is not
441 * controlled by its parent.
442 */
443 pr_err("%s: independent clock %s has no enable_reg\n",
444 __func__, __clk_get_name(hw->clk));
445 return;
446 }
447
Tero Kristo519ab8b2013-10-22 11:49:58 +0300448 v = omap2_clk_readl(clk, clk->enable_reg);
Mike Turquette32cc0022012-11-10 16:58:41 -0700449 if (clk->flags & INVERT_ENABLE)
450 v |= (1 << clk->enable_bit);
451 else
452 v &= ~(1 << clk->enable_bit);
Tero Kristo519ab8b2013-10-22 11:49:58 +0300453 omap2_clk_writel(v, clk, clk->enable_reg);
Mike Turquette32cc0022012-11-10 16:58:41 -0700454 /* No OCP barrier needed here since it is a disable operation */
455
456 if (clkdm_control && clk->clkdm)
457 clkdm_clk_disable(clk->clkdm, hw->clk);
458}
459
460/**
461 * omap2_clkops_enable_clkdm - increment usecount on clkdm of @hw
462 * @hw: struct clk_hw * of the clock being enabled
463 *
464 * Increment the usecount of the clockdomain of the clock pointed to
465 * by @hw; if the usecount is 1, the clockdomain will be "enabled."
466 * Only needed for clocks that don't use omap2_dflt_clk_enable() as
467 * their enable function pointer. Passes along the return value of
468 * clkdm_clk_enable(), -EINVAL if @hw is not associated with a
469 * clockdomain, or 0 if clock framework-based clockdomain control is
470 * not implemented.
471 */
472int omap2_clkops_enable_clkdm(struct clk_hw *hw)
473{
474 struct clk_hw_omap *clk;
475 int ret = 0;
476
477 clk = to_clk_hw_omap(hw);
478
479 if (unlikely(!clk->clkdm)) {
480 pr_err("%s: %s: no clkdm set ?!\n", __func__,
481 __clk_get_name(hw->clk));
482 return -EINVAL;
483 }
484
485 if (unlikely(clk->enable_reg))
486 pr_err("%s: %s: should use dflt_clk_enable ?!\n", __func__,
487 __clk_get_name(hw->clk));
488
489 if (!clkdm_control) {
490 pr_err("%s: %s: clkfw-based clockdomain control disabled ?!\n",
491 __func__, __clk_get_name(hw->clk));
492 return 0;
493 }
494
495 ret = clkdm_clk_enable(clk->clkdm, hw->clk);
496 WARN(ret, "%s: could not enable %s's clockdomain %s: %d\n",
497 __func__, __clk_get_name(hw->clk), clk->clkdm->name, ret);
498
499 return ret;
500}
501
502/**
503 * omap2_clkops_disable_clkdm - decrement usecount on clkdm of @hw
504 * @hw: struct clk_hw * of the clock being disabled
505 *
506 * Decrement the usecount of the clockdomain of the clock pointed to
507 * by @hw; if the usecount is 0, the clockdomain will be "disabled."
508 * Only needed for clocks that don't use omap2_dflt_clk_disable() as their
509 * disable function pointer. No return value.
510 */
511void omap2_clkops_disable_clkdm(struct clk_hw *hw)
512{
513 struct clk_hw_omap *clk;
514
515 clk = to_clk_hw_omap(hw);
516
517 if (unlikely(!clk->clkdm)) {
518 pr_err("%s: %s: no clkdm set ?!\n", __func__,
519 __clk_get_name(hw->clk));
520 return;
521 }
522
523 if (unlikely(clk->enable_reg))
524 pr_err("%s: %s: should use dflt_clk_disable ?!\n", __func__,
525 __clk_get_name(hw->clk));
526
527 if (!clkdm_control) {
528 pr_err("%s: %s: clkfw-based clockdomain control disabled ?!\n",
529 __func__, __clk_get_name(hw->clk));
530 return;
531 }
532
533 clkdm_clk_disable(clk->clkdm, hw->clk);
534}
535
536/**
537 * omap2_dflt_clk_is_enabled - is clock enabled in the hardware?
538 * @hw: struct clk_hw * to check
539 *
540 * Return 1 if the clock represented by @hw is enabled in the
541 * hardware, or 0 otherwise. Intended for use in the struct
542 * clk_ops.is_enabled function pointer.
543 */
544int omap2_dflt_clk_is_enabled(struct clk_hw *hw)
545{
546 struct clk_hw_omap *clk = to_clk_hw_omap(hw);
547 u32 v;
548
Tero Kristo519ab8b2013-10-22 11:49:58 +0300549 v = omap2_clk_readl(clk, clk->enable_reg);
Mike Turquette32cc0022012-11-10 16:58:41 -0700550
551 if (clk->flags & INVERT_ENABLE)
552 v ^= BIT(clk->enable_bit);
553
554 v &= BIT(clk->enable_bit);
555
556 return v ? 1 : 0;
557}
558
559static int __initdata mpurate;
560
561/*
562 * By default we use the rate set by the bootloader.
563 * You can override this with mpurate= cmdline option.
564 */
565static int __init omap_clk_setup(char *str)
566{
567 get_option(&str, &mpurate);
568
569 if (!mpurate)
570 return 1;
571
572 if (mpurate < 1000)
573 mpurate *= 1000000;
574
575 return 1;
576}
577__setup("mpurate=", omap_clk_setup);
578
579const struct clk_hw_omap_ops clkhwops_wait = {
580 .find_idlest = omap2_clk_dflt_find_idlest,
581 .find_companion = omap2_clk_dflt_find_companion,
582};
Mike Turquette32cc0022012-11-10 16:58:41 -0700583
Paul Walmsley4d30e822010-02-22 22:09:36 -0700584/**
Paul Walmsley4d30e822010-02-22 22:09:36 -0700585 * omap2_clk_print_new_rates - print summary of current clock tree rates
586 * @hfclkin_ck_name: clk name for the off-chip HF oscillator
587 * @core_ck_name: clk name for the on-chip CORE_CLK
588 * @mpu_ck_name: clk name for the ARM MPU clock
589 *
590 * Prints a short message to the console with the HFCLKIN oscillator
591 * rate, the rate of the CORE clock, and the rate of the ARM MPU clock.
592 * Called by the boot-time MPU rate switching code. XXX This is intended
593 * to be handled by the OPP layer code in the near future and should be
594 * removed from the clock code. No return value.
595 */
596void __init omap2_clk_print_new_rates(const char *hfclkin_ck_name,
597 const char *core_ck_name,
598 const char *mpu_ck_name)
599{
600 struct clk *hfclkin_ck, *core_ck, *mpu_ck;
601 unsigned long hfclkin_rate;
602
603 mpu_ck = clk_get(NULL, mpu_ck_name);
604 if (WARN(IS_ERR(mpu_ck), "clock: failed to get %s.\n", mpu_ck_name))
605 return;
606
607 core_ck = clk_get(NULL, core_ck_name);
608 if (WARN(IS_ERR(core_ck), "clock: failed to get %s.\n", core_ck_name))
609 return;
610
611 hfclkin_ck = clk_get(NULL, hfclkin_ck_name);
612 if (WARN(IS_ERR(hfclkin_ck), "Failed to get %s.\n", hfclkin_ck_name))
613 return;
614
615 hfclkin_rate = clk_get_rate(hfclkin_ck);
616
Paul Walmsley7852ec02012-07-26 00:54:26 -0600617 pr_info("Switched to new clocking rate (Crystal/Core/MPU): %ld.%01ld/%ld/%ld MHz\n",
618 (hfclkin_rate / 1000000), ((hfclkin_rate / 100000) % 10),
Paul Walmsley4d30e822010-02-22 22:09:36 -0700619 (clk_get_rate(core_ck) / 1000000),
620 (clk_get_rate(mpu_ck) / 1000000));
621}
Tero Kristo8111e012014-07-02 11:47:39 +0300622
623/**
624 * ti_clk_init_features - init clock features struct for the SoC
625 *
626 * Initializes the clock features struct based on the SoC type.
627 */
628void __init ti_clk_init_features(void)
629{
Tero Kristof3b19aa2015-02-27 17:54:14 +0200630 struct ti_clk_features features = { 0 };
Tero Kristoa24886e2014-07-02 11:47:40 +0300631 /* Fint setup for DPLLs */
632 if (cpu_is_omap3430()) {
Tero Kristof3b19aa2015-02-27 17:54:14 +0200633 features.fint_min = OMAP3430_DPLL_FINT_BAND1_MIN;
634 features.fint_max = OMAP3430_DPLL_FINT_BAND2_MAX;
635 features.fint_band1_max = OMAP3430_DPLL_FINT_BAND1_MAX;
636 features.fint_band2_min = OMAP3430_DPLL_FINT_BAND2_MIN;
Tero Kristoa24886e2014-07-02 11:47:40 +0300637 } else {
Tero Kristof3b19aa2015-02-27 17:54:14 +0200638 features.fint_min = OMAP3PLUS_DPLL_FINT_MIN;
639 features.fint_max = OMAP3PLUS_DPLL_FINT_MAX;
Tero Kristoa24886e2014-07-02 11:47:40 +0300640 }
Tero Kristo512d91c2014-07-02 11:47:42 +0300641
642 /* Bypass value setup for DPLLs */
643 if (cpu_is_omap24xx()) {
Tero Kristof3b19aa2015-02-27 17:54:14 +0200644 features.dpll_bypass_vals |=
Tero Kristo512d91c2014-07-02 11:47:42 +0300645 (1 << OMAP2XXX_EN_DPLL_LPBYPASS) |
646 (1 << OMAP2XXX_EN_DPLL_FRBYPASS);
647 } else if (cpu_is_omap34xx()) {
Tero Kristof3b19aa2015-02-27 17:54:14 +0200648 features.dpll_bypass_vals |=
Tero Kristo512d91c2014-07-02 11:47:42 +0300649 (1 << OMAP3XXX_EN_DPLL_LPBYPASS) |
650 (1 << OMAP3XXX_EN_DPLL_FRBYPASS);
651 } else if (soc_is_am33xx() || cpu_is_omap44xx() || soc_is_am43xx() ||
652 soc_is_omap54xx() || soc_is_dra7xx()) {
Tero Kristof3b19aa2015-02-27 17:54:14 +0200653 features.dpll_bypass_vals |=
Tero Kristo512d91c2014-07-02 11:47:42 +0300654 (1 << OMAP4XXX_EN_DPLL_LPBYPASS) |
655 (1 << OMAP4XXX_EN_DPLL_FRBYPASS) |
656 (1 << OMAP4XXX_EN_DPLL_MNBYPASS);
657 }
Tero Kristo2337c5b2014-07-02 11:47:43 +0300658
659 /* Jitter correction only available on OMAP343X */
660 if (cpu_is_omap343x())
Tero Kristof3b19aa2015-02-27 17:54:14 +0200661 features.flags |= TI_CLK_DPLL_HAS_FREQSEL;
Tero Kristo066edb22014-07-02 11:47:44 +0300662
663 /* Idlest value for interface clocks.
664 * 24xx uses 0 to indicate not ready, and 1 to indicate ready.
665 * 34xx reverses this, just to keep us on our toes
666 * AM35xx uses both, depending on the module.
667 */
668 if (cpu_is_omap24xx())
Tero Kristof3b19aa2015-02-27 17:54:14 +0200669 features.cm_idlest_val = OMAP24XX_CM_IDLEST_VAL;
Tero Kristo066edb22014-07-02 11:47:44 +0300670 else if (cpu_is_omap34xx())
Tero Kristof3b19aa2015-02-27 17:54:14 +0200671 features.cm_idlest_val = OMAP34XX_CM_IDLEST_VAL;
Tero Kristof0d2f682014-10-03 16:57:10 +0300672
673 /* On OMAP3430 ES1.0, DPLL4 can't be re-programmed */
674 if (omap_rev() == OMAP3430_REV_ES1_0)
Tero Kristof3b19aa2015-02-27 17:54:14 +0200675 features.flags |= TI_CLK_DPLL4_DENY_REPROGRAM;
676
677 ti_clk_setup_features(&features);
Tero Kristo8111e012014-07-02 11:47:39 +0300678}