2 * Au1xxx counter0 (aka Time-Of-Year counter) RTC interface driver.
4 * Copyright (C) 2008 Manuel Lauss <mano@roarinelk.homelinux.net>
6 * This file is subject to the terms and conditions of the GNU General Public
7 * License. See the file "COPYING" in the main directory of this archive
11 /* All current Au1xxx SoCs have 2 counters fed by an external 32.768 kHz
12 * crystal. Counter 0, which keeps counting during sleep/powerdown, is
13 * used to count seconds since the beginning of the unix epoch.
15 * The counters must be configured and enabled by bootloader/board code;
16 * no checks as to whether they really get a proper 32.768kHz clock are
17 * made as this would take far too long.
20 #include <linux/module.h>
21 #include <linux/kernel.h>
22 #include <linux/rtc.h>
23 #include <linux/init.h>
24 #include <linux/platform_device.h>
26 #include <asm/mach-au1x00/au1000.h>
28 /* 32kHz clock enabled and detected */
29 #define CNTR_OK (SYS_CNTRL_E0 | SYS_CNTRL_32S)
31 static int au1xtoy_rtc_read_time(struct device
*dev
, struct rtc_time
*tm
)
35 t
= au_readl(SYS_TOYREAD
);
37 rtc_time_to_tm(t
, tm
);
39 return rtc_valid_tm(tm
);
42 static int au1xtoy_rtc_set_time(struct device
*dev
, struct rtc_time
*tm
)
46 rtc_tm_to_time(tm
, &t
);
48 au_writel(t
, SYS_TOYWRITE
);
51 /* wait for the pending register write to succeed. This can
52 * take up to 6 seconds...
54 while (au_readl(SYS_COUNTER_CNTRL
) & SYS_CNTRL_C0S
)
60 static struct rtc_class_ops au1xtoy_rtc_ops
= {
61 .read_time
= au1xtoy_rtc_read_time
,
62 .set_time
= au1xtoy_rtc_set_time
,
65 static int au1xtoy_rtc_probe(struct platform_device
*pdev
)
67 struct rtc_device
*rtcdev
;
71 t
= au_readl(SYS_COUNTER_CNTRL
);
73 dev_err(&pdev
->dev
, "counters not working; aborting.\n");
80 /* set counter0 tickrate to 1Hz if necessary */
81 if (au_readl(SYS_TOYTRIM
) != 32767) {
82 /* wait until hardware gives access to TRIM register */
84 while ((au_readl(SYS_COUNTER_CNTRL
) & SYS_CNTRL_T0S
) && --t
)
88 /* timed out waiting for register access; assume
89 * counters are unusable.
91 dev_err(&pdev
->dev
, "timeout waiting for access\n");
95 /* set 1Hz TOY tick rate */
96 au_writel(32767, SYS_TOYTRIM
);
100 /* wait until the hardware allows writes to the counter reg */
101 while (au_readl(SYS_COUNTER_CNTRL
) & SYS_CNTRL_C0S
)
104 rtcdev
= devm_rtc_device_register(&pdev
->dev
, "rtc-au1xxx",
105 &au1xtoy_rtc_ops
, THIS_MODULE
);
106 if (IS_ERR(rtcdev
)) {
107 ret
= PTR_ERR(rtcdev
);
111 platform_set_drvdata(pdev
, rtcdev
);
119 static int au1xtoy_rtc_remove(struct platform_device
*pdev
)
121 platform_set_drvdata(pdev
, NULL
);
126 static struct platform_driver au1xrtc_driver
= {
128 .name
= "rtc-au1xxx",
129 .owner
= THIS_MODULE
,
131 .remove
= au1xtoy_rtc_remove
,
134 module_platform_driver_probe(au1xrtc_driver
, au1xtoy_rtc_probe
);
136 MODULE_DESCRIPTION("Au1xxx TOY-counter-based RTC driver");
137 MODULE_AUTHOR("Manuel Lauss <manuel.lauss@gmail.com>");
138 MODULE_LICENSE("GPL");
139 MODULE_ALIAS("platform:rtc-au1xxx");