nandflash.tcl 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. # ----------------------------------------------------------------------------
  2. # ATMEL Microcontroller
  3. # ----------------------------------------------------------------------------
  4. # Copyright (c) 2015, Atmel Corporation
  5. #
  6. # All rights reserved.
  7. #
  8. # Redistribution and use in source and binary forms, with or without
  9. # modification, are permitted provided that the following conditions are met:
  10. #
  11. # - Redistributions of source code must retain the above copyright notice,
  12. # this list of conditions and the disclaimer below.
  13. #
  14. # Atmel's name may not be used to endorse or promote products derived from
  15. # this software without specific prior written permission.
  16. #
  17. # DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
  18. # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  19. # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  20. # DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
  21. # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  22. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  23. # OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  24. # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  25. # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  26. # EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. # ----------------------------------------------------------------------------
  28. ################################################################################
  29. # Script data
  30. ################################################################################
  31. # DBGU address for rm9200, 9260/9g20, 9261/9g10, 9rl, 9x5
  32. set at91_base_dbgu0 0xfffff200
  33. # DBGU address for 9263, 9g45, sama5d3
  34. set at91_base_dbgu1 0xffffee00
  35. # DBGU address for sama5d4
  36. set at91_base_dbgu2 0xfc069000
  37. set arch_exid_offset 0x44
  38. # arch id
  39. set arch_id_at91sam9g20 0x019905a0
  40. set arch_id_at91sam9g45 0x819b05a0
  41. set arch_id_at91sam9x5 0x819a05a0
  42. set arch_id_at91sam9n12 0x819a07a0
  43. set arch_id_sama5d3 0x8a5c07c0
  44. ## Find out at91sam9x5 variant to load the corresponding dtb file
  45. array set at91sam9x5_variant {
  46. 0x00000000 at91sam9g15
  47. 0x00000001 at91sam9g35
  48. 0x00000002 at91sam9x35
  49. 0x00000003 at91sam9g25
  50. 0x00000004 at91sam9x25
  51. }
  52. ## Find out sama5d3 variant to load the corresponding dtb file
  53. array set sama5d3_variant {
  54. 0x00444300 sama5d31
  55. 0x00414300 sama5d33
  56. 0x00414301 sama5d34
  57. 0x00584300 sama5d35
  58. 0x00004301 sama5d36
  59. }
  60. ## Find out sama5d4 variant
  61. array set sama5d4_variant {
  62. 0x00000001 sama5d41
  63. 0x00000002 sama5d42
  64. 0x00000003 sama5d43
  65. 0x00000004 sama5d44
  66. }
  67. ################################################################################
  68. # proc uboot_env: Convert u-boot variables in a string ready to be flashed
  69. # in the region reserved for environment variables
  70. ################################################################################
  71. proc set_uboot_env {nameOfLstOfVar} {
  72. upvar $nameOfLstOfVar lstOfVar
  73. # sector size is the size defined in u-boot CFG_ENV_SIZE
  74. set sectorSize [expr 0x20000 - 5]
  75. set strEnv [join $lstOfVar "\0"]
  76. while {[string length $strEnv] < $sectorSize} {
  77. append strEnv "\0"
  78. }
  79. # \0 between crc and strEnv is the flag value for redundant environment
  80. set strCrc [binary format i [::vfs::crc $strEnv]]
  81. return "$strCrc\0$strEnv"
  82. }
  83. ################################################################################
  84. proc find_variant_name {boardType} {
  85. global at91_base_dbgu0
  86. global at91_base_dbgu1
  87. global at91_base_dbgu2
  88. global arch_exid_offset
  89. global at91sam9x5_variant
  90. global sama5d3_variant
  91. global sama5d4_variant
  92. set socName "none"
  93. switch $boardType {
  94. at91sam9x5ek {
  95. set exidAddr [expr {$at91_base_dbgu0 + $arch_exid_offset}]
  96. set chip_variant [format "0x%08x" [read_int $exidAddr]]
  97. foreach {key value} [array get at91sam9x5_variant] {
  98. if {$key == $chip_variant} {
  99. set socName "$value"
  100. break;
  101. }
  102. }
  103. }
  104. sama5d3xek {
  105. set exidAddr [expr {$at91_base_dbgu1 + $arch_exid_offset}]
  106. set chip_variant [format "0x%08x" [read_int $exidAddr]]
  107. foreach {key value} [array get sama5d3_variant] {
  108. #puts "-I- === $chip_variant ? $key ($value) ==="
  109. if {$key == $chip_variant} {
  110. set socName "$value"
  111. break;
  112. }
  113. }
  114. }
  115. sama5d3_xplained {
  116. set exidAddr [expr {$at91_base_dbgu1 + $arch_exid_offset}]
  117. set chip_variant [format "0x%08x" [read_int $exidAddr]]
  118. foreach {key value} [array get sama5d3_variant] {
  119. #puts "-I- === $chip_variant ? $key ($value) ==="
  120. if {$key == $chip_variant} {
  121. set socName "$value"
  122. break;
  123. }
  124. }
  125. }
  126. sama5d4ek {
  127. set exidAddr [expr {$at91_base_dbgu2 + $arch_exid_offset}]
  128. set chip_variant [format "0x%08x" [read_int $exidAddr]]
  129. foreach {key value} [array get sama5d4_variant] {
  130. #puts "-I- === $chip_variant ? $key ($value) ==="
  131. if {$key == $chip_variant} {
  132. set socName "$value"
  133. break;
  134. }
  135. }
  136. }
  137. sama5d4_xplained {
  138. set exidAddr [expr {$at91_base_dbgu2 + $arch_exid_offset}]
  139. set chip_variant [format "0x%08x" [read_int $exidAddr]]
  140. foreach {key value} [array get sama5d4_variant] {
  141. #puts "-I- === $chip_variant ? $key ($value) ==="
  142. if {$key == $chip_variant} {
  143. set socName "$value"
  144. break;
  145. }
  146. }
  147. }
  148. }
  149. return "$socName"
  150. }
  151. proc find_variant_ecc {boardType} {
  152. set eccType "none"
  153. switch $boardType {
  154. at91sam9x5ek {
  155. set eccType 0xc0c00405
  156. }
  157. at91sam9n12ek {
  158. set eccType 0xc0c00405
  159. }
  160. sama5d3xek {
  161. set eccType 0xc0902405
  162. }
  163. sama5d3_xplained {
  164. set eccType 0xc0902405
  165. }
  166. sama5d4ek {
  167. set eccType 0xc1e04e07
  168. }
  169. sama5d4_xplained {
  170. set eccType 0xc1e04e07
  171. }
  172. }
  173. puts "-I- === eccType is $eccType ==="
  174. return $eccType
  175. }
  176. proc get_kernel_load_addr {boardType} {
  177. set kernel_load_addr 0x22000000
  178. switch $boardType {
  179. at91sam9m10g45ek {
  180. set kernel_load_addr 0x72000000
  181. }
  182. }
  183. return $kernel_load_addr
  184. }
  185. proc get_dtb_load_addr {boardType} {
  186. set dtb_load_addr 0x21000000
  187. switch $boardType {
  188. at91sam9m10g45ek {
  189. set dtb_load_addr 0x71000000
  190. }
  191. }
  192. return $dtb_load_addr
  193. }
  194. ################################################################################
  195. # Main script: Load the linux demo in NandFlash,
  196. # Update the environment variables
  197. ################################################################################
  198. ################################################################################
  199. # check for proper variable initialization
  200. if {! [info exists boardFamily]} {
  201. puts "-I- === Parsing script arguments ==="
  202. if {! [info exists env(O)]} {
  203. puts "-E- === Binaries path not defined ==="
  204. exit
  205. }
  206. set bootstrapFile "$env(O)/at91bootstrap.bin"
  207. set ubootFile "$env(O)/u-boot.bin"
  208. set kernelFile "$env(O)/zImage"
  209. set rootfsFile "$env(O)/rootfs.ubi"
  210. set build_uboot_env "yes"
  211. set i 1
  212. foreach arg $::argv {
  213. puts "argument $i is $arg"
  214. switch $i {
  215. 4 { set boardFamily $arg }
  216. 5 { set dtbFile "$env(O)/$arg" }
  217. 6 { set videoMode $arg }
  218. }
  219. incr i
  220. }
  221. }
  222. puts "-I- === Board Family is $boardFamily ==="
  223. set pmeccConfig [find_variant_ecc $boardFamily]
  224. ## Now check for the needed files
  225. if {! [file exists $bootstrapFile]} {
  226. puts "-E- === AT91Bootstrap file not found ==="
  227. exit
  228. }
  229. if {! [file exists $ubootFile]} {
  230. puts "-E- === U-Boot file not found ==="
  231. exit
  232. }
  233. if {! [file exists $kernelFile]} {
  234. puts "-E- === Linux kernel file not found ==="
  235. exit
  236. }
  237. if {! [file exists $dtbFile]} {
  238. puts "-E- === Device Tree binary: $dtbFile file not found ==="
  239. exit
  240. }
  241. if {! [file exists $rootfsFile]} {
  242. puts "-E- === Rootfs file not found ==="
  243. exit
  244. }
  245. ## NandFlash Mapping
  246. set bootStrapAddr 0x00000000
  247. set ubootAddr 0x00040000
  248. set ubootEnvAddr 0x000c0000
  249. set dtbAddr 0x00180000
  250. set kernelAddr 0x00200000
  251. set rootfsAddr 0x00800000
  252. ## u-boot variable
  253. set kernelLoadAddr [get_kernel_load_addr $boardFamily]
  254. set dtbLoadAddr [get_dtb_load_addr $boardFamily]
  255. ## NandFlash Mapping
  256. set kernelSize [format "0x%08X" [file size $kernelFile]]
  257. set dtbSize [format "0x%08X" [file size $dtbFile]]
  258. set bootCmd "bootcmd=nand read $dtbLoadAddr $dtbAddr $dtbSize; nand read $kernelLoadAddr $kernelAddr $kernelSize; bootz $kernelLoadAddr - $dtbLoadAddr"
  259. set rootfsSize [format "0x%08X" [file size $rootfsFile]]
  260. lappend u_boot_variables \
  261. "bootdelay=1" \
  262. "baudrate=115200" \
  263. "stdin=serial" \
  264. "stdout=serial" \
  265. "stderr=serial" \
  266. "bootargs=console=ttyS0,115200 mtdparts=atmel_nand:256k(bootstrap)ro,512k(uboot)ro,256k(env),256k(env_redundant),256k(spare),512k(dtb),6M(kernel)ro,-(rootfs) rootfstype=ubifs ubi.mtd=7 root=ubi0:rootfs rw $videoMode" \
  267. "$bootCmd"
  268. ## Additional files to load
  269. set ubootEnvFile "ubootEnvtFileNandFlash.bin"
  270. ## Start flashing procedure ##################################################
  271. puts "-I- === Initialize the NAND access ==="
  272. NANDFLASH::Init
  273. if {$pmeccConfig != "none"} {
  274. puts "-I- === Enable PMECC OS Parameters ==="
  275. NANDFLASH::NandHeaderValue HEADER $pmeccConfig
  276. }
  277. puts "-I- === Erase all the NAND flash blocs and test the erasing ==="
  278. NANDFLASH::EraseAllNandFlash
  279. puts "-I- === Load AT91Bootstrap in the first sector ==="
  280. if {$pmeccConfig != "none"} {
  281. NANDFLASH::SendBootFilePmeccCmd $bootstrapFile
  282. } else {
  283. NANDFLASH::sendBootFile $bootstrapFile
  284. }
  285. puts "-I- === Load u-boot in the next sectors ==="
  286. send_file {NandFlash} "$ubootFile" $ubootAddr 0
  287. if {$build_uboot_env == "yes"} {
  288. puts "-I- === Load the u-boot environment variables ==="
  289. set fh [open "$ubootEnvFile" w]
  290. fconfigure $fh -translation binary
  291. puts -nonewline $fh [set_uboot_env u_boot_variables]
  292. close $fh
  293. send_file {NandFlash} "$ubootEnvFile" $ubootEnvAddr 0
  294. }
  295. puts "-I- === Load the Kernel image and device tree database ==="
  296. send_file {NandFlash} "$dtbFile" $dtbAddr 0
  297. send_file {NandFlash} "$kernelFile" $kernelAddr 0
  298. if {$pmeccConfig != "none"} {
  299. puts "-I- === Enable trimffs ==="
  300. NANDFLASH::NandSetTrimffs 1
  301. }
  302. puts "-I- === Load the linux file system ==="
  303. send_file {NandFlash} "$rootfsFile" $rootfsAddr 0
  304. puts "-I- === DONE. ==="