accel/amdxdna: Return error when setting clock failed for npu1
[drm/drm-misc.git] / drivers / clocksource / timer-ralink.c
blob6ecdb4228f7637662fdaf5362991aa80f7cb867c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Ralink System Tick Counter driver present on RT3352 and MT7620 SoCs.
5 * Copyright (C) 2013 by John Crispin <john@phrozen.org>
6 */
8 #include <linux/clockchips.h>
9 #include <linux/clocksource.h>
10 #include <linux/interrupt.h>
11 #include <linux/reset.h>
12 #include <linux/init.h>
13 #include <linux/time.h>
14 #include <linux/of.h>
15 #include <linux/of_irq.h>
16 #include <linux/of_address.h>
18 #define SYSTICK_FREQ (50 * 1000)
20 #define SYSTICK_CONFIG 0x00
21 #define SYSTICK_COMPARE 0x04
22 #define SYSTICK_COUNT 0x08
24 /* route systick irq to mips irq 7 instead of the r4k-timer */
25 #define CFG_EXT_STK_EN 0x2
26 /* enable the counter */
27 #define CFG_CNT_EN 0x1
29 struct systick_device {
30 void __iomem *membase;
31 struct clock_event_device dev;
32 int irq_requested;
33 int freq_scale;
36 static int systick_set_oneshot(struct clock_event_device *evt);
37 static int systick_shutdown(struct clock_event_device *evt);
39 static int systick_next_event(unsigned long delta,
40 struct clock_event_device *evt)
42 struct systick_device *sdev;
43 u32 count;
45 sdev = container_of(evt, struct systick_device, dev);
46 count = ioread32(sdev->membase + SYSTICK_COUNT);
47 count = (count + delta) % SYSTICK_FREQ;
48 iowrite32(count, sdev->membase + SYSTICK_COMPARE);
50 return 0;
53 static void systick_event_handler(struct clock_event_device *dev)
55 /* noting to do here */
58 static irqreturn_t systick_interrupt(int irq, void *dev_id)
60 struct clock_event_device *dev = (struct clock_event_device *)dev_id;
62 dev->event_handler(dev);
64 return IRQ_HANDLED;
67 static struct systick_device systick = {
68 .dev = {
70 * cevt-r4k uses 300, make sure systick
71 * gets used if available
73 .rating = 310,
74 .features = CLOCK_EVT_FEAT_ONESHOT,
75 .set_next_event = systick_next_event,
76 .set_state_shutdown = systick_shutdown,
77 .set_state_oneshot = systick_set_oneshot,
78 .event_handler = systick_event_handler,
82 static int systick_shutdown(struct clock_event_device *evt)
84 struct systick_device *sdev;
86 sdev = container_of(evt, struct systick_device, dev);
88 if (sdev->irq_requested)
89 free_irq(systick.dev.irq, &systick.dev);
90 sdev->irq_requested = 0;
91 iowrite32(0, systick.membase + SYSTICK_CONFIG);
93 return 0;
96 static int systick_set_oneshot(struct clock_event_device *evt)
98 const char *name = systick.dev.name;
99 struct systick_device *sdev;
100 int irq = systick.dev.irq;
102 sdev = container_of(evt, struct systick_device, dev);
104 if (!sdev->irq_requested) {
105 if (request_irq(irq, systick_interrupt,
106 IRQF_PERCPU | IRQF_TIMER, name, &systick.dev))
107 pr_err("Failed to request irq %d (%s)\n", irq, name);
109 sdev->irq_requested = 1;
110 iowrite32(CFG_EXT_STK_EN | CFG_CNT_EN,
111 systick.membase + SYSTICK_CONFIG);
113 return 0;
116 static int __init ralink_systick_init(struct device_node *np)
118 int ret;
120 systick.membase = of_iomap(np, 0);
121 if (!systick.membase)
122 return -ENXIO;
124 systick.dev.name = np->name;
125 clockevents_calc_mult_shift(&systick.dev, SYSTICK_FREQ, 60);
126 systick.dev.max_delta_ns = clockevent_delta2ns(0x7fff, &systick.dev);
127 systick.dev.max_delta_ticks = 0x7fff;
128 systick.dev.min_delta_ns = clockevent_delta2ns(0x3, &systick.dev);
129 systick.dev.min_delta_ticks = 0x3;
130 systick.dev.irq = irq_of_parse_and_map(np, 0);
131 if (!systick.dev.irq) {
132 pr_err("%pOFn: request_irq failed", np);
133 return -EINVAL;
136 ret = clocksource_mmio_init(systick.membase + SYSTICK_COUNT, np->name,
137 SYSTICK_FREQ, 301, 16,
138 clocksource_mmio_readl_up);
139 if (ret)
140 return ret;
142 clockevents_register_device(&systick.dev);
144 pr_info("%pOFn: running - mult: %d, shift: %d\n",
145 np, systick.dev.mult, systick.dev.shift);
147 return 0;
150 TIMER_OF_DECLARE(systick, "ralink,cevt-systick", ralink_systick_init);