clk-sunxi.c 18.2 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
/*
 * Copyright 2013 Emilio López
 *
 * Emilio López <emilio@elopez.com.ar>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include <linux/clk-provider.h>
#include <linux/clkdev.h>
#include <linux/of.h>
#include <linux/of_address.h>

#include "clk-factors.h"

static DEFINE_SPINLOCK(clk_lock);

26 27 28
/* Maximum number of parents our clocks have */
#define SUNXI_MAX_PARENTS	5

29
/**
30
 * sun4i_osc_clk_setup() - Setup function for gatable oscillator
31 32 33 34
 */

#define SUNXI_OSC24M_GATE	0

35
static void __init sun4i_osc_clk_setup(struct device_node *node)
36 37
{
	struct clk *clk;
38 39
	struct clk_fixed_rate *fixed;
	struct clk_gate *gate;
40
	const char *clk_name = node->name;
41
	u32 rate;
42

43 44 45
	if (of_property_read_u32(node, "clock-frequency", &rate))
		return;

46 47 48 49 50
	/* allocate fixed-rate and gate clock structs */
	fixed = kzalloc(sizeof(struct clk_fixed_rate), GFP_KERNEL);
	if (!fixed)
		return;
	gate = kzalloc(sizeof(struct clk_gate), GFP_KERNEL);
51 52
	if (!gate)
		goto err_free_fixed;
53 54 55 56 57 58

	/* set up gate and fixed rate properties */
	gate->reg = of_iomap(node, 0);
	gate->bit_idx = SUNXI_OSC24M_GATE;
	gate->lock = &clk_lock;
	fixed->fixed_rate = rate;
59

60 61 62 63 64 65
	clk = clk_register_composite(NULL, clk_name,
			NULL, 0,
			NULL, NULL,
			&fixed->hw, &clk_fixed_rate_ops,
			&gate->hw, &clk_gate_ops,
			CLK_IS_ROOT);
66

67 68 69 70 71 72 73 74 75 76 77 78
	if (IS_ERR(clk))
		goto err_free_gate;

	of_clk_add_provider(node, of_clk_src_simple_get, clk);
	clk_register_clkdev(clk, clk_name, NULL);

	return;

err_free_gate:
	kfree(gate);
err_free_fixed:
	kfree(fixed);
79
}
80
CLK_OF_DECLARE(sun4i_osc, "allwinner,sun4i-osc-clk", sun4i_osc_clk_setup);
81 82 83 84



/**
85
 * sun4i_get_pll1_factors() - calculates n, k, m, p factors for PLL1
86 87 88 89 90
 * PLL1 rate is calculated as follows
 * rate = (parent_rate * n * (k + 1) >> p) / (m + 1);
 * parent_rate is always 24Mhz
 */

91
static void sun4i_get_pll1_factors(u32 *freq, u32 parent_rate,
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
				   u8 *n, u8 *k, u8 *m, u8 *p)
{
	u8 div;

	/* Normalize value to a 6M multiple */
	div = *freq / 6000000;
	*freq = 6000000 * div;

	/* we were called to round the frequency, we can now return */
	if (n == NULL)
		return;

	/* m is always zero for pll1 */
	*m = 0;

	/* k is 1 only on these cases */
	if (*freq >= 768000000 || *freq == 42000000 || *freq == 54000000)
		*k = 1;
	else
		*k = 0;

	/* p will be 3 for divs under 10 */
	if (div < 10)
		*p = 3;

	/* p will be 2 for divs between 10 - 20 and odd divs under 32 */
	else if (div < 20 || (div < 32 && (div & 1)))
		*p = 2;

	/* p will be 1 for even divs under 32, divs under 40 and odd pairs
	 * of divs between 40-62 */
	else if (div < 40 || (div < 64 && (div & 2)))
		*p = 1;

	/* any other entries have p = 0 */
	else
		*p = 0;

	/* calculate a suitable n based on k and p */
	div <<= *p;
	div /= (*k + 1);
	*n = div / 4;
}

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
/**
 * sun6i_a31_get_pll1_factors() - calculates n, k and m factors for PLL1
 * PLL1 rate is calculated as follows
 * rate = parent_rate * (n + 1) * (k + 1) / (m + 1);
 * parent_rate should always be 24MHz
 */
static void sun6i_a31_get_pll1_factors(u32 *freq, u32 parent_rate,
				       u8 *n, u8 *k, u8 *m, u8 *p)
{
	/*
	 * We can operate only on MHz, this will make our life easier
	 * later.
	 */
	u32 freq_mhz = *freq / 1000000;
	u32 parent_freq_mhz = parent_rate / 1000000;

	/*
	 * Round down the frequency to the closest multiple of either
	 * 6 or 16
	 */
	u32 round_freq_6 = round_down(freq_mhz, 6);
	u32 round_freq_16 = round_down(freq_mhz, 16);

	if (round_freq_6 > round_freq_16)
		freq_mhz = round_freq_6;
	else
		freq_mhz = round_freq_16;
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
	*freq = freq_mhz * 1000000;

	/*
	 * If the factors pointer are null, we were just called to
	 * round down the frequency.
	 * Exit.
	 */
	if (n == NULL)
		return;

	/* If the frequency is a multiple of 32 MHz, k is always 3 */
	if (!(freq_mhz % 32))
		*k = 3;
	/* If the frequency is a multiple of 9 MHz, k is always 2 */
	else if (!(freq_mhz % 9))
		*k = 2;
	/* If the frequency is a multiple of 8 MHz, k is always 1 */
	else if (!(freq_mhz % 8))
		*k = 1;
	/* Otherwise, we don't use the k factor */
	else
		*k = 0;

	/*
	 * If the frequency is a multiple of 2 but not a multiple of
	 * 3, m is 3. This is the first time we use 6 here, yet we
	 * will use it on several other places.
	 * We use this number because it's the lowest frequency we can
	 * generate (with n = 0, k = 0, m = 3), so every other frequency
	 * somehow relates to this frequency.
	 */
	if ((freq_mhz % 6) == 2 || (freq_mhz % 6) == 4)
		*m = 2;
	/*
	 * If the frequency is a multiple of 6MHz, but the factor is
	 * odd, m will be 3
	 */
	else if ((freq_mhz / 6) & 1)
		*m = 3;
	/* Otherwise, we end up with m = 1 */
	else
		*m = 1;

	/* Calculate n thanks to the above factors we already got */
	*n = freq_mhz * (*m + 1) / ((*k + 1) * parent_freq_mhz) - 1;

	/*
	 * If n end up being outbound, and that we can still decrease
	 * m, do it.
	 */
	if ((*n + 1) > 31 && (*m + 1) > 1) {
		*n = (*n + 1) / 2 - 1;
		*m = (*m + 1) / 2 - 1;
	}
}
219 220

/**
221
 * sun4i_get_apb1_factors() - calculates m, p factors for APB1
222 223 224 225
 * APB1 rate is calculated as follows
 * rate = (parent_rate >> p) / (m + 1);
 */

226
static void sun4i_get_apb1_factors(u32 *freq, u32 parent_rate,
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
				   u8 *n, u8 *k, u8 *m, u8 *p)
{
	u8 calcm, calcp;

	if (parent_rate < *freq)
		*freq = parent_rate;

	parent_rate = (parent_rate + (*freq - 1)) / *freq;

	/* Invalid rate! */
	if (parent_rate > 32)
		return;

	if (parent_rate <= 4)
		calcp = 0;
	else if (parent_rate <= 8)
		calcp = 1;
	else if (parent_rate <= 16)
		calcp = 2;
	else
		calcp = 3;

	calcm = (parent_rate >> calcp) - 1;

	*freq = (parent_rate >> calcp) / (calcm + 1);

	/* we were called to round the frequency, we can now return */
	if (n == NULL)
		return;

	*m = calcm;
	*p = calcp;
}



/**
 * sunxi_factors_clk_setup() - Setup function for factor clocks
 */

267 268
#define SUNXI_FACTORS_MUX_MASK 0x3

269
struct factors_data {
270 271
	int enable;
	int mux;
272 273 274 275
	struct clk_factors_config *table;
	void (*getter) (u32 *rate, u32 parent_rate, u8 *n, u8 *k, u8 *m, u8 *p);
};

276
static struct clk_factors_config sun4i_pll1_config = {
277 278 279 280 281 282 283 284 285 286
	.nshift = 8,
	.nwidth = 5,
	.kshift = 4,
	.kwidth = 2,
	.mshift = 0,
	.mwidth = 2,
	.pshift = 16,
	.pwidth = 2,
};

287 288 289 290 291 292 293 294 295
static struct clk_factors_config sun6i_a31_pll1_config = {
	.nshift	= 8,
	.nwidth = 5,
	.kshift = 4,
	.kwidth = 2,
	.mshift = 0,
	.mwidth = 2,
};

296
static struct clk_factors_config sun4i_apb1_config = {
297 298 299 300 301 302
	.mshift = 0,
	.mwidth = 5,
	.pshift = 16,
	.pwidth = 2,
};

303
static const struct factors_data sun4i_pll1_data __initconst = {
304
	.enable = 31,
305 306
	.table = &sun4i_pll1_config,
	.getter = sun4i_get_pll1_factors,
307 308
};

309
static const struct factors_data sun6i_a31_pll1_data __initconst = {
310
	.enable = 31,
311 312 313 314
	.table = &sun6i_a31_pll1_config,
	.getter = sun6i_a31_get_pll1_factors,
};

315
static const struct factors_data sun4i_apb1_data __initconst = {
316 317
	.table = &sun4i_apb1_config,
	.getter = sun4i_get_apb1_factors,
318 319
};

320 321
static struct clk * __init sunxi_factors_clk_setup(struct device_node *node,
						const struct factors_data *data)
322 323
{
	struct clk *clk;
324 325 326 327 328
	struct clk_factors *factors;
	struct clk_gate *gate = NULL;
	struct clk_mux *mux = NULL;
	struct clk_hw *gate_hw = NULL;
	struct clk_hw *mux_hw = NULL;
329
	const char *clk_name = node->name;
330
	const char *parents[SUNXI_MAX_PARENTS];
331
	void *reg;
332
	int i = 0;
333 334 335

	reg = of_iomap(node, 0);

336 337 338 339 340 341 342
	/* if we have a mux, we will have >1 parents */
	while (i < SUNXI_MAX_PARENTS &&
	       (parents[i] = of_clk_get_parent_name(node, i)) != NULL)
		i++;

	factors = kzalloc(sizeof(struct clk_factors), GFP_KERNEL);
	if (!factors)
343
		return NULL;
344 345 346 347 348 349

	/* Add a gate if this factor clock can be gated */
	if (data->enable) {
		gate = kzalloc(sizeof(struct clk_gate), GFP_KERNEL);
		if (!gate) {
			kfree(factors);
350
			return NULL;
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
		}

		/* set up gate properties */
		gate->reg = reg;
		gate->bit_idx = data->enable;
		gate->lock = &clk_lock;
		gate_hw = &gate->hw;
	}

	/* Add a mux if this factor clock can be muxed */
	if (data->mux) {
		mux = kzalloc(sizeof(struct clk_mux), GFP_KERNEL);
		if (!mux) {
			kfree(factors);
			kfree(gate);
366
			return NULL;
367 368 369 370 371 372 373 374 375
		}

		/* set up gate properties */
		mux->reg = reg;
		mux->shift = data->mux;
		mux->mask = SUNXI_FACTORS_MUX_MASK;
		mux->lock = &clk_lock;
		mux_hw = &mux->hw;
	}
376

377 378 379 380 381 382 383 384 385 386
	/* set up factors properties */
	factors->reg = reg;
	factors->config = data->table;
	factors->get_factors = data->getter;
	factors->lock = &clk_lock;

	clk = clk_register_composite(NULL, clk_name,
			parents, i,
			mux_hw, &clk_mux_ops,
			&factors->hw, &clk_factors_ops,
387
			gate_hw, &clk_gate_ops, 0);
388

389
	if (!IS_ERR(clk)) {
390 391 392
		of_clk_add_provider(node, of_clk_src_simple_get, clk);
		clk_register_clkdev(clk, clk_name, NULL);
	}
393 394

	return clk;
395 396 397 398 399 400 401 402 403 404 405 406 407 408
}



/**
 * sunxi_mux_clk_setup() - Setup function for muxes
 */

#define SUNXI_MUX_GATE_WIDTH	2

struct mux_data {
	u8 shift;
};

409
static const struct mux_data sun4i_cpu_mux_data __initconst = {
410 411 412
	.shift = 16,
};

413
static const struct mux_data sun6i_a31_ahb1_mux_data __initconst = {
414 415 416
	.shift = 12,
};

417
static const struct mux_data sun4i_apb1_mux_data __initconst = {
418 419 420 421 422 423 424 425
	.shift = 24,
};

static void __init sunxi_mux_clk_setup(struct device_node *node,
				       struct mux_data *data)
{
	struct clk *clk;
	const char *clk_name = node->name;
426
	const char *parents[SUNXI_MAX_PARENTS];
427 428 429 430 431
	void *reg;
	int i = 0;

	reg = of_iomap(node, 0);

432 433
	while (i < SUNXI_MAX_PARENTS &&
	       (parents[i] = of_clk_get_parent_name(node, i)) != NULL)
434 435
		i++;

436 437
	clk = clk_register_mux(NULL, clk_name, parents, i,
			       CLK_SET_RATE_NO_REPARENT, reg,
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
			       data->shift, SUNXI_MUX_GATE_WIDTH,
			       0, &clk_lock);

	if (clk) {
		of_clk_add_provider(node, of_clk_src_simple_get, clk);
		clk_register_clkdev(clk, clk_name, NULL);
	}
}



/**
 * sunxi_divider_clk_setup() - Setup function for simple divider clocks
 */

struct div_data {
454 455 456
	u8	shift;
	u8	pow;
	u8	width;
457 458
};

459
static const struct div_data sun4i_axi_data __initconst = {
460 461 462
	.shift	= 0,
	.pow	= 0,
	.width	= 2,
463 464
};

465
static const struct div_data sun4i_ahb_data __initconst = {
466 467 468
	.shift	= 4,
	.pow	= 1,
	.width	= 2,
469 470
};

471
static const struct div_data sun4i_apb0_data __initconst = {
472 473 474
	.shift	= 8,
	.pow	= 1,
	.width	= 2,
475 476
};

477
static const struct div_data sun6i_a31_apb2_div_data __initconst = {
478 479 480 481 482
	.shift	= 0,
	.pow	= 0,
	.width	= 4,
};

483 484 485 486 487 488 489 490 491 492 493 494 495
static void __init sunxi_divider_clk_setup(struct device_node *node,
					   struct div_data *data)
{
	struct clk *clk;
	const char *clk_name = node->name;
	const char *clk_parent;
	void *reg;

	reg = of_iomap(node, 0);

	clk_parent = of_clk_get_parent_name(node, 0);

	clk = clk_register_divider(NULL, clk_name, clk_parent, 0,
496
				   reg, data->shift, data->width,
497 498 499 500 501 502 503 504 505
				   data->pow ? CLK_DIVIDER_POWER_OF_TWO : 0,
				   &clk_lock);
	if (clk) {
		of_clk_add_provider(node, of_clk_src_simple_get, clk);
		clk_register_clkdev(clk, clk_name, NULL);
	}
}


506 507 508 509 510 511 512 513 514 515 516

/**
 * sunxi_gates_clk_setup() - Setup function for leaf gates on clocks
 */

#define SUNXI_GATES_MAX_SIZE	64

struct gates_data {
	DECLARE_BITMAP(mask, SUNXI_GATES_MAX_SIZE);
};

517
static const struct gates_data sun4i_axi_gates_data __initconst = {
518 519 520
	.mask = {1},
};

521
static const struct gates_data sun4i_ahb_gates_data __initconst = {
522 523 524
	.mask = {0x7F77FFF, 0x14FB3F},
};

525
static const struct gates_data sun5i_a10s_ahb_gates_data __initconst = {
M
Maxime Ripard 已提交
526 527 528
	.mask = {0x147667e7, 0x185915},
};

529
static const struct gates_data sun5i_a13_ahb_gates_data __initconst = {
530 531 532
	.mask = {0x107067e7, 0x185111},
};

533
static const struct gates_data sun6i_a31_ahb1_gates_data __initconst = {
534 535 536
	.mask = {0xEDFE7F62, 0x794F931},
};

537
static const struct gates_data sun7i_a20_ahb_gates_data __initconst = {
538 539 540
	.mask = { 0x12f77fff, 0x16ff3f },
};

541
static const struct gates_data sun4i_apb0_gates_data __initconst = {
542 543 544
	.mask = {0x4EF},
};

545
static const struct gates_data sun5i_a10s_apb0_gates_data __initconst = {
M
Maxime Ripard 已提交
546 547 548
	.mask = {0x469},
};

549
static const struct gates_data sun5i_a13_apb0_gates_data __initconst = {
550 551 552
	.mask = {0x61},
};

553
static const struct gates_data sun7i_a20_apb0_gates_data __initconst = {
554 555 556
	.mask = { 0x4ff },
};

557
static const struct gates_data sun4i_apb1_gates_data __initconst = {
558 559 560
	.mask = {0xFF00F7},
};

561
static const struct gates_data sun5i_a10s_apb1_gates_data __initconst = {
M
Maxime Ripard 已提交
562 563 564
	.mask = {0xf0007},
};

565
static const struct gates_data sun5i_a13_apb1_gates_data __initconst = {
566 567 568
	.mask = {0xa0007},
};

569
static const struct gates_data sun6i_a31_apb1_gates_data __initconst = {
570 571 572
	.mask = {0x3031},
};

573
static const struct gates_data sun6i_a31_apb2_gates_data __initconst = {
574 575 576
	.mask = {0x3F000F},
};

577
static const struct gates_data sun7i_a20_apb1_gates_data __initconst = {
578 579 580
	.mask = { 0xff80ff },
};

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
static void __init sunxi_gates_clk_setup(struct device_node *node,
					 struct gates_data *data)
{
	struct clk_onecell_data *clk_data;
	const char *clk_parent;
	const char *clk_name;
	void *reg;
	int qty;
	int i = 0;
	int j = 0;
	int ignore;

	reg = of_iomap(node, 0);

	clk_parent = of_clk_get_parent_name(node, 0);

	/* Worst-case size approximation and memory allocation */
	qty = find_last_bit(data->mask, SUNXI_GATES_MAX_SIZE);
	clk_data = kmalloc(sizeof(struct clk_onecell_data), GFP_KERNEL);
	if (!clk_data)
		return;
	clk_data->clks = kzalloc((qty+1) * sizeof(struct clk *), GFP_KERNEL);
	if (!clk_data->clks) {
		kfree(clk_data);
		return;
	}

	for_each_set_bit(i, data->mask, SUNXI_GATES_MAX_SIZE) {
		of_property_read_string_index(node, "clock-output-names",
					      j, &clk_name);

		/* No driver claims this clock, but it should remain gated */
		ignore = !strcmp("ahb_sdram", clk_name) ? CLK_IGNORE_UNUSED : 0;

		clk_data->clks[i] = clk_register_gate(NULL, clk_name,
						      clk_parent, ignore,
						      reg + 4 * (i/32), i % 32,
						      0, &clk_lock);
		WARN_ON(IS_ERR(clk_data->clks[i]));

		j++;
	}

	/* Adjust to the real max */
	clk_data->clk_num = i;

	of_clk_add_provider(node, of_clk_src_onecell_get, clk_data);
}

630
/* Matches for factors clocks */
631
static const struct of_device_id clk_factors_match[] __initconst = {
632
	{.compatible = "allwinner,sun4i-pll1-clk", .data = &sun4i_pll1_data,},
633
	{.compatible = "allwinner,sun6i-a31-pll1-clk", .data = &sun6i_a31_pll1_data,},
634
	{.compatible = "allwinner,sun4i-apb1-clk", .data = &sun4i_apb1_data,},
635 636 637 638
	{}
};

/* Matches for divider clocks */
639
static const struct of_device_id clk_div_match[] __initconst = {
640 641 642
	{.compatible = "allwinner,sun4i-axi-clk", .data = &sun4i_axi_data,},
	{.compatible = "allwinner,sun4i-ahb-clk", .data = &sun4i_ahb_data,},
	{.compatible = "allwinner,sun4i-apb0-clk", .data = &sun4i_apb0_data,},
643
	{.compatible = "allwinner,sun6i-a31-apb2-div-clk", .data = &sun6i_a31_apb2_div_data,},
644 645 646 647
	{}
};

/* Matches for mux clocks */
648
static const struct of_device_id clk_mux_match[] __initconst = {
649 650
	{.compatible = "allwinner,sun4i-cpu-clk", .data = &sun4i_cpu_mux_data,},
	{.compatible = "allwinner,sun4i-apb1-mux-clk", .data = &sun4i_apb1_mux_data,},
651
	{.compatible = "allwinner,sun6i-a31-ahb1-mux-clk", .data = &sun6i_a31_ahb1_mux_data,},
652 653 654
	{}
};

655
/* Matches for gate clocks */
656
static const struct of_device_id clk_gates_match[] __initconst = {
657 658
	{.compatible = "allwinner,sun4i-axi-gates-clk", .data = &sun4i_axi_gates_data,},
	{.compatible = "allwinner,sun4i-ahb-gates-clk", .data = &sun4i_ahb_gates_data,},
M
Maxime Ripard 已提交
659
	{.compatible = "allwinner,sun5i-a10s-ahb-gates-clk", .data = &sun5i_a10s_ahb_gates_data,},
660
	{.compatible = "allwinner,sun5i-a13-ahb-gates-clk", .data = &sun5i_a13_ahb_gates_data,},
661
	{.compatible = "allwinner,sun6i-a31-ahb1-gates-clk", .data = &sun6i_a31_ahb1_gates_data,},
662
	{.compatible = "allwinner,sun7i-a20-ahb-gates-clk", .data = &sun7i_a20_ahb_gates_data,},
663
	{.compatible = "allwinner,sun4i-apb0-gates-clk", .data = &sun4i_apb0_gates_data,},
M
Maxime Ripard 已提交
664
	{.compatible = "allwinner,sun5i-a10s-apb0-gates-clk", .data = &sun5i_a10s_apb0_gates_data,},
665
	{.compatible = "allwinner,sun5i-a13-apb0-gates-clk", .data = &sun5i_a13_apb0_gates_data,},
666
	{.compatible = "allwinner,sun7i-a20-apb0-gates-clk", .data = &sun7i_a20_apb0_gates_data,},
667
	{.compatible = "allwinner,sun4i-apb1-gates-clk", .data = &sun4i_apb1_gates_data,},
M
Maxime Ripard 已提交
668
	{.compatible = "allwinner,sun5i-a10s-apb1-gates-clk", .data = &sun5i_a10s_apb1_gates_data,},
669
	{.compatible = "allwinner,sun5i-a13-apb1-gates-clk", .data = &sun5i_a13_apb1_gates_data,},
670
	{.compatible = "allwinner,sun6i-a31-apb1-gates-clk", .data = &sun6i_a31_apb1_gates_data,},
671
	{.compatible = "allwinner,sun7i-a20-apb1-gates-clk", .data = &sun7i_a20_apb1_gates_data,},
672
	{.compatible = "allwinner,sun6i-a31-apb2-gates-clk", .data = &sun6i_a31_apb2_gates_data,},
673 674 675
	{}
};

676 677 678 679 680 681 682 683 684 685 686 687 688 689 690
static void __init of_sunxi_table_clock_setup(const struct of_device_id *clk_match,
					      void *function)
{
	struct device_node *np;
	const struct div_data *data;
	const struct of_device_id *match;
	void (*setup_function)(struct device_node *, const void *) = function;

	for_each_matching_node(np, clk_match) {
		match = of_match_node(clk_match, np);
		data = match->data;
		setup_function(np, data);
	}
}

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
/**
 * System clock protection
 *
 * By enabling these critical clocks, we prevent their accidental gating
 * by the framework
 */
static void __init sunxi_clock_protect(void)
{
	struct clk *clk;

	/* memory bus clock - sun5i+ */
	clk = clk_get(NULL, "mbus");
	if (!IS_ERR(clk)) {
		clk_prepare_enable(clk);
		clk_put(clk);
	}

	/* DDR clock - sun4i+ */
	clk = clk_get(NULL, "pll5_ddr");
	if (!IS_ERR(clk)) {
		clk_prepare_enable(clk);
		clk_put(clk);
	}
}

716
static void __init sunxi_init_clocks(void)
717 718 719 720 721 722 723 724 725
{
	/* Register factor clocks */
	of_sunxi_table_clock_setup(clk_factors_match, sunxi_factors_clk_setup);

	/* Register divider clocks */
	of_sunxi_table_clock_setup(clk_div_match, sunxi_divider_clk_setup);

	/* Register mux clocks */
	of_sunxi_table_clock_setup(clk_mux_match, sunxi_mux_clk_setup);
726 727 728

	/* Register gate clocks */
	of_sunxi_table_clock_setup(clk_gates_match, sunxi_gates_clk_setup);
729 730 731

	/* Enable core system clocks */
	sunxi_clock_protect();
732
}
733 734 735 736 737
CLK_OF_DECLARE(sun4i_a10_clk_init, "allwinner,sun4i-a10", sunxi_init_clocks);
CLK_OF_DECLARE(sun5i_a10s_clk_init, "allwinner,sun5i-a10s", sunxi_init_clocks);
CLK_OF_DECLARE(sun5i_a13_clk_init, "allwinner,sun5i-a13", sunxi_init_clocks);
CLK_OF_DECLARE(sun6i_a31_clk_init, "allwinner,sun6i-a31", sunxi_init_clocks);
CLK_OF_DECLARE(sun7i_a20_clk_init, "allwinner,sun7i-a20", sunxi_init_clocks);