ip27-timer.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /*
  2. * Copytight (C) 1999, 2000, 05, 06 Ralf Baechle (ralf@linux-mips.org)
  3. * Copytight (C) 1999, 2000 Silicon Graphics, Inc.
  4. */
  5. #include <linux/bcd.h>
  6. #include <linux/init.h>
  7. #include <linux/kernel.h>
  8. #include <linux/sched.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/kernel_stat.h>
  11. #include <linux/param.h>
  12. #include <linux/time.h>
  13. #include <linux/timex.h>
  14. #include <linux/mm.h>
  15. #include <asm/time.h>
  16. #include <asm/pgtable.h>
  17. #include <asm/sgialib.h>
  18. #include <asm/sn/ioc3.h>
  19. #include <asm/m48t35.h>
  20. #include <asm/sn/klconfig.h>
  21. #include <asm/sn/arch.h>
  22. #include <asm/sn/addrs.h>
  23. #include <asm/sn/sn_private.h>
  24. #include <asm/sn/sn0/ip27.h>
  25. #include <asm/sn/sn0/hub.h>
  26. /*
  27. * This is a hack; we really need to figure these values out dynamically
  28. *
  29. * Since 800 ns works very well with various HUB frequencies, such as
  30. * 360, 380, 390 and 400 MHZ, we use 800 ns rtc cycle time.
  31. *
  32. * Ralf: which clock rate is used to feed the counter?
  33. */
  34. #define NSEC_PER_CYCLE 800
  35. #define CYCLES_PER_SEC (NSEC_PER_SEC/NSEC_PER_CYCLE)
  36. #define CYCLES_PER_JIFFY (CYCLES_PER_SEC/HZ)
  37. #define TICK_SIZE (tick_nsec / 1000)
  38. static unsigned long ct_cur[NR_CPUS]; /* What counter should be at next timer irq */
  39. static long last_rtc_update; /* Last time the rtc clock got updated */
  40. extern volatile unsigned long wall_jiffies;
  41. #if 0
  42. static int set_rtc_mmss(unsigned long nowtime)
  43. {
  44. int retval = 0;
  45. int real_seconds, real_minutes, cmos_minutes;
  46. struct m48t35_rtc *rtc;
  47. nasid_t nid;
  48. nid = get_nasid();
  49. rtc = (struct m48t35_rtc *)(KL_CONFIG_CH_CONS_INFO(nid)->memory_base +
  50. IOC3_BYTEBUS_DEV0);
  51. rtc->control |= M48T35_RTC_READ;
  52. cmos_minutes = BCD2BIN(rtc->min);
  53. rtc->control &= ~M48T35_RTC_READ;
  54. /*
  55. * Since we're only adjusting minutes and seconds, don't interfere with
  56. * hour overflow. This avoids messing with unknown time zones but
  57. * requires your RTC not to be off by more than 15 minutes
  58. */
  59. real_seconds = nowtime % 60;
  60. real_minutes = nowtime / 60;
  61. if (((abs(real_minutes - cmos_minutes) + 15)/30) & 1)
  62. real_minutes += 30; /* correct for half hour time zone */
  63. real_minutes %= 60;
  64. if (abs(real_minutes - cmos_minutes) < 30) {
  65. real_seconds = BIN2BCD(real_seconds);
  66. real_minutes = BIN2BCD(real_minutes);
  67. rtc->control |= M48T35_RTC_SET;
  68. rtc->sec = real_seconds;
  69. rtc->min = real_minutes;
  70. rtc->control &= ~M48T35_RTC_SET;
  71. } else {
  72. printk(KERN_WARNING
  73. "set_rtc_mmss: can't update from %d to %d\n",
  74. cmos_minutes, real_minutes);
  75. retval = -1;
  76. }
  77. return retval;
  78. }
  79. #endif
  80. static unsigned int rt_timer_irq;
  81. void ip27_rt_timer_interrupt(struct pt_regs *regs)
  82. {
  83. int cpu = smp_processor_id();
  84. int cpuA = cputoslice(cpu) == 0;
  85. unsigned int irq = rt_timer_irq;
  86. irq_enter();
  87. write_seqlock(&xtime_lock);
  88. again:
  89. LOCAL_HUB_S(cpuA ? PI_RT_PEND_A : PI_RT_PEND_B, 0); /* Ack */
  90. ct_cur[cpu] += CYCLES_PER_JIFFY;
  91. LOCAL_HUB_S(cpuA ? PI_RT_COMPARE_A : PI_RT_COMPARE_B, ct_cur[cpu]);
  92. if (LOCAL_HUB_L(PI_RT_COUNT) >= ct_cur[cpu])
  93. goto again;
  94. kstat_this_cpu.irqs[irq]++; /* kstat only for bootcpu? */
  95. if (cpu == 0)
  96. do_timer(regs);
  97. update_process_times(user_mode(regs));
  98. /*
  99. * If we have an externally synchronized Linux clock, then update
  100. * RTC clock accordingly every ~11 minutes. Set_rtc_mmss() has to be
  101. * called as close as possible to when a second starts.
  102. */
  103. if (ntp_synced() &&
  104. xtime.tv_sec > last_rtc_update + 660 &&
  105. (xtime.tv_nsec / 1000) >= 500000 - ((unsigned) TICK_SIZE) / 2 &&
  106. (xtime.tv_nsec / 1000) <= 500000 + ((unsigned) TICK_SIZE) / 2) {
  107. if (rtc_mips_set_time(xtime.tv_sec) == 0) {
  108. last_rtc_update = xtime.tv_sec;
  109. } else {
  110. last_rtc_update = xtime.tv_sec - 600;
  111. /* do it again in 60 s */
  112. }
  113. }
  114. write_sequnlock(&xtime_lock);
  115. irq_exit();
  116. }
  117. unsigned long ip27_do_gettimeoffset(void)
  118. {
  119. unsigned long ct_cur1;
  120. ct_cur1 = REMOTE_HUB_L(cputonasid(0), PI_RT_COUNT) + CYCLES_PER_JIFFY;
  121. return (ct_cur1 - ct_cur[0]) * NSEC_PER_CYCLE / 1000;
  122. }
  123. /* Includes for ioc3_init(). */
  124. #include <asm/sn/types.h>
  125. #include <asm/sn/sn0/addrs.h>
  126. #include <asm/sn/sn0/hubni.h>
  127. #include <asm/sn/sn0/hubio.h>
  128. #include <asm/pci/bridge.h>
  129. static __init unsigned long get_m48t35_time(void)
  130. {
  131. unsigned int year, month, date, hour, min, sec;
  132. struct m48t35_rtc *rtc;
  133. nasid_t nid;
  134. nid = get_nasid();
  135. rtc = (struct m48t35_rtc *)(KL_CONFIG_CH_CONS_INFO(nid)->memory_base +
  136. IOC3_BYTEBUS_DEV0);
  137. rtc->control |= M48T35_RTC_READ;
  138. sec = rtc->sec;
  139. min = rtc->min;
  140. hour = rtc->hour;
  141. date = rtc->date;
  142. month = rtc->month;
  143. year = rtc->year;
  144. rtc->control &= ~M48T35_RTC_READ;
  145. sec = BCD2BIN(sec);
  146. min = BCD2BIN(min);
  147. hour = BCD2BIN(hour);
  148. date = BCD2BIN(date);
  149. month = BCD2BIN(month);
  150. year = BCD2BIN(year);
  151. year += 1970;
  152. return mktime(year, month, date, hour, min, sec);
  153. }
  154. static unsigned int startup_rt_irq(unsigned int irq)
  155. {
  156. return 0;
  157. }
  158. static void shutdown_rt_irq(unsigned int irq)
  159. {
  160. }
  161. static void enable_rt_irq(unsigned int irq)
  162. {
  163. }
  164. static void disable_rt_irq(unsigned int irq)
  165. {
  166. }
  167. static void mask_and_ack_rt(unsigned int irq)
  168. {
  169. }
  170. static void end_rt_irq(unsigned int irq)
  171. {
  172. }
  173. static struct irq_chip rt_irq_type = {
  174. .typename = "SN HUB RT timer",
  175. .startup = startup_rt_irq,
  176. .shutdown = shutdown_rt_irq,
  177. .enable = enable_rt_irq,
  178. .disable = disable_rt_irq,
  179. .ack = mask_and_ack_rt,
  180. .end = end_rt_irq,
  181. };
  182. static struct irqaction rt_irqaction = {
  183. .handler = ip27_rt_timer_interrupt,
  184. .flags = IRQF_DISABLED,
  185. .mask = CPU_MASK_NONE,
  186. .name = "timer"
  187. };
  188. extern int allocate_irqno(void);
  189. void __init plat_timer_setup(struct irqaction *irq)
  190. {
  191. int irqno = allocate_irqno();
  192. if (irqno < 0)
  193. panic("Can't allocate interrupt number for timer interrupt");
  194. irq_desc[irqno].status = IRQ_DISABLED;
  195. irq_desc[irqno].action = NULL;
  196. irq_desc[irqno].depth = 1;
  197. irq_desc[irqno].chip = &rt_irq_type;
  198. /* over-write the handler, we use our own way */
  199. irq->handler = no_action;
  200. /* setup irqaction */
  201. irq_desc[irqno].status |= IRQ_PER_CPU;
  202. rt_timer_irq = irqno;
  203. /*
  204. * Only needed to get /proc/interrupt to display timer irq stats
  205. */
  206. setup_irq(irqno, &rt_irqaction);
  207. }
  208. void __init ip27_time_init(void)
  209. {
  210. xtime.tv_sec = get_m48t35_time();
  211. xtime.tv_nsec = 0;
  212. do_gettimeoffset = ip27_do_gettimeoffset;
  213. }
  214. void __init cpu_time_init(void)
  215. {
  216. lboard_t *board;
  217. klcpu_t *cpu;
  218. int cpuid;
  219. /* Don't use ARCS. ARCS is fragile. Klconfig is simple and sane. */
  220. board = find_lboard(KL_CONFIG_INFO(get_nasid()), KLTYPE_IP27);
  221. if (!board)
  222. panic("Can't find board info for myself.");
  223. cpuid = LOCAL_HUB_L(PI_CPU_NUM) ? IP27_CPU0_INDEX : IP27_CPU1_INDEX;
  224. cpu = (klcpu_t *) KLCF_COMP(board, cpuid);
  225. if (!cpu)
  226. panic("No information about myself?");
  227. printk("CPU %d clock is %dMHz.\n", smp_processor_id(), cpu->cpu_speed);
  228. set_c0_status(SRB_TIMOCLK);
  229. }
  230. void __init hub_rtc_init(cnodeid_t cnode)
  231. {
  232. /*
  233. * We only need to initialize the current node.
  234. * If this is not the current node then it is a cpuless
  235. * node and timeouts will not happen there.
  236. */
  237. if (get_compact_nodeid() == cnode) {
  238. int cpu = smp_processor_id();
  239. LOCAL_HUB_S(PI_RT_EN_A, 1);
  240. LOCAL_HUB_S(PI_RT_EN_B, 1);
  241. LOCAL_HUB_S(PI_PROF_EN_A, 0);
  242. LOCAL_HUB_S(PI_PROF_EN_B, 0);
  243. ct_cur[cpu] = CYCLES_PER_JIFFY;
  244. LOCAL_HUB_S(PI_RT_COMPARE_A, ct_cur[cpu]);
  245. LOCAL_HUB_S(PI_RT_COUNT, 0);
  246. LOCAL_HUB_S(PI_RT_PEND_A, 0);
  247. LOCAL_HUB_S(PI_RT_COMPARE_B, ct_cur[cpu]);
  248. LOCAL_HUB_S(PI_RT_COUNT, 0);
  249. LOCAL_HUB_S(PI_RT_PEND_B, 0);
  250. }
  251. }