time-efm32.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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 void efm32_clock_event_set_mode(enum clock_event_mode mode,
  44. struct clock_event_device *evtdev)
  45. {
  46. struct efm32_clock_event_ddata *ddata =
  47. container_of(evtdev, struct efm32_clock_event_ddata, evtdev);
  48. switch (mode) {
  49. case CLOCK_EVT_MODE_PERIODIC:
  50. writel_relaxed(TIMERn_CMD_STOP, ddata->base + TIMERn_CMD);
  51. writel_relaxed(ddata->periodic_top, ddata->base + TIMERn_TOP);
  52. writel_relaxed(TIMERn_CTRL_PRESC_1024 |
  53. TIMERn_CTRL_CLKSEL_PRESCHFPERCLK |
  54. TIMERn_CTRL_MODE_DOWN,
  55. ddata->base + TIMERn_CTRL);
  56. writel_relaxed(TIMERn_CMD_START, ddata->base + TIMERn_CMD);
  57. break;
  58. case CLOCK_EVT_MODE_ONESHOT:
  59. writel_relaxed(TIMERn_CMD_STOP, ddata->base + TIMERn_CMD);
  60. writel_relaxed(TIMERn_CTRL_PRESC_1024 |
  61. TIMERn_CTRL_CLKSEL_PRESCHFPERCLK |
  62. TIMERn_CTRL_OSMEN |
  63. TIMERn_CTRL_MODE_DOWN,
  64. ddata->base + TIMERn_CTRL);
  65. break;
  66. case CLOCK_EVT_MODE_UNUSED:
  67. case CLOCK_EVT_MODE_SHUTDOWN:
  68. writel_relaxed(TIMERn_CMD_STOP, ddata->base + TIMERn_CMD);
  69. break;
  70. case CLOCK_EVT_MODE_RESUME:
  71. break;
  72. }
  73. }
  74. static int efm32_clock_event_set_next_event(unsigned long evt,
  75. struct clock_event_device *evtdev)
  76. {
  77. struct efm32_clock_event_ddata *ddata =
  78. container_of(evtdev, struct efm32_clock_event_ddata, evtdev);
  79. writel_relaxed(TIMERn_CMD_STOP, ddata->base + TIMERn_CMD);
  80. writel_relaxed(evt, ddata->base + TIMERn_CNT);
  81. writel_relaxed(TIMERn_CMD_START, ddata->base + TIMERn_CMD);
  82. return 0;
  83. }
  84. static irqreturn_t efm32_clock_event_handler(int irq, void *dev_id)
  85. {
  86. struct efm32_clock_event_ddata *ddata = dev_id;
  87. writel_relaxed(TIMERn_IRQ_UF, ddata->base + TIMERn_IFC);
  88. ddata->evtdev.event_handler(&ddata->evtdev);
  89. return IRQ_HANDLED;
  90. }
  91. static struct efm32_clock_event_ddata clock_event_ddata = {
  92. .evtdev = {
  93. .name = "efm32 clockevent",
  94. .features = CLOCK_EVT_FEAT_ONESHOT | CLOCK_EVT_FEAT_PERIODIC,
  95. .set_mode = efm32_clock_event_set_mode,
  96. .set_next_event = efm32_clock_event_set_next_event,
  97. .rating = 200,
  98. },
  99. };
  100. static struct irqaction efm32_clock_event_irq = {
  101. .name = "efm32 clockevent",
  102. .flags = IRQF_TIMER,
  103. .handler = efm32_clock_event_handler,
  104. .dev_id = &clock_event_ddata,
  105. };
  106. static int __init efm32_clocksource_init(struct device_node *np)
  107. {
  108. struct clk *clk;
  109. void __iomem *base;
  110. unsigned long rate;
  111. int ret;
  112. clk = of_clk_get(np, 0);
  113. if (IS_ERR(clk)) {
  114. ret = PTR_ERR(clk);
  115. pr_err("failed to get clock for clocksource (%d)\n", ret);
  116. goto err_clk_get;
  117. }
  118. ret = clk_prepare_enable(clk);
  119. if (ret) {
  120. pr_err("failed to enable timer clock for clocksource (%d)\n",
  121. ret);
  122. goto err_clk_enable;
  123. }
  124. rate = clk_get_rate(clk);
  125. base = of_iomap(np, 0);
  126. if (!base) {
  127. ret = -EADDRNOTAVAIL;
  128. pr_err("failed to map registers for clocksource\n");
  129. goto err_iomap;
  130. }
  131. writel_relaxed(TIMERn_CTRL_PRESC_1024 |
  132. TIMERn_CTRL_CLKSEL_PRESCHFPERCLK |
  133. TIMERn_CTRL_MODE_UP, base + TIMERn_CTRL);
  134. writel_relaxed(TIMERn_CMD_START, base + TIMERn_CMD);
  135. ret = clocksource_mmio_init(base + TIMERn_CNT, "efm32 timer",
  136. DIV_ROUND_CLOSEST(rate, 1024), 200, 16,
  137. clocksource_mmio_readl_up);
  138. if (ret) {
  139. pr_err("failed to init clocksource (%d)\n", ret);
  140. goto err_clocksource_init;
  141. }
  142. return 0;
  143. err_clocksource_init:
  144. iounmap(base);
  145. err_iomap:
  146. clk_disable_unprepare(clk);
  147. err_clk_enable:
  148. clk_put(clk);
  149. err_clk_get:
  150. return ret;
  151. }
  152. static int __init efm32_clockevent_init(struct device_node *np)
  153. {
  154. struct clk *clk;
  155. void __iomem *base;
  156. unsigned long rate;
  157. int irq;
  158. int ret;
  159. clk = of_clk_get(np, 0);
  160. if (IS_ERR(clk)) {
  161. ret = PTR_ERR(clk);
  162. pr_err("failed to get clock for clockevent (%d)\n", ret);
  163. goto err_clk_get;
  164. }
  165. ret = clk_prepare_enable(clk);
  166. if (ret) {
  167. pr_err("failed to enable timer clock for clockevent (%d)\n",
  168. ret);
  169. goto err_clk_enable;
  170. }
  171. rate = clk_get_rate(clk);
  172. base = of_iomap(np, 0);
  173. if (!base) {
  174. ret = -EADDRNOTAVAIL;
  175. pr_err("failed to map registers for clockevent\n");
  176. goto err_iomap;
  177. }
  178. irq = irq_of_parse_and_map(np, 0);
  179. if (!irq) {
  180. ret = -ENOENT;
  181. pr_err("failed to get irq for clockevent\n");
  182. goto err_get_irq;
  183. }
  184. writel_relaxed(TIMERn_IRQ_UF, base + TIMERn_IEN);
  185. clock_event_ddata.base = base;
  186. clock_event_ddata.periodic_top = DIV_ROUND_CLOSEST(rate, 1024 * HZ);
  187. clockevents_config_and_register(&clock_event_ddata.evtdev,
  188. DIV_ROUND_CLOSEST(rate, 1024),
  189. 0xf, 0xffff);
  190. setup_irq(irq, &efm32_clock_event_irq);
  191. return 0;
  192. err_get_irq:
  193. iounmap(base);
  194. err_iomap:
  195. clk_disable_unprepare(clk);
  196. err_clk_enable:
  197. clk_put(clk);
  198. err_clk_get:
  199. return ret;
  200. }
  201. /*
  202. * This function asserts that we have exactly one clocksource and one
  203. * clock_event_device in the end.
  204. */
  205. static void __init efm32_timer_init(struct device_node *np)
  206. {
  207. static int has_clocksource, has_clockevent;
  208. int ret;
  209. if (!has_clocksource) {
  210. ret = efm32_clocksource_init(np);
  211. if (!ret) {
  212. has_clocksource = 1;
  213. return;
  214. }
  215. }
  216. if (!has_clockevent) {
  217. ret = efm32_clockevent_init(np);
  218. if (!ret) {
  219. has_clockevent = 1;
  220. return;
  221. }
  222. }
  223. }
  224. CLOCKSOURCE_OF_DECLARE(efm32compat, "efm32,timer", efm32_timer_init);
  225. CLOCKSOURCE_OF_DECLARE(efm32, "energymicro,efm32-timer", efm32_timer_init);