at91sam9_rtt.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * (C) Copyright 2010
  3. * Reinhard Meyer, reinhard.meyer@emk-elektronik.de
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. /*
  8. * Date & Time support for the internal Real-time Timer
  9. * of AT91SAM9260 and compatibles.
  10. * Compatible with the LinuX rtc driver workaround:
  11. * The RTT cannot be written to, but only reset.
  12. * The actual time is the sum of RTT and one of
  13. * the four GPBR registers.
  14. *
  15. * The at91sam9260 has 4 GPBR (0-3).
  16. * For their typical use see at91_gpbr.h !
  17. *
  18. * make sure u-boot and kernel use the same GPBR !
  19. */
  20. #include <common.h>
  21. #include <command.h>
  22. #include <rtc.h>
  23. #include <asm/io.h>
  24. #include <asm/errno.h>
  25. #include <asm/arch/hardware.h>
  26. #include <asm/arch/at91_rtt.h>
  27. #include <asm/arch/at91_gpbr.h>
  28. #if defined(CONFIG_CMD_DATE)
  29. int rtc_get (struct rtc_time *tmp)
  30. {
  31. at91_rtt_t *rtt = (at91_rtt_t *) ATMEL_BASE_RTT;
  32. at91_gpbr_t *gpbr = (at91_gpbr_t *) ATMEL_BASE_GPBR;
  33. ulong tim;
  34. ulong tim2;
  35. ulong off;
  36. do {
  37. tim = readl(&rtt->vr);
  38. tim2 = readl(&rtt->vr);
  39. } while (tim!=tim2);
  40. off = readl(&gpbr->reg[AT91_GPBR_INDEX_TIMEOFF]);
  41. /* off==0 means time is invalid, but we ignore that */
  42. rtc_to_tm(tim+off, tmp);
  43. return 0;
  44. }
  45. int rtc_set (struct rtc_time *tmp)
  46. {
  47. at91_rtt_t *rtt = (at91_rtt_t *) ATMEL_BASE_RTT;
  48. at91_gpbr_t *gpbr = (at91_gpbr_t *) ATMEL_BASE_GPBR;
  49. ulong tim;
  50. tim = mktime (tmp->tm_year, tmp->tm_mon, tmp->tm_mday,
  51. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  52. /* clear alarm, set prescaler to 32768, clear counter */
  53. writel(32768+AT91_RTT_RTTRST, &rtt->mr);
  54. writel(~0, &rtt->ar);
  55. writel(tim, &gpbr->reg[AT91_GPBR_INDEX_TIMEOFF]);
  56. /* wait for counter clear to happen, takes less than a 1/32768th second */
  57. while (readl(&rtt->vr) != 0)
  58. ;
  59. return 0;
  60. }
  61. void rtc_reset (void)
  62. {
  63. at91_rtt_t *rtt = (at91_rtt_t *) ATMEL_BASE_RTT;
  64. at91_gpbr_t *gpbr = (at91_gpbr_t *) ATMEL_BASE_GPBR;
  65. /* clear alarm, set prescaler to 32768, clear counter */
  66. writel(32768+AT91_RTT_RTTRST, &rtt->mr);
  67. writel(~0, &rtt->ar);
  68. writel(0, &gpbr->reg[AT91_GPBR_INDEX_TIMEOFF]);
  69. /* wait for counter clear to happen, takes less than a 1/32768th second */
  70. while (readl(&rtt->vr) != 0)
  71. ;
  72. }
  73. #endif