1 \ tag: Property management
3 \ this code implements IEEE 1275-1994 ch. 5.3.5
5 \ Copyright (C) 2003 Stefan Reinauer
7 \ See the file "COPYING" for further information about
8 \ the copyright and warranty status of this work.
11 \ small helpers.. these should go elsewhere.
13 10 here ! here c@ 10 <>
30 \ allocate n bytes for device tree information
31 \ until I know where to put this, I put it in the
34 : alloc-tree ( n -- addr )
37 dup r> 0 fill \ clear memory
44 : no-active true abort" no active package." ;
47 \ 5.3.5 Property management
51 : find-property ( name len phandle -- &&prop|0 )
56 dup @ >prop.name @ ( name len prop propname )
57 2over comp0 ( name len prop equal? )
58 0= if nip nip exit then
65 \ From package (5.3.4.1)
67 ( previous-str previous-len phandle -- false | name-str name-len true )
70 2drop r> >dn.properties @
72 r> find-property dup if @ then
73 dup if >prop.next @ then
77 >prop.name @ dup cstrlen true
78 ( phandle name-str name-len true )
86 \ 5.3.5.4 Property value access
89 \ Return value for name string property in package phandle.
90 : get-package-property
91 ( name-str name-len phandle -- true | prop-addr prop-len false )
101 \ Return value for given property in the current instance or its parents.
102 : get-inherited-property
103 ( name-str name-len -- true | prop-addr prop-len false )
108 dup >in.device-node @ ( str len ihandle phandle )
109 2over rot find-property ?dup if
111 ( str len ihandle prop )
113 dup >prop.addr @ swap >prop.len @
124 \ Return value for given property in this package.
125 : get-my-property ( name-str name-len -- true | prop-addr prop-len false )
126 my-self >in.device-node @ ( -- phandle )
132 \ 5.3.5.2 Property array decoding
135 : decode-int ( prop-addr1 prop-len1 -- prop-addr2 prop-len2 n )
137 dup 4 min >r ( addr1 len1 R:minlen )
138 over r@ + swap ( addr1 addr2 len1 R:minlen )
139 r> - ( addr1 addr2 len2 )
146 \ HELPER: get #address-cell value (from parent)
147 \ Legal values are 1..4 (we may optionally support longer addresses)
148 : my-#acells ( -- #address-cells )
149 my-self ?dup if >in.device-node @ else active-package then
150 ?dup if >dn.parent @ then
152 " #address-cells" rot get-package-property if 2 exit then
153 \ we don't have to support more than 4 (and 0 is illegal)
154 decode-int nip nip 4 min 1 max
160 \ HELPER: get #size-cells value (from parent)
161 : my-#scells ( -- #size-cells )
162 my-self ?dup if >in.device-node @ else active-package then
163 ?dup if >dn.parent @ then
165 " #size-cells" rot get-package-property if 1 exit then
172 : decode-string ( prop-addr1 prop-len1 -- prop-addr2 prop-len2 str len )
174 2dup bounds \ check property for 0 bytes
175 0 -rot \ initial string len is 0
181 loop ( prop-addr1 prop-len1 len )
182 1+ rot >r ( prop-len1 len R: prop-addr1 )
183 over min 2dup - ( prop-len1 nlen prop-len2 R: prop-addr1 )
184 r@ 2 pick + ( prop-len1 nlen prop-len2 prop-addr2 )
185 >r >r >r ( R: prop-addr1 prop-addr2 prop-len2 nlen )
187 r> r> r> ( nlen prop-len2 prop-addr2 )
188 -rot swap 1- ( prop-addr2 prop-len2 nlen )
189 r> swap ( prop-addr2 prop-len2 str len )
195 : decode-bytes ( addr1 len1 #bytes -- addr len2 addr1 #bytes )
196 tuck - ( addr1 #bytes len2 )
197 r> 2dup + ( addr1 #bytes addr2 ) ( R: len2 )
202 ( prop-addr1 prop-len1 -- prop-addr2 prop-len2 phys.lo ... phys.hi )
204 decode-int r> r> rot >r >r >r
213 \ 5.3.5.1 Property array encoding
216 : encode-int ( n -- prop-addr prop-len )
217 /l alloc-tree tuck l!-be /l
220 : encode-string ( str len -- prop-addr prop-len )
221 \ we trust len here. should probably check string?
222 tuck char+ alloc-tree ( len str prop-addr )
223 tuck 3 pick move ( len prop-addr )
227 : encode-bytes ( data-addr data-len -- prop-addr prop-len )
228 tuck alloc-tree ( len str prop-addr )
233 : encode+ ( prop-addr1 prop-len1 prop-addr2 prop-len2 -- prop-addr3 prop-len3 )
237 : encode-phys ( phys.lo ... phys.hi -- prop-addr prop-len )
238 encode-int my-#acells 1- 0 ?do
239 rot encode-int encode+
243 defer sbus-intr>cpu ( sbus-intr# -- cpu-intr# )
244 : (sbus-intr>cpu) ." No SBUS present on this machine." cr ;
245 ['] (sbus-intr>cpu) to sbus-intr>cpu
249 \ 5.3.5.3 Property declaration
252 : (property) ( prop-addr prop-len name-str name-len dnode -- )
255 find-property ?dup if
256 \ If a property with that property name already exists in the
257 \ package in which the property would be created, replace its
258 \ value with the new value.
259 @ r> drop \ don't need the device node anymore.
260 -rot 2drop tuck \ drop property name
261 >prop.len ! \ overwrite old values
266 ( prop-addr prop-len name-str name-len R: dn )
267 prop-node.size alloc-tree
270 dup r> >dn.properties
271 begin dup @ while @ >prop.next repeat !
274 ( prop-addr prop-len name-str name-len R: prop )
276 \ create copy of property name
279 ( prop-addr prop-len new-name R: prop )
286 : property ( prop-addr prop-len name-str name-len -- )
299 : (delete-property) ( name len dnode -- )
300 find-property ?dup if
301 dup @ >prop.next @ swap !
302 \ maybe we should try to reclaim the space?
306 : delete-property ( name-str name-len -- )
307 active-package ?dup if
314 \ Create the "name" property; value is indicated string.
315 : device-name ( str len -- )
316 encode-string " name" property
319 \ Create "device_type" property, value is indicated string.
320 : device-type ( str len -- )
321 encode-string " device_type" property
324 \ Create the "reg" property with the given values.
325 : reg ( phys.lo ... phys.hi size -- )
326 >r ( phys.lo ... phys.hi ) encode-phys ( addr len )
327 r> ( addr1 len1 size ) encode-int ( addr1 len1 addr2 len2 )
332 \ Create the "model" property; value is indicated string.
333 : model ( str len -- )
334 encode-string " model" property