time_sh2.c 1.6 KB

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