pmac_time.c 4.9 KB

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