fdt_subr.c revision 1.3 1 /* $NetBSD: fdt_subr.c,v 1.3 2015/12/16 12:17:45 jmcneill Exp $ */
2
3 /*-
4 * Copyright (c) 2015 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: fdt_subr.c,v 1.3 2015/12/16 12:17:45 jmcneill Exp $");
31
32 #include <sys/param.h>
33 #include <sys/bus.h>
34 #include <sys/kmem.h>
35
36 #include <libfdt.h>
37 #include <dev/fdt/fdtvar.h>
38
39 static const void *fdt_data;
40
41 bool
42 fdtbus_set_data(const void *data)
43 {
44 KASSERT(fdt_data == NULL);
45 if (fdt_check_header(data) != 0) {
46 return false;
47 }
48 fdt_data = data;
49 return true;
50 }
51
52 const void *
53 fdtbus_get_data(void)
54 {
55 return fdt_data;
56 }
57
58 int
59 fdtbus_offset2phandle(int offset)
60 {
61 if (offset < 0)
62 return 0;
63
64 return offset + fdt_off_dt_struct(fdt_data);
65 }
66
67 int
68 fdtbus_phandle2offset(int phandle)
69 {
70 const int dtoff = fdt_off_dt_struct(fdt_data);
71
72 if (phandle == -1)
73 phandle = dtoff;
74
75 if (phandle < dtoff)
76 return -1;
77
78 return phandle - dtoff;
79 }
80
81
82 static int
83 fdtbus_get_addr_cells(int phandle)
84 {
85 int val, addr_cells, error;
86
87 const int parent = OF_parent(phandle);
88 if (parent == -1)
89 return -1;
90
91 error = OF_getprop(parent, "#address-cells", &val, sizeof(val));
92 if (error <= 0) {
93 addr_cells = 2;
94 } else {
95 addr_cells = fdt32_to_cpu(val);
96 }
97
98 return addr_cells;
99 }
100
101 static int
102 fdtbus_get_size_cells(int phandle)
103 {
104 int val, size_cells, error;
105
106 const int parent = OF_parent(phandle);
107 if (parent == -1)
108 return -1;
109
110 error = OF_getprop(parent, "#size-cells", &val, sizeof(val));
111 if (error <= 0) {
112 size_cells = 0;
113 } else {
114 size_cells = fdt32_to_cpu(val);
115 }
116
117 return size_cells;
118 }
119
120 int
121 fdtbus_get_phandle(int phandle, const char *prop)
122 {
123 u_int phandle_ref;
124 u_int *buf;
125 int len;
126
127 len = OF_getproplen(phandle, prop);
128 if (len < sizeof(phandle_ref))
129 return -1;
130
131 buf = kmem_alloc(len, KM_SLEEP);
132
133 if (OF_getprop(phandle, prop, buf, len) != len) {
134 kmem_free(buf, len);
135 return -1;
136 }
137
138 phandle_ref = fdt32_to_cpu(buf[0]);
139 kmem_free(buf, len);
140
141 const int off = fdt_node_offset_by_phandle(fdt_data, phandle_ref);
142 if (off < 0) {
143 return -1;
144 }
145
146 return fdtbus_offset2phandle(off);
147 }
148
149 int
150 fdtbus_get_reg(int phandle, u_int index, bus_addr_t *paddr, bus_size_t *psize)
151 {
152 bus_addr_t addr;
153 bus_size_t size;
154 uint8_t *buf;
155 int len;
156
157 const int addr_cells = fdtbus_get_addr_cells(phandle);
158 const int size_cells = fdtbus_get_size_cells(phandle);
159 if (addr_cells == -1 || size_cells == -1)
160 return -1;
161
162 const u_int reglen = size_cells * 4 + addr_cells * 4;
163 if (reglen == 0)
164 return -1;
165
166 len = OF_getproplen(phandle, "reg");
167 if (len <= 0)
168 return -1;
169
170 const u_int nregs = len / reglen;
171
172 if (index >= nregs)
173 return -1;
174
175 buf = kmem_alloc(len, KM_SLEEP);
176 if (buf == NULL)
177 return -1;
178
179 len = OF_getprop(phandle, "reg", buf, len);
180
181 switch (addr_cells) {
182 case 0:
183 addr = 0;
184 break;
185 case 1:
186 addr = be32dec(&buf[index * reglen + 0]);
187 break;
188 case 2:
189 addr = be64dec(&buf[index * reglen + 0]);
190 break;
191 default:
192 panic("fdtbus_get_reg: unsupported addr_cells %d", addr_cells);
193 }
194
195 switch (size_cells) {
196 case 0:
197 size = 0;
198 break;
199 case 1:
200 size = be32dec(&buf[index * reglen + addr_cells * 4]);
201 break;
202 case 2:
203 size = be64dec(&buf[index * reglen + addr_cells * 4]);
204 break;
205 default:
206 panic("fdtbus_get_reg: unsupported size_cells %d", size_cells);
207 }
208
209 if (paddr)
210 *paddr = addr;
211 if (psize)
212 *psize = size;
213
214 kmem_free(buf, len);
215
216 return 0;
217 }
218