blob: 404de455409b1ecb91795f0f04c230c793cad279 [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>
Alexandre Bellonia0151062018-02-12 23:47:55 +010017
18#define TX4939_RTCCTL_ALME 0x00000080
19#define TX4939_RTCCTL_ALMD 0x00000040
20#define TX4939_RTCCTL_BUSY 0x00000020
21
22#define TX4939_RTCCTL_COMMAND 0x00000007
23#define TX4939_RTCCTL_COMMAND_NOP 0x00000000
24#define TX4939_RTCCTL_COMMAND_GETTIME 0x00000001
25#define TX4939_RTCCTL_COMMAND_SETTIME 0x00000002
26#define TX4939_RTCCTL_COMMAND_GETALARM 0x00000003
27#define TX4939_RTCCTL_COMMAND_SETALARM 0x00000004
28
29#define TX4939_RTCTBC_PM 0x00000080
30#define TX4939_RTCTBC_COMP 0x0000007f
31
32#define TX4939_RTC_REG_RAMSIZE 0x00000100
33#define TX4939_RTC_REG_RWBSIZE 0x00000006
34
35struct tx4939_rtc_reg {
36 __u32 ctl;
37 __u32 adr;
38 __u32 dat;
39 __u32 tbc;
40};
Atsushi Nemoto0e149232009-01-06 14:42:22 -080041
42struct tx4939rtc_plat_data {
43 struct rtc_device *rtc;
44 struct tx4939_rtc_reg __iomem *rtcreg;
Atsushi Nemotoaf69a182009-12-15 16:46:00 -080045 spinlock_t lock;
Atsushi Nemoto0e149232009-01-06 14:42:22 -080046};
47
48static struct tx4939rtc_plat_data *get_tx4939rtc_plat_data(struct device *dev)
49{
50 return platform_get_drvdata(to_platform_device(dev));
51}
52
53static int tx4939_rtc_cmd(struct tx4939_rtc_reg __iomem *rtcreg, int cmd)
54{
55 int i = 0;
56
57 __raw_writel(cmd, &rtcreg->ctl);
58 /* This might take 30us (next 32.768KHz clock) */
59 while (__raw_readl(&rtcreg->ctl) & TX4939_RTCCTL_BUSY) {
60 /* timeout on approx. 100us (@ GBUS200MHz) */
61 if (i++ > 200 * 100)
62 return -EBUSY;
63 cpu_relax();
64 }
65 return 0;
66}
67
Alexandre Belloni1ebb9092019-03-03 23:19:30 +010068static int tx4939_rtc_set_time(struct device *dev, struct rtc_time *tm)
Atsushi Nemoto0e149232009-01-06 14:42:22 -080069{
70 struct tx4939rtc_plat_data *pdata = get_tx4939rtc_plat_data(dev);
71 struct tx4939_rtc_reg __iomem *rtcreg = pdata->rtcreg;
Alexandre Belloni1ebb9092019-03-03 23:19:30 +010072 unsigned long secs = rtc_tm_to_time64(tm);
Atsushi Nemoto0e149232009-01-06 14:42:22 -080073 int i, ret;
74 unsigned char buf[6];
75
76 buf[0] = 0;
77 buf[1] = 0;
78 buf[2] = secs;
79 buf[3] = secs >> 8;
80 buf[4] = secs >> 16;
81 buf[5] = secs >> 24;
Atsushi Nemotoaf69a182009-12-15 16:46:00 -080082 spin_lock_irq(&pdata->lock);
Atsushi Nemoto0e149232009-01-06 14:42:22 -080083 __raw_writel(0, &rtcreg->adr);
84 for (i = 0; i < 6; i++)
85 __raw_writel(buf[i], &rtcreg->dat);
86 ret = tx4939_rtc_cmd(rtcreg,
87 TX4939_RTCCTL_COMMAND_SETTIME |
88 (__raw_readl(&rtcreg->ctl) & TX4939_RTCCTL_ALME));
Atsushi Nemotoaf69a182009-12-15 16:46:00 -080089 spin_unlock_irq(&pdata->lock);
Atsushi Nemoto0e149232009-01-06 14:42:22 -080090 return ret;
91}
92
93static int tx4939_rtc_read_time(struct device *dev, struct rtc_time *tm)
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
Atsushi Nemotoaf69a182009-12-15 16:46:00 -0800101 spin_lock_irq(&pdata->lock);
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800102 ret = tx4939_rtc_cmd(rtcreg,
103 TX4939_RTCCTL_COMMAND_GETTIME |
104 (__raw_readl(&rtcreg->ctl) & TX4939_RTCCTL_ALME));
105 if (ret) {
Atsushi Nemotoaf69a182009-12-15 16:46:00 -0800106 spin_unlock_irq(&pdata->lock);
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800107 return ret;
108 }
109 __raw_writel(2, &rtcreg->adr);
110 for (i = 2; i < 6; i++)
111 buf[i] = __raw_readl(&rtcreg->dat);
Atsushi Nemotoaf69a182009-12-15 16:46:00 -0800112 spin_unlock_irq(&pdata->lock);
Colin Ian King347876a2018-02-15 19:36:14 +0000113 sec = ((unsigned long)buf[5] << 24) | (buf[4] << 16) |
114 (buf[3] << 8) | buf[2];
Alexandre Bellonie230b2c2019-03-03 23:14:35 +0100115 rtc_time64_to_tm(sec, tm);
Alexandre Belloniab626702018-02-19 16:23:55 +0100116 return 0;
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800117}
118
119static int tx4939_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
120{
121 struct tx4939rtc_plat_data *pdata = get_tx4939rtc_plat_data(dev);
122 struct tx4939_rtc_reg __iomem *rtcreg = pdata->rtcreg;
123 int i, ret;
124 unsigned long sec;
125 unsigned char buf[6];
126
Alexandre Bellonie230b2c2019-03-03 23:14:35 +0100127 sec = rtc_tm_to_time64(&alrm->time);
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800128 buf[0] = 0;
129 buf[1] = 0;
130 buf[2] = sec;
131 buf[3] = sec >> 8;
132 buf[4] = sec >> 16;
133 buf[5] = sec >> 24;
Atsushi Nemotoaf69a182009-12-15 16:46:00 -0800134 spin_lock_irq(&pdata->lock);
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800135 __raw_writel(0, &rtcreg->adr);
136 for (i = 0; i < 6; i++)
137 __raw_writel(buf[i], &rtcreg->dat);
138 ret = tx4939_rtc_cmd(rtcreg, TX4939_RTCCTL_COMMAND_SETALARM |
139 (alrm->enabled ? TX4939_RTCCTL_ALME : 0));
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
144static int tx4939_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
145{
146 struct tx4939rtc_plat_data *pdata = get_tx4939rtc_plat_data(dev);
147 struct tx4939_rtc_reg __iomem *rtcreg = pdata->rtcreg;
148 int i, ret;
149 unsigned long sec;
150 unsigned char buf[6];
151 u32 ctl;
152
Atsushi Nemotoaf69a182009-12-15 16:46:00 -0800153 spin_lock_irq(&pdata->lock);
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800154 ret = tx4939_rtc_cmd(rtcreg,
155 TX4939_RTCCTL_COMMAND_GETALARM |
156 (__raw_readl(&rtcreg->ctl) & TX4939_RTCCTL_ALME));
157 if (ret) {
Atsushi Nemotoaf69a182009-12-15 16:46:00 -0800158 spin_unlock_irq(&pdata->lock);
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800159 return ret;
160 }
161 __raw_writel(2, &rtcreg->adr);
162 for (i = 2; i < 6; i++)
163 buf[i] = __raw_readl(&rtcreg->dat);
164 ctl = __raw_readl(&rtcreg->ctl);
165 alrm->enabled = (ctl & TX4939_RTCCTL_ALME) ? 1 : 0;
166 alrm->pending = (ctl & TX4939_RTCCTL_ALMD) ? 1 : 0;
Atsushi Nemotoaf69a182009-12-15 16:46:00 -0800167 spin_unlock_irq(&pdata->lock);
Colin Ian King347876a2018-02-15 19:36:14 +0000168 sec = ((unsigned long)buf[5] << 24) | (buf[4] << 16) |
169 (buf[3] << 8) | buf[2];
Alexandre Bellonie230b2c2019-03-03 23:14:35 +0100170 rtc_time64_to_tm(sec, &alrm->time);
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800171 return rtc_valid_tm(&alrm->time);
172}
173
174static int tx4939_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
175{
176 struct tx4939rtc_plat_data *pdata = get_tx4939rtc_plat_data(dev);
177
Atsushi Nemotoaf69a182009-12-15 16:46:00 -0800178 spin_lock_irq(&pdata->lock);
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800179 tx4939_rtc_cmd(pdata->rtcreg,
180 TX4939_RTCCTL_COMMAND_NOP |
181 (enabled ? TX4939_RTCCTL_ALME : 0));
Atsushi Nemotoaf69a182009-12-15 16:46:00 -0800182 spin_unlock_irq(&pdata->lock);
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800183 return 0;
184}
185
186static irqreturn_t tx4939_rtc_interrupt(int irq, void *dev_id)
187{
188 struct tx4939rtc_plat_data *pdata = get_tx4939rtc_plat_data(dev_id);
189 struct tx4939_rtc_reg __iomem *rtcreg = pdata->rtcreg;
190 unsigned long events = RTC_IRQF;
191
Atsushi Nemotoaf69a182009-12-15 16:46:00 -0800192 spin_lock(&pdata->lock);
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800193 if (__raw_readl(&rtcreg->ctl) & TX4939_RTCCTL_ALMD) {
194 events |= RTC_AF;
195 tx4939_rtc_cmd(rtcreg, TX4939_RTCCTL_COMMAND_NOP);
196 }
Atsushi Nemotoaf69a182009-12-15 16:46:00 -0800197 spin_unlock(&pdata->lock);
Alexander Shiyan0d719152014-04-03 14:50:20 -0700198 rtc_update_irq(pdata->rtc, 1, events);
199
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800200 return IRQ_HANDLED;
201}
202
203static const struct rtc_class_ops tx4939_rtc_ops = {
204 .read_time = tx4939_rtc_read_time,
205 .read_alarm = tx4939_rtc_read_alarm,
206 .set_alarm = tx4939_rtc_set_alarm,
Alexandre Belloni1ebb9092019-03-03 23:19:30 +0100207 .set_time = tx4939_rtc_set_time,
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800208 .alarm_irq_enable = tx4939_rtc_alarm_irq_enable,
209};
210
Alexandre Bellonidd888c62018-02-12 23:47:59 +0100211static int tx4939_nvram_read(void *priv, unsigned int pos, void *val,
212 size_t bytes)
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800213{
Alexandre Bellonidd888c62018-02-12 23:47:59 +0100214 struct tx4939rtc_plat_data *pdata = priv;
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800215 struct tx4939_rtc_reg __iomem *rtcreg = pdata->rtcreg;
Alexandre Bellonidd888c62018-02-12 23:47:59 +0100216 u8 *buf = val;
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800217
Atsushi Nemotoaf69a182009-12-15 16:46:00 -0800218 spin_lock_irq(&pdata->lock);
Alexandre Bellonidd888c62018-02-12 23:47:59 +0100219 for (; bytes; bytes--) {
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800220 __raw_writel(pos++, &rtcreg->adr);
221 *buf++ = __raw_readl(&rtcreg->dat);
222 }
Atsushi Nemotoaf69a182009-12-15 16:46:00 -0800223 spin_unlock_irq(&pdata->lock);
Alexandre Bellonidd888c62018-02-12 23:47:59 +0100224 return 0;
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800225}
226
Alexandre Bellonidd888c62018-02-12 23:47:59 +0100227static int tx4939_nvram_write(void *priv, unsigned int pos, void *val,
228 size_t bytes)
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800229{
Alexandre Bellonidd888c62018-02-12 23:47:59 +0100230 struct tx4939rtc_plat_data *pdata = priv;
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800231 struct tx4939_rtc_reg __iomem *rtcreg = pdata->rtcreg;
Alexandre Bellonidd888c62018-02-12 23:47:59 +0100232 u8 *buf = val;
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800233
Atsushi Nemotoaf69a182009-12-15 16:46:00 -0800234 spin_lock_irq(&pdata->lock);
Alexandre Bellonidd888c62018-02-12 23:47:59 +0100235 for (; bytes; bytes--) {
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800236 __raw_writel(pos++, &rtcreg->adr);
237 __raw_writel(*buf++, &rtcreg->dat);
238 }
Atsushi Nemotoaf69a182009-12-15 16:46:00 -0800239 spin_unlock_irq(&pdata->lock);
Alexandre Bellonidd888c62018-02-12 23:47:59 +0100240 return 0;
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800241}
242
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800243static int __init tx4939_rtc_probe(struct platform_device *pdev)
244{
245 struct rtc_device *rtc;
246 struct tx4939rtc_plat_data *pdata;
247 struct resource *res;
248 int irq, ret;
Alexandre Bellonidd888c62018-02-12 23:47:59 +0100249 struct nvmem_config nvmem_cfg = {
Alexandre Belloni2ab78752018-09-19 03:14:58 +0200250 .name = "tx4939_nvram",
Alexandre Bellonidd888c62018-02-12 23:47:59 +0100251 .size = TX4939_RTC_REG_RAMSIZE,
252 .reg_read = tx4939_nvram_read,
253 .reg_write = tx4939_nvram_write,
254 };
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800255
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800256 irq = platform_get_irq(pdev, 0);
257 if (irq < 0)
258 return -ENODEV;
259 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
260 if (!pdata)
261 return -ENOMEM;
262 platform_set_drvdata(pdev, pdata);
263
Julia Lawall7c1d69e2013-09-11 14:24:27 -0700264 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
265 pdata->rtcreg = devm_ioremap_resource(&pdev->dev, res);
266 if (IS_ERR(pdata->rtcreg))
267 return PTR_ERR(pdata->rtcreg);
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800268
Atsushi Nemotoaf69a182009-12-15 16:46:00 -0800269 spin_lock_init(&pdata->lock);
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800270 tx4939_rtc_cmd(pdata->rtcreg, TX4939_RTCCTL_COMMAND_NOP);
271 if (devm_request_irq(&pdev->dev, irq, tx4939_rtc_interrupt,
Yong Zhang2f6e5f92012-03-23 15:02:34 -0700272 0, pdev->name, &pdev->dev) < 0)
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800273 return -EBUSY;
Alexandre Belloni68981fd2018-02-12 23:47:57 +0100274 rtc = devm_rtc_allocate_device(&pdev->dev);
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800275 if (IS_ERR(rtc))
276 return PTR_ERR(rtc);
Alexandre Belloni68981fd2018-02-12 23:47:57 +0100277
278 rtc->ops = &tx4939_rtc_ops;
Alexandre Bellonidd888c62018-02-12 23:47:59 +0100279 rtc->nvram_old_abi = true;
Alexandre Bellonie866cbd2019-03-03 23:08:09 +0100280 rtc->range_max = U32_MAX;
Alexandre Belloni68981fd2018-02-12 23:47:57 +0100281
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800282 pdata->rtc = rtc;
Alexandre Bellonia2fa9b82018-02-12 23:47:58 +0100283
Alexandre Bellonidd888c62018-02-12 23:47:59 +0100284 nvmem_cfg.priv = pdata;
285 ret = rtc_nvmem_register(rtc, &nvmem_cfg);
286 if (ret)
287 return ret;
Jingoo Hane38fc302013-04-29 16:19:18 -0700288
Alexandre Bellonidd888c62018-02-12 23:47:59 +0100289 return rtc_register_device(rtc);
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800290}
291
292static int __exit tx4939_rtc_remove(struct platform_device *pdev)
293{
294 struct tx4939rtc_plat_data *pdata = platform_get_drvdata(pdev);
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800295
Atsushi Nemotoaf69a182009-12-15 16:46:00 -0800296 spin_lock_irq(&pdata->lock);
297 tx4939_rtc_cmd(pdata->rtcreg, TX4939_RTCCTL_COMMAND_NOP);
298 spin_unlock_irq(&pdata->lock);
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800299 return 0;
300}
301
302static struct platform_driver tx4939_rtc_driver = {
303 .remove = __exit_p(tx4939_rtc_remove),
304 .driver = {
305 .name = "tx4939rtc",
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800306 },
307};
308
Jingoo Han80f1def2013-04-29 16:18:54 -0700309module_platform_driver_probe(tx4939_rtc_driver, tx4939_rtc_probe);
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800310
311MODULE_AUTHOR("Atsushi Nemoto <anemo@mba.ocn.ne.jp>");
312MODULE_DESCRIPTION("TX4939 internal RTC driver");
313MODULE_LICENSE("GPL");
314MODULE_ALIAS("platform:tx4939rtc");