sunxi_platform.c revision 1.10 1 1.10 jmcneill /* $NetBSD: sunxi_platform.c,v 1.10 2017/10/08 18:00:36 jmcneill Exp $ */
2 1.1 jmcneill
3 1.1 jmcneill /*-
4 1.1 jmcneill * Copyright (c) 2017 Jared 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.1 jmcneill #include "opt_soc.h"
30 1.1 jmcneill #include "opt_multiprocessor.h"
31 1.1 jmcneill #include "opt_fdt_arm.h"
32 1.1 jmcneill
33 1.1 jmcneill #include <sys/cdefs.h>
34 1.10 jmcneill __KERNEL_RCSID(0, "$NetBSD: sunxi_platform.c,v 1.10 2017/10/08 18:00:36 jmcneill Exp $");
35 1.1 jmcneill
36 1.1 jmcneill #include <sys/param.h>
37 1.1 jmcneill #include <sys/bus.h>
38 1.1 jmcneill #include <sys/cpu.h>
39 1.1 jmcneill #include <sys/device.h>
40 1.1 jmcneill #include <sys/termios.h>
41 1.1 jmcneill
42 1.1 jmcneill #include <dev/fdt/fdtvar.h>
43 1.1 jmcneill #include <arm/fdt/arm_fdtvar.h>
44 1.1 jmcneill
45 1.1 jmcneill #include <uvm/uvm_extern.h>
46 1.1 jmcneill
47 1.1 jmcneill #include <machine/bootconfig.h>
48 1.1 jmcneill #include <arm/cpufunc.h>
49 1.1 jmcneill
50 1.1 jmcneill #include <arm/cortex/gtmr_var.h>
51 1.1 jmcneill #include <arm/cortex/gic_reg.h>
52 1.1 jmcneill
53 1.1 jmcneill #include <dev/ic/ns16550reg.h>
54 1.1 jmcneill #include <dev/ic/comreg.h>
55 1.1 jmcneill
56 1.1 jmcneill #include <arm/arm/psci.h>
57 1.1 jmcneill #include <arm/fdt/psci_fdt.h>
58 1.1 jmcneill
59 1.1 jmcneill #include <arm/sunxi/sunxi_platform.h>
60 1.1 jmcneill
61 1.8 jmcneill #include <libfdt.h>
62 1.8 jmcneill
63 1.1 jmcneill #define SUNXI_REF_FREQ 24000000
64 1.1 jmcneill
65 1.7 jmcneill #define SUN4I_TIMER_BASE 0x01c20c00
66 1.7 jmcneill #define SUN4I_TIMER_SIZE 0x90
67 1.7 jmcneill #define SUN4I_TIMER_0_VAL 0x18
68 1.7 jmcneill
69 1.7 jmcneill #define SUN4I_WDT_BASE 0x01c20c90
70 1.7 jmcneill #define SUN4I_WDT_SIZE 0x10
71 1.7 jmcneill #define SUN4I_WDT_CTRL 0x00
72 1.7 jmcneill #define SUN4I_WDT_CTRL_KEY (0x333 << 1)
73 1.7 jmcneill #define SUN4I_WDT_CTRL_RESTART __BIT(0)
74 1.7 jmcneill #define SUN4I_WDT_MODE 0x04
75 1.7 jmcneill #define SUN4I_WDT_MODE_RST_EN __BIT(1)
76 1.7 jmcneill #define SUN4I_WDT_MODE_EN __BIT(0)
77 1.7 jmcneill
78 1.3 jmcneill #define SUN6I_WDT_BASE 0x01c20ca0
79 1.3 jmcneill #define SUN6I_WDT_SIZE 0x20
80 1.3 jmcneill #define SUN6I_WDT_CFG 0x14
81 1.7 jmcneill #define SUN6I_WDT_CFG_SYS __BIT(0)
82 1.3 jmcneill #define SUN6I_WDT_MODE 0x18
83 1.7 jmcneill #define SUN6I_WDT_MODE_EN __BIT(0)
84 1.1 jmcneill
85 1.10 jmcneill #define SUN9I_WDT_BASE 0x06000ca0
86 1.10 jmcneill #define SUN9I_WDT_SIZE 0x20
87 1.10 jmcneill #define SUN9I_WDT_CFG 0x14
88 1.10 jmcneill #define SUN9I_WDT_CFG_SYS __BIT(0)
89 1.10 jmcneill #define SUN9I_WDT_MODE 0x18
90 1.10 jmcneill #define SUN9I_WDT_MODE_EN __BIT(0)
91 1.10 jmcneill
92 1.1 jmcneill
93 1.1 jmcneill #define DEVMAP_ALIGN(a) ((a) & ~L1_S_OFFSET)
94 1.1 jmcneill #define DEVMAP_SIZE(s) roundup2((s), L1_S_SIZE)
95 1.1 jmcneill #define DEVMAP_ENTRY(va, pa, sz) \
96 1.1 jmcneill { \
97 1.1 jmcneill .pd_va = DEVMAP_ALIGN(va), \
98 1.1 jmcneill .pd_pa = DEVMAP_ALIGN(pa), \
99 1.1 jmcneill .pd_size = DEVMAP_SIZE(sz), \
100 1.1 jmcneill .pd_prot = VM_PROT_READ|VM_PROT_WRITE, \
101 1.1 jmcneill .pd_cache = PTE_NOCACHE \
102 1.1 jmcneill }
103 1.1 jmcneill #define DEVMAP_ENTRY_END { 0 }
104 1.1 jmcneill
105 1.1 jmcneill extern struct bus_space armv7_generic_bs_tag;
106 1.1 jmcneill extern struct bus_space armv7_generic_a4x_bs_tag;
107 1.1 jmcneill extern struct arm32_bus_dma_tag armv7_generic_dma_tag;
108 1.1 jmcneill
109 1.1 jmcneill static const struct pmap_devmap *
110 1.1 jmcneill sunxi_platform_devmap(void)
111 1.1 jmcneill {
112 1.1 jmcneill static const struct pmap_devmap devmap[] = {
113 1.1 jmcneill DEVMAP_ENTRY(SUNXI_CORE_VBASE,
114 1.1 jmcneill SUNXI_CORE_PBASE,
115 1.1 jmcneill SUNXI_CORE_SIZE),
116 1.1 jmcneill DEVMAP_ENTRY_END
117 1.1 jmcneill };
118 1.1 jmcneill
119 1.1 jmcneill return devmap;
120 1.1 jmcneill }
121 1.1 jmcneill
122 1.1 jmcneill static void
123 1.1 jmcneill sunxi_platform_init_attach_args(struct fdt_attach_args *faa)
124 1.1 jmcneill {
125 1.1 jmcneill faa->faa_bst = &armv7_generic_bs_tag;
126 1.1 jmcneill faa->faa_a4x_bst = &armv7_generic_a4x_bs_tag;
127 1.1 jmcneill faa->faa_dmat = &armv7_generic_dma_tag;
128 1.1 jmcneill }
129 1.1 jmcneill
130 1.1 jmcneill static void
131 1.1 jmcneill sunxi_platform_early_putchar(char c)
132 1.1 jmcneill {
133 1.1 jmcneill #ifdef CONSADDR
134 1.1 jmcneill #define CONSADDR_VA ((CONSADDR - SUNXI_CORE_PBASE) + SUNXI_CORE_VBASE)
135 1.1 jmcneill volatile uint32_t *uartaddr = (volatile uint32_t *)CONSADDR_VA;
136 1.1 jmcneill
137 1.1 jmcneill while ((uartaddr[com_lsr] & LSR_TXRDY) == 0)
138 1.1 jmcneill ;
139 1.1 jmcneill
140 1.1 jmcneill uartaddr[com_data] = c;
141 1.1 jmcneill #endif
142 1.1 jmcneill }
143 1.1 jmcneill
144 1.1 jmcneill static void
145 1.1 jmcneill sunxi_platform_device_register(device_t self, void *aux)
146 1.1 jmcneill {
147 1.1 jmcneill }
148 1.1 jmcneill
149 1.3 jmcneill static u_int
150 1.3 jmcneill sunxi_platform_uart_freq(void)
151 1.3 jmcneill {
152 1.3 jmcneill return SUNXI_REF_FREQ;
153 1.3 jmcneill }
154 1.3 jmcneill
155 1.1 jmcneill static void
156 1.8 jmcneill sunxi_platform_bootstrap(void)
157 1.8 jmcneill {
158 1.8 jmcneill if (match_bootconf_option(boot_args, "console", "fb")) {
159 1.8 jmcneill void *fdt_data = __UNCONST(fdtbus_get_data());
160 1.8 jmcneill const int chosen_off = fdt_path_offset(fdt_data, "/chosen");
161 1.8 jmcneill const int framebuffer_off =
162 1.8 jmcneill fdt_path_offset(fdt_data, "/chosen/framebuffer");
163 1.8 jmcneill if (chosen_off >= 0 && framebuffer_off >= 0)
164 1.8 jmcneill fdt_setprop_string(fdt_data, chosen_off, "stdout-path",
165 1.8 jmcneill "/chosen/framebuffer");
166 1.8 jmcneill }
167 1.8 jmcneill }
168 1.8 jmcneill
169 1.8 jmcneill static void
170 1.8 jmcneill sunxi_platform_psci_bootstrap(void)
171 1.7 jmcneill {
172 1.8 jmcneill psci_fdt_bootstrap();
173 1.8 jmcneill sunxi_platform_bootstrap();
174 1.7 jmcneill }
175 1.7 jmcneill
176 1.7 jmcneill static void
177 1.7 jmcneill sun4i_platform_reset(void)
178 1.7 jmcneill {
179 1.7 jmcneill bus_space_tag_t bst = &armv7_generic_bs_tag;
180 1.7 jmcneill bus_space_handle_t bsh;
181 1.7 jmcneill
182 1.7 jmcneill bus_space_map(bst, SUN4I_WDT_BASE, SUN4I_WDT_SIZE, 0, &bsh);
183 1.7 jmcneill
184 1.7 jmcneill bus_space_write_4(bst, bsh, SUN4I_WDT_CTRL,
185 1.7 jmcneill SUN4I_WDT_CTRL_KEY | SUN4I_WDT_CTRL_RESTART);
186 1.7 jmcneill for (;;) {
187 1.7 jmcneill bus_space_write_4(bst, bsh, SUN4I_WDT_MODE,
188 1.7 jmcneill SUN4I_WDT_MODE_EN | SUN4I_WDT_MODE_RST_EN);
189 1.7 jmcneill }
190 1.7 jmcneill }
191 1.7 jmcneill
192 1.7 jmcneill static void
193 1.7 jmcneill sun4i_platform_delay(u_int n)
194 1.7 jmcneill {
195 1.7 jmcneill static bus_space_tag_t bst = &armv7_generic_bs_tag;
196 1.7 jmcneill static bus_space_handle_t bsh = 0;
197 1.7 jmcneill uint32_t cur, prev;
198 1.7 jmcneill long ticks = n;
199 1.7 jmcneill
200 1.7 jmcneill if (bsh == 0)
201 1.7 jmcneill bus_space_map(bst, SUN4I_TIMER_BASE, SUN4I_TIMER_SIZE, 0, &bsh);
202 1.7 jmcneill
203 1.7 jmcneill prev = ~bus_space_read_4(bst, bsh, SUN4I_TIMER_0_VAL);
204 1.7 jmcneill while (ticks > 0) {
205 1.7 jmcneill cur = ~bus_space_read_4(bst, bsh, SUN4I_TIMER_0_VAL);
206 1.7 jmcneill if (cur > prev)
207 1.7 jmcneill ticks -= (cur - prev);
208 1.7 jmcneill else
209 1.7 jmcneill ticks -= (~0U - cur + prev);
210 1.7 jmcneill prev = cur;
211 1.7 jmcneill }
212 1.7 jmcneill }
213 1.7 jmcneill
214 1.7 jmcneill static void
215 1.3 jmcneill sun6i_platform_reset(void)
216 1.1 jmcneill {
217 1.1 jmcneill bus_space_tag_t bst = &armv7_generic_bs_tag;
218 1.1 jmcneill bus_space_handle_t bsh;
219 1.1 jmcneill
220 1.3 jmcneill bus_space_map(bst, SUN6I_WDT_BASE, SUN6I_WDT_SIZE, 0, &bsh);
221 1.1 jmcneill
222 1.3 jmcneill bus_space_write_4(bst, bsh, SUN6I_WDT_CFG, SUN6I_WDT_CFG_SYS);
223 1.3 jmcneill bus_space_write_4(bst, bsh, SUN6I_WDT_MODE, SUN6I_WDT_MODE_EN);
224 1.1 jmcneill }
225 1.1 jmcneill
226 1.10 jmcneill static void
227 1.10 jmcneill sun9i_platform_reset(void)
228 1.10 jmcneill {
229 1.10 jmcneill bus_space_tag_t bst = &armv7_generic_bs_tag;
230 1.10 jmcneill bus_space_handle_t bsh;
231 1.10 jmcneill
232 1.10 jmcneill bus_space_map(bst, SUN9I_WDT_BASE, SUN9I_WDT_SIZE, 0, &bsh);
233 1.10 jmcneill
234 1.10 jmcneill bus_space_write_4(bst, bsh, SUN9I_WDT_CFG, SUN9I_WDT_CFG_SYS);
235 1.10 jmcneill bus_space_write_4(bst, bsh, SUN9I_WDT_MODE, SUN9I_WDT_MODE_EN);
236 1.10 jmcneill }
237 1.10 jmcneill
238 1.9 jmcneill static const struct arm_platform sun4i_platform = {
239 1.9 jmcneill .devmap = sunxi_platform_devmap,
240 1.9 jmcneill .bootstrap = sunxi_platform_bootstrap,
241 1.9 jmcneill .init_attach_args = sunxi_platform_init_attach_args,
242 1.9 jmcneill .early_putchar = sunxi_platform_early_putchar,
243 1.9 jmcneill .device_register = sunxi_platform_device_register,
244 1.9 jmcneill .reset = sun4i_platform_reset,
245 1.9 jmcneill .delay = sun4i_platform_delay,
246 1.9 jmcneill .uart_freq = sunxi_platform_uart_freq,
247 1.9 jmcneill };
248 1.9 jmcneill
249 1.9 jmcneill ARM_PLATFORM(sun4i_a10, "allwinner,sun4i-a10", &sun4i_platform);
250 1.9 jmcneill
251 1.7 jmcneill static const struct arm_platform sun5i_platform = {
252 1.7 jmcneill .devmap = sunxi_platform_devmap,
253 1.8 jmcneill .bootstrap = sunxi_platform_bootstrap,
254 1.7 jmcneill .init_attach_args = sunxi_platform_init_attach_args,
255 1.7 jmcneill .early_putchar = sunxi_platform_early_putchar,
256 1.7 jmcneill .device_register = sunxi_platform_device_register,
257 1.7 jmcneill .reset = sun4i_platform_reset,
258 1.7 jmcneill .delay = sun4i_platform_delay,
259 1.7 jmcneill .uart_freq = sunxi_platform_uart_freq,
260 1.7 jmcneill };
261 1.7 jmcneill
262 1.7 jmcneill ARM_PLATFORM(sun5i_a13, "allwinner,sun5i-a13", &sun5i_platform);
263 1.6 jmcneill
264 1.6 jmcneill static const struct arm_platform sun6i_platform = {
265 1.6 jmcneill .devmap = sunxi_platform_devmap,
266 1.8 jmcneill .bootstrap = sunxi_platform_psci_bootstrap,
267 1.6 jmcneill .init_attach_args = sunxi_platform_init_attach_args,
268 1.6 jmcneill .early_putchar = sunxi_platform_early_putchar,
269 1.6 jmcneill .device_register = sunxi_platform_device_register,
270 1.6 jmcneill .reset = sun6i_platform_reset,
271 1.6 jmcneill .delay = gtmr_delay,
272 1.6 jmcneill .uart_freq = sunxi_platform_uart_freq,
273 1.6 jmcneill };
274 1.6 jmcneill
275 1.6 jmcneill ARM_PLATFORM(sun6i_a31, "allwinner,sun6i-a31", &sun6i_platform);
276 1.6 jmcneill
277 1.9 jmcneill static const struct arm_platform sun7i_platform = {
278 1.9 jmcneill .devmap = sunxi_platform_devmap,
279 1.9 jmcneill .bootstrap = sunxi_platform_psci_bootstrap,
280 1.9 jmcneill .init_attach_args = sunxi_platform_init_attach_args,
281 1.9 jmcneill .early_putchar = sunxi_platform_early_putchar,
282 1.9 jmcneill .device_register = sunxi_platform_device_register,
283 1.9 jmcneill .reset = sun4i_platform_reset,
284 1.9 jmcneill .delay = sun4i_platform_delay,
285 1.9 jmcneill .uart_freq = sunxi_platform_uart_freq,
286 1.9 jmcneill };
287 1.9 jmcneill
288 1.9 jmcneill ARM_PLATFORM(sun7i_a20, "allwinner,sun7i-a20", &sun7i_platform);
289 1.9 jmcneill
290 1.3 jmcneill static const struct arm_platform sun8i_platform = {
291 1.3 jmcneill .devmap = sunxi_platform_devmap,
292 1.8 jmcneill .bootstrap = sunxi_platform_psci_bootstrap,
293 1.3 jmcneill .init_attach_args = sunxi_platform_init_attach_args,
294 1.3 jmcneill .early_putchar = sunxi_platform_early_putchar,
295 1.3 jmcneill .device_register = sunxi_platform_device_register,
296 1.3 jmcneill .reset = sun6i_platform_reset,
297 1.3 jmcneill .delay = gtmr_delay,
298 1.3 jmcneill .uart_freq = sunxi_platform_uart_freq,
299 1.3 jmcneill };
300 1.3 jmcneill
301 1.5 jmcneill ARM_PLATFORM(sun8i_h2plus, "allwinner,sun8i-h2-plus", &sun8i_platform);
302 1.3 jmcneill ARM_PLATFORM(sun8i_h3, "allwinner,sun8i-h3", &sun8i_platform);
303 1.4 jmcneill ARM_PLATFORM(sun8i_a83t, "allwinner,sun8i-a83t", &sun8i_platform);
304 1.1 jmcneill
305 1.10 jmcneill static const struct arm_platform sun9i_platform = {
306 1.10 jmcneill .devmap = sunxi_platform_devmap,
307 1.10 jmcneill .bootstrap = sunxi_platform_bootstrap,
308 1.10 jmcneill .init_attach_args = sunxi_platform_init_attach_args,
309 1.10 jmcneill .early_putchar = sunxi_platform_early_putchar,
310 1.10 jmcneill .device_register = sunxi_platform_device_register,
311 1.10 jmcneill .reset = sun9i_platform_reset,
312 1.10 jmcneill .delay = gtmr_delay,
313 1.10 jmcneill .uart_freq = sunxi_platform_uart_freq,
314 1.10 jmcneill };
315 1.10 jmcneill
316 1.10 jmcneill ARM_PLATFORM(sun9i_a80, "allwinner,sun9i-a80", &sun9i_platform);
317 1.10 jmcneill
318 1.6 jmcneill static const struct arm_platform sun50i_platform = {
319 1.1 jmcneill .devmap = sunxi_platform_devmap,
320 1.8 jmcneill .bootstrap = sunxi_platform_bootstrap,
321 1.1 jmcneill .init_attach_args = sunxi_platform_init_attach_args,
322 1.1 jmcneill .early_putchar = sunxi_platform_early_putchar,
323 1.1 jmcneill .device_register = sunxi_platform_device_register,
324 1.3 jmcneill .reset = sun6i_platform_reset,
325 1.1 jmcneill .delay = gtmr_delay,
326 1.1 jmcneill .uart_freq = sunxi_platform_uart_freq,
327 1.1 jmcneill };
328 1.1 jmcneill
329 1.6 jmcneill ARM_PLATFORM(sun50i_a64, "allwinner,sun50i-a64", &sun50i_platform);
330