clock2xxx.c 15.0 KB
Newer Older
1 2 3
/*
 *  linux/arch/arm/mach-omap2/clock.c
 *
4 5
 *  Copyright (C) 2005-2008 Texas Instruments, Inc.
 *  Copyright (C) 2004-2008 Nokia Corporation
6
 *
7 8 9
 *  Contacts:
 *  Richard Woodruff <r-woodruff2@ti.com>
 *  Paul Walmsley
10
 *
11 12
 *  Based on earlier work by Tuukka Tikkanen, Tony Lindgren,
 *  Gordon McNutt and RidgeRun, Inc.
13 14 15 16 17
 *
 * 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
#include <linux/io.h>
#include <linux/cpufreq.h>
29
#include <linux/bitops.h>
30

31 32 33
#include <plat/clock.h>
#include <plat/sram.h>
#include <plat/prcm.h>
34
#include <plat/clkdev_omap.h>
35
#include <asm/div64.h>
36
#include <asm/clkdev.h>
37

38
#include <plat/sdrc.h>
39
#include "clock.h"
40 41
#include "clock2xxx.h"
#include "opp2xxx.h"
42 43 44 45
#include "prm.h"
#include "prm-regbits-24xx.h"
#include "cm.h"
#include "cm-regbits-24xx.h"
46

47

48 49 50
/* CM_CLKEN_PLL.EN_{54,96}M_PLL options (24XX) */
#define EN_APLL_STOPPED			0
#define EN_APLL_LOCKED			3
51

52 53 54 55 56 57
/* 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 */
58

59 60 61 62
const struct prcm_config *curr_prcm_set;
const struct prcm_config *rate_table;

struct clk *vclk, *sclk, *dclk;
63

64
void __iomem *prcm_clksrc_ctrl;
65

66
/*-------------------------------------------------------------------------
67
 * Omap24xx specific clock functions
68 69
 *-------------------------------------------------------------------------*/

70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
/**
 * omap2430_clk_i2chs_find_idlest - return CM_IDLEST info for 2430 I2CHS
 * @clk: struct clk * being enabled
 * @idlest_reg: void __iomem ** to store CM_IDLEST reg address into
 * @idlest_bit: pointer to a u8 to store the CM_IDLEST bit shift into
 *
 * OMAP2430 I2CHS CM_IDLEST bits are in CM_IDLEST1_CORE, but the
 * CM_*CLKEN bits are in CM_{I,F}CLKEN2_CORE.  This custom function
 * passes back the correct CM_IDLEST register address for I2CHS
 * modules.  No return value.
 */
static void omap2430_clk_i2chs_find_idlest(struct clk *clk,
					   void __iomem **idlest_reg,
					   u8 *idlest_bit)
{
	*idlest_reg = OMAP_CM_REGADDR(CORE_MOD, CM_IDLEST);
	*idlest_bit = clk->enable_bit;
}

89 90 91 92 93 94 95
/* 2430 I2CHS has non-standard IDLEST register */
const struct clkops clkops_omap2430_i2chs_wait = {
	.enable		= omap2_dflt_clk_enable,
	.disable	= omap2_dflt_clk_disable,
	.find_idlest	= omap2430_clk_i2chs_find_idlest,
	.find_companion = omap2_clk_dflt_find_companion,
};
96

97 98 99 100 101 102 103 104 105 106
/**
 * omap2xxx_clk_get_core_rate - return the CORE_CLK rate
 * @clk: pointer to the combined dpll_ck + core_ck (currently "dpll_ck")
 *
 * Returns the CORE_CLK rate.  CORE_CLK can have one of three rate
 * sources on OMAP2xxx: the DPLL CLKOUT rate, DPLL CLKOUTX2, or 32KHz
 * (the latter is unusual).  This currently should be called with
 * struct clk *dpll_ck, which is a composite clock of dpll_ck and
 * core_ck.
 */
107
unsigned long omap2xxx_clk_get_core_rate(struct clk *clk)
108
{
109 110
	long long core_clk;
	u32 v;
111

112
	core_clk = omap2_get_dpll_rate(clk);
113

114 115 116 117 118 119 120
	v = cm_read_mod_reg(PLL_MOD, CM_CLKSEL2);
	v &= OMAP24XX_CORE_CLK_SRC_MASK;

	if (v == CORE_CLK_SRC_32K)
		core_clk = 32768;
	else
		core_clk *= v;
121

122
	return core_clk;
123 124
}

125 126 127 128
static int omap2_enable_osc_ck(struct clk *clk)
{
	u32 pcc;

129
	pcc = __raw_readl(prcm_clksrc_ctrl);
130

131
	__raw_writel(pcc & ~OMAP_AUTOEXTCLKMODE_MASK, prcm_clksrc_ctrl);
132 133 134 135 136 137 138 139

	return 0;
}

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

140
	pcc = __raw_readl(prcm_clksrc_ctrl);
141

142
	__raw_writel(pcc | OMAP_AUTOEXTCLKMODE_MASK, prcm_clksrc_ctrl);
143 144
}

145 146 147
const struct clkops clkops_oscck = {
	.enable		= omap2_enable_osc_ck,
	.disable	= omap2_disable_osc_ck,
148 149
};

150
#ifdef OLD_CK
151
/* Recalculate SYST_CLK */
152
static void omap2_sys_clk_recalc(struct clk *clk)
153 154 155 156 157 158 159
{
	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);
}
160
#endif	/* OLD_CK */
161 162

/* Enable an APLL if off */
163
static int omap2_clk_apll_enable(struct clk *clk, u32 status_mask)
164
{
165
	u32 cval, apll_mask;
166

167
	apll_mask = EN_APLL_LOCKED << clk->enable_bit;
168

169
	cval = cm_read_mod_reg(PLL_MOD, CM_CLKEN);
170

171 172
	if ((cval & apll_mask) == apll_mask)
		return 0;   /* apll already enabled */
173

174 175 176
	cval &= ~apll_mask;
	cval |= apll_mask;
	cm_write_mod_reg(cval, PLL_MOD, CM_CLKEN);
177

178
	omap2_cm_wait_idlest(OMAP_CM_REGADDR(PLL_MOD, CM_IDLEST), status_mask,
179
			     clk->name);
180 181 182 183 184 185

	/*
	 * REVISIT: Should we return an error code if omap2_wait_clock_ready()
	 * fails?
	 */
	return 0;
186 187
}

188 189 190 191 192 193 194 195 196 197
static int omap2_clk_apll96_enable(struct clk *clk)
{
	return omap2_clk_apll_enable(clk, OMAP24XX_ST_96M_APLL);
}

static int omap2_clk_apll54_enable(struct clk *clk)
{
	return omap2_clk_apll_enable(clk, OMAP24XX_ST_54M_APLL);
}

198
/* Stop APLL */
199
static void omap2_clk_apll_disable(struct clk *clk)
200 201 202
{
	u32 cval;

203 204 205
	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);
206 207
}

208 209 210
const struct clkops clkops_apll96 = {
	.enable		= omap2_clk_apll96_enable,
	.disable	= omap2_clk_apll_disable,
211 212
};

213 214 215
const struct clkops clkops_apll54 = {
	.enable		= omap2_clk_apll54_enable,
	.disable	= omap2_clk_apll_disable,
216 217
};

218 219 220 221
/*
 * Uses the current prcm set to tell if a rate is valid.
 * You can go slower, but not faster within a given rate set.
 */
222
long omap2_dpllcore_round_rate(unsigned long target_rate)
223
{
224
	u32 high, low, core_clk_src;
225

226 227 228 229
	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 */
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
		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

}

251
unsigned long omap2_dpllcore_recalc(struct clk *clk)
252
{
253
	return omap2xxx_clk_get_core_rate(clk);
254
}
255

256
int omap2_reprogram_dpllcore(struct clk *clk, unsigned long rate)
257
{
258
	u32 cur_rate, low, mult, div, valid_rate, done_rate;
259 260
	u32 bypass = 0;
	struct prcm_config tmpset;
261
	const struct dpll_data *dd;
262

263
	cur_rate = omap2xxx_clk_get_core_rate(dclk);
264 265
	mult = cm_read_mod_reg(PLL_MOD, CM_CLKSEL2);
	mult &= OMAP24XX_CORE_CLK_SRC_MASK;
266 267

	if ((rate == (cur_rate / 2)) && (mult == 2)) {
268
		omap2xxx_sdrc_reprogram(CORE_CLK_SRC_DPLL, 1);
269
	} else if ((rate == (cur_rate * 2)) && (mult == 1)) {
270
		omap2xxx_sdrc_reprogram(CORE_CLK_SRC_DPLL_X2, 1);
271
	} else if (rate != cur_rate) {
272
		valid_rate = omap2_dpllcore_round_rate(rate);
273
		if (valid_rate != rate)
274
			return -EINVAL;
275

276
		if (mult == 1)
277 278 279 280
			low = curr_prcm_set->dpll_speed;
		else
			low = curr_prcm_set->dpll_speed / 2;

281 282
		dd = clk->dpll_data;
		if (!dd)
283
			return -EINVAL;
284 285 286 287

		tmpset.cm_clksel1_pll = __raw_readl(dd->mult_div1_reg);
		tmpset.cm_clksel1_pll &= ~(dd->mult_mask |
					   dd->div1_mask);
288
		div = ((curr_prcm_set->xtal_speed / 1000000) - 1);
289 290
		tmpset.cm_clksel2_pll = cm_read_mod_reg(PLL_MOD, CM_CLKSEL2);
		tmpset.cm_clksel2_pll &= ~OMAP24XX_CORE_CLK_SRC_MASK;
291
		if (rate > low) {
292
			tmpset.cm_clksel2_pll |= CORE_CLK_SRC_DPLL_X2;
293
			mult = ((rate / 2) / 1000000);
294
			done_rate = CORE_CLK_SRC_DPLL_X2;
295
		} else {
296
			tmpset.cm_clksel2_pll |= CORE_CLK_SRC_DPLL;
297
			mult = (rate / 1000000);
298
			done_rate = CORE_CLK_SRC_DPLL;
299
		}
300 301
		tmpset.cm_clksel1_pll |= (div << __ffs(dd->mult_mask));
		tmpset.cm_clksel1_pll |= (mult << __ffs(dd->div1_mask));
302 303

		/* Worst case */
304
		tmpset.base_sdrc_rfr = SDRC_RFR_CTRL_BYPASS;
305 306 307 308

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

309 310
		/* For omap2xxx_sdrc_init_params() */
		omap2xxx_sdrc_reprogram(CORE_CLK_SRC_DPLL_X2, 1);
311 312 313 314 315 316

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

		/* Errata: ret dll entry state */
317 318
		omap2xxx_sdrc_init_params(omap2xxx_sdrc_dll_is_unlocked());
		omap2xxx_sdrc_reprogram(done_rate, 0);
319 320
	}

321
	return 0;
322 323
}

324 325 326 327 328 329
/**
 * 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.
 */
330
unsigned long omap2_table_mpu_recalc(struct clk *clk)
331
{
332
	return curr_prcm_set->mpu_speed;
333 334 335 336 337 338 339 340 341
}

/*
 * 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.
 */
342
long omap2_round_to_table_rate(struct clk *clk, unsigned long rate)
343
{
344
	const struct prcm_config *ptr;
345
	long highest_rate;
346 347 348
	long sys_ck_rate;

	sys_ck_rate = clk_get_rate(sclk);
349 350 351 352

	highest_rate = -EINVAL;

	for (ptr = rate_table; ptr->mpu_speed; ptr++) {
353 354
		if (!(ptr->flags & cpu_mask))
			continue;
355
		if (ptr->xtal_speed != sys_ck_rate)
356 357 358 359 360 361 362 363 364 365 366 367
			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 */
368
int omap2_select_table_rate(struct clk *clk, unsigned long rate)
369
{
370
	u32 cur_rate, done_rate, bypass = 0, tmp;
371
	const struct prcm_config *prcm;
372
	unsigned long found_speed = 0;
373
	unsigned long flags;
374 375 376
	long sys_ck_rate;

	sys_ck_rate = clk_get_rate(sclk);
377 378 379 380 381

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

382
		if (prcm->xtal_speed != sys_ck_rate)
383 384 385 386 387 388 389 390 391 392
			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",
393
		       rate / 1000000);
394 395 396 397
		return -EINVAL;
	}

	curr_prcm_set = prcm;
398
	cur_rate = omap2xxx_clk_get_core_rate(dclk);
399 400

	if (prcm->dpll_speed == cur_rate / 2) {
401
		omap2xxx_sdrc_reprogram(CORE_CLK_SRC_DPLL, 1);
402
	} else if (prcm->dpll_speed == cur_rate * 2) {
403
		omap2xxx_sdrc_reprogram(CORE_CLK_SRC_DPLL_X2, 1);
404 405 406 407 408 409
	} else if (prcm->dpll_speed != cur_rate) {
		local_irq_save(flags);

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

410 411 412
		if ((prcm->cm_clksel2_pll & OMAP24XX_CORE_CLK_SRC_MASK) ==
		    CORE_CLK_SRC_DPLL_X2)
			done_rate = CORE_CLK_SRC_DPLL_X2;
413
		else
414
			done_rate = CORE_CLK_SRC_DPLL;
415 416

		/* MPU divider */
417
		cm_write_mod_reg(prcm->cm_clksel_mpu, MPU_MOD, CM_CLKSEL);
418 419

		/* dsp + iva1 div(2420), iva2.1(2430) */
420 421
		cm_write_mod_reg(prcm->cm_clksel_dsp,
				 OMAP24XX_DSP_MOD, CM_CLKSEL);
422

423
		cm_write_mod_reg(prcm->cm_clksel_gfx, GFX_MOD, CM_CLKSEL);
424 425

		/* Major subsystem dividers */
426
		tmp = cm_read_mod_reg(CORE_MOD, CM_CLKSEL1) & OMAP24XX_CLKSEL_DSS2_MASK;
427 428 429
		cm_write_mod_reg(prcm->cm_clksel1_core | tmp, CORE_MOD,
				 CM_CLKSEL1);

430
		if (cpu_is_omap2430())
431 432
			cm_write_mod_reg(prcm->cm_clksel_mdm,
					 OMAP2430_MDM_MOD, CM_CLKSEL);
433

434 435
		/* x2 to enter omap2xxx_sdrc_init_params() */
		omap2xxx_sdrc_reprogram(CORE_CLK_SRC_DPLL_X2, 1);
436 437 438 439

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

440 441
		omap2xxx_sdrc_init_params(omap2xxx_sdrc_dll_is_unlocked());
		omap2xxx_sdrc_reprogram(done_rate, 0);
442 443 444 445 446 447 448

		local_irq_restore(flags);
	}

	return 0;
}

449 450 451
#ifdef CONFIG_CPU_FREQ
/*
 * Walk PRCM rate table and fillout cpufreq freq_table
452
 * XXX This should be replaced by an OPP layer in the near future
453
 */
454
static struct cpufreq_frequency_table *freq_table;
455 456 457

void omap2_clk_init_cpufreq_table(struct cpufreq_frequency_table **table)
{
458
	const struct prcm_config *prcm;
459
	long sys_ck_rate;
460
	int i = 0;
461
	int tbl_sz = 0;
462

463 464
	sys_ck_rate = clk_get_rate(sclk);

465 466 467
	for (prcm = rate_table; prcm->mpu_speed; prcm++) {
		if (!(prcm->flags & cpu_mask))
			continue;
468
		if (prcm->xtal_speed != sys_ck_rate)
469 470 471 472 473 474
			continue;

		/* don't put bypass rates in table */
		if (prcm->dpll_speed == prcm->xtal_speed)
			continue;

475
		tbl_sz++;
476 477
	}

478 479 480 481 482 483 484
	/*
	 * XXX Ensure that we're doing what CPUFreq expects for this error
	 * case and the following one
	 */
	if (tbl_sz == 0) {
		pr_warning("%s: no matching entries in rate_table\n",
			   __func__);
485 486 487
		return;
	}

488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
	/* Include the CPUFREQ_TABLE_END terminator entry */
	tbl_sz++;

	freq_table = kzalloc(sizeof(struct cpufreq_frequency_table) * tbl_sz,
			     GFP_ATOMIC);
	if (!freq_table) {
		pr_err("%s: could not kzalloc frequency table\n", __func__);
		return;
	}

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

		/* don't put bypass rates in table */
		if (prcm->dpll_speed == prcm->xtal_speed)
			continue;

		freq_table[i].index = i;
		freq_table[i].frequency = prcm->mpu_speed / 1000;
		i++;
	}

513 514 515 516 517
	freq_table[i].index = i;
	freq_table[i].frequency = CPUFREQ_TABLE_END;

	*table = &freq_table[0];
}
518 519 520 521 522 523

void omap2_clk_exit_cpufreq_table(struct cpufreq_frequency_table **table)
{
	kfree(freq_table);
}

524 525
#endif

526
struct clk_functions omap2_clk_functions = {
527 528 529 530 531
	.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,
532
	.clk_disable_unused	= omap2_clk_disable_unused,
533 534
#ifdef	CONFIG_CPU_FREQ
	.clk_init_cpufreq_table	= omap2_clk_init_cpufreq_table,
535
	.clk_exit_cpufreq_table	= omap2_clk_exit_cpufreq_table,
536
#endif
537 538
};

539
static u32 omap2_get_apll_clkin(void)
540
{
541
	u32 aplls, srate = 0;
542

543 544 545
	aplls = cm_read_mod_reg(PLL_MOD, CM_CLKSEL1);
	aplls &= OMAP24XX_APLLS_CLKIN_MASK;
	aplls >>= OMAP24XX_APLLS_CLKIN_SHIFT;
546

547
	if (aplls == APLLS_CLKIN_19_2MHZ)
548
		srate = 19200000;
549
	else if (aplls == APLLS_CLKIN_13MHZ)
550
		srate = 13000000;
551
	else if (aplls == APLLS_CLKIN_12MHZ)
552
		srate = 12000000;
553

554
	return srate;
555 556 557 558 559 560
}

static u32 omap2_get_sysclkdiv(void)
{
	u32 div;

561
	div = __raw_readl(prcm_clksrc_ctrl);
562 563
	div &= OMAP_SYSCLKDIV_MASK;
	div >>= OMAP_SYSCLKDIV_SHIFT;
564

565 566 567
	return div;
}

568
unsigned long omap2_osc_clk_recalc(struct clk *clk)
569
{
570
	return omap2_get_apll_clkin() * omap2_get_sysclkdiv();
571 572
}

573
unsigned long omap2_sys_clk_recalc(struct clk *clk)
574
{
575
	return clk->parent->rate / omap2_get_sysclkdiv();
576 577
}

578 579 580 581 582 583 584 585 586 587 588 589 590 591
/*
 * 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);
}

592 593 594 595 596 597
/*
 * 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)
{
598 599 600
	struct clk *virt_prcm_set, *sys_ck, *dpll_ck, *mpu_ck;
	unsigned long sys_ck_rate;

601 602 603
	if (!mpurate)
		return -EINVAL;

604 605 606 607 608 609
	virt_prcm_set = clk_get(NULL, "virt_prcm_set");
	sys_ck = clk_get(NULL, "sys_ck");
	dpll_ck = clk_get(NULL, "dpll_ck");
	mpu_ck = clk_get(NULL, "mpu_ck");

	if (clk_set_rate(virt_prcm_set, mpurate))
610 611
		printk(KERN_ERR "Could not find matching MPU rate\n");

612
	recalculate_root_clocks();
613

614 615 616 617 618 619 620
	sys_ck_rate = clk_get_rate(sys_ck);

	pr_info("Switched to new clocking rate (Crystal/DPLL/MPU): "
		"%ld.%01ld/%ld/%ld MHz\n",
		(sys_ck_rate / 1000000), (sys_ck_rate / 100000) % 10,
		(clk_get_rate(dpll_ck) / 1000000),
		(clk_get_rate(mpu_ck) / 1000000));
621 622 623 624 625 626

	return 0;
}
arch_initcall(omap2_clk_arch_init);