ip27-timer.c 5.6 KB

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