brintos

brintos / linux-shallow public Read only

0
0
Text · 3.1 KiB · 880b015 Raw
130 lines · c
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 10struct aspeed_rtc {11	struct rtc_device *rtc_dev;12	void __iomem *base;13};14 15#define RTC_TIME	0x0016#define RTC_YEAR	0x0417#define RTC_CTRL	0x1018 19#define RTC_UNLOCK	BIT(1)20#define RTC_ENABLE	BIT(0)21 22static 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 53static 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 80static const struct rtc_class_ops aspeed_rtc_ops = {81	.read_time = aspeed_rtc_read_time,82	.set_time = aspeed_rtc_set_time,83};84 85static int aspeed_rtc_probe(struct platform_device *pdev)86{87	struct aspeed_rtc *rtc;88 89	rtc = devm_kzalloc(&pdev->dev, sizeof(*rtc), GFP_KERNEL);90	if (!rtc)91		return -ENOMEM;92 93	rtc->base = devm_platform_ioremap_resource(pdev, 0);94	if (IS_ERR(rtc->base))95		return PTR_ERR(rtc->base);96 97	rtc->rtc_dev = devm_rtc_allocate_device(&pdev->dev);98	if (IS_ERR(rtc->rtc_dev))99		return PTR_ERR(rtc->rtc_dev);100 101	platform_set_drvdata(pdev, rtc);102 103	rtc->rtc_dev->ops = &aspeed_rtc_ops;104	rtc->rtc_dev->range_min = RTC_TIMESTAMP_BEGIN_1900;105	rtc->rtc_dev->range_max = 38814989399LL; /* 3199-12-31 23:59:59 */106 107	return devm_rtc_register_device(rtc->rtc_dev);108}109 110static const struct of_device_id aspeed_rtc_match[] = {111	{ .compatible = "aspeed,ast2400-rtc", },112	{ .compatible = "aspeed,ast2500-rtc", },113	{ .compatible = "aspeed,ast2600-rtc", },114	{}115};116MODULE_DEVICE_TABLE(of, aspeed_rtc_match);117 118static struct platform_driver aspeed_rtc_driver = {119	.driver = {120		.name = "aspeed-rtc",121		.of_match_table = aspeed_rtc_match,122	},123};124 125module_platform_driver_probe(aspeed_rtc_driver, aspeed_rtc_probe);126 127MODULE_DESCRIPTION("ASPEED RTC driver");128MODULE_AUTHOR("Joel Stanley <joel@jms.id.au>");129MODULE_LICENSE("GPL");130