bf5xx_rtc.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * (C) Copyright 2001
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.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. * Real Time Clock interface of ADI21535 (Blackfin) for uCLinux
  23. *
  24. * Copyright (C) 2003 Motorola Corporation. All rights reserved.
  25. * Richard Xiao (A2590C@email.mot.com)
  26. *
  27. * Copyright (C) 1996 Paul Gortmaker
  28. *
  29. *
  30. * Based on other minimal char device drivers, like Alan's
  31. * watchdog, Ted's random, etc. etc.
  32. *
  33. * 1.07 Paul Gortmaker.
  34. * 1.08 Miquel van Smoorenburg: disallow certain things on the
  35. * DEC Alpha as the CMOS clock is also used for other things.
  36. * 1.09 Nikita Schmidt: epoch support and some Alpha cleanup.
  37. * 1.09a Pete Zaitcev: Sun SPARC
  38. * 1.09b Jeff Garzik: Modularize, init cleanup
  39. * 1.09c Jeff Garzik: SMP cleanup
  40. * 1.10 Paul Barton-Davis: add support for async I/O
  41. * 1.10a Andrea Arcangeli: Alpha updates
  42. * 1.10b Andrew Morton: SMP lock fix
  43. * 1.10c Cesar Barros: SMP locking fixes and cleanup
  44. * 1.10d Paul Gortmaker: delete paranoia check in rtc_exit
  45. * 1.10e LG Soft India: Register access is different in BF533.
  46. */
  47. #include <common.h>
  48. #include <command.h>
  49. #include <rtc.h>
  50. #if defined(CONFIG_RTC_BFIN) && defined(CONFIG_CMD_DATE)
  51. #include <asm/blackfin.h>
  52. #include <asm/mach-common/bits/rtc.h>
  53. #define MIN_TO_SECS(_x_) (60 * _x_)
  54. #define HRS_TO_SECS(_x_) (60 * 60 * _x_)
  55. #define DAYS_TO_SECS(_x_) (24 * 60 * 60 * _x_)
  56. #define NUM_SECS_IN_DAY (24 * 3600)
  57. #define NUM_SECS_IN_HOUR (3600)
  58. #define NUM_SECS_IN_MIN (60)
  59. /* Shift values for RTC_STAT register */
  60. #define DAY_BITS_OFF 17
  61. #define HOUR_BITS_OFF 12
  62. #define MIN_BITS_OFF 6
  63. #define SEC_BITS_OFF 0
  64. void rtc_reset(void)
  65. {
  66. return; /* nothing to do */
  67. }
  68. /* Wait for pending writes to complete */
  69. void wait_for_complete(void)
  70. {
  71. while (!(*(volatile unsigned short *)RTC_ISTAT & 0x8000)) {
  72. printf("");
  73. }
  74. *(volatile unsigned short *)RTC_ISTAT = 0x8000;
  75. }
  76. /* Enable the RTC prescaler enable register */
  77. void rtc_init()
  78. {
  79. *(volatile unsigned short *)RTC_PREN = 0x1;
  80. wait_for_complete();
  81. }
  82. /* Set the time. Get the time_in_secs which is the number of seconds since Jan 1970 and set the RTC registers
  83. * based on this value.
  84. */
  85. void rtc_set(struct rtc_time *tmp)
  86. {
  87. unsigned long n_days_1970 = 0;
  88. unsigned long n_secs_rem = 0;
  89. unsigned long n_hrs = 0;
  90. unsigned long n_mins = 0;
  91. unsigned long n_secs = 0;
  92. unsigned long time_in_secs;
  93. if (tmp == NULL) {
  94. printf("Error setting the date/time \n");
  95. return;
  96. }
  97. time_in_secs =
  98. mktime(tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_hour,
  99. tmp->tm_min, tmp->tm_sec);
  100. /* Compute no. of days since 1970 */
  101. n_days_1970 = (unsigned long)(time_in_secs / (NUM_SECS_IN_DAY));
  102. /* From the remining secs, compute the hrs(0-23), mins(0-59) and secs(0-59) */
  103. n_secs_rem = (unsigned long)(time_in_secs % (NUM_SECS_IN_DAY));
  104. n_hrs = n_secs_rem / (NUM_SECS_IN_HOUR);
  105. n_secs_rem = n_secs_rem % (NUM_SECS_IN_HOUR);
  106. n_mins = n_secs_rem / (NUM_SECS_IN_MIN);
  107. n_secs = n_secs_rem % (NUM_SECS_IN_MIN);
  108. /* Store the new time in the RTC_STAT register */
  109. *(volatile unsigned long *)RTC_STAT =
  110. ((n_days_1970 << DAY_BITS_OFF) | (n_hrs << HOUR_BITS_OFF) |
  111. (n_mins << MIN_BITS_OFF) | (n_secs << SEC_BITS_OFF));
  112. wait_for_complete();
  113. }
  114. /* Read the time from the RTC_STAT. time_in_seconds is seconds since Jan 1970 */
  115. void rtc_get(struct rtc_time *tmp)
  116. {
  117. unsigned long cur_rtc_stat = 0;
  118. unsigned long time_in_sec;
  119. unsigned long tm_sec = 0, tm_min = 0, tm_hour = 0, tm_day = 0;
  120. if (tmp == NULL) {
  121. printf("Error getting the date/time \n");
  122. return;
  123. }
  124. /* Read the RTC_STAT register */
  125. cur_rtc_stat = *(volatile unsigned long *)RTC_STAT;
  126. /* Get the secs (0-59), mins (0-59), hrs (0-23) and the days since Jan 1970 */
  127. tm_sec = (cur_rtc_stat >> SEC_BITS_OFF) & 0x3f;
  128. tm_min = (cur_rtc_stat >> MIN_BITS_OFF) & 0x3f;
  129. tm_hour = (cur_rtc_stat >> HOUR_BITS_OFF) & 0x1f;
  130. tm_day = (cur_rtc_stat >> DAY_BITS_OFF) & 0x7fff;
  131. /* Calculate the total number of seconds since Jan 1970 */
  132. time_in_sec = (tm_sec) +
  133. MIN_TO_SECS(tm_min) + HRS_TO_SECS(tm_hour) + DAYS_TO_SECS(tm_day);
  134. to_tm(time_in_sec, tmp);
  135. }
  136. #endif