18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Support code for virtual Ranchu board for MIPS. 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Author: Miodrag Dinic <miodrag.dinic@mips.com> 68c2ecf20Sopenharmony_ci */ 78c2ecf20Sopenharmony_ci 88c2ecf20Sopenharmony_ci#include <linux/of_address.h> 98c2ecf20Sopenharmony_ci#include <linux/types.h> 108c2ecf20Sopenharmony_ci 118c2ecf20Sopenharmony_ci#include <asm/machine.h> 128c2ecf20Sopenharmony_ci#include <asm/mipsregs.h> 138c2ecf20Sopenharmony_ci#include <asm/time.h> 148c2ecf20Sopenharmony_ci 158c2ecf20Sopenharmony_ci#define GOLDFISH_TIMER_LOW 0x00 168c2ecf20Sopenharmony_ci#define GOLDFISH_TIMER_HIGH 0x04 178c2ecf20Sopenharmony_ci 188c2ecf20Sopenharmony_cistatic __init u64 read_rtc_time(void __iomem *base) 198c2ecf20Sopenharmony_ci{ 208c2ecf20Sopenharmony_ci u32 time_low; 218c2ecf20Sopenharmony_ci u32 time_high; 228c2ecf20Sopenharmony_ci 238c2ecf20Sopenharmony_ci /* 248c2ecf20Sopenharmony_ci * Reading the low address latches the high value 258c2ecf20Sopenharmony_ci * as well so there is no fear that we may read 268c2ecf20Sopenharmony_ci * inaccurate high value. 278c2ecf20Sopenharmony_ci */ 288c2ecf20Sopenharmony_ci time_low = readl(base + GOLDFISH_TIMER_LOW); 298c2ecf20Sopenharmony_ci time_high = readl(base + GOLDFISH_TIMER_HIGH); 308c2ecf20Sopenharmony_ci 318c2ecf20Sopenharmony_ci return ((u64)time_high << 32) | time_low; 328c2ecf20Sopenharmony_ci} 338c2ecf20Sopenharmony_ci 348c2ecf20Sopenharmony_cistatic __init unsigned int ranchu_measure_hpt_freq(void) 358c2ecf20Sopenharmony_ci{ 368c2ecf20Sopenharmony_ci u64 rtc_start, rtc_current, rtc_delta; 378c2ecf20Sopenharmony_ci unsigned int start, count; 388c2ecf20Sopenharmony_ci struct device_node *np; 398c2ecf20Sopenharmony_ci void __iomem *rtc_base; 408c2ecf20Sopenharmony_ci 418c2ecf20Sopenharmony_ci np = of_find_compatible_node(NULL, NULL, "google,goldfish-rtc"); 428c2ecf20Sopenharmony_ci if (!np) 438c2ecf20Sopenharmony_ci panic("%s(): Failed to find 'google,goldfish-rtc' dt node!", 448c2ecf20Sopenharmony_ci __func__); 458c2ecf20Sopenharmony_ci 468c2ecf20Sopenharmony_ci rtc_base = of_iomap(np, 0); 478c2ecf20Sopenharmony_ci if (!rtc_base) 488c2ecf20Sopenharmony_ci panic("%s(): Failed to ioremap Goldfish RTC base!", __func__); 498c2ecf20Sopenharmony_ci 508c2ecf20Sopenharmony_ci /* 518c2ecf20Sopenharmony_ci * Poll the nanosecond resolution RTC for one 528c2ecf20Sopenharmony_ci * second to calibrate the CPU frequency. 538c2ecf20Sopenharmony_ci */ 548c2ecf20Sopenharmony_ci rtc_start = read_rtc_time(rtc_base); 558c2ecf20Sopenharmony_ci start = read_c0_count(); 568c2ecf20Sopenharmony_ci 578c2ecf20Sopenharmony_ci do { 588c2ecf20Sopenharmony_ci rtc_current = read_rtc_time(rtc_base); 598c2ecf20Sopenharmony_ci rtc_delta = rtc_current - rtc_start; 608c2ecf20Sopenharmony_ci } while (rtc_delta < NSEC_PER_SEC); 618c2ecf20Sopenharmony_ci 628c2ecf20Sopenharmony_ci count = read_c0_count() - start; 638c2ecf20Sopenharmony_ci 648c2ecf20Sopenharmony_ci /* 658c2ecf20Sopenharmony_ci * Make sure the frequency will be a round number. 668c2ecf20Sopenharmony_ci * Without this correction, the returned value may vary 678c2ecf20Sopenharmony_ci * between subsequent emulation executions. 688c2ecf20Sopenharmony_ci * 698c2ecf20Sopenharmony_ci * TODO: Set this value using device tree. 708c2ecf20Sopenharmony_ci */ 718c2ecf20Sopenharmony_ci count += 5000; 728c2ecf20Sopenharmony_ci count -= count % 10000; 738c2ecf20Sopenharmony_ci 748c2ecf20Sopenharmony_ci iounmap(rtc_base); 758c2ecf20Sopenharmony_ci 768c2ecf20Sopenharmony_ci return count; 778c2ecf20Sopenharmony_ci} 788c2ecf20Sopenharmony_ci 798c2ecf20Sopenharmony_cistatic const struct of_device_id ranchu_of_match[] __initconst = { 808c2ecf20Sopenharmony_ci { 818c2ecf20Sopenharmony_ci .compatible = "mti,ranchu", 828c2ecf20Sopenharmony_ci }, 838c2ecf20Sopenharmony_ci {} 848c2ecf20Sopenharmony_ci}; 858c2ecf20Sopenharmony_ci 868c2ecf20Sopenharmony_ciMIPS_MACHINE(ranchu) = { 878c2ecf20Sopenharmony_ci .matches = ranchu_of_match, 888c2ecf20Sopenharmony_ci .measure_hpt_freq = ranchu_measure_hpt_freq, 898c2ecf20Sopenharmony_ci}; 90