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