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