timer.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2007-2008
  4. * Stelian Pop <stelian@popies.net>
  5. * Lead Tech Design <www.leadtechdesign.com>
  6. *
  7. * (C) Copyright 2013
  8. * Bo Shen <voice.shen@atmel.com>
  9. */
  10. #include <common.h>
  11. #include <init.h>
  12. #include <time.h>
  13. #include <asm/io.h>
  14. #include <asm/arch/hardware.h>
  15. #include <asm/arch/at91_pit.h>
  16. #include <asm/arch/clk.h>
  17. #include <div64.h>
  18. #if !defined(CONFIG_AT91FAMILY)
  19. # error You need to define CONFIG_AT91FAMILY in your board config!
  20. #endif
  21. DECLARE_GLOBAL_DATA_PTR;
  22. /*
  23. * We're using the SAMA5D3x PITC in 32 bit mode, by
  24. * setting the 20 bit counter period to its maximum (0xfffff).
  25. * (See the relevant data sheets to understand that this really works)
  26. *
  27. * We do also mimic the typical powerpc way of incrementing
  28. * two 32 bit registers called tbl and tbu.
  29. *
  30. * Those registers increment at 1/16 the main clock rate.
  31. */
  32. #define TIMER_LOAD_VAL 0xfffff
  33. /*
  34. * Use the PITC in full 32 bit incrementing mode
  35. */
  36. int timer_init(void)
  37. {
  38. at91_pit_t *pit = (at91_pit_t *)ATMEL_BASE_PIT;
  39. /* Enable PITC Clock */
  40. at91_periph_clk_enable(ATMEL_ID_PIT);
  41. /* Enable PITC */
  42. writel(TIMER_LOAD_VAL | AT91_PIT_MR_EN , &pit->mr);
  43. gd->arch.timer_rate_hz = get_pit_clk_rate() / 16;
  44. return 0;
  45. }
  46. /*
  47. * Return the number of timer ticks per second.
  48. */
  49. ulong get_tbclk(void)
  50. {
  51. return gd->arch.timer_rate_hz;
  52. }