genode/os/include/drivers/timer/sp804_base.h

184 lines
4.5 KiB
C
Raw Normal View History

2012-05-24 10:57:36 +00:00
/*
* \brief Basic driver for the ARM SP804 timer
* \author Martin stein
* \date 2012-04-23
*/
/*
* Copyright (C) 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-05-25 17:28:09 +00:00
#ifndef _INCLUDE__DRIVERS__TIMER__SP804_H_
#define _INCLUDE__DRIVERS__TIMER__SP804_H_
2012-05-24 10:57:36 +00:00
/* Genode includes */
#include <util/mmio.h>
namespace Genode
{
/**
* Basic driver for the ARM SP804 timer
*/
template <unsigned long CLK>
class Sp804_base : public Mmio
{
enum {
TICS_PER_MS = CLK / 1000,
TICS_PER_US = TICS_PER_MS / 1000,
AVOID_INVALID_TEMPLATE_ARGS = 1 / TICS_PER_US,
};
/**
* Holds value that shall be loaded to the timer value register
*/
struct Load : Register<0x0, 32> { };
/**
* Raw interrupt status
*/
struct Ris : Register<0x10, 1> { };
/**
* Background load register
*/
struct Bgload : Register<0x18, 32> { };
/**
* Timer value register
*/
struct Value : Register<0x4, 32> { enum { MAX_VALUE = 0xffffffff }; };
/**
* Timer control register
*/
struct Control : Register<0x8, 8>
{
struct Oneshot : Bitfield<0,1> { };
struct Size : Bitfield<1,1> { };
struct Pre : Bitfield<2,2> { };
struct Int_en : Bitfield<5,1> { };
struct Mode : Bitfield<6,1> { };
struct Timer_en : Bitfield<7,1> { };
};
/**
* Clears the timer interrupt
*/
struct Int_clr : Register<0xc, 1> { };
public:
/**
* Constructor, clears interrupt output
*/
Sp804_base(addr_t const mmio_base) : Mmio(mmio_base) {
clear_interrupt(); }
/**
* Run the timer in order that it raises IRQ when
* it reaches zero, then stop
*
2012-05-25 17:28:09 +00:00
* \param tics native timer value used to assess the delay
2012-05-24 10:57:36 +00:00
* of the timer interrupt as of this call
*/
void run_and_stop(unsigned long const tics)
{
2012-05-25 17:28:09 +00:00
/* disable and configure timer for a one-shot */
2012-05-24 10:57:36 +00:00
clear_interrupt();
write<typename Control::Timer_en>(0);
write<Control>(Control::Timer_en::bits(0) |
Control::Mode::bits(1) |
Control::Int_en::bits(1) |
Control::Pre::bits(0) |
Control::Size::bits(1) |
Control::Oneshot::bits(1));
2012-05-25 17:28:09 +00:00
/* load value and enable timer */
2012-05-24 10:57:36 +00:00
write<Load>(tics);
write<typename Control::Timer_en>(1);
}
/**
2012-05-25 17:28:09 +00:00
* Run the timer in order that it raises IRQ when it reaches zero,
* then wrap and continue
2012-05-24 10:57:36 +00:00
*
2012-05-25 17:28:09 +00:00
* \param tics native timer value used to assess the delay
2012-05-24 10:57:36 +00:00
* of the timer interrupt as of this call
*/
void run_and_wrap(unsigned long const tics)
{
2012-05-25 17:28:09 +00:00
/* configure the timer in order that it reloads on 0 */
2012-05-24 10:57:36 +00:00
clear_interrupt();
write<typename Control::Timer_en>(0);
write<Control>(Control::Timer_en::bits(0) |
Control::Mode::bits(1) |
Control::Int_en::bits(1) |
Control::Pre::bits(0) |
Control::Size::bits(1) |
Control::Oneshot::bits(0));
2012-05-25 17:28:09 +00:00
/* start timer with the initial value */
2012-05-24 10:57:36 +00:00
write<Load>(tics);
write<typename Control::Timer_en>(1);
2012-05-25 17:28:09 +00:00
/*
* Ensure that the timer loads its max value instead of the
* initial value when it reaches 0 in order that it looks like
* it wraps.
*/
2012-05-24 10:57:36 +00:00
write<Bgload>(max_value());
}
/**
* Current timer value
*/
unsigned long value() const { return read<Value>(); }
/**
* Get timer value and corresponding wrapped status of timer
*/
unsigned long value(bool & wrapped) const
{
typename Value::access_t v = read<Value>();
wrapped = (bool)read<Ris>();
if (!wrapped) return v;
return read<Value>();
}
/**
* Clear interrupt output line
*/
void clear_interrupt() { write<Int_clr>(1); }
/**
* Translate milliseconds to a native timer value
*/
static unsigned long ms_to_tics(unsigned long const ms) {
return ms * TICS_PER_MS; }
/**
* Translate native timer value to microseconds
*/
static unsigned long tics_to_us(unsigned long const tics) {
return tics / TICS_PER_US; }
/**
* Translate microseconds to a native timer value
*/
static unsigned long us_to_tics(unsigned long const us) {
return us * TICS_PER_US; }
2012-05-24 10:57:36 +00:00
/**
* Translate native timer value to microseconds
*/
static unsigned long max_value() { return Value::MAX_VALUE; }
};
}
2012-05-25 17:28:09 +00:00
#endif /* _INCLUDE__DRIVERS__TIMER__SP804_H_ */
2012-05-24 10:57:36 +00:00