m48t35ax.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. /* ------------------------------------------------------------------------- */
  35. int rtc_get (struct rtc_time *tmp)
  36. {
  37. uchar sec, min, hour, cent_day, date, month, year;
  38. uchar ccr; /* Clock control register */
  39. /* Lock RTC for read using clock control register */
  40. ccr = rtc_read(0);
  41. ccr = ccr | 0x40;
  42. rtc_write(0, ccr);
  43. sec = rtc_read (0x1);
  44. min = rtc_read (0x2);
  45. hour = rtc_read (0x3);
  46. cent_day= rtc_read (0x4);
  47. date = rtc_read (0x5);
  48. month = rtc_read (0x6);
  49. year = rtc_read (0x7);
  50. /* UNLock RTC */
  51. ccr = rtc_read(0);
  52. ccr = ccr & 0xBF;
  53. rtc_write(0, ccr);
  54. debug ( "Get RTC year: %02x month: %02x date: %02x cent_day: %02x "
  55. "hr: %02x min: %02x sec: %02x\n",
  56. year, month, date, cent_day,
  57. hour, min, sec );
  58. tmp->tm_sec = bcd2bin (sec & 0x7F);
  59. tmp->tm_min = bcd2bin (min & 0x7F);
  60. tmp->tm_hour = bcd2bin (hour & 0x3F);
  61. tmp->tm_mday = bcd2bin (date & 0x3F);
  62. tmp->tm_mon = bcd2bin (month & 0x1F);
  63. tmp->tm_year = bcd2bin (year) + ((cent_day & 0x10) ? 2000 : 1900);
  64. tmp->tm_wday = bcd2bin (cent_day & 0x07);
  65. tmp->tm_yday = 0;
  66. tmp->tm_isdst= 0;
  67. debug ( "Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  68. tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
  69. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  70. return 0;
  71. }
  72. int rtc_set (struct rtc_time *tmp)
  73. {
  74. uchar ccr; /* Clock control register */
  75. uchar century;
  76. debug ( "Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  77. tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
  78. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  79. /* Lock RTC for write using clock control register */
  80. ccr = rtc_read(0);
  81. ccr = ccr | 0x80;
  82. rtc_write(0, ccr);
  83. rtc_write (0x07, bin2bcd(tmp->tm_year % 100));
  84. rtc_write (0x06, bin2bcd(tmp->tm_mon));
  85. rtc_write (0x05, bin2bcd(tmp->tm_mday));
  86. century = ((tmp->tm_year >= 2000) ? 0x10 : 0) | 0x20;
  87. rtc_write (0x04, bin2bcd(tmp->tm_wday) | century);
  88. rtc_write (0x03, bin2bcd(tmp->tm_hour));
  89. rtc_write (0x02, bin2bcd(tmp->tm_min ));
  90. rtc_write (0x01, bin2bcd(tmp->tm_sec ));
  91. /* UNLock RTC */
  92. ccr = rtc_read(0);
  93. ccr = ccr & 0x7F;
  94. rtc_write(0, ccr);
  95. return 0;
  96. }
  97. void rtc_reset (void)
  98. {
  99. uchar val;
  100. /* Clear all clock control registers */
  101. rtc_write (0x0, 0x80); /* No Read Lock or calibration */
  102. /* Clear stop bit */
  103. val = rtc_read (0x1);
  104. val &= 0x7f;
  105. rtc_write(0x1, val);
  106. /* Enable century / disable frequency test */
  107. val = rtc_read (0x4);
  108. val = (val & 0xBF) | 0x20;
  109. rtc_write(0x4, val);
  110. /* Clear write lock */
  111. rtc_write(0x0, 0);
  112. }
  113. /* ------------------------------------------------------------------------- */
  114. static uchar rtc_read (uchar reg)
  115. {
  116. uchar val;
  117. val = *(unsigned char *)
  118. ((CONFIG_SYS_NVRAM_BASE_ADDR + CONFIG_SYS_NVRAM_SIZE - 8) + reg);
  119. return val;
  120. }
  121. static void rtc_write (uchar reg, uchar val)
  122. {
  123. *(unsigned char *)
  124. ((CONFIG_SYS_NVRAM_BASE_ADDR + CONFIG_SYS_NVRAM_SIZE - 8) + reg) = val;
  125. }
  126. #endif