mcfrtc.c 2.1 KB

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