dw_apb_timer.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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 inline u32 apbt_readl(struct dw_apb_timer *timer, unsigned long offs)
  46. {
  47. return readl(timer->base + offs);
  48. }
  49. static inline void apbt_writel(struct dw_apb_timer *timer, u32 val,
  50. unsigned long offs)
  51. {
  52. writel(val, timer->base + offs);
  53. }
  54. static inline u32 apbt_readl_relaxed(struct dw_apb_timer *timer, unsigned long offs)
  55. {
  56. return readl_relaxed(timer->base + offs);
  57. }
  58. static inline void apbt_writel_relaxed(struct dw_apb_timer *timer, u32 val,
  59. unsigned long offs)
  60. {
  61. writel_relaxed(val, timer->base + offs);
  62. }
  63. static void apbt_disable_int(struct dw_apb_timer *timer)
  64. {
  65. u32 ctrl = apbt_readl(timer, APBTMR_N_CONTROL);
  66. ctrl |= APBTMR_CONTROL_INT;
  67. apbt_writel(timer, ctrl, APBTMR_N_CONTROL);
  68. }
  69. /**
  70. * dw_apb_clockevent_pause() - stop the clock_event_device from running
  71. *
  72. * @dw_ced: The APB clock to stop generating events.
  73. */
  74. void dw_apb_clockevent_pause(struct dw_apb_clock_event_device *dw_ced)
  75. {
  76. disable_irq(dw_ced->timer.irq);
  77. apbt_disable_int(&dw_ced->timer);
  78. }
  79. static void apbt_eoi(struct dw_apb_timer *timer)
  80. {
  81. apbt_readl_relaxed(timer, APBTMR_N_EOI);
  82. }
  83. static irqreturn_t dw_apb_clockevent_irq(int irq, void *data)
  84. {
  85. struct clock_event_device *evt = data;
  86. struct dw_apb_clock_event_device *dw_ced = ced_to_dw_apb_ced(evt);
  87. if (!evt->event_handler) {
  88. pr_info("Spurious APBT timer interrupt %d\n", irq);
  89. return IRQ_NONE;
  90. }
  91. if (dw_ced->eoi)
  92. dw_ced->eoi(&dw_ced->timer);
  93. evt->event_handler(evt);
  94. return IRQ_HANDLED;
  95. }
  96. static void apbt_enable_int(struct dw_apb_timer *timer)
  97. {
  98. u32 ctrl = apbt_readl(timer, APBTMR_N_CONTROL);
  99. /* clear pending intr */
  100. apbt_readl(timer, APBTMR_N_EOI);
  101. ctrl &= ~APBTMR_CONTROL_INT;
  102. apbt_writel(timer, ctrl, APBTMR_N_CONTROL);
  103. }
  104. static int apbt_shutdown(struct clock_event_device *evt)
  105. {
  106. struct dw_apb_clock_event_device *dw_ced = ced_to_dw_apb_ced(evt);
  107. u32 ctrl;
  108. pr_debug("%s CPU %d state=shutdown\n", __func__,
  109. cpumask_first(evt->cpumask));
  110. ctrl = apbt_readl(&dw_ced->timer, APBTMR_N_CONTROL);
  111. ctrl &= ~APBTMR_CONTROL_ENABLE;
  112. apbt_writel(&dw_ced->timer, ctrl, APBTMR_N_CONTROL);
  113. return 0;
  114. }
  115. static int apbt_set_oneshot(struct clock_event_device *evt)
  116. {
  117. struct dw_apb_clock_event_device *dw_ced = ced_to_dw_apb_ced(evt);
  118. u32 ctrl;
  119. pr_debug("%s CPU %d state=oneshot\n", __func__,
  120. cpumask_first(evt->cpumask));
  121. ctrl = apbt_readl(&dw_ced->timer, APBTMR_N_CONTROL);
  122. /*
  123. * set free running mode, this mode will let timer reload max
  124. * timeout which will give time (3min on 25MHz clock) to rearm
  125. * the next event, therefore emulate the one-shot mode.
  126. */
  127. ctrl &= ~APBTMR_CONTROL_ENABLE;
  128. ctrl &= ~APBTMR_CONTROL_MODE_PERIODIC;
  129. apbt_writel(&dw_ced->timer, ctrl, APBTMR_N_CONTROL);
  130. /* write again to set free running mode */
  131. apbt_writel(&dw_ced->timer, ctrl, APBTMR_N_CONTROL);
  132. /*
  133. * DW APB p. 46, load counter with all 1s before starting free
  134. * running mode.
  135. */
  136. apbt_writel(&dw_ced->timer, ~0, APBTMR_N_LOAD_COUNT);
  137. ctrl &= ~APBTMR_CONTROL_INT;
  138. ctrl |= APBTMR_CONTROL_ENABLE;
  139. apbt_writel(&dw_ced->timer, ctrl, APBTMR_N_CONTROL);
  140. return 0;
  141. }
  142. static int apbt_set_periodic(struct clock_event_device *evt)
  143. {
  144. struct dw_apb_clock_event_device *dw_ced = ced_to_dw_apb_ced(evt);
  145. unsigned long period = DIV_ROUND_UP(dw_ced->timer.freq, HZ);
  146. u32 ctrl;
  147. pr_debug("%s CPU %d state=periodic\n", __func__,
  148. cpumask_first(evt->cpumask));
  149. ctrl = apbt_readl(&dw_ced->timer, APBTMR_N_CONTROL);
  150. ctrl |= APBTMR_CONTROL_MODE_PERIODIC;
  151. apbt_writel(&dw_ced->timer, ctrl, APBTMR_N_CONTROL);
  152. /*
  153. * DW APB p. 46, have to disable timer before load counter,
  154. * may cause sync problem.
  155. */
  156. ctrl &= ~APBTMR_CONTROL_ENABLE;
  157. apbt_writel(&dw_ced->timer, ctrl, APBTMR_N_CONTROL);
  158. udelay(1);
  159. pr_debug("Setting clock period %lu for HZ %d\n", period, HZ);
  160. apbt_writel(&dw_ced->timer, period, APBTMR_N_LOAD_COUNT);
  161. ctrl |= APBTMR_CONTROL_ENABLE;
  162. apbt_writel(&dw_ced->timer, ctrl, APBTMR_N_CONTROL);
  163. return 0;
  164. }
  165. static int apbt_resume(struct clock_event_device *evt)
  166. {
  167. struct dw_apb_clock_event_device *dw_ced = ced_to_dw_apb_ced(evt);
  168. pr_debug("%s CPU %d state=resume\n", __func__,
  169. cpumask_first(evt->cpumask));
  170. apbt_enable_int(&dw_ced->timer);
  171. return 0;
  172. }
  173. static int apbt_next_event(unsigned long delta,
  174. struct clock_event_device *evt)
  175. {
  176. u32 ctrl;
  177. struct dw_apb_clock_event_device *dw_ced = ced_to_dw_apb_ced(evt);
  178. /* Disable timer */
  179. ctrl = apbt_readl_relaxed(&dw_ced->timer, APBTMR_N_CONTROL);
  180. ctrl &= ~APBTMR_CONTROL_ENABLE;
  181. apbt_writel_relaxed(&dw_ced->timer, ctrl, APBTMR_N_CONTROL);
  182. /* write new count */
  183. apbt_writel_relaxed(&dw_ced->timer, delta, APBTMR_N_LOAD_COUNT);
  184. ctrl |= APBTMR_CONTROL_ENABLE;
  185. apbt_writel_relaxed(&dw_ced->timer, ctrl, APBTMR_N_CONTROL);
  186. return 0;
  187. }
  188. /**
  189. * dw_apb_clockevent_init() - use an APB timer as a clock_event_device
  190. *
  191. * @cpu: The CPU the events will be targeted at.
  192. * @name: The name used for the timer and the IRQ for it.
  193. * @rating: The rating to give the timer.
  194. * @base: I/O base for the timer registers.
  195. * @irq: The interrupt number to use for the timer.
  196. * @freq: The frequency that the timer counts at.
  197. *
  198. * This creates a clock_event_device for using with the generic clock layer
  199. * but does not start and register it. This should be done with
  200. * dw_apb_clockevent_register() as the next step. If this is the first time
  201. * it has been called for a timer then the IRQ will be requested, if not it
  202. * just be enabled to allow CPU hotplug to avoid repeatedly requesting and
  203. * releasing the IRQ.
  204. */
  205. struct dw_apb_clock_event_device *
  206. dw_apb_clockevent_init(int cpu, const char *name, unsigned rating,
  207. void __iomem *base, int irq, unsigned long freq)
  208. {
  209. struct dw_apb_clock_event_device *dw_ced =
  210. kzalloc(sizeof(*dw_ced), GFP_KERNEL);
  211. int err;
  212. if (!dw_ced)
  213. return NULL;
  214. dw_ced->timer.base = base;
  215. dw_ced->timer.irq = irq;
  216. dw_ced->timer.freq = freq;
  217. clockevents_calc_mult_shift(&dw_ced->ced, freq, APBT_MIN_PERIOD);
  218. dw_ced->ced.max_delta_ns = clockevent_delta2ns(0x7fffffff,
  219. &dw_ced->ced);
  220. dw_ced->ced.max_delta_ticks = 0x7fffffff;
  221. dw_ced->ced.min_delta_ns = clockevent_delta2ns(5000, &dw_ced->ced);
  222. dw_ced->ced.min_delta_ticks = 5000;
  223. dw_ced->ced.cpumask = cpumask_of(cpu);
  224. dw_ced->ced.features = CLOCK_EVT_FEAT_PERIODIC |
  225. CLOCK_EVT_FEAT_ONESHOT | CLOCK_EVT_FEAT_DYNIRQ;
  226. dw_ced->ced.set_state_shutdown = apbt_shutdown;
  227. dw_ced->ced.set_state_periodic = apbt_set_periodic;
  228. dw_ced->ced.set_state_oneshot = apbt_set_oneshot;
  229. dw_ced->ced.set_state_oneshot_stopped = apbt_shutdown;
  230. dw_ced->ced.tick_resume = apbt_resume;
  231. dw_ced->ced.set_next_event = apbt_next_event;
  232. dw_ced->ced.irq = dw_ced->timer.irq;
  233. dw_ced->ced.rating = rating;
  234. dw_ced->ced.name = name;
  235. dw_ced->irqaction.name = dw_ced->ced.name;
  236. dw_ced->irqaction.handler = dw_apb_clockevent_irq;
  237. dw_ced->irqaction.dev_id = &dw_ced->ced;
  238. dw_ced->irqaction.irq = irq;
  239. dw_ced->irqaction.flags = IRQF_TIMER | IRQF_IRQPOLL |
  240. IRQF_NOBALANCING;
  241. dw_ced->eoi = apbt_eoi;
  242. err = setup_irq(irq, &dw_ced->irqaction);
  243. if (err) {
  244. pr_err("failed to request timer irq\n");
  245. kfree(dw_ced);
  246. dw_ced = NULL;
  247. }
  248. return dw_ced;
  249. }
  250. /**
  251. * dw_apb_clockevent_resume() - resume a clock that has been paused.
  252. *
  253. * @dw_ced: The APB clock to resume.
  254. */
  255. void dw_apb_clockevent_resume(struct dw_apb_clock_event_device *dw_ced)
  256. {
  257. enable_irq(dw_ced->timer.irq);
  258. }
  259. /**
  260. * dw_apb_clockevent_stop() - stop the clock_event_device and release the IRQ.
  261. *
  262. * @dw_ced: The APB clock to stop generating the events.
  263. */
  264. void dw_apb_clockevent_stop(struct dw_apb_clock_event_device *dw_ced)
  265. {
  266. free_irq(dw_ced->timer.irq, &dw_ced->ced);
  267. }
  268. /**
  269. * dw_apb_clockevent_register() - register the clock with the generic layer
  270. *
  271. * @dw_ced: The APB clock to register as a clock_event_device.
  272. */
  273. void dw_apb_clockevent_register(struct dw_apb_clock_event_device *dw_ced)
  274. {
  275. apbt_writel(&dw_ced->timer, 0, APBTMR_N_CONTROL);
  276. clockevents_register_device(&dw_ced->ced);
  277. apbt_enable_int(&dw_ced->timer);
  278. }
  279. /**
  280. * dw_apb_clocksource_start() - start the clocksource counting.
  281. *
  282. * @dw_cs: The clocksource to start.
  283. *
  284. * This is used to start the clocksource before registration and can be used
  285. * to enable calibration of timers.
  286. */
  287. void dw_apb_clocksource_start(struct dw_apb_clocksource *dw_cs)
  288. {
  289. /*
  290. * start count down from 0xffff_ffff. this is done by toggling the
  291. * enable bit then load initial load count to ~0.
  292. */
  293. u32 ctrl = apbt_readl(&dw_cs->timer, APBTMR_N_CONTROL);
  294. ctrl &= ~APBTMR_CONTROL_ENABLE;
  295. apbt_writel(&dw_cs->timer, ctrl, APBTMR_N_CONTROL);
  296. apbt_writel(&dw_cs->timer, ~0, APBTMR_N_LOAD_COUNT);
  297. /* enable, mask interrupt */
  298. ctrl &= ~APBTMR_CONTROL_MODE_PERIODIC;
  299. ctrl |= (APBTMR_CONTROL_ENABLE | APBTMR_CONTROL_INT);
  300. apbt_writel(&dw_cs->timer, ctrl, APBTMR_N_CONTROL);
  301. /* read it once to get cached counter value initialized */
  302. dw_apb_clocksource_read(dw_cs);
  303. }
  304. static u64 __apbt_read_clocksource(struct clocksource *cs)
  305. {
  306. u32 current_count;
  307. struct dw_apb_clocksource *dw_cs =
  308. clocksource_to_dw_apb_clocksource(cs);
  309. current_count = apbt_readl_relaxed(&dw_cs->timer,
  310. APBTMR_N_CURRENT_VALUE);
  311. return (u64)~current_count;
  312. }
  313. static void apbt_restart_clocksource(struct clocksource *cs)
  314. {
  315. struct dw_apb_clocksource *dw_cs =
  316. clocksource_to_dw_apb_clocksource(cs);
  317. dw_apb_clocksource_start(dw_cs);
  318. }
  319. /**
  320. * dw_apb_clocksource_init() - use an APB timer as a clocksource.
  321. *
  322. * @rating: The rating to give the clocksource.
  323. * @name: The name for the clocksource.
  324. * @base: The I/O base for the timer registers.
  325. * @freq: The frequency that the timer counts at.
  326. *
  327. * This creates a clocksource using an APB timer but does not yet register it
  328. * with the clocksource system. This should be done with
  329. * dw_apb_clocksource_register() as the next step.
  330. */
  331. struct dw_apb_clocksource *
  332. dw_apb_clocksource_init(unsigned rating, const char *name, void __iomem *base,
  333. unsigned long freq)
  334. {
  335. struct dw_apb_clocksource *dw_cs = kzalloc(sizeof(*dw_cs), GFP_KERNEL);
  336. if (!dw_cs)
  337. return NULL;
  338. dw_cs->timer.base = base;
  339. dw_cs->timer.freq = freq;
  340. dw_cs->cs.name = name;
  341. dw_cs->cs.rating = rating;
  342. dw_cs->cs.read = __apbt_read_clocksource;
  343. dw_cs->cs.mask = CLOCKSOURCE_MASK(32);
  344. dw_cs->cs.flags = CLOCK_SOURCE_IS_CONTINUOUS;
  345. dw_cs->cs.resume = apbt_restart_clocksource;
  346. return dw_cs;
  347. }
  348. /**
  349. * dw_apb_clocksource_register() - register the APB clocksource.
  350. *
  351. * @dw_cs: The clocksource to register.
  352. */
  353. void dw_apb_clocksource_register(struct dw_apb_clocksource *dw_cs)
  354. {
  355. clocksource_register_hz(&dw_cs->cs, dw_cs->timer.freq);
  356. }
  357. /**
  358. * dw_apb_clocksource_read() - read the current value of a clocksource.
  359. *
  360. * @dw_cs: The clocksource to read.
  361. */
  362. u64 dw_apb_clocksource_read(struct dw_apb_clocksource *dw_cs)
  363. {
  364. return (u64)~apbt_readl(&dw_cs->timer, APBTMR_N_CURRENT_VALUE);
  365. }