/arch/mips/mti-malta/malta-time.c

https://github.com/google/kmsan · C · 256 lines · 190 code · 42 blank · 24 comment · 38 complexity · bc863e483e584d2246e2d24cbd830bf9 MD5 · raw file

  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Carsten Langgaard, carstenl@mips.com
  4. * Copyright (C) 1999,2000 MIPS Technologies, Inc. All rights reserved.
  5. *
  6. * Setting up the clock on the MIPS boards.
  7. */
  8. #include <linux/types.h>
  9. #include <linux/i8253.h>
  10. #include <linux/init.h>
  11. #include <linux/kernel_stat.h>
  12. #include <linux/libfdt.h>
  13. #include <linux/math64.h>
  14. #include <linux/sched.h>
  15. #include <linux/spinlock.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/timex.h>
  18. #include <linux/mc146818rtc.h>
  19. #include <asm/cpu.h>
  20. #include <asm/mipsregs.h>
  21. #include <asm/mipsmtregs.h>
  22. #include <asm/hardirq.h>
  23. #include <asm/irq.h>
  24. #include <asm/div64.h>
  25. #include <asm/setup.h>
  26. #include <asm/time.h>
  27. #include <asm/mc146818-time.h>
  28. #include <asm/msc01_ic.h>
  29. #include <asm/mips-cps.h>
  30. #include <asm/mips-boards/generic.h>
  31. #include <asm/mips-boards/maltaint.h>
  32. static int mips_cpu_timer_irq;
  33. static int mips_cpu_perf_irq;
  34. extern int cp0_perfcount_irq;
  35. static unsigned int gic_frequency;
  36. static void mips_timer_dispatch(void)
  37. {
  38. do_IRQ(mips_cpu_timer_irq);
  39. }
  40. static void mips_perf_dispatch(void)
  41. {
  42. do_IRQ(mips_cpu_perf_irq);
  43. }
  44. static unsigned int freqround(unsigned int freq, unsigned int amount)
  45. {
  46. freq += amount;
  47. freq -= freq % (amount*2);
  48. return freq;
  49. }
  50. /*
  51. * Estimate CPU and GIC frequencies.
  52. */
  53. static void __init estimate_frequencies(void)
  54. {
  55. unsigned long flags;
  56. unsigned int count, start;
  57. unsigned char secs1, secs2, ctrl;
  58. int secs;
  59. u64 giccount = 0, gicstart = 0;
  60. #if defined(CONFIG_KVM_GUEST) && CONFIG_KVM_GUEST_TIMER_FREQ
  61. mips_hpt_frequency = CONFIG_KVM_GUEST_TIMER_FREQ * 1000000;
  62. return;
  63. #endif
  64. local_irq_save(flags);
  65. if (mips_gic_present())
  66. clear_gic_config(GIC_CONFIG_COUNTSTOP);
  67. /*
  68. * Read counters exactly on rising edge of update flag.
  69. * This helps get an accurate reading under virtualisation.
  70. */
  71. while (CMOS_READ(RTC_REG_A) & RTC_UIP);
  72. while (!(CMOS_READ(RTC_REG_A) & RTC_UIP));
  73. start = read_c0_count();
  74. if (mips_gic_present())
  75. gicstart = read_gic_counter();
  76. /* Wait for falling edge before reading RTC. */
  77. while (CMOS_READ(RTC_REG_A) & RTC_UIP);
  78. secs1 = CMOS_READ(RTC_SECONDS);
  79. /* Read counters again exactly on rising edge of update flag. */
  80. while (!(CMOS_READ(RTC_REG_A) & RTC_UIP));
  81. count = read_c0_count();
  82. if (mips_gic_present())
  83. giccount = read_gic_counter();
  84. /* Wait for falling edge before reading RTC again. */
  85. while (CMOS_READ(RTC_REG_A) & RTC_UIP);
  86. secs2 = CMOS_READ(RTC_SECONDS);
  87. ctrl = CMOS_READ(RTC_CONTROL);
  88. local_irq_restore(flags);
  89. if (!(ctrl & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
  90. secs1 = bcd2bin(secs1);
  91. secs2 = bcd2bin(secs2);
  92. }
  93. secs = secs2 - secs1;
  94. if (secs < 1)
  95. secs += 60;
  96. count -= start;
  97. count /= secs;
  98. mips_hpt_frequency = count;
  99. if (mips_gic_present()) {
  100. giccount = div_u64(giccount - gicstart, secs);
  101. gic_frequency = giccount;
  102. }
  103. }
  104. void read_persistent_clock64(struct timespec64 *ts)
  105. {
  106. ts->tv_sec = mc146818_get_cmos_time();
  107. ts->tv_nsec = 0;
  108. }
  109. int get_c0_fdc_int(void)
  110. {
  111. /*
  112. * Some cores claim the FDC is routable through the GIC, but it doesn't
  113. * actually seem to be connected for those Malta bitstreams.
  114. */
  115. switch (current_cpu_type()) {
  116. case CPU_INTERAPTIV:
  117. case CPU_PROAPTIV:
  118. return -1;
  119. };
  120. if (cpu_has_veic)
  121. return -1;
  122. else if (mips_gic_present())
  123. return gic_get_c0_fdc_int();
  124. else if (cp0_fdc_irq >= 0)
  125. return MIPS_CPU_IRQ_BASE + cp0_fdc_irq;
  126. else
  127. return -1;
  128. }
  129. int get_c0_perfcount_int(void)
  130. {
  131. if (cpu_has_veic) {
  132. set_vi_handler(MSC01E_INT_PERFCTR, mips_perf_dispatch);
  133. mips_cpu_perf_irq = MSC01E_INT_BASE + MSC01E_INT_PERFCTR;
  134. } else if (mips_gic_present()) {
  135. mips_cpu_perf_irq = gic_get_c0_perfcount_int();
  136. } else if (cp0_perfcount_irq >= 0) {
  137. mips_cpu_perf_irq = MIPS_CPU_IRQ_BASE + cp0_perfcount_irq;
  138. } else {
  139. mips_cpu_perf_irq = -1;
  140. }
  141. return mips_cpu_perf_irq;
  142. }
  143. EXPORT_SYMBOL_GPL(get_c0_perfcount_int);
  144. unsigned int get_c0_compare_int(void)
  145. {
  146. if (cpu_has_veic) {
  147. set_vi_handler(MSC01E_INT_CPUCTR, mips_timer_dispatch);
  148. mips_cpu_timer_irq = MSC01E_INT_BASE + MSC01E_INT_CPUCTR;
  149. } else if (mips_gic_present()) {
  150. mips_cpu_timer_irq = gic_get_c0_compare_int();
  151. } else {
  152. mips_cpu_timer_irq = MIPS_CPU_IRQ_BASE + cp0_compare_irq;
  153. }
  154. return mips_cpu_timer_irq;
  155. }
  156. static void __init init_rtc(void)
  157. {
  158. unsigned char freq, ctrl;
  159. /* Set 32KHz time base if not already set */
  160. freq = CMOS_READ(RTC_FREQ_SELECT);
  161. if ((freq & RTC_DIV_CTL) != RTC_REF_CLCK_32KHZ)
  162. CMOS_WRITE(RTC_REF_CLCK_32KHZ, RTC_FREQ_SELECT);
  163. /* Ensure SET bit is clear so RTC can run */
  164. ctrl = CMOS_READ(RTC_CONTROL);
  165. if (ctrl & RTC_SET)
  166. CMOS_WRITE(ctrl & ~RTC_SET, RTC_CONTROL);
  167. }
  168. #ifdef CONFIG_CLKSRC_MIPS_GIC
  169. static u32 gic_frequency_dt;
  170. static struct property gic_frequency_prop = {
  171. .name = "clock-frequency",
  172. .length = sizeof(u32),
  173. .value = &gic_frequency_dt,
  174. };
  175. static void update_gic_frequency_dt(void)
  176. {
  177. struct device_node *node;
  178. gic_frequency_dt = cpu_to_be32(gic_frequency);
  179. node = of_find_compatible_node(NULL, NULL, "mti,gic-timer");
  180. if (!node) {
  181. pr_err("mti,gic-timer device node not found\n");
  182. return;
  183. }
  184. if (of_update_property(node, &gic_frequency_prop) < 0)
  185. pr_err("error updating gic frequency property\n");
  186. }
  187. #endif
  188. void __init plat_time_init(void)
  189. {
  190. unsigned int prid = read_c0_prid() & (PRID_COMP_MASK | PRID_IMP_MASK);
  191. unsigned int freq;
  192. init_rtc();
  193. estimate_frequencies();
  194. freq = mips_hpt_frequency;
  195. if ((prid != (PRID_COMP_MIPS | PRID_IMP_20KC)) &&
  196. (prid != (PRID_COMP_MIPS | PRID_IMP_25KF)))
  197. freq *= 2;
  198. freq = freqround(freq, 5000);
  199. printk("CPU frequency %d.%02d MHz\n", freq/1000000,
  200. (freq%1000000)*100/1000000);
  201. #ifdef CONFIG_I8253
  202. /* Only Malta has a PIT. */
  203. setup_pit_timer();
  204. #endif
  205. if (mips_gic_present()) {
  206. freq = freqround(gic_frequency, 5000);
  207. printk("GIC frequency %d.%02d MHz\n", freq/1000000,
  208. (freq%1000000)*100/1000000);
  209. #ifdef CONFIG_CLKSRC_MIPS_GIC
  210. update_gic_frequency_dt();
  211. timer_probe();
  212. #endif
  213. }
  214. }