fdtbus.c revision 1.6 1 1.6 jmcneill /* $NetBSD: fdtbus.c,v 1.6 2017/04/14 22:55:06 jmcneill Exp $ */
2 1.1 jmcneill
3 1.1 jmcneill /*-
4 1.1 jmcneill * Copyright (c) 2015 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.1 jmcneill #include <sys/cdefs.h>
30 1.6 jmcneill __KERNEL_RCSID(0, "$NetBSD: fdtbus.c,v 1.6 2017/04/14 22:55:06 jmcneill Exp $");
31 1.1 jmcneill
32 1.1 jmcneill #include <sys/param.h>
33 1.1 jmcneill #include <sys/systm.h>
34 1.1 jmcneill #include <sys/device.h>
35 1.1 jmcneill #include <sys/kmem.h>
36 1.1 jmcneill
37 1.1 jmcneill #include <sys/bus.h>
38 1.1 jmcneill
39 1.1 jmcneill #include <dev/ofw/openfirm.h>
40 1.1 jmcneill
41 1.1 jmcneill #include <dev/fdt/fdtvar.h>
42 1.1 jmcneill
43 1.4 jmcneill #define FDT_MAX_PATH 256
44 1.4 jmcneill
45 1.1 jmcneill static int fdt_match(device_t, cfdata_t, void *);
46 1.1 jmcneill static void fdt_attach(device_t, device_t, void *);
47 1.1 jmcneill static void fdt_scan(device_t, const struct fdt_attach_args *, const char *);
48 1.1 jmcneill static bool fdt_is_init(const struct fdt_attach_args *, const char *);
49 1.1 jmcneill
50 1.1 jmcneill CFATTACH_DECL_NEW(fdt, 0,
51 1.1 jmcneill fdt_match, fdt_attach, NULL, NULL);
52 1.1 jmcneill
53 1.1 jmcneill static int fdt_print(void *, const char *);
54 1.1 jmcneill
55 1.1 jmcneill static int
56 1.1 jmcneill fdt_match(device_t parent, cfdata_t cf, void *aux)
57 1.1 jmcneill {
58 1.1 jmcneill const struct fdt_attach_args *faa = aux;
59 1.6 jmcneill const char * const compatible[] = { "simple-bus", NULL };
60 1.6 jmcneill int match;
61 1.1 jmcneill
62 1.1 jmcneill if (!OF_child(faa->faa_phandle))
63 1.1 jmcneill return 0;
64 1.1 jmcneill
65 1.6 jmcneill match = of_match_compatible(faa->faa_phandle, compatible);
66 1.6 jmcneill if (match)
67 1.6 jmcneill return match;
68 1.6 jmcneill
69 1.6 jmcneill return OF_finddevice("/") == faa->faa_phandle;
70 1.1 jmcneill }
71 1.1 jmcneill
72 1.1 jmcneill static void
73 1.1 jmcneill fdt_attach(device_t parent, device_t self, void *aux)
74 1.1 jmcneill {
75 1.1 jmcneill const struct fdt_attach_args *faa = aux;
76 1.1 jmcneill const int phandle = faa->faa_phandle;
77 1.1 jmcneill char *model;
78 1.1 jmcneill int len, n;
79 1.1 jmcneill
80 1.1 jmcneill aprint_naive("\n");
81 1.1 jmcneill len = OF_getproplen(phandle, "model");
82 1.1 jmcneill if (len > 0) {
83 1.1 jmcneill model = kmem_zalloc(len, KM_SLEEP);
84 1.1 jmcneill if (OF_getprop(phandle, "model", model, len) == len) {
85 1.1 jmcneill aprint_normal(": %s\n", model);
86 1.1 jmcneill } else {
87 1.1 jmcneill aprint_normal("\n");
88 1.1 jmcneill }
89 1.1 jmcneill kmem_free(model, len);
90 1.1 jmcneill } else {
91 1.1 jmcneill aprint_normal("\n");
92 1.1 jmcneill }
93 1.1 jmcneill
94 1.1 jmcneill for (n = 0; n < faa->faa_ninit; n++) {
95 1.1 jmcneill fdt_scan(self, faa, faa->faa_init[n]);
96 1.1 jmcneill }
97 1.1 jmcneill fdt_scan(self, faa, NULL);
98 1.1 jmcneill }
99 1.1 jmcneill
100 1.1 jmcneill static void
101 1.1 jmcneill fdt_scan(device_t self, const struct fdt_attach_args *faa, const char *devname)
102 1.1 jmcneill {
103 1.1 jmcneill const int phandle = faa->faa_phandle;
104 1.3 skrll int len, child;
105 1.1 jmcneill char *name, *status;
106 1.1 jmcneill
107 1.1 jmcneill for (child = OF_child(phandle); child; child = OF_peer(child)) {
108 1.1 jmcneill struct fdt_attach_args nfaa = *faa;
109 1.1 jmcneill nfaa.faa_phandle = child;
110 1.1 jmcneill
111 1.1 jmcneill /* If there is a "status" property, make sure it is "okay" */
112 1.1 jmcneill len = OF_getproplen(child, "status");
113 1.1 jmcneill if (len > 0) {
114 1.1 jmcneill status = kmem_zalloc(len, KM_SLEEP);
115 1.3 skrll int alen __diagused = OF_getprop(child, "status", status, len);
116 1.1 jmcneill KASSERT(alen == len);
117 1.2 jmcneill const bool okay_p = strcmp(status, "okay") == 0 ||
118 1.2 jmcneill strcmp(status, "ok") == 0;
119 1.1 jmcneill kmem_free(status, len);
120 1.1 jmcneill if (!okay_p) {
121 1.1 jmcneill continue;
122 1.1 jmcneill }
123 1.1 jmcneill }
124 1.1 jmcneill
125 1.1 jmcneill /* Attach the device */
126 1.1 jmcneill len = OF_getproplen(child, "name");
127 1.1 jmcneill if (len <= 0) {
128 1.1 jmcneill continue;
129 1.1 jmcneill }
130 1.1 jmcneill name = kmem_zalloc(len, KM_SLEEP);
131 1.1 jmcneill if (OF_getprop(child, "name", name, len) == len) {
132 1.1 jmcneill nfaa.faa_name = name;
133 1.1 jmcneill if ((devname != NULL && strcmp(name, devname) == 0) ||
134 1.1 jmcneill (devname == NULL && !fdt_is_init(faa, name))) {
135 1.1 jmcneill config_found(self, &nfaa, fdt_print);
136 1.1 jmcneill }
137 1.1 jmcneill }
138 1.1 jmcneill kmem_free(name, len);
139 1.1 jmcneill }
140 1.1 jmcneill }
141 1.1 jmcneill
142 1.1 jmcneill static bool
143 1.1 jmcneill fdt_is_init(const struct fdt_attach_args *faa, const char *devname)
144 1.1 jmcneill {
145 1.1 jmcneill u_int n;
146 1.1 jmcneill
147 1.1 jmcneill for (n = 0; n < faa->faa_ninit; n++) {
148 1.1 jmcneill if (strcmp(faa->faa_init[n], devname) == 0)
149 1.1 jmcneill return true;
150 1.1 jmcneill }
151 1.1 jmcneill
152 1.1 jmcneill return false;
153 1.1 jmcneill }
154 1.1 jmcneill
155 1.1 jmcneill static int
156 1.1 jmcneill fdt_print(void *aux, const char *pnp)
157 1.1 jmcneill {
158 1.1 jmcneill const struct fdt_attach_args * const faa = aux;
159 1.4 jmcneill char buf[FDT_MAX_PATH];
160 1.4 jmcneill const char *name = buf;
161 1.1 jmcneill
162 1.5 jmcneill /* Skip "not configured" for nodes w/o compatible property */
163 1.5 jmcneill if (OF_getproplen(faa->faa_phandle, "compatible") <= 0)
164 1.5 jmcneill return QUIET;
165 1.5 jmcneill
166 1.1 jmcneill if (pnp) {
167 1.4 jmcneill if (!fdtbus_get_path(faa->faa_phandle, buf, sizeof(buf)))
168 1.4 jmcneill name = faa->faa_name;
169 1.4 jmcneill aprint_normal("%s at %s", name, pnp);
170 1.1 jmcneill }
171 1.1 jmcneill
172 1.1 jmcneill return UNCONF;
173 1.1 jmcneill }
174