time.c 8.7 KB

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