/* MN10300 Low level time management1*2* Copyright (C) 2007-2008 Red Hat, Inc. All Rights Reserved.3* Written by David Howells ([email protected])4* - Derived from arch/i386/kernel/time.c5*6* This program is free software; you can redistribute it and/or7* modify it under the terms of the GNU General Public Licence8* as published by the Free Software Foundation; either version9* 2 of the Licence, or (at your option) any later version.10*/11#include <linux/sched.h>12#include <linux/kernel.h>13#include <linux/interrupt.h>14#include <linux/time.h>15#include <linux/init.h>16#include <linux/smp.h>17#include <linux/profile.h>18#include <linux/cnt32_to_63.h>19#include <linux/clocksource.h>20#include <linux/clockchips.h>21#include <asm/irq.h>22#include <asm/div64.h>23#include <asm/processor.h>24#include <asm/intctl-regs.h>25#include <asm/rtc.h>26#include "internal.h"2728static unsigned long mn10300_last_tsc; /* time-stamp counter at last time29* interrupt occurred */3031static unsigned long sched_clock_multiplier;3233/*34* scheduler clock - returns current time in nanosec units.35*/36unsigned long long sched_clock(void)37{38union {39unsigned long long ll;40unsigned l[2];41} tsc64, result;42unsigned long tmp;43unsigned product[3]; /* 96-bit intermediate value */4445/* cnt32_to_63() is not safe with preemption */46preempt_disable();4748/* expand the tsc to 64-bits.49* - sched_clock() must be called once a minute or better or the50* following will go horribly wrong - see cnt32_to_63()51*/52tsc64.ll = cnt32_to_63(get_cycles()) & 0x7fffffffffffffffULL;5354preempt_enable();5556/* scale the 64-bit TSC value to a nanosecond value via a 96-bit57* intermediate58*/59asm("mulu %2,%0,%3,%0 \n" /* LSW * mult -> 0:%3:%0 */60"mulu %2,%1,%2,%1 \n" /* MSW * mult -> %2:%1:0 */61"add %3,%1 \n"62"addc 0,%2 \n" /* result in %2:%1:%0 */63: "=r"(product[0]), "=r"(product[1]), "=r"(product[2]), "=r"(tmp)64: "0"(tsc64.l[0]), "1"(tsc64.l[1]), "2"(sched_clock_multiplier)65: "cc");6667result.l[0] = product[1] << 16 | product[0] >> 16;68result.l[1] = product[2] << 16 | product[1] >> 16;6970return result.ll;71}7273/*74* initialise the scheduler clock75*/76static void __init mn10300_sched_clock_init(void)77{78sched_clock_multiplier =79__muldiv64u(NSEC_PER_SEC, 1 << 16, MN10300_TSCCLK);80}8182/**83* local_timer_interrupt - Local timer interrupt handler84*85* Handle local timer interrupts for this CPU. They may have been propagated86* to this CPU from the CPU that actually gets them by way of an IPI.87*/88irqreturn_t local_timer_interrupt(void)89{90profile_tick(CPU_PROFILING);91update_process_times(user_mode(get_irq_regs()));92return IRQ_HANDLED;93}9495/*96* initialise the various timers used by the main part of the kernel97*/98void __init time_init(void)99{100/* we need the prescalar running to be able to use IOCLK/8101* - IOCLK runs at 1/4 (ST5 open) or 1/8 (ST5 closed) internal CPU clock102* - IOCLK runs at Fosc rate (crystal speed)103*/104TMPSCNT |= TMPSCNT_ENABLE;105106init_clocksource();107108printk(KERN_INFO109"timestamp counter I/O clock running at %lu.%02lu"110" (calibrated against RTC)\n",111MN10300_TSCCLK / 1000000, (MN10300_TSCCLK / 10000) % 100);112113mn10300_last_tsc = read_timestamp_counter();114115init_clockevents();116117#ifdef CONFIG_MN10300_WD_TIMER118/* start the watchdog timer */119watchdog_go();120#endif121122mn10300_sched_clock_init();123}124125126