#ifndef CYGONCE_HAL_PLF_IO_H
#define CYGONCE_HAL_PLF_IO_H
//=============================================================================
//
//      plf_io.h
//
//      Platform specific registers
//
//=============================================================================
// ####ECOSGPLCOPYRIGHTBEGIN####                                            
// -------------------------------------------                              
// This file is part of eCos, the Embedded Configurable Operating System.   
// Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2005, 2006, 2008 Free Software Foundation, Inc.
// Copyright (C) 2003, 2005, 2006, 2008 eCosCentric Limited                 
//
// eCos 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 or (at your option) any later      
// version.                                                                 
//
// eCos 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 eCos; if not, write to the Free Software Foundation, Inc.,    
// 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.            
//
// As a special exception, if other files instantiate templates or use      
// macros or inline functions from this file, or you compile this file      
// and link it with other works to produce a work based on this file,       
// this file does not by itself cause the resulting work to be covered by   
// the GNU General Public License. However the source code for this file    
// must still be made available in accordance with section (3) of the GNU   
// General Public License v2.                                               
//
// This exception does not invalidate any other reasons why a work based    
// on this file might be covered by the GNU General Public License.         
// -------------------------------------------                              
// ####ECOSGPLCOPYRIGHTEND####                                              
//=============================================================================
//#####DESCRIPTIONBEGIN####
//
// Author(s):    eCosCentric, jskov
// Contributors: 
// Date:         2005-07-29
// Purpose:      Atmel SAM9260-EK platform specific registers
// Description: 
// Usage:        #include <cyg/hal/plf_io.h>
//
//####DESCRIPTIONEND####
//
//=============================================================================

#include <cyg/hal/sam9260ek.h>
#include <pkgconf/hal_arm_arm9_sam9.h>

//-----------------------------------------------------------------------------
// Address space translation macros

#define CYGARC_PHYSICAL_ADDRESS(_x_)                                    \
({ CYG_ADDRWORD _p = (CYG_ADDRWORD)(_x_);                               \
 if (_p >= SAM9260EK_SDRAM_VIRT_BASE &&                                 \
     _p < (SAM9260EK_SDRAM_VIRT_BASE+SAM9260EK_SDRAM_SIZE))             \
     _p = (_p & SAM9260EK_SDRAM_MASK) + SAM9260EK_SDRAM_PHYS_BASE;      \
 else if (_p >= SAM9260EK_SRAM_VIRT_BASE &&                             \
          _p < (SAM9260EK_SRAM_VIRT_BASE+SAM9260EK_SRAM_SIZE))          \
     _p = (_p & SAM9260EK_SRAM_MASK) + SAM9260EK_SRAM_PHYS_BASE;        \
 else if (_p >= SAM9260EK_IROM_VIRT_BASE &&                             \
          _p < (SAM9260EK_IROM_VIRT_BASE+SAM9260EK_IROM_SIZE))          \
     _p = (_p & SAM9260EK_IROM_MASK) + SAM9260EK_IROM_PHYS_BASE;        \
 else if (_p >= SAM9260EK_UHPREG_VIRT_BASE &&                           \
          _p < (SAM9260EK_UHPREG_VIRT_BASE+SAM9260EK_UHPREG_SIZE))      \
     _p = (_p & SAM9260EK_UHPREG_MASK) + SAM9260EK_UHPREG_PHYS_BASE;    \
 _p; })

#define CYGARC_VIRTUAL_ADDRESS(_x_)                                     \
({ CYG_ADDRWORD _p = (CYG_ADDRWORD)(_x_);                               \
 if (_p >= SAM9260EK_SDRAM_PHYS_BASE &&                                 \
     _p < (SAM9260EK_SDRAM_PHYS_BASE+SAM9260EK_SDRAM_SIZE))             \
     _p = (_p & SAM9260EK_SDRAM_MASK) + SAM9260EK_SDRAM_VIRT_BASE;      \
 else if (_p >= SAM9260EK_SRAM_PHYS_BASE &&                             \
          _p < (SAM9260EK_SRAM_PHYS_BASE+SAM9260EK_SRAM_SIZE))          \
     _p = (_p & SAM9260EK_SRAM_MASK) + SAM9260EK_SRAM_VIRT_BASE;        \
 else if (_p >= SAM9260EK_IROM_PHYS_BASE &&                             \
          _p < (SAM9260EK_IROM_PHYS_BASE+SAM9260EK_IROM_SIZE))          \
     _p = (_p & SAM9260EK_IROM_MASK) + SAM9260EK_IROM_VIRT_BASE;        \
 else if (_p >= SAM9260EK_UHPREG_PHYS_BASE &&                           \
          _p < (SAM9260EK_UHPREG_PHYS_BASE+SAM9260EK_UHPREG_SIZE))      \
     _p = (_p & SAM9260EK_UHPREG_MASK) + SAM9260EK_UHPREG_VIRT_BASE;    \
 _p; })

#define CYGARC_UNCACHED_ADDRESS(_x_)                                    \
({ CYG_ADDRWORD _p = (CYG_ADDRWORD)(_x_);                               \
 if (_p >= SAM9260EK_SDRAM_VIRT_BASE &&                                 \
     _p < (SAM9260EK_SDRAM_VIRT_BASE+SAM9260EK_SDRAM_SIZE))             \
     _p = (_p & SAM9260EK_SDRAM_MASK) + SAM9260EK_SDRAM_UNCACHED_BASE;  \
 else if (_p >= SAM9260EK_SRAM_VIRT_BASE &&                             \
     _p < (SAM9260EK_SRAM_VIRT_BASE+SAM9260EK_SRAM_SIZE))               \
     _p = _p + SAM9260EK_SRAM_UNCACHED_OFFSET;                          \
 else if (_p >= SAM9260EK_IROM_VIRT_BASE &&                             \
          _p < (SAM9260EK_IROM_VIRT_BASE+SAM9260EK_IROM_SIZE))          \
     _p = _p + SAM9260EK_IROM_UNCACHED_OFFSET;                          \
 else if (_p >= SAM9260EK_UHPREG_VIRT_BASE &&                           \
          _p < (SAM9260EK_UHPREG_VIRT_BASE+SAM9260EK_UHPREG_SIZE))      \
     _p = _p + SAM9260EK_UHPREG_UNCACHED_OFFSET;                        \
 _p; })

//-----------------------------------------------------------------------------
// TWI (I2C) bus

#ifdef CYGPKG_HAL_SAM9_TWI
# define HAL_I2C_EXPORTED_DEVICES \
    extern cyg_i2c_bus hal_sam9_i2c_bus;
#endif

//-----------------------------------------------------------------------------
// MMC/SD Interface (MCI) bus

// Only files that need the below defines will have set __MMCSD_DRIVER_PRIVATE before inclusion.
#if defined(__MMCSD_DRIVER_PRIVATE) && defined(CYGPKG_HAL_ARM_ARM9_SAM9_MCI)
#include <cyg/infra/cyg_type.h>

// Only one socket, so ignore card and bus throughout below.

// Platform hook for bus initialisation, independent of socket, e.g.
// socket-independent pin config.
// err only needs to be set if there's an error.
//#define HAL_MMCSD_PLF_INIT_BUS( _bus_, _err_ )

#define HAL_MMCSD_PLF_GET_SOCKET_CAPABILITY(_bus_, _socket_, _err_)     \
    CYG_MACRO_START                                                     \
    (_socket_)->sockethw_caps.valid = 1;                                \
    (_socket_)->sockethw_caps.sd = 1;                                   \
    (_socket_)->sockethw_caps.fourbit = 1;                              \
    (_socket_)->sockethw_caps.card_detect = 0;                          \
    (_socket_)->sockethw_caps.card_detect_int = 0;                      \
    (_socket_)->sockethw_vdd = MMCSD_OCR_REGISTER_VDD_32_33;            \
    (_socket_)->sockethw_caps.multiblock_io = 0;                        \
    CYG_MACRO_END
    

// Platform hook for initialising socket, primarily pin configuration.
// err only needs to be set if there's an error.
// For this platform:
// There is no card detection or write protect switch sensing, so
// nothing needs doing about that here. But the standard MCI pins do
// need setting up. Only slot A is wired up.
//  MCCK      = PA8          (clock)               (periph func. A)
//  MCCDB     = PA1          (command/response)    (periph func. B)
//  MCDB0     = PA0          (data0)               (periph func. B)
//  MCDB1,2,3 = PA5,4,3      (data1,2,3)           (periph func. B)
// Also on this platform SPI0's CLK (PA2) is connected to MCCK, and so
// must be disabled and tri-stated (set to input with PIO).

#define HAL_MMCSD_PLF_INIT_SOCKET( _card_, _err_ )              \
    CYG_MACRO_START                                             \
    const cyg_uint32 pins_peripha = 0x00000100;                 \
    const cyg_uint32 pins_periphb = 0x0000003B;                 \
    cyg_uint32 pins = pins_peripha | pins_periphb;              \
                                                                \
    /* Disable GPIO interrupt mode: */                          \
    HAL_WRITE_UINT32( SAM9_PIOA + _PIO_IDR, pins );             \
    /* Enable pull-ups */                                       \
    HAL_WRITE_UINT32( SAM9_PIOA + _PIO_PUER, pins );            \
    /* Disable glitch filter: */                                \
    HAL_WRITE_UINT32( SAM9_PIOA + _PIO_IFDR, pins );            \
    /* Disable multi-drive mode: */                             \
    HAL_WRITE_UINT32( SAM9_PIOA + _PIO_MDDR, pins );            \
    /* Set appropriate peripheral function: */                  \
    HAL_WRITE_UINT32( SAM9_PIOA + _PIO_ASR, pins_peripha );     \
    HAL_WRITE_UINT32( SAM9_PIOA + _PIO_BSR, pins_periphb );     \
    /* Disable PIO mode: */                                     \
    HAL_WRITE_UINT32( SAM9_PIOA + _PIO_PDR, pins );             \
    /* Set SPI0 CLK to input */                                 \
    HAL_WRITE_UINT32( SAM9_PIOA + _PIO_ODR, 1<<2 );             \
    /* Set SPI0 CLK to PIO mode */                              \
    HAL_WRITE_UINT32( SAM9_PIOA + _PIO_PER, 1<<2 );             \
    CYG_MACRO_END

// Until multiple socket support is implemented, we set the socket here
#define _MCI_SDCR_SDCSEL _MCI_SDCR_SDCSEL_B

// Platform hook for initialising card detection for polling. Usually some PIOs.
// Used in addition to HAL_MMCSD_PLF_INIT_CARD_DETECT_INTERRUPT if relevant.
// For this platform:
// There is no card detection or write protect switch sensing, so
// nothing to do here
//
//#define HAL_MMCSD_PLF_INIT_CARD_DETECT( _bus_, _card_, _err_ )


// Platform hook for initialisation of interrupt handler for card
// insertion/removal detection. Used in addition to HAL_MMCSD_PLF_INIT_CARD_DETECT.
// err only needs to be set if there's an error.
//#define HAL_MMCSD_PLF_INIT_CARD_DETECT_INTERRUPT(_bus_, _socket_, _err_)

#endif // if defined(__MMCSD_DRIVER_PRIVATE) && defined(CYGPKG_HAL_ARM_ARM9_SAM9_MCI)

//-----------------------------------------------------------------------------
// end of plf_io.h
#endif // CYGONCE_HAL_PLF_IO_H
