1
2
3
4
5
6
7
8
9
10
11#include <linux/init.h>
12#include <linux/module.h>
13#include <linux/kernel.h>
14#include <linux/platform_device.h>
15#include <linux/time.h>
16#include <linux/rtc.h>
17#include <linux/spinlock.h>
18#include <linux/io.h>
19#include <linux/bcd.h>
20#include <asm/rtc.h>
21
22#define DRV_NAME "rtc-ds1302"
23#define DRV_VERSION "0.1.0"
24
25#define RTC_CMD_READ 0x81
26#define RTC_CMD_WRITE 0x80
27
28#define RTC_ADDR_RAM0 0x20
29#define RTC_ADDR_TCR 0x08
30#define RTC_ADDR_YEAR 0x06
31#define RTC_ADDR_DAY 0x05
32#define RTC_ADDR_MON 0x04
33#define RTC_ADDR_DATE 0x03
34#define RTC_ADDR_HOUR 0x02
35#define RTC_ADDR_MIN 0x01
36#define RTC_ADDR_SEC 0x00
37
38#define RTC_RESET 0x1000
39#define RTC_IODATA 0x0800
40#define RTC_SCLK 0x0400
41
42#ifdef CONFIG_SH_SECUREEDGE5410
43#include <mach/snapgear.h>
44#define set_dp(x) SECUREEDGE_WRITE_IOPORT(x, 0x1c00)
45#define get_dp() SECUREEDGE_READ_IOPORT()
46#else
47#error "Add support for your platform"
48#endif
49
50struct ds1302_rtc {
51 struct rtc_device *rtc_dev;
52 spinlock_t lock;
53};
54
55static void ds1302_sendbits(unsigned int val)
56{
57 int i;
58
59 for (i = 8; (i); i--, val >>= 1) {
60 set_dp((get_dp() & ~RTC_IODATA) | ((val & 0x1) ?
61 RTC_IODATA : 0));
62 set_dp(get_dp() | RTC_SCLK);
63 set_dp(get_dp() & ~RTC_SCLK);
64 }
65}
66
67static unsigned int ds1302_recvbits(void)
68{
69 unsigned int val;
70 int i;
71
72 for (i = 0, val = 0; (i < 8); i++) {
73 val |= (((get_dp() & RTC_IODATA) ? 1 : 0) << i);
74 set_dp(get_dp() | RTC_SCLK);
75 set_dp(get_dp() & ~RTC_SCLK);
76 }
77
78 return val;
79}
80
81static unsigned int ds1302_readbyte(unsigned int addr)
82{
83 unsigned int val;
84
85 set_dp(get_dp() & ~(RTC_RESET | RTC_IODATA | RTC_SCLK));
86
87 set_dp(get_dp() | RTC_RESET);
88 ds1302_sendbits(((addr & 0x3f) << 1) | RTC_CMD_READ);
89 val = ds1302_recvbits();
90 set_dp(get_dp() & ~RTC_RESET);
91
92 return val;
93}
94
95static void ds1302_writebyte(unsigned int addr, unsigned int val)
96{
97 set_dp(get_dp() & ~(RTC_RESET | RTC_IODATA | RTC_SCLK));
98 set_dp(get_dp() | RTC_RESET);
99 ds1302_sendbits(((addr & 0x3f) << 1) | RTC_CMD_WRITE);
100 ds1302_sendbits(val);
101 set_dp(get_dp() & ~RTC_RESET);
102}
103
104static int ds1302_rtc_read_time(struct device *dev, struct rtc_time *tm)
105{
106 struct ds1302_rtc *rtc = dev_get_drvdata(dev);
107
108 spin_lock_irq(&rtc->lock);
109
110 tm->tm_sec = bcd2bin(ds1302_readbyte(RTC_ADDR_SEC));
111 tm->tm_min = bcd2bin(ds1302_readbyte(RTC_ADDR_MIN));
112 tm->tm_hour = bcd2bin(ds1302_readbyte(RTC_ADDR_HOUR));
113 tm->tm_wday = bcd2bin(ds1302_readbyte(RTC_ADDR_DAY));
114 tm->tm_mday = bcd2bin(ds1302_readbyte(RTC_ADDR_DATE));
115 tm->tm_mon = bcd2bin(ds1302_readbyte(RTC_ADDR_MON)) - 1;
116 tm->tm_year = bcd2bin(ds1302_readbyte(RTC_ADDR_YEAR));
117
118 if (tm->tm_year < 70)
119 tm->tm_year += 100;
120
121 spin_unlock_irq(&rtc->lock);
122
123 dev_dbg(dev, "%s: tm is secs=%d, mins=%d, hours=%d, "
124 "mday=%d, mon=%d, year=%d, wday=%d\n",
125 __func__,
126 tm->tm_sec, tm->tm_min, tm->tm_hour,
127 tm->tm_mday, tm->tm_mon + 1, tm->tm_year, tm->tm_wday);
128
129 if (rtc_valid_tm(tm) < 0)
130 dev_err(dev, "invalid date\n");
131
132 return 0;
133}
134
135static int ds1302_rtc_set_time(struct device *dev, struct rtc_time *tm)
136{
137 struct ds1302_rtc *rtc = dev_get_drvdata(dev);
138
139 spin_lock_irq(&rtc->lock);
140
141
142 ds1302_writebyte(RTC_ADDR_SEC, ds1302_readbyte(RTC_ADDR_SEC) | 0x80);
143
144 ds1302_writebyte(RTC_ADDR_SEC, bin2bcd(tm->tm_sec));
145 ds1302_writebyte(RTC_ADDR_MIN, bin2bcd(tm->tm_min));
146 ds1302_writebyte(RTC_ADDR_HOUR, bin2bcd(tm->tm_hour));
147 ds1302_writebyte(RTC_ADDR_DAY, bin2bcd(tm->tm_wday));
148 ds1302_writebyte(RTC_ADDR_DATE, bin2bcd(tm->tm_mday));
149 ds1302_writebyte(RTC_ADDR_MON, bin2bcd(tm->tm_mon + 1));
150 ds1302_writebyte(RTC_ADDR_YEAR, bin2bcd(tm->tm_year % 100));
151
152
153 ds1302_writebyte(RTC_ADDR_SEC, ds1302_readbyte(RTC_ADDR_SEC) & ~0x80);
154
155 spin_unlock_irq(&rtc->lock);
156
157 return 0;
158}
159
160static int ds1302_rtc_ioctl(struct device *dev, unsigned int cmd,
161 unsigned long arg)
162{
163 switch (cmd) {
164#ifdef RTC_SET_CHARGE
165 case RTC_SET_CHARGE:
166 {
167 struct ds1302_rtc *rtc = dev_get_drvdata(dev);
168 int tcs_val;
169
170 if (copy_from_user(&tcs_val, (int __user *)arg, sizeof(int)))
171 return -EFAULT;
172
173 spin_lock_irq(&rtc->lock);
174 ds1302_writebyte(RTC_ADDR_TCR, (0xa0 | tcs_val * 0xf));
175 spin_unlock_irq(&rtc->lock);
176 return 0;
177 }
178#endif
179 }
180
181 return -ENOIOCTLCMD;
182}
183
184static struct rtc_class_ops ds1302_rtc_ops = {
185 .read_time = ds1302_rtc_read_time,
186 .set_time = ds1302_rtc_set_time,
187 .ioctl = ds1302_rtc_ioctl,
188};
189
190static int __devinit ds1302_rtc_probe(struct platform_device *pdev)
191{
192 struct ds1302_rtc *rtc;
193 int ret;
194
195
196 set_dp(get_dp() & ~(RTC_RESET | RTC_IODATA | RTC_SCLK));
197
198
199 ds1302_writebyte(RTC_ADDR_RAM0, 0x42);
200 if (ds1302_readbyte(RTC_ADDR_RAM0) != 0x42)
201 return -ENODEV;
202
203 rtc = kzalloc(sizeof(struct ds1302_rtc), GFP_KERNEL);
204 if (unlikely(!rtc))
205 return -ENOMEM;
206
207 spin_lock_init(&rtc->lock);
208 rtc->rtc_dev = rtc_device_register("ds1302", &pdev->dev,
209 &ds1302_rtc_ops, THIS_MODULE);
210 if (IS_ERR(rtc->rtc_dev)) {
211 ret = PTR_ERR(rtc->rtc_dev);
212 goto out;
213 }
214
215 platform_set_drvdata(pdev, rtc);
216
217 return 0;
218out:
219 kfree(rtc);
220 return ret;
221}
222
223static int __devexit ds1302_rtc_remove(struct platform_device *pdev)
224{
225 struct ds1302_rtc *rtc = platform_get_drvdata(pdev);
226
227 if (likely(rtc->rtc_dev))
228 rtc_device_unregister(rtc->rtc_dev);
229
230 platform_set_drvdata(pdev, NULL);
231
232 kfree(rtc);
233
234 return 0;
235}
236
237static struct platform_driver ds1302_platform_driver = {
238 .driver = {
239 .name = DRV_NAME,
240 .owner = THIS_MODULE,
241 },
242 .probe = ds1302_rtc_probe,
243 .remove = __devexit_p(ds1302_rtc_remove),
244};
245
246static int __init ds1302_rtc_init(void)
247{
248 return platform_driver_register(&ds1302_platform_driver);
249}
250
251static void __exit ds1302_rtc_exit(void)
252{
253 platform_driver_unregister(&ds1302_platform_driver);
254}
255
256module_init(ds1302_rtc_init);
257module_exit(ds1302_rtc_exit);
258
259MODULE_DESCRIPTION("Dallas DS1302 RTC driver");
260MODULE_VERSION(DRV_VERSION);
261MODULE_AUTHOR("Paul Mundt, David McCullough");
262MODULE_LICENSE("GPL v2");
263