mcfrtc.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * Copyright (C) 2004-2007 Freescale Semiconductor, Inc.
  3. * TsiChung Liew (Tsi-Chung.Liew@freescale.com)
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <common.h>
  8. #if defined(CONFIG_CMD_DATE)
  9. #include <command.h>
  10. #include <rtc.h>
  11. #include <asm/immap.h>
  12. #include <asm/rtc.h>
  13. #undef RTC_DEBUG
  14. #ifndef CONFIG_SYS_MCFRTC_BASE
  15. #error RTC_BASE is not defined!
  16. #endif
  17. #define isleap(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0)
  18. #define STARTOFTIME 1970
  19. int rtc_get(struct rtc_time *tmp)
  20. {
  21. volatile rtc_t *rtc = (rtc_t *) (CONFIG_SYS_MCFRTC_BASE);
  22. int rtc_days, rtc_hrs, rtc_mins;
  23. int tim;
  24. rtc_days = rtc->days;
  25. rtc_hrs = rtc->hourmin >> 8;
  26. rtc_mins = RTC_HOURMIN_MINUTES(rtc->hourmin);
  27. tim = (rtc_days * 24) + rtc_hrs;
  28. tim = (tim * 60) + rtc_mins;
  29. tim = (tim * 60) + rtc->seconds;
  30. rtc_to_tm(tim, tmp);
  31. tmp->tm_yday = 0;
  32. tmp->tm_isdst = 0;
  33. #ifdef RTC_DEBUG
  34. printf("Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  35. tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
  36. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  37. #endif
  38. return 0;
  39. }
  40. int rtc_set(struct rtc_time *tmp)
  41. {
  42. volatile rtc_t *rtc = (rtc_t *) (CONFIG_SYS_MCFRTC_BASE);
  43. static int month_days[12] = {
  44. 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
  45. };
  46. int days, i, months;
  47. if (tmp->tm_year > 2037) {
  48. printf("Unable to handle. Exceeding integer limitation!\n");
  49. tmp->tm_year = 2027;
  50. }
  51. #ifdef RTC_DEBUG
  52. printf("Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  53. tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
  54. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  55. #endif
  56. /* calculate days by years */
  57. for (i = STARTOFTIME, days = 0; i < tmp->tm_year; i++) {
  58. days += 365 + isleap(i);
  59. }
  60. /* calculate days by months */
  61. months = tmp->tm_mon - 1;
  62. for (i = 0; i < months; i++) {
  63. days += month_days[i];
  64. if (i == 1)
  65. days += isleap(i);
  66. }
  67. days += tmp->tm_mday - 1;
  68. rtc->days = days;
  69. rtc->hourmin = (tmp->tm_hour << 8) | tmp->tm_min;
  70. rtc->seconds = tmp->tm_sec;
  71. return 0;
  72. }
  73. void rtc_reset(void)
  74. {
  75. volatile rtc_t *rtc = (rtc_t *) (CONFIG_SYS_MCFRTC_BASE);
  76. if ((rtc->cr & RTC_CR_EN) == 0) {
  77. printf("real-time-clock was stopped. Now starting...\n");
  78. rtc->cr |= RTC_CR_EN;
  79. }
  80. rtc->cr |= RTC_CR_SWR;
  81. }
  82. #endif /* CONFIG_MCFRTC && CONFIG_CMD_DATE */