isl1208.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * (C) Copyright 2008
  3. * Tor Krill, Excito Elektronik i Skåne , tor@excito.com
  4. *
  5. * Modelled after the ds1337 driver
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  20. * MA 02111-1307 USA
  21. */
  22. /*
  23. * Date & Time support (no alarms) for Intersil
  24. * ISL1208 Real Time Clock (RTC).
  25. */
  26. #include <common.h>
  27. #include <command.h>
  28. #include <rtc.h>
  29. #include <i2c.h>
  30. /*---------------------------------------------------------------------*/
  31. #ifdef DEBUG_RTC
  32. #define DEBUGR(fmt,args...) printf(fmt ,##args)
  33. #else
  34. #define DEBUGR(fmt,args...)
  35. #endif
  36. /*---------------------------------------------------------------------*/
  37. /*
  38. * RTC register addresses
  39. */
  40. #define RTC_SEC_REG_ADDR 0x0
  41. #define RTC_MIN_REG_ADDR 0x1
  42. #define RTC_HR_REG_ADDR 0x2
  43. #define RTC_DATE_REG_ADDR 0x3
  44. #define RTC_MON_REG_ADDR 0x4
  45. #define RTC_YR_REG_ADDR 0x5
  46. #define RTC_DAY_REG_ADDR 0x6
  47. #define RTC_STAT_REG_ADDR 0x7
  48. /*
  49. * RTC control register bits
  50. */
  51. /*
  52. * RTC status register bits
  53. */
  54. #define RTC_STAT_BIT_ARST 0x80 /* AUTO RESET ENABLE BIT */
  55. #define RTC_STAT_BIT_XTOSCB 0x40 /* CRYSTAL OSCILLATOR ENABLE BIT */
  56. #define RTC_STAT_BIT_WRTC 0x10 /* WRITE RTC ENABLE BIT */
  57. #define RTC_STAT_BIT_ALM 0x04 /* ALARM BIT */
  58. #define RTC_STAT_BIT_BAT 0x02 /* BATTERY BIT */
  59. #define RTC_STAT_BIT_RTCF 0x01 /* REAL TIME CLOCK FAIL BIT */
  60. static uchar rtc_read (uchar reg);
  61. static void rtc_write (uchar reg, uchar val);
  62. static uchar bin2bcd (unsigned int n);
  63. static unsigned bcd2bin (uchar c);
  64. /*
  65. * Get the current time from the RTC
  66. */
  67. int rtc_get (struct rtc_time *tmp)
  68. {
  69. int rel = 0;
  70. uchar sec, min, hour, mday, wday, mon, year, status;
  71. status = rtc_read (RTC_STAT_REG_ADDR);
  72. sec = rtc_read (RTC_SEC_REG_ADDR);
  73. min = rtc_read (RTC_MIN_REG_ADDR);
  74. hour = rtc_read (RTC_HR_REG_ADDR);
  75. wday = rtc_read (RTC_DAY_REG_ADDR);
  76. mday = rtc_read (RTC_DATE_REG_ADDR);
  77. mon = rtc_read (RTC_MON_REG_ADDR);
  78. year = rtc_read (RTC_YR_REG_ADDR);
  79. DEBUGR ("Get RTC year: %02x mon: %02x mday: %02x wday: %02x "
  80. "hr: %02x min: %02x sec: %02x status: %02x\n",
  81. year, mon, mday, wday, hour, min, sec, status);
  82. if (status & RTC_STAT_BIT_RTCF) {
  83. printf ("### Warning: RTC oscillator has stopped\n");
  84. rtc_write(RTC_STAT_REG_ADDR,
  85. rtc_read(RTC_STAT_REG_ADDR) &~ (RTC_STAT_BIT_BAT|RTC_STAT_BIT_RTCF));
  86. rel = -1;
  87. }
  88. tmp->tm_sec = bcd2bin (sec & 0x7F);
  89. tmp->tm_min = bcd2bin (min & 0x7F);
  90. tmp->tm_hour = bcd2bin (hour & 0x3F);
  91. tmp->tm_mday = bcd2bin (mday & 0x3F);
  92. tmp->tm_mon = bcd2bin (mon & 0x1F);
  93. tmp->tm_year = bcd2bin (year)+2000;
  94. tmp->tm_wday = bcd2bin (wday & 0x07);
  95. tmp->tm_yday = 0;
  96. tmp->tm_isdst= 0;
  97. DEBUGR ("Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  98. tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
  99. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  100. return rel;
  101. }
  102. /*
  103. * Set the RTC
  104. */
  105. int rtc_set (struct rtc_time *tmp)
  106. {
  107. DEBUGR ("Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  108. tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
  109. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  110. /* enable write */
  111. rtc_write(RTC_STAT_REG_ADDR,
  112. rtc_read(RTC_STAT_REG_ADDR) | RTC_STAT_BIT_WRTC);
  113. rtc_write (RTC_YR_REG_ADDR, bin2bcd (tmp->tm_year % 100));
  114. rtc_write (RTC_MON_REG_ADDR, bin2bcd (tmp->tm_mon));
  115. rtc_write (RTC_DAY_REG_ADDR, bin2bcd (tmp->tm_wday));
  116. rtc_write (RTC_DATE_REG_ADDR, bin2bcd (tmp->tm_mday));
  117. rtc_write (RTC_HR_REG_ADDR, bin2bcd (tmp->tm_hour) | 0x80 ); /* 24h clock */
  118. rtc_write (RTC_MIN_REG_ADDR, bin2bcd (tmp->tm_min));
  119. rtc_write (RTC_SEC_REG_ADDR, bin2bcd (tmp->tm_sec));
  120. /* disable write */
  121. rtc_write(RTC_STAT_REG_ADDR,
  122. rtc_read(RTC_STAT_REG_ADDR) & ~RTC_STAT_BIT_WRTC);
  123. return 0;
  124. }
  125. void rtc_reset (void)
  126. {
  127. }
  128. /*
  129. * Helper functions
  130. */
  131. static uchar rtc_read (uchar reg)
  132. {
  133. return (i2c_reg_read (CONFIG_SYS_I2C_RTC_ADDR, reg));
  134. }
  135. static void rtc_write (uchar reg, uchar val)
  136. {
  137. i2c_reg_write (CONFIG_SYS_I2C_RTC_ADDR, reg, val);
  138. }
  139. static unsigned bcd2bin (uchar n)
  140. {
  141. return ((((n >> 4) & 0x0F) * 10) + (n & 0x0F));
  142. }
  143. static unsigned char bin2bcd (unsigned int n)
  144. {
  145. return (((n / 10) << 4) | (n % 10));
  146. }