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 or -1 if CPU affiliation
  192. * isn't required.
  193. * @name: The name used for the timer and the IRQ for it.
  194. * @rating: The rating to give the timer.
  195. * @base: I/O base for the timer registers.
  196. * @irq: The interrupt number to use for the timer.
  197. * @freq: The frequency that the timer counts at.
  198. *
  199. * This creates a clock_event_device for using with the generic clock layer
  200. * but does not start and register it. This should be done with
  201. * dw_apb_clockevent_register() as the next step. If this is the first time
  202. * it has been called for a timer then the IRQ will be requested, if not it
  203. * just be enabled to allow CPU hotplug to avoid repeatedly requesting and
  204. * releasing the IRQ.
  205. */
  206. struct dw_apb_clock_event_device *
  207. dw_apb_clockevent_init(int cpu, const char *name, unsigned rating,
  208. void __iomem *base, int irq, unsigned long freq)
  209. {
  210. struct dw_apb_clock_event_device *dw_ced =
  211. kzalloc(sizeof(*dw_ced), GFP_KERNEL);
  212. int err;
  213. if (!dw_ced)
  214. return NULL;
  215. dw_ced->timer.base = base;
  216. dw_ced->timer.irq = irq;
  217. dw_ced->timer.freq = freq;
  218. clockevents_calc_mult_shift(&dw_ced->ced, freq, APBT_MIN_PERIOD);
  219. dw_ced->ced.max_delta_ns = clockevent_delta2ns(0x7fffffff,
  220. &dw_ced->ced);
  221. dw_ced->ced.max_delta_ticks = 0x7fffffff;
  222. dw_ced->ced.min_delta_ns = clockevent_delta2ns(5000, &dw_ced->ced);
  223. dw_ced->ced.min_delta_ticks = 5000;
  224. dw_ced->ced.cpumask = cpu < 0 ? cpu_possible_mask : cpumask_of(cpu);
  225. dw_ced->ced.features = CLOCK_EVT_FEAT_PERIODIC |
  226. CLOCK_EVT_FEAT_ONESHOT | CLOCK_EVT_FEAT_DYNIRQ;
  227. dw_ced->ced.set_state_shutdown = apbt_shutdown;
  228. dw_ced->ced.set_state_periodic = apbt_set_periodic;
  229. dw_ced->ced.set_state_oneshot = apbt_set_oneshot;
  230. dw_ced->ced.set_state_oneshot_stopped = apbt_shutdown;
  231. dw_ced->ced.tick_resume = apbt_resume;
  232. dw_ced->ced.set_next_event = apbt_next_event;
  233. dw_ced->ced.irq = dw_ced->timer.irq;
  234. dw_ced->ced.rating = rating;
  235. dw_ced->ced.name = name;
  236. dw_ced->irqaction.name = dw_ced->ced.name;
  237. dw_ced->irqaction.handler = dw_apb_clockevent_irq;
  238. dw_ced->irqaction.dev_id = &dw_ced->ced;
  239. dw_ced->irqaction.irq = irq;
  240. dw_ced->irqaction.flags = IRQF_TIMER | IRQF_IRQPOLL |
  241. IRQF_NOBALANCING;
  242. dw_ced->eoi = apbt_eoi;
  243. err = setup_irq(irq, &dw_ced->irqaction);
  244. if (err) {
  245. pr_err("failed to request timer irq\n");
  246. kfree(dw_ced);
  247. dw_ced = NULL;
  248. }
  249. return dw_ced;
  250. }
  251. /**
  252. * dw_apb_clockevent_resume() - resume a clock that has been paused.
  253. *
  254. * @dw_ced: The APB clock to resume.
  255. */
  256. void dw_apb_clockevent_resume(struct dw_apb_clock_event_device *dw_ced)
  257. {
  258. enable_irq(dw_ced->timer.irq);
  259. }
  260. /**
  261. * dw_apb_clockevent_stop() - stop the clock_event_device and release the IRQ.
  262. *
  263. * @dw_ced: The APB clock to stop generating the events.
  264. */
  265. void dw_apb_clockevent_stop(struct dw_apb_clock_event_device *dw_ced)
  266. {
  267. free_irq(dw_ced->timer.irq, &dw_ced->ced);
  268. }
  269. /**
  270. * dw_apb_clockevent_register() - register the clock with the generic layer
  271. *
  272. * @dw_ced: The APB clock to register as a clock_event_device.
  273. */
  274. void dw_apb_clockevent_register(struct dw_apb_clock_event_device *dw_ced)
  275. {
  276. apbt_writel(&dw_ced->timer, 0, APBTMR_N_CONTROL);
  277. clockevents_register_device(&dw_ced->ced);
  278. apbt_enable_int(&dw_ced->timer);
  279. }
  280. /**
  281. * dw_apb_clocksource_start() - start the clocksource counting.
  282. *
  283. * @dw_cs: The clocksource to start.
  284. *
  285. * This is used to start the clocksource before registration and can be used
  286. * to enable calibration of timers.
  287. */
  288. void dw_apb_clocksource_start(struct dw_apb_clocksource *dw_cs)
  289. {
  290. /*
  291. * start count down from 0xffff_ffff. this is done by toggling the
  292. * enable bit then load initial load count to ~0.
  293. */
  294. u32 ctrl = apbt_readl(&dw_cs->timer, APBTMR_N_CONTROL);
  295. ctrl &= ~APBTMR_CONTROL_ENABLE;
  296. apbt_writel(&dw_cs->timer, ctrl, APBTMR_N_CONTROL);
  297. apbt_writel(&dw_cs->timer, ~0, APBTMR_N_LOAD_COUNT);
  298. /* enable, mask interrupt */
  299. ctrl &= ~APBTMR_CONTROL_MODE_PERIODIC;
  300. ctrl |= (APBTMR_CONTROL_ENABLE | APBTMR_CONTROL_INT);
  301. apbt_writel(&dw_cs->timer, ctrl, APBTMR_N_CONTROL);
  302. /* read it once to get cached counter value initialized */
  303. dw_apb_clocksource_read(dw_cs);
  304. }
  305. static u64 __apbt_read_clocksource(struct clocksource *cs)
  306. {
  307. u32 current_count;
  308. struct dw_apb_clocksource *dw_cs =
  309. clocksource_to_dw_apb_clocksource(cs);
  310. current_count = apbt_readl_relaxed(&dw_cs->timer,
  311. APBTMR_N_CURRENT_VALUE);
  312. return (u64)~current_count;
  313. }
  314. static void apbt_restart_clocksource(struct clocksource *cs)
  315. {
  316. struct dw_apb_clocksource *dw_cs =
  317. clocksource_to_dw_apb_clocksource(cs);
  318. dw_apb_clocksource_start(dw_cs);
  319. }
  320. /**
  321. * dw_apb_clocksource_init() - use an APB timer as a clocksource.
  322. *
  323. * @rating: The rating to give the clocksource.
  324. * @name: The name for the clocksource.
  325. * @base: The I/O base for the timer registers.
  326. * @freq: The frequency that the timer counts at.
  327. *
  328. * This creates a clocksource using an APB timer but does not yet register it
  329. * with the clocksource system. This should be done with
  330. * dw_apb_clocksource_register() as the next step.
  331. */
  332. struct dw_apb_clocksource *
  333. dw_apb_clocksource_init(unsigned rating, const char *name, void __iomem *base,
  334. unsigned long freq)
  335. {
  336. struct dw_apb_clocksource *dw_cs = kzalloc(sizeof(*dw_cs), GFP_KERNEL);
  337. if (!dw_cs)
  338. return NULL;
  339. dw_cs->timer.base = base;
  340. dw_cs->timer.freq = freq;
  341. dw_cs->cs.name = name;
  342. dw_cs->cs.rating = rating;
  343. dw_cs->cs.read = __apbt_read_clocksource;
  344. dw_cs->cs.mask = CLOCKSOURCE_MASK(32);
  345. dw_cs->cs.flags = CLOCK_SOURCE_IS_CONTINUOUS;
  346. dw_cs->cs.resume = apbt_restart_clocksource;
  347. return dw_cs;
  348. }
  349. /**
  350. * dw_apb_clocksource_register() - register the APB clocksource.
  351. *
  352. * @dw_cs: The clocksource to register.
  353. */
  354. void dw_apb_clocksource_register(struct dw_apb_clocksource *dw_cs)
  355. {
  356. clocksource_register_hz(&dw_cs->cs, dw_cs->timer.freq);
  357. }
  358. /**
  359. * dw_apb_clocksource_read() - read the current value of a clocksource.
  360. *
  361. * @dw_cs: The clocksource to read.
  362. */
  363. u64 dw_apb_clocksource_read(struct dw_apb_clocksource *dw_cs)
  364. {
  365. return (u64)~apbt_readl(&dw_cs->timer, APBTMR_N_CURRENT_VALUE);
  366. }