csrc-octeon.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. * Copyright (C) 2009, 2012 Cavium, Inc.
  8. */
  9. #include <linux/clocksource.h>
  10. #include <linux/sched/clock.h>
  11. #include <linux/export.h>
  12. #include <linux/init.h>
  13. #include <linux/smp.h>
  14. #include <asm/cpu-info.h>
  15. #include <asm/cpu-type.h>
  16. #include <asm/time.h>
  17. #include <asm/octeon/octeon.h>
  18. #include <asm/octeon/cvmx-ipd-defs.h>
  19. #include <asm/octeon/cvmx-mio-defs.h>
  20. #include <asm/octeon/cvmx-rst-defs.h>
  21. #include <asm/octeon/cvmx-fpa-defs.h>
  22. static u64 f;
  23. static u64 rdiv;
  24. static u64 sdiv;
  25. static u64 octeon_udelay_factor;
  26. static u64 octeon_ndelay_factor;
  27. void __init octeon_setup_delays(void)
  28. {
  29. octeon_udelay_factor = octeon_get_clock_rate() / 1000000;
  30. /*
  31. * For __ndelay we divide by 2^16, so the factor is multiplied
  32. * by the same amount.
  33. */
  34. octeon_ndelay_factor = (octeon_udelay_factor * 0x10000ull) / 1000ull;
  35. preset_lpj = octeon_get_clock_rate() / HZ;
  36. if (current_cpu_type() == CPU_CAVIUM_OCTEON2) {
  37. union cvmx_mio_rst_boot rst_boot;
  38. rst_boot.u64 = cvmx_read_csr(CVMX_MIO_RST_BOOT);
  39. rdiv = rst_boot.s.c_mul; /* CPU clock */
  40. sdiv = rst_boot.s.pnr_mul; /* I/O clock */
  41. f = (0x8000000000000000ull / sdiv) * 2;
  42. } else if (current_cpu_type() == CPU_CAVIUM_OCTEON3) {
  43. union cvmx_rst_boot rst_boot;
  44. rst_boot.u64 = cvmx_read_csr(CVMX_RST_BOOT);
  45. rdiv = rst_boot.s.c_mul; /* CPU clock */
  46. sdiv = rst_boot.s.pnr_mul; /* I/O clock */
  47. f = (0x8000000000000000ull / sdiv) * 2;
  48. }
  49. }
  50. /*
  51. * Set the current core's cvmcount counter to the value of the
  52. * IPD_CLK_COUNT. We do this on all cores as they are brought
  53. * on-line. This allows for a read from a local cpu register to
  54. * access a synchronized counter.
  55. *
  56. * On CPU_CAVIUM_OCTEON2 the IPD_CLK_COUNT is scaled by rdiv/sdiv.
  57. */
  58. void octeon_init_cvmcount(void)
  59. {
  60. u64 clk_reg;
  61. unsigned long flags;
  62. unsigned loops = 2;
  63. clk_reg = octeon_has_feature(OCTEON_FEATURE_FPA3) ?
  64. CVMX_FPA_CLK_COUNT : CVMX_IPD_CLK_COUNT;
  65. /* Clobber loops so GCC will not unroll the following while loop. */
  66. asm("" : "+r" (loops));
  67. local_irq_save(flags);
  68. /*
  69. * Loop several times so we are executing from the cache,
  70. * which should give more deterministic timing.
  71. */
  72. while (loops--) {
  73. u64 clk_count = cvmx_read_csr(clk_reg);
  74. if (rdiv != 0) {
  75. clk_count *= rdiv;
  76. if (f != 0) {
  77. asm("dmultu\t%[cnt],%[f]\n\t"
  78. "mfhi\t%[cnt]"
  79. : [cnt] "+r" (clk_count)
  80. : [f] "r" (f)
  81. : "hi", "lo");
  82. }
  83. }
  84. write_c0_cvmcount(clk_count);
  85. }
  86. local_irq_restore(flags);
  87. }
  88. static u64 octeon_cvmcount_read(struct clocksource *cs)
  89. {
  90. return read_c0_cvmcount();
  91. }
  92. static struct clocksource clocksource_mips = {
  93. .name = "OCTEON_CVMCOUNT",
  94. .read = octeon_cvmcount_read,
  95. .mask = CLOCKSOURCE_MASK(64),
  96. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  97. };
  98. unsigned long long notrace sched_clock(void)
  99. {
  100. /* 64-bit arithmatic can overflow, so use 128-bit. */
  101. u64 t1, t2, t3;
  102. unsigned long long rv;
  103. u64 mult = clocksource_mips.mult;
  104. u64 shift = clocksource_mips.shift;
  105. u64 cnt = read_c0_cvmcount();
  106. asm (
  107. "dmultu\t%[cnt],%[mult]\n\t"
  108. "nor\t%[t1],$0,%[shift]\n\t"
  109. "mfhi\t%[t2]\n\t"
  110. "mflo\t%[t3]\n\t"
  111. "dsll\t%[t2],%[t2],1\n\t"
  112. "dsrlv\t%[rv],%[t3],%[shift]\n\t"
  113. "dsllv\t%[t1],%[t2],%[t1]\n\t"
  114. "or\t%[rv],%[t1],%[rv]\n\t"
  115. : [rv] "=&r" (rv), [t1] "=&r" (t1), [t2] "=&r" (t2), [t3] "=&r" (t3)
  116. : [cnt] "r" (cnt), [mult] "r" (mult), [shift] "r" (shift)
  117. : "hi", "lo");
  118. return rv;
  119. }
  120. void __init plat_time_init(void)
  121. {
  122. clocksource_mips.rating = 300;
  123. clocksource_register_hz(&clocksource_mips, octeon_get_clock_rate());
  124. }
  125. void __udelay(unsigned long us)
  126. {
  127. u64 cur, end, inc;
  128. cur = read_c0_cvmcount();
  129. inc = us * octeon_udelay_factor;
  130. end = cur + inc;
  131. while (end > cur)
  132. cur = read_c0_cvmcount();
  133. }
  134. EXPORT_SYMBOL(__udelay);
  135. void __ndelay(unsigned long ns)
  136. {
  137. u64 cur, end, inc;
  138. cur = read_c0_cvmcount();
  139. inc = ((ns * octeon_ndelay_factor) >> 16);
  140. end = cur + inc;
  141. while (end > cur)
  142. cur = read_c0_cvmcount();
  143. }
  144. EXPORT_SYMBOL(__ndelay);
  145. void __delay(unsigned long loops)
  146. {
  147. u64 cur, end;
  148. cur = read_c0_cvmcount();
  149. end = cur + loops;
  150. while (end > cur)
  151. cur = read_c0_cvmcount();
  152. }
  153. EXPORT_SYMBOL(__delay);
  154. /**
  155. * octeon_io_clk_delay - wait for a given number of io clock cycles to pass.
  156. *
  157. * We scale the wait by the clock ratio, and then wait for the
  158. * corresponding number of core clocks.
  159. *
  160. * @count: The number of clocks to wait.
  161. */
  162. void octeon_io_clk_delay(unsigned long count)
  163. {
  164. u64 cur, end;
  165. cur = read_c0_cvmcount();
  166. if (rdiv != 0) {
  167. end = count * rdiv;
  168. if (f != 0) {
  169. asm("dmultu\t%[cnt],%[f]\n\t"
  170. "mfhi\t%[cnt]"
  171. : [cnt] "+r" (end)
  172. : [f] "r" (f)
  173. : "hi", "lo");
  174. }
  175. end = cur + end;
  176. } else {
  177. end = cur + count;
  178. }
  179. while (end > cur)
  180. cur = read_c0_cvmcount();
  181. }
  182. EXPORT_SYMBOL(octeon_io_clk_delay);