clock24xx.c 12.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 *  linux/arch/arm/mach-omap2/clock.c
 *
 *  Copyright (C) 2005 Texas Instruments Inc.
 *  Richard Woodruff <r-woodruff2@ti.com>
 *  Created for OMAP2.
 *
 *  Cleaned up and modified to use omap shared clock framework by
 *  Tony Lindgren <tony@atomide.com>
 *
 *  Based on omap1 clock.c, Copyright (C) 2004 - 2005 Nokia corporation
 *  Written by Tuukka Tikkanen <tuukka.tikkanen@elektrobit.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */
18 19
#undef DEBUG

20 21 22 23 24 25
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/device.h>
#include <linux/list.h>
#include <linux/errno.h>
#include <linux/delay.h>
26
#include <linux/clk.h>
27

28 29
#include <linux/io.h>
#include <linux/cpufreq.h>
30 31 32

#include <asm/arch/clock.h>
#include <asm/arch/sram.h>
33
#include <asm/div64.h>
34
#include <asm/bitops.h>
35

36
#include "memory.h"
37
#include "clock.h"
38
#include "clock24xx.h"
39 40 41 42
#include "prm.h"
#include "prm-regbits-24xx.h"
#include "cm.h"
#include "cm-regbits-24xx.h"
43

44 45 46
/* CM_CLKEN_PLL.EN_{54,96}M_PLL options (24XX) */
#define EN_APLL_STOPPED			0
#define EN_APLL_LOCKED			3
47

48 49 50 51 52 53
/* CM_CLKSEL1_PLL.APLLS_CLKIN options (24XX) */
#define APLLS_CLKIN_19_2MHZ		0
#define APLLS_CLKIN_13MHZ		2
#define APLLS_CLKIN_12MHZ		3

/* #define DOWN_VARIABLE_DPLL 1 */		/* Experimental */
54 55

static struct prcm_config *curr_prcm_set;
56 57
static struct clk *vclk;
static struct clk *sclk;
58 59

/*-------------------------------------------------------------------------
60
 * Omap24xx specific clock functions
61 62
 *-------------------------------------------------------------------------*/

63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
static int omap2_enable_osc_ck(struct clk *clk)
{
	u32 pcc;

	pcc = __raw_readl(OMAP24XX_PRCM_CLKSRC_CTRL);

	__raw_writel(pcc & ~OMAP_AUTOEXTCLKMODE_MASK,
		      OMAP24XX_PRCM_CLKSRC_CTRL);

	return 0;
}

static void omap2_disable_osc_ck(struct clk *clk)
{
	u32 pcc;

	pcc = __raw_readl(OMAP24XX_PRCM_CLKSRC_CTRL);

	__raw_writel(pcc | OMAP_AUTOEXTCLKMODE_MASK,
		      OMAP24XX_PRCM_CLKSRC_CTRL);
}

#ifdef OLD_CK
86 87 88 89 90 91 92 93 94
/* Recalculate SYST_CLK */
static void omap2_sys_clk_recalc(struct clk * clk)
{
	u32 div = PRCM_CLKSRC_CTRL;
	div &= (1 << 7) | (1 << 6);	/* Test if ext clk divided by 1 or 2 */
	div >>= clk->rate_offset;
	clk->rate = (clk->parent->rate / div);
	propagate_rate(clk);
}
95
#endif	/* OLD_CK */
96

97 98
/* This actually returns the rate of core_ck, not dpll_ck. */
static u32 omap2_get_dpll_rate_24xx(struct clk *tclk)
99
{
100
	long long dpll_clk;
101 102 103
	u8 amult;

	dpll_clk = omap2_get_dpll_rate(tclk);
104

105 106
	amult = cm_read_mod_reg(PLL_MOD, CM_CLKSEL2);
	amult &= OMAP24XX_CORE_CLK_SRC_MASK;
107 108 109 110 111 112
	dpll_clk *= amult;

	return dpll_clk;
}

/* Enable an APLL if off */
113
static int omap2_clk_fixed_enable(struct clk *clk)
114
{
115
	u32 cval, apll_mask;
116

117
	apll_mask = EN_APLL_LOCKED << clk->enable_bit;
118

119
	cval = cm_read_mod_reg(PLL_MOD, CM_CLKEN);
120

121 122
	if ((cval & apll_mask) == apll_mask)
		return 0;   /* apll already enabled */
123

124 125 126
	cval &= ~apll_mask;
	cval |= apll_mask;
	cm_write_mod_reg(cval, PLL_MOD, CM_CLKEN);
127 128

	if (clk == &apll96_ck)
129
		cval = OMAP24XX_ST_96M_APLL;
130
	else if (clk == &apll54_ck)
131
		cval = OMAP24XX_ST_54M_APLL;
132

133 134 135 136 137 138 139 140
	omap2_wait_clock_ready(OMAP_CM_REGADDR(PLL_MOD, CM_IDLEST), cval,
			    clk->name);

	/*
	 * REVISIT: Should we return an error code if omap2_wait_clock_ready()
	 * fails?
	 */
	return 0;
141 142 143 144 145 146 147
}

/* Stop APLL */
static void omap2_clk_fixed_disable(struct clk *clk)
{
	u32 cval;

148 149 150
	cval = cm_read_mod_reg(PLL_MOD, CM_CLKEN);
	cval &= ~(EN_APLL_LOCKED << clk->enable_bit);
	cm_write_mod_reg(cval, PLL_MOD, CM_CLKEN);
151 152 153 154 155 156 157 158
}

/*
 * Uses the current prcm set to tell if a rate is valid.
 * You can go slower, but not faster within a given rate set.
 */
static u32 omap2_dpll_round_rate(unsigned long target_rate)
{
159
	u32 high, low, core_clk_src;
160

161 162 163 164
	core_clk_src = cm_read_mod_reg(PLL_MOD, CM_CLKSEL2);
	core_clk_src &= OMAP24XX_CORE_CLK_SRC_MASK;

	if (core_clk_src == CORE_CLK_SRC_DPLL) {	/* DPLL clockout */
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
		high = curr_prcm_set->dpll_speed * 2;
		low = curr_prcm_set->dpll_speed;
	} else {				/* DPLL clockout x 2 */
		high = curr_prcm_set->dpll_speed;
		low = curr_prcm_set->dpll_speed / 2;
	}

#ifdef DOWN_VARIABLE_DPLL
	if (target_rate > high)
		return high;
	else
		return target_rate;
#else
	if (target_rate > low)
		return high;
	else
		return low;
#endif

}

186 187 188 189 190 191
static void omap2_dpll_recalc(struct clk *clk)
{
	clk->rate = omap2_get_dpll_rate_24xx(clk);

	propagate_rate(clk);
}
192

193
static int omap2_reprogram_dpll(struct clk *clk, unsigned long rate)
194
{
195
	u32 cur_rate, low, mult, div, valid_rate, done_rate;
196 197
	u32 bypass = 0;
	struct prcm_config tmpset;
198 199
	const struct dpll_data *dd;
	unsigned long flags;
200 201 202
	int ret = -EINVAL;

	local_irq_save(flags);
203 204 205
	cur_rate = omap2_get_dpll_rate_24xx(&dpll_ck);
	mult = cm_read_mod_reg(PLL_MOD, CM_CLKSEL2);
	mult &= OMAP24XX_CORE_CLK_SRC_MASK;
206 207

	if ((rate == (cur_rate / 2)) && (mult == 2)) {
208
		omap2_reprogram_sdrc(CORE_CLK_SRC_DPLL, 1);
209
	} else if ((rate == (cur_rate * 2)) && (mult == 1)) {
210
		omap2_reprogram_sdrc(CORE_CLK_SRC_DPLL_X2, 1);
211 212 213 214 215
	} else if (rate != cur_rate) {
		valid_rate = omap2_dpll_round_rate(rate);
		if (valid_rate != rate)
			goto dpll_exit;

216
		if (mult == 1)
217 218 219 220
			low = curr_prcm_set->dpll_speed;
		else
			low = curr_prcm_set->dpll_speed / 2;

221 222 223 224 225 226 227
		dd = clk->dpll_data;
		if (!dd)
			goto dpll_exit;

		tmpset.cm_clksel1_pll = __raw_readl(dd->mult_div1_reg);
		tmpset.cm_clksel1_pll &= ~(dd->mult_mask |
					   dd->div1_mask);
228
		div = ((curr_prcm_set->xtal_speed / 1000000) - 1);
229 230
		tmpset.cm_clksel2_pll = cm_read_mod_reg(PLL_MOD, CM_CLKSEL2);
		tmpset.cm_clksel2_pll &= ~OMAP24XX_CORE_CLK_SRC_MASK;
231
		if (rate > low) {
232
			tmpset.cm_clksel2_pll |= CORE_CLK_SRC_DPLL_X2;
233
			mult = ((rate / 2) / 1000000);
234
			done_rate = CORE_CLK_SRC_DPLL_X2;
235
		} else {
236
			tmpset.cm_clksel2_pll |= CORE_CLK_SRC_DPLL;
237
			mult = (rate / 1000000);
238
			done_rate = CORE_CLK_SRC_DPLL;
239
		}
240 241
		tmpset.cm_clksel1_pll |= (div << __ffs(dd->mult_mask));
		tmpset.cm_clksel1_pll |= (mult << __ffs(dd->div1_mask));
242 243

		/* Worst case */
244
		tmpset.base_sdrc_rfr = SDRC_RFR_CTRL_BYPASS;
245 246 247 248

		if (rate == curr_prcm_set->xtal_speed)	/* If asking for 1-1 */
			bypass = 1;

249
		omap2_reprogram_sdrc(CORE_CLK_SRC_DPLL_X2, 1); /* For init_mem */
250 251 252 253 254 255 256 257 258

		/* Force dll lock mode */
		omap2_set_prcm(tmpset.cm_clksel1_pll, tmpset.base_sdrc_rfr,
			       bypass);

		/* Errata: ret dll entry state */
		omap2_init_memory_params(omap2_dll_force_needed());
		omap2_reprogram_sdrc(done_rate, 0);
	}
259
	omap2_dpll_recalc(&dpll_ck);
260 261 262 263 264 265 266
	ret = 0;

dpll_exit:
	local_irq_restore(flags);
	return(ret);
}

267 268 269 270 271 272 273
/**
 * omap2_table_mpu_recalc - just return the MPU speed
 * @clk: virt_prcm_set struct clk
 *
 * Set virt_prcm_set's rate to the mpu_speed field of the current PRCM set.
 */
static void omap2_table_mpu_recalc(struct clk *clk)
274 275 276 277 278 279 280 281 282 283 284
{
	clk->rate = curr_prcm_set->mpu_speed;
}

/*
 * Look for a rate equal or less than the target rate given a configuration set.
 *
 * What's not entirely clear is "which" field represents the key field.
 * Some might argue L3-DDR, others ARM, others IVA. This code is simple and
 * just uses the ARM rates.
 */
285
static long omap2_round_to_table_rate(struct clk *clk, unsigned long rate)
286
{
287
	struct prcm_config *ptr;
288 289 290 291 292 293 294 295
	long highest_rate;

	if (clk != &virt_prcm_set)
		return -EINVAL;

	highest_rate = -EINVAL;

	for (ptr = rate_table; ptr->mpu_speed; ptr++) {
296 297
		if (!(ptr->flags & cpu_mask))
			continue;
298 299 300 301 302 303 304 305 306 307 308 309 310
		if (ptr->xtal_speed != sys_ck.rate)
			continue;

		highest_rate = ptr->mpu_speed;

		/* Can check only after xtal frequency check */
		if (ptr->mpu_speed <= rate)
			break;
	}
	return highest_rate;
}

/* Sets basic clocks based on the specified rate */
311
static int omap2_select_table_rate(struct clk *clk, unsigned long rate)
312
{
313
	u32 cur_rate, done_rate, bypass = 0, tmp;
314 315
	struct prcm_config *prcm;
	unsigned long found_speed = 0;
316
	unsigned long flags;
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340

	if (clk != &virt_prcm_set)
		return -EINVAL;

	for (prcm = rate_table; prcm->mpu_speed; prcm++) {
		if (!(prcm->flags & cpu_mask))
			continue;

		if (prcm->xtal_speed != sys_ck.rate)
			continue;

		if (prcm->mpu_speed <= rate) {
			found_speed = prcm->mpu_speed;
			break;
		}
	}

	if (!found_speed) {
		printk(KERN_INFO "Could not set MPU rate to %luMHz\n",
	 rate / 1000000);
		return -EINVAL;
	}

	curr_prcm_set = prcm;
341
	cur_rate = omap2_get_dpll_rate_24xx(&dpll_ck);
342 343

	if (prcm->dpll_speed == cur_rate / 2) {
344
		omap2_reprogram_sdrc(CORE_CLK_SRC_DPLL, 1);
345
	} else if (prcm->dpll_speed == cur_rate * 2) {
346
		omap2_reprogram_sdrc(CORE_CLK_SRC_DPLL_X2, 1);
347 348 349 350 351 352
	} else if (prcm->dpll_speed != cur_rate) {
		local_irq_save(flags);

		if (prcm->dpll_speed == prcm->xtal_speed)
			bypass = 1;

353 354 355
		if ((prcm->cm_clksel2_pll & OMAP24XX_CORE_CLK_SRC_MASK) ==
		    CORE_CLK_SRC_DPLL_X2)
			done_rate = CORE_CLK_SRC_DPLL_X2;
356
		else
357
			done_rate = CORE_CLK_SRC_DPLL;
358 359

		/* MPU divider */
360
		cm_write_mod_reg(prcm->cm_clksel_mpu, MPU_MOD, CM_CLKSEL);
361 362

		/* dsp + iva1 div(2420), iva2.1(2430) */
363 364
		cm_write_mod_reg(prcm->cm_clksel_dsp,
				 OMAP24XX_DSP_MOD, CM_CLKSEL);
365

366
		cm_write_mod_reg(prcm->cm_clksel_gfx, GFX_MOD, CM_CLKSEL);
367 368

		/* Major subsystem dividers */
369 370
		tmp = cm_read_mod_reg(CORE_MOD, CM_CLKSEL1) & OMAP24XX_CLKSEL_DSS2_MASK;
		cm_write_mod_reg(prcm->cm_clksel1_core | tmp, CORE_MOD, CM_CLKSEL1);
371
		if (cpu_is_omap2430())
372 373
			cm_write_mod_reg(prcm->cm_clksel_mdm,
					 OMAP2430_MDM_MOD, CM_CLKSEL);
374 375

		/* x2 to enter init_mem */
376
		omap2_reprogram_sdrc(CORE_CLK_SRC_DPLL_X2, 1);
377 378 379 380 381 382 383 384 385

		omap2_set_prcm(prcm->cm_clksel1_pll, prcm->base_sdrc_rfr,
			       bypass);

		omap2_init_memory_params(omap2_dll_force_needed());
		omap2_reprogram_sdrc(done_rate, 0);

		local_irq_restore(flags);
	}
386
	omap2_dpll_recalc(&dpll_ck);
387 388 389 390 391 392 393 394 395 396

	return 0;
}

static struct clk_functions omap2_clk_functions = {
	.clk_enable		= omap2_clk_enable,
	.clk_disable		= omap2_clk_disable,
	.clk_round_rate		= omap2_clk_round_rate,
	.clk_set_rate		= omap2_clk_set_rate,
	.clk_set_parent		= omap2_clk_set_parent,
397
	.clk_disable_unused	= omap2_clk_disable_unused,
398 399
};

400
static u32 omap2_get_apll_clkin(void)
401
{
402
	u32 aplls, sclk = 0;
403

404 405 406
	aplls = cm_read_mod_reg(PLL_MOD, CM_CLKSEL1);
	aplls &= OMAP24XX_APLLS_CLKIN_MASK;
	aplls >>= OMAP24XX_APLLS_CLKIN_SHIFT;
407

408
	if (aplls == APLLS_CLKIN_19_2MHZ)
409
		sclk = 19200000;
410
	else if (aplls == APLLS_CLKIN_13MHZ)
411
		sclk = 13000000;
412
	else if (aplls == APLLS_CLKIN_12MHZ)
413 414
		sclk = 12000000;

415 416 417 418 419 420 421 422 423 424
	return sclk;
}

static u32 omap2_get_sysclkdiv(void)
{
	u32 div;

	div = __raw_readl(OMAP24XX_PRCM_CLKSRC_CTRL);
	div &= OMAP_SYSCLKDIV_MASK;
	div >>= OMAP_SYSCLKDIV_SHIFT;
425

426 427 428 429 430 431 432 433 434 435 436 437 438
	return div;
}

static void omap2_osc_clk_recalc(struct clk *clk)
{
	clk->rate = omap2_get_apll_clkin() * omap2_get_sysclkdiv();
	propagate_rate(clk);
}

static void omap2_sys_clk_recalc(struct clk *clk)
{
	clk->rate = clk->parent->rate / omap2_get_sysclkdiv();
	propagate_rate(clk);
439 440
}

441 442 443 444 445 446 447 448 449 450 451 452 453 454
/*
 * Set clocks for bypass mode for reboot to work.
 */
void omap2_clk_prepare_for_reboot(void)
{
	u32 rate;

	if (vclk == NULL || sclk == NULL)
		return;

	rate = clk_get_rate(sclk);
	clk_set_rate(vclk, rate);
}

455 456 457 458 459 460 461 462 463 464 465 466
/*
 * Switch the MPU rate if specified on cmdline.
 * We cannot do this early until cmdline is parsed.
 */
static int __init omap2_clk_arch_init(void)
{
	if (!mpurate)
		return -EINVAL;

	if (omap2_select_table_rate(&virt_prcm_set, mpurate))
		printk(KERN_ERR "Could not find matching MPU rate\n");

467
	recalculate_root_clocks();
468 469 470 471 472 473 474 475 476 477 478 479 480

	printk(KERN_INFO "Switched to new clocking rate (Crystal/DPLL/MPU): "
	       "%ld.%01ld/%ld/%ld MHz\n",
	       (sys_ck.rate / 1000000), (sys_ck.rate / 100000) % 10,
	       (dpll_ck.rate / 1000000), (mpu_ck.rate / 1000000)) ;

	return 0;
}
arch_initcall(omap2_clk_arch_init);

int __init omap2_clk_init(void)
{
	struct prcm_config *prcm;
481
	struct clk **clkp;
482 483
	u32 clkrate;

484 485 486 487 488
	if (cpu_is_omap242x())
		cpu_mask = RATE_IN_242X;
	else if (cpu_is_omap2430())
		cpu_mask = RATE_IN_243X;

489 490
	clk_init(&omap2_clk_functions);

491 492 493 494 495
	omap2_osc_clk_recalc(&osc_ck);
	omap2_sys_clk_recalc(&sys_ck);

	for (clkp = onchip_24xx_clks;
	     clkp < onchip_24xx_clks + ARRAY_SIZE(onchip_24xx_clks);
496 497 498 499 500 501 502 503 504 505 506 507 508 509
	     clkp++) {

		if ((*clkp)->flags & CLOCK_IN_OMAP242X && cpu_is_omap2420()) {
			clk_register(*clkp);
			continue;
		}

		if ((*clkp)->flags & CLOCK_IN_OMAP243X && cpu_is_omap2430()) {
			clk_register(*clkp);
			continue;
		}
	}

	/* Check the MPU rate set by bootloader */
510
	clkrate = omap2_get_dpll_rate_24xx(&dpll_ck);
511
	for (prcm = rate_table; prcm->mpu_speed; prcm++) {
512 513
		if (!(prcm->flags & cpu_mask))
			continue;
514 515 516 517 518 519 520
		if (prcm->xtal_speed != sys_ck.rate)
			continue;
		if (prcm->dpll_speed <= clkrate)
			 break;
	}
	curr_prcm_set = prcm;

521
	recalculate_root_clocks();
522 523 524 525 526 527 528 529 530 531

	printk(KERN_INFO "Clocking rate (Crystal/DPLL/MPU): "
	       "%ld.%01ld/%ld/%ld MHz\n",
	       (sys_ck.rate / 1000000), (sys_ck.rate / 100000) % 10,
	       (dpll_ck.rate / 1000000), (mpu_ck.rate / 1000000)) ;

	/*
	 * Only enable those clocks we will need, let the drivers
	 * enable other clocks as necessary
	 */
532
	clk_enable_init_clocks();
533

534 535 536 537
	/* Avoid sleeping sleeping during omap2_clk_prepare_for_reboot() */
	vclk = clk_get(NULL, "virt_prcm_set");
	sclk = clk_get(NULL, "sys_ck");

538 539
	return 0;
}