ip27-timer.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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/smp.h>
  14. #include <linux/time.h>
  15. #include <linux/timex.h>
  16. #include <linux/mm.h>
  17. #include <linux/platform_device.h>
  18. #include <asm/time.h>
  19. #include <asm/pgtable.h>
  20. #include <asm/sgialib.h>
  21. #include <asm/sn/ioc3.h>
  22. #include <asm/sn/klconfig.h>
  23. #include <asm/sn/arch.h>
  24. #include <asm/sn/addrs.h>
  25. #include <asm/sn/sn_private.h>
  26. #include <asm/sn/sn0/ip27.h>
  27. #include <asm/sn/sn0/hub.h>
  28. #define TICK_SIZE (tick_nsec / 1000)
  29. /* Includes for ioc3_init(). */
  30. #include <asm/sn/types.h>
  31. #include <asm/sn/sn0/addrs.h>
  32. #include <asm/sn/sn0/hubni.h>
  33. #include <asm/sn/sn0/hubio.h>
  34. #include <asm/pci/bridge.h>
  35. static void enable_rt_irq(unsigned int irq)
  36. {
  37. }
  38. static void disable_rt_irq(unsigned int irq)
  39. {
  40. }
  41. static struct irq_chip rt_irq_type = {
  42. .name = "SN HUB RT timer",
  43. .ack = disable_rt_irq,
  44. .mask = disable_rt_irq,
  45. .mask_ack = disable_rt_irq,
  46. .unmask = enable_rt_irq,
  47. .eoi = enable_rt_irq,
  48. };
  49. static int rt_next_event(unsigned long delta, struct clock_event_device *evt)
  50. {
  51. unsigned int cpu = smp_processor_id();
  52. int slice = cputoslice(cpu);
  53. unsigned long cnt;
  54. cnt = LOCAL_HUB_L(PI_RT_COUNT);
  55. cnt += delta;
  56. LOCAL_HUB_S(PI_RT_COMPARE_A + PI_COUNT_OFFSET * slice, cnt);
  57. return LOCAL_HUB_L(PI_RT_COUNT) >= cnt ? -ETIME : 0;
  58. }
  59. static void rt_set_mode(enum clock_event_mode mode,
  60. struct clock_event_device *evt)
  61. {
  62. switch (mode) {
  63. case CLOCK_EVT_MODE_ONESHOT:
  64. /* The only mode supported */
  65. break;
  66. case CLOCK_EVT_MODE_PERIODIC:
  67. case CLOCK_EVT_MODE_UNUSED:
  68. case CLOCK_EVT_MODE_SHUTDOWN:
  69. case CLOCK_EVT_MODE_RESUME:
  70. /* Nothing to do */
  71. break;
  72. }
  73. }
  74. int rt_timer_irq;
  75. static DEFINE_PER_CPU(struct clock_event_device, hub_rt_clockevent);
  76. static DEFINE_PER_CPU(char [11], hub_rt_name);
  77. static irqreturn_t hub_rt_counter_handler(int irq, void *dev_id)
  78. {
  79. unsigned int cpu = smp_processor_id();
  80. struct clock_event_device *cd = &per_cpu(hub_rt_clockevent, cpu);
  81. int slice = cputoslice(cpu);
  82. /*
  83. * Ack
  84. */
  85. LOCAL_HUB_S(PI_RT_PEND_A + PI_COUNT_OFFSET * slice, 0);
  86. cd->event_handler(cd);
  87. return IRQ_HANDLED;
  88. }
  89. struct irqaction hub_rt_irqaction = {
  90. .handler = hub_rt_counter_handler,
  91. .flags = IRQF_DISABLED | IRQF_PERCPU | IRQF_TIMER,
  92. .name = "hub-rt",
  93. };
  94. /*
  95. * This is a hack; we really need to figure these values out dynamically
  96. *
  97. * Since 800 ns works very well with various HUB frequencies, such as
  98. * 360, 380, 390 and 400 MHZ, we use 800 ns rtc cycle time.
  99. *
  100. * Ralf: which clock rate is used to feed the counter?
  101. */
  102. #define NSEC_PER_CYCLE 800
  103. #define CYCLES_PER_SEC (NSEC_PER_SEC / NSEC_PER_CYCLE)
  104. void __cpuinit hub_rt_clock_event_init(void)
  105. {
  106. unsigned int cpu = smp_processor_id();
  107. struct clock_event_device *cd = &per_cpu(hub_rt_clockevent, cpu);
  108. unsigned char *name = per_cpu(hub_rt_name, cpu);
  109. int irq = rt_timer_irq;
  110. sprintf(name, "hub-rt %d", cpu);
  111. cd->name = name;
  112. cd->features = CLOCK_EVT_FEAT_ONESHOT;
  113. clockevent_set_clock(cd, CYCLES_PER_SEC);
  114. cd->max_delta_ns = clockevent_delta2ns(0xfffffffffffff, cd);
  115. cd->min_delta_ns = clockevent_delta2ns(0x300, cd);
  116. cd->rating = 200;
  117. cd->irq = irq;
  118. cd->cpumask = cpumask_of(cpu);
  119. cd->set_next_event = rt_next_event;
  120. cd->set_mode = rt_set_mode;
  121. clockevents_register_device(cd);
  122. }
  123. static void __init hub_rt_clock_event_global_init(void)
  124. {
  125. int irq;
  126. do {
  127. smp_wmb();
  128. irq = rt_timer_irq;
  129. if (irq)
  130. break;
  131. irq = allocate_irqno();
  132. if (irq < 0)
  133. panic("Allocation of irq number for timer failed");
  134. } while (xchg(&rt_timer_irq, irq));
  135. set_irq_chip_and_handler(irq, &rt_irq_type, handle_percpu_irq);
  136. setup_irq(irq, &hub_rt_irqaction);
  137. }
  138. static cycle_t hub_rt_read(struct clocksource *cs)
  139. {
  140. return REMOTE_HUB_L(cputonasid(0), PI_RT_COUNT);
  141. }
  142. struct clocksource hub_rt_clocksource = {
  143. .name = "HUB-RT",
  144. .rating = 200,
  145. .read = hub_rt_read,
  146. .mask = CLOCKSOURCE_MASK(52),
  147. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  148. };
  149. static void __init hub_rt_clocksource_init(void)
  150. {
  151. struct clocksource *cs = &hub_rt_clocksource;
  152. clocksource_set_clock(cs, CYCLES_PER_SEC);
  153. clocksource_register(cs);
  154. }
  155. void __init plat_time_init(void)
  156. {
  157. hub_rt_clocksource_init();
  158. hub_rt_clock_event_global_init();
  159. hub_rt_clock_event_init();
  160. }
  161. void __cpuinit cpu_time_init(void)
  162. {
  163. lboard_t *board;
  164. klcpu_t *cpu;
  165. int cpuid;
  166. /* Don't use ARCS. ARCS is fragile. Klconfig is simple and sane. */
  167. board = find_lboard(KL_CONFIG_INFO(get_nasid()), KLTYPE_IP27);
  168. if (!board)
  169. panic("Can't find board info for myself.");
  170. cpuid = LOCAL_HUB_L(PI_CPU_NUM) ? IP27_CPU0_INDEX : IP27_CPU1_INDEX;
  171. cpu = (klcpu_t *) KLCF_COMP(board, cpuid);
  172. if (!cpu)
  173. panic("No information about myself?");
  174. printk("CPU %d clock is %dMHz.\n", smp_processor_id(), cpu->cpu_speed);
  175. set_c0_status(SRB_TIMOCLK);
  176. }
  177. void __cpuinit hub_rtc_init(cnodeid_t cnode)
  178. {
  179. /*
  180. * We only need to initialize the current node.
  181. * If this is not the current node then it is a cpuless
  182. * node and timeouts will not happen there.
  183. */
  184. if (get_compact_nodeid() == cnode) {
  185. LOCAL_HUB_S(PI_RT_EN_A, 1);
  186. LOCAL_HUB_S(PI_RT_EN_B, 1);
  187. LOCAL_HUB_S(PI_PROF_EN_A, 0);
  188. LOCAL_HUB_S(PI_PROF_EN_B, 0);
  189. LOCAL_HUB_S(PI_RT_COUNT, 0);
  190. LOCAL_HUB_S(PI_RT_PEND_A, 0);
  191. LOCAL_HUB_S(PI_RT_PEND_B, 0);
  192. }
  193. }
  194. static int __init sgi_ip27_rtc_devinit(void)
  195. {
  196. struct resource res;
  197. memset(&res, 0, sizeof(res));
  198. res.start = XPHYSADDR(KL_CONFIG_CH_CONS_INFO(master_nasid)->memory_base +
  199. IOC3_BYTEBUS_DEV0);
  200. res.end = res.start + 32767;
  201. res.flags = IORESOURCE_MEM;
  202. return IS_ERR(platform_device_register_simple("rtc-m48t35", -1,
  203. &res, 1));
  204. }
  205. /*
  206. * kludge make this a device_initcall after ioc3 resource conflicts
  207. * are resolved
  208. */
  209. late_initcall(sgi_ip27_rtc_devinit);