1 /*
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2012 Oleksandr Tymoshenko <gonzo@freebsd.org>
5  * Copyright (c) 2012 Damjan Marion <dmarion@freebsd.org>
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/bus.h>
33 #include <sys/kernel.h>
34 #include <sys/module.h>
35 #include <sys/malloc.h>
36 #include <sys/rman.h>
37 #include <sys/timeet.h>
38 #include <sys/timetc.h>
39 #include <sys/watchdog.h>
40 #include <machine/bus.h>
41 #include <machine/cpu.h>
42 #include <machine/intr.h>
43 #include <machine/machdep.h>
44 
45 #include <dev/ofw/openfirm.h>
46 #include <dev/ofw/ofw_bus.h>
47 #include <dev/ofw/ofw_bus_subr.h>
48 
49 #include <machine/bus.h>
50 
51 #define	BCM2835_NUM_TIMERS	4
52 
53 #define	DEFAULT_TIMER		3
54 #define	DEFAULT_TIMER_NAME	"BCM2835-3"
55 #define	DEFAULT_FREQUENCY	1000000
56 #define	MIN_PERIOD		5LLU
57 
58 #define	SYSTIMER_CS	0x00
59 #define	SYSTIMER_CLO	0x04
60 #define	SYSTIMER_CHI	0x08
61 #define	SYSTIMER_C0	0x0C
62 #define	SYSTIMER_C1	0x10
63 #define	SYSTIMER_C2	0x14
64 #define	SYSTIMER_C3	0x18
65 
66 struct systimer {
67 	int			index;
68 	bool			enabled;
69 	struct eventtimer	et;
70 };
71 
72 struct bcm_systimer_softc {
73 	struct resource*	mem_res;
74 	struct resource*	irq_res[BCM2835_NUM_TIMERS];
75 	void*			intr_hl[BCM2835_NUM_TIMERS];
76 	uint32_t		sysclk_freq;
77 	bus_space_tag_t		bst;
78 	bus_space_handle_t	bsh;
79 	struct systimer		st[BCM2835_NUM_TIMERS];
80 };
81 
82 static struct resource_spec bcm_systimer_irq_spec[] = {
83 	{ SYS_RES_IRQ,      0,  RF_ACTIVE },
84 	{ SYS_RES_IRQ,      1,  RF_ACTIVE },
85 	{ SYS_RES_IRQ,      2,  RF_ACTIVE },
86 	{ SYS_RES_IRQ,      3,  RF_ACTIVE },
87 	{ -1,               0,  0 }
88 };
89 
90 static struct ofw_compat_data compat_data[] = {
91 	{"broadcom,bcm2835-system-timer",	1},
92 	{"brcm,bcm2835-system-timer",		1},
93 	{NULL,					0}
94 };
95 
96 static struct bcm_systimer_softc *bcm_systimer_sc = NULL;
97 
98 /* Read/Write macros for Timer used as timecounter */
99 #define bcm_systimer_tc_read_4(reg)		\
100 	bus_space_read_4(bcm_systimer_sc->bst, \
101 		bcm_systimer_sc->bsh, reg)
102 
103 #define bcm_systimer_tc_write_4(reg, val)	\
104 	bus_space_write_4(bcm_systimer_sc->bst, \
105 		bcm_systimer_sc->bsh, reg, val)
106 
107 static unsigned bcm_systimer_tc_get_timecount(struct timecounter *);
108 
109 static delay_func bcm_systimer_delay;
110 
111 static struct timecounter bcm_systimer_tc = {
112 	.tc_name           = DEFAULT_TIMER_NAME,
113 	.tc_get_timecount  = bcm_systimer_tc_get_timecount,
114 	.tc_poll_pps       = NULL,
115 	.tc_counter_mask   = ~0u,
116 	.tc_frequency      = 0,
117 	.tc_quality        = 1000,
118 };
119 
120 static unsigned
121 bcm_systimer_tc_get_timecount(struct timecounter *tc)
122 {
123 	if (bcm_systimer_sc == NULL)
124 		return (0);
125 
126 	return bcm_systimer_tc_read_4(SYSTIMER_CLO);
127 }
128 
129 static int
130 bcm_systimer_start(struct eventtimer *et, sbintime_t first, sbintime_t period)
131 {
132 	struct systimer *st = et->et_priv;
133 	uint32_t clo, clo1;
134 	uint32_t count;
135 	register_t s;
136 
137 	if (first != 0) {
138 		count = ((uint32_t)et->et_frequency * first) >> 32;
139 
140 		s = intr_disable();
141 		clo = bcm_systimer_tc_read_4(SYSTIMER_CLO);
142 restart:
143 		clo += count;
144 		/*
145 		 * Clear pending interrupts
146 		 */
147 		bcm_systimer_tc_write_4(SYSTIMER_CS, (1 << st->index));
148 		bcm_systimer_tc_write_4(SYSTIMER_C0 + st->index*4, clo);
149 		clo1 = bcm_systimer_tc_read_4(SYSTIMER_CLO);
150 		if ((int32_t)(clo1 - clo) >= 0) {
151 			count *= 2;
152 			clo = clo1;
153 			goto restart;
154 		}
155 		st->enabled = 1;
156 		intr_restore(s);
157 
158 		return (0);
159 	}
160 
161 	return (EINVAL);
162 }
163 
164 static int
165 bcm_systimer_stop(struct eventtimer *et)
166 {
167 	struct systimer *st = et->et_priv;
168 	st->enabled = 0;
169 
170 	return (0);
171 }
172 
173 static int
174 bcm_systimer_intr(void *arg)
175 {
176 	struct systimer *st = (struct systimer *)arg;
177 	uint32_t cs;
178 
179 	cs = bcm_systimer_tc_read_4(SYSTIMER_CS);
180 	if ((cs & (1 << st->index)) == 0)
181 		return (FILTER_STRAY);
182 
183 	/* ACK interrupt */
184 	bcm_systimer_tc_write_4(SYSTIMER_CS, (1 << st->index));
185 	if (st->enabled) {
186 		if (st->et.et_active) {
187 			st->et.et_event_cb(&st->et, st->et.et_arg);
188 		}
189 	}
190 
191 	return (FILTER_HANDLED);
192 }
193 
194 static int
195 bcm_systimer_probe(device_t dev)
196 {
197 
198 	if (!ofw_bus_status_okay(dev))
199 		return (ENXIO);
200 
201 	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
202 		return (ENXIO);
203 
204 	return (BUS_PROBE_DEFAULT);
205 }
206 
207 static int
208 bcm_systimer_attach(device_t dev)
209 {
210 	struct bcm_systimer_softc *sc = device_get_softc(dev);
211 	int err;
212 	int rid = 0;
213 
214 	if (bcm_systimer_sc != NULL)
215 		return (EINVAL);
216 
217 	sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
218 	if (sc->mem_res == NULL) {
219 		device_printf(dev, "could not allocate memory resource\n");
220 		return (ENXIO);
221 	}
222 
223 	sc->bst = rman_get_bustag(sc->mem_res);
224 	sc->bsh = rman_get_bushandle(sc->mem_res);
225 
226 	/* Request the IRQ resources */
227 	err = bus_alloc_resources(dev, bcm_systimer_irq_spec,
228 		sc->irq_res);
229 	if (err) {
230 		device_printf(dev, "Error: could not allocate irq resources\n");
231 		return (ENXIO);
232 	}
233 
234 	/* TODO: get frequency from FDT */
235 	sc->sysclk_freq = DEFAULT_FREQUENCY;
236 
237 	/* Setup and enable the timer */
238 	if (bus_setup_intr(dev, sc->irq_res[DEFAULT_TIMER], INTR_TYPE_CLK,
239 			bcm_systimer_intr, NULL, &sc->st[DEFAULT_TIMER],
240 			&sc->intr_hl[DEFAULT_TIMER]) != 0) {
241 		bus_release_resources(dev, bcm_systimer_irq_spec,
242 			sc->irq_res);
243 		device_printf(dev, "Unable to setup the clock irq handler.\n");
244 		return (ENXIO);
245 	}
246 
247 	sc->st[DEFAULT_TIMER].index = DEFAULT_TIMER;
248 	sc->st[DEFAULT_TIMER].enabled = 0;
249 	sc->st[DEFAULT_TIMER].et.et_name = DEFAULT_TIMER_NAME;
250 	sc->st[DEFAULT_TIMER].et.et_flags = ET_FLAGS_ONESHOT;
251 	sc->st[DEFAULT_TIMER].et.et_quality = 1000;
252 	sc->st[DEFAULT_TIMER].et.et_frequency = sc->sysclk_freq;
253 	sc->st[DEFAULT_TIMER].et.et_min_period =
254 	    (MIN_PERIOD << 32) / sc->st[DEFAULT_TIMER].et.et_frequency + 1;
255 	sc->st[DEFAULT_TIMER].et.et_max_period =
256 	    (0x7ffffffeLLU << 32) / sc->st[DEFAULT_TIMER].et.et_frequency;
257 	sc->st[DEFAULT_TIMER].et.et_start = bcm_systimer_start;
258 	sc->st[DEFAULT_TIMER].et.et_stop = bcm_systimer_stop;
259 	sc->st[DEFAULT_TIMER].et.et_priv = &sc->st[DEFAULT_TIMER];
260 	et_register(&sc->st[DEFAULT_TIMER].et);
261 
262 	bcm_systimer_sc = sc;
263 
264 	if (device_get_unit(dev) == 0)
265 		arm_set_delay(bcm_systimer_delay, sc);
266 
267 	bcm_systimer_tc.tc_frequency = DEFAULT_FREQUENCY;
268 	tc_init(&bcm_systimer_tc);
269 
270 	return (0);
271 }
272 
273 static device_method_t bcm_systimer_methods[] = {
274 	DEVMETHOD(device_probe,		bcm_systimer_probe),
275 	DEVMETHOD(device_attach,	bcm_systimer_attach),
276 	{ 0, 0 }
277 };
278 
279 static driver_t bcm_systimer_driver = {
280 	"systimer",
281 	bcm_systimer_methods,
282 	sizeof(struct bcm_systimer_softc),
283 };
284 
285 DRIVER_MODULE(bcm_systimer, simplebus, bcm_systimer_driver, 0, 0);
286 
287 static void
288 bcm_systimer_delay(int usec, void *arg)
289 {
290 	int32_t counts;
291 	uint32_t first, last;
292 
293 	/* Get the number of times to count */
294 	counts = usec * (bcm_systimer_tc.tc_frequency / 1000000) + 1;
295 
296 	first = bcm_systimer_tc_read_4(SYSTIMER_CLO);
297 
298 	while (counts > 0) {
299 		last = bcm_systimer_tc_read_4(SYSTIMER_CLO);
300 		if (last == first)
301 			continue;
302 		if (last>first) {
303 			counts -= (int32_t)(last - first);
304 		} else {
305 			counts -= (int32_t)((0xFFFFFFFF - first) + last);
306 		}
307 		first = last;
308 	}
309 }
310