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