📄 flash.c
字号:
/* * (C) Copyright 2001 * Wolfgang Denk, DENX Software Engineering, wd@denx.de. * Keith Outwater, keith_outwater@mvsi.com * * See file CREDITS for list of people who contributed to this * project. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of * the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, * MA 02111-1307 USA */#include <common.h>#include <mpc8xx.h>#if defined(CFG_ENV_IS_IN_FLASH)# ifndef CFG_ENV_ADDR# define CFG_ENV_ADDR (CFG_FLASH_BASE + CFG_ENV_OFFSET)# endif# ifndef CFG_ENV_SIZE# define CFG_ENV_SIZE CFG_ENV_SECT_SIZE# endif# ifndef CFG_ENV_SECT_SIZE# define CFG_ENV_SECT_SIZE CFG_ENV_SIZE# endif#endif/* * Use buffered writes to flash by default - they are about 32x faster than * single byte writes. */#ifndef CFG_GEN860T_FLASH_USE_WRITE_BUFFER#define CFG_GEN860T_FLASH_USE_WRITE_BUFFER#endif/* * Max time to wait (in mS) for flash device to allocate a write buffer. */#ifndef CFG_FLASH_ALLOC_BUFFER_TOUT#define CFG_FLASH_ALLOC_BUFFER_TOUT 100#endif/* * These functions support a single Intel StrataFlash device (28F128J3A) * in byte mode only!. The flash routines are very basic and simple * since there isn't really any remapping necessary. *//* * Intel SCS (Scalable Command Set) command definitions * (taken from 28F128J3A datasheet) */#define SCS_READ_CMD 0xff#define SCS_READ_ID_CMD 0x90#define SCS_QUERY_CMD 0x98#define SCS_READ_STATUS_CMD 0x70#define SCS_CLEAR_STATUS_CMD 0x50#define SCS_WRITE_BUF_CMD 0xe8#define SCS_PROGRAM_CMD 0x40#define SCS_BLOCK_ERASE_CMD 0x20#define SCS_BLOCK_ERASE_RESUME_CMD 0xd0#define SCS_PROGRAM_RESUME_CMD 0xd0#define SCS_BLOCK_ERASE_SUSPEND_CMD 0xb0#define SCS_SET_BLOCK_LOCK_CMD 0x60#define SCS_CLR_BLOCK_LOCK_CMD 0x60/* * SCS status/extended status register bit definitions */#define SCS_SR7 0x80#define SCS_XSR7 0x80/*---------------------------------------------------------------------*/#if 0#define DEBUG_FLASH#endif#ifdef DEBUG_FLASH#define PRINTF(fmt,args...) printf(fmt ,##args)#else#define PRINTF(fmt,args...)#endif/*---------------------------------------------------------------------*/flash_info_t flash_info[CFG_MAX_FLASH_BANKS];/*----------------------------------------------------------------------- * Functions */static ulong flash_get_size (vu_char *addr, flash_info_t *info);static int write_data8 (flash_info_t *info, ulong dest, uchar data);static void flash_get_offsets (ulong base, flash_info_t *info);/*----------------------------------------------------------------------- * Initialize the flash memory. */unsigned longflash_init (void){ volatile immap_t *immap = (immap_t *)CFG_IMMR; volatile memctl8xx_t *memctl = &immap->im_memctl; unsigned long size_b0; int i; for (i= 0; i < CFG_MAX_FLASH_BANKS; ++i) { flash_info[i].flash_id = FLASH_UNKNOWN; } /* * The gen860t board only has one FLASH memory device, so the * FLASH Bank configuration is done statically. */ PRINTF("\n## Get flash bank 1 size @ 0x%08x\n", FLASH_BASE0_PRELIM); size_b0 = flash_get_size((vu_char *)FLASH_BASE0_PRELIM, &flash_info[0]); if (flash_info[0].flash_id == FLASH_UNKNOWN) { printf ("## Unknown FLASH on Bank 0: " "ID 0x%lx, Size = 0x%08lx = %ld MB\n", flash_info[0].flash_id,size_b0, size_b0 << 20); } PRINTF("## Before remap:\n" " BR0: 0x%08x OR0: 0x%08x\n BR1: 0x%08x OR1: 0x%08x\n", memctl->memc_br0, memctl->memc_or0, memctl->memc_br1, memctl->memc_or1); /* * Remap FLASH according to real size */ memctl->memc_or0 |= (-size_b0 & 0xFFFF8000); memctl->memc_br0 |= (CFG_FLASH_BASE & BR_BA_MSK); PRINTF("## After remap:\n" " BR0: 0x%08x OR0: 0x%08x\n", memctl->memc_br0, memctl->memc_or0); /* * Re-do sizing to get full correct info */ size_b0 = flash_get_size ((vu_char *)CFG_FLASH_BASE, &flash_info[0]); flash_get_offsets (CFG_FLASH_BASE, &flash_info[0]); flash_info[0].size = size_b0;#if CFG_MONITOR_BASE >= CFG_FLASH_BASE /* * Monitor protection is ON by default */ flash_protect(FLAG_PROTECT_SET, CFG_MONITOR_BASE, CFG_MONITOR_BASE + monitor_flash_len - 1, &flash_info[0]);#endif#ifdef CFG_ENV_IS_IN_FLASH /* * Environment protection ON by default */ flash_protect(FLAG_PROTECT_SET, CFG_ENV_ADDR, CFG_ENV_ADDR + CFG_ENV_SECT_SIZE - 1, &flash_info[0]);#endif PRINTF("## Final Flash bank size: 0x%08lx\n",size_b0); return (size_b0);}/*----------------------------------------------------------------------- * Fill in the FLASH offset table */static voidflash_get_offsets (ulong base, flash_info_t *info){ int i; if (info->flash_id == FLASH_UNKNOWN) { return; } switch (info->flash_id & FLASH_VENDMASK) { case FLASH_MAN_INTEL: for (i = 0; i < info->sector_count; i++) { info->start[i] = base; base += 1024 * 128; } return; default: printf ("Don't know sector offsets for FLASH" " type 0x%lx\n", info->flash_id); return; }}/*----------------------------------------------------------------------- * Display FLASH device info */voidflash_print_info (flash_info_t *info){ int i; if (info->flash_id == FLASH_UNKNOWN) { printf ("Missing or unknown FLASH type\n"); return; } switch (info->flash_id & FLASH_VENDMASK) { case FLASH_MAN_INTEL: printf ("Intel "); break; default: printf ("Unknown Vendor "); break; } switch (info->flash_id & FLASH_TYPEMASK) { case FLASH_28F128J3A: printf ("28F128J3A (128Mbit = 128K x 128)\n"); break; default: printf ("Unknown Chip Type\n"); break; } if (info->size >= (1024 * 1024)) { i = 20; } else { i = 10; } printf (" Size: %ld %cB in %d Sectors\n", info->size >> i, (i == 20) ? 'M' : 'k', info->sector_count); printf (" Sector Start Addresses:"); for (i=0; i<info->sector_count; ++i) { if ((i % 5) == 0) printf ("\n "); printf (" %08lX%s", info->start[i], info->protect[i] ? " (RO)" : " " ); } printf ("\n"); return;}/*----------------------------------------------------------------------- * Get size and other information for a FLASH device. * NOTE: The following code cannot be run from FLASH! */staticulong flash_get_size (vu_char *addr, flash_info_t *info){#define NO_FLASH 0 vu_char value[2]; /* * Try to read the manufacturer ID */ addr[0] = SCS_READ_CMD; addr[0] = SCS_READ_ID_CMD; value[0] = addr[0]; value[1] = addr[2]; addr[0] = SCS_READ_CMD; PRINTF("Manuf. ID @ 0x%08lx: 0x%02x\n", (ulong)addr, value[0]); switch (value[0]) { case (INTEL_MANUFACT & 0xff): info->flash_id = FLASH_MAN_INTEL; break; default: info->flash_id = FLASH_UNKNOWN; info->sector_count = 0; info->size = 0; return (NO_FLASH); } /* * Read the device ID */ PRINTF("Device ID @ 0x%08lx: 0x%02x\n", (ulong)(&addr[2]), value[1]); switch (value[1]) { case (INTEL_ID_28F128J3A & 0xff): info->flash_id += FLASH_28F128J3A; info->sector_count = 128; info->size = 16 * 1024 * 1024; break; default: info->flash_id = FLASH_UNKNOWN; return (NO_FLASH); } if (info->sector_count > CFG_MAX_FLASH_SECT) { printf ("** ERROR: sector count %d > max (%d) **\n", info->sector_count, CFG_MAX_FLASH_SECT); info->sector_count = CFG_MAX_FLASH_SECT; } return (info->size);}/*----------------------------------------------------------------------- * Erase the specified sectors in the specified FLASH device */intflash_erase(flash_info_t *info, int s_first, int s_last)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -