time.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Support for periodic interrupts (100 per second) and for getting
  4. * the current time from the RTC on Power Macintoshes.
  5. *
  6. * We use the decrementer register for our periodic interrupts.
  7. *
  8. * Paul Mackerras August 1996.
  9. * Copyright (C) 1996 Paul Mackerras.
  10. * Copyright (C) 2003-2005 Benjamin Herrenschmidt.
  11. *
  12. */
  13. #include <linux/errno.h>
  14. #include <linux/sched.h>
  15. #include <linux/kernel.h>
  16. #include <linux/param.h>
  17. #include <linux/string.h>
  18. #include <linux/mm.h>
  19. #include <linux/init.h>
  20. #include <linux/time.h>
  21. #include <linux/adb.h>
  22. #include <linux/cuda.h>
  23. #include <linux/pmu.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/hardirq.h>
  26. #include <linux/rtc.h>
  27. #include <linux/of_address.h>
  28. #include <asm/early_ioremap.h>
  29. #include <asm/sections.h>
  30. #include <asm/machdep.h>
  31. #include <asm/time.h>
  32. #include <asm/nvram.h>
  33. #include <asm/smu.h>
  34. #include "pmac.h"
  35. #undef DEBUG
  36. #ifdef DEBUG
  37. #define DBG(x...) printk(x)
  38. #else
  39. #define DBG(x...)
  40. #endif
  41. /*
  42. * Calibrate the decrementer frequency with the VIA timer 1.
  43. */
  44. #define VIA_TIMER_FREQ_6 4700000 /* time 1 frequency * 6 */
  45. /* VIA registers */
  46. #define RS 0x200 /* skip between registers */
  47. #define T1CL (4*RS) /* Timer 1 ctr/latch (low 8 bits) */
  48. #define T1CH (5*RS) /* Timer 1 counter (high 8 bits) */
  49. #define T1LL (6*RS) /* Timer 1 latch (low 8 bits) */
  50. #define T1LH (7*RS) /* Timer 1 latch (high 8 bits) */
  51. #define ACR (11*RS) /* Auxiliary control register */
  52. #define IFR (13*RS) /* Interrupt flag register */
  53. /* Bits in ACR */
  54. #define T1MODE 0xc0 /* Timer 1 mode */
  55. #define T1MODE_CONT 0x40 /* continuous interrupts */
  56. /* Bits in IFR and IER */
  57. #define T1_INT 0x40 /* Timer 1 interrupt */
  58. long __init pmac_time_init(void)
  59. {
  60. s32 delta = 0;
  61. #if defined(CONFIG_NVRAM) && defined(CONFIG_PPC32)
  62. int dst;
  63. delta = ((s32)pmac_xpram_read(PMAC_XPRAM_MACHINE_LOC + 0x9)) << 16;
  64. delta |= ((s32)pmac_xpram_read(PMAC_XPRAM_MACHINE_LOC + 0xa)) << 8;
  65. delta |= pmac_xpram_read(PMAC_XPRAM_MACHINE_LOC + 0xb);
  66. if (delta & 0x00800000UL)
  67. delta |= 0xFF000000UL;
  68. dst = ((pmac_xpram_read(PMAC_XPRAM_MACHINE_LOC + 0x8) & 0x80) != 0);
  69. printk("GMT Delta read from XPRAM: %d minutes, DST: %s\n", delta/60,
  70. dst ? "on" : "off");
  71. #endif
  72. return delta;
  73. }
  74. #ifdef CONFIG_PMAC_SMU
  75. static time64_t smu_get_time(void)
  76. {
  77. struct rtc_time tm;
  78. if (smu_get_rtc_time(&tm, 1))
  79. return 0;
  80. return rtc_tm_to_time64(&tm);
  81. }
  82. #endif
  83. /* Can't be __init, it's called when suspending and resuming */
  84. time64_t pmac_get_boot_time(void)
  85. {
  86. /* Get the time from the RTC, used only at boot time */
  87. switch (sys_ctrler) {
  88. #ifdef CONFIG_ADB_CUDA
  89. case SYS_CTRLER_CUDA:
  90. return cuda_get_time();
  91. #endif
  92. #ifdef CONFIG_ADB_PMU
  93. case SYS_CTRLER_PMU:
  94. return pmu_get_time();
  95. #endif
  96. #ifdef CONFIG_PMAC_SMU
  97. case SYS_CTRLER_SMU:
  98. return smu_get_time();
  99. #endif
  100. default:
  101. return 0;
  102. }
  103. }
  104. void pmac_get_rtc_time(struct rtc_time *tm)
  105. {
  106. /* Get the time from the RTC, used only at boot time */
  107. switch (sys_ctrler) {
  108. #ifdef CONFIG_ADB_CUDA
  109. case SYS_CTRLER_CUDA:
  110. rtc_time64_to_tm(cuda_get_time(), tm);
  111. break;
  112. #endif
  113. #ifdef CONFIG_ADB_PMU
  114. case SYS_CTRLER_PMU:
  115. rtc_time64_to_tm(pmu_get_time(), tm);
  116. break;
  117. #endif
  118. #ifdef CONFIG_PMAC_SMU
  119. case SYS_CTRLER_SMU:
  120. smu_get_rtc_time(tm, 1);
  121. break;
  122. #endif
  123. default:
  124. ;
  125. }
  126. }
  127. int pmac_set_rtc_time(struct rtc_time *tm)
  128. {
  129. switch (sys_ctrler) {
  130. #ifdef CONFIG_ADB_CUDA
  131. case SYS_CTRLER_CUDA:
  132. return cuda_set_rtc_time(tm);
  133. #endif
  134. #ifdef CONFIG_ADB_PMU
  135. case SYS_CTRLER_PMU:
  136. return pmu_set_rtc_time(tm);
  137. #endif
  138. #ifdef CONFIG_PMAC_SMU
  139. case SYS_CTRLER_SMU:
  140. return smu_set_rtc_time(tm, 1);
  141. #endif
  142. default:
  143. return -ENODEV;
  144. }
  145. }
  146. #ifdef CONFIG_PPC32
  147. /*
  148. * Calibrate the decrementer register using VIA timer 1.
  149. * This is used both on powermacs and CHRP machines.
  150. */
  151. static int __init via_calibrate_decr(void)
  152. {
  153. struct device_node *vias;
  154. volatile unsigned char __iomem *via;
  155. int count = VIA_TIMER_FREQ_6 / 100;
  156. unsigned int dstart, dend;
  157. struct resource rsrc;
  158. vias = of_find_node_by_name(NULL, "via-cuda");
  159. if (vias == NULL)
  160. vias = of_find_node_by_name(NULL, "via-pmu");
  161. if (vias == NULL)
  162. vias = of_find_node_by_name(NULL, "via");
  163. if (vias == NULL || of_address_to_resource(vias, 0, &rsrc)) {
  164. of_node_put(vias);
  165. return 0;
  166. }
  167. of_node_put(vias);
  168. via = early_ioremap(rsrc.start, resource_size(&rsrc));
  169. if (via == NULL) {
  170. printk(KERN_ERR "Failed to map VIA for timer calibration !\n");
  171. return 0;
  172. }
  173. /* set timer 1 for continuous interrupts */
  174. out_8(&via[ACR], (via[ACR] & ~T1MODE) | T1MODE_CONT);
  175. /* set the counter to a small value */
  176. out_8(&via[T1CH], 2);
  177. /* set the latch to `count' */
  178. out_8(&via[T1LL], count);
  179. out_8(&via[T1LH], count >> 8);
  180. /* wait until it hits 0 */
  181. while ((in_8(&via[IFR]) & T1_INT) == 0)
  182. ;
  183. dstart = get_dec();
  184. /* clear the interrupt & wait until it hits 0 again */
  185. in_8(&via[T1CL]);
  186. while ((in_8(&via[IFR]) & T1_INT) == 0)
  187. ;
  188. dend = get_dec();
  189. ppc_tb_freq = (dstart - dend) * 100 / 6;
  190. early_iounmap((void *)via, resource_size(&rsrc));
  191. return 1;
  192. }
  193. #endif
  194. /*
  195. * Query the OF and get the decr frequency.
  196. */
  197. void __init pmac_calibrate_decr(void)
  198. {
  199. generic_calibrate_decr();
  200. #ifdef CONFIG_PPC32
  201. /* We assume MacRISC2 machines have correct device-tree
  202. * calibration. That's better since the VIA itself seems
  203. * to be slightly off. --BenH
  204. */
  205. if (!of_machine_is_compatible("MacRISC2") &&
  206. !of_machine_is_compatible("MacRISC3") &&
  207. !of_machine_is_compatible("MacRISC4"))
  208. if (via_calibrate_decr())
  209. return;
  210. /* Special case: QuickSilver G4s seem to have a badly calibrated
  211. * timebase-frequency in OF, VIA is much better on these. We should
  212. * probably implement calibration based on the KL timer on these
  213. * machines anyway... -BenH
  214. */
  215. if (of_machine_is_compatible("PowerMac3,5"))
  216. if (via_calibrate_decr())
  217. return;
  218. #endif
  219. }