time-ns9360.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * arch/arm/mach-ns9xxx/time-ns9360.c
  3. *
  4. * Copyright (C) 2006,2007 by Digi International Inc.
  5. * All rights reserved.
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License version 2 as published by
  9. * the Free Software Foundation.
  10. */
  11. #include <linux/jiffies.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/irq.h>
  14. #include <linux/stringify.h>
  15. #include <linux/clocksource.h>
  16. #include <linux/clockchips.h>
  17. #include <mach/processor-ns9360.h>
  18. #include <mach/regs-sys-ns9360.h>
  19. #include <mach/irqs.h>
  20. #include <mach/system.h>
  21. #include "generic.h"
  22. #define TIMER_CLOCKSOURCE 0
  23. #define TIMER_CLOCKEVENT 1
  24. static u32 latch;
  25. static cycle_t ns9360_clocksource_read(struct clocksource *cs)
  26. {
  27. return __raw_readl(SYS_TR(TIMER_CLOCKSOURCE));
  28. }
  29. static struct clocksource ns9360_clocksource = {
  30. .name = "ns9360-timer" __stringify(TIMER_CLOCKSOURCE),
  31. .rating = 300,
  32. .read = ns9360_clocksource_read,
  33. .mask = CLOCKSOURCE_MASK(32),
  34. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  35. };
  36. static void ns9360_clockevent_setmode(enum clock_event_mode mode,
  37. struct clock_event_device *clk)
  38. {
  39. u32 tc = __raw_readl(SYS_TC(TIMER_CLOCKEVENT));
  40. switch (mode) {
  41. case CLOCK_EVT_MODE_PERIODIC:
  42. __raw_writel(latch, SYS_TRC(TIMER_CLOCKEVENT));
  43. REGSET(tc, SYS_TCx, REN, EN);
  44. REGSET(tc, SYS_TCx, INTS, EN);
  45. REGSET(tc, SYS_TCx, TEN, EN);
  46. break;
  47. case CLOCK_EVT_MODE_ONESHOT:
  48. REGSET(tc, SYS_TCx, REN, DIS);
  49. REGSET(tc, SYS_TCx, INTS, EN);
  50. /* fall through */
  51. case CLOCK_EVT_MODE_UNUSED:
  52. case CLOCK_EVT_MODE_SHUTDOWN:
  53. case CLOCK_EVT_MODE_RESUME:
  54. default:
  55. REGSET(tc, SYS_TCx, TEN, DIS);
  56. break;
  57. }
  58. __raw_writel(tc, SYS_TC(TIMER_CLOCKEVENT));
  59. }
  60. static int ns9360_clockevent_setnextevent(unsigned long evt,
  61. struct clock_event_device *clk)
  62. {
  63. u32 tc = __raw_readl(SYS_TC(TIMER_CLOCKEVENT));
  64. if (REGGET(tc, SYS_TCx, TEN)) {
  65. REGSET(tc, SYS_TCx, TEN, DIS);
  66. __raw_writel(tc, SYS_TC(TIMER_CLOCKEVENT));
  67. }
  68. REGSET(tc, SYS_TCx, TEN, EN);
  69. __raw_writel(evt, SYS_TRC(TIMER_CLOCKEVENT));
  70. __raw_writel(tc, SYS_TC(TIMER_CLOCKEVENT));
  71. return 0;
  72. }
  73. static struct clock_event_device ns9360_clockevent_device = {
  74. .name = "ns9360-timer" __stringify(TIMER_CLOCKEVENT),
  75. .shift = 20,
  76. .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
  77. .set_mode = ns9360_clockevent_setmode,
  78. .set_next_event = ns9360_clockevent_setnextevent,
  79. };
  80. static irqreturn_t ns9360_clockevent_handler(int irq, void *dev_id)
  81. {
  82. int timerno = irq - IRQ_NS9360_TIMER0;
  83. u32 tc;
  84. struct clock_event_device *evt = &ns9360_clockevent_device;
  85. /* clear irq */
  86. tc = __raw_readl(SYS_TC(timerno));
  87. if (REGGET(tc, SYS_TCx, REN) == SYS_TCx_REN_DIS) {
  88. REGSET(tc, SYS_TCx, TEN, DIS);
  89. __raw_writel(tc, SYS_TC(timerno));
  90. }
  91. REGSET(tc, SYS_TCx, INTC, SET);
  92. __raw_writel(tc, SYS_TC(timerno));
  93. REGSET(tc, SYS_TCx, INTC, UNSET);
  94. __raw_writel(tc, SYS_TC(timerno));
  95. evt->event_handler(evt);
  96. return IRQ_HANDLED;
  97. }
  98. static struct irqaction ns9360_clockevent_action = {
  99. .name = "ns9360-timer" __stringify(TIMER_CLOCKEVENT),
  100. .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
  101. .handler = ns9360_clockevent_handler,
  102. };
  103. static void __init ns9360_timer_init(void)
  104. {
  105. int tc;
  106. tc = __raw_readl(SYS_TC(TIMER_CLOCKSOURCE));
  107. if (REGGET(tc, SYS_TCx, TEN)) {
  108. REGSET(tc, SYS_TCx, TEN, DIS);
  109. __raw_writel(tc, SYS_TC(TIMER_CLOCKSOURCE));
  110. }
  111. __raw_writel(0, SYS_TRC(TIMER_CLOCKSOURCE));
  112. REGSET(tc, SYS_TCx, TEN, EN);
  113. REGSET(tc, SYS_TCx, TDBG, STOP);
  114. REGSET(tc, SYS_TCx, TLCS, CPU);
  115. REGSET(tc, SYS_TCx, TM, IEE);
  116. REGSET(tc, SYS_TCx, INTS, DIS);
  117. REGSET(tc, SYS_TCx, UDS, UP);
  118. REGSET(tc, SYS_TCx, TSZ, 32);
  119. REGSET(tc, SYS_TCx, REN, EN);
  120. __raw_writel(tc, SYS_TC(TIMER_CLOCKSOURCE));
  121. clocksource_register_hz(&ns9360_clocksource, ns9360_cpuclock());
  122. latch = SH_DIV(ns9360_cpuclock(), HZ, 0);
  123. tc = __raw_readl(SYS_TC(TIMER_CLOCKEVENT));
  124. REGSET(tc, SYS_TCx, TEN, DIS);
  125. REGSET(tc, SYS_TCx, TDBG, STOP);
  126. REGSET(tc, SYS_TCx, TLCS, CPU);
  127. REGSET(tc, SYS_TCx, TM, IEE);
  128. REGSET(tc, SYS_TCx, INTS, DIS);
  129. REGSET(tc, SYS_TCx, UDS, DOWN);
  130. REGSET(tc, SYS_TCx, TSZ, 32);
  131. REGSET(tc, SYS_TCx, REN, EN);
  132. __raw_writel(tc, SYS_TC(TIMER_CLOCKEVENT));
  133. ns9360_clockevent_device.mult = div_sc(ns9360_cpuclock(),
  134. NSEC_PER_SEC, ns9360_clockevent_device.shift);
  135. ns9360_clockevent_device.max_delta_ns =
  136. clockevent_delta2ns(-1, &ns9360_clockevent_device);
  137. ns9360_clockevent_device.min_delta_ns =
  138. clockevent_delta2ns(1, &ns9360_clockevent_device);
  139. ns9360_clockevent_device.cpumask = cpumask_of(0);
  140. clockevents_register_device(&ns9360_clockevent_device);
  141. setup_irq(IRQ_NS9360_TIMER0 + TIMER_CLOCKEVENT,
  142. &ns9360_clockevent_action);
  143. }
  144. struct sys_timer ns9360_timer = {
  145. .init = ns9360_timer_init,
  146. };