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