arm_fdt.c revision 1.11 1 1.11 jmcneill /* $NetBSD: arm_fdt.c,v 1.11 2020/06/21 17:25:03 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.5 jmcneill #include "opt_arm_timer.h"
30 1.11 jmcneill #include "opt_modular.h"
31 1.5 jmcneill
32 1.1 jmcneill #include <sys/cdefs.h>
33 1.11 jmcneill __KERNEL_RCSID(0, "$NetBSD: arm_fdt.c,v 1.11 2020/06/21 17:25:03 jmcneill Exp $");
34 1.1 jmcneill
35 1.1 jmcneill #include <sys/param.h>
36 1.1 jmcneill #include <sys/systm.h>
37 1.7 skrll #include <sys/cpu.h>
38 1.1 jmcneill #include <sys/device.h>
39 1.2 jmcneill #include <sys/kmem.h>
40 1.2 jmcneill #include <sys/bus.h>
41 1.11 jmcneill #include <sys/module.h>
42 1.11 jmcneill
43 1.11 jmcneill #include <uvm/uvm_extern.h>
44 1.1 jmcneill
45 1.1 jmcneill #include <dev/fdt/fdtvar.h>
46 1.1 jmcneill #include <dev/ofw/openfirm.h>
47 1.1 jmcneill
48 1.1 jmcneill #include <arm/fdt/arm_fdtvar.h>
49 1.1 jmcneill
50 1.1 jmcneill static int arm_fdt_match(device_t, cfdata_t, void *);
51 1.1 jmcneill static void arm_fdt_attach(device_t, device_t, void *);
52 1.1 jmcneill
53 1.1 jmcneill CFATTACH_DECL_NEW(arm_fdt, 0,
54 1.1 jmcneill arm_fdt_match, arm_fdt_attach, NULL, NULL);
55 1.1 jmcneill
56 1.2 jmcneill struct arm_fdt_cpu_hatch_cb {
57 1.2 jmcneill TAILQ_ENTRY(arm_fdt_cpu_hatch_cb) next;
58 1.2 jmcneill void (*cb)(void *, struct cpu_info *);
59 1.2 jmcneill void *priv;
60 1.2 jmcneill };
61 1.2 jmcneill
62 1.2 jmcneill static TAILQ_HEAD(, arm_fdt_cpu_hatch_cb) arm_fdt_cpu_hatch_cbs =
63 1.2 jmcneill TAILQ_HEAD_INITIALIZER(arm_fdt_cpu_hatch_cbs);
64 1.2 jmcneill
65 1.3 jmcneill static void (*_arm_fdt_irq_handler)(void *) = NULL;
66 1.5 jmcneill static void (*_arm_fdt_timer_init)(void) = NULL;
67 1.3 jmcneill
68 1.1 jmcneill int
69 1.1 jmcneill arm_fdt_match(device_t parent, cfdata_t cf, void *aux)
70 1.1 jmcneill {
71 1.1 jmcneill return 1;
72 1.1 jmcneill }
73 1.1 jmcneill
74 1.1 jmcneill void
75 1.1 jmcneill arm_fdt_attach(device_t parent, device_t self, void *aux)
76 1.1 jmcneill {
77 1.1 jmcneill const struct arm_platform *plat = arm_fdt_platform();
78 1.1 jmcneill struct fdt_attach_args faa;
79 1.1 jmcneill
80 1.1 jmcneill aprint_naive("\n");
81 1.1 jmcneill aprint_normal("\n");
82 1.1 jmcneill
83 1.8 skrll plat->ap_init_attach_args(&faa);
84 1.1 jmcneill faa.faa_name = "";
85 1.1 jmcneill faa.faa_phandle = OF_peer(0);
86 1.1 jmcneill
87 1.1 jmcneill config_found(self, &faa, NULL);
88 1.1 jmcneill }
89 1.1 jmcneill
90 1.1 jmcneill const struct arm_platform *
91 1.1 jmcneill arm_fdt_platform(void)
92 1.1 jmcneill {
93 1.1 jmcneill static const struct arm_platform_info *booted_platform = NULL;
94 1.10 jmcneill __link_set_decl(arm_platforms, struct arm_platform_info);
95 1.10 jmcneill struct arm_platform_info * const *info;
96 1.1 jmcneill
97 1.1 jmcneill if (booted_platform == NULL) {
98 1.1 jmcneill const struct arm_platform_info *best_info = NULL;
99 1.1 jmcneill const int phandle = OF_peer(0);
100 1.1 jmcneill int match, best_match = 0;
101 1.1 jmcneill
102 1.1 jmcneill __link_set_foreach(info, arm_platforms) {
103 1.8 skrll const char * const compat[] = { (*info)->api_compat, NULL };
104 1.1 jmcneill match = of_match_compatible(phandle, compat);
105 1.1 jmcneill if (match > best_match) {
106 1.1 jmcneill best_match = match;
107 1.1 jmcneill best_info = *info;
108 1.1 jmcneill }
109 1.1 jmcneill }
110 1.1 jmcneill
111 1.1 jmcneill booted_platform = best_info;
112 1.1 jmcneill }
113 1.1 jmcneill
114 1.10 jmcneill /*
115 1.10 jmcneill * No SoC specific platform was found. Try to find a generic
116 1.10 jmcneill * platform definition and use that if available.
117 1.10 jmcneill */
118 1.10 jmcneill if (booted_platform == NULL) {
119 1.10 jmcneill __link_set_foreach(info, arm_platforms) {
120 1.10 jmcneill if (strcmp((*info)->api_compat, ARM_PLATFORM_DEFAULT) == 0) {
121 1.10 jmcneill booted_platform = *info;
122 1.10 jmcneill break;
123 1.10 jmcneill }
124 1.10 jmcneill }
125 1.10 jmcneill }
126 1.10 jmcneill
127 1.8 skrll return booted_platform == NULL ? NULL : booted_platform->api_ops;
128 1.1 jmcneill }
129 1.2 jmcneill
130 1.2 jmcneill void
131 1.2 jmcneill arm_fdt_cpu_hatch_register(void *priv, void (*cb)(void *, struct cpu_info *))
132 1.2 jmcneill {
133 1.2 jmcneill struct arm_fdt_cpu_hatch_cb *c;
134 1.2 jmcneill
135 1.2 jmcneill c = kmem_alloc(sizeof(*c), KM_SLEEP);
136 1.2 jmcneill c->priv = priv;
137 1.2 jmcneill c->cb = cb;
138 1.2 jmcneill TAILQ_INSERT_TAIL(&arm_fdt_cpu_hatch_cbs, c, next);
139 1.2 jmcneill }
140 1.2 jmcneill
141 1.2 jmcneill void
142 1.2 jmcneill arm_fdt_cpu_hatch(struct cpu_info *ci)
143 1.2 jmcneill {
144 1.2 jmcneill struct arm_fdt_cpu_hatch_cb *c;
145 1.2 jmcneill
146 1.2 jmcneill TAILQ_FOREACH(c, &arm_fdt_cpu_hatch_cbs, next)
147 1.2 jmcneill c->cb(c->priv, ci);
148 1.2 jmcneill }
149 1.3 jmcneill
150 1.3 jmcneill void
151 1.3 jmcneill arm_fdt_irq_set_handler(void (*irq_handler)(void *))
152 1.3 jmcneill {
153 1.3 jmcneill KASSERT(_arm_fdt_irq_handler == NULL);
154 1.3 jmcneill _arm_fdt_irq_handler = irq_handler;
155 1.3 jmcneill }
156 1.3 jmcneill
157 1.3 jmcneill void
158 1.3 jmcneill arm_fdt_irq_handler(void *tf)
159 1.3 jmcneill {
160 1.3 jmcneill _arm_fdt_irq_handler(tf);
161 1.3 jmcneill }
162 1.4 jmcneill
163 1.4 jmcneill void
164 1.5 jmcneill arm_fdt_timer_register(void (*timerfn)(void))
165 1.5 jmcneill {
166 1.5 jmcneill if (_arm_fdt_timer_init != NULL) {
167 1.5 jmcneill #ifdef DIAGNOSTIC
168 1.5 jmcneill aprint_verbose("%s: timer already registered\n", __func__);
169 1.5 jmcneill #endif
170 1.5 jmcneill return;
171 1.5 jmcneill }
172 1.5 jmcneill _arm_fdt_timer_init = timerfn;
173 1.5 jmcneill }
174 1.5 jmcneill
175 1.5 jmcneill void
176 1.4 jmcneill arm_fdt_memory_dump(paddr_t pa)
177 1.4 jmcneill {
178 1.4 jmcneill const struct arm_platform *plat = arm_fdt_platform();
179 1.4 jmcneill struct fdt_attach_args faa;
180 1.4 jmcneill bus_space_tag_t bst;
181 1.4 jmcneill bus_space_handle_t bsh;
182 1.4 jmcneill
183 1.8 skrll plat->ap_init_attach_args(&faa);
184 1.4 jmcneill
185 1.4 jmcneill bst = faa.faa_bst;
186 1.4 jmcneill bus_space_map(bst, pa, 0x100, 0, &bsh);
187 1.4 jmcneill
188 1.4 jmcneill for (int i = 0; i < 0x100; i += 0x10) {
189 1.6 skrll printf("%" PRIxPTR ": %08x %08x %08x %08x\n",
190 1.6 skrll (uintptr_t)(pa + i),
191 1.4 jmcneill bus_space_read_4(bst, bsh, i + 0),
192 1.4 jmcneill bus_space_read_4(bst, bsh, i + 4),
193 1.4 jmcneill bus_space_read_4(bst, bsh, i + 8),
194 1.4 jmcneill bus_space_read_4(bst, bsh, i + 12));
195 1.4 jmcneill }
196 1.4 jmcneill }
197 1.5 jmcneill
198 1.5 jmcneill #ifdef __HAVE_GENERIC_CPU_INITCLOCKS
199 1.5 jmcneill void
200 1.5 jmcneill cpu_initclocks(void)
201 1.5 jmcneill {
202 1.5 jmcneill if (_arm_fdt_timer_init == NULL)
203 1.5 jmcneill panic("cpu_initclocks: no timer registered");
204 1.5 jmcneill _arm_fdt_timer_init();
205 1.5 jmcneill }
206 1.5 jmcneill #endif
207 1.11 jmcneill
208 1.11 jmcneill void
209 1.11 jmcneill arm_fdt_module_init(void)
210 1.11 jmcneill {
211 1.11 jmcneill #ifdef MODULAR
212 1.11 jmcneill const int chosen = OF_finddevice("/chosen");
213 1.11 jmcneill const char *module_name;
214 1.11 jmcneill const uint64_t *data;
215 1.11 jmcneill u_int index;
216 1.11 jmcneill paddr_t pa;
217 1.11 jmcneill vaddr_t va;
218 1.11 jmcneill int len;
219 1.11 jmcneill
220 1.11 jmcneill if (chosen == -1)
221 1.11 jmcneill return;
222 1.11 jmcneill
223 1.11 jmcneill data = fdtbus_get_prop(chosen, "netbsd,modules", &len);
224 1.11 jmcneill if (data == NULL)
225 1.11 jmcneill return;
226 1.11 jmcneill
227 1.11 jmcneill for (index = 0; index < len / 16; index++, data += 2) {
228 1.11 jmcneill module_name = fdtbus_get_string_index(chosen,
229 1.11 jmcneill "netbsd,module-names", index);
230 1.11 jmcneill if (module_name == NULL)
231 1.11 jmcneill break;
232 1.11 jmcneill
233 1.11 jmcneill const paddr_t startpa = (paddr_t)be64dec(data + 0);
234 1.11 jmcneill const size_t size = (size_t)be64dec(data + 1);
235 1.11 jmcneill const paddr_t endpa = round_page(startpa + size);
236 1.11 jmcneill
237 1.11 jmcneill const vaddr_t startva = uvm_km_alloc(kernel_map, endpa - startpa,
238 1.11 jmcneill 0, UVM_KMF_VAONLY | UVM_KMF_NOWAIT);
239 1.11 jmcneill if (startva == 0) {
240 1.11 jmcneill printf("ERROR: Cannot allocate VA for module %s\n",
241 1.11 jmcneill module_name);
242 1.11 jmcneill continue;
243 1.11 jmcneill }
244 1.11 jmcneill
245 1.11 jmcneill for (pa = startpa, va = startva;
246 1.11 jmcneill pa < endpa;
247 1.11 jmcneill pa += PAGE_SIZE, va += PAGE_SIZE) {
248 1.11 jmcneill pmap_kenter_pa(va, pa, VM_PROT_ALL, PMAP_WRITE_BACK);
249 1.11 jmcneill }
250 1.11 jmcneill pmap_update(pmap_kernel());
251 1.11 jmcneill
252 1.11 jmcneill module_prime(module_name, (void *)(uintptr_t)startva, size);
253 1.11 jmcneill }
254 1.11 jmcneill #endif /* !MODULAR */
255 1.11 jmcneill }
256