mcfrtc.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * (C) Copyright 2007
  3. * Freescale Semiconductor Tsi-Chung.Liew@freescale.com
  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. #include <common.h>
  24. #include <command.h>
  25. #include <rtc.h>
  26. #include <asm/mcfrtc.h>
  27. #if defined(CONFIG_MCFRTC) && (CONFIG_COMMANDS & CFG_CMD_DATE)
  28. #undef RTC_DEBUG
  29. #ifndef CFG_MCFRTC_BASE
  30. #error RTC_BASE is not defined!
  31. #endif
  32. #define isleap(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0)
  33. #define STARTOFTIME 1970
  34. void rtc_get(struct rtc_time *tmp)
  35. {
  36. volatile rtc_t *rtc = (rtc_t *) (CFG_MCFRTC_BASE);
  37. int rtc_days, rtc_hrs, rtc_mins;
  38. int tim;
  39. rtc_days = rtc->days;
  40. rtc_hrs = rtc->hourmin >> 8;
  41. rtc_mins = RTC_HOURMIN_MINUTES(rtc->hourmin);
  42. tim = (rtc_days * 24) + rtc_hrs;
  43. tim = (tim * 60) + rtc_mins;
  44. tim = (tim * 60) + rtc->seconds;
  45. to_tm(tim, tmp);
  46. tmp->tm_yday = 0;
  47. tmp->tm_isdst = 0;
  48. #ifdef RTC_DEBUG
  49. printf("Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  50. tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
  51. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  52. #endif
  53. }
  54. void rtc_set(struct rtc_time *tmp)
  55. {
  56. volatile rtc_t *rtc = (rtc_t *) (CFG_MCFRTC_BASE);
  57. static int month_days[12] = {
  58. 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
  59. };
  60. int days, i, months;
  61. if (tmp->tm_year > 2037) {
  62. printf("Unable to handle. Exceeding integer limitation!\n");
  63. tmp->tm_year = 2027;
  64. }
  65. #ifdef RTC_DEBUG
  66. printf("Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  67. tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
  68. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  69. #endif
  70. /* calculate days by years */
  71. for (i = STARTOFTIME, days = 0; i < tmp->tm_year; i++) {
  72. days += 365 + isleap(i);
  73. }
  74. /* calculate days by months */
  75. months = tmp->tm_mon - 1;
  76. for (i = 0; i < months; i++) {
  77. days += month_days[i];
  78. if (i == 1)
  79. days += isleap(i);
  80. }
  81. days += tmp->tm_mday - 1;
  82. rtc->days = days;
  83. rtc->hourmin = (tmp->tm_hour << 8) | tmp->tm_min;
  84. rtc->seconds = tmp->tm_sec;
  85. }
  86. void rtc_reset(void)
  87. {
  88. volatile rtc_t *rtc = (rtc_t *) (CFG_MCFRTC_BASE);
  89. if ((rtc->cr & RTC_CR_EN) == 0) {
  90. printf("real-time-clock was stopped. Now starting...\n");
  91. rtc->cr |= RTC_CR_EN;
  92. }
  93. rtc->cr |= RTC_CR_SWR;
  94. }
  95. #endif /* CONFIG_MCFRTC && CFG_CMD_DATE */