fdt_subr.c revision 1.4 1 /* $NetBSD: fdt_subr.c,v 1.4 2015/12/16 19:33:55 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.4 2015/12/16 19:33:55 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 uint32_t addr_cells;
86
87 const int parent = OF_parent(phandle);
88 if (parent == -1)
89 return -1;
90
91 if (of_getprop_uint32(parent, "#address-cells", &addr_cells))
92 addr_cells = 2;
93
94 return addr_cells;
95 }
96
97 static int
98 fdtbus_get_size_cells(int phandle)
99 {
100 uint32_t size_cells;
101
102 const int parent = OF_parent(phandle);
103 if (parent == -1)
104 return -1;
105
106 if (of_getprop_uint32(parent, "#size-cells", &size_cells))
107 size_cells = 0;
108
109 return size_cells;
110 }
111
112 int
113 fdtbus_get_phandle(int phandle, const char *prop)
114 {
115 u_int phandle_ref;
116 u_int *buf;
117 int len;
118
119 len = OF_getproplen(phandle, prop);
120 if (len < sizeof(phandle_ref))
121 return -1;
122
123 buf = kmem_alloc(len, KM_SLEEP);
124
125 if (OF_getprop(phandle, prop, buf, len) != len) {
126 kmem_free(buf, len);
127 return -1;
128 }
129
130 phandle_ref = fdt32_to_cpu(buf[0]);
131 kmem_free(buf, len);
132
133 const int off = fdt_node_offset_by_phandle(fdt_data, phandle_ref);
134 if (off < 0) {
135 return -1;
136 }
137
138 return fdtbus_offset2phandle(off);
139 }
140
141 int
142 fdtbus_get_reg(int phandle, u_int index, bus_addr_t *paddr, bus_size_t *psize)
143 {
144 bus_addr_t addr;
145 bus_size_t size;
146 uint8_t *buf;
147 int len;
148
149 const int addr_cells = fdtbus_get_addr_cells(phandle);
150 const int size_cells = fdtbus_get_size_cells(phandle);
151 if (addr_cells == -1 || size_cells == -1)
152 return -1;
153
154 const u_int reglen = size_cells * 4 + addr_cells * 4;
155 if (reglen == 0)
156 return -1;
157
158 len = OF_getproplen(phandle, "reg");
159 if (len <= 0)
160 return -1;
161
162 const u_int nregs = len / reglen;
163
164 if (index >= nregs)
165 return -1;
166
167 buf = kmem_alloc(len, KM_SLEEP);
168 if (buf == NULL)
169 return -1;
170
171 len = OF_getprop(phandle, "reg", buf, len);
172
173 switch (addr_cells) {
174 case 0:
175 addr = 0;
176 break;
177 case 1:
178 addr = be32dec(&buf[index * reglen + 0]);
179 break;
180 case 2:
181 addr = be64dec(&buf[index * reglen + 0]);
182 break;
183 default:
184 panic("fdtbus_get_reg: unsupported addr_cells %d", addr_cells);
185 }
186
187 switch (size_cells) {
188 case 0:
189 size = 0;
190 break;
191 case 1:
192 size = be32dec(&buf[index * reglen + addr_cells * 4]);
193 break;
194 case 2:
195 size = be64dec(&buf[index * reglen + addr_cells * 4]);
196 break;
197 default:
198 panic("fdtbus_get_reg: unsupported size_cells %d", size_cells);
199 }
200
201 if (paddr)
202 *paddr = addr;
203 if (psize)
204 *psize = size;
205
206 kmem_free(buf, len);
207
208 return 0;
209 }
210