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