blob: d6afc1291fe9fd3a0dcbe776dc59169e70b0abc7 [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,
Tero Kristo9a356d62015-03-03 11:14:31 +0200123 .clkdm_clk_enable = clkdm_clk_enable,
124 .clkdm_clk_disable = clkdm_clk_disable,
Tero Kristo9f029b12014-10-22 15:15:36 +0300125};
Tero Kristo3ada6b102013-10-22 11:47:08 +0300126
Tero Kristo9f029b12014-10-22 15:15:36 +0300127/**
128 * omap2_clk_provider_init - initialize a clock provider
129 * @match_table: DT device table to match for devices to init
130 * @np: device node pointer for the this clock provider
131 * @index: index for the clock provider
Tero Kristo80cbb222015-02-06 16:00:32 +0200132 + @syscon: syscon regmap pointer
133 * @mem: iomem pointer for the clock provider memory area, only used if
134 * syscon is not provided
Tero Kristo9f029b12014-10-22 15:15:36 +0300135 *
136 * Initializes a clock provider module (CM/PRM etc.), registering
137 * the memory mapping at specified index and initializing the
138 * low level driver infrastructure. Returns 0 in success.
139 */
140int __init omap2_clk_provider_init(struct device_node *np, int index,
Tero Kristo80cbb222015-02-06 16:00:32 +0200141 struct regmap *syscon, void __iomem *mem)
Tero Kristo9f029b12014-10-22 15:15:36 +0300142{
Tero Kristo80cbb222015-02-06 16:00:32 +0200143 struct clk_iomap *io;
144
Tero Kristo9f029b12014-10-22 15:15:36 +0300145 ti_clk_ll_ops = &omap_clk_ll_ops;
146
Tero Kristo80cbb222015-02-06 16:00:32 +0200147 io = kzalloc(sizeof(*io), GFP_KERNEL);
148
149 io->regmap = syscon;
150 io->mem = mem;
151
152 clk_memmaps[index] = io;
Tero Kristo9f029b12014-10-22 15:15:36 +0300153
154 ti_dt_clk_init_provider(np, index);
155
156 return 0;
157}
158
159/**
160 * omap2_clk_legacy_provider_init - initialize a legacy clock provider
161 * @index: index for the clock provider
162 * @mem: iomem pointer for the clock provider memory area
163 *
164 * Initializes a legacy clock provider memory mapping.
165 */
166void __init omap2_clk_legacy_provider_init(int index, void __iomem *mem)
167{
Tero Kristo80cbb222015-02-06 16:00:32 +0200168 struct clk_iomap *io;
169
Tero Kristo9f029b12014-10-22 15:15:36 +0300170 ti_clk_ll_ops = &omap_clk_ll_ops;
171
Tero Kristo80cbb222015-02-06 16:00:32 +0200172 io = memblock_virt_alloc(sizeof(*io), 0);
173
174 io->mem = mem;
175
176 clk_memmaps[index] = io;
Tero Kristo3ada6b102013-10-22 11:47:08 +0300177}
Mike Turquette32cc0022012-11-10 16:58:41 -0700178
179/*
Paul Walmsley30962d92010-02-22 22:09:38 -0700180 * OMAP2+ specific clock functions
181 */
Paul Walmsley543d9372008-03-18 10:22:06 +0200182
Paul Walmsley4b1f76e2010-01-26 20:13:04 -0700183/* Private functions */
184
Paul Walmsleyc4ceedc2012-10-29 20:56:29 -0600185
186/**
187 * _wait_idlest_generic - wait for a module to leave the idle state
Tero Kristo519ab8b2013-10-22 11:49:58 +0300188 * @clk: module clock to wait for (needed for register offsets)
Paul Walmsleyc4ceedc2012-10-29 20:56:29 -0600189 * @reg: virtual address of module IDLEST register
190 * @mask: value to mask against to determine if the module is active
191 * @idlest: idle state indicator (0 or 1) for the clock
192 * @name: name of the clock (for printk)
193 *
194 * Wait for a module to leave idle, where its idle-status register is
195 * not inside the CM module. Returns 1 if the module left idle
196 * promptly, or 0 if the module did not leave idle before the timeout
197 * elapsed. XXX Deprecated - should be moved into drivers for the
198 * individual IP block that the IDLEST register exists in.
199 */
Tero Kristo519ab8b2013-10-22 11:49:58 +0300200static int _wait_idlest_generic(struct clk_hw_omap *clk, void __iomem *reg,
201 u32 mask, u8 idlest, const char *name)
Paul Walmsleyc4ceedc2012-10-29 20:56:29 -0600202{
203 int i = 0, ena = 0;
204
205 ena = (idlest) ? 0 : mask;
206
Tero Kristo519ab8b2013-10-22 11:49:58 +0300207 omap_test_timeout(((omap2_clk_readl(clk, reg) & mask) == ena),
Paul Walmsleyc4ceedc2012-10-29 20:56:29 -0600208 MAX_MODULE_ENABLE_WAIT, i);
209
210 if (i < MAX_MODULE_ENABLE_WAIT)
211 pr_debug("omap clock: module associated with clock %s ready after %d loops\n",
212 name, i);
213 else
214 pr_err("omap clock: module associated with clock %s didn't enable in %d tries\n",
215 name, MAX_MODULE_ENABLE_WAIT);
216
217 return (i < MAX_MODULE_ENABLE_WAIT) ? 1 : 0;
218};
219
Paul Walmsley4b1f76e2010-01-26 20:13:04 -0700220/**
221 * _omap2_module_wait_ready - wait for an OMAP module to leave IDLE
222 * @clk: struct clk * belonging to the module
223 *
224 * If the necessary clocks for the OMAP hardware IP block that
225 * corresponds to clock @clk are enabled, then wait for the module to
226 * indicate readiness (i.e., to leave IDLE). This code does not
227 * belong in the clock code and will be moved in the medium term to
228 * module-dependent code. No return value.
229 */
Mike Turquette32cc0022012-11-10 16:58:41 -0700230static void _omap2_module_wait_ready(struct clk_hw_omap *clk)
Paul Walmsley4b1f76e2010-01-26 20:13:04 -0700231{
232 void __iomem *companion_reg, *idlest_reg;
Paul Walmsleyc4ceedc2012-10-29 20:56:29 -0600233 u8 other_bit, idlest_bit, idlest_val, idlest_reg_id;
234 s16 prcm_mod;
235 int r;
Paul Walmsley4b1f76e2010-01-26 20:13:04 -0700236
237 /* Not all modules have multiple clocks that their IDLEST depends on */
238 if (clk->ops->find_companion) {
239 clk->ops->find_companion(clk, &companion_reg, &other_bit);
Tero Kristo519ab8b2013-10-22 11:49:58 +0300240 if (!(omap2_clk_readl(clk, companion_reg) & (1 << other_bit)))
Paul Walmsley4b1f76e2010-01-26 20:13:04 -0700241 return;
242 }
243
Ranjith Lohithakshan419cc972010-02-24 12:05:54 -0700244 clk->ops->find_idlest(clk, &idlest_reg, &idlest_bit, &idlest_val);
Paul Walmsleyc4ceedc2012-10-29 20:56:29 -0600245 r = cm_split_idlest_reg(idlest_reg, &prcm_mod, &idlest_reg_id);
246 if (r) {
247 /* IDLEST register not in the CM module */
Tero Kristo519ab8b2013-10-22 11:49:58 +0300248 _wait_idlest_generic(clk, idlest_reg, (1 << idlest_bit),
249 idlest_val, __clk_get_name(clk->hw.clk));
Paul Walmsleyc4ceedc2012-10-29 20:56:29 -0600250 } else {
Tero Kristo021b6ff2014-10-27 08:39:23 -0700251 omap_cm_wait_module_ready(0, prcm_mod, idlest_reg_id,
252 idlest_bit);
Paul Walmsleyc4ceedc2012-10-29 20:56:29 -0600253 };
Paul Walmsley4b1f76e2010-01-26 20:13:04 -0700254}
255
Paul Walmsley4b1f76e2010-01-26 20:13:04 -0700256/* Public functions */
257
Paul Walmsley543d9372008-03-18 10:22:06 +0200258/**
Paul Walmsley333943b2008-08-19 11:08:45 +0300259 * omap2_init_clk_clkdm - look up a clockdomain name, store pointer in clk
260 * @clk: OMAP clock struct ptr to use
261 *
262 * Convert a clockdomain name stored in a struct clk 'clk' into a
263 * clockdomain pointer, and save it into the struct clk. Intended to be
264 * called during clk_register(). No return value.
265 */
Mike Turquette32cc0022012-11-10 16:58:41 -0700266void omap2_init_clk_clkdm(struct clk_hw *hw)
267{
268 struct clk_hw_omap *clk = to_clk_hw_omap(hw);
Paul Walmsley333943b2008-08-19 11:08:45 +0300269 struct clockdomain *clkdm;
Rajendra Nayak5dcc3b92012-09-22 02:24:17 -0600270 const char *clk_name;
Paul Walmsley333943b2008-08-19 11:08:45 +0300271
272 if (!clk->clkdm_name)
273 return;
274
Mike Turquette32cc0022012-11-10 16:58:41 -0700275 clk_name = __clk_get_name(hw->clk);
Rajendra Nayak5dcc3b92012-09-22 02:24:17 -0600276
Paul Walmsley333943b2008-08-19 11:08:45 +0300277 clkdm = clkdm_lookup(clk->clkdm_name);
278 if (clkdm) {
279 pr_debug("clock: associated clk %s to clkdm %s\n",
Rajendra Nayak5dcc3b92012-09-22 02:24:17 -0600280 clk_name, clk->clkdm_name);
Paul Walmsley333943b2008-08-19 11:08:45 +0300281 clk->clkdm = clkdm;
282 } else {
Paul Walmsley7852ec02012-07-26 00:54:26 -0600283 pr_debug("clock: could not associate clk %s to clkdm %s\n",
Rajendra Nayak5dcc3b92012-09-22 02:24:17 -0600284 clk_name, clk->clkdm_name);
Paul Walmsley333943b2008-08-19 11:08:45 +0300285 }
286}
287
288/**
Paul Walmsley12706c52011-07-10 05:57:06 -0600289 * omap2_clk_disable_clkdm_control - disable clkdm control on clk enable/disable
290 *
291 * Prevent the OMAP clock code from calling into the clockdomain code
292 * when a hardware clock in that clockdomain is enabled or disabled.
293 * Intended to be called at init time from omap*_clk_init(). No
294 * return value.
295 */
296void __init omap2_clk_disable_clkdm_control(void)
297{
298 clkdm_control = false;
299}
300
301/**
Paul Walmsley72350b22009-07-24 19:44:03 -0600302 * omap2_clk_dflt_find_companion - find companion clock to @clk
303 * @clk: struct clk * to find the companion clock of
304 * @other_reg: void __iomem ** to return the companion clock CM_*CLKEN va in
305 * @other_bit: u8 ** to return the companion clock bit shift in
Paul Walmsley543d9372008-03-18 10:22:06 +0200306 *
Paul Walmsley72350b22009-07-24 19:44:03 -0600307 * Note: We don't need special code here for INVERT_ENABLE for the
308 * time being since INVERT_ENABLE only applies to clocks enabled by
Paul Walmsley543d9372008-03-18 10:22:06 +0200309 * CM_CLKEN_PLL
Paul Walmsley72350b22009-07-24 19:44:03 -0600310 *
311 * Convert CM_ICLKEN* <-> CM_FCLKEN*. This conversion assumes it's
312 * just a matter of XORing the bits.
313 *
314 * Some clocks don't have companion clocks. For example, modules with
315 * only an interface clock (such as MAILBOXES) don't have a companion
316 * clock. Right now, this code relies on the hardware exporting a bit
317 * in the correct companion register that indicates that the
318 * nonexistent 'companion clock' is active. Future patches will
319 * associate this type of code with per-module data structures to
320 * avoid this issue, and remove the casts. No return value.
Paul Walmsley543d9372008-03-18 10:22:06 +0200321 */
Mike Turquette32cc0022012-11-10 16:58:41 -0700322void omap2_clk_dflt_find_companion(struct clk_hw_omap *clk,
Mike Turquette32cc0022012-11-10 16:58:41 -0700323 void __iomem **other_reg, u8 *other_bit)
Paul Walmsley543d9372008-03-18 10:22:06 +0200324{
Paul Walmsley72350b22009-07-24 19:44:03 -0600325 u32 r;
Paul Walmsley543d9372008-03-18 10:22:06 +0200326
Russell Kingc1168dc32008-11-04 21:24:00 +0000327 /*
328 * Convert CM_ICLKEN* <-> CM_FCLKEN*. This conversion assumes
329 * it's just a matter of XORing the bits.
330 */
Paul Walmsley72350b22009-07-24 19:44:03 -0600331 r = ((__force u32)clk->enable_reg ^ (CM_FCLKEN ^ CM_ICLKEN));
Paul Walmsley543d9372008-03-18 10:22:06 +0200332
Paul Walmsley72350b22009-07-24 19:44:03 -0600333 *other_reg = (__force void __iomem *)r;
334 *other_bit = clk->enable_bit;
Paul Walmsley543d9372008-03-18 10:22:06 +0200335}
336
Paul Walmsley72350b22009-07-24 19:44:03 -0600337/**
338 * omap2_clk_dflt_find_idlest - find CM_IDLEST reg va, bit shift for @clk
339 * @clk: struct clk * to find IDLEST info for
340 * @idlest_reg: void __iomem ** to return the CM_IDLEST va in
Ranjith Lohithakshan419cc972010-02-24 12:05:54 -0700341 * @idlest_bit: u8 * to return the CM_IDLEST bit shift in
342 * @idlest_val: u8 * to return the idle status indicator
Paul Walmsley72350b22009-07-24 19:44:03 -0600343 *
344 * Return the CM_IDLEST register address and bit shift corresponding
345 * to the module that "owns" this clock. This default code assumes
346 * that the CM_IDLEST bit shift is the CM_*CLKEN bit shift, and that
347 * the IDLEST register address ID corresponds to the CM_*CLKEN
348 * register address ID (e.g., that CM_FCLKEN2 corresponds to
349 * CM_IDLEST2). This is not true for all modules. No return value.
350 */
Mike Turquette32cc0022012-11-10 16:58:41 -0700351void omap2_clk_dflt_find_idlest(struct clk_hw_omap *clk,
Mike Turquette32cc0022012-11-10 16:58:41 -0700352 void __iomem **idlest_reg, u8 *idlest_bit, u8 *idlest_val)
Paul Walmsley72350b22009-07-24 19:44:03 -0600353{
354 u32 r;
355
356 r = (((__force u32)clk->enable_reg & ~0xf0) | 0x20);
357 *idlest_reg = (__force void __iomem *)r;
358 *idlest_bit = clk->enable_bit;
Ranjith Lohithakshan419cc972010-02-24 12:05:54 -0700359
360 /*
361 * 24xx uses 0 to indicate not ready, and 1 to indicate ready.
362 * 34xx reverses this, just to keep us on our toes
363 * AM35xx uses both, depending on the module.
364 */
Tero Kristof3b19aa2015-02-27 17:54:14 +0200365 *idlest_val = ti_clk_get_features()->cm_idlest_val;
Paul Walmsley72350b22009-07-24 19:44:03 -0600366}
367
Mike Turquette32cc0022012-11-10 16:58:41 -0700368/**
369 * omap2_dflt_clk_enable - enable a clock in the hardware
370 * @hw: struct clk_hw * of the clock to enable
371 *
372 * Enable the clock @hw in the hardware. We first call into the OMAP
373 * clockdomain code to "enable" the corresponding clockdomain if this
374 * is the first enabled user of the clockdomain. Then program the
375 * hardware to enable the clock. Then wait for the IP block that uses
376 * this clock to leave idle (if applicable). Returns the error value
377 * from clkdm_clk_enable() if it terminated with an error, or -EINVAL
378 * if @hw has a null clock enable_reg, or zero upon success.
379 */
380int omap2_dflt_clk_enable(struct clk_hw *hw)
381{
382 struct clk_hw_omap *clk;
383 u32 v;
384 int ret = 0;
385
386 clk = to_clk_hw_omap(hw);
387
388 if (clkdm_control && clk->clkdm) {
389 ret = clkdm_clk_enable(clk->clkdm, hw->clk);
390 if (ret) {
391 WARN(1, "%s: could not enable %s's clockdomain %s: %d\n",
392 __func__, __clk_get_name(hw->clk),
393 clk->clkdm->name, ret);
394 return ret;
395 }
396 }
397
398 if (unlikely(clk->enable_reg == NULL)) {
399 pr_err("%s: %s missing enable_reg\n", __func__,
400 __clk_get_name(hw->clk));
401 ret = -EINVAL;
402 goto err;
403 }
404
405 /* FIXME should not have INVERT_ENABLE bit here */
Tero Kristo519ab8b2013-10-22 11:49:58 +0300406 v = omap2_clk_readl(clk, clk->enable_reg);
Mike Turquette32cc0022012-11-10 16:58:41 -0700407 if (clk->flags & INVERT_ENABLE)
408 v &= ~(1 << clk->enable_bit);
409 else
410 v |= (1 << clk->enable_bit);
Tero Kristo519ab8b2013-10-22 11:49:58 +0300411 omap2_clk_writel(v, clk, clk->enable_reg);
412 v = omap2_clk_readl(clk, clk->enable_reg); /* OCP barrier */
Mike Turquette32cc0022012-11-10 16:58:41 -0700413
414 if (clk->ops && clk->ops->find_idlest)
415 _omap2_module_wait_ready(clk);
416
417 return 0;
418
419err:
420 if (clkdm_control && clk->clkdm)
421 clkdm_clk_disable(clk->clkdm, hw->clk);
422 return ret;
423}
424
425/**
426 * omap2_dflt_clk_disable - disable a clock in the hardware
427 * @hw: struct clk_hw * of the clock to disable
428 *
429 * Disable the clock @hw in the hardware, and call into the OMAP
430 * clockdomain code to "disable" the corresponding clockdomain if all
431 * clocks/hwmods in that clockdomain are now disabled. No return
432 * value.
433 */
434void omap2_dflt_clk_disable(struct clk_hw *hw)
435{
436 struct clk_hw_omap *clk;
437 u32 v;
438
439 clk = to_clk_hw_omap(hw);
440 if (!clk->enable_reg) {
441 /*
442 * 'independent' here refers to a clock which is not
443 * controlled by its parent.
444 */
445 pr_err("%s: independent clock %s has no enable_reg\n",
446 __func__, __clk_get_name(hw->clk));
447 return;
448 }
449
Tero Kristo519ab8b2013-10-22 11:49:58 +0300450 v = omap2_clk_readl(clk, clk->enable_reg);
Mike Turquette32cc0022012-11-10 16:58:41 -0700451 if (clk->flags & INVERT_ENABLE)
452 v |= (1 << clk->enable_bit);
453 else
454 v &= ~(1 << clk->enable_bit);
Tero Kristo519ab8b2013-10-22 11:49:58 +0300455 omap2_clk_writel(v, clk, clk->enable_reg);
Mike Turquette32cc0022012-11-10 16:58:41 -0700456 /* No OCP barrier needed here since it is a disable operation */
457
458 if (clkdm_control && clk->clkdm)
459 clkdm_clk_disable(clk->clkdm, hw->clk);
460}
461
462/**
463 * omap2_clkops_enable_clkdm - increment usecount on clkdm of @hw
464 * @hw: struct clk_hw * of the clock being enabled
465 *
466 * Increment the usecount of the clockdomain of the clock pointed to
467 * by @hw; if the usecount is 1, the clockdomain will be "enabled."
468 * Only needed for clocks that don't use omap2_dflt_clk_enable() as
469 * their enable function pointer. Passes along the return value of
470 * clkdm_clk_enable(), -EINVAL if @hw is not associated with a
471 * clockdomain, or 0 if clock framework-based clockdomain control is
472 * not implemented.
473 */
474int omap2_clkops_enable_clkdm(struct clk_hw *hw)
475{
476 struct clk_hw_omap *clk;
477 int ret = 0;
478
479 clk = to_clk_hw_omap(hw);
480
481 if (unlikely(!clk->clkdm)) {
482 pr_err("%s: %s: no clkdm set ?!\n", __func__,
483 __clk_get_name(hw->clk));
484 return -EINVAL;
485 }
486
487 if (unlikely(clk->enable_reg))
488 pr_err("%s: %s: should use dflt_clk_enable ?!\n", __func__,
489 __clk_get_name(hw->clk));
490
491 if (!clkdm_control) {
492 pr_err("%s: %s: clkfw-based clockdomain control disabled ?!\n",
493 __func__, __clk_get_name(hw->clk));
494 return 0;
495 }
496
497 ret = clkdm_clk_enable(clk->clkdm, hw->clk);
498 WARN(ret, "%s: could not enable %s's clockdomain %s: %d\n",
499 __func__, __clk_get_name(hw->clk), clk->clkdm->name, ret);
500
501 return ret;
502}
503
504/**
505 * omap2_clkops_disable_clkdm - decrement usecount on clkdm of @hw
506 * @hw: struct clk_hw * of the clock being disabled
507 *
508 * Decrement the usecount of the clockdomain of the clock pointed to
509 * by @hw; if the usecount is 0, the clockdomain will be "disabled."
510 * Only needed for clocks that don't use omap2_dflt_clk_disable() as their
511 * disable function pointer. No return value.
512 */
513void omap2_clkops_disable_clkdm(struct clk_hw *hw)
514{
515 struct clk_hw_omap *clk;
516
517 clk = to_clk_hw_omap(hw);
518
519 if (unlikely(!clk->clkdm)) {
520 pr_err("%s: %s: no clkdm set ?!\n", __func__,
521 __clk_get_name(hw->clk));
522 return;
523 }
524
525 if (unlikely(clk->enable_reg))
526 pr_err("%s: %s: should use dflt_clk_disable ?!\n", __func__,
527 __clk_get_name(hw->clk));
528
529 if (!clkdm_control) {
530 pr_err("%s: %s: clkfw-based clockdomain control disabled ?!\n",
531 __func__, __clk_get_name(hw->clk));
532 return;
533 }
534
535 clkdm_clk_disable(clk->clkdm, hw->clk);
536}
537
538/**
539 * omap2_dflt_clk_is_enabled - is clock enabled in the hardware?
540 * @hw: struct clk_hw * to check
541 *
542 * Return 1 if the clock represented by @hw is enabled in the
543 * hardware, or 0 otherwise. Intended for use in the struct
544 * clk_ops.is_enabled function pointer.
545 */
546int omap2_dflt_clk_is_enabled(struct clk_hw *hw)
547{
548 struct clk_hw_omap *clk = to_clk_hw_omap(hw);
549 u32 v;
550
Tero Kristo519ab8b2013-10-22 11:49:58 +0300551 v = omap2_clk_readl(clk, clk->enable_reg);
Mike Turquette32cc0022012-11-10 16:58:41 -0700552
553 if (clk->flags & INVERT_ENABLE)
554 v ^= BIT(clk->enable_bit);
555
556 v &= BIT(clk->enable_bit);
557
558 return v ? 1 : 0;
559}
560
561static int __initdata mpurate;
562
563/*
564 * By default we use the rate set by the bootloader.
565 * You can override this with mpurate= cmdline option.
566 */
567static int __init omap_clk_setup(char *str)
568{
569 get_option(&str, &mpurate);
570
571 if (!mpurate)
572 return 1;
573
574 if (mpurate < 1000)
575 mpurate *= 1000000;
576
577 return 1;
578}
579__setup("mpurate=", omap_clk_setup);
580
581const struct clk_hw_omap_ops clkhwops_wait = {
582 .find_idlest = omap2_clk_dflt_find_idlest,
583 .find_companion = omap2_clk_dflt_find_companion,
584};
Mike Turquette32cc0022012-11-10 16:58:41 -0700585
Paul Walmsley4d30e822010-02-22 22:09:36 -0700586/**
Paul Walmsley4d30e822010-02-22 22:09:36 -0700587 * omap2_clk_print_new_rates - print summary of current clock tree rates
588 * @hfclkin_ck_name: clk name for the off-chip HF oscillator
589 * @core_ck_name: clk name for the on-chip CORE_CLK
590 * @mpu_ck_name: clk name for the ARM MPU clock
591 *
592 * Prints a short message to the console with the HFCLKIN oscillator
593 * rate, the rate of the CORE clock, and the rate of the ARM MPU clock.
594 * Called by the boot-time MPU rate switching code. XXX This is intended
595 * to be handled by the OPP layer code in the near future and should be
596 * removed from the clock code. No return value.
597 */
598void __init omap2_clk_print_new_rates(const char *hfclkin_ck_name,
599 const char *core_ck_name,
600 const char *mpu_ck_name)
601{
602 struct clk *hfclkin_ck, *core_ck, *mpu_ck;
603 unsigned long hfclkin_rate;
604
605 mpu_ck = clk_get(NULL, mpu_ck_name);
606 if (WARN(IS_ERR(mpu_ck), "clock: failed to get %s.\n", mpu_ck_name))
607 return;
608
609 core_ck = clk_get(NULL, core_ck_name);
610 if (WARN(IS_ERR(core_ck), "clock: failed to get %s.\n", core_ck_name))
611 return;
612
613 hfclkin_ck = clk_get(NULL, hfclkin_ck_name);
614 if (WARN(IS_ERR(hfclkin_ck), "Failed to get %s.\n", hfclkin_ck_name))
615 return;
616
617 hfclkin_rate = clk_get_rate(hfclkin_ck);
618
Paul Walmsley7852ec02012-07-26 00:54:26 -0600619 pr_info("Switched to new clocking rate (Crystal/Core/MPU): %ld.%01ld/%ld/%ld MHz\n",
620 (hfclkin_rate / 1000000), ((hfclkin_rate / 100000) % 10),
Paul Walmsley4d30e822010-02-22 22:09:36 -0700621 (clk_get_rate(core_ck) / 1000000),
622 (clk_get_rate(mpu_ck) / 1000000));
623}
Tero Kristo8111e012014-07-02 11:47:39 +0300624
625/**
626 * ti_clk_init_features - init clock features struct for the SoC
627 *
628 * Initializes the clock features struct based on the SoC type.
629 */
630void __init ti_clk_init_features(void)
631{
Tero Kristof3b19aa2015-02-27 17:54:14 +0200632 struct ti_clk_features features = { 0 };
Tero Kristoa24886e2014-07-02 11:47:40 +0300633 /* Fint setup for DPLLs */
634 if (cpu_is_omap3430()) {
Tero Kristof3b19aa2015-02-27 17:54:14 +0200635 features.fint_min = OMAP3430_DPLL_FINT_BAND1_MIN;
636 features.fint_max = OMAP3430_DPLL_FINT_BAND2_MAX;
637 features.fint_band1_max = OMAP3430_DPLL_FINT_BAND1_MAX;
638 features.fint_band2_min = OMAP3430_DPLL_FINT_BAND2_MIN;
Tero Kristoa24886e2014-07-02 11:47:40 +0300639 } else {
Tero Kristof3b19aa2015-02-27 17:54:14 +0200640 features.fint_min = OMAP3PLUS_DPLL_FINT_MIN;
641 features.fint_max = OMAP3PLUS_DPLL_FINT_MAX;
Tero Kristoa24886e2014-07-02 11:47:40 +0300642 }
Tero Kristo512d91c2014-07-02 11:47:42 +0300643
644 /* Bypass value setup for DPLLs */
645 if (cpu_is_omap24xx()) {
Tero Kristof3b19aa2015-02-27 17:54:14 +0200646 features.dpll_bypass_vals |=
Tero Kristo512d91c2014-07-02 11:47:42 +0300647 (1 << OMAP2XXX_EN_DPLL_LPBYPASS) |
648 (1 << OMAP2XXX_EN_DPLL_FRBYPASS);
649 } else if (cpu_is_omap34xx()) {
Tero Kristof3b19aa2015-02-27 17:54:14 +0200650 features.dpll_bypass_vals |=
Tero Kristo512d91c2014-07-02 11:47:42 +0300651 (1 << OMAP3XXX_EN_DPLL_LPBYPASS) |
652 (1 << OMAP3XXX_EN_DPLL_FRBYPASS);
653 } else if (soc_is_am33xx() || cpu_is_omap44xx() || soc_is_am43xx() ||
654 soc_is_omap54xx() || soc_is_dra7xx()) {
Tero Kristof3b19aa2015-02-27 17:54:14 +0200655 features.dpll_bypass_vals |=
Tero Kristo512d91c2014-07-02 11:47:42 +0300656 (1 << OMAP4XXX_EN_DPLL_LPBYPASS) |
657 (1 << OMAP4XXX_EN_DPLL_FRBYPASS) |
658 (1 << OMAP4XXX_EN_DPLL_MNBYPASS);
659 }
Tero Kristo2337c5b2014-07-02 11:47:43 +0300660
661 /* Jitter correction only available on OMAP343X */
662 if (cpu_is_omap343x())
Tero Kristof3b19aa2015-02-27 17:54:14 +0200663 features.flags |= TI_CLK_DPLL_HAS_FREQSEL;
Tero Kristo066edb22014-07-02 11:47:44 +0300664
665 /* Idlest value for interface clocks.
666 * 24xx uses 0 to indicate not ready, and 1 to indicate ready.
667 * 34xx reverses this, just to keep us on our toes
668 * AM35xx uses both, depending on the module.
669 */
670 if (cpu_is_omap24xx())
Tero Kristof3b19aa2015-02-27 17:54:14 +0200671 features.cm_idlest_val = OMAP24XX_CM_IDLEST_VAL;
Tero Kristo066edb22014-07-02 11:47:44 +0300672 else if (cpu_is_omap34xx())
Tero Kristof3b19aa2015-02-27 17:54:14 +0200673 features.cm_idlest_val = OMAP34XX_CM_IDLEST_VAL;
Tero Kristof0d2f682014-10-03 16:57:10 +0300674
675 /* On OMAP3430 ES1.0, DPLL4 can't be re-programmed */
676 if (omap_rev() == OMAP3430_REV_ES1_0)
Tero Kristof3b19aa2015-02-27 17:54:14 +0200677 features.flags |= TI_CLK_DPLL4_DENY_REPROGRAM;
678
679 ti_clk_setup_features(&features);
Tero Kristo8111e012014-07-02 11:47:39 +0300680}