timer.c 1.9 KB

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