head.S 9.5 KB

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