timer.c 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * Copyright (C) 2011 Vladimir Zapolskiy <vz@mleia.com>
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version 2
  7. * of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  17. * MA 02110-1301, USA.
  18. */
  19. #include <common.h>
  20. #include <asm/arch/cpu.h>
  21. #include <asm/arch/clk.h>
  22. #include <asm/arch/timer.h>
  23. #include <asm/io.h>
  24. static struct timer_regs *timer0 = (struct timer_regs *)TIMER0_BASE;
  25. static struct timer_regs *timer1 = (struct timer_regs *)TIMER1_BASE;
  26. static struct clk_pm_regs *clk = (struct clk_pm_regs *)CLK_PM_BASE;
  27. static void lpc32xx_timer_clock(u32 bit, int enable)
  28. {
  29. if (enable)
  30. setbits_le32(&clk->timclk_ctrl1, bit);
  31. else
  32. clrbits_le32(&clk->timclk_ctrl1, bit);
  33. }
  34. static void lpc32xx_timer_reset(struct timer_regs *timer, u32 freq)
  35. {
  36. writel(TIMER_TCR_COUNTER_RESET, &timer->tcr);
  37. writel(TIMER_TCR_COUNTER_DISABLE, &timer->tcr);
  38. writel(0, &timer->tc);
  39. writel(0, &timer->pr);
  40. /* Count mode is every rising PCLK edge */
  41. writel(TIMER_CTCR_MODE_TIMER, &timer->ctcr);
  42. /* Set prescale counter value */
  43. writel((get_periph_clk_rate() / freq) - 1, &timer->pr);
  44. }
  45. static void lpc32xx_timer_count(struct timer_regs *timer, int enable)
  46. {
  47. if (enable)
  48. writel(TIMER_TCR_COUNTER_ENABLE, &timer->tcr);
  49. else
  50. writel(TIMER_TCR_COUNTER_DISABLE, &timer->tcr);
  51. }
  52. int timer_init(void)
  53. {
  54. lpc32xx_timer_clock(CLK_TIMCLK_TIMER0, 1);
  55. lpc32xx_timer_reset(timer0, CONFIG_SYS_HZ);
  56. lpc32xx_timer_count(timer0, 1);
  57. return 0;
  58. }
  59. ulong get_timer(ulong base)
  60. {
  61. return readl(&timer0->tc) - base;
  62. }
  63. void __udelay(unsigned long usec)
  64. {
  65. lpc32xx_timer_clock(CLK_TIMCLK_TIMER1, 1);
  66. lpc32xx_timer_reset(timer1, CONFIG_SYS_HZ * 1000);
  67. lpc32xx_timer_count(timer1, 1);
  68. while (readl(&timer1->tc) < usec)
  69. /* NOP */;
  70. lpc32xx_timer_count(timer1, 0);
  71. lpc32xx_timer_clock(CLK_TIMCLK_TIMER1, 0);
  72. }
  73. unsigned long long get_ticks(void)
  74. {
  75. return get_timer(0);
  76. }
  77. ulong get_tbclk(void)
  78. {
  79. return CONFIG_SYS_HZ;
  80. }