time.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*
  2. * Copyright (C) 2013-2014 Altera Corporation
  3. * Copyright (C) 2010 Tobias Klauser <tklauser@distanz.ch>
  4. * Copyright (C) 2004 Microtronix Datacom Ltd.
  5. *
  6. * This file is subject to the terms and conditions of the GNU General Public
  7. * License. See the file "COPYING" in the main directory of this archive
  8. * for more details.
  9. */
  10. #include <linux/interrupt.h>
  11. #include <linux/clockchips.h>
  12. #include <linux/clocksource.h>
  13. #include <linux/delay.h>
  14. #include <linux/of.h>
  15. #include <linux/of_address.h>
  16. #include <linux/of_irq.h>
  17. #include <linux/io.h>
  18. #include <linux/slab.h>
  19. #define ALTERA_TIMER_STATUS_REG 0
  20. #define ALTERA_TIMER_CONTROL_REG 4
  21. #define ALTERA_TIMER_PERIODL_REG 8
  22. #define ALTERA_TIMER_PERIODH_REG 12
  23. #define ALTERA_TIMER_SNAPL_REG 16
  24. #define ALTERA_TIMER_SNAPH_REG 20
  25. #define ALTERA_TIMER_CONTROL_ITO_MSK (0x1)
  26. #define ALTERA_TIMER_CONTROL_CONT_MSK (0x2)
  27. #define ALTERA_TIMER_CONTROL_START_MSK (0x4)
  28. #define ALTERA_TIMER_CONTROL_STOP_MSK (0x8)
  29. struct nios2_timer {
  30. void __iomem *base;
  31. unsigned long freq;
  32. };
  33. struct nios2_clockevent_dev {
  34. struct nios2_timer timer;
  35. struct clock_event_device ced;
  36. };
  37. struct nios2_clocksource {
  38. struct nios2_timer timer;
  39. struct clocksource cs;
  40. };
  41. static inline struct nios2_clockevent_dev *
  42. to_nios2_clkevent(struct clock_event_device *evt)
  43. {
  44. return container_of(evt, struct nios2_clockevent_dev, ced);
  45. }
  46. static inline struct nios2_clocksource *
  47. to_nios2_clksource(struct clocksource *cs)
  48. {
  49. return container_of(cs, struct nios2_clocksource, cs);
  50. }
  51. static u16 timer_readw(struct nios2_timer *timer, u32 offs)
  52. {
  53. return readw(timer->base + offs);
  54. }
  55. static void timer_writew(struct nios2_timer *timer, u16 val, u32 offs)
  56. {
  57. writew(val, timer->base + offs);
  58. }
  59. static inline unsigned long read_timersnapshot(struct nios2_timer *timer)
  60. {
  61. unsigned long count;
  62. timer_writew(timer, 0, ALTERA_TIMER_SNAPL_REG);
  63. count = timer_readw(timer, ALTERA_TIMER_SNAPH_REG) << 16 |
  64. timer_readw(timer, ALTERA_TIMER_SNAPL_REG);
  65. return count;
  66. }
  67. static cycle_t nios2_timer_read(struct clocksource *cs)
  68. {
  69. struct nios2_clocksource *nios2_cs = to_nios2_clksource(cs);
  70. unsigned long flags;
  71. u32 count;
  72. local_irq_save(flags);
  73. count = read_timersnapshot(&nios2_cs->timer);
  74. local_irq_restore(flags);
  75. /* Counter is counting down */
  76. return ~count;
  77. }
  78. static struct nios2_clocksource nios2_cs = {
  79. .cs = {
  80. .name = "nios2-clksrc",
  81. .rating = 250,
  82. .read = nios2_timer_read,
  83. .mask = CLOCKSOURCE_MASK(32),
  84. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  85. },
  86. };
  87. cycles_t get_cycles(void)
  88. {
  89. return nios2_timer_read(&nios2_cs.cs);
  90. }
  91. static void nios2_timer_start(struct nios2_timer *timer)
  92. {
  93. u16 ctrl;
  94. ctrl = timer_readw(timer, ALTERA_TIMER_CONTROL_REG);
  95. ctrl |= ALTERA_TIMER_CONTROL_START_MSK;
  96. timer_writew(timer, ctrl, ALTERA_TIMER_CONTROL_REG);
  97. }
  98. static void nios2_timer_stop(struct nios2_timer *timer)
  99. {
  100. u16 ctrl;
  101. ctrl = timer_readw(timer, ALTERA_TIMER_CONTROL_REG);
  102. ctrl |= ALTERA_TIMER_CONTROL_STOP_MSK;
  103. timer_writew(timer, ctrl, ALTERA_TIMER_CONTROL_REG);
  104. }
  105. static void nios2_timer_config(struct nios2_timer *timer, unsigned long period,
  106. enum clock_event_mode mode)
  107. {
  108. u16 ctrl;
  109. /* The timer's actual period is one cycle greater than the value
  110. * stored in the period register. */
  111. period--;
  112. ctrl = timer_readw(timer, ALTERA_TIMER_CONTROL_REG);
  113. /* stop counter */
  114. timer_writew(timer, ctrl | ALTERA_TIMER_CONTROL_STOP_MSK,
  115. ALTERA_TIMER_CONTROL_REG);
  116. /* write new count */
  117. timer_writew(timer, period, ALTERA_TIMER_PERIODL_REG);
  118. timer_writew(timer, period >> 16, ALTERA_TIMER_PERIODH_REG);
  119. ctrl |= ALTERA_TIMER_CONTROL_START_MSK | ALTERA_TIMER_CONTROL_ITO_MSK;
  120. if (mode == CLOCK_EVT_MODE_PERIODIC)
  121. ctrl |= ALTERA_TIMER_CONTROL_CONT_MSK;
  122. else
  123. ctrl &= ~ALTERA_TIMER_CONTROL_CONT_MSK;
  124. timer_writew(timer, ctrl, ALTERA_TIMER_CONTROL_REG);
  125. }
  126. static int nios2_timer_set_next_event(unsigned long delta,
  127. struct clock_event_device *evt)
  128. {
  129. struct nios2_clockevent_dev *nios2_ced = to_nios2_clkevent(evt);
  130. nios2_timer_config(&nios2_ced->timer, delta, evt->mode);
  131. return 0;
  132. }
  133. static void nios2_timer_set_mode(enum clock_event_mode mode,
  134. struct clock_event_device *evt)
  135. {
  136. unsigned long period;
  137. struct nios2_clockevent_dev *nios2_ced = to_nios2_clkevent(evt);
  138. struct nios2_timer *timer = &nios2_ced->timer;
  139. switch (mode) {
  140. case CLOCK_EVT_MODE_PERIODIC:
  141. period = DIV_ROUND_UP(timer->freq, HZ);
  142. nios2_timer_config(timer, period, CLOCK_EVT_MODE_PERIODIC);
  143. break;
  144. case CLOCK_EVT_MODE_ONESHOT:
  145. case CLOCK_EVT_MODE_UNUSED:
  146. case CLOCK_EVT_MODE_SHUTDOWN:
  147. nios2_timer_stop(timer);
  148. break;
  149. case CLOCK_EVT_MODE_RESUME:
  150. nios2_timer_start(timer);
  151. break;
  152. }
  153. }
  154. irqreturn_t timer_interrupt(int irq, void *dev_id)
  155. {
  156. struct clock_event_device *evt = (struct clock_event_device *) dev_id;
  157. struct nios2_clockevent_dev *nios2_ced = to_nios2_clkevent(evt);
  158. /* Clear the interrupt condition */
  159. timer_writew(&nios2_ced->timer, 0, ALTERA_TIMER_STATUS_REG);
  160. evt->event_handler(evt);
  161. return IRQ_HANDLED;
  162. }
  163. static void __init nios2_timer_get_base_and_freq(struct device_node *np,
  164. void __iomem **base, u32 *freq)
  165. {
  166. *base = of_iomap(np, 0);
  167. if (!*base)
  168. panic("Unable to map reg for %s\n", np->name);
  169. if (of_property_read_u32(np, "clock-frequency", freq))
  170. panic("Unable to get %s clock frequency\n", np->name);
  171. }
  172. static struct nios2_clockevent_dev nios2_ce = {
  173. .ced = {
  174. .name = "nios2-clkevent",
  175. .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
  176. .rating = 250,
  177. .shift = 32,
  178. .set_next_event = nios2_timer_set_next_event,
  179. .set_mode = nios2_timer_set_mode,
  180. },
  181. };
  182. static __init void nios2_clockevent_init(struct device_node *timer)
  183. {
  184. void __iomem *iobase;
  185. u32 freq;
  186. int irq;
  187. nios2_timer_get_base_and_freq(timer, &iobase, &freq);
  188. irq = irq_of_parse_and_map(timer, 0);
  189. if (!irq)
  190. panic("Unable to parse timer irq\n");
  191. nios2_ce.timer.base = iobase;
  192. nios2_ce.timer.freq = freq;
  193. nios2_ce.ced.cpumask = cpumask_of(0);
  194. nios2_ce.ced.irq = irq;
  195. nios2_timer_stop(&nios2_ce.timer);
  196. /* clear pending interrupt */
  197. timer_writew(&nios2_ce.timer, 0, ALTERA_TIMER_STATUS_REG);
  198. if (request_irq(irq, timer_interrupt, IRQF_TIMER, timer->name,
  199. &nios2_ce.ced))
  200. panic("Unable to setup timer irq\n");
  201. clockevents_config_and_register(&nios2_ce.ced, freq, 1, ULONG_MAX);
  202. }
  203. static __init void nios2_clocksource_init(struct device_node *timer)
  204. {
  205. unsigned int ctrl;
  206. void __iomem *iobase;
  207. u32 freq;
  208. nios2_timer_get_base_and_freq(timer, &iobase, &freq);
  209. nios2_cs.timer.base = iobase;
  210. nios2_cs.timer.freq = freq;
  211. clocksource_register_hz(&nios2_cs.cs, freq);
  212. timer_writew(&nios2_cs.timer, USHRT_MAX, ALTERA_TIMER_PERIODL_REG);
  213. timer_writew(&nios2_cs.timer, USHRT_MAX, ALTERA_TIMER_PERIODH_REG);
  214. /* interrupt disable + continuous + start */
  215. ctrl = ALTERA_TIMER_CONTROL_CONT_MSK | ALTERA_TIMER_CONTROL_START_MSK;
  216. timer_writew(&nios2_cs.timer, ctrl, ALTERA_TIMER_CONTROL_REG);
  217. /* Calibrate the delay loop directly */
  218. lpj_fine = freq / HZ;
  219. }
  220. /*
  221. * The first timer instance will use as a clockevent. If there are two or
  222. * more instances, the second one gets used as clocksource and all
  223. * others are unused.
  224. */
  225. static void __init nios2_time_init(struct device_node *timer)
  226. {
  227. static int num_called;
  228. switch (num_called) {
  229. case 0:
  230. nios2_clockevent_init(timer);
  231. break;
  232. case 1:
  233. nios2_clocksource_init(timer);
  234. break;
  235. default:
  236. break;
  237. }
  238. num_called++;
  239. }
  240. void read_persistent_clock(struct timespec *ts)
  241. {
  242. ts->tv_sec = mktime(2007, 1, 1, 0, 0, 0);
  243. ts->tv_nsec = 0;
  244. }
  245. void __init time_init(void)
  246. {
  247. clocksource_of_init();
  248. }
  249. CLOCKSOURCE_OF_DECLARE(nios2_timer, "altr,timer-1.0", nios2_time_init);