em_sti.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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_prepare_enable(p->clk);
  74. if (ret) {
  75. dev_err(&p->pdev->dev, "cannot enable clock\n");
  76. return ret;
  77. }
  78. /* configure channel, periodic mode and maximum timeout */
  79. p->rate = clk_get_rate(p->clk);
  80. /* reset the counter */
  81. em_sti_write(p, STI_SET_H, 0x40000000);
  82. em_sti_write(p, STI_SET_L, 0x00000000);
  83. /* mask and clear pending interrupts */
  84. em_sti_write(p, STI_INTENCLR, 3);
  85. em_sti_write(p, STI_INTFFCLR, 3);
  86. /* enable updates of counter registers */
  87. em_sti_write(p, STI_CONTROL, 1);
  88. return 0;
  89. }
  90. static void em_sti_disable(struct em_sti_priv *p)
  91. {
  92. /* mask interrupts */
  93. em_sti_write(p, STI_INTENCLR, 3);
  94. /* stop clock */
  95. clk_disable_unprepare(p->clk);
  96. }
  97. static cycle_t em_sti_count(struct em_sti_priv *p)
  98. {
  99. cycle_t ticks;
  100. unsigned long flags;
  101. /* the STI hardware buffers the 48-bit count, but to
  102. * break it out into two 32-bit access the registers
  103. * must be accessed in a certain order.
  104. * Always read STI_COUNT_H before STI_COUNT_L.
  105. */
  106. raw_spin_lock_irqsave(&p->lock, flags);
  107. ticks = (cycle_t)(em_sti_read(p, STI_COUNT_H) & 0xffff) << 32;
  108. ticks |= em_sti_read(p, STI_COUNT_L);
  109. raw_spin_unlock_irqrestore(&p->lock, flags);
  110. return ticks;
  111. }
  112. static cycle_t em_sti_set_next(struct em_sti_priv *p, cycle_t next)
  113. {
  114. unsigned long flags;
  115. raw_spin_lock_irqsave(&p->lock, flags);
  116. /* mask compare A interrupt */
  117. em_sti_write(p, STI_INTENCLR, 1);
  118. /* update compare A value */
  119. em_sti_write(p, STI_COMPA_H, next >> 32);
  120. em_sti_write(p, STI_COMPA_L, next & 0xffffffff);
  121. /* clear compare A interrupt source */
  122. em_sti_write(p, STI_INTFFCLR, 1);
  123. /* unmask compare A interrupt */
  124. em_sti_write(p, STI_INTENSET, 1);
  125. raw_spin_unlock_irqrestore(&p->lock, flags);
  126. return next;
  127. }
  128. static irqreturn_t em_sti_interrupt(int irq, void *dev_id)
  129. {
  130. struct em_sti_priv *p = dev_id;
  131. p->ced.event_handler(&p->ced);
  132. return IRQ_HANDLED;
  133. }
  134. static int em_sti_start(struct em_sti_priv *p, unsigned int user)
  135. {
  136. unsigned long flags;
  137. int used_before;
  138. int ret = 0;
  139. raw_spin_lock_irqsave(&p->lock, flags);
  140. used_before = p->active[USER_CLOCKSOURCE] | p->active[USER_CLOCKEVENT];
  141. if (!used_before)
  142. ret = em_sti_enable(p);
  143. if (!ret)
  144. p->active[user] = 1;
  145. raw_spin_unlock_irqrestore(&p->lock, flags);
  146. return ret;
  147. }
  148. static void em_sti_stop(struct em_sti_priv *p, unsigned int user)
  149. {
  150. unsigned long flags;
  151. int used_before, used_after;
  152. raw_spin_lock_irqsave(&p->lock, flags);
  153. used_before = p->active[USER_CLOCKSOURCE] | p->active[USER_CLOCKEVENT];
  154. p->active[user] = 0;
  155. used_after = p->active[USER_CLOCKSOURCE] | p->active[USER_CLOCKEVENT];
  156. if (used_before && !used_after)
  157. em_sti_disable(p);
  158. raw_spin_unlock_irqrestore(&p->lock, flags);
  159. }
  160. static struct em_sti_priv *cs_to_em_sti(struct clocksource *cs)
  161. {
  162. return container_of(cs, struct em_sti_priv, cs);
  163. }
  164. static cycle_t em_sti_clocksource_read(struct clocksource *cs)
  165. {
  166. return em_sti_count(cs_to_em_sti(cs));
  167. }
  168. static int em_sti_clocksource_enable(struct clocksource *cs)
  169. {
  170. int ret;
  171. struct em_sti_priv *p = cs_to_em_sti(cs);
  172. ret = em_sti_start(p, USER_CLOCKSOURCE);
  173. if (!ret)
  174. __clocksource_update_freq_hz(cs, p->rate);
  175. return ret;
  176. }
  177. static void em_sti_clocksource_disable(struct clocksource *cs)
  178. {
  179. em_sti_stop(cs_to_em_sti(cs), USER_CLOCKSOURCE);
  180. }
  181. static void em_sti_clocksource_resume(struct clocksource *cs)
  182. {
  183. em_sti_clocksource_enable(cs);
  184. }
  185. static int em_sti_register_clocksource(struct em_sti_priv *p)
  186. {
  187. struct clocksource *cs = &p->cs;
  188. cs->name = dev_name(&p->pdev->dev);
  189. cs->rating = 200;
  190. cs->read = em_sti_clocksource_read;
  191. cs->enable = em_sti_clocksource_enable;
  192. cs->disable = em_sti_clocksource_disable;
  193. cs->suspend = em_sti_clocksource_disable;
  194. cs->resume = em_sti_clocksource_resume;
  195. cs->mask = CLOCKSOURCE_MASK(48);
  196. cs->flags = CLOCK_SOURCE_IS_CONTINUOUS;
  197. dev_info(&p->pdev->dev, "used as clock source\n");
  198. /* Register with dummy 1 Hz value, gets updated in ->enable() */
  199. clocksource_register_hz(cs, 1);
  200. return 0;
  201. }
  202. static struct em_sti_priv *ced_to_em_sti(struct clock_event_device *ced)
  203. {
  204. return container_of(ced, struct em_sti_priv, ced);
  205. }
  206. static int em_sti_clock_event_shutdown(struct clock_event_device *ced)
  207. {
  208. struct em_sti_priv *p = ced_to_em_sti(ced);
  209. em_sti_stop(p, USER_CLOCKEVENT);
  210. return 0;
  211. }
  212. static int em_sti_clock_event_set_oneshot(struct clock_event_device *ced)
  213. {
  214. struct em_sti_priv *p = ced_to_em_sti(ced);
  215. dev_info(&p->pdev->dev, "used for oneshot clock events\n");
  216. em_sti_start(p, USER_CLOCKEVENT);
  217. clockevents_config(&p->ced, p->rate);
  218. return 0;
  219. }
  220. static int em_sti_clock_event_next(unsigned long delta,
  221. struct clock_event_device *ced)
  222. {
  223. struct em_sti_priv *p = ced_to_em_sti(ced);
  224. cycle_t next;
  225. int safe;
  226. next = em_sti_set_next(p, em_sti_count(p) + delta);
  227. safe = em_sti_count(p) < (next - 1);
  228. return !safe;
  229. }
  230. static void em_sti_register_clockevent(struct em_sti_priv *p)
  231. {
  232. struct clock_event_device *ced = &p->ced;
  233. ced->name = dev_name(&p->pdev->dev);
  234. ced->features = CLOCK_EVT_FEAT_ONESHOT;
  235. ced->rating = 200;
  236. ced->cpumask = cpu_possible_mask;
  237. ced->set_next_event = em_sti_clock_event_next;
  238. ced->set_state_shutdown = em_sti_clock_event_shutdown;
  239. ced->set_state_oneshot = em_sti_clock_event_set_oneshot;
  240. dev_info(&p->pdev->dev, "used for clock events\n");
  241. /* Register with dummy 1 Hz value, gets updated in ->set_state_oneshot() */
  242. clockevents_config_and_register(ced, 1, 2, 0xffffffff);
  243. }
  244. static int em_sti_probe(struct platform_device *pdev)
  245. {
  246. struct em_sti_priv *p;
  247. struct resource *res;
  248. int irq;
  249. p = devm_kzalloc(&pdev->dev, sizeof(*p), GFP_KERNEL);
  250. if (p == NULL)
  251. return -ENOMEM;
  252. p->pdev = pdev;
  253. platform_set_drvdata(pdev, p);
  254. irq = platform_get_irq(pdev, 0);
  255. if (irq < 0) {
  256. dev_err(&pdev->dev, "failed to get irq\n");
  257. return -EINVAL;
  258. }
  259. /* map memory, let base point to the STI instance */
  260. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  261. p->base = devm_ioremap_resource(&pdev->dev, res);
  262. if (IS_ERR(p->base))
  263. return PTR_ERR(p->base);
  264. /* get hold of clock */
  265. p->clk = devm_clk_get(&pdev->dev, "sclk");
  266. if (IS_ERR(p->clk)) {
  267. dev_err(&pdev->dev, "cannot get clock\n");
  268. return PTR_ERR(p->clk);
  269. }
  270. if (devm_request_irq(&pdev->dev, irq, em_sti_interrupt,
  271. IRQF_TIMER | IRQF_IRQPOLL | IRQF_NOBALANCING,
  272. dev_name(&pdev->dev), p)) {
  273. dev_err(&pdev->dev, "failed to request low IRQ\n");
  274. return -ENOENT;
  275. }
  276. raw_spin_lock_init(&p->lock);
  277. em_sti_register_clockevent(p);
  278. em_sti_register_clocksource(p);
  279. return 0;
  280. }
  281. static int em_sti_remove(struct platform_device *pdev)
  282. {
  283. return -EBUSY; /* cannot unregister clockevent and clocksource */
  284. }
  285. static const struct of_device_id em_sti_dt_ids[] = {
  286. { .compatible = "renesas,em-sti", },
  287. {},
  288. };
  289. MODULE_DEVICE_TABLE(of, em_sti_dt_ids);
  290. static struct platform_driver em_sti_device_driver = {
  291. .probe = em_sti_probe,
  292. .remove = em_sti_remove,
  293. .driver = {
  294. .name = "em_sti",
  295. .of_match_table = em_sti_dt_ids,
  296. }
  297. };
  298. static int __init em_sti_init(void)
  299. {
  300. return platform_driver_register(&em_sti_device_driver);
  301. }
  302. static void __exit em_sti_exit(void)
  303. {
  304. platform_driver_unregister(&em_sti_device_driver);
  305. }
  306. subsys_initcall(em_sti_init);
  307. module_exit(em_sti_exit);
  308. MODULE_AUTHOR("Magnus Damm");
  309. MODULE_DESCRIPTION("Renesas Emma Mobile STI Timer Driver");
  310. MODULE_LICENSE("GPL v2");