time_sh2.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. * See file CREDITS for list of people who contributed to this
  9. * project.
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License as
  13. * published by the Free Software Foundation; either version 2 of
  14. * the License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  24. * MA 02111-1307 USA
  25. */
  26. #include <common.h>
  27. #include <asm/io.h>
  28. #include <asm/processor.h>
  29. #define CMT_CMCSR_INIT 0x0001 /* PCLK/32 */
  30. #define CMT_CMCSR_CALIB 0x0000
  31. #define CMT_MAX_COUNTER (0xFFFFFFFF)
  32. #define CMT_TIMER_RESET (0xFFFF)
  33. static vu_long cmt0_timer;
  34. static void cmt_timer_start(unsigned int timer)
  35. {
  36. writew(readw(CMSTR) | 0x01, CMSTR);
  37. }
  38. static void cmt_timer_stop(unsigned int timer)
  39. {
  40. writew(readw(CMSTR) & ~0x01, CMSTR);
  41. }
  42. int timer_init(void)
  43. {
  44. cmt0_timer = 0;
  45. /* Divide clock by 32 */
  46. readw(CMCSR_0);
  47. writew(CMT_CMCSR_INIT, CMCSR_0);
  48. /* User Device 0 only */
  49. cmt_timer_stop(0);
  50. set_timer(CMT_TIMER_RESET);
  51. cmt_timer_start(0);
  52. return 0;
  53. }
  54. unsigned long long get_ticks(void)
  55. {
  56. return cmt0_timer;
  57. }
  58. static vu_long cmcnt = 0;
  59. static unsigned long get_usec (void)
  60. {
  61. ulong data = readw(CMCNT_0);
  62. if (data >= cmcnt)
  63. cmcnt = data - cmcnt;
  64. else
  65. cmcnt = (CMT_TIMER_RESET - cmcnt) + data;
  66. if ((cmt0_timer + cmcnt) > CMT_MAX_COUNTER)
  67. cmt0_timer = ((cmt0_timer + cmcnt) - CMT_MAX_COUNTER);
  68. else
  69. cmt0_timer += cmcnt;
  70. cmcnt = data;
  71. return cmt0_timer;
  72. }
  73. /* return msec */
  74. ulong get_timer(ulong base)
  75. {
  76. return (get_usec() / 1000) - base;
  77. }
  78. void set_timer(ulong t)
  79. {
  80. writew((u16) t, CMCOR_0);
  81. }
  82. void reset_timer(void)
  83. {
  84. cmt_timer_stop(0);
  85. set_timer(CMT_TIMER_RESET);
  86. cmt0_timer = 0;
  87. cmt_timer_start(0);
  88. }
  89. void udelay(unsigned long usec)
  90. {
  91. unsigned long end = get_usec() + usec;
  92. while (get_usec() < end)
  93. continue;
  94. }
  95. unsigned long get_tbclk(void)
  96. {
  97. return CONFIG_SYS_HZ;
  98. }