mcfrtc.c 3.0 KB

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