timer.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2011 Vladimir Zapolskiy <vz@mleia.com>
  4. */
  5. #include <common.h>
  6. #include <asm/arch/cpu.h>
  7. #include <asm/arch/clk.h>
  8. #include <asm/arch/timer.h>
  9. #include <asm/io.h>
  10. static struct timer_regs *timer0 = (struct timer_regs *)TIMER0_BASE;
  11. static struct timer_regs *timer1 = (struct timer_regs *)TIMER1_BASE;
  12. static struct clk_pm_regs *clk = (struct clk_pm_regs *)CLK_PM_BASE;
  13. static void lpc32xx_timer_clock(u32 bit, int enable)
  14. {
  15. if (enable)
  16. setbits_le32(&clk->timclk_ctrl1, bit);
  17. else
  18. clrbits_le32(&clk->timclk_ctrl1, bit);
  19. }
  20. static void lpc32xx_timer_reset(struct timer_regs *timer, u32 freq)
  21. {
  22. writel(TIMER_TCR_COUNTER_RESET, &timer->tcr);
  23. writel(TIMER_TCR_COUNTER_DISABLE, &timer->tcr);
  24. writel(0, &timer->tc);
  25. writel(0, &timer->pr);
  26. /* Count mode is every rising PCLK edge */
  27. writel(TIMER_CTCR_MODE_TIMER, &timer->ctcr);
  28. /* Set prescale counter value */
  29. writel((get_periph_clk_rate() / freq) - 1, &timer->pr);
  30. }
  31. static void lpc32xx_timer_count(struct timer_regs *timer, int enable)
  32. {
  33. if (enable)
  34. writel(TIMER_TCR_COUNTER_ENABLE, &timer->tcr);
  35. else
  36. writel(TIMER_TCR_COUNTER_DISABLE, &timer->tcr);
  37. }
  38. int timer_init(void)
  39. {
  40. lpc32xx_timer_clock(CLK_TIMCLK_TIMER0, 1);
  41. lpc32xx_timer_reset(timer0, CONFIG_SYS_HZ);
  42. lpc32xx_timer_count(timer0, 1);
  43. return 0;
  44. }
  45. ulong get_timer(ulong base)
  46. {
  47. return readl(&timer0->tc) - base;
  48. }
  49. void __udelay(unsigned long usec)
  50. {
  51. lpc32xx_timer_clock(CLK_TIMCLK_TIMER1, 1);
  52. lpc32xx_timer_reset(timer1, CONFIG_SYS_HZ * 1000);
  53. lpc32xx_timer_count(timer1, 1);
  54. while (readl(&timer1->tc) < usec)
  55. /* NOP */;
  56. lpc32xx_timer_count(timer1, 0);
  57. lpc32xx_timer_clock(CLK_TIMCLK_TIMER1, 0);
  58. }
  59. unsigned long long get_ticks(void)
  60. {
  61. return get_timer(0);
  62. }
  63. ulong get_tbclk(void)
  64. {
  65. return CONFIG_SYS_HZ;
  66. }