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

📄 flashutil.c

📁 采用ST20 CPU的机顶盒的烧写程序
💻 C
📖 第 1 页 / 共 5 页
字号:
/*
 * File     : flashutil.c
 * Synopsys : FLASH utilities.
 *
 * Copyright (c) 2003-2007 STMicroelectronics Limited. All right reserved.
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <os21.h>

/*
 * Define device specific macro based on platform type;
 * should be removed when defined externally.
 */
#if defined(__st200__)
#if defined(__mb379__) || defined(__mb392__)
#define __stm8000__
#elif defined(__mb385__)
#define __stm5700__
#elif defined(__mb390__) || defined(__mb424__) || defined(__mb435__)
#define __stb5301__
#elif defined(__mb421__) || defined(__mb426__)
#define __stm8010__
#elif defined(__mb428__)
#define __stx5525__
#endif /* board types */
#else /* !__st200__ */
#include <boardreg.h>
#if defined(__db457__) || defined(__mb293__) || defined(__mb350__) || defined(__mb360__) || defined(__mb374__)
#define __st40ra__
#elif defined(__mb317a__) || defined(__mb317b__) || defined(__mediaref__)
#define __st40gx1__
#elif defined(__espresso__) || defined(__mb376__)
#define __sti5528__
#elif defined(__mb379__) || defined(__mb392__) || defined(__tmmidr04__) || defined(__tmmlr1__) || defined(__tmmlr2__)
#define __stm8000__
#elif defined(__mb411__) || defined(__mb411stb7100__) || defined(__mb442__) || defined(__mb442stb7100__) || defined(__stb7100ref__)
#define __stb7100__
#elif defined(__mb411stb7109__) || defined(__mb442stb7109__) || defined(__mb448__)
#define __stb7109__
#elif defined(__mb422__)
#define __std2000__
#elif defined(__mb519__)
#define __sti7200__
#elif defined(__mb521__)
#define __stv0498__
#elif defined(__mb548__)
#define __std1000__
#endif /* platform types */
#endif /* __st200__ */

#include "flash.h"
#ifdef __ST231__
#include "tlbhelper.h"
#endif /* __ST231__ */

#define ST_ID16                    0x00000020
#define ST_ID                      0x00200020
#define AMD_ID16                   0x00000001
#define AMD_ID                     0x00010001
#define INTEL_ID16                 0x00000089
#define INTEL_ID                   0x00890089
#define MICRON_ID16                0x0000002C
#define MICRON_ID                  0x002C002C
#define SPANSION_ID 0x01

#define BLOCK_ERASE_TIMEOUT        10
#define UNPROTECT_TIMEOUT          10
#define WRITE_TIMEOUT              10
#define CHIP_ERASE_TIMEOUT         60
#define PAUSE_PERIOD               (time_ticks_per_sec () / 2)

/*
 * Flash commands
 */
#define CMD_ERASE_SETUP            0x00200020
#define CMD_PROGRAM_SETUP          0x00400040
#define CMD_CLEAR_STATUS_REG       0x00500050
#define CMD_READ_ESIG              0x00900090
#define CMD_CONFIRM                0x00D000D0
#define CMD_PROTECTION_SETUP       0x00600060
#define CMD_READ_STATUS_REG        0x00700070
#define CMD_READ_ARRAY             0x00FF00FF

#define CMD_ERASE_SETUP16          0x00000020
#define CMD_PROGRAM_SETUP16        0x00000040
#define CMD_CLEAR_STATUS_REG16     0x00000050
#define CMD_READ_ESIG16            0x00000090
#define CMD_CONFIRM16              0x000000D0
#define CMD_PROTECTION_SETUP16     0x00000060
#define CMD_READ_STATUS_REG16      0x00000070
#define CMD_READ_ARRAY16           0x000000FF

/*
 * Status register bits
 */
#define PECS_STATUS_BIT            0x00800080
#define STATUS_MASK                0x00FE00FE
#define PROTECT_STATUS_BIT         0x00010001
#define VPP_STATUS_BIT             0x00080008
#define ESS_STATUS_BIT             0x00400040
#define ES_STATUS_BIT              0x00200020

#define PECS_STATUS_BIT16          0x00000080
#define STATUS_MASK16              0x000000FE
#define PROTECT_STATUS_BIT16       0x00000001
#define VPP_STATUS_BIT16           0x00000008
#define ESS_STATUS_BIT16           0x00000040
#define ES_STATUS_BIT16            0x00000020

/*
 * Polynomial value used for CRCs
 */
#define CRC_POLY                   0x04C14BD7

/* PIO setup macros - derived from OSPlus code (may be needed for VPP control) */
#define PIO_CFG_IN                 0x0
#define PIO_CFG_BIDIR              0x1
#define PIO_CFG_OUT                0x2
#define PIO_CFG_ALT_IN             0x5
#define PIO_CFG_ALT_OUT            0x6
#define PIO_CFG_ALT_BIDIR          0x7

#define PIO_CONFIGURE(BASE, BIT, MODE)\
  (*((volatile unsigned int*) (BASE+0x28)) = (1<<(BIT)));\
  (*((volatile unsigned int*) (BASE+0x38)) = (1<<(BIT)));\
  (*((volatile unsigned int*) (BASE+0x48)) = (1<<(BIT)));\
  (*((volatile unsigned int*) (BASE+0x24)) = (((MODE)&0x1)?1:0)<<(BIT));\
  (*((volatile unsigned int*) (BASE+0x34)) = (((MODE)&0x2)?1:0)<<(BIT));\
  (*((volatile unsigned int*) (BASE+0x44)) = (((MODE)&0x4)?1:0)<<(BIT));

/*
 * Local prototypes
 */
static void switchOnVDD(flashTarget_t* target);
static void switchOffVDD(flashTarget_t* target);
static void writeEnable(flashTarget_t* target);
static void writeDisable(flashTarget_t* target);
static void resetM29W(flashDevice_t* device);
static void resetM29W16(flashDevice_t* device);
static void resetM58LT16(flashDevice_t* device);
static void resetM58LW(flashDevice_t* device);
static void resetM58LW16(flashDevice_t* device);
static void blockUnprotectCFI16(flashDevice_t* device, unsigned int blockAddress);
static void blockUnprotectCFI(flashDevice_t* device, unsigned int blockAddress);
static void chipEraseM29W(flashTarget_t* target, flashDevice_t* device, flashProgress_t progressFn);
static void chipEraseSimple(flashTarget_t* target, flashDevice_t* device, flashProgress_t progressFn);
static void blockEraseM29W(flashDevice_t* device, unsigned int blockAddress);
static void blockEraseM29W16(flashDevice_t* device, unsigned int blockAddress);
static void blockEraseCFI16(flashDevice_t* device, unsigned int blockAddress);
static void blockEraseCFI(flashDevice_t* device, unsigned int blockAddress);
static void checkErase(flashDevice_t* device, unsigned int blockAddress);
static void showProgress(flashProgress_t progressFn, unsigned int done, unsigned int total);
static void writeWordM29W(flashDevice_t* device, unsigned int address, unsigned int value);
static void writeWordM29W16(flashDevice_t* device, unsigned int address, unsigned int value);
static void writeWordCFI(flashDevice_t* device, unsigned int address, unsigned int value);
static void writeWordCFI16(flashDevice_t* device, unsigned int address, unsigned int value);
#if defined(__st200__)
#if defined(__stm8000__)
static void writeEnable_mb379(void);
static void writeDisable_mb379(void);
static void writeEnable_mb392(void);
static void writeDisable_mb392(void);
#elif defined(__stb5301__)
static void writeEnable_mb390(void);
static void writeDisable_mb390(void);
#elif defined(__stm8010__)
static void writeEnable_mb421(void);
static void writeDisable_mb421(void);
static void writeEnable_mb426(void);
static void writeDisable_mb426(void);
#elif defined(__stx5525__)
static void switchOnVDD_mb428(void);
static void switchOffVDD_mb428(void);
#endif
#else
#if defined(__st40ra__)
static void writeEnable_mb293(void);
static void writeDisable_mb293(void);
static void writeEnable_mb374(void);
static void writeDisable_mb374(void);
#elif defined(__st40gx1__)
static void switchOnVDD_mb317(void);
static void switchOffVDD_mb317(void);
#elif defined(__sti5528__)
static void writeEnable_mb376(void);
static void writeDisable_mb376(void);
static void switchOnVDD_mb376(void);
static void switchOffVDD_mb376(void);
#elif defined(__stm8000__)
static void writeEnable_mb379(void);
static void writeDisable_mb379(void);
static void writeEnable_mb392(void);
static void writeDisable_mb392(void);
static void writeEnable_tmmidr04(void);
static void writeDisable_tmmidr04(void);
static void writeEnable_tmmlr2(void);
static void writeDisable_tmmlr2(void);
#elif defined(__stb7100__) || defined(__stb7109__)
static void writeEnable_mb411(void);
static void writeDisable_mb411(void);
#elif defined(__sti7200__)
static void writeEnable_mb519(void);
static void writeDisable_mb519(void);
#endif
#endif

/*
 * Block layout of the FLASH parts used on our boards
 *
 * These are address and size pairs. Read the tables
 * as 'addresses below this are broken up into blocks
 * of this size'.
 */
static flashBlockInfo_t M58LT256GT_blockInfo[] = {
  {0x1FE0000, 0x20000},
  {0x2000000, 0x08000},
  {0x0000000, 0x00000}
};

static flashBlockInfo_t M58LT128GSB_blockInfo[] = {
  {0x0020000, 0x08000},
  {0x1000000, 0x20000},
  {0x0000000, 0x00000}
};

static flashBlockInfo_t M29W160BT_blockInfo[] = {
  {0x3E0000, 0x20000},
  {0x3F0000, 0x10000},
  {0x3F8000, 0x04000},
  {0x400000, 0x08000},
  {0x000000, 0x00000}
};

static flashBlockInfo_t M29W160BB_blockInfo[] = {
  {0x008000, 0x08000},
  {0x010000, 0x04000},
  {0x020000, 0x10000},
  {0x400000, 0x20000},
  {0x000000, 0x00000}
};

static flashBlockInfo_t M28W320CB_blockInfo[] = {
  {0x7E0000, 0x20000},
  {0x800000, 0x04000},
  {0x000000, 0x00000}
};

static flashBlockInfo_t E28F640J3A_blockInfo[] = {
  {0x1000000, 0x40000},
  {0x0000000, 0x00000}
};

static flashBlockInfo_t E28F640J3A16_blockInfo[] = {
  {0x0800000, 0x20000},
  {0x0000000, 0x00000}
};

static flashBlockInfo_t M58LW064C_blockInfo[] = {
  {0x800000, 0x20000},
  {0x000000, 0x00000}
};

static flashBlockInfo_t M58LW032A_blockInfo[] = {
  {0x800000, 0x20000},
  {0x000000, 0x00000}
};

/* This is for 2 M29DW324DB chips in parallel across the address bus */
static flashBlockInfo_t M29DW324DB_blockInfo[] = {
  {0x020000, 0x04000},
  {0x800000, 0x20000},
  {0x000000, 0x00000}
};

static flashBlockInfo_t M29DW324DB16_blockInfo[] = {
  {0x010000, 0x02000},
  {0x400000, 0x10000},
  {0x000000, 0x00000}
};

static flashBlockInfo_t M29KW064E_blockInfo[] = {
  {0x800000, 0x40000},
  {0x000000, 0x00000}
};

/* Used for M29W640DT and FT parts */
static flashBlockInfo_t M29W640DT_blockInfo[] = {
  {0x07F0000, 0x10000},
  {0x0800000, 0x02000},
  {0x0000000, 0x00000}
};

/* Used for M29W640DB and FB parts */
static flashBlockInfo_t M29W640DB_blockInfo[] = {
  {0x0010000, 0x02000},
  {0x0800000, 0x10000},
  {0x0000000, 0x00000}
};

static flashBlockInfo_t M29W320EB_blockInfo[] = {
  {0x0010000, 0x02000},
  {0x0400000, 0x10000},
  {0x0000000, 0x00000}
};

static flashBlockInfo_t M28W640FSUx16_blockInfo[] = {
  {0x0800000, 0x20000},
  {0x0000000, 0x00000}
};

/*
 * The list of support FLASH parts, and their attributes
 */
static flashDevice_t supportedDevices[] = {
  {
   M58LT256GT,
   "M58LT256GT",
   NULL,
   0x00008870,
   1024 * 1024 * 32,
   0,
   M58LT256GT_blockInfo,

   resetM58LT16,
   blockUnprotectCFI16,
   blockEraseCFI16,
   chipEraseSimple,
   writeWordCFI16
  },
  {
   M58LT256GT_ALT_ID,
   "M58LT256GT_ALT_ID",
   NULL,
   0x0000880D,
   1024 * 1024 * 32,
   0,
   M58LT256GT_blockInfo,

   resetM58LT16,
   blockUnprotectCFI16,
   blockEraseCFI16,
   chipEraseSimple,
   writeWordCFI16
  },
  {
   M58LT128GSB,
   "M58LT128GSB",
   NULL,
   0x000088C7,
   1024 * 1024 * 16,
   0,
   M58LT128GSB_blockInfo,

   resetM58LT16,
   blockUnprotectCFI16,
   blockEraseCFI16,
   chipEraseSimple,
   writeWordCFI16
  },
  {
   M29W160BT,
   "M29W160BT/DT",
   NULL,
   0x22C422C4,
   1024 * 1024 * 4,
   0,
   M29W160BT_blockInfo,

   resetM29W,
   NULL,
   blockEraseM29W,
   chipEraseM29W,
   writeWordM29W
  },
  {
   M29W160BB,
   "M29W160BB/DB",
   NULL,
   0x22492249,
   1024 * 1024 * 4,
   0,
   M29W160BB_blockInfo,

   resetM29W,
   NULL,
   blockEraseM29W,
   chipEraseM29W,
   writeWordM29W
  },
  {
   M29W640DT,
   "M29W640DT",
   NULL,
   0x000022DE,
   1024 * 1024 * 8,
   0,
   M29W640DT_blockInfo,

   resetM29W16,
   NULL,
   blockEraseM29W16,
   chipEraseM29W,
   writeWordM29W16
  },
  {
   M29W640DB,
   "M29W640DB",
   NULL,
   0x000022DF,
   1024 * 1024 * 8,
   0,
   M29W640DB_blockInfo,

   resetM29W16,
   NULL,
   blockEraseM29W16,
   chipEraseM29W,
   writeWordM29W16
  },
  {
   M29W640FT,
   "M29W640FT",
   NULL,
   0x000022ED,
   1024 * 1024 * 8,
   0,
   M29W640DT_blockInfo,

   resetM29W16,
   NULL,
   blockEraseM29W16,
   chipEraseM29W,
   writeWordM29W16
  },
  {
   M29W640FB,
   "M29W640FB",
   NULL,
   0x000022FD,
   1024 * 1024 * 8,
   0,
   M29W640DB_blockInfo,

   resetM29W16,
   NULL,
   blockEraseM29W16,
   chipEraseM29W,
   writeWordM29W16
  },
  {
   M29W320EB,
   "M29W320EB",
   NULL,
   0x00002257,
   1024 * 1024 * 4,
   0,
   M29W320EB_blockInfo,

   resetM29W16,
   NULL,
   blockEraseM29W16,

⌨️ 快捷键说明

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