18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * (c) Copyright 2004 Benjamin Herrenschmidt (benh@kernel.crashing.org), 48c2ecf20Sopenharmony_ci * IBM Corp. 58c2ecf20Sopenharmony_ci */ 68c2ecf20Sopenharmony_ci 78c2ecf20Sopenharmony_ci#undef DEBUG 88c2ecf20Sopenharmony_ci 98c2ecf20Sopenharmony_ci#include <linux/errno.h> 108c2ecf20Sopenharmony_ci#include <linux/sched.h> 118c2ecf20Sopenharmony_ci#include <linux/kernel.h> 128c2ecf20Sopenharmony_ci#include <linux/param.h> 138c2ecf20Sopenharmony_ci#include <linux/string.h> 148c2ecf20Sopenharmony_ci#include <linux/mm.h> 158c2ecf20Sopenharmony_ci#include <linux/init.h> 168c2ecf20Sopenharmony_ci#include <linux/time.h> 178c2ecf20Sopenharmony_ci#include <linux/adb.h> 188c2ecf20Sopenharmony_ci#include <linux/pmu.h> 198c2ecf20Sopenharmony_ci#include <linux/interrupt.h> 208c2ecf20Sopenharmony_ci#include <linux/mc146818rtc.h> 218c2ecf20Sopenharmony_ci#include <linux/bcd.h> 228c2ecf20Sopenharmony_ci 238c2ecf20Sopenharmony_ci#include <asm/sections.h> 248c2ecf20Sopenharmony_ci#include <asm/prom.h> 258c2ecf20Sopenharmony_ci#include <asm/io.h> 268c2ecf20Sopenharmony_ci#include <asm/machdep.h> 278c2ecf20Sopenharmony_ci#include <asm/time.h> 288c2ecf20Sopenharmony_ci 298c2ecf20Sopenharmony_ci#include "maple.h" 308c2ecf20Sopenharmony_ci 318c2ecf20Sopenharmony_ci#ifdef DEBUG 328c2ecf20Sopenharmony_ci#define DBG(x...) printk(x) 338c2ecf20Sopenharmony_ci#else 348c2ecf20Sopenharmony_ci#define DBG(x...) 358c2ecf20Sopenharmony_ci#endif 368c2ecf20Sopenharmony_ci 378c2ecf20Sopenharmony_cistatic int maple_rtc_addr; 388c2ecf20Sopenharmony_ci 398c2ecf20Sopenharmony_cistatic int maple_clock_read(int addr) 408c2ecf20Sopenharmony_ci{ 418c2ecf20Sopenharmony_ci outb_p(addr, maple_rtc_addr); 428c2ecf20Sopenharmony_ci return inb_p(maple_rtc_addr+1); 438c2ecf20Sopenharmony_ci} 448c2ecf20Sopenharmony_ci 458c2ecf20Sopenharmony_cistatic void maple_clock_write(unsigned long val, int addr) 468c2ecf20Sopenharmony_ci{ 478c2ecf20Sopenharmony_ci outb_p(addr, maple_rtc_addr); 488c2ecf20Sopenharmony_ci outb_p(val, maple_rtc_addr+1); 498c2ecf20Sopenharmony_ci} 508c2ecf20Sopenharmony_ci 518c2ecf20Sopenharmony_civoid maple_get_rtc_time(struct rtc_time *tm) 528c2ecf20Sopenharmony_ci{ 538c2ecf20Sopenharmony_ci do { 548c2ecf20Sopenharmony_ci tm->tm_sec = maple_clock_read(RTC_SECONDS); 558c2ecf20Sopenharmony_ci tm->tm_min = maple_clock_read(RTC_MINUTES); 568c2ecf20Sopenharmony_ci tm->tm_hour = maple_clock_read(RTC_HOURS); 578c2ecf20Sopenharmony_ci tm->tm_mday = maple_clock_read(RTC_DAY_OF_MONTH); 588c2ecf20Sopenharmony_ci tm->tm_mon = maple_clock_read(RTC_MONTH); 598c2ecf20Sopenharmony_ci tm->tm_year = maple_clock_read(RTC_YEAR); 608c2ecf20Sopenharmony_ci } while (tm->tm_sec != maple_clock_read(RTC_SECONDS)); 618c2ecf20Sopenharmony_ci 628c2ecf20Sopenharmony_ci if (!(maple_clock_read(RTC_CONTROL) & RTC_DM_BINARY) 638c2ecf20Sopenharmony_ci || RTC_ALWAYS_BCD) { 648c2ecf20Sopenharmony_ci tm->tm_sec = bcd2bin(tm->tm_sec); 658c2ecf20Sopenharmony_ci tm->tm_min = bcd2bin(tm->tm_min); 668c2ecf20Sopenharmony_ci tm->tm_hour = bcd2bin(tm->tm_hour); 678c2ecf20Sopenharmony_ci tm->tm_mday = bcd2bin(tm->tm_mday); 688c2ecf20Sopenharmony_ci tm->tm_mon = bcd2bin(tm->tm_mon); 698c2ecf20Sopenharmony_ci tm->tm_year = bcd2bin(tm->tm_year); 708c2ecf20Sopenharmony_ci } 718c2ecf20Sopenharmony_ci if ((tm->tm_year + 1900) < 1970) 728c2ecf20Sopenharmony_ci tm->tm_year += 100; 738c2ecf20Sopenharmony_ci 748c2ecf20Sopenharmony_ci tm->tm_wday = -1; 758c2ecf20Sopenharmony_ci} 768c2ecf20Sopenharmony_ci 778c2ecf20Sopenharmony_ciint maple_set_rtc_time(struct rtc_time *tm) 788c2ecf20Sopenharmony_ci{ 798c2ecf20Sopenharmony_ci unsigned char save_control, save_freq_select; 808c2ecf20Sopenharmony_ci int sec, min, hour, mon, mday, year; 818c2ecf20Sopenharmony_ci 828c2ecf20Sopenharmony_ci spin_lock(&rtc_lock); 838c2ecf20Sopenharmony_ci 848c2ecf20Sopenharmony_ci save_control = maple_clock_read(RTC_CONTROL); /* tell the clock it's being set */ 858c2ecf20Sopenharmony_ci 868c2ecf20Sopenharmony_ci maple_clock_write((save_control|RTC_SET), RTC_CONTROL); 878c2ecf20Sopenharmony_ci 888c2ecf20Sopenharmony_ci save_freq_select = maple_clock_read(RTC_FREQ_SELECT); /* stop and reset prescaler */ 898c2ecf20Sopenharmony_ci 908c2ecf20Sopenharmony_ci maple_clock_write((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT); 918c2ecf20Sopenharmony_ci 928c2ecf20Sopenharmony_ci sec = tm->tm_sec; 938c2ecf20Sopenharmony_ci min = tm->tm_min; 948c2ecf20Sopenharmony_ci hour = tm->tm_hour; 958c2ecf20Sopenharmony_ci mon = tm->tm_mon; 968c2ecf20Sopenharmony_ci mday = tm->tm_mday; 978c2ecf20Sopenharmony_ci year = tm->tm_year; 988c2ecf20Sopenharmony_ci 998c2ecf20Sopenharmony_ci if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) { 1008c2ecf20Sopenharmony_ci sec = bin2bcd(sec); 1018c2ecf20Sopenharmony_ci min = bin2bcd(min); 1028c2ecf20Sopenharmony_ci hour = bin2bcd(hour); 1038c2ecf20Sopenharmony_ci mon = bin2bcd(mon); 1048c2ecf20Sopenharmony_ci mday = bin2bcd(mday); 1058c2ecf20Sopenharmony_ci year = bin2bcd(year); 1068c2ecf20Sopenharmony_ci } 1078c2ecf20Sopenharmony_ci maple_clock_write(sec, RTC_SECONDS); 1088c2ecf20Sopenharmony_ci maple_clock_write(min, RTC_MINUTES); 1098c2ecf20Sopenharmony_ci maple_clock_write(hour, RTC_HOURS); 1108c2ecf20Sopenharmony_ci maple_clock_write(mon, RTC_MONTH); 1118c2ecf20Sopenharmony_ci maple_clock_write(mday, RTC_DAY_OF_MONTH); 1128c2ecf20Sopenharmony_ci maple_clock_write(year, RTC_YEAR); 1138c2ecf20Sopenharmony_ci 1148c2ecf20Sopenharmony_ci /* The following flags have to be released exactly in this order, 1158c2ecf20Sopenharmony_ci * otherwise the DS12887 (popular MC146818A clone with integrated 1168c2ecf20Sopenharmony_ci * battery and quartz) will not reset the oscillator and will not 1178c2ecf20Sopenharmony_ci * update precisely 500 ms later. You won't find this mentioned in 1188c2ecf20Sopenharmony_ci * the Dallas Semiconductor data sheets, but who believes data 1198c2ecf20Sopenharmony_ci * sheets anyway ... -- Markus Kuhn 1208c2ecf20Sopenharmony_ci */ 1218c2ecf20Sopenharmony_ci maple_clock_write(save_control, RTC_CONTROL); 1228c2ecf20Sopenharmony_ci maple_clock_write(save_freq_select, RTC_FREQ_SELECT); 1238c2ecf20Sopenharmony_ci 1248c2ecf20Sopenharmony_ci spin_unlock(&rtc_lock); 1258c2ecf20Sopenharmony_ci 1268c2ecf20Sopenharmony_ci return 0; 1278c2ecf20Sopenharmony_ci} 1288c2ecf20Sopenharmony_ci 1298c2ecf20Sopenharmony_cistatic struct resource rtc_iores = { 1308c2ecf20Sopenharmony_ci .name = "rtc", 1318c2ecf20Sopenharmony_ci .flags = IORESOURCE_IO | IORESOURCE_BUSY, 1328c2ecf20Sopenharmony_ci}; 1338c2ecf20Sopenharmony_ci 1348c2ecf20Sopenharmony_citime64_t __init maple_get_boot_time(void) 1358c2ecf20Sopenharmony_ci{ 1368c2ecf20Sopenharmony_ci struct rtc_time tm; 1378c2ecf20Sopenharmony_ci struct device_node *rtcs; 1388c2ecf20Sopenharmony_ci 1398c2ecf20Sopenharmony_ci rtcs = of_find_compatible_node(NULL, "rtc", "pnpPNP,b00"); 1408c2ecf20Sopenharmony_ci if (rtcs) { 1418c2ecf20Sopenharmony_ci struct resource r; 1428c2ecf20Sopenharmony_ci if (of_address_to_resource(rtcs, 0, &r)) { 1438c2ecf20Sopenharmony_ci printk(KERN_EMERG "Maple: Unable to translate RTC" 1448c2ecf20Sopenharmony_ci " address\n"); 1458c2ecf20Sopenharmony_ci goto bail; 1468c2ecf20Sopenharmony_ci } 1478c2ecf20Sopenharmony_ci if (!(r.flags & IORESOURCE_IO)) { 1488c2ecf20Sopenharmony_ci printk(KERN_EMERG "Maple: RTC address isn't PIO!\n"); 1498c2ecf20Sopenharmony_ci goto bail; 1508c2ecf20Sopenharmony_ci } 1518c2ecf20Sopenharmony_ci maple_rtc_addr = r.start; 1528c2ecf20Sopenharmony_ci printk(KERN_INFO "Maple: Found RTC at IO 0x%x\n", 1538c2ecf20Sopenharmony_ci maple_rtc_addr); 1548c2ecf20Sopenharmony_ci } 1558c2ecf20Sopenharmony_ci bail: 1568c2ecf20Sopenharmony_ci if (maple_rtc_addr == 0) { 1578c2ecf20Sopenharmony_ci maple_rtc_addr = RTC_PORT(0); /* legacy address */ 1588c2ecf20Sopenharmony_ci printk(KERN_INFO "Maple: No device node for RTC, assuming " 1598c2ecf20Sopenharmony_ci "legacy address (0x%x)\n", maple_rtc_addr); 1608c2ecf20Sopenharmony_ci } 1618c2ecf20Sopenharmony_ci 1628c2ecf20Sopenharmony_ci rtc_iores.start = maple_rtc_addr; 1638c2ecf20Sopenharmony_ci rtc_iores.end = maple_rtc_addr + 7; 1648c2ecf20Sopenharmony_ci request_resource(&ioport_resource, &rtc_iores); 1658c2ecf20Sopenharmony_ci 1668c2ecf20Sopenharmony_ci maple_get_rtc_time(&tm); 1678c2ecf20Sopenharmony_ci return rtc_tm_to_time64(&tm); 1688c2ecf20Sopenharmony_ci} 1698c2ecf20Sopenharmony_ci 170