traveo2_detect_geometry.cfg 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. #
  2. # Copyright (C) <2019-2021>
  3. # <Cypress Semiconductor Corporation (an Infineon company)>
  4. #
  5. source [find target/mxs40/cympn_parser.cfg]
  6. #
  7. # extracts number of large and small sectors from passed geometry
  8. #
  9. # arguments:
  10. # geometry - the geometry to be parsed
  11. # large_sect_tbl - the table that represents large sector coding
  12. # small_sect_tbl - the table that represents small sector coding
  13. #
  14. # return:
  15. # number of large and small sectors in form [Large_sect_count << 16 | Small_sect_count]
  16. #
  17. proc get_sectors_from_geom { geometry large_sect_tbl small_sect_tbl } {
  18. set geometry_msk [expr {$geometry & 0x07}]
  19. set num_sup_comb [llength $large_sect_tbl]
  20. if { $geometry_msk >= $num_sup_comb } {
  21. puts stderr "Error: Cannot get sector count for Flash Geometry = $geometry_msk"
  22. return -1
  23. }
  24. set large_sect_num [lindex $large_sect_tbl $geometry_msk]
  25. set small_sect_num [lindex $small_sect_tbl $geometry_msk]
  26. set ret_value [expr {($large_sect_num << 16) | $small_sect_num}]
  27. return $ret_value
  28. }
  29. #
  30. # converts flash size to geometry
  31. #
  32. # arguments:
  33. # size_kb - the flash size in kb
  34. # size_tbl - the table that represents flash size to geometry coding
  35. #
  36. # return:
  37. # flash geometry that represents passed flash size
  38. #
  39. proc get_geom_from_size {size_kb size_tbl} {
  40. set num_elements [llength $size_tbl]
  41. set geometry -1
  42. for {set i 0} { $i < $num_elements } {incr i} {
  43. set size [lindex $size_tbl $i]
  44. if {$size_kb == $size} {
  45. set geometry $i
  46. break
  47. }
  48. }
  49. return $geometry
  50. }
  51. #
  52. # detects flash geometry of TRAVEO™II devices
  53. #
  54. # tryies to get TRAVEO™II geometry from the:
  55. # 1. $MAIN_FLASH_SIZE_OVERRIDE and $WORK_FLASH_SIZE_OVERRIDE if defined
  56. # 2. Flash GEOMETRY register
  57. # 3. UDD
  58. #
  59. # arguments:
  60. # main_reg_name - the name of main region to be set
  61. # work_reg_name - the name of work region to be set
  62. #
  63. proc traveo2_detect_geometry { target_arch main_reg_name work_reg_name} {
  64. set tgt [target current]
  65. set CHIPNAME [string range ${tgt} 0 [expr {[string first "." ${tgt}] - 1}]]
  66. global ${CHIPNAME}::MAIN_FLASH_SIZE_OVERRIDE
  67. global ${CHIPNAME}::WORK_FLASH_SIZE_OVERRIDE
  68. set detected_main_sectors -1
  69. set detected_work_sectors -1
  70. # main / work size where index corresponds to flash geometry
  71. set main_size_kb_tbl { 576 1088 2112 4160 6336 8384 }
  72. set work_size_kb_tbl { 0 64 96 128 256 512 }
  73. # main flash geometry decoding
  74. # main size kb 576 1088 2112 4160 6336 8384
  75. set main_large_sect_num_tbl { 14 30 62 126 190 254 }
  76. set main_small_sect_num_tbl { 16 16 16 16 32 32 }
  77. # work flash geometry decoding
  78. # work size kb 0 64 96 128 256 512
  79. set work_large_sect_num_tbl { 0 24 36 48 96 192 }
  80. set work_small_sect_num_tbl { 0 128 192 256 512 1024 }
  81. # 1. Checking ${CHIPNAME}::MAIN_FLASH_SIZE_OVERRIDE / ${CHIPNAME}::WORK_FLASH_SIZE_OVERRIDE
  82. if { [info exists ${CHIPNAME}::MAIN_FLASH_SIZE_OVERRIDE] } {
  83. if { [set ${CHIPNAME}::MAIN_FLASH_SIZE_OVERRIDE] } {
  84. set detected_main_sectors [set ${CHIPNAME}::MAIN_FLASH_SIZE_OVERRIDE]
  85. #each large sector conatins 32 KB
  86. set m_size_large [expr {([set ${CHIPNAME}::MAIN_FLASH_SIZE_OVERRIDE] >> 16) << 5}]
  87. #each small sector conatins 8 KB
  88. set m_size_small [expr {([set ${CHIPNAME}::MAIN_FLASH_SIZE_OVERRIDE] & 0xFFFF) << 3}]
  89. echo "** Use overriden Main Flash size, kb: [expr {$m_size_large + $m_size_small}]"
  90. }
  91. }
  92. if { [info exists ${CHIPNAME}::WORK_FLASH_SIZE_OVERRIDE] } {
  93. if { [set ${CHIPNAME}::WORK_FLASH_SIZE_OVERRIDE] } {
  94. set detected_work_sectors [set ${CHIPNAME}::WORK_FLASH_SIZE_OVERRIDE]
  95. #each large sector conatins 2 KB
  96. set w_size_large [expr {([set ${CHIPNAME}::WORK_FLASH_SIZE_OVERRIDE] >> 16) << 1}]
  97. #each small sector conatins 128 B
  98. set w_size_small [expr {([set ${CHIPNAME}::WORK_FLASH_SIZE_OVERRIDE] & 0xFFFF) >> 3}]
  99. echo "** Use overriden Work Flash size, kb: [expr {$w_size_large + $w_size_small}]"
  100. }
  101. }
  102. # print MPN
  103. set main_work_size [cyp_get_mpn_work_main_size "traveo2"]
  104. set flash_geom 0
  105. if { $detected_main_sectors == -1 || $detected_work_sectors == -1 } {
  106. # 2. Use UDD if flash size is not overriden
  107. if { [llength $main_work_size] == 2} {
  108. if { $detected_main_sectors == -1 } {
  109. set main_size [lindex $main_work_size 0]
  110. echo "** Detected Main Flash size, kb: $main_size"
  111. set geom [get_geom_from_size $main_size $main_size_kb_tbl]
  112. if { $geom == -1 } {
  113. puts stderr "Error: Cannot get geometry for main size kb = $main_size"
  114. return
  115. }
  116. set flash_geom [expr {($flash_geom & 0xF8) | $geom} ]
  117. }
  118. if { $detected_work_sectors == -1 } {
  119. set work_size [lindex $main_work_size 1]
  120. echo "** Detected Work Flash size, kb: $work_size"
  121. set geom [get_geom_from_size $work_size $work_size_kb_tbl]
  122. if { $geom == -1 } {
  123. puts stderr "Error: Cannot get geometry for work size kb = $work_size"
  124. return
  125. }
  126. set flash_geom [expr {($flash_geom & 0xC7) | ($geom << 3)}]
  127. }
  128. }
  129. if {!$flash_geom} {
  130. # 3. Read Flash GEOMETRY register if main/works sectors are not detected yet
  131. set MEM_SPCIF3_GEOMETRY 0x4024F00C
  132. catch { set flash_geom [expr {[mrw $MEM_SPCIF3_GEOMETRY] & 0x3F}] }
  133. if {$flash_geom} {
  134. if { $detected_main_sectors == -1 } {
  135. set m_size [lindex $main_size_kb_tbl [expr {$flash_geom & 0x07}]]
  136. echo "** Detected Main Flash size from geometry register, kb: $m_size"
  137. }
  138. if { $detected_work_sectors == -1 } {
  139. set w_size [lindex $work_size_kb_tbl [expr {$flash_geom >> 3}]]
  140. echo "** Detected Work Flash size from geometry register, kb: $w_size"
  141. }
  142. }
  143. }
  144. if { !$flash_geom} {
  145. puts stderr "Error: Flash geometry was not detected"
  146. return
  147. }
  148. # get main/works sectors from geometry if they are not detected yet
  149. if { $detected_main_sectors == -1 } {
  150. set detected_main_sectors [get_sectors_from_geom $flash_geom $main_large_sect_num_tbl $main_small_sect_num_tbl]
  151. if { $detected_main_sectors == -1 } {
  152. puts stderr "Error: Cannot get sectors for main flash from geometry = $flash_geom"
  153. return
  154. }
  155. }
  156. if { $detected_work_sectors == -1 } {
  157. set work_geom [expr {$flash_geom >> 3}]
  158. set detected_work_sectors [get_sectors_from_geom $work_geom $work_large_sect_num_tbl $work_small_sect_num_tbl]
  159. if { $detected_work_sectors == -1 } {
  160. puts stderr "Error: Cannot get sectors for work flash from geometry = $work_geom"
  161. return
  162. }
  163. }
  164. }
  165. if { $detected_main_sectors == -1 } {
  166. puts stderr "Error: Main Flash geometry was not detected"
  167. return
  168. }
  169. if { $detected_work_sectors == -1 } {
  170. puts stderr "Error: Work Flash geometry was not detected"
  171. return
  172. }
  173. if { ${main_reg_name} != "" } { traveo2 set_region_size ${main_reg_name} $detected_main_sectors }
  174. if { ${work_reg_name} != "" } { traveo2 set_region_size ${work_reg_name} $detected_work_sectors }
  175. }