rtc_rx5c348.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * RTC routines for RICOH Rx5C348 SPI chip.
  3. * Copyright (C) 2000-2001 Toshiba Corporation
  4. *
  5. * 2003-2005 (c) MontaVista Software, Inc. This file is licensed under the
  6. * terms of the GNU General Public License version 2. This program is
  7. * licensed "as is" without any warranty of any kind, whether express
  8. * or implied.
  9. *
  10. * Support for TX4938 in 2.6 - Manish Lachwani (mlachwani@mvista.com)
  11. */
  12. #include <linux/init.h>
  13. #include <linux/kernel.h>
  14. #include <linux/string.h>
  15. #include <linux/rtc.h>
  16. #include <linux/time.h>
  17. #include <linux/bcd.h>
  18. #include <asm/time.h>
  19. #include <asm/tx4938/spi.h>
  20. #define EPOCH 2000
  21. /* registers */
  22. #define Rx5C348_REG_SECOND 0
  23. #define Rx5C348_REG_MINUTE 1
  24. #define Rx5C348_REG_HOUR 2
  25. #define Rx5C348_REG_WEEK 3
  26. #define Rx5C348_REG_DAY 4
  27. #define Rx5C348_REG_MONTH 5
  28. #define Rx5C348_REG_YEAR 6
  29. #define Rx5C348_REG_ADJUST 7
  30. #define Rx5C348_REG_ALARM_W_MIN 8
  31. #define Rx5C348_REG_ALARM_W_HOUR 9
  32. #define Rx5C348_REG_ALARM_W_WEEK 10
  33. #define Rx5C348_REG_ALARM_D_MIN 11
  34. #define Rx5C348_REG_ALARM_D_HOUR 12
  35. #define Rx5C348_REG_CTL1 14
  36. #define Rx5C348_REG_CTL2 15
  37. /* register bits */
  38. #define Rx5C348_BIT_PM 0x20 /* REG_HOUR */
  39. #define Rx5C348_BIT_Y2K 0x80 /* REG_MONTH */
  40. #define Rx5C348_BIT_24H 0x20 /* REG_CTL1 */
  41. #define Rx5C348_BIT_XSTP 0x10 /* REG_CTL2 */
  42. /* commands */
  43. #define Rx5C348_CMD_W(addr) (((addr) << 4) | 0x08) /* single write */
  44. #define Rx5C348_CMD_R(addr) (((addr) << 4) | 0x0c) /* single read */
  45. #define Rx5C348_CMD_MW(addr) (((addr) << 4) | 0x00) /* burst write */
  46. #define Rx5C348_CMD_MR(addr) (((addr) << 4) | 0x04) /* burst read */
  47. static struct spi_dev_desc srtc_dev_desc = {
  48. .baud = 1000000, /* 1.0Mbps @ Vdd 2.0V */
  49. .tcss = 31,
  50. .tcsh = 1,
  51. .tcsr = 62,
  52. /* 31us for Tcss (62us for Tcsr) is required for carry operation) */
  53. .byteorder = 1, /* MSB-First */
  54. .polarity = 0, /* High-Active */
  55. .phase = 1, /* Shift-Then-Sample */
  56. };
  57. static int srtc_chipid;
  58. static int srtc_24h;
  59. static inline int
  60. spi_rtc_io(unsigned char *inbuf, unsigned char *outbuf, unsigned int count)
  61. {
  62. unsigned char *inbufs[1], *outbufs[1];
  63. unsigned int incounts[2], outcounts[2];
  64. inbufs[0] = inbuf;
  65. incounts[0] = count;
  66. incounts[1] = 0;
  67. outbufs[0] = outbuf;
  68. outcounts[0] = count;
  69. outcounts[1] = 0;
  70. return txx9_spi_io(srtc_chipid, &srtc_dev_desc,
  71. inbufs, incounts, outbufs, outcounts, 0);
  72. }
  73. /* RTC-dependent code for time.c */
  74. static int
  75. rtc_rx5c348_set_time(unsigned long t)
  76. {
  77. unsigned char inbuf[8];
  78. struct rtc_time tm;
  79. u8 year, month, day, hour, minute, second, century;
  80. /* convert */
  81. to_tm(t, &tm);
  82. year = tm.tm_year % 100;
  83. month = tm.tm_mon+1; /* tm_mon starts from 0 to 11 */
  84. day = tm.tm_mday;
  85. hour = tm.tm_hour;
  86. minute = tm.tm_min;
  87. second = tm.tm_sec;
  88. century = tm.tm_year / 100;
  89. inbuf[0] = Rx5C348_CMD_MW(Rx5C348_REG_SECOND);
  90. BIN_TO_BCD(second);
  91. inbuf[1] = second;
  92. BIN_TO_BCD(minute);
  93. inbuf[2] = minute;
  94. if (srtc_24h) {
  95. BIN_TO_BCD(hour);
  96. inbuf[3] = hour;
  97. } else {
  98. /* hour 0 is AM12, noon is PM12 */
  99. inbuf[3] = 0;
  100. if (hour >= 12)
  101. inbuf[3] = Rx5C348_BIT_PM;
  102. hour = (hour + 11) % 12 + 1;
  103. BIN_TO_BCD(hour);
  104. inbuf[3] |= hour;
  105. }
  106. inbuf[4] = 0; /* ignore week */
  107. BIN_TO_BCD(day);
  108. inbuf[5] = day;
  109. BIN_TO_BCD(month);
  110. inbuf[6] = month;
  111. if (century >= 20)
  112. inbuf[6] |= Rx5C348_BIT_Y2K;
  113. BIN_TO_BCD(year);
  114. inbuf[7] = year;
  115. /* write in one transfer to avoid data inconsistency */
  116. return spi_rtc_io(inbuf, NULL, 8);
  117. }
  118. static unsigned long
  119. rtc_rx5c348_get_time(void)
  120. {
  121. unsigned char inbuf[8], outbuf[8];
  122. unsigned int year, month, day, hour, minute, second;
  123. inbuf[0] = Rx5C348_CMD_MR(Rx5C348_REG_SECOND);
  124. memset(inbuf + 1, 0, 7);
  125. /* read in one transfer to avoid data inconsistency */
  126. if (spi_rtc_io(inbuf, outbuf, 8))
  127. return 0;
  128. second = outbuf[1];
  129. BCD_TO_BIN(second);
  130. minute = outbuf[2];
  131. BCD_TO_BIN(minute);
  132. if (srtc_24h) {
  133. hour = outbuf[3];
  134. BCD_TO_BIN(hour);
  135. } else {
  136. hour = outbuf[3] & ~Rx5C348_BIT_PM;
  137. BCD_TO_BIN(hour);
  138. hour %= 12;
  139. if (outbuf[3] & Rx5C348_BIT_PM)
  140. hour += 12;
  141. }
  142. day = outbuf[5];
  143. BCD_TO_BIN(day);
  144. month = outbuf[6] & ~Rx5C348_BIT_Y2K;
  145. BCD_TO_BIN(month);
  146. year = outbuf[7];
  147. BCD_TO_BIN(year);
  148. year += EPOCH;
  149. return mktime(year, month, day, hour, minute, second);
  150. }
  151. void __init
  152. rtc_rx5c348_init(int chipid)
  153. {
  154. unsigned char inbuf[2], outbuf[2];
  155. srtc_chipid = chipid;
  156. /* turn on RTC if it is not on */
  157. inbuf[0] = Rx5C348_CMD_R(Rx5C348_REG_CTL2);
  158. inbuf[1] = 0;
  159. spi_rtc_io(inbuf, outbuf, 2);
  160. if (outbuf[1] & Rx5C348_BIT_XSTP) {
  161. inbuf[0] = Rx5C348_CMD_W(Rx5C348_REG_CTL2);
  162. inbuf[1] = 0;
  163. spi_rtc_io(inbuf, NULL, 2);
  164. }
  165. inbuf[0] = Rx5C348_CMD_R(Rx5C348_REG_CTL1);
  166. inbuf[1] = 0;
  167. spi_rtc_io(inbuf, outbuf, 2);
  168. if (outbuf[1] & Rx5C348_BIT_24H)
  169. srtc_24h = 1;
  170. /* set the function pointers */
  171. rtc_mips_get_time = rtc_rx5c348_get_time;
  172. rtc_mips_set_time = rtc_rx5c348_set_time;
  173. }