time.c 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2009
  4. * Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
  5. *
  6. * (C) Copyright 2007-2012
  7. * Nobobuhiro Iwamatsu <iwamatsu@nigauri.org>
  8. *
  9. * (C) Copyright 2003
  10. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  11. */
  12. #include <common.h>
  13. #include <asm/processor.h>
  14. #include <asm/io.h>
  15. #include <sh_tmu.h>
  16. #define TCR_TPSC 0x07
  17. static struct tmu_regs *tmu = (struct tmu_regs *)TMU_BASE;
  18. unsigned long get_tbclk(void)
  19. {
  20. u16 tmu_bit = (ffs(CONFIG_SYS_TMU_CLK_DIV) >> 1) - 1;
  21. return get_tmu0_clk_rate() >> ((tmu_bit + 1) * 2);
  22. }
  23. unsigned long timer_read_counter(void)
  24. {
  25. return ~readl(&tmu->tcnt0);
  26. }
  27. static void tmu_timer_start(unsigned int timer)
  28. {
  29. if (timer > 2)
  30. return;
  31. writeb(readb(&tmu->tstr) | (1 << timer), &tmu->tstr);
  32. }
  33. static void tmu_timer_stop(unsigned int timer)
  34. {
  35. if (timer > 2)
  36. return;
  37. writeb(readb(&tmu->tstr) & ~(1 << timer), &tmu->tstr);
  38. }
  39. int timer_init(void)
  40. {
  41. u16 tmu_bit = (ffs(CONFIG_SYS_TMU_CLK_DIV) >> 1) - 1;
  42. writew((readw(&tmu->tcr0) & ~TCR_TPSC) | tmu_bit, &tmu->tcr0);
  43. tmu_timer_stop(0);
  44. tmu_timer_start(0);
  45. return 0;
  46. }