18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Real Time Clock interface for Linux on the MVME16x 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Based on the PC driver by Paul Gortmaker. 68c2ecf20Sopenharmony_ci */ 78c2ecf20Sopenharmony_ci 88c2ecf20Sopenharmony_ci#define RTC_VERSION "1.00" 98c2ecf20Sopenharmony_ci 108c2ecf20Sopenharmony_ci#include <linux/types.h> 118c2ecf20Sopenharmony_ci#include <linux/errno.h> 128c2ecf20Sopenharmony_ci#include <linux/miscdevice.h> 138c2ecf20Sopenharmony_ci#include <linux/ioport.h> 148c2ecf20Sopenharmony_ci#include <linux/capability.h> 158c2ecf20Sopenharmony_ci#include <linux/fcntl.h> 168c2ecf20Sopenharmony_ci#include <linux/init.h> 178c2ecf20Sopenharmony_ci#include <linux/poll.h> 188c2ecf20Sopenharmony_ci#include <linux/rtc.h> /* For struct rtc_time and ioctls, etc */ 198c2ecf20Sopenharmony_ci#include <linux/bcd.h> 208c2ecf20Sopenharmony_ci#include <asm/mvme16xhw.h> 218c2ecf20Sopenharmony_ci 228c2ecf20Sopenharmony_ci#include <asm/io.h> 238c2ecf20Sopenharmony_ci#include <linux/uaccess.h> 248c2ecf20Sopenharmony_ci#include <asm/setup.h> 258c2ecf20Sopenharmony_ci 268c2ecf20Sopenharmony_ci/* 278c2ecf20Sopenharmony_ci * We sponge a minor off of the misc major. No need slurping 288c2ecf20Sopenharmony_ci * up another valuable major dev number for this. If you add 298c2ecf20Sopenharmony_ci * an ioctl, make sure you don't conflict with SPARC's RTC 308c2ecf20Sopenharmony_ci * ioctls. 318c2ecf20Sopenharmony_ci */ 328c2ecf20Sopenharmony_ci 338c2ecf20Sopenharmony_cistatic const unsigned char days_in_mo[] = 348c2ecf20Sopenharmony_ci{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 358c2ecf20Sopenharmony_ci 368c2ecf20Sopenharmony_cistatic atomic_t rtc_ready = ATOMIC_INIT(1); 378c2ecf20Sopenharmony_ci 388c2ecf20Sopenharmony_cistatic long rtc_ioctl(struct file *file, unsigned int cmd, unsigned long arg) 398c2ecf20Sopenharmony_ci{ 408c2ecf20Sopenharmony_ci volatile MK48T08ptr_t rtc = (MK48T08ptr_t)MVME_RTC_BASE; 418c2ecf20Sopenharmony_ci unsigned long flags; 428c2ecf20Sopenharmony_ci struct rtc_time wtime; 438c2ecf20Sopenharmony_ci void __user *argp = (void __user *)arg; 448c2ecf20Sopenharmony_ci 458c2ecf20Sopenharmony_ci switch (cmd) { 468c2ecf20Sopenharmony_ci case RTC_RD_TIME: /* Read the time/date from RTC */ 478c2ecf20Sopenharmony_ci { 488c2ecf20Sopenharmony_ci local_irq_save(flags); 498c2ecf20Sopenharmony_ci /* Ensure clock and real-time-mode-register are accessible */ 508c2ecf20Sopenharmony_ci rtc->ctrl = RTC_READ; 518c2ecf20Sopenharmony_ci memset(&wtime, 0, sizeof(struct rtc_time)); 528c2ecf20Sopenharmony_ci wtime.tm_sec = bcd2bin(rtc->bcd_sec); 538c2ecf20Sopenharmony_ci wtime.tm_min = bcd2bin(rtc->bcd_min); 548c2ecf20Sopenharmony_ci wtime.tm_hour = bcd2bin(rtc->bcd_hr); 558c2ecf20Sopenharmony_ci wtime.tm_mday = bcd2bin(rtc->bcd_dom); 568c2ecf20Sopenharmony_ci wtime.tm_mon = bcd2bin(rtc->bcd_mth)-1; 578c2ecf20Sopenharmony_ci wtime.tm_year = bcd2bin(rtc->bcd_year); 588c2ecf20Sopenharmony_ci if (wtime.tm_year < 70) 598c2ecf20Sopenharmony_ci wtime.tm_year += 100; 608c2ecf20Sopenharmony_ci wtime.tm_wday = bcd2bin(rtc->bcd_dow)-1; 618c2ecf20Sopenharmony_ci rtc->ctrl = 0; 628c2ecf20Sopenharmony_ci local_irq_restore(flags); 638c2ecf20Sopenharmony_ci return copy_to_user(argp, &wtime, sizeof wtime) ? 648c2ecf20Sopenharmony_ci -EFAULT : 0; 658c2ecf20Sopenharmony_ci } 668c2ecf20Sopenharmony_ci case RTC_SET_TIME: /* Set the RTC */ 678c2ecf20Sopenharmony_ci { 688c2ecf20Sopenharmony_ci struct rtc_time rtc_tm; 698c2ecf20Sopenharmony_ci unsigned char mon, day, hrs, min, sec, leap_yr; 708c2ecf20Sopenharmony_ci unsigned int yrs; 718c2ecf20Sopenharmony_ci 728c2ecf20Sopenharmony_ci if (!capable(CAP_SYS_ADMIN)) 738c2ecf20Sopenharmony_ci return -EACCES; 748c2ecf20Sopenharmony_ci 758c2ecf20Sopenharmony_ci if (copy_from_user(&rtc_tm, argp, sizeof(struct rtc_time))) 768c2ecf20Sopenharmony_ci return -EFAULT; 778c2ecf20Sopenharmony_ci 788c2ecf20Sopenharmony_ci yrs = rtc_tm.tm_year; 798c2ecf20Sopenharmony_ci if (yrs < 1900) 808c2ecf20Sopenharmony_ci yrs += 1900; 818c2ecf20Sopenharmony_ci mon = rtc_tm.tm_mon + 1; /* tm_mon starts at zero */ 828c2ecf20Sopenharmony_ci day = rtc_tm.tm_mday; 838c2ecf20Sopenharmony_ci hrs = rtc_tm.tm_hour; 848c2ecf20Sopenharmony_ci min = rtc_tm.tm_min; 858c2ecf20Sopenharmony_ci sec = rtc_tm.tm_sec; 868c2ecf20Sopenharmony_ci 878c2ecf20Sopenharmony_ci leap_yr = ((!(yrs % 4) && (yrs % 100)) || !(yrs % 400)); 888c2ecf20Sopenharmony_ci 898c2ecf20Sopenharmony_ci if ((mon > 12) || (day == 0)) 908c2ecf20Sopenharmony_ci return -EINVAL; 918c2ecf20Sopenharmony_ci 928c2ecf20Sopenharmony_ci if (day > (days_in_mo[mon] + ((mon == 2) && leap_yr))) 938c2ecf20Sopenharmony_ci return -EINVAL; 948c2ecf20Sopenharmony_ci 958c2ecf20Sopenharmony_ci if ((hrs >= 24) || (min >= 60) || (sec >= 60)) 968c2ecf20Sopenharmony_ci return -EINVAL; 978c2ecf20Sopenharmony_ci 988c2ecf20Sopenharmony_ci if (yrs >= 2070) 998c2ecf20Sopenharmony_ci return -EINVAL; 1008c2ecf20Sopenharmony_ci 1018c2ecf20Sopenharmony_ci local_irq_save(flags); 1028c2ecf20Sopenharmony_ci rtc->ctrl = RTC_WRITE; 1038c2ecf20Sopenharmony_ci 1048c2ecf20Sopenharmony_ci rtc->bcd_sec = bin2bcd(sec); 1058c2ecf20Sopenharmony_ci rtc->bcd_min = bin2bcd(min); 1068c2ecf20Sopenharmony_ci rtc->bcd_hr = bin2bcd(hrs); 1078c2ecf20Sopenharmony_ci rtc->bcd_dom = bin2bcd(day); 1088c2ecf20Sopenharmony_ci rtc->bcd_mth = bin2bcd(mon); 1098c2ecf20Sopenharmony_ci rtc->bcd_year = bin2bcd(yrs%100); 1108c2ecf20Sopenharmony_ci 1118c2ecf20Sopenharmony_ci rtc->ctrl = 0; 1128c2ecf20Sopenharmony_ci local_irq_restore(flags); 1138c2ecf20Sopenharmony_ci return 0; 1148c2ecf20Sopenharmony_ci } 1158c2ecf20Sopenharmony_ci default: 1168c2ecf20Sopenharmony_ci return -EINVAL; 1178c2ecf20Sopenharmony_ci } 1188c2ecf20Sopenharmony_ci} 1198c2ecf20Sopenharmony_ci 1208c2ecf20Sopenharmony_ci/* 1218c2ecf20Sopenharmony_ci * We enforce only one user at a time here with the open/close. 1228c2ecf20Sopenharmony_ci */ 1238c2ecf20Sopenharmony_cistatic int rtc_open(struct inode *inode, struct file *file) 1248c2ecf20Sopenharmony_ci{ 1258c2ecf20Sopenharmony_ci if( !atomic_dec_and_test(&rtc_ready) ) 1268c2ecf20Sopenharmony_ci { 1278c2ecf20Sopenharmony_ci atomic_inc( &rtc_ready ); 1288c2ecf20Sopenharmony_ci return -EBUSY; 1298c2ecf20Sopenharmony_ci } 1308c2ecf20Sopenharmony_ci return 0; 1318c2ecf20Sopenharmony_ci} 1328c2ecf20Sopenharmony_ci 1338c2ecf20Sopenharmony_cistatic int rtc_release(struct inode *inode, struct file *file) 1348c2ecf20Sopenharmony_ci{ 1358c2ecf20Sopenharmony_ci atomic_inc( &rtc_ready ); 1368c2ecf20Sopenharmony_ci return 0; 1378c2ecf20Sopenharmony_ci} 1388c2ecf20Sopenharmony_ci 1398c2ecf20Sopenharmony_ci/* 1408c2ecf20Sopenharmony_ci * The various file operations we support. 1418c2ecf20Sopenharmony_ci */ 1428c2ecf20Sopenharmony_ci 1438c2ecf20Sopenharmony_cistatic const struct file_operations rtc_fops = { 1448c2ecf20Sopenharmony_ci .unlocked_ioctl = rtc_ioctl, 1458c2ecf20Sopenharmony_ci .open = rtc_open, 1468c2ecf20Sopenharmony_ci .release = rtc_release, 1478c2ecf20Sopenharmony_ci .llseek = noop_llseek, 1488c2ecf20Sopenharmony_ci}; 1498c2ecf20Sopenharmony_ci 1508c2ecf20Sopenharmony_cistatic struct miscdevice rtc_dev= 1518c2ecf20Sopenharmony_ci{ 1528c2ecf20Sopenharmony_ci .minor = RTC_MINOR, 1538c2ecf20Sopenharmony_ci .name = "rtc", 1548c2ecf20Sopenharmony_ci .fops = &rtc_fops 1558c2ecf20Sopenharmony_ci}; 1568c2ecf20Sopenharmony_ci 1578c2ecf20Sopenharmony_cistatic int __init rtc_MK48T08_init(void) 1588c2ecf20Sopenharmony_ci{ 1598c2ecf20Sopenharmony_ci if (!MACH_IS_MVME16x) 1608c2ecf20Sopenharmony_ci return -ENODEV; 1618c2ecf20Sopenharmony_ci 1628c2ecf20Sopenharmony_ci pr_info("MK48T08 Real Time Clock Driver v%s\n", RTC_VERSION); 1638c2ecf20Sopenharmony_ci return misc_register(&rtc_dev); 1648c2ecf20Sopenharmony_ci} 1658c2ecf20Sopenharmony_cidevice_initcall(rtc_MK48T08_init); 166