/******************************************************************************* * Function Name: CySpcCreateCmdEraseSector ******************************************************************************** * Summary: * Sets up the command to EraseSector. * * * Parameters: * array: * Id of the array. * * * address: * flash/eeprom addrress * * * Return: * CYRET_SUCCESS if the command was created sucessfuly. * CYRET_BAD_PARAM if an invalid parameter was passed. * * * Theory: * * *******************************************************************************/ cystatus CySpcCreateCmdEraseSector(uint8 array, uint16 address) { cystatus status; /* Create packet command. */ cyCommand[0] = SPC_KEY_ONE; cyCommand[1] = SPC_KEY_TWO(SPC_CMD_ER_SECTOR); cyCommand[2] = SPC_CMD_ER_SECTOR; /* Create packet parameters. */ cyCommand[3] = array; if(array < LAST_FLASH_ARRAYID) { cyCommand[4] = FLASH_SECTOR_ADDRESS(address); } else { cyCommand[4] = EEPRM_SECTOR_ADDRESS(address); } cyCommandSize = SIZEOF_CMD_ER_SECTOR; status = CYRET_SUCCESS; return status; }
LOCAL int flashRead(int sectorNum, char *buff, unsigned int offset, unsigned int count) { if (sectorNum < 0 || sectorNum >= flashSectorCount) { printf("flashRead(): Illegal sector %d\n", sectorNum); return (ERROR); } bcopy((char *)(FLASH_SECTOR_ADDRESS(sectorNum) + offset), buff, count); return (0); }
/* * Check if we can program this part of the flash. All * the data has to be all "1" to be programmed. Because * the flash has to be init to all 1's and change from 1 to 0 */ LOCAL int flashCheckCanProgram(int sectorNum, unsigned int offset, unsigned int count) { unsigned char *flashBuffPtr; int i; flashBuffPtr = (unsigned char *)(FLASH_SECTOR_ADDRESS(sectorNum) + offset); for (i = 0; i < count; i++) { if (flashBuffPtr[i] != 0xff) return (ERROR); } return (OK); }
LOCAL int flashWrite(int sectorNum, char *buff, unsigned int offset, unsigned int count) { UINT8 *curBuffPtr, *flashBuffPtr; int i; curBuffPtr = (UINT8 *)buff; flashBuffPtr = (UINT8 *)(FLASH_SECTOR_ADDRESS(sectorNum) + offset); for (i = 0; i < count; i++) { if (((offset + i) % INTEL_FLASH_WBSIZE) == 0) { break; } if (flashProgramDevices(flashBuffPtr, *curBuffPtr) == ERROR) { printf("flashWrite(): Failed: Sector %d, address 0x%x\n", sectorNum, (int)flashBuffPtr); return (ERROR); } flashBuffPtr++; curBuffPtr++; } for (; i < count; i += INTEL_FLASH_WBSIZE) { if ((i + INTEL_FLASH_WBSIZE) >= count) { break; } if (flashProgramBlock(flashBuffPtr, curBuffPtr) != OK) { printf("flashWrite(): Failed: Sector %d, address 0x%x\n", sectorNum, (int)flashBuffPtr); return (ERROR); } flashBuffPtr += INTEL_FLASH_WBSIZE; curBuffPtr += INTEL_FLASH_WBSIZE; } for (; i < count; i++) { if (flashProgramDevices(flashBuffPtr, *curBuffPtr) == ERROR) { printf("flashWrite(): Failed: Sector %d, address 0x%x\n", sectorNum, (int)flashBuffPtr); return (ERROR); } flashBuffPtr++; curBuffPtr++; } return (0); }
LOCAL int flashEraseSector(int sectorNum) { unsigned char *sectorBasePtr = (unsigned char *)FLASH_SECTOR_ADDRESS(sectorNum); if (sectorNum < 0 || sectorNum >= flashSectorCount) { printf("flashEraseSector(): Sector %d invalid\n", sectorNum); return (ERROR); } if (flashEraseDevices(sectorBasePtr) == ERROR) { printf("flashEraseSector(): erase devices failed sector=%d\n", sectorNum); return (ERROR); } /* Boot Sectored devices need multiple erases in sector 0 Sector 0 is * broken up into 16k, 8k, 8k, 32k sub sectors. Each one must have an * erase issued in that sub sector to erase all of logical sector 0. */ if (sectorNum == 0) { if (flashEraseDevices(sectorBasePtr + 0x4000) == ERROR) { printf("flashEraseSector(): erase devices failed sector=0.1\n"); return (ERROR); } if (flashEraseDevices(sectorBasePtr + 0x6000) == ERROR) { printf("flashEraseSector(): erase devices failed sector=0.2\n"); return (ERROR); } if (flashEraseDevices(sectorBasePtr + 0x8000) == ERROR) { printf("flashEraseSector(): erase devices failed sector=0.3\n"); return (ERROR); } } if (flashVerbose) printf("flashEraseSector(): Sector %d erased\n", sectorNum); return (OK); }
LOCAL int flashEraseSector(int sectorNum) { UINT8 *sectorBasePtr = (UINT8 *)FLASH_SECTOR_ADDRESS(sectorNum); if (sectorNum < 0 || sectorNum >= flashSectorCount) { printf("flashEraseSector(): Sector %d invalid\n", sectorNum); return (ERROR); } if (flashEraseDevices(sectorBasePtr) == ERROR) { printf("flashEraseSector(): erase devices failed sector=%d\n", sectorNum); return (ERROR); } if (flashVerbose) printf("flashEraseSector(): Sector %d erased\n", sectorNum); return (OK); }
LOCAL int flashWrite(int sectorNum, char *buff, unsigned int offset, unsigned int count) { unsigned char *curBuffPtr, *flashBuffPtr; int i; curBuffPtr = (unsigned char *)buff; flashBuffPtr = (unsigned char *)(FLASH_SECTOR_ADDRESS(sectorNum) + offset); for (i = 0; i < count; i++) { if (flashProgramDevices(flashBuffPtr, *curBuffPtr) == ERROR) { printf("flashWrite(): Failed: Sector %d, address 0x%x\n", sectorNum, (int)flashBuffPtr); return (ERROR); } flashBuffPtr++; curBuffPtr++; } return (0); }
flashProgramDevices(volatile UINT16 *addr, UINT16 val) #endif { int polls; unsigned int tmp; FLASH_WRITE(FLASH_BASE_ADDRESS, 0xaaa, 0xaa); FLASH_WRITE(FLASH_BASE_ADDRESS, 0x555, 0x55); FLASH_WRITE(FLASH_BASE_ADDRESS, 0xaaa, 0xa0); /*FLASH_WRITE(addr, 0x0, val);*/ *addr = val; for (polls = 0; polls < FLASH_PROGRAM_TIMEOUT_POLLS; polls++) { tmp = *addr; #if IS_8_BIT if ((tmp & 0x80) == (val & 0x80)) { #else if ((tmp & 0x8080) == (val & 0x8080)) { #endif if (flashVerbose > 2) printf("flashProgramDevices(): devices programmed\n"); return (OK); } } #if IS_8_BIT if ((tmp & 0x2020) != 0) { #else if ((tmp & 0x20) != 0) { #endif /* * We've already waited so long that chances are nil that the * 0x80 bits will change again. Don't bother re-checking them. */ printf("flashProgramDevices(): Address 0x%08x program failed\n", (int)addr); } else { printf("flashProgramDevices(): timed out\n"); } flashReadReset(); return (ERROR); } LOCAL int flashWrite(int sectorNum, char *buff, unsigned int offset, unsigned int count) { #if IS_8_BIT unsigned char *curBuffPtr, *flashBuffPtr; #else UINT16 *curBuffPtr, *flashBuffPtr; #endif int i; #if IS_8_BIT curBuffPtr = (unsigned char *)buff; flashBuffPtr = (unsigned char *)(FLASH_SECTOR_ADDRESS(sectorNum) + offset); #else curBuffPtr = (UINT16 *)buff; flashBuffPtr = (UINT16 *)(FLASH_SECTOR_ADDRESS(sectorNum) + offset); #endif #if IS_8_BIT for (i = 0; i < count; i++) { #else for (i = 0; i < (count+1)/2; i++) { #endif if (flashProgramDevices(flashBuffPtr, *curBuffPtr) == ERROR) { printf("flashWrite(): Failed: Sector %d, address 0x%x\n", sectorNum, (int)flashBuffPtr); return (ERROR); } flashBuffPtr++; curBuffPtr++; } return (0); } struct flash_drv_funcs_s flash29l320 = { FLASH_2L320, AMD, flashAutoSelect, flashEraseSector, flashRead, flashWrite };