clock.c 20.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 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 144 145 146 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 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 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 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 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 282 283 284 285 286 287 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 356 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 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 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 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 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 682 683 684 685 686 687 688 689 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 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 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
/*
 * arch/arm/mach-pnx4008/clock.c
 *
 * Clock control driver for PNX4008
 *
 * Authors: Vitaly Wool, Dmitry Chigirev <source@mvista.com>
 * Generic clock management functions are partially based on:
 *  linux/arch/arm/mach-omap/clock.c
 *
 * 2005-2006 (c) MontaVista Software, Inc. This file is licensed under
 * the terms of the GNU General Public License version 2. This program
 * is licensed "as is" without any warranty of any kind, whether express
 * or implied.
 */

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/list.h>
#include <linux/errno.h>
#include <linux/device.h>
#include <linux/err.h>
#include <linux/delay.h>

#include <asm/semaphore.h>
#include <asm/hardware.h>
#include <asm/io.h>

#include <asm/arch/clock.h>
#include "clock.h"

/*forward declaration*/
static struct clk per_ck;
static struct clk hclk_ck;
static struct clk ck_1MHz;
static struct clk ck_13MHz;
static struct clk ck_pll1;
static int local_set_rate(struct clk *clk, u32 rate);

static inline void clock_lock(void)
{
	local_irq_disable();
}

static inline void clock_unlock(void)
{
	local_irq_enable();
}

static void propagate_rate(struct clk *clk)
{
	struct clk *tmp_clk;

	tmp_clk = clk;
	while (tmp_clk->propagate_next) {
		tmp_clk = tmp_clk->propagate_next;
		local_set_rate(tmp_clk, tmp_clk->user_rate);
	}
}

static inline void clk_reg_disable(struct clk *clk)
{
	if (clk->enable_reg)
		__raw_writel(__raw_readl(clk->enable_reg) &
			     ~(1 << clk->enable_shift), clk->enable_reg);
}

static inline void clk_reg_enable(struct clk *clk)
{
	if (clk->enable_reg)
		__raw_writel(__raw_readl(clk->enable_reg) |
			     (1 << clk->enable_shift), clk->enable_reg);
}

static inline void clk_reg_disable1(struct clk *clk)
{
	if (clk->enable_reg1)
		__raw_writel(__raw_readl(clk->enable_reg1) &
			     ~(1 << clk->enable_shift1), clk->enable_reg1);
}

static inline void clk_reg_enable1(struct clk *clk)
{
	if (clk->enable_reg1)
		__raw_writel(__raw_readl(clk->enable_reg1) |
			     (1 << clk->enable_shift1), clk->enable_reg1);
}

static int clk_wait_for_pll_lock(struct clk *clk)
{
	int i;
	i = 0;
	while (i++ < 0xFFF && !(__raw_readl(clk->scale_reg) & 1)) ;	/*wait for PLL to lock */

	if (!(__raw_readl(clk->scale_reg) & 1)) {
		printk(KERN_ERR
		       "%s ERROR: failed to lock, scale reg data: %x\n",
		       clk->name, __raw_readl(clk->scale_reg));
		return -1;
	}
	return 0;
}

static int switch_to_dirty_13mhz(struct clk *clk)
{
	int i;
	int ret;
	u32 tmp_reg;

	ret = 0;

	if (!clk->rate)
		clk_reg_enable1(clk);

	tmp_reg = __raw_readl(clk->parent_switch_reg);
	/*if 13Mhz clock selected, select 13'MHz (dirty) source from OSC */
	if (!(tmp_reg & 1)) {
		tmp_reg |= (1 << 1);	/* Trigger switch to 13'MHz (dirty) clock */
		__raw_writel(tmp_reg, clk->parent_switch_reg);
		i = 0;
		while (i++ < 0xFFF && !(__raw_readl(clk->parent_switch_reg) & 1)) ;	/*wait for 13'MHz selection status */

		if (!(__raw_readl(clk->parent_switch_reg) & 1)) {
			printk(KERN_ERR
			       "%s ERROR: failed to select 13'MHz, parent sw reg data: %x\n",
			       clk->name, __raw_readl(clk->parent_switch_reg));
			ret = -1;
		}
	}

	if (!clk->rate)
		clk_reg_disable1(clk);

	return ret;
}

static int switch_to_clean_13mhz(struct clk *clk)
{
	int i;
	int ret;
	u32 tmp_reg;

	ret = 0;

	if (!clk->rate)
		clk_reg_enable1(clk);

	tmp_reg = __raw_readl(clk->parent_switch_reg);
	/*if 13'Mhz clock selected, select 13MHz (clean) source from OSC */
	if (tmp_reg & 1) {
		tmp_reg &= ~(1 << 1);	/* Trigger switch to 13MHz (clean) clock */
		__raw_writel(tmp_reg, clk->parent_switch_reg);
		i = 0;
		while (i++ < 0xFFF && (__raw_readl(clk->parent_switch_reg) & 1)) ;	/*wait for 13MHz selection status */

		if (__raw_readl(clk->parent_switch_reg) & 1) {
			printk(KERN_ERR
			       "%s ERROR: failed to select 13MHz, parent sw reg data: %x\n",
			       clk->name, __raw_readl(clk->parent_switch_reg));
			ret = -1;
		}
	}

	if (!clk->rate)
		clk_reg_disable1(clk);

	return ret;
}

static int set_13MHz_parent(struct clk *clk, struct clk *parent)
{
	int ret = -EINVAL;

	if (parent == &ck_13MHz)
		ret = switch_to_clean_13mhz(clk);
	else if (parent == &ck_pll1)
		ret = switch_to_dirty_13mhz(clk);

	return ret;
}

#define PLL160_MIN_FCCO 156000
#define PLL160_MAX_FCCO 320000

/*
 * Calculate pll160 settings.
 * Possible input: up to 320MHz with step of clk->parent->rate.
 * In PNX4008 parent rate for pll160s may be either 1 or 13MHz.
 * Ignored paths: "feedback" (bit 13 set), "div-by-N".
 * Setting ARM PLL4 rate to 0 will put CPU into direct run mode.
 * Setting PLL5 and PLL3 rate to 0 will disable USB and DSP clock input.
 * Please refer to PNX4008 IC manual for details.
 */

static int pll160_set_rate(struct clk *clk, u32 rate)
{
	u32 tmp_reg, tmp_m, tmp_2p, i;
	u32 parent_rate;
	int ret = -EINVAL;

	parent_rate = clk->parent->rate;

	if (!parent_rate)
		goto out;

	/* set direct run for ARM or disable output for others  */
	clk_reg_disable(clk);

	/* disable source input as well (ignored for ARM) */
	clk_reg_disable1(clk);

	tmp_reg = __raw_readl(clk->scale_reg);
	tmp_reg &= ~0x1ffff;	/*clear all settings, power down */
	__raw_writel(tmp_reg, clk->scale_reg);

	rate -= rate % parent_rate;	/*round down the input */

	if (rate > PLL160_MAX_FCCO)
		rate = PLL160_MAX_FCCO;

	if (!rate) {
		clk->rate = 0;
		ret = 0;
		goto out;
	}

	clk_reg_enable1(clk);
	tmp_reg = __raw_readl(clk->scale_reg);

	if (rate == parent_rate) {
		/*enter direct bypass mode */
		tmp_reg |= ((1 << 14) | (1 << 15));
		__raw_writel(tmp_reg, clk->scale_reg);
		clk->rate = parent_rate;
		clk_reg_enable(clk);
		ret = 0;
		goto out;
	}

	i = 0;
	for (tmp_2p = 1; tmp_2p < 16; tmp_2p <<= 1) {
		if (rate * tmp_2p >= PLL160_MIN_FCCO)
			break;
		i++;
	}

	if (tmp_2p > 1)
		tmp_reg |= ((i - 1) << 11);
	else
		tmp_reg |= (1 << 14);	/*direct mode, no divide */

	tmp_m = rate * tmp_2p;
	tmp_m /= parent_rate;

	tmp_reg |= (tmp_m - 1) << 1;	/*calculate M */
	tmp_reg |= (1 << 16);	/*power up PLL */
	__raw_writel(tmp_reg, clk->scale_reg);

	if (clk_wait_for_pll_lock(clk) < 0) {
		clk_reg_disable(clk);
		clk_reg_disable1(clk);

		tmp_reg = __raw_readl(clk->scale_reg);
		tmp_reg &= ~0x1ffff;	/*clear all settings, power down */
		__raw_writel(tmp_reg, clk->scale_reg);
		clk->rate = 0;
		ret = -EFAULT;
		goto out;
	}

	clk->rate = (tmp_m * parent_rate) / tmp_2p;

	if (clk->flags & RATE_PROPAGATES)
		propagate_rate(clk);

	clk_reg_enable(clk);
	ret = 0;

out:
	return ret;
}

/*configure PER_CLK*/
static int per_clk_set_rate(struct clk *clk, u32 rate)
{
	u32 tmp;

	tmp = __raw_readl(clk->scale_reg);
	tmp &= ~(0x1f << 2);
	tmp |= ((clk->parent->rate / clk->rate) - 1) << 2;
	__raw_writel(tmp, clk->scale_reg);
	clk->rate = rate;
	return 0;
}

/*configure HCLK*/
static int hclk_set_rate(struct clk *clk, u32 rate)
{
	u32 tmp;
	tmp = __raw_readl(clk->scale_reg);
	tmp = tmp & ~0x3;
	switch (rate) {
	case 1:
		break;
	case 2:
		tmp |= 1;
		break;
	case 4:
		tmp |= 2;
		break;
	}

	__raw_writel(tmp, clk->scale_reg);
	clk->rate = rate;
	return 0;
}

static u32 hclk_round_rate(struct clk *clk, u32 rate)
{
	switch (rate) {
	case 1:
	case 4:
		return rate;
	}
	return 2;
}

static u32 per_clk_round_rate(struct clk *clk, u32 rate)
{
	return CLK_RATE_13MHZ;
}

static int on_off_set_rate(struct clk *clk, u32 rate)
{
	if (rate) {
		clk_reg_enable(clk);
		clk->rate = 1;
	} else {
		clk_reg_disable(clk);
		clk->rate = 0;
	}
	return 0;
}

static int on_off_inv_set_rate(struct clk *clk, u32 rate)
{
	if (rate) {
		clk_reg_disable(clk);	/*enable bit is inverted */
		clk->rate = 1;
	} else {
		clk_reg_enable(clk);
		clk->rate = 0;
	}
	return 0;
}

static u32 on_off_round_rate(struct clk *clk, u32 rate)
{
	return (rate ? 1 : 0);
}

static u32 pll4_round_rate(struct clk *clk, u32 rate)
{
	if (rate > CLK_RATE_208MHZ)
		rate = CLK_RATE_208MHZ;
	if (rate == CLK_RATE_208MHZ && hclk_ck.user_rate == 1)
		rate = CLK_RATE_208MHZ - CLK_RATE_13MHZ;
	return (rate - (rate % (hclk_ck.user_rate * CLK_RATE_13MHZ)));
}

static u32 pll3_round_rate(struct clk *clk, u32 rate)
{
	if (rate > CLK_RATE_208MHZ)
		rate = CLK_RATE_208MHZ;
	return (rate - rate % CLK_RATE_13MHZ);
}

static u32 pll5_round_rate(struct clk *clk, u32 rate)
{
	return (rate ? CLK_RATE_48MHZ : 0);
}

static u32 ck_13MHz_round_rate(struct clk *clk, u32 rate)
{
	return (rate ? CLK_RATE_13MHZ : 0);
}

static int ck_13MHz_set_rate(struct clk *clk, u32 rate)
{
	if (rate) {
		clk_reg_disable(clk);	/*enable bit is inverted */
		udelay(500);
		clk->rate = CLK_RATE_13MHZ;
		ck_1MHz.rate = CLK_RATE_1MHZ;
	} else {
		clk_reg_enable(clk);
		clk->rate = 0;
		ck_1MHz.rate = 0;
	}
	return 0;
}

static int pll1_set_rate(struct clk *clk, u32 rate)
{
#if 0 /* doesn't work on some boards, probably a HW BUG */
	if (rate) {
		clk_reg_disable(clk);	/*enable bit is inverted */
		if (!clk_wait_for_pll_lock(clk)) {
			clk->rate = CLK_RATE_13MHZ;
		} else {
			clk_reg_enable(clk);
			clk->rate = 0;
		}

	} else {
		clk_reg_enable(clk);
		clk->rate = 0;
	}
#endif
	return 0;
}

/* Clock sources */

static struct clk osc_13MHz = {
	.name = "osc_13MHz",
	.flags = FIXED_RATE,
	.rate = CLK_RATE_13MHZ,
};

static struct clk ck_13MHz = {
	.name = "ck_13MHz",
	.parent = &osc_13MHz,
	.flags = NEEDS_INITIALIZATION,
	.round_rate = &ck_13MHz_round_rate,
	.set_rate = &ck_13MHz_set_rate,
	.enable_reg = OSC13CTRL_REG,
	.enable_shift = 0,
	.rate = CLK_RATE_13MHZ,
};

static struct clk osc_32KHz = {
	.name = "osc_32KHz",
	.flags = FIXED_RATE,
	.rate = CLK_RATE_32KHZ,
};

/*attached to PLL5*/
static struct clk ck_1MHz = {
	.name = "ck_1MHz",
	.flags = FIXED_RATE | PARENT_SET_RATE,
	.parent = &ck_13MHz,
};

/* PLL1 (397) - provides 13' MHz clock */
static struct clk ck_pll1 = {
	.name = "ck_pll1",
	.parent = &osc_32KHz,
	.flags = NEEDS_INITIALIZATION,
	.round_rate = &ck_13MHz_round_rate,
	.set_rate = &pll1_set_rate,
	.enable_reg = PLLCTRL_REG,
	.enable_shift = 1,
	.scale_reg = PLLCTRL_REG,
	.rate = CLK_RATE_13MHZ,
};

/* CPU/Bus PLL */
static struct clk ck_pll4 = {
	.name = "ck_pll4",
	.parent = &ck_pll1,
	.flags = RATE_PROPAGATES | NEEDS_INITIALIZATION,
	.propagate_next = &per_ck,
	.round_rate = &pll4_round_rate,
	.set_rate = &pll160_set_rate,
	.rate = CLK_RATE_208MHZ,
	.scale_reg = HCLKPLLCTRL_REG,
	.enable_reg = PWRCTRL_REG,
	.enable_shift = 2,
	.parent_switch_reg = SYSCLKCTRL_REG,
	.set_parent = &set_13MHz_parent,
};

/* USB PLL */
static struct clk ck_pll5 = {
	.name = "ck_pll5",
	.parent = &ck_1MHz,
	.flags = NEEDS_INITIALIZATION,
	.round_rate = &pll5_round_rate,
	.set_rate = &pll160_set_rate,
	.scale_reg = USBCTRL_REG,
	.enable_reg = USBCTRL_REG,
	.enable_shift = 18,
	.enable_reg1 = USBCTRL_REG,
	.enable_shift1 = 17,
};

/* XPERTTeak DSP PLL */
static struct clk ck_pll3 = {
	.name = "ck_pll3",
	.parent = &ck_pll1,
	.flags = NEEDS_INITIALIZATION,
	.round_rate = &pll3_round_rate,
	.set_rate = &pll160_set_rate,
	.scale_reg = DSPPLLCTRL_REG,
	.enable_reg = DSPCLKCTRL_REG,
	.enable_shift = 3,
	.enable_reg1 = DSPCLKCTRL_REG,
	.enable_shift1 = 2,
	.parent_switch_reg = DSPCLKCTRL_REG,
	.set_parent = &set_13MHz_parent,
};

static struct clk hclk_ck = {
	.name = "hclk_ck",
	.parent = &ck_pll4,
	.flags = PARENT_SET_RATE,
	.set_rate = &hclk_set_rate,
	.round_rate = &hclk_round_rate,
	.scale_reg = HCLKDIVCTRL_REG,
	.rate = 2,
	.user_rate = 2,
};

static struct clk per_ck = {
	.name = "per_ck",
	.parent = &ck_pll4,
	.flags = FIXED_RATE,
	.propagate_next = &hclk_ck,
	.set_rate = &per_clk_set_rate,
	.round_rate = &per_clk_round_rate,
	.scale_reg = HCLKDIVCTRL_REG,
	.rate = CLK_RATE_13MHZ,
	.user_rate = CLK_RATE_13MHZ,
};

static struct clk m2hclk_ck = {
	.name = "m2hclk_ck",
	.parent = &hclk_ck,
	.flags = NEEDS_INITIALIZATION,
	.round_rate = &on_off_round_rate,
	.set_rate = &on_off_inv_set_rate,
	.rate = 1,
	.enable_shift = 6,
	.enable_reg = PWRCTRL_REG,
};

static struct clk vfp9_ck = {
	.name = "vfp9_ck",
	.parent = &ck_pll4,
	.flags = NEEDS_INITIALIZATION,
	.round_rate = &on_off_round_rate,
	.set_rate = &on_off_set_rate,
	.rate = 1,
	.enable_shift = 4,
	.enable_reg = VFP9CLKCTRL_REG,
};

static struct clk keyscan_ck = {
	.name = "keyscan_ck",
	.parent = &osc_32KHz,
	.flags = NEEDS_INITIALIZATION,
	.round_rate = &on_off_round_rate,
	.set_rate = &on_off_set_rate,
	.enable_shift = 0,
	.enable_reg = KEYCLKCTRL_REG,
};

static struct clk touch_ck = {
	.name = "touch_ck",
	.parent = &osc_32KHz,
	.flags = NEEDS_INITIALIZATION,
	.round_rate = &on_off_round_rate,
	.set_rate = &on_off_set_rate,
	.enable_shift = 0,
	.enable_reg = TSCLKCTRL_REG,
};

static struct clk pwm1_ck = {
	.name = "pwm1_ck",
	.parent = &osc_32KHz,
	.flags = NEEDS_INITIALIZATION,
	.round_rate = &on_off_round_rate,
	.set_rate = &on_off_set_rate,
	.enable_shift = 0,
	.enable_reg = PWMCLKCTRL_REG,
};

static struct clk pwm2_ck = {
	.name = "pwm2_ck",
	.parent = &osc_32KHz,
	.flags = NEEDS_INITIALIZATION,
	.round_rate = &on_off_round_rate,
	.set_rate = &on_off_set_rate,
	.enable_shift = 2,
	.enable_reg = PWMCLKCTRL_REG,
};

static struct clk jpeg_ck = {
	.name = "jpeg_ck",
	.parent = &hclk_ck,
	.flags = NEEDS_INITIALIZATION,
	.round_rate = &on_off_round_rate,
	.set_rate = &on_off_set_rate,
	.enable_shift = 0,
	.enable_reg = JPEGCLKCTRL_REG,
};

static struct clk ms_ck = {
	.name = "ms_ck",
	.parent = &ck_pll4,
	.flags = NEEDS_INITIALIZATION,
	.round_rate = &on_off_round_rate,
	.set_rate = &on_off_set_rate,
	.enable_shift = 5,
	.enable_reg = MSCTRL_REG,
};

static struct clk dum_ck = {
	.name = "dum_ck",
	.parent = &hclk_ck,
	.flags = NEEDS_INITIALIZATION,
	.round_rate = &on_off_round_rate,
	.set_rate = &on_off_set_rate,
	.enable_shift = 0,
	.enable_reg = DUMCLKCTRL_REG,
};

static struct clk flash_ck = {
	.name = "flash_ck",
	.parent = &hclk_ck,
	.round_rate = &on_off_round_rate,
	.set_rate = &on_off_set_rate,
	.enable_shift = 1,	/* Only MLC clock supported */
	.enable_reg = FLASHCLKCTRL_REG,
};

static struct clk i2c0_ck = {
	.name = "i2c0_ck",
	.parent = &per_ck,
	.flags = NEEDS_INITIALIZATION,
	.round_rate = &on_off_round_rate,
	.set_rate = &on_off_set_rate,
	.enable_shift = 0,
	.enable_reg = I2CCLKCTRL_REG,
};

static struct clk i2c1_ck = {
	.name = "i2c1_ck",
	.parent = &per_ck,
	.flags = NEEDS_INITIALIZATION,
	.round_rate = &on_off_round_rate,
	.set_rate = &on_off_set_rate,
	.enable_shift = 1,
	.enable_reg = I2CCLKCTRL_REG,
};

static struct clk i2c2_ck = {
	.name = "i2c2_ck",
	.parent = &per_ck,
	.flags = NEEDS_INITIALIZATION,
	.round_rate = &on_off_round_rate,
	.set_rate = &on_off_set_rate,
	.enable_shift = 2,
	.enable_reg = USB_OTG_CLKCTRL_REG,
};

static struct clk spi0_ck = {
	.name = "spi0_ck",
	.parent = &hclk_ck,
	.flags = NEEDS_INITIALIZATION,
	.round_rate = &on_off_round_rate,
	.set_rate = &on_off_set_rate,
	.enable_shift = 0,
	.enable_reg = SPICTRL_REG,
};

static struct clk spi1_ck = {
	.name = "spi1_ck",
	.parent = &hclk_ck,
	.flags = NEEDS_INITIALIZATION,
	.round_rate = &on_off_round_rate,
	.set_rate = &on_off_set_rate,
	.enable_shift = 4,
	.enable_reg = SPICTRL_REG,
};

static struct clk dma_ck = {
	.name = "dma_ck",
	.parent = &hclk_ck,
	.round_rate = &on_off_round_rate,
	.set_rate = &on_off_set_rate,
	.enable_shift = 0,
	.enable_reg = DMACLKCTRL_REG,
};

static struct clk uart3_ck = {
	.name = "uart3_ck",
	.parent = &per_ck,
	.flags = NEEDS_INITIALIZATION,
	.round_rate = &on_off_round_rate,
	.set_rate = &on_off_set_rate,
	.rate = 1,
	.enable_shift = 0,
	.enable_reg = UARTCLKCTRL_REG,
};

static struct clk uart4_ck = {
	.name = "uart4_ck",
	.parent = &per_ck,
	.flags = NEEDS_INITIALIZATION,
	.round_rate = &on_off_round_rate,
	.set_rate = &on_off_set_rate,
	.enable_shift = 1,
	.enable_reg = UARTCLKCTRL_REG,
};

static struct clk uart5_ck = {
	.name = "uart5_ck",
	.parent = &per_ck,
	.flags = NEEDS_INITIALIZATION,
	.round_rate = &on_off_round_rate,
	.set_rate = &on_off_set_rate,
	.rate = 1,
	.enable_shift = 2,
	.enable_reg = UARTCLKCTRL_REG,
};

static struct clk uart6_ck = {
	.name = "uart6_ck",
	.parent = &per_ck,
	.flags = NEEDS_INITIALIZATION,
	.round_rate = &on_off_round_rate,
	.set_rate = &on_off_set_rate,
	.enable_shift = 3,
	.enable_reg = UARTCLKCTRL_REG,
};

/* These clocks are visible outside this module
 * and can be initialized
 */
static struct clk *onchip_clks[] = {
	&ck_13MHz,
	&ck_pll1,
	&ck_pll4,
	&ck_pll5,
	&ck_pll3,
	&vfp9_ck,
	&m2hclk_ck,
	&hclk_ck,
	&dma_ck,
	&flash_ck,
	&dum_ck,
	&keyscan_ck,
	&pwm1_ck,
	&pwm2_ck,
	&jpeg_ck,
	&ms_ck,
	&touch_ck,
	&i2c0_ck,
	&i2c1_ck,
	&i2c2_ck,
	&spi0_ck,
	&spi1_ck,
	&uart3_ck,
	&uart4_ck,
	&uart5_ck,
	&uart6_ck,
};

static int local_set_rate(struct clk *clk, u32 rate)
{
	int ret = -EINVAL;
	if (clk->set_rate) {

		if (clk->user_rate == clk->rate && clk->parent->rate) {
			/* if clock enabled or rate not set */
			clk->user_rate = clk->round_rate(clk, rate);
			ret = clk->set_rate(clk, clk->user_rate);
		} else
			clk->user_rate = clk->round_rate(clk, rate);
		ret = 0;
	}
	return ret;
}

int clk_set_rate(struct clk *clk, unsigned long rate)
{
	int ret = -EINVAL;

	if (clk->flags & FIXED_RATE)
		goto out;

	clock_lock();
	if ((clk->flags & PARENT_SET_RATE) && clk->parent) {

		clk->user_rate = clk->round_rate(clk, rate);
		/* parent clock needs to be refreshed
		   for the setting to take effect */
	} else {
		ret = local_set_rate(clk, rate);
	}
	ret = 0;
	clock_unlock();

out:
	return ret;
}

EXPORT_SYMBOL(clk_set_rate);

struct clk *clk_get(struct device *dev, const char *id)
{
	struct clk *clk = ERR_PTR(-ENOENT);
	struct clk **clkp;

	clock_lock();
	for (clkp = onchip_clks; clkp < onchip_clks + ARRAY_SIZE(onchip_clks);
	     clkp++) {
		if (strcmp(id, (*clkp)->name) == 0
		    && try_module_get((*clkp)->owner)) {
			clk = (*clkp);
			break;
		}
	}
	clock_unlock();

	return clk;
}
EXPORT_SYMBOL(clk_get);

void clk_put(struct clk *clk)
{
	clock_lock();
	if (clk && !IS_ERR(clk))
		module_put(clk->owner);
	clock_unlock();
}
EXPORT_SYMBOL(clk_put);

unsigned long clk_get_rate(struct clk *clk)
{
	unsigned long ret;
	clock_lock();
	ret = clk->rate;
	clock_unlock();
	return ret;
}
EXPORT_SYMBOL(clk_get_rate);

static int local_clk_enable(struct clk *clk)
{
	int ret = 0;

	if (!(clk->flags & FIXED_RATE) && !clk->rate && clk->set_rate
	    && clk->user_rate)
		ret = clk->set_rate(clk, clk->user_rate);
	return ret;
}

static void local_clk_disable(struct clk *clk)
{
	if (!(clk->flags & FIXED_RATE) && clk->rate && clk->set_rate)
		clk->set_rate(clk, 0);
}

int clk_enable(struct clk *clk)
{
	int ret = 0;

	clock_lock();
	ret = local_clk_enable(clk);
	clock_unlock();
	return ret;
}

EXPORT_SYMBOL(clk_enable);

void clk_disable(struct clk *clk)
{
	clock_lock();
	local_clk_disable(clk);
	clock_unlock();
}

EXPORT_SYMBOL(clk_disable);

static void local_clk_unuse(struct clk *clk)
{
	if (clk->usecount > 0 && !(--clk->usecount)) {
		local_clk_disable(clk);
		if (clk->parent)
			local_clk_unuse(clk->parent);
	}
}

static int local_clk_use(struct clk *clk)
{
	int ret = 0;
	if (clk->usecount++ == 0) {
		if (clk->parent)
			ret = local_clk_use(clk->parent);

		if (ret != 0) {
			clk->usecount--;
			goto out;
		}

		ret = local_clk_enable(clk);

		if (ret != 0 && clk->parent) {
			local_clk_unuse(clk->parent);
			clk->usecount--;
		}
	}
out:
	return ret;
}

/* The main purpose of clk_use ans clk_unuse functions
 * is to control switching 13MHz oscillator and PLL1 (13'MHz),
 * so that they are disabled whenever none of PLL2-5 is using them.
 * Although in theory these functions should work with any clock,
 * please use them only on PLL2 - PLL5 to avoid confusion.
 */
int clk_use(struct clk *clk)
{
	int ret = 0;

	clock_lock();
	ret = local_clk_use(clk);
	clock_unlock();
	return ret;
}
EXPORT_SYMBOL(clk_use);

void clk_unuse(struct clk *clk)
{

	clock_lock();
	local_clk_unuse(clk);
	clock_unlock();
}

EXPORT_SYMBOL(clk_unuse);

long clk_round_rate(struct clk *clk, unsigned long rate)
{
	long ret;
	clock_lock();
	if (clk->round_rate)
		ret = clk->round_rate(clk, rate);
	else
		ret = clk->rate;
	clock_unlock();
	return ret;
}

EXPORT_SYMBOL(clk_round_rate);

int clk_set_parent(struct clk *clk, struct clk *parent)
{
	int ret = -ENODEV;
	if (!clk->set_parent)
		goto out;

	clock_lock();
	ret = clk->set_parent(clk, parent);
	if (!ret)
		clk->parent = parent;
	clock_unlock();

out:
	return ret;
}

EXPORT_SYMBOL(clk_set_parent);

static int __init clk_init(void)
{
	struct clk **clkp;

	/* Disable autoclocking, as it doesn't seem to work */
	__raw_writel(0xff, AUTOCLK_CTRL);

	for (clkp = onchip_clks; clkp < onchip_clks + ARRAY_SIZE(onchip_clks);
	     clkp++) {
		if (((*clkp)->flags & NEEDS_INITIALIZATION)
		    && ((*clkp)->set_rate)) {
			(*clkp)->user_rate = (*clkp)->rate;
			local_set_rate((*clkp), (*clkp)->user_rate);
			if ((*clkp)->set_parent)
				(*clkp)->set_parent((*clkp), (*clkp)->parent);
		}
		pr_debug("%s: clock %s, rate %ld\n",
			__FUNCTION__, (*clkp)->name, (*clkp)->rate);
	}

	clk_use(&ck_pll4);

	/* if ck_13MHz is not used, disable it. */
	if (ck_13MHz.usecount == 0)
		local_clk_disable(&ck_13MHz);

	/* Disable autoclocking */
	__raw_writeb(0xff, AUTOCLK_CTRL);

	return 0;
}

arch_initcall(clk_init);