exynos_platform.c revision 1.21 1 1.21 jmcneill /* $NetBSD: exynos_platform.c,v 1.21 2019/01/03 23:04:09 jmcneill Exp $ */
2 1.1 jmcneill
3 1.1 jmcneill /*-
4 1.1 jmcneill * Copyright (c) 2017 Jared D. McNeill <jmcneill (at) invisible.ca>
5 1.1 jmcneill * All rights reserved.
6 1.1 jmcneill *
7 1.1 jmcneill * Redistribution and use in source and binary forms, with or without
8 1.1 jmcneill * modification, are permitted provided that the following conditions
9 1.1 jmcneill * are met:
10 1.1 jmcneill * 1. Redistributions of source code must retain the above copyright
11 1.1 jmcneill * notice, this list of conditions and the following disclaimer.
12 1.1 jmcneill * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 jmcneill * notice, this list of conditions and the following disclaimer in the
14 1.1 jmcneill * documentation and/or other materials provided with the distribution.
15 1.1 jmcneill *
16 1.1 jmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.1 jmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.1 jmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.1 jmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.1 jmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 1.1 jmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 1.1 jmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 1.1 jmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 1.1 jmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1 jmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1 jmcneill * SUCH DAMAGE.
27 1.1 jmcneill */
28 1.1 jmcneill
29 1.11 skrll #include "opt_arm_debug.h"
30 1.17 skrll #include "opt_console.h"
31 1.1 jmcneill #include "opt_exynos.h"
32 1.1 jmcneill #include "opt_multiprocessor.h"
33 1.17 skrll #include "opt_console.h"
34 1.1 jmcneill
35 1.1 jmcneill #include "ukbd.h"
36 1.1 jmcneill
37 1.1 jmcneill #include <sys/cdefs.h>
38 1.21 jmcneill __KERNEL_RCSID(0, "$NetBSD: exynos_platform.c,v 1.21 2019/01/03 23:04:09 jmcneill Exp $");
39 1.1 jmcneill
40 1.1 jmcneill #include <sys/param.h>
41 1.1 jmcneill #include <sys/bus.h>
42 1.1 jmcneill #include <sys/cpu.h>
43 1.1 jmcneill #include <sys/device.h>
44 1.1 jmcneill #include <sys/termios.h>
45 1.1 jmcneill
46 1.1 jmcneill #include <dev/fdt/fdtvar.h>
47 1.1 jmcneill
48 1.1 jmcneill #include <uvm/uvm_extern.h>
49 1.1 jmcneill
50 1.1 jmcneill #include <machine/bootconfig.h>
51 1.1 jmcneill #include <arm/cpufunc.h>
52 1.1 jmcneill
53 1.1 jmcneill #include <arm/samsung/exynos_reg.h>
54 1.1 jmcneill #include <arm/samsung/exynos_var.h>
55 1.10 jmcneill #include <arm/samsung/mct_var.h>
56 1.13 skrll #include <arm/samsung/sscom_reg.h>
57 1.1 jmcneill
58 1.2 jmcneill #include <evbarm/exynos/platform.h>
59 1.19 skrll #include <evbarm/fdt/machdep.h>
60 1.2 jmcneill
61 1.1 jmcneill #include <arm/fdt/arm_fdtvar.h>
62 1.1 jmcneill
63 1.13 skrll void exynos_platform_early_putchar(char);
64 1.13 skrll
65 1.15 jmcneill #define EXYNOS5800_PMU_BASE 0x10040000
66 1.15 jmcneill #define EXYNOS5800_PMU_SIZE 0x20000
67 1.21 jmcneill #define EXYNOS5800_PMU_SWRESET 0x0400
68 1.21 jmcneill #define EXYNOS5800_PMU_KFC_ETM_RESET(n) __BIT(20 + (n))
69 1.21 jmcneill #define EXYNOS5800_PMU_KFC_CORE_RESET(n) __BIT(8 + (n))
70 1.21 jmcneill #define EXYNOS5800_PMU_SPARE2 0x0908
71 1.21 jmcneill #define EXYNOS5800_PMU_SPARE3 0x090c
72 1.21 jmcneill #define EXYNOS5800_PMU_SWRESET_KFC_SEL 0x3
73 1.21 jmcneill #define EXYNOS5800_PMU_CORE_CONFIG(n) (0x2000 + 0x80 * (n))
74 1.21 jmcneill #define EXYNOS5800_PMU_CORE_STATUS(n) (0x2004 + 0x80 * (n))
75 1.21 jmcneill #define EXYNOS5800_PMU_CORE_POWER_EN 0x3
76 1.21 jmcneill #define EXYNOS5800_PMU_COMMON_CONFIG(n) (0x2500 + 0x80 * (n))
77 1.21 jmcneill #define EXYNOS5800_PMU_COMMON_POWER_EN 0x3
78 1.21 jmcneill #define EXYNOS5800_PMU_COMMON_OPTION(n) (0x2508 + 0x80 * (n))
79 1.21 jmcneill #define EXYNOS5800_PMU_USE_L2_COMMON_UP_STATE __BIT(30)
80 1.21 jmcneill #define EXYNOS5800_PMU_USE_ARM_CORE_DOWN_STATE __BIT(29)
81 1.21 jmcneill #define EXYNOS5800_PMU_AUTO_CORE_DOWN __BIT(9)
82 1.21 jmcneill
83 1.21 jmcneill #define EXYNOS5800_SYSRAM_BASE 0x02073000
84 1.21 jmcneill #define EXYNOS5800_SYSRAM_SIZE 0x1000
85 1.21 jmcneill #define EXYNOS5800_SYSRAM_HOTPLUG 0x001c
86 1.19 skrll
87 1.19 skrll static void
88 1.19 skrll exynos5800_mpstart(void)
89 1.19 skrll {
90 1.19 skrll #if defined(MULTIPROCESSOR)
91 1.15 jmcneill bus_space_tag_t bst = &armv7_generic_bs_tag;
92 1.15 jmcneill bus_space_handle_t pmu_bsh, sysram_bsh;
93 1.21 jmcneill uint64_t mpidr, bp_mpidr;
94 1.15 jmcneill uint32_t val, started = 0;
95 1.21 jmcneill u_int cpuindex, n;
96 1.21 jmcneill int child;
97 1.15 jmcneill
98 1.15 jmcneill bus_space_map(bst, EXYNOS5800_PMU_BASE, EXYNOS5800_PMU_SIZE, 0, &pmu_bsh);
99 1.15 jmcneill bus_space_map(bst, EXYNOS5800_SYSRAM_BASE, EXYNOS5800_SYSRAM_SIZE, 0, &sysram_bsh);
100 1.15 jmcneill
101 1.21 jmcneill const int cpus = OF_finddevice("/cpus");
102 1.21 jmcneill if (cpus == -1) {
103 1.21 jmcneill aprint_error("%s: no /cpus node found\n", __func__);
104 1.21 jmcneill return;
105 1.21 jmcneill }
106 1.21 jmcneill
107 1.21 jmcneill /* MPIDR affinity levels of boot processor. */
108 1.21 jmcneill bp_mpidr = cpu_mpidr_aff_read();
109 1.21 jmcneill
110 1.21 jmcneill /* Setup KFC reset */
111 1.21 jmcneill bus_space_write_4(bst, pmu_bsh, EXYNOS5800_PMU_SPARE3, EXYNOS5800_PMU_SWRESET_KFC_SEL);
112 1.15 jmcneill
113 1.21 jmcneill const uint32_t option = EXYNOS5800_PMU_USE_L2_COMMON_UP_STATE |
114 1.21 jmcneill EXYNOS5800_PMU_USE_ARM_CORE_DOWN_STATE |
115 1.21 jmcneill EXYNOS5800_PMU_AUTO_CORE_DOWN;
116 1.21 jmcneill val = bus_space_read_4(bst, pmu_bsh, EXYNOS5800_PMU_COMMON_OPTION(0));
117 1.21 jmcneill bus_space_write_4(bst, pmu_bsh, EXYNOS5800_PMU_COMMON_OPTION(0), val | option);
118 1.21 jmcneill val = bus_space_read_4(bst, pmu_bsh, EXYNOS5800_PMU_COMMON_OPTION(1));
119 1.21 jmcneill bus_space_write_4(bst, pmu_bsh, EXYNOS5800_PMU_COMMON_OPTION(1), val | option);
120 1.21 jmcneill
121 1.21 jmcneill bus_space_write_4(bst, sysram_bsh, EXYNOS5800_SYSRAM_HOTPLUG, KERN_VTOPHYS((vaddr_t)cpu_mpstart));
122 1.21 jmcneill arm_dsb();
123 1.21 jmcneill
124 1.21 jmcneill /* Power on clusters */
125 1.21 jmcneill bus_space_write_4(bst, pmu_bsh, EXYNOS5800_PMU_COMMON_CONFIG(0),
126 1.21 jmcneill EXYNOS5800_PMU_COMMON_POWER_EN);
127 1.21 jmcneill bus_space_write_4(bst, pmu_bsh, EXYNOS5800_PMU_COMMON_CONFIG(1),
128 1.21 jmcneill EXYNOS5800_PMU_COMMON_POWER_EN);
129 1.21 jmcneill
130 1.21 jmcneill /* Boot APs */
131 1.21 jmcneill cpuindex = 1;
132 1.21 jmcneill for (child = OF_child(cpus); child; child = OF_peer(child)) {
133 1.21 jmcneill if (fdtbus_get_reg64(child, 0, &mpidr, NULL) != 0)
134 1.21 jmcneill continue;
135 1.21 jmcneill
136 1.21 jmcneill if (mpidr == bp_mpidr)
137 1.21 jmcneill continue; /* BP already started */
138 1.21 jmcneill
139 1.21 jmcneill const u_int cluster = __SHIFTOUT(mpidr, MPIDR_AFF1);
140 1.21 jmcneill const u_int aff0 = __SHIFTOUT(mpidr, MPIDR_AFF0);
141 1.21 jmcneill const u_int cpu = cluster * 4 + aff0;
142 1.21 jmcneill
143 1.21 jmcneill val = bus_space_read_4(bst, pmu_bsh, EXYNOS5800_PMU_CORE_STATUS(cpu));
144 1.21 jmcneill bus_space_write_4(bst, pmu_bsh, EXYNOS5800_PMU_CORE_CONFIG(cpu),
145 1.15 jmcneill EXYNOS5800_PMU_CORE_POWER_EN);
146 1.21 jmcneill
147 1.21 jmcneill for (n = 0x100000; n > 0; n--) {
148 1.21 jmcneill val = bus_space_read_4(bst, pmu_bsh, EXYNOS5800_PMU_CORE_STATUS(cpu));
149 1.15 jmcneill if ((val & EXYNOS5800_PMU_CORE_POWER_EN) == EXYNOS5800_PMU_CORE_POWER_EN) {
150 1.21 jmcneill started |= __BIT(cpuindex);
151 1.15 jmcneill break;
152 1.15 jmcneill }
153 1.15 jmcneill }
154 1.21 jmcneill if (n == 0)
155 1.21 jmcneill aprint_error("cpu%d: WARNING: AP failed to power on\n", cpuindex);
156 1.21 jmcneill
157 1.21 jmcneill if (cluster == 1 && __SHIFTOUT(bp_mpidr, MPIDR_AFF1) == 1) {
158 1.21 jmcneill while (bus_space_read_4(bst, pmu_bsh, EXYNOS5800_PMU_SPARE2) == 0)
159 1.21 jmcneill ;
160 1.21 jmcneill bus_space_write_4(bst, pmu_bsh, EXYNOS5800_PMU_SWRESET,
161 1.21 jmcneill EXYNOS5800_PMU_KFC_CORE_RESET(aff0) |
162 1.21 jmcneill EXYNOS5800_PMU_KFC_ETM_RESET(aff0));
163 1.21 jmcneill }
164 1.21 jmcneill
165 1.21 jmcneill /* Wait for AP to start */
166 1.21 jmcneill for (n = 0x100000; n > 0; n--) {
167 1.21 jmcneill membar_consumer();
168 1.21 jmcneill if (arm_cpu_hatched & __BIT(cpuindex))
169 1.21 jmcneill break;
170 1.21 jmcneill }
171 1.21 jmcneill if (n == 0)
172 1.21 jmcneill aprint_error("cpu%d: WARNING: AP failed to start\n", cpuindex);
173 1.15 jmcneill
174 1.21 jmcneill cpuindex++;
175 1.15 jmcneill }
176 1.15 jmcneill
177 1.15 jmcneill bus_space_unmap(bst, sysram_bsh, EXYNOS5800_SYSRAM_SIZE);
178 1.15 jmcneill bus_space_unmap(bst, pmu_bsh, EXYNOS5800_PMU_SIZE);
179 1.15 jmcneill #endif
180 1.15 jmcneill }
181 1.15 jmcneill
182 1.15 jmcneill static struct of_compat_data mp_compat_data[] = {
183 1.19 skrll { "samsung,exynos5800", (uintptr_t)exynos5800_mpstart },
184 1.15 jmcneill { NULL }
185 1.15 jmcneill };
186 1.15 jmcneill
187 1.1 jmcneill static void
188 1.19 skrll exynos_platform_mpstart(void)
189 1.1 jmcneill {
190 1.13 skrll
191 1.19 skrll void (*mp_start)(void) = NULL;
192 1.19 skrll
193 1.15 jmcneill const struct of_compat_data *cd = of_search_compatible(OF_finddevice("/"), mp_compat_data);
194 1.15 jmcneill if (cd)
195 1.19 skrll mp_start = (void (*)(void))cd->data;
196 1.15 jmcneill
197 1.19 skrll if (mp_start)
198 1.19 skrll mp_start();
199 1.1 jmcneill }
200 1.1 jmcneill
201 1.1 jmcneill static void
202 1.1 jmcneill exynos_platform_init_attach_args(struct fdt_attach_args *faa)
203 1.1 jmcneill {
204 1.1 jmcneill extern struct bus_space armv7_generic_bs_tag;
205 1.1 jmcneill extern struct bus_space armv7_generic_a4x_bs_tag;
206 1.9 ryo extern struct arm32_bus_dma_tag arm_generic_dma_tag;
207 1.1 jmcneill
208 1.1 jmcneill faa->faa_bst = &armv7_generic_bs_tag;
209 1.1 jmcneill faa->faa_a4x_bst = &armv7_generic_a4x_bs_tag;
210 1.9 ryo faa->faa_dmat = &arm_generic_dma_tag;
211 1.1 jmcneill }
212 1.1 jmcneill
213 1.13 skrll void
214 1.1 jmcneill exynos_platform_early_putchar(char c)
215 1.1 jmcneill {
216 1.13 skrll #ifdef CONSADDR
217 1.13 skrll #define CONSADDR_VA (CONSADDR - EXYNOS_CORE_PBASE + EXYNOS_CORE_VBASE)
218 1.2 jmcneill
219 1.13 skrll volatile uint32_t *uartaddr = cpu_earlydevice_va_p() ?
220 1.13 skrll (volatile uint32_t *)CONSADDR_VA :
221 1.13 skrll (volatile uint32_t *)CONSADDR;
222 1.14 skrll
223 1.13 skrll while ((uartaddr[SSCOM_UFSTAT / 4] & UFSTAT_TXFULL) != 0)
224 1.13 skrll ;
225 1.13 skrll
226 1.13 skrll uartaddr[SSCOM_UTXH / 4] = c;
227 1.4 jmcneill #endif
228 1.1 jmcneill }
229 1.1 jmcneill
230 1.1 jmcneill static void
231 1.1 jmcneill exynos_platform_device_register(device_t self, void *aux)
232 1.1 jmcneill {
233 1.1 jmcneill exynos_device_register(self, aux);
234 1.1 jmcneill }
235 1.1 jmcneill
236 1.1 jmcneill static void
237 1.6 jmcneill exynos5_platform_reset(void)
238 1.1 jmcneill {
239 1.6 jmcneill bus_space_tag_t bst = &armv7_generic_bs_tag;
240 1.6 jmcneill bus_space_handle_t bsh;
241 1.6 jmcneill
242 1.21 jmcneill bus_space_map(bst, EXYNOS5800_PMU_BASE + EXYNOS5800_PMU_SWRESET, 4, 0, &bsh);
243 1.6 jmcneill bus_space_write_4(bst, bsh, 0, 1);
244 1.1 jmcneill }
245 1.1 jmcneill
246 1.1 jmcneill static u_int
247 1.1 jmcneill exynos_platform_uart_freq(void)
248 1.1 jmcneill {
249 1.1 jmcneill return EXYNOS_UART_FREQ;
250 1.1 jmcneill }
251 1.1 jmcneill
252 1.13 skrll
253 1.13 skrll #if defined(SOC_EXYNOS4)
254 1.13 skrll static const struct pmap_devmap *
255 1.13 skrll exynos4_platform_devmap(void)
256 1.13 skrll {
257 1.13 skrll static const struct pmap_devmap devmap[] = {
258 1.13 skrll DEVMAP_ENTRY(EXYNOS_CORE_VBASE,
259 1.13 skrll EXYNOS_CORE_PBASE,
260 1.13 skrll EXYNOS4_CORE_SIZE),
261 1.13 skrll DEVMAP_ENTRY(EXYNOS4_AUDIOCORE_VBASE,
262 1.13 skrll EXYNOS4_AUDIOCORE_PBASE,
263 1.13 skrll EXYNOS4_AUDIOCORE_SIZE),
264 1.13 skrll DEVMAP_ENTRY_END
265 1.13 skrll };
266 1.13 skrll
267 1.13 skrll return devmap;
268 1.13 skrll }
269 1.13 skrll
270 1.18 skrll static void
271 1.18 skrll exynos4_platform_bootstrap(void)
272 1.18 skrll {
273 1.18 skrll
274 1.18 skrll exynos_bootstrap(4);
275 1.18 skrll
276 1.21 jmcneill #if defined(MULTIPROCESSOR)
277 1.21 jmcneill arm_cpu_max = 1 + __SHIFTOUT(armreg_l2ctrl_read(), L2CTRL_NUMCPU);
278 1.21 jmcneill #endif
279 1.18 skrll }
280 1.18 skrll
281 1.13 skrll static const struct arm_platform exynos4_platform = {
282 1.13 skrll .ap_devmap = exynos4_platform_devmap,
283 1.19 skrll // .ap_mpstart = exynos4_mpstart,
284 1.18 skrll .ap_bootstrap = exynos4_platform_bootstrap,
285 1.13 skrll .ap_init_attach_args = exynos_platform_init_attach_args,
286 1.13 skrll .ap_device_register = exynos_platform_device_register,
287 1.13 skrll .ap_reset = exynos5_platform_reset,
288 1.13 skrll .ap_delay = mct_delay,
289 1.13 skrll .ap_uart_freq = exynos_platform_uart_freq,
290 1.13 skrll };
291 1.13 skrll
292 1.13 skrll ARM_PLATFORM(exynos4, "samsung,exynos4", &exynos4_platform);
293 1.13 skrll #endif
294 1.13 skrll
295 1.13 skrll
296 1.13 skrll #if defined(SOC_EXYNOS5)
297 1.13 skrll static const struct pmap_devmap *
298 1.13 skrll exynos5_platform_devmap(void)
299 1.13 skrll {
300 1.13 skrll static const struct pmap_devmap devmap[] = {
301 1.13 skrll DEVMAP_ENTRY(EXYNOS_CORE_VBASE,
302 1.13 skrll EXYNOS_CORE_PBASE,
303 1.13 skrll EXYNOS5_CORE_SIZE),
304 1.13 skrll DEVMAP_ENTRY(EXYNOS5_AUDIOCORE_VBASE,
305 1.13 skrll EXYNOS5_AUDIOCORE_PBASE,
306 1.13 skrll EXYNOS5_AUDIOCORE_SIZE),
307 1.15 jmcneill DEVMAP_ENTRY(EXYNOS5_SYSRAM_VBASE,
308 1.15 jmcneill EXYNOS5_SYSRAM_PBASE,
309 1.15 jmcneill EXYNOS5_SYSRAM_SIZE),
310 1.13 skrll DEVMAP_ENTRY_END
311 1.13 skrll };
312 1.13 skrll
313 1.13 skrll return devmap;
314 1.13 skrll }
315 1.13 skrll
316 1.18 skrll static void
317 1.18 skrll exynos5_platform_bootstrap(void)
318 1.18 skrll {
319 1.18 skrll
320 1.18 skrll exynos_bootstrap(5);
321 1.18 skrll
322 1.21 jmcneill arm_fdt_cpu_bootstrap();
323 1.18 skrll }
324 1.18 skrll
325 1.1 jmcneill static const struct arm_platform exynos5_platform = {
326 1.13 skrll .ap_devmap = exynos5_platform_devmap,
327 1.18 skrll .ap_bootstrap = exynos5_platform_bootstrap,
328 1.19 skrll .ap_mpstart = exynos_platform_mpstart,
329 1.12 skrll .ap_init_attach_args = exynos_platform_init_attach_args,
330 1.12 skrll .ap_device_register = exynos_platform_device_register,
331 1.12 skrll .ap_reset = exynos5_platform_reset,
332 1.12 skrll .ap_delay = mct_delay,
333 1.12 skrll .ap_uart_freq = exynos_platform_uart_freq,
334 1.1 jmcneill };
335 1.1 jmcneill
336 1.1 jmcneill ARM_PLATFORM(exynos5, "samsung,exynos5", &exynos5_platform);
337 1.13 skrll #endif
338