mk48t59.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  4. * Andreas Heppel <aheppel@sysgo.de>
  5. */
  6. /*
  7. * Date & Time support for the MK48T59 RTC
  8. */
  9. #undef RTC_DEBUG
  10. #include <common.h>
  11. #include <command.h>
  12. #include <config.h>
  13. #include <rtc.h>
  14. #include <mk48t59.h>
  15. #if defined(CONFIG_BAB7xx)
  16. static uchar rtc_read (short reg)
  17. {
  18. out8(RTC_PORT_ADDR0, reg & 0xFF);
  19. out8(RTC_PORT_ADDR1, (reg>>8) & 0xFF);
  20. return in8(RTC_PORT_DATA);
  21. }
  22. static void rtc_write (short reg, uchar val)
  23. {
  24. out8(RTC_PORT_ADDR0, reg & 0xFF);
  25. out8(RTC_PORT_ADDR1, (reg>>8) & 0xFF);
  26. out8(RTC_PORT_DATA, val);
  27. }
  28. #elif defined(CONFIG_EVAL5200)
  29. static uchar rtc_read (short reg)
  30. {
  31. return in8(RTC(reg));
  32. }
  33. static void rtc_write (short reg, uchar val)
  34. {
  35. out8(RTC(reg),val);
  36. }
  37. #else
  38. # error Board specific rtc access functions should be supplied
  39. #endif
  40. /* ------------------------------------------------------------------------- */
  41. void *nvram_read(void *dest, const short src, size_t count)
  42. {
  43. uchar *d = (uchar *) dest;
  44. short s = src;
  45. while (count--)
  46. *d++ = rtc_read(s++);
  47. return dest;
  48. }
  49. void nvram_write(short dest, const void *src, size_t count)
  50. {
  51. short d = dest;
  52. uchar *s = (uchar *) src;
  53. while (count--)
  54. rtc_write(d++, *s++);
  55. }
  56. /* ------------------------------------------------------------------------- */
  57. int rtc_get (struct rtc_time *tmp)
  58. {
  59. uchar save_ctrl_a;
  60. uchar sec, min, hour, mday, wday, mon, year;
  61. /* Simple: freeze the clock, read it and allow updates again */
  62. save_ctrl_a = rtc_read(RTC_CONTROLA);
  63. /* Set the register to read the value. */
  64. save_ctrl_a |= RTC_CA_READ;
  65. rtc_write(RTC_CONTROLA, save_ctrl_a);
  66. sec = rtc_read (RTC_SECONDS);
  67. min = rtc_read (RTC_MINUTES);
  68. hour = rtc_read (RTC_HOURS);
  69. mday = rtc_read (RTC_DAY_OF_MONTH);
  70. wday = rtc_read (RTC_DAY_OF_WEEK);
  71. mon = rtc_read (RTC_MONTH);
  72. year = rtc_read (RTC_YEAR);
  73. /* re-enable update */
  74. save_ctrl_a &= ~RTC_CA_READ;
  75. rtc_write(RTC_CONTROLA, save_ctrl_a);
  76. #ifdef RTC_DEBUG
  77. printf ( "Get RTC year: %02x mon/cent: %02x mday: %02x wday: %02x "
  78. "hr: %02x min: %02x sec: %02x\n",
  79. year, mon, mday, wday,
  80. hour, min, sec );
  81. #endif
  82. tmp->tm_sec = bcd2bin (sec & 0x7F);
  83. tmp->tm_min = bcd2bin (min & 0x7F);
  84. tmp->tm_hour = bcd2bin (hour & 0x3F);
  85. tmp->tm_mday = bcd2bin (mday & 0x3F);
  86. tmp->tm_mon = bcd2bin (mon & 0x1F);
  87. tmp->tm_year = bcd2bin (year);
  88. tmp->tm_wday = bcd2bin (wday & 0x07);
  89. if(tmp->tm_year<70)
  90. tmp->tm_year+=2000;
  91. else
  92. tmp->tm_year+=1900;
  93. tmp->tm_yday = 0;
  94. tmp->tm_isdst= 0;
  95. #ifdef RTC_DEBUG
  96. printf ( "Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  97. tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
  98. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  99. #endif
  100. return 0;
  101. }
  102. int rtc_set (struct rtc_time *tmp)
  103. {
  104. uchar save_ctrl_a;
  105. #ifdef RTC_DEBUG
  106. printf ( "Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  107. tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
  108. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  109. #endif
  110. save_ctrl_a = rtc_read(RTC_CONTROLA);
  111. save_ctrl_a |= RTC_CA_WRITE;
  112. rtc_write(RTC_CONTROLA, save_ctrl_a); /* disables the RTC to update the regs */
  113. rtc_write (RTC_YEAR, bin2bcd(tmp->tm_year % 100));
  114. rtc_write (RTC_MONTH, bin2bcd(tmp->tm_mon));
  115. rtc_write (RTC_DAY_OF_WEEK, bin2bcd(tmp->tm_wday));
  116. rtc_write (RTC_DAY_OF_MONTH, bin2bcd(tmp->tm_mday));
  117. rtc_write (RTC_HOURS, bin2bcd(tmp->tm_hour));
  118. rtc_write (RTC_MINUTES, bin2bcd(tmp->tm_min ));
  119. rtc_write (RTC_SECONDS, bin2bcd(tmp->tm_sec ));
  120. save_ctrl_a &= ~RTC_CA_WRITE;
  121. rtc_write(RTC_CONTROLA, save_ctrl_a); /* enables the RTC to update the regs */
  122. return 0;
  123. }
  124. void rtc_reset (void)
  125. {
  126. uchar control_b;
  127. /*
  128. * Start oscillator here.
  129. */
  130. control_b = rtc_read(RTC_CONTROLB);
  131. control_b &= ~RTC_CB_STOP;
  132. rtc_write(RTC_CONTROLB, control_b);
  133. }
  134. void rtc_set_watchdog(short multi, short res)
  135. {
  136. uchar wd_value;
  137. wd_value = RTC_WDS | ((multi & 0x1F) << 2) | (res & 0x3);
  138. rtc_write(RTC_WATCHDOG, wd_value);
  139. }