~funderscore blog cgit wiki get in touch
aboutsummaryrefslogtreecommitdiff
blob: 68d48939d7d23df64d00820309ca7ec9ce934ea6 (plain)
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
// SPDX-License-Identifier: GPL-2.0+
/*
 * Copyright (C) 2015-2016 Wills Wang <wills.wang@live.com>
 */

#include <common.h>
#include <clock_legacy.h>
#include <asm/global_data.h>
#include <asm/io.h>
#include <asm/addrspace.h>
#include <asm/types.h>
#include <mach/ar71xx_regs.h>
#include <mach/ath79.h>

DECLARE_GLOBAL_DATA_PTR;

static u32 ar933x_get_xtal(void)
{
	u32 val;

	val = ath79_get_bootstrap();
	if (val & AR933X_BOOTSTRAP_REF_CLK_40)
		return 40000000;
	else
		return 25000000;
}

int get_serial_clock(void)
{
	return ar933x_get_xtal();
}

int get_clocks(void)
{
	void __iomem *regs;
	u32 val, xtal, pll, div;

	regs = map_physmem(AR71XX_PLL_BASE, AR71XX_PLL_SIZE,
			   MAP_NOCACHE);
	xtal = ar933x_get_xtal();
	val = readl(regs + AR933X_PLL_CPU_CONFIG_REG);

	/* VCOOUT = XTAL * DIV_INT */
	div = (val >> AR933X_PLL_CPU_CONFIG_REFDIV_SHIFT)
			& AR933X_PLL_CPU_CONFIG_REFDIV_MASK;
	pll = xtal / div;

	/* PLLOUT = VCOOUT * (1/2^OUTDIV) */
	div = (val >> AR933X_PLL_CPU_CONFIG_NINT_SHIFT)
			& AR933X_PLL_CPU_CONFIG_NINT_MASK;
	pll *= div;
	div = (val >> AR933X_PLL_CPU_CONFIG_OUTDIV_SHIFT)
			& AR933X_PLL_CPU_CONFIG_OUTDIV_MASK;
	if (!div)
		div = 1;
	pll >>= div;

	val = readl(regs + AR933X_PLL_CLK_CTRL_REG);

	/* CPU_CLK = PLLOUT / CPU_POST_DIV */
	div = ((val >> AR933X_PLL_CLK_CTRL_CPU_POST_DIV_SHIFT)
			& AR933X_PLL_CLK_CTRL_CPU_POST_DIV_MASK) + 1;
	gd->cpu_clk = pll / div;

	/* DDR_CLK = PLLOUT / DDR_POST_DIV */
	div = ((val >> AR933X_PLL_CLK_CTRL_DDR_POST_DIV_SHIFT)
			& AR933X_PLL_CLK_CTRL_DDR_POST_DIV_MASK) + 1;
	gd->mem_clk = pll / div;

	/* AHB_CLK = PLLOUT / AHB_POST_DIV */
	div = ((val >> AR933X_PLL_CLK_CTRL_AHB_POST_DIV_SHIFT)
			& AR933X_PLL_CLK_CTRL_AHB_POST_DIV_MASK) + 1;
	gd->bus_clk = pll / div;

	return 0;
}

ulong get_bus_freq(ulong dummy)
{
	if (!gd->bus_clk)
		get_clocks();
	return gd->bus_clk;
}

ulong get_ddr_freq(ulong dummy)
{
	if (!gd->mem_clk)
		get_clocks();
	return gd->mem_clk;
}