7) 转到RAM 中执行
使用指令ldr,pc,RAM 中C 函数地址就可以转到RAM 中去执行。
ldr pc, _start_armboot
5. 系统初始化部分
1) 串口部分(u-boot-1.1.2/cpu/s3c44b0/serial.c)
串口的设置主要包括初始化串口部分,值得注意的串口的Baudrate 与时钟MCLK 有很大关系,是通过:rUBRDIV0=( (int)(MCLK/16./(gd ->baudrate) + 0.5) -1 )计算得出。这可以在手册中查到。由于u-boot支持可变的波特率,所以采用宏定义设置默认波特率(64Mhz,115200bps)和其他波特率。代码如下:
- void serial_setbrg (void)
- {
- DECLARE_GLOBAL_DATA_PTR;
- u32 divisor = 0;
- /* get correct divisor */
- switch(gd->baudrate)
- {
- case 1200:
- #if CONFIG_S3C44B0_CLOCK_SPEED==66
- divisor = 3124;
- #elif CONFIG_S3C44B0_CLOCK_SPEED==75
- divisor = 3905;
- #elif CONFIG_S3C44B0_CLOCK_SPEED==64 /默认
- divisor = 3332;
- #else
- # error CONFIG_S3C44B0_CLOCK_SPEED undefined
- #endif
- break;
- case 9600:
- #if CONFIG_S3C44B0_CLOCK_SPEED==66
- divisor = 390;
- #elif CONFIG_S3C44B0_CLOCK_SPEED==75
- divisor = 487;
- #elif CONFIG_S3C44B0_CLOCK_SPEED==64 /默认
- divisor = 416;
- #else
- # error CONFIG_S3C44B0_CLOCK_SPEED undefined
- #endif
- break;
- case 19200:
- #if CONFIG_S3C44B0_CLOCK_SPEED==66
- divisor = 194;
- #elif CONFIG_S3C44B0_CLOCK_SPEED==75
- divisor = 243;
- #elif CONFIG_S3C44B0_CLOCK_SPEED==64 /默认
- divisor = 207;
- #else
- # error CONFIG_S3C44B0_CLOCK_SPEED undefined
- #endif
- break;
- case 38400:
- #if CONFIG_S3C44B0_CLOCK_SPEED==66
- divisor = 97;
- #elif CONFIG_S3C44B0_CLOCK_SPEED==75
- divisor = 121;
- #elif CONFIG_S3C44B0_CLOCK_SPEED==64 /默认
- divisor = 103;
- #else
- # error CONFIG_S3C44B0_CLOCK_SPEED undefined
- #endif break;
- case 57600:
- #if CONFIG_S3C44B0_CLOCK_SPEED==66
- divisor = 64;
- #elif CONFIG_S3C44B0_CLOCK_SPEED==75
- divisor = 80;
- #elif CONFIG_S3C44B0_CLOCK_SPEED==64 /默认
- divisor = 68;
- #else
- # error CONFIG_S3C44B0_CLOCK_SPEED undefined
- #endif break;
- case 115200:
- #if CONFIG_S3C44B0_CLOCK_SPEED==66
- divisor = 32;
- #elif CONFIG_S3C44B0_CLOCK_SPEED==64
- divisor = 34;
- #elif CONFIG_S3C44B0_CLOCK_SPEED==75 /默认
- divisor = 40;
- #else
- # error CONFIG_S3C44B0_CLOCK_SPEED undefined
- #endif break;
- }
- serial_flush_output();
- serial_flush_input();
- UFCON0 = 0x0;
- ULCON0 = 0x03;
- UCON0 = 0x05;
- UBRDIV0 = divisor;
- UFCON1 = 0x0;
- ULCON1 = 0x03;
- UCON1 = 0x05;
- UBRDIV1 = divisor;
- for(divisor=0; divisor<100; divisor++)
- {
- /* NOP */
- }
- }
其他的函数包括发送,接收。这个时候没有中断,是通过循环等待来判断是否动作完成。
例如,接收函数:
- static int serial_flush_input(void)
- {
- volatile u32 tmp;
- /* keep on reading as long as the receiver is not empty */
- while(UTRSTAT0&0x01) {
- tmp = REGB(URXH0);
- }
- return 0;
- }
2) 时钟部分(u-boot-1.1.2/cpu/s3c44b0/interrupt.c)
实现了延时函数udelay。
这里的get_timer 由于没有使用中断,是使用全局变量来累加的。
- void udelay (unsigned long usec)
- {
- ulong tmo;
- tmo = usec / 1000;
- tmo *= CFG_HZ;
- tmo /= 8;
- tmo += get_timer (0);
- while (get_timer_masked () < tmo)
- /*NOP*/;
- }
3) flash 部分(u-boot-1.1.2/board/gold44b.c)
flash 作为内存的一部分,读肯定没有问题,关键是flash 的写部分。
Flash 的写必须先擦除,然后再写。
flash_init 完成初始化部分,这里的主要目的是检验flash 的型号是否正确。
- unsigned long flash_init (void)
- {
- #ifdef __DEBUG_START_FROM_SRAM__
- return CFG_DUMMY_FLASH_SIZE;
- #else
- unsigned long size_b0;
- int i;
- /* Init: no FLASHes known */
- for (i=0; i<CFG_MAX_FLASH_BANKS; ++i) {
- flash_info[i].flash_id = FLASH_UNKNOWN;
- }
- /* Static FLASH Bank configuration here - FIXME XXX */
- size_b0 = flash_get_size((vu_long *)CFG_FLASH_BASE, &flash_info[0]);
- if (flash_info[0].flash_id == FLASH_UNKNOWN) {
- printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
- size_b0, size_b0<<20);
- }
- /* Setup offsets */
- flash_get_offsets (0, &flash_info[0]);
- /* Monitor protection ON by default */
- (void)flash_protect(FLAG_PROTECT_SET,
- -CFG_MONITOR_LEN,
- 0xffffffff,
- &flash_info[0]);
- flash_info[0].size = size_b0;
- return (size_b0);
- #endif
- }
- flash_erase 擦除flash,BlankCheck 则检查该部分内容是否擦除成功。
- int flash_erase (flash_info_t *info, int s_first, int s_last)
- {
- volatile CFG_FLASH_WORD_SIZE *addr = (CFG_FLASH_WORD_SIZE *)(info->start[0]);
- volatile CFG_FLASH_WORD_SIZE *addr2;
- int flag, prot, sect, l_sect;
- ulong start, now, last;
- int i;
- if ((s_first < 0) || (s_first > s_last)) {
- if (info->flash_id == FLASH_UNKNOWN) {
- printf ("- missing\n");
- } else {
- printf ("- no sectors to erase\n");
- }
- return 1;
- }
- if (info->flash_id == FLASH_UNKNOWN) {
- printf ("Can't erase unknown flash type - aborted\n");
- return 1;
- }
- prot = 0;
- for (sect=s_first; sect<=s_last; ++sect) {
- if (info->protect[sect]) {
- prot++;
- }
- }
- if (prot) {
- printf ("- Warning: %d protected sectors will not be erased!\n",
- prot);
- } else {
- printf ("\n");
- }
- l_sect = -1;
- /* Disable interrupts which might cause a timeout here */
- flag = disable_interrupts();
- /* Start erase on unprotected sectors */
- for (sect = s_first; sect<=s_last; sect++) {
- if (info->protect[sect] == 0) { /* not protected */
- addr2 = (CFG_FLASH_WORD_SIZE *)(info->start[sect]);
- if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) {
- addr[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00AA00AA;
- addr[CFG_FLASH_ADDR1] = (CFG_FLASH_WORD_SIZE)0x00550055;
- addr[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00800080;
- addr[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00AA00AA;
- addr[CFG_FLASH_ADDR1] = (CFG_FLASH_WORD_SIZE)0x00550055;
- addr2[0] = (CFG_FLASH_WORD_SIZE)0x00500050; /* block erase */
- for (i=0; i<50; i++)
- udelay(1000); /* wait 1 ms */
- } else {
- if (sect == s_first) {
- addr[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00AA00AA;
- addr[CFG_FLASH_ADDR1] = (CFG_FLASH_WORD_SIZE)0x00550055;
- addr[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00800080;
- addr[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00AA00AA;
- addr[CFG_FLASH_ADDR1] = (CFG_FLASH_WORD_SIZE)0x00550055;
- }
- addr2[0] = (CFG_FLASH_WORD_SIZE)0x00300030; /* sector erase */
- }
- l_sect = sect;
- }
- }
- /* re-enable interrupts if necessary */
- if (flag)
- enable_interrupts();
- /* wait at least 80us - let's wait 1 ms */
- udelay (1000);
- /*
- * We wait for the last triggered sector
- */
- if (l_sect < 0)
- goto DONE;
- start = get_timer (0);
- last = start;
- addr = (CFG_FLASH_WORD_SIZE *)(info->start[l_sect]);
- while ((addr[0] & (CFG_FLASH_WORD_SIZE)0x00800080) != (CFG_FLASH_WORD_SIZE)0x00800080) {
- if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
- printf ("Timeout\n");
- return 1;
- }
- /* show that we're waiting */
- if ((now - last) > 50000000) { /* every second */
- putc ('.');
- last = now;
- }
- }
- DONE:
- /* reset to read mode */
- addr = (CFG_FLASH_WORD_SIZE *)info->start[0];
- addr[0] = (CFG_FLASH_WORD_SIZE)0x00F000F0; /* reset bank */
- printf (" done\n");
- return 0;
- }
wirte_word 则想flash 里面写入unsigned long 类型的data,因为flash 一次只能写入16bits,所以这里分两次写入。
- /*-----------------------------------------------------------------------
- * Write a word to Flash, returns:
- * 0 - OK
- * 1 - write timeout
- * 2 - Flash not erased
- */
- static int write_word (flash_info_t *info, ulong dest, ulong data)
- {
- volatile CFG_FLASH_WORD_SIZE *addr2 = (CFG_FLASH_WORD_SIZE *)(info->start[0]);
- volatile CFG_FLASH_WORD_SIZE *dest2 = (CFG_FLASH_WORD_SIZE *)dest;
- volatile CFG_FLASH_WORD_SIZE *data2 = (CFG_FLASH_WORD_SIZE *)&data;
- ulong start;
- int flag;
- int i;
- /* Check if Flash is (sufficiently) erased */
- if ((*((volatile ulong *)dest) & data) != data)
- {
- return (2);
- }
- /* Disable interrupts which might cause a timeout here */
- flag = disable_interrupts();
- for (i=0; i<4/sizeof(CFG_FLASH_WORD_SIZE); i++)
- {
- addr2[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00AA00AA;
- addr2[CFG_FLASH_ADDR1] = (CFG_FLASH_WORD_SIZE)0x00550055;
- addr2[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00A000A0;
- dest2[i] = data2[i];
- /* re-enable interrupts if necessary */
- if (flag)
- enable_interrupts();
- /* data polling for D7 */
- start = get_timer (0);
- while ((dest2[i] & (CFG_FLASH_WORD_SIZE)0x00800080) !=
- (data2[i] & (CFG_FLASH_WORD_SIZE)0x00800080))
- {
- if (get_timer(start) > CFG_FLASH_WRITE_TOUT)
- {
- return (1);
- }
- }
- }
- return (0);
- }