timer.c 1.8 KB

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