blob: b9124afcca11336ac50d0b3a6382facb10f0fc19 [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 Gaikwad92fe58f2012-08-06 11:57:43 +0530379static u8 tegra20_cop_clk_get_parent(struct clk_hw *hw)
380{
381 return 0;
Colin Cross71fc84c2010-06-07 20:49:46 -0700382}
383
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530384struct clk_ops tegra_cop_ops = {
385 .get_parent = tegra20_cop_clk_get_parent,
Colin Crossd8611962010-01-28 16:40:29 -0800386};
387
Colin Cross9c7dc562011-02-12 21:25:23 -0800388/* virtual cop clock functions. Used to acquire the fake 'cop' clock to
389 * reset the COP block (i.e. AVP) */
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530390void tegra2_cop_clk_reset(struct clk_hw *hw, bool assert)
Colin Cross9c7dc562011-02-12 21:25:23 -0800391{
392 unsigned long reg = assert ? RST_DEVICES_SET : RST_DEVICES_CLR;
393
394 pr_debug("%s %s\n", __func__, assert ? "assert" : "deassert");
395 clk_writel(1 << 1, reg);
396}
397
Colin Crossd8611962010-01-28 16:40:29 -0800398/* bus clock functions */
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530399static int tegra20_bus_clk_is_enabled(struct clk_hw *hw)
Colin Crossd8611962010-01-28 16:40:29 -0800400{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530401 struct clk_tegra *c = to_clk_tegra(hw);
Colin Crossd8611962010-01-28 16:40:29 -0800402 u32 val = clk_readl(c->reg);
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530403
Colin Crossd8611962010-01-28 16:40:29 -0800404 c->state = ((val >> c->reg_shift) & BUS_CLK_DISABLE) ? OFF : ON;
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530405 return c->state;
Colin Crossd8611962010-01-28 16:40:29 -0800406}
407
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530408static int tegra20_bus_clk_enable(struct clk_hw *hw)
Colin Crossd8611962010-01-28 16:40:29 -0800409{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530410 struct clk_tegra *c = to_clk_tegra(hw);
Colin Cross4729fd72011-02-12 16:43:05 -0800411 unsigned long flags;
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530412 u32 val;
Colin Cross4729fd72011-02-12 16:43:05 -0800413
414 spin_lock_irqsave(&clock_register_lock, flags);
415
416 val = clk_readl(c->reg);
Colin Crossd8611962010-01-28 16:40:29 -0800417 val &= ~(BUS_CLK_DISABLE << c->reg_shift);
418 clk_writel(val, c->reg);
Colin Cross4729fd72011-02-12 16:43:05 -0800419
420 spin_unlock_irqrestore(&clock_register_lock, flags);
421
Colin Crossd8611962010-01-28 16:40:29 -0800422 return 0;
423}
424
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530425static void tegra20_bus_clk_disable(struct clk_hw *hw)
Colin Crossd8611962010-01-28 16:40:29 -0800426{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530427 struct clk_tegra *c = to_clk_tegra(hw);
Colin Cross4729fd72011-02-12 16:43:05 -0800428 unsigned long flags;
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530429 u32 val;
Colin Cross4729fd72011-02-12 16:43:05 -0800430
431 spin_lock_irqsave(&clock_register_lock, flags);
432
433 val = clk_readl(c->reg);
Colin Crossd8611962010-01-28 16:40:29 -0800434 val |= BUS_CLK_DISABLE << c->reg_shift;
435 clk_writel(val, c->reg);
Colin Cross4729fd72011-02-12 16:43:05 -0800436
437 spin_unlock_irqrestore(&clock_register_lock, flags);
Colin Crossd8611962010-01-28 16:40:29 -0800438}
439
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530440static unsigned long tegra20_bus_clk_recalc_rate(struct clk_hw *hw,
441 unsigned long prate)
Colin Crossd8611962010-01-28 16:40:29 -0800442{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530443 struct clk_tegra *c = to_clk_tegra(hw);
444 u32 val = clk_readl(c->reg);
445 u64 rate = prate;
446
447 c->div = ((val >> c->reg_shift) & BUS_CLK_DIV_MASK) + 1;
448 c->mul = 1;
449
450 if (c->mul != 0 && c->div != 0) {
451 rate *= c->mul;
452 rate += c->div - 1; /* round up */
453 do_div(rate, c->div);
454 }
455 return rate;
456}
457
458static int tegra20_bus_clk_set_rate(struct clk_hw *hw, unsigned long rate,
459 unsigned long parent_rate)
460{
461 struct clk_tegra *c = to_clk_tegra(hw);
Colin Cross4729fd72011-02-12 16:43:05 -0800462 int ret = -EINVAL;
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530463 unsigned long flags;
464 u32 val;
Colin Crossd8611962010-01-28 16:40:29 -0800465 int i;
Colin Cross4729fd72011-02-12 16:43:05 -0800466
467 spin_lock_irqsave(&clock_register_lock, flags);
468
469 val = clk_readl(c->reg);
Colin Crossd8611962010-01-28 16:40:29 -0800470 for (i = 1; i <= 4; i++) {
471 if (rate == parent_rate / i) {
472 val &= ~(BUS_CLK_DIV_MASK << c->reg_shift);
473 val |= (i - 1) << c->reg_shift;
474 clk_writel(val, c->reg);
475 c->div = i;
476 c->mul = 1;
Colin Cross4729fd72011-02-12 16:43:05 -0800477 ret = 0;
478 break;
Colin Crossd8611962010-01-28 16:40:29 -0800479 }
480 }
Colin Cross4729fd72011-02-12 16:43:05 -0800481
482 spin_unlock_irqrestore(&clock_register_lock, flags);
483
484 return ret;
Colin Crossd8611962010-01-28 16:40:29 -0800485}
486
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530487static long tegra20_bus_clk_round_rate(struct clk_hw *hw, unsigned long rate,
488 unsigned long *prate)
489{
490 unsigned long parent_rate = *prate;
491 s64 divider;
492
493 if (rate >= parent_rate)
494 return rate;
495
496 divider = parent_rate;
497 divider += rate - 1;
498 do_div(divider, rate);
499
500 if (divider < 0)
501 return divider;
502
503 if (divider > 4)
504 divider = 4;
505 do_div(parent_rate, divider);
506
507 return parent_rate;
508}
509
Prashant Gaikwad86edb872012-08-06 11:57:40 +0530510struct clk_ops tegra_bus_ops = {
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530511 .is_enabled = tegra20_bus_clk_is_enabled,
512 .enable = tegra20_bus_clk_enable,
513 .disable = tegra20_bus_clk_disable,
514 .set_rate = tegra20_bus_clk_set_rate,
515 .round_rate = tegra20_bus_clk_round_rate,
516 .recalc_rate = tegra20_bus_clk_recalc_rate,
Colin Crossd8611962010-01-28 16:40:29 -0800517};
518
Colin Crosscea62c82010-10-04 11:49:26 -0700519/* Blink output functions */
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530520static int tegra20_blink_clk_is_enabled(struct clk_hw *hw)
Colin Crosscea62c82010-10-04 11:49:26 -0700521{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530522 struct clk_tegra *c = to_clk_tegra(hw);
Colin Crosscea62c82010-10-04 11:49:26 -0700523 u32 val;
524
525 val = pmc_readl(PMC_CTRL);
526 c->state = (val & PMC_CTRL_BLINK_ENB) ? ON : OFF;
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530527 return c->state;
528}
529
530static unsigned long tegra20_blink_clk_recalc_rate(struct clk_hw *hw,
531 unsigned long prate)
532{
533 struct clk_tegra *c = to_clk_tegra(hw);
534 u64 rate = prate;
535 u32 val;
536
Colin Crosscea62c82010-10-04 11:49:26 -0700537 c->mul = 1;
538 val = pmc_readl(c->reg);
539
540 if (val & PMC_BLINK_TIMER_ENB) {
541 unsigned int on_off;
542
543 on_off = (val >> PMC_BLINK_TIMER_DATA_ON_SHIFT) &
544 PMC_BLINK_TIMER_DATA_ON_MASK;
545 val >>= PMC_BLINK_TIMER_DATA_OFF_SHIFT;
546 val &= PMC_BLINK_TIMER_DATA_OFF_MASK;
547 on_off += val;
548 /* each tick in the blink timer is 4 32KHz clocks */
549 c->div = on_off * 4;
550 } else {
551 c->div = 1;
552 }
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530553
554 if (c->mul != 0 && c->div != 0) {
555 rate *= c->mul;
556 rate += c->div - 1; /* round up */
557 do_div(rate, c->div);
558 }
559 return rate;
Colin Crosscea62c82010-10-04 11:49:26 -0700560}
561
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530562static int tegra20_blink_clk_enable(struct clk_hw *hw)
Colin Crosscea62c82010-10-04 11:49:26 -0700563{
564 u32 val;
565
566 val = pmc_readl(PMC_DPD_PADS_ORIDE);
567 pmc_writel(val | PMC_DPD_PADS_ORIDE_BLINK_ENB, PMC_DPD_PADS_ORIDE);
568
569 val = pmc_readl(PMC_CTRL);
570 pmc_writel(val | PMC_CTRL_BLINK_ENB, PMC_CTRL);
571
572 return 0;
573}
574
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530575static void tegra20_blink_clk_disable(struct clk_hw *hw)
Colin Crosscea62c82010-10-04 11:49:26 -0700576{
577 u32 val;
578
579 val = pmc_readl(PMC_CTRL);
580 pmc_writel(val & ~PMC_CTRL_BLINK_ENB, PMC_CTRL);
581
582 val = pmc_readl(PMC_DPD_PADS_ORIDE);
583 pmc_writel(val & ~PMC_DPD_PADS_ORIDE_BLINK_ENB, PMC_DPD_PADS_ORIDE);
584}
585
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530586static int tegra20_blink_clk_set_rate(struct clk_hw *hw, unsigned long rate,
587 unsigned long parent_rate)
Colin Crosscea62c82010-10-04 11:49:26 -0700588{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530589 struct clk_tegra *c = to_clk_tegra(hw);
590
Colin Cross4729fd72011-02-12 16:43:05 -0800591 if (rate >= parent_rate) {
Colin Crosscea62c82010-10-04 11:49:26 -0700592 c->div = 1;
593 pmc_writel(0, c->reg);
594 } else {
595 unsigned int on_off;
596 u32 val;
597
Colin Cross4729fd72011-02-12 16:43:05 -0800598 on_off = DIV_ROUND_UP(parent_rate / 8, rate);
Colin Crosscea62c82010-10-04 11:49:26 -0700599 c->div = on_off * 8;
600
601 val = (on_off & PMC_BLINK_TIMER_DATA_ON_MASK) <<
602 PMC_BLINK_TIMER_DATA_ON_SHIFT;
603 on_off &= PMC_BLINK_TIMER_DATA_OFF_MASK;
604 on_off <<= PMC_BLINK_TIMER_DATA_OFF_SHIFT;
605 val |= on_off;
606 val |= PMC_BLINK_TIMER_ENB;
607 pmc_writel(val, c->reg);
608 }
609
610 return 0;
611}
612
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530613static long tegra20_blink_clk_round_rate(struct clk_hw *hw, unsigned long rate,
614 unsigned long *prate)
615{
616 int div;
617 int mul;
618 long round_rate = *prate;
619
620 mul = 1;
621
622 if (rate >= *prate) {
623 div = 1;
624 } else {
625 div = DIV_ROUND_UP(*prate / 8, rate);
626 div *= 8;
627 }
628
629 round_rate *= mul;
630 round_rate += div - 1;
631 do_div(round_rate, div);
632
633 return round_rate;
634}
635
Prashant Gaikwad86edb872012-08-06 11:57:40 +0530636struct clk_ops tegra_blink_clk_ops = {
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530637 .is_enabled = tegra20_blink_clk_is_enabled,
638 .enable = tegra20_blink_clk_enable,
639 .disable = tegra20_blink_clk_disable,
640 .set_rate = tegra20_blink_clk_set_rate,
641 .round_rate = tegra20_blink_clk_round_rate,
642 .recalc_rate = tegra20_blink_clk_recalc_rate,
Colin Crosscea62c82010-10-04 11:49:26 -0700643};
644
Colin Crossd8611962010-01-28 16:40:29 -0800645/* PLL Functions */
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530646static int tegra20_pll_clk_wait_for_lock(struct clk_tegra *c)
Colin Crossd8611962010-01-28 16:40:29 -0800647{
Colin Crossf1519612011-02-12 16:05:31 -0800648 udelay(c->u.pll.lock_delay);
Colin Crossd8611962010-01-28 16:40:29 -0800649 return 0;
650}
651
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530652static int tegra20_pll_clk_is_enabled(struct clk_hw *hw)
Colin Crossd8611962010-01-28 16:40:29 -0800653{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530654 struct clk_tegra *c = to_clk_tegra(hw);
Colin Crossd8611962010-01-28 16:40:29 -0800655 u32 val = clk_readl(c->reg + PLL_BASE);
656
657 c->state = (val & PLL_BASE_ENABLE) ? ON : OFF;
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530658 return c->state;
659}
660
661static unsigned long tegra20_pll_clk_recalc_rate(struct clk_hw *hw,
662 unsigned long prate)
663{
664 struct clk_tegra *c = to_clk_tegra(hw);
665 u32 val = clk_readl(c->reg + PLL_BASE);
666 u64 rate = prate;
Colin Crossd8611962010-01-28 16:40:29 -0800667
668 if (c->flags & PLL_FIXED && !(val & PLL_BASE_OVERRIDE)) {
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530669 const struct clk_pll_freq_table *sel;
670 for (sel = c->u.pll.freq_table; sel->input_rate != 0; sel++) {
671 if (sel->input_rate == prate &&
672 sel->output_rate == c->u.pll.fixed_rate) {
673 c->mul = sel->n;
674 c->div = sel->m * sel->p;
675 break;
676 }
677 }
678 pr_err("Clock %s has unknown fixed frequency\n",
679 __clk_get_name(hw->clk));
680 BUG();
Colin Crossd8611962010-01-28 16:40:29 -0800681 } else if (val & PLL_BASE_BYPASS) {
Colin Cross71fc84c2010-06-07 20:49:46 -0700682 c->mul = 1;
683 c->div = 1;
Colin Crossd8611962010-01-28 16:40:29 -0800684 } else {
Colin Cross71fc84c2010-06-07 20:49:46 -0700685 c->mul = (val & PLL_BASE_DIVN_MASK) >> PLL_BASE_DIVN_SHIFT;
686 c->div = (val & PLL_BASE_DIVM_MASK) >> PLL_BASE_DIVM_SHIFT;
687 if (c->flags & PLLU)
688 c->div *= (val & PLLU_BASE_POST_DIV) ? 1 : 2;
689 else
690 c->div *= (val & PLL_BASE_DIVP_MASK) ? 2 : 1;
Colin Crossd8611962010-01-28 16:40:29 -0800691 }
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530692
693 if (c->mul != 0 && c->div != 0) {
694 rate *= c->mul;
695 rate += c->div - 1; /* round up */
696 do_div(rate, c->div);
697 }
698 return rate;
Colin Crossd8611962010-01-28 16:40:29 -0800699}
700
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530701static int tegra20_pll_clk_enable(struct clk_hw *hw)
Colin Crossd8611962010-01-28 16:40:29 -0800702{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530703 struct clk_tegra *c = to_clk_tegra(hw);
Colin Crossd8611962010-01-28 16:40:29 -0800704 u32 val;
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530705 pr_debug("%s on clock %s\n", __func__, __clk_get_name(hw->clk));
Colin Crossd8611962010-01-28 16:40:29 -0800706
707 val = clk_readl(c->reg + PLL_BASE);
708 val &= ~PLL_BASE_BYPASS;
709 val |= PLL_BASE_ENABLE;
710 clk_writel(val, c->reg + PLL_BASE);
711
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530712 tegra20_pll_clk_wait_for_lock(c);
Colin Crossd8611962010-01-28 16:40:29 -0800713
714 return 0;
715}
716
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530717static void tegra20_pll_clk_disable(struct clk_hw *hw)
Colin Crossd8611962010-01-28 16:40:29 -0800718{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530719 struct clk_tegra *c = to_clk_tegra(hw);
Colin Crossd8611962010-01-28 16:40:29 -0800720 u32 val;
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530721 pr_debug("%s on clock %s\n", __func__, __clk_get_name(hw->clk));
Colin Crossd8611962010-01-28 16:40:29 -0800722
723 val = clk_readl(c->reg);
724 val &= ~(PLL_BASE_BYPASS | PLL_BASE_ENABLE);
725 clk_writel(val, c->reg);
726}
727
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530728static int tegra20_pll_clk_set_rate(struct clk_hw *hw, unsigned long rate,
729 unsigned long parent_rate)
Colin Crossd8611962010-01-28 16:40:29 -0800730{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530731 struct clk_tegra *c = to_clk_tegra(hw);
732 unsigned long input_rate = parent_rate;
Colin Crossf1519612011-02-12 16:05:31 -0800733 const struct clk_pll_freq_table *sel;
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530734 u32 val;
Colin Crossd8611962010-01-28 16:40:29 -0800735
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530736 pr_debug("%s: %s %lu\n", __func__, __clk_get_name(hw->clk), rate);
Colin Crossd8611962010-01-28 16:40:29 -0800737
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530738 if (c->flags & PLL_FIXED) {
739 int ret = 0;
740 if (rate != c->u.pll.fixed_rate) {
741 pr_err("%s: Can not change %s fixed rate %lu to %lu\n",
742 __func__, __clk_get_name(hw->clk),
743 c->u.pll.fixed_rate, rate);
744 ret = -EINVAL;
745 }
746 return ret;
747 }
748
Colin Crossf1519612011-02-12 16:05:31 -0800749 for (sel = c->u.pll.freq_table; sel->input_rate != 0; sel++) {
Colin Crossd8611962010-01-28 16:40:29 -0800750 if (sel->input_rate == input_rate && sel->output_rate == rate) {
Colin Cross71fc84c2010-06-07 20:49:46 -0700751 c->mul = sel->n;
752 c->div = sel->m * sel->p;
Colin Crossd8611962010-01-28 16:40:29 -0800753
754 val = clk_readl(c->reg + PLL_BASE);
755 if (c->flags & PLL_FIXED)
756 val |= PLL_BASE_OVERRIDE;
757 val &= ~(PLL_BASE_DIVP_MASK | PLL_BASE_DIVN_MASK |
758 PLL_BASE_DIVM_MASK);
Colin Cross71fc84c2010-06-07 20:49:46 -0700759 val |= (sel->m << PLL_BASE_DIVM_SHIFT) |
760 (sel->n << PLL_BASE_DIVN_SHIFT);
761 BUG_ON(sel->p < 1 || sel->p > 2);
762 if (c->flags & PLLU) {
763 if (sel->p == 1)
764 val |= PLLU_BASE_POST_DIV;
765 } else {
766 if (sel->p == 2)
767 val |= 1 << PLL_BASE_DIVP_SHIFT;
768 }
Colin Crossd8611962010-01-28 16:40:29 -0800769 clk_writel(val, c->reg + PLL_BASE);
770
771 if (c->flags & PLL_HAS_CPCON) {
Colin Cross71fc84c2010-06-07 20:49:46 -0700772 val = clk_readl(c->reg + PLL_MISC(c));
773 val &= ~PLL_MISC_CPCON_MASK;
774 val |= sel->cpcon << PLL_MISC_CPCON_SHIFT;
Colin Crossd8611962010-01-28 16:40:29 -0800775 clk_writel(val, c->reg + PLL_MISC(c));
776 }
777
778 if (c->state == ON)
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530779 tegra20_pll_clk_enable(hw);
Colin Crossd8611962010-01-28 16:40:29 -0800780 return 0;
781 }
782 }
783 return -EINVAL;
784}
785
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530786static long tegra20_pll_clk_round_rate(struct clk_hw *hw, unsigned long rate,
787 unsigned long *prate)
788{
789 struct clk_tegra *c = to_clk_tegra(hw);
790 const struct clk_pll_freq_table *sel;
791 unsigned long input_rate = *prate;
792 unsigned long output_rate = *prate;
793 int mul;
794 int div;
795
796 if (c->flags & PLL_FIXED)
797 return c->u.pll.fixed_rate;
798
799 for (sel = c->u.pll.freq_table; sel->input_rate != 0; sel++)
800 if (sel->input_rate == input_rate && sel->output_rate == rate) {
801 mul = sel->n;
802 div = sel->m * sel->p;
803 break;
804 }
805
806 if (sel->input_rate == 0)
807 return -EINVAL;
808
809 output_rate *= mul;
810 output_rate += div - 1; /* round up */
811 do_div(output_rate, div);
812
813 return output_rate;
814}
815
Prashant Gaikwad86edb872012-08-06 11:57:40 +0530816struct clk_ops tegra_pll_ops = {
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530817 .is_enabled = tegra20_pll_clk_is_enabled,
818 .enable = tegra20_pll_clk_enable,
819 .disable = tegra20_pll_clk_disable,
820 .set_rate = tegra20_pll_clk_set_rate,
821 .recalc_rate = tegra20_pll_clk_recalc_rate,
822 .round_rate = tegra20_pll_clk_round_rate,
Colin Cross71fc84c2010-06-07 20:49:46 -0700823};
824
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530825static void tegra20_pllx_clk_init(struct clk_hw *hw)
Colin Cross71fc84c2010-06-07 20:49:46 -0700826{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530827 struct clk_tegra *c = to_clk_tegra(hw);
Colin Cross71fc84c2010-06-07 20:49:46 -0700828
Olof Johansson9a1086d2011-10-13 00:31:20 -0700829 if (tegra_sku_id == 7)
Colin Cross71fc84c2010-06-07 20:49:46 -0700830 c->max_rate = 750000000;
831}
832
Prashant Gaikwad86edb872012-08-06 11:57:40 +0530833struct clk_ops tegra_pllx_ops = {
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530834 .init = tegra20_pllx_clk_init,
835 .is_enabled = tegra20_pll_clk_is_enabled,
836 .enable = tegra20_pll_clk_enable,
837 .disable = tegra20_pll_clk_disable,
838 .set_rate = tegra20_pll_clk_set_rate,
839 .recalc_rate = tegra20_pll_clk_recalc_rate,
840 .round_rate = tegra20_pll_clk_round_rate,
Colin Crossd8611962010-01-28 16:40:29 -0800841};
842
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530843static int tegra20_plle_clk_enable(struct clk_hw *hw)
Mike Rapoport8d685bc2010-09-27 11:26:32 +0200844{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530845 struct clk_tegra *c = to_clk_tegra(hw);
Mike Rapoport8d685bc2010-09-27 11:26:32 +0200846 u32 val;
847
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530848 pr_debug("%s on clock %s\n", __func__, __clk_get_name(hw->clk));
Mike Rapoport8d685bc2010-09-27 11:26:32 +0200849
850 mdelay(1);
851
852 val = clk_readl(c->reg + PLL_BASE);
853 if (!(val & PLLE_MISC_READY))
854 return -EBUSY;
855
856 val = clk_readl(c->reg + PLL_BASE);
857 val |= PLL_BASE_ENABLE | PLL_BASE_BYPASS;
858 clk_writel(val, c->reg + PLL_BASE);
859
860 return 0;
861}
862
Prashant Gaikwad86edb872012-08-06 11:57:40 +0530863struct clk_ops tegra_plle_ops = {
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530864 .is_enabled = tegra20_pll_clk_is_enabled,
865 .enable = tegra20_plle_clk_enable,
866 .set_rate = tegra20_pll_clk_set_rate,
867 .recalc_rate = tegra20_pll_clk_recalc_rate,
868 .round_rate = tegra20_pll_clk_round_rate,
Mike Rapoport8d685bc2010-09-27 11:26:32 +0200869};
870
Colin Crossd8611962010-01-28 16:40:29 -0800871/* Clock divider ops */
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530872static int tegra20_pll_div_clk_is_enabled(struct clk_hw *hw)
Colin Crossd8611962010-01-28 16:40:29 -0800873{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530874 struct clk_tegra *c = to_clk_tegra(hw);
Colin Crossd8611962010-01-28 16:40:29 -0800875 u32 val = clk_readl(c->reg);
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530876
Colin Crossd8611962010-01-28 16:40:29 -0800877 val >>= c->reg_shift;
878 c->state = (val & PLL_OUT_CLKEN) ? ON : OFF;
879 if (!(val & PLL_OUT_RESET_DISABLE))
880 c->state = OFF;
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530881 return c->state;
882}
883
884static unsigned long tegra20_pll_div_clk_recalc_rate(struct clk_hw *hw,
885 unsigned long prate)
886{
887 struct clk_tegra *c = to_clk_tegra(hw);
888 u64 rate = prate;
889 u32 val = clk_readl(c->reg);
890 u32 divu71;
891
892 val >>= c->reg_shift;
Colin Crossd8611962010-01-28 16:40:29 -0800893
894 if (c->flags & DIV_U71) {
895 divu71 = (val & PLL_OUT_RATIO_MASK) >> PLL_OUT_RATIO_SHIFT;
896 c->div = (divu71 + 2);
897 c->mul = 2;
898 } else if (c->flags & DIV_2) {
899 c->div = 2;
900 c->mul = 1;
901 } else {
902 c->div = 1;
903 c->mul = 1;
904 }
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530905
906 rate *= c->mul;
907 rate += c->div - 1; /* round up */
908 do_div(rate, c->div);
909
910 return rate;
Colin Crossd8611962010-01-28 16:40:29 -0800911}
912
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530913static int tegra20_pll_div_clk_enable(struct clk_hw *hw)
Colin Crossd8611962010-01-28 16:40:29 -0800914{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530915 struct clk_tegra *c = to_clk_tegra(hw);
Colin Cross4729fd72011-02-12 16:43:05 -0800916 unsigned long flags;
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530917 u32 new_val;
918 u32 val;
Colin Crossd8611962010-01-28 16:40:29 -0800919
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530920 pr_debug("%s: %s\n", __func__, __clk_get_name(hw->clk));
921
Colin Crossd8611962010-01-28 16:40:29 -0800922 if (c->flags & DIV_U71) {
Colin Cross4729fd72011-02-12 16:43:05 -0800923 spin_lock_irqsave(&clock_register_lock, flags);
Colin Crossd8611962010-01-28 16:40:29 -0800924 val = clk_readl(c->reg);
925 new_val = val >> c->reg_shift;
926 new_val &= 0xFFFF;
927
928 new_val |= PLL_OUT_CLKEN | PLL_OUT_RESET_DISABLE;
929
930 val &= ~(0xFFFF << c->reg_shift);
931 val |= new_val << c->reg_shift;
932 clk_writel(val, c->reg);
Colin Cross4729fd72011-02-12 16:43:05 -0800933 spin_unlock_irqrestore(&clock_register_lock, flags);
Colin Crossd8611962010-01-28 16:40:29 -0800934 return 0;
935 } else if (c->flags & DIV_2) {
936 BUG_ON(!(c->flags & PLLD));
Colin Cross4729fd72011-02-12 16:43:05 -0800937 spin_lock_irqsave(&clock_register_lock, flags);
Colin Crossd8611962010-01-28 16:40:29 -0800938 val = clk_readl(c->reg);
939 val &= ~PLLD_MISC_DIV_RST;
940 clk_writel(val, c->reg);
Colin Cross4729fd72011-02-12 16:43:05 -0800941 spin_unlock_irqrestore(&clock_register_lock, flags);
Colin Crossd8611962010-01-28 16:40:29 -0800942 return 0;
943 }
944 return -EINVAL;
945}
946
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530947static void tegra20_pll_div_clk_disable(struct clk_hw *hw)
Colin Crossd8611962010-01-28 16:40:29 -0800948{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530949 struct clk_tegra *c = to_clk_tegra(hw);
Colin Cross4729fd72011-02-12 16:43:05 -0800950 unsigned long flags;
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530951 u32 new_val;
952 u32 val;
Colin Crossd8611962010-01-28 16:40:29 -0800953
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530954 pr_debug("%s: %s\n", __func__, __clk_get_name(hw->clk));
955
Colin Crossd8611962010-01-28 16:40:29 -0800956 if (c->flags & DIV_U71) {
Colin Cross4729fd72011-02-12 16:43:05 -0800957 spin_lock_irqsave(&clock_register_lock, flags);
Colin Crossd8611962010-01-28 16:40:29 -0800958 val = clk_readl(c->reg);
959 new_val = val >> c->reg_shift;
960 new_val &= 0xFFFF;
961
962 new_val &= ~(PLL_OUT_CLKEN | PLL_OUT_RESET_DISABLE);
963
964 val &= ~(0xFFFF << c->reg_shift);
965 val |= new_val << c->reg_shift;
966 clk_writel(val, c->reg);
Colin Cross4729fd72011-02-12 16:43:05 -0800967 spin_unlock_irqrestore(&clock_register_lock, flags);
Colin Crossd8611962010-01-28 16:40:29 -0800968 } else if (c->flags & DIV_2) {
969 BUG_ON(!(c->flags & PLLD));
Colin Cross4729fd72011-02-12 16:43:05 -0800970 spin_lock_irqsave(&clock_register_lock, flags);
Colin Crossd8611962010-01-28 16:40:29 -0800971 val = clk_readl(c->reg);
972 val |= PLLD_MISC_DIV_RST;
973 clk_writel(val, c->reg);
Colin Cross4729fd72011-02-12 16:43:05 -0800974 spin_unlock_irqrestore(&clock_register_lock, flags);
Colin Crossd8611962010-01-28 16:40:29 -0800975 }
976}
977
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530978static int tegra20_pll_div_clk_set_rate(struct clk_hw *hw, unsigned long rate,
979 unsigned long parent_rate)
Colin Crossd8611962010-01-28 16:40:29 -0800980{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530981 struct clk_tegra *c = to_clk_tegra(hw);
Colin Cross4729fd72011-02-12 16:43:05 -0800982 unsigned long flags;
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530983 int divider_u71;
984 u32 new_val;
985 u32 val;
Colin Cross4729fd72011-02-12 16:43:05 -0800986
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +0530987 pr_debug("%s: %s %lu\n", __func__, __clk_get_name(hw->clk), rate);
988
Colin Crossd8611962010-01-28 16:40:29 -0800989 if (c->flags & DIV_U71) {
Colin Cross4729fd72011-02-12 16:43:05 -0800990 divider_u71 = clk_div71_get_divider(parent_rate, rate);
Colin Crossd8611962010-01-28 16:40:29 -0800991 if (divider_u71 >= 0) {
Colin Cross4729fd72011-02-12 16:43:05 -0800992 spin_lock_irqsave(&clock_register_lock, flags);
Colin Crossd8611962010-01-28 16:40:29 -0800993 val = clk_readl(c->reg);
994 new_val = val >> c->reg_shift;
995 new_val &= 0xFFFF;
996 if (c->flags & DIV_U71_FIXED)
997 new_val |= PLL_OUT_OVERRIDE;
998 new_val &= ~PLL_OUT_RATIO_MASK;
999 new_val |= divider_u71 << PLL_OUT_RATIO_SHIFT;
1000
1001 val &= ~(0xFFFF << c->reg_shift);
1002 val |= new_val << c->reg_shift;
1003 clk_writel(val, c->reg);
1004 c->div = divider_u71 + 2;
1005 c->mul = 2;
Colin Cross4729fd72011-02-12 16:43:05 -08001006 spin_unlock_irqrestore(&clock_register_lock, flags);
Colin Crossd8611962010-01-28 16:40:29 -08001007 return 0;
1008 }
1009 } else if (c->flags & DIV_2) {
Colin Cross4729fd72011-02-12 16:43:05 -08001010 if (parent_rate == rate * 2)
Colin Crossd8611962010-01-28 16:40:29 -08001011 return 0;
Colin Crossd8611962010-01-28 16:40:29 -08001012 }
1013 return -EINVAL;
1014}
1015
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301016static long tegra20_pll_div_clk_round_rate(struct clk_hw *hw, unsigned long rate,
1017 unsigned long *prate)
Colin Cross71fc84c2010-06-07 20:49:46 -07001018{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301019 struct clk_tegra *c = to_clk_tegra(hw);
1020 unsigned long parent_rate = *prate;
Colin Cross71fc84c2010-06-07 20:49:46 -07001021 int divider;
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301022
1023 pr_debug("%s: %s %lu\n", __func__, __clk_get_name(hw->clk), rate);
Colin Cross71fc84c2010-06-07 20:49:46 -07001024
1025 if (c->flags & DIV_U71) {
Colin Cross4729fd72011-02-12 16:43:05 -08001026 divider = clk_div71_get_divider(parent_rate, rate);
Colin Cross71fc84c2010-06-07 20:49:46 -07001027 if (divider < 0)
1028 return divider;
Colin Cross421186e2011-02-12 18:21:47 -08001029 return DIV_ROUND_UP(parent_rate * 2, divider + 2);
Colin Cross71fc84c2010-06-07 20:49:46 -07001030 } else if (c->flags & DIV_2) {
Colin Cross421186e2011-02-12 18:21:47 -08001031 return DIV_ROUND_UP(parent_rate, 2);
Colin Cross71fc84c2010-06-07 20:49:46 -07001032 }
1033 return -EINVAL;
1034}
Colin Crossd8611962010-01-28 16:40:29 -08001035
Prashant Gaikwad86edb872012-08-06 11:57:40 +05301036struct clk_ops tegra_pll_div_ops = {
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301037 .is_enabled = tegra20_pll_div_clk_is_enabled,
1038 .enable = tegra20_pll_div_clk_enable,
1039 .disable = tegra20_pll_div_clk_disable,
1040 .set_rate = tegra20_pll_div_clk_set_rate,
1041 .round_rate = tegra20_pll_div_clk_round_rate,
1042 .recalc_rate = tegra20_pll_div_clk_recalc_rate,
Colin Crossd8611962010-01-28 16:40:29 -08001043};
1044
1045/* Periph clk ops */
1046
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301047static int tegra20_periph_clk_is_enabled(struct clk_hw *hw)
Colin Crossd8611962010-01-28 16:40:29 -08001048{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301049 struct clk_tegra *c = to_clk_tegra(hw);
Colin Crossd8611962010-01-28 16:40:29 -08001050
1051 c->state = ON;
Colin Cross1be3d052011-02-21 16:44:07 -08001052
1053 if (!c->u.periph.clk_num)
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301054 goto out;
Colin Cross1be3d052011-02-21 16:44:07 -08001055
Colin Crossd8611962010-01-28 16:40:29 -08001056 if (!(clk_readl(CLK_OUT_ENB + PERIPH_CLK_TO_ENB_REG(c)) &
1057 PERIPH_CLK_TO_ENB_BIT(c)))
1058 c->state = OFF;
Colin Cross1be3d052011-02-21 16:44:07 -08001059
Colin Crossd8611962010-01-28 16:40:29 -08001060 if (!(c->flags & PERIPH_NO_RESET))
1061 if (clk_readl(RST_DEVICES + PERIPH_CLK_TO_ENB_REG(c)) &
1062 PERIPH_CLK_TO_ENB_BIT(c))
1063 c->state = OFF;
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301064
1065out:
1066 return c->state;
Colin Crossd8611962010-01-28 16:40:29 -08001067}
1068
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301069static int tegra20_periph_clk_enable(struct clk_hw *hw)
Colin Crossd8611962010-01-28 16:40:29 -08001070{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301071 struct clk_tegra *c = to_clk_tegra(hw);
Colin Cross78f379b2010-10-20 19:19:58 -07001072 unsigned long flags;
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301073 u32 val;
1074
1075 pr_debug("%s on clock %s\n", __func__, __clk_get_name(hw->clk));
Colin Crossd8611962010-01-28 16:40:29 -08001076
Colin Cross1be3d052011-02-21 16:44:07 -08001077 if (!c->u.periph.clk_num)
1078 return 0;
1079
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301080 tegra_periph_clk_enable_refcount[c->u.periph.clk_num]++;
1081 if (tegra_periph_clk_enable_refcount[c->u.periph.clk_num] > 1)
1082 return 0;
1083
Colin Cross78f379b2010-10-20 19:19:58 -07001084 spin_lock_irqsave(&clock_register_lock, flags);
1085
Colin Crossd8611962010-01-28 16:40:29 -08001086 clk_writel(PERIPH_CLK_TO_ENB_BIT(c),
1087 CLK_OUT_ENB_SET + PERIPH_CLK_TO_ENB_SET_REG(c));
1088 if (!(c->flags & PERIPH_NO_RESET) && !(c->flags & PERIPH_MANUAL_RESET))
1089 clk_writel(PERIPH_CLK_TO_ENB_BIT(c),
1090 RST_DEVICES_CLR + PERIPH_CLK_TO_ENB_SET_REG(c));
1091 if (c->flags & PERIPH_EMC_ENB) {
1092 /* The EMC peripheral clock has 2 extra enable bits */
1093 /* FIXME: Do they need to be disabled? */
1094 val = clk_readl(c->reg);
1095 val |= 0x3 << 24;
1096 clk_writel(val, c->reg);
1097 }
Colin Cross78f379b2010-10-20 19:19:58 -07001098
Colin Cross78f379b2010-10-20 19:19:58 -07001099 spin_unlock_irqrestore(&clock_register_lock, flags);
1100
Colin Crossd8611962010-01-28 16:40:29 -08001101 return 0;
1102}
1103
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301104static void tegra20_periph_clk_disable(struct clk_hw *hw)
Colin Crossd8611962010-01-28 16:40:29 -08001105{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301106 struct clk_tegra *c = to_clk_tegra(hw);
Colin Cross78f379b2010-10-20 19:19:58 -07001107 unsigned long flags;
1108
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301109 pr_debug("%s on clock %s\n", __func__, __clk_get_name(hw->clk));
Colin Crossd8611962010-01-28 16:40:29 -08001110
Colin Cross1be3d052011-02-21 16:44:07 -08001111 if (!c->u.periph.clk_num)
1112 return;
1113
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301114 tegra_periph_clk_enable_refcount[c->u.periph.clk_num]--;
1115
1116 if (tegra_periph_clk_enable_refcount[c->u.periph.clk_num] > 0)
1117 return;
1118
Colin Cross78f379b2010-10-20 19:19:58 -07001119 spin_lock_irqsave(&clock_register_lock, flags);
1120
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301121 clk_writel(PERIPH_CLK_TO_ENB_BIT(c),
1122 CLK_OUT_ENB_CLR + PERIPH_CLK_TO_ENB_SET_REG(c));
Colin Cross78f379b2010-10-20 19:19:58 -07001123
1124 spin_unlock_irqrestore(&clock_register_lock, flags);
Colin Crossd8611962010-01-28 16:40:29 -08001125}
1126
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301127void tegra2_periph_clk_reset(struct clk_hw *hw, bool assert)
Colin Crossd8611962010-01-28 16:40:29 -08001128{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301129 struct clk_tegra *c = to_clk_tegra(hw);
Dima Zavin2b84cb4f2010-09-02 19:11:11 -07001130 unsigned long base = assert ? RST_DEVICES_SET : RST_DEVICES_CLR;
1131
1132 pr_debug("%s %s on clock %s\n", __func__,
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301133 assert ? "assert" : "deassert", __clk_get_name(hw->clk));
Colin Cross1be3d052011-02-21 16:44:07 -08001134
1135 BUG_ON(!c->u.periph.clk_num);
1136
Colin Crossd8611962010-01-28 16:40:29 -08001137 if (!(c->flags & PERIPH_NO_RESET))
1138 clk_writel(PERIPH_CLK_TO_ENB_BIT(c),
Dima Zavin2b84cb4f2010-09-02 19:11:11 -07001139 base + PERIPH_CLK_TO_ENB_SET_REG(c));
Colin Crossd8611962010-01-28 16:40:29 -08001140}
1141
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301142static int tegra20_periph_clk_set_parent(struct clk_hw *hw, u8 index)
Colin Crossd8611962010-01-28 16:40:29 -08001143{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301144 struct clk_tegra *c = to_clk_tegra(hw);
Colin Crossd8611962010-01-28 16:40:29 -08001145 u32 val;
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301146 u32 mask;
1147 u32 shift;
Simon Quebb1dccf2011-12-16 20:11:22 +01001148
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301149 pr_debug("%s: %s %d\n", __func__, __clk_get_name(hw->clk), index);
Simon Quebb1dccf2011-12-16 20:11:22 +01001150
1151 if (c->flags & MUX_PWM) {
1152 shift = PERIPH_CLK_SOURCE_PWM_SHIFT;
1153 mask = PERIPH_CLK_SOURCE_PWM_MASK;
1154 } else {
1155 shift = PERIPH_CLK_SOURCE_SHIFT;
1156 mask = PERIPH_CLK_SOURCE_MASK;
1157 }
1158
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301159 val = clk_readl(c->reg);
1160 val &= ~mask;
1161 val |= (index) << shift;
Colin Cross71fc84c2010-06-07 20:49:46 -07001162
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301163 clk_writel(val, c->reg);
Colin Cross71fc84c2010-06-07 20:49:46 -07001164
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301165 return 0;
Colin Crossd8611962010-01-28 16:40:29 -08001166}
1167
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301168static u8 tegra20_periph_clk_get_parent(struct clk_hw *hw)
Colin Crossd8611962010-01-28 16:40:29 -08001169{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301170 struct clk_tegra *c = to_clk_tegra(hw);
1171 u32 val = clk_readl(c->reg);
1172 u32 mask;
1173 u32 shift;
1174
1175 if (c->flags & MUX_PWM) {
1176 shift = PERIPH_CLK_SOURCE_PWM_SHIFT;
1177 mask = PERIPH_CLK_SOURCE_PWM_MASK;
1178 } else {
1179 shift = PERIPH_CLK_SOURCE_SHIFT;
1180 mask = PERIPH_CLK_SOURCE_MASK;
1181 }
1182
1183 if (c->flags & MUX)
1184 return (val & mask) >> shift;
1185 else
1186 return 0;
1187}
1188
1189static unsigned long tegra20_periph_clk_recalc_rate(struct clk_hw *hw,
1190 unsigned long prate)
1191{
1192 struct clk_tegra *c = to_clk_tegra(hw);
1193 unsigned long rate = prate;
1194 u32 val = clk_readl(c->reg);
1195
1196 if (c->flags & DIV_U71) {
1197 u32 divu71 = val & PERIPH_CLK_SOURCE_DIVU71_MASK;
1198 c->div = divu71 + 2;
1199 c->mul = 2;
1200 } else if (c->flags & DIV_U16) {
1201 u32 divu16 = val & PERIPH_CLK_SOURCE_DIVU16_MASK;
1202 c->div = divu16 + 1;
1203 c->mul = 1;
1204 } else {
1205 c->div = 1;
1206 c->mul = 1;
1207 return rate;
1208 }
1209
1210 if (c->mul != 0 && c->div != 0) {
1211 rate *= c->mul;
1212 rate += c->div - 1; /* round up */
1213 do_div(rate, c->div);
1214 }
1215
1216 return rate;
1217}
1218
1219static int tegra20_periph_clk_set_rate(struct clk_hw *hw, unsigned long rate,
1220 unsigned long parent_rate)
1221{
1222 struct clk_tegra *c = to_clk_tegra(hw);
Colin Crossd8611962010-01-28 16:40:29 -08001223 u32 val;
Colin Cross71fc84c2010-06-07 20:49:46 -07001224 int divider;
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301225
1226 val = clk_readl(c->reg);
Colin Cross4729fd72011-02-12 16:43:05 -08001227
Colin Crossd8611962010-01-28 16:40:29 -08001228 if (c->flags & DIV_U71) {
Colin Cross4729fd72011-02-12 16:43:05 -08001229 divider = clk_div71_get_divider(parent_rate, rate);
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301230
Colin Cross71fc84c2010-06-07 20:49:46 -07001231 if (divider >= 0) {
Colin Crossd8611962010-01-28 16:40:29 -08001232 val = clk_readl(c->reg);
Colin Cross71fc84c2010-06-07 20:49:46 -07001233 val &= ~PERIPH_CLK_SOURCE_DIVU71_MASK;
1234 val |= divider;
Colin Crossd8611962010-01-28 16:40:29 -08001235 clk_writel(val, c->reg);
Colin Cross71fc84c2010-06-07 20:49:46 -07001236 c->div = divider + 2;
Colin Crossd8611962010-01-28 16:40:29 -08001237 c->mul = 2;
Colin Crossd8611962010-01-28 16:40:29 -08001238 return 0;
1239 }
Colin Cross71fc84c2010-06-07 20:49:46 -07001240 } else if (c->flags & DIV_U16) {
Colin Cross4729fd72011-02-12 16:43:05 -08001241 divider = clk_div16_get_divider(parent_rate, rate);
Colin Cross71fc84c2010-06-07 20:49:46 -07001242 if (divider >= 0) {
1243 val = clk_readl(c->reg);
1244 val &= ~PERIPH_CLK_SOURCE_DIVU16_MASK;
1245 val |= divider;
1246 clk_writel(val, c->reg);
1247 c->div = divider + 1;
1248 c->mul = 1;
1249 return 0;
1250 }
Colin Cross4729fd72011-02-12 16:43:05 -08001251 } else if (parent_rate <= rate) {
Colin Cross71fc84c2010-06-07 20:49:46 -07001252 c->div = 1;
1253 c->mul = 1;
1254 return 0;
1255 }
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301256
Colin Cross71fc84c2010-06-07 20:49:46 -07001257 return -EINVAL;
1258}
1259
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301260static long tegra20_periph_clk_round_rate(struct clk_hw *hw,
1261 unsigned long rate, unsigned long *prate)
Colin Cross71fc84c2010-06-07 20:49:46 -07001262{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301263 struct clk_tegra *c = to_clk_tegra(hw);
1264 unsigned long parent_rate = __clk_get_rate(__clk_get_parent(hw->clk));
Colin Cross71fc84c2010-06-07 20:49:46 -07001265 int divider;
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301266
1267 pr_debug("%s: %s %lu\n", __func__, __clk_get_name(hw->clk), rate);
1268
1269 if (prate)
1270 parent_rate = *prate;
Colin Cross71fc84c2010-06-07 20:49:46 -07001271
1272 if (c->flags & DIV_U71) {
Colin Cross4729fd72011-02-12 16:43:05 -08001273 divider = clk_div71_get_divider(parent_rate, rate);
Colin Cross71fc84c2010-06-07 20:49:46 -07001274 if (divider < 0)
1275 return divider;
1276
Colin Cross421186e2011-02-12 18:21:47 -08001277 return DIV_ROUND_UP(parent_rate * 2, divider + 2);
Colin Cross71fc84c2010-06-07 20:49:46 -07001278 } else if (c->flags & DIV_U16) {
Colin Cross4729fd72011-02-12 16:43:05 -08001279 divider = clk_div16_get_divider(parent_rate, rate);
Colin Cross71fc84c2010-06-07 20:49:46 -07001280 if (divider < 0)
1281 return divider;
Colin Cross421186e2011-02-12 18:21:47 -08001282 return DIV_ROUND_UP(parent_rate, divider + 1);
Colin Crossd8611962010-01-28 16:40:29 -08001283 }
1284 return -EINVAL;
1285}
1286
Prashant Gaikwad86edb872012-08-06 11:57:40 +05301287struct clk_ops tegra_periph_clk_ops = {
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301288 .is_enabled = tegra20_periph_clk_is_enabled,
1289 .enable = tegra20_periph_clk_enable,
1290 .disable = tegra20_periph_clk_disable,
1291 .set_parent = tegra20_periph_clk_set_parent,
1292 .get_parent = tegra20_periph_clk_get_parent,
1293 .set_rate = tegra20_periph_clk_set_rate,
1294 .round_rate = tegra20_periph_clk_round_rate,
1295 .recalc_rate = tegra20_periph_clk_recalc_rate,
Colin Crossd8611962010-01-28 16:40:29 -08001296};
1297
Colin Cross6d296822010-11-22 18:37:54 -08001298/* External memory controller clock ops */
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301299static void tegra20_emc_clk_init(struct clk_hw *hw)
Colin Cross6d296822010-11-22 18:37:54 -08001300{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301301 struct clk_tegra *c = to_clk_tegra(hw);
1302 c->max_rate = __clk_get_rate(hw->clk);
Colin Cross6d296822010-11-22 18:37:54 -08001303}
1304
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301305static long tegra20_emc_clk_round_rate(struct clk_hw *hw, unsigned long rate,
1306 unsigned long *prate)
Colin Cross6d296822010-11-22 18:37:54 -08001307{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301308 struct clk_tegra *c = to_clk_tegra(hw);
Stephen Warrene186ad72012-02-06 17:09:15 -08001309 long emc_rate;
1310 long clk_rate;
Colin Cross6d296822010-11-22 18:37:54 -08001311
Stephen Warrene186ad72012-02-06 17:09:15 -08001312 /*
1313 * The slowest entry in the EMC clock table that is at least as
1314 * fast as rate.
1315 */
1316 emc_rate = tegra_emc_round_rate(rate);
1317 if (emc_rate < 0)
Colin Cross6d296822010-11-22 18:37:54 -08001318 return c->max_rate;
1319
Stephen Warrene186ad72012-02-06 17:09:15 -08001320 /*
1321 * The fastest rate the PLL will generate that is at most the
1322 * requested rate.
1323 */
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301324 clk_rate = tegra20_periph_clk_round_rate(hw, emc_rate, NULL);
Colin Cross6d296822010-11-22 18:37:54 -08001325
Stephen Warrene186ad72012-02-06 17:09:15 -08001326 /*
1327 * If this fails, and emc_rate > clk_rate, it's because the maximum
1328 * rate in the EMC tables is larger than the maximum rate of the EMC
1329 * clock. The EMC clock's max rate is the rate it was running when the
1330 * kernel booted. Such a mismatch is probably due to using the wrong
1331 * BCT, i.e. using a Tegra20 BCT with an EMC table written for Tegra25.
1332 */
1333 WARN_ONCE(emc_rate != clk_rate,
1334 "emc_rate %ld != clk_rate %ld",
1335 emc_rate, clk_rate);
1336
1337 return emc_rate;
Colin Cross6d296822010-11-22 18:37:54 -08001338}
1339
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301340static int tegra20_emc_clk_set_rate(struct clk_hw *hw, unsigned long rate,
1341 unsigned long parent_rate)
Colin Cross6d296822010-11-22 18:37:54 -08001342{
1343 int ret;
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301344
Colin Cross6d296822010-11-22 18:37:54 -08001345 /*
1346 * The Tegra2 memory controller has an interlock with the clock
1347 * block that allows memory shadowed registers to be updated,
1348 * and then transfer them to the main registers at the same
1349 * time as the clock update without glitches.
1350 */
1351 ret = tegra_emc_set_rate(rate);
1352 if (ret < 0)
1353 return ret;
1354
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301355 ret = tegra20_periph_clk_set_rate(hw, rate, parent_rate);
Colin Cross6d296822010-11-22 18:37:54 -08001356 udelay(1);
1357
1358 return ret;
1359}
1360
Prashant Gaikwad86edb872012-08-06 11:57:40 +05301361struct clk_ops tegra_emc_clk_ops = {
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301362 .init = tegra20_emc_clk_init,
1363 .is_enabled = tegra20_periph_clk_is_enabled,
1364 .enable = tegra20_periph_clk_enable,
1365 .disable = tegra20_periph_clk_disable,
1366 .set_parent = tegra20_periph_clk_set_parent,
1367 .get_parent = tegra20_periph_clk_get_parent,
1368 .set_rate = tegra20_emc_clk_set_rate,
1369 .round_rate = tegra20_emc_clk_round_rate,
1370 .recalc_rate = tegra20_periph_clk_recalc_rate,
Colin Cross6d296822010-11-22 18:37:54 -08001371};
1372
Colin Crossd8611962010-01-28 16:40:29 -08001373/* Clock doubler ops */
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301374static int tegra20_clk_double_is_enabled(struct clk_hw *hw)
Colin Crossd8611962010-01-28 16:40:29 -08001375{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301376 struct clk_tegra *c = to_clk_tegra(hw);
1377
Colin Crossd8611962010-01-28 16:40:29 -08001378 c->state = ON;
Colin Cross1be3d052011-02-21 16:44:07 -08001379
1380 if (!c->u.periph.clk_num)
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301381 goto out;
Colin Cross1be3d052011-02-21 16:44:07 -08001382
Colin Crossd8611962010-01-28 16:40:29 -08001383 if (!(clk_readl(CLK_OUT_ENB + PERIPH_CLK_TO_ENB_REG(c)) &
1384 PERIPH_CLK_TO_ENB_BIT(c)))
1385 c->state = OFF;
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301386
1387out:
1388 return c->state;
Colin Crossd8611962010-01-28 16:40:29 -08001389};
1390
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301391static unsigned long tegra20_clk_double_recalc_rate(struct clk_hw *hw,
1392 unsigned long prate)
Colin Cross71fc84c2010-06-07 20:49:46 -07001393{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301394 struct clk_tegra *c = to_clk_tegra(hw);
1395 u64 rate = prate;
1396
Colin Cross71fc84c2010-06-07 20:49:46 -07001397 c->mul = 2;
1398 c->div = 1;
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301399
1400 rate *= c->mul;
1401 rate += c->div - 1; /* round up */
1402 do_div(rate, c->div);
1403
1404 return rate;
1405}
1406
1407static long tegra20_clk_double_round_rate(struct clk_hw *hw, unsigned long rate,
1408 unsigned long *prate)
1409{
1410 unsigned long output_rate = *prate;
1411
1412 do_div(output_rate, 2);
1413 return output_rate;
1414}
1415
1416static int tegra20_clk_double_set_rate(struct clk_hw *hw, unsigned long rate,
1417 unsigned long parent_rate)
1418{
1419 if (rate != 2 * parent_rate)
1420 return -EINVAL;
Colin Cross71fc84c2010-06-07 20:49:46 -07001421 return 0;
1422}
1423
Prashant Gaikwad86edb872012-08-06 11:57:40 +05301424struct clk_ops tegra_clk_double_ops = {
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301425 .is_enabled = tegra20_clk_double_is_enabled,
1426 .enable = tegra20_periph_clk_enable,
1427 .disable = tegra20_periph_clk_disable,
1428 .set_rate = tegra20_clk_double_set_rate,
1429 .recalc_rate = tegra20_clk_double_recalc_rate,
1430 .round_rate = tegra20_clk_double_round_rate,
Colin Cross71fc84c2010-06-07 20:49:46 -07001431};
1432
Colin Crosscea62c82010-10-04 11:49:26 -07001433/* Audio sync clock ops */
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301434static int tegra20_audio_sync_clk_is_enabled(struct clk_hw *hw)
Colin Cross71fc84c2010-06-07 20:49:46 -07001435{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301436 struct clk_tegra *c = to_clk_tegra(hw);
Colin Cross71fc84c2010-06-07 20:49:46 -07001437 u32 val = clk_readl(c->reg);
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301438
Colin Cross71fc84c2010-06-07 20:49:46 -07001439 c->state = (val & (1<<4)) ? OFF : ON;
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301440 return c->state;
Colin Cross71fc84c2010-06-07 20:49:46 -07001441}
1442
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301443static int tegra20_audio_sync_clk_enable(struct clk_hw *hw)
Colin Cross71fc84c2010-06-07 20:49:46 -07001444{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301445 struct clk_tegra *c = to_clk_tegra(hw);
1446
Colin Cross71fc84c2010-06-07 20:49:46 -07001447 clk_writel(0, c->reg);
1448 return 0;
1449}
1450
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301451static void tegra20_audio_sync_clk_disable(struct clk_hw *hw)
Colin Cross71fc84c2010-06-07 20:49:46 -07001452{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301453 struct clk_tegra *c = to_clk_tegra(hw);
Colin Cross71fc84c2010-06-07 20:49:46 -07001454 clk_writel(1, c->reg);
1455}
1456
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301457static u8 tegra20_audio_sync_clk_get_parent(struct clk_hw *hw)
Colin Cross71fc84c2010-06-07 20:49:46 -07001458{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301459 struct clk_tegra *c = to_clk_tegra(hw);
1460 u32 val = clk_readl(c->reg);
1461 int source;
1462
1463 source = val & 0xf;
1464 return source;
1465}
1466
1467static int tegra20_audio_sync_clk_set_parent(struct clk_hw *hw, u8 index)
1468{
1469 struct clk_tegra *c = to_clk_tegra(hw);
Colin Cross71fc84c2010-06-07 20:49:46 -07001470 u32 val;
Colin Cross71fc84c2010-06-07 20:49:46 -07001471
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301472 val = clk_readl(c->reg);
1473 val &= ~0xf;
1474 val |= index;
Colin Cross71fc84c2010-06-07 20:49:46 -07001475
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301476 clk_writel(val, c->reg);
Colin Cross71fc84c2010-06-07 20:49:46 -07001477
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301478 return 0;
Colin Cross71fc84c2010-06-07 20:49:46 -07001479}
1480
Prashant Gaikwad86edb872012-08-06 11:57:40 +05301481struct clk_ops tegra_audio_sync_clk_ops = {
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301482 .is_enabled = tegra20_audio_sync_clk_is_enabled,
1483 .enable = tegra20_audio_sync_clk_enable,
1484 .disable = tegra20_audio_sync_clk_disable,
1485 .set_parent = tegra20_audio_sync_clk_set_parent,
1486 .get_parent = tegra20_audio_sync_clk_get_parent,
Colin Crossd8611962010-01-28 16:40:29 -08001487};
1488
Colin Crosscea62c82010-10-04 11:49:26 -07001489/* cdev1 and cdev2 (dap_mclk1 and dap_mclk2) ops */
1490
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301491static int tegra20_cdev_clk_is_enabled(struct clk_hw *hw)
Colin Crosscea62c82010-10-04 11:49:26 -07001492{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301493 struct clk_tegra *c = to_clk_tegra(hw);
Colin Crosscea62c82010-10-04 11:49:26 -07001494 /* We could un-tristate the cdev1 or cdev2 pingroup here; this is
1495 * currently done in the pinmux code. */
1496 c->state = ON;
Colin Cross1be3d052011-02-21 16:44:07 -08001497
1498 BUG_ON(!c->u.periph.clk_num);
1499
Colin Crosscea62c82010-10-04 11:49:26 -07001500 if (!(clk_readl(CLK_OUT_ENB + PERIPH_CLK_TO_ENB_REG(c)) &
1501 PERIPH_CLK_TO_ENB_BIT(c)))
1502 c->state = OFF;
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301503 return c->state;
Colin Crosscea62c82010-10-04 11:49:26 -07001504}
1505
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301506static int tegra20_cdev_clk_enable(struct clk_hw *hw)
Colin Crosscea62c82010-10-04 11:49:26 -07001507{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301508 struct clk_tegra *c = to_clk_tegra(hw);
Colin Cross1be3d052011-02-21 16:44:07 -08001509 BUG_ON(!c->u.periph.clk_num);
1510
Colin Crosscea62c82010-10-04 11:49:26 -07001511 clk_writel(PERIPH_CLK_TO_ENB_BIT(c),
1512 CLK_OUT_ENB_SET + PERIPH_CLK_TO_ENB_SET_REG(c));
1513 return 0;
1514}
1515
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301516static void tegra20_cdev_clk_disable(struct clk_hw *hw)
Colin Crosscea62c82010-10-04 11:49:26 -07001517{
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301518 struct clk_tegra *c = to_clk_tegra(hw);
Colin Cross1be3d052011-02-21 16:44:07 -08001519 BUG_ON(!c->u.periph.clk_num);
1520
Colin Crosscea62c82010-10-04 11:49:26 -07001521 clk_writel(PERIPH_CLK_TO_ENB_BIT(c),
1522 CLK_OUT_ENB_CLR + PERIPH_CLK_TO_ENB_SET_REG(c));
1523}
1524
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301525static unsigned long tegra20_cdev_recalc_rate(struct clk_hw *hw,
1526 unsigned long prate)
1527{
1528 return to_clk_tegra(hw)->fixed_rate;
1529}
1530
Prashant Gaikwad86edb872012-08-06 11:57:40 +05301531struct clk_ops tegra_cdev_clk_ops = {
Prashant Gaikwad92fe58f2012-08-06 11:57:43 +05301532 .is_enabled = tegra20_cdev_clk_is_enabled,
1533 .enable = tegra20_cdev_clk_enable,
1534 .disable = tegra20_cdev_clk_disable,
1535 .recalc_rate = tegra20_cdev_recalc_rate,
Colin Cross310992c2011-02-12 16:14:03 -08001536};