dw_apb_timer.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /*
  2. * (C) Copyright 2009 Intel Corporation
  3. * Author: Jacob Pan (jacob.jun.pan@intel.com)
  4. *
  5. * Shared with ARM platforms, Jamie Iles, Picochip 2011
  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 version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * Support for the Synopsys DesignWare APB Timers.
  12. */
  13. #include <linux/dw_apb_timer.h>
  14. #include <linux/delay.h>
  15. #include <linux/kernel.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/irq.h>
  18. #include <linux/io.h>
  19. #include <linux/slab.h>
  20. #define APBT_MIN_PERIOD 4
  21. #define APBT_MIN_DELTA_USEC 200
  22. #define APBTMR_N_LOAD_COUNT 0x00
  23. #define APBTMR_N_CURRENT_VALUE 0x04
  24. #define APBTMR_N_CONTROL 0x08
  25. #define APBTMR_N_EOI 0x0c
  26. #define APBTMR_N_INT_STATUS 0x10
  27. #define APBTMRS_INT_STATUS 0xa0
  28. #define APBTMRS_EOI 0xa4
  29. #define APBTMRS_RAW_INT_STATUS 0xa8
  30. #define APBTMRS_COMP_VERSION 0xac
  31. #define APBTMR_CONTROL_ENABLE (1 << 0)
  32. /* 1: periodic, 0:free running. */
  33. #define APBTMR_CONTROL_MODE_PERIODIC (1 << 1)
  34. #define APBTMR_CONTROL_INT (1 << 2)
  35. static inline struct dw_apb_clock_event_device *
  36. ced_to_dw_apb_ced(struct clock_event_device *evt)
  37. {
  38. return container_of(evt, struct dw_apb_clock_event_device, ced);
  39. }
  40. static inline struct dw_apb_clocksource *
  41. clocksource_to_dw_apb_clocksource(struct clocksource *cs)
  42. {
  43. return container_of(cs, struct dw_apb_clocksource, cs);
  44. }
  45. static unsigned long apbt_readl(struct dw_apb_timer *timer, unsigned long offs)
  46. {
  47. return readl(timer->base + offs);
  48. }
  49. static void apbt_writel(struct dw_apb_timer *timer, unsigned long val,
  50. unsigned long offs)
  51. {
  52. writel(val, timer->base + offs);
  53. }
  54. static void apbt_disable_int(struct dw_apb_timer *timer)
  55. {
  56. unsigned long ctrl = apbt_readl(timer, APBTMR_N_CONTROL);
  57. ctrl |= APBTMR_CONTROL_INT;
  58. apbt_writel(timer, ctrl, APBTMR_N_CONTROL);
  59. }
  60. /**
  61. * dw_apb_clockevent_pause() - stop the clock_event_device from running
  62. *
  63. * @dw_ced: The APB clock to stop generating events.
  64. */
  65. void dw_apb_clockevent_pause(struct dw_apb_clock_event_device *dw_ced)
  66. {
  67. disable_irq(dw_ced->timer.irq);
  68. apbt_disable_int(&dw_ced->timer);
  69. }
  70. static void apbt_eoi(struct dw_apb_timer *timer)
  71. {
  72. apbt_readl(timer, APBTMR_N_EOI);
  73. }
  74. static irqreturn_t dw_apb_clockevent_irq(int irq, void *data)
  75. {
  76. struct clock_event_device *evt = data;
  77. struct dw_apb_clock_event_device *dw_ced = ced_to_dw_apb_ced(evt);
  78. if (!evt->event_handler) {
  79. pr_info("Spurious APBT timer interrupt %d", irq);
  80. return IRQ_NONE;
  81. }
  82. if (dw_ced->eoi)
  83. dw_ced->eoi(&dw_ced->timer);
  84. evt->event_handler(evt);
  85. return IRQ_HANDLED;
  86. }
  87. static void apbt_enable_int(struct dw_apb_timer *timer)
  88. {
  89. unsigned long ctrl = apbt_readl(timer, APBTMR_N_CONTROL);
  90. /* clear pending intr */
  91. apbt_readl(timer, APBTMR_N_EOI);
  92. ctrl &= ~APBTMR_CONTROL_INT;
  93. apbt_writel(timer, ctrl, APBTMR_N_CONTROL);
  94. }
  95. static void apbt_set_mode(enum clock_event_mode mode,
  96. struct clock_event_device *evt)
  97. {
  98. unsigned long ctrl;
  99. unsigned long period;
  100. struct dw_apb_clock_event_device *dw_ced = ced_to_dw_apb_ced(evt);
  101. pr_debug("%s CPU %d mode=%d\n", __func__, first_cpu(*evt->cpumask),
  102. mode);
  103. switch (mode) {
  104. case CLOCK_EVT_MODE_PERIODIC:
  105. period = DIV_ROUND_UP(dw_ced->timer.freq, HZ);
  106. ctrl = apbt_readl(&dw_ced->timer, APBTMR_N_CONTROL);
  107. ctrl |= APBTMR_CONTROL_MODE_PERIODIC;
  108. apbt_writel(&dw_ced->timer, ctrl, APBTMR_N_CONTROL);
  109. /*
  110. * DW APB p. 46, have to disable timer before load counter,
  111. * may cause sync problem.
  112. */
  113. ctrl &= ~APBTMR_CONTROL_ENABLE;
  114. apbt_writel(&dw_ced->timer, ctrl, APBTMR_N_CONTROL);
  115. udelay(1);
  116. pr_debug("Setting clock period %lu for HZ %d\n", period, HZ);
  117. apbt_writel(&dw_ced->timer, period, APBTMR_N_LOAD_COUNT);
  118. ctrl |= APBTMR_CONTROL_ENABLE;
  119. apbt_writel(&dw_ced->timer, ctrl, APBTMR_N_CONTROL);
  120. break;
  121. case CLOCK_EVT_MODE_ONESHOT:
  122. ctrl = apbt_readl(&dw_ced->timer, APBTMR_N_CONTROL);
  123. /*
  124. * set free running mode, this mode will let timer reload max
  125. * timeout which will give time (3min on 25MHz clock) to rearm
  126. * the next event, therefore emulate the one-shot mode.
  127. */
  128. ctrl &= ~APBTMR_CONTROL_ENABLE;
  129. ctrl &= ~APBTMR_CONTROL_MODE_PERIODIC;
  130. apbt_writel(&dw_ced->timer, ctrl, APBTMR_N_CONTROL);
  131. /* write again to set free running mode */
  132. apbt_writel(&dw_ced->timer, ctrl, APBTMR_N_CONTROL);
  133. /*
  134. * DW APB p. 46, load counter with all 1s before starting free
  135. * running mode.
  136. */
  137. apbt_writel(&dw_ced->timer, ~0, APBTMR_N_LOAD_COUNT);
  138. ctrl &= ~APBTMR_CONTROL_INT;
  139. ctrl |= APBTMR_CONTROL_ENABLE;
  140. apbt_writel(&dw_ced->timer, ctrl, APBTMR_N_CONTROL);
  141. break;
  142. case CLOCK_EVT_MODE_UNUSED:
  143. case CLOCK_EVT_MODE_SHUTDOWN:
  144. ctrl = apbt_readl(&dw_ced->timer, APBTMR_N_CONTROL);
  145. ctrl &= ~APBTMR_CONTROL_ENABLE;
  146. apbt_writel(&dw_ced->timer, ctrl, APBTMR_N_CONTROL);
  147. break;
  148. case CLOCK_EVT_MODE_RESUME:
  149. apbt_enable_int(&dw_ced->timer);
  150. break;
  151. }
  152. }
  153. static int apbt_next_event(unsigned long delta,
  154. struct clock_event_device *evt)
  155. {
  156. unsigned long ctrl;
  157. struct dw_apb_clock_event_device *dw_ced = ced_to_dw_apb_ced(evt);
  158. /* Disable timer */
  159. ctrl = apbt_readl(&dw_ced->timer, APBTMR_N_CONTROL);
  160. ctrl &= ~APBTMR_CONTROL_ENABLE;
  161. apbt_writel(&dw_ced->timer, ctrl, APBTMR_N_CONTROL);
  162. /* write new count */
  163. apbt_writel(&dw_ced->timer, delta, APBTMR_N_LOAD_COUNT);
  164. ctrl |= APBTMR_CONTROL_ENABLE;
  165. apbt_writel(&dw_ced->timer, ctrl, APBTMR_N_CONTROL);
  166. return 0;
  167. }
  168. /**
  169. * dw_apb_clockevent_init() - use an APB timer as a clock_event_device
  170. *
  171. * @cpu: The CPU the events will be targeted at.
  172. * @name: The name used for the timer and the IRQ for it.
  173. * @rating: The rating to give the timer.
  174. * @base: I/O base for the timer registers.
  175. * @irq: The interrupt number to use for the timer.
  176. * @freq: The frequency that the timer counts at.
  177. *
  178. * This creates a clock_event_device for using with the generic clock layer
  179. * but does not start and register it. This should be done with
  180. * dw_apb_clockevent_register() as the next step. If this is the first time
  181. * it has been called for a timer then the IRQ will be requested, if not it
  182. * just be enabled to allow CPU hotplug to avoid repeatedly requesting and
  183. * releasing the IRQ.
  184. */
  185. struct dw_apb_clock_event_device *
  186. dw_apb_clockevent_init(int cpu, const char *name, unsigned rating,
  187. void __iomem *base, int irq, unsigned long freq)
  188. {
  189. struct dw_apb_clock_event_device *dw_ced =
  190. kzalloc(sizeof(*dw_ced), GFP_KERNEL);
  191. int err;
  192. if (!dw_ced)
  193. return NULL;
  194. dw_ced->timer.base = base;
  195. dw_ced->timer.irq = irq;
  196. dw_ced->timer.freq = freq;
  197. clockevents_calc_mult_shift(&dw_ced->ced, freq, APBT_MIN_PERIOD);
  198. dw_ced->ced.max_delta_ns = clockevent_delta2ns(0x7fffffff,
  199. &dw_ced->ced);
  200. dw_ced->ced.min_delta_ns = clockevent_delta2ns(5000, &dw_ced->ced);
  201. dw_ced->ced.cpumask = cpumask_of(cpu);
  202. dw_ced->ced.features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT;
  203. dw_ced->ced.set_mode = apbt_set_mode;
  204. dw_ced->ced.set_next_event = apbt_next_event;
  205. dw_ced->ced.irq = dw_ced->timer.irq;
  206. dw_ced->ced.rating = rating;
  207. dw_ced->ced.name = name;
  208. dw_ced->irqaction.name = dw_ced->ced.name;
  209. dw_ced->irqaction.handler = dw_apb_clockevent_irq;
  210. dw_ced->irqaction.dev_id = &dw_ced->ced;
  211. dw_ced->irqaction.irq = irq;
  212. dw_ced->irqaction.flags = IRQF_TIMER | IRQF_IRQPOLL |
  213. IRQF_NOBALANCING |
  214. IRQF_DISABLED;
  215. dw_ced->eoi = apbt_eoi;
  216. err = setup_irq(irq, &dw_ced->irqaction);
  217. if (err) {
  218. pr_err("failed to request timer irq\n");
  219. kfree(dw_ced);
  220. dw_ced = NULL;
  221. }
  222. return dw_ced;
  223. }
  224. /**
  225. * dw_apb_clockevent_resume() - resume a clock that has been paused.
  226. *
  227. * @dw_ced: The APB clock to resume.
  228. */
  229. void dw_apb_clockevent_resume(struct dw_apb_clock_event_device *dw_ced)
  230. {
  231. enable_irq(dw_ced->timer.irq);
  232. }
  233. /**
  234. * dw_apb_clockevent_stop() - stop the clock_event_device and release the IRQ.
  235. *
  236. * @dw_ced: The APB clock to stop generating the events.
  237. */
  238. void dw_apb_clockevent_stop(struct dw_apb_clock_event_device *dw_ced)
  239. {
  240. free_irq(dw_ced->timer.irq, &dw_ced->ced);
  241. }
  242. /**
  243. * dw_apb_clockevent_register() - register the clock with the generic layer
  244. *
  245. * @dw_ced: The APB clock to register as a clock_event_device.
  246. */
  247. void dw_apb_clockevent_register(struct dw_apb_clock_event_device *dw_ced)
  248. {
  249. apbt_writel(&dw_ced->timer, 0, APBTMR_N_CONTROL);
  250. clockevents_register_device(&dw_ced->ced);
  251. apbt_enable_int(&dw_ced->timer);
  252. }
  253. /**
  254. * dw_apb_clocksource_start() - start the clocksource counting.
  255. *
  256. * @dw_cs: The clocksource to start.
  257. *
  258. * This is used to start the clocksource before registration and can be used
  259. * to enable calibration of timers.
  260. */
  261. void dw_apb_clocksource_start(struct dw_apb_clocksource *dw_cs)
  262. {
  263. /*
  264. * start count down from 0xffff_ffff. this is done by toggling the
  265. * enable bit then load initial load count to ~0.
  266. */
  267. unsigned long ctrl = apbt_readl(&dw_cs->timer, APBTMR_N_CONTROL);
  268. ctrl &= ~APBTMR_CONTROL_ENABLE;
  269. apbt_writel(&dw_cs->timer, ctrl, APBTMR_N_CONTROL);
  270. apbt_writel(&dw_cs->timer, ~0, APBTMR_N_LOAD_COUNT);
  271. /* enable, mask interrupt */
  272. ctrl &= ~APBTMR_CONTROL_MODE_PERIODIC;
  273. ctrl |= (APBTMR_CONTROL_ENABLE | APBTMR_CONTROL_INT);
  274. apbt_writel(&dw_cs->timer, ctrl, APBTMR_N_CONTROL);
  275. /* read it once to get cached counter value initialized */
  276. dw_apb_clocksource_read(dw_cs);
  277. }
  278. static cycle_t __apbt_read_clocksource(struct clocksource *cs)
  279. {
  280. unsigned long current_count;
  281. struct dw_apb_clocksource *dw_cs =
  282. clocksource_to_dw_apb_clocksource(cs);
  283. current_count = apbt_readl(&dw_cs->timer, APBTMR_N_CURRENT_VALUE);
  284. return (cycle_t)~current_count;
  285. }
  286. static void apbt_restart_clocksource(struct clocksource *cs)
  287. {
  288. struct dw_apb_clocksource *dw_cs =
  289. clocksource_to_dw_apb_clocksource(cs);
  290. dw_apb_clocksource_start(dw_cs);
  291. }
  292. /**
  293. * dw_apb_clocksource_init() - use an APB timer as a clocksource.
  294. *
  295. * @rating: The rating to give the clocksource.
  296. * @name: The name for the clocksource.
  297. * @base: The I/O base for the timer registers.
  298. * @freq: The frequency that the timer counts at.
  299. *
  300. * This creates a clocksource using an APB timer but does not yet register it
  301. * with the clocksource system. This should be done with
  302. * dw_apb_clocksource_register() as the next step.
  303. */
  304. struct dw_apb_clocksource *
  305. dw_apb_clocksource_init(unsigned rating, const char *name, void __iomem *base,
  306. unsigned long freq)
  307. {
  308. struct dw_apb_clocksource *dw_cs = kzalloc(sizeof(*dw_cs), GFP_KERNEL);
  309. if (!dw_cs)
  310. return NULL;
  311. dw_cs->timer.base = base;
  312. dw_cs->timer.freq = freq;
  313. dw_cs->cs.name = name;
  314. dw_cs->cs.rating = rating;
  315. dw_cs->cs.read = __apbt_read_clocksource;
  316. dw_cs->cs.mask = CLOCKSOURCE_MASK(32);
  317. dw_cs->cs.flags = CLOCK_SOURCE_IS_CONTINUOUS;
  318. dw_cs->cs.resume = apbt_restart_clocksource;
  319. return dw_cs;
  320. }
  321. /**
  322. * dw_apb_clocksource_register() - register the APB clocksource.
  323. *
  324. * @dw_cs: The clocksource to register.
  325. */
  326. void dw_apb_clocksource_register(struct dw_apb_clocksource *dw_cs)
  327. {
  328. clocksource_register_hz(&dw_cs->cs, dw_cs->timer.freq);
  329. }
  330. /**
  331. * dw_apb_clocksource_read() - read the current value of a clocksource.
  332. *
  333. * @dw_cs: The clocksource to read.
  334. */
  335. cycle_t dw_apb_clocksource_read(struct dw_apb_clocksource *dw_cs)
  336. {
  337. return (cycle_t)~apbt_readl(&dw_cs->timer, APBTMR_N_CURRENT_VALUE);
  338. }
  339. /**
  340. * dw_apb_clocksource_unregister() - unregister and free a clocksource.
  341. *
  342. * @dw_cs: The clocksource to unregister/free.
  343. */
  344. void dw_apb_clocksource_unregister(struct dw_apb_clocksource *dw_cs)
  345. {
  346. clocksource_unregister(&dw_cs->cs);
  347. kfree(dw_cs);
  348. }