csrc-octeon.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. #if (__GNUC__ < 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ <= 3))
  49. u64 t1, t2, t3;
  50. unsigned long long rv;
  51. u64 mult = clocksource_mips.mult;
  52. u64 shift = clocksource_mips.shift;
  53. u64 cnt = read_c0_cvmcount();
  54. asm (
  55. "dmultu\t%[cnt],%[mult]\n\t"
  56. "nor\t%[t1],$0,%[shift]\n\t"
  57. "mfhi\t%[t2]\n\t"
  58. "mflo\t%[t3]\n\t"
  59. "dsll\t%[t2],%[t2],1\n\t"
  60. "dsrlv\t%[rv],%[t3],%[shift]\n\t"
  61. "dsllv\t%[t1],%[t2],%[t1]\n\t"
  62. "or\t%[rv],%[t1],%[rv]\n\t"
  63. : [rv] "=&r" (rv), [t1] "=&r" (t1), [t2] "=&r" (t2), [t3] "=&r" (t3)
  64. : [cnt] "r" (cnt), [mult] "r" (mult), [shift] "r" (shift)
  65. : "hi", "lo");
  66. return rv;
  67. #else
  68. /* GCC > 4.3 do it the easy way. */
  69. unsigned int __attribute__((mode(TI))) t;
  70. t = read_c0_cvmcount();
  71. t = t * clocksource_mips.mult;
  72. return (unsigned long long)(t >> clocksource_mips.shift);
  73. #endif
  74. }
  75. void __init plat_time_init(void)
  76. {
  77. clocksource_mips.rating = 300;
  78. clocksource_set_clock(&clocksource_mips, mips_hpt_frequency);
  79. clocksource_register(&clocksource_mips);
  80. }