2 * TX4939 internal RTC driver
3 * Based on RBTX49xx patch from CELF patch archive.
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
9 * (C) Copyright TOSHIBA CORPORATION 2005-2007
11 #include <linux/rtc.h>
12 #include <linux/platform_device.h>
13 #include <linux/interrupt.h>
14 #include <linux/module.h>
16 #include <linux/gfp.h>
18 #define TX4939_RTCCTL_ALME 0x00000080
19 #define TX4939_RTCCTL_ALMD 0x00000040
20 #define TX4939_RTCCTL_BUSY 0x00000020
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
29 #define TX4939_RTCTBC_PM 0x00000080
30 #define TX4939_RTCTBC_COMP 0x0000007f
32 #define TX4939_RTC_REG_RAMSIZE 0x00000100
33 #define TX4939_RTC_REG_RWBSIZE 0x00000006
35 struct tx4939_rtc_reg
{
42 struct tx4939rtc_plat_data
{
43 struct rtc_device
*rtc
;
44 struct tx4939_rtc_reg __iomem
*rtcreg
;
48 static struct tx4939rtc_plat_data
*get_tx4939rtc_plat_data(struct device
*dev
)
50 return platform_get_drvdata(to_platform_device(dev
));
53 static int tx4939_rtc_cmd(struct tx4939_rtc_reg __iomem
*rtcreg
, int cmd
)
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) */
68 static int tx4939_rtc_set_mmss(struct device
*dev
, unsigned long secs
)
70 struct tx4939rtc_plat_data
*pdata
= get_tx4939rtc_plat_data(dev
);
71 struct tx4939_rtc_reg __iomem
*rtcreg
= pdata
->rtcreg
;
81 spin_lock_irq(&pdata
->lock
);
82 __raw_writel(0, &rtcreg
->adr
);
83 for (i
= 0; i
< 6; i
++)
84 __raw_writel(buf
[i
], &rtcreg
->dat
);
85 ret
= tx4939_rtc_cmd(rtcreg
,
86 TX4939_RTCCTL_COMMAND_SETTIME
|
87 (__raw_readl(&rtcreg
->ctl
) & TX4939_RTCCTL_ALME
));
88 spin_unlock_irq(&pdata
->lock
);
92 static int tx4939_rtc_read_time(struct device
*dev
, struct rtc_time
*tm
)
94 struct tx4939rtc_plat_data
*pdata
= get_tx4939rtc_plat_data(dev
);
95 struct tx4939_rtc_reg __iomem
*rtcreg
= pdata
->rtcreg
;
100 spin_lock_irq(&pdata
->lock
);
101 ret
= tx4939_rtc_cmd(rtcreg
,
102 TX4939_RTCCTL_COMMAND_GETTIME
|
103 (__raw_readl(&rtcreg
->ctl
) & TX4939_RTCCTL_ALME
));
105 spin_unlock_irq(&pdata
->lock
);
108 __raw_writel(2, &rtcreg
->adr
);
109 for (i
= 2; i
< 6; i
++)
110 buf
[i
] = __raw_readl(&rtcreg
->dat
);
111 spin_unlock_irq(&pdata
->lock
);
112 sec
= ((unsigned long)buf
[5] << 24) | (buf
[4] << 16) |
113 (buf
[3] << 8) | buf
[2];
114 rtc_time_to_tm(sec
, tm
);
118 static int tx4939_rtc_set_alarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
120 struct tx4939rtc_plat_data
*pdata
= get_tx4939rtc_plat_data(dev
);
121 struct tx4939_rtc_reg __iomem
*rtcreg
= pdata
->rtcreg
;
124 unsigned char buf
[6];
126 if (alrm
->time
.tm_sec
< 0 ||
127 alrm
->time
.tm_min
< 0 ||
128 alrm
->time
.tm_hour
< 0 ||
129 alrm
->time
.tm_mday
< 0 ||
130 alrm
->time
.tm_mon
< 0 ||
131 alrm
->time
.tm_year
< 0)
133 rtc_tm_to_time(&alrm
->time
, &sec
);
140 spin_lock_irq(&pdata
->lock
);
141 __raw_writel(0, &rtcreg
->adr
);
142 for (i
= 0; i
< 6; i
++)
143 __raw_writel(buf
[i
], &rtcreg
->dat
);
144 ret
= tx4939_rtc_cmd(rtcreg
, TX4939_RTCCTL_COMMAND_SETALARM
|
145 (alrm
->enabled
? TX4939_RTCCTL_ALME
: 0));
146 spin_unlock_irq(&pdata
->lock
);
150 static int tx4939_rtc_read_alarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
152 struct tx4939rtc_plat_data
*pdata
= get_tx4939rtc_plat_data(dev
);
153 struct tx4939_rtc_reg __iomem
*rtcreg
= pdata
->rtcreg
;
156 unsigned char buf
[6];
159 spin_lock_irq(&pdata
->lock
);
160 ret
= tx4939_rtc_cmd(rtcreg
,
161 TX4939_RTCCTL_COMMAND_GETALARM
|
162 (__raw_readl(&rtcreg
->ctl
) & TX4939_RTCCTL_ALME
));
164 spin_unlock_irq(&pdata
->lock
);
167 __raw_writel(2, &rtcreg
->adr
);
168 for (i
= 2; i
< 6; i
++)
169 buf
[i
] = __raw_readl(&rtcreg
->dat
);
170 ctl
= __raw_readl(&rtcreg
->ctl
);
171 alrm
->enabled
= (ctl
& TX4939_RTCCTL_ALME
) ? 1 : 0;
172 alrm
->pending
= (ctl
& TX4939_RTCCTL_ALMD
) ? 1 : 0;
173 spin_unlock_irq(&pdata
->lock
);
174 sec
= ((unsigned long)buf
[5] << 24) | (buf
[4] << 16) |
175 (buf
[3] << 8) | buf
[2];
176 rtc_time_to_tm(sec
, &alrm
->time
);
177 return rtc_valid_tm(&alrm
->time
);
180 static int tx4939_rtc_alarm_irq_enable(struct device
*dev
, unsigned int enabled
)
182 struct tx4939rtc_plat_data
*pdata
= get_tx4939rtc_plat_data(dev
);
184 spin_lock_irq(&pdata
->lock
);
185 tx4939_rtc_cmd(pdata
->rtcreg
,
186 TX4939_RTCCTL_COMMAND_NOP
|
187 (enabled
? TX4939_RTCCTL_ALME
: 0));
188 spin_unlock_irq(&pdata
->lock
);
192 static irqreturn_t
tx4939_rtc_interrupt(int irq
, void *dev_id
)
194 struct tx4939rtc_plat_data
*pdata
= get_tx4939rtc_plat_data(dev_id
);
195 struct tx4939_rtc_reg __iomem
*rtcreg
= pdata
->rtcreg
;
196 unsigned long events
= RTC_IRQF
;
198 spin_lock(&pdata
->lock
);
199 if (__raw_readl(&rtcreg
->ctl
) & TX4939_RTCCTL_ALMD
) {
201 tx4939_rtc_cmd(rtcreg
, TX4939_RTCCTL_COMMAND_NOP
);
203 spin_unlock(&pdata
->lock
);
204 rtc_update_irq(pdata
->rtc
, 1, events
);
209 static const struct rtc_class_ops tx4939_rtc_ops
= {
210 .read_time
= tx4939_rtc_read_time
,
211 .read_alarm
= tx4939_rtc_read_alarm
,
212 .set_alarm
= tx4939_rtc_set_alarm
,
213 .set_mmss
= tx4939_rtc_set_mmss
,
214 .alarm_irq_enable
= tx4939_rtc_alarm_irq_enable
,
217 static int tx4939_nvram_read(void *priv
, unsigned int pos
, void *val
,
220 struct tx4939rtc_plat_data
*pdata
= priv
;
221 struct tx4939_rtc_reg __iomem
*rtcreg
= pdata
->rtcreg
;
224 spin_lock_irq(&pdata
->lock
);
225 for (; bytes
; bytes
--) {
226 __raw_writel(pos
++, &rtcreg
->adr
);
227 *buf
++ = __raw_readl(&rtcreg
->dat
);
229 spin_unlock_irq(&pdata
->lock
);
233 static int tx4939_nvram_write(void *priv
, unsigned int pos
, void *val
,
236 struct tx4939rtc_plat_data
*pdata
= priv
;
237 struct tx4939_rtc_reg __iomem
*rtcreg
= pdata
->rtcreg
;
240 spin_lock_irq(&pdata
->lock
);
241 for (; bytes
; bytes
--) {
242 __raw_writel(pos
++, &rtcreg
->adr
);
243 __raw_writel(*buf
++, &rtcreg
->dat
);
245 spin_unlock_irq(&pdata
->lock
);
249 static int __init
tx4939_rtc_probe(struct platform_device
*pdev
)
251 struct rtc_device
*rtc
;
252 struct tx4939rtc_plat_data
*pdata
;
253 struct resource
*res
;
255 struct nvmem_config nvmem_cfg
= {
256 .name
= "tx4939_nvram",
257 .size
= TX4939_RTC_REG_RAMSIZE
,
258 .reg_read
= tx4939_nvram_read
,
259 .reg_write
= tx4939_nvram_write
,
262 irq
= platform_get_irq(pdev
, 0);
265 pdata
= devm_kzalloc(&pdev
->dev
, sizeof(*pdata
), GFP_KERNEL
);
268 platform_set_drvdata(pdev
, pdata
);
270 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
271 pdata
->rtcreg
= devm_ioremap_resource(&pdev
->dev
, res
);
272 if (IS_ERR(pdata
->rtcreg
))
273 return PTR_ERR(pdata
->rtcreg
);
275 spin_lock_init(&pdata
->lock
);
276 tx4939_rtc_cmd(pdata
->rtcreg
, TX4939_RTCCTL_COMMAND_NOP
);
277 if (devm_request_irq(&pdev
->dev
, irq
, tx4939_rtc_interrupt
,
278 0, pdev
->name
, &pdev
->dev
) < 0)
280 rtc
= devm_rtc_allocate_device(&pdev
->dev
);
284 rtc
->ops
= &tx4939_rtc_ops
;
285 rtc
->nvram_old_abi
= true;
289 nvmem_cfg
.priv
= pdata
;
290 ret
= rtc_nvmem_register(rtc
, &nvmem_cfg
);
294 return rtc_register_device(rtc
);
297 static int __exit
tx4939_rtc_remove(struct platform_device
*pdev
)
299 struct tx4939rtc_plat_data
*pdata
= platform_get_drvdata(pdev
);
301 spin_lock_irq(&pdata
->lock
);
302 tx4939_rtc_cmd(pdata
->rtcreg
, TX4939_RTCCTL_COMMAND_NOP
);
303 spin_unlock_irq(&pdata
->lock
);
307 static struct platform_driver tx4939_rtc_driver
= {
308 .remove
= __exit_p(tx4939_rtc_remove
),
314 module_platform_driver_probe(tx4939_rtc_driver
, tx4939_rtc_probe
);
316 MODULE_AUTHOR("Atsushi Nemoto <anemo@mba.ocn.ne.jp>");
317 MODULE_DESCRIPTION("TX4939 internal RTC driver");
318 MODULE_LICENSE("GPL");
319 MODULE_ALIAS("platform:tx4939rtc");