aboutsummaryrefslogtreecommitdiff
path: root/board/atmel/nandflash.tcl
blob: 3955b345a44cc89e5cca7a91a2c0b8474cd2d473 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
# ----------------------------------------------------------------------------
#         ATMEL Microcontroller
# ----------------------------------------------------------------------------
# Copyright (c) 2015, Atmel Corporation
#
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# - Redistributions of source code must retain the above copyright notice,
# this list of conditions and the disclaimer below.
#
# Atmel's name may not be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
# DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
# OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# ----------------------------------------------------------------------------

################################################################################
#  Script data
################################################################################
# DBGU address for rm9200, 9260/9g20, 9261/9g10, 9rl, 9x5
set at91_base_dbgu0 0xfffff200
# DBGU address for 9263, 9g45, sama5d3
set at91_base_dbgu1 0xffffee00
# DBGU address for sama5d4
set at91_base_dbgu2 0xfc069000

set arch_exid_offset 0x44

# arch id
set arch_id_at91sam9g20 0x019905a0
set arch_id_at91sam9g45 0x819b05a0
set arch_id_at91sam9x5  0x819a05a0
set arch_id_at91sam9n12 0x819a07a0
set arch_id_sama5d3     0x8a5c07c0

## Find out at91sam9x5 variant to load the corresponding dtb file
array set at91sam9x5_variant {
   0x00000000 at91sam9g15
   0x00000001 at91sam9g35
   0x00000002 at91sam9x35
   0x00000003 at91sam9g25
   0x00000004 at91sam9x25
}

## Find out sama5d3 variant to load the corresponding dtb file
array set sama5d3_variant {
   0x00444300 sama5d31
   0x00414300 sama5d33
   0x00414301 sama5d34
   0x00584300 sama5d35
   0x00004301 sama5d36
}

## Find out sama5d4 variant
array set sama5d4_variant {
   0x00000001 sama5d41
   0x00000002 sama5d42
   0x00000003 sama5d43
   0x00000004 sama5d44
}

################################################################################
#  proc uboot_env: Convert u-boot variables in a string ready to be flashed
#                  in the region reserved for environment variables
################################################################################
proc set_uboot_env {nameOfLstOfVar} {
    upvar $nameOfLstOfVar lstOfVar

    # sector size is the size defined in u-boot CFG_ENV_SIZE
    set sectorSize [expr 0x20000 - 5]

    set strEnv [join $lstOfVar "\0"]
    while {[string length $strEnv] < $sectorSize} {
        append strEnv "\0"
    }
    # \0 between crc and strEnv is the flag value for redundant environment
    set strCrc [binary format i [::vfs::crc $strEnv]]
    return "$strCrc\0$strEnv"
}

################################################################################
proc find_variant_name {boardType} {
   global at91_base_dbgu0
   global at91_base_dbgu1
   global at91_base_dbgu2
   global arch_exid_offset
   global at91sam9x5_variant
   global sama5d3_variant
   global sama5d4_variant
   set socName "none"

   switch $boardType {
      at91sam9x5ek {
         set exidAddr [expr {$at91_base_dbgu0 + $arch_exid_offset}]
         set chip_variant [format "0x%08x" [read_int $exidAddr]]

         foreach {key value} [array get at91sam9x5_variant] {
            if {$key == $chip_variant} {
               set socName "$value"
               break;
            }
         }
      }
      sama5d3xek {
         set exidAddr [expr {$at91_base_dbgu1 + $arch_exid_offset}]
         set chip_variant [format "0x%08x" [read_int $exidAddr]]

         foreach {key value} [array get sama5d3_variant] {
            #puts "-I- === $chip_variant ? $key ($value) ==="
            if {$key == $chip_variant} {
               set socName "$value"
               break;
            }
         }
      }
      sama5d3_xplained {
         set exidAddr [expr {$at91_base_dbgu1 + $arch_exid_offset}]
         set chip_variant [format "0x%08x" [read_int $exidAddr]]

         foreach {key value} [array get sama5d3_variant] {
            #puts "-I- === $chip_variant ? $key ($value) ==="
            if {$key == $chip_variant} {
               set socName "$value"
               break;
            }
         }
      }
      sama5d4ek {
         set exidAddr [expr {$at91_base_dbgu2 + $arch_exid_offset}]
         set chip_variant [format "0x%08x" [read_int $exidAddr]]

         foreach {key value} [array get sama5d4_variant] {
            #puts "-I- === $chip_variant ? $key ($value) ==="
            if {$key == $chip_variant} {
               set socName "$value"
               break;
            }
         }
      }
      sama5d4_xplained {
         set exidAddr [expr {$at91_base_dbgu2 + $arch_exid_offset}]
         set chip_variant [format "0x%08x" [read_int $exidAddr]]

         foreach {key value} [array get sama5d4_variant] {
            #puts "-I- === $chip_variant ? $key ($value) ==="
            if {$key == $chip_variant} {
               set socName "$value"
               break;
            }
         }
      }
   }

   return "$socName"
}

proc find_variant_ecc {boardType} {
   set eccType "none"

   switch $boardType {
      at91sam9x5ek {
         set eccType 0xc0c00405
      }
      at91sam9n12ek {
         set eccType 0xc0c00405
      }
      sama5d3xek {
         set eccType 0xc0902405
      }
      sama5d3_xplained {
         set eccType 0xc0902405
      }
      sama5d4ek {
         set eccType 0xc1e04e07
      }
      sama5d4_xplained {
         set eccType 0xc1e04e07
      }
   }

   puts "-I- === eccType is $eccType ==="
   return $eccType
}

proc get_kernel_load_addr {boardType} {
   set kernel_load_addr 0x22000000

   switch $boardType {
      at91sam9m10g45ek {
         set kernel_load_addr 0x72000000
      }
   }

   return $kernel_load_addr
}

proc get_dtb_load_addr {boardType} {
   set dtb_load_addr 0x21000000

   switch $boardType {
      at91sam9m10g45ek {
         set dtb_load_addr 0x71000000
      }
   }

   return $dtb_load_addr
}

################################################################################
#  Main script: Load the linux demo in NandFlash,
#               Update the environment variables
################################################################################

################################################################################

# check for proper variable initialization
if {! [info exists boardFamily]} {
   puts "-I- === Parsing script arguments ==="
   if {! [info exists env(O)]} {
      puts "-E- === Binaries path not defined ==="
      exit
   }

   set bootstrapFile   "$env(O)/at91bootstrap.bin"
   set ubootFile       "$env(O)/u-boot.bin"
   set kernelFile      "$env(O)/zImage"
   set rootfsFile      "$env(O)/rootfs.ubi"
   set build_uboot_env "yes"

   set i 1
   foreach arg $::argv {
      puts "argument $i is $arg"
      switch $i {
         4 { set boardFamily $arg }
         5 { set dtbFile "$env(O)/$arg" }
         6 { set videoMode $arg }
      }
      incr i
    }
}

puts "-I- === Board Family is $boardFamily ==="

set pmeccConfig [find_variant_ecc $boardFamily]

## Now check for the needed files
if {! [file exists $bootstrapFile]} {
   puts "-E- === AT91Bootstrap file not found ==="
   exit
}

if {! [file exists $ubootFile]} {
   puts "-E- === U-Boot file not found ==="
   exit
}

if {! [file exists $kernelFile]} {
   puts "-E- === Linux kernel file not found ==="
   exit
}

if {! [file exists $dtbFile]} {
   puts "-E- === Device Tree binary: $dtbFile file not found ==="
   exit
}

if {! [file exists $rootfsFile]} {
   puts "-E- === Rootfs file not found ==="
   exit
}

## NandFlash Mapping
set bootStrapAddr	0x00000000
set ubootAddr		0x00040000
set ubootEnvAddr	0x000c0000
set dtbAddr		0x00180000
set kernelAddr		0x00200000
set rootfsAddr		0x00800000

## u-boot variable
set kernelLoadAddr [get_kernel_load_addr $boardFamily]
set dtbLoadAddr	[get_dtb_load_addr $boardFamily]

## NandFlash Mapping
set kernelSize	[format "0x%08X" [file size $kernelFile]]
set dtbSize	[format "0x%08X" [file size $dtbFile]]
set bootCmd "bootcmd=nand read $dtbLoadAddr $dtbAddr $dtbSize; nand read $kernelLoadAddr $kernelAddr $kernelSize; bootz $kernelLoadAddr - $dtbLoadAddr"
set rootfsSize	[format "0x%08X" [file size $rootfsFile]]

lappend u_boot_variables \
    "bootdelay=1" \
    "baudrate=115200" \
    "stdin=serial" \
    "stdout=serial" \
    "stderr=serial" \
    "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" \
    "$bootCmd"

## Additional files to load
set ubootEnvFile	"ubootEnvtFileNandFlash.bin"


##  Start flashing procedure  ##################################################
puts "-I- === Initialize the NAND access ==="
NANDFLASH::Init

if {$pmeccConfig != "none"} {
   puts "-I- === Enable PMECC OS Parameters ==="
   NANDFLASH::NandHeaderValue HEADER $pmeccConfig
}

puts "-I- === Erase all the NAND flash blocs and test the erasing ==="
NANDFLASH::EraseAllNandFlash

puts "-I- === Load AT91Bootstrap in the first sector ==="
if {$pmeccConfig != "none"} {
   NANDFLASH::SendBootFilePmeccCmd $bootstrapFile
} else {
   NANDFLASH::sendBootFile $bootstrapFile
}

puts "-I- === Load u-boot in the next sectors ==="
send_file {NandFlash} "$ubootFile" $ubootAddr 0 

if {$build_uboot_env == "yes"} {
   puts "-I- === Load the u-boot environment variables ==="
   set fh [open "$ubootEnvFile" w]
   fconfigure $fh -translation binary
   puts -nonewline $fh [set_uboot_env u_boot_variables]
   close $fh
   send_file {NandFlash} "$ubootEnvFile" $ubootEnvAddr 0
}

puts "-I- === Load the Kernel image and device tree database ==="
send_file {NandFlash} "$dtbFile" $dtbAddr 0
send_file {NandFlash} "$kernelFile" $kernelAddr 0

if {$pmeccConfig != "none"} {
   puts "-I- === Enable trimffs ==="
   NANDFLASH::NandSetTrimffs 1
}

puts "-I- === Load the linux file system ==="
send_file {NandFlash} "$rootfsFile" $rootfsAddr 0

puts "-I- === DONE. ==="