os/kernelhwsrv/kerneltest/e32test/misc/strataflash.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     1 // Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
     2 // All rights reserved.
     3 // This component and the accompanying materials are made available
     4 // under the terms of the License "Eclipse Public License v1.0"
     5 // which accompanies this distribution, and is available
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7 //
     8 // Initial Contributors:
     9 // Nokia Corporation - initial contribution.
    10 //
    11 // Contributors:
    12 //
    13 // Description:
    14 // e32test\misc\strataflash.cpp
    15 // 
    16 //
    17 
    18 #include <e32def.h>
    19 #include <e32def_private.h>
    20 #include "flash.h"
    21 
    22 class StrataFlash : public Flash
    23 	{
    24 public:
    25 	virtual TInt Read(TUint32 anAddr, TUint32 aSize, TUint8* aDest);
    26 	virtual TInt BlankCheck(TUint32 anAddr, TUint32 aSize);
    27 	virtual TInt Erase(TUint32 anAddr, TUint32 aSize);
    28 	virtual TInt Write(TUint32 anAddr, TUint32 aSize, const TUint8* aSrc);
    29 	};
    30 
    31 
    32 Flash* Flash::New(TUint32 /*anAddr*/)
    33 	{
    34 	return new StrataFlash;
    35 	}
    36 
    37 TInt StrataFlash::Read(TUint32 anAddr, TUint32 aSize, TUint8* aDest)
    38 	{
    39 	Mem::Copy(aDest,(const TUint8*)anAddr,aSize);
    40 	return KErrNone;
    41 	}
    42 
    43 TInt StrataFlash::BlankCheck(TUint32 anAddr, TUint32 aSize)
    44 	{
    45 	const TUint8* p=(const TUint8*)anAddr;
    46 	const TUint8* pE=p+aSize;
    47 	while(p<pE)
    48 		{
    49 		if (*p++!=0xff)
    50 			return (TUint32)p-anAddr;
    51 		}
    52 	return 0;
    53 	}
    54 
    55 TInt StrataFlash::Erase(TUint32 anAddr, TUint32 aSize)
    56 	{
    57 	TUint32 base=anAddr&~0x1ffff;	// round base address down to block
    58 	TUint32 end=anAddr+aSize;
    59 	end=(end+0x1ffff)&~0x1ffff;	// round end address up to block
    60 	TUint32 size=end-base;
    61 	for (; size; size-=0x20000, base+=0x20000)
    62 		{
    63 		volatile TUint8* p=(volatile TUint8*)base;
    64 		*p=0x20;		// block erase
    65 		*p=0xd0;		// block erase confirm
    66 		TUint s=0;
    67 		while ((s&0x80)==0)
    68 			s=*p;
    69 		*p=0x50;		// clear status reg
    70 		*p=0xff;		// read mode
    71 		if (s&0x20)
    72 			{
    73 			// error
    74 			return (TUint32)p-anAddr+1;
    75 			}
    76 		}
    77 	return 0;
    78 	}
    79 
    80 TInt StrataFlash::Write(TUint32 anAddr, TUint32 aSize, const TUint8* aSrc)
    81 	{
    82 	volatile TUint8* p=(volatile TUint8*)anAddr;
    83 	const TUint8* pE=aSrc+aSize;
    84 	for (; aSrc<pE; aSrc++, p++)
    85 		{
    86 		*p=0x40;		// byte write
    87 		*p=*aSrc;		// write data
    88 		TUint s=0;
    89 		while ((s&0x80)==0)
    90 			s=*p;
    91 		*p=0x50;		// clear status reg
    92 		*p=0xff;		// read mode
    93 		if (s&0x10)
    94 			{
    95 			// error
    96 			return (TUint32)p-anAddr+1;
    97 			}
    98 		}
    99 	return 0;
   100 	}
   101 
   102