rk_platform.c revision 1.1.2.5 1 1.1.2.5 pgoyette /* $NetBSD: rk_platform.c,v 1.1.2.5 2018/10/20 06:58:26 pgoyette Exp $ */
2 1.1.2.2 pgoyette
3 1.1.2.2 pgoyette /*-
4 1.1.2.2 pgoyette * Copyright (c) 2018 Jared McNeill <jmcneill (at) invisible.ca>
5 1.1.2.2 pgoyette * All rights reserved.
6 1.1.2.2 pgoyette *
7 1.1.2.2 pgoyette * Redistribution and use in source and binary forms, with or without
8 1.1.2.2 pgoyette * modification, are permitted provided that the following conditions
9 1.1.2.2 pgoyette * are met:
10 1.1.2.2 pgoyette * 1. Redistributions of source code must retain the above copyright
11 1.1.2.2 pgoyette * notice, this list of conditions and the following disclaimer.
12 1.1.2.2 pgoyette * 2. Redistributions in binary form must reproduce the above copyright
13 1.1.2.2 pgoyette * notice, this list of conditions and the following disclaimer in the
14 1.1.2.2 pgoyette * documentation and/or other materials provided with the distribution.
15 1.1.2.2 pgoyette *
16 1.1.2.2 pgoyette * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.1.2.2 pgoyette * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.1.2.2 pgoyette * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.1.2.2 pgoyette * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.1.2.2 pgoyette * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 1.1.2.2 pgoyette * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 1.1.2.2 pgoyette * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 1.1.2.2 pgoyette * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 1.1.2.2 pgoyette * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1.2.2 pgoyette * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1.2.2 pgoyette * SUCH DAMAGE.
27 1.1.2.2 pgoyette */
28 1.1.2.2 pgoyette
29 1.1.2.2 pgoyette #include "opt_soc.h"
30 1.1.2.2 pgoyette #include "opt_multiprocessor.h"
31 1.1.2.4 pgoyette #include "opt_console.h"
32 1.1.2.2 pgoyette
33 1.1.2.2 pgoyette #include <sys/cdefs.h>
34 1.1.2.5 pgoyette __KERNEL_RCSID(0, "$NetBSD: rk_platform.c,v 1.1.2.5 2018/10/20 06:58:26 pgoyette Exp $");
35 1.1.2.2 pgoyette
36 1.1.2.2 pgoyette #include <sys/param.h>
37 1.1.2.2 pgoyette #include <sys/bus.h>
38 1.1.2.2 pgoyette #include <sys/cpu.h>
39 1.1.2.2 pgoyette #include <sys/device.h>
40 1.1.2.2 pgoyette #include <sys/termios.h>
41 1.1.2.2 pgoyette
42 1.1.2.2 pgoyette #include <dev/fdt/fdtvar.h>
43 1.1.2.2 pgoyette #include <arm/fdt/arm_fdtvar.h>
44 1.1.2.2 pgoyette
45 1.1.2.2 pgoyette #include <uvm/uvm_extern.h>
46 1.1.2.2 pgoyette
47 1.1.2.2 pgoyette #include <machine/bootconfig.h>
48 1.1.2.2 pgoyette #include <arm/cpufunc.h>
49 1.1.2.2 pgoyette
50 1.1.2.2 pgoyette #include <arm/cortex/gtmr_var.h>
51 1.1.2.2 pgoyette
52 1.1.2.2 pgoyette #include <dev/ic/ns16550reg.h>
53 1.1.2.2 pgoyette #include <dev/ic/comreg.h>
54 1.1.2.2 pgoyette
55 1.1.2.2 pgoyette #include <arm/arm/psci.h>
56 1.1.2.4 pgoyette #include <arm/fdt/psci_fdtvar.h>
57 1.1.2.2 pgoyette
58 1.1.2.2 pgoyette #include <libfdt.h>
59 1.1.2.2 pgoyette
60 1.1.2.2 pgoyette extern struct arm32_bus_dma_tag arm_generic_dma_tag;
61 1.1.2.2 pgoyette extern struct bus_space arm_generic_bs_tag;
62 1.1.2.2 pgoyette extern struct bus_space arm_generic_a4x_bs_tag;
63 1.1.2.2 pgoyette
64 1.1.2.2 pgoyette static void
65 1.1.2.2 pgoyette rk_platform_init_attach_args(struct fdt_attach_args *faa)
66 1.1.2.2 pgoyette {
67 1.1.2.2 pgoyette faa->faa_bst = &arm_generic_bs_tag;
68 1.1.2.2 pgoyette faa->faa_a4x_bst = &arm_generic_a4x_bs_tag;
69 1.1.2.2 pgoyette faa->faa_dmat = &arm_generic_dma_tag;
70 1.1.2.2 pgoyette }
71 1.1.2.2 pgoyette
72 1.1.2.2 pgoyette static void
73 1.1.2.2 pgoyette rk_platform_device_register(device_t self, void *aux)
74 1.1.2.2 pgoyette {
75 1.1.2.2 pgoyette }
76 1.1.2.2 pgoyette
77 1.1.2.2 pgoyette static void
78 1.1.2.2 pgoyette rk_platform_bootstrap(void)
79 1.1.2.2 pgoyette {
80 1.1.2.2 pgoyette void *fdt_data = __UNCONST(fdtbus_get_data());
81 1.1.2.2 pgoyette
82 1.1.2.4 pgoyette arm_fdt_cpu_bootstrap();
83 1.1.2.2 pgoyette
84 1.1.2.2 pgoyette const int chosen_off = fdt_path_offset(fdt_data, "/chosen");
85 1.1.2.2 pgoyette if (chosen_off < 0)
86 1.1.2.2 pgoyette return;
87 1.1.2.2 pgoyette
88 1.1.2.2 pgoyette if (match_bootconf_option(boot_args, "console", "fb")) {
89 1.1.2.2 pgoyette const int framebuffer_off =
90 1.1.2.2 pgoyette fdt_path_offset(fdt_data, "/chosen/framebuffer");
91 1.1.2.2 pgoyette if (framebuffer_off >= 0) {
92 1.1.2.2 pgoyette const char *status = fdt_getprop(fdt_data,
93 1.1.2.2 pgoyette framebuffer_off, "status", NULL);
94 1.1.2.2 pgoyette if (status == NULL || strncmp(status, "ok", 2) == 0) {
95 1.1.2.2 pgoyette fdt_setprop_string(fdt_data, chosen_off,
96 1.1.2.2 pgoyette "stdout-path", "/chosen/framebuffer");
97 1.1.2.2 pgoyette }
98 1.1.2.2 pgoyette }
99 1.1.2.2 pgoyette } else if (match_bootconf_option(boot_args, "console", "serial")) {
100 1.1.2.2 pgoyette fdt_setprop_string(fdt_data, chosen_off,
101 1.1.2.2 pgoyette "stdout-path", "serial0:115200n8");
102 1.1.2.2 pgoyette }
103 1.1.2.2 pgoyette }
104 1.1.2.2 pgoyette
105 1.1.2.2 pgoyette #ifdef SOC_RK3328
106 1.1.2.2 pgoyette
107 1.1.2.2 pgoyette #include <arm/rockchip/rk3328_platform.h>
108 1.1.2.2 pgoyette
109 1.1.2.2 pgoyette static const struct pmap_devmap *
110 1.1.2.2 pgoyette rk3328_platform_devmap(void)
111 1.1.2.2 pgoyette {
112 1.1.2.2 pgoyette static const struct pmap_devmap devmap[] = {
113 1.1.2.2 pgoyette DEVMAP_ENTRY(RK3328_CORE_VBASE,
114 1.1.2.2 pgoyette RK3328_CORE_PBASE,
115 1.1.2.2 pgoyette RK3328_CORE_SIZE),
116 1.1.2.2 pgoyette DEVMAP_ENTRY_END
117 1.1.2.2 pgoyette };
118 1.1.2.2 pgoyette
119 1.1.2.2 pgoyette return devmap;
120 1.1.2.2 pgoyette }
121 1.1.2.2 pgoyette
122 1.1.2.2 pgoyette void rk3328_platform_early_putchar(char);
123 1.1.2.2 pgoyette
124 1.1.2.2 pgoyette void
125 1.1.2.2 pgoyette rk3328_platform_early_putchar(char c)
126 1.1.2.2 pgoyette {
127 1.1.2.2 pgoyette #ifdef CONSADDR
128 1.1.2.2 pgoyette #define CONSADDR_VA ((CONSADDR - RK3328_CORE_PBASE) + RK3328_CORE_VBASE)
129 1.1.2.2 pgoyette volatile uint32_t *uartaddr = cpu_earlydevice_va_p() ?
130 1.1.2.2 pgoyette (volatile uint32_t *)CONSADDR_VA :
131 1.1.2.2 pgoyette (volatile uint32_t *)CONSADDR;
132 1.1.2.2 pgoyette
133 1.1.2.2 pgoyette while ((le32toh(uartaddr[com_lsr]) & LSR_TXRDY) == 0)
134 1.1.2.2 pgoyette ;
135 1.1.2.2 pgoyette
136 1.1.2.2 pgoyette uartaddr[com_data] = htole32(c);
137 1.1.2.2 pgoyette #undef CONSADDR_VA
138 1.1.2.2 pgoyette #endif
139 1.1.2.2 pgoyette }
140 1.1.2.2 pgoyette
141 1.1.2.2 pgoyette static u_int
142 1.1.2.2 pgoyette rk3328_platform_uart_freq(void)
143 1.1.2.2 pgoyette {
144 1.1.2.2 pgoyette return RK3328_UART_FREQ;
145 1.1.2.2 pgoyette }
146 1.1.2.2 pgoyette
147 1.1.2.2 pgoyette static const struct arm_platform rk3328_platform = {
148 1.1.2.3 pgoyette .ap_devmap = rk3328_platform_devmap,
149 1.1.2.3 pgoyette .ap_bootstrap = rk_platform_bootstrap,
150 1.1.2.3 pgoyette .ap_init_attach_args = rk_platform_init_attach_args,
151 1.1.2.3 pgoyette .ap_early_putchar = rk3328_platform_early_putchar,
152 1.1.2.3 pgoyette .ap_device_register = rk_platform_device_register,
153 1.1.2.3 pgoyette .ap_reset = psci_fdt_reset,
154 1.1.2.3 pgoyette .ap_delay = gtmr_delay,
155 1.1.2.3 pgoyette .ap_uart_freq = rk3328_platform_uart_freq,
156 1.1.2.5 pgoyette .ap_mpstart = arm_fdt_cpu_mpstart,
157 1.1.2.2 pgoyette };
158 1.1.2.2 pgoyette
159 1.1.2.2 pgoyette ARM_PLATFORM(rk3328, "rockchip,rk3328", &rk3328_platform);
160 1.1.2.2 pgoyette
161 1.1.2.2 pgoyette #endif /* SOC_RK3328 */
162 1.1.2.3 pgoyette
163 1.1.2.3 pgoyette
164 1.1.2.3 pgoyette #ifdef SOC_RK3399
165 1.1.2.3 pgoyette
166 1.1.2.3 pgoyette #include <arm/rockchip/rk3399_platform.h>
167 1.1.2.3 pgoyette
168 1.1.2.3 pgoyette static const struct pmap_devmap *
169 1.1.2.3 pgoyette rk3399_platform_devmap(void)
170 1.1.2.3 pgoyette {
171 1.1.2.3 pgoyette static const struct pmap_devmap devmap[] = {
172 1.1.2.3 pgoyette DEVMAP_ENTRY(RK3399_CORE_VBASE,
173 1.1.2.3 pgoyette RK3399_CORE_PBASE,
174 1.1.2.3 pgoyette RK3399_CORE_SIZE),
175 1.1.2.3 pgoyette DEVMAP_ENTRY_END
176 1.1.2.3 pgoyette };
177 1.1.2.3 pgoyette
178 1.1.2.3 pgoyette return devmap;
179 1.1.2.3 pgoyette }
180 1.1.2.3 pgoyette
181 1.1.2.3 pgoyette void rk3399_platform_early_putchar(char);
182 1.1.2.3 pgoyette
183 1.1.2.3 pgoyette void
184 1.1.2.3 pgoyette rk3399_platform_early_putchar(char c)
185 1.1.2.3 pgoyette {
186 1.1.2.3 pgoyette #ifdef CONSADDR
187 1.1.2.3 pgoyette #define CONSADDR_VA ((CONSADDR - RK3399_CORE_PBASE) + RK3399_CORE_VBASE)
188 1.1.2.3 pgoyette volatile uint32_t *uartaddr = cpu_earlydevice_va_p() ?
189 1.1.2.3 pgoyette (volatile uint32_t *)CONSADDR_VA :
190 1.1.2.3 pgoyette (volatile uint32_t *)CONSADDR;
191 1.1.2.3 pgoyette
192 1.1.2.3 pgoyette while ((le32toh(uartaddr[com_lsr]) & LSR_TXRDY) == 0)
193 1.1.2.3 pgoyette ;
194 1.1.2.3 pgoyette
195 1.1.2.3 pgoyette uartaddr[com_data] = htole32(c);
196 1.1.2.3 pgoyette #undef CONSADDR_VA
197 1.1.2.3 pgoyette #endif
198 1.1.2.3 pgoyette }
199 1.1.2.3 pgoyette
200 1.1.2.3 pgoyette static u_int
201 1.1.2.3 pgoyette rk3399_platform_uart_freq(void)
202 1.1.2.3 pgoyette {
203 1.1.2.3 pgoyette return RK3399_UART_FREQ;
204 1.1.2.3 pgoyette }
205 1.1.2.3 pgoyette
206 1.1.2.3 pgoyette static const struct arm_platform rk3399_platform = {
207 1.1.2.3 pgoyette .ap_devmap = rk3399_platform_devmap,
208 1.1.2.3 pgoyette .ap_bootstrap = rk_platform_bootstrap,
209 1.1.2.3 pgoyette .ap_init_attach_args = rk_platform_init_attach_args,
210 1.1.2.3 pgoyette .ap_early_putchar = rk3399_platform_early_putchar,
211 1.1.2.3 pgoyette .ap_device_register = rk_platform_device_register,
212 1.1.2.3 pgoyette .ap_reset = psci_fdt_reset,
213 1.1.2.3 pgoyette .ap_delay = gtmr_delay,
214 1.1.2.3 pgoyette .ap_uart_freq = rk3399_platform_uart_freq,
215 1.1.2.3 pgoyette };
216 1.1.2.3 pgoyette
217 1.1.2.3 pgoyette ARM_PLATFORM(rk3399, "rockchip,rk3399", &rk3399_platform);
218 1.1.2.3 pgoyette
219 1.1.2.3 pgoyette #endif /* SOC_RK3399 */
220