Annotation of qemu/roms/SLOF/board-qemu/slof/fdt.fs, revision 1.1

1.1     ! root        1: \ *****************************************************************************
        !             2: \ * Copyright (c) 2011 IBM Corporation
        !             3: \ * All rights reserved.
        !             4: \ * This program and the accompanying materials
        !             5: \ * are made available under the terms of the BSD License
        !             6: \ * which accompanies this distribution, and is available at
        !             7: \ * http://www.opensource.org/licenses/bsd-license.php
        !             8: \ *
        !             9: \ * Contributors:
        !            10: \ *     IBM Corporation - initial implementation
        !            11: \ ****************************************************************************/
        !            12: 
        !            13: 0 VALUE fdt-debug
        !            14: 
        !            15: \ Bail out if no fdt
        !            16: fdt-start 0 = IF -1 throw THEN
        !            17: 
        !            18: struct
        !            19:   4 field >fdth_magic
        !            20:   4 field >fdth_tsize
        !            21:   4 field >fdth_struct_off
        !            22:   4 field >fdth_string_off
        !            23:   4 field >fdth_rsvmap_off
        !            24:   4 field >fdth_version
        !            25:   4 field >fdth_compat_vers
        !            26:   4 field >fdth_boot_cpu
        !            27:   4 field >fdth_string_size
        !            28:   4 field >fdth_struct_size
        !            29: drop
        !            30: 
        !            31: h# d00dfeed constant OF_DT_HEADER
        !            32: h#        1 constant OF_DT_BEGIN_NODE
        !            33: h#        2 constant OF_DT_END_NODE
        !            34: h#        3 constant OF_DT_PROP
        !            35: h#        4 constant OF_DT_NOP
        !            36: h#        9 constant OF_DT_END
        !            37: 
        !            38: \ Create some variables early
        !            39: fdt-start
        !            40: dup dup >fdth_struct_off l@ + value fdt-struct
        !            41: dup dup >fdth_string_off l@ + value fdt-strings
        !            42: drop
        !            43: 
        !            44: \ Dump fdt header for all to see and check FDT validity
        !            45: : fdt-check-header ( -- )
        !            46:     fdt-start dup 0 = IF
        !            47:         ." No flat device tree !" cr drop -1 throw EXIT THEN
        !            48:     hex
        !            49:     fdt-debug IF
        !            50:         ." Flat device tree header at 0x" dup . s" :" type cr
        !            51:         ."  magic            : 0x" dup >fdth_magic l@ . cr
        !            52:         ."  total size       : 0x" dup >fdth_tsize l@ . cr
        !            53:         ."  offset to struct : 0x" dup >fdth_struct_off l@ . cr
        !            54:         ."  offset to strings: 0x" dup >fdth_string_off l@ . cr
        !            55:         ."  offset to rsvmap : 0x" dup >fdth_rsvmap_off l@ . cr
        !            56:         ."  version          : " dup >fdth_version l@ decimal . hex cr
        !            57:         ."  last compat vers : " dup >fdth_compat_vers l@ decimal . hex cr
        !            58:         dup >fdth_version l@ 2 >= IF
        !            59:             ."  boot CPU         : 0x" dup >fdth_boot_cpu l@ . cr
        !            60:         THEN
        !            61:         dup >fdth_version l@ 3 >= IF
        !            62:             ."  strings size     : 0x" dup >fdth_string_size l@ . cr
        !            63:         THEN
        !            64:         dup >fdth_version l@ 17 >= IF
        !            65:             ."  struct size      : 0x" dup >fdth_struct_size l@ . cr
        !            66:         THEN
        !            67:     THEN
        !            68:     dup >fdth_magic l@ OF_DT_HEADER <> IF
        !            69:         ." Flat device tree has incorrect magic value !" cr
        !            70:        drop -1 throw EXIT
        !            71:     THEN
        !            72:     dup >fdth_version l@ 10 < IF
        !            73:         ." Flat device tree has usupported version !" cr
        !            74:        drop -1 throw EXIT
        !            75:     THEN
        !            76: 
        !            77:     drop
        !            78: ;
        !            79: fdt-check-header
        !            80: 
        !            81: \ Fetch next tag, skip nops and increment address
        !            82: : fdt-next-tag ( addr -- nextaddr tag )
        !            83:   0                            ( dummy tag on stack for loop )
        !            84:   BEGIN
        !            85:     drop                       ( drop previous tag )
        !            86:     dup l@                     ( read new tag )
        !            87:     swap 4 + swap              ( increment addr )
        !            88:   dup OF_DT_NOP <> UNTIL       ( loop until not nop )
        !            89: ;
        !            90: 
        !            91: \ Parse unit name and advance addr
        !            92: : fdt-fetch-unit ( addr -- addr $name )
        !            93:   dup from-cstring            \  get string size
        !            94:   2dup + 1 + 3 + fffffffc and -rot
        !            95: ;
        !            96: 
        !            97: \ Lookup a string by index
        !            98: : fdt-fetch-string ( index -- $string)  
        !            99:   fdt-strings + dup from-cstring
        !           100: ;
        !           101: 
        !           102: : fdt-create-dec  s" decode-unit" $CREATE , DOES> @ hex-decode-unit ;
        !           103: : fdt-create-enc  s" encode-unit" $CREATE , DOES> @ hex-encode-unit ;
        !           104: 
        !           105: \ Method to unflatten a node
        !           106: : fdt-unflatten-node ( start -- end )
        !           107:   \ this can and will recurse
        !           108:   recursive
        !           109: 
        !           110:   \ Get & check first tag of node ( addr -- addr)
        !           111:   fdt-next-tag dup OF_DT_BEGIN_NODE <> IF
        !           112:     s" Weird tag 0x" type . " at start of node" type cr
        !           113:     -1 throw
        !           114:   THEN drop
        !           115: 
        !           116:   new-device
        !           117: 
        !           118:   \ Parse name, split unit address
        !           119:   fdt-fetch-unit
        !           120:   dup 0 = IF drop drop " /" THEN
        !           121:   40 left-parse-string
        !           122:   \ Set name
        !           123:   device-name
        !           124: 
        !           125:   \ Set unit address
        !           126:   dup IF
        !           127:      " #address-cells" get-parent get-package-property IF
        !           128:         2drop
        !           129:      ELSE
        !           130:         decode-int nip nip
        !           131:        hex-decode-unit
        !           132:        set-unit
        !           133:      THEN
        !           134:   ELSE 2drop THEN
        !           135: 
        !           136:   \ Iterate sub tags
        !           137:   BEGIN
        !           138:     fdt-next-tag dup OF_DT_END_NODE <>
        !           139:   WHILE
        !           140:     dup OF_DT_PROP = IF
        !           141:       \ Found property
        !           142:       drop dup                 ( drop tag, dup addr     : a1 a1 )
        !           143:       dup l@ dup rot 4 +       ( fetch size, stack is   : a1 s s a2)
        !           144:       dup l@ swap 4 +          ( fetch nameid, stack is : a1 s s i a3 )
        !           145:       rot                       ( we now have: a1 s i a3 s )
        !           146:       encode-bytes rot         ( a1 s pa ps i)
        !           147:       fdt-fetch-string         ( a1 s pa ps $pn )
        !           148:       property
        !           149:       + 8 + 3 + fffffffc and
        !           150:     ELSE dup OF_DT_BEGIN_NODE = IF
        !           151:       drop                     ( drop tag )
        !           152:       4 -
        !           153:       fdt-unflatten-node
        !           154:     ELSE
        !           155:       drop -1 throw
        !           156:     THEN THEN
        !           157:   REPEAT drop \ drop tag
        !           158: 
        !           159:   \ Create encode/decode unit
        !           160:   " #address-cells" get-node get-package-property IF ELSE
        !           161:     decode-int dup fdt-create-dec fdt-create-enc 2drop
        !           162:   THEN
        !           163: 
        !           164:   finish-device  
        !           165: ;
        !           166: 
        !           167: \ Start unflattening
        !           168: : fdt-unflatten-tree
        !           169:     fdt-debug IF
        !           170:         ." Unflattening device tree..." cr THEN
        !           171:     fdt-struct fdt-unflatten-node drop
        !           172:     fdt-debug IF
        !           173:         ." Done !" cr THEN
        !           174: ;
        !           175: fdt-unflatten-tree
        !           176: 
        !           177: \ Find memory size
        !           178: : fdt-parse-memory
        !           179:     " /memory" find-device
        !           180:     " reg" get-node get-package-property IF throw -1 THEN
        !           181: 
        !           182:     \ XXX FIXME Assume one entry only in "reg" property for now
        !           183:     decode-phys 2drop decode-phys
        !           184:     my-#address-cells 1 > IF 20 << or THEN
        !           185:     
        !           186:     fdt-debug IF
        !           187:         dup ." Memory size: " . cr
        !           188:     THEN
        !           189:     MIN-RAM-SIZE swap release
        !           190:     2drop device-end
        !           191: ;
        !           192: fdt-parse-memory
        !           193: 
        !           194: 
        !           195: \ Claim fdt memory and reserve map
        !           196: : fdt-claim-reserve
        !           197:     fdt-start
        !           198:     dup dup >fdth_tsize l@ 0 claim drop
        !           199:     dup >fdth_rsvmap_off l@ +
        !           200:     BEGIN
        !           201:         dup dup x@ swap 8 + x@
        !           202:        dup 0 <>
        !           203:     WHILE
        !           204:        fdt-debug IF
        !           205:            2dup swap ." Reserve map entry: " . ." : " . cr
        !           206:        THEN
        !           207:        0 claim drop
        !           208:        10 +
        !           209:     REPEAT drop drop drop
        !           210: ;
        !           211: fdt-claim-reserve 
        !           212: 
        !           213: \ Remaining bits from root.fs
        !           214: 
        !           215: defer (client-exec)
        !           216: defer client-exec
        !           217: 
        !           218: \ defined in slof/fs/client.fs
        !           219: defer callback
        !           220: defer continue-client
        !           221: 
        !           222: : set-chosen ( prop len name len -- )
        !           223:   s" /chosen" find-node set-property ;
        !           224: 
        !           225: : get-chosen ( name len -- [ prop len ] success )
        !           226:   s" /chosen" find-node get-property 0= ;
        !           227: 
        !           228: " /" find-device
        !           229: 
        !           230: new-device
        !           231:   s" aliases" device-name
        !           232: finish-device
        !           233: 
        !           234: new-device
        !           235:   s" options" device-name
        !           236: finish-device
        !           237: 
        !           238: new-device
        !           239:   s" openprom" device-name
        !           240:   s" BootROM" device-type
        !           241: finish-device
        !           242: 
        !           243: new-device 
        !           244: #include <packages.fs>
        !           245: finish-device
        !           246: 
        !           247: : open true ;
        !           248: : close ;
        !           249: 
        !           250: device-end
        !           251: 

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.