ip27-timer.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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/clockchips.h>
  7. #include <linux/init.h>
  8. #include <linux/kernel.h>
  9. #include <linux/sched.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/kernel_stat.h>
  12. #include <linux/param.h>
  13. #include <linux/time.h>
  14. #include <linux/timex.h>
  15. #include <linux/mm.h>
  16. #include <asm/time.h>
  17. #include <asm/pgtable.h>
  18. #include <asm/sgialib.h>
  19. #include <asm/sn/ioc3.h>
  20. #include <asm/m48t35.h>
  21. #include <asm/sn/klconfig.h>
  22. #include <asm/sn/arch.h>
  23. #include <asm/sn/addrs.h>
  24. #include <asm/sn/sn_private.h>
  25. #include <asm/sn/sn0/ip27.h>
  26. #include <asm/sn/sn0/hub.h>
  27. #define TICK_SIZE (tick_nsec / 1000)
  28. #if 0
  29. static int set_rtc_mmss(unsigned long nowtime)
  30. {
  31. int retval = 0;
  32. int real_seconds, real_minutes, cmos_minutes;
  33. struct m48t35_rtc *rtc;
  34. nasid_t nid;
  35. nid = get_nasid();
  36. rtc = (struct m48t35_rtc *)(KL_CONFIG_CH_CONS_INFO(nid)->memory_base +
  37. IOC3_BYTEBUS_DEV0);
  38. rtc->control |= M48T35_RTC_READ;
  39. cmos_minutes = BCD2BIN(rtc->min);
  40. rtc->control &= ~M48T35_RTC_READ;
  41. /*
  42. * Since we're only adjusting minutes and seconds, don't interfere with
  43. * hour overflow. This avoids messing with unknown time zones but
  44. * requires your RTC not to be off by more than 15 minutes
  45. */
  46. real_seconds = nowtime % 60;
  47. real_minutes = nowtime / 60;
  48. if (((abs(real_minutes - cmos_minutes) + 15)/30) & 1)
  49. real_minutes += 30; /* correct for half hour time zone */
  50. real_minutes %= 60;
  51. if (abs(real_minutes - cmos_minutes) < 30) {
  52. real_seconds = BIN2BCD(real_seconds);
  53. real_minutes = BIN2BCD(real_minutes);
  54. rtc->control |= M48T35_RTC_SET;
  55. rtc->sec = real_seconds;
  56. rtc->min = real_minutes;
  57. rtc->control &= ~M48T35_RTC_SET;
  58. } else {
  59. printk(KERN_WARNING
  60. "set_rtc_mmss: can't update from %d to %d\n",
  61. cmos_minutes, real_minutes);
  62. retval = -1;
  63. }
  64. return retval;
  65. }
  66. #endif
  67. /* Includes for ioc3_init(). */
  68. #include <asm/sn/types.h>
  69. #include <asm/sn/sn0/addrs.h>
  70. #include <asm/sn/sn0/hubni.h>
  71. #include <asm/sn/sn0/hubio.h>
  72. #include <asm/pci/bridge.h>
  73. unsigned long read_persistent_clock(void)
  74. {
  75. unsigned int year, month, date, hour, min, sec;
  76. struct m48t35_rtc *rtc;
  77. nasid_t nid;
  78. nid = get_nasid();
  79. rtc = (struct m48t35_rtc *)(KL_CONFIG_CH_CONS_INFO(nid)->memory_base +
  80. IOC3_BYTEBUS_DEV0);
  81. rtc->control |= M48T35_RTC_READ;
  82. sec = rtc->sec;
  83. min = rtc->min;
  84. hour = rtc->hour;
  85. date = rtc->date;
  86. month = rtc->month;
  87. year = rtc->year;
  88. rtc->control &= ~M48T35_RTC_READ;
  89. sec = BCD2BIN(sec);
  90. min = BCD2BIN(min);
  91. hour = BCD2BIN(hour);
  92. date = BCD2BIN(date);
  93. month = BCD2BIN(month);
  94. year = BCD2BIN(year);
  95. year += 1970;
  96. return mktime(year, month, date, hour, min, sec);
  97. }
  98. static int rt_set_next_event(unsigned long delta,
  99. struct clock_event_device *evt)
  100. {
  101. unsigned int cpu = smp_processor_id();
  102. int slice = cputoslice(cpu) == 0;
  103. unsigned long cnt;
  104. cnt = LOCAL_HUB_L(PI_RT_COUNT);
  105. cnt += delta;
  106. LOCAL_HUB_S(slice ? PI_RT_COMPARE_A : PI_RT_COMPARE_B, cnt);
  107. return LOCAL_HUB_L(PI_RT_COUNT) >= cnt ? -ETIME : 0;
  108. }
  109. static void rt_set_mode(enum clock_event_mode mode,
  110. struct clock_event_device *evt)
  111. {
  112. switch (mode) {
  113. case CLOCK_EVT_MODE_PERIODIC:
  114. /* The only mode supported */
  115. break;
  116. case CLOCK_EVT_MODE_UNUSED:
  117. case CLOCK_EVT_MODE_SHUTDOWN:
  118. case CLOCK_EVT_MODE_ONESHOT:
  119. case CLOCK_EVT_MODE_RESUME:
  120. /* Nothing to do */
  121. break;
  122. }
  123. }
  124. struct clock_event_device rt_clock_event_device = {
  125. .name = "HUB-RT",
  126. .features = CLOCK_EVT_FEAT_ONESHOT,
  127. .rating = 300,
  128. .set_next_event = rt_set_next_event,
  129. .set_mode = rt_set_mode,
  130. };
  131. static void enable_rt_irq(unsigned int irq)
  132. {
  133. }
  134. static void disable_rt_irq(unsigned int irq)
  135. {
  136. }
  137. static struct irq_chip rt_irq_type = {
  138. .name = "SN HUB RT timer",
  139. .ack = disable_rt_irq,
  140. .mask = disable_rt_irq,
  141. .mask_ack = disable_rt_irq,
  142. .unmask = enable_rt_irq,
  143. .eoi = enable_rt_irq,
  144. };
  145. unsigned int rt_timer_irq;
  146. static irqreturn_t ip27_rt_timer_interrupt(int irq, void *dev_id)
  147. {
  148. struct clock_event_device *cd = &rt_clock_event_device;
  149. unsigned int cpu = smp_processor_id();
  150. int slice = cputoslice(cpu) == 0;
  151. LOCAL_HUB_S(slice ? PI_RT_PEND_A : PI_RT_PEND_B, 0); /* Ack */
  152. cd->event_handler(cd);
  153. return IRQ_HANDLED;
  154. }
  155. static struct irqaction rt_irqaction = {
  156. .handler = (irq_handler_t) ip27_rt_timer_interrupt,
  157. .flags = IRQF_DISABLED,
  158. .mask = CPU_MASK_NONE,
  159. .name = "timer"
  160. };
  161. /*
  162. * This is a hack; we really need to figure these values out dynamically
  163. *
  164. * Since 800 ns works very well with various HUB frequencies, such as
  165. * 360, 380, 390 and 400 MHZ, we use 800 ns rtc cycle time.
  166. *
  167. * Ralf: which clock rate is used to feed the counter?
  168. */
  169. #define NSEC_PER_CYCLE 800
  170. #define CYCLES_PER_SEC (NSEC_PER_SEC / NSEC_PER_CYCLE)
  171. static void __init ip27_rt_clock_event_init(void)
  172. {
  173. struct clock_event_device *cd = &rt_clock_event_device;
  174. unsigned int cpu = smp_processor_id();
  175. int irq = allocate_irqno();
  176. if (irq < 0)
  177. panic("Can't allocate interrupt number for timer interrupt");
  178. rt_timer_irq = irq;
  179. cd->irq = irq,
  180. cd->cpumask = cpumask_of_cpu(cpu),
  181. /*
  182. * Calculate the min / max delta
  183. */
  184. cd->mult =
  185. div_sc((unsigned long) CYCLES_PER_SEC, NSEC_PER_SEC, 32);
  186. cd->shift = 32;
  187. cd->max_delta_ns = clockevent_delta2ns(0x7fffffff, cd);
  188. cd->min_delta_ns = clockevent_delta2ns(0x300, cd);
  189. clockevents_register_device(cd);
  190. set_irq_chip_and_handler(irq, &rt_irq_type, handle_percpu_irq);
  191. setup_irq(irq, &rt_irqaction);
  192. }
  193. static cycle_t hub_rt_read(void)
  194. {
  195. return REMOTE_HUB_L(cputonasid(0), PI_RT_COUNT);
  196. }
  197. struct clocksource ht_rt_clocksource = {
  198. .name = "HUB-RT",
  199. .rating = 200,
  200. .read = hub_rt_read,
  201. .mask = CLOCKSOURCE_MASK(52),
  202. .shift = 32,
  203. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  204. };
  205. static void __init ip27_rt_clocksource_init(void)
  206. {
  207. clocksource_register(&ht_rt_clocksource);
  208. }
  209. void __init plat_time_init(void)
  210. {
  211. ip27_rt_clock_event_init();
  212. ip27_rt_clocksource_init();
  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. LOCAL_HUB_S(PI_RT_EN_A, 1);
  239. LOCAL_HUB_S(PI_RT_EN_B, 1);
  240. LOCAL_HUB_S(PI_PROF_EN_A, 0);
  241. LOCAL_HUB_S(PI_PROF_EN_B, 0);
  242. LOCAL_HUB_S(PI_RT_COUNT, 0);
  243. LOCAL_HUB_S(PI_RT_PEND_A, 0);
  244. LOCAL_HUB_S(PI_RT_PEND_B, 0);
  245. }
  246. }