gptu.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * This program is free software; you can redistribute it and/or modify it
  3. * under the terms of the GNU General Public License version 2 as published
  4. * by the Free Software Foundation.
  5. *
  6. * Copyright (C) 2012 John Crispin <john@phrozen.org>
  7. * Copyright (C) 2012 Lantiq GmbH
  8. */
  9. #include <linux/interrupt.h>
  10. #include <linux/ioport.h>
  11. #include <linux/module.h>
  12. #include <linux/of_platform.h>
  13. #include <linux/of_irq.h>
  14. #include <lantiq_soc.h>
  15. #include "../clk.h"
  16. /* the magic ID byte of the core */
  17. #define GPTU_MAGIC 0x59
  18. /* clock control register */
  19. #define GPTU_CLC 0x00
  20. /* id register */
  21. #define GPTU_ID 0x08
  22. /* interrupt node enable */
  23. #define GPTU_IRNEN 0xf4
  24. /* interrupt control register */
  25. #define GPTU_IRCR 0xf8
  26. /* interrupt capture register */
  27. #define GPTU_IRNCR 0xfc
  28. /* there are 3 identical blocks of 2 timers. calculate register offsets */
  29. #define GPTU_SHIFT(x) (x % 2 ? 4 : 0)
  30. #define GPTU_BASE(x) (((x >> 1) * 0x20) + 0x10)
  31. /* timer control register */
  32. #define GPTU_CON(x) (GPTU_BASE(x) + GPTU_SHIFT(x) + 0x00)
  33. /* timer auto reload register */
  34. #define GPTU_RUN(x) (GPTU_BASE(x) + GPTU_SHIFT(x) + 0x08)
  35. /* timer manual reload register */
  36. #define GPTU_RLD(x) (GPTU_BASE(x) + GPTU_SHIFT(x) + 0x10)
  37. /* timer count register */
  38. #define GPTU_CNT(x) (GPTU_BASE(x) + GPTU_SHIFT(x) + 0x18)
  39. /* GPTU_CON(x) */
  40. #define CON_CNT BIT(2)
  41. #define CON_EDGE_ANY (BIT(7) | BIT(6))
  42. #define CON_SYNC BIT(8)
  43. #define CON_CLK_INT BIT(10)
  44. /* GPTU_RUN(x) */
  45. #define RUN_SEN BIT(0)
  46. #define RUN_RL BIT(2)
  47. /* set clock to runmode */
  48. #define CLC_RMC BIT(8)
  49. /* bring core out of suspend */
  50. #define CLC_SUSPEND BIT(4)
  51. /* the disable bit */
  52. #define CLC_DISABLE BIT(0)
  53. #define gptu_w32(x, y) ltq_w32((x), gptu_membase + (y))
  54. #define gptu_r32(x) ltq_r32(gptu_membase + (x))
  55. enum gptu_timer {
  56. TIMER1A = 0,
  57. TIMER1B,
  58. TIMER2A,
  59. TIMER2B,
  60. TIMER3A,
  61. TIMER3B
  62. };
  63. static void __iomem *gptu_membase;
  64. static struct resource irqres[6];
  65. static irqreturn_t timer_irq_handler(int irq, void *priv)
  66. {
  67. int timer = irq - irqres[0].start;
  68. gptu_w32(1 << timer, GPTU_IRNCR);
  69. return IRQ_HANDLED;
  70. }
  71. static void gptu_hwinit(void)
  72. {
  73. gptu_w32(0x00, GPTU_IRNEN);
  74. gptu_w32(0xff, GPTU_IRNCR);
  75. gptu_w32(CLC_RMC | CLC_SUSPEND, GPTU_CLC);
  76. }
  77. static void gptu_hwexit(void)
  78. {
  79. gptu_w32(0x00, GPTU_IRNEN);
  80. gptu_w32(0xff, GPTU_IRNCR);
  81. gptu_w32(CLC_DISABLE, GPTU_CLC);
  82. }
  83. static int gptu_enable(struct clk *clk)
  84. {
  85. int ret = request_irq(irqres[clk->bits].start, timer_irq_handler,
  86. IRQF_TIMER, "gtpu", NULL);
  87. if (ret) {
  88. pr_err("gptu: failed to request irq\n");
  89. return ret;
  90. }
  91. gptu_w32(CON_CNT | CON_EDGE_ANY | CON_SYNC | CON_CLK_INT,
  92. GPTU_CON(clk->bits));
  93. gptu_w32(1, GPTU_RLD(clk->bits));
  94. gptu_w32(gptu_r32(GPTU_IRNEN) | BIT(clk->bits), GPTU_IRNEN);
  95. gptu_w32(RUN_SEN | RUN_RL, GPTU_RUN(clk->bits));
  96. return 0;
  97. }
  98. static void gptu_disable(struct clk *clk)
  99. {
  100. gptu_w32(0, GPTU_RUN(clk->bits));
  101. gptu_w32(0, GPTU_CON(clk->bits));
  102. gptu_w32(0, GPTU_RLD(clk->bits));
  103. gptu_w32(gptu_r32(GPTU_IRNEN) & ~BIT(clk->bits), GPTU_IRNEN);
  104. free_irq(irqres[clk->bits].start, NULL);
  105. }
  106. static inline void clkdev_add_gptu(struct device *dev, const char *con,
  107. unsigned int timer)
  108. {
  109. struct clk *clk = kzalloc(sizeof(struct clk), GFP_KERNEL);
  110. clk->cl.dev_id = dev_name(dev);
  111. clk->cl.con_id = con;
  112. clk->cl.clk = clk;
  113. clk->enable = gptu_enable;
  114. clk->disable = gptu_disable;
  115. clk->bits = timer;
  116. clkdev_add(&clk->cl);
  117. }
  118. static int gptu_probe(struct platform_device *pdev)
  119. {
  120. struct clk *clk;
  121. struct resource *res;
  122. if (of_irq_to_resource_table(pdev->dev.of_node, irqres, 6) != 6) {
  123. dev_err(&pdev->dev, "Failed to get IRQ list\n");
  124. return -EINVAL;
  125. }
  126. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  127. /* remap gptu register range */
  128. gptu_membase = devm_ioremap_resource(&pdev->dev, res);
  129. if (IS_ERR(gptu_membase))
  130. return PTR_ERR(gptu_membase);
  131. /* enable our clock */
  132. clk = clk_get(&pdev->dev, NULL);
  133. if (IS_ERR(clk)) {
  134. dev_err(&pdev->dev, "Failed to get clock\n");
  135. return -ENOENT;
  136. }
  137. clk_enable(clk);
  138. /* power up the core */
  139. gptu_hwinit();
  140. /* the gptu has a ID register */
  141. if (((gptu_r32(GPTU_ID) >> 8) & 0xff) != GPTU_MAGIC) {
  142. dev_err(&pdev->dev, "Failed to find magic\n");
  143. gptu_hwexit();
  144. clk_disable(clk);
  145. clk_put(clk);
  146. return -ENAVAIL;
  147. }
  148. /* register the clocks */
  149. clkdev_add_gptu(&pdev->dev, "timer1a", TIMER1A);
  150. clkdev_add_gptu(&pdev->dev, "timer1b", TIMER1B);
  151. clkdev_add_gptu(&pdev->dev, "timer2a", TIMER2A);
  152. clkdev_add_gptu(&pdev->dev, "timer2b", TIMER2B);
  153. clkdev_add_gptu(&pdev->dev, "timer3a", TIMER3A);
  154. clkdev_add_gptu(&pdev->dev, "timer3b", TIMER3B);
  155. dev_info(&pdev->dev, "gptu: 6 timers loaded\n");
  156. return 0;
  157. }
  158. static const struct of_device_id gptu_match[] = {
  159. { .compatible = "lantiq,gptu-xway" },
  160. {},
  161. };
  162. MODULE_DEVICE_TABLE(of, dma_match);
  163. static struct platform_driver dma_driver = {
  164. .probe = gptu_probe,
  165. .driver = {
  166. .name = "gptu-xway",
  167. .of_match_table = gptu_match,
  168. },
  169. };
  170. int __init gptu_init(void)
  171. {
  172. int ret = platform_driver_register(&dma_driver);
  173. if (ret)
  174. pr_info("gptu: Error registering platform driver\n");
  175. return ret;
  176. }
  177. arch_initcall(gptu_init);