timer-sp.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. * linux/arch/arm/common/timer-sp.c
  3. *
  4. * Copyright (C) 1999 - 2003 ARM Limited
  5. * Copyright (C) 2000 Deep Blue Solutions Ltd
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <linux/clk.h>
  22. #include <linux/clocksource.h>
  23. #include <linux/clockchips.h>
  24. #include <linux/err.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/irq.h>
  27. #include <linux/io.h>
  28. #include <linux/of.h>
  29. #include <linux/of_address.h>
  30. #include <linux/of_irq.h>
  31. #include <linux/sched_clock.h>
  32. #include <asm/hardware/arm_timer.h>
  33. #include <asm/hardware/timer-sp.h>
  34. static long __init sp804_get_clock_rate(struct clk *clk)
  35. {
  36. long rate;
  37. int err;
  38. err = clk_prepare(clk);
  39. if (err) {
  40. pr_err("sp804: clock failed to prepare: %d\n", err);
  41. clk_put(clk);
  42. return err;
  43. }
  44. err = clk_enable(clk);
  45. if (err) {
  46. pr_err("sp804: clock failed to enable: %d\n", err);
  47. clk_unprepare(clk);
  48. clk_put(clk);
  49. return err;
  50. }
  51. rate = clk_get_rate(clk);
  52. if (rate < 0) {
  53. pr_err("sp804: clock failed to get rate: %ld\n", rate);
  54. clk_disable(clk);
  55. clk_unprepare(clk);
  56. clk_put(clk);
  57. }
  58. return rate;
  59. }
  60. static void __iomem *sched_clock_base;
  61. static u32 sp804_read(void)
  62. {
  63. return ~readl_relaxed(sched_clock_base + TIMER_VALUE);
  64. }
  65. void __init __sp804_clocksource_and_sched_clock_init(void __iomem *base,
  66. const char *name,
  67. struct clk *clk,
  68. int use_sched_clock)
  69. {
  70. long rate;
  71. if (!clk) {
  72. clk = clk_get_sys("sp804", name);
  73. if (IS_ERR(clk)) {
  74. pr_err("sp804: clock not found: %d\n",
  75. (int)PTR_ERR(clk));
  76. return;
  77. }
  78. }
  79. rate = sp804_get_clock_rate(clk);
  80. if (rate < 0)
  81. return;
  82. /* setup timer 0 as free-running clocksource */
  83. writel(0, base + TIMER_CTRL);
  84. writel(0xffffffff, base + TIMER_LOAD);
  85. writel(0xffffffff, base + TIMER_VALUE);
  86. writel(TIMER_CTRL_32BIT | TIMER_CTRL_ENABLE | TIMER_CTRL_PERIODIC,
  87. base + TIMER_CTRL);
  88. clocksource_mmio_init(base + TIMER_VALUE, name,
  89. rate, 200, 32, clocksource_mmio_readl_down);
  90. if (use_sched_clock) {
  91. sched_clock_base = base;
  92. setup_sched_clock(sp804_read, 32, rate);
  93. }
  94. }
  95. static void __iomem *clkevt_base;
  96. static unsigned long clkevt_reload;
  97. /*
  98. * IRQ handler for the timer
  99. */
  100. static irqreturn_t sp804_timer_interrupt(int irq, void *dev_id)
  101. {
  102. struct clock_event_device *evt = dev_id;
  103. /* clear the interrupt */
  104. writel(1, clkevt_base + TIMER_INTCLR);
  105. evt->event_handler(evt);
  106. return IRQ_HANDLED;
  107. }
  108. static void sp804_set_mode(enum clock_event_mode mode,
  109. struct clock_event_device *evt)
  110. {
  111. unsigned long ctrl = TIMER_CTRL_32BIT | TIMER_CTRL_IE;
  112. writel(ctrl, clkevt_base + TIMER_CTRL);
  113. switch (mode) {
  114. case CLOCK_EVT_MODE_PERIODIC:
  115. writel(clkevt_reload, clkevt_base + TIMER_LOAD);
  116. ctrl |= TIMER_CTRL_PERIODIC | TIMER_CTRL_ENABLE;
  117. break;
  118. case CLOCK_EVT_MODE_ONESHOT:
  119. /* period set, and timer enabled in 'next_event' hook */
  120. ctrl |= TIMER_CTRL_ONESHOT;
  121. break;
  122. case CLOCK_EVT_MODE_UNUSED:
  123. case CLOCK_EVT_MODE_SHUTDOWN:
  124. default:
  125. break;
  126. }
  127. writel(ctrl, clkevt_base + TIMER_CTRL);
  128. }
  129. static int sp804_set_next_event(unsigned long next,
  130. struct clock_event_device *evt)
  131. {
  132. unsigned long ctrl = readl(clkevt_base + TIMER_CTRL);
  133. writel(next, clkevt_base + TIMER_LOAD);
  134. writel(ctrl | TIMER_CTRL_ENABLE, clkevt_base + TIMER_CTRL);
  135. return 0;
  136. }
  137. static struct clock_event_device sp804_clockevent = {
  138. .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
  139. .set_mode = sp804_set_mode,
  140. .set_next_event = sp804_set_next_event,
  141. .rating = 300,
  142. };
  143. static struct irqaction sp804_timer_irq = {
  144. .name = "timer",
  145. .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
  146. .handler = sp804_timer_interrupt,
  147. .dev_id = &sp804_clockevent,
  148. };
  149. void __init __sp804_clockevents_init(void __iomem *base, unsigned int irq, struct clk *clk, const char *name)
  150. {
  151. struct clock_event_device *evt = &sp804_clockevent;
  152. long rate;
  153. if (!clk)
  154. clk = clk_get_sys("sp804", name);
  155. if (IS_ERR(clk)) {
  156. pr_err("sp804: %s clock not found: %d\n", name,
  157. (int)PTR_ERR(clk));
  158. return;
  159. }
  160. rate = sp804_get_clock_rate(clk);
  161. if (rate < 0)
  162. return;
  163. clkevt_base = base;
  164. clkevt_reload = DIV_ROUND_CLOSEST(rate, HZ);
  165. evt->name = name;
  166. evt->irq = irq;
  167. evt->cpumask = cpu_possible_mask;
  168. writel(0, base + TIMER_CTRL);
  169. setup_irq(irq, &sp804_timer_irq);
  170. clockevents_config_and_register(evt, rate, 0xf, 0xffffffff);
  171. }
  172. static void __init sp804_of_init(struct device_node *np)
  173. {
  174. static bool initialized = false;
  175. void __iomem *base;
  176. int irq;
  177. u32 irq_num = 0;
  178. struct clk *clk1, *clk2;
  179. const char *name = of_get_property(np, "compatible", NULL);
  180. base = of_iomap(np, 0);
  181. if (WARN_ON(!base))
  182. return;
  183. /* Ensure timers are disabled */
  184. writel(0, base + TIMER_CTRL);
  185. writel(0, base + TIMER_2_BASE + TIMER_CTRL);
  186. if (initialized || !of_device_is_available(np))
  187. goto err;
  188. clk1 = of_clk_get(np, 0);
  189. if (IS_ERR(clk1))
  190. clk1 = NULL;
  191. /* Get the 2nd clock if the timer has 2 timer clocks */
  192. if (of_count_phandle_with_args(np, "clocks", "#clock-cells") == 3) {
  193. clk2 = of_clk_get(np, 1);
  194. if (IS_ERR(clk2)) {
  195. pr_err("sp804: %s clock not found: %d\n", np->name,
  196. (int)PTR_ERR(clk2));
  197. goto err;
  198. }
  199. } else
  200. clk2 = clk1;
  201. irq = irq_of_parse_and_map(np, 0);
  202. if (irq <= 0)
  203. goto err;
  204. of_property_read_u32(np, "arm,sp804-has-irq", &irq_num);
  205. if (irq_num == 2) {
  206. __sp804_clockevents_init(base + TIMER_2_BASE, irq, clk2, name);
  207. __sp804_clocksource_and_sched_clock_init(base, name, clk1, 1);
  208. } else {
  209. __sp804_clockevents_init(base, irq, clk1 , name);
  210. __sp804_clocksource_and_sched_clock_init(base + TIMER_2_BASE,
  211. name, clk2, 1);
  212. }
  213. initialized = true;
  214. return;
  215. err:
  216. iounmap(base);
  217. }
  218. CLOCKSOURCE_OF_DECLARE(sp804, "arm,sp804", sp804_of_init);
  219. static void __init integrator_cp_of_init(struct device_node *np)
  220. {
  221. static int init_count = 0;
  222. void __iomem *base;
  223. int irq;
  224. const char *name = of_get_property(np, "compatible", NULL);
  225. base = of_iomap(np, 0);
  226. if (WARN_ON(!base))
  227. return;
  228. /* Ensure timer is disabled */
  229. writel(0, base + TIMER_CTRL);
  230. if (init_count == 2 || !of_device_is_available(np))
  231. goto err;
  232. if (!init_count)
  233. sp804_clocksource_init(base, name);
  234. else {
  235. irq = irq_of_parse_and_map(np, 0);
  236. if (irq <= 0)
  237. goto err;
  238. sp804_clockevents_init(base, irq, name);
  239. }
  240. init_count++;
  241. return;
  242. err:
  243. iounmap(base);
  244. }
  245. CLOCKSOURCE_OF_DECLARE(intcp, "arm,integrator-cp-timer", integrator_cp_of_init);