fdtbus.c revision 1.4 1 1.4 jmcneill /* $NetBSD: fdtbus.c,v 1.4 2017/04/13 22:12:53 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.4 jmcneill __KERNEL_RCSID(0, "$NetBSD: fdtbus.c,v 1.4 2017/04/13 22:12:53 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.1 jmcneill
60 1.1 jmcneill if (!OF_child(faa->faa_phandle))
61 1.1 jmcneill return 0;
62 1.1 jmcneill
63 1.1 jmcneill return 1;
64 1.1 jmcneill }
65 1.1 jmcneill
66 1.1 jmcneill static void
67 1.1 jmcneill fdt_attach(device_t parent, device_t self, void *aux)
68 1.1 jmcneill {
69 1.1 jmcneill const struct fdt_attach_args *faa = aux;
70 1.1 jmcneill const int phandle = faa->faa_phandle;
71 1.1 jmcneill char *model;
72 1.1 jmcneill int len, n;
73 1.1 jmcneill
74 1.1 jmcneill aprint_naive("\n");
75 1.1 jmcneill len = OF_getproplen(phandle, "model");
76 1.1 jmcneill if (len > 0) {
77 1.1 jmcneill model = kmem_zalloc(len, KM_SLEEP);
78 1.1 jmcneill if (OF_getprop(phandle, "model", model, len) == len) {
79 1.1 jmcneill aprint_normal(": %s\n", model);
80 1.1 jmcneill } else {
81 1.1 jmcneill aprint_normal("\n");
82 1.1 jmcneill }
83 1.1 jmcneill kmem_free(model, len);
84 1.1 jmcneill } else {
85 1.1 jmcneill aprint_normal("\n");
86 1.1 jmcneill }
87 1.1 jmcneill
88 1.1 jmcneill for (n = 0; n < faa->faa_ninit; n++) {
89 1.1 jmcneill fdt_scan(self, faa, faa->faa_init[n]);
90 1.1 jmcneill }
91 1.1 jmcneill fdt_scan(self, faa, NULL);
92 1.1 jmcneill }
93 1.1 jmcneill
94 1.1 jmcneill static void
95 1.1 jmcneill fdt_scan(device_t self, const struct fdt_attach_args *faa, const char *devname)
96 1.1 jmcneill {
97 1.1 jmcneill const int phandle = faa->faa_phandle;
98 1.3 skrll int len, child;
99 1.1 jmcneill char *name, *status;
100 1.1 jmcneill
101 1.1 jmcneill for (child = OF_child(phandle); child; child = OF_peer(child)) {
102 1.1 jmcneill struct fdt_attach_args nfaa = *faa;
103 1.1 jmcneill nfaa.faa_phandle = child;
104 1.1 jmcneill
105 1.1 jmcneill /* Only attach to nodes with a compatible property */
106 1.1 jmcneill len = OF_getproplen(child, "compatible");
107 1.1 jmcneill if (len <= 0)
108 1.1 jmcneill continue;
109 1.1 jmcneill
110 1.1 jmcneill /* If there is a "status" property, make sure it is "okay" */
111 1.1 jmcneill len = OF_getproplen(child, "status");
112 1.1 jmcneill if (len > 0) {
113 1.1 jmcneill status = kmem_zalloc(len, KM_SLEEP);
114 1.3 skrll int alen __diagused = OF_getprop(child, "status", status, len);
115 1.1 jmcneill KASSERT(alen == len);
116 1.2 jmcneill const bool okay_p = strcmp(status, "okay") == 0 ||
117 1.2 jmcneill strcmp(status, "ok") == 0;
118 1.1 jmcneill kmem_free(status, len);
119 1.1 jmcneill if (!okay_p) {
120 1.1 jmcneill continue;
121 1.1 jmcneill }
122 1.1 jmcneill }
123 1.1 jmcneill
124 1.1 jmcneill /* Attach the device */
125 1.1 jmcneill len = OF_getproplen(child, "name");
126 1.1 jmcneill if (len <= 0) {
127 1.1 jmcneill continue;
128 1.1 jmcneill }
129 1.1 jmcneill name = kmem_zalloc(len, KM_SLEEP);
130 1.1 jmcneill if (OF_getprop(child, "name", name, len) == len) {
131 1.1 jmcneill nfaa.faa_name = name;
132 1.1 jmcneill if ((devname != NULL && strcmp(name, devname) == 0) ||
133 1.1 jmcneill (devname == NULL && !fdt_is_init(faa, name))) {
134 1.1 jmcneill config_found(self, &nfaa, fdt_print);
135 1.1 jmcneill }
136 1.1 jmcneill }
137 1.1 jmcneill kmem_free(name, len);
138 1.1 jmcneill }
139 1.1 jmcneill }
140 1.1 jmcneill
141 1.1 jmcneill static bool
142 1.1 jmcneill fdt_is_init(const struct fdt_attach_args *faa, const char *devname)
143 1.1 jmcneill {
144 1.1 jmcneill u_int n;
145 1.1 jmcneill
146 1.1 jmcneill for (n = 0; n < faa->faa_ninit; n++) {
147 1.1 jmcneill if (strcmp(faa->faa_init[n], devname) == 0)
148 1.1 jmcneill return true;
149 1.1 jmcneill }
150 1.1 jmcneill
151 1.1 jmcneill return false;
152 1.1 jmcneill }
153 1.1 jmcneill
154 1.1 jmcneill static int
155 1.1 jmcneill fdt_print(void *aux, const char *pnp)
156 1.1 jmcneill {
157 1.1 jmcneill const struct fdt_attach_args * const faa = aux;
158 1.4 jmcneill char buf[FDT_MAX_PATH];
159 1.4 jmcneill const char *name = buf;
160 1.1 jmcneill
161 1.1 jmcneill if (pnp) {
162 1.4 jmcneill if (!fdtbus_get_path(faa->faa_phandle, buf, sizeof(buf)))
163 1.4 jmcneill name = faa->faa_name;
164 1.4 jmcneill aprint_normal("%s at %s", name, pnp);
165 1.1 jmcneill }
166 1.1 jmcneill
167 1.1 jmcneill return UNCONF;
168 1.1 jmcneill }
169