ip27-timer.c 5.8 KB

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