1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (c) 2011 Zhao Zhang <zhzhl555@gmail.com>
5 * Derived from driver/rtc/rtc-au1xxx.c
8 #include <linux/module.h>
9 #include <linux/kernel.h>
10 #include <linux/rtc.h>
11 #include <linux/init.h>
12 #include <linux/platform_device.h>
13 #include <linux/delay.h>
14 #include <linux/types.h>
16 #include <loongson1.h>
18 #define LS1X_RTC_REG_OFFSET (LS1X_RTC_BASE + 0x20)
19 #define LS1X_RTC_REGS(x) \
20 ((void __iomem *)KSEG1ADDR(LS1X_RTC_REG_OFFSET + (x)))
22 /*RTC programmable counters 0 and 1*/
23 #define SYS_COUNTER_CNTRL (LS1X_RTC_REGS(0x20))
24 #define SYS_CNTRL_ERS (1 << 23)
25 #define SYS_CNTRL_RTS (1 << 20)
26 #define SYS_CNTRL_RM2 (1 << 19)
27 #define SYS_CNTRL_RM1 (1 << 18)
28 #define SYS_CNTRL_RM0 (1 << 17)
29 #define SYS_CNTRL_RS (1 << 16)
30 #define SYS_CNTRL_BP (1 << 14)
31 #define SYS_CNTRL_REN (1 << 13)
32 #define SYS_CNTRL_BRT (1 << 12)
33 #define SYS_CNTRL_TEN (1 << 11)
34 #define SYS_CNTRL_BTT (1 << 10)
35 #define SYS_CNTRL_E0 (1 << 8)
36 #define SYS_CNTRL_ETS (1 << 7)
37 #define SYS_CNTRL_32S (1 << 5)
38 #define SYS_CNTRL_TTS (1 << 4)
39 #define SYS_CNTRL_TM2 (1 << 3)
40 #define SYS_CNTRL_TM1 (1 << 2)
41 #define SYS_CNTRL_TM0 (1 << 1)
42 #define SYS_CNTRL_TS (1 << 0)
44 /* Programmable Counter 0 Registers */
45 #define SYS_TOYTRIM (LS1X_RTC_REGS(0))
46 #define SYS_TOYWRITE0 (LS1X_RTC_REGS(4))
47 #define SYS_TOYWRITE1 (LS1X_RTC_REGS(8))
48 #define SYS_TOYREAD0 (LS1X_RTC_REGS(0xC))
49 #define SYS_TOYREAD1 (LS1X_RTC_REGS(0x10))
50 #define SYS_TOYMATCH0 (LS1X_RTC_REGS(0x14))
51 #define SYS_TOYMATCH1 (LS1X_RTC_REGS(0x18))
52 #define SYS_TOYMATCH2 (LS1X_RTC_REGS(0x1C))
54 /* Programmable Counter 1 Registers */
55 #define SYS_RTCTRIM (LS1X_RTC_REGS(0x40))
56 #define SYS_RTCWRITE0 (LS1X_RTC_REGS(0x44))
57 #define SYS_RTCREAD0 (LS1X_RTC_REGS(0x48))
58 #define SYS_RTCMATCH0 (LS1X_RTC_REGS(0x4C))
59 #define SYS_RTCMATCH1 (LS1X_RTC_REGS(0x50))
60 #define SYS_RTCMATCH2 (LS1X_RTC_REGS(0x54))
62 #define LS1X_SEC_OFFSET (4)
63 #define LS1X_MIN_OFFSET (10)
64 #define LS1X_HOUR_OFFSET (16)
65 #define LS1X_DAY_OFFSET (21)
66 #define LS1X_MONTH_OFFSET (26)
69 #define LS1X_SEC_MASK (0x3f)
70 #define LS1X_MIN_MASK (0x3f)
71 #define LS1X_HOUR_MASK (0x1f)
72 #define LS1X_DAY_MASK (0x1f)
73 #define LS1X_MONTH_MASK (0x3f)
74 #define LS1X_YEAR_MASK (0xffffffff)
76 #define ls1x_get_sec(t) (((t) >> LS1X_SEC_OFFSET) & LS1X_SEC_MASK)
77 #define ls1x_get_min(t) (((t) >> LS1X_MIN_OFFSET) & LS1X_MIN_MASK)
78 #define ls1x_get_hour(t) (((t) >> LS1X_HOUR_OFFSET) & LS1X_HOUR_MASK)
79 #define ls1x_get_day(t) (((t) >> LS1X_DAY_OFFSET) & LS1X_DAY_MASK)
80 #define ls1x_get_month(t) (((t) >> LS1X_MONTH_OFFSET) & LS1X_MONTH_MASK)
82 #define RTC_CNTR_OK (SYS_CNTRL_E0 | SYS_CNTRL_32S)
84 static int ls1x_rtc_read_time(struct device *dev, struct rtc_time *rtm)
89 v = readl(SYS_TOYREAD0);
90 t = readl(SYS_TOYREAD1);
92 memset(rtm, 0, sizeof(struct rtc_time));
93 t = mktime64((t & LS1X_YEAR_MASK), ls1x_get_month(v),
94 ls1x_get_day(v), ls1x_get_hour(v),
95 ls1x_get_min(v), ls1x_get_sec(v));
96 rtc_time64_to_tm(t, rtm);
101 static int ls1x_rtc_set_time(struct device *dev, struct rtc_time *rtm)
103 unsigned long v, t, c;
104 int ret = -ETIMEDOUT;
106 v = ((rtm->tm_mon + 1) << LS1X_MONTH_OFFSET)
107 | (rtm->tm_mday << LS1X_DAY_OFFSET)
108 | (rtm->tm_hour << LS1X_HOUR_OFFSET)
109 | (rtm->tm_min << LS1X_MIN_OFFSET)
110 | (rtm->tm_sec << LS1X_SEC_OFFSET);
112 writel(v, SYS_TOYWRITE0);
114 /* add timeout check counter, for more safe */
115 while ((readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_TS) && --c)
116 usleep_range(1000, 3000);
119 dev_err(dev, "set time timeout!\n");
123 t = rtm->tm_year + 1900;
124 writel(t, SYS_TOYWRITE1);
126 while ((readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_TS) && --c)
127 usleep_range(1000, 3000);
130 dev_err(dev, "set time timeout!\n");
138 static const struct rtc_class_ops ls1x_rtc_ops = {
139 .read_time = ls1x_rtc_read_time,
140 .set_time = ls1x_rtc_set_time,
143 static int ls1x_rtc_probe(struct platform_device *pdev)
145 struct rtc_device *rtcdev;
148 v = readl(SYS_COUNTER_CNTRL);
149 if (!(v & RTC_CNTR_OK)) {
150 dev_err(&pdev->dev, "rtc counters not working\n");
154 /* set to 1 HZ if needed */
155 if (readl(SYS_TOYTRIM) != 32767) {
157 while ((readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_TTS) && --v)
158 usleep_range(1000, 3000);
161 dev_err(&pdev->dev, "time out\n");
164 writel(32767, SYS_TOYTRIM);
166 /* this loop coundn't be endless */
167 while (readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_TTS)
168 usleep_range(1000, 3000);
170 rtcdev = devm_rtc_allocate_device(&pdev->dev);
172 return PTR_ERR(rtcdev);
174 platform_set_drvdata(pdev, rtcdev);
175 rtcdev->ops = &ls1x_rtc_ops;
176 rtcdev->range_min = RTC_TIMESTAMP_BEGIN_1900;
177 rtcdev->range_max = RTC_TIMESTAMP_END_2099;
179 return devm_rtc_register_device(rtcdev);
182 static struct platform_driver ls1x_rtc_driver = {
186 .probe = ls1x_rtc_probe,
189 module_platform_driver(ls1x_rtc_driver);
191 MODULE_AUTHOR("zhao zhang <zhzhl555@gmail.com>");
192 MODULE_LICENSE("GPL");