ARM: Integrator: pass 'khz' to integrator_time_init
[safe/jmp/linux-2.6] / arch / arm / mach-integrator / core.c
index 8d880cb..d02f0e3 100644 (file)
 #include <linux/termios.h>
 #include <linux/amba/bus.h>
 #include <linux/amba/serial.h>
-
-#include <asm/hardware.h>
+#include <linux/clocksource.h>
+#include <linux/clockchips.h>
+#include <linux/io.h>
+
+#include <asm/clkdev.h>
+#include <mach/clkdev.h>
+#include <mach/hardware.h>
+#include <mach/platform.h>
 #include <asm/irq.h>
-#include <asm/io.h>
 #include <asm/hardware/arm_timer.h>
-#include <asm/arch/cm.h>
+#include <mach/cm.h>
 #include <asm/system.h>
 #include <asm/leds.h>
 #include <asm/mach/time.h>
@@ -35,7 +40,7 @@ static struct amba_pl010_data integrator_uart_data;
 
 static struct amba_device rtc_device = {
        .dev            = {
-               .bus_id = "mb:15",
+               .init_name = "mb:15",
        },
        .res            = {
                .start  = INTEGRATOR_RTC_BASE,
@@ -48,7 +53,7 @@ static struct amba_device rtc_device = {
 
 static struct amba_device uart0_device = {
        .dev            = {
-               .bus_id = "mb:16",
+               .init_name = "mb:16",
                .platform_data = &integrator_uart_data,
        },
        .res            = {
@@ -62,7 +67,7 @@ static struct amba_device uart0_device = {
 
 static struct amba_device uart1_device = {
        .dev            = {
-               .bus_id = "mb:17",
+               .init_name = "mb:17",
                .platform_data = &integrator_uart_data,
        },
        .res            = {
@@ -76,7 +81,7 @@ static struct amba_device uart1_device = {
 
 static struct amba_device kmi0_device = {
        .dev            = {
-               .bus_id = "mb:18",
+               .init_name = "mb:18",
        },
        .res            = {
                .start  = KMI0_BASE,
@@ -89,7 +94,7 @@ static struct amba_device kmi0_device = {
 
 static struct amba_device kmi1_device = {
        .dev            = {
-               .bus_id = "mb:19",
+               .init_name = "mb:19",
        },
        .res            = {
                .start  = KMI1_BASE,
@@ -108,10 +113,42 @@ static struct amba_device *amba_devs[] __initdata = {
        &kmi1_device,
 };
 
+/*
+ * These are fixed clocks.
+ */
+static struct clk clk24mhz = {
+       .rate   = 24000000,
+};
+
+static struct clk uartclk = {
+       .rate   = 14745600,
+};
+
+static struct clk_lookup lookups[] = {
+       {       /* UART0 */
+               .dev_id         = "mb:16",
+               .clk            = &uartclk,
+       }, {    /* UART1 */
+               .dev_id         = "mb:17",
+               .clk            = &uartclk,
+       }, {    /* KMI0 */
+               .dev_id         = "mb:18",
+               .clk            = &clk24mhz,
+       }, {    /* KMI1 */
+               .dev_id         = "mb:19",
+               .clk            = &clk24mhz,
+       }, {    /* MMCI - IntegratorCP */
+               .dev_id         = "mb:1c",
+               .clk            = &uartclk,
+       }
+};
+
 static int __init integrator_init(void)
 {
        int i;
 
+       clkdev_add_table(lookups, ARRAY_SIZE(lookups));
+
        for (i = 0; i < ARRAY_SIZE(amba_devs); i++) {
                struct amba_device *d = amba_devs[i];
                amba_device_register(d, &iomem_resource);
@@ -190,7 +227,6 @@ EXPORT_SYMBOL(cm_control);
 #define TIMER0_VA_BASE (IO_ADDRESS(INTEGRATOR_CT_BASE)+0x00000000)
 #define TIMER1_VA_BASE (IO_ADDRESS(INTEGRATOR_CT_BASE)+0x00000100)
 #define TIMER2_VA_BASE (IO_ADDRESS(INTEGRATOR_CT_BASE)+0x00000200)
-#define VA_IC_BASE     IO_ADDRESS(INTEGRATOR_IC_BASE) 
 
 /*
  * How long is the timer interval?
@@ -206,117 +242,133 @@ EXPORT_SYMBOL(cm_control);
 
 static unsigned long timer_reload;
 
-/*
- * Returns number of ms since last clock interrupt.  Note that interrupts
- * will have been disabled by do_gettimeoffset()
- */
-unsigned long integrator_gettimeoffset(void)
+static void __iomem * const clksrc_base = (void __iomem *)TIMER2_VA_BASE;
+
+static cycle_t timersp_read(struct clocksource *cs)
 {
-       unsigned long ticks1, ticks2, status;
-
-       /*
-        * Get the current number of ticks.  Note that there is a race
-        * condition between us reading the timer and checking for
-        * an interrupt.  We get around this by ensuring that the
-        * counter has not reloaded between our two reads.
-        */
-       ticks2 = readl(TIMER1_VA_BASE + TIMER_VALUE) & 0xffff;
-       do {
-               ticks1 = ticks2;
-               status = __raw_readl(VA_IC_BASE + IRQ_RAW_STATUS);
-               ticks2 = readl(TIMER1_VA_BASE + TIMER_VALUE) & 0xffff;
-       } while (ticks2 > ticks1);
-
-       /*
-        * Number of ticks since last interrupt.
-        */
-       ticks1 = timer_reload - ticks2;
-
-       /*
-        * Interrupt pending?  If so, we've reloaded once already.
-        */
-       if (status & (1 << IRQ_TIMERINT1))
-               ticks1 += timer_reload;
-
-       /*
-        * Convert the ticks to usecs
-        */
-       return TICKS2USECS(ticks1);
+       return ~(readl(clksrc_base + TIMER_VALUE) & 0xffff);
 }
 
+static struct clocksource clocksource_timersp = {
+       .name           = "timer2",
+       .rating         = 200,
+       .read           = timersp_read,
+       .mask           = CLOCKSOURCE_MASK(16),
+       .shift          = 16,
+       .flags          = CLOCK_SOURCE_IS_CONTINUOUS,
+};
+
+static void integrator_clocksource_init(u32 khz)
+{
+       struct clocksource *cs = &clocksource_timersp;
+       void __iomem *base = clksrc_base;
+       u32 ctrl = TIMER_CTRL_ENABLE;
+
+       if (khz >= 1500) {
+               khz /= 16;
+               ctrl = TIMER_CTRL_DIV16;
+       }
+
+       writel(ctrl, base + TIMER_CTRL);
+       writel(0xffff, base + TIMER_LOAD);
+
+       cs->mult = clocksource_khz2mult(khz, cs->shift);
+       clocksource_register(cs);
+}
+
+static void __iomem * const clkevt_base = (void __iomem *)TIMER1_VA_BASE;
+
 /*
  * IRQ handler for the timer
  */
-static irqreturn_t
-integrator_timer_interrupt(int irq, void *dev_id)
+static irqreturn_t integrator_timer_interrupt(int irq, void *dev_id)
 {
-       write_seqlock(&xtime_lock);
-
-       /*
-        * clear the interrupt
-        */
-       writel(1, TIMER1_VA_BASE + TIMER_INTCLR);
-
-       /*
-        * the clock tick routines are only processed on the
-        * primary CPU
-        */
-       if (hard_smp_processor_id() == 0) {
-               timer_tick();
-#ifdef CONFIG_SMP
-               smp_send_timer();
-#endif
-       }
+       struct clock_event_device *evt = dev_id;
 
-#ifdef CONFIG_SMP
-       /*
-        * this is the ARM equivalent of the APIC timer interrupt
-        */
-       update_process_times(user_mode(get_irq_regs()));
-#endif /* CONFIG_SMP */
+       /* clear the interrupt */
+       writel(1, clkevt_base + TIMER_INTCLR);
 
-       write_sequnlock(&xtime_lock);
+       evt->event_handler(evt);
 
        return IRQ_HANDLED;
 }
 
+static void clkevt_set_mode(enum clock_event_mode mode, struct clock_event_device *evt)
+{
+       u32 ctrl = readl(clkevt_base + TIMER_CTRL) & ~TIMER_CTRL_ENABLE;
+
+       BUG_ON(mode == CLOCK_EVT_MODE_ONESHOT);
+
+       if (mode == CLOCK_EVT_MODE_PERIODIC) {
+               writel(ctrl, clkevt_base + TIMER_CTRL);
+               writel(timer_reload, clkevt_base + TIMER_LOAD);
+               ctrl |= TIMER_CTRL_PERIODIC | TIMER_CTRL_ENABLE;
+       }
+
+       writel(ctrl, clkevt_base + TIMER_CTRL);
+}
+
+static int clkevt_set_next_event(unsigned long next, struct clock_event_device *evt)
+{
+       unsigned long ctrl = readl(clkevt_base + TIMER_CTRL);
+
+       writel(ctrl & ~TIMER_CTRL_ENABLE, clkevt_base + TIMER_CTRL);
+       writel(next, clkevt_base + TIMER_LOAD);
+       writel(ctrl | TIMER_CTRL_ENABLE, clkevt_base + TIMER_CTRL);
+
+       return 0;
+}
+
+static struct clock_event_device integrator_clockevent = {
+       .name           = "timer1",
+       .shift          = 34,
+       .features       = CLOCK_EVT_FEAT_PERIODIC,
+       .set_mode       = clkevt_set_mode,
+       .set_next_event = clkevt_set_next_event,
+       .rating         = 300,
+       .cpumask        = cpu_all_mask,
+};
+
 static struct irqaction integrator_timer_irq = {
-       .name           = "Integrator Timer Tick",
-       .flags          = IRQF_DISABLED | IRQF_TIMER,
+       .name           = "timer",
+       .flags          = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
        .handler        = integrator_timer_interrupt,
+       .dev_id         = &integrator_clockevent,
 };
 
-/*
- * Set up timer interrupt, and return the current time in seconds.
- */
-void __init integrator_time_init(unsigned long reload, unsigned int ctrl)
+static void integrator_clockevent_init(u32 khz, unsigned int ctrl)
 {
-       unsigned int timer_ctrl = TIMER_CTRL_ENABLE | TIMER_CTRL_PERIODIC;
+       struct clock_event_device *evt = &integrator_clockevent;
+
+       if (khz * 1000 > 0x100000 * HZ) {
+               khz /= 256;
+               ctrl |= TIMER_CTRL_DIV256;
+       } else if (khz * 1000 > 0x10000 * HZ) {
+               khz /= 16;
+               ctrl |= TIMER_CTRL_DIV16;
+       }
 
-       timer_reload = reload;
-       timer_ctrl |= ctrl;
+       timer_reload = khz * 1000 / HZ;
+       writel(ctrl, clkevt_base + TIMER_CTRL);
 
-       if (timer_reload > 0x100000) {
-               timer_reload >>= 8;
-               timer_ctrl |= TIMER_CTRL_DIV256;
-       } else if (timer_reload > 0x010000) {
-               timer_reload >>= 4;
-               timer_ctrl |= TIMER_CTRL_DIV16;
-       }
+       evt->irq = IRQ_TIMERINT1;
+       evt->mult = div_sc(khz, NSEC_PER_MSEC, evt->shift);
+       evt->max_delta_ns = clockevent_delta2ns(0xffff, evt);
+       evt->min_delta_ns = clockevent_delta2ns(0xf, evt);
 
-       /*
-        * Initialise to a known state (all timers off)
-        */
+       setup_irq(IRQ_TIMERINT1, &integrator_timer_irq);
+       clockevents_register_device(evt);
+}
+
+/*
+ * Set up timer(s).
+ */
+void __init integrator_time_init(u32 khz, unsigned int ctrl)
+{
        writel(0, TIMER0_VA_BASE + TIMER_CTRL);
        writel(0, TIMER1_VA_BASE + TIMER_CTRL);
        writel(0, TIMER2_VA_BASE + TIMER_CTRL);
 
-       writel(timer_reload, TIMER1_VA_BASE + TIMER_LOAD);
-       writel(timer_reload, TIMER1_VA_BASE + TIMER_VALUE);
-       writel(timer_ctrl, TIMER1_VA_BASE + TIMER_CTRL);
-
-       /*
-        * Make irqs happen for the system timer
-        */
-       setup_irq(IRQ_TIMERINT1, &integrator_timer_irq);
+       integrator_clocksource_init(khz);
+       integrator_clockevent_init(khz, ctrl);
 }