2 * This program is free software; you can redistribute it and/or modify it
3 * under the terms of the GNU General Public License version 2 as published
4 * by the Free Software Foundation.
6 * Parts of this file are based on Ralink's 2.6.21 BSP
8 * Copyright (C) 2008-2011 Gabor Juhos <juhosg@openwrt.org>
9 * Copyright (C) 2008 Imre Kaloz <kaloz@openwrt.org>
10 * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
13 #include <linux/kernel.h>
14 #include <linux/init.h>
15 #include <linux/module.h>
17 #include <asm/mipsregs.h>
18 #include <asm/mach-ralink/ralink_regs.h>
19 #include <asm/mach-ralink/mt7620.h>
23 /* does the board have sdram or ddram */
26 static struct ralink_pinmux_grp mode_mux
[] = {
29 .mask
= MT7620_GPIO_MODE_I2C
,
34 .mask
= MT7620_GPIO_MODE_SPI
,
39 .mask
= MT7620_GPIO_MODE_UART1
,
44 .mask
= MT7620_GPIO_MODE_WDT
,
49 .mask
= MT7620_GPIO_MODE_MDIO
,
54 .mask
= MT7620_GPIO_MODE_RGMII1
,
59 .mask
= MT7620_GPIO_MODE_SPI_REF_CLK
,
64 .mask
= MT7620_GPIO_MODE_JTAG
,
68 /* shared lines with jtag */
70 .mask
= MT7620_GPIO_MODE_EPHY
,
75 .mask
= MT7620_GPIO_MODE_JTAG
,
80 .mask
= MT7620_GPIO_MODE_RGMII2
,
85 .mask
= MT7620_GPIO_MODE_WLED
,
91 static struct ralink_pinmux_grp uart_mux
[] = {
94 .mask
= MT7620_GPIO_MODE_UARTF
,
99 .mask
= MT7620_GPIO_MODE_PCM_UARTF
,
104 .mask
= MT7620_GPIO_MODE_PCM_I2S
,
109 .mask
= MT7620_GPIO_MODE_I2S_UARTF
,
114 .mask
= MT7620_GPIO_MODE_PCM_GPIO
,
118 .name
= "gpio uartf",
119 .mask
= MT7620_GPIO_MODE_GPIO_UARTF
,
124 .mask
= MT7620_GPIO_MODE_GPIO_I2S
,
129 .mask
= MT7620_GPIO_MODE_GPIO
,
133 struct ralink_pinmux rt_gpio_pinmux
= {
136 .uart_shift
= MT7620_GPIO_MODE_UART0_SHIFT
,
137 .uart_mask
= MT7620_GPIO_MODE_UART0_MASK
,
141 mt7620_calc_rate(u32 ref_rate
, u32 mul
, u32 div
)
152 #define MHZ(x) ((x) * 1000 * 1000)
154 static __init
unsigned long
155 mt7620_get_xtal_rate(void)
159 reg
= rt_sysc_r32(SYSC_REG_SYSTEM_CONFIG0
);
160 if (reg
& SYSCFG0_XTAL_FREQ_SEL
)
166 static __init
unsigned long
167 mt7620_get_periph_rate(unsigned long xtal_rate
)
171 reg
= rt_sysc_r32(SYSC_REG_CLKCFG0
);
172 if (reg
& CLKCFG0_PERI_CLK_SEL
)
178 static const u32 mt7620_clk_divider
[] __initconst
= { 2, 3, 4, 8 };
180 static __init
unsigned long
181 mt7620_get_cpu_pll_rate(unsigned long xtal_rate
)
187 reg
= rt_sysc_r32(SYSC_REG_CPLL_CONFIG0
);
188 if (reg
& CPLL_CFG0_BYPASS_REF_CLK
)
191 if ((reg
& CPLL_CFG0_SW_CFG
) == 0)
194 mul
= (reg
>> CPLL_CFG0_PLL_MULT_RATIO_SHIFT
) &
195 CPLL_CFG0_PLL_MULT_RATIO_MASK
;
197 if (reg
& CPLL_CFG0_LC_CURFCK
)
200 div
= (reg
>> CPLL_CFG0_PLL_DIV_RATIO_SHIFT
) &
201 CPLL_CFG0_PLL_DIV_RATIO_MASK
;
203 WARN_ON(div
>= ARRAY_SIZE(mt7620_clk_divider
));
205 return mt7620_calc_rate(xtal_rate
, mul
, mt7620_clk_divider
[div
]);
208 static __init
unsigned long
209 mt7620_get_pll_rate(unsigned long xtal_rate
, unsigned long cpu_pll_rate
)
213 reg
= rt_sysc_r32(SYSC_REG_CPLL_CONFIG1
);
214 if (reg
& CPLL_CFG1_CPU_AUX1
)
217 if (reg
& CPLL_CFG1_CPU_AUX0
)
223 static __init
unsigned long
224 mt7620_get_cpu_rate(unsigned long pll_rate
)
230 reg
= rt_sysc_r32(SYSC_REG_CPU_SYS_CLKCFG
);
232 mul
= reg
& CPU_SYS_CLKCFG_CPU_FFRAC_MASK
;
233 div
= (reg
>> CPU_SYS_CLKCFG_CPU_FDIV_SHIFT
) &
234 CPU_SYS_CLKCFG_CPU_FDIV_MASK
;
236 return mt7620_calc_rate(pll_rate
, mul
, div
);
239 static const u32 mt7620_ocp_dividers
[16] __initconst
= {
240 [CPU_SYS_CLKCFG_OCP_RATIO_2
] = 2,
241 [CPU_SYS_CLKCFG_OCP_RATIO_3
] = 3,
242 [CPU_SYS_CLKCFG_OCP_RATIO_4
] = 4,
243 [CPU_SYS_CLKCFG_OCP_RATIO_5
] = 5,
244 [CPU_SYS_CLKCFG_OCP_RATIO_10
] = 10,
247 static __init
unsigned long
248 mt7620_get_dram_rate(unsigned long pll_rate
)
250 if (dram_type
== SYSCFG0_DRAM_TYPE_SDRAM
)
256 static __init
unsigned long
257 mt7620_get_sys_rate(unsigned long cpu_rate
)
263 reg
= rt_sysc_r32(SYSC_REG_CPU_SYS_CLKCFG
);
265 ocp_ratio
= (reg
>> CPU_SYS_CLKCFG_OCP_RATIO_SHIFT
) &
266 CPU_SYS_CLKCFG_OCP_RATIO_MASK
;
268 if (WARN_ON(ocp_ratio
>= ARRAY_SIZE(mt7620_ocp_dividers
)))
271 div
= mt7620_ocp_dividers
[ocp_ratio
];
272 if (WARN(!div
, "invalid divider for OCP ratio %u", ocp_ratio
))
275 return cpu_rate
/ div
;
278 void __init
ralink_clk_init(void)
280 unsigned long xtal_rate
;
281 unsigned long cpu_pll_rate
;
282 unsigned long pll_rate
;
283 unsigned long cpu_rate
;
284 unsigned long sys_rate
;
285 unsigned long dram_rate
;
286 unsigned long periph_rate
;
288 xtal_rate
= mt7620_get_xtal_rate();
290 cpu_pll_rate
= mt7620_get_cpu_pll_rate(xtal_rate
);
291 pll_rate
= mt7620_get_pll_rate(xtal_rate
, cpu_pll_rate
);
293 cpu_rate
= mt7620_get_cpu_rate(pll_rate
);
294 dram_rate
= mt7620_get_dram_rate(pll_rate
);
295 sys_rate
= mt7620_get_sys_rate(cpu_rate
);
296 periph_rate
= mt7620_get_periph_rate(xtal_rate
);
298 #define RFMT(label) label ":%lu.%03luMHz "
299 #define RINT(x) ((x) / 1000000)
300 #define RFRAC(x) (((x) / 1000) % 1000)
302 pr_debug(RFMT("XTAL") RFMT("CPU_PLL") RFMT("PLL"),
303 RINT(xtal_rate
), RFRAC(xtal_rate
),
304 RINT(cpu_pll_rate
), RFRAC(cpu_pll_rate
),
305 RINT(pll_rate
), RFRAC(pll_rate
));
307 pr_debug(RFMT("CPU") RFMT("DRAM") RFMT("SYS") RFMT("PERIPH"),
308 RINT(cpu_rate
), RFRAC(cpu_rate
),
309 RINT(dram_rate
), RFRAC(dram_rate
),
310 RINT(sys_rate
), RFRAC(sys_rate
),
311 RINT(periph_rate
), RFRAC(periph_rate
));
317 ralink_clk_add("cpu", cpu_rate
);
318 ralink_clk_add("10000100.timer", periph_rate
);
319 ralink_clk_add("10000120.watchdog", periph_rate
);
320 ralink_clk_add("10000500.uart", periph_rate
);
321 ralink_clk_add("10000b00.spi", sys_rate
);
322 ralink_clk_add("10000c00.uartlite", periph_rate
);
325 void __init
ralink_of_remap(void)
327 rt_sysc_membase
= plat_of_remap_node("ralink,mt7620a-sysc");
328 rt_memc_membase
= plat_of_remap_node("ralink,mt7620a-memc");
330 if (!rt_sysc_membase
|| !rt_memc_membase
)
331 panic("Failed to remap core resources");
334 void prom_soc_init(struct ralink_soc_info
*soc_info
)
336 void __iomem
*sysc
= (void __iomem
*) KSEG1ADDR(MT7620_SYSC_BASE
);
337 unsigned char *name
= NULL
;
343 n0
= __raw_readl(sysc
+ SYSC_REG_CHIP_NAME0
);
344 n1
= __raw_readl(sysc
+ SYSC_REG_CHIP_NAME1
);
346 if (n0
== MT7620N_CHIP_NAME0
&& n1
== MT7620N_CHIP_NAME1
) {
348 soc_info
->compatible
= "ralink,mt7620n-soc";
349 } else if (n0
== MT7620A_CHIP_NAME0
&& n1
== MT7620A_CHIP_NAME1
) {
351 soc_info
->compatible
= "ralink,mt7620a-soc";
353 panic("mt7620: unknown SoC, n0:%08x n1:%08x\n", n0
, n1
);
356 rev
= __raw_readl(sysc
+ SYSC_REG_CHIP_REV
);
358 snprintf(soc_info
->sys_type
, RAMIPS_SYS_TYPE_LEN
,
359 "Ralink %s ver:%u eco:%u",
361 (rev
>> CHIP_REV_VER_SHIFT
) & CHIP_REV_VER_MASK
,
362 (rev
& CHIP_REV_ECO_MASK
));
364 cfg0
= __raw_readl(sysc
+ SYSC_REG_SYSTEM_CONFIG0
);
365 dram_type
= (cfg0
>> SYSCFG0_DRAM_TYPE_SHIFT
) & SYSCFG0_DRAM_TYPE_MASK
;
368 case SYSCFG0_DRAM_TYPE_SDRAM
:
369 pr_info("Board has SDRAM\n");
370 soc_info
->mem_size_min
= MT7620_SDRAM_SIZE_MIN
;
371 soc_info
->mem_size_max
= MT7620_SDRAM_SIZE_MAX
;
374 case SYSCFG0_DRAM_TYPE_DDR1
:
375 pr_info("Board has DDR1\n");
376 soc_info
->mem_size_min
= MT7620_DDR1_SIZE_MIN
;
377 soc_info
->mem_size_max
= MT7620_DDR1_SIZE_MAX
;
380 case SYSCFG0_DRAM_TYPE_DDR2
:
381 pr_info("Board has DDR2\n");
382 soc_info
->mem_size_min
= MT7620_DDR2_SIZE_MIN
;
383 soc_info
->mem_size_max
= MT7620_DDR2_SIZE_MAX
;
388 soc_info
->mem_base
= MT7620_DRAM_BASE
;