csrc-octeon.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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/init.h>
  11. #include <linux/smp.h>
  12. #include <asm/cpu-info.h>
  13. #include <asm/time.h>
  14. #include <asm/octeon/octeon.h>
  15. #include <asm/octeon/cvmx-ipd-defs.h>
  16. #include <asm/octeon/cvmx-mio-defs.h>
  17. /*
  18. * Set the current core's cvmcount counter to the value of the
  19. * IPD_CLK_COUNT. We do this on all cores as they are brought
  20. * on-line. This allows for a read from a local cpu register to
  21. * access a synchronized counter.
  22. *
  23. * On CPU_CAVIUM_OCTEON2 the IPD_CLK_COUNT is scaled by rdiv/sdiv.
  24. */
  25. void octeon_init_cvmcount(void)
  26. {
  27. unsigned long flags;
  28. unsigned loops = 2;
  29. u64 f = 0;
  30. u64 rdiv = 0;
  31. u64 sdiv = 0;
  32. if (current_cpu_type() == CPU_CAVIUM_OCTEON2) {
  33. union cvmx_mio_rst_boot rst_boot;
  34. rst_boot.u64 = cvmx_read_csr(CVMX_MIO_RST_BOOT);
  35. rdiv = rst_boot.s.c_mul; /* CPU clock */
  36. sdiv = rst_boot.s.pnr_mul; /* I/O clock */
  37. f = (0x8000000000000000ull / sdiv) * 2;
  38. }
  39. /* Clobber loops so GCC will not unroll the following while loop. */
  40. asm("" : "+r" (loops));
  41. local_irq_save(flags);
  42. /*
  43. * Loop several times so we are executing from the cache,
  44. * which should give more deterministic timing.
  45. */
  46. while (loops--) {
  47. u64 ipd_clk_count = cvmx_read_csr(CVMX_IPD_CLK_COUNT);
  48. if (rdiv != 0) {
  49. ipd_clk_count *= rdiv;
  50. if (f != 0) {
  51. asm("dmultu\t%[cnt],%[f]\n\t"
  52. "mfhi\t%[cnt]"
  53. : [cnt] "+r" (ipd_clk_count),
  54. [f] "=r" (f)
  55. : : "hi", "lo");
  56. }
  57. }
  58. write_c0_cvmcount(ipd_clk_count);
  59. }
  60. local_irq_restore(flags);
  61. }
  62. static cycle_t octeon_cvmcount_read(struct clocksource *cs)
  63. {
  64. return read_c0_cvmcount();
  65. }
  66. static struct clocksource clocksource_mips = {
  67. .name = "OCTEON_CVMCOUNT",
  68. .read = octeon_cvmcount_read,
  69. .mask = CLOCKSOURCE_MASK(64),
  70. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  71. };
  72. unsigned long long notrace sched_clock(void)
  73. {
  74. /* 64-bit arithmatic can overflow, so use 128-bit. */
  75. u64 t1, t2, t3;
  76. unsigned long long rv;
  77. u64 mult = clocksource_mips.mult;
  78. u64 shift = clocksource_mips.shift;
  79. u64 cnt = read_c0_cvmcount();
  80. asm (
  81. "dmultu\t%[cnt],%[mult]\n\t"
  82. "nor\t%[t1],$0,%[shift]\n\t"
  83. "mfhi\t%[t2]\n\t"
  84. "mflo\t%[t3]\n\t"
  85. "dsll\t%[t2],%[t2],1\n\t"
  86. "dsrlv\t%[rv],%[t3],%[shift]\n\t"
  87. "dsllv\t%[t1],%[t2],%[t1]\n\t"
  88. "or\t%[rv],%[t1],%[rv]\n\t"
  89. : [rv] "=&r" (rv), [t1] "=&r" (t1), [t2] "=&r" (t2), [t3] "=&r" (t3)
  90. : [cnt] "r" (cnt), [mult] "r" (mult), [shift] "r" (shift)
  91. : "hi", "lo");
  92. return rv;
  93. }
  94. void __init plat_time_init(void)
  95. {
  96. clocksource_mips.rating = 300;
  97. clocksource_set_clock(&clocksource_mips, octeon_get_clock_rate());
  98. clocksource_register(&clocksource_mips);
  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);