CRIS v10: Update rescue head.s
[safe/jmp/linux-2.6] / arch / cris / arch-v10 / boot / rescue / head.S
1 /*
2  * Rescue code, made to reside at the beginning of the
3  * flash-memory. when it starts, it checks a partition
4  * table at the first sector after the rescue sector.
5  * the partition table was generated by the product builder
6  * script and contains offsets, lengths, types and checksums
7  * for each partition that this code should check.
8  *
9  * If any of the checksums fail, we assume the flash is so
10  * corrupt that we cant use it to boot into the ftp flash
11  * loader, and instead we initialize the serial port to
12  * receive a flash-loader and new flash image. we dont include
13  * any flash code here, but just accept a certain amount of
14  * bytes from the serial port and jump into it. the downloaded
15  * code is put in the cache.
16  *
17  * The partitiontable is designed so that it is transparent to
18  * code execution - it has a relative branch opcode in the
19  * beginning that jumps over it. each entry contains extra
20  * data so we can add stuff later.
21  *
22  * Partition table format:
23  *
24  *     Code transparency:
25  *
26  *     2 bytes    [opcode 'nop']
27  *     2 bytes    [opcode 'di']
28  *     4 bytes    [opcode 'ba <offset>', 8-bit or 16-bit version]
29  *     2 bytes    [opcode 'nop', delay slot]
30  *
31  *     Table validation (at +10):
32  *
33  *     2 bytes    [magic/version word for partitiontable - 0xef, 0xbe]
34  *     2 bytes    [length of all entries plus the end marker]
35  *     4 bytes    [checksum for the partitiontable itself]
36  *
37  *     Entries, each with the following format, last has offset -1:
38  *
39  *        4 bytes    [offset in bytes, from start of flash]
40  *        4 bytes    [length in bytes of partition]
41  *        4 bytes    [checksum, simple longword sum]
42  *        2 bytes    [partition type]
43  *        2 bytes    [flags, only bit 0 used, ro/rw = 1/0]
44  *        16 bytes   [reserved for future use]
45  *
46  *     End marker
47  *
48  *        4 bytes    [-1]
49  *
50  *       10 bytes    [0, padding]
51  *
52  * Bit 0 in flags signifies RW or RO. The rescue code only bothers
53  * to check the checksum for RO partitions, since the others will
54  * change their data without updating the checksums. A 1 in bit 0
55  * means RO, 0 means RW. That way, it is possible to set a partition
56  * in RO mode initially, and later mark it as RW, since you can always
57  * write 0's to the flash.
58  *
59  * During the wait for serial input, the status LED will flash so the
60  * user knows something went wrong.
61  *
62  * Copyright (C) 1999-2007 Axis Communications AB
63  */
64
65 #ifdef CONFIG_ETRAX_AXISFLASHMAP
66
67 #define ASSEMBLER_MACROS_ONLY
68 #include <asm/arch/sv_addr_ag.h>
69
70         ;; The partitiontable is looked for at the first sector after the boot
71         ;; sector. Sector size is 65536 bytes in all flashes we use.
72
73 #define PTABLE_START CONFIG_ETRAX_PTABLE_SECTOR
74 #define PTABLE_MAGIC 0xbeef
75
76         ;; The normal Etrax100 on-chip boot ROM does serial boot at 0x380000f0.
77         ;; That is not where we put our downloaded serial boot-code. The length is
78         ;; enough for downloading code that loads the rest of itself (after
79         ;; having setup the DRAM etc). It is the same length as the on-chip
80         ;; ROM loads, so the same host loader can be used to load a rescued
81         ;; product as well as one booted through the Etrax serial boot code.
82
83 #define CODE_START 0x40000000
84 #define CODE_LENGTH 784
85
86 #ifdef CONFIG_ETRAX_RESCUE_SER0
87 #define SERXOFF R_SERIAL0_XOFF
88 #define SERBAUD R_SERIAL0_BAUD
89 #define SERRECC R_SERIAL0_REC_CTRL
90 #define SERRDAT R_SERIAL0_REC_DATA
91 #define SERSTAT R_SERIAL0_STATUS
92 #endif
93 #ifdef CONFIG_ETRAX_RESCUE_SER1
94 #define SERXOFF R_SERIAL1_XOFF
95 #define SERBAUD R_SERIAL1_BAUD
96 #define SERRECC R_SERIAL1_REC_CTRL
97 #define SERRDAT R_SERIAL1_REC_DATA
98 #define SERSTAT R_SERIAL1_STATUS
99 #endif
100 #ifdef CONFIG_ETRAX_RESCUE_SER2
101 #define SERXOFF R_SERIAL2_XOFF
102 #define SERBAUD R_SERIAL2_BAUD
103 #define SERRECC R_SERIAL2_REC_CTRL
104 #define SERRDAT R_SERIAL2_REC_DATA
105 #define SERSTAT R_SERIAL2_STATUS
106 #endif
107 #ifdef CONFIG_ETRAX_RESCUE_SER3
108 #define SERXOFF R_SERIAL3_XOFF
109 #define SERBAUD R_SERIAL3_BAUD
110 #define SERRECC R_SERIAL3_REC_CTRL
111 #define SERRDAT R_SERIAL3_REC_DATA
112 #define SERSTAT R_SERIAL3_STATUS
113 #endif
114
115 #define NOP_DI 0xf025050f
116 #define RAM_INIT_MAGIC 0x56902387
117
118         .text
119
120         ;; This is the entry point of the rescue code
121         ;; 0x80000000 if loaded in flash (as it should be)
122         ;; Since etrax actually starts at address 2 when booting from flash, we
123         ;; put a nop (2 bytes) here first so we dont accidentally skip the di
124
125         nop
126         di
127
128         jump    in_cache        ; enter cached area instead
129 in_cache:
130
131
132         ;; First put a jump test to give a possibility of upgrading the
133         ;; rescue code without erasing/reflashing the sector.
134         ;; We put a longword of -1 here and if it is not -1, we jump using
135         ;; the value as jump target. Since we can always change 1's to 0's
136         ;; without erasing the sector, it is possible to add new
137         ;; code after this and altering the jumptarget in an upgrade.
138
139 jtcd:   move.d  [jumptarget], $r0
140         cmp.d   0xffffffff, $r0
141         beq     no_newjump
142         nop
143
144         jump    [$r0]
145
146 jumptarget:
147         .dword  0xffffffff      ; can be overwritten later to insert new code
148
149 no_newjump:
150 #ifdef CONFIG_ETRAX_ETHERNET
151         ;; Start MII clock to make sure it is running when tranceiver is reset
152         move.d 0x3, $r0    ; enable = on, phy = mii_clk
153         move.d $r0, [R_NETWORK_GEN_CONFIG]
154 #endif
155
156         ;; We need to setup the bus registers before we start using the DRAM
157 #include "../../lib/dram_init.S"
158
159         ;; we now should go through the checksum-table and check the listed
160         ;; partitions for errors.
161
162         move.d  PTABLE_START, $r3
163         move.d  [$r3], $r0
164         cmp.d   NOP_DI, $r0     ; make sure the nop/di is there...
165         bne     do_rescue
166         nop
167
168         ;; skip the code transparency block (10 bytes).
169
170         addq    10, $r3
171
172         ;; check for correct magic
173
174         move.w  [$r3+], $r0
175         cmp.w   PTABLE_MAGIC, $r0
176         bne     do_rescue       ; didn't recognize - trig rescue
177         nop
178
179         ;; check for correct ptable checksum
180
181         movu.w  [$r3+], $r2     ; ptable length
182         move.d  $r2, $r8        ; save for later, length of total ptable
183         addq    28, $r8         ; account for the rest
184         move.d  [$r3+], $r4     ; ptable checksum
185         move.d  $r3, $r1
186         jsr     checksum        ; r1 source, r2 length, returns in r0
187
188         cmp.d   $r0, $r4
189         bne     do_rescue       ; didn't match - trig rescue
190         nop
191
192         ;; ptable is ok. validate each entry.
193
194         moveq   -1, $r7
195
196 ploop:  move.d  [$r3+], $r1     ; partition offset (from ptable start)
197         bne     notfirst        ; check if it is the partition containing ptable
198         nop                     ; yes..
199         move.d  $r8, $r1        ; for its checksum check, skip the ptable
200         move.d  [$r3+], $r2     ; partition length
201         sub.d   $r8, $r2        ; minus the ptable length
202         ba      bosse
203         nop
204 notfirst:
205         cmp.d   -1, $r1         ; the end of the ptable ?
206         beq     flash_ok        ;   if so, the flash is validated
207         move.d  [$r3+], $r2     ; partition length
208 bosse:  move.d  [$r3+], $r5     ; checksum
209         move.d  [$r3+], $r4     ; type and flags
210         addq    16, $r3         ; skip the reserved bytes
211         btstq   16, $r4         ; check ro flag
212         bpl     ploop           ;   rw partition, skip validation
213         nop
214         btstq   17, $r4         ; check bootable flag
215         bpl     1f
216         nop
217         move.d  $r1, $r7        ; remember boot partition offset
218 1:
219         add.d   PTABLE_START, $r1
220
221         jsr     checksum        ; checksum the partition
222
223         cmp.d   $r0, $r5
224         beq     ploop           ; checksums matched, go to next entry
225         nop
226
227         ;; otherwise fall through to the rescue code.
228
229 do_rescue:
230         ;; setup port PA and PB default initial directions and data
231         ;; (so we can flash LEDs, and so that DTR and others are set)
232
233         move.b  CONFIG_ETRAX_DEF_R_PORT_PA_DIR, $r0
234         move.b  $r0, [R_PORT_PA_DIR]
235         move.b  CONFIG_ETRAX_DEF_R_PORT_PA_DATA, $r0
236         move.b  $r0, [R_PORT_PA_DATA]
237
238         move.b  CONFIG_ETRAX_DEF_R_PORT_PB_DIR, $r0
239         move.b  $r0, [R_PORT_PB_DIR]
240         move.b  CONFIG_ETRAX_DEF_R_PORT_PB_DATA, $r0
241         move.b  $r0, [R_PORT_PB_DATA]
242
243         ;; setup the serial port at 115200 baud
244
245         moveq   0, $r0
246         move.d  $r0, [SERXOFF]
247
248         move.b  0x99, $r0
249         move.b  $r0, [SERBAUD]  ; 115.2kbaud for both transmit and receive
250
251         move.b  0x40, $r0       ; rec enable
252         move.b  $r0, [SERRECC]
253
254         moveq   0, $r1          ; "timer" to clock out a LED red flash
255         move.d  CODE_START, $r3 ; destination counter
256         movu.w  CODE_LENGTH, $r4; length
257
258 wait_ser:
259         addq    1, $r1
260 #ifndef CONFIG_ETRAX_NO_LEDS
261 #ifdef CONFIG_ETRAX_PA_LEDS
262         move.b  CONFIG_ETRAX_DEF_R_PORT_PA_DATA, $r2
263 #endif
264 #ifdef CONFIG_ETRAX_PB_LEDS
265         move.b  CONFIG_ETRAX_DEF_R_PORT_PB_DATA, $r2
266 #endif
267         move.d  (1 << CONFIG_ETRAX_LED1R) | (1 << CONFIG_ETRAX_LED2R), $r0
268         btstq   16, $r1
269         bpl     1f
270         nop
271         or.d    $r0, $r2        ; set bit
272         ba      2f
273         nop
274 1:      not     $r0             ; clear bit
275         and.d   $r0, $r2
276 2:
277 #ifdef CONFIG_ETRAX_PA_LEDS
278         move.b  $r2, [R_PORT_PA_DATA]
279 #endif
280 #ifdef CONFIG_ETRAX_PB_LEDS
281         move.b  $r2, [R_PORT_PB_DATA]
282 #endif
283 #ifdef CONFIG_ETRAX_90000000_LEDS
284         move.b  $r2, [0x90000000]
285 #endif
286 #endif
287
288         ;; check if we got something on the serial port
289
290         move.b  [SERSTAT], $r0
291         btstq   0, $r0          ; data_avail
292         bpl     wait_ser
293         nop
294
295         ;; got something - copy the byte and loop
296
297         move.b  [SERRDAT], $r0
298         move.b  $r0, [$r3+]
299
300         subq    1, $r4          ; decrease length
301         bne     wait_ser
302         nop
303
304         ;; jump into downloaded code
305
306         move.d  RAM_INIT_MAGIC, $r8     ; Tell next product that DRAM is
307                                         ; initialized
308         jump    CODE_START
309
310 flash_ok:
311         ;; check r7, which contains either -1 or the partition to boot from
312
313         cmp.d   -1, $r7
314         bne     1f
315         nop
316         move.d  PTABLE_START, $r7; otherwise use the ptable start
317 1:
318         move.d  RAM_INIT_MAGIC, $r8     ; Tell next product that DRAM is
319                                         ; initialized
320         jump    $r7             ; boot!
321
322
323         ;; Helper subroutines
324
325         ;; Will checksum by simple addition
326         ;; r1 - source
327         ;; r2 - length in bytes
328         ;; result will be in r0
329 checksum:
330         moveq   0, $r0
331         moveq   CONFIG_ETRAX_FLASH1_SIZE, $r6
332
333         ;; If the first physical flash memory is exceeded wrap to the second one
334         btstq   26, $r1         ; Are we addressing first flash?
335         bpl     1f
336         nop
337         clear.d $r6
338
339 1:      test.d  $r6             ; 0 = no wrapping
340         beq     2f
341         nop
342         lslq    20, $r6         ; Convert MB to bytes
343         sub.d   $r1, $r6
344
345 2:      addu.b  [$r1+], $r0
346         subq    1, $r6          ; Flash memory left
347         beq     3f
348         subq    1, $r2          ; Length left
349         bne     2b
350         nop
351         ret
352         nop
353
354 3:      move.d  MEM_CSE1_START, $r1 ; wrap to second flash
355         ba      2b
356         nop
357
358 #endif