blob: ec6313d153599d5f745550760b5ad1990d7e7b3c [file] [log] [blame]
Atsushi Nemoto0e149232009-01-06 14:42:22 -08001/*
2 * TX4939 internal RTC driver
3 * Based on RBTX49xx patch from CELF patch archive.
4 *
5 * This file is subject to the terms and conditions of the GNU General Public
6 * License. See the file "COPYING" in the main directory of this archive
7 * for more details.
8 *
9 * (C) Copyright TOSHIBA CORPORATION 2005-2007
10 */
11#include <linux/rtc.h>
12#include <linux/platform_device.h>
13#include <linux/interrupt.h>
14#include <linux/io.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/gfp.h>
Atsushi Nemoto0e149232009-01-06 14:42:22 -080016#include <asm/txx9/tx4939.h>
17
18struct tx4939rtc_plat_data {
19 struct rtc_device *rtc;
20 struct tx4939_rtc_reg __iomem *rtcreg;
Atsushi Nemotoaf69a182009-12-15 16:46:00 -080021 spinlock_t lock;
Atsushi Nemoto0e149232009-01-06 14:42:22 -080022};
23
24static struct tx4939rtc_plat_data *get_tx4939rtc_plat_data(struct device *dev)
25{
26 return platform_get_drvdata(to_platform_device(dev));
27}
28
29static int tx4939_rtc_cmd(struct tx4939_rtc_reg __iomem *rtcreg, int cmd)
30{
31 int i = 0;
32
33 __raw_writel(cmd, &rtcreg->ctl);
34 /* This might take 30us (next 32.768KHz clock) */
35 while (__raw_readl(&rtcreg->ctl) & TX4939_RTCCTL_BUSY) {
36 /* timeout on approx. 100us (@ GBUS200MHz) */
37 if (i++ > 200 * 100)
38 return -EBUSY;
39 cpu_relax();
40 }
41 return 0;
42}
43
44static int tx4939_rtc_set_mmss(struct device *dev, unsigned long secs)
45{
46 struct tx4939rtc_plat_data *pdata = get_tx4939rtc_plat_data(dev);
47 struct tx4939_rtc_reg __iomem *rtcreg = pdata->rtcreg;
48 int i, ret;
49 unsigned char buf[6];
50
51 buf[0] = 0;
52 buf[1] = 0;
53 buf[2] = secs;
54 buf[3] = secs >> 8;
55 buf[4] = secs >> 16;
56 buf[5] = secs >> 24;
Atsushi Nemotoaf69a182009-12-15 16:46:00 -080057 spin_lock_irq(&pdata->lock);
Atsushi Nemoto0e149232009-01-06 14:42:22 -080058 __raw_writel(0, &rtcreg->adr);
59 for (i = 0; i < 6; i++)
60 __raw_writel(buf[i], &rtcreg->dat);
61 ret = tx4939_rtc_cmd(rtcreg,
62 TX4939_RTCCTL_COMMAND_SETTIME |
63 (__raw_readl(&rtcreg->ctl) & TX4939_RTCCTL_ALME));
Atsushi Nemotoaf69a182009-12-15 16:46:00 -080064 spin_unlock_irq(&pdata->lock);
Atsushi Nemoto0e149232009-01-06 14:42:22 -080065 return ret;
66}
67
68static int tx4939_rtc_read_time(struct device *dev, struct rtc_time *tm)
69{
70 struct tx4939rtc_plat_data *pdata = get_tx4939rtc_plat_data(dev);
71 struct tx4939_rtc_reg __iomem *rtcreg = pdata->rtcreg;
72 int i, ret;
73 unsigned long sec;
74 unsigned char buf[6];
75
Atsushi Nemotoaf69a182009-12-15 16:46:00 -080076 spin_lock_irq(&pdata->lock);
Atsushi Nemoto0e149232009-01-06 14:42:22 -080077 ret = tx4939_rtc_cmd(rtcreg,
78 TX4939_RTCCTL_COMMAND_GETTIME |
79 (__raw_readl(&rtcreg->ctl) & TX4939_RTCCTL_ALME));
80 if (ret) {
Atsushi Nemotoaf69a182009-12-15 16:46:00 -080081 spin_unlock_irq(&pdata->lock);
Atsushi Nemoto0e149232009-01-06 14:42:22 -080082 return ret;
83 }
84 __raw_writel(2, &rtcreg->adr);
85 for (i = 2; i < 6; i++)
86 buf[i] = __raw_readl(&rtcreg->dat);
Atsushi Nemotoaf69a182009-12-15 16:46:00 -080087 spin_unlock_irq(&pdata->lock);
Atsushi Nemoto0e149232009-01-06 14:42:22 -080088 sec = (buf[5] << 24) | (buf[4] << 16) | (buf[3] << 8) | buf[2];
89 rtc_time_to_tm(sec, tm);
90 return rtc_valid_tm(tm);
91}
92
93static int tx4939_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
94{
95 struct tx4939rtc_plat_data *pdata = get_tx4939rtc_plat_data(dev);
96 struct tx4939_rtc_reg __iomem *rtcreg = pdata->rtcreg;
97 int i, ret;
98 unsigned long sec;
99 unsigned char buf[6];
100
101 if (alrm->time.tm_sec < 0 ||
102 alrm->time.tm_min < 0 ||
103 alrm->time.tm_hour < 0 ||
104 alrm->time.tm_mday < 0 ||
105 alrm->time.tm_mon < 0 ||
106 alrm->time.tm_year < 0)
107 return -EINVAL;
108 rtc_tm_to_time(&alrm->time, &sec);
109 buf[0] = 0;
110 buf[1] = 0;
111 buf[2] = sec;
112 buf[3] = sec >> 8;
113 buf[4] = sec >> 16;
114 buf[5] = sec >> 24;
Atsushi Nemotoaf69a182009-12-15 16:46:00 -0800115 spin_lock_irq(&pdata->lock);
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800116 __raw_writel(0, &rtcreg->adr);
117 for (i = 0; i < 6; i++)
118 __raw_writel(buf[i], &rtcreg->dat);
119 ret = tx4939_rtc_cmd(rtcreg, TX4939_RTCCTL_COMMAND_SETALARM |
120 (alrm->enabled ? TX4939_RTCCTL_ALME : 0));
Atsushi Nemotoaf69a182009-12-15 16:46:00 -0800121 spin_unlock_irq(&pdata->lock);
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800122 return ret;
123}
124
125static int tx4939_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
126{
127 struct tx4939rtc_plat_data *pdata = get_tx4939rtc_plat_data(dev);
128 struct tx4939_rtc_reg __iomem *rtcreg = pdata->rtcreg;
129 int i, ret;
130 unsigned long sec;
131 unsigned char buf[6];
132 u32 ctl;
133
Atsushi Nemotoaf69a182009-12-15 16:46:00 -0800134 spin_lock_irq(&pdata->lock);
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800135 ret = tx4939_rtc_cmd(rtcreg,
136 TX4939_RTCCTL_COMMAND_GETALARM |
137 (__raw_readl(&rtcreg->ctl) & TX4939_RTCCTL_ALME));
138 if (ret) {
Atsushi Nemotoaf69a182009-12-15 16:46:00 -0800139 spin_unlock_irq(&pdata->lock);
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800140 return ret;
141 }
142 __raw_writel(2, &rtcreg->adr);
143 for (i = 2; i < 6; i++)
144 buf[i] = __raw_readl(&rtcreg->dat);
145 ctl = __raw_readl(&rtcreg->ctl);
146 alrm->enabled = (ctl & TX4939_RTCCTL_ALME) ? 1 : 0;
147 alrm->pending = (ctl & TX4939_RTCCTL_ALMD) ? 1 : 0;
Atsushi Nemotoaf69a182009-12-15 16:46:00 -0800148 spin_unlock_irq(&pdata->lock);
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800149 sec = (buf[5] << 24) | (buf[4] << 16) | (buf[3] << 8) | buf[2];
150 rtc_time_to_tm(sec, &alrm->time);
151 return rtc_valid_tm(&alrm->time);
152}
153
154static int tx4939_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
155{
156 struct tx4939rtc_plat_data *pdata = get_tx4939rtc_plat_data(dev);
157
Atsushi Nemotoaf69a182009-12-15 16:46:00 -0800158 spin_lock_irq(&pdata->lock);
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800159 tx4939_rtc_cmd(pdata->rtcreg,
160 TX4939_RTCCTL_COMMAND_NOP |
161 (enabled ? TX4939_RTCCTL_ALME : 0));
Atsushi Nemotoaf69a182009-12-15 16:46:00 -0800162 spin_unlock_irq(&pdata->lock);
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800163 return 0;
164}
165
166static irqreturn_t tx4939_rtc_interrupt(int irq, void *dev_id)
167{
168 struct tx4939rtc_plat_data *pdata = get_tx4939rtc_plat_data(dev_id);
169 struct tx4939_rtc_reg __iomem *rtcreg = pdata->rtcreg;
170 unsigned long events = RTC_IRQF;
171
Atsushi Nemotoaf69a182009-12-15 16:46:00 -0800172 spin_lock(&pdata->lock);
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800173 if (__raw_readl(&rtcreg->ctl) & TX4939_RTCCTL_ALMD) {
174 events |= RTC_AF;
175 tx4939_rtc_cmd(rtcreg, TX4939_RTCCTL_COMMAND_NOP);
176 }
Atsushi Nemotoaf69a182009-12-15 16:46:00 -0800177 spin_unlock(&pdata->lock);
178 if (likely(pdata->rtc))
179 rtc_update_irq(pdata->rtc, 1, events);
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800180 return IRQ_HANDLED;
181}
182
183static const struct rtc_class_ops tx4939_rtc_ops = {
184 .read_time = tx4939_rtc_read_time,
185 .read_alarm = tx4939_rtc_read_alarm,
186 .set_alarm = tx4939_rtc_set_alarm,
187 .set_mmss = tx4939_rtc_set_mmss,
188 .alarm_irq_enable = tx4939_rtc_alarm_irq_enable,
189};
190
Chris Wright2c3c8be2010-05-12 18:28:57 -0700191static ssize_t tx4939_rtc_nvram_read(struct file *filp, struct kobject *kobj,
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800192 struct bin_attribute *bin_attr,
193 char *buf, loff_t pos, size_t size)
194{
195 struct device *dev = container_of(kobj, struct device, kobj);
196 struct tx4939rtc_plat_data *pdata = get_tx4939rtc_plat_data(dev);
197 struct tx4939_rtc_reg __iomem *rtcreg = pdata->rtcreg;
198 ssize_t count;
199
Atsushi Nemotoaf69a182009-12-15 16:46:00 -0800200 spin_lock_irq(&pdata->lock);
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800201 for (count = 0; size > 0 && pos < TX4939_RTC_REG_RAMSIZE;
202 count++, size--) {
203 __raw_writel(pos++, &rtcreg->adr);
204 *buf++ = __raw_readl(&rtcreg->dat);
205 }
Atsushi Nemotoaf69a182009-12-15 16:46:00 -0800206 spin_unlock_irq(&pdata->lock);
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800207 return count;
208}
209
Chris Wright2c3c8be2010-05-12 18:28:57 -0700210static ssize_t tx4939_rtc_nvram_write(struct file *filp, struct kobject *kobj,
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800211 struct bin_attribute *bin_attr,
212 char *buf, loff_t pos, size_t size)
213{
214 struct device *dev = container_of(kobj, struct device, kobj);
215 struct tx4939rtc_plat_data *pdata = get_tx4939rtc_plat_data(dev);
216 struct tx4939_rtc_reg __iomem *rtcreg = pdata->rtcreg;
217 ssize_t count;
218
Atsushi Nemotoaf69a182009-12-15 16:46:00 -0800219 spin_lock_irq(&pdata->lock);
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800220 for (count = 0; size > 0 && pos < TX4939_RTC_REG_RAMSIZE;
221 count++, size--) {
222 __raw_writel(pos++, &rtcreg->adr);
223 __raw_writel(*buf++, &rtcreg->dat);
224 }
Atsushi Nemotoaf69a182009-12-15 16:46:00 -0800225 spin_unlock_irq(&pdata->lock);
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800226 return count;
227}
228
229static struct bin_attribute tx4939_rtc_nvram_attr = {
230 .attr = {
231 .name = "nvram",
232 .mode = S_IRUGO | S_IWUSR,
233 },
234 .size = TX4939_RTC_REG_RAMSIZE,
235 .read = tx4939_rtc_nvram_read,
236 .write = tx4939_rtc_nvram_write,
237};
238
239static int __init tx4939_rtc_probe(struct platform_device *pdev)
240{
241 struct rtc_device *rtc;
242 struct tx4939rtc_plat_data *pdata;
243 struct resource *res;
244 int irq, ret;
245
246 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
247 if (!res)
248 return -ENODEV;
249 irq = platform_get_irq(pdev, 0);
250 if (irq < 0)
251 return -ENODEV;
252 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
253 if (!pdata)
254 return -ENOMEM;
255 platform_set_drvdata(pdev, pdata);
256
257 if (!devm_request_mem_region(&pdev->dev, res->start,
258 resource_size(res), pdev->name))
259 return -EBUSY;
260 pdata->rtcreg = devm_ioremap(&pdev->dev, res->start,
261 resource_size(res));
262 if (!pdata->rtcreg)
263 return -EBUSY;
264
Atsushi Nemotoaf69a182009-12-15 16:46:00 -0800265 spin_lock_init(&pdata->lock);
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800266 tx4939_rtc_cmd(pdata->rtcreg, TX4939_RTCCTL_COMMAND_NOP);
267 if (devm_request_irq(&pdev->dev, irq, tx4939_rtc_interrupt,
Atsushi Nemoto0a817f72009-06-17 16:26:12 -0700268 IRQF_DISABLED, pdev->name, &pdev->dev) < 0)
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800269 return -EBUSY;
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800270 rtc = rtc_device_register(pdev->name, &pdev->dev,
271 &tx4939_rtc_ops, THIS_MODULE);
272 if (IS_ERR(rtc))
273 return PTR_ERR(rtc);
274 pdata->rtc = rtc;
275 ret = sysfs_create_bin_file(&pdev->dev.kobj, &tx4939_rtc_nvram_attr);
276 if (ret)
277 rtc_device_unregister(rtc);
278 return ret;
279}
280
281static int __exit tx4939_rtc_remove(struct platform_device *pdev)
282{
283 struct tx4939rtc_plat_data *pdata = platform_get_drvdata(pdev);
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800284
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800285 sysfs_remove_bin_file(&pdev->dev.kobj, &tx4939_rtc_nvram_attr);
Atsushi Nemotoaf69a182009-12-15 16:46:00 -0800286 rtc_device_unregister(pdata->rtc);
287 spin_lock_irq(&pdata->lock);
288 tx4939_rtc_cmd(pdata->rtcreg, TX4939_RTCCTL_COMMAND_NOP);
289 spin_unlock_irq(&pdata->lock);
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800290 return 0;
291}
292
293static struct platform_driver tx4939_rtc_driver = {
294 .remove = __exit_p(tx4939_rtc_remove),
295 .driver = {
296 .name = "tx4939rtc",
297 .owner = THIS_MODULE,
298 },
299};
300
301static int __init tx4939rtc_init(void)
302{
303 return platform_driver_probe(&tx4939_rtc_driver, tx4939_rtc_probe);
304}
305
306static void __exit tx4939rtc_exit(void)
307{
308 platform_driver_unregister(&tx4939_rtc_driver);
309}
310
311module_init(tx4939rtc_init);
312module_exit(tx4939rtc_exit);
313
314MODULE_AUTHOR("Atsushi Nemoto <anemo@mba.ocn.ne.jp>");
315MODULE_DESCRIPTION("TX4939 internal RTC driver");
316MODULE_LICENSE("GPL");
317MODULE_ALIAS("platform:tx4939rtc");