rtc_m41t81.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2000, 2001 Broadcom Corporation
  4. *
  5. * Copyright (C) 2002 MontaVista Software Inc.
  6. * Author: [email protected] or [email protected]
  7. */
  8. #include <linux/bcd.h>
  9. #include <linux/types.h>
  10. #include <linux/time.h>
  11. #include <asm/time.h>
  12. #include <asm/addrspace.h>
  13. #include <asm/io.h>
  14. #include <asm/sibyte/sb1250.h>
  15. #include <asm/sibyte/sb1250_regs.h>
  16. #include <asm/sibyte/sb1250_smbus.h>
  17. /* M41T81 definitions */
  18. /*
  19. * Register bits
  20. */
  21. #define M41T81REG_SC_ST 0x80 /* stop bit */
  22. #define M41T81REG_HR_CB 0x40 /* century bit */
  23. #define M41T81REG_HR_CEB 0x80 /* century enable bit */
  24. #define M41T81REG_CTL_S 0x20 /* sign bit */
  25. #define M41T81REG_CTL_FT 0x40 /* frequency test bit */
  26. #define M41T81REG_CTL_OUT 0x80 /* output level */
  27. #define M41T81REG_WD_RB0 0x01 /* watchdog resolution bit 0 */
  28. #define M41T81REG_WD_RB1 0x02 /* watchdog resolution bit 1 */
  29. #define M41T81REG_WD_BMB0 0x04 /* watchdog multiplier bit 0 */
  30. #define M41T81REG_WD_BMB1 0x08 /* watchdog multiplier bit 1 */
  31. #define M41T81REG_WD_BMB2 0x10 /* watchdog multiplier bit 2 */
  32. #define M41T81REG_WD_BMB3 0x20 /* watchdog multiplier bit 3 */
  33. #define M41T81REG_WD_BMB4 0x40 /* watchdog multiplier bit 4 */
  34. #define M41T81REG_AMO_ABE 0x20 /* alarm in "battery back-up mode" enable bit */
  35. #define M41T81REG_AMO_SQWE 0x40 /* square wave enable */
  36. #define M41T81REG_AMO_AFE 0x80 /* alarm flag enable flag */
  37. #define M41T81REG_ADT_RPT5 0x40 /* alarm repeat mode bit 5 */
  38. #define M41T81REG_ADT_RPT4 0x80 /* alarm repeat mode bit 4 */
  39. #define M41T81REG_AHR_RPT3 0x80 /* alarm repeat mode bit 3 */
  40. #define M41T81REG_AHR_HT 0x40 /* halt update bit */
  41. #define M41T81REG_AMN_RPT2 0x80 /* alarm repeat mode bit 2 */
  42. #define M41T81REG_ASC_RPT1 0x80 /* alarm repeat mode bit 1 */
  43. #define M41T81REG_FLG_AF 0x40 /* alarm flag (read only) */
  44. #define M41T81REG_FLG_WDF 0x80 /* watchdog flag (read only) */
  45. #define M41T81REG_SQW_RS0 0x10 /* sqw frequency bit 0 */
  46. #define M41T81REG_SQW_RS1 0x20 /* sqw frequency bit 1 */
  47. #define M41T81REG_SQW_RS2 0x40 /* sqw frequency bit 2 */
  48. #define M41T81REG_SQW_RS3 0x80 /* sqw frequency bit 3 */
  49. /*
  50. * Register numbers
  51. */
  52. #define M41T81REG_TSC 0x00 /* tenths/hundredths of second */
  53. #define M41T81REG_SC 0x01 /* seconds */
  54. #define M41T81REG_MN 0x02 /* minute */
  55. #define M41T81REG_HR 0x03 /* hour/century */
  56. #define M41T81REG_DY 0x04 /* day of week */
  57. #define M41T81REG_DT 0x05 /* date of month */
  58. #define M41T81REG_MO 0x06 /* month */
  59. #define M41T81REG_YR 0x07 /* year */
  60. #define M41T81REG_CTL 0x08 /* control */
  61. #define M41T81REG_WD 0x09 /* watchdog */
  62. #define M41T81REG_AMO 0x0A /* alarm: month */
  63. #define M41T81REG_ADT 0x0B /* alarm: date */
  64. #define M41T81REG_AHR 0x0C /* alarm: hour */
  65. #define M41T81REG_AMN 0x0D /* alarm: minute */
  66. #define M41T81REG_ASC 0x0E /* alarm: second */
  67. #define M41T81REG_FLG 0x0F /* flags */
  68. #define M41T81REG_SQW 0x13 /* square wave register */
  69. #define M41T81_CCR_ADDRESS 0x68
  70. #define SMB_CSR(reg) IOADDR(A_SMB_REGISTER(1, reg))
  71. static int m41t81_read(uint8_t addr)
  72. {
  73. while (__raw_readq(SMB_CSR(R_SMB_STATUS)) & M_SMB_BUSY)
  74. ;
  75. __raw_writeq(addr & 0xff, SMB_CSR(R_SMB_CMD));
  76. __raw_writeq(V_SMB_ADDR(M41T81_CCR_ADDRESS) | V_SMB_TT_WR1BYTE,
  77. SMB_CSR(R_SMB_START));
  78. while (__raw_readq(SMB_CSR(R_SMB_STATUS)) & M_SMB_BUSY)
  79. ;
  80. __raw_writeq(V_SMB_ADDR(M41T81_CCR_ADDRESS) | V_SMB_TT_RD1BYTE,
  81. SMB_CSR(R_SMB_START));
  82. while (__raw_readq(SMB_CSR(R_SMB_STATUS)) & M_SMB_BUSY)
  83. ;
  84. if (__raw_readq(SMB_CSR(R_SMB_STATUS)) & M_SMB_ERROR) {
  85. /* Clear error bit by writing a 1 */
  86. __raw_writeq(M_SMB_ERROR, SMB_CSR(R_SMB_STATUS));
  87. return -1;
  88. }
  89. return __raw_readq(SMB_CSR(R_SMB_DATA)) & 0xff;
  90. }
  91. static int m41t81_write(uint8_t addr, int b)
  92. {
  93. while (__raw_readq(SMB_CSR(R_SMB_STATUS)) & M_SMB_BUSY)
  94. ;
  95. __raw_writeq(addr & 0xff, SMB_CSR(R_SMB_CMD));
  96. __raw_writeq(b & 0xff, SMB_CSR(R_SMB_DATA));
  97. __raw_writeq(V_SMB_ADDR(M41T81_CCR_ADDRESS) | V_SMB_TT_WR2BYTE,
  98. SMB_CSR(R_SMB_START));
  99. while (__raw_readq(SMB_CSR(R_SMB_STATUS)) & M_SMB_BUSY)
  100. ;
  101. if (__raw_readq(SMB_CSR(R_SMB_STATUS)) & M_SMB_ERROR) {
  102. /* Clear error bit by writing a 1 */
  103. __raw_writeq(M_SMB_ERROR, SMB_CSR(R_SMB_STATUS));
  104. return -1;
  105. }
  106. /* read the same byte again to make sure it is written */
  107. __raw_writeq(V_SMB_ADDR(M41T81_CCR_ADDRESS) | V_SMB_TT_RD1BYTE,
  108. SMB_CSR(R_SMB_START));
  109. while (__raw_readq(SMB_CSR(R_SMB_STATUS)) & M_SMB_BUSY)
  110. ;
  111. return 0;
  112. }
  113. int m41t81_set_time(time64_t t)
  114. {
  115. struct rtc_time tm;
  116. unsigned long flags;
  117. /* Note we don't care about the century */
  118. rtc_time64_to_tm(t, &tm);
  119. /*
  120. * Note the write order matters as it ensures the correctness.
  121. * When we write sec, 10th sec is clear. It is reasonable to
  122. * believe we should finish writing min within a second.
  123. */
  124. spin_lock_irqsave(&rtc_lock, flags);
  125. tm.tm_sec = bin2bcd(tm.tm_sec);
  126. m41t81_write(M41T81REG_SC, tm.tm_sec);
  127. tm.tm_min = bin2bcd(tm.tm_min);
  128. m41t81_write(M41T81REG_MN, tm.tm_min);
  129. tm.tm_hour = bin2bcd(tm.tm_hour);
  130. tm.tm_hour = (tm.tm_hour & 0x3f) | (m41t81_read(M41T81REG_HR) & 0xc0);
  131. m41t81_write(M41T81REG_HR, tm.tm_hour);
  132. /* tm_wday starts from 0 to 6 */
  133. if (tm.tm_wday == 0) tm.tm_wday = 7;
  134. tm.tm_wday = bin2bcd(tm.tm_wday);
  135. m41t81_write(M41T81REG_DY, tm.tm_wday);
  136. tm.tm_mday = bin2bcd(tm.tm_mday);
  137. m41t81_write(M41T81REG_DT, tm.tm_mday);
  138. /* tm_mon starts from 0, *ick* */
  139. tm.tm_mon ++;
  140. tm.tm_mon = bin2bcd(tm.tm_mon);
  141. m41t81_write(M41T81REG_MO, tm.tm_mon);
  142. /* we don't do century, everything is beyond 2000 */
  143. tm.tm_year %= 100;
  144. tm.tm_year = bin2bcd(tm.tm_year);
  145. m41t81_write(M41T81REG_YR, tm.tm_year);
  146. spin_unlock_irqrestore(&rtc_lock, flags);
  147. return 0;
  148. }
  149. time64_t m41t81_get_time(void)
  150. {
  151. unsigned int year, mon, day, hour, min, sec;
  152. unsigned long flags;
  153. /*
  154. * min is valid if two reads of sec are the same.
  155. */
  156. for (;;) {
  157. spin_lock_irqsave(&rtc_lock, flags);
  158. sec = m41t81_read(M41T81REG_SC);
  159. min = m41t81_read(M41T81REG_MN);
  160. if (sec == m41t81_read(M41T81REG_SC)) break;
  161. spin_unlock_irqrestore(&rtc_lock, flags);
  162. }
  163. hour = m41t81_read(M41T81REG_HR) & 0x3f;
  164. day = m41t81_read(M41T81REG_DT);
  165. mon = m41t81_read(M41T81REG_MO);
  166. year = m41t81_read(M41T81REG_YR);
  167. spin_unlock_irqrestore(&rtc_lock, flags);
  168. sec = bcd2bin(sec);
  169. min = bcd2bin(min);
  170. hour = bcd2bin(hour);
  171. day = bcd2bin(day);
  172. mon = bcd2bin(mon);
  173. year = bcd2bin(year);
  174. year += 2000;
  175. return mktime64(year, mon, day, hour, min, sec);
  176. }
  177. int m41t81_probe(void)
  178. {
  179. unsigned int tmp;
  180. /* enable chip if it is not enabled yet */
  181. tmp = m41t81_read(M41T81REG_SC);
  182. m41t81_write(M41T81REG_SC, tmp & 0x7f);
  183. return m41t81_read(M41T81REG_SC) != -1;
  184. }