blob: e9a80c6f12fc2bf8748bc1d69a98608aea2bf08e [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Wills Wang6b83b442016-03-16 16:59:54 +08002/*
3 * Copyright (C) 2015-2016 Wills Wang <wills.wang@live.com>
Wills Wang6b83b442016-03-16 16:59:54 +08004 */
5
Simon Glass85d65312019-12-28 10:44:58 -07006#include <clock_legacy.h>
Simon Glass3ba929a2020-10-30 21:38:53 -06007#include <asm/global_data.h>
Wills Wang6b83b442016-03-16 16:59:54 +08008#include <asm/io.h>
9#include <asm/addrspace.h>
10#include <asm/types.h>
11#include <mach/ar71xx_regs.h>
Wills Wangddc05522016-05-30 22:54:50 +080012#include <mach/ath79.h>
Wills Wang6b83b442016-03-16 16:59:54 +080013
14DECLARE_GLOBAL_DATA_PTR;
15
16static u32 qca953x_get_xtal(void)
17{
18 u32 val;
19
Wills Wangddc05522016-05-30 22:54:50 +080020 val = ath79_get_bootstrap();
Wills Wang6b83b442016-03-16 16:59:54 +080021 if (val & QCA953X_BOOTSTRAP_REF_CLK_40)
22 return 40000000;
23 else
24 return 25000000;
25}
26
27int get_serial_clock(void)
28{
29 return qca953x_get_xtal();
30}
31
32int get_clocks(void)
33{
34 void __iomem *regs;
35 u32 val, ctrl, xtal, pll, div;
36
37 regs = map_physmem(AR71XX_PLL_BASE, AR71XX_PLL_SIZE,
38 MAP_NOCACHE);
39
40 xtal = qca953x_get_xtal();
41 ctrl = readl(regs + QCA953X_PLL_CLK_CTRL_REG);
42 val = readl(regs + QCA953X_PLL_CPU_CONFIG_REG);
43
44 /* VCOOUT = XTAL * DIV_INT */
45 div = (val >> QCA953X_PLL_CPU_CONFIG_REFDIV_SHIFT)
46 & QCA953X_PLL_CPU_CONFIG_REFDIV_MASK;
47 pll = xtal / div;
48
49 /* PLLOUT = VCOOUT * (1/2^OUTDIV) */
50 div = (val >> QCA953X_PLL_CPU_CONFIG_NINT_SHIFT)
51 & QCA953X_PLL_CPU_CONFIG_NINT_MASK;
52 pll *= div;
53 div = (val >> QCA953X_PLL_CPU_CONFIG_OUTDIV_SHIFT)
54 & QCA953X_PLL_CPU_CONFIG_OUTDIV_MASK;
55 if (!div)
56 div = 1;
57 pll >>= div;
58
59 /* CPU_CLK = PLLOUT / CPU_POST_DIV */
60 div = ((ctrl >> QCA953X_PLL_CLK_CTRL_CPU_POST_DIV_SHIFT)
61 & QCA953X_PLL_CLK_CTRL_CPU_POST_DIV_MASK) + 1;
62 gd->cpu_clk = pll / div;
63
Wills Wang6b83b442016-03-16 16:59:54 +080064 val = readl(regs + QCA953X_PLL_DDR_CONFIG_REG);
65 /* VCOOUT = XTAL * DIV_INT */
66 div = (val >> QCA953X_PLL_DDR_CONFIG_REFDIV_SHIFT)
67 & QCA953X_PLL_DDR_CONFIG_REFDIV_MASK;
68 pll = xtal / div;
69
70 /* PLLOUT = VCOOUT * (1/2^OUTDIV) */
71 div = (val >> QCA953X_PLL_DDR_CONFIG_NINT_SHIFT)
72 & QCA953X_PLL_DDR_CONFIG_NINT_MASK;
73 pll *= div;
74 div = (val >> QCA953X_PLL_DDR_CONFIG_OUTDIV_SHIFT)
75 & QCA953X_PLL_DDR_CONFIG_OUTDIV_MASK;
76 if (!div)
77 div = 1;
78 pll >>= div;
79
80 /* DDR_CLK = PLLOUT / DDR_POST_DIV */
81 div = ((ctrl >> QCA953X_PLL_CLK_CTRL_DDR_POST_DIV_SHIFT)
82 & QCA953X_PLL_CLK_CTRL_DDR_POST_DIV_MASK) + 1;
83 gd->mem_clk = pll / div;
84
85 div = ((ctrl >> QCA953X_PLL_CLK_CTRL_AHB_POST_DIV_SHIFT)
86 & QCA953X_PLL_CLK_CTRL_AHB_POST_DIV_MASK) + 1;
87 if (ctrl & QCA953X_PLL_CLK_CTRL_AHBCLK_FROM_DDRPLL) {
88 /* AHB_CLK = DDR_CLK / AHB_POST_DIV */
89 gd->bus_clk = gd->mem_clk / (div + 1);
90 } else {
91 /* AHB_CLK = CPU_CLK / AHB_POST_DIV */
92 gd->bus_clk = gd->cpu_clk / (div + 1);
93 }
94
95 return 0;
96}
97
98ulong get_bus_freq(ulong dummy)
99{
100 if (!gd->bus_clk)
101 get_clocks();
102 return gd->bus_clk;
103}
104
105ulong get_ddr_freq(ulong dummy)
106{
107 if (!gd->mem_clk)
108 get_clocks();
109 return gd->mem_clk;
110}