time_sh2.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 <asm/io.h>
  11. #include <asm/processor.h>
  12. #define CMT_CMCSR_INIT 0x0001 /* PCLK/32 */
  13. #define CMT_CMCSR_CALIB 0x0000
  14. #define CMT_MAX_COUNTER (0xFFFFFFFF)
  15. #define CMT_TIMER_RESET (0xFFFF)
  16. static vu_long cmt0_timer;
  17. static void cmt_timer_start(unsigned int timer)
  18. {
  19. writew(readw(CMSTR) | 0x01, CMSTR);
  20. }
  21. static void cmt_timer_stop(unsigned int timer)
  22. {
  23. writew(readw(CMSTR) & ~0x01, CMSTR);
  24. }
  25. int timer_init(void)
  26. {
  27. cmt0_timer = 0;
  28. /* Divide clock by 32 */
  29. readw(CMCSR_0);
  30. writew(CMT_CMCSR_INIT, CMCSR_0);
  31. /* User Device 0 only */
  32. cmt_timer_stop(0);
  33. writew(CMT_TIMER_RESET, CMCOR_0);
  34. cmt_timer_start(0);
  35. return 0;
  36. }
  37. unsigned long long get_ticks(void)
  38. {
  39. return cmt0_timer;
  40. }
  41. static vu_long cmcnt = 0;
  42. static unsigned long get_usec (void)
  43. {
  44. ulong data = readw(CMCNT_0);
  45. if (data >= cmcnt)
  46. cmcnt = data - cmcnt;
  47. else
  48. cmcnt = (CMT_TIMER_RESET - cmcnt) + data;
  49. if ((cmt0_timer + cmcnt) > CMT_MAX_COUNTER)
  50. cmt0_timer = ((cmt0_timer + cmcnt) - CMT_MAX_COUNTER);
  51. else
  52. cmt0_timer += cmcnt;
  53. cmcnt = data;
  54. return cmt0_timer;
  55. }
  56. /* return msec */
  57. ulong get_timer(ulong base)
  58. {
  59. return (get_usec() / 1000) - base;
  60. }
  61. void __udelay(unsigned long usec)
  62. {
  63. unsigned long end = get_usec() + usec;
  64. while (get_usec() < end)
  65. continue;
  66. }
  67. unsigned long get_tbclk(void)
  68. {
  69. return CONFIG_SH_CMT_CLK_FREQ;
  70. }