mcfrtc.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. #if defined(CONFIG_MCFRTC) && defined(CONFIG_CMD_DATE)
  25. #include <command.h>
  26. #include <rtc.h>
  27. #include <asm/immap.h>
  28. #include <asm/rtc.h>
  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. int 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. return 0;
  55. }
  56. void rtc_set(struct rtc_time *tmp)
  57. {
  58. volatile rtc_t *rtc = (rtc_t *) (CFG_MCFRTC_BASE);
  59. static int month_days[12] = {
  60. 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
  61. };
  62. int days, i, months;
  63. if (tmp->tm_year > 2037) {
  64. printf("Unable to handle. Exceeding integer limitation!\n");
  65. tmp->tm_year = 2027;
  66. }
  67. #ifdef RTC_DEBUG
  68. printf("Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  69. tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
  70. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  71. #endif
  72. /* calculate days by years */
  73. for (i = STARTOFTIME, days = 0; i < tmp->tm_year; i++) {
  74. days += 365 + isleap(i);
  75. }
  76. /* calculate days by months */
  77. months = tmp->tm_mon - 1;
  78. for (i = 0; i < months; i++) {
  79. days += month_days[i];
  80. if (i == 1)
  81. days += isleap(i);
  82. }
  83. days += tmp->tm_mday - 1;
  84. rtc->days = days;
  85. rtc->hourmin = (tmp->tm_hour << 8) | tmp->tm_min;
  86. rtc->seconds = tmp->tm_sec;
  87. }
  88. void rtc_reset(void)
  89. {
  90. volatile rtc_t *rtc = (rtc_t *) (CFG_MCFRTC_BASE);
  91. if ((rtc->cr & RTC_CR_EN) == 0) {
  92. printf("real-time-clock was stopped. Now starting...\n");
  93. rtc->cr |= RTC_CR_EN;
  94. }
  95. rtc->cr |= RTC_CR_SWR;
  96. }
  97. #endif /* CONFIG_MCFRTC && CONFIG_CMD_DATE */