max6900.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * (C) Copyright 2004
  3. * Wolfgang Denk, DENX Software Engineering, wd@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. /*
  24. * Date & Time support for MAXIM MAX6900 RTC
  25. */
  26. /* #define DEBUG */
  27. #include <common.h>
  28. #include <command.h>
  29. #include <rtc.h>
  30. #include <i2c.h>
  31. #if defined(CONFIG_CMD_DATE)
  32. #ifndef CONFIG_SYS_I2C_RTC_ADDR
  33. #define CONFIG_SYS_I2C_RTC_ADDR 0x50
  34. #endif
  35. /* ------------------------------------------------------------------------- */
  36. static uchar rtc_read (uchar reg)
  37. {
  38. return (i2c_reg_read (CONFIG_SYS_I2C_RTC_ADDR, reg));
  39. }
  40. static void rtc_write (uchar reg, uchar val)
  41. {
  42. i2c_reg_write (CONFIG_SYS_I2C_RTC_ADDR, reg, val);
  43. udelay(2500);
  44. }
  45. static unsigned bcd2bin (uchar n)
  46. {
  47. return ((((n >> 4) & 0x0F) * 10) + (n & 0x0F));
  48. }
  49. static unsigned char bin2bcd (unsigned int n)
  50. {
  51. return (((n / 10) << 4) | (n % 10));
  52. }
  53. /* ------------------------------------------------------------------------- */
  54. int rtc_get (struct rtc_time *tmp)
  55. {
  56. uchar sec, min, hour, mday, wday, mon, cent, year;
  57. int retry = 1;
  58. do {
  59. sec = rtc_read (0x80);
  60. min = rtc_read (0x82);
  61. hour = rtc_read (0x84);
  62. mday = rtc_read (0x86);
  63. mon = rtc_read (0x88);
  64. wday = rtc_read (0x8a);
  65. year = rtc_read (0x8c);
  66. cent = rtc_read (0x92);
  67. /*
  68. * Check for seconds rollover
  69. */
  70. if ((sec != 59) || (rtc_read(0x80) == sec)){
  71. retry = 0;
  72. }
  73. } while (retry);
  74. debug ( "Get RTC year: %02x mon: %02x cent: %02x mday: %02x wday: %02x "
  75. "hr: %02x min: %02x sec: %02x\n",
  76. year, mon, cent, mday, wday,
  77. hour, min, sec );
  78. tmp->tm_sec = bcd2bin (sec & 0x7F);
  79. tmp->tm_min = bcd2bin (min & 0x7F);
  80. tmp->tm_hour = bcd2bin (hour & 0x3F);
  81. tmp->tm_mday = bcd2bin (mday & 0x3F);
  82. tmp->tm_mon = bcd2bin (mon & 0x1F);
  83. tmp->tm_year = bcd2bin (year) + bcd2bin(cent) * 100;
  84. tmp->tm_wday = bcd2bin (wday & 0x07);
  85. tmp->tm_yday = 0;
  86. tmp->tm_isdst= 0;
  87. debug ( "Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  88. tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
  89. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  90. return 0;
  91. }
  92. int rtc_set (struct rtc_time *tmp)
  93. {
  94. debug ( "Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  95. tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
  96. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  97. rtc_write (0x9E, 0x00);
  98. rtc_write (0x80, 0); /* Clear seconds to ensure no rollover */
  99. rtc_write (0x92, bin2bcd(tmp->tm_year / 100));
  100. rtc_write (0x8c, bin2bcd(tmp->tm_year % 100));
  101. rtc_write (0x8a, bin2bcd(tmp->tm_wday));
  102. rtc_write (0x88, bin2bcd(tmp->tm_mon));
  103. rtc_write (0x86, bin2bcd(tmp->tm_mday));
  104. rtc_write (0x84, bin2bcd(tmp->tm_hour));
  105. rtc_write (0x82, bin2bcd(tmp->tm_min ));
  106. rtc_write (0x80, bin2bcd(tmp->tm_sec ));
  107. return 0;
  108. }
  109. void rtc_reset (void)
  110. {
  111. }
  112. #endif