2009-01-05 00:00:29 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
2009-01-05 00:06:45 +00:00
|
|
|
* Copyright (C) 2009 by Michael Sevakis
|
2009-01-05 00:00:29 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
#include "config.h"
|
|
|
|
#include "system.h"
|
|
|
|
#include "timer.h"
|
2009-03-22 02:13:27 +00:00
|
|
|
#include "ccm-imx31.h"
|
2009-01-05 00:00:29 +00:00
|
|
|
#include "avic-imx31.h"
|
|
|
|
|
|
|
|
static void __attribute__((interrupt("IRQ"))) EPIT2_HANDLER(void)
|
|
|
|
{
|
|
|
|
EPITSR2 = EPITSR_OCIF; /* Clear the pending status */
|
|
|
|
|
|
|
|
if (pfn_timer != NULL)
|
|
|
|
pfn_timer();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void stop_timer(bool clock_off)
|
|
|
|
{
|
|
|
|
/* Ensure clock gating on (before touching any module registers) */
|
2009-03-22 01:50:48 +00:00
|
|
|
ccm_module_clock_gating(CG_EPIT2, CGM_ON_RUN_WAIT);
|
2009-01-05 00:00:29 +00:00
|
|
|
/* Disable insterrupt */
|
2009-03-22 01:50:48 +00:00
|
|
|
avic_disable_int(INT_EPIT2);
|
2009-01-05 00:00:29 +00:00
|
|
|
/* Clear wakeup mask */
|
2009-03-22 01:50:48 +00:00
|
|
|
CCM_WIMR0 &= ~CCM_WIMR0_IPI_INT_EPIT2;
|
2009-01-05 00:00:29 +00:00
|
|
|
/* Disable counter */
|
|
|
|
EPITCR2 &= ~(EPITCR_OCIEN | EPITCR_EN);
|
|
|
|
/* Clear pending */
|
|
|
|
EPITSR2 = EPITSR_OCIF;
|
|
|
|
|
|
|
|
if (clock_off)
|
|
|
|
{
|
|
|
|
/* Final stop, not reset; don't clock module any longer */
|
2009-03-22 01:50:48 +00:00
|
|
|
ccm_module_clock_gating(CG_EPIT2, CGM_OFF);
|
2009-01-05 00:00:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-29 14:29:57 +00:00
|
|
|
bool timer_set(long cycles, bool start)
|
2009-01-05 00:00:29 +00:00
|
|
|
{
|
|
|
|
/* Maximum cycle count expressible in the cycles parameter is 2^31-1
|
|
|
|
* and the modulus counter is capable of 2^32-1 and as a result there is
|
|
|
|
* no requirement to use a prescaler > 1. This gives a frequency range of
|
|
|
|
* ~0.015366822Hz - 66000000Hz. The highest input frequency gives the
|
|
|
|
* greatest possible accuracy anyway. */
|
|
|
|
int oldstatus = disable_interrupt_save(IRQ_FIQ_STATUS);
|
|
|
|
|
|
|
|
/* Halt timer if running - leave module clock enabled */
|
|
|
|
stop_timer(false);
|
|
|
|
|
|
|
|
if (start && pfn_unregister != NULL)
|
|
|
|
{
|
|
|
|
pfn_unregister();
|
|
|
|
pfn_unregister = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* CLKSRC = ipg_clk,
|
|
|
|
* EPIT output disconnected,
|
|
|
|
* Enabled in wait mode
|
|
|
|
* Prescale 1 for 66MHz
|
|
|
|
* Reload from modulus register,
|
|
|
|
* Count from load value */
|
|
|
|
EPITCR2 = EPITCR_CLKSRC_IPG_CLK | EPITCR_WAITEN | EPITCR_IOVW |
|
2009-03-22 02:27:20 +00:00
|
|
|
((1-1) << EPITCR_PRESCALER_POS) | EPITCR_RLD | EPITCR_ENMOD;
|
2009-01-05 00:00:29 +00:00
|
|
|
EPITLR2 = cycles;
|
|
|
|
/* Event when counter reaches 0 */
|
|
|
|
EPITCMPR2 = 0;
|
|
|
|
|
|
|
|
restore_interrupt(oldstatus);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-06-29 14:29:57 +00:00
|
|
|
bool timer_start(void)
|
2009-01-05 00:00:29 +00:00
|
|
|
{
|
|
|
|
int oldstatus = disable_interrupt_save(IRQ_FIQ_STATUS);
|
|
|
|
|
|
|
|
/* Halt timer if running - leave module clock enabled */
|
|
|
|
stop_timer(false);
|
|
|
|
|
|
|
|
/* Enable interrupt */
|
|
|
|
EPITCR2 |= EPITCR_OCIEN;
|
2009-03-22 01:50:48 +00:00
|
|
|
avic_enable_int(INT_EPIT2, INT_TYPE_IRQ, INT_PRIO_DEFAULT,
|
|
|
|
EPIT2_HANDLER);
|
2009-01-05 00:00:29 +00:00
|
|
|
/* Start timer */
|
|
|
|
EPITCR2 |= EPITCR_EN;
|
|
|
|
|
|
|
|
restore_interrupt(oldstatus);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-06-29 14:29:57 +00:00
|
|
|
void timer_stop(void)
|
2009-01-05 00:00:29 +00:00
|
|
|
{
|
|
|
|
int oldstatus = disable_interrupt_save(IRQ_FIQ_STATUS);
|
|
|
|
/* Halt timer if running - stop module clock */
|
|
|
|
stop_timer(true);
|
|
|
|
restore_interrupt(oldstatus);
|
|
|
|
}
|