xref: /linux/drivers/rtc/rtc-opal.c (revision f4a2eecb3ff9f51b179b213e7cc3766f920f2dc5)
116b1d26eSNeelesh Gupta /*
216b1d26eSNeelesh Gupta  * IBM OPAL RTC driver
316b1d26eSNeelesh Gupta  * Copyright (C) 2014 IBM
416b1d26eSNeelesh Gupta  *
516b1d26eSNeelesh Gupta  * This program is free software; you can redistribute it and/or modify
616b1d26eSNeelesh Gupta  * it under the terms of the GNU General Public License as published by
716b1d26eSNeelesh Gupta  * the Free Software Foundation; either version 2 of the License, or
816b1d26eSNeelesh Gupta  * (at your option) any later version.
916b1d26eSNeelesh Gupta  *
1016b1d26eSNeelesh Gupta  * This program is distributed in the hope that it will be useful,
1116b1d26eSNeelesh Gupta  * but WITHOUT ANY WARRANTY; without even the implied warranty of
1216b1d26eSNeelesh Gupta  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1316b1d26eSNeelesh Gupta  * GNU General Public License for more details.
1416b1d26eSNeelesh Gupta  *
1516b1d26eSNeelesh Gupta  * You should have received a copy of the GNU General Public License
1616b1d26eSNeelesh Gupta  * along with this program.
1716b1d26eSNeelesh Gupta  */
1816b1d26eSNeelesh Gupta 
19a737e835SJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20a737e835SJoe Perches 
2116b1d26eSNeelesh Gupta #define DRVNAME		"rtc-opal"
2216b1d26eSNeelesh Gupta 
2316b1d26eSNeelesh Gupta #include <linux/module.h>
2416b1d26eSNeelesh Gupta #include <linux/err.h>
2516b1d26eSNeelesh Gupta #include <linux/rtc.h>
2616b1d26eSNeelesh Gupta #include <linux/delay.h>
2716b1d26eSNeelesh Gupta #include <linux/bcd.h>
2816b1d26eSNeelesh Gupta #include <linux/platform_device.h>
2916b1d26eSNeelesh Gupta #include <linux/of.h>
3016b1d26eSNeelesh Gupta #include <asm/opal.h>
3116b1d26eSNeelesh Gupta #include <asm/firmware.h>
3216b1d26eSNeelesh Gupta 
3316b1d26eSNeelesh Gupta static void opal_to_tm(u32 y_m_d, u64 h_m_s_ms, struct rtc_time *tm)
3416b1d26eSNeelesh Gupta {
3516b1d26eSNeelesh Gupta 	tm->tm_year = ((bcd2bin(y_m_d >> 24) * 100) +
3616b1d26eSNeelesh Gupta 		       bcd2bin((y_m_d >> 16) & 0xff)) - 1900;
3716b1d26eSNeelesh Gupta 	tm->tm_mon  = bcd2bin((y_m_d >> 8) & 0xff) - 1;
3816b1d26eSNeelesh Gupta 	tm->tm_mday = bcd2bin(y_m_d & 0xff);
3916b1d26eSNeelesh Gupta 	tm->tm_hour = bcd2bin((h_m_s_ms >> 56) & 0xff);
4016b1d26eSNeelesh Gupta 	tm->tm_min  = bcd2bin((h_m_s_ms >> 48) & 0xff);
4116b1d26eSNeelesh Gupta 	tm->tm_sec  = bcd2bin((h_m_s_ms >> 40) & 0xff);
4216b1d26eSNeelesh Gupta 
4316b1d26eSNeelesh Gupta 	GregorianDay(tm);
4416b1d26eSNeelesh Gupta }
4516b1d26eSNeelesh Gupta 
4616b1d26eSNeelesh Gupta static void tm_to_opal(struct rtc_time *tm, u32 *y_m_d, u64 *h_m_s_ms)
4716b1d26eSNeelesh Gupta {
4816b1d26eSNeelesh Gupta 	*y_m_d |= ((u32)bin2bcd((tm->tm_year + 1900) / 100)) << 24;
4916b1d26eSNeelesh Gupta 	*y_m_d |= ((u32)bin2bcd((tm->tm_year + 1900) % 100)) << 16;
5016b1d26eSNeelesh Gupta 	*y_m_d |= ((u32)bin2bcd((tm->tm_mon + 1))) << 8;
5116b1d26eSNeelesh Gupta 	*y_m_d |= ((u32)bin2bcd(tm->tm_mday));
5216b1d26eSNeelesh Gupta 
5316b1d26eSNeelesh Gupta 	*h_m_s_ms |= ((u64)bin2bcd(tm->tm_hour)) << 56;
5416b1d26eSNeelesh Gupta 	*h_m_s_ms |= ((u64)bin2bcd(tm->tm_min)) << 48;
5516b1d26eSNeelesh Gupta 	*h_m_s_ms |= ((u64)bin2bcd(tm->tm_sec)) << 40;
5616b1d26eSNeelesh Gupta }
5716b1d26eSNeelesh Gupta 
5816b1d26eSNeelesh Gupta static int opal_get_rtc_time(struct device *dev, struct rtc_time *tm)
5916b1d26eSNeelesh Gupta {
6016b1d26eSNeelesh Gupta 	long rc = OPAL_BUSY;
6116b1d26eSNeelesh Gupta 	u32 y_m_d;
6216b1d26eSNeelesh Gupta 	u64 h_m_s_ms;
6316b1d26eSNeelesh Gupta 	__be32 __y_m_d;
6416b1d26eSNeelesh Gupta 	__be64 __h_m_s_ms;
6516b1d26eSNeelesh Gupta 
6616b1d26eSNeelesh Gupta 	while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) {
6716b1d26eSNeelesh Gupta 		rc = opal_rtc_read(&__y_m_d, &__h_m_s_ms);
6816b1d26eSNeelesh Gupta 		if (rc == OPAL_BUSY_EVENT)
6916b1d26eSNeelesh Gupta 			opal_poll_events(NULL);
7016b1d26eSNeelesh Gupta 		else
7116b1d26eSNeelesh Gupta 			msleep(10);
7216b1d26eSNeelesh Gupta 	}
7316b1d26eSNeelesh Gupta 
7416b1d26eSNeelesh Gupta 	if (rc != OPAL_SUCCESS)
7516b1d26eSNeelesh Gupta 		return -EIO;
7616b1d26eSNeelesh Gupta 
7716b1d26eSNeelesh Gupta 	y_m_d = be32_to_cpu(__y_m_d);
7816b1d26eSNeelesh Gupta 	h_m_s_ms = be64_to_cpu(__h_m_s_ms);
7916b1d26eSNeelesh Gupta 	opal_to_tm(y_m_d, h_m_s_ms, tm);
8016b1d26eSNeelesh Gupta 
8116b1d26eSNeelesh Gupta 	return 0;
8216b1d26eSNeelesh Gupta }
8316b1d26eSNeelesh Gupta 
8416b1d26eSNeelesh Gupta static int opal_set_rtc_time(struct device *dev, struct rtc_time *tm)
8516b1d26eSNeelesh Gupta {
8616b1d26eSNeelesh Gupta 	long rc = OPAL_BUSY;
8716b1d26eSNeelesh Gupta 	u32 y_m_d = 0;
8816b1d26eSNeelesh Gupta 	u64 h_m_s_ms = 0;
8916b1d26eSNeelesh Gupta 
9016b1d26eSNeelesh Gupta 	tm_to_opal(tm, &y_m_d, &h_m_s_ms);
9116b1d26eSNeelesh Gupta 	while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) {
9216b1d26eSNeelesh Gupta 		rc = opal_rtc_write(y_m_d, h_m_s_ms);
9316b1d26eSNeelesh Gupta 		if (rc == OPAL_BUSY_EVENT)
9416b1d26eSNeelesh Gupta 			opal_poll_events(NULL);
9516b1d26eSNeelesh Gupta 		else
9616b1d26eSNeelesh Gupta 			msleep(10);
9716b1d26eSNeelesh Gupta 	}
9816b1d26eSNeelesh Gupta 
9916b1d26eSNeelesh Gupta 	return rc == OPAL_SUCCESS ? 0 : -EIO;
10016b1d26eSNeelesh Gupta }
10116b1d26eSNeelesh Gupta 
10216b1d26eSNeelesh Gupta /*
10316b1d26eSNeelesh Gupta  * TPO	Timed Power-On
10416b1d26eSNeelesh Gupta  *
10516b1d26eSNeelesh Gupta  * TPO get/set OPAL calls care about the hour and min and to make it consistent
10616b1d26eSNeelesh Gupta  * with the rtc utility time conversion functions, we use the 'u64' to store
10716b1d26eSNeelesh Gupta  * its value and perform bit shift by 32 before use..
10816b1d26eSNeelesh Gupta  */
10916b1d26eSNeelesh Gupta static int opal_get_tpo_time(struct device *dev, struct rtc_wkalrm *alarm)
11016b1d26eSNeelesh Gupta {
11116b1d26eSNeelesh Gupta 	__be32 __y_m_d, __h_m;
11216b1d26eSNeelesh Gupta 	struct opal_msg msg;
11316b1d26eSNeelesh Gupta 	int rc, token;
11416b1d26eSNeelesh Gupta 	u64 h_m_s_ms;
11516b1d26eSNeelesh Gupta 	u32 y_m_d;
11616b1d26eSNeelesh Gupta 
11716b1d26eSNeelesh Gupta 	token = opal_async_get_token_interruptible();
11816b1d26eSNeelesh Gupta 	if (token < 0) {
11916b1d26eSNeelesh Gupta 		if (token != -ERESTARTSYS)
12016b1d26eSNeelesh Gupta 			pr_err("Failed to get the async token\n");
12116b1d26eSNeelesh Gupta 
12216b1d26eSNeelesh Gupta 		return token;
12316b1d26eSNeelesh Gupta 	}
12416b1d26eSNeelesh Gupta 
12516b1d26eSNeelesh Gupta 	rc = opal_tpo_read(token, &__y_m_d, &__h_m);
12616b1d26eSNeelesh Gupta 	if (rc != OPAL_ASYNC_COMPLETION) {
12716b1d26eSNeelesh Gupta 		rc = -EIO;
12816b1d26eSNeelesh Gupta 		goto exit;
12916b1d26eSNeelesh Gupta 	}
13016b1d26eSNeelesh Gupta 
13116b1d26eSNeelesh Gupta 	rc = opal_async_wait_response(token, &msg);
13216b1d26eSNeelesh Gupta 	if (rc) {
13316b1d26eSNeelesh Gupta 		rc = -EIO;
13416b1d26eSNeelesh Gupta 		goto exit;
13516b1d26eSNeelesh Gupta 	}
13616b1d26eSNeelesh Gupta 
13716b1d26eSNeelesh Gupta 	rc = be64_to_cpu(msg.params[1]);
13816b1d26eSNeelesh Gupta 	if (rc != OPAL_SUCCESS) {
13916b1d26eSNeelesh Gupta 		rc = -EIO;
14016b1d26eSNeelesh Gupta 		goto exit;
14116b1d26eSNeelesh Gupta 	}
14216b1d26eSNeelesh Gupta 
14316b1d26eSNeelesh Gupta 	y_m_d = be32_to_cpu(__y_m_d);
14416b1d26eSNeelesh Gupta 	h_m_s_ms = ((u64)be32_to_cpu(__h_m) << 32);
14516b1d26eSNeelesh Gupta 	opal_to_tm(y_m_d, h_m_s_ms, &alarm->time);
14616b1d26eSNeelesh Gupta 
14716b1d26eSNeelesh Gupta exit:
14816b1d26eSNeelesh Gupta 	opal_async_release_token(token);
14916b1d26eSNeelesh Gupta 	return rc;
15016b1d26eSNeelesh Gupta }
15116b1d26eSNeelesh Gupta 
15216b1d26eSNeelesh Gupta /* Set Timed Power-On */
15316b1d26eSNeelesh Gupta static int opal_set_tpo_time(struct device *dev, struct rtc_wkalrm *alarm)
15416b1d26eSNeelesh Gupta {
15516b1d26eSNeelesh Gupta 	u64 h_m_s_ms = 0, token;
15616b1d26eSNeelesh Gupta 	struct opal_msg msg;
15716b1d26eSNeelesh Gupta 	u32 y_m_d = 0;
15816b1d26eSNeelesh Gupta 	int rc;
15916b1d26eSNeelesh Gupta 
16016b1d26eSNeelesh Gupta 	tm_to_opal(&alarm->time, &y_m_d, &h_m_s_ms);
16116b1d26eSNeelesh Gupta 
16216b1d26eSNeelesh Gupta 	token = opal_async_get_token_interruptible();
16316b1d26eSNeelesh Gupta 	if (token < 0) {
16416b1d26eSNeelesh Gupta 		if (token != -ERESTARTSYS)
16516b1d26eSNeelesh Gupta 			pr_err("Failed to get the async token\n");
16616b1d26eSNeelesh Gupta 
16716b1d26eSNeelesh Gupta 		return token;
16816b1d26eSNeelesh Gupta 	}
16916b1d26eSNeelesh Gupta 
17016b1d26eSNeelesh Gupta 	/* TPO, we care about hour and minute */
17116b1d26eSNeelesh Gupta 	rc = opal_tpo_write(token, y_m_d,
17216b1d26eSNeelesh Gupta 			    (u32)((h_m_s_ms >> 32) & 0xffff0000));
17316b1d26eSNeelesh Gupta 	if (rc != OPAL_ASYNC_COMPLETION) {
17416b1d26eSNeelesh Gupta 		rc = -EIO;
17516b1d26eSNeelesh Gupta 		goto exit;
17616b1d26eSNeelesh Gupta 	}
17716b1d26eSNeelesh Gupta 
17816b1d26eSNeelesh Gupta 	rc = opal_async_wait_response(token, &msg);
17916b1d26eSNeelesh Gupta 	if (rc) {
18016b1d26eSNeelesh Gupta 		rc = -EIO;
18116b1d26eSNeelesh Gupta 		goto exit;
18216b1d26eSNeelesh Gupta 	}
18316b1d26eSNeelesh Gupta 
18416b1d26eSNeelesh Gupta 	rc = be64_to_cpu(msg.params[1]);
18516b1d26eSNeelesh Gupta 	if (rc != OPAL_SUCCESS)
18616b1d26eSNeelesh Gupta 		rc = -EIO;
18716b1d26eSNeelesh Gupta 
18816b1d26eSNeelesh Gupta exit:
18916b1d26eSNeelesh Gupta 	opal_async_release_token(token);
19016b1d26eSNeelesh Gupta 	return rc;
19116b1d26eSNeelesh Gupta }
19216b1d26eSNeelesh Gupta 
193*f4a2eecbSVaibhav Jain static struct rtc_class_ops opal_rtc_ops = {
19416b1d26eSNeelesh Gupta 	.read_time	= opal_get_rtc_time,
19516b1d26eSNeelesh Gupta 	.set_time	= opal_set_rtc_time,
19616b1d26eSNeelesh Gupta };
19716b1d26eSNeelesh Gupta 
19816b1d26eSNeelesh Gupta static int opal_rtc_probe(struct platform_device *pdev)
19916b1d26eSNeelesh Gupta {
20016b1d26eSNeelesh Gupta 	struct rtc_device *rtc;
20116b1d26eSNeelesh Gupta 
20216b1d26eSNeelesh Gupta 	if (pdev->dev.of_node && of_get_property(pdev->dev.of_node, "has-tpo",
203*f4a2eecbSVaibhav Jain 						 NULL)) {
20416b1d26eSNeelesh Gupta 		device_set_wakeup_capable(&pdev->dev, true);
205*f4a2eecbSVaibhav Jain 		opal_rtc_ops.read_alarm	= opal_get_tpo_time;
206*f4a2eecbSVaibhav Jain 		opal_rtc_ops.set_alarm = opal_set_tpo_time;
207*f4a2eecbSVaibhav Jain 	}
20816b1d26eSNeelesh Gupta 
20916b1d26eSNeelesh Gupta 	rtc = devm_rtc_device_register(&pdev->dev, DRVNAME, &opal_rtc_ops,
21016b1d26eSNeelesh Gupta 				       THIS_MODULE);
21116b1d26eSNeelesh Gupta 	if (IS_ERR(rtc))
21216b1d26eSNeelesh Gupta 		return PTR_ERR(rtc);
21316b1d26eSNeelesh Gupta 
21416b1d26eSNeelesh Gupta 	rtc->uie_unsupported = 1;
21516b1d26eSNeelesh Gupta 
21616b1d26eSNeelesh Gupta 	return 0;
21716b1d26eSNeelesh Gupta }
21816b1d26eSNeelesh Gupta 
21916b1d26eSNeelesh Gupta static const struct of_device_id opal_rtc_match[] = {
22016b1d26eSNeelesh Gupta 	{
22116b1d26eSNeelesh Gupta 		.compatible	= "ibm,opal-rtc",
22216b1d26eSNeelesh Gupta 	},
22316b1d26eSNeelesh Gupta 	{ }
22416b1d26eSNeelesh Gupta };
22516b1d26eSNeelesh Gupta MODULE_DEVICE_TABLE(of, opal_rtc_match);
22616b1d26eSNeelesh Gupta 
22716b1d26eSNeelesh Gupta static const struct platform_device_id opal_rtc_driver_ids[] = {
22816b1d26eSNeelesh Gupta 	{
22916b1d26eSNeelesh Gupta 		.name		= "opal-rtc",
23016b1d26eSNeelesh Gupta 	},
23116b1d26eSNeelesh Gupta 	{ }
23216b1d26eSNeelesh Gupta };
23316b1d26eSNeelesh Gupta MODULE_DEVICE_TABLE(platform, opal_rtc_driver_ids);
23416b1d26eSNeelesh Gupta 
23516b1d26eSNeelesh Gupta static struct platform_driver opal_rtc_driver = {
23616b1d26eSNeelesh Gupta 	.probe		= opal_rtc_probe,
23716b1d26eSNeelesh Gupta 	.id_table	= opal_rtc_driver_ids,
23816b1d26eSNeelesh Gupta 	.driver		= {
23916b1d26eSNeelesh Gupta 		.name		= DRVNAME,
24016b1d26eSNeelesh Gupta 		.of_match_table	= opal_rtc_match,
24116b1d26eSNeelesh Gupta 	},
24216b1d26eSNeelesh Gupta };
24316b1d26eSNeelesh Gupta 
24416b1d26eSNeelesh Gupta static int __init opal_rtc_init(void)
24516b1d26eSNeelesh Gupta {
24616b1d26eSNeelesh Gupta 	if (!firmware_has_feature(FW_FEATURE_OPAL))
24716b1d26eSNeelesh Gupta 		return -ENODEV;
24816b1d26eSNeelesh Gupta 
24916b1d26eSNeelesh Gupta 	return platform_driver_register(&opal_rtc_driver);
25016b1d26eSNeelesh Gupta }
25116b1d26eSNeelesh Gupta 
25216b1d26eSNeelesh Gupta static void __exit opal_rtc_exit(void)
25316b1d26eSNeelesh Gupta {
25416b1d26eSNeelesh Gupta 	platform_driver_unregister(&opal_rtc_driver);
25516b1d26eSNeelesh Gupta }
25616b1d26eSNeelesh Gupta 
25716b1d26eSNeelesh Gupta MODULE_AUTHOR("Neelesh Gupta <neelegup@linux.vnet.ibm.com>");
25816b1d26eSNeelesh Gupta MODULE_DESCRIPTION("IBM OPAL RTC driver");
25916b1d26eSNeelesh Gupta MODULE_LICENSE("GPL");
26016b1d26eSNeelesh Gupta 
26116b1d26eSNeelesh Gupta module_init(opal_rtc_init);
26216b1d26eSNeelesh Gupta module_exit(opal_rtc_exit);
263