?? flash.c
字號:
/* This file contains all of the flash support code that does not need to be relocated to RAM. Two separate files (flash.c and flashpic.c) are maintained because under certain compilers, they may need to be compiled with different options to be made position independent. NOTE: THESE FUNCTIONS ARE NOT RE-ENTRANT!!! All of the FLASH routines assume they can copy themselves into the array FlashFunc[]; hence, only one operation can be active at any time.*/#include "config.h"#if INCLUDE_FLASH#include "cpu.h"#include "flash.h"typedef unsigned char uchar;typedef unsigned short ushort;typedef unsigned long ulong;typedef volatile unsigned char vuchar;typedef volatile unsigned short vushort;typedef volatile unsigned long vulong;typedef volatile unsigned int vuint;typedef volatile int vint;extern int FLASHTYPE();extern int ENDFLASHTYPE();extern int FLASHWRITE();extern int ENDFLASHWRITE();extern int FLASHEWRITE();extern int ENDFLASHEWRITE();extern int FLASHERASE();extern int ENDFLASHERASE();int FlashOpError();/* FlashFunc[]: This array will contain the flash operation function that is executing. Recall that to operate on the flash, you cannot be executing out of it. The flash functions are copied here, then executed through the function pointer flashfunc which is set to point to FlashFunc.*/long FlashFunc[FLASHFUNCSIZE];int (*flashfunc)();/* FlashProtectWindow: Must be set to allow any flash operation to be done on space assumed to be boot code.*/int FlashProtectWindow;/* FlashBank[]: This structure contains all of the information that must be made available to the various flash operation commands. It is initialized by flashtype() and used thereafter by the other operations.*/struct flashinfo FlashBank[FLASHBANKS];/* FlashNamId[]: Used to correlate between the ID and a string representing the name of the flash device. */struct flashdesc FlashNamId[] = { AMD29LV160B, "AMD-29LV160B", AMD29LV160T, "AMD-29LV160T",};/* SectorSizes160B: There are a total of 35 sectors for this part. This table reflects the size of each sector based on the fact that this configuration is for a x32 configuration (2 devices are used, each in x16 mode). */int SectorSizes160B[] = { 0x8000, 0x4000, 0x4000, 0x10000, 0x20000, 0x20000, 0x20000, 0x20000, 0x20000, 0x20000, 0x20000, 0x20000, 0x20000, 0x20000, 0x20000, 0x20000, 0x20000, 0x20000, 0x20000, 0x20000, 0x20000, 0x20000, 0x20000, 0x20000, 0x20000, 0x20000, 0x20000, 0x20000, 0x20000, 0x20000, 0x20000, 0x20000, 0x20000, 0x20000, 0x20000,};showflashtype(id)ulong id;{ int i; for(i=0;i<sizeof FlashNamId/sizeof(struct flashdesc);i++) { if (id == FlashNamId[i].id) { printf("Device = %s\n",FlashNamId[i].desc); return(0); } } printf("Flash id 0x%04x not recognized\n",id); return(-1);}showflashinfo(fdev)struct flashinfo *fdev;{ int i; if (showflashtype(fdev->id) < 0) return(-1); printf(" Base addr : 0x%08x\n",fdev->base); printf(" Sectors : %d\n",fdev->sectorcnt); printf(" Bank width : %d bytes\n",fdev->width); printf(" Sector Begin End Size SW-Protected?\n"); for(i=0;i<fdev->sectorcnt;i++) { printf(" %2d 0x%08x 0x%08x 0x%06x %s\n", i,fdev->sectors[i].begin,fdev->sectors[i].end, fdev->sectors[i].size, fdev->sectors[i].protected ? "yes" : " no"); } return(0);}/* flashopload(): Used by all flash operations to load the appropriate flash function into ram. It first verifies that the request is not larger than the allocated area (FlashFunc[]), then it copies, and verifies the copy. Finally, caches are flushed. Note that this function is copying code into data space with the intent that the code will be executed shortly. This means that after the code is copied to data space the data cache MUST be flushed (to insure that the code is in physical memory) and the instruction cache MUST be invalidated (to insure that there is no instruction cached from a previous run of this function that may have copied different code to the data space). The generic calls to preFlashopload() and postFlashopload() are used to keep this function generic.*/flashopload(begin,end)ulong *begin, *end;{ extern ulong preFlashopload(), postFlashopload(); volatile ulong *bp, *copy; int ret, mode; /* Verify space availability: */ if (((int)end - (int)begin) >= sizeof FlashFunc) { printf("flashopload overflow ((0x%x-0x%x) > 0x%x)\n", end,begin,sizeof FlashFunc); return(-1); } ret = 0; mode = preFlashopload((char *)FlashFunc, (char *)(&FlashFunc[FLASHFUNCSIZE-1])); /* Copy function() to RAM, then verify: */ copy = (ulong *)FlashFunc; bp = begin; while(bp <= end) { *copy = *bp; if (*copy++ != *bp++) { printf("flashopload failed\n"); ret = -1; break; } } postFlashopload(mode,(char *)FlashFunc, (char *)(&FlashFunc[FLASHFUNCSIZE-1])); flashfunc = (int(*)())FlashFunc; return(ret);}/* flashtype(): Copy the Flashtype() function to the array FlashFunc[], then use the function pointer flashfunc to call the routine relocated to RAM space.*/flashtype(fdev)struct flashinfo *fdev;{ int ret; if (flashopload((ulong *)FLASHTYPE,(ulong *)ENDFLASHTYPE) < 0) return(-1); ret = flashfunc(fdev); return(ret);}/* flasherase(): Copy the Flasherase() function to the array FlashFunc[], then use the function pointer flashfunc to call the routine relocated to RAM space.*/flasherase(fdev,snum)struct flashinfo *fdev;int snum;{ if (flashopload((ulong *)FLASHERASE,(ulong *)ENDFLASHERASE) < 0) return(-1); return(flashfunc(fdev,snum));}/* flashwrite(): Copy the Flashwrite() function to the array FlashFunc[], then use the function pointer flashfunc to call the routine relocated to RAM space. First make a few checks on the request, then write to flash if all checks succeed.*/flashwrite(fdev,dest,src,bytecnt)struct flashinfo *fdev;uchar *src, *dest;long bytecnt;{ int i, j, lowsector, highsector; register uchar *dp, *sp, *edp; dp = dest; sp = src; edp = (dest + bytecnt) - 1; /* If outside the devices space, return failed.. */ if ((edp < fdev->sectors[0].begin) || (dp > fdev->sectors[fdev->sectorcnt-1].end)) { printf("flashwrite() failed: dest out of flash range\n"); return(-1); } /* Make sure the destination is not within a protected sector */ if (FlashProtectWindow == FLASH_PROTECT_WINDOW_CLOSED) { /* First determine the sectors that overlap with the */ /* flash space to be written... */ lowsector = highsector = -1; for(j=0;j<fdev->sectorcnt;j++) { if ((dp >= fdev->sectors[j].begin) && (dp <= fdev->sectors[j].end)) lowsector = j; } for(j=0;j<fdev->sectorcnt;j++) { if ((edp >= fdev->sectors[j].begin) && (edp <= fdev->sectors[j].end)) highsector = j; } if ((lowsector == -1) || (highsector == -1)) { printf("flashwrite() failed: can't find sector\n"); return(-1); } /* Now that the range of affected sectors is known, */ /* verify that those sectors are not protected... */ for(j=lowsector;j<=highsector;j++) { if (fdev->sectors[j].protected) { printf("flashwrite() failed: sector protected\n"); return(-1); } } } /* Now make sure that there is no attempt to transition a bit */ /* in the affected range from 0 to 1... A flash write can only */ /* bring bits low (erase brings them high). */ while(dp < edp) { if ((*dp & *sp) != *sp) { printf("flashwrite() failed: bit 0->1 rqst denied.\n"); return(-1); } dp++; sp++; } if (flashopload((ulong *)FLASHWRITE,(ulong *)ENDFLASHWRITE) < 0) { printf("flashopload() failed\n"); return(-1); } return(flashfunc(fdev,dest,src,bytecnt));}/* flashewrite(): Copy the Flashewrite() function to the array FlashFunc[], then use the function pointer flashfunc to call the routine relocated to RAM space.*/flashewrite(fdev,dest,src,bytecnt)struct flashinfo *fdev;ulong *src, *dest;long bytecnt;{ int i;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -