at91sam9_rtt.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * (C) Copyright 2010
  3. * Reinhard Meyer, reinhard.meyer@emk-elektronik.de
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. /*
  24. * Date & Time support for the internal Real-time Timer
  25. * of AT91SAM9260 and compatibles.
  26. * Compatible with the LinuX rtc driver workaround:
  27. * The RTT cannot be written to, but only reset.
  28. * The actual time is the sum of RTT and one of
  29. * the four GPBR registers.
  30. *
  31. * The at91sam9260 has 4 GPBR (0-3).
  32. * For their typical use see at91_gpbr.h !
  33. *
  34. * make sure u-boot and kernel use the same GPBR !
  35. */
  36. #include <common.h>
  37. #include <command.h>
  38. #include <rtc.h>
  39. #include <asm/errno.h>
  40. #include <asm/arch/hardware.h>
  41. #include <asm/arch/io.h>
  42. #include <asm/arch/at91_rtt.h>
  43. #include <asm/arch/at91_gpbr.h>
  44. #if defined(CONFIG_CMD_DATE)
  45. int rtc_get (struct rtc_time *tmp)
  46. {
  47. at91_rtt_t *rtt = (at91_rtt_t *) AT91_RTT_BASE;
  48. at91_gpbr_t *gpbr = (at91_gpbr_t *) AT91_GPR_BASE;
  49. ulong tim;
  50. ulong tim2;
  51. ulong off;
  52. do {
  53. tim = readl(&rtt->vr);
  54. tim2 = readl(&rtt->vr);
  55. } while (tim!=tim2);
  56. off = readl(&gpbr->reg[AT91_GPBR_INDEX_TIMEOFF]);
  57. /* off==0 means time is invalid, but we ignore that */
  58. to_tm (tim+off, tmp);
  59. return 0;
  60. }
  61. int rtc_set (struct rtc_time *tmp)
  62. {
  63. at91_rtt_t *rtt = (at91_rtt_t *) AT91_RTT_BASE;
  64. at91_gpbr_t *gpbr = (at91_gpbr_t *) AT91_GPR_BASE;
  65. ulong tim;
  66. tim = mktime (tmp->tm_year, tmp->tm_mon, tmp->tm_mday,
  67. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  68. /* clear alarm, set prescaler to 32768, clear counter */
  69. writel(32768+AT91_RTT_RTTRST, &rtt->mr);
  70. writel(~0, &rtt->ar);
  71. writel(tim, &gpbr->reg[AT91_GPBR_INDEX_TIMEOFF]);
  72. /* wait for counter clear to happen, takes less than a 1/32768th second */
  73. while (readl(&rtt->vr) != 0)
  74. ;
  75. return 0;
  76. }
  77. void rtc_reset (void)
  78. {
  79. at91_rtt_t *rtt = (at91_rtt_t *) AT91_RTT_BASE;
  80. at91_gpbr_t *gpbr = (at91_gpbr_t *) AT91_GPR_BASE;
  81. /* clear alarm, set prescaler to 32768, clear counter */
  82. writel(32768+AT91_RTT_RTTRST, &rtt->mr);
  83. writel(~0, &rtt->ar);
  84. writel(0, &gpbr->reg[AT91_GPBR_INDEX_TIMEOFF]);
  85. /* wait for counter clear to happen, takes less than a 1/32768th second */
  86. while (readl(&rtt->vr) != 0)
  87. ;
  88. }
  89. #endif