Path: blob/master/arch/mips/loongson/common/cs5536/cs5536_mfgpt.c
10820 views
/*1* CS5536 General timer functions2*3* Copyright (C) 2007 Lemote Inc. & Insititute of Computing Technology4* Author: Yanhua, [email protected]5*6* Copyright (C) 2009 Lemote Inc.7* Author: Wu zhangjin, [email protected]8*9* Reference: AMD Geode(TM) CS5536 Companion Device Data Book10*11* This program is free software; you can redistribute it and/or modify it12* under the terms of the GNU General Public License as published by the13* Free Software Foundation; either version 2 of the License, or (at your14* option) any later version.15*/1617#include <linux/io.h>18#include <linux/init.h>19#include <linux/module.h>20#include <linux/jiffies.h>21#include <linux/spinlock.h>22#include <linux/interrupt.h>23#include <linux/clockchips.h>2425#include <asm/time.h>2627#include <cs5536/cs5536_mfgpt.h>2829DEFINE_SPINLOCK(mfgpt_lock);30EXPORT_SYMBOL(mfgpt_lock);3132static u32 mfgpt_base;3334/*35* Initialize the MFGPT timer.36*37* This is also called after resume to bring the MFGPT into operation again.38*/3940/* disable counter */41void disable_mfgpt0_counter(void)42{43outw(inw(MFGPT0_SETUP) & 0x7fff, MFGPT0_SETUP);44}45EXPORT_SYMBOL(disable_mfgpt0_counter);4647/* enable counter, comparator2 to event mode, 14.318MHz clock */48void enable_mfgpt0_counter(void)49{50outw(0xe310, MFGPT0_SETUP);51}52EXPORT_SYMBOL(enable_mfgpt0_counter);5354static void init_mfgpt_timer(enum clock_event_mode mode,55struct clock_event_device *evt)56{57spin_lock(&mfgpt_lock);5859switch (mode) {60case CLOCK_EVT_MODE_PERIODIC:61outw(COMPARE, MFGPT0_CMP2); /* set comparator2 */62outw(0, MFGPT0_CNT); /* set counter to 0 */63enable_mfgpt0_counter();64break;6566case CLOCK_EVT_MODE_SHUTDOWN:67case CLOCK_EVT_MODE_UNUSED:68if (evt->mode == CLOCK_EVT_MODE_PERIODIC ||69evt->mode == CLOCK_EVT_MODE_ONESHOT)70disable_mfgpt0_counter();71break;7273case CLOCK_EVT_MODE_ONESHOT:74/* The oneshot mode have very high deviation, Not use it! */75break;7677case CLOCK_EVT_MODE_RESUME:78/* Nothing to do here */79break;80}81spin_unlock(&mfgpt_lock);82}8384static struct clock_event_device mfgpt_clockevent = {85.name = "mfgpt",86.features = CLOCK_EVT_FEAT_PERIODIC,87.set_mode = init_mfgpt_timer,88.irq = CS5536_MFGPT_INTR,89};9091static irqreturn_t timer_interrupt(int irq, void *dev_id)92{93u32 basehi;9495/*96* get MFGPT base address97*98* NOTE: do not remove me, it's need for the value of mfgpt_base is99* variable100*/101_rdmsr(DIVIL_MSR_REG(DIVIL_LBAR_MFGPT), &basehi, &mfgpt_base);102103/* ack */104outw(inw(MFGPT0_SETUP) | 0x4000, MFGPT0_SETUP);105106mfgpt_clockevent.event_handler(&mfgpt_clockevent);107108return IRQ_HANDLED;109}110111static struct irqaction irq5 = {112.handler = timer_interrupt,113.flags = IRQF_DISABLED | IRQF_NOBALANCING | IRQF_TIMER,114.name = "timer"115};116117/*118* Initialize the conversion factor and the min/max deltas of the clock event119* structure and register the clock event source with the framework.120*/121void __init setup_mfgpt0_timer(void)122{123u32 basehi;124struct clock_event_device *cd = &mfgpt_clockevent;125unsigned int cpu = smp_processor_id();126127cd->cpumask = cpumask_of(cpu);128clockevent_set_clock(cd, MFGPT_TICK_RATE);129cd->max_delta_ns = clockevent_delta2ns(0xffff, cd);130cd->min_delta_ns = clockevent_delta2ns(0xf, cd);131132/* Enable MFGPT0 Comparator 2 Output to the Interrupt Mapper */133_wrmsr(DIVIL_MSR_REG(MFGPT_IRQ), 0, 0x100);134135/* Enable Interrupt Gate 5 */136_wrmsr(DIVIL_MSR_REG(PIC_ZSEL_LOW), 0, 0x50000);137138/* get MFGPT base address */139_rdmsr(DIVIL_MSR_REG(DIVIL_LBAR_MFGPT), &basehi, &mfgpt_base);140141clockevents_register_device(cd);142143setup_irq(CS5536_MFGPT_INTR, &irq5);144}145146/*147* Since the MFGPT overflows every tick, its not very useful148* to just read by itself. So use jiffies to emulate a free149* running counter:150*/151static cycle_t mfgpt_read(struct clocksource *cs)152{153unsigned long flags;154int count;155u32 jifs;156static int old_count;157static u32 old_jifs;158159spin_lock_irqsave(&mfgpt_lock, flags);160/*161* Although our caller may have the read side of xtime_lock,162* this is now a seqlock, and we are cheating in this routine163* by having side effects on state that we cannot undo if164* there is a collision on the seqlock and our caller has to165* retry. (Namely, old_jifs and old_count.) So we must treat166* jiffies as volatile despite the lock. We read jiffies167* before latching the timer count to guarantee that although168* the jiffies value might be older than the count (that is,169* the counter may underflow between the last point where170* jiffies was incremented and the point where we latch the171* count), it cannot be newer.172*/173jifs = jiffies;174/* read the count */175count = inw(MFGPT0_CNT);176177/*178* It's possible for count to appear to go the wrong way for this179* reason:180*181* The timer counter underflows, but we haven't handled the resulting182* interrupt and incremented jiffies yet.183*184* Previous attempts to handle these cases intelligently were buggy, so185* we just do the simple thing now.186*/187if (count < old_count && jifs == old_jifs)188count = old_count;189190old_count = count;191old_jifs = jifs;192193spin_unlock_irqrestore(&mfgpt_lock, flags);194195return (cycle_t) (jifs * COMPARE) + count;196}197198static struct clocksource clocksource_mfgpt = {199.name = "mfgpt",200.rating = 120, /* Functional for real use, but not desired */201.read = mfgpt_read,202.mask = CLOCKSOURCE_MASK(32),203};204205int __init init_mfgpt_clocksource(void)206{207if (num_possible_cpus() > 1) /* MFGPT does not scale! */208return 0;209210return clocksource_register_hz(&clocksource_mfgpt, MFGPT_TICK_RATE);211}212213arch_initcall(init_mfgpt_clocksource);214215216