time.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. bool periodic)
  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 (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, false);
  134. return 0;
  135. }
  136. static int nios2_timer_shutdown(struct clock_event_device *evt)
  137. {
  138. struct nios2_clockevent_dev *nios2_ced = to_nios2_clkevent(evt);
  139. struct nios2_timer *timer = &nios2_ced->timer;
  140. nios2_timer_stop(timer);
  141. return 0;
  142. }
  143. static int nios2_timer_set_periodic(struct clock_event_device *evt)
  144. {
  145. unsigned long period;
  146. struct nios2_clockevent_dev *nios2_ced = to_nios2_clkevent(evt);
  147. struct nios2_timer *timer = &nios2_ced->timer;
  148. period = DIV_ROUND_UP(timer->freq, HZ);
  149. nios2_timer_config(timer, period, true);
  150. return 0;
  151. }
  152. static int nios2_timer_resume(struct clock_event_device *evt)
  153. {
  154. struct nios2_clockevent_dev *nios2_ced = to_nios2_clkevent(evt);
  155. struct nios2_timer *timer = &nios2_ced->timer;
  156. nios2_timer_start(timer);
  157. return 0;
  158. }
  159. irqreturn_t timer_interrupt(int irq, void *dev_id)
  160. {
  161. struct clock_event_device *evt = (struct clock_event_device *) dev_id;
  162. struct nios2_clockevent_dev *nios2_ced = to_nios2_clkevent(evt);
  163. /* Clear the interrupt condition */
  164. timer_writew(&nios2_ced->timer, 0, ALTERA_TIMER_STATUS_REG);
  165. evt->event_handler(evt);
  166. return IRQ_HANDLED;
  167. }
  168. static void __init nios2_timer_get_base_and_freq(struct device_node *np,
  169. void __iomem **base, u32 *freq)
  170. {
  171. *base = of_iomap(np, 0);
  172. if (!*base)
  173. panic("Unable to map reg for %s\n", np->name);
  174. if (of_property_read_u32(np, "clock-frequency", freq))
  175. panic("Unable to get %s clock frequency\n", np->name);
  176. }
  177. static struct nios2_clockevent_dev nios2_ce = {
  178. .ced = {
  179. .name = "nios2-clkevent",
  180. .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
  181. .rating = 250,
  182. .shift = 32,
  183. .set_next_event = nios2_timer_set_next_event,
  184. .set_state_shutdown = nios2_timer_shutdown,
  185. .set_state_periodic = nios2_timer_set_periodic,
  186. .set_state_oneshot = nios2_timer_shutdown,
  187. .tick_resume = nios2_timer_resume,
  188. },
  189. };
  190. static __init void nios2_clockevent_init(struct device_node *timer)
  191. {
  192. void __iomem *iobase;
  193. u32 freq;
  194. int irq;
  195. nios2_timer_get_base_and_freq(timer, &iobase, &freq);
  196. irq = irq_of_parse_and_map(timer, 0);
  197. if (!irq)
  198. panic("Unable to parse timer irq\n");
  199. nios2_ce.timer.base = iobase;
  200. nios2_ce.timer.freq = freq;
  201. nios2_ce.ced.cpumask = cpumask_of(0);
  202. nios2_ce.ced.irq = irq;
  203. nios2_timer_stop(&nios2_ce.timer);
  204. /* clear pending interrupt */
  205. timer_writew(&nios2_ce.timer, 0, ALTERA_TIMER_STATUS_REG);
  206. if (request_irq(irq, timer_interrupt, IRQF_TIMER, timer->name,
  207. &nios2_ce.ced))
  208. panic("Unable to setup timer irq\n");
  209. clockevents_config_and_register(&nios2_ce.ced, freq, 1, ULONG_MAX);
  210. }
  211. static __init void nios2_clocksource_init(struct device_node *timer)
  212. {
  213. unsigned int ctrl;
  214. void __iomem *iobase;
  215. u32 freq;
  216. nios2_timer_get_base_and_freq(timer, &iobase, &freq);
  217. nios2_cs.timer.base = iobase;
  218. nios2_cs.timer.freq = freq;
  219. clocksource_register_hz(&nios2_cs.cs, freq);
  220. timer_writew(&nios2_cs.timer, USHRT_MAX, ALTERA_TIMER_PERIODL_REG);
  221. timer_writew(&nios2_cs.timer, USHRT_MAX, ALTERA_TIMER_PERIODH_REG);
  222. /* interrupt disable + continuous + start */
  223. ctrl = ALTERA_TIMER_CONTROL_CONT_MSK | ALTERA_TIMER_CONTROL_START_MSK;
  224. timer_writew(&nios2_cs.timer, ctrl, ALTERA_TIMER_CONTROL_REG);
  225. /* Calibrate the delay loop directly */
  226. lpj_fine = freq / HZ;
  227. }
  228. /*
  229. * The first timer instance will use as a clockevent. If there are two or
  230. * more instances, the second one gets used as clocksource and all
  231. * others are unused.
  232. */
  233. static void __init nios2_time_init(struct device_node *timer)
  234. {
  235. static int num_called;
  236. switch (num_called) {
  237. case 0:
  238. nios2_clockevent_init(timer);
  239. break;
  240. case 1:
  241. nios2_clocksource_init(timer);
  242. break;
  243. default:
  244. break;
  245. }
  246. num_called++;
  247. }
  248. void read_persistent_clock(struct timespec *ts)
  249. {
  250. ts->tv_sec = mktime(2007, 1, 1, 0, 0, 0);
  251. ts->tv_nsec = 0;
  252. }
  253. void __init time_init(void)
  254. {
  255. struct device_node *np;
  256. int count = 0;
  257. for_each_compatible_node(np, NULL, ALTR_TIMER_COMPATIBLE)
  258. count++;
  259. if (count < 2)
  260. panic("%d timer is found, it needs 2 timers in system\n", count);
  261. clocksource_probe();
  262. }
  263. CLOCKSOURCE_OF_DECLARE(nios2_timer, ALTR_TIMER_COMPATIBLE, nios2_time_init);