/**
 * \file	io.h
 * \date	5 September 2012
 * \author	Cesar Fuguet
 *
 * Utility functions to write or read memory mapped hardware registers
 */
#ifndef IO_H
#define IO_H

#define in_reset __attribute__((section (".reset")))

/**
 * Read an 32 bits memory mapped hardware register
 */
in_reset static inline unsigned int ioread32(void * addr)
{
	return *(volatile unsigned int *) addr;
}

/**
 * Read an 16 bits memory mapped hardware register
 */
in_reset static inline unsigned short ioread16(void * addr)
{
	return *(volatile unsigned short *) addr;
}

/**
 * Read an 8 bits memory mapped hardware register
 */
in_reset static inline unsigned char ioread8(void * addr)
{
	return *(volatile unsigned char *) addr;
}

/**
 * Write an 32 bits memory mapped hardware register
 */
in_reset static inline void iowrite32(void * addr, unsigned int value)
{
	*(volatile unsigned int *) addr = value;
	asm volatile("sync");
}

/**
 * Write an 16 bits memory mapped hardware register
 */
in_reset static inline void iowrite16(void * addr, unsigned short value)
{
	*(volatile unsigned short *) addr = value;
	asm volatile("sync");
}

/**
 * Write an 8 bits memory mapped hardware register
 */
in_reset static inline void iowrite8(void * addr, unsigned char value)
{
	*(volatile unsigned char *) addr = value;
	asm volatile("sync");
}

#undef in_reset

#endif
