2005-07-26 20:01:11 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2005 Jens Arnold
|
|
|
|
*
|
2008-06-28 18:10:04 +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.
|
2005-07-26 20:01:11 +00:00
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include "config.h"
|
|
|
|
#include "cpu.h"
|
|
|
|
#include "system.h"
|
|
|
|
#include "timer.h"
|
2007-07-06 21:36:32 +00:00
|
|
|
#include "logf.h"
|
2005-07-26 20:01:11 +00:00
|
|
|
|
|
|
|
static int timer_prio = -1;
|
2008-04-06 04:34:57 +00:00
|
|
|
void SHAREDBSS_ATTR (*pfn_timer)(void) = NULL; /* timer callback */
|
|
|
|
void SHAREDBSS_ATTR (*pfn_unregister)(void) = NULL; /* unregister callback */
|
2005-10-03 09:24:36 +00:00
|
|
|
#ifdef CPU_COLDFIRE
|
|
|
|
static int base_prescale;
|
2007-03-24 19:26:13 +00:00
|
|
|
#elif defined CPU_PP || CONFIG_CPU == PNX0101
|
2008-04-06 04:34:57 +00:00
|
|
|
static long SHAREDBSS_ATTR cycles_new = 0;
|
2005-10-03 09:24:36 +00:00
|
|
|
#endif
|
2005-07-26 20:01:11 +00:00
|
|
|
|
|
|
|
/* interrupt handler */
|
|
|
|
#if CONFIG_CPU == SH7034
|
|
|
|
void IMIA4(void) __attribute__((interrupt_handler));
|
|
|
|
void IMIA4(void)
|
|
|
|
{
|
|
|
|
if (pfn_timer != NULL)
|
|
|
|
pfn_timer();
|
|
|
|
and_b(~0x01, &TSR4); /* clear the interrupt */
|
|
|
|
}
|
|
|
|
#elif defined CPU_COLDFIRE
|
|
|
|
void TIMER1(void) __attribute__ ((interrupt_handler));
|
|
|
|
void TIMER1(void)
|
|
|
|
{
|
|
|
|
if (pfn_timer != NULL)
|
|
|
|
pfn_timer();
|
|
|
|
TER1 = 0xff; /* clear all events */
|
|
|
|
}
|
2008-11-06 02:31:32 +00:00
|
|
|
#elif CONFIG_CPU == AS3525
|
|
|
|
void INT_TIMER1(void)
|
|
|
|
{
|
|
|
|
if (pfn_timer != NULL)
|
|
|
|
pfn_timer();
|
|
|
|
|
|
|
|
TIMER1_INTCLR = 0; /* clear interrupt */
|
|
|
|
}
|
2006-11-22 00:41:30 +00:00
|
|
|
#elif defined(CPU_PP)
|
2006-03-17 00:08:39 +00:00
|
|
|
void TIMER2(void)
|
|
|
|
{
|
|
|
|
TIMER2_VAL; /* ACK interrupt */
|
2006-08-27 23:43:04 +00:00
|
|
|
if (cycles_new > 0)
|
|
|
|
{
|
2006-09-01 06:13:33 +00:00
|
|
|
TIMER2_CFG = 0xc0000000 | (cycles_new - 1);
|
2006-08-27 23:43:04 +00:00
|
|
|
cycles_new = 0;
|
|
|
|
}
|
2006-03-17 00:08:39 +00:00
|
|
|
if (pfn_timer != NULL)
|
2006-08-28 06:47:26 +00:00
|
|
|
{
|
2006-09-01 22:03:14 +00:00
|
|
|
cycles_new = -1;
|
2006-08-28 06:47:26 +00:00
|
|
|
/* "lock" the variable, in case timer_set_period()
|
|
|
|
* is called within pfn_timer() */
|
2006-03-17 00:08:39 +00:00
|
|
|
pfn_timer();
|
2006-08-28 06:47:26 +00:00
|
|
|
cycles_new = 0;
|
|
|
|
}
|
2006-03-17 00:08:39 +00:00
|
|
|
}
|
2007-03-24 19:26:13 +00:00
|
|
|
#elif CONFIG_CPU == PNX0101
|
|
|
|
void TIMER1_ISR(void)
|
|
|
|
{
|
|
|
|
if (cycles_new > 0)
|
|
|
|
{
|
|
|
|
TIMER1.load = cycles_new - 1;
|
|
|
|
cycles_new = 0;
|
|
|
|
}
|
|
|
|
if (pfn_timer != NULL)
|
|
|
|
{
|
|
|
|
cycles_new = -1;
|
|
|
|
/* "lock" the variable, in case timer_set_period()
|
|
|
|
* is called within pfn_timer() */
|
|
|
|
pfn_timer();
|
|
|
|
cycles_new = 0;
|
|
|
|
}
|
|
|
|
TIMER1.clr = 1; /* clear the interrupt */
|
|
|
|
}
|
2005-07-26 20:01:11 +00:00
|
|
|
#endif /* CONFIG_CPU */
|
|
|
|
|
|
|
|
static bool timer_set(long cycles, bool start)
|
|
|
|
{
|
2008-11-06 02:31:32 +00:00
|
|
|
#if CONFIG_CPU == SH7034 || defined(CPU_COLDFIRE) || CONFIG_CPU == AS3525
|
2005-07-26 20:01:11 +00:00
|
|
|
int phi = 0; /* bits for the prescaler */
|
|
|
|
int prescale = 1;
|
2005-11-11 17:51:35 +00:00
|
|
|
|
2008-11-06 02:31:32 +00:00
|
|
|
#if CONFIG_CPU == SH7034 || defined(CPU_COLDFIRE)
|
|
|
|
#define PRESCALE_STEP 1
|
|
|
|
#else /* CONFIG_CPU == AS3525 */
|
|
|
|
#define PRESCALE_STEP 4
|
|
|
|
#endif
|
|
|
|
|
2005-07-26 20:01:11 +00:00
|
|
|
while (cycles > 0x10000)
|
|
|
|
{ /* work out the smallest prescaler that makes it fit */
|
2008-11-06 02:31:32 +00:00
|
|
|
#if CONFIG_CPU == SH7034 || CONFIG_CPU == AS3525
|
2005-07-26 20:01:11 +00:00
|
|
|
phi++;
|
|
|
|
#endif
|
2008-11-06 02:31:32 +00:00
|
|
|
prescale <<= PRESCALE_STEP;
|
|
|
|
cycles >>= PRESCALE_STEP;
|
2005-07-26 20:01:11 +00:00
|
|
|
}
|
2006-03-17 00:08:39 +00:00
|
|
|
#endif
|
2005-07-26 20:01:11 +00:00
|
|
|
|
2007-03-24 19:26:13 +00:00
|
|
|
#if CONFIG_CPU == PNX0101
|
|
|
|
if (start)
|
|
|
|
{
|
|
|
|
if (pfn_unregister != NULL)
|
|
|
|
{
|
|
|
|
pfn_unregister();
|
|
|
|
pfn_unregister = NULL;
|
|
|
|
}
|
|
|
|
TIMER1.ctrl &= ~0x80; /* disable the counter */
|
|
|
|
TIMER1.ctrl |= 0x40; /* reload after counting down to zero */
|
|
|
|
TIMER1.ctrl &= ~0xc; /* no prescaler */
|
|
|
|
TIMER1.clr = 1; /* clear an interrupt event */
|
|
|
|
}
|
|
|
|
if (start || (cycles_new == -1)) /* within isr, cycles_new is "locked" */
|
|
|
|
{ /* enable timer */
|
|
|
|
TIMER1.load = cycles - 1;
|
|
|
|
TIMER1.ctrl |= 0x80; /* enable the counter */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
cycles_new = cycles;
|
2006-08-27 23:43:04 +00:00
|
|
|
|
2007-07-06 21:36:32 +00:00
|
|
|
return true;
|
|
|
|
#elif CONFIG_CPU == SH7034
|
2005-07-26 20:01:11 +00:00
|
|
|
if (prescale > 8)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (start)
|
|
|
|
{
|
|
|
|
if (pfn_unregister != NULL)
|
|
|
|
{
|
|
|
|
pfn_unregister();
|
|
|
|
pfn_unregister = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
and_b(~0x10, &TSTR); /* Stop the timer 4 */
|
|
|
|
and_b(~0x10, &TSNC); /* No synchronization */
|
|
|
|
and_b(~0x10, &TMDR); /* Operate normally */
|
|
|
|
|
|
|
|
TIER4 = 0xF9; /* Enable GRA match interrupt */
|
|
|
|
}
|
|
|
|
|
|
|
|
TCR4 = 0x20 | phi; /* clear at GRA match, set prescaler */
|
|
|
|
GRA4 = (unsigned short)(cycles - 1);
|
|
|
|
if (start || (TCNT4 >= GRA4))
|
|
|
|
TCNT4 = 0;
|
|
|
|
and_b(~0x01, &TSR4); /* clear an eventual interrupt */
|
|
|
|
|
2008-11-06 02:31:32 +00:00
|
|
|
return true;
|
|
|
|
#elif CONFIG_CPU == AS3525
|
|
|
|
/* XXX: 32 bits cycles could be used */
|
|
|
|
if (prescale > 256 || cycles > 0x10000)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (start)
|
|
|
|
{
|
|
|
|
if (pfn_unregister != NULL)
|
|
|
|
{
|
|
|
|
pfn_unregister();
|
|
|
|
pfn_unregister = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TIMER1_LOAD = TIMER1_BGLOAD = cycles;
|
|
|
|
/* /!\ bit 4 (reserved) must not be modified
|
|
|
|
* periodic mode, interrupt enabled, 16 bits counter */
|
|
|
|
TIMER1_CONTROL = (TIMER1_CONTROL & (1<<4)) | 0xe0 | (phi<<2);
|
2007-07-06 21:36:32 +00:00
|
|
|
return true;
|
2005-07-26 20:01:11 +00:00
|
|
|
#elif defined CPU_COLDFIRE
|
2005-10-03 09:24:36 +00:00
|
|
|
if (prescale > 4096/CPUFREQ_MAX_MULT)
|
2005-07-26 20:01:11 +00:00
|
|
|
return false;
|
|
|
|
|
2005-10-03 09:24:36 +00:00
|
|
|
if (prescale > 256/CPUFREQ_MAX_MULT)
|
2005-07-26 20:01:11 +00:00
|
|
|
{
|
|
|
|
phi = 0x05; /* prescale sysclk/16, timer enabled */
|
|
|
|
prescale >>= 4;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
phi = 0x03; /* prescale sysclk, timer enabled */
|
2008-11-06 02:31:32 +00:00
|
|
|
|
2005-10-03 09:24:36 +00:00
|
|
|
base_prescale = prescale;
|
|
|
|
prescale *= (cpu_frequency / CPU_FREQ);
|
2005-07-26 20:01:11 +00:00
|
|
|
|
|
|
|
if (start)
|
|
|
|
{
|
|
|
|
if (pfn_unregister != NULL)
|
|
|
|
{
|
|
|
|
pfn_unregister();
|
|
|
|
pfn_unregister = NULL;
|
|
|
|
}
|
|
|
|
phi &= ~1; /* timer disabled at start */
|
2006-08-25 11:46:04 +00:00
|
|
|
|
|
|
|
/* If it is already enabled, writing a 0 to the RST bit will clear
|
|
|
|
the register, so we clear RST explicitly before writing the real
|
|
|
|
data. */
|
|
|
|
TMR1 = 0;
|
2005-07-26 20:01:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* We are using timer 1 */
|
|
|
|
TMR1 = 0x0018 | (unsigned short)phi | ((unsigned short)(prescale - 1) << 8);
|
|
|
|
TRR1 = (unsigned short)(cycles - 1);
|
|
|
|
if (start || (TCN1 >= TRR1))
|
|
|
|
TCN1 = 0; /* reset the timer */
|
|
|
|
TER1 = 0xff; /* clear all events */
|
2007-07-06 21:36:32 +00:00
|
|
|
|
|
|
|
return true;
|
2006-11-22 00:41:30 +00:00
|
|
|
#elif defined(CPU_PP)
|
2006-09-01 22:03:14 +00:00
|
|
|
if (cycles > 0x20000000 || cycles < 2)
|
2006-08-27 23:43:04 +00:00
|
|
|
return false;
|
|
|
|
|
2006-03-17 00:08:39 +00:00
|
|
|
if (start)
|
|
|
|
{
|
|
|
|
if (pfn_unregister != NULL)
|
|
|
|
{
|
|
|
|
pfn_unregister();
|
|
|
|
pfn_unregister = NULL;
|
|
|
|
}
|
2008-06-03 05:08:24 +00:00
|
|
|
CPU_INT_DIS = TIMER2_MASK;
|
|
|
|
COP_INT_DIS = TIMER2_MASK;
|
2006-03-17 00:08:39 +00:00
|
|
|
}
|
2006-08-28 06:47:26 +00:00
|
|
|
if (start || (cycles_new == -1)) /* within isr, cycles_new is "locked" */
|
2006-09-01 06:13:33 +00:00
|
|
|
TIMER2_CFG = 0xc0000000 | (cycles - 1); /* enable timer */
|
2006-08-27 23:43:04 +00:00
|
|
|
else
|
|
|
|
cycles_new = cycles;
|
|
|
|
|
2005-07-26 20:01:11 +00:00
|
|
|
return true;
|
2007-09-22 02:17:08 +00:00
|
|
|
#elif (CONFIG_CPU == IMX31L)
|
|
|
|
/* TODO */
|
2008-04-15 14:44:32 +00:00
|
|
|
(void)cycles; (void)start;
|
|
|
|
return false;
|
2007-09-20 04:46:41 +00:00
|
|
|
#else
|
2007-07-06 21:36:32 +00:00
|
|
|
return __TIMER_SET(cycles, start);
|
|
|
|
#endif /* CONFIG_CPU */
|
2005-07-26 20:01:11 +00:00
|
|
|
}
|
|
|
|
|
2005-10-03 09:24:36 +00:00
|
|
|
#ifdef CPU_COLDFIRE
|
|
|
|
void timers_adjust_prescale(int multiplier, bool enable_irq)
|
|
|
|
{
|
|
|
|
/* tick timer */
|
|
|
|
TMR0 = (TMR0 & 0x00ef)
|
|
|
|
| ((unsigned short)(multiplier - 1) << 8)
|
|
|
|
| (enable_irq ? 0x10 : 0);
|
|
|
|
|
|
|
|
if (pfn_timer)
|
|
|
|
{
|
|
|
|
/* user timer */
|
|
|
|
int prescale = base_prescale * multiplier;
|
|
|
|
TMR1 = (TMR1 & 0x00ef)
|
|
|
|
| ((unsigned short)(prescale - 1) << 8)
|
|
|
|
| (enable_irq ? 0x10 : 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2006-03-17 00:08:39 +00:00
|
|
|
/* Register a user timer, called every <cycles> TIMER_FREQ cycles */
|
2005-07-26 20:01:11 +00:00
|
|
|
bool timer_register(int reg_prio, void (*unregister_callback)(void),
|
2008-04-04 19:38:46 +00:00
|
|
|
long cycles, int int_prio, void (*timer_callback)(void)
|
|
|
|
IF_COP(, int core))
|
2005-07-26 20:01:11 +00:00
|
|
|
{
|
|
|
|
if (reg_prio <= timer_prio || cycles == 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
#if CONFIG_CPU == SH7034
|
|
|
|
if (int_prio < 1 || int_prio > 15)
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (!timer_set(cycles, true))
|
|
|
|
return false;
|
2008-11-06 02:31:32 +00:00
|
|
|
|
2005-07-26 20:01:11 +00:00
|
|
|
pfn_timer = timer_callback;
|
|
|
|
pfn_unregister = unregister_callback;
|
|
|
|
timer_prio = reg_prio;
|
|
|
|
|
|
|
|
#if CONFIG_CPU == SH7034
|
|
|
|
IPRD = (IPRD & 0xFF0F) | int_prio << 4; /* interrupt priority */
|
|
|
|
or_b(0x10, &TSTR); /* start timer 4 */
|
2007-07-06 21:36:32 +00:00
|
|
|
return true;
|
2005-07-26 20:01:11 +00:00
|
|
|
#elif defined CPU_COLDFIRE
|
2005-11-05 03:28:20 +00:00
|
|
|
ICR2 = 0x90; /* interrupt on level 4.0 */
|
2005-07-26 20:01:11 +00:00
|
|
|
and_l(~(1<<10), &IMR);
|
|
|
|
TMR1 |= 1; /* start timer */
|
2007-07-06 21:36:32 +00:00
|
|
|
return true;
|
2006-11-22 00:41:30 +00:00
|
|
|
#elif defined(CPU_PP)
|
2006-03-17 00:08:39 +00:00
|
|
|
/* unmask interrupt source */
|
2008-04-04 19:38:46 +00:00
|
|
|
#if NUM_CORES > 1
|
|
|
|
if (core == COP)
|
|
|
|
COP_INT_EN = TIMER2_MASK;
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
CPU_INT_EN = TIMER2_MASK;
|
2007-07-06 21:36:32 +00:00
|
|
|
return true;
|
2007-03-24 19:26:13 +00:00
|
|
|
#elif CONFIG_CPU == PNX0101
|
|
|
|
irq_set_int_handler(IRQ_TIMER1, TIMER1_ISR);
|
|
|
|
irq_enable_int(IRQ_TIMER1);
|
2005-07-26 20:01:11 +00:00
|
|
|
return true;
|
2008-11-06 02:31:32 +00:00
|
|
|
#elif CONFIG_CPU == AS3525
|
|
|
|
CGU_PERI |= CGU_TIMER1_CLOCK_ENABLE; /* enable peripheral */
|
|
|
|
VIC_INT_ENABLE |= INTERRUPT_TIMER1;
|
|
|
|
return true;
|
2007-09-22 02:17:08 +00:00
|
|
|
#elif CONFIG_CPU == IMX31L
|
|
|
|
/* TODO */
|
2008-04-15 14:44:32 +00:00
|
|
|
return false;
|
2007-09-20 04:46:41 +00:00
|
|
|
#else
|
2007-07-06 21:36:32 +00:00
|
|
|
return __TIMER_REGISTER(reg_prio, unregister_callback, cycles,
|
|
|
|
int_prio, timer_callback);
|
|
|
|
#endif
|
|
|
|
/* Cover for targets that don't use all these */
|
|
|
|
(void)reg_prio;
|
|
|
|
(void)unregister_callback;
|
|
|
|
(void)cycles;
|
|
|
|
/* TODO: Implement for PortalPlayer and iFP (if possible) */
|
|
|
|
(void)int_prio;
|
2008-11-06 02:31:32 +00:00
|
|
|
(void)timer_callback;
|
2005-07-26 20:01:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool timer_set_period(long cycles)
|
|
|
|
{
|
|
|
|
return timer_set(cycles, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void timer_unregister(void)
|
|
|
|
{
|
|
|
|
#if CONFIG_CPU == SH7034
|
|
|
|
and_b(~0x10, &TSTR); /* stop the timer 4 */
|
|
|
|
IPRD = (IPRD & 0xFF0F); /* disable interrupt */
|
|
|
|
#elif defined CPU_COLDFIRE
|
|
|
|
TMR1 = 0; /* disable timer 1 */
|
|
|
|
or_l((1<<10), &IMR); /* disable interrupt */
|
2006-11-22 00:41:30 +00:00
|
|
|
#elif defined(CPU_PP)
|
2006-08-27 23:43:04 +00:00
|
|
|
TIMER2_CFG = 0; /* stop timer 2 */
|
2008-06-03 05:08:24 +00:00
|
|
|
CPU_INT_DIS = TIMER2_MASK;
|
|
|
|
COP_INT_DIS = TIMER2_MASK;
|
2007-03-24 19:26:13 +00:00
|
|
|
#elif CONFIG_CPU == PNX0101
|
|
|
|
TIMER1.ctrl &= ~0x80; /* disable timer 1 */
|
2007-09-22 23:37:58 +00:00
|
|
|
irq_disable_int(IRQ_TIMER1);
|
2008-11-06 02:31:32 +00:00
|
|
|
#elif CONFIG_CPU == AS3525
|
|
|
|
TIMER1_CONTROL &= 0x10; /* disable timer 1 (don't modify bit 4) */
|
2008-12-05 14:37:28 +00:00
|
|
|
VIC_INT_EN_CLEAR = INTERRUPT_TIMER1; /* disable interrupt */
|
2008-11-06 02:31:32 +00:00
|
|
|
CGU_PERI &= ~CGU_TIMER1_CLOCK_ENABLE; /* disable peripheral */
|
2008-04-24 20:08:28 +00:00
|
|
|
#elif CONFIG_CPU == S3C2440 || CONFIG_CPU == DM320
|
2007-07-06 21:36:32 +00:00
|
|
|
__TIMER_UNREGISTER();
|
2005-07-26 20:01:11 +00:00
|
|
|
#endif
|
|
|
|
pfn_timer = NULL;
|
|
|
|
pfn_unregister = NULL;
|
|
|
|
timer_prio = -1;
|
|
|
|
}
|
|
|
|
|