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