clock.c 12.3 KB
Newer Older
1
/*
2
 * Clock and PLL control for DaVinci devices
3
 *
4 5
 * Copyright (C) 2006-2007 Texas Instruments.
 * Copyright (C) 2008-2009 Deep Root Systems, LLC
6 7 8 9 10 11 12 13 14 15 16
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 */

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/errno.h>
17
#include <linux/clk.h>
18 19
#include <linux/err.h>
#include <linux/mutex.h>
20
#include <linux/io.h>
21
#include <linux/delay.h>
22

23
#include <mach/hardware.h>
24

25
#include <mach/clock.h>
26
#include <mach/psc.h>
27
#include <mach/cputype.h>
28 29 30 31 32 33
#include "clock.h"

static LIST_HEAD(clocks);
static DEFINE_MUTEX(clocks_mutex);
static DEFINE_SPINLOCK(clockfw_lock);

34
static unsigned psc_domain(struct clk *clk)
35
{
36 37 38
	return (clk->flags & PSC_DSP)
		? DAVINCI_GPSC_DSPDOMAIN
		: DAVINCI_GPSC_ARMDOMAIN;
39 40
}

41
static void __clk_enable(struct clk *clk)
42
{
43 44 45
	if (clk->parent)
		__clk_enable(clk->parent);
	if (clk->usecount++ == 0 && (clk->flags & CLK_PSC))
46 47
		davinci_psc_config(psc_domain(clk), clk->gpsc, clk->lpsc,
				PSC_STATE_ENABLE);
48 49 50 51
}

static void __clk_disable(struct clk *clk)
{
52
	if (WARN_ON(clk->usecount == 0))
53
		return;
54 55
	if (--clk->usecount == 0 && !(clk->flags & CLK_PLL) &&
	    (clk->flags & CLK_PSC))
56 57 58
		davinci_psc_config(psc_domain(clk), clk->gpsc, clk->lpsc,
				(clk->flags & PSC_SWRSTDISABLE) ?
				PSC_STATE_SWRSTDISABLE : PSC_STATE_DISABLE);
59 60
	if (clk->parent)
		__clk_disable(clk->parent);
61 62 63 64 65 66 67 68 69
}

int clk_enable(struct clk *clk)
{
	unsigned long flags;

	if (clk == NULL || IS_ERR(clk))
		return -EINVAL;

70 71 72
	spin_lock_irqsave(&clockfw_lock, flags);
	__clk_enable(clk);
	spin_unlock_irqrestore(&clockfw_lock, flags);
73

74
	return 0;
75 76 77 78 79 80 81 82 83 84
}
EXPORT_SYMBOL(clk_enable);

void clk_disable(struct clk *clk)
{
	unsigned long flags;

	if (clk == NULL || IS_ERR(clk))
		return;

85 86 87
	spin_lock_irqsave(&clockfw_lock, flags);
	__clk_disable(clk);
	spin_unlock_irqrestore(&clockfw_lock, flags);
88 89 90 91 92 93 94 95
}
EXPORT_SYMBOL(clk_disable);

unsigned long clk_get_rate(struct clk *clk)
{
	if (clk == NULL || IS_ERR(clk))
		return -EINVAL;

96
	return clk->rate;
97 98 99 100 101 102 103 104
}
EXPORT_SYMBOL(clk_get_rate);

long clk_round_rate(struct clk *clk, unsigned long rate)
{
	if (clk == NULL || IS_ERR(clk))
		return -EINVAL;

105 106 107
	if (clk->round_rate)
		return clk->round_rate(clk, rate);

108
	return clk->rate;
109 110 111
}
EXPORT_SYMBOL(clk_round_rate);

112 113 114 115 116 117 118 119 120 121 122 123
/* Propagate rate to children */
static void propagate_rate(struct clk *root)
{
	struct clk *clk;

	list_for_each_entry(clk, &root->children, childnode) {
		if (clk->recalc)
			clk->rate = clk->recalc(clk);
		propagate_rate(clk);
	}
}

124 125
int clk_set_rate(struct clk *clk, unsigned long rate)
{
126 127 128
	unsigned long flags;
	int ret = -EINVAL;

129
	if (clk == NULL || IS_ERR(clk))
130 131 132 133
		return ret;

	if (clk->set_rate)
		ret = clk->set_rate(clk, rate);
134 135

	spin_lock_irqsave(&clockfw_lock, flags);
136 137 138 139 140 141
	if (ret == 0) {
		if (clk->recalc)
			clk->rate = clk->recalc(clk);
		propagate_rate(clk);
	}
	spin_unlock_irqrestore(&clockfw_lock, flags);
142

143
	return ret;
144 145 146
}
EXPORT_SYMBOL(clk_set_rate);

147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
int clk_set_parent(struct clk *clk, struct clk *parent)
{
	unsigned long flags;

	if (clk == NULL || IS_ERR(clk))
		return -EINVAL;

	/* Cannot change parent on enabled clock */
	if (WARN_ON(clk->usecount))
		return -EINVAL;

	mutex_lock(&clocks_mutex);
	clk->parent = parent;
	list_del_init(&clk->childnode);
	list_add(&clk->childnode, &clk->parent->children);
	mutex_unlock(&clocks_mutex);

	spin_lock_irqsave(&clockfw_lock, flags);
	if (clk->recalc)
		clk->rate = clk->recalc(clk);
	propagate_rate(clk);
	spin_unlock_irqrestore(&clockfw_lock, flags);

	return 0;
}
EXPORT_SYMBOL(clk_set_parent);

174 175 176 177 178
int clk_register(struct clk *clk)
{
	if (clk == NULL || IS_ERR(clk))
		return -EINVAL;

179 180 181 182 183
	if (WARN(clk->parent && !clk->parent->rate,
			"CLK: %s parent %s has no rate!\n",
			clk->name, clk->parent->name))
		return -EINVAL;

184 185
	INIT_LIST_HEAD(&clk->children);

186
	mutex_lock(&clocks_mutex);
187
	list_add_tail(&clk->node, &clocks);
188 189
	if (clk->parent)
		list_add_tail(&clk->childnode, &clk->parent->children);
190 191
	mutex_unlock(&clocks_mutex);

192 193 194 195
	/* If rate is already set, use it */
	if (clk->rate)
		return 0;

196 197 198 199
	/* Else, see if there is a way to calculate it */
	if (clk->recalc)
		clk->rate = clk->recalc(clk);

200
	/* Otherwise, default to parent rate */
201
	else if (clk->parent)
202 203
		clk->rate = clk->parent->rate;

204 205 206 207 208 209 210 211 212 213 214
	return 0;
}
EXPORT_SYMBOL(clk_register);

void clk_unregister(struct clk *clk)
{
	if (clk == NULL || IS_ERR(clk))
		return;

	mutex_lock(&clocks_mutex);
	list_del(&clk->node);
215
	list_del(&clk->childnode);
216 217 218 219
	mutex_unlock(&clocks_mutex);
}
EXPORT_SYMBOL(clk_unregister);

220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
#ifdef CONFIG_DAVINCI_RESET_CLOCKS
/*
 * Disable any unused clocks left on by the bootloader
 */
static int __init clk_disable_unused(void)
{
	struct clk *ck;

	spin_lock_irq(&clockfw_lock);
	list_for_each_entry(ck, &clocks, node) {
		if (ck->usecount > 0)
			continue;
		if (!(ck->flags & CLK_PSC))
			continue;

		/* ignore if in Disabled or SwRstDisable states */
236
		if (!davinci_psc_is_clk_active(ck->gpsc, ck->lpsc))
237 238 239
			continue;

		pr_info("Clocks: disable unused %s\n", ck->name);
240 241 242 243

		davinci_psc_config(psc_domain(ck), ck->gpsc, ck->lpsc,
				(ck->flags & PSC_SWRSTDISABLE) ?
				PSC_STATE_SWRSTDISABLE : PSC_STATE_DISABLE);
244
	}
245 246 247 248 249 250
	spin_unlock_irq(&clockfw_lock);

	return 0;
}
late_initcall(clk_disable_unused);
#endif
251

252
static unsigned long clk_sysclk_recalc(struct clk *clk)
253
{
254 255
	u32 v, plldiv;
	struct pll_data *pll;
256
	unsigned long rate = clk->rate;
257 258 259

	/* If this is the PLL base clock, no more calculations needed */
	if (clk->pll_data)
260
		return rate;
261 262

	if (WARN_ON(!clk->parent))
263
		return rate;
264

265
	rate = clk->parent->rate;
266 267 268

	/* Otherwise, the parent must be a PLL */
	if (WARN_ON(!clk->parent->pll_data))
269
		return rate;
270 271 272 273 274

	pll = clk->parent->pll_data;

	/* If pre-PLL, source clock is before the multiplier and divider(s) */
	if (clk->flags & PRE_PLL)
275
		rate = pll->input_rate;
276 277

	if (!clk->div_reg)
278
		return rate;
279 280 281

	v = __raw_readl(pll->base + clk->div_reg);
	if (v & PLLDIV_EN) {
282
		plldiv = (v & pll->div_ratio_mask) + 1;
283
		if (plldiv)
284
			rate /= plldiv;
285
	}
286 287 288 289 290 291 292 293 294 295

	return rate;
}

static unsigned long clk_leafclk_recalc(struct clk *clk)
{
	if (WARN_ON(!clk->parent))
		return clk->rate;

	return clk->parent->rate;
296 297
}

298
static unsigned long clk_pllclk_recalc(struct clk *clk)
299 300 301 302
{
	u32 ctrl, mult = 1, prediv = 1, postdiv = 1;
	u8 bypass;
	struct pll_data *pll = clk->pll_data;
303
	unsigned long rate = clk->rate;
304 305

	ctrl = __raw_readl(pll->base + PLLCTL);
306
	rate = pll->input_rate = clk->parent->rate;
307 308 309 310

	if (ctrl & PLLCTL_PLLEN) {
		bypass = 0;
		mult = __raw_readl(pll->base + PLLM);
311 312 313 314
		if (cpu_is_davinci_dm365())
			mult = 2 * (mult & PLLM_PLLM_MASK);
		else
			mult = (mult & PLLM_PLLM_MASK) + 1;
315 316 317 318 319 320
	} else
		bypass = 1;

	if (pll->flags & PLL_HAS_PREDIV) {
		prediv = __raw_readl(pll->base + PREDIV);
		if (prediv & PLLDIV_EN)
321
			prediv = (prediv & pll->div_ratio_mask) + 1;
322 323 324 325 326 327 328 329 330 331 332
		else
			prediv = 1;
	}

	/* pre-divider is fixed, but (some?) chips won't report that */
	if (cpu_is_davinci_dm355() && pll->num == 1)
		prediv = 8;

	if (pll->flags & PLL_HAS_POSTDIV) {
		postdiv = __raw_readl(pll->base + POSTDIV);
		if (postdiv & PLLDIV_EN)
333
			postdiv = (postdiv & pll->div_ratio_mask) + 1;
334 335 336 337 338
		else
			postdiv = 1;
	}

	if (!bypass) {
339 340 341
		rate /= prediv;
		rate *= mult;
		rate /= postdiv;
342 343 344 345 346 347 348 349 350 351 352 353
	}

	pr_debug("PLL%d: input = %lu MHz [ ",
		 pll->num, clk->parent->rate / 1000000);
	if (bypass)
		pr_debug("bypass ");
	if (prediv > 1)
		pr_debug("/ %d ", prediv);
	if (mult > 1)
		pr_debug("* %d ", mult);
	if (postdiv > 1)
		pr_debug("/ %d ", postdiv);
354 355 356
	pr_debug("] --> %lu MHz output.\n", rate / 1000000);

	return rate;
357 358
}

359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
/**
 * davinci_set_pllrate - set the output rate of a given PLL.
 *
 * Note: Currently tested to work with OMAP-L138 only.
 *
 * @pll: pll whose rate needs to be changed.
 * @prediv: The pre divider value. Passing 0 disables the pre-divider.
 * @pllm: The multiplier value. Passing 0 leads to multiply-by-one.
 * @postdiv: The post divider value. Passing 0 disables the post-divider.
 */
int davinci_set_pllrate(struct pll_data *pll, unsigned int prediv,
					unsigned int mult, unsigned int postdiv)
{
	u32 ctrl;
	unsigned int locktime;
374
	unsigned long flags;
375 376 377 378 379 380 381 382 383 384 385 386 387

	if (pll->base == NULL)
		return -EINVAL;

	/*
	 *  PLL lock time required per OMAP-L138 datasheet is
	 * (2000 * prediv)/sqrt(pllm) OSCIN cycles. We approximate sqrt(pllm)
	 * as 4 and OSCIN cycle as 25 MHz.
	 */
	if (prediv) {
		locktime = ((2000 * prediv) / 100);
		prediv = (prediv - 1) | PLLDIV_EN;
	} else {
388
		locktime = PLL_LOCK_TIME;
389 390 391 392 393 394
	}
	if (postdiv)
		postdiv = (postdiv - 1) | PLLDIV_EN;
	if (mult)
		mult = mult - 1;

395 396 397
	/* Protect against simultaneous calls to PLL setting seqeunce */
	spin_lock_irqsave(&clockfw_lock, flags);

398 399 400 401 402 403
	ctrl = __raw_readl(pll->base + PLLCTL);

	/* Switch the PLL to bypass mode */
	ctrl &= ~(PLLCTL_PLLENSRC | PLLCTL_PLLEN);
	__raw_writel(ctrl, pll->base + PLLCTL);

404
	udelay(PLL_BYPASS_TIME);
405 406 407 408 409 410 411 412 413 414 415 416 417

	/* Reset and enable PLL */
	ctrl &= ~(PLLCTL_PLLRST | PLLCTL_PLLDIS);
	__raw_writel(ctrl, pll->base + PLLCTL);

	if (pll->flags & PLL_HAS_PREDIV)
		__raw_writel(prediv, pll->base + PREDIV);

	__raw_writel(mult, pll->base + PLLM);

	if (pll->flags & PLL_HAS_POSTDIV)
		__raw_writel(postdiv, pll->base + POSTDIV);

418
	udelay(PLL_RESET_TIME);
419 420 421 422 423 424 425 426 427 428 429

	/* Bring PLL out of reset */
	ctrl |= PLLCTL_PLLRST;
	__raw_writel(ctrl, pll->base + PLLCTL);

	udelay(locktime);

	/* Remove PLL from bypass mode */
	ctrl |= PLLCTL_PLLEN;
	__raw_writel(ctrl, pll->base + PLLCTL);

430 431
	spin_unlock_irqrestore(&clockfw_lock, flags);

432 433 434 435
	return 0;
}
EXPORT_SYMBOL(davinci_set_pllrate);

436
int __init davinci_clk_init(struct clk_lookup *clocks)
437
  {
438
	struct clk_lookup *c;
439
	struct clk *clk;
440
	size_t num_clocks = 0;
441

442 443
	for (c = clocks; c->clk; c++) {
		clk = c->clk;
444

445 446 447 448 449 450 451 452 453 454 455 456 457 458
		if (!clk->recalc) {

			/* Check if clock is a PLL */
			if (clk->pll_data)
				clk->recalc = clk_pllclk_recalc;

			/* Else, if it is a PLL-derived clock */
			else if (clk->flags & CLK_PLL)
				clk->recalc = clk_sysclk_recalc;

			/* Otherwise, it is a leaf clock (PSC clock) */
			else if (clk->parent)
				clk->recalc = clk_leafclk_recalc;
		}
459

C
Cyril Chemparathy 已提交
460 461 462 463 464 465 466 467 468 469 470
		if (clk->pll_data) {
			struct pll_data *pll = clk->pll_data;

			if (!pll->div_ratio_mask)
				pll->div_ratio_mask = PLLDIV_RATIO_MASK;

			if (pll->phys_base && !pll->base) {
				pll->base = ioremap(pll->phys_base, SZ_4K);
				WARN_ON(!pll->base);
			}
		}
471

472 473
		if (clk->recalc)
			clk->rate = clk->recalc(clk);
474 475 476 477 478

		if (clk->lpsc)
			clk->flags |= CLK_PSC;

		clk_register(clk);
479
		num_clocks++;
480 481 482 483

		/* Turn on clocks that Linux doesn't otherwise manage */
		if (clk->flags & ALWAYS_ENABLED)
			clk_enable(clk);
484 485
	}

486 487
	clkdev_add_table(clocks, num_clocks);

488 489 490
	return 0;
}

491
#ifdef CONFIG_DEBUG_FS
492

493 494
#include <linux/debugfs.h>
#include <linux/seq_file.h>
495

496 497 498 499 500 501
#define CLKNAME_MAX	10		/* longest clock name */
#define NEST_DELTA	2
#define NEST_MAX	4

static void
dump_clock(struct seq_file *s, unsigned nest, struct clk *parent)
502
{
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526
	char		*state;
	char		buf[CLKNAME_MAX + NEST_DELTA * NEST_MAX];
	struct clk	*clk;
	unsigned	i;

	if (parent->flags & CLK_PLL)
		state = "pll";
	else if (parent->flags & CLK_PSC)
		state = "psc";
	else
		state = "";

	/* <nest spaces> name <pad to end> */
	memset(buf, ' ', sizeof(buf) - 1);
	buf[sizeof(buf) - 1] = 0;
	i = strlen(parent->name);
	memcpy(buf + nest, parent->name,
			min(i, (unsigned)(sizeof(buf) - 1 - nest)));

	seq_printf(s, "%s users=%2d %-3s %9ld Hz\n",
		   buf, parent->usecount, state, clk_get_rate(parent));
	/* REVISIT show device associations too */

	/* cost is now small, but not linear... */
527 528
	list_for_each_entry(clk, &parent->children, childnode) {
		dump_clock(s, nest + NEST_DELTA, clk);
529 530
	}
}
531

532 533
static int davinci_ck_show(struct seq_file *m, void *v)
{
534 535 536 537
	struct clk *clk;

	/*
	 * Show clock tree; We trust nonzero usecounts equate to PSC enables...
538 539
	 */
	mutex_lock(&clocks_mutex);
540 541 542
	list_for_each_entry(clk, &clocks, node)
		if (!clk->parent)
			dump_clock(m, 0, clk);
543
	mutex_unlock(&clocks_mutex);
544 545 546 547 548 549

	return 0;
}

static int davinci_ck_open(struct inode *inode, struct file *file)
{
550
	return single_open(file, davinci_ck_show, NULL);
551 552
}

553
static const struct file_operations davinci_ck_operations = {
554 555 556
	.open		= davinci_ck_open,
	.read		= seq_read,
	.llseek		= seq_lseek,
557
	.release	= single_release,
558 559
};

560
static int __init davinci_clk_debugfs_init(void)
561
{
562 563
	debugfs_create_file("davinci_clocks", S_IFREG | S_IRUGO, NULL, NULL,
						&davinci_ck_operations);
564 565 566
	return 0;

}
567 568
device_initcall(davinci_clk_debugfs_init);
#endif /* CONFIG_DEBUG_FS */