David Daney | 5b3b168 | 2009-01-08 16:46:40 -0800 | [diff] [blame] | 1 | /* |
| 2 | * This file is subject to the terms and conditions of the GNU General Public |
| 3 | * License. See the file "COPYING" in the main directory of this archive |
| 4 | * for more details. |
| 5 | * |
| 6 | * Copyright (C) 2007 by Ralf Baechle |
| 7 | */ |
| 8 | #include <linux/clocksource.h> |
| 9 | #include <linux/init.h> |
| 10 | |
| 11 | #include <asm/time.h> |
| 12 | |
| 13 | #include <asm/octeon/octeon.h> |
| 14 | #include <asm/octeon/cvmx-ipd-defs.h> |
| 15 | |
| 16 | /* |
| 17 | * Set the current core's cvmcount counter to the value of the |
| 18 | * IPD_CLK_COUNT. We do this on all cores as they are brought |
| 19 | * on-line. This allows for a read from a local cpu register to |
| 20 | * access a synchronized counter. |
| 21 | * |
| 22 | */ |
| 23 | void octeon_init_cvmcount(void) |
| 24 | { |
| 25 | unsigned long flags; |
| 26 | unsigned loops = 2; |
| 27 | |
| 28 | /* Clobber loops so GCC will not unroll the following while loop. */ |
| 29 | asm("" : "+r" (loops)); |
| 30 | |
| 31 | local_irq_save(flags); |
| 32 | /* |
| 33 | * Loop several times so we are executing from the cache, |
| 34 | * which should give more deterministic timing. |
| 35 | */ |
| 36 | while (loops--) |
| 37 | write_c0_cvmcount(cvmx_read_csr(CVMX_IPD_CLK_COUNT)); |
| 38 | local_irq_restore(flags); |
| 39 | } |
| 40 | |
Coly Li | d0ce9a5 | 2009-04-23 03:06:06 +0800 | [diff] [blame] | 41 | static cycle_t octeon_cvmcount_read(struct clocksource *cs) |
David Daney | 5b3b168 | 2009-01-08 16:46:40 -0800 | [diff] [blame] | 42 | { |
| 43 | return read_c0_cvmcount(); |
| 44 | } |
| 45 | |
| 46 | static struct clocksource clocksource_mips = { |
| 47 | .name = "OCTEON_CVMCOUNT", |
| 48 | .read = octeon_cvmcount_read, |
| 49 | .mask = CLOCKSOURCE_MASK(64), |
| 50 | .flags = CLOCK_SOURCE_IS_CONTINUOUS, |
| 51 | }; |
| 52 | |
David Daney | c6a3c85 | 2009-12-23 13:18:54 -0800 | [diff] [blame] | 53 | unsigned long long notrace sched_clock(void) |
| 54 | { |
David Daney | 0e8a1d8 | 2010-01-08 14:47:36 -0800 | [diff] [blame] | 55 | /* 64-bit arithmatic can overflow, so use 128-bit. */ |
David Daney | 0e8a1d8 | 2010-01-08 14:47:36 -0800 | [diff] [blame] | 56 | u64 t1, t2, t3; |
| 57 | unsigned long long rv; |
| 58 | u64 mult = clocksource_mips.mult; |
| 59 | u64 shift = clocksource_mips.shift; |
| 60 | u64 cnt = read_c0_cvmcount(); |
| 61 | |
| 62 | asm ( |
| 63 | "dmultu\t%[cnt],%[mult]\n\t" |
| 64 | "nor\t%[t1],$0,%[shift]\n\t" |
| 65 | "mfhi\t%[t2]\n\t" |
| 66 | "mflo\t%[t3]\n\t" |
| 67 | "dsll\t%[t2],%[t2],1\n\t" |
| 68 | "dsrlv\t%[rv],%[t3],%[shift]\n\t" |
| 69 | "dsllv\t%[t1],%[t2],%[t1]\n\t" |
| 70 | "or\t%[rv],%[t1],%[rv]\n\t" |
| 71 | : [rv] "=&r" (rv), [t1] "=&r" (t1), [t2] "=&r" (t2), [t3] "=&r" (t3) |
| 72 | : [cnt] "r" (cnt), [mult] "r" (mult), [shift] "r" (shift) |
| 73 | : "hi", "lo"); |
| 74 | return rv; |
David Daney | c6a3c85 | 2009-12-23 13:18:54 -0800 | [diff] [blame] | 75 | } |
| 76 | |
David Daney | 5b3b168 | 2009-01-08 16:46:40 -0800 | [diff] [blame] | 77 | void __init plat_time_init(void) |
| 78 | { |
| 79 | clocksource_mips.rating = 300; |
| 80 | clocksource_set_clock(&clocksource_mips, mips_hpt_frequency); |
| 81 | clocksource_register(&clocksource_mips); |
| 82 | } |
David Daney | ca14812 | 2010-06-01 13:18:15 -0700 | [diff] [blame^] | 83 | |
| 84 | static u64 octeon_udelay_factor; |
| 85 | static u64 octeon_ndelay_factor; |
| 86 | |
| 87 | void __init octeon_setup_delays(void) |
| 88 | { |
| 89 | octeon_udelay_factor = octeon_get_clock_rate() / 1000000; |
| 90 | /* |
| 91 | * For __ndelay we divide by 2^16, so the factor is multiplied |
| 92 | * by the same amount. |
| 93 | */ |
| 94 | octeon_ndelay_factor = (octeon_udelay_factor * 0x10000ull) / 1000ull; |
| 95 | |
| 96 | preset_lpj = octeon_get_clock_rate() / HZ; |
| 97 | } |
| 98 | |
| 99 | void __udelay(unsigned long us) |
| 100 | { |
| 101 | u64 cur, end, inc; |
| 102 | |
| 103 | cur = read_c0_cvmcount(); |
| 104 | |
| 105 | inc = us * octeon_udelay_factor; |
| 106 | end = cur + inc; |
| 107 | |
| 108 | while (end > cur) |
| 109 | cur = read_c0_cvmcount(); |
| 110 | } |
| 111 | EXPORT_SYMBOL(__udelay); |
| 112 | |
| 113 | void __ndelay(unsigned long ns) |
| 114 | { |
| 115 | u64 cur, end, inc; |
| 116 | |
| 117 | cur = read_c0_cvmcount(); |
| 118 | |
| 119 | inc = ((ns * octeon_ndelay_factor) >> 16); |
| 120 | end = cur + inc; |
| 121 | |
| 122 | while (end > cur) |
| 123 | cur = read_c0_cvmcount(); |
| 124 | } |
| 125 | EXPORT_SYMBOL(__ndelay); |
| 126 | |
| 127 | void __delay(unsigned long loops) |
| 128 | { |
| 129 | u64 cur, end; |
| 130 | |
| 131 | cur = read_c0_cvmcount(); |
| 132 | end = cur + loops; |
| 133 | |
| 134 | while (end > cur) |
| 135 | cur = read_c0_cvmcount(); |
| 136 | } |
| 137 | EXPORT_SYMBOL(__delay); |