0011-MIPS-ralink-add-support-for-systick-timer-found-on-n.patch 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. From 3b511d972b556712f89ccc68825c0ec8f398dc5c Mon Sep 17 00:00:00 2001
  2. From: John Crispin <[email protected]>
  3. Date: Sun, 28 Jul 2013 13:46:09 +0200
  4. Subject: [PATCH 11/25] MIPS: ralink: add support for systick timer found on
  5. newer ralink SoC
  6. Newer Ralink SoC (MT7620x and RT5350) have a 50KHz clock that runs independent
  7. of the SoC master clock. If we want to automatic frequency scaling to work we
  8. need to use the systick timer as the clock source.
  9. Signed-off-by: John Crispin <[email protected]>
  10. ---
  11. arch/mips/ralink/Kconfig | 7 ++
  12. arch/mips/ralink/Makefile | 2 +
  13. arch/mips/ralink/cevt-rt3352.c | 145 ++++++++++++++++++++++++++++++++++++++++
  14. 3 files changed, 154 insertions(+)
  15. create mode 100644 arch/mips/ralink/cevt-rt3352.c
  16. --- a/arch/mips/ralink/Kconfig
  17. +++ b/arch/mips/ralink/Kconfig
  18. @@ -1,5 +1,12 @@
  19. if RALINK
  20. +config CLKEVT_RT3352
  21. + bool "Systick Clockevent source"
  22. + depends on SOC_RT305X || SOC_MT7620
  23. + default y
  24. + select CLKSRC_OF
  25. + select CLKSRC_MMIO
  26. +
  27. choice
  28. prompt "Ralink SoC selection"
  29. default SOC_RT305X
  30. --- a/arch/mips/ralink/Makefile
  31. +++ b/arch/mips/ralink/Makefile
  32. @@ -8,6 +8,8 @@
  33. obj-y := prom.o of.o reset.o clk.o irq.o timer.o
  34. +obj-$(CONFIG_CLKEVT_RT3352) += cevt-rt3352.o
  35. +
  36. obj-$(CONFIG_SOC_RT288X) += rt288x.o
  37. obj-$(CONFIG_SOC_RT305X) += rt305x.o
  38. obj-$(CONFIG_SOC_RT3883) += rt3883.o
  39. --- /dev/null
  40. +++ b/arch/mips/ralink/cevt-rt3352.c
  41. @@ -0,0 +1,145 @@
  42. +/*
  43. + * This file is subject to the terms and conditions of the GNU General Public
  44. + * License. See the file "COPYING" in the main directory of this archive
  45. + * for more details.
  46. + *
  47. + * Copyright (C) 2013 by John Crispin <[email protected]>
  48. + */
  49. +
  50. +#include <linux/clockchips.h>
  51. +#include <linux/clocksource.h>
  52. +#include <linux/interrupt.h>
  53. +#include <linux/reset.h>
  54. +#include <linux/init.h>
  55. +#include <linux/time.h>
  56. +#include <linux/of.h>
  57. +#include <linux/of_irq.h>
  58. +#include <linux/of_address.h>
  59. +
  60. +#include <asm/mach-ralink/ralink_regs.h>
  61. +
  62. +#define SYSTICK_FREQ (50 * 1000)
  63. +
  64. +#define SYSTICK_CONFIG 0x00
  65. +#define SYSTICK_COMPARE 0x04
  66. +#define SYSTICK_COUNT 0x08
  67. +
  68. +/* route systick irq to mips irq 7 instead of the r4k-timer */
  69. +#define CFG_EXT_STK_EN 0x2
  70. +/* enable the counter */
  71. +#define CFG_CNT_EN 0x1
  72. +
  73. +struct systick_device {
  74. + void __iomem *membase;
  75. + struct clock_event_device dev;
  76. + int irq_requested;
  77. + int freq_scale;
  78. +};
  79. +
  80. +static void systick_set_clock_mode(enum clock_event_mode mode,
  81. + struct clock_event_device *evt);
  82. +
  83. +static int systick_next_event(unsigned long delta,
  84. + struct clock_event_device *evt)
  85. +{
  86. + struct systick_device *sdev;
  87. + u32 count;
  88. +
  89. + sdev = container_of(evt, struct systick_device, dev);
  90. + count = ioread32(sdev->membase + SYSTICK_COUNT);
  91. + count = (count + delta) % SYSTICK_FREQ;
  92. + iowrite32(count + delta, sdev->membase + SYSTICK_COMPARE);
  93. +
  94. + return 0;
  95. +}
  96. +
  97. +static void systick_event_handler(struct clock_event_device *dev)
  98. +{
  99. + /* noting to do here */
  100. +}
  101. +
  102. +static irqreturn_t systick_interrupt(int irq, void *dev_id)
  103. +{
  104. + struct clock_event_device *dev = (struct clock_event_device *) dev_id;
  105. +
  106. + dev->event_handler(dev);
  107. +
  108. + return IRQ_HANDLED;
  109. +}
  110. +
  111. +static struct systick_device systick = {
  112. + .dev = {
  113. + /*
  114. + * cevt-r4k uses 300, make sure systick
  115. + * gets used if available
  116. + */
  117. + .rating = 310,
  118. + .features = CLOCK_EVT_FEAT_ONESHOT,
  119. + .set_next_event = systick_next_event,
  120. + .set_mode = systick_set_clock_mode,
  121. + .event_handler = systick_event_handler,
  122. + },
  123. +};
  124. +
  125. +static struct irqaction systick_irqaction = {
  126. + .handler = systick_interrupt,
  127. + .flags = IRQF_PERCPU | IRQF_TIMER,
  128. + .dev_id = &systick.dev,
  129. +};
  130. +
  131. +static void systick_set_clock_mode(enum clock_event_mode mode,
  132. + struct clock_event_device *evt)
  133. +{
  134. + struct systick_device *sdev;
  135. +
  136. + sdev = container_of(evt, struct systick_device, dev);
  137. +
  138. + switch (mode) {
  139. + case CLOCK_EVT_MODE_ONESHOT:
  140. + if (!sdev->irq_requested)
  141. + setup_irq(systick.dev.irq, &systick_irqaction);
  142. + sdev->irq_requested = 1;
  143. + iowrite32(CFG_EXT_STK_EN | CFG_CNT_EN,
  144. + systick.membase + SYSTICK_CONFIG);
  145. + break;
  146. +
  147. + case CLOCK_EVT_MODE_SHUTDOWN:
  148. + if (sdev->irq_requested)
  149. + free_irq(systick.dev.irq, &systick_irqaction);
  150. + sdev->irq_requested = 0;
  151. + iowrite32(0, systick.membase + SYSTICK_CONFIG);
  152. + break;
  153. +
  154. + default:
  155. + pr_err("%s: Unhandeled mips clock_mode\n", systick.dev.name);
  156. + break;
  157. + }
  158. +}
  159. +
  160. +static void __init ralink_systick_init(struct device_node *np)
  161. +{
  162. + systick.membase = of_iomap(np, 0);
  163. + if (!systick.membase)
  164. + return;
  165. +
  166. + systick_irqaction.name = np->name;
  167. + systick.dev.name = np->name;
  168. + clockevents_calc_mult_shift(&systick.dev, SYSTICK_FREQ, 60);
  169. + systick.dev.max_delta_ns = clockevent_delta2ns(0x7fff, &systick.dev);
  170. + systick.dev.min_delta_ns = clockevent_delta2ns(0x3, &systick.dev);
  171. + systick.dev.irq = irq_of_parse_and_map(np, 0);
  172. + if (!systick.dev.irq) {
  173. + pr_err("%s: request_irq failed", np->name);
  174. + return;
  175. + }
  176. +
  177. + clocksource_mmio_init(systick.membase + SYSTICK_COUNT, np->name,
  178. + SYSTICK_FREQ, 301, 16, clocksource_mmio_readl_up);
  179. +
  180. + clockevents_register_device(&systick.dev);
  181. +
  182. + pr_info("%s: runing - mult: %d, shift: %d\n",
  183. + np->name, systick.dev.mult, systick.dev.shift);
  184. +}
  185. +
  186. +CLOCKSOURCE_OF_DECLARE(systick, "ralink,cevt-systick", ralink_systick_init);