ip27-timer.c 5.7 KB

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