timer-oxnas-rps.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /*
  2. * drivers/clocksource/timer-oxnas-rps.c
  3. *
  4. * Copyright (C) 2009 Oxford Semiconductor Ltd
  5. * Copyright (C) 2013 Ma Haijun <mahaijuns@gmail.com>
  6. * Copyright (C) 2016 Neil Armstrong <narmstrong@baylibre.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms and conditions of the GNU General Public License,
  10. * version 2, as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope it will be useful, but WITHOUT
  13. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  15. * more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  21. #include <linux/init.h>
  22. #include <linux/irq.h>
  23. #include <linux/io.h>
  24. #include <linux/clk.h>
  25. #include <linux/slab.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/of_irq.h>
  28. #include <linux/of_address.h>
  29. #include <linux/clockchips.h>
  30. #include <linux/sched_clock.h>
  31. /* TIMER1 used as tick
  32. * TIMER2 used as clocksource
  33. */
  34. /* Registers definitions */
  35. #define TIMER_LOAD_REG 0x0
  36. #define TIMER_CURR_REG 0x4
  37. #define TIMER_CTRL_REG 0x8
  38. #define TIMER_CLRINT_REG 0xC
  39. #define TIMER_BITS 24
  40. #define TIMER_MAX_VAL (BIT(TIMER_BITS) - 1)
  41. #define TIMER_PERIODIC BIT(6)
  42. #define TIMER_ENABLE BIT(7)
  43. #define TIMER_DIV1 (0)
  44. #define TIMER_DIV16 (1 << 2)
  45. #define TIMER_DIV256 (2 << 2)
  46. #define TIMER1_REG_OFFSET 0
  47. #define TIMER2_REG_OFFSET 0x20
  48. /* Clockevent & Clocksource data */
  49. struct oxnas_rps_timer {
  50. struct clock_event_device clkevent;
  51. void __iomem *clksrc_base;
  52. void __iomem *clkevt_base;
  53. unsigned long timer_period;
  54. unsigned int timer_prescaler;
  55. struct clk *clk;
  56. int irq;
  57. };
  58. static irqreturn_t oxnas_rps_timer_irq(int irq, void *dev_id)
  59. {
  60. struct oxnas_rps_timer *rps = dev_id;
  61. writel_relaxed(0, rps->clkevt_base + TIMER_CLRINT_REG);
  62. rps->clkevent.event_handler(&rps->clkevent);
  63. return IRQ_HANDLED;
  64. }
  65. static void oxnas_rps_timer_config(struct oxnas_rps_timer *rps,
  66. unsigned long period,
  67. unsigned int periodic)
  68. {
  69. uint32_t cfg = rps->timer_prescaler;
  70. if (period)
  71. cfg |= TIMER_ENABLE;
  72. if (periodic)
  73. cfg |= TIMER_PERIODIC;
  74. writel_relaxed(period, rps->clkevt_base + TIMER_LOAD_REG);
  75. writel_relaxed(cfg, rps->clkevt_base + TIMER_CTRL_REG);
  76. }
  77. static int oxnas_rps_timer_shutdown(struct clock_event_device *evt)
  78. {
  79. struct oxnas_rps_timer *rps =
  80. container_of(evt, struct oxnas_rps_timer, clkevent);
  81. oxnas_rps_timer_config(rps, 0, 0);
  82. return 0;
  83. }
  84. static int oxnas_rps_timer_set_periodic(struct clock_event_device *evt)
  85. {
  86. struct oxnas_rps_timer *rps =
  87. container_of(evt, struct oxnas_rps_timer, clkevent);
  88. oxnas_rps_timer_config(rps, rps->timer_period, 1);
  89. return 0;
  90. }
  91. static int oxnas_rps_timer_set_oneshot(struct clock_event_device *evt)
  92. {
  93. struct oxnas_rps_timer *rps =
  94. container_of(evt, struct oxnas_rps_timer, clkevent);
  95. oxnas_rps_timer_config(rps, rps->timer_period, 0);
  96. return 0;
  97. }
  98. static int oxnas_rps_timer_next_event(unsigned long delta,
  99. struct clock_event_device *evt)
  100. {
  101. struct oxnas_rps_timer *rps =
  102. container_of(evt, struct oxnas_rps_timer, clkevent);
  103. oxnas_rps_timer_config(rps, delta, 0);
  104. return 0;
  105. }
  106. static int __init oxnas_rps_clockevent_init(struct oxnas_rps_timer *rps)
  107. {
  108. ulong clk_rate = clk_get_rate(rps->clk);
  109. ulong timer_rate;
  110. /* Start with prescaler 1 */
  111. rps->timer_prescaler = TIMER_DIV1;
  112. rps->timer_period = DIV_ROUND_UP(clk_rate, HZ);
  113. timer_rate = clk_rate;
  114. if (rps->timer_period > TIMER_MAX_VAL) {
  115. rps->timer_prescaler = TIMER_DIV16;
  116. timer_rate = clk_rate / 16;
  117. rps->timer_period = DIV_ROUND_UP(timer_rate, HZ);
  118. }
  119. if (rps->timer_period > TIMER_MAX_VAL) {
  120. rps->timer_prescaler = TIMER_DIV256;
  121. timer_rate = clk_rate / 256;
  122. rps->timer_period = DIV_ROUND_UP(timer_rate, HZ);
  123. }
  124. rps->clkevent.name = "oxnas-rps";
  125. rps->clkevent.features = CLOCK_EVT_FEAT_PERIODIC |
  126. CLOCK_EVT_FEAT_ONESHOT |
  127. CLOCK_EVT_FEAT_DYNIRQ;
  128. rps->clkevent.tick_resume = oxnas_rps_timer_shutdown;
  129. rps->clkevent.set_state_shutdown = oxnas_rps_timer_shutdown;
  130. rps->clkevent.set_state_periodic = oxnas_rps_timer_set_periodic;
  131. rps->clkevent.set_state_oneshot = oxnas_rps_timer_set_oneshot;
  132. rps->clkevent.set_next_event = oxnas_rps_timer_next_event;
  133. rps->clkevent.rating = 200;
  134. rps->clkevent.cpumask = cpu_possible_mask;
  135. rps->clkevent.irq = rps->irq;
  136. clockevents_config_and_register(&rps->clkevent,
  137. timer_rate,
  138. 1,
  139. TIMER_MAX_VAL);
  140. pr_info("Registered clock event rate %luHz prescaler %x period %lu\n",
  141. clk_rate,
  142. rps->timer_prescaler,
  143. rps->timer_period);
  144. return 0;
  145. }
  146. /* Clocksource */
  147. static void __iomem *timer_sched_base;
  148. static u64 notrace oxnas_rps_read_sched_clock(void)
  149. {
  150. return ~readl_relaxed(timer_sched_base);
  151. }
  152. static int __init oxnas_rps_clocksource_init(struct oxnas_rps_timer *rps)
  153. {
  154. ulong clk_rate = clk_get_rate(rps->clk);
  155. int ret;
  156. /* use prescale 16 */
  157. clk_rate = clk_rate / 16;
  158. writel_relaxed(TIMER_MAX_VAL, rps->clksrc_base + TIMER_LOAD_REG);
  159. writel_relaxed(TIMER_PERIODIC | TIMER_ENABLE | TIMER_DIV16,
  160. rps->clksrc_base + TIMER_CTRL_REG);
  161. timer_sched_base = rps->clksrc_base + TIMER_CURR_REG;
  162. sched_clock_register(oxnas_rps_read_sched_clock,
  163. TIMER_BITS, clk_rate);
  164. ret = clocksource_mmio_init(timer_sched_base,
  165. "oxnas_rps_clocksource_timer",
  166. clk_rate, 250, TIMER_BITS,
  167. clocksource_mmio_readl_down);
  168. if (WARN_ON(ret)) {
  169. pr_err("can't register clocksource\n");
  170. return ret;
  171. }
  172. pr_info("Registered clocksource rate %luHz\n", clk_rate);
  173. return 0;
  174. }
  175. static int __init oxnas_rps_timer_init(struct device_node *np)
  176. {
  177. struct oxnas_rps_timer *rps;
  178. void __iomem *base;
  179. int ret;
  180. rps = kzalloc(sizeof(*rps), GFP_KERNEL);
  181. if (!rps)
  182. return -ENOMEM;
  183. rps->clk = of_clk_get(np, 0);
  184. if (IS_ERR(rps->clk)) {
  185. ret = PTR_ERR(rps->clk);
  186. goto err_alloc;
  187. }
  188. ret = clk_prepare_enable(rps->clk);
  189. if (ret)
  190. goto err_clk;
  191. base = of_iomap(np, 0);
  192. if (!base) {
  193. ret = -ENXIO;
  194. goto err_clk_prepare;
  195. }
  196. rps->irq = irq_of_parse_and_map(np, 0);
  197. if (rps->irq < 0) {
  198. ret = -EINVAL;
  199. goto err_iomap;
  200. }
  201. rps->clkevt_base = base + TIMER1_REG_OFFSET;
  202. rps->clksrc_base = base + TIMER2_REG_OFFSET;
  203. /* Disable timers */
  204. writel_relaxed(0, rps->clkevt_base + TIMER_CTRL_REG);
  205. writel_relaxed(0, rps->clksrc_base + TIMER_CTRL_REG);
  206. writel_relaxed(0, rps->clkevt_base + TIMER_LOAD_REG);
  207. writel_relaxed(0, rps->clksrc_base + TIMER_LOAD_REG);
  208. writel_relaxed(0, rps->clkevt_base + TIMER_CLRINT_REG);
  209. writel_relaxed(0, rps->clksrc_base + TIMER_CLRINT_REG);
  210. ret = request_irq(rps->irq, oxnas_rps_timer_irq,
  211. IRQF_TIMER | IRQF_IRQPOLL,
  212. "rps-timer", rps);
  213. if (ret)
  214. goto err_iomap;
  215. ret = oxnas_rps_clocksource_init(rps);
  216. if (ret)
  217. goto err_irqreq;
  218. ret = oxnas_rps_clockevent_init(rps);
  219. if (ret)
  220. goto err_irqreq;
  221. return 0;
  222. err_irqreq:
  223. free_irq(rps->irq, rps);
  224. err_iomap:
  225. iounmap(base);
  226. err_clk_prepare:
  227. clk_disable_unprepare(rps->clk);
  228. err_clk:
  229. clk_put(rps->clk);
  230. err_alloc:
  231. kfree(rps);
  232. return ret;
  233. }
  234. TIMER_OF_DECLARE(ox810se_rps,
  235. "oxsemi,ox810se-rps-timer", oxnas_rps_timer_init);
  236. TIMER_OF_DECLARE(ox820_rps,
  237. "oxsemi,ox820-rps-timer", oxnas_rps_timer_init);