2017-07-07 20:09:39 +02:00
|
|
|
/*
|
|
|
|
* \brief Time source that uses the Enhanced Periodic Interrupt Timer (Freescale)
|
|
|
|
* \author Norman Feske
|
|
|
|
* \author Martin Stein
|
|
|
|
* \author Stefan Kalkowski
|
|
|
|
* \date 2009-06-16
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (C) 2009-2017 Genode Labs GmbH
|
|
|
|
*
|
|
|
|
* This file is part of the Genode OS framework, which is distributed
|
|
|
|
* under the terms of the GNU Affero General Public License version 3.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _TIME_SOURCE_H_
|
|
|
|
#define _TIME_SOURCE_H_
|
|
|
|
|
|
|
|
/* Genode includes */
|
|
|
|
#include <irq_session/connection.h>
|
2017-11-24 13:47:53 +01:00
|
|
|
#include <os/attached_mmio.h>
|
|
|
|
#include <drivers/timer/util.h>
|
2017-07-07 20:09:39 +02:00
|
|
|
|
|
|
|
/* local includes */
|
|
|
|
#include <signalled_time_source.h>
|
|
|
|
|
2017-11-24 13:47:53 +01:00
|
|
|
namespace Timer { class Time_source; }
|
2017-07-07 20:09:39 +02:00
|
|
|
|
|
|
|
|
2017-11-24 13:47:53 +01:00
|
|
|
class Timer::Time_source : public Genode::Attached_mmio,
|
|
|
|
public Genode::Signalled_time_source
|
2017-07-07 20:09:39 +02:00
|
|
|
{
|
|
|
|
private:
|
|
|
|
|
2017-11-24 13:47:53 +01:00
|
|
|
enum { TICKS_PER_MS = 66000 };
|
|
|
|
|
|
|
|
struct Cr : Register<0x0, 32>
|
|
|
|
{
|
|
|
|
struct En : Bitfield<0, 1> { };
|
|
|
|
struct En_mod : Bitfield<1, 1> { enum { RELOAD = 1 }; };
|
|
|
|
struct Oci_en : Bitfield<2, 1> { };
|
|
|
|
struct Swr : Bitfield<16, 1> { };
|
|
|
|
struct Clk_src : Bitfield<24, 2> { enum { HIGH_FREQ_REF_CLK = 2 }; };
|
|
|
|
|
|
|
|
static access_t prepare_one_shot()
|
|
|
|
{
|
|
|
|
access_t cr = 0;
|
|
|
|
En_mod::set(cr, En_mod::RELOAD);
|
|
|
|
Oci_en::set(cr, 1);
|
|
|
|
Clk_src::set(cr, Clk_src::HIGH_FREQ_REF_CLK);
|
|
|
|
return cr;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Sr : Register<0x4, 32> { struct Ocif : Bitfield<0, 1> { }; };
|
|
|
|
struct Cmpr : Register<0xc, 32> { };
|
timer epit: fix multi-wraps and bug in rate limit
Multi-wraps
-----------
Previously, on every new timeout, we programmed registers LR=timeout and
CMP=0. The counter than counted from LR down to 0, triggered the IRQ,
jumped back to LR, and counted down again. If one installed small
timeouts (< 1000 us), it was likely that the counter wrapped multiple
times before we were able to read it out. Initially, this was not a big
issue as the additional wraps were simply ignored and the amount of time
lost through this was not big. But when we want to do correct rate
limitation, multiple wraps cause an overflow in the additional
calculations, and this has a big effect on the resulting time value.
Thus, we now program the counter to start from ~0 and count down to 0.
We set CMP=~0-timeout so that the timer still triggers the IRQ at the right
time. The counter continues counting down after the IRQ has triggered until
we install a new timeout. We do not consider anymore that the counter wraps.
The maximum timeout is set to half the maximum counter value, so, we should
be able to install a new timeout before the counter wraps.
Rate limit for time updates
---------------------------
In the time span between two interrupts we have to remember how many ticks
we have already added to the time value. This is because at each call of
curr_time we can only see how many ticks have passed since the last call of
schedule_timeout and not since the last call of curr_time. But we want to
limit the rate of time updates in curr_time. With the member for ticks that
were already added since the last call to schedule_timeout we can then
calculate how many are yet to be added.
2017-11-28 09:46:35 +01:00
|
|
|
struct Cnt : Register<0x10, 32> { enum { MAX = ~(access_t)0 }; };
|
2017-11-24 13:47:53 +01:00
|
|
|
|
|
|
|
Genode::Irq_connection _timer_irq;
|
timer epit: fix multi-wraps and bug in rate limit
Multi-wraps
-----------
Previously, on every new timeout, we programmed registers LR=timeout and
CMP=0. The counter than counted from LR down to 0, triggered the IRQ,
jumped back to LR, and counted down again. If one installed small
timeouts (< 1000 us), it was likely that the counter wrapped multiple
times before we were able to read it out. Initially, this was not a big
issue as the additional wraps were simply ignored and the amount of time
lost through this was not big. But when we want to do correct rate
limitation, multiple wraps cause an overflow in the additional
calculations, and this has a big effect on the resulting time value.
Thus, we now program the counter to start from ~0 and count down to 0.
We set CMP=~0-timeout so that the timer still triggers the IRQ at the right
time. The counter continues counting down after the IRQ has triggered until
we install a new timeout. We do not consider anymore that the counter wraps.
The maximum timeout is set to half the maximum counter value, so, we should
be able to install a new timeout before the counter wraps.
Rate limit for time updates
---------------------------
In the time span between two interrupts we have to remember how many ticks
we have already added to the time value. This is because at each call of
curr_time we can only see how many ticks have passed since the last call of
schedule_timeout and not since the last call of curr_time. But we want to
limit the rate of time updates in curr_time. With the member for ticks that
were already added since the last call to schedule_timeout we can then
calculate how many are yet to be added.
2017-11-28 09:46:35 +01:00
|
|
|
Genode::Duration _curr_time { Genode::Microseconds(0) };
|
|
|
|
Genode::Microseconds const _max_timeout { Genode::timer_ticks_to_us(Cnt::MAX / 2, TICKS_PER_MS) };
|
|
|
|
unsigned long _cleared_ticks { 0 };
|
2017-07-07 20:09:39 +02:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
Time_source(Genode::Env &env);
|
|
|
|
|
|
|
|
|
|
|
|
/*************************
|
|
|
|
** Genode::Time_source **
|
|
|
|
*************************/
|
|
|
|
|
2017-11-24 13:47:53 +01:00
|
|
|
Genode::Duration curr_time() override;
|
|
|
|
void schedule_timeout(Genode::Microseconds duration, Timeout_handler &handler) override;
|
|
|
|
Genode::Microseconds max_timeout() const override { return _max_timeout; };
|
2017-07-07 20:09:39 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* _TIME_SOURCE_H_ */
|