gptu.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. *
  4. * Copyright (C) 2012 John Crispin <[email protected]>
  5. * Copyright (C) 2012 Lantiq GmbH
  6. */
  7. #include <linux/interrupt.h>
  8. #include <linux/ioport.h>
  9. #include <linux/init.h>
  10. #include <linux/of_platform.h>
  11. #include <linux/of_irq.h>
  12. #include <lantiq_soc.h>
  13. #include "../clk.h"
  14. /* the magic ID byte of the core */
  15. #define GPTU_MAGIC 0x59
  16. /* clock control register */
  17. #define GPTU_CLC 0x00
  18. /* id register */
  19. #define GPTU_ID 0x08
  20. /* interrupt node enable */
  21. #define GPTU_IRNEN 0xf4
  22. /* interrupt control register */
  23. #define GPTU_IRCR 0xf8
  24. /* interrupt capture register */
  25. #define GPTU_IRNCR 0xfc
  26. /* there are 3 identical blocks of 2 timers. calculate register offsets */
  27. #define GPTU_SHIFT(x) (x % 2 ? 4 : 0)
  28. #define GPTU_BASE(x) (((x >> 1) * 0x20) + 0x10)
  29. /* timer control register */
  30. #define GPTU_CON(x) (GPTU_BASE(x) + GPTU_SHIFT(x) + 0x00)
  31. /* timer auto reload register */
  32. #define GPTU_RUN(x) (GPTU_BASE(x) + GPTU_SHIFT(x) + 0x08)
  33. /* timer manual reload register */
  34. #define GPTU_RLD(x) (GPTU_BASE(x) + GPTU_SHIFT(x) + 0x10)
  35. /* timer count register */
  36. #define GPTU_CNT(x) (GPTU_BASE(x) + GPTU_SHIFT(x) + 0x18)
  37. /* GPTU_CON(x) */
  38. #define CON_CNT BIT(2)
  39. #define CON_EDGE_ANY (BIT(7) | BIT(6))
  40. #define CON_SYNC BIT(8)
  41. #define CON_CLK_INT BIT(10)
  42. /* GPTU_RUN(x) */
  43. #define RUN_SEN BIT(0)
  44. #define RUN_RL BIT(2)
  45. /* set clock to runmode */
  46. #define CLC_RMC BIT(8)
  47. /* bring core out of suspend */
  48. #define CLC_SUSPEND BIT(4)
  49. /* the disable bit */
  50. #define CLC_DISABLE BIT(0)
  51. #define gptu_w32(x, y) ltq_w32((x), gptu_membase + (y))
  52. #define gptu_r32(x) ltq_r32(gptu_membase + (x))
  53. enum gptu_timer {
  54. TIMER1A = 0,
  55. TIMER1B,
  56. TIMER2A,
  57. TIMER2B,
  58. TIMER3A,
  59. TIMER3B
  60. };
  61. static void __iomem *gptu_membase;
  62. static struct resource irqres[6];
  63. static irqreturn_t timer_irq_handler(int irq, void *priv)
  64. {
  65. int timer = irq - irqres[0].start;
  66. gptu_w32(1 << timer, GPTU_IRNCR);
  67. return IRQ_HANDLED;
  68. }
  69. static void gptu_hwinit(void)
  70. {
  71. gptu_w32(0x00, GPTU_IRNEN);
  72. gptu_w32(0xff, GPTU_IRNCR);
  73. gptu_w32(CLC_RMC | CLC_SUSPEND, GPTU_CLC);
  74. }
  75. static void gptu_hwexit(void)
  76. {
  77. gptu_w32(0x00, GPTU_IRNEN);
  78. gptu_w32(0xff, GPTU_IRNCR);
  79. gptu_w32(CLC_DISABLE, GPTU_CLC);
  80. }
  81. static int gptu_enable(struct clk *clk)
  82. {
  83. int ret = request_irq(irqres[clk->bits].start, timer_irq_handler,
  84. IRQF_TIMER, "gtpu", NULL);
  85. if (ret) {
  86. pr_err("gptu: failed to request irq\n");
  87. return ret;
  88. }
  89. gptu_w32(CON_CNT | CON_EDGE_ANY | CON_SYNC | CON_CLK_INT,
  90. GPTU_CON(clk->bits));
  91. gptu_w32(1, GPTU_RLD(clk->bits));
  92. gptu_w32(gptu_r32(GPTU_IRNEN) | BIT(clk->bits), GPTU_IRNEN);
  93. gptu_w32(RUN_SEN | RUN_RL, GPTU_RUN(clk->bits));
  94. return 0;
  95. }
  96. static void gptu_disable(struct clk *clk)
  97. {
  98. gptu_w32(0, GPTU_RUN(clk->bits));
  99. gptu_w32(0, GPTU_CON(clk->bits));
  100. gptu_w32(0, GPTU_RLD(clk->bits));
  101. gptu_w32(gptu_r32(GPTU_IRNEN) & ~BIT(clk->bits), GPTU_IRNEN);
  102. free_irq(irqres[clk->bits].start, NULL);
  103. }
  104. static inline void clkdev_add_gptu(struct device *dev, const char *con,
  105. unsigned int timer)
  106. {
  107. struct clk *clk = kzalloc(sizeof(struct clk), GFP_KERNEL);
  108. if (!clk)
  109. return;
  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. static struct platform_driver dma_driver = {
  163. .probe = gptu_probe,
  164. .driver = {
  165. .name = "gptu-xway",
  166. .of_match_table = gptu_match,
  167. },
  168. };
  169. int __init gptu_init(void)
  170. {
  171. int ret = platform_driver_register(&dma_driver);
  172. if (ret)
  173. pr_info("gptu: Error registering platform driver\n");
  174. return ret;
  175. }
  176. arch_initcall(gptu_init);