time-efm32.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /*
  2. * Copyright (C) 2013 Pengutronix
  3. * Uwe Kleine-Koenig <u.kleine-koenig@pengutronix.de>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it under
  6. * the terms of the GNU General Public License version 2 as published by the
  7. * Free Software Foundation.
  8. */
  9. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  10. #include <linux/kernel.h>
  11. #include <linux/clocksource.h>
  12. #include <linux/clockchips.h>
  13. #include <linux/irq.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/of.h>
  16. #include <linux/of_address.h>
  17. #include <linux/of_irq.h>
  18. #include <linux/clk.h>
  19. #define TIMERn_CTRL 0x00
  20. #define TIMERn_CTRL_PRESC(val) (((val) & 0xf) << 24)
  21. #define TIMERn_CTRL_PRESC_1024 TIMERn_CTRL_PRESC(10)
  22. #define TIMERn_CTRL_CLKSEL(val) (((val) & 0x3) << 16)
  23. #define TIMERn_CTRL_CLKSEL_PRESCHFPERCLK TIMERn_CTRL_CLKSEL(0)
  24. #define TIMERn_CTRL_OSMEN 0x00000010
  25. #define TIMERn_CTRL_MODE(val) (((val) & 0x3) << 0)
  26. #define TIMERn_CTRL_MODE_UP TIMERn_CTRL_MODE(0)
  27. #define TIMERn_CTRL_MODE_DOWN TIMERn_CTRL_MODE(1)
  28. #define TIMERn_CMD 0x04
  29. #define TIMERn_CMD_START 0x00000001
  30. #define TIMERn_CMD_STOP 0x00000002
  31. #define TIMERn_IEN 0x0c
  32. #define TIMERn_IF 0x10
  33. #define TIMERn_IFS 0x14
  34. #define TIMERn_IFC 0x18
  35. #define TIMERn_IRQ_UF 0x00000002
  36. #define TIMERn_TOP 0x1c
  37. #define TIMERn_CNT 0x24
  38. struct efm32_clock_event_ddata {
  39. struct clock_event_device evtdev;
  40. void __iomem *base;
  41. unsigned periodic_top;
  42. };
  43. static int efm32_clock_event_shutdown(struct clock_event_device *evtdev)
  44. {
  45. struct efm32_clock_event_ddata *ddata =
  46. container_of(evtdev, struct efm32_clock_event_ddata, evtdev);
  47. writel_relaxed(TIMERn_CMD_STOP, ddata->base + TIMERn_CMD);
  48. return 0;
  49. }
  50. static int efm32_clock_event_set_oneshot(struct clock_event_device *evtdev)
  51. {
  52. struct efm32_clock_event_ddata *ddata =
  53. container_of(evtdev, struct efm32_clock_event_ddata, evtdev);
  54. writel_relaxed(TIMERn_CMD_STOP, ddata->base + TIMERn_CMD);
  55. writel_relaxed(TIMERn_CTRL_PRESC_1024 |
  56. TIMERn_CTRL_CLKSEL_PRESCHFPERCLK |
  57. TIMERn_CTRL_OSMEN |
  58. TIMERn_CTRL_MODE_DOWN,
  59. ddata->base + TIMERn_CTRL);
  60. return 0;
  61. }
  62. static int efm32_clock_event_set_periodic(struct clock_event_device *evtdev)
  63. {
  64. struct efm32_clock_event_ddata *ddata =
  65. container_of(evtdev, struct efm32_clock_event_ddata, evtdev);
  66. writel_relaxed(TIMERn_CMD_STOP, ddata->base + TIMERn_CMD);
  67. writel_relaxed(ddata->periodic_top, ddata->base + TIMERn_TOP);
  68. writel_relaxed(TIMERn_CTRL_PRESC_1024 |
  69. TIMERn_CTRL_CLKSEL_PRESCHFPERCLK |
  70. TIMERn_CTRL_MODE_DOWN,
  71. ddata->base + TIMERn_CTRL);
  72. writel_relaxed(TIMERn_CMD_START, ddata->base + TIMERn_CMD);
  73. return 0;
  74. }
  75. static int efm32_clock_event_set_next_event(unsigned long evt,
  76. struct clock_event_device *evtdev)
  77. {
  78. struct efm32_clock_event_ddata *ddata =
  79. container_of(evtdev, struct efm32_clock_event_ddata, evtdev);
  80. writel_relaxed(TIMERn_CMD_STOP, ddata->base + TIMERn_CMD);
  81. writel_relaxed(evt, ddata->base + TIMERn_CNT);
  82. writel_relaxed(TIMERn_CMD_START, ddata->base + TIMERn_CMD);
  83. return 0;
  84. }
  85. static irqreturn_t efm32_clock_event_handler(int irq, void *dev_id)
  86. {
  87. struct efm32_clock_event_ddata *ddata = dev_id;
  88. writel_relaxed(TIMERn_IRQ_UF, ddata->base + TIMERn_IFC);
  89. ddata->evtdev.event_handler(&ddata->evtdev);
  90. return IRQ_HANDLED;
  91. }
  92. static struct efm32_clock_event_ddata clock_event_ddata = {
  93. .evtdev = {
  94. .name = "efm32 clockevent",
  95. .features = CLOCK_EVT_FEAT_ONESHOT | CLOCK_EVT_FEAT_PERIODIC,
  96. .set_state_shutdown = efm32_clock_event_shutdown,
  97. .set_state_periodic = efm32_clock_event_set_periodic,
  98. .set_state_oneshot = efm32_clock_event_set_oneshot,
  99. .set_next_event = efm32_clock_event_set_next_event,
  100. .rating = 200,
  101. },
  102. };
  103. static struct irqaction efm32_clock_event_irq = {
  104. .name = "efm32 clockevent",
  105. .flags = IRQF_TIMER,
  106. .handler = efm32_clock_event_handler,
  107. .dev_id = &clock_event_ddata,
  108. };
  109. static int __init efm32_clocksource_init(struct device_node *np)
  110. {
  111. struct clk *clk;
  112. void __iomem *base;
  113. unsigned long rate;
  114. int ret;
  115. clk = of_clk_get(np, 0);
  116. if (IS_ERR(clk)) {
  117. ret = PTR_ERR(clk);
  118. pr_err("failed to get clock for clocksource (%d)\n", ret);
  119. goto err_clk_get;
  120. }
  121. ret = clk_prepare_enable(clk);
  122. if (ret) {
  123. pr_err("failed to enable timer clock for clocksource (%d)\n",
  124. ret);
  125. goto err_clk_enable;
  126. }
  127. rate = clk_get_rate(clk);
  128. base = of_iomap(np, 0);
  129. if (!base) {
  130. ret = -EADDRNOTAVAIL;
  131. pr_err("failed to map registers for clocksource\n");
  132. goto err_iomap;
  133. }
  134. writel_relaxed(TIMERn_CTRL_PRESC_1024 |
  135. TIMERn_CTRL_CLKSEL_PRESCHFPERCLK |
  136. TIMERn_CTRL_MODE_UP, base + TIMERn_CTRL);
  137. writel_relaxed(TIMERn_CMD_START, base + TIMERn_CMD);
  138. ret = clocksource_mmio_init(base + TIMERn_CNT, "efm32 timer",
  139. DIV_ROUND_CLOSEST(rate, 1024), 200, 16,
  140. clocksource_mmio_readl_up);
  141. if (ret) {
  142. pr_err("failed to init clocksource (%d)\n", ret);
  143. goto err_clocksource_init;
  144. }
  145. return 0;
  146. err_clocksource_init:
  147. iounmap(base);
  148. err_iomap:
  149. clk_disable_unprepare(clk);
  150. err_clk_enable:
  151. clk_put(clk);
  152. err_clk_get:
  153. return ret;
  154. }
  155. static int __init efm32_clockevent_init(struct device_node *np)
  156. {
  157. struct clk *clk;
  158. void __iomem *base;
  159. unsigned long rate;
  160. int irq;
  161. int ret;
  162. clk = of_clk_get(np, 0);
  163. if (IS_ERR(clk)) {
  164. ret = PTR_ERR(clk);
  165. pr_err("failed to get clock for clockevent (%d)\n", ret);
  166. goto err_clk_get;
  167. }
  168. ret = clk_prepare_enable(clk);
  169. if (ret) {
  170. pr_err("failed to enable timer clock for clockevent (%d)\n",
  171. ret);
  172. goto err_clk_enable;
  173. }
  174. rate = clk_get_rate(clk);
  175. base = of_iomap(np, 0);
  176. if (!base) {
  177. ret = -EADDRNOTAVAIL;
  178. pr_err("failed to map registers for clockevent\n");
  179. goto err_iomap;
  180. }
  181. irq = irq_of_parse_and_map(np, 0);
  182. if (!irq) {
  183. ret = -ENOENT;
  184. pr_err("failed to get irq for clockevent\n");
  185. goto err_get_irq;
  186. }
  187. writel_relaxed(TIMERn_IRQ_UF, base + TIMERn_IEN);
  188. clock_event_ddata.base = base;
  189. clock_event_ddata.periodic_top = DIV_ROUND_CLOSEST(rate, 1024 * HZ);
  190. clockevents_config_and_register(&clock_event_ddata.evtdev,
  191. DIV_ROUND_CLOSEST(rate, 1024),
  192. 0xf, 0xffff);
  193. ret = setup_irq(irq, &efm32_clock_event_irq);
  194. if (ret) {
  195. pr_err("Failed setup irq\n");
  196. goto err_setup_irq;
  197. }
  198. return 0;
  199. err_setup_irq:
  200. err_get_irq:
  201. iounmap(base);
  202. err_iomap:
  203. clk_disable_unprepare(clk);
  204. err_clk_enable:
  205. clk_put(clk);
  206. err_clk_get:
  207. return ret;
  208. }
  209. /*
  210. * This function asserts that we have exactly one clocksource and one
  211. * clock_event_device in the end.
  212. */
  213. static int __init efm32_timer_init(struct device_node *np)
  214. {
  215. static int has_clocksource, has_clockevent;
  216. int ret = 0;
  217. if (!has_clocksource) {
  218. ret = efm32_clocksource_init(np);
  219. if (!ret) {
  220. has_clocksource = 1;
  221. return 0;
  222. }
  223. }
  224. if (!has_clockevent) {
  225. ret = efm32_clockevent_init(np);
  226. if (!ret) {
  227. has_clockevent = 1;
  228. return 0;
  229. }
  230. }
  231. return ret;
  232. }
  233. TIMER_OF_DECLARE(efm32compat, "efm32,timer", efm32_timer_init);
  234. TIMER_OF_DECLARE(efm32, "energymicro,efm32-timer", efm32_timer_init);