* arch/arm/src/sam34/sam_rtt.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <nuttx/arch.h>
#include <sys/types.h>
#include <stdint.h>
#include <assert.h>
#include <errno.h>
#include <debug.h>
#include <nuttx/irq.h>
#include <nuttx/timers/timer.h>
#include <arch/board/board.h>
#include "arm_internal.h"
#include "sam_rtt.h"
#include "sam_periphclks.h"
#if defined(CONFIG_TIMER) && (defined(CONFIG_SAM34_RTT))
* Pre-processor Definitions
****************************************************************************/
#if defined(CONFIG_RTC_HIRES) && defined (CONFIG_SAM34_RTC)
# define RTT_PRES (32768/CONFIG_RTC_FREQUENCY)
#else
# define RTT_PRES 1
#endif
#define RTT_FCLK (BOARD_SCLK_FREQUENCY/RTT_PRES)
#define RTT_MAXTIMEOUT ((1000000ULL * (0x100000000ULL)) / RTT_FCLK)
#ifndef CONFIG_DEBUG_TIMER_INFO
# undef CONFIG_SAM34_RTT_REGDEBUG
#endif
* Private Types
****************************************************************************/
* driver state structure. This structure must be cast-compatible with the
* timer_lowerhalf_s structure.
*/
struct sam34_lowerhalf_s
{
const struct timer_ops_s *ops;
tccb_t callback;
void *arg;
uint32_t timeout;
uint32_t clkticks;
uint32_t val;
uint32_t adjustment;
bool started;
};
* Private Function Prototypes
****************************************************************************/
#ifdef CONFIG_SAM34_RTT_REGDEBUG
static uint32_t sam34_getreg(uint32_t addr);
static void sam34_putreg(uint32_t val, uint32_t addr);
#else
# define sam34_getreg(addr) getreg32(addr)
# define sam34_putreg(val,addr) putreg32(val,addr)
#endif
static int sam34_interrupt(int irq, void *context, void *arg);
static int sam34_start(struct timer_lowerhalf_s *lower);
static int sam34_stop(struct timer_lowerhalf_s *lower);
static int sam34_getstatus(struct timer_lowerhalf_s *lower,
struct timer_status_s *status);
static int sam34_settimeout(struct timer_lowerhalf_s *lower,
uint32_t timeout);
static void sam34_setcallback(struct timer_lowerhalf_s *lower,
tccb_t callback, void *arg);
static int sam34_ioctl(struct timer_lowerhalf_s *lower, int cmd,
unsigned long arg);
* Private Data
****************************************************************************/
static const struct timer_ops_s g_tcops =
{
.start = sam34_start,
.stop = sam34_stop,
.getstatus = sam34_getstatus,
.settimeout = sam34_settimeout,
.setcallback = sam34_setcallback,
.ioctl = sam34_ioctl,
};
static struct sam34_lowerhalf_s g_tcdev;
* Private Functions
****************************************************************************/
* Name: sam34_readvr
*
* Description:
* Get the contents of the value register.
*
****************************************************************************/
static inline uint32_t sam34_readvr(void)
{
register uint32_t v;
do
{
v = getreg32(SAM_RTT_VR);
}
while (v != getreg32(SAM_RTT_VR));
return v;
}
* Name: sam34_getreg
*
* Description:
* Get the contents of a register
*
****************************************************************************/
#ifdef CONFIG_SAM34_RTT_REGDEBUG
static uint32_t sam34_getreg(uint32_t addr)
{
static uint32_t prevaddr = 0;
static uint32_t count = 0;
static uint32_t preval = 0;
uint32_t val = getreg32(addr);
* Are we polling the register? If so, suppress some of the output.
*/
if (addr == prevaddr && val == preval)
{
if (count == 0xffffffff || ++count > 3)
{
if (count == 4)
{
tmrinfo("...\n");
}
return val;
}
}
else
{
if (count > 3)
{
tmrinfo("[repeats %d more times]\n", count - 3);
}
prevaddr = addr;
preval = val;
count = 1;
}
tmrinfo("%08lx->%08lx\n", addr, val);
return val;
}
#endif
* Name: sam34_putreg
*
* Description:
* Set the contents of an SAM34 register to a value
*
****************************************************************************/
#ifdef CONFIG_SAM34_RTT_REGDEBUG
static void sam34_putreg(uint32_t val, uint32_t addr)
{
tmrinfo("%08lx<-%08lx\n", addr, val);
putreg32(val, addr);
}
#endif
* Name: sam34_interrupt
*
* Description:
* TC interrupt
*
* Input Parameters:
* Usual interrupt handler arguments.
*
* Returned Value:
* Always returns OK.
*
****************************************************************************/
static int sam34_interrupt(int irq, void *context, void *arg)
{
struct sam34_lowerhalf_s *priv = (struct sam34_lowerhalf_s *)arg;
tmrinfo("Entry\n");
DEBUGASSERT(priv != NULL);
if ((sam34_getreg(SAM_RTT_SR) & RTT_SR_ALMS) != 0)
{
uint32_t timeout;
uint32_t mrval;
uint32_t vr;
uint32_t lateticks;
if (priv->callback && priv->callback(&priv->timeout, priv->arg))
{
mrval = sam34_getreg(SAM_RTT_MR);
sam34_putreg(mrval & ~RTT_MR_ALMIEN, SAM_RTT_MR);
vr = sam34_readvr();
priv->clkticks =
((uint64_t)(priv->adjustment + priv->timeout)) *
RTT_FCLK / 1000000;
* TODO calculate lost ticks?
*/
lateticks = vr - priv->val;
if (lateticks <= (priv->clkticks >> 1))
{
priv->clkticks -= lateticks;
}
priv->val = vr + priv->clkticks;
sam34_putreg(priv->val - 1, SAM_RTT_AR);
sam34_putreg(mrval, SAM_RTT_MR);
timeout = (1000000ULL * priv->clkticks) / RTT_FCLK;
priv->adjustment = (priv->adjustment + priv->timeout) - timeout;
}
else
{
sam34_stop((struct timer_lowerhalf_s *)priv);
}
}
return OK;
}
* Name: sam34_start
*
* Description:
* Start the timer, resetting the time to the current timeout,
*
* Input Parameters:
* lower - A pointer the publicly visible representation of the
* "lower-half" driver state structure.
*
* Returned Value:
* Zero on success; a negated errno value on failure.
*
****************************************************************************/
static int sam34_start(struct timer_lowerhalf_s *lower)
{
struct sam34_lowerhalf_s *priv = (struct sam34_lowerhalf_s *)lower;
uint32_t mr;
uint32_t vr;
tmrinfo("Entry\n");
DEBUGASSERT(priv);
if (priv->started)
{
return -EINVAL;
}
#if defined(CONFIG_RTC_HIRES) && defined (CONFIG_SAM34_RTC)
mr = sam34_getreg(SAM_RTT_MR) & ~RTT_MR_ALMIEN;
sam34_putreg(mr, SAM_RTT_MR);
vr = sam34_readvr();
#else
sam_rtt_enableclk();
mr = RTT_MR_RTPRES(RTT_PRES);
sam34_putreg(mr, SAM_RTT_MR);
vr = 0;
#endif
priv->val = vr + priv->clkticks;
sam34_putreg(priv->val - 1, SAM_RTT_AR);
if (priv->callback)
{
sam34_getreg(SAM_RTT_SR);
mr |= RTT_MR_ALMIEN;
sam34_putreg(mr, SAM_RTT_MR);
}
#if !(defined(CONFIG_RTC_HIRES) && defined (CONFIG_SAM34_RTC))
sam34_putreg(mr | RTT_MR_RTTRST, SAM_RTT_MR);
#endif
priv->started = true;
return OK;
}
* Name: sam34_stop
*
* Description:
* Stop the timer
*
* Input Parameters:
* lower - A pointer the publicly visible representation of the
* "lower-half" driver state structure.
*
* Returned Value:
* Zero on success; a negated errno value on failure.
*
****************************************************************************/
static int sam34_stop(struct timer_lowerhalf_s *lower)
{
struct sam34_lowerhalf_s *priv = (struct sam34_lowerhalf_s *)lower;
tmrinfo("Entry\n");
DEBUGASSERT(priv);
if (!priv->started)
{
return -EINVAL;
}
#if !(defined(CONFIG_RTC_HIRES) && defined (CONFIG_SAM34_RTC))
#if defined(RTT_MR_RTTDIS)
sam34_putreg(RTT_MR_RTTDIS, SAM_RTT_MR);
#endif
sam_rtt_disableclk();
#endif
priv->started = false;
return OK;
}
* Name: sam34_getstatus
*
* Description:
* Get the current timer status
*
* Input Parameters:
* lower - A pointer the publicly visible representation of the
* "lower-half" driver state structure.
* status - The location to return the status information.
*
* Returned Value:
* Zero on success; a negated errno value on failure.
*
****************************************************************************/
static int sam34_getstatus(struct timer_lowerhalf_s *lower,
struct timer_status_s *status)
{
struct sam34_lowerhalf_s *priv = (struct sam34_lowerhalf_s *)lower;
tmrinfo("Entry\n");
DEBUGASSERT(priv);
status->flags = 0;
if (priv->started)
{
status->flags |= TCFLAGS_ACTIVE;
}
if (priv->callback)
{
status->flags |= TCFLAGS_HANDLER;
}
status->timeout = priv->timeout;
status->timeleft = 1000000ULL*(sam34_getreg(SAM_RTT_AR) -
sam34_readvr()) / RTT_FCLK;
tmrinfo(" flags : %08x\n", status->flags);
tmrinfo(" timeout : %d\n", status->timeout);
tmrinfo(" timeleft : %d\n", status->timeleft);
return OK;
}
* Name: sam34_settimeout
*
* Description:
* Set a new timeout value (and reset the timer)
*
* Input Parameters:
* lower - A pointer the publicly visible representation of the
* "lower-half" driver state structure.
* timeout - The new timeout value in milliseconds.
*
* Returned Value:
* Zero on success; a negated errno value on failure.
*
****************************************************************************/
static int sam34_settimeout(struct timer_lowerhalf_s *lower,
uint32_t timeout)
{
struct sam34_lowerhalf_s *priv = (struct sam34_lowerhalf_s *)lower;
DEBUGASSERT(priv);
tmrinfo("Entry: timeout=%d\n", timeout);
if (priv->started)
{
return -EPERM;
}
if (timeout < 1 || timeout > RTT_MAXTIMEOUT)
{
tmrerr("ERROR: Cannot represent timeout=%lu > %lu\n",
timeout, RTT_MAXTIMEOUT);
return -ERANGE;
}
priv->timeout = timeout;
priv->clkticks = (((uint64_t)timeout * RTT_FCLK) / 1000000);
timeout = (1000000ULL * priv->clkticks) / RTT_FCLK;
priv->adjustment = priv->timeout - timeout;
tmrinfo("fclk=%d clkticks=%d timeout=%d, adjustment=%d\n",
RTT_FCLK, priv->clkticks, priv->timeout, priv->adjustment);
return OK;
}
* Name: sam34_setcallback
*
* Description:
* Call this user provided timeout callback.
*
* Input Parameters:
* lower - A pointer the publicly visible representation of the
* "lower-half" driver state structure.
* callback - The new timer expiration function pointer. If this
* function pointer is NULL, then the reset-on-expiration
* behavior is restored,
* arg - Argument that will be provided in the callback
*
* Returned Value:
* The previous timer expiration function pointer or NULL is there was
* no previous function pointer.
*
****************************************************************************/
static void sam34_setcallback(struct timer_lowerhalf_s *lower,
tccb_t callback, void *arg)
{
struct sam34_lowerhalf_s *priv = (struct sam34_lowerhalf_s *)lower;
irqstate_t flags;
flags = enter_critical_section();
DEBUGASSERT(priv);
tmrinfo("Entry: callback=%p\n", callback);
priv->callback = callback;
priv->arg = arg;
leave_critical_section(flags);
}
* Name: sam34_ioctl
*
* Description:
* Any ioctl commands that are not recognized by the "upper-half" driver
* are forwarded to the lower half driver through this method.
*
* Input Parameters:
* lower - A pointer the publicly visible representation of the
* "lower-half" driver state structure.
* cmd - The ioctl command value
* arg - The optional argument that accompanies the 'cmd'. The
* interpretation of this argument depends on the particular
* command.
*
* Returned Value:
* Zero on success; a negated errno value on failure.
*
****************************************************************************/
static int sam34_ioctl(struct timer_lowerhalf_s *lower, int cmd,
unsigned long arg)
{
struct sam34_lowerhalf_s *priv = (struct sam34_lowerhalf_s *)lower;
int ret = -ENOTTY;
DEBUGASSERT(priv);
tmrinfo("Entry: cmd=%d arg=%ld\n", cmd, arg);
UNUSED(priv);
return ret;
}
* Public Functions
****************************************************************************/
* Name: sam_rttinitialize
*
* Description:
* Initialize the timer. The timer is initialized and registers as
* 'devpath'.
*
* Input Parameters:
* devpath - The full path to the timer. This should be of the form
* /dev/rtt0
*
* Returned Value:
* None
*
****************************************************************************/
void sam_rttinitialize(const char *devpath)
{
struct sam34_lowerhalf_s *priv = &g_tcdev;
tmrinfo("Entry: devpath=%s\n", devpath);
* structure lies in .bss and was zeroed at reset time. (2) This function
* is only called once so it is never necessary to re-zero the structure.
*/
priv->ops = &g_tcops;
irq_attach(SAM_IRQ_RTT, sam34_interrupt, priv);
up_enable_irq(SAM_IRQ_RTT);
timer_register(devpath, (struct timer_lowerhalf_s *)priv);
}
#endif