rtc_ds1742.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * Copyright 2001 MontaVista Software Inc.
  3. * Author: MontaVista Software, Inc.
  4. * ahennessy@mvista.com
  5. *
  6. * arch/mips/jmr3927/common/rtc_ds1742.c
  7. * Based on arch/mips/ddb5xxx/common/rtc_ds1386.c
  8. * low-level RTC hookups for s for Dallas 1742 chip.
  9. *
  10. * Copyright (C) 2000-2001 Toshiba Corporation
  11. *
  12. * This program is free software; you can redistribute it and/or modify it
  13. * under the terms of the GNU General Public License as published by the
  14. * Free Software Foundation; either version 2 of the License, or (at your
  15. * option) any later version.
  16. *
  17. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  18. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  19. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
  20. * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  21. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  22. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  23. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  24. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. *
  28. * You should have received a copy of the GNU General Public License along
  29. * with this program; if not, write to the Free Software Foundation, Inc.,
  30. * 675 Mass Ave, Cambridge, MA 02139, USA.
  31. */
  32. /*
  33. * This file exports a function, rtc_ds1386_init(), which expects an
  34. * uncached base address as the argument. It will set the two function
  35. * pointers expected by the MIPS generic timer code.
  36. */
  37. #include <linux/bcd.h>
  38. #include <linux/types.h>
  39. #include <linux/time.h>
  40. #include <linux/rtc.h>
  41. #include <linux/ds1742rtc.h>
  42. #include <asm/time.h>
  43. #include <asm/addrspace.h>
  44. #include <asm/debug.h>
  45. #define EPOCH 2000
  46. static unsigned long rtc_base;
  47. static unsigned long
  48. rtc_ds1742_get_time(void)
  49. {
  50. unsigned int year, month, day, hour, minute, second;
  51. unsigned int century;
  52. unsigned long flags;
  53. spin_lock_irqsave(&rtc_lock, flags);
  54. CMOS_WRITE(RTC_READ, RTC_CONTROL);
  55. second = BCD2BIN(CMOS_READ(RTC_SECONDS) & RTC_SECONDS_MASK);
  56. minute = BCD2BIN(CMOS_READ(RTC_MINUTES));
  57. hour = BCD2BIN(CMOS_READ(RTC_HOURS));
  58. day = BCD2BIN(CMOS_READ(RTC_DATE));
  59. month = BCD2BIN(CMOS_READ(RTC_MONTH));
  60. year = BCD2BIN(CMOS_READ(RTC_YEAR));
  61. century = BCD2BIN(CMOS_READ(RTC_CENTURY) & RTC_CENTURY_MASK);
  62. CMOS_WRITE(0, RTC_CONTROL);
  63. spin_unlock_irqrestore(&rtc_lock, flags);
  64. year += century * 100;
  65. return mktime(year, month, day, hour, minute, second);
  66. }
  67. extern void to_tm(unsigned long tim, struct rtc_time * tm);
  68. static int
  69. rtc_ds1742_set_time(unsigned long t)
  70. {
  71. struct rtc_time tm;
  72. u8 year, month, day, hour, minute, second;
  73. u8 cmos_year, cmos_month, cmos_day, cmos_hour, cmos_minute, cmos_second;
  74. int cmos_century;
  75. unsigned long flags;
  76. spin_lock_irqsave(&rtc_lock, flags);
  77. CMOS_WRITE(RTC_READ, RTC_CONTROL);
  78. cmos_second = (u8)(CMOS_READ(RTC_SECONDS) & RTC_SECONDS_MASK);
  79. cmos_minute = (u8)CMOS_READ(RTC_MINUTES);
  80. cmos_hour = (u8)CMOS_READ(RTC_HOURS);
  81. cmos_day = (u8)CMOS_READ(RTC_DATE);
  82. cmos_month = (u8)CMOS_READ(RTC_MONTH);
  83. cmos_year = (u8)CMOS_READ(RTC_YEAR);
  84. cmos_century = CMOS_READ(RTC_CENTURY) & RTC_CENTURY_MASK;
  85. CMOS_WRITE(RTC_WRITE, RTC_CONTROL);
  86. /* convert */
  87. to_tm(t, &tm);
  88. /* check each field one by one */
  89. year = BIN2BCD(tm.tm_year - EPOCH);
  90. if (year != cmos_year) {
  91. CMOS_WRITE(year,RTC_YEAR);
  92. }
  93. month = BIN2BCD(tm.tm_mon);
  94. if (month != (cmos_month & 0x1f)) {
  95. CMOS_WRITE((month & 0x1f) | (cmos_month & ~0x1f),RTC_MONTH);
  96. }
  97. day = BIN2BCD(tm.tm_mday);
  98. if (day != cmos_day) {
  99. CMOS_WRITE(day, RTC_DATE);
  100. }
  101. if (cmos_hour & 0x40) {
  102. /* 12 hour format */
  103. hour = 0x40;
  104. if (tm.tm_hour > 12) {
  105. hour |= 0x20 | (BIN2BCD(hour-12) & 0x1f);
  106. } else {
  107. hour |= BIN2BCD(tm.tm_hour);
  108. }
  109. } else {
  110. /* 24 hour format */
  111. hour = BIN2BCD(tm.tm_hour) & 0x3f;
  112. }
  113. if (hour != cmos_hour) CMOS_WRITE(hour, RTC_HOURS);
  114. minute = BIN2BCD(tm.tm_min);
  115. if (minute != cmos_minute) {
  116. CMOS_WRITE(minute, RTC_MINUTES);
  117. }
  118. second = BIN2BCD(tm.tm_sec);
  119. if (second != cmos_second) {
  120. CMOS_WRITE(second & RTC_SECONDS_MASK,RTC_SECONDS);
  121. }
  122. /* RTC_CENTURY and RTC_CONTROL share same address... */
  123. CMOS_WRITE(cmos_century, RTC_CONTROL);
  124. spin_unlock_irqrestore(&rtc_lock, flags);
  125. return 0;
  126. }
  127. void
  128. rtc_ds1742_init(unsigned long base)
  129. {
  130. u8 cmos_second;
  131. /* remember the base */
  132. rtc_base = base;
  133. db_assert((rtc_base & 0xe0000000) == KSEG1);
  134. /* set the function pointers */
  135. rtc_get_time = rtc_ds1742_get_time;
  136. rtc_set_time = rtc_ds1742_set_time;
  137. /* clear oscillator stop bit */
  138. CMOS_WRITE(RTC_READ, RTC_CONTROL);
  139. cmos_second = (u8)(CMOS_READ(RTC_SECONDS) & RTC_SECONDS_MASK);
  140. CMOS_WRITE(RTC_WRITE, RTC_CONTROL);
  141. CMOS_WRITE(cmos_second, RTC_SECONDS); /* clear msb */
  142. CMOS_WRITE(0, RTC_CONTROL);
  143. }