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 4300b912b0SDaniel Axtens tm->tm_wday = -1; 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 137d0226d31SSuraj Jitindar Singh rc = opal_get_async_rc(msg); 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); 145*6dc1cf6fSVaibhav Jain 146*6dc1cf6fSVaibhav Jain /* check if no alarm is set */ 147*6dc1cf6fSVaibhav Jain if (y_m_d == 0 && h_m_s_ms == 0) { 148*6dc1cf6fSVaibhav Jain pr_debug("No alarm is set\n"); 149*6dc1cf6fSVaibhav Jain rc = -ENOENT; 150*6dc1cf6fSVaibhav Jain goto exit; 151*6dc1cf6fSVaibhav Jain } else { 152*6dc1cf6fSVaibhav Jain pr_debug("Alarm set to %x %llx\n", y_m_d, h_m_s_ms); 153*6dc1cf6fSVaibhav Jain } 154*6dc1cf6fSVaibhav Jain 15516b1d26eSNeelesh Gupta opal_to_tm(y_m_d, h_m_s_ms, &alarm->time); 15616b1d26eSNeelesh Gupta 15716b1d26eSNeelesh Gupta exit: 15816b1d26eSNeelesh Gupta opal_async_release_token(token); 15916b1d26eSNeelesh Gupta return rc; 16016b1d26eSNeelesh Gupta } 16116b1d26eSNeelesh Gupta 16216b1d26eSNeelesh Gupta /* Set Timed Power-On */ 16316b1d26eSNeelesh Gupta static int opal_set_tpo_time(struct device *dev, struct rtc_wkalrm *alarm) 16416b1d26eSNeelesh Gupta { 165c3530094SAndrzej Hajda u64 h_m_s_ms = 0; 16616b1d26eSNeelesh Gupta struct opal_msg msg; 16716b1d26eSNeelesh Gupta u32 y_m_d = 0; 168c3530094SAndrzej Hajda int token, rc; 16916b1d26eSNeelesh Gupta 17016b1d26eSNeelesh Gupta tm_to_opal(&alarm->time, &y_m_d, &h_m_s_ms); 17116b1d26eSNeelesh Gupta 17216b1d26eSNeelesh Gupta token = opal_async_get_token_interruptible(); 17316b1d26eSNeelesh Gupta if (token < 0) { 17416b1d26eSNeelesh Gupta if (token != -ERESTARTSYS) 17516b1d26eSNeelesh Gupta pr_err("Failed to get the async token\n"); 17616b1d26eSNeelesh Gupta 17716b1d26eSNeelesh Gupta return token; 17816b1d26eSNeelesh Gupta } 17916b1d26eSNeelesh Gupta 18016b1d26eSNeelesh Gupta /* TPO, we care about hour and minute */ 18116b1d26eSNeelesh Gupta rc = opal_tpo_write(token, y_m_d, 18216b1d26eSNeelesh Gupta (u32)((h_m_s_ms >> 32) & 0xffff0000)); 18316b1d26eSNeelesh Gupta if (rc != OPAL_ASYNC_COMPLETION) { 18416b1d26eSNeelesh Gupta rc = -EIO; 18516b1d26eSNeelesh Gupta goto exit; 18616b1d26eSNeelesh Gupta } 18716b1d26eSNeelesh Gupta 18816b1d26eSNeelesh Gupta rc = opal_async_wait_response(token, &msg); 18916b1d26eSNeelesh Gupta if (rc) { 19016b1d26eSNeelesh Gupta rc = -EIO; 19116b1d26eSNeelesh Gupta goto exit; 19216b1d26eSNeelesh Gupta } 19316b1d26eSNeelesh Gupta 194d0226d31SSuraj Jitindar Singh rc = opal_get_async_rc(msg); 19516b1d26eSNeelesh Gupta if (rc != OPAL_SUCCESS) 19616b1d26eSNeelesh Gupta rc = -EIO; 19716b1d26eSNeelesh Gupta 19816b1d26eSNeelesh Gupta exit: 19916b1d26eSNeelesh Gupta opal_async_release_token(token); 20016b1d26eSNeelesh Gupta return rc; 20116b1d26eSNeelesh Gupta } 20216b1d26eSNeelesh Gupta 203f4a2eecbSVaibhav Jain static struct rtc_class_ops opal_rtc_ops = { 20416b1d26eSNeelesh Gupta .read_time = opal_get_rtc_time, 20516b1d26eSNeelesh Gupta .set_time = opal_set_rtc_time, 20616b1d26eSNeelesh Gupta }; 20716b1d26eSNeelesh Gupta 20816b1d26eSNeelesh Gupta static int opal_rtc_probe(struct platform_device *pdev) 20916b1d26eSNeelesh Gupta { 21016b1d26eSNeelesh Gupta struct rtc_device *rtc; 21116b1d26eSNeelesh Gupta 212347e40f0SSudeep Holla if (pdev->dev.of_node && 213347e40f0SSudeep Holla (of_property_read_bool(pdev->dev.of_node, "wakeup-source") || 214347e40f0SSudeep Holla of_property_read_bool(pdev->dev.of_node, "has-tpo")/* legacy */)) { 21516b1d26eSNeelesh Gupta device_set_wakeup_capable(&pdev->dev, true); 216f4a2eecbSVaibhav Jain opal_rtc_ops.read_alarm = opal_get_tpo_time; 217f4a2eecbSVaibhav Jain opal_rtc_ops.set_alarm = opal_set_tpo_time; 218f4a2eecbSVaibhav Jain } 21916b1d26eSNeelesh Gupta 22016b1d26eSNeelesh Gupta rtc = devm_rtc_device_register(&pdev->dev, DRVNAME, &opal_rtc_ops, 22116b1d26eSNeelesh Gupta THIS_MODULE); 22216b1d26eSNeelesh Gupta if (IS_ERR(rtc)) 22316b1d26eSNeelesh Gupta return PTR_ERR(rtc); 22416b1d26eSNeelesh Gupta 22516b1d26eSNeelesh Gupta rtc->uie_unsupported = 1; 22616b1d26eSNeelesh Gupta 22716b1d26eSNeelesh Gupta return 0; 22816b1d26eSNeelesh Gupta } 22916b1d26eSNeelesh Gupta 23016b1d26eSNeelesh Gupta static const struct of_device_id opal_rtc_match[] = { 23116b1d26eSNeelesh Gupta { 23216b1d26eSNeelesh Gupta .compatible = "ibm,opal-rtc", 23316b1d26eSNeelesh Gupta }, 23416b1d26eSNeelesh Gupta { } 23516b1d26eSNeelesh Gupta }; 23616b1d26eSNeelesh Gupta MODULE_DEVICE_TABLE(of, opal_rtc_match); 23716b1d26eSNeelesh Gupta 23816b1d26eSNeelesh Gupta static const struct platform_device_id opal_rtc_driver_ids[] = { 23916b1d26eSNeelesh Gupta { 24016b1d26eSNeelesh Gupta .name = "opal-rtc", 24116b1d26eSNeelesh Gupta }, 24216b1d26eSNeelesh Gupta { } 24316b1d26eSNeelesh Gupta }; 24416b1d26eSNeelesh Gupta MODULE_DEVICE_TABLE(platform, opal_rtc_driver_ids); 24516b1d26eSNeelesh Gupta 24616b1d26eSNeelesh Gupta static struct platform_driver opal_rtc_driver = { 24716b1d26eSNeelesh Gupta .probe = opal_rtc_probe, 24816b1d26eSNeelesh Gupta .id_table = opal_rtc_driver_ids, 24916b1d26eSNeelesh Gupta .driver = { 25016b1d26eSNeelesh Gupta .name = DRVNAME, 25116b1d26eSNeelesh Gupta .of_match_table = opal_rtc_match, 25216b1d26eSNeelesh Gupta }, 25316b1d26eSNeelesh Gupta }; 25416b1d26eSNeelesh Gupta 25516b1d26eSNeelesh Gupta static int __init opal_rtc_init(void) 25616b1d26eSNeelesh Gupta { 25716b1d26eSNeelesh Gupta if (!firmware_has_feature(FW_FEATURE_OPAL)) 25816b1d26eSNeelesh Gupta return -ENODEV; 25916b1d26eSNeelesh Gupta 26016b1d26eSNeelesh Gupta return platform_driver_register(&opal_rtc_driver); 26116b1d26eSNeelesh Gupta } 26216b1d26eSNeelesh Gupta 26316b1d26eSNeelesh Gupta static void __exit opal_rtc_exit(void) 26416b1d26eSNeelesh Gupta { 26516b1d26eSNeelesh Gupta platform_driver_unregister(&opal_rtc_driver); 26616b1d26eSNeelesh Gupta } 26716b1d26eSNeelesh Gupta 26816b1d26eSNeelesh Gupta MODULE_AUTHOR("Neelesh Gupta <neelegup@linux.vnet.ibm.com>"); 26916b1d26eSNeelesh Gupta MODULE_DESCRIPTION("IBM OPAL RTC driver"); 27016b1d26eSNeelesh Gupta MODULE_LICENSE("GPL"); 27116b1d26eSNeelesh Gupta 27216b1d26eSNeelesh Gupta module_init(opal_rtc_init); 27316b1d26eSNeelesh Gupta module_exit(opal_rtc_exit); 274