dw_apb_timer.c 12 KB

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