2012-04-17 20:20:49 +02:00
|
|
|
/*
|
2011-03-12 00:02:07 +01:00
|
|
|
* Copyright (c) 2011 Samsung Electronics Co., Ltd.
|
|
|
|
* http://www.samsung.com/
|
|
|
|
*
|
2013-01-10 03:47:04 +01:00
|
|
|
* samsung - Common hr-timer support (s3c and s5p)
|
2011-03-12 00:02:07 +01:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License version 2 as
|
|
|
|
* published by the Free Software Foundation.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/interrupt.h>
|
|
|
|
#include <linux/irq.h>
|
|
|
|
#include <linux/err.h>
|
|
|
|
#include <linux/clk.h>
|
|
|
|
#include <linux/clockchips.h>
|
|
|
|
#include <linux/platform_device.h>
|
2013-06-02 08:39:40 +02:00
|
|
|
#include <linux/sched_clock.h>
|
2011-03-12 00:02:07 +01:00
|
|
|
|
|
|
|
#include <asm/smp_twd.h>
|
|
|
|
#include <asm/mach/time.h>
|
|
|
|
#include <asm/mach/arch.h>
|
|
|
|
#include <asm/mach/map.h>
|
|
|
|
|
|
|
|
#include <mach/map.h>
|
|
|
|
#include <plat/devs.h>
|
|
|
|
#include <plat/regs-timer.h>
|
2013-01-10 03:47:03 +01:00
|
|
|
#include <plat/samsung-time.h>
|
2011-03-12 00:02:07 +01:00
|
|
|
|
|
|
|
static struct clk *tin_event;
|
|
|
|
static struct clk *tin_source;
|
|
|
|
static struct clk *tdiv_event;
|
|
|
|
static struct clk *tdiv_source;
|
|
|
|
static struct clk *timerclk;
|
2013-01-10 03:47:03 +01:00
|
|
|
static struct samsung_timer_source timer_source;
|
2011-03-12 00:02:07 +01:00
|
|
|
static unsigned long clock_count_per_tick;
|
2013-01-10 03:47:03 +01:00
|
|
|
static void samsung_timer_resume(void);
|
2011-03-12 00:02:07 +01:00
|
|
|
|
2013-01-10 03:47:03 +01:00
|
|
|
static void samsung_time_stop(enum samsung_timer_mode mode)
|
2011-03-12 00:02:07 +01:00
|
|
|
{
|
|
|
|
unsigned long tcon;
|
|
|
|
|
|
|
|
tcon = __raw_readl(S3C2410_TCON);
|
|
|
|
|
|
|
|
switch (mode) {
|
2013-01-10 03:47:03 +01:00
|
|
|
case SAMSUNG_PWM0:
|
2011-03-12 00:02:07 +01:00
|
|
|
tcon &= ~S3C2410_TCON_T0START;
|
|
|
|
break;
|
|
|
|
|
2013-01-10 03:47:03 +01:00
|
|
|
case SAMSUNG_PWM1:
|
2011-03-12 00:02:07 +01:00
|
|
|
tcon &= ~S3C2410_TCON_T1START;
|
|
|
|
break;
|
|
|
|
|
2013-01-10 03:47:03 +01:00
|
|
|
case SAMSUNG_PWM2:
|
2011-03-12 00:02:07 +01:00
|
|
|
tcon &= ~S3C2410_TCON_T2START;
|
|
|
|
break;
|
|
|
|
|
2013-01-10 03:47:03 +01:00
|
|
|
case SAMSUNG_PWM3:
|
2011-03-12 00:02:07 +01:00
|
|
|
tcon &= ~S3C2410_TCON_T3START;
|
|
|
|
break;
|
|
|
|
|
2013-01-10 03:47:03 +01:00
|
|
|
case SAMSUNG_PWM4:
|
2011-03-12 00:02:07 +01:00
|
|
|
tcon &= ~S3C2410_TCON_T4START;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
printk(KERN_ERR "Invalid Timer %d\n", mode);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
__raw_writel(tcon, S3C2410_TCON);
|
|
|
|
}
|
|
|
|
|
2013-01-10 03:47:03 +01:00
|
|
|
static void samsung_time_setup(enum samsung_timer_mode mode, unsigned long tcnt)
|
2011-03-12 00:02:07 +01:00
|
|
|
{
|
|
|
|
unsigned long tcon;
|
|
|
|
|
|
|
|
tcon = __raw_readl(S3C2410_TCON);
|
|
|
|
|
|
|
|
tcnt--;
|
|
|
|
|
|
|
|
switch (mode) {
|
2013-01-10 03:47:03 +01:00
|
|
|
case SAMSUNG_PWM0:
|
2011-03-12 00:02:07 +01:00
|
|
|
tcon &= ~(0x0f << 0);
|
|
|
|
tcon |= S3C2410_TCON_T0MANUALUPD;
|
|
|
|
break;
|
|
|
|
|
2013-01-10 03:47:03 +01:00
|
|
|
case SAMSUNG_PWM1:
|
2011-03-12 00:02:07 +01:00
|
|
|
tcon &= ~(0x0f << 8);
|
|
|
|
tcon |= S3C2410_TCON_T1MANUALUPD;
|
|
|
|
break;
|
|
|
|
|
2013-01-10 03:47:03 +01:00
|
|
|
case SAMSUNG_PWM2:
|
2011-03-12 00:02:07 +01:00
|
|
|
tcon &= ~(0x0f << 12);
|
|
|
|
tcon |= S3C2410_TCON_T2MANUALUPD;
|
|
|
|
break;
|
|
|
|
|
2013-01-10 03:47:03 +01:00
|
|
|
case SAMSUNG_PWM3:
|
2011-03-12 00:02:07 +01:00
|
|
|
tcon &= ~(0x0f << 16);
|
|
|
|
tcon |= S3C2410_TCON_T3MANUALUPD;
|
|
|
|
break;
|
|
|
|
|
2013-01-10 03:47:03 +01:00
|
|
|
case SAMSUNG_PWM4:
|
2011-03-12 00:02:07 +01:00
|
|
|
tcon &= ~(0x07 << 20);
|
|
|
|
tcon |= S3C2410_TCON_T4MANUALUPD;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
printk(KERN_ERR "Invalid Timer %d\n", mode);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
__raw_writel(tcnt, S3C2410_TCNTB(mode));
|
|
|
|
__raw_writel(tcnt, S3C2410_TCMPB(mode));
|
|
|
|
__raw_writel(tcon, S3C2410_TCON);
|
|
|
|
}
|
|
|
|
|
2013-01-10 03:47:03 +01:00
|
|
|
static void samsung_time_start(enum samsung_timer_mode mode, bool periodic)
|
2011-03-12 00:02:07 +01:00
|
|
|
{
|
|
|
|
unsigned long tcon;
|
|
|
|
|
|
|
|
tcon = __raw_readl(S3C2410_TCON);
|
|
|
|
|
|
|
|
switch (mode) {
|
2013-01-10 03:47:03 +01:00
|
|
|
case SAMSUNG_PWM0:
|
2011-03-12 00:02:07 +01:00
|
|
|
tcon |= S3C2410_TCON_T0START;
|
|
|
|
tcon &= ~S3C2410_TCON_T0MANUALUPD;
|
|
|
|
|
|
|
|
if (periodic)
|
|
|
|
tcon |= S3C2410_TCON_T0RELOAD;
|
|
|
|
else
|
|
|
|
tcon &= ~S3C2410_TCON_T0RELOAD;
|
|
|
|
break;
|
|
|
|
|
2013-01-10 03:47:03 +01:00
|
|
|
case SAMSUNG_PWM1:
|
2011-03-12 00:02:07 +01:00
|
|
|
tcon |= S3C2410_TCON_T1START;
|
|
|
|
tcon &= ~S3C2410_TCON_T1MANUALUPD;
|
|
|
|
|
|
|
|
if (periodic)
|
|
|
|
tcon |= S3C2410_TCON_T1RELOAD;
|
|
|
|
else
|
|
|
|
tcon &= ~S3C2410_TCON_T1RELOAD;
|
|
|
|
break;
|
|
|
|
|
2013-01-10 03:47:03 +01:00
|
|
|
case SAMSUNG_PWM2:
|
2011-03-12 00:02:07 +01:00
|
|
|
tcon |= S3C2410_TCON_T2START;
|
|
|
|
tcon &= ~S3C2410_TCON_T2MANUALUPD;
|
|
|
|
|
|
|
|
if (periodic)
|
|
|
|
tcon |= S3C2410_TCON_T2RELOAD;
|
|
|
|
else
|
|
|
|
tcon &= ~S3C2410_TCON_T2RELOAD;
|
|
|
|
break;
|
|
|
|
|
2013-01-10 03:47:03 +01:00
|
|
|
case SAMSUNG_PWM3:
|
2011-03-12 00:02:07 +01:00
|
|
|
tcon |= S3C2410_TCON_T3START;
|
|
|
|
tcon &= ~S3C2410_TCON_T3MANUALUPD;
|
|
|
|
|
|
|
|
if (periodic)
|
|
|
|
tcon |= S3C2410_TCON_T3RELOAD;
|
|
|
|
else
|
|
|
|
tcon &= ~S3C2410_TCON_T3RELOAD;
|
|
|
|
break;
|
|
|
|
|
2013-01-10 03:47:03 +01:00
|
|
|
case SAMSUNG_PWM4:
|
2011-03-12 00:02:07 +01:00
|
|
|
tcon |= S3C2410_TCON_T4START;
|
|
|
|
tcon &= ~S3C2410_TCON_T4MANUALUPD;
|
|
|
|
|
|
|
|
if (periodic)
|
|
|
|
tcon |= S3C2410_TCON_T4RELOAD;
|
|
|
|
else
|
|
|
|
tcon &= ~S3C2410_TCON_T4RELOAD;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
printk(KERN_ERR "Invalid Timer %d\n", mode);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
__raw_writel(tcon, S3C2410_TCON);
|
|
|
|
}
|
|
|
|
|
2013-01-10 03:47:03 +01:00
|
|
|
static int samsung_set_next_event(unsigned long cycles,
|
2011-03-12 00:02:07 +01:00
|
|
|
struct clock_event_device *evt)
|
|
|
|
{
|
2013-01-10 03:47:03 +01:00
|
|
|
samsung_time_setup(timer_source.event_id, cycles);
|
|
|
|
samsung_time_start(timer_source.event_id, NON_PERIODIC);
|
2011-03-12 00:02:07 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-01-10 03:47:03 +01:00
|
|
|
static void samsung_set_mode(enum clock_event_mode mode,
|
2011-03-12 00:02:07 +01:00
|
|
|
struct clock_event_device *evt)
|
|
|
|
{
|
2013-01-10 03:47:03 +01:00
|
|
|
samsung_time_stop(timer_source.event_id);
|
2011-03-12 00:02:07 +01:00
|
|
|
|
|
|
|
switch (mode) {
|
|
|
|
case CLOCK_EVT_MODE_PERIODIC:
|
2013-01-10 03:47:03 +01:00
|
|
|
samsung_time_setup(timer_source.event_id, clock_count_per_tick);
|
|
|
|
samsung_time_start(timer_source.event_id, PERIODIC);
|
2011-03-12 00:02:07 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case CLOCK_EVT_MODE_ONESHOT:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CLOCK_EVT_MODE_UNUSED:
|
|
|
|
case CLOCK_EVT_MODE_SHUTDOWN:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CLOCK_EVT_MODE_RESUME:
|
2013-01-10 03:47:03 +01:00
|
|
|
samsung_timer_resume();
|
2011-03-12 00:02:07 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-10 03:47:03 +01:00
|
|
|
static void samsung_timer_resume(void)
|
2011-03-12 00:02:07 +01:00
|
|
|
{
|
|
|
|
/* event timer restart */
|
2013-01-10 03:47:03 +01:00
|
|
|
samsung_time_setup(timer_source.event_id, clock_count_per_tick);
|
|
|
|
samsung_time_start(timer_source.event_id, PERIODIC);
|
2011-03-12 00:02:07 +01:00
|
|
|
|
|
|
|
/* source timer restart */
|
2013-01-10 03:47:03 +01:00
|
|
|
samsung_time_setup(timer_source.source_id, TCNT_MAX);
|
|
|
|
samsung_time_start(timer_source.source_id, PERIODIC);
|
2011-03-12 00:02:07 +01:00
|
|
|
}
|
|
|
|
|
2013-01-10 03:47:03 +01:00
|
|
|
void __init samsung_set_timer_source(enum samsung_timer_mode event,
|
|
|
|
enum samsung_timer_mode source)
|
2011-03-12 00:02:07 +01:00
|
|
|
{
|
|
|
|
s3c_device_timer[event].dev.bus = &platform_bus_type;
|
|
|
|
s3c_device_timer[source].dev.bus = &platform_bus_type;
|
|
|
|
|
|
|
|
timer_source.event_id = event;
|
|
|
|
timer_source.source_id = source;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct clock_event_device time_event_device = {
|
2013-01-10 03:47:03 +01:00
|
|
|
.name = "samsung_event_timer",
|
2011-03-12 00:02:07 +01:00
|
|
|
.features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
|
|
|
|
.rating = 200,
|
2013-01-10 03:47:03 +01:00
|
|
|
.set_next_event = samsung_set_next_event,
|
|
|
|
.set_mode = samsung_set_mode,
|
2011-03-12 00:02:07 +01:00
|
|
|
};
|
|
|
|
|
2013-01-10 03:47:03 +01:00
|
|
|
static irqreturn_t samsung_clock_event_isr(int irq, void *dev_id)
|
2011-03-12 00:02:07 +01:00
|
|
|
{
|
|
|
|
struct clock_event_device *evt = dev_id;
|
|
|
|
|
|
|
|
evt->event_handler(evt);
|
|
|
|
|
|
|
|
return IRQ_HANDLED;
|
|
|
|
}
|
|
|
|
|
2013-01-10 03:47:03 +01:00
|
|
|
static struct irqaction samsung_clock_event_irq = {
|
|
|
|
.name = "samsung_time_irq",
|
2011-03-12 00:02:07 +01:00
|
|
|
.flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
|
2013-01-10 03:47:03 +01:00
|
|
|
.handler = samsung_clock_event_isr,
|
2011-03-12 00:02:07 +01:00
|
|
|
.dev_id = &time_event_device,
|
|
|
|
};
|
|
|
|
|
2013-01-10 03:47:03 +01:00
|
|
|
static void __init samsung_clockevent_init(void)
|
2011-03-12 00:02:07 +01:00
|
|
|
{
|
|
|
|
unsigned long pclk;
|
|
|
|
unsigned long clock_rate;
|
|
|
|
unsigned int irq_number;
|
|
|
|
struct clk *tscaler;
|
|
|
|
|
|
|
|
pclk = clk_get_rate(timerclk);
|
|
|
|
|
|
|
|
tscaler = clk_get_parent(tdiv_event);
|
|
|
|
|
2013-01-10 03:47:04 +01:00
|
|
|
clk_set_rate(tscaler, pclk / TSCALER_DIV);
|
|
|
|
clk_set_rate(tdiv_event, pclk / TDIV);
|
2011-03-12 00:02:07 +01:00
|
|
|
clk_set_parent(tin_event, tdiv_event);
|
|
|
|
|
|
|
|
clock_rate = clk_get_rate(tin_event);
|
|
|
|
clock_count_per_tick = clock_rate / HZ;
|
|
|
|
|
|
|
|
time_event_device.cpumask = cpumask_of(0);
|
2013-01-12 12:50:05 +01:00
|
|
|
clockevents_config_and_register(&time_event_device, clock_rate, 1, -1);
|
2011-03-12 00:02:07 +01:00
|
|
|
|
|
|
|
irq_number = timer_source.event_id + IRQ_TIMER0;
|
2013-01-10 03:47:03 +01:00
|
|
|
setup_irq(irq_number, &samsung_clock_event_irq);
|
2011-03-12 00:02:07 +01:00
|
|
|
}
|
|
|
|
|
2013-01-10 03:47:03 +01:00
|
|
|
static void __iomem *samsung_timer_reg(void)
|
2011-03-12 00:02:07 +01:00
|
|
|
{
|
|
|
|
unsigned long offset = 0;
|
|
|
|
|
|
|
|
switch (timer_source.source_id) {
|
2013-01-10 03:47:03 +01:00
|
|
|
case SAMSUNG_PWM0:
|
|
|
|
case SAMSUNG_PWM1:
|
|
|
|
case SAMSUNG_PWM2:
|
|
|
|
case SAMSUNG_PWM3:
|
2011-03-12 00:02:07 +01:00
|
|
|
offset = (timer_source.source_id * 0x0c) + 0x14;
|
|
|
|
break;
|
|
|
|
|
2013-01-10 03:47:03 +01:00
|
|
|
case SAMSUNG_PWM4:
|
2011-03-12 00:02:07 +01:00
|
|
|
offset = 0x40;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
printk(KERN_ERR "Invalid Timer %d\n", timer_source.source_id);
|
2011-05-08 16:43:07 +02:00
|
|
|
return NULL;
|
2011-03-12 00:02:07 +01:00
|
|
|
}
|
|
|
|
|
2011-05-08 16:43:07 +02:00
|
|
|
return S3C_TIMERREG(offset);
|
|
|
|
}
|
|
|
|
|
2011-03-12 00:02:07 +01:00
|
|
|
/*
|
|
|
|
* Override the global weak sched_clock symbol with this
|
|
|
|
* local implementation which uses the clocksource to get some
|
|
|
|
* better resolution when scheduling the kernel. We accept that
|
|
|
|
* this wraps around for now, since it is just a relative time
|
|
|
|
* stamp. (Inspired by U300 implementation.)
|
|
|
|
*/
|
2013-01-10 03:47:03 +01:00
|
|
|
static u32 notrace samsung_read_sched_clock(void)
|
2011-03-12 00:02:07 +01:00
|
|
|
{
|
2013-01-10 03:47:03 +01:00
|
|
|
void __iomem *reg = samsung_timer_reg();
|
2011-03-12 00:02:07 +01:00
|
|
|
|
2011-05-08 16:43:07 +02:00
|
|
|
if (!reg)
|
2011-03-12 00:02:07 +01:00
|
|
|
return 0;
|
|
|
|
|
2011-12-15 12:19:23 +01:00
|
|
|
return ~__raw_readl(reg);
|
2011-03-12 00:02:07 +01:00
|
|
|
}
|
|
|
|
|
2013-01-10 03:47:03 +01:00
|
|
|
static void __init samsung_clocksource_init(void)
|
2011-03-12 00:02:07 +01:00
|
|
|
{
|
|
|
|
unsigned long pclk;
|
|
|
|
unsigned long clock_rate;
|
|
|
|
|
|
|
|
pclk = clk_get_rate(timerclk);
|
|
|
|
|
2013-01-10 03:47:04 +01:00
|
|
|
clk_set_rate(tdiv_source, pclk / TDIV);
|
2011-03-12 00:02:07 +01:00
|
|
|
clk_set_parent(tin_source, tdiv_source);
|
|
|
|
|
|
|
|
clock_rate = clk_get_rate(tin_source);
|
|
|
|
|
2013-01-10 03:47:03 +01:00
|
|
|
samsung_time_setup(timer_source.source_id, TCNT_MAX);
|
|
|
|
samsung_time_start(timer_source.source_id, PERIODIC);
|
2011-03-12 00:02:07 +01:00
|
|
|
|
2013-01-10 03:47:04 +01:00
|
|
|
setup_sched_clock(samsung_read_sched_clock, TSIZE, clock_rate);
|
2011-06-23 14:43:58 +02:00
|
|
|
|
2013-01-10 03:47:03 +01:00
|
|
|
if (clocksource_mmio_init(samsung_timer_reg(), "samsung_clocksource_timer",
|
2013-01-10 03:47:04 +01:00
|
|
|
clock_rate, 250, TSIZE, clocksource_mmio_readl_down))
|
2013-01-10 03:47:03 +01:00
|
|
|
panic("samsung_clocksource_timer: can't register clocksource\n");
|
2011-03-12 00:02:07 +01:00
|
|
|
}
|
|
|
|
|
2013-01-10 03:47:03 +01:00
|
|
|
static void __init samsung_timer_resources(void)
|
2011-03-12 00:02:07 +01:00
|
|
|
{
|
|
|
|
|
|
|
|
unsigned long event_id = timer_source.event_id;
|
|
|
|
unsigned long source_id = timer_source.source_id;
|
2011-06-14 12:12:27 +02:00
|
|
|
char devname[15];
|
2011-03-12 00:02:07 +01:00
|
|
|
|
|
|
|
timerclk = clk_get(NULL, "timers");
|
|
|
|
if (IS_ERR(timerclk))
|
|
|
|
panic("failed to get timers clock for timer");
|
|
|
|
|
|
|
|
clk_enable(timerclk);
|
|
|
|
|
2011-06-14 12:12:27 +02:00
|
|
|
sprintf(devname, "s3c24xx-pwm.%lu", event_id);
|
|
|
|
s3c_device_timer[event_id].id = event_id;
|
|
|
|
s3c_device_timer[event_id].dev.init_name = devname;
|
|
|
|
|
2011-03-12 00:02:07 +01:00
|
|
|
tin_event = clk_get(&s3c_device_timer[event_id].dev, "pwm-tin");
|
|
|
|
if (IS_ERR(tin_event))
|
|
|
|
panic("failed to get pwm-tin clock for event timer");
|
|
|
|
|
|
|
|
tdiv_event = clk_get(&s3c_device_timer[event_id].dev, "pwm-tdiv");
|
|
|
|
if (IS_ERR(tdiv_event))
|
|
|
|
panic("failed to get pwm-tdiv clock for event timer");
|
|
|
|
|
|
|
|
clk_enable(tin_event);
|
|
|
|
|
2011-06-14 12:12:27 +02:00
|
|
|
sprintf(devname, "s3c24xx-pwm.%lu", source_id);
|
|
|
|
s3c_device_timer[source_id].id = source_id;
|
|
|
|
s3c_device_timer[source_id].dev.init_name = devname;
|
|
|
|
|
2011-03-12 00:02:07 +01:00
|
|
|
tin_source = clk_get(&s3c_device_timer[source_id].dev, "pwm-tin");
|
|
|
|
if (IS_ERR(tin_source))
|
|
|
|
panic("failed to get pwm-tin clock for source timer");
|
|
|
|
|
|
|
|
tdiv_source = clk_get(&s3c_device_timer[source_id].dev, "pwm-tdiv");
|
|
|
|
if (IS_ERR(tdiv_source))
|
|
|
|
panic("failed to get pwm-tdiv clock for source timer");
|
|
|
|
|
|
|
|
clk_enable(tin_source);
|
|
|
|
}
|
|
|
|
|
2013-01-10 03:47:03 +01:00
|
|
|
void __init samsung_timer_init(void)
|
2011-03-12 00:02:07 +01:00
|
|
|
{
|
2013-01-10 03:47:03 +01:00
|
|
|
samsung_timer_resources();
|
|
|
|
samsung_clockevent_init();
|
|
|
|
samsung_clocksource_init();
|
2011-03-12 00:02:07 +01:00
|
|
|
}
|