/*
    Copyright 2001 (c) by Authors,
    Swiss Federal Institute of Technology,
    Computer Engineering and Networks Laboratory.
 
    TOPSY -- A Teachable Operating System.
             Implementation of a tiny and simple
             micro kernel for teaching purposes.
 
    For further information, please visit http://www.tik.ee.ethz.ch/~topsy
 
    This software is provided under the terms of the GNU General Public 
    Licence. A full copy of the GNU GPL is provided in the file COPYING 
    found in the development root of Topsy.
*/
/*
	
    File:                  $Source: /home/ruf/cvs_Topsy/cvs/Topsy/IO/sa1100/Drivers/Attic/IXPMemAccess.c,v $
    Affiliation:           ETH Zuerich, TIK
    Version:               $Revision: 2936 $
    Last Date of Change:   $Date: 2007-03-21 15:23:42 +0100 (Mit, 21 Mär 2007) $  by: $Author: baderma $
 
*/

// this code is based upon parts of vera from Princeton

//
// Utilities for the Intel IXP1200
// Princeton University
// Scott Karlin and Tammo Spalink
//
// Copyright Princeton University Board of Trustees
// All rights reserved
//

#include "types.h"
#include "cpu.h"
#include "IXP_Defines.h"


#define SCRATCH_LEN (1024 * 4)
#define SRAM_LEN    (1024 * 1024 * 2)
#define DRAM_LEN    (1024 * 1024 * 16)


/*****************************************************************************/
/*                                                                           */
/* Function Name: mem[Read|Write][8|16|32]                                   */
/* Parameters:                                                               */
/*      uint32             address                                           */
/*      u[8|16|32]      value   (Value always follows address.)              */
/* Returns: u[8|16|32] for Read, <none> for Write                            */
/* Effects: Memory access functions. Hidden away for convenience.            */
/*                                                                           */
/*****************************************************************************/


inline uint32 memRead32(uint32 address)
{
  return *((volatile uint32 *)address);
}

inline void memWrite32(uint32 address, uint32 value)
{
  *((volatile uint32 *)address) = value;
}

inline uint8 memRead8(uint32 address)
{
  // memory access is big endian so the lowest byte ist at address + 3
  return *((volatile uint8 *)(address + 3L));
}

inline void memWrite8(uint32 address, uint8 value)
{
  // memory access is big endian so the lowest byte ist at address + 3
  *((volatile uint8 *)(address + 3L)) = value;
}

inline uint32 dramRead32(uint32 address)
{
  return memRead32(DRAM_BASE + (address * 4));
}

inline uint32 sramRead32(uint32 address)
{
  return memRead32(SRAM_BASE + (address * 4));
}

inline uint32 scratchRead32(uint32 address)
{
  return memRead32(SCRATCH_BASE + (address * 4));
}

inline void dramWrite32(uint32 address, uint32 value)
{
  memWrite32(DRAM_BASE + (address * 4), value);
}

inline void sramWrite32(uint32 address, uint32 value)
{
  memWrite32(SRAM_BASE + (address * 4), value);
}

inline void scratchWrite32(uint32 address, uint32 value)
{
  memWrite32(SCRATCH_BASE + (address * 4), value);
}


/*****************************************************************************/
/*                                                                           */
/* Function Name: MemAccess_Zero                                             */
/* Parameters:                                                               */
/*      MemAccess_Region    regionAddr  Region of memory to zero.            */
/*      uint32              startAddr   Relative to start of region          */
/*      int                 bytes       Number of bytes to zero              */
/* Returns: <none>                                                           */
/* Effects: Sets a region of memory bytes long to 0, beginning at startAddr  */
/*                                                                           */
/*****************************************************************************/

void MemAccess_Zero(address_t regionAddr, uint32 startAddr, int bytes)
{
  int i = 0;


  for (i = 0; i < bytes; i++)
  {
    memWrite8(regionAddr + startAddr + i, 0);
  }
}


/*****************************************************************************/
/*                                                                           */
/* Function Name: MemAccess_Init                                             */
/* Parameters: <none>                                                        */
/* Returns: <none>                                                           */
/* Effects: Sets all memory in the given regions to 0.                       */
/*                                                                           */
/*****************************************************************************/

void MemAccess_Init(void)
{
  MemAccess_Zero(IXP1200_SCRATCH_START, 0, SCRATCH_LEN);
  MemAccess_Zero(SRAM_BASE, 0, SRAM_LEN);
  MemAccess_Zero(DRAM_BASE, 0, DRAM_LEN);
}
