2 * Copyright (c) 2016 Stanislav Galabov.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/module.h>
37 #include <machine/fdt.h>
39 #include <dev/ofw/openfirm.h>
40 #include <dev/ofw/ofw_bus.h>
41 #include <dev/ofw/ofw_bus_subr.h>
43 #include <dev/fdt/fdt_common.h>
44 #include <dev/fdt/fdt_clock.h>
46 #include <mips/mediatek/fdt_reset.h>
47 #include <mips/mediatek/mtk_sysctl.h>
48 #include <mips/mediatek/mtk_soc.h>
50 static uint32_t mtk_soc_socid = MTK_SOC_UNKNOWN;
51 static uint32_t mtk_soc_uartclk = 0;
52 static uint32_t mtk_soc_cpuclk = MTK_CPU_CLK_880MHZ;
53 static uint32_t mtk_soc_timerclk = MTK_CPU_CLK_880MHZ / 2;
55 static const struct ofw_compat_data compat_data[] = {
56 { "ralink,rt3050-soc", MTK_SOC_RT3050 },
57 { "ralink,rt3052-soc", MTK_SOC_RT3052 },
58 { "ralink,rt3350-soc", MTK_SOC_RT3350 },
59 { "ralink,rt3352-soc", MTK_SOC_RT3352 },
60 { "ralink,rt3662-soc", MTK_SOC_RT3662 },
61 { "ralink,rt3883-soc", MTK_SOC_RT3883 },
62 { "ralink,rt5350-soc", MTK_SOC_RT5350 },
63 { "ralink,mtk7620a-soc", MTK_SOC_MT7620A },
64 { "ralink,mt7620a-soc", MTK_SOC_MT7620A },
65 { "ralink,mtk7620n-soc", MTK_SOC_MT7620N },
66 { "ralink,mt7620n-soc", MTK_SOC_MT7620N },
67 { "mediatek,mtk7621-soc", MTK_SOC_MT7621 },
68 { "mediatek,mt7621-soc", MTK_SOC_MT7621 },
69 { "ralink,mt7621-soc", MTK_SOC_MT7621 },
70 { "ralink,mtk7621-soc", MTK_SOC_MT7621 },
71 { "ralink,mtk7628an-soc", MTK_SOC_MT7628 },
72 { "mediatek,mt7628an-soc", MTK_SOC_MT7628 },
73 { "ralink,mtk7688-soc", MTK_SOC_MT7688 },
76 { NULL, MTK_SOC_UNKNOWN },
80 mtk_detect_cpuclk_rt305x(bus_space_tag_t bst, bus_space_handle_t bsh)
84 val = bus_space_read_4(bst, bsh, SYSCTL_CHIPID0_3);
85 if (val == RT3350_CHIPID0_3)
86 return (MTK_CPU_CLK_320MHZ);
88 val = bus_space_read_4(bst, bsh, SYSCTL_SYSCFG);
89 val >>= RT305X_CPU_CLKSEL_OFF;
90 val &= RT305X_CPU_CLKSEL_MSK;
92 return ((val == 0) ? MTK_CPU_CLK_320MHZ : MTK_CPU_CLK_384MHZ);
96 mtk_detect_cpuclk_rt3352(bus_space_tag_t bst, bus_space_handle_t bsh)
100 val = bus_space_read_4(bst, bsh, SYSCTL_SYSCFG);
101 val >>= RT3352_CPU_CLKSEL_OFF;
102 val &= RT3352_CPU_CLKSEL_MSK;
105 return (MTK_CPU_CLK_400MHZ);
107 return (MTK_CPU_CLK_384MHZ);
111 mtk_detect_cpuclk_rt3883(bus_space_tag_t bst, bus_space_handle_t bsh)
115 val = bus_space_read_4(bst, bsh, SYSCTL_SYSCFG);
116 val >>= RT3883_CPU_CLKSEL_OFF;
117 val &= RT3883_CPU_CLKSEL_MSK;
121 return (MTK_CPU_CLK_250MHZ);
123 return (MTK_CPU_CLK_384MHZ);
125 return (MTK_CPU_CLK_480MHZ);
127 return (MTK_CPU_CLK_500MHZ);
135 mtk_detect_cpuclk_rt5350(bus_space_tag_t bst, bus_space_handle_t bsh)
139 val1 = val2 = bus_space_read_4(bst, bsh, SYSCTL_SYSCFG);
141 val1 >>= RT5350_CPU_CLKSEL_OFF1;
142 val2 >>= RT5350_CPU_CLKSEL_OFF2;
143 val1 &= RT5350_CPU_CLKSEL_MSK;
144 val2 &= RT5350_CPU_CLKSEL_MSK;
149 return (MTK_CPU_CLK_360MHZ);
151 /* Reserved value, but we return UNKNOWN */
152 return (MTK_CPU_CLK_UNKNOWN);
154 return (MTK_CPU_CLK_320MHZ);
156 return (MTK_CPU_CLK_300MHZ);
164 mtk_detect_cpuclk_mt7620(bus_space_tag_t bst, bus_space_handle_t bsh)
166 uint32_t val, mul, div, res;
168 val = bus_space_read_4(bst, bsh, SYSCTL_MT7620_CPLL_CFG1);
169 if (val & MT7620_CPU_CLK_AUX0)
170 return (MTK_CPU_CLK_480MHZ);
172 val = bus_space_read_4(bst, bsh, SYSCTL_MT7620_CPLL_CFG0);
173 if (!(val & MT7620_CPLL_SW_CFG))
174 return (MTK_CPU_CLK_600MHZ);
176 mul = MT7620_PLL_MULT_RATIO_BASE + ((val >> MT7620_PLL_MULT_RATIO_OFF) &
177 MT7620_PLL_MULT_RATIO_MSK);
178 div = (val >> MT7620_PLL_DIV_RATIO_OFF) & MT7620_PLL_DIV_RATIO_MSK;
180 if (div != MT7620_PLL_DIV_RATIO_MSK)
181 div += MT7620_PLL_DIV_RATIO_BASE;
183 div = MT7620_PLL_DIV_RATIO_MAX;
185 res = (MT7620_XTAL_40 * mul) / div;
187 return (MTK_MHZ(res));
191 mtk_detect_cpuclk_mt7621(bus_space_tag_t bst, bus_space_handle_t bsh)
193 uint32_t val, div, res;
195 val = bus_space_read_4(bst, bsh, SYSCTL_CLKCFG0);
196 if (val & MT7621_USES_MEMDIV) {
197 div = bus_space_read_4(bst, bsh, MTK_MT7621_CLKDIV_REG);
198 div >>= MT7621_MEMDIV_OFF;
199 div &= MT7621_MEMDIV_MSK;
200 div += MT7621_MEMDIV_BASE;
202 val = bus_space_read_4(bst, bsh, SYSCTL_SYSCFG);
203 val >>= MT7621_CLKSEL_OFF;
204 val &= MT7621_CLKSEL_MSK;
206 if (val >= MT7621_CLKSEL_25MHZ_VAL)
207 res = div * MT7621_CLKSEL_25MHZ;
208 else if (val >= MT7621_CLKSEL_20MHZ_VAL)
209 res = div * MT7621_CLKSEL_20MHZ;
211 res = div * 0; /* XXX: not sure about this */
213 val = bus_space_read_4(bst, bsh, SYSCTL_CUR_CLK_STS);
214 div = (val >> MT7621_CLK_STS_DIV_OFF) & MT7621_CLK_STS_MSK;
215 val &= MT7621_CLK_STS_MSK;
217 res = (MT7621_CLK_STS_BASE * val) / div;
220 return (MTK_MHZ(res));
224 mtk_detect_cpuclk_mt7628(bus_space_tag_t bst, bus_space_handle_t bsh)
228 val = bus_space_read_4(bst, bsh, SYSCTL_SYSCFG);
229 val >>= MT7628_CPU_CLKSEL_OFF;
230 val &= MT7628_CPU_CLKSEL_MSK;
233 return (MTK_CPU_CLK_580MHZ);
235 return (MTK_CPU_CLK_575MHZ);
239 mtk_soc_try_early_detect(void)
242 bus_space_handle_t bsh;
247 if ((node = OF_finddevice("/")) == -1)
250 for (i = 0; compat_data[i].ocd_str != NULL; i++) {
251 if (fdt_is_compatible(node, compat_data[i].ocd_str)) {
252 mtk_soc_socid = compat_data[i].ocd_data;
257 if (mtk_soc_socid == MTK_SOC_UNKNOWN) {
258 /* We don't know the SoC, so we don't know how to get clocks */
263 if (mtk_soc_socid == MTK_SOC_MT7621)
264 base = MTK_MT7621_BASE;
266 base = MTK_DEFAULT_BASE;
268 if (bus_space_map(bst, base, MTK_DEFAULT_SIZE, 0, &bsh))
271 /* First, figure out the CPU clock */
272 switch (mtk_soc_socid) {
273 case MTK_SOC_RT3050: /* fallthrough */
276 mtk_soc_cpuclk = mtk_detect_cpuclk_rt305x(bst, bsh);
279 mtk_soc_cpuclk = mtk_detect_cpuclk_rt3352(bst, bsh);
281 case MTK_SOC_RT3662: /* fallthrough */
283 mtk_soc_cpuclk = mtk_detect_cpuclk_rt3883(bst, bsh);
286 mtk_soc_cpuclk = mtk_detect_cpuclk_rt5350(bst, bsh);
288 case MTK_SOC_MT7620A: /* fallthrough */
289 case MTK_SOC_MT7620N:
290 mtk_soc_cpuclk = mtk_detect_cpuclk_mt7620(bst, bsh);
293 mtk_soc_cpuclk = mtk_detect_cpuclk_mt7621(bst, bsh);
295 case MTK_SOC_MT7628: /* fallthrough */
297 mtk_soc_cpuclk = mtk_detect_cpuclk_mt7628(bst, bsh);
300 /* We don't know the SoC, so we can't find the CPU clock */
304 /* Now figure out the timer clock */
305 if (mtk_soc_socid == MTK_SOC_MT7621) {
308 * We use the GIC timer for timing source and its clock freq is
309 * the same as the CPU's clock freq
311 mtk_soc_timerclk = mtk_soc_cpuclk;
314 * When GIC timer and MIPS timer are ready to co-exist and
315 * GIC timer is actually implemented, we need to switch to it.
316 * Until then we use a fake GIC timer, which is actually a
317 * normal MIPS ticker, so the timer clock is half the CPU clock
319 mtk_soc_timerclk = mtk_soc_cpuclk / 2;
323 * We use the MIPS ticker for the rest for now, so
324 * the CPU clock is divided by 2
326 mtk_soc_timerclk = mtk_soc_cpuclk / 2;
329 switch (mtk_soc_socid) {
330 case MTK_SOC_RT3350: /* fallthrough */
331 case MTK_SOC_RT3050: /* fallthrough */
333 /* UART clock is CPU clock / 3 */
334 mtk_soc_uartclk = mtk_soc_cpuclk / MTK_UARTDIV_3;
336 case MTK_SOC_RT3352: /* fallthrough */
337 case MTK_SOC_RT3662: /* fallthrough */
338 case MTK_SOC_RT3883: /* fallthrough */
339 case MTK_SOC_RT5350: /* fallthrough */
340 case MTK_SOC_MT7620A: /* fallthrough */
341 case MTK_SOC_MT7620N: /* fallthrough */
342 case MTK_SOC_MT7628: /* fallthrough */
344 /* UART clock is always 40MHz */
345 mtk_soc_uartclk = MTK_UART_CLK_40MHZ;
348 /* UART clock is always 50MHz */
349 mtk_soc_uartclk = MTK_UART_CLK_50MHZ;
352 /* We don't know the SoC, so we don't know the UART clock */
356 bus_space_unmap(bst, bsh, MTK_DEFAULT_SIZE);
360 mtk_soc_get_uartclk(void)
363 return mtk_soc_uartclk;
367 mtk_soc_get_cpuclk(void)
370 return mtk_soc_cpuclk;
374 mtk_soc_get_timerclk(void)
377 return mtk_soc_timerclk;
381 mtk_soc_get_socid(void)
384 return mtk_soc_socid;
388 * The following are generic reset and clock functions
391 /* Default reset time is 100ms */
392 #define DEFAULT_RESET_TIME 100000
395 mtk_soc_reset_device(device_t dev)
399 res = fdt_reset_assert_all(dev);
401 DELAY(DEFAULT_RESET_TIME);
402 res = fdt_reset_deassert_all(dev);
404 DELAY(DEFAULT_RESET_TIME);
411 mtk_soc_stop_clock(device_t dev)
414 return (fdt_clock_disable_all(dev));
418 mtk_soc_start_clock(device_t dev)
421 return (fdt_clock_enable_all(dev));
425 mtk_soc_assert_reset(device_t dev)
428 return (fdt_reset_assert_all(dev));
432 mtk_soc_deassert_reset(device_t dev)
435 return (fdt_reset_deassert_all(dev));
442 mtk_sysctl_clr_set(SYSCTL_RSTCTRL, 0, 1);
443 mtk_sysctl_clr_set(SYSCTL_RSTCTRL, 1, 0);