ingenic-ost.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * JZ47xx SoCs TCU Operating System Timer driver
  4. *
  5. * Copyright (C) 2016 Maarten ter Huurne <[email protected]>
  6. * Copyright (C) 2020 Paul Cercueil <[email protected]>
  7. */
  8. #include <linux/clk.h>
  9. #include <linux/clocksource.h>
  10. #include <linux/mfd/ingenic-tcu.h>
  11. #include <linux/mfd/syscon.h>
  12. #include <linux/of.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/pm.h>
  15. #include <linux/regmap.h>
  16. #include <linux/sched_clock.h>
  17. #define TCU_OST_TCSR_MASK 0xffc0
  18. #define TCU_OST_TCSR_CNT_MD BIT(15)
  19. #define TCU_OST_CHANNEL 15
  20. /*
  21. * The TCU_REG_OST_CNT{L,R} from <linux/mfd/ingenic-tcu.h> are only for the
  22. * regmap; these are for use with the __iomem pointer.
  23. */
  24. #define OST_REG_CNTL 0x4
  25. #define OST_REG_CNTH 0x8
  26. struct ingenic_ost_soc_info {
  27. bool is64bit;
  28. };
  29. struct ingenic_ost {
  30. void __iomem *regs;
  31. struct clk *clk;
  32. struct clocksource cs;
  33. };
  34. static struct ingenic_ost *ingenic_ost;
  35. static u64 notrace ingenic_ost_read_cntl(void)
  36. {
  37. /* Read using __iomem pointer instead of regmap to avoid locking */
  38. return readl(ingenic_ost->regs + OST_REG_CNTL);
  39. }
  40. static u64 notrace ingenic_ost_read_cnth(void)
  41. {
  42. /* Read using __iomem pointer instead of regmap to avoid locking */
  43. return readl(ingenic_ost->regs + OST_REG_CNTH);
  44. }
  45. static u64 notrace ingenic_ost_clocksource_readl(struct clocksource *cs)
  46. {
  47. return ingenic_ost_read_cntl();
  48. }
  49. static u64 notrace ingenic_ost_clocksource_readh(struct clocksource *cs)
  50. {
  51. return ingenic_ost_read_cnth();
  52. }
  53. static int __init ingenic_ost_probe(struct platform_device *pdev)
  54. {
  55. const struct ingenic_ost_soc_info *soc_info;
  56. struct device *dev = &pdev->dev;
  57. struct ingenic_ost *ost;
  58. struct clocksource *cs;
  59. struct regmap *map;
  60. unsigned long rate;
  61. int err;
  62. soc_info = device_get_match_data(dev);
  63. if (!soc_info)
  64. return -EINVAL;
  65. ost = devm_kzalloc(dev, sizeof(*ost), GFP_KERNEL);
  66. if (!ost)
  67. return -ENOMEM;
  68. ingenic_ost = ost;
  69. ost->regs = devm_platform_ioremap_resource(pdev, 0);
  70. if (IS_ERR(ost->regs))
  71. return PTR_ERR(ost->regs);
  72. map = device_node_to_regmap(dev->parent->of_node);
  73. if (IS_ERR(map)) {
  74. dev_err(dev, "regmap not found");
  75. return PTR_ERR(map);
  76. }
  77. ost->clk = devm_clk_get(dev, "ost");
  78. if (IS_ERR(ost->clk))
  79. return PTR_ERR(ost->clk);
  80. err = clk_prepare_enable(ost->clk);
  81. if (err)
  82. return err;
  83. /* Clear counter high/low registers */
  84. if (soc_info->is64bit)
  85. regmap_write(map, TCU_REG_OST_CNTL, 0);
  86. regmap_write(map, TCU_REG_OST_CNTH, 0);
  87. /* Don't reset counter at compare value. */
  88. regmap_update_bits(map, TCU_REG_OST_TCSR,
  89. TCU_OST_TCSR_MASK, TCU_OST_TCSR_CNT_MD);
  90. rate = clk_get_rate(ost->clk);
  91. /* Enable OST TCU channel */
  92. regmap_write(map, TCU_REG_TESR, BIT(TCU_OST_CHANNEL));
  93. cs = &ost->cs;
  94. cs->name = "ingenic-ost";
  95. cs->rating = 320;
  96. cs->flags = CLOCK_SOURCE_IS_CONTINUOUS;
  97. cs->mask = CLOCKSOURCE_MASK(32);
  98. if (soc_info->is64bit)
  99. cs->read = ingenic_ost_clocksource_readl;
  100. else
  101. cs->read = ingenic_ost_clocksource_readh;
  102. err = clocksource_register_hz(cs, rate);
  103. if (err) {
  104. dev_err(dev, "clocksource registration failed");
  105. clk_disable_unprepare(ost->clk);
  106. return err;
  107. }
  108. if (soc_info->is64bit)
  109. sched_clock_register(ingenic_ost_read_cntl, 32, rate);
  110. else
  111. sched_clock_register(ingenic_ost_read_cnth, 32, rate);
  112. return 0;
  113. }
  114. static int __maybe_unused ingenic_ost_suspend(struct device *dev)
  115. {
  116. struct ingenic_ost *ost = dev_get_drvdata(dev);
  117. clk_disable(ost->clk);
  118. return 0;
  119. }
  120. static int __maybe_unused ingenic_ost_resume(struct device *dev)
  121. {
  122. struct ingenic_ost *ost = dev_get_drvdata(dev);
  123. return clk_enable(ost->clk);
  124. }
  125. static const struct dev_pm_ops __maybe_unused ingenic_ost_pm_ops = {
  126. /* _noirq: We want the OST clock to be gated last / ungated first */
  127. .suspend_noirq = ingenic_ost_suspend,
  128. .resume_noirq = ingenic_ost_resume,
  129. };
  130. static const struct ingenic_ost_soc_info jz4725b_ost_soc_info = {
  131. .is64bit = false,
  132. };
  133. static const struct ingenic_ost_soc_info jz4760b_ost_soc_info = {
  134. .is64bit = true,
  135. };
  136. static const struct of_device_id ingenic_ost_of_match[] = {
  137. { .compatible = "ingenic,jz4725b-ost", .data = &jz4725b_ost_soc_info, },
  138. { .compatible = "ingenic,jz4760b-ost", .data = &jz4760b_ost_soc_info, },
  139. { .compatible = "ingenic,jz4770-ost", .data = &jz4760b_ost_soc_info, },
  140. { }
  141. };
  142. static struct platform_driver ingenic_ost_driver = {
  143. .driver = {
  144. .name = "ingenic-ost",
  145. #ifdef CONFIG_PM_SUSPEND
  146. .pm = &ingenic_ost_pm_ops,
  147. #endif
  148. .of_match_table = ingenic_ost_of_match,
  149. },
  150. };
  151. builtin_platform_driver_probe(ingenic_ost_driver, ingenic_ost_probe);