time.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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/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/system.h>
  30. #include <asm/io.h>
  31. #include <asm/pgtable.h>
  32. #include <asm/machdep.h>
  33. #include <asm/time.h>
  34. #include <asm/nvram.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. #ifdef CONFIG_NVRAM
  63. s32 delta = 0;
  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. return delta;
  74. #else
  75. return 0;
  76. #endif
  77. }
  78. unsigned long pmac_get_boot_time(void)
  79. {
  80. #if defined(CONFIG_ADB_CUDA) || defined(CONFIG_ADB_PMU)
  81. struct adb_request req;
  82. unsigned long now;
  83. #endif
  84. /* Get the time from the RTC */
  85. switch (sys_ctrler) {
  86. #ifdef CONFIG_ADB_CUDA
  87. case SYS_CTRLER_CUDA:
  88. if (cuda_request(&req, NULL, 2, CUDA_PACKET, CUDA_GET_TIME) < 0)
  89. return 0;
  90. while (!req.complete)
  91. cuda_poll();
  92. if (req.reply_len != 7)
  93. printk(KERN_ERR "pmac_get_rtc_time: got %d byte reply\n",
  94. req.reply_len);
  95. now = (req.reply[3] << 24) + (req.reply[4] << 16)
  96. + (req.reply[5] << 8) + req.reply[6];
  97. return now - RTC_OFFSET;
  98. #endif /* CONFIG_ADB_CUDA */
  99. #ifdef CONFIG_ADB_PMU
  100. case SYS_CTRLER_PMU:
  101. if (pmu_request(&req, NULL, 1, PMU_READ_RTC) < 0)
  102. return 0;
  103. while (!req.complete)
  104. pmu_poll();
  105. if (req.reply_len != 4)
  106. printk(KERN_ERR "pmac_get_rtc_time: got %d byte reply\n",
  107. req.reply_len);
  108. now = (req.reply[0] << 24) + (req.reply[1] << 16)
  109. + (req.reply[2] << 8) + req.reply[3];
  110. return now - RTC_OFFSET;
  111. #endif /* CONFIG_ADB_PMU */
  112. default: ;
  113. }
  114. return 0;
  115. }
  116. void pmac_get_rtc_time(struct rtc_time *tm)
  117. {
  118. unsigned long now;
  119. now = pmac_get_boot_time();
  120. to_tm(now, tm);
  121. tm->tm_year -= 1900;
  122. tm->tm_mon -= 1; /* month is 0-based */
  123. }
  124. int pmac_set_rtc_time(struct rtc_time *tm)
  125. {
  126. unsigned long nowtime;
  127. #if defined(CONFIG_ADB_CUDA) || defined(CONFIG_ADB_PMU)
  128. struct adb_request req;
  129. #endif
  130. nowtime = mktime(tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday,
  131. tm->tm_hour, tm->tm_min, tm->tm_sec);
  132. nowtime += RTC_OFFSET;
  133. switch (sys_ctrler) {
  134. #ifdef CONFIG_ADB_CUDA
  135. case SYS_CTRLER_CUDA:
  136. if (cuda_request(&req, NULL, 6, CUDA_PACKET, CUDA_SET_TIME,
  137. nowtime >> 24, nowtime >> 16, nowtime >> 8,
  138. nowtime) < 0)
  139. return 0;
  140. while (!req.complete)
  141. cuda_poll();
  142. if ((req.reply_len != 3) && (req.reply_len != 7))
  143. printk(KERN_ERR "pmac_set_rtc_time: got %d byte reply\n",
  144. req.reply_len);
  145. return 1;
  146. #endif /* CONFIG_ADB_CUDA */
  147. #ifdef CONFIG_ADB_PMU
  148. case SYS_CTRLER_PMU:
  149. if (pmu_request(&req, NULL, 5, PMU_SET_RTC,
  150. nowtime >> 24, nowtime >> 16, nowtime >> 8, nowtime) < 0)
  151. return 0;
  152. while (!req.complete)
  153. pmu_poll();
  154. if (req.reply_len != 0)
  155. printk(KERN_ERR "pmac_set_rtc_time: got %d byte reply\n",
  156. req.reply_len);
  157. return 1;
  158. #endif /* CONFIG_ADB_PMU */
  159. default:
  160. return 0;
  161. }
  162. }
  163. /*
  164. * Calibrate the decrementer register using VIA timer 1.
  165. * This is used both on powermacs and CHRP machines.
  166. */
  167. int __init
  168. via_calibrate_decr(void)
  169. {
  170. struct device_node *vias;
  171. volatile unsigned char __iomem *via;
  172. int count = VIA_TIMER_FREQ_6 / 100;
  173. unsigned int dstart, dend;
  174. vias = find_devices("via-cuda");
  175. if (vias == 0)
  176. vias = find_devices("via-pmu");
  177. if (vias == 0)
  178. vias = find_devices("via");
  179. if (vias == 0 || vias->n_addrs == 0)
  180. return 0;
  181. via = ioremap(vias->addrs[0].address, vias->addrs[0].size);
  182. /* set timer 1 for continuous interrupts */
  183. out_8(&via[ACR], (via[ACR] & ~T1MODE) | T1MODE_CONT);
  184. /* set the counter to a small value */
  185. out_8(&via[T1CH], 2);
  186. /* set the latch to `count' */
  187. out_8(&via[T1LL], count);
  188. out_8(&via[T1LH], count >> 8);
  189. /* wait until it hits 0 */
  190. while ((in_8(&via[IFR]) & T1_INT) == 0)
  191. ;
  192. dstart = get_dec();
  193. /* clear the interrupt & wait until it hits 0 again */
  194. in_8(&via[T1CL]);
  195. while ((in_8(&via[IFR]) & T1_INT) == 0)
  196. ;
  197. dend = get_dec();
  198. ppc_tb_freq = (dstart - dend) * 100 / 6;
  199. tb_ticks_per_jiffy = (dstart - dend) / ((6 * HZ)/100);
  200. printk(KERN_INFO "via_calibrate_decr: ticks per jiffy = %lu (%u ticks)\n",
  201. tb_ticks_per_jiffy, dstart - dend);
  202. iounmap(via);
  203. return 1;
  204. }
  205. #ifdef CONFIG_PM
  206. /*
  207. * Reset the time after a sleep.
  208. */
  209. static int
  210. time_sleep_notify(struct pmu_sleep_notifier *self, int when)
  211. {
  212. static unsigned long time_diff;
  213. unsigned long flags;
  214. unsigned long seq;
  215. struct timespec tv;
  216. switch (when) {
  217. case PBOOK_SLEEP_NOW:
  218. do {
  219. seq = read_seqbegin_irqsave(&xtime_lock, flags);
  220. time_diff = xtime.tv_sec - pmac_get_boot_time();
  221. } while (read_seqretry_irqrestore(&xtime_lock, seq, flags));
  222. break;
  223. case PBOOK_WAKE:
  224. tv.tv_sec = pmac_get_boot_time() + time_diff;
  225. tv.tv_nsec = 0;
  226. do_settimeofday(&tv);
  227. break;
  228. }
  229. return PBOOK_SLEEP_OK;
  230. }
  231. static struct pmu_sleep_notifier time_sleep_notifier = {
  232. time_sleep_notify, SLEEP_LEVEL_MISC,
  233. };
  234. #endif /* CONFIG_PM */
  235. /*
  236. * Query the OF and get the decr frequency.
  237. * This was taken from the pmac time_init() when merging the prep/pmac
  238. * time functions.
  239. */
  240. void __init
  241. pmac_calibrate_decr(void)
  242. {
  243. struct device_node *cpu;
  244. unsigned int freq, *fp;
  245. #ifdef CONFIG_PM
  246. pmu_register_sleep_notifier(&time_sleep_notifier);
  247. #endif /* CONFIG_PM */
  248. /* We assume MacRISC2 machines have correct device-tree
  249. * calibration. That's better since the VIA itself seems
  250. * to be slightly off. --BenH
  251. */
  252. if (!machine_is_compatible("MacRISC2") &&
  253. !machine_is_compatible("MacRISC3") &&
  254. !machine_is_compatible("MacRISC4"))
  255. if (via_calibrate_decr())
  256. return;
  257. /* Special case: QuickSilver G4s seem to have a badly calibrated
  258. * timebase-frequency in OF, VIA is much better on these. We should
  259. * probably implement calibration based on the KL timer on these
  260. * machines anyway... -BenH
  261. */
  262. if (machine_is_compatible("PowerMac3,5"))
  263. if (via_calibrate_decr())
  264. return;
  265. /*
  266. * The cpu node should have a timebase-frequency property
  267. * to tell us the rate at which the decrementer counts.
  268. */
  269. cpu = find_type_devices("cpu");
  270. if (cpu == 0)
  271. panic("can't find cpu node in time_init");
  272. fp = (unsigned int *) get_property(cpu, "timebase-frequency", NULL);
  273. if (fp == 0)
  274. panic("can't get cpu timebase frequency");
  275. freq = *fp;
  276. printk("time_init: decrementer frequency = %u.%.6u MHz\n",
  277. freq/1000000, freq%1000000);
  278. ppc_tb_freq = freq;
  279. }