rtc4543.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * (C) Copyright 2008, 2009
  3. * Andreas Pfefferle, DENX Software Engineering, ap@denx.de.
  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. #include <asm/io.h>
  24. #include <common.h>
  25. #include <command.h>
  26. #include <config.h>
  27. #include <bcd.h>
  28. #include <rtc.h>
  29. #include <tws.h>
  30. #if defined(CONFIG_CMD_DATE)
  31. /*
  32. * Note: The acrobatics below is due to the hideously ingenius idea of
  33. * the chip designers. As the chip does not allow register
  34. * addressing, all values need to be read and written in one go. Sure
  35. * enough, the 'wday' field (0-6) is transferred using the economic
  36. * number of 4 bits right in the middle of the packet.....
  37. */
  38. int rtc_get(struct rtc_time *tm)
  39. {
  40. int rel = 0;
  41. uchar buffer[7];
  42. memset(buffer, 0, 7);
  43. /* Read 52 bits into our buffer */
  44. tws_read(buffer, 52);
  45. tm->tm_sec = BCD2BIN( buffer[0] & 0x7F);
  46. tm->tm_min = BCD2BIN( buffer[1] & 0x7F);
  47. tm->tm_hour = BCD2BIN( buffer[2] & 0x3F);
  48. tm->tm_wday = BCD2BIN( buffer[3] & 0x07);
  49. tm->tm_mday = BCD2BIN((buffer[3] & 0xF0) >> 4 | (buffer[4] & 0x0F) << 4);
  50. tm->tm_mon = BCD2BIN((buffer[4] & 0x30) >> 4 | (buffer[5] & 0x0F) << 4);
  51. tm->tm_year = BCD2BIN((buffer[5] & 0xF0) >> 4 | (buffer[6] & 0x0F) << 4) + 2000;
  52. tm->tm_yday = 0;
  53. tm->tm_isdst = 0;
  54. if (tm->tm_sec & 0x80) {
  55. puts("### Warning: RTC Low Voltage - date/time not reliable\n");
  56. rel = -1;
  57. }
  58. debug("Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  59. tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_wday,
  60. tm->tm_hour, tm->tm_min, tm->tm_sec);
  61. return rel;
  62. }
  63. int rtc_set(struct rtc_time *tm)
  64. {
  65. uchar buffer[7];
  66. uchar tmp;
  67. debug("Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  68. tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_wday,
  69. tm->tm_hour, tm->tm_min, tm->tm_sec);
  70. memset(buffer, 0, 7);
  71. buffer[0] = BIN2BCD(tm->tm_sec);
  72. buffer[1] = BIN2BCD(tm->tm_min);
  73. buffer[2] = BIN2BCD(tm->tm_hour);
  74. buffer[3] = BIN2BCD(tm->tm_wday);
  75. tmp = BIN2BCD(tm->tm_mday);
  76. buffer[3] |= (tmp & 0x0F) << 4;
  77. buffer[4] = (tmp & 0xF0) >> 4;
  78. tmp = BIN2BCD(tm->tm_mon);
  79. buffer[4] |= (tmp & 0x0F) << 4;
  80. buffer[5] = (tmp & 0xF0) >> 4;
  81. tmp = BIN2BCD(tm->tm_year % 100);
  82. buffer[5] |= (tmp & 0x0F) << 4;
  83. buffer[6] = (tmp & 0xF0) >> 4;
  84. /* Write the resulting 52 bits to device */
  85. tws_write(buffer, 52);
  86. return 0;
  87. }
  88. void rtc_reset(void)
  89. {
  90. struct rtc_time tmp;
  91. tmp.tm_sec = 0;
  92. tmp.tm_min = 0;
  93. tmp.tm_hour = 0;
  94. tmp.tm_wday = 4;
  95. tmp.tm_mday = 1;
  96. tmp.tm_mon = 1;
  97. tmp.tm_year = 2000;
  98. rtc_set(&tmp);
  99. }
  100. #endif