ip27-timer.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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 void enable_rt_irq(unsigned int irq)
  99. {
  100. }
  101. static void disable_rt_irq(unsigned int irq)
  102. {
  103. }
  104. static struct irq_chip rt_irq_type = {
  105. .name = "SN HUB RT timer",
  106. .ack = disable_rt_irq,
  107. .mask = disable_rt_irq,
  108. .mask_ack = disable_rt_irq,
  109. .unmask = enable_rt_irq,
  110. .eoi = enable_rt_irq,
  111. };
  112. static int rt_next_event(unsigned long delta, struct clock_event_device *evt)
  113. {
  114. unsigned int cpu = smp_processor_id();
  115. int slice = cputoslice(cpu);
  116. unsigned long cnt;
  117. cnt = LOCAL_HUB_L(PI_RT_COUNT);
  118. cnt += delta;
  119. LOCAL_HUB_S(PI_RT_COMPARE_A + PI_COUNT_OFFSET * slice, cnt);
  120. return LOCAL_HUB_L(PI_RT_COUNT) >= cnt ? -ETIME : 0;
  121. }
  122. static void rt_set_mode(enum clock_event_mode mode,
  123. struct clock_event_device *evt)
  124. {
  125. switch (mode) {
  126. case CLOCK_EVT_MODE_ONESHOT:
  127. /* The only mode supported */
  128. break;
  129. case CLOCK_EVT_MODE_PERIODIC:
  130. case CLOCK_EVT_MODE_UNUSED:
  131. case CLOCK_EVT_MODE_SHUTDOWN:
  132. case CLOCK_EVT_MODE_RESUME:
  133. /* Nothing to do */
  134. break;
  135. }
  136. }
  137. int rt_timer_irq;
  138. static DEFINE_PER_CPU(struct clock_event_device, hub_rt_clockevent);
  139. static DEFINE_PER_CPU(char [11], hub_rt_name);
  140. static irqreturn_t hub_rt_counter_handler(int irq, void *dev_id)
  141. {
  142. unsigned int cpu = smp_processor_id();
  143. struct clock_event_device *cd = &per_cpu(hub_rt_clockevent, cpu);
  144. int slice = cputoslice(cpu);
  145. /*
  146. * Ack
  147. */
  148. LOCAL_HUB_S(PI_RT_PEND_A + PI_COUNT_OFFSET * slice, 0);
  149. cd->event_handler(cd);
  150. return IRQ_HANDLED;
  151. }
  152. struct irqaction hub_rt_irqaction = {
  153. .handler = hub_rt_counter_handler,
  154. .flags = IRQF_DISABLED | IRQF_PERCPU,
  155. .name = "hub-rt",
  156. };
  157. /*
  158. * This is a hack; we really need to figure these values out dynamically
  159. *
  160. * Since 800 ns works very well with various HUB frequencies, such as
  161. * 360, 380, 390 and 400 MHZ, we use 800 ns rtc cycle time.
  162. *
  163. * Ralf: which clock rate is used to feed the counter?
  164. */
  165. #define NSEC_PER_CYCLE 800
  166. #define CYCLES_PER_SEC (NSEC_PER_SEC / NSEC_PER_CYCLE)
  167. void __cpuinit hub_rt_clock_event_init(void)
  168. {
  169. unsigned int cpu = smp_processor_id();
  170. struct clock_event_device *cd = &per_cpu(hub_rt_clockevent, cpu);
  171. unsigned char *name = per_cpu(hub_rt_name, cpu);
  172. int irq = rt_timer_irq;
  173. sprintf(name, "hub-rt %d", cpu);
  174. cd->name = name;
  175. cd->features = CLOCK_EVT_FEAT_ONESHOT;
  176. clockevent_set_clock(cd, CYCLES_PER_SEC);
  177. cd->max_delta_ns = clockevent_delta2ns(0xfffffffffffff, cd);
  178. cd->min_delta_ns = clockevent_delta2ns(0x300, cd);
  179. cd->rating = 200;
  180. cd->irq = irq;
  181. cd->cpumask = cpumask_of_cpu(cpu);
  182. cd->set_next_event = rt_next_event;
  183. cd->set_mode = rt_set_mode;
  184. clockevents_register_device(cd);
  185. }
  186. static void __init hub_rt_clock_event_global_init(void)
  187. {
  188. int irq;
  189. do {
  190. smp_wmb();
  191. irq = rt_timer_irq;
  192. if (irq)
  193. break;
  194. irq = allocate_irqno();
  195. if (irq < 0)
  196. panic("Allocation of irq number for timer failed");
  197. } while (xchg(&rt_timer_irq, irq));
  198. set_irq_chip_and_handler(irq, &rt_irq_type, handle_percpu_irq);
  199. setup_irq(irq, &hub_rt_irqaction);
  200. }
  201. static cycle_t hub_rt_read(void)
  202. {
  203. return REMOTE_HUB_L(cputonasid(0), PI_RT_COUNT);
  204. }
  205. struct clocksource hub_rt_clocksource = {
  206. .name = "HUB-RT",
  207. .rating = 200,
  208. .read = hub_rt_read,
  209. .mask = CLOCKSOURCE_MASK(52),
  210. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  211. };
  212. static void __init hub_rt_clocksource_init(void)
  213. {
  214. struct clocksource *cs = &hub_rt_clocksource;
  215. clocksource_set_clock(cs, CYCLES_PER_SEC);
  216. clocksource_register(cs);
  217. }
  218. void __init plat_time_init(void)
  219. {
  220. hub_rt_clocksource_init();
  221. hub_rt_clock_event_global_init();
  222. hub_rt_clock_event_init();
  223. }
  224. void __cpuinit cpu_time_init(void)
  225. {
  226. lboard_t *board;
  227. klcpu_t *cpu;
  228. int cpuid;
  229. /* Don't use ARCS. ARCS is fragile. Klconfig is simple and sane. */
  230. board = find_lboard(KL_CONFIG_INFO(get_nasid()), KLTYPE_IP27);
  231. if (!board)
  232. panic("Can't find board info for myself.");
  233. cpuid = LOCAL_HUB_L(PI_CPU_NUM) ? IP27_CPU0_INDEX : IP27_CPU1_INDEX;
  234. cpu = (klcpu_t *) KLCF_COMP(board, cpuid);
  235. if (!cpu)
  236. panic("No information about myself?");
  237. printk("CPU %d clock is %dMHz.\n", smp_processor_id(), cpu->cpu_speed);
  238. set_c0_status(SRB_TIMOCLK);
  239. }
  240. void __cpuinit hub_rtc_init(cnodeid_t cnode)
  241. {
  242. /*
  243. * We only need to initialize the current node.
  244. * If this is not the current node then it is a cpuless
  245. * node and timeouts will not happen there.
  246. */
  247. if (get_compact_nodeid() == cnode) {
  248. LOCAL_HUB_S(PI_RT_EN_A, 1);
  249. LOCAL_HUB_S(PI_RT_EN_B, 1);
  250. LOCAL_HUB_S(PI_PROF_EN_A, 0);
  251. LOCAL_HUB_S(PI_PROF_EN_B, 0);
  252. LOCAL_HUB_S(PI_RT_COUNT, 0);
  253. LOCAL_HUB_S(PI_RT_PEND_A, 0);
  254. LOCAL_HUB_S(PI_RT_PEND_B, 0);
  255. }
  256. }