PageRenderTime 27ms CodeModel.GetById 35ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/clocksource/em_sti.c

https://bitbucket.org/danhamilt1/linux
C | 417 lines | 305 code | 73 blank | 39 comment | 16 complexity | 54ad564331315baed5618773d13ec60a MD5 | raw file
  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. /* 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(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_updatefreq_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. memset(cs, 0, sizeof(*cs));
  189. cs->name = dev_name(&p->pdev->dev);
  190. cs->rating = 200;
  191. cs->read = em_sti_clocksource_read;
  192. cs->enable = em_sti_clocksource_enable;
  193. cs->disable = em_sti_clocksource_disable;
  194. cs->suspend = em_sti_clocksource_disable;
  195. cs->resume = em_sti_clocksource_resume;
  196. cs->mask = CLOCKSOURCE_MASK(48);
  197. cs->flags = CLOCK_SOURCE_IS_CONTINUOUS;
  198. dev_info(&p->pdev->dev, "used as clock source\n");
  199. /* Register with dummy 1 Hz value, gets updated in ->enable() */
  200. clocksource_register_hz(cs, 1);
  201. return 0;
  202. }
  203. static struct em_sti_priv *ced_to_em_sti(struct clock_event_device *ced)
  204. {
  205. return container_of(ced, struct em_sti_priv, ced);
  206. }
  207. static void em_sti_clock_event_mode(enum clock_event_mode mode,
  208. struct clock_event_device *ced)
  209. {
  210. struct em_sti_priv *p = ced_to_em_sti(ced);
  211. /* deal with old setting first */
  212. switch (ced->mode) {
  213. case CLOCK_EVT_MODE_ONESHOT:
  214. em_sti_stop(p, USER_CLOCKEVENT);
  215. break;
  216. default:
  217. break;
  218. }
  219. switch (mode) {
  220. case CLOCK_EVT_MODE_ONESHOT:
  221. dev_info(&p->pdev->dev, "used for oneshot clock events\n");
  222. em_sti_start(p, USER_CLOCKEVENT);
  223. clockevents_config(&p->ced, p->rate);
  224. break;
  225. case CLOCK_EVT_MODE_SHUTDOWN:
  226. case CLOCK_EVT_MODE_UNUSED:
  227. em_sti_stop(p, USER_CLOCKEVENT);
  228. break;
  229. default:
  230. break;
  231. }
  232. }
  233. static int em_sti_clock_event_next(unsigned long delta,
  234. struct clock_event_device *ced)
  235. {
  236. struct em_sti_priv *p = ced_to_em_sti(ced);
  237. cycle_t next;
  238. int safe;
  239. next = em_sti_set_next(p, em_sti_count(p) + delta);
  240. safe = em_sti_count(p) < (next - 1);
  241. return !safe;
  242. }
  243. static void em_sti_register_clockevent(struct em_sti_priv *p)
  244. {
  245. struct clock_event_device *ced = &p->ced;
  246. memset(ced, 0, sizeof(*ced));
  247. ced->name = dev_name(&p->pdev->dev);
  248. ced->features = CLOCK_EVT_FEAT_ONESHOT;
  249. ced->rating = 200;
  250. ced->cpumask = cpumask_of(0);
  251. ced->set_next_event = em_sti_clock_event_next;
  252. ced->set_mode = em_sti_clock_event_mode;
  253. dev_info(&p->pdev->dev, "used for clock events\n");
  254. /* Register with dummy 1 Hz value, gets updated in ->set_mode() */
  255. clockevents_config_and_register(ced, 1, 2, 0xffffffff);
  256. }
  257. static int em_sti_probe(struct platform_device *pdev)
  258. {
  259. struct em_sti_priv *p;
  260. struct resource *res;
  261. int irq, ret;
  262. p = kzalloc(sizeof(*p), GFP_KERNEL);
  263. if (p == NULL) {
  264. dev_err(&pdev->dev, "failed to allocate driver data\n");
  265. ret = -ENOMEM;
  266. goto err0;
  267. }
  268. p->pdev = pdev;
  269. platform_set_drvdata(pdev, p);
  270. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  271. if (!res) {
  272. dev_err(&pdev->dev, "failed to get I/O memory\n");
  273. ret = -EINVAL;
  274. goto err0;
  275. }
  276. irq = platform_get_irq(pdev, 0);
  277. if (irq < 0) {
  278. dev_err(&pdev->dev, "failed to get irq\n");
  279. ret = -EINVAL;
  280. goto err0;
  281. }
  282. /* map memory, let base point to the STI instance */
  283. p->base = ioremap_nocache(res->start, resource_size(res));
  284. if (p->base == NULL) {
  285. dev_err(&pdev->dev, "failed to remap I/O memory\n");
  286. ret = -ENXIO;
  287. goto err0;
  288. }
  289. /* get hold of clock */
  290. p->clk = clk_get(&pdev->dev, "sclk");
  291. if (IS_ERR(p->clk)) {
  292. dev_err(&pdev->dev, "cannot get clock\n");
  293. ret = PTR_ERR(p->clk);
  294. goto err1;
  295. }
  296. if (request_irq(irq, em_sti_interrupt,
  297. IRQF_TIMER | IRQF_IRQPOLL | IRQF_NOBALANCING,
  298. dev_name(&pdev->dev), p)) {
  299. dev_err(&pdev->dev, "failed to request low IRQ\n");
  300. ret = -ENOENT;
  301. goto err2;
  302. }
  303. raw_spin_lock_init(&p->lock);
  304. em_sti_register_clockevent(p);
  305. em_sti_register_clocksource(p);
  306. return 0;
  307. err2:
  308. clk_put(p->clk);
  309. err1:
  310. iounmap(p->base);
  311. err0:
  312. kfree(p);
  313. return ret;
  314. }
  315. static int em_sti_remove(struct platform_device *pdev)
  316. {
  317. return -EBUSY; /* cannot unregister clockevent and clocksource */
  318. }
  319. static const struct of_device_id em_sti_dt_ids[] = {
  320. { .compatible = "renesas,em-sti", },
  321. {},
  322. };
  323. MODULE_DEVICE_TABLE(of, em_sti_dt_ids);
  324. static struct platform_driver em_sti_device_driver = {
  325. .probe = em_sti_probe,
  326. .remove = em_sti_remove,
  327. .driver = {
  328. .name = "em_sti",
  329. .of_match_table = em_sti_dt_ids,
  330. }
  331. };
  332. static int __init em_sti_init(void)
  333. {
  334. return platform_driver_register(&em_sti_device_driver);
  335. }
  336. static void __exit em_sti_exit(void)
  337. {
  338. platform_driver_unregister(&em_sti_device_driver);
  339. }
  340. subsys_initcall(em_sti_init);
  341. module_exit(em_sti_exit);
  342. MODULE_AUTHOR("Magnus Damm");
  343. MODULE_DESCRIPTION("Renesas Emma Mobile STI Timer Driver");
  344. MODULE_LICENSE("GPL v2");