blob: 2d24babc4057fbea1357d57810470f97ffcbba97 [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
45static struct tx4939rtc_plat_data *get_tx4939rtc_plat_data(struct device *dev)
46{
47 return platform_get_drvdata(to_platform_device(dev));
48}
49
50static int tx4939_rtc_cmd(struct tx4939_rtc_reg __iomem *rtcreg, int cmd)
51{
52 int i = 0;
53
54 __raw_writel(cmd, &rtcreg->ctl);
55 /* This might take 30us (next 32.768KHz clock) */
56 while (__raw_readl(&rtcreg->ctl) & TX4939_RTCCTL_BUSY) {
57 /* timeout on approx. 100us (@ GBUS200MHz) */
58 if (i++ > 200 * 100)
59 return -EBUSY;
60 cpu_relax();
61 }
62 return 0;
63}
64
Alexandre Belloni1ebb9092019-03-03 23:19:30 +010065static int tx4939_rtc_set_time(struct device *dev, struct rtc_time *tm)
Atsushi Nemoto0e149232009-01-06 14:42:22 -080066{
67 struct tx4939rtc_plat_data *pdata = get_tx4939rtc_plat_data(dev);
68 struct tx4939_rtc_reg __iomem *rtcreg = pdata->rtcreg;
Alexandre Belloni1ebb9092019-03-03 23:19:30 +010069 unsigned long secs = rtc_tm_to_time64(tm);
Atsushi Nemoto0e149232009-01-06 14:42:22 -080070 int i, ret;
71 unsigned char buf[6];
72
73 buf[0] = 0;
74 buf[1] = 0;
75 buf[2] = secs;
76 buf[3] = secs >> 8;
77 buf[4] = secs >> 16;
78 buf[5] = secs >> 24;
Atsushi Nemotoaf69a182009-12-15 16:46:00 -080079 spin_lock_irq(&pdata->lock);
Atsushi Nemoto0e149232009-01-06 14:42:22 -080080 __raw_writel(0, &rtcreg->adr);
81 for (i = 0; i < 6; i++)
82 __raw_writel(buf[i], &rtcreg->dat);
83 ret = tx4939_rtc_cmd(rtcreg,
84 TX4939_RTCCTL_COMMAND_SETTIME |
85 (__raw_readl(&rtcreg->ctl) & TX4939_RTCCTL_ALME));
Atsushi Nemotoaf69a182009-12-15 16:46:00 -080086 spin_unlock_irq(&pdata->lock);
Atsushi Nemoto0e149232009-01-06 14:42:22 -080087 return ret;
88}
89
90static int tx4939_rtc_read_time(struct device *dev, struct rtc_time *tm)
91{
92 struct tx4939rtc_plat_data *pdata = get_tx4939rtc_plat_data(dev);
93 struct tx4939_rtc_reg __iomem *rtcreg = pdata->rtcreg;
94 int i, ret;
95 unsigned long sec;
96 unsigned char buf[6];
97
Atsushi Nemotoaf69a182009-12-15 16:46:00 -080098 spin_lock_irq(&pdata->lock);
Atsushi Nemoto0e149232009-01-06 14:42:22 -080099 ret = tx4939_rtc_cmd(rtcreg,
100 TX4939_RTCCTL_COMMAND_GETTIME |
101 (__raw_readl(&rtcreg->ctl) & TX4939_RTCCTL_ALME));
102 if (ret) {
Atsushi Nemotoaf69a182009-12-15 16:46:00 -0800103 spin_unlock_irq(&pdata->lock);
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800104 return ret;
105 }
106 __raw_writel(2, &rtcreg->adr);
107 for (i = 2; i < 6; i++)
108 buf[i] = __raw_readl(&rtcreg->dat);
Atsushi Nemotoaf69a182009-12-15 16:46:00 -0800109 spin_unlock_irq(&pdata->lock);
Colin Ian King347876a2018-02-15 19:36:14 +0000110 sec = ((unsigned long)buf[5] << 24) | (buf[4] << 16) |
111 (buf[3] << 8) | buf[2];
Alexandre Bellonie230b2c2019-03-03 23:14:35 +0100112 rtc_time64_to_tm(sec, tm);
Alexandre Belloniab626702018-02-19 16:23:55 +0100113 return 0;
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800114}
115
116static int tx4939_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
117{
118 struct tx4939rtc_plat_data *pdata = get_tx4939rtc_plat_data(dev);
119 struct tx4939_rtc_reg __iomem *rtcreg = pdata->rtcreg;
120 int i, ret;
121 unsigned long sec;
122 unsigned char buf[6];
123
Alexandre Bellonie230b2c2019-03-03 23:14:35 +0100124 sec = rtc_tm_to_time64(&alrm->time);
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800125 buf[0] = 0;
126 buf[1] = 0;
127 buf[2] = sec;
128 buf[3] = sec >> 8;
129 buf[4] = sec >> 16;
130 buf[5] = sec >> 24;
Atsushi Nemotoaf69a182009-12-15 16:46:00 -0800131 spin_lock_irq(&pdata->lock);
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800132 __raw_writel(0, &rtcreg->adr);
133 for (i = 0; i < 6; i++)
134 __raw_writel(buf[i], &rtcreg->dat);
135 ret = tx4939_rtc_cmd(rtcreg, TX4939_RTCCTL_COMMAND_SETALARM |
136 (alrm->enabled ? TX4939_RTCCTL_ALME : 0));
Atsushi Nemotoaf69a182009-12-15 16:46:00 -0800137 spin_unlock_irq(&pdata->lock);
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800138 return ret;
139}
140
141static int tx4939_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
142{
143 struct tx4939rtc_plat_data *pdata = get_tx4939rtc_plat_data(dev);
144 struct tx4939_rtc_reg __iomem *rtcreg = pdata->rtcreg;
145 int i, ret;
146 unsigned long sec;
147 unsigned char buf[6];
148 u32 ctl;
149
Atsushi Nemotoaf69a182009-12-15 16:46:00 -0800150 spin_lock_irq(&pdata->lock);
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800151 ret = tx4939_rtc_cmd(rtcreg,
152 TX4939_RTCCTL_COMMAND_GETALARM |
153 (__raw_readl(&rtcreg->ctl) & TX4939_RTCCTL_ALME));
154 if (ret) {
Atsushi Nemotoaf69a182009-12-15 16:46:00 -0800155 spin_unlock_irq(&pdata->lock);
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800156 return ret;
157 }
158 __raw_writel(2, &rtcreg->adr);
159 for (i = 2; i < 6; i++)
160 buf[i] = __raw_readl(&rtcreg->dat);
161 ctl = __raw_readl(&rtcreg->ctl);
162 alrm->enabled = (ctl & TX4939_RTCCTL_ALME) ? 1 : 0;
163 alrm->pending = (ctl & TX4939_RTCCTL_ALMD) ? 1 : 0;
Atsushi Nemotoaf69a182009-12-15 16:46:00 -0800164 spin_unlock_irq(&pdata->lock);
Colin Ian King347876a2018-02-15 19:36:14 +0000165 sec = ((unsigned long)buf[5] << 24) | (buf[4] << 16) |
166 (buf[3] << 8) | buf[2];
Alexandre Bellonie230b2c2019-03-03 23:14:35 +0100167 rtc_time64_to_tm(sec, &alrm->time);
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800168 return rtc_valid_tm(&alrm->time);
169}
170
171static int tx4939_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
172{
173 struct tx4939rtc_plat_data *pdata = get_tx4939rtc_plat_data(dev);
174
Atsushi Nemotoaf69a182009-12-15 16:46:00 -0800175 spin_lock_irq(&pdata->lock);
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800176 tx4939_rtc_cmd(pdata->rtcreg,
177 TX4939_RTCCTL_COMMAND_NOP |
178 (enabled ? TX4939_RTCCTL_ALME : 0));
Atsushi Nemotoaf69a182009-12-15 16:46:00 -0800179 spin_unlock_irq(&pdata->lock);
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800180 return 0;
181}
182
183static irqreturn_t tx4939_rtc_interrupt(int irq, void *dev_id)
184{
185 struct tx4939rtc_plat_data *pdata = get_tx4939rtc_plat_data(dev_id);
186 struct tx4939_rtc_reg __iomem *rtcreg = pdata->rtcreg;
187 unsigned long events = RTC_IRQF;
188
Atsushi Nemotoaf69a182009-12-15 16:46:00 -0800189 spin_lock(&pdata->lock);
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800190 if (__raw_readl(&rtcreg->ctl) & TX4939_RTCCTL_ALMD) {
191 events |= RTC_AF;
192 tx4939_rtc_cmd(rtcreg, TX4939_RTCCTL_COMMAND_NOP);
193 }
Atsushi Nemotoaf69a182009-12-15 16:46:00 -0800194 spin_unlock(&pdata->lock);
Alexander Shiyan0d719152014-04-03 14:50:20 -0700195 rtc_update_irq(pdata->rtc, 1, events);
196
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800197 return IRQ_HANDLED;
198}
199
200static const struct rtc_class_ops tx4939_rtc_ops = {
201 .read_time = tx4939_rtc_read_time,
202 .read_alarm = tx4939_rtc_read_alarm,
203 .set_alarm = tx4939_rtc_set_alarm,
Alexandre Belloni1ebb9092019-03-03 23:19:30 +0100204 .set_time = tx4939_rtc_set_time,
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800205 .alarm_irq_enable = tx4939_rtc_alarm_irq_enable,
206};
207
Alexandre Bellonidd888c62018-02-12 23:47:59 +0100208static int tx4939_nvram_read(void *priv, unsigned int pos, void *val,
209 size_t bytes)
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800210{
Alexandre Bellonidd888c62018-02-12 23:47:59 +0100211 struct tx4939rtc_plat_data *pdata = priv;
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800212 struct tx4939_rtc_reg __iomem *rtcreg = pdata->rtcreg;
Alexandre Bellonidd888c62018-02-12 23:47:59 +0100213 u8 *buf = val;
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800214
Atsushi Nemotoaf69a182009-12-15 16:46:00 -0800215 spin_lock_irq(&pdata->lock);
Alexandre Bellonidd888c62018-02-12 23:47:59 +0100216 for (; bytes; bytes--) {
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800217 __raw_writel(pos++, &rtcreg->adr);
218 *buf++ = __raw_readl(&rtcreg->dat);
219 }
Atsushi Nemotoaf69a182009-12-15 16:46:00 -0800220 spin_unlock_irq(&pdata->lock);
Alexandre Bellonidd888c62018-02-12 23:47:59 +0100221 return 0;
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800222}
223
Alexandre Bellonidd888c62018-02-12 23:47:59 +0100224static int tx4939_nvram_write(void *priv, unsigned int pos, void *val,
225 size_t bytes)
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800226{
Alexandre Bellonidd888c62018-02-12 23:47:59 +0100227 struct tx4939rtc_plat_data *pdata = priv;
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800228 struct tx4939_rtc_reg __iomem *rtcreg = pdata->rtcreg;
Alexandre Bellonidd888c62018-02-12 23:47:59 +0100229 u8 *buf = val;
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800230
Atsushi Nemotoaf69a182009-12-15 16:46:00 -0800231 spin_lock_irq(&pdata->lock);
Alexandre Bellonidd888c62018-02-12 23:47:59 +0100232 for (; bytes; bytes--) {
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800233 __raw_writel(pos++, &rtcreg->adr);
234 __raw_writel(*buf++, &rtcreg->dat);
235 }
Atsushi Nemotoaf69a182009-12-15 16:46:00 -0800236 spin_unlock_irq(&pdata->lock);
Alexandre Bellonidd888c62018-02-12 23:47:59 +0100237 return 0;
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800238}
239
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800240static int __init tx4939_rtc_probe(struct platform_device *pdev)
241{
242 struct rtc_device *rtc;
243 struct tx4939rtc_plat_data *pdata;
244 struct resource *res;
245 int irq, ret;
Alexandre Bellonidd888c62018-02-12 23:47:59 +0100246 struct nvmem_config nvmem_cfg = {
Alexandre Belloni2ab78752018-09-19 03:14:58 +0200247 .name = "tx4939_nvram",
Alexandre Bellonidd888c62018-02-12 23:47:59 +0100248 .size = TX4939_RTC_REG_RAMSIZE,
249 .reg_read = tx4939_nvram_read,
250 .reg_write = tx4939_nvram_write,
251 };
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800252
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800253 irq = platform_get_irq(pdev, 0);
254 if (irq < 0)
255 return -ENODEV;
256 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
257 if (!pdata)
258 return -ENOMEM;
259 platform_set_drvdata(pdev, pdata);
260
Julia Lawall7c1d69e2013-09-11 14:24:27 -0700261 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
262 pdata->rtcreg = devm_ioremap_resource(&pdev->dev, res);
263 if (IS_ERR(pdata->rtcreg))
264 return PTR_ERR(pdata->rtcreg);
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800265
Atsushi Nemotoaf69a182009-12-15 16:46:00 -0800266 spin_lock_init(&pdata->lock);
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800267 tx4939_rtc_cmd(pdata->rtcreg, TX4939_RTCCTL_COMMAND_NOP);
268 if (devm_request_irq(&pdev->dev, irq, tx4939_rtc_interrupt,
Yong Zhang2f6e5f92012-03-23 15:02:34 -0700269 0, pdev->name, &pdev->dev) < 0)
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800270 return -EBUSY;
Alexandre Belloni68981fd2018-02-12 23:47:57 +0100271 rtc = devm_rtc_allocate_device(&pdev->dev);
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800272 if (IS_ERR(rtc))
273 return PTR_ERR(rtc);
Alexandre Belloni68981fd2018-02-12 23:47:57 +0100274
275 rtc->ops = &tx4939_rtc_ops;
Alexandre Bellonidd888c62018-02-12 23:47:59 +0100276 rtc->nvram_old_abi = true;
Alexandre Bellonie866cbd2019-03-03 23:08:09 +0100277 rtc->range_max = U32_MAX;
Alexandre Belloni68981fd2018-02-12 23:47:57 +0100278
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800279 pdata->rtc = rtc;
Alexandre Bellonia2fa9b82018-02-12 23:47:58 +0100280
Alexandre Bellonidd888c62018-02-12 23:47:59 +0100281 nvmem_cfg.priv = pdata;
282 ret = rtc_nvmem_register(rtc, &nvmem_cfg);
283 if (ret)
284 return ret;
Jingoo Hane38fc302013-04-29 16:19:18 -0700285
Alexandre Bellonidd888c62018-02-12 23:47:59 +0100286 return rtc_register_device(rtc);
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800287}
288
289static int __exit tx4939_rtc_remove(struct platform_device *pdev)
290{
291 struct tx4939rtc_plat_data *pdata = platform_get_drvdata(pdev);
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800292
Atsushi Nemotoaf69a182009-12-15 16:46:00 -0800293 spin_lock_irq(&pdata->lock);
294 tx4939_rtc_cmd(pdata->rtcreg, TX4939_RTCCTL_COMMAND_NOP);
295 spin_unlock_irq(&pdata->lock);
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800296 return 0;
297}
298
299static struct platform_driver tx4939_rtc_driver = {
300 .remove = __exit_p(tx4939_rtc_remove),
301 .driver = {
302 .name = "tx4939rtc",
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800303 },
304};
305
Jingoo Han80f1def2013-04-29 16:18:54 -0700306module_platform_driver_probe(tx4939_rtc_driver, tx4939_rtc_probe);
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800307
308MODULE_AUTHOR("Atsushi Nemoto <anemo@mba.ocn.ne.jp>");
309MODULE_DESCRIPTION("TX4939 internal RTC driver");
Alexandre Belloni1a9de212019-03-04 20:02:05 +0100310MODULE_LICENSE("GPL v2");
Atsushi Nemoto0e149232009-01-06 14:42:22 -0800311MODULE_ALIAS("platform:tx4939rtc");