time.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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 <asm/sections.h>
  28. #include <asm/prom.h>
  29. #include <asm/io.h>
  30. #include <asm/pgtable.h>
  31. #include <asm/machdep.h>
  32. #include <asm/time.h>
  33. #include <asm/nvram.h>
  34. #include <asm/smu.h>
  35. #undef DEBUG
  36. #ifdef DEBUG
  37. #define DBG(x...) printk(x)
  38. #else
  39. #define DBG(x...)
  40. #endif
  41. /* Apparently the RTC stores seconds since 1 Jan 1904 */
  42. #define RTC_OFFSET 2082844800
  43. /*
  44. * Calibrate the decrementer frequency with the VIA timer 1.
  45. */
  46. #define VIA_TIMER_FREQ_6 4700000 /* time 1 frequency * 6 */
  47. /* VIA registers */
  48. #define RS 0x200 /* skip between registers */
  49. #define T1CL (4*RS) /* Timer 1 ctr/latch (low 8 bits) */
  50. #define T1CH (5*RS) /* Timer 1 counter (high 8 bits) */
  51. #define T1LL (6*RS) /* Timer 1 latch (low 8 bits) */
  52. #define T1LH (7*RS) /* Timer 1 latch (high 8 bits) */
  53. #define ACR (11*RS) /* Auxiliary control register */
  54. #define IFR (13*RS) /* Interrupt flag register */
  55. /* Bits in ACR */
  56. #define T1MODE 0xc0 /* Timer 1 mode */
  57. #define T1MODE_CONT 0x40 /* continuous interrupts */
  58. /* Bits in IFR and IER */
  59. #define T1_INT 0x40 /* Timer 1 interrupt */
  60. long __init pmac_time_init(void)
  61. {
  62. s32 delta = 0;
  63. #ifdef CONFIG_NVRAM
  64. int dst;
  65. delta = ((s32)pmac_xpram_read(PMAC_XPRAM_MACHINE_LOC + 0x9)) << 16;
  66. delta |= ((s32)pmac_xpram_read(PMAC_XPRAM_MACHINE_LOC + 0xa)) << 8;
  67. delta |= pmac_xpram_read(PMAC_XPRAM_MACHINE_LOC + 0xb);
  68. if (delta & 0x00800000UL)
  69. delta |= 0xFF000000UL;
  70. dst = ((pmac_xpram_read(PMAC_XPRAM_MACHINE_LOC + 0x8) & 0x80) != 0);
  71. printk("GMT Delta read from XPRAM: %d minutes, DST: %s\n", delta/60,
  72. dst ? "on" : "off");
  73. #endif
  74. return delta;
  75. }
  76. #ifdef CONFIG_ADB_CUDA
  77. static time64_t cuda_get_time(void)
  78. {
  79. struct adb_request req;
  80. time64_t now;
  81. if (cuda_request(&req, NULL, 2, CUDA_PACKET, CUDA_GET_TIME) < 0)
  82. return 0;
  83. while (!req.complete)
  84. cuda_poll();
  85. if (req.reply_len != 7)
  86. printk(KERN_ERR "cuda_get_time: got %d byte reply\n",
  87. req.reply_len);
  88. now = (req.reply[3] << 24) + (req.reply[4] << 16)
  89. + (req.reply[5] << 8) + req.reply[6];
  90. return now - RTC_OFFSET;
  91. }
  92. #define cuda_get_rtc_time(tm) rtc_time64_to_tm(cuda_get_time(), (tm))
  93. static int cuda_set_rtc_time(struct rtc_time *tm)
  94. {
  95. time64_t nowtime;
  96. struct adb_request req;
  97. nowtime = rtc_tm_to_time64(tm) + RTC_OFFSET;
  98. if (cuda_request(&req, NULL, 6, CUDA_PACKET, CUDA_SET_TIME,
  99. nowtime >> 24, nowtime >> 16, nowtime >> 8,
  100. nowtime) < 0)
  101. return -ENXIO;
  102. while (!req.complete)
  103. cuda_poll();
  104. if ((req.reply_len != 3) && (req.reply_len != 7))
  105. printk(KERN_ERR "cuda_set_rtc_time: got %d byte reply\n",
  106. req.reply_len);
  107. return 0;
  108. }
  109. #else
  110. #define cuda_get_time() 0
  111. #define cuda_get_rtc_time(tm)
  112. #define cuda_set_rtc_time(tm) 0
  113. #endif
  114. #ifdef CONFIG_ADB_PMU
  115. static time64_t pmu_get_time(void)
  116. {
  117. struct adb_request req;
  118. time64_t now;
  119. if (pmu_request(&req, NULL, 1, PMU_READ_RTC) < 0)
  120. return 0;
  121. pmu_wait_complete(&req);
  122. if (req.reply_len != 4)
  123. printk(KERN_ERR "pmu_get_time: got %d byte reply from PMU\n",
  124. req.reply_len);
  125. now = (req.reply[0] << 24) + (req.reply[1] << 16)
  126. + (req.reply[2] << 8) + req.reply[3];
  127. return now - RTC_OFFSET;
  128. }
  129. #define pmu_get_rtc_time(tm) rtc_time64_to_tm(pmu_get_time(), (tm))
  130. static int pmu_set_rtc_time(struct rtc_time *tm)
  131. {
  132. time64_t nowtime;
  133. struct adb_request req;
  134. nowtime = rtc_tm_to_time64(tm) + RTC_OFFSET;
  135. if (pmu_request(&req, NULL, 5, PMU_SET_RTC, nowtime >> 24,
  136. nowtime >> 16, nowtime >> 8, nowtime) < 0)
  137. return -ENXIO;
  138. pmu_wait_complete(&req);
  139. if (req.reply_len != 0)
  140. printk(KERN_ERR "pmu_set_rtc_time: %d byte reply from PMU\n",
  141. req.reply_len);
  142. return 0;
  143. }
  144. #else
  145. #define pmu_get_time() 0
  146. #define pmu_get_rtc_time(tm)
  147. #define pmu_set_rtc_time(tm) 0
  148. #endif
  149. #ifdef CONFIG_PMAC_SMU
  150. static time64_t smu_get_time(void)
  151. {
  152. struct rtc_time tm;
  153. if (smu_get_rtc_time(&tm, 1))
  154. return 0;
  155. return rtc_tm_to_time64(&tm);
  156. }
  157. #else
  158. #define smu_get_time() 0
  159. #define smu_get_rtc_time(tm, spin)
  160. #define smu_set_rtc_time(tm, spin) 0
  161. #endif
  162. /* Can't be __init, it's called when suspending and resuming */
  163. time64_t pmac_get_boot_time(void)
  164. {
  165. /* Get the time from the RTC, used only at boot time */
  166. switch (sys_ctrler) {
  167. case SYS_CTRLER_CUDA:
  168. return cuda_get_time();
  169. case SYS_CTRLER_PMU:
  170. return pmu_get_time();
  171. case SYS_CTRLER_SMU:
  172. return smu_get_time();
  173. default:
  174. return 0;
  175. }
  176. }
  177. void pmac_get_rtc_time(struct rtc_time *tm)
  178. {
  179. /* Get the time from the RTC, used only at boot time */
  180. switch (sys_ctrler) {
  181. case SYS_CTRLER_CUDA:
  182. cuda_get_rtc_time(tm);
  183. break;
  184. case SYS_CTRLER_PMU:
  185. pmu_get_rtc_time(tm);
  186. break;
  187. case SYS_CTRLER_SMU:
  188. smu_get_rtc_time(tm, 1);
  189. break;
  190. default:
  191. ;
  192. }
  193. }
  194. int pmac_set_rtc_time(struct rtc_time *tm)
  195. {
  196. switch (sys_ctrler) {
  197. case SYS_CTRLER_CUDA:
  198. return cuda_set_rtc_time(tm);
  199. case SYS_CTRLER_PMU:
  200. return pmu_set_rtc_time(tm);
  201. case SYS_CTRLER_SMU:
  202. return smu_set_rtc_time(tm, 1);
  203. default:
  204. return -ENODEV;
  205. }
  206. }
  207. #ifdef CONFIG_PPC32
  208. /*
  209. * Calibrate the decrementer register using VIA timer 1.
  210. * This is used both on powermacs and CHRP machines.
  211. */
  212. int __init via_calibrate_decr(void)
  213. {
  214. struct device_node *vias;
  215. volatile unsigned char __iomem *via;
  216. int count = VIA_TIMER_FREQ_6 / 100;
  217. unsigned int dstart, dend;
  218. struct resource rsrc;
  219. vias = of_find_node_by_name(NULL, "via-cuda");
  220. if (vias == NULL)
  221. vias = of_find_node_by_name(NULL, "via-pmu");
  222. if (vias == NULL)
  223. vias = of_find_node_by_name(NULL, "via");
  224. if (vias == NULL || of_address_to_resource(vias, 0, &rsrc)) {
  225. of_node_put(vias);
  226. return 0;
  227. }
  228. of_node_put(vias);
  229. via = ioremap(rsrc.start, resource_size(&rsrc));
  230. if (via == NULL) {
  231. printk(KERN_ERR "Failed to map VIA for timer calibration !\n");
  232. return 0;
  233. }
  234. /* set timer 1 for continuous interrupts */
  235. out_8(&via[ACR], (via[ACR] & ~T1MODE) | T1MODE_CONT);
  236. /* set the counter to a small value */
  237. out_8(&via[T1CH], 2);
  238. /* set the latch to `count' */
  239. out_8(&via[T1LL], count);
  240. out_8(&via[T1LH], count >> 8);
  241. /* wait until it hits 0 */
  242. while ((in_8(&via[IFR]) & T1_INT) == 0)
  243. ;
  244. dstart = get_dec();
  245. /* clear the interrupt & wait until it hits 0 again */
  246. in_8(&via[T1CL]);
  247. while ((in_8(&via[IFR]) & T1_INT) == 0)
  248. ;
  249. dend = get_dec();
  250. ppc_tb_freq = (dstart - dend) * 100 / 6;
  251. iounmap(via);
  252. return 1;
  253. }
  254. #endif
  255. /*
  256. * Query the OF and get the decr frequency.
  257. */
  258. void __init pmac_calibrate_decr(void)
  259. {
  260. generic_calibrate_decr();
  261. #ifdef CONFIG_PPC32
  262. /* We assume MacRISC2 machines have correct device-tree
  263. * calibration. That's better since the VIA itself seems
  264. * to be slightly off. --BenH
  265. */
  266. if (!of_machine_is_compatible("MacRISC2") &&
  267. !of_machine_is_compatible("MacRISC3") &&
  268. !of_machine_is_compatible("MacRISC4"))
  269. if (via_calibrate_decr())
  270. return;
  271. /* Special case: QuickSilver G4s seem to have a badly calibrated
  272. * timebase-frequency in OF, VIA is much better on these. We should
  273. * probably implement calibration based on the KL timer on these
  274. * machines anyway... -BenH
  275. */
  276. if (of_machine_is_compatible("PowerMac3,5"))
  277. if (via_calibrate_decr())
  278. return;
  279. #endif
  280. }