pmac_time.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 <linux/rtc.h>
  25. #include <asm/sections.h>
  26. #include <asm/prom.h>
  27. #include <asm/system.h>
  28. #include <asm/io.h>
  29. #include <asm/pgtable.h>
  30. #include <asm/machdep.h>
  31. #include <asm/time.h>
  32. #include <asm/nvram.h>
  33. #include <asm/smu.h>
  34. #undef DEBUG
  35. #ifdef DEBUG
  36. #define DBG(x...) printk(x)
  37. #else
  38. #define DBG(x...)
  39. #endif
  40. /* Apparently the RTC stores seconds since 1 Jan 1904 */
  41. #define RTC_OFFSET 2082844800
  42. /*
  43. * Calibrate the decrementer frequency with the VIA timer 1.
  44. */
  45. #define VIA_TIMER_FREQ_6 4700000 /* time 1 frequency * 6 */
  46. extern struct timezone sys_tz;
  47. extern void to_tm(int tim, struct rtc_time * tm);
  48. void pmac_get_rtc_time(struct rtc_time *tm)
  49. {
  50. switch(sys_ctrler) {
  51. #ifdef CONFIG_ADB_PMU
  52. case SYS_CTRLER_PMU: {
  53. /* TODO: Move that to a function in the PMU driver */
  54. struct adb_request req;
  55. unsigned int now;
  56. if (pmu_request(&req, NULL, 1, PMU_READ_RTC) < 0)
  57. return;
  58. pmu_wait_complete(&req);
  59. if (req.reply_len != 4)
  60. printk(KERN_ERR "pmac_get_rtc_time: PMU returned a %d"
  61. " bytes reply\n", req.reply_len);
  62. now = (req.reply[0] << 24) + (req.reply[1] << 16)
  63. + (req.reply[2] << 8) + req.reply[3];
  64. DBG("get: %u -> %u\n", (int)now, (int)(now - RTC_OFFSET));
  65. now -= RTC_OFFSET;
  66. to_tm(now, tm);
  67. tm->tm_year -= 1900;
  68. tm->tm_mon -= 1;
  69. DBG("-> tm_mday: %d, tm_mon: %d, tm_year: %d, %d:%02d:%02d\n",
  70. tm->tm_mday, tm->tm_mon, tm->tm_year,
  71. tm->tm_hour, tm->tm_min, tm->tm_sec);
  72. break;
  73. }
  74. #endif /* CONFIG_ADB_PMU */
  75. #ifdef CONFIG_PMAC_SMU
  76. case SYS_CTRLER_SMU:
  77. smu_get_rtc_time(tm, 1);
  78. break;
  79. #endif /* CONFIG_PMAC_SMU */
  80. default:
  81. ;
  82. }
  83. }
  84. int pmac_set_rtc_time(struct rtc_time *tm)
  85. {
  86. switch(sys_ctrler) {
  87. #ifdef CONFIG_ADB_PMU
  88. case SYS_CTRLER_PMU: {
  89. /* TODO: Move that to a function in the PMU driver */
  90. struct adb_request req;
  91. unsigned int nowtime;
  92. DBG("set: tm_mday: %d, tm_mon: %d, tm_year: %d,"
  93. " %d:%02d:%02d\n",
  94. tm->tm_mday, tm->tm_mon, tm->tm_year,
  95. tm->tm_hour, tm->tm_min, tm->tm_sec);
  96. nowtime = mktime(tm->tm_year + 1900, tm->tm_mon + 1,
  97. tm->tm_mday, tm->tm_hour, tm->tm_min,
  98. tm->tm_sec);
  99. DBG("-> %u -> %u\n", (int)nowtime,
  100. (int)(nowtime + RTC_OFFSET));
  101. nowtime += RTC_OFFSET;
  102. if (pmu_request(&req, NULL, 5, PMU_SET_RTC,
  103. nowtime >> 24, nowtime >> 16,
  104. nowtime >> 8, nowtime) < 0)
  105. return -ENXIO;
  106. pmu_wait_complete(&req);
  107. if (req.reply_len != 0)
  108. printk(KERN_ERR "pmac_set_rtc_time: PMU returned a %d"
  109. " bytes reply\n", req.reply_len);
  110. return 0;
  111. }
  112. #endif /* CONFIG_ADB_PMU */
  113. #ifdef CONFIG_PMAC_SMU
  114. case SYS_CTRLER_SMU:
  115. return smu_set_rtc_time(tm, 1);
  116. #endif /* CONFIG_PMAC_SMU */
  117. default:
  118. return -ENODEV;
  119. }
  120. }
  121. unsigned long __init pmac_get_boot_time(void)
  122. {
  123. struct rtc_time tm;
  124. pmac_get_rtc_time(&tm);
  125. return mktime(tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday,
  126. tm.tm_hour, tm.tm_min, tm.tm_sec);
  127. }
  128. /*
  129. * Query the OF and get the decr frequency.
  130. * FIXME: merge this with generic_calibrate_decr
  131. */
  132. void __init pmac_calibrate_decr(void)
  133. {
  134. struct device_node *cpu;
  135. unsigned int *fp;
  136. /*
  137. * The cpu node should have a timebase-frequency property
  138. * to tell us the rate at which the decrementer counts.
  139. */
  140. cpu = find_type_devices("cpu");
  141. if (cpu == 0)
  142. panic("can't find cpu node in time_init");
  143. fp = (unsigned int *) get_property(cpu, "timebase-frequency", NULL);
  144. if (fp == 0)
  145. panic("can't get cpu timebase frequency");
  146. ppc_tb_freq = *fp;
  147. fp = (unsigned int *)get_property(cpu, "clock-frequency", NULL);
  148. if (fp == 0)
  149. panic("can't get cpu processor frequency");
  150. ppc_proc_freq = *fp;
  151. }