blob: 715b82981279d474b0dff874c37d999011b7402e [file] [log] [blame]
Alexandre Belloni1a9de212019-03-04 20:02:05 +01001// SPDX-License-Identifier: GPL-2.0
Atsushi Nemoto0e149232009-01-06 14:42:22 -08002/*
3 * TX4939 internal RTC driver
4 * Based on RBTX49xx patch from CELF patch archive.
5 *
Atsushi Nemoto0e149232009-01-06 14:42:22 -08006 * (C) Copyright TOSHIBA CORPORATION 2005-2007
7 */
8#include <linux/rtc.h>
9#include <linux/platform_device.h>
10#include <linux/interrupt.h>
Paul Gortmaker21138522011-05-27 09:57:25 -040011#include <linux/module.h>
Atsushi Nemoto0e149232009-01-06 14:42:22 -080012#include <linux/io.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/gfp.h>
Alexandre Bellonia0151062018-02-12 23:47:55 +010014
15#define TX4939_RTCCTL_ALME 0x00000080
16#define TX4939_RTCCTL_ALMD 0x00000040
17#define TX4939_RTCCTL_BUSY 0x00000020
18
19#define TX4939_RTCCTL_COMMAND 0x00000007
20#define TX4939_RTCCTL_COMMAND_NOP 0x00000000
21#define TX4939_RTCCTL_COMMAND_GETTIME 0x00000001
22#define TX4939_RTCCTL_COMMAND_SETTIME 0x00000002
23#define TX4939_RTCCTL_COMMAND_GETALARM 0x00000003
24#define TX4939_RTCCTL_COMMAND_SETALARM 0x00000004
25
26#define TX4939_RTCTBC_PM 0x00000080
27#define TX4939_RTCTBC_COMP 0x0000007f
28
29#define TX4939_RTC_REG_RAMSIZE 0x00000100
30#define TX4939_RTC_REG_RWBSIZE 0x00000006
31
32struct tx4939_rtc_reg {
33 __u32 ctl;
34 __u32 adr;
35 __u32 dat;
36 __u32 tbc;
37};
Atsushi Nemoto0e149232009-01-06 14:42:22 -080038
39struct tx4939rtc_plat_data {
40 struct rtc_device *rtc;
41 struct tx4939_rtc_reg __iomem *rtcreg;
Atsushi Nemotoaf69a182009-12-15 16:46:00 -080042 spinlock_t lock;
Atsushi Nemoto0e149232009-01-06 14:42:22 -080043};
44
Atsushi Nemoto0e149232009-01-06 14:42:22 -080045static int tx4939_rtc_cmd(struct tx4939_rtc_reg __iomem *rtcreg, int cmd)
46{
47 int i = 0;
48
49 __raw_writel(cmd, &rtcreg->ctl);
50 /* This might take 30us (next 32.768KHz clock) */
51 while (__raw_readl(&rtcreg->ctl) & TX4939_RTCCTL_BUSY) {
52 /* timeout on approx. 100us (@ GBUS200MHz) */
53 if (i++ > 200 * 100)
54 return -EBUSY;
55 cpu_relax();
56 }
57 return 0;
58}
59
Alexandre Belloni1ebb9092019-03-03 23:19:30 +010060static int tx4939_rtc_set_time(struct device *dev, struct rtc_time *tm)
Atsushi Nemoto0e149232009-01-06 14:42:22 -080061{
Kefeng Wang527bd752019-04-23 15:50:16 +080062 struct tx4939rtc_plat_data *pdata = dev_get_drvdata(dev);
Atsushi Nemoto0e149232009-01-06 14:42:22 -080063 struct tx4939_rtc_reg __iomem *rtcreg = pdata->rtcreg;
Alexandre Belloni1ebb9092019-03-03 23:19:30 +010064 unsigned long secs = rtc_tm_to_time64(tm);
Atsushi Nemoto0e149232009-01-06 14:42:22 -080065 int i, ret;
66 unsigned char buf[6];
67
68 buf[0] = 0;
69 buf[1] = 0;
70 buf[2] = secs;
71 buf[3] = secs >> 8;
72 buf[4] = secs >> 16;
73 buf[5] = secs >> 24;
Atsushi Nemotoaf69a182009-12-15 16:46:00 -080074 spin_lock_irq(&pdata->lock);
Atsushi Nemoto0e149232009-01-06 14:42:22 -080075 __raw_writel(0, &rtcreg->adr);
76 for (i = 0; i < 6; i++)
77 __raw_writel(buf[i], &rtcreg->dat);
78 ret = tx4939_rtc_cmd(rtcreg,
79 TX4939_RTCCTL_COMMAND_SETTIME |
80 (__raw_readl(&rtcreg->ctl) & TX4939_RTCCTL_ALME));
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
85static int tx4939_rtc_read_time(struct device *dev, struct rtc_time *tm)
86{
Kefeng Wang527bd752019-04-23 15:50:16 +080087 struct tx4939rtc_plat_data *pdata = dev_get_drvdata(dev);
Atsushi Nemoto0e149232009-01-06 14:42:22 -080088 struct tx4939_rtc_reg __iomem *rtcreg = pdata->rtcreg;
89 int i, ret;
90 unsigned long sec;
91 unsigned char buf[6];
92
Atsushi Nemotoaf69a182009-12-15 16:46:00 -080093 spin_lock_irq(&pdata->lock);
Atsushi Nemoto0e149232009-01-06 14:42:22 -080094 ret = tx4939_rtc_cmd(rtcreg,
95 TX4939_RTCCTL_COMMAND_GETTIME |
96 (__raw_readl(&rtcreg->ctl) & TX4939_RTCCTL_ALME));
97 if (ret) {
Atsushi Nemotoaf69a182009-12-15 16:46:00 -080098 spin_unlock_irq(&pdata->lock);
Atsushi Nemoto0e149232009-01-06 14:42:22 -080099 return ret;
100 }
101 __raw_writel(2, &rtcreg->adr);
102 for (i = 2; i < 6; i++)
103 buf[i] = __raw_readl(&rtcreg->dat);
Atsushi Nemotoaf69a182009-12-15 16:46:00 -0800104 spin_unlock_irq(&pdata->lock);
Colin Ian King347876a2018-02-15 19:36:14 +0000105 sec = ((unsigned long)buf[5] << 24) | (buf[4] << 16) |
106 (buf[3] << 8) | buf[2];
Alexandre Bellonie230b2c2019-03-03 23:14:35 +0100107 rtc_time64_to_tm(sec, tm);
Alexandre Belloniab626702018-02-19 16:23:55 +0100108 return 0;
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800109}
110
111static int tx4939_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
112{
Kefeng Wang527bd752019-04-23 15:50:16 +0800113 struct tx4939rtc_plat_data *pdata = dev_get_drvdata(dev);
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800114 struct tx4939_rtc_reg __iomem *rtcreg = pdata->rtcreg;
115 int i, ret;
116 unsigned long sec;
117 unsigned char buf[6];
118
Alexandre Bellonie230b2c2019-03-03 23:14:35 +0100119 sec = rtc_tm_to_time64(&alrm->time);
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800120 buf[0] = 0;
121 buf[1] = 0;
122 buf[2] = sec;
123 buf[3] = sec >> 8;
124 buf[4] = sec >> 16;
125 buf[5] = sec >> 24;
Atsushi Nemotoaf69a182009-12-15 16:46:00 -0800126 spin_lock_irq(&pdata->lock);
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800127 __raw_writel(0, &rtcreg->adr);
128 for (i = 0; i < 6; i++)
129 __raw_writel(buf[i], &rtcreg->dat);
130 ret = tx4939_rtc_cmd(rtcreg, TX4939_RTCCTL_COMMAND_SETALARM |
131 (alrm->enabled ? TX4939_RTCCTL_ALME : 0));
Atsushi Nemotoaf69a182009-12-15 16:46:00 -0800132 spin_unlock_irq(&pdata->lock);
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800133 return ret;
134}
135
136static int tx4939_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
137{
Kefeng Wang527bd752019-04-23 15:50:16 +0800138 struct tx4939rtc_plat_data *pdata = dev_get_drvdata(dev);
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800139 struct tx4939_rtc_reg __iomem *rtcreg = pdata->rtcreg;
140 int i, ret;
141 unsigned long sec;
142 unsigned char buf[6];
143 u32 ctl;
144
Atsushi Nemotoaf69a182009-12-15 16:46:00 -0800145 spin_lock_irq(&pdata->lock);
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800146 ret = tx4939_rtc_cmd(rtcreg,
147 TX4939_RTCCTL_COMMAND_GETALARM |
148 (__raw_readl(&rtcreg->ctl) & TX4939_RTCCTL_ALME));
149 if (ret) {
Atsushi Nemotoaf69a182009-12-15 16:46:00 -0800150 spin_unlock_irq(&pdata->lock);
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800151 return ret;
152 }
153 __raw_writel(2, &rtcreg->adr);
154 for (i = 2; i < 6; i++)
155 buf[i] = __raw_readl(&rtcreg->dat);
156 ctl = __raw_readl(&rtcreg->ctl);
157 alrm->enabled = (ctl & TX4939_RTCCTL_ALME) ? 1 : 0;
158 alrm->pending = (ctl & TX4939_RTCCTL_ALMD) ? 1 : 0;
Atsushi Nemotoaf69a182009-12-15 16:46:00 -0800159 spin_unlock_irq(&pdata->lock);
Colin Ian King347876a2018-02-15 19:36:14 +0000160 sec = ((unsigned long)buf[5] << 24) | (buf[4] << 16) |
161 (buf[3] << 8) | buf[2];
Alexandre Bellonie230b2c2019-03-03 23:14:35 +0100162 rtc_time64_to_tm(sec, &alrm->time);
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800163 return rtc_valid_tm(&alrm->time);
164}
165
166static int tx4939_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
167{
Kefeng Wang527bd752019-04-23 15:50:16 +0800168 struct tx4939rtc_plat_data *pdata = dev_get_drvdata(dev);
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800169
Atsushi Nemotoaf69a182009-12-15 16:46:00 -0800170 spin_lock_irq(&pdata->lock);
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800171 tx4939_rtc_cmd(pdata->rtcreg,
172 TX4939_RTCCTL_COMMAND_NOP |
173 (enabled ? TX4939_RTCCTL_ALME : 0));
Atsushi Nemotoaf69a182009-12-15 16:46:00 -0800174 spin_unlock_irq(&pdata->lock);
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800175 return 0;
176}
177
178static irqreturn_t tx4939_rtc_interrupt(int irq, void *dev_id)
179{
Kefeng Wang527bd752019-04-23 15:50:16 +0800180 struct tx4939rtc_plat_data *pdata = dev_get_drvdata(dev_id);
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800181 struct tx4939_rtc_reg __iomem *rtcreg = pdata->rtcreg;
182 unsigned long events = RTC_IRQF;
183
Atsushi Nemotoaf69a182009-12-15 16:46:00 -0800184 spin_lock(&pdata->lock);
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800185 if (__raw_readl(&rtcreg->ctl) & TX4939_RTCCTL_ALMD) {
186 events |= RTC_AF;
187 tx4939_rtc_cmd(rtcreg, TX4939_RTCCTL_COMMAND_NOP);
188 }
Atsushi Nemotoaf69a182009-12-15 16:46:00 -0800189 spin_unlock(&pdata->lock);
Alexander Shiyan0d719152014-04-03 14:50:20 -0700190 rtc_update_irq(pdata->rtc, 1, events);
191
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800192 return IRQ_HANDLED;
193}
194
195static const struct rtc_class_ops tx4939_rtc_ops = {
196 .read_time = tx4939_rtc_read_time,
197 .read_alarm = tx4939_rtc_read_alarm,
198 .set_alarm = tx4939_rtc_set_alarm,
Alexandre Belloni1ebb9092019-03-03 23:19:30 +0100199 .set_time = tx4939_rtc_set_time,
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800200 .alarm_irq_enable = tx4939_rtc_alarm_irq_enable,
201};
202
Alexandre Bellonidd888c62018-02-12 23:47:59 +0100203static int tx4939_nvram_read(void *priv, unsigned int pos, void *val,
204 size_t bytes)
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800205{
Alexandre Bellonidd888c62018-02-12 23:47:59 +0100206 struct tx4939rtc_plat_data *pdata = priv;
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800207 struct tx4939_rtc_reg __iomem *rtcreg = pdata->rtcreg;
Alexandre Bellonidd888c62018-02-12 23:47:59 +0100208 u8 *buf = val;
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800209
Atsushi Nemotoaf69a182009-12-15 16:46:00 -0800210 spin_lock_irq(&pdata->lock);
Alexandre Bellonidd888c62018-02-12 23:47:59 +0100211 for (; bytes; bytes--) {
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800212 __raw_writel(pos++, &rtcreg->adr);
213 *buf++ = __raw_readl(&rtcreg->dat);
214 }
Atsushi Nemotoaf69a182009-12-15 16:46:00 -0800215 spin_unlock_irq(&pdata->lock);
Alexandre Bellonidd888c62018-02-12 23:47:59 +0100216 return 0;
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800217}
218
Alexandre Bellonidd888c62018-02-12 23:47:59 +0100219static int tx4939_nvram_write(void *priv, unsigned int pos, void *val,
220 size_t bytes)
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800221{
Alexandre Bellonidd888c62018-02-12 23:47:59 +0100222 struct tx4939rtc_plat_data *pdata = priv;
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800223 struct tx4939_rtc_reg __iomem *rtcreg = pdata->rtcreg;
Alexandre Bellonidd888c62018-02-12 23:47:59 +0100224 u8 *buf = val;
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800225
Atsushi Nemotoaf69a182009-12-15 16:46:00 -0800226 spin_lock_irq(&pdata->lock);
Alexandre Bellonidd888c62018-02-12 23:47:59 +0100227 for (; bytes; bytes--) {
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800228 __raw_writel(pos++, &rtcreg->adr);
229 __raw_writel(*buf++, &rtcreg->dat);
230 }
Atsushi Nemotoaf69a182009-12-15 16:46:00 -0800231 spin_unlock_irq(&pdata->lock);
Alexandre Bellonidd888c62018-02-12 23:47:59 +0100232 return 0;
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800233}
234
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800235static int __init tx4939_rtc_probe(struct platform_device *pdev)
236{
237 struct rtc_device *rtc;
238 struct tx4939rtc_plat_data *pdata;
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800239 int irq, ret;
Alexandre Bellonidd888c62018-02-12 23:47:59 +0100240 struct nvmem_config nvmem_cfg = {
Alexandre Belloni2ab78752018-09-19 03:14:58 +0200241 .name = "tx4939_nvram",
Alexandre Bellonidd888c62018-02-12 23:47:59 +0100242 .size = TX4939_RTC_REG_RAMSIZE,
243 .reg_read = tx4939_nvram_read,
244 .reg_write = tx4939_nvram_write,
245 };
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800246
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800247 irq = platform_get_irq(pdev, 0);
248 if (irq < 0)
249 return -ENODEV;
250 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
251 if (!pdata)
252 return -ENOMEM;
253 platform_set_drvdata(pdev, pdata);
254
YueHaibing09ef18b2019-10-06 18:29:20 +0800255 pdata->rtcreg = devm_platform_ioremap_resource(pdev, 0);
Julia Lawall7c1d69e2013-09-11 14:24:27 -0700256 if (IS_ERR(pdata->rtcreg))
257 return PTR_ERR(pdata->rtcreg);
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800258
Atsushi Nemotoaf69a182009-12-15 16:46:00 -0800259 spin_lock_init(&pdata->lock);
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800260 tx4939_rtc_cmd(pdata->rtcreg, TX4939_RTCCTL_COMMAND_NOP);
261 if (devm_request_irq(&pdev->dev, irq, tx4939_rtc_interrupt,
Yong Zhang2f6e5f92012-03-23 15:02:34 -0700262 0, pdev->name, &pdev->dev) < 0)
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800263 return -EBUSY;
Alexandre Belloni68981fd2018-02-12 23:47:57 +0100264 rtc = devm_rtc_allocate_device(&pdev->dev);
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800265 if (IS_ERR(rtc))
266 return PTR_ERR(rtc);
Alexandre Belloni68981fd2018-02-12 23:47:57 +0100267
268 rtc->ops = &tx4939_rtc_ops;
Alexandre Bellonidd888c62018-02-12 23:47:59 +0100269 rtc->nvram_old_abi = true;
Alexandre Bellonie866cbd2019-03-03 23:08:09 +0100270 rtc->range_max = U32_MAX;
Alexandre Belloni68981fd2018-02-12 23:47:57 +0100271
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800272 pdata->rtc = rtc;
Alexandre Bellonia2fa9b82018-02-12 23:47:58 +0100273
Alexandre Bellonidd888c62018-02-12 23:47:59 +0100274 nvmem_cfg.priv = pdata;
275 ret = rtc_nvmem_register(rtc, &nvmem_cfg);
276 if (ret)
277 return ret;
Jingoo Hane38fc302013-04-29 16:19:18 -0700278
Alexandre Bellonidd888c62018-02-12 23:47:59 +0100279 return rtc_register_device(rtc);
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800280}
281
282static int __exit tx4939_rtc_remove(struct platform_device *pdev)
283{
284 struct tx4939rtc_plat_data *pdata = platform_get_drvdata(pdev);
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800285
Atsushi Nemotoaf69a182009-12-15 16:46:00 -0800286 spin_lock_irq(&pdata->lock);
287 tx4939_rtc_cmd(pdata->rtcreg, TX4939_RTCCTL_COMMAND_NOP);
288 spin_unlock_irq(&pdata->lock);
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800289 return 0;
290}
291
292static struct platform_driver tx4939_rtc_driver = {
293 .remove = __exit_p(tx4939_rtc_remove),
294 .driver = {
295 .name = "tx4939rtc",
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800296 },
297};
298
Jingoo Han80f1def2013-04-29 16:18:54 -0700299module_platform_driver_probe(tx4939_rtc_driver, tx4939_rtc_probe);
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800300
301MODULE_AUTHOR("Atsushi Nemoto <anemo@mba.ocn.ne.jp>");
302MODULE_DESCRIPTION("TX4939 internal RTC driver");
Alexandre Belloni1a9de212019-03-04 20:02:05 +0100303MODULE_LICENSE("GPL v2");
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800304MODULE_ALIAS("platform:tx4939rtc");