0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017 #include <linux/module.h>
0018 #include <linux/kernel.h>
0019 #include <linux/rtc.h>
0020 #include <linux/init.h>
0021 #include <linux/platform_device.h>
0022 #include <linux/io.h>
0023 #include <asm/mach-au1x00/au1000.h>
0024
0025
0026 #define CNTR_OK (SYS_CNTRL_E0 | SYS_CNTRL_32S)
0027
0028 static int au1xtoy_rtc_read_time(struct device *dev, struct rtc_time *tm)
0029 {
0030 unsigned long t;
0031
0032 t = alchemy_rdsys(AU1000_SYS_TOYREAD);
0033
0034 rtc_time64_to_tm(t, tm);
0035
0036 return 0;
0037 }
0038
0039 static int au1xtoy_rtc_set_time(struct device *dev, struct rtc_time *tm)
0040 {
0041 unsigned long t;
0042
0043 t = rtc_tm_to_time64(tm);
0044
0045 alchemy_wrsys(t, AU1000_SYS_TOYWRITE);
0046
0047
0048
0049
0050 while (alchemy_rdsys(AU1000_SYS_CNTRCTRL) & SYS_CNTRL_C0S)
0051 msleep(1);
0052
0053 return 0;
0054 }
0055
0056 static const struct rtc_class_ops au1xtoy_rtc_ops = {
0057 .read_time = au1xtoy_rtc_read_time,
0058 .set_time = au1xtoy_rtc_set_time,
0059 };
0060
0061 static int au1xtoy_rtc_probe(struct platform_device *pdev)
0062 {
0063 struct rtc_device *rtcdev;
0064 unsigned long t;
0065
0066 t = alchemy_rdsys(AU1000_SYS_CNTRCTRL);
0067 if (!(t & CNTR_OK)) {
0068 dev_err(&pdev->dev, "counters not working; aborting.\n");
0069 return -ENODEV;
0070 }
0071
0072
0073 if (alchemy_rdsys(AU1000_SYS_TOYTRIM) != 32767) {
0074
0075 t = 0x00100000;
0076 while ((alchemy_rdsys(AU1000_SYS_CNTRCTRL) & SYS_CNTRL_T0S) && --t)
0077 msleep(1);
0078
0079 if (!t) {
0080
0081
0082
0083 dev_err(&pdev->dev, "timeout waiting for access\n");
0084 return -ETIMEDOUT;
0085 }
0086
0087
0088 alchemy_wrsys(32767, AU1000_SYS_TOYTRIM);
0089 }
0090
0091
0092 while (alchemy_rdsys(AU1000_SYS_CNTRCTRL) & SYS_CNTRL_C0S)
0093 msleep(1);
0094
0095 rtcdev = devm_rtc_allocate_device(&pdev->dev);
0096 if (IS_ERR(rtcdev))
0097 return PTR_ERR(rtcdev);
0098
0099 rtcdev->ops = &au1xtoy_rtc_ops;
0100 rtcdev->range_max = U32_MAX;
0101
0102 platform_set_drvdata(pdev, rtcdev);
0103
0104 return devm_rtc_register_device(rtcdev);
0105 }
0106
0107 static struct platform_driver au1xrtc_driver = {
0108 .driver = {
0109 .name = "rtc-au1xxx",
0110 },
0111 };
0112
0113 module_platform_driver_probe(au1xrtc_driver, au1xtoy_rtc_probe);
0114
0115 MODULE_DESCRIPTION("Au1xxx TOY-counter-based RTC driver");
0116 MODULE_AUTHOR("Manuel Lauss <manuel.lauss@gmail.com>");
0117 MODULE_LICENSE("GPL");
0118 MODULE_ALIAS("platform:rtc-au1xxx");