rtc4543.c 2.4 KB

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