flashDrvLib.c
资源名称:mpc8548.rar [点击查看]
上传用户:dqzhongke1
上传日期:2022-06-26
资源大小:667k
文件大小:15k
源码类别:
VxWorks
开发平台:
C/C++
- /*
- * $Id: flashDrvLib.c,v 1.4.2.1 Broadcom SDK $
- * $Copyright: Copyright 2008 Broadcom Corporation.
- * This program is the proprietary software of Broadcom Corporation
- * and/or its licensors, and may only be used, duplicated, modified
- * or distributed pursuant to the terms and conditions of a separate,
- * written license agreement executed between you and Broadcom
- * (an "Authorized License"). Except as set forth in an Authorized
- * License, Broadcom grants no license (express or implied), right
- * to use, or waiver of any kind with respect to the Software, and
- * Broadcom expressly reserves all rights in and to the Software
- * and all intellectual property rights therein. IF YOU HAVE
- * NO AUTHORIZED LICENSE, THEN YOU HAVE NO RIGHT TO USE THIS SOFTWARE
- * IN ANY WAY, AND SHOULD IMMEDIATELY NOTIFY BROADCOM AND DISCONTINUE
- * ALL USE OF THE SOFTWARE.
- *
- * Except as expressly set forth in the Authorized License,
- *
- * 1. This program, including its structure, sequence and organization,
- * constitutes the valuable trade secrets of Broadcom, and you shall use
- * all reasonable efforts to protect the confidentiality thereof,
- * and to use this information only in connection with your use of
- * Broadcom integrated circuit products.
- *
- * 2. TO THE MAXIMUM EXTENT PERMITTED BY LAW, THE SOFTWARE IS
- * PROVIDED "AS IS" AND WITH ALL FAULTS AND BROADCOM MAKES NO PROMISES,
- * REPRESENTATIONS OR WARRANTIES, EITHER EXPRESS, IMPLIED, STATUTORY,
- * OR OTHERWISE, WITH RESPECT TO THE SOFTWARE. BROADCOM SPECIFICALLY
- * DISCLAIMS ANY AND ALL IMPLIED WARRANTIES OF TITLE, MERCHANTABILITY,
- * NONINFRINGEMENT, FITNESS FOR A PARTICULAR PURPOSE, LACK OF VIRUSES,
- * ACCURACY OR COMPLETENESS, QUIET ENJOYMENT, QUIET POSSESSION OR
- * CORRESPONDENCE TO DESCRIPTION. YOU ASSUME THE ENTIRE RISK ARISING
- * OUT OF USE OR PERFORMANCE OF THE SOFTWARE.
- *
- * 3. TO THE MAXIMUM EXTENT PERMITTED BY LAW, IN NO EVENT SHALL
- * BROADCOM OR ITS LICENSORS BE LIABLE FOR (i) CONSEQUENTIAL,
- * INCIDENTAL, SPECIAL, INDIRECT, OR EXEMPLARY DAMAGES WHATSOEVER
- * ARISING OUT OF OR IN ANY WAY RELATING TO YOUR USE OF OR INABILITY
- * TO USE THE SOFTWARE EVEN IF BROADCOM HAS BEEN ADVISED OF THE
- * POSSIBILITY OF SUCH DAMAGES; OR (ii) ANY AMOUNT IN EXCESS OF
- * THE AMOUNT ACTUALLY PAID FOR THE SOFTWARE ITSELF OR U.S. $1,
- * WHICHEVER IS GREATER. THESE LIMITATIONS SHALL APPLY NOTWITHSTANDING
- * ANY FAILURE OF ESSENTIAL PURPOSE OF ANY LIMITED REMEDY.$
- *
- * File: flashDrvLib.c
- */
- #include "vxWorks.h"
- #include "taskLib.h"
- #include "stdlib.h"
- #include "stdio.h"
- #include "string.h"
- #include "ctype.h"
- #include "config.h"
- #include "flashDrvLib.h"
- #define TOTAL_LOADED_SECS 8
- int flashVerbose = 0; /* DEBUG */
- unsigned int flashBaseAddress = 0xf8000000;
- int flashSize = 0x8000000;
- int flashDevSectorSize = 0x20000;
- int flashSectorCount = 0;
- #define FLUSH_HISTORY_BUF_LEN 16
- static int flashSectorFlushHistory[FLUSH_HISTORY_BUF_LEN];
- LOCAL struct flash_drv_funcs_s *flashDrvFuncs = &flash29GL1G;
- LOCAL struct flashLoadedSectorInfo {
- SEM_ID fsSemID;
- int sector;
- int dirty;
- char *buffer;
- } flashLoadedSectors[TOTAL_LOADED_SECS];
- #define FS_CACHE_LOCK(_x_)
- semTake(flashLoadedSectors[(_x_)].fsSemID, WAIT_FOREVER)
- #define FS_CACHE_UNLOCK(_x_)
- semGive(flashLoadedSectors[(_x_)].fsSemID)
- LOCAL int
- flashFlushLoadedSector(int number, int reason)
- {
- if (flashLoadedSectors[number].sector < 0 ||
- !flashLoadedSectors[number].dirty) {
- if (flashVerbose)
- printf("flashFlushLoadedSector(%d): not dirtyn", number);
- return (OK);
- }
- if (flashVerbose) {
- printf("flashFlushLoadedSector(%d): Flushing sector %d %sn", number,
- flashLoadedSectors[number].sector,
- (reason == 0 ? "<- flashSync" : "<- flashBlkWrite"));
- }
- if (flashDrvFuncs->flashEraseSector(flashLoadedSectors[number].sector)==ERROR) {
- return (ERROR);
- }
- if (flashDrvFuncs->flashWrite(flashLoadedSectors[number].sector,
- flashLoadedSectors[number].buffer, 0, FLASH_SECTOR_SIZE) == ERROR) {
- return (ERROR);
- }
- if (flashVerbose) {
- printf(" Flushing %d donen",
- flashLoadedSectors[number].sector);
- }
- flashLoadedSectors[number].sector = -1;
- flashLoadedSectors[number].dirty = 0;
- return (OK);
- }
- LOCAL int
- getSectorFetchHistory(int sectorNum)
- {
- int idx = 0, cnt = 0;
- while(idx < FLUSH_HISTORY_BUF_LEN) {
- cnt += (flashSectorFlushHistory[idx] == sectorNum) ? 1 : 0;
- idx ++;
- }
- return cnt;
- }
- LOCAL int
- allocEmptyLoadedSector(int sectorNum)
- {
- int number = 0, ii, min_read = FLUSH_HISTORY_BUF_LEN, tmp;
- static int ring_index = 0;
- for (ii = 0; ii < TOTAL_LOADED_SECS; ii++) {
- if (flashLoadedSectors[ii].sector < 0) {
- flashLoadedSectors[ii].sector = sectorNum;
- flashLoadedSectors[ii].dirty = 0;
- flashSectorFlushHistory[ring_index] = sectorNum;
- ring_index = (ring_index + 1) % FLUSH_HISTORY_BUF_LEN;
- return ii;
- }
- }
- for (ii = 0; ii < TOTAL_LOADED_SECS; ii++) {
- if ((tmp = getSectorFetchHistory(flashLoadedSectors[ii].sector)) <
- min_read) {
- number = ii;
- }
- }
- if (flashVerbose)
- printf("allocEmptyLoadedSector(%d): erasing %d, new sector %dn",
- number, flashLoadedSectors[number].sector, sectorNum);
- FS_CACHE_LOCK(number);
- if (flashFlushLoadedSector(number, 1) == ERROR) {
- FS_CACHE_UNLOCK(number);
- return (-1);
- }
- flashLoadedSectors[number].sector = sectorNum;
- flashLoadedSectors[number].dirty = 0;
- flashSectorFlushHistory[ring_index] = sectorNum;
- ring_index = (ring_index + 1) % FLUSH_HISTORY_BUF_LEN;
- FS_CACHE_UNLOCK(number);
- return (number);
- }
- int
- flashDrvLibInit(void)
- {
- FLASH_TYPES dev;
- FLASH_VENDORS vendor;
- int i;
- flashDrvFuncs = &flash29GL1G;
- flashDrvFuncs->flashAutoSelect(&dev, &vendor);
- if ((vendor == 0xFF) && (dev == 0xFF)) {
- printf("flashInit(): No flash Foundn");
- return (ERROR);
- }
- switch (vendor) {
- case AMD:
- switch (dev & 0x0000ffff) {
- case FLASH_29GL1G:
- flashSectorCount = 1024;
- flashDevSectorSize = 0x20000;
- if (flashVerbose)
- printf("flashInit(): 29GL1G Foundn");
- break;
- default:
- printf("flashInit(): Unrecognized AMD Device (0x%02X)n", dev);
- return (ERROR);
- }
- break;
- default:
- printf("flashInit(): Unrecognized Vendor (0x%02X)n", vendor);
- return (ERROR);
- }
- flashSize = flashDevSectorSize * flashSectorCount;
- for (i = 0; i < TOTAL_LOADED_SECS; i++) {
- flashLoadedSectors[i].buffer = malloc(FLASH_SECTOR_SIZE);
- if (flashLoadedSectors[i].buffer == NULL) {
- printf("flashInit(): malloc() failedn");
- for (; i > 0; i--) {
- free(flashLoadedSectors[i-1].buffer);
- }
- return (ERROR);
- }
- flashLoadedSectors[i].sector = -1;
- flashLoadedSectors[i].dirty = 0;
- flashLoadedSectors[i].fsSemID =
- semMCreate (SEM_Q_PRIORITY | SEM_DELETE_SAFE);
- }
- memset(flashSectorFlushHistory, 0, sizeof(int) * FLUSH_HISTORY_BUF_LEN);
- return (OK);
- }
- int
- flashGetSectorSize(void)
- {
- return (flashDevSectorSize);
- }
- int
- flashGetSectorCount(void)
- {
- return (flashSectorCount);
- }
- int
- flashEraseBank(int firstSector, int nSectors)
- {
- int sectorNum, errCnt = 0;
- if (firstSector < 0 || firstSector + nSectors > flashSectorCount) {
- printf("flashEraseBank(): Illegal parms %d, %dn",
- firstSector, nSectors);
- return ERROR;
- }
- for (sectorNum = firstSector;
- sectorNum < firstSector + nSectors; sectorNum++) {
- printf(".");
- if (flashDrvFuncs->flashEraseSector(sectorNum))
- errCnt++;
- }
- printf("n");
- if (errCnt)
- return (ERROR);
- else
- return (OK);
- }
- int
- flashBlkRead(int sectorNum, char *buff,
- unsigned int offset, unsigned int count)
- {
- int i;
- if (sectorNum < 0 || sectorNum >= flashSectorCount) {
- printf("flashBlkRead(): Sector %d invalidn", sectorNum);
- return (ERROR);
- }
- if (offset < 0 || offset >= FLASH_SECTOR_SIZE) {
- printf("flashBlkRead(): Offset 0x%x invalidn", offset);
- return (ERROR);
- }
- if (count < 0 || count > FLASH_SECTOR_SIZE - offset) {
- printf("flashBlkRead(): Count 0x%x invalidn", count);
- return (ERROR);
- }
- /*
- * If the sector is loaded, read from it. Else, read from the
- * flash itself (slower).
- */
- for (i = 0; i < TOTAL_LOADED_SECS; i++) {
- if (flashLoadedSectors[i].sector == sectorNum) {
- if (flashVerbose)
- printf("flashBlkRead(): from loaded sector %dn", sectorNum);
- bcopy(&flashLoadedSectors[i].buffer[offset], buff, count);
- return (OK);
- }
- }
- flashDrvFuncs->flashRead(sectorNum, buff, offset, count);
- return (OK);
- }
- /*
- * 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);
- }
- int
- flashBlkWrite(int sectorNum, char *buff,
- unsigned int offset, unsigned int count)
- {
- int i;
- if (sectorNum < 0 || sectorNum >= flashSectorCount) {
- printf("flashBlkWrite(): Sector %d invalidn", sectorNum);
- return (ERROR);
- }
- if (offset < 0 || offset >= FLASH_SECTOR_SIZE) {
- printf("flashBlkWrite(): Offset 0x%x invalidn", offset);
- return (ERROR);
- }
- /*
- * Count must be within range and must be a long word multiple, as
- * we always program long words.
- */
- if ((count < 0) ||
- (count > ((flashSectorCount - sectorNum) * FLASH_SECTOR_SIZE - offset))) {
- printf("flashBlkWrite(): Count 0x%x invalidn", count);
- return (ERROR);
- }
- /*
- * If the Sector is loaded, write it to buffer. Else check to see
- * if we can program the sector; if so, program it. Else, flush the
- * first loaded sector (if loaded and dirty), push loaded sectors
- * up by one, load the new one and copy the data into the last one.
- */
- for (i = 0; i < TOTAL_LOADED_SECS; i++) {
- FS_CACHE_LOCK(i);
- if (flashLoadedSectors[i].sector == sectorNum) {
- if (flashVerbose)
- printf("%d ", sectorNum);
- bcopy(buff, &flashLoadedSectors[i].buffer[offset], count);
- flashLoadedSectors[i].dirty = 1;
- FS_CACHE_UNLOCK(i);
- return (OK);
- }
- FS_CACHE_UNLOCK(i);
- }
- if (flashCheckCanProgram(sectorNum, offset, count) != ERROR) {
- return (flashDrvFuncs->flashWrite(sectorNum, buff, offset, count));
- }
- /* Find empty sector */
- if ((i = allocEmptyLoadedSector(sectorNum)) < 0) {
- return (ERROR);
- }
- FS_CACHE_LOCK(i);
- if (flashDrvFuncs->flashRead(sectorNum, flashLoadedSectors[i].buffer,
- 0, FLASH_SECTOR_SIZE) == ERROR) {
- flashLoadedSectors[i].sector = -1;
- FS_CACHE_UNLOCK(i);
- return (ERROR);
- }
- bcopy(buff, &flashLoadedSectors[i].buffer[offset], count);
- flashLoadedSectors[i].dirty = 1;
- FS_CACHE_UNLOCK(i);
- if (flashVerbose) {
- printf("flashBlkWrite(): load %d sect %d (and write to cache only)n",
- i, sectorNum);
- }
- return (OK);
- }
- int
- flashDiagnostic(void)
- {
- unsigned int *flashSectorBuff;
- int sectorNum, i;
- /*
- * Probe flash; allocate flashLoadedSector Buffer
- */
- flashDrvLibInit(); /* Probe; clear loaded sector */
- flashSectorBuff = (unsigned int *) flashLoadedSectors[0].buffer;
- if (flashVerbose) {
- printf("flashDiagnostic(): Executing. Erasing %d Sectorsn",
- flashSectorCount);
- }
- if (flashEraseBank(0, flashSectorCount) == ERROR) {
- if (flashVerbose) {
- printf("flashDiagnostic(): flashEraseBank() #1 failedn");
- }
- return (ERROR);
- }
- /* Write unique counting pattern to each sector. */
- for (sectorNum = 0; sectorNum < flashSectorCount; sectorNum++) {
- if (flashVerbose) {
- printf("flashDiagnostic(): writing sector %dn", sectorNum);
- }
- for (i = 0; i < FLASH_SECTOR_SIZE / sizeof (unsigned int); i++) {
- flashSectorBuff[i] = (i + sectorNum);
- }
- if (flashDrvFuncs->flashWrite(sectorNum, (char *)flashSectorBuff,
- 0, FLASH_SECTOR_SIZE) == ERROR) {
- if (flashVerbose) {
- printf("flashDiagnostic(): flashWrite() failed on %dn",
- sectorNum);
- }
- return (ERROR);
- }
- }
- /* Verify each sector. */
- for (sectorNum = 0; sectorNum < flashSectorCount; sectorNum++) {
- if (flashVerbose) {
- printf("flashDiagnostic(): verifying sector %dn", sectorNum);
- }
- if (flashDrvFuncs->flashRead(sectorNum, (char *)flashSectorBuff,
- 0, FLASH_SECTOR_SIZE) == ERROR) {
- if (flashVerbose) {
- printf("flashDiagnostic(): flashRead() failed on %dn",
- sectorNum);
- }
- return (ERROR);
- }
- for (i = 0; i < FLASH_SECTOR_SIZE / sizeof (unsigned int); i++) {
- if (flashSectorBuff[i] != (i + sectorNum)) {
- if (flashVerbose) {
- printf("flashDiagnostic(): verification failedn");
- printf("flashDiagnostic(): sector %d, offset 0x%xn",
- sectorNum, (i * sizeof(unsigned int)));
- printf("flashDiagnostic(): expected 0x%x, got 0x%xn",
- (i + sectorNum), (int)flashSectorBuff[i]);
- }
- return (ERROR);
- }
- }
- }
- if (flashEraseBank(0, flashSectorCount) == ERROR) {
- if (flashVerbose) {
- printf("flashDiagnostic(): flashEraseBank() #2 failedn");
- return (ERROR);
- }
- }
- if (flashVerbose) {
- printf("flashDiagnostic(): Completed without errorn");
- }
- return (OK);
- }
- int
- flashSyncFilesystem(void)
- {
- int i;
- for (i = 0; i < TOTAL_LOADED_SECS; i++) {
- FS_CACHE_LOCK(i);
- if (flashFlushLoadedSector(i, 0) != OK) {
- FS_CACHE_UNLOCK(i);
- return (ERROR);
- }
- FS_CACHE_UNLOCK(i);
- }
- return (OK);
- }