1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17#include <linux/module.h>
18#include <linux/kernel.h>
19#include <linux/rtc.h>
20#include <linux/init.h>
21#include <linux/platform_device.h>
22#include <linux/io.h>
23#include <asm/mach-au1x00/au1000.h>
24
25
26#define CNTR_OK (SYS_CNTRL_E0 | SYS_CNTRL_32S)
27
28static int au1xtoy_rtc_read_time(struct device *dev, struct rtc_time *tm)
29{
30 unsigned long t;
31
32 t = alchemy_rdsys(AU1000_SYS_TOYREAD);
33
34 rtc_time64_to_tm(t, tm);
35
36 return 0;
37}
38
39static int au1xtoy_rtc_set_time(struct device *dev, struct rtc_time *tm)
40{
41 unsigned long t;
42
43 t = rtc_tm_to_time64(tm);
44
45 alchemy_wrsys(t, AU1000_SYS_TOYWRITE);
46
47
48
49
50 while (alchemy_rdsys(AU1000_SYS_CNTRCTRL) & SYS_CNTRL_C0S)
51 msleep(1);
52
53 return 0;
54}
55
56static const struct rtc_class_ops au1xtoy_rtc_ops = {
57 .read_time = au1xtoy_rtc_read_time,
58 .set_time = au1xtoy_rtc_set_time,
59};
60
61static int au1xtoy_rtc_probe(struct platform_device *pdev)
62{
63 struct rtc_device *rtcdev;
64 unsigned long t;
65
66 t = alchemy_rdsys(AU1000_SYS_CNTRCTRL);
67 if (!(t & CNTR_OK)) {
68 dev_err(&pdev->dev, "counters not working; aborting.\n");
69 return -ENODEV;
70 }
71
72
73 if (alchemy_rdsys(AU1000_SYS_TOYTRIM) != 32767) {
74
75 t = 0x00100000;
76 while ((alchemy_rdsys(AU1000_SYS_CNTRCTRL) & SYS_CNTRL_T0S) && --t)
77 msleep(1);
78
79 if (!t) {
80
81
82
83 dev_err(&pdev->dev, "timeout waiting for access\n");
84 return -ETIMEDOUT;
85 }
86
87
88 alchemy_wrsys(32767, AU1000_SYS_TOYTRIM);
89 }
90
91
92 while (alchemy_rdsys(AU1000_SYS_CNTRCTRL) & SYS_CNTRL_C0S)
93 msleep(1);
94
95 rtcdev = devm_rtc_allocate_device(&pdev->dev);
96 if (IS_ERR(rtcdev))
97 return PTR_ERR(rtcdev);
98
99 rtcdev->ops = &au1xtoy_rtc_ops;
100 rtcdev->range_max = U32_MAX;
101
102 platform_set_drvdata(pdev, rtcdev);
103
104 return devm_rtc_register_device(rtcdev);
105}
106
107static struct platform_driver au1xrtc_driver = {
108 .driver = {
109 .name = "rtc-au1xxx",
110 },
111};
112
113module_platform_driver_probe(au1xrtc_driver, au1xtoy_rtc_probe);
114
115MODULE_DESCRIPTION("Au1xxx TOY-counter-based RTC driver");
116MODULE_AUTHOR("Manuel Lauss <manuel.lauss@gmail.com>");
117MODULE_LICENSE("GPL");
118MODULE_ALIAS("platform:rtc-au1xxx");
119