2 * Copyright (c) 2014 Ganbold Tsagaankhuu <ganbold@freebsd.org>
3 * Copyright (c) 2016 Emmanuel Vadot <manu@freebsd.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/systm.h>
33 #include <sys/kernel.h>
35 #include <sys/mutex.h>
41 #include <machine/cpu.h>
42 #include <machine/cpu-v6.h>
43 #include <machine/smp.h>
44 #include <machine/fdt.h>
45 #include <machine/intr.h>
46 #include <machine/platformvar.h>
48 #include <arm/allwinner/aw_mp.h>
49 #include <arm/allwinner/aw_machdep.h>
51 /* Register for all dual-core SoC */
52 #define A20_CPUCFG_BASE 0x01c25c00
53 /* Register for all quad-core SoC */
54 #define CPUCFG_BASE 0x01f01c00
55 #define CPUCFG_SIZE 0x400
56 #define PRCM_BASE 0x01f01400
57 #define PRCM_SIZE 0x800
58 /* Register for multi-cluster SoC */
59 #define CPUXCFG_BASE 0x01700000
60 #define CPUXCFG_SIZE 0x400
62 #define CPU_OFFSET 0x40
63 #define CPU_OFFSET_CTL 0x04
64 #define CPU_OFFSET_STATUS 0x08
65 #define CPU_RST_CTL(cpuid) ((cpuid + 1) * CPU_OFFSET)
66 #define CPU_CTL(cpuid) (((cpuid + 1) * CPU_OFFSET) + CPU_OFFSET_CTL)
67 #define CPU_STATUS(cpuid) (((cpuid + 1) * CPU_OFFSET) + CPU_OFFSET_STATUS)
69 #define CPU_RESET (1 << 0)
70 #define CPU_CORE_RESET (1 << 1)
72 #define CPUCFG_GENCTL 0x184
73 #define CPUCFG_P_REG0 0x1a4
75 #define A20_CPU1_PWR_CLAMP 0x1b0
76 #define CPU_PWR_CLAMP_REG 0x140
77 #define CPU_PWR_CLAMP(cpu) ((cpu * 4) + CPU_PWR_CLAMP_REG)
78 #define CPU_PWR_CLAMP_STEPS 8
80 #define A20_CPU1_PWROFF_REG 0x1b4
81 #define CPU_PWROFF 0x100
83 #define CPUCFG_DBGCTL0 0x1e0
84 #define CPUCFG_DBGCTL1 0x1e4
86 #define CPUS_CL_RST(cl) (0x30 + (cl) * 0x4)
87 #define CPUX_CL_CTRL0(cl) (0x0 + (cl) * 0x10)
88 #define CPUX_CL_CTRL1(cl) (0x4 + (cl) * 0x10)
89 #define CPUX_CL_CPU_STATUS(cl) (0x30 + (cl) * 0x4)
90 #define CPUX_CL_RST(cl) (0x80 + (cl) * 0x4)
91 #define PRCM_CL_PWROFF(cl) (0x100 + (cl) * 0x4)
92 #define PRCM_CL_PWR_CLAMP(cl, cpu) (0x140 + (cl) * 0x4 + (cpu) * 0x4)
95 aw_mp_setmaxid(platform_t plat)
103 reg = cp15_l2ctlr_get();
104 ncpu = CPUV7_L2CTLR_NPROC(reg);
111 aw_mp_start_ap(platform_t plat)
113 bus_space_handle_t cpucfg;
114 bus_space_handle_t prcm;
115 int i, j, soc_family;
118 soc_family = allwinner_soc_family();
119 if (soc_family == ALLWINNERSOC_SUN7I) {
120 if (bus_space_map(fdtbus_bs_tag, A20_CPUCFG_BASE, CPUCFG_SIZE,
122 panic("Couldn't map the CPUCFG\n");
124 if (bus_space_map(fdtbus_bs_tag, CPUCFG_BASE, CPUCFG_SIZE,
126 panic("Couldn't map the CPUCFG\n");
127 if (bus_space_map(fdtbus_bs_tag, PRCM_BASE, PRCM_SIZE, 0,
129 panic("Couldn't map the PRCM\n");
132 dcache_wbinv_poc_all();
134 bus_space_write_4(fdtbus_bs_tag, cpucfg, CPUCFG_P_REG0,
135 pmap_kextract((vm_offset_t)mpentry));
138 * Assert nCOREPORESET low and set L1RSTDISABLE low.
139 * Ensure DBGPWRDUP is set to LOW to prevent any external
140 * debug access to the processor.
142 for (i = 1; i < mp_ncpus; i++)
143 bus_space_write_4(fdtbus_bs_tag, cpucfg, CPU_RST_CTL(i), 0);
145 /* Set L1RSTDISABLE low */
146 val = bus_space_read_4(fdtbus_bs_tag, cpucfg, CPUCFG_GENCTL);
147 for (i = 1; i < mp_ncpus; i++)
149 bus_space_write_4(fdtbus_bs_tag, cpucfg, CPUCFG_GENCTL, val);
151 /* Set DBGPWRDUP low */
152 val = bus_space_read_4(fdtbus_bs_tag, cpucfg, CPUCFG_DBGCTL1);
153 for (i = 1; i < mp_ncpus; i++)
155 bus_space_write_4(fdtbus_bs_tag, cpucfg, CPUCFG_DBGCTL1, val);
157 /* Release power clamp */
158 for (i = 1; i < mp_ncpus; i++)
159 for (j = 0; j <= CPU_PWR_CLAMP_STEPS; j++) {
160 if (soc_family != ALLWINNERSOC_SUN7I) {
161 bus_space_write_4(fdtbus_bs_tag, prcm,
162 CPU_PWR_CLAMP(i), 0xff >> j);
164 bus_space_write_4(fdtbus_bs_tag,
165 cpucfg, A20_CPU1_PWR_CLAMP, 0xff >> j);
170 /* Clear power-off gating */
171 if (soc_family != ALLWINNERSOC_SUN7I) {
172 val = bus_space_read_4(fdtbus_bs_tag, prcm, CPU_PWROFF);
173 for (i = 0; i < mp_ncpus; i++)
175 bus_space_write_4(fdtbus_bs_tag, prcm, CPU_PWROFF, val);
177 val = bus_space_read_4(fdtbus_bs_tag,
178 cpucfg, A20_CPU1_PWROFF_REG);
180 bus_space_write_4(fdtbus_bs_tag, cpucfg,
181 A20_CPU1_PWROFF_REG, val);
185 /* De-assert cpu core reset */
186 for (i = 1; i < mp_ncpus; i++)
187 bus_space_write_4(fdtbus_bs_tag, cpucfg, CPU_RST_CTL(i),
188 CPU_RESET | CPU_CORE_RESET);
190 /* Assert DBGPWRDUP signal */
191 val = bus_space_read_4(fdtbus_bs_tag, cpucfg, CPUCFG_DBGCTL1);
192 for (i = 1; i < mp_ncpus; i++)
194 bus_space_write_4(fdtbus_bs_tag, cpucfg, CPUCFG_DBGCTL1, val);
198 bus_space_unmap(fdtbus_bs_tag, cpucfg, CPUCFG_SIZE);
199 if (soc_family != ALLWINNERSOC_SUN7I)
200 bus_space_unmap(fdtbus_bs_tag, prcm, PRCM_SIZE);
204 aw_mc_mp_start_cpu(bus_space_handle_t cpuscfg, bus_space_handle_t cpuxcfg,
205 bus_space_handle_t prcm, int cluster, int cpu)
210 /* Assert core reset */
211 val = bus_space_read_4(fdtbus_bs_tag, cpuxcfg, CPUX_CL_RST(cluster));
213 bus_space_write_4(fdtbus_bs_tag, cpuxcfg, CPUX_CL_RST(cluster), val);
215 /* Assert power-on reset */
216 val = bus_space_read_4(fdtbus_bs_tag, cpuscfg, CPUS_CL_RST(cluster));
218 bus_space_write_4(fdtbus_bs_tag, cpuscfg, CPUS_CL_RST(cluster), val);
220 /* Disable automatic L1 cache invalidate at reset */
221 val = bus_space_read_4(fdtbus_bs_tag, cpuxcfg, CPUX_CL_CTRL0(cluster));
223 bus_space_write_4(fdtbus_bs_tag, cpuxcfg, CPUX_CL_CTRL0(cluster), val);
225 /* Release power clamp */
226 for (i = 0; i <= CPU_PWR_CLAMP_STEPS; i++)
227 bus_space_write_4(fdtbus_bs_tag, prcm,
228 PRCM_CL_PWR_CLAMP(cluster, cpu), 0xff >> i);
229 while (bus_space_read_4(fdtbus_bs_tag, prcm,
230 PRCM_CL_PWR_CLAMP(cluster, cpu)) != 0)
233 /* Clear power-off gating */
234 val = bus_space_read_4(fdtbus_bs_tag, prcm, PRCM_CL_PWROFF(cluster));
236 bus_space_write_4(fdtbus_bs_tag, prcm, PRCM_CL_PWROFF(cluster), val);
238 /* De-assert power-on reset */
239 val = bus_space_read_4(fdtbus_bs_tag, cpuscfg, CPUS_CL_RST(cluster));
241 bus_space_write_4(fdtbus_bs_tag, cpuscfg, CPUS_CL_RST(cluster), val);
243 /* De-assert core reset */
244 val = bus_space_read_4(fdtbus_bs_tag, cpuxcfg, CPUX_CL_RST(cluster));
246 bus_space_write_4(fdtbus_bs_tag, cpuxcfg, CPUX_CL_RST(cluster), val);
250 aw_mc_mp_start_ap(bus_space_handle_t cpuscfg, bus_space_handle_t cpuxcfg,
251 bus_space_handle_t prcm)
255 KASSERT(mp_ncpus <= 4, ("multiple clusters not yet supported"));
257 dcache_wbinv_poc_all();
259 bus_space_write_4(fdtbus_bs_tag, cpuscfg, CPUCFG_P_REG0,
260 pmap_kextract((vm_offset_t)mpentry));
263 for (cpu = 1; cpu < mp_ncpus; cpu++)
264 aw_mc_mp_start_cpu(cpuscfg, cpuxcfg, prcm, cluster, cpu);
268 a83t_mp_start_ap(platform_t plat)
270 bus_space_handle_t cpuscfg, cpuxcfg, prcm;
272 if (bus_space_map(fdtbus_bs_tag, CPUCFG_BASE, CPUCFG_SIZE,
274 panic("Couldn't map the CPUCFG\n");
275 if (bus_space_map(fdtbus_bs_tag, CPUXCFG_BASE, CPUXCFG_SIZE,
277 panic("Couldn't map the CPUXCFG\n");
278 if (bus_space_map(fdtbus_bs_tag, PRCM_BASE, PRCM_SIZE, 0,
280 panic("Couldn't map the PRCM\n");
282 aw_mc_mp_start_ap(cpuscfg, cpuxcfg, prcm);
285 bus_space_unmap(fdtbus_bs_tag, cpuxcfg, CPUXCFG_SIZE);
286 bus_space_unmap(fdtbus_bs_tag, cpuscfg, CPUCFG_SIZE);
287 bus_space_unmap(fdtbus_bs_tag, prcm, PRCM_SIZE);