isl1208.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. * SPDX-License-Identifier: GPL-2.0+
  8. */
  9. /*
  10. * Date & Time support (no alarms) for Intersil
  11. * ISL1208 Real Time Clock (RTC).
  12. */
  13. #include <common.h>
  14. #include <command.h>
  15. #include <rtc.h>
  16. #include <i2c.h>
  17. /*---------------------------------------------------------------------*/
  18. #ifdef DEBUG_RTC
  19. #define DEBUGR(fmt,args...) printf(fmt ,##args)
  20. #else
  21. #define DEBUGR(fmt,args...)
  22. #endif
  23. /*---------------------------------------------------------------------*/
  24. /*
  25. * RTC register addresses
  26. */
  27. #define RTC_SEC_REG_ADDR 0x0
  28. #define RTC_MIN_REG_ADDR 0x1
  29. #define RTC_HR_REG_ADDR 0x2
  30. #define RTC_DATE_REG_ADDR 0x3
  31. #define RTC_MON_REG_ADDR 0x4
  32. #define RTC_YR_REG_ADDR 0x5
  33. #define RTC_DAY_REG_ADDR 0x6
  34. #define RTC_STAT_REG_ADDR 0x7
  35. /*
  36. * RTC control register bits
  37. */
  38. /*
  39. * RTC status register bits
  40. */
  41. #define RTC_STAT_BIT_ARST 0x80 /* AUTO RESET ENABLE BIT */
  42. #define RTC_STAT_BIT_XTOSCB 0x40 /* CRYSTAL OSCILLATOR ENABLE BIT */
  43. #define RTC_STAT_BIT_WRTC 0x10 /* WRITE RTC ENABLE BIT */
  44. #define RTC_STAT_BIT_ALM 0x04 /* ALARM BIT */
  45. #define RTC_STAT_BIT_BAT 0x02 /* BATTERY BIT */
  46. #define RTC_STAT_BIT_RTCF 0x01 /* REAL TIME CLOCK FAIL BIT */
  47. static uchar rtc_read (uchar reg);
  48. static void rtc_write (uchar reg, uchar val);
  49. /*
  50. * Get the current time from the RTC
  51. */
  52. int rtc_get (struct rtc_time *tmp)
  53. {
  54. int rel = 0;
  55. uchar sec, min, hour, mday, wday, mon, year, status;
  56. status = rtc_read (RTC_STAT_REG_ADDR);
  57. sec = rtc_read (RTC_SEC_REG_ADDR);
  58. min = rtc_read (RTC_MIN_REG_ADDR);
  59. hour = rtc_read (RTC_HR_REG_ADDR);
  60. wday = rtc_read (RTC_DAY_REG_ADDR);
  61. mday = rtc_read (RTC_DATE_REG_ADDR);
  62. mon = rtc_read (RTC_MON_REG_ADDR);
  63. year = rtc_read (RTC_YR_REG_ADDR);
  64. DEBUGR ("Get RTC year: %02x mon: %02x mday: %02x wday: %02x "
  65. "hr: %02x min: %02x sec: %02x status: %02x\n",
  66. year, mon, mday, wday, hour, min, sec, status);
  67. if (status & RTC_STAT_BIT_RTCF) {
  68. printf ("### Warning: RTC oscillator has stopped\n");
  69. rtc_write(RTC_STAT_REG_ADDR,
  70. rtc_read(RTC_STAT_REG_ADDR) &~ (RTC_STAT_BIT_BAT|RTC_STAT_BIT_RTCF));
  71. rel = -1;
  72. }
  73. tmp->tm_sec = bcd2bin (sec & 0x7F);
  74. tmp->tm_min = bcd2bin (min & 0x7F);
  75. tmp->tm_hour = bcd2bin (hour & 0x3F);
  76. tmp->tm_mday = bcd2bin (mday & 0x3F);
  77. tmp->tm_mon = bcd2bin (mon & 0x1F);
  78. tmp->tm_year = bcd2bin (year)+2000;
  79. tmp->tm_wday = bcd2bin (wday & 0x07);
  80. tmp->tm_yday = 0;
  81. tmp->tm_isdst= 0;
  82. DEBUGR ("Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  83. tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
  84. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  85. return rel;
  86. }
  87. /*
  88. * Set the RTC
  89. */
  90. int rtc_set (struct rtc_time *tmp)
  91. {
  92. DEBUGR ("Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  93. tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
  94. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  95. /* enable write */
  96. rtc_write(RTC_STAT_REG_ADDR,
  97. rtc_read(RTC_STAT_REG_ADDR) | RTC_STAT_BIT_WRTC);
  98. rtc_write (RTC_YR_REG_ADDR, bin2bcd (tmp->tm_year % 100));
  99. rtc_write (RTC_MON_REG_ADDR, bin2bcd (tmp->tm_mon));
  100. rtc_write (RTC_DAY_REG_ADDR, bin2bcd (tmp->tm_wday));
  101. rtc_write (RTC_DATE_REG_ADDR, bin2bcd (tmp->tm_mday));
  102. rtc_write (RTC_HR_REG_ADDR, bin2bcd (tmp->tm_hour) | 0x80 ); /* 24h clock */
  103. rtc_write (RTC_MIN_REG_ADDR, bin2bcd (tmp->tm_min));
  104. rtc_write (RTC_SEC_REG_ADDR, bin2bcd (tmp->tm_sec));
  105. /* disable write */
  106. rtc_write(RTC_STAT_REG_ADDR,
  107. rtc_read(RTC_STAT_REG_ADDR) & ~RTC_STAT_BIT_WRTC);
  108. return 0;
  109. }
  110. void rtc_reset (void)
  111. {
  112. }
  113. /*
  114. * Helper functions
  115. */
  116. static uchar rtc_read (uchar reg)
  117. {
  118. return (i2c_reg_read (CONFIG_SYS_I2C_RTC_ADDR, reg));
  119. }
  120. static void rtc_write (uchar reg, uchar val)
  121. {
  122. i2c_reg_write (CONFIG_SYS_I2C_RTC_ADDR, reg, val);
  123. }