Blame view

drivers/clocksource/h8300_timer8.c 4.4 KB
b24413180   Greg Kroah-Hartman   License cleanup: ...
1
  // SPDX-License-Identifier: GPL-2.0
618b902d8   Yoshinori Sato   h8300: clocksource
2
3
4
5
6
7
8
9
10
11
  /*
   *  linux/arch/h8300/kernel/cpu/timer/timer8.c
   *
   *  Yoshinori Sato <ysato@users.sourcefoge.jp>
   *
   *  8bit Timer driver
   *
   */
  
  #include <linux/errno.h>
618b902d8   Yoshinori Sato   h8300: clocksource
12
13
14
  #include <linux/kernel.h>
  #include <linux/interrupt.h>
  #include <linux/init.h>
618b902d8   Yoshinori Sato   h8300: clocksource
15
  #include <linux/clockchips.h>
618b902d8   Yoshinori Sato   h8300: clocksource
16
17
18
  #include <linux/clk.h>
  #include <linux/io.h>
  #include <linux/of.h>
4633f4cac   Yoshinori Sato   clocksource/drive...
19
20
  #include <linux/of_address.h>
  #include <linux/of_irq.h>
618b902d8   Yoshinori Sato   h8300: clocksource
21

618b902d8   Yoshinori Sato   h8300: clocksource
22
23
24
25
26
  #define _8TCR	0
  #define _8TCSR	2
  #define TCORA	4
  #define TCORB	6
  #define _8TCNT	8
d33f250af   Yoshinori Sato   clocksource/drive...
27
28
  #define CMIEA	6
  #define CMFA	6
618b902d8   Yoshinori Sato   h8300: clocksource
29
  #define FLAG_STARTED (1 << 3)
4633f4cac   Yoshinori Sato   clocksource/drive...
30
  #define SCALE 64
d33f250af   Yoshinori Sato   clocksource/drive...
31
32
  #define bset(b, a) iowrite8(ioread8(a) | (1 << (b)), (a))
  #define bclr(b, a) iowrite8(ioread8(a) & ~(1 << (b)), (a))
618b902d8   Yoshinori Sato   h8300: clocksource
33
  struct timer8_priv {
618b902d8   Yoshinori Sato   h8300: clocksource
34
  	struct clock_event_device ced;
751605152   Daniel Lezcano   h8300: Rename ctl...
35
  	void __iomem *mapbase;
618b902d8   Yoshinori Sato   h8300: clocksource
36
37
  	unsigned long flags;
  	unsigned int rate;
618b902d8   Yoshinori Sato   h8300: clocksource
38
  };
618b902d8   Yoshinori Sato   h8300: clocksource
39
40
41
  static irqreturn_t timer8_interrupt(int irq, void *dev_id)
  {
  	struct timer8_priv *p = dev_id;
7053fdac7   Daniel Lezcano   clocksource/drive...
42
  	if (clockevent_state_oneshot(&p->ced))
d33f250af   Yoshinori Sato   clocksource/drive...
43
  		iowrite16be(0x0000, p->mapbase + _8TCR);
7053fdac7   Daniel Lezcano   clocksource/drive...
44
45
  
  	p->ced.event_handler(&p->ced);
618b902d8   Yoshinori Sato   h8300: clocksource
46

d33f250af   Yoshinori Sato   clocksource/drive...
47
  	bclr(CMFA, p->mapbase + _8TCSR);
f37632d1e   Yoshinori Sato   clocksource/drive...
48

618b902d8   Yoshinori Sato   h8300: clocksource
49
50
51
52
53
  	return IRQ_HANDLED;
  }
  
  static void timer8_set_next(struct timer8_priv *p, unsigned long delta)
  {
618b902d8   Yoshinori Sato   h8300: clocksource
54
  	if (delta >= 0x10000)
8c09b7d6b   Daniel Lezcano   clocksource/drive...
55
56
  		pr_warn("delta out of range
  ");
d33f250af   Yoshinori Sato   clocksource/drive...
57
58
59
60
61
  	bclr(CMIEA, p->mapbase + _8TCR);
  	iowrite16be(delta, p->mapbase + TCORA);
  	iowrite16be(0x0000, p->mapbase + _8TCNT);
  	bclr(CMFA, p->mapbase + _8TCSR);
  	bset(CMIEA, p->mapbase + _8TCR);
618b902d8   Yoshinori Sato   h8300: clocksource
62
63
64
65
  }
  
  static int timer8_enable(struct timer8_priv *p)
  {
d33f250af   Yoshinori Sato   clocksource/drive...
66
67
68
  	iowrite16be(0xffff, p->mapbase + TCORA);
  	iowrite16be(0x0000, p->mapbase + _8TCNT);
  	iowrite16be(0x0c02, p->mapbase + _8TCR);
618b902d8   Yoshinori Sato   h8300: clocksource
69
70
71
72
73
74
  
  	return 0;
  }
  
  static int timer8_start(struct timer8_priv *p)
  {
cce483e0e   Daniel Lezcano   clocksource/drive...
75
  	int ret;
618b902d8   Yoshinori Sato   h8300: clocksource
76

cce483e0e   Daniel Lezcano   clocksource/drive...
77
78
  	if ((p->flags & FLAG_STARTED))
  		return 0;
618b902d8   Yoshinori Sato   h8300: clocksource
79

cce483e0e   Daniel Lezcano   clocksource/drive...
80
81
82
  	ret = timer8_enable(p);
  	if (!ret)
  		p->flags |= FLAG_STARTED;
618b902d8   Yoshinori Sato   h8300: clocksource
83

618b902d8   Yoshinori Sato   h8300: clocksource
84
85
86
87
88
  	return ret;
  }
  
  static void timer8_stop(struct timer8_priv *p)
  {
d33f250af   Yoshinori Sato   clocksource/drive...
89
  	iowrite16be(0x0000, p->mapbase + _8TCR);
618b902d8   Yoshinori Sato   h8300: clocksource
90
91
92
93
94
95
  }
  
  static inline struct timer8_priv *ced_to_priv(struct clock_event_device *ced)
  {
  	return container_of(ced, struct timer8_priv, ced);
  }
1f058d52b   Daniel Lezcano   clocksource/drive...
96
  static void timer8_clock_event_start(struct timer8_priv *p, unsigned long delta)
618b902d8   Yoshinori Sato   h8300: clocksource
97
  {
618b902d8   Yoshinori Sato   h8300: clocksource
98
  	timer8_start(p);
1f058d52b   Daniel Lezcano   clocksource/drive...
99
  	timer8_set_next(p, delta);
618b902d8   Yoshinori Sato   h8300: clocksource
100
  }
fc2b2f5df   Viresh Kumar   clockevents/drive...
101
102
103
104
105
106
107
  static int timer8_clock_event_shutdown(struct clock_event_device *ced)
  {
  	timer8_stop(ced_to_priv(ced));
  	return 0;
  }
  
  static int timer8_clock_event_periodic(struct clock_event_device *ced)
618b902d8   Yoshinori Sato   h8300: clocksource
108
109
  {
  	struct timer8_priv *p = ced_to_priv(ced);
4633f4cac   Yoshinori Sato   clocksource/drive...
110
111
  	pr_info("%s: used for periodic clock events
  ", ced->name);
fc2b2f5df   Viresh Kumar   clockevents/drive...
112
  	timer8_stop(p);
1f058d52b   Daniel Lezcano   clocksource/drive...
113
  	timer8_clock_event_start(p, (p->rate + HZ/2) / HZ);
fc2b2f5df   Viresh Kumar   clockevents/drive...
114
115
116
117
118
119
120
  
  	return 0;
  }
  
  static int timer8_clock_event_oneshot(struct clock_event_device *ced)
  {
  	struct timer8_priv *p = ced_to_priv(ced);
4633f4cac   Yoshinori Sato   clocksource/drive...
121
122
  	pr_info("%s: used for oneshot clock events
  ", ced->name);
fc2b2f5df   Viresh Kumar   clockevents/drive...
123
  	timer8_stop(p);
1f058d52b   Daniel Lezcano   clocksource/drive...
124
  	timer8_clock_event_start(p, 0x10000);
fc2b2f5df   Viresh Kumar   clockevents/drive...
125
126
  
  	return 0;
618b902d8   Yoshinori Sato   h8300: clocksource
127
128
129
130
131
132
  }
  
  static int timer8_clock_event_next(unsigned long delta,
  				   struct clock_event_device *ced)
  {
  	struct timer8_priv *p = ced_to_priv(ced);
fc2b2f5df   Viresh Kumar   clockevents/drive...
133
  	BUG_ON(!clockevent_state_oneshot(ced));
618b902d8   Yoshinori Sato   h8300: clocksource
134
135
136
137
  	timer8_set_next(p, delta - 1);
  
  	return 0;
  }
4633f4cac   Yoshinori Sato   clocksource/drive...
138
139
140
141
142
143
144
145
146
147
148
  static struct timer8_priv timer8_priv = {
  	.ced = {
  		.name = "h8300_8timer",
  		.features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
  		.rating = 200,
  		.set_next_event = timer8_clock_event_next,
  		.set_state_shutdown = timer8_clock_event_shutdown,
  		.set_state_periodic = timer8_clock_event_periodic,
  		.set_state_oneshot = timer8_clock_event_oneshot,
  	},
  };
691f8f878   Daniel Lezcano   clocksource/drive...
149
  static int __init h8300_8timer_init(struct device_node *node)
618b902d8   Yoshinori Sato   h8300: clocksource
150
  {
4633f4cac   Yoshinori Sato   clocksource/drive...
151
  	void __iomem *base;
691f8f878   Daniel Lezcano   clocksource/drive...
152
  	int irq, ret;
4633f4cac   Yoshinori Sato   clocksource/drive...
153
  	struct clk *clk;
618b902d8   Yoshinori Sato   h8300: clocksource
154

4633f4cac   Yoshinori Sato   clocksource/drive...
155
156
157
158
  	clk = of_clk_get(node, 0);
  	if (IS_ERR(clk)) {
  		pr_err("failed to get clock for clockevent
  ");
691f8f878   Daniel Lezcano   clocksource/drive...
159
  		return PTR_ERR(clk);
4633f4cac   Yoshinori Sato   clocksource/drive...
160
  	}
618b902d8   Yoshinori Sato   h8300: clocksource
161

400d033f5   Tianjia Zhang   clocksource/drive...
162
  	ret = -ENXIO;
4633f4cac   Yoshinori Sato   clocksource/drive...
163
164
165
166
167
  	base = of_iomap(node, 0);
  	if (!base) {
  		pr_err("failed to map registers for clockevent
  ");
  		goto free_clk;
618b902d8   Yoshinori Sato   h8300: clocksource
168
  	}
691f8f878   Daniel Lezcano   clocksource/drive...
169
  	ret = -EINVAL;
4633f4cac   Yoshinori Sato   clocksource/drive...
170
  	irq = irq_of_parse_and_map(node, 0);
54a0cd5a7   Daniel Lezcano   clocksource/drive...
171
  	if (!irq) {
4633f4cac   Yoshinori Sato   clocksource/drive...
172
173
174
  		pr_err("failed to get irq for clockevent
  ");
  		goto unmap_reg;
618b902d8   Yoshinori Sato   h8300: clocksource
175
  	}
751605152   Daniel Lezcano   h8300: Rename ctl...
176
  	timer8_priv.mapbase = base;
cce483e0e   Daniel Lezcano   clocksource/drive...
177

6f2b611db   Yoshinori Sato   clocksource/drive...
178
179
  	timer8_priv.rate = clk_get_rate(clk) / SCALE;
  	if (!timer8_priv.rate) {
cce483e0e   Daniel Lezcano   clocksource/drive...
180
181
182
183
  		pr_err("Failed to get rate for the clocksource
  ");
  		goto unmap_reg;
  	}
618b902d8   Yoshinori Sato   h8300: clocksource
184

6f2b611db   Yoshinori Sato   clocksource/drive...
185
186
  	if (request_irq(irq, timer8_interrupt, IRQF_TIMER,
  			timer8_priv.ced.name, &timer8_priv) < 0) {
4633f4cac   Yoshinori Sato   clocksource/drive...
187
188
189
  		pr_err("failed to request irq %d for clockevent
  ", irq);
  		goto unmap_reg;
618b902d8   Yoshinori Sato   h8300: clocksource
190
  	}
cce483e0e   Daniel Lezcano   clocksource/drive...
191

6f2b611db   Yoshinori Sato   clocksource/drive...
192
193
  	clockevents_config_and_register(&timer8_priv.ced,
  					timer8_priv.rate, 1, 0x0000ffff);
4633f4cac   Yoshinori Sato   clocksource/drive...
194

691f8f878   Daniel Lezcano   clocksource/drive...
195
  	return 0;
4633f4cac   Yoshinori Sato   clocksource/drive...
196
197
198
199
  unmap_reg:
  	iounmap(base);
  free_clk:
  	clk_put(clk);
691f8f878   Daniel Lezcano   clocksource/drive...
200
  	return ret;
618b902d8   Yoshinori Sato   h8300: clocksource
201
  }
172733959   Daniel Lezcano   clocksource/drive...
202
  TIMER_OF_DECLARE(h8300_8bit, "renesas,8bit-timer", h8300_8timer_init);