start_gcc.S 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. /*
  2. * File : start_gcc.S
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2011, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2010-05-17 swkyer first version
  13. * 2010-09-04 bernard porting to Jz47xx
  14. */
  15. #ifndef __ASSEMBLY__
  16. #define __ASSEMBLY__
  17. #endif
  18. #include "../common/mips_def.h"
  19. #include "../common/stackframe.h"
  20. #include "sdram_cfg.h"
  21. #include "cache.h"
  22. #include "rtconfig.h"
  23. #define SR_BOOT_EXC_VEC 0x00400000
  24. /* config pll div for cpu and sdram */
  25. #define PLL_MULT (0x54) // 晶振为24Mhz时,PLL=504Mhz
  26. #define SDRAM_DIV (0) // SDRAM为CPU的2分频
  27. #define CPU_DIV (2) // CPU为PLL的2分频
  28. // 配置内存大小
  29. #define MEM_SIZE (0x02000000) // 32MByte
  30. /* Delay macro */
  31. #define DELAY(count) \
  32. li v0, count; \
  33. 99: \
  34. bnez v0, 99b;\
  35. addiu v0, -1
  36. #define msize s2
  37. #define output_en s3
  38. .section ".start", "ax"
  39. .set noreorder
  40. /* the program entry */
  41. .globl _start
  42. _start:
  43. .set noreorder
  44. la ra, _start
  45. #if !defined(RT_USING_SELF_BOOT)
  46. /* disable interrupt */
  47. mfc0 t0, CP0_STATUS
  48. and t0, 0xfffffffe # By default it will be disabled.
  49. mtc0 t0, CP0_STATUS # Set CPU to disable interrupt.
  50. nop
  51. /* disable cache */
  52. mfc0 t0, CP0_CONFIG
  53. and t0, 0xfffffff8
  54. or t0, 0x2 # disable,!default value is not it!
  55. mtc0 t0, CP0_CONFIG # Set CPU to disable cache.
  56. nop
  57. /* setup stack pointer */
  58. li sp, SYSTEM_STACK
  59. la gp, _gp
  60. /* clear bss */
  61. la t0, __bss_start
  62. la t1, __bss_end
  63. _clr_bss_loop:
  64. sw zero, 0(t0)
  65. bne t0, t1, _clr_bss_loop
  66. addiu t0, t0, 4
  67. /* jump to RT-Thread RTOS */
  68. jal rtthread_startup
  69. nop
  70. /* restart, never die */
  71. j _start
  72. nop
  73. #else
  74. mtc0 zero, CP0_STATUS // 清零cp0 status寄存器
  75. mtc0 zero, CP0_CAUSE // 清零cp0 cause寄存器
  76. /*
  77. 设置启动异常向量入口地址为ROM地址(0xbfc00000)
  78. 将寄存器cp0 status的BEV置1,使CPU采用ROM(kseg1)空间的异常入口点
  79. */
  80. li t0, SR_BOOT_EXC_VEC /* Exception to Boostrap Location */
  81. mtc0 t0, CP0_STATUS
  82. /* setup stack pointer */
  83. li sp, SYSTEM_STACK
  84. la gp, _gp
  85. /* initialize spi */
  86. li t0, 0xbfe80000 //地址0xbfe80000为SPI0的寄存器基地址
  87. li t1, 0x17 // div 4, fast_read + burst_en + memory_en double I/O 模式 部分SPI flash可能不支持
  88. sb t1, 0x4(t0) // 设置寄存器sfc_param
  89. li t1, 0x05
  90. sb t1, 0x6(t0) // 设置寄存器sfc_timing
  91. /* 设置sdram cs1复用关系,开发板使用ejtag_sel gpio_0引脚(第五复用)作为第二片sdram的片选
  92. 注意sw2拨码开关的设置,使用ejtag烧录pmon时需要调整拨码开关,烧录完再调整回来 */
  93. li a0, 0xbfd011c0
  94. lw a1, 0x40(a0)
  95. ori a1, 0x01
  96. sw a1, 0x40(a0)
  97. bal locate
  98. nop
  99. /* restart, never die */
  100. j _start
  101. nop
  102. #endif
  103. .set reorder
  104. .globl cp0_get_cause
  105. cp0_get_cause:
  106. mfc0 v0, CP0_CAUSE
  107. jr ra
  108. nop
  109. .globl cp0_get_status
  110. cp0_get_status:
  111. mfc0 v0, CP0_STATUS
  112. jr ra
  113. nop
  114. .globl cp0_get_hi
  115. cp0_get_hi:
  116. mfhi v0
  117. jr ra
  118. nop
  119. .globl cp0_get_lo
  120. cp0_get_lo:
  121. mflo v0
  122. jr ra
  123. nop
  124. #if defined(RT_USING_SELF_BOOT)
  125. /****************************************LOCATE*********************************/
  126. /*
  127. * We get here from executing a bal to get the PC value of the current execute
  128. * location into ra. Check to see if we run from ROM or if this is ramloaded.
  129. * 寄存器ra内保持着函数的返回地址,根据ra的值来判断当前是从ROM冷启动,还是从RAM热复位的
  130. * ROM冷启动由通电引起,RAM热复位为各种异常引起,比如看门狗引起的复位等,
  131. * 也就是RAM热复位之前CPU已经开始运行了
  132. * 如果是从ROM冷启动,则寄存器ra的值为指令"bal locate"所在位置加8字节,大概在0xBFC00000附近
  133. * 如果是从RAM热复位,则集成器ra的值为0x80xxxxxx
  134. */
  135. locate:
  136. // la s0, uncached
  137. // subu s0, ra, s0
  138. /*
  139. * start.s的这段汇编程序在ROM(入口点为0xBFC00000)中运行
  140. * 而编译链接时指定的起始地址是0x80100000,所以需要修正一下地址
  141. * s0中保存着ra与start的差值,在后续的代码中可以起到修正地址的作用
  142. * 在看看文件开始的时候,对寄存器s0用途的描述是“ link versus load offset, used to relocate absolute adresses”
  143. * 除了修正地址外,还通过s0的值来判断是从ROM冷启动,还是从RAM热启动
  144. */
  145. la s0, _start // s0 = _start, 其中start的地址为编译链接时,指定的0x80010000
  146. subu s0, ra, s0 // s0 = ra - s0,其中ra的值在ROM入口地址0xBFC00000附近
  147. and s0, 0xffff0000 // s0 = s0 & 0xffff0000
  148. /*
  149. * 初始化cp0的status寄存器和cause寄存器
  150. * 在异常引起的(从RAM)热复位后,需要重新初始化cp0的status和cause,
  151. * 如果是从ROM冷启动的,那么前面已经初始化了,这里是再次重复初始化,没有影响的
  152. */
  153. li t0, SR_BOOT_EXC_VEC
  154. mtc0 t0, CP0_CONFIG // 重新初始化cp0的status寄存器
  155. mtc0 zero, CP0_CAUSE // 重新清零cp0的cause寄存器
  156. .set noreorder
  157. li t0, 0xbfe78030 // 地址0xbfe78030为PLL/SDRAM频率配置寄存器的地址
  158. /* 设置PLL倍频 及SDRAM分频 */
  159. li t2, (0x80000008 | (PLL_MULT << 8) | (0x3 << 2) | SDRAM_DIV)
  160. /* 设置CPU分频 */
  161. li t3, (0x00008003 | (CPU_DIV << 8))
  162. /* 注意:首先需要把分频使能位清零 */
  163. li t1, 0x2
  164. sw t1, 0x4(t0) // 清零CPU_DIV_VALID,即disable
  165. sw t2, 0x0(t0) // 写寄存器START_FREQ
  166. sw t3, 0x4(t0) // 写寄存器CLK_DIV_PARAM
  167. DELAY(2000)
  168. /* 芯片上电默认使用gpio(输入模式)但大多时候是使用模块的功能,如lcd i2c spi ac97等
  169. 所以这里把gpio都关闭,方便使用模块功能。如果上电后需要gpio输出一个确定电平,
  170. 如继电器、LDE等,可以修改这里的代码。*/
  171. /* disable all gpio */
  172. li a0,0xbfd00000
  173. sw zero,0x10c0(a0) /* disable gpio 0-31 */
  174. sw zero,0x10c4(a0) /* disable gpio 32-63 */
  175. sw zero,0x10c8(a0) /* disable gpio 64-95 */
  176. sw zero,0x10cc(a0)
  177. li t0, 0xffffffff
  178. sw t0, 0x10d0(a0)
  179. sw t0, 0x10d4(a0)
  180. sw t0, 0x10d8(a0)
  181. sw t0, 0x10dc(a0)
  182. sw t0, 0x10f0(a0)
  183. sw t0, 0x10f4(a0)
  184. sw t0, 0x10f8(a0)
  185. sw t0, 0x10fc(a0)
  186. /* lcd soft_reset and panel config & timing */
  187. #ifdef DC_FB0
  188. /* li a0, 0xbc301240
  189. li a1, 0x00100103
  190. sw a1, 0x0(a0)
  191. li a1, 0x00000103
  192. sw a1, 0x0(a0) //soft_reset
  193. li a1, 0x00100103
  194. sw a1, 0x0(a0)
  195. li a1, 0x80001111
  196. sw a1, 0x180(a0) //panel config
  197. li a1, 0x33333333
  198. sw a1, 0x1a0(a0)*/
  199. #endif
  200. li output_en, 0x1
  201. #ifdef FAST_STARTUP
  202. li a1, 0x03000000
  203. sw a1, 0x10c4(a0)
  204. sw a1, 0x10d4(a0)
  205. lw a2, 0x10e4(a0)
  206. and a2, a1
  207. beq a2, a1, get_pin_val_finish
  208. nop
  209. li output_en, 0x1
  210. get_pin_val_finish:
  211. #endif
  212. /* Initializing. Standby... */
  213. /*
  214. * 根据s0的值判断是否为ROM冷启动
  215. * 如果s0不等于0,则是ROM冷启动;如果等于0,则是RAM热复位
  216. * 冷启动,则需要初始化内存,cache,加载代码到内存等
  217. */
  218. bnez s0, 1f // 如果寄存器s0不等于0,则说明是ROM冷启动,则跳转到下一个标号1处进行彻底初始化
  219. nop
  220. li a0, 128
  221. jal rtthread_startup // 热复位,则直接跳转到函数main
  222. nop
  223. 1:
  224. /* use only 8wins */
  225. #define CPU_WIN_BASE 0xbfd00000
  226. #define CPU_WIN_MASK 0xbfd00040
  227. #define CPU_WIN_MMAP 0xbfd00080
  228. #define set_cpu_window(id, base, mask, mmap) \
  229. li t0, CPU_WIN_BASE ; \
  230. sw $0, 0x80+id*8(t0) ; \
  231. li t1, base ; \
  232. sw t1, 0x00+id*8(t0) ; \
  233. sw $0, 0x04+id*8(t0) ; \
  234. li t1, mask ; \
  235. sw t1, 0x40+id*8(t0) ; \
  236. sw $0, 0x44+id*8(t0) ; \
  237. li t1, mmap ; \
  238. sw t1, 0x80+id*8(t0) ; \
  239. sw $0, 0x84+id*8(t0)
  240. /* fixup cpu window */
  241. cpu_win_fixup:
  242. //
  243. // hit = (paddr & mask) == (mmap & mask)
  244. // mapped_addr = paddr &~mask | mmap & mask
  245. //
  246. // mmap[7] -> enable
  247. // mmap[5] -> block trans enable
  248. // mmap[4] -> cachable
  249. // mmap[1:0] -> destination
  250. //
  251. // NOTE: the address windows has priority, win0 > win1 > ... > win7
  252. /* set_cpu_window(0, 0x1c280000, 0xfff80000, 0x1c280083) // camera 512K
  253. set_cpu_window(1, 0x1c300000, 0xfff00000, 0x1c300081) // dc 1M
  254. set_cpu_window(2, 0x1fe10000, 0xffffe000, 0x1fe10082) // gmac0 8K
  255. set_cpu_window(3, 0x1fe10000, 0xffff0000, 0x1fe100d0) // gmac0 64K
  256. set_cpu_window(4, 0x1f000000, 0xff000000, 0x1f000082) // AXIMUX 16M
  257. set_cpu_window(5, 0x00000000, 0x00000000, 0x000000f0) // ddr 0
  258. set_cpu_window(6, 0x00000000, 0x00000000, 0x000000f0) // ddr 0
  259. set_cpu_window(7, 0x00000000, 0x00000000, 0x000000f0) // ddr 0*/
  260. /* set_cpu_window(0, 0x1c280000, 0xfff80000, 0x1c2800d3) // camera
  261. // set_cpu_window(1, 0x1fc00000, 0xfff00000, 0x1fc000f2) //
  262. set_cpu_window(2, 0x1c300000, 0xfff00000, 0x1c3000d1) // dc 1M
  263. // set_cpu_window(3, 0x1f000000, 0xff000000, 0x1f0000d2) //
  264. set_cpu_window(4, 0x00000000, 0x00000000, 0x000000f0)
  265. set_cpu_window(5, 0x00000000, 0x00000000, 0x000000f0)
  266. set_cpu_window(6, 0x00000000, 0x00000000, 0x000000f0) // ddr 0
  267. set_cpu_window(7, 0x00000000, 0x00000000, 0x000000f0) // ddr 0*/
  268. // after this fixup, the kernel code should be compiled with
  269. // uncached instruction fetch patch
  270. /* 配置内存 */
  271. li msize, MEM_SIZE
  272. #if !defined(NAND_BOOT_EN)
  273. /*
  274. 手册建议,先写寄存器SD_CONFIG[31:0],然后再写寄存器的SD_CONFIG[63:32],
  275. 即先写低32位,再写高32位。
  276. 写三次寄存器,最后一次将最高位置一,即使能
  277. */
  278. // 写第一次
  279. li t1, 0xbfd00410 // 寄存器SD_CONFIG[31:0]的地址为0xbfd00410
  280. li a1, SD_PARA0 // 宏SD_PARA0在sdram_cfg.S中定义的
  281. sw a1, 0x0(t1) // 将宏SD_PARA0的值写入寄存器SD_CONFIG[31:0]
  282. li a1, SD_PARA1
  283. sw a1, 0x4(t1) // 同理,将宏SD_PARA1的值写入寄存器SD_CONFIG[63:32]
  284. // 写第二次
  285. li a1, SD_PARA0
  286. sw a1, 0x0(t1)
  287. li a1, SD_PARA1
  288. sw a1, 0x4(t1)
  289. // 写第三次
  290. li a1, SD_PARA0
  291. sw a1, 0x0(t1)
  292. li a1, SD_PARA1_EN // 使能
  293. sw a1, 0x4(t1)
  294. // DELAY(100)
  295. #endif
  296. /**************************************CACHE*****************************/
  297. #define CF_7_SE (1 << 3) /* Secondary cache enable */
  298. #define CF_7_SC (1 << 31) /* Secondary cache not present */
  299. #define CF_7_TE (1 << 12) /* Tertiary cache enable */
  300. #define CF_7_TC (1 << 17) /* Tertiary cache not present */
  301. #define CF_7_TS (3 << 20) /* Tertiary cache size */
  302. #define CF_7_TS_AL 20 /* Shift to align */
  303. #define NOP8 nop;nop;nop;nop;nop;nop;nop;nop
  304. do_caches:
  305. /* Init caches... */
  306. li s7, 0 /* no L2 cache */
  307. li s8, 0 /* no L3 cache */
  308. bal cache_init // 调用汇编函数cache_init
  309. nop
  310. mfc0 a0, CP0_CONFIG // 将协处理器0的config寄存器的值加载到寄存器a0
  311. and a0, a0, ~((1<<12) | 7) // a0 = a0 & ~((1<<12) | 7)
  312. or a0, a0, 2 // a0 |= 2
  313. mtc0 a0, CP0_CONFIG // 将寄存器a0的值写入协处理器0的config寄存器
  314. /***********************MEMORY DEBUGGING AND COPY SELF TO RAM***********************/
  315. //#include "newtest.32/mydebug.S"
  316. bootnow:
  317. /* copy program to sdram to make copy fast */
  318. /* 先将执行拷贝pmon到内存任务的代码,拷贝到内存0xa0000000 */
  319. /* 先确定需要拷贝的代码段为标号121到标号122之间的代码
  320. * 由于链接时指定的起始地址是0x80010000,
  321. * 而目前正在ROM(SPI NOR FLASH,起始地址为0xBFC00000)运行
  322. * 所以需要用寄存器s0来修正一下地址
  323. */
  324. la t0, 121f // 将下一个标号121所在地址,加载到寄存器t0
  325. addu t0, s0 // 使用寄存器s0修正t0中的(标号121的)地址
  326. la t1, 122f // 将下一个标号122所在地址,加载到寄存器t1
  327. addu t1, s0 // 使用寄存器s0修正t1中的(标号122的)地址
  328. li t2, 0xa0000000 // 将立即数0xa0000000(起始地址)加载到寄存器t2
  329. 1:
  330. lw v0, (t0) // 将寄存器t0所指的内存地址开始4字节的数据加载到寄存器v0
  331. sw v0, (t2) // 将寄存器v0的内容保存到寄存器t2所指的内存中
  332. addu t0, 4 // 寄存器t0向后移4字节
  333. addu t2, 4 // 寄存器t2向后移4字节
  334. ble t0, t1, 1b // 如果t0 <= t1,则跳转到上一个标号1处,继续拷贝后面的4字节
  335. nop
  336. li t0, 0xa0000000 // 将立即数0xa0000000加载到寄存器t0
  337. jr t0 // 跳转到起始地址0xa0000000处开始执行(拷贝任务)
  338. nop
  339. 121:
  340. /* Copy PMON to execute location... */
  341. /* 将固件拷贝到起始地址为0xa0010000的内存空间
  342. 由于kseg0(0x8000 0000 - 0x9FFF FFFF)和kseg1(0xA000 0000 - 0xBFFF FFFF)是映射到物理内存的相同区域
  343. 即拷贝到0xA000 0000开始的kseg1,就相当于拷贝到0x8000 0000开始的kseg0
  344. 这就是为什么链接时,指定的地址是0x8001 0000,而拷贝的目标起始地址是0xA001 0000
  345. */
  346. la a0, _start // 加载符号start所在地址0x80010000加载到寄存器a0中
  347. addu a1, a0, s0 // 使用寄存器s0修正寄存器a0中的地址,a1=0xBFC00000
  348. la a2, __bss_start // 加载_edata(链接脚本中的一个符号)到寄存器a2
  349. or a0, 0xa0000000 // a0 = a0 | 0xa0000000 = 0xa0010000
  350. or a2, 0xa0000000 // a2 = a2 | 0xa0000000,修正地址_edata
  351. subu t1, a2, a0 // t1 = a2 - a0,即计算从start到_edata之间的长度(字节数)
  352. srl t1, t1, 2 // t1 >>= 2,即t1除以4。(和前面类似,每次拷贝4字节,所以除以4)
  353. // 似乎t1计算结果没有被使用,马上就被后面的覆盖了
  354. move t0, a0 // t0 = a0 = 0xa0010000 (目标起始地址)
  355. move t1, a1 // t1 = a1 = 0xBFC00000 (start在ROM中的地址,源起始地址)
  356. move t2, a2 // t2 = a2 (_edata在ROM中的地址,源结束地址)
  357. /* copy text section */
  358. 1: and t3, t0, 0x0000ffff // t3 = t0 & 0x0000ffff,取低16位
  359. bnez t3, 2f // 如果t3不等于0,则跳转到下一个标号2处继续执行,t3的计算结果似乎没被使用,就被后面的覆盖了
  360. nop
  361. 2: lw t3, 0(t1) // 从源地址t1处加载4字节到寄存器t3中
  362. nop
  363. sw t3, 0(t0) // 将寄存器t3中的4字节数据保存到目标地址t0处
  364. addu t0, 4 // 目标地址t0后移4字节
  365. addu t1, 4 // 源地址t1 后移4字节
  366. bne t2, t0, 1b // 如果t2不等于t0,则跳到上一个标号1处继续拷贝,总的来说就是判断拷贝是否结束
  367. nop
  368. /* copy text section done. */
  369. /* clear bss */
  370. la t0, __bss_start
  371. la t1, __bss_end
  372. _clr_bss_loop:
  373. sw zero, 0(t0)
  374. bne t0, t1, _clr_bss_loop
  375. addiu t0, t0, 4
  376. /* disable interrupt */
  377. mfc0 t0, CP0_STATUS
  378. and t0, 0xfffffffe # By default it will be disabled.
  379. mtc0 t0, CP0_STATUS # Set CPU to disable interrupt.
  380. nop
  381. /* disable cache */
  382. mfc0 t0, CP0_CONFIG
  383. and t0, 0xfffffff8
  384. or t0, 0x2 # disable,!default value is not it!
  385. mtc0 t0, CP0_CONFIG # Set CPU to disable cache.
  386. nop
  387. /* jump to RT-Thread RTOS */
  388. jal rtthread_startup
  389. nop
  390. /* restart, never die */
  391. j _start
  392. nop
  393. 122:
  394. stuck:
  395. b stuck
  396. nop
  397. #endif
  398. .extern tlb_refill_handler
  399. .extern cache_error_handler
  400. /* Exception Handler */
  401. /* 0x0 - TLB refill handler */
  402. .section .vectors.1, "ax", %progbits
  403. .global tlb_refill_exception
  404. .type tlb_refill_exception,@function
  405. tlb_refill_exception:
  406. j tlb_refill_handler
  407. nop
  408. /* 0x100 - Cache error handler */
  409. .section .vectors.2, "ax", %progbits
  410. j cache_error_handler
  411. nop
  412. /* 0x180 - Exception/Interrupt handler */
  413. .section .vectors.3, "ax", %progbits
  414. .global general_exception
  415. .type general_exception,@function
  416. general_exception:
  417. j _general_exception_handler
  418. nop
  419. /* 0x200 - Special Exception Interrupt handler (when IV is set in CP0_CAUSE) */
  420. .section .vectors.4, "ax", %progbits
  421. .global irq_exception
  422. .type irq_exception,@function
  423. irq_exception:
  424. j _irq_handler
  425. nop
  426. .section .vectors, "ax", %progbits
  427. .extern mips_irq_handle
  428. /* general exception handler */
  429. _general_exception_handler:
  430. .set noreorder
  431. la k0, mips_irq_handle
  432. jr k0
  433. nop
  434. .set reorder
  435. /* interrupt handler */
  436. _irq_handler:
  437. .set noreorder
  438. la k0, mips_irq_handle
  439. jr k0
  440. nop
  441. .set reorder