Skip to content

Commit e6046b5

Browse files
981213paulburton
authored andcommitted
MIPS: ralink: fix cpu clock of mt7621 and add dt clk devices
For a long time the mt7621 uses a fixed cpu clock which causes a problem if the cpu frequency is not 880MHz. This patch fixes the cpu clock calculation and adds the cpu/bus clkdev which will be used in dts. Ported from OpenWrt: c7ca224299 ramips: fix cpu clock of mt7621 and add dt clk devices Signed-off-by: Weijie Gao <hackpascal@gmail.com> Signed-off-by: Chuanhong Guo <gch981213@gmail.com> Signed-off-by: Paul Burton <paul.burton@mips.com> Cc: linux-mips@vger.kernel.org Cc: Ralf Baechle <ralf@linux-mips.org> Cc: James Hogan <jhogan@kernel.org> Cc: John Crispin <john@phrozen.org> Cc: linux-kernel@vger.kernel.org
1 parent e6331a3 commit e6046b5

3 files changed

Lines changed: 93 additions & 33 deletions

File tree

‎arch/mips/include/asm/mach-ralink/mt7621.h‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,33 @@
1919
#define SYSC_REG_CHIP_REV 0x0c
2020
#define SYSC_REG_SYSTEM_CONFIG0 0x10
2121
#define SYSC_REG_SYSTEM_CONFIG1 0x14
22+
#define SYSC_REG_CLKCFG0 0x2c
23+
#define SYSC_REG_CUR_CLK_STS 0x44
24+
25+
#define MEMC_REG_CPU_PLL 0x648
2226

2327
#define CHIP_REV_PKG_MASK 0x1
2428
#define CHIP_REV_PKG_SHIFT 16
2529
#define CHIP_REV_VER_MASK 0xf
2630
#define CHIP_REV_VER_SHIFT 8
2731
#define CHIP_REV_ECO_MASK 0xf
2832

33+
#define XTAL_MODE_SEL_MASK 0x7
34+
#define XTAL_MODE_SEL_SHIFT 6
35+
36+
#define CPU_CLK_SEL_MASK 0x3
37+
#define CPU_CLK_SEL_SHIFT 30
38+
39+
#define CUR_CPU_FDIV_MASK 0x1f
40+
#define CUR_CPU_FDIV_SHIFT 8
41+
#define CUR_CPU_FFRAC_MASK 0x1f
42+
#define CUR_CPU_FFRAC_SHIFT 0
43+
44+
#define CPU_PLL_PREDIV_MASK 0x3
45+
#define CPU_PLL_PREDIV_SHIFT 12
46+
#define CPU_PLL_FBDIV_MASK 0x7f
47+
#define CPU_PLL_FBDIV_SHIFT 4
48+
2949
#define MT7621_DRAM_BASE 0x0
3050
#define MT7621_DDR2_SIZE_MIN 32
3151
#define MT7621_DDR2_SIZE_MAX 256

‎arch/mips/ralink/mt7621.c‎

Lines changed: 71 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,22 @@
99

1010
#include <linux/kernel.h>
1111
#include <linux/init.h>
12+
#include <linux/clk.h>
13+
#include <linux/clkdev.h>
14+
#include <linux/clk-provider.h>
15+
#include <dt-bindings/clock/mt7621-clk.h>
1216

1317
#include <asm/mipsregs.h>
1418
#include <asm/smp-ops.h>
1519
#include <asm/mips-cps.h>
1620
#include <asm/mach-ralink/ralink_regs.h>
1721
#include <asm/mach-ralink/mt7621.h>
22+
#include <asm/time.h>
1823

1924
#include <pinmux.h>
2025

2126
#include "common.h"
2227

23-
#define SYSC_REG_SYSCFG 0x10
24-
#define SYSC_REG_CPLL_CLKCFG0 0x2c
25-
#define SYSC_REG_CUR_CLK_STS 0x44
26-
#define CPU_CLK_SEL (BIT(30) | BIT(31))
27-
2828
#define MT7621_GPIO_MODE_UART1 1
2929
#define MT7621_GPIO_MODE_I2C 2
3030
#define MT7621_GPIO_MODE_UART3_MASK 0x3
@@ -110,49 +110,89 @@ static struct rt2880_pmx_group mt7621_pinmux_data[] = {
110110
{ 0 }
111111
};
112112

113+
static struct clk *clks[MT7621_CLK_MAX];
114+
static struct clk_onecell_data clk_data = {
115+
.clks = clks,
116+
.clk_num = ARRAY_SIZE(clks),
117+
};
118+
113119
phys_addr_t mips_cpc_default_phys_base(void)
114120
{
115121
panic("Cannot detect cpc address");
116122
}
117123

124+
static struct clk *__init mt7621_add_sys_clkdev(
125+
const char *id, unsigned long rate)
126+
{
127+
struct clk *clk;
128+
int err;
129+
130+
clk = clk_register_fixed_rate(NULL, id, NULL, 0, rate);
131+
if (IS_ERR(clk))
132+
panic("failed to allocate %s clock structure", id);
133+
134+
err = clk_register_clkdev(clk, id, NULL);
135+
if (err)
136+
panic("unable to register %s clock device", id);
137+
138+
return clk;
139+
}
140+
118141
void __init ralink_clk_init(void)
119142
{
120-
int cpu_fdiv = 0;
121-
int cpu_ffrac = 0;
122-
int fbdiv = 0;
123-
u32 clk_sts, syscfg;
124-
u8 clk_sel = 0, xtal_mode;
125-
u32 cpu_clk;
143+
const static u32 prediv_tbl[] = {0, 1, 2, 2};
144+
u32 syscfg, xtal_sel, clkcfg, clk_sel, curclk, ffiv, ffrac;
145+
u32 pll, prediv, fbdiv;
146+
u32 xtal_clk, cpu_clk, bus_clk;
147+
148+
syscfg = rt_sysc_r32(SYSC_REG_SYSTEM_CONFIG0);
149+
xtal_sel = (syscfg >> XTAL_MODE_SEL_SHIFT) & XTAL_MODE_SEL_MASK;
126150

127-
if ((rt_sysc_r32(SYSC_REG_CPLL_CLKCFG0) & CPU_CLK_SEL) != 0)
128-
clk_sel = 1;
151+
clkcfg = rt_sysc_r32(SYSC_REG_CLKCFG0);
152+
clk_sel = (clkcfg >> CPU_CLK_SEL_SHIFT) & CPU_CLK_SEL_MASK;
153+
154+
curclk = rt_sysc_r32(SYSC_REG_CUR_CLK_STS);
155+
ffiv = (curclk >> CUR_CPU_FDIV_SHIFT) & CUR_CPU_FDIV_MASK;
156+
ffrac = (curclk >> CUR_CPU_FFRAC_SHIFT) & CUR_CPU_FFRAC_MASK;
157+
158+
if (xtal_sel <= 2)
159+
xtal_clk = 20 * 1000 * 1000;
160+
else if (xtal_sel <= 5)
161+
xtal_clk = 40 * 1000 * 1000;
162+
else
163+
xtal_clk = 25 * 1000 * 1000;
129164

130165
switch (clk_sel) {
131166
case 0:
132-
clk_sts = rt_sysc_r32(SYSC_REG_CUR_CLK_STS);
133-
cpu_fdiv = ((clk_sts >> 8) & 0x1F);
134-
cpu_ffrac = (clk_sts & 0x1F);
135-
cpu_clk = (500 * cpu_ffrac / cpu_fdiv) * 1000 * 1000;
167+
cpu_clk = 500 * 1000 * 1000;
136168
break;
137-
138169
case 1:
139-
fbdiv = ((rt_sysc_r32(0x648) >> 4) & 0x7F) + 1;
140-
syscfg = rt_sysc_r32(SYSC_REG_SYSCFG);
141-
xtal_mode = (syscfg >> 6) & 0x7;
142-
if (xtal_mode >= 6) {
143-
/* 25Mhz Xtal */
144-
cpu_clk = 25 * fbdiv * 1000 * 1000;
145-
} else if (xtal_mode >= 3) {
146-
/* 40Mhz Xtal */
147-
cpu_clk = 40 * fbdiv * 1000 * 1000;
148-
} else {
149-
/* 20Mhz Xtal */
150-
cpu_clk = 20 * fbdiv * 1000 * 1000;
151-
}
170+
pll = rt_memc_r32(MEMC_REG_CPU_PLL);
171+
fbdiv = (pll >> CPU_PLL_FBDIV_SHIFT) & CPU_PLL_FBDIV_MASK;
172+
prediv = (pll >> CPU_PLL_PREDIV_SHIFT) & CPU_PLL_PREDIV_MASK;
173+
cpu_clk = ((fbdiv + 1) * xtal_clk) >> prediv_tbl[prediv];
152174
break;
175+
default:
176+
cpu_clk = xtal_clk;
153177
}
178+
179+
cpu_clk = cpu_clk / ffiv * ffrac;
180+
bus_clk = cpu_clk / 4;
181+
182+
clks[MT7621_CLK_CPU] = mt7621_add_sys_clkdev("cpu", cpu_clk);
183+
clks[MT7621_CLK_BUS] = mt7621_add_sys_clkdev("bus", bus_clk);
184+
185+
pr_info("CPU Clock: %dMHz\n", cpu_clk / 1000000);
186+
mips_hpt_frequency = cpu_clk / 2;
154187
}
155188

189+
static void __init mt7621_clocks_init_dt(struct device_node *np)
190+
{
191+
of_clk_add_provider(np, of_clk_src_onecell_get, &clk_data);
192+
}
193+
194+
CLK_OF_DECLARE(mt7621_clk, "mediatek,mt7621-pll", mt7621_clocks_init_dt);
195+
156196
void __init ralink_of_remap(void)
157197
{
158198
rt_sysc_membase = plat_of_remap_node("mtk,mt7621-sysc");

‎arch/mips/ralink/timer-gic.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@
1111

1212
#include <linux/of.h>
1313
#include <linux/clk-provider.h>
14-
#include <linux/clocksource.h>
14+
#include <asm/time.h>
1515

1616
#include "common.h"
1717

1818
void __init plat_time_init(void)
1919
{
2020
ralink_of_remap();
21-
21+
ralink_clk_init();
2222
of_clk_init(NULL);
2323
timer_probe();
2424
}

0 commit comments

Comments
 (0)