em_sti.c 9.2 KB

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