time.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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/errno.h>
  13. #include <linux/sched.h>
  14. #include <linux/kernel.h>
  15. #include <linux/param.h>
  16. #include <linux/string.h>
  17. #include <linux/mm.h>
  18. #include <linux/init.h>
  19. #include <linux/time.h>
  20. #include <linux/adb.h>
  21. #include <linux/cuda.h>
  22. #include <linux/pmu.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/hardirq.h>
  25. #include <linux/rtc.h>
  26. #include <asm/sections.h>
  27. #include <asm/prom.h>
  28. #include <asm/system.h>
  29. #include <asm/io.h>
  30. #include <asm/pgtable.h>
  31. #include <asm/machdep.h>
  32. #include <asm/time.h>
  33. #include <asm/nvram.h>
  34. #include <asm/smu.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. s32 delta = 0;
  63. #ifdef CONFIG_NVRAM
  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. #endif
  74. return delta;
  75. }
  76. #if defined(CONFIG_ADB_CUDA) || defined(CONFIG_ADB_PMU)
  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. #endif
  84. #if defined(CONFIG_ADB_CUDA) || defined(CONFIG_ADB_PMU) || \
  85. defined(CONFIG_PMAC_SMU)
  86. static unsigned long from_rtc_time(struct rtc_time *tm)
  87. {
  88. return mktime(tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday,
  89. tm->tm_hour, tm->tm_min, tm->tm_sec);
  90. }
  91. #endif
  92. #ifdef CONFIG_ADB_CUDA
  93. static unsigned long cuda_get_time(void)
  94. {
  95. struct adb_request req;
  96. unsigned int now;
  97. if (cuda_request(&req, NULL, 2, CUDA_PACKET, CUDA_GET_TIME) < 0)
  98. return 0;
  99. while (!req.complete)
  100. cuda_poll();
  101. if (req.reply_len != 7)
  102. printk(KERN_ERR "cuda_get_time: got %d byte reply\n",
  103. req.reply_len);
  104. now = (req.reply[3] << 24) + (req.reply[4] << 16)
  105. + (req.reply[5] << 8) + req.reply[6];
  106. return ((unsigned long)now) - RTC_OFFSET;
  107. }
  108. #define cuda_get_rtc_time(tm) to_rtc_time(cuda_get_time(), (tm))
  109. static int cuda_set_rtc_time(struct rtc_time *tm)
  110. {
  111. unsigned int nowtime;
  112. struct adb_request req;
  113. nowtime = from_rtc_time(tm) + RTC_OFFSET;
  114. if (cuda_request(&req, NULL, 6, CUDA_PACKET, CUDA_SET_TIME,
  115. nowtime >> 24, nowtime >> 16, nowtime >> 8,
  116. nowtime) < 0)
  117. return -ENXIO;
  118. while (!req.complete)
  119. cuda_poll();
  120. if ((req.reply_len != 3) && (req.reply_len != 7))
  121. printk(KERN_ERR "cuda_set_rtc_time: got %d byte reply\n",
  122. req.reply_len);
  123. return 0;
  124. }
  125. #else
  126. #define cuda_get_time() 0
  127. #define cuda_get_rtc_time(tm)
  128. #define cuda_set_rtc_time(tm) 0
  129. #endif
  130. #ifdef CONFIG_ADB_PMU
  131. static unsigned long pmu_get_time(void)
  132. {
  133. struct adb_request req;
  134. unsigned int now;
  135. if (pmu_request(&req, NULL, 1, PMU_READ_RTC) < 0)
  136. return 0;
  137. pmu_wait_complete(&req);
  138. if (req.reply_len != 4)
  139. printk(KERN_ERR "pmu_get_time: got %d byte reply from PMU\n",
  140. req.reply_len);
  141. now = (req.reply[0] << 24) + (req.reply[1] << 16)
  142. + (req.reply[2] << 8) + req.reply[3];
  143. return ((unsigned long)now) - RTC_OFFSET;
  144. }
  145. #define pmu_get_rtc_time(tm) to_rtc_time(pmu_get_time(), (tm))
  146. static int pmu_set_rtc_time(struct rtc_time *tm)
  147. {
  148. unsigned int nowtime;
  149. struct adb_request req;
  150. nowtime = from_rtc_time(tm) + RTC_OFFSET;
  151. if (pmu_request(&req, NULL, 5, PMU_SET_RTC, nowtime >> 24,
  152. nowtime >> 16, nowtime >> 8, nowtime) < 0)
  153. return -ENXIO;
  154. pmu_wait_complete(&req);
  155. if (req.reply_len != 0)
  156. printk(KERN_ERR "pmu_set_rtc_time: %d byte reply from PMU\n",
  157. req.reply_len);
  158. return 0;
  159. }
  160. #else
  161. #define pmu_get_time() 0
  162. #define pmu_get_rtc_time(tm)
  163. #define pmu_set_rtc_time(tm) 0
  164. #endif
  165. #ifdef CONFIG_PMAC_SMU
  166. static unsigned long smu_get_time(void)
  167. {
  168. struct rtc_time tm;
  169. if (smu_get_rtc_time(&tm, 1))
  170. return 0;
  171. return from_rtc_time(&tm);
  172. }
  173. #else
  174. #define smu_get_time() 0
  175. #define smu_get_rtc_time(tm, spin)
  176. #define smu_set_rtc_time(tm, spin) 0
  177. #endif
  178. /* Can't be __init, it's called when suspending and resuming */
  179. unsigned long pmac_get_boot_time(void)
  180. {
  181. /* Get the time from the RTC, used only at boot time */
  182. switch (sys_ctrler) {
  183. case SYS_CTRLER_CUDA:
  184. return cuda_get_time();
  185. case SYS_CTRLER_PMU:
  186. return pmu_get_time();
  187. case SYS_CTRLER_SMU:
  188. return smu_get_time();
  189. default:
  190. return 0;
  191. }
  192. }
  193. void pmac_get_rtc_time(struct rtc_time *tm)
  194. {
  195. /* Get the time from the RTC, used only at boot time */
  196. switch (sys_ctrler) {
  197. case SYS_CTRLER_CUDA:
  198. cuda_get_rtc_time(tm);
  199. break;
  200. case SYS_CTRLER_PMU:
  201. pmu_get_rtc_time(tm);
  202. break;
  203. case SYS_CTRLER_SMU:
  204. smu_get_rtc_time(tm, 1);
  205. break;
  206. default:
  207. ;
  208. }
  209. }
  210. int pmac_set_rtc_time(struct rtc_time *tm)
  211. {
  212. switch (sys_ctrler) {
  213. case SYS_CTRLER_CUDA:
  214. return cuda_set_rtc_time(tm);
  215. case SYS_CTRLER_PMU:
  216. return pmu_set_rtc_time(tm);
  217. case SYS_CTRLER_SMU:
  218. return smu_set_rtc_time(tm, 1);
  219. default:
  220. return -ENODEV;
  221. }
  222. }
  223. #ifdef CONFIG_PPC32
  224. /*
  225. * Calibrate the decrementer register using VIA timer 1.
  226. * This is used both on powermacs and CHRP machines.
  227. */
  228. int __init via_calibrate_decr(void)
  229. {
  230. struct device_node *vias;
  231. volatile unsigned char __iomem *via;
  232. int count = VIA_TIMER_FREQ_6 / 100;
  233. unsigned int dstart, dend;
  234. struct resource rsrc;
  235. vias = of_find_node_by_name(NULL, "via-cuda");
  236. if (vias == NULL)
  237. vias = of_find_node_by_name(NULL, "via-pmu");
  238. if (vias == NULL)
  239. vias = of_find_node_by_name(NULL, "via");
  240. if (vias == NULL || of_address_to_resource(vias, 0, &rsrc)) {
  241. of_node_put(vias);
  242. return 0;
  243. }
  244. of_node_put(vias);
  245. via = ioremap(rsrc.start, rsrc.end - rsrc.start + 1);
  246. if (via == NULL) {
  247. printk(KERN_ERR "Failed to map VIA for timer calibration !\n");
  248. return 0;
  249. }
  250. /* set timer 1 for continuous interrupts */
  251. out_8(&via[ACR], (via[ACR] & ~T1MODE) | T1MODE_CONT);
  252. /* set the counter to a small value */
  253. out_8(&via[T1CH], 2);
  254. /* set the latch to `count' */
  255. out_8(&via[T1LL], count);
  256. out_8(&via[T1LH], count >> 8);
  257. /* wait until it hits 0 */
  258. while ((in_8(&via[IFR]) & T1_INT) == 0)
  259. ;
  260. dstart = get_dec();
  261. /* clear the interrupt & wait until it hits 0 again */
  262. in_8(&via[T1CL]);
  263. while ((in_8(&via[IFR]) & T1_INT) == 0)
  264. ;
  265. dend = get_dec();
  266. ppc_tb_freq = (dstart - dend) * 100 / 6;
  267. iounmap(via);
  268. return 1;
  269. }
  270. #endif
  271. /*
  272. * Query the OF and get the decr frequency.
  273. */
  274. void __init pmac_calibrate_decr(void)
  275. {
  276. generic_calibrate_decr();
  277. #ifdef CONFIG_PPC32
  278. /* We assume MacRISC2 machines have correct device-tree
  279. * calibration. That's better since the VIA itself seems
  280. * to be slightly off. --BenH
  281. */
  282. if (!of_machine_is_compatible("MacRISC2") &&
  283. !of_machine_is_compatible("MacRISC3") &&
  284. !of_machine_is_compatible("MacRISC4"))
  285. if (via_calibrate_decr())
  286. return;
  287. /* Special case: QuickSilver G4s seem to have a badly calibrated
  288. * timebase-frequency in OF, VIA is much better on these. We should
  289. * probably implement calibration based on the KL timer on these
  290. * machines anyway... -BenH
  291. */
  292. if (of_machine_is_compatible("PowerMac3,5"))
  293. if (via_calibrate_decr())
  294. return;
  295. #endif
  296. }