time_sh2.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2007,2008 Nobobuhiro Iwamatsu <iwamatsu@nigauri.org>
  4. * Copyright (C) 2008 Renesas Solutions Corp.
  5. *
  6. * (C) Copyright 2003
  7. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  8. */
  9. #include <common.h>
  10. #include <init.h>
  11. #include <time.h>
  12. #include <asm/io.h>
  13. #include <asm/processor.h>
  14. #include <linux/delay.h>
  15. #define CMT_CMCSR_INIT 0x0001 /* PCLK/32 */
  16. #define CMT_CMCSR_CALIB 0x0000
  17. #define CMT_MAX_COUNTER (0xFFFFFFFF)
  18. #define CMT_TIMER_RESET (0xFFFF)
  19. static vu_long cmt0_timer;
  20. static void cmt_timer_start(unsigned int timer)
  21. {
  22. writew(readw(CMSTR) | 0x01, CMSTR);
  23. }
  24. static void cmt_timer_stop(unsigned int timer)
  25. {
  26. writew(readw(CMSTR) & ~0x01, CMSTR);
  27. }
  28. int timer_init(void)
  29. {
  30. cmt0_timer = 0;
  31. /* Divide clock by 32 */
  32. readw(CMCSR_0);
  33. writew(CMT_CMCSR_INIT, CMCSR_0);
  34. /* User Device 0 only */
  35. cmt_timer_stop(0);
  36. writew(CMT_TIMER_RESET, CMCOR_0);
  37. cmt_timer_start(0);
  38. return 0;
  39. }
  40. unsigned long long get_ticks(void)
  41. {
  42. return cmt0_timer;
  43. }
  44. static vu_long cmcnt = 0;
  45. static unsigned long get_usec (void)
  46. {
  47. ulong data = readw(CMCNT_0);
  48. if (data >= cmcnt)
  49. cmcnt = data - cmcnt;
  50. else
  51. cmcnt = (CMT_TIMER_RESET - cmcnt) + data;
  52. if ((cmt0_timer + cmcnt) > CMT_MAX_COUNTER)
  53. cmt0_timer = ((cmt0_timer + cmcnt) - CMT_MAX_COUNTER);
  54. else
  55. cmt0_timer += cmcnt;
  56. cmcnt = data;
  57. return cmt0_timer;
  58. }
  59. /* return msec */
  60. ulong get_timer(ulong base)
  61. {
  62. return (get_usec() / 1000) - base;
  63. }
  64. void __udelay(unsigned long usec)
  65. {
  66. unsigned long end = get_usec() + usec;
  67. while (get_usec() < end)
  68. continue;
  69. }
  70. unsigned long get_tbclk(void)
  71. {
  72. return CONFIG_SH_CMT_CLK_FREQ;
  73. }