mcfrtc.c 2.2 KB

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