1d5bb994bSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 216b1d26eSNeelesh Gupta /* 316b1d26eSNeelesh Gupta * IBM OPAL RTC driver 416b1d26eSNeelesh Gupta * Copyright (C) 2014 IBM 516b1d26eSNeelesh Gupta */ 616b1d26eSNeelesh Gupta 7a737e835SJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 8a737e835SJoe Perches 916b1d26eSNeelesh Gupta #define DRVNAME "rtc-opal" 1016b1d26eSNeelesh Gupta 1116b1d26eSNeelesh Gupta #include <linux/module.h> 1216b1d26eSNeelesh Gupta #include <linux/err.h> 1316b1d26eSNeelesh Gupta #include <linux/rtc.h> 1416b1d26eSNeelesh Gupta #include <linux/delay.h> 1516b1d26eSNeelesh Gupta #include <linux/bcd.h> 1616b1d26eSNeelesh Gupta #include <linux/platform_device.h> 1716b1d26eSNeelesh Gupta #include <linux/of.h> 1816b1d26eSNeelesh Gupta #include <asm/opal.h> 1916b1d26eSNeelesh Gupta #include <asm/firmware.h> 2016b1d26eSNeelesh Gupta 2116b1d26eSNeelesh Gupta static void opal_to_tm(u32 y_m_d, u64 h_m_s_ms, struct rtc_time *tm) 2216b1d26eSNeelesh Gupta { 2316b1d26eSNeelesh Gupta tm->tm_year = ((bcd2bin(y_m_d >> 24) * 100) + 2416b1d26eSNeelesh Gupta bcd2bin((y_m_d >> 16) & 0xff)) - 1900; 2516b1d26eSNeelesh Gupta tm->tm_mon = bcd2bin((y_m_d >> 8) & 0xff) - 1; 2616b1d26eSNeelesh Gupta tm->tm_mday = bcd2bin(y_m_d & 0xff); 2716b1d26eSNeelesh Gupta tm->tm_hour = bcd2bin((h_m_s_ms >> 56) & 0xff); 2816b1d26eSNeelesh Gupta tm->tm_min = bcd2bin((h_m_s_ms >> 48) & 0xff); 2916b1d26eSNeelesh Gupta tm->tm_sec = bcd2bin((h_m_s_ms >> 40) & 0xff); 3016b1d26eSNeelesh Gupta 3100b912b0SDaniel Axtens tm->tm_wday = -1; 3216b1d26eSNeelesh Gupta } 3316b1d26eSNeelesh Gupta 3416b1d26eSNeelesh Gupta static void tm_to_opal(struct rtc_time *tm, u32 *y_m_d, u64 *h_m_s_ms) 3516b1d26eSNeelesh Gupta { 3616b1d26eSNeelesh Gupta *y_m_d |= ((u32)bin2bcd((tm->tm_year + 1900) / 100)) << 24; 3716b1d26eSNeelesh Gupta *y_m_d |= ((u32)bin2bcd((tm->tm_year + 1900) % 100)) << 16; 3816b1d26eSNeelesh Gupta *y_m_d |= ((u32)bin2bcd((tm->tm_mon + 1))) << 8; 3916b1d26eSNeelesh Gupta *y_m_d |= ((u32)bin2bcd(tm->tm_mday)); 4016b1d26eSNeelesh Gupta 4116b1d26eSNeelesh Gupta *h_m_s_ms |= ((u64)bin2bcd(tm->tm_hour)) << 56; 4216b1d26eSNeelesh Gupta *h_m_s_ms |= ((u64)bin2bcd(tm->tm_min)) << 48; 4316b1d26eSNeelesh Gupta *h_m_s_ms |= ((u64)bin2bcd(tm->tm_sec)) << 40; 4416b1d26eSNeelesh Gupta } 4516b1d26eSNeelesh Gupta 4616b1d26eSNeelesh Gupta static int opal_get_rtc_time(struct device *dev, struct rtc_time *tm) 4716b1d26eSNeelesh Gupta { 48682e6b4dSNicholas Piggin s64 rc = OPAL_BUSY; 495b8b5806SStewart Smith int retries = 10; 5016b1d26eSNeelesh Gupta u32 y_m_d; 5116b1d26eSNeelesh Gupta u64 h_m_s_ms; 5216b1d26eSNeelesh Gupta __be32 __y_m_d; 5316b1d26eSNeelesh Gupta __be64 __h_m_s_ms; 5416b1d26eSNeelesh Gupta 5516b1d26eSNeelesh Gupta while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) { 5616b1d26eSNeelesh Gupta rc = opal_rtc_read(&__y_m_d, &__h_m_s_ms); 57682e6b4dSNicholas Piggin if (rc == OPAL_BUSY_EVENT) { 58682e6b4dSNicholas Piggin msleep(OPAL_BUSY_DELAY_MS); 5916b1d26eSNeelesh Gupta opal_poll_events(NULL); 60682e6b4dSNicholas Piggin } else if (rc == OPAL_BUSY) { 61682e6b4dSNicholas Piggin msleep(OPAL_BUSY_DELAY_MS); 62682e6b4dSNicholas Piggin } else if (rc == OPAL_HARDWARE || rc == OPAL_INTERNAL_ERROR) { 63682e6b4dSNicholas Piggin if (retries--) { 64682e6b4dSNicholas Piggin msleep(10); /* Wait 10ms before retry */ 65682e6b4dSNicholas Piggin rc = OPAL_BUSY; /* go around again */ 66682e6b4dSNicholas Piggin } 67682e6b4dSNicholas Piggin } 6816b1d26eSNeelesh Gupta } 6916b1d26eSNeelesh Gupta 7016b1d26eSNeelesh Gupta if (rc != OPAL_SUCCESS) 7116b1d26eSNeelesh Gupta return -EIO; 7216b1d26eSNeelesh Gupta 7316b1d26eSNeelesh Gupta y_m_d = be32_to_cpu(__y_m_d); 7416b1d26eSNeelesh Gupta h_m_s_ms = be64_to_cpu(__h_m_s_ms); 7516b1d26eSNeelesh Gupta opal_to_tm(y_m_d, h_m_s_ms, tm); 7616b1d26eSNeelesh Gupta 7716b1d26eSNeelesh Gupta return 0; 7816b1d26eSNeelesh Gupta } 7916b1d26eSNeelesh Gupta 8016b1d26eSNeelesh Gupta static int opal_set_rtc_time(struct device *dev, struct rtc_time *tm) 8116b1d26eSNeelesh Gupta { 82682e6b4dSNicholas Piggin s64 rc = OPAL_BUSY; 835b8b5806SStewart Smith int retries = 10; 8416b1d26eSNeelesh Gupta u32 y_m_d = 0; 8516b1d26eSNeelesh Gupta u64 h_m_s_ms = 0; 8616b1d26eSNeelesh Gupta 8716b1d26eSNeelesh Gupta tm_to_opal(tm, &y_m_d, &h_m_s_ms); 88682e6b4dSNicholas Piggin 8916b1d26eSNeelesh Gupta while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) { 9016b1d26eSNeelesh Gupta rc = opal_rtc_write(y_m_d, h_m_s_ms); 91682e6b4dSNicholas Piggin if (rc == OPAL_BUSY_EVENT) { 92682e6b4dSNicholas Piggin msleep(OPAL_BUSY_DELAY_MS); 9316b1d26eSNeelesh Gupta opal_poll_events(NULL); 94682e6b4dSNicholas Piggin } else if (rc == OPAL_BUSY) { 95682e6b4dSNicholas Piggin msleep(OPAL_BUSY_DELAY_MS); 96682e6b4dSNicholas Piggin } else if (rc == OPAL_HARDWARE || rc == OPAL_INTERNAL_ERROR) { 97682e6b4dSNicholas Piggin if (retries--) { 98682e6b4dSNicholas Piggin msleep(10); /* Wait 10ms before retry */ 99682e6b4dSNicholas Piggin rc = OPAL_BUSY; /* go around again */ 100682e6b4dSNicholas Piggin } 101682e6b4dSNicholas Piggin } 10216b1d26eSNeelesh Gupta } 10316b1d26eSNeelesh Gupta 10416b1d26eSNeelesh Gupta return rc == OPAL_SUCCESS ? 0 : -EIO; 10516b1d26eSNeelesh Gupta } 10616b1d26eSNeelesh Gupta 10716b1d26eSNeelesh Gupta /* 10816b1d26eSNeelesh Gupta * TPO Timed Power-On 10916b1d26eSNeelesh Gupta * 11016b1d26eSNeelesh Gupta * TPO get/set OPAL calls care about the hour and min and to make it consistent 11116b1d26eSNeelesh Gupta * with the rtc utility time conversion functions, we use the 'u64' to store 11216b1d26eSNeelesh Gupta * its value and perform bit shift by 32 before use.. 11316b1d26eSNeelesh Gupta */ 11416b1d26eSNeelesh Gupta static int opal_get_tpo_time(struct device *dev, struct rtc_wkalrm *alarm) 11516b1d26eSNeelesh Gupta { 11616b1d26eSNeelesh Gupta __be32 __y_m_d, __h_m; 11716b1d26eSNeelesh Gupta struct opal_msg msg; 11816b1d26eSNeelesh Gupta int rc, token; 11916b1d26eSNeelesh Gupta u64 h_m_s_ms; 12016b1d26eSNeelesh Gupta u32 y_m_d; 12116b1d26eSNeelesh Gupta 12216b1d26eSNeelesh Gupta token = opal_async_get_token_interruptible(); 12316b1d26eSNeelesh Gupta if (token < 0) { 12416b1d26eSNeelesh Gupta if (token != -ERESTARTSYS) 12516b1d26eSNeelesh Gupta pr_err("Failed to get the async token\n"); 12616b1d26eSNeelesh Gupta 12716b1d26eSNeelesh Gupta return token; 12816b1d26eSNeelesh Gupta } 12916b1d26eSNeelesh Gupta 13016b1d26eSNeelesh Gupta rc = opal_tpo_read(token, &__y_m_d, &__h_m); 13116b1d26eSNeelesh Gupta if (rc != OPAL_ASYNC_COMPLETION) { 13216b1d26eSNeelesh Gupta rc = -EIO; 13316b1d26eSNeelesh Gupta goto exit; 13416b1d26eSNeelesh Gupta } 13516b1d26eSNeelesh Gupta 13616b1d26eSNeelesh Gupta rc = opal_async_wait_response(token, &msg); 13716b1d26eSNeelesh Gupta if (rc) { 13816b1d26eSNeelesh Gupta rc = -EIO; 13916b1d26eSNeelesh Gupta goto exit; 14016b1d26eSNeelesh Gupta } 14116b1d26eSNeelesh Gupta 142d0226d31SSuraj Jitindar Singh rc = opal_get_async_rc(msg); 14316b1d26eSNeelesh Gupta if (rc != OPAL_SUCCESS) { 14416b1d26eSNeelesh Gupta rc = -EIO; 14516b1d26eSNeelesh Gupta goto exit; 14616b1d26eSNeelesh Gupta } 14716b1d26eSNeelesh Gupta 14816b1d26eSNeelesh Gupta y_m_d = be32_to_cpu(__y_m_d); 14916b1d26eSNeelesh Gupta h_m_s_ms = ((u64)be32_to_cpu(__h_m) << 32); 1506dc1cf6fSVaibhav Jain 1516dc1cf6fSVaibhav Jain /* check if no alarm is set */ 1526dc1cf6fSVaibhav Jain if (y_m_d == 0 && h_m_s_ms == 0) { 1536dc1cf6fSVaibhav Jain pr_debug("No alarm is set\n"); 1546dc1cf6fSVaibhav Jain rc = -ENOENT; 1556dc1cf6fSVaibhav Jain goto exit; 1566dc1cf6fSVaibhav Jain } else { 1576dc1cf6fSVaibhav Jain pr_debug("Alarm set to %x %llx\n", y_m_d, h_m_s_ms); 1586dc1cf6fSVaibhav Jain } 1596dc1cf6fSVaibhav Jain 16016b1d26eSNeelesh Gupta opal_to_tm(y_m_d, h_m_s_ms, &alarm->time); 16116b1d26eSNeelesh Gupta 16216b1d26eSNeelesh Gupta exit: 16316b1d26eSNeelesh Gupta opal_async_release_token(token); 16416b1d26eSNeelesh Gupta return rc; 16516b1d26eSNeelesh Gupta } 16616b1d26eSNeelesh Gupta 16716b1d26eSNeelesh Gupta /* Set Timed Power-On */ 16816b1d26eSNeelesh Gupta static int opal_set_tpo_time(struct device *dev, struct rtc_wkalrm *alarm) 16916b1d26eSNeelesh Gupta { 170c3530094SAndrzej Hajda u64 h_m_s_ms = 0; 17116b1d26eSNeelesh Gupta struct opal_msg msg; 17216b1d26eSNeelesh Gupta u32 y_m_d = 0; 173c3530094SAndrzej Hajda int token, rc; 17416b1d26eSNeelesh Gupta 1750ec7769aSVaibhav Jain /* if alarm is enabled */ 1760ec7769aSVaibhav Jain if (alarm->enabled) { 17716b1d26eSNeelesh Gupta tm_to_opal(&alarm->time, &y_m_d, &h_m_s_ms); 1780ec7769aSVaibhav Jain pr_debug("Alarm set to %x %llx\n", y_m_d, h_m_s_ms); 1790ec7769aSVaibhav Jain 1800ec7769aSVaibhav Jain } else { 1810ec7769aSVaibhav Jain pr_debug("Alarm getting disabled\n"); 1820ec7769aSVaibhav Jain } 18316b1d26eSNeelesh Gupta 18416b1d26eSNeelesh Gupta token = opal_async_get_token_interruptible(); 18516b1d26eSNeelesh Gupta if (token < 0) { 18616b1d26eSNeelesh Gupta if (token != -ERESTARTSYS) 18716b1d26eSNeelesh Gupta pr_err("Failed to get the async token\n"); 18816b1d26eSNeelesh Gupta 18916b1d26eSNeelesh Gupta return token; 19016b1d26eSNeelesh Gupta } 19116b1d26eSNeelesh Gupta 19216b1d26eSNeelesh Gupta /* TPO, we care about hour and minute */ 19316b1d26eSNeelesh Gupta rc = opal_tpo_write(token, y_m_d, 19416b1d26eSNeelesh Gupta (u32)((h_m_s_ms >> 32) & 0xffff0000)); 19516b1d26eSNeelesh Gupta if (rc != OPAL_ASYNC_COMPLETION) { 19616b1d26eSNeelesh Gupta rc = -EIO; 19716b1d26eSNeelesh Gupta goto exit; 19816b1d26eSNeelesh Gupta } 19916b1d26eSNeelesh Gupta 20016b1d26eSNeelesh Gupta rc = opal_async_wait_response(token, &msg); 20116b1d26eSNeelesh Gupta if (rc) { 20216b1d26eSNeelesh Gupta rc = -EIO; 20316b1d26eSNeelesh Gupta goto exit; 20416b1d26eSNeelesh Gupta } 20516b1d26eSNeelesh Gupta 206d0226d31SSuraj Jitindar Singh rc = opal_get_async_rc(msg); 20716b1d26eSNeelesh Gupta if (rc != OPAL_SUCCESS) 20816b1d26eSNeelesh Gupta rc = -EIO; 20916b1d26eSNeelesh Gupta 21016b1d26eSNeelesh Gupta exit: 21116b1d26eSNeelesh Gupta opal_async_release_token(token); 21216b1d26eSNeelesh Gupta return rc; 21316b1d26eSNeelesh Gupta } 21416b1d26eSNeelesh Gupta 215fd86b2d4SYueHaibing static int opal_tpo_alarm_irq_enable(struct device *dev, unsigned int enabled) 2160ec7769aSVaibhav Jain { 2170ec7769aSVaibhav Jain struct rtc_wkalrm alarm = { .enabled = 0 }; 2180ec7769aSVaibhav Jain 2190ec7769aSVaibhav Jain /* 2200ec7769aSVaibhav Jain * TPO is automatically enabled when opal_set_tpo_time() is called with 2210ec7769aSVaibhav Jain * non-zero rtc-time. We only handle disable case which needs to be 2220ec7769aSVaibhav Jain * explicitly told to opal. 2230ec7769aSVaibhav Jain */ 2240ec7769aSVaibhav Jain return enabled ? 0 : opal_set_tpo_time(dev, &alarm); 2250ec7769aSVaibhav Jain } 2260ec7769aSVaibhav Jain 227*50f97e91SAlexandre Belloni static const struct rtc_class_ops opal_rtc_ops = { 22816b1d26eSNeelesh Gupta .read_time = opal_get_rtc_time, 22916b1d26eSNeelesh Gupta .set_time = opal_set_rtc_time, 230*50f97e91SAlexandre Belloni .read_alarm = opal_get_tpo_time, 231*50f97e91SAlexandre Belloni .set_alarm = opal_set_tpo_time, 232*50f97e91SAlexandre Belloni .alarm_irq_enable = opal_tpo_alarm_irq_enable, 23316b1d26eSNeelesh Gupta }; 23416b1d26eSNeelesh Gupta 23516b1d26eSNeelesh Gupta static int opal_rtc_probe(struct platform_device *pdev) 23616b1d26eSNeelesh Gupta { 23716b1d26eSNeelesh Gupta struct rtc_device *rtc; 23816b1d26eSNeelesh Gupta 2391bf3e6ccSAlexandre Belloni rtc = devm_rtc_allocate_device(&pdev->dev); 2401bf3e6ccSAlexandre Belloni if (IS_ERR(rtc)) 2411bf3e6ccSAlexandre Belloni return PTR_ERR(rtc); 2421bf3e6ccSAlexandre Belloni 243347e40f0SSudeep Holla if (pdev->dev.of_node && 244347e40f0SSudeep Holla (of_property_read_bool(pdev->dev.of_node, "wakeup-source") || 245*50f97e91SAlexandre Belloni of_property_read_bool(pdev->dev.of_node, "has-tpo")/* legacy */)) 24616b1d26eSNeelesh Gupta device_set_wakeup_capable(&pdev->dev, true); 247*50f97e91SAlexandre Belloni else 248*50f97e91SAlexandre Belloni clear_bit(RTC_FEATURE_ALARM, rtc->features); 24916b1d26eSNeelesh Gupta 2501bf3e6ccSAlexandre Belloni rtc->ops = &opal_rtc_ops; 2511bf3e6ccSAlexandre Belloni rtc->range_min = RTC_TIMESTAMP_BEGIN_0000; 2521bf3e6ccSAlexandre Belloni rtc->range_max = RTC_TIMESTAMP_END_9999; 25316b1d26eSNeelesh Gupta rtc->uie_unsupported = 1; 25416b1d26eSNeelesh Gupta 2551bf3e6ccSAlexandre Belloni return devm_rtc_register_device(rtc); 25616b1d26eSNeelesh Gupta } 25716b1d26eSNeelesh Gupta 25816b1d26eSNeelesh Gupta static const struct of_device_id opal_rtc_match[] = { 25916b1d26eSNeelesh Gupta { 26016b1d26eSNeelesh Gupta .compatible = "ibm,opal-rtc", 26116b1d26eSNeelesh Gupta }, 26216b1d26eSNeelesh Gupta { } 26316b1d26eSNeelesh Gupta }; 26416b1d26eSNeelesh Gupta MODULE_DEVICE_TABLE(of, opal_rtc_match); 26516b1d26eSNeelesh Gupta 26616b1d26eSNeelesh Gupta static const struct platform_device_id opal_rtc_driver_ids[] = { 26716b1d26eSNeelesh Gupta { 26816b1d26eSNeelesh Gupta .name = "opal-rtc", 26916b1d26eSNeelesh Gupta }, 27016b1d26eSNeelesh Gupta { } 27116b1d26eSNeelesh Gupta }; 27216b1d26eSNeelesh Gupta MODULE_DEVICE_TABLE(platform, opal_rtc_driver_ids); 27316b1d26eSNeelesh Gupta 27416b1d26eSNeelesh Gupta static struct platform_driver opal_rtc_driver = { 27516b1d26eSNeelesh Gupta .probe = opal_rtc_probe, 27616b1d26eSNeelesh Gupta .id_table = opal_rtc_driver_ids, 27716b1d26eSNeelesh Gupta .driver = { 27816b1d26eSNeelesh Gupta .name = DRVNAME, 27916b1d26eSNeelesh Gupta .of_match_table = opal_rtc_match, 28016b1d26eSNeelesh Gupta }, 28116b1d26eSNeelesh Gupta }; 28216b1d26eSNeelesh Gupta 28316b1d26eSNeelesh Gupta static int __init opal_rtc_init(void) 28416b1d26eSNeelesh Gupta { 28516b1d26eSNeelesh Gupta if (!firmware_has_feature(FW_FEATURE_OPAL)) 28616b1d26eSNeelesh Gupta return -ENODEV; 28716b1d26eSNeelesh Gupta 28816b1d26eSNeelesh Gupta return platform_driver_register(&opal_rtc_driver); 28916b1d26eSNeelesh Gupta } 29016b1d26eSNeelesh Gupta 29116b1d26eSNeelesh Gupta static void __exit opal_rtc_exit(void) 29216b1d26eSNeelesh Gupta { 29316b1d26eSNeelesh Gupta platform_driver_unregister(&opal_rtc_driver); 29416b1d26eSNeelesh Gupta } 29516b1d26eSNeelesh Gupta 29616b1d26eSNeelesh Gupta MODULE_AUTHOR("Neelesh Gupta <neelegup@linux.vnet.ibm.com>"); 29716b1d26eSNeelesh Gupta MODULE_DESCRIPTION("IBM OPAL RTC driver"); 29816b1d26eSNeelesh Gupta MODULE_LICENSE("GPL"); 29916b1d26eSNeelesh Gupta 30016b1d26eSNeelesh Gupta module_init(opal_rtc_init); 30116b1d26eSNeelesh Gupta module_exit(opal_rtc_exit); 302