2012-01-09 12:29:03 +00:00
|
|
|
/*
|
2012-01-14 01:42:45 +00:00
|
|
|
* \brief Generic MMIO access framework
|
2012-01-09 12:29:03 +00:00
|
|
|
* \author Martin stein
|
|
|
|
* \date 2011-10-26
|
|
|
|
*/
|
|
|
|
|
2012-01-10 11:56:45 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2011-2012 Genode Labs GmbH
|
|
|
|
*
|
|
|
|
* This file is part of the Genode OS framework, which is distributed
|
|
|
|
* under the terms of the GNU General Public License version 2.
|
|
|
|
*/
|
|
|
|
|
2012-01-09 12:29:03 +00:00
|
|
|
#ifndef _BASE__INCLUDE__UTIL__MMIO_H_
|
|
|
|
#define _BASE__INCLUDE__UTIL__MMIO_H_
|
|
|
|
|
2012-05-24 09:56:54 +00:00
|
|
|
/* Genode includes */
|
2012-01-09 20:05:49 +00:00
|
|
|
#include <util/register.h>
|
2012-01-09 12:29:03 +00:00
|
|
|
|
|
|
|
namespace Genode
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* A continuous MMIO region
|
|
|
|
*/
|
|
|
|
class Mmio
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
|
|
|
|
/**
|
2012-01-17 08:47:23 +00:00
|
|
|
* Write typed 'value' to MMIO base + 'o'
|
2012-01-09 12:29:03 +00:00
|
|
|
*/
|
2012-01-17 17:55:24 +00:00
|
|
|
template <typename _ACCESS_T>
|
2012-05-24 09:56:54 +00:00
|
|
|
inline void _write(off_t const o, _ACCESS_T const value) {
|
|
|
|
*(_ACCESS_T volatile *)((addr_t)base + o) = value; }
|
2012-01-09 12:29:03 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Read typed from MMIO base + 'o'
|
|
|
|
*/
|
2012-01-17 17:55:24 +00:00
|
|
|
template <typename _ACCESS_T>
|
2012-05-24 09:56:54 +00:00
|
|
|
inline _ACCESS_T _read(off_t const o) const {
|
|
|
|
return *(_ACCESS_T volatile *)((addr_t)base + o); }
|
2012-01-09 12:29:03 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
2012-01-14 01:42:45 +00:00
|
|
|
enum { BYTE_WIDTH_LOG2 = 3, BYTE_WIDTH = 1 << BYTE_WIDTH_LOG2 };
|
2012-01-09 12:29:03 +00:00
|
|
|
|
|
|
|
/**
|
2012-01-17 17:55:24 +00:00
|
|
|
* An integer like region within a MMIO region.
|
|
|
|
*
|
2012-05-24 09:56:54 +00:00
|
|
|
* \param _OFFSET Offset of the region relative to the
|
|
|
|
* base of the compound MMIO
|
|
|
|
* \param _ACCESS_WIDTH Bit width of the region, for a list of
|
|
|
|
* supported widths see 'Genode::Register'
|
|
|
|
* \param _STRICT_WRITE If set to 0, when writing a bitfield, we
|
|
|
|
* read the register value, update the bits
|
|
|
|
* on it, and write it back to the register.
|
|
|
|
* If set to 1 we take an empty register
|
|
|
|
* value instead, apply the bitfield on it,
|
|
|
|
* and write it to the register. This can
|
|
|
|
* be useful if you have registers that have
|
|
|
|
* different means on reads and writes.
|
2012-01-17 08:47:23 +00:00
|
|
|
*
|
2012-02-22 15:14:41 +00:00
|
|
|
* \detail See 'Genode::Register'
|
2012-01-09 12:29:03 +00:00
|
|
|
*/
|
2012-05-24 09:56:54 +00:00
|
|
|
template <off_t _OFFSET, unsigned long _ACCESS_WIDTH,
|
|
|
|
bool _STRICT_WRITE = false>
|
2012-01-17 17:55:24 +00:00
|
|
|
struct Register : public Genode::Register<_ACCESS_WIDTH>
|
2012-01-09 12:29:03 +00:00
|
|
|
{
|
2012-01-17 17:55:24 +00:00
|
|
|
enum {
|
|
|
|
OFFSET = _OFFSET,
|
|
|
|
ACCESS_WIDTH = _ACCESS_WIDTH,
|
2012-05-24 09:56:54 +00:00
|
|
|
STRICT_WRITE = _STRICT_WRITE,
|
2012-01-17 17:55:24 +00:00
|
|
|
};
|
2012-01-09 12:29:03 +00:00
|
|
|
|
|
|
|
/**
|
2012-01-17 17:55:24 +00:00
|
|
|
* A region within a register
|
2012-02-22 15:14:41 +00:00
|
|
|
*
|
|
|
|
* \param _SHIFT Bit shift of the first bit within the
|
|
|
|
* compound register
|
2012-01-17 17:55:24 +00:00
|
|
|
* \param _WIDTH Bit width of the region
|
|
|
|
*
|
|
|
|
* \detail See 'Genode::Register::Bitfield'
|
2012-01-09 12:29:03 +00:00
|
|
|
*/
|
2012-01-17 17:55:24 +00:00
|
|
|
template <unsigned long _SHIFT, unsigned long _WIDTH>
|
2012-02-22 15:14:41 +00:00
|
|
|
struct Bitfield :
|
|
|
|
public Genode::Register<ACCESS_WIDTH>::template Bitfield<_SHIFT, _WIDTH>
|
2012-01-09 12:29:03 +00:00
|
|
|
{
|
2012-05-24 09:56:54 +00:00
|
|
|
/* Back reference to containing register */
|
|
|
|
typedef Register<OFFSET, ACCESS_WIDTH, STRICT_WRITE> Compound_reg;
|
2012-01-09 12:29:03 +00:00
|
|
|
};
|
2012-01-14 01:42:45 +00:00
|
|
|
};
|
2012-01-09 12:29:03 +00:00
|
|
|
|
2012-01-14 01:42:45 +00:00
|
|
|
/**
|
2012-01-17 17:55:24 +00:00
|
|
|
* An array of successive equally structured regions
|
|
|
|
*
|
2012-02-22 15:14:41 +00:00
|
|
|
* \param _OFFSET Offset of the first region relative to
|
|
|
|
* the base of the compound MMIO.
|
|
|
|
* \param _ACCESS_WIDTH Bit width of a single access, must be at
|
|
|
|
* least the item width.
|
|
|
|
* \param _ITEMS How many times the region gets iterated
|
|
|
|
* successive
|
2012-01-17 17:55:24 +00:00
|
|
|
* \param _ITEM_WIDTH Bit width of a region
|
2012-05-24 09:56:54 +00:00
|
|
|
* \param _STRICT_WRITE If set to 0, when writing a bitfield, we
|
|
|
|
* read the register value, update the bits
|
|
|
|
* on it, and write it back to the register.
|
|
|
|
* If set to 1, we take an empty register
|
|
|
|
* value instead, apply the bitfield on it,
|
|
|
|
* and write it to the register. This can
|
|
|
|
* be useful if you have registers that have
|
|
|
|
* different means on reads and writes.
|
|
|
|
* Please note that ACCESS_WIDTH is decisive
|
|
|
|
* for the range of such strictness.
|
2012-01-14 01:42:45 +00:00
|
|
|
*
|
2012-02-22 15:14:41 +00:00
|
|
|
* \detail The array takes all inner structures, wich are covered
|
|
|
|
* by an item width and iterates them successive. Such
|
|
|
|
* structures that are partially exceed an item range are
|
|
|
|
* read and written also partially. Structures that are
|
|
|
|
* completely out of the item range are read as '0' and
|
|
|
|
* trying to overwrite them has no effect. The array is
|
|
|
|
* not limited to its access width, it extends to the
|
|
|
|
* memory region of its successive items. Trying to read
|
|
|
|
* out read with an item index out of the array range
|
|
|
|
* returns '0', trying to write to such indices has no
|
|
|
|
* effect
|
2012-01-14 01:42:45 +00:00
|
|
|
*/
|
2012-02-22 15:14:41 +00:00
|
|
|
template <off_t _OFFSET, unsigned long _ACCESS_WIDTH,
|
2012-05-24 09:56:54 +00:00
|
|
|
unsigned long _ITEMS, unsigned long _ITEM_WIDTH,
|
|
|
|
bool _STRICT_WRITE = false>
|
|
|
|
|
|
|
|
struct Register_array : public Register<_OFFSET, _ACCESS_WIDTH,
|
|
|
|
_STRICT_WRITE>
|
2012-01-14 01:42:45 +00:00
|
|
|
{
|
2012-02-22 15:14:41 +00:00
|
|
|
typedef
|
|
|
|
typename Trait::Uint_type<_ACCESS_WIDTH>::template Divisor<_ITEM_WIDTH>
|
|
|
|
Item;
|
2012-01-17 17:55:24 +00:00
|
|
|
|
2012-01-14 01:42:45 +00:00
|
|
|
enum {
|
2012-05-24 09:56:54 +00:00
|
|
|
STRICT_WRITE = _STRICT_WRITE,
|
2012-01-17 17:55:24 +00:00
|
|
|
OFFSET = _OFFSET,
|
|
|
|
ACCESS_WIDTH = _ACCESS_WIDTH,
|
|
|
|
ITEMS = _ITEMS,
|
|
|
|
ITEM_WIDTH = _ITEM_WIDTH,
|
|
|
|
|
|
|
|
ITEM_WIDTH_LOG2 = Item::WIDTH_LOG2,
|
2012-01-14 01:42:45 +00:00
|
|
|
MAX_INDEX = ITEMS - 1,
|
|
|
|
ITEM_MASK = (1 << ITEM_WIDTH) - 1,
|
|
|
|
};
|
2012-01-09 12:29:03 +00:00
|
|
|
|
2012-05-24 09:56:54 +00:00
|
|
|
typedef
|
|
|
|
typename Register<OFFSET, ACCESS_WIDTH, STRICT_WRITE>::access_t
|
2012-02-22 15:14:41 +00:00
|
|
|
access_t;
|
2012-01-17 17:55:24 +00:00
|
|
|
|
2012-01-14 01:42:45 +00:00
|
|
|
/**
|
2012-01-17 17:55:24 +00:00
|
|
|
* A bitregion within a register array item
|
|
|
|
*
|
|
|
|
* \param _SHIFT Bit shift of the first bit within an item
|
|
|
|
* \param _WIDTH Bit width of the region
|
2012-01-14 01:42:45 +00:00
|
|
|
*
|
2012-02-22 15:14:41 +00:00
|
|
|
* \detail See 'Genode::Register::Bitfield'
|
2012-01-14 01:42:45 +00:00
|
|
|
*/
|
2012-01-17 17:55:24 +00:00
|
|
|
template <unsigned long _SHIFT, unsigned long _SIZE>
|
2012-02-22 15:14:41 +00:00
|
|
|
struct Bitfield :
|
2012-05-24 09:56:54 +00:00
|
|
|
public Register<OFFSET, ACCESS_WIDTH, STRICT_WRITE>::template Bitfield<_SHIFT, _SIZE>
|
2012-01-14 01:42:45 +00:00
|
|
|
{
|
2012-05-24 09:56:54 +00:00
|
|
|
/* Back reference to containing register array */
|
|
|
|
typedef Register_array<OFFSET, ACCESS_WIDTH, ITEMS,
|
|
|
|
ITEM_WIDTH, STRICT_WRITE>
|
2012-02-22 15:14:41 +00:00
|
|
|
Compound_array;
|
2012-01-09 12:29:03 +00:00
|
|
|
};
|
|
|
|
|
2012-01-14 01:42:45 +00:00
|
|
|
/**
|
2012-02-22 15:14:41 +00:00
|
|
|
* Calculate destination of an array-item access
|
|
|
|
*
|
|
|
|
* \param offset Gets overridden with the offset of the
|
|
|
|
* access type instance, that contains the
|
|
|
|
* access destination, relative to the MMIO
|
|
|
|
* base
|
|
|
|
* \param shift Gets overridden with the shift of the
|
|
|
|
* destination within the access type instance
|
|
|
|
* targeted by 'offset'
|
|
|
|
* \param index Index of the targeted array item
|
2012-01-14 01:42:45 +00:00
|
|
|
*/
|
2012-02-22 15:14:41 +00:00
|
|
|
static inline void access_dest(off_t & offset,
|
|
|
|
unsigned long & shift,
|
2012-01-14 01:42:45 +00:00
|
|
|
unsigned long const index)
|
|
|
|
{
|
|
|
|
unsigned long const bit_off = index << ITEM_WIDTH_LOG2;
|
2012-02-22 15:14:41 +00:00
|
|
|
offset = (off_t) ((bit_off >> BYTE_WIDTH_LOG2)
|
|
|
|
& ~(sizeof(access_t)-1) );
|
2012-01-14 01:42:45 +00:00
|
|
|
shift = bit_off - ( offset << BYTE_WIDTH_LOG2 );
|
2012-01-17 17:55:24 +00:00
|
|
|
offset += OFFSET;
|
2012-01-14 01:42:45 +00:00
|
|
|
}
|
|
|
|
};
|
2012-01-09 12:29:03 +00:00
|
|
|
|
|
|
|
addr_t const base;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*/
|
2012-01-14 01:42:45 +00:00
|
|
|
inline Mmio(addr_t mmio_base) : base(mmio_base) { }
|
|
|
|
|
|
|
|
|
|
|
|
/*************************
|
|
|
|
** Access to registers **
|
|
|
|
*************************/
|
2012-01-09 12:29:03 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Typed address of register 'REGISTER'
|
|
|
|
*/
|
|
|
|
template <typename REGISTER>
|
2012-05-24 09:56:54 +00:00
|
|
|
inline typename REGISTER::access_t volatile * typed_addr() const {
|
|
|
|
return (typename REGISTER::access_t volatile *)
|
|
|
|
base + REGISTER::OFFSET; }
|
2012-01-09 12:29:03 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Read the whole register 'REGISTER'
|
|
|
|
*/
|
|
|
|
template <typename REGISTER>
|
2012-05-24 09:56:54 +00:00
|
|
|
inline typename REGISTER::access_t read() const {
|
|
|
|
return _read<typename REGISTER::access_t>(REGISTER::OFFSET); }
|
2012-01-09 12:29:03 +00:00
|
|
|
|
2012-01-14 01:42:45 +00:00
|
|
|
/**
|
2012-01-17 08:47:23 +00:00
|
|
|
* Write 'value' to the register 'REGISTER'
|
2012-01-14 01:42:45 +00:00
|
|
|
*/
|
|
|
|
template <typename REGISTER>
|
2012-05-24 09:56:54 +00:00
|
|
|
inline void write(typename REGISTER::access_t const value) {
|
|
|
|
_write<typename REGISTER::access_t>(REGISTER::OFFSET, value); }
|
2012-01-14 01:42:45 +00:00
|
|
|
|
|
|
|
|
2012-01-17 08:47:23 +00:00
|
|
|
/******************************************
|
|
|
|
** Access to bitfields within registers **
|
|
|
|
******************************************/
|
2012-01-14 01:42:45 +00:00
|
|
|
|
2012-01-09 12:29:03 +00:00
|
|
|
/**
|
2012-01-17 08:47:23 +00:00
|
|
|
* Read the bitfield 'BITFIELD'
|
2012-01-09 12:29:03 +00:00
|
|
|
*/
|
2012-01-17 08:47:23 +00:00
|
|
|
template <typename BITFIELD>
|
2012-01-17 17:55:24 +00:00
|
|
|
inline typename BITFIELD::Compound_reg::access_t read() const;
|
2012-01-09 12:29:03 +00:00
|
|
|
|
|
|
|
/**
|
2012-01-17 08:47:23 +00:00
|
|
|
* Write value to the bitfield 'BITFIELD'
|
2012-01-09 12:29:03 +00:00
|
|
|
*/
|
2012-01-17 08:47:23 +00:00
|
|
|
template <typename BITFIELD>
|
2012-02-22 15:14:41 +00:00
|
|
|
inline void
|
|
|
|
write(typename BITFIELD::Compound_reg::access_t const value);
|
2012-01-09 12:29:03 +00:00
|
|
|
|
2012-01-14 01:42:45 +00:00
|
|
|
|
|
|
|
/*******************************
|
|
|
|
** Access to register arrays **
|
|
|
|
*******************************/
|
2012-01-09 12:29:03 +00:00
|
|
|
|
|
|
|
/**
|
2012-01-17 08:47:23 +00:00
|
|
|
* Read the whole item 'index' of the array 'REGISTER_ARRAY'
|
2012-01-09 12:29:03 +00:00
|
|
|
*/
|
2012-01-17 08:47:23 +00:00
|
|
|
template <typename REGISTER_ARRAY>
|
2012-02-22 15:14:41 +00:00
|
|
|
inline typename REGISTER_ARRAY::access_t
|
|
|
|
read(unsigned long const index) const;
|
2012-01-09 12:29:03 +00:00
|
|
|
|
|
|
|
/**
|
2012-02-22 15:14:41 +00:00
|
|
|
* Write 'value' to item 'index' of the array 'REGISTER_ARRAY'
|
2012-01-09 12:29:03 +00:00
|
|
|
*/
|
2012-01-17 08:47:23 +00:00
|
|
|
template <typename REGISTER_ARRAY>
|
2012-01-17 17:55:24 +00:00
|
|
|
inline void write(typename REGISTER_ARRAY::access_t const value,
|
2012-01-09 12:29:03 +00:00
|
|
|
unsigned long const index);
|
|
|
|
|
2012-01-14 01:42:45 +00:00
|
|
|
|
2012-01-17 08:47:23 +00:00
|
|
|
/*****************************************************
|
|
|
|
** Access to bitfields within register array items **
|
|
|
|
*****************************************************/
|
2012-01-14 01:42:45 +00:00
|
|
|
|
2012-01-09 12:29:03 +00:00
|
|
|
/**
|
2012-02-22 15:14:41 +00:00
|
|
|
* Read the bitfield 'ARRAY_BITFIELD' of item 'index' of the
|
|
|
|
* compound reg array
|
2012-01-09 12:29:03 +00:00
|
|
|
*/
|
2012-01-17 08:47:23 +00:00
|
|
|
template <typename ARRAY_BITFIELD>
|
2012-02-22 15:14:41 +00:00
|
|
|
inline typename ARRAY_BITFIELD::Compound_array::access_t
|
|
|
|
read(unsigned long const index) const;
|
2012-01-09 12:29:03 +00:00
|
|
|
|
|
|
|
/**
|
2012-02-22 15:14:41 +00:00
|
|
|
* Write 'value' to bitfield 'ARRAY_BITFIELD' of item 'index' of
|
|
|
|
* the compound reg array
|
2012-01-09 12:29:03 +00:00
|
|
|
*/
|
2012-01-17 08:47:23 +00:00
|
|
|
template <typename ARRAY_BITFIELD>
|
2012-02-22 15:14:41 +00:00
|
|
|
inline void
|
|
|
|
write(typename ARRAY_BITFIELD::Compound_array::access_t const value,
|
|
|
|
long unsigned const index);
|
2012-07-03 11:06:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
/*********************************
|
|
|
|
** Polling for bitfield states **
|
|
|
|
*********************************/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Interface for delaying the execution of a calling thread
|
|
|
|
*/
|
|
|
|
struct Delayer
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Delay the execution of the caller for the specified amount
|
|
|
|
* of microseconds
|
|
|
|
*/
|
|
|
|
virtual void usleep(unsigned us) = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Wait until the 'BITFIELD' contains the specified 'value'
|
|
|
|
*
|
|
|
|
* \param value value to wait for
|
|
|
|
* \param delayer sleeping facility to be used when the
|
|
|
|
* value is not reached yet
|
|
|
|
* \param max_attempts number of bitfield probing attempts
|
|
|
|
* \param us number of microseconds between attempts
|
|
|
|
*/
|
|
|
|
template <typename BITFIELD>
|
|
|
|
inline bool
|
|
|
|
wait_for(typename BITFIELD::Compound_reg::access_t const value,
|
|
|
|
Delayer &delayer,
|
|
|
|
unsigned max_attempts = 500,
|
|
|
|
unsigned us = 1000)
|
|
|
|
{
|
|
|
|
for (unsigned i = 0; i < max_attempts; i++, delayer.usleep(us))
|
|
|
|
if (read<BITFIELD>() == value)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2012-01-09 12:29:03 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-01-17 08:47:23 +00:00
|
|
|
/******************************************
|
|
|
|
** Access to bitfields within registers **
|
|
|
|
******************************************/
|
2012-01-14 01:42:45 +00:00
|
|
|
|
|
|
|
|
2012-01-17 08:47:23 +00:00
|
|
|
template <typename BITFIELD>
|
2012-01-17 17:55:24 +00:00
|
|
|
typename BITFIELD::Compound_reg::access_t Genode::Mmio::read() const
|
2012-01-09 12:29:03 +00:00
|
|
|
{
|
2012-01-17 08:47:23 +00:00
|
|
|
typedef typename BITFIELD::Compound_reg Register;
|
2012-01-17 17:55:24 +00:00
|
|
|
typedef typename Register::access_t access_t;
|
|
|
|
return BITFIELD::get(_read<access_t>(Register::OFFSET));
|
2012-01-09 12:29:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-01-17 08:47:23 +00:00
|
|
|
template <typename BITFIELD>
|
2012-01-17 17:55:24 +00:00
|
|
|
void Genode::Mmio::write(typename BITFIELD::Compound_reg::access_t const value)
|
2012-01-09 12:29:03 +00:00
|
|
|
{
|
2012-05-24 09:56:54 +00:00
|
|
|
/* Initialize the pattern that is written finally to the register */
|
2012-01-17 08:47:23 +00:00
|
|
|
typedef typename BITFIELD::Compound_reg Register;
|
2012-05-24 09:56:54 +00:00
|
|
|
typename Register::access_t write_value;
|
|
|
|
if (Register::STRICT_WRITE)
|
|
|
|
{
|
|
|
|
/* We must only apply the bitfield to an empty write pattern */
|
|
|
|
write_value = 0;
|
|
|
|
} else {
|
2012-01-09 12:29:03 +00:00
|
|
|
|
2012-05-24 09:56:54 +00:00
|
|
|
/* We've got to apply the bitfield to the old register value */
|
|
|
|
write_value = read<Register>();
|
|
|
|
BITFIELD::clear(write_value);
|
|
|
|
}
|
|
|
|
/* Apply bitfield value and override register */
|
|
|
|
BITFIELD::set(write_value, value);
|
|
|
|
write<Register>(write_value);
|
2012-01-09 12:29:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-01-14 01:42:45 +00:00
|
|
|
/************************************
|
|
|
|
** Access to register array items **
|
|
|
|
************************************/
|
|
|
|
|
|
|
|
|
2012-01-17 08:47:23 +00:00
|
|
|
template <typename REGISTER_ARRAY>
|
2012-02-22 15:14:41 +00:00
|
|
|
typename REGISTER_ARRAY::access_t
|
|
|
|
Genode::Mmio::read(unsigned long const index) const
|
2012-01-09 12:29:03 +00:00
|
|
|
{
|
2012-05-24 09:56:54 +00:00
|
|
|
/* Reads outside the array return 0 */
|
2012-01-17 08:47:23 +00:00
|
|
|
if (index > REGISTER_ARRAY::MAX_INDEX) return 0;
|
2012-01-09 12:29:03 +00:00
|
|
|
|
2012-05-24 09:56:54 +00:00
|
|
|
/* If item width equals access width we optimize the access */
|
2012-01-14 01:42:45 +00:00
|
|
|
off_t offset;
|
2012-01-17 17:55:24 +00:00
|
|
|
if (REGISTER_ARRAY::ITEM_WIDTH == REGISTER_ARRAY::ACCESS_WIDTH) {
|
2012-02-22 15:14:41 +00:00
|
|
|
offset = REGISTER_ARRAY::OFFSET
|
|
|
|
+ (index << REGISTER_ARRAY::ITEM_WIDTH_LOG2);
|
2012-01-17 17:55:24 +00:00
|
|
|
return _read<typename REGISTER_ARRAY::access_t>(offset);
|
2012-01-09 12:29:03 +00:00
|
|
|
|
2012-05-24 09:56:54 +00:00
|
|
|
/* Access width and item width differ */
|
2012-01-14 01:42:45 +00:00
|
|
|
} else {
|
|
|
|
long unsigned shift;
|
2012-01-17 08:47:23 +00:00
|
|
|
REGISTER_ARRAY::access_dest(offset, shift, index);
|
2012-02-22 15:14:41 +00:00
|
|
|
return (_read<typename REGISTER_ARRAY::access_t>(offset) >> shift)
|
|
|
|
& REGISTER_ARRAY::ITEM_MASK;
|
2012-01-14 01:42:45 +00:00
|
|
|
}
|
2012-01-09 12:29:03 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-01-17 08:47:23 +00:00
|
|
|
template <typename REGISTER_ARRAY>
|
2012-01-17 17:55:24 +00:00
|
|
|
void Genode::Mmio::write(typename REGISTER_ARRAY::access_t const value,
|
2012-01-09 12:29:03 +00:00
|
|
|
unsigned long const index)
|
|
|
|
{
|
2012-05-24 09:56:54 +00:00
|
|
|
/* Writes outside the array are ignored */
|
2012-01-17 08:47:23 +00:00
|
|
|
if (index > REGISTER_ARRAY::MAX_INDEX) return;
|
2012-01-09 12:29:03 +00:00
|
|
|
|
2012-05-24 09:56:54 +00:00
|
|
|
/* If item width equals access width we optimize the access */
|
2012-01-14 01:42:45 +00:00
|
|
|
off_t offset;
|
2012-01-17 17:55:24 +00:00
|
|
|
if (REGISTER_ARRAY::ITEM_WIDTH == REGISTER_ARRAY::ACCESS_WIDTH) {
|
2012-02-22 15:14:41 +00:00
|
|
|
offset = REGISTER_ARRAY::OFFSET
|
|
|
|
+ (index << REGISTER_ARRAY::ITEM_WIDTH_LOG2);
|
2012-01-17 17:55:24 +00:00
|
|
|
_write<typename REGISTER_ARRAY::access_t>(offset, value);
|
2012-01-09 12:29:03 +00:00
|
|
|
|
2012-05-24 09:56:54 +00:00
|
|
|
/* Access width and item width differ */
|
2012-01-14 01:42:45 +00:00
|
|
|
} else {
|
|
|
|
long unsigned shift;
|
2012-01-17 08:47:23 +00:00
|
|
|
REGISTER_ARRAY::access_dest(offset, shift, index);
|
2012-01-09 12:29:03 +00:00
|
|
|
|
2012-05-24 09:56:54 +00:00
|
|
|
/* Insert new value into old register value */
|
|
|
|
typename REGISTER_ARRAY::access_t write_value;
|
|
|
|
if (REGISTER_ARRAY::STRICT_WRITE)
|
|
|
|
{
|
|
|
|
/* We must only apply the bitfield to an empty write pattern */
|
|
|
|
write_value = 0;
|
|
|
|
} else {
|
|
|
|
|
|
|
|
/* We've got to apply the bitfield to the old register value */
|
|
|
|
write_value = _read<typename REGISTER_ARRAY::access_t>(offset);
|
|
|
|
write_value &= ~(REGISTER_ARRAY::ITEM_MASK << shift);
|
|
|
|
}
|
|
|
|
/* Apply bitfield value and override register */
|
|
|
|
write_value |= (value & REGISTER_ARRAY::ITEM_MASK) << shift;
|
|
|
|
_write<typename REGISTER_ARRAY::access_t>(offset, write_value);
|
2012-01-14 01:42:45 +00:00
|
|
|
}
|
2012-01-09 12:29:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-01-17 08:47:23 +00:00
|
|
|
/*****************************************************
|
|
|
|
** Access to bitfields within register array items **
|
|
|
|
*****************************************************/
|
2012-01-14 01:42:45 +00:00
|
|
|
|
|
|
|
|
2012-01-17 08:47:23 +00:00
|
|
|
template <typename ARRAY_BITFIELD>
|
2012-02-22 15:14:41 +00:00
|
|
|
void
|
|
|
|
Genode::Mmio::write(typename ARRAY_BITFIELD::Compound_array::access_t const value,
|
|
|
|
long unsigned const index)
|
2012-01-09 12:29:03 +00:00
|
|
|
{
|
2012-05-24 09:56:54 +00:00
|
|
|
/* Initialize the pattern that is finally written to the register */
|
2012-01-17 08:47:23 +00:00
|
|
|
typedef typename ARRAY_BITFIELD::Compound_array Register_array;
|
2012-05-24 09:56:54 +00:00
|
|
|
typename Register_array::access_t write_value;
|
|
|
|
if (Register_array::STRICT_WRITE)
|
|
|
|
{
|
|
|
|
/* We must only apply the bitfield to an empty write pattern */
|
|
|
|
write_value = 0;
|
|
|
|
} else {
|
2012-01-09 12:29:03 +00:00
|
|
|
|
2012-05-24 09:56:54 +00:00
|
|
|
/* We've got to apply the bitfield to the old register value */
|
|
|
|
write_value = read<Register_array>(index);
|
|
|
|
ARRAY_BITFIELD::clear(write_value);
|
|
|
|
}
|
|
|
|
/* Apply bitfield value and override register */
|
|
|
|
ARRAY_BITFIELD::set(write_value, value);
|
|
|
|
write<Register_array>(write_value, index);
|
2012-01-14 01:42:45 +00:00
|
|
|
}
|
2012-01-09 12:29:03 +00:00
|
|
|
|
|
|
|
|
2012-01-17 08:47:23 +00:00
|
|
|
template <typename ARRAY_BITFIELD>
|
2012-02-22 15:14:41 +00:00
|
|
|
typename ARRAY_BITFIELD::Compound_array::access_t
|
|
|
|
Genode::Mmio::read(long unsigned const index) const
|
2012-01-14 01:42:45 +00:00
|
|
|
{
|
2012-01-17 08:47:23 +00:00
|
|
|
typedef typename ARRAY_BITFIELD::Compound_array Array;
|
2012-01-17 17:55:24 +00:00
|
|
|
typedef typename Array::access_t access_t;
|
2012-01-17 08:47:23 +00:00
|
|
|
return ARRAY_BITFIELD::get(read<Array>(index));
|
2012-01-09 12:29:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endif /* _BASE__INCLUDE__UTIL__MMIO_H_ */
|
2012-01-09 20:05:49 +00:00
|
|
|
|