⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 loader.c

📁 4200_boot 这个程序很重要
💻 C
字号:
/*
 *  Start of Zoran Standard Header
 *  Copyright (c) 2003 - 2004 Zoran Corporation.
 *  
 *  
 *  All rights reserved.  Proprietary and confidential.
 *  
 *  DESCRIPTION for loader.c
 *      Program to load firmware into RAM and execute it at it's load
 *      location. Firmware data is loaded via the USB interface.
 *      For now, a static buffer is used to contain the loaded firmware
 *      image. Later, a memory allocation (malloc) should be used.
 *
 *  NEW HISTORY COMMENT (description must be followed by a blank line)
 *  <Enter change description here>

 *  ===== HISTORY of changes
 *  27/Jan/05   GaryLiang   Created it.
 *  
 *  End of Zoran Standard Header
 */
#include <stdio.h>
#include <string.h>
#include <locale.h>
#include <rt_heap.h>

#include "targmach.gh"
#include "debug.gh"
#include "datetime.h"
#include "univ.gh"
#include "arch.h"
#include "pile.h"

#include "oti4100.h"
#include "iomacros.h"
#include "icu.h"
#include "bios.h"

#include "dbg.h"
#include "serial.h"
#include "zlib.h"

#include "usb.h"

/* Prototypes are here now until I find a good place form them
 */
extern int xprintf(const char *, ...);
extern void armIntsOn(void);

#define IO_READW(p)     (Uint32)(*(volatile Uint32 *)(p))
#define IO_READC(p)     (Uint8)(*(volatile Uint8 *)(p))
#define IO_WRITEW(p,v)  (*((volatile Uint32 *)(p)) = (Uint32)(v))
#define IO_WRITEC(p,c)  (*((volatile Uint8 *)(p)) = ((Uint8)(c)))

#define MB_16 (Uint32)(16 * 1024 * 1024)
#define MB_32 (Uint32)(32 * 1024 * 1024)

#ifdef OTI4100
#if 1
#define CLOCKMHz (Uint32)132
#else
#define CLOCKMHz (Uint32)120
#endif
#define BAUDRATE (Uint32)57600
#else
#define CLOCKMHz (Uint32)150
#define BAUDRATE (Uint32)9600
#endif

#define VERBOSE

#define PLL_INPUT_FREQUENCY (Uint32)48000000
#define MHZ ((Uint32)1000000)

#define BASE_CLOCK_SPEED   (48 * MHZ)
#define NORMAL_CLOCK_SPEED (133 * MHZ)

/* Interrupt Exception Cause Register Assignments
 */
#define EC_USB2 (Uint32)(1<<1)
#define EC_JBIG (Uint32)(1<<2)
#define EC_TIM0 (Uint32)(1<<26)

#define HEADER_SIZE (16 + 8)
#define FIRMWARE_PROG 0xD0
#define FIRMWARE_LOAD 0xD1
#define DEFAULT_ROM_ADRS ((Uint32)0xF9000000 + (1024 * 96))

#define BUFFER_SIZE (1024*1024*4)
    
/* Firmware Program/Load common common data code sequence.
 */
static Uint8 FirmwareSeq[] = { 0x1B, 0x2A, 0x6D, 0x00 };
static Uint8 Hdr[HEADER_SIZE];
static Uint32 FWCmprCodeSize, FWChecksum, FWLength, FWAddress, FWProgAdr;
static Uint32 RWsize;
static int HeaderSize = HEADER_SIZE;

#if 0
// For now, just allocate a static buffer to hold the firmware image.
Uint8 Buffer[1024 * 1024 * 4];
#endif

// Reset source strings:
static char *ResetSrc[] = {
    "Power On Reset",
    "Watchdog Timeout",
    "Programmed Reset"
};


char *pBuffer;

/* Externals:
 */
extern void *bottom_of_heap;

extern void InitHiResTimer(void);
extern Uint32 GetClockSpeed(void);

#ifndef ZR4050
extern void USB_lisr(int);
#else
extern void _usb_dci_vusb11_isr(void);
#endif

/* Local functions:
 */
static void Loader(int, char **argv);

Uint8 *FreeMemory;
Uint32 PhysicalMemorySize;

static void   *SystemMemoryStart;
static Uint32 SystemMemorySize;

volatile Uint32 TimerCount;

/* This is where "init.s" calls the application code.
 */
void INC_Initialize(void *free_memory)
{

    /* BIOSGetMemorySize() MUST be called at init time!!!! */
    PhysicalMemorySize = BIOSGetMemorySize();


    FreeMemory = SystemMemoryStart = free_memory;
    SystemMemorySize  = PhysicalMemorySize - (Uint32)SystemMemoryStart;

    _init_alloc((unsigned)SystemMemoryStart, (unsigned)PhysicalMemorySize);

    Loader(0,0);
}

static void Loader(int argc, char *argv[])
{
   Boolean AutoLoad;
   Uint8 *p;
   Uint32 CalcChecksum; 
   int n, sts;
   Uint32 ClockSpeed, ClockMod;

   /* Not sure if this is the best place for this stuff.
    */
   setlocale(LC_ALL, "C");
    
   ClockSpeed = GetClockSpeed();
   ClockMod = ((IO_READ32(PLLCTR) >> 12) & 7);
   InitHiResTimer();

   #ifdef ZR4050   
      IO_WRITE8(SIODIR1, 0x00);    
      IO_WRITE8(SIOWD1,  0x00);       
   #else  
      IO_WRITE8(SIODIR0, 0x01);    
      IO_WRITE8(SIOWD0,  0x00);    
   #endif
    
   /* No autoload if the following two buttons are depressed on startup:
    * 
    * Color and Black Buttons or
    */
   {
      volatile Uint8* pData=(Uint8*)0xFC000000;
      volatile Uint8* pRow=(Uint8*)0xFC000001;
      Uint8 data;

      *pRow = 0x20;   // select row 2
      data = *pData;  // get key data

      if ( 0x30 == (0x30 & data) )    // both Mono Copy and Color Copy buttons are pressed
      {
         AutoLoad = FALSE;
      }
      else 
      {
         AutoLoad = TRUE;
      }
   }

   InitSerial(CLOCKMHz, BAUDRATE);
    
   PSPRINTF("\n----------------------------------------\n");
   PSPRINTF("Zoran Loader Build Date: %s\n", builddate);
   PSPRINTF("TCM[0-1]                     : 0x%08lX 0x%08lX\n", 
            *(Uint32 *)TCMBASE,*(Uint32 *)(TCMBASE+4));
   PSPRINTF("SDRAM Configuration (SDCONF) : 0x%08lX\n", IO_READ32(SDCONF));
   PSPRINTF("Physical Memory Size         : %ld bytes\n", PhysicalMemorySize);
   PSPRINTF("System Clock Speed           : %d.%d MHz\n", ClockSpeed/MHZ,
            (ClockSpeed - (ClockSpeed/MHZ)*MHZ)*10/MHZ);
   if ((ClockMod & 4) == 0)
      PSPRINTF("Frequency Modulation         : %d.%d%%\n",
               ((ClockMod + 1) * 25)/10, ((ClockMod + 1) * 25)%10);
   PSPRINTF("Loader Version               : %s\n", sys_version);

   n = GetResetSource();
   PSPRINTF("Reset was initiated by       : %s\n", ResetSrc[n]);

#ifdef VERBOSE  
   PSPRINTF("PLLCTR = 0x%08lX\n",IO_READ32(PLLCTR));
   PSPRINTF("GetClockSpeed returned %d\n", ClockSpeed);
   PSPRINTF("bottom_of_heap    = 0x%08lX\n", (Uint32 *)&bottom_of_heap);
   PSPRINTF("SystemMemoryStart = 0x%08lX\n", (Uint32 *)SystemMemoryStart);
   PSPRINTF("SystemMemorysize  = %ld bytes\n", (Uint32 *)SystemMemorySize);
#endif

   if (AutoLoad) {
    
      // Set the serial flash to memory mapped mode
    
      SPI_CacheReadEnable();

      // Read the firmware header
      memcpy((void *)Hdr, (void *)DEFAULT_ROM_ADRS, HeaderSize);
      PSPRINTF("Loader: verifying header\n");
        
      // Verify that it is valid
      if (*((Uint32 *)Hdr) == *((Uint32 *)FirmwareSeq)) {
         FWCmprCodeSize  = *((Uint32 *)&Hdr[4]) >> 8;
         FWChecksum  = *((Uint32 *)&Hdr[8]);
         FWLength    = *((Uint32 *)&Hdr[12]);
         FWAddress   = *((Uint32 *)&Hdr[16]);
         FWProgAdr   = (DEFAULT_ROM_ADRS + HeaderSize);

         p = (Uint8 *)FWProgAdr;
         RWsize = *((Uint32 *)&p[FWCmprCodeSize]);
            
         PSPRINTF("Loader: Valid firmware header\n");

         // One more step. Verify the checksum.
         p = (Uint8 *)FWProgAdr;
         for (CalcChecksum = 0, n = 0; n < FWLength; n++) {
            CalcChecksum += *p++;
         }
            
#ifdef VERBOSE
         PSPRINTF("FWProgAdr = 0x%08lX\n", FWProgAdr);
         PSPRINTF("FWLength = %d, CalcChecksum = 0x%08lX\n", 
                  FWLength, CalcChecksum);
#endif
            
         if (CalcChecksum != FWChecksum) {
            PSPRINTF("Loader: BAD firmware checksum ... aborting!\n");
            PSPRINTF("Loader: FWChecksum    = 0x%08lX\n", FWChecksum);
            PSPRINTF("Loader: CalcChecksum  = 0x%08lX\n", CalcChecksum);
         }
         else {
            Uint32 *source = (Uint32 *)FWProgAdr;
            Uint32 src_size = FWLength;
#if 0
            Uint32 *dest   = (Uint32 *)Buffer;
            Uint32 dst_size = sizeof(Buffer);
#else
            Uint32 *dest, dst_size;

            // Malloc a buffer for decompression.
            // There is PLENTY of memory available, so don't check!

            dst_size = (Uint32)BUFFER_SIZE;
            dest = malloc(dst_size);
#endif
                
            // It's good. Load and execute the program.
            PSPRINTF("Loader: Serial flash contains valid program\n");

            // Uncompress the image first
            sts = uncompress((void *)dest, (Uint32 *)&dst_size,
                             (void *)source, src_size);

            // Set SPI bus to the normal mode
 
            SPI_CmdModeEnable(FALSE);

            // Check the uncompression result

            if (sts != 0) {
               PSPRINTF("Loader: uzip ERROR %d\n", sts);
            }
            else {
               if (FWCmprCodeSize) {
                  PSPRINTF("Loader: Compressed code size  = %ld bytes\n", FWCmprCodeSize);
                  PSPRINTF("Loader: RO segment size  = %8ld bytes\n", dst_size);
                  PSPRINTF("Loader: RW segment size  = %8ld bytes (loaded from ROM)\n", RWsize);
               }

               PSPRINTF("Loader: Loading firmware to RAM address 0x%08lX\n",
                        FWAddress);
               FWload((Uint32 *)FWAddress, dest, dst_size);
            }
         }
      }

      // Set SPI bus to the normal mode
 
      SPI_CmdModeEnable(FALSE);
   }
   else {
      // Auto-load disabled by buttons
      PSPRINTF("Auto-load disabled! Waiting for USB input.\n");
   }
    
   /* Auto load failed or it was disabled.
    * Wait for USB input of firmware load/program data.
    */
    
   // Set the normal SPI mode so USB can read NVRAM in flash

   SPI_Config();
   LockSPIMode(TRUE);

   // Just wait for USB data!
   while (TRUE) {
      WaitForFirmwareData();
   }      
}


/* ARM Exception Handlers:
 */

__irq void ISR_Interrupt(void)
{
    Uint32 ExcCauseA, ExcCauseB;
    
    /* Read then clear the Exception Cause Registers
     */
    ExcCauseA = IO_READW(EXC1A);
    ExcCauseB = IO_READW(EXC1B);
    IO_WRITEW(EXC1A,ExcCauseA);
    IO_WRITEW(EXC1B,ExcCauseB);
    
    /* We could be "general purpose here and poll every source,
     * but since there SHOULD be only one source of an interrupt (USB),
     * then just test for it and report an error if anything else.
     */
    if (ExcCauseA & EC_USB2) {
        ExcCauseA ^= EC_USB2;
#ifndef ZR4050        
        USB_lisr(1);
#else
        _usb_dci_vusb11_isr();
#endif        
    }
    
    if (ExcCauseA & EC_TIM0) {
        ExcCauseA ^= EC_TIM0;
    }
    
#if 0
    if (ExcCauseA | ExcCauseB)
        PSPRINTF("Spurious Interrupt!\n");
#endif
    
}

__irq void ISR_Undefined_Instruction(void)
{
    PSPRINTF("ARM EXCEPTION --- Undefined Instruction\n");
}

__irq void ISR_Software_Interrupt(void)
{
    PSPRINTF("ARM EXCEPTION --- Software Interrupt (SWI)\n");
}

__irq void ISR_Prefetch_Abort(void)
{
    PSPRINTF("ARM EXCEPTION --- Prefetch Abort\n");
}

__irq void ISR_Data_Abort(void)
{
    PSPRINTF("ARM EXCEPTION --- Data Abort\n");
}

__irq void ISR_Fast_Interrupt(void)
{
    PSPRINTF("ARM EXCEPTION --- Fast Interrupt\n");
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -