/arch/ppc64/kernel/pmac_time.c

https://bitbucket.org/evzijst/gittest · C · 201 lines · 148 code · 27 blank · 26 comment · 16 complexity · f241506a11c9768c1ae1a3e69468fc1d MD5 · raw file

  1. /*
  2. * Support for periodic interrupts (100 per second) and for getting
  3. * the current time from the RTC on Power Macintoshes.
  4. *
  5. * We use the decrementer register for our periodic interrupts.
  6. *
  7. * Paul Mackerras August 1996.
  8. * Copyright (C) 1996 Paul Mackerras.
  9. * Copyright (C) 2003-2005 Benjamin Herrenschmidt.
  10. *
  11. */
  12. #include <linux/config.h>
  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/pmu.h>
  23. #include <linux/interrupt.h>
  24. #include <asm/sections.h>
  25. #include <asm/prom.h>
  26. #include <asm/system.h>
  27. #include <asm/io.h>
  28. #include <asm/pgtable.h>
  29. #include <asm/machdep.h>
  30. #include <asm/time.h>
  31. #include <asm/nvram.h>
  32. #include <asm/smu.h>
  33. #undef DEBUG
  34. #ifdef DEBUG
  35. #define DBG(x...) printk(x)
  36. #else
  37. #define DBG(x...)
  38. #endif
  39. extern void setup_default_decr(void);
  40. extern unsigned long ppc_tb_freq;
  41. extern unsigned long ppc_proc_freq;
  42. /* Apparently the RTC stores seconds since 1 Jan 1904 */
  43. #define RTC_OFFSET 2082844800
  44. /*
  45. * Calibrate the decrementer frequency with the VIA timer 1.
  46. */
  47. #define VIA_TIMER_FREQ_6 4700000 /* time 1 frequency * 6 */
  48. extern struct timezone sys_tz;
  49. extern void to_tm(int tim, struct rtc_time * tm);
  50. void __pmac pmac_get_rtc_time(struct rtc_time *tm)
  51. {
  52. switch(sys_ctrler) {
  53. #ifdef CONFIG_ADB_PMU
  54. case SYS_CTRLER_PMU: {
  55. /* TODO: Move that to a function in the PMU driver */
  56. struct adb_request req;
  57. unsigned int now;
  58. if (pmu_request(&req, NULL, 1, PMU_READ_RTC) < 0)
  59. return;
  60. pmu_wait_complete(&req);
  61. if (req.reply_len != 4)
  62. printk(KERN_ERR "pmac_get_rtc_time: PMU returned a %d"
  63. " bytes reply\n", req.reply_len);
  64. now = (req.reply[0] << 24) + (req.reply[1] << 16)
  65. + (req.reply[2] << 8) + req.reply[3];
  66. DBG("get: %u -> %u\n", (int)now, (int)(now - RTC_OFFSET));
  67. now -= RTC_OFFSET;
  68. to_tm(now, tm);
  69. tm->tm_year -= 1900;
  70. tm->tm_mon -= 1;
  71. DBG("-> tm_mday: %d, tm_mon: %d, tm_year: %d, %d:%02d:%02d\n",
  72. tm->tm_mday, tm->tm_mon, tm->tm_year,
  73. tm->tm_hour, tm->tm_min, tm->tm_sec);
  74. break;
  75. }
  76. #endif /* CONFIG_ADB_PMU */
  77. #ifdef CONFIG_PMAC_SMU
  78. case SYS_CTRLER_SMU:
  79. smu_get_rtc_time(tm);
  80. break;
  81. #endif /* CONFIG_PMAC_SMU */
  82. default:
  83. ;
  84. }
  85. }
  86. int __pmac pmac_set_rtc_time(struct rtc_time *tm)
  87. {
  88. switch(sys_ctrler) {
  89. #ifdef CONFIG_ADB_PMU
  90. case SYS_CTRLER_PMU: {
  91. /* TODO: Move that to a function in the PMU driver */
  92. struct adb_request req;
  93. unsigned int nowtime;
  94. DBG("set: tm_mday: %d, tm_mon: %d, tm_year: %d,"
  95. " %d:%02d:%02d\n",
  96. tm->tm_mday, tm->tm_mon, tm->tm_year,
  97. tm->tm_hour, tm->tm_min, tm->tm_sec);
  98. nowtime = mktime(tm->tm_year + 1900, tm->tm_mon + 1,
  99. tm->tm_mday, tm->tm_hour, tm->tm_min,
  100. tm->tm_sec);
  101. DBG("-> %u -> %u\n", (int)nowtime,
  102. (int)(nowtime + RTC_OFFSET));
  103. nowtime += RTC_OFFSET;
  104. if (pmu_request(&req, NULL, 5, PMU_SET_RTC,
  105. nowtime >> 24, nowtime >> 16,
  106. nowtime >> 8, nowtime) < 0)
  107. return -ENXIO;
  108. pmu_wait_complete(&req);
  109. if (req.reply_len != 0)
  110. printk(KERN_ERR "pmac_set_rtc_time: PMU returned a %d"
  111. " bytes reply\n", req.reply_len);
  112. return 0;
  113. }
  114. #endif /* CONFIG_ADB_PMU */
  115. #ifdef CONFIG_PMAC_SMU
  116. case SYS_CTRLER_SMU:
  117. return smu_set_rtc_time(tm);
  118. #endif /* CONFIG_PMAC_SMU */
  119. default:
  120. return -ENODEV;
  121. }
  122. }
  123. void __init pmac_get_boot_time(struct rtc_time *tm)
  124. {
  125. pmac_get_rtc_time(tm);
  126. #ifdef disabled__CONFIG_NVRAM
  127. s32 delta = 0;
  128. int dst;
  129. delta = ((s32)pmac_xpram_read(PMAC_XPRAM_MACHINE_LOC + 0x9)) << 16;
  130. delta |= ((s32)pmac_xpram_read(PMAC_XPRAM_MACHINE_LOC + 0xa)) << 8;
  131. delta |= pmac_xpram_read(PMAC_XPRAM_MACHINE_LOC + 0xb);
  132. if (delta & 0x00800000UL)
  133. delta |= 0xFF000000UL;
  134. dst = ((pmac_xpram_read(PMAC_XPRAM_MACHINE_LOC + 0x8) & 0x80) != 0);
  135. printk("GMT Delta read from XPRAM: %d minutes, DST: %s\n", delta/60,
  136. dst ? "on" : "off");
  137. #endif
  138. }
  139. /*
  140. * Query the OF and get the decr frequency.
  141. * This was taken from the pmac time_init() when merging the prep/pmac
  142. * time functions.
  143. */
  144. void __init pmac_calibrate_decr(void)
  145. {
  146. struct device_node *cpu;
  147. unsigned int freq, *fp;
  148. struct div_result divres;
  149. /*
  150. * The cpu node should have a timebase-frequency property
  151. * to tell us the rate at which the decrementer counts.
  152. */
  153. cpu = find_type_devices("cpu");
  154. if (cpu == 0)
  155. panic("can't find cpu node in time_init");
  156. fp = (unsigned int *) get_property(cpu, "timebase-frequency", NULL);
  157. if (fp == 0)
  158. panic("can't get cpu timebase frequency");
  159. freq = *fp;
  160. printk("time_init: decrementer frequency = %u.%.6u MHz\n",
  161. freq/1000000, freq%1000000);
  162. tb_ticks_per_jiffy = freq / HZ;
  163. tb_ticks_per_sec = tb_ticks_per_jiffy * HZ;
  164. tb_ticks_per_usec = freq / 1000000;
  165. tb_to_us = mulhwu_scale_factor(freq, 1000000);
  166. div128_by_32( 1024*1024, 0, tb_ticks_per_sec, &divres );
  167. tb_to_xs = divres.result_low;
  168. ppc_tb_freq = freq;
  169. fp = (unsigned int *)get_property(cpu, "clock-frequency", NULL);
  170. if (fp == 0)
  171. panic("can't get cpu processor frequency");
  172. ppc_proc_freq = *fp;
  173. setup_default_decr();
  174. }