clock-sh7722.c 20.6 KB
Newer Older
1 2 3
/*
 * arch/sh/kernel/cpu/sh4a/clock-sh7722.c
 *
4
 * SH7343, SH7722, SH7723 & SH7366 support for the clock framework
5 6 7 8 9 10 11 12 13 14 15 16
 *
 * Copyright (c) 2006-2007 Nomad Global Solutions Inc
 * Based on code for sh7343 by Paul Mundt
 *
 * This file is subject to the terms and conditions of the GNU General Public
 * License.  See the file "COPYING" in the main directory of this archive
 * for more details.
 */
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/io.h>
#include <linux/errno.h>
17
#include <linux/stringify.h>
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
#include <asm/clock.h>
#include <asm/freq.h>

#define N  (-1)
#define NM (-2)
#define ROUND_NEAREST 0
#define ROUND_DOWN -1
#define ROUND_UP   +1

static int adjust_algos[][3] = {
	{},	/* NO_CHANGE */
	{ NM, N, 1 },   /* N:1, N:1 */
	{ 3, 2, 2 },	/* 3:2:2 */
	{ 5, 2, 2 },    /* 5:2:2 */
	{ N, 1, 1 },	/* N:1:1 */

	{ N, 1 },	/* N:1 */

	{ N, 1 },	/* N:1 */
	{ 3, 2 },
	{ 4, 3 },
	{ 5, 4 },

	{ N, 1 }
};

static unsigned long adjust_pair_of_clocks(unsigned long r1, unsigned long r2,
			int m1, int m2, int round_flag)
{
	unsigned long rem, div;
	int the_one = 0;

	pr_debug( "Actual values: r1 = %ld\n", r1);
	pr_debug( "...............r2 = %ld\n", r2);

	if (m1 == m2) {
		r2 = r1;
		pr_debug( "setting equal rates: r2 now %ld\n", r2);
	} else if ((m2 == N  && m1 == 1) ||
		   (m2 == NM && m1 == N)) { /* N:1 or NM:N */
		pr_debug( "Setting rates as 1:N (N:N*M)\n");
		rem = r2 % r1;
		pr_debug( "...remainder = %ld\n", rem);
		if (rem) {
			div = r2 / r1;
			pr_debug( "...div = %ld\n", div);
			switch (round_flag) {
			case ROUND_NEAREST:
				the_one = rem >= r1/2 ? 1 : 0; break;
			case ROUND_UP:
				the_one = 1; break;
			case ROUND_DOWN:
				the_one = 0; break;
			}

			r2 = r1 * (div + the_one);
			pr_debug( "...setting r2 to %ld\n", r2);
		}
	} else if ((m2 == 1  && m1 == N) ||
		   (m2 == N && m1 == NM)) { /* 1:N or N:NM */
		pr_debug( "Setting rates as N:1 (N*M:N)\n");
		rem = r1 % r2;
		pr_debug( "...remainder = %ld\n", rem);
		if (rem) {
			div = r1 / r2;
			pr_debug( "...div = %ld\n", div);
			switch (round_flag) {
			case ROUND_NEAREST:
				the_one = rem > r2/2 ? 1 : 0; break;
			case ROUND_UP:
				the_one = 0; break;
			case ROUND_DOWN:
				the_one = 1; break;
			}

			r2 = r1 / (div + the_one);
			pr_debug( "...setting r2 to %ld\n", r2);
		}
	} else { /* value:value */
		pr_debug( "Setting rates as %d:%d\n", m1, m2);
		div = r1 / m1;
		r2 = div * m2;
		pr_debug( "...div = %ld\n", div);
		pr_debug( "...setting r2 to %ld\n", r2);
	}

	return r2;
}

static void adjust_clocks(int originate, int *l, unsigned long v[],
			  int n_in_line)
{
	int x;

	pr_debug( "Go down from %d...\n", originate);
	/* go up recalculation clocks */
	for (x = originate; x>0; x -- )
		v[x-1] = adjust_pair_of_clocks(v[x], v[x-1],
					l[x], l[x-1],
					ROUND_UP);

	pr_debug( "Go up from %d...\n", originate);
	/* go down recalculation clocks */
	for (x = originate; x<n_in_line - 1; x ++ )
		v[x+1] = adjust_pair_of_clocks(v[x], v[x+1],
					l[x], l[x+1],
					ROUND_UP);
}


/*
 * SH7722 uses a common set of multipliers and divisors, so this
 * is quite simple..
 */

/*
 * Instead of having two separate multipliers/divisors set, like this:
 *
 * static int multipliers[] = { 1, 2, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
 * static int divisors[] = { 1, 3, 2, 5, 3, 4, 5, 6, 8, 10, 12, 16, 20 };
 *
 * I created the divisors2 array, which is used to calculate rate like
 *   rate = parent * 2 / divisors2[ divisor ];
*/
static int divisors2[] = { 2, 3, 4, 5, 6, 8, 10, 12, 16, 20, 24, 32, 40 };

144 145 146 147 148 149 150
static void master_clk_recalc(struct clk *clk)
{
	unsigned frqcr = ctrl_inl(FRQCR);

	clk->rate = CONFIG_SH_PCLK_FREQ * (((frqcr >> 24) & 0x1f) + 1);
}

151 152
static void master_clk_init(struct clk *clk)
{
153 154 155 156
	clk->parent = NULL;
	clk->flags |= CLK_RATE_PROPAGATES;
	clk->rate = CONFIG_SH_PCLK_FREQ;
	master_clk_recalc(clk);
157 158
}

159 160

static void module_clk_recalc(struct clk *clk)
161 162 163
{
	unsigned long frqcr = ctrl_inl(FRQCR);

164
	clk->rate = clk->parent->rate / (((frqcr >> 24) & 0x1f) + 1);
165 166 167 168
}

static int master_clk_setrate(struct clk *clk, unsigned long rate, int id)
{
169
	int div = rate / clk->rate;
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
	int master_divs[] = { 2, 3, 4, 6, 8, 16 };
	int index;
	unsigned long frqcr;

	for (index = 1; index < ARRAY_SIZE(master_divs); index++)
		if (div >= master_divs[index - 1] && div < master_divs[index])
			break;

	if (index >= ARRAY_SIZE(master_divs))
		index = ARRAY_SIZE(master_divs);
	div = master_divs[index - 1];

	frqcr = ctrl_inl(FRQCR);
	frqcr &= ~(0xF << 24);
	frqcr |= ( (div-1) << 24);
	ctrl_outl(frqcr, FRQCR);

	return 0;
}

static struct clk_ops sh7722_master_clk_ops = {
	.init = master_clk_init,
	.recalc = master_clk_recalc,
	.set_rate = master_clk_setrate,
};

196 197 198 199
static struct clk_ops sh7722_module_clk_ops = {
       .recalc = module_clk_recalc,
};

200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
struct frqcr_context {
	unsigned mask;
	unsigned shift;
};

struct frqcr_context sh7722_get_clk_context(const char *name)
{
	struct frqcr_context ctx = { 0, };

	if (!strcmp(name, "peripheral_clk")) {
		ctx.shift = 0;
		ctx.mask = 0xF;
	} else if (!strcmp(name, "sdram_clk")) {
		ctx.shift = 4;
		ctx.mask = 0xF;
	} else if (!strcmp(name, "bus_clk")) {
		ctx.shift = 8;
		ctx.mask = 0xF;
	} else if (!strcmp(name, "sh_clk")) {
		ctx.shift = 12;
		ctx.mask = 0xF;
	} else if (!strcmp(name, "umem_clk")) {
		ctx.shift = 16;
		ctx.mask = 0xF;
	} else if (!strcmp(name, "cpu_clk")) {
		ctx.shift = 20;
		ctx.mask = 7;
	}
	return ctx;
}

/**
232
 * sh7722_find_div_index - find divisor for setting rate
233 234 235 236 237 238 239 240
 *
 * All sh7722 clocks use the same set of multipliers/divisors. This function
 * chooses correct divisor to set the rate of clock with parent clock that
 * generates frequency of 'parent_rate'
 *
 * @parent_rate: rate of parent clock
 * @rate: requested rate to be set
 */
241
static int sh7722_find_div_index(unsigned long parent_rate, unsigned rate)
242 243 244 245 246 247 248 249
{
	unsigned div2 = parent_rate * 2 / rate;
	int index;

	if (rate > parent_rate)
		return -EINVAL;

	for (index = 1; index < ARRAY_SIZE(divisors2); index++) {
250
		if (div2 > divisors2[index - 1] && div2 <= divisors2[index])
251 252 253 254
			break;
	}
	if (index >= ARRAY_SIZE(divisors2))
		index = ARRAY_SIZE(divisors2) - 1;
255
	return index;
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
}

static void sh7722_frqcr_recalc(struct clk *clk)
{
	struct frqcr_context ctx = sh7722_get_clk_context(clk->name);
	unsigned long frqcr = ctrl_inl(FRQCR);
	int index;

	index = (frqcr >> ctx.shift) & ctx.mask;
	clk->rate = clk->parent->rate * 2 / divisors2[index];
}

static int sh7722_frqcr_set_rate(struct clk *clk, unsigned long rate,
				 int algo_id)
{
	struct frqcr_context ctx = sh7722_get_clk_context(clk->name);
	unsigned long parent_rate = clk->parent->rate;
	int div;
	unsigned long frqcr;
	int err = 0;

	/* pretty invalid */
	if (parent_rate < rate)
		return -EINVAL;

	/* look for multiplier/divisor pair */
282
	div = sh7722_find_div_index(parent_rate, rate);
283 284 285 286
	if (div<0)
		return div;

	/* calculate new value of clock rate */
287
	clk->rate = parent_rate * 2 / divisors2[div];
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
	frqcr = ctrl_inl(FRQCR);

	/* FIXME: adjust as algo_id specifies */
	if (algo_id != NO_CHANGE) {
		int originator;
		char *algo_group_1[] = { "cpu_clk", "umem_clk", "sh_clk" };
		char *algo_group_2[] = { "sh_clk", "bus_clk" };
		char *algo_group_3[] = { "sh_clk", "sdram_clk" };
		char *algo_group_4[] = { "bus_clk", "peripheral_clk" };
		char *algo_group_5[] = { "cpu_clk", "peripheral_clk" };
		char **algo_current = NULL;
		/* 3 is the maximum number of clocks in relation */
		struct clk *ck[3];
		unsigned long values[3]; /* the same comment as above */
		int part_length = -1;
		int i;

		/*
		 * all the steps below only required if adjustion was
		 * requested
		 */
		if (algo_id == IUS_N1_N1 ||
		    algo_id == IUS_322 ||
		    algo_id == IUS_522 ||
		    algo_id == IUS_N11) {
			algo_current = algo_group_1;
			part_length = 3;
		}
		if (algo_id == SB_N1) {
			algo_current = algo_group_2;
			part_length = 2;
		}
		if (algo_id == SB3_N1 ||
		    algo_id == SB3_32 ||
		    algo_id == SB3_43 ||
		    algo_id == SB3_54) {
			algo_current = algo_group_3;
			part_length = 2;
		}
		if (algo_id == BP_N1) {
			algo_current = algo_group_4;
			part_length = 2;
		}
		if (algo_id == IP_N1) {
			algo_current = algo_group_5;
			part_length = 2;
		}
		if (!algo_current)
			goto incorrect_algo_id;

		originator = -1;
		for (i = 0; i < part_length; i ++ ) {
			if (originator >= 0 && !strcmp(clk->name,
						       algo_current[i]))
				originator = i;
			ck[i] = clk_get(NULL, algo_current[i]);
			values[i] = clk_get_rate(ck[i]);
		}

		if (originator >= 0)
			adjust_clocks(originator, adjust_algos[algo_id],
				      values, part_length);

		for (i = 0; i < part_length; i ++ ) {
			struct frqcr_context part_ctx;
			int part_div;

			if (likely(!err)) {
356
				part_div = sh7722_find_div_index(parent_rate,
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
								rate);
				if (part_div > 0) {
					part_ctx = sh7722_get_clk_context(
								ck[i]->name);
					frqcr &= ~(part_ctx.mask <<
						   part_ctx.shift);
					frqcr |= part_div << part_ctx.shift;
				} else
					err = part_div;
			}

			ck[i]->ops->recalc(ck[i]);
			clk_put(ck[i]);
		}
	}

	/* was there any error during recalculation ? If so, bail out.. */
	if (unlikely(err!=0))
		goto out_err;

	/* clear FRQCR bits */
	frqcr &= ~(ctx.mask << ctx.shift);
	frqcr |= div << ctx.shift;

	/* ...and perform actual change */
	ctrl_outl(frqcr, FRQCR);
	return 0;

incorrect_algo_id:
	return -EINVAL;
out_err:
	return err;
}

391 392 393 394 395 396
static long sh7722_frqcr_round_rate(struct clk *clk, unsigned long rate)
{
	unsigned long parent_rate = clk->parent->rate;
	int div;

	/* look for multiplier/divisor pair */
397
	div = sh7722_find_div_index(parent_rate, rate);
398 399 400 401
	if (div < 0)
		return clk->rate;

	/* calculate new value of clock rate */
402
	return parent_rate * 2 / divisors2[div];
403 404
}

405 406 407
static struct clk_ops sh7722_frqcr_clk_ops = {
	.recalc = sh7722_frqcr_recalc,
	.set_rate = sh7722_frqcr_set_rate,
408
	.round_rate = sh7722_frqcr_round_rate,
409 410 411 412 413 414 415
};

/*
 * clock ops methods for SIU A/B and IrDA clock
 *
 */

416 417 418 419 420 421 422 423
#ifndef CONFIG_CPU_SUBTYPE_SH7343

static int sh7722_siu_set_rate(struct clk *clk, unsigned long rate, int algo_id)
{
	unsigned long r;
	int div;

	r = ctrl_inl(clk->arch_flags);
424
	div = sh7722_find_div_index(clk->parent->rate, rate);
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
	if (div < 0)
		return div;
	r = (r & ~0xF) | div;
	ctrl_outl(r, clk->arch_flags);
	return 0;
}

static void sh7722_siu_recalc(struct clk *clk)
{
	unsigned long r;

	r = ctrl_inl(clk->arch_flags);
	clk->rate = clk->parent->rate * 2 / divisors2[r & 0xF];
}

440 441 442 443
static int sh7722_siu_start_stop(struct clk *clk, int enable)
{
	unsigned long r;

444
	r = ctrl_inl(clk->arch_flags);
445
	if (enable)
446
		ctrl_outl(r & ~(1 << 8), clk->arch_flags);
447
	else
448
		ctrl_outl(r | (1 << 8), clk->arch_flags);
449 450 451 452 453 454 455 456 457 458 459 460 461
	return 0;
}

static void sh7722_siu_enable(struct clk *clk)
{
	sh7722_siu_start_stop(clk, 1);
}

static void sh7722_siu_disable(struct clk *clk)
{
	sh7722_siu_start_stop(clk, 0);
}

462 463 464 465 466 467 468 469 470
static struct clk_ops sh7722_siu_clk_ops = {
	.recalc = sh7722_siu_recalc,
	.set_rate = sh7722_siu_set_rate,
	.enable = sh7722_siu_enable,
	.disable = sh7722_siu_disable,
};

#endif /* CONFIG_CPU_SUBTYPE_SH7343 */

471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 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 513 514 515 516 517 518
static void sh7722_video_enable(struct clk *clk)
{
	unsigned long r;

	r = ctrl_inl(VCLKCR);
	ctrl_outl( r & ~(1<<8), VCLKCR);
}

static void sh7722_video_disable(struct clk *clk)
{
	unsigned long r;

	r = ctrl_inl(VCLKCR);
	ctrl_outl( r | (1<<8), VCLKCR);
}

static int sh7722_video_set_rate(struct clk *clk, unsigned long rate,
				 int algo_id)
{
	unsigned long r;

	r = ctrl_inl(VCLKCR);
	r &= ~0x3F;
	r |= ((clk->parent->rate / rate - 1) & 0x3F);
	ctrl_outl(r, VCLKCR);
	return 0;
}

static void sh7722_video_recalc(struct clk *clk)
{
	unsigned long r;

	r = ctrl_inl(VCLKCR);
	clk->rate = clk->parent->rate / ((r & 0x3F) + 1);
}

static struct clk_ops sh7722_video_clk_ops = {
	.recalc = sh7722_video_recalc,
	.set_rate = sh7722_video_set_rate,
	.enable = sh7722_video_enable,
	.disable = sh7722_video_disable,
};
/*
 * and at last, clock definitions themselves
 */
static struct clk sh7722_umem_clock = {
	.name = "umem_clk",
	.ops = &sh7722_frqcr_clk_ops,
519
	.flags = CLK_RATE_PROPAGATES,
520 521 522 523 524
};

static struct clk sh7722_sh_clock = {
	.name = "sh_clk",
	.ops = &sh7722_frqcr_clk_ops,
525
	.flags = CLK_RATE_PROPAGATES,
526 527 528 529 530
};

static struct clk sh7722_peripheral_clock = {
	.name = "peripheral_clk",
	.ops = &sh7722_frqcr_clk_ops,
531
	.flags = CLK_RATE_PROPAGATES,
532 533 534 535 536 537 538
};

static struct clk sh7722_sdram_clock = {
	.name = "sdram_clk",
	.ops = &sh7722_frqcr_clk_ops,
};

539 540 541 542 543
static struct clk sh7722_r_clock = {
	.name = "r_clk",
	.rate = 32768,
	.flags = CLK_RATE_PROPAGATES,
};
544 545 546

#ifndef CONFIG_CPU_SUBTYPE_SH7343

547 548 549 550 551 552 553
/*
 * these three clocks - SIU A, SIU B, IrDA - share the same clk_ops
 * methods of clk_ops determine which register they should access by
 * examining clk->name field
 */
static struct clk sh7722_siu_a_clock = {
	.name = "siu_a_clk",
554
	.arch_flags = SCLKACR,
555 556 557 558 559
	.ops = &sh7722_siu_clk_ops,
};

static struct clk sh7722_siu_b_clock = {
	.name = "siu_b_clk",
560
	.arch_flags = SCLKBCR,
561 562 563
	.ops = &sh7722_siu_clk_ops,
};

564
#if defined(CONFIG_CPU_SUBTYPE_SH7722)
565 566
static struct clk sh7722_irda_clock = {
	.name = "irda_clk",
567
	.arch_flags = IrDACLKCR,
568 569
	.ops = &sh7722_siu_clk_ops,
};
570
#endif
571
#endif /* CONFIG_CPU_SUBTYPE_SH7343 */
572 573 574 575 576 577

static struct clk sh7722_video_clock = {
	.name = "video_clk",
	.ops = &sh7722_video_clk_ops,
};

578 579 580 581 582
#define MSTPCR_ARCH_FLAGS(reg, bit) (((reg) << 8) | (bit))
#define MSTPCR_ARCH_FLAGS_REG(value) ((value) >> 8)
#define MSTPCR_ARCH_FLAGS_BIT(value) ((value) & 0xff)

static int sh7722_mstpcr_start_stop(struct clk *clk, int enable)
583
{
584 585
	unsigned long bit = MSTPCR_ARCH_FLAGS_BIT(clk->arch_flags);
	unsigned long reg;
586 587
	unsigned long r;

588 589 590 591 592 593 594 595 596 597 598 599 600 601
	switch(MSTPCR_ARCH_FLAGS_REG(clk->arch_flags)) {
	case 0:
		reg = MSTPCR0;
		break;
	case 1:
		reg = MSTPCR1;
		break;
	case 2:
		reg = MSTPCR2;
		break;
	default:
		return -EINVAL;
	}  

602 603 604 605 606 607 608 609 610 611 612
	r = ctrl_inl(reg);

	if (enable)
		r &= ~(1 << bit);
	else
		r |= (1 << bit);

	ctrl_outl(r, reg);
	return 0;
}

613
static void sh7722_mstpcr_enable(struct clk *clk)
614
{
615
	sh7722_mstpcr_start_stop(clk, 1);
616 617
}

618
static void sh7722_mstpcr_disable(struct clk *clk)
619
{
620
	sh7722_mstpcr_start_stop(clk, 0);
621 622
}

623 624 625 626 627 628
static void sh7722_mstpcr_recalc(struct clk *clk)
{
	if (clk->parent)
		clk->rate = clk->parent->rate;
}

629 630 631
static struct clk_ops sh7722_mstpcr_clk_ops = {
	.enable = sh7722_mstpcr_enable,
	.disable = sh7722_mstpcr_disable,
632
	.recalc = sh7722_mstpcr_recalc,
633 634 635 636 637
};

#define DECLARE_MSTPCRN(regnr, bitnr, bitstr)		\
{							\
	.name = "mstp" __stringify(regnr) bitstr,	\
638 639
	.arch_flags = MSTPCR_ARCH_FLAGS(regnr, bitnr),	\
	.ops = &sh7722_mstpcr_clk_ops,	\
640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681
}

#define DECLARE_MSTPCR(regnr) \
	DECLARE_MSTPCRN(regnr, 31, "31"), \
	DECLARE_MSTPCRN(regnr, 30, "30"), \
	DECLARE_MSTPCRN(regnr, 29, "29"), \
	DECLARE_MSTPCRN(regnr, 28, "28"), \
	DECLARE_MSTPCRN(regnr, 27, "27"), \
	DECLARE_MSTPCRN(regnr, 26, "26"), \
	DECLARE_MSTPCRN(regnr, 25, "25"), \
	DECLARE_MSTPCRN(regnr, 24, "24"), \
	DECLARE_MSTPCRN(regnr, 23, "23"), \
	DECLARE_MSTPCRN(regnr, 22, "22"), \
	DECLARE_MSTPCRN(regnr, 21, "21"), \
	DECLARE_MSTPCRN(regnr, 20, "20"), \
	DECLARE_MSTPCRN(regnr, 19, "19"), \
	DECLARE_MSTPCRN(regnr, 18, "18"), \
	DECLARE_MSTPCRN(regnr, 17, "17"), \
	DECLARE_MSTPCRN(regnr, 16, "16"), \
	DECLARE_MSTPCRN(regnr, 15, "15"), \
	DECLARE_MSTPCRN(regnr, 14, "14"), \
	DECLARE_MSTPCRN(regnr, 13, "13"), \
	DECLARE_MSTPCRN(regnr, 12, "12"), \
	DECLARE_MSTPCRN(regnr, 11, "11"), \
	DECLARE_MSTPCRN(regnr, 10, "10"), \
	DECLARE_MSTPCRN(regnr, 9, "09"), \
	DECLARE_MSTPCRN(regnr, 8, "08"), \
	DECLARE_MSTPCRN(regnr, 7, "07"), \
	DECLARE_MSTPCRN(regnr, 6, "06"), \
	DECLARE_MSTPCRN(regnr, 5, "05"), \
	DECLARE_MSTPCRN(regnr, 4, "04"), \
	DECLARE_MSTPCRN(regnr, 3, "03"), \
	DECLARE_MSTPCRN(regnr, 2, "02"), \
	DECLARE_MSTPCRN(regnr, 1, "01"), \
	DECLARE_MSTPCRN(regnr, 0, "00")

static struct clk sh7722_mstpcr[] = {
	DECLARE_MSTPCR(0),
	DECLARE_MSTPCR(1),
	DECLARE_MSTPCR(2),
};

682 683 684 685 686 687 688 689
#define MSTPCR(_name, _parent, regnr, bitnr) \
{						\
	.name = _name,				\
	.arch_flags = MSTPCR_ARCH_FLAGS(regnr, bitnr),	\
	.ops = (void *)_parent,		\
}

static struct clk sh7722_mstpcr_clocks[] = {
690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714
#if defined(CONFIG_CPU_SUBTYPE_SH7722)
	MSTPCR("uram0", "umem_clk", 0, 28),
	MSTPCR("xymem0", "bus_clk", 0, 26),
	MSTPCR("tmu0", "peripheral_clk", 0, 15),
	MSTPCR("cmt0", "r_clk", 0, 14),
	MSTPCR("rwdt0", "r_clk", 0, 13),
	MSTPCR("flctl0", "peripheral_clk", 0, 10),
	MSTPCR("scif0", "peripheral_clk", 0, 7),
	MSTPCR("scif1", "peripheral_clk", 0, 6),
	MSTPCR("scif2", "peripheral_clk", 0, 5),
	MSTPCR("i2c0", "peripheral_clk", 1, 9),
	MSTPCR("rtc0", "r_clk", 1, 8),
	MSTPCR("sdhi0", "peripheral_clk", 2, 18),
	MSTPCR("keysc0", "r_clk", 2, 14),
	MSTPCR("usbf0", "peripheral_clk", 2, 11),
	MSTPCR("2dg0", "bus_clk", 2, 9),
	MSTPCR("siu0", "bus_clk", 2, 8),
	MSTPCR("vou0", "bus_clk", 2, 5),
	MSTPCR("jpu0", "bus_clk", 2, 6),
	MSTPCR("beu0", "bus_clk", 2, 4),
	MSTPCR("ceu0", "bus_clk", 2, 3),
	MSTPCR("veu0", "bus_clk", 2, 2),
	MSTPCR("vpu0", "bus_clk", 2, 1),
	MSTPCR("lcdc0", "bus_clk", 2, 0),
#endif
715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764
#if defined(CONFIG_CPU_SUBTYPE_SH7723)
	/* See page 60 of Datasheet V1.0: Overview -> Block Diagram */
	MSTPCR("tlb0", "cpu_clk", 0, 31),
	MSTPCR("ic0", "cpu_clk", 0, 30),
	MSTPCR("oc0", "cpu_clk", 0, 29),
	MSTPCR("l2c0", "sh_clk", 0, 28),
	MSTPCR("ilmem0", "cpu_clk", 0, 27),
	MSTPCR("fpu0", "cpu_clk", 0, 24),
	MSTPCR("intc0", "cpu_clk", 0, 22),
	MSTPCR("dmac0", "bus_clk", 0, 21),
	MSTPCR("sh0", "sh_clk", 0, 20),
	MSTPCR("hudi0", "peripheral_clk", 0, 19),
	MSTPCR("ubc0", "cpu_clk", 0, 17),
	MSTPCR("tmu0", "peripheral_clk", 0, 15),
	MSTPCR("cmt0", "r_clk", 0, 14),
	MSTPCR("rwdt0", "r_clk", 0, 13),
	MSTPCR("dmac1", "bus_clk", 0, 12),
	MSTPCR("tmu1", "peripheral_clk", 0, 11),
	MSTPCR("flctl0", "peripheral_clk", 0, 10),
	MSTPCR("scif0", "peripheral_clk", 0, 9),
	MSTPCR("scif1", "peripheral_clk", 0, 8),
	MSTPCR("scif2", "peripheral_clk", 0, 7),
	MSTPCR("scif3", "bus_clk", 0, 6),
	MSTPCR("scif4", "bus_clk", 0, 5),
	MSTPCR("scif5", "bus_clk", 0, 4),
	MSTPCR("msiof0", "bus_clk", 0, 2),
	MSTPCR("msiof1", "bus_clk", 0, 1),
	MSTPCR("meram0", "sh_clk", 0, 0),
	MSTPCR("i2c0", "peripheral_clk", 1, 9),
	MSTPCR("rtc0", "r_clk", 1, 8),
	MSTPCR("atapi0", "sh_clk", 2, 28),
	MSTPCR("adc0", "peripheral_clk", 2, 28),
	MSTPCR("tpu0", "bus_clk", 2, 25),
	MSTPCR("irda0", "peripheral_clk", 2, 24),
	MSTPCR("tsif0", "bus_clk", 2, 22),
	MSTPCR("icb0", "bus_clk", 2, 21),
	MSTPCR("sdhi0", "bus_clk", 2, 18),
	MSTPCR("sdhi1", "bus_clk", 2, 17),
	MSTPCR("keysc0", "r_clk", 2, 14),
	MSTPCR("usb0", "bus_clk", 2, 11),
	MSTPCR("2dg0", "bus_clk", 2, 10),
	MSTPCR("siu0", "bus_clk", 2, 8),
	MSTPCR("veu1", "bus_clk", 2, 6),
	MSTPCR("vou0", "bus_clk", 2, 5),
	MSTPCR("beu0", "bus_clk", 2, 4),
	MSTPCR("ceu0", "bus_clk", 2, 3),
	MSTPCR("veu0", "bus_clk", 2, 2),
	MSTPCR("vpu0", "bus_clk", 2, 1),
	MSTPCR("lcdc0", "bus_clk", 2, 0),
#endif
765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788
#if defined(CONFIG_CPU_SUBTYPE_SH7343)
	MSTPCR("uram0", "umem_clk", 0, 28),
	MSTPCR("xymem0", "bus_clk", 0, 26),
	MSTPCR("tmu0", "peripheral_clk", 0, 15),
	MSTPCR("cmt0", "r_clk", 0, 14),
	MSTPCR("rwdt0", "r_clk", 0, 13),
	MSTPCR("scif0", "peripheral_clk", 0, 7),
	MSTPCR("scif1", "peripheral_clk", 0, 6),
	MSTPCR("scif2", "peripheral_clk", 0, 5),
	MSTPCR("scif3", "peripheral_clk", 0, 4),
	MSTPCR("i2c0", "peripheral_clk", 1, 9),
	MSTPCR("i2c1", "peripheral_clk", 1, 8),
	MSTPCR("sdhi0", "peripheral_clk", 2, 18),
	MSTPCR("keysc0", "r_clk", 2, 14),
	MSTPCR("usbf0", "peripheral_clk", 2, 11),
	MSTPCR("siu0", "bus_clk", 2, 8),
	MSTPCR("jpu0", "bus_clk", 2, 6),
	MSTPCR("vou0", "bus_clk", 2, 5),
	MSTPCR("beu0", "bus_clk", 2, 4),
	MSTPCR("ceu0", "bus_clk", 2, 3),
	MSTPCR("veu0", "bus_clk", 2, 2),
	MSTPCR("vpu0", "bus_clk", 2, 1),
	MSTPCR("lcdc0", "bus_clk", 2, 0),
#endif
789 790
};

791 792 793 794 795
static struct clk *sh7722_clocks[] = {
	&sh7722_umem_clock,
	&sh7722_sh_clock,
	&sh7722_peripheral_clock,
	&sh7722_sdram_clock,
796
#ifndef CONFIG_CPU_SUBTYPE_SH7343
797 798
	&sh7722_siu_a_clock,
	&sh7722_siu_b_clock,
799
#if defined(CONFIG_CPU_SUBTYPE_SH7722)
800
	&sh7722_irda_clock,
801
#endif
802
#endif
803 804 805 806 807 808 809 810
	&sh7722_video_clock,
};

/*
 * init in order: master, module, bus, cpu
 */
struct clk_ops *onchip_ops[] = {
	&sh7722_master_clk_ops,
811
	&sh7722_module_clk_ops,
812 813 814 815 816 817 818 819 820 821 822
	&sh7722_frqcr_clk_ops,
	&sh7722_frqcr_clk_ops,
};

void __init
arch_init_clk_ops(struct clk_ops **ops, int type)
{
	BUG_ON(type < 0 || type > ARRAY_SIZE(onchip_ops));
	*ops = onchip_ops[type];
}

823
int __init arch_clk_init(void)
824
{
825
	struct clk *clk;
826 827
	int i;

828
	clk = clk_get(NULL, "master_clk");
829 830
	for (i = 0; i < ARRAY_SIZE(sh7722_clocks); i++) {
		pr_debug( "Registering clock '%s'\n", sh7722_clocks[i]->name);
831
		sh7722_clocks[i]->parent = clk;
832 833
		clk_register(sh7722_clocks[i]);
	}
834 835 836 837 838 839 840 841 842 843 844 845 846
	clk_put(clk);

	clk_register(&sh7722_r_clock);

	for (i = 0; i < ARRAY_SIZE(sh7722_mstpcr_clocks); i++) {
		pr_debug( "Registering mstpcr clock '%s'\n",
			  sh7722_mstpcr_clocks[i].name);
		clk = clk_get(NULL, (void *) sh7722_mstpcr_clocks[i].ops);
		sh7722_mstpcr_clocks[i].parent = clk;
		sh7722_mstpcr_clocks[i].ops = &sh7722_mstpcr_clk_ops;
		clk_register(&sh7722_mstpcr_clocks[i]);
		clk_put(clk);
	}
847 848 849 850 851 852

	for (i = 0; i < ARRAY_SIZE(sh7722_mstpcr); i++) {
		pr_debug( "Registering mstpcr '%s'\n", sh7722_mstpcr[i].name);
		clk_register(&sh7722_mstpcr[i]);
	}

853 854
	return 0;
}