at91sam9_rtt.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 <linux/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 = rtc_mktime(tmp);
  51. /* clear alarm, set prescaler to 32768, clear counter */
  52. writel(32768+AT91_RTT_RTTRST, &rtt->mr);
  53. writel(~0, &rtt->ar);
  54. writel(tim, &gpbr->reg[AT91_GPBR_INDEX_TIMEOFF]);
  55. /* wait for counter clear to happen, takes less than a 1/32768th second */
  56. while (readl(&rtt->vr) != 0)
  57. ;
  58. return 0;
  59. }
  60. void rtc_reset (void)
  61. {
  62. at91_rtt_t *rtt = (at91_rtt_t *) ATMEL_BASE_RTT;
  63. at91_gpbr_t *gpbr = (at91_gpbr_t *) ATMEL_BASE_GPBR;
  64. /* clear alarm, set prescaler to 32768, clear counter */
  65. writel(32768+AT91_RTT_RTTRST, &rtt->mr);
  66. writel(~0, &rtt->ar);
  67. writel(0, &gpbr->reg[AT91_GPBR_INDEX_TIMEOFF]);
  68. /* wait for counter clear to happen, takes less than a 1/32768th second */
  69. while (readl(&rtt->vr) != 0)
  70. ;
  71. }
  72. #endif