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