/******************************************************************************/
/*                                                                            */
/*  Copyright (C), 1995-2007, SanDisk IL Ltd. All rights reserved.            */
/*                                                                            */
/*  Redistribution and use in source and binary forms, with or without        */
/*  modification, are permitted provided that the following conditions are    */
/*  met:                                                                      */
/*  1. Redistributions of source code must retain the above copyright notice, */
/*     this list of conditions and the following disclaimer.                  */
/*  2. Redistributions in binary form must reproduce the above copyright      */
/*     notice, this list of conditions and the following disclaimer in the    */
/*     documentation and/or other materials provided with the distribution.   */
/*  3. Neither the name of SanDisk IL nor the names of its contributors may be*/
/*     used to endorse or promote products derived from this software without */
/*     specific prior written permission.                                     */
/*                                                                            */
/*  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       */
/*  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED */
/*  TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR             */
/*  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT      */
/*  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,     */
/*  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED  */
/*  TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR    */
/*  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF    */
/*  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING      */
/*  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS        */
/*  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.              */
/*                                                                            */
/*  NOTWITHSTANDING ANYTHING STATED TO THE CONTRARY, SANDISK'S TOTAL LIABILITY*/
/*  ARISING OUT OF OR RELATED TO THE SOFTWARE OR ANY SUPPORT SERVICES         */
/*  PROVIDED WITH RESPECT THERETO SHALL NOT EXCEED $100.                      */
/*                                                                            */
/*  The laws of the State of California, United States of America, exclusive  */
/*  of conflict-of-laws provisions, shall govern this license in all respects.*/
/*  The the federal or state courts of competent jurisdiction in the State    */ 
/*  of California shall have exclusive jurisdiction with respect to all       */
/*  actions commenced in relation to the software. Licensee agrees not        */ 
/*  to export or re-export, directly or indirectly any technical data         */ 
/*  acquired from SanDisk or any products utilizing such data in violation    */
/*  of applicable export laws and regulations.                                */
/*                                                                            */
/******************************************************************************/
/*
 * $Log:   V:/PVCSDB/DiskOnChip/archives/Testing/TrueFFS 6.3/Drop 2.5/3/bddoc/src/docsys.c-arc  $
 * 
 *    Rev 1.9   Oct 22 2007 11:34:50   einat.avidan
 * update copyrights header
 * 
 *    Rev 1.8   Mar 11 2007 16:37:30   einat.avidan
 * bug fix: working on x86 requires read buffer not to be paged out
 * replace the former solution (access to the begining and the end of the buffer) 
 * with full solution: usage of a buffer that is locked on RAM
 * 
 *    Rev 1.7   Feb 28 2007 09:53:44   einat.avidan
 * No change
 * 
 *    Rev 1.6.1.0   Nov 13 2006 15:13:50   Yaniv.Iarovici
 * Added environment variable - 'FL_DISABLE_MEMCPY'.
 * 
 *    Rev 1.6   Oct 05 2006 11:00:48   yaniv.iarovici
 * Removed support for flUse8Bit.
 * 
 *    Rev 1.5   Sep 11 2006 13:45:18   yaniv.iarovici
 * Legal header added
 * 
 *    Rev 1.4   Aug 09 2006 16:52:44   Polina.Marimont
 * initial for DOC Driver 1.0
 */

#include "docsys.h"
#ifdef FL_PARALLEL_MTD_SUPPORT
#include "pacclayr.h"
#endif /* FL_PARALLEL_MTD_SUPPORT */

FLBoolean flPreventMemcpy = 0;

#ifndef FL_NO_USE_FUNC

/*********************************************************/
/*     Report DiskOnChip Memory size                     */
/*********************************************************/

/*----------------------------------------------------------------------
   f l D o c M e m W i n S i z e N o S h i f t

   This routine is called from MTD to quary the size of the DiskOnChip
   memory window for none shifted DiskOnChip.
------------------------------------------------------------------------*/

FLDword FAR1 flDocMemWinSizeNoShift(void)
{
  return 0x2000;
}

/*----------------------------------------------------------------------
   f l D o c M e m W i n S i z e S i n g l e S h i f t

   This routine is called from MTD to quary the size of the DiskOnChip
   memory window for DiskOnChip connected with a single addres shift.
------------------------------------------------------------------------*/

FLDword FAR1 flDocMemWinSizeSingleShift(void)
{
  return 0x4000;
}

/*----------------------------------------------------------------------
   f l D o c M e m W i n S i z e D o u b l e S h i f t

   This routine is called from MTD to quary the size of the DiskOnChip
   memory window for DiskOnChip connected with a double addres shift.
------------------------------------------------------------------------*/

FLDword FAR1 flDocMemWinSizeDoubleShift(void)
{
  return 0x8000;
}

/*********************************************************/
/*     Write 16 bits to DiskOnChip memory window         */
/*********************************************************/


/*----------------------------------------------------------------------
   f l W r i t e 16 b i t U s i n g 8 b i t s N o S h i f t
                                                                       
   Write 16-bits Using 8-bits operands and 8 bit data 
   With no address shifted.       
------------------------------------------------------------------------*/

void FAR1 flWrite16bitUsing8bitsNoShift(volatile FLByte FAR0 * win, FLWord offset,FLWord val)
{
#ifndef FL_BIG_ENDIAN
   FLWRITE_IO_BYTE((FLByte)val,(win + offset));
   FLWRITE_IO_BYTE((FLByte)(val>>0x8),(win + offset+1));
#else
   FLWRITE_IO_BYTE((FLByte)(val>>0x8),(win + offset));
   FLWRITE_IO_BYTE((FLByte)val,(win + offset + 1));
#endif /* FL_BIG_ENDIAN */
}

/*----------------------------------------------------------------------
   f l W r i t e 1 6 b i t U s i n g 1 6 b i t s N o S h i f t

   Note : offset must be 16-bits aligned.

   Note2 : Used for if_cfg = 16 when using address shift.

   Write 16-bit Using 16-bits operands and 16-bits of data 
   with no address shifted.
------------------------------------------------------------------------*/

void FAR1 flWrite16bitUsing16bitsNoShift(volatile  FLByte FAR0 * win, FLWord offset,Reg16bitType val)
{
   FLWRITE_IO_WORD(val,((volatile FLWord FAR0 *)win + (offset>>1)));
}

/*----------------------------------------------------------------------
   f l W r i t e 1 6 b i t U s i n g 3 2 b i t s S i n g l e S h i f t

   Note : offset must be 16-bits aligned.

   Write 16-bit Using 32-bits operands and 16-bits of data
   With a single address shifted.
------------------------------------------------------------------------*/

void FAR1 flWrite16bitUsing32bitsSingleShift(volatile  FLByte FAR0 * win, FLWord offset,Reg16bitType val)
{
#ifdef FL_BIG_ENDIAN
   FLWRITE_IO_DWORD((FLDword)(((FLDword)val)<<16),(((volatile FLDword FAR0 *)win)+(offset>>1)));
#else
   FLWRITE_IO_DWORD((FLDword)val,(((volatile FLDword FAR0 *)win)+(offset>>1)));
#endif /* FL_BIG_ENDIAN */
}

/*----------------------------------------------------------------------
   f l W r i t e 1 6 b i t U s i n g 1 6 b i t s S i n g l e S h i f t

   Note : offset must be 16-bits aligned.

   Note2 : Used for if_cfg = 8 when using address shift.

   Write 16-bit Using 16-bits operands and 8-bits of data
   with a single address shifted.
------------------------------------------------------------------------*/

void FAR1 flWrite16bitUsing16bitsSingleShift(volatile  FLByte FAR0 * win, FLWord offset,Reg16bitType val)
{
#ifdef FL_BIG_ENDIAN
   FLWRITE_IO_WORD((FLWord)(val>>8),(((volatile FLWord FAR0 *)win) + offset));
   FLWRITE_IO_WORD((FLWord)(val),(((volatile FLWord FAR0 *)win) + (offset+1)));
#else
   FLWRITE_IO_WORD(val,(((volatile FLWord FAR0 *)win) + offset));
   FLWRITE_IO_WORD((FLWord)(val>>8),(((volatile FLWord FAR0 *)win) + (offset + 1)));
#endif /* FL_BIG_ENDIAN */
}

/*----------------------------------------------------------------------
   f l W r i t e 1 6 b i t U s i n g 3 2 b i t s D o u b l e S h i f t

   Note : offset must be 16-bits aligned.

   Note2 : Used for if_cfg = 8 when using address shift.

   Write 16-bit Using 32-bits operands and 8-bits of data
   with a single address shifted.
------------------------------------------------------------------------*/

void FAR1 flWrite16bitUsing32bitsDoubleShift(volatile  FLByte FAR0 * win, FLWord offset,Reg16bitType val)
{
#ifdef FL_BIG_ENDIAN
   FLWRITE_IO_DWORD(((FLDword)(val>>8)),(((volatile FLDword FAR0 *)win) + offset));
   FLWRITE_IO_DWORD(((FLDword)((FLByte)val)),(((volatile FLDword FAR0 *)win) + (offset + 1)));
#else
   FLWRITE_IO_DWORD(((FLDword)((FLByte)val)),(((volatile FLDword FAR0 *)win) + offset));
   FLWRITE_IO_DWORD(((FLDword)(val>>8)),(((volatile FLDword FAR0 *)win) + (offset + 1)));
#endif /* FL_BIG_ENDIAN */
}

/*********************************************************/
/*     Read 16 bits from DiskOnChip memory window        */
/*********************************************************/


/*----------------------------------------------------------------------
   f l R e a d 16 b i t U s i n g 8 b i t s N o S h i f t
                                                                       
   Read 16-bits Using 8-bits operands and 8-bits of data
   With no address shifted.       
------------------------------------------------------------------------*/

FLWord FAR1 flRead16bitUsing8bitsNoShift(volatile  FLByte FAR0 * win,FLWord offset)
{
#ifndef FL_BIG_ENDIAN
   register FLWord val = (FLWord)FLREAD_IO_BYTE((win+offset));
   return val | (((FLWord)FLREAD_IO_BYTE((win+offset+1)))<<8);
#else
   register FLWord val = ((FLWord)FLREAD_IO_BYTE(win+offset))<<8;
   return val | ((FLWord)FLREAD_IO_BYTE(win+offset+1));
#endif /* FL_BIG_ENDIAN */
}

/*----------------------------------------------------------------------
   f l R e a d 1 6 b i t U s i n g 1 6 b i t s N o S h i f t

   Note : offset must be 16-bits aligned.

   Read 16-bit Using 16-bits operands and 16-bits of data
   With no address shifted.
------------------------------------------------------------------------*/

Reg16bitType FAR1 flRead16bitUsing16bitsNoShift(volatile  FLByte FAR0 * win,FLWord offset)
{
   return( FLREAD_IO_WORD(((volatile FLWord FAR0 *)win + (offset>>1))) );
}

/*----------------------------------------------------------------------
   f l R e a d 1 6 b i t U s i n g 3 2 b i t s S i n g l e S h i f t

   Note : offset must be 16-bits aligned.

   Note2 : Used for if_cfg = 16 when using address shift.

   Read 16-bit Using 32-bits operands and 16-bits of data 
   With single address shifted.
------------------------------------------------------------------------*/

Reg16bitType FAR1 flRead16bitUsing32bitsSingleShift(volatile  FLByte FAR0 * win,FLWord offset)
{
#ifdef FL_BIG_ENDIAN
   return (Reg16bitType) (FLREAD_IO_DWORD(((volatile FLDword FAR0*)win)+(offset>>1))>>16);
#else
   return((Reg16bitType)FLREAD_IO_DWORD(((volatile FLDword FAR0 *)win)+(offset>>1)));
#endif /* FL_BIG_ENDIAN */
}

/*----------------------------------------------------------------------
   f l R e a d 1 6 b i t U s i n g 1 6 b i t s S i n g l e S h i f t

   Note : offset must be 16-bits aligned.

   Note2 : Used for if_cfg = 8 when using address shift.

   Read 16-bit Using 16-bits operands and 8-bits of data 
   With single address shifted.
------------------------------------------------------------------------*/

Reg16bitType FAR1 flRead16bitUsing16bitsSingleShift(volatile  FLByte FAR0 * win,FLWord offset)
{
	FLByte val[2];

	val[0] = ((Reg8bitType)(FLREAD_IO_WORD(((volatile FLWord FAR0 *)win)+offset)));
    val[1] = ((Reg8bitType)(FLREAD_IO_WORD(((volatile FLWord FAR0 *)win)+(offset+1))));

	return *((Reg16bitType *)val);
}

/*----------------------------------------------------------------------
   f l R e a d 1 6 b i t U s i n g 3 2 b i t s D o u b l e S h i f t

   Note : offset must be 16-bits aligned.

   Note2 : Used for if_cfg = 8 when using address shift.

   Read 16-bit Using 32-bits operands and 8-bits of data 
   With double address shifted.
------------------------------------------------------------------------*/

Reg16bitType FAR1 flRead16bitUsing32bitsDoubleShift(volatile  FLByte FAR0 * win,FLWord offset)
{
	FLByte val[2];

	val[0] = ((Reg8bitType)(FLREAD_IO_DWORD(((volatile FLDword FAR0 *)win)+offset)));
    val[1] = ((Reg8bitType)(FLREAD_IO_DWORD(((volatile FLDword FAR0 *)win)+(offset+1))));

	return *((Reg16bitType *)val);
}


/*********************************************************/
/*     Write 8 bits to DiskOnChip memory window          */
/*********************************************************/

/*----------------------------------------------------------------------
   f l W r i t e 8 b i t U s i n g 8 b i t s N o S h i f t

   Write 8-bits Using 8-bits operands with no address shifted.
------------------------------------------------------------------------*/

void FAR1 flWrite8bitUsing8bitsNoShift(volatile FLByte FAR0 * win, FLWord offset,Reg8bitType val)
{
   FLWRITE_IO_BYTE(val,win + offset);
}


/*----------------------------------------------------------------------
   f l W r i t e 8 b i t U s i n g 16 b i t s N o S h i f t

   Note : DiskOnChip is connected with 16-bit data bus.
   Note : Data is written only to lower memory addresses.

   Write 8-bits Using 16-bits operands with no address shifted.
------------------------------------------------------------------------*/

void FAR1 flWrite8bitUsing16bitsNoShift(volatile  FLByte FAR0 * win, FLWord offset,Reg8bitType val)
{
#ifdef FL_BIG_ENDIAN
   FLWRITE_IO_WORD(((FLWord)val)<<8,((volatile FLWord FAR0 *)win)+(offset>>1));
#else
   FLWRITE_IO_WORD((FLWord)val,(((volatile FLWord FAR0 *)win)+(offset>>1)));   
#endif /* FL_BIG_ENDIAN */
}

/*----------------------------------------------------------------------
   f l W r i t e 8 b i t U s i n g 16 b i t s S i n g l e S h i f t

   Note : Data is written only to 8-LSB.

   Write 8-bits Using 16-bits operands with Single address shifted.
------------------------------------------------------------------------*/

void FAR1 flWrite8bitUsing16bitsSingleShift(volatile  FLByte FAR0 * win, FLWord offset,Reg8bitType val)
{
   FLWRITE_IO_WORD((FLWord)val,(((volatile FLWord FAR0 *)win)+offset));
}

/*----------------------------------------------------------------------
   f l W r i t e 8 b i t U s i n g 32 b i t s S i n g l e S h i f t

   Note : DiskOnChip is connected with 16-bit data bus.
   Note : Data is written to both data bus 8-bits

   Write 8-bits Using 32-bits operands with single address shifted.
------------------------------------------------------------------------*/

void FAR1 flWrite8bitUsing32bitsSingleShift(volatile  FLByte FAR0 * win, FLWord offset,Reg8bitType val)
{
#ifdef FL_BIG_ENDIAN
   FLWRITE_IO_DWORD(((FLDword)((FLDword)val*0x01010101L)),(((volatile FLDword FAR0 *)win)+(offset>>1)));
#else
   FLWRITE_IO_DWORD((FLDword)val,(((volatile FLDword FAR0 *)win)+(offset>>1)));
#endif /* FL_BIG_ENDIAN */
}

/*----------------------------------------------------------------------
   f l W r i t e 8 b i t U s i n g 32 b i t s D o u b l e S h i f t

   Note : Data is written only to 8-LSB.

   Write 8-bits Using 32-bits operands with Double address shifted.
------------------------------------------------------------------------*/

void FAR1 flWrite8bitUsing32bitsDoubleShift(volatile  FLByte FAR0 * win, FLWord offset,Reg8bitType val)
{
   FLWRITE_IO_DWORD((FLDword)val,(((volatile FLDword FAR0 *)win)+offset));
}

/*********************************************************/
/*     Read 8 bits to DiskOnChip memory window           */
/*********************************************************/

/*----------------------------------------------------------------------
   f l R e a d 8 b i t U s i n g 8 b i t s N o S h i f t

   Read 8-bits Using 8-bits operands with no address shifted.
------------------------------------------------------------------------*/

Reg8bitType FAR1 flRead8bitUsing8bitsNoShift(volatile  FLByte FAR0 * win,FLWord offset)
{
   return FLREAD_IO_BYTE(((volatile void FAR0 *)(win + offset)));
}


/*----------------------------------------------------------------------
   f l R e a d 8 b i t U s i n g 16 b i t s N o S h i f t

   Note : DiskOnChip is connected with 16-bit data bus.

   Read 8-bits Using 16-bits operands with no address shifted.
------------------------------------------------------------------------*/

Reg8bitType FAR1 flRead8bitUsing16bitsNoShift(volatile  FLByte FAR0 * win,FLWord offset)
{
#ifdef FL_BIG_ENDIAN
   return (((offset & 0x1) == 0) ?
#else
   return (( offset & 0x1      ) ?
#endif /* FL_BIG_ENDIAN */
      (Reg8bitType)(FLREAD_IO_WORD(((volatile FLWord FAR0 *)win + (offset>>1))) >> 8) :
      (Reg8bitType)(FLREAD_IO_WORD(((volatile FLWord FAR0 *)win + (offset>>1)))) );

}

/*----------------------------------------------------------------------
   f l R e a d 8 b i t U s i n g 16 b i t s S i n g l e S h i f t

   Note : Assume data is found in 8-LSB of DiskOnChip

   Read 8-bits Using 16-bits operands with Single address shifted.
------------------------------------------------------------------------*/

Reg8bitType FAR1 flRead8bitUsing16bitsSingleShift(volatile  FLByte FAR0 * win,FLWord offset)
{
   return( (Reg8bitType)FLREAD_IO_WORD((((volatile FLWord FAR0 *)win)+offset)) );
}

/*----------------------------------------------------------------------
   f l R e a d 8 b i t U s i n g 32 b i t s S i n g l e S h i f t

   Note : DiskOnChip is connected with 16-bit data bus.

   Read 8-bits Using 16-bits operands with Single address shifted.
------------------------------------------------------------------------*/

Reg8bitType FAR1 flRead8bitUsing32bitsSingleShift(volatile  FLByte FAR0 * win,FLWord offset)
{
#ifdef FL_BIG_ENDIAN
   return (((offset & 0x1) == 0) ?
           (Reg8bitType)(FLREAD_IO_DWORD(((volatile FLDword FAR0 *)win)+(offset>>1))>>24) :
           (Reg8bitType)(FLREAD_IO_DWORD(((volatile FLDword FAR0 *)win)+(offset>>1))>>16));
#else
   return (( offset & 0x1      ) ?
           (Reg8bitType)(FLREAD_IO_DWORD(((volatile FLDword FAR0 *)win)+(offset>>1))>>8) :
           (Reg8bitType)(FLREAD_IO_DWORD(((volatile FLDword FAR0 *)win)+(offset>>1))) );
#endif /* FL_BIG_ENDIAN */
}

/*----------------------------------------------------------------------
   f l R e a d 8 b i t U s i n g 32 b i t s D o u b l e S h i f t

   Note : Assume data is found in 8-LSB of DiskOnChip

   Read 8-bits Using 16-bits operands with Single address shifted.
------------------------------------------------------------------------*/

Reg8bitType FAR1 flRead8bitUsing32bitsDoubleShift(volatile  FLByte FAR0 * win,FLWord offset)
{
   return((Reg8bitType)FLREAD_IO_DWORD(((volatile FLDword FAR0 *)win)+offset));
}

/*********************************************************/
/*********************************************************/
/***    Operation on several bytes (read/write/set)    ***/
/*********************************************************/
/*********************************************************/

/*************************************************/
/*         8-Bit DiskOnChip - No Shift           */
/*************************************************/

/*----------------------------------------------------------------------
   f l 8 b i t D o c R e a d N o S h i f t

   Read 'count' bytes, from a none shifted address bus using tffscpy.
------------------------------------------------------------------------*/

void FAR1 fl8bitDocReadNoShift(volatile  FLByte FAR0 * win,FLWord offset,FLByte FAR1* dest,FLWord count)
{
#ifdef FL_ENVIRONMENT_VARS
  if(flPreventMemcpy == 1)
  {
	  register int i ;

	  for(i = 0 ; i < count ; i++)
		  dest[i] = FLREAD_IO_BYTE(win + offset + i);
  }
  else
#endif /* FL_ENVIRONMENT_VARS */
  TFFSCPY_FROM_IO_8_BITS(dest,win+offset,count);
}

/*----------------------------------------------------------------------
   f l 8 b i t D o c W r i t e N o S h i f t

   Write 'count' bytes, from a none shifted address bus using tffscpy.
------------------------------------------------------------------------*/

void FAR1 fl8bitDocWriteNoShift(volatile  FLByte FAR0 * win,FLWord offset,FLByte FAR1* src,FLWord count)
{
#ifdef FL_ENVIRONMENT_VARS
  if(flPreventMemcpy == 1)
  {
	 register int i ;

	 for(i = 0 ; i < count ; i++)
        FLWRITE_IO_BYTE(src[i],win + offset + i);		  
  }
  else
#endif /* FL_ENVIRONMENT_VARS */
	TFFSCPY_TO_IO_8_BITS(win+offset,src,count);
}

/*----------------------------------------------------------------------
   f l 8 b i t D o c S e t N o S h i f t

   Set 'count' bytes, from a none shifted address bus using tffsset.
------------------------------------------------------------------------*/

void FAR1 fl8bitDocSetNoShift(volatile  FLByte FAR0 * win,FLWord offset,FLWord count, FLByte val)
{
#ifdef FL_ENVIRONMENT_VARS
  if(flPreventMemcpy == 1)
  {
	 register int i ;
	 for(i = 0 ; i < count ; i++)
        FLWRITE_IO_BYTE(val,win + offset + i);		  
  }
  else
#endif /* FL_ENVIRONMENT_VARS */
	TFFSSET_IO_8_BITS(win+offset,val,count);
}

/*************************************************/
/*        8-Bit DiskOnChip - Single Shift        */
/*************************************************/

/*----------------------------------------------------------------------
   f l 8 b i t D o c R e a d S i n g l e S h i f t

   Note : Assume data is found in 8-LSB of DiskOnChip

   Read 'count' bytes, from data bus's LSB lane with 1 address shifted
------------------------------------------------------------------------*/

void FAR1 fl8bitDocReadSingleShift(volatile  FLByte FAR0 * win,FLWord offset,FLByte FAR1* dest,FLWord count)
{
  volatile FLWord FAR0 * doc = ((volatile FLWord FAR0 *)win) + offset;
  register int         i;


  for(i=0;( i < count );i++)
    dest[i] = (Reg8bitType)FLREAD_IO_WORD(doc+i);
}

/*----------------------------------------------------------------------
   f l 8 b i t D o c W r i t e S i n g l e S h i f t

   Note : Assume data is found in 8-LSB of DiskOnChip

   Write 'count' bytes, to data bus's LSB lane with 1 address shifted.
------------------------------------------------------------------------*/

void FAR1 fl8bitDocWriteSingleShift(volatile  FLByte FAR0 * win,FLWord offset,FLByte FAR1* src,FLWord count)
{
  volatile FLWord FAR0 * doc = ((volatile FLWord FAR0 *)win) + offset;
  register int  i;

  for(i=0;( i < count );i++)
    FLWRITE_IO_WORD((FLWord)(src[i]),doc+i);
}

/*----------------------------------------------------------------------
   f l 8 b i t D o c S e t S i n g l e S h i f t

   Note : Assume data is found in 8-LSB of DiskOnChip

   Set 'count' bytes, of data bus's LSB lane with 1 address shifted.
------------------------------------------------------------------------*/

void FAR1 fl8bitDocSetSingleShift(volatile  FLByte FAR0 * win,FLWord offset,FLWord count, FLByte val)
{
  volatile FLWord FAR0 * doc = ((volatile FLWord FAR0 *)win) + offset;
  register int  i;

   for(i=0;( i < count );i++)
      FLWRITE_IO_WORD((FLWord)val,doc+i);
}

/*************************************************/
/*        8-Bit DiskOnChip - Double Shift        */
/*************************************************/

/*----------------------------------------------------------------------
   f l 8 b i t D o c R e a d D o u b l e S h i f t

   Note : Assume data is found in 8-LSB of DiskOnChip

   Read 'count' bytes, from data bus's LSB lane with 2 address shifted.
------------------------------------------------------------------------*/

void FAR1 fl8bitDocReadDoubleShift(volatile  FLByte FAR0 * win,FLWord offset,FLByte FAR1* dest,FLWord count)
{
  volatile FLDword FAR0 *doc = ((volatile FLDword FAR0 *)win) + offset;
  register int         i;


  for(i=0;( i < count );i++)
     dest[i] = (Reg8bitType)FLREAD_IO_DWORD(doc+i);
}

/*----------------------------------------------------------------------
   f l 8 b i t D o c W r i t e D o u b l e S h i f t

   Note : Assume data is found in 8-LSB of DiskOnChip

   Write 'count' bytes, to data bus's LSB lane with 2 address shifted.
------------------------------------------------------------------------*/

void FAR1 fl8bitDocWriteDoubleShift(volatile  FLByte FAR0 * win,FLWord offset,FLByte FAR1* src,FLWord count)
{
  volatile FLDword FAR0 * doc = ((volatile FLDword FAR0 *)win) + offset;
  register int         i;

  for(i=0;( i < count );i++)
    FLWRITE_IO_DWORD((FLDword)(src[i]),doc+i);
}

/*----------------------------------------------------------------------
   f l 8 b i t D o c S e t D o u b l e S h i f t

   Note : Assume data is found in 8-LSB of DiskOnChip

   Set 'count' bytes, of data bus's LSB lane with 2 address shifted.
------------------------------------------------------------------------*/

void FAR1 fl8bitDocSetDoubleShift(volatile  FLByte FAR0 * win,FLWord offset,FLWord count, FLByte val)
{
  volatile FLDword FAR0 * doc = ((volatile FLDword FAR0 *)win)+offset;
  register int         i;

  for(i=0;( i < count );i++)
    FLWRITE_IO_DWORD((FLDword)val,doc+i);
}

/*************************************************/
/*        16-Bit DiskOnChip - No Shift           */
/*************************************************/

/*----------------------------------------------------------------------
   f l 1 6 b i t D o c R e a d N o S h i f t

   Note - this routine can read only even number of bytes 

   Read 'count' bytes from M+ DiskOnChip with none shifted address bus.
------------------------------------------------------------------------*/

void FAR1 fl16bitDocReadNoShift (volatile  FLByte FAR0 * win, FLWord offset, FLByte FAR1 * dest, FLWord count )
{
   volatile FLWord FAR0 * swin = (volatile FLWord FAR0 *)( win + offset);
   register int         i;
   register FLWord        tmp;


   if( (FLDword)dest & 0x1 )
   {
      /* rare case: unaligned target buffer */
      for (i = 0; i < (int)count; )
      {
         tmp = FLREAD_IO_WORD(swin);
#ifdef FL_BIG_ENDIAN
         dest[i++] = (FLByte)(tmp>>8);
         dest[i++] = (FLByte)tmp;
#else
         dest[i++] = (FLByte)tmp;
         dest[i++] = (FLByte)(tmp>>8);
#endif /* FL_BIG_ENDIAN */
      }
   }
   else
   {   /* mainstream case */
#ifdef FL_ENVIRONMENT_VARS
      /* Some memcpy implementations from none 4 bytes aligned destination
	   * buffer may use a for loop of single byte calls to the first 2 
	   * bytes. This implementation may be good for RAM, but will be 
	   * problematic to 16bit DiskOnChip that does not have a BHE signal.
	   * so if the buffer is not dword aligned we would not perform memcpy */

      if ((flPreventMemcpy == 0) &&  (((FLDword)dest&0x3)==0)   )
      {
        TFFSCPY_FROM_IO_16_BITS( dest,win + offset, count); 
      }
      else
#endif /* FL_ENVIRONMENT_VARS */
      {   /* read in short words */
         for (i = 0, count = count >> 1; i < (int)count; i++)
#ifndef FL_XSCALE_BOOT_MODE
            /* while incrementing DiskOnChip offset */
            ((FLWord FAR1 *)dest)[i] = FLREAD_IO_WORD(swin+i);
#else
            /* but do not increment DiskOnChip offset */
            ((FLWord FAR1 *)dest)[i] = FLREAD_IO_WORD(swin);
#endif /* FL_XSCALE_BOOT_MODE */
      }
   }
}

/*----------------------------------------------------------------------
   f l 1 6 b i t D o c W r i t e N o S h i f t

   Note - this routine can write only even number of bytes 

   Write 'count' bytes to M+ DiskOnChip with none shifted address bus.
------------------------------------------------------------------------*/

void FAR1 fl16bitDocWriteNoShift ( volatile  FLByte FAR0 * win , FLWord offset ,
                             FLByte FAR1 * src, FLWord count )
{
   volatile FLWord FAR0 * swin = (volatile FLWord FAR0 *)( win + offset);
   register int         i;
   register FLWord        tmp;

   if( (FLDword)src & 0x1 ) /* rare case: unaligned source buffer */
   {
       for (i = 0; i < (int)count; i+=2)
       {
          /* tmp variable is just a precation from compiler optimizations */
#ifdef FL_BIG_ENDIAN
          tmp = ((FLWord)src[i]<<8) + (FLWord)src[i+1];
#else
          tmp = (FLWord)src[i] + ((FLWord)src[i+1]<<8);
#endif /* FL_BIG_ENDIAN */
         FLWRITE_IO_WORD(tmp,swin);
        }
   }
   else /* mainstream case */
   {
#ifdef FL_ENVIRONMENT_VARS
      if ((flPreventMemcpy == 0) && (((FLDword)src&0x3)==0))
      {
        TFFSCPY_TO_IO_16_BITS(win + offset,src,count);
      }
      else
#endif /* FL_ENVIRONMENT_VARS */
      {
         /* write in short words */
         for (i = 0, count = count >> 1; i < (int)count; i++)
           /* while incrementing DiskOnChip offset */
           /* but do not increament DiskOnChip offset */
            FLWRITE_IO_WORD(((FLWord FAR1 *)src)[i],swin);
      }
   }
}

/*----------------------------------------------------------------------
   f l 1 6 b i t D o c S e t N o S h i f t

   Note - this routine can write only even number of bytes 

   Set 'count' bytes of M+ DiskOnChip with none shifted address bus
------------------------------------------------------------------------*/

void FAR1 fl16bitDocSetNoShift ( volatile  FLByte FAR0 * win , FLWord offset ,
                                  FLWord count , FLByte val)
{
   volatile FLWord FAR0 * swin = (volatile FLWord FAR0 *)( win + offset);
   register int         i;
   register FLWord        tmpVal = (FLWord)val * 0x0101;

#ifdef FL_ENVIRONMENT_VARS
   if (flPreventMemcpy == 0)
   {
       TFFSSET_IO_16_BITS(win + offset, val, count );
   }
   else
#endif /* FL_ENVIRONMENT_VARS */
   {   /* write in short words */
      for (i = 0; i < (int)count; i+=2)
       FLWRITE_IO_WORD(tmpVal,swin);
   }
}

/*************************************************************/
/*    16-Bit DiskOnChip - No Shift - Only 8 bits are valid   */
/*************************************************************/

/*----------------------------------------------------------------------
   f l 1 6 b i t D o c R e a d N o S h i f t I g n o r e H i g h e r 8 B i t s

   Note : offset must be 16-bits aligned.

   Read 'count' bytes from M+ DiskOnChip connected with all 16 data bits, but
   in interleave-1 mode , therefore only one of the 8 bits contains actual data.
   The DiskOnChip is connected without an address shift.
------------------------------------------------------------------------*/

void FAR1 fl16bitDocReadNoShiftIgnoreHigher8bits(volatile  FLByte FAR0 * win, FLWord offset, FLByte FAR1 * dest, FLWord count )
{
   volatile FLWord FAR0 * swin = (volatile FLWord FAR0 *)( win + offset);
   register int         i;


   for (i = 0; i < (int)count; i++)
   {
#ifndef FL_XSCALE_BOOT_MODE
      /* Read while incrementing DiskOnChip window offset */
#ifdef FL_BIG_ENDIAN
      dest[i] = (FLByte)(FLREAD_IO_WORD((volatile void FAR0 *)(swin+i))>>8);
#else
      dest[i] = (FLByte)FLREAD_IO_WORD((volatile void FAR0 *)(swin+i));
#endif /* FL_BIG_ENDIAN */
#else
      /* Read while not incrementing DiskOnChip window offset */
#ifdef FL_BIG_ENDIAN
   dest[i] = (FLByte)(FLREAD_IO_WORD(swin)>>8);
#else
   dest[i] = (FLByte)(FLREAD_IO_WORD(swin));
#endif /* FL_BIG_ENDIAN */
#endif /* FL_XSCALE_BOOT_MODE */
   }
}

/*----------------------------------------------------------------------
   f l 1 6 D o c W r i t e N o S h i f t I g n o r e H i g h e r 8 b i t s

   Note : offset must be 16-bits aligned.

   Write 'count' bytes to M+ DiskOnChip connected with all 16 data bits, but
   in interleave-1 mode , therefore only one of the 8bits contains actual data.
   The DiskOnChip is connected without an address shift.
------------------------------------------------------------------------*/

void FAR1 fl16bitDocWriteNoShiftIgnoreHigher8bits ( volatile  FLByte FAR0 * win , FLWord offset ,
                             FLByte FAR1 * src, FLWord count )
{
   volatile FLWord FAR0 * swin = (volatile FLWord FAR0 *)( win + offset);
   register int         i;

   for (i = 0; i < (int)count; i++)
   {
      /* Write while not incrementing DiskOnChip window offset */
#ifdef FL_BIG_ENDIAN
      FLWRITE_IO_WORD((((FLWord)src[i])<<8),swin);
#else
      FLWRITE_IO_WORD((FLWord)src[i],swin);
#endif /* FL_BIG_ENDIAN */
   }
}

/*----------------------------------------------------------------------
   f l 1 6 D o c S e t N o S h i f t I g n o r e H i g h e r 8 b i t s

   Note : offset must be 16-bits aligned.

   Set 'count' bytes to M+ DiskOnChip connected with all 16 data bits, but
   in interleave-1 mode , therefore only one of the 8bits contains actual data.
   The DiskOnChip is connected without an address shift.
------------------------------------------------------------------------*/

void FAR1 fl16bitDocSetNoShiftIgnoreHigher8bits ( volatile  FLByte FAR0 * win , FLWord offset ,
                                  FLWord count , FLByte val)
{
   volatile FLWord FAR0 * swin = (volatile FLWord FAR0 *)( win + offset );
   register int         i;
   register FLWord        tmpVal = val * 0x0101;

   for (i = 0; i < (int)count; i++)
    FLWRITE_IO_WORD(tmpVal,swin);
}

/****************************************/
/*   16-Bit DiskOnChip - Single Shift   */
/****************************************/

/*----------------------------------------------------------------------
   f l 1 6 b i t D o c R e a d S i n g l e S h i f t
 
   Read 'count' bytes from M+ DiskOnChip with none shifted address bus.
------------------------------------------------------------------------*/

void FAR1 fl16bitDocReadSingleShift (volatile  FLByte FAR0 * win, FLWord offset, FLByte FAR1 * dest, FLWord count )
{
   volatile FLDword FAR0 * swin = ((volatile FLDword FAR0 *)win) + (offset>>1);
   register int         i;
   register FLDword       tmp;


   if( (FLDword)dest & 0x1 )
   {
      /* rare case: unaligned target buffer */
      for (i = 0; i < (int)count; )
      {
         tmp = FLREAD_IO_DWORD(swin);
#ifdef FL_BIG_ENDIAN
         dest[i++] = (FLByte)(tmp>>24);
         dest[i++] = (FLByte)(tmp>>16);
#else
         dest[i++] = (FLByte)tmp;
         dest[i++] = (FLByte)(tmp>>8);
#endif /* FL_BIG_ENDIAN */
      }
   }
   else
   {   /* mainstream case */
      for (i = 0, count = count >> 1; i < (int)count; i++)
      {
#ifdef FL_BIG_ENDIAN         
         ((FLWord FAR1 *)dest)[i]=(FLWord)(FLREAD_IO_DWORD(swin+i)>>16);
#else
         ((FLWord FAR1 *)dest)[i] = (FLWord)FLREAD_IO_DWORD(swin+i);
#endif /* FL_BIG_ENDIAN */
      }
   }
}

/*----------------------------------------------------------------------
   f l 1 6 b i t D o c W r i t e S i n g l e S h i f t
 
   Write 'count' bytes to M+ DiskOnChip with none shifted address bus.
------------------------------------------------------------------------*/

void FAR1 fl16bitDocWriteSingleShift ( volatile  FLByte FAR0 * win , FLWord offset ,
                             FLByte FAR1 * src, FLWord count )
{
   volatile FLDword FAR0 * swin = ((volatile FLDword FAR0 *)win)+ (offset>>1);
   register int         i;
   register FLDword       tmp;

   if( (FLDword)src & 0x1 ) /* rare case: unaligned source buffer */
   {       
       for (i = 0; i < (int)count; i+=2)
       {
#ifdef FL_BIG_ENDIAN
          tmp = (((FLDword)src[i])<<24) + (((FLDword)src[i+1])<<16);
#else
          tmp = (FLDword)src[i] + (((FLDword)src[i+1])<<8);
#endif /* FL_BIG_ENDIAN */
           FLWRITE_IO_DWORD(tmp,swin);
       }
   }
   else /* mainstream case */
   {    
      for (i = 0, count = count >> 1; i < (int)count; i++)
#ifdef FL_BIG_ENDIAN
        FLWRITE_IO_DWORD(((FLDword)((FLWord FAR1 *)src)[i])<<16,swin);
#else
        FLWRITE_IO_DWORD(((FLDword)((FLWord FAR1 *)src)[i]),swin);
#endif /* FL_BIG_ENDIAN */
   }
}

/*----------------------------------------------------------------------
   f l 1 6 b i t D o c S e t S i n g l e S h i f t
 
   Set 'count' bytes of M+ DiskOnChip with none shifted address bus
------------------------------------------------------------------------*/

void FAR1 fl16bitDocSetSingleShift ( volatile  FLByte FAR0 * win , FLWord offset ,
                                  FLWord count , FLByte val)
{
   volatile FLDword FAR0 * swin = ((volatile FLDword FAR0 *)win)+ (offset>>1);
   register int          i;
   register FLDword        tmpVal = (FLDword)val * 0x01010101L;

   for (i = 0; i < (int)count; i+=2)
    FLWRITE_IO_DWORD(tmpVal,swin);
}


/**************************************************************/
/*  16-Bit DiskOnChip - Single Shift - Only 8 bits are valid  */
/**************************************************************/

/*----------------------------------------------------------------------
   f l 1 6 b i t D o c R e a d S i n g l e S h i f t I g n o r e H i g h e r 8 B i t s

   Note : offset must be 16-bits aligned.
  
   Read 'count' bytes from M+ DiskOnChip connected with all 16 data bits, but
   in interleave-1 mode , therefore only one of the 8 bits contains actual data.
   The DiskOnChip is connected without an address shift.
------------------------------------------------------------------------*/

void FAR1 fl16bitDocReadSingleShiftIgnoreHigher8bits(volatile  FLByte FAR0 * win, FLWord offset, FLByte FAR1 * dest, FLWord count )
{
   volatile FLDword FAR0* swin = ((volatile FLDword FAR0 *)win)+ (offset>>1);
   register int         i;


   for (i = 0; i < (int)count; i++)
   {
#ifdef FL_BIG_ENDIAN
     dest[i] = (FLByte)(FLREAD_IO_DWORD(swin+i)>>24);
#else
      dest[i] = (FLByte)FLREAD_IO_DWORD(swin+i);
#endif /* FL_BIG_ENDIAN */
   }
}

/*----------------------------------------------------------------------
   f l 1 6 D o c W r i t e S i n g l e S h i f t I g n o r e H i g h e r 8 b i t s

   Note : offset must be 16-bits aligned.

   Write 'count' bytes to M+ DiskOnChip connected with all 16 data bits, but
   in interleave-1 mode , therefore only one of the 8bits contains actual data.
   The DiskOnChip is connected without an address shift.
------------------------------------------------------------------------*/

void FAR1 fl16bitDocWriteSingleShiftIgnoreHigher8bits ( volatile  FLByte FAR0 * win , FLWord offset ,
                             FLByte FAR1 * src, FLWord count )
{
   volatile FLDword FAR0 * swin = ((volatile FLDword FAR0 *)win)+ (offset>>1);
   register int         i;

   for (i = 0; i < (int)count; i++)
   {
#ifdef FL_BIG_ENDIAN
    FLWRITE_IO_DWORD(((FLDword)src[i]<<24),swin);
#else
    FLWRITE_IO_DWORD(((FLDword)src[i]),swin);
#endif /* FL_BIG_ENDIAN */
   }
}

/*----------------------------------------------------------------------
   f l 1 6 D o c S e t S i n g l e S h i f t I g n o r e H i g h e r 8 b i t s

   Note : offset must be 16-bits aligned.

   Set 'count' bytes to M+ DiskOnChip connected with all 16 data bits, but
   in interleave-1 mode , therefore only one of the 8bits contains actual data.
   The DiskOnChip is connected without an address shift.
------------------------------------------------------------------------*/

void FAR1 fl16bitDocSetSingleShiftIgnoreHigher8bits ( volatile  FLByte FAR0 * win , FLWord offset ,
                                  FLWord count , FLByte val)
{
   volatile FLDword FAR0 * swin = ((volatile FLDword FAR0 *)win) + (offset>>1);
   register int         i;
   register FLDword        tmpVal = (FLDword)val * 0x01010101L;

   for (i = 0; i < (int)count; i++)
    FLWRITE_IO_DWORD(tmpVal,swin);
}


/**********************************************************/
/* Set proper access type routines into the proper record */
/**********************************************************/

/*----------------------------------------------------------------------*/
/*                 s e t B u s T y p e O f F l a s h                    */
/*                                                                      */
/* Set DiskOnChip socket / flash memory access routine.                 */
/* This routine must be called by the MTD prior to any access to the    */
/* DiskOnChip                                                           */
/*                                                                      */
/* Parameters:                                                          */
/*    flflash    : Pointer to sockets flash record.                     */
/*    access     : Type of memory access routines to install            */
/*                                                                      */
/* Note: The possible type of memory access routine are comprised of:   */
/*                                                                      */
/*    Address shift:                                                    */
/*         FL_NO_ADDR_SHIFT         - No address shift                  */
/*         FL_SINGLE_ADDR_SHIFT     - Single address shift              */
/*         FL_DOUBLE_ADDR_SHIFT     - Double address shift              */
/*                                                                      */
/*    Platform bus capabilities (access width):                         */
/*         FL_BUS_HAS_8BIT_ACCESS   -  Bus can access 8-bit             */
/*         FL_BUS_HAS_16BIT_ACCESS  -  Bus can access 16-bit            */
/*         FL_BUS_HAS_32BIT_ACCESS  -  Bus can access 32-bit            */
/*                                                                      */
/*    Number of data bits connected to the DiskOnChip (if_cfg):         */
/*         FL_8BIT_DOC_ACCESS       - DiskOnChip has 8 data bits        */
/*         FL_16BIT_DOC_ACCESS      - DiskOnChip has 16 data bits       */
/*                                                                      */
/*    Flash data bits that can be accessed in a bus cycle (interleave): */
/*         FL_8BIT_FLASH_ACCESS     - 8 bits of flash per cycle         */
/*         FL_16BIT_FLASH_ACCESS    - 16 bits of flash per cycle        */
/*                                                                      */
/*    Ignore all of the above and use user defined access routines:     */
/*         FL_ACCESS_USER_DEFINED - Do not install any routine since    */
/*                                  user already installed customer made*/
/*                                  routines                            */
/*                                                                      */
/* Returns:                                                             */
/*        FLStatus        : 0 on success, otherwise failed              */
/*----------------------------------------------------------------------*/

FLStatus FAR1 setBusTypeOfFlash(FLFlash * flash,FLDword access)
{
   /* sanity checks here if needed */
   if(flash==NULL)
   {
      DBG_PRINT_ERR(FLZONE_MTD,"ERROR in setBusTypeOfFlash: Flash record passed to setBusTypeOfFlash is NULL.\r\n");
      return flBadParameter;
   }

   /* check if user already defined the memory access routines */
   if ((access & FL_ACCESS_USER_DEFINED) != 0)
      return flOK;

   /************************************/
   /* install requested access methods */
   /************************************/

   switch(access & FL_XX_ADDR_SHIFT_MASK)
   {
      case FL_NO_ADDR_SHIFT:

         flash->memWindowSize = flDocMemWinSizeNoShift;
         switch(access & FL_XX_DATA_BITS_MASK)
         {
            case FL_8BIT_DOC_ACCESS:  /* if_cfg set to 8-bits */

               /* Make sure bus supports 8 bit access */
               if((access & FL_BUS_HAS_8BIT_ACCESS) == 0)
               {
                  DBG_PRINT_WRN(FLZONE_MTD,"Warning: TrueFFS requires 8-bit access to DiskOnChip memory window\r\n");
                  DBG_PRINT_WRN(FLZONE_MTD,"         for 8-bit DiskOnChip connected with no address shift.\r\n");
                  return flBadParameter;
               }

               flash->memWrite8bit  = flWrite8bitUsing8bitsNoShift;
               flash->memRead8bit   = flRead8bitUsing8bitsNoShift;
               flash->memRead16bit  = flRead16bitUsing8bitsNoShift;
               flash->memWrite16bit = flWrite16bitUsing8bitsNoShift;
               flash->memRead       = fl8bitDocReadNoShift;
               flash->memWrite      = fl8bitDocWriteNoShift;
               flash->memSet        = fl8bitDocSetNoShift;
               break;

            case FL_16BIT_DOC_ACCESS: /* if_cfg set to 16-bits (Plus family) */

               /* Make sure bus supports 16 bit access */
               if((access & FL_BUS_HAS_16BIT_ACCESS) == 0)
               {
                  DBG_PRINT_WRN(FLZONE_MTD,"Warning: TrueFFS requires 16-bit access to DiskOnChip memory window\r\n");
                  DBG_PRINT_WRN(FLZONE_MTD,"         for 16-bit DiskOnChip connected with no address shift.\r\n");
                  return flBadParameter;
               }

               flash->memWrite8bit  = flWrite8bitUsing16bitsNoShift;
               flash->memRead8bit   = flRead8bitUsing16bitsNoShift;
               flash->memRead16bit  = flRead16bitUsing16bitsNoShift;
               flash->memWrite16bit = flWrite16bitUsing16bitsNoShift;

               switch(access & FL_XX_FLASH_ACCESS_MASK) /* Interleave */
               {
                  case FL_8BIT_FLASH_ACCESS:  /* Interleave - 1 */
                     flash->memRead       = fl16bitDocReadNoShiftIgnoreHigher8bits;
                     flash->memWrite      = fl16bitDocWriteNoShiftIgnoreHigher8bits;
                     flash->memSet        = fl16bitDocSetNoShiftIgnoreHigher8bits;
                     break;
                  case FL_16BIT_FLASH_ACCESS: /* Interleave - 2 */
                     flash->memRead       = fl16bitDocReadNoShift;
                     flash->memWrite      = fl16bitDocWriteNoShift;
                     flash->memSet        = fl16bitDocSetNoShift;
                     break;
                  default:
                     DBG_PRINT_ERR(FLZONE_MTD,"ERROR in setBusTypeOfFlash: TrueFFS does not support this flash access type (setBusTypeOfFlash).\r\n");
                     return flBadParameter;
               }
               break;

            default:
               DBG_PRINT_ERR(FLZONE_MTD,"ERROR in setBusTypeOfFlash: TrueFFS does not support this number of DiskOnChip data bits (setBusTypeOfFlash).\r\n");
               return flBadParameter;
         }
         break;

      case FL_SINGLE_ADDR_SHIFT:

         /* Install memory window size routine */
         flash->memWindowSize = flDocMemWinSizeSingleShift;
         switch(access & FL_XX_DATA_BITS_MASK)
         {
            case FL_8BIT_DOC_ACCESS:  /* if_cfg set to 8bits (or none plus family) */

               /* Make sure bus supports 16 bit access */
               if((access & FL_BUS_HAS_16BIT_ACCESS) == 0)
               {
                  DBG_PRINT_WRN(FLZONE_MTD,"Warning: TrueFFS requires 16-bit access to DiskOnChip memory window\r\n");
                  DBG_PRINT_WRN(FLZONE_MTD,"         for 8-bit DiskOnChip connected with a single address shift.\r\n");
                  return flBadParameter;
               }

               flash->memWrite8bit  = flWrite8bitUsing16bitsSingleShift;
               flash->memRead8bit   = flRead8bitUsing16bitsSingleShift;
			   flash->memWrite16bit = flWrite16bitUsing16bitsSingleShift;
               flash->memRead16bit  = flRead16bitUsing16bitsSingleShift;               
               flash->memRead       = fl8bitDocReadSingleShift;
               flash->memWrite      = fl8bitDocWriteSingleShift;
               flash->memSet        = fl8bitDocSetSingleShift;
               break;

            case FL_16BIT_DOC_ACCESS: /* if_cfg set to 16bits */

               /* Make sure bus supports 32 bit access */
               if((access & FL_BUS_HAS_32BIT_ACCESS) == 0)
               {
                  DBG_PRINT_WRN(FLZONE_MTD,"Warning: TrueFFS requires 32-bit access to DiskOnChip memory window\r\n");
                  DBG_PRINT_WRN(FLZONE_MTD,"         for 16-bit DiskOnChip connected with a single address shift.\r\n");
                  return flBadParameter;
               }

               flash->memWrite8bit  = flWrite8bitUsing32bitsSingleShift;
               flash->memRead8bit   = flRead8bitUsing32bitsSingleShift;
               flash->memRead16bit  = flRead16bitUsing32bitsSingleShift;
               flash->memWrite16bit = flWrite16bitUsing32bitsSingleShift;

               switch(access & FL_XX_FLASH_ACCESS_MASK) /* Interleave */
               {
                  case FL_8BIT_FLASH_ACCESS:  /* Interleave - 1 */
                     flash->memRead       = fl16bitDocReadSingleShiftIgnoreHigher8bits;
                     flash->memWrite      = fl16bitDocWriteSingleShiftIgnoreHigher8bits;
                     flash->memSet        = fl16bitDocSetSingleShiftIgnoreHigher8bits;
                     break;
                  case FL_16BIT_FLASH_ACCESS: /* Interleave - 2 */
                     flash->memRead       = fl16bitDocReadSingleShift;
                     flash->memWrite      = fl16bitDocWriteSingleShift;
                     flash->memSet        = fl16bitDocSetSingleShift;
                     break;
                  default:
                     DBG_PRINT_ERR(FLZONE_MTD,"ERROR in setBusTypeOfFlash: TrueFFS does not support this flash access type (setBusTypeOfFlash).\r\n");
                     return flBadParameter;
               }
               break;

            default:
               DBG_PRINT_ERR(FLZONE_MTD,"ERROR in setBusTypeOfFlash: TrueFFS does not support this number of DiskOnChip data bits (setBusTypeOfFlash).\r\n");
               return flBadParameter;
         }
         break;

      case FL_DOUBLE_ADDR_SHIFT:

         /* Install memory window size routine */
         flash->memWindowSize = flDocMemWinSizeDoubleShift;
         switch(access & FL_XX_DATA_BITS_MASK)
         {
            case FL_8BIT_DOC_ACCESS:  /* if_cfg set to 8bits or none plus family */

               /* Make sure bus supports 32 bit access */
               if((access & FL_BUS_HAS_32BIT_ACCESS) == 0)
               {
                  DBG_PRINT_WRN(FLZONE_MTD,"Warning: TrueFFS requires 32-bit access to DiskOnChip memory window\r\n");
                  DBG_PRINT_WRN(FLZONE_MTD,"         for 8-bit DiskOnChip connected with a double address shift.\r\n");
                  return flBadParameter;
               }

               flash->memWrite8bit  = flWrite8bitUsing32bitsDoubleShift;
               flash->memRead8bit   = flRead8bitUsing32bitsDoubleShift;
			   flash->memWrite16bit = flWrite16bitUsing32bitsDoubleShift;
               flash->memRead16bit  = flRead16bitUsing32bitsDoubleShift;               
               flash->memRead       = fl8bitDocReadDoubleShift;
               flash->memWrite      = fl8bitDocWriteDoubleShift;
               flash->memSet        = fl8bitDocSetDoubleShift;
               break;

            default:
               DBG_PRINT_ERR(FLZONE_MTD,"ERROR in setBusTypeOfFlash: TrueFFS does not support this number of DiskOnChip data bits\r\n");
               DBG_PRINT_ERR(FLZONE_MTD,"                            when connected with a double address shift (setBusTypeOfFlash).\r\n");
               return flBadParameter;
         }
         break;

      default:
         DBG_PRINT_ERR(FLZONE_MTD,"ERROR in setBusTypeOfFlash: TrueFFS does not support this kind of address shifting (setBusTypeOfFlash).\r\n");
         return flBadParameter;
   }

   /* Store access type in flash record */
   flash->busAccessType = access;
   return flOK;
}

#endif /* FL_NO_USE_FUNC */





