timer-ti-32k.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /**
  2. * timer-ti-32k.c - OMAP2 32k Timer Support
  3. *
  4. * Copyright (C) 2009 Nokia Corporation
  5. *
  6. * Update to use new clocksource/clockevent layers
  7. * Author: Kevin Hilman, MontaVista Software, Inc. <source@mvista.com>
  8. * Copyright (C) 2007 MontaVista Software, Inc.
  9. *
  10. * Original driver:
  11. * Copyright (C) 2005 Nokia Corporation
  12. * Author: Paul Mundt <paul.mundt@nokia.com>
  13. * Juha Yrjölä <juha.yrjola@nokia.com>
  14. * OMAP Dual-mode timer framework support by Timo Teras
  15. *
  16. * Some parts based off of TI's 24xx code:
  17. *
  18. * Copyright (C) 2004-2009 Texas Instruments, Inc.
  19. *
  20. * Roughly modelled after the OMAP1 MPU timer code.
  21. * Added OMAP4 support - Santosh Shilimkar <santosh.shilimkar@ti.com>
  22. *
  23. * Copyright (C) 2015 Texas Instruments Incorporated - http://www.ti.com
  24. *
  25. * This program is free software: you can redistribute it and/or modify
  26. * it under the terms of the GNU General Public License version 2 of
  27. * the License as published by the Free Software Foundation.
  28. *
  29. * This program is distributed in the hope that it will be useful,
  30. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  31. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  32. * GNU General Public License for more details.
  33. *
  34. * You should have received a copy of the GNU General Public License
  35. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  36. */
  37. #include <linux/init.h>
  38. #include <linux/time.h>
  39. #include <linux/sched_clock.h>
  40. #include <linux/clocksource.h>
  41. #include <linux/of.h>
  42. #include <linux/of_address.h>
  43. /*
  44. * 32KHz clocksource ... always available, on pretty most chips except
  45. * OMAP 730 and 1510. Other timers could be used as clocksources, with
  46. * higher resolution in free-running counter modes (e.g. 12 MHz xtal),
  47. * but systems won't necessarily want to spend resources that way.
  48. */
  49. #define OMAP2_32KSYNCNT_REV_OFF 0x0
  50. #define OMAP2_32KSYNCNT_REV_SCHEME (0x3 << 30)
  51. #define OMAP2_32KSYNCNT_CR_OFF_LOW 0x10
  52. #define OMAP2_32KSYNCNT_CR_OFF_HIGH 0x30
  53. struct ti_32k {
  54. void __iomem *base;
  55. void __iomem *counter;
  56. struct clocksource cs;
  57. };
  58. static inline struct ti_32k *to_ti_32k(struct clocksource *cs)
  59. {
  60. return container_of(cs, struct ti_32k, cs);
  61. }
  62. static cycle_t notrace ti_32k_read_cycles(struct clocksource *cs)
  63. {
  64. struct ti_32k *ti = to_ti_32k(cs);
  65. return (cycle_t)readl_relaxed(ti->counter);
  66. }
  67. static struct ti_32k ti_32k_timer = {
  68. .cs = {
  69. .name = "32k_counter",
  70. .rating = 250,
  71. .read = ti_32k_read_cycles,
  72. .mask = CLOCKSOURCE_MASK(32),
  73. .flags = CLOCK_SOURCE_IS_CONTINUOUS |
  74. CLOCK_SOURCE_SUSPEND_NONSTOP,
  75. },
  76. };
  77. static u64 notrace omap_32k_read_sched_clock(void)
  78. {
  79. return ti_32k_read_cycles(&ti_32k_timer.cs);
  80. }
  81. static int __init ti_32k_timer_init(struct device_node *np)
  82. {
  83. int ret;
  84. ti_32k_timer.base = of_iomap(np, 0);
  85. if (!ti_32k_timer.base) {
  86. pr_err("Can't ioremap 32k timer base\n");
  87. return -ENXIO;
  88. }
  89. ti_32k_timer.counter = ti_32k_timer.base;
  90. /*
  91. * 32k sync Counter IP register offsets vary between the highlander
  92. * version and the legacy ones.
  93. *
  94. * The 'SCHEME' bits(30-31) of the revision register is used to identify
  95. * the version.
  96. */
  97. if (readl_relaxed(ti_32k_timer.base + OMAP2_32KSYNCNT_REV_OFF) &
  98. OMAP2_32KSYNCNT_REV_SCHEME)
  99. ti_32k_timer.counter += OMAP2_32KSYNCNT_CR_OFF_HIGH;
  100. else
  101. ti_32k_timer.counter += OMAP2_32KSYNCNT_CR_OFF_LOW;
  102. ret = clocksource_register_hz(&ti_32k_timer.cs, 32768);
  103. if (ret) {
  104. pr_err("32k_counter: can't register clocksource\n");
  105. return ret;
  106. }
  107. sched_clock_register(omap_32k_read_sched_clock, 32, 32768);
  108. pr_info("OMAP clocksource: 32k_counter at 32768 Hz\n");
  109. return 0;
  110. }
  111. CLOCKSOURCE_OF_DECLARE(ti_32k_timer, "ti,omap-counter32k",
  112. ti_32k_timer_init);