em_sti.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Emma Mobile Timer Support - STI
  4. *
  5. * Copyright (C) 2012 Magnus Damm
  6. */
  7. #include <linux/init.h>
  8. #include <linux/platform_device.h>
  9. #include <linux/spinlock.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/ioport.h>
  12. #include <linux/io.h>
  13. #include <linux/clk.h>
  14. #include <linux/irq.h>
  15. #include <linux/err.h>
  16. #include <linux/delay.h>
  17. #include <linux/clocksource.h>
  18. #include <linux/clockchips.h>
  19. #include <linux/slab.h>
  20. #include <linux/module.h>
  21. enum { USER_CLOCKSOURCE, USER_CLOCKEVENT, USER_NR };
  22. struct em_sti_priv {
  23. void __iomem *base;
  24. struct clk *clk;
  25. struct platform_device *pdev;
  26. unsigned int active[USER_NR];
  27. unsigned long rate;
  28. raw_spinlock_t lock;
  29. struct clock_event_device ced;
  30. struct clocksource cs;
  31. };
  32. #define STI_CONTROL 0x00
  33. #define STI_COMPA_H 0x10
  34. #define STI_COMPA_L 0x14
  35. #define STI_COMPB_H 0x18
  36. #define STI_COMPB_L 0x1c
  37. #define STI_COUNT_H 0x20
  38. #define STI_COUNT_L 0x24
  39. #define STI_COUNT_RAW_H 0x28
  40. #define STI_COUNT_RAW_L 0x2c
  41. #define STI_SET_H 0x30
  42. #define STI_SET_L 0x34
  43. #define STI_INTSTATUS 0x40
  44. #define STI_INTRAWSTATUS 0x44
  45. #define STI_INTENSET 0x48
  46. #define STI_INTENCLR 0x4c
  47. #define STI_INTFFCLR 0x50
  48. static inline unsigned long em_sti_read(struct em_sti_priv *p, int offs)
  49. {
  50. return ioread32(p->base + offs);
  51. }
  52. static inline void em_sti_write(struct em_sti_priv *p, int offs,
  53. unsigned long value)
  54. {
  55. iowrite32(value, p->base + offs);
  56. }
  57. static int em_sti_enable(struct em_sti_priv *p)
  58. {
  59. int ret;
  60. /* enable clock */
  61. ret = clk_enable(p->clk);
  62. if (ret) {
  63. dev_err(&p->pdev->dev, "cannot enable clock\n");
  64. return ret;
  65. }
  66. /* reset the counter */
  67. em_sti_write(p, STI_SET_H, 0x40000000);
  68. em_sti_write(p, STI_SET_L, 0x00000000);
  69. /* mask and clear pending interrupts */
  70. em_sti_write(p, STI_INTENCLR, 3);
  71. em_sti_write(p, STI_INTFFCLR, 3);
  72. /* enable updates of counter registers */
  73. em_sti_write(p, STI_CONTROL, 1);
  74. return 0;
  75. }
  76. static void em_sti_disable(struct em_sti_priv *p)
  77. {
  78. /* mask interrupts */
  79. em_sti_write(p, STI_INTENCLR, 3);
  80. /* stop clock */
  81. clk_disable(p->clk);
  82. }
  83. static u64 em_sti_count(struct em_sti_priv *p)
  84. {
  85. u64 ticks;
  86. unsigned long flags;
  87. /* the STI hardware buffers the 48-bit count, but to
  88. * break it out into two 32-bit access the registers
  89. * must be accessed in a certain order.
  90. * Always read STI_COUNT_H before STI_COUNT_L.
  91. */
  92. raw_spin_lock_irqsave(&p->lock, flags);
  93. ticks = (u64)(em_sti_read(p, STI_COUNT_H) & 0xffff) << 32;
  94. ticks |= em_sti_read(p, STI_COUNT_L);
  95. raw_spin_unlock_irqrestore(&p->lock, flags);
  96. return ticks;
  97. }
  98. static u64 em_sti_set_next(struct em_sti_priv *p, u64 next)
  99. {
  100. unsigned long flags;
  101. raw_spin_lock_irqsave(&p->lock, flags);
  102. /* mask compare A interrupt */
  103. em_sti_write(p, STI_INTENCLR, 1);
  104. /* update compare A value */
  105. em_sti_write(p, STI_COMPA_H, next >> 32);
  106. em_sti_write(p, STI_COMPA_L, next & 0xffffffff);
  107. /* clear compare A interrupt source */
  108. em_sti_write(p, STI_INTFFCLR, 1);
  109. /* unmask compare A interrupt */
  110. em_sti_write(p, STI_INTENSET, 1);
  111. raw_spin_unlock_irqrestore(&p->lock, flags);
  112. return next;
  113. }
  114. static irqreturn_t em_sti_interrupt(int irq, void *dev_id)
  115. {
  116. struct em_sti_priv *p = dev_id;
  117. p->ced.event_handler(&p->ced);
  118. return IRQ_HANDLED;
  119. }
  120. static int em_sti_start(struct em_sti_priv *p, unsigned int user)
  121. {
  122. unsigned long flags;
  123. int used_before;
  124. int ret = 0;
  125. raw_spin_lock_irqsave(&p->lock, flags);
  126. used_before = p->active[USER_CLOCKSOURCE] | p->active[USER_CLOCKEVENT];
  127. if (!used_before)
  128. ret = em_sti_enable(p);
  129. if (!ret)
  130. p->active[user] = 1;
  131. raw_spin_unlock_irqrestore(&p->lock, flags);
  132. return ret;
  133. }
  134. static void em_sti_stop(struct em_sti_priv *p, unsigned int user)
  135. {
  136. unsigned long flags;
  137. int used_before, used_after;
  138. raw_spin_lock_irqsave(&p->lock, flags);
  139. used_before = p->active[USER_CLOCKSOURCE] | p->active[USER_CLOCKEVENT];
  140. p->active[user] = 0;
  141. used_after = p->active[USER_CLOCKSOURCE] | p->active[USER_CLOCKEVENT];
  142. if (used_before && !used_after)
  143. em_sti_disable(p);
  144. raw_spin_unlock_irqrestore(&p->lock, flags);
  145. }
  146. static struct em_sti_priv *cs_to_em_sti(struct clocksource *cs)
  147. {
  148. return container_of(cs, struct em_sti_priv, cs);
  149. }
  150. static u64 em_sti_clocksource_read(struct clocksource *cs)
  151. {
  152. return em_sti_count(cs_to_em_sti(cs));
  153. }
  154. static int em_sti_clocksource_enable(struct clocksource *cs)
  155. {
  156. struct em_sti_priv *p = cs_to_em_sti(cs);
  157. return em_sti_start(p, USER_CLOCKSOURCE);
  158. }
  159. static void em_sti_clocksource_disable(struct clocksource *cs)
  160. {
  161. em_sti_stop(cs_to_em_sti(cs), USER_CLOCKSOURCE);
  162. }
  163. static void em_sti_clocksource_resume(struct clocksource *cs)
  164. {
  165. em_sti_clocksource_enable(cs);
  166. }
  167. static int em_sti_register_clocksource(struct em_sti_priv *p)
  168. {
  169. struct clocksource *cs = &p->cs;
  170. cs->name = dev_name(&p->pdev->dev);
  171. cs->rating = 200;
  172. cs->read = em_sti_clocksource_read;
  173. cs->enable = em_sti_clocksource_enable;
  174. cs->disable = em_sti_clocksource_disable;
  175. cs->suspend = em_sti_clocksource_disable;
  176. cs->resume = em_sti_clocksource_resume;
  177. cs->mask = CLOCKSOURCE_MASK(48);
  178. cs->flags = CLOCK_SOURCE_IS_CONTINUOUS;
  179. dev_info(&p->pdev->dev, "used as clock source\n");
  180. clocksource_register_hz(cs, p->rate);
  181. return 0;
  182. }
  183. static struct em_sti_priv *ced_to_em_sti(struct clock_event_device *ced)
  184. {
  185. return container_of(ced, struct em_sti_priv, ced);
  186. }
  187. static int em_sti_clock_event_shutdown(struct clock_event_device *ced)
  188. {
  189. struct em_sti_priv *p = ced_to_em_sti(ced);
  190. em_sti_stop(p, USER_CLOCKEVENT);
  191. return 0;
  192. }
  193. static int em_sti_clock_event_set_oneshot(struct clock_event_device *ced)
  194. {
  195. struct em_sti_priv *p = ced_to_em_sti(ced);
  196. dev_info(&p->pdev->dev, "used for oneshot clock events\n");
  197. em_sti_start(p, USER_CLOCKEVENT);
  198. return 0;
  199. }
  200. static int em_sti_clock_event_next(unsigned long delta,
  201. struct clock_event_device *ced)
  202. {
  203. struct em_sti_priv *p = ced_to_em_sti(ced);
  204. u64 next;
  205. int safe;
  206. next = em_sti_set_next(p, em_sti_count(p) + delta);
  207. safe = em_sti_count(p) < (next - 1);
  208. return !safe;
  209. }
  210. static void em_sti_register_clockevent(struct em_sti_priv *p)
  211. {
  212. struct clock_event_device *ced = &p->ced;
  213. ced->name = dev_name(&p->pdev->dev);
  214. ced->features = CLOCK_EVT_FEAT_ONESHOT;
  215. ced->rating = 200;
  216. ced->cpumask = cpu_possible_mask;
  217. ced->set_next_event = em_sti_clock_event_next;
  218. ced->set_state_shutdown = em_sti_clock_event_shutdown;
  219. ced->set_state_oneshot = em_sti_clock_event_set_oneshot;
  220. dev_info(&p->pdev->dev, "used for clock events\n");
  221. clockevents_config_and_register(ced, p->rate, 2, 0xffffffff);
  222. }
  223. static int em_sti_probe(struct platform_device *pdev)
  224. {
  225. struct em_sti_priv *p;
  226. int irq, ret;
  227. p = devm_kzalloc(&pdev->dev, sizeof(*p), GFP_KERNEL);
  228. if (p == NULL)
  229. return -ENOMEM;
  230. p->pdev = pdev;
  231. platform_set_drvdata(pdev, p);
  232. irq = platform_get_irq(pdev, 0);
  233. if (irq < 0)
  234. return irq;
  235. /* map memory, let base point to the STI instance */
  236. p->base = devm_platform_ioremap_resource(pdev, 0);
  237. if (IS_ERR(p->base))
  238. return PTR_ERR(p->base);
  239. ret = devm_request_irq(&pdev->dev, irq, em_sti_interrupt,
  240. IRQF_TIMER | IRQF_IRQPOLL | IRQF_NOBALANCING,
  241. dev_name(&pdev->dev), p);
  242. if (ret) {
  243. dev_err(&pdev->dev, "failed to request low IRQ\n");
  244. return ret;
  245. }
  246. /* get hold of clock */
  247. p->clk = devm_clk_get(&pdev->dev, "sclk");
  248. if (IS_ERR(p->clk)) {
  249. dev_err(&pdev->dev, "cannot get clock\n");
  250. return PTR_ERR(p->clk);
  251. }
  252. ret = clk_prepare(p->clk);
  253. if (ret < 0) {
  254. dev_err(&pdev->dev, "cannot prepare clock\n");
  255. return ret;
  256. }
  257. ret = clk_enable(p->clk);
  258. if (ret < 0) {
  259. dev_err(&p->pdev->dev, "cannot enable clock\n");
  260. clk_unprepare(p->clk);
  261. return ret;
  262. }
  263. p->rate = clk_get_rate(p->clk);
  264. clk_disable(p->clk);
  265. raw_spin_lock_init(&p->lock);
  266. em_sti_register_clockevent(p);
  267. em_sti_register_clocksource(p);
  268. return 0;
  269. }
  270. static int em_sti_remove(struct platform_device *pdev)
  271. {
  272. return -EBUSY; /* cannot unregister clockevent and clocksource */
  273. }
  274. static const struct of_device_id em_sti_dt_ids[] = {
  275. { .compatible = "renesas,em-sti", },
  276. {},
  277. };
  278. MODULE_DEVICE_TABLE(of, em_sti_dt_ids);
  279. static struct platform_driver em_sti_device_driver = {
  280. .probe = em_sti_probe,
  281. .remove = em_sti_remove,
  282. .driver = {
  283. .name = "em_sti",
  284. .of_match_table = em_sti_dt_ids,
  285. }
  286. };
  287. static int __init em_sti_init(void)
  288. {
  289. return platform_driver_register(&em_sti_device_driver);
  290. }
  291. static void __exit em_sti_exit(void)
  292. {
  293. platform_driver_unregister(&em_sti_device_driver);
  294. }
  295. subsys_initcall(em_sti_init);
  296. module_exit(em_sti_exit);
  297. MODULE_AUTHOR("Magnus Damm");
  298. MODULE_DESCRIPTION("Renesas Emma Mobile STI Timer Driver");
  299. MODULE_LICENSE("GPL v2");