arm_fdt.c revision 1.3 1 /* $NetBSD: arm_fdt.c,v 1.3 2017/05/30 22:00:25 jmcneill Exp $ */
2
3 /*-
4 * Copyright (c) 2017 Jared D. 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 <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: arm_fdt.c,v 1.3 2017/05/30 22:00:25 jmcneill Exp $");
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/device.h>
35 #include <sys/kmem.h>
36 #include <sys/bus.h>
37
38 #include <machine/cpu.h>
39
40 #include <dev/fdt/fdtvar.h>
41 #include <dev/ofw/openfirm.h>
42
43 #include <arm/fdt/arm_fdtvar.h>
44
45 static int arm_fdt_match(device_t, cfdata_t, void *);
46 static void arm_fdt_attach(device_t, device_t, void *);
47
48 CFATTACH_DECL_NEW(arm_fdt, 0,
49 arm_fdt_match, arm_fdt_attach, NULL, NULL);
50
51 static struct arm_platlist arm_platform_list =
52 TAILQ_HEAD_INITIALIZER(arm_platform_list);
53
54 struct arm_fdt_cpu_hatch_cb {
55 TAILQ_ENTRY(arm_fdt_cpu_hatch_cb) next;
56 void (*cb)(void *, struct cpu_info *);
57 void *priv;
58 };
59
60 static TAILQ_HEAD(, arm_fdt_cpu_hatch_cb) arm_fdt_cpu_hatch_cbs =
61 TAILQ_HEAD_INITIALIZER(arm_fdt_cpu_hatch_cbs);
62
63 static void (*_arm_fdt_irq_handler)(void *) = NULL;
64
65 int
66 arm_fdt_match(device_t parent, cfdata_t cf, void *aux)
67 {
68 return 1;
69 }
70
71 void
72 arm_fdt_attach(device_t parent, device_t self, void *aux)
73 {
74 const struct arm_platform *plat = arm_fdt_platform();
75 struct fdt_attach_args faa;
76
77 aprint_naive("\n");
78 aprint_normal("\n");
79
80 plat->init_attach_args(&faa);
81 faa.faa_name = "";
82 faa.faa_phandle = OF_peer(0);
83
84 config_found(self, &faa, NULL);
85 }
86
87 const struct arm_platform *
88 arm_fdt_platform(void)
89 {
90 static const struct arm_platform_info *booted_platform = NULL;
91
92 if (booted_platform == NULL) {
93 __link_set_decl(arm_platforms, struct arm_platform_info);
94 struct arm_platform_info * const *info;
95 const struct arm_platform_info *best_info = NULL;
96 const int phandle = OF_peer(0);
97 int match, best_match = 0;
98
99 __link_set_foreach(info, arm_platforms) {
100 const char * const compat[] = { (*info)->compat, NULL };
101 match = of_match_compatible(phandle, compat);
102 if (match > best_match) {
103 best_match = match;
104 best_info = *info;
105 }
106 }
107
108 booted_platform = best_info;
109 }
110
111 return booted_platform == NULL ? NULL : booted_platform->ops;
112 }
113
114 void
115 arm_fdt_cpu_hatch_register(void *priv, void (*cb)(void *, struct cpu_info *))
116 {
117 struct arm_fdt_cpu_hatch_cb *c;
118
119 c = kmem_alloc(sizeof(*c), KM_SLEEP);
120 c->priv = priv;
121 c->cb = cb;
122 TAILQ_INSERT_TAIL(&arm_fdt_cpu_hatch_cbs, c, next);
123 }
124
125 void
126 arm_fdt_cpu_hatch(struct cpu_info *ci)
127 {
128 struct arm_fdt_cpu_hatch_cb *c;
129
130 TAILQ_FOREACH(c, &arm_fdt_cpu_hatch_cbs, next)
131 c->cb(c->priv, ci);
132 }
133
134 void
135 arm_fdt_irq_set_handler(void (*irq_handler)(void *))
136 {
137 KASSERT(_arm_fdt_irq_handler == NULL);
138 _arm_fdt_irq_handler = irq_handler;
139 }
140
141 void
142 arm_fdt_irq_handler(void *tf)
143 {
144 _arm_fdt_irq_handler(tf);
145 }
146