clk-prima2.c 25.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
/*
 * Clock tree for CSR SiRFprimaII
 *
 * Copyright (c) 2011 Cambridge Silicon Radio Limited, a CSR plc group company.
 *
 * Licensed under GPLv2 or later.
 */

#include <linux/module.h>
#include <linux/bitops.h>
#include <linux/io.h>
#include <linux/clk.h>
13 14
#include <linux/clkdev.h>
#include <linux/clk-provider.h>
15
#include <linux/of_address.h>
16
#include <linux/syscore_ops.h>
17 18 19 20 21 22 23 24 25 26 27

#define SIRFSOC_CLKC_CLK_EN0    0x0000
#define SIRFSOC_CLKC_CLK_EN1    0x0004
#define SIRFSOC_CLKC_REF_CFG    0x0014
#define SIRFSOC_CLKC_CPU_CFG    0x0018
#define SIRFSOC_CLKC_MEM_CFG    0x001c
#define SIRFSOC_CLKC_SYS_CFG    0x0020
#define SIRFSOC_CLKC_IO_CFG     0x0024
#define SIRFSOC_CLKC_DSP_CFG    0x0028
#define SIRFSOC_CLKC_GFX_CFG    0x002c
#define SIRFSOC_CLKC_MM_CFG     0x0030
28
#define SIRFSOC_CLKC_LCD_CFG     0x0034
29 30 31 32 33 34 35 36 37 38
#define SIRFSOC_CLKC_MMC_CFG    0x0038
#define SIRFSOC_CLKC_PLL1_CFG0  0x0040
#define SIRFSOC_CLKC_PLL2_CFG0  0x0044
#define SIRFSOC_CLKC_PLL3_CFG0  0x0048
#define SIRFSOC_CLKC_PLL1_CFG1  0x004c
#define SIRFSOC_CLKC_PLL2_CFG1  0x0050
#define SIRFSOC_CLKC_PLL3_CFG1  0x0054
#define SIRFSOC_CLKC_PLL1_CFG2  0x0058
#define SIRFSOC_CLKC_PLL2_CFG2  0x005c
#define SIRFSOC_CLKC_PLL3_CFG2  0x0060
39 40 41 42
#define SIRFSOC_USBPHY_PLL_CTRL 0x0008
#define SIRFSOC_USBPHY_PLL_POWERDOWN  BIT(1)
#define SIRFSOC_USBPHY_PLL_BYPASS     BIT(2)
#define SIRFSOC_USBPHY_PLL_LOCK       BIT(3)
43

44
static void *sirfsoc_clk_vbase, *sirfsoc_rsc_vbase;
45 46 47 48

#define KHZ     1000
#define MHZ     (KHZ * KHZ)

49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
/*
 * SiRFprimaII clock controller
 * - 2 oscillators: osc-26MHz, rtc-32.768KHz
 * - 3 standard configurable plls: pll1, pll2 & pll3
 * - 2 exclusive plls: usb phy pll and sata phy pll
 * - 8 clock domains: cpu/cpudiv, mem/memdiv, sys/io, dsp, graphic, multimedia,
 *     display and sdphy.
 *     Each clock domain can select its own clock source from five clock sources,
 *     X_XIN, X_XINW, PLL1, PLL2 and PLL3. The domain clock is used as the source
 *     clock of the group clock.
 *     - dsp domain: gps, mf
 *     - io domain: dmac, nand, audio, uart, i2c, spi, usp, pwm, pulse
 *     - sys domain: security
 */

struct clk_pll {
	struct clk_hw hw;
	unsigned short regofs;  /* register offset */
67 68
};

69 70 71 72
#define to_pllclk(_hw) container_of(_hw, struct clk_pll, hw)

struct clk_dmn {
	struct clk_hw hw;
73 74 75 76
	signed char enable_bit; /* enable bit: 0 ~ 63 */
	unsigned short regofs;  /* register offset */
};

77 78 79 80 81 82 83 84 85 86 87 88
#define to_dmnclk(_hw) container_of(_hw, struct clk_dmn, hw)

struct clk_std {
	struct clk_hw hw;
	signed char enable_bit; /* enable bit: 0 ~ 63 */
};

#define to_stdclk(_hw) container_of(_hw, struct clk_std, hw)

static int std_clk_is_enabled(struct clk_hw *hw);
static int std_clk_enable(struct clk_hw *hw);
static void std_clk_disable(struct clk_hw *hw);
89 90 91

static inline unsigned long clkc_readl(unsigned reg)
{
92
	return readl(sirfsoc_clk_vbase + reg);
93 94 95 96
}

static inline void clkc_writel(u32 val, unsigned reg)
{
97
	writel(val, sirfsoc_clk_vbase + reg);
98 99 100 101 102
}

/*
 * std pll
 */
103 104 105

static unsigned long pll_clk_recalc_rate(struct clk_hw *hw,
	unsigned long parent_rate)
106
{
107 108
	unsigned long fin = parent_rate;
	struct clk_pll *clk = to_pllclk(hw);
109 110 111 112 113
	u32 regcfg2 = clk->regofs + SIRFSOC_CLKC_PLL1_CFG2 -
		SIRFSOC_CLKC_PLL1_CFG0;

	if (clkc_readl(regcfg2) & BIT(2)) {
		/* pll bypass mode */
114
		return fin;
115 116 117 118 119 120 121
	} else {
		/* fout = fin * nf / nr / od */
		u32 cfg0 = clkc_readl(clk->regofs);
		u32 nf = (cfg0 & (BIT(13) - 1)) + 1;
		u32 nr = ((cfg0 >> 13) & (BIT(6) - 1)) + 1;
		u32 od = ((cfg0 >> 19) & (BIT(4) - 1)) + 1;
		WARN_ON(fin % MHZ);
122
		return fin / MHZ * nf / nr / od * MHZ;
123
	}
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
}

static long pll_clk_round_rate(struct clk_hw *hw, unsigned long rate,
	unsigned long *parent_rate)
{
	unsigned long fin, nf, nr, od;

	/*
	 * fout = fin * nf / (nr * od);
	 * set od = 1, nr = fin/MHz, so fout = nf * MHz
	 */
	rate = rate - rate % MHZ;

	nf = rate / MHZ;
	if (nf > BIT(13))
		nf = BIT(13);
	if (nf < 1)
		nf = 1;
142

143 144 145 146 147 148 149 150
	fin = *parent_rate;

	nr = fin / MHZ;
	if (nr > BIT(6))
		nr = BIT(6);
	od = 1;

	return fin * nf / (nr * od);
151 152
}

153 154
static int pll_clk_set_rate(struct clk_hw *hw, unsigned long rate,
	unsigned long parent_rate)
155
{
156
	struct clk_pll *clk = to_pllclk(hw);
157 158 159 160 161 162 163 164 165 166 167
	unsigned long fin, nf, nr, od, reg;

	/*
	 * fout = fin * nf / (nr * od);
	 * set od = 1, nr = fin/MHz, so fout = nf * MHz
	 */

	nf = rate / MHZ;
	if (unlikely((rate % MHZ) || nf > BIT(13) || nf < 1))
		return -EINVAL;

168
	fin = parent_rate;
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
	BUG_ON(fin < MHZ);

	nr = fin / MHZ;
	BUG_ON((fin % MHZ) || nr > BIT(6));

	od = 1;

	reg = (nf - 1) | ((nr - 1) << 13) | ((od - 1) << 19);
	clkc_writel(reg, clk->regofs);

	reg = clk->regofs + SIRFSOC_CLKC_PLL1_CFG1 - SIRFSOC_CLKC_PLL1_CFG0;
	clkc_writel((nf >> 1) - 1, reg);

	reg = clk->regofs + SIRFSOC_CLKC_PLL1_CFG2 - SIRFSOC_CLKC_PLL1_CFG0;
	while (!(clkc_readl(reg) & BIT(6)))
		cpu_relax();

	return 0;
}

static struct clk_ops std_pll_ops = {
190 191 192
	.recalc_rate = pll_clk_recalc_rate,
	.round_rate = pll_clk_round_rate,
	.set_rate = pll_clk_set_rate,
193 194
};

195 196 197 198 199 200
static const char *pll_clk_parents[] = {
	"osc",
};

static struct clk_init_data clk_pll1_init = {
	.name = "pll1",
201
	.ops = &std_pll_ops,
202 203
	.parent_names = pll_clk_parents,
	.num_parents = ARRAY_SIZE(pll_clk_parents),
204 205
};

206 207
static struct clk_init_data clk_pll2_init = {
	.name = "pll2",
208
	.ops = &std_pll_ops,
209 210
	.parent_names = pll_clk_parents,
	.num_parents = ARRAY_SIZE(pll_clk_parents),
211 212
};

213 214
static struct clk_init_data clk_pll3_init = {
	.name = "pll3",
215
	.ops = &std_pll_ops,
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
	.parent_names = pll_clk_parents,
	.num_parents = ARRAY_SIZE(pll_clk_parents),
};

static struct clk_pll clk_pll1 = {
	.regofs = SIRFSOC_CLKC_PLL1_CFG0,
	.hw = {
		.init = &clk_pll1_init,
	},
};

static struct clk_pll clk_pll2 = {
	.regofs = SIRFSOC_CLKC_PLL2_CFG0,
	.hw = {
		.init = &clk_pll2_init,
	},
};

static struct clk_pll clk_pll3 = {
	.regofs = SIRFSOC_CLKC_PLL3_CFG0,
	.hw = {
		.init = &clk_pll3_init,
	},
239 240 241
};

/*
242
 * usb uses specified pll
243 244
 */

245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
static int usb_pll_clk_enable(struct clk_hw *hw)
{
	u32 reg = readl(sirfsoc_rsc_vbase + SIRFSOC_USBPHY_PLL_CTRL);
	reg &= ~(SIRFSOC_USBPHY_PLL_POWERDOWN | SIRFSOC_USBPHY_PLL_BYPASS);
	writel(reg, sirfsoc_rsc_vbase + SIRFSOC_USBPHY_PLL_CTRL);
	while (!(readl(sirfsoc_rsc_vbase + SIRFSOC_USBPHY_PLL_CTRL) &
			SIRFSOC_USBPHY_PLL_LOCK))
		cpu_relax();

	return 0;
}

static void usb_pll_clk_disable(struct clk_hw *clk)
{
	u32 reg = readl(sirfsoc_rsc_vbase + SIRFSOC_USBPHY_PLL_CTRL);
	reg |= (SIRFSOC_USBPHY_PLL_POWERDOWN | SIRFSOC_USBPHY_PLL_BYPASS);
	writel(reg, sirfsoc_rsc_vbase + SIRFSOC_USBPHY_PLL_CTRL);
}
263

264
static unsigned long usb_pll_clk_recalc_rate(struct clk_hw *hw, unsigned long parent_rate)
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
	u32 reg = readl(sirfsoc_rsc_vbase + SIRFSOC_USBPHY_PLL_CTRL);
	return (reg & SIRFSOC_USBPHY_PLL_BYPASS) ? parent_rate : 48*MHZ;
}

static struct clk_ops usb_pll_ops = {
	.enable = usb_pll_clk_enable,
	.disable = usb_pll_clk_disable,
	.recalc_rate = usb_pll_clk_recalc_rate,
};

static struct clk_init_data clk_usb_pll_init = {
	.name = "usb_pll",
	.ops = &usb_pll_ops,
	.parent_names = pll_clk_parents,
	.num_parents = ARRAY_SIZE(pll_clk_parents),
};

static struct clk_hw usb_pll_clk_hw = {
	.init = &clk_usb_pll_init,
};

/*
 * clock domains - cpu, mem, sys/io, dsp, gfx
 */

static const char *dmn_clk_parents[] = {
	"rtc",
	"osc",
	"pll1",
	"pll2",
	"pll3",
};

static u8 dmn_clk_get_parent(struct clk_hw *hw)
{
	struct clk_dmn *clk = to_dmnclk(hw);
302
	u32 cfg = clkc_readl(clk->regofs);
303 304 305 306 307

	/* parent of io domain can only be pll3 */
	if (strcmp(hw->init->name, "io") == 0)
		return 4;

308
	WARN_ON((cfg & (BIT(3) - 1)) > 4);
309 310

	return cfg & (BIT(3) - 1);
311 312
}

313
static int dmn_clk_set_parent(struct clk_hw *hw, u8 parent)
314
{
315
	struct clk_dmn *clk = to_dmnclk(hw);
316
	u32 cfg = clkc_readl(clk->regofs);
317 318 319 320 321 322 323 324 325 326 327 328

	/* parent of io domain can only be pll3 */
	if (strcmp(hw->init->name, "io") == 0)
		return -EINVAL;

	cfg &= ~(BIT(3) - 1);
	clkc_writel(cfg | parent, clk->regofs);
	/* BIT(3) - switching status: 1 - busy, 0 - done */
	while (clkc_readl(clk->regofs) & BIT(3))
		cpu_relax();

	return 0;
329 330
}

331 332 333
static unsigned long dmn_clk_recalc_rate(struct clk_hw *hw,
	unsigned long parent_rate)

334
{
335 336 337
	unsigned long fin = parent_rate;
	struct clk_dmn *clk = to_dmnclk(hw);

338
	u32 cfg = clkc_readl(clk->regofs);
339

340 341
	if (cfg & BIT(24)) {
		/* fcd bypass mode */
342
		return fin;
343 344 345 346 347 348 349
	} else {
		/*
		 * wait count: bit[19:16], hold count: bit[23:20]
		 */
		u32 wait = (cfg >> 16) & (BIT(4) - 1);
		u32 hold = (cfg >> 20) & (BIT(4) - 1);

350
		return fin / (wait + hold + 2);
351
	}
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
}

static long dmn_clk_round_rate(struct clk_hw *hw, unsigned long rate,
	unsigned long *parent_rate)
{
	unsigned long fin;
	unsigned ratio, wait, hold;
	unsigned bits = (strcmp(hw->init->name, "mem") == 0) ? 3 : 4;

	fin = *parent_rate;
	ratio = fin / rate;

	if (ratio < 2)
		ratio = 2;
	if (ratio > BIT(bits + 1))
		ratio = BIT(bits + 1);
368

369 370 371 372
	wait = (ratio >> 1) - 1;
	hold = ratio - wait - 2;

	return fin / (wait + hold + 2);
373 374
}

375 376
static int dmn_clk_set_rate(struct clk_hw *hw, unsigned long rate,
	unsigned long parent_rate)
377
{
378
	struct clk_dmn *clk = to_dmnclk(hw);
379 380
	unsigned long fin;
	unsigned ratio, wait, hold, reg;
381
	unsigned bits = (strcmp(hw->init->name, "mem") == 0) ? 3 : 4;
382

383
	fin = parent_rate;
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
	ratio = fin / rate;

	if (unlikely(ratio < 2 || ratio > BIT(bits + 1)))
		return -EINVAL;

	WARN_ON(fin % rate);

	wait = (ratio >> 1) - 1;
	hold = ratio - wait - 2;

	reg = clkc_readl(clk->regofs);
	reg &= ~(((BIT(bits) - 1) << 16) | ((BIT(bits) - 1) << 20));
	reg |= (wait << 16) | (hold << 20) | BIT(25);
	clkc_writel(reg, clk->regofs);

	/* waiting FCD been effective */
	while (clkc_readl(clk->regofs) & BIT(25))
		cpu_relax();

	return 0;
}

406 407 408 409 410 411 412
static struct clk_ops msi_ops = {
	.set_rate = dmn_clk_set_rate,
	.round_rate = dmn_clk_round_rate,
	.recalc_rate = dmn_clk_recalc_rate,
	.set_parent = dmn_clk_set_parent,
	.get_parent = dmn_clk_get_parent,
};
413

414 415 416 417 418 419
static struct clk_init_data clk_mem_init = {
	.name = "mem",
	.ops = &msi_ops,
	.parent_names = dmn_clk_parents,
	.num_parents = ARRAY_SIZE(dmn_clk_parents),
};
420

421 422 423 424 425 426
static struct clk_dmn clk_mem = {
	.regofs = SIRFSOC_CLKC_MEM_CFG,
	.hw = {
		.init = &clk_mem_init,
	},
};
427

428 429 430 431 432 433 434
static struct clk_init_data clk_sys_init = {
	.name = "sys",
	.ops = &msi_ops,
	.parent_names = dmn_clk_parents,
	.num_parents = ARRAY_SIZE(dmn_clk_parents),
	.flags = CLK_SET_RATE_GATE,
};
435

436 437 438 439 440 441
static struct clk_dmn clk_sys = {
	.regofs = SIRFSOC_CLKC_SYS_CFG,
	.hw = {
		.init = &clk_sys_init,
	},
};
442

443 444 445 446 447 448
static struct clk_init_data clk_io_init = {
	.name = "io",
	.ops = &msi_ops,
	.parent_names = dmn_clk_parents,
	.num_parents = ARRAY_SIZE(dmn_clk_parents),
};
449

450 451 452 453 454 455
static struct clk_dmn clk_io = {
	.regofs = SIRFSOC_CLKC_IO_CFG,
	.hw = {
		.init = &clk_io_init,
	},
};
456 457

static struct clk_ops cpu_ops = {
458 459
	.set_parent = dmn_clk_set_parent,
	.get_parent = dmn_clk_get_parent,
460 461
};

462 463
static struct clk_init_data clk_cpu_init = {
	.name = "cpu",
464
	.ops = &cpu_ops,
465 466 467
	.parent_names = dmn_clk_parents,
	.num_parents = ARRAY_SIZE(dmn_clk_parents),
	.flags = CLK_SET_RATE_PARENT,
468 469
};

470 471 472 473 474 475
static struct clk_dmn clk_cpu = {
	.regofs = SIRFSOC_CLKC_CPU_CFG,
	.hw = {
		.init = &clk_cpu_init,
	},
};
476

477 478 479 480 481 482 483 484 485
static struct clk_ops dmn_ops = {
	.is_enabled = std_clk_is_enabled,
	.enable = std_clk_enable,
	.disable = std_clk_disable,
	.set_rate = dmn_clk_set_rate,
	.round_rate = dmn_clk_round_rate,
	.recalc_rate = dmn_clk_recalc_rate,
	.set_parent = dmn_clk_set_parent,
	.get_parent = dmn_clk_get_parent,
486 487
};

488 489 490 491 492 493 494
/* dsp, gfx, mm, lcd and vpp domain */

static struct clk_init_data clk_dsp_init = {
	.name = "dsp",
	.ops = &dmn_ops,
	.parent_names = dmn_clk_parents,
	.num_parents = ARRAY_SIZE(dmn_clk_parents),
495 496
};

497 498 499 500 501 502
static struct clk_dmn clk_dsp = {
	.regofs = SIRFSOC_CLKC_DSP_CFG,
	.enable_bit = 0,
	.hw = {
		.init = &clk_dsp_init,
	},
503 504
};

505 506 507 508 509
static struct clk_init_data clk_gfx_init = {
	.name = "gfx",
	.ops = &dmn_ops,
	.parent_names = dmn_clk_parents,
	.num_parents = ARRAY_SIZE(dmn_clk_parents),
510 511
};

512 513 514 515 516 517 518
static struct clk_dmn clk_gfx = {
	.regofs = SIRFSOC_CLKC_GFX_CFG,
	.enable_bit = 8,
	.hw = {
		.init = &clk_gfx_init,
	},
};
519

520 521 522 523 524 525
static struct clk_init_data clk_mm_init = {
	.name = "mm",
	.ops = &dmn_ops,
	.parent_names = dmn_clk_parents,
	.num_parents = ARRAY_SIZE(dmn_clk_parents),
};
526

527 528 529 530 531 532 533
static struct clk_dmn clk_mm = {
	.regofs = SIRFSOC_CLKC_MM_CFG,
	.enable_bit = 9,
	.hw = {
		.init = &clk_mm_init,
	},
};
534

535 536 537 538 539 540
static struct clk_init_data clk_lcd_init = {
	.name = "lcd",
	.ops = &dmn_ops,
	.parent_names = dmn_clk_parents,
	.num_parents = ARRAY_SIZE(dmn_clk_parents),
};
541

542 543 544 545 546 547 548
static struct clk_dmn clk_lcd = {
	.regofs = SIRFSOC_CLKC_LCD_CFG,
	.enable_bit = 10,
	.hw = {
		.init = &clk_lcd_init,
	},
};
549

550 551 552 553 554 555
static struct clk_init_data clk_vpp_init = {
	.name = "vpp",
	.ops = &dmn_ops,
	.parent_names = dmn_clk_parents,
	.num_parents = ARRAY_SIZE(dmn_clk_parents),
};
556

557 558 559 560 561 562 563
static struct clk_dmn clk_vpp = {
	.regofs = SIRFSOC_CLKC_LCD_CFG,
	.enable_bit = 11,
	.hw = {
		.init = &clk_vpp_init,
	},
};
564

565 566 567 568 569 570
static struct clk_init_data clk_mmc01_init = {
	.name = "mmc01",
	.ops = &dmn_ops,
	.parent_names = dmn_clk_parents,
	.num_parents = ARRAY_SIZE(dmn_clk_parents),
};
571

572 573 574 575 576 577 578
static struct clk_dmn clk_mmc01 = {
	.regofs = SIRFSOC_CLKC_MMC_CFG,
	.enable_bit = 59,
	.hw = {
		.init = &clk_mmc01_init,
	},
};
579

580 581 582 583 584 585
static struct clk_init_data clk_mmc23_init = {
	.name = "mmc23",
	.ops = &dmn_ops,
	.parent_names = dmn_clk_parents,
	.num_parents = ARRAY_SIZE(dmn_clk_parents),
};
586

587 588 589 590 591 592 593
static struct clk_dmn clk_mmc23 = {
	.regofs = SIRFSOC_CLKC_MMC_CFG,
	.enable_bit = 60,
	.hw = {
		.init = &clk_mmc23_init,
	},
};
594

595 596 597 598 599 600
static struct clk_init_data clk_mmc45_init = {
	.name = "mmc45",
	.ops = &dmn_ops,
	.parent_names = dmn_clk_parents,
	.num_parents = ARRAY_SIZE(dmn_clk_parents),
};
601

602 603 604 605 606 607 608
static struct clk_dmn clk_mmc45 = {
	.regofs = SIRFSOC_CLKC_MMC_CFG,
	.enable_bit = 61,
	.hw = {
		.init = &clk_mmc45_init,
	},
};
609

610 611 612 613 614
/*
 * peripheral controllers in io domain
 */

static int std_clk_is_enabled(struct clk_hw *hw)
615
{
616 617 618
	u32 reg;
	int bit;
	struct clk_std *clk = to_stdclk(hw);
619

620 621 622
	bit = clk->enable_bit % 32;
	reg = clk->enable_bit / 32;
	reg = SIRFSOC_CLKC_CLK_EN0 + reg * sizeof(reg);
623

624
	return !!(clkc_readl(reg) & BIT(bit));
625 626
}

627
static int std_clk_enable(struct clk_hw *hw)
628
{
629 630 631
	u32 val, reg;
	int bit;
	struct clk_std *clk = to_stdclk(hw);
632

633 634 635 636 637
	BUG_ON(clk->enable_bit < 0 || clk->enable_bit > 63);

	bit = clk->enable_bit % 32;
	reg = clk->enable_bit / 32;
	reg = SIRFSOC_CLKC_CLK_EN0 + reg * sizeof(reg);
638

639 640 641
	val = clkc_readl(reg) | BIT(bit);
	clkc_writel(val, reg);
	return 0;
642 643
}

644
static void std_clk_disable(struct clk_hw *hw)
645
{
646 647 648
	u32 val, reg;
	int bit;
	struct clk_std *clk = to_stdclk(hw);
649

650
	BUG_ON(clk->enable_bit < 0 || clk->enable_bit > 63);
651

652 653 654
	bit = clk->enable_bit % 32;
	reg = clk->enable_bit / 32;
	reg = SIRFSOC_CLKC_CLK_EN0 + reg * sizeof(reg);
655

656 657
	val = clkc_readl(reg) & ~BIT(bit);
	clkc_writel(val, reg);
658 659
}

660 661 662
static const char *std_clk_io_parents[] = {
	"io",
};
663

664 665 666 667 668
static struct clk_ops ios_ops = {
	.is_enabled = std_clk_is_enabled,
	.enable = std_clk_enable,
	.disable = std_clk_disable,
};
669

670 671 672 673 674 675
static struct clk_init_data clk_dmac0_init = {
	.name = "dmac0",
	.ops = &ios_ops,
	.parent_names = std_clk_io_parents,
	.num_parents = ARRAY_SIZE(std_clk_io_parents),
};
676

677 678 679 680 681 682
static struct clk_std clk_dmac0 = {
	.enable_bit = 32,
	.hw = {
		.init = &clk_dmac0_init,
	},
};
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 1011 1012 1013 1014 1015 1016
static struct clk_init_data clk_dmac1_init = {
	.name = "dmac1",
	.ops = &ios_ops,
	.parent_names = std_clk_io_parents,
	.num_parents = ARRAY_SIZE(std_clk_io_parents),
};

static struct clk_std clk_dmac1 = {
	.enable_bit = 33,
	.hw = {
		.init = &clk_dmac1_init,
	},
};

static struct clk_init_data clk_nand_init = {
	.name = "nand",
	.ops = &ios_ops,
	.parent_names = std_clk_io_parents,
	.num_parents = ARRAY_SIZE(std_clk_io_parents),
};

static struct clk_std clk_nand = {
	.enable_bit = 34,
	.hw = {
		.init = &clk_nand_init,
	},
};

static struct clk_init_data clk_audio_init = {
	.name = "audio",
	.ops = &ios_ops,
	.parent_names = std_clk_io_parents,
	.num_parents = ARRAY_SIZE(std_clk_io_parents),
};

static struct clk_std clk_audio = {
	.enable_bit = 35,
	.hw = {
		.init = &clk_audio_init,
	},
};

static struct clk_init_data clk_uart0_init = {
	.name = "uart0",
	.ops = &ios_ops,
	.parent_names = std_clk_io_parents,
	.num_parents = ARRAY_SIZE(std_clk_io_parents),
};

static struct clk_std clk_uart0 = {
	.enable_bit = 36,
	.hw = {
		.init = &clk_uart0_init,
	},
};

static struct clk_init_data clk_uart1_init = {
	.name = "uart1",
	.ops = &ios_ops,
	.parent_names = std_clk_io_parents,
	.num_parents = ARRAY_SIZE(std_clk_io_parents),
};

static struct clk_std clk_uart1 = {
	.enable_bit = 37,
	.hw = {
		.init = &clk_uart1_init,
	},
};

static struct clk_init_data clk_uart2_init = {
	.name = "uart2",
	.ops = &ios_ops,
	.parent_names = std_clk_io_parents,
	.num_parents = ARRAY_SIZE(std_clk_io_parents),
};

static struct clk_std clk_uart2 = {
	.enable_bit = 38,
	.hw = {
		.init = &clk_uart2_init,
	},
};

static struct clk_init_data clk_usp0_init = {
	.name = "usp0",
	.ops = &ios_ops,
	.parent_names = std_clk_io_parents,
	.num_parents = ARRAY_SIZE(std_clk_io_parents),
};

static struct clk_std clk_usp0 = {
	.enable_bit = 39,
	.hw = {
		.init = &clk_usp0_init,
	},
};

static struct clk_init_data clk_usp1_init = {
	.name = "usp1",
	.ops = &ios_ops,
	.parent_names = std_clk_io_parents,
	.num_parents = ARRAY_SIZE(std_clk_io_parents),
};

static struct clk_std clk_usp1 = {
	.enable_bit = 40,
	.hw = {
		.init = &clk_usp1_init,
	},
};

static struct clk_init_data clk_usp2_init = {
	.name = "usp2",
	.ops = &ios_ops,
	.parent_names = std_clk_io_parents,
	.num_parents = ARRAY_SIZE(std_clk_io_parents),
};

static struct clk_std clk_usp2 = {
	.enable_bit = 41,
	.hw = {
		.init = &clk_usp2_init,
	},
};

static struct clk_init_data clk_vip_init = {
	.name = "vip",
	.ops = &ios_ops,
	.parent_names = std_clk_io_parents,
	.num_parents = ARRAY_SIZE(std_clk_io_parents),
};

static struct clk_std clk_vip = {
	.enable_bit = 42,
	.hw = {
		.init = &clk_vip_init,
	},
};

static struct clk_init_data clk_spi0_init = {
	.name = "spi0",
	.ops = &ios_ops,
	.parent_names = std_clk_io_parents,
	.num_parents = ARRAY_SIZE(std_clk_io_parents),
};

static struct clk_std clk_spi0 = {
	.enable_bit = 43,
	.hw = {
		.init = &clk_spi0_init,
	},
};

static struct clk_init_data clk_spi1_init = {
	.name = "spi1",
	.ops = &ios_ops,
	.parent_names = std_clk_io_parents,
	.num_parents = ARRAY_SIZE(std_clk_io_parents),
};

static struct clk_std clk_spi1 = {
	.enable_bit = 44,
	.hw = {
		.init = &clk_spi1_init,
	},
};

static struct clk_init_data clk_tsc_init = {
	.name = "tsc",
	.ops = &ios_ops,
	.parent_names = std_clk_io_parents,
	.num_parents = ARRAY_SIZE(std_clk_io_parents),
};

static struct clk_std clk_tsc = {
	.enable_bit = 45,
	.hw = {
		.init = &clk_tsc_init,
	},
};

static struct clk_init_data clk_i2c0_init = {
	.name = "i2c0",
	.ops = &ios_ops,
	.parent_names = std_clk_io_parents,
	.num_parents = ARRAY_SIZE(std_clk_io_parents),
};

static struct clk_std clk_i2c0 = {
	.enable_bit = 46,
	.hw = {
		.init = &clk_i2c0_init,
	},
};

static struct clk_init_data clk_i2c1_init = {
	.name = "i2c1",
	.ops = &ios_ops,
	.parent_names = std_clk_io_parents,
	.num_parents = ARRAY_SIZE(std_clk_io_parents),
};

static struct clk_std clk_i2c1 = {
	.enable_bit = 47,
	.hw = {
		.init = &clk_i2c1_init,
	},
};

static struct clk_init_data clk_pwmc_init = {
	.name = "pwmc",
	.ops = &ios_ops,
	.parent_names = std_clk_io_parents,
	.num_parents = ARRAY_SIZE(std_clk_io_parents),
};

static struct clk_std clk_pwmc = {
	.enable_bit = 48,
	.hw = {
		.init = &clk_pwmc_init,
	},
};

static struct clk_init_data clk_efuse_init = {
	.name = "efuse",
	.ops = &ios_ops,
	.parent_names = std_clk_io_parents,
	.num_parents = ARRAY_SIZE(std_clk_io_parents),
};

static struct clk_std clk_efuse = {
	.enable_bit = 49,
	.hw = {
		.init = &clk_efuse_init,
	},
};

static struct clk_init_data clk_pulse_init = {
	.name = "pulse",
	.ops = &ios_ops,
	.parent_names = std_clk_io_parents,
	.num_parents = ARRAY_SIZE(std_clk_io_parents),
};

static struct clk_std clk_pulse = {
	.enable_bit = 50,
	.hw = {
		.init = &clk_pulse_init,
	},
};

static const char *std_clk_dsp_parents[] = {
	"dsp",
};

static struct clk_init_data clk_gps_init = {
	.name = "gps",
	.ops = &ios_ops,
	.parent_names = std_clk_dsp_parents,
	.num_parents = ARRAY_SIZE(std_clk_dsp_parents),
};

static struct clk_std clk_gps = {
	.enable_bit = 1,
	.hw = {
		.init = &clk_gps_init,
	},
};

static struct clk_init_data clk_mf_init = {
	.name = "mf",
	.ops = &ios_ops,
	.parent_names = std_clk_io_parents,
	.num_parents = ARRAY_SIZE(std_clk_io_parents),
};

static struct clk_std clk_mf = {
	.enable_bit = 2,
	.hw = {
		.init = &clk_mf_init,
	},
};

static const char *std_clk_sys_parents[] = {
	"sys",
};

static struct clk_init_data clk_security_init = {
	.name = "mf",
	.ops = &ios_ops,
	.parent_names = std_clk_sys_parents,
	.num_parents = ARRAY_SIZE(std_clk_sys_parents),
};

static struct clk_std clk_security = {
	.enable_bit = 19,
	.hw = {
		.init = &clk_security_init,
	},
};

static const char *std_clk_usb_parents[] = {
	"usb_pll",
};

static struct clk_init_data clk_usb0_init = {
	.name = "usb0",
	.ops = &ios_ops,
	.parent_names = std_clk_usb_parents,
	.num_parents = ARRAY_SIZE(std_clk_usb_parents),
};

static struct clk_std clk_usb0 = {
	.enable_bit = 16,
	.hw = {
		.init = &clk_usb0_init,
	},
};

static struct clk_init_data clk_usb1_init = {
	.name = "usb1",
	.ops = &ios_ops,
	.parent_names = std_clk_usb_parents,
	.num_parents = ARRAY_SIZE(std_clk_usb_parents),
};

static struct clk_std clk_usb1 = {
	.enable_bit = 17,
	.hw = {
		.init = &clk_usb1_init,
	},
};
1017 1018 1019

static struct of_device_id clkc_ids[] = {
	{ .compatible = "sirf,prima2-clkc" },
1020
	{},
1021 1022
};

1023 1024 1025 1026 1027
static struct of_device_id rsc_ids[] = {
	{ .compatible = "sirf,prima2-rsc" },
	{},
};

1028 1029
void __init sirfsoc_of_clk_init(void)
{
1030
	struct clk *clk;
1031 1032 1033 1034 1035 1036
	struct device_node *np;

	np = of_find_matching_node(NULL, clkc_ids);
	if (!np)
		panic("unable to find compatible clkc node in dtb\n");

1037 1038 1039 1040
	sirfsoc_clk_vbase = of_iomap(np, 0);
	if (!sirfsoc_clk_vbase)
		panic("unable to map clkc registers\n");

1041 1042
	of_node_put(np);

1043 1044 1045 1046 1047 1048 1049 1050 1051
	np = of_find_matching_node(NULL, rsc_ids);
	if (!np)
		panic("unable to find compatible rsc node in dtb\n");

	sirfsoc_rsc_vbase = of_iomap(np, 0);
	if (!sirfsoc_rsc_vbase)
		panic("unable to map rsc registers\n");

	of_node_put(np);
1052 1053


1054 1055 1056
	/* These are always available (RTC and 26MHz OSC)*/
	clk = clk_register_fixed_rate(NULL, "rtc", NULL,
		CLK_IS_ROOT, 32768);
1057
	BUG_ON(IS_ERR(clk));
1058 1059
	clk = clk_register_fixed_rate(NULL, "osc", NULL,
		CLK_IS_ROOT, 26000000);
1060
	BUG_ON(IS_ERR(clk));
1061 1062

	clk = clk_register(NULL, &clk_pll1.hw);
1063
	BUG_ON(IS_ERR(clk));
1064
	clk = clk_register(NULL, &clk_pll2.hw);
1065
	BUG_ON(IS_ERR(clk));
1066
	clk = clk_register(NULL, &clk_pll3.hw);
1067
	BUG_ON(IS_ERR(clk));
1068
	clk = clk_register(NULL, &clk_mem.hw);
1069
	BUG_ON(IS_ERR(clk));
1070
	clk = clk_register(NULL, &clk_sys.hw);
1071
	BUG_ON(IS_ERR(clk));
1072
	clk = clk_register(NULL, &clk_security.hw);
1073
	BUG_ON(IS_ERR(clk));
1074 1075
	clk_register_clkdev(clk, NULL, "b8030000.security");
	clk = clk_register(NULL, &clk_dsp.hw);
1076
	BUG_ON(IS_ERR(clk));
1077
	clk = clk_register(NULL, &clk_gps.hw);
1078
	BUG_ON(IS_ERR(clk));
1079 1080
	clk_register_clkdev(clk, NULL, "a8010000.gps");
	clk = clk_register(NULL, &clk_mf.hw);
1081
	BUG_ON(IS_ERR(clk));
1082
	clk = clk_register(NULL, &clk_io.hw);
1083
	BUG_ON(IS_ERR(clk));
1084 1085
	clk_register_clkdev(clk, NULL, "io");
	clk = clk_register(NULL, &clk_cpu.hw);
1086
	BUG_ON(IS_ERR(clk));
1087 1088
	clk_register_clkdev(clk, NULL, "cpu");
	clk = clk_register(NULL, &clk_uart0.hw);
1089
	BUG_ON(IS_ERR(clk));
1090 1091
	clk_register_clkdev(clk, NULL, "b0050000.uart");
	clk = clk_register(NULL, &clk_uart1.hw);
1092
	BUG_ON(IS_ERR(clk));
1093 1094
	clk_register_clkdev(clk, NULL, "b0060000.uart");
	clk = clk_register(NULL, &clk_uart2.hw);
1095
	BUG_ON(IS_ERR(clk));
1096 1097
	clk_register_clkdev(clk, NULL, "b0070000.uart");
	clk = clk_register(NULL, &clk_tsc.hw);
1098
	BUG_ON(IS_ERR(clk));
1099 1100
	clk_register_clkdev(clk, NULL, "b0110000.tsc");
	clk = clk_register(NULL, &clk_i2c0.hw);
1101
	BUG_ON(IS_ERR(clk));
1102 1103
	clk_register_clkdev(clk, NULL, "b00e0000.i2c");
	clk = clk_register(NULL, &clk_i2c1.hw);
1104
	BUG_ON(IS_ERR(clk));
1105 1106
	clk_register_clkdev(clk, NULL, "b00f0000.i2c");
	clk = clk_register(NULL, &clk_spi0.hw);
1107
	BUG_ON(IS_ERR(clk));
1108 1109
	clk_register_clkdev(clk, NULL, "b00d0000.spi");
	clk = clk_register(NULL, &clk_spi1.hw);
1110
	BUG_ON(IS_ERR(clk));
1111 1112
	clk_register_clkdev(clk, NULL, "b0170000.spi");
	clk = clk_register(NULL, &clk_pwmc.hw);
1113
	BUG_ON(IS_ERR(clk));
1114 1115
	clk_register_clkdev(clk, NULL, "b0130000.pwm");
	clk = clk_register(NULL, &clk_efuse.hw);
1116
	BUG_ON(IS_ERR(clk));
1117 1118
	clk_register_clkdev(clk, NULL, "b0140000.efusesys");
	clk = clk_register(NULL, &clk_pulse.hw);
1119
	BUG_ON(IS_ERR(clk));
1120 1121
	clk_register_clkdev(clk, NULL, "b0150000.pulsec");
	clk = clk_register(NULL, &clk_dmac0.hw);
1122
	BUG_ON(IS_ERR(clk));
1123 1124
	clk_register_clkdev(clk, NULL, "b00b0000.dma-controller");
	clk = clk_register(NULL, &clk_dmac1.hw);
1125
	BUG_ON(IS_ERR(clk));
1126 1127
	clk_register_clkdev(clk, NULL, "b0160000.dma-controller");
	clk = clk_register(NULL, &clk_nand.hw);
1128
	BUG_ON(IS_ERR(clk));
1129 1130
	clk_register_clkdev(clk, NULL, "b0030000.nand");
	clk = clk_register(NULL, &clk_audio.hw);
1131
	BUG_ON(IS_ERR(clk));
1132 1133
	clk_register_clkdev(clk, NULL, "b0040000.audio");
	clk = clk_register(NULL, &clk_usp0.hw);
1134
	BUG_ON(IS_ERR(clk));
1135 1136
	clk_register_clkdev(clk, NULL, "b0080000.usp");
	clk = clk_register(NULL, &clk_usp1.hw);
1137
	BUG_ON(IS_ERR(clk));
1138 1139
	clk_register_clkdev(clk, NULL, "b0090000.usp");
	clk = clk_register(NULL, &clk_usp2.hw);
1140
	BUG_ON(IS_ERR(clk));
1141 1142
	clk_register_clkdev(clk, NULL, "b00a0000.usp");
	clk = clk_register(NULL, &clk_vip.hw);
1143
	BUG_ON(IS_ERR(clk));
1144 1145
	clk_register_clkdev(clk, NULL, "b00c0000.vip");
	clk = clk_register(NULL, &clk_gfx.hw);
1146
	BUG_ON(IS_ERR(clk));
1147 1148
	clk_register_clkdev(clk, NULL, "98000000.graphics");
	clk = clk_register(NULL, &clk_mm.hw);
1149
	BUG_ON(IS_ERR(clk));
1150 1151
	clk_register_clkdev(clk, NULL, "a0000000.multimedia");
	clk = clk_register(NULL, &clk_lcd.hw);
1152
	BUG_ON(IS_ERR(clk));
1153 1154
	clk_register_clkdev(clk, NULL, "90010000.display");
	clk = clk_register(NULL, &clk_vpp.hw);
1155
	BUG_ON(IS_ERR(clk));
1156 1157
	clk_register_clkdev(clk, NULL, "90020000.vpp");
	clk = clk_register(NULL, &clk_mmc01.hw);
1158
	BUG_ON(IS_ERR(clk));
1159
	clk = clk_register(NULL, &clk_mmc23.hw);
1160
	BUG_ON(IS_ERR(clk));
1161
	clk = clk_register(NULL, &clk_mmc45.hw);
1162
	BUG_ON(IS_ERR(clk));
1163
	clk = clk_register(NULL, &usb_pll_clk_hw);
1164
	BUG_ON(IS_ERR(clk));
1165
	clk = clk_register(NULL, &clk_usb0.hw);
1166
	BUG_ON(IS_ERR(clk));
1167 1168
	clk_register_clkdev(clk, NULL, "b00e0000.usb");
	clk = clk_register(NULL, &clk_usb1.hw);
1169
	BUG_ON(IS_ERR(clk));
1170
	clk_register_clkdev(clk, NULL, "b00f0000.usb");
1171
}