time.c 7.8 KB

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