^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) // Copyright 2015 IBM Corp.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/rtc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) struct aspeed_rtc {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) struct rtc_device *rtc_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) void __iomem *base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #define RTC_TIME 0x00
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #define RTC_YEAR 0x04
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #define RTC_CTRL 0x10
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #define RTC_UNLOCK BIT(1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #define RTC_ENABLE BIT(0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) static int aspeed_rtc_read_time(struct device *dev, struct rtc_time *tm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) struct aspeed_rtc *rtc = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) unsigned int cent, year;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) u32 reg1, reg2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) if (!(readl(rtc->base + RTC_CTRL) & RTC_ENABLE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) dev_dbg(dev, "%s failing as rtc disabled\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) reg2 = readl(rtc->base + RTC_YEAR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) reg1 = readl(rtc->base + RTC_TIME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) } while (reg2 != readl(rtc->base + RTC_YEAR));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) tm->tm_mday = (reg1 >> 24) & 0x1f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) tm->tm_hour = (reg1 >> 16) & 0x1f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) tm->tm_min = (reg1 >> 8) & 0x3f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) tm->tm_sec = (reg1 >> 0) & 0x3f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) cent = (reg2 >> 16) & 0x1f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) year = (reg2 >> 8) & 0x7f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) tm->tm_mon = ((reg2 >> 0) & 0x0f) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) tm->tm_year = year + (cent * 100) - 1900;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) dev_dbg(dev, "%s %ptR", __func__, tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) static int aspeed_rtc_set_time(struct device *dev, struct rtc_time *tm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) struct aspeed_rtc *rtc = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) u32 reg1, reg2, ctrl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) int year, cent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) cent = (tm->tm_year + 1900) / 100;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) year = tm->tm_year % 100;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) reg1 = (tm->tm_mday << 24) | (tm->tm_hour << 16) | (tm->tm_min << 8) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) tm->tm_sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) reg2 = ((cent & 0x1f) << 16) | ((year & 0x7f) << 8) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) ((tm->tm_mon + 1) & 0xf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) ctrl = readl(rtc->base + RTC_CTRL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) writel(ctrl | RTC_UNLOCK, rtc->base + RTC_CTRL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) writel(reg1, rtc->base + RTC_TIME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) writel(reg2, rtc->base + RTC_YEAR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) /* Re-lock and ensure enable is set now that a time is programmed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) writel(ctrl | RTC_ENABLE, rtc->base + RTC_CTRL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) static const struct rtc_class_ops aspeed_rtc_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) .read_time = aspeed_rtc_read_time,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) .set_time = aspeed_rtc_set_time,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) static int aspeed_rtc_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) struct aspeed_rtc *rtc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) rtc = devm_kzalloc(&pdev->dev, sizeof(*rtc), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) if (!rtc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) rtc->base = devm_platform_ioremap_resource(pdev, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) if (IS_ERR(rtc->base))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) return PTR_ERR(rtc->base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) rtc->rtc_dev = devm_rtc_allocate_device(&pdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) if (IS_ERR(rtc->rtc_dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) return PTR_ERR(rtc->rtc_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) platform_set_drvdata(pdev, rtc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) rtc->rtc_dev->ops = &aspeed_rtc_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) rtc->rtc_dev->range_min = RTC_TIMESTAMP_BEGIN_1900;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) rtc->rtc_dev->range_max = 38814989399LL; /* 3199-12-31 23:59:59 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) return rtc_register_device(rtc->rtc_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) static const struct of_device_id aspeed_rtc_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) { .compatible = "aspeed,ast2400-rtc", },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) { .compatible = "aspeed,ast2500-rtc", },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) { .compatible = "aspeed,ast2600-rtc", },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) {}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) MODULE_DEVICE_TABLE(of, aspeed_rtc_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) static struct platform_driver aspeed_rtc_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) .name = "aspeed-rtc",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) .of_match_table = of_match_ptr(aspeed_rtc_match),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) module_platform_driver_probe(aspeed_rtc_driver, aspeed_rtc_probe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) MODULE_DESCRIPTION("ASPEED RTC driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) MODULE_AUTHOR("Joel Stanley <joel@jms.id.au>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) MODULE_LICENSE("GPL");