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/frame.h>
44 #include <machine/intr.h>
45 
46 #include <dev/fdt/fdt_common.h>
47 #include <dev/ofw/openfirm.h>
48 #include <dev/ofw/ofw_bus.h>
49 #include <dev/ofw/ofw_bus_subr.h>
50 
51 #include <machine/bus.h>
52 #include <machine/fdt.h>
53 
54 #define	BCM2835_NUM_TIMERS	4
55 
56 #define	DEFAULT_TIMER		3
57 #define	DEFAULT_FREQUENCY	1000000
58 #define	MIN_PERIOD		5LLU
59 
60 #define	SYSTIMER_CS	0x00
61 #define	SYSTIMER_CLO	0x04
62 #define	SYSTIMER_CHI	0x08
63 #define	SYSTIMER_C0	0x0C
64 #define	SYSTIMER_C1	0x10
65 #define	SYSTIMER_C2	0x14
66 #define	SYSTIMER_C3	0x18
67 
68 struct systimer {
69 	int			index;
70 	bool			enabled;
71 	struct eventtimer	et;
72 };
73 
74 struct bcm_systimer_softc {
75 	struct resource*	mem_res;
76 	struct resource*	irq_res[BCM2835_NUM_TIMERS];
77 	void*			intr_hl[BCM2835_NUM_TIMERS];
78 	uint32_t		sysclk_freq;
79 	bus_space_tag_t		bst;
80 	bus_space_handle_t	bsh;
81 	struct systimer		st[BCM2835_NUM_TIMERS];
82 };
83 
84 static struct resource_spec bcm_systimer_irq_spec[] = {
85 	{ SYS_RES_IRQ,      0,  RF_ACTIVE },
86 	{ SYS_RES_IRQ,      1,  RF_ACTIVE },
87 	{ SYS_RES_IRQ,      2,  RF_ACTIVE },
88 	{ SYS_RES_IRQ,      3,  RF_ACTIVE },
89 	{ -1,               0,  0 }
90 };
91 
92 static struct bcm_systimer_softc *bcm_systimer_sc = NULL;
93 
94 /* Read/Write macros for Timer used as timecounter */
95 #define bcm_systimer_tc_read_4(reg)		\
96 	bus_space_read_4(bcm_systimer_sc->bst, \
97 		bcm_systimer_sc->bsh, reg)
98 
99 #define bcm_systimer_tc_write_4(reg, val)	\
100 	bus_space_write_4(bcm_systimer_sc->bst, \
101 		bcm_systimer_sc->bsh, reg, val)
102 
103 static unsigned bcm_systimer_tc_get_timecount(struct timecounter *);
104 
105 static struct timecounter bcm_systimer_tc = {
106 	.tc_name           = "BCM2835 Timecounter",
107 	.tc_get_timecount  = bcm_systimer_tc_get_timecount,
108 	.tc_poll_pps       = NULL,
109 	.tc_counter_mask   = ~0u,
110 	.tc_frequency      = 0,
111 	.tc_quality        = 1000,
112 };
113 
114 static unsigned
115 bcm_systimer_tc_get_timecount(struct timecounter *tc)
116 {
117 	return bcm_systimer_tc_read_4(SYSTIMER_CLO);
118 }
119 
120 static int
121 bcm_systimer_start(struct eventtimer *et, sbintime_t first, sbintime_t period)
122 {
123 	struct systimer *st = et->et_priv;
124 	uint32_t clo, clo1;
125 	uint32_t count;
126 	register_t s;
127 
128 	if (first != 0) {
129 
130 		count = ((uint32_t)et->et_frequency * first) >> 32;
131 
132 		s = intr_disable();
133 		clo = bcm_systimer_tc_read_4(SYSTIMER_CLO);
134 restart:
135 		clo += count;
136 		/*
137 		 * Clear pending interrupts
138 		 */
139 		bcm_systimer_tc_write_4(SYSTIMER_CS, (1 << st->index));
140 		bcm_systimer_tc_write_4(SYSTIMER_C0 + st->index*4, clo);
141 		clo1 = bcm_systimer_tc_read_4(SYSTIMER_CLO);
142 		if ((int32_t)(clo1 - clo) >= 0) {
143 			count *= 2;
144 			clo = clo1;
145 			goto restart;
146 		}
147 		st->enabled = 1;
148 		intr_restore(s);
149 
150 		return (0);
151 	}
152 
153 	return (EINVAL);
154 }
155 
156 static int
157 bcm_systimer_stop(struct eventtimer *et)
158 {
159 	struct systimer *st = et->et_priv;
160 	st->enabled = 0;
161 
162 	return (0);
163 }
164 
165 static int
166 bcm_systimer_intr(void *arg)
167 {
168 	struct systimer *st = (struct systimer *)arg;
169 	uint32_t cs;
170 
171  	cs = bcm_systimer_tc_read_4(SYSTIMER_CS);
172 	if ((cs & (1 << st->index)) == 0)
173 		return (FILTER_STRAY);
174 
175 	/* ACK interrupt */
176 	bcm_systimer_tc_write_4(SYSTIMER_CS, (1 << st->index));
177 	if (st->enabled) {
178 		if (st->et.et_active) {
179 			st->et.et_event_cb(&st->et, st->et.et_arg);
180 		}
181 	}
182 
183 	return (FILTER_HANDLED);
184 }
185 
186 static int
187 bcm_systimer_probe(device_t dev)
188 {
189 
190 	if (ofw_bus_is_compatible(dev, "broadcom,bcm2835-system-timer")) {
191 		device_set_desc(dev, "BCM2835 System Timer");
192 		return (BUS_PROBE_DEFAULT);
193 	}
194 
195 	return (ENXIO);
196 }
197 
198 static int
199 bcm_systimer_attach(device_t dev)
200 {
201 	struct bcm_systimer_softc *sc = device_get_softc(dev);
202 	int err;
203 	int rid = 0;
204 
205 	if (bcm_systimer_sc != NULL)
206 		return (EINVAL);
207 
208 	sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
209 	if (sc->mem_res == NULL) {
210 		device_printf(dev, "could not allocate memory resource\n");
211 		return (ENXIO);
212 	}
213 
214 	sc->bst = rman_get_bustag(sc->mem_res);
215 	sc->bsh = rman_get_bushandle(sc->mem_res);
216 
217 	/* Request the IRQ resources */
218 	err = bus_alloc_resources(dev, bcm_systimer_irq_spec,
219 		sc->irq_res);
220 	if (err) {
221 		device_printf(dev, "Error: could not allocate irq resources\n");
222 		return (ENXIO);
223 	}
224 
225 	/* TODO: get frequency from FDT */
226 	sc->sysclk_freq = DEFAULT_FREQUENCY;
227 
228 	/* Setup and enable the timer */
229 	if (bus_setup_intr(dev, sc->irq_res[DEFAULT_TIMER], INTR_TYPE_CLK,
230 			bcm_systimer_intr, NULL, &sc->st[DEFAULT_TIMER],
231 			&sc->intr_hl[DEFAULT_TIMER]) != 0) {
232 		bus_release_resources(dev, bcm_systimer_irq_spec,
233 			sc->irq_res);
234 		device_printf(dev, "Unable to setup the clock irq handler.\n");
235 		return (ENXIO);
236 	}
237 
238 	sc->st[DEFAULT_TIMER].index = DEFAULT_TIMER;
239 	sc->st[DEFAULT_TIMER].enabled = 0;
240 	sc->st[DEFAULT_TIMER].et.et_name = malloc(64, M_DEVBUF, M_NOWAIT | M_ZERO);
241 	sprintf(sc->st[DEFAULT_TIMER].et.et_name, "BCM2835 Event Timer %d", DEFAULT_TIMER);
242 	sc->st[DEFAULT_TIMER].et.et_flags = ET_FLAGS_ONESHOT;
243 	sc->st[DEFAULT_TIMER].et.et_quality = 1000;
244 	sc->st[DEFAULT_TIMER].et.et_frequency = sc->sysclk_freq;
245 	sc->st[DEFAULT_TIMER].et.et_min_period =
246 	    (MIN_PERIOD << 32) / sc->st[DEFAULT_TIMER].et.et_frequency + 1;
247 	sc->st[DEFAULT_TIMER].et.et_max_period =
248 	    (0x7ffffffeLLU << 32) / sc->st[DEFAULT_TIMER].et.et_frequency;
249 	sc->st[DEFAULT_TIMER].et.et_start = bcm_systimer_start;
250 	sc->st[DEFAULT_TIMER].et.et_stop = bcm_systimer_stop;
251 	sc->st[DEFAULT_TIMER].et.et_priv = &sc->st[DEFAULT_TIMER];
252 	et_register(&sc->st[DEFAULT_TIMER].et);
253 
254 	bcm_systimer_sc = sc;
255 
256 	bcm_systimer_tc.tc_frequency = DEFAULT_FREQUENCY;
257 	tc_init(&bcm_systimer_tc);
258 
259 	return (0);
260 }
261 
262 static device_method_t bcm_systimer_methods[] = {
263 	DEVMETHOD(device_probe,		bcm_systimer_probe),
264 	DEVMETHOD(device_attach,	bcm_systimer_attach),
265 	{ 0, 0 }
266 };
267 
268 static driver_t bcm_systimer_driver = {
269 	"systimer",
270 	bcm_systimer_methods,
271 	sizeof(struct bcm_systimer_softc),
272 };
273 
274 static devclass_t bcm_systimer_devclass;
275 
276 DRIVER_MODULE(bcm_systimer, simplebus, bcm_systimer_driver, bcm_systimer_devclass, 0, 0);
277 
278 void
279 cpu_initclocks(void)
280 {
281 	cpu_initclocks_bsp();
282 }
283 
284 void
285 DELAY(int usec)
286 {
287 	int32_t counts;
288 	uint32_t first, last;
289 
290 	if (bcm_systimer_sc == NULL) {
291 		for (; usec > 0; usec--)
292 			for (counts = 200; counts > 0; counts--)
293 				/* Prevent gcc from optimizing  out the loop */
294 				cpufunc_nullop();
295 		return;
296 	}
297 
298 	/* Get the number of times to count */
299 	counts = usec * ((bcm_systimer_tc.tc_frequency / 1000000) + 1);
300 
301 	first = bcm_systimer_tc_read_4(SYSTIMER_CLO);
302 
303 	while (counts > 0) {
304 		last = bcm_systimer_tc_read_4(SYSTIMER_CLO);
305 		if (last == first)
306 			continue;
307 		if (last>first) {
308 			counts -= (int32_t)(last - first);
309 		} else {
310 			counts -= (int32_t)((0xFFFFFFFF - first) + last);
311 		}
312 		first = last;
313 	}
314 }
315