/******************************************************************************
 *                                                                            *
 * Project: DOC Driver for Linux 2.6 Block device driver for mDOC H3  family  *
 * of devices under Linux kernel 2.6.                                         *
 *                                                                            *
 *   Version: 1.0                                                             *
 *   Email questions to: oemsupport@sandisk.com                               *
 *   Copyright (C) SanDisk IL Ltd. 1995 - 2007                                *
 *   SanDisk IL Ltd., 7 Atir Yeda Street, Kfar Saba 44425, Israel             *
 *                                                                            *
 ******************************************************************************
 *                                                                            *
 * This program is free software; you can redistribute it and/or modify it    *
 * under the terms of the GNU General Public License as published by the Free *
 * Software Foundation; either version 2 of the License, or any later version.*
 * This program is distributed in the hope that it will be useful, but WITHOUT*
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or      *
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for  *
 * more details, which is set forth in the readme.txt file.                   *
 * You should have received a copy of the GNU General Public License along    *
 * with this program; if not, write to the Free Software Foundation, Inc., 51 *
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA                    *
 *                                                                            *
 * This License does not grant you any right to use the trademarks, service   *
 * marks or logos of SanDisk IL Ltd. or SanDisk Corporation.                  *
 * Subject to the foregoing, SanDisk IL Ltd., for itself and on behalf of its *
 * licensors, hereby reserves all intellectual property rights in the program,*
 * except for the rights expressly granted in this License.                   *
 *                                                                            *
 ******************************************************************************/

/*
 * $Log$
 */


#include "tffsdrv.h"
#include "flbase.h"
#include "doch_sys.h"



/*
 * macro
 */

#define Align(ptr,alignment)  (((unsigned long)(ptr)) & (~((alignment) - 1)))

#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)) && !defined(virt_addr_valid)
#  define virt_addr_valid(vaddr)  (((vaddr) >= PAGE_OFFSET) && ((vaddr) < (unsigned long)high_memory))
#endif


/*
 * static routines
 */
static void __dmasw_bread  (int off, void *vbuf, unsigned int bytes);
static void __dmasw_bwrite (int off, void *vbuf, unsigned int bytes);




/*-----------------------------------------------------------------------*
 *                                                                       *
 *                           t f f s _ d m a s w                         *
 *                                                                       *
 * Transfer data between DiskOnChip's and RAM buffer.                    *
 * This routines handles any kind of virtual buffer (linearly mapped or  *
 * vmalloc() allocated, spanning multiple memory pages, containing       *
 * multiple sectors).                                                    *
 *                                                                       *
 * Parameters:                                                           *
 *      win            pointer to DiskOnChip's I/O window                *
 *      off            offset of DiskOnChip's I/O register from the base *
 *                     of DiskOnChip window                              *
 *      vbuf           virtual address of RAM buffer                     *
 *      bytes          bytes to transfer                                 *
 *      read           DMA direction: '1' for DiskOnChip->RAM, or zero   *
 *                     for RAM->DiskOnChip                               *
 *                                                                       *
 *-----------------------------------------------------------------------*/

void tffs_dmasw ( char         * win,
                  int            off,
                  void         * vbuf, 
                  unsigned int   bytes,
                  int            read )
{
    /* We check if 'vbuf' is linearly mapped kernel virtual address (and
     * is therefore laid out in consecutive physical pages). If 'vbuf' 
     * is  not linearly mapped (i.e. came from user space or allocated 
     * via vmalloc()), we check if it entirely fits into a single memory
     * page.
     */
    if ((virt_addr_valid((unsigned long)vbuf)) /* linearly mapped kernel virtual address */
            ||
        (Align(vbuf,PAGE_SIZE) == Align((unsigned long)vbuf + bytes,PAGE_SIZE)))
    {
        if (read == 1)
            __dmasw_bread (off, vbuf, bytes);
        else
            __dmasw_bwrite (off, vbuf, bytes);
    }
    else  /* 'vbuf' crosses memory page boundary */
    {
        if (bytes >= (FL_SECTOR_SIZE * 2))  /* two or more sectors */
        {
            register unsigned long  bytes_on_this_page;

            /* Do multiple DMA transfers, each one fitting into a single
             * memory page.
             */
            do 
            {
                bytes_on_this_page = PAGE_SIZE - ((unsigned long)vbuf & (PAGE_SIZE - 1));
                if (bytes_on_this_page > bytes)
                    bytes_on_this_page = bytes;

                if (read == 1)
                    __dmasw_bread (off, vbuf, bytes_on_this_page);
                else
                    __dmasw_bwrite (off, vbuf, bytes);

                bytes -= bytes_on_this_page;
                vbuf   = (char *)vbuf + bytes_on_this_page;

            } while (bytes > 0);
        }
        else  /* single sector; don't bother with DMA ... */
        {
            if (read == 1)
                tffs_readw  ((unsigned long)(win + off), vbuf, (bytes / sizeof(short)));
            else
                tffs_writew ((unsigned long)(win + off), vbuf, (bytes / sizeof(short)));
        }
    }
}





#if defined(CONFIG_ARCH_MAINSTONE) || defined(CONFIG_MACH_MAINSTONE)

/*_____________________________________________________________________________
 |                                                                            |
 |                                                                            |
 |                Intel PXA27x ("Mainstone") board                            |
 |                                                                            |
 |____________________________________________________________________________| 
*/


# include <asm/dma.h>
# include <linux/pci.h>
# include <asm/arch/pxa-regs.h>


/* Routine blk_dma_inv_range_harvard() (arch/arm/mm/blockops.c) appears
 * to be buggy: it seems to constantly shoot past the end of the address
 * range [vbuf..vbuf+bytes] by 32 bytes. It also appears to write some
 * bogus data into this RAM address range while invalidating it in D-cache. 
 */
# define PXA27X_DCACHE_BUG_WORKAROUND


/*
 * static routines for PXA27x
 */

static void tffs_dma_init (int resume);
static void tffs_dma_release (void);
static void tffs_dma_intr (int dma, void *dummy, struct pt_regs *regs);




/*-----------------------------------------------------------------------*
 *                                                                       *
 *                    t f f s a r c h _ i n i t                          *
 *                                                                       *
 * This routine is called during module initialization time with 'resume'*
 * argument set to FALSE, and from Power Manager's 'resume' routine with *
 * 'resume' argument set to TRUE. If 'resume' is FALSE, this routine     *
 * allocates and initializes all required board resources; if 'resume'   *
 * is TRUE, this routine does minimal required re-initialization of      *
 * these resources after power suspend.                                  *
 *                                                                       *
 * Parameters:                                                           *
 *      suspend     FALSE (module initialization) or TRUE (power resume) *
 *                                                                       *
 * Returns:                                                              *
 *      always '1' (success)                                             *
 *                                                                       *
 *-----------------------------------------------------------------------*/

unsigned char tffsarch_init ( int resume )
{
    /* chip select that DiskOnChip is connected to */
#define PXA27X_CS  5      

    /* Value to write to chip select's conf register. Possible values are:
     *
     *     0x099C   VLIO DiskOnChip adapter and DiskOnChip G4/G3/H3 device
     *     0x0AAC   VLIO DiskOnChip adapter and DiskOnChip H1 device 
     *     0xA998   sync DiskOnChip adapter and DiskOnChip G4/G3/H3 device
     *     0xAAA8   sync DiskOnChip adapter and DiskOnChip H1 device
     *
     * Register description:
     *
     *     31     0 - slow device (VLIO); 1 - fast device
     *     30:28  The value of this bit is half the number of memory clock cycles
     *            from the time that chip select is de-asserted after a read or
     *            write until the next chip select (of a different static memory
     *            bank) or nSDCS is asserted
     *     27:24  ROM Delay Next Access The RDN field is encoded as follows:
     *            ENCODED (Programmed) Value -----> DECODED (Actual) Value
     *                                  0-11 -----> 0-11
     *                                    12 -----> 15
     *                                    13 -----> 20
     *                                    14 -----> 26
     *                                    15 -----> 30
     *            RDNx * 2 = amount of time nOE or nPWE is deasserted to address
     *            hold and address setup to nOE or nPWE assertion time
     *     23:20  This determines the minimum number of memory clock cycles
     *            (minus 1) of nOE (nPWE) assert time for each beat of read (write)
     *        19  ROM Bus Width (1 = 16 bits)
     *     16-18  0 - Synchronous flash; 2 - burst 4; 3 - burst 8; 4 - Variable-latency
     *            I/O (VLIO)
     */

#define PXA27X_CS_VAL  0x026C /* andrayk Nov 242 006: originally was 0x044C */

    int            cs  = PXA27X_CS;
    unsigned long  val;

    if (resume == FALSE)
        { PrintkInfo ("use Chip Select %d", cs); }

    /* Line below configures GPIO33 as output. 
     * Without this line, DiskOnChip driver fails to detect DiskOnChip G4.
     */
    GPDR1 |= 0x2; /* was: GPDR1 = 0xfc3fab83 */

    /* read chip select config reg */
    val = __REG(0x48000008 + ((cs / 2) * sizeof(long)));

    if (cs & 1)
        val = ((PXA27X_CS_VAL << 16) | (val & 0xffff));
    else
        val = ((val & 0xffff0000) | PXA27X_CS_VAL);

    /* write chip select config reg */
    __REG(0x48000008 + ((cs / 2) * sizeof(long))) = val;

    /* if DMA is requested, configure it */
    if (tffs_dma_mode > 0)
    {
        tffs_dma_init (resume);
    }

    return 1;
}



void TffsHWRelease(void)
{
    /* if DMA was actually used, release all DMA resources */
    if (tffs_dma_mode > 0)
    {
        tffs_dma_release ();
    }
}




/*-----------------------------------------------------------------------*
 *                                                                       *
 *                    t f f s _ d m a _ i n i t                          *
 *                                                                       *
 * This routine is called during module initialization time with 'resume'*
 * argument set to FALSE, and from Power Manager's 'resume' routine with *
 * 'resume' argument set to TRUE. If 'resume' is FALSE, this routine     *
 * allocates and initializes all required DMA resources; if 'resume' is  *
 * TRUE, this routine does minimal required re-initialization of DMA     *
 * channel after power suspend.                                          *
 *                                                                       *
 * Parameters:                                                           *
 *      resume     FALSE (module initialization) or TRUE (power resume)  *
 *                                                                       *
 *-----------------------------------------------------------------------*/
static 
void tffs_dma_init (int resume)
{
    if (resume == FALSE) 
    {
        /* We use single DMA channel for both 'read' and
         * 'write' operations, so we allocate it here.
         */
        if ((tffsInfo.channel = pxa_request_dma(TFFS_DEVICE_NAME, 
                                                DMA_PRIO_LOW, tffs_dma_intr, NULL)) < 0)
        {
            /* failed to allocate DMA channel, won't use DMA */
            tffs_dma_mode = 0;

            PrintkError ("can't get DMA chan");
            return;
        }
        PrintkInfo ("use DMA channel %d", tffsInfo.channel);
    }

    DRCMR(tffsInfo.channel) = (DRCMR_MAPVLD | (tffsInfo.channel & 0x1f));
    __REG(0x400000a0) |= (1 << tffsInfo.channel);

    /* clear any pending DMA errors/interrupts */
    DCSR(tffsInfo.channel) = (DCSR_NODESC | DCSR_ENDINTR | DCSR_STARTINTR | DCSR_BUSERR);

    if (resume == FALSE)
    {
        tffsInfo.dma_buf_vptr = NULL;
        tffsInfo.dma_buf_size = 0;

        if ((tffs_dma_mode >= 1)
#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)) || !defined(PXA27X_DCACHE_BUG_WORKAROUND)
                                 && (tffs_dma_mode <= 3)
#endif
           )
        {
            /* allocate intermediate DMA buffer (we assume one MMU page will be enough) */
            tffsInfo.dma_buf_size = PAGE_SIZE;

            tffsInfo.dma_buf_vptr = 
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
                consistent_alloc ((GFP_KERNEL | GFP_DMA), tffsInfo.dma_buf_size, 
                                        &tffsInfo.dma_buf_paddr);
#else
                dma_alloc_coherent (NULL, tffsInfo.dma_buf_size, 
                                        &tffsInfo.dma_buf_paddr, (GFP_KERNEL | GFP_DMA));
#endif
            if (tffsInfo.dma_buf_vptr == NULL)
            {
                /* failed to allocate intermediate DMA buffer, won't use DMA */
                tffs_dma_mode = 0;

                pxa_free_dma (tffsInfo.channel);

                tffsInfo.channel      = -1;
                tffsInfo.dma_buf_size = 0;

                PrintkError ("can't alloc DMA buffer, won't use DMA");
            }
        }
    }
}



/*-----------------------------------------------------------------------*
 *                                                                       *
 *                    t f f s _ d m a _ r e l e a s e                    *
 *                                                                       *
 * Release all previously allocated DMA resources (channel, intermediate *
 * DMA buffer etc.). For Intel PXA27x (Mainstone) board.                 *
 *                                                                       *
 *-----------------------------------------------------------------------*/

static 
void tffs_dma_release(void)
{
    /* if previously was allocated, release DMA channel */
    if (tffsInfo.channel >= 0)
    {
        pxa_free_dma (tffsInfo.channel);

        tffsInfo.channel = -1;
    }

    /* if previously was allocated, release intermediate DMA buffer */
    if (tffsInfo.dma_buf_vptr != NULL)
    {
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
        consistent_free (tffsInfo.dma_buf_vptr, PAGE_SIZE, tffsInfo.dma_buf_paddr);
#else
        dma_free_coherent (NULL, PAGE_SIZE, tffsInfo.dma_buf_vptr, tffsInfo.dma_buf_paddr);
#endif
        tffsInfo.dma_buf_vptr = NULL;
    }
}




/*-----------------------------------------------------------------------*
 *                                                                       *
 *                    _ _ d m a s w _ b r e a d                          *
 *                                                                       *
 * Transfer data from DiskOnChip's I/O reguster to RAM buffer.           *
 * Example implementation For Intel PXA27x (Mainstone) board.            *
 *                                                                       *
 * Parameters:                                                           *
 *      off            offset of DiskOnChip's I/O register from the base *
 *                     of DiskOnChip window                              *
 *      vbuf           virtual address of RAM buffer                     *
 *      bytes          bytes to transfer                                 *
 *                                                                       *
 *-----------------------------------------------------------------------*/
static
void __dmasw_bread ( int            off,
                     void         * vbuf, 
                     unsigned int   bytes )
{
    int           chan = tffsInfo.channel;
    u_int         tmp;
    register int  waiting;
    register int  direct_dma;

    profiling[2]++;

    direct_dma = ((tffs_dma_mode & 4) ? TRUE : FALSE);

    /* Due to a bug in kernel routine blk_dma_inv_range_harvard()
     * (arch/arm/mm/blockops.c), we allow direct DMA to destination
     * buffer 'vbuf' only when it's aligned at CPU's cache line boundary.
     */
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)) && defined(PXA27X_DCACHE_BUG_WORKAROUND)
    if ((u32)vbuf & 31)
        direct_dma = FALSE;
#endif

    /* Set source physical address for DMA transfer (this would be DiskOnChip's
     * I/O register which is located at offset 'off' from the DiskOnChip's
     * base address as specified by tffs_addr[0]).
     */
    DSADR(chan) = tffs_addr[0] + off;

    /* Set target physical address for DMA transfer.
     * This would be physical address of intermediate DMA buffer (for
     * DMA modes 1/2/3), or physical address of the destination buffer
     * (for DMA modes 5/6/7).
     */
    if (direct_dma == TRUE) /* do DMA transfer directly into 'vbuf' */
    { 
        if (virt_addr_valid((unsigned long)vbuf)) /* 'vbuf' is in low memory */
        {
            DTADR(chan) = __pa (vbuf);
        }
        else                                      /* 'vbuf' is in high memory */
        {
            char * k_vbuf = (char *) page_address(vmalloc_to_page(vbuf)) +
                                          ((unsigned long)vbuf & (PAGE_SIZE - 1));
            if (k_vbuf == NULL)
            { 
                PrintkError ("error, DMA requested on high memory page");
                return;
            }

            DTADR(chan) = __pa (k_vbuf);

            /* andrayk Nov 22 2006: possible bug. Routine consistent_alloc()
             * (arch/arm/mm/consistent.c) applies __pa() macro to it's 'vbuf' 
             * argument (see below). This might cause CPU exception.
             */
        }

        /* Write (if dirty) and purge contents of 'vbuf' from CPU cache(s) */
        /* Possible options:
         *   consistent_sync(vbuf, bytes, DMA_FROM_DEVICE);            ARM/SH
         *   dma_cache_inv((unsigned long)vbuf, (unsigned long)bytes); MIPS/PPC/x86
         */

#ifdef PXA27X_DCACHE_BUG_WORKAROUND

        /* Routine blk_dma_inv_range_harvard() (arch/arm/mm/blockops.c) appears
         * to be buggy: it seems to constantly shoot past the end of the address
         * range [vbuf..vbuf+bytes] by 32 bytes. It also appears to write some
         * bogus data into this RAM address range while invalidating it in D-cache. 
         */
        consistent_sync (vbuf, bytes - 1, DMA_FROM_DEVICE);
#else
        consistent_sync (vbuf, bytes, DMA_FROM_DEVICE);
#endif
    }
    else /* transfer into intermediate DMA buffer */
    {
        DTADR(chan) = tffsInfo.dma_buf_paddr;
    }

    /* specify parameters of DMA transfer */
    tmp = DCMD_INCTRGADDR |     /* increment target address */
          DCMD_WIDTH2     |     /* 16-bit data path */
       /* DCMD_ENDIRQEN   | */  /* don't generate interrupt at the end of transfer */
          bytes;

    if ((bytes & 31) == 0)
        tmp |= DCMD_BURST32;    /* 32-bytes burst */

    DCMD(chan) = tmp;

    /* clear all status bits, and start DMA transfer */
    DCSR(chan) = (DCSR_RUN | DCSR_NODESC | DCSR_ENDINTR | DCSR_STOPSTATE | DCSR_BUSERR);

    /* poll DMA channel's status register for end of DMA transfer */
    for (waiting = 100000; waiting > 0; waiting--) /* hope this is long enough ... */
    {
        /* read Channel Status Register to find out status of DMA transfer */
        tmp = DCSR (chan);

        if (tmp & (DCSR_STOPSTATE | DCSR_BUSERR))
        {
            if ((tmp & DCSR_BUSERR) == 0)
            {
                /* DMA transfer completed, move data from the intermediate DMA
                 * buffer to the destination buffer (in case it was used).
                 */
                if (direct_dma == FALSE)
                    memcpy (vbuf, tffsInfo.dma_buf_vptr, bytes);
            }
            else
            { 
                PrintkError ("Bus error, DMA transfer failed");
                /* andrayk Mar 24 2006: DMA transfer failed, find way to let MTD know this */
            }
            return;
        }
    }

    PrintkError ("DMA transfer timed out");
}




/*-----------------------------------------------------------------------*
 *                                                                       *
 *                    _ _ d m a s w _ b w r i t e                        *
 *                                                                       *
 * Transfer data from RAM buffer to DiskOnChip's I/O reguster.           *
 * Example implementation for Intel PXA27x (Mainstone) board.            *
 *                                                                       *
 * Parameters:                                                           *
 *      off            offset of DiskOnChip's I/O register from the base *
 *                     of DiskOnChip window                              *
 *      vbuf           virtual address of RAM buffer                     *
 *      bytes          bytes to transfer                                 *
 *                                                                       *
 *-----------------------------------------------------------------------*/
static
void __dmasw_bwrite ( int            off,
                      void         * vbuf, 
                      unsigned int   bytes )
{
    int           chan = tffsInfo.channel;
    u_int         tmp;
    register int  waiting;

    profiling[3]++;

    /* Set target physical address for DMA transfer (this would be DiskOnChip's
     * I/O register which is located at offset 'off' from the DiskOnChip's
     * base address as specified by tffs_addr[0]).
     */
    DTADR(chan) = tffs_addr[0] + off;

    /* set source physical address for DMA transfer */

    if (tffs_dma_mode & 4) /* do DMA transfer directly from 'vbuf' */
    {
        /* Write (if dirty) contents of 'vbuf' from CPU D-cache to RAM.
         * Possible options:
         *   consistent_sync(vbuf, bytes, DMA_TO_DEVICE);                ARM/SH
         *   dma_cache_wback((unsigned long)vbuf, (unsigned long)bytes); MIPS/PPC/x86
         */
        consistent_sync (vbuf, bytes, DMA_TO_DEVICE);

        if (virt_addr_valid((unsigned long)vbuf)) /* 'vbuf' is in low memory */
        {
            DSADR(chan) = __pa (vbuf);
        }
        else                                      /* 'vbuf' is in high memory */
        {
            char * k_vbuf = (char *) page_address(vmalloc_to_page(vbuf)) +
                                          ((unsigned long)vbuf & (PAGE_SIZE - 1));
            if (k_vbuf == NULL)
            { 
                PrintkError ("error, DMA requested on high memory page");
                return;
            }

            DSADR(chan) = __pa (k_vbuf);
        }
    }
    else                   /* use intermediate DMA buffer */
    {
        /* move data from RAM buffer to the intermediate DMA buffer */
        memcpy (tffsInfo.dma_buf_vptr, vbuf, bytes);

        DSADR(chan) = tffsInfo.dma_buf_paddr;
    }

    /* specify parameters of DMA transfer */
    tmp = DCMD_INCSRCADDR |     /* increment source address */
          DCMD_WIDTH2     |     /* 16-bit data path */
       /* DCMD_ENDIRQEN   | */  /* don't generate interrupt at the end of transfer */
          bytes;

    if ((bytes & 31) == 0)
        tmp |= DCMD_BURST32;    /* 32-bytes burst */

    DCMD(chan) = tmp;

    /* clear all status bits, and start DMA transfer */
    DCSR(chan) = (DCSR_RUN | DCSR_NODESC | DCSR_ENDINTR | DCSR_STOPSTATE | DCSR_BUSERR);

    /* poll DMA channel's status register for end of DMA transfer */
    for (waiting = 100000; waiting > 0; waiting--) /* hope this is long enough ... */
    {
        /* read Channel Status Register to find out status of DMA transfer */
        tmp = DCSR (chan);

        if (tmp & (DCSR_STOPSTATE | DCSR_BUSERR))
        {
            if (tmp & DCSR_BUSERR)
            { 
                PrintkError ("Bus error, DMA transfer failed");
                /* andrayk Mar 24 2006: DMA transfer failed, find way to let MTD know this */
            }
            else
            {
                /* DMA transfer successful */ 
            }
            return;
        }
    }

    PrintkError ("DMA transfer timed out");
}




/*-----------------------------------------------------------------------*
 *                                                                       *
 *                     t f f s _ d m a _ i n t r                         *
 *                                                                       *
 * Handle DMA interrupt on Intel PXA27x (Mainstone) board.               *
 *                                                                       *
 * Parameters:                                                           *
 *      dma           interrupting DMA channel                           *
 *      dummy         not used                                           *
 *      regs          not used                                           *
 *                                                                       *
 *-----------------------------------------------------------------------*/
static 
void tffs_dma_intr ( int              dma, 
                     void           * dummy,    /* not used */
                     struct pt_regs * regs )    /* not used */
{
    register u_int tmp;

    profiling[4]++;

    /* check if interrupt came from our DMA channel */
    if (dma == tffsInfo.channel)
    {
        /* check if DMA transfer was successful */
        if ((tmp = DCSR(dma)) & DCSR_BUSERR)
        { 
            /* andrayk Mar 24 2006: DMA transfer failed, find way to let MTD know this */
            PrintkError ("Bus error, DMA transfer failed");
        }

        /* clear DMA interrupt from this channel */
        DCSR(dma) = (tmp | (DCSR_BUSERR | DCSR_ENDINTR));
    }
    else
        { PrintkError ("received DMA interrupt on wrong channel"); }
}




#elif defined(CONFIG_OMAP_H2) || defined(CONFIG_MACH_OMAP_H2)

/*_____________________________________________________________________________
 |                                                                            |
 |                                                                            |
 |                Texas Instruments OMAP-1610 (H2) board                      |
 |                                                                            |
 |____________________________________________________________________________| 
*/

# include <asm/arch/hardware.h>
# include <asm/arch/dma.h>

    /*
    
    EMIFS: Extended Memory Interface Slow. This 16-bit wide bus can interface
    with and handle all transactions to flash memory, ROM, asynchronous
    memories, and synchronous burst flash.
    The EMIFS can support 16-bit interface width only.
    TC: traffic controller
    The EMIFS supports 8-,16-, or 32-bit asynchronous and synchronous
    read, 4- x 32-bit synchronous burst read and 8-, 16-, or 32-bit
    asynchronous write.
    
    EMIFS_PRIOR		EMIFS LRU priority register				FFFECC04
    EMIFS_CONFIG	EMIFS configuration register			FFFECC0C
    EMIFS_CCS2		EMIFS chip-select configuration CS2		FFFECC18
    	7:4		RDWST			Controls the wait states cycle number for asynchronous read operation and the initial idle time for asynchronous read page mode and synchronous read mode.
    	15:12	PGWST/WELEN		Controls the wait states cycle number between accesses in a page for asynchronous page mode. Controls the WE pulse length during a write access. When PGWSTEN is 0, this bit specifies both PGWST/WELEN. When PGWSTEN is 1, this bit specifies only WELEN
    EMIFS_PTOR1		EMIFS dynamic priority time-out 1		FFFECC28
    EMIFS_PTOR2		EMIFS dynamic priority time-out 2		FFFECC2C
    EMIFS_PTOR3		EMIFS dynamic priority time-out 3		FFFECC30
    EMIFS_DWS		EMIFS dynamic wait states				FFFECC40
    EMIFS_AADDR		EMIFS abort address						FFFECC44
    EMIFS_ATYPER	EMIFS abort type						FFFECC48
    EMIFS_ATOR		EMIFS abort time-out					FFFECC4C
    EMIFS_ACS2		Advanced EMIFS chip-select configuration nCS2	FFFECC58
    
    
    EMIFS_CS2_CONFIG 0xFFFECC18 
    	BSP sets the reg val 0xf800f22a, but the val after kernel starts is 0xff80fff3
    
    */




/*
 * static routines for OMAP1610
 */

static void tffs_dma_init (int resume);
static void tffs_dma_release (void);



/*
 * static vars
 */

static dma_regs_t * dma_regs = NULL;



/*-----------------------------------------------------------------------*
 *                                                                       *
 *                    t f f s a r c h _ i n i t                          *
 *                                                                       *
 * This routine is called during module initialization time with 'resume'*
 * argument set to FALSE, and from Power Manager's 'resume' routine with *
 * 'resume' argument set to TRUE. If 'resume' is FALSE, this routine     *
 * allocates and initializes all required board resources; if 'resume'   *
 * is TRUE, this routine does minimal required re-initialization of      *
 * these resources after power suspend.                                  *
 *                                                                       *
 * Parameters:                                                           *
 *      suspend     FALSE (module initialization) or TRUE (power resume) *
 *                                                                       *
 * Returns:                                                              *
 *      always '1' (success)                                             *
 *                                                                       *
 *-----------------------------------------------------------------------*/

unsigned char tffsarch_init (int resume)
{
    register unsigned short tmp16;
    register unsigned long  tmp32;

    /* Configure Chip Select that DiskOnChip is connected to.
     * 
     * NOTE. Code below configures Chip Select to set duration of
     *       DiskOnChip access cycle to 70 nanosecond, which is
     *       appropriate for the case when DiskOnChip is connected to
     *       OMAP1610 board via M-Systems' adapters. If you solder
     *       DiskOnChip into your board, consider changing Chip Select
     *       settings to significantly shorten DiskOnChip acecss cycle.
     *       This should have significant positive efefct on
     *       DiskOnChip's 'read' performance.
     */
    tmp32 = (1 << 27) | /* wait states between accesses in a page for async. page mode */
            (1 << 23) | /* idle cycles for bus turn around */
            (0 << 16) | /* asynchronous read */  
            (1 << 12) | /* WE length */
            (0 <<  8) | /* wait states in write operation */
            (0 <<  4) | /* wait states in read operation */
                   2;   /* REF_CLK = TC_CK / 2 */
    
#define OMAP1610_H2_CS  2 /* chip select that DiskOnChip is connected to */

    outl (tmp32, (EMIFS_CS0_CONFIG + (OMAP1610_H2_CS * sizeof(unsigned long))));

    if (tffs_irq >= 0)
    {
        /* Configure GPIO-9 as input. This is actually a default state
         * after reset, but just to be sure ...
         */
        tmp16 = inw (GPIO_DIRECTION_REG);
        outw ((tmp16 | (1 << (tffs_irq - IH_GPIO_BASE))), GPIO_DIRECTION_REG);
      
        /* configure DiskOnChip IRQ as GPIO-9, disable PullDown */
        outl( (inl(PULL_DWN_CTRL_2) | (1 << 23)), PULL_DWN_CTRL_2);

        /* select function W8 pin as GPIO-9 */
        outl( (inl(FUNC_MUX_CTRL_B) & 0xFF1FFFFF), FUNC_MUX_CTRL_B);
    }

# if 0
	volatile unsigned long*reg;
	unsigned long ulTemp;

	/* configure DMAREQ# as MPUIO4, disable PullDown */
	ulTemp  = *(volatile unsigned long*) 0xFFFE1044;
	ulTemp  |= 0x00002000; /* 0x1<<13; */
	*(volatile unsigned long *)0xFFFE1044 = (unsigned long)ulTemp;

	/* select function DMA_REQ tffsInfo.channel 6 */
	ulTemp  = *(volatile unsigned long*) 0xFFFE1020;
	ulTemp  |= 0x8000; /* 0x1<<15; */
	*(volatile unsigned long *)0xFFFE1020 = (unsigned long)ulTemp;
	
	/* configure mDoc IRQ# as GPIO-9, disable PullDown */
	/* TH: For H2 Platform */
	ulTemp  = *(volatile unsigned long*) 0xFFFE1048;
	ulTemp  |= 0x800000; /* 0x1<<23; */
	*(volatile unsigned long *)0xFFFE1048 = (unsigned long)ulTemp;

	/* select function W8 pin as GPIO-9 */
	ulTemp  = *(volatile unsigned long*) (0xFFFE1030);
	ulTemp  &= 0xFF1FFFFF;
	*(volatile unsigned long *)(0xFFFE1030) = (unsigned long)ulTemp;

	reg=(unsigned long*)EMIFS_CONFIG_REG;
	PrintkInfo("EMIFS_CONFIG_REG=0x%lx",*reg);
	*reg=0x0c;
	reg=(unsigned long*)EMIFS_CONFIG_REG;
	PrintkInfo("EMIFS_CONFIG_REG=0x%lx",*reg);

	reg=(unsigned long*)EMIFS_CS2_CONFIG2;
	PrintkInfo("EMIFS_CS2_CONFIG2=0x%lx",*reg);

	reg=(unsigned long*)EMIFS_CS2_CONFIG;
	PrintkInfo("EMIFS_CS2_CONFIG=0x%lx",*reg);
	*reg=0x8070;
	reg=(unsigned long*)EMIFS_CS2_CONFIG;
	PrintkInfo("EMIFS_CS2_CONFIG=0x%lx",*reg);
# endif

    /* if DMA is requested, configure it */
    if (tffs_dma_mode > 0)
    {
        tffs_dma_init (resume);
    }

    return 1;
}



void TffsHWRelease(void)
{
    /* if DMA was actually used, release all DMA resources */
    if (tffs_dma_mode > 0)
    {
        tffs_dma_release ();
    }
}


/*-----------------------------------------------------------------------*
 *                                                                       *
 *                    t f f s _ d m a _ i n i t                          *
 *                                                                       *
 * This routine is called during module initialization time with 'resume'*
 * argument set to FALSE, and from Power Manager's 'resume' routine with *
 * 'resume' argument set to TRUE. If 'resume' is FALSE, this routine     *
 * allocates and initializes all required DMA resources; if 'resume' is  *
 * TRUE, this routine does minimal required re-initialization of DMA     *
 * channel after power suspend.                                          *
 *                                                                       *
 * Parameters:                                                           *
 *      resume     FALSE (module initialization) or TRUE (power resume)  *
 *                                                                       *
 *-----------------------------------------------------------------------*/
static 
void tffs_dma_init (int resume)
{
    u32 tmp;

    if (resume == FALSE) 
    {
        /* We use single DMA channel for both 'read' and 'write' operations,
         * so we allocate it here. We don't use DMA interrupts, so we won't
         * need DMA callback.
         */
        tffsInfo.channel = 
            omap_request_dma (eDMANotSync, /* not connected to any DMA line  */
                              TFFS_DEVICE_NAME,
                              NULL,        /* no callback when DMA completes */
                              NULL,        /* data to pass to DMA callback   */
                              &dma_regs);
        if ((tffsInfo.channel != 0) || (dma_regs == NULL))
        {
            /* failed to allocate DMA channel, won't use DMA */
            tffs_dma_mode = 0;

            PrintkError ("can't get DMA chan");
            return;
        }
    }

    /* Logical Channel Control Register.
     * Specify peripheral DMA transfer.
     */
    dma_regs->lch_ctrl = LCH_TYPE_P;

    /* Channel Element Number Register.
     * Specify single element (16-bit word) per DMA frame.
     */
    dma_regs->cen = 1;

    /* Channel Control Register 2.
     * Specify block synchronization.
     */
    dma_regs->ccr2 = (1 << 2);

    /* Channel Interrupt Control Register.
     * Enable time-out interrupt, disable all others.
     */
    dma_regs->cicr = /* (1 << 5) | */   /* end-of-block interrupt */
                     1;                 /* timeout interrupt */

    /* read Channel Status register to clear it */
    tmp = dma_regs->csr;

    if (resume == FALSE)
    {
        tffsInfo.dma_buf_vptr = NULL;
        tffsInfo.dma_buf_size = 0;

        if ((tffs_dma_mode >= 1) && (tffs_dma_mode <= 3))
        {
            /* allocate intermediate DMA buffer (we assume one MMU page will be enough) */
            tffsInfo.dma_buf_size = PAGE_SIZE;

            tffsInfo.dma_buf_vptr = 
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
                consistent_alloc ((GFP_KERNEL | GFP_DMA), tffsInfo.dma_buf_size,
                                         &tffsInfo.dma_buf_paddr);
#else
                dma_alloc_coherent (NULL, tffsInfo.dma_buf_size, 
                                         &tffsInfo.dma_buf_paddr, (GFP_KERNEL | GFP_DMA));
#endif
            if (tffsInfo.dma_buf_vptr == NULL)
            {
                /* failed to allocate intermediate DMA buffer, won't use DMA */
                tffs_dma_mode = 0;

                omap_free_dma (dma_regs);
                dma_regs = NULL;

                tffsInfo.dma_buf_size = 0;
                PrintkError ("can't alloc DMA buffer");
            }
        }
    }
}




/*-----------------------------------------------------------------------*
 *                                                                       *
 *                    t f f s _ d m a _ r e l e a s e                    *
 *                                                                       *
 * Release all previously allocated DMA resources (channel, intermediate *
 * DMA buffer etc.). For OMAP1610 (H2) board.                            *
 *                                                                       *
 *-----------------------------------------------------------------------*/
static 
void tffs_dma_release(void)
{
    /* if previously was allocated, release DMA channel */
    if (tffsInfo.channel == 0)
    {
        omap_free_dma (dma_regs);
        dma_regs = NULL;

        tffsInfo.channel = -1;
    }

    /* if previously was allocated, release intermediate DMA buffer */
    if (tffsInfo.dma_buf_vptr != NULL)
    {
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
        consistent_free (tffsInfo.dma_buf_vptr, PAGE_SIZE, tffsInfo.dma_buf_paddr);
#else
        dma_free_coherent (NULL, PAGE_SIZE, tffsInfo.dma_buf_vptr, tffsInfo.dma_buf_paddr);
#endif
        tffsInfo.dma_buf_vptr = NULL;
    }
}




/*-----------------------------------------------------------------------*
 *                                                                       *
 *                    _ _ d m a s w _ b r e a d                          *
 *                                                                       *
 * Transfer data from DiskOnChip's I/O reguster to RAM buffer.           *
 * Example implementation For Texas Instruments OMAP1610 H2 board.       *
 *                                                                       *
 * Parameters:                                                           *
 *      off            offset of DiskOnChip's I/O register from the base *
 *                     of DiskOnChip window                              *
 *      vbuf           virtual address of RAM buffer                     *
 *      bytes          bytes to transfer                                 *
 *                                                                       *
 *-----------------------------------------------------------------------*/
static
void __dmasw_bread ( int            off,
                     void         * vbuf, 
                     unsigned int   bytes )
{
    register u32  tmp;
    register int  waiting;

    profiling[2]++;

    /* Channel Source/Destination Parameters.
     * Specify 16-bit port width, EMIFS source, allow source packing,
     * 4-words burst allowed for source, EMIFF destination. allow
     * destination packing, 4-words burst allowed for destination.
     */
    dma_regs->csdp =    (BURST_4 << 14)   |    /* dest.: use 4-word burst      */
                        DCSDP_DST_PACK    |    /* pack dest.                   */
                        (PORT_EMIFF << 9) |    /* dest. is EMIFF (RAM)         */
                        (BURST_4 <<  7)   |    /* source: use 4-word burst     */
                        DCSDP_SRC_PACK    |    /* pack source                  */
                        (PORT_EMIFS << 2) |    /* source is EMIFS (DiskOnChip) */
                        DATA_TYPE_S16;         /* 16-bit data path             */

    /* Channel Control Register.
     * Specify no synchronization, no OMAP 3.0/3.1 compatibility, no
     * frame synchronization, high priority, constant source address,
     * post-increment destination address.
     */ 
    dma_regs->ccr = (AMODE_POST_INC << 14) | /* dest (RAM): post-increment        */
                    (AMODE_CONST << 12)    | /* source (DiskOnChip) : const addr. */
                     DCCR_N31COMP          | /* no OMAP 3.0/3.1 compatibility     */
                     DCCR_PRIO;              /* high priority */ 

    /* Channel Frame Number Register. 
     * We assume that frame consists of single element, and element
     * consists of single 16-bit word.
     */
    dma_regs->cfn = (bytes / sizeof(u16));

    /* Channel Source Start Address Registers. This would be DiskOnChip's
     * I/O register which is located at offset 'off' from the DiskOnChip's
     * base address as specified by tffs_addr[0]).
     */
    tmp = tffs_addr[0] + off;
    dma_regs->cssa_u = (tmp >> 16);
    dma_regs->cssa_l = (tmp & 0xffff);

    /* Channel Destination Start Address Registers */
    if (tffs_dma_mode & 4) /* do DMA transfer directly into 'vbuf' */
    { 
        if (virt_addr_valid((unsigned long)vbuf)) /* 'vbuf' is in low memory */
        {
            tmp = __pa (vbuf);
        }
        else                                      /* 'vbuf' is in high memory */
        {
            char * k_vbuf = (char *) page_address(vmalloc_to_page(vbuf)) +
                                          ((unsigned long)vbuf & (PAGE_SIZE - 1));
            tmp = __pa (k_vbuf);
        }

        /* write (if dirty) and purge contents of 'vbuf' from CPU cache(s) */
#if 1   
        /* D-cache only. Possible options:
         *   flush_dcache_range((unsigned long)vbuf, (unsigned long)(vbuf + bytes));    ARM/PPC
         *   consistent_sync(vbuf, bytes, PCI_DMA_BIDIRECTIONAL);                       ARM/PPC
         *   dma_cache_wback_inv((unsigned long)vbuf, (unsigned long)bytes);            MIPS/PPC/SH/x86
         */
        flush_dcache_range ((unsigned long)vbuf, (unsigned long)(vbuf + bytes));
#else   
        /* both D- and I-caches */
        cpu_cache_clean_invalidate_range ((unsigned long)vbuf, (unsigned long)(vbuf + bytes), 1);
#endif
    }
    else /* transfer into intermediate DMA buffer */
    {
        tmp = tffsInfo.dma_buf_paddr;
    }
    dma_regs->cdsa_u = (tmp >> 16);
    dma_regs->cdsa_l = (tmp & 0xffff);

    /* start DMA channel */
    dma_regs->ccr |= DCCR_EN;

    /* wait for DMA transfer to finish */
    for (waiting = 100000; waiting > 0; waiting--) /* hope this is long enough ... */
    {
        if (((tmp = dma_regs->ccr) & DCCR_EN) == 0)
        {
            /* read Channel Status Register to clear it */
            if (((tmp = dma_regs->csr) & DCSR_ERROR) == 0)
            {
                /* DMA transfer completed, move data from the intermediate DMA
                 * buffer to the destination buffer (in case it was used).
                 */
                if ((tffs_dma_mode & 4) == 0)
                    memcpy (vbuf, tffsInfo.dma_buf_vptr, bytes);
            }
            else /* andrayk June 13 2006: DMA transfer failed, find way to let MTD know this */
            { 
                PrintkError ("Bus error, DMA transfer failed");
            }
            return;
        }
    }

    PrintkError ("DMA transfer timed out");
}




/*-----------------------------------------------------------------------*
 *                                                                       *
 *                    _ _ d m a s w _ b w r i t e                        *
 *                                                                       *
 * Transfer data from RAM buffer to DiskOnChip's I/O reguster.           *
 * Example implementation For Texas Instruments OMAP1610 H2 board.       *
 *                                                                       *
 * Parameters:                                                           *
 *      off            offset of DiskOnChip's I/O register from the base *
 *                     of DiskOnChip window                              *
 *      vbuf           virtual address of RAM buffer                     *
 *      bytes          bytes to transfer                                 *
 *                                                                       *
 *-----------------------------------------------------------------------*/
static
void __dmasw_bwrite ( int            off,
                      void         * vbuf, 
                      unsigned int   bytes )
{
    register u32  tmp;
    register int  waiting;

    profiling[3]++;

    /* Channel Source/Destination Parameters.
     * Specify 16-bit port width, EMIFF source, allow source packing,
     * 4-words burst allowed for source, EMIFS destination. allow
     * destination packing, 4-words burst allowed for destination.
     */
    dma_regs->csdp =    (BURST_4 << 14)   |    /* dest.: use 4-word burst     */
                        DCSDP_DST_PACK    |    /* pack dest.                  */
                        (PORT_EMIFS << 9) |    /* dest. is EMIFS (DiskOnChip) */
                        (BURST_4 <<  7)   |    /* source: use 4-word burt     */
                        DCSDP_SRC_PACK    |    /* pack source                 */
                        (PORT_EMIFF << 2) |    /* source is EMIFF (RAM)       */
                        DATA_TYPE_S16;         /* 16-bit data path            */

    /* Channel Control Register.
     * Specify no synchronization, no OMAP 3.0/3.1 compatibility, no
     * frame synchronization, high priority, constant dest. address,
     * post-increment source address.
     */ 
    dma_regs->ccr = (AMODE_CONST << 14)    | /* dest. (DiskOnChip): const addr. */
                    (AMODE_POST_INC << 12) | /* source (RAM): post-increment    */
                     DCCR_N31COMP          | /* no OMAP 3.1/3.0 compatibility   */
                     DCCR_PRIO;              /* high priority */

    /* Channel Frame Number Register.
     * We assume that frame consists of single elemnt, and element consists
     * of single 16-bit word.
     */
    dma_regs->cfn = (bytes / sizeof(u16));

    /* Channel Source Start Address Registers */
    if (tffs_dma_mode & 4) /* do DMA transfer directly from 'vbuf' */
    {
        /* Write (if dirty) contents of 'vbuf' from CPU D-cache to RAM.
         * Possible options:
         *   clean_dcache_range((unsigned long)vbuf, (unsigned long)(vbuf + bytes));    ARM/PPC
         *   consistent_sync(vbuf, bytes, PCI_DMA_TODEVICE);                            ARM/PPC
         *   dma_cache_wback((unsigned long)vbuf, (unsigned long)bytes);                MIPS/PPC/SH/x86
         */
        clean_dcache_range ((unsigned long)vbuf, (unsigned long)(vbuf + bytes));

        if (virt_addr_valid((unsigned long)vbuf)) /* 'vbuf' is in low memory */
        {
            tmp = __pa (vbuf);
        }
        else                                      /* 'vbuf' is in high memory */
        {
            char * k_vbuf = (char *) page_address(vmalloc_to_page(vbuf)) +
                                          ((unsigned long)vbuf & (PAGE_SIZE - 1));
            tmp = __pa (k_vbuf);
        }
    }
    else                   /* use intermediate DMA buffer */
    {
        /* move data from RAM buffer to the intermediate DMA buffer */
        memcpy (tffsInfo.dma_buf_vptr, vbuf, bytes);

        tmp = tffsInfo.dma_buf_paddr;
    }
    dma_regs->cssa_u = (tmp >> 16);
    dma_regs->cssa_l = (tmp & 0xffff);

    /* Channel Destination Start Address Regist.This would be DiskOnChip's
     * I/O register which is located at offset 'off' from the DiskOnChip's
     * base address as specified by tffs_addr[0]).
     */
    tmp = tffs_addr[0] + off;
    dma_regs->cdsa_u = (tmp >> 16);
    dma_regs->cdsa_l = (tmp & 0xffff);

    /* start DMA channel */
    dma_regs->ccr |= DCCR_EN;

    /* poll DMA channel's status register for end of DMA transfer */
    for (waiting = 100000; waiting > 0; waiting--) /* hope this is long enough ... */
    {
        if (((tmp = dma_regs->ccr) & DCCR_EN) == 0)
        {
            if ((tmp = dma_regs->csr) & DCSR_ERROR) /* andrayk June 14 2006: DMA transfer failed, find way to let MTD know this */
            { 
                PrintkError ("Bus error, DMA transfer failed");
            }
            return;
        }
    }

    PrintkError ("DMA transfer timed out");
}



#elif defined(CONFIG_ARCH_OMAP24XX)

/*_____________________________________________________________________________
 |                                                                            |
 |                                                                            |
 |                Texas Instruments OMAP-2420 (H4) board                      |
 |                                                                            |
 |____________________________________________________________________________| 
*/

# include <asm/arch/hardware.h>
# include <asm/arch/dma.h>
/* # include <asm/arch/pm_prcm.h> */  /* conflicts with <asm/arch/hardware.h> */
# include <asm/arch/gpio.h>
# include <asm/arch/mux.h>

/* Routine blk_dma_inv_range_harvard() (arch/arm/mm/blockops.c) appears
 * to be buggy: it seems to constantly shoot past the end of the address
 * range [vbuf..vbuf+bytes] by 32 bytes. It also appears to write some
 * bogus data into this RAM address range while invalidating it in D-cache. 
 */
# define OMAP2420_DCACHE_BUG_WORKAROUND


# ifndef OMAP_DMA4_CCR_EN
#   define OMAP_DMA4_CCR_EN   (1 << 7)
# endif

# define GPMC_CONFIG1_CS2  0xC0                         /* phys. addr. 0x6800A0C0 */
# define GPMC_CONFIG2_CS2  (GPMC_CONFIG1_CS2 +  0x4)    /* phys. addr. 0x6800A0C4 */
# define GPMC_CONFIG3_CS2  (GPMC_CONFIG1_CS2 +  0x8)    /* phys. addr. 0x6800A0C8 */
# define GPMC_CONFIG4_CS2  (GPMC_CONFIG1_CS2 +  0xC)    /* phys. addr. 0x6800A0CC */
# define GPMC_CONFIG5_CS2  (GPMC_CONFIG1_CS2 + 0x10)    /* phys. addr. 0x6800A0D0 */
# define GPMC_CONFIG6_CS2  (GPMC_CONFIG1_CS2 + 0x14)    /* phys. addr. 0x6800A0D4 */
# define GPMC_CONFIG7_CS2  (GPMC_CONFIG1_CS2 + 0x18)    /* phys. addr. 0x6800A0D8 */

# ifndef OMAP2420_GPIO_SET_IRQENABLE1
#  define OMAP2420_GPIO_SET_IRQENABLE1 0x0064
# endif
# ifndef OMAP2420_GPIO_CLEAR_IRQENABLE1
#  define OMAP2420_GPIO_CLEAR_IRQENABLE1 0x0060
# endif
# ifndef OMAP2420_GPIO_IRQSTATUS1
#  define OMAP2420_GPIO_IRQSTATUS1 0x0018
# endif
# ifndef OMAP2420_GPMC_BASE
#  define OMAP2420_GPMC_BASE OMAP24XX_GPMC_BASE
# endif

/* layout of DMA channel's registers */
typedef struct {
    volatile u32  ccr;     /* Channel Control Register                */
    volatile u32  clnk;    /* Channel Link Control                    */
    volatile u32  cicr;    /* Channel Interrupt Control Register      */
    volatile u32  csr;     /* Channel Status Register                 */
    volatile u32  csdp;    /* Channel Source / Destination Parameters */
    volatile u32  cen;     /* Channel Element Number                  */
    volatile u32  cfn;     /* Channel Frame Number                    */
    volatile u32  cssa;    /* Channel Source Start Address            */
    volatile u32  cdsa;    /* Channel Destination Start Address       */
    volatile u32  csei;    /* Channel Source Element Index            */
    volatile u32  csfi;    /* Channel Source Frame Index              */
    volatile u32  cdei;    /* Channel Destination Element Index       */
    volatile u32  cdfi;    /* Channel Destination Frame Index         */
    volatile u32  csac;    /* Channel Source Address Counter          */
    volatile u32  cdac;    /* Channel Destination Address Counter     */
    volatile u32  ccen;    /* Channel Current Element Number          */
    volatile u32  ccfn;    /* Channel Current Frame Number            */
} __dma_reg_t;


/*
 * static routines for OMAP2420
 */
static void tffs_dma_init (int resume);
static void tffs_dma_release (void);


/*
 * static vars
 */
static __dma_reg_t * dma_regs_vptr = NULL;



void omap24xx_enable_gpio_irq (int gpio_irq, int enable)
{
    register u32 gpio_line = gpio_irq - IH_GPIO_BASE; 
    register u32 bit       = OMAP_GPIO_BIT(gpio_line);
    register u32 base_reg  = OMAP_GPIO_BASE_REG(gpio_line);
    unsigned int val       = 0;

    if (enable == TRUE)
    {
        writel (bit, (base_reg + OMAP2420_GPIO_SET_IRQENABLE1));
        __asm__ __volatile__("mcr p15, 0, %0, c7, c10, 4"::"r"(val)); /* memory barrier */

        omap_set_gpio_edge_ctrl (gpio_line, OMAP_GPIO_LEVEL_LOW);
    }
    else
    {
        omap_set_gpio_edge_ctrl (gpio_line, 0);

        writel (bit, (base_reg + OMAP2420_GPIO_CLEAR_IRQENABLE1));
        __asm__ __volatile__("mcr p15, 0, %0, c7, c10, 4"::"r"(val)); /* memory barrier */

        writel (bit, (base_reg + OMAP2420_GPIO_IRQSTATUS1));
        __asm__ __volatile__("mcr p15, 0, %0, c7, c10, 4"::"r"(val)); /* memory barrier */
    }
}




/*-----------------------------------------------------------------------*
 *                                                                       *
 *                    t f f s a r c h _ i n i t                          *
 *                                                                       *
 * This routine is called during module initialization time with 'resume'*
 * argument set to FALSE, and from Power Manager's 'resume' routine with *
 * 'resume' argument set to TRUE. If 'resume' is FALSE, this routine     *
 * allocates and initializes all required board resources; if 'resume'   *
 * is TRUE, this routine does minimal required re-initialization of      *
 * these resources after power suspend. For OMAP2420 (H4) board.         *
 *                                                                       *
 * Parameters:                                                           *
 *      suspend     FALSE (module initialization) or TRUE (power resume) *
 *                                                                       *
 * Returns:                                                              *
 *      always '1' (success)                                             *
 *                                                                       *
 *-----------------------------------------------------------------------*/

unsigned char tffsarch_init (int resume)
{
    register int  tmp;
    char        * vp;

    if ((vp = (char *)ioremap_nocache(OMAP2420_GPMC_BASE, PAGE_SIZE)) == NULL)
    {
        PrintkError ("ioremap_nocache failed");
    }
    else
    {
        /* Configure Chip Select that DiskOnChip is connected to.
         * 
         * NOTE. Code below configures Chip Select to set duration of
         *       DiskOnChip access cycle to 65 nanosecond, which is
         *       appropriate for the case when DiskOnChip is connected to
         *       OMAP2420 board via M-Systems' adapters. If you solder
         *       DiskOnChip into your board, consider changing Chip Select
         *       settings to significantly shorten DiskOnChip acecss cycle.
         *       This should have significant positive efefct on
         *       DiskOnChip's 'read' performance.
         */

        tmp = (0 << 31) |  /* synchronous wrapping burst not supported */
              (0 << 30) |  /* single access */
              (0 << 29) |  /* asynchronous read */
              (0 << 28) |  /* single access */
              (0 << 27) |  /* write asynchronous */
              (0 << 25) |  /* start access time at first rising edge of GPMC.CLK */
              (0 << 23) |  /* burst length 4 words */
              (0 << 22) |  /* wait pin not monitored for read access */
              (0 << 21) |  /* wait pin not monitored for write access */
              (0 << 18) |  /* wait pin is monitored with valid data */
              (0 << 16) |  /* wait input pin is WAIT0 */
              (1 << 12) |  /* 16-bit device */
              (0 << 10) |  /* NOR flash, pSRAM, or asynchronous device */
              (1 <<  9) |  /* address/data multiplexed attached device */
              (0 <<  4) |  /* x1 latencies */
              (3 <<  0);   /* GPMC.CLK = GPMC_FCLK/4 */
        writel (tmp /* 0x00001203 */, vp + GPMC_CONFIG1_CS2);

        tmp = (6 << 16) | /* nCS assertion time for write access */
              (6 <<  8) | /* nCS assertion time for read access */
              (0 <<  7) | /* nCS timing sygnal is not delayed */
              (0 <<  0);  /* nCS assertion time */
        writel (tmp /* 0x00060600 */, vp + GPMC_CONFIG2_CS2);

        tmp = (1 << 16) | /* nADV deassertion time for write access */
              (1 <<  8) | /* nADV deassertion time for read access */
              (0 <<  7) | /* nADV timing control sygnal is not delayed */
              (0 <<  0);  /* nADV assertion time */
        writel (tmp /* 0x00010100 */, vp + GPMC_CONFIG3_CS2);

        tmp = (5 << 24) | /* nWE deassertion time */
              (0 << 23) | /* nWE timing control sygnal is not delayed */
              (2 << 16) | /* nWE assertion time */
              (6 <<  8) | /* nOE deassertion time */
              (0 <<  7) | /* nOE timing control sygnal is not delayed */
              (2 <<  0);  /* nOE assertion time */
        writel (tmp /* 0x05020602 */, vp + GPMC_CONFIG4_CS2);

        tmp = (1 << 24) | /* delay between successive words in multiple access */
              (5 << 16) | /* delay between start-cycle and first data valid */
              (7 <<  8) | /* total write cycle time in GPMC_FCLK cycles */
              (7 <<  0);  /* total read cycle time in GPMC_FCLK cycles */
        writel (tmp /* 0x01050707 */, vp + GPMC_CONFIG5_CS2);

        /* Map Chips Select CS2 at physical address 0x10000000 (as
         * specified by macro TFFS_PHYS_ADDR).
         */
        tmp = 0x00000F50;
        writel (tmp, vp + GPMC_CONFIG7_CS2);

        iounmap (vp);
    }

    if (tffs_irq >= 0)
    {
        register int gpio_line = (tffs_irq - IH_GPIO_BASE);

        if( omap_request_gpio(gpio_line) == 0 )
        {
            register u8 x = readb (OMAP24XX_VA_SYSTEM_CONTROL_BASE + 0x10c);

            /* set GPIO function (mux mode 3), disable pullup/pulldown */
            x &= ~(MUXMODE_MASK);
            x |= 3;
            x &= ~(PULL_UD_ENABLE_MASK);
            writeb (x, OMAP24XX_VA_SYSTEM_CONTROL_BASE + 0x10c);

            /* configure this GPIO line as input */
            omap_set_gpio_direction (gpio_line, TRUE);

            /* configure GPIO interrupt as level-low for this GPIO line */
#if 0
            omap_set_gpio_edge_ctrl (gpio_line, /* OMAP_GPIO_LEVEL_LOW */ OMAP_GPIO_FALLING_EDGE);
#endif
        }
        else
        {
            PrintkWarning ("failed to get IRQ %d", tffs_irq);
            tffs_irq = -1;
        }
    }

    /* if DMA is requested, configure it */
    if (tffs_dma_mode > 0)
    {
        tffs_dma_init (resume);
    }

    return 1;
}




void TffsHWRelease(void)
{
    /* if DMA was actually used, release all DMA resources */
    if (tffs_dma_mode > 0)
    {
        tffs_dma_release ();
    }

    /* if IRQ was actually used, release respective GPIO resources */
    if (tffs_irq >= 0)
    {
        register int gpio_line = (tffs_irq - IH_GPIO_BASE);

        omap_free_gpio (gpio_line);
    }
}




/*-----------------------------------------------------------------------*
 *                                                                       *
 *                    t f f s _ d m a _ i n i t                          *
 *                                                                       *
 * This routine is called during module initialization time with 'resume'*
 * argument set to FALSE, and from Power Manager's 'resume' routine with *
 * 'resume' argument set to TRUE. If 'resume' is FALSE, this routine     *
 * allocates and initializes all required DMA resources; if 'resume' is  *
 * TRUE, this routine does minimal required re-initialization of DMA     *
 * channel after power suspend. For OMAP2420 (H4) board.                 *
 *                                                                       *
 * Parameters:                                                           *
 *      suspend     FALSE (module initialization) or TRUE (power resume) *
 *                                                                       *
 *-----------------------------------------------------------------------*/
static 
void tffs_dma_init (int resume)
{
    register u32  tmp;
    int           rc;

    if (resume == FALSE) 
    {
        /* We use single DMA channel for both 'read' and 'write' operations,
         * so we allocate it here. We don't use DMA interrupts, so we won't
         * need DMA callback.
         */
        rc = omap_request_dma (0,
                               TFFS_DEVICE_NAME,
                               NULL,        /* no callback when DMA completes */
                               NULL,        /* data to pass to DMA callback   */
                               &tffsInfo.channel);

        if ((rc != 0) || (tffsInfo.channel < 0) || 
                         (tffsInfo.channel >= OMAP24XX_LOGICAL_DMA_CH_COUNT))
        {
            /* failed to allocate DMA channel, won't use DMA */
            tffs_dma_mode = 0;

            PrintkError ("can't get DMA chan");
            return;
        }
        else
        { 
            PrintkInfo ("use DMA channel %d", tffsInfo.channel); 

            /* virt. address of DMA channel's register set */
            dma_regs_vptr = (__dma_reg_t *)
                (OMAP_DMA4_BASE + (0x60 * tffsInfo.channel) + 0x80);
        }
    }

    /* Channel Control Register */
    tmp =  (0 << 25) |     /* buffering enabled                    */
           (0 << 24) |     /* destination triggers DMA request     */
           (1 << 23) |     /* prefetch enabled                     */
           (0 << 21) |     /* secure mode disabled                 */
           (0 << 19) |     /* no synchronization                   */
           (1 << 18) |     /* block synchronization                */
           (0 << 17) |     /* transaparent copy disabled           */
           (0 << 16) |     /* constant fill disabled               */
           (1 << 14) |     /* post-increment destination address   */
           (0 << 12) |     /* constant source address              */
           (0 <<  8) |     /* ignore MSUSPEND                      */
           (0 <<  7) |     /* channel disabled (stopped)           */
           (1 <<  6) |     /* high priority channel                */
           (0 <<  5) |     /* no frame synchronization             */
           (0 <<  0);      /* no synchronization (i.e. s/w DMA)    */
    writel (tmp, &dma_regs_vptr->ccr);

    /* Channel Frame Number Register. 
     * We assume that frame consists of single element.
     */
    writel (1, &dma_regs_vptr->cfn);

    /* Channel Interrupt Control Register.
     * Disable all interrupts - we will be polling DMA controller.
     */
    tmp = /* OMAP_DMA_DROP_IRQ  | */ /* synchronization drop     */
          /* OMAP_DMA_BLOCK_IRQ | */ /* end-of-block interrupt   */
          /* (1 << 11)          | */ /* misaligned address error */
          /* (1 <<  8)          | */ /* transaction error        */
          0;
    writel (tmp, &dma_regs_vptr->cicr);

    /* Channel Status Register.
     * Write all-ones to clear any pending interrupts.
     */
    writel (~0, &dma_regs_vptr->csr);

    if (resume == FALSE)
    {
        tffsInfo.dma_buf_vptr = NULL;
        tffsInfo.dma_buf_size = 0;

        if ((tffs_dma_mode >= 1)
#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)) || !defined(OMAP2420_DCACHE_BUG_WORKAROUND)
                                 && (tffs_dma_mode <= 3)
#endif
           )
        {
            /* allocate intermediate DMA buffer (we assume one MMU page will be enough) */
            tffsInfo.dma_buf_size = PAGE_SIZE;

            tffsInfo.dma_buf_vptr = 
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
                consistent_alloc ((GFP_KERNEL | GFP_DMA), tffsInfo.dma_buf_size,
                                       &tffsInfo.dma_buf_paddr);
#else
                dma_alloc_coherent (NULL, tffsInfo.dma_buf_size, 
                                       &tffsInfo.dma_buf_paddr, (GFP_KERNEL | GFP_DMA));
#endif
            if (tffsInfo.dma_buf_vptr == NULL)
            {
                /* failed to allocate intermediate DMA buffer, won't use DMA */
                tffs_dma_mode = 0;

                omap_free_dma (tffsInfo.channel);

                tffsInfo.channel      = -1;
                tffsInfo.dma_buf_size = 0;

                dma_regs_vptr = NULL;

                PrintkError ("can't alloc DMA buffer, won't use DMA");
            }
        }
    }
}




/*-----------------------------------------------------------------------*
 *                                                                       *
 *                    t f f s _ d m a _ r e l e a s e                    *
 *                                                                       *
 * Release all previously allocated DMA resources (channel, intermediate *
 * DMA buffer etc.). For OMAP2420 (H4) board.                            *
 *                                                                       *
 *-----------------------------------------------------------------------*/
static 
void tffs_dma_release(void)
{
    /* if previously was allocated, release DMA channel */
    if (tffsInfo.channel >= 0)
    {
        omap_free_dma (tffsInfo.channel);

        tffsInfo.channel = -1;
        dma_regs_vptr    = NULL;
    }

    /* if previously was allocated, release intermediate DMA buffer */
    if (tffsInfo.dma_buf_vptr != NULL)
    {
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
        consistent_free (tffsInfo.dma_buf_vptr, PAGE_SIZE, tffsInfo.dma_buf_paddr);
#else
        dma_free_coherent (NULL, PAGE_SIZE, tffsInfo.dma_buf_vptr, tffsInfo.dma_buf_paddr);
#endif
        tffsInfo.dma_buf_vptr = NULL;
    }
}




/*-----------------------------------------------------------------------*
 *                                                                       *
 *                    _ _ d m a s w _ b r e a d                          *
 *                                                                       *
 * Transfer data from DiskOnChip's I/O reguster to RAM buffer.           *
 * Example implementation For Texas Instruments OMAP2420 H4 board.       *
 *                                                                       *
 * Parameters:                                                           *
 *      off            offset of DiskOnChip's I/O register from the base *
 *                     of DiskOnChip window                              *
 *      vbuf           virtual address of RAM buffer                     *
 *      bytes          bytes to transfer                                 *
 *                                                                       *
 *-----------------------------------------------------------------------*/
static
void __dmasw_bread ( int            off,
                     void         * vbuf, 
                     unsigned int   bytes )
{
    register __dma_reg_t * regs = dma_regs_vptr;
    register u32           tmp, tmp2;
    register int           waiting;
    register int           direct_dma;

    profiling[2]++;

    direct_dma = ((tffs_dma_mode & 4) ? TRUE : FALSE);

    /* Due to a bug in kernel routine blk_dma_inv_range_harvard()
     * (arch/arm/mm/blockops.c), we allow direct DMA to destination
     * buffer 'vbuf' only when it's aligned at CPU's cache line boundary.
     */
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)) && defined(OMAP2420_DCACHE_BUG_WORKAROUND)
    if ((u32)vbuf & 31)
        direct_dma = FALSE;
#endif

    /* Channel Source/Destination Parameters */
    tmp = (0 << 21) |      /* little-endian source (DiskOnChip)    */
          (1 << 20) |      /* source endianism lock                */
          (0 << 19) |      /* little-endian destination (SDRAM)    */
          (1 << 18) |      /* destination endianism adapt          */
          (1 << 16) |      /* writes (to SDRAM) posted             */
          (3 << 14) |      /* 64-byte burst on destination (SDRAM) */
          (1 << 13) |      /* destination (SDRAM) packed           */
          (3 <<  7) |      /* 64-byte burst on source (DiskOnChip) */
          (1 <<  6) |      /* source (DiskOnChip) packed           */
          (1 <<  0) ;      /* 16-bit data port width               */
   writel (tmp, &regs->csdp);

    /* Channel Control Register */
    tmp2 = (0 << 25) |     /* buffering enabled                    */
           (0 << 24) |     /* destination triggers DMA request     */
           (1 << 23) |     /* prefetch enabled                     */
           (0 << 21) |     /* secure mode disabled                 */
           (0 << 19) |     /* no synchronization                   */
           (1 << 18) |     /* block synchronization                */
           (0 << 17) |     /* transaparent copy disabled           */
           (0 << 16) |     /* constant fill disabled               */
           (1 << 14) |     /* post-increment destination address   */
           (0 << 12) |     /* constant source address              */
           (0 <<  8) |     /* ignore MSUSPEND                      */
           (0 <<  7) |     /* channel disabled (stopped)           */
           (1 <<  6) |     /* high priority channel                */
           (0 <<  5) |     /* no frame synchronization             */
           (0 <<  0);      /* no synchronization (i.e. s/w DMA)    */
    writel (tmp2, &regs->ccr);

    /* Channel Element Number Register */
    writel ((bytes >> 1), &regs->cen);

    /* Channel Source Start Address Register. 
     * This would be DiskOnChip's I/O register which is located at offset
     * 'off' from the DiskOnChip's base address as specified by tffs_addr[0]).
     */
    writel ((tffs_addr[0] + off), &regs->cssa);

    /* Channel Destination Start Address Register.
     * This would be physical address of intermediate DMA buffer (for
     * DMA modes 1 and 3), or physical address of the destination buffer
     * (for DMA modes 5 and 7).
     */
    if (direct_dma == TRUE) /* do DMA transfer directly into 'vbuf' */
    { 
        if (virt_addr_valid((unsigned long)vbuf)) /* 'vbuf' is in low memory */
        {
            tmp = __pa (vbuf);
        }
        else                                      /* 'vbuf' is in high memory */
        {
            char * k_vbuf = (char *) page_address(vmalloc_to_page(vbuf)) +
                                          ((unsigned long)vbuf & (PAGE_SIZE - 1));
            if (k_vbuf == NULL)
            { 
                PrintkError ("error, DMA requested on high memory page");
                return;
            }

            tmp = __pa (k_vbuf);
        }

        /* Write (if dirty) and purge contents of 'vbuf' from CPU cache(s) */
        /* Possible options:
         *   consistent_sync(vbuf, bytes, DMA_FROM_DEVICE);            ARM/SH
         *   dma_cache_inv((unsigned long)vbuf, (unsigned long)bytes); MIPS/PPC/x86
         */
#ifdef OMAP2420_DCACHE_BUG_WORKAROUND
        /* Routine blk_dma_inv_range_harvard() (arch/arm/mm/blockops.c) appears
         * to be buggy: it seems to constantly shoot past the end of the address
         * range [vbuf..vbuf+bytes] by 32 bytes. It also appears to write some
         * bogus data into this RAM address range while invalidating it in D-cache. 
         */
        consistent_sync (vbuf, bytes - 1, DMA_FROM_DEVICE);
#else
        consistent_sync (vbuf, bytes, DMA_FROM_DEVICE);
#endif

    }
    else /* transfer into intermediate DMA buffer */
    {
        tmp = tffsInfo.dma_buf_paddr;
    }
    writel (tmp, &regs->cdsa);

    /* Channel Status Register.
     * Write all-ones to clear any pending interrupts.
     */
    writel (~0, &regs->csr);

    /* start DMA channel by setting ENABLED bit in Channel Control Register */
    writel ((tmp2 | OMAP_DMA4_CCR_EN), &regs->ccr);

    /* we don't want to see any of these DMA errors */
    tmp2 = (1 << 11) |  /* misaligned error         */
           (1 <<  9) |  /* secure transaction error */
           (1 <<  8) |  /* transaction error        */
           (1 <<  1);   /* synchronization drop     */

    /* Wait for DMA transfer to finish (ENABLED bit in
     * Channel Control Register changes to zero).
     */
    for (waiting = 100000; waiting > 0; waiting--) /* hope this is long enough ... */
    {
        if( readl(&regs->ccr) & OMAP_DMA4_CCR_EN )
            continue;

        /* read Channel Status Register to find out status of DMA transfer */
        tmp = readl(&regs->csr) & tmp2;

        /* write all-ones to clear any pending interrupts */
        writel (~0, &regs->csr);

        /* any errors ? */
        if (tmp == 0)
        {
            /* DMA transfer successfully completed, move data from intermediate
             * DMA buffer to destination buffer (in case it was used).
             */
            if (direct_dma == FALSE)
                memcpy (vbuf, tffsInfo.dma_buf_vptr, bytes);
        }
        else /* andrayk June 13 2006: DMA transfer failed, find way to let MTD know this */
        { 
            PrintkError ("DMA transfer failed");
        }
        return;
    }

    PrintkError ("DMA transfer timed out");
}




/*-----------------------------------------------------------------------*
 *                                                                       *
 *                    _ _ d m a s w _ b w r i t e                        *
 *                                                                       *
 * Transfer data from RAM buffer to DiskOnChip's I/O reguster.           *
 * Example implementation For Texas Instruments OMAP2420 H4 board.       *
 *                                                                       *
 * Parameters:                                                           *
 *      off            offset of DiskOnChip's I/O register from the base *
 *                     of DiskOnChip window                              *
 *      vbuf           virtual address of RAM buffer                     *
 *      bytes          bytes to transfer                                 *
 *                                                                       *
 *-----------------------------------------------------------------------*/
static
void __dmasw_bwrite ( int            off,
                      void         * vbuf, 
                      unsigned int   bytes )
{
    register __dma_reg_t * regs = dma_regs_vptr;
    register u32           tmp, tmp2;
    register int           waiting;

    profiling[3]++;

    /* Channel Source/Destination Parameters */
    tmp = (0 << 21) |      /* little-endian source (SDRAM)         */
          (1 << 20) |      /* source endianism locked              */
          (0 << 19) |      /* little-endian dest. (DiskOnChip)     */
          (1 << 18) |      /* destination endianism locked         */
          (1 << 16) |      /* writes (to DiskOnChip) posted        */
          (0 << 14) |      /* 64-byte burst on dest. (DiskOnChip)  */
          (1 << 13) |      /* destination (DiskOnChip) packed      */
          (3 <<  7) |      /* 64-byte burst on source (SDRAM)      */
          (1 <<  6) |      /* source (SDRAM) packed                */
          (1 <<  0) ;      /* 16-bit data port width               */
    writel (tmp, &regs->csdp);

    /* Channel Control Register */
    tmp2 = (0 << 25) |     /* buffering enabled                    */
           (1 << 24) |     /* source triggers DMA request          */
           (1 << 23) |     /* prefetch enabled                     */
           (0 << 21) |     /* secure mode disabled                 */
           (0 << 19) |     /* no synchronization                   */
           (1 << 18) |     /* block synchronization                */
           (0 << 17) |     /* transaparent copy disabled           */
           (0 << 16) |     /* constant fill disabled               */
           (0 << 14) |     /* constant destination address         */
           (1 << 12) |     /* post-increment source address        */
           (0 <<  8) |     /* ignore MSUSPEND                      */
           (0 <<  7) |     /* channel disabled (stopped)           */
           (1 <<  6) |     /* high priority channel                */
           (0 <<  5) |     /* no frame synchronization             */
           (0 <<  0);      /* no synchronization (i.e. s/w DMA)    */
    writel (tmp2, &regs->ccr);

    /* Channel Element Number Register */
    writel ((bytes >> 1), &regs->cen);

    /* Channel Source Start Address Register.
     * This would be physical address of intermediate DMA buffer (for
     * DMA modes 2 and 3), or physical address of the destination buffer
     * (for DMA modes 6 and 7).
     */
    if (tffs_dma_mode & 4) /* do DMA transfer directly from 'vbuf' */
    {
        /* Write (if dirty) contents of 'vbuf' from CPU D-cache to RAM.
         * Possible options:
         *   consistent_sync(vbuf, bytes, DMA_TO_DEVICE);                ARM/SH
         *   dma_cache_wback((unsigned long)vbuf, (unsigned long)bytes); MIPS/PPC/x86
         */
        consistent_sync (vbuf, bytes, DMA_TO_DEVICE);

        if (virt_addr_valid((unsigned long)vbuf)) /* 'vbuf' is in low memory */
        {
            tmp = __pa (vbuf);
        }
        else                                      /* 'vbuf' is in high memory */
        {
            char * k_vbuf = (char *) page_address(vmalloc_to_page(vbuf)) +
                                          ((unsigned long)vbuf & (PAGE_SIZE - 1));
            if (k_vbuf == NULL)
            { 
                PrintkError ("error, DMA requested on high memory page");
                return;
            }

            tmp = __pa (k_vbuf);
        }
    }
    else                   /* use intermediate DMA buffer */
    {
        /* move data from RAM buffer to the intermediate DMA buffer */
        memcpy (tffsInfo.dma_buf_vptr, vbuf, bytes);

        tmp = tffsInfo.dma_buf_paddr;
    }
    writel (tmp, &regs->cssa);

    /* Channel Destination Start Address Register.
     * This would be DiskOnChip's I/O register which is located at offset
     * 'off' from the DiskOnChip's base address as specified by tffs_addr[0]).
     */
    writel ((tffs_addr[0] + off), &regs->cdsa);

    /* Channel Status Register.
     * Write all-ones to clear any pending interrupts.
     */
    writel (~0, &regs->csr);

    /* start DMA channel by setting ENABLED bit in Channel Control Register */
    writel ((tmp2 | OMAP_DMA4_CCR_EN), &regs->ccr);

    /* we don't want to see any of these DMA errors */
    tmp2 = (1 << 11) |  /* misaligned error         */
           (1 <<  9) |  /* secure transaction error */
           (1 <<  8) |  /* transaction error        */
           (1 <<  1);   /* synchronization drop     */

    /* Wait for DMA transfer to finish (ENABLED bit in
     * Channel Control Register changes to zero).
     */
    for (waiting = 100000; waiting > 0; waiting--) /* hope this is long enough ... */
    {
        if( readl(&regs->ccr) & OMAP_DMA4_CCR_EN )
            continue;

        /* read Channel Status Register to find out status of DMA transfer */
        tmp = readl(&regs->csr) & tmp2;

        /* write all-ones to clear any pending interrupts */
        writel (~0, &regs->csr);

        /* any errors ? */
        if (tmp != 0) /* andrayk June 13 2006: DMA transfer failed, find way to let MTD know this */
        { 
            PrintkError ("DMA transfer failed");
        }
        return;
    }

    PrintkError ("DMA transfer timed out");
}




#elif defined(CONFIG_OMAP_OSK)

/*_____________________________________________________________________________
 |                                                                            |
 |                                                                            |
 |                Texas Instruments OMAP OSK board                            |
 |                                                                            |
 |____________________________________________________________________________| 
*/


unsigned char tffsarch_init (int resume)
{
  (*(short*)0xFFFEC808) = 0x00F5;                                                
  (*(short*)0xFFFEC808) = 0x00A0;                                                                                                         
  return 1;
}

void TffsHWRelease(void)
{ }

static void __dmasw_bread (int off, void * vbuf, unsigned int bytes)
{ }

static void __dmasw_bwrite (int off, void * vbuf, unsigned int bytes)
{ }




#elif defined(CONFIG_ARCH_LUBBOCK)

/*_____________________________________________________________________________
 |                                                                            |
 |                                                                            |
 |                Intel PXA25x ("Lubbock") board                              |
 |                                                                            |
 |____________________________________________________________________________| 
*/

# define MSC2_CNTR_REG_ADDR 0x48000000
# define MSC2_OFFSET        0x10
# define MCS5_INIT_VAL      0xFFF80000

unsigned char tffsarch_init (int resume)
{
	volatile void *myMSC2ptr;
	unsigned long msc2Val;

	myMSC2ptr = (volatile void *)ioremap_nocache(MSC2_CNTR_REG_ADDR, 0x1000);
	/* MSC2 Static Memory Control Register - 16-bit mode for CS5 */
	msc2Val = readl( myMSC2ptr + MSC2_OFFSET );
	writel( ((msc2Val & 0x0000FFFF) | MCS5_INIT_VAL), myMSC2ptr + MSC2_OFFSET );
	/*  *(myMCS5 + 4) = MCS5_INIT_VAL; */
	iounmap( (void *)myMSC2ptr);

	return 1;
}

static void __dmasw_bread (int off, void * vbuf, unsigned int bytes)
{ }

static void __dmasw_bwrite (int off, void * vbuf, unsigned int bytes)
{ }




#else /* ARCH */

/*_____________________________________________________________________________
 |                                                                            |
 |                                                                            |
 |                         All other boards                                   |
 |                                                                            |
 |____________________________________________________________________________| 
*/

unsigned char tffsarch_init (int resume)
{ 
    return 1;
}

void TffsHWRelease(void)
{ }

static void __dmasw_bread (int off, void * vbuf, unsigned int bytes)
{ }

static void __dmasw_bwrite (int off, void * vbuf, unsigned int bytes)
{ }

#endif /* ARCH */

