head_v10.S 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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 can't 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. #ifdef CONFIG_ETRAX_AXISFLASHMAP
  65. #define ASSEMBLER_MACROS_ONLY
  66. #include <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.
  73. ;; The length is enough for downloading code that loads the rest
  74. ;; of itself (after having setup the DRAM etc).
  75. ;; It is the same length as the on-chip ROM loads, so the same
  76. ;; host loader can be used to load a rescued product as well as
  77. ;; one booted through the Etrax serial boot code.
  78. #define CODE_START 0x40000000
  79. #define CODE_LENGTH 784
  80. #ifdef CONFIG_ETRAX_RESCUE_SER0
  81. #define SERXOFF R_SERIAL0_XOFF
  82. #define SERBAUD R_SERIAL0_BAUD
  83. #define SERRECC R_SERIAL0_REC_CTRL
  84. #define SERRDAT R_SERIAL0_REC_DATA
  85. #define SERSTAT R_SERIAL0_STATUS
  86. #endif
  87. #ifdef CONFIG_ETRAX_RESCUE_SER1
  88. #define SERXOFF R_SERIAL1_XOFF
  89. #define SERBAUD R_SERIAL1_BAUD
  90. #define SERRECC R_SERIAL1_REC_CTRL
  91. #define SERRDAT R_SERIAL1_REC_DATA
  92. #define SERSTAT R_SERIAL1_STATUS
  93. #endif
  94. #ifdef CONFIG_ETRAX_RESCUE_SER2
  95. #define SERXOFF R_SERIAL2_XOFF
  96. #define SERBAUD R_SERIAL2_BAUD
  97. #define SERRECC R_SERIAL2_REC_CTRL
  98. #define SERRDAT R_SERIAL2_REC_DATA
  99. #define SERSTAT R_SERIAL2_STATUS
  100. #endif
  101. #ifdef CONFIG_ETRAX_RESCUE_SER3
  102. #define SERXOFF R_SERIAL3_XOFF
  103. #define SERBAUD R_SERIAL3_BAUD
  104. #define SERRECC R_SERIAL3_REC_CTRL
  105. #define SERRDAT R_SERIAL3_REC_DATA
  106. #define SERSTAT R_SERIAL3_STATUS
  107. #endif
  108. #define NOP_DI 0xf025050f
  109. #define RAM_INIT_MAGIC 0x56902387
  110. .text
  111. ;; This is the entry point of the rescue code
  112. ;; 0x80000000 if loaded in flash (as it should be)
  113. ;; Since etrax actually starts at address 2 when booting from flash, we
  114. ;; put a nop (2 bytes) here first so we dont accidentally skip the di
  115. nop
  116. di
  117. jump in_cache ; enter cached area instead
  118. in_cache:
  119. ;; First put a jump test to give a possibility of upgrading the
  120. ;; rescue code without erasing/reflashing the sector.
  121. ;; We put a longword of -1 here and if it is not -1, we jump using
  122. ;; the value as jump target. Since we can always change 1's to 0's
  123. ;; without erasing the sector, it is possible to add new
  124. ;; code after this and altering the jumptarget in an upgrade.
  125. jtcd: move.d [jumptarget], $r0
  126. cmp.d 0xffffffff, $r0
  127. beq no_newjump
  128. nop
  129. jump [$r0]
  130. jumptarget:
  131. .dword 0xffffffff ; can be overwritten later to insert new code
  132. no_newjump:
  133. #ifdef CONFIG_ETRAX_ETHERNET
  134. ;; Start MII clock to make sure it is running when tranceiver is reset
  135. move.d 0x3, $r0 ; enable = on, phy = mii_clk
  136. move.d $r0, [R_NETWORK_GEN_CONFIG]
  137. #endif
  138. ;; We need to setup the bus registers before we start using the DRAM
  139. #include "../../../arch-v10/lib/dram_init.S"
  140. ;; we now should go through the checksum-table and check the listed
  141. ;; partitions for errors.
  142. move.d PTABLE_START, $r3
  143. move.d [$r3], $r0
  144. cmp.d NOP_DI, $r0 ; make sure the nop/di is there...
  145. bne do_rescue
  146. nop
  147. ;; skip the code transparency block (10 bytes).
  148. addq 10, $r3
  149. ;; check for correct magic
  150. move.w [$r3+], $r0
  151. cmp.w PTABLE_MAGIC, $r0
  152. bne do_rescue ; didn't recognize - trig rescue
  153. nop
  154. ;; check for correct ptable checksum
  155. movu.w [$r3+], $r2 ; ptable length
  156. move.d $r2, $r8 ; save for later, length of total ptable
  157. addq 28, $r8 ; account for the rest
  158. move.d [$r3+], $r4 ; ptable checksum
  159. move.d $r3, $r1
  160. jsr checksum ; r1 source, r2 length, returns in r0
  161. cmp.d $r0, $r4
  162. bne do_rescue ; didn't match - trig rescue
  163. nop
  164. ;; ptable is ok. validate each entry.
  165. moveq -1, $r7
  166. ploop: move.d [$r3+], $r1 ; partition offset (from ptable start)
  167. bne notfirst ; check if it is the partition containing ptable
  168. nop ; yes..
  169. move.d $r8, $r1 ; for its checksum check, skip the ptable
  170. move.d [$r3+], $r2 ; partition length
  171. sub.d $r8, $r2 ; minus the ptable length
  172. ba bosse
  173. nop
  174. notfirst:
  175. cmp.d -1, $r1 ; the end of the ptable ?
  176. beq flash_ok ; if so, the flash is validated
  177. move.d [$r3+], $r2 ; partition length
  178. bosse: move.d [$r3+], $r5 ; checksum
  179. move.d [$r3+], $r4 ; type and flags
  180. addq 16, $r3 ; skip the reserved bytes
  181. btstq 16, $r4 ; check ro flag
  182. bpl ploop ; rw partition, skip validation
  183. nop
  184. btstq 17, $r4 ; check bootable flag
  185. bpl 1f
  186. nop
  187. move.d $r1, $r7 ; remember boot partition offset
  188. 1:
  189. add.d PTABLE_START, $r1
  190. jsr checksum ; checksum the partition
  191. cmp.d $r0, $r5
  192. beq ploop ; checksums matched, go to next entry
  193. nop
  194. ;; otherwise fall through to the rescue code.
  195. do_rescue:
  196. ;; setup port PA and PB default initial directions and data
  197. ;; (so we can flash LEDs, and so that DTR and others are set)
  198. move.b CONFIG_ETRAX_DEF_R_PORT_PA_DIR, $r0
  199. move.b $r0, [R_PORT_PA_DIR]
  200. move.b CONFIG_ETRAX_DEF_R_PORT_PA_DATA, $r0
  201. move.b $r0, [R_PORT_PA_DATA]
  202. move.b CONFIG_ETRAX_DEF_R_PORT_PB_DIR, $r0
  203. move.b $r0, [R_PORT_PB_DIR]
  204. move.b CONFIG_ETRAX_DEF_R_PORT_PB_DATA, $r0
  205. move.b $r0, [R_PORT_PB_DATA]
  206. ;; setup the serial port at 115200 baud
  207. moveq 0, $r0
  208. move.d $r0, [SERXOFF]
  209. move.b 0x99, $r0
  210. move.b $r0, [SERBAUD] ; 115.2kbaud for both transmit and receive
  211. move.b 0x40, $r0 ; rec enable
  212. move.b $r0, [SERRECC]
  213. moveq 0, $r1 ; "timer" to clock out a LED red flash
  214. move.d CODE_START, $r3 ; destination counter
  215. movu.w CODE_LENGTH, $r4; length
  216. wait_ser:
  217. addq 1, $r1
  218. #ifndef CONFIG_ETRAX_NO_LEDS
  219. #ifdef CONFIG_ETRAX_PA_LEDS
  220. move.b CONFIG_ETRAX_DEF_R_PORT_PA_DATA, $r2
  221. #endif
  222. #ifdef CONFIG_ETRAX_PB_LEDS
  223. move.b CONFIG_ETRAX_DEF_R_PORT_PB_DATA, $r2
  224. #endif
  225. move.d (1 << CONFIG_ETRAX_LED1R) | (1 << CONFIG_ETRAX_LED2R), $r0
  226. btstq 16, $r1
  227. bpl 1f
  228. nop
  229. or.d $r0, $r2 ; set bit
  230. ba 2f
  231. nop
  232. 1: not $r0 ; clear bit
  233. and.d $r0, $r2
  234. 2:
  235. #ifdef CONFIG_ETRAX_PA_LEDS
  236. move.b $r2, [R_PORT_PA_DATA]
  237. #endif
  238. #ifdef CONFIG_ETRAX_PB_LEDS
  239. move.b $r2, [R_PORT_PB_DATA]
  240. #endif
  241. #endif
  242. ;; check if we got something on the serial port
  243. move.b [SERSTAT], $r0
  244. btstq 0, $r0 ; data_avail
  245. bpl wait_ser
  246. nop
  247. ;; got something - copy the byte and loop
  248. move.b [SERRDAT], $r0
  249. move.b $r0, [$r3+]
  250. subq 1, $r4 ; decrease length
  251. bne wait_ser
  252. nop
  253. ;; jump into downloaded code
  254. move.d RAM_INIT_MAGIC, $r8 ; Tell next product that DRAM is
  255. ; 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
  265. ; initialized
  266. jump $r7 ; boot!
  267. ;; Helper subroutines
  268. ;; Will checksum by simple addition
  269. ;; r1 - source
  270. ;; r2 - length in bytes
  271. ;; result will be in r0
  272. checksum:
  273. moveq 0, $r0
  274. moveq CONFIG_ETRAX_FLASH1_SIZE, $r6
  275. ;; If the first physical flash memory is exceeded wrap to the
  276. ;; second one
  277. btstq 26, $r1 ; Are we addressing first flash?
  278. bpl 1f
  279. nop
  280. clear.d $r6
  281. 1: test.d $r6 ; 0 = no wrapping
  282. beq 2f
  283. nop
  284. lslq 20, $r6 ; Convert MB to bytes
  285. sub.d $r1, $r6
  286. 2: addu.b [$r1+], $r0
  287. subq 1, $r6 ; Flash memory left
  288. beq 3f
  289. subq 1, $r2 ; Length left
  290. bne 2b
  291. nop
  292. ret
  293. nop
  294. 3: move.d MEM_CSE1_START, $r1 ; wrap to second flash
  295. ba 2b
  296. nop
  297. #endif