csrc-octeon.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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, 2010 Cavium Networks, 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/time.h>
  15. #include <asm/octeon/octeon.h>
  16. #include <asm/octeon/cvmx-ipd-defs.h>
  17. #include <asm/octeon/cvmx-mio-defs.h>
  18. /*
  19. * Set the current core's cvmcount counter to the value of the
  20. * IPD_CLK_COUNT. We do this on all cores as they are brought
  21. * on-line. This allows for a read from a local cpu register to
  22. * access a synchronized counter.
  23. *
  24. * On CPU_CAVIUM_OCTEON2 the IPD_CLK_COUNT is scaled by rdiv/sdiv.
  25. */
  26. void octeon_init_cvmcount(void)
  27. {
  28. unsigned long flags;
  29. unsigned loops = 2;
  30. u64 f = 0;
  31. u64 rdiv = 0;
  32. u64 sdiv = 0;
  33. if (current_cpu_type() == CPU_CAVIUM_OCTEON2) {
  34. union cvmx_mio_rst_boot rst_boot;
  35. rst_boot.u64 = cvmx_read_csr(CVMX_MIO_RST_BOOT);
  36. rdiv = rst_boot.s.c_mul; /* CPU clock */
  37. sdiv = rst_boot.s.pnr_mul; /* I/O clock */
  38. f = (0x8000000000000000ull / sdiv) * 2;
  39. }
  40. /* Clobber loops so GCC will not unroll the following while loop. */
  41. asm("" : "+r" (loops));
  42. local_irq_save(flags);
  43. /*
  44. * Loop several times so we are executing from the cache,
  45. * which should give more deterministic timing.
  46. */
  47. while (loops--) {
  48. u64 ipd_clk_count = cvmx_read_csr(CVMX_IPD_CLK_COUNT);
  49. if (rdiv != 0) {
  50. ipd_clk_count *= rdiv;
  51. if (f != 0) {
  52. asm("dmultu\t%[cnt],%[f]\n\t"
  53. "mfhi\t%[cnt]"
  54. : [cnt] "+r" (ipd_clk_count),
  55. [f] "=r" (f)
  56. : : "hi", "lo");
  57. }
  58. }
  59. write_c0_cvmcount(ipd_clk_count);
  60. }
  61. local_irq_restore(flags);
  62. }
  63. static cycle_t octeon_cvmcount_read(struct clocksource *cs)
  64. {
  65. return read_c0_cvmcount();
  66. }
  67. static struct clocksource clocksource_mips = {
  68. .name = "OCTEON_CVMCOUNT",
  69. .read = octeon_cvmcount_read,
  70. .mask = CLOCKSOURCE_MASK(64),
  71. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  72. };
  73. unsigned long long notrace sched_clock(void)
  74. {
  75. /* 64-bit arithmatic can overflow, so use 128-bit. */
  76. u64 t1, t2, t3;
  77. unsigned long long rv;
  78. u64 mult = clocksource_mips.mult;
  79. u64 shift = clocksource_mips.shift;
  80. u64 cnt = read_c0_cvmcount();
  81. asm (
  82. "dmultu\t%[cnt],%[mult]\n\t"
  83. "nor\t%[t1],$0,%[shift]\n\t"
  84. "mfhi\t%[t2]\n\t"
  85. "mflo\t%[t3]\n\t"
  86. "dsll\t%[t2],%[t2],1\n\t"
  87. "dsrlv\t%[rv],%[t3],%[shift]\n\t"
  88. "dsllv\t%[t1],%[t2],%[t1]\n\t"
  89. "or\t%[rv],%[t1],%[rv]\n\t"
  90. : [rv] "=&r" (rv), [t1] "=&r" (t1), [t2] "=&r" (t2), [t3] "=&r" (t3)
  91. : [cnt] "r" (cnt), [mult] "r" (mult), [shift] "r" (shift)
  92. : "hi", "lo");
  93. return rv;
  94. }
  95. void __init plat_time_init(void)
  96. {
  97. clocksource_mips.rating = 300;
  98. clocksource_register_hz(&clocksource_mips, octeon_get_clock_rate());
  99. }
  100. static u64 octeon_udelay_factor;
  101. static u64 octeon_ndelay_factor;
  102. void __init octeon_setup_delays(void)
  103. {
  104. octeon_udelay_factor = octeon_get_clock_rate() / 1000000;
  105. /*
  106. * For __ndelay we divide by 2^16, so the factor is multiplied
  107. * by the same amount.
  108. */
  109. octeon_ndelay_factor = (octeon_udelay_factor * 0x10000ull) / 1000ull;
  110. preset_lpj = octeon_get_clock_rate() / HZ;
  111. }
  112. void __udelay(unsigned long us)
  113. {
  114. u64 cur, end, inc;
  115. cur = read_c0_cvmcount();
  116. inc = us * octeon_udelay_factor;
  117. end = cur + inc;
  118. while (end > cur)
  119. cur = read_c0_cvmcount();
  120. }
  121. EXPORT_SYMBOL(__udelay);
  122. void __ndelay(unsigned long ns)
  123. {
  124. u64 cur, end, inc;
  125. cur = read_c0_cvmcount();
  126. inc = ((ns * octeon_ndelay_factor) >> 16);
  127. end = cur + inc;
  128. while (end > cur)
  129. cur = read_c0_cvmcount();
  130. }
  131. EXPORT_SYMBOL(__ndelay);
  132. void __delay(unsigned long loops)
  133. {
  134. u64 cur, end;
  135. cur = read_c0_cvmcount();
  136. end = cur + loops;
  137. while (end > cur)
  138. cur = read_c0_cvmcount();
  139. }
  140. EXPORT_SYMBOL(__delay);