csrc-octeon.c 3.0 KB

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