/*
 * soclib_dma.h - soclib Multi Channels DMA driver definition.
 *
 * Author     Alain Greiner (2017)
 *
 * Copyright (c) UPMC Sorbonne Universites
 *
 * This file is part of ALMOS-MKH.
 *
 * ALMOS-MKH 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; version 2.0 of the License.
 *
 * ALMOS-MKH 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with ALMOS-MKH; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 */

#ifndef _SOCLIB_DMA_H_
#define _SOCLIB_DMA_H_

/********************************************************************************************
 * This driver supports the SocLib VciBlockDevice component, that is a simgle channel,
 * block oriented, external storage controler, supporting only one I/O transaction,
 * at a given time.
 *******************************************************************************************/

/********************************************************************************************
 *       SOCLIB_DMA registers offset 
 *******************************************************************************************/

enum SoclibDmaRegisters 
{
    DMA_SRC		     = 0,    /*! source buffer 32 LSB address bits                         */
    DMA_DST		     = 1,    /*! source buffer 32 LSB address bits                         */
    DMA_LEN_STS	     = 2,    /*! number of bytes (on write) / transfer status (on read)    */
    DMA_RESET		 = 3,    /*! desactivate channel (can be usde to acknowledge IRQ)      */
    DMA_IRQ_DISABLED = 4,    /*! no IRQ generated if non zero                              */
    DMA_SRC_EXT      = 5,    /*! source buffer 32 MSB address bits                         */
    DMA_DST_EXT      = 6,    /*! source buffer 32 MSB address bits                         */

    DMA_SPAN		 = 8,	 /*! number of registers per channel                           */
};

/********************************************************************************************
 *       SOCLIB_DMA status values
 *******************************************************************************************/

#define DMA_SUCCESS       0
#define DMA_IDLE          2
#define DMA_ERROR_READ    1
#define DMA_ERROR_WRITE   3
#define DMA_BUSY          4    // or any value larger than 3


/********************************************************************************************
 * This function access the SOCLIB_DMA hardware register to disable interrupts,
 * because the most frequent operations are supposed to be synchronous accesses.
 ********************************************************************************************
 * @ chdev     : pointer on DMA chdev descriptor.
 *******************************************************************************************/
extern void soclib_dma_init( chdev_t * chdev );

/********************************************************************************************
 * This function can be called by the "server" thread associated to the DMA channel, for an
 * asynchronous access, or can be directly called by the "client" thread for a synchronous
 * access. In both cases, it get the command arguments from the calling thread descriptor,
 * and access the DMA registers to launch the DMA transfer. 
 * Then, the waiting policy depends on the command type:
 * - for asynchronous access, it enables the DMA interrupts, blocks on THREAD_BLOCKED_ISR,
 *   and deschedules. It will be re-activated by the soclib_dma_isr() function.
 * - for a synchronous transfer, it polls the DMA status register until completion,
 *   and reports the transfer status in the command registered in the client thread.  
 ********************************************************************************************
 * @ thread_xp  : extended pointer on the client thread.
 *******************************************************************************************/
extern void soclib_dma_cmd( xptr_t thread_xp );

/********************************************************************************************
 * This Interrupt Service Routine is executed when the IRQ signaling the completion of 
 * an asynchronous DMA command is received by a core. It acknowledge the IRQ by accessing
 * the proper SOCLIB_DMA register, reports the transfer status in the command registered
 * in the client thread descriptor, and unblock the server thread.
 ********************************************************************************************
 * @ chdev     : pointer on DMA chdev descriptor.
 *******************************************************************************************/
extern void soclib_dma_isr( chdev_t * chdev );


#endif	/* _SOCLIB_DMA_H_ */
