arm_fdt.c revision 1.5.2.2 1 1.5.2.2 skrll /* $NetBSD: arm_fdt.c,v 1.5.2.2 2017/08/28 17:51:30 skrll Exp $ */
2 1.5.2.2 skrll
3 1.5.2.2 skrll /*-
4 1.5.2.2 skrll * Copyright (c) 2017 Jared D. McNeill <jmcneill (at) invisible.ca>
5 1.5.2.2 skrll * All rights reserved.
6 1.5.2.2 skrll *
7 1.5.2.2 skrll * Redistribution and use in source and binary forms, with or without
8 1.5.2.2 skrll * modification, are permitted provided that the following conditions
9 1.5.2.2 skrll * are met:
10 1.5.2.2 skrll * 1. Redistributions of source code must retain the above copyright
11 1.5.2.2 skrll * notice, this list of conditions and the following disclaimer.
12 1.5.2.2 skrll * 2. Redistributions in binary form must reproduce the above copyright
13 1.5.2.2 skrll * notice, this list of conditions and the following disclaimer in the
14 1.5.2.2 skrll * documentation and/or other materials provided with the distribution.
15 1.5.2.2 skrll *
16 1.5.2.2 skrll * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.5.2.2 skrll * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.5.2.2 skrll * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.5.2.2 skrll * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.5.2.2 skrll * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 1.5.2.2 skrll * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 1.5.2.2 skrll * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 1.5.2.2 skrll * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 1.5.2.2 skrll * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.5.2.2 skrll * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.5.2.2 skrll * SUCH DAMAGE.
27 1.5.2.2 skrll */
28 1.5.2.2 skrll
29 1.5.2.2 skrll #include "opt_arm_timer.h"
30 1.5.2.2 skrll
31 1.5.2.2 skrll #include <sys/cdefs.h>
32 1.5.2.2 skrll __KERNEL_RCSID(0, "$NetBSD: arm_fdt.c,v 1.5.2.2 2017/08/28 17:51:30 skrll Exp $");
33 1.5.2.2 skrll
34 1.5.2.2 skrll #include <sys/param.h>
35 1.5.2.2 skrll #include <sys/systm.h>
36 1.5.2.2 skrll #include <sys/device.h>
37 1.5.2.2 skrll #include <sys/kmem.h>
38 1.5.2.2 skrll #include <sys/bus.h>
39 1.5.2.2 skrll
40 1.5.2.2 skrll #include <machine/cpu.h>
41 1.5.2.2 skrll
42 1.5.2.2 skrll #include <dev/fdt/fdtvar.h>
43 1.5.2.2 skrll #include <dev/ofw/openfirm.h>
44 1.5.2.2 skrll
45 1.5.2.2 skrll #include <arm/fdt/arm_fdtvar.h>
46 1.5.2.2 skrll
47 1.5.2.2 skrll static int arm_fdt_match(device_t, cfdata_t, void *);
48 1.5.2.2 skrll static void arm_fdt_attach(device_t, device_t, void *);
49 1.5.2.2 skrll
50 1.5.2.2 skrll CFATTACH_DECL_NEW(arm_fdt, 0,
51 1.5.2.2 skrll arm_fdt_match, arm_fdt_attach, NULL, NULL);
52 1.5.2.2 skrll
53 1.5.2.2 skrll static struct arm_platlist arm_platform_list =
54 1.5.2.2 skrll TAILQ_HEAD_INITIALIZER(arm_platform_list);
55 1.5.2.2 skrll
56 1.5.2.2 skrll struct arm_fdt_cpu_hatch_cb {
57 1.5.2.2 skrll TAILQ_ENTRY(arm_fdt_cpu_hatch_cb) next;
58 1.5.2.2 skrll void (*cb)(void *, struct cpu_info *);
59 1.5.2.2 skrll void *priv;
60 1.5.2.2 skrll };
61 1.5.2.2 skrll
62 1.5.2.2 skrll static TAILQ_HEAD(, arm_fdt_cpu_hatch_cb) arm_fdt_cpu_hatch_cbs =
63 1.5.2.2 skrll TAILQ_HEAD_INITIALIZER(arm_fdt_cpu_hatch_cbs);
64 1.5.2.2 skrll
65 1.5.2.2 skrll static void (*_arm_fdt_irq_handler)(void *) = NULL;
66 1.5.2.2 skrll static void (*_arm_fdt_timer_init)(void) = NULL;
67 1.5.2.2 skrll
68 1.5.2.2 skrll int
69 1.5.2.2 skrll arm_fdt_match(device_t parent, cfdata_t cf, void *aux)
70 1.5.2.2 skrll {
71 1.5.2.2 skrll return 1;
72 1.5.2.2 skrll }
73 1.5.2.2 skrll
74 1.5.2.2 skrll void
75 1.5.2.2 skrll arm_fdt_attach(device_t parent, device_t self, void *aux)
76 1.5.2.2 skrll {
77 1.5.2.2 skrll const struct arm_platform *plat = arm_fdt_platform();
78 1.5.2.2 skrll struct fdt_attach_args faa;
79 1.5.2.2 skrll
80 1.5.2.2 skrll aprint_naive("\n");
81 1.5.2.2 skrll aprint_normal("\n");
82 1.5.2.2 skrll
83 1.5.2.2 skrll plat->init_attach_args(&faa);
84 1.5.2.2 skrll faa.faa_name = "";
85 1.5.2.2 skrll faa.faa_phandle = OF_peer(0);
86 1.5.2.2 skrll
87 1.5.2.2 skrll config_found(self, &faa, NULL);
88 1.5.2.2 skrll }
89 1.5.2.2 skrll
90 1.5.2.2 skrll const struct arm_platform *
91 1.5.2.2 skrll arm_fdt_platform(void)
92 1.5.2.2 skrll {
93 1.5.2.2 skrll static const struct arm_platform_info *booted_platform = NULL;
94 1.5.2.2 skrll
95 1.5.2.2 skrll if (booted_platform == NULL) {
96 1.5.2.2 skrll __link_set_decl(arm_platforms, struct arm_platform_info);
97 1.5.2.2 skrll struct arm_platform_info * const *info;
98 1.5.2.2 skrll const struct arm_platform_info *best_info = NULL;
99 1.5.2.2 skrll const int phandle = OF_peer(0);
100 1.5.2.2 skrll int match, best_match = 0;
101 1.5.2.2 skrll
102 1.5.2.2 skrll __link_set_foreach(info, arm_platforms) {
103 1.5.2.2 skrll const char * const compat[] = { (*info)->compat, NULL };
104 1.5.2.2 skrll match = of_match_compatible(phandle, compat);
105 1.5.2.2 skrll if (match > best_match) {
106 1.5.2.2 skrll best_match = match;
107 1.5.2.2 skrll best_info = *info;
108 1.5.2.2 skrll }
109 1.5.2.2 skrll }
110 1.5.2.2 skrll
111 1.5.2.2 skrll booted_platform = best_info;
112 1.5.2.2 skrll }
113 1.5.2.2 skrll
114 1.5.2.2 skrll return booted_platform == NULL ? NULL : booted_platform->ops;
115 1.5.2.2 skrll }
116 1.5.2.2 skrll
117 1.5.2.2 skrll void
118 1.5.2.2 skrll arm_fdt_cpu_hatch_register(void *priv, void (*cb)(void *, struct cpu_info *))
119 1.5.2.2 skrll {
120 1.5.2.2 skrll struct arm_fdt_cpu_hatch_cb *c;
121 1.5.2.2 skrll
122 1.5.2.2 skrll c = kmem_alloc(sizeof(*c), KM_SLEEP);
123 1.5.2.2 skrll c->priv = priv;
124 1.5.2.2 skrll c->cb = cb;
125 1.5.2.2 skrll TAILQ_INSERT_TAIL(&arm_fdt_cpu_hatch_cbs, c, next);
126 1.5.2.2 skrll }
127 1.5.2.2 skrll
128 1.5.2.2 skrll void
129 1.5.2.2 skrll arm_fdt_cpu_hatch(struct cpu_info *ci)
130 1.5.2.2 skrll {
131 1.5.2.2 skrll struct arm_fdt_cpu_hatch_cb *c;
132 1.5.2.2 skrll
133 1.5.2.2 skrll TAILQ_FOREACH(c, &arm_fdt_cpu_hatch_cbs, next)
134 1.5.2.2 skrll c->cb(c->priv, ci);
135 1.5.2.2 skrll }
136 1.5.2.2 skrll
137 1.5.2.2 skrll void
138 1.5.2.2 skrll arm_fdt_irq_set_handler(void (*irq_handler)(void *))
139 1.5.2.2 skrll {
140 1.5.2.2 skrll KASSERT(_arm_fdt_irq_handler == NULL);
141 1.5.2.2 skrll _arm_fdt_irq_handler = irq_handler;
142 1.5.2.2 skrll }
143 1.5.2.2 skrll
144 1.5.2.2 skrll void
145 1.5.2.2 skrll arm_fdt_irq_handler(void *tf)
146 1.5.2.2 skrll {
147 1.5.2.2 skrll _arm_fdt_irq_handler(tf);
148 1.5.2.2 skrll }
149 1.5.2.2 skrll
150 1.5.2.2 skrll void
151 1.5.2.2 skrll arm_fdt_timer_register(void (*timerfn)(void))
152 1.5.2.2 skrll {
153 1.5.2.2 skrll if (_arm_fdt_timer_init != NULL) {
154 1.5.2.2 skrll #ifdef DIAGNOSTIC
155 1.5.2.2 skrll aprint_verbose("%s: timer already registered\n", __func__);
156 1.5.2.2 skrll #endif
157 1.5.2.2 skrll return;
158 1.5.2.2 skrll }
159 1.5.2.2 skrll _arm_fdt_timer_init = timerfn;
160 1.5.2.2 skrll }
161 1.5.2.2 skrll
162 1.5.2.2 skrll void
163 1.5.2.2 skrll arm_fdt_memory_dump(paddr_t pa)
164 1.5.2.2 skrll {
165 1.5.2.2 skrll const struct arm_platform *plat = arm_fdt_platform();
166 1.5.2.2 skrll struct fdt_attach_args faa;
167 1.5.2.2 skrll bus_space_tag_t bst;
168 1.5.2.2 skrll bus_space_handle_t bsh;
169 1.5.2.2 skrll
170 1.5.2.2 skrll plat->init_attach_args(&faa);
171 1.5.2.2 skrll
172 1.5.2.2 skrll bst = faa.faa_bst;
173 1.5.2.2 skrll bus_space_map(bst, pa, 0x100, 0, &bsh);
174 1.5.2.2 skrll
175 1.5.2.2 skrll for (int i = 0; i < 0x100; i += 0x10) {
176 1.5.2.2 skrll printf("%" PRIxPTR ": %08x %08x %08x %08x\n",
177 1.5.2.2 skrll (uintptr_t)(pa + i),
178 1.5.2.2 skrll bus_space_read_4(bst, bsh, i + 0),
179 1.5.2.2 skrll bus_space_read_4(bst, bsh, i + 4),
180 1.5.2.2 skrll bus_space_read_4(bst, bsh, i + 8),
181 1.5.2.2 skrll bus_space_read_4(bst, bsh, i + 12));
182 1.5.2.2 skrll }
183 1.5.2.2 skrll }
184 1.5.2.2 skrll
185 1.5.2.2 skrll #ifdef __HAVE_GENERIC_CPU_INITCLOCKS
186 1.5.2.2 skrll void
187 1.5.2.2 skrll cpu_initclocks(void)
188 1.5.2.2 skrll {
189 1.5.2.2 skrll if (_arm_fdt_timer_init == NULL)
190 1.5.2.2 skrll panic("cpu_initclocks: no timer registered");
191 1.5.2.2 skrll _arm_fdt_timer_init();
192 1.5.2.2 skrll }
193 1.5.2.2 skrll #endif
194