head.S 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. #include <linux/config.h>
  66. #define ASSEMBLER_MACROS_ONLY
  67. #include <asm/arch/sv_addr_ag.h>
  68. ;; The partitiontable is looked for at the first sector after the boot
  69. ;; sector. Sector size is 65536 bytes in all flashes we use.
  70. #define PTABLE_START CONFIG_ETRAX_PTABLE_SECTOR
  71. #define PTABLE_MAGIC 0xbeef
  72. ;; The normal Etrax100 on-chip boot ROM does serial boot at 0x380000f0.
  73. ;; That is not where we put our downloaded serial boot-code. The length is
  74. ;; enough for downloading code that loads the rest of itself (after
  75. ;; having setup the DRAM etc). It is the same length as the on-chip
  76. ;; ROM loads, so the same host loader can be used to load a rescued
  77. ;; product as well as 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 rescue code
  120. ;; without erasing/reflashing the sector. we put a longword of -1 here and if
  121. ;; it is not -1, we jump using the value as jump target. since we can always
  122. ;; change 1's to 0's without erasing the sector, it is possible to add new
  123. ;; code after this and altering the jumptarget in an upgrade.
  124. jtcd: move.d [jumptarget], $r0
  125. cmp.d 0xffffffff, $r0
  126. beq no_newjump
  127. nop
  128. jump [$r0]
  129. jumptarget:
  130. .dword 0xffffffff ; can be overwritten later to insert new code
  131. no_newjump:
  132. #ifdef CONFIG_ETRAX_ETHERNET
  133. ;; Start MII clock to make sure it is running when tranceiver is reset
  134. move.d 0x3, $r0 ; enable = on, phy = mii_clk
  135. move.d $r0, [R_NETWORK_GEN_CONFIG]
  136. #endif
  137. ;; We need to setup the bus registers before we start using the DRAM
  138. #include "../../lib/dram_init.S"
  139. ;; we now should go through the checksum-table and check the listed
  140. ;; partitions for errors.
  141. move.d PTABLE_START, $r3
  142. move.d [$r3], $r0
  143. cmp.d NOP_DI, $r0 ; make sure the nop/di is there...
  144. bne do_rescue
  145. nop
  146. ;; skip the code transparency block (10 bytes).
  147. addq 10, $r3
  148. ;; check for correct magic
  149. move.w [$r3+], $r0
  150. cmp.w PTABLE_MAGIC, $r0
  151. bne do_rescue ; didn't recognize - trig rescue
  152. nop
  153. ;; check for correct ptable checksum
  154. movu.w [$r3+], $r2 ; ptable length
  155. move.d $r2, $r8 ; save for later, length of total ptable
  156. addq 28, $r8 ; account for the rest
  157. move.d [$r3+], $r4 ; ptable checksum
  158. move.d $r3, $r1
  159. jsr checksum ; r1 source, r2 length, returns in r0
  160. cmp.d $r0, $r4
  161. bne do_rescue ; didn't match - trig rescue
  162. nop
  163. ;; ptable is ok. validate each entry.
  164. moveq -1, $r7
  165. ploop: move.d [$r3+], $r1 ; partition offset (from ptable start)
  166. bne notfirst ; check if it is the partition containing ptable
  167. nop ; yes..
  168. move.d $r8, $r1 ; for its checksum check, skip the ptable
  169. move.d [$r3+], $r2 ; partition length
  170. sub.d $r8, $r2 ; minus the ptable length
  171. ba bosse
  172. nop
  173. notfirst:
  174. cmp.d -1, $r1 ; the end of the ptable ?
  175. beq flash_ok ; if so, the flash is validated
  176. move.d [$r3+], $r2 ; partition length
  177. bosse: move.d [$r3+], $r5 ; checksum
  178. move.d [$r3+], $r4 ; type and flags
  179. addq 16, $r3 ; skip the reserved bytes
  180. btstq 16, $r4 ; check ro flag
  181. bpl ploop ; rw partition, skip validation
  182. nop
  183. btstq 17, $r4 ; check bootable flag
  184. bpl 1f
  185. nop
  186. move.d $r1, $r7 ; remember boot partition offset
  187. 1:
  188. add.d PTABLE_START, $r1
  189. jsr checksum ; checksum the partition
  190. cmp.d $r0, $r5
  191. beq ploop ; checksums matched, go to next entry
  192. nop
  193. ;; otherwise fall through to the rescue code.
  194. do_rescue:
  195. ;; setup port PA and PB default initial directions and data
  196. ;; (so we can flash LEDs, and so that DTR and others are set)
  197. move.b CONFIG_ETRAX_DEF_R_PORT_PA_DIR, $r0
  198. move.b $r0, [R_PORT_PA_DIR]
  199. move.b CONFIG_ETRAX_DEF_R_PORT_PA_DATA, $r0
  200. move.b $r0, [R_PORT_PA_DATA]
  201. move.b CONFIG_ETRAX_DEF_R_PORT_PB_DIR, $r0
  202. move.b $r0, [R_PORT_PB_DIR]
  203. move.b CONFIG_ETRAX_DEF_R_PORT_PB_DATA, $r0
  204. move.b $r0, [R_PORT_PB_DATA]
  205. ;; setup the serial port at 115200 baud
  206. moveq 0, $r0
  207. move.d $r0, [SERXOFF]
  208. move.b 0x99, $r0
  209. move.b $r0, [SERBAUD] ; 115.2kbaud for both transmit and receive
  210. move.b 0x40, $r0 ; rec enable
  211. move.b $r0, [SERRECC]
  212. moveq 0, $r1 ; "timer" to clock out a LED red flash
  213. move.d CODE_START, $r3 ; destination counter
  214. movu.w CODE_LENGTH, $r4; length
  215. wait_ser:
  216. addq 1, $r1
  217. #ifndef CONFIG_ETRAX_NO_LEDS
  218. #ifdef CONFIG_ETRAX_PA_LEDS
  219. move.b CONFIG_ETRAX_DEF_R_PORT_PA_DATA, $r2
  220. #endif
  221. #ifdef CONFIG_ETRAX_PB_LEDS
  222. move.b CONFIG_ETRAX_DEF_R_PORT_PB_DATA, $r2
  223. #endif
  224. move.d (1 << CONFIG_ETRAX_LED1R) | (1 << CONFIG_ETRAX_LED2R), $r0
  225. btstq 16, $r1
  226. bpl 1f
  227. nop
  228. or.d $r0, $r2 ; set bit
  229. ba 2f
  230. nop
  231. 1: not $r0 ; clear bit
  232. and.d $r0, $r2
  233. 2:
  234. #ifdef CONFIG_ETRAX_PA_LEDS
  235. move.b $r2, [R_PORT_PA_DATA]
  236. #endif
  237. #ifdef CONFIG_ETRAX_PB_LEDS
  238. move.b $r2, [R_PORT_PB_DATA]
  239. #endif
  240. #ifdef CONFIG_ETRAX_90000000_LEDS
  241. move.b $r2, [0x90000000]
  242. #endif
  243. #endif
  244. ;; check if we got something on the serial port
  245. move.b [SERSTAT], $r0
  246. btstq 0, $r0 ; data_avail
  247. bpl wait_ser
  248. nop
  249. ;; got something - copy the byte and loop
  250. move.b [SERRDAT], $r0
  251. move.b $r0, [$r3+]
  252. subq 1, $r4 ; decrease length
  253. bne wait_ser
  254. nop
  255. ;; jump into downloaded code
  256. move.d RAM_INIT_MAGIC, $r8 ; Tell next product that DRAM is initialized
  257. jump CODE_START
  258. flash_ok:
  259. ;; check r7, which contains either -1 or the partition to boot from
  260. cmp.d -1, $r7
  261. bne 1f
  262. nop
  263. move.d PTABLE_START, $r7; otherwise use the ptable start
  264. 1:
  265. move.d RAM_INIT_MAGIC, $r8 ; Tell next product that DRAM is 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 second one.
  276. btstq 26, $r1 ; Are we addressing first flash?
  277. bpl 1f
  278. nop
  279. clear.d $r6
  280. 1: test.d $r6 ; 0 = no wrapping
  281. beq 2f
  282. nop
  283. lslq 20, $r6 ; Convert MB to bytes
  284. sub.d $r1, $r6
  285. 2: addu.b [$r1+], $r0
  286. subq 1, $r6 ; Flash memory left
  287. beq 3f
  288. subq 1, $r2 ; Length left
  289. bne 2b
  290. nop
  291. ret
  292. nop
  293. 3: move.d MEM_CSE1_START, $r1 ; wrap to second flash
  294. ba 2b
  295. nop