Path: blob/master/arch/mips/loongson2ef/common/cs5536/cs5536_mfgpt.c
26495 views
// SPDX-License-Identifier: GPL-2.0-or-later1/*2* CS5536 General timer functions3*4* Copyright (C) 2007 Lemote Inc. & Institute of Computing Technology5* Author: Yanhua, [email protected]6*7* Copyright (C) 2009 Lemote Inc.8* Author: Wu zhangjin, [email protected]9*10* Reference: AMD Geode(TM) CS5536 Companion Device Data Book11*/1213#include <linux/io.h>14#include <linux/init.h>15#include <linux/export.h>16#include <linux/jiffies.h>17#include <linux/spinlock.h>18#include <linux/interrupt.h>19#include <linux/clockchips.h>2021#include <asm/time.h>2223#include <cs5536/cs5536_mfgpt.h>2425static DEFINE_RAW_SPINLOCK(mfgpt_lock);2627static u32 mfgpt_base;2829/*30* Initialize the MFGPT timer.31*32* This is also called after resume to bring the MFGPT into operation again.33*/3435/* disable counter */36void disable_mfgpt0_counter(void)37{38outw(inw(MFGPT0_SETUP) & 0x7fff, MFGPT0_SETUP);39}40EXPORT_SYMBOL(disable_mfgpt0_counter);4142/* enable counter, comparator2 to event mode, 14.318MHz clock */43void enable_mfgpt0_counter(void)44{45outw(0xe310, MFGPT0_SETUP);46}47EXPORT_SYMBOL(enable_mfgpt0_counter);4849static int mfgpt_timer_set_periodic(struct clock_event_device *evt)50{51raw_spin_lock(&mfgpt_lock);5253outw(COMPARE, MFGPT0_CMP2); /* set comparator2 */54outw(0, MFGPT0_CNT); /* set counter to 0 */55enable_mfgpt0_counter();5657raw_spin_unlock(&mfgpt_lock);58return 0;59}6061static int mfgpt_timer_shutdown(struct clock_event_device *evt)62{63if (clockevent_state_periodic(evt) || clockevent_state_oneshot(evt)) {64raw_spin_lock(&mfgpt_lock);65disable_mfgpt0_counter();66raw_spin_unlock(&mfgpt_lock);67}6869return 0;70}7172static struct clock_event_device mfgpt_clockevent = {73.name = "mfgpt",74.features = CLOCK_EVT_FEAT_PERIODIC,7576/* The oneshot mode have very high deviation, don't use it! */77.set_state_shutdown = mfgpt_timer_shutdown,78.set_state_periodic = mfgpt_timer_set_periodic,79.irq = CS5536_MFGPT_INTR,80};8182static irqreturn_t timer_interrupt(int irq, void *dev_id)83{84u32 basehi;8586/*87* get MFGPT base address88*89* NOTE: do not remove me, it's need for the value of mfgpt_base is90* variable91*/92_rdmsr(DIVIL_MSR_REG(DIVIL_LBAR_MFGPT), &basehi, &mfgpt_base);9394/* ack */95outw(inw(MFGPT0_SETUP) | 0x4000, MFGPT0_SETUP);9697mfgpt_clockevent.event_handler(&mfgpt_clockevent);9899return IRQ_HANDLED;100}101102/*103* Initialize the conversion factor and the min/max deltas of the clock event104* structure and register the clock event source with the framework.105*/106void __init setup_mfgpt0_timer(void)107{108u32 basehi;109struct clock_event_device *cd = &mfgpt_clockevent;110unsigned int cpu = smp_processor_id();111112cd->cpumask = cpumask_of(cpu);113clockevent_set_clock(cd, MFGPT_TICK_RATE);114cd->max_delta_ns = clockevent_delta2ns(0xffff, cd);115cd->max_delta_ticks = 0xffff;116cd->min_delta_ns = clockevent_delta2ns(0xf, cd);117cd->min_delta_ticks = 0xf;118119/* Enable MFGPT0 Comparator 2 Output to the Interrupt Mapper */120_wrmsr(DIVIL_MSR_REG(MFGPT_IRQ), 0, 0x100);121122/* Enable Interrupt Gate 5 */123_wrmsr(DIVIL_MSR_REG(PIC_ZSEL_LOW), 0, 0x50000);124125/* get MFGPT base address */126_rdmsr(DIVIL_MSR_REG(DIVIL_LBAR_MFGPT), &basehi, &mfgpt_base);127128clockevents_register_device(cd);129130if (request_irq(CS5536_MFGPT_INTR, timer_interrupt,131IRQF_NOBALANCING | IRQF_TIMER, "timer", NULL))132pr_err("Failed to register timer interrupt\n");133}134135/*136* Since the MFGPT overflows every tick, its not very useful137* to just read by itself. So use jiffies to emulate a free138* running counter:139*/140static u64 mfgpt_read(struct clocksource *cs)141{142unsigned long flags;143int count;144u32 jifs;145static int old_count;146static u32 old_jifs;147148raw_spin_lock_irqsave(&mfgpt_lock, flags);149/*150* Although our caller may have the read side of xtime_lock,151* this is now a seqlock, and we are cheating in this routine152* by having side effects on state that we cannot undo if153* there is a collision on the seqlock and our caller has to154* retry. (Namely, old_jifs and old_count.) So we must treat155* jiffies as volatile despite the lock. We read jiffies156* before latching the timer count to guarantee that although157* the jiffies value might be older than the count (that is,158* the counter may underflow between the last point where159* jiffies was incremented and the point where we latch the160* count), it cannot be newer.161*/162jifs = jiffies;163/* read the count */164count = inw(MFGPT0_CNT);165166/*167* It's possible for count to appear to go the wrong way for this168* reason:169*170* The timer counter underflows, but we haven't handled the resulting171* interrupt and incremented jiffies yet.172*173* Previous attempts to handle these cases intelligently were buggy, so174* we just do the simple thing now.175*/176if (count < old_count && jifs == old_jifs)177count = old_count;178179old_count = count;180old_jifs = jifs;181182raw_spin_unlock_irqrestore(&mfgpt_lock, flags);183184return (u64) (jifs * COMPARE) + count;185}186187static struct clocksource clocksource_mfgpt = {188.name = "mfgpt",189.rating = 120, /* Functional for real use, but not desired */190.read = mfgpt_read,191.mask = CLOCKSOURCE_MASK(32),192};193194int __init init_mfgpt_clocksource(void)195{196if (num_possible_cpus() > 1) /* MFGPT does not scale! */197return 0;198199return clocksource_register_hz(&clocksource_mfgpt, MFGPT_TICK_RATE);200}201202arch_initcall(init_mfgpt_clocksource);203204205