1184a182eSJoel Stanley // SPDX-License-Identifier: GPL-2.0+
2184a182eSJoel Stanley // Copyright 2015 IBM Corp.
3184a182eSJoel Stanley
4184a182eSJoel Stanley #include <linux/module.h>
5184a182eSJoel Stanley #include <linux/of.h>
6184a182eSJoel Stanley #include <linux/platform_device.h>
7184a182eSJoel Stanley #include <linux/rtc.h>
8184a182eSJoel Stanley #include <linux/io.h>
9184a182eSJoel Stanley
10184a182eSJoel Stanley struct aspeed_rtc {
11184a182eSJoel Stanley struct rtc_device *rtc_dev;
12184a182eSJoel Stanley void __iomem *base;
13184a182eSJoel Stanley };
14184a182eSJoel Stanley
15184a182eSJoel Stanley #define RTC_TIME 0x00
16184a182eSJoel Stanley #define RTC_YEAR 0x04
17184a182eSJoel Stanley #define RTC_CTRL 0x10
18184a182eSJoel Stanley
19184a182eSJoel Stanley #define RTC_UNLOCK BIT(1)
20184a182eSJoel Stanley #define RTC_ENABLE BIT(0)
21184a182eSJoel Stanley
aspeed_rtc_read_time(struct device * dev,struct rtc_time * tm)22184a182eSJoel Stanley static int aspeed_rtc_read_time(struct device *dev, struct rtc_time *tm)
23184a182eSJoel Stanley {
24184a182eSJoel Stanley struct aspeed_rtc *rtc = dev_get_drvdata(dev);
25184a182eSJoel Stanley unsigned int cent, year;
26184a182eSJoel Stanley u32 reg1, reg2;
27184a182eSJoel Stanley
28184a182eSJoel Stanley if (!(readl(rtc->base + RTC_CTRL) & RTC_ENABLE)) {
29184a182eSJoel Stanley dev_dbg(dev, "%s failing as rtc disabled\n", __func__);
30184a182eSJoel Stanley return -EINVAL;
31184a182eSJoel Stanley }
32184a182eSJoel Stanley
33184a182eSJoel Stanley do {
34184a182eSJoel Stanley reg2 = readl(rtc->base + RTC_YEAR);
35184a182eSJoel Stanley reg1 = readl(rtc->base + RTC_TIME);
36184a182eSJoel Stanley } while (reg2 != readl(rtc->base + RTC_YEAR));
37184a182eSJoel Stanley
38184a182eSJoel Stanley tm->tm_mday = (reg1 >> 24) & 0x1f;
39184a182eSJoel Stanley tm->tm_hour = (reg1 >> 16) & 0x1f;
40184a182eSJoel Stanley tm->tm_min = (reg1 >> 8) & 0x3f;
41184a182eSJoel Stanley tm->tm_sec = (reg1 >> 0) & 0x3f;
42184a182eSJoel Stanley
43184a182eSJoel Stanley cent = (reg2 >> 16) & 0x1f;
44184a182eSJoel Stanley year = (reg2 >> 8) & 0x7f;
45184a182eSJoel Stanley tm->tm_mon = ((reg2 >> 0) & 0x0f) - 1;
46184a182eSJoel Stanley tm->tm_year = year + (cent * 100) - 1900;
47184a182eSJoel Stanley
48184a182eSJoel Stanley dev_dbg(dev, "%s %ptR", __func__, tm);
49184a182eSJoel Stanley
50184a182eSJoel Stanley return 0;
51184a182eSJoel Stanley }
52184a182eSJoel Stanley
aspeed_rtc_set_time(struct device * dev,struct rtc_time * tm)53184a182eSJoel Stanley static int aspeed_rtc_set_time(struct device *dev, struct rtc_time *tm)
54184a182eSJoel Stanley {
55184a182eSJoel Stanley struct aspeed_rtc *rtc = dev_get_drvdata(dev);
56184a182eSJoel Stanley u32 reg1, reg2, ctrl;
57184a182eSJoel Stanley int year, cent;
58184a182eSJoel Stanley
59184a182eSJoel Stanley cent = (tm->tm_year + 1900) / 100;
60184a182eSJoel Stanley year = tm->tm_year % 100;
61184a182eSJoel Stanley
62184a182eSJoel Stanley reg1 = (tm->tm_mday << 24) | (tm->tm_hour << 16) | (tm->tm_min << 8) |
63184a182eSJoel Stanley tm->tm_sec;
64184a182eSJoel Stanley
65184a182eSJoel Stanley reg2 = ((cent & 0x1f) << 16) | ((year & 0x7f) << 8) |
66184a182eSJoel Stanley ((tm->tm_mon + 1) & 0xf);
67184a182eSJoel Stanley
68184a182eSJoel Stanley ctrl = readl(rtc->base + RTC_CTRL);
69184a182eSJoel Stanley writel(ctrl | RTC_UNLOCK, rtc->base + RTC_CTRL);
70184a182eSJoel Stanley
71184a182eSJoel Stanley writel(reg1, rtc->base + RTC_TIME);
72184a182eSJoel Stanley writel(reg2, rtc->base + RTC_YEAR);
73184a182eSJoel Stanley
74184a182eSJoel Stanley /* Re-lock and ensure enable is set now that a time is programmed */
75184a182eSJoel Stanley writel(ctrl | RTC_ENABLE, rtc->base + RTC_CTRL);
76184a182eSJoel Stanley
77184a182eSJoel Stanley return 0;
78184a182eSJoel Stanley }
79184a182eSJoel Stanley
80184a182eSJoel Stanley static const struct rtc_class_ops aspeed_rtc_ops = {
81184a182eSJoel Stanley .read_time = aspeed_rtc_read_time,
82184a182eSJoel Stanley .set_time = aspeed_rtc_set_time,
83184a182eSJoel Stanley };
84184a182eSJoel Stanley
aspeed_rtc_probe(struct platform_device * pdev)85184a182eSJoel Stanley static int aspeed_rtc_probe(struct platform_device *pdev)
86184a182eSJoel Stanley {
87184a182eSJoel Stanley struct aspeed_rtc *rtc;
88184a182eSJoel Stanley
89184a182eSJoel Stanley rtc = devm_kzalloc(&pdev->dev, sizeof(*rtc), GFP_KERNEL);
90184a182eSJoel Stanley if (!rtc)
91184a182eSJoel Stanley return -ENOMEM;
92184a182eSJoel Stanley
9309ef18bcSYueHaibing rtc->base = devm_platform_ioremap_resource(pdev, 0);
94184a182eSJoel Stanley if (IS_ERR(rtc->base))
95184a182eSJoel Stanley return PTR_ERR(rtc->base);
96184a182eSJoel Stanley
97184a182eSJoel Stanley rtc->rtc_dev = devm_rtc_allocate_device(&pdev->dev);
98184a182eSJoel Stanley if (IS_ERR(rtc->rtc_dev))
99184a182eSJoel Stanley return PTR_ERR(rtc->rtc_dev);
100184a182eSJoel Stanley
101184a182eSJoel Stanley platform_set_drvdata(pdev, rtc);
102184a182eSJoel Stanley
103184a182eSJoel Stanley rtc->rtc_dev->ops = &aspeed_rtc_ops;
104184a182eSJoel Stanley rtc->rtc_dev->range_min = RTC_TIMESTAMP_BEGIN_1900;
105184a182eSJoel Stanley rtc->rtc_dev->range_max = 38814989399LL; /* 3199-12-31 23:59:59 */
106184a182eSJoel Stanley
107fdcfd854SBartosz Golaszewski return devm_rtc_register_device(rtc->rtc_dev);
108184a182eSJoel Stanley }
109184a182eSJoel Stanley
110184a182eSJoel Stanley static const struct of_device_id aspeed_rtc_match[] = {
111184a182eSJoel Stanley { .compatible = "aspeed,ast2400-rtc", },
112184a182eSJoel Stanley { .compatible = "aspeed,ast2500-rtc", },
113184a182eSJoel Stanley { .compatible = "aspeed,ast2600-rtc", },
114184a182eSJoel Stanley {}
115184a182eSJoel Stanley };
116184a182eSJoel Stanley MODULE_DEVICE_TABLE(of, aspeed_rtc_match);
117184a182eSJoel Stanley
118184a182eSJoel Stanley static struct platform_driver aspeed_rtc_driver = {
119184a182eSJoel Stanley .driver = {
120184a182eSJoel Stanley .name = "aspeed-rtc",
121*4f3688dcSZhu Wang .of_match_table = aspeed_rtc_match,
122184a182eSJoel Stanley },
123184a182eSJoel Stanley };
124184a182eSJoel Stanley
125184a182eSJoel Stanley module_platform_driver_probe(aspeed_rtc_driver, aspeed_rtc_probe);
126184a182eSJoel Stanley
127184a182eSJoel Stanley MODULE_DESCRIPTION("ASPEED RTC driver");
128184a182eSJoel Stanley MODULE_AUTHOR("Joel Stanley <joel@jms.id.au>");
129184a182eSJoel Stanley MODULE_LICENSE("GPL");
130