blob: 840ab262272a67b82b7354a7a476dc601c6cd96f [file] [log] [blame]
Colin Crossd8611962010-01-28 16:40:29 -08001/*
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05302 * arch/arm/mach-tegra/tegra20_clocks.c
Colin Crossd8611962010-01-28 16:40:29 -08003 *
4 * Copyright (C) 2010 Google, Inc.
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05305 * Copyright (c) 2010-2012 NVIDIA CORPORATION. All rights reserved.
Colin Crossd8611962010-01-28 16:40:29 -08006 *
7 * Author:
8 * Colin Cross <ccross@google.com>
9 *
10 * This software is licensed under the terms of the GNU General Public
11 * License version 2, as published by the Free Software Foundation, and
12 * may be copied, distributed, and modified under those terms.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 */
20
21#include <linux/kernel.h>
22#include <linux/module.h>
23#include <linux/list.h>
24#include <linux/spinlock.h>
25#include <linux/delay.h>
26#include <linux/io.h>
Jean-Christop PLAGNIOL-VILLARD6d803ba2010-11-17 10:04:33 +010027#include <linux/clkdev.h>
Colin Cross4729fd72011-02-12 16:43:05 -080028#include <linux/clk.h>
Colin Crossd8611962010-01-28 16:40:29 -080029
30#include <mach/iomap.h>
Colin Cross2ea67fd2010-10-04 08:49:49 -070031#include <mach/suspend.h>
Colin Crossd8611962010-01-28 16:40:29 -080032
33#include "clock.h"
Colin Cross71fc84c2010-06-07 20:49:46 -070034#include "fuse.h"
Colin Cross6d296822010-11-22 18:37:54 -080035#include "tegra2_emc.h"
Colin Crossd8611962010-01-28 16:40:29 -080036
37#define RST_DEVICES 0x004
38#define RST_DEVICES_SET 0x300
39#define RST_DEVICES_CLR 0x304
Colin Cross71fc84c2010-06-07 20:49:46 -070040#define RST_DEVICES_NUM 3
Colin Crossd8611962010-01-28 16:40:29 -080041
42#define CLK_OUT_ENB 0x010
43#define CLK_OUT_ENB_SET 0x320
44#define CLK_OUT_ENB_CLR 0x324
Colin Cross71fc84c2010-06-07 20:49:46 -070045#define CLK_OUT_ENB_NUM 3
46
47#define CLK_MASK_ARM 0x44
48#define MISC_CLK_ENB 0x48
Colin Crossd8611962010-01-28 16:40:29 -080049
50#define OSC_CTRL 0x50
51#define OSC_CTRL_OSC_FREQ_MASK (3<<30)
52#define OSC_CTRL_OSC_FREQ_13MHZ (0<<30)
53#define OSC_CTRL_OSC_FREQ_19_2MHZ (1<<30)
54#define OSC_CTRL_OSC_FREQ_12MHZ (2<<30)
55#define OSC_CTRL_OSC_FREQ_26MHZ (3<<30)
Colin Crosscea62c82010-10-04 11:49:26 -070056#define OSC_CTRL_MASK (0x3f2 | OSC_CTRL_OSC_FREQ_MASK)
Colin Crossd8611962010-01-28 16:40:29 -080057
58#define OSC_FREQ_DET 0x58
59#define OSC_FREQ_DET_TRIG (1<<31)
60
61#define OSC_FREQ_DET_STATUS 0x5C
62#define OSC_FREQ_DET_BUSY (1<<31)
63#define OSC_FREQ_DET_CNT_MASK 0xFFFF
64
Colin Cross71fc84c2010-06-07 20:49:46 -070065#define PERIPH_CLK_SOURCE_I2S1 0x100
66#define PERIPH_CLK_SOURCE_EMC 0x19c
67#define PERIPH_CLK_SOURCE_OSC 0x1fc
68#define PERIPH_CLK_SOURCE_NUM \
69 ((PERIPH_CLK_SOURCE_OSC - PERIPH_CLK_SOURCE_I2S1) / 4)
70
Colin Crossd8611962010-01-28 16:40:29 -080071#define PERIPH_CLK_SOURCE_MASK (3<<30)
72#define PERIPH_CLK_SOURCE_SHIFT 30
Simon Quebb1dccf2011-12-16 20:11:22 +010073#define PERIPH_CLK_SOURCE_PWM_MASK (7<<28)
74#define PERIPH_CLK_SOURCE_PWM_SHIFT 28
Colin Crossd8611962010-01-28 16:40:29 -080075#define PERIPH_CLK_SOURCE_ENABLE (1<<28)
Colin Cross71fc84c2010-06-07 20:49:46 -070076#define PERIPH_CLK_SOURCE_DIVU71_MASK 0xFF
77#define PERIPH_CLK_SOURCE_DIVU16_MASK 0xFFFF
Colin Crossd8611962010-01-28 16:40:29 -080078#define PERIPH_CLK_SOURCE_DIV_SHIFT 0
79
Colin Cross9743b382011-02-12 18:24:32 -080080#define SDMMC_CLK_INT_FB_SEL (1 << 23)
81#define SDMMC_CLK_INT_FB_DLY_SHIFT 16
82#define SDMMC_CLK_INT_FB_DLY_MASK (0xF << SDMMC_CLK_INT_FB_DLY_SHIFT)
83
Colin Crossd8611962010-01-28 16:40:29 -080084#define PLL_BASE 0x0
85#define PLL_BASE_BYPASS (1<<31)
86#define PLL_BASE_ENABLE (1<<30)
87#define PLL_BASE_REF_ENABLE (1<<29)
88#define PLL_BASE_OVERRIDE (1<<28)
Colin Crossd8611962010-01-28 16:40:29 -080089#define PLL_BASE_DIVP_MASK (0x7<<20)
90#define PLL_BASE_DIVP_SHIFT 20
91#define PLL_BASE_DIVN_MASK (0x3FF<<8)
92#define PLL_BASE_DIVN_SHIFT 8
93#define PLL_BASE_DIVM_MASK (0x1F)
94#define PLL_BASE_DIVM_SHIFT 0
95
96#define PLL_OUT_RATIO_MASK (0xFF<<8)
97#define PLL_OUT_RATIO_SHIFT 8
98#define PLL_OUT_OVERRIDE (1<<2)
99#define PLL_OUT_CLKEN (1<<1)
100#define PLL_OUT_RESET_DISABLE (1<<0)
101
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530102#define PLL_MISC(c) (((c)->flags & PLL_ALT_MISC_REG) ? 0x4 : 0xc)
Colin Cross71fc84c2010-06-07 20:49:46 -0700103
Colin Crossd8611962010-01-28 16:40:29 -0800104#define PLL_MISC_DCCON_SHIFT 20
Colin Crossd8611962010-01-28 16:40:29 -0800105#define PLL_MISC_CPCON_SHIFT 8
106#define PLL_MISC_CPCON_MASK (0xF<<PLL_MISC_CPCON_SHIFT)
107#define PLL_MISC_LFCON_SHIFT 4
108#define PLL_MISC_LFCON_MASK (0xF<<PLL_MISC_LFCON_SHIFT)
109#define PLL_MISC_VCOCON_SHIFT 0
110#define PLL_MISC_VCOCON_MASK (0xF<<PLL_MISC_VCOCON_SHIFT)
111
Colin Cross71fc84c2010-06-07 20:49:46 -0700112#define PLLU_BASE_POST_DIV (1<<20)
113
Colin Crossd8611962010-01-28 16:40:29 -0800114#define PLLD_MISC_CLKENABLE (1<<30)
115#define PLLD_MISC_DIV_RST (1<<23)
116#define PLLD_MISC_DCCON_SHIFT 12
117
Mike Rapoport8d685bc2010-09-27 11:26:32 +0200118#define PLLE_MISC_READY (1 << 15)
119
Colin Crossf1519612011-02-12 16:05:31 -0800120#define PERIPH_CLK_TO_ENB_REG(c) ((c->u.periph.clk_num / 32) * 4)
121#define PERIPH_CLK_TO_ENB_SET_REG(c) ((c->u.periph.clk_num / 32) * 8)
122#define PERIPH_CLK_TO_ENB_BIT(c) (1 << (c->u.periph.clk_num % 32))
Colin Crossd8611962010-01-28 16:40:29 -0800123
124#define SUPER_CLK_MUX 0x00
125#define SUPER_STATE_SHIFT 28
126#define SUPER_STATE_MASK (0xF << SUPER_STATE_SHIFT)
127#define SUPER_STATE_STANDBY (0x0 << SUPER_STATE_SHIFT)
128#define SUPER_STATE_IDLE (0x1 << SUPER_STATE_SHIFT)
129#define SUPER_STATE_RUN (0x2 << SUPER_STATE_SHIFT)
130#define SUPER_STATE_IRQ (0x3 << SUPER_STATE_SHIFT)
131#define SUPER_STATE_FIQ (0x4 << SUPER_STATE_SHIFT)
132#define SUPER_SOURCE_MASK 0xF
133#define SUPER_FIQ_SOURCE_SHIFT 12
134#define SUPER_IRQ_SOURCE_SHIFT 8
135#define SUPER_RUN_SOURCE_SHIFT 4
136#define SUPER_IDLE_SOURCE_SHIFT 0
137
138#define SUPER_CLK_DIVIDER 0x04
139
140#define BUS_CLK_DISABLE (1<<3)
141#define BUS_CLK_DIV_MASK 0x3
142
Colin Crosscea62c82010-10-04 11:49:26 -0700143#define PMC_CTRL 0x0
144 #define PMC_CTRL_BLINK_ENB (1 << 7)
145
146#define PMC_DPD_PADS_ORIDE 0x1c
147 #define PMC_DPD_PADS_ORIDE_BLINK_ENB (1 << 20)
148
149#define PMC_BLINK_TIMER_DATA_ON_SHIFT 0
150#define PMC_BLINK_TIMER_DATA_ON_MASK 0x7fff
151#define PMC_BLINK_TIMER_ENB (1 << 15)
152#define PMC_BLINK_TIMER_DATA_OFF_SHIFT 16
153#define PMC_BLINK_TIMER_DATA_OFF_MASK 0xffff
154
Colin Crossd8611962010-01-28 16:40:29 -0800155static void __iomem *reg_clk_base = IO_ADDRESS(TEGRA_CLK_RESET_BASE);
Colin Crosscea62c82010-10-04 11:49:26 -0700156static void __iomem *reg_pmc_base = IO_ADDRESS(TEGRA_PMC_BASE);
Colin Crossd8611962010-01-28 16:40:29 -0800157
Colin Cross4729fd72011-02-12 16:43:05 -0800158/*
159 * Some clocks share a register with other clocks. Any clock op that
160 * non-atomically modifies a register used by another clock must lock
161 * clock_register_lock first.
162 */
163static DEFINE_SPINLOCK(clock_register_lock);
164
Colin Cross78f379b2010-10-20 19:19:58 -0700165/*
166 * Some peripheral clocks share an enable bit, so refcount the enable bits
167 * in registers CLK_ENABLE_L, CLK_ENABLE_H, and CLK_ENABLE_U
168 */
169static int tegra_periph_clk_enable_refcount[3 * 32];
170
Colin Crossd8611962010-01-28 16:40:29 -0800171#define clk_writel(value, reg) \
Olof Johanssond3959352011-09-08 17:56:59 -0700172 __raw_writel(value, reg_clk_base + (reg))
Colin Crossd8611962010-01-28 16:40:29 -0800173#define clk_readl(reg) \
Olof Johanssond3959352011-09-08 17:56:59 -0700174 __raw_readl(reg_clk_base + (reg))
Colin Crosscea62c82010-10-04 11:49:26 -0700175#define pmc_writel(value, reg) \
Olof Johanssond3959352011-09-08 17:56:59 -0700176 __raw_writel(value, reg_pmc_base + (reg))
Colin Crosscea62c82010-10-04 11:49:26 -0700177#define pmc_readl(reg) \
Olof Johanssond3959352011-09-08 17:56:59 -0700178 __raw_readl(reg_pmc_base + (reg))
Colin Crossd8611962010-01-28 16:40:29 -0800179
Peter De Schrijver8e4fab22011-12-14 17:03:16 +0200180static unsigned long clk_measure_input_freq(void)
Colin Crossd8611962010-01-28 16:40:29 -0800181{
182 u32 clock_autodetect;
183 clk_writel(OSC_FREQ_DET_TRIG | 1, OSC_FREQ_DET);
184 do {} while (clk_readl(OSC_FREQ_DET_STATUS) & OSC_FREQ_DET_BUSY);
185 clock_autodetect = clk_readl(OSC_FREQ_DET_STATUS);
186 if (clock_autodetect >= 732 - 3 && clock_autodetect <= 732 + 3) {
187 return 12000000;
188 } else if (clock_autodetect >= 794 - 3 && clock_autodetect <= 794 + 3) {
189 return 13000000;
190 } else if (clock_autodetect >= 1172 - 3 && clock_autodetect <= 1172 + 3) {
191 return 19200000;
192 } else if (clock_autodetect >= 1587 - 3 && clock_autodetect <= 1587 + 3) {
193 return 26000000;
194 } else {
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530195 pr_err("%s: Unexpected clock autodetect value %d",
196 __func__, clock_autodetect);
Colin Crossd8611962010-01-28 16:40:29 -0800197 BUG();
198 return 0;
199 }
200}
201
Colin Cross71fc84c2010-06-07 20:49:46 -0700202static int clk_div71_get_divider(unsigned long parent_rate, unsigned long rate)
Colin Crossd8611962010-01-28 16:40:29 -0800203{
Colin Cross71fc84c2010-06-07 20:49:46 -0700204 s64 divider_u71 = parent_rate * 2;
205 divider_u71 += rate - 1;
206 do_div(divider_u71, rate);
Colin Crossd8611962010-01-28 16:40:29 -0800207
Colin Cross71fc84c2010-06-07 20:49:46 -0700208 if (divider_u71 - 2 < 0)
209 return 0;
Colin Crossd8611962010-01-28 16:40:29 -0800210
Colin Cross71fc84c2010-06-07 20:49:46 -0700211 if (divider_u71 - 2 > 255)
Colin Crossd8611962010-01-28 16:40:29 -0800212 return -EINVAL;
213
214 return divider_u71 - 2;
215}
216
Colin Cross71fc84c2010-06-07 20:49:46 -0700217static int clk_div16_get_divider(unsigned long parent_rate, unsigned long rate)
Colin Crossd8611962010-01-28 16:40:29 -0800218{
Colin Cross71fc84c2010-06-07 20:49:46 -0700219 s64 divider_u16;
Colin Crossd8611962010-01-28 16:40:29 -0800220
Colin Cross71fc84c2010-06-07 20:49:46 -0700221 divider_u16 = parent_rate;
222 divider_u16 += rate - 1;
223 do_div(divider_u16, rate);
224
225 if (divider_u16 - 1 < 0)
226 return 0;
227
Stephen Warreneb70e1b2012-07-24 15:48:12 -0600228 if (divider_u16 - 1 > 0xFFFF)
Colin Cross71fc84c2010-06-07 20:49:46 -0700229 return -EINVAL;
230
231 return divider_u16 - 1;
Colin Crossd8611962010-01-28 16:40:29 -0800232}
233
Joseph Lob78c0302012-08-17 14:51:21 +0800234static unsigned long tegra_clk_fixed_recalc_rate(struct clk_hw *hw,
235 unsigned long parent_rate)
236{
237 return to_clk_tegra(hw)->fixed_rate;
238}
239
240struct clk_ops tegra_clk_32k_ops = {
241 .recalc_rate = tegra_clk_fixed_recalc_rate,
242};
243
Colin Crossd8611962010-01-28 16:40:29 -0800244/* clk_m functions */
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530245static unsigned long tegra20_clk_m_recalc_rate(struct clk_hw *hw,
246 unsigned long prate)
Colin Crossd8611962010-01-28 16:40:29 -0800247{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530248 if (!to_clk_tegra(hw)->fixed_rate)
249 to_clk_tegra(hw)->fixed_rate = clk_measure_input_freq();
250 return to_clk_tegra(hw)->fixed_rate;
251}
Colin Crossd8611962010-01-28 16:40:29 -0800252
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530253static void tegra20_clk_m_init(struct clk_hw *hw)
254{
255 struct clk_tegra *c = to_clk_tegra(hw);
256 u32 osc_ctrl = clk_readl(OSC_CTRL);
257 u32 auto_clock_control = osc_ctrl & ~OSC_CTRL_OSC_FREQ_MASK;
258
259 switch (c->fixed_rate) {
Colin Crossd8611962010-01-28 16:40:29 -0800260 case 12000000:
261 auto_clock_control |= OSC_CTRL_OSC_FREQ_12MHZ;
262 break;
263 case 13000000:
264 auto_clock_control |= OSC_CTRL_OSC_FREQ_13MHZ;
265 break;
266 case 19200000:
267 auto_clock_control |= OSC_CTRL_OSC_FREQ_19_2MHZ;
268 break;
269 case 26000000:
270 auto_clock_control |= OSC_CTRL_OSC_FREQ_26MHZ;
271 break;
272 default:
Colin Crossd8611962010-01-28 16:40:29 -0800273 BUG();
274 }
275 clk_writel(auto_clock_control, OSC_CTRL);
Colin Crossd8611962010-01-28 16:40:29 -0800276}
277
Prashant Gaikwad86edb872012-08-06 11:57:40 +0530278struct clk_ops tegra_clk_m_ops = {
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530279 .init = tegra20_clk_m_init,
280 .recalc_rate = tegra20_clk_m_recalc_rate,
Colin Crossd8611962010-01-28 16:40:29 -0800281};
282
283/* super clock functions */
284/* "super clocks" on tegra have two-stage muxes and a clock skipping
285 * super divider. We will ignore the clock skipping divider, since we
286 * can't lower the voltage when using the clock skip, but we can if we
287 * lower the PLL frequency.
288 */
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530289static int tegra20_super_clk_is_enabled(struct clk_hw *hw)
Colin Crossd8611962010-01-28 16:40:29 -0800290{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530291 struct clk_tegra *c = to_clk_tegra(hw);
Colin Crossd8611962010-01-28 16:40:29 -0800292 u32 val;
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530293
294 val = clk_readl(c->reg + SUPER_CLK_MUX);
295 BUG_ON(((val & SUPER_STATE_MASK) != SUPER_STATE_RUN) &&
296 ((val & SUPER_STATE_MASK) != SUPER_STATE_IDLE));
297 c->state = ON;
298 return c->state;
299}
300
301static int tegra20_super_clk_enable(struct clk_hw *hw)
302{
303 struct clk_tegra *c = to_clk_tegra(hw);
304 clk_writel(0, c->reg + SUPER_CLK_DIVIDER);
305 return 0;
306}
307
308static void tegra20_super_clk_disable(struct clk_hw *hw)
309{
310 pr_debug("%s on clock %s\n", __func__, __clk_get_name(hw->clk));
311
312 /* oops - don't disable the CPU clock! */
313 BUG();
314}
315
316static u8 tegra20_super_clk_get_parent(struct clk_hw *hw)
317{
318 struct clk_tegra *c = to_clk_tegra(hw);
319 int val = clk_readl(c->reg + SUPER_CLK_MUX);
Colin Crossd8611962010-01-28 16:40:29 -0800320 int source;
321 int shift;
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530322
Colin Crossd8611962010-01-28 16:40:29 -0800323 BUG_ON(((val & SUPER_STATE_MASK) != SUPER_STATE_RUN) &&
324 ((val & SUPER_STATE_MASK) != SUPER_STATE_IDLE));
325 shift = ((val & SUPER_STATE_MASK) == SUPER_STATE_IDLE) ?
326 SUPER_IDLE_SOURCE_SHIFT : SUPER_RUN_SOURCE_SHIFT;
327 source = (val >> shift) & SUPER_SOURCE_MASK;
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530328 return source;
Colin Crossd8611962010-01-28 16:40:29 -0800329}
330
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530331static int tegra20_super_clk_set_parent(struct clk_hw *hw, u8 index)
Colin Crossd8611962010-01-28 16:40:29 -0800332{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530333 struct clk_tegra *c = to_clk_tegra(hw);
334 u32 val = clk_readl(c->reg + SUPER_CLK_MUX);
Colin Crossd8611962010-01-28 16:40:29 -0800335 int shift;
Colin Cross71fc84c2010-06-07 20:49:46 -0700336
Colin Crossd8611962010-01-28 16:40:29 -0800337 BUG_ON(((val & SUPER_STATE_MASK) != SUPER_STATE_RUN) &&
338 ((val & SUPER_STATE_MASK) != SUPER_STATE_IDLE));
339 shift = ((val & SUPER_STATE_MASK) == SUPER_STATE_IDLE) ?
340 SUPER_IDLE_SOURCE_SHIFT : SUPER_RUN_SOURCE_SHIFT;
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530341 val &= ~(SUPER_SOURCE_MASK << shift);
342 val |= index << shift;
Colin Cross71fc84c2010-06-07 20:49:46 -0700343
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530344 clk_writel(val, c->reg);
Colin Cross71fc84c2010-06-07 20:49:46 -0700345
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530346 return 0;
Colin Crossd8611962010-01-28 16:40:29 -0800347}
348
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530349/* FIX ME: Need to switch parents to change the source PLL rate */
350static unsigned long tegra20_super_clk_recalc_rate(struct clk_hw *hw,
351 unsigned long prate)
Colin Cross9c7dc562011-02-12 21:25:23 -0800352{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530353 return prate;
Colin Cross9c7dc562011-02-12 21:25:23 -0800354}
355
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530356static long tegra20_super_clk_round_rate(struct clk_hw *hw, unsigned long rate,
357 unsigned long *prate)
Colin Cross71fc84c2010-06-07 20:49:46 -0700358{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530359 return *prate;
Colin Cross71fc84c2010-06-07 20:49:46 -0700360}
361
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530362static int tegra20_super_clk_set_rate(struct clk_hw *hw, unsigned long rate,
363 unsigned long parent_rate)
Colin Cross71fc84c2010-06-07 20:49:46 -0700364{
365 return 0;
366}
367
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530368struct clk_ops tegra_super_ops = {
369 .is_enabled = tegra20_super_clk_is_enabled,
370 .enable = tegra20_super_clk_enable,
371 .disable = tegra20_super_clk_disable,
372 .set_parent = tegra20_super_clk_set_parent,
373 .get_parent = tegra20_super_clk_get_parent,
374 .set_rate = tegra20_super_clk_set_rate,
375 .round_rate = tegra20_super_clk_round_rate,
376 .recalc_rate = tegra20_super_clk_recalc_rate,
377};
Colin Cross71fc84c2010-06-07 20:49:46 -0700378
Prashant Gaikwadb4350f42012-09-13 15:04:33 +0530379static unsigned long tegra20_twd_clk_recalc_rate(struct clk_hw *hw,
380 unsigned long parent_rate)
381{
382 struct clk_tegra *c = to_clk_tegra(hw);
383 u64 rate = parent_rate;
384
385 if (c->mul != 0 && c->div != 0) {
386 rate *= c->mul;
387 rate += c->div - 1; /* round up */
388 do_div(rate, c->div);
389 }
390
391 return rate;
392}
393
394struct clk_ops tegra_twd_ops = {
395 .recalc_rate = tegra20_twd_clk_recalc_rate,
396};
397
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530398static u8 tegra20_cop_clk_get_parent(struct clk_hw *hw)
399{
400 return 0;
Colin Cross71fc84c2010-06-07 20:49:46 -0700401}
402
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530403struct clk_ops tegra_cop_ops = {
404 .get_parent = tegra20_cop_clk_get_parent,
Colin Crossd8611962010-01-28 16:40:29 -0800405};
406
Colin Cross9c7dc562011-02-12 21:25:23 -0800407/* virtual cop clock functions. Used to acquire the fake 'cop' clock to
408 * reset the COP block (i.e. AVP) */
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530409void tegra2_cop_clk_reset(struct clk_hw *hw, bool assert)
Colin Cross9c7dc562011-02-12 21:25:23 -0800410{
411 unsigned long reg = assert ? RST_DEVICES_SET : RST_DEVICES_CLR;
412
413 pr_debug("%s %s\n", __func__, assert ? "assert" : "deassert");
414 clk_writel(1 << 1, reg);
415}
416
Colin Crossd8611962010-01-28 16:40:29 -0800417/* bus clock functions */
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530418static int tegra20_bus_clk_is_enabled(struct clk_hw *hw)
Colin Crossd8611962010-01-28 16:40:29 -0800419{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530420 struct clk_tegra *c = to_clk_tegra(hw);
Colin Crossd8611962010-01-28 16:40:29 -0800421 u32 val = clk_readl(c->reg);
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530422
Colin Crossd8611962010-01-28 16:40:29 -0800423 c->state = ((val >> c->reg_shift) & BUS_CLK_DISABLE) ? OFF : ON;
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530424 return c->state;
Colin Crossd8611962010-01-28 16:40:29 -0800425}
426
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530427static int tegra20_bus_clk_enable(struct clk_hw *hw)
Colin Crossd8611962010-01-28 16:40:29 -0800428{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530429 struct clk_tegra *c = to_clk_tegra(hw);
Colin Cross4729fd72011-02-12 16:43:05 -0800430 unsigned long flags;
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530431 u32 val;
Colin Cross4729fd72011-02-12 16:43:05 -0800432
433 spin_lock_irqsave(&clock_register_lock, flags);
434
435 val = clk_readl(c->reg);
Colin Crossd8611962010-01-28 16:40:29 -0800436 val &= ~(BUS_CLK_DISABLE << c->reg_shift);
437 clk_writel(val, c->reg);
Colin Cross4729fd72011-02-12 16:43:05 -0800438
439 spin_unlock_irqrestore(&clock_register_lock, flags);
440
Colin Crossd8611962010-01-28 16:40:29 -0800441 return 0;
442}
443
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530444static void tegra20_bus_clk_disable(struct clk_hw *hw)
Colin Crossd8611962010-01-28 16:40:29 -0800445{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530446 struct clk_tegra *c = to_clk_tegra(hw);
Colin Cross4729fd72011-02-12 16:43:05 -0800447 unsigned long flags;
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530448 u32 val;
Colin Cross4729fd72011-02-12 16:43:05 -0800449
450 spin_lock_irqsave(&clock_register_lock, flags);
451
452 val = clk_readl(c->reg);
Colin Crossd8611962010-01-28 16:40:29 -0800453 val |= BUS_CLK_DISABLE << c->reg_shift;
454 clk_writel(val, c->reg);
Colin Cross4729fd72011-02-12 16:43:05 -0800455
456 spin_unlock_irqrestore(&clock_register_lock, flags);
Colin Crossd8611962010-01-28 16:40:29 -0800457}
458
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530459static unsigned long tegra20_bus_clk_recalc_rate(struct clk_hw *hw,
460 unsigned long prate)
Colin Crossd8611962010-01-28 16:40:29 -0800461{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530462 struct clk_tegra *c = to_clk_tegra(hw);
463 u32 val = clk_readl(c->reg);
464 u64 rate = prate;
465
466 c->div = ((val >> c->reg_shift) & BUS_CLK_DIV_MASK) + 1;
467 c->mul = 1;
468
469 if (c->mul != 0 && c->div != 0) {
470 rate *= c->mul;
471 rate += c->div - 1; /* round up */
472 do_div(rate, c->div);
473 }
474 return rate;
475}
476
477static int tegra20_bus_clk_set_rate(struct clk_hw *hw, unsigned long rate,
478 unsigned long parent_rate)
479{
480 struct clk_tegra *c = to_clk_tegra(hw);
Colin Cross4729fd72011-02-12 16:43:05 -0800481 int ret = -EINVAL;
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530482 unsigned long flags;
483 u32 val;
Colin Crossd8611962010-01-28 16:40:29 -0800484 int i;
Colin Cross4729fd72011-02-12 16:43:05 -0800485
486 spin_lock_irqsave(&clock_register_lock, flags);
487
488 val = clk_readl(c->reg);
Colin Crossd8611962010-01-28 16:40:29 -0800489 for (i = 1; i <= 4; i++) {
490 if (rate == parent_rate / i) {
491 val &= ~(BUS_CLK_DIV_MASK << c->reg_shift);
492 val |= (i - 1) << c->reg_shift;
493 clk_writel(val, c->reg);
494 c->div = i;
495 c->mul = 1;
Colin Cross4729fd72011-02-12 16:43:05 -0800496 ret = 0;
497 break;
Colin Crossd8611962010-01-28 16:40:29 -0800498 }
499 }
Colin Cross4729fd72011-02-12 16:43:05 -0800500
501 spin_unlock_irqrestore(&clock_register_lock, flags);
502
503 return ret;
Colin Crossd8611962010-01-28 16:40:29 -0800504}
505
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530506static long tegra20_bus_clk_round_rate(struct clk_hw *hw, unsigned long rate,
507 unsigned long *prate)
508{
509 unsigned long parent_rate = *prate;
510 s64 divider;
511
512 if (rate >= parent_rate)
513 return rate;
514
515 divider = parent_rate;
516 divider += rate - 1;
517 do_div(divider, rate);
518
519 if (divider < 0)
520 return divider;
521
522 if (divider > 4)
523 divider = 4;
524 do_div(parent_rate, divider);
525
526 return parent_rate;
527}
528
Prashant Gaikwad86edb872012-08-06 11:57:40 +0530529struct clk_ops tegra_bus_ops = {
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530530 .is_enabled = tegra20_bus_clk_is_enabled,
531 .enable = tegra20_bus_clk_enable,
532 .disable = tegra20_bus_clk_disable,
533 .set_rate = tegra20_bus_clk_set_rate,
534 .round_rate = tegra20_bus_clk_round_rate,
535 .recalc_rate = tegra20_bus_clk_recalc_rate,
Colin Crossd8611962010-01-28 16:40:29 -0800536};
537
Colin Crosscea62c82010-10-04 11:49:26 -0700538/* Blink output functions */
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530539static int tegra20_blink_clk_is_enabled(struct clk_hw *hw)
Colin Crosscea62c82010-10-04 11:49:26 -0700540{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530541 struct clk_tegra *c = to_clk_tegra(hw);
Colin Crosscea62c82010-10-04 11:49:26 -0700542 u32 val;
543
544 val = pmc_readl(PMC_CTRL);
545 c->state = (val & PMC_CTRL_BLINK_ENB) ? ON : OFF;
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530546 return c->state;
547}
548
549static unsigned long tegra20_blink_clk_recalc_rate(struct clk_hw *hw,
550 unsigned long prate)
551{
552 struct clk_tegra *c = to_clk_tegra(hw);
553 u64 rate = prate;
554 u32 val;
555
Colin Crosscea62c82010-10-04 11:49:26 -0700556 c->mul = 1;
557 val = pmc_readl(c->reg);
558
559 if (val & PMC_BLINK_TIMER_ENB) {
560 unsigned int on_off;
561
562 on_off = (val >> PMC_BLINK_TIMER_DATA_ON_SHIFT) &
563 PMC_BLINK_TIMER_DATA_ON_MASK;
564 val >>= PMC_BLINK_TIMER_DATA_OFF_SHIFT;
565 val &= PMC_BLINK_TIMER_DATA_OFF_MASK;
566 on_off += val;
567 /* each tick in the blink timer is 4 32KHz clocks */
568 c->div = on_off * 4;
569 } else {
570 c->div = 1;
571 }
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530572
573 if (c->mul != 0 && c->div != 0) {
574 rate *= c->mul;
575 rate += c->div - 1; /* round up */
576 do_div(rate, c->div);
577 }
578 return rate;
Colin Crosscea62c82010-10-04 11:49:26 -0700579}
580
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530581static int tegra20_blink_clk_enable(struct clk_hw *hw)
Colin Crosscea62c82010-10-04 11:49:26 -0700582{
583 u32 val;
584
585 val = pmc_readl(PMC_DPD_PADS_ORIDE);
586 pmc_writel(val | PMC_DPD_PADS_ORIDE_BLINK_ENB, PMC_DPD_PADS_ORIDE);
587
588 val = pmc_readl(PMC_CTRL);
589 pmc_writel(val | PMC_CTRL_BLINK_ENB, PMC_CTRL);
590
591 return 0;
592}
593
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530594static void tegra20_blink_clk_disable(struct clk_hw *hw)
Colin Crosscea62c82010-10-04 11:49:26 -0700595{
596 u32 val;
597
598 val = pmc_readl(PMC_CTRL);
599 pmc_writel(val & ~PMC_CTRL_BLINK_ENB, PMC_CTRL);
600
601 val = pmc_readl(PMC_DPD_PADS_ORIDE);
602 pmc_writel(val & ~PMC_DPD_PADS_ORIDE_BLINK_ENB, PMC_DPD_PADS_ORIDE);
603}
604
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530605static int tegra20_blink_clk_set_rate(struct clk_hw *hw, unsigned long rate,
606 unsigned long parent_rate)
Colin Crosscea62c82010-10-04 11:49:26 -0700607{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530608 struct clk_tegra *c = to_clk_tegra(hw);
609
Colin Cross4729fd72011-02-12 16:43:05 -0800610 if (rate >= parent_rate) {
Colin Crosscea62c82010-10-04 11:49:26 -0700611 c->div = 1;
612 pmc_writel(0, c->reg);
613 } else {
614 unsigned int on_off;
615 u32 val;
616
Colin Cross4729fd72011-02-12 16:43:05 -0800617 on_off = DIV_ROUND_UP(parent_rate / 8, rate);
Colin Crosscea62c82010-10-04 11:49:26 -0700618 c->div = on_off * 8;
619
620 val = (on_off & PMC_BLINK_TIMER_DATA_ON_MASK) <<
621 PMC_BLINK_TIMER_DATA_ON_SHIFT;
622 on_off &= PMC_BLINK_TIMER_DATA_OFF_MASK;
623 on_off <<= PMC_BLINK_TIMER_DATA_OFF_SHIFT;
624 val |= on_off;
625 val |= PMC_BLINK_TIMER_ENB;
626 pmc_writel(val, c->reg);
627 }
628
629 return 0;
630}
631
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530632static long tegra20_blink_clk_round_rate(struct clk_hw *hw, unsigned long rate,
633 unsigned long *prate)
634{
635 int div;
636 int mul;
637 long round_rate = *prate;
638
639 mul = 1;
640
641 if (rate >= *prate) {
642 div = 1;
643 } else {
644 div = DIV_ROUND_UP(*prate / 8, rate);
645 div *= 8;
646 }
647
648 round_rate *= mul;
649 round_rate += div - 1;
650 do_div(round_rate, div);
651
652 return round_rate;
653}
654
Prashant Gaikwad86edb872012-08-06 11:57:40 +0530655struct clk_ops tegra_blink_clk_ops = {
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530656 .is_enabled = tegra20_blink_clk_is_enabled,
657 .enable = tegra20_blink_clk_enable,
658 .disable = tegra20_blink_clk_disable,
659 .set_rate = tegra20_blink_clk_set_rate,
660 .round_rate = tegra20_blink_clk_round_rate,
661 .recalc_rate = tegra20_blink_clk_recalc_rate,
Colin Crosscea62c82010-10-04 11:49:26 -0700662};
663
Colin Crossd8611962010-01-28 16:40:29 -0800664/* PLL Functions */
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530665static int tegra20_pll_clk_wait_for_lock(struct clk_tegra *c)
Colin Crossd8611962010-01-28 16:40:29 -0800666{
Colin Crossf1519612011-02-12 16:05:31 -0800667 udelay(c->u.pll.lock_delay);
Colin Crossd8611962010-01-28 16:40:29 -0800668 return 0;
669}
670
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530671static int tegra20_pll_clk_is_enabled(struct clk_hw *hw)
Colin Crossd8611962010-01-28 16:40:29 -0800672{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530673 struct clk_tegra *c = to_clk_tegra(hw);
Colin Crossd8611962010-01-28 16:40:29 -0800674 u32 val = clk_readl(c->reg + PLL_BASE);
675
676 c->state = (val & PLL_BASE_ENABLE) ? ON : OFF;
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530677 return c->state;
678}
679
680static unsigned long tegra20_pll_clk_recalc_rate(struct clk_hw *hw,
681 unsigned long prate)
682{
683 struct clk_tegra *c = to_clk_tegra(hw);
684 u32 val = clk_readl(c->reg + PLL_BASE);
685 u64 rate = prate;
Colin Crossd8611962010-01-28 16:40:29 -0800686
687 if (c->flags & PLL_FIXED && !(val & PLL_BASE_OVERRIDE)) {
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530688 const struct clk_pll_freq_table *sel;
689 for (sel = c->u.pll.freq_table; sel->input_rate != 0; sel++) {
690 if (sel->input_rate == prate &&
691 sel->output_rate == c->u.pll.fixed_rate) {
692 c->mul = sel->n;
693 c->div = sel->m * sel->p;
694 break;
695 }
696 }
697 pr_err("Clock %s has unknown fixed frequency\n",
698 __clk_get_name(hw->clk));
699 BUG();
Colin Crossd8611962010-01-28 16:40:29 -0800700 } else if (val & PLL_BASE_BYPASS) {
Colin Cross71fc84c2010-06-07 20:49:46 -0700701 c->mul = 1;
702 c->div = 1;
Colin Crossd8611962010-01-28 16:40:29 -0800703 } else {
Colin Cross71fc84c2010-06-07 20:49:46 -0700704 c->mul = (val & PLL_BASE_DIVN_MASK) >> PLL_BASE_DIVN_SHIFT;
705 c->div = (val & PLL_BASE_DIVM_MASK) >> PLL_BASE_DIVM_SHIFT;
706 if (c->flags & PLLU)
707 c->div *= (val & PLLU_BASE_POST_DIV) ? 1 : 2;
708 else
709 c->div *= (val & PLL_BASE_DIVP_MASK) ? 2 : 1;
Colin Crossd8611962010-01-28 16:40:29 -0800710 }
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530711
712 if (c->mul != 0 && c->div != 0) {
713 rate *= c->mul;
714 rate += c->div - 1; /* round up */
715 do_div(rate, c->div);
716 }
717 return rate;
Colin Crossd8611962010-01-28 16:40:29 -0800718}
719
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530720static int tegra20_pll_clk_enable(struct clk_hw *hw)
Colin Crossd8611962010-01-28 16:40:29 -0800721{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530722 struct clk_tegra *c = to_clk_tegra(hw);
Colin Crossd8611962010-01-28 16:40:29 -0800723 u32 val;
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530724 pr_debug("%s on clock %s\n", __func__, __clk_get_name(hw->clk));
Colin Crossd8611962010-01-28 16:40:29 -0800725
726 val = clk_readl(c->reg + PLL_BASE);
727 val &= ~PLL_BASE_BYPASS;
728 val |= PLL_BASE_ENABLE;
729 clk_writel(val, c->reg + PLL_BASE);
730
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530731 tegra20_pll_clk_wait_for_lock(c);
Colin Crossd8611962010-01-28 16:40:29 -0800732
733 return 0;
734}
735
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530736static void tegra20_pll_clk_disable(struct clk_hw *hw)
Colin Crossd8611962010-01-28 16:40:29 -0800737{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530738 struct clk_tegra *c = to_clk_tegra(hw);
Colin Crossd8611962010-01-28 16:40:29 -0800739 u32 val;
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530740 pr_debug("%s on clock %s\n", __func__, __clk_get_name(hw->clk));
Colin Crossd8611962010-01-28 16:40:29 -0800741
742 val = clk_readl(c->reg);
743 val &= ~(PLL_BASE_BYPASS | PLL_BASE_ENABLE);
744 clk_writel(val, c->reg);
745}
746
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530747static int tegra20_pll_clk_set_rate(struct clk_hw *hw, unsigned long rate,
748 unsigned long parent_rate)
Colin Crossd8611962010-01-28 16:40:29 -0800749{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530750 struct clk_tegra *c = to_clk_tegra(hw);
751 unsigned long input_rate = parent_rate;
Colin Crossf1519612011-02-12 16:05:31 -0800752 const struct clk_pll_freq_table *sel;
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530753 u32 val;
Colin Crossd8611962010-01-28 16:40:29 -0800754
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530755 pr_debug("%s: %s %lu\n", __func__, __clk_get_name(hw->clk), rate);
Colin Crossd8611962010-01-28 16:40:29 -0800756
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530757 if (c->flags & PLL_FIXED) {
758 int ret = 0;
759 if (rate != c->u.pll.fixed_rate) {
760 pr_err("%s: Can not change %s fixed rate %lu to %lu\n",
761 __func__, __clk_get_name(hw->clk),
762 c->u.pll.fixed_rate, rate);
763 ret = -EINVAL;
764 }
765 return ret;
766 }
767
Colin Crossf1519612011-02-12 16:05:31 -0800768 for (sel = c->u.pll.freq_table; sel->input_rate != 0; sel++) {
Colin Crossd8611962010-01-28 16:40:29 -0800769 if (sel->input_rate == input_rate && sel->output_rate == rate) {
Colin Cross71fc84c2010-06-07 20:49:46 -0700770 c->mul = sel->n;
771 c->div = sel->m * sel->p;
Colin Crossd8611962010-01-28 16:40:29 -0800772
773 val = clk_readl(c->reg + PLL_BASE);
774 if (c->flags & PLL_FIXED)
775 val |= PLL_BASE_OVERRIDE;
776 val &= ~(PLL_BASE_DIVP_MASK | PLL_BASE_DIVN_MASK |
777 PLL_BASE_DIVM_MASK);
Colin Cross71fc84c2010-06-07 20:49:46 -0700778 val |= (sel->m << PLL_BASE_DIVM_SHIFT) |
779 (sel->n << PLL_BASE_DIVN_SHIFT);
780 BUG_ON(sel->p < 1 || sel->p > 2);
781 if (c->flags & PLLU) {
782 if (sel->p == 1)
783 val |= PLLU_BASE_POST_DIV;
784 } else {
785 if (sel->p == 2)
786 val |= 1 << PLL_BASE_DIVP_SHIFT;
787 }
Colin Crossd8611962010-01-28 16:40:29 -0800788 clk_writel(val, c->reg + PLL_BASE);
789
790 if (c->flags & PLL_HAS_CPCON) {
Colin Cross71fc84c2010-06-07 20:49:46 -0700791 val = clk_readl(c->reg + PLL_MISC(c));
792 val &= ~PLL_MISC_CPCON_MASK;
793 val |= sel->cpcon << PLL_MISC_CPCON_SHIFT;
Colin Crossd8611962010-01-28 16:40:29 -0800794 clk_writel(val, c->reg + PLL_MISC(c));
795 }
796
797 if (c->state == ON)
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530798 tegra20_pll_clk_enable(hw);
Colin Crossd8611962010-01-28 16:40:29 -0800799 return 0;
800 }
801 }
802 return -EINVAL;
803}
804
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530805static long tegra20_pll_clk_round_rate(struct clk_hw *hw, unsigned long rate,
806 unsigned long *prate)
807{
808 struct clk_tegra *c = to_clk_tegra(hw);
809 const struct clk_pll_freq_table *sel;
810 unsigned long input_rate = *prate;
Stephen Warren7a74a442012-09-10 17:02:45 -0600811 u64 output_rate = *prate;
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530812 int mul;
813 int div;
814
815 if (c->flags & PLL_FIXED)
816 return c->u.pll.fixed_rate;
817
818 for (sel = c->u.pll.freq_table; sel->input_rate != 0; sel++)
819 if (sel->input_rate == input_rate && sel->output_rate == rate) {
820 mul = sel->n;
821 div = sel->m * sel->p;
822 break;
823 }
824
825 if (sel->input_rate == 0)
826 return -EINVAL;
827
828 output_rate *= mul;
829 output_rate += div - 1; /* round up */
830 do_div(output_rate, div);
831
832 return output_rate;
833}
834
Prashant Gaikwad86edb872012-08-06 11:57:40 +0530835struct clk_ops tegra_pll_ops = {
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530836 .is_enabled = tegra20_pll_clk_is_enabled,
837 .enable = tegra20_pll_clk_enable,
838 .disable = tegra20_pll_clk_disable,
839 .set_rate = tegra20_pll_clk_set_rate,
840 .recalc_rate = tegra20_pll_clk_recalc_rate,
841 .round_rate = tegra20_pll_clk_round_rate,
Colin Cross71fc84c2010-06-07 20:49:46 -0700842};
843
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530844static void tegra20_pllx_clk_init(struct clk_hw *hw)
Colin Cross71fc84c2010-06-07 20:49:46 -0700845{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530846 struct clk_tegra *c = to_clk_tegra(hw);
Colin Cross71fc84c2010-06-07 20:49:46 -0700847
Olof Johansson9a1086d2011-10-13 00:31:20 -0700848 if (tegra_sku_id == 7)
Colin Cross71fc84c2010-06-07 20:49:46 -0700849 c->max_rate = 750000000;
850}
851
Prashant Gaikwad86edb872012-08-06 11:57:40 +0530852struct clk_ops tegra_pllx_ops = {
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530853 .init = tegra20_pllx_clk_init,
854 .is_enabled = tegra20_pll_clk_is_enabled,
855 .enable = tegra20_pll_clk_enable,
856 .disable = tegra20_pll_clk_disable,
857 .set_rate = tegra20_pll_clk_set_rate,
858 .recalc_rate = tegra20_pll_clk_recalc_rate,
859 .round_rate = tegra20_pll_clk_round_rate,
Colin Crossd8611962010-01-28 16:40:29 -0800860};
861
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530862static int tegra20_plle_clk_enable(struct clk_hw *hw)
Mike Rapoport8d685bc2010-09-27 11:26:32 +0200863{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530864 struct clk_tegra *c = to_clk_tegra(hw);
Mike Rapoport8d685bc2010-09-27 11:26:32 +0200865 u32 val;
866
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530867 pr_debug("%s on clock %s\n", __func__, __clk_get_name(hw->clk));
Mike Rapoport8d685bc2010-09-27 11:26:32 +0200868
869 mdelay(1);
870
871 val = clk_readl(c->reg + PLL_BASE);
872 if (!(val & PLLE_MISC_READY))
873 return -EBUSY;
874
875 val = clk_readl(c->reg + PLL_BASE);
876 val |= PLL_BASE_ENABLE | PLL_BASE_BYPASS;
877 clk_writel(val, c->reg + PLL_BASE);
878
879 return 0;
880}
881
Prashant Gaikwad86edb872012-08-06 11:57:40 +0530882struct clk_ops tegra_plle_ops = {
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530883 .is_enabled = tegra20_pll_clk_is_enabled,
884 .enable = tegra20_plle_clk_enable,
885 .set_rate = tegra20_pll_clk_set_rate,
886 .recalc_rate = tegra20_pll_clk_recalc_rate,
887 .round_rate = tegra20_pll_clk_round_rate,
Mike Rapoport8d685bc2010-09-27 11:26:32 +0200888};
889
Colin Crossd8611962010-01-28 16:40:29 -0800890/* Clock divider ops */
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530891static int tegra20_pll_div_clk_is_enabled(struct clk_hw *hw)
Colin Crossd8611962010-01-28 16:40:29 -0800892{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530893 struct clk_tegra *c = to_clk_tegra(hw);
Colin Crossd8611962010-01-28 16:40:29 -0800894 u32 val = clk_readl(c->reg);
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530895
Colin Crossd8611962010-01-28 16:40:29 -0800896 val >>= c->reg_shift;
897 c->state = (val & PLL_OUT_CLKEN) ? ON : OFF;
898 if (!(val & PLL_OUT_RESET_DISABLE))
899 c->state = OFF;
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530900 return c->state;
901}
902
903static unsigned long tegra20_pll_div_clk_recalc_rate(struct clk_hw *hw,
904 unsigned long prate)
905{
906 struct clk_tegra *c = to_clk_tegra(hw);
907 u64 rate = prate;
908 u32 val = clk_readl(c->reg);
909 u32 divu71;
910
911 val >>= c->reg_shift;
Colin Crossd8611962010-01-28 16:40:29 -0800912
913 if (c->flags & DIV_U71) {
914 divu71 = (val & PLL_OUT_RATIO_MASK) >> PLL_OUT_RATIO_SHIFT;
915 c->div = (divu71 + 2);
916 c->mul = 2;
917 } else if (c->flags & DIV_2) {
918 c->div = 2;
919 c->mul = 1;
920 } else {
921 c->div = 1;
922 c->mul = 1;
923 }
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530924
925 rate *= c->mul;
926 rate += c->div - 1; /* round up */
927 do_div(rate, c->div);
928
929 return rate;
Colin Crossd8611962010-01-28 16:40:29 -0800930}
931
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530932static int tegra20_pll_div_clk_enable(struct clk_hw *hw)
Colin Crossd8611962010-01-28 16:40:29 -0800933{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530934 struct clk_tegra *c = to_clk_tegra(hw);
Colin Cross4729fd72011-02-12 16:43:05 -0800935 unsigned long flags;
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530936 u32 new_val;
937 u32 val;
Colin Crossd8611962010-01-28 16:40:29 -0800938
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530939 pr_debug("%s: %s\n", __func__, __clk_get_name(hw->clk));
940
Colin Crossd8611962010-01-28 16:40:29 -0800941 if (c->flags & DIV_U71) {
Colin Cross4729fd72011-02-12 16:43:05 -0800942 spin_lock_irqsave(&clock_register_lock, flags);
Colin Crossd8611962010-01-28 16:40:29 -0800943 val = clk_readl(c->reg);
944 new_val = val >> c->reg_shift;
945 new_val &= 0xFFFF;
946
947 new_val |= PLL_OUT_CLKEN | PLL_OUT_RESET_DISABLE;
948
949 val &= ~(0xFFFF << c->reg_shift);
950 val |= new_val << c->reg_shift;
951 clk_writel(val, c->reg);
Colin Cross4729fd72011-02-12 16:43:05 -0800952 spin_unlock_irqrestore(&clock_register_lock, flags);
Colin Crossd8611962010-01-28 16:40:29 -0800953 return 0;
954 } else if (c->flags & DIV_2) {
955 BUG_ON(!(c->flags & PLLD));
Colin Cross4729fd72011-02-12 16:43:05 -0800956 spin_lock_irqsave(&clock_register_lock, flags);
Colin Crossd8611962010-01-28 16:40:29 -0800957 val = clk_readl(c->reg);
958 val &= ~PLLD_MISC_DIV_RST;
959 clk_writel(val, c->reg);
Colin Cross4729fd72011-02-12 16:43:05 -0800960 spin_unlock_irqrestore(&clock_register_lock, flags);
Colin Crossd8611962010-01-28 16:40:29 -0800961 return 0;
962 }
963 return -EINVAL;
964}
965
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530966static void tegra20_pll_div_clk_disable(struct clk_hw *hw)
Colin Crossd8611962010-01-28 16:40:29 -0800967{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530968 struct clk_tegra *c = to_clk_tegra(hw);
Colin Cross4729fd72011-02-12 16:43:05 -0800969 unsigned long flags;
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530970 u32 new_val;
971 u32 val;
Colin Crossd8611962010-01-28 16:40:29 -0800972
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530973 pr_debug("%s: %s\n", __func__, __clk_get_name(hw->clk));
974
Colin Crossd8611962010-01-28 16:40:29 -0800975 if (c->flags & DIV_U71) {
Colin Cross4729fd72011-02-12 16:43:05 -0800976 spin_lock_irqsave(&clock_register_lock, flags);
Colin Crossd8611962010-01-28 16:40:29 -0800977 val = clk_readl(c->reg);
978 new_val = val >> c->reg_shift;
979 new_val &= 0xFFFF;
980
981 new_val &= ~(PLL_OUT_CLKEN | PLL_OUT_RESET_DISABLE);
982
983 val &= ~(0xFFFF << c->reg_shift);
984 val |= new_val << c->reg_shift;
985 clk_writel(val, c->reg);
Colin Cross4729fd72011-02-12 16:43:05 -0800986 spin_unlock_irqrestore(&clock_register_lock, flags);
Colin Crossd8611962010-01-28 16:40:29 -0800987 } else if (c->flags & DIV_2) {
988 BUG_ON(!(c->flags & PLLD));
Colin Cross4729fd72011-02-12 16:43:05 -0800989 spin_lock_irqsave(&clock_register_lock, flags);
Colin Crossd8611962010-01-28 16:40:29 -0800990 val = clk_readl(c->reg);
991 val |= PLLD_MISC_DIV_RST;
992 clk_writel(val, c->reg);
Colin Cross4729fd72011-02-12 16:43:05 -0800993 spin_unlock_irqrestore(&clock_register_lock, flags);
Colin Crossd8611962010-01-28 16:40:29 -0800994 }
995}
996
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530997static int tegra20_pll_div_clk_set_rate(struct clk_hw *hw, unsigned long rate,
998 unsigned long parent_rate)
Colin Crossd8611962010-01-28 16:40:29 -0800999{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301000 struct clk_tegra *c = to_clk_tegra(hw);
Colin Cross4729fd72011-02-12 16:43:05 -08001001 unsigned long flags;
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301002 int divider_u71;
1003 u32 new_val;
1004 u32 val;
Colin Cross4729fd72011-02-12 16:43:05 -08001005
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301006 pr_debug("%s: %s %lu\n", __func__, __clk_get_name(hw->clk), rate);
1007
Colin Crossd8611962010-01-28 16:40:29 -08001008 if (c->flags & DIV_U71) {
Colin Cross4729fd72011-02-12 16:43:05 -08001009 divider_u71 = clk_div71_get_divider(parent_rate, rate);
Colin Crossd8611962010-01-28 16:40:29 -08001010 if (divider_u71 >= 0) {
Colin Cross4729fd72011-02-12 16:43:05 -08001011 spin_lock_irqsave(&clock_register_lock, flags);
Colin Crossd8611962010-01-28 16:40:29 -08001012 val = clk_readl(c->reg);
1013 new_val = val >> c->reg_shift;
1014 new_val &= 0xFFFF;
1015 if (c->flags & DIV_U71_FIXED)
1016 new_val |= PLL_OUT_OVERRIDE;
1017 new_val &= ~PLL_OUT_RATIO_MASK;
1018 new_val |= divider_u71 << PLL_OUT_RATIO_SHIFT;
1019
1020 val &= ~(0xFFFF << c->reg_shift);
1021 val |= new_val << c->reg_shift;
1022 clk_writel(val, c->reg);
1023 c->div = divider_u71 + 2;
1024 c->mul = 2;
Colin Cross4729fd72011-02-12 16:43:05 -08001025 spin_unlock_irqrestore(&clock_register_lock, flags);
Colin Crossd8611962010-01-28 16:40:29 -08001026 return 0;
1027 }
1028 } else if (c->flags & DIV_2) {
Colin Cross4729fd72011-02-12 16:43:05 -08001029 if (parent_rate == rate * 2)
Colin Crossd8611962010-01-28 16:40:29 -08001030 return 0;
Colin Crossd8611962010-01-28 16:40:29 -08001031 }
1032 return -EINVAL;
1033}
1034
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301035static long tegra20_pll_div_clk_round_rate(struct clk_hw *hw, unsigned long rate,
1036 unsigned long *prate)
Colin Cross71fc84c2010-06-07 20:49:46 -07001037{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301038 struct clk_tegra *c = to_clk_tegra(hw);
1039 unsigned long parent_rate = *prate;
Colin Cross71fc84c2010-06-07 20:49:46 -07001040 int divider;
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301041
1042 pr_debug("%s: %s %lu\n", __func__, __clk_get_name(hw->clk), rate);
Colin Cross71fc84c2010-06-07 20:49:46 -07001043
1044 if (c->flags & DIV_U71) {
Colin Cross4729fd72011-02-12 16:43:05 -08001045 divider = clk_div71_get_divider(parent_rate, rate);
Colin Cross71fc84c2010-06-07 20:49:46 -07001046 if (divider < 0)
1047 return divider;
Colin Cross421186e2011-02-12 18:21:47 -08001048 return DIV_ROUND_UP(parent_rate * 2, divider + 2);
Colin Cross71fc84c2010-06-07 20:49:46 -07001049 } else if (c->flags & DIV_2) {
Colin Cross421186e2011-02-12 18:21:47 -08001050 return DIV_ROUND_UP(parent_rate, 2);
Colin Cross71fc84c2010-06-07 20:49:46 -07001051 }
1052 return -EINVAL;
1053}
Colin Crossd8611962010-01-28 16:40:29 -08001054
Prashant Gaikwad86edb872012-08-06 11:57:40 +05301055struct clk_ops tegra_pll_div_ops = {
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301056 .is_enabled = tegra20_pll_div_clk_is_enabled,
1057 .enable = tegra20_pll_div_clk_enable,
1058 .disable = tegra20_pll_div_clk_disable,
1059 .set_rate = tegra20_pll_div_clk_set_rate,
1060 .round_rate = tegra20_pll_div_clk_round_rate,
1061 .recalc_rate = tegra20_pll_div_clk_recalc_rate,
Colin Crossd8611962010-01-28 16:40:29 -08001062};
1063
1064/* Periph clk ops */
1065
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301066static int tegra20_periph_clk_is_enabled(struct clk_hw *hw)
Colin Crossd8611962010-01-28 16:40:29 -08001067{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301068 struct clk_tegra *c = to_clk_tegra(hw);
Colin Crossd8611962010-01-28 16:40:29 -08001069
1070 c->state = ON;
Colin Cross1be3d052011-02-21 16:44:07 -08001071
1072 if (!c->u.periph.clk_num)
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301073 goto out;
Colin Cross1be3d052011-02-21 16:44:07 -08001074
Colin Crossd8611962010-01-28 16:40:29 -08001075 if (!(clk_readl(CLK_OUT_ENB + PERIPH_CLK_TO_ENB_REG(c)) &
1076 PERIPH_CLK_TO_ENB_BIT(c)))
1077 c->state = OFF;
Colin Cross1be3d052011-02-21 16:44:07 -08001078
Colin Crossd8611962010-01-28 16:40:29 -08001079 if (!(c->flags & PERIPH_NO_RESET))
1080 if (clk_readl(RST_DEVICES + PERIPH_CLK_TO_ENB_REG(c)) &
1081 PERIPH_CLK_TO_ENB_BIT(c))
1082 c->state = OFF;
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301083
1084out:
1085 return c->state;
Colin Crossd8611962010-01-28 16:40:29 -08001086}
1087
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301088static int tegra20_periph_clk_enable(struct clk_hw *hw)
Colin Crossd8611962010-01-28 16:40:29 -08001089{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301090 struct clk_tegra *c = to_clk_tegra(hw);
Colin Cross78f379b2010-10-20 19:19:58 -07001091 unsigned long flags;
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301092 u32 val;
1093
1094 pr_debug("%s on clock %s\n", __func__, __clk_get_name(hw->clk));
Colin Crossd8611962010-01-28 16:40:29 -08001095
Colin Cross1be3d052011-02-21 16:44:07 -08001096 if (!c->u.periph.clk_num)
1097 return 0;
1098
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301099 tegra_periph_clk_enable_refcount[c->u.periph.clk_num]++;
1100 if (tegra_periph_clk_enable_refcount[c->u.periph.clk_num] > 1)
1101 return 0;
1102
Colin Cross78f379b2010-10-20 19:19:58 -07001103 spin_lock_irqsave(&clock_register_lock, flags);
1104
Colin Crossd8611962010-01-28 16:40:29 -08001105 clk_writel(PERIPH_CLK_TO_ENB_BIT(c),
1106 CLK_OUT_ENB_SET + PERIPH_CLK_TO_ENB_SET_REG(c));
1107 if (!(c->flags & PERIPH_NO_RESET) && !(c->flags & PERIPH_MANUAL_RESET))
1108 clk_writel(PERIPH_CLK_TO_ENB_BIT(c),
1109 RST_DEVICES_CLR + PERIPH_CLK_TO_ENB_SET_REG(c));
1110 if (c->flags & PERIPH_EMC_ENB) {
1111 /* The EMC peripheral clock has 2 extra enable bits */
1112 /* FIXME: Do they need to be disabled? */
1113 val = clk_readl(c->reg);
1114 val |= 0x3 << 24;
1115 clk_writel(val, c->reg);
1116 }
Colin Cross78f379b2010-10-20 19:19:58 -07001117
Colin Cross78f379b2010-10-20 19:19:58 -07001118 spin_unlock_irqrestore(&clock_register_lock, flags);
1119
Colin Crossd8611962010-01-28 16:40:29 -08001120 return 0;
1121}
1122
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301123static void tegra20_periph_clk_disable(struct clk_hw *hw)
Colin Crossd8611962010-01-28 16:40:29 -08001124{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301125 struct clk_tegra *c = to_clk_tegra(hw);
Colin Cross78f379b2010-10-20 19:19:58 -07001126 unsigned long flags;
1127
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301128 pr_debug("%s on clock %s\n", __func__, __clk_get_name(hw->clk));
Colin Crossd8611962010-01-28 16:40:29 -08001129
Colin Cross1be3d052011-02-21 16:44:07 -08001130 if (!c->u.periph.clk_num)
1131 return;
1132
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301133 tegra_periph_clk_enable_refcount[c->u.periph.clk_num]--;
1134
1135 if (tegra_periph_clk_enable_refcount[c->u.periph.clk_num] > 0)
1136 return;
1137
Colin Cross78f379b2010-10-20 19:19:58 -07001138 spin_lock_irqsave(&clock_register_lock, flags);
1139
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301140 clk_writel(PERIPH_CLK_TO_ENB_BIT(c),
1141 CLK_OUT_ENB_CLR + PERIPH_CLK_TO_ENB_SET_REG(c));
Colin Cross78f379b2010-10-20 19:19:58 -07001142
1143 spin_unlock_irqrestore(&clock_register_lock, flags);
Colin Crossd8611962010-01-28 16:40:29 -08001144}
1145
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301146void tegra2_periph_clk_reset(struct clk_hw *hw, bool assert)
Colin Crossd8611962010-01-28 16:40:29 -08001147{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301148 struct clk_tegra *c = to_clk_tegra(hw);
Dima Zavin2b84cb4f2010-09-02 19:11:11 -07001149 unsigned long base = assert ? RST_DEVICES_SET : RST_DEVICES_CLR;
1150
1151 pr_debug("%s %s on clock %s\n", __func__,
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301152 assert ? "assert" : "deassert", __clk_get_name(hw->clk));
Colin Cross1be3d052011-02-21 16:44:07 -08001153
1154 BUG_ON(!c->u.periph.clk_num);
1155
Colin Crossd8611962010-01-28 16:40:29 -08001156 if (!(c->flags & PERIPH_NO_RESET))
1157 clk_writel(PERIPH_CLK_TO_ENB_BIT(c),
Dima Zavin2b84cb4f2010-09-02 19:11:11 -07001158 base + PERIPH_CLK_TO_ENB_SET_REG(c));
Colin Crossd8611962010-01-28 16:40:29 -08001159}
1160
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301161static int tegra20_periph_clk_set_parent(struct clk_hw *hw, u8 index)
Colin Crossd8611962010-01-28 16:40:29 -08001162{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301163 struct clk_tegra *c = to_clk_tegra(hw);
Colin Crossd8611962010-01-28 16:40:29 -08001164 u32 val;
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301165 u32 mask;
1166 u32 shift;
Simon Quebb1dccf2011-12-16 20:11:22 +01001167
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301168 pr_debug("%s: %s %d\n", __func__, __clk_get_name(hw->clk), index);
Simon Quebb1dccf2011-12-16 20:11:22 +01001169
1170 if (c->flags & MUX_PWM) {
1171 shift = PERIPH_CLK_SOURCE_PWM_SHIFT;
1172 mask = PERIPH_CLK_SOURCE_PWM_MASK;
1173 } else {
1174 shift = PERIPH_CLK_SOURCE_SHIFT;
1175 mask = PERIPH_CLK_SOURCE_MASK;
1176 }
1177
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301178 val = clk_readl(c->reg);
1179 val &= ~mask;
1180 val |= (index) << shift;
Colin Cross71fc84c2010-06-07 20:49:46 -07001181
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301182 clk_writel(val, c->reg);
Colin Cross71fc84c2010-06-07 20:49:46 -07001183
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301184 return 0;
Colin Crossd8611962010-01-28 16:40:29 -08001185}
1186
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301187static u8 tegra20_periph_clk_get_parent(struct clk_hw *hw)
Colin Crossd8611962010-01-28 16:40:29 -08001188{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301189 struct clk_tegra *c = to_clk_tegra(hw);
1190 u32 val = clk_readl(c->reg);
1191 u32 mask;
1192 u32 shift;
1193
1194 if (c->flags & MUX_PWM) {
1195 shift = PERIPH_CLK_SOURCE_PWM_SHIFT;
1196 mask = PERIPH_CLK_SOURCE_PWM_MASK;
1197 } else {
1198 shift = PERIPH_CLK_SOURCE_SHIFT;
1199 mask = PERIPH_CLK_SOURCE_MASK;
1200 }
1201
1202 if (c->flags & MUX)
1203 return (val & mask) >> shift;
1204 else
1205 return 0;
1206}
1207
1208static unsigned long tegra20_periph_clk_recalc_rate(struct clk_hw *hw,
1209 unsigned long prate)
1210{
1211 struct clk_tegra *c = to_clk_tegra(hw);
1212 unsigned long rate = prate;
1213 u32 val = clk_readl(c->reg);
1214
1215 if (c->flags & DIV_U71) {
1216 u32 divu71 = val & PERIPH_CLK_SOURCE_DIVU71_MASK;
1217 c->div = divu71 + 2;
1218 c->mul = 2;
1219 } else if (c->flags & DIV_U16) {
1220 u32 divu16 = val & PERIPH_CLK_SOURCE_DIVU16_MASK;
1221 c->div = divu16 + 1;
1222 c->mul = 1;
1223 } else {
1224 c->div = 1;
1225 c->mul = 1;
1226 return rate;
1227 }
1228
1229 if (c->mul != 0 && c->div != 0) {
1230 rate *= c->mul;
1231 rate += c->div - 1; /* round up */
1232 do_div(rate, c->div);
1233 }
1234
1235 return rate;
1236}
1237
1238static int tegra20_periph_clk_set_rate(struct clk_hw *hw, unsigned long rate,
1239 unsigned long parent_rate)
1240{
1241 struct clk_tegra *c = to_clk_tegra(hw);
Colin Crossd8611962010-01-28 16:40:29 -08001242 u32 val;
Colin Cross71fc84c2010-06-07 20:49:46 -07001243 int divider;
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301244
1245 val = clk_readl(c->reg);
Colin Cross4729fd72011-02-12 16:43:05 -08001246
Colin Crossd8611962010-01-28 16:40:29 -08001247 if (c->flags & DIV_U71) {
Colin Cross4729fd72011-02-12 16:43:05 -08001248 divider = clk_div71_get_divider(parent_rate, rate);
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301249
Colin Cross71fc84c2010-06-07 20:49:46 -07001250 if (divider >= 0) {
Colin Crossd8611962010-01-28 16:40:29 -08001251 val = clk_readl(c->reg);
Colin Cross71fc84c2010-06-07 20:49:46 -07001252 val &= ~PERIPH_CLK_SOURCE_DIVU71_MASK;
1253 val |= divider;
Colin Crossd8611962010-01-28 16:40:29 -08001254 clk_writel(val, c->reg);
Colin Cross71fc84c2010-06-07 20:49:46 -07001255 c->div = divider + 2;
Colin Crossd8611962010-01-28 16:40:29 -08001256 c->mul = 2;
Colin Crossd8611962010-01-28 16:40:29 -08001257 return 0;
1258 }
Colin Cross71fc84c2010-06-07 20:49:46 -07001259 } else if (c->flags & DIV_U16) {
Colin Cross4729fd72011-02-12 16:43:05 -08001260 divider = clk_div16_get_divider(parent_rate, rate);
Colin Cross71fc84c2010-06-07 20:49:46 -07001261 if (divider >= 0) {
1262 val = clk_readl(c->reg);
1263 val &= ~PERIPH_CLK_SOURCE_DIVU16_MASK;
1264 val |= divider;
1265 clk_writel(val, c->reg);
1266 c->div = divider + 1;
1267 c->mul = 1;
1268 return 0;
1269 }
Colin Cross4729fd72011-02-12 16:43:05 -08001270 } else if (parent_rate <= rate) {
Colin Cross71fc84c2010-06-07 20:49:46 -07001271 c->div = 1;
1272 c->mul = 1;
1273 return 0;
1274 }
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301275
Colin Cross71fc84c2010-06-07 20:49:46 -07001276 return -EINVAL;
1277}
1278
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301279static long tegra20_periph_clk_round_rate(struct clk_hw *hw,
1280 unsigned long rate, unsigned long *prate)
Colin Cross71fc84c2010-06-07 20:49:46 -07001281{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301282 struct clk_tegra *c = to_clk_tegra(hw);
1283 unsigned long parent_rate = __clk_get_rate(__clk_get_parent(hw->clk));
Colin Cross71fc84c2010-06-07 20:49:46 -07001284 int divider;
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301285
1286 pr_debug("%s: %s %lu\n", __func__, __clk_get_name(hw->clk), rate);
1287
1288 if (prate)
1289 parent_rate = *prate;
Colin Cross71fc84c2010-06-07 20:49:46 -07001290
1291 if (c->flags & DIV_U71) {
Colin Cross4729fd72011-02-12 16:43:05 -08001292 divider = clk_div71_get_divider(parent_rate, rate);
Colin Cross71fc84c2010-06-07 20:49:46 -07001293 if (divider < 0)
1294 return divider;
1295
Colin Cross421186e2011-02-12 18:21:47 -08001296 return DIV_ROUND_UP(parent_rate * 2, divider + 2);
Colin Cross71fc84c2010-06-07 20:49:46 -07001297 } else if (c->flags & DIV_U16) {
Colin Cross4729fd72011-02-12 16:43:05 -08001298 divider = clk_div16_get_divider(parent_rate, rate);
Colin Cross71fc84c2010-06-07 20:49:46 -07001299 if (divider < 0)
1300 return divider;
Colin Cross421186e2011-02-12 18:21:47 -08001301 return DIV_ROUND_UP(parent_rate, divider + 1);
Colin Crossd8611962010-01-28 16:40:29 -08001302 }
1303 return -EINVAL;
1304}
1305
Prashant Gaikwad86edb872012-08-06 11:57:40 +05301306struct clk_ops tegra_periph_clk_ops = {
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301307 .is_enabled = tegra20_periph_clk_is_enabled,
1308 .enable = tegra20_periph_clk_enable,
1309 .disable = tegra20_periph_clk_disable,
1310 .set_parent = tegra20_periph_clk_set_parent,
1311 .get_parent = tegra20_periph_clk_get_parent,
1312 .set_rate = tegra20_periph_clk_set_rate,
1313 .round_rate = tegra20_periph_clk_round_rate,
1314 .recalc_rate = tegra20_periph_clk_recalc_rate,
Colin Crossd8611962010-01-28 16:40:29 -08001315};
1316
Colin Cross6d296822010-11-22 18:37:54 -08001317/* External memory controller clock ops */
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301318static void tegra20_emc_clk_init(struct clk_hw *hw)
Colin Cross6d296822010-11-22 18:37:54 -08001319{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301320 struct clk_tegra *c = to_clk_tegra(hw);
1321 c->max_rate = __clk_get_rate(hw->clk);
Colin Cross6d296822010-11-22 18:37:54 -08001322}
1323
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301324static long tegra20_emc_clk_round_rate(struct clk_hw *hw, unsigned long rate,
1325 unsigned long *prate)
Colin Cross6d296822010-11-22 18:37:54 -08001326{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301327 struct clk_tegra *c = to_clk_tegra(hw);
Stephen Warrene186ad72012-02-06 17:09:15 -08001328 long emc_rate;
1329 long clk_rate;
Colin Cross6d296822010-11-22 18:37:54 -08001330
Stephen Warrene186ad72012-02-06 17:09:15 -08001331 /*
1332 * The slowest entry in the EMC clock table that is at least as
1333 * fast as rate.
1334 */
1335 emc_rate = tegra_emc_round_rate(rate);
1336 if (emc_rate < 0)
Colin Cross6d296822010-11-22 18:37:54 -08001337 return c->max_rate;
1338
Stephen Warrene186ad72012-02-06 17:09:15 -08001339 /*
1340 * The fastest rate the PLL will generate that is at most the
1341 * requested rate.
1342 */
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301343 clk_rate = tegra20_periph_clk_round_rate(hw, emc_rate, NULL);
Colin Cross6d296822010-11-22 18:37:54 -08001344
Stephen Warrene186ad72012-02-06 17:09:15 -08001345 /*
1346 * If this fails, and emc_rate > clk_rate, it's because the maximum
1347 * rate in the EMC tables is larger than the maximum rate of the EMC
1348 * clock. The EMC clock's max rate is the rate it was running when the
1349 * kernel booted. Such a mismatch is probably due to using the wrong
1350 * BCT, i.e. using a Tegra20 BCT with an EMC table written for Tegra25.
1351 */
1352 WARN_ONCE(emc_rate != clk_rate,
1353 "emc_rate %ld != clk_rate %ld",
1354 emc_rate, clk_rate);
1355
1356 return emc_rate;
Colin Cross6d296822010-11-22 18:37:54 -08001357}
1358
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301359static int tegra20_emc_clk_set_rate(struct clk_hw *hw, unsigned long rate,
1360 unsigned long parent_rate)
Colin Cross6d296822010-11-22 18:37:54 -08001361{
1362 int ret;
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301363
Colin Cross6d296822010-11-22 18:37:54 -08001364 /*
1365 * The Tegra2 memory controller has an interlock with the clock
1366 * block that allows memory shadowed registers to be updated,
1367 * and then transfer them to the main registers at the same
1368 * time as the clock update without glitches.
1369 */
1370 ret = tegra_emc_set_rate(rate);
1371 if (ret < 0)
1372 return ret;
1373
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301374 ret = tegra20_periph_clk_set_rate(hw, rate, parent_rate);
Colin Cross6d296822010-11-22 18:37:54 -08001375 udelay(1);
1376
1377 return ret;
1378}
1379
Prashant Gaikwad86edb872012-08-06 11:57:40 +05301380struct clk_ops tegra_emc_clk_ops = {
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301381 .init = tegra20_emc_clk_init,
1382 .is_enabled = tegra20_periph_clk_is_enabled,
1383 .enable = tegra20_periph_clk_enable,
1384 .disable = tegra20_periph_clk_disable,
1385 .set_parent = tegra20_periph_clk_set_parent,
1386 .get_parent = tegra20_periph_clk_get_parent,
1387 .set_rate = tegra20_emc_clk_set_rate,
1388 .round_rate = tegra20_emc_clk_round_rate,
1389 .recalc_rate = tegra20_periph_clk_recalc_rate,
Colin Cross6d296822010-11-22 18:37:54 -08001390};
1391
Colin Crossd8611962010-01-28 16:40:29 -08001392/* Clock doubler ops */
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301393static int tegra20_clk_double_is_enabled(struct clk_hw *hw)
Colin Crossd8611962010-01-28 16:40:29 -08001394{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301395 struct clk_tegra *c = to_clk_tegra(hw);
1396
Colin Crossd8611962010-01-28 16:40:29 -08001397 c->state = ON;
Colin Cross1be3d052011-02-21 16:44:07 -08001398
1399 if (!c->u.periph.clk_num)
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301400 goto out;
Colin Cross1be3d052011-02-21 16:44:07 -08001401
Colin Crossd8611962010-01-28 16:40:29 -08001402 if (!(clk_readl(CLK_OUT_ENB + PERIPH_CLK_TO_ENB_REG(c)) &
1403 PERIPH_CLK_TO_ENB_BIT(c)))
1404 c->state = OFF;
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301405
1406out:
1407 return c->state;
Colin Crossd8611962010-01-28 16:40:29 -08001408};
1409
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301410static unsigned long tegra20_clk_double_recalc_rate(struct clk_hw *hw,
1411 unsigned long prate)
Colin Cross71fc84c2010-06-07 20:49:46 -07001412{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301413 struct clk_tegra *c = to_clk_tegra(hw);
1414 u64 rate = prate;
1415
Colin Cross71fc84c2010-06-07 20:49:46 -07001416 c->mul = 2;
1417 c->div = 1;
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301418
1419 rate *= c->mul;
1420 rate += c->div - 1; /* round up */
1421 do_div(rate, c->div);
1422
1423 return rate;
1424}
1425
1426static long tegra20_clk_double_round_rate(struct clk_hw *hw, unsigned long rate,
1427 unsigned long *prate)
1428{
1429 unsigned long output_rate = *prate;
1430
1431 do_div(output_rate, 2);
1432 return output_rate;
1433}
1434
1435static int tegra20_clk_double_set_rate(struct clk_hw *hw, unsigned long rate,
1436 unsigned long parent_rate)
1437{
1438 if (rate != 2 * parent_rate)
1439 return -EINVAL;
Colin Cross71fc84c2010-06-07 20:49:46 -07001440 return 0;
1441}
1442
Prashant Gaikwad86edb872012-08-06 11:57:40 +05301443struct clk_ops tegra_clk_double_ops = {
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301444 .is_enabled = tegra20_clk_double_is_enabled,
1445 .enable = tegra20_periph_clk_enable,
1446 .disable = tegra20_periph_clk_disable,
1447 .set_rate = tegra20_clk_double_set_rate,
1448 .recalc_rate = tegra20_clk_double_recalc_rate,
1449 .round_rate = tegra20_clk_double_round_rate,
Colin Cross71fc84c2010-06-07 20:49:46 -07001450};
1451
Colin Crosscea62c82010-10-04 11:49:26 -07001452/* Audio sync clock ops */
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301453static int tegra20_audio_sync_clk_is_enabled(struct clk_hw *hw)
Colin Cross71fc84c2010-06-07 20:49:46 -07001454{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301455 struct clk_tegra *c = to_clk_tegra(hw);
Colin Cross71fc84c2010-06-07 20:49:46 -07001456 u32 val = clk_readl(c->reg);
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301457
Colin Cross71fc84c2010-06-07 20:49:46 -07001458 c->state = (val & (1<<4)) ? OFF : ON;
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301459 return c->state;
Colin Cross71fc84c2010-06-07 20:49:46 -07001460}
1461
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301462static int tegra20_audio_sync_clk_enable(struct clk_hw *hw)
Colin Cross71fc84c2010-06-07 20:49:46 -07001463{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301464 struct clk_tegra *c = to_clk_tegra(hw);
1465
Colin Cross71fc84c2010-06-07 20:49:46 -07001466 clk_writel(0, c->reg);
1467 return 0;
1468}
1469
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301470static void tegra20_audio_sync_clk_disable(struct clk_hw *hw)
Colin Cross71fc84c2010-06-07 20:49:46 -07001471{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301472 struct clk_tegra *c = to_clk_tegra(hw);
Colin Cross71fc84c2010-06-07 20:49:46 -07001473 clk_writel(1, c->reg);
1474}
1475
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301476static u8 tegra20_audio_sync_clk_get_parent(struct clk_hw *hw)
Colin Cross71fc84c2010-06-07 20:49:46 -07001477{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301478 struct clk_tegra *c = to_clk_tegra(hw);
1479 u32 val = clk_readl(c->reg);
1480 int source;
1481
1482 source = val & 0xf;
1483 return source;
1484}
1485
1486static int tegra20_audio_sync_clk_set_parent(struct clk_hw *hw, u8 index)
1487{
1488 struct clk_tegra *c = to_clk_tegra(hw);
Colin Cross71fc84c2010-06-07 20:49:46 -07001489 u32 val;
Colin Cross71fc84c2010-06-07 20:49:46 -07001490
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301491 val = clk_readl(c->reg);
1492 val &= ~0xf;
1493 val |= index;
Colin Cross71fc84c2010-06-07 20:49:46 -07001494
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301495 clk_writel(val, c->reg);
Colin Cross71fc84c2010-06-07 20:49:46 -07001496
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301497 return 0;
Colin Cross71fc84c2010-06-07 20:49:46 -07001498}
1499
Prashant Gaikwad86edb872012-08-06 11:57:40 +05301500struct clk_ops tegra_audio_sync_clk_ops = {
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301501 .is_enabled = tegra20_audio_sync_clk_is_enabled,
1502 .enable = tegra20_audio_sync_clk_enable,
1503 .disable = tegra20_audio_sync_clk_disable,
1504 .set_parent = tegra20_audio_sync_clk_set_parent,
1505 .get_parent = tegra20_audio_sync_clk_get_parent,
Colin Crossd8611962010-01-28 16:40:29 -08001506};
1507
Colin Crosscea62c82010-10-04 11:49:26 -07001508/* cdev1 and cdev2 (dap_mclk1 and dap_mclk2) ops */
1509
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301510static int tegra20_cdev_clk_is_enabled(struct clk_hw *hw)
Colin Crosscea62c82010-10-04 11:49:26 -07001511{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301512 struct clk_tegra *c = to_clk_tegra(hw);
Colin Crosscea62c82010-10-04 11:49:26 -07001513 /* We could un-tristate the cdev1 or cdev2 pingroup here; this is
1514 * currently done in the pinmux code. */
1515 c->state = ON;
Colin Cross1be3d052011-02-21 16:44:07 -08001516
1517 BUG_ON(!c->u.periph.clk_num);
1518
Colin Crosscea62c82010-10-04 11:49:26 -07001519 if (!(clk_readl(CLK_OUT_ENB + PERIPH_CLK_TO_ENB_REG(c)) &
1520 PERIPH_CLK_TO_ENB_BIT(c)))
1521 c->state = OFF;
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301522 return c->state;
Colin Crosscea62c82010-10-04 11:49:26 -07001523}
1524
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301525static int tegra20_cdev_clk_enable(struct clk_hw *hw)
Colin Crosscea62c82010-10-04 11:49:26 -07001526{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301527 struct clk_tegra *c = to_clk_tegra(hw);
Colin Cross1be3d052011-02-21 16:44:07 -08001528 BUG_ON(!c->u.periph.clk_num);
1529
Colin Crosscea62c82010-10-04 11:49:26 -07001530 clk_writel(PERIPH_CLK_TO_ENB_BIT(c),
1531 CLK_OUT_ENB_SET + PERIPH_CLK_TO_ENB_SET_REG(c));
1532 return 0;
1533}
1534
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301535static void tegra20_cdev_clk_disable(struct clk_hw *hw)
Colin Crosscea62c82010-10-04 11:49:26 -07001536{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301537 struct clk_tegra *c = to_clk_tegra(hw);
Colin Cross1be3d052011-02-21 16:44:07 -08001538 BUG_ON(!c->u.periph.clk_num);
1539
Colin Crosscea62c82010-10-04 11:49:26 -07001540 clk_writel(PERIPH_CLK_TO_ENB_BIT(c),
1541 CLK_OUT_ENB_CLR + PERIPH_CLK_TO_ENB_SET_REG(c));
1542}
1543
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301544static unsigned long tegra20_cdev_recalc_rate(struct clk_hw *hw,
1545 unsigned long prate)
1546{
1547 return to_clk_tegra(hw)->fixed_rate;
1548}
1549
Prashant Gaikwad86edb872012-08-06 11:57:40 +05301550struct clk_ops tegra_cdev_clk_ops = {
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301551 .is_enabled = tegra20_cdev_clk_is_enabled,
1552 .enable = tegra20_cdev_clk_enable,
1553 .disable = tegra20_cdev_clk_disable,
1554 .recalc_rate = tegra20_cdev_recalc_rate,
Colin Cross310992c2011-02-12 16:14:03 -08001555};