Path: blob/master/arch/mips/cavium-octeon/csrc-octeon.c
10818 views
/*1* This file is subject to the terms and conditions of the GNU General Public2* License. See the file "COPYING" in the main directory of this archive3* for more details.4*5* Copyright (C) 2007 by Ralf Baechle6* Copyright (C) 2009, 2010 Cavium Networks, Inc.7*/8#include <linux/clocksource.h>9#include <linux/init.h>10#include <linux/smp.h>1112#include <asm/cpu-info.h>13#include <asm/time.h>1415#include <asm/octeon/octeon.h>16#include <asm/octeon/cvmx-ipd-defs.h>17#include <asm/octeon/cvmx-mio-defs.h>1819/*20* Set the current core's cvmcount counter to the value of the21* IPD_CLK_COUNT. We do this on all cores as they are brought22* on-line. This allows for a read from a local cpu register to23* access a synchronized counter.24*25* On CPU_CAVIUM_OCTEON2 the IPD_CLK_COUNT is scaled by rdiv/sdiv.26*/27void octeon_init_cvmcount(void)28{29unsigned long flags;30unsigned loops = 2;31u64 f = 0;32u64 rdiv = 0;33u64 sdiv = 0;34if (current_cpu_type() == CPU_CAVIUM_OCTEON2) {35union cvmx_mio_rst_boot rst_boot;36rst_boot.u64 = cvmx_read_csr(CVMX_MIO_RST_BOOT);37rdiv = rst_boot.s.c_mul; /* CPU clock */38sdiv = rst_boot.s.pnr_mul; /* I/O clock */39f = (0x8000000000000000ull / sdiv) * 2;40}414243/* Clobber loops so GCC will not unroll the following while loop. */44asm("" : "+r" (loops));4546local_irq_save(flags);47/*48* Loop several times so we are executing from the cache,49* which should give more deterministic timing.50*/51while (loops--) {52u64 ipd_clk_count = cvmx_read_csr(CVMX_IPD_CLK_COUNT);53if (rdiv != 0) {54ipd_clk_count *= rdiv;55if (f != 0) {56asm("dmultu\t%[cnt],%[f]\n\t"57"mfhi\t%[cnt]"58: [cnt] "+r" (ipd_clk_count),59[f] "=r" (f)60: : "hi", "lo");61}62}63write_c0_cvmcount(ipd_clk_count);64}65local_irq_restore(flags);66}6768static cycle_t octeon_cvmcount_read(struct clocksource *cs)69{70return read_c0_cvmcount();71}7273static struct clocksource clocksource_mips = {74.name = "OCTEON_CVMCOUNT",75.read = octeon_cvmcount_read,76.mask = CLOCKSOURCE_MASK(64),77.flags = CLOCK_SOURCE_IS_CONTINUOUS,78};7980unsigned long long notrace sched_clock(void)81{82/* 64-bit arithmatic can overflow, so use 128-bit. */83u64 t1, t2, t3;84unsigned long long rv;85u64 mult = clocksource_mips.mult;86u64 shift = clocksource_mips.shift;87u64 cnt = read_c0_cvmcount();8889asm (90"dmultu\t%[cnt],%[mult]\n\t"91"nor\t%[t1],$0,%[shift]\n\t"92"mfhi\t%[t2]\n\t"93"mflo\t%[t3]\n\t"94"dsll\t%[t2],%[t2],1\n\t"95"dsrlv\t%[rv],%[t3],%[shift]\n\t"96"dsllv\t%[t1],%[t2],%[t1]\n\t"97"or\t%[rv],%[t1],%[rv]\n\t"98: [rv] "=&r" (rv), [t1] "=&r" (t1), [t2] "=&r" (t2), [t3] "=&r" (t3)99: [cnt] "r" (cnt), [mult] "r" (mult), [shift] "r" (shift)100: "hi", "lo");101return rv;102}103104void __init plat_time_init(void)105{106clocksource_mips.rating = 300;107clocksource_register_hz(&clocksource_mips, octeon_get_clock_rate());108}109110static u64 octeon_udelay_factor;111static u64 octeon_ndelay_factor;112113void __init octeon_setup_delays(void)114{115octeon_udelay_factor = octeon_get_clock_rate() / 1000000;116/*117* For __ndelay we divide by 2^16, so the factor is multiplied118* by the same amount.119*/120octeon_ndelay_factor = (octeon_udelay_factor * 0x10000ull) / 1000ull;121122preset_lpj = octeon_get_clock_rate() / HZ;123}124125void __udelay(unsigned long us)126{127u64 cur, end, inc;128129cur = read_c0_cvmcount();130131inc = us * octeon_udelay_factor;132end = cur + inc;133134while (end > cur)135cur = read_c0_cvmcount();136}137EXPORT_SYMBOL(__udelay);138139void __ndelay(unsigned long ns)140{141u64 cur, end, inc;142143cur = read_c0_cvmcount();144145inc = ((ns * octeon_ndelay_factor) >> 16);146end = cur + inc;147148while (end > cur)149cur = read_c0_cvmcount();150}151EXPORT_SYMBOL(__ndelay);152153void __delay(unsigned long loops)154{155u64 cur, end;156157cur = read_c0_cvmcount();158end = cur + loops;159160while (end > cur)161cur = read_c0_cvmcount();162}163EXPORT_SYMBOL(__delay);164165166