slicetimer.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2007, 2012 Freescale Semiconductor, Inc.
  4. * TsiChung Liew (Tsi-Chung.Liew@freescale.com)
  5. */
  6. #include <common.h>
  7. #include <init.h>
  8. #include <irq_func.h>
  9. #include <linux/delay.h>
  10. #include <asm/timer.h>
  11. #include <asm/immap.h>
  12. #include <asm/io.h>
  13. DECLARE_GLOBAL_DATA_PTR;
  14. static ulong timestamp;
  15. #if defined(CONFIG_SLTTMR)
  16. #ifndef CONFIG_SYS_UDELAY_BASE
  17. # error "uDelay base not defined!"
  18. #endif
  19. #if !defined(CONFIG_SYS_TMR_BASE) || !defined(CONFIG_SYS_INTR_BASE) || !defined(CONFIG_SYS_TMRINTR_NO) || !defined(CONFIG_SYS_TMRINTR_MASK)
  20. # error "TMR_BASE, INTR_BASE, TMRINTR_NO or TMRINTR_MASk not defined!"
  21. #endif
  22. extern void dtimer_intr_setup(void);
  23. void __udelay(unsigned long usec)
  24. {
  25. slt_t *timerp = (slt_t *) (CONFIG_SYS_UDELAY_BASE);
  26. u32 now, freq;
  27. /* 1 us period */
  28. freq = CONFIG_SYS_TIMER_PRESCALER;
  29. /* Disable */
  30. out_be32(&timerp->cr, 0);
  31. out_be32(&timerp->tcnt, usec * freq);
  32. out_be32(&timerp->cr, SLT_CR_TEN);
  33. now = in_be32(&timerp->cnt);
  34. while (now != 0)
  35. now = in_be32(&timerp->cnt);
  36. setbits_be32(&timerp->sr, SLT_SR_ST);
  37. out_be32(&timerp->cr, 0);
  38. }
  39. void dtimer_interrupt(void *not_used)
  40. {
  41. slt_t *timerp = (slt_t *) (CONFIG_SYS_TMR_BASE);
  42. /* check for timer interrupt asserted */
  43. if ((CONFIG_SYS_TMRPND_REG & CONFIG_SYS_TMRINTR_MASK) == CONFIG_SYS_TMRINTR_PEND) {
  44. setbits_be32(&timerp->sr, SLT_SR_ST);
  45. timestamp++;
  46. return;
  47. }
  48. }
  49. int timer_init(void)
  50. {
  51. slt_t *timerp = (slt_t *) (CONFIG_SYS_TMR_BASE);
  52. timestamp = 0;
  53. /* disable timer */
  54. out_be32(&timerp->cr, 0);
  55. out_be32(&timerp->tcnt, 0);
  56. /* clear status */
  57. out_be32(&timerp->sr, SLT_SR_BE | SLT_SR_ST);
  58. /* initialize and enable timer interrupt */
  59. irq_install_handler(CONFIG_SYS_TMRINTR_NO, dtimer_interrupt, 0);
  60. /* Interrupt every ms */
  61. out_be32(&timerp->tcnt, 1000 * CONFIG_SYS_TIMER_PRESCALER);
  62. dtimer_intr_setup();
  63. /* set a period of 1us, set timer mode to restart and
  64. enable timer and interrupt */
  65. out_be32(&timerp->cr, SLT_CR_RUN | SLT_CR_IEN | SLT_CR_TEN);
  66. return 0;
  67. }
  68. ulong get_timer(ulong base)
  69. {
  70. return (timestamp - base);
  71. }
  72. #endif /* CONFIG_SLTTMR */