m48t35ax.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * (C) Copyright 2001
  3. * Erik Theisen, Wave 7 Optics, etheisen@mindspring.com.
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. /*
  24. * Date & Time support for ST Electronics M48T35Ax RTC
  25. */
  26. /*#define DEBUG */
  27. #include <common.h>
  28. #include <command.h>
  29. #include <rtc.h>
  30. #include <config.h>
  31. #if defined(CONFIG_CMD_DATE)
  32. static uchar rtc_read (uchar reg);
  33. static void rtc_write (uchar reg, uchar val);
  34. static uchar bin2bcd (unsigned int n);
  35. static unsigned bcd2bin(uchar c);
  36. /* ------------------------------------------------------------------------- */
  37. int rtc_get (struct rtc_time *tmp)
  38. {
  39. uchar sec, min, hour, cent_day, date, month, year;
  40. uchar ccr; /* Clock control register */
  41. /* Lock RTC for read using clock control register */
  42. ccr = rtc_read(0);
  43. ccr = ccr | 0x40;
  44. rtc_write(0, ccr);
  45. sec = rtc_read (0x1);
  46. min = rtc_read (0x2);
  47. hour = rtc_read (0x3);
  48. cent_day= rtc_read (0x4);
  49. date = rtc_read (0x5);
  50. month = rtc_read (0x6);
  51. year = rtc_read (0x7);
  52. /* UNLock RTC */
  53. ccr = rtc_read(0);
  54. ccr = ccr & 0xBF;
  55. rtc_write(0, ccr);
  56. debug ( "Get RTC year: %02x month: %02x date: %02x cent_day: %02x "
  57. "hr: %02x min: %02x sec: %02x\n",
  58. year, month, date, cent_day,
  59. hour, min, sec );
  60. tmp->tm_sec = bcd2bin (sec & 0x7F);
  61. tmp->tm_min = bcd2bin (min & 0x7F);
  62. tmp->tm_hour = bcd2bin (hour & 0x3F);
  63. tmp->tm_mday = bcd2bin (date & 0x3F);
  64. tmp->tm_mon = bcd2bin (month & 0x1F);
  65. tmp->tm_year = bcd2bin (year) + ((cent_day & 0x10) ? 2000 : 1900);
  66. tmp->tm_wday = bcd2bin (cent_day & 0x07);
  67. tmp->tm_yday = 0;
  68. tmp->tm_isdst= 0;
  69. debug ( "Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  70. tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
  71. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  72. return 0;
  73. }
  74. int rtc_set (struct rtc_time *tmp)
  75. {
  76. uchar ccr; /* Clock control register */
  77. uchar century;
  78. debug ( "Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  79. tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
  80. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  81. /* Lock RTC for write using clock control register */
  82. ccr = rtc_read(0);
  83. ccr = ccr | 0x80;
  84. rtc_write(0, ccr);
  85. rtc_write (0x07, bin2bcd(tmp->tm_year % 100));
  86. rtc_write (0x06, bin2bcd(tmp->tm_mon));
  87. rtc_write (0x05, bin2bcd(tmp->tm_mday));
  88. century = ((tmp->tm_year >= 2000) ? 0x10 : 0) | 0x20;
  89. rtc_write (0x04, bin2bcd(tmp->tm_wday) | century);
  90. rtc_write (0x03, bin2bcd(tmp->tm_hour));
  91. rtc_write (0x02, bin2bcd(tmp->tm_min ));
  92. rtc_write (0x01, bin2bcd(tmp->tm_sec ));
  93. /* UNLock RTC */
  94. ccr = rtc_read(0);
  95. ccr = ccr & 0x7F;
  96. rtc_write(0, ccr);
  97. return 0;
  98. }
  99. void rtc_reset (void)
  100. {
  101. uchar val;
  102. /* Clear all clock control registers */
  103. rtc_write (0x0, 0x80); /* No Read Lock or calibration */
  104. /* Clear stop bit */
  105. val = rtc_read (0x1);
  106. val &= 0x7f;
  107. rtc_write(0x1, val);
  108. /* Enable century / disable frequency test */
  109. val = rtc_read (0x4);
  110. val = (val & 0xBF) | 0x20;
  111. rtc_write(0x4, val);
  112. /* Clear write lock */
  113. rtc_write(0x0, 0);
  114. }
  115. /* ------------------------------------------------------------------------- */
  116. static uchar rtc_read (uchar reg)
  117. {
  118. uchar val;
  119. val = *(unsigned char *)
  120. ((CONFIG_SYS_NVRAM_BASE_ADDR + CONFIG_SYS_NVRAM_SIZE - 8) + reg);
  121. return val;
  122. }
  123. static void rtc_write (uchar reg, uchar val)
  124. {
  125. *(unsigned char *)
  126. ((CONFIG_SYS_NVRAM_BASE_ADDR + CONFIG_SYS_NVRAM_SIZE - 8) + reg) = val;
  127. }
  128. static unsigned bcd2bin (uchar n)
  129. {
  130. return ((((n >> 4) & 0x0F) * 10) + (n & 0x0F));
  131. }
  132. static unsigned char bin2bcd (unsigned int n)
  133. {
  134. return (((n / 10) << 4) | (n % 10));
  135. }
  136. #endif