mcfrtc.c 2.3 KB

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