tsc_timer.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * Copyright (c) 2012 The Chromium OS Authors.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of
  7. * the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  17. * MA 02111-1307 USA
  18. */
  19. #include <common.h>
  20. #include <malloc.h>
  21. #include <asm/io.h>
  22. #include <asm/i8254.h>
  23. #include <asm/ibmpc.h>
  24. #include <asm/msr.h>
  25. #include <asm/u-boot-x86.h>
  26. DECLARE_GLOBAL_DATA_PTR;
  27. void timer_set_base(u64 base)
  28. {
  29. gd->arch.tsc_base = base;
  30. }
  31. /*
  32. * Get the number of CPU time counter ticks since it was read first time after
  33. * restart. This yields a free running counter guaranteed to take almost 6
  34. * years to wrap around even at 100GHz clock rate.
  35. */
  36. u64 __attribute__((no_instrument_function)) get_ticks(void)
  37. {
  38. u64 now_tick = rdtsc();
  39. /* We assume that 0 means the base hasn't been set yet */
  40. if (!gd->arch.tsc_base)
  41. panic("No tick base available");
  42. return now_tick - gd->arch.tsc_base;
  43. }
  44. #define PLATFORM_INFO_MSR 0xce
  45. /* Get the speed of the TSC timer in MHz */
  46. unsigned __attribute__((no_instrument_function)) long get_tbclk_mhz(void)
  47. {
  48. u32 ratio;
  49. u64 platform_info = native_read_msr(PLATFORM_INFO_MSR);
  50. /* 100MHz times Max Non Turbo ratio */
  51. ratio = (platform_info >> 8) & 0xff;
  52. return 100 * ratio;
  53. }
  54. unsigned long get_tbclk(void)
  55. {
  56. return get_tbclk_mhz() * 1000 * 1000;
  57. }
  58. static ulong get_ms_timer(void)
  59. {
  60. return (get_ticks() * 1000) / get_tbclk();
  61. }
  62. ulong get_timer(ulong base)
  63. {
  64. return get_ms_timer() - base;
  65. }
  66. ulong __attribute__((no_instrument_function)) timer_get_us(void)
  67. {
  68. return get_ticks() / get_tbclk_mhz();
  69. }
  70. ulong timer_get_boot_us(void)
  71. {
  72. return timer_get_us();
  73. }
  74. void __udelay(unsigned long usec)
  75. {
  76. u64 now = get_ticks();
  77. u64 stop;
  78. stop = now + usec * get_tbclk_mhz();
  79. while ((int64_t)(stop - get_ticks()) > 0)
  80. ;
  81. }
  82. int timer_init(void)
  83. {
  84. #ifdef CONFIG_SYS_PCAT_TIMER
  85. /* Set up the PCAT timer if required */
  86. pcat_timer_init();
  87. #endif
  88. return 0;
  89. }