blob: f24eee04e16af6df0a73266d9f346de9636b33d7 [file] [log] [blame]
John Crispin1f2acc52013-08-08 13:08:06 +02001/*
2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
4 * for more details.
5 *
John Crispin97b92102016-05-05 09:57:56 +02006 * Copyright (C) 2013 by John Crispin <john@phrozen.org>
John Crispin1f2acc52013-08-08 13:08:06 +02007 */
8
9#include <linux/clockchips.h>
10#include <linux/clocksource.h>
11#include <linux/interrupt.h>
12#include <linux/reset.h>
13#include <linux/init.h>
14#include <linux/time.h>
15#include <linux/of.h>
16#include <linux/of_irq.h>
17#include <linux/of_address.h>
18
19#include <asm/mach-ralink/ralink_regs.h>
20
21#define SYSTICK_FREQ (50 * 1000)
22
23#define SYSTICK_CONFIG 0x00
24#define SYSTICK_COMPARE 0x04
25#define SYSTICK_COUNT 0x08
26
27/* route systick irq to mips irq 7 instead of the r4k-timer */
28#define CFG_EXT_STK_EN 0x2
29/* enable the counter */
30#define CFG_CNT_EN 0x1
31
32struct systick_device {
33 void __iomem *membase;
34 struct clock_event_device dev;
35 int irq_requested;
36 int freq_scale;
37};
38
Viresh Kumar59113d92015-07-06 16:42:03 +053039static int systick_set_oneshot(struct clock_event_device *evt);
40static int systick_shutdown(struct clock_event_device *evt);
John Crispin1f2acc52013-08-08 13:08:06 +020041
42static int systick_next_event(unsigned long delta,
43 struct clock_event_device *evt)
44{
45 struct systick_device *sdev;
46 u32 count;
47
48 sdev = container_of(evt, struct systick_device, dev);
49 count = ioread32(sdev->membase + SYSTICK_COUNT);
50 count = (count + delta) % SYSTICK_FREQ;
John Crispin37bcc032015-11-04 11:50:10 +010051 iowrite32(count, sdev->membase + SYSTICK_COMPARE);
John Crispin1f2acc52013-08-08 13:08:06 +020052
53 return 0;
54}
55
56static void systick_event_handler(struct clock_event_device *dev)
57{
58 /* noting to do here */
59}
60
61static irqreturn_t systick_interrupt(int irq, void *dev_id)
62{
63 struct clock_event_device *dev = (struct clock_event_device *) dev_id;
64
65 dev->event_handler(dev);
66
67 return IRQ_HANDLED;
68}
69
70static struct systick_device systick = {
71 .dev = {
72 /*
73 * cevt-r4k uses 300, make sure systick
74 * gets used if available
75 */
Viresh Kumar59113d92015-07-06 16:42:03 +053076 .rating = 310,
77 .features = CLOCK_EVT_FEAT_ONESHOT,
78 .set_next_event = systick_next_event,
79 .set_state_shutdown = systick_shutdown,
80 .set_state_oneshot = systick_set_oneshot,
81 .event_handler = systick_event_handler,
John Crispin1f2acc52013-08-08 13:08:06 +020082 },
83};
84
85static struct irqaction systick_irqaction = {
86 .handler = systick_interrupt,
87 .flags = IRQF_PERCPU | IRQF_TIMER,
88 .dev_id = &systick.dev,
89};
90
Viresh Kumar59113d92015-07-06 16:42:03 +053091static int systick_shutdown(struct clock_event_device *evt)
John Crispin1f2acc52013-08-08 13:08:06 +020092{
93 struct systick_device *sdev;
94
95 sdev = container_of(evt, struct systick_device, dev);
96
Viresh Kumar59113d92015-07-06 16:42:03 +053097 if (sdev->irq_requested)
98 free_irq(systick.dev.irq, &systick_irqaction);
99 sdev->irq_requested = 0;
100 iowrite32(0, systick.membase + SYSTICK_CONFIG);
John Crispin1f2acc52013-08-08 13:08:06 +0200101
Viresh Kumar59113d92015-07-06 16:42:03 +0530102 return 0;
103}
John Crispin1f2acc52013-08-08 13:08:06 +0200104
Viresh Kumar59113d92015-07-06 16:42:03 +0530105static int systick_set_oneshot(struct clock_event_device *evt)
106{
107 struct systick_device *sdev;
108
109 sdev = container_of(evt, struct systick_device, dev);
110
111 if (!sdev->irq_requested)
112 setup_irq(systick.dev.irq, &systick_irqaction);
113 sdev->irq_requested = 1;
114 iowrite32(CFG_EXT_STK_EN | CFG_CNT_EN,
115 systick.membase + SYSTICK_CONFIG);
116
117 return 0;
John Crispin1f2acc52013-08-08 13:08:06 +0200118}
119
Daniel Lezcano27126162016-06-07 00:03:49 +0200120static int __init ralink_systick_init(struct device_node *np)
John Crispin1f2acc52013-08-08 13:08:06 +0200121{
Daniel Lezcano27126162016-06-07 00:03:49 +0200122 int ret;
123
John Crispin1f2acc52013-08-08 13:08:06 +0200124 systick.membase = of_iomap(np, 0);
125 if (!systick.membase)
Daniel Lezcano27126162016-06-07 00:03:49 +0200126 return -ENXIO;
John Crispin1f2acc52013-08-08 13:08:06 +0200127
128 systick_irqaction.name = np->name;
129 systick.dev.name = np->name;
130 clockevents_calc_mult_shift(&systick.dev, SYSTICK_FREQ, 60);
131 systick.dev.max_delta_ns = clockevent_delta2ns(0x7fff, &systick.dev);
132 systick.dev.min_delta_ns = clockevent_delta2ns(0x3, &systick.dev);
133 systick.dev.irq = irq_of_parse_and_map(np, 0);
134 if (!systick.dev.irq) {
135 pr_err("%s: request_irq failed", np->name);
Daniel Lezcano27126162016-06-07 00:03:49 +0200136 return -EINVAL;
John Crispin1f2acc52013-08-08 13:08:06 +0200137 }
138
Daniel Lezcano27126162016-06-07 00:03:49 +0200139 ret = clocksource_mmio_init(systick.membase + SYSTICK_COUNT, np->name,
140 SYSTICK_FREQ, 301, 16,
141 clocksource_mmio_readl_up);
142 if (ret)
143 return ret;
John Crispin1f2acc52013-08-08 13:08:06 +0200144
145 clockevents_register_device(&systick.dev);
146
Masanari Iida77d84ff2013-12-09 00:22:53 +0900147 pr_info("%s: running - mult: %d, shift: %d\n",
John Crispin1f2acc52013-08-08 13:08:06 +0200148 np->name, systick.dev.mult, systick.dev.shift);
Daniel Lezcano27126162016-06-07 00:03:49 +0200149
150 return 0;
John Crispin1f2acc52013-08-08 13:08:06 +0200151}
152
Daniel Lezcano177cf6e2016-06-07 00:27:44 +0200153CLOCKSOURCE_OF_DECLARE(systick, "ralink,cevt-systick", ralink_systick_init);