blob: 560d9a5e02253fe3ef5cf159c71d94198498b45c [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>
Paul Gortmaker21138522011-05-27 09:57:25 -040014#include <linux/module.h>
Atsushi Nemoto0e149232009-01-06 14:42:22 -080015#include <linux/io.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/gfp.h>
Atsushi Nemoto0e149232009-01-06 14:42:22 -080017#include <asm/txx9/tx4939.h>
18
19struct tx4939rtc_plat_data {
20 struct rtc_device *rtc;
21 struct tx4939_rtc_reg __iomem *rtcreg;
Atsushi Nemotoaf69a182009-12-15 16:46:00 -080022 spinlock_t lock;
Atsushi Nemoto0e149232009-01-06 14:42:22 -080023};
24
25static struct tx4939rtc_plat_data *get_tx4939rtc_plat_data(struct device *dev)
26{
27 return platform_get_drvdata(to_platform_device(dev));
28}
29
30static int tx4939_rtc_cmd(struct tx4939_rtc_reg __iomem *rtcreg, int cmd)
31{
32 int i = 0;
33
34 __raw_writel(cmd, &rtcreg->ctl);
35 /* This might take 30us (next 32.768KHz clock) */
36 while (__raw_readl(&rtcreg->ctl) & TX4939_RTCCTL_BUSY) {
37 /* timeout on approx. 100us (@ GBUS200MHz) */
38 if (i++ > 200 * 100)
39 return -EBUSY;
40 cpu_relax();
41 }
42 return 0;
43}
44
45static int tx4939_rtc_set_mmss(struct device *dev, unsigned long secs)
46{
47 struct tx4939rtc_plat_data *pdata = get_tx4939rtc_plat_data(dev);
48 struct tx4939_rtc_reg __iomem *rtcreg = pdata->rtcreg;
49 int i, ret;
50 unsigned char buf[6];
51
52 buf[0] = 0;
53 buf[1] = 0;
54 buf[2] = secs;
55 buf[3] = secs >> 8;
56 buf[4] = secs >> 16;
57 buf[5] = secs >> 24;
Atsushi Nemotoaf69a182009-12-15 16:46:00 -080058 spin_lock_irq(&pdata->lock);
Atsushi Nemoto0e149232009-01-06 14:42:22 -080059 __raw_writel(0, &rtcreg->adr);
60 for (i = 0; i < 6; i++)
61 __raw_writel(buf[i], &rtcreg->dat);
62 ret = tx4939_rtc_cmd(rtcreg,
63 TX4939_RTCCTL_COMMAND_SETTIME |
64 (__raw_readl(&rtcreg->ctl) & TX4939_RTCCTL_ALME));
Atsushi Nemotoaf69a182009-12-15 16:46:00 -080065 spin_unlock_irq(&pdata->lock);
Atsushi Nemoto0e149232009-01-06 14:42:22 -080066 return ret;
67}
68
69static int tx4939_rtc_read_time(struct device *dev, struct rtc_time *tm)
70{
71 struct tx4939rtc_plat_data *pdata = get_tx4939rtc_plat_data(dev);
72 struct tx4939_rtc_reg __iomem *rtcreg = pdata->rtcreg;
73 int i, ret;
74 unsigned long sec;
75 unsigned char buf[6];
76
Atsushi Nemotoaf69a182009-12-15 16:46:00 -080077 spin_lock_irq(&pdata->lock);
Atsushi Nemoto0e149232009-01-06 14:42:22 -080078 ret = tx4939_rtc_cmd(rtcreg,
79 TX4939_RTCCTL_COMMAND_GETTIME |
80 (__raw_readl(&rtcreg->ctl) & TX4939_RTCCTL_ALME));
81 if (ret) {
Atsushi Nemotoaf69a182009-12-15 16:46:00 -080082 spin_unlock_irq(&pdata->lock);
Atsushi Nemoto0e149232009-01-06 14:42:22 -080083 return ret;
84 }
85 __raw_writel(2, &rtcreg->adr);
86 for (i = 2; i < 6; i++)
87 buf[i] = __raw_readl(&rtcreg->dat);
Atsushi Nemotoaf69a182009-12-15 16:46:00 -080088 spin_unlock_irq(&pdata->lock);
Atsushi Nemoto0e149232009-01-06 14:42:22 -080089 sec = (buf[5] << 24) | (buf[4] << 16) | (buf[3] << 8) | buf[2];
90 rtc_time_to_tm(sec, tm);
91 return rtc_valid_tm(tm);
92}
93
94static int tx4939_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
95{
96 struct tx4939rtc_plat_data *pdata = get_tx4939rtc_plat_data(dev);
97 struct tx4939_rtc_reg __iomem *rtcreg = pdata->rtcreg;
98 int i, ret;
99 unsigned long sec;
100 unsigned char buf[6];
101
102 if (alrm->time.tm_sec < 0 ||
103 alrm->time.tm_min < 0 ||
104 alrm->time.tm_hour < 0 ||
105 alrm->time.tm_mday < 0 ||
106 alrm->time.tm_mon < 0 ||
107 alrm->time.tm_year < 0)
108 return -EINVAL;
109 rtc_tm_to_time(&alrm->time, &sec);
110 buf[0] = 0;
111 buf[1] = 0;
112 buf[2] = sec;
113 buf[3] = sec >> 8;
114 buf[4] = sec >> 16;
115 buf[5] = sec >> 24;
Atsushi Nemotoaf69a182009-12-15 16:46:00 -0800116 spin_lock_irq(&pdata->lock);
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800117 __raw_writel(0, &rtcreg->adr);
118 for (i = 0; i < 6; i++)
119 __raw_writel(buf[i], &rtcreg->dat);
120 ret = tx4939_rtc_cmd(rtcreg, TX4939_RTCCTL_COMMAND_SETALARM |
121 (alrm->enabled ? TX4939_RTCCTL_ALME : 0));
Atsushi Nemotoaf69a182009-12-15 16:46:00 -0800122 spin_unlock_irq(&pdata->lock);
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800123 return ret;
124}
125
126static int tx4939_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
127{
128 struct tx4939rtc_plat_data *pdata = get_tx4939rtc_plat_data(dev);
129 struct tx4939_rtc_reg __iomem *rtcreg = pdata->rtcreg;
130 int i, ret;
131 unsigned long sec;
132 unsigned char buf[6];
133 u32 ctl;
134
Atsushi Nemotoaf69a182009-12-15 16:46:00 -0800135 spin_lock_irq(&pdata->lock);
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800136 ret = tx4939_rtc_cmd(rtcreg,
137 TX4939_RTCCTL_COMMAND_GETALARM |
138 (__raw_readl(&rtcreg->ctl) & TX4939_RTCCTL_ALME));
139 if (ret) {
Atsushi Nemotoaf69a182009-12-15 16:46:00 -0800140 spin_unlock_irq(&pdata->lock);
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800141 return ret;
142 }
143 __raw_writel(2, &rtcreg->adr);
144 for (i = 2; i < 6; i++)
145 buf[i] = __raw_readl(&rtcreg->dat);
146 ctl = __raw_readl(&rtcreg->ctl);
147 alrm->enabled = (ctl & TX4939_RTCCTL_ALME) ? 1 : 0;
148 alrm->pending = (ctl & TX4939_RTCCTL_ALMD) ? 1 : 0;
Atsushi Nemotoaf69a182009-12-15 16:46:00 -0800149 spin_unlock_irq(&pdata->lock);
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800150 sec = (buf[5] << 24) | (buf[4] << 16) | (buf[3] << 8) | buf[2];
151 rtc_time_to_tm(sec, &alrm->time);
152 return rtc_valid_tm(&alrm->time);
153}
154
155static int tx4939_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
156{
157 struct tx4939rtc_plat_data *pdata = get_tx4939rtc_plat_data(dev);
158
Atsushi Nemotoaf69a182009-12-15 16:46:00 -0800159 spin_lock_irq(&pdata->lock);
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800160 tx4939_rtc_cmd(pdata->rtcreg,
161 TX4939_RTCCTL_COMMAND_NOP |
162 (enabled ? TX4939_RTCCTL_ALME : 0));
Atsushi Nemotoaf69a182009-12-15 16:46:00 -0800163 spin_unlock_irq(&pdata->lock);
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800164 return 0;
165}
166
167static irqreturn_t tx4939_rtc_interrupt(int irq, void *dev_id)
168{
169 struct tx4939rtc_plat_data *pdata = get_tx4939rtc_plat_data(dev_id);
170 struct tx4939_rtc_reg __iomem *rtcreg = pdata->rtcreg;
171 unsigned long events = RTC_IRQF;
172
Atsushi Nemotoaf69a182009-12-15 16:46:00 -0800173 spin_lock(&pdata->lock);
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800174 if (__raw_readl(&rtcreg->ctl) & TX4939_RTCCTL_ALMD) {
175 events |= RTC_AF;
176 tx4939_rtc_cmd(rtcreg, TX4939_RTCCTL_COMMAND_NOP);
177 }
Atsushi Nemotoaf69a182009-12-15 16:46:00 -0800178 spin_unlock(&pdata->lock);
Alexander Shiyan0d719152014-04-03 14:50:20 -0700179 rtc_update_irq(pdata->rtc, 1, events);
180
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800181 return IRQ_HANDLED;
182}
183
184static const struct rtc_class_ops tx4939_rtc_ops = {
185 .read_time = tx4939_rtc_read_time,
186 .read_alarm = tx4939_rtc_read_alarm,
187 .set_alarm = tx4939_rtc_set_alarm,
188 .set_mmss = tx4939_rtc_set_mmss,
189 .alarm_irq_enable = tx4939_rtc_alarm_irq_enable,
190};
191
Chris Wright2c3c8be2010-05-12 18:28:57 -0700192static ssize_t tx4939_rtc_nvram_read(struct file *filp, struct kobject *kobj,
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800193 struct bin_attribute *bin_attr,
194 char *buf, loff_t pos, size_t size)
195{
196 struct device *dev = container_of(kobj, struct device, kobj);
197 struct tx4939rtc_plat_data *pdata = get_tx4939rtc_plat_data(dev);
198 struct tx4939_rtc_reg __iomem *rtcreg = pdata->rtcreg;
199 ssize_t count;
200
Atsushi Nemotoaf69a182009-12-15 16:46:00 -0800201 spin_lock_irq(&pdata->lock);
Vladimir Zapolskiy824625d2015-07-27 00:48:36 +0300202 for (count = 0; count < size; count++) {
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800203 __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);
Vladimir Zapolskiy824625d2015-07-27 00:48:36 +0300220 for (count = 0; count < size; count++) {
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800221 __raw_writel(pos++, &rtcreg->adr);
222 __raw_writel(*buf++, &rtcreg->dat);
223 }
Atsushi Nemotoaf69a182009-12-15 16:46:00 -0800224 spin_unlock_irq(&pdata->lock);
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800225 return count;
226}
227
228static struct bin_attribute tx4939_rtc_nvram_attr = {
229 .attr = {
230 .name = "nvram",
231 .mode = S_IRUGO | S_IWUSR,
232 },
233 .size = TX4939_RTC_REG_RAMSIZE,
234 .read = tx4939_rtc_nvram_read,
235 .write = tx4939_rtc_nvram_write,
236};
237
238static int __init tx4939_rtc_probe(struct platform_device *pdev)
239{
240 struct rtc_device *rtc;
241 struct tx4939rtc_plat_data *pdata;
242 struct resource *res;
243 int irq, ret;
244
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800245 irq = platform_get_irq(pdev, 0);
246 if (irq < 0)
247 return -ENODEV;
248 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
249 if (!pdata)
250 return -ENOMEM;
251 platform_set_drvdata(pdev, pdata);
252
Julia Lawall7c1d69e2013-09-11 14:24:27 -0700253 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
254 pdata->rtcreg = devm_ioremap_resource(&pdev->dev, res);
255 if (IS_ERR(pdata->rtcreg))
256 return PTR_ERR(pdata->rtcreg);
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800257
Atsushi Nemotoaf69a182009-12-15 16:46:00 -0800258 spin_lock_init(&pdata->lock);
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800259 tx4939_rtc_cmd(pdata->rtcreg, TX4939_RTCCTL_COMMAND_NOP);
260 if (devm_request_irq(&pdev->dev, irq, tx4939_rtc_interrupt,
Yong Zhang2f6e5f92012-03-23 15:02:34 -0700261 0, pdev->name, &pdev->dev) < 0)
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800262 return -EBUSY;
Jingoo Hane38fc302013-04-29 16:19:18 -0700263 rtc = devm_rtc_device_register(&pdev->dev, pdev->name,
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800264 &tx4939_rtc_ops, THIS_MODULE);
265 if (IS_ERR(rtc))
266 return PTR_ERR(rtc);
267 pdata->rtc = rtc;
268 ret = sysfs_create_bin_file(&pdev->dev.kobj, &tx4939_rtc_nvram_attr);
Jingoo Hane38fc302013-04-29 16:19:18 -0700269
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800270 return ret;
271}
272
273static int __exit tx4939_rtc_remove(struct platform_device *pdev)
274{
275 struct tx4939rtc_plat_data *pdata = platform_get_drvdata(pdev);
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800276
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800277 sysfs_remove_bin_file(&pdev->dev.kobj, &tx4939_rtc_nvram_attr);
Atsushi Nemotoaf69a182009-12-15 16:46:00 -0800278 spin_lock_irq(&pdata->lock);
279 tx4939_rtc_cmd(pdata->rtcreg, TX4939_RTCCTL_COMMAND_NOP);
280 spin_unlock_irq(&pdata->lock);
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800281 return 0;
282}
283
284static struct platform_driver tx4939_rtc_driver = {
285 .remove = __exit_p(tx4939_rtc_remove),
286 .driver = {
287 .name = "tx4939rtc",
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800288 },
289};
290
Jingoo Han80f1def2013-04-29 16:18:54 -0700291module_platform_driver_probe(tx4939_rtc_driver, tx4939_rtc_probe);
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800292
293MODULE_AUTHOR("Atsushi Nemoto <anemo@mba.ocn.ne.jp>");
294MODULE_DESCRIPTION("TX4939 internal RTC driver");
295MODULE_LICENSE("GPL");
296MODULE_ALIAS("platform:tx4939rtc");