blob: d4217b93e10befee1360785af72e277c9af203ab [file] [log] [blame]
Paul Walmsley02e19a92008-03-18 15:09:51 +02001/*
2 * OMAP3-specific clock framework functions
3 *
Paul Walmsley542313c2008-07-03 12:24:45 +03004 * Copyright (C) 2007-2008 Texas Instruments, Inc.
Paul Walmsley3c82e222009-07-24 19:44:06 -06005 * Copyright (C) 2007-2009 Nokia Corporation
Paul Walmsley02e19a92008-03-18 15:09:51 +02006 *
7 * Written by Paul Walmsley
Paul Walmsley542313c2008-07-03 12:24:45 +03008 * Testing and integration fixes by Jouni Högander
Paul Walmsley02e19a92008-03-18 15:09:51 +02009 *
10 * Parts of this code are based on code written by
11 * Richard Woodruff, Tony Lindgren, Tuukka Tikkanen, Karthik Dasu
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License version 2 as
15 * published by the Free Software Foundation.
16 */
17#undef DEBUG
18
19#include <linux/module.h>
20#include <linux/kernel.h>
21#include <linux/device.h>
22#include <linux/list.h>
23#include <linux/errno.h>
24#include <linux/delay.h>
25#include <linux/clk.h>
26#include <linux/io.h>
Paul Walmsley542313c2008-07-03 12:24:45 +030027#include <linux/limits.h>
Russell Kingfbd3bdb2008-09-06 12:13:59 +010028#include <linux/bitops.h>
Paul Walmsley02e19a92008-03-18 15:09:51 +020029
Tony Lindgrence491cf2009-10-20 09:40:47 -070030#include <plat/cpu.h>
31#include <plat/clock.h>
32#include <plat/sram.h>
Paul Walmsley82e9bd52009-12-08 16:18:47 -070033#include <plat/sdrc.h>
Paul Walmsley02e19a92008-03-18 15:09:51 +020034#include <asm/div64.h>
Russell King44dc9d02009-01-19 15:51:11 +000035#include <asm/clkdev.h>
Paul Walmsley02e19a92008-03-18 15:09:51 +020036
Paul Walmsley02e19a92008-03-18 15:09:51 +020037#include "clock.h"
Paul Walmsley82e9bd52009-12-08 16:18:47 -070038#include "clock34xx.h"
39#include "sdrc.h"
Paul Walmsley02e19a92008-03-18 15:09:51 +020040#include "prm.h"
41#include "prm-regbits-34xx.h"
42#include "cm.h"
43#include "cm-regbits-34xx.h"
44
Paul Walmsleyc9812d02009-06-19 19:08:26 -060045#define CYCLES_PER_MHZ 1000000
46
Rajendra Nayak7a66a392009-10-05 13:31:44 -070047/*
48 * DPLL5_FREQ_FOR_USBHOST: USBHOST and USBTLL are the only clocks
49 * that are sourced by DPLL5, and both of these require this clock
50 * to be at 120 MHz for proper operation.
51 */
52#define DPLL5_FREQ_FOR_USBHOST 120000000
53
Paul Walmsley82e9bd52009-12-08 16:18:47 -070054/* needed by omap3_core_dpll_m2_set_rate() */
55struct clk *sdrc_ick_p, *arm_fck_p;
56
Paul Walmsley02e19a92008-03-18 15:09:51 +020057/**
Paul Walmsley3c82e222009-07-24 19:44:06 -060058 * omap3430es2_clk_ssi_find_idlest - return CM_IDLEST info for SSI
59 * @clk: struct clk * being enabled
60 * @idlest_reg: void __iomem ** to store CM_IDLEST reg address into
61 * @idlest_bit: pointer to a u8 to store the CM_IDLEST bit shift into
62 *
63 * The OMAP3430ES2 SSI target CM_IDLEST bit is at a different shift
64 * from the CM_{I,F}CLKEN bit. Pass back the correct info via
65 * @idlest_reg and @idlest_bit. No return value.
66 */
67static void omap3430es2_clk_ssi_find_idlest(struct clk *clk,
68 void __iomem **idlest_reg,
69 u8 *idlest_bit)
70{
71 u32 r;
72
73 r = (((__force u32)clk->enable_reg & ~0xf0) | 0x20);
74 *idlest_reg = (__force void __iomem *)r;
75 *idlest_bit = OMAP3430ES2_ST_SSI_IDLE_SHIFT;
76}
77
Paul Walmsley82e9bd52009-12-08 16:18:47 -070078const struct clkops clkops_omap3430es2_ssi_wait = {
79 .enable = omap2_dflt_clk_enable,
80 .disable = omap2_dflt_clk_disable,
81 .find_idlest = omap3430es2_clk_ssi_find_idlest,
82 .find_companion = omap2_clk_dflt_find_companion,
83};
84
Paul Walmsley3c82e222009-07-24 19:44:06 -060085/**
86 * omap3430es2_clk_dss_usbhost_find_idlest - CM_IDLEST info for DSS, USBHOST
87 * @clk: struct clk * being enabled
88 * @idlest_reg: void __iomem ** to store CM_IDLEST reg address into
89 * @idlest_bit: pointer to a u8 to store the CM_IDLEST bit shift into
90 *
91 * Some OMAP modules on OMAP3 ES2+ chips have both initiator and
92 * target IDLEST bits. For our purposes, we are concerned with the
93 * target IDLEST bits, which exist at a different bit position than
94 * the *CLKEN bit position for these modules (DSS and USBHOST) (The
95 * default find_idlest code assumes that they are at the same
96 * position.) No return value.
97 */
98static void omap3430es2_clk_dss_usbhost_find_idlest(struct clk *clk,
99 void __iomem **idlest_reg,
100 u8 *idlest_bit)
101{
102 u32 r;
103
104 r = (((__force u32)clk->enable_reg & ~0xf0) | 0x20);
105 *idlest_reg = (__force void __iomem *)r;
106 /* USBHOST_IDLE has same shift */
107 *idlest_bit = OMAP3430ES2_ST_DSS_IDLE_SHIFT;
108}
109
Paul Walmsley82e9bd52009-12-08 16:18:47 -0700110const struct clkops clkops_omap3430es2_dss_usbhost_wait = {
111 .enable = omap2_dflt_clk_enable,
112 .disable = omap2_dflt_clk_disable,
113 .find_idlest = omap3430es2_clk_dss_usbhost_find_idlest,
114 .find_companion = omap2_clk_dflt_find_companion,
115};
116
Paul Walmsley3c82e222009-07-24 19:44:06 -0600117/**
118 * omap3430es2_clk_hsotgusb_find_idlest - return CM_IDLEST info for HSOTGUSB
119 * @clk: struct clk * being enabled
120 * @idlest_reg: void __iomem ** to store CM_IDLEST reg address into
121 * @idlest_bit: pointer to a u8 to store the CM_IDLEST bit shift into
122 *
123 * The OMAP3430ES2 HSOTGUSB target CM_IDLEST bit is at a different
124 * shift from the CM_{I,F}CLKEN bit. Pass back the correct info via
125 * @idlest_reg and @idlest_bit. No return value.
126 */
127static void omap3430es2_clk_hsotgusb_find_idlest(struct clk *clk,
128 void __iomem **idlest_reg,
129 u8 *idlest_bit)
130{
131 u32 r;
132
133 r = (((__force u32)clk->enable_reg & ~0xf0) | 0x20);
134 *idlest_reg = (__force void __iomem *)r;
135 *idlest_bit = OMAP3430ES2_ST_HSOTGUSB_IDLE_SHIFT;
136}
137
Paul Walmsley82e9bd52009-12-08 16:18:47 -0700138const struct clkops clkops_omap3430es2_hsotgusb_wait = {
139 .enable = omap2_dflt_clk_enable,
140 .disable = omap2_dflt_clk_disable,
141 .find_idlest = omap3430es2_clk_hsotgusb_find_idlest,
142 .find_companion = omap2_clk_dflt_find_companion,
143};
144
Paul Walmsley82e9bd52009-12-08 16:18:47 -0700145const struct clkops clkops_noncore_dpll_ops = {
146 .enable = omap3_noncore_dpll_enable,
147 .disable = omap3_noncore_dpll_disable,
148};
Paul Walmsley16c90f02009-01-27 19:12:47 -0700149
Paul Walmsley82e9bd52009-12-08 16:18:47 -0700150int omap3_dpll4_set_rate(struct clk *clk, unsigned long rate)
Paul Walmsley16c90f02009-01-27 19:12:47 -0700151{
152 /*
153 * According to the 12-5 CDP code from TI, "Limitation 2.5"
154 * on 3430ES1 prevents us from changing DPLL multipliers or dividers
155 * on DPLL4.
156 */
157 if (omap_rev() == OMAP3430_REV_ES1_0) {
158 printk(KERN_ERR "clock: DPLL4 cannot change rate due to "
159 "silicon 'Limitation 2.5' on 3430ES1.\n");
160 return -EINVAL;
161 }
162 return omap3_noncore_dpll_set_rate(clk, rate);
163}
164
Paul Walmsley0eafd472009-01-28 12:27:42 -0700165
166/*
167 * CORE DPLL (DPLL3) rate programming functions
168 *
169 * These call into SRAM code to do the actual CM writes, since the SDRAM
170 * is clocked from DPLL3.
171 */
172
173/**
174 * omap3_core_dpll_m2_set_rate - set CORE DPLL M2 divider
175 * @clk: struct clk * of DPLL to set
176 * @rate: rounded target rate
177 *
178 * Program the DPLL M2 divider with the rounded target rate. Returns
179 * -EINVAL upon error, or 0 upon success.
180 */
Paul Walmsley82e9bd52009-12-08 16:18:47 -0700181int omap3_core_dpll_m2_set_rate(struct clk *clk, unsigned long rate)
Paul Walmsley0eafd472009-01-28 12:27:42 -0700182{
183 u32 new_div = 0;
Paul Walmsley4519c2b2009-05-12 17:26:32 -0600184 u32 unlock_dll = 0;
Paul Walmsleyc9812d02009-06-19 19:08:26 -0600185 u32 c;
Paul Walmsley82e9bd52009-12-08 16:18:47 -0700186 unsigned long validrate, sdrcrate, _mpurate;
Jean Pihet58cda882009-07-24 19:43:25 -0600187 struct omap_sdrc_params *sdrc_cs0;
188 struct omap_sdrc_params *sdrc_cs1;
189 int ret;
Paul Walmsley0eafd472009-01-28 12:27:42 -0700190
191 if (!clk || !rate)
192 return -EINVAL;
193
Paul Walmsley0eafd472009-01-28 12:27:42 -0700194 validrate = omap2_clksel_round_rate_div(clk, rate, &new_div);
195 if (validrate != rate)
196 return -EINVAL;
197
Paul Walmsley82e9bd52009-12-08 16:18:47 -0700198 sdrcrate = sdrc_ick_p->rate;
Paul Walmsley0eafd472009-01-28 12:27:42 -0700199 if (rate > clk->rate)
Tero Kristo3afec632009-06-19 19:08:29 -0600200 sdrcrate <<= ((rate / clk->rate) >> 1);
Paul Walmsley0eafd472009-01-28 12:27:42 -0700201 else
Tero Kristo3afec632009-06-19 19:08:29 -0600202 sdrcrate >>= ((clk->rate / rate) >> 1);
Paul Walmsley0eafd472009-01-28 12:27:42 -0700203
Jean Pihet58cda882009-07-24 19:43:25 -0600204 ret = omap2_sdrc_get_params(sdrcrate, &sdrc_cs0, &sdrc_cs1);
205 if (ret)
Paul Walmsley0eafd472009-01-28 12:27:42 -0700206 return -EINVAL;
207
Paul Walmsley4519c2b2009-05-12 17:26:32 -0600208 if (sdrcrate < MIN_SDRC_DLL_LOCK_FREQ) {
209 pr_debug("clock: will unlock SDRC DLL\n");
210 unlock_dll = 1;
211 }
212
Paul Walmsleyc9812d02009-06-19 19:08:26 -0600213 /*
214 * XXX This only needs to be done when the CPU frequency changes
215 */
Paul Walmsley82e9bd52009-12-08 16:18:47 -0700216 _mpurate = arm_fck_p->rate / CYCLES_PER_MHZ;
217 c = (_mpurate << SDRC_MPURATE_SCALE) >> SDRC_MPURATE_BASE_SHIFT;
Paul Walmsleyc9812d02009-06-19 19:08:26 -0600218 c += 1; /* for safety */
219 c *= SDRC_MPURATE_LOOPS;
220 c >>= SDRC_MPURATE_SCALE;
221 if (c == 0)
222 c = 1;
223
Paul Walmsleyb7aee4b2009-05-12 17:27:10 -0600224 pr_debug("clock: changing CORE DPLL rate from %lu to %lu\n", clk->rate,
225 validrate);
Jean Pihet58cda882009-07-24 19:43:25 -0600226 pr_debug("clock: SDRC CS0 timing params used:"
227 " RFR %08x CTRLA %08x CTRLB %08x MR %08x\n",
228 sdrc_cs0->rfr_ctrl, sdrc_cs0->actim_ctrla,
229 sdrc_cs0->actim_ctrlb, sdrc_cs0->mr);
230 if (sdrc_cs1)
231 pr_debug("clock: SDRC CS1 timing params used: "
232 " RFR %08x CTRLA %08x CTRLB %08x MR %08x\n",
233 sdrc_cs1->rfr_ctrl, sdrc_cs1->actim_ctrla,
234 sdrc_cs1->actim_ctrlb, sdrc_cs1->mr);
Paul Walmsley0eafd472009-01-28 12:27:42 -0700235
Jean Pihet58cda882009-07-24 19:43:25 -0600236 if (sdrc_cs1)
237 omap3_configure_core_dpll(
238 new_div, unlock_dll, c, rate > clk->rate,
239 sdrc_cs0->rfr_ctrl, sdrc_cs0->actim_ctrla,
240 sdrc_cs0->actim_ctrlb, sdrc_cs0->mr,
241 sdrc_cs1->rfr_ctrl, sdrc_cs1->actim_ctrla,
242 sdrc_cs1->actim_ctrlb, sdrc_cs1->mr);
243 else
244 omap3_configure_core_dpll(
245 new_div, unlock_dll, c, rate > clk->rate,
246 sdrc_cs0->rfr_ctrl, sdrc_cs0->actim_ctrla,
247 sdrc_cs0->actim_ctrlb, sdrc_cs0->mr,
248 0, 0, 0, 0);
Paul Walmsley0eafd472009-01-28 12:27:42 -0700249
Paul Walmsley0eafd472009-01-28 12:27:42 -0700250 return 0;
251}
252
Paul Walmsley542313c2008-07-03 12:24:45 +0300253/* Common clock code */
254
Paul Walmsley02e19a92008-03-18 15:09:51 +0200255/*
256 * As it is structured now, this will prevent an OMAP2/3 multiboot
257 * kernel from compiling. This will need further attention.
258 */
259#if defined(CONFIG_ARCH_OMAP3)
260
Paul Walmsley82e9bd52009-12-08 16:18:47 -0700261struct clk_functions omap2_clk_functions = {
Paul Walmsley02e19a92008-03-18 15:09:51 +0200262 .clk_enable = omap2_clk_enable,
263 .clk_disable = omap2_clk_disable,
264 .clk_round_rate = omap2_clk_round_rate,
265 .clk_set_rate = omap2_clk_set_rate,
266 .clk_set_parent = omap2_clk_set_parent,
267 .clk_disable_unused = omap2_clk_disable_unused,
268};
269
270/*
271 * Set clocks for bypass mode for reboot to work.
272 */
273void omap2_clk_prepare_for_reboot(void)
274{
275 /* REVISIT: Not ready for 343x */
276#if 0
277 u32 rate;
278
279 if (vclk == NULL || sclk == NULL)
280 return;
281
282 rate = clk_get_rate(sclk);
283 clk_set_rate(vclk, rate);
284#endif
285}
286
Paul Walmsley82e9bd52009-12-08 16:18:47 -0700287void omap3_clk_lock_dpll5(void)
Rajendra Nayak7a66a392009-10-05 13:31:44 -0700288{
289 struct clk *dpll5_clk;
290 struct clk *dpll5_m2_clk;
291
292 dpll5_clk = clk_get(NULL, "dpll5_ck");
293 clk_set_rate(dpll5_clk, DPLL5_FREQ_FOR_USBHOST);
294 clk_enable(dpll5_clk);
295
296 /* Enable autoidle to allow it to enter low power bypass */
297 omap3_dpll_allow_idle(dpll5_clk);
298
299 /* Program dpll5_m2_clk divider for no division */
300 dpll5_m2_clk = clk_get(NULL, "dpll5_m2_ck");
301 clk_enable(dpll5_m2_clk);
302 clk_set_rate(dpll5_m2_clk, DPLL5_FREQ_FOR_USBHOST);
303
304 clk_disable(dpll5_m2_clk);
305 clk_disable(dpll5_clk);
306 return;
307}
308
Paul Walmsley02e19a92008-03-18 15:09:51 +0200309/* REVISIT: Move this init stuff out into clock.c */
310
311/*
312 * Switch the MPU rate if specified on cmdline.
313 * We cannot do this early until cmdline is parsed.
314 */
315static int __init omap2_clk_arch_init(void)
316{
Paul Walmsley82e9bd52009-12-08 16:18:47 -0700317 struct clk *osc_sys_ck, *dpll1_ck, *arm_fck, *core_ck;
318 unsigned long osc_sys_rate;
319
Paul Walmsley02e19a92008-03-18 15:09:51 +0200320 if (!mpurate)
321 return -EINVAL;
322
Paul Walmsley82e9bd52009-12-08 16:18:47 -0700323 /* XXX test these for success */
324 dpll1_ck = clk_get(NULL, "dpll1_ck");
325 arm_fck = clk_get(NULL, "arm_fck");
326 core_ck = clk_get(NULL, "core_ck");
327 osc_sys_ck = clk_get(NULL, "osc_sys_ck");
328
Paul Walmsley02e19a92008-03-18 15:09:51 +0200329 /* REVISIT: not yet ready for 343x */
Paul Walmsley82e9bd52009-12-08 16:18:47 -0700330 if (clk_set_rate(dpll1_ck, mpurate))
Sanjeev Premi11b66382009-09-03 20:13:58 +0300331 printk(KERN_ERR "*** Unable to set MPU rate\n");
Paul Walmsley02e19a92008-03-18 15:09:51 +0200332
333 recalculate_root_clocks();
334
Paul Walmsley82e9bd52009-12-08 16:18:47 -0700335 osc_sys_rate = clk_get_rate(osc_sys_ck);
336
337 pr_info("Switched to new clocking rate (Crystal/Core/MPU): "
338 "%ld.%01ld/%ld/%ld MHz\n",
339 (osc_sys_rate / 1000000),
340 ((osc_sys_rate / 100000) % 10),
341 (clk_get_rate(core_ck) / 1000000),
342 (clk_get_rate(arm_fck) / 1000000));
Sanjeev Premi11b66382009-09-03 20:13:58 +0300343
344 calibrate_delay();
Paul Walmsley02e19a92008-03-18 15:09:51 +0200345
346 return 0;
347}
348arch_initcall(omap2_clk_arch_init);
349
Paul Walmsley02e19a92008-03-18 15:09:51 +0200350
351#endif
Paul Walmsley82e9bd52009-12-08 16:18:47 -0700352
353