ds1603.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * Dallas Semiconductors 1603 RTC driver
  3. *
  4. * Brian Murphy <brian@murphy.dk>
  5. *
  6. */
  7. #include <linux/kernel.h>
  8. #include <asm/lasat/lasat.h>
  9. #include <linux/delay.h>
  10. #include <asm/lasat/ds1603.h>
  11. #include <asm/time.h>
  12. #include "ds1603.h"
  13. #define READ_TIME_CMD 0x81
  14. #define SET_TIME_CMD 0x80
  15. #define TRIMMER_SET_CMD 0xC0
  16. #define TRIMMER_VALUE_MASK 0x38
  17. #define TRIMMER_SHIFT 3
  18. struct ds_defs *ds1603;
  19. /* HW specific register functions */
  20. static void rtc_reg_write(unsigned long val)
  21. {
  22. *ds1603->reg = val;
  23. }
  24. static unsigned long rtc_reg_read(void)
  25. {
  26. unsigned long tmp = *ds1603->reg;
  27. return tmp;
  28. }
  29. static unsigned long rtc_datareg_read(void)
  30. {
  31. unsigned long tmp = *ds1603->data_reg;
  32. return tmp;
  33. }
  34. static void rtc_nrst_high(void)
  35. {
  36. rtc_reg_write(rtc_reg_read() | ds1603->rst);
  37. }
  38. static void rtc_nrst_low(void)
  39. {
  40. rtc_reg_write(rtc_reg_read() & ~ds1603->rst);
  41. }
  42. static void rtc_cycle_clock(unsigned long data)
  43. {
  44. data |= ds1603->clk;
  45. rtc_reg_write(data);
  46. lasat_ndelay(250);
  47. if (ds1603->data_reversed)
  48. data &= ~ds1603->data;
  49. else
  50. data |= ds1603->data;
  51. data &= ~ds1603->clk;
  52. rtc_reg_write(data);
  53. lasat_ndelay(250 + ds1603->huge_delay);
  54. }
  55. static void rtc_write_databit(unsigned int bit)
  56. {
  57. unsigned long data = rtc_reg_read();
  58. if (ds1603->data_reversed)
  59. bit = !bit;
  60. if (bit)
  61. data |= ds1603->data;
  62. else
  63. data &= ~ds1603->data;
  64. rtc_reg_write(data);
  65. lasat_ndelay(50 + ds1603->huge_delay);
  66. rtc_cycle_clock(data);
  67. }
  68. static unsigned int rtc_read_databit(void)
  69. {
  70. unsigned int data;
  71. data = (rtc_datareg_read() & (1 << ds1603->data_read_shift))
  72. >> ds1603->data_read_shift;
  73. rtc_cycle_clock(rtc_reg_read());
  74. return data;
  75. }
  76. static void rtc_write_byte(unsigned int byte)
  77. {
  78. int i;
  79. for (i = 0; i <= 7; i++) {
  80. rtc_write_databit(byte & 1L);
  81. byte >>= 1;
  82. }
  83. }
  84. static void rtc_write_word(unsigned long word)
  85. {
  86. int i;
  87. for (i = 0; i <= 31; i++) {
  88. rtc_write_databit(word & 1L);
  89. word >>= 1;
  90. }
  91. }
  92. static unsigned long rtc_read_word(void)
  93. {
  94. int i;
  95. unsigned long word = 0;
  96. unsigned long shift = 0;
  97. for (i = 0; i <= 31; i++) {
  98. word |= rtc_read_databit() << shift;
  99. shift++;
  100. }
  101. return word;
  102. }
  103. static void rtc_init_op(void)
  104. {
  105. rtc_nrst_high();
  106. rtc_reg_write(rtc_reg_read() & ~ds1603->clk);
  107. lasat_ndelay(50);
  108. }
  109. static void rtc_end_op(void)
  110. {
  111. rtc_nrst_low();
  112. lasat_ndelay(1000);
  113. }
  114. void read_persistent_clock(struct timespec *ts)
  115. {
  116. unsigned long word;
  117. unsigned long flags;
  118. spin_lock_irqsave(&rtc_lock, flags);
  119. rtc_init_op();
  120. rtc_write_byte(READ_TIME_CMD);
  121. word = rtc_read_word();
  122. rtc_end_op();
  123. spin_unlock_irqrestore(&rtc_lock, flags);
  124. ts->tv_sec = word;
  125. ts->tv_nsec = 0;
  126. }
  127. int rtc_mips_set_mmss(unsigned long time)
  128. {
  129. unsigned long flags;
  130. spin_lock_irqsave(&rtc_lock, flags);
  131. rtc_init_op();
  132. rtc_write_byte(SET_TIME_CMD);
  133. rtc_write_word(time);
  134. rtc_end_op();
  135. spin_unlock_irqrestore(&rtc_lock, flags);
  136. return 0;
  137. }
  138. void ds1603_set_trimmer(unsigned int trimval)
  139. {
  140. rtc_init_op();
  141. rtc_write_byte(((trimval << TRIMMER_SHIFT) & TRIMMER_VALUE_MASK)
  142. | (TRIMMER_SET_CMD));
  143. rtc_end_op();
  144. }
  145. void ds1603_disable(void)
  146. {
  147. ds1603_set_trimmer(TRIMMER_DISABLE_RTC);
  148. }
  149. void ds1603_enable(void)
  150. {
  151. ds1603_set_trimmer(TRIMMER_DEFAULT);
  152. }