• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Dallas Semiconductors 1603 RTC driver
4  *
5  * Brian Murphy <brian@murphy.dk>
6  *
7  */
8 #include <linux/kernel.h>
9 #include <asm/lasat/lasat.h>
10 #include <linux/delay.h>
11 #include <asm/lasat/ds1603.h>
12 #include <asm/time.h>
13 
14 #include "ds1603.h"
15 
16 #define READ_TIME_CMD 0x81
17 #define SET_TIME_CMD 0x80
18 #define TRIMMER_SET_CMD 0xC0
19 #define TRIMMER_VALUE_MASK 0x38
20 #define TRIMMER_SHIFT 3
21 
22 struct ds_defs *ds1603;
23 
24 /* HW specific register functions */
rtc_reg_write(unsigned long val)25 static void rtc_reg_write(unsigned long val)
26 {
27 	*ds1603->reg = val;
28 }
29 
rtc_reg_read(void)30 static unsigned long rtc_reg_read(void)
31 {
32 	unsigned long tmp = *ds1603->reg;
33 	return tmp;
34 }
35 
rtc_datareg_read(void)36 static unsigned long rtc_datareg_read(void)
37 {
38 	unsigned long tmp = *ds1603->data_reg;
39 	return tmp;
40 }
41 
rtc_nrst_high(void)42 static void rtc_nrst_high(void)
43 {
44 	rtc_reg_write(rtc_reg_read() | ds1603->rst);
45 }
46 
rtc_nrst_low(void)47 static void rtc_nrst_low(void)
48 {
49 	rtc_reg_write(rtc_reg_read() & ~ds1603->rst);
50 }
51 
rtc_cycle_clock(unsigned long data)52 static void rtc_cycle_clock(unsigned long data)
53 {
54 	data |= ds1603->clk;
55 	rtc_reg_write(data);
56 	lasat_ndelay(250);
57 	if (ds1603->data_reversed)
58 		data &= ~ds1603->data;
59 	else
60 		data |= ds1603->data;
61 	data &= ~ds1603->clk;
62 	rtc_reg_write(data);
63 	lasat_ndelay(250 + ds1603->huge_delay);
64 }
65 
rtc_write_databit(unsigned int bit)66 static void rtc_write_databit(unsigned int bit)
67 {
68 	unsigned long data = rtc_reg_read();
69 	if (ds1603->data_reversed)
70 		bit = !bit;
71 	if (bit)
72 		data |= ds1603->data;
73 	else
74 		data &= ~ds1603->data;
75 
76 	rtc_reg_write(data);
77 	lasat_ndelay(50 + ds1603->huge_delay);
78 	rtc_cycle_clock(data);
79 }
80 
rtc_read_databit(void)81 static unsigned int rtc_read_databit(void)
82 {
83 	unsigned int data;
84 
85 	data = (rtc_datareg_read() & (1 << ds1603->data_read_shift))
86 		>> ds1603->data_read_shift;
87 	rtc_cycle_clock(rtc_reg_read());
88 	return data;
89 }
90 
rtc_write_byte(unsigned int byte)91 static void rtc_write_byte(unsigned int byte)
92 {
93 	int i;
94 
95 	for (i = 0; i <= 7; i++) {
96 		rtc_write_databit(byte & 1L);
97 		byte >>= 1;
98 	}
99 }
100 
rtc_write_word(unsigned long word)101 static void rtc_write_word(unsigned long word)
102 {
103 	int i;
104 
105 	for (i = 0; i <= 31; i++) {
106 		rtc_write_databit(word & 1L);
107 		word >>= 1;
108 	}
109 }
110 
rtc_read_word(void)111 static unsigned long rtc_read_word(void)
112 {
113 	int i;
114 	unsigned long word = 0;
115 	unsigned long shift = 0;
116 
117 	for (i = 0; i <= 31; i++) {
118 		word |= rtc_read_databit() << shift;
119 		shift++;
120 	}
121 	return word;
122 }
123 
rtc_init_op(void)124 static void rtc_init_op(void)
125 {
126 	rtc_nrst_high();
127 
128 	rtc_reg_write(rtc_reg_read() & ~ds1603->clk);
129 
130 	lasat_ndelay(50);
131 }
132 
rtc_end_op(void)133 static void rtc_end_op(void)
134 {
135 	rtc_nrst_low();
136 	lasat_ndelay(1000);
137 }
138 
read_persistent_clock64(struct timespec64 * ts)139 void read_persistent_clock64(struct timespec64 *ts)
140 {
141 	unsigned long word;
142 	unsigned long flags;
143 
144 	spin_lock_irqsave(&rtc_lock, flags);
145 	rtc_init_op();
146 	rtc_write_byte(READ_TIME_CMD);
147 	word = rtc_read_word();
148 	rtc_end_op();
149 	spin_unlock_irqrestore(&rtc_lock, flags);
150 
151 	ts->tv_sec = word;
152 	ts->tv_nsec = 0;
153 }
154 
update_persistent_clock64(struct timespec64 now)155 int update_persistent_clock64(struct timespec64 now)
156 {
157 	time64_t time = now.tv_sec;
158 	unsigned long flags;
159 
160 	spin_lock_irqsave(&rtc_lock, flags);
161 	rtc_init_op();
162 	rtc_write_byte(SET_TIME_CMD);
163 	/*
164 	 * Due to the hardware limitation, we cast to 'unsigned long' type,
165 	 * so it will overflow in year 2106 on 32-bit machine.
166 	 */
167 	rtc_write_word((unsigned long)time);
168 	rtc_end_op();
169 	spin_unlock_irqrestore(&rtc_lock, flags);
170 
171 	return 0;
172 }
173 
ds1603_set_trimmer(unsigned int trimval)174 void ds1603_set_trimmer(unsigned int trimval)
175 {
176 	rtc_init_op();
177 	rtc_write_byte(((trimval << TRIMMER_SHIFT) & TRIMMER_VALUE_MASK)
178 			| (TRIMMER_SET_CMD));
179 	rtc_end_op();
180 }
181 
ds1603_disable(void)182 void ds1603_disable(void)
183 {
184 	ds1603_set_trimmer(TRIMMER_DISABLE_RTC);
185 }
186 
ds1603_enable(void)187 void ds1603_enable(void)
188 {
189 	ds1603_set_trimmer(TRIMMER_DEFAULT);
190 }
191