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

📄 nfc_ipl_read.c

📁 mx21的NAND Flash Bootloader源代码
💻 C
字号:
#ifndef __NFC_IPL_READ_C_
#define __NFC_IPL_READ_C_
#endif

#include <stdio.h>
#include "mx2.h"
#include "nfc.h"

u32 _gPageNumPerBlock    = 32;
u32 _gRowAddressCycleNum = 2;
u32 _gColAddressCycleNum = 1;
u32 _gMainPageByteSize   = 512;
u32 _gSparePageByteSize  = 16;

static void MemoryCopy(void* SourceAddress, void* TargetAddress, u32 ByteSize)
{
  u32 i;

  for(i=0; i<(ByteSize/4); i++)
     *(u32 *)((u32 *)TargetAddress+i) = *(u32 *)((u32 *)SourceAddress+i);
}

/*****************************************************************************/

static void nfc_RAM_buffer_select(u8 BufferNum)
{
   _reg_NFC_RAM_BUF_ADDR = (u16)BufferNum;
}

/* Activate one address cycle */
static void nfc_address_input(u16 address)
{
//   printf("0x%X\n", address);
   _reg_NFC_NAND_FLASH_ADDR = address;

   _reg_NFC_NF_CONFIG2 = 0x0002;

   while(NFC_BUSY);    /* Wait for Basic Operation Complete */
}

static void nfc_address_cycle(u16 iPageNum, u8 RowAddressCycleNum, u8 iColumnCycleNum)
{
  u8 i;

  for(i=0; i<iColumnCycleNum; i++)  
    nfc_address_input(0);

  for(i=0; i<RowAddressCycleNum; i++)
    nfc_address_input( (u16)(iPageNum>>(i*8)) );
}

static void nfc_command_input(u16 command)
{
    _reg_NFC_NAND_FLASH_CMD  = command;

    _reg_NFC_NF_CONFIG2 = 0x0001;
    
    while(NFC_BUSY); /* Wait for Basic Operation Complete */
}

static void nfc_flash_data_output(void)
{
   _reg_NFC_NF_CONFIG2 = 0x0008;

   while(NFC_BUSY);
}

/*****************************************************************************/

u32 nfc_read_status(void)
{
  u32 status;

  nfc_RAM_buffer_select(3);
  nfc_command_input(NAND_CMD_READ_STATUS);
  
  _reg_NFC_NF_CONFIG2 = 0x0020; // FDO -> Read Status
  
  while(NFC_BUSY);

  status = (*(volatile u32 *)(NFC_MAB3_BASE)) & 0xFF ;
  
  return status;
}

u32 nfc_status_ready(void)
{
#define NAND_STATUS_READY_BIT 0x40

  if( NAND_STATUS_READY_BIT & nfc_read_status() )
    return 1;
  else
    return 0;  
}

/*****************************************************************************/
/*                              NFC READ                                     */
/*****************************************************************************/

/*****************************************************************************/
/*                                                                           */
/* NAME                                                                      */
/*      nfc_read                                                             */
/* DESCRIPTION                                                               */
/*      This function reads NAND page area (528Bytes size)                   */
/* PARAMETERS                                                                */
/*      iPageNum        wanted page number of the upper blk_num block        */
/*                      of the NAND Flash to read                            */
/*      pMBuf           unsigned char type pointer of main area sized        */
/*                      buffer to read                                       */
/*      pSBuf           unsigned char type pointer of spare area sized       */
/*                      buffer to read                                       */
/* RETURN VALUES                                                             */
/*      This function returns NFC_NO_ERR when it does successfully.          */
/*      If previous write error occurs, this function returns NFC_WRITE_ERR. */
/*      If ECC error occured, ECC related error is returned                  */
/*                                                                           */
/*****************************************************************************/
u32 nfc_read(u16 iPageNum, u32* pMBuf, u32* pSBuf)
{
  if( (pMBuf == (u32*)0) && (pSBuf == (u32*)0) )
    return NFC_ILLEGAL_ACCESS;
    
  if(!nfc_status_ready())
    return NFC_WRITE_ERR;

  _reg_NFC_BLK_ADD_LOCK = iPageNum / 32;

  nfc_RAM_buffer_select(0);

  _reg_NFC_NF_CONFIG1 = NFC_CONFIG1_READ; 
  nfc_command_input(NAND_CMD_READ);
  nfc_address_cycle(iPageNum, _gRowAddressCycleNum, _gColAddressCycleNum);
  nfc_flash_data_output();
  if(pMBuf != (u32*)0)
    MemoryCopy((void*)NFC_MAB0_BASE, (void*)pMBuf, _gMainPageByteSize);
  if(pSBuf != (u32*)0)
    MemoryCopy((void*)NFC_SAB0_BASE, (void*)pSBuf, _gSparePageByteSize);

  return NFC_NO_ERR;  
}

/*****************************************************************************/
/*                                                                           */
/* NAME                                                                      */
/*      nfc_read_page (interface function)                                   */
/* DESCRIPTION                                                               */
/*      This function reads NAND page area (528Bytes size)                   */
/* PARAMETERS                                                                */
/*      iPageNum        wanted page number of the upper blk_num block        */
/*                      of the NAND Flash to read                            */
/*      pMBuf       	unsigned char type pointer of main area sized        */
/*                      buffer to read                                       */
/*      pSBuf           unsigned char type pointer of spare area sized       */
/*                      buffer to read                                       */
/* RETURN VALUES                                                             */
/*      This function returns EAG_NO_ERR when it does successfully.          */
/*      If previous write error occurs, this function returns EAG_WRITE_ERR. */
/*      If ECC error occured, ECC related error is returned                  */
/*                                                                           */
/*****************************************************************************/
u32 nfc_read_page(u16 iPageNum, u32* pMBuf, u32* pSBuf)
{
 return(nfc_read(iPageNum, (u32*)pMBuf, (u32*)pSBuf));
}                            

void nfc_block_unlock(u16 iStartBlockNum, u16 iEndBlockNum)
{
  _reg_NFC_ULOCK_START_BLK = iStartBlockNum;
  _reg_NFC_ULOCK_END_BLK   = iEndBlockNum;
  _reg_NFC_NF_WR_PROT      = 0x0004;
}

/*****************************************************************************/

void nfc_init(void)
{

  _reg_SYS_FMCR  &= ~( NF_FMS_2KB );
  _reg_SYS_FMCR |= NF_16BIT;
  
  _reg_CRM_PCDR0  &= ~0x0000F000;
  _reg_CRM_PCDR0  |=  0x00009000;    // NFCDIV: keep even numbers on the NFCDIV to get a clock aligned with the AHB clock edges
  _reg_CRM_PCCR0 |=  0x00080000;    // Enable NFC

  nfc_block_unlock(0, 0xFFFF);

  _reg_NFC_CONFIGURATION = 0x0002; // unlock the internal RAM buffer
  _reg_NFC_NF_CONFIG2    = 0x0000; // clear all operation
}

⌨️ 快捷键说明

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