timer.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. * (C) Copyright 2004-2005, Greg Ungerer <greg.ungerer@opengear.com>
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. */
  6. #include <common.h>
  7. #include <asm/arch/platform.h>
  8. /*
  9. * Initial timer set constants. Nothing complicated, just set for a 1ms
  10. * tick.
  11. */
  12. #define TIMER_INTERVAL (TICKS_PER_uSEC * mSEC_1)
  13. #define TIMER_COUNT (TIMER_INTERVAL / 2)
  14. #define TIMER_PULSE TIMER_COUNT
  15. /*
  16. * Handy KS8695 register access functions.
  17. */
  18. #define ks8695_read(a) *((volatile ulong *) (KS8695_IO_BASE + (a)))
  19. #define ks8695_write(a,v) *((volatile ulong *) (KS8695_IO_BASE + (a))) = (v)
  20. ulong timer_ticks;
  21. int timer_init (void)
  22. {
  23. /* Set the hadware timer for 1ms */
  24. ks8695_write(KS8695_TIMER1, TIMER_COUNT);
  25. ks8695_write(KS8695_TIMER1_PCOUNT, TIMER_PULSE);
  26. ks8695_write(KS8695_TIMER_CTRL, 0x2);
  27. timer_ticks = 0;
  28. return 0;
  29. }
  30. ulong get_timer_masked(void)
  31. {
  32. /* Check for timer wrap */
  33. if (ks8695_read(KS8695_INT_STATUS) & KS8695_INTMASK_TIMERINT1) {
  34. /* Clear interrupt condition */
  35. ks8695_write(KS8695_INT_STATUS, KS8695_INTMASK_TIMERINT1);
  36. timer_ticks++;
  37. }
  38. return timer_ticks;
  39. }
  40. ulong get_timer(ulong base)
  41. {
  42. return (get_timer_masked() - base);
  43. }
  44. void __udelay(ulong usec)
  45. {
  46. ulong start = get_timer_masked();
  47. ulong end;
  48. /* Only 1ms resolution :-( */
  49. end = usec / 1000;
  50. while (get_timer(start) < end)
  51. ;
  52. }
  53. void reset_cpu (ulong ignored)
  54. {
  55. ulong tc;
  56. /* Set timer0 to watchdog, and let it timeout */
  57. tc = ks8695_read(KS8695_TIMER_CTRL) & 0x2;
  58. ks8695_write(KS8695_TIMER_CTRL, tc);
  59. ks8695_write(KS8695_TIMER0, ((10 << 8) | 0xff));
  60. ks8695_write(KS8695_TIMER_CTRL, (tc | 0x1));
  61. /* Should only wait here till watchdog resets */
  62. for (;;)
  63. ;
  64. }