pt7c4338.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright 2010 Freescale Semiconductor, Inc.
  3. *
  4. * Author: Priyanka Jain <Priyanka.Jain@freescale.com>
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. */
  8. /*
  9. * This file provides Date & Time support (no alarms) for PT7C4338 chip.
  10. *
  11. * This file is based on drivers/rtc/ds1337.c
  12. *
  13. * PT7C4338 chip is manufactured by Pericom Technology Inc.
  14. * It is a serial real-time clock which provides
  15. * 1)Low-power clock/calendar.
  16. * 2)Programmable square-wave output.
  17. * It has 56 bytes of nonvolatile RAM.
  18. */
  19. #include <common.h>
  20. #include <command.h>
  21. #include <rtc.h>
  22. #include <i2c.h>
  23. /* RTC register addresses */
  24. #define RTC_SEC_REG_ADDR 0x0
  25. #define RTC_MIN_REG_ADDR 0x1
  26. #define RTC_HR_REG_ADDR 0x2
  27. #define RTC_DAY_REG_ADDR 0x3
  28. #define RTC_DATE_REG_ADDR 0x4
  29. #define RTC_MON_REG_ADDR 0x5
  30. #define RTC_YR_REG_ADDR 0x6
  31. #define RTC_CTL_STAT_REG_ADDR 0x7
  32. /* RTC second register address bit */
  33. #define RTC_SEC_BIT_CH 0x80 /* Clock Halt (in Register 0) */
  34. /* RTC control and status register bits */
  35. #define RTC_CTL_STAT_BIT_RS0 0x1 /* Rate select 0 */
  36. #define RTC_CTL_STAT_BIT_RS1 0x2 /* Rate select 1 */
  37. #define RTC_CTL_STAT_BIT_SQWE 0x10 /* Square Wave Enable */
  38. #define RTC_CTL_STAT_BIT_OSF 0x20 /* Oscillator Stop Flag */
  39. #define RTC_CTL_STAT_BIT_OUT 0x80 /* Output Level Control */
  40. /* RTC reset value */
  41. #define RTC_PT7C4338_RESET_VAL \
  42. (RTC_CTL_STAT_BIT_RS0 | RTC_CTL_STAT_BIT_RS1 | RTC_CTL_STAT_BIT_OUT)
  43. /****** Helper functions ****************************************/
  44. static u8 rtc_read(u8 reg)
  45. {
  46. return i2c_reg_read(CONFIG_SYS_I2C_RTC_ADDR, reg);
  47. }
  48. static void rtc_write(u8 reg, u8 val)
  49. {
  50. i2c_reg_write(CONFIG_SYS_I2C_RTC_ADDR, reg, val);
  51. }
  52. /****************************************************************/
  53. /* Get the current time from the RTC */
  54. int rtc_get(struct rtc_time *tmp)
  55. {
  56. int ret = 0;
  57. u8 sec, min, hour, mday, wday, mon, year, ctl_stat;
  58. ctl_stat = rtc_read(RTC_CTL_STAT_REG_ADDR);
  59. sec = rtc_read(RTC_SEC_REG_ADDR);
  60. min = rtc_read(RTC_MIN_REG_ADDR);
  61. hour = rtc_read(RTC_HR_REG_ADDR);
  62. wday = rtc_read(RTC_DAY_REG_ADDR);
  63. mday = rtc_read(RTC_DATE_REG_ADDR);
  64. mon = rtc_read(RTC_MON_REG_ADDR);
  65. year = rtc_read(RTC_YR_REG_ADDR);
  66. debug("Get RTC year: %02x mon: %02x mday: %02x wday: %02x "
  67. "hr: %02x min: %02x sec: %02x control_status: %02x\n",
  68. year, mon, mday, wday, hour, min, sec, ctl_stat);
  69. if (ctl_stat & RTC_CTL_STAT_BIT_OSF) {
  70. printf("### Warning: RTC oscillator has stopped\n");
  71. /* clear the OSF flag */
  72. rtc_write(RTC_CTL_STAT_REG_ADDR,
  73. rtc_read(RTC_CTL_STAT_REG_ADDR)\
  74. & ~RTC_CTL_STAT_BIT_OSF);
  75. ret = -1;
  76. }
  77. tmp->tm_sec = bcd2bin(sec & 0x7F);
  78. tmp->tm_min = bcd2bin(min & 0x7F);
  79. tmp->tm_hour = bcd2bin(hour & 0x3F);
  80. tmp->tm_mday = bcd2bin(mday & 0x3F);
  81. tmp->tm_mon = bcd2bin(mon & 0x1F);
  82. tmp->tm_year = bcd2bin(year) + 2000;
  83. tmp->tm_wday = bcd2bin((wday - 1) & 0x07);
  84. tmp->tm_yday = 0;
  85. tmp->tm_isdst = 0;
  86. debug("Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  87. tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
  88. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  89. return ret;
  90. }
  91. /* Set the RTC */
  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(RTC_YR_REG_ADDR, bin2bcd(tmp->tm_year % 100));
  98. rtc_write(RTC_MON_REG_ADDR, bin2bcd(tmp->tm_mon));
  99. rtc_write(RTC_DAY_REG_ADDR, bin2bcd(tmp->tm_wday + 1));
  100. rtc_write(RTC_DATE_REG_ADDR, bin2bcd(tmp->tm_mday));
  101. rtc_write(RTC_HR_REG_ADDR, bin2bcd(tmp->tm_hour));
  102. rtc_write(RTC_MIN_REG_ADDR, bin2bcd(tmp->tm_min));
  103. rtc_write(RTC_SEC_REG_ADDR, bin2bcd(tmp->tm_sec));
  104. return 0;
  105. }
  106. /* Reset the RTC */
  107. void rtc_reset(void)
  108. {
  109. rtc_write(RTC_SEC_REG_ADDR, 0x00); /* clearing Clock Halt */
  110. rtc_write(RTC_CTL_STAT_REG_ADDR, RTC_PT7C4338_RESET_VAL);
  111. }