blob: 532acf9b05d895f2a7c8762ce0ec9c98cfe620ff [file] [log] [blame]
Paul Mundt739d3402008-02-06 01:38:44 -08001/*
2 * Dallas DS1302 RTC Support
3 *
Alessandro Zummo2bfc3302009-08-20 12:31:49 +09004 * Copyright (C) 2002 David McCullough
5 * Copyright (C) 2003 - 2007 Paul Mundt
Paul Mundt739d3402008-02-06 01:38:44 -08006 *
7 * This file is subject to the terms and conditions of the GNU General Public
Alessandro Zummo2bfc3302009-08-20 12:31:49 +09008 * License version 2. See the file "COPYING" in the main directory of
Paul Mundt739d3402008-02-06 01:38:44 -08009 * this archive for more details.
10 */
Alessandro Zummo2bfc3302009-08-20 12:31:49 +090011
Paul Mundt739d3402008-02-06 01:38:44 -080012#include <linux/init.h>
13#include <linux/module.h>
14#include <linux/kernel.h>
15#include <linux/platform_device.h>
Paul Mundt739d3402008-02-06 01:38:44 -080016#include <linux/rtc.h>
Paul Mundt739d3402008-02-06 01:38:44 -080017#include <linux/io.h>
18#include <linux/bcd.h>
19#include <asm/rtc.h>
20
21#define DRV_NAME "rtc-ds1302"
Alessandro Zummo2bfc3302009-08-20 12:31:49 +090022#define DRV_VERSION "0.1.1"
Paul Mundt739d3402008-02-06 01:38:44 -080023
24#define RTC_CMD_READ 0x81 /* Read command */
25#define RTC_CMD_WRITE 0x80 /* Write command */
26
27#define RTC_ADDR_RAM0 0x20 /* Address of RAM0 */
28#define RTC_ADDR_TCR 0x08 /* Address of trickle charge register */
29#define RTC_ADDR_YEAR 0x06 /* Address of year register */
30#define RTC_ADDR_DAY 0x05 /* Address of day of week register */
31#define RTC_ADDR_MON 0x04 /* Address of month register */
32#define RTC_ADDR_DATE 0x03 /* Address of day of month register */
33#define RTC_ADDR_HOUR 0x02 /* Address of hour register */
34#define RTC_ADDR_MIN 0x01 /* Address of minute register */
35#define RTC_ADDR_SEC 0x00 /* Address of second register */
36
37#define RTC_RESET 0x1000
38#define RTC_IODATA 0x0800
39#define RTC_SCLK 0x0400
40
41#ifdef CONFIG_SH_SECUREEDGE5410
Paul Mundt7639a452008-10-20 13:02:48 +090042#include <mach/snapgear.h>
Paul Mundt739d3402008-02-06 01:38:44 -080043#define set_dp(x) SECUREEDGE_WRITE_IOPORT(x, 0x1c00)
44#define get_dp() SECUREEDGE_READ_IOPORT()
45#else
46#error "Add support for your platform"
47#endif
48
Paul Mundt739d3402008-02-06 01:38:44 -080049static void ds1302_sendbits(unsigned int val)
50{
51 int i;
52
53 for (i = 8; (i); i--, val >>= 1) {
54 set_dp((get_dp() & ~RTC_IODATA) | ((val & 0x1) ?
55 RTC_IODATA : 0));
56 set_dp(get_dp() | RTC_SCLK); /* clock high */
57 set_dp(get_dp() & ~RTC_SCLK); /* clock low */
58 }
59}
60
61static unsigned int ds1302_recvbits(void)
62{
63 unsigned int val;
64 int i;
65
66 for (i = 0, val = 0; (i < 8); i++) {
67 val |= (((get_dp() & RTC_IODATA) ? 1 : 0) << i);
68 set_dp(get_dp() | RTC_SCLK); /* clock high */
69 set_dp(get_dp() & ~RTC_SCLK); /* clock low */
70 }
71
72 return val;
73}
74
75static unsigned int ds1302_readbyte(unsigned int addr)
76{
77 unsigned int val;
78
79 set_dp(get_dp() & ~(RTC_RESET | RTC_IODATA | RTC_SCLK));
80
81 set_dp(get_dp() | RTC_RESET);
82 ds1302_sendbits(((addr & 0x3f) << 1) | RTC_CMD_READ);
83 val = ds1302_recvbits();
84 set_dp(get_dp() & ~RTC_RESET);
85
86 return val;
87}
88
89static void ds1302_writebyte(unsigned int addr, unsigned int val)
90{
91 set_dp(get_dp() & ~(RTC_RESET | RTC_IODATA | RTC_SCLK));
92 set_dp(get_dp() | RTC_RESET);
93 ds1302_sendbits(((addr & 0x3f) << 1) | RTC_CMD_WRITE);
94 ds1302_sendbits(val);
95 set_dp(get_dp() & ~RTC_RESET);
96}
97
98static int ds1302_rtc_read_time(struct device *dev, struct rtc_time *tm)
99{
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700100 tm->tm_sec = bcd2bin(ds1302_readbyte(RTC_ADDR_SEC));
101 tm->tm_min = bcd2bin(ds1302_readbyte(RTC_ADDR_MIN));
102 tm->tm_hour = bcd2bin(ds1302_readbyte(RTC_ADDR_HOUR));
103 tm->tm_wday = bcd2bin(ds1302_readbyte(RTC_ADDR_DAY));
104 tm->tm_mday = bcd2bin(ds1302_readbyte(RTC_ADDR_DATE));
105 tm->tm_mon = bcd2bin(ds1302_readbyte(RTC_ADDR_MON)) - 1;
106 tm->tm_year = bcd2bin(ds1302_readbyte(RTC_ADDR_YEAR));
Paul Mundt739d3402008-02-06 01:38:44 -0800107
108 if (tm->tm_year < 70)
109 tm->tm_year += 100;
110
Paul Mundt739d3402008-02-06 01:38:44 -0800111 dev_dbg(dev, "%s: tm is secs=%d, mins=%d, hours=%d, "
112 "mday=%d, mon=%d, year=%d, wday=%d\n",
Harvey Harrison2a4e2b82008-04-28 02:12:00 -0700113 __func__,
Paul Mundt739d3402008-02-06 01:38:44 -0800114 tm->tm_sec, tm->tm_min, tm->tm_hour,
115 tm->tm_mday, tm->tm_mon + 1, tm->tm_year, tm->tm_wday);
116
Alessandro Zummo2bfc3302009-08-20 12:31:49 +0900117 return rtc_valid_tm(tm);
Paul Mundt739d3402008-02-06 01:38:44 -0800118}
119
120static int ds1302_rtc_set_time(struct device *dev, struct rtc_time *tm)
121{
Paul Mundt739d3402008-02-06 01:38:44 -0800122 /* Stop RTC */
123 ds1302_writebyte(RTC_ADDR_SEC, ds1302_readbyte(RTC_ADDR_SEC) | 0x80);
124
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700125 ds1302_writebyte(RTC_ADDR_SEC, bin2bcd(tm->tm_sec));
126 ds1302_writebyte(RTC_ADDR_MIN, bin2bcd(tm->tm_min));
127 ds1302_writebyte(RTC_ADDR_HOUR, bin2bcd(tm->tm_hour));
128 ds1302_writebyte(RTC_ADDR_DAY, bin2bcd(tm->tm_wday));
129 ds1302_writebyte(RTC_ADDR_DATE, bin2bcd(tm->tm_mday));
130 ds1302_writebyte(RTC_ADDR_MON, bin2bcd(tm->tm_mon + 1));
131 ds1302_writebyte(RTC_ADDR_YEAR, bin2bcd(tm->tm_year % 100));
Paul Mundt739d3402008-02-06 01:38:44 -0800132
133 /* Start RTC */
134 ds1302_writebyte(RTC_ADDR_SEC, ds1302_readbyte(RTC_ADDR_SEC) & ~0x80);
135
Paul Mundt739d3402008-02-06 01:38:44 -0800136 return 0;
137}
138
139static int ds1302_rtc_ioctl(struct device *dev, unsigned int cmd,
140 unsigned long arg)
141{
142 switch (cmd) {
143#ifdef RTC_SET_CHARGE
144 case RTC_SET_CHARGE:
145 {
Paul Mundt739d3402008-02-06 01:38:44 -0800146 int tcs_val;
147
148 if (copy_from_user(&tcs_val, (int __user *)arg, sizeof(int)))
149 return -EFAULT;
150
Paul Mundt739d3402008-02-06 01:38:44 -0800151 ds1302_writebyte(RTC_ADDR_TCR, (0xa0 | tcs_val * 0xf));
Paul Mundt739d3402008-02-06 01:38:44 -0800152 return 0;
153 }
154#endif
155 }
156
157 return -ENOIOCTLCMD;
158}
159
160static struct rtc_class_ops ds1302_rtc_ops = {
161 .read_time = ds1302_rtc_read_time,
162 .set_time = ds1302_rtc_set_time,
163 .ioctl = ds1302_rtc_ioctl,
164};
165
Alessandro Zummo2bfc3302009-08-20 12:31:49 +0900166static int __init ds1302_rtc_probe(struct platform_device *pdev)
Paul Mundt739d3402008-02-06 01:38:44 -0800167{
Alessandro Zummo2bfc3302009-08-20 12:31:49 +0900168 struct rtc_device *rtc;
Paul Mundt739d3402008-02-06 01:38:44 -0800169
170 /* Reset */
171 set_dp(get_dp() & ~(RTC_RESET | RTC_IODATA | RTC_SCLK));
172
173 /* Write a magic value to the DS1302 RAM, and see if it sticks. */
174 ds1302_writebyte(RTC_ADDR_RAM0, 0x42);
175 if (ds1302_readbyte(RTC_ADDR_RAM0) != 0x42)
176 return -ENODEV;
177
Alessandro Zummo2bfc3302009-08-20 12:31:49 +0900178 rtc = rtc_device_register("ds1302", &pdev->dev,
Paul Mundt739d3402008-02-06 01:38:44 -0800179 &ds1302_rtc_ops, THIS_MODULE);
Alessandro Zummo2bfc3302009-08-20 12:31:49 +0900180 if (IS_ERR(rtc))
181 return PTR_ERR(rtc);
Paul Mundt739d3402008-02-06 01:38:44 -0800182
183 platform_set_drvdata(pdev, rtc);
184
185 return 0;
Paul Mundt739d3402008-02-06 01:38:44 -0800186}
187
188static int __devexit ds1302_rtc_remove(struct platform_device *pdev)
189{
Alessandro Zummo2bfc3302009-08-20 12:31:49 +0900190 struct rtc_device *rtc = platform_get_drvdata(pdev);
Paul Mundt739d3402008-02-06 01:38:44 -0800191
Alessandro Zummo2bfc3302009-08-20 12:31:49 +0900192 rtc_device_unregister(rtc);
Paul Mundt739d3402008-02-06 01:38:44 -0800193 platform_set_drvdata(pdev, NULL);
194
Paul Mundt739d3402008-02-06 01:38:44 -0800195 return 0;
196}
197
198static struct platform_driver ds1302_platform_driver = {
199 .driver = {
200 .name = DRV_NAME,
201 .owner = THIS_MODULE,
202 },
Uwe Kleine-Königb9e05c62009-11-24 22:07:23 +0100203 .remove = __devexit_p(ds1302_rtc_remove),
Paul Mundt739d3402008-02-06 01:38:44 -0800204};
205
206static int __init ds1302_rtc_init(void)
207{
Alessandro Zummo2bfc3302009-08-20 12:31:49 +0900208 return platform_driver_probe(&ds1302_platform_driver, ds1302_rtc_probe);
Paul Mundt739d3402008-02-06 01:38:44 -0800209}
210
211static void __exit ds1302_rtc_exit(void)
212{
213 platform_driver_unregister(&ds1302_platform_driver);
214}
215
216module_init(ds1302_rtc_init);
217module_exit(ds1302_rtc_exit);
218
219MODULE_DESCRIPTION("Dallas DS1302 RTC driver");
220MODULE_VERSION(DRV_VERSION);
221MODULE_AUTHOR("Paul Mundt, David McCullough");
222MODULE_LICENSE("GPL v2");