s5pc100-clock.c 25.1 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
/* linux/arch/arm/plat-s5pc1xx/s5pc100-clock.c
 *
 * Copyright 2009 Samsung Electronics, Co.
 *	Byungho Min <bhmin@samsung.com>
 *
 * S5PC100 based common clock support
 *
 * Based on plat-s3c64xx/s3c6400-clock.c
 *
 * 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.
*/

#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/list.h>
#include <linux/errno.h>
#include <linux/err.h>
#include <linux/clk.h>
#include <linux/sysdev.h>
#include <linux/io.h>

#include <mach/hardware.h>
#include <mach/map.h>

#include <plat/cpu-freq.h>

#include <plat/regs-clock.h>
#include <plat/clock.h>
#include <plat/cpu.h>
#include <plat/pll.h>
#include <plat/devs.h>
#include <plat/s5pc100.h>

/* fin_apll, fin_mpll and fin_epll are all the same clock, which we call
 * ext_xtal_mux for want of an actual name from the manual.
*/

static struct clk clk_ext_xtal_mux = {
	.name		= "ext_xtal",
	.id		= -1,
};

#define clk_fin_apll clk_ext_xtal_mux
#define clk_fin_mpll clk_ext_xtal_mux
#define clk_fin_epll clk_ext_xtal_mux
#define clk_fin_hpll clk_ext_xtal_mux

#define clk_fout_mpll	clk_mpll
52
#define clk_vclk_54m	clk_54m
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70

struct clk_sources {
	unsigned int	nr_sources;
	struct clk	**sources;
};

struct clksrc_clk {
	struct clk		clk;
	unsigned int		mask;
	unsigned int		shift;

	struct clk_sources	*sources;

	unsigned int		divider_shift;
	void __iomem		*reg_divider;
	void __iomem		*reg_source;
};

71 72 73
/* APLL */
static struct clk clk_fout_apll = {
	.name		= "fout_apll",
74 75 76 77
	.id		= -1,
	.rate		= 27000000,
};

78 79 80 81
static struct clk *clk_src_apll_list[] = {
	[0] = &clk_fin_apll,
	[1] = &clk_fout_apll,
};
82

83 84 85 86
static struct clk_sources clk_src_apll = {
	.sources	= clk_src_apll_list,
	.nr_sources	= ARRAY_SIZE(clk_src_apll_list),
};
87

88 89 90 91 92 93 94 95 96 97
static struct clksrc_clk clk_mout_apll = {
	.clk	= {
		.name		= "mout_apll",
		.id		= -1,
	},
	.shift		= S5PC100_CLKSRC0_APLL_SHIFT,
	.mask		= S5PC100_CLKSRC0_APLL_MASK,
	.sources	= &clk_src_apll,
	.reg_source	= S5PC100_CLKSRC0,
};
98

99 100 101 102
static unsigned long s5pc100_clk_dout_apll_get_rate(struct clk *clk)
{
	unsigned long rate = clk_get_rate(clk->parent);
	unsigned int ratio;
103

104 105
	ratio = __raw_readl(S5PC100_CLKDIV0) & S5PC100_CLKDIV0_APLL_MASK;
	ratio >>= S5PC100_CLKDIV0_APLL_SHIFT;
106

107 108
	return rate / (ratio + 1);
}
109

110 111
static struct clk clk_dout_apll = {
	.name		= "dout_apll",
112
	.id		= -1,
113
	.parent		= &clk_mout_apll.clk,
114 115 116
	.ops		= &(struct clk_ops) {
		.get_rate	= s5pc100_clk_dout_apll_get_rate,
	},
117 118
};

119 120 121 122
static unsigned long s5pc100_clk_arm_get_rate(struct clk *clk)
{
	unsigned long rate = clk_get_rate(clk->parent);
	unsigned int ratio;
123

124 125
	ratio = __raw_readl(S5PC100_CLKDIV0) & S5PC100_CLKDIV0_ARM_MASK;
	ratio >>= S5PC100_CLKDIV0_ARM_SHIFT;
126

127 128
	return rate / (ratio + 1);
}
129

130 131
static unsigned long s5pc100_clk_arm_round_rate(struct clk *clk,
						unsigned long rate)
132
{
133 134
	unsigned long parent = clk_get_rate(clk->parent);
	u32 div;
135

136 137
	if (parent < rate)
		return rate;
138

139 140 141
	div = (parent / rate) - 1;
	if (div > S5PC100_CLKDIV0_ARM_MASK)
		div = S5PC100_CLKDIV0_ARM_MASK;
142

143
	return parent / (div + 1);
144 145
}

146
static int s5pc100_clk_arm_set_rate(struct clk *clk, unsigned long rate)
147
{
148 149 150
	unsigned long parent = clk_get_rate(clk->parent);
	u32 div;
	u32 val;
151

152 153
	if (rate < parent / (S5PC100_CLKDIV0_ARM_MASK + 1))
		return -EINVAL;
154

155 156
	rate = clk_round_rate(clk, rate);
	div = clk_get_rate(clk->parent) / rate;
157

158 159 160 161
	val = __raw_readl(S5PC100_CLKDIV0);
	val &= S5PC100_CLKDIV0_ARM_MASK;
	val |= (div - 1);
	__raw_writel(val, S5PC100_CLKDIV0);
162

163
	return 0;
164 165
}

166 167 168 169
static struct clk clk_arm = {
	.name		= "armclk",
	.id		= -1,
	.parent		= &clk_dout_apll,
170 171 172 173 174
	.ops		= &(struct clk_ops) {
		.get_rate	= s5pc100_clk_arm_get_rate,
		.set_rate	= s5pc100_clk_arm_set_rate,
		.round_rate	= s5pc100_clk_arm_round_rate,
	},
175
};
176

177
static unsigned long s5pc100_clk_dout_d0_bus_get_rate(struct clk *clk)
178
{
179 180
	unsigned long rate = clk_get_rate(clk->parent);
	unsigned int ratio;
181

182 183
	ratio = __raw_readl(S5PC100_CLKDIV0) & S5PC100_CLKDIV0_D0_MASK;
	ratio >>= S5PC100_CLKDIV0_D0_SHIFT;
184

185
	return rate / (ratio + 1);
186 187
}

188 189 190 191
static struct clk clk_dout_d0_bus = {
	.name		= "dout_d0_bus",
	.id		= -1,
	.parent		= &clk_arm,
192 193 194
	.ops		= &(struct clk_ops) {
		.get_rate	= s5pc100_clk_dout_d0_bus_get_rate,
	},
195
};
196

197
static unsigned long s5pc100_clk_dout_pclkd0_get_rate(struct clk *clk)
198
{
199 200 201 202 203 204 205
	unsigned long rate = clk_get_rate(clk->parent);
	unsigned int ratio;

	ratio = __raw_readl(S5PC100_CLKDIV0) & S5PC100_CLKDIV0_PCLKD0_MASK;
	ratio >>= S5PC100_CLKDIV0_PCLKD0_SHIFT;

	return rate / (ratio + 1);
206 207
}

208 209 210 211
static struct clk clk_dout_pclkd0 = {
	.name		= "dout_pclkd0",
	.id		= -1,
	.parent		= &clk_dout_d0_bus,
212 213 214
	.ops		= &(struct clk_ops) {
		.get_rate	= s5pc100_clk_dout_pclkd0_get_rate,
	},
215 216 217
};

static unsigned long s5pc100_clk_dout_apll2_get_rate(struct clk *clk)
218
{
219 220 221 222 223 224 225
	unsigned long rate = clk_get_rate(clk->parent);
	unsigned int ratio;

	ratio = __raw_readl(S5PC100_CLKDIV1) & S5PC100_CLKDIV1_APLL2_MASK;
	ratio >>= S5PC100_CLKDIV1_APLL2_SHIFT;

	return rate / (ratio + 1);
226 227
}

228 229 230 231
static struct clk clk_dout_apll2 = {
	.name		= "dout_apll2",
	.id		= -1,
	.parent		= &clk_mout_apll.clk,
232 233 234
	.ops		= &(struct clk_ops) {
		.get_rate	= s5pc100_clk_dout_apll2_get_rate,
	},
235 236
};

237 238 239 240 241
/* MPLL */
static struct clk *clk_src_mpll_list[] = {
	[0] = &clk_fin_mpll,
	[1] = &clk_fout_mpll,
};
242

243 244 245 246
static struct clk_sources clk_src_mpll = {
	.sources	= clk_src_mpll_list,
	.nr_sources	= ARRAY_SIZE(clk_src_mpll_list),
};
247

248 249 250
static struct clksrc_clk clk_mout_mpll = {
	.clk = {
		.name		= "mout_mpll",
251 252
		.id		= -1,
	},
253 254 255 256 257
	.shift		= S5PC100_CLKSRC0_MPLL_SHIFT,
	.mask		= S5PC100_CLKSRC0_MPLL_MASK,
	.sources	= &clk_src_mpll,
	.reg_source	= S5PC100_CLKSRC0,
};
258

259 260 261 262
static struct clk *clkset_am_list[] = {
	[0] = &clk_mout_mpll.clk,
	[1] = &clk_dout_apll2,
};
263

264 265 266 267
static struct clk_sources clk_src_am = {
	.sources	= clkset_am_list,
	.nr_sources	= ARRAY_SIZE(clkset_am_list),
};
268

269 270 271
static struct clksrc_clk clk_mout_am = {
	.clk = {
		.name		= "mout_am",
272 273
		.id		= -1,
	},
274 275 276 277 278
	.shift		= S5PC100_CLKSRC0_AMMUX_SHIFT,
	.mask		= S5PC100_CLKSRC0_AMMUX_MASK,
	.sources	= &clk_src_am,
	.reg_source	= S5PC100_CLKSRC0,
};
279

280 281 282 283
static unsigned long s5pc100_clk_dout_d1_bus_get_rate(struct clk *clk)
{
	unsigned long rate = clk_get_rate(clk->parent);
	unsigned int ratio;
284

285
	printk(KERN_DEBUG "%s: parent is %ld\n", __func__, rate);
286

287 288
	ratio = __raw_readl(S5PC100_CLKDIV1) & S5PC100_CLKDIV1_D1_MASK;
	ratio >>= S5PC100_CLKDIV1_D1_SHIFT;
289

290 291
	return rate / (ratio + 1);
}
292

293 294 295 296
static struct clk clk_dout_d1_bus = {
	.name		= "dout_d1_bus",
	.id		= -1,
	.parent		= &clk_mout_am.clk,
297 298 299
	.ops		= &(struct clk_ops) {
		.get_rate	= s5pc100_clk_dout_d1_bus_get_rate,
	},
300
};
301

302 303 304 305 306 307 308 309 310 311 312 313 314
static struct clk *clkset_onenand_list[] = {
	[0] = &clk_dout_d0_bus,
	[1] = &clk_dout_d1_bus,
};

static struct clk_sources clk_src_onenand = {
	.sources	= clkset_onenand_list,
	.nr_sources	= ARRAY_SIZE(clkset_onenand_list),
};

static struct clksrc_clk clk_mout_onenand = {
	.clk = {
		.name		= "mout_onenand",
315 316
		.id		= -1,
	},
317 318 319 320
	.shift		= S5PC100_CLKSRC0_ONENAND_SHIFT,
	.mask		= S5PC100_CLKSRC0_ONENAND_MASK,
	.sources	= &clk_src_onenand,
	.reg_source	= S5PC100_CLKSRC0,
321 322
};

323
static unsigned long s5pc100_clk_dout_pclkd1_get_rate(struct clk *clk)
324
{
325 326
	unsigned long rate = clk_get_rate(clk->parent);
	unsigned int ratio;
327

328
	printk(KERN_DEBUG "%s: parent is %ld\n", __func__, rate);
329

330 331
	ratio = __raw_readl(S5PC100_CLKDIV1) & S5PC100_CLKDIV1_PCLKD1_MASK;
	ratio >>= S5PC100_CLKDIV1_PCLKD1_SHIFT;
332

333 334
	return rate / (ratio + 1);
}
335

336 337 338 339
static struct clk clk_dout_pclkd1 = {
	.name		= "dout_pclkd1",
	.id		= -1,
	.parent		= &clk_dout_d1_bus,
340 341 342
	.ops		= &(struct clk_ops) {
		.get_rate	= s5pc100_clk_dout_pclkd1_get_rate,
	},
343
};
344

345 346 347 348 349 350 351 352 353 354 355
static unsigned long s5pc100_clk_dout_mpll2_get_rate(struct clk *clk)
{
	unsigned long rate = clk_get_rate(clk->parent);
	unsigned int ratio;

	printk(KERN_DEBUG "%s: parent is %ld\n", __func__, rate);

	ratio = __raw_readl(S5PC100_CLKDIV1) & S5PC100_CLKDIV1_MPLL2_MASK;
	ratio >>= S5PC100_CLKDIV1_MPLL2_SHIFT;

	return rate / (ratio + 1);
356
}
357 358 359

static struct clk clk_dout_mpll2 = {
	.name		= "dout_mpll2",
360
	.id		= -1,
361
	.parent		= &clk_mout_am.clk,
362 363 364
	.ops		= &(struct clk_ops) {
		.get_rate	= s5pc100_clk_dout_mpll2_get_rate,
	},
365 366
};

367 368 369 370
static unsigned long s5pc100_clk_dout_cam_get_rate(struct clk *clk)
{
	unsigned long rate = clk_get_rate(clk->parent);
	unsigned int ratio;
371

372 373 374 375 376 377 378 379 380 381 382 383
	printk(KERN_DEBUG "%s: parent is %ld\n", __func__, rate);

	ratio = __raw_readl(S5PC100_CLKDIV1) & S5PC100_CLKDIV1_CAM_MASK;
	ratio >>= S5PC100_CLKDIV1_CAM_SHIFT;

	return rate / (ratio + 1);
}

static struct clk clk_dout_cam = {
	.name		= "dout_cam",
	.id		= -1,
	.parent		= &clk_dout_mpll2,
384 385 386
	.ops		= &(struct clk_ops) {
		.get_rate	= s5pc100_clk_dout_cam_get_rate,
	},
387 388
};

389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
static unsigned long s5pc100_clk_dout_mpll_get_rate(struct clk *clk)
{
	unsigned long rate = clk_get_rate(clk->parent);
	unsigned int ratio;

	printk(KERN_DEBUG "%s: parent is %ld\n", __func__, rate);

	ratio = __raw_readl(S5PC100_CLKDIV1) & S5PC100_CLKDIV1_MPLL_MASK;
	ratio >>= S5PC100_CLKDIV1_MPLL_SHIFT;

	return rate / (ratio + 1);
}

static struct clk clk_dout_mpll = {
	.name		= "dout_mpll",
	.id		= -1,
	.parent		= &clk_mout_am.clk,
406 407 408
	.ops		= &(struct clk_ops) {
		.get_rate	= s5pc100_clk_dout_mpll_get_rate,
	},
409 410
};

411
/* EPLL */
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
static struct clk clk_fout_epll = {
	.name		= "fout_epll",
	.id		= -1,
};

static struct clk *clk_src_epll_list[] = {
	[0] = &clk_fin_epll,
	[1] = &clk_fout_epll,
};

static struct clk_sources clk_src_epll = {
	.sources	= clk_src_epll_list,
	.nr_sources	= ARRAY_SIZE(clk_src_epll_list),
};

static struct clksrc_clk clk_mout_epll = {
	.clk	= {
		.name		= "mout_epll",
		.id		= -1,
	},
432 433
	.shift		= S5PC100_CLKSRC0_EPLL_SHIFT,
	.mask		= S5PC100_CLKSRC0_EPLL_MASK,
434
	.sources	= &clk_src_epll,
435
	.reg_source	= S5PC100_CLKSRC0,
436 437
};

438 439 440 441
/* HPLL */
static struct clk clk_fout_hpll = {
	.name		= "fout_hpll",
	.id		= -1,
442 443
};

444 445 446
static struct clk *clk_src_hpll_list[] = {
	[0] = &clk_27m,
	[1] = &clk_fout_hpll,
447 448
};

449 450 451 452 453 454 455 456
static struct clk_sources clk_src_hpll = {
	.sources	= clk_src_hpll_list,
	.nr_sources	= ARRAY_SIZE(clk_src_hpll_list),
};

static struct clksrc_clk clk_mout_hpll = {
	.clk	= {
		.name		= "mout_hpll",
457 458
		.id		= -1,
	},
459 460 461
	.shift		= S5PC100_CLKSRC0_HPLL_SHIFT,
	.mask		= S5PC100_CLKSRC0_HPLL_MASK,
	.sources	= &clk_src_hpll,
462
	.reg_source	= S5PC100_CLKSRC0,
463 464
};

465 466 467 468 469 470 471 472 473 474 475
/* Peripherals */
/*
 * The peripheral clocks are all controlled via clocksource followed
 * by an optional divider and gate stage. We currently roll this into
 * one clock which hides the intermediate clock from the mux.
 *
 * Note, the JPEG clock can only be an even divider...
 *
 * The scaler and LCD clocks depend on the S5PC100 version, and also
 * have a common parent divisor so are not included here.
 */
476 477 478 479 480 481

static inline struct clksrc_clk *to_clksrc(struct clk *clk)
{
	return container_of(clk, struct clksrc_clk, clk);
}

482
static unsigned long s5pc100_getrate_clksrc(struct clk *clk)
483 484 485 486 487 488 489 490 491 492 493 494 495
{
	struct clksrc_clk *sclk = to_clksrc(clk);
	unsigned long rate = clk_get_rate(clk->parent);
	u32 clkdiv = __raw_readl(sclk->reg_divider);

	clkdiv >>= sclk->divider_shift;
	clkdiv &= 0xf;
	clkdiv++;

	rate /= clkdiv;
	return rate;
}

496
static int s5pc100_setrate_clksrc(struct clk *clk, unsigned long rate)
497 498 499 500 501 502 503 504 505 506 507 508
{
	struct clksrc_clk *sclk = to_clksrc(clk);
	void __iomem *reg = sclk->reg_divider;
	unsigned int div;
	u32 val;

	rate = clk_round_rate(clk, rate);
	div = clk_get_rate(clk->parent) / rate;
	if (div > 16)
		return -EINVAL;

	val = __raw_readl(reg);
509 510
	val &= ~(0xf << sclk->divider_shift);
	val |= (div - 1) << sclk->divider_shift;
511 512 513 514 515
	__raw_writel(val, reg);

	return 0;
}

516
static int s5pc100_setparent_clksrc(struct clk *clk, struct clk *parent)
517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540
{
	struct clksrc_clk *sclk = to_clksrc(clk);
	struct clk_sources *srcs = sclk->sources;
	u32 clksrc = __raw_readl(sclk->reg_source);
	int src_nr = -1;
	int ptr;

	for (ptr = 0; ptr < srcs->nr_sources; ptr++)
		if (srcs->sources[ptr] == parent) {
			src_nr = ptr;
			break;
		}

	if (src_nr >= 0) {
		clksrc &= ~sclk->mask;
		clksrc |= src_nr << sclk->shift;

		__raw_writel(clksrc, sclk->reg_source);
		return 0;
	}

	return -EINVAL;
}

541
static unsigned long s5pc100_roundrate_clksrc(struct clk *clk,
542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562
					      unsigned long rate)
{
	unsigned long parent_rate = clk_get_rate(clk->parent);
	int div;

	if (rate > parent_rate)
		rate = parent_rate;
	else {
		div = rate / parent_rate;

		if (div == 0)
			div = 1;
		if (div > 16)
			div = 16;

		rate = parent_rate / div;
	}

	return rate;
}

563 564 565 566 567 568 569
static struct clk_ops s5pc100_clksrc_ops = {
	.set_parent	= s5pc100_setparent_clksrc,
	.get_rate	= s5pc100_getrate_clksrc,
	.set_rate	= s5pc100_setrate_clksrc,
	.round_rate	= s5pc100_roundrate_clksrc,
};

570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587
static struct clk *clkset_spi_list[] = {
	&clk_mout_epll.clk,
	&clk_dout_mpll2,
	&clk_fin_epll,
	&clk_mout_hpll.clk,
};

static struct clk_sources clkset_spi = {
	.sources	= clkset_spi_list,
	.nr_sources	= ARRAY_SIZE(clkset_spi_list),
};

static struct clksrc_clk clk_spi0 = {
	.clk	= {
		.name		= "spi_bus",
		.id		= 0,
		.ctrlbit	= S5PC100_CLKGATE_SCLK0_SPI0,
		.enable		= s5pc100_sclk0_ctrl,
588

589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
	},
	.shift		= S5PC100_CLKSRC1_SPI0_SHIFT,
	.mask		= S5PC100_CLKSRC1_SPI0_MASK,
	.sources	= &clkset_spi,
	.divider_shift	= S5PC100_CLKDIV2_SPI0_SHIFT,
	.reg_divider	= S5PC100_CLKDIV2,
	.reg_source	= S5PC100_CLKSRC1,
};

static struct clksrc_clk clk_spi1 = {
	.clk	= {
		.name		= "spi_bus",
		.id		= 1,
		.ctrlbit	= S5PC100_CLKGATE_SCLK0_SPI1,
		.enable		= s5pc100_sclk0_ctrl,
604
		.ops		= &s5pc100_clksrc_ops,
605 606 607 608 609 610 611 612 613 614 615 616 617 618 619
	},
	.shift		= S5PC100_CLKSRC1_SPI1_SHIFT,
	.mask		= S5PC100_CLKSRC1_SPI1_MASK,
	.sources	= &clkset_spi,
	.divider_shift	= S5PC100_CLKDIV2_SPI1_SHIFT,
	.reg_divider	= S5PC100_CLKDIV2,
	.reg_source	= S5PC100_CLKSRC1,
};

static struct clksrc_clk clk_spi2 = {
	.clk	= {
		.name		= "spi_bus",
		.id		= 2,
		.ctrlbit	= S5PC100_CLKGATE_SCLK0_SPI2,
		.enable		= s5pc100_sclk0_ctrl,
620
		.ops		= &s5pc100_clksrc_ops,
621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639
	},
	.shift		= S5PC100_CLKSRC1_SPI2_SHIFT,
	.mask		= S5PC100_CLKSRC1_SPI2_MASK,
	.sources	= &clkset_spi,
	.divider_shift	= S5PC100_CLKDIV2_SPI2_SHIFT,
	.reg_divider	= S5PC100_CLKDIV2,
	.reg_source	= S5PC100_CLKSRC1,
};

static struct clk *clkset_uart_list[] = {
	&clk_mout_epll.clk,
	&clk_dout_mpll,
};

static struct clk_sources clkset_uart = {
	.sources	= clkset_uart_list,
	.nr_sources	= ARRAY_SIZE(clkset_uart_list),
};

640 641 642 643 644
static struct clksrc_clk clk_uart_uclk1 = {
	.clk	= {
		.name		= "uclk1",
		.id		= -1,
		.ctrlbit        = S5PC100_CLKGATE_SCLK0_UART,
645
		.enable		= s5pc100_sclk0_ctrl,
646
		.ops		= &s5pc100_clksrc_ops,
647 648 649 650 651
	},
	.shift		= S5PC100_CLKSRC1_UART_SHIFT,
	.mask		= S5PC100_CLKSRC1_UART_MASK,
	.sources	= &clkset_uart,
	.divider_shift	= S5PC100_CLKDIV2_UART_SHIFT,
652 653
	.reg_divider	= S5PC100_CLKDIV2,
	.reg_source	= S5PC100_CLKSRC1,
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
static struct clk clk_iis_cd0 = {
	.name		= "iis_cdclk0",
	.id		= -1,
};

static struct clk clk_iis_cd1 = {
	.name		= "iis_cdclk1",
	.id		= -1,
};

static struct clk clk_iis_cd2 = {
	.name		= "iis_cdclk2",
	.id		= -1,
};

static struct clk clk_pcm_cd0 = {
	.name		= "pcm_cdclk0",
	.id		= -1,
};

static struct clk clk_pcm_cd1 = {
	.name		= "pcm_cdclk1",
	.id		= -1,
};

static struct clk *clkset_audio0_list[] = {
	&clk_mout_epll.clk,
	&clk_dout_mpll,
	&clk_fin_epll,
	&clk_iis_cd0,
	&clk_pcm_cd0,
	&clk_mout_hpll.clk,
};

static struct clk_sources clkset_audio0 = {
	.sources	= clkset_audio0_list,
	.nr_sources	= ARRAY_SIZE(clkset_audio0_list),
};

static struct clksrc_clk clk_audio0 = {
	.clk	= {
		.name		= "audio-bus",
		.id		= 0,
		.ctrlbit	= S5PC100_CLKGATE_SCLK1_AUDIO0,
		.enable		= s5pc100_sclk1_ctrl,
701
		.ops		= &s5pc100_clksrc_ops,
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
	},
	.shift		= S5PC100_CLKSRC3_AUDIO0_SHIFT,
	.mask		= S5PC100_CLKSRC3_AUDIO0_MASK,
	.sources	= &clkset_audio0,
	.divider_shift	= S5PC100_CLKDIV4_AUDIO0_SHIFT,
	.reg_divider	= S5PC100_CLKDIV4,
	.reg_source	= S5PC100_CLKSRC3,
};

static struct clk *clkset_audio1_list[] = {
	&clk_mout_epll.clk,
	&clk_dout_mpll,
	&clk_fin_epll,
	&clk_iis_cd1,
	&clk_pcm_cd1,
	&clk_mout_hpll.clk,
};

static struct clk_sources clkset_audio1 = {
	.sources	= clkset_audio1_list,
	.nr_sources	= ARRAY_SIZE(clkset_audio1_list),
};

static struct clksrc_clk clk_audio1 = {
	.clk	= {
		.name		= "audio-bus",
		.id		= 1,
		.ctrlbit	= S5PC100_CLKGATE_SCLK1_AUDIO1,
		.enable		= s5pc100_sclk1_ctrl,
731
		.ops		= &s5pc100_clksrc_ops,
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
	},
	.shift		= S5PC100_CLKSRC3_AUDIO1_SHIFT,
	.mask		= S5PC100_CLKSRC3_AUDIO1_MASK,
	.sources	= &clkset_audio1,
	.divider_shift	= S5PC100_CLKDIV4_AUDIO1_SHIFT,
	.reg_divider	= S5PC100_CLKDIV4,
	.reg_source	= S5PC100_CLKSRC3,
};

static struct clk *clkset_audio2_list[] = {
	&clk_mout_epll.clk,
	&clk_dout_mpll,
	&clk_fin_epll,
	&clk_iis_cd2,
	&clk_mout_hpll.clk,
};

static struct clk_sources clkset_audio2 = {
	.sources	= clkset_audio2_list,
	.nr_sources	= ARRAY_SIZE(clkset_audio2_list),
};

static struct clksrc_clk clk_audio2 = {
	.clk	= {
		.name		= "audio-bus",
		.id		= 2,
		.ctrlbit	= S5PC100_CLKGATE_SCLK1_AUDIO2,
		.enable		= s5pc100_sclk1_ctrl,
760
		.ops		= &s5pc100_clksrc_ops,
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
	},
	.shift		= S5PC100_CLKSRC3_AUDIO2_SHIFT,
	.mask		= S5PC100_CLKSRC3_AUDIO2_MASK,
	.sources	= &clkset_audio2,
	.divider_shift	= S5PC100_CLKDIV4_AUDIO2_SHIFT,
	.reg_divider	= S5PC100_CLKDIV4,
	.reg_source	= S5PC100_CLKSRC3,
};

static struct clk *clkset_spdif_list[] = {
	&clk_audio0.clk,
	&clk_audio1.clk,
	&clk_audio2.clk,
};

static struct clk_sources clkset_spdif = {
	.sources	= clkset_spdif_list,
	.nr_sources	= ARRAY_SIZE(clkset_spdif_list),
};

static struct clksrc_clk clk_spdif = {
	.clk	= {
		.name		= "spdif",
		.id		= -1,
	},
	.shift		= S5PC100_CLKSRC3_SPDIF_SHIFT,
	.mask		= S5PC100_CLKSRC3_SPDIF_MASK,
	.sources	= &clkset_spdif,
	.reg_source	= S5PC100_CLKSRC3,
};

static struct clk *clkset_lcd_fimc_list[] = {
	&clk_mout_epll.clk,
	&clk_dout_mpll,
	&clk_mout_hpll.clk,
	&clk_vclk_54m,
};

static struct clk_sources clkset_lcd_fimc = {
	.sources	= clkset_lcd_fimc_list,
	.nr_sources	= ARRAY_SIZE(clkset_lcd_fimc_list),
};

static struct clksrc_clk clk_lcd = {
	.clk	= {
		.name		= "lcd",
		.id		= -1,
		.ctrlbit	= S5PC100_CLKGATE_SCLK1_LCD,
		.enable		= s5pc100_sclk1_ctrl,
810
		.ops		= &s5pc100_clksrc_ops,
811 812 813 814 815 816 817 818 819 820 821 822 823 824 825
	},
	.shift		= S5PC100_CLKSRC2_LCD_SHIFT,
	.mask		= S5PC100_CLKSRC2_LCD_MASK,
	.sources	= &clkset_lcd_fimc,
	.divider_shift	= S5PC100_CLKDIV3_LCD_SHIFT,
	.reg_divider	= S5PC100_CLKDIV3,
	.reg_source	= S5PC100_CLKSRC2,
};

static struct clksrc_clk clk_fimc0 = {
	.clk	= {
		.name		= "fimc",
		.id		= 0,
		.ctrlbit	= S5PC100_CLKGATE_SCLK1_FIMC0,
		.enable		= s5pc100_sclk1_ctrl,
826
		.ops		= &s5pc100_clksrc_ops,
827 828 829 830 831 832 833 834 835 836 837 838 839 840 841
	},
	.shift		= S5PC100_CLKSRC2_FIMC0_SHIFT,
	.mask		= S5PC100_CLKSRC2_FIMC0_MASK,
	.sources	= &clkset_lcd_fimc,
	.divider_shift	= S5PC100_CLKDIV3_FIMC0_SHIFT,
	.reg_divider	= S5PC100_CLKDIV3,
	.reg_source	= S5PC100_CLKSRC2,
};

static struct clksrc_clk clk_fimc1 = {
	.clk	= {
		.name		= "fimc",
		.id		= 1,
		.ctrlbit	= S5PC100_CLKGATE_SCLK1_FIMC1,
		.enable		= s5pc100_sclk1_ctrl,
842
		.ops		= &s5pc100_clksrc_ops,
843 844 845 846 847 848 849 850 851 852 853 854 855 856 857
	},
	.shift		= S5PC100_CLKSRC2_FIMC1_SHIFT,
	.mask		= S5PC100_CLKSRC2_FIMC1_MASK,
	.sources	= &clkset_lcd_fimc,
	.divider_shift	= S5PC100_CLKDIV3_FIMC1_SHIFT,
	.reg_divider	= S5PC100_CLKDIV3,
	.reg_source	= S5PC100_CLKSRC2,
};

static struct clksrc_clk clk_fimc2 = {
	.clk	= {
		.name		= "fimc",
		.id		= 2,
		.ctrlbit	= S5PC100_CLKGATE_SCLK1_FIMC2,
		.enable		= s5pc100_sclk1_ctrl,
858
		.ops		= &s5pc100_clksrc_ops,
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
	},
	.shift		= S5PC100_CLKSRC2_FIMC2_SHIFT,
	.mask		= S5PC100_CLKSRC2_FIMC2_MASK,
	.sources	= &clkset_lcd_fimc,
	.divider_shift	= S5PC100_CLKDIV3_FIMC2_SHIFT,
	.reg_divider	= S5PC100_CLKDIV3,
	.reg_source	= S5PC100_CLKSRC2,
};

static struct clk *clkset_mmc_list[] = {
	&clk_mout_epll.clk,
	&clk_dout_mpll,
	&clk_fin_epll,
	&clk_mout_hpll.clk ,
};

static struct clk_sources clkset_mmc = {
	.sources	= clkset_mmc_list,
	.nr_sources	= ARRAY_SIZE(clkset_mmc_list),
};

static struct clksrc_clk clk_mmc0 = {
	.clk	= {
		.name		= "mmc_bus",
		.id		= 0,
		.ctrlbit	= S5PC100_CLKGATE_SCLK0_MMC0,
		.enable		= s5pc100_sclk0_ctrl,
886
		.ops		= &s5pc100_clksrc_ops,
887 888 889 890 891 892 893 894 895 896 897 898 899 900 901
	},
	.shift		= S5PC100_CLKSRC2_MMC0_SHIFT,
	.mask		= S5PC100_CLKSRC2_MMC0_MASK,
	.sources	= &clkset_mmc,
	.divider_shift	= S5PC100_CLKDIV3_MMC0_SHIFT,
	.reg_divider	= S5PC100_CLKDIV3,
	.reg_source	= S5PC100_CLKSRC2,
};

static struct clksrc_clk clk_mmc1 = {
	.clk	= {
		.name		= "mmc_bus",
		.id		= 1,
		.ctrlbit	= S5PC100_CLKGATE_SCLK0_MMC1,
		.enable		= s5pc100_sclk0_ctrl,
902
		.ops		= &s5pc100_clksrc_ops,
903 904 905 906 907 908 909 910 911 912 913 914 915 916 917
	},
	.shift		= S5PC100_CLKSRC2_MMC1_SHIFT,
	.mask		= S5PC100_CLKSRC2_MMC1_MASK,
	.sources	= &clkset_mmc,
	.divider_shift	= S5PC100_CLKDIV3_MMC1_SHIFT,
	.reg_divider	= S5PC100_CLKDIV3,
	.reg_source	= S5PC100_CLKSRC2,
};

static struct clksrc_clk clk_mmc2 = {
	.clk	= {
		.name		= "mmc_bus",
		.id		= 2,
		.ctrlbit	= S5PC100_CLKGATE_SCLK0_MMC2,
		.enable		= s5pc100_sclk0_ctrl,
918
		.ops		= &s5pc100_clksrc_ops,
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
	},
	.shift		= S5PC100_CLKSRC2_MMC2_SHIFT,
	.mask		= S5PC100_CLKSRC2_MMC2_MASK,
	.sources	= &clkset_mmc,
	.divider_shift	= S5PC100_CLKDIV3_MMC2_SHIFT,
	.reg_divider	= S5PC100_CLKDIV3,
	.reg_source	= S5PC100_CLKSRC2,
};


static struct clk *clkset_usbhost_list[] = {
	&clk_mout_epll.clk,
	&clk_dout_mpll,
	&clk_mout_hpll.clk,
	&clk_48m,
};

static struct clk_sources clkset_usbhost = {
	.sources	= clkset_usbhost_list,
	.nr_sources	= ARRAY_SIZE(clkset_usbhost_list),
};

static struct clksrc_clk clk_usbhost = {
	.clk	= {
		.name		= "usbhost",
		.id		= -1,
		.ctrlbit        = S5PC100_CLKGATE_SCLK0_USBHOST,
		.enable		= s5pc100_sclk0_ctrl,
947
		.ops		= &s5pc100_clksrc_ops,
948 949 950 951 952 953 954 955 956
	},
	.shift		= S5PC100_CLKSRC1_UHOST_SHIFT,
	.mask		= S5PC100_CLKSRC1_UHOST_MASK,
	.sources	= &clkset_usbhost,
	.divider_shift	= S5PC100_CLKDIV2_UHOST_SHIFT,
	.reg_divider	= S5PC100_CLKDIV2,
	.reg_source	= S5PC100_CLKSRC1,
};

957 958 959 960 961
/* Clock initialisation code */

static struct clksrc_clk *init_parents[] = {
	&clk_mout_apll,
	&clk_mout_mpll,
962 963 964 965 966 967 968
	&clk_mout_am,
	&clk_mout_onenand,
	&clk_mout_epll,
	&clk_mout_hpll,
	&clk_spi0,
	&clk_spi1,
	&clk_spi2,
969
	&clk_uart_uclk1,
970 971 972 973 974 975 976 977 978 979 980 981
	&clk_audio0,
	&clk_audio1,
	&clk_audio2,
	&clk_spdif,
	&clk_lcd,
	&clk_fimc0,
	&clk_fimc1,
	&clk_fimc2,
	&clk_mmc0,
	&clk_mmc1,
	&clk_mmc2,
	&clk_usbhost,
982 983
};

984
static void __init_or_cpufreq s5pc100_set_clksrc(struct clksrc_clk *clk)
985 986 987 988 989 990 991 992 993 994 995 996 997 998 999
{
	struct clk_sources *srcs = clk->sources;
	u32 clksrc = __raw_readl(clk->reg_source);

	clksrc &= clk->mask;
	clksrc >>= clk->shift;

	if (clksrc > srcs->nr_sources || !srcs->sources[clksrc]) {
		printk(KERN_ERR "%s: bad source %d\n",
		       clk->clk.name, clksrc);
		return;
	}

	clk->clk.parent = srcs->sources[clksrc];

1000 1001 1002
	printk(KERN_INFO "%s: source is %s (%d), rate is %ld.%03ld MHz\n",
		clk->clk.name, clk->clk.parent->name, clksrc,
		print_mhz(clk_get_rate(&clk->clk)));
1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015
}

#define GET_DIV(clk, field) ((((clk) & field##_MASK) >> field##_SHIFT) + 1)

void __init_or_cpufreq s5pc100_setup_clocks(void)
{
	struct clk *xtal_clk;
	unsigned long xtal;
	unsigned long armclk;
	unsigned long hclkd0;
	unsigned long hclk;
	unsigned long pclkd0;
	unsigned long pclk;
1016
	unsigned long apll, mpll, epll, hpll;
1017 1018 1019 1020 1021
	unsigned int ptr;
	u32 clkdiv0, clkdiv1;

	printk(KERN_DEBUG "%s: registering clocks\n", __func__);

1022 1023
	clkdiv0 = __raw_readl(S5PC100_CLKDIV0);
	clkdiv1 = __raw_readl(S5PC100_CLKDIV1);
1024

1025
	printk(KERN_DEBUG "%s: clkdiv0 = %08x, clkdiv1 = %08x\n", __func__, clkdiv0, clkdiv1);
1026 1027 1028 1029 1030 1031 1032 1033 1034

	xtal_clk = clk_get(NULL, "xtal");
	BUG_ON(IS_ERR(xtal_clk));

	xtal = clk_get_rate(xtal_clk);
	clk_put(xtal_clk);

	printk(KERN_DEBUG "%s: xtal is %ld\n", __func__, xtal);

1035 1036 1037
	apll = s5pc1xx_get_pll(xtal, __raw_readl(S5PC100_APLL_CON));
	mpll = s5pc1xx_get_pll(xtal, __raw_readl(S5PC100_MPLL_CON));
	epll = s5pc1xx_get_pll(xtal, __raw_readl(S5PC100_EPLL_CON));
1038 1039
	hpll = s5pc1xx_get_pll(xtal, __raw_readl(S5PC100_HPLL_CON));

1040 1041 1042 1043
	printk(KERN_INFO "S5PC100: Apll=%ld.%03ld Mhz, Mpll=%ld.%03ld Mhz"
		", Epll=%ld.%03ld Mhz, Hpll=%ld.%03ld Mhz\n",
		print_mhz(apll), print_mhz(mpll),
		print_mhz(epll), print_mhz(hpll));
1044

1045
	armclk = apll / GET_DIV(clkdiv0, S5PC100_CLKDIV0_APLL);
1046 1047 1048 1049 1050 1051
	armclk = armclk / GET_DIV(clkdiv0, S5PC100_CLKDIV0_ARM);
	hclkd0 = armclk / GET_DIV(clkdiv0, S5PC100_CLKDIV0_D0);
	pclkd0 = hclkd0 / GET_DIV(clkdiv0, S5PC100_CLKDIV0_PCLKD0);
	hclk = mpll / GET_DIV(clkdiv1, S5PC100_CLKDIV1_D1);
	pclk = hclk / GET_DIV(clkdiv1, S5PC100_CLKDIV1_PCLKD1);

1052 1053 1054 1055 1056
	printk(KERN_INFO "S5PC100: ARMCLK=%ld.%03ld MHz, HCLKD0=%ld.%03ld MHz,"
		" PCLKD0=%ld.%03ld MHz\n, HCLK=%ld.%03ld MHz,"
		" PCLK=%ld.%03ld MHz\n",
		print_mhz(armclk), print_mhz(hclkd0),
		print_mhz(pclkd0), print_mhz(hclk), print_mhz(pclk));
1057 1058 1059 1060

	clk_fout_apll.rate = apll;
	clk_fout_mpll.rate = mpll;
	clk_fout_epll.rate = epll;
1061
	clk_fout_hpll.rate = hpll;
1062 1063 1064

	clk_h.rate = hclk;
	clk_p.rate = pclk;
1065
	clk_f.rate = armclk;
1066 1067

	for (ptr = 0; ptr < ARRAY_SIZE(init_parents); ptr++)
1068
		s5pc100_set_clksrc(init_parents[ptr]);
1069 1070 1071 1072
}

static struct clk *clks[] __initdata = {
	&clk_ext_xtal_mux,
1073 1074 1075 1076 1077
	&clk_mout_apll.clk,
	&clk_dout_apll,
	&clk_dout_d0_bus,
	&clk_dout_pclkd0,
	&clk_dout_apll2,
1078
	&clk_mout_mpll.clk,
1079 1080 1081 1082 1083 1084
	&clk_mout_am.clk,
	&clk_dout_d1_bus,
	&clk_mout_onenand.clk,
	&clk_dout_pclkd1,
	&clk_dout_mpll2,
	&clk_dout_cam,
1085
	&clk_dout_mpll,
1086 1087 1088 1089 1090 1091 1092 1093 1094 1095
	&clk_mout_epll.clk,
	&clk_fout_epll,
	&clk_iis_cd0,
	&clk_iis_cd1,
	&clk_iis_cd2,
	&clk_pcm_cd0,
	&clk_pcm_cd1,
	&clk_spi0.clk,
	&clk_spi1.clk,
	&clk_spi2.clk,
1096
	&clk_uart_uclk1.clk,
1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109
	&clk_audio0.clk,
	&clk_audio1.clk,
	&clk_audio2.clk,
	&clk_spdif.clk,
	&clk_lcd.clk,
	&clk_fimc0.clk,
	&clk_fimc1.clk,
	&clk_fimc2.clk,
	&clk_mmc0.clk,
	&clk_mmc1.clk,
	&clk_mmc2.clk,
	&clk_usbhost.clk,
	&clk_arm,
1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126
};

void __init s5pc100_register_clocks(void)
{
	struct clk *clkp;
	int ret;
	int ptr;

	for (ptr = 0; ptr < ARRAY_SIZE(clks); ptr++) {
		clkp = clks[ptr];
		ret = s3c24xx_register_clock(clkp);
		if (ret < 0) {
			printk(KERN_ERR "Failed to register clock %s (%d)\n",
			       clkp->name, ret);
		}
	}
}