aboutsummaryrefslogtreecommitdiffstats
path: root/arch/mips/lasat/ds1603.c
diff options
context:
space:
mode:
Diffstat (limited to 'arch/mips/lasat/ds1603.c')
-rw-r--r--arch/mips/lasat/ds1603.c174
1 files changed, 174 insertions, 0 deletions
diff --git a/arch/mips/lasat/ds1603.c b/arch/mips/lasat/ds1603.c
new file mode 100644
index 000000000000..7bbf6cf923c9
--- /dev/null
+++ b/arch/mips/lasat/ds1603.c
@@ -0,0 +1,174 @@
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
12#include "ds1603.h"
13
14#define READ_TIME_CMD 0x81
15#define SET_TIME_CMD 0x80
16#define TRIMMER_SET_CMD 0xC0
17#define TRIMMER_VALUE_MASK 0x38
18#define TRIMMER_SHIFT 3
19
20struct ds_defs *ds1603 = NULL;
21
22/* HW specific register functions */
23static void rtc_reg_write(unsigned long val)
24{
25 *ds1603->reg = val;
26}
27
28static unsigned long rtc_reg_read(void)
29{
30 unsigned long tmp = *ds1603->reg;
31 return tmp;
32}
33
34static unsigned long rtc_datareg_read(void)
35{
36 unsigned long tmp = *ds1603->data_reg;
37 return tmp;
38}
39
40static void rtc_nrst_high(void)
41{
42 rtc_reg_write(rtc_reg_read() | ds1603->rst);
43}
44
45static void rtc_nrst_low(void)
46{
47 rtc_reg_write(rtc_reg_read() & ~ds1603->rst);
48}
49
50static void rtc_cycle_clock(unsigned long data)
51{
52 data |= ds1603->clk;
53 rtc_reg_write(data);
54 lasat_ndelay(250);
55 if (ds1603->data_reversed)
56 data &= ~ds1603->data;
57 else
58 data |= ds1603->data;
59 data &= ~ds1603->clk;
60 rtc_reg_write(data);
61 lasat_ndelay(250 + ds1603->huge_delay);
62}
63
64static void rtc_write_databit(unsigned int bit)
65{
66 unsigned long data = rtc_reg_read();
67 if (ds1603->data_reversed)
68 bit = !bit;
69 if (bit)
70 data |= ds1603->data;
71 else
72 data &= ~ds1603->data;
73
74 rtc_reg_write(data);
75 lasat_ndelay(50 + ds1603->huge_delay);
76 rtc_cycle_clock(data);
77}
78
79static unsigned int rtc_read_databit(void)
80{
81 unsigned int data;
82
83 data = (rtc_datareg_read() & (1 << ds1603->data_read_shift))
84 >> ds1603->data_read_shift;
85 rtc_cycle_clock(rtc_reg_read());
86 return data;
87}
88
89static void rtc_write_byte(unsigned int byte)
90{
91 int i;
92
93 for (i = 0; i<=7; i++) {
94 rtc_write_databit(byte & 1L);
95 byte >>= 1;
96 }
97}
98
99static void rtc_write_word(unsigned long word)
100{
101 int i;
102
103 for (i = 0; i<=31; i++) {
104 rtc_write_databit(word & 1L);
105 word >>= 1;
106 }
107}
108
109static unsigned long rtc_read_word(void)
110{
111 int i;
112 unsigned long word = 0;
113 unsigned long shift = 0;
114
115 for (i = 0; i<=31; i++) {
116 word |= rtc_read_databit() << shift;
117 shift++;
118 }
119 return word;
120}
121
122static void rtc_init_op(void)
123{
124 rtc_nrst_high();
125
126 rtc_reg_write(rtc_reg_read() & ~ds1603->clk);
127
128 lasat_ndelay(50);
129}
130
131static void rtc_end_op(void)
132{
133 rtc_nrst_low();
134 lasat_ndelay(1000);
135}
136
137/* interface */
138unsigned long ds1603_read(void)
139{
140 unsigned long word;
141 rtc_init_op();
142 rtc_write_byte(READ_TIME_CMD);
143 word = rtc_read_word();
144 rtc_end_op();
145 return word;
146}
147
148int ds1603_set(unsigned long time)
149{
150 rtc_init_op();
151 rtc_write_byte(SET_TIME_CMD);
152 rtc_write_word(time);
153 rtc_end_op();
154
155 return 0;
156}
157
158void ds1603_set_trimmer(unsigned int trimval)
159{
160 rtc_init_op();
161 rtc_write_byte(((trimval << TRIMMER_SHIFT) & TRIMMER_VALUE_MASK)
162 | (TRIMMER_SET_CMD));
163 rtc_end_op();
164}
165
166void ds1603_disable(void)
167{
168 ds1603_set_trimmer(TRIMMER_DISABLE_RTC);
169}
170
171void ds1603_enable(void)
172{
173 ds1603_set_trimmer(TRIMMER_DEFAULT);
174}