1 // SPDX-License-Identifier: GPL-2.0
3 * TX4939 internal RTC driver
4 * Based on RBTX49xx patch from CELF patch archive.
6 * (C) Copyright TOSHIBA CORPORATION 2005-2007
9 #include <linux/platform_device.h>
10 #include <linux/interrupt.h>
11 #include <linux/module.h>
13 #include <linux/gfp.h>
15 #define TX4939_RTCCTL_ALME 0x00000080
16 #define TX4939_RTCCTL_ALMD 0x00000040
17 #define TX4939_RTCCTL_BUSY 0x00000020
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
26 #define TX4939_RTCTBC_PM 0x00000080
27 #define TX4939_RTCTBC_COMP 0x0000007f
29 #define TX4939_RTC_REG_RAMSIZE 0x00000100
30 #define TX4939_RTC_REG_RWBSIZE 0x00000006
32 struct tx4939_rtc_reg
{
39 struct tx4939rtc_plat_data
{
40 struct rtc_device
*rtc
;
41 struct tx4939_rtc_reg __iomem
*rtcreg
;
45 static int tx4939_rtc_cmd(struct tx4939_rtc_reg __iomem
*rtcreg
, int cmd
)
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) */
60 static int tx4939_rtc_set_time(struct device
*dev
, struct rtc_time
*tm
)
62 struct tx4939rtc_plat_data
*pdata
= dev_get_drvdata(dev
);
63 struct tx4939_rtc_reg __iomem
*rtcreg
= pdata
->rtcreg
;
64 unsigned long secs
= rtc_tm_to_time64(tm
);
74 spin_lock_irq(&pdata
->lock
);
75 __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
));
81 spin_unlock_irq(&pdata
->lock
);
85 static int tx4939_rtc_read_time(struct device
*dev
, struct rtc_time
*tm
)
87 struct tx4939rtc_plat_data
*pdata
= dev_get_drvdata(dev
);
88 struct tx4939_rtc_reg __iomem
*rtcreg
= pdata
->rtcreg
;
93 spin_lock_irq(&pdata
->lock
);
94 ret
= tx4939_rtc_cmd(rtcreg
,
95 TX4939_RTCCTL_COMMAND_GETTIME
|
96 (__raw_readl(&rtcreg
->ctl
) & TX4939_RTCCTL_ALME
));
98 spin_unlock_irq(&pdata
->lock
);
101 __raw_writel(2, &rtcreg
->adr
);
102 for (i
= 2; i
< 6; i
++)
103 buf
[i
] = __raw_readl(&rtcreg
->dat
);
104 spin_unlock_irq(&pdata
->lock
);
105 sec
= ((unsigned long)buf
[5] << 24) | (buf
[4] << 16) |
106 (buf
[3] << 8) | buf
[2];
107 rtc_time64_to_tm(sec
, tm
);
111 static int tx4939_rtc_set_alarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
113 struct tx4939rtc_plat_data
*pdata
= dev_get_drvdata(dev
);
114 struct tx4939_rtc_reg __iomem
*rtcreg
= pdata
->rtcreg
;
117 unsigned char buf
[6];
119 sec
= rtc_tm_to_time64(&alrm
->time
);
126 spin_lock_irq(&pdata
->lock
);
127 __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));
132 spin_unlock_irq(&pdata
->lock
);
136 static int tx4939_rtc_read_alarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
138 struct tx4939rtc_plat_data
*pdata
= dev_get_drvdata(dev
);
139 struct tx4939_rtc_reg __iomem
*rtcreg
= pdata
->rtcreg
;
142 unsigned char buf
[6];
145 spin_lock_irq(&pdata
->lock
);
146 ret
= tx4939_rtc_cmd(rtcreg
,
147 TX4939_RTCCTL_COMMAND_GETALARM
|
148 (__raw_readl(&rtcreg
->ctl
) & TX4939_RTCCTL_ALME
));
150 spin_unlock_irq(&pdata
->lock
);
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;
159 spin_unlock_irq(&pdata
->lock
);
160 sec
= ((unsigned long)buf
[5] << 24) | (buf
[4] << 16) |
161 (buf
[3] << 8) | buf
[2];
162 rtc_time64_to_tm(sec
, &alrm
->time
);
163 return rtc_valid_tm(&alrm
->time
);
166 static int tx4939_rtc_alarm_irq_enable(struct device
*dev
, unsigned int enabled
)
168 struct tx4939rtc_plat_data
*pdata
= dev_get_drvdata(dev
);
170 spin_lock_irq(&pdata
->lock
);
171 tx4939_rtc_cmd(pdata
->rtcreg
,
172 TX4939_RTCCTL_COMMAND_NOP
|
173 (enabled
? TX4939_RTCCTL_ALME
: 0));
174 spin_unlock_irq(&pdata
->lock
);
178 static irqreturn_t
tx4939_rtc_interrupt(int irq
, void *dev_id
)
180 struct tx4939rtc_plat_data
*pdata
= dev_get_drvdata(dev_id
);
181 struct tx4939_rtc_reg __iomem
*rtcreg
= pdata
->rtcreg
;
182 unsigned long events
= RTC_IRQF
;
184 spin_lock(&pdata
->lock
);
185 if (__raw_readl(&rtcreg
->ctl
) & TX4939_RTCCTL_ALMD
) {
187 tx4939_rtc_cmd(rtcreg
, TX4939_RTCCTL_COMMAND_NOP
);
189 spin_unlock(&pdata
->lock
);
190 rtc_update_irq(pdata
->rtc
, 1, events
);
195 static 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
,
199 .set_time
= tx4939_rtc_set_time
,
200 .alarm_irq_enable
= tx4939_rtc_alarm_irq_enable
,
203 static int tx4939_nvram_read(void *priv
, unsigned int pos
, void *val
,
206 struct tx4939rtc_plat_data
*pdata
= priv
;
207 struct tx4939_rtc_reg __iomem
*rtcreg
= pdata
->rtcreg
;
210 spin_lock_irq(&pdata
->lock
);
211 for (; bytes
; bytes
--) {
212 __raw_writel(pos
++, &rtcreg
->adr
);
213 *buf
++ = __raw_readl(&rtcreg
->dat
);
215 spin_unlock_irq(&pdata
->lock
);
219 static int tx4939_nvram_write(void *priv
, unsigned int pos
, void *val
,
222 struct tx4939rtc_plat_data
*pdata
= priv
;
223 struct tx4939_rtc_reg __iomem
*rtcreg
= pdata
->rtcreg
;
226 spin_lock_irq(&pdata
->lock
);
227 for (; bytes
; bytes
--) {
228 __raw_writel(pos
++, &rtcreg
->adr
);
229 __raw_writel(*buf
++, &rtcreg
->dat
);
231 spin_unlock_irq(&pdata
->lock
);
235 static int __init
tx4939_rtc_probe(struct platform_device
*pdev
)
237 struct rtc_device
*rtc
;
238 struct tx4939rtc_plat_data
*pdata
;
240 struct nvmem_config nvmem_cfg
= {
241 .name
= "tx4939_nvram",
242 .size
= TX4939_RTC_REG_RAMSIZE
,
243 .reg_read
= tx4939_nvram_read
,
244 .reg_write
= tx4939_nvram_write
,
247 irq
= platform_get_irq(pdev
, 0);
250 pdata
= devm_kzalloc(&pdev
->dev
, sizeof(*pdata
), GFP_KERNEL
);
253 platform_set_drvdata(pdev
, pdata
);
255 pdata
->rtcreg
= devm_platform_ioremap_resource(pdev
, 0);
256 if (IS_ERR(pdata
->rtcreg
))
257 return PTR_ERR(pdata
->rtcreg
);
259 spin_lock_init(&pdata
->lock
);
260 tx4939_rtc_cmd(pdata
->rtcreg
, TX4939_RTCCTL_COMMAND_NOP
);
261 if (devm_request_irq(&pdev
->dev
, irq
, tx4939_rtc_interrupt
,
262 0, pdev
->name
, &pdev
->dev
) < 0)
264 rtc
= devm_rtc_allocate_device(&pdev
->dev
);
268 rtc
->ops
= &tx4939_rtc_ops
;
269 rtc
->nvram_old_abi
= true;
270 rtc
->range_max
= U32_MAX
;
274 nvmem_cfg
.priv
= pdata
;
275 ret
= rtc_nvmem_register(rtc
, &nvmem_cfg
);
279 return rtc_register_device(rtc
);
282 static int __exit
tx4939_rtc_remove(struct platform_device
*pdev
)
284 struct tx4939rtc_plat_data
*pdata
= platform_get_drvdata(pdev
);
286 spin_lock_irq(&pdata
->lock
);
287 tx4939_rtc_cmd(pdata
->rtcreg
, TX4939_RTCCTL_COMMAND_NOP
);
288 spin_unlock_irq(&pdata
->lock
);
292 static struct platform_driver tx4939_rtc_driver
= {
293 .remove
= __exit_p(tx4939_rtc_remove
),
299 module_platform_driver_probe(tx4939_rtc_driver
, tx4939_rtc_probe
);
301 MODULE_AUTHOR("Atsushi Nemoto <anemo@mba.ocn.ne.jp>");
302 MODULE_DESCRIPTION("TX4939 internal RTC driver");
303 MODULE_LICENSE("GPL v2");
304 MODULE_ALIAS("platform:tx4939rtc");