xref: /netbsd-src/sys/arch/arm/broadcom/bcm2835_tmr.c (revision 1580a27b92f58fcdcb23fdfbc04a7c2b54a0b7c8)
1 /*	$NetBSD: bcm2835_tmr.c,v 1.8 2017/09/21 19:28:14 skrll Exp $	*/
2 
3 /*-
4  * Copyright (c) 2012 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Nick Hudson
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: bcm2835_tmr.c,v 1.8 2017/09/21 19:28:14 skrll Exp $");
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/device.h>
38 #include <sys/kernel.h>
39 #include <sys/timetc.h>
40 #include <sys/bus.h>
41 
42 #include <arm/broadcom/bcm_amba.h>
43 #include <arm/broadcom/bcm2835_intr.h>
44 #include <arm/broadcom/bcm2835reg.h>
45 
46 #define	BCM2835_STIMER_CS	0x00
47 #define	 BCM2835_STIMER_M0	 __BIT(0)
48 #define	 BCM2835_STIMER_M1	 __BIT(1)
49 #define	 BCM2835_STIMER_M2	 __BIT(2)
50 #define	 BCM2835_STIMER_M3	 __BIT(3)
51 #define	BCM2835_STIMER_CLO	0x04
52 #define	BCM2835_STIMER_CHI	0x08
53 #define	BCM2835_STIMER_C0	0x0c
54 #define	BCM2835_STIMER_C1	0x10
55 #define	BCM2835_STIMER_C2	0x14
56 #define	BCM2835_STIMER_C3	0x18
57 
58 #define	BCM2835_STIMER_HZ	1000000
59 
60 static const uint32_t counts_per_usec = (BCM2835_STIMER_HZ / 1000000);
61 static uint32_t counts_per_hz = ~0;
62 
63 struct bcm2835tmr_softc {
64 	device_t		sc_dev;
65 
66 	bus_space_tag_t sc_iot;
67 	bus_space_handle_t sc_ioh;
68 };
69 
70 static int bcmtmr_match(device_t, cfdata_t, void *);
71 static void bcmtmr_attach(device_t, device_t, void *);
72 
73 static int clockhandler(void *);
74 
75 static u_int bcm2835tmr_get_timecount(struct timecounter *);
76 
77 static struct bcm2835tmr_softc *bcm2835tmr_sc;
78 
79 static struct timecounter bcm2835tmr_timecounter = {
80 	.tc_get_timecount = bcm2835tmr_get_timecount,
81 	.tc_poll_pps = 0,
82 	.tc_counter_mask = ~0u,
83 	.tc_frequency = BCM2835_STIMER_HZ,
84 	.tc_name = NULL,			/* set by attach */
85 	.tc_quality = 100,
86 	.tc_priv = NULL,
87 	.tc_next = NULL,
88 };
89 
90 CFATTACH_DECL_NEW(bcmtmr_amba, sizeof(struct bcm2835tmr_softc),
91     bcmtmr_match, bcmtmr_attach, NULL, NULL);
92 
93 /* ARGSUSED */
94 static int
95 bcmtmr_match(device_t parent, cfdata_t match, void *aux)
96 {
97 	struct amba_attach_args *aaa = aux;
98 
99 	if (strcmp(aaa->aaa_name, "bcmtmr") != 0)
100 		return 0;
101 
102 	return 1;
103 }
104 
105 static void
106 bcmtmr_attach(device_t parent, device_t self, void *aux)
107 {
108 	struct bcm2835tmr_softc *sc = device_private(self);
109  	struct amba_attach_args *aaa = aux;
110 
111 	aprint_naive("\n");
112 	aprint_normal(": VC System Timer\n");
113 
114 	if (bcm2835tmr_sc == NULL)
115 		bcm2835tmr_sc = sc;
116 
117 	sc->sc_dev = self;
118 	sc->sc_iot = aaa->aaa_iot;
119 
120 	if (bus_space_map(aaa->aaa_iot, aaa->aaa_addr, BCM2835_STIMER_SIZE, 0,
121 	    &sc->sc_ioh)) {
122 		aprint_error_dev(sc->sc_dev, "unable to map device\n");
123 		return;
124 	}
125 
126 	bcm2835tmr_timecounter.tc_name = device_xname(self);
127 }
128 
129 void
130 cpu_initclocks(void)
131 {
132 	struct bcm2835tmr_softc *sc = bcm2835tmr_sc;
133 	void *clock_ih;
134 	uint32_t stcl;
135 
136 	KASSERT(sc != NULL);
137 
138 	bcm2835tmr_timecounter.tc_priv = sc;
139 
140 	counts_per_hz = BCM2835_STIMER_HZ / hz;
141 
142 	stcl = bus_space_read_4(sc->sc_iot, sc->sc_ioh, BCM2835_STIMER_CLO);
143 	stcl += counts_per_hz;
144 
145 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, BCM2835_STIMER_C3, stcl);
146 	clock_ih = intr_establish(BCM2835_INT_TIMER3, IPL_CLOCK, IST_LEVEL,
147 	    clockhandler, NULL);
148 	if (clock_ih == NULL)
149 		panic("%s: unable to register timer interrupt", __func__);
150 
151 	tc_init(&bcm2835tmr_timecounter);
152 }
153 
154 void
155 delay(unsigned int n)
156 {
157 	struct bcm2835tmr_softc *sc = bcm2835tmr_sc;
158 	uint32_t last, curr;
159 	uint32_t delta, usecs;
160 
161 	KASSERT(sc != NULL);
162 
163 	last = bus_space_read_4(sc->sc_iot, sc->sc_ioh, BCM2835_STIMER_CLO);
164 
165 	delta = usecs = 0;
166 	while (n > usecs) {
167 		curr = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
168 		    BCM2835_STIMER_CLO);
169 
170 		/* Check to see if the timer has wrapped around. */
171 		if (curr < last)
172 			delta += curr + (UINT32_MAX - last);
173 		else
174 			delta += curr - last;
175 
176 		last = curr;
177 
178 		if (delta >= counts_per_usec) {
179 			usecs += delta / counts_per_usec;
180 			delta %= counts_per_usec;
181 		}
182 	}
183 
184 }
185 
186 /*
187  * clockhandler:
188  *
189  *	Handle the hardclock interrupt.
190  */
191 static int
192 clockhandler(void *arg)
193 {
194 	struct bcm2835tmr_softc *sc = bcm2835tmr_sc;
195 	struct clockframe *frame = arg;
196 	uint32_t curr, status;
197 
198 	status = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
199 	    BCM2835_STIMER_CS);
200 
201 	if (!(status & BCM2835_STIMER_M3))
202 		return 0;
203 
204 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, BCM2835_STIMER_CS,
205 	    BCM2835_STIMER_M3);
206 
207 	hardclock(frame);
208 
209 	curr = bus_space_read_4(sc->sc_iot, sc->sc_ioh, BCM2835_STIMER_CLO);
210 
211 	curr += counts_per_hz;
212 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, BCM2835_STIMER_C3, curr);
213 
214 	return 1;
215 }
216 
217 void
218 setstatclockrate(int newhz)
219 {
220 }
221 
222 static u_int
223 bcm2835tmr_get_timecount(struct timecounter *tc)
224 {
225 	struct bcm2835tmr_softc *sc = tc->tc_priv;
226 
227 	return bus_space_read_4(sc->sc_iot, sc->sc_ioh, BCM2835_STIMER_CLO);
228 }
229 
230