time.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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 u64 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. /* Only read timer if it has been initialized */
  92. if (nios2_cs.timer.base)
  93. return nios2_timer_read(&nios2_cs.cs);
  94. return 0;
  95. }
  96. EXPORT_SYMBOL(get_cycles);
  97. static void nios2_timer_start(struct nios2_timer *timer)
  98. {
  99. u16 ctrl;
  100. ctrl = timer_readw(timer, ALTERA_TIMER_CONTROL_REG);
  101. ctrl |= ALTERA_TIMER_CONTROL_START_MSK;
  102. timer_writew(timer, ctrl, ALTERA_TIMER_CONTROL_REG);
  103. }
  104. static void nios2_timer_stop(struct nios2_timer *timer)
  105. {
  106. u16 ctrl;
  107. ctrl = timer_readw(timer, ALTERA_TIMER_CONTROL_REG);
  108. ctrl |= ALTERA_TIMER_CONTROL_STOP_MSK;
  109. timer_writew(timer, ctrl, ALTERA_TIMER_CONTROL_REG);
  110. }
  111. static void nios2_timer_config(struct nios2_timer *timer, unsigned long period,
  112. bool periodic)
  113. {
  114. u16 ctrl;
  115. /* The timer's actual period is one cycle greater than the value
  116. * stored in the period register. */
  117. period--;
  118. ctrl = timer_readw(timer, ALTERA_TIMER_CONTROL_REG);
  119. /* stop counter */
  120. timer_writew(timer, ctrl | ALTERA_TIMER_CONTROL_STOP_MSK,
  121. ALTERA_TIMER_CONTROL_REG);
  122. /* write new count */
  123. timer_writew(timer, period, ALTERA_TIMER_PERIODL_REG);
  124. timer_writew(timer, period >> 16, ALTERA_TIMER_PERIODH_REG);
  125. ctrl |= ALTERA_TIMER_CONTROL_START_MSK | ALTERA_TIMER_CONTROL_ITO_MSK;
  126. if (periodic)
  127. ctrl |= ALTERA_TIMER_CONTROL_CONT_MSK;
  128. else
  129. ctrl &= ~ALTERA_TIMER_CONTROL_CONT_MSK;
  130. timer_writew(timer, ctrl, ALTERA_TIMER_CONTROL_REG);
  131. }
  132. static int nios2_timer_set_next_event(unsigned long delta,
  133. struct clock_event_device *evt)
  134. {
  135. struct nios2_clockevent_dev *nios2_ced = to_nios2_clkevent(evt);
  136. nios2_timer_config(&nios2_ced->timer, delta, false);
  137. return 0;
  138. }
  139. static int nios2_timer_shutdown(struct clock_event_device *evt)
  140. {
  141. struct nios2_clockevent_dev *nios2_ced = to_nios2_clkevent(evt);
  142. struct nios2_timer *timer = &nios2_ced->timer;
  143. nios2_timer_stop(timer);
  144. return 0;
  145. }
  146. static int nios2_timer_set_periodic(struct clock_event_device *evt)
  147. {
  148. unsigned long period;
  149. struct nios2_clockevent_dev *nios2_ced = to_nios2_clkevent(evt);
  150. struct nios2_timer *timer = &nios2_ced->timer;
  151. period = DIV_ROUND_UP(timer->freq, HZ);
  152. nios2_timer_config(timer, period, true);
  153. return 0;
  154. }
  155. static int nios2_timer_resume(struct clock_event_device *evt)
  156. {
  157. struct nios2_clockevent_dev *nios2_ced = to_nios2_clkevent(evt);
  158. struct nios2_timer *timer = &nios2_ced->timer;
  159. nios2_timer_start(timer);
  160. return 0;
  161. }
  162. irqreturn_t timer_interrupt(int irq, void *dev_id)
  163. {
  164. struct clock_event_device *evt = (struct clock_event_device *) dev_id;
  165. struct nios2_clockevent_dev *nios2_ced = to_nios2_clkevent(evt);
  166. /* Clear the interrupt condition */
  167. timer_writew(&nios2_ced->timer, 0, ALTERA_TIMER_STATUS_REG);
  168. evt->event_handler(evt);
  169. return IRQ_HANDLED;
  170. }
  171. static int __init nios2_timer_get_base_and_freq(struct device_node *np,
  172. void __iomem **base, u32 *freq)
  173. {
  174. *base = of_iomap(np, 0);
  175. if (!*base) {
  176. pr_crit("Unable to map reg for %pOFn\n", np);
  177. return -ENXIO;
  178. }
  179. if (of_property_read_u32(np, "clock-frequency", freq)) {
  180. pr_crit("Unable to get %pOFn clock frequency\n", np);
  181. return -EINVAL;
  182. }
  183. return 0;
  184. }
  185. static struct nios2_clockevent_dev nios2_ce = {
  186. .ced = {
  187. .name = "nios2-clkevent",
  188. .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
  189. .rating = 250,
  190. .shift = 32,
  191. .set_next_event = nios2_timer_set_next_event,
  192. .set_state_shutdown = nios2_timer_shutdown,
  193. .set_state_periodic = nios2_timer_set_periodic,
  194. .set_state_oneshot = nios2_timer_shutdown,
  195. .tick_resume = nios2_timer_resume,
  196. },
  197. };
  198. static __init int nios2_clockevent_init(struct device_node *timer)
  199. {
  200. void __iomem *iobase;
  201. u32 freq;
  202. int irq, ret;
  203. ret = nios2_timer_get_base_and_freq(timer, &iobase, &freq);
  204. if (ret)
  205. return ret;
  206. irq = irq_of_parse_and_map(timer, 0);
  207. if (!irq) {
  208. pr_crit("Unable to parse timer irq\n");
  209. return -EINVAL;
  210. }
  211. nios2_ce.timer.base = iobase;
  212. nios2_ce.timer.freq = freq;
  213. nios2_ce.ced.cpumask = cpumask_of(0);
  214. nios2_ce.ced.irq = irq;
  215. nios2_timer_stop(&nios2_ce.timer);
  216. /* clear pending interrupt */
  217. timer_writew(&nios2_ce.timer, 0, ALTERA_TIMER_STATUS_REG);
  218. ret = request_irq(irq, timer_interrupt, IRQF_TIMER, timer->name,
  219. &nios2_ce.ced);
  220. if (ret) {
  221. pr_crit("Unable to setup timer irq\n");
  222. return ret;
  223. }
  224. clockevents_config_and_register(&nios2_ce.ced, freq, 1, ULONG_MAX);
  225. return 0;
  226. }
  227. static __init int nios2_clocksource_init(struct device_node *timer)
  228. {
  229. unsigned int ctrl;
  230. void __iomem *iobase;
  231. u32 freq;
  232. int ret;
  233. ret = nios2_timer_get_base_and_freq(timer, &iobase, &freq);
  234. if (ret)
  235. return ret;
  236. nios2_cs.timer.base = iobase;
  237. nios2_cs.timer.freq = freq;
  238. ret = clocksource_register_hz(&nios2_cs.cs, freq);
  239. if (ret)
  240. return ret;
  241. timer_writew(&nios2_cs.timer, USHRT_MAX, ALTERA_TIMER_PERIODL_REG);
  242. timer_writew(&nios2_cs.timer, USHRT_MAX, ALTERA_TIMER_PERIODH_REG);
  243. /* interrupt disable + continuous + start */
  244. ctrl = ALTERA_TIMER_CONTROL_CONT_MSK | ALTERA_TIMER_CONTROL_START_MSK;
  245. timer_writew(&nios2_cs.timer, ctrl, ALTERA_TIMER_CONTROL_REG);
  246. /* Calibrate the delay loop directly */
  247. lpj_fine = freq / HZ;
  248. return 0;
  249. }
  250. /*
  251. * The first timer instance will use as a clockevent. If there are two or
  252. * more instances, the second one gets used as clocksource and all
  253. * others are unused.
  254. */
  255. static int __init nios2_time_init(struct device_node *timer)
  256. {
  257. static int num_called;
  258. int ret;
  259. switch (num_called) {
  260. case 0:
  261. ret = nios2_clockevent_init(timer);
  262. break;
  263. case 1:
  264. ret = nios2_clocksource_init(timer);
  265. break;
  266. default:
  267. ret = 0;
  268. break;
  269. }
  270. num_called++;
  271. return ret;
  272. }
  273. void read_persistent_clock64(struct timespec64 *ts)
  274. {
  275. ts->tv_sec = mktime64(2007, 1, 1, 0, 0, 0);
  276. ts->tv_nsec = 0;
  277. }
  278. void __init time_init(void)
  279. {
  280. struct device_node *np;
  281. int count = 0;
  282. for_each_compatible_node(np, NULL, ALTR_TIMER_COMPATIBLE)
  283. count++;
  284. if (count < 2)
  285. panic("%d timer is found, it needs 2 timers in system\n", count);
  286. timer_probe();
  287. }
  288. TIMER_OF_DECLARE(nios2_timer, ALTR_TIMER_COMPATIBLE, nios2_time_init);